examples: replace some offload flags with packet type
[dpdk.git] / examples / l3fwd / main.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_vect.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_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_udp.h>
73 #include <rte_string_fns.h>
74
75 #include <cmdline_parse.h>
76 #include <cmdline_parse_etheraddr.h>
77
78 #define APP_LOOKUP_EXACT_MATCH          0
79 #define APP_LOOKUP_LPM                  1
80 #define DO_RFC_1812_CHECKS
81
82 #ifndef APP_LOOKUP_METHOD
83 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
84 #endif
85
86 /*
87  *  When set to zero, simple forwaring path is eanbled.
88  *  When set to one, optimized forwarding path is enabled.
89  *  Note that LPM optimisation path uses SSE4.1 instructions.
90  */
91 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
92 #define ENABLE_MULTI_BUFFER_OPTIMIZE    0
93 #else
94 #define ENABLE_MULTI_BUFFER_OPTIMIZE    1
95 #endif
96
97 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
98 #include <rte_hash.h>
99 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
100 #include <rte_lpm.h>
101 #include <rte_lpm6.h>
102 #else
103 #error "APP_LOOKUP_METHOD set to incorrect value"
104 #endif
105
106 #ifndef IPv6_BYTES
107 #define IPv6_BYTES_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"\
108                        "%02x%02x:%02x%02x:%02x%02x:%02x%02x"
109 #define IPv6_BYTES(addr) \
110         addr[0],  addr[1], addr[2],  addr[3], \
111         addr[4],  addr[5], addr[6],  addr[7], \
112         addr[8],  addr[9], addr[10], addr[11],\
113         addr[12], addr[13],addr[14], addr[15]
114 #endif
115
116
117 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
118
119 #define MAX_JUMBO_PKT_LEN  9600
120
121 #define IPV6_ADDR_LEN 16
122
123 #define MEMPOOL_CACHE_SIZE 256
124
125 /*
126  * This expression is used to calculate the number of mbufs needed depending on user input, taking
127  *  into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
128  *  RTE_MAX is used to ensure that NB_MBUF never goes below a minimum value of 8192
129  */
130
131 #define NB_MBUF RTE_MAX (                                                                                                                                       \
132                                 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT +                                                        \
133                                 nb_ports*nb_lcores*MAX_PKT_BURST +                                                                                      \
134                                 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT +                                                          \
135                                 nb_lcores*MEMPOOL_CACHE_SIZE),                                                                                          \
136                                 (unsigned)8192)
137
138 #define MAX_PKT_BURST     32
139 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
140
141 /*
142  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
143  */
144 #define MAX_TX_BURST    (MAX_PKT_BURST / 2)
145
146 #define NB_SOCKETS 8
147
148 /* Configure how many packets ahead to prefetch, when reading packets */
149 #define PREFETCH_OFFSET 3
150
151 /* Used to mark destination port as 'invalid'. */
152 #define BAD_PORT        ((uint16_t)-1)
153
154 #define FWDSTEP 4
155
156 /*
157  * Configurable number of RX/TX ring descriptors
158  */
159 #define RTE_TEST_RX_DESC_DEFAULT 128
160 #define RTE_TEST_TX_DESC_DEFAULT 512
161 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
162 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
163
164 /* ethernet addresses of ports */
165 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
166 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
167
168 static __m128i val_eth[RTE_MAX_ETHPORTS];
169
170 /* replace first 12B of the ethernet header. */
171 #define MASK_ETH        0x3f
172
173 /* mask of enabled ports */
174 static uint32_t enabled_port_mask = 0;
175 static int promiscuous_on = 0; /**< Ports set in promiscuous mode off by default. */
176 static int numa_on = 1; /**< NUMA is enabled by default. */
177
178 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
179 static int ipv6 = 0; /**< ipv6 is false by default. */
180 #endif
181
182 struct mbuf_table {
183         uint16_t len;
184         struct rte_mbuf *m_table[MAX_PKT_BURST];
185 };
186
187 struct lcore_rx_queue {
188         uint8_t port_id;
189         uint8_t queue_id;
190 } __rte_cache_aligned;
191
192 #define MAX_RX_QUEUE_PER_LCORE 16
193 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
194 #define MAX_RX_QUEUE_PER_PORT 128
195
196 #define MAX_LCORE_PARAMS 1024
197 struct lcore_params {
198         uint8_t port_id;
199         uint8_t queue_id;
200         uint8_t lcore_id;
201 } __rte_cache_aligned;
202
203 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
204 static struct lcore_params lcore_params_array_default[] = {
205         {0, 0, 2},
206         {0, 1, 2},
207         {0, 2, 2},
208         {1, 0, 2},
209         {1, 1, 2},
210         {1, 2, 2},
211         {2, 0, 2},
212         {3, 0, 3},
213         {3, 1, 3},
214 };
215
216 static struct lcore_params * lcore_params = lcore_params_array_default;
217 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
218                                 sizeof(lcore_params_array_default[0]);
219
220 static struct rte_eth_conf port_conf = {
221         .rxmode = {
222                 .mq_mode = ETH_MQ_RX_RSS,
223                 .max_rx_pkt_len = ETHER_MAX_LEN,
224                 .split_hdr_size = 0,
225                 .header_split   = 0, /**< Header Split disabled */
226                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
227                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
228                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
229                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
230         },
231         .rx_adv_conf = {
232                 .rss_conf = {
233                         .rss_key = NULL,
234                         .rss_hf = ETH_RSS_IP,
235                 },
236         },
237         .txmode = {
238                 .mq_mode = ETH_MQ_TX_NONE,
239         },
240 };
241
242 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
243
244 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
245
246 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
247 #include <rte_hash_crc.h>
248 #define DEFAULT_HASH_FUNC       rte_hash_crc
249 #else
250 #include <rte_jhash.h>
251 #define DEFAULT_HASH_FUNC       rte_jhash
252 #endif
253
254 struct ipv4_5tuple {
255         uint32_t ip_dst;
256         uint32_t ip_src;
257         uint16_t port_dst;
258         uint16_t port_src;
259         uint8_t  proto;
260 } __attribute__((__packed__));
261
262 union ipv4_5tuple_host {
263         struct {
264                 uint8_t  pad0;
265                 uint8_t  proto;
266                 uint16_t pad1;
267                 uint32_t ip_src;
268                 uint32_t ip_dst;
269                 uint16_t port_src;
270                 uint16_t port_dst;
271         };
272         __m128i xmm;
273 };
274
275 #define XMM_NUM_IN_IPV6_5TUPLE 3
276
277 struct ipv6_5tuple {
278         uint8_t  ip_dst[IPV6_ADDR_LEN];
279         uint8_t  ip_src[IPV6_ADDR_LEN];
280         uint16_t port_dst;
281         uint16_t port_src;
282         uint8_t  proto;
283 } __attribute__((__packed__));
284
285 union ipv6_5tuple_host {
286         struct {
287                 uint16_t pad0;
288                 uint8_t  proto;
289                 uint8_t  pad1;
290                 uint8_t  ip_src[IPV6_ADDR_LEN];
291                 uint8_t  ip_dst[IPV6_ADDR_LEN];
292                 uint16_t port_src;
293                 uint16_t port_dst;
294                 uint64_t reserve;
295         };
296         __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
297 };
298
299 struct ipv4_l3fwd_route {
300         struct ipv4_5tuple key;
301         uint8_t if_out;
302 };
303
304 struct ipv6_l3fwd_route {
305         struct ipv6_5tuple key;
306         uint8_t if_out;
307 };
308
309 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
310         {{IPv4(101,0,0,0), IPv4(100,10,0,1),  101, 11, IPPROTO_TCP}, 0},
311         {{IPv4(201,0,0,0), IPv4(200,20,0,1),  102, 12, IPPROTO_TCP}, 1},
312         {{IPv4(111,0,0,0), IPv4(100,30,0,1),  101, 11, IPPROTO_TCP}, 2},
313         {{IPv4(211,0,0,0), IPv4(200,40,0,1),  102, 12, IPPROTO_TCP}, 3},
314 };
315
316 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
317         {{
318         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
319         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
320         101, 11, IPPROTO_TCP}, 0},
321
322         {{
323         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
324         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
325         102, 12, IPPROTO_TCP}, 1},
326
327         {{
328         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
329         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
330         101, 11, IPPROTO_TCP}, 2},
331
332         {{
333         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
334         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
335         102, 12, IPPROTO_TCP}, 3},
336 };
337
338 typedef struct rte_hash lookup_struct_t;
339 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
340 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
341
342 #ifdef RTE_ARCH_X86_64
343 /* default to 4 million hash entries (approx) */
344 #define L3FWD_HASH_ENTRIES              1024*1024*4
345 #else
346 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
347 #define L3FWD_HASH_ENTRIES              1024*1024*1
348 #endif
349 #define HASH_ENTRY_NUMBER_DEFAULT       4
350
351 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
352
353 static inline uint32_t
354 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
355         uint32_t init_val)
356 {
357         const union ipv4_5tuple_host *k;
358         uint32_t t;
359         const uint32_t *p;
360
361         k = data;
362         t = k->proto;
363         p = (const uint32_t *)&k->port_src;
364
365 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
366         init_val = rte_hash_crc_4byte(t, init_val);
367         init_val = rte_hash_crc_4byte(k->ip_src, init_val);
368         init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
369         init_val = rte_hash_crc_4byte(*p, init_val);
370 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
371         init_val = rte_jhash_1word(t, init_val);
372         init_val = rte_jhash_1word(k->ip_src, init_val);
373         init_val = rte_jhash_1word(k->ip_dst, init_val);
374         init_val = rte_jhash_1word(*p, init_val);
375 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
376         return (init_val);
377 }
378
379 static inline uint32_t
380 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len, uint32_t init_val)
381 {
382         const union ipv6_5tuple_host *k;
383         uint32_t t;
384         const uint32_t *p;
385 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
386         const uint32_t  *ip_src0, *ip_src1, *ip_src2, *ip_src3;
387         const uint32_t  *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
388 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
389
390         k = data;
391         t = k->proto;
392         p = (const uint32_t *)&k->port_src;
393
394 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
395         ip_src0 = (const uint32_t *) k->ip_src;
396         ip_src1 = (const uint32_t *)(k->ip_src+4);
397         ip_src2 = (const uint32_t *)(k->ip_src+8);
398         ip_src3 = (const uint32_t *)(k->ip_src+12);
399         ip_dst0 = (const uint32_t *) k->ip_dst;
400         ip_dst1 = (const uint32_t *)(k->ip_dst+4);
401         ip_dst2 = (const uint32_t *)(k->ip_dst+8);
402         ip_dst3 = (const uint32_t *)(k->ip_dst+12);
403         init_val = rte_hash_crc_4byte(t, init_val);
404         init_val = rte_hash_crc_4byte(*ip_src0, init_val);
405         init_val = rte_hash_crc_4byte(*ip_src1, init_val);
406         init_val = rte_hash_crc_4byte(*ip_src2, init_val);
407         init_val = rte_hash_crc_4byte(*ip_src3, init_val);
408         init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
409         init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
410         init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
411         init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
412         init_val = rte_hash_crc_4byte(*p, init_val);
413 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
414         init_val = rte_jhash_1word(t, init_val);
415         init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
416         init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
417         init_val = rte_jhash_1word(*p, init_val);
418 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
419         return (init_val);
420 }
421
422 #define IPV4_L3FWD_NUM_ROUTES \
423         (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
424
425 #define IPV6_L3FWD_NUM_ROUTES \
426         (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
427
428 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
429 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
430
431 #endif
432
433 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
434 struct ipv4_l3fwd_route {
435         uint32_t ip;
436         uint8_t  depth;
437         uint8_t  if_out;
438 };
439
440 struct ipv6_l3fwd_route {
441         uint8_t ip[16];
442         uint8_t  depth;
443         uint8_t  if_out;
444 };
445
446 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
447         {IPv4(1,1,1,0), 24, 0},
448         {IPv4(2,1,1,0), 24, 1},
449         {IPv4(3,1,1,0), 24, 2},
450         {IPv4(4,1,1,0), 24, 3},
451         {IPv4(5,1,1,0), 24, 4},
452         {IPv4(6,1,1,0), 24, 5},
453         {IPv4(7,1,1,0), 24, 6},
454         {IPv4(8,1,1,0), 24, 7},
455 };
456
457 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
458         {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 0},
459         {{2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 1},
460         {{3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 2},
461         {{4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 3},
462         {{5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 4},
463         {{6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 5},
464         {{7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 6},
465         {{8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 48, 7},
466 };
467
468 #define IPV4_L3FWD_NUM_ROUTES \
469         (sizeof(ipv4_l3fwd_route_array) / sizeof(ipv4_l3fwd_route_array[0]))
470 #define IPV6_L3FWD_NUM_ROUTES \
471         (sizeof(ipv6_l3fwd_route_array) / sizeof(ipv6_l3fwd_route_array[0]))
472
473 #define IPV4_L3FWD_LPM_MAX_RULES         1024
474 #define IPV6_L3FWD_LPM_MAX_RULES         1024
475 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
476
477 typedef struct rte_lpm lookup_struct_t;
478 typedef struct rte_lpm6 lookup6_struct_t;
479 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
480 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
481 #endif
482
483 struct lcore_conf {
484         uint16_t n_rx_queue;
485         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
486         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
487         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
488         lookup_struct_t * ipv4_lookup_struct;
489 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
490         lookup6_struct_t * ipv6_lookup_struct;
491 #else
492         lookup_struct_t * ipv6_lookup_struct;
493 #endif
494 } __rte_cache_aligned;
495
496 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
497
498 /* Send burst of packets on an output interface */
499 static inline int
500 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
501 {
502         struct rte_mbuf **m_table;
503         int ret;
504         uint16_t queueid;
505
506         queueid = qconf->tx_queue_id[port];
507         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
508
509         ret = rte_eth_tx_burst(port, queueid, m_table, n);
510         if (unlikely(ret < n)) {
511                 do {
512                         rte_pktmbuf_free(m_table[ret]);
513                 } while (++ret < n);
514         }
515
516         return 0;
517 }
518
519 /* Enqueue a single packet, and send burst if queue is filled */
520 static inline int
521 send_single_packet(struct rte_mbuf *m, uint8_t port)
522 {
523         uint32_t lcore_id;
524         uint16_t len;
525         struct lcore_conf *qconf;
526
527         lcore_id = rte_lcore_id();
528
529         qconf = &lcore_conf[lcore_id];
530         len = qconf->tx_mbufs[port].len;
531         qconf->tx_mbufs[port].m_table[len] = m;
532         len++;
533
534         /* enough pkts to be sent */
535         if (unlikely(len == MAX_PKT_BURST)) {
536                 send_burst(qconf, MAX_PKT_BURST, port);
537                 len = 0;
538         }
539
540         qconf->tx_mbufs[port].len = len;
541         return 0;
542 }
543
544 static inline __attribute__((always_inline)) void
545 send_packetsx4(struct lcore_conf *qconf, uint8_t port,
546         struct rte_mbuf *m[], uint32_t num)
547 {
548         uint32_t len, j, n;
549
550         len = qconf->tx_mbufs[port].len;
551
552         /*
553          * If TX buffer for that queue is empty, and we have enough packets,
554          * then send them straightway.
555          */
556         if (num >= MAX_TX_BURST && len == 0) {
557                 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
558                 if (unlikely(n < num)) {
559                         do {
560                                 rte_pktmbuf_free(m[n]);
561                         } while (++n < num);
562                 }
563                 return;
564         }
565
566         /*
567          * Put packets into TX buffer for that queue.
568          */
569
570         n = len + num;
571         n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
572
573         j = 0;
574         switch (n % FWDSTEP) {
575         while (j < n) {
576         case 0:
577                 qconf->tx_mbufs[port].m_table[len + j] = m[j];
578                 j++;
579         case 3:
580                 qconf->tx_mbufs[port].m_table[len + j] = m[j];
581                 j++;
582         case 2:
583                 qconf->tx_mbufs[port].m_table[len + j] = m[j];
584                 j++;
585         case 1:
586                 qconf->tx_mbufs[port].m_table[len + j] = m[j];
587                 j++;
588         }
589         }
590
591         len += n;
592
593         /* enough pkts to be sent */
594         if (unlikely(len == MAX_PKT_BURST)) {
595
596                 send_burst(qconf, MAX_PKT_BURST, port);
597
598                 /* copy rest of the packets into the TX buffer. */
599                 len = num - n;
600                 j = 0;
601                 switch (len % FWDSTEP) {
602                 while (j < len) {
603                 case 0:
604                         qconf->tx_mbufs[port].m_table[j] = m[n + j];
605                         j++;
606                 case 3:
607                         qconf->tx_mbufs[port].m_table[j] = m[n + j];
608                         j++;
609                 case 2:
610                         qconf->tx_mbufs[port].m_table[j] = m[n + j];
611                         j++;
612                 case 1:
613                         qconf->tx_mbufs[port].m_table[j] = m[n + j];
614                         j++;
615                 }
616                 }
617         }
618
619         qconf->tx_mbufs[port].len = len;
620 }
621
622 #ifdef DO_RFC_1812_CHECKS
623 static inline int
624 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
625 {
626         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
627         /*
628          * 1. The packet length reported by the Link Layer must be large
629          * enough to hold the minimum length legal IP datagram (20 bytes).
630          */
631         if (link_len < sizeof(struct ipv4_hdr))
632                 return -1;
633
634         /* 2. The IP checksum must be correct. */
635         /* this is checked in H/W */
636
637         /*
638          * 3. The IP version number must be 4. If the version number is not 4
639          * then the packet may be another version of IP, such as IPng or
640          * ST-II.
641          */
642         if (((pkt->version_ihl) >> 4) != 4)
643                 return -3;
644         /*
645          * 4. The IP header length field must be large enough to hold the
646          * minimum length legal IP datagram (20 bytes = 5 words).
647          */
648         if ((pkt->version_ihl & 0xf) < 5)
649                 return -4;
650
651         /*
652          * 5. The IP total length field must be large enough to hold the IP
653          * datagram header, whose length is specified in the IP header length
654          * field.
655          */
656         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
657                 return -5;
658
659         return 0;
660 }
661 #endif
662
663 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
664
665 static __m128i mask0;
666 static __m128i mask1;
667 static __m128i mask2;
668 static inline uint8_t
669 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
670 {
671         int ret = 0;
672         union ipv4_5tuple_host key;
673
674         ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
675         __m128i data = _mm_loadu_si128((__m128i*)(ipv4_hdr));
676         /* Get 5 tuple: dst port, src port, dst IP address, src IP address and protocol */
677         key.xmm = _mm_and_si128(data, mask0);
678         /* Find destination port */
679         ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
680         return (uint8_t)((ret < 0)? portid : ipv4_l3fwd_out_if[ret]);
681 }
682
683 static inline uint8_t
684 get_ipv6_dst_port(void *ipv6_hdr,  uint8_t portid, lookup_struct_t * ipv6_l3fwd_lookup_struct)
685 {
686         int ret = 0;
687         union ipv6_5tuple_host key;
688
689         ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
690         __m128i data0 = _mm_loadu_si128((__m128i*)(ipv6_hdr));
691         __m128i data1 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)));
692         __m128i data2 = _mm_loadu_si128((__m128i*)(((uint8_t*)ipv6_hdr)+sizeof(__m128i)+sizeof(__m128i)));
693         /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
694         key.xmm[0] = _mm_and_si128(data0, mask1);
695         /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address higher 32 bits */
696         key.xmm[1] = data1;
697         /* Get part of 5 tuple: dst port and src port and dst IP address higher 32 bits */
698         key.xmm[2] = _mm_and_si128(data2, mask2);
699
700         /* Find destination port */
701         ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
702         return (uint8_t)((ret < 0)? portid : ipv6_l3fwd_out_if[ret]);
703 }
704 #endif
705
706 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
707
708 static inline uint8_t
709 get_ipv4_dst_port(void *ipv4_hdr,  uint8_t portid, lookup_struct_t * ipv4_l3fwd_lookup_struct)
710 {
711         uint8_t next_hop;
712
713         return (uint8_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
714                 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
715                 &next_hop) == 0) ? next_hop : portid);
716 }
717
718 static inline uint8_t
719 get_ipv6_dst_port(void *ipv6_hdr,  uint8_t portid, lookup6_struct_t * ipv6_l3fwd_lookup_struct)
720 {
721         uint8_t next_hop;
722         return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
723                         ((struct ipv6_hdr*)ipv6_hdr)->dst_addr, &next_hop) == 0)?
724                         next_hop : portid);
725 }
726 #endif
727
728 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid,
729         struct lcore_conf *qconf)  __attribute__((unused));
730
731 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
732         (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
733
734 #define MASK_ALL_PKTS    0xf
735 #define EXECLUDE_1ST_PKT 0xe
736 #define EXECLUDE_2ND_PKT 0xd
737 #define EXECLUDE_3RD_PKT 0xb
738 #define EXECLUDE_4TH_PKT 0x7
739
740 static inline void
741 simple_ipv4_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *qconf)
742 {
743         struct ether_hdr *eth_hdr[4];
744         struct ipv4_hdr *ipv4_hdr[4];
745         uint8_t dst_port[4];
746         int32_t ret[4];
747         union ipv4_5tuple_host key[4];
748         __m128i data[4];
749
750         eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
751         eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
752         eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
753         eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
754
755         /* Handle IPv4 headers.*/
756         ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
757                                               sizeof(struct ether_hdr));
758         ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
759                                               sizeof(struct ether_hdr));
760         ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
761                                               sizeof(struct ether_hdr));
762         ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
763                                               sizeof(struct ether_hdr));
764
765 #ifdef DO_RFC_1812_CHECKS
766         /* Check to make sure the packet is valid (RFC1812) */
767         uint8_t valid_mask = MASK_ALL_PKTS;
768         if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
769                 rte_pktmbuf_free(m[0]);
770                 valid_mask &= EXECLUDE_1ST_PKT;
771         }
772         if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
773                 rte_pktmbuf_free(m[1]);
774                 valid_mask &= EXECLUDE_2ND_PKT;
775         }
776         if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
777                 rte_pktmbuf_free(m[2]);
778                 valid_mask &= EXECLUDE_3RD_PKT;
779         }
780         if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
781                 rte_pktmbuf_free(m[3]);
782                 valid_mask &= EXECLUDE_4TH_PKT;
783         }
784         if (unlikely(valid_mask != MASK_ALL_PKTS)) {
785                 if (valid_mask == 0){
786                         return;
787                 } else {
788                         uint8_t i = 0;
789                         for (i = 0; i < 4; i++) {
790                                 if ((0x1 << i) & valid_mask) {
791                                         l3fwd_simple_forward(m[i], portid, qconf);
792                                 }
793                         }
794                         return;
795                 }
796         }
797 #endif // End of #ifdef DO_RFC_1812_CHECKS
798
799         data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
800         data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
801         data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
802         data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv4_hdr, time_to_live)));
803
804         key[0].xmm = _mm_and_si128(data[0], mask0);
805         key[1].xmm = _mm_and_si128(data[1], mask0);
806         key[2].xmm = _mm_and_si128(data[2], mask0);
807         key[3].xmm = _mm_and_si128(data[3], mask0);
808
809         const void *key_array[4] = {&key[0], &key[1], &key[2],&key[3]};
810         rte_hash_lookup_multi(qconf->ipv4_lookup_struct, &key_array[0], 4, ret);
811         dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
812         dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
813         dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
814         dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
815
816         if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
817                 dst_port[0] = portid;
818         if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
819                 dst_port[1] = portid;
820         if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
821                 dst_port[2] = portid;
822         if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
823                 dst_port[3] = portid;
824
825 #ifdef DO_RFC_1812_CHECKS
826         /* Update time to live and header checksum */
827         --(ipv4_hdr[0]->time_to_live);
828         --(ipv4_hdr[1]->time_to_live);
829         --(ipv4_hdr[2]->time_to_live);
830         --(ipv4_hdr[3]->time_to_live);
831         ++(ipv4_hdr[0]->hdr_checksum);
832         ++(ipv4_hdr[1]->hdr_checksum);
833         ++(ipv4_hdr[2]->hdr_checksum);
834         ++(ipv4_hdr[3]->hdr_checksum);
835 #endif
836
837         /* dst addr */
838         *(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
839         *(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
840         *(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
841         *(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
842
843         /* src addr */
844         ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
845         ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
846         ether_addr_copy(&ports_eth_addr[dst_port[2]], &eth_hdr[2]->s_addr);
847         ether_addr_copy(&ports_eth_addr[dst_port[3]], &eth_hdr[3]->s_addr);
848
849         send_single_packet(m[0], (uint8_t)dst_port[0]);
850         send_single_packet(m[1], (uint8_t)dst_port[1]);
851         send_single_packet(m[2], (uint8_t)dst_port[2]);
852         send_single_packet(m[3], (uint8_t)dst_port[3]);
853
854 }
855
856 static inline void get_ipv6_5tuple(struct rte_mbuf* m0, __m128i mask0, __m128i mask1,
857                                  union ipv6_5tuple_host * key)
858 {
859         __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len)));
860         __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
861         __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0, __m128i *, sizeof(struct ether_hdr) + offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) + sizeof(__m128i)));
862         key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
863         key->xmm[1] = tmpdata1;
864         key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
865         return;
866 }
867
868 static inline void
869 simple_ipv6_fwd_4pkts(struct rte_mbuf* m[4], uint8_t portid, struct lcore_conf *qconf)
870 {
871         struct ether_hdr *eth_hdr[4];
872         __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[4];
873         uint8_t dst_port[4];
874         int32_t ret[4];
875         union ipv6_5tuple_host key[4];
876
877         eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
878         eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
879         eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
880         eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
881
882         /* Handle IPv6 headers.*/
883         ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
884                                               sizeof(struct ether_hdr));
885         ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
886                                               sizeof(struct ether_hdr));
887         ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
888                                               sizeof(struct ether_hdr));
889         ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
890                                               sizeof(struct ether_hdr));
891
892         get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
893         get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
894         get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
895         get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
896
897         const void *key_array[4] = {&key[0], &key[1], &key[2],&key[3]};
898         rte_hash_lookup_multi(qconf->ipv6_lookup_struct, &key_array[0], 4, ret);
899         dst_port[0] = (uint8_t) ((ret[0] < 0)? portid:ipv6_l3fwd_out_if[ret[0]]);
900         dst_port[1] = (uint8_t) ((ret[1] < 0)? portid:ipv6_l3fwd_out_if[ret[1]]);
901         dst_port[2] = (uint8_t) ((ret[2] < 0)? portid:ipv6_l3fwd_out_if[ret[2]]);
902         dst_port[3] = (uint8_t) ((ret[3] < 0)? portid:ipv6_l3fwd_out_if[ret[3]]);
903
904         if (dst_port[0] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[0]) == 0)
905                 dst_port[0] = portid;
906         if (dst_port[1] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[1]) == 0)
907                 dst_port[1] = portid;
908         if (dst_port[2] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[2]) == 0)
909                 dst_port[2] = portid;
910         if (dst_port[3] >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port[3]) == 0)
911                 dst_port[3] = portid;
912
913         /* dst addr */
914         *(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
915         *(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
916         *(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
917         *(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
918
919         /* src addr */
920         ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
921         ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
922         ether_addr_copy(&ports_eth_addr[dst_port[2]], &eth_hdr[2]->s_addr);
923         ether_addr_copy(&ports_eth_addr[dst_port[3]], &eth_hdr[3]->s_addr);
924
925         send_single_packet(m[0], (uint8_t)dst_port[0]);
926         send_single_packet(m[1], (uint8_t)dst_port[1]);
927         send_single_packet(m[2], (uint8_t)dst_port[2]);
928         send_single_packet(m[3], (uint8_t)dst_port[3]);
929
930 }
931 #endif /* APP_LOOKUP_METHOD */
932
933 static inline __attribute__((always_inline)) void
934 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid, struct lcore_conf *qconf)
935 {
936         struct ether_hdr *eth_hdr;
937         struct ipv4_hdr *ipv4_hdr;
938         uint8_t dst_port;
939
940         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
941
942 #ifdef RTE_NEXT_ABI
943         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
944 #else
945         if (m->ol_flags & PKT_RX_IPV4_HDR) {
946 #endif
947                 /* Handle IPv4 headers.*/
948                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
949                                                    sizeof(struct ether_hdr));
950
951 #ifdef DO_RFC_1812_CHECKS
952                 /* Check to make sure the packet is valid (RFC1812) */
953                 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
954                         rte_pktmbuf_free(m);
955                         return;
956                 }
957 #endif
958
959                  dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
960                         qconf->ipv4_lookup_struct);
961                 if (dst_port >= RTE_MAX_ETHPORTS ||
962                                 (enabled_port_mask & 1 << dst_port) == 0)
963                         dst_port = portid;
964
965 #ifdef DO_RFC_1812_CHECKS
966                 /* Update time to live and header checksum */
967                 --(ipv4_hdr->time_to_live);
968                 ++(ipv4_hdr->hdr_checksum);
969 #endif
970                 /* dst addr */
971                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
972
973                 /* src addr */
974                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
975
976                 send_single_packet(m, dst_port);
977 #ifdef RTE_NEXT_ABI
978         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
979 #else
980         } else {
981 #endif
982                 /* Handle IPv6 headers.*/
983                 struct ipv6_hdr *ipv6_hdr;
984
985                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
986                                                    sizeof(struct ether_hdr));
987
988                 dst_port = get_ipv6_dst_port(ipv6_hdr, portid, qconf->ipv6_lookup_struct);
989
990                 if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
991                         dst_port = portid;
992
993                 /* dst addr */
994                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
995
996                 /* src addr */
997                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
998
999                 send_single_packet(m, dst_port);
1000 #ifdef RTE_NEXT_ABI
1001         } else
1002                 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1003                 rte_pktmbuf_free(m);
1004 #else
1005         }
1006 #endif
1007 }
1008
1009 #ifdef DO_RFC_1812_CHECKS
1010
1011 #define IPV4_MIN_VER_IHL        0x45
1012 #define IPV4_MAX_VER_IHL        0x4f
1013 #define IPV4_MAX_VER_IHL_DIFF   (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1014
1015 /* Minimum value of IPV4 total length (20B) in network byte order. */
1016 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1017
1018 /*
1019  * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1020  * - The IP version number must be 4.
1021  * - The IP header length field must be large enough to hold the
1022  *    minimum length legal IP datagram (20 bytes = 5 words).
1023  * - The IP total length field must be large enough to hold the IP
1024  *   datagram header, whose length is specified in the IP header length
1025  *   field.
1026  * If we encounter invalid IPV4 packet, then set destination port for it
1027  * to BAD_PORT value.
1028  */
1029 static inline __attribute__((always_inline)) void
1030 #ifdef RTE_NEXT_ABI
1031 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1032 #else
1033 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t flags)
1034 #endif
1035 {
1036         uint8_t ihl;
1037
1038 #ifdef RTE_NEXT_ABI
1039         if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1040 #else
1041         if ((flags & PKT_RX_IPV4_HDR) != 0) {
1042 #endif
1043                 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1044
1045                 ipv4_hdr->time_to_live--;
1046                 ipv4_hdr->hdr_checksum++;
1047
1048                 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1049                                 ((uint8_t)ipv4_hdr->total_length == 0 &&
1050                                 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1051                         dp[0] = BAD_PORT;
1052                 }
1053         }
1054 }
1055
1056 #else
1057 #define rfc1812_process(mb, dp) do { } while (0)
1058 #endif /* DO_RFC_1812_CHECKS */
1059
1060
1061 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1062         (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1063
1064 static inline __attribute__((always_inline)) uint16_t
1065 get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
1066         uint32_t dst_ipv4, uint8_t portid)
1067 {
1068         uint8_t next_hop;
1069         struct ipv6_hdr *ipv6_hdr;
1070         struct ether_hdr *eth_hdr;
1071
1072 #ifdef RTE_NEXT_ABI
1073         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1074 #else
1075         if (pkt->ol_flags & PKT_RX_IPV4_HDR) {
1076 #endif
1077                 if (rte_lpm_lookup(qconf->ipv4_lookup_struct, dst_ipv4,
1078                                 &next_hop) != 0)
1079                         next_hop = portid;
1080 #ifdef RTE_NEXT_ABI
1081         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1082 #else
1083         } else if (pkt->ol_flags & PKT_RX_IPV6_HDR) {
1084 #endif
1085                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1086                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1087                 if (rte_lpm6_lookup(qconf->ipv6_lookup_struct,
1088                                 ipv6_hdr->dst_addr, &next_hop) != 0)
1089                         next_hop = portid;
1090         } else {
1091                 next_hop = portid;
1092         }
1093
1094         return next_hop;
1095 }
1096
1097 static inline void
1098 process_packet(struct lcore_conf *qconf, struct rte_mbuf *pkt,
1099         uint16_t *dst_port, uint8_t portid)
1100 {
1101         struct ether_hdr *eth_hdr;
1102         struct ipv4_hdr *ipv4_hdr;
1103         uint32_t dst_ipv4;
1104         uint16_t dp;
1105         __m128i te, ve;
1106
1107         eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1108         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1109
1110         dst_ipv4 = ipv4_hdr->dst_addr;
1111         dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1112         dp = get_dst_port(qconf, pkt, dst_ipv4, portid);
1113
1114         te = _mm_load_si128((__m128i *)eth_hdr);
1115         ve = val_eth[dp];
1116
1117         dst_port[0] = dp;
1118 #ifdef RTE_NEXT_ABI
1119         rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1120 #else
1121         rfc1812_process(ipv4_hdr, dst_port, pkt->ol_flags);
1122 #endif
1123
1124         te =  _mm_blend_epi16(te, ve, MASK_ETH);
1125         _mm_store_si128((__m128i *)eth_hdr, te);
1126 }
1127
1128 #ifdef RTE_NEXT_ABI
1129 /*
1130  * Read packet_type and destination IPV4 addresses from 4 mbufs.
1131  */
1132 static inline void
1133 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1134                 __m128i *dip,
1135                 uint32_t *ipv4_flag)
1136 {
1137         struct ipv4_hdr *ipv4_hdr;
1138         struct ether_hdr *eth_hdr;
1139         uint32_t x0, x1, x2, x3;
1140
1141         eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1142         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1143         x0 = ipv4_hdr->dst_addr;
1144         ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1145
1146         eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1147         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1148         x1 = ipv4_hdr->dst_addr;
1149         ipv4_flag[0] &= pkt[1]->packet_type;
1150
1151         eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1152         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1153         x2 = ipv4_hdr->dst_addr;
1154         ipv4_flag[0] &= pkt[2]->packet_type;
1155
1156         eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1157         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1158         x3 = ipv4_hdr->dst_addr;
1159         ipv4_flag[0] &= pkt[3]->packet_type;
1160
1161         dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1162 }
1163 #else /* RTE_NEXT_ABI */
1164 /*
1165  * Read ol_flags and destination IPV4 addresses from 4 mbufs.
1166  */
1167 static inline void
1168 processx4_step1(struct rte_mbuf *pkt[FWDSTEP], __m128i *dip, uint32_t *flag)
1169 {
1170         struct ipv4_hdr *ipv4_hdr;
1171         struct ether_hdr *eth_hdr;
1172         uint32_t x0, x1, x2, x3;
1173
1174         eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1175         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1176         x0 = ipv4_hdr->dst_addr;
1177         flag[0] = pkt[0]->ol_flags & PKT_RX_IPV4_HDR;
1178
1179         eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1180         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1181         x1 = ipv4_hdr->dst_addr;
1182         flag[0] &= pkt[1]->ol_flags;
1183
1184         eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1185         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1186         x2 = ipv4_hdr->dst_addr;
1187         flag[0] &= pkt[2]->ol_flags;
1188
1189         eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1190         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1191         x3 = ipv4_hdr->dst_addr;
1192         flag[0] &= pkt[3]->ol_flags;
1193
1194         dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1195 }
1196 #endif /* RTE_NEXT_ABI */
1197
1198 /*
1199  * Lookup into LPM for destination port.
1200  * If lookup fails, use incoming port (portid) as destination port.
1201  */
1202 static inline void
1203 #ifdef RTE_NEXT_ABI
1204 processx4_step2(const struct lcore_conf *qconf,
1205                 __m128i dip,
1206                 uint32_t ipv4_flag,
1207                 uint8_t portid,
1208                 struct rte_mbuf *pkt[FWDSTEP],
1209                 uint16_t dprt[FWDSTEP])
1210 #else
1211 processx4_step2(const struct lcore_conf *qconf, __m128i dip, uint32_t flag,
1212         uint8_t portid, struct rte_mbuf *pkt[FWDSTEP], uint16_t dprt[FWDSTEP])
1213 #endif /* RTE_NEXT_ABI */
1214 {
1215         rte_xmm_t dst;
1216         const  __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1217                                                 4, 5, 6, 7, 0, 1, 2, 3);
1218
1219         /* Byte swap 4 IPV4 addresses. */
1220         dip = _mm_shuffle_epi8(dip, bswap_mask);
1221
1222         /* if all 4 packets are IPV4. */
1223 #ifdef RTE_NEXT_ABI
1224         if (likely(ipv4_flag)) {
1225 #else
1226         if (likely(flag != 0)) {
1227 #endif
1228                 rte_lpm_lookupx4(qconf->ipv4_lookup_struct, dip, dprt, portid);
1229         } else {
1230                 dst.x = dip;
1231                 dprt[0] = get_dst_port(qconf, pkt[0], dst.u32[0], portid);
1232                 dprt[1] = get_dst_port(qconf, pkt[1], dst.u32[1], portid);
1233                 dprt[2] = get_dst_port(qconf, pkt[2], dst.u32[2], portid);
1234                 dprt[3] = get_dst_port(qconf, pkt[3], dst.u32[3], portid);
1235         }
1236 }
1237
1238 /*
1239  * Update source and destination MAC addresses in the ethernet header.
1240  * Perform RFC1812 checks and updates for IPV4 packets.
1241  */
1242 static inline void
1243 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1244 {
1245         __m128i te[FWDSTEP];
1246         __m128i ve[FWDSTEP];
1247         __m128i *p[FWDSTEP];
1248
1249         p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1250         p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1251         p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1252         p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1253
1254         ve[0] = val_eth[dst_port[0]];
1255         te[0] = _mm_load_si128(p[0]);
1256
1257         ve[1] = val_eth[dst_port[1]];
1258         te[1] = _mm_load_si128(p[1]);
1259
1260         ve[2] = val_eth[dst_port[2]];
1261         te[2] = _mm_load_si128(p[2]);
1262
1263         ve[3] = val_eth[dst_port[3]];
1264         te[3] = _mm_load_si128(p[3]);
1265
1266         /* Update first 12 bytes, keep rest bytes intact. */
1267         te[0] =  _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1268         te[1] =  _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1269         te[2] =  _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1270         te[3] =  _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1271
1272         _mm_store_si128(p[0], te[0]);
1273         _mm_store_si128(p[1], te[1]);
1274         _mm_store_si128(p[2], te[2]);
1275         _mm_store_si128(p[3], te[3]);
1276
1277 #ifdef RTE_NEXT_ABI
1278         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1279                 &dst_port[0], pkt[0]->packet_type);
1280         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1281                 &dst_port[1], pkt[1]->packet_type);
1282         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1283                 &dst_port[2], pkt[2]->packet_type);
1284         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1285                 &dst_port[3], pkt[3]->packet_type);
1286 #else /* RTE_NEXT_ABI */
1287         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1288                 &dst_port[0], pkt[0]->ol_flags);
1289         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1290                 &dst_port[1], pkt[1]->ol_flags);
1291         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1292                 &dst_port[2], pkt[2]->ol_flags);
1293         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1294                 &dst_port[3], pkt[3]->ol_flags);
1295 #endif /* RTE_NEXT_ABI */
1296 }
1297
1298 /*
1299  * We group consecutive packets with the same destionation port into one burst.
1300  * To avoid extra latency this is done together with some other packet
1301  * processing, but after we made a final decision about packet's destination.
1302  * To do this we maintain:
1303  * pnum - array of number of consecutive packets with the same dest port for
1304  * each packet in the input burst.
1305  * lp - pointer to the last updated element in the pnum.
1306  * dlp - dest port value lp corresponds to.
1307  */
1308
1309 #define GRPSZ   (1 << FWDSTEP)
1310 #define GRPMSK  (GRPSZ - 1)
1311
1312 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx)  do { \
1313         if (likely((dlp) == (dcp)[(idx)])) {         \
1314                 (lp)[0]++;                           \
1315         } else {                                     \
1316                 (dlp) = (dcp)[idx];                  \
1317                 (lp) = (pn) + (idx);                 \
1318                 (lp)[0] = 1;                         \
1319         }                                            \
1320 } while (0)
1321
1322 /*
1323  * Group consecutive packets with the same destination port in bursts of 4.
1324  * Suppose we have array of destionation ports:
1325  * dst_port[] = {a, b, c, d,, e, ... }
1326  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1327  * We doing 4 comparisions at once and the result is 4 bit mask.
1328  * This mask is used as an index into prebuild array of pnum values.
1329  */
1330 static inline uint16_t *
1331 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1332 {
1333         static const struct {
1334                 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1335                 int32_t  idx;  /* index for new last updated elemnet. */
1336                 uint16_t lpv;  /* add value to the last updated element. */
1337         } gptbl[GRPSZ] = {
1338         {
1339                 /* 0: a != b, b != c, c != d, d != e */
1340                 .pnum = UINT64_C(0x0001000100010001),
1341                 .idx = 4,
1342                 .lpv = 0,
1343         },
1344         {
1345                 /* 1: a == b, b != c, c != d, d != e */
1346                 .pnum = UINT64_C(0x0001000100010002),
1347                 .idx = 4,
1348                 .lpv = 1,
1349         },
1350         {
1351                 /* 2: a != b, b == c, c != d, d != e */
1352                 .pnum = UINT64_C(0x0001000100020001),
1353                 .idx = 4,
1354                 .lpv = 0,
1355         },
1356         {
1357                 /* 3: a == b, b == c, c != d, d != e */
1358                 .pnum = UINT64_C(0x0001000100020003),
1359                 .idx = 4,
1360                 .lpv = 2,
1361         },
1362         {
1363                 /* 4: a != b, b != c, c == d, d != e */
1364                 .pnum = UINT64_C(0x0001000200010001),
1365                 .idx = 4,
1366                 .lpv = 0,
1367         },
1368         {
1369                 /* 5: a == b, b != c, c == d, d != e */
1370                 .pnum = UINT64_C(0x0001000200010002),
1371                 .idx = 4,
1372                 .lpv = 1,
1373         },
1374         {
1375                 /* 6: a != b, b == c, c == d, d != e */
1376                 .pnum = UINT64_C(0x0001000200030001),
1377                 .idx = 4,
1378                 .lpv = 0,
1379         },
1380         {
1381                 /* 7: a == b, b == c, c == d, d != e */
1382                 .pnum = UINT64_C(0x0001000200030004),
1383                 .idx = 4,
1384                 .lpv = 3,
1385         },
1386         {
1387                 /* 8: a != b, b != c, c != d, d == e */
1388                 .pnum = UINT64_C(0x0002000100010001),
1389                 .idx = 3,
1390                 .lpv = 0,
1391         },
1392         {
1393                 /* 9: a == b, b != c, c != d, d == e */
1394                 .pnum = UINT64_C(0x0002000100010002),
1395                 .idx = 3,
1396                 .lpv = 1,
1397         },
1398         {
1399                 /* 0xa: a != b, b == c, c != d, d == e */
1400                 .pnum = UINT64_C(0x0002000100020001),
1401                 .idx = 3,
1402                 .lpv = 0,
1403         },
1404         {
1405                 /* 0xb: a == b, b == c, c != d, d == e */
1406                 .pnum = UINT64_C(0x0002000100020003),
1407                 .idx = 3,
1408                 .lpv = 2,
1409         },
1410         {
1411                 /* 0xc: a != b, b != c, c == d, d == e */
1412                 .pnum = UINT64_C(0x0002000300010001),
1413                 .idx = 2,
1414                 .lpv = 0,
1415         },
1416         {
1417                 /* 0xd: a == b, b != c, c == d, d == e */
1418                 .pnum = UINT64_C(0x0002000300010002),
1419                 .idx = 2,
1420                 .lpv = 1,
1421         },
1422         {
1423                 /* 0xe: a != b, b == c, c == d, d == e */
1424                 .pnum = UINT64_C(0x0002000300040001),
1425                 .idx = 1,
1426                 .lpv = 0,
1427         },
1428         {
1429                 /* 0xf: a == b, b == c, c == d, d == e */
1430                 .pnum = UINT64_C(0x0002000300040005),
1431                 .idx = 0,
1432                 .lpv = 4,
1433         },
1434         };
1435
1436         union {
1437                 uint16_t u16[FWDSTEP + 1];
1438                 uint64_t u64;
1439         } *pnum = (void *)pn;
1440
1441         int32_t v;
1442
1443         dp1 = _mm_cmpeq_epi16(dp1, dp2);
1444         dp1 = _mm_unpacklo_epi16(dp1, dp1);
1445         v = _mm_movemask_ps((__m128)dp1);
1446
1447         /* update last port counter. */
1448         lp[0] += gptbl[v].lpv;
1449
1450         /* if dest port value has changed. */
1451         if (v != GRPMSK) {
1452                 lp = pnum->u16 + gptbl[v].idx;
1453                 lp[0] = 1;
1454                 pnum->u64 = gptbl[v].pnum;
1455         }
1456
1457         return lp;
1458 }
1459
1460 #endif /* APP_LOOKUP_METHOD */
1461
1462 /* main processing loop */
1463 static int
1464 main_loop(__attribute__((unused)) void *dummy)
1465 {
1466         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
1467         unsigned lcore_id;
1468         uint64_t prev_tsc, diff_tsc, cur_tsc;
1469         int i, j, nb_rx;
1470         uint8_t portid, queueid;
1471         struct lcore_conf *qconf;
1472         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1473                 US_PER_S * BURST_TX_DRAIN_US;
1474
1475 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1476         (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1477         int32_t k;
1478         uint16_t dlp;
1479         uint16_t *lp;
1480         uint16_t dst_port[MAX_PKT_BURST];
1481         __m128i dip[MAX_PKT_BURST / FWDSTEP];
1482 #ifdef RTE_NEXT_ABI
1483         uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1484 #else
1485         uint32_t flag[MAX_PKT_BURST / FWDSTEP];
1486 #endif
1487         uint16_t pnum[MAX_PKT_BURST + 1];
1488 #endif
1489
1490         prev_tsc = 0;
1491
1492         lcore_id = rte_lcore_id();
1493         qconf = &lcore_conf[lcore_id];
1494
1495         if (qconf->n_rx_queue == 0) {
1496                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
1497                 return 0;
1498         }
1499
1500         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
1501
1502         for (i = 0; i < qconf->n_rx_queue; i++) {
1503
1504                 portid = qconf->rx_queue_list[i].port_id;
1505                 queueid = qconf->rx_queue_list[i].queue_id;
1506                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n", lcore_id,
1507                         portid, queueid);
1508         }
1509
1510         while (1) {
1511
1512                 cur_tsc = rte_rdtsc();
1513
1514                 /*
1515                  * TX burst queue drain
1516                  */
1517                 diff_tsc = cur_tsc - prev_tsc;
1518                 if (unlikely(diff_tsc > drain_tsc)) {
1519
1520                         /*
1521                          * This could be optimized (use queueid instead of
1522                          * portid), but it is not called so often
1523                          */
1524                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1525                                 if (qconf->tx_mbufs[portid].len == 0)
1526                                         continue;
1527                                 send_burst(qconf,
1528                                         qconf->tx_mbufs[portid].len,
1529                                         portid);
1530                                 qconf->tx_mbufs[portid].len = 0;
1531                         }
1532
1533                         prev_tsc = cur_tsc;
1534                 }
1535
1536                 /*
1537                  * Read packet from RX queues
1538                  */
1539                 for (i = 0; i < qconf->n_rx_queue; ++i) {
1540                         portid = qconf->rx_queue_list[i].port_id;
1541                         queueid = qconf->rx_queue_list[i].queue_id;
1542                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
1543                                 MAX_PKT_BURST);
1544                         if (nb_rx == 0)
1545                                 continue;
1546
1547 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1548 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1549                         {
1550                                 /*
1551                                  * Send nb_rx - nb_rx%4 packets
1552                                  * in groups of 4.
1553                                  */
1554                                 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 4);
1555                                 for (j = 0; j < n ; j+=4) {
1556 #ifdef RTE_NEXT_ABI
1557                                         uint32_t pkt_type =
1558                                                 pkts_burst[j]->packet_type &
1559                                                 pkts_burst[j+1]->packet_type &
1560                                                 pkts_burst[j+2]->packet_type &
1561                                                 pkts_burst[j+3]->packet_type;
1562                                         if (pkt_type & RTE_PTYPE_L3_IPV4) {
1563                                                 simple_ipv4_fwd_4pkts(
1564                                                 &pkts_burst[j], portid, qconf);
1565                                         } else if (pkt_type &
1566                                                 RTE_PTYPE_L3_IPV6) {
1567 #else /* RTE_NEXT_ABI */
1568                                         uint32_t ol_flag = pkts_burst[j]->ol_flags
1569                                                         & pkts_burst[j+1]->ol_flags
1570                                                         & pkts_burst[j+2]->ol_flags
1571                                                         & pkts_burst[j+3]->ol_flags;
1572                                         if (ol_flag & PKT_RX_IPV4_HDR ) {
1573                                                 simple_ipv4_fwd_4pkts(&pkts_burst[j],
1574                                                                         portid, qconf);
1575                                         } else if (ol_flag & PKT_RX_IPV6_HDR) {
1576 #endif /* RTE_NEXT_ABI */
1577                                                 simple_ipv6_fwd_4pkts(&pkts_burst[j],
1578                                                                         portid, qconf);
1579                                         } else {
1580                                                 l3fwd_simple_forward(pkts_burst[j],
1581                                                                         portid, qconf);
1582                                                 l3fwd_simple_forward(pkts_burst[j+1],
1583                                                                         portid, qconf);
1584                                                 l3fwd_simple_forward(pkts_burst[j+2],
1585                                                                         portid, qconf);
1586                                                 l3fwd_simple_forward(pkts_burst[j+3],
1587                                                                         portid, qconf);
1588                                         }
1589                                 }
1590                                 for (; j < nb_rx ; j++) {
1591                                         l3fwd_simple_forward(pkts_burst[j],
1592                                                                 portid, qconf);
1593                                 }
1594                         }
1595 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1596
1597                         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1598                         for (j = 0; j != k; j += FWDSTEP) {
1599                                 processx4_step1(&pkts_burst[j],
1600                                         &dip[j / FWDSTEP],
1601 #ifdef RTE_NEXT_ABI
1602                                         &ipv4_flag[j / FWDSTEP]);
1603 #else
1604                                         &flag[j / FWDSTEP]);
1605 #endif
1606                         }
1607
1608                         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1609                         for (j = 0; j != k; j += FWDSTEP) {
1610                                 processx4_step2(qconf, dip[j / FWDSTEP],
1611 #ifdef RTE_NEXT_ABI
1612                                         ipv4_flag[j / FWDSTEP], portid,
1613 #else
1614                                         flag[j / FWDSTEP], portid,
1615 #endif
1616                                         &pkts_burst[j], &dst_port[j]);
1617                         }
1618
1619                         /*
1620                          * Finish packet processing and group consecutive
1621                          * packets with the same destination port.
1622                          */
1623                         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1624                         if (k != 0) {
1625                                 __m128i dp1, dp2;
1626
1627                                 lp = pnum;
1628                                 lp[0] = 1;
1629
1630                                 processx4_step3(pkts_burst, dst_port);
1631
1632                                 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1633                                 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1634
1635                                 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1636                                         processx4_step3(&pkts_burst[j],
1637                                                 &dst_port[j]);
1638
1639                                         /*
1640                                          * dp2:
1641                                          * <d[j-3], d[j-2], d[j-1], d[j], ... >
1642                                          */
1643                                         dp2 = _mm_loadu_si128((__m128i *)
1644                                                 &dst_port[j - FWDSTEP + 1]);
1645                                         lp  = port_groupx4(&pnum[j - FWDSTEP],
1646                                                 lp, dp1, dp2);
1647
1648                                         /*
1649                                          * dp1:
1650                                          * <d[j], d[j+1], d[j+2], d[j+3], ... >
1651                                          */
1652                                         dp1 = _mm_srli_si128(dp2,
1653                                                 (FWDSTEP - 1) *
1654                                                 sizeof(dst_port[0]));
1655                                 }
1656
1657                                 /*
1658                                  * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1659                                  */
1660                                 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1661                                 lp  = port_groupx4(&pnum[j - FWDSTEP], lp,
1662                                         dp1, dp2);
1663
1664                                 /*
1665                                  * remove values added by the last repeated
1666                                  * dst port.
1667                                  */
1668                                 lp[0]--;
1669                                 dlp = dst_port[j - 1];
1670                         } else {
1671                                 /* set dlp and lp to the never used values. */
1672                                 dlp = BAD_PORT - 1;
1673                                 lp = pnum + MAX_PKT_BURST;
1674                         }
1675
1676                         /* Process up to last 3 packets one by one. */
1677                         switch (nb_rx % FWDSTEP) {
1678                         case 3:
1679                                 process_packet(qconf, pkts_burst[j],
1680                                         dst_port + j, portid);
1681                                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1682                                 j++;
1683                         case 2:
1684                                 process_packet(qconf, pkts_burst[j],
1685                                         dst_port + j, portid);
1686                                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1687                                 j++;
1688                         case 1:
1689                                 process_packet(qconf, pkts_burst[j],
1690                                         dst_port + j, portid);
1691                                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1692                                 j++;
1693                         }
1694
1695                         /*
1696                          * Send packets out, through destination port.
1697                          * Consecuteve pacekts with the same destination port
1698                          * are already grouped together.
1699                          * If destination port for the packet equals BAD_PORT,
1700                          * then free the packet without sending it out.
1701                          */
1702                         for (j = 0; j < nb_rx; j += k) {
1703
1704                                 int32_t m;
1705                                 uint16_t pn;
1706
1707                                 pn = dst_port[j];
1708                                 k = pnum[j];
1709
1710                                 if (likely(pn != BAD_PORT)) {
1711                                         send_packetsx4(qconf, pn,
1712                                                 pkts_burst + j, k);
1713                                 } else {
1714                                         for (m = j; m != j + k; m++)
1715                                                 rte_pktmbuf_free(pkts_burst[m]);
1716                                 }
1717                         }
1718
1719 #endif /* APP_LOOKUP_METHOD */
1720 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1721
1722                         /* Prefetch first packets */
1723                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
1724                                 rte_prefetch0(rte_pktmbuf_mtod(
1725                                                 pkts_burst[j], void *));
1726                         }
1727
1728                         /* Prefetch and forward already prefetched packets */
1729                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1730                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1731                                                 j + PREFETCH_OFFSET], void *));
1732                                 l3fwd_simple_forward(pkts_burst[j], portid,
1733                                         qconf);
1734                         }
1735
1736                         /* Forward remaining prefetched packets */
1737                         for (; j < nb_rx; j++) {
1738                                 l3fwd_simple_forward(pkts_burst[j], portid,
1739                                         qconf);
1740                         }
1741 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1742
1743                 }
1744         }
1745 }
1746
1747 static int
1748 check_lcore_params(void)
1749 {
1750         uint8_t queue, lcore;
1751         uint16_t i;
1752         int socketid;
1753
1754         for (i = 0; i < nb_lcore_params; ++i) {
1755                 queue = lcore_params[i].queue_id;
1756                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
1757                         printf("invalid queue number: %hhu\n", queue);
1758                         return -1;
1759                 }
1760                 lcore = lcore_params[i].lcore_id;
1761                 if (!rte_lcore_is_enabled(lcore)) {
1762                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
1763                         return -1;
1764                 }
1765                 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
1766                         (numa_on == 0)) {
1767                         printf("warning: lcore %hhu is on socket %d with numa off \n",
1768                                 lcore, socketid);
1769                 }
1770         }
1771         return 0;
1772 }
1773
1774 static int
1775 check_port_config(const unsigned nb_ports)
1776 {
1777         unsigned portid;
1778         uint16_t i;
1779
1780         for (i = 0; i < nb_lcore_params; ++i) {
1781                 portid = lcore_params[i].port_id;
1782                 if ((enabled_port_mask & (1 << portid)) == 0) {
1783                         printf("port %u is not enabled in port mask\n", portid);
1784                         return -1;
1785                 }
1786                 if (portid >= nb_ports) {
1787                         printf("port %u is not present on the board\n", portid);
1788                         return -1;
1789                 }
1790         }
1791         return 0;
1792 }
1793
1794 static uint8_t
1795 get_port_n_rx_queues(const uint8_t port)
1796 {
1797         int queue = -1;
1798         uint16_t i;
1799
1800         for (i = 0; i < nb_lcore_params; ++i) {
1801                 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
1802                         queue = lcore_params[i].queue_id;
1803         }
1804         return (uint8_t)(++queue);
1805 }
1806
1807 static int
1808 init_lcore_rx_queues(void)
1809 {
1810         uint16_t i, nb_rx_queue;
1811         uint8_t lcore;
1812
1813         for (i = 0; i < nb_lcore_params; ++i) {
1814                 lcore = lcore_params[i].lcore_id;
1815                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
1816                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1817                         printf("error: too many queues (%u) for lcore: %u\n",
1818                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
1819                         return -1;
1820                 } else {
1821                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1822                                 lcore_params[i].port_id;
1823                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1824                                 lcore_params[i].queue_id;
1825                         lcore_conf[lcore].n_rx_queue++;
1826                 }
1827         }
1828         return 0;
1829 }
1830
1831 /* display usage */
1832 static void
1833 print_usage(const char *prgname)
1834 {
1835         printf ("%s [EAL options] -- -p PORTMASK -P"
1836                 "  [--config (port,queue,lcore)[,(port,queue,lcore]]"
1837                 "  [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
1838                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
1839                 "  -P : enable promiscuous mode\n"
1840                 "  --config (port,queue,lcore): rx queues configuration\n"
1841                 "  --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
1842                 "  --no-numa: optional, disable numa awareness\n"
1843                 "  --ipv6: optional, specify it if running ipv6 packets\n"
1844                 "  --enable-jumbo: enable jumbo frame"
1845                 " which max packet len is PKTLEN in decimal (64-9600)\n"
1846                 "  --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n",
1847                 prgname);
1848 }
1849
1850 static int parse_max_pkt_len(const char *pktlen)
1851 {
1852         char *end = NULL;
1853         unsigned long len;
1854
1855         /* parse decimal string */
1856         len = strtoul(pktlen, &end, 10);
1857         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
1858                 return -1;
1859
1860         if (len == 0)
1861                 return -1;
1862
1863         return len;
1864 }
1865
1866 static int
1867 parse_portmask(const char *portmask)
1868 {
1869         char *end = NULL;
1870         unsigned long pm;
1871
1872         /* parse hexadecimal string */
1873         pm = strtoul(portmask, &end, 16);
1874         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1875                 return -1;
1876
1877         if (pm == 0)
1878                 return -1;
1879
1880         return pm;
1881 }
1882
1883 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1884 static int
1885 parse_hash_entry_number(const char *hash_entry_num)
1886 {
1887         char *end = NULL;
1888         unsigned long hash_en;
1889         /* parse hexadecimal string */
1890         hash_en = strtoul(hash_entry_num, &end, 16);
1891         if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
1892                 return -1;
1893
1894         if (hash_en == 0)
1895                 return -1;
1896
1897         return hash_en;
1898 }
1899 #endif
1900
1901 static int
1902 parse_config(const char *q_arg)
1903 {
1904         char s[256];
1905         const char *p, *p0 = q_arg;
1906         char *end;
1907         enum fieldnames {
1908                 FLD_PORT = 0,
1909                 FLD_QUEUE,
1910                 FLD_LCORE,
1911                 _NUM_FLD
1912         };
1913         unsigned long int_fld[_NUM_FLD];
1914         char *str_fld[_NUM_FLD];
1915         int i;
1916         unsigned size;
1917
1918         nb_lcore_params = 0;
1919
1920         while ((p = strchr(p0,'(')) != NULL) {
1921                 ++p;
1922                 if((p0 = strchr(p,')')) == NULL)
1923                         return -1;
1924
1925                 size = p0 - p;
1926                 if(size >= sizeof(s))
1927                         return -1;
1928
1929                 snprintf(s, sizeof(s), "%.*s", size, p);
1930                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
1931                         return -1;
1932                 for (i = 0; i < _NUM_FLD; i++){
1933                         errno = 0;
1934                         int_fld[i] = strtoul(str_fld[i], &end, 0);
1935                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1936                                 return -1;
1937                 }
1938                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1939                         printf("exceeded max number of lcore params: %hu\n",
1940                                 nb_lcore_params);
1941                         return -1;
1942                 }
1943                 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
1944                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
1945                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
1946                 ++nb_lcore_params;
1947         }
1948         lcore_params = lcore_params_array;
1949         return 0;
1950 }
1951
1952 static void
1953 parse_eth_dest(const char *optarg)
1954 {
1955         uint8_t portid;
1956         char *port_end;
1957         uint8_t c, *dest, peer_addr[6];
1958
1959         errno = 0;
1960         portid = strtoul(optarg, &port_end, 10);
1961         if (errno != 0 || port_end == optarg || *port_end++ != ',')
1962                 rte_exit(EXIT_FAILURE,
1963                 "Invalid eth-dest: %s", optarg);
1964         if (portid >= RTE_MAX_ETHPORTS)
1965                 rte_exit(EXIT_FAILURE,
1966                 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
1967                 portid, RTE_MAX_ETHPORTS);
1968
1969         if (cmdline_parse_etheraddr(NULL, port_end,
1970                 &peer_addr, sizeof(peer_addr)) < 0)
1971                 rte_exit(EXIT_FAILURE,
1972                 "Invalid ethernet address: %s\n",
1973                 port_end);
1974         dest = (uint8_t *)&dest_eth_addr[portid];
1975         for (c = 0; c < 6; c++)
1976                 dest[c] = peer_addr[c];
1977         *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
1978 }
1979
1980 #define CMD_LINE_OPT_CONFIG "config"
1981 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
1982 #define CMD_LINE_OPT_NO_NUMA "no-numa"
1983 #define CMD_LINE_OPT_IPV6 "ipv6"
1984 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
1985 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
1986
1987 /* Parse the argument given in the command line of the application */
1988 static int
1989 parse_args(int argc, char **argv)
1990 {
1991         int opt, ret;
1992         char **argvopt;
1993         int option_index;
1994         char *prgname = argv[0];
1995         static struct option lgopts[] = {
1996                 {CMD_LINE_OPT_CONFIG, 1, 0, 0},
1997                 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
1998                 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
1999                 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2000                 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2001                 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2002                 {NULL, 0, 0, 0}
2003         };
2004
2005         argvopt = argv;
2006
2007         while ((opt = getopt_long(argc, argvopt, "p:P",
2008                                 lgopts, &option_index)) != EOF) {
2009
2010                 switch (opt) {
2011                 /* portmask */
2012                 case 'p':
2013                         enabled_port_mask = parse_portmask(optarg);
2014                         if (enabled_port_mask == 0) {
2015                                 printf("invalid portmask\n");
2016                                 print_usage(prgname);
2017                                 return -1;
2018                         }
2019                         break;
2020                 case 'P':
2021                         printf("Promiscuous mode selected\n");
2022                         promiscuous_on = 1;
2023                         break;
2024
2025                 /* long options */
2026                 case 0:
2027                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_CONFIG,
2028                                 sizeof (CMD_LINE_OPT_CONFIG))) {
2029                                 ret = parse_config(optarg);
2030                                 if (ret) {
2031                                         printf("invalid config\n");
2032                                         print_usage(prgname);
2033                                         return -1;
2034                                 }
2035                         }
2036
2037                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2038                                 sizeof(CMD_LINE_OPT_CONFIG))) {
2039                                         parse_eth_dest(optarg);
2040                         }
2041
2042                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2043                                 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2044                                 printf("numa is disabled \n");
2045                                 numa_on = 0;
2046                         }
2047
2048 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2049                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2050                                 sizeof(CMD_LINE_OPT_IPV6))) {
2051                                 printf("ipv6 is specified \n");
2052                                 ipv6 = 1;
2053                         }
2054 #endif
2055
2056                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2057                                 sizeof (CMD_LINE_OPT_ENABLE_JUMBO))) {
2058                                 struct option lenopts = {"max-pkt-len", required_argument, 0, 0};
2059
2060                                 printf("jumbo frame is enabled - disabling simple TX path\n");
2061                                 port_conf.rxmode.jumbo_frame = 1;
2062
2063                                 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2064                                 if (0 == getopt_long(argc, argvopt, "", &lenopts, &option_index)) {
2065                                         ret = parse_max_pkt_len(optarg);
2066                                         if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)){
2067                                                 printf("invalid packet length\n");
2068                                                 print_usage(prgname);
2069                                                 return -1;
2070                                         }
2071                                         port_conf.rxmode.max_rx_pkt_len = ret;
2072                                 }
2073                                 printf("set jumbo frame max packet length to %u\n",
2074                                                 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2075                         }
2076 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2077                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2078                                 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2079                                 ret = parse_hash_entry_number(optarg);
2080                                 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2081                                         hash_entry_number = ret;
2082                                 } else {
2083                                         printf("invalid hash entry number\n");
2084                                         print_usage(prgname);
2085                                         return -1;
2086                                 }
2087                         }
2088 #endif
2089                         break;
2090
2091                 default:
2092                         print_usage(prgname);
2093                         return -1;
2094                 }
2095         }
2096
2097         if (optind >= 0)
2098                 argv[optind-1] = prgname;
2099
2100         ret = optind-1;
2101         optind = 0; /* reset getopt lib */
2102         return ret;
2103 }
2104
2105 static void
2106 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
2107 {
2108         char buf[ETHER_ADDR_FMT_SIZE];
2109         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
2110         printf("%s%s", name, buf);
2111 }
2112
2113 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2114
2115 static void convert_ipv4_5tuple(struct ipv4_5tuple* key1,
2116                 union ipv4_5tuple_host* key2)
2117 {
2118         key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
2119         key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
2120         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
2121         key2->port_src = rte_cpu_to_be_16(key1->port_src);
2122         key2->proto = key1->proto;
2123         key2->pad0 = 0;
2124         key2->pad1 = 0;
2125         return;
2126 }
2127
2128 static void convert_ipv6_5tuple(struct ipv6_5tuple* key1,
2129                 union ipv6_5tuple_host* key2)
2130 {
2131         uint32_t i;
2132         for (i = 0; i < 16; i++)
2133         {
2134                 key2->ip_dst[i] = key1->ip_dst[i];
2135                 key2->ip_src[i] = key1->ip_src[i];
2136         }
2137         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
2138         key2->port_src = rte_cpu_to_be_16(key1->port_src);
2139         key2->proto = key1->proto;
2140         key2->pad0 = 0;
2141         key2->pad1 = 0;
2142         key2->reserve = 0;
2143         return;
2144 }
2145
2146 #define BYTE_VALUE_MAX 256
2147 #define ALL_32_BITS 0xffffffff
2148 #define BIT_8_TO_15 0x0000ff00
2149 static inline void
2150 populate_ipv4_few_flow_into_table(const struct rte_hash* h)
2151 {
2152         uint32_t i;
2153         int32_t ret;
2154         uint32_t array_len = sizeof(ipv4_l3fwd_route_array)/sizeof(ipv4_l3fwd_route_array[0]);
2155
2156         mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
2157         for (i = 0; i < array_len; i++) {
2158                 struct ipv4_l3fwd_route  entry;
2159                 union ipv4_5tuple_host newkey;
2160                 entry = ipv4_l3fwd_route_array[i];
2161                 convert_ipv4_5tuple(&entry.key, &newkey);
2162                 ret = rte_hash_add_key (h,(void *) &newkey);
2163                 if (ret < 0) {
2164                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
2165                                 " to the l3fwd hash.\n", i);
2166                 }
2167                 ipv4_l3fwd_out_if[ret] = entry.if_out;
2168         }
2169         printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
2170 }
2171
2172 #define BIT_16_TO_23 0x00ff0000
2173 static inline void
2174 populate_ipv6_few_flow_into_table(const struct rte_hash* h)
2175 {
2176         uint32_t i;
2177         int32_t ret;
2178         uint32_t array_len = sizeof(ipv6_l3fwd_route_array)/sizeof(ipv6_l3fwd_route_array[0]);
2179
2180         mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
2181         mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
2182         for (i = 0; i < array_len; i++) {
2183                 struct ipv6_l3fwd_route entry;
2184                 union ipv6_5tuple_host newkey;
2185                 entry = ipv6_l3fwd_route_array[i];
2186                 convert_ipv6_5tuple(&entry.key, &newkey);
2187                 ret = rte_hash_add_key (h, (void *) &newkey);
2188                 if (ret < 0) {
2189                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
2190                                 " to the l3fwd hash.\n", i);
2191                 }
2192                 ipv6_l3fwd_out_if[ret] = entry.if_out;
2193         }
2194         printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
2195 }
2196
2197 #define NUMBER_PORT_USED 4
2198 static inline void
2199 populate_ipv4_many_flow_into_table(const struct rte_hash* h,
2200                 unsigned int nr_flow)
2201 {
2202         unsigned i;
2203         mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
2204         for (i = 0; i < nr_flow; i++) {
2205                 struct ipv4_l3fwd_route entry;
2206                 union ipv4_5tuple_host newkey;
2207                 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
2208                 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
2209                 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
2210                 /* Create the ipv4 exact match flow */
2211                 memset(&entry, 0, sizeof(entry));
2212                 switch (i & (NUMBER_PORT_USED -1)) {
2213                 case 0:
2214                         entry = ipv4_l3fwd_route_array[0];
2215                         entry.key.ip_dst = IPv4(101,c,b,a);
2216                         break;
2217                 case 1:
2218                         entry = ipv4_l3fwd_route_array[1];
2219                         entry.key.ip_dst = IPv4(201,c,b,a);
2220                         break;
2221                 case 2:
2222                         entry = ipv4_l3fwd_route_array[2];
2223                         entry.key.ip_dst = IPv4(111,c,b,a);
2224                         break;
2225                 case 3:
2226                         entry = ipv4_l3fwd_route_array[3];
2227                         entry.key.ip_dst = IPv4(211,c,b,a);
2228                         break;
2229                 };
2230                 convert_ipv4_5tuple(&entry.key, &newkey);
2231                 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
2232                 if (ret < 0) {
2233                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
2234                 }
2235                 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
2236
2237         }
2238         printf("Hash: Adding 0x%x keys\n", nr_flow);
2239 }
2240
2241 static inline void
2242 populate_ipv6_many_flow_into_table(const struct rte_hash* h,
2243                 unsigned int nr_flow)
2244 {
2245         unsigned i;
2246         mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
2247         mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
2248         for (i = 0; i < nr_flow; i++) {
2249                 struct ipv6_l3fwd_route entry;
2250                 union ipv6_5tuple_host newkey;
2251                 uint8_t a = (uint8_t) ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
2252                 uint8_t b = (uint8_t) (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
2253                 uint8_t c = (uint8_t) ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
2254                 /* Create the ipv6 exact match flow */
2255                 memset(&entry, 0, sizeof(entry));
2256                 switch (i & (NUMBER_PORT_USED - 1)) {
2257                 case 0: entry = ipv6_l3fwd_route_array[0]; break;
2258                 case 1: entry = ipv6_l3fwd_route_array[1]; break;
2259                 case 2: entry = ipv6_l3fwd_route_array[2]; break;
2260                 case 3: entry = ipv6_l3fwd_route_array[3]; break;
2261                 };
2262                 entry.key.ip_dst[13] = c;
2263                 entry.key.ip_dst[14] = b;
2264                 entry.key.ip_dst[15] = a;
2265                 convert_ipv6_5tuple(&entry.key, &newkey);
2266                 int32_t ret = rte_hash_add_key(h,(void *) &newkey);
2267                 if (ret < 0) {
2268                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
2269                 }
2270                 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
2271
2272         }
2273         printf("Hash: Adding 0x%x keys\n", nr_flow);
2274 }
2275
2276 static void
2277 setup_hash(int socketid)
2278 {
2279     struct rte_hash_parameters ipv4_l3fwd_hash_params = {
2280         .name = NULL,
2281         .entries = L3FWD_HASH_ENTRIES,
2282         .key_len = sizeof(union ipv4_5tuple_host),
2283         .hash_func = ipv4_hash_crc,
2284         .hash_func_init_val = 0,
2285     };
2286
2287     struct rte_hash_parameters ipv6_l3fwd_hash_params = {
2288         .name = NULL,
2289         .entries = L3FWD_HASH_ENTRIES,
2290         .key_len = sizeof(union ipv6_5tuple_host),
2291         .hash_func = ipv6_hash_crc,
2292         .hash_func_init_val = 0,
2293     };
2294
2295     char s[64];
2296
2297         /* create ipv4 hash */
2298         snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
2299         ipv4_l3fwd_hash_params.name = s;
2300         ipv4_l3fwd_hash_params.socket_id = socketid;
2301         ipv4_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv4_l3fwd_hash_params);
2302         if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2303                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2304                                 "socket %d\n", socketid);
2305
2306         /* create ipv6 hash */
2307         snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
2308         ipv6_l3fwd_hash_params.name = s;
2309         ipv6_l3fwd_hash_params.socket_id = socketid;
2310         ipv6_l3fwd_lookup_struct[socketid] = rte_hash_create(&ipv6_l3fwd_hash_params);
2311         if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2312                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
2313                                 "socket %d\n", socketid);
2314
2315         if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
2316                 /* For testing hash matching with a large number of flows we
2317                  * generate millions of IP 5-tuples with an incremented dst
2318                  * address to initialize the hash table. */
2319                 if (ipv6 == 0) {
2320                         /* populate the ipv4 hash */
2321                         populate_ipv4_many_flow_into_table(
2322                                 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
2323                 } else {
2324                         /* populate the ipv6 hash */
2325                         populate_ipv6_many_flow_into_table(
2326                                 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
2327                 }
2328         } else {
2329                 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize the hash table */
2330                 if (ipv6 == 0) {
2331                         /* populate the ipv4 hash */
2332                         populate_ipv4_few_flow_into_table(ipv4_l3fwd_lookup_struct[socketid]);
2333                 } else {
2334                         /* populate the ipv6 hash */
2335                         populate_ipv6_few_flow_into_table(ipv6_l3fwd_lookup_struct[socketid]);
2336                 }
2337         }
2338 }
2339 #endif
2340
2341 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2342 static void
2343 setup_lpm(int socketid)
2344 {
2345         struct rte_lpm6_config config;
2346         unsigned i;
2347         int ret;
2348         char s[64];
2349
2350         /* create the LPM table */
2351         snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
2352         ipv4_l3fwd_lookup_struct[socketid] = rte_lpm_create(s, socketid,
2353                                 IPV4_L3FWD_LPM_MAX_RULES, 0);
2354         if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
2355                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2356                                 " on socket %d\n", socketid);
2357
2358         /* populate the LPM table */
2359         for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
2360
2361                 /* skip unused ports */
2362                 if ((1 << ipv4_l3fwd_route_array[i].if_out &
2363                                 enabled_port_mask) == 0)
2364                         continue;
2365
2366                 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
2367                         ipv4_l3fwd_route_array[i].ip,
2368                         ipv4_l3fwd_route_array[i].depth,
2369                         ipv4_l3fwd_route_array[i].if_out);
2370
2371                 if (ret < 0) {
2372                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2373                                 "l3fwd LPM table on socket %d\n",
2374                                 i, socketid);
2375                 }
2376
2377                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
2378                         (unsigned)ipv4_l3fwd_route_array[i].ip,
2379                         ipv4_l3fwd_route_array[i].depth,
2380                         ipv4_l3fwd_route_array[i].if_out);
2381         }
2382
2383         /* create the LPM6 table */
2384         snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
2385
2386         config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
2387         config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
2388         config.flags = 0;
2389         ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
2390                                 &config);
2391         if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
2392                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
2393                                 " on socket %d\n", socketid);
2394
2395         /* populate the LPM table */
2396         for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
2397
2398                 /* skip unused ports */
2399                 if ((1 << ipv6_l3fwd_route_array[i].if_out &
2400                                 enabled_port_mask) == 0)
2401                         continue;
2402
2403                 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
2404                         ipv6_l3fwd_route_array[i].ip,
2405                         ipv6_l3fwd_route_array[i].depth,
2406                         ipv6_l3fwd_route_array[i].if_out);
2407
2408                 if (ret < 0) {
2409                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
2410                                 "l3fwd LPM table on socket %d\n",
2411                                 i, socketid);
2412                 }
2413
2414                 printf("LPM: Adding route %s / %d (%d)\n",
2415                         "IPV6",
2416                         ipv6_l3fwd_route_array[i].depth,
2417                         ipv6_l3fwd_route_array[i].if_out);
2418         }
2419 }
2420 #endif
2421
2422 static int
2423 init_mem(unsigned nb_mbuf)
2424 {
2425         struct lcore_conf *qconf;
2426         int socketid;
2427         unsigned lcore_id;
2428         char s[64];
2429
2430         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2431                 if (rte_lcore_is_enabled(lcore_id) == 0)
2432                         continue;
2433
2434                 if (numa_on)
2435                         socketid = rte_lcore_to_socket_id(lcore_id);
2436                 else
2437                         socketid = 0;
2438
2439                 if (socketid >= NB_SOCKETS) {
2440                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
2441                                 socketid, lcore_id, NB_SOCKETS);
2442                 }
2443                 if (pktmbuf_pool[socketid] == NULL) {
2444                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
2445                         pktmbuf_pool[socketid] =
2446                                 rte_pktmbuf_pool_create(s, nb_mbuf,
2447                                         MEMPOOL_CACHE_SIZE, 0,
2448                                         RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
2449                         if (pktmbuf_pool[socketid] == NULL)
2450                                 rte_exit(EXIT_FAILURE,
2451                                                 "Cannot init mbuf pool on socket %d\n", socketid);
2452                         else
2453                                 printf("Allocated mbuf pool on socket %d\n", socketid);
2454
2455 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
2456                         setup_lpm(socketid);
2457 #else
2458                         setup_hash(socketid);
2459 #endif
2460                 }
2461                 qconf = &lcore_conf[lcore_id];
2462                 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
2463                 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
2464         }
2465         return 0;
2466 }
2467
2468 /* Check the link status of all ports in up to 9s, and print them finally */
2469 static void
2470 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
2471 {
2472 #define CHECK_INTERVAL 100 /* 100ms */
2473 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
2474         uint8_t portid, count, all_ports_up, print_flag = 0;
2475         struct rte_eth_link link;
2476
2477         printf("\nChecking link status");
2478         fflush(stdout);
2479         for (count = 0; count <= MAX_CHECK_TIME; count++) {
2480                 all_ports_up = 1;
2481                 for (portid = 0; portid < port_num; portid++) {
2482                         if ((port_mask & (1 << portid)) == 0)
2483                                 continue;
2484                         memset(&link, 0, sizeof(link));
2485                         rte_eth_link_get_nowait(portid, &link);
2486                         /* print link status if flag set */
2487                         if (print_flag == 1) {
2488                                 if (link.link_status)
2489                                         printf("Port %d Link Up - speed %u "
2490                                                 "Mbps - %s\n", (uint8_t)portid,
2491                                                 (unsigned)link.link_speed,
2492                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
2493                                         ("full-duplex") : ("half-duplex\n"));
2494                                 else
2495                                         printf("Port %d Link Down\n",
2496                                                 (uint8_t)portid);
2497                                 continue;
2498                         }
2499                         /* clear all_ports_up flag if any link down */
2500                         if (link.link_status == 0) {
2501                                 all_ports_up = 0;
2502                                 break;
2503                         }
2504                 }
2505                 /* after finally printing all link status, get out */
2506                 if (print_flag == 1)
2507                         break;
2508
2509                 if (all_ports_up == 0) {
2510                         printf(".");
2511                         fflush(stdout);
2512                         rte_delay_ms(CHECK_INTERVAL);
2513                 }
2514
2515                 /* set the print_flag if all ports up or timeout */
2516                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
2517                         print_flag = 1;
2518                         printf("done\n");
2519                 }
2520         }
2521 }
2522
2523 int
2524 main(int argc, char **argv)
2525 {
2526         struct lcore_conf *qconf;
2527         struct rte_eth_dev_info dev_info;
2528         struct rte_eth_txconf *txconf;
2529         int ret;
2530         unsigned nb_ports;
2531         uint16_t queueid;
2532         unsigned lcore_id;
2533         uint32_t n_tx_queue, nb_lcores;
2534         uint8_t portid, nb_rx_queue, queue, socketid;
2535
2536         /* init EAL */
2537         ret = rte_eal_init(argc, argv);
2538         if (ret < 0)
2539                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2540         argc -= ret;
2541         argv += ret;
2542
2543         /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
2544         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2545                 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
2546                 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2547         }
2548
2549         /* parse application arguments (after the EAL ones) */
2550         ret = parse_args(argc, argv);
2551         if (ret < 0)
2552                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
2553
2554         if (check_lcore_params() < 0)
2555                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
2556
2557         ret = init_lcore_rx_queues();
2558         if (ret < 0)
2559                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2560
2561         nb_ports = rte_eth_dev_count();
2562         if (nb_ports > RTE_MAX_ETHPORTS)
2563                 nb_ports = RTE_MAX_ETHPORTS;
2564
2565         if (check_port_config(nb_ports) < 0)
2566                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
2567
2568         nb_lcores = rte_lcore_count();
2569
2570         /* initialize all ports */
2571         for (portid = 0; portid < nb_ports; portid++) {
2572                 /* skip ports that are not enabled */
2573                 if ((enabled_port_mask & (1 << portid)) == 0) {
2574                         printf("\nSkipping disabled port %d\n", portid);
2575                         continue;
2576                 }
2577
2578                 /* init port */
2579                 printf("Initializing port %d ... ", portid );
2580                 fflush(stdout);
2581
2582                 nb_rx_queue = get_port_n_rx_queues(portid);
2583                 n_tx_queue = nb_lcores;
2584                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
2585                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
2586                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
2587                         nb_rx_queue, (unsigned)n_tx_queue );
2588                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
2589                                         (uint16_t)n_tx_queue, &port_conf);
2590                 if (ret < 0)
2591                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
2592                                 ret, portid);
2593
2594                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
2595                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
2596                 printf(", ");
2597                 print_ethaddr("Destination:",
2598                         (const struct ether_addr *)&dest_eth_addr[portid]);
2599                 printf(", ");
2600
2601                 /*
2602                  * prepare src MACs for each port.
2603                  */
2604                 ether_addr_copy(&ports_eth_addr[portid],
2605                         (struct ether_addr *)(val_eth + portid) + 1);
2606
2607                 /* init memory */
2608                 ret = init_mem(NB_MBUF);
2609                 if (ret < 0)
2610                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
2611
2612                 /* init one TX queue per couple (lcore,port) */
2613                 queueid = 0;
2614                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2615                         if (rte_lcore_is_enabled(lcore_id) == 0)
2616                                 continue;
2617
2618                         if (numa_on)
2619                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2620                         else
2621                                 socketid = 0;
2622
2623                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
2624                         fflush(stdout);
2625
2626                         rte_eth_dev_info_get(portid, &dev_info);
2627                         txconf = &dev_info.default_txconf;
2628                         if (port_conf.rxmode.jumbo_frame)
2629                                 txconf->txq_flags = 0;
2630                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
2631                                                      socketid, txconf);
2632                         if (ret < 0)
2633                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
2634                                         "port=%d\n", ret, portid);
2635
2636                         qconf = &lcore_conf[lcore_id];
2637                         qconf->tx_queue_id[portid] = queueid;
2638                         queueid++;
2639                 }
2640                 printf("\n");
2641         }
2642
2643         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2644                 if (rte_lcore_is_enabled(lcore_id) == 0)
2645                         continue;
2646                 qconf = &lcore_conf[lcore_id];
2647                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
2648                 fflush(stdout);
2649                 /* init RX queues */
2650                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
2651                         portid = qconf->rx_queue_list[queue].port_id;
2652                         queueid = qconf->rx_queue_list[queue].queue_id;
2653
2654                         if (numa_on)
2655                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2656                         else
2657                                 socketid = 0;
2658
2659                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
2660                         fflush(stdout);
2661
2662                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
2663                                         socketid,
2664                                         NULL,
2665                                         pktmbuf_pool[socketid]);
2666                         if (ret < 0)
2667                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
2668                                                 "port=%d\n", ret, portid);
2669                 }
2670         }
2671
2672         printf("\n");
2673
2674         /* start ports */
2675         for (portid = 0; portid < nb_ports; portid++) {
2676                 if ((enabled_port_mask & (1 << portid)) == 0) {
2677                         continue;
2678                 }
2679                 /* Start device */
2680                 ret = rte_eth_dev_start(portid);
2681                 if (ret < 0)
2682                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
2683                                 ret, portid);
2684
2685                 /*
2686                  * If enabled, put device in promiscuous mode.
2687                  * This allows IO forwarding mode to forward packets
2688                  * to itself through 2 cross-connected  ports of the
2689                  * target machine.
2690                  */
2691                 if (promiscuous_on)
2692                         rte_eth_promiscuous_enable(portid);
2693         }
2694
2695         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
2696
2697         /* launch per-lcore init on every lcore */
2698         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2699         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2700                 if (rte_eal_wait_lcore(lcore_id) < 0)
2701                         return -1;
2702         }
2703
2704         return 0;
2705 }