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