FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
mpcap.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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 #include <sys/fcntl.h>
17 #include <vppinfra/mpcap.h>
18 
19 /*
20  * Unfortunately, the "make test" infra won't work with mapped pcap files.
21  * Given enough work [mostly in .py code], one could fix that.
22  */
23 
24 /**
25  * @file
26  * @brief mapped pcap file support
27  *
28  * Usage:
29  *
30  * <code><pre>
31  * \#include <vnet/unix/mpcap.h>
32  *
33  * static mpcap_main_t mpcap = {
34  * .file_name = "/tmp/ip4",
35  * .n_packets_to_capture = 2,
36  * .packet_type = MPCAP_PACKET_TYPE_ip,
37  * };
38  * </pre></code>
39  *
40  * To add a buffer:
41  *
42  * <code><pre>mpcap_add_buffer (&mpcap, vm, pi0, 128);</pre></code>
43  *
44  * File will be written after @c n_packets_to_capture
45  * or call to mpcap_close
46  *
47  */
48 
49 /**
50  * @brief Close a mapped pcap file
51  * @param mpcap_main_t * pm
52  * @return rc - clib_error_t
53  *
54  */
55 __clib_export clib_error_t *
57 {
58  u64 actual_size = pm->current_va - pm->file_baseva;
59 
60  /* Not open? Done... */
61  if ((pm->flags & MPCAP_FLAG_INIT_DONE) == 0)
62  return 0;
63 
64  (void) munmap (pm->file_baseva, pm->max_file_size);
65  pm->file_baseva = 0;
66  pm->current_va = 0;
68 
69  if ((pm->flags & MPCAP_FLAG_WRITE_ENABLE) == 0)
70  return 0;
71 
72  if (truncate (pm->file_name, actual_size) < 0)
73  clib_unix_warning ("setting file size to %llu", actual_size);
74 
75  return 0;
76 }
77 
78 /**
79  * @brief Initialize a mapped pcap file
80  * @param mpcap_main_t * pm
81  * @return rc - clib_error_t
82  *
83  */
84 __clib_export clib_error_t *
86 {
88  u8 zero = 0;
89  int fd;
90 
91  if (pm->flags & MPCAP_FLAG_INIT_DONE)
92  return 0;
93 
94  if (!pm->file_name)
95  pm->file_name = "/tmp/vppinfra.mpcap";
96 
97  if (pm->flags & MPCAP_FLAG_THREAD_SAFE)
98  clib_spinlock_init (&pm->lock);
99 
100  fd = open (pm->file_name, O_CREAT | O_TRUNC | O_RDWR, 0664);
101  if (fd < 0)
102  {
103  return clib_error_return_unix (0, "failed to create `%s'",
104  pm->file_name);
105  }
106 
107  if (pm->max_file_size == 0ULL)
109 
110  /* Round to a multiple of the page size */
113 
114  /* Set file size. */
115  if (lseek (fd, pm->max_file_size - 1, SEEK_SET) == (off_t) - 1)
116  {
117  close (fd);
118  (void) unlink (pm->file_name);
119  return clib_error_return_unix (0, "file size seek");
120  }
121 
122  if (write (fd, &zero, 1) != 1)
123  {
124  close (fd);
125  (void) unlink (pm->file_name);
126  return clib_error_return_unix (0, "file size write");
127  }
128 
129  pm->file_baseva = mmap (0, pm->max_file_size,
130  PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
131  if (pm->file_baseva == (u8 *) MAP_FAILED)
132  {
134  close (fd);
135  (void) unlink (pm->file_name);
136  return error;
137  }
138  (void) close (fd);
139 
141  pm->n_packets_captured = 0;
142  pm->n_mpcap_data_written = 0;
143 
144  /* Initialize file header */
145  fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva;
146  pm->current_va = pm->file_baseva + sizeof (*fh);
147 
148  fh->magic = 0xa1b2c3d4;
149  fh->major_version = 2;
150  fh->minor_version = 4;
151  fh->time_zone = 0;
152  fh->max_packet_size_in_bytes = 1 << 16;
153  fh->packet_type = pm->packet_type;
154  return 0;
155 }
156 
157 
158 /**
159  * @brief mmap a mapped pcap file, e.g. to read from another process
160  * @param pcap_main_t *pm
161  * @return rc - clib_error_t
162  */
163 clib_error_t *
165 {
166  clib_error_t *error = 0;
167  int fd = -1;
170  struct stat statb;
171  u64 packets_read = 0;
172  u32 min_packet_bytes = ~0;
173  u32 max_packet_bytes = 0;
174 
175  fd = open (pm->file_name, O_RDONLY);
176  if (fd < 0)
177  {
178  error = clib_error_return_unix (0, "open `%s'", pm->file_name);
179  goto done;
180  }
181 
182  if (fstat (fd, &statb) < 0)
183  {
184  error = clib_error_return_unix (0, "stat `%s'", pm->file_name);
185  goto done;
186  }
187 
188  if ((statb.st_mode & S_IFREG) == 0)
189  {
190  error = clib_error_return (0, "'%s' is not a regular file",
191  pm->file_name);
192  goto done;
193  }
194 
195  if (statb.st_size < sizeof (*fh) + sizeof (*ph))
196  {
197  error = clib_error_return_unix (0, "`%s' is too short", pm->file_name);
198  goto done;
199  }
200 
201  pm->max_file_size = statb.st_size;
202  pm->file_baseva = mmap (0, statb.st_size, PROT_READ, MAP_SHARED, fd, 0);
203  if (pm->file_baseva == (u8 *) MAP_FAILED)
204  {
205  error = clib_error_return_unix (0, "mmap");
206  goto done;
207  }
208 
210  fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva;
211  ph = (mpcap_packet_header_t *) (fh + 1);
212 
213  if (fh->magic != 0xa1b2c3d4)
214  {
215  error = clib_error_return (0, "bad magic `%s'", pm->file_name);
216  pm->flags &= ~(MPCAP_FLAG_INIT_DONE);
217  (void) munmap (pm->file_baseva, pm->max_file_size);
218  goto done;
219  }
220 
221  /* for the client's convenience, count packets; compute min/max sizes */
222  while (ph < (mpcap_packet_header_t *) pm->file_baseva + pm->max_file_size)
223  {
224  if (ph->n_packet_bytes_stored_in_file == 0)
225  break;
226 
227  packets_read++;
228  min_packet_bytes =
229  ph->n_packet_bytes_stored_in_file <
230  min_packet_bytes ? ph->n_packet_bytes_stored_in_file :
231  min_packet_bytes;
232  max_packet_bytes =
233  ph->n_packet_bytes_stored_in_file >
234  max_packet_bytes ? ph->n_packet_bytes_stored_in_file :
235  max_packet_bytes;
236 
238  (((u8 *) (ph)) + sizeof (*ph) + ph->n_packet_bytes_stored_in_file);
239  }
240  pm->packets_read = packets_read;
241  pm->min_packet_bytes = min_packet_bytes;
242  pm->max_packet_bytes = max_packet_bytes;
243 
244 done:
245  if (fd >= 0)
246  close (fd);
247  return error;
248 }
249 
250 /*
251  * fd.io coding-style-patch-verification: ON
252  *
253  * Local Variables:
254  * eval: (c-set-style "gnu")
255  * End:
256  */
mpcap_main_t::max_packet_bytes
u32 max_packet_bytes
Definition: mpcap.h:149
clib_spinlock_init
static void clib_spinlock_init(clib_spinlock_t *p)
Definition: lock.h:65
mpcap_main_t::current_va
u8 * current_va
current memory address
Definition: mpcap.h:125
mpcap_main_t::flags
u32 flags
flags
Definition: mpcap.h:134
mpcap_packet_header_t
Packet header.
Definition: mpcap.h:92
mpcap_main_t::lock
clib_spinlock_t lock
spinlock, initialized if flagged MPCAP_FLAG_THREAD_SAFE
Definition: mpcap.h:110
mpcap_init
__clib_export clib_error_t * mpcap_init(mpcap_main_t *pm)
Initialize a mapped pcap file.
Definition: mpcap.c:85
mpcap_file_header_t
File header struct.
Definition: mpcap.h:73
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
MPCAP_FLAG_WRITE_ENABLE
#define MPCAP_FLAG_WRITE_ENABLE
Definition: mpcap.h:137
mpcap_main_t::file_baseva
u8 * file_baseva
Base address.
Definition: mpcap.h:122
mpcap_main_t::max_file_size
u64 max_file_size
Maximum file size.
Definition: mpcap.h:119
clib_unix_warning
#define clib_unix_warning(format, args...)
Definition: error.h:68
error
Definition: cJSON.c:88
ph
pool_header_t * ph(void *p)
GDB callable function: ph - call pool_header - get pool header.
Definition: gdb_funcs.c:78
mpcap_map
clib_error_t * mpcap_map(mpcap_main_t *pm)
mmap a mapped pcap file, e.g.
Definition: mpcap.c:164
mpcap_main_t::min_packet_bytes
u32 min_packet_bytes
Min/Max Packet bytes.
Definition: mpcap.h:149
mpcap.h
MPCAP utility definitions.
mpcap_close
__clib_export clib_error_t * mpcap_close(mpcap_main_t *pm)
Close a mapped pcap file.
Definition: mpcap.c:56
u64
unsigned long u64
Definition: types.h:89
MPCAP_FLAG_THREAD_SAFE
#define MPCAP_FLAG_THREAD_SAFE
Definition: mpcap.h:136
MPCAP_DEFAULT_FILE_SIZE
#define MPCAP_DEFAULT_FILE_SIZE
Definition: mpcap.h:153
mpcap_main_t::file_header
mpcap_file_header_t * file_header
Pointer to file header in svm, for ease of updating.
Definition: mpcap.h:131
u32
unsigned int u32
Definition: types.h:88
mpcap_main_t::packet_type
mpcap_packet_type_t packet_type
Packet type.
Definition: mpcap.h:116
mpcap_main_t
MPCAP main state data structure.
Definition: mpcap.h:104
clib_mem_get_page_size
static_always_inline uword clib_mem_get_page_size(void)
Definition: mem.h:471
clib_error_return_unix
#define clib_error_return_unix(e, args...)
Definition: error.h:102
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
MPCAP_FLAG_INIT_DONE
#define MPCAP_FLAG_INIT_DONE
Definition: mpcap.h:135
mpcap_main_t::n_mpcap_data_written
u32 n_mpcap_data_written
Bytes written.
Definition: mpcap.h:140
mpcap_main_t::n_packets_captured
u32 n_packets_captured
Number of packets currently captured.
Definition: mpcap.h:128
mpcap_main_t::packets_read
u64 packets_read
Packets in mapped mpcap file.
Definition: mpcap.h:146
mpcap_main_t::file_name
char * file_name
File name of mpcap output.
Definition: mpcap.h:107