ring: return free space when enqueuing
[dpdk.git] / examples / performance-thread / l3fwd-thread / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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 #define _GNU_SOURCE
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <sys/types.h>
41 #include <string.h>
42 #include <sys/queue.h>
43 #include <stdarg.h>
44 #include <errno.h>
45 #include <getopt.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 #include <lthread_api.h>
81
82 #define APP_LOOKUP_EXACT_MATCH          0
83 #define APP_LOOKUP_LPM                  1
84 #define DO_RFC_1812_CHECKS
85
86 /* Enable cpu-load stats 0-off, 1-on */
87 #define APP_CPU_LOAD                 1
88
89 #ifndef APP_LOOKUP_METHOD
90 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
91 #endif
92
93 static int
94 check_ptype(int portid)
95 {
96         int i, ret;
97         int ipv4 = 0, ipv6 = 0;
98
99         ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK, NULL,
100                         0);
101         if (ret <= 0)
102                 return 0;
103
104         uint32_t ptypes[ret];
105
106         ret = rte_eth_dev_get_supported_ptypes(portid, RTE_PTYPE_L3_MASK,
107                         ptypes, ret);
108         for (i = 0; i < ret; ++i) {
109                 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
110                         ipv4 = 1;
111                 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
112                         ipv6 = 1;
113         }
114
115         if (ipv4 && ipv6)
116                 return 1;
117
118         return 0;
119 }
120
121 static inline void
122 parse_ptype(struct rte_mbuf *m)
123 {
124         struct ether_hdr *eth_hdr;
125         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
126         uint16_t ether_type;
127
128         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
129         ether_type = eth_hdr->ether_type;
130         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
131                 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
132         else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
133                 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
134
135         m->packet_type = packet_type;
136 }
137
138 static uint16_t
139 cb_parse_ptype(__rte_unused uint8_t port, __rte_unused uint16_t queue,
140                 struct rte_mbuf *pkts[], uint16_t nb_pkts,
141                 __rte_unused uint16_t max_pkts, __rte_unused void *user_param)
142 {
143         unsigned int i;
144
145         for (i = 0; i < nb_pkts; i++)
146                 parse_ptype(pkts[i]);
147
148         return nb_pkts;
149 }
150
151 /*
152  *  When set to zero, simple forwaring path is eanbled.
153  *  When set to one, optimized forwarding path is enabled.
154  *  Note that LPM optimisation path uses SSE4.1 instructions.
155  */
156 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && !defined(__SSE4_1__))
157 #define ENABLE_MULTI_BUFFER_OPTIMIZE    0
158 #else
159 #define ENABLE_MULTI_BUFFER_OPTIMIZE    1
160 #endif
161
162 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
163 #include <rte_hash.h>
164 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
165 #include <rte_lpm.h>
166 #include <rte_lpm6.h>
167 #else
168 #error "APP_LOOKUP_METHOD set to incorrect value"
169 #endif
170
171 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
172
173 #define MAX_JUMBO_PKT_LEN  9600
174
175 #define IPV6_ADDR_LEN 16
176
177 #define MEMPOOL_CACHE_SIZE 256
178
179 /*
180  * This expression is used to calculate the number of mbufs needed depending on
181  * user input, taking into account memory for rx and tx hardware rings, cache
182  * per lcore and mtable per port per lcore. RTE_MAX is used to ensure that
183  * NB_MBUF never goes below a minimum value of 8192
184  */
185
186 #define NB_MBUF RTE_MAX(\
187                 (nb_ports*nb_rx_queue*RTE_TEST_RX_DESC_DEFAULT +       \
188                 nb_ports*nb_lcores*MAX_PKT_BURST +                     \
189                 nb_ports*n_tx_queue*RTE_TEST_TX_DESC_DEFAULT +         \
190                 nb_lcores*MEMPOOL_CACHE_SIZE),                         \
191                 (unsigned)8192)
192
193 #define MAX_PKT_BURST     32
194 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
195
196 /*
197  * Try to avoid TX buffering if we have at least MAX_TX_BURST packets to send.
198  */
199 #define MAX_TX_BURST  (MAX_PKT_BURST / 2)
200 #define BURST_SIZE    MAX_TX_BURST
201
202 #define NB_SOCKETS 8
203
204 /* Configure how many packets ahead to prefetch, when reading packets */
205 #define PREFETCH_OFFSET 3
206
207 /* Used to mark destination port as 'invalid'. */
208 #define BAD_PORT        ((uint16_t)-1)
209
210 #define FWDSTEP 4
211
212 /*
213  * Configurable number of RX/TX ring descriptors
214  */
215 #define RTE_TEST_RX_DESC_DEFAULT 128
216 #define RTE_TEST_TX_DESC_DEFAULT 128
217 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
218 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
219
220 /* ethernet addresses of ports */
221 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
222 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
223
224 static __m128i val_eth[RTE_MAX_ETHPORTS];
225
226 /* replace first 12B of the ethernet header. */
227 #define MASK_ETH 0x3f
228
229 /* mask of enabled ports */
230 static uint32_t enabled_port_mask;
231 static int promiscuous_on; /**< Set in promiscuous mode off by default. */
232 static int numa_on = 1;    /**< NUMA is enabled by default. */
233 static int parse_ptype_on;
234
235 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
236 static int ipv6;           /**< ipv6 is false by default. */
237 #endif
238
239 #if (APP_CPU_LOAD == 1)
240
241 #define MAX_CPU RTE_MAX_LCORE
242 #define CPU_LOAD_TIMEOUT_US (5 * 1000 * 1000)  /**< Timeout for collecting 5s */
243
244 #define CPU_PROCESS     0
245 #define CPU_POLL        1
246 #define MAX_CPU_COUNTER 2
247
248 struct cpu_load {
249         uint16_t       n_cpu;
250         uint64_t       counter;
251         uint64_t       hits[MAX_CPU_COUNTER][MAX_CPU];
252 } __rte_cache_aligned;
253
254 static struct cpu_load cpu_load;
255 static int cpu_load_lcore_id = -1;
256
257 #define SET_CPU_BUSY(thread, counter) \
258                 thread->conf.busy[counter] = 1
259
260 #define SET_CPU_IDLE(thread, counter) \
261                 thread->conf.busy[counter] = 0
262
263 #define IS_CPU_BUSY(thread, counter) \
264                 (thread->conf.busy[counter] > 0)
265
266 #else
267
268 #define SET_CPU_BUSY(thread, counter)
269 #define SET_CPU_IDLE(thread, counter)
270 #define IS_CPU_BUSY(thread, counter) 0
271
272 #endif
273
274 struct mbuf_table {
275         uint16_t len;
276         struct rte_mbuf *m_table[MAX_PKT_BURST];
277 };
278
279 struct lcore_rx_queue {
280         uint8_t port_id;
281         uint8_t queue_id;
282 } __rte_cache_aligned;
283
284 #define MAX_RX_QUEUE_PER_LCORE 16
285 #define MAX_TX_QUEUE_PER_PORT  RTE_MAX_ETHPORTS
286 #define MAX_RX_QUEUE_PER_PORT  128
287
288 #define MAX_LCORE_PARAMS       1024
289 struct rx_thread_params {
290         uint8_t port_id;
291         uint8_t queue_id;
292         uint8_t lcore_id;
293         uint8_t thread_id;
294 } __rte_cache_aligned;
295
296 static struct rx_thread_params rx_thread_params_array[MAX_LCORE_PARAMS];
297 static struct rx_thread_params rx_thread_params_array_default[] = {
298         {0, 0, 2, 0},
299         {0, 1, 2, 1},
300         {0, 2, 2, 2},
301         {1, 0, 2, 3},
302         {1, 1, 2, 4},
303         {1, 2, 2, 5},
304         {2, 0, 2, 6},
305         {3, 0, 3, 7},
306         {3, 1, 3, 8},
307 };
308
309 static struct rx_thread_params *rx_thread_params =
310                 rx_thread_params_array_default;
311 static uint16_t nb_rx_thread_params = RTE_DIM(rx_thread_params_array_default);
312
313 struct tx_thread_params {
314         uint8_t lcore_id;
315         uint8_t thread_id;
316 } __rte_cache_aligned;
317
318 static struct tx_thread_params tx_thread_params_array[MAX_LCORE_PARAMS];
319 static struct tx_thread_params tx_thread_params_array_default[] = {
320         {4, 0},
321         {5, 1},
322         {6, 2},
323         {7, 3},
324         {8, 4},
325         {9, 5},
326         {10, 6},
327         {11, 7},
328         {12, 8},
329 };
330
331 static struct tx_thread_params *tx_thread_params =
332                 tx_thread_params_array_default;
333 static uint16_t nb_tx_thread_params = RTE_DIM(tx_thread_params_array_default);
334
335 static struct rte_eth_conf port_conf = {
336         .rxmode = {
337                 .mq_mode = ETH_MQ_RX_RSS,
338                 .max_rx_pkt_len = ETHER_MAX_LEN,
339                 .split_hdr_size = 0,
340                 .header_split   = 0, /**< Header Split disabled */
341                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
342                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
343                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
344                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
345         },
346         .rx_adv_conf = {
347                 .rss_conf = {
348                         .rss_key = NULL,
349                         .rss_hf = ETH_RSS_TCP,
350                 },
351         },
352         .txmode = {
353                 .mq_mode = ETH_MQ_TX_NONE,
354         },
355 };
356
357 static struct rte_mempool *pktmbuf_pool[NB_SOCKETS];
358
359 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
360
361 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
362 #include <rte_hash_crc.h>
363 #define DEFAULT_HASH_FUNC       rte_hash_crc
364 #else
365 #include <rte_jhash.h>
366 #define DEFAULT_HASH_FUNC       rte_jhash
367 #endif
368
369 struct ipv4_5tuple {
370         uint32_t ip_dst;
371         uint32_t ip_src;
372         uint16_t port_dst;
373         uint16_t port_src;
374         uint8_t  proto;
375 } __attribute__((__packed__));
376
377 union ipv4_5tuple_host {
378         struct {
379                 uint8_t  pad0;
380                 uint8_t  proto;
381                 uint16_t pad1;
382                 uint32_t ip_src;
383                 uint32_t ip_dst;
384                 uint16_t port_src;
385                 uint16_t port_dst;
386         };
387         __m128i xmm;
388 };
389
390 #define XMM_NUM_IN_IPV6_5TUPLE 3
391
392 struct ipv6_5tuple {
393         uint8_t  ip_dst[IPV6_ADDR_LEN];
394         uint8_t  ip_src[IPV6_ADDR_LEN];
395         uint16_t port_dst;
396         uint16_t port_src;
397         uint8_t  proto;
398 } __attribute__((__packed__));
399
400 union ipv6_5tuple_host {
401         struct {
402                 uint16_t pad0;
403                 uint8_t  proto;
404                 uint8_t  pad1;
405                 uint8_t  ip_src[IPV6_ADDR_LEN];
406                 uint8_t  ip_dst[IPV6_ADDR_LEN];
407                 uint16_t port_src;
408                 uint16_t port_dst;
409                 uint64_t reserve;
410         };
411         __m128i xmm[XMM_NUM_IN_IPV6_5TUPLE];
412 };
413
414 struct ipv4_l3fwd_route {
415         struct ipv4_5tuple key;
416         uint8_t if_out;
417 };
418
419 struct ipv6_l3fwd_route {
420         struct ipv6_5tuple key;
421         uint8_t if_out;
422 };
423
424 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
425         {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1),  101, 11, IPPROTO_TCP}, 0},
426         {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1),  102, 12, IPPROTO_TCP}, 1},
427         {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1),  101, 11, IPPROTO_TCP}, 2},
428         {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1),  102, 12, IPPROTO_TCP}, 3},
429 };
430
431 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
432         {{
433         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
434         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
435                         0x05},
436         101, 11, IPPROTO_TCP}, 0},
437
438         {{
439         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
440         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
441                         0x05},
442         102, 12, IPPROTO_TCP}, 1},
443
444         {{
445         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
446         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
447                         0x05},
448         101, 11, IPPROTO_TCP}, 2},
449
450         {{
451         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
452         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38,
453                         0x05},
454         102, 12, IPPROTO_TCP}, 3},
455 };
456
457 typedef struct rte_hash lookup_struct_t;
458 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
459 static lookup_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
460
461 #ifdef RTE_ARCH_X86_64
462 /* default to 4 million hash entries (approx) */
463 #define L3FWD_HASH_ENTRIES (1024*1024*4)
464 #else
465 /* 32-bit has less address-space for hugepage memory, limit to 1M entries */
466 #define L3FWD_HASH_ENTRIES (1024*1024*1)
467 #endif
468 #define HASH_ENTRY_NUMBER_DEFAULT 4
469
470 static uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
471
472 static inline uint32_t
473 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
474                 uint32_t init_val)
475 {
476         const union ipv4_5tuple_host *k;
477         uint32_t t;
478         const uint32_t *p;
479
480         k = data;
481         t = k->proto;
482         p = (const uint32_t *)&k->port_src;
483
484 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
485         init_val = rte_hash_crc_4byte(t, init_val);
486         init_val = rte_hash_crc_4byte(k->ip_src, init_val);
487         init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
488         init_val = rte_hash_crc_4byte(*p, init_val);
489 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
490         init_val = rte_jhash_1word(t, init_val);
491         init_val = rte_jhash_1word(k->ip_src, init_val);
492         init_val = rte_jhash_1word(k->ip_dst, init_val);
493         init_val = rte_jhash_1word(*p, init_val);
494 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
495         return init_val;
496 }
497
498 static inline uint32_t
499 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
500                 uint32_t init_val)
501 {
502         const union ipv6_5tuple_host *k;
503         uint32_t t;
504         const uint32_t *p;
505 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
506         const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
507         const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
508 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
509
510         k = data;
511         t = k->proto;
512         p = (const uint32_t *)&k->port_src;
513
514 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
515         ip_src0 = (const uint32_t *) k->ip_src;
516         ip_src1 = (const uint32_t *)(k->ip_src + 4);
517         ip_src2 = (const uint32_t *)(k->ip_src + 8);
518         ip_src3 = (const uint32_t *)(k->ip_src + 12);
519         ip_dst0 = (const uint32_t *) k->ip_dst;
520         ip_dst1 = (const uint32_t *)(k->ip_dst + 4);
521         ip_dst2 = (const uint32_t *)(k->ip_dst + 8);
522         ip_dst3 = (const uint32_t *)(k->ip_dst + 12);
523         init_val = rte_hash_crc_4byte(t, init_val);
524         init_val = rte_hash_crc_4byte(*ip_src0, init_val);
525         init_val = rte_hash_crc_4byte(*ip_src1, init_val);
526         init_val = rte_hash_crc_4byte(*ip_src2, init_val);
527         init_val = rte_hash_crc_4byte(*ip_src3, init_val);
528         init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
529         init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
530         init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
531         init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
532         init_val = rte_hash_crc_4byte(*p, init_val);
533 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
534         init_val = rte_jhash_1word(t, init_val);
535         init_val = rte_jhash(k->ip_src, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
536         init_val = rte_jhash(k->ip_dst, sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
537         init_val = rte_jhash_1word(*p, init_val);
538 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
539         return init_val;
540 }
541
542 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
543 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
544
545 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
546 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
547
548 #endif
549
550 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
551 struct ipv4_l3fwd_route {
552         uint32_t ip;
553         uint8_t  depth;
554         uint8_t  if_out;
555 };
556
557 struct ipv6_l3fwd_route {
558         uint8_t ip[16];
559         uint8_t depth;
560         uint8_t if_out;
561 };
562
563 static struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
564         {IPv4(1, 1, 1, 0), 24, 0},
565         {IPv4(2, 1, 1, 0), 24, 1},
566         {IPv4(3, 1, 1, 0), 24, 2},
567         {IPv4(4, 1, 1, 0), 24, 3},
568         {IPv4(5, 1, 1, 0), 24, 4},
569         {IPv4(6, 1, 1, 0), 24, 5},
570         {IPv4(7, 1, 1, 0), 24, 6},
571         {IPv4(8, 1, 1, 0), 24, 7},
572 };
573
574 static struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
575         {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
576         {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
577         {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
578         {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
579         {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
580         {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
581         {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
582         {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
583 };
584
585 #define IPV4_L3FWD_NUM_ROUTES RTE_DIM(ipv4_l3fwd_route_array)
586 #define IPV6_L3FWD_NUM_ROUTES RTE_DIM(ipv6_l3fwd_route_array)
587
588 #define IPV4_L3FWD_LPM_MAX_RULES         1024
589 #define IPV6_L3FWD_LPM_MAX_RULES         1024
590 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
591
592 typedef struct rte_lpm lookup_struct_t;
593 typedef struct rte_lpm6 lookup6_struct_t;
594 static lookup_struct_t *ipv4_l3fwd_lookup_struct[NB_SOCKETS];
595 static lookup6_struct_t *ipv6_l3fwd_lookup_struct[NB_SOCKETS];
596 #endif
597
598 struct lcore_conf {
599         lookup_struct_t *ipv4_lookup_struct;
600 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
601         lookup6_struct_t *ipv6_lookup_struct;
602 #else
603         lookup_struct_t *ipv6_lookup_struct;
604 #endif
605         void *data;
606 } __rte_cache_aligned;
607
608 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
609 RTE_DEFINE_PER_LCORE(struct lcore_conf *, lcore_conf);
610
611 #define MAX_RX_QUEUE_PER_THREAD 16
612 #define MAX_TX_PORT_PER_THREAD  RTE_MAX_ETHPORTS
613 #define MAX_TX_QUEUE_PER_PORT   RTE_MAX_ETHPORTS
614 #define MAX_RX_QUEUE_PER_PORT   128
615
616 #define MAX_RX_THREAD 1024
617 #define MAX_TX_THREAD 1024
618 #define MAX_THREAD    (MAX_RX_THREAD + MAX_TX_THREAD)
619
620 /**
621  * Producers and consumers threads configuration
622  */
623 static int lthreads_on = 1; /**< Use lthreads for processing*/
624
625 rte_atomic16_t rx_counter;  /**< Number of spawned rx threads */
626 rte_atomic16_t tx_counter;  /**< Number of spawned tx threads */
627
628 struct thread_conf {
629         uint16_t lcore_id;      /**< Initial lcore for rx thread */
630         uint16_t cpu_id;        /**< Cpu id for cpu load stats counter */
631         uint16_t thread_id;     /**< Thread ID */
632
633 #if (APP_CPU_LOAD > 0)
634         int busy[MAX_CPU_COUNTER];
635 #endif
636 };
637
638 struct thread_rx_conf {
639         struct thread_conf conf;
640
641         uint16_t n_rx_queue;
642         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
643
644         uint16_t n_ring;        /**< Number of output rings */
645         struct rte_ring *ring[RTE_MAX_LCORE];
646         struct lthread_cond *ready[RTE_MAX_LCORE];
647
648 #if (APP_CPU_LOAD > 0)
649         int busy[MAX_CPU_COUNTER];
650 #endif
651 } __rte_cache_aligned;
652
653 uint16_t n_rx_thread;
654 struct thread_rx_conf rx_thread[MAX_RX_THREAD];
655
656 struct thread_tx_conf {
657         struct thread_conf conf;
658
659         uint16_t tx_queue_id[RTE_MAX_LCORE];
660         struct mbuf_table tx_mbufs[RTE_MAX_LCORE];
661
662         struct rte_ring *ring;
663         struct lthread_cond **ready;
664
665 } __rte_cache_aligned;
666
667 uint16_t n_tx_thread;
668 struct thread_tx_conf tx_thread[MAX_TX_THREAD];
669
670 /* Send burst of packets on an output interface */
671 static inline int
672 send_burst(struct thread_tx_conf *qconf, uint16_t n, uint8_t port)
673 {
674         struct rte_mbuf **m_table;
675         int ret;
676         uint16_t queueid;
677
678         queueid = qconf->tx_queue_id[port];
679         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
680
681         ret = rte_eth_tx_burst(port, queueid, m_table, n);
682         if (unlikely(ret < n)) {
683                 do {
684                         rte_pktmbuf_free(m_table[ret]);
685                 } while (++ret < n);
686         }
687
688         return 0;
689 }
690
691 /* Enqueue a single packet, and send burst if queue is filled */
692 static inline int
693 send_single_packet(struct rte_mbuf *m, uint8_t port)
694 {
695         uint16_t len;
696         struct thread_tx_conf *qconf;
697
698         if (lthreads_on)
699                 qconf = (struct thread_tx_conf *)lthread_get_data();
700         else
701                 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
702
703         len = qconf->tx_mbufs[port].len;
704         qconf->tx_mbufs[port].m_table[len] = m;
705         len++;
706
707         /* enough pkts to be sent */
708         if (unlikely(len == MAX_PKT_BURST)) {
709                 send_burst(qconf, MAX_PKT_BURST, port);
710                 len = 0;
711         }
712
713         qconf->tx_mbufs[port].len = len;
714         return 0;
715 }
716
717 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
718         (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
719 static inline __attribute__((always_inline)) void
720 send_packetsx4(uint8_t port,
721         struct rte_mbuf *m[], uint32_t num)
722 {
723         uint32_t len, j, n;
724         struct thread_tx_conf *qconf;
725
726         if (lthreads_on)
727                 qconf = (struct thread_tx_conf *)lthread_get_data();
728         else
729                 qconf = (struct thread_tx_conf *)RTE_PER_LCORE(lcore_conf)->data;
730
731         len = qconf->tx_mbufs[port].len;
732
733         /*
734          * If TX buffer for that queue is empty, and we have enough packets,
735          * then send them straightway.
736          */
737         if (num >= MAX_TX_BURST && len == 0) {
738                 n = rte_eth_tx_burst(port, qconf->tx_queue_id[port], m, num);
739                 if (unlikely(n < num)) {
740                         do {
741                                 rte_pktmbuf_free(m[n]);
742                         } while (++n < num);
743                 }
744                 return;
745         }
746
747         /*
748          * Put packets into TX buffer for that queue.
749          */
750
751         n = len + num;
752         n = (n > MAX_PKT_BURST) ? MAX_PKT_BURST - len : num;
753
754         j = 0;
755         switch (n % FWDSTEP) {
756         while (j < n) {
757         case 0:
758                 qconf->tx_mbufs[port].m_table[len + j] = m[j];
759                 j++;
760         case 3:
761                 qconf->tx_mbufs[port].m_table[len + j] = m[j];
762                 j++;
763         case 2:
764                 qconf->tx_mbufs[port].m_table[len + j] = m[j];
765                 j++;
766         case 1:
767                 qconf->tx_mbufs[port].m_table[len + j] = m[j];
768                 j++;
769         }
770         }
771
772         len += n;
773
774         /* enough pkts to be sent */
775         if (unlikely(len == MAX_PKT_BURST)) {
776
777                 send_burst(qconf, MAX_PKT_BURST, port);
778
779                 /* copy rest of the packets into the TX buffer. */
780                 len = num - n;
781                 j = 0;
782                 switch (len % FWDSTEP) {
783                 while (j < len) {
784                 case 0:
785                         qconf->tx_mbufs[port].m_table[j] = m[n + j];
786                         j++;
787                 case 3:
788                         qconf->tx_mbufs[port].m_table[j] = m[n + j];
789                         j++;
790                 case 2:
791                         qconf->tx_mbufs[port].m_table[j] = m[n + j];
792                         j++;
793                 case 1:
794                         qconf->tx_mbufs[port].m_table[j] = m[n + j];
795                         j++;
796                 }
797                 }
798         }
799
800         qconf->tx_mbufs[port].len = len;
801 }
802 #endif /* APP_LOOKUP_LPM */
803
804 #ifdef DO_RFC_1812_CHECKS
805 static inline int
806 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
807 {
808         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
809         /*
810          * 1. The packet length reported by the Link Layer must be large
811          * enough to hold the minimum length legal IP datagram (20 bytes).
812          */
813         if (link_len < sizeof(struct ipv4_hdr))
814                 return -1;
815
816         /* 2. The IP checksum must be correct. */
817         /* this is checked in H/W */
818
819         /*
820          * 3. The IP version number must be 4. If the version number is not 4
821          * then the packet may be another version of IP, such as IPng or
822          * ST-II.
823          */
824         if (((pkt->version_ihl) >> 4) != 4)
825                 return -3;
826         /*
827          * 4. The IP header length field must be large enough to hold the
828          * minimum length legal IP datagram (20 bytes = 5 words).
829          */
830         if ((pkt->version_ihl & 0xf) < 5)
831                 return -4;
832
833         /*
834          * 5. The IP total length field must be large enough to hold the IP
835          * datagram header, whose length is specified in the IP header length
836          * field.
837          */
838         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
839                 return -5;
840
841         return 0;
842 }
843 #endif
844
845 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
846
847 static __m128i mask0;
848 static __m128i mask1;
849 static __m128i mask2;
850 static inline uint8_t
851 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
852                 lookup_struct_t *ipv4_l3fwd_lookup_struct)
853 {
854         int ret = 0;
855         union ipv4_5tuple_host key;
856
857         ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
858         __m128i data = _mm_loadu_si128((__m128i *)(ipv4_hdr));
859         /* Get 5 tuple: dst port, src port, dst IP address, src IP address and
860            protocol */
861         key.xmm = _mm_and_si128(data, mask0);
862         /* Find destination port */
863         ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
864         return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
865 }
866
867 static inline uint8_t
868 get_ipv6_dst_port(void *ipv6_hdr, uint8_t portid,
869                 lookup_struct_t *ipv6_l3fwd_lookup_struct)
870 {
871         int ret = 0;
872         union ipv6_5tuple_host key;
873
874         ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
875         __m128i data0 = _mm_loadu_si128((__m128i *)(ipv6_hdr));
876         __m128i data1 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
877                         sizeof(__m128i)));
878         __m128i data2 = _mm_loadu_si128((__m128i *)(((uint8_t *)ipv6_hdr) +
879                         sizeof(__m128i) + sizeof(__m128i)));
880         /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
881         key.xmm[0] = _mm_and_si128(data0, mask1);
882         /* Get part of 5 tuple: dst IP address lower 96 bits and src IP address
883            higher 32 bits */
884         key.xmm[1] = data1;
885         /* Get part of 5 tuple: dst port and src port and dst IP address higher
886            32 bits */
887         key.xmm[2] = _mm_and_si128(data2, mask2);
888
889         /* Find destination port */
890         ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
891         return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
892 }
893 #endif
894
895 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
896
897 static inline uint8_t
898 get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid,
899                 lookup_struct_t *ipv4_l3fwd_lookup_struct)
900 {
901         uint32_t next_hop;
902
903         return (uint8_t)((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
904                 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
905                 &next_hop) == 0) ? next_hop : portid);
906 }
907
908 static inline uint8_t
909 get_ipv6_dst_port(void *ipv6_hdr,  uint8_t portid,
910                 lookup6_struct_t *ipv6_l3fwd_lookup_struct)
911 {
912         uint32_t next_hop;
913
914         return (uint8_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
915                         ((struct ipv6_hdr *)ipv6_hdr)->dst_addr, &next_hop) == 0) ?
916                         next_hop : portid);
917 }
918 #endif
919
920 static inline void l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
921                 __attribute__((unused));
922
923 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH) && \
924         (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
925
926 #define MASK_ALL_PKTS   0xff
927 #define EXCLUDE_1ST_PKT 0xfe
928 #define EXCLUDE_2ND_PKT 0xfd
929 #define EXCLUDE_3RD_PKT 0xfb
930 #define EXCLUDE_4TH_PKT 0xf7
931 #define EXCLUDE_5TH_PKT 0xef
932 #define EXCLUDE_6TH_PKT 0xdf
933 #define EXCLUDE_7TH_PKT 0xbf
934 #define EXCLUDE_8TH_PKT 0x7f
935
936 static inline void
937 simple_ipv4_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
938 {
939         struct ether_hdr *eth_hdr[8];
940         struct ipv4_hdr *ipv4_hdr[8];
941         uint8_t dst_port[8];
942         int32_t ret[8];
943         union ipv4_5tuple_host key[8];
944         __m128i data[8];
945
946         eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
947         eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
948         eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
949         eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
950         eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
951         eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
952         eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
953         eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
954
955         /* Handle IPv4 headers.*/
956         ipv4_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv4_hdr *,
957                         sizeof(struct ether_hdr));
958         ipv4_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv4_hdr *,
959                         sizeof(struct ether_hdr));
960         ipv4_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv4_hdr *,
961                         sizeof(struct ether_hdr));
962         ipv4_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv4_hdr *,
963                         sizeof(struct ether_hdr));
964         ipv4_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv4_hdr *,
965                         sizeof(struct ether_hdr));
966         ipv4_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv4_hdr *,
967                         sizeof(struct ether_hdr));
968         ipv4_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv4_hdr *,
969                         sizeof(struct ether_hdr));
970         ipv4_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv4_hdr *,
971                         sizeof(struct ether_hdr));
972
973 #ifdef DO_RFC_1812_CHECKS
974         /* Check to make sure the packet is valid (RFC1812) */
975         uint8_t valid_mask = MASK_ALL_PKTS;
976
977         if (is_valid_ipv4_pkt(ipv4_hdr[0], m[0]->pkt_len) < 0) {
978                 rte_pktmbuf_free(m[0]);
979                 valid_mask &= EXCLUDE_1ST_PKT;
980         }
981         if (is_valid_ipv4_pkt(ipv4_hdr[1], m[1]->pkt_len) < 0) {
982                 rte_pktmbuf_free(m[1]);
983                 valid_mask &= EXCLUDE_2ND_PKT;
984         }
985         if (is_valid_ipv4_pkt(ipv4_hdr[2], m[2]->pkt_len) < 0) {
986                 rte_pktmbuf_free(m[2]);
987                 valid_mask &= EXCLUDE_3RD_PKT;
988         }
989         if (is_valid_ipv4_pkt(ipv4_hdr[3], m[3]->pkt_len) < 0) {
990                 rte_pktmbuf_free(m[3]);
991                 valid_mask &= EXCLUDE_4TH_PKT;
992         }
993         if (is_valid_ipv4_pkt(ipv4_hdr[4], m[4]->pkt_len) < 0) {
994                 rte_pktmbuf_free(m[4]);
995                 valid_mask &= EXCLUDE_5TH_PKT;
996         }
997         if (is_valid_ipv4_pkt(ipv4_hdr[5], m[5]->pkt_len) < 0) {
998                 rte_pktmbuf_free(m[5]);
999                 valid_mask &= EXCLUDE_6TH_PKT;
1000         }
1001         if (is_valid_ipv4_pkt(ipv4_hdr[6], m[6]->pkt_len) < 0) {
1002                 rte_pktmbuf_free(m[6]);
1003                 valid_mask &= EXCLUDE_7TH_PKT;
1004         }
1005         if (is_valid_ipv4_pkt(ipv4_hdr[7], m[7]->pkt_len) < 0) {
1006                 rte_pktmbuf_free(m[7]);
1007                 valid_mask &= EXCLUDE_8TH_PKT;
1008         }
1009         if (unlikely(valid_mask != MASK_ALL_PKTS)) {
1010                 if (valid_mask == 0)
1011                         return;
1012
1013                 uint8_t i = 0;
1014
1015                 for (i = 0; i < 8; i++)
1016                         if ((0x1 << i) & valid_mask)
1017                                 l3fwd_simple_forward(m[i], portid);
1018         }
1019 #endif /* End of #ifdef DO_RFC_1812_CHECKS */
1020
1021         data[0] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[0], __m128i *,
1022                         sizeof(struct ether_hdr) +
1023                         offsetof(struct ipv4_hdr, time_to_live)));
1024         data[1] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[1], __m128i *,
1025                         sizeof(struct ether_hdr) +
1026                         offsetof(struct ipv4_hdr, time_to_live)));
1027         data[2] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[2], __m128i *,
1028                         sizeof(struct ether_hdr) +
1029                         offsetof(struct ipv4_hdr, time_to_live)));
1030         data[3] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[3], __m128i *,
1031                         sizeof(struct ether_hdr) +
1032                         offsetof(struct ipv4_hdr, time_to_live)));
1033         data[4] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[4], __m128i *,
1034                         sizeof(struct ether_hdr) +
1035                         offsetof(struct ipv4_hdr, time_to_live)));
1036         data[5] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[5], __m128i *,
1037                         sizeof(struct ether_hdr) +
1038                         offsetof(struct ipv4_hdr, time_to_live)));
1039         data[6] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[6], __m128i *,
1040                         sizeof(struct ether_hdr) +
1041                         offsetof(struct ipv4_hdr, time_to_live)));
1042         data[7] = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m[7], __m128i *,
1043                         sizeof(struct ether_hdr) +
1044                         offsetof(struct ipv4_hdr, time_to_live)));
1045
1046         key[0].xmm = _mm_and_si128(data[0], mask0);
1047         key[1].xmm = _mm_and_si128(data[1], mask0);
1048         key[2].xmm = _mm_and_si128(data[2], mask0);
1049         key[3].xmm = _mm_and_si128(data[3], mask0);
1050         key[4].xmm = _mm_and_si128(data[4], mask0);
1051         key[5].xmm = _mm_and_si128(data[5], mask0);
1052         key[6].xmm = _mm_and_si128(data[6], mask0);
1053         key[7].xmm = _mm_and_si128(data[7], mask0);
1054
1055         const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1056                         &key[4], &key[5], &key[6], &key[7]};
1057
1058         rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct,
1059                         &key_array[0], 8, ret);
1060         dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv4_l3fwd_out_if[ret[0]]);
1061         dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv4_l3fwd_out_if[ret[1]]);
1062         dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv4_l3fwd_out_if[ret[2]]);
1063         dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv4_l3fwd_out_if[ret[3]]);
1064         dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv4_l3fwd_out_if[ret[4]]);
1065         dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv4_l3fwd_out_if[ret[5]]);
1066         dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv4_l3fwd_out_if[ret[6]]);
1067         dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv4_l3fwd_out_if[ret[7]]);
1068
1069         if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1070                         (enabled_port_mask & 1 << dst_port[0]) == 0)
1071                 dst_port[0] = portid;
1072         if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1073                         (enabled_port_mask & 1 << dst_port[1]) == 0)
1074                 dst_port[1] = portid;
1075         if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1076                         (enabled_port_mask & 1 << dst_port[2]) == 0)
1077                 dst_port[2] = portid;
1078         if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1079                         (enabled_port_mask & 1 << dst_port[3]) == 0)
1080                 dst_port[3] = portid;
1081         if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1082                         (enabled_port_mask & 1 << dst_port[4]) == 0)
1083                 dst_port[4] = portid;
1084         if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1085                         (enabled_port_mask & 1 << dst_port[5]) == 0)
1086                 dst_port[5] = portid;
1087         if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1088                         (enabled_port_mask & 1 << dst_port[6]) == 0)
1089                 dst_port[6] = portid;
1090         if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1091                         (enabled_port_mask & 1 << dst_port[7]) == 0)
1092                 dst_port[7] = portid;
1093
1094 #ifdef DO_RFC_1812_CHECKS
1095         /* Update time to live and header checksum */
1096         --(ipv4_hdr[0]->time_to_live);
1097         --(ipv4_hdr[1]->time_to_live);
1098         --(ipv4_hdr[2]->time_to_live);
1099         --(ipv4_hdr[3]->time_to_live);
1100         ++(ipv4_hdr[0]->hdr_checksum);
1101         ++(ipv4_hdr[1]->hdr_checksum);
1102         ++(ipv4_hdr[2]->hdr_checksum);
1103         ++(ipv4_hdr[3]->hdr_checksum);
1104         --(ipv4_hdr[4]->time_to_live);
1105         --(ipv4_hdr[5]->time_to_live);
1106         --(ipv4_hdr[6]->time_to_live);
1107         --(ipv4_hdr[7]->time_to_live);
1108         ++(ipv4_hdr[4]->hdr_checksum);
1109         ++(ipv4_hdr[5]->hdr_checksum);
1110         ++(ipv4_hdr[6]->hdr_checksum);
1111         ++(ipv4_hdr[7]->hdr_checksum);
1112 #endif
1113
1114         /* dst addr */
1115         *(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1116         *(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1117         *(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1118         *(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1119         *(uint64_t *)&eth_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1120         *(uint64_t *)&eth_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1121         *(uint64_t *)&eth_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1122         *(uint64_t *)&eth_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1123
1124         /* src addr */
1125         ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
1126         ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
1127         ether_addr_copy(&ports_eth_addr[dst_port[2]], &eth_hdr[2]->s_addr);
1128         ether_addr_copy(&ports_eth_addr[dst_port[3]], &eth_hdr[3]->s_addr);
1129         ether_addr_copy(&ports_eth_addr[dst_port[4]], &eth_hdr[4]->s_addr);
1130         ether_addr_copy(&ports_eth_addr[dst_port[5]], &eth_hdr[5]->s_addr);
1131         ether_addr_copy(&ports_eth_addr[dst_port[6]], &eth_hdr[6]->s_addr);
1132         ether_addr_copy(&ports_eth_addr[dst_port[7]], &eth_hdr[7]->s_addr);
1133
1134         send_single_packet(m[0], (uint8_t)dst_port[0]);
1135         send_single_packet(m[1], (uint8_t)dst_port[1]);
1136         send_single_packet(m[2], (uint8_t)dst_port[2]);
1137         send_single_packet(m[3], (uint8_t)dst_port[3]);
1138         send_single_packet(m[4], (uint8_t)dst_port[4]);
1139         send_single_packet(m[5], (uint8_t)dst_port[5]);
1140         send_single_packet(m[6], (uint8_t)dst_port[6]);
1141         send_single_packet(m[7], (uint8_t)dst_port[7]);
1142
1143 }
1144
1145 static inline void get_ipv6_5tuple(struct rte_mbuf *m0, __m128i mask0,
1146                 __m128i mask1, union ipv6_5tuple_host *key)
1147 {
1148         __m128i tmpdata0 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1149                         __m128i *, sizeof(struct ether_hdr) +
1150                         offsetof(struct ipv6_hdr, payload_len)));
1151         __m128i tmpdata1 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1152                         __m128i *, sizeof(struct ether_hdr) +
1153                         offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i)));
1154         __m128i tmpdata2 = _mm_loadu_si128(rte_pktmbuf_mtod_offset(m0,
1155                         __m128i *, sizeof(struct ether_hdr) +
1156                         offsetof(struct ipv6_hdr, payload_len) + sizeof(__m128i) +
1157                         sizeof(__m128i)));
1158         key->xmm[0] = _mm_and_si128(tmpdata0, mask0);
1159         key->xmm[1] = tmpdata1;
1160         key->xmm[2] = _mm_and_si128(tmpdata2, mask1);
1161 }
1162
1163 static inline void
1164 simple_ipv6_fwd_8pkts(struct rte_mbuf *m[8], uint8_t portid)
1165 {
1166         int32_t ret[8];
1167         uint8_t dst_port[8];
1168         struct ether_hdr *eth_hdr[8];
1169         union ipv6_5tuple_host key[8];
1170
1171         __attribute__((unused)) struct ipv6_hdr *ipv6_hdr[8];
1172
1173         eth_hdr[0] = rte_pktmbuf_mtod(m[0], struct ether_hdr *);
1174         eth_hdr[1] = rte_pktmbuf_mtod(m[1], struct ether_hdr *);
1175         eth_hdr[2] = rte_pktmbuf_mtod(m[2], struct ether_hdr *);
1176         eth_hdr[3] = rte_pktmbuf_mtod(m[3], struct ether_hdr *);
1177         eth_hdr[4] = rte_pktmbuf_mtod(m[4], struct ether_hdr *);
1178         eth_hdr[5] = rte_pktmbuf_mtod(m[5], struct ether_hdr *);
1179         eth_hdr[6] = rte_pktmbuf_mtod(m[6], struct ether_hdr *);
1180         eth_hdr[7] = rte_pktmbuf_mtod(m[7], struct ether_hdr *);
1181
1182         /* Handle IPv6 headers.*/
1183         ipv6_hdr[0] = rte_pktmbuf_mtod_offset(m[0], struct ipv6_hdr *,
1184                         sizeof(struct ether_hdr));
1185         ipv6_hdr[1] = rte_pktmbuf_mtod_offset(m[1], struct ipv6_hdr *,
1186                         sizeof(struct ether_hdr));
1187         ipv6_hdr[2] = rte_pktmbuf_mtod_offset(m[2], struct ipv6_hdr *,
1188                         sizeof(struct ether_hdr));
1189         ipv6_hdr[3] = rte_pktmbuf_mtod_offset(m[3], struct ipv6_hdr *,
1190                         sizeof(struct ether_hdr));
1191         ipv6_hdr[4] = rte_pktmbuf_mtod_offset(m[4], struct ipv6_hdr *,
1192                         sizeof(struct ether_hdr));
1193         ipv6_hdr[5] = rte_pktmbuf_mtod_offset(m[5], struct ipv6_hdr *,
1194                         sizeof(struct ether_hdr));
1195         ipv6_hdr[6] = rte_pktmbuf_mtod_offset(m[6], struct ipv6_hdr *,
1196                         sizeof(struct ether_hdr));
1197         ipv6_hdr[7] = rte_pktmbuf_mtod_offset(m[7], struct ipv6_hdr *,
1198                         sizeof(struct ether_hdr));
1199
1200         get_ipv6_5tuple(m[0], mask1, mask2, &key[0]);
1201         get_ipv6_5tuple(m[1], mask1, mask2, &key[1]);
1202         get_ipv6_5tuple(m[2], mask1, mask2, &key[2]);
1203         get_ipv6_5tuple(m[3], mask1, mask2, &key[3]);
1204         get_ipv6_5tuple(m[4], mask1, mask2, &key[4]);
1205         get_ipv6_5tuple(m[5], mask1, mask2, &key[5]);
1206         get_ipv6_5tuple(m[6], mask1, mask2, &key[6]);
1207         get_ipv6_5tuple(m[7], mask1, mask2, &key[7]);
1208
1209         const void *key_array[8] = {&key[0], &key[1], &key[2], &key[3],
1210                         &key[4], &key[5], &key[6], &key[7]};
1211
1212         rte_hash_lookup_bulk(RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1213                         &key_array[0], 4, ret);
1214         dst_port[0] = (uint8_t) ((ret[0] < 0) ? portid : ipv6_l3fwd_out_if[ret[0]]);
1215         dst_port[1] = (uint8_t) ((ret[1] < 0) ? portid : ipv6_l3fwd_out_if[ret[1]]);
1216         dst_port[2] = (uint8_t) ((ret[2] < 0) ? portid : ipv6_l3fwd_out_if[ret[2]]);
1217         dst_port[3] = (uint8_t) ((ret[3] < 0) ? portid : ipv6_l3fwd_out_if[ret[3]]);
1218         dst_port[4] = (uint8_t) ((ret[4] < 0) ? portid : ipv6_l3fwd_out_if[ret[4]]);
1219         dst_port[5] = (uint8_t) ((ret[5] < 0) ? portid : ipv6_l3fwd_out_if[ret[5]]);
1220         dst_port[6] = (uint8_t) ((ret[6] < 0) ? portid : ipv6_l3fwd_out_if[ret[6]]);
1221         dst_port[7] = (uint8_t) ((ret[7] < 0) ? portid : ipv6_l3fwd_out_if[ret[7]]);
1222
1223         if (dst_port[0] >= RTE_MAX_ETHPORTS ||
1224                         (enabled_port_mask & 1 << dst_port[0]) == 0)
1225                 dst_port[0] = portid;
1226         if (dst_port[1] >= RTE_MAX_ETHPORTS ||
1227                         (enabled_port_mask & 1 << dst_port[1]) == 0)
1228                 dst_port[1] = portid;
1229         if (dst_port[2] >= RTE_MAX_ETHPORTS ||
1230                         (enabled_port_mask & 1 << dst_port[2]) == 0)
1231                 dst_port[2] = portid;
1232         if (dst_port[3] >= RTE_MAX_ETHPORTS ||
1233                         (enabled_port_mask & 1 << dst_port[3]) == 0)
1234                 dst_port[3] = portid;
1235         if (dst_port[4] >= RTE_MAX_ETHPORTS ||
1236                         (enabled_port_mask & 1 << dst_port[4]) == 0)
1237                 dst_port[4] = portid;
1238         if (dst_port[5] >= RTE_MAX_ETHPORTS ||
1239                         (enabled_port_mask & 1 << dst_port[5]) == 0)
1240                 dst_port[5] = portid;
1241         if (dst_port[6] >= RTE_MAX_ETHPORTS ||
1242                         (enabled_port_mask & 1 << dst_port[6]) == 0)
1243                 dst_port[6] = portid;
1244         if (dst_port[7] >= RTE_MAX_ETHPORTS ||
1245                         (enabled_port_mask & 1 << dst_port[7]) == 0)
1246                 dst_port[7] = portid;
1247
1248         /* dst addr */
1249         *(uint64_t *)&eth_hdr[0]->d_addr = dest_eth_addr[dst_port[0]];
1250         *(uint64_t *)&eth_hdr[1]->d_addr = dest_eth_addr[dst_port[1]];
1251         *(uint64_t *)&eth_hdr[2]->d_addr = dest_eth_addr[dst_port[2]];
1252         *(uint64_t *)&eth_hdr[3]->d_addr = dest_eth_addr[dst_port[3]];
1253         *(uint64_t *)&eth_hdr[4]->d_addr = dest_eth_addr[dst_port[4]];
1254         *(uint64_t *)&eth_hdr[5]->d_addr = dest_eth_addr[dst_port[5]];
1255         *(uint64_t *)&eth_hdr[6]->d_addr = dest_eth_addr[dst_port[6]];
1256         *(uint64_t *)&eth_hdr[7]->d_addr = dest_eth_addr[dst_port[7]];
1257
1258         /* src addr */
1259         ether_addr_copy(&ports_eth_addr[dst_port[0]], &eth_hdr[0]->s_addr);
1260         ether_addr_copy(&ports_eth_addr[dst_port[1]], &eth_hdr[1]->s_addr);
1261         ether_addr_copy(&ports_eth_addr[dst_port[2]], &eth_hdr[2]->s_addr);
1262         ether_addr_copy(&ports_eth_addr[dst_port[3]], &eth_hdr[3]->s_addr);
1263         ether_addr_copy(&ports_eth_addr[dst_port[4]], &eth_hdr[4]->s_addr);
1264         ether_addr_copy(&ports_eth_addr[dst_port[5]], &eth_hdr[5]->s_addr);
1265         ether_addr_copy(&ports_eth_addr[dst_port[6]], &eth_hdr[6]->s_addr);
1266         ether_addr_copy(&ports_eth_addr[dst_port[7]], &eth_hdr[7]->s_addr);
1267
1268         send_single_packet(m[0], (uint8_t)dst_port[0]);
1269         send_single_packet(m[1], (uint8_t)dst_port[1]);
1270         send_single_packet(m[2], (uint8_t)dst_port[2]);
1271         send_single_packet(m[3], (uint8_t)dst_port[3]);
1272         send_single_packet(m[4], (uint8_t)dst_port[4]);
1273         send_single_packet(m[5], (uint8_t)dst_port[5]);
1274         send_single_packet(m[6], (uint8_t)dst_port[6]);
1275         send_single_packet(m[7], (uint8_t)dst_port[7]);
1276
1277 }
1278 #endif /* APP_LOOKUP_METHOD */
1279
1280 static inline __attribute__((always_inline)) void
1281 l3fwd_simple_forward(struct rte_mbuf *m, uint8_t portid)
1282 {
1283         struct ether_hdr *eth_hdr;
1284         struct ipv4_hdr *ipv4_hdr;
1285         uint8_t dst_port;
1286
1287         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
1288
1289         if (RTE_ETH_IS_IPV4_HDR(m->packet_type)) {
1290                 /* Handle IPv4 headers.*/
1291                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
1292                                 sizeof(struct ether_hdr));
1293
1294 #ifdef DO_RFC_1812_CHECKS
1295                 /* Check to make sure the packet is valid (RFC1812) */
1296                 if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
1297                         rte_pktmbuf_free(m);
1298                         return;
1299                 }
1300 #endif
1301
1302                  dst_port = get_ipv4_dst_port(ipv4_hdr, portid,
1303                         RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct);
1304                 if (dst_port >= RTE_MAX_ETHPORTS ||
1305                                 (enabled_port_mask & 1 << dst_port) == 0)
1306                         dst_port = portid;
1307
1308 #ifdef DO_RFC_1812_CHECKS
1309                 /* Update time to live and header checksum */
1310                 --(ipv4_hdr->time_to_live);
1311                 ++(ipv4_hdr->hdr_checksum);
1312 #endif
1313                 /* dst addr */
1314                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
1315
1316                 /* src addr */
1317                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
1318
1319                 send_single_packet(m, dst_port);
1320         } else if (RTE_ETH_IS_IPV6_HDR(m->packet_type)) {
1321                 /* Handle IPv6 headers.*/
1322                 struct ipv6_hdr *ipv6_hdr;
1323
1324                 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct ipv6_hdr *,
1325                                 sizeof(struct ether_hdr));
1326
1327                 dst_port = get_ipv6_dst_port(ipv6_hdr, portid,
1328                                 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct);
1329
1330                 if (dst_port >= RTE_MAX_ETHPORTS ||
1331                                 (enabled_port_mask & 1 << dst_port) == 0)
1332                         dst_port = portid;
1333
1334                 /* dst addr */
1335                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[dst_port];
1336
1337                 /* src addr */
1338                 ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
1339
1340                 send_single_packet(m, dst_port);
1341         } else
1342                 /* Free the mbuf that contains non-IPV4/IPV6 packet */
1343                 rte_pktmbuf_free(m);
1344 }
1345
1346 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1347         (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1348 #ifdef DO_RFC_1812_CHECKS
1349
1350 #define IPV4_MIN_VER_IHL        0x45
1351 #define IPV4_MAX_VER_IHL        0x4f
1352 #define IPV4_MAX_VER_IHL_DIFF   (IPV4_MAX_VER_IHL - IPV4_MIN_VER_IHL)
1353
1354 /* Minimum value of IPV4 total length (20B) in network byte order. */
1355 #define IPV4_MIN_LEN_BE (sizeof(struct ipv4_hdr) << 8)
1356
1357 /*
1358  * From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2:
1359  * - The IP version number must be 4.
1360  * - The IP header length field must be large enough to hold the
1361  *    minimum length legal IP datagram (20 bytes = 5 words).
1362  * - The IP total length field must be large enough to hold the IP
1363  *   datagram header, whose length is specified in the IP header length
1364  *   field.
1365  * If we encounter invalid IPV4 packet, then set destination port for it
1366  * to BAD_PORT value.
1367  */
1368 static inline __attribute__((always_inline)) void
1369 rfc1812_process(struct ipv4_hdr *ipv4_hdr, uint16_t *dp, uint32_t ptype)
1370 {
1371         uint8_t ihl;
1372
1373         if (RTE_ETH_IS_IPV4_HDR(ptype)) {
1374                 ihl = ipv4_hdr->version_ihl - IPV4_MIN_VER_IHL;
1375
1376                 ipv4_hdr->time_to_live--;
1377                 ipv4_hdr->hdr_checksum++;
1378
1379                 if (ihl > IPV4_MAX_VER_IHL_DIFF ||
1380                                 ((uint8_t)ipv4_hdr->total_length == 0 &&
1381                                 ipv4_hdr->total_length < IPV4_MIN_LEN_BE)) {
1382                         dp[0] = BAD_PORT;
1383                 }
1384         }
1385 }
1386
1387 #else
1388 #define rfc1812_process(mb, dp, ptype)  do { } while (0)
1389 #endif /* DO_RFC_1812_CHECKS */
1390 #endif /* APP_LOOKUP_LPM && ENABLE_MULTI_BUFFER_OPTIMIZE */
1391
1392
1393 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1394         (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1395
1396 static inline __attribute__((always_inline)) uint16_t
1397 get_dst_port(struct rte_mbuf *pkt, uint32_t dst_ipv4, uint8_t portid)
1398 {
1399         uint32_t next_hop;
1400         struct ipv6_hdr *ipv6_hdr;
1401         struct ether_hdr *eth_hdr;
1402
1403         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
1404                 return (uint16_t) ((rte_lpm_lookup(
1405                                 RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dst_ipv4,
1406                                 &next_hop) == 0) ? next_hop : portid);
1407
1408         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
1409
1410                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1411                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
1412
1413                 return (uint16_t) ((rte_lpm6_lookup(
1414                                 RTE_PER_LCORE(lcore_conf)->ipv6_lookup_struct,
1415                                 ipv6_hdr->dst_addr, &next_hop) == 0) ?
1416                                 next_hop : portid);
1417
1418         }
1419
1420         return portid;
1421 }
1422
1423 static inline void
1424 process_packet(struct rte_mbuf *pkt, uint16_t *dst_port, uint8_t portid)
1425 {
1426         struct ether_hdr *eth_hdr;
1427         struct ipv4_hdr *ipv4_hdr;
1428         uint32_t dst_ipv4;
1429         uint16_t dp;
1430         __m128i te, ve;
1431
1432         eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
1433         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1434
1435         dst_ipv4 = ipv4_hdr->dst_addr;
1436         dst_ipv4 = rte_be_to_cpu_32(dst_ipv4);
1437         dp = get_dst_port(pkt, dst_ipv4, portid);
1438
1439         te = _mm_load_si128((__m128i *)eth_hdr);
1440         ve = val_eth[dp];
1441
1442         dst_port[0] = dp;
1443         rfc1812_process(ipv4_hdr, dst_port, pkt->packet_type);
1444
1445         te =  _mm_blend_epi16(te, ve, MASK_ETH);
1446         _mm_store_si128((__m128i *)eth_hdr, te);
1447 }
1448
1449 /*
1450  * Read packet_type and destination IPV4 addresses from 4 mbufs.
1451  */
1452 static inline void
1453 processx4_step1(struct rte_mbuf *pkt[FWDSTEP],
1454                 __m128i *dip,
1455                 uint32_t *ipv4_flag)
1456 {
1457         struct ipv4_hdr *ipv4_hdr;
1458         struct ether_hdr *eth_hdr;
1459         uint32_t x0, x1, x2, x3;
1460
1461         eth_hdr = rte_pktmbuf_mtod(pkt[0], struct ether_hdr *);
1462         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1463         x0 = ipv4_hdr->dst_addr;
1464         ipv4_flag[0] = pkt[0]->packet_type & RTE_PTYPE_L3_IPV4;
1465
1466         eth_hdr = rte_pktmbuf_mtod(pkt[1], struct ether_hdr *);
1467         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1468         x1 = ipv4_hdr->dst_addr;
1469         ipv4_flag[0] &= pkt[1]->packet_type;
1470
1471         eth_hdr = rte_pktmbuf_mtod(pkt[2], struct ether_hdr *);
1472         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1473         x2 = ipv4_hdr->dst_addr;
1474         ipv4_flag[0] &= pkt[2]->packet_type;
1475
1476         eth_hdr = rte_pktmbuf_mtod(pkt[3], struct ether_hdr *);
1477         ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
1478         x3 = ipv4_hdr->dst_addr;
1479         ipv4_flag[0] &= pkt[3]->packet_type;
1480
1481         dip[0] = _mm_set_epi32(x3, x2, x1, x0);
1482 }
1483
1484 /*
1485  * Lookup into LPM for destination port.
1486  * If lookup fails, use incoming port (portid) as destination port.
1487  */
1488 static inline void
1489 processx4_step2(__m128i dip,
1490                 uint32_t ipv4_flag,
1491                 uint8_t portid,
1492                 struct rte_mbuf *pkt[FWDSTEP],
1493                 uint16_t dprt[FWDSTEP])
1494 {
1495         rte_xmm_t dst;
1496         const __m128i bswap_mask = _mm_set_epi8(12, 13, 14, 15, 8, 9, 10, 11,
1497                         4, 5, 6, 7, 0, 1, 2, 3);
1498
1499         /* Byte swap 4 IPV4 addresses. */
1500         dip = _mm_shuffle_epi8(dip, bswap_mask);
1501
1502         /* if all 4 packets are IPV4. */
1503         if (likely(ipv4_flag)) {
1504                 rte_lpm_lookupx4(RTE_PER_LCORE(lcore_conf)->ipv4_lookup_struct, dip,
1505                                 dst.u32, portid);
1506
1507                 /* get rid of unused upper 16 bit for each dport. */
1508                 dst.x = _mm_packs_epi32(dst.x, dst.x);
1509                 *(uint64_t *)dprt = dst.u64[0];
1510         } else {
1511                 dst.x = dip;
1512                 dprt[0] = get_dst_port(pkt[0], dst.u32[0], portid);
1513                 dprt[1] = get_dst_port(pkt[1], dst.u32[1], portid);
1514                 dprt[2] = get_dst_port(pkt[2], dst.u32[2], portid);
1515                 dprt[3] = get_dst_port(pkt[3], dst.u32[3], portid);
1516         }
1517 }
1518
1519 /*
1520  * Update source and destination MAC addresses in the ethernet header.
1521  * Perform RFC1812 checks and updates for IPV4 packets.
1522  */
1523 static inline void
1524 processx4_step3(struct rte_mbuf *pkt[FWDSTEP], uint16_t dst_port[FWDSTEP])
1525 {
1526         __m128i te[FWDSTEP];
1527         __m128i ve[FWDSTEP];
1528         __m128i *p[FWDSTEP];
1529
1530         p[0] = rte_pktmbuf_mtod(pkt[0], __m128i *);
1531         p[1] = rte_pktmbuf_mtod(pkt[1], __m128i *);
1532         p[2] = rte_pktmbuf_mtod(pkt[2], __m128i *);
1533         p[3] = rte_pktmbuf_mtod(pkt[3], __m128i *);
1534
1535         ve[0] = val_eth[dst_port[0]];
1536         te[0] = _mm_load_si128(p[0]);
1537
1538         ve[1] = val_eth[dst_port[1]];
1539         te[1] = _mm_load_si128(p[1]);
1540
1541         ve[2] = val_eth[dst_port[2]];
1542         te[2] = _mm_load_si128(p[2]);
1543
1544         ve[3] = val_eth[dst_port[3]];
1545         te[3] = _mm_load_si128(p[3]);
1546
1547         /* Update first 12 bytes, keep rest bytes intact. */
1548         te[0] =  _mm_blend_epi16(te[0], ve[0], MASK_ETH);
1549         te[1] =  _mm_blend_epi16(te[1], ve[1], MASK_ETH);
1550         te[2] =  _mm_blend_epi16(te[2], ve[2], MASK_ETH);
1551         te[3] =  _mm_blend_epi16(te[3], ve[3], MASK_ETH);
1552
1553         _mm_store_si128(p[0], te[0]);
1554         _mm_store_si128(p[1], te[1]);
1555         _mm_store_si128(p[2], te[2]);
1556         _mm_store_si128(p[3], te[3]);
1557
1558         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[0] + 1),
1559                         &dst_port[0], pkt[0]->packet_type);
1560         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[1] + 1),
1561                         &dst_port[1], pkt[1]->packet_type);
1562         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[2] + 1),
1563                         &dst_port[2], pkt[2]->packet_type);
1564         rfc1812_process((struct ipv4_hdr *)((struct ether_hdr *)p[3] + 1),
1565                         &dst_port[3], pkt[3]->packet_type);
1566 }
1567
1568 /*
1569  * We group consecutive packets with the same destionation port into one burst.
1570  * To avoid extra latency this is done together with some other packet
1571  * processing, but after we made a final decision about packet's destination.
1572  * To do this we maintain:
1573  * pnum - array of number of consecutive packets with the same dest port for
1574  * each packet in the input burst.
1575  * lp - pointer to the last updated element in the pnum.
1576  * dlp - dest port value lp corresponds to.
1577  */
1578
1579 #define GRPSZ   (1 << FWDSTEP)
1580 #define GRPMSK  (GRPSZ - 1)
1581
1582 #define GROUP_PORT_STEP(dlp, dcp, lp, pn, idx)  do { \
1583         if (likely((dlp) == (dcp)[(idx)])) {         \
1584                 (lp)[0]++;                           \
1585         } else {                                     \
1586                 (dlp) = (dcp)[idx];                  \
1587                 (lp) = (pn) + (idx);                 \
1588                 (lp)[0] = 1;                         \
1589         }                                            \
1590 } while (0)
1591
1592 /*
1593  * Group consecutive packets with the same destination port in bursts of 4.
1594  * Suppose we have array of destionation ports:
1595  * dst_port[] = {a, b, c, d,, e, ... }
1596  * dp1 should contain: <a, b, c, d>, dp2: <b, c, d, e>.
1597  * We doing 4 comparisions at once and the result is 4 bit mask.
1598  * This mask is used as an index into prebuild array of pnum values.
1599  */
1600 static inline uint16_t *
1601 port_groupx4(uint16_t pn[FWDSTEP + 1], uint16_t *lp, __m128i dp1, __m128i dp2)
1602 {
1603         static const struct {
1604                 uint64_t pnum; /* prebuild 4 values for pnum[]. */
1605                 int32_t  idx;  /* index for new last updated elemnet. */
1606                 uint16_t lpv;  /* add value to the last updated element. */
1607         } gptbl[GRPSZ] = {
1608         {
1609                 /* 0: a != b, b != c, c != d, d != e */
1610                 .pnum = UINT64_C(0x0001000100010001),
1611                 .idx = 4,
1612                 .lpv = 0,
1613         },
1614         {
1615                 /* 1: a == b, b != c, c != d, d != e */
1616                 .pnum = UINT64_C(0x0001000100010002),
1617                 .idx = 4,
1618                 .lpv = 1,
1619         },
1620         {
1621                 /* 2: a != b, b == c, c != d, d != e */
1622                 .pnum = UINT64_C(0x0001000100020001),
1623                 .idx = 4,
1624                 .lpv = 0,
1625         },
1626         {
1627                 /* 3: a == b, b == c, c != d, d != e */
1628                 .pnum = UINT64_C(0x0001000100020003),
1629                 .idx = 4,
1630                 .lpv = 2,
1631         },
1632         {
1633                 /* 4: a != b, b != c, c == d, d != e */
1634                 .pnum = UINT64_C(0x0001000200010001),
1635                 .idx = 4,
1636                 .lpv = 0,
1637         },
1638         {
1639                 /* 5: a == b, b != c, c == d, d != e */
1640                 .pnum = UINT64_C(0x0001000200010002),
1641                 .idx = 4,
1642                 .lpv = 1,
1643         },
1644         {
1645                 /* 6: a != b, b == c, c == d, d != e */
1646                 .pnum = UINT64_C(0x0001000200030001),
1647                 .idx = 4,
1648                 .lpv = 0,
1649         },
1650         {
1651                 /* 7: a == b, b == c, c == d, d != e */
1652                 .pnum = UINT64_C(0x0001000200030004),
1653                 .idx = 4,
1654                 .lpv = 3,
1655         },
1656         {
1657                 /* 8: a != b, b != c, c != d, d == e */
1658                 .pnum = UINT64_C(0x0002000100010001),
1659                 .idx = 3,
1660                 .lpv = 0,
1661         },
1662         {
1663                 /* 9: a == b, b != c, c != d, d == e */
1664                 .pnum = UINT64_C(0x0002000100010002),
1665                 .idx = 3,
1666                 .lpv = 1,
1667         },
1668         {
1669                 /* 0xa: a != b, b == c, c != d, d == e */
1670                 .pnum = UINT64_C(0x0002000100020001),
1671                 .idx = 3,
1672                 .lpv = 0,
1673         },
1674         {
1675                 /* 0xb: a == b, b == c, c != d, d == e */
1676                 .pnum = UINT64_C(0x0002000100020003),
1677                 .idx = 3,
1678                 .lpv = 2,
1679         },
1680         {
1681                 /* 0xc: a != b, b != c, c == d, d == e */
1682                 .pnum = UINT64_C(0x0002000300010001),
1683                 .idx = 2,
1684                 .lpv = 0,
1685         },
1686         {
1687                 /* 0xd: a == b, b != c, c == d, d == e */
1688                 .pnum = UINT64_C(0x0002000300010002),
1689                 .idx = 2,
1690                 .lpv = 1,
1691         },
1692         {
1693                 /* 0xe: a != b, b == c, c == d, d == e */
1694                 .pnum = UINT64_C(0x0002000300040001),
1695                 .idx = 1,
1696                 .lpv = 0,
1697         },
1698         {
1699                 /* 0xf: a == b, b == c, c == d, d == e */
1700                 .pnum = UINT64_C(0x0002000300040005),
1701                 .idx = 0,
1702                 .lpv = 4,
1703         },
1704         };
1705
1706         union {
1707                 uint16_t u16[FWDSTEP + 1];
1708                 uint64_t u64;
1709         } *pnum = (void *)pn;
1710
1711         int32_t v;
1712
1713         dp1 = _mm_cmpeq_epi16(dp1, dp2);
1714         dp1 = _mm_unpacklo_epi16(dp1, dp1);
1715         v = _mm_movemask_ps((__m128)dp1);
1716
1717         /* update last port counter. */
1718         lp[0] += gptbl[v].lpv;
1719
1720         /* if dest port value has changed. */
1721         if (v != GRPMSK) {
1722                 pnum->u64 = gptbl[v].pnum;
1723                 pnum->u16[FWDSTEP] = 1;
1724                 lp = pnum->u16 + gptbl[v].idx;
1725         }
1726
1727         return lp;
1728 }
1729
1730 #endif /* APP_LOOKUP_METHOD */
1731
1732 static void
1733 process_burst(struct rte_mbuf *pkts_burst[MAX_PKT_BURST], int nb_rx,
1734                 uint8_t portid) {
1735
1736         int j;
1737
1738 #if ((APP_LOOKUP_METHOD == APP_LOOKUP_LPM) && \
1739         (ENABLE_MULTI_BUFFER_OPTIMIZE == 1))
1740         int32_t k;
1741         uint16_t dlp;
1742         uint16_t *lp;
1743         uint16_t dst_port[MAX_PKT_BURST];
1744         __m128i dip[MAX_PKT_BURST / FWDSTEP];
1745         uint32_t ipv4_flag[MAX_PKT_BURST / FWDSTEP];
1746         uint16_t pnum[MAX_PKT_BURST + 1];
1747 #endif
1748
1749
1750 #if (ENABLE_MULTI_BUFFER_OPTIMIZE == 1)
1751 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
1752         {
1753                 /*
1754                  * Send nb_rx - nb_rx%8 packets
1755                  * in groups of 8.
1756                  */
1757                 int32_t n = RTE_ALIGN_FLOOR(nb_rx, 8);
1758
1759                 for (j = 0; j < n; j += 8) {
1760                         uint32_t pkt_type =
1761                                 pkts_burst[j]->packet_type &
1762                                 pkts_burst[j+1]->packet_type &
1763                                 pkts_burst[j+2]->packet_type &
1764                                 pkts_burst[j+3]->packet_type &
1765                                 pkts_burst[j+4]->packet_type &
1766                                 pkts_burst[j+5]->packet_type &
1767                                 pkts_burst[j+6]->packet_type &
1768                                 pkts_burst[j+7]->packet_type;
1769                         if (pkt_type & RTE_PTYPE_L3_IPV4) {
1770                                 simple_ipv4_fwd_8pkts(&pkts_burst[j], portid);
1771                         } else if (pkt_type &
1772                                 RTE_PTYPE_L3_IPV6) {
1773                                 simple_ipv6_fwd_8pkts(&pkts_burst[j], portid);
1774                         } else {
1775                                 l3fwd_simple_forward(pkts_burst[j], portid);
1776                                 l3fwd_simple_forward(pkts_burst[j+1], portid);
1777                                 l3fwd_simple_forward(pkts_burst[j+2], portid);
1778                                 l3fwd_simple_forward(pkts_burst[j+3], portid);
1779                                 l3fwd_simple_forward(pkts_burst[j+4], portid);
1780                                 l3fwd_simple_forward(pkts_burst[j+5], portid);
1781                                 l3fwd_simple_forward(pkts_burst[j+6], portid);
1782                                 l3fwd_simple_forward(pkts_burst[j+7], portid);
1783                         }
1784                 }
1785                 for (; j < nb_rx ; j++)
1786                         l3fwd_simple_forward(pkts_burst[j], portid);
1787         }
1788 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
1789
1790         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1791         for (j = 0; j != k; j += FWDSTEP)
1792                 processx4_step1(&pkts_burst[j], &dip[j / FWDSTEP],
1793                                 &ipv4_flag[j / FWDSTEP]);
1794
1795         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1796         for (j = 0; j != k; j += FWDSTEP)
1797                 processx4_step2(dip[j / FWDSTEP], ipv4_flag[j / FWDSTEP],
1798                                 portid, &pkts_burst[j], &dst_port[j]);
1799
1800         /*
1801          * Finish packet processing and group consecutive
1802          * packets with the same destination port.
1803          */
1804         k = RTE_ALIGN_FLOOR(nb_rx, FWDSTEP);
1805         if (k != 0) {
1806                 __m128i dp1, dp2;
1807
1808                 lp = pnum;
1809                 lp[0] = 1;
1810
1811                 processx4_step3(pkts_burst, dst_port);
1812
1813                 /* dp1: <d[0], d[1], d[2], d[3], ... > */
1814                 dp1 = _mm_loadu_si128((__m128i *)dst_port);
1815
1816                 for (j = FWDSTEP; j != k; j += FWDSTEP) {
1817                         processx4_step3(&pkts_burst[j], &dst_port[j]);
1818
1819                         /*
1820                          * dp2:
1821                          * <d[j-3], d[j-2], d[j-1], d[j], ... >
1822                          */
1823                         dp2 = _mm_loadu_si128(
1824                                         (__m128i *)&dst_port[j - FWDSTEP + 1]);
1825                         lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1826
1827                         /*
1828                          * dp1:
1829                          * <d[j], d[j+1], d[j+2], d[j+3], ... >
1830                          */
1831                         dp1 = _mm_srli_si128(dp2, (FWDSTEP - 1) *
1832                                         sizeof(dst_port[0]));
1833                 }
1834
1835                 /*
1836                  * dp2: <d[j-3], d[j-2], d[j-1], d[j-1], ... >
1837                  */
1838                 dp2 = _mm_shufflelo_epi16(dp1, 0xf9);
1839                 lp  = port_groupx4(&pnum[j - FWDSTEP], lp, dp1, dp2);
1840
1841                 /*
1842                  * remove values added by the last repeated
1843                  * dst port.
1844                  */
1845                 lp[0]--;
1846                 dlp = dst_port[j - 1];
1847         } else {
1848                 /* set dlp and lp to the never used values. */
1849                 dlp = BAD_PORT - 1;
1850                 lp = pnum + MAX_PKT_BURST;
1851         }
1852
1853         /* Process up to last 3 packets one by one. */
1854         switch (nb_rx % FWDSTEP) {
1855         case 3:
1856                 process_packet(pkts_burst[j], dst_port + j, portid);
1857                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1858                 j++;
1859         case 2:
1860                 process_packet(pkts_burst[j], dst_port + j, portid);
1861                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1862                 j++;
1863         case 1:
1864                 process_packet(pkts_burst[j], dst_port + j, portid);
1865                 GROUP_PORT_STEP(dlp, dst_port, lp, pnum, j);
1866                 j++;
1867         }
1868
1869         /*
1870          * Send packets out, through destination port.
1871          * Consecuteve pacekts with the same destination port
1872          * are already grouped together.
1873          * If destination port for the packet equals BAD_PORT,
1874          * then free the packet without sending it out.
1875          */
1876         for (j = 0; j < nb_rx; j += k) {
1877
1878                 int32_t m;
1879                 uint16_t pn;
1880
1881                 pn = dst_port[j];
1882                 k = pnum[j];
1883
1884                 if (likely(pn != BAD_PORT))
1885                         send_packetsx4(pn, pkts_burst + j, k);
1886                 else
1887                         for (m = j; m != j + k; m++)
1888                                 rte_pktmbuf_free(pkts_burst[m]);
1889
1890         }
1891
1892 #endif /* APP_LOOKUP_METHOD */
1893 #else /* ENABLE_MULTI_BUFFER_OPTIMIZE == 0 */
1894
1895         /* Prefetch first packets */
1896         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++)
1897                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[j], void *));
1898
1899         /* Prefetch and forward already prefetched packets */
1900         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
1901                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
1902                                 j + PREFETCH_OFFSET], void *));
1903                 l3fwd_simple_forward(pkts_burst[j], portid);
1904         }
1905
1906         /* Forward remaining prefetched packets */
1907         for (; j < nb_rx; j++)
1908                 l3fwd_simple_forward(pkts_burst[j], portid);
1909
1910 #endif /* ENABLE_MULTI_BUFFER_OPTIMIZE */
1911
1912 }
1913
1914 #if (APP_CPU_LOAD > 0)
1915
1916 /*
1917  * CPU-load stats collector
1918  */
1919 static int
1920 cpu_load_collector(__rte_unused void *arg) {
1921         unsigned i, j, k;
1922         uint64_t hits;
1923         uint64_t prev_tsc, diff_tsc, cur_tsc;
1924         uint64_t total[MAX_CPU] = { 0 };
1925         unsigned min_cpu = MAX_CPU;
1926         unsigned max_cpu = 0;
1927         unsigned cpu_id;
1928         int busy_total = 0;
1929         int busy_flag = 0;
1930
1931         unsigned int n_thread_per_cpu[MAX_CPU] = { 0 };
1932         struct thread_conf *thread_per_cpu[MAX_CPU][MAX_THREAD];
1933
1934         struct thread_conf *thread_conf;
1935
1936         const uint64_t interval_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
1937                 US_PER_S * CPU_LOAD_TIMEOUT_US;
1938
1939         prev_tsc = 0;
1940         /*
1941          * Wait for all threads
1942          */
1943
1944         printf("Waiting for %d rx threads and %d tx threads\n", n_rx_thread,
1945                         n_tx_thread);
1946
1947         while (rte_atomic16_read(&rx_counter) < n_rx_thread)
1948                 rte_pause();
1949
1950         while (rte_atomic16_read(&tx_counter) < n_tx_thread)
1951                 rte_pause();
1952
1953         for (i = 0; i < n_rx_thread; i++) {
1954
1955                 thread_conf = &rx_thread[i].conf;
1956                 cpu_id = thread_conf->cpu_id;
1957                 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1958
1959                 if (cpu_id > max_cpu)
1960                         max_cpu = cpu_id;
1961                 if (cpu_id < min_cpu)
1962                         min_cpu = cpu_id;
1963         }
1964         for (i = 0; i < n_tx_thread; i++) {
1965
1966                 thread_conf = &tx_thread[i].conf;
1967                 cpu_id = thread_conf->cpu_id;
1968                 thread_per_cpu[cpu_id][n_thread_per_cpu[cpu_id]++] = thread_conf;
1969
1970                 if (thread_conf->cpu_id > max_cpu)
1971                         max_cpu = thread_conf->cpu_id;
1972                 if (thread_conf->cpu_id < min_cpu)
1973                         min_cpu = thread_conf->cpu_id;
1974         }
1975
1976         while (1) {
1977
1978                 cpu_load.counter++;
1979                 for (i = min_cpu; i <= max_cpu; i++) {
1980                         for (j = 0; j < MAX_CPU_COUNTER; j++) {
1981                                 for (k = 0; k < n_thread_per_cpu[i]; k++)
1982                                         if (thread_per_cpu[i][k]->busy[j]) {
1983                                                 busy_flag = 1;
1984                                                 break;
1985                                         }
1986                                 if (busy_flag) {
1987                                         cpu_load.hits[j][i]++;
1988                                         busy_total = 1;
1989                                         busy_flag = 0;
1990                                 }
1991                         }
1992
1993                         if (busy_total) {
1994                                 total[i]++;
1995                                 busy_total = 0;
1996                         }
1997                 }
1998
1999                 cur_tsc = rte_rdtsc();
2000
2001                 diff_tsc = cur_tsc - prev_tsc;
2002                 if (unlikely(diff_tsc > interval_tsc)) {
2003
2004                         printf("\033c");
2005
2006                         printf("Cpu usage for %d rx threads and %d tx threads:\n\n",
2007                                         n_rx_thread, n_tx_thread);
2008
2009                         printf("cpu#     proc%%  poll%%  overhead%%\n\n");
2010
2011                         for (i = min_cpu; i <= max_cpu; i++) {
2012                                 hits = 0;
2013                                 printf("CPU %d:", i);
2014                                 for (j = 0; j < MAX_CPU_COUNTER; j++) {
2015                                         printf("%7" PRIu64 "",
2016                                                         cpu_load.hits[j][i] * 100 / cpu_load.counter);
2017                                         hits += cpu_load.hits[j][i];
2018                                         cpu_load.hits[j][i] = 0;
2019                                 }
2020                                 printf("%7" PRIu64 "\n",
2021                                                 100 - total[i] * 100 / cpu_load.counter);
2022                                 total[i] = 0;
2023                         }
2024                         cpu_load.counter = 0;
2025
2026                         prev_tsc = cur_tsc;
2027                 }
2028
2029         }
2030 }
2031 #endif /* APP_CPU_LOAD */
2032
2033 /*
2034  * Null processing lthread loop
2035  *
2036  * This loop is used to start empty scheduler on lcore.
2037  */
2038 static void
2039 lthread_null(__rte_unused void *args)
2040 {
2041         int lcore_id = rte_lcore_id();
2042
2043         RTE_LOG(INFO, L3FWD, "Starting scheduler on lcore %d.\n", lcore_id);
2044         lthread_exit(NULL);
2045 }
2046
2047 /* main processing loop */
2048 static void
2049 lthread_tx_per_ring(void *dummy)
2050 {
2051         int nb_rx;
2052         uint8_t portid;
2053         struct rte_ring *ring;
2054         struct thread_tx_conf *tx_conf;
2055         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2056         struct lthread_cond *ready;
2057
2058         tx_conf = (struct thread_tx_conf *)dummy;
2059         ring = tx_conf->ring;
2060         ready = *tx_conf->ready;
2061
2062         lthread_set_data((void *)tx_conf);
2063
2064         /*
2065          * Move this lthread to lcore
2066          */
2067         lthread_set_affinity(tx_conf->conf.lcore_id);
2068
2069         RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2070
2071         nb_rx = 0;
2072         rte_atomic16_inc(&tx_counter);
2073         while (1) {
2074
2075                 /*
2076                  * Read packet from ring
2077                  */
2078                 SET_CPU_BUSY(tx_conf, CPU_POLL);
2079                 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2080                                 MAX_PKT_BURST);
2081                 SET_CPU_IDLE(tx_conf, CPU_POLL);
2082
2083                 if (nb_rx > 0) {
2084                         SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2085                         portid = pkts_burst[0]->port;
2086                         process_burst(pkts_burst, nb_rx, portid);
2087                         SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2088                         lthread_yield();
2089                 } else
2090                         lthread_cond_wait(ready, 0);
2091
2092         }
2093 }
2094
2095 /*
2096  * Main tx-lthreads spawner lthread.
2097  *
2098  * This lthread is used to spawn one new lthread per ring from producers.
2099  *
2100  */
2101 static void
2102 lthread_tx(void *args)
2103 {
2104         struct lthread *lt;
2105
2106         unsigned lcore_id;
2107         uint8_t portid;
2108         struct thread_tx_conf *tx_conf;
2109
2110         tx_conf = (struct thread_tx_conf *)args;
2111         lthread_set_data((void *)tx_conf);
2112
2113         /*
2114          * Move this lthread to the selected lcore
2115          */
2116         lthread_set_affinity(tx_conf->conf.lcore_id);
2117
2118         /*
2119          * Spawn tx readers (one per input ring)
2120          */
2121         lthread_create(&lt, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2122                         (void *)tx_conf);
2123
2124         lcore_id = rte_lcore_id();
2125
2126         RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2127
2128         tx_conf->conf.cpu_id = sched_getcpu();
2129         while (1) {
2130
2131                 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2132
2133                 /*
2134                  * TX burst queue drain
2135                  */
2136                 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2137                         if (tx_conf->tx_mbufs[portid].len == 0)
2138                                 continue;
2139                         SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2140                         send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2141                         SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2142                         tx_conf->tx_mbufs[portid].len = 0;
2143                 }
2144
2145         }
2146 }
2147
2148 static void
2149 lthread_rx(void *dummy)
2150 {
2151         int ret;
2152         uint16_t nb_rx;
2153         int i;
2154         uint8_t portid, queueid;
2155         int worker_id;
2156         int len[RTE_MAX_LCORE] = { 0 };
2157         int old_len, new_len;
2158         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2159         struct thread_rx_conf *rx_conf;
2160
2161         rx_conf = (struct thread_rx_conf *)dummy;
2162         lthread_set_data((void *)rx_conf);
2163
2164         /*
2165          * Move this lthread to lcore
2166          */
2167         lthread_set_affinity(rx_conf->conf.lcore_id);
2168
2169         if (rx_conf->n_rx_queue == 0) {
2170                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2171                 return;
2172         }
2173
2174         RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2175
2176         for (i = 0; i < rx_conf->n_rx_queue; i++) {
2177
2178                 portid = rx_conf->rx_queue_list[i].port_id;
2179                 queueid = rx_conf->rx_queue_list[i].queue_id;
2180                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2181                                 rte_lcore_id(), portid, queueid);
2182         }
2183
2184         /*
2185          * Init all condition variables (one per rx thread)
2186          */
2187         for (i = 0; i < rx_conf->n_rx_queue; i++)
2188                 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2189
2190         worker_id = 0;
2191
2192         rx_conf->conf.cpu_id = sched_getcpu();
2193         rte_atomic16_inc(&rx_counter);
2194         while (1) {
2195
2196                 /*
2197                  * Read packet from RX queues
2198                  */
2199                 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2200                         portid = rx_conf->rx_queue_list[i].port_id;
2201                         queueid = rx_conf->rx_queue_list[i].queue_id;
2202
2203                         SET_CPU_BUSY(rx_conf, CPU_POLL);
2204                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2205                                 MAX_PKT_BURST);
2206                         SET_CPU_IDLE(rx_conf, CPU_POLL);
2207
2208                         if (nb_rx != 0) {
2209                                 worker_id = (worker_id + 1) % rx_conf->n_ring;
2210                                 old_len = len[worker_id];
2211
2212                                 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2213                                 ret = rte_ring_sp_enqueue_burst(
2214                                                 rx_conf->ring[worker_id],
2215                                                 (void **) pkts_burst,
2216                                                 nb_rx, NULL);
2217
2218                                 new_len = old_len + ret;
2219
2220                                 if (new_len >= BURST_SIZE) {
2221                                         lthread_cond_signal(rx_conf->ready[worker_id]);
2222                                         new_len = 0;
2223                                 }
2224
2225                                 len[worker_id] = new_len;
2226
2227                                 if (unlikely(ret < nb_rx)) {
2228                                         uint32_t k;
2229
2230                                         for (k = ret; k < nb_rx; k++) {
2231                                                 struct rte_mbuf *m = pkts_burst[k];
2232
2233                                                 rte_pktmbuf_free(m);
2234                                         }
2235                                 }
2236                                 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2237                         }
2238
2239                         lthread_yield();
2240                 }
2241         }
2242 }
2243
2244 /*
2245  * Start scheduler with initial lthread on lcore
2246  *
2247  * This lthread loop spawns all rx and tx lthreads on master lcore
2248  */
2249
2250 static void
2251 lthread_spawner(__rte_unused void *arg) {
2252         struct lthread *lt[MAX_THREAD];
2253         int i;
2254         int n_thread = 0;
2255
2256         printf("Entering lthread_spawner\n");
2257
2258         /*
2259          * Create producers (rx threads) on default lcore
2260          */
2261         for (i = 0; i < n_rx_thread; i++) {
2262                 rx_thread[i].conf.thread_id = i;
2263                 lthread_create(&lt[n_thread], -1, lthread_rx,
2264                                 (void *)&rx_thread[i]);
2265                 n_thread++;
2266         }
2267
2268         /*
2269          * Wait for all producers. Until some producers can be started on the same
2270          * scheduler as this lthread, yielding is required to let them to run and
2271          * prevent deadlock here.
2272          */
2273         while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2274                 lthread_sleep(100000);
2275
2276         /*
2277          * Create consumers (tx threads) on default lcore_id
2278          */
2279         for (i = 0; i < n_tx_thread; i++) {
2280                 tx_thread[i].conf.thread_id = i;
2281                 lthread_create(&lt[n_thread], -1, lthread_tx,
2282                                 (void *)&tx_thread[i]);
2283                 n_thread++;
2284         }
2285
2286         /*
2287          * Wait for all threads finished
2288          */
2289         for (i = 0; i < n_thread; i++)
2290                 lthread_join(lt[i], NULL);
2291
2292 }
2293
2294 /*
2295  * Start master scheduler with initial lthread spawning rx and tx lthreads
2296  * (main_lthread_master).
2297  */
2298 static int
2299 lthread_master_spawner(__rte_unused void *arg) {
2300         struct lthread *lt;
2301         int lcore_id = rte_lcore_id();
2302
2303         RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2304         lthread_create(&lt, -1, lthread_spawner, NULL);
2305         lthread_run();
2306
2307         return 0;
2308 }
2309
2310 /*
2311  * Start scheduler on lcore.
2312  */
2313 static int
2314 sched_spawner(__rte_unused void *arg) {
2315         struct lthread *lt;
2316         int lcore_id = rte_lcore_id();
2317
2318 #if (APP_CPU_LOAD)
2319         if (lcore_id == cpu_load_lcore_id) {
2320                 cpu_load_collector(arg);
2321                 return 0;
2322         }
2323 #endif /* APP_CPU_LOAD */
2324
2325         RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2326         lthread_create(&lt, -1, lthread_null, NULL);
2327         lthread_run();
2328
2329         return 0;
2330 }
2331
2332 /* main processing loop */
2333 static int
2334 pthread_tx(void *dummy)
2335 {
2336         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2337         uint64_t prev_tsc, diff_tsc, cur_tsc;
2338         int nb_rx;
2339         uint8_t portid;
2340         struct thread_tx_conf *tx_conf;
2341
2342         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2343                 US_PER_S * BURST_TX_DRAIN_US;
2344
2345         prev_tsc = 0;
2346
2347         tx_conf = (struct thread_tx_conf *)dummy;
2348
2349         RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2350
2351         tx_conf->conf.cpu_id = sched_getcpu();
2352         rte_atomic16_inc(&tx_counter);
2353         while (1) {
2354
2355                 cur_tsc = rte_rdtsc();
2356
2357                 /*
2358                  * TX burst queue drain
2359                  */
2360                 diff_tsc = cur_tsc - prev_tsc;
2361                 if (unlikely(diff_tsc > drain_tsc)) {
2362
2363                         /*
2364                          * This could be optimized (use queueid instead of
2365                          * portid), but it is not called so often
2366                          */
2367                         SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2368                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2369                                 if (tx_conf->tx_mbufs[portid].len == 0)
2370                                         continue;
2371                                 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2372                                 tx_conf->tx_mbufs[portid].len = 0;
2373                         }
2374                         SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2375
2376                         prev_tsc = cur_tsc;
2377                 }
2378
2379                 /*
2380                  * Read packet from ring
2381                  */
2382                 SET_CPU_BUSY(tx_conf, CPU_POLL);
2383                 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2384                                 (void **)pkts_burst, MAX_PKT_BURST);
2385                 SET_CPU_IDLE(tx_conf, CPU_POLL);
2386
2387                 if (unlikely(nb_rx == 0)) {
2388                         sched_yield();
2389                         continue;
2390                 }
2391
2392                 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2393                 portid = pkts_burst[0]->port;
2394                 process_burst(pkts_burst, nb_rx, portid);
2395                 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2396
2397         }
2398 }
2399
2400 static int
2401 pthread_rx(void *dummy)
2402 {
2403         int i;
2404         int worker_id;
2405         uint32_t n;
2406         uint32_t nb_rx;
2407         unsigned lcore_id;
2408         uint8_t portid, queueid;
2409         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2410
2411         struct thread_rx_conf *rx_conf;
2412
2413         lcore_id = rte_lcore_id();
2414         rx_conf = (struct thread_rx_conf *)dummy;
2415
2416         if (rx_conf->n_rx_queue == 0) {
2417                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2418                 return 0;
2419         }
2420
2421         RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2422
2423         for (i = 0; i < rx_conf->n_rx_queue; i++) {
2424
2425                 portid = rx_conf->rx_queue_list[i].port_id;
2426                 queueid = rx_conf->rx_queue_list[i].queue_id;
2427                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2428                                 lcore_id, portid, queueid);
2429         }
2430
2431         worker_id = 0;
2432         rx_conf->conf.cpu_id = sched_getcpu();
2433         rte_atomic16_inc(&rx_counter);
2434         while (1) {
2435
2436                 /*
2437                  * Read packet from RX queues
2438                  */
2439                 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2440                         portid = rx_conf->rx_queue_list[i].port_id;
2441                         queueid = rx_conf->rx_queue_list[i].queue_id;
2442
2443                         SET_CPU_BUSY(rx_conf, CPU_POLL);
2444                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2445                                 MAX_PKT_BURST);
2446                         SET_CPU_IDLE(rx_conf, CPU_POLL);
2447
2448                         if (nb_rx == 0) {
2449                                 sched_yield();
2450                                 continue;
2451                         }
2452
2453                         SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2454                         worker_id = (worker_id + 1) % rx_conf->n_ring;
2455                         n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2456                                         (void **)pkts_burst, nb_rx, NULL);
2457
2458                         if (unlikely(n != nb_rx)) {
2459                                 uint32_t k;
2460
2461                                 for (k = n; k < nb_rx; k++) {
2462                                         struct rte_mbuf *m = pkts_burst[k];
2463
2464                                         rte_pktmbuf_free(m);
2465                                 }
2466                         }
2467
2468                         SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2469
2470                 }
2471         }
2472 }
2473
2474 /*
2475  * P-Thread spawner.
2476  */
2477 static int
2478 pthread_run(__rte_unused void *arg) {
2479         int lcore_id = rte_lcore_id();
2480         int i;
2481
2482         for (i = 0; i < n_rx_thread; i++)
2483                 if (rx_thread[i].conf.lcore_id == lcore_id) {
2484                         printf("Start rx thread on %d...\n", lcore_id);
2485                         RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2486                         RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2487                         pthread_rx((void *)&rx_thread[i]);
2488                         return 0;
2489                 }
2490
2491         for (i = 0; i < n_tx_thread; i++)
2492                 if (tx_thread[i].conf.lcore_id == lcore_id) {
2493                         printf("Start tx thread on %d...\n", lcore_id);
2494                         RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2495                         RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2496                         pthread_tx((void *)&tx_thread[i]);
2497                         return 0;
2498                 }
2499
2500 #if (APP_CPU_LOAD)
2501         if (lcore_id == cpu_load_lcore_id)
2502                 cpu_load_collector(arg);
2503 #endif /* APP_CPU_LOAD */
2504
2505         return 0;
2506 }
2507
2508 static int
2509 check_lcore_params(void)
2510 {
2511         uint8_t queue, lcore;
2512         uint16_t i;
2513         int socketid;
2514
2515         for (i = 0; i < nb_rx_thread_params; ++i) {
2516                 queue = rx_thread_params[i].queue_id;
2517                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2518                         printf("invalid queue number: %hhu\n", queue);
2519                         return -1;
2520                 }
2521                 lcore = rx_thread_params[i].lcore_id;
2522                 if (!rte_lcore_is_enabled(lcore)) {
2523                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2524                         return -1;
2525                 }
2526                 socketid = rte_lcore_to_socket_id(lcore);
2527                 if ((socketid != 0) && (numa_on == 0))
2528                         printf("warning: lcore %hhu is on socket %d with numa off\n",
2529                                 lcore, socketid);
2530         }
2531         return 0;
2532 }
2533
2534 static int
2535 check_port_config(const unsigned nb_ports)
2536 {
2537         unsigned portid;
2538         uint16_t i;
2539
2540         for (i = 0; i < nb_rx_thread_params; ++i) {
2541                 portid = rx_thread_params[i].port_id;
2542                 if ((enabled_port_mask & (1 << portid)) == 0) {
2543                         printf("port %u is not enabled in port mask\n", portid);
2544                         return -1;
2545                 }
2546                 if (portid >= nb_ports) {
2547                         printf("port %u is not present on the board\n", portid);
2548                         return -1;
2549                 }
2550         }
2551         return 0;
2552 }
2553
2554 static uint8_t
2555 get_port_n_rx_queues(const uint8_t port)
2556 {
2557         int queue = -1;
2558         uint16_t i;
2559
2560         for (i = 0; i < nb_rx_thread_params; ++i)
2561                 if (rx_thread_params[i].port_id == port &&
2562                                 rx_thread_params[i].queue_id > queue)
2563                         queue = rx_thread_params[i].queue_id;
2564
2565         return (uint8_t)(++queue);
2566 }
2567
2568 static int
2569 init_rx_rings(void)
2570 {
2571         unsigned socket_io;
2572         struct thread_rx_conf *rx_conf;
2573         struct thread_tx_conf *tx_conf;
2574         unsigned rx_thread_id, tx_thread_id;
2575         char name[256];
2576         struct rte_ring *ring = NULL;
2577
2578         for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2579
2580                 tx_conf = &tx_thread[tx_thread_id];
2581
2582                 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2583                                 tx_conf->conf.thread_id);
2584
2585                 rx_thread_id = tx_conf->conf.thread_id;
2586                 if (rx_thread_id > n_tx_thread) {
2587                         printf("connection from tx-thread %u to rx-thread %u fails "
2588                                         "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2589                         return -1;
2590                 }
2591
2592                 rx_conf = &rx_thread[rx_thread_id];
2593                 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2594
2595                 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2596                                 socket_io, rx_thread_id, tx_thread_id);
2597
2598                 ring = rte_ring_create(name, 1024 * 4, socket_io,
2599                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
2600
2601                 if (ring == NULL) {
2602                         rte_panic("Cannot create ring to connect rx-thread %u "
2603                                         "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2604                 }
2605
2606                 rx_conf->ring[rx_conf->n_ring] = ring;
2607
2608                 tx_conf->ring = ring;
2609                 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2610
2611                 rx_conf->n_ring++;
2612         }
2613         return 0;
2614 }
2615
2616 static int
2617 init_rx_queues(void)
2618 {
2619         uint16_t i, nb_rx_queue;
2620         uint8_t thread;
2621
2622         n_rx_thread = 0;
2623
2624         for (i = 0; i < nb_rx_thread_params; ++i) {
2625                 thread = rx_thread_params[i].thread_id;
2626                 nb_rx_queue = rx_thread[thread].n_rx_queue;
2627
2628                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2629                         printf("error: too many queues (%u) for thread: %u\n",
2630                                 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2631                         return -1;
2632                 }
2633
2634                 rx_thread[thread].conf.thread_id = thread;
2635                 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2636                 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2637                         rx_thread_params[i].port_id;
2638                 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2639                         rx_thread_params[i].queue_id;
2640                 rx_thread[thread].n_rx_queue++;
2641
2642                 if (thread >= n_rx_thread)
2643                         n_rx_thread = thread + 1;
2644
2645         }
2646         return 0;
2647 }
2648
2649 static int
2650 init_tx_threads(void)
2651 {
2652         int i;
2653
2654         n_tx_thread = 0;
2655         for (i = 0; i < nb_tx_thread_params; ++i) {
2656                 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2657                 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2658                 n_tx_thread++;
2659         }
2660         return 0;
2661 }
2662
2663 /* display usage */
2664 static void
2665 print_usage(const char *prgname)
2666 {
2667         printf("%s [EAL options] -- -p PORTMASK -P"
2668                 "  [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2669                 "  [--tx (lcore,thread)[,(lcore,thread]]"
2670                 "  [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2671                 "  [--parse-ptype]\n\n"
2672                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2673                 "  -P : enable promiscuous mode\n"
2674                 "  --rx (port,queue,lcore,thread): rx queues configuration\n"
2675                 "  --tx (lcore,thread): tx threads configuration\n"
2676                 "  --stat-lcore LCORE: use lcore for stat collector\n"
2677                 "  --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2678                 "  --no-numa: optional, disable numa awareness\n"
2679                 "  --ipv6: optional, specify it if running ipv6 packets\n"
2680                 "  --enable-jumbo: enable jumbo frame"
2681                 " which max packet len is PKTLEN in decimal (64-9600)\n"
2682                 "  --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2683                 "  --no-lthreads: turn off lthread model\n"
2684                 "  --parse-ptype: set to use software to analyze packet type\n\n",
2685                 prgname);
2686 }
2687
2688 static int parse_max_pkt_len(const char *pktlen)
2689 {
2690         char *end = NULL;
2691         unsigned long len;
2692
2693         /* parse decimal string */
2694         len = strtoul(pktlen, &end, 10);
2695         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2696                 return -1;
2697
2698         if (len == 0)
2699                 return -1;
2700
2701         return len;
2702 }
2703
2704 static int
2705 parse_portmask(const char *portmask)
2706 {
2707         char *end = NULL;
2708         unsigned long pm;
2709
2710         /* parse hexadecimal string */
2711         pm = strtoul(portmask, &end, 16);
2712         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2713                 return -1;
2714
2715         if (pm == 0)
2716                 return -1;
2717
2718         return pm;
2719 }
2720
2721 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2722 static int
2723 parse_hash_entry_number(const char *hash_entry_num)
2724 {
2725         char *end = NULL;
2726         unsigned long hash_en;
2727
2728         /* parse hexadecimal string */
2729         hash_en = strtoul(hash_entry_num, &end, 16);
2730         if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2731                 return -1;
2732
2733         if (hash_en == 0)
2734                 return -1;
2735
2736         return hash_en;
2737 }
2738 #endif
2739
2740 static int
2741 parse_rx_config(const char *q_arg)
2742 {
2743         char s[256];
2744         const char *p, *p0 = q_arg;
2745         char *end;
2746         enum fieldnames {
2747                 FLD_PORT = 0,
2748                 FLD_QUEUE,
2749                 FLD_LCORE,
2750                 FLD_THREAD,
2751                 _NUM_FLD
2752         };
2753         unsigned long int_fld[_NUM_FLD];
2754         char *str_fld[_NUM_FLD];
2755         int i;
2756         unsigned size;
2757
2758         nb_rx_thread_params = 0;
2759
2760         while ((p = strchr(p0, '(')) != NULL) {
2761                 ++p;
2762                 p0 = strchr(p, ')');
2763                 if (p0 == NULL)
2764                         return -1;
2765
2766                 size = p0 - p;
2767                 if (size >= sizeof(s))
2768                         return -1;
2769
2770                 snprintf(s, sizeof(s), "%.*s", size, p);
2771                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2772                         return -1;
2773                 for (i = 0; i < _NUM_FLD; i++) {
2774                         errno = 0;
2775                         int_fld[i] = strtoul(str_fld[i], &end, 0);
2776                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2777                                 return -1;
2778                 }
2779                 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2780                         printf("exceeded max number of rx params: %hu\n",
2781                                         nb_rx_thread_params);
2782                         return -1;
2783                 }
2784                 rx_thread_params_array[nb_rx_thread_params].port_id =
2785                                 (uint8_t)int_fld[FLD_PORT];
2786                 rx_thread_params_array[nb_rx_thread_params].queue_id =
2787                                 (uint8_t)int_fld[FLD_QUEUE];
2788                 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2789                                 (uint8_t)int_fld[FLD_LCORE];
2790                 rx_thread_params_array[nb_rx_thread_params].thread_id =
2791                                 (uint8_t)int_fld[FLD_THREAD];
2792                 ++nb_rx_thread_params;
2793         }
2794         rx_thread_params = rx_thread_params_array;
2795         return 0;
2796 }
2797
2798 static int
2799 parse_tx_config(const char *q_arg)
2800 {
2801         char s[256];
2802         const char *p, *p0 = q_arg;
2803         char *end;
2804         enum fieldnames {
2805                 FLD_LCORE = 0,
2806                 FLD_THREAD,
2807                 _NUM_FLD
2808         };
2809         unsigned long int_fld[_NUM_FLD];
2810         char *str_fld[_NUM_FLD];
2811         int i;
2812         unsigned size;
2813
2814         nb_tx_thread_params = 0;
2815
2816         while ((p = strchr(p0, '(')) != NULL) {
2817                 ++p;
2818                 p0 = strchr(p, ')');
2819                 if (p0 == NULL)
2820                         return -1;
2821
2822                 size = p0 - p;
2823                 if (size >= sizeof(s))
2824                         return -1;
2825
2826                 snprintf(s, sizeof(s), "%.*s", size, p);
2827                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2828                         return -1;
2829                 for (i = 0; i < _NUM_FLD; i++) {
2830                         errno = 0;
2831                         int_fld[i] = strtoul(str_fld[i], &end, 0);
2832                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2833                                 return -1;
2834                 }
2835                 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2836                         printf("exceeded max number of tx params: %hu\n",
2837                                 nb_tx_thread_params);
2838                         return -1;
2839                 }
2840                 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2841                                 (uint8_t)int_fld[FLD_LCORE];
2842                 tx_thread_params_array[nb_tx_thread_params].thread_id =
2843                                 (uint8_t)int_fld[FLD_THREAD];
2844                 ++nb_tx_thread_params;
2845         }
2846         tx_thread_params = tx_thread_params_array;
2847
2848         return 0;
2849 }
2850
2851 #if (APP_CPU_LOAD > 0)
2852 static int
2853 parse_stat_lcore(const char *stat_lcore)
2854 {
2855         char *end = NULL;
2856         unsigned long lcore_id;
2857
2858         lcore_id = strtoul(stat_lcore, &end, 10);
2859         if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2860                 return -1;
2861
2862         return lcore_id;
2863 }
2864 #endif
2865
2866 static void
2867 parse_eth_dest(const char *optarg)
2868 {
2869         uint8_t portid;
2870         char *port_end;
2871         uint8_t c, *dest, peer_addr[6];
2872
2873         errno = 0;
2874         portid = strtoul(optarg, &port_end, 10);
2875         if (errno != 0 || port_end == optarg || *port_end++ != ',')
2876                 rte_exit(EXIT_FAILURE,
2877                 "Invalid eth-dest: %s", optarg);
2878         if (portid >= RTE_MAX_ETHPORTS)
2879                 rte_exit(EXIT_FAILURE,
2880                 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2881                 portid, RTE_MAX_ETHPORTS);
2882
2883         if (cmdline_parse_etheraddr(NULL, port_end,
2884                 &peer_addr, sizeof(peer_addr)) < 0)
2885                 rte_exit(EXIT_FAILURE,
2886                 "Invalid ethernet address: %s\n",
2887                 port_end);
2888         dest = (uint8_t *)&dest_eth_addr[portid];
2889         for (c = 0; c < 6; c++)
2890                 dest[c] = peer_addr[c];
2891         *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2892 }
2893
2894 #define CMD_LINE_OPT_RX_CONFIG "rx"
2895 #define CMD_LINE_OPT_TX_CONFIG "tx"
2896 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2897 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2898 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2899 #define CMD_LINE_OPT_IPV6 "ipv6"
2900 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2901 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2902 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2903 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
2904
2905 /* Parse the argument given in the command line of the application */
2906 static int
2907 parse_args(int argc, char **argv)
2908 {
2909         int opt, ret;
2910         char **argvopt;
2911         int option_index;
2912         char *prgname = argv[0];
2913         static struct option lgopts[] = {
2914                 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2915                 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2916                 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2917                 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2918                 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2919                 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2920                 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2921                 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2922                 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2923                 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, 0},
2924                 {NULL, 0, 0, 0}
2925         };
2926
2927         argvopt = argv;
2928
2929         while ((opt = getopt_long(argc, argvopt, "p:P",
2930                                 lgopts, &option_index)) != EOF) {
2931
2932                 switch (opt) {
2933                 /* portmask */
2934                 case 'p':
2935                         enabled_port_mask = parse_portmask(optarg);
2936                         if (enabled_port_mask == 0) {
2937                                 printf("invalid portmask\n");
2938                                 print_usage(prgname);
2939                                 return -1;
2940                         }
2941                         break;
2942                 case 'P':
2943                         printf("Promiscuous mode selected\n");
2944                         promiscuous_on = 1;
2945                         break;
2946
2947                 /* long options */
2948                 case 0:
2949                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2950                                 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2951                                 ret = parse_rx_config(optarg);
2952                                 if (ret) {
2953                                         printf("invalid rx-config\n");
2954                                         print_usage(prgname);
2955                                         return -1;
2956                                 }
2957                         }
2958
2959                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2960                                 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2961                                 ret = parse_tx_config(optarg);
2962                                 if (ret) {
2963                                         printf("invalid tx-config\n");
2964                                         print_usage(prgname);
2965                                         return -1;
2966                                 }
2967                         }
2968
2969 #if (APP_CPU_LOAD > 0)
2970                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2971                                         sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2972                                 cpu_load_lcore_id = parse_stat_lcore(optarg);
2973                         }
2974 #endif
2975
2976                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2977                                 sizeof(CMD_LINE_OPT_ETH_DEST)))
2978                                         parse_eth_dest(optarg);
2979
2980                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2981                                 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2982                                 printf("numa is disabled\n");
2983                                 numa_on = 0;
2984                         }
2985
2986 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2987                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2988                                 sizeof(CMD_LINE_OPT_IPV6))) {
2989                                 printf("ipv6 is specified\n");
2990                                 ipv6 = 1;
2991                         }
2992 #endif
2993
2994                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2995                                         sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2996                                 printf("l-threads model is disabled\n");
2997                                 lthreads_on = 0;
2998                         }
2999
3000                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_PARSE_PTYPE,
3001                                         sizeof(CMD_LINE_OPT_PARSE_PTYPE))) {
3002                                 printf("software packet type parsing enabled\n");
3003                                 parse_ptype_on = 1;
3004                         }
3005
3006                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
3007                                 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
3008                                 struct option lenopts = {"max-pkt-len", required_argument, 0,
3009                                                 0};
3010
3011                                 printf("jumbo frame is enabled - disabling simple TX path\n");
3012                                 port_conf.rxmode.jumbo_frame = 1;
3013
3014                                 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
3015                                 if (0 == getopt_long(argc, argvopt, "", &lenopts,
3016                                                 &option_index)) {
3017
3018                                         ret = parse_max_pkt_len(optarg);
3019                                         if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
3020                                                 printf("invalid packet length\n");
3021                                                 print_usage(prgname);
3022                                                 return -1;
3023                                         }
3024                                         port_conf.rxmode.max_rx_pkt_len = ret;
3025                                 }
3026                                 printf("set jumbo frame max packet length to %u\n",
3027                                                 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
3028                         }
3029 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3030                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
3031                                 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
3032                                 ret = parse_hash_entry_number(optarg);
3033                                 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
3034                                         hash_entry_number = ret;
3035                                 } else {
3036                                         printf("invalid hash entry number\n");
3037                                         print_usage(prgname);
3038                                         return -1;
3039                                 }
3040                         }
3041 #endif
3042                         break;
3043
3044                 default:
3045                         print_usage(prgname);
3046                         return -1;
3047                 }
3048         }
3049
3050         if (optind >= 0)
3051                 argv[optind-1] = prgname;
3052
3053         ret = optind-1;
3054         optind = 1; /* reset getopt lib */
3055         return ret;
3056 }
3057
3058 static void
3059 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
3060 {
3061         char buf[ETHER_ADDR_FMT_SIZE];
3062
3063         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3064         printf("%s%s", name, buf);
3065 }
3066
3067 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3068
3069 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3070                 union ipv4_5tuple_host *key2)
3071 {
3072         key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3073         key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3074         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3075         key2->port_src = rte_cpu_to_be_16(key1->port_src);
3076         key2->proto = key1->proto;
3077         key2->pad0 = 0;
3078         key2->pad1 = 0;
3079 }
3080
3081 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3082                 union ipv6_5tuple_host *key2)
3083 {
3084         uint32_t i;
3085
3086         for (i = 0; i < 16; i++) {
3087                 key2->ip_dst[i] = key1->ip_dst[i];
3088                 key2->ip_src[i] = key1->ip_src[i];
3089         }
3090         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3091         key2->port_src = rte_cpu_to_be_16(key1->port_src);
3092         key2->proto = key1->proto;
3093         key2->pad0 = 0;
3094         key2->pad1 = 0;
3095         key2->reserve = 0;
3096 }
3097
3098 #define BYTE_VALUE_MAX 256
3099 #define ALL_32_BITS 0xffffffff
3100 #define BIT_8_TO_15 0x0000ff00
3101 static inline void
3102 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3103 {
3104         uint32_t i;
3105         int32_t ret;
3106         uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3107
3108         mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3109         for (i = 0; i < array_len; i++) {
3110                 struct ipv4_l3fwd_route  entry;
3111                 union ipv4_5tuple_host newkey;
3112
3113                 entry = ipv4_l3fwd_route_array[i];
3114                 convert_ipv4_5tuple(&entry.key, &newkey);
3115                 ret = rte_hash_add_key(h, (void *)&newkey);
3116                 if (ret < 0) {
3117                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3118                                 " to the l3fwd hash.\n", i);
3119                 }
3120                 ipv4_l3fwd_out_if[ret] = entry.if_out;
3121         }
3122         printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3123 }
3124
3125 #define BIT_16_TO_23 0x00ff0000
3126 static inline void
3127 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3128 {
3129         uint32_t i;
3130         int32_t ret;
3131         uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3132
3133         mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3134         mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3135         for (i = 0; i < array_len; i++) {
3136                 struct ipv6_l3fwd_route entry;
3137                 union ipv6_5tuple_host newkey;
3138
3139                 entry = ipv6_l3fwd_route_array[i];
3140                 convert_ipv6_5tuple(&entry.key, &newkey);
3141                 ret = rte_hash_add_key(h, (void *)&newkey);
3142                 if (ret < 0) {
3143                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3144                                 " to the l3fwd hash.\n", i);
3145                 }
3146                 ipv6_l3fwd_out_if[ret] = entry.if_out;
3147         }
3148         printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3149 }
3150
3151 #define NUMBER_PORT_USED 4
3152 static inline void
3153 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3154                 unsigned int nr_flow)
3155 {
3156         unsigned i;
3157
3158         mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3159
3160         for (i = 0; i < nr_flow; i++) {
3161                 struct ipv4_l3fwd_route entry;
3162                 union ipv4_5tuple_host newkey;
3163                 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3164                 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3165                                 BYTE_VALUE_MAX);
3166                 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3167                                 BYTE_VALUE_MAX));
3168                 /* Create the ipv4 exact match flow */
3169                 memset(&entry, 0, sizeof(entry));
3170                 switch (i & (NUMBER_PORT_USED - 1)) {
3171                 case 0:
3172                         entry = ipv4_l3fwd_route_array[0];
3173                         entry.key.ip_dst = IPv4(101, c, b, a);
3174                         break;
3175                 case 1:
3176                         entry = ipv4_l3fwd_route_array[1];
3177                         entry.key.ip_dst = IPv4(201, c, b, a);
3178                         break;
3179                 case 2:
3180                         entry = ipv4_l3fwd_route_array[2];
3181                         entry.key.ip_dst = IPv4(111, c, b, a);
3182                         break;
3183                 case 3:
3184                         entry = ipv4_l3fwd_route_array[3];
3185                         entry.key.ip_dst = IPv4(211, c, b, a);
3186                         break;
3187                 };
3188                 convert_ipv4_5tuple(&entry.key, &newkey);
3189                 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3190
3191                 if (ret < 0)
3192                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3193
3194                 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3195
3196         }
3197         printf("Hash: Adding 0x%x keys\n", nr_flow);
3198 }
3199
3200 static inline void
3201 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3202                 unsigned int nr_flow)
3203 {
3204         unsigned i;
3205
3206         mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3207         mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3208         for (i = 0; i < nr_flow; i++) {
3209                 struct ipv6_l3fwd_route entry;
3210                 union ipv6_5tuple_host newkey;
3211
3212                 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3213                 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3214                                 BYTE_VALUE_MAX);
3215                 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3216                                 BYTE_VALUE_MAX));
3217
3218                 /* Create the ipv6 exact match flow */
3219                 memset(&entry, 0, sizeof(entry));
3220                 switch (i & (NUMBER_PORT_USED - 1)) {
3221                 case 0:
3222                         entry = ipv6_l3fwd_route_array[0];
3223                         break;
3224                 case 1:
3225                         entry = ipv6_l3fwd_route_array[1];
3226                         break;
3227                 case 2:
3228                         entry = ipv6_l3fwd_route_array[2];
3229                         break;
3230                 case 3:
3231                         entry = ipv6_l3fwd_route_array[3];
3232                         break;
3233                 };
3234                 entry.key.ip_dst[13] = c;
3235                 entry.key.ip_dst[14] = b;
3236                 entry.key.ip_dst[15] = a;
3237                 convert_ipv6_5tuple(&entry.key, &newkey);
3238                 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3239
3240                 if (ret < 0)
3241                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3242
3243                 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3244
3245         }
3246         printf("Hash: Adding 0x%x keys\n", nr_flow);
3247 }
3248
3249 static void
3250 setup_hash(int socketid)
3251 {
3252         struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3253                 .name = NULL,
3254                 .entries = L3FWD_HASH_ENTRIES,
3255                 .key_len = sizeof(union ipv4_5tuple_host),
3256                 .hash_func = ipv4_hash_crc,
3257                 .hash_func_init_val = 0,
3258         };
3259
3260         struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3261                 .name = NULL,
3262                 .entries = L3FWD_HASH_ENTRIES,
3263                 .key_len = sizeof(union ipv6_5tuple_host),
3264                 .hash_func = ipv6_hash_crc,
3265                 .hash_func_init_val = 0,
3266         };
3267
3268         char s[64];
3269
3270         /* create ipv4 hash */
3271         snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3272         ipv4_l3fwd_hash_params.name = s;
3273         ipv4_l3fwd_hash_params.socket_id = socketid;
3274         ipv4_l3fwd_lookup_struct[socketid] =
3275                         rte_hash_create(&ipv4_l3fwd_hash_params);
3276         if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3277                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3278                                 "socket %d\n", socketid);
3279
3280         /* create ipv6 hash */
3281         snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3282         ipv6_l3fwd_hash_params.name = s;
3283         ipv6_l3fwd_hash_params.socket_id = socketid;
3284         ipv6_l3fwd_lookup_struct[socketid] =
3285                         rte_hash_create(&ipv6_l3fwd_hash_params);
3286         if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3287                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3288                                 "socket %d\n", socketid);
3289
3290         if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3291                 /* For testing hash matching with a large number of flows we
3292                  * generate millions of IP 5-tuples with an incremented dst
3293                  * address to initialize the hash table. */
3294                 if (ipv6 == 0) {
3295                         /* populate the ipv4 hash */
3296                         populate_ipv4_many_flow_into_table(
3297                                 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3298                 } else {
3299                         /* populate the ipv6 hash */
3300                         populate_ipv6_many_flow_into_table(
3301                                 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3302                 }
3303         } else {
3304                 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3305                  * the hash table */
3306                 if (ipv6 == 0) {
3307                         /* populate the ipv4 hash */
3308                         populate_ipv4_few_flow_into_table(
3309                                         ipv4_l3fwd_lookup_struct[socketid]);
3310                 } else {
3311                         /* populate the ipv6 hash */
3312                         populate_ipv6_few_flow_into_table(
3313                                         ipv6_l3fwd_lookup_struct[socketid]);
3314                 }
3315         }
3316 }
3317 #endif
3318
3319 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3320 static void
3321 setup_lpm(int socketid)
3322 {
3323         struct rte_lpm6_config config;
3324         struct rte_lpm_config lpm_ipv4_config;
3325         unsigned i;
3326         int ret;
3327         char s[64];
3328
3329         /* create the LPM table */
3330         snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3331         lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3332         lpm_ipv4_config.number_tbl8s = 256;
3333         lpm_ipv4_config.flags = 0;
3334         ipv4_l3fwd_lookup_struct[socketid] =
3335                         rte_lpm_create(s, socketid, &lpm_ipv4_config);
3336         if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3337                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3338                                 " on socket %d\n", socketid);
3339
3340         /* populate the LPM table */
3341         for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3342
3343                 /* skip unused ports */
3344                 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3345                                 enabled_port_mask) == 0)
3346                         continue;
3347
3348                 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3349                         ipv4_l3fwd_route_array[i].ip,
3350                         ipv4_l3fwd_route_array[i].depth,
3351                         ipv4_l3fwd_route_array[i].if_out);
3352
3353                 if (ret < 0) {
3354                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3355                                 "l3fwd LPM table on socket %d\n",
3356                                 i, socketid);
3357                 }
3358
3359                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3360                         (unsigned)ipv4_l3fwd_route_array[i].ip,
3361                         ipv4_l3fwd_route_array[i].depth,
3362                         ipv4_l3fwd_route_array[i].if_out);
3363         }
3364
3365         /* create the LPM6 table */
3366         snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3367
3368         config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3369         config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3370         config.flags = 0;
3371         ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3372                                 &config);
3373         if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3374                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3375                                 " on socket %d\n", socketid);
3376
3377         /* populate the LPM table */
3378         for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3379
3380                 /* skip unused ports */
3381                 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3382                                 enabled_port_mask) == 0)
3383                         continue;
3384
3385                 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3386                         ipv6_l3fwd_route_array[i].ip,
3387                         ipv6_l3fwd_route_array[i].depth,
3388                         ipv6_l3fwd_route_array[i].if_out);
3389
3390                 if (ret < 0) {
3391                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3392                                 "l3fwd LPM table on socket %d\n",
3393                                 i, socketid);
3394                 }
3395
3396                 printf("LPM: Adding route %s / %d (%d)\n",
3397                         "IPV6",
3398                         ipv6_l3fwd_route_array[i].depth,
3399                         ipv6_l3fwd_route_array[i].if_out);
3400         }
3401 }
3402 #endif
3403
3404 static int
3405 init_mem(unsigned nb_mbuf)
3406 {
3407         struct lcore_conf *qconf;
3408         int socketid;
3409         unsigned lcore_id;
3410         char s[64];
3411
3412         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3413                 if (rte_lcore_is_enabled(lcore_id) == 0)
3414                         continue;
3415
3416                 if (numa_on)
3417                         socketid = rte_lcore_to_socket_id(lcore_id);
3418                 else
3419                         socketid = 0;
3420
3421                 if (socketid >= NB_SOCKETS) {
3422                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3423                                 socketid, lcore_id, NB_SOCKETS);
3424                 }
3425                 if (pktmbuf_pool[socketid] == NULL) {
3426                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3427                         pktmbuf_pool[socketid] =
3428                                 rte_pktmbuf_pool_create(s, nb_mbuf,
3429                                         MEMPOOL_CACHE_SIZE, 0,
3430                                         RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3431                         if (pktmbuf_pool[socketid] == NULL)
3432                                 rte_exit(EXIT_FAILURE,
3433                                                 "Cannot init mbuf pool on socket %d\n", socketid);
3434                         else
3435                                 printf("Allocated mbuf pool on socket %d\n", socketid);
3436
3437 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3438                         setup_lpm(socketid);
3439 #else
3440                         setup_hash(socketid);
3441 #endif
3442                 }
3443                 qconf = &lcore_conf[lcore_id];
3444                 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3445                 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3446         }
3447         return 0;
3448 }
3449
3450 /* Check the link status of all ports in up to 9s, and print them finally */
3451 static void
3452 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3453 {
3454 #define CHECK_INTERVAL 100 /* 100ms */
3455 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3456         uint8_t portid, count, all_ports_up, print_flag = 0;
3457         struct rte_eth_link link;
3458
3459         printf("\nChecking link status");
3460         fflush(stdout);
3461         for (count = 0; count <= MAX_CHECK_TIME; count++) {
3462                 all_ports_up = 1;
3463                 for (portid = 0; portid < port_num; portid++) {
3464                         if ((port_mask & (1 << portid)) == 0)
3465                                 continue;
3466                         memset(&link, 0, sizeof(link));
3467                         rte_eth_link_get_nowait(portid, &link);
3468                         /* print link status if flag set */
3469                         if (print_flag == 1) {
3470                                 if (link.link_status)
3471                                         printf("Port %d Link Up - speed %u "
3472                                                 "Mbps - %s\n", (uint8_t)portid,
3473                                                 (unsigned)link.link_speed,
3474                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3475                                         ("full-duplex") : ("half-duplex\n"));
3476                                 else
3477                                         printf("Port %d Link Down\n",
3478                                                 (uint8_t)portid);
3479                                 continue;
3480                         }
3481                         /* clear all_ports_up flag if any link down */
3482                         if (link.link_status == ETH_LINK_DOWN) {
3483                                 all_ports_up = 0;
3484                                 break;
3485                         }
3486                 }
3487                 /* after finally printing all link status, get out */
3488                 if (print_flag == 1)
3489                         break;
3490
3491                 if (all_ports_up == 0) {
3492                         printf(".");
3493                         fflush(stdout);
3494                         rte_delay_ms(CHECK_INTERVAL);
3495                 }
3496
3497                 /* set the print_flag if all ports up or timeout */
3498                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3499                         print_flag = 1;
3500                         printf("done\n");
3501                 }
3502         }
3503 }
3504
3505 int
3506 main(int argc, char **argv)
3507 {
3508         struct rte_eth_dev_info dev_info;
3509         struct rte_eth_txconf *txconf;
3510         int ret;
3511         int i;
3512         unsigned nb_ports;
3513         uint16_t queueid;
3514         unsigned lcore_id;
3515         uint32_t n_tx_queue, nb_lcores;
3516         uint8_t portid, nb_rx_queue, queue, socketid;
3517
3518         /* init EAL */
3519         ret = rte_eal_init(argc, argv);
3520         if (ret < 0)
3521                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3522         argc -= ret;
3523         argv += ret;
3524
3525         /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3526         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3527                 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3528                                 ((uint64_t)portid << 40);
3529                 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3530         }
3531
3532         /* parse application arguments (after the EAL ones) */
3533         ret = parse_args(argc, argv);
3534         if (ret < 0)
3535                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3536
3537         if (check_lcore_params() < 0)
3538                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3539
3540         printf("Initializing rx-queues...\n");
3541         ret = init_rx_queues();
3542         if (ret < 0)
3543                 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3544
3545         printf("Initializing tx-threads...\n");
3546         ret = init_tx_threads();
3547         if (ret < 0)
3548                 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3549
3550         printf("Initializing rings...\n");
3551         ret = init_rx_rings();
3552         if (ret < 0)
3553                 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3554
3555         nb_ports = rte_eth_dev_count();
3556
3557         if (check_port_config(nb_ports) < 0)
3558                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3559
3560         nb_lcores = rte_lcore_count();
3561
3562         /* initialize all ports */
3563         for (portid = 0; portid < nb_ports; portid++) {
3564                 /* skip ports that are not enabled */
3565                 if ((enabled_port_mask & (1 << portid)) == 0) {
3566                         printf("\nSkipping disabled port %d\n", portid);
3567                         continue;
3568                 }
3569
3570                 /* init port */
3571                 printf("Initializing port %d ... ", portid);
3572                 fflush(stdout);
3573
3574                 nb_rx_queue = get_port_n_rx_queues(portid);
3575                 n_tx_queue = nb_lcores;
3576                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3577                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3578                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3579                         nb_rx_queue, (unsigned)n_tx_queue);
3580                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3581                                         (uint16_t)n_tx_queue, &port_conf);
3582                 if (ret < 0)
3583                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3584                                 ret, portid);
3585
3586                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3587                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3588                 printf(", ");
3589                 print_ethaddr("Destination:",
3590                         (const struct ether_addr *)&dest_eth_addr[portid]);
3591                 printf(", ");
3592
3593                 /*
3594                  * prepare src MACs for each port.
3595                  */
3596                 ether_addr_copy(&ports_eth_addr[portid],
3597                         (struct ether_addr *)(val_eth + portid) + 1);
3598
3599                 /* init memory */
3600                 ret = init_mem(NB_MBUF);
3601                 if (ret < 0)
3602                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
3603
3604                 /* init one TX queue per couple (lcore,port) */
3605                 queueid = 0;
3606                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3607                         if (rte_lcore_is_enabled(lcore_id) == 0)
3608                                 continue;
3609
3610                         if (numa_on)
3611                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3612                         else
3613                                 socketid = 0;
3614
3615                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3616                         fflush(stdout);
3617
3618                         rte_eth_dev_info_get(portid, &dev_info);
3619                         txconf = &dev_info.default_txconf;
3620                         if (port_conf.rxmode.jumbo_frame)
3621                                 txconf->txq_flags = 0;
3622                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3623                                                      socketid, txconf);
3624                         if (ret < 0)
3625                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3626                                         "port=%d\n", ret, portid);
3627
3628                         tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3629                         queueid++;
3630                 }
3631                 printf("\n");
3632         }
3633
3634         for (i = 0; i < n_rx_thread; i++) {
3635                 lcore_id = rx_thread[i].conf.lcore_id;
3636
3637                 if (rte_lcore_is_enabled(lcore_id) == 0) {
3638                         rte_exit(EXIT_FAILURE,
3639                                         "Cannot start Rx thread on lcore %u: lcore disabled\n",
3640                                         lcore_id
3641                                 );
3642                 }
3643
3644                 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3645                                 i, lcore_id);
3646                 fflush(stdout);
3647
3648                 /* init RX queues */
3649                 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3650                         portid = rx_thread[i].rx_queue_list[queue].port_id;
3651                         queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3652
3653                         if (numa_on)
3654                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3655                         else
3656                                 socketid = 0;
3657
3658                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3659                         fflush(stdout);
3660
3661                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3662                                         socketid,
3663                                         NULL,
3664                                         pktmbuf_pool[socketid]);
3665                         if (ret < 0)
3666                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3667                                                 "port=%d\n", ret, portid);
3668                 }
3669         }
3670
3671         printf("\n");
3672
3673         /* start ports */
3674         for (portid = 0; portid < nb_ports; portid++) {
3675                 if ((enabled_port_mask & (1 << portid)) == 0)
3676                         continue;
3677
3678                 /* Start device */
3679                 ret = rte_eth_dev_start(portid);
3680                 if (ret < 0)
3681                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3682                                 ret, portid);
3683
3684                 /*
3685                  * If enabled, put device in promiscuous mode.
3686                  * This allows IO forwarding mode to forward packets
3687                  * to itself through 2 cross-connected  ports of the
3688                  * target machine.
3689                  */
3690                 if (promiscuous_on)
3691                         rte_eth_promiscuous_enable(portid);
3692         }
3693
3694         for (i = 0; i < n_rx_thread; i++) {
3695                 lcore_id = rx_thread[i].conf.lcore_id;
3696                 if (rte_lcore_is_enabled(lcore_id) == 0)
3697                         continue;
3698
3699                 /* check if hw packet type is supported */
3700                 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3701                         portid = rx_thread[i].rx_queue_list[queue].port_id;
3702                         queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3703
3704                         if (parse_ptype_on) {
3705                                 if (!rte_eth_add_rx_callback(portid, queueid,
3706                                                 cb_parse_ptype, NULL))
3707                                         rte_exit(EXIT_FAILURE,
3708                                                 "Failed to add rx callback: "
3709                                                 "port=%d\n", portid);
3710                         } else if (!check_ptype(portid))
3711                                 rte_exit(EXIT_FAILURE,
3712                                         "Port %d cannot parse packet type.\n\n"
3713                                         "Please add --parse-ptype to use sw "
3714                                         "packet type analyzer.\n\n",
3715                                         portid);
3716                 }
3717         }
3718
3719         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3720
3721         if (lthreads_on) {
3722                 printf("Starting L-Threading Model\n");
3723
3724 #if (APP_CPU_LOAD > 0)
3725                 if (cpu_load_lcore_id > 0)
3726                         /* Use one lcore for cpu load collector */
3727                         nb_lcores--;
3728 #endif
3729
3730                 lthread_num_schedulers_set(nb_lcores);
3731                 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3732                 lthread_master_spawner(NULL);
3733
3734         } else {
3735                 printf("Starting P-Threading Model\n");
3736                 /* launch per-lcore init on every lcore */
3737                 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3738                 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3739                         if (rte_eal_wait_lcore(lcore_id) < 0)
3740                                 return -1;
3741                 }
3742         }
3743
3744         return 0;
3745 }