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