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 "net_pcap_%s_%d,tx_pcap=%s"
72#define VDEV_IFACE "net_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
99enum pdump_en_dis {
100	DISABLE = 1,
101	ENABLE = 2
102};
103
104enum pcap_stream {
105	IFACE = 1,
106	PCAP = 2
107};
108
109enum pdump_by {
110	PORT_ID = 1,
111	DEVICE_ID = 2
112};
113
114const 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
127struct pdump_stats {
128	uint64_t dequeue_pkts;
129	uint64_t tx_pkts;
130	uint64_t freed_pkts;
131};
132
133struct 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;
161static struct pdump_tuples pdump_t[APP_ARG_TCPDUMP_MAX_TUPLES];
162
163struct parse_val {
164	uint64_t min;
165	uint64_t max;
166	uint64_t val;
167};
168
169int num_tuples;
170static struct rte_eth_conf port_conf_default;
171volatile uint8_t quit_signal;
172static char server_socket_path[PATH_MAX];
173static char client_socket_path[PATH_MAX];
174
175/**< display usage */
176static void
177pdump_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
194static int
195parse_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
206static int
207parse_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
221static int
222parse_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
242static int
243parse_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
273static int
274parse_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
402free_kvlist:
403	rte_kvargs_free(kvlist);
404	return ret;
405}
406
407/* Parse the argument given in the command line of the application */
408static int
409launch_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
464static void
465print_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
482static inline void
483disable_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
492static inline void
493pdump_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
519static void
520free_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
527static void
528cleanup_rings(void)
529{
530	int i;
531	struct pdump_tuples *pt;
532
533	for (i = 0; i < num_tuples; i++) {
534		pt = &pdump_t[i];
535
536		if (pt->device_id)
537			free(pt->device_id);
538
539		/* free the rings */
540		if (pt->rx_ring)
541			rte_ring_free(pt->rx_ring);
542		if (pt->tx_ring)
543			rte_ring_free(pt->tx_ring);
544	}
545}
546
547static void
548cleanup_pdump_resources(void)
549{
550	int i;
551	struct pdump_tuples *pt;
552
553	/* disable pdump and free the pdump_tuple resources */
554	for (i = 0; i < num_tuples; i++) {
555		pt = &pdump_t[i];
556
557		/* remove callbacks */
558		disable_pdump(pt);
559
560		/*
561		* transmit rest of the enqueued packets of the rings on to
562		* the vdev, in order to release mbufs to the mepool.
563		**/
564		if (pt->dir & RTE_PDUMP_FLAG_RX)
565			free_ring_data(pt->rx_ring, pt->rx_vdev_id, &pt->stats);
566		if (pt->dir & RTE_PDUMP_FLAG_TX)
567			free_ring_data(pt->tx_ring, pt->tx_vdev_id, &pt->stats);
568	}
569	cleanup_rings();
570}
571
572static void
573signal_handler(int sig_num)
574{
575	if (sig_num == SIGINT) {
576		printf("\n\nSignal %d received, preparing to exit...\n",
577				sig_num);
578		quit_signal = 1;
579	}
580}
581
582static inline int
583configure_vdev(uint8_t port_id)
584{
585	struct ether_addr addr;
586	const uint16_t rxRings = 0, txRings = 1;
587	const uint8_t nb_ports = rte_eth_dev_count();
588	int ret;
589	uint16_t q;
590
591	if (port_id > nb_ports)
592		return -1;
593
594	ret = rte_eth_dev_configure(port_id, rxRings, txRings,
595					&port_conf_default);
596	if (ret != 0)
597		rte_exit(EXIT_FAILURE, "dev config failed\n");
598
599	 for (q = 0; q < txRings; q++) {
600		ret = rte_eth_tx_queue_setup(port_id, q, TX_DESC_PER_QUEUE,
601				rte_eth_dev_socket_id(port_id), NULL);
602		if (ret < 0)
603			rte_exit(EXIT_FAILURE, "queue setup failed\n");
604	}
605
606	ret = rte_eth_dev_start(port_id);
607	if (ret < 0)
608		rte_exit(EXIT_FAILURE, "dev start failed\n");
609
610	rte_eth_macaddr_get(port_id, &addr);
611	printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
612			" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
613			(unsigned)port_id,
614			addr.addr_bytes[0], addr.addr_bytes[1],
615			addr.addr_bytes[2], addr.addr_bytes[3],
616			addr.addr_bytes[4], addr.addr_bytes[5]);
617
618	rte_eth_promiscuous_enable(port_id);
619
620	return 0;
621}
622
623static void
624create_mp_ring_vdev(void)
625{
626	int i;
627	uint8_t portid;
628	struct pdump_tuples *pt = NULL;
629	struct rte_mempool *mbuf_pool = NULL;
630	char vdev_args[SIZE];
631	char ring_name[SIZE];
632	char mempool_name[SIZE];
633
634	for (i = 0; i < num_tuples; i++) {
635		pt = &pdump_t[i];
636		snprintf(mempool_name, SIZE, MP_NAME, i);
637		mbuf_pool = rte_mempool_lookup(mempool_name);
638		if (mbuf_pool == NULL) {
639			/* create mempool */
640			mbuf_pool = rte_pktmbuf_pool_create(mempool_name,
641					pt->total_num_mbufs,
642					MBUF_POOL_CACHE_SIZE, 0,
643					pt->mbuf_data_size,
644					rte_socket_id());
645			if (mbuf_pool == NULL) {
646				cleanup_rings();
647				rte_exit(EXIT_FAILURE,
648					"Mempool creation failed: %s\n",
649					rte_strerror(rte_errno));
650			}
651		}
652		pt->mp = mbuf_pool;
653
654		if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
655			/* if captured packets has to send to the same vdev */
656			/* create rx_ring */
657			snprintf(ring_name, SIZE, RX_RING, i);
658			pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
659					rte_socket_id(), 0);
660			if (pt->rx_ring == NULL) {
661				cleanup_rings();
662				rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
663						rte_strerror(rte_errno),
664						__func__, __LINE__);
665			}
666
667			/* create tx_ring */
668			snprintf(ring_name, SIZE, TX_RING, i);
669			pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
670					rte_socket_id(), 0);
671			if (pt->tx_ring == NULL) {
672				cleanup_rings();
673				rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
674						rte_strerror(rte_errno),
675						__func__, __LINE__);
676			}
677
678			/* create vdevs */
679			(pt->rx_vdev_stream_type == IFACE) ?
680			snprintf(vdev_args, SIZE, VDEV_IFACE, RX_STR, i,
681			pt->rx_dev) :
682			snprintf(vdev_args, SIZE, VDEV_PCAP, RX_STR, i,
683			pt->rx_dev);
684			if (rte_eth_dev_attach(vdev_args, &portid) < 0) {
685				cleanup_rings();
686				rte_exit(EXIT_FAILURE,
687					"vdev creation failed:%s:%d\n",
688					__func__, __LINE__);
689			}
690			pt->rx_vdev_id = portid;
691
692			/* configure vdev */
693			configure_vdev(pt->rx_vdev_id);
694
695			if (pt->single_pdump_dev)
696				pt->tx_vdev_id = portid;
697			else {
698				(pt->tx_vdev_stream_type == IFACE) ?
699				snprintf(vdev_args, SIZE, VDEV_IFACE, TX_STR, i,
700				pt->tx_dev) :
701				snprintf(vdev_args, SIZE, VDEV_PCAP, TX_STR, i,
702				pt->tx_dev);
703				if (rte_eth_dev_attach(vdev_args,
704							&portid) < 0) {
705					cleanup_rings();
706					rte_exit(EXIT_FAILURE,
707						"vdev creation failed:"
708						"%s:%d\n", __func__, __LINE__);
709				}
710				pt->tx_vdev_id = portid;
711
712				/* configure vdev */
713				configure_vdev(pt->tx_vdev_id);
714			}
715		} else if (pt->dir == RTE_PDUMP_FLAG_RX) {
716
717			/* create rx_ring */
718			snprintf(ring_name, SIZE, RX_RING, i);
719			pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
720					rte_socket_id(), 0);
721			if (pt->rx_ring == NULL) {
722				cleanup_rings();
723				rte_exit(EXIT_FAILURE, "%s\n",
724					rte_strerror(rte_errno));
725			}
726
727			(pt->rx_vdev_stream_type == IFACE) ?
728			snprintf(vdev_args, SIZE, VDEV_IFACE, RX_STR, i,
729				pt->rx_dev) :
730			snprintf(vdev_args, SIZE, VDEV_PCAP, RX_STR, i,
731				pt->rx_dev);
732			if (rte_eth_dev_attach(vdev_args, &portid) < 0) {
733				cleanup_rings();
734				rte_exit(EXIT_FAILURE,
735					"vdev creation failed:%s:%d\n",
736					__func__, __LINE__);
737			}
738			pt->rx_vdev_id = portid;
739			/* configure vdev */
740			configure_vdev(pt->rx_vdev_id);
741		} else if (pt->dir == RTE_PDUMP_FLAG_TX) {
742
743			/* create tx_ring */
744			snprintf(ring_name, SIZE, TX_RING, i);
745			pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
746					rte_socket_id(), 0);
747			if (pt->tx_ring == NULL) {
748				cleanup_rings();
749				rte_exit(EXIT_FAILURE, "%s\n",
750					rte_strerror(rte_errno));
751			}
752
753			(pt->tx_vdev_stream_type == IFACE) ?
754			snprintf(vdev_args, SIZE, VDEV_IFACE, TX_STR, i,
755				pt->tx_dev) :
756			snprintf(vdev_args, SIZE, VDEV_PCAP, TX_STR, i,
757				pt->tx_dev);
758			if (rte_eth_dev_attach(vdev_args, &portid) < 0) {
759				cleanup_rings();
760				rte_exit(EXIT_FAILURE,
761					"vdev creation failed\n");
762			}
763			pt->tx_vdev_id = portid;
764
765			/* configure vdev */
766			configure_vdev(pt->tx_vdev_id);
767		}
768	}
769}
770
771static void
772enable_pdump(void)
773{
774	int i;
775	struct pdump_tuples *pt;
776	int ret = 0, ret1 = 0;
777
778	if (server_socket_path[0] != 0)
779		ret = rte_pdump_set_socket_dir(server_socket_path,
780				RTE_PDUMP_SOCKET_SERVER);
781	if (ret == 0 && client_socket_path[0] != 0) {
782		ret = rte_pdump_set_socket_dir(client_socket_path,
783				RTE_PDUMP_SOCKET_CLIENT);
784	}
785	if (ret < 0) {
786		cleanup_pdump_resources();
787		rte_exit(EXIT_FAILURE,
788				"failed to set socket paths of server:%s, "
789				"client:%s\n",
790				server_socket_path,
791				client_socket_path);
792	}
793
794	for (i = 0; i < num_tuples; i++) {
795		pt = &pdump_t[i];
796		if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
797			if (pt->dump_by_type == DEVICE_ID) {
798				ret = rte_pdump_enable_by_deviceid(
799						pt->device_id,
800						pt->queue,
801						RTE_PDUMP_FLAG_RX,
802						pt->rx_ring,
803						pt->mp, NULL);
804				ret1 = rte_pdump_enable_by_deviceid(
805						pt->device_id,
806						pt->queue,
807						RTE_PDUMP_FLAG_TX,
808						pt->tx_ring,
809						pt->mp, NULL);
810			} else if (pt->dump_by_type == PORT_ID) {
811				ret = rte_pdump_enable(pt->port, pt->queue,
812						RTE_PDUMP_FLAG_RX,
813						pt->rx_ring, pt->mp, NULL);
814				ret1 = rte_pdump_enable(pt->port, pt->queue,
815						RTE_PDUMP_FLAG_TX,
816						pt->tx_ring, pt->mp, NULL);
817			}
818		} else if (pt->dir == RTE_PDUMP_FLAG_RX) {
819			if (pt->dump_by_type == DEVICE_ID)
820				ret = rte_pdump_enable_by_deviceid(
821						pt->device_id,
822						pt->queue,
823						pt->dir, pt->rx_ring,
824						pt->mp, NULL);
825			else if (pt->dump_by_type == PORT_ID)
826				ret = rte_pdump_enable(pt->port, pt->queue,
827						pt->dir,
828						pt->rx_ring, pt->mp, NULL);
829		} else if (pt->dir == RTE_PDUMP_FLAG_TX) {
830			if (pt->dump_by_type == DEVICE_ID)
831				ret = rte_pdump_enable_by_deviceid(
832						pt->device_id,
833						pt->queue,
834						pt->dir,
835						pt->tx_ring, pt->mp, NULL);
836			else if (pt->dump_by_type == PORT_ID)
837				ret = rte_pdump_enable(pt->port, pt->queue,
838						pt->dir,
839						pt->tx_ring, pt->mp, NULL);
840		}
841		if (ret < 0 || ret1 < 0) {
842			cleanup_pdump_resources();
843			rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
844		}
845	}
846}
847
848static inline void
849dump_packets(void)
850{
851	int i;
852	struct pdump_tuples *pt;
853
854	while (!quit_signal) {
855		for (i = 0; i < num_tuples; i++) {
856			pt = &pdump_t[i];
857			if (pt->dir & RTE_PDUMP_FLAG_RX)
858				pdump_rxtx(pt->rx_ring, pt->rx_vdev_id,
859					&pt->stats);
860			if (pt->dir & RTE_PDUMP_FLAG_TX)
861				pdump_rxtx(pt->tx_ring, pt->tx_vdev_id,
862					&pt->stats);
863		}
864	}
865}
866
867int
868main(int argc, char **argv)
869{
870	int diag;
871	int ret;
872	int i;
873
874	char c_flag[] = "-c1";
875	char n_flag[] = "-n4";
876	char mp_flag[] = "--proc-type=secondary";
877	char *argp[argc + 3];
878
879	/* catch ctrl-c so we can print on exit */
880	signal(SIGINT, signal_handler);
881
882	argp[0] = argv[0];
883	argp[1] = c_flag;
884	argp[2] = n_flag;
885	argp[3] = mp_flag;
886
887	for (i = 1; i < argc; i++)
888		argp[i + 3] = argv[i];
889
890	argc += 3;
891
892	diag = rte_eal_init(argc, argp);
893	if (diag < 0)
894		rte_panic("Cannot init EAL\n");
895
896	argc -= diag;
897	argv += (diag - 3);
898
899	/* parse app arguments */
900	if (argc > 1) {
901		ret = launch_args_parse(argc, argv, argp[0]);
902		if (ret < 0)
903			rte_exit(EXIT_FAILURE, "Invalid argument\n");
904	}
905
906	/* create mempool, ring and vdevs info */
907	create_mp_ring_vdev();
908	enable_pdump();
909	dump_packets();
910
911	cleanup_pdump_resources();
912	/* dump debug stats */
913	print_pdump_stats();
914
915	return 0;
916}
917