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