FD.io VPP  v16.06
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 #ifndef included_vnet_pcap_h
41 #define included_vnet_pcap_h
42 
43 #include <vlib/vlib.h>
44 
45 #define foreach_vnet_pcap_packet_type \
46  _ (null, 0) \
47  _ (ethernet, 1) \
48  _ (ppp, 9) \
49  _ (ip, 12) \
50  _ (hdlc, 104)
51 
52 typedef enum {
53 #define _(f,n) PCAP_PACKET_TYPE_##f = (n),
55 #undef _
57 
58 #define foreach_pcap_file_header \
59  /* 0xa1b2c3d4 host byte order. \
60  0xd4c3b2a1 => need to byte swap everything. */ \
61  _ (u32, magic) \
62  \
63  /* Currently major 2 minor 4. */ \
64  _ (u16, major_version) \
65  _ (u16, minor_version) \
66  \
67  /* 0 for GMT. */ \
68  _ (u32, time_zone) \
69  \
70  /* Accuracy of timestamps. Typically set to 0. */ \
71  _ (u32, sigfigs) \
72  \
73  /* Size of largest packet in file. */ \
74  _ (u32, max_packet_size_in_bytes) \
75  \
76  /* One of vnet_pcap_packet_type_t. */ \
77  _ (u32, packet_type)
78 
79 /* File header. */
80 typedef struct {
81 #define _(t, f) t f;
83 #undef _
85 
86 #define foreach_pcap_packet_header \
87  /* Time stamp in seconds and microseconds. */ \
88  _ (u32, time_in_sec) \
89  _ (u32, time_in_usec) \
90  \
91  /* Number of bytes stored in file and size of actual packet. */ \
92  _ (u32, n_packet_bytes_stored_in_file) \
93  _ (u32, n_bytes_in_packet)
94 
95 /* Packet header. */
96 typedef struct {
97 #define _(t, f) t f;
99 #undef _
100 
101  /* Packet data follows. */
102  u8 data[0];
104 
105 typedef struct {
106  /* File name of pcap output. */
107  char * file_name;
108 
109  /* Number of packets to capture. */
111 
113 
114  /* Number of packets currently captured. */
116 
118 #define PCAP_MAIN_INIT_DONE (1 << 0)
119 
120  /* File descriptor for reading/writing. */
122 
124 
125  /* Vector of pcap data. */
127 
128  /* Packets read from file. */
130 
131  u32 min_packet_bytes, max_packet_bytes;
132 } pcap_main_t;
133 
134 /* Write out data to output file. */
136 
138 
139 static inline void *
141  f64 time_now,
142  u32 n_bytes_in_trace,
143  u32 n_bytes_in_packet)
144 {
146  u8 * d;
147 
148  vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace);
149  h = (void *) (d);
150  h->time_in_sec = time_now;
151  h->time_in_usec = 1e6*(time_now - h->time_in_sec);
152  h->n_packet_bytes_stored_in_file = n_bytes_in_trace;
153  h->n_bytes_in_packet = n_bytes_in_packet;
154  pm->n_packets_captured++;
155  return h->data;
156 }
157 
158 static inline void
160  vlib_main_t * vm, u32 buffer_index,
161  u32 n_bytes_in_trace)
162 {
163  vlib_buffer_t * b = vlib_get_buffer (vm, buffer_index);
164  u32 n = vlib_buffer_length_in_chain (vm, b);
165  i32 n_left = clib_min (n_bytes_in_trace, n);
166  f64 time_now = vlib_time_now (vm);
167  void * d;
168 
169  d = pcap_add_packet (pm, time_now, n_bytes_in_trace, n_left);
170  while (1)
171  {
172  u32 copy_length = clib_min ((u32) n_left, b->current_length);
173  clib_memcpy (d, b->data + b->current_data, copy_length);
174  n_left -= b->current_length;
175  if (n_left <= 0)
176  break;
177  d += b->current_length;
179  b = vlib_get_buffer (vm, b->next_buffer);
180  }
181 
182  /* Flush output vector. */
183  if (vec_len (pm->pcap_data) >= 64*1024
185  pcap_write (pm);
186 }
187 
188 #endif /* included_vnet_pcap_h */
char * file_name
Definition: pcap.h:107
#define clib_min(x, y)
Definition: clib.h:295
u32 flags
Definition: pcap.h:117
int file_descriptor
Definition: pcap.h:121
u32 n_packets_to_capture
Definition: pcap.h:110
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
clib_error_t * pcap_write(pcap_main_t *pm)
Definition: pcap.c:62
static void * pcap_add_packet(pcap_main_t *pm, f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet)
Definition: pcap.h:140
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:77
foreach_pcap_packet_header u8 data[0]
Definition: pcap.h:102
int i32
Definition: types.h:81
#define foreach_pcap_packet_header
Definition: pcap.h:86
always_inline 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:112
u32 n_pcap_data_written
Definition: pcap.h:123
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:93
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:81
pcap_packet_type_t
Definition: pcap.h:52
u8 * pcap_data
Definition: pcap.h:126
static void pcap_add_buffer(pcap_main_t *pm, vlib_main_t *vm, u32 buffer_index, u32 n_bytes_in_trace)
Definition: pcap.h:159
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:112
clib_error_t * pcap_read(pcap_main_t *pm)
Definition: pcap.c:142
u32 min_packet_bytes
Definition: pcap.h:131
pcap_packet_type_t packet_type
Definition: pcap.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:140
unsigned char u8
Definition: types.h:56
u8 data[0]
Packet data.
Definition: buffer.h:150
always_inline f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
u8 ** packets_read
Definition: pcap.h:129
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
u32 n_packets_captured
Definition: pcap.h:115
#define foreach_vnet_pcap_packet_type
Definition: pcap.h:45
#define foreach_pcap_file_header
Definition: pcap.h:58