FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
mpcap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 /**
17  * @file
18  * @brief MPCAP utility definitions
19  */
20 #ifndef included_vnet_mpcap_h
21 #define included_vnet_mpcap_h
22 
23 #include <vlib/vlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <vppinfra/time_range.h>
28 
29 /**
30  * @brief Packet types supported by MPCAP
31  *
32  * null 0
33  * ethernet 1
34  * ppp 9
35  * ip 12
36  * hdlc 104
37  */
38 #define foreach_vnet_mpcap_packet_type \
39  _ (null, 0) \
40  _ (ethernet, 1) \
41  _ (ppp, 9) \
42  _ (ip, 12) \
43  _ (hdlc, 104)
44 
45 typedef enum
46 {
47 #define _(f,n) MPCAP_PACKET_TYPE_##f = (n),
49 #undef _
51 
52 #define foreach_mpcap_file_header \
53  /** 0xa1b2c3d4 host byte order. \
54  0xd4c3b2a1 => need to byte swap everything. */ \
55  _ (u32, magic) \
56  \
57  /** Currently major 2 minor 4. */ \
58  _ (u16, major_version) \
59  _ (u16, minor_version) \
60  \
61  /** 0 for GMT. */ \
62  _ (u32, time_zone) \
63  \
64  /** Accuracy of timestamps. Typically set to 0. */ \
65  _ (u32, sigfigs) \
66  \
67  /** Size of largest packet in file. */ \
68  _ (u32, max_packet_size_in_bytes) \
69  \
70  /** One of vnet_mpcap_packet_type_t. */ \
71  _ (u32, packet_type)
72 
73 /** File header struct */
74 typedef struct
75 {
76 #define _(t, f) t f;
78 #undef _
80 
81 #define foreach_mpcap_packet_header \
82  /** Time stamp in seconds */ \
83  _ (u32, time_in_sec) \
84  /** Time stamp in microseconds. */ \
85  _ (u32, time_in_usec) \
86  \
87  /** Number of bytes stored in file. */ \
88  _ (u32, n_packet_bytes_stored_in_file) \
89  /** Number of bytes in actual packet. */ \
90  _ (u32, n_bytes_in_packet)
91 
92 /** Packet header. */
93 typedef struct
94 {
95 #define _(t, f) t f;
97 #undef _
98  /** Packet data follows. */
99  u8 data[0];
101 
102 /**
103  * @brief MPCAP main state data structure
104  */
105 typedef struct
106 {
107  /** File name of mpcap output. */
108  char *file_name;
109 
110  /** spinlock, initialized if flagged MPCAP_FLAG_THREAD_SAFE */
112 
113  /** Number of packets to capture. */
115 
116  /** Packet type */
118 
119  /** Maximum file size */
121 
122  /** Base address */
124 
125  /** current memory address */
127 
128  /** Number of packets currently captured. */
130 
131  /** Pointer to file header in svm, for ease of updating */
133 
134  /** flags */
136 #define MPCAP_FLAG_INIT_DONE (1 << 0)
137 #define MPCAP_FLAG_THREAD_SAFE (1 << 1)
138 #define MPCAP_FLAG_WRITE_ENABLE (1 << 2)
139 
140  /** Bytes written */
142 
143  /** Vector of mpcap data. */
145 
146  /** Packets in mapped mpcap file. */
148 
149  /** Min/Max Packet bytes */
150  u32 min_packet_bytes, max_packet_bytes;
151 } mpcap_main_t;
152 
153 /* Some sensible default size */
154 #define MPCAP_DEFAULT_FILE_SIZE (10<<20)
155 
156 /** initialize a mpcap file (for writing) */
158 
159 /** Flush / unmap a mpcap file */
161 
162 /** mmap a mpcap data file. */
164 
165 /**
166  * @brief Add packet
167  *
168  * @param *pm - mpcap_main_t
169  * @param time_now - f64
170  * @param n_bytes_in_trace - u32
171  * @param n_bytes_in_packet - u32
172  *
173  * @return Packet Data
174  *
175  */
176 static inline void *
178  f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
179 {
181  u8 *d;
182 
183  /* File already closed? */
184  if (PREDICT_FALSE (pm->flags & MPCAP_FLAG_INIT_DONE) == 0)
185  return 0;
186 
187  d = pm->current_va;
188  pm->current_va += sizeof (h[0]) + n_bytes_in_trace;
189 
190  /* Out of space? */
191  if (PREDICT_FALSE (pm->current_va >= pm->file_baseva + pm->max_file_size))
192  return 0;
193  h = (void *) (d);
194  h->time_in_sec = time_now;
195  h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
196  h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
197  h->n_bytes_in_packet = n_bytes_in_packet;
198  pm->n_packets_captured++;
199  return h->data;
200 }
201 
202 /**
203  * @brief Add buffer (vlib_buffer_t) to the trace
204  *
205  * @param *pm - mpcap_main_t
206  * @param *vm - vlib_main_t
207  * @param time_now - f64
208  * @param buffer_index - u32
209  * @param n_bytes_in_trace - u32
210  *
211  */
212 static inline void
214  vlib_main_t * vm,
215  f64 time_now, u32 buffer_index, u32 n_bytes_in_trace)
216 {
217  vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
218  u32 n = vlib_buffer_length_in_chain (vm, b);
219  i32 n_left = clib_min (n_bytes_in_trace, n);
220  void *d;
221 
223 
224  d = mpcap_add_packet (pm, time_now, n_left, n);
225  if (PREDICT_FALSE (d == 0))
226  {
227  mpcap_close (pm);
229  return;
230  }
231 
232  while (1)
233  {
234  u32 copy_length = clib_min ((u32) n_left, b->current_length);
235  clib_memcpy (d, b->data + b->current_data, copy_length);
236  n_left -= b->current_length;
237  if (n_left <= 0)
238  break;
239  d += b->current_length;
240  ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
241  b = vlib_get_buffer (vm, b->next_buffer);
242  }
244  mpcap_close (pm);
245 
247 }
248 
249 #endif /* included_vnet_mpcap_h */
250 
251 /*
252  * fd.io coding-style-patch-verification: ON
253  *
254  * Local Variables:
255  * eval: (c-set-style "gnu")
256  * End:
257  */
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
u32 min_packet_bytes
Min/Max Packet bytes.
Definition: mpcap.h:150
#define foreach_mpcap_file_header
Definition: mpcap.h:52
#define clib_min(x, y)
Definition: clib.h:295
#define foreach_vnet_mpcap_packet_type
Packet types supported by MPCAP.
Definition: mpcap.h:38
#define MPCAP_FLAG_INIT_DONE
Definition: mpcap.h:136
u8 * file_baseva
Base address.
Definition: mpcap.h:123
static void * mpcap_add_packet(mpcap_main_t *pm, f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
Add packet.
Definition: mpcap.h:177
File header struct.
Definition: mpcap.h:74
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
unsigned long u64
Definition: types.h:89
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:110
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
clib_error_t * mpcap_map(mpcap_main_t *pm)
mmap a mpcap data file.
Definition: mpcap.c:164
u32 n_mpcap_data_written
Bytes written.
Definition: mpcap.h:141
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
#define foreach_mpcap_packet_header
Definition: mpcap.h:81
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
#define clib_memcpy(d, s, n)
Definition: string.h:180
unsigned int u32
Definition: types.h:88
clib_error_t * mpcap_close(mpcap_main_t *pm)
Flush / unmap a mpcap file.
Definition: mpcap.c:56
char * file_name
File name of mpcap output.
Definition: mpcap.h:108
static void mpcap_add_buffer(mpcap_main_t *pm, vlib_main_t *vm, f64 time_now, u32 buffer_index, u32 n_bytes_in_trace)
Add buffer (vlib_buffer_t) to the trace.
Definition: mpcap.h:213
#define PREDICT_FALSE(x)
Definition: clib.h:111
MPCAP main state data structure.
Definition: mpcap.h:105
vlib_main_t * vm
Definition: in2out_ed.c:1810
Packet header.
Definition: mpcap.h:93
mpcap_packet_type_t
Definition: mpcap.h:45
u32 flags
flags
Definition: mpcap.h:135
signed int i32
Definition: types.h:77
#define ASSERT(truth)
u8 data[128]
Definition: ipsec_types.api:87
u32 n_packets_captured
Number of packets currently captured.
Definition: mpcap.h:129
clib_error_t * mpcap_init(mpcap_main_t *pm)
initialize a mpcap file (for writing)
Definition: mpcap.c:85
clib_spinlock_t lock
spinlock, initialized if flagged MPCAP_FLAG_THREAD_SAFE
Definition: mpcap.h:111
mpcap_file_header_t * file_header
Pointer to file header in svm, for ease of updating.
Definition: mpcap.h:132
u8 * mpcap_data
Vector of mpcap data.
Definition: mpcap.h:144
foreach_mpcap_packet_header u8 data[0]
Packet data follows.
Definition: mpcap.h:99
mpcap_packet_type_t packet_type
Packet type.
Definition: mpcap.h:117
u64 packets_read
Packets in mapped mpcap file.
Definition: mpcap.h:147
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
VLIB buffer representation.
Definition: buffer.h:102
u32 n_packets_to_capture
Number of packets to capture.
Definition: mpcap.h:114
u64 max_file_size
Maximum file size.
Definition: mpcap.h:120
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:95
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
u8 * current_va
current memory address
Definition: mpcap.h:126