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>
50 #include <rte_memzone.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_atomic.h>
55 #include <rte_cycles.h>
56 #include <rte_prefetch.h>
57 #include <rte_lcore.h>
58 #include <rte_per_lcore.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_interrupts.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
67 #include <rte_mempool.h>
75 #ifndef APP_LCORE_IO_FLUSH
76 #define APP_LCORE_IO_FLUSH 1000000
79 #ifndef APP_LCORE_WORKER_FLUSH
80 #define APP_LCORE_WORKER_FLUSH 1000000
84 #define APP_STATS 1000000
87 #define APP_IO_RX_DROP_ALL_PACKETS 0
88 #define APP_WORKER_DROP_ALL_PACKETS 0
89 #define APP_IO_TX_DROP_ALL_PACKETS 0
91 #ifndef APP_IO_RX_PREFETCH_ENABLE
92 #define APP_IO_RX_PREFETCH_ENABLE 1
95 #ifndef APP_WORKER_PREFETCH_ENABLE
96 #define APP_WORKER_PREFETCH_ENABLE 1
99 #ifndef APP_IO_TX_PREFETCH_ENABLE
100 #define APP_IO_TX_PREFETCH_ENABLE 1
103 #if APP_IO_RX_PREFETCH_ENABLE
104 #define APP_IO_RX_PREFETCH0(p) rte_prefetch0(p)
105 #define APP_IO_RX_PREFETCH1(p) rte_prefetch1(p)
107 #define APP_IO_RX_PREFETCH0(p)
108 #define APP_IO_RX_PREFETCH1(p)
111 #if APP_WORKER_PREFETCH_ENABLE
112 #define APP_WORKER_PREFETCH0(p) rte_prefetch0(p)
113 #define APP_WORKER_PREFETCH1(p) rte_prefetch1(p)
115 #define APP_WORKER_PREFETCH0(p)
116 #define APP_WORKER_PREFETCH1(p)
119 #if APP_IO_TX_PREFETCH_ENABLE
120 #define APP_IO_TX_PREFETCH0(p) rte_prefetch0(p)
121 #define APP_IO_TX_PREFETCH1(p) rte_prefetch1(p)
123 #define APP_IO_TX_PREFETCH0(p)
124 #define APP_IO_TX_PREFETCH1(p)
128 app_lcore_io_rx_buffer_to_send (
129 struct app_lcore_params_io *lp,
131 struct rte_mbuf *mbuf,
137 pos = lp->rx.mbuf_out[worker].n_mbufs;
138 lp->rx.mbuf_out[worker].array[pos ++] = mbuf;
139 if (likely(pos < bsz)) {
140 lp->rx.mbuf_out[worker].n_mbufs = pos;
144 ret = rte_ring_sp_enqueue_bulk(
145 lp->rx.rings[worker],
146 (void **) lp->rx.mbuf_out[worker].array,
149 if (unlikely(ret == 0)) {
151 for (k = 0; k < bsz; k ++) {
152 struct rte_mbuf *m = lp->rx.mbuf_out[worker].array[k];
157 lp->rx.mbuf_out[worker].n_mbufs = 0;
158 lp->rx.mbuf_out_flush[worker] = 0;
161 lp->rx.rings_iters[worker] ++;
162 if (likely(ret == 0)) {
163 lp->rx.rings_count[worker] ++;
165 if (unlikely(lp->rx.rings_iters[worker] == APP_STATS)) {
166 unsigned lcore = rte_lcore_id();
168 printf("\tI/O RX %u out (worker %u): enq success rate = %.2f\n",
171 ((double) lp->rx.rings_count[worker]) / ((double) lp->rx.rings_iters[worker]));
172 lp->rx.rings_iters[worker] = 0;
173 lp->rx.rings_count[worker] = 0;
180 struct app_lcore_params_io *lp,
186 struct rte_mbuf *mbuf_1_0, *mbuf_1_1, *mbuf_2_0, *mbuf_2_1;
187 uint8_t *data_1_0, *data_1_1 = NULL;
190 for (i = 0; i < lp->rx.n_nic_queues; i ++) {
191 uint8_t port = lp->rx.nic_queues[i].port;
192 uint8_t queue = lp->rx.nic_queues[i].queue;
195 n_mbufs = rte_eth_rx_burst(
198 lp->rx.mbuf_in.array,
201 if (unlikely(n_mbufs == 0)) {
206 lp->rx.nic_queues_iters[i] ++;
207 lp->rx.nic_queues_count[i] += n_mbufs;
208 if (unlikely(lp->rx.nic_queues_iters[i] == APP_STATS)) {
209 struct rte_eth_stats stats;
210 unsigned lcore = rte_lcore_id();
212 rte_eth_stats_get(port, &stats);
214 printf("I/O RX %u in (NIC port %u): NIC drop ratio = %.2f avg burst size = %.2f\n",
217 (double) stats.imissed / (double) (stats.imissed + stats.ipackets),
218 ((double) lp->rx.nic_queues_count[i]) / ((double) lp->rx.nic_queues_iters[i]));
219 lp->rx.nic_queues_iters[i] = 0;
220 lp->rx.nic_queues_count[i] = 0;
224 #if APP_IO_RX_DROP_ALL_PACKETS
225 for (j = 0; j < n_mbufs; j ++) {
226 struct rte_mbuf *pkt = lp->rx.mbuf_in.array[j];
227 rte_pktmbuf_free(pkt);
233 mbuf_1_0 = lp->rx.mbuf_in.array[0];
234 mbuf_1_1 = lp->rx.mbuf_in.array[1];
235 data_1_0 = rte_pktmbuf_mtod(mbuf_1_0, uint8_t *);
236 if (likely(n_mbufs > 1)) {
237 data_1_1 = rte_pktmbuf_mtod(mbuf_1_1, uint8_t *);
240 mbuf_2_0 = lp->rx.mbuf_in.array[2];
241 mbuf_2_1 = lp->rx.mbuf_in.array[3];
242 APP_IO_RX_PREFETCH0(mbuf_2_0);
243 APP_IO_RX_PREFETCH0(mbuf_2_1);
245 for (j = 0; j + 3 < n_mbufs; j += 2) {
246 struct rte_mbuf *mbuf_0_0, *mbuf_0_1;
247 uint8_t *data_0_0, *data_0_1;
248 uint32_t worker_0, worker_1;
257 data_1_0 = rte_pktmbuf_mtod(mbuf_2_0, uint8_t *);
258 data_1_1 = rte_pktmbuf_mtod(mbuf_2_1, uint8_t *);
259 APP_IO_RX_PREFETCH0(data_1_0);
260 APP_IO_RX_PREFETCH0(data_1_1);
262 mbuf_2_0 = lp->rx.mbuf_in.array[j+4];
263 mbuf_2_1 = lp->rx.mbuf_in.array[j+5];
264 APP_IO_RX_PREFETCH0(mbuf_2_0);
265 APP_IO_RX_PREFETCH0(mbuf_2_1);
267 worker_0 = data_0_0[pos_lb] & (n_workers - 1);
268 worker_1 = data_0_1[pos_lb] & (n_workers - 1);
270 app_lcore_io_rx_buffer_to_send(lp, worker_0, mbuf_0_0, bsz_wr);
271 app_lcore_io_rx_buffer_to_send(lp, worker_1, mbuf_0_1, bsz_wr);
274 /* Handle the last 1, 2 (when n_mbufs is even) or 3 (when n_mbufs is odd) packets */
275 for ( ; j < n_mbufs; j += 1) {
276 struct rte_mbuf *mbuf;
285 data = rte_pktmbuf_mtod(mbuf, uint8_t *);
287 APP_IO_RX_PREFETCH0(mbuf_1_0);
289 worker = data[pos_lb] & (n_workers - 1);
291 app_lcore_io_rx_buffer_to_send(lp, worker, mbuf, bsz_wr);
297 app_lcore_io_rx_flush(struct app_lcore_params_io *lp, uint32_t n_workers)
301 for (worker = 0; worker < n_workers; worker ++) {
304 if (likely((lp->rx.mbuf_out_flush[worker] == 0) ||
305 (lp->rx.mbuf_out[worker].n_mbufs == 0))) {
306 lp->rx.mbuf_out_flush[worker] = 1;
310 ret = rte_ring_sp_enqueue_bulk(
311 lp->rx.rings[worker],
312 (void **) lp->rx.mbuf_out[worker].array,
313 lp->rx.mbuf_out[worker].n_mbufs);
315 if (unlikely(ret == 0)) {
317 for (k = 0; k < lp->rx.mbuf_out[worker].n_mbufs; k ++) {
318 struct rte_mbuf *pkt_to_free = lp->rx.mbuf_out[worker].array[k];
319 rte_pktmbuf_free(pkt_to_free);
323 lp->rx.mbuf_out[worker].n_mbufs = 0;
324 lp->rx.mbuf_out_flush[worker] = 1;
330 struct app_lcore_params_io *lp,
337 for (worker = 0; worker < n_workers; worker ++) {
340 for (i = 0; i < lp->tx.n_nic_ports; i ++) {
341 uint8_t port = lp->tx.nic_ports[i];
342 struct rte_ring *ring = lp->tx.rings[port][worker];
343 uint32_t n_mbufs, n_pkts;
346 n_mbufs = lp->tx.mbuf_out[port].n_mbufs;
347 ret = rte_ring_sc_dequeue_bulk(
349 (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)
421 for (port = 0; port < lp->tx.n_nic_ports; port ++) {
424 if (likely((lp->tx.mbuf_out_flush[port] == 0) ||
425 (lp->tx.mbuf_out[port].n_mbufs == 0))) {
426 lp->tx.mbuf_out_flush[port] = 1;
430 n_pkts = rte_eth_tx_burst(
433 lp->tx.mbuf_out[port].array,
434 (uint16_t) lp->tx.mbuf_out[port].n_mbufs);
436 if (unlikely(n_pkts < lp->tx.mbuf_out[port].n_mbufs)) {
438 for (k = n_pkts; k < lp->tx.mbuf_out[port].n_mbufs; k ++) {
439 struct rte_mbuf *pkt_to_free = lp->tx.mbuf_out[port].array[k];
440 rte_pktmbuf_free(pkt_to_free);
444 lp->tx.mbuf_out[port].n_mbufs = 0;
445 lp->tx.mbuf_out_flush[port] = 1;
450 app_lcore_main_loop_io(void)
452 uint32_t lcore = rte_lcore_id();
453 struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
454 uint32_t n_workers = app_get_lcores_worker();
457 uint32_t bsz_rx_rd = app.burst_size_io_rx_read;
458 uint32_t bsz_rx_wr = app.burst_size_io_rx_write;
459 uint32_t bsz_tx_rd = app.burst_size_io_tx_read;
460 uint32_t bsz_tx_wr = app.burst_size_io_tx_write;
462 uint8_t pos_lb = app.pos_lb;
465 if (APP_LCORE_IO_FLUSH && (unlikely(i == APP_LCORE_IO_FLUSH))) {
466 if (likely(lp->rx.n_nic_queues > 0)) {
467 app_lcore_io_rx_flush(lp, n_workers);
470 if (likely(lp->tx.n_nic_ports > 0)) {
471 app_lcore_io_tx_flush(lp);
477 if (likely(lp->rx.n_nic_queues > 0)) {
478 app_lcore_io_rx(lp, n_workers, bsz_rx_rd, bsz_rx_wr, pos_lb);
481 if (likely(lp->tx.n_nic_ports > 0)) {
482 app_lcore_io_tx(lp, n_workers, bsz_tx_rd, bsz_tx_wr);
491 struct app_lcore_params_worker *lp,
497 for (i = 0; i < lp->n_rings_in; i ++) {
498 struct rte_ring *ring_in = lp->rings_in[i];
502 ret = rte_ring_sc_dequeue_bulk(
504 (void **) lp->mbuf_in.array,
507 if (unlikely(ret == 0))
510 #if APP_WORKER_DROP_ALL_PACKETS
511 for (j = 0; j < bsz_rd; j ++) {
512 struct rte_mbuf *pkt = lp->mbuf_in.array[j];
513 rte_pktmbuf_free(pkt);
519 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[0], unsigned char *));
520 APP_WORKER_PREFETCH0(lp->mbuf_in.array[1]);
522 for (j = 0; j < bsz_rd; j ++) {
523 struct rte_mbuf *pkt;
524 struct ipv4_hdr *ipv4_hdr;
525 uint32_t ipv4_dst, pos;
528 if (likely(j < bsz_rd - 1)) {
529 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[j+1], unsigned char *));
531 if (likely(j < bsz_rd - 2)) {
532 APP_WORKER_PREFETCH0(lp->mbuf_in.array[j+2]);
535 pkt = lp->mbuf_in.array[j];
536 ipv4_hdr = rte_pktmbuf_mtod_offset(pkt,
538 sizeof(struct ether_hdr));
539 ipv4_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
541 if (unlikely(rte_lpm_lookup(lp->lpm_table, ipv4_dst, &port) != 0)) {
545 pos = lp->mbuf_out[port].n_mbufs;
547 lp->mbuf_out[port].array[pos ++] = pkt;
548 if (likely(pos < bsz_wr)) {
549 lp->mbuf_out[port].n_mbufs = pos;
553 ret = rte_ring_sp_enqueue_bulk(
555 (void **) lp->mbuf_out[port].array,
559 lp->rings_out_iters[port] ++;
561 lp->rings_out_count[port] += 1;
563 if (lp->rings_out_iters[port] == APP_STATS){
564 printf("\t\tWorker %u out (NIC port %u): enq success rate = %.2f\n",
565 (unsigned) lp->worker_id,
567 ((double) lp->rings_out_count[port]) / ((double) lp->rings_out_iters[port]));
568 lp->rings_out_iters[port] = 0;
569 lp->rings_out_count[port] = 0;
573 if (unlikely(ret == 0)) {
575 for (k = 0; k < bsz_wr; k ++) {
576 struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
577 rte_pktmbuf_free(pkt_to_free);
581 lp->mbuf_out[port].n_mbufs = 0;
582 lp->mbuf_out_flush[port] = 0;
588 app_lcore_worker_flush(struct app_lcore_params_worker *lp)
592 for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
595 if (unlikely(lp->rings_out[port] == NULL)) {
599 if (likely((lp->mbuf_out_flush[port] == 0) ||
600 (lp->mbuf_out[port].n_mbufs == 0))) {
601 lp->mbuf_out_flush[port] = 1;
605 ret = rte_ring_sp_enqueue_bulk(
607 (void **) lp->mbuf_out[port].array,
608 lp->mbuf_out[port].n_mbufs);
610 if (unlikely(ret == 0)) {
612 for (k = 0; k < lp->mbuf_out[port].n_mbufs; k ++) {
613 struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
614 rte_pktmbuf_free(pkt_to_free);
618 lp->mbuf_out[port].n_mbufs = 0;
619 lp->mbuf_out_flush[port] = 1;
624 app_lcore_main_loop_worker(void) {
625 uint32_t lcore = rte_lcore_id();
626 struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
629 uint32_t bsz_rd = app.burst_size_worker_read;
630 uint32_t bsz_wr = app.burst_size_worker_write;
633 if (APP_LCORE_WORKER_FLUSH && (unlikely(i == APP_LCORE_WORKER_FLUSH))) {
634 app_lcore_worker_flush(lp);
638 app_lcore_worker(lp, bsz_rd, bsz_wr);
645 app_lcore_main_loop(__attribute__((unused)) void *arg)
647 struct app_lcore_params *lp;
650 lcore = rte_lcore_id();
651 lp = &app.lcore_params[lcore];
653 if (lp->type == e_APP_LCORE_IO) {
654 printf("Logical core %u (I/O) main loop.\n", lcore);
655 app_lcore_main_loop_io();
658 if (lp->type == e_APP_LCORE_WORKER) {
659 printf("Logical core %u (worker %u) main loop.\n",
661 (unsigned) lp->worker.worker_id);
662 app_lcore_main_loop_worker();