FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
pcap_funcs.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef included_vppinfra_pcap_funcs_h
17 #define included_vppinfra_pcap_funcs_h
18 
19 /** Write out data to output file. */
21 
22 /** Read data from file. */
24 
25 /**
26  * @brief Add packet
27  *
28  * @param *pm - pcap_main_t
29  * @param time_now - f64
30  * @param n_bytes_in_trace - u32
31  * @param n_bytes_in_packet - u32
32  *
33  * @return Packet Data
34  *
35  */
36 static inline void *
38  f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
39 {
41  u8 *d;
42 
43  vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace);
44  h = (void *) (d);
45  h->time_in_sec = time_now;
46  h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
47  h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
48  h->n_bytes_in_packet = n_bytes_in_packet;
49  pm->n_packets_captured++;
50  return h->data;
51 }
52 
53 /**
54  * @brief Add buffer (vlib_buffer_t) to the trace
55  *
56  * @param *pm - pcap_main_t
57  * @param *vm - vlib_main_t
58  * @param buffer_index - u32
59  * @param n_bytes_in_trace - u32
60  *
61  */
62 static inline void
64  struct vlib_main_t *vm, u32 buffer_index,
65  u32 n_bytes_in_trace)
66 {
67  vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
68  u32 n = vlib_buffer_length_in_chain (vm, b);
69  i32 n_left = clib_min (n_bytes_in_trace, n);
70  f64 time_now = vlib_time_now (vm);
71  void *d;
72 
74  {
76  d = pcap_add_packet (pm, time_now, n_left, n);
77  while (1)
78  {
79  u32 copy_length = clib_min ((u32) n_left, b->current_length);
80  clib_memcpy_fast (d, b->data + b->current_data, copy_length);
81  n_left -= b->current_length;
82  if (n_left <= 0)
83  break;
84  d += b->current_length;
85  ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
86  b = vlib_get_buffer (vm, b->next_buffer);
87  }
89  }
90 }
91 
92 #endif /* included_vppinfra_pcap_funcs_h */
93 
94 /*
95  * fd.io coding-style-patch-verification: ON
96  *
97  * Local Variables:
98  * eval: (c-set-style "gnu")
99  * End:
100  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
#define clib_min(x, y)
Definition: clib.h:295
u32 n_packets_to_capture
Number of packets to capture.
Definition: pcap.h:165
#define PREDICT_TRUE(x)
Definition: clib.h:112
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:98
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
u8 data[0]
Packet data.
Definition: buffer.h:181
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:560
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:366
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
PCAP main state data structure.
Definition: pcap.h:156
clib_spinlock_t lock
spinlock to protect e.g.
Definition: pcap.h:159
foreach_pcap_packet_header u8 data[0]
Packet data follows.
Definition: pcap.h:150
unsigned int u32
Definition: types.h:88
clib_error_t * pcap_read(pcap_main_t *pm)
Read data from file.
Definition: pcap.c:179
u8 * pcap_data
Vector of pcap data.
Definition: pcap.h:184
static void * pcap_add_packet(pcap_main_t *pm, f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
Add packet.
Definition: pcap_funcs.h:37
vlib_main_t * vm
Definition: buffer.c:312
Packet header.
Definition: pcap.h:144
signed int i32
Definition: types.h:77
#define ASSERT(truth)
clib_error_t * pcap_write(pcap_main_t *pm)
Write out data to output file.
Definition: pcap.c:89
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
VLIB buffer representation.
Definition: buffer.h:102
static void pcap_add_buffer(pcap_main_t *pm, struct vlib_main_t *vm, u32 buffer_index, u32 n_bytes_in_trace)
Add buffer (vlib_buffer_t) to the trace.
Definition: pcap_funcs.h:63
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:82
u32 n_packets_captured
Number of packets currently captured.
Definition: pcap.h:171
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85