net/fm10k: move PCI device ids to the driver
[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 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"
67
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
71
72 #define MP_NAME "pdump_pool_%d"
73
74 #define RX_RING "rx_ring_%d"
75 #define TX_RING "tx_ring_%d"
76
77 #define RX_STR "rx"
78 #define TX_STR "tx"
79
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
88 #define SIZE 256
89 #define BURST_SIZE 32
90 #define NUM_VDEVS 2
91
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)
95
96 enum pdump_en_dis {
97         DISABLE = 1,
98         ENABLE = 2
99 };
100
101 enum pcap_stream {
102         IFACE = 1,
103         PCAP = 2
104 };
105
106 enum pdump_by {
107         PORT_ID = 1,
108         DEVICE_ID = 2
109 };
110
111 const char *valid_pdump_arguments[] = {
112         PDUMP_PORT_ARG,
113         PDUMP_PCI_ARG,
114         PDUMP_QUEUE_ARG,
115         PDUMP_DIR_ARG,
116         PDUMP_RX_DEV_ARG,
117         PDUMP_TX_DEV_ARG,
118         PDUMP_RING_SIZE_ARG,
119         PDUMP_MSIZE_ARG,
120         PDUMP_NUM_MBUFS_ARG,
121         NULL
122 };
123
124 struct pdump_stats {
125         uint64_t dequeue_pkts;
126         uint64_t tx_pkts;
127         uint64_t freed_pkts;
128 };
129
130 struct pdump_tuples {
131         /* cli params */
132         uint8_t port;
133         char *device_id;
134         uint16_t queue;
135         char rx_dev[TX_STREAM_SIZE];
136         char tx_dev[TX_STREAM_SIZE];
137         uint32_t ring_size;
138         uint16_t mbuf_data_size;
139         uint32_t total_num_mbufs;
140
141         /* params for library API call */
142         uint32_t dir;
143         struct rte_mempool *mp;
144         struct rte_ring *rx_ring;
145         struct rte_ring *tx_ring;
146
147         /* params for packet dumping */
148         enum pdump_by dump_by_type;
149         int rx_vdev_id;
150         int tx_vdev_id;
151         enum pcap_stream rx_vdev_stream_type;
152         enum pcap_stream tx_vdev_stream_type;
153         bool single_pdump_dev;
154
155         /* stats */
156         struct pdump_stats stats;
157 } __rte_cache_aligned;
158 static struct pdump_tuples pdump_t[APP_ARG_TCPDUMP_MAX_TUPLES];
159
160 struct parse_val {
161         uint64_t min;
162         uint64_t max;
163         uint64_t val;
164 };
165
166 int num_tuples;
167 static struct rte_eth_conf port_conf_default;
168 volatile uint8_t quit_signal;
169
170 /**< display usage */
171 static void
172 pdump_usage(const char *prgname)
173 {
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]"
182                         "'\n",
183                         prgname);
184 }
185
186 static int
187 parse_device_id(const char *key __rte_unused, const char *value,
188                 void *extra_args)
189 {
190         struct pdump_tuples *pt = extra_args;
191
192         pt->device_id = strdup(value);
193         pt->dump_by_type = DEVICE_ID;
194
195         return 0;
196 }
197
198 static int
199 parse_queue(const char *key __rte_unused, const char *value, void *extra_args)
200 {
201         unsigned long n;
202         struct pdump_tuples *pt = extra_args;
203
204         if (!strcmp(value, "*"))
205                 pt->queue = RTE_PDUMP_ALL_QUEUES;
206         else {
207                 n = strtoul(value, NULL, 10);
208                 pt->queue = (uint16_t) n;
209         }
210         return 0;
211 }
212
213 static int
214 parse_rxtxdev(const char *key, const char *value, void *extra_args)
215 {
216
217         struct pdump_tuples *pt = extra_args;
218
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;
229         } else {
230                 printf("invalid dev type %s, must be rx or tx\n", value);
231                 return -1;
232         }
233
234         return 0;
235 }
236
237 static int
238 parse_uint_value(const char *key, const char *value, void *extra_args)
239 {
240         struct parse_val *v;
241         unsigned long t;
242         char *end;
243         int ret = 0;
244
245         errno = 0;
246         v = extra_args;
247         t = strtoul(value, &end, 10);
248
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);
253                 ret = -EINVAL;
254         }
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);
258                 ret = -EINVAL;
259         }
260
261         if (ret != 0)
262                 return ret;
263
264         v->val = t;
265         return 0;
266 }
267
268 static int
269 parse_pdump(const char *optarg)
270 {
271         struct rte_kvargs *kvlist;
272         int ret = 0, cnt1, cnt2;
273         struct pdump_tuples *pt;
274         struct parse_val v = {0};
275
276         pt = &pdump_t[num_tuples];
277
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);
282                 return -1;
283         }
284
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);
291                 ret = -1;
292                 goto free_kvlist;
293         } else if (cnt1 == 1) {
294                 v.min = 0;
295                 v.max = RTE_MAX_ETHPORTS-1;
296                 ret = rte_kvargs_process(kvlist, PDUMP_PORT_ARG,
297                                 &parse_uint_value, &v);
298                 if (ret < 0)
299                         goto free_kvlist;
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);
305                 if (ret < 0)
306                         goto free_kvlist;
307         }
308
309         /* queue parsing and validation */
310         cnt1 = rte_kvargs_count(kvlist, PDUMP_QUEUE_ARG);
311         if (cnt1 != 1) {
312                 printf("--pdump=\"%s\": must have queue argument\n", optarg);
313                 ret = -1;
314                 goto free_kvlist;
315         }
316         ret = rte_kvargs_process(kvlist, PDUMP_QUEUE_ARG, &parse_queue, pt);
317         if (ret < 0)
318                 goto free_kvlist;
319
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);
326                 ret = -1;
327                 goto free_kvlist;
328         } else if (cnt1 == 1 && cnt2 == 1) {
329                 ret = rte_kvargs_process(kvlist, PDUMP_RX_DEV_ARG,
330                                         &parse_rxtxdev, pt);
331                 if (ret < 0)
332                         goto free_kvlist;
333                 ret = rte_kvargs_process(kvlist, PDUMP_TX_DEV_ARG,
334                                         &parse_rxtxdev, pt);
335                 if (ret < 0)
336                         goto free_kvlist;
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,
343                                         &parse_rxtxdev, pt);
344                 if (ret < 0)
345                         goto free_kvlist;
346                 pt->dir = RTE_PDUMP_FLAG_RX;
347         } else if (cnt2 == 1) {
348                 ret = rte_kvargs_process(kvlist, PDUMP_TX_DEV_ARG,
349                                         &parse_rxtxdev, pt);
350                 if (ret < 0)
351                         goto free_kvlist;
352                 pt->dir = RTE_PDUMP_FLAG_TX;
353         }
354
355         /* optional */
356         /* ring_size parsing and validation */
357         cnt1 = rte_kvargs_count(kvlist, PDUMP_RING_SIZE_ARG);
358         if (cnt1 == 1) {
359                 v.min = 2;
360                 v.max = RTE_RING_SZ_MASK-1;
361                 ret = rte_kvargs_process(kvlist, PDUMP_RING_SIZE_ARG,
362                                                 &parse_uint_value, &v);
363                 if (ret < 0)
364                         goto free_kvlist;
365                 pt->ring_size = (uint32_t) v.val;
366         } else
367                 pt->ring_size = RING_SIZE;
368
369         /* mbuf_data_size parsing and validation */
370         cnt1 = rte_kvargs_count(kvlist, PDUMP_MSIZE_ARG);
371         if (cnt1 == 1) {
372                 v.min = 1;
373                 v.max = UINT16_MAX;
374                 ret = rte_kvargs_process(kvlist, PDUMP_MSIZE_ARG,
375                                                 &parse_uint_value, &v);
376                 if (ret < 0)
377                         goto free_kvlist;
378                 pt->mbuf_data_size = (uint16_t) v.val;
379         } else
380                 pt->mbuf_data_size = RTE_MBUF_DEFAULT_BUF_SIZE;
381
382         /* total_num_mbufs parsing and validation */
383         cnt1 = rte_kvargs_count(kvlist, PDUMP_NUM_MBUFS_ARG);
384         if (cnt1 == 1) {
385                 v.min = 1025;
386                 v.max = UINT16_MAX;
387                 ret = rte_kvargs_process(kvlist, PDUMP_NUM_MBUFS_ARG,
388                                                 &parse_uint_value, &v);
389                 if (ret < 0)
390                         goto free_kvlist;
391                 pt->total_num_mbufs = (uint16_t) v.val;
392         } else
393                 pt->total_num_mbufs = MBUFS_PER_POOL;
394
395         num_tuples++;
396
397 free_kvlist:
398         rte_kvargs_free(kvlist);
399         return ret;
400 }
401
402 /* Parse the argument given in the command line of the application */
403 static int
404 launch_args_parse(int argc, char **argv, char *prgname)
405 {
406         int opt, ret;
407         int option_index;
408         static struct option long_option[] = {
409                 {"pdump", 1, 0, 0},
410                 {NULL, 0, 0, 0}
411         };
412
413         if (argc == 1)
414                 pdump_usage(prgname);
415
416         /* Parse command line */
417         while ((opt = getopt_long(argc, argv, " ",
418                         long_option, &option_index)) != EOF) {
419                 switch (opt) {
420                 case 0:
421                         if (!strncmp(long_option[option_index].name, "pdump",
422                                         MAX_LONG_OPT_SZ)) {
423                                 ret = parse_pdump(optarg);
424                                 if (ret) {
425                                         pdump_usage(prgname);
426                                         return -1;
427                                 }
428                         }
429                         break;
430                 default:
431                         pdump_usage(prgname);
432                         return -1;
433                 }
434         }
435
436         return 0;
437 }
438
439 static void
440 print_pdump_stats(void)
441 {
442         int i;
443         struct pdump_tuples *pt;
444
445         for (i = 0; i < num_tuples; i++) {
446                 printf("##### PDUMP DEBUG STATS #####\n");
447                 pt = &pdump_t[i];
448                 printf(" -packets dequeued:                     %"PRIu64"\n",
449                                                         pt->stats.dequeue_pkts);
450                 printf(" -packets transmitted to vdev:          %"PRIu64"\n",
451                                                         pt->stats.tx_pkts);
452                 printf(" -packets freed:                        %"PRIu64"\n",
453                                                         pt->stats.freed_pkts);
454         }
455 }
456
457 static inline void
458 disable_pdump(struct pdump_tuples *pt)
459 {
460         if (pt->dump_by_type == DEVICE_ID)
461                 rte_pdump_disable_by_deviceid(pt->device_id, pt->queue,
462                                                 pt->dir);
463         else if (pt->dump_by_type == PORT_ID)
464                 rte_pdump_disable(pt->port, pt->queue, pt->dir);
465 }
466
467 static inline void
468 pdump_rxtx(struct rte_ring *ring, uint8_t vdev_id, struct pdump_stats *stats)
469 {
470         /* write input packets of port to vdev for pdump */
471         struct rte_mbuf *rxtx_bufs[BURST_SIZE];
472
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;
477
478         if (nb_in_deq) {
479                 /* then sent on vdev */
480                 uint16_t nb_in_txd = rte_eth_tx_burst(
481                                 vdev_id,
482                                 0, rxtx_bufs, nb_in_deq);
483                 stats->tx_pkts += nb_in_txd;
484
485                 if (unlikely(nb_in_txd < nb_in_deq)) {
486                         do {
487                                 rte_pktmbuf_free(rxtx_bufs[nb_in_txd]);
488                                 stats->freed_pkts++;
489                         } while (++nb_in_txd < nb_in_deq);
490                 }
491         }
492 }
493
494 static void
495 free_ring_data(struct rte_ring *ring, uint8_t vdev_id,
496                 struct pdump_stats *stats)
497 {
498         while (rte_ring_count(ring))
499                 pdump_rxtx(ring, vdev_id, stats);
500 }
501
502 static void
503 cleanup_pdump_resources(void)
504 {
505         int i;
506         struct pdump_tuples *pt;
507
508         /* disable pdump and free the pdump_tuple resources */
509         for (i = 0; i < num_tuples; i++) {
510                 pt = &pdump_t[i];
511
512                 /* remove callbacks */
513                 disable_pdump(pt);
514
515                 /*
516                 * transmit rest of the enqueued packets of the rings on to
517                 * the vdev, in order to release mbufs to the mepool.
518                 **/
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);
523
524                 if (pt->device_id)
525                         free(pt->device_id);
526
527                 /* free the rings */
528                 if (pt->rx_ring)
529                         rte_ring_free(pt->rx_ring);
530                 if (pt->tx_ring)
531                         rte_ring_free(pt->tx_ring);
532         }
533 }
534
535 static void
536 signal_handler(int sig_num)
537 {
538         if (sig_num == SIGINT) {
539                 printf("\n\nSignal %d received, preparing to exit...\n",
540                                 sig_num);
541                 quit_signal = 1;
542         }
543 }
544
545 static inline int
546 configure_vdev(uint8_t port_id)
547 {
548         struct ether_addr addr;
549         const uint16_t rxRings = 0, txRings = 1;
550         const uint8_t nb_ports = rte_eth_dev_count();
551         int ret;
552         uint16_t q;
553
554         if (port_id > nb_ports)
555                 return -1;
556
557         ret = rte_eth_dev_configure(port_id, rxRings, txRings,
558                                         &port_conf_default);
559         if (ret != 0)
560                 rte_exit(EXIT_FAILURE, "dev config failed\n");
561
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);
565                 if (ret < 0)
566                         rte_exit(EXIT_FAILURE, "queue setup failed\n");
567         }
568
569         ret = rte_eth_dev_start(port_id);
570         if (ret < 0)
571                 rte_exit(EXIT_FAILURE, "dev start failed\n");
572
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",
576                         (unsigned)port_id,
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]);
580
581         rte_eth_promiscuous_enable(port_id);
582
583         return 0;
584 }
585
586 static void
587 create_mp_ring_vdev(void)
588 {
589         int i;
590         uint8_t portid;
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];
596
597         for (i = 0; i < num_tuples; i++) {
598                 pt = &pdump_t[i];
599                 snprintf(mempool_name, SIZE, MP_NAME, i);
600                 mbuf_pool = rte_mempool_lookup(mempool_name);
601                 if (mbuf_pool == NULL) {
602                         /* create mempool */
603                         mbuf_pool = rte_pktmbuf_pool_create(mempool_name,
604                                         pt->total_num_mbufs,
605                                         MBUF_POOL_CACHE_SIZE, 0,
606                                         pt->mbuf_data_size,
607                                         rte_socket_id());
608                         if (mbuf_pool == NULL)
609                                 rte_exit(EXIT_FAILURE,
610                                         "Mempool creation failed: %s\n",
611                                         rte_strerror(rte_errno));
612                 }
613                 pt->mp = mbuf_pool;
614
615                 if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
616                         /* if captured packets has to send to the same vdev */
617                         /* create rx_ring */
618                         snprintf(ring_name, SIZE, RX_RING, i);
619                         pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
620                                         rte_socket_id(), 0);
621                         if (pt->rx_ring == NULL)
622                                 rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
623                                                 rte_strerror(rte_errno),
624                                                 __func__, __LINE__);
625
626                         /* create tx_ring */
627                         snprintf(ring_name, SIZE, TX_RING, i);
628                         pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
629                                         rte_socket_id(), 0);
630                         if (pt->tx_ring == NULL)
631                                 rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
632                                                 rte_strerror(rte_errno),
633                                                 __func__, __LINE__);
634
635                         /* create vdevs */
636                         (pt->rx_vdev_stream_type == IFACE) ?
637                         snprintf(vdev_args, SIZE, VDEV_IFACE, RX_STR, i,
638                         pt->rx_dev) :
639                         snprintf(vdev_args, SIZE, VDEV_PCAP, RX_STR, i,
640                         pt->rx_dev);
641                         if (rte_eth_dev_attach(vdev_args, &portid) < 0)
642                                 rte_exit(EXIT_FAILURE,
643                                         "vdev creation failed:%s:%d\n",
644                                         __func__, __LINE__);
645                         pt->rx_vdev_id = portid;
646
647                         /* configure vdev */
648                         configure_vdev(pt->rx_vdev_id);
649
650                         if (pt->single_pdump_dev)
651                                 pt->tx_vdev_id = portid;
652                         else {
653                                 (pt->tx_vdev_stream_type == IFACE) ?
654                                 snprintf(vdev_args, SIZE, VDEV_IFACE, TX_STR, i,
655                                 pt->tx_dev) :
656                                 snprintf(vdev_args, SIZE, VDEV_PCAP, TX_STR, i,
657                                 pt->tx_dev);
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;
663
664                                 /* configure vdev */
665                                 configure_vdev(pt->tx_vdev_id);
666                         }
667                 } else if (pt->dir == RTE_PDUMP_FLAG_RX) {
668
669                         /* create rx_ring */
670                         snprintf(ring_name, SIZE, RX_RING, i);
671                         pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
672                                         rte_socket_id(), 0);
673                         if (pt->rx_ring == NULL)
674                                 rte_exit(EXIT_FAILURE, "%s\n",
675                                         rte_strerror(rte_errno));
676
677                         (pt->rx_vdev_stream_type == IFACE) ?
678                         snprintf(vdev_args, SIZE, VDEV_IFACE, RX_STR, i,
679                                 pt->rx_dev) :
680                         snprintf(vdev_args, SIZE, VDEV_PCAP, RX_STR, i,
681                                 pt->rx_dev);
682                         if (rte_eth_dev_attach(vdev_args, &portid) < 0)
683                                 rte_exit(EXIT_FAILURE,
684                                         "vdev creation failed:%s:%d\n",
685                                         __func__, __LINE__);
686                         pt->rx_vdev_id = portid;
687                         /* configure vdev */
688                         configure_vdev(pt->rx_vdev_id);
689                 } else if (pt->dir == RTE_PDUMP_FLAG_TX) {
690
691                         /* create tx_ring */
692                         snprintf(ring_name, SIZE, TX_RING, i);
693                         pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
694                                         rte_socket_id(), 0);
695                         if (pt->tx_ring == NULL)
696                                 rte_exit(EXIT_FAILURE, "%s\n",
697                                         rte_strerror(rte_errno));
698
699                         (pt->tx_vdev_stream_type == IFACE) ?
700                         snprintf(vdev_args, SIZE, VDEV_IFACE, TX_STR, i,
701                                 pt->tx_dev) :
702                         snprintf(vdev_args, SIZE, VDEV_PCAP, TX_STR, i,
703                                 pt->tx_dev);
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;
708
709                         /* configure vdev */
710                         configure_vdev(pt->tx_vdev_id);
711                 }
712         }
713 }
714
715 static void
716 enable_pdump(void)
717 {
718         int i;
719         struct pdump_tuples *pt;
720         int ret = 0, ret1 = 0;
721
722         for (i = 0; i < num_tuples; i++) {
723                 pt = &pdump_t[i];
724                 if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
725                         if (pt->dump_by_type == DEVICE_ID) {
726                                 ret = rte_pdump_enable_by_deviceid(
727                                                 pt->device_id,
728                                                 pt->queue,
729                                                 RTE_PDUMP_FLAG_RX,
730                                                 pt->rx_ring,
731                                                 pt->mp, NULL);
732                                 ret = rte_pdump_enable_by_deviceid(
733                                                 pt->device_id,
734                                                 pt->queue,
735                                                 RTE_PDUMP_FLAG_TX,
736                                                 pt->tx_ring,
737                                                 pt->mp, NULL);
738                         } else if (pt->dump_by_type == PORT_ID) {
739                                 ret = rte_pdump_enable(pt->port, pt->queue,
740                                                 RTE_PDUMP_FLAG_RX,
741                                                 pt->rx_ring, pt->mp, NULL);
742                                 ret1 = rte_pdump_enable(pt->port, pt->queue,
743                                                 RTE_PDUMP_FLAG_TX,
744                                                 pt->tx_ring, pt->mp, NULL);
745                         }
746                 } else if (pt->dir == RTE_PDUMP_FLAG_RX) {
747                         if (pt->dump_by_type == DEVICE_ID)
748                                 ret = rte_pdump_enable_by_deviceid(
749                                                 pt->device_id,
750                                                 pt->queue,
751                                                 pt->dir, pt->rx_ring,
752                                                 pt->mp, NULL);
753                         else if (pt->dump_by_type == PORT_ID)
754                                 ret = rte_pdump_enable(pt->port, pt->queue,
755                                                 pt->dir,
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(
760                                                 pt->device_id,
761                                                 pt->queue,
762                                                 pt->dir,
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,
766                                                 pt->dir,
767                                                 pt->tx_ring, pt->mp, NULL);
768                 }
769                 if (ret < 0 || ret1 < 0) {
770                         cleanup_pdump_resources();
771                         rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
772                 }
773         }
774 }
775
776 static inline void
777 dump_packets(void)
778 {
779         int i;
780         struct pdump_tuples *pt;
781
782         while (!quit_signal) {
783                 for (i = 0; i < num_tuples; i++) {
784                         pt = &pdump_t[i];
785                         if (pt->dir & RTE_PDUMP_FLAG_RX)
786                                 pdump_rxtx(pt->rx_ring, pt->rx_vdev_id,
787                                         &pt->stats);
788                         if (pt->dir & RTE_PDUMP_FLAG_TX)
789                                 pdump_rxtx(pt->tx_ring, pt->tx_vdev_id,
790                                         &pt->stats);
791                 }
792         }
793 }
794
795 int
796 main(int argc, char **argv)
797 {
798         int diag;
799         int ret;
800         int i;
801
802         char c_flag[] = "-c1";
803         char n_flag[] = "-n4";
804         char mp_flag[] = "--proc-type=secondary";
805         char *argp[argc + 3];
806
807         /* catch ctrl-c so we can print on exit */
808         signal(SIGINT, signal_handler);
809
810         argp[0] = argv[0];
811         argp[1] = c_flag;
812         argp[2] = n_flag;
813         argp[3] = mp_flag;
814
815         for (i = 1; i < argc; i++)
816                 argp[i + 3] = argv[i];
817
818         argc += 3;
819
820         diag = rte_eal_init(argc, argp);
821         if (diag < 0)
822                 rte_panic("Cannot init EAL\n");
823
824         argc -= diag;
825         argv += (diag - 3);
826
827         /* parse app arguments */
828         if (argc > 1) {
829                 ret = launch_args_parse(argc, argv, argp[0]);
830                 if (ret < 0)
831                         rte_exit(EXIT_FAILURE, "Invalid argument\n");
832         }
833
834         /* create mempool, ring and vdevs info */
835         create_mp_ring_vdev();
836         enable_pdump();
837         dump_packets();
838
839         cleanup_pdump_resources();
840         /* dump debug stats */
841         print_pdump_stats();
842
843         return 0;
844 }