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