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