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