4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 #include <rte_common.h>
46 #include <rte_debug.h>
47 #include <rte_ethdev.h>
48 #include <rte_memory.h>
49 #include <rte_lcore.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_errno.h>
53 #include <rte_kvargs.h>
54 #include <rte_mempool.h>
56 #include <rte_pdump.h>
58 #define PDUMP_PORT_ARG "port"
59 #define PDUMP_PCI_ARG "device_id"
60 #define PDUMP_QUEUE_ARG "queue"
61 #define PDUMP_DIR_ARG "dir"
62 #define PDUMP_RX_DEV_ARG "rx-dev"
63 #define PDUMP_TX_DEV_ARG "tx-dev"
64 #define PDUMP_RING_SIZE_ARG "ring-size"
65 #define PDUMP_MSIZE_ARG "mbuf-size"
66 #define PDUMP_NUM_MBUFS_ARG "total-num-mbufs"
68 #define VDEV_PCAP "eth_pcap_%s_%d,tx_pcap=%s"
69 #define VDEV_IFACE "eth_pcap_%s_%d,tx_iface=%s"
70 #define TX_STREAM_SIZE 64
72 #define MP_NAME "pdump_pool_%d"
74 #define RX_RING "rx_ring_%d"
75 #define TX_RING "tx_ring_%d"
80 /* Maximum long option length for option parsing. */
81 #define APP_ARG_TCPDUMP_MAX_TUPLES 54
82 #define MBUF_POOL_CACHE_SIZE 250
83 #define TX_DESC_PER_QUEUE 512
84 #define RX_DESC_PER_QUEUE 128
85 #define MBUFS_PER_POOL 65535
86 #define MAX_LONG_OPT_SZ 64
87 #define RING_SIZE 16384
92 #define RTE_RING_SZ_MASK (unsigned)(0x0fffffff) /**< Ring size mask */
93 /* true if x is a power of 2 */
94 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
111 const char *valid_pdump_arguments[] = {
125 uint64_t dequeue_pkts;
130 struct pdump_tuples {
135 char rx_dev[TX_STREAM_SIZE];
136 char tx_dev[TX_STREAM_SIZE];
138 uint16_t mbuf_data_size;
139 uint32_t total_num_mbufs;
141 /* params for library API call */
143 struct rte_mempool *mp;
144 struct rte_ring *rx_ring;
145 struct rte_ring *tx_ring;
147 /* params for packet dumping */
148 enum pdump_by dump_by_type;
151 enum pcap_stream rx_vdev_stream_type;
152 enum pcap_stream tx_vdev_stream_type;
153 bool single_pdump_dev;
156 struct pdump_stats stats;
157 } __rte_cache_aligned;
158 static struct pdump_tuples pdump_t[APP_ARG_TCPDUMP_MAX_TUPLES];
167 static struct rte_eth_conf port_conf_default;
168 volatile uint8_t quit_signal;
170 /**< display usage */
172 pdump_usage(const char *prgname)
174 printf("usage: %s [EAL options] -- --pdump "
175 "'(port=<port id> | device_id=<pci id or vdev name>),"
176 "(queue=<queue_id>),"
177 "(rx-dev=<iface or pcap file> |"
178 " tx-dev=<iface or pcap file>,"
179 "[ring-size=<ring size>default:16384],"
180 "[mbuf-size=<mbuf data size>default:2176],"
181 "[total-num-mbufs=<number of mbufs>default:65535]"
187 parse_device_id(const char *key __rte_unused, const char *value,
190 struct pdump_tuples *pt = extra_args;
192 pt->device_id = strdup(value);
193 pt->dump_by_type = DEVICE_ID;
199 parse_queue(const char *key __rte_unused, const char *value, void *extra_args)
202 struct pdump_tuples *pt = extra_args;
204 if (!strcmp(value, "*"))
205 pt->queue = RTE_PDUMP_ALL_QUEUES;
207 n = strtoul(value, NULL, 10);
208 pt->queue = (uint16_t) n;
214 parse_rxtxdev(const char *key, const char *value, void *extra_args)
217 struct pdump_tuples *pt = extra_args;
219 if (!strcmp(key, PDUMP_RX_DEV_ARG)) {
220 snprintf(pt->rx_dev, sizeof(pt->rx_dev), "%s", value);
221 /* identify the tx stream type for pcap vdev */
222 if (if_nametoindex(pt->rx_dev))
223 pt->rx_vdev_stream_type = IFACE;
224 } else if (!strcmp(key, PDUMP_TX_DEV_ARG)) {
225 snprintf(pt->tx_dev, sizeof(pt->tx_dev), "%s", value);
226 /* identify the tx stream type for pcap vdev */
227 if (if_nametoindex(pt->tx_dev))
228 pt->tx_vdev_stream_type = IFACE;
230 printf("invalid dev type %s, must be rx or tx\n", value);
238 parse_uint_value(const char *key, const char *value, void *extra_args)
247 t = strtoul(value, &end, 10);
249 if (errno != 0 || end[0] != 0 || t < v->min || t > v->max) {
250 printf("invalid value:\"%s\" for key:\"%s\", "
251 "value must be >= %"PRIu64" and <= %"PRIu64"\n",
252 value, key, v->min, v->max);
255 if (!strcmp(key, PDUMP_RING_SIZE_ARG) && !POWEROF2(t)) {
256 printf("invalid value:\"%s\" for key:\"%s\", "
257 "value must be power of 2\n", value, key);
269 parse_pdump(const char *optarg)
271 struct rte_kvargs *kvlist;
272 int ret = 0, cnt1, cnt2;
273 struct pdump_tuples *pt;
274 struct parse_val v = {0};
276 pt = &pdump_t[num_tuples];
278 /* initial check for invalid arguments */
279 kvlist = rte_kvargs_parse(optarg, valid_pdump_arguments);
280 if (kvlist == NULL) {
281 printf("--pdump=\"%s\": invalid argument passed\n", optarg);
285 /* port/device_id parsing and validation */
286 cnt1 = rte_kvargs_count(kvlist, PDUMP_PORT_ARG);
287 cnt2 = rte_kvargs_count(kvlist, PDUMP_PCI_ARG);
288 if (!((cnt1 == 1 && cnt2 == 0) || (cnt1 == 0 && cnt2 == 1))) {
289 printf("--pdump=\"%s\": must have either port or "
290 "device_id argument\n", optarg);
293 } else if (cnt1 == 1) {
295 v.max = RTE_MAX_ETHPORTS-1;
296 ret = rte_kvargs_process(kvlist, PDUMP_PORT_ARG,
297 &parse_uint_value, &v);
300 pt->port = (uint8_t) v.val;
301 pt->dump_by_type = PORT_ID;
302 } else if (cnt2 == 1) {
303 ret = rte_kvargs_process(kvlist, PDUMP_PCI_ARG,
304 &parse_device_id, pt);
309 /* queue parsing and validation */
310 cnt1 = rte_kvargs_count(kvlist, PDUMP_QUEUE_ARG);
312 printf("--pdump=\"%s\": must have queue argument\n", optarg);
316 ret = rte_kvargs_process(kvlist, PDUMP_QUEUE_ARG, &parse_queue, pt);
320 /* rx-dev and tx-dev parsing and validation */
321 cnt1 = rte_kvargs_count(kvlist, PDUMP_RX_DEV_ARG);
322 cnt2 = rte_kvargs_count(kvlist, PDUMP_TX_DEV_ARG);
323 if (cnt1 == 0 && cnt2 == 0) {
324 printf("--pdump=\"%s\": must have either rx-dev or "
325 "tx-dev argument\n", optarg);
328 } else if (cnt1 == 1 && cnt2 == 1) {
329 ret = rte_kvargs_process(kvlist, PDUMP_RX_DEV_ARG,
333 ret = rte_kvargs_process(kvlist, PDUMP_TX_DEV_ARG,
337 /* if captured packets has to send to the same vdev */
338 if (!strcmp(pt->rx_dev, pt->tx_dev))
339 pt->single_pdump_dev = true;
340 pt->dir = RTE_PDUMP_FLAG_RXTX;
341 } else if (cnt1 == 1) {
342 ret = rte_kvargs_process(kvlist, PDUMP_RX_DEV_ARG,
346 pt->dir = RTE_PDUMP_FLAG_RX;
347 } else if (cnt2 == 1) {
348 ret = rte_kvargs_process(kvlist, PDUMP_TX_DEV_ARG,
352 pt->dir = RTE_PDUMP_FLAG_TX;
356 /* ring_size parsing and validation */
357 cnt1 = rte_kvargs_count(kvlist, PDUMP_RING_SIZE_ARG);
360 v.max = RTE_RING_SZ_MASK-1;
361 ret = rte_kvargs_process(kvlist, PDUMP_RING_SIZE_ARG,
362 &parse_uint_value, &v);
365 pt->ring_size = (uint32_t) v.val;
367 pt->ring_size = RING_SIZE;
369 /* mbuf_data_size parsing and validation */
370 cnt1 = rte_kvargs_count(kvlist, PDUMP_MSIZE_ARG);
374 ret = rte_kvargs_process(kvlist, PDUMP_MSIZE_ARG,
375 &parse_uint_value, &v);
378 pt->mbuf_data_size = (uint16_t) v.val;
380 pt->mbuf_data_size = RTE_MBUF_DEFAULT_BUF_SIZE;
382 /* total_num_mbufs parsing and validation */
383 cnt1 = rte_kvargs_count(kvlist, PDUMP_NUM_MBUFS_ARG);
387 ret = rte_kvargs_process(kvlist, PDUMP_NUM_MBUFS_ARG,
388 &parse_uint_value, &v);
391 pt->total_num_mbufs = (uint16_t) v.val;
393 pt->total_num_mbufs = MBUFS_PER_POOL;
398 rte_kvargs_free(kvlist);
402 /* Parse the argument given in the command line of the application */
404 launch_args_parse(int argc, char **argv, char *prgname)
408 static struct option long_option[] = {
414 pdump_usage(prgname);
416 /* Parse command line */
417 while ((opt = getopt_long(argc, argv, " ",
418 long_option, &option_index)) != EOF) {
421 if (!strncmp(long_option[option_index].name, "pdump",
423 ret = parse_pdump(optarg);
425 pdump_usage(prgname);
431 pdump_usage(prgname);
440 print_pdump_stats(void)
443 struct pdump_tuples *pt;
445 for (i = 0; i < num_tuples; i++) {
446 printf("##### PDUMP DEBUG STATS #####\n");
448 printf(" -packets dequeued: %"PRIu64"\n",
449 pt->stats.dequeue_pkts);
450 printf(" -packets transmitted to vdev: %"PRIu64"\n",
452 printf(" -packets freed: %"PRIu64"\n",
453 pt->stats.freed_pkts);
458 disable_pdump(struct pdump_tuples *pt)
460 if (pt->dump_by_type == DEVICE_ID)
461 rte_pdump_disable_by_deviceid(pt->device_id, pt->queue,
463 else if (pt->dump_by_type == PORT_ID)
464 rte_pdump_disable(pt->port, pt->queue, pt->dir);
468 pdump_rxtx(struct rte_ring *ring, uint8_t vdev_id, struct pdump_stats *stats)
470 /* write input packets of port to vdev for pdump */
471 struct rte_mbuf *rxtx_bufs[BURST_SIZE];
473 /* first dequeue packets from ring of primary process */
474 const uint16_t nb_in_deq = rte_ring_dequeue_burst(ring,
475 (void *)rxtx_bufs, BURST_SIZE);
476 stats->dequeue_pkts += nb_in_deq;
479 /* then sent on vdev */
480 uint16_t nb_in_txd = rte_eth_tx_burst(
482 0, rxtx_bufs, nb_in_deq);
483 stats->tx_pkts += nb_in_txd;
485 if (unlikely(nb_in_txd < nb_in_deq)) {
487 rte_pktmbuf_free(rxtx_bufs[nb_in_txd]);
489 } while (++nb_in_txd < nb_in_deq);
495 free_ring_data(struct rte_ring *ring, uint8_t vdev_id,
496 struct pdump_stats *stats)
498 while (rte_ring_count(ring))
499 pdump_rxtx(ring, vdev_id, stats);
503 cleanup_pdump_resources(void)
506 struct pdump_tuples *pt;
508 /* disable pdump and free the pdump_tuple resources */
509 for (i = 0; i < num_tuples; i++) {
512 /* remove callbacks */
516 * transmit rest of the enqueued packets of the rings on to
517 * the vdev, in order to release mbufs to the mepool.
519 if (pt->dir & RTE_PDUMP_FLAG_RX)
520 free_ring_data(pt->rx_ring, pt->rx_vdev_id, &pt->stats);
521 if (pt->dir & RTE_PDUMP_FLAG_TX)
522 free_ring_data(pt->tx_ring, pt->tx_vdev_id, &pt->stats);
529 rte_ring_free(pt->rx_ring);
531 rte_ring_free(pt->tx_ring);
536 signal_handler(int sig_num)
538 if (sig_num == SIGINT) {
539 printf("\n\nSignal %d received, preparing to exit...\n",
546 configure_vdev(uint8_t port_id)
548 struct ether_addr addr;
549 const uint16_t rxRings = 0, txRings = 1;
550 const uint8_t nb_ports = rte_eth_dev_count();
554 if (port_id > nb_ports)
557 ret = rte_eth_dev_configure(port_id, rxRings, txRings,
560 rte_exit(EXIT_FAILURE, "dev config failed\n");
562 for (q = 0; q < txRings; q++) {
563 ret = rte_eth_tx_queue_setup(port_id, q, TX_DESC_PER_QUEUE,
564 rte_eth_dev_socket_id(port_id), NULL);
566 rte_exit(EXIT_FAILURE, "queue setup failed\n");
569 ret = rte_eth_dev_start(port_id);
571 rte_exit(EXIT_FAILURE, "dev start failed\n");
573 rte_eth_macaddr_get(port_id, &addr);
574 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
575 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
577 addr.addr_bytes[0], addr.addr_bytes[1],
578 addr.addr_bytes[2], addr.addr_bytes[3],
579 addr.addr_bytes[4], addr.addr_bytes[5]);
581 rte_eth_promiscuous_enable(port_id);
587 create_mp_ring_vdev(void)
591 struct pdump_tuples *pt = NULL;
592 struct rte_mempool *mbuf_pool = NULL;
593 char vdev_args[SIZE];
594 char ring_name[SIZE];
595 char mempool_name[SIZE];
597 for (i = 0; i < num_tuples; i++) {
599 snprintf(mempool_name, SIZE, MP_NAME, i);
600 mbuf_pool = rte_mempool_lookup(mempool_name);
601 if (mbuf_pool == NULL) {
603 mbuf_pool = rte_pktmbuf_pool_create(mempool_name,
605 MBUF_POOL_CACHE_SIZE, 0,
608 if (mbuf_pool == NULL)
609 rte_exit(EXIT_FAILURE,
610 "Mempool creation failed: %s\n",
611 rte_strerror(rte_errno));
615 if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
616 /* if captured packets has to send to the same vdev */
618 snprintf(ring_name, SIZE, RX_RING, i);
619 pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
621 if (pt->rx_ring == NULL)
622 rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
623 rte_strerror(rte_errno),
627 snprintf(ring_name, SIZE, TX_RING, i);
628 pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
630 if (pt->tx_ring == NULL)
631 rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
632 rte_strerror(rte_errno),
636 (pt->rx_vdev_stream_type == IFACE) ?
637 snprintf(vdev_args, SIZE, VDEV_IFACE, RX_STR, i,
639 snprintf(vdev_args, SIZE, VDEV_PCAP, RX_STR, i,
641 if (rte_eth_dev_attach(vdev_args, &portid) < 0)
642 rte_exit(EXIT_FAILURE,
643 "vdev creation failed:%s:%d\n",
645 pt->rx_vdev_id = portid;
648 configure_vdev(pt->rx_vdev_id);
650 if (pt->single_pdump_dev)
651 pt->tx_vdev_id = portid;
653 (pt->tx_vdev_stream_type == IFACE) ?
654 snprintf(vdev_args, SIZE, VDEV_IFACE, TX_STR, i,
656 snprintf(vdev_args, SIZE, VDEV_PCAP, TX_STR, i,
658 if (rte_eth_dev_attach(vdev_args, &portid) < 0)
659 rte_exit(EXIT_FAILURE,
660 "vdev creation failed:"
661 "%s:%d\n", __func__, __LINE__);
662 pt->tx_vdev_id = portid;
665 configure_vdev(pt->tx_vdev_id);
667 } else if (pt->dir == RTE_PDUMP_FLAG_RX) {
670 snprintf(ring_name, SIZE, RX_RING, i);
671 pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
673 if (pt->rx_ring == NULL)
674 rte_exit(EXIT_FAILURE, "%s\n",
675 rte_strerror(rte_errno));
677 (pt->rx_vdev_stream_type == IFACE) ?
678 snprintf(vdev_args, SIZE, VDEV_IFACE, RX_STR, i,
680 snprintf(vdev_args, SIZE, VDEV_PCAP, RX_STR, i,
682 if (rte_eth_dev_attach(vdev_args, &portid) < 0)
683 rte_exit(EXIT_FAILURE,
684 "vdev creation failed:%s:%d\n",
686 pt->rx_vdev_id = portid;
688 configure_vdev(pt->rx_vdev_id);
689 } else if (pt->dir == RTE_PDUMP_FLAG_TX) {
692 snprintf(ring_name, SIZE, TX_RING, i);
693 pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
695 if (pt->tx_ring == NULL)
696 rte_exit(EXIT_FAILURE, "%s\n",
697 rte_strerror(rte_errno));
699 (pt->tx_vdev_stream_type == IFACE) ?
700 snprintf(vdev_args, SIZE, VDEV_IFACE, TX_STR, i,
702 snprintf(vdev_args, SIZE, VDEV_PCAP, TX_STR, i,
704 if (rte_eth_dev_attach(vdev_args, &portid) < 0)
705 rte_exit(EXIT_FAILURE,
706 "vdev creation failed\n");
707 pt->tx_vdev_id = portid;
710 configure_vdev(pt->tx_vdev_id);
719 struct pdump_tuples *pt;
720 int ret = 0, ret1 = 0;
722 for (i = 0; i < num_tuples; i++) {
724 if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
725 if (pt->dump_by_type == DEVICE_ID) {
726 ret = rte_pdump_enable_by_deviceid(
732 ret = rte_pdump_enable_by_deviceid(
738 } else if (pt->dump_by_type == PORT_ID) {
739 ret = rte_pdump_enable(pt->port, pt->queue,
741 pt->rx_ring, pt->mp, NULL);
742 ret1 = rte_pdump_enable(pt->port, pt->queue,
744 pt->tx_ring, pt->mp, NULL);
746 } else if (pt->dir == RTE_PDUMP_FLAG_RX) {
747 if (pt->dump_by_type == DEVICE_ID)
748 ret = rte_pdump_enable_by_deviceid(
751 pt->dir, pt->rx_ring,
753 else if (pt->dump_by_type == PORT_ID)
754 ret = rte_pdump_enable(pt->port, pt->queue,
756 pt->rx_ring, pt->mp, NULL);
757 } else if (pt->dir == RTE_PDUMP_FLAG_TX) {
758 if (pt->dump_by_type == DEVICE_ID)
759 ret = rte_pdump_enable_by_deviceid(
763 pt->tx_ring, pt->mp, NULL);
764 else if (pt->dump_by_type == PORT_ID)
765 ret = rte_pdump_enable(pt->port, pt->queue,
767 pt->tx_ring, pt->mp, NULL);
769 if (ret < 0 || ret1 < 0) {
770 cleanup_pdump_resources();
771 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
780 struct pdump_tuples *pt;
782 while (!quit_signal) {
783 for (i = 0; i < num_tuples; i++) {
785 if (pt->dir & RTE_PDUMP_FLAG_RX)
786 pdump_rxtx(pt->rx_ring, pt->rx_vdev_id,
788 if (pt->dir & RTE_PDUMP_FLAG_TX)
789 pdump_rxtx(pt->tx_ring, pt->tx_vdev_id,
796 main(int argc, char **argv)
802 char c_flag[] = "-c1";
803 char n_flag[] = "-n4";
804 char mp_flag[] = "--proc-type=secondary";
805 char *argp[argc + 3];
807 /* catch ctrl-c so we can print on exit */
808 signal(SIGINT, signal_handler);
815 for (i = 1; i < argc; i++)
816 argp[i + 3] = argv[i];
820 diag = rte_eal_init(argc, argp);
822 rte_panic("Cannot init EAL\n");
827 /* parse app arguments */
829 ret = launch_args_parse(argc, argv, argp[0]);
831 rte_exit(EXIT_FAILURE, "Invalid argument\n");
834 /* create mempool, ring and vdevs info */
835 create_mp_ring_vdev();
839 cleanup_pdump_resources();
840 /* dump debug stats */