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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <sys/fcntl.h>
17 #include <vppinfra/mpcap.h>
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.
26 * @brief mapped pcap file support
31 * \#include <vnet/unix/mpcap.h>
33 * static mpcap_main_t mpcap = {
34 * .file_name = "/tmp/ip4",
35 * .n_packets_to_capture = 2,
36 * .packet_type = MPCAP_PACKET_TYPE_ip,
42 * <code><pre>mpcap_add_buffer (&mpcap, vm, pi0, 128);</pre></code>
44 * File will be written after @c n_packets_to_capture
45 * or call to mpcap_close
50 * @brief Close a mapped pcap file
51 * @param mpcap_main_t * pm
52 * @return rc - clib_error_t
56 mpcap_close (mpcap_main_t * pm)
58 u64 actual_size = pm->current_va - pm->file_baseva;
60 /* Not open? Done... */
61 if ((pm->flags & MPCAP_FLAG_INIT_DONE) == 0)
64 (void) munmap (pm->file_baseva, pm->max_file_size);
67 pm->flags &= ~MPCAP_FLAG_INIT_DONE;
69 if ((pm->flags & MPCAP_FLAG_WRITE_ENABLE) == 0)
72 if (truncate (pm->file_name, actual_size) < 0)
73 clib_unix_warning ("setting file size to %llu", actual_size);
79 * @brief Initialize a mapped pcap file
80 * @param mpcap_main_t * pm
81 * @return rc - clib_error_t
85 mpcap_init (mpcap_main_t * pm)
87 mpcap_file_header_t *fh;
91 if (pm->flags & MPCAP_FLAG_INIT_DONE)
95 pm->file_name = "/tmp/vppinfra.mpcap";
97 if (pm->flags & MPCAP_FLAG_THREAD_SAFE)
98 clib_spinlock_init (&pm->lock);
100 fd = open (pm->file_name, O_CREAT | O_TRUNC | O_RDWR, 0664);
103 return clib_error_return_unix (0, "failed to create `%s'",
107 if (pm->max_file_size == 0ULL)
108 pm->max_file_size = MPCAP_DEFAULT_FILE_SIZE;
110 /* Round to a multiple of the page size */
111 pm->max_file_size += (u64) clib_mem_get_page_size ();
112 pm->max_file_size &= ~(u64) clib_mem_get_page_size ();
115 if (lseek (fd, pm->max_file_size - 1, SEEK_SET) == (off_t) - 1)
118 (void) unlink (pm->file_name);
119 return clib_error_return_unix (0, "file size seek");
122 if (write (fd, &zero, 1) != 1)
125 (void) unlink (pm->file_name);
126 return clib_error_return_unix (0, "file size write");
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)
133 clib_error_t *error = clib_error_return_unix (0, "mmap");
135 (void) unlink (pm->file_name);
140 pm->flags |= MPCAP_FLAG_INIT_DONE | MPCAP_FLAG_WRITE_ENABLE;
141 pm->n_packets_captured = 0;
142 pm->n_mpcap_data_written = 0;
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);
148 fh->magic = 0xa1b2c3d4;
149 fh->major_version = 2;
150 fh->minor_version = 4;
152 fh->max_packet_size_in_bytes = 1 << 16;
153 fh->packet_type = pm->packet_type;
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
164 mpcap_map (mpcap_main_t * pm)
166 clib_error_t *error = 0;
168 mpcap_file_header_t *fh;
169 mpcap_packet_header_t *ph;
171 u64 packets_read = 0;
172 u32 min_packet_bytes = ~0;
173 u32 max_packet_bytes = 0;
175 fd = open (pm->file_name, O_RDONLY);
178 error = clib_error_return_unix (0, "open `%s'", pm->file_name);
182 if (fstat (fd, &statb) < 0)
184 error = clib_error_return_unix (0, "stat `%s'", pm->file_name);
188 if ((statb.st_mode & S_IFREG) == 0)
190 error = clib_error_return (0, "'%s' is not a regular file",
195 if (statb.st_size < sizeof (*fh) + sizeof (*ph))
197 error = clib_error_return_unix (0, "`%s' is too short", pm->file_name);
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)
205 error = clib_error_return_unix (0, "mmap");
209 pm->flags |= MPCAP_FLAG_INIT_DONE;
210 fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva;
211 ph = (mpcap_packet_header_t *) (fh + 1);
213 if (fh->magic != 0xa1b2c3d4)
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);
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)
224 if (ph->n_packet_bytes_stored_in_file == 0)
229 ph->n_packet_bytes_stored_in_file <
230 min_packet_bytes ? ph->n_packet_bytes_stored_in_file :
233 ph->n_packet_bytes_stored_in_file >
234 max_packet_bytes ? ph->n_packet_bytes_stored_in_file :
237 ph = (mpcap_packet_header_t *)
238 (((u8 *) (ph)) + sizeof (*ph) + ph->n_packet_bytes_stored_in_file);
240 pm->packets_read = packets_read;
241 pm->min_packet_bytes = min_packet_bytes;
242 pm->max_packet_bytes = max_packet_bytes;
251 * fd.io coding-style-patch-verification: ON
254 * eval: (c-set-style "gnu")