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