FD.io VPP  v18.01.2-1-g9b554f3
Vector Packet Processing
pcap.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  * pcap.h: libpcap packet capture format
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 /**
40  * @file
41  * @brief PCAP utility definitions
42  */
43 #ifndef included_vnet_pcap_h
44 #define included_vnet_pcap_h
45 
46 #include <vlib/vlib.h>
47 
48 /**
49  * @brief Packet types supported by PCAP
50  *
51  * null 0
52  * ethernet 1
53  * ppp 9
54  * ip 12
55  * hdlc 104
56  */
57 #define foreach_vnet_pcap_packet_type \
58  _ (null, 0) \
59  _ (ethernet, 1) \
60  _ (ppp, 9) \
61  _ (ip, 12) \
62  _ (hdlc, 104)
63 
64 typedef enum
65 {
66 #define _(f,n) PCAP_PACKET_TYPE_##f = (n),
68 #undef _
70 
71 #define foreach_pcap_file_header \
72  /** 0xa1b2c3d4 host byte order. \
73  0xd4c3b2a1 => need to byte swap everything. */ \
74  _ (u32, magic) \
75  \
76  /** Currently major 2 minor 4. */ \
77  _ (u16, major_version) \
78  _ (u16, minor_version) \
79  \
80  /** 0 for GMT. */ \
81  _ (u32, time_zone) \
82  \
83  /** Accuracy of timestamps. Typically set to 0. */ \
84  _ (u32, sigfigs) \
85  \
86  /** Size of largest packet in file. */ \
87  _ (u32, max_packet_size_in_bytes) \
88  \
89  /** One of vnet_pcap_packet_type_t. */ \
90  _ (u32, packet_type)
91 
92 /** File header struct */
93 typedef struct
94 {
95 #define _(t, f) t f;
97 #undef _
99 
100 #define foreach_pcap_packet_header \
101  /** Time stamp in seconds */ \
102  _ (u32, time_in_sec) \
103  /** Time stamp in microseconds. */ \
104  _ (u32, time_in_usec) \
105  \
106  /** Number of bytes stored in file. */ \
107  _ (u32, n_packet_bytes_stored_in_file) \
108  /** Number of bytes in actual packet. */ \
109  _ (u32, n_bytes_in_packet)
110 
111 /** Packet header. */
112 typedef struct
113 {
114 #define _(t, f) t f;
116 #undef _
117  /** Packet data follows. */
118  u8 data[0];
120 
121 /**
122  * @brief PCAP main state data structure
123  */
124 typedef struct
125 {
126  /** File name of pcap output. */
127  char *file_name;
128 
129  /** Number of packets to capture. */
131 
132  /** Packet type */
134 
135  /** Number of packets currently captured. */
137 
138  /** flags */
140 #define PCAP_MAIN_INIT_DONE (1 << 0)
141 
142  /** File descriptor for reading/writing. */
144 
145  /** Bytes written */
147 
148  /** Vector of pcap data. */
150 
151  /** Packets read from file. */
153 
154  /** Min/Max Packet bytes */
155  u32 min_packet_bytes, max_packet_bytes;
156 } pcap_main_t;
157 
158 /** Write out data to output file. */
160 
161 /** Read data from file. */
163 
164 /**
165  * @brief Add packet
166  *
167  * @param *pm - pcap_main_t
168  * @param time_now - f64
169  * @param n_bytes_in_trace - u32
170  * @param n_bytes_in_packet - u32
171  *
172  * @return Packet Data
173  *
174  */
175 static inline void *
177  f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
178 {
180  u8 *d;
181 
182  vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace);
183  h = (void *) (d);
184  h->time_in_sec = time_now;
185  h->time_in_usec = 1e6 * (time_now - h->time_in_sec);
186  h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
187  h->n_bytes_in_packet = n_bytes_in_packet;
188  pm->n_packets_captured++;
189  return h->data;
190 }
191 
192 /**
193  * @brief Add buffer (vlib_buffer_t) to the trace
194  *
195  * @param *pm - pcap_main_t
196  * @param *vm - vlib_main_t
197  * @param buffer_index - u32
198  * @param n_bytes_in_trace - u32
199  *
200  */
201 static inline void
203  vlib_main_t * vm, u32 buffer_index, u32 n_bytes_in_trace)
204 {
205  vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
206  u32 n = vlib_buffer_length_in_chain (vm, b);
207  i32 n_left = clib_min (n_bytes_in_trace, n);
208  f64 time_now = vlib_time_now (vm);
209  void *d;
210 
211  d = pcap_add_packet (pm, time_now, n_left, n);
212  while (1)
213  {
214  u32 copy_length = clib_min ((u32) n_left, b->current_length);
215  clib_memcpy (d, b->data + b->current_data, copy_length);
216  n_left -= b->current_length;
217  if (n_left <= 0)
218  break;
219  d += b->current_length;
221  b = vlib_get_buffer (vm, b->next_buffer);
222  }
223 
224  /** Flush output vector. */
225  if (vec_len (pm->pcap_data) >= 64 * 1024
227  pcap_write (pm);
228 }
229 
230 #endif /* included_vnet_pcap_h */
231 
232 /*
233  * fd.io coding-style-patch-verification: ON
234  *
235  * Local Variables:
236  * eval: (c-set-style "gnu")
237  * End:
238  */
char * file_name
File name of pcap output.
Definition: pcap.h:127
#define clib_min(x, y)
Definition: clib.h:340
u32 flags
flags
Definition: pcap.h:139
int file_descriptor
File descriptor for reading/writing.
Definition: pcap.h:143
u32 n_packets_to_capture
Number of packets to capture.
Definition: pcap.h:130
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:224
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:557
clib_error_t * pcap_write(pcap_main_t *pm)
Write out data to output file.
Definition: pcap.c:89
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:107
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.h:176
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:95
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:68
PCAP main state data structure.
Definition: pcap.h:124
foreach_pcap_packet_header u8 data[0]
Packet data follows.
Definition: pcap.h:118
int i32
Definition: types.h:81
#define foreach_pcap_packet_header
Definition: pcap.h:100
u32 n_pcap_data_written
Bytes written.
Definition: pcap.h:146
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:72
pcap_packet_type_t
Definition: pcap.h:64
u8 * pcap_data
Vector of pcap data.
Definition: pcap.h:149
static void pcap_add_buffer(pcap_main_t *pm, vlib_main_t *vm, u32 buffer_index, u32 n_bytes_in_trace)
Add buffer (vlib_buffer_t) to the trace.
Definition: pcap.h:202
vlib_main_t * vm
Definition: buffer.c:283
vec_header_t h
Definition: buffer.c:282
#define clib_memcpy(a, b, c)
Definition: string.h:75
Packet header.
Definition: pcap.h:112
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:109
clib_error_t * pcap_read(pcap_main_t *pm)
Read data from file.
Definition: pcap.c:178
u32 min_packet_bytes
Min/Max Packet bytes.
Definition: pcap.h:155
pcap_packet_type_t packet_type
Packet type.
Definition: pcap.h:133
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
u8 data[0]
Packet data.
Definition: buffer.h:159
File header struct.
Definition: pcap.h:93
u8 ** packets_read
Packets read from file.
Definition: pcap.h:152
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:75
u32 n_packets_captured
Number of packets currently captured.
Definition: pcap.h:136
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
#define foreach_vnet_pcap_packet_type
Packet types supported by PCAP.
Definition: pcap.h:57
#define foreach_pcap_file_header
Definition: pcap.h:71