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