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