4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include <sys/types.h>
40 #include <sys/queue.h>
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
51 #include <rte_launch.h>
52 #include <rte_atomic.h>
53 #include <rte_cycles.h>
54 #include <rte_prefetch.h>
55 #include <rte_lcore.h>
56 #include <rte_per_lcore.h>
57 #include <rte_branch_prediction.h>
58 #include <rte_interrupts.h>
59 #include <rte_random.h>
60 #include <rte_debug.h>
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
64 #include <rte_mempool.h>
72 #ifndef APP_LCORE_IO_FLUSH
73 #define APP_LCORE_IO_FLUSH 1000000
76 #ifndef APP_LCORE_WORKER_FLUSH
77 #define APP_LCORE_WORKER_FLUSH 1000000
81 #define APP_STATS 1000000
84 #define APP_IO_RX_DROP_ALL_PACKETS 0
85 #define APP_WORKER_DROP_ALL_PACKETS 0
86 #define APP_IO_TX_DROP_ALL_PACKETS 0
88 #ifndef APP_IO_RX_PREFETCH_ENABLE
89 #define APP_IO_RX_PREFETCH_ENABLE 1
92 #ifndef APP_WORKER_PREFETCH_ENABLE
93 #define APP_WORKER_PREFETCH_ENABLE 1
96 #ifndef APP_IO_TX_PREFETCH_ENABLE
97 #define APP_IO_TX_PREFETCH_ENABLE 1
100 #if APP_IO_RX_PREFETCH_ENABLE
101 #define APP_IO_RX_PREFETCH0(p) rte_prefetch0(p)
102 #define APP_IO_RX_PREFETCH1(p) rte_prefetch1(p)
104 #define APP_IO_RX_PREFETCH0(p)
105 #define APP_IO_RX_PREFETCH1(p)
108 #if APP_WORKER_PREFETCH_ENABLE
109 #define APP_WORKER_PREFETCH0(p) rte_prefetch0(p)
110 #define APP_WORKER_PREFETCH1(p) rte_prefetch1(p)
112 #define APP_WORKER_PREFETCH0(p)
113 #define APP_WORKER_PREFETCH1(p)
116 #if APP_IO_TX_PREFETCH_ENABLE
117 #define APP_IO_TX_PREFETCH0(p) rte_prefetch0(p)
118 #define APP_IO_TX_PREFETCH1(p) rte_prefetch1(p)
120 #define APP_IO_TX_PREFETCH0(p)
121 #define APP_IO_TX_PREFETCH1(p)
125 app_lcore_io_rx_buffer_to_send (
126 struct app_lcore_params_io *lp,
128 struct rte_mbuf *mbuf,
134 pos = lp->rx.mbuf_out[worker].n_mbufs;
135 lp->rx.mbuf_out[worker].array[pos ++] = mbuf;
136 if (likely(pos < bsz)) {
137 lp->rx.mbuf_out[worker].n_mbufs = pos;
141 ret = rte_ring_sp_enqueue_bulk(
142 lp->rx.rings[worker],
143 (void **) lp->rx.mbuf_out[worker].array,
147 if (unlikely(ret == 0)) {
149 for (k = 0; k < bsz; k ++) {
150 struct rte_mbuf *m = lp->rx.mbuf_out[worker].array[k];
155 lp->rx.mbuf_out[worker].n_mbufs = 0;
156 lp->rx.mbuf_out_flush[worker] = 0;
159 lp->rx.rings_iters[worker] ++;
160 if (likely(ret == 0)) {
161 lp->rx.rings_count[worker] ++;
163 if (unlikely(lp->rx.rings_iters[worker] == APP_STATS)) {
164 unsigned lcore = rte_lcore_id();
166 printf("\tI/O RX %u out (worker %u): enq success rate = %.2f\n",
169 ((double) lp->rx.rings_count[worker]) / ((double) lp->rx.rings_iters[worker]));
170 lp->rx.rings_iters[worker] = 0;
171 lp->rx.rings_count[worker] = 0;
178 struct app_lcore_params_io *lp,
184 struct rte_mbuf *mbuf_1_0, *mbuf_1_1, *mbuf_2_0, *mbuf_2_1;
185 uint8_t *data_1_0, *data_1_1 = NULL;
188 for (i = 0; i < lp->rx.n_nic_queues; i ++) {
189 uint16_t port = lp->rx.nic_queues[i].port;
190 uint8_t queue = lp->rx.nic_queues[i].queue;
193 n_mbufs = rte_eth_rx_burst(
196 lp->rx.mbuf_in.array,
199 if (unlikely(n_mbufs == 0)) {
204 lp->rx.nic_queues_iters[i] ++;
205 lp->rx.nic_queues_count[i] += n_mbufs;
206 if (unlikely(lp->rx.nic_queues_iters[i] == APP_STATS)) {
207 struct rte_eth_stats stats;
208 unsigned lcore = rte_lcore_id();
210 rte_eth_stats_get(port, &stats);
212 printf("I/O RX %u in (NIC port %u): NIC drop ratio = %.2f avg burst size = %.2f\n",
215 (double) stats.imissed / (double) (stats.imissed + stats.ipackets),
216 ((double) lp->rx.nic_queues_count[i]) / ((double) lp->rx.nic_queues_iters[i]));
217 lp->rx.nic_queues_iters[i] = 0;
218 lp->rx.nic_queues_count[i] = 0;
222 #if APP_IO_RX_DROP_ALL_PACKETS
223 for (j = 0; j < n_mbufs; j ++) {
224 struct rte_mbuf *pkt = lp->rx.mbuf_in.array[j];
225 rte_pktmbuf_free(pkt);
231 mbuf_1_0 = lp->rx.mbuf_in.array[0];
232 mbuf_1_1 = lp->rx.mbuf_in.array[1];
233 data_1_0 = rte_pktmbuf_mtod(mbuf_1_0, uint8_t *);
234 if (likely(n_mbufs > 1)) {
235 data_1_1 = rte_pktmbuf_mtod(mbuf_1_1, uint8_t *);
238 mbuf_2_0 = lp->rx.mbuf_in.array[2];
239 mbuf_2_1 = lp->rx.mbuf_in.array[3];
240 APP_IO_RX_PREFETCH0(mbuf_2_0);
241 APP_IO_RX_PREFETCH0(mbuf_2_1);
243 for (j = 0; j + 3 < n_mbufs; j += 2) {
244 struct rte_mbuf *mbuf_0_0, *mbuf_0_1;
245 uint8_t *data_0_0, *data_0_1;
246 uint32_t worker_0, worker_1;
255 data_1_0 = rte_pktmbuf_mtod(mbuf_2_0, uint8_t *);
256 data_1_1 = rte_pktmbuf_mtod(mbuf_2_1, uint8_t *);
257 APP_IO_RX_PREFETCH0(data_1_0);
258 APP_IO_RX_PREFETCH0(data_1_1);
260 mbuf_2_0 = lp->rx.mbuf_in.array[j+4];
261 mbuf_2_1 = lp->rx.mbuf_in.array[j+5];
262 APP_IO_RX_PREFETCH0(mbuf_2_0);
263 APP_IO_RX_PREFETCH0(mbuf_2_1);
265 worker_0 = data_0_0[pos_lb] & (n_workers - 1);
266 worker_1 = data_0_1[pos_lb] & (n_workers - 1);
268 app_lcore_io_rx_buffer_to_send(lp, worker_0, mbuf_0_0, bsz_wr);
269 app_lcore_io_rx_buffer_to_send(lp, worker_1, mbuf_0_1, bsz_wr);
272 /* Handle the last 1, 2 (when n_mbufs is even) or 3 (when n_mbufs is odd) packets */
273 for ( ; j < n_mbufs; j += 1) {
274 struct rte_mbuf *mbuf;
283 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
285 APP_IO_RX_PREFETCH0(mbuf_1_0);
287 worker = data[pos_lb] & (n_workers - 1);
289 app_lcore_io_rx_buffer_to_send(lp, worker, mbuf, bsz_wr);
295 app_lcore_io_rx_flush(struct app_lcore_params_io *lp, uint32_t n_workers)
299 for (worker = 0; worker < n_workers; worker ++) {
302 if (likely((lp->rx.mbuf_out_flush[worker] == 0) ||
303 (lp->rx.mbuf_out[worker].n_mbufs == 0))) {
304 lp->rx.mbuf_out_flush[worker] = 1;
308 ret = rte_ring_sp_enqueue_bulk(
309 lp->rx.rings[worker],
310 (void **) lp->rx.mbuf_out[worker].array,
311 lp->rx.mbuf_out[worker].n_mbufs,
314 if (unlikely(ret == 0)) {
316 for (k = 0; k < lp->rx.mbuf_out[worker].n_mbufs; k ++) {
317 struct rte_mbuf *pkt_to_free = lp->rx.mbuf_out[worker].array[k];
318 rte_pktmbuf_free(pkt_to_free);
322 lp->rx.mbuf_out[worker].n_mbufs = 0;
323 lp->rx.mbuf_out_flush[worker] = 1;
329 struct app_lcore_params_io *lp,
336 for (worker = 0; worker < n_workers; worker ++) {
339 for (i = 0; i < lp->tx.n_nic_ports; i ++) {
340 uint16_t port = lp->tx.nic_ports[i];
341 struct rte_ring *ring = lp->tx.rings[port][worker];
342 uint32_t n_mbufs, n_pkts;
345 n_mbufs = lp->tx.mbuf_out[port].n_mbufs;
346 ret = rte_ring_sc_dequeue_bulk(
348 (void **) &lp->tx.mbuf_out[port].array[n_mbufs],
352 if (unlikely(ret == 0))
357 #if APP_IO_TX_DROP_ALL_PACKETS
360 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[0]);
361 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[1]);
363 for (j = 0; j < n_mbufs; j ++) {
364 if (likely(j < n_mbufs - 2)) {
365 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[j + 2]);
368 rte_pktmbuf_free(lp->tx.mbuf_out[port].array[j]);
371 lp->tx.mbuf_out[port].n_mbufs = 0;
377 if (unlikely(n_mbufs < bsz_wr)) {
378 lp->tx.mbuf_out[port].n_mbufs = n_mbufs;
382 n_pkts = rte_eth_tx_burst(
385 lp->tx.mbuf_out[port].array,
389 lp->tx.nic_ports_iters[port] ++;
390 lp->tx.nic_ports_count[port] += n_pkts;
391 if (unlikely(lp->tx.nic_ports_iters[port] == APP_STATS)) {
392 unsigned lcore = rte_lcore_id();
394 printf("\t\t\tI/O TX %u out (port %u): avg burst size = %.2f\n",
397 ((double) lp->tx.nic_ports_count[port]) / ((double) lp->tx.nic_ports_iters[port]));
398 lp->tx.nic_ports_iters[port] = 0;
399 lp->tx.nic_ports_count[port] = 0;
403 if (unlikely(n_pkts < n_mbufs)) {
405 for (k = n_pkts; k < n_mbufs; k ++) {
406 struct rte_mbuf *pkt_to_free = lp->tx.mbuf_out[port].array[k];
407 rte_pktmbuf_free(pkt_to_free);
410 lp->tx.mbuf_out[port].n_mbufs = 0;
411 lp->tx.mbuf_out_flush[port] = 0;
417 app_lcore_io_tx_flush(struct app_lcore_params_io *lp)
422 for (i = 0; i < lp->tx.n_nic_ports; i++) {
425 port = lp->tx.nic_ports[i];
426 if (likely((lp->tx.mbuf_out_flush[port] == 0) ||
427 (lp->tx.mbuf_out[port].n_mbufs == 0))) {
428 lp->tx.mbuf_out_flush[port] = 1;
432 n_pkts = rte_eth_tx_burst(
435 lp->tx.mbuf_out[port].array,
436 (uint16_t) lp->tx.mbuf_out[port].n_mbufs);
438 if (unlikely(n_pkts < lp->tx.mbuf_out[port].n_mbufs)) {
440 for (k = n_pkts; k < lp->tx.mbuf_out[port].n_mbufs; k ++) {
441 struct rte_mbuf *pkt_to_free = lp->tx.mbuf_out[port].array[k];
442 rte_pktmbuf_free(pkt_to_free);
446 lp->tx.mbuf_out[port].n_mbufs = 0;
447 lp->tx.mbuf_out_flush[port] = 1;
452 app_lcore_main_loop_io(void)
454 uint32_t lcore = rte_lcore_id();
455 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
456 uint32_t n_workers = app_get_lcores_worker();
459 uint32_t bsz_rx_rd = app.burst_size_io_rx_read;
460 uint32_t bsz_rx_wr = app.burst_size_io_rx_write;
461 uint32_t bsz_tx_rd = app.burst_size_io_tx_read;
462 uint32_t bsz_tx_wr = app.burst_size_io_tx_write;
464 uint8_t pos_lb = app.pos_lb;
467 if (APP_LCORE_IO_FLUSH && (unlikely(i == APP_LCORE_IO_FLUSH))) {
468 if (likely(lp->rx.n_nic_queues > 0)) {
469 app_lcore_io_rx_flush(lp, n_workers);
472 if (likely(lp->tx.n_nic_ports > 0)) {
473 app_lcore_io_tx_flush(lp);
479 if (likely(lp->rx.n_nic_queues > 0)) {
480 app_lcore_io_rx(lp, n_workers, bsz_rx_rd, bsz_rx_wr, pos_lb);
483 if (likely(lp->tx.n_nic_ports > 0)) {
484 app_lcore_io_tx(lp, n_workers, bsz_tx_rd, bsz_tx_wr);
493 struct app_lcore_params_worker *lp,
499 for (i = 0; i < lp->n_rings_in; i ++) {
500 struct rte_ring *ring_in = lp->rings_in[i];
504 ret = rte_ring_sc_dequeue_bulk(
506 (void **) lp->mbuf_in.array,
510 if (unlikely(ret == 0))
513 #if APP_WORKER_DROP_ALL_PACKETS
514 for (j = 0; j < bsz_rd; j ++) {
515 struct rte_mbuf *pkt = lp->mbuf_in.array[j];
516 rte_pktmbuf_free(pkt);
522 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[0], unsigned char *));
523 APP_WORKER_PREFETCH0(lp->mbuf_in.array[1]);
525 for (j = 0; j < bsz_rd; j ++) {
526 struct rte_mbuf *pkt;
527 struct ipv4_hdr *ipv4_hdr;
528 uint32_t ipv4_dst, pos;
531 if (likely(j < bsz_rd - 1)) {
532 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[j+1], unsigned char *));
534 if (likely(j < bsz_rd - 2)) {
535 APP_WORKER_PREFETCH0(lp->mbuf_in.array[j+2]);
538 pkt = lp->mbuf_in.array[j];
539 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt,
541 sizeof(struct ether_hdr));
542 ipv4_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
544 if (unlikely(rte_lpm_lookup(lp->lpm_table, ipv4_dst, &port) != 0)) {
548 pos = lp->mbuf_out[port].n_mbufs;
550 lp->mbuf_out[port].array[pos ++] = pkt;
551 if (likely(pos < bsz_wr)) {
552 lp->mbuf_out[port].n_mbufs = pos;
556 ret = rte_ring_sp_enqueue_bulk(
558 (void **) lp->mbuf_out[port].array,
563 lp->rings_out_iters[port] ++;
565 lp->rings_out_count[port] += 1;
567 if (lp->rings_out_iters[port] == APP_STATS){
568 printf("\t\tWorker %u out (NIC port %u): enq success rate = %.2f\n",
569 (unsigned) lp->worker_id,
571 ((double) lp->rings_out_count[port]) / ((double) lp->rings_out_iters[port]));
572 lp->rings_out_iters[port] = 0;
573 lp->rings_out_count[port] = 0;
577 if (unlikely(ret == 0)) {
579 for (k = 0; k < bsz_wr; k ++) {
580 struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
581 rte_pktmbuf_free(pkt_to_free);
585 lp->mbuf_out[port].n_mbufs = 0;
586 lp->mbuf_out_flush[port] = 0;
592 app_lcore_worker_flush(struct app_lcore_params_worker *lp)
596 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
599 if (unlikely(lp->rings_out[port] == NULL)) {
603 if (likely((lp->mbuf_out_flush[port] == 0) ||
604 (lp->mbuf_out[port].n_mbufs == 0))) {
605 lp->mbuf_out_flush[port] = 1;
609 ret = rte_ring_sp_enqueue_bulk(
611 (void **) lp->mbuf_out[port].array,
612 lp->mbuf_out[port].n_mbufs,
615 if (unlikely(ret == 0)) {
617 for (k = 0; k < lp->mbuf_out[port].n_mbufs; k ++) {
618 struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
619 rte_pktmbuf_free(pkt_to_free);
623 lp->mbuf_out[port].n_mbufs = 0;
624 lp->mbuf_out_flush[port] = 1;
629 app_lcore_main_loop_worker(void) {
630 uint32_t lcore = rte_lcore_id();
631 struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
634 uint32_t bsz_rd = app.burst_size_worker_read;
635 uint32_t bsz_wr = app.burst_size_worker_write;
638 if (APP_LCORE_WORKER_FLUSH && (unlikely(i == APP_LCORE_WORKER_FLUSH))) {
639 app_lcore_worker_flush(lp);
643 app_lcore_worker(lp, bsz_rd, bsz_wr);
650 app_lcore_main_loop(__attribute__((unused)) void *arg)
652 struct app_lcore_params *lp;
655 lcore = rte_lcore_id();
656 lp = &app.lcore_params[lcore];
658 if (lp->type == e_APP_LCORE_IO) {
659 printf("Logical core %u (I/O) main loop.\n", lcore);
660 app_lcore_main_loop_io();
663 if (lp->type == e_APP_LCORE_WORKER) {
664 printf("Logical core %u (worker %u) main loop.\n",
666 (unsigned) lp->worker.worker_id);
667 app_lcore_main_loop_worker();