1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019 Microsoft Corporation
14 #include <rte_common.h>
15 #include <rte_cycles.h>
17 #include <rte_errno.h>
18 #include <rte_ethdev.h>
19 #include <rte_ether.h>
21 #include <rte_pcapng.h>
24 #include "pcapng_proto.h"
26 /* conversion from DPDK speed to PCAPNG */
27 #define PCAPNG_MBPS_SPEED 1000000ull
29 /* Format of the capture file handle */
31 int outfd; /* output file */
32 /* DPDK port id to interface index in file */
33 uint32_t port_index[RTE_MAX_ETHPORTS];
36 /* For converting TSC cycles to PCAPNG ns format */
46 pcapng_time.cycles = rte_get_tsc_cycles();
47 clock_gettime(CLOCK_REALTIME, &ts);
48 pcapng_time.ns = rte_timespec_to_ns(&ts);
51 /* PCAPNG timestamps are in nanoseconds */
52 static uint64_t pcapng_tsc_to_ns(uint64_t cycles)
56 delta = cycles - pcapng_time.cycles;
57 return pcapng_time.ns + (delta * NSEC_PER_SEC) / rte_get_tsc_hz();
60 /* length of option including padding */
61 static uint16_t pcapng_optlen(uint16_t len)
63 return RTE_ALIGN(sizeof(struct pcapng_option) + len,
67 /* build TLV option and return location of next */
68 static struct pcapng_option *
69 pcapng_add_option(struct pcapng_option *popt, uint16_t code,
70 const void *data, uint16_t len)
74 memcpy(popt->data, data, len);
76 return (struct pcapng_option *)((uint8_t *)popt + pcapng_optlen(len));
80 * Write required initial section header describing the capture
83 pcapng_section_block(rte_pcapng_t *self,
84 const char *os, const char *hw,
85 const char *app, const char *comment)
87 struct pcapng_section_header *hdr;
88 struct pcapng_option *opt;
95 len += pcapng_optlen(strlen(hw));
97 len += pcapng_optlen(strlen(os));
99 len += pcapng_optlen(strlen(app));
101 len += pcapng_optlen(strlen(comment));
103 /* reserve space for OPT_END */
104 len += pcapng_optlen(0);
105 len += sizeof(uint32_t);
107 buf = calloc(1, len);
111 hdr = (struct pcapng_section_header *)buf;
112 *hdr = (struct pcapng_section_header) {
113 .block_type = PCAPNG_SECTION_BLOCK,
115 .byte_order_magic = PCAPNG_BYTE_ORDER_MAGIC,
116 .major_version = PCAPNG_MAJOR_VERS,
117 .minor_version = PCAPNG_MINOR_VERS,
118 .section_length = UINT64_MAX,
121 /* After the section header insert variable length options. */
122 opt = (struct pcapng_option *)(hdr + 1);
124 opt = pcapng_add_option(opt, PCAPNG_OPT_COMMENT,
125 comment, strlen(comment));
127 opt = pcapng_add_option(opt, PCAPNG_SHB_HARDWARE,
130 opt = pcapng_add_option(opt, PCAPNG_SHB_OS,
133 opt = pcapng_add_option(opt, PCAPNG_SHB_USERAPPL,
136 /* The standard requires last option to be OPT_END */
137 opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);
139 /* clone block_length after option */
140 memcpy(opt, &hdr->block_length, sizeof(uint32_t));
142 cc = write(self->outfd, buf, len);
148 /* Write an interface block for a DPDK port */
150 pcapng_add_interface(rte_pcapng_t *self, uint16_t port)
152 struct pcapng_interface_block *hdr;
153 struct rte_eth_dev_info dev_info;
154 struct rte_ether_addr *ea, macaddr;
155 const struct rte_device *dev;
156 struct rte_eth_link link;
157 struct pcapng_option *opt;
158 const uint8_t tsresol = 9; /* nanosecond resolution */
161 char ifname[IF_NAMESIZE];
165 if (rte_eth_dev_info_get(port, &dev_info) < 0)
168 /* make something like an interface name */
169 if (if_indextoname(dev_info.if_index, ifname) == NULL)
170 snprintf(ifname, IF_NAMESIZE, "dpdk:%u", port);
172 /* make a useful device hardware string */
173 dev = dev_info.device;
175 snprintf(ifhw, sizeof(ifhw),
176 "%s-%s", dev->bus->name, dev->name);
178 /* DPDK reports in units of Mbps */
179 if (rte_eth_link_get(port, &link) == 0 &&
180 link.link_status == RTE_ETH_LINK_UP)
181 speed = link.link_speed * PCAPNG_MBPS_SPEED;
183 if (rte_eth_macaddr_get(port, &macaddr) < 0)
188 /* Compute length of interface block options */
191 len += pcapng_optlen(sizeof(tsresol)); /* timestamp */
192 len += pcapng_optlen(strlen(ifname)); /* ifname */
195 len += pcapng_optlen(RTE_ETHER_ADDR_LEN); /* macaddr */
197 len += pcapng_optlen(sizeof(uint64_t));
199 len += pcapng_optlen(strlen(ifhw));
201 len += pcapng_optlen(0);
202 len += sizeof(uint32_t);
208 hdr = (struct pcapng_interface_block *)buf;
209 *hdr = (struct pcapng_interface_block) {
210 .block_type = PCAPNG_INTERFACE_BLOCK,
211 .link_type = 1, /* DLT_EN10MB - Ethernet */
215 opt = (struct pcapng_option *)(hdr + 1);
216 opt = pcapng_add_option(opt, PCAPNG_IFB_TSRESOL,
217 &tsresol, sizeof(tsresol));
218 opt = pcapng_add_option(opt, PCAPNG_IFB_NAME,
219 ifname, strlen(ifname));
221 opt = pcapng_add_option(opt, PCAPNG_IFB_MACADDR,
222 ea, RTE_ETHER_ADDR_LEN);
224 opt = pcapng_add_option(opt, PCAPNG_IFB_SPEED,
225 &speed, sizeof(uint64_t));
227 opt = pcapng_add_option(opt, PCAPNG_IFB_HARDWARE,
229 opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);
231 /* clone block_length after optionsa */
232 memcpy(opt, &hdr->block_length, sizeof(uint32_t));
234 return write(self->outfd, buf, len);
238 * Write the list of possible interfaces at the start
242 pcapng_interfaces(rte_pcapng_t *self)
247 RTE_ETH_FOREACH_DEV(port_id) {
248 /* The list if ports in pcapng needs to be contiguous */
249 self->port_index[port_id] = index++;
250 if (pcapng_add_interface(self, port_id) < 0)
257 * Write an Interface statistics block at the end of capture.
260 rte_pcapng_write_stats(rte_pcapng_t *self, uint16_t port_id,
262 uint64_t start_time, uint64_t end_time,
263 uint64_t ifrecv, uint64_t ifdrop)
265 struct pcapng_statistics *hdr;
266 struct pcapng_option *opt;
267 uint32_t optlen, len;
271 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL);
275 if (ifrecv != UINT64_MAX)
276 optlen += pcapng_optlen(sizeof(ifrecv));
277 if (ifdrop != UINT64_MAX)
278 optlen += pcapng_optlen(sizeof(ifdrop));
280 optlen += pcapng_optlen(sizeof(start_time));
282 optlen += pcapng_optlen(sizeof(end_time));
284 optlen += pcapng_optlen(strlen(comment));
286 optlen += pcapng_optlen(0);
288 len = sizeof(*hdr) + optlen + sizeof(uint32_t);
293 hdr = (struct pcapng_statistics *)buf;
294 opt = (struct pcapng_option *)(hdr + 1);
297 opt = pcapng_add_option(opt, PCAPNG_OPT_COMMENT,
298 comment, strlen(comment));
300 opt = pcapng_add_option(opt, PCAPNG_ISB_STARTTIME,
301 &start_time, sizeof(start_time));
303 opt = pcapng_add_option(opt, PCAPNG_ISB_ENDTIME,
304 &end_time, sizeof(end_time));
305 if (ifrecv != UINT64_MAX)
306 opt = pcapng_add_option(opt, PCAPNG_ISB_IFRECV,
307 &ifrecv, sizeof(ifrecv));
308 if (ifdrop != UINT64_MAX)
309 opt = pcapng_add_option(opt, PCAPNG_ISB_IFDROP,
310 &ifdrop, sizeof(ifdrop));
312 opt = pcapng_add_option(opt, PCAPNG_OPT_END, NULL, 0);
314 hdr->block_type = PCAPNG_INTERFACE_STATS_BLOCK;
315 hdr->block_length = len;
316 hdr->interface_id = self->port_index[port_id];
318 ns = pcapng_tsc_to_ns(rte_get_tsc_cycles());
319 hdr->timestamp_hi = ns >> 32;
320 hdr->timestamp_lo = (uint32_t)ns;
322 /* clone block_length after option */
323 memcpy(opt, &len, sizeof(uint32_t));
325 return write(self->outfd, buf, len);
329 rte_pcapng_mbuf_size(uint32_t length)
331 /* The VLAN and EPB header must fit in the mbuf headroom. */
332 RTE_ASSERT(sizeof(struct pcapng_enhance_packet_block) +
333 sizeof(struct rte_vlan_hdr) <= RTE_PKTMBUF_HEADROOM);
335 /* The flags and queue information are added at the end. */
336 return sizeof(struct rte_mbuf)
337 + RTE_ALIGN(length, sizeof(uint32_t))
338 + pcapng_optlen(sizeof(uint32_t)) /* flag option */
339 + pcapng_optlen(sizeof(uint32_t)) /* queue option */
340 + sizeof(uint32_t); /* length */
343 /* More generalized version rte_vlan_insert() */
345 pcapng_vlan_insert(struct rte_mbuf *m, uint16_t ether_type, uint16_t tci)
347 struct rte_ether_hdr *nh, *oh;
348 struct rte_vlan_hdr *vh;
350 if (!RTE_MBUF_DIRECT(m) || rte_mbuf_refcnt_read(m) > 1)
353 if (rte_pktmbuf_data_len(m) < sizeof(*oh))
356 oh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
357 nh = (struct rte_ether_hdr *)
358 rte_pktmbuf_prepend(m, sizeof(struct rte_vlan_hdr));
362 memmove(nh, oh, 2 * RTE_ETHER_ADDR_LEN);
363 nh->ether_type = rte_cpu_to_be_16(ether_type);
365 vh = (struct rte_vlan_hdr *) (nh + 1);
366 vh->vlan_tci = rte_cpu_to_be_16(tci);
372 * The mbufs created use the Pcapng standard enhanced packet block.
375 * 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
376 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
377 * 0 | Block Type = 0x00000006 |
378 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
379 * 4 | Block Total Length |
380 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
382 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383 * 12 | Timestamp (High) |
384 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
385 * 16 | Timestamp (Low) |
386 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
387 * 20 | Captured Packet Length |
388 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
389 * 24 | Original Packet Length |
390 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
393 * / variable length, padded to 32 bits /
395 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
396 * | Option Code = 0x0002 | Option Length = 0x004 |
397 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
398 * | Flags (direction) |
399 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
400 * | Option Code = 0x0006 | Option Length = 0x002 |
401 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
403 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
404 * | Block Total Length |
405 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
408 /* Make a copy of original mbuf with pcapng header and options */
410 rte_pcapng_copy(uint16_t port_id, uint32_t queue,
411 const struct rte_mbuf *md,
412 struct rte_mempool *mp,
413 uint32_t length, uint64_t cycles,
414 enum rte_pcapng_direction direction)
416 struct pcapng_enhance_packet_block *epb;
417 uint32_t orig_len, data_len, padding, flags;
418 struct pcapng_option *opt;
419 const uint16_t optlen = pcapng_optlen(sizeof(flags)) + pcapng_optlen(sizeof(queue));
423 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
424 RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, NULL);
426 ns = pcapng_tsc_to_ns(cycles);
428 orig_len = rte_pktmbuf_pkt_len(md);
430 /* Take snapshot of the data */
431 mc = rte_pktmbuf_copy(md, mp, 0, length);
432 if (unlikely(mc == NULL))
435 /* Expand any offloaded VLAN information */
436 if ((direction == RTE_PCAPNG_DIRECTION_IN &&
437 (md->ol_flags & RTE_MBUF_F_RX_VLAN_STRIPPED)) ||
438 (direction == RTE_PCAPNG_DIRECTION_OUT &&
439 (md->ol_flags & RTE_MBUF_F_TX_VLAN))) {
440 if (pcapng_vlan_insert(mc, RTE_ETHER_TYPE_VLAN,
445 if ((direction == RTE_PCAPNG_DIRECTION_IN &&
446 (md->ol_flags & RTE_MBUF_F_RX_QINQ_STRIPPED)) ||
447 (direction == RTE_PCAPNG_DIRECTION_OUT &&
448 (md->ol_flags & RTE_MBUF_F_TX_QINQ))) {
449 if (pcapng_vlan_insert(mc, RTE_ETHER_TYPE_QINQ,
450 md->vlan_tci_outer) != 0)
454 /* pad the packet to 32 bit boundary */
455 data_len = rte_pktmbuf_data_len(mc);
456 padding = RTE_ALIGN(data_len, sizeof(uint32_t)) - data_len;
458 void *tail = rte_pktmbuf_append(mc, padding);
462 memset(tail, 0, padding);
465 /* reserve trailing options and block length */
466 opt = (struct pcapng_option *)
467 rte_pktmbuf_append(mc, optlen + sizeof(uint32_t));
468 if (unlikely(opt == NULL))
472 case RTE_PCAPNG_DIRECTION_IN:
473 flags = PCAPNG_IFB_INBOUND;
475 case RTE_PCAPNG_DIRECTION_OUT:
476 flags = PCAPNG_IFB_OUTBOUND;
482 opt = pcapng_add_option(opt, PCAPNG_EPB_FLAGS,
483 &flags, sizeof(flags));
485 opt = pcapng_add_option(opt, PCAPNG_EPB_QUEUE,
486 &queue, sizeof(queue));
488 /* Note: END_OPT necessary here. Wireshark doesn't do it. */
490 /* Add PCAPNG packet header */
491 epb = (struct pcapng_enhance_packet_block *)
492 rte_pktmbuf_prepend(mc, sizeof(*epb));
493 if (unlikely(epb == NULL))
496 epb->block_type = PCAPNG_ENHANCED_PACKET_BLOCK;
497 epb->block_length = rte_pktmbuf_data_len(mc);
499 /* Interface index is filled in later during write */
502 epb->timestamp_hi = ns >> 32;
503 epb->timestamp_lo = (uint32_t)ns;
504 epb->capture_length = data_len;
505 epb->original_length = orig_len;
507 /* set trailer of block length */
508 *(uint32_t *)opt = epb->block_length;
513 rte_pktmbuf_free(mc);
517 /* Count how many segments are in this array of mbufs */
519 mbuf_burst_segs(struct rte_mbuf *pkts[], unsigned int n)
521 unsigned int i, iovcnt;
523 for (iovcnt = 0, i = 0; i < n; i++) {
524 const struct rte_mbuf *m = pkts[i];
526 __rte_mbuf_sanity_check(m, 1);
528 iovcnt += m->nb_segs;
533 /* Write pre-formatted packets to file. */
535 rte_pcapng_write_packets(rte_pcapng_t *self,
536 struct rte_mbuf *pkts[], uint16_t nb_pkts)
538 int iovcnt = mbuf_burst_segs(pkts, nb_pkts);
539 struct iovec iov[iovcnt];
543 for (i = cnt = 0; i < nb_pkts; i++) {
544 struct rte_mbuf *m = pkts[i];
545 struct pcapng_enhance_packet_block *epb;
547 /* sanity check that is really a pcapng mbuf */
548 epb = rte_pktmbuf_mtod(m, struct pcapng_enhance_packet_block *);
549 if (unlikely(epb->block_type != PCAPNG_ENHANCED_PACKET_BLOCK ||
550 epb->block_length != rte_pktmbuf_data_len(m))) {
556 * The DPDK port is recorded during pcapng_copy.
557 * Map that to PCAPNG interface in file.
559 epb->interface_id = self->port_index[m->port];
561 iov[cnt].iov_base = rte_pktmbuf_mtod(m, void *);
562 iov[cnt].iov_len = rte_pktmbuf_data_len(m);
564 } while ((m = m->next));
567 ret = writev(self->outfd, iov, iovcnt);
568 if (unlikely(ret < 0))
573 /* Create new pcapng writer handle */
575 rte_pcapng_fdopen(int fd,
576 const char *osname, const char *hardware,
577 const char *appname, const char *comment)
581 self = malloc(sizeof(*self));
589 if (pcapng_section_block(self, osname, hardware, appname, comment) < 0)
592 if (pcapng_interfaces(self) < 0)
602 rte_pcapng_close(rte_pcapng_t *self)