ethdev: increase port id range
[dpdk.git] / examples / load_balancer / runtime.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memcpy.h>
50 #include <rte_memzone.h>
51 #include <rte_eal.h>
52 #include <rte_launch.h>
53 #include <rte_atomic.h>
54 #include <rte_cycles.h>
55 #include <rte_prefetch.h>
56 #include <rte_lcore.h>
57 #include <rte_per_lcore.h>
58 #include <rte_branch_prediction.h>
59 #include <rte_interrupts.h>
60 #include <rte_pci.h>
61 #include <rte_random.h>
62 #include <rte_debug.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_ring.h>
66 #include <rte_mempool.h>
67 #include <rte_mbuf.h>
68 #include <rte_ip.h>
69 #include <rte_tcp.h>
70 #include <rte_lpm.h>
71
72 #include "main.h"
73
74 #ifndef APP_LCORE_IO_FLUSH
75 #define APP_LCORE_IO_FLUSH           1000000
76 #endif
77
78 #ifndef APP_LCORE_WORKER_FLUSH
79 #define APP_LCORE_WORKER_FLUSH       1000000
80 #endif
81
82 #ifndef APP_STATS
83 #define APP_STATS                    1000000
84 #endif
85
86 #define APP_IO_RX_DROP_ALL_PACKETS   0
87 #define APP_WORKER_DROP_ALL_PACKETS  0
88 #define APP_IO_TX_DROP_ALL_PACKETS   0
89
90 #ifndef APP_IO_RX_PREFETCH_ENABLE
91 #define APP_IO_RX_PREFETCH_ENABLE    1
92 #endif
93
94 #ifndef APP_WORKER_PREFETCH_ENABLE
95 #define APP_WORKER_PREFETCH_ENABLE   1
96 #endif
97
98 #ifndef APP_IO_TX_PREFETCH_ENABLE
99 #define APP_IO_TX_PREFETCH_ENABLE    1
100 #endif
101
102 #if APP_IO_RX_PREFETCH_ENABLE
103 #define APP_IO_RX_PREFETCH0(p)       rte_prefetch0(p)
104 #define APP_IO_RX_PREFETCH1(p)       rte_prefetch1(p)
105 #else
106 #define APP_IO_RX_PREFETCH0(p)
107 #define APP_IO_RX_PREFETCH1(p)
108 #endif
109
110 #if APP_WORKER_PREFETCH_ENABLE
111 #define APP_WORKER_PREFETCH0(p)      rte_prefetch0(p)
112 #define APP_WORKER_PREFETCH1(p)      rte_prefetch1(p)
113 #else
114 #define APP_WORKER_PREFETCH0(p)
115 #define APP_WORKER_PREFETCH1(p)
116 #endif
117
118 #if APP_IO_TX_PREFETCH_ENABLE
119 #define APP_IO_TX_PREFETCH0(p)       rte_prefetch0(p)
120 #define APP_IO_TX_PREFETCH1(p)       rte_prefetch1(p)
121 #else
122 #define APP_IO_TX_PREFETCH0(p)
123 #define APP_IO_TX_PREFETCH1(p)
124 #endif
125
126 static inline void
127 app_lcore_io_rx_buffer_to_send (
128         struct app_lcore_params_io *lp,
129         uint32_t worker,
130         struct rte_mbuf *mbuf,
131         uint32_t bsz)
132 {
133         uint32_t pos;
134         int ret;
135
136         pos = lp->rx.mbuf_out[worker].n_mbufs;
137         lp->rx.mbuf_out[worker].array[pos ++] = mbuf;
138         if (likely(pos < bsz)) {
139                 lp->rx.mbuf_out[worker].n_mbufs = pos;
140                 return;
141         }
142
143         ret = rte_ring_sp_enqueue_bulk(
144                 lp->rx.rings[worker],
145                 (void **) lp->rx.mbuf_out[worker].array,
146                 bsz,
147                 NULL);
148
149         if (unlikely(ret == 0)) {
150                 uint32_t k;
151                 for (k = 0; k < bsz; k ++) {
152                         struct rte_mbuf *m = lp->rx.mbuf_out[worker].array[k];
153                         rte_pktmbuf_free(m);
154                 }
155         }
156
157         lp->rx.mbuf_out[worker].n_mbufs = 0;
158         lp->rx.mbuf_out_flush[worker] = 0;
159
160 #if APP_STATS
161         lp->rx.rings_iters[worker] ++;
162         if (likely(ret == 0)) {
163                 lp->rx.rings_count[worker] ++;
164         }
165         if (unlikely(lp->rx.rings_iters[worker] == APP_STATS)) {
166                 unsigned lcore = rte_lcore_id();
167
168                 printf("\tI/O RX %u out (worker %u): enq success rate = %.2f\n",
169                         lcore,
170                         (unsigned)worker,
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;
174         }
175 #endif
176 }
177
178 static inline void
179 app_lcore_io_rx(
180         struct app_lcore_params_io *lp,
181         uint32_t n_workers,
182         uint32_t bsz_rd,
183         uint32_t bsz_wr,
184         uint8_t pos_lb)
185 {
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;
188         uint32_t i;
189
190         for (i = 0; i < lp->rx.n_nic_queues; i ++) {
191                 uint16_t port = lp->rx.nic_queues[i].port;
192                 uint8_t queue = lp->rx.nic_queues[i].queue;
193                 uint32_t n_mbufs, j;
194
195                 n_mbufs = rte_eth_rx_burst(
196                         port,
197                         queue,
198                         lp->rx.mbuf_in.array,
199                         (uint16_t) bsz_rd);
200
201                 if (unlikely(n_mbufs == 0)) {
202                         continue;
203                 }
204
205 #if APP_STATS
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();
211
212                         rte_eth_stats_get(port, &stats);
213
214                         printf("I/O RX %u in (NIC port %u): NIC drop ratio = %.2f avg burst size = %.2f\n",
215                                 lcore,
216                                 port,
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;
221                 }
222 #endif
223
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);
228                 }
229
230                 continue;
231 #endif
232
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 *);
238                 }
239
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);
244
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;
249
250                         mbuf_0_0 = mbuf_1_0;
251                         mbuf_0_1 = mbuf_1_1;
252                         data_0_0 = data_1_0;
253                         data_0_1 = data_1_1;
254
255                         mbuf_1_0 = mbuf_2_0;
256                         mbuf_1_1 = mbuf_2_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);
261
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);
266
267                         worker_0 = data_0_0[pos_lb] & (n_workers - 1);
268                         worker_1 = data_0_1[pos_lb] & (n_workers - 1);
269
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);
272                 }
273
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;
277                         uint8_t *data;
278                         uint32_t worker;
279
280                         mbuf = mbuf_1_0;
281                         mbuf_1_0 = mbuf_1_1;
282                         mbuf_1_1 = mbuf_2_0;
283                         mbuf_2_0 = mbuf_2_1;
284
285                         data = rte_pktmbuf_mtod(mbuf, uint8_t *);
286
287                         APP_IO_RX_PREFETCH0(mbuf_1_0);
288
289                         worker = data[pos_lb] & (n_workers - 1);
290
291                         app_lcore_io_rx_buffer_to_send(lp, worker, mbuf, bsz_wr);
292                 }
293         }
294 }
295
296 static inline void
297 app_lcore_io_rx_flush(struct app_lcore_params_io *lp, uint32_t n_workers)
298 {
299         uint32_t worker;
300
301         for (worker = 0; worker < n_workers; worker ++) {
302                 int ret;
303
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;
307                         continue;
308                 }
309
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,
314                         NULL);
315
316                 if (unlikely(ret == 0)) {
317                         uint32_t k;
318                         for (k = 0; k < lp->rx.mbuf_out[worker].n_mbufs; k ++) {
319                                 struct rte_mbuf *pkt_to_free = lp->rx.mbuf_out[worker].array[k];
320                                 rte_pktmbuf_free(pkt_to_free);
321                         }
322                 }
323
324                 lp->rx.mbuf_out[worker].n_mbufs = 0;
325                 lp->rx.mbuf_out_flush[worker] = 1;
326         }
327 }
328
329 static inline void
330 app_lcore_io_tx(
331         struct app_lcore_params_io *lp,
332         uint32_t n_workers,
333         uint32_t bsz_rd,
334         uint32_t bsz_wr)
335 {
336         uint32_t worker;
337
338         for (worker = 0; worker < n_workers; worker ++) {
339                 uint32_t i;
340
341                 for (i = 0; i < lp->tx.n_nic_ports; i ++) {
342                         uint16_t port = lp->tx.nic_ports[i];
343                         struct rte_ring *ring = lp->tx.rings[port][worker];
344                         uint32_t n_mbufs, n_pkts;
345                         int ret;
346
347                         n_mbufs = lp->tx.mbuf_out[port].n_mbufs;
348                         ret = rte_ring_sc_dequeue_bulk(
349                                 ring,
350                                 (void **) &lp->tx.mbuf_out[port].array[n_mbufs],
351                                 bsz_rd,
352                                 NULL);
353
354                         if (unlikely(ret == 0))
355                                 continue;
356
357                         n_mbufs += bsz_rd;
358
359 #if APP_IO_TX_DROP_ALL_PACKETS
360                         {
361                                 uint32_t j;
362                                 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[0]);
363                                 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[1]);
364
365                                 for (j = 0; j < n_mbufs; j ++) {
366                                         if (likely(j < n_mbufs - 2)) {
367                                                 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[j + 2]);
368                                         }
369
370                                         rte_pktmbuf_free(lp->tx.mbuf_out[port].array[j]);
371                                 }
372
373                                 lp->tx.mbuf_out[port].n_mbufs = 0;
374
375                                 continue;
376                         }
377 #endif
378
379                         if (unlikely(n_mbufs < bsz_wr)) {
380                                 lp->tx.mbuf_out[port].n_mbufs = n_mbufs;
381                                 continue;
382                         }
383
384                         n_pkts = rte_eth_tx_burst(
385                                 port,
386                                 0,
387                                 lp->tx.mbuf_out[port].array,
388                                 (uint16_t) n_mbufs);
389
390 #if APP_STATS
391                         lp->tx.nic_ports_iters[port] ++;
392                         lp->tx.nic_ports_count[port] += n_pkts;
393                         if (unlikely(lp->tx.nic_ports_iters[port] == APP_STATS)) {
394                                 unsigned lcore = rte_lcore_id();
395
396                                 printf("\t\t\tI/O TX %u out (port %u): avg burst size = %.2f\n",
397                                         lcore,
398                                         port,
399                                         ((double) lp->tx.nic_ports_count[port]) / ((double) lp->tx.nic_ports_iters[port]));
400                                 lp->tx.nic_ports_iters[port] = 0;
401                                 lp->tx.nic_ports_count[port] = 0;
402                         }
403 #endif
404
405                         if (unlikely(n_pkts < n_mbufs)) {
406                                 uint32_t k;
407                                 for (k = n_pkts; k < n_mbufs; k ++) {
408                                         struct rte_mbuf *pkt_to_free = lp->tx.mbuf_out[port].array[k];
409                                         rte_pktmbuf_free(pkt_to_free);
410                                 }
411                         }
412                         lp->tx.mbuf_out[port].n_mbufs = 0;
413                         lp->tx.mbuf_out_flush[port] = 0;
414                 }
415         }
416 }
417
418 static inline void
419 app_lcore_io_tx_flush(struct app_lcore_params_io *lp)
420 {
421         uint16_t port;
422         uint32_t i;
423
424         for (i = 0; i < lp->tx.n_nic_ports; i++) {
425                 uint32_t n_pkts;
426
427                 port = lp->tx.nic_ports[i];
428                 if (likely((lp->tx.mbuf_out_flush[port] == 0) ||
429                            (lp->tx.mbuf_out[port].n_mbufs == 0))) {
430                         lp->tx.mbuf_out_flush[port] = 1;
431                         continue;
432                 }
433
434                 n_pkts = rte_eth_tx_burst(
435                         port,
436                         0,
437                         lp->tx.mbuf_out[port].array,
438                         (uint16_t) lp->tx.mbuf_out[port].n_mbufs);
439
440                 if (unlikely(n_pkts < lp->tx.mbuf_out[port].n_mbufs)) {
441                         uint32_t k;
442                         for (k = n_pkts; k < lp->tx.mbuf_out[port].n_mbufs; k ++) {
443                                 struct rte_mbuf *pkt_to_free = lp->tx.mbuf_out[port].array[k];
444                                 rte_pktmbuf_free(pkt_to_free);
445                         }
446                 }
447
448                 lp->tx.mbuf_out[port].n_mbufs = 0;
449                 lp->tx.mbuf_out_flush[port] = 1;
450         }
451 }
452
453 static void
454 app_lcore_main_loop_io(void)
455 {
456         uint32_t lcore = rte_lcore_id();
457         struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
458         uint32_t n_workers = app_get_lcores_worker();
459         uint64_t i = 0;
460
461         uint32_t bsz_rx_rd = app.burst_size_io_rx_read;
462         uint32_t bsz_rx_wr = app.burst_size_io_rx_write;
463         uint32_t bsz_tx_rd = app.burst_size_io_tx_read;
464         uint32_t bsz_tx_wr = app.burst_size_io_tx_write;
465
466         uint8_t pos_lb = app.pos_lb;
467
468         for ( ; ; ) {
469                 if (APP_LCORE_IO_FLUSH && (unlikely(i == APP_LCORE_IO_FLUSH))) {
470                         if (likely(lp->rx.n_nic_queues > 0)) {
471                                 app_lcore_io_rx_flush(lp, n_workers);
472                         }
473
474                         if (likely(lp->tx.n_nic_ports > 0)) {
475                                 app_lcore_io_tx_flush(lp);
476                         }
477
478                         i = 0;
479                 }
480
481                 if (likely(lp->rx.n_nic_queues > 0)) {
482                         app_lcore_io_rx(lp, n_workers, bsz_rx_rd, bsz_rx_wr, pos_lb);
483                 }
484
485                 if (likely(lp->tx.n_nic_ports > 0)) {
486                         app_lcore_io_tx(lp, n_workers, bsz_tx_rd, bsz_tx_wr);
487                 }
488
489                 i ++;
490         }
491 }
492
493 static inline void
494 app_lcore_worker(
495         struct app_lcore_params_worker *lp,
496         uint32_t bsz_rd,
497         uint32_t bsz_wr)
498 {
499         uint32_t i;
500
501         for (i = 0; i < lp->n_rings_in; i ++) {
502                 struct rte_ring *ring_in = lp->rings_in[i];
503                 uint32_t j;
504                 int ret;
505
506                 ret = rte_ring_sc_dequeue_bulk(
507                         ring_in,
508                         (void **) lp->mbuf_in.array,
509                         bsz_rd,
510                         NULL);
511
512                 if (unlikely(ret == 0))
513                         continue;
514
515 #if APP_WORKER_DROP_ALL_PACKETS
516                 for (j = 0; j < bsz_rd; j ++) {
517                         struct rte_mbuf *pkt = lp->mbuf_in.array[j];
518                         rte_pktmbuf_free(pkt);
519                 }
520
521                 continue;
522 #endif
523
524                 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[0], unsigned char *));
525                 APP_WORKER_PREFETCH0(lp->mbuf_in.array[1]);
526
527                 for (j = 0; j < bsz_rd; j ++) {
528                         struct rte_mbuf *pkt;
529                         struct ipv4_hdr *ipv4_hdr;
530                         uint32_t ipv4_dst, pos;
531                         uint32_t port;
532
533                         if (likely(j < bsz_rd - 1)) {
534                                 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[j+1], unsigned char *));
535                         }
536                         if (likely(j < bsz_rd - 2)) {
537                                 APP_WORKER_PREFETCH0(lp->mbuf_in.array[j+2]);
538                         }
539
540                         pkt = lp->mbuf_in.array[j];
541                         ipv4_hdr = rte_pktmbuf_mtod_offset(pkt,
542                                                            struct ipv4_hdr *,
543                                                            sizeof(struct ether_hdr));
544                         ipv4_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
545
546                         if (unlikely(rte_lpm_lookup(lp->lpm_table, ipv4_dst, &port) != 0)) {
547                                 port = pkt->port;
548                         }
549
550                         pos = lp->mbuf_out[port].n_mbufs;
551
552                         lp->mbuf_out[port].array[pos ++] = pkt;
553                         if (likely(pos < bsz_wr)) {
554                                 lp->mbuf_out[port].n_mbufs = pos;
555                                 continue;
556                         }
557
558                         ret = rte_ring_sp_enqueue_bulk(
559                                 lp->rings_out[port],
560                                 (void **) lp->mbuf_out[port].array,
561                                 bsz_wr,
562                                 NULL);
563
564 #if APP_STATS
565                         lp->rings_out_iters[port] ++;
566                         if (ret > 0) {
567                                 lp->rings_out_count[port] += 1;
568                         }
569                         if (lp->rings_out_iters[port] == APP_STATS){
570                                 printf("\t\tWorker %u out (NIC port %u): enq success rate = %.2f\n",
571                                         (unsigned) lp->worker_id,
572                                         port,
573                                         ((double) lp->rings_out_count[port]) / ((double) lp->rings_out_iters[port]));
574                                 lp->rings_out_iters[port] = 0;
575                                 lp->rings_out_count[port] = 0;
576                         }
577 #endif
578
579                         if (unlikely(ret == 0)) {
580                                 uint32_t k;
581                                 for (k = 0; k < bsz_wr; k ++) {
582                                         struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
583                                         rte_pktmbuf_free(pkt_to_free);
584                                 }
585                         }
586
587                         lp->mbuf_out[port].n_mbufs = 0;
588                         lp->mbuf_out_flush[port] = 0;
589                 }
590         }
591 }
592
593 static inline void
594 app_lcore_worker_flush(struct app_lcore_params_worker *lp)
595 {
596         uint32_t port;
597
598         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
599                 int ret;
600
601                 if (unlikely(lp->rings_out[port] == NULL)) {
602                         continue;
603                 }
604
605                 if (likely((lp->mbuf_out_flush[port] == 0) ||
606                            (lp->mbuf_out[port].n_mbufs == 0))) {
607                         lp->mbuf_out_flush[port] = 1;
608                         continue;
609                 }
610
611                 ret = rte_ring_sp_enqueue_bulk(
612                         lp->rings_out[port],
613                         (void **) lp->mbuf_out[port].array,
614                         lp->mbuf_out[port].n_mbufs,
615                         NULL);
616
617                 if (unlikely(ret == 0)) {
618                         uint32_t k;
619                         for (k = 0; k < lp->mbuf_out[port].n_mbufs; k ++) {
620                                 struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
621                                 rte_pktmbuf_free(pkt_to_free);
622                         }
623                 }
624
625                 lp->mbuf_out[port].n_mbufs = 0;
626                 lp->mbuf_out_flush[port] = 1;
627         }
628 }
629
630 static void
631 app_lcore_main_loop_worker(void) {
632         uint32_t lcore = rte_lcore_id();
633         struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
634         uint64_t i = 0;
635
636         uint32_t bsz_rd = app.burst_size_worker_read;
637         uint32_t bsz_wr = app.burst_size_worker_write;
638
639         for ( ; ; ) {
640                 if (APP_LCORE_WORKER_FLUSH && (unlikely(i == APP_LCORE_WORKER_FLUSH))) {
641                         app_lcore_worker_flush(lp);
642                         i = 0;
643                 }
644
645                 app_lcore_worker(lp, bsz_rd, bsz_wr);
646
647                 i ++;
648         }
649 }
650
651 int
652 app_lcore_main_loop(__attribute__((unused)) void *arg)
653 {
654         struct app_lcore_params *lp;
655         unsigned lcore;
656
657         lcore = rte_lcore_id();
658         lp = &app.lcore_params[lcore];
659
660         if (lp->type == e_APP_LCORE_IO) {
661                 printf("Logical core %u (I/O) main loop.\n", lcore);
662                 app_lcore_main_loop_io();
663         }
664
665         if (lp->type == e_APP_LCORE_WORKER) {
666                 printf("Logical core %u (worker %u) main loop.\n",
667                         lcore,
668                         (unsigned) lp->worker.worker_id);
669                 app_lcore_main_loop_worker();
670         }
671
672         return 0;
673 }