Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2019 Microsoft Corporation
3 : : */
4 : :
5 : : #include <errno.h>
6 : : #include <stdbool.h>
7 : : #include <stdio.h>
8 : : #include <stdlib.h>
9 : : #include <string.h>
10 : : #include <time.h>
11 : : #include <unistd.h>
12 : :
13 : : #ifndef RTE_EXEC_ENV_WINDOWS
14 : : #include <net/if.h>
15 : : #include <sys/uio.h>
16 : : #endif
17 : :
18 : : #include <bus_driver.h>
19 : : #include <eal_export.h>
20 : : #include <rte_common.h>
21 : : #include <rte_cycles.h>
22 : : #include <dev_driver.h>
23 : : #include <rte_errno.h>
24 : : #include <rte_ethdev.h>
25 : : #include <rte_ether.h>
26 : : #include <rte_mbuf.h>
27 : : #include <rte_os_shim.h>
28 : : #include <rte_pcapng.h>
29 : : #include <rte_time.h>
30 : :
31 : : #include "pcapng_proto.h"
32 : :
33 : : /* conversion from DPDK speed to PCAPNG */
34 : : #define PCAPNG_MBPS_SPEED 1000000ull
35 : :
36 : : /* upper bound for strings in pcapng option data */
37 : : #define PCAPNG_STR_MAX UINT16_MAX
38 : :
39 : : /* Format of the capture file handle */
40 : : struct rte_pcapng {
41 : : int outfd; /* output file */
42 : : unsigned int ports; /* number of interfaces added */
43 : : uint64_t offset_ns; /* ns since 1/1/1970 when initialized */
44 : : uint64_t tsc_base; /* TSC when started */
45 : :
46 : : /* DPDK port id to interface index in file */
47 : : uint32_t port_index[RTE_MAX_ETHPORTS];
48 : : };
49 : :
50 : : #ifdef RTE_EXEC_ENV_WINDOWS
51 : : /*
52 : : * Windows does not have writev() call.
53 : : * Emulate this by copying to a new buffer.
54 : : * The copy is necessary since pcapng needs to be thread-safe
55 : : * and do atomic write operations.
56 : : */
57 : :
58 : : #define IOV_MAX 128
59 : : struct iovec {
60 : : void *iov_base;
61 : : size_t iov_len;
62 : : };
63 : :
64 : : static ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
65 : : {
66 : : size_t bytes = 0;
67 : : uint8_t *ptr;
68 : : void *tmp_buf;
69 : : ssize_t ret;
70 : : int i;
71 : :
72 : : for (i = 0; i < iovcnt; i++)
73 : : bytes += iov[i].iov_len;
74 : :
75 : : if (unlikely(bytes == 0))
76 : : return 0;
77 : :
78 : : tmp_buf = malloc(bytes);
79 : : if (unlikely(tmp_buf == NULL)) {
80 : : errno = ENOMEM;
81 : : return -1;
82 : : }
83 : :
84 : : ptr = tmp_buf;
85 : : for (i = 0; i < iovcnt; i++) {
86 : : rte_memcpy(ptr, iov[i].iov_base, iov[i].iov_len);
87 : : ptr += iov[i].iov_len;
88 : : }
89 : :
90 : : ret = write(fd, tmp_buf, bytes);
91 : : free(tmp_buf);
92 : : return ret;
93 : : }
94 : :
95 : : #define IF_NAMESIZE 16
96 : : /* compatibility wrapper because name is optional */
97 : : #define if_indextoname(ifindex, ifname) NULL
98 : : #endif
99 : :
100 : : /*
101 : : * Convert a count of cycles to nanoseconds.
102 : : *
103 : : * Compute the whole seconds first, so that the remainder is always
104 : : * less than the frequency and scaling it by NS_PER_S cannot wrap.
105 : : */
106 : : static uint64_t
107 : : pcapng_cycles_to_ns(uint64_t delta)
108 : : {
109 : 2648 : const uint64_t hz = rte_get_tsc_hz();
110 : 2648 : uint64_t secs = delta / hz;
111 : 2648 : uint64_t rem = delta % hz;
112 : :
113 : 2648 : return secs * NS_PER_S + (rem * NS_PER_S) / hz;
114 : : }
115 : :
116 : : /* Convert from TSC (CPU cycles) to nanoseconds */
117 : : static uint64_t
118 : 2648 : pcapng_timestamp(const rte_pcapng_t *self, uint64_t cycles)
119 : : {
120 : : /*
121 : : * A packet may be copied before the file was opened, so the TSC
122 : : * can be behind the reference point. Handle both directions on
123 : : * an unsigned magnitude.
124 : : */
125 [ + + ]: 2648 : if (unlikely(cycles < self->tsc_base))
126 : 8 : return self->offset_ns -
127 : 8 : pcapng_cycles_to_ns(self->tsc_base - cycles);
128 : :
129 : 2640 : return self->offset_ns +
130 : 2640 : pcapng_cycles_to_ns(cycles - self->tsc_base);
131 : : }
132 : :
133 : : /* length of option including padding */
134 : : static uint16_t pcapng_optlen(uint16_t len)
135 : : {
136 : 114 : return RTE_ALIGN(sizeof(struct pcapng_option) + len,
137 : : sizeof(uint32_t));
138 : : }
139 : :
140 : : /* build TLV option and return location of next */
141 : : static struct pcapng_option *
142 : : pcapng_add_option(struct pcapng_option *popt, uint16_t code,
143 : : const void *data, uint16_t len)
144 : : {
145 : 2735 : popt->code = code;
146 : 2735 : popt->length = len;
147 : 56 : if (len > 0)
148 [ - + ]: 2717 : memcpy(popt->data, data, len);
149 : :
150 : 64 : return (struct pcapng_option *)((uint8_t *)popt + pcapng_optlen(len));
151 : : }
152 : :
153 : : /*
154 : : * Write required initial section header describing the capture
155 : : */
156 : : static int
157 : 7 : pcapng_section_block(rte_pcapng_t *self,
158 : : const char *os, const char *hw,
159 : : const char *app, const char *comment)
160 : : {
161 : : struct pcapng_section_header *hdr;
162 : : struct pcapng_option *opt;
163 : : uint32_t *buf;
164 : : uint32_t len;
165 : : ssize_t ret;
166 : :
167 : : len = sizeof(*hdr);
168 [ - + ]: 7 : if (hw)
169 : 0 : len += pcapng_optlen(strlen(hw));
170 [ - + ]: 7 : if (os)
171 : 0 : len += pcapng_optlen(strlen(os));
172 [ + - ]: 7 : if (app)
173 : 7 : len += pcapng_optlen(strlen(app));
174 [ - + ]: 7 : if (comment)
175 : 0 : len += pcapng_optlen(strlen(comment));
176 : :
177 : : /* reserve space for OPT_END */
178 : : len += pcapng_optlen(0);
179 : 7 : len += sizeof(uint32_t);
180 : :
181 : 7 : buf = malloc(len);
182 [ + - ]: 7 : if (buf == NULL)
183 : : return -ENOMEM;
184 : :
185 : : hdr = (struct pcapng_section_header *)buf;
186 : 7 : *hdr = (struct pcapng_section_header) {
187 : : .block_type = PCAPNG_SECTION_BLOCK,
188 : : .block_length = len,
189 : : .byte_order_magic = PCAPNG_BYTE_ORDER_MAGIC,
190 : : .major_version = PCAPNG_MAJOR_VERS,
191 : : .minor_version = PCAPNG_MINOR_VERS,
192 : : .section_length = UINT64_MAX,
193 : : };
194 : :
195 : : /* After the section header insert variable length options. */
196 : 7 : opt = (struct pcapng_option *)(hdr + 1);
197 [ - + ]: 7 : if (comment)
198 : 0 : opt = pcapng_add_option(opt, PCAPNG_OPT_COMMENT,
199 [ # # ]: 0 : comment, strlen(comment));
200 [ - + ]: 7 : if (hw)
201 : 0 : opt = pcapng_add_option(opt, PCAPNG_SHB_HARDWARE,
202 [ # # ]: 0 : hw, strlen(hw));
203 [ - + ]: 7 : if (os)
204 : 0 : opt = pcapng_add_option(opt, PCAPNG_SHB_OS,
205 [ # # ]: 0 : os, strlen(os));
206 [ + - ]: 7 : if (app)
207 : 7 : opt = pcapng_add_option(opt, PCAPNG_SHB_USERAPPL,
208 [ + - ]: 7 : app, strlen(app));
209 : :
210 : : /* The standard requires last option to be OPT_END */
211 : : opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);
212 : :
213 : : /* clone block_length after option */
214 : 7 : memcpy(opt, &hdr->block_length, sizeof(uint32_t));
215 : :
216 : 7 : ret = write(self->outfd, buf, len);
217 : 7 : free(buf);
218 [ - + ]: 7 : return ret < 0 ? -errno : 0;
219 : : }
220 : :
221 : : /* Write an interface block for a DPDK port */
222 : : RTE_EXPORT_SYMBOL(rte_pcapng_add_interface)
223 : : int
224 : 9 : rte_pcapng_add_interface(rte_pcapng_t *self, uint16_t port, uint16_t link_type,
225 : : const char *ifname, const char *ifdescr,
226 : : const char *filter)
227 : : {
228 : : struct pcapng_interface_block *hdr;
229 : : struct rte_eth_dev_info dev_info;
230 : : struct rte_ether_addr *ea, macaddr;
231 : : const struct rte_device *dev;
232 : : struct rte_eth_link link;
233 : : struct pcapng_option *opt;
234 : 9 : const uint8_t tsresol = 9; /* nanosecond resolution */
235 : : uint32_t len;
236 : : uint32_t *buf;
237 : : char ifname_buf[IF_NAMESIZE];
238 : : char ifhw[256];
239 : 9 : uint64_t speed = 0;
240 : : int ret;
241 : :
242 : 9 : ret = rte_eth_dev_info_get(port, &dev_info);
243 [ + - ]: 9 : if (ret < 0)
244 : : return -1; /* should be ret */
245 : :
246 : : /* make something like an interface name */
247 [ + + ]: 9 : if (ifname == NULL) {
248 : : /* Use kernel name if available */
249 : 8 : ifname = if_indextoname(dev_info.if_index, ifname_buf);
250 [ + - ]: 8 : if (ifname == NULL) {
251 : : snprintf(ifname_buf, IF_NAMESIZE, "dpdk:%u", port);
252 : : ifname = ifname_buf;
253 : : }
254 [ + - ]: 1 : } else if (strlen(ifname) > PCAPNG_STR_MAX) {
255 : : return -1; /* ENAMETOOLONG */
256 : : }
257 : :
258 [ + + + - : 9 : if ((ifdescr && strlen(ifdescr) > PCAPNG_STR_MAX) ||
+ + ]
259 [ + - ]: 1 : (filter && strlen(filter) > PCAPNG_STR_MAX))
260 : : return -1; /* EINVAL */
261 : :
262 : : /* make a useful device hardware string */
263 : 9 : dev = dev_info.device;
264 [ + - ]: 9 : if (dev)
265 : : snprintf(ifhw, sizeof(ifhw),
266 : 9 : "%s-%s", dev->bus->name, dev->name);
267 : :
268 : : /* DPDK reports in units of Mbps */
269 [ + - ]: 9 : if (rte_eth_link_get(port, &link) == 0 &&
270 [ - + ]: 9 : link.link_status == RTE_ETH_LINK_UP)
271 : 0 : speed = link.link_speed * PCAPNG_MBPS_SPEED;
272 : :
273 [ + - ]: 9 : if (rte_eth_macaddr_get(port, &macaddr) < 0)
274 : : ea = NULL;
275 : : else
276 : : ea = &macaddr;
277 : :
278 : : /* Compute length of interface block options */
279 : : len = sizeof(*hdr);
280 : :
281 : : len += pcapng_optlen(sizeof(tsresol)); /* timestamp */
282 : 9 : len += pcapng_optlen(strlen(ifname)); /* ifname */
283 : :
284 [ + + ]: 9 : if (ifdescr)
285 : 1 : len += pcapng_optlen(strlen(ifdescr));
286 [ + - ]: 9 : if (ea)
287 : 9 : len += pcapng_optlen(RTE_ETHER_ADDR_LEN); /* macaddr */
288 [ - + ]: 9 : if (speed != 0)
289 : 0 : len += pcapng_optlen(sizeof(uint64_t));
290 [ + + ]: 9 : if (filter)
291 : 1 : len += pcapng_optlen(strlen(filter) + 1);
292 [ + - ]: 9 : if (dev)
293 : 9 : len += pcapng_optlen(strlen(ifhw));
294 : :
295 : : len += pcapng_optlen(0);
296 : 9 : len += sizeof(uint32_t);
297 : :
298 : 9 : buf = malloc(len);
299 [ + - ]: 9 : if (buf == NULL)
300 : : return -1; /* ENOMEM */
301 : :
302 : : hdr = (struct pcapng_interface_block *)buf;
303 [ + - ]: 9 : *hdr = (struct pcapng_interface_block) {
304 : : .block_type = PCAPNG_INTERFACE_BLOCK,
305 : : .link_type = link_type,
306 : : .block_length = len,
307 : : };
308 : :
309 : : opt = (struct pcapng_option *)(hdr + 1);
310 : : opt = pcapng_add_option(opt, PCAPNG_IFB_TSRESOL,
311 : : &tsresol, sizeof(tsresol));
312 : 9 : opt = pcapng_add_option(opt, PCAPNG_IFB_NAME,
313 [ + - ]: 9 : ifname, strlen(ifname));
314 [ + + ]: 9 : if (ifdescr)
315 : 1 : opt = pcapng_add_option(opt, PCAPNG_IFB_DESCRIPTION,
316 [ + - ]: 1 : ifdescr, strlen(ifdescr));
317 [ + - ]: 9 : if (ea)
318 : : opt = pcapng_add_option(opt, PCAPNG_IFB_MACADDR,
319 : : ea, RTE_ETHER_ADDR_LEN);
320 [ - + ]: 9 : if (speed != 0)
321 : : opt = pcapng_add_option(opt, PCAPNG_IFB_SPEED,
322 : : &speed, sizeof(uint64_t));
323 [ + - ]: 9 : if (dev)
324 : 9 : opt = pcapng_add_option(opt, PCAPNG_IFB_HARDWARE,
325 [ + - ]: 9 : ifhw, strlen(ifhw));
326 [ + + ]: 9 : if (filter) {
327 : 1 : const size_t filter_len = strlen(filter) + 1;
328 : :
329 : 1 : opt->code = PCAPNG_IFB_FILTER;
330 : 1 : opt->length = filter_len;
331 : : /* Encoding is that the first octet indicates string vs BPF */
332 : 1 : opt->data[0] = 0;
333 : 1 : memcpy(opt->data + 1, filter, strlen(filter));
334 : :
335 : 1 : opt = (struct pcapng_option *)((uint8_t *)opt + pcapng_optlen(filter_len));
336 : : }
337 : :
338 : : opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);
339 : :
340 : : /* clone block_length after options */
341 : 9 : memcpy(opt, &hdr->block_length, sizeof(uint32_t));
342 : :
343 : 9 : ret = write(self->outfd, buf, len);
344 : 9 : free(buf);
345 : :
346 : : /* remember the file index only after successful write */
347 [ + - ]: 9 : if (ret > 0)
348 : 9 : self->port_index[port] = self->ports++;
349 : :
350 : : return ret;
351 : : }
352 : :
353 : : /*
354 : : * Write an Interface statistics block at the end of capture.
355 : : */
356 : : RTE_EXPORT_SYMBOL(rte_pcapng_write_stats)
357 : : ssize_t
358 : 2 : rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,
359 : : uint64_t ifrecv, uint64_t ifdrop,
360 : : const char *comment)
361 : : {
362 : : struct pcapng_statistics *hdr;
363 : : struct pcapng_option *opt;
364 : 2 : uint64_t start_time = self->offset_ns;
365 : : uint64_t sample_time;
366 : : uint32_t optlen, len;
367 : : uint32_t *buf;
368 : : ssize_t ret;
369 : :
370 [ - + ]: 2 : RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
371 : :
372 [ + + + - ]: 2 : if (comment && strlen(comment) > PCAPNG_STR_MAX)
373 : : return -EINVAL;
374 : :
375 : : optlen = 0;
376 : :
377 [ + - ]: 2 : if (ifrecv != UINT64_MAX)
378 : : optlen += pcapng_optlen(sizeof(ifrecv));
379 [ + - ]: 2 : if (ifdrop != UINT64_MAX)
380 : 2 : optlen += pcapng_optlen(sizeof(ifdrop));
381 : :
382 [ + - ]: 2 : if (start_time != 0)
383 : 2 : optlen += pcapng_optlen(sizeof(start_time));
384 : :
385 [ + + ]: 2 : if (comment)
386 : 1 : optlen += pcapng_optlen(strlen(comment));
387 [ + - ]: 2 : if (optlen != 0)
388 : 2 : optlen += pcapng_optlen(0);
389 : :
390 : 2 : len = sizeof(*hdr) + optlen + sizeof(uint32_t);
391 : 2 : buf = malloc(len);
392 [ + - ]: 2 : if (buf == NULL)
393 : : return -ENOMEM;
394 : :
395 : : hdr = (struct pcapng_statistics *)buf;
396 : 2 : opt = (struct pcapng_option *)(hdr + 1);
397 : :
398 [ + + ]: 2 : if (comment)
399 : 1 : opt = pcapng_add_option(opt, PCAPNG_OPT_COMMENT,
400 [ + - ]: 1 : comment, strnlen(comment, PCAPNG_STR_MAX));
401 [ + - ]: 2 : if (start_time != 0)
402 : : opt = pcapng_add_option(opt, PCAPNG_ISB_STARTTIME,
403 : : &start_time, sizeof(start_time));
404 [ + - ]: 2 : if (ifrecv != UINT64_MAX)
405 : : opt = pcapng_add_option(opt, PCAPNG_ISB_IFRECV,
406 : : &ifrecv, sizeof(ifrecv));
407 [ + - ]: 2 : if (ifdrop != UINT64_MAX)
408 : : opt = pcapng_add_option(opt, PCAPNG_ISB_IFDROP,
409 : : &ifdrop, sizeof(ifdrop));
410 [ + - ]: 2 : if (optlen != 0)
411 : : opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);
412 : :
413 : 2 : hdr->block_type = PCAPNG_INTERFACE_STATS_BLOCK;
414 : 2 : hdr->block_length = len;
415 : 2 : hdr->interface_id = self->port_index[port_id];
416 : :
417 : 2 : sample_time = pcapng_timestamp(self, rte_get_tsc_cycles());
418 : 2 : hdr->timestamp_hi = sample_time >> 32;
419 : 2 : hdr->timestamp_lo = (uint32_t)sample_time;
420 : :
421 : : /* clone block_length after option */
422 : : memcpy(opt, &len, sizeof(uint32_t));
423 : :
424 : 2 : ret = write(self->outfd, buf, len);
425 : 2 : free(buf);
426 : 2 : return ret;
427 : : }
428 : :
429 : : RTE_EXPORT_SYMBOL(rte_pcapng_mbuf_size)
430 : : uint32_t
431 : 1 : rte_pcapng_mbuf_size(uint32_t length)
432 : : {
433 : : /* The VLAN and EPB header must fit in the mbuf headroom. */
434 : : RTE_ASSERT(sizeof(struct pcapng_enhance_packet_block) +
435 : : sizeof(struct rte_vlan_hdr) <= RTE_PKTMBUF_HEADROOM);
436 : :
437 : : /* The flags and queue information are added at the end. */
438 : : return sizeof(struct rte_mbuf)
439 : 1 : + RTE_ALIGN(length, sizeof(uint32_t))
440 : : + pcapng_optlen(sizeof(uint32_t)) /* flag option */
441 : : + pcapng_optlen(sizeof(uint32_t)) /* queue option */
442 : 1 : + sizeof(uint32_t); /* length */
443 : : }
444 : :
445 : : /* More generalized version rte_vlan_insert() */
446 : : static int
447 : 0 : pcapng_vlan_insert(struct rte_mbuf *m, uint16_t ether_type, uint16_t tci)
448 : : {
449 : : struct rte_ether_hdr *nh, *oh;
450 : : struct rte_vlan_hdr *vh;
451 : :
452 [ # # # # ]: 0 : if (!RTE_MBUF_DIRECT(m) || rte_mbuf_refcnt_read(m) > 1)
453 : 0 : return -EINVAL;
454 : :
455 [ # # ]: 0 : if (rte_pktmbuf_data_len(m) < sizeof(*oh))
456 : : return -EINVAL;
457 : :
458 [ # # ]: 0 : oh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
459 : : nh = (struct rte_ether_hdr *)
460 : : rte_pktmbuf_prepend(m, sizeof(struct rte_vlan_hdr));
461 [ # # ]: 0 : if (nh == NULL)
462 : 0 : return -ENOSPC;
463 : :
464 : : memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
465 [ # # ]: 0 : nh->ether_type = rte_cpu_to_be_16(ether_type);
466 : :
467 : : vh = (struct rte_vlan_hdr *) (nh + 1);
468 [ # # ]: 0 : vh->vlan_tci = rte_cpu_to_be_16(tci);
469 : :
470 : 0 : return 0;
471 : : }
472 : :
473 : : /*
474 : : * The mbufs created use the Pcapng standard enhanced packet block.
475 : : *
476 : : * 1 2 3
477 : : * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
478 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
479 : : * 0 | Block Type = 0x00000006 |
480 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
481 : : * 4 | Block Total Length |
482 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
483 : : * 8 | Interface ID |
484 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
485 : : * 12 | Timestamp (High) |
486 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
487 : : * 16 | Timestamp (Low) |
488 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489 : : * 20 | Captured Packet Length |
490 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491 : : * 24 | Original Packet Length |
492 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493 : : * 28 / /
494 : : * / Packet Data /
495 : : * / variable length, padded to 32 bits /
496 : : * / /
497 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
498 : : * | Option Code = 0x0002 | Option Length = 0x004 |
499 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
500 : : * | Flags (direction) |
501 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
502 : : * | Option Code = 0x0006 | Option Length = 0x002 |
503 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
504 : : * | Queue id |
505 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
506 : : * | Block Total Length |
507 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
508 : : */
509 : :
510 : : /* Make a copy of original mbuf with pcapng header and options */
511 : : RTE_EXPORT_SYMBOL(rte_pcapng_copy)
512 : : struct rte_mbuf *
513 : 2646 : rte_pcapng_copy(uint16_t port_id, uint32_t queue,
514 : : const struct rte_mbuf *md,
515 : : struct rte_mempool *mp,
516 : : uint32_t length,
517 : : enum rte_pcapng_direction direction,
518 : : const char *comment)
519 : : {
520 : : struct pcapng_enhance_packet_block *epb;
521 : : uint32_t orig_len, pkt_len, padding, flags;
522 : : struct pcapng_option *opt;
523 : : uint64_t timestamp;
524 : : uint16_t optlen;
525 : : struct rte_mbuf *mc;
526 : : bool rss_hash;
527 : :
528 : : #ifdef RTE_LIBRTE_ETHDEV_DEBUG
529 : : /*
530 : : * Since this function is used in the fast path for packet capture
531 : : * skip argument validation checks unless debug is enabled.
532 : : */
533 : : RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, NULL);
534 : : #endif
535 : 2646 : orig_len = rte_pktmbuf_pkt_len(md);
536 : :
537 : : /* Take snapshot of the data */
538 : 2646 : mc = rte_pktmbuf_copy(md, mp, 0, length);
539 [ + - ]: 2646 : if (unlikely(mc == NULL))
540 : : return NULL;
541 : :
542 : : /* Expand any offloaded VLAN information */
543 [ + - ]: 2646 : if ((direction == RTE_PCAPNG_DIRECTION_IN &&
544 [ + - - + ]: 2646 : (md->ol_flags & RTE_MBUF_F_RX_VLAN_STRIPPED)) ||
545 : 0 : (direction == RTE_PCAPNG_DIRECTION_OUT &&
546 [ # # ]: 0 : (md->ol_flags & RTE_MBUF_F_TX_VLAN))) {
547 [ # # ]: 0 : if (pcapng_vlan_insert(mc, RTE_ETHER_TYPE_VLAN,
548 : 0 : md->vlan_tci) != 0)
549 : 0 : goto fail;
550 : : }
551 : :
552 [ + - ]: 2646 : if ((direction == RTE_PCAPNG_DIRECTION_IN &&
553 [ + - - + ]: 2646 : (md->ol_flags & RTE_MBUF_F_RX_QINQ_STRIPPED)) ||
554 : 0 : (direction == RTE_PCAPNG_DIRECTION_OUT &&
555 [ # # ]: 0 : (md->ol_flags & RTE_MBUF_F_TX_QINQ))) {
556 [ # # ]: 0 : if (pcapng_vlan_insert(mc, RTE_ETHER_TYPE_QINQ,
557 : 0 : md->vlan_tci_outer) != 0)
558 : 0 : goto fail;
559 : : }
560 : :
561 : : /* record HASH on incoming packets */
562 [ + - ]: 2646 : rss_hash = (direction == RTE_PCAPNG_DIRECTION_IN &&
563 [ + - ]: 2646 : (md->ol_flags & RTE_MBUF_F_RX_RSS_HASH));
564 : :
565 : : /* pad the packet to 32 bit boundary */
566 : 2646 : pkt_len = rte_pktmbuf_pkt_len(mc);
567 : 2646 : padding = RTE_ALIGN(pkt_len, sizeof(uint32_t)) - pkt_len;
568 [ + + ]: 2646 : if (padding > 0) {
569 : 1974 : void *tail = rte_pktmbuf_append(mc, padding);
570 : :
571 [ - + ]: 1974 : if (tail == NULL)
572 : 0 : goto fail;
573 : 1974 : memset(tail, 0, padding);
574 : : }
575 : :
576 : : optlen = pcapng_optlen(sizeof(flags));
577 : : optlen += pcapng_optlen(sizeof(queue));
578 [ - + ]: 2646 : if (rss_hash)
579 : : optlen += pcapng_optlen(sizeof(uint8_t) + sizeof(uint32_t));
580 : :
581 : : /* Comment is silently truncated if necessary */
582 [ + + ]: 2646 : if (comment)
583 : 29 : optlen += pcapng_optlen(strnlen(comment, PCAPNG_STR_MAX));
584 : :
585 : : /*
586 : : * Try to put options at the end of this mbuf.
587 : : * If not extend the mbuf by adding another segment.
588 : : */
589 : 2646 : opt = (struct pcapng_option *)rte_pktmbuf_append(mc, optlen + sizeof(uint32_t));
590 [ + + ]: 2646 : if (unlikely(opt == NULL)) {
591 : 24 : struct rte_mbuf *ml = rte_pktmbuf_alloc(mp);
592 : :
593 [ - + ]: 24 : if (unlikely(ml == NULL))
594 : 0 : goto fail; /* mbuf pool is empty */
595 : :
596 [ - + ]: 24 : if (unlikely(rte_pktmbuf_chain(mc, ml) != 0)) {
597 : 0 : rte_pktmbuf_free(ml);
598 : 0 : goto fail; /* too many segments in the mbuf */
599 : : }
600 : :
601 : : opt = (struct pcapng_option *)rte_pktmbuf_append(mc, optlen + sizeof(uint32_t));
602 [ - + ]: 24 : if (unlikely(opt == NULL))
603 : 0 : goto fail; /* additional segment and still no space */
604 : : }
605 : :
606 [ + - - ]: 2646 : switch (direction) {
607 : 2646 : case RTE_PCAPNG_DIRECTION_IN:
608 : 2646 : flags = PCAPNG_IFB_INBOUND;
609 : 2646 : break;
610 : 0 : case RTE_PCAPNG_DIRECTION_OUT:
611 : 0 : flags = PCAPNG_IFB_OUTBOUND;
612 : 0 : break;
613 : 0 : default:
614 : 0 : flags = 0;
615 : : }
616 : :
617 : : opt = pcapng_add_option(opt, PCAPNG_EPB_FLAGS,
618 : : &flags, sizeof(flags));
619 : :
620 : : opt = pcapng_add_option(opt, PCAPNG_EPB_QUEUE,
621 : : &queue, sizeof(queue));
622 : :
623 [ - + ]: 2646 : if (rss_hash) {
624 : : uint8_t hash_opt[5];
625 : :
626 : : /* The algorithm could be something else if
627 : : * using rte_flow_action_rss; but the current API does not
628 : : * have a way for ethdev to report this on a per-packet basis.
629 : : */
630 : 0 : hash_opt[0] = PCAPNG_HASH_TOEPLITZ;
631 : :
632 : : memcpy(&hash_opt[1], &md->hash.rss, sizeof(uint32_t));
633 : : opt = pcapng_add_option(opt, PCAPNG_EPB_HASH,
634 : : &hash_opt, sizeof(hash_opt));
635 : : }
636 : :
637 [ + + ]: 2646 : if (comment)
638 : 29 : opt = pcapng_add_option(opt, PCAPNG_OPT_COMMENT, comment,
639 [ + - ]: 29 : strnlen(comment, PCAPNG_STR_MAX));
640 : :
641 : : /* Note: END_OPT necessary here. Wireshark doesn't do it. */
642 : :
643 : : /* Add PCAPNG packet header */
644 : : epb = (struct pcapng_enhance_packet_block *)
645 : : rte_pktmbuf_prepend(mc, sizeof(*epb));
646 [ - + ]: 2646 : if (unlikely(epb == NULL))
647 : 0 : goto fail;
648 : :
649 : 2646 : epb->block_type = PCAPNG_ENHANCED_PACKET_BLOCK;
650 : 2646 : epb->block_length = rte_pktmbuf_pkt_len(mc);
651 : :
652 : : /* Interface index is filled in later during write */
653 : 2646 : mc->port = port_id;
654 : :
655 : : /* Put timestamp in cycles here - adjust in packet write */
656 : : timestamp = rte_get_tsc_cycles();
657 : 2646 : epb->timestamp_hi = timestamp >> 32;
658 : 2646 : epb->timestamp_lo = (uint32_t)timestamp;
659 : 2646 : epb->capture_length = pkt_len;
660 : 2646 : epb->original_length = orig_len;
661 : :
662 : : /* set trailer of block length */
663 : 2646 : *(uint32_t *)opt = epb->block_length;
664 : :
665 : 2646 : return mc;
666 : :
667 : 0 : fail:
668 : 0 : rte_pktmbuf_free(mc);
669 : 0 : return NULL;
670 : : }
671 : :
672 : : /* Write pre-formatted packets to file. */
673 : : RTE_EXPORT_SYMBOL(rte_pcapng_write_packets)
674 : : ssize_t
675 : 155 : rte_pcapng_write_packets(rte_pcapng_t *self,
676 : : struct rte_mbuf *pkts[], uint16_t nb_pkts)
677 : : {
678 : : struct iovec iov[IOV_MAX];
679 : : unsigned int i, cnt = 0;
680 : : ssize_t ret, total = 0;
681 : :
682 [ + + ]: 2801 : for (i = 0; i < nb_pkts; i++) {
683 : 2646 : struct rte_mbuf *m = pkts[i];
684 : : struct pcapng_enhance_packet_block *epb;
685 : : uint64_t cycles, timestamp;
686 : :
687 : : /* sanity check that is really a pcapng mbuf */
688 : 2646 : epb = rte_pktmbuf_mtod(m, struct pcapng_enhance_packet_block *);
689 [ + - - + ]: 2646 : if (unlikely(epb->block_type != PCAPNG_ENHANCED_PACKET_BLOCK ||
690 : : epb->block_length != rte_pktmbuf_pkt_len(m))) {
691 : 0 : rte_errno = EINVAL;
692 : 0 : return -1;
693 : : }
694 : :
695 : : /* check that this interface was added. */
696 : 2646 : epb->interface_id = self->port_index[m->port];
697 [ - + ]: 2646 : if (unlikely(epb->interface_id > RTE_MAX_ETHPORTS)) {
698 : 0 : rte_errno = EINVAL;
699 : 0 : return -1;
700 : : }
701 : :
702 : : /* adjust timestamp recorded in packet */
703 : 2646 : cycles = (uint64_t)epb->timestamp_hi << 32;
704 : 2646 : cycles += epb->timestamp_lo;
705 : 2646 : timestamp = pcapng_timestamp(self, cycles);
706 : 2646 : epb->timestamp_hi = timestamp >> 32;
707 : 2646 : epb->timestamp_lo = (uint32_t)timestamp;
708 : :
709 : : /*
710 : : * Handle case of highly fragmented and large burst size
711 : : * Note: this assumes that max segments per mbuf < IOV_MAX
712 : : */
713 [ - + ]: 2646 : if (unlikely(cnt + m->nb_segs >= IOV_MAX)) {
714 : 0 : ret = writev(self->outfd, iov, cnt);
715 [ # # ]: 0 : if (unlikely(ret < 0)) {
716 : 0 : rte_errno = errno;
717 : 0 : return -1;
718 : : }
719 : 0 : total += ret;
720 : : cnt = 0;
721 : : }
722 : :
723 : : /*
724 : : * The DPDK port is recorded during pcapng_copy.
725 : : * Map that to PCAPNG interface in file.
726 : : */
727 : : do {
728 : 2707 : iov[cnt].iov_base = rte_pktmbuf_mtod(m, void *);
729 : 2707 : iov[cnt].iov_len = rte_pktmbuf_data_len(m);
730 : 2707 : ++cnt;
731 [ + + ]: 2707 : } while ((m = m->next));
732 : : }
733 : :
734 : 155 : ret = writev(self->outfd, iov, cnt);
735 [ - + ]: 155 : if (unlikely(ret < 0)) {
736 : 0 : rte_errno = errno;
737 : 0 : return -1;
738 : : }
739 : 155 : return total + ret;
740 : : }
741 : :
742 : : /* Create new pcapng writer handle */
743 : : RTE_EXPORT_SYMBOL(rte_pcapng_fdopen)
744 : : rte_pcapng_t *
745 : 7 : rte_pcapng_fdopen(int fd,
746 : : const char *osname, const char *hardware,
747 : : const char *appname, const char *comment)
748 : : {
749 : : unsigned int i;
750 : : rte_pcapng_t *self;
751 : : struct timespec ts;
752 : : uint64_t cycles;
753 : : int ret;
754 : :
755 [ - + - - : 7 : if ((osname && strlen(osname) > PCAPNG_STR_MAX) ||
- + ]
756 [ - - + - ]: 7 : (hardware && strlen(hardware) > PCAPNG_STR_MAX) ||
757 [ + - - + ]: 7 : (appname && strlen(appname) > PCAPNG_STR_MAX) ||
758 [ # # ]: 0 : (comment && strlen(comment) > PCAPNG_STR_MAX)) {
759 : 0 : rte_errno = EINVAL;
760 : 0 : return NULL;
761 : : }
762 : :
763 : 7 : self = malloc(sizeof(*self));
764 [ - + ]: 7 : if (!self) {
765 : 0 : rte_errno = ENOMEM;
766 : 0 : return NULL;
767 : : }
768 : :
769 : 7 : self->outfd = fd;
770 : 7 : self->ports = 0;
771 : :
772 : : /* If Hz is zero, something is seriously broken. */
773 [ - + ]: 7 : if (rte_get_tsc_hz() == 0) {
774 : 0 : rte_errno = ENODEV;
775 : 0 : goto fail;
776 : : }
777 : :
778 : : /* record start time in ns since 1/1/1970 */
779 : : cycles = rte_get_tsc_cycles();
780 : 7 : clock_gettime(CLOCK_REALTIME, &ts);
781 : 7 : self->tsc_base = (cycles + rte_get_tsc_cycles()) / 2;
782 : 7 : self->offset_ns = rte_timespec_to_ns(&ts);
783 : :
784 [ + + ]: 231 : for (i = 0; i < RTE_MAX_ETHPORTS; i++)
785 : 224 : self->port_index[i] = UINT32_MAX;
786 : :
787 : 7 : ret = pcapng_section_block(self, osname, hardware, appname, comment);
788 [ - + ]: 7 : if (ret < 0) {
789 : 0 : rte_errno = -ret;
790 : 0 : goto fail;
791 : : }
792 : :
793 : : return self;
794 : 0 : fail:
795 : 0 : free(self);
796 : 0 : return NULL;
797 : : }
798 : :
799 : : RTE_EXPORT_SYMBOL(rte_pcapng_close)
800 : : void
801 : 7 : rte_pcapng_close(rte_pcapng_t *self)
802 : : {
803 [ + - ]: 7 : if (self) {
804 : 7 : close(self->outfd);
805 : 7 : free(self);
806 : : }
807 : 7 : }
|