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