examples/l3fwd: fix exact match performance
[dpdk.git] / examples / l3fwd / l3fwd_em.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 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <stdbool.h>
45
46 #include <rte_debug.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
49 #include <rte_ring.h>
50 #include <rte_mempool.h>
51 #include <rte_cycles.h>
52 #include <rte_mbuf.h>
53 #include <rte_ip.h>
54 #include <rte_tcp.h>
55 #include <rte_udp.h>
56 #include <rte_hash.h>
57
58 #include "l3fwd.h"
59
60 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
61 #include <rte_hash_crc.h>
62 #define DEFAULT_HASH_FUNC       rte_hash_crc
63 #else
64 #include <rte_jhash.h>
65 #define DEFAULT_HASH_FUNC       rte_jhash
66 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
67
68 #define IPV6_ADDR_LEN 16
69
70 struct ipv4_5tuple {
71         uint32_t ip_dst;
72         uint32_t ip_src;
73         uint16_t port_dst;
74         uint16_t port_src;
75         uint8_t  proto;
76 } __attribute__((__packed__));
77
78 union ipv4_5tuple_host {
79         struct {
80                 uint8_t  pad0;
81                 uint8_t  proto;
82                 uint16_t pad1;
83                 uint32_t ip_src;
84                 uint32_t ip_dst;
85                 uint16_t port_src;
86                 uint16_t port_dst;
87         };
88         xmm_t xmm;
89 };
90
91 #define XMM_NUM_IN_IPV6_5TUPLE 3
92
93 struct ipv6_5tuple {
94         uint8_t  ip_dst[IPV6_ADDR_LEN];
95         uint8_t  ip_src[IPV6_ADDR_LEN];
96         uint16_t port_dst;
97         uint16_t port_src;
98         uint8_t  proto;
99 } __attribute__((__packed__));
100
101 union ipv6_5tuple_host {
102         struct {
103                 uint16_t pad0;
104                 uint8_t  proto;
105                 uint8_t  pad1;
106                 uint8_t  ip_src[IPV6_ADDR_LEN];
107                 uint8_t  ip_dst[IPV6_ADDR_LEN];
108                 uint16_t port_src;
109                 uint16_t port_dst;
110                 uint64_t reserve;
111         };
112         xmm_t xmm[XMM_NUM_IN_IPV6_5TUPLE];
113 };
114
115
116
117 struct ipv4_l3fwd_em_route {
118         struct ipv4_5tuple key;
119         uint8_t if_out;
120 };
121
122 struct ipv6_l3fwd_em_route {
123         struct ipv6_5tuple key;
124         uint8_t if_out;
125 };
126
127 static struct ipv4_l3fwd_em_route ipv4_l3fwd_em_route_array[] = {
128         {{IPv4(101, 0, 0, 0), IPv4(100, 10, 0, 1),  101, 11, IPPROTO_TCP}, 0},
129         {{IPv4(201, 0, 0, 0), IPv4(200, 20, 0, 1),  102, 12, IPPROTO_TCP}, 1},
130         {{IPv4(111, 0, 0, 0), IPv4(100, 30, 0, 1),  101, 11, IPPROTO_TCP}, 2},
131         {{IPv4(211, 0, 0, 0), IPv4(200, 40, 0, 1),  102, 12, IPPROTO_TCP}, 3},
132 };
133
134 static struct ipv6_l3fwd_em_route ipv6_l3fwd_em_route_array[] = {
135         {{
136         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
137         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
138         101, 11, IPPROTO_TCP}, 0},
139
140         {{
141         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
142         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
143         102, 12, IPPROTO_TCP}, 1},
144
145         {{
146         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
147         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
148         101, 11, IPPROTO_TCP}, 2},
149
150         {{
151         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
152         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
153         102, 12, IPPROTO_TCP}, 3},
154 };
155
156 struct rte_hash *ipv4_l3fwd_em_lookup_struct[NB_SOCKETS];
157 struct rte_hash *ipv6_l3fwd_em_lookup_struct[NB_SOCKETS];
158
159 static inline uint32_t
160 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
161                 uint32_t init_val)
162 {
163         const union ipv4_5tuple_host *k;
164         uint32_t t;
165         const uint32_t *p;
166
167         k = data;
168         t = k->proto;
169         p = (const uint32_t *)&k->port_src;
170
171 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
172         init_val = rte_hash_crc_4byte(t, init_val);
173         init_val = rte_hash_crc_4byte(k->ip_src, init_val);
174         init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
175         init_val = rte_hash_crc_4byte(*p, init_val);
176 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
177         init_val = rte_jhash_1word(t, init_val);
178         init_val = rte_jhash_1word(k->ip_src, init_val);
179         init_val = rte_jhash_1word(k->ip_dst, init_val);
180         init_val = rte_jhash_1word(*p, init_val);
181 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
182
183         return init_val;
184 }
185
186 static inline uint32_t
187 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
188                 uint32_t init_val)
189 {
190         const union ipv6_5tuple_host *k;
191         uint32_t t;
192         const uint32_t *p;
193 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
194         const uint32_t  *ip_src0, *ip_src1, *ip_src2, *ip_src3;
195         const uint32_t  *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
196 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
197
198         k = data;
199         t = k->proto;
200         p = (const uint32_t *)&k->port_src;
201
202 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
203         ip_src0 = (const uint32_t *) k->ip_src;
204         ip_src1 = (const uint32_t *)(k->ip_src+4);
205         ip_src2 = (const uint32_t *)(k->ip_src+8);
206         ip_src3 = (const uint32_t *)(k->ip_src+12);
207         ip_dst0 = (const uint32_t *) k->ip_dst;
208         ip_dst1 = (const uint32_t *)(k->ip_dst+4);
209         ip_dst2 = (const uint32_t *)(k->ip_dst+8);
210         ip_dst3 = (const uint32_t *)(k->ip_dst+12);
211         init_val = rte_hash_crc_4byte(t, init_val);
212         init_val = rte_hash_crc_4byte(*ip_src0, init_val);
213         init_val = rte_hash_crc_4byte(*ip_src1, init_val);
214         init_val = rte_hash_crc_4byte(*ip_src2, init_val);
215         init_val = rte_hash_crc_4byte(*ip_src3, init_val);
216         init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
217         init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
218         init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
219         init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
220         init_val = rte_hash_crc_4byte(*p, init_val);
221 #else /* RTE_MACHINE_CPUFLAG_SSE4_2 */
222         init_val = rte_jhash_1word(t, init_val);
223         init_val = rte_jhash(k->ip_src,
224                         sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
225         init_val = rte_jhash(k->ip_dst,
226                         sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
227         init_val = rte_jhash_1word(*p, init_val);
228 #endif /* RTE_MACHINE_CPUFLAG_SSE4_2 */
229         return init_val;
230 }
231
232 #define IPV4_L3FWD_EM_NUM_ROUTES \
233         (sizeof(ipv4_l3fwd_em_route_array) / sizeof(ipv4_l3fwd_em_route_array[0]))
234
235 #define IPV6_L3FWD_EM_NUM_ROUTES \
236         (sizeof(ipv6_l3fwd_em_route_array) / sizeof(ipv6_l3fwd_em_route_array[0]))
237
238 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
239 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
240
241 static rte_xmm_t mask0;
242 static rte_xmm_t mask1;
243 static rte_xmm_t mask2;
244
245 #if defined(__SSE2__)
246 static inline xmm_t
247 em_mask_key(void *key, xmm_t mask)
248 {
249         __m128i data = _mm_loadu_si128((__m128i *)(key));
250
251         return _mm_and_si128(data, mask);
252 }
253 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
254 static inline xmm_t
255 em_mask_key(void *key, xmm_t mask)
256 {
257         int32x4_t data = vld1q_s32((int32_t *)key);
258
259         return vandq_s32(data, mask);
260 }
261 #endif
262
263 static inline uint8_t
264 em_get_ipv4_dst_port(void *ipv4_hdr, uint8_t portid, void *lookup_struct)
265 {
266         int ret = 0;
267         union ipv4_5tuple_host key;
268         struct rte_hash *ipv4_l3fwd_lookup_struct =
269                 (struct rte_hash *)lookup_struct;
270
271         ipv4_hdr = (uint8_t *)ipv4_hdr + offsetof(struct ipv4_hdr, time_to_live);
272
273         /*
274          * Get 5 tuple: dst port, src port, dst IP address,
275          * src IP address and protocol.
276          */
277         key.xmm = em_mask_key(ipv4_hdr, mask0.x);
278
279         /* Find destination port */
280         ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
281         return (uint8_t)((ret < 0) ? portid : ipv4_l3fwd_out_if[ret]);
282 }
283
284 static inline uint8_t
285 em_get_ipv6_dst_port(void *ipv6_hdr,  uint8_t portid, void *lookup_struct)
286 {
287         int ret = 0;
288         union ipv6_5tuple_host key;
289         struct rte_hash *ipv6_l3fwd_lookup_struct =
290                 (struct rte_hash *)lookup_struct;
291
292         ipv6_hdr = (uint8_t *)ipv6_hdr + offsetof(struct ipv6_hdr, payload_len);
293         void *data0 = ipv6_hdr;
294         void *data1 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t);
295         void *data2 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t) + sizeof(xmm_t);
296
297         /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
298         key.xmm[0] = em_mask_key(data0, mask1.x);
299
300         /*
301          * Get part of 5 tuple: dst IP address lower 96 bits
302          * and src IP address higher 32 bits.
303          */
304         key.xmm[1] = *(xmm_t *)data1;
305
306         /*
307          * Get part of 5 tuple: dst port and src port
308          * and dst IP address higher 32 bits.
309          */
310         key.xmm[2] = em_mask_key(data2, mask2.x);
311
312         /* Find destination port */
313         ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
314         return (uint8_t)((ret < 0) ? portid : ipv6_l3fwd_out_if[ret]);
315 }
316
317
318 /*
319  * Include header file if SSE4_1 is enabled for
320  * buffer optimization i.e. ENABLE_MULTI_BUFFER_OPTIMIZE=1.
321  */
322 #if defined(__SSE4_1__)
323 #if defined(NO_HASH_MULTI_LOOKUP)
324 #include "l3fwd_em_sse.h"
325 #else
326 #include "l3fwd_em_hlm_sse.h"
327 #endif
328 #else
329 #include "l3fwd_em.h"
330 #endif
331
332 static void
333 convert_ipv4_5tuple(struct ipv4_5tuple *key1,
334                 union ipv4_5tuple_host *key2)
335 {
336         key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
337         key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
338         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
339         key2->port_src = rte_cpu_to_be_16(key1->port_src);
340         key2->proto = key1->proto;
341         key2->pad0 = 0;
342         key2->pad1 = 0;
343 }
344
345 static void
346 convert_ipv6_5tuple(struct ipv6_5tuple *key1,
347                 union ipv6_5tuple_host *key2)
348 {
349         uint32_t i;
350
351         for (i = 0; i < 16; i++) {
352                 key2->ip_dst[i] = key1->ip_dst[i];
353                 key2->ip_src[i] = key1->ip_src[i];
354         }
355         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
356         key2->port_src = rte_cpu_to_be_16(key1->port_src);
357         key2->proto = key1->proto;
358         key2->pad0 = 0;
359         key2->pad1 = 0;
360         key2->reserve = 0;
361 }
362
363 #define BYTE_VALUE_MAX 256
364 #define ALL_32_BITS 0xffffffff
365 #define BIT_8_TO_15 0x0000ff00
366
367 static inline void
368 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
369 {
370         uint32_t i;
371         int32_t ret;
372
373         mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
374                                 ALL_32_BITS, ALL_32_BITS} };
375
376         for (i = 0; i < IPV4_L3FWD_EM_NUM_ROUTES; i++) {
377                 struct ipv4_l3fwd_em_route  entry;
378                 union ipv4_5tuple_host newkey;
379
380                 entry = ipv4_l3fwd_em_route_array[i];
381                 convert_ipv4_5tuple(&entry.key, &newkey);
382                 ret = rte_hash_add_key(h, (void *) &newkey);
383                 if (ret < 0) {
384                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
385                                 " to the l3fwd hash.\n", i);
386                 }
387                 ipv4_l3fwd_out_if[ret] = entry.if_out;
388         }
389         printf("Hash: Adding 0x%" PRIx64 " keys\n",
390                 (uint64_t)IPV4_L3FWD_EM_NUM_ROUTES);
391 }
392
393 #define BIT_16_TO_23 0x00ff0000
394 static inline void
395 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
396 {
397         uint32_t i;
398         int32_t ret;
399
400         mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
401                                 ALL_32_BITS, ALL_32_BITS} };
402
403         mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
404
405         for (i = 0; i < IPV6_L3FWD_EM_NUM_ROUTES; i++) {
406                 struct ipv6_l3fwd_em_route entry;
407                 union ipv6_5tuple_host newkey;
408
409                 entry = ipv6_l3fwd_em_route_array[i];
410                 convert_ipv6_5tuple(&entry.key, &newkey);
411                 ret = rte_hash_add_key(h, (void *) &newkey);
412                 if (ret < 0) {
413                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
414                                 " to the l3fwd hash.\n", i);
415                 }
416                 ipv6_l3fwd_out_if[ret] = entry.if_out;
417         }
418         printf("Hash: Adding 0x%" PRIx64 "keys\n",
419                 (uint64_t)IPV6_L3FWD_EM_NUM_ROUTES);
420 }
421
422 #define NUMBER_PORT_USED 4
423 static inline void
424 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
425                 unsigned int nr_flow)
426 {
427         unsigned i;
428
429         mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
430                                 ALL_32_BITS, ALL_32_BITS} };
431
432         for (i = 0; i < nr_flow; i++) {
433                 struct ipv4_l3fwd_em_route entry;
434                 union ipv4_5tuple_host newkey;
435
436                 uint8_t a = (uint8_t)
437                         ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
438                 uint8_t b = (uint8_t)
439                         (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
440                 uint8_t c = (uint8_t)
441                         ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
442
443                 /* Create the ipv4 exact match flow */
444                 memset(&entry, 0, sizeof(entry));
445                 switch (i & (NUMBER_PORT_USED - 1)) {
446                 case 0:
447                         entry = ipv4_l3fwd_em_route_array[0];
448                         entry.key.ip_dst = IPv4(101, c, b, a);
449                         break;
450                 case 1:
451                         entry = ipv4_l3fwd_em_route_array[1];
452                         entry.key.ip_dst = IPv4(201, c, b, a);
453                         break;
454                 case 2:
455                         entry = ipv4_l3fwd_em_route_array[2];
456                         entry.key.ip_dst = IPv4(111, c, b, a);
457                         break;
458                 case 3:
459                         entry = ipv4_l3fwd_em_route_array[3];
460                         entry.key.ip_dst = IPv4(211, c, b, a);
461                         break;
462                 };
463                 convert_ipv4_5tuple(&entry.key, &newkey);
464                 int32_t ret = rte_hash_add_key(h, (void *) &newkey);
465
466                 if (ret < 0)
467                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
468
469                 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
470
471         }
472         printf("Hash: Adding 0x%x keys\n", nr_flow);
473 }
474
475 static inline void
476 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
477                 unsigned int nr_flow)
478 {
479         unsigned i;
480
481         mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
482                                 ALL_32_BITS, ALL_32_BITS} };
483         mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
484
485         for (i = 0; i < nr_flow; i++) {
486                 struct ipv6_l3fwd_em_route entry;
487                 union ipv6_5tuple_host newkey;
488
489                 uint8_t a = (uint8_t)
490                         ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
491                 uint8_t b = (uint8_t)
492                         (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
493                 uint8_t c = (uint8_t)
494                         ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
495
496                 /* Create the ipv6 exact match flow */
497                 memset(&entry, 0, sizeof(entry));
498                 switch (i & (NUMBER_PORT_USED - 1)) {
499                 case 0:
500                         entry = ipv6_l3fwd_em_route_array[0];
501                         break;
502                 case 1:
503                         entry = ipv6_l3fwd_em_route_array[1];
504                         break;
505                 case 2:
506                         entry = ipv6_l3fwd_em_route_array[2];
507                         break;
508                 case 3:
509                         entry = ipv6_l3fwd_em_route_array[3];
510                         break;
511                 };
512                 entry.key.ip_dst[13] = c;
513                 entry.key.ip_dst[14] = b;
514                 entry.key.ip_dst[15] = a;
515                 convert_ipv6_5tuple(&entry.key, &newkey);
516                 int32_t ret = rte_hash_add_key(h, (void *) &newkey);
517
518                 if (ret < 0)
519                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
520
521                 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
522
523         }
524         printf("Hash: Adding 0x%x keys\n", nr_flow);
525 }
526
527 /* main processing loop */
528 int
529 em_main_loop(__attribute__((unused)) void *dummy)
530 {
531         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
532         unsigned lcore_id;
533         uint64_t prev_tsc, diff_tsc, cur_tsc;
534         int i, nb_rx;
535         uint8_t portid, queueid;
536         struct lcore_conf *qconf;
537         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
538                 US_PER_S * BURST_TX_DRAIN_US;
539
540         prev_tsc = 0;
541
542         lcore_id = rte_lcore_id();
543         qconf = &lcore_conf[lcore_id];
544
545         if (qconf->n_rx_queue == 0) {
546                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
547                 return 0;
548         }
549
550         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
551
552         for (i = 0; i < qconf->n_rx_queue; i++) {
553
554                 portid = qconf->rx_queue_list[i].port_id;
555                 queueid = qconf->rx_queue_list[i].queue_id;
556                 RTE_LOG(INFO, L3FWD,
557                         " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
558                         lcore_id, portid, queueid);
559         }
560
561         while (!force_quit) {
562
563                 cur_tsc = rte_rdtsc();
564
565                 /*
566                  * TX burst queue drain
567                  */
568                 diff_tsc = cur_tsc - prev_tsc;
569                 if (unlikely(diff_tsc > drain_tsc)) {
570
571                         for (i = 0; i < qconf->n_tx_port; ++i) {
572                                 portid = qconf->tx_port_id[i];
573                                 if (qconf->tx_mbufs[portid].len == 0)
574                                         continue;
575                                 send_burst(qconf,
576                                         qconf->tx_mbufs[portid].len,
577                                         portid);
578                                 qconf->tx_mbufs[portid].len = 0;
579                         }
580
581                         prev_tsc = cur_tsc;
582                 }
583
584                 /*
585                  * Read packet from RX queues
586                  */
587                 for (i = 0; i < qconf->n_rx_queue; ++i) {
588                         portid = qconf->rx_queue_list[i].port_id;
589                         queueid = qconf->rx_queue_list[i].queue_id;
590                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
591                                 MAX_PKT_BURST);
592                         if (nb_rx == 0)
593                                 continue;
594
595                         /*
596                          * For SSE4_1 use ENABLE_MULTI_BUFFER_OPTIMIZE=1
597                          * code.
598                          */
599 #if defined(__SSE4_1__)
600                         l3fwd_em_send_packets(nb_rx, pkts_burst,
601                                                         portid, qconf);
602 #else
603                         l3fwd_em_no_opt_send_packets(nb_rx, pkts_burst,
604                                                         portid, qconf);
605 #endif /* __SSE_4_1__ */
606                 }
607         }
608
609         return 0;
610 }
611
612 /*
613  * Initialize exact match (hash) parameters.
614  */
615 void
616 setup_hash(const int socketid)
617 {
618         struct rte_hash_parameters ipv4_l3fwd_hash_params = {
619                 .name = NULL,
620                 .entries = L3FWD_HASH_ENTRIES,
621                 .key_len = sizeof(union ipv4_5tuple_host),
622                 .hash_func = ipv4_hash_crc,
623                 .hash_func_init_val = 0,
624         };
625
626         struct rte_hash_parameters ipv6_l3fwd_hash_params = {
627                 .name = NULL,
628                 .entries = L3FWD_HASH_ENTRIES,
629                 .key_len = sizeof(union ipv6_5tuple_host),
630                 .hash_func = ipv6_hash_crc,
631                 .hash_func_init_val = 0,
632         };
633
634         char s[64];
635
636         /* create ipv4 hash */
637         snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
638         ipv4_l3fwd_hash_params.name = s;
639         ipv4_l3fwd_hash_params.socket_id = socketid;
640         ipv4_l3fwd_em_lookup_struct[socketid] =
641                 rte_hash_create(&ipv4_l3fwd_hash_params);
642         if (ipv4_l3fwd_em_lookup_struct[socketid] == NULL)
643                 rte_exit(EXIT_FAILURE,
644                         "Unable to create the l3fwd hash on socket %d\n",
645                         socketid);
646
647         /* create ipv6 hash */
648         snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
649         ipv6_l3fwd_hash_params.name = s;
650         ipv6_l3fwd_hash_params.socket_id = socketid;
651         ipv6_l3fwd_em_lookup_struct[socketid] =
652                 rte_hash_create(&ipv6_l3fwd_hash_params);
653         if (ipv6_l3fwd_em_lookup_struct[socketid] == NULL)
654                 rte_exit(EXIT_FAILURE,
655                         "Unable to create the l3fwd hash on socket %d\n",
656                         socketid);
657
658         if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
659                 /* For testing hash matching with a large number of flows we
660                  * generate millions of IP 5-tuples with an incremented dst
661                  * address to initialize the hash table. */
662                 if (ipv6 == 0) {
663                         /* populate the ipv4 hash */
664                         populate_ipv4_many_flow_into_table(
665                                 ipv4_l3fwd_em_lookup_struct[socketid],
666                                 hash_entry_number);
667                 } else {
668                         /* populate the ipv6 hash */
669                         populate_ipv6_many_flow_into_table(
670                                 ipv6_l3fwd_em_lookup_struct[socketid],
671                                 hash_entry_number);
672                 }
673         } else {
674                 /*
675                  * Use data in ipv4/ipv6 l3fwd lookup table
676                  * directly to initialize the hash table.
677                  */
678                 if (ipv6 == 0) {
679                         /* populate the ipv4 hash */
680                         populate_ipv4_few_flow_into_table(
681                                 ipv4_l3fwd_em_lookup_struct[socketid]);
682                 } else {
683                         /* populate the ipv6 hash */
684                         populate_ipv6_few_flow_into_table(
685                                 ipv6_l3fwd_em_lookup_struct[socketid]);
686                 }
687         }
688 }
689
690 /* Return ipv4/ipv6 em fwd lookup struct. */
691 void *
692 em_get_ipv4_l3fwd_lookup_struct(const int socketid)
693 {
694         return ipv4_l3fwd_em_lookup_struct[socketid];
695 }
696
697 void *
698 em_get_ipv6_l3fwd_lookup_struct(const int socketid)
699 {
700         return ipv6_l3fwd_em_lookup_struct[socketid];
701 }