Line data Source code
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 <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 *
56 1 : mpcap_close (mpcap_main_t * pm)
57 : {
58 1 : u64 actual_size = pm->current_va - pm->file_baseva;
59 :
60 : /* Not open? Done... */
61 1 : if ((pm->flags & MPCAP_FLAG_INIT_DONE) == 0)
62 0 : return 0;
63 :
64 1 : (void) munmap (pm->file_baseva, pm->max_file_size);
65 1 : pm->file_baseva = 0;
66 1 : pm->current_va = 0;
67 1 : pm->flags &= ~MPCAP_FLAG_INIT_DONE;
68 :
69 1 : if ((pm->flags & MPCAP_FLAG_WRITE_ENABLE) == 0)
70 0 : return 0;
71 :
72 1 : if (truncate (pm->file_name, actual_size) < 0)
73 0 : clib_unix_warning ("setting file size to %llu", actual_size);
74 :
75 1 : 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 *
85 1 : mpcap_init (mpcap_main_t * pm)
86 : {
87 : mpcap_file_header_t *fh;
88 1 : u8 zero = 0;
89 : int fd;
90 :
91 1 : if (pm->flags & MPCAP_FLAG_INIT_DONE)
92 0 : return 0;
93 :
94 1 : if (!pm->file_name)
95 0 : pm->file_name = "/tmp/vppinfra.mpcap";
96 :
97 1 : if (pm->flags & MPCAP_FLAG_THREAD_SAFE)
98 0 : clib_spinlock_init (&pm->lock);
99 :
100 1 : fd = open (pm->file_name, O_CREAT | O_TRUNC | O_RDWR, 0664);
101 1 : if (fd < 0)
102 : {
103 0 : return clib_error_return_unix (0, "failed to create `%s'",
104 : pm->file_name);
105 : }
106 :
107 1 : if (pm->max_file_size == 0ULL)
108 1 : pm->max_file_size = MPCAP_DEFAULT_FILE_SIZE;
109 :
110 : /* Round to a multiple of the page size */
111 1 : pm->max_file_size += (u64) clib_mem_get_page_size ();
112 1 : pm->max_file_size &= ~(u64) clib_mem_get_page_size ();
113 :
114 : /* Set file size. */
115 1 : if (lseek (fd, pm->max_file_size - 1, SEEK_SET) == (off_t) - 1)
116 : {
117 0 : close (fd);
118 0 : (void) unlink (pm->file_name);
119 0 : return clib_error_return_unix (0, "file size seek");
120 : }
121 :
122 1 : if (write (fd, &zero, 1) != 1)
123 : {
124 0 : close (fd);
125 0 : (void) unlink (pm->file_name);
126 0 : return clib_error_return_unix (0, "file size write");
127 : }
128 :
129 1 : pm->file_baseva = mmap (0, pm->max_file_size,
130 : PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
131 1 : if (pm->file_baseva == (u8 *) MAP_FAILED)
132 : {
133 0 : clib_error_t *error = clib_error_return_unix (0, "mmap");
134 0 : close (fd);
135 0 : (void) unlink (pm->file_name);
136 0 : return error;
137 : }
138 1 : (void) close (fd);
139 :
140 1 : pm->flags |= MPCAP_FLAG_INIT_DONE | MPCAP_FLAG_WRITE_ENABLE;
141 1 : pm->n_packets_captured = 0;
142 1 : pm->n_mpcap_data_written = 0;
143 :
144 : /* Initialize file header */
145 1 : fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva;
146 1 : pm->current_va = pm->file_baseva + sizeof (*fh);
147 :
148 1 : fh->magic = 0xa1b2c3d4;
149 1 : fh->major_version = 2;
150 1 : fh->minor_version = 4;
151 1 : fh->time_zone = 0;
152 1 : fh->max_packet_size_in_bytes = 1 << 16;
153 1 : fh->packet_type = pm->packet_type;
154 1 : 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 *
164 0 : mpcap_map (mpcap_main_t * pm)
165 : {
166 0 : clib_error_t *error = 0;
167 0 : int fd = -1;
168 : mpcap_file_header_t *fh;
169 : mpcap_packet_header_t *ph;
170 : struct stat statb;
171 0 : u64 packets_read = 0;
172 0 : u32 min_packet_bytes = ~0;
173 0 : u32 max_packet_bytes = 0;
174 :
175 0 : fd = open (pm->file_name, O_RDONLY);
176 0 : if (fd < 0)
177 : {
178 0 : error = clib_error_return_unix (0, "open `%s'", pm->file_name);
179 0 : goto done;
180 : }
181 :
182 0 : if (fstat (fd, &statb) < 0)
183 : {
184 0 : error = clib_error_return_unix (0, "stat `%s'", pm->file_name);
185 0 : goto done;
186 : }
187 :
188 0 : if ((statb.st_mode & S_IFREG) == 0)
189 : {
190 0 : error = clib_error_return (0, "'%s' is not a regular file",
191 : pm->file_name);
192 0 : goto done;
193 : }
194 :
195 0 : if (statb.st_size < sizeof (*fh) + sizeof (*ph))
196 : {
197 0 : error = clib_error_return_unix (0, "`%s' is too short", pm->file_name);
198 0 : goto done;
199 : }
200 :
201 0 : pm->max_file_size = statb.st_size;
202 0 : pm->file_baseva = mmap (0, statb.st_size, PROT_READ, MAP_SHARED, fd, 0);
203 0 : if (pm->file_baseva == (u8 *) MAP_FAILED)
204 : {
205 0 : error = clib_error_return_unix (0, "mmap");
206 0 : goto done;
207 : }
208 :
209 0 : pm->flags |= MPCAP_FLAG_INIT_DONE;
210 0 : fh = pm->file_header = (mpcap_file_header_t *) pm->file_baseva;
211 0 : ph = (mpcap_packet_header_t *) (fh + 1);
212 :
213 0 : if (fh->magic != 0xa1b2c3d4)
214 : {
215 0 : error = clib_error_return (0, "bad magic `%s'", pm->file_name);
216 0 : pm->flags &= ~(MPCAP_FLAG_INIT_DONE);
217 0 : (void) munmap (pm->file_baseva, pm->max_file_size);
218 0 : goto done;
219 : }
220 :
221 : /* for the client's convenience, count packets; compute min/max sizes */
222 0 : while (ph < (mpcap_packet_header_t *) pm->file_baseva + pm->max_file_size)
223 : {
224 0 : if (ph->n_packet_bytes_stored_in_file == 0)
225 0 : break;
226 :
227 0 : packets_read++;
228 0 : min_packet_bytes =
229 0 : ph->n_packet_bytes_stored_in_file <
230 : min_packet_bytes ? ph->n_packet_bytes_stored_in_file :
231 : min_packet_bytes;
232 0 : max_packet_bytes =
233 0 : ph->n_packet_bytes_stored_in_file >
234 : max_packet_bytes ? ph->n_packet_bytes_stored_in_file :
235 : max_packet_bytes;
236 :
237 0 : ph = (mpcap_packet_header_t *)
238 0 : (((u8 *) (ph)) + sizeof (*ph) + ph->n_packet_bytes_stored_in_file);
239 : }
240 0 : pm->packets_read = packets_read;
241 0 : pm->min_packet_bytes = min_packet_bytes;
242 0 : pm->max_packet_bytes = max_packet_bytes;
243 :
244 0 : done:
245 0 : if (fd >= 0)
246 0 : close (fd);
247 0 : 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 : */
|