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