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