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