dd403ca86a96fa041af1a6aed126819a0c473529
[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 }
1992
1993 /* main processing loop */
1994 static void
1995 lthread_tx_per_ring(void *dummy)
1996 {
1997         int nb_rx;
1998         uint8_t portid;
1999         struct rte_ring *ring;
2000         struct thread_tx_conf *tx_conf;
2001         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2002         struct lthread_cond *ready;
2003
2004         tx_conf = (struct thread_tx_conf *)dummy;
2005         ring = tx_conf->ring;
2006         ready = *tx_conf->ready;
2007
2008         lthread_set_data((void *)tx_conf);
2009
2010         /*
2011          * Move this lthread to lcore
2012          */
2013         lthread_set_affinity(tx_conf->conf.lcore_id);
2014
2015         RTE_LOG(INFO, L3FWD, "entering main tx loop on lcore %u\n", rte_lcore_id());
2016
2017         nb_rx = 0;
2018         rte_atomic16_inc(&tx_counter);
2019         while (1) {
2020
2021                 /*
2022                  * Read packet from ring
2023                  */
2024                 SET_CPU_BUSY(tx_conf, CPU_POLL);
2025                 nb_rx = rte_ring_sc_dequeue_burst(ring, (void **)pkts_burst,
2026                                 MAX_PKT_BURST);
2027                 SET_CPU_IDLE(tx_conf, CPU_POLL);
2028
2029                 if (nb_rx > 0) {
2030                         SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2031                         portid = pkts_burst[0]->port;
2032                         process_burst(pkts_burst, nb_rx, portid);
2033                         SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2034                         lthread_yield();
2035                 } else
2036                         lthread_cond_wait(ready, 0);
2037
2038         }
2039 }
2040
2041 /*
2042  * Main tx-lthreads spawner lthread.
2043  *
2044  * This lthread is used to spawn one new lthread per ring from producers.
2045  *
2046  */
2047 static void
2048 lthread_tx(void *args)
2049 {
2050         struct lthread *lt;
2051
2052         unsigned lcore_id;
2053         uint8_t portid;
2054         struct thread_tx_conf *tx_conf;
2055
2056         tx_conf = (struct thread_tx_conf *)args;
2057         lthread_set_data((void *)tx_conf);
2058
2059         /*
2060          * Move this lthread to the selected lcore
2061          */
2062         lthread_set_affinity(tx_conf->conf.lcore_id);
2063
2064         /*
2065          * Spawn tx readers (one per input ring)
2066          */
2067         lthread_create(&lt, tx_conf->conf.lcore_id, lthread_tx_per_ring,
2068                         (void *)tx_conf);
2069
2070         lcore_id = rte_lcore_id();
2071
2072         RTE_LOG(INFO, L3FWD, "Entering Tx main loop on lcore %u\n", lcore_id);
2073
2074         tx_conf->conf.cpu_id = sched_getcpu();
2075         while (1) {
2076
2077                 lthread_sleep(BURST_TX_DRAIN_US * 1000);
2078
2079                 /*
2080                  * TX burst queue drain
2081                  */
2082                 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2083                         if (tx_conf->tx_mbufs[portid].len == 0)
2084                                 continue;
2085                         SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2086                         send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2087                         SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2088                         tx_conf->tx_mbufs[portid].len = 0;
2089                 }
2090
2091         }
2092 }
2093
2094 static void
2095 lthread_rx(void *dummy)
2096 {
2097         int ret;
2098         uint16_t nb_rx;
2099         int i;
2100         uint8_t portid, queueid;
2101         int worker_id;
2102         int len[RTE_MAX_LCORE] = { 0 };
2103         int old_len, new_len;
2104         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2105         struct thread_rx_conf *rx_conf;
2106
2107         rx_conf = (struct thread_rx_conf *)dummy;
2108         lthread_set_data((void *)rx_conf);
2109
2110         /*
2111          * Move this lthread to lcore
2112          */
2113         lthread_set_affinity(rx_conf->conf.lcore_id);
2114
2115         if (rx_conf->n_rx_queue == 0) {
2116                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", rte_lcore_id());
2117                 return;
2118         }
2119
2120         RTE_LOG(INFO, L3FWD, "Entering main Rx loop on lcore %u\n", rte_lcore_id());
2121
2122         for (i = 0; i < rx_conf->n_rx_queue; i++) {
2123
2124                 portid = rx_conf->rx_queue_list[i].port_id;
2125                 queueid = rx_conf->rx_queue_list[i].queue_id;
2126                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2127                                 rte_lcore_id(), portid, queueid);
2128         }
2129
2130         /*
2131          * Init all condition variables (one per rx thread)
2132          */
2133         for (i = 0; i < rx_conf->n_rx_queue; i++)
2134                 lthread_cond_init(NULL, &rx_conf->ready[i], NULL);
2135
2136         worker_id = 0;
2137
2138         rx_conf->conf.cpu_id = sched_getcpu();
2139         rte_atomic16_inc(&rx_counter);
2140         while (1) {
2141
2142                 /*
2143                  * Read packet from RX queues
2144                  */
2145                 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2146                         portid = rx_conf->rx_queue_list[i].port_id;
2147                         queueid = rx_conf->rx_queue_list[i].queue_id;
2148
2149                         SET_CPU_BUSY(rx_conf, CPU_POLL);
2150                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2151                                 MAX_PKT_BURST);
2152                         SET_CPU_IDLE(rx_conf, CPU_POLL);
2153
2154                         if (nb_rx != 0) {
2155                                 worker_id = (worker_id + 1) % rx_conf->n_ring;
2156                                 old_len = len[worker_id];
2157
2158                                 SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2159                                 ret = rte_ring_sp_enqueue_burst(
2160                                                 rx_conf->ring[worker_id],
2161                                                 (void **) pkts_burst,
2162                                                 nb_rx);
2163
2164                                 new_len = old_len + ret;
2165
2166                                 if (new_len >= BURST_SIZE) {
2167                                         lthread_cond_signal(rx_conf->ready[worker_id]);
2168                                         new_len = 0;
2169                                 }
2170
2171                                 len[worker_id] = new_len;
2172
2173                                 if (unlikely(ret < nb_rx)) {
2174                                         uint32_t k;
2175
2176                                         for (k = ret; k < nb_rx; k++) {
2177                                                 struct rte_mbuf *m = pkts_burst[k];
2178
2179                                                 rte_pktmbuf_free(m);
2180                                         }
2181                                 }
2182                                 SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2183                         }
2184
2185                         lthread_yield();
2186                 }
2187         }
2188 }
2189
2190 /*
2191  * Start scheduler with initial lthread on lcore
2192  *
2193  * This lthread loop spawns all rx and tx lthreads on master lcore
2194  */
2195
2196 static void
2197 lthread_spawner(__rte_unused void *arg) {
2198         struct lthread *lt[MAX_THREAD];
2199         int i;
2200         int n_thread = 0;
2201
2202         printf("Entering lthread_spawner\n");
2203
2204         /*
2205          * Create producers (rx threads) on default lcore
2206          */
2207         for (i = 0; i < n_rx_thread; i++) {
2208                 rx_thread[i].conf.thread_id = i;
2209                 lthread_create(&lt[n_thread], -1, lthread_rx,
2210                                 (void *)&rx_thread[i]);
2211                 n_thread++;
2212         }
2213
2214         /*
2215          * Wait for all producers. Until some producers can be started on the same
2216          * scheduler as this lthread, yielding is required to let them to run and
2217          * prevent deadlock here.
2218          */
2219         while (rte_atomic16_read(&rx_counter) < n_rx_thread)
2220                 lthread_sleep(100000);
2221
2222         /*
2223          * Create consumers (tx threads) on default lcore_id
2224          */
2225         for (i = 0; i < n_tx_thread; i++) {
2226                 tx_thread[i].conf.thread_id = i;
2227                 lthread_create(&lt[n_thread], -1, lthread_tx,
2228                                 (void *)&tx_thread[i]);
2229                 n_thread++;
2230         }
2231
2232         /*
2233          * Wait for all threads finished
2234          */
2235         for (i = 0; i < n_thread; i++)
2236                 lthread_join(lt[i], NULL);
2237
2238 }
2239
2240 /*
2241  * Start master scheduler with initial lthread spawning rx and tx lthreads
2242  * (main_lthread_master).
2243  */
2244 static int
2245 lthread_master_spawner(__rte_unused void *arg) {
2246         struct lthread *lt;
2247         int lcore_id = rte_lcore_id();
2248
2249         RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2250         lthread_create(&lt, -1, lthread_spawner, NULL);
2251         lthread_run();
2252
2253         return 0;
2254 }
2255
2256 /*
2257  * Start scheduler on lcore.
2258  */
2259 static int
2260 sched_spawner(__rte_unused void *arg) {
2261         struct lthread *lt;
2262         int lcore_id = rte_lcore_id();
2263
2264 #if (APP_CPU_LOAD)
2265         if (lcore_id == cpu_load_lcore_id) {
2266                 cpu_load_collector(arg);
2267                 return 0;
2268         }
2269 #endif /* APP_CPU_LOAD */
2270
2271         RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2272         lthread_create(&lt, -1, lthread_null, NULL);
2273         lthread_run();
2274
2275         return 0;
2276 }
2277
2278 /* main processing loop */
2279 static int
2280 pthread_tx(void *dummy)
2281 {
2282         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2283         uint64_t prev_tsc, diff_tsc, cur_tsc;
2284         int nb_rx;
2285         uint8_t portid;
2286         struct thread_tx_conf *tx_conf;
2287
2288         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
2289                 US_PER_S * BURST_TX_DRAIN_US;
2290
2291         prev_tsc = 0;
2292
2293         tx_conf = (struct thread_tx_conf *)dummy;
2294
2295         RTE_LOG(INFO, L3FWD, "Entering main Tx loop on lcore %u\n", rte_lcore_id());
2296
2297         tx_conf->conf.cpu_id = sched_getcpu();
2298         rte_atomic16_inc(&tx_counter);
2299         while (1) {
2300
2301                 cur_tsc = rte_rdtsc();
2302
2303                 /*
2304                  * TX burst queue drain
2305                  */
2306                 diff_tsc = cur_tsc - prev_tsc;
2307                 if (unlikely(diff_tsc > drain_tsc)) {
2308
2309                         /*
2310                          * This could be optimized (use queueid instead of
2311                          * portid), but it is not called so often
2312                          */
2313                         SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2314                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
2315                                 if (tx_conf->tx_mbufs[portid].len == 0)
2316                                         continue;
2317                                 send_burst(tx_conf, tx_conf->tx_mbufs[portid].len, portid);
2318                                 tx_conf->tx_mbufs[portid].len = 0;
2319                         }
2320                         SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2321
2322                         prev_tsc = cur_tsc;
2323                 }
2324
2325                 /*
2326                  * Read packet from ring
2327                  */
2328                 SET_CPU_BUSY(tx_conf, CPU_POLL);
2329                 nb_rx = rte_ring_sc_dequeue_burst(tx_conf->ring,
2330                                 (void **)pkts_burst, MAX_PKT_BURST);
2331                 SET_CPU_IDLE(tx_conf, CPU_POLL);
2332
2333                 if (unlikely(nb_rx == 0)) {
2334                         sched_yield();
2335                         continue;
2336                 }
2337
2338                 SET_CPU_BUSY(tx_conf, CPU_PROCESS);
2339                 portid = pkts_burst[0]->port;
2340                 process_burst(pkts_burst, nb_rx, portid);
2341                 SET_CPU_IDLE(tx_conf, CPU_PROCESS);
2342
2343         }
2344 }
2345
2346 static int
2347 pthread_rx(void *dummy)
2348 {
2349         int i;
2350         int worker_id;
2351         uint32_t n;
2352         uint32_t nb_rx;
2353         unsigned lcore_id;
2354         uint8_t portid, queueid;
2355         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
2356
2357         struct thread_rx_conf *rx_conf;
2358
2359         lcore_id = rte_lcore_id();
2360         rx_conf = (struct thread_rx_conf *)dummy;
2361
2362         if (rx_conf->n_rx_queue == 0) {
2363                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
2364                 return 0;
2365         }
2366
2367         RTE_LOG(INFO, L3FWD, "entering main rx loop on lcore %u\n", lcore_id);
2368
2369         for (i = 0; i < rx_conf->n_rx_queue; i++) {
2370
2371                 portid = rx_conf->rx_queue_list[i].port_id;
2372                 queueid = rx_conf->rx_queue_list[i].queue_id;
2373                 RTE_LOG(INFO, L3FWD, " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
2374                                 lcore_id, portid, queueid);
2375         }
2376
2377         worker_id = 0;
2378         rx_conf->conf.cpu_id = sched_getcpu();
2379         rte_atomic16_inc(&rx_counter);
2380         while (1) {
2381
2382                 /*
2383                  * Read packet from RX queues
2384                  */
2385                 for (i = 0; i < rx_conf->n_rx_queue; ++i) {
2386                         portid = rx_conf->rx_queue_list[i].port_id;
2387                         queueid = rx_conf->rx_queue_list[i].queue_id;
2388
2389                         SET_CPU_BUSY(rx_conf, CPU_POLL);
2390                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
2391                                 MAX_PKT_BURST);
2392                         SET_CPU_IDLE(rx_conf, CPU_POLL);
2393
2394                         if (nb_rx == 0) {
2395                                 sched_yield();
2396                                 continue;
2397                         }
2398
2399                         SET_CPU_BUSY(rx_conf, CPU_PROCESS);
2400                         worker_id = (worker_id + 1) % rx_conf->n_ring;
2401                         n = rte_ring_sp_enqueue_burst(rx_conf->ring[worker_id],
2402                                         (void **)pkts_burst, nb_rx);
2403
2404                         if (unlikely(n != nb_rx)) {
2405                                 uint32_t k;
2406
2407                                 for (k = n; k < nb_rx; k++) {
2408                                         struct rte_mbuf *m = pkts_burst[k];
2409
2410                                         rte_pktmbuf_free(m);
2411                                 }
2412                         }
2413
2414                         SET_CPU_IDLE(rx_conf, CPU_PROCESS);
2415
2416                 }
2417         }
2418 }
2419
2420 /*
2421  * P-Thread spawner.
2422  */
2423 static int
2424 pthread_run(__rte_unused void *arg) {
2425         int lcore_id = rte_lcore_id();
2426         int i;
2427
2428         for (i = 0; i < n_rx_thread; i++)
2429                 if (rx_thread[i].conf.lcore_id == lcore_id) {
2430                         printf("Start rx thread on %d...\n", lcore_id);
2431                         RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2432                         RTE_PER_LCORE(lcore_conf)->data = (void *)&rx_thread[i];
2433                         pthread_rx((void *)&rx_thread[i]);
2434                         return 0;
2435                 }
2436
2437         for (i = 0; i < n_tx_thread; i++)
2438                 if (tx_thread[i].conf.lcore_id == lcore_id) {
2439                         printf("Start tx thread on %d...\n", lcore_id);
2440                         RTE_PER_LCORE(lcore_conf) = &lcore_conf[lcore_id];
2441                         RTE_PER_LCORE(lcore_conf)->data = (void *)&tx_thread[i];
2442                         pthread_tx((void *)&tx_thread[i]);
2443                         return 0;
2444                 }
2445
2446 #if (APP_CPU_LOAD)
2447         if (lcore_id == cpu_load_lcore_id)
2448                 cpu_load_collector(arg);
2449 #endif /* APP_CPU_LOAD */
2450
2451         return 0;
2452 }
2453
2454 static int
2455 check_lcore_params(void)
2456 {
2457         uint8_t queue, lcore;
2458         uint16_t i;
2459         int socketid;
2460
2461         for (i = 0; i < nb_rx_thread_params; ++i) {
2462                 queue = rx_thread_params[i].queue_id;
2463                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
2464                         printf("invalid queue number: %hhu\n", queue);
2465                         return -1;
2466                 }
2467                 lcore = rx_thread_params[i].lcore_id;
2468                 if (!rte_lcore_is_enabled(lcore)) {
2469                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
2470                         return -1;
2471                 }
2472                 socketid = rte_lcore_to_socket_id(lcore);
2473                 if ((socketid != 0) && (numa_on == 0))
2474                         printf("warning: lcore %hhu is on socket %d with numa off\n",
2475                                 lcore, socketid);
2476         }
2477         return 0;
2478 }
2479
2480 static int
2481 check_port_config(const unsigned nb_ports)
2482 {
2483         unsigned portid;
2484         uint16_t i;
2485
2486         for (i = 0; i < nb_rx_thread_params; ++i) {
2487                 portid = rx_thread_params[i].port_id;
2488                 if ((enabled_port_mask & (1 << portid)) == 0) {
2489                         printf("port %u is not enabled in port mask\n", portid);
2490                         return -1;
2491                 }
2492                 if (portid >= nb_ports) {
2493                         printf("port %u is not present on the board\n", portid);
2494                         return -1;
2495                 }
2496         }
2497         return 0;
2498 }
2499
2500 static uint8_t
2501 get_port_n_rx_queues(const uint8_t port)
2502 {
2503         int queue = -1;
2504         uint16_t i;
2505
2506         for (i = 0; i < nb_rx_thread_params; ++i)
2507                 if (rx_thread_params[i].port_id == port &&
2508                                 rx_thread_params[i].queue_id > queue)
2509                         queue = rx_thread_params[i].queue_id;
2510
2511         return (uint8_t)(++queue);
2512 }
2513
2514 static int
2515 init_rx_rings(void)
2516 {
2517         unsigned socket_io;
2518         struct thread_rx_conf *rx_conf;
2519         struct thread_tx_conf *tx_conf;
2520         unsigned rx_thread_id, tx_thread_id;
2521         char name[256];
2522         struct rte_ring *ring = NULL;
2523
2524         for (tx_thread_id = 0; tx_thread_id < n_tx_thread; tx_thread_id++) {
2525
2526                 tx_conf = &tx_thread[tx_thread_id];
2527
2528                 printf("Connecting tx-thread %d with rx-thread %d\n", tx_thread_id,
2529                                 tx_conf->conf.thread_id);
2530
2531                 rx_thread_id = tx_conf->conf.thread_id;
2532                 if (rx_thread_id > n_tx_thread) {
2533                         printf("connection from tx-thread %u to rx-thread %u fails "
2534                                         "(rx-thread not defined)\n", tx_thread_id, rx_thread_id);
2535                         return -1;
2536                 }
2537
2538                 rx_conf = &rx_thread[rx_thread_id];
2539                 socket_io = rte_lcore_to_socket_id(rx_conf->conf.lcore_id);
2540
2541                 snprintf(name, sizeof(name), "app_ring_s%u_rx%u_tx%u",
2542                                 socket_io, rx_thread_id, tx_thread_id);
2543
2544                 ring = rte_ring_create(name, 1024 * 4, socket_io,
2545                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
2546
2547                 if (ring == NULL) {
2548                         rte_panic("Cannot create ring to connect rx-thread %u "
2549                                         "with tx-thread %u\n", rx_thread_id, tx_thread_id);
2550                 }
2551
2552                 rx_conf->ring[rx_conf->n_ring] = ring;
2553
2554                 tx_conf->ring = ring;
2555                 tx_conf->ready = &rx_conf->ready[rx_conf->n_ring];
2556
2557                 rx_conf->n_ring++;
2558         }
2559         return 0;
2560 }
2561
2562 static int
2563 init_rx_queues(void)
2564 {
2565         uint16_t i, nb_rx_queue;
2566         uint8_t thread;
2567
2568         n_rx_thread = 0;
2569
2570         for (i = 0; i < nb_rx_thread_params; ++i) {
2571                 thread = rx_thread_params[i].thread_id;
2572                 nb_rx_queue = rx_thread[thread].n_rx_queue;
2573
2574                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
2575                         printf("error: too many queues (%u) for thread: %u\n",
2576                                 (unsigned)nb_rx_queue + 1, (unsigned)thread);
2577                         return -1;
2578                 }
2579
2580                 rx_thread[thread].conf.thread_id = thread;
2581                 rx_thread[thread].conf.lcore_id = rx_thread_params[i].lcore_id;
2582                 rx_thread[thread].rx_queue_list[nb_rx_queue].port_id =
2583                         rx_thread_params[i].port_id;
2584                 rx_thread[thread].rx_queue_list[nb_rx_queue].queue_id =
2585                         rx_thread_params[i].queue_id;
2586                 rx_thread[thread].n_rx_queue++;
2587
2588                 if (thread >= n_rx_thread)
2589                         n_rx_thread = thread + 1;
2590
2591         }
2592         return 0;
2593 }
2594
2595 static int
2596 init_tx_threads(void)
2597 {
2598         int i;
2599
2600         n_tx_thread = 0;
2601         for (i = 0; i < nb_tx_thread_params; ++i) {
2602                 tx_thread[n_tx_thread].conf.thread_id = tx_thread_params[i].thread_id;
2603                 tx_thread[n_tx_thread].conf.lcore_id = tx_thread_params[i].lcore_id;
2604                 n_tx_thread++;
2605         }
2606         return 0;
2607 }
2608
2609 /* display usage */
2610 static void
2611 print_usage(const char *prgname)
2612 {
2613         printf("%s [EAL options] -- -p PORTMASK -P"
2614                 "  [--rx (port,queue,lcore,thread)[,(port,queue,lcore,thread]]"
2615                 "  [--tx (lcore,thread)[,(lcore,thread]]"
2616                 "  [--enable-jumbo [--max-pkt-len PKTLEN]]\n"
2617                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
2618                 "  -P : enable promiscuous mode\n"
2619                 "  --rx (port,queue,lcore,thread): rx queues configuration\n"
2620                 "  --tx (lcore,thread): tx threads configuration\n"
2621                 "  --stat-lcore LCORE: use lcore for stat collector\n"
2622                 "  --eth-dest=X,MM:MM:MM:MM:MM:MM: optional, ethernet destination for port X\n"
2623                 "  --no-numa: optional, disable numa awareness\n"
2624                 "  --ipv6: optional, specify it if running ipv6 packets\n"
2625                 "  --enable-jumbo: enable jumbo frame"
2626                 " which max packet len is PKTLEN in decimal (64-9600)\n"
2627                 "  --hash-entry-num: specify the hash entry number in hexadecimal to be setup\n"
2628                 "  --no-lthreads: turn off lthread model\n",
2629                 prgname);
2630 }
2631
2632 static int parse_max_pkt_len(const char *pktlen)
2633 {
2634         char *end = NULL;
2635         unsigned long len;
2636
2637         /* parse decimal string */
2638         len = strtoul(pktlen, &end, 10);
2639         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
2640                 return -1;
2641
2642         if (len == 0)
2643                 return -1;
2644
2645         return len;
2646 }
2647
2648 static int
2649 parse_portmask(const char *portmask)
2650 {
2651         char *end = NULL;
2652         unsigned long pm;
2653
2654         /* parse hexadecimal string */
2655         pm = strtoul(portmask, &end, 16);
2656         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
2657                 return -1;
2658
2659         if (pm == 0)
2660                 return -1;
2661
2662         return pm;
2663 }
2664
2665 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2666 static int
2667 parse_hash_entry_number(const char *hash_entry_num)
2668 {
2669         char *end = NULL;
2670         unsigned long hash_en;
2671
2672         /* parse hexadecimal string */
2673         hash_en = strtoul(hash_entry_num, &end, 16);
2674         if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
2675                 return -1;
2676
2677         if (hash_en == 0)
2678                 return -1;
2679
2680         return hash_en;
2681 }
2682 #endif
2683
2684 static int
2685 parse_rx_config(const char *q_arg)
2686 {
2687         char s[256];
2688         const char *p, *p0 = q_arg;
2689         char *end;
2690         enum fieldnames {
2691                 FLD_PORT = 0,
2692                 FLD_QUEUE,
2693                 FLD_LCORE,
2694                 FLD_THREAD,
2695                 _NUM_FLD
2696         };
2697         unsigned long int_fld[_NUM_FLD];
2698         char *str_fld[_NUM_FLD];
2699         int i;
2700         unsigned size;
2701
2702         nb_rx_thread_params = 0;
2703
2704         while ((p = strchr(p0, '(')) != NULL) {
2705                 ++p;
2706                 p0 = strchr(p, ')');
2707                 if (p0 == NULL)
2708                         return -1;
2709
2710                 size = p0 - p;
2711                 if (size >= sizeof(s))
2712                         return -1;
2713
2714                 snprintf(s, sizeof(s), "%.*s", size, p);
2715                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2716                         return -1;
2717                 for (i = 0; i < _NUM_FLD; i++) {
2718                         errno = 0;
2719                         int_fld[i] = strtoul(str_fld[i], &end, 0);
2720                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2721                                 return -1;
2722                 }
2723                 if (nb_rx_thread_params >= MAX_LCORE_PARAMS) {
2724                         printf("exceeded max number of rx params: %hu\n",
2725                                         nb_rx_thread_params);
2726                         return -1;
2727                 }
2728                 rx_thread_params_array[nb_rx_thread_params].port_id =
2729                                 (uint8_t)int_fld[FLD_PORT];
2730                 rx_thread_params_array[nb_rx_thread_params].queue_id =
2731                                 (uint8_t)int_fld[FLD_QUEUE];
2732                 rx_thread_params_array[nb_rx_thread_params].lcore_id =
2733                                 (uint8_t)int_fld[FLD_LCORE];
2734                 rx_thread_params_array[nb_rx_thread_params].thread_id =
2735                                 (uint8_t)int_fld[FLD_THREAD];
2736                 ++nb_rx_thread_params;
2737         }
2738         rx_thread_params = rx_thread_params_array;
2739         return 0;
2740 }
2741
2742 static int
2743 parse_tx_config(const char *q_arg)
2744 {
2745         char s[256];
2746         const char *p, *p0 = q_arg;
2747         char *end;
2748         enum fieldnames {
2749                 FLD_LCORE = 0,
2750                 FLD_THREAD,
2751                 _NUM_FLD
2752         };
2753         unsigned long int_fld[_NUM_FLD];
2754         char *str_fld[_NUM_FLD];
2755         int i;
2756         unsigned size;
2757
2758         nb_tx_thread_params = 0;
2759
2760         while ((p = strchr(p0, '(')) != NULL) {
2761                 ++p;
2762                 p0 = strchr(p, ')');
2763                 if (p0 == NULL)
2764                         return -1;
2765
2766                 size = p0 - p;
2767                 if (size >= sizeof(s))
2768                         return -1;
2769
2770                 snprintf(s, sizeof(s), "%.*s", size, p);
2771                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
2772                         return -1;
2773                 for (i = 0; i < _NUM_FLD; i++) {
2774                         errno = 0;
2775                         int_fld[i] = strtoul(str_fld[i], &end, 0);
2776                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
2777                                 return -1;
2778                 }
2779                 if (nb_tx_thread_params >= MAX_LCORE_PARAMS) {
2780                         printf("exceeded max number of tx params: %hu\n",
2781                                 nb_tx_thread_params);
2782                         return -1;
2783                 }
2784                 tx_thread_params_array[nb_tx_thread_params].lcore_id =
2785                                 (uint8_t)int_fld[FLD_LCORE];
2786                 tx_thread_params_array[nb_tx_thread_params].thread_id =
2787                                 (uint8_t)int_fld[FLD_THREAD];
2788                 ++nb_tx_thread_params;
2789         }
2790         tx_thread_params = tx_thread_params_array;
2791
2792         return 0;
2793 }
2794
2795 #if (APP_CPU_LOAD > 0)
2796 static int
2797 parse_stat_lcore(const char *stat_lcore)
2798 {
2799         char *end = NULL;
2800         unsigned long lcore_id;
2801
2802         lcore_id = strtoul(stat_lcore, &end, 10);
2803         if ((stat_lcore[0] == '\0') || (end == NULL) || (*end != '\0'))
2804                 return -1;
2805
2806         return lcore_id;
2807 }
2808 #endif
2809
2810 static void
2811 parse_eth_dest(const char *optarg)
2812 {
2813         uint8_t portid;
2814         char *port_end;
2815         uint8_t c, *dest, peer_addr[6];
2816
2817         errno = 0;
2818         portid = strtoul(optarg, &port_end, 10);
2819         if (errno != 0 || port_end == optarg || *port_end++ != ',')
2820                 rte_exit(EXIT_FAILURE,
2821                 "Invalid eth-dest: %s", optarg);
2822         if (portid >= RTE_MAX_ETHPORTS)
2823                 rte_exit(EXIT_FAILURE,
2824                 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
2825                 portid, RTE_MAX_ETHPORTS);
2826
2827         if (cmdline_parse_etheraddr(NULL, port_end,
2828                 &peer_addr, sizeof(peer_addr)) < 0)
2829                 rte_exit(EXIT_FAILURE,
2830                 "Invalid ethernet address: %s\n",
2831                 port_end);
2832         dest = (uint8_t *)&dest_eth_addr[portid];
2833         for (c = 0; c < 6; c++)
2834                 dest[c] = peer_addr[c];
2835         *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
2836 }
2837
2838 #define CMD_LINE_OPT_RX_CONFIG "rx"
2839 #define CMD_LINE_OPT_TX_CONFIG "tx"
2840 #define CMD_LINE_OPT_STAT_LCORE "stat-lcore"
2841 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
2842 #define CMD_LINE_OPT_NO_NUMA "no-numa"
2843 #define CMD_LINE_OPT_IPV6 "ipv6"
2844 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
2845 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
2846 #define CMD_LINE_OPT_NO_LTHREADS "no-lthreads"
2847
2848 /* Parse the argument given in the command line of the application */
2849 static int
2850 parse_args(int argc, char **argv)
2851 {
2852         int opt, ret;
2853         char **argvopt;
2854         int option_index;
2855         char *prgname = argv[0];
2856         static struct option lgopts[] = {
2857                 {CMD_LINE_OPT_RX_CONFIG, 1, 0, 0},
2858                 {CMD_LINE_OPT_TX_CONFIG, 1, 0, 0},
2859                 {CMD_LINE_OPT_STAT_LCORE, 1, 0, 0},
2860                 {CMD_LINE_OPT_ETH_DEST, 1, 0, 0},
2861                 {CMD_LINE_OPT_NO_NUMA, 0, 0, 0},
2862                 {CMD_LINE_OPT_IPV6, 0, 0, 0},
2863                 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, 0},
2864                 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, 0},
2865                 {CMD_LINE_OPT_NO_LTHREADS, 0, 0, 0},
2866                 {NULL, 0, 0, 0}
2867         };
2868
2869         argvopt = argv;
2870
2871         while ((opt = getopt_long(argc, argvopt, "p:P",
2872                                 lgopts, &option_index)) != EOF) {
2873
2874                 switch (opt) {
2875                 /* portmask */
2876                 case 'p':
2877                         enabled_port_mask = parse_portmask(optarg);
2878                         if (enabled_port_mask == 0) {
2879                                 printf("invalid portmask\n");
2880                                 print_usage(prgname);
2881                                 return -1;
2882                         }
2883                         break;
2884                 case 'P':
2885                         printf("Promiscuous mode selected\n");
2886                         promiscuous_on = 1;
2887                         break;
2888
2889                 /* long options */
2890                 case 0:
2891                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_RX_CONFIG,
2892                                 sizeof(CMD_LINE_OPT_RX_CONFIG))) {
2893                                 ret = parse_rx_config(optarg);
2894                                 if (ret) {
2895                                         printf("invalid rx-config\n");
2896                                         print_usage(prgname);
2897                                         return -1;
2898                                 }
2899                         }
2900
2901                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_TX_CONFIG,
2902                                 sizeof(CMD_LINE_OPT_TX_CONFIG))) {
2903                                 ret = parse_tx_config(optarg);
2904                                 if (ret) {
2905                                         printf("invalid tx-config\n");
2906                                         print_usage(prgname);
2907                                         return -1;
2908                                 }
2909                         }
2910
2911 #if (APP_CPU_LOAD > 0)
2912                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_STAT_LCORE,
2913                                         sizeof(CMD_LINE_OPT_STAT_LCORE))) {
2914                                 cpu_load_lcore_id = parse_stat_lcore(optarg);
2915                         }
2916 #endif
2917
2918                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ETH_DEST,
2919                                 sizeof(CMD_LINE_OPT_ETH_DEST)))
2920                                         parse_eth_dest(optarg);
2921
2922                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_NUMA,
2923                                 sizeof(CMD_LINE_OPT_NO_NUMA))) {
2924                                 printf("numa is disabled\n");
2925                                 numa_on = 0;
2926                         }
2927
2928 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2929                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_IPV6,
2930                                 sizeof(CMD_LINE_OPT_IPV6))) {
2931                                 printf("ipv6 is specified\n");
2932                                 ipv6 = 1;
2933                         }
2934 #endif
2935
2936                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_NO_LTHREADS,
2937                                         sizeof(CMD_LINE_OPT_NO_LTHREADS))) {
2938                                 printf("l-threads model is disabled\n");
2939                                 lthreads_on = 0;
2940                         }
2941
2942                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_ENABLE_JUMBO,
2943                                 sizeof(CMD_LINE_OPT_ENABLE_JUMBO))) {
2944                                 struct option lenopts = {"max-pkt-len", required_argument, 0,
2945                                                 0};
2946
2947                                 printf("jumbo frame is enabled - disabling simple TX path\n");
2948                                 port_conf.rxmode.jumbo_frame = 1;
2949
2950                                 /* if no max-pkt-len set, use the default value ETHER_MAX_LEN */
2951                                 if (0 == getopt_long(argc, argvopt, "", &lenopts,
2952                                                 &option_index)) {
2953
2954                                         ret = parse_max_pkt_len(optarg);
2955                                         if ((ret < 64) || (ret > MAX_JUMBO_PKT_LEN)) {
2956                                                 printf("invalid packet length\n");
2957                                                 print_usage(prgname);
2958                                                 return -1;
2959                                         }
2960                                         port_conf.rxmode.max_rx_pkt_len = ret;
2961                                 }
2962                                 printf("set jumbo frame max packet length to %u\n",
2963                                                 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
2964                         }
2965 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
2966                         if (!strncmp(lgopts[option_index].name, CMD_LINE_OPT_HASH_ENTRY_NUM,
2967                                 sizeof(CMD_LINE_OPT_HASH_ENTRY_NUM))) {
2968                                 ret = parse_hash_entry_number(optarg);
2969                                 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
2970                                         hash_entry_number = ret;
2971                                 } else {
2972                                         printf("invalid hash entry number\n");
2973                                         print_usage(prgname);
2974                                         return -1;
2975                                 }
2976                         }
2977 #endif
2978                         break;
2979
2980                 default:
2981                         print_usage(prgname);
2982                         return -1;
2983                 }
2984         }
2985
2986         if (optind >= 0)
2987                 argv[optind-1] = prgname;
2988
2989         ret = optind-1;
2990         optind = 0; /* reset getopt lib */
2991         return ret;
2992 }
2993
2994 static void
2995 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
2996 {
2997         char buf[ETHER_ADDR_FMT_SIZE];
2998
2999         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
3000         printf("%s%s", name, buf);
3001 }
3002
3003 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
3004
3005 static void convert_ipv4_5tuple(struct ipv4_5tuple *key1,
3006                 union ipv4_5tuple_host *key2)
3007 {
3008         key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
3009         key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
3010         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3011         key2->port_src = rte_cpu_to_be_16(key1->port_src);
3012         key2->proto = key1->proto;
3013         key2->pad0 = 0;
3014         key2->pad1 = 0;
3015 }
3016
3017 static void convert_ipv6_5tuple(struct ipv6_5tuple *key1,
3018                 union ipv6_5tuple_host *key2)
3019 {
3020         uint32_t i;
3021
3022         for (i = 0; i < 16; i++) {
3023                 key2->ip_dst[i] = key1->ip_dst[i];
3024                 key2->ip_src[i] = key1->ip_src[i];
3025         }
3026         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
3027         key2->port_src = rte_cpu_to_be_16(key1->port_src);
3028         key2->proto = key1->proto;
3029         key2->pad0 = 0;
3030         key2->pad1 = 0;
3031         key2->reserve = 0;
3032 }
3033
3034 #define BYTE_VALUE_MAX 256
3035 #define ALL_32_BITS 0xffffffff
3036 #define BIT_8_TO_15 0x0000ff00
3037 static inline void
3038 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
3039 {
3040         uint32_t i;
3041         int32_t ret;
3042         uint32_t array_len = RTE_DIM(ipv4_l3fwd_route_array);
3043
3044         mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3045         for (i = 0; i < array_len; i++) {
3046                 struct ipv4_l3fwd_route  entry;
3047                 union ipv4_5tuple_host newkey;
3048
3049                 entry = ipv4_l3fwd_route_array[i];
3050                 convert_ipv4_5tuple(&entry.key, &newkey);
3051                 ret = rte_hash_add_key(h, (void *)&newkey);
3052                 if (ret < 0) {
3053                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3054                                 " to the l3fwd hash.\n", i);
3055                 }
3056                 ipv4_l3fwd_out_if[ret] = entry.if_out;
3057         }
3058         printf("Hash: Adding 0x%" PRIx32 " keys\n", array_len);
3059 }
3060
3061 #define BIT_16_TO_23 0x00ff0000
3062 static inline void
3063 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
3064 {
3065         uint32_t i;
3066         int32_t ret;
3067         uint32_t array_len = RTE_DIM(ipv6_l3fwd_route_array);
3068
3069         mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3070         mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3071         for (i = 0; i < array_len; i++) {
3072                 struct ipv6_l3fwd_route entry;
3073                 union ipv6_5tuple_host newkey;
3074
3075                 entry = ipv6_l3fwd_route_array[i];
3076                 convert_ipv6_5tuple(&entry.key, &newkey);
3077                 ret = rte_hash_add_key(h, (void *)&newkey);
3078                 if (ret < 0) {
3079                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
3080                                 " to the l3fwd hash.\n", i);
3081                 }
3082                 ipv6_l3fwd_out_if[ret] = entry.if_out;
3083         }
3084         printf("Hash: Adding 0x%" PRIx32 "keys\n", array_len);
3085 }
3086
3087 #define NUMBER_PORT_USED 4
3088 static inline void
3089 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
3090                 unsigned int nr_flow)
3091 {
3092         unsigned i;
3093
3094         mask0 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_8_TO_15);
3095
3096         for (i = 0; i < nr_flow; i++) {
3097                 struct ipv4_l3fwd_route entry;
3098                 union ipv4_5tuple_host newkey;
3099                 uint8_t a = (uint8_t)((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3100                 uint8_t b = (uint8_t)(((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3101                                 BYTE_VALUE_MAX);
3102                 uint8_t c = (uint8_t)((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3103                                 BYTE_VALUE_MAX));
3104                 /* Create the ipv4 exact match flow */
3105                 memset(&entry, 0, sizeof(entry));
3106                 switch (i & (NUMBER_PORT_USED - 1)) {
3107                 case 0:
3108                         entry = ipv4_l3fwd_route_array[0];
3109                         entry.key.ip_dst = IPv4(101, c, b, a);
3110                         break;
3111                 case 1:
3112                         entry = ipv4_l3fwd_route_array[1];
3113                         entry.key.ip_dst = IPv4(201, c, b, a);
3114                         break;
3115                 case 2:
3116                         entry = ipv4_l3fwd_route_array[2];
3117                         entry.key.ip_dst = IPv4(111, c, b, a);
3118                         break;
3119                 case 3:
3120                         entry = ipv4_l3fwd_route_array[3];
3121                         entry.key.ip_dst = IPv4(211, c, b, a);
3122                         break;
3123                 };
3124                 convert_ipv4_5tuple(&entry.key, &newkey);
3125                 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3126
3127                 if (ret < 0)
3128                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3129
3130                 ipv4_l3fwd_out_if[ret] = (uint8_t)entry.if_out;
3131
3132         }
3133         printf("Hash: Adding 0x%x keys\n", nr_flow);
3134 }
3135
3136 static inline void
3137 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
3138                 unsigned int nr_flow)
3139 {
3140         unsigned i;
3141
3142         mask1 = _mm_set_epi32(ALL_32_BITS, ALL_32_BITS, ALL_32_BITS, BIT_16_TO_23);
3143         mask2 = _mm_set_epi32(0, 0, ALL_32_BITS, ALL_32_BITS);
3144         for (i = 0; i < nr_flow; i++) {
3145                 struct ipv6_l3fwd_route entry;
3146                 union ipv6_5tuple_host newkey;
3147
3148                 uint8_t a = (uint8_t) ((i / NUMBER_PORT_USED) % BYTE_VALUE_MAX);
3149                 uint8_t b = (uint8_t) (((i / NUMBER_PORT_USED) / BYTE_VALUE_MAX) %
3150                                 BYTE_VALUE_MAX);
3151                 uint8_t c = (uint8_t) ((i / NUMBER_PORT_USED) / (BYTE_VALUE_MAX *
3152                                 BYTE_VALUE_MAX));
3153
3154                 /* Create the ipv6 exact match flow */
3155                 memset(&entry, 0, sizeof(entry));
3156                 switch (i & (NUMBER_PORT_USED - 1)) {
3157                 case 0:
3158                         entry = ipv6_l3fwd_route_array[0];
3159                         break;
3160                 case 1:
3161                         entry = ipv6_l3fwd_route_array[1];
3162                         break;
3163                 case 2:
3164                         entry = ipv6_l3fwd_route_array[2];
3165                         break;
3166                 case 3:
3167                         entry = ipv6_l3fwd_route_array[3];
3168                         break;
3169                 };
3170                 entry.key.ip_dst[13] = c;
3171                 entry.key.ip_dst[14] = b;
3172                 entry.key.ip_dst[15] = a;
3173                 convert_ipv6_5tuple(&entry.key, &newkey);
3174                 int32_t ret = rte_hash_add_key(h, (void *)&newkey);
3175
3176                 if (ret < 0)
3177                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
3178
3179                 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
3180
3181         }
3182         printf("Hash: Adding 0x%x keys\n", nr_flow);
3183 }
3184
3185 static void
3186 setup_hash(int socketid)
3187 {
3188         struct rte_hash_parameters ipv4_l3fwd_hash_params = {
3189                 .name = NULL,
3190                 .entries = L3FWD_HASH_ENTRIES,
3191                 .key_len = sizeof(union ipv4_5tuple_host),
3192                 .hash_func = ipv4_hash_crc,
3193                 .hash_func_init_val = 0,
3194         };
3195
3196         struct rte_hash_parameters ipv6_l3fwd_hash_params = {
3197                 .name = NULL,
3198                 .entries = L3FWD_HASH_ENTRIES,
3199                 .key_len = sizeof(union ipv6_5tuple_host),
3200                 .hash_func = ipv6_hash_crc,
3201                 .hash_func_init_val = 0,
3202         };
3203
3204         char s[64];
3205
3206         /* create ipv4 hash */
3207         snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
3208         ipv4_l3fwd_hash_params.name = s;
3209         ipv4_l3fwd_hash_params.socket_id = socketid;
3210         ipv4_l3fwd_lookup_struct[socketid] =
3211                         rte_hash_create(&ipv4_l3fwd_hash_params);
3212         if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3213                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3214                                 "socket %d\n", socketid);
3215
3216         /* create ipv6 hash */
3217         snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
3218         ipv6_l3fwd_hash_params.name = s;
3219         ipv6_l3fwd_hash_params.socket_id = socketid;
3220         ipv6_l3fwd_lookup_struct[socketid] =
3221                         rte_hash_create(&ipv6_l3fwd_hash_params);
3222         if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3223                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
3224                                 "socket %d\n", socketid);
3225
3226         if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
3227                 /* For testing hash matching with a large number of flows we
3228                  * generate millions of IP 5-tuples with an incremented dst
3229                  * address to initialize the hash table. */
3230                 if (ipv6 == 0) {
3231                         /* populate the ipv4 hash */
3232                         populate_ipv4_many_flow_into_table(
3233                                 ipv4_l3fwd_lookup_struct[socketid], hash_entry_number);
3234                 } else {
3235                         /* populate the ipv6 hash */
3236                         populate_ipv6_many_flow_into_table(
3237                                 ipv6_l3fwd_lookup_struct[socketid], hash_entry_number);
3238                 }
3239         } else {
3240                 /* Use data in ipv4/ipv6 l3fwd lookup table directly to initialize
3241                  * the hash table */
3242                 if (ipv6 == 0) {
3243                         /* populate the ipv4 hash */
3244                         populate_ipv4_few_flow_into_table(
3245                                         ipv4_l3fwd_lookup_struct[socketid]);
3246                 } else {
3247                         /* populate the ipv6 hash */
3248                         populate_ipv6_few_flow_into_table(
3249                                         ipv6_l3fwd_lookup_struct[socketid]);
3250                 }
3251         }
3252 }
3253 #endif
3254
3255 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3256 static void
3257 setup_lpm(int socketid)
3258 {
3259         struct rte_lpm6_config config;
3260         struct rte_lpm_config lpm_ipv4_config;
3261         unsigned i;
3262         int ret;
3263         char s[64];
3264
3265         /* create the LPM table */
3266         snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
3267         lpm_ipv4_config.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
3268         lpm_ipv4_config.number_tbl8s = 256;
3269         lpm_ipv4_config.flags = 0;
3270         ipv4_l3fwd_lookup_struct[socketid] =
3271                         rte_lpm_create(s, socketid, &lpm_ipv4_config);
3272         if (ipv4_l3fwd_lookup_struct[socketid] == NULL)
3273                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3274                                 " on socket %d\n", socketid);
3275
3276         /* populate the LPM table */
3277         for (i = 0; i < IPV4_L3FWD_NUM_ROUTES; i++) {
3278
3279                 /* skip unused ports */
3280                 if ((1 << ipv4_l3fwd_route_array[i].if_out &
3281                                 enabled_port_mask) == 0)
3282                         continue;
3283
3284                 ret = rte_lpm_add(ipv4_l3fwd_lookup_struct[socketid],
3285                         ipv4_l3fwd_route_array[i].ip,
3286                         ipv4_l3fwd_route_array[i].depth,
3287                         ipv4_l3fwd_route_array[i].if_out);
3288
3289                 if (ret < 0) {
3290                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3291                                 "l3fwd LPM table on socket %d\n",
3292                                 i, socketid);
3293                 }
3294
3295                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
3296                         (unsigned)ipv4_l3fwd_route_array[i].ip,
3297                         ipv4_l3fwd_route_array[i].depth,
3298                         ipv4_l3fwd_route_array[i].if_out);
3299         }
3300
3301         /* create the LPM6 table */
3302         snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
3303
3304         config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
3305         config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
3306         config.flags = 0;
3307         ipv6_l3fwd_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
3308                                 &config);
3309         if (ipv6_l3fwd_lookup_struct[socketid] == NULL)
3310                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
3311                                 " on socket %d\n", socketid);
3312
3313         /* populate the LPM table */
3314         for (i = 0; i < IPV6_L3FWD_NUM_ROUTES; i++) {
3315
3316                 /* skip unused ports */
3317                 if ((1 << ipv6_l3fwd_route_array[i].if_out &
3318                                 enabled_port_mask) == 0)
3319                         continue;
3320
3321                 ret = rte_lpm6_add(ipv6_l3fwd_lookup_struct[socketid],
3322                         ipv6_l3fwd_route_array[i].ip,
3323                         ipv6_l3fwd_route_array[i].depth,
3324                         ipv6_l3fwd_route_array[i].if_out);
3325
3326                 if (ret < 0) {
3327                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
3328                                 "l3fwd LPM table on socket %d\n",
3329                                 i, socketid);
3330                 }
3331
3332                 printf("LPM: Adding route %s / %d (%d)\n",
3333                         "IPV6",
3334                         ipv6_l3fwd_route_array[i].depth,
3335                         ipv6_l3fwd_route_array[i].if_out);
3336         }
3337 }
3338 #endif
3339
3340 static int
3341 init_mem(unsigned nb_mbuf)
3342 {
3343         struct lcore_conf *qconf;
3344         int socketid;
3345         unsigned lcore_id;
3346         char s[64];
3347
3348         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3349                 if (rte_lcore_is_enabled(lcore_id) == 0)
3350                         continue;
3351
3352                 if (numa_on)
3353                         socketid = rte_lcore_to_socket_id(lcore_id);
3354                 else
3355                         socketid = 0;
3356
3357                 if (socketid >= NB_SOCKETS) {
3358                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
3359                                 socketid, lcore_id, NB_SOCKETS);
3360                 }
3361                 if (pktmbuf_pool[socketid] == NULL) {
3362                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
3363                         pktmbuf_pool[socketid] =
3364                                 rte_pktmbuf_pool_create(s, nb_mbuf,
3365                                         MEMPOOL_CACHE_SIZE, 0,
3366                                         RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
3367                         if (pktmbuf_pool[socketid] == NULL)
3368                                 rte_exit(EXIT_FAILURE,
3369                                                 "Cannot init mbuf pool on socket %d\n", socketid);
3370                         else
3371                                 printf("Allocated mbuf pool on socket %d\n", socketid);
3372
3373 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
3374                         setup_lpm(socketid);
3375 #else
3376                         setup_hash(socketid);
3377 #endif
3378                 }
3379                 qconf = &lcore_conf[lcore_id];
3380                 qconf->ipv4_lookup_struct = ipv4_l3fwd_lookup_struct[socketid];
3381                 qconf->ipv6_lookup_struct = ipv6_l3fwd_lookup_struct[socketid];
3382         }
3383         return 0;
3384 }
3385
3386 /* Check the link status of all ports in up to 9s, and print them finally */
3387 static void
3388 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
3389 {
3390 #define CHECK_INTERVAL 100 /* 100ms */
3391 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
3392         uint8_t portid, count, all_ports_up, print_flag = 0;
3393         struct rte_eth_link link;
3394
3395         printf("\nChecking link status");
3396         fflush(stdout);
3397         for (count = 0; count <= MAX_CHECK_TIME; count++) {
3398                 all_ports_up = 1;
3399                 for (portid = 0; portid < port_num; portid++) {
3400                         if ((port_mask & (1 << portid)) == 0)
3401                                 continue;
3402                         memset(&link, 0, sizeof(link));
3403                         rte_eth_link_get_nowait(portid, &link);
3404                         /* print link status if flag set */
3405                         if (print_flag == 1) {
3406                                 if (link.link_status)
3407                                         printf("Port %d Link Up - speed %u "
3408                                                 "Mbps - %s\n", (uint8_t)portid,
3409                                                 (unsigned)link.link_speed,
3410                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
3411                                         ("full-duplex") : ("half-duplex\n"));
3412                                 else
3413                                         printf("Port %d Link Down\n",
3414                                                 (uint8_t)portid);
3415                                 continue;
3416                         }
3417                         /* clear all_ports_up flag if any link down */
3418                         if (link.link_status == ETH_LINK_DOWN) {
3419                                 all_ports_up = 0;
3420                                 break;
3421                         }
3422                 }
3423                 /* after finally printing all link status, get out */
3424                 if (print_flag == 1)
3425                         break;
3426
3427                 if (all_ports_up == 0) {
3428                         printf(".");
3429                         fflush(stdout);
3430                         rte_delay_ms(CHECK_INTERVAL);
3431                 }
3432
3433                 /* set the print_flag if all ports up or timeout */
3434                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
3435                         print_flag = 1;
3436                         printf("done\n");
3437                 }
3438         }
3439 }
3440
3441 int
3442 main(int argc, char **argv)
3443 {
3444         struct rte_eth_dev_info dev_info;
3445         struct rte_eth_txconf *txconf;
3446         int ret;
3447         int i;
3448         unsigned nb_ports;
3449         uint16_t queueid;
3450         unsigned lcore_id;
3451         uint32_t n_tx_queue, nb_lcores;
3452         uint8_t portid, nb_rx_queue, queue, socketid;
3453
3454         /* init EAL */
3455         ret = rte_eal_init(argc, argv);
3456         if (ret < 0)
3457                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
3458         argc -= ret;
3459         argv += ret;
3460
3461         /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
3462         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
3463                 dest_eth_addr[portid] = ETHER_LOCAL_ADMIN_ADDR +
3464                                 ((uint64_t)portid << 40);
3465                 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
3466         }
3467
3468         /* parse application arguments (after the EAL ones) */
3469         ret = parse_args(argc, argv);
3470         if (ret < 0)
3471                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
3472
3473         if (check_lcore_params() < 0)
3474                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
3475
3476         printf("Initializing rx-queues...\n");
3477         ret = init_rx_queues();
3478         if (ret < 0)
3479                 rte_exit(EXIT_FAILURE, "init_rx_queues failed\n");
3480
3481         printf("Initializing tx-threads...\n");
3482         ret = init_tx_threads();
3483         if (ret < 0)
3484                 rte_exit(EXIT_FAILURE, "init_tx_threads failed\n");
3485
3486         printf("Initializing rings...\n");
3487         ret = init_rx_rings();
3488         if (ret < 0)
3489                 rte_exit(EXIT_FAILURE, "init_rx_rings failed\n");
3490
3491         nb_ports = rte_eth_dev_count();
3492
3493         if (check_port_config(nb_ports) < 0)
3494                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
3495
3496         nb_lcores = rte_lcore_count();
3497
3498         /* initialize all ports */
3499         for (portid = 0; portid < nb_ports; portid++) {
3500                 /* skip ports that are not enabled */
3501                 if ((enabled_port_mask & (1 << portid)) == 0) {
3502                         printf("\nSkipping disabled port %d\n", portid);
3503                         continue;
3504                 }
3505
3506                 /* init port */
3507                 printf("Initializing port %d ... ", portid);
3508                 fflush(stdout);
3509
3510                 nb_rx_queue = get_port_n_rx_queues(portid);
3511                 n_tx_queue = nb_lcores;
3512                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
3513                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
3514                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
3515                         nb_rx_queue, (unsigned)n_tx_queue);
3516                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
3517                                         (uint16_t)n_tx_queue, &port_conf);
3518                 if (ret < 0)
3519                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
3520                                 ret, portid);
3521
3522                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
3523                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
3524                 printf(", ");
3525                 print_ethaddr("Destination:",
3526                         (const struct ether_addr *)&dest_eth_addr[portid]);
3527                 printf(", ");
3528
3529                 /*
3530                  * prepare src MACs for each port.
3531                  */
3532                 ether_addr_copy(&ports_eth_addr[portid],
3533                         (struct ether_addr *)(val_eth + portid) + 1);
3534
3535                 /* init memory */
3536                 ret = init_mem(NB_MBUF);
3537                 if (ret < 0)
3538                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
3539
3540                 /* init one TX queue per couple (lcore,port) */
3541                 queueid = 0;
3542                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3543                         if (rte_lcore_is_enabled(lcore_id) == 0)
3544                                 continue;
3545
3546                         if (numa_on)
3547                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3548                         else
3549                                 socketid = 0;
3550
3551                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
3552                         fflush(stdout);
3553
3554                         rte_eth_dev_info_get(portid, &dev_info);
3555                         txconf = &dev_info.default_txconf;
3556                         if (port_conf.rxmode.jumbo_frame)
3557                                 txconf->txq_flags = 0;
3558                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
3559                                                      socketid, txconf);
3560                         if (ret < 0)
3561                                 rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
3562                                         "port=%d\n", ret, portid);
3563
3564                         tx_thread[lcore_id].tx_queue_id[portid] = queueid;
3565                         queueid++;
3566                 }
3567                 printf("\n");
3568         }
3569
3570         for (i = 0; i < n_rx_thread; i++) {
3571                 lcore_id = rx_thread[i].conf.lcore_id;
3572
3573                 if (rte_lcore_is_enabled(lcore_id) == 0) {
3574                         rte_exit(EXIT_FAILURE,
3575                                         "Cannot start Rx thread on lcore %u: lcore disabled\n",
3576                                         lcore_id
3577                                 );
3578                 }
3579
3580                 printf("\nInitializing rx queues for Rx thread %d on lcore %u ... ",
3581                                 i, lcore_id);
3582                 fflush(stdout);
3583
3584                 /* init RX queues */
3585                 for (queue = 0; queue < rx_thread[i].n_rx_queue; ++queue) {
3586                         portid = rx_thread[i].rx_queue_list[queue].port_id;
3587                         queueid = rx_thread[i].rx_queue_list[queue].queue_id;
3588
3589                         if (numa_on)
3590                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
3591                         else
3592                                 socketid = 0;
3593
3594                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
3595                         fflush(stdout);
3596
3597                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
3598                                         socketid,
3599                                         NULL,
3600                                         pktmbuf_pool[socketid]);
3601                         if (ret < 0)
3602                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, "
3603                                                 "port=%d\n", ret, portid);
3604                 }
3605         }
3606
3607         printf("\n");
3608
3609         /* start ports */
3610         for (portid = 0; portid < nb_ports; portid++) {
3611                 if ((enabled_port_mask & (1 << portid)) == 0)
3612                         continue;
3613
3614                 /* Start device */
3615                 ret = rte_eth_dev_start(portid);
3616                 if (ret < 0)
3617                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
3618                                 ret, portid);
3619
3620                 /*
3621                  * If enabled, put device in promiscuous mode.
3622                  * This allows IO forwarding mode to forward packets
3623                  * to itself through 2 cross-connected  ports of the
3624                  * target machine.
3625                  */
3626                 if (promiscuous_on)
3627                         rte_eth_promiscuous_enable(portid);
3628         }
3629
3630         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
3631
3632         if (lthreads_on) {
3633                 printf("Starting L-Threading Model\n");
3634
3635 #if (APP_CPU_LOAD > 0)
3636                 if (cpu_load_lcore_id > 0)
3637                         /* Use one lcore for cpu load collector */
3638                         nb_lcores--;
3639 #endif
3640
3641                 lthread_num_schedulers_set(nb_lcores);
3642                 rte_eal_mp_remote_launch(sched_spawner, NULL, SKIP_MASTER);
3643                 lthread_master_spawner(NULL);
3644
3645         } else {
3646                 printf("Starting P-Threading Model\n");
3647                 /* launch per-lcore init on every lcore */
3648                 rte_eal_mp_remote_launch(pthread_run, NULL, CALL_MASTER);
3649                 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
3650                         if (rte_eal_wait_lcore(lcore_id) < 0)
3651                                 return -1;
3652                 }
3653         }
3654
3655         return 0;
3656 }