7f918aa4574305f45120459b6966a99546305552
[deb_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_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_atomic.h>
55 #include <rte_cycles.h>
56 #include <rte_prefetch.h>
57 #include <rte_lcore.h>
58 #include <rte_per_lcore.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_interrupts.h>
61 #include <rte_pci.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_ring.h>
67 #include <rte_mempool.h>
68 #include <rte_mbuf.h>
69 #include <rte_ip.h>
70 #include <rte_tcp.h>
71 #include <rte_lpm.h>
72
73 #include "main.h"
74
75 #ifndef APP_LCORE_IO_FLUSH
76 #define APP_LCORE_IO_FLUSH           1000000
77 #endif
78
79 #ifndef APP_LCORE_WORKER_FLUSH
80 #define APP_LCORE_WORKER_FLUSH       1000000
81 #endif
82
83 #ifndef APP_STATS
84 #define APP_STATS                    1000000
85 #endif
86
87 #define APP_IO_RX_DROP_ALL_PACKETS   0
88 #define APP_WORKER_DROP_ALL_PACKETS  0
89 #define APP_IO_TX_DROP_ALL_PACKETS   0
90
91 #ifndef APP_IO_RX_PREFETCH_ENABLE
92 #define APP_IO_RX_PREFETCH_ENABLE    1
93 #endif
94
95 #ifndef APP_WORKER_PREFETCH_ENABLE
96 #define APP_WORKER_PREFETCH_ENABLE   1
97 #endif
98
99 #ifndef APP_IO_TX_PREFETCH_ENABLE
100 #define APP_IO_TX_PREFETCH_ENABLE    1
101 #endif
102
103 #if APP_IO_RX_PREFETCH_ENABLE
104 #define APP_IO_RX_PREFETCH0(p)       rte_prefetch0(p)
105 #define APP_IO_RX_PREFETCH1(p)       rte_prefetch1(p)
106 #else
107 #define APP_IO_RX_PREFETCH0(p)
108 #define APP_IO_RX_PREFETCH1(p)
109 #endif
110
111 #if APP_WORKER_PREFETCH_ENABLE
112 #define APP_WORKER_PREFETCH0(p)      rte_prefetch0(p)
113 #define APP_WORKER_PREFETCH1(p)      rte_prefetch1(p)
114 #else
115 #define APP_WORKER_PREFETCH0(p)
116 #define APP_WORKER_PREFETCH1(p)
117 #endif
118
119 #if APP_IO_TX_PREFETCH_ENABLE
120 #define APP_IO_TX_PREFETCH0(p)       rte_prefetch0(p)
121 #define APP_IO_TX_PREFETCH1(p)       rte_prefetch1(p)
122 #else
123 #define APP_IO_TX_PREFETCH0(p)
124 #define APP_IO_TX_PREFETCH1(p)
125 #endif
126
127 static inline void
128 app_lcore_io_rx_buffer_to_send (
129         struct app_lcore_params_io *lp,
130         uint32_t worker,
131         struct rte_mbuf *mbuf,
132         uint32_t bsz)
133 {
134         uint32_t pos;
135         int ret;
136
137         pos = lp->rx.mbuf_out[worker].n_mbufs;
138         lp->rx.mbuf_out[worker].array[pos ++] = mbuf;
139         if (likely(pos < bsz)) {
140                 lp->rx.mbuf_out[worker].n_mbufs = pos;
141                 return;
142         }
143
144         ret = rte_ring_sp_enqueue_bulk(
145                 lp->rx.rings[worker],
146                 (void **) lp->rx.mbuf_out[worker].array,
147                 bsz,
148                 NULL);
149
150         if (unlikely(ret == 0)) {
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.imissed / (double) (stats.imissed + 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                         NULL);
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                                 NULL);
354
355                         if (unlikely(ret == 0))
356                                 continue;
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                                 unsigned lcore = rte_lcore_id();
396
397                                 printf("\t\t\tI/O TX %u out (port %u): avg burst size = %.2f\n",
398                                         lcore,
399                                         (unsigned) 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         uint32_t i;
424
425         for (i = 0; i < lp->tx.n_nic_ports; i++) {
426                 uint32_t n_pkts;
427
428                 port = lp->tx.nic_ports[i];
429                 if (likely((lp->tx.mbuf_out_flush[port] == 0) ||
430                            (lp->tx.mbuf_out[port].n_mbufs == 0))) {
431                         lp->tx.mbuf_out_flush[port] = 1;
432                         continue;
433                 }
434
435                 n_pkts = rte_eth_tx_burst(
436                         port,
437                         0,
438                         lp->tx.mbuf_out[port].array,
439                         (uint16_t) lp->tx.mbuf_out[port].n_mbufs);
440
441                 if (unlikely(n_pkts < lp->tx.mbuf_out[port].n_mbufs)) {
442                         uint32_t k;
443                         for (k = n_pkts; k < lp->tx.mbuf_out[port].n_mbufs; k ++) {
444                                 struct rte_mbuf *pkt_to_free = lp->tx.mbuf_out[port].array[k];
445                                 rte_pktmbuf_free(pkt_to_free);
446                         }
447                 }
448
449                 lp->tx.mbuf_out[port].n_mbufs = 0;
450                 lp->tx.mbuf_out_flush[port] = 1;
451         }
452 }
453
454 static void
455 app_lcore_main_loop_io(void)
456 {
457         uint32_t lcore = rte_lcore_id();
458         struct app_lcore_params_io *lp = &app.lcore_params[lcore].io;
459         uint32_t n_workers = app_get_lcores_worker();
460         uint64_t i = 0;
461
462         uint32_t bsz_rx_rd = app.burst_size_io_rx_read;
463         uint32_t bsz_rx_wr = app.burst_size_io_rx_write;
464         uint32_t bsz_tx_rd = app.burst_size_io_tx_read;
465         uint32_t bsz_tx_wr = app.burst_size_io_tx_write;
466
467         uint8_t pos_lb = app.pos_lb;
468
469         for ( ; ; ) {
470                 if (APP_LCORE_IO_FLUSH && (unlikely(i == APP_LCORE_IO_FLUSH))) {
471                         if (likely(lp->rx.n_nic_queues > 0)) {
472                                 app_lcore_io_rx_flush(lp, n_workers);
473                         }
474
475                         if (likely(lp->tx.n_nic_ports > 0)) {
476                                 app_lcore_io_tx_flush(lp);
477                         }
478
479                         i = 0;
480                 }
481
482                 if (likely(lp->rx.n_nic_queues > 0)) {
483                         app_lcore_io_rx(lp, n_workers, bsz_rx_rd, bsz_rx_wr, pos_lb);
484                 }
485
486                 if (likely(lp->tx.n_nic_ports > 0)) {
487                         app_lcore_io_tx(lp, n_workers, bsz_tx_rd, bsz_tx_wr);
488                 }
489
490                 i ++;
491         }
492 }
493
494 static inline void
495 app_lcore_worker(
496         struct app_lcore_params_worker *lp,
497         uint32_t bsz_rd,
498         uint32_t bsz_wr)
499 {
500         uint32_t i;
501
502         for (i = 0; i < lp->n_rings_in; i ++) {
503                 struct rte_ring *ring_in = lp->rings_in[i];
504                 uint32_t j;
505                 int ret;
506
507                 ret = rte_ring_sc_dequeue_bulk(
508                         ring_in,
509                         (void **) lp->mbuf_in.array,
510                         bsz_rd,
511                         NULL);
512
513                 if (unlikely(ret == 0))
514                         continue;
515
516 #if APP_WORKER_DROP_ALL_PACKETS
517                 for (j = 0; j < bsz_rd; j ++) {
518                         struct rte_mbuf *pkt = lp->mbuf_in.array[j];
519                         rte_pktmbuf_free(pkt);
520                 }
521
522                 continue;
523 #endif
524
525                 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[0], unsigned char *));
526                 APP_WORKER_PREFETCH0(lp->mbuf_in.array[1]);
527
528                 for (j = 0; j < bsz_rd; j ++) {
529                         struct rte_mbuf *pkt;
530                         struct ipv4_hdr *ipv4_hdr;
531                         uint32_t ipv4_dst, pos;
532                         uint32_t port;
533
534                         if (likely(j < bsz_rd - 1)) {
535                                 APP_WORKER_PREFETCH1(rte_pktmbuf_mtod(lp->mbuf_in.array[j+1], unsigned char *));
536                         }
537                         if (likely(j < bsz_rd - 2)) {
538                                 APP_WORKER_PREFETCH0(lp->mbuf_in.array[j+2]);
539                         }
540
541                         pkt = lp->mbuf_in.array[j];
542                         ipv4_hdr = rte_pktmbuf_mtod_offset(pkt,
543                                                            struct ipv4_hdr *,
544                                                            sizeof(struct ether_hdr));
545                         ipv4_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
546
547                         if (unlikely(rte_lpm_lookup(lp->lpm_table, ipv4_dst, &port) != 0)) {
548                                 port = pkt->port;
549                         }
550
551                         pos = lp->mbuf_out[port].n_mbufs;
552
553                         lp->mbuf_out[port].array[pos ++] = pkt;
554                         if (likely(pos < bsz_wr)) {
555                                 lp->mbuf_out[port].n_mbufs = pos;
556                                 continue;
557                         }
558
559                         ret = rte_ring_sp_enqueue_bulk(
560                                 lp->rings_out[port],
561                                 (void **) lp->mbuf_out[port].array,
562                                 bsz_wr,
563                                 NULL);
564
565 #if APP_STATS
566                         lp->rings_out_iters[port] ++;
567                         if (ret > 0) {
568                                 lp->rings_out_count[port] += 1;
569                         }
570                         if (lp->rings_out_iters[port] == APP_STATS){
571                                 printf("\t\tWorker %u out (NIC port %u): enq success rate = %.2f\n",
572                                         (unsigned) lp->worker_id,
573                                         (unsigned) port,
574                                         ((double) lp->rings_out_count[port]) / ((double) lp->rings_out_iters[port]));
575                                 lp->rings_out_iters[port] = 0;
576                                 lp->rings_out_count[port] = 0;
577                         }
578 #endif
579
580                         if (unlikely(ret == 0)) {
581                                 uint32_t k;
582                                 for (k = 0; k < bsz_wr; k ++) {
583                                         struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
584                                         rte_pktmbuf_free(pkt_to_free);
585                                 }
586                         }
587
588                         lp->mbuf_out[port].n_mbufs = 0;
589                         lp->mbuf_out_flush[port] = 0;
590                 }
591         }
592 }
593
594 static inline void
595 app_lcore_worker_flush(struct app_lcore_params_worker *lp)
596 {
597         uint32_t port;
598
599         for (port = 0; port < APP_MAX_NIC_PORTS; port ++) {
600                 int ret;
601
602                 if (unlikely(lp->rings_out[port] == NULL)) {
603                         continue;
604                 }
605
606                 if (likely((lp->mbuf_out_flush[port] == 0) ||
607                            (lp->mbuf_out[port].n_mbufs == 0))) {
608                         lp->mbuf_out_flush[port] = 1;
609                         continue;
610                 }
611
612                 ret = rte_ring_sp_enqueue_bulk(
613                         lp->rings_out[port],
614                         (void **) lp->mbuf_out[port].array,
615                         lp->mbuf_out[port].n_mbufs,
616                         NULL);
617
618                 if (unlikely(ret == 0)) {
619                         uint32_t k;
620                         for (k = 0; k < lp->mbuf_out[port].n_mbufs; k ++) {
621                                 struct rte_mbuf *pkt_to_free = lp->mbuf_out[port].array[k];
622                                 rte_pktmbuf_free(pkt_to_free);
623                         }
624                 }
625
626                 lp->mbuf_out[port].n_mbufs = 0;
627                 lp->mbuf_out_flush[port] = 1;
628         }
629 }
630
631 static void
632 app_lcore_main_loop_worker(void) {
633         uint32_t lcore = rte_lcore_id();
634         struct app_lcore_params_worker *lp = &app.lcore_params[lcore].worker;
635         uint64_t i = 0;
636
637         uint32_t bsz_rd = app.burst_size_worker_read;
638         uint32_t bsz_wr = app.burst_size_worker_write;
639
640         for ( ; ; ) {
641                 if (APP_LCORE_WORKER_FLUSH && (unlikely(i == APP_LCORE_WORKER_FLUSH))) {
642                         app_lcore_worker_flush(lp);
643                         i = 0;
644                 }
645
646                 app_lcore_worker(lp, bsz_rd, bsz_wr);
647
648                 i ++;
649         }
650 }
651
652 int
653 app_lcore_main_loop(__attribute__((unused)) void *arg)
654 {
655         struct app_lcore_params *lp;
656         unsigned lcore;
657
658         lcore = rte_lcore_id();
659         lp = &app.lcore_params[lcore];
660
661         if (lp->type == e_APP_LCORE_IO) {
662                 printf("Logical core %u (I/O) main loop.\n", lcore);
663                 app_lcore_main_loop_io();
664         }
665
666         if (lp->type == e_APP_LCORE_WORKER) {
667                 printf("Logical core %u (worker %u) main loop.\n",
668                         lcore,
669                         (unsigned) lp->worker.worker_id);
670                 app_lcore_main_loop_worker();
671         }
672
673         return 0;
674 }