FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
pcap.c
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.c: 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 #include <sys/fcntl.h>
41 #include <vppinfra/pcap.h>
42 
43 /**
44  * @file
45  * @brief PCAP function.
46  *
47  * Usage:
48  *
49  * <code><pre>
50  * \#include <vppinfra/pcap.h>
51  *
52  * static pcap_main_t pcap = {
53  * .file_name = "/tmp/ip4",
54  * .n_packets_to_capture = 2,
55  * .packet_type = PCAP_PACKET_TYPE_ip,
56  * };
57  * </pre></code>
58  *
59  * To add a buffer:
60  *
61  * <code><pre>pcap_add_buffer (&pcap, vm, pi0, 128);</pre></code>
62  *
63  * File will be written after @c n_packets_to_capture or call to pcap_write (&amp;pcap).
64  *
65 */
66 
67 /**
68  * @brief Close PCAP file
69  *
70  * @return rc - clib_error_t
71  *
72  */
73 __clib_export clib_error_t *
75 {
76  close (pm->file_descriptor);
77  pm->flags &= ~PCAP_MAIN_INIT_DONE;
78  pm->file_descriptor = -1;
79  return 0;
80 }
81 
82 /**
83  * @brief Write PCAP file
84  *
85  * @return rc - clib_error_t
86  *
87  */
88 __clib_export clib_error_t *
90 {
91  clib_error_t *error = 0;
92 
93  if (!(pm->flags & PCAP_MAIN_INIT_DONE))
94  {
96  int n;
97 
98  if (!pm->file_name)
99  pm->file_name = "/tmp/vnet.pcap";
100 
101  pm->file_descriptor =
102  open (pm->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0664);
103  if (pm->file_descriptor < 0)
104  {
105  error =
106  clib_error_return_unix (0, "failed to open `%s'", pm->file_name);
107  goto done;
108  }
109 
110  pm->flags |= PCAP_MAIN_INIT_DONE;
111  pm->n_packets_captured = 0;
112  pm->n_pcap_data_written = 0;
113  clib_spinlock_init (&pm->lock);
114 
115  /* Write file header. */
116  clib_memset (&fh, 0, sizeof (fh));
117  fh.magic = 0xa1b2c3d4;
118  fh.major_version = 2;
119  fh.minor_version = 4;
120  fh.time_zone = 0;
121  fh.max_packet_size_in_bytes = 1 << 16;
122  fh.packet_type = pm->packet_type;
123  n = write (pm->file_descriptor, &fh, sizeof (fh));
124  if (n != sizeof (fh))
125  {
126  if (n < 0)
127  error =
128  clib_error_return_unix (0, "write file header `%s'",
129  pm->file_name);
130  else
131  error =
132  clib_error_return (0, "short write of file header `%s'",
133  pm->file_name);
134  goto done;
135  }
136  }
137 
138  while (vec_len (pm->pcap_data) > pm->n_pcap_data_written)
139  {
140  int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written;
141 
142  n = write (pm->file_descriptor,
144  n);
145 
146  if (n < 0 && unix_error_is_fatal (errno))
147  {
148  error = clib_error_return_unix (0, "write `%s'", pm->file_name);
149  goto done;
150  }
151  pm->n_pcap_data_written += n;
152  }
153 
154  if (pm->n_pcap_data_written >= vec_len (pm->pcap_data))
155  {
157  pm->n_pcap_data_written = 0;
158  }
159 
160 done:
161  if (error)
162  {
163  if (pm->file_descriptor >= 0)
164  close (pm->file_descriptor);
165  }
166  return error;
167 }
168 
169 /**
170  * @brief Read PCAP file
171  *
172  * @return rc - clib_error_t
173  *
174  */
175 __clib_export clib_error_t *
177 {
178  clib_error_t *error = 0;
179  int fd, need_swap, n;
182 
183  fd = open (pm->file_name, O_RDONLY);
184  if (fd < 0)
185  {
186  error = clib_error_return_unix (0, "open `%s'", pm->file_name);
187  goto done;
188  }
189 
190  if (read (fd, &fh, sizeof (fh)) != sizeof (fh))
191  {
192  error =
193  clib_error_return_unix (0, "read file header `%s'", pm->file_name);
194  goto done;
195  }
196 
197  need_swap = 0;
198  if (fh.magic == 0xd4c3b2a1)
199  {
200  need_swap = 1;
201 #define _(t,f) fh.f = clib_byte_swap_##t (fh.f);
203 #undef _
204  }
205 
206  if (fh.magic != 0xa1b2c3d4)
207  {
208  error = clib_error_return (0, "bad magic `%s'", pm->file_name);
209  goto done;
210  }
211 
212  pm->min_packet_bytes = 0;
213  pm->max_packet_bytes = 0;
214  while ((n = read (fd, &ph, sizeof (ph))) != 0)
215  {
216  u8 *data;
217  u64 timestamp;
218  u32 timestamp_sec;
219  u32 timestamp_usec;
220 
221  if (need_swap)
222  {
223 #define _(t,f) ph.f = clib_byte_swap_##t (ph.f);
225 #undef _
226  }
227 
228  data = vec_new (u8, ph.n_bytes_in_packet);
229  if (read (fd, data, ph.n_packet_bytes_stored_in_file) !=
230  ph.n_packet_bytes_stored_in_file)
231  {
232  error = clib_error_return (0, "short read `%s'", pm->file_name);
233  goto done;
234  }
235 
236  if (vec_len (pm->packets_read) == 0)
237  pm->min_packet_bytes = pm->max_packet_bytes = ph.n_bytes_in_packet;
238  else
239  {
240  pm->min_packet_bytes =
241  clib_min (pm->min_packet_bytes, ph.n_bytes_in_packet);
242  pm->max_packet_bytes =
243  clib_max (pm->max_packet_bytes, ph.n_bytes_in_packet);
244  }
245 
246  timestamp_sec = ph.time_in_sec;
247  timestamp_usec = ph.time_in_usec;
248  timestamp = ((u64) timestamp_sec) * 1000000 + (u64) timestamp_usec;
249  vec_add1 (pm->packets_read, data);
251  }
252 
253 done:
254  if (fd >= 0)
255  close (fd);
256  return error;
257 
258 }
259 
260 /*
261  * fd.io coding-style-patch-verification: ON
262  *
263  * Local Variables:
264  * eval: (c-set-style "gnu")
265  * End:
266  */
vec_reset_length
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
Definition: vec_bootstrap.h:194
clib_spinlock_init
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
foreach_pcap_packet_header
#define foreach_pcap_packet_header
Definition: pcap.h:131
pcap_read
__clib_export clib_error_t * pcap_read(pcap_main_t *pm)
Read PCAP file.
Definition: pcap.c:176
pcap_main_t::flags
u32 flags
flags
Definition: pcap.h:173
vec_new
#define vec_new(T, N)
Create new vector of given type and length (unspecified alignment, no header).
Definition: vec.h:365
clib_max
#define clib_max(x, y)
Definition: clib.h:335
pcap_main_t::min_packet_bytes
u32 min_packet_bytes
Min/Max Packet bytes.
Definition: pcap.h:192
pcap_packet_header_t
Packet header.
Definition: pcap.h:143
unix_error_is_fatal
static word unix_error_is_fatal(word error)
Definition: error.h:118
pcap_close
__clib_export clib_error_t * pcap_close(pcap_main_t *pm)
Close PCAP file.
Definition: pcap.c:74
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
pcap_main_t::packet_type
pcap_packet_type_t packet_type
Packet type.
Definition: pcap.h:167
pcap_main_t
PCAP main state data structure.
Definition: pcap.h:155
error
Definition: cJSON.c:88
pcap_main_t::timestamps
u64 * timestamps
Timestamps.
Definition: pcap.h:189
ph
pool_header_t * ph(void *p)
GDB callable function: ph - call pool_header - get pool header.
Definition: gdb_funcs.c:78
pcap_file_header_t
File header struct.
Definition: pcap.h:124
pcap_main_t::lock
clib_spinlock_t lock
spinlock to protect e.g.
Definition: pcap.h:158
pcap_main_t::file_descriptor
int file_descriptor
File descriptor for reading/writing.
Definition: pcap.h:177
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
foreach_pcap_file_header
#define foreach_pcap_file_header
Definition: pcap.h:103
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
pcap.h
PCAP utility definitions.
pcap_main_t::pcap_data
u8 * pcap_data
Vector of pcap data.
Definition: pcap.h:183
clib_min
#define clib_min(x, y)
Definition: clib.h:342
pcap_main_t::packets_read
u8 ** packets_read
Packets read from file.
Definition: pcap.h:186
pcap_main_t::n_pcap_data_written
u32 n_pcap_data_written
Bytes written.
Definition: pcap.h:180
data
u8 data[128]
Definition: ipsec_types.api:92
pcap_main_t::max_packet_bytes
u32 max_packet_bytes
Definition: pcap.h:192
u64
unsigned long u64
Definition: types.h:89
u32
unsigned int u32
Definition: types.h:88
clib_error_return_unix
#define clib_error_return_unix(e, args...)
Definition: error.h:102
pcap_write
__clib_export clib_error_t * pcap_write(pcap_main_t *pm)
Write PCAP file.
Definition: pcap.c:89
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
PCAP_MAIN_INIT_DONE
#define PCAP_MAIN_INIT_DONE
Definition: pcap.h:174
pcap_main_t::file_name
char * file_name
File name of pcap output.
Definition: pcap.h:161
pcap_main_t::n_packets_captured
u32 n_packets_captured
Number of packets currently captured.
Definition: pcap.h:170
timestamp
f64 timestamp
Definition: vpe_types.api:28