remove version in all files
[dpdk.git] / examples / load_balancer / runtime.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <sys/types.h>
40 #include <string.h>
41 #include <sys/queue.h>
42 #include <stdarg.h>
43 #include <errno.h>
44 #include <getopt.h>
45
46 #include <rte_common.h>
47 #include <rte_byteorder.h>
48 #include <rte_log.h>
49 #include <rte_memory.h>
50 #include <rte_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_tailq.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_launch.h>
56 #include <rte_atomic.h>
57 #include <rte_cycles.h>
58 #include <rte_prefetch.h>
59 #include <rte_lcore.h>
60 #include <rte_per_lcore.h>
61 #include <rte_branch_prediction.h>
62 #include <rte_interrupts.h>
63 #include <rte_pci.h>
64 #include <rte_random.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
68 #include <rte_ring.h>
69 #include <rte_mempool.h>
70 #include <rte_mbuf.h>
71 #include <rte_ip.h>
72 #include <rte_tcp.h>
73 #include <rte_lpm.h>
74
75 #include "main.h"
76
77 #ifndef APP_LCORE_IO_FLUSH
78 #define APP_LCORE_IO_FLUSH           1000000
79 #endif
80
81 #ifndef APP_LCORE_WORKER_FLUSH
82 #define APP_LCORE_WORKER_FLUSH       1000000
83 #endif
84
85 #ifndef APP_STATS
86 #define APP_STATS                    1000000
87 #endif
88
89 #define APP_IO_RX_DROP_ALL_PACKETS   0
90 #define APP_WORKER_DROP_ALL_PACKETS  0
91 #define APP_IO_TX_DROP_ALL_PACKETS   0
92
93 #ifndef APP_IO_RX_PREFETCH_ENABLE
94 #define APP_IO_RX_PREFETCH_ENABLE    1
95 #endif
96
97 #ifndef APP_WORKER_PREFETCH_ENABLE
98 #define APP_WORKER_PREFETCH_ENABLE   1
99 #endif
100
101 #ifndef APP_IO_TX_PREFETCH_ENABLE
102 #define APP_IO_TX_PREFETCH_ENABLE    1
103 #endif
104
105 #if APP_IO_RX_PREFETCH_ENABLE
106 #define APP_IO_RX_PREFETCH0(p)       rte_prefetch0(p)
107 #define APP_IO_RX_PREFETCH1(p)       rte_prefetch1(p)
108 #else
109 #define APP_IO_RX_PREFETCH0(p)
110 #define APP_IO_RX_PREFETCH1(p)
111 #endif
112
113 #if APP_WORKER_PREFETCH_ENABLE
114 #define APP_WORKER_PREFETCH0(p)      rte_prefetch0(p)
115 #define APP_WORKER_PREFETCH1(p)      rte_prefetch1(p)
116 #else
117 #define APP_WORKER_PREFETCH0(p)
118 #define APP_WORKER_PREFETCH1(p)
119 #endif
120
121 #if APP_IO_TX_PREFETCH_ENABLE
122 #define APP_IO_TX_PREFETCH0(p)       rte_prefetch0(p)
123 #define APP_IO_TX_PREFETCH1(p)       rte_prefetch1(p)
124 #else
125 #define APP_IO_TX_PREFETCH0(p)
126 #define APP_IO_TX_PREFETCH1(p)
127 #endif
128
129 static inline void
130 app_lcore_io_rx_buffer_to_send (
131         struct app_lcore_params_io *lp,
132         uint32_t worker,
133         struct rte_mbuf *mbuf,
134         uint32_t bsz)
135 {
136         uint32_t pos;
137         int ret;
138
139         pos = lp->rx.mbuf_out[worker].n_mbufs;
140         lp->rx.mbuf_out[worker].array[pos ++] = mbuf;
141         if (likely(pos < bsz)) {
142                 lp->rx.mbuf_out[worker].n_mbufs = pos;
143                 return;
144         }
145
146         ret = rte_ring_sp_enqueue_bulk(
147                 lp->rx.rings[worker],
148                 (void **) lp->rx.mbuf_out[worker].array,
149                 bsz);
150
151         if (unlikely(ret == -ENOBUFS)) {
152                 uint32_t k;
153                 for (k = 0; k < bsz; k ++) {
154                         struct rte_mbuf *m = lp->rx.mbuf_out[worker].array[k];
155                         rte_pktmbuf_free(m);
156                 }
157         }
158
159         lp->rx.mbuf_out[worker].n_mbufs = 0;
160         lp->rx.mbuf_out_flush[worker] = 0;
161
162 #if APP_STATS
163         lp->rx.rings_iters[worker] ++;
164         if (likely(ret == 0)) {
165                 lp->rx.rings_count[worker] ++;
166         }
167         if (unlikely(lp->rx.rings_iters[worker] == APP_STATS)) {
168                 uint32_t lcore = rte_lcore_id();
169
170                 printf("\tI/O RX %u out (worker %u): enq success rate = %.2f\n",
171                         lcore,
172                         worker,
173                         ((double) lp->rx.rings_count[worker]) / ((double) lp->rx.rings_iters[worker]));
174                 lp->rx.rings_iters[worker] = 0;
175                 lp->rx.rings_count[worker] = 0;
176         }
177 #endif
178 }
179
180 static inline void
181 app_lcore_io_rx(
182         struct app_lcore_params_io *lp,
183         uint32_t n_workers,
184         uint32_t bsz_rd,
185         uint32_t bsz_wr,
186         uint8_t pos_lb)
187 {
188         struct rte_mbuf *mbuf_1_0, *mbuf_1_1, *mbuf_2_0, *mbuf_2_1;
189         uint8_t *data_1_0, *data_1_1;
190         uint32_t i;
191
192         for (i = 0; i < lp->rx.n_nic_queues; i ++) {
193                 uint8_t port = lp->rx.nic_queues[i].port;
194                 uint8_t queue = lp->rx.nic_queues[i].queue;
195                 uint32_t n_mbufs, j;
196
197                 n_mbufs = rte_eth_rx_burst(
198                         port,
199                         queue,
200                         lp->rx.mbuf_in.array,
201                         (uint16_t) bsz_rd);
202
203                 if (unlikely(n_mbufs == 0)) {
204                         continue;
205                 }
206
207 #if APP_STATS
208                 lp->rx.nic_queues_iters[i] ++;
209                 lp->rx.nic_queues_count[i] += n_mbufs;
210                 if (unlikely(lp->rx.nic_queues_iters[i] == APP_STATS)) {
211                         struct rte_eth_stats stats;
212                         uint32_t lcore = rte_lcore_id();
213
214                         rte_eth_stats_get(port, &stats);
215
216                         printf("I/O RX %u in (NIC port %u): NIC drop ratio = %.2f avg burst size = %.2f\n",
217                                 lcore,
218                                 (uint32_t) port,
219                                 (double) stats.ierrors / (double) (stats.ierrors + stats.ipackets),
220                                 ((double) lp->rx.nic_queues_count[i]) / ((double) lp->rx.nic_queues_iters[i]));
221                         lp->rx.nic_queues_iters[i] = 0;
222                         lp->rx.nic_queues_count[i] = 0;
223                 }
224 #endif
225
226 #if APP_IO_RX_DROP_ALL_PACKETS
227                 for (j = 0; j < n_mbufs; j ++) {
228                         struct rte_mbuf *pkt = lp->rx.mbuf_in.array[j];
229                         rte_pktmbuf_free(pkt);
230                 }
231
232                 continue;
233 #endif
234
235                 mbuf_1_0 = lp->rx.mbuf_in.array[0];
236                 mbuf_1_1 = lp->rx.mbuf_in.array[1];
237                 data_1_0 = rte_pktmbuf_mtod(mbuf_1_0, uint8_t *);
238                 if (likely(n_mbufs > 1)) {
239                         data_1_1 = rte_pktmbuf_mtod(mbuf_1_1, uint8_t *);
240                 }
241
242                 mbuf_2_0 = lp->rx.mbuf_in.array[2];
243                 mbuf_2_1 = lp->rx.mbuf_in.array[3];
244                 APP_IO_RX_PREFETCH0(mbuf_2_0);
245                 APP_IO_RX_PREFETCH0(mbuf_2_1);
246
247                 for (j = 0; j + 3 < n_mbufs; j += 2) {
248                         struct rte_mbuf *mbuf_0_0, *mbuf_0_1;
249                         uint8_t *data_0_0, *data_0_1;
250                         uint32_t worker_0, worker_1;
251
252                         mbuf_0_0 = mbuf_1_0;
253                         mbuf_0_1 = mbuf_1_1;
254                         data_0_0 = data_1_0;
255                         data_0_1 = data_1_1;
256
257                         mbuf_1_0 = mbuf_2_0;
258                         mbuf_1_1 = mbuf_2_1;
259                         data_1_0 = rte_pktmbuf_mtod(mbuf_2_0, uint8_t *);
260                         data_1_1 = rte_pktmbuf_mtod(mbuf_2_1, uint8_t *);
261                         APP_IO_RX_PREFETCH0(data_1_0);
262                         APP_IO_RX_PREFETCH0(data_1_1);
263
264                         mbuf_2_0 = lp->rx.mbuf_in.array[j+4];
265                         mbuf_2_1 = lp->rx.mbuf_in.array[j+5];
266                         APP_IO_RX_PREFETCH0(mbuf_2_0);
267                         APP_IO_RX_PREFETCH0(mbuf_2_1);
268
269                         worker_0 = data_0_0[pos_lb] & (n_workers - 1);
270                         worker_1 = data_0_1[pos_lb] & (n_workers - 1);
271
272                         app_lcore_io_rx_buffer_to_send(lp, worker_0, mbuf_0_0, bsz_wr);
273                         app_lcore_io_rx_buffer_to_send(lp, worker_1, mbuf_0_1, bsz_wr);
274                 }
275
276                 /* Handle the last 1, 2 (when n_mbufs is even) or 3 (when n_mbufs is odd) packets  */
277                 for ( ; j < n_mbufs; j += 1) {
278                         struct rte_mbuf *mbuf;
279                         uint8_t *data;
280                         uint32_t worker;
281
282                         mbuf = mbuf_1_0;
283                         mbuf_1_0 = mbuf_1_1;
284                         mbuf_1_1 = mbuf_2_0;
285                         mbuf_2_0 = mbuf_2_1;
286
287                         data = rte_pktmbuf_mtod(mbuf, uint8_t *);
288
289                         APP_IO_RX_PREFETCH0(mbuf_1_0);
290
291                         worker = data[pos_lb] & (n_workers - 1);
292
293                         app_lcore_io_rx_buffer_to_send(lp, worker, mbuf, bsz_wr);
294                 }
295         }
296 }
297
298 static inline void
299 app_lcore_io_rx_flush(struct app_lcore_params_io *lp, uint32_t n_workers)
300 {
301         uint32_t worker;
302
303         for (worker = 0; worker < n_workers; worker ++) {
304                 int ret;
305
306                 if (likely((lp->rx.mbuf_out_flush[worker] == 0) ||
307                            (lp->rx.mbuf_out[worker].n_mbufs == 0))) {
308                         lp->rx.mbuf_out_flush[worker] = 1;
309                         continue;
310                 }
311
312                 ret = rte_ring_sp_enqueue_bulk(
313                         lp->rx.rings[worker],
314                         (void **) lp->rx.mbuf_out[worker].array,
315                         lp->rx.mbuf_out[worker].n_mbufs);
316
317                 if (unlikely(ret < 0)) {
318                         uint32_t k;
319                         for (k = 0; k < lp->rx.mbuf_out[worker].n_mbufs; k ++) {
320                                 struct rte_mbuf *pkt_to_free = lp->rx.mbuf_out[worker].array[k];
321                                 rte_pktmbuf_free(pkt_to_free);
322                         }
323                 }
324
325                 lp->rx.mbuf_out[worker].n_mbufs = 0;
326                 lp->rx.mbuf_out_flush[worker] = 1;
327         }
328 }
329
330 static inline void
331 app_lcore_io_tx(
332         struct app_lcore_params_io *lp,
333         uint32_t n_workers,
334         uint32_t bsz_rd,
335         uint32_t bsz_wr)
336 {
337         uint32_t worker;
338
339         for (worker = 0; worker < n_workers; worker ++) {
340                 uint32_t i;
341
342                 for (i = 0; i < lp->tx.n_nic_ports; i ++) {
343                         uint8_t port = lp->tx.nic_ports[i];
344                         struct rte_ring *ring = lp->tx.rings[port][worker];
345                         uint32_t n_mbufs, n_pkts;
346                         int ret;
347
348                         n_mbufs = lp->tx.mbuf_out[port].n_mbufs;
349                         ret = rte_ring_sc_dequeue_bulk(
350                                 ring,
351                                 (void **) &lp->tx.mbuf_out[port].array[n_mbufs],
352                                 bsz_rd);
353
354                         if (unlikely(ret == -ENOENT)) {
355                                 continue;
356                         }
357
358                         n_mbufs += bsz_rd;
359
360 #if APP_IO_TX_DROP_ALL_PACKETS
361                         {
362                                 uint32_t j;
363                                 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[0]);
364                                 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[1]);
365
366                                 for (j = 0; j < n_mbufs; j ++) {
367                                         if (likely(j < n_mbufs - 2)) {
368                                                 APP_IO_TX_PREFETCH0(lp->tx.mbuf_out[port].array[j + 2]);
369                                         }
370
371                                         rte_pktmbuf_free(lp->tx.mbuf_out[port].array[j]);
372                                 }
373
374                                 lp->tx.mbuf_out[port].n_mbufs = 0;
375
376                                 continue;
377                         }
378 #endif
379
380                         if (unlikely(n_mbufs < bsz_wr)) {
381                                 lp->tx.mbuf_out[port].n_mbufs = n_mbufs;
382                                 continue;
383                         }
384
385                         n_pkts = rte_eth_tx_burst(
386                                 port,
387                                 0,
388                                 lp->tx.mbuf_out[port].array,
389                                 (uint16_t) n_mbufs);
390
391 #if APP_STATS
392                         lp->tx.nic_ports_iters[port] ++;
393                         lp->tx.nic_ports_count[port] += n_pkts;
394                         if (unlikely(lp->tx.nic_ports_iters[port] == APP_STATS)) {
395                                 uint32_t lcore = rte_lcore_id();
396
397                                 printf("\t\t\tI/O TX %u out (port %u): avg burst size = %.2f\n",
398                                         lcore,
399                                         (uint32_t) port,
400                                         ((double) lp->tx.nic_ports_count[port]) / ((double) lp->tx.nic_ports_iters[port]));
401                                 lp->tx.nic_ports_iters[port] = 0;
402                                 lp->tx.nic_ports_count[port] = 0;
403                         }
404 #endif
405
406                         if (unlikely(n_pkts < n_mbufs)) {
407                                 uint32_t k;
408                                 for (k = n_pkts; k < n_mbufs; k ++) {
409                                         struct rte_mbuf *pkt_to_free = lp->tx.mbuf_out[port].array[k];
410                                         rte_pktmbuf_free(pkt_to_free);
411                                 }
412                         }
413                         lp->tx.mbuf_out[port].n_mbufs = 0;
414                         lp->tx.mbuf_out_flush[port] = 0;
415                 }
416         }
417 }
418
419 static inline void
420 app_lcore_io_tx_flush(struct app_lcore_params_io *lp)
421 {
422         uint8_t port;
423
424         for (port = 0; port < lp->tx.n_nic_ports; port ++) {
425                 uint32_t n_pkts;
426
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
510                 if (unlikely(ret == -ENOENT)) {
511                         continue;
512                 }
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                         uint8_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 = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, unsigned char *) + sizeof(struct ether_hdr));
541                         ipv4_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
542
543                         if (unlikely(rte_lpm_lookup(lp->lpm_table, ipv4_dst, &port) != 0)) {
544                                 port = pkt->pkt.in_port;
545                         }
546
547                         pos = lp->mbuf_out[port].n_mbufs;
548
549                         lp->mbuf_out[port].array[pos ++] = pkt;
550                         if (likely(pos < bsz_wr)) {
551                                 lp->mbuf_out[port].n_mbufs = pos;
552                                 continue;
553                         }
554
555                         ret = rte_ring_sp_enqueue_bulk(
556                                 lp->rings_out[port],
557                                 (void **) lp->mbuf_out[port].array,
558                                 bsz_wr);
559
560 #if APP_STATS
561                         lp->rings_out_iters[port] ++;
562                         if (ret == 0) {
563                                 lp->rings_out_count[port] += 1;
564                         }
565                         if (lp->rings_out_iters[port] == APP_STATS){
566                                 printf("\t\tWorker %u out (NIC port %u): enq success rate = %.2f\n",
567                                         lp->worker_id,
568                                         (uint32_t) port,
569                                         ((double) lp->rings_out_count[port]) / ((double) lp->rings_out_iters[port]));
570                                 lp->rings_out_iters[port] = 0;
571                                 lp->rings_out_count[port] = 0;
572                         }
573 #endif
574
575                         if (unlikely(ret == -ENOBUFS)) {
576                                 uint32_t k;
577                                 for (k = 0; k < bsz_wr; k ++) {
578                                         struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
579                                         rte_pktmbuf_free(pkt_to_free);
580                                 }
581                         }
582
583                         lp->mbuf_out[port].n_mbufs = 0;
584                         lp->mbuf_out_flush[port] = 0;
585                 }
586         }
587 }
588
589 static inline void
590 app_lcore_worker_flush(struct app_lcore_params_worker *lp)
591 {
592         uint32_t port;
593
594         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
595                 int ret;
596
597                 if (unlikely(lp->rings_out[port] == NULL)) {
598                         continue;
599                 }
600
601                 if (likely((lp->mbuf_out_flush[port] == 0) ||
602                            (lp->mbuf_out[port].n_mbufs == 0))) {
603                         lp->mbuf_out_flush[port] = 1;
604                         continue;
605                 }
606
607                 ret = rte_ring_sp_enqueue_bulk(
608                         lp->rings_out[port],
609                         (void **) lp->mbuf_out[port].array,
610                         lp->mbuf_out[port].n_mbufs);
611
612                 if (unlikely(ret < 0)) {
613                         uint32_t k;
614                         for (k = 0; k < lp->mbuf_out[port].n_mbufs; k ++) {
615                                 struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
616                                 rte_pktmbuf_free(pkt_to_free);
617                         }
618                 }
619
620                 lp->mbuf_out[port].n_mbufs = 0;
621                 lp->mbuf_out_flush[port] = 1;
622         }
623 }
624
625 static void
626 app_lcore_main_loop_worker(void) {
627         uint32_t lcore = rte_lcore_id();
628         struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
629         uint64_t i = 0;
630
631         uint32_t bsz_rd = app.burst_size_worker_read;
632         uint32_t bsz_wr = app.burst_size_worker_write;
633
634         for ( ; ; ) {
635                 if (APP_LCORE_WORKER_FLUSH && (unlikely(i == APP_LCORE_WORKER_FLUSH))) {
636                         app_lcore_worker_flush(lp);
637                         i = 0;
638                 }
639
640                 app_lcore_worker(lp, bsz_rd, bsz_wr);
641
642                 i ++;
643         }
644 }
645
646 int
647 app_lcore_main_loop(__attribute__((unused)) void *arg)
648 {
649         struct app_lcore_params *lp;
650         uint32_t lcore;
651
652         lcore = rte_lcore_id();
653         lp = &app.lcore_params[lcore];
654
655         if (lp->type == e_APP_LCORE_IO) {
656                 printf("Logical core %u (I/O) main loop.\n", lcore);
657                 app_lcore_main_loop_io();
658         }
659
660         if (lp->type == e_APP_LCORE_WORKER) {
661                 printf("Logical core %u (worker %u) main loop.\n",
662                         lcore,
663                         lp->worker.worker_id);
664                 app_lcore_main_loop_worker();
665         }
666
667         return 0;
668 }