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