1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
3 */
4
5 #include <stdlib.h>
6 #include <signal.h>
7 #include <getopt.h>
8 #include <stdbool.h>
9
10 #include <rte_eal.h>
11 #include <rte_common.h>
12 #include <rte_errno.h>
13 #include <rte_ethdev.h>
14 #include <rte_lcore.h>
15 #include <rte_malloc.h>
16 #include <rte_mbuf.h>
17 #include <rte_mempool.h>
18 #include <rte_ring.h>
19 #include <rte_reorder.h>
20
21 #define RX_DESC_PER_QUEUE 1024
22 #define TX_DESC_PER_QUEUE 1024
23
24 #define MAX_PKTS_BURST 32
25 #define REORDER_BUFFER_SIZE 8192
26 #define MBUF_PER_POOL 65535
27 #define MBUF_POOL_CACHE_SIZE 250
28
29 #define RING_SIZE 16384
30
31 /* Macros for printing using RTE_LOG */
32 #define RTE_LOGTYPE_REORDERAPP RTE_LOGTYPE_USER1
33
34 enum {
35 #define OPT_DISABLE_REORDER "disable-reorder"
36 OPT_DISABLE_REORDER_NUM = 256,
37 #define OPT_INSIGHT_WORKER "insight-worker"
38 OPT_INSIGHT_WORKER_NUM,
39 };
40
41 unsigned int portmask;
42 unsigned int disable_reorder;
43 unsigned int insight_worker;
44 volatile uint8_t quit_signal;
45
46 static struct rte_mempool *mbuf_pool;
47
48 static struct rte_eth_conf port_conf_default;
49
50 struct worker_thread_args {
51 struct rte_ring *ring_in;
52 struct rte_ring *ring_out;
53 };
54
55 struct send_thread_args {
56 struct rte_ring *ring_in;
57 struct rte_reorder_buffer *buffer;
58 };
59
60 volatile struct app_stats {
61 alignas(RTE_CACHE_LINE_SIZE) struct {
62 uint64_t rx_pkts;
63 uint64_t enqueue_pkts;
64 uint64_t enqueue_failed_pkts;
65 } rx;
66
67 alignas(RTE_CACHE_LINE_SIZE) struct {
68 uint64_t dequeue_pkts;
69 uint64_t enqueue_pkts;
70 uint64_t enqueue_failed_pkts;
71 } wkr;
72
73 alignas(RTE_CACHE_LINE_SIZE) struct {
74 uint64_t dequeue_pkts;
75 /* Too early pkts transmitted directly w/o reordering */
76 uint64_t early_pkts_txtd_woro;
77 /* Too early pkts failed from direct transmit */
78 uint64_t early_pkts_tx_failed_woro;
79 uint64_t ro_tx_pkts;
80 uint64_t ro_tx_failed_pkts;
81 } tx;
82 } app_stats;
83
84 /* per worker lcore stats */
85 struct __rte_cache_aligned wkr_stats_per {
86 uint64_t deq_pkts;
87 uint64_t enq_pkts;
88 uint64_t enq_failed_pkts;
89 };
90
91 static struct wkr_stats_per wkr_stats[RTE_MAX_LCORE] = { {0} };
92 /**
93 * Get the last enabled lcore ID
94 *
95 * @return
96 * The last enabled lcore ID.
97 */
98 static unsigned int
get_last_lcore_id(void)99 get_last_lcore_id(void)
100 {
101 int i;
102
103 for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
104 if (rte_lcore_is_enabled(i))
105 return i;
106 return 0;
107 }
108
109 /**
110 * Get the previous enabled lcore ID
111 * @param id
112 * The current lcore ID
113 * @return
114 * The previous enabled lcore ID or the current lcore
115 * ID if it is the first available core.
116 */
117 static unsigned int
get_previous_lcore_id(unsigned int id)118 get_previous_lcore_id(unsigned int id)
119 {
120 int i;
121
122 for (i = id - 1; i >= 0; i--)
123 if (rte_lcore_is_enabled(i))
124 return i;
125 return id;
126 }
127
128 static inline void
pktmbuf_free_bulk(struct rte_mbuf * mbuf_table[],unsigned n)129 pktmbuf_free_bulk(struct rte_mbuf *mbuf_table[], unsigned n)
130 {
131 unsigned int i;
132
133 for (i = 0; i < n; i++)
134 rte_pktmbuf_free(mbuf_table[i]);
135 }
136
137 /* display usage */
138 static void
print_usage(const char * prgname)139 print_usage(const char *prgname)
140 {
141 printf("%s [EAL options] -- -p PORTMASK\n"
142 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
143 prgname);
144 }
145
146 static int
parse_portmask(const char * portmask)147 parse_portmask(const char *portmask)
148 {
149 unsigned long pm;
150 char *end = NULL;
151
152 /* parse hexadecimal string */
153 pm = strtoul(portmask, &end, 16);
154 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
155 return 0;
156
157 return pm;
158 }
159
160 /* Parse the argument given in the command line of the application */
161 static int
parse_args(int argc,char ** argv)162 parse_args(int argc, char **argv)
163 {
164 int opt;
165 int option_index;
166 char **argvopt;
167 char *prgname = argv[0];
168 static struct option lgopts[] = {
169 {OPT_DISABLE_REORDER, 0, NULL, OPT_DISABLE_REORDER_NUM},
170 {OPT_INSIGHT_WORKER, 0, NULL, OPT_INSIGHT_WORKER_NUM },
171 {NULL, 0, 0, 0 }
172 };
173
174 argvopt = argv;
175
176 while ((opt = getopt_long(argc, argvopt, "p:",
177 lgopts, &option_index)) != EOF) {
178 switch (opt) {
179 /* portmask */
180 case 'p':
181 portmask = parse_portmask(optarg);
182 if (portmask == 0) {
183 printf("invalid portmask\n");
184 print_usage(prgname);
185 return -1;
186 }
187 break;
188
189 /* long options */
190 case OPT_DISABLE_REORDER_NUM:
191 printf("reorder disabled\n");
192 disable_reorder = 1;
193 break;
194
195 case OPT_INSIGHT_WORKER_NUM:
196 printf("print all worker statistics\n");
197 insight_worker = 1;
198 break;
199
200 default:
201 print_usage(prgname);
202 return -1;
203 }
204 }
205 if (optind <= 1) {
206 print_usage(prgname);
207 return -1;
208 }
209
210 argv[optind-1] = prgname;
211 optind = 1; /* reset getopt lib */
212 return 0;
213 }
214
215 /*
216 * Tx buffer error callback
217 */
218 static void
flush_tx_error_callback(struct rte_mbuf ** unsent,uint16_t count,void * userdata __rte_unused)219 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
220 void *userdata __rte_unused) {
221
222 /* free the mbufs which failed from transmit */
223 app_stats.tx.ro_tx_failed_pkts += count;
224 RTE_LOG_DP(DEBUG, REORDERAPP, "%s:Packet loss with tx_burst\n", __func__);
225 pktmbuf_free_bulk(unsent, count);
226
227 }
228
229 static inline int
free_tx_buffers(struct rte_eth_dev_tx_buffer * tx_buffer[])230 free_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[]) {
231 uint16_t port_id;
232
233 /* initialize buffers for all ports */
234 RTE_ETH_FOREACH_DEV(port_id) {
235 /* skip ports that are not enabled */
236 if ((portmask & (1 << port_id)) == 0)
237 continue;
238
239 rte_free(tx_buffer[port_id]);
240 }
241 return 0;
242 }
243
244 static inline int
configure_tx_buffers(struct rte_eth_dev_tx_buffer * tx_buffer[])245 configure_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[])
246 {
247 uint16_t port_id;
248 int ret;
249
250 /* initialize buffers for all ports */
251 RTE_ETH_FOREACH_DEV(port_id) {
252 /* skip ports that are not enabled */
253 if ((portmask & (1 << port_id)) == 0)
254 continue;
255
256 /* Initialize TX buffers */
257 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
258 RTE_ETH_TX_BUFFER_SIZE(MAX_PKTS_BURST), 0,
259 rte_eth_dev_socket_id(port_id));
260 if (tx_buffer[port_id] == NULL)
261 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
262 port_id);
263
264 rte_eth_tx_buffer_init(tx_buffer[port_id], MAX_PKTS_BURST);
265
266 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
267 flush_tx_error_callback, NULL);
268 if (ret < 0)
269 rte_exit(EXIT_FAILURE,
270 "Cannot set error callback for tx buffer on port %u\n",
271 port_id);
272 }
273 return 0;
274 }
275
276 static inline int
configure_eth_port(uint16_t port_id)277 configure_eth_port(uint16_t port_id)
278 {
279 struct rte_ether_addr addr;
280 const uint16_t rxRings = 1, txRings = 1;
281 int ret;
282 uint16_t q;
283 uint16_t nb_rxd = RX_DESC_PER_QUEUE;
284 uint16_t nb_txd = TX_DESC_PER_QUEUE;
285 struct rte_eth_dev_info dev_info;
286 struct rte_eth_txconf txconf;
287 struct rte_eth_conf port_conf = port_conf_default;
288
289 if (!rte_eth_dev_is_valid_port(port_id))
290 return -1;
291
292 ret = rte_eth_dev_info_get(port_id, &dev_info);
293 if (ret != 0) {
294 printf("Error during getting device (port %u) info: %s\n",
295 port_id, strerror(-ret));
296 return ret;
297 }
298
299 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
300 port_conf.txmode.offloads |=
301 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
302 ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf);
303 if (ret != 0)
304 return ret;
305
306 ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
307 if (ret != 0)
308 return ret;
309
310 for (q = 0; q < rxRings; q++) {
311 ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd,
312 rte_eth_dev_socket_id(port_id), NULL,
313 mbuf_pool);
314 if (ret < 0)
315 return ret;
316 }
317
318 txconf = dev_info.default_txconf;
319 txconf.offloads = port_conf.txmode.offloads;
320 for (q = 0; q < txRings; q++) {
321 ret = rte_eth_tx_queue_setup(port_id, q, nb_txd,
322 rte_eth_dev_socket_id(port_id), &txconf);
323 if (ret < 0)
324 return ret;
325 }
326
327 ret = rte_eth_dev_start(port_id);
328 if (ret < 0)
329 return ret;
330
331 ret = rte_eth_macaddr_get(port_id, &addr);
332 if (ret != 0) {
333 printf("Failed to get MAC address (port %u): %s\n",
334 port_id, rte_strerror(-ret));
335 return ret;
336 }
337
338 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
339 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
340 port_id, RTE_ETHER_ADDR_BYTES(&addr));
341
342 ret = rte_eth_promiscuous_enable(port_id);
343 if (ret != 0)
344 return ret;
345
346 return 0;
347 }
348
349 static void
print_stats(void)350 print_stats(void)
351 {
352 uint16_t i;
353 struct rte_eth_stats eth_stats;
354 unsigned int lcore_id, last_lcore_id, main_lcore_id, end_w_lcore_id;
355
356 last_lcore_id = get_last_lcore_id();
357 main_lcore_id = rte_get_main_lcore();
358 end_w_lcore_id = get_previous_lcore_id(last_lcore_id);
359
360 printf("\nRX thread stats:\n");
361 printf(" - Pkts rxd: %"PRIu64"\n",
362 app_stats.rx.rx_pkts);
363 printf(" - Pkts enqd to workers ring: %"PRIu64"\n",
364 app_stats.rx.enqueue_pkts);
365
366 for (lcore_id = 0; lcore_id <= end_w_lcore_id; lcore_id++) {
367 if (insight_worker
368 && rte_lcore_is_enabled(lcore_id)
369 && lcore_id != main_lcore_id) {
370 printf("\nWorker thread stats on core [%u]:\n",
371 lcore_id);
372 printf(" - Pkts deqd from workers ring: %"PRIu64"\n",
373 wkr_stats[lcore_id].deq_pkts);
374 printf(" - Pkts enqd to tx ring: %"PRIu64"\n",
375 wkr_stats[lcore_id].enq_pkts);
376 printf(" - Pkts enq to tx failed: %"PRIu64"\n",
377 wkr_stats[lcore_id].enq_failed_pkts);
378 }
379
380 app_stats.wkr.dequeue_pkts += wkr_stats[lcore_id].deq_pkts;
381 app_stats.wkr.enqueue_pkts += wkr_stats[lcore_id].enq_pkts;
382 app_stats.wkr.enqueue_failed_pkts +=
383 wkr_stats[lcore_id].enq_failed_pkts;
384 }
385
386 printf("\nWorker thread stats:\n");
387 printf(" - Pkts deqd from workers ring: %"PRIu64"\n",
388 app_stats.wkr.dequeue_pkts);
389 printf(" - Pkts enqd to tx ring: %"PRIu64"\n",
390 app_stats.wkr.enqueue_pkts);
391 printf(" - Pkts enq to tx failed: %"PRIu64"\n",
392 app_stats.wkr.enqueue_failed_pkts);
393
394 printf("\nTX stats:\n");
395 printf(" - Pkts deqd from tx ring: %"PRIu64"\n",
396 app_stats.tx.dequeue_pkts);
397 printf(" - Ro Pkts transmitted: %"PRIu64"\n",
398 app_stats.tx.ro_tx_pkts);
399 printf(" - Ro Pkts tx failed: %"PRIu64"\n",
400 app_stats.tx.ro_tx_failed_pkts);
401 printf(" - Pkts transmitted w/o reorder: %"PRIu64"\n",
402 app_stats.tx.early_pkts_txtd_woro);
403 printf(" - Pkts tx failed w/o reorder: %"PRIu64"\n",
404 app_stats.tx.early_pkts_tx_failed_woro);
405
406 RTE_ETH_FOREACH_DEV(i) {
407 rte_eth_stats_get(i, ð_stats);
408 printf("\nPort %u stats:\n", i);
409 printf(" - Pkts in: %"PRIu64"\n", eth_stats.ipackets);
410 printf(" - Pkts out: %"PRIu64"\n", eth_stats.opackets);
411 printf(" - In Errs: %"PRIu64"\n", eth_stats.ierrors);
412 printf(" - Out Errs: %"PRIu64"\n", eth_stats.oerrors);
413 printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
414 }
415 }
416
417 static void
int_handler(int sig_num)418 int_handler(int sig_num)
419 {
420 printf("Exiting on signal %d\n", sig_num);
421 quit_signal = 1;
422 }
423
424 /**
425 * This thread receives mbufs from the port and affects them an internal
426 * sequence number to keep track of their order of arrival through an
427 * mbuf structure.
428 * The mbufs are then passed to the worker threads via the rx_to_workers
429 * ring.
430 */
431 static __rte_always_inline int
rx_thread(struct rte_ring * ring_out,bool disable_reorder_flag)432 rx_thread(struct rte_ring *ring_out, bool disable_reorder_flag)
433 {
434 uint32_t seqn = 0;
435 uint16_t i, ret = 0;
436 uint16_t nb_rx_pkts;
437 uint16_t port_id;
438 struct rte_mbuf *pkts[MAX_PKTS_BURST];
439
440 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
441 rte_lcore_id());
442
443 while (!quit_signal) {
444
445 RTE_ETH_FOREACH_DEV(port_id) {
446 if ((portmask & (1 << port_id)) != 0) {
447
448 /* receive packets */
449 nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
450 pkts, MAX_PKTS_BURST);
451 if (nb_rx_pkts == 0) {
452 RTE_LOG_DP(DEBUG, REORDERAPP,
453 "%s():Received zero packets\n", __func__);
454 continue;
455 }
456 app_stats.rx.rx_pkts += nb_rx_pkts;
457
458 /* mark sequence number if reorder is enabled */
459 if (!disable_reorder_flag) {
460 for (i = 0; i < nb_rx_pkts;)
461 *rte_reorder_seqn(pkts[i++]) = seqn++;
462 }
463
464 /* enqueue to rx_to_workers ring */
465 ret = rte_ring_enqueue_burst(ring_out,
466 (void *)pkts, nb_rx_pkts, NULL);
467 app_stats.rx.enqueue_pkts += ret;
468 if (unlikely(ret < nb_rx_pkts)) {
469 app_stats.rx.enqueue_failed_pkts +=
470 (nb_rx_pkts-ret);
471 pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
472 }
473 }
474 }
475 }
476 return 0;
477 }
478
479 static __rte_noinline int
rx_thread_reorder(struct rte_ring * ring_out)480 rx_thread_reorder(struct rte_ring *ring_out)
481 {
482 return rx_thread(ring_out, false);
483 }
484
485 static __rte_noinline int
rx_thread_reorder_disabled(struct rte_ring * ring_out)486 rx_thread_reorder_disabled(struct rte_ring *ring_out)
487 {
488 return rx_thread(ring_out, true);
489 }
490
491 /**
492 * This thread takes bursts of packets from the rx_to_workers ring and
493 * Changes the input port value to output port value. And feds it to
494 * workers_to_tx
495 */
496 static int
worker_thread(void * args_ptr)497 worker_thread(void *args_ptr)
498 {
499 const uint16_t nb_ports = rte_eth_dev_count_avail();
500 uint16_t i, ret = 0;
501 uint16_t burst_size = 0;
502 struct worker_thread_args *args;
503 struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
504 struct rte_ring *ring_in, *ring_out;
505 const unsigned xor_val = (nb_ports > 1);
506 unsigned int core_id = rte_lcore_id();
507
508 args = (struct worker_thread_args *) args_ptr;
509 ring_in = args->ring_in;
510 ring_out = args->ring_out;
511
512 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
513 core_id);
514
515 while (!quit_signal) {
516
517 /* dequeue the mbufs from rx_to_workers ring */
518 burst_size = rte_ring_dequeue_burst(ring_in,
519 (void *)burst_buffer, MAX_PKTS_BURST, NULL);
520 if (unlikely(burst_size == 0))
521 continue;
522
523 wkr_stats[core_id].deq_pkts += burst_size;
524
525 /* just do some operation on mbuf */
526 for (i = 0; i < burst_size;)
527 burst_buffer[i++]->port ^= xor_val;
528
529 /* enqueue the modified mbufs to workers_to_tx ring */
530 ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer,
531 burst_size, NULL);
532 wkr_stats[core_id].enq_pkts += ret;
533 if (unlikely(ret < burst_size)) {
534 /* Return the mbufs to their respective pool, dropping packets */
535 wkr_stats[core_id].enq_failed_pkts += burst_size - ret;
536 pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
537 }
538 }
539 return 0;
540 }
541
542 /**
543 * Dequeue mbufs from the workers_to_tx ring and reorder them before
544 * transmitting.
545 */
546 static int
send_thread(struct send_thread_args * args)547 send_thread(struct send_thread_args *args)
548 {
549 int ret;
550 unsigned int i, dret;
551 uint16_t nb_dq_mbufs;
552 uint8_t outp;
553 unsigned sent;
554 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
555 struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
556 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
557
558 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
559
560 configure_tx_buffers(tx_buffer);
561
562 while (!quit_signal) {
563
564 /* deque the mbufs from workers_to_tx ring */
565 nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
566 (void *)mbufs, MAX_PKTS_BURST, NULL);
567
568 if (unlikely(nb_dq_mbufs == 0))
569 continue;
570
571 app_stats.tx.dequeue_pkts += nb_dq_mbufs;
572
573 for (i = 0; i < nb_dq_mbufs; i++) {
574 /* send dequeued mbufs for reordering */
575 ret = rte_reorder_insert(args->buffer, mbufs[i]);
576
577 if (ret == -1 && rte_errno == ERANGE) {
578 /* Too early pkts should be transmitted out directly */
579 RTE_LOG_DP(DEBUG, REORDERAPP,
580 "%s():Cannot reorder early packet "
581 "direct enqueuing to TX\n", __func__);
582 outp = mbufs[i]->port;
583 if ((portmask & (1 << outp)) == 0) {
584 rte_pktmbuf_free(mbufs[i]);
585 continue;
586 }
587 if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
588 rte_pktmbuf_free(mbufs[i]);
589 app_stats.tx.early_pkts_tx_failed_woro++;
590 } else
591 app_stats.tx.early_pkts_txtd_woro++;
592 } else if (ret == -1 && rte_errno == ENOSPC) {
593 /**
594 * Early pkts just outside of window should be dropped
595 */
596 rte_pktmbuf_free(mbufs[i]);
597 }
598 }
599
600 /*
601 * drain MAX_PKTS_BURST of reordered
602 * mbufs for transmit
603 */
604 dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
605 for (i = 0; i < dret; i++) {
606
607 struct rte_eth_dev_tx_buffer *outbuf;
608 uint8_t outp1;
609
610 outp1 = rombufs[i]->port;
611 /* skip ports that are not enabled */
612 if ((portmask & (1 << outp1)) == 0) {
613 rte_pktmbuf_free(rombufs[i]);
614 continue;
615 }
616
617 outbuf = tx_buffer[outp1];
618 sent = rte_eth_tx_buffer(outp1, 0, outbuf, rombufs[i]);
619 if (sent)
620 app_stats.tx.ro_tx_pkts += sent;
621 }
622 }
623
624 free_tx_buffers(tx_buffer);
625
626 return 0;
627 }
628
629 /**
630 * Dequeue mbufs from the workers_to_tx ring and transmit them
631 */
632 static int
tx_thread(struct rte_ring * ring_in)633 tx_thread(struct rte_ring *ring_in)
634 {
635 uint32_t i, dqnum;
636 uint8_t outp;
637 unsigned sent;
638 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
639 struct rte_eth_dev_tx_buffer *outbuf;
640 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
641
642 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
643 rte_lcore_id());
644
645 configure_tx_buffers(tx_buffer);
646
647 while (!quit_signal) {
648
649 /* deque the mbufs from workers_to_tx ring */
650 dqnum = rte_ring_dequeue_burst(ring_in,
651 (void *)mbufs, MAX_PKTS_BURST, NULL);
652
653 if (unlikely(dqnum == 0))
654 continue;
655
656 app_stats.tx.dequeue_pkts += dqnum;
657
658 for (i = 0; i < dqnum; i++) {
659 outp = mbufs[i]->port;
660 /* skip ports that are not enabled */
661 if ((portmask & (1 << outp)) == 0) {
662 rte_pktmbuf_free(mbufs[i]);
663 continue;
664 }
665
666 outbuf = tx_buffer[outp];
667 sent = rte_eth_tx_buffer(outp, 0, outbuf, mbufs[i]);
668 if (sent)
669 app_stats.tx.ro_tx_pkts += sent;
670 }
671 }
672
673 return 0;
674 }
675
676 int
main(int argc,char ** argv)677 main(int argc, char **argv)
678 {
679 int ret;
680 unsigned nb_ports;
681 unsigned int lcore_id, last_lcore_id, main_lcore_id;
682 uint16_t port_id;
683 uint16_t nb_ports_available;
684 struct worker_thread_args worker_args = {NULL, NULL};
685 struct send_thread_args send_args = {NULL, NULL};
686 struct rte_ring *rx_to_workers;
687 struct rte_ring *workers_to_tx;
688
689 /* catch ctrl-c so we can print on exit */
690 signal(SIGINT, int_handler);
691
692 /* Initialize EAL */
693 ret = rte_eal_init(argc, argv);
694 if (ret < 0)
695 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
696
697 argc -= ret;
698 argv += ret;
699
700 /* Parse the application specific arguments */
701 ret = parse_args(argc, argv);
702 if (ret < 0)
703 rte_exit(EXIT_FAILURE, "Invalid packet_ordering arguments\n");
704
705 /* Check if we have enough cores */
706 if (rte_lcore_count() < 3)
707 rte_exit(EXIT_FAILURE, "Error, This application needs at "
708 "least 3 logical cores to run:\n"
709 "1 lcore for packet RX\n"
710 "1 lcore for packet TX\n"
711 "and at least 1 lcore for worker threads\n");
712
713 nb_ports = rte_eth_dev_count_avail();
714 if (nb_ports == 0)
715 rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
716 if (nb_ports != 1 && (nb_ports & 1))
717 rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
718 "when using a single port\n");
719
720 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
721 MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
722 rte_socket_id());
723 if (mbuf_pool == NULL)
724 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
725
726 nb_ports_available = nb_ports;
727
728 /* initialize all ports */
729 RTE_ETH_FOREACH_DEV(port_id) {
730 /* skip ports that are not enabled */
731 if ((portmask & (1 << port_id)) == 0) {
732 printf("\nSkipping disabled port %d\n", port_id);
733 nb_ports_available--;
734 continue;
735 }
736 /* init port */
737 printf("Initializing port %u... done\n", port_id);
738
739 if (configure_eth_port(port_id) != 0)
740 rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
741 port_id);
742 }
743
744 if (!nb_ports_available) {
745 rte_exit(EXIT_FAILURE,
746 "All available ports are disabled. Please set portmask.\n");
747 }
748
749 /* Create rings for inter core communication */
750 rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
751 RING_F_SP_ENQ);
752 if (rx_to_workers == NULL)
753 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
754
755 workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
756 RING_F_SC_DEQ);
757 if (workers_to_tx == NULL)
758 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
759
760 if (!disable_reorder) {
761 send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
762 REORDER_BUFFER_SIZE);
763 if (send_args.buffer == NULL)
764 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
765 }
766
767 last_lcore_id = get_last_lcore_id();
768 main_lcore_id = rte_get_main_lcore();
769
770 worker_args.ring_in = rx_to_workers;
771 worker_args.ring_out = workers_to_tx;
772
773 /* Start worker_thread() on all the available worker cores but the last 1 */
774 for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
775 if (rte_lcore_is_enabled(lcore_id) && lcore_id != main_lcore_id)
776 rte_eal_remote_launch(worker_thread, (void *)&worker_args,
777 lcore_id);
778
779 if (disable_reorder) {
780 /* Start tx_thread() on the last worker core */
781 rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
782 last_lcore_id);
783 } else {
784 send_args.ring_in = workers_to_tx;
785 /* Start send_thread() on the last worker core */
786 rte_eal_remote_launch((lcore_function_t *)send_thread,
787 (void *)&send_args, last_lcore_id);
788 }
789
790 /* Start rx_thread_xxx() on the main core */
791 if (disable_reorder)
792 rx_thread_reorder_disabled(rx_to_workers);
793 else
794 rx_thread_reorder(rx_to_workers);
795
796 RTE_LCORE_FOREACH_WORKER(lcore_id) {
797 if (rte_eal_wait_lcore(lcore_id) < 0)
798 return -1;
799 }
800
801 print_stats();
802
803 /* clean up the EAL */
804 rte_eal_cleanup();
805
806 return 0;
807 }
808