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