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