1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2020 Microsoft Corporation
4 * DPDK application to dump network traffic
5 * This is designed to look and act like the Wireshark
20 #include <sys/queue.h>
21 #include <sys/types.h>
22 #include <sys/utsname.h>
26 #include <rte_alarm.h>
28 #include <rte_config.h>
29 #include <rte_debug.h>
31 #include <rte_errno.h>
32 #include <rte_ethdev.h>
33 #include <rte_lcore.h>
34 #include <rte_malloc.h>
36 #include <rte_mempool.h>
37 #include <rte_pcapng.h>
38 #include <rte_pdump.h>
40 #include <rte_string_fns.h>
42 #include <rte_version.h>
44 #include <pcap/pcap.h>
47 #define RING_NAME "capture-ring"
48 #define MONITOR_INTERVAL (500 * 1000)
49 #define MBUF_POOL_CACHE_SIZE 32
51 #define SLEEP_THRESHOLD 1000
53 /* command line flags */
54 static const char *progname;
55 static bool quit_signal;
56 static bool group_read;
58 static bool promiscuous_mode = true;
59 static bool use_pcapng = true;
60 static char *output_name;
61 static const char *filter_str;
62 static unsigned int ring_size = 2048;
63 static const char *capture_comment;
64 static uint32_t snaplen = RTE_MBUF_DEFAULT_BUF_SIZE;
67 uint64_t duration; /* nanoseconds */
68 unsigned long packets; /* number of packets in file */
69 size_t size; /* file size (bytes) */
73 static struct rte_bpf_prm *bpf_prm;
74 static uint64_t start_time, end_time;
75 static uint64_t packets_received;
76 static size_t file_size;
79 TAILQ_ENTRY(interface) next;
81 char name[RTE_ETH_NAME_MAX_LEN];
83 struct rte_rxtx_callback *rx_cb[RTE_MAX_QUEUES_PER_PORT];
86 TAILQ_HEAD(interface_list, interface);
87 static struct interface_list interfaces = TAILQ_HEAD_INITIALIZER(interfaces);
88 static struct interface *port2intf[RTE_MAX_ETHPORTS];
90 /* Can do either pcap or pcapng format output */
93 pcap_dumper_t *dumper;
96 static void usage(void)
98 printf("Usage: %s [options] ...\n\n", progname);
99 printf("Capture Interface:\n"
100 " -i <interface> name or port index of interface\n"
101 " -f <capture filter> packet filter in libpcap filter syntax\n");
102 printf(" -s <snaplen>, --snapshot-length <snaplen>\n"
103 " packet snapshot length (def: %u)\n",
104 RTE_MBUF_DEFAULT_BUF_SIZE);
105 printf(" -p, --no-promiscuous-mode\n"
106 " don't capture in promiscuous mode\n"
107 " -D, --list-interfaces print list of interfaces and exit\n"
108 " -d print generated BPF code for capture filter\n"
111 " -c <packet count> stop after n packets (def: infinite)\n"
112 " -a <autostop cond.> ..., --autostop <autostop cond.> ...\n"
113 " duration:NUM - stop after NUM seconds\n"
114 " filesize:NUM - stop this file after NUM kB\n"
115 " packets:NUM - stop after NUM packets\n"
117 " -w <filename> name of file to save (def: tempfile)\n"
118 " -g enable group read access on the output file(s)\n"
119 " -n use pcapng format instead of pcap (default)\n"
120 " -P use libpcap format instead of pcapng\n"
121 " --capture-comment <comment>\n"
122 " add a capture comment to the output file\n"
125 " -q don't report packet capture counts\n"
126 " -v, --version print version information and exit\n"
127 " -h, --help display this help and exit\n"
129 "Use Ctrl-C to stop capturing at any time.\n");
132 static const char *version(void)
134 static char str[128];
136 snprintf(str, sizeof(str),
137 "%s 1.0 (%s)\n", progname, rte_version());
141 /* Parse numeric argument from command line */
142 static unsigned long get_uint(const char *arg, const char *name,
148 u = strtoul(arg, &endp, 0);
149 if (*arg == '\0' || *endp != '\0')
150 rte_exit(EXIT_FAILURE,
151 "Specified %s \"%s\" is not a valid number\n",
153 if (limit && u > limit)
154 rte_exit(EXIT_FAILURE,
155 "Specified %s \"%s\" is too large (greater than %u)\n",
161 /* Set auto stop values */
162 static void auto_stop(char *opt)
166 value = strchr(opt, ':');
168 rte_exit(EXIT_FAILURE,
169 "Missing colon in auto stop parameter\n");
172 if (strcmp(opt, "duration") == 0) {
173 double interval = strtod(value, &endp);
175 if (*value == '\0' || *endp != '\0' || interval <= 0)
176 rte_exit(EXIT_FAILURE,
177 "Invalid duration \"%s\"\n", value);
178 stop.duration = NSEC_PER_SEC * interval;
179 } else if (strcmp(opt, "filesize") == 0) {
180 stop.size = get_uint(value, "filesize", 0) * 1024;
181 } else if (strcmp(opt, "packets") == 0) {
182 stop.packets = get_uint(value, "packets", 0);
184 rte_exit(EXIT_FAILURE,
185 "Unknown autostop parameter \"%s\"\n", opt);
189 /* Add interface to list of interfaces to capture */
190 static void add_interface(uint16_t port, const char *name)
192 struct interface *intf;
194 intf = malloc(sizeof(*intf));
196 rte_exit(EXIT_FAILURE, "no memory for interface\n");
198 memset(intf, 0, sizeof(*intf));
199 rte_strscpy(intf->name, name, sizeof(intf->name));
201 printf("Capturing on '%s'\n", name);
203 port2intf[port] = intf;
204 TAILQ_INSERT_TAIL(&interfaces, intf, next);
207 /* Select all valid DPDK interfaces */
208 static void select_all_interfaces(void)
210 char name[RTE_ETH_NAME_MAX_LEN];
213 RTE_ETH_FOREACH_DEV(p) {
214 if (rte_eth_dev_get_name_by_port(p, name) < 0)
216 add_interface(p, name);
221 * Choose interface to capture if no -i option given.
222 * Select the first DPDK port, this matches what dumpcap does.
224 static void set_default_interface(void)
226 char name[RTE_ETH_NAME_MAX_LEN];
229 RTE_ETH_FOREACH_DEV(p) {
230 if (rte_eth_dev_get_name_by_port(p, name) < 0)
232 add_interface(p, name);
235 rte_exit(EXIT_FAILURE, "No usable interfaces found\n");
238 /* Lookup interface by name or port and add it to the list */
239 static void select_interface(const char *arg)
243 if (strcmp(arg, "*"))
244 select_all_interfaces();
245 else if (rte_eth_dev_get_port_by_name(arg, &port) == 0)
246 add_interface(port, arg);
248 char name[RTE_ETH_NAME_MAX_LEN];
250 port = get_uint(arg, "port_number", UINT16_MAX);
251 if (rte_eth_dev_get_name_by_port(port, name) < 0)
252 rte_exit(EXIT_FAILURE, "Invalid port number %u\n",
254 add_interface(port, name);
258 /* Display list of possible interfaces that can be used. */
259 static void show_interfaces(void)
261 char name[RTE_ETH_NAME_MAX_LEN];
264 RTE_ETH_FOREACH_DEV(p) {
265 if (rte_eth_dev_get_name_by_port(p, name) < 0)
267 printf("%u. %s\n", p, name);
271 static void compile_filter(void)
273 struct bpf_program bf;
276 pcap = pcap_open_dead(DLT_EN10MB, snaplen);
278 rte_exit(EXIT_FAILURE, "can not open pcap\n");
280 if (pcap_compile(pcap, &bf, filter_str,
281 1, PCAP_NETMASK_UNKNOWN) != 0)
282 rte_exit(EXIT_FAILURE, "pcap filter string not valid (%s)\n",
285 bpf_prm = rte_bpf_convert(&bf);
287 rte_exit(EXIT_FAILURE,
288 "bpf convert failed\n");
291 printf("cBPF program (%u insns)\n", bf.bf_len);
293 printf("\neBPF program (%u insns)\n", bpf_prm->nb_ins);
294 rte_bpf_dump(stdout, bpf_prm->ins, bpf_prm->nb_ins);
298 /* Don't care about original program any more */
304 * Parse command line options.
305 * These are chosen to be similar to dumpcap command.
307 static void parse_opts(int argc, char **argv)
309 static const struct option long_options[] = {
310 { "autostop", required_argument, NULL, 'a' },
311 { "capture-comment", required_argument, NULL, 0 },
312 { "help", no_argument, NULL, 'h' },
313 { "interface", required_argument, NULL, 'i' },
314 { "list-interfaces", no_argument, NULL, 'D' },
315 { "no-promiscuous-mode", no_argument, NULL, 'p' },
316 { "output-file", required_argument, NULL, 'w' },
317 { "ring-buffer", required_argument, NULL, 'b' },
318 { "snapshot-length", required_argument, NULL, 's' },
319 { "version", no_argument, NULL, 'v' },
325 c = getopt_long(argc, argv, "a:b:c:dDf:ghi:nN:pPqs:vw:",
326 long_options, &option_index);
332 switch (option_index) {
334 capture_comment = optarg;
345 rte_exit(EXIT_FAILURE,
346 "multiple files not implemented\n");
349 stop.packets = get_uint(optarg, "packet_count", 0);
364 printf("%s\n\n", version());
368 select_interface(optarg);
374 ring_size = get_uint(optarg, "packet_limit", 0);
377 promiscuous_mode = false;
386 snaplen = get_uint(optarg, "snap_len", 0);
389 output_name = optarg;
392 printf("%s\n", version());
395 fprintf(stderr, "Invalid option: %s\n",
404 signal_handler(int sig_num __rte_unused)
406 __atomic_store_n(&quit_signal, true, __ATOMIC_RELAXED);
409 /* Return the time since 1/1/1970 in nanoseconds */
410 static uint64_t create_timestamp(void)
414 clock_gettime(CLOCK_MONOTONIC, &now);
415 return rte_timespec_to_ns(&now);
419 cleanup_pdump_resources(void)
421 struct interface *intf;
423 TAILQ_FOREACH(intf, &interfaces, next) {
424 rte_pdump_disable(intf->port,
425 RTE_PDUMP_ALL_QUEUES, RTE_PDUMP_FLAG_RXTX);
426 if (promiscuous_mode)
427 rte_eth_promiscuous_disable(intf->port);
431 /* Alarm signal handler, used to check that primary process */
433 monitor_primary(void *arg __rte_unused)
435 if (__atomic_load_n(&quit_signal, __ATOMIC_RELAXED))
438 if (rte_eal_primary_proc_alive(NULL)) {
439 rte_eal_alarm_set(MONITOR_INTERVAL, monitor_primary, NULL);
442 "Primary process is no longer active, exiting...\n");
443 __atomic_store_n(&quit_signal, true, __ATOMIC_RELAXED);
447 /* Setup handler to check when primary exits. */
449 enable_primary_monitor(void)
453 /* Once primary exits, so will pdump. */
454 ret = rte_eal_alarm_set(MONITOR_INTERVAL, monitor_primary, NULL);
456 fprintf(stderr, "Fail to enable monitor:%d\n", ret);
460 disable_primary_monitor(void)
464 ret = rte_eal_alarm_cancel(monitor_primary, NULL);
466 fprintf(stderr, "Fail to disable monitor:%d\n", ret);
470 report_packet_stats(dumpcap_out_t out)
472 struct rte_pdump_stats pdump_stats;
473 struct interface *intf;
474 uint64_t ifrecv, ifdrop;
478 TAILQ_FOREACH(intf, &interfaces, next) {
479 if (rte_pdump_stats(intf->port, &pdump_stats) < 0)
482 /* do what Wiretap does */
483 ifrecv = pdump_stats.accepted + pdump_stats.filtered;
484 ifdrop = pdump_stats.nombuf + pdump_stats.ringfull;
487 rte_pcapng_write_stats(out.pcapng, intf->port, NULL,
488 start_time, end_time,
494 percent = 100. * ifrecv / (ifrecv + ifdrop);
497 "Packets received/dropped on interface '%s': "
498 "%"PRIu64 "/%" PRIu64 " (%.1f)\n",
499 intf->name, ifrecv, ifdrop, percent);
504 * Start DPDK EAL with arguments.
505 * Unlike most DPDK programs, this application does not use the
506 * typical EAL command line arguments.
507 * We don't want to expose all the DPDK internals to the user.
509 static void dpdk_init(void)
511 static const char * const args[] = {
512 "dumpcap", "--proc-type", "secondary",
513 "--log-level", "notice"
516 const int eal_argc = RTE_DIM(args);
520 /* DPDK API requires mutable versions of command line arguments. */
521 eal_argv = calloc(eal_argc + 1, sizeof(char *));
522 if (eal_argv == NULL)
523 rte_panic("No memory\n");
525 eal_argv[0] = strdup(progname);
526 for (i = 1; i < RTE_DIM(args); i++)
527 eal_argv[i] = strdup(args[i]);
529 if (rte_eal_init(eal_argc, eal_argv) < 0)
530 rte_exit(EXIT_FAILURE, "EAL init failed: is primary process running?\n");
532 if (rte_eth_dev_count_avail() == 0)
533 rte_exit(EXIT_FAILURE, "No Ethernet ports found\n");
536 /* Create packet ring shared between callbacks and process */
537 static struct rte_ring *create_ring(void)
539 struct rte_ring *ring;
542 /* Find next power of 2 >= size. */
544 log2 = sizeof(size) * 8 - __builtin_clzl(size - 1);
547 if (size != ring_size) {
548 fprintf(stderr, "Ring size %u rounded up to %zu\n",
553 ring = rte_ring_lookup(RING_NAME);
555 ring = rte_ring_create(RING_NAME, ring_size,
558 rte_exit(EXIT_FAILURE, "Could not create ring :%s\n",
559 rte_strerror(rte_errno));
564 static struct rte_mempool *create_mempool(void)
566 static const char pool_name[] = "capture_mbufs";
567 size_t num_mbufs = 2 * ring_size;
568 struct rte_mempool *mp;
570 mp = rte_mempool_lookup(pool_name);
574 mp = rte_pktmbuf_pool_create_by_ops(pool_name, num_mbufs,
575 MBUF_POOL_CACHE_SIZE, 0,
576 rte_pcapng_mbuf_size(snaplen),
577 rte_socket_id(), "ring_mp_sc");
579 rte_exit(EXIT_FAILURE,
580 "Mempool (%s) creation failed: %s\n", pool_name,
581 rte_strerror(rte_errno));
587 * Get Operating System information.
588 * Returns an string allocated via malloc().
590 static char *get_os_info(void)
598 if (asprintf(&osname, "%s %s",
599 uts.sysname, uts.release) == -1)
605 static dumpcap_out_t create_output(void)
608 static char tmp_path[PATH_MAX];
611 /* If no filename specified make a tempfile name */
612 if (output_name == NULL) {
613 struct interface *intf;
618 intf = TAILQ_FIRST(&interfaces);
620 tm = localtime(&now);
622 rte_panic("localtime failed\n");
624 strftime(ts, sizeof(ts), "%Y%m%d%H%M%S", tm);
626 snprintf(tmp_path, sizeof(tmp_path),
627 "/tmp/%s_%u_%s_%s.%s",
628 progname, intf->port, intf->name, ts,
629 use_pcapng ? "pcapng" : "pcap");
630 output_name = tmp_path;
633 if (strcmp(output_name, "-") == 0)
636 mode_t mode = group_read ? 0640 : 0600;
638 fd = open(output_name, O_WRONLY | O_CREAT, mode);
640 rte_exit(EXIT_FAILURE, "Can not open \"%s\": %s\n",
641 output_name, strerror(errno));
645 char *os = get_os_info();
647 ret.pcapng = rte_pcapng_fdopen(fd, os, NULL,
648 version(), capture_comment);
649 if (ret.pcapng == NULL)
650 rte_exit(EXIT_FAILURE, "pcapng_fdopen failed: %s\n",
651 strerror(rte_errno));
656 pcap = pcap_open_dead_with_tstamp_precision(DLT_EN10MB, snaplen,
657 PCAP_TSTAMP_PRECISION_NANO);
659 rte_exit(EXIT_FAILURE, "pcap_open_dead failed\n");
661 ret.dumper = pcap_dump_fopen(pcap, fdopen(fd, "w"));
662 if (ret.dumper == NULL)
663 rte_exit(EXIT_FAILURE, "pcap_dump_fopen failed: %s\n",
670 static void enable_pdump(struct rte_ring *r, struct rte_mempool *mp)
672 struct interface *intf;
676 flags = RTE_PDUMP_FLAG_RXTX;
678 flags |= RTE_PDUMP_FLAG_PCAPNG;
680 TAILQ_FOREACH(intf, &interfaces, next) {
681 if (promiscuous_mode)
682 rte_eth_promiscuous_enable(intf->port);
684 ret = rte_pdump_enable_bpf(intf->port, RTE_PDUMP_ALL_QUEUES,
688 rte_exit(EXIT_FAILURE,
689 "Packet dump enable failed: %s\n",
695 * Show current count of captured packets
696 * with backspaces to overwrite last value.
698 static void show_count(uint64_t count)
701 static unsigned int bt;
703 for (i = 0; i < bt; i++)
706 bt = fprintf(stderr, "%"PRIu64" ", count);
709 /* Write multiple packets in older pcap format */
711 pcap_write_packets(pcap_dumper_t *dumper,
712 struct rte_mbuf *pkts[], uint16_t n)
714 uint8_t temp_data[snaplen];
715 struct pcap_pkthdr header;
719 gettimeofday(&header.ts, NULL);
721 for (i = 0; i < n; i++) {
722 struct rte_mbuf *m = pkts[i];
724 header.len = rte_pktmbuf_pkt_len(m);
725 header.caplen = RTE_MIN(header.len, snaplen);
727 pcap_dump((u_char *)dumper, &header,
728 rte_pktmbuf_read(m, 0, header.caplen, temp_data));
730 total += sizeof(header) + header.len;
736 /* Process all packets in ring and dump to capture file */
737 static int process_ring(dumpcap_out_t out, struct rte_ring *r)
739 struct rte_mbuf *pkts[BURST_SIZE];
740 unsigned int avail, n;
741 static unsigned int empty_count;
744 n = rte_ring_sc_dequeue_burst(r, (void **) pkts, BURST_SIZE,
747 /* don't consume endless amounts of cpu if idle */
748 if (empty_count < SLEEP_THRESHOLD)
755 empty_count = (avail == 0);
758 written = rte_pcapng_write_packets(out.pcapng, pkts, n);
760 written = pcap_write_packets(out.dumper, pkts, n);
762 rte_pktmbuf_free_bulk(pkts, n);
767 file_size += written;
768 packets_received += n;
770 show_count(packets_received);
775 int main(int argc, char **argv)
778 struct rte_mempool *mp;
784 parse_opts(argc, argv);
789 if (TAILQ_EMPTY(&interfaces))
790 set_default_interface();
793 mp = create_mempool();
794 out = create_output();
796 start_time = create_timestamp();
799 signal(SIGINT, signal_handler);
800 signal(SIGPIPE, SIG_IGN);
802 enable_primary_monitor();
805 fprintf(stderr, "Packets captured: ");
809 while (!__atomic_load_n(&quit_signal, __ATOMIC_RELAXED)) {
810 if (process_ring(out, r) < 0) {
811 fprintf(stderr, "pcapng file write failed; %s\n",
816 if (stop.size && file_size >= stop.size)
819 if (stop.packets && packets_received >= stop.packets)
822 if (stop.duration != 0 &&
823 create_timestamp() - start_time > stop.duration)
827 end_time = create_timestamp();
828 disable_primary_monitor();
830 if (rte_eal_primary_proc_alive(NULL))
831 report_packet_stats(out);
834 rte_pcapng_close(out.pcapng);
836 pcap_dump_close(out.dumper);
838 cleanup_pdump_resources();
839 rte_free(bpf_filter);
841 rte_mempool_free(mp);
843 return rte_eal_cleanup() ? EXIT_FAILURE : 0;