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