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