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