update Intel copyright years to 2014
[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_tailq.h>
52 #include <rte_eal.h>
53 #include <rte_per_lcore.h>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_cycles.h>
57 #include <rte_prefetch.h>
58 #include <rte_lcore.h>
59 #include <rte_per_lcore.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_interrupts.h>
62 #include <rte_pci.h>
63 #include <rte_random.h>
64 #include <rte_debug.h>
65 #include <rte_ether.h>
66 #include <rte_ethdev.h>
67 #include <rte_ring.h>
68 #include <rte_mempool.h>
69 #include <rte_mbuf.h>
70 #include <rte_ip.h>
71 #include <rte_tcp.h>
72 #include <rte_lpm.h>
73
74 #include "main.h"
75
76 #ifndef APP_LCORE_IO_FLUSH
77 #define APP_LCORE_IO_FLUSH           1000000
78 #endif
79
80 #ifndef APP_LCORE_WORKER_FLUSH
81 #define APP_LCORE_WORKER_FLUSH       1000000
82 #endif
83
84 #ifndef APP_STATS
85 #define APP_STATS                    1000000
86 #endif
87
88 #define APP_IO_RX_DROP_ALL_PACKETS   0
89 #define APP_WORKER_DROP_ALL_PACKETS  0
90 #define APP_IO_TX_DROP_ALL_PACKETS   0
91
92 #ifndef APP_IO_RX_PREFETCH_ENABLE
93 #define APP_IO_RX_PREFETCH_ENABLE    1
94 #endif
95
96 #ifndef APP_WORKER_PREFETCH_ENABLE
97 #define APP_WORKER_PREFETCH_ENABLE   1
98 #endif
99
100 #ifndef APP_IO_TX_PREFETCH_ENABLE
101 #define APP_IO_TX_PREFETCH_ENABLE    1
102 #endif
103
104 #if APP_IO_RX_PREFETCH_ENABLE
105 #define APP_IO_RX_PREFETCH0(p)       rte_prefetch0(p)
106 #define APP_IO_RX_PREFETCH1(p)       rte_prefetch1(p)
107 #else
108 #define APP_IO_RX_PREFETCH0(p)
109 #define APP_IO_RX_PREFETCH1(p)
110 #endif
111
112 #if APP_WORKER_PREFETCH_ENABLE
113 #define APP_WORKER_PREFETCH0(p)      rte_prefetch0(p)
114 #define APP_WORKER_PREFETCH1(p)      rte_prefetch1(p)
115 #else
116 #define APP_WORKER_PREFETCH0(p)
117 #define APP_WORKER_PREFETCH1(p)
118 #endif
119
120 #if APP_IO_TX_PREFETCH_ENABLE
121 #define APP_IO_TX_PREFETCH0(p)       rte_prefetch0(p)
122 #define APP_IO_TX_PREFETCH1(p)       rte_prefetch1(p)
123 #else
124 #define APP_IO_TX_PREFETCH0(p)
125 #define APP_IO_TX_PREFETCH1(p)
126 #endif
127
128 static inline void
129 app_lcore_io_rx_buffer_to_send (
130         struct app_lcore_params_io *lp,
131         uint32_t worker,
132         struct rte_mbuf *mbuf,
133         uint32_t bsz)
134 {
135         uint32_t pos;
136         int ret;
137
138         pos = lp->rx.mbuf_out[worker].n_mbufs;
139         lp->rx.mbuf_out[worker].array[pos ++] = mbuf;
140         if (likely(pos < bsz)) {
141                 lp->rx.mbuf_out[worker].n_mbufs = pos;
142                 return;
143         }
144
145         ret = rte_ring_sp_enqueue_bulk(
146                 lp->rx.rings[worker],
147                 (void **) lp->rx.mbuf_out[worker].array,
148                 bsz);
149
150         if (unlikely(ret == -ENOBUFS)) {
151                 uint32_t k;
152                 for (k = 0; k < bsz; k ++) {
153                         struct rte_mbuf *m = lp->rx.mbuf_out[worker].array[k];
154                         rte_pktmbuf_free(m);
155                 }
156         }
157
158         lp->rx.mbuf_out[worker].n_mbufs = 0;
159         lp->rx.mbuf_out_flush[worker] = 0;
160
161 #if APP_STATS
162         lp->rx.rings_iters[worker] ++;
163         if (likely(ret == 0)) {
164                 lp->rx.rings_count[worker] ++;
165         }
166         if (unlikely(lp->rx.rings_iters[worker] == APP_STATS)) {
167                 unsigned lcore = rte_lcore_id();
168
169                 printf("\tI/O RX %u out (worker %u): enq success rate = %.2f\n",
170                         lcore,
171                         (unsigned)worker,
172                         ((double) lp->rx.rings_count[worker]) / ((double) lp->rx.rings_iters[worker]));
173                 lp->rx.rings_iters[worker] = 0;
174                 lp->rx.rings_count[worker] = 0;
175         }
176 #endif
177 }
178
179 static inline void
180 app_lcore_io_rx(
181         struct app_lcore_params_io *lp,
182         uint32_t n_workers,
183         uint32_t bsz_rd,
184         uint32_t bsz_wr,
185         uint8_t pos_lb)
186 {
187         struct rte_mbuf *mbuf_1_0, *mbuf_1_1, *mbuf_2_0, *mbuf_2_1;
188         uint8_t *data_1_0, *data_1_1 = NULL;
189         uint32_t i;
190
191         for (i = 0; i < lp->rx.n_nic_queues; i ++) {
192                 uint8_t port = lp->rx.nic_queues[i].port;
193                 uint8_t queue = lp->rx.nic_queues[i].queue;
194                 uint32_t n_mbufs, j;
195
196                 n_mbufs = rte_eth_rx_burst(
197                         port,
198                         queue,
199                         lp->rx.mbuf_in.array,
200                         (uint16_t) bsz_rd);
201
202                 if (unlikely(n_mbufs == 0)) {
203                         continue;
204                 }
205
206 #if APP_STATS
207                 lp->rx.nic_queues_iters[i] ++;
208                 lp->rx.nic_queues_count[i] += n_mbufs;
209                 if (unlikely(lp->rx.nic_queues_iters[i] == APP_STATS)) {
210                         struct rte_eth_stats stats;
211                         unsigned lcore = rte_lcore_id();
212
213                         rte_eth_stats_get(port, &stats);
214
215                         printf("I/O RX %u in (NIC port %u): NIC drop ratio = %.2f avg burst size = %.2f\n",
216                                 lcore,
217                                 (unsigned) port,
218                                 (double) stats.ierrors / (double) (stats.ierrors + stats.ipackets),
219                                 ((double) lp->rx.nic_queues_count[i]) / ((double) lp->rx.nic_queues_iters[i]));
220                         lp->rx.nic_queues_iters[i] = 0;
221                         lp->rx.nic_queues_count[i] = 0;
222                 }
223 #endif
224
225 #if APP_IO_RX_DROP_ALL_PACKETS
226                 for (j = 0; j < n_mbufs; j ++) {
227                         struct rte_mbuf *pkt = lp->rx.mbuf_in.array[j];
228                         rte_pktmbuf_free(pkt);
229                 }
230
231                 continue;
232 #endif
233
234                 mbuf_1_0 = lp->rx.mbuf_in.array[0];
235                 mbuf_1_1 = lp->rx.mbuf_in.array[1];
236                 data_1_0 = rte_pktmbuf_mtod(mbuf_1_0, uint8_t *);
237                 if (likely(n_mbufs > 1)) {
238                         data_1_1 = rte_pktmbuf_mtod(mbuf_1_1, uint8_t *);
239                 }
240
241                 mbuf_2_0 = lp->rx.mbuf_in.array[2];
242                 mbuf_2_1 = lp->rx.mbuf_in.array[3];
243                 APP_IO_RX_PREFETCH0(mbuf_2_0);
244                 APP_IO_RX_PREFETCH0(mbuf_2_1);
245
246                 for (j = 0; j + 3 < n_mbufs; j += 2) {
247                         struct rte_mbuf *mbuf_0_0, *mbuf_0_1;
248                         uint8_t *data_0_0, *data_0_1;
249                         uint32_t worker_0, worker_1;
250
251                         mbuf_0_0 = mbuf_1_0;
252                         mbuf_0_1 = mbuf_1_1;
253                         data_0_0 = data_1_0;
254                         data_0_1 = data_1_1;
255
256                         mbuf_1_0 = mbuf_2_0;
257                         mbuf_1_1 = mbuf_2_1;
258                         data_1_0 = rte_pktmbuf_mtod(mbuf_2_0, uint8_t *);
259                         data_1_1 = rte_pktmbuf_mtod(mbuf_2_1, uint8_t *);
260                         APP_IO_RX_PREFETCH0(data_1_0);
261                         APP_IO_RX_PREFETCH0(data_1_1);
262
263                         mbuf_2_0 = lp->rx.mbuf_in.array[j+4];
264                         mbuf_2_1 = lp->rx.mbuf_in.array[j+5];
265                         APP_IO_RX_PREFETCH0(mbuf_2_0);
266                         APP_IO_RX_PREFETCH0(mbuf_2_1);
267
268                         worker_0 = data_0_0[pos_lb] & (n_workers - 1);
269                         worker_1 = data_0_1[pos_lb] & (n_workers - 1);
270
271                         app_lcore_io_rx_buffer_to_send(lp, worker_0, mbuf_0_0, bsz_wr);
272                         app_lcore_io_rx_buffer_to_send(lp, worker_1, mbuf_0_1, bsz_wr);
273                 }
274
275                 /* Handle the last 1, 2 (when n_mbufs is even) or 3 (when n_mbufs is odd) packets  */
276                 for ( ; j < n_mbufs; j += 1) {
277                         struct rte_mbuf *mbuf;
278                         uint8_t *data;
279                         uint32_t worker;
280
281                         mbuf = mbuf_1_0;
282                         mbuf_1_0 = mbuf_1_1;
283                         mbuf_1_1 = mbuf_2_0;
284                         mbuf_2_0 = mbuf_2_1;
285
286                         data = rte_pktmbuf_mtod(mbuf, uint8_t *);
287
288                         APP_IO_RX_PREFETCH0(mbuf_1_0);
289
290                         worker = data[pos_lb] & (n_workers - 1);
291
292                         app_lcore_io_rx_buffer_to_send(lp, worker, mbuf, bsz_wr);
293                 }
294         }
295 }
296
297 static inline void
298 app_lcore_io_rx_flush(struct app_lcore_params_io *lp, uint32_t n_workers)
299 {
300         uint32_t worker;
301
302         for (worker = 0; worker < n_workers; worker ++) {
303                 int ret;
304
305                 if (likely((lp->rx.mbuf_out_flush[worker] == 0) ||
306                            (lp->rx.mbuf_out[worker].n_mbufs == 0))) {
307                         lp->rx.mbuf_out_flush[worker] = 1;
308                         continue;
309                 }
310
311                 ret = rte_ring_sp_enqueue_bulk(
312                         lp->rx.rings[worker],
313                         (void **) lp->rx.mbuf_out[worker].array,
314                         lp->rx.mbuf_out[worker].n_mbufs);
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                         uint8_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
353                         if (unlikely(ret == -ENOENT)) {
354                                 continue;
355                         }
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                                         (unsigned) 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         uint8_t port;
422
423         for (port = 0; port < lp->tx.n_nic_ports; port ++) {
424                 uint32_t n_pkts;
425
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;
429                         continue;
430                 }
431
432                 n_pkts = rte_eth_tx_burst(
433                         port,
434                         0,
435                         lp->tx.mbuf_out[port].array,
436                         (uint16_t) lp->tx.mbuf_out[port].n_mbufs);
437
438                 if (unlikely(n_pkts < lp->tx.mbuf_out[port].n_mbufs)) {
439                         uint32_t k;
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);
443                         }
444                 }
445
446                 lp->tx.mbuf_out[port].n_mbufs = 0;
447                 lp->tx.mbuf_out_flush[port] = 1;
448         }
449 }
450
451 static void
452 app_lcore_main_loop_io(void)
453 {
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();
457         uint64_t i = 0;
458
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;
463
464         uint8_t pos_lb = app.pos_lb;
465
466         for ( ; ; ) {
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);
470                         }
471
472                         if (likely(lp->tx.n_nic_ports > 0)) {
473                                 app_lcore_io_tx_flush(lp);
474                         }
475
476                         i = 0;
477                 }
478
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);
481                 }
482
483                 if (likely(lp->tx.n_nic_ports > 0)) {
484                         app_lcore_io_tx(lp, n_workers, bsz_tx_rd, bsz_tx_wr);
485                 }
486
487                 i ++;
488         }
489 }
490
491 static inline void
492 app_lcore_worker(
493         struct app_lcore_params_worker *lp,
494         uint32_t bsz_rd,
495         uint32_t bsz_wr)
496 {
497         uint32_t i;
498
499         for (i = 0; i < lp->n_rings_in; i ++) {
500                 struct rte_ring *ring_in = lp->rings_in[i];
501                 uint32_t j;
502                 int ret;
503
504                 ret = rte_ring_sc_dequeue_bulk(
505                         ring_in,
506                         (void **) lp->mbuf_in.array,
507                         bsz_rd);
508
509                 if (unlikely(ret == -ENOENT)) {
510                         continue;
511                 }
512
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);
517                 }
518
519                 continue;
520 #endif
521
522                 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[0], unsigned char *));
523                 APP_WORKER_PREFETCH0(lp->mbuf_in.array[1]);
524
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;
529                         uint8_t port;
530
531                         if (likely(j < bsz_rd - 1)) {
532                                 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[j+1], unsigned char *));
533                         }
534                         if (likely(j < bsz_rd - 2)) {
535                                 APP_WORKER_PREFETCH0(lp->mbuf_in.array[j+2]);
536                         }
537
538                         pkt = lp->mbuf_in.array[j];
539                         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, unsigned char *) + sizeof(struct ether_hdr));
540                         ipv4_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
541
542                         if (unlikely(rte_lpm_lookup(lp->lpm_table, ipv4_dst, &port) != 0)) {
543                                 port = pkt->pkt.in_port;
544                         }
545
546                         pos = lp->mbuf_out[port].n_mbufs;
547
548                         lp->mbuf_out[port].array[pos ++] = pkt;
549                         if (likely(pos < bsz_wr)) {
550                                 lp->mbuf_out[port].n_mbufs = pos;
551                                 continue;
552                         }
553
554                         ret = rte_ring_sp_enqueue_bulk(
555                                 lp->rings_out[port],
556                                 (void **) lp->mbuf_out[port].array,
557                                 bsz_wr);
558
559 #if APP_STATS
560                         lp->rings_out_iters[port] ++;
561                         if (ret == 0) {
562                                 lp->rings_out_count[port] += 1;
563                         }
564                         if (lp->rings_out_iters[port] == APP_STATS){
565                                 printf("\t\tWorker %u out (NIC port %u): enq success rate = %.2f\n",
566                                         (unsigned) lp->worker_id,
567                                         (unsigned) port,
568                                         ((double) lp->rings_out_count[port]) / ((double) lp->rings_out_iters[port]));
569                                 lp->rings_out_iters[port] = 0;
570                                 lp->rings_out_count[port] = 0;
571                         }
572 #endif
573
574                         if (unlikely(ret == -ENOBUFS)) {
575                                 uint32_t k;
576                                 for (k = 0; k < bsz_wr; k ++) {
577                                         struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
578                                         rte_pktmbuf_free(pkt_to_free);
579                                 }
580                         }
581
582                         lp->mbuf_out[port].n_mbufs = 0;
583                         lp->mbuf_out_flush[port] = 0;
584                 }
585         }
586 }
587
588 static inline void
589 app_lcore_worker_flush(struct app_lcore_params_worker *lp)
590 {
591         uint32_t port;
592
593         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
594                 int ret;
595
596                 if (unlikely(lp->rings_out[port] == NULL)) {
597                         continue;
598                 }
599
600                 if (likely((lp->mbuf_out_flush[port] == 0) ||
601                            (lp->mbuf_out[port].n_mbufs == 0))) {
602                         lp->mbuf_out_flush[port] = 1;
603                         continue;
604                 }
605
606                 ret = rte_ring_sp_enqueue_bulk(
607                         lp->rings_out[port],
608                         (void **) lp->mbuf_out[port].array,
609                         lp->mbuf_out[port].n_mbufs);
610
611                 if (unlikely(ret < 0)) {
612                         uint32_t k;
613                         for (k = 0; k < lp->mbuf_out[port].n_mbufs; k ++) {
614                                 struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
615                                 rte_pktmbuf_free(pkt_to_free);
616                         }
617                 }
618
619                 lp->mbuf_out[port].n_mbufs = 0;
620                 lp->mbuf_out_flush[port] = 1;
621         }
622 }
623
624 static void
625 app_lcore_main_loop_worker(void) {
626         uint32_t lcore = rte_lcore_id();
627         struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
628         uint64_t i = 0;
629
630         uint32_t bsz_rd = app.burst_size_worker_read;
631         uint32_t bsz_wr = app.burst_size_worker_write;
632
633         for ( ; ; ) {
634                 if (APP_LCORE_WORKER_FLUSH && (unlikely(i == APP_LCORE_WORKER_FLUSH))) {
635                         app_lcore_worker_flush(lp);
636                         i = 0;
637                 }
638
639                 app_lcore_worker(lp, bsz_rd, bsz_wr);
640
641                 i ++;
642         }
643 }
644
645 int
646 app_lcore_main_loop(__attribute__((unused)) void *arg)
647 {
648         struct app_lcore_params *lp;
649         unsigned lcore;
650
651         lcore = rte_lcore_id();
652         lp = &app.lcore_params[lcore];
653
654         if (lp->type == e_APP_LCORE_IO) {
655                 printf("Logical core %u (I/O) main loop.\n", lcore);
656                 app_lcore_main_loop_io();
657         }
658
659         if (lp->type == e_APP_LCORE_WORKER) {
660                 printf("Logical core %u (worker %u) main loop.\n",
661                         lcore,
662                         (unsigned) lp->worker.worker_id);
663                 app_lcore_main_loop_worker();
664         }
665
666         return 0;
667 }