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