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