Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016 Intel Corporation
3 : : */
4 : :
5 : : #include <stdio.h>
6 : : #include <string.h>
7 : : #include <stdint.h>
8 : : #include <inttypes.h>
9 : : #include <stdlib.h>
10 : : #include <getopt.h>
11 : : #include <signal.h>
12 : : #include <stdbool.h>
13 : : #include <net/if.h>
14 : :
15 : : #include <rte_eal.h>
16 : : #include <rte_alarm.h>
17 : : #include <rte_common.h>
18 : : #include <rte_debug.h>
19 : : #include <rte_ethdev.h>
20 : : #include <rte_memory.h>
21 : : #include <rte_lcore.h>
22 : : #include <rte_branch_prediction.h>
23 : : #include <rte_errno.h>
24 : : #include <rte_dev.h>
25 : : #include <rte_kvargs.h>
26 : : #include <rte_mempool.h>
27 : : #include <rte_ring.h>
28 : : #include <rte_string_fns.h>
29 : : #include <rte_pdump.h>
30 : :
31 : : #define CMD_LINE_OPT_PDUMP "pdump"
32 : : #define CMD_LINE_OPT_PDUMP_NUM 256
33 : : #define CMD_LINE_OPT_MULTI "multi"
34 : : #define CMD_LINE_OPT_MULTI_NUM 257
35 : : #define PDUMP_PORT_ARG "port"
36 : : #define PDUMP_PCI_ARG "device_id"
37 : : #define PDUMP_QUEUE_ARG "queue"
38 : : #define PDUMP_DIR_ARG "dir"
39 : : #define PDUMP_RX_DEV_ARG "rx-dev"
40 : : #define PDUMP_TX_DEV_ARG "tx-dev"
41 : : #define PDUMP_RING_SIZE_ARG "ring-size"
42 : : #define PDUMP_MSIZE_ARG "mbuf-size"
43 : : #define PDUMP_NUM_MBUFS_ARG "total-num-mbufs"
44 : :
45 : : #define VDEV_NAME_FMT "net_pcap_%s_%d"
46 : : #define VDEV_PCAP_ARGS_FMT "tx_pcap=%s"
47 : : #define VDEV_IFACE_ARGS_FMT "tx_iface=%s"
48 : : #define TX_STREAM_SIZE 64
49 : :
50 : : #define MP_NAME "pdump_pool_%d"
51 : :
52 : : #define RX_RING "rx_ring_%d"
53 : : #define TX_RING "tx_ring_%d"
54 : :
55 : : #define RX_STR "rx"
56 : : #define TX_STR "tx"
57 : :
58 : : /* Maximum long option length for option parsing. */
59 : : #define APP_ARG_TCPDUMP_MAX_TUPLES 54
60 : : #define MBUF_POOL_CACHE_SIZE 250
61 : : #define TX_DESC_PER_QUEUE 512
62 : : #define RX_DESC_PER_QUEUE 128
63 : : #define MBUFS_PER_POOL 65535
64 : : #define MAX_LONG_OPT_SZ 64
65 : : #define RING_SIZE 16384
66 : : #define SIZE 256
67 : : #define BURST_SIZE 32
68 : : #define NUM_VDEVS 2
69 : : /* Maximum delay for exiting after primary process. */
70 : : #define MONITOR_INTERVAL (500 * 1000)
71 : :
72 : : /* true if x is a power of 2 */
73 : : #define POWEROF2(x) ((((x)-1) & (x)) == 0)
74 : :
75 : : enum pdump_en_dis {
76 : : DISABLE = 1,
77 : : ENABLE = 2
78 : : };
79 : :
80 : : enum pcap_stream {
81 : : IFACE = 1,
82 : : PCAP = 2
83 : : };
84 : :
85 : : enum pdump_by {
86 : : PORT_ID = 1,
87 : : DEVICE_ID = 2
88 : : };
89 : :
90 : : static const char * const valid_pdump_arguments[] = {
91 : : PDUMP_PORT_ARG,
92 : : PDUMP_PCI_ARG,
93 : : PDUMP_QUEUE_ARG,
94 : : PDUMP_DIR_ARG,
95 : : PDUMP_RX_DEV_ARG,
96 : : PDUMP_TX_DEV_ARG,
97 : : PDUMP_RING_SIZE_ARG,
98 : : PDUMP_MSIZE_ARG,
99 : : PDUMP_NUM_MBUFS_ARG,
100 : : NULL
101 : : };
102 : :
103 : : struct pdump_stats {
104 : : uint64_t dequeue_pkts;
105 : : uint64_t tx_pkts;
106 : : uint64_t freed_pkts;
107 : : };
108 : :
109 : : struct __rte_cache_aligned pdump_tuples {
110 : : /* cli params */
111 : : uint16_t port;
112 : : char *device_id;
113 : : uint16_t queue;
114 : : char rx_dev[TX_STREAM_SIZE];
115 : : char tx_dev[TX_STREAM_SIZE];
116 : : uint32_t ring_size;
117 : : uint16_t mbuf_data_size;
118 : : uint32_t total_num_mbufs;
119 : :
120 : : /* params for library API call */
121 : : uint32_t dir;
122 : : struct rte_mempool *mp;
123 : : struct rte_ring *rx_ring;
124 : : struct rte_ring *tx_ring;
125 : :
126 : : /* params for packet dumping */
127 : : enum pdump_by dump_by_type;
128 : : uint16_t rx_vdev_id;
129 : : uint16_t tx_vdev_id;
130 : : enum pcap_stream rx_vdev_stream_type;
131 : : enum pcap_stream tx_vdev_stream_type;
132 : : bool single_pdump_dev;
133 : :
134 : : /* stats */
135 : : struct pdump_stats stats;
136 : : };
137 : : static struct pdump_tuples pdump_t[APP_ARG_TCPDUMP_MAX_TUPLES];
138 : :
139 : : struct parse_val {
140 : : uint64_t min;
141 : : uint64_t max;
142 : : uint64_t val;
143 : : };
144 : :
145 : : static int num_tuples;
146 : : static struct rte_eth_conf port_conf_default;
147 : : static volatile uint8_t quit_signal;
148 : : static uint8_t multiple_core_capture;
149 : :
150 : : /**< display usage */
151 : : static void
152 : : pdump_usage(const char *prgname)
153 : : {
154 : : printf("usage: %s [EAL options] --"
155 : : " --["CMD_LINE_OPT_MULTI"]\n"
156 : : " --"CMD_LINE_OPT_PDUMP" "
157 : : "'(port=<port id> | device_id=<pci id or vdev name>),"
158 : : "(queue=<queue_id>),"
159 : : "(rx-dev=<iface or pcap file> |"
160 : : " tx-dev=<iface or pcap file>,"
161 : : "[ring-size=<ring size>default:16384],"
162 : : "[mbuf-size=<mbuf data size>default:2176],"
163 : : "[total-num-mbufs=<number of mbufs>default:65535]'\n",
164 : : prgname);
165 : 0 : }
166 : :
167 : : static int
168 : 0 : parse_device_id(const char *key __rte_unused, const char *value,
169 : : void *extra_args)
170 : : {
171 : : struct pdump_tuples *pt = extra_args;
172 : :
173 : 0 : pt->device_id = strdup(value);
174 : 0 : if (pt->device_id == NULL)
175 : : return -1;
176 : :
177 : 0 : pt->dump_by_type = DEVICE_ID;
178 : :
179 : 0 : return 0;
180 : : }
181 : :
182 : : static int
183 : 0 : parse_queue(const char *key __rte_unused, const char *value, void *extra_args)
184 : : {
185 : : unsigned long n;
186 : : struct pdump_tuples *pt = extra_args;
187 : :
188 : 0 : if (!strcmp(value, "*"))
189 : 0 : pt->queue = RTE_PDUMP_ALL_QUEUES;
190 : : else {
191 : 0 : n = strtoul(value, NULL, 10);
192 : 0 : pt->queue = (uint16_t) n;
193 : : }
194 : 0 : return 0;
195 : : }
196 : :
197 : : static int
198 : 0 : parse_rxtxdev(const char *key, const char *value, void *extra_args)
199 : : {
200 : :
201 : : struct pdump_tuples *pt = extra_args;
202 : :
203 : 0 : if (!strcmp(key, PDUMP_RX_DEV_ARG)) {
204 : 0 : strlcpy(pt->rx_dev, value, sizeof(pt->rx_dev));
205 : : /* identify the tx stream type for pcap vdev */
206 : 0 : if (if_nametoindex(pt->rx_dev))
207 : 0 : pt->rx_vdev_stream_type = IFACE;
208 : 0 : } else if (!strcmp(key, PDUMP_TX_DEV_ARG)) {
209 : 0 : strlcpy(pt->tx_dev, value, sizeof(pt->tx_dev));
210 : : /* identify the tx stream type for pcap vdev */
211 : 0 : if (if_nametoindex(pt->tx_dev))
212 : 0 : pt->tx_vdev_stream_type = IFACE;
213 : : }
214 : :
215 : 0 : return 0;
216 : : }
217 : :
218 : : static int
219 : 0 : parse_uint_value(const char *key, const char *value, void *extra_args)
220 : : {
221 : : struct parse_val *v;
222 : : unsigned long t;
223 : : char *end;
224 : : int ret = 0;
225 : :
226 : 0 : errno = 0;
227 : : v = extra_args;
228 : 0 : t = strtoul(value, &end, 10);
229 : :
230 : 0 : if (errno != 0 || end[0] != 0 || t < v->min || t > v->max) {
231 : 0 : printf("invalid value:\"%s\" for key:\"%s\", "
232 : : "value must be >= %"PRIu64" and <= %"PRIu64"\n",
233 : : value, key, v->min, v->max);
234 : : ret = -EINVAL;
235 : : }
236 : 0 : if (!strcmp(key, PDUMP_RING_SIZE_ARG) && !POWEROF2(t)) {
237 : : printf("invalid value:\"%s\" for key:\"%s\", "
238 : : "value must be power of 2\n", value, key);
239 : : ret = -EINVAL;
240 : : }
241 : :
242 : 0 : if (ret != 0)
243 : 0 : return ret;
244 : :
245 : 0 : v->val = t;
246 : 0 : return 0;
247 : : }
248 : :
249 : : static int
250 : 0 : parse_pdump(const char *optarg)
251 : : {
252 : : struct rte_kvargs *kvlist;
253 : : int ret = 0, cnt1, cnt2;
254 : : struct pdump_tuples *pt;
255 : 0 : struct parse_val v = {0};
256 : :
257 : 0 : pt = &pdump_t[num_tuples];
258 : :
259 : : /* initial check for invalid arguments */
260 : 0 : kvlist = rte_kvargs_parse(optarg, valid_pdump_arguments);
261 : 0 : if (kvlist == NULL) {
262 : : printf("--pdump=\"%s\": invalid argument passed\n", optarg);
263 : 0 : return -1;
264 : : }
265 : :
266 : : /* port/device_id parsing and validation */
267 : 0 : cnt1 = rte_kvargs_count(kvlist, PDUMP_PORT_ARG);
268 : 0 : cnt2 = rte_kvargs_count(kvlist, PDUMP_PCI_ARG);
269 : 0 : if (!((cnt1 == 1 && cnt2 == 0) || (cnt1 == 0 && cnt2 == 1))) {
270 : : printf("--pdump=\"%s\": must have either port or "
271 : : "device_id argument\n", optarg);
272 : : ret = -1;
273 : 0 : goto free_kvlist;
274 : 0 : } else if (cnt1 == 1) {
275 : 0 : v.min = 0;
276 : 0 : v.max = RTE_MAX_ETHPORTS-1;
277 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_PORT_ARG,
278 : : &parse_uint_value, &v);
279 : 0 : if (ret < 0)
280 : 0 : goto free_kvlist;
281 : 0 : pt->port = (uint16_t) v.val;
282 : 0 : pt->dump_by_type = PORT_ID;
283 : 0 : } else if (cnt2 == 1) {
284 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_PCI_ARG,
285 : : &parse_device_id, pt);
286 : 0 : if (ret < 0)
287 : 0 : goto free_kvlist;
288 : : }
289 : :
290 : : /* queue parsing and validation */
291 : 0 : cnt1 = rte_kvargs_count(kvlist, PDUMP_QUEUE_ARG);
292 : 0 : if (cnt1 != 1) {
293 : : printf("--pdump=\"%s\": must have queue argument\n", optarg);
294 : : ret = -1;
295 : 0 : goto free_kvlist;
296 : : }
297 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_QUEUE_ARG, &parse_queue, pt);
298 : 0 : if (ret < 0)
299 : 0 : goto free_kvlist;
300 : :
301 : : /* rx-dev and tx-dev parsing and validation */
302 : 0 : cnt1 = rte_kvargs_count(kvlist, PDUMP_RX_DEV_ARG);
303 : 0 : cnt2 = rte_kvargs_count(kvlist, PDUMP_TX_DEV_ARG);
304 : 0 : if (cnt1 == 0 && cnt2 == 0) {
305 : : printf("--pdump=\"%s\": must have either rx-dev or "
306 : : "tx-dev argument\n", optarg);
307 : : ret = -1;
308 : 0 : goto free_kvlist;
309 : 0 : } else if (cnt1 == 1 && cnt2 == 1) {
310 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_RX_DEV_ARG,
311 : : &parse_rxtxdev, pt);
312 : 0 : if (ret < 0)
313 : 0 : goto free_kvlist;
314 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_TX_DEV_ARG,
315 : : &parse_rxtxdev, pt);
316 : 0 : if (ret < 0)
317 : 0 : goto free_kvlist;
318 : : /* if captured packets has to send to the same vdev */
319 : 0 : if (!strcmp(pt->rx_dev, pt->tx_dev))
320 : 0 : pt->single_pdump_dev = true;
321 : 0 : pt->dir = RTE_PDUMP_FLAG_RXTX;
322 : 0 : } else if (cnt1 == 1) {
323 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_RX_DEV_ARG,
324 : : &parse_rxtxdev, pt);
325 : 0 : if (ret < 0)
326 : 0 : goto free_kvlist;
327 : 0 : pt->dir = RTE_PDUMP_FLAG_RX;
328 : 0 : } else if (cnt2 == 1) {
329 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_TX_DEV_ARG,
330 : : &parse_rxtxdev, pt);
331 : 0 : if (ret < 0)
332 : 0 : goto free_kvlist;
333 : 0 : pt->dir = RTE_PDUMP_FLAG_TX;
334 : : }
335 : :
336 : : /* optional */
337 : : /* ring_size parsing and validation */
338 : 0 : cnt1 = rte_kvargs_count(kvlist, PDUMP_RING_SIZE_ARG);
339 : 0 : if (cnt1 == 1) {
340 : 0 : v.min = 2;
341 : 0 : v.max = RTE_RING_SZ_MASK-1;
342 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_RING_SIZE_ARG,
343 : : &parse_uint_value, &v);
344 : 0 : if (ret < 0)
345 : 0 : goto free_kvlist;
346 : 0 : pt->ring_size = (uint32_t) v.val;
347 : : } else
348 : 0 : pt->ring_size = RING_SIZE;
349 : :
350 : : /* mbuf_data_size parsing and validation */
351 : 0 : cnt1 = rte_kvargs_count(kvlist, PDUMP_MSIZE_ARG);
352 : 0 : if (cnt1 == 1) {
353 : 0 : v.min = 1;
354 : 0 : v.max = UINT16_MAX;
355 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_MSIZE_ARG,
356 : : &parse_uint_value, &v);
357 : 0 : if (ret < 0)
358 : 0 : goto free_kvlist;
359 : 0 : pt->mbuf_data_size = (uint16_t) v.val;
360 : : } else
361 : 0 : pt->mbuf_data_size = RTE_MBUF_DEFAULT_BUF_SIZE;
362 : :
363 : : /* total_num_mbufs parsing and validation */
364 : 0 : cnt1 = rte_kvargs_count(kvlist, PDUMP_NUM_MBUFS_ARG);
365 : 0 : if (cnt1 == 1) {
366 : 0 : v.min = 1025;
367 : 0 : v.max = UINT16_MAX;
368 : 0 : ret = rte_kvargs_process(kvlist, PDUMP_NUM_MBUFS_ARG,
369 : : &parse_uint_value, &v);
370 : 0 : if (ret < 0)
371 : 0 : goto free_kvlist;
372 : 0 : pt->total_num_mbufs = (uint16_t) v.val;
373 : : } else
374 : 0 : pt->total_num_mbufs = MBUFS_PER_POOL;
375 : :
376 : 0 : num_tuples++;
377 : :
378 : 0 : free_kvlist:
379 : 0 : rte_kvargs_free(kvlist);
380 : 0 : return ret;
381 : : }
382 : :
383 : : /* Parse the argument given in the command line of the application */
384 : : static int
385 : 0 : launch_args_parse(int argc, char **argv, char *prgname)
386 : : {
387 : : int opt, ret;
388 : : int option_index;
389 : : static struct option long_option[] = {
390 : : {CMD_LINE_OPT_PDUMP, 1, 0, CMD_LINE_OPT_PDUMP_NUM},
391 : : {CMD_LINE_OPT_MULTI, 0, 0, CMD_LINE_OPT_MULTI_NUM},
392 : : {NULL, 0, 0, 0}
393 : : };
394 : :
395 : 0 : if (argc == 1)
396 : : pdump_usage(prgname);
397 : :
398 : : /* Parse command line */
399 : 0 : while ((opt = getopt_long(argc, argv, " ",
400 : 0 : long_option, &option_index)) != EOF) {
401 : 0 : switch (opt) {
402 : 0 : case CMD_LINE_OPT_PDUMP_NUM:
403 : 0 : ret = parse_pdump(optarg);
404 : 0 : if (ret) {
405 : : pdump_usage(prgname);
406 : 0 : return -1;
407 : : }
408 : : break;
409 : 0 : case CMD_LINE_OPT_MULTI_NUM:
410 : 0 : multiple_core_capture = 1;
411 : 0 : break;
412 : : default:
413 : : pdump_usage(prgname);
414 : 0 : return -1;
415 : : }
416 : : }
417 : :
418 : : return 0;
419 : : }
420 : :
421 : : static void
422 : 0 : monitor_primary(void *arg __rte_unused)
423 : : {
424 : 0 : if (quit_signal)
425 : : return;
426 : :
427 : 0 : if (rte_eal_primary_proc_alive(NULL)) {
428 : 0 : rte_eal_alarm_set(MONITOR_INTERVAL, monitor_primary, NULL);
429 : 0 : return;
430 : : }
431 : :
432 : : printf("Primary process is no longer active, exiting...\n");
433 : 0 : quit_signal = 1;
434 : : }
435 : :
436 : : static void
437 : 0 : print_pdump_stats(void)
438 : : {
439 : : int i;
440 : : struct pdump_tuples *pt;
441 : :
442 : 0 : for (i = 0; i < num_tuples; i++) {
443 : : printf("##### PDUMP DEBUG STATS #####\n");
444 : : pt = &pdump_t[i];
445 : 0 : printf(" -packets dequeued: %"PRIu64"\n",
446 : : pt->stats.dequeue_pkts);
447 : 0 : printf(" -packets transmitted to vdev: %"PRIu64"\n",
448 : : pt->stats.tx_pkts);
449 : 0 : printf(" -packets freed: %"PRIu64"\n",
450 : : pt->stats.freed_pkts);
451 : : }
452 : 0 : }
453 : :
454 : : static inline void
455 : 0 : disable_pdump(struct pdump_tuples *pt)
456 : : {
457 : 0 : if (pt->dump_by_type == DEVICE_ID)
458 : 0 : rte_pdump_disable_by_deviceid(pt->device_id, pt->queue,
459 : : pt->dir);
460 : 0 : else if (pt->dump_by_type == PORT_ID)
461 : 0 : rte_pdump_disable(pt->port, pt->queue, pt->dir);
462 : 0 : }
463 : :
464 : : static inline void
465 : 0 : pdump_rxtx(struct rte_ring *ring, uint16_t vdev_id, struct pdump_stats *stats)
466 : : {
467 : : /* write input packets of port to vdev for pdump */
468 : : struct rte_mbuf *rxtx_bufs[BURST_SIZE];
469 : :
470 : : /* first dequeue packets from ring of primary process */
471 : 0 : const uint16_t nb_in_deq = rte_ring_dequeue_burst(ring,
472 : : (void *)rxtx_bufs, BURST_SIZE, NULL);
473 : 0 : stats->dequeue_pkts += nb_in_deq;
474 : :
475 : 0 : if (nb_in_deq) {
476 : : /* then sent on vdev */
477 : 0 : uint16_t nb_in_txd = rte_eth_tx_burst(
478 : : vdev_id,
479 : : 0, rxtx_bufs, nb_in_deq);
480 : 0 : stats->tx_pkts += nb_in_txd;
481 : :
482 : 0 : if (unlikely(nb_in_txd < nb_in_deq)) {
483 : 0 : unsigned int drops = nb_in_deq - nb_in_txd;
484 : :
485 : 0 : rte_pktmbuf_free_bulk(&rxtx_bufs[nb_in_txd], drops);
486 : 0 : stats->freed_pkts += drops;
487 : : }
488 : : }
489 : 0 : }
490 : :
491 : : static void
492 : : free_ring_data(struct rte_ring *ring, uint16_t vdev_id,
493 : : struct pdump_stats *stats)
494 : : {
495 : 0 : while (rte_ring_count(ring))
496 : 0 : pdump_rxtx(ring, vdev_id, stats);
497 : : }
498 : :
499 : : static void
500 : 0 : cleanup_rings(void)
501 : : {
502 : : int i;
503 : : struct pdump_tuples *pt;
504 : :
505 : 0 : for (i = 0; i < num_tuples; i++) {
506 : : pt = &pdump_t[i];
507 : :
508 : 0 : free(pt->device_id);
509 : :
510 : : /* free the rings */
511 : 0 : rte_ring_free(pt->rx_ring);
512 : 0 : rte_ring_free(pt->tx_ring);
513 : 0 : rte_mempool_free(pt->mp);
514 : : }
515 : 0 : }
516 : :
517 : : static void
518 : 0 : cleanup_pdump_resources(void)
519 : : {
520 : : int i;
521 : : struct pdump_tuples *pt;
522 : : char name[RTE_ETH_NAME_MAX_LEN];
523 : :
524 : : /* disable pdump and free the pdump_tuple resources */
525 : 0 : for (i = 0; i < num_tuples; i++) {
526 : 0 : pt = &pdump_t[i];
527 : :
528 : : /* remove callbacks */
529 : 0 : disable_pdump(pt);
530 : :
531 : : /*
532 : : * transmit rest of the enqueued packets of the rings on to
533 : : * the vdev, in order to release mbufs to the mepool.
534 : : **/
535 : 0 : if (pt->dir & RTE_PDUMP_FLAG_RX)
536 : 0 : free_ring_data(pt->rx_ring, pt->rx_vdev_id, &pt->stats);
537 : 0 : if (pt->dir & RTE_PDUMP_FLAG_TX)
538 : 0 : free_ring_data(pt->tx_ring, pt->tx_vdev_id, &pt->stats);
539 : :
540 : : /* Remove the vdev(s) created */
541 : 0 : if (pt->dir & RTE_PDUMP_FLAG_RX) {
542 : 0 : rte_eth_dev_get_name_by_port(pt->rx_vdev_id, name);
543 : 0 : rte_eal_hotplug_remove("vdev", name);
544 : : }
545 : :
546 : 0 : if (pt->single_pdump_dev)
547 : 0 : continue;
548 : :
549 : 0 : if (pt->dir & RTE_PDUMP_FLAG_TX) {
550 : 0 : rte_eth_dev_get_name_by_port(pt->tx_vdev_id, name);
551 : 0 : rte_eal_hotplug_remove("vdev", name);
552 : : }
553 : :
554 : : }
555 : 0 : rte_pdump_uninit();
556 : 0 : cleanup_rings();
557 : 0 : }
558 : :
559 : : static void
560 : 0 : disable_primary_monitor(void)
561 : : {
562 : : int ret;
563 : :
564 : : /*
565 : : * Cancel monitoring of primary process.
566 : : * There will be no error if no alarm is set
567 : : * (in case primary process kill was detected earlier).
568 : : */
569 : 0 : ret = rte_eal_alarm_cancel(monitor_primary, NULL);
570 : 0 : if (ret < 0)
571 : : printf("Fail to disable monitor:%d\n", ret);
572 : 0 : }
573 : :
574 : : static void
575 : 0 : signal_handler(int sig_num __rte_unused)
576 : : {
577 : 0 : quit_signal = 1;
578 : 0 : }
579 : :
580 : : static inline int
581 : 0 : configure_vdev(uint16_t port_id)
582 : : {
583 : : struct rte_ether_addr addr;
584 : : const uint16_t rxRings = 0, txRings = 1;
585 : : int ret;
586 : : uint16_t q;
587 : :
588 : 0 : if (!rte_eth_dev_is_valid_port(port_id))
589 : : return -1;
590 : :
591 : 0 : ret = rte_eth_dev_configure(port_id, rxRings, txRings,
592 : : &port_conf_default);
593 : 0 : if (ret != 0)
594 : 0 : rte_exit(EXIT_FAILURE, "dev config failed\n");
595 : :
596 : 0 : for (q = 0; q < txRings; q++) {
597 : 0 : ret = rte_eth_tx_queue_setup(port_id, q, TX_DESC_PER_QUEUE,
598 : 0 : rte_eth_dev_socket_id(port_id), NULL);
599 : 0 : if (ret < 0)
600 : 0 : rte_exit(EXIT_FAILURE, "queue setup failed\n");
601 : : }
602 : :
603 : 0 : ret = rte_eth_dev_start(port_id);
604 : 0 : if (ret < 0)
605 : 0 : rte_exit(EXIT_FAILURE, "dev start failed\n");
606 : :
607 : 0 : ret = rte_eth_macaddr_get(port_id, &addr);
608 : 0 : if (ret != 0)
609 : 0 : rte_exit(EXIT_FAILURE, "macaddr get failed\n");
610 : :
611 : 0 : printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
612 : : " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
613 : 0 : port_id, RTE_ETHER_ADDR_BYTES(&addr));
614 : :
615 : 0 : ret = rte_eth_promiscuous_enable(port_id);
616 : 0 : if (ret != 0) {
617 : 0 : rte_exit(EXIT_FAILURE,
618 : : "promiscuous mode enable failed: %s\n",
619 : : rte_strerror(-ret));
620 : : return ret;
621 : : }
622 : :
623 : : return 0;
624 : : }
625 : :
626 : : static void
627 : 0 : create_mp_ring_vdev(void)
628 : : {
629 : : int i;
630 : : uint16_t portid;
631 : : struct pdump_tuples *pt = NULL;
632 : : struct rte_mempool *mbuf_pool = NULL;
633 : : char vdev_name[SIZE];
634 : : char vdev_args[SIZE];
635 : : char ring_name[SIZE];
636 : : char mempool_name[SIZE];
637 : :
638 : 0 : for (i = 0; i < num_tuples; i++) {
639 : : pt = &pdump_t[i];
640 : : snprintf(mempool_name, SIZE, MP_NAME, i);
641 : 0 : mbuf_pool = rte_mempool_lookup(mempool_name);
642 : 0 : if (mbuf_pool == NULL) {
643 : : /* create mempool */
644 : 0 : mbuf_pool = rte_pktmbuf_pool_create_by_ops(mempool_name,
645 : : pt->total_num_mbufs,
646 : : MBUF_POOL_CACHE_SIZE, 0,
647 : 0 : pt->mbuf_data_size,
648 : 0 : rte_socket_id(), "ring_mp_mc");
649 : 0 : if (mbuf_pool == NULL) {
650 : 0 : cleanup_rings();
651 : 0 : rte_exit(EXIT_FAILURE,
652 : : "Mempool creation failed: %s\n",
653 : : rte_strerror(rte_errno));
654 : : }
655 : : }
656 : 0 : pt->mp = mbuf_pool;
657 : :
658 : 0 : if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
659 : : /* if captured packets has to send to the same vdev */
660 : : /* create rx_ring */
661 : : snprintf(ring_name, SIZE, RX_RING, i);
662 : 0 : pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
663 : 0 : rte_socket_id(), 0);
664 : 0 : if (pt->rx_ring == NULL) {
665 : 0 : cleanup_rings();
666 : 0 : rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
667 : : rte_strerror(rte_errno),
668 : : __func__, __LINE__);
669 : : }
670 : :
671 : : /* create tx_ring */
672 : : snprintf(ring_name, SIZE, TX_RING, i);
673 : 0 : pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
674 : 0 : rte_socket_id(), 0);
675 : 0 : if (pt->tx_ring == NULL) {
676 : 0 : cleanup_rings();
677 : 0 : rte_exit(EXIT_FAILURE, "%s:%s:%d\n",
678 : : rte_strerror(rte_errno),
679 : : __func__, __LINE__);
680 : : }
681 : :
682 : : /* create vdevs */
683 : : snprintf(vdev_name, sizeof(vdev_name),
684 : : VDEV_NAME_FMT, RX_STR, i);
685 : 0 : (pt->rx_vdev_stream_type == IFACE) ?
686 : : snprintf(vdev_args, sizeof(vdev_args),
687 : 0 : VDEV_IFACE_ARGS_FMT, pt->rx_dev) :
688 : : snprintf(vdev_args, sizeof(vdev_args),
689 : 0 : VDEV_PCAP_ARGS_FMT, pt->rx_dev);
690 : 0 : if (rte_eal_hotplug_add("vdev", vdev_name,
691 : : vdev_args) < 0) {
692 : 0 : cleanup_rings();
693 : 0 : rte_exit(EXIT_FAILURE,
694 : : "vdev creation failed:%s:%d\n",
695 : : __func__, __LINE__);
696 : : }
697 : 0 : if (rte_eth_dev_get_port_by_name(vdev_name,
698 : : &portid) != 0) {
699 : 0 : rte_eal_hotplug_remove("vdev", vdev_name);
700 : 0 : cleanup_rings();
701 : 0 : rte_exit(EXIT_FAILURE,
702 : : "cannot find added vdev %s:%s:%d\n",
703 : : vdev_name, __func__, __LINE__);
704 : : }
705 : 0 : pt->rx_vdev_id = portid;
706 : :
707 : : /* configure vdev */
708 : 0 : configure_vdev(pt->rx_vdev_id);
709 : :
710 : 0 : if (pt->single_pdump_dev)
711 : 0 : pt->tx_vdev_id = portid;
712 : : else {
713 : : snprintf(vdev_name, sizeof(vdev_name),
714 : : VDEV_NAME_FMT, TX_STR, i);
715 : 0 : (pt->rx_vdev_stream_type == IFACE) ?
716 : : snprintf(vdev_args, sizeof(vdev_args),
717 : 0 : VDEV_IFACE_ARGS_FMT, pt->tx_dev) :
718 : : snprintf(vdev_args, sizeof(vdev_args),
719 : 0 : VDEV_PCAP_ARGS_FMT, pt->tx_dev);
720 : 0 : if (rte_eal_hotplug_add("vdev", vdev_name,
721 : : vdev_args) < 0) {
722 : 0 : cleanup_rings();
723 : 0 : rte_exit(EXIT_FAILURE,
724 : : "vdev creation failed:"
725 : : "%s:%d\n", __func__, __LINE__);
726 : : }
727 : 0 : if (rte_eth_dev_get_port_by_name(vdev_name,
728 : : &portid) != 0) {
729 : 0 : rte_eal_hotplug_remove("vdev",
730 : : vdev_name);
731 : 0 : cleanup_rings();
732 : 0 : rte_exit(EXIT_FAILURE,
733 : : "cannot find added vdev %s:%s:%d\n",
734 : : vdev_name, __func__, __LINE__);
735 : : }
736 : 0 : pt->tx_vdev_id = portid;
737 : :
738 : : /* configure vdev */
739 : 0 : configure_vdev(pt->tx_vdev_id);
740 : : }
741 : 0 : } else if (pt->dir == RTE_PDUMP_FLAG_RX) {
742 : :
743 : : /* create rx_ring */
744 : : snprintf(ring_name, SIZE, RX_RING, i);
745 : 0 : pt->rx_ring = rte_ring_create(ring_name, pt->ring_size,
746 : 0 : rte_socket_id(), 0);
747 : 0 : if (pt->rx_ring == NULL) {
748 : 0 : cleanup_rings();
749 : 0 : rte_exit(EXIT_FAILURE, "%s\n",
750 : : rte_strerror(rte_errno));
751 : : }
752 : :
753 : : snprintf(vdev_name, sizeof(vdev_name),
754 : : VDEV_NAME_FMT, RX_STR, i);
755 : 0 : (pt->rx_vdev_stream_type == IFACE) ?
756 : : snprintf(vdev_args, sizeof(vdev_args),
757 : 0 : VDEV_IFACE_ARGS_FMT, pt->rx_dev) :
758 : : snprintf(vdev_args, sizeof(vdev_args),
759 : 0 : VDEV_PCAP_ARGS_FMT, pt->rx_dev);
760 : 0 : if (rte_eal_hotplug_add("vdev", vdev_name,
761 : : vdev_args) < 0) {
762 : 0 : cleanup_rings();
763 : 0 : rte_exit(EXIT_FAILURE,
764 : : "vdev creation failed:%s:%d\n",
765 : : __func__, __LINE__);
766 : : }
767 : 0 : if (rte_eth_dev_get_port_by_name(vdev_name,
768 : : &portid) != 0) {
769 : 0 : rte_eal_hotplug_remove("vdev", vdev_name);
770 : 0 : cleanup_rings();
771 : 0 : rte_exit(EXIT_FAILURE,
772 : : "cannot find added vdev %s:%s:%d\n",
773 : : vdev_name, __func__, __LINE__);
774 : : }
775 : 0 : pt->rx_vdev_id = portid;
776 : : /* configure vdev */
777 : 0 : configure_vdev(pt->rx_vdev_id);
778 : 0 : } else if (pt->dir == RTE_PDUMP_FLAG_TX) {
779 : :
780 : : /* create tx_ring */
781 : : snprintf(ring_name, SIZE, TX_RING, i);
782 : 0 : pt->tx_ring = rte_ring_create(ring_name, pt->ring_size,
783 : 0 : rte_socket_id(), 0);
784 : 0 : if (pt->tx_ring == NULL) {
785 : 0 : cleanup_rings();
786 : 0 : rte_exit(EXIT_FAILURE, "%s\n",
787 : : rte_strerror(rte_errno));
788 : : }
789 : :
790 : : snprintf(vdev_name, sizeof(vdev_name),
791 : : VDEV_NAME_FMT, TX_STR, i);
792 : 0 : (pt->tx_vdev_stream_type == IFACE) ?
793 : : snprintf(vdev_args, sizeof(vdev_args),
794 : 0 : VDEV_IFACE_ARGS_FMT, pt->tx_dev) :
795 : : snprintf(vdev_args, sizeof(vdev_args),
796 : 0 : VDEV_PCAP_ARGS_FMT, pt->tx_dev);
797 : 0 : if (rte_eal_hotplug_add("vdev", vdev_name,
798 : : vdev_args) < 0) {
799 : 0 : cleanup_rings();
800 : 0 : rte_exit(EXIT_FAILURE,
801 : : "vdev creation failed\n");
802 : : }
803 : 0 : if (rte_eth_dev_get_port_by_name(vdev_name,
804 : : &portid) != 0) {
805 : 0 : rte_eal_hotplug_remove("vdev", vdev_name);
806 : 0 : cleanup_rings();
807 : 0 : rte_exit(EXIT_FAILURE,
808 : : "cannot find added vdev %s:%s:%d\n",
809 : : vdev_name, __func__, __LINE__);
810 : : }
811 : 0 : pt->tx_vdev_id = portid;
812 : :
813 : : /* configure vdev */
814 : 0 : configure_vdev(pt->tx_vdev_id);
815 : : }
816 : : }
817 : 0 : }
818 : :
819 : : static void
820 : 0 : enable_pdump(void)
821 : : {
822 : : int i;
823 : : struct pdump_tuples *pt;
824 : : int ret = 0, ret1 = 0;
825 : :
826 : 0 : if (rte_pdump_init() < 0)
827 : 0 : rte_exit(EXIT_FAILURE, "pdump init failed\n");
828 : :
829 : 0 : for (i = 0; i < num_tuples; i++) {
830 : : pt = &pdump_t[i];
831 : 0 : if (pt->dir == RTE_PDUMP_FLAG_RXTX) {
832 : 0 : if (pt->dump_by_type == DEVICE_ID) {
833 : 0 : ret = rte_pdump_enable_by_deviceid(
834 : : pt->device_id,
835 : 0 : pt->queue,
836 : : RTE_PDUMP_FLAG_RX,
837 : : pt->rx_ring,
838 : : pt->mp, NULL);
839 : 0 : ret1 = rte_pdump_enable_by_deviceid(
840 : : pt->device_id,
841 : 0 : pt->queue,
842 : : RTE_PDUMP_FLAG_TX,
843 : : pt->tx_ring,
844 : : pt->mp, NULL);
845 : 0 : } else if (pt->dump_by_type == PORT_ID) {
846 : 0 : ret = rte_pdump_enable(pt->port, pt->queue,
847 : : RTE_PDUMP_FLAG_RX,
848 : : pt->rx_ring, pt->mp, NULL);
849 : 0 : ret1 = rte_pdump_enable(pt->port, pt->queue,
850 : : RTE_PDUMP_FLAG_TX,
851 : : pt->tx_ring, pt->mp, NULL);
852 : : }
853 : 0 : } else if (pt->dir == RTE_PDUMP_FLAG_RX) {
854 : 0 : if (pt->dump_by_type == DEVICE_ID)
855 : 0 : ret = rte_pdump_enable_by_deviceid(
856 : : pt->device_id,
857 : 0 : pt->queue,
858 : : pt->dir, pt->rx_ring,
859 : : pt->mp, NULL);
860 : 0 : else if (pt->dump_by_type == PORT_ID)
861 : 0 : ret = rte_pdump_enable(pt->port, pt->queue,
862 : : pt->dir,
863 : : pt->rx_ring, pt->mp, NULL);
864 : 0 : } else if (pt->dir == RTE_PDUMP_FLAG_TX) {
865 : 0 : if (pt->dump_by_type == DEVICE_ID)
866 : 0 : ret = rte_pdump_enable_by_deviceid(
867 : : pt->device_id,
868 : 0 : pt->queue,
869 : : pt->dir,
870 : : pt->tx_ring, pt->mp, NULL);
871 : 0 : else if (pt->dump_by_type == PORT_ID)
872 : 0 : ret = rte_pdump_enable(pt->port, pt->queue,
873 : : pt->dir,
874 : : pt->tx_ring, pt->mp, NULL);
875 : : }
876 : 0 : if (ret < 0 || ret1 < 0) {
877 : 0 : cleanup_pdump_resources();
878 : 0 : rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
879 : : }
880 : : }
881 : 0 : }
882 : :
883 : : static inline void
884 : 0 : pdump_packets(struct pdump_tuples *pt)
885 : : {
886 : 0 : if (pt->dir & RTE_PDUMP_FLAG_RX)
887 : 0 : pdump_rxtx(pt->rx_ring, pt->rx_vdev_id, &pt->stats);
888 : 0 : if (pt->dir & RTE_PDUMP_FLAG_TX)
889 : 0 : pdump_rxtx(pt->tx_ring, pt->tx_vdev_id, &pt->stats);
890 : 0 : }
891 : :
892 : : static int
893 : 0 : dump_packets_core(void *arg)
894 : : {
895 : : struct pdump_tuples *pt = (struct pdump_tuples *) arg;
896 : :
897 : 0 : printf(" core (%u); port %u device (%s) queue %u\n",
898 : 0 : rte_lcore_id(), pt->port, pt->device_id, pt->queue);
899 : 0 : fflush(stdout);
900 : :
901 : 0 : while (!quit_signal)
902 : 0 : pdump_packets(pt);
903 : :
904 : 0 : return 0;
905 : : }
906 : :
907 : : static unsigned int
908 : 0 : get_next_core(unsigned int lcore)
909 : : {
910 : 0 : lcore = rte_get_next_lcore(lcore, 1, 0);
911 : 0 : if (lcore == RTE_MAX_LCORE)
912 : 0 : rte_exit(EXIT_FAILURE,
913 : : "Max core limit %u reached for packet capture", lcore);
914 : 0 : return lcore;
915 : : }
916 : :
917 : : static inline void
918 : 0 : dump_packets(void)
919 : : {
920 : : int i;
921 : : unsigned int lcore_id = 0;
922 : :
923 : 0 : if (num_tuples == 0)
924 : 0 : rte_exit(EXIT_FAILURE, "No device specified for capture\n");
925 : :
926 : 0 : if (!multiple_core_capture) {
927 : : printf(" core (%u), capture for (%d) tuples\n",
928 : : rte_lcore_id(), num_tuples);
929 : :
930 : 0 : for (i = 0; i < num_tuples; i++)
931 : 0 : printf(" - port %u device (%s) queue %u\n",
932 : 0 : pdump_t[i].port,
933 : : pdump_t[i].device_id,
934 : 0 : pdump_t[i].queue);
935 : :
936 : 0 : while (!quit_signal) {
937 : 0 : for (i = 0; i < num_tuples; i++)
938 : 0 : pdump_packets(&pdump_t[i]);
939 : : }
940 : :
941 : : return;
942 : : }
943 : :
944 : : /* check if there enough core */
945 : 0 : if ((uint32_t)num_tuples >= rte_lcore_count()) {
946 : : printf("Insufficient cores to run parallel!\n");
947 : 0 : return;
948 : : }
949 : :
950 : 0 : lcore_id = get_next_core(lcore_id);
951 : :
952 : 0 : for (i = 0; i < num_tuples; i++) {
953 : 0 : rte_eal_remote_launch(dump_packets_core,
954 : 0 : &pdump_t[i], lcore_id);
955 : 0 : lcore_id = get_next_core(lcore_id);
956 : :
957 : 0 : if (rte_eal_wait_lcore(lcore_id) < 0)
958 : 0 : rte_exit(EXIT_FAILURE, "failed to wait\n");
959 : : }
960 : :
961 : : /* main core */
962 : 0 : while (!quit_signal)
963 : : ;
964 : : }
965 : :
966 : : static void
967 : 0 : enable_primary_monitor(void)
968 : : {
969 : : int ret;
970 : :
971 : : /* Once primary exits, so will pdump. */
972 : 0 : ret = rte_eal_alarm_set(MONITOR_INTERVAL, monitor_primary, NULL);
973 : 0 : if (ret < 0)
974 : : printf("Fail to enable monitor:%d\n", ret);
975 : 0 : }
976 : :
977 : : int
978 : 0 : main(int argc, char **argv)
979 : 0 : {
980 : 0 : struct sigaction action = {
981 : : .sa_flags = SA_RESTART,
982 : : .sa_handler = signal_handler,
983 : : };
984 : : struct sigaction origaction;
985 : : int diag;
986 : : int ret;
987 : : int i;
988 : :
989 : 0 : char mp_flag[] = "--proc-type=secondary";
990 : 0 : char *argp[argc + 2]; /* add proc-type, and final NULL entry */
991 : : int n_argp = 0;
992 : :
993 : : /* catch ctrl-c so we can cleanup on exit */
994 : 0 : sigemptyset(&action.sa_mask);
995 : 0 : sigaction(SIGTERM, &action, NULL);
996 : 0 : sigaction(SIGINT, &action, NULL);
997 : 0 : sigaction(SIGPIPE, &action, NULL);
998 : 0 : sigaction(SIGHUP, NULL, &origaction);
999 : 0 : if (origaction.sa_handler == SIG_DFL)
1000 : 0 : sigaction(SIGHUP, &action, NULL);
1001 : :
1002 : 0 : argp[n_argp++] = argv[0];
1003 : 0 : argp[n_argp++] = mp_flag;
1004 : :
1005 : 0 : for (i = 1; i < argc; i++) {
1006 : 0 : argp[n_argp] = argv[i];
1007 : : /* drop any user-provided proc-type to avoid dup flags */
1008 : 0 : if (strncmp(argv[i], mp_flag, strlen("--proc-type")) != 0)
1009 : 0 : n_argp++;
1010 : : }
1011 : 0 : argp[n_argp] = NULL;
1012 : :
1013 : 0 : diag = rte_eal_init(n_argp, argp);
1014 : 0 : if (diag < 0)
1015 : 0 : rte_panic("Cannot init EAL\n");
1016 : :
1017 : 0 : if (rte_eth_dev_count_avail() == 0)
1018 : 0 : rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
1019 : :
1020 : : /* parse app arguments */
1021 : 0 : if (n_argp - diag > 1) {
1022 : 0 : ret = launch_args_parse(n_argp - diag, argp + diag, argp[0]);
1023 : 0 : if (ret < 0)
1024 : 0 : rte_exit(EXIT_FAILURE, "Invalid argument\n");
1025 : : }
1026 : :
1027 : : /* create mempool, ring and vdevs info */
1028 : 0 : create_mp_ring_vdev();
1029 : 0 : enable_pdump();
1030 : 0 : enable_primary_monitor();
1031 : 0 : dump_packets();
1032 : :
1033 : 0 : disable_primary_monitor();
1034 : :
1035 : : /* dump debug stats */
1036 : 0 : print_pdump_stats();
1037 : :
1038 : : /* If primary has exited, do not try and communicate with it */
1039 : 0 : if (!rte_eal_primary_proc_alive(NULL))
1040 : : return 0;
1041 : :
1042 : 0 : cleanup_pdump_resources();
1043 : :
1044 : 0 : return rte_eal_cleanup() ? EXIT_FAILURE : 0;
1045 : : }
|