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