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