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