pdump: use generic multi-process channel
[dpdk.git] / app / pdump / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <stdlib.h>
10 #include <getopt.h>
11 #include <signal.h>
12 #include <stdbool.h>
13 #include <net/if.h>
14
15 #include <rte_eal.h>
16 #include <rte_common.h>
17 #include <rte_debug.h>
18 #include <rte_ethdev.h>
19 #include <rte_memory.h>
20 #include <rte_lcore.h>
21 #include <rte_branch_prediction.h>
22 #include <rte_errno.h>
23 #include <rte_dev.h>
24 #include <rte_kvargs.h>
25 #include <rte_mempool.h>
26 #include <rte_ring.h>
27 #include <rte_string_fns.h>
28 #include <rte_pdump.h>
29
30 #define CMD_LINE_OPT_PDUMP "pdump"
31 #define PDUMP_PORT_ARG "port"
32 #define PDUMP_PCI_ARG "device_id"
33 #define PDUMP_QUEUE_ARG "queue"
34 #define PDUMP_DIR_ARG "dir"
35 #define PDUMP_RX_DEV_ARG "rx-dev"
36 #define PDUMP_TX_DEV_ARG "tx-dev"
37 #define PDUMP_RING_SIZE_ARG "ring-size"
38 #define PDUMP_MSIZE_ARG "mbuf-size"
39 #define PDUMP_NUM_MBUFS_ARG "total-num-mbufs"
40 #define CMD_LINE_OPT_SER_SOCK_PATH "server-socket-path"
41 #define CMD_LINE_OPT_CLI_SOCK_PATH "client-socket-path"
42
43 #define VDEV_PCAP "net_pcap_%s_%d,tx_pcap=%s"
44 #define VDEV_IFACE "net_pcap_%s_%d,tx_iface=%s"
45 #define TX_STREAM_SIZE 64
46
47 #define MP_NAME "pdump_pool_%d"
48
49 #define RX_RING "rx_ring_%d"
50 #define TX_RING "tx_ring_%d"
51
52 #define RX_STR "rx"
53 #define TX_STR "tx"
54
55 /* Maximum long option length for option parsing. */
56 #define APP_ARG_TCPDUMP_MAX_TUPLES 54
57 #define MBUF_POOL_CACHE_SIZE 250
58 #define TX_DESC_PER_QUEUE 512
59 #define RX_DESC_PER_QUEUE 128
60 #define MBUFS_PER_POOL 65535
61 #define MAX_LONG_OPT_SZ 64
62 #define RING_SIZE 16384
63 #define SIZE 256
64 #define BURST_SIZE 32
65 #define NUM_VDEVS 2
66
67 /* true if x is a power of 2 */
68 #define POWEROF2(x) ((((x)-1) & (x)) == 0)
69
70 enum pdump_en_dis {
71         DISABLE = 1,
72         ENABLE = 2
73 };
74
75 enum pcap_stream {
76         IFACE = 1,
77         PCAP = 2
78 };
79
80 enum pdump_by {
81         PORT_ID = 1,
82         DEVICE_ID = 2
83 };
84
85 const char *valid_pdump_arguments[] = {
86         PDUMP_PORT_ARG,
87         PDUMP_PCI_ARG,
88         PDUMP_QUEUE_ARG,
89         PDUMP_DIR_ARG,
90         PDUMP_RX_DEV_ARG,
91         PDUMP_TX_DEV_ARG,
92         PDUMP_RING_SIZE_ARG,
93         PDUMP_MSIZE_ARG,
94         PDUMP_NUM_MBUFS_ARG,
95         NULL
96 };
97
98 struct pdump_stats {
99         uint64_t dequeue_pkts;
100         uint64_t tx_pkts;
101         uint64_t freed_pkts;
102 };
103
104 struct pdump_tuples {
105         /* cli params */
106         uint16_t port;
107         char *device_id;
108         uint16_t queue;
109         char rx_dev[TX_STREAM_SIZE];
110         char tx_dev[TX_STREAM_SIZE];
111         uint32_t ring_size;
112         uint16_t mbuf_data_size;
113         uint32_t total_num_mbufs;
114
115         /* params for library API call */
116         uint32_t dir;
117         struct rte_mempool *mp;
118         struct rte_ring *rx_ring;
119         struct rte_ring *tx_ring;
120
121         /* params for packet dumping */
122         enum pdump_by dump_by_type;
123         int rx_vdev_id;
124         int tx_vdev_id;
125         enum pcap_stream rx_vdev_stream_type;
126         enum pcap_stream tx_vdev_stream_type;
127         bool single_pdump_dev;
128
129         /* stats */
130         struct pdump_stats stats;
131 } __rte_cache_aligned;
132 static struct pdump_tuples pdump_t[APP_ARG_TCPDUMP_MAX_TUPLES];
133
134 struct parse_val {
135         uint64_t min;
136         uint64_t max;
137         uint64_t val;
138 };
139
140 int num_tuples;
141 static struct rte_eth_conf port_conf_default;
142 volatile uint8_t quit_signal;
143 static char server_socket_path[PATH_MAX];
144 static char client_socket_path[PATH_MAX];
145
146 /**< display usage */
147 static void
148 pdump_usage(const char *prgname)
149 {
150         printf("usage: %s [EAL options] -- --pdump "
151                         "'(port=<port id> | device_id=<pci id or vdev name>),"
152                         "(queue=<queue_id>),"
153                         "(rx-dev=<iface or pcap file> |"
154                         " tx-dev=<iface or pcap file>,"
155                         "[ring-size=<ring size>default:16384],"
156                         "[mbuf-size=<mbuf data size>default:2176],"
157                         "[total-num-mbufs=<number of mbufs>default:65535]'\n"
158                         "[--server-socket-path=<server socket dir>"
159                                 " which is deprecated and will be removed soon,"
160                                 " default:/var/run/.dpdk/ (or) ~/.dpdk/]\n"
161                         "[--client-socket-path=<client socket dir>"
162                                 " which is deprecated and will be removed soon,"
163                                 " default:/var/run/.dpdk/ (or) ~/.dpdk/]\n",
164                         prgname);
165 }
166
167 static int
168 parse_device_id(const char *key __rte_unused, const char *value,
169                 void *extra_args)
170 {
171         struct pdump_tuples *pt = extra_args;
172
173         pt->device_id = strdup(value);
174         pt->dump_by_type = DEVICE_ID;
175
176         return 0;
177 }
178
179 static int
180 parse_queue(const char *key __rte_unused, const char *value, void *extra_args)
181 {
182         unsigned long n;
183         struct pdump_tuples *pt = extra_args;
184
185         if (!strcmp(value, "*"))
186                 pt->queue = RTE_PDUMP_ALL_QUEUES;
187         else {
188                 n = strtoul(value, NULL, 10);
189                 pt->queue = (uint16_t) n;
190         }
191         return 0;
192 }
193
194 static int
195 parse_rxtxdev(const char *key, const char *value, void *extra_args)
196 {
197
198         struct pdump_tuples *pt = extra_args;
199
200         if (!strcmp(key, PDUMP_RX_DEV_ARG)) {
201                 snprintf(pt->rx_dev, sizeof(pt->rx_dev), "%s", value);
202                 /* identify the tx stream type for pcap vdev */
203                 if (if_nametoindex(pt->rx_dev))
204                         pt->rx_vdev_stream_type = IFACE;
205         } else if (!strcmp(key, PDUMP_TX_DEV_ARG)) {
206                 snprintf(pt->tx_dev, sizeof(pt->tx_dev), "%s", value);
207                 /* identify the tx stream type for pcap vdev */
208                 if (if_nametoindex(pt->tx_dev))
209                         pt->tx_vdev_stream_type = IFACE;
210         }
211
212         return 0;
213 }
214
215 static int
216 parse_uint_value(const char *key, const char *value, void *extra_args)
217 {
218         struct parse_val *v;
219         unsigned long t;
220         char *end;
221         int ret = 0;
222
223         errno = 0;
224         v = extra_args;
225         t = strtoul(value, &end, 10);
226
227         if (errno != 0 || end[0] != 0 || t < v->min || t > v->max) {
228                 printf("invalid value:\"%s\" for key:\"%s\", "
229                         "value must be >= %"PRIu64" and <= %"PRIu64"\n",
230                         value, key, v->min, v->max);
231                 ret = -EINVAL;
232         }
233         if (!strcmp(key, PDUMP_RING_SIZE_ARG) && !POWEROF2(t)) {
234                 printf("invalid value:\"%s\" for key:\"%s\", "
235                         "value must be power of 2\n", value, key);
236                 ret = -EINVAL;
237         }
238
239         if (ret != 0)
240                 return ret;
241
242         v->val = t;
243         return 0;
244 }
245
246 static int
247 parse_pdump(const char *optarg)
248 {
249         struct rte_kvargs *kvlist;
250         int ret = 0, cnt1, cnt2;
251         struct pdump_tuples *pt;
252         struct parse_val v = {0};
253
254         pt = &pdump_t[num_tuples];
255
256         /* initial check for invalid arguments */
257         kvlist = rte_kvargs_parse(optarg, valid_pdump_arguments);
258         if (kvlist == NULL) {
259                 printf("--pdump=\"%s\": invalid argument passed\n", optarg);
260                 return -1;
261         }
262
263         /* port/device_id parsing and validation */
264         cnt1 = rte_kvargs_count(kvlist, PDUMP_PORT_ARG);
265         cnt2 = rte_kvargs_count(kvlist, PDUMP_PCI_ARG);
266         if (!((cnt1 == 1 && cnt2 == 0) || (cnt1 == 0 && cnt2 == 1))) {
267                 printf("--pdump=\"%s\": must have either port or "
268                         "device_id argument\n", optarg);
269                 ret = -1;
270                 goto free_kvlist;
271         } else if (cnt1 == 1) {
272                 v.min = 0;
273                 v.max = RTE_MAX_ETHPORTS-1;
274                 ret = rte_kvargs_process(kvlist, PDUMP_PORT_ARG,
275                                 &parse_uint_value, &v);
276                 if (ret < 0)
277                         goto free_kvlist;
278                 pt->port = (uint8_t) v.val;
279                 pt->dump_by_type = PORT_ID;
280         } else if (cnt2 == 1) {
281                 ret = rte_kvargs_process(kvlist, PDUMP_PCI_ARG,
282                                 &parse_device_id, pt);
283                 if (ret < 0)
284                         goto free_kvlist;
285         }
286
287         /* queue parsing and validation */
288         cnt1 = rte_kvargs_count(kvlist, PDUMP_QUEUE_ARG);
289         if (cnt1 != 1) {
290                 printf("--pdump=\"%s\": must have queue argument\n", optarg);
291                 ret = -1;
292                 goto free_kvlist;
293         }
294         ret = rte_kvargs_process(kvlist, PDUMP_QUEUE_ARG, &parse_queue, pt);
295         if (ret < 0)
296                 goto free_kvlist;
297
298         /* rx-dev and tx-dev parsing and validation */
299         cnt1 = rte_kvargs_count(kvlist, PDUMP_RX_DEV_ARG);
300         cnt2 = rte_kvargs_count(kvlist, PDUMP_TX_DEV_ARG);
301         if (cnt1 == 0 && cnt2 == 0) {
302                 printf("--pdump=\"%s\": must have either rx-dev or "
303                         "tx-dev argument\n", optarg);
304                 ret = -1;
305                 goto free_kvlist;
306         } else if (cnt1 == 1 && cnt2 == 1) {
307                 ret = rte_kvargs_process(kvlist, PDUMP_RX_DEV_ARG,
308                                         &parse_rxtxdev, pt);
309                 if (ret < 0)
310                         goto free_kvlist;
311                 ret = rte_kvargs_process(kvlist, PDUMP_TX_DEV_ARG,
312                                         &parse_rxtxdev, pt);
313                 if (ret < 0)
314                         goto free_kvlist;
315                 /* if captured packets has to send to the same vdev */
316                 if (!strcmp(pt->rx_dev, pt->tx_dev))
317                         pt->single_pdump_dev = true;
318                 pt->dir = RTE_PDUMP_FLAG_RXTX;
319         } else if (cnt1 == 1) {
320                 ret = rte_kvargs_process(kvlist, PDUMP_RX_DEV_ARG,
321                                         &parse_rxtxdev, pt);
322                 if (ret < 0)
323                         goto free_kvlist;
324                 pt->dir = RTE_PDUMP_FLAG_RX;
325         } else if (cnt2 == 1) {
326                 ret = rte_kvargs_process(kvlist, PDUMP_TX_DEV_ARG,
327                                         &parse_rxtxdev, pt);
328                 if (ret < 0)
329                         goto free_kvlist;
330                 pt->dir = RTE_PDUMP_FLAG_TX;
331         }
332
333         /* optional */
334         /* ring_size parsing and validation */
335         cnt1 = rte_kvargs_count(kvlist, PDUMP_RING_SIZE_ARG);
336         if (cnt1 == 1) {
337                 v.min = 2;
338                 v.max = RTE_RING_SZ_MASK-1;
339                 ret = rte_kvargs_process(kvlist, PDUMP_RING_SIZE_ARG,
340                                                 &parse_uint_value, &v);
341                 if (ret < 0)
342                         goto free_kvlist;
343                 pt->ring_size = (uint32_t) v.val;
344         } else
345                 pt->ring_size = RING_SIZE;
346
347         /* mbuf_data_size parsing and validation */
348         cnt1 = rte_kvargs_count(kvlist, PDUMP_MSIZE_ARG);
349         if (cnt1 == 1) {
350                 v.min = 1;
351                 v.max = UINT16_MAX;
352                 ret = rte_kvargs_process(kvlist, PDUMP_MSIZE_ARG,
353                                                 &parse_uint_value, &v);
354                 if (ret < 0)
355                         goto free_kvlist;
356                 pt->mbuf_data_size = (uint16_t) v.val;
357         } else
358                 pt->mbuf_data_size = RTE_MBUF_DEFAULT_BUF_SIZE;
359
360         /* total_num_mbufs parsing and validation */
361         cnt1 = rte_kvargs_count(kvlist, PDUMP_NUM_MBUFS_ARG);
362         if (cnt1 == 1) {
363                 v.min = 1025;
364                 v.max = UINT16_MAX;
365                 ret = rte_kvargs_process(kvlist, PDUMP_NUM_MBUFS_ARG,
366                                                 &parse_uint_value, &v);
367                 if (ret < 0)
368                         goto free_kvlist;
369                 pt->total_num_mbufs = (uint16_t) v.val;
370         } else
371                 pt->total_num_mbufs = MBUFS_PER_POOL;
372
373         num_tuples++;
374
375 free_kvlist:
376         rte_kvargs_free(kvlist);
377         return ret;
378 }
379
380 /* Parse the argument given in the command line of the application */
381 static int
382 launch_args_parse(int argc, char **argv, char *prgname)
383 {
384         int opt, ret;
385         int option_index;
386         static struct option long_option[] = {
387                 {"pdump", 1, 0, 0},
388                 {"server-socket-path", 1, 0, 0},
389                 {"client-socket-path", 1, 0, 0},
390                 {NULL, 0, 0, 0}
391         };
392
393         if (argc == 1)
394                 pdump_usage(prgname);
395
396         /* Parse command line */
397         while ((opt = getopt_long(argc, argv, " ",
398                         long_option, &option_index)) != EOF) {
399                 switch (opt) {
400                 case 0:
401                         if (!strncmp(long_option[option_index].name,
402                                         CMD_LINE_OPT_PDUMP,
403                                         sizeof(CMD_LINE_OPT_PDUMP))) {
404                                 ret = parse_pdump(optarg);
405                                 if (ret) {
406                                         pdump_usage(prgname);
407                                         return -1;
408                                 }
409                         }
410
411                         if (!strncmp(long_option[option_index].name,
412                                         CMD_LINE_OPT_SER_SOCK_PATH,
413                                         sizeof(CMD_LINE_OPT_SER_SOCK_PATH))) {
414                                 strlcpy(server_socket_path, optarg,
415                                         sizeof(server_socket_path));
416                         }
417
418                         if (!strncmp(long_option[option_index].name,
419                                         CMD_LINE_OPT_CLI_SOCK_PATH,
420                                         sizeof(CMD_LINE_OPT_CLI_SOCK_PATH))) {
421                                 strlcpy(client_socket_path, optarg,
422                                         sizeof(client_socket_path));
423                         }
424
425                         break;
426                 default:
427                         pdump_usage(prgname);
428                         return -1;
429                 }
430         }
431
432         return 0;
433 }
434
435 static void
436 print_pdump_stats(void)
437 {
438         int i;
439         struct pdump_tuples *pt;
440
441         for (i = 0; i < num_tuples; i++) {
442                 printf("##### PDUMP DEBUG STATS #####\n");
443                 pt = &pdump_t[i];
444                 printf(" -packets dequeued:                     %"PRIu64"\n",
445                                                         pt->stats.dequeue_pkts);
446                 printf(" -packets transmitted to vdev:          %"PRIu64"\n",
447                                                         pt->stats.tx_pkts);
448                 printf(" -packets freed:                        %"PRIu64"\n",
449                                                         pt->stats.freed_pkts);
450         }
451 }
452
453 static inline void
454 disable_pdump(struct pdump_tuples *pt)
455 {
456         if (pt->dump_by_type == DEVICE_ID)
457                 rte_pdump_disable_by_deviceid(pt->device_id, pt->queue,
458                                                 pt->dir);
459         else if (pt->dump_by_type == PORT_ID)
460                 rte_pdump_disable(pt->port, pt->queue, pt->dir);
461 }
462
463 static inline void
464 pdump_rxtx(struct rte_ring *ring, uint8_t vdev_id, struct pdump_stats *stats)
465 {
466         /* write input packets of port to vdev for pdump */
467         struct rte_mbuf *rxtx_bufs[BURST_SIZE];
468
469         /* first dequeue packets from ring of primary process */
470         const uint16_t nb_in_deq = rte_ring_dequeue_burst(ring,
471                         (void *)rxtx_bufs, BURST_SIZE, NULL);
472         stats->dequeue_pkts += nb_in_deq;
473
474         if (nb_in_deq) {
475                 /* then sent on vdev */
476                 uint16_t nb_in_txd = rte_eth_tx_burst(
477                                 vdev_id,
478                                 0, rxtx_bufs, nb_in_deq);
479                 stats->tx_pkts += nb_in_txd;
480
481                 if (unlikely(nb_in_txd < nb_in_deq)) {
482                         do {
483                                 rte_pktmbuf_free(rxtx_bufs[nb_in_txd]);
484                                 stats->freed_pkts++;
485                         } while (++nb_in_txd < nb_in_deq);
486                 }
487         }
488 }
489
490 static void
491 free_ring_data(struct rte_ring *ring, uint8_t vdev_id,
492                 struct pdump_stats *stats)
493 {
494         while (rte_ring_count(ring))
495                 pdump_rxtx(ring, vdev_id, stats);
496 }
497
498 static void
499 cleanup_rings(void)
500 {
501         int i;
502         struct pdump_tuples *pt;
503
504         for (i = 0; i < num_tuples; i++) {
505                 pt = &pdump_t[i];
506
507                 if (pt->device_id)
508                         free(pt->device_id);
509
510                 /* free the rings */
511                 if (pt->rx_ring)
512                         rte_ring_free(pt->rx_ring);
513                 if (pt->tx_ring)
514                         rte_ring_free(pt->tx_ring);
515         }
516 }
517
518 static void
519 cleanup_pdump_resources(void)
520 {
521         int i;
522         struct pdump_tuples *pt;
523
524         /* disable pdump and free the pdump_tuple resources */
525         for (i = 0; i < num_tuples; i++) {
526                 pt = &pdump_t[i];
527
528                 /* remove callbacks */
529                 disable_pdump(pt);
530
531                 /*
532                 * transmit rest of the enqueued packets of the rings on to
533                 * the vdev, in order to release mbufs to the mepool.
534                 **/
535                 if (pt->dir & RTE_PDUMP_FLAG_RX)
536                         free_ring_data(pt->rx_ring, pt->rx_vdev_id, &pt->stats);
537                 if (pt->dir & RTE_PDUMP_FLAG_TX)
538                         free_ring_data(pt->tx_ring, pt->tx_vdev_id, &pt->stats);
539         }
540         cleanup_rings();
541 }
542
543 static void
544 signal_handler(int sig_num)
545 {
546         if (sig_num == SIGINT) {
547                 printf("\n\nSignal %d received, preparing to exit...\n",
548                                 sig_num);
549                 quit_signal = 1;
550         }
551 }
552
553 static inline int
554 configure_vdev(uint16_t port_id)
555 {
556         struct ether_addr addr;
557         const uint16_t rxRings = 0, txRings = 1;
558         int ret;
559         uint16_t q;
560
561         if (!rte_eth_dev_is_valid_port(port_id))
562                 return -1;
563
564         ret = rte_eth_dev_configure(port_id, rxRings, txRings,
565                                         &port_conf_default);
566         if (ret != 0)
567                 rte_exit(EXIT_FAILURE, "dev config failed\n");
568
569          for (q = 0; q < txRings; q++) {
570                 ret = rte_eth_tx_queue_setup(port_id, q, TX_DESC_PER_QUEUE,
571                                 rte_eth_dev_socket_id(port_id), NULL);
572                 if (ret < 0)
573                         rte_exit(EXIT_FAILURE, "queue setup failed\n");
574         }
575
576         ret = rte_eth_dev_start(port_id);
577         if (ret < 0)
578                 rte_exit(EXIT_FAILURE, "dev start failed\n");
579
580         rte_eth_macaddr_get(port_id, &addr);
581         printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
582                         " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
583                         port_id,
584                         addr.addr_bytes[0], addr.addr_bytes[1],
585                         addr.addr_bytes[2], addr.addr_bytes[3],
586                         addr.addr_bytes[4], addr.addr_bytes[5]);
587
588         rte_eth_promiscuous_enable(port_id);
589
590         return 0;
591 }
592
593 static void
594 create_mp_ring_vdev(void)
595 {
596         int i;
597         uint16_t portid;
598         struct pdump_tuples *pt = NULL;
599         struct rte_mempool *mbuf_pool = NULL;
600         char vdev_args[SIZE];
601         char ring_name[SIZE];
602         char mempool_name[SIZE];
603
604         for (i = 0; i < num_tuples; i++) {
605                 pt = &pdump_t[i];
606                 snprintf(mempool_name, SIZE, MP_NAME, i);
607                 mbuf_pool = rte_mempool_lookup(mempool_name);
608                 if (mbuf_pool == NULL) {
609                         /* create mempool */
610                         mbuf_pool = rte_pktmbuf_pool_create(mempool_name,
611                                         pt->total_num_mbufs,
612                                         MBUF_POOL_CACHE_SIZE, 0,
613                                         pt->mbuf_data_size,
614                                         rte_socket_id());
615                         if (mbuf_pool == NULL) {
616                                 cleanup_rings();
617                                 rte_exit(EXIT_FAILURE,
618                                         "Mempool creation failed: %s\n",
619                                         rte_strerror(rte_errno));
620                         }
621                 }
622                 pt->mp = mbuf_pool;
623
624                 if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
625                         /* if captured packets has to send to the same vdev */
626                         /* create rx_ring */
627                         snprintf(ring_name, SIZE, RX_RING, i);
628                         pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
629                                         rte_socket_id(), 0);
630                         if (pt->rx_ring == NULL) {
631                                 cleanup_rings();
632                                 rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
633                                                 rte_strerror(rte_errno),
634                                                 __func__, __LINE__);
635                         }
636
637                         /* create tx_ring */
638                         snprintf(ring_name, SIZE, TX_RING, i);
639                         pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
640                                         rte_socket_id(), 0);
641                         if (pt->tx_ring == NULL) {
642                                 cleanup_rings();
643                                 rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
644                                                 rte_strerror(rte_errno),
645                                                 __func__, __LINE__);
646                         }
647
648                         /* create vdevs */
649                         (pt->rx_vdev_stream_type == IFACE) ?
650                         snprintf(vdev_args, SIZE, VDEV_IFACE, RX_STR, i,
651                         pt->rx_dev) :
652                         snprintf(vdev_args, SIZE, VDEV_PCAP, RX_STR, i,
653                         pt->rx_dev);
654                         if (rte_eth_dev_attach(vdev_args, &portid) < 0) {
655                                 cleanup_rings();
656                                 rte_exit(EXIT_FAILURE,
657                                         "vdev creation failed:%s:%d\n",
658                                         __func__, __LINE__);
659                         }
660                         pt->rx_vdev_id = portid;
661
662                         /* configure vdev */
663                         configure_vdev(pt->rx_vdev_id);
664
665                         if (pt->single_pdump_dev)
666                                 pt->tx_vdev_id = portid;
667                         else {
668                                 (pt->tx_vdev_stream_type == IFACE) ?
669                                 snprintf(vdev_args, SIZE, VDEV_IFACE, TX_STR, i,
670                                 pt->tx_dev) :
671                                 snprintf(vdev_args, SIZE, VDEV_PCAP, TX_STR, i,
672                                 pt->tx_dev);
673                                 if (rte_eth_dev_attach(vdev_args,
674                                                         &portid) < 0) {
675                                         cleanup_rings();
676                                         rte_exit(EXIT_FAILURE,
677                                                 "vdev creation failed:"
678                                                 "%s:%d\n", __func__, __LINE__);
679                                 }
680                                 pt->tx_vdev_id = portid;
681
682                                 /* configure vdev */
683                                 configure_vdev(pt->tx_vdev_id);
684                         }
685                 } else if (pt->dir == RTE_PDUMP_FLAG_RX) {
686
687                         /* create rx_ring */
688                         snprintf(ring_name, SIZE, RX_RING, i);
689                         pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
690                                         rte_socket_id(), 0);
691                         if (pt->rx_ring == NULL) {
692                                 cleanup_rings();
693                                 rte_exit(EXIT_FAILURE, "%s\n",
694                                         rte_strerror(rte_errno));
695                         }
696
697                         (pt->rx_vdev_stream_type == IFACE) ?
698                         snprintf(vdev_args, SIZE, VDEV_IFACE, RX_STR, i,
699                                 pt->rx_dev) :
700                         snprintf(vdev_args, SIZE, VDEV_PCAP, RX_STR, i,
701                                 pt->rx_dev);
702                         if (rte_eth_dev_attach(vdev_args, &portid) < 0) {
703                                 cleanup_rings();
704                                 rte_exit(EXIT_FAILURE,
705                                         "vdev creation failed:%s:%d\n",
706                                         __func__, __LINE__);
707                         }
708                         pt->rx_vdev_id = portid;
709                         /* configure vdev */
710                         configure_vdev(pt->rx_vdev_id);
711                 } else if (pt->dir == RTE_PDUMP_FLAG_TX) {
712
713                         /* create tx_ring */
714                         snprintf(ring_name, SIZE, TX_RING, i);
715                         pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
716                                         rte_socket_id(), 0);
717                         if (pt->tx_ring == NULL) {
718                                 cleanup_rings();
719                                 rte_exit(EXIT_FAILURE, "%s\n",
720                                         rte_strerror(rte_errno));
721                         }
722
723                         (pt->tx_vdev_stream_type == IFACE) ?
724                         snprintf(vdev_args, SIZE, VDEV_IFACE, TX_STR, i,
725                                 pt->tx_dev) :
726                         snprintf(vdev_args, SIZE, VDEV_PCAP, TX_STR, i,
727                                 pt->tx_dev);
728                         if (rte_eth_dev_attach(vdev_args, &portid) < 0) {
729                                 cleanup_rings();
730                                 rte_exit(EXIT_FAILURE,
731                                         "vdev creation failed\n");
732                         }
733                         pt->tx_vdev_id = portid;
734
735                         /* configure vdev */
736                         configure_vdev(pt->tx_vdev_id);
737                 }
738         }
739 }
740
741 static void
742 enable_pdump(void)
743 {
744         int i;
745         struct pdump_tuples *pt;
746         int ret = 0, ret1 = 0;
747
748         for (i = 0; i < num_tuples; i++) {
749                 pt = &pdump_t[i];
750                 if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
751                         if (pt->dump_by_type == DEVICE_ID) {
752                                 ret = rte_pdump_enable_by_deviceid(
753                                                 pt->device_id,
754                                                 pt->queue,
755                                                 RTE_PDUMP_FLAG_RX,
756                                                 pt->rx_ring,
757                                                 pt->mp, NULL);
758                                 ret1 = rte_pdump_enable_by_deviceid(
759                                                 pt->device_id,
760                                                 pt->queue,
761                                                 RTE_PDUMP_FLAG_TX,
762                                                 pt->tx_ring,
763                                                 pt->mp, NULL);
764                         } else if (pt->dump_by_type == PORT_ID) {
765                                 ret = rte_pdump_enable(pt->port, pt->queue,
766                                                 RTE_PDUMP_FLAG_RX,
767                                                 pt->rx_ring, pt->mp, NULL);
768                                 ret1 = rte_pdump_enable(pt->port, pt->queue,
769                                                 RTE_PDUMP_FLAG_TX,
770                                                 pt->tx_ring, pt->mp, NULL);
771                         }
772                 } else if (pt->dir == RTE_PDUMP_FLAG_RX) {
773                         if (pt->dump_by_type == DEVICE_ID)
774                                 ret = rte_pdump_enable_by_deviceid(
775                                                 pt->device_id,
776                                                 pt->queue,
777                                                 pt->dir, pt->rx_ring,
778                                                 pt->mp, NULL);
779                         else if (pt->dump_by_type == PORT_ID)
780                                 ret = rte_pdump_enable(pt->port, pt->queue,
781                                                 pt->dir,
782                                                 pt->rx_ring, pt->mp, NULL);
783                 } else if (pt->dir == RTE_PDUMP_FLAG_TX) {
784                         if (pt->dump_by_type == DEVICE_ID)
785                                 ret = rte_pdump_enable_by_deviceid(
786                                                 pt->device_id,
787                                                 pt->queue,
788                                                 pt->dir,
789                                                 pt->tx_ring, pt->mp, NULL);
790                         else if (pt->dump_by_type == PORT_ID)
791                                 ret = rte_pdump_enable(pt->port, pt->queue,
792                                                 pt->dir,
793                                                 pt->tx_ring, pt->mp, NULL);
794                 }
795                 if (ret < 0 || ret1 < 0) {
796                         cleanup_pdump_resources();
797                         rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
798                 }
799         }
800 }
801
802 static inline void
803 dump_packets(void)
804 {
805         int i;
806         struct pdump_tuples *pt;
807
808         while (!quit_signal) {
809                 for (i = 0; i < num_tuples; i++) {
810                         pt = &pdump_t[i];
811                         if (pt->dir & RTE_PDUMP_FLAG_RX)
812                                 pdump_rxtx(pt->rx_ring, pt->rx_vdev_id,
813                                         &pt->stats);
814                         if (pt->dir & RTE_PDUMP_FLAG_TX)
815                                 pdump_rxtx(pt->tx_ring, pt->tx_vdev_id,
816                                         &pt->stats);
817                 }
818         }
819 }
820
821 int
822 main(int argc, char **argv)
823 {
824         int diag;
825         int ret;
826         int i;
827
828         char c_flag[] = "-c1";
829         char n_flag[] = "-n4";
830         char mp_flag[] = "--proc-type=secondary";
831         char *argp[argc + 3];
832
833         /* catch ctrl-c so we can print on exit */
834         signal(SIGINT, signal_handler);
835
836         argp[0] = argv[0];
837         argp[1] = c_flag;
838         argp[2] = n_flag;
839         argp[3] = mp_flag;
840
841         for (i = 1; i < argc; i++)
842                 argp[i + 3] = argv[i];
843
844         argc += 3;
845
846         diag = rte_eal_init(argc, argp);
847         if (diag < 0)
848                 rte_panic("Cannot init EAL\n");
849
850         argc -= diag;
851         argv += (diag - 3);
852
853         /* parse app arguments */
854         if (argc > 1) {
855                 ret = launch_args_parse(argc, argv, argp[0]);
856                 if (ret < 0)
857                         rte_exit(EXIT_FAILURE, "Invalid argument\n");
858         }
859
860         /* create mempool, ring and vdevs info */
861         create_mp_ring_vdev();
862         enable_pdump();
863         dump_packets();
864
865         cleanup_pdump_resources();
866         /* dump debug stats */
867         print_pdump_stats();
868
869         ret = rte_eal_cleanup();
870         if (ret)
871                 printf("Error from rte_eal_cleanup(), %d\n", ret);
872
873         return 0;
874 }