blob: d8e36c29fbd4d40fd5a8c79e6b8ede79328400bc [file] [log] [blame]
Gary Boona9ed6f72019-07-22 10:57:56 -04001/*
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
Guillaume Solignacaf5744c2023-01-10 15:37:18 +010016#include <fcntl.h>
Gary Boona9ed6f72019-07-22 10:57:56 -040017#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 */
Damjan Mariondae1c7e2020-10-17 13:32:25 +020055__clib_export clib_error_t *
Gary Boona9ed6f72019-07-22 10:57:56 -040056mpcap_close (mpcap_main_t * pm)
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;
67 pm->flags &= ~MPCAP_FLAG_INIT_DONE;
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 */
Damjan Mariondae1c7e2020-10-17 13:32:25 +020084__clib_export clib_error_t *
Gary Boona9ed6f72019-07-22 10:57:56 -040085mpcap_init (mpcap_main_t * pm)
86{
87 mpcap_file_header_t *fh;
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)
Dave Barach6c84fb22019-07-25 07:22:47 -040095 pm->file_name = "/tmp/vppinfra.mpcap";
Gary Boona9ed6f72019-07-22 10:57:56 -040096
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)
108 pm->max_file_size = MPCAP_DEFAULT_FILE_SIZE;
109
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 ();
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 {
133 clib_error_t *error = clib_error_return_unix (0, "mmap");
134 close (fd);
135 (void) unlink (pm->file_name);
136 return error;
137 }
138 (void) close (fd);
139
140 pm->flags |= MPCAP_FLAG_INIT_DONE | MPCAP_FLAG_WRITE_ENABLE;
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 */
163clib_error_t *
164mpcap_map (mpcap_main_t * pm)
165{
166 clib_error_t *error = 0;
167 int fd = -1;
168 mpcap_file_header_t *fh;
169 mpcap_packet_header_t *ph;
170 struct stat statb;
171 u64 packets_read = 0;
172 u32 min_packet_bytes = ~0;
173 u32 max_packet_bytes = 0;
174
Dave Barach6c84fb22019-07-25 07:22:47 -0400175 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)
Gary Boona9ed6f72019-07-22 10:57:56 -0400183 {
184 error = clib_error_return_unix (0, "stat `%s'", pm->file_name);
185 goto done;
186 }
187
Dave Barach6c84fb22019-07-25 07:22:47 -0400188 if ((statb.st_mode & S_IFREG) == 0)
Gary Boona9ed6f72019-07-22 10:57:56 -0400189 {
Dave Barach6c84fb22019-07-25 07:22:47 -0400190 error = clib_error_return (0, "'%s' is not a regular file",
191 pm->file_name);
Gary Boona9ed6f72019-07-22 10:57:56 -0400192 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
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);
212
213 if (fh->magic != 0xa1b2c3d4)
214 {
215 error = clib_error_return (0, "bad magic `%s'", pm->file_name);
Dave Barach6c84fb22019-07-25 07:22:47 -0400216 pm->flags &= ~(MPCAP_FLAG_INIT_DONE);
217 (void) munmap (pm->file_baseva, pm->max_file_size);
Gary Boona9ed6f72019-07-22 10:57:56 -0400218 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
237 ph = (mpcap_packet_header_t *)
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
244done:
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 */