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