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