1a8bc91681d1fc5bcea3f3af4b0dbc18e0463c1a
[dpdk.git] / examples / l3fwd / l3fwd_em.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <sys/queue.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <getopt.h>
15 #include <stdbool.h>
16 #include <netinet/in.h>
17
18 #include <rte_debug.h>
19 #include <rte_ether.h>
20 #include <rte_ethdev.h>
21 #include <rte_cycles.h>
22 #include <rte_mbuf.h>
23 #include <rte_ip.h>
24 #include <rte_tcp.h>
25 #include <rte_udp.h>
26 #include <rte_hash.h>
27
28 #include "l3fwd.h"
29 #include "l3fwd_event.h"
30
31 #if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_CRC32)
32 #define EM_HASH_CRC 1
33 #endif
34
35 #ifdef EM_HASH_CRC
36 #include <rte_hash_crc.h>
37 #define DEFAULT_HASH_FUNC       rte_hash_crc
38 #else
39 #include <rte_jhash.h>
40 #define DEFAULT_HASH_FUNC       rte_jhash
41 #endif
42
43 #define IPV6_ADDR_LEN 16
44
45 struct ipv4_5tuple {
46         uint32_t ip_dst;
47         uint32_t ip_src;
48         uint16_t port_dst;
49         uint16_t port_src;
50         uint8_t  proto;
51 } __attribute__((__packed__));
52
53 union ipv4_5tuple_host {
54         struct {
55                 uint8_t  pad0;
56                 uint8_t  proto;
57                 uint16_t pad1;
58                 uint32_t ip_src;
59                 uint32_t ip_dst;
60                 uint16_t port_src;
61                 uint16_t port_dst;
62         };
63         xmm_t xmm;
64 };
65
66 #define XMM_NUM_IN_IPV6_5TUPLE 3
67
68 struct ipv6_5tuple {
69         uint8_t  ip_dst[IPV6_ADDR_LEN];
70         uint8_t  ip_src[IPV6_ADDR_LEN];
71         uint16_t port_dst;
72         uint16_t port_src;
73         uint8_t  proto;
74 } __attribute__((__packed__));
75
76 union ipv6_5tuple_host {
77         struct {
78                 uint16_t pad0;
79                 uint8_t  proto;
80                 uint8_t  pad1;
81                 uint8_t  ip_src[IPV6_ADDR_LEN];
82                 uint8_t  ip_dst[IPV6_ADDR_LEN];
83                 uint16_t port_src;
84                 uint16_t port_dst;
85                 uint64_t reserve;
86         };
87         xmm_t xmm[XMM_NUM_IN_IPV6_5TUPLE];
88 };
89
90
91
92 struct ipv4_l3fwd_em_route {
93         struct ipv4_5tuple key;
94         uint8_t if_out;
95 };
96
97 struct ipv6_l3fwd_em_route {
98         struct ipv6_5tuple key;
99         uint8_t if_out;
100 };
101
102 static struct ipv4_l3fwd_em_route ipv4_l3fwd_em_route_array[] = {
103         {{RTE_IPV4(101, 0, 0, 0), RTE_IPV4(100, 10, 0, 1),  101, 11, IPPROTO_TCP}, 0},
104         {{RTE_IPV4(201, 0, 0, 0), RTE_IPV4(200, 20, 0, 1),  102, 12, IPPROTO_TCP}, 1},
105         {{RTE_IPV4(111, 0, 0, 0), RTE_IPV4(100, 30, 0, 1),  101, 11, IPPROTO_TCP}, 2},
106         {{RTE_IPV4(211, 0, 0, 0), RTE_IPV4(200, 40, 0, 1),  102, 12, IPPROTO_TCP}, 3},
107 };
108
109 static struct ipv6_l3fwd_em_route ipv6_l3fwd_em_route_array[] = {
110         {{
111         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
112         {0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
113         101, 11, IPPROTO_TCP}, 0},
114
115         {{
116         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
117         {0xfe, 0x90, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
118         102, 12, IPPROTO_TCP}, 1},
119
120         {{
121         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
122         {0xfe, 0xa0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
123         101, 11, IPPROTO_TCP}, 2},
124
125         {{
126         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1e, 0x67, 0xff, 0xfe, 0, 0, 0},
127         {0xfe, 0xb0, 0, 0, 0, 0, 0, 0, 0x02, 0x1b, 0x21, 0xff, 0xfe, 0x91, 0x38, 0x05},
128         102, 12, IPPROTO_TCP}, 3},
129 };
130
131 struct rte_hash *ipv4_l3fwd_em_lookup_struct[NB_SOCKETS];
132 struct rte_hash *ipv6_l3fwd_em_lookup_struct[NB_SOCKETS];
133
134 static inline uint32_t
135 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
136                 uint32_t init_val)
137 {
138         const union ipv4_5tuple_host *k;
139         uint32_t t;
140         const uint32_t *p;
141
142         k = data;
143         t = k->proto;
144         p = (const uint32_t *)&k->port_src;
145
146 #ifdef EM_HASH_CRC
147         init_val = rte_hash_crc_4byte(t, init_val);
148         init_val = rte_hash_crc_4byte(k->ip_src, init_val);
149         init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
150         init_val = rte_hash_crc_4byte(*p, init_val);
151 #else
152         init_val = rte_jhash_1word(t, init_val);
153         init_val = rte_jhash_1word(k->ip_src, init_val);
154         init_val = rte_jhash_1word(k->ip_dst, init_val);
155         init_val = rte_jhash_1word(*p, init_val);
156 #endif
157
158         return init_val;
159 }
160
161 static inline uint32_t
162 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
163                 uint32_t init_val)
164 {
165         const union ipv6_5tuple_host *k;
166         uint32_t t;
167         const uint32_t *p;
168 #ifdef EM_HASH_CRC
169         const uint32_t  *ip_src0, *ip_src1, *ip_src2, *ip_src3;
170         const uint32_t  *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
171 #endif
172
173         k = data;
174         t = k->proto;
175         p = (const uint32_t *)&k->port_src;
176
177 #ifdef EM_HASH_CRC
178         ip_src0 = (const uint32_t *) k->ip_src;
179         ip_src1 = (const uint32_t *)(k->ip_src+4);
180         ip_src2 = (const uint32_t *)(k->ip_src+8);
181         ip_src3 = (const uint32_t *)(k->ip_src+12);
182         ip_dst0 = (const uint32_t *) k->ip_dst;
183         ip_dst1 = (const uint32_t *)(k->ip_dst+4);
184         ip_dst2 = (const uint32_t *)(k->ip_dst+8);
185         ip_dst3 = (const uint32_t *)(k->ip_dst+12);
186         init_val = rte_hash_crc_4byte(t, init_val);
187         init_val = rte_hash_crc_4byte(*ip_src0, init_val);
188         init_val = rte_hash_crc_4byte(*ip_src1, init_val);
189         init_val = rte_hash_crc_4byte(*ip_src2, init_val);
190         init_val = rte_hash_crc_4byte(*ip_src3, init_val);
191         init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
192         init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
193         init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
194         init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
195         init_val = rte_hash_crc_4byte(*p, init_val);
196 #else
197         init_val = rte_jhash_1word(t, init_val);
198         init_val = rte_jhash(k->ip_src,
199                         sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
200         init_val = rte_jhash(k->ip_dst,
201                         sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
202         init_val = rte_jhash_1word(*p, init_val);
203 #endif
204         return init_val;
205 }
206
207 #define IPV4_L3FWD_EM_NUM_ROUTES \
208         (sizeof(ipv4_l3fwd_em_route_array) / sizeof(ipv4_l3fwd_em_route_array[0]))
209
210 #define IPV6_L3FWD_EM_NUM_ROUTES \
211         (sizeof(ipv6_l3fwd_em_route_array) / sizeof(ipv6_l3fwd_em_route_array[0]))
212
213 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
214 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
215
216 static rte_xmm_t mask0;
217 static rte_xmm_t mask1;
218 static rte_xmm_t mask2;
219
220 #if defined(RTE_MACHINE_CPUFLAG_SSE2)
221 static inline xmm_t
222 em_mask_key(void *key, xmm_t mask)
223 {
224         __m128i data = _mm_loadu_si128((__m128i *)(key));
225
226         return _mm_and_si128(data, mask);
227 }
228 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
229 static inline xmm_t
230 em_mask_key(void *key, xmm_t mask)
231 {
232         int32x4_t data = vld1q_s32((int32_t *)key);
233
234         return vandq_s32(data, mask);
235 }
236 #elif defined(RTE_MACHINE_CPUFLAG_ALTIVEC)
237 static inline xmm_t
238 em_mask_key(void *key, xmm_t mask)
239 {
240         xmm_t data = vec_ld(0, (xmm_t *)(key));
241
242         return vec_and(data, mask);
243 }
244 #else
245 #error No vector engine (SSE, NEON, ALTIVEC) available, check your toolchain
246 #endif
247
248 static inline uint16_t
249 em_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
250 {
251         int ret = 0;
252         union ipv4_5tuple_host key;
253         struct rte_hash *ipv4_l3fwd_lookup_struct =
254                 (struct rte_hash *)lookup_struct;
255
256         ipv4_hdr = (uint8_t *)ipv4_hdr +
257                 offsetof(struct rte_ipv4_hdr, time_to_live);
258
259         /*
260          * Get 5 tuple: dst port, src port, dst IP address,
261          * src IP address and protocol.
262          */
263         key.xmm = em_mask_key(ipv4_hdr, mask0.x);
264
265         /* Find destination port */
266         ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
267         return (ret < 0) ? portid : ipv4_l3fwd_out_if[ret];
268 }
269
270 static inline uint16_t
271 em_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
272 {
273         int ret = 0;
274         union ipv6_5tuple_host key;
275         struct rte_hash *ipv6_l3fwd_lookup_struct =
276                 (struct rte_hash *)lookup_struct;
277
278         ipv6_hdr = (uint8_t *)ipv6_hdr +
279                 offsetof(struct rte_ipv6_hdr, payload_len);
280         void *data0 = ipv6_hdr;
281         void *data1 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t);
282         void *data2 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t) + sizeof(xmm_t);
283
284         /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
285         key.xmm[0] = em_mask_key(data0, mask1.x);
286
287         /*
288          * Get part of 5 tuple: dst IP address lower 96 bits
289          * and src IP address higher 32 bits.
290          */
291 #if defined RTE_ARCH_X86
292         key.xmm[1] = _mm_loadu_si128(data1);
293 #else
294         key.xmm[1] = *(xmm_t *)data1;
295 #endif
296
297         /*
298          * Get part of 5 tuple: dst port and src port
299          * and dst IP address higher 32 bits.
300          */
301         key.xmm[2] = em_mask_key(data2, mask2.x);
302
303         /* Find destination port */
304         ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
305         return (ret < 0) ? portid : ipv6_l3fwd_out_if[ret];
306 }
307
308 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON
309 #if defined(NO_HASH_MULTI_LOOKUP)
310 #include "l3fwd_em_sequential.h"
311 #else
312 #include "l3fwd_em_hlm.h"
313 #endif
314 #else
315 #include "l3fwd_em.h"
316 #endif
317
318 static void
319 convert_ipv4_5tuple(struct ipv4_5tuple *key1,
320                 union ipv4_5tuple_host *key2)
321 {
322         key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
323         key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
324         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
325         key2->port_src = rte_cpu_to_be_16(key1->port_src);
326         key2->proto = key1->proto;
327         key2->pad0 = 0;
328         key2->pad1 = 0;
329 }
330
331 static void
332 convert_ipv6_5tuple(struct ipv6_5tuple *key1,
333                 union ipv6_5tuple_host *key2)
334 {
335         uint32_t i;
336
337         for (i = 0; i < 16; i++) {
338                 key2->ip_dst[i] = key1->ip_dst[i];
339                 key2->ip_src[i] = key1->ip_src[i];
340         }
341         key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
342         key2->port_src = rte_cpu_to_be_16(key1->port_src);
343         key2->proto = key1->proto;
344         key2->pad0 = 0;
345         key2->pad1 = 0;
346         key2->reserve = 0;
347 }
348
349 #define BYTE_VALUE_MAX 256
350 #define ALL_32_BITS 0xffffffff
351 #define BIT_8_TO_15 0x0000ff00
352
353 static inline void
354 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
355 {
356         uint32_t i;
357         int32_t ret;
358
359         mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
360                                 ALL_32_BITS, ALL_32_BITS} };
361
362         for (i = 0; i < IPV4_L3FWD_EM_NUM_ROUTES; i++) {
363                 struct ipv4_l3fwd_em_route  entry;
364                 union ipv4_5tuple_host newkey;
365
366                 entry = ipv4_l3fwd_em_route_array[i];
367                 convert_ipv4_5tuple(&entry.key, &newkey);
368                 ret = rte_hash_add_key(h, (void *) &newkey);
369                 if (ret < 0) {
370                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
371                                 " to the l3fwd hash.\n", i);
372                 }
373                 ipv4_l3fwd_out_if[ret] = entry.if_out;
374         }
375         printf("Hash: Adding 0x%" PRIx64 " keys\n",
376                 (uint64_t)IPV4_L3FWD_EM_NUM_ROUTES);
377 }
378
379 #define BIT_16_TO_23 0x00ff0000
380 static inline void
381 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
382 {
383         uint32_t i;
384         int32_t ret;
385
386         mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
387                                 ALL_32_BITS, ALL_32_BITS} };
388
389         mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
390
391         for (i = 0; i < IPV6_L3FWD_EM_NUM_ROUTES; i++) {
392                 struct ipv6_l3fwd_em_route entry;
393                 union ipv6_5tuple_host newkey;
394
395                 entry = ipv6_l3fwd_em_route_array[i];
396                 convert_ipv6_5tuple(&entry.key, &newkey);
397                 ret = rte_hash_add_key(h, (void *) &newkey);
398                 if (ret < 0) {
399                         rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
400                                 " to the l3fwd hash.\n", i);
401                 }
402                 ipv6_l3fwd_out_if[ret] = entry.if_out;
403         }
404         printf("Hash: Adding 0x%" PRIx64 "keys\n",
405                 (uint64_t)IPV6_L3FWD_EM_NUM_ROUTES);
406 }
407
408 #define NUMBER_PORT_USED 4
409 static inline void
410 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
411                 unsigned int nr_flow)
412 {
413         unsigned i;
414
415         mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
416                                 ALL_32_BITS, ALL_32_BITS} };
417
418         for (i = 0; i < nr_flow; i++) {
419                 struct ipv4_l3fwd_em_route entry;
420                 union ipv4_5tuple_host newkey;
421
422                 uint8_t a = (uint8_t)
423                         ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
424                 uint8_t b = (uint8_t)
425                         (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
426                 uint8_t c = (uint8_t)
427                         ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
428
429                 /* Create the ipv4 exact match flow */
430                 memset(&entry, 0, sizeof(entry));
431                 switch (i & (NUMBER_PORT_USED - 1)) {
432                 case 0:
433                         entry = ipv4_l3fwd_em_route_array[0];
434                         entry.key.ip_dst = RTE_IPV4(101, c, b, a);
435                         break;
436                 case 1:
437                         entry = ipv4_l3fwd_em_route_array[1];
438                         entry.key.ip_dst = RTE_IPV4(201, c, b, a);
439                         break;
440                 case 2:
441                         entry = ipv4_l3fwd_em_route_array[2];
442                         entry.key.ip_dst = RTE_IPV4(111, c, b, a);
443                         break;
444                 case 3:
445                         entry = ipv4_l3fwd_em_route_array[3];
446                         entry.key.ip_dst = RTE_IPV4(211, c, b, a);
447                         break;
448                 };
449                 convert_ipv4_5tuple(&entry.key, &newkey);
450                 int32_t ret = rte_hash_add_key(h, (void *) &newkey);
451
452                 if (ret < 0)
453                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
454
455                 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
456
457         }
458         printf("Hash: Adding 0x%x keys\n", nr_flow);
459 }
460
461 static inline void
462 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
463                 unsigned int nr_flow)
464 {
465         unsigned i;
466
467         mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
468                                 ALL_32_BITS, ALL_32_BITS} };
469         mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
470
471         for (i = 0; i < nr_flow; i++) {
472                 struct ipv6_l3fwd_em_route entry;
473                 union ipv6_5tuple_host newkey;
474
475                 uint8_t a = (uint8_t)
476                         ((i/NUMBER_PORT_USED)%BYTE_VALUE_MAX);
477                 uint8_t b = (uint8_t)
478                         (((i/NUMBER_PORT_USED)/BYTE_VALUE_MAX)%BYTE_VALUE_MAX);
479                 uint8_t c = (uint8_t)
480                         ((i/NUMBER_PORT_USED)/(BYTE_VALUE_MAX*BYTE_VALUE_MAX));
481
482                 /* Create the ipv6 exact match flow */
483                 memset(&entry, 0, sizeof(entry));
484                 switch (i & (NUMBER_PORT_USED - 1)) {
485                 case 0:
486                         entry = ipv6_l3fwd_em_route_array[0];
487                         break;
488                 case 1:
489                         entry = ipv6_l3fwd_em_route_array[1];
490                         break;
491                 case 2:
492                         entry = ipv6_l3fwd_em_route_array[2];
493                         break;
494                 case 3:
495                         entry = ipv6_l3fwd_em_route_array[3];
496                         break;
497                 };
498                 entry.key.ip_dst[13] = c;
499                 entry.key.ip_dst[14] = b;
500                 entry.key.ip_dst[15] = a;
501                 convert_ipv6_5tuple(&entry.key, &newkey);
502                 int32_t ret = rte_hash_add_key(h, (void *) &newkey);
503
504                 if (ret < 0)
505                         rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
506
507                 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
508
509         }
510         printf("Hash: Adding 0x%x keys\n", nr_flow);
511 }
512
513 /* Requirements:
514  * 1. IP packets without extension;
515  * 2. L4 payload should be either TCP or UDP.
516  */
517 int
518 em_check_ptype(int portid)
519 {
520         int i, ret;
521         int ptype_l3_ipv4_ext = 0;
522         int ptype_l3_ipv6_ext = 0;
523         int ptype_l4_tcp = 0;
524         int ptype_l4_udp = 0;
525         uint32_t ptype_mask = RTE_PTYPE_L3_MASK | RTE_PTYPE_L4_MASK;
526
527         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
528         if (ret <= 0)
529                 return 0;
530
531         uint32_t ptypes[ret];
532
533         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
534         for (i = 0; i < ret; ++i) {
535                 switch (ptypes[i]) {
536                 case RTE_PTYPE_L3_IPV4_EXT:
537                         ptype_l3_ipv4_ext = 1;
538                         break;
539                 case RTE_PTYPE_L3_IPV6_EXT:
540                         ptype_l3_ipv6_ext = 1;
541                         break;
542                 case RTE_PTYPE_L4_TCP:
543                         ptype_l4_tcp = 1;
544                         break;
545                 case RTE_PTYPE_L4_UDP:
546                         ptype_l4_udp = 1;
547                         break;
548                 }
549         }
550
551         if (ptype_l3_ipv4_ext == 0)
552                 printf("port %d cannot parse RTE_PTYPE_L3_IPV4_EXT\n", portid);
553         if (ptype_l3_ipv6_ext == 0)
554                 printf("port %d cannot parse RTE_PTYPE_L3_IPV6_EXT\n", portid);
555         if (!ptype_l3_ipv4_ext || !ptype_l3_ipv6_ext)
556                 return 0;
557
558         if (ptype_l4_tcp == 0)
559                 printf("port %d cannot parse RTE_PTYPE_L4_TCP\n", portid);
560         if (ptype_l4_udp == 0)
561                 printf("port %d cannot parse RTE_PTYPE_L4_UDP\n", portid);
562         if (ptype_l4_tcp && ptype_l4_udp)
563                 return 1;
564
565         return 0;
566 }
567
568 static inline void
569 em_parse_ptype(struct rte_mbuf *m)
570 {
571         struct rte_ether_hdr *eth_hdr;
572         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
573         uint16_t ether_type;
574         void *l3;
575         int hdr_len;
576         struct rte_ipv4_hdr *ipv4_hdr;
577         struct rte_ipv6_hdr *ipv6_hdr;
578
579         eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
580         ether_type = eth_hdr->ether_type;
581         l3 = (uint8_t *)eth_hdr + sizeof(struct rte_ether_hdr);
582         if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
583                 ipv4_hdr = (struct rte_ipv4_hdr *)l3;
584                 hdr_len = (ipv4_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
585                           RTE_IPV4_IHL_MULTIPLIER;
586                 if (hdr_len == sizeof(struct rte_ipv4_hdr)) {
587                         packet_type |= RTE_PTYPE_L3_IPV4;
588                         if (ipv4_hdr->next_proto_id == IPPROTO_TCP)
589                                 packet_type |= RTE_PTYPE_L4_TCP;
590                         else if (ipv4_hdr->next_proto_id == IPPROTO_UDP)
591                                 packet_type |= RTE_PTYPE_L4_UDP;
592                 } else
593                         packet_type |= RTE_PTYPE_L3_IPV4_EXT;
594         } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
595                 ipv6_hdr = (struct rte_ipv6_hdr *)l3;
596                 if (ipv6_hdr->proto == IPPROTO_TCP)
597                         packet_type |= RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_TCP;
598                 else if (ipv6_hdr->proto == IPPROTO_UDP)
599                         packet_type |= RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_UDP;
600                 else
601                         packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
602         }
603
604         m->packet_type = packet_type;
605 }
606
607 uint16_t
608 em_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
609                   struct rte_mbuf *pkts[], uint16_t nb_pkts,
610                   uint16_t max_pkts __rte_unused,
611                   void *user_param __rte_unused)
612 {
613         unsigned i;
614
615         for (i = 0; i < nb_pkts; ++i)
616                 em_parse_ptype(pkts[i]);
617
618         return nb_pkts;
619 }
620
621 /* main processing loop */
622 int
623 em_main_loop(__attribute__((unused)) void *dummy)
624 {
625         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
626         unsigned lcore_id;
627         uint64_t prev_tsc, diff_tsc, cur_tsc;
628         int i, nb_rx;
629         uint8_t queueid;
630         uint16_t portid;
631         struct lcore_conf *qconf;
632         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
633                 US_PER_S * BURST_TX_DRAIN_US;
634
635         prev_tsc = 0;
636
637         lcore_id = rte_lcore_id();
638         qconf = &lcore_conf[lcore_id];
639
640         if (qconf->n_rx_queue == 0) {
641                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
642                 return 0;
643         }
644
645         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
646
647         for (i = 0; i < qconf->n_rx_queue; i++) {
648
649                 portid = qconf->rx_queue_list[i].port_id;
650                 queueid = qconf->rx_queue_list[i].queue_id;
651                 RTE_LOG(INFO, L3FWD,
652                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
653                         lcore_id, portid, queueid);
654         }
655
656         while (!force_quit) {
657
658                 cur_tsc = rte_rdtsc();
659
660                 /*
661                  * TX burst queue drain
662                  */
663                 diff_tsc = cur_tsc - prev_tsc;
664                 if (unlikely(diff_tsc > drain_tsc)) {
665
666                         for (i = 0; i < qconf->n_tx_port; ++i) {
667                                 portid = qconf->tx_port_id[i];
668                                 if (qconf->tx_mbufs[portid].len == 0)
669                                         continue;
670                                 send_burst(qconf,
671                                         qconf->tx_mbufs[portid].len,
672                                         portid);
673                                 qconf->tx_mbufs[portid].len = 0;
674                         }
675
676                         prev_tsc = cur_tsc;
677                 }
678
679                 /*
680                  * Read packet from RX queues
681                  */
682                 for (i = 0; i < qconf->n_rx_queue; ++i) {
683                         portid = qconf->rx_queue_list[i].port_id;
684                         queueid = qconf->rx_queue_list[i].queue_id;
685                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
686                                 MAX_PKT_BURST);
687                         if (nb_rx == 0)
688                                 continue;
689
690 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON
691                         l3fwd_em_send_packets(nb_rx, pkts_burst,
692                                                         portid, qconf);
693 #else
694                         l3fwd_em_no_opt_send_packets(nb_rx, pkts_burst,
695                                                         portid, qconf);
696 #endif
697                 }
698         }
699
700         return 0;
701 }
702
703 static __rte_always_inline void
704 em_event_loop_single(struct l3fwd_event_resources *evt_rsrc,
705                 const uint8_t flags)
706 {
707         const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
708         const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
709                 evt_rsrc->evq.nb_queues - 1];
710         const uint8_t event_d_id = evt_rsrc->event_d_id;
711         struct lcore_conf *lconf;
712         unsigned int lcore_id;
713         struct rte_event ev;
714
715         if (event_p_id < 0)
716                 return;
717
718         lcore_id = rte_lcore_id();
719         lconf = &lcore_conf[lcore_id];
720
721         RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
722         while (!force_quit) {
723                 if (!rte_event_dequeue_burst(event_d_id, event_p_id, &ev, 1, 0))
724                         continue;
725
726                 struct rte_mbuf *mbuf = ev.mbuf;
727
728 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON
729                 mbuf->port = em_get_dst_port(lconf, mbuf, mbuf->port);
730                 process_packet(mbuf, &mbuf->port);
731 #else
732                 l3fwd_em_simple_process(mbuf, lconf);
733 #endif
734                 if (mbuf->port == BAD_PORT) {
735                         rte_pktmbuf_free(mbuf);
736                         continue;
737                 }
738
739                 if (flags & L3FWD_EVENT_TX_ENQ) {
740                         ev.queue_id = tx_q_id;
741                         ev.op = RTE_EVENT_OP_FORWARD;
742                         while (rte_event_enqueue_burst(event_d_id, event_p_id,
743                                                 &ev, 1) && !force_quit)
744                                 ;
745                 }
746
747                 if (flags & L3FWD_EVENT_TX_DIRECT) {
748                         rte_event_eth_tx_adapter_txq_set(mbuf, 0);
749                         while (!rte_event_eth_tx_adapter_enqueue(event_d_id,
750                                                 event_p_id, &ev, 1, 0) &&
751                                         !force_quit)
752                                 ;
753                 }
754         }
755 }
756
757 static __rte_always_inline void
758 em_event_loop_burst(struct l3fwd_event_resources *evt_rsrc,
759                 const uint8_t flags)
760 {
761         const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
762         const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
763                 evt_rsrc->evq.nb_queues - 1];
764         const uint8_t event_d_id = evt_rsrc->event_d_id;
765         const uint16_t deq_len = evt_rsrc->deq_depth;
766         struct rte_event events[MAX_PKT_BURST];
767         struct lcore_conf *lconf;
768         unsigned int lcore_id;
769         int i, nb_enq, nb_deq;
770
771         if (event_p_id < 0)
772                 return;
773
774         lcore_id = rte_lcore_id();
775
776         lconf = &lcore_conf[lcore_id];
777
778         RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
779
780         while (!force_quit) {
781                 /* Read events from RX queues */
782                 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
783                                 events, deq_len, 0);
784                 if (nb_deq == 0) {
785                         rte_pause();
786                         continue;
787                 }
788
789 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON
790                 l3fwd_em_process_events(nb_deq, (struct rte_event **)&events,
791                                         lconf);
792 #else
793                 l3fwd_em_no_opt_process_events(nb_deq,
794                                                (struct rte_event **)&events,
795                                                lconf);
796 #endif
797                 for (i = 0; i < nb_deq; i++) {
798                         if (flags & L3FWD_EVENT_TX_ENQ) {
799                                 events[i].queue_id = tx_q_id;
800                                 events[i].op = RTE_EVENT_OP_FORWARD;
801                         }
802
803                         if (flags & L3FWD_EVENT_TX_DIRECT)
804                                 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
805                                                                  0);
806                 }
807
808                 if (flags & L3FWD_EVENT_TX_ENQ) {
809                         nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
810                                         events, nb_deq);
811                         while (nb_enq < nb_deq && !force_quit)
812                                 nb_enq += rte_event_enqueue_burst(event_d_id,
813                                                 event_p_id, events + nb_enq,
814                                                 nb_deq - nb_enq);
815                 }
816
817                 if (flags & L3FWD_EVENT_TX_DIRECT) {
818                         nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
819                                         event_p_id, events, nb_deq, 0);
820                         while (nb_enq < nb_deq && !force_quit)
821                                 nb_enq += rte_event_eth_tx_adapter_enqueue(
822                                                 event_d_id, event_p_id,
823                                                 events + nb_enq,
824                                                 nb_deq - nb_enq, 0);
825                 }
826         }
827 }
828
829 static __rte_always_inline void
830 em_event_loop(struct l3fwd_event_resources *evt_rsrc,
831                  const uint8_t flags)
832 {
833         if (flags & L3FWD_EVENT_SINGLE)
834                 em_event_loop_single(evt_rsrc, flags);
835         if (flags & L3FWD_EVENT_BURST)
836                 em_event_loop_burst(evt_rsrc, flags);
837 }
838
839 int __rte_noinline
840 em_event_main_loop_tx_d(__attribute__((unused)) void *dummy)
841 {
842         struct l3fwd_event_resources *evt_rsrc =
843                                         l3fwd_get_eventdev_rsrc();
844
845         em_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_SINGLE);
846         return 0;
847 }
848
849 int __rte_noinline
850 em_event_main_loop_tx_d_burst(__attribute__((unused)) void *dummy)
851 {
852         struct l3fwd_event_resources *evt_rsrc =
853                                         l3fwd_get_eventdev_rsrc();
854
855         em_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_BURST);
856         return 0;
857 }
858
859 int __rte_noinline
860 em_event_main_loop_tx_q(__attribute__((unused)) void *dummy)
861 {
862         struct l3fwd_event_resources *evt_rsrc =
863                                         l3fwd_get_eventdev_rsrc();
864
865         em_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_SINGLE);
866         return 0;
867 }
868
869 int __rte_noinline
870 em_event_main_loop_tx_q_burst(__attribute__((unused)) void *dummy)
871 {
872         struct l3fwd_event_resources *evt_rsrc =
873                                         l3fwd_get_eventdev_rsrc();
874
875         em_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_BURST);
876         return 0;
877 }
878
879 /*
880  * Initialize exact match (hash) parameters.
881  */
882 void
883 setup_hash(const int socketid)
884 {
885         struct rte_hash_parameters ipv4_l3fwd_hash_params = {
886                 .name = NULL,
887                 .entries = L3FWD_HASH_ENTRIES,
888                 .key_len = sizeof(union ipv4_5tuple_host),
889                 .hash_func = ipv4_hash_crc,
890                 .hash_func_init_val = 0,
891         };
892
893         struct rte_hash_parameters ipv6_l3fwd_hash_params = {
894                 .name = NULL,
895                 .entries = L3FWD_HASH_ENTRIES,
896                 .key_len = sizeof(union ipv6_5tuple_host),
897                 .hash_func = ipv6_hash_crc,
898                 .hash_func_init_val = 0,
899         };
900
901         char s[64];
902
903         /* create ipv4 hash */
904         snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
905         ipv4_l3fwd_hash_params.name = s;
906         ipv4_l3fwd_hash_params.socket_id = socketid;
907         ipv4_l3fwd_em_lookup_struct[socketid] =
908                 rte_hash_create(&ipv4_l3fwd_hash_params);
909         if (ipv4_l3fwd_em_lookup_struct[socketid] == NULL)
910                 rte_exit(EXIT_FAILURE,
911                         "Unable to create the l3fwd hash on socket %d\n",
912                         socketid);
913
914         /* create ipv6 hash */
915         snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
916         ipv6_l3fwd_hash_params.name = s;
917         ipv6_l3fwd_hash_params.socket_id = socketid;
918         ipv6_l3fwd_em_lookup_struct[socketid] =
919                 rte_hash_create(&ipv6_l3fwd_hash_params);
920         if (ipv6_l3fwd_em_lookup_struct[socketid] == NULL)
921                 rte_exit(EXIT_FAILURE,
922                         "Unable to create the l3fwd hash on socket %d\n",
923                         socketid);
924
925         if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
926                 /* For testing hash matching with a large number of flows we
927                  * generate millions of IP 5-tuples with an incremented dst
928                  * address to initialize the hash table. */
929                 if (ipv6 == 0) {
930                         /* populate the ipv4 hash */
931                         populate_ipv4_many_flow_into_table(
932                                 ipv4_l3fwd_em_lookup_struct[socketid],
933                                 hash_entry_number);
934                 } else {
935                         /* populate the ipv6 hash */
936                         populate_ipv6_many_flow_into_table(
937                                 ipv6_l3fwd_em_lookup_struct[socketid],
938                                 hash_entry_number);
939                 }
940         } else {
941                 /*
942                  * Use data in ipv4/ipv6 l3fwd lookup table
943                  * directly to initialize the hash table.
944                  */
945                 if (ipv6 == 0) {
946                         /* populate the ipv4 hash */
947                         populate_ipv4_few_flow_into_table(
948                                 ipv4_l3fwd_em_lookup_struct[socketid]);
949                 } else {
950                         /* populate the ipv6 hash */
951                         populate_ipv6_few_flow_into_table(
952                                 ipv6_l3fwd_em_lookup_struct[socketid]);
953                 }
954         }
955 }
956
957 /* Return ipv4/ipv6 em fwd lookup struct. */
958 void *
959 em_get_ipv4_l3fwd_lookup_struct(const int socketid)
960 {
961         return ipv4_l3fwd_em_lookup_struct[socketid];
962 }
963
964 void *
965 em_get_ipv6_l3fwd_lookup_struct(const int socketid)
966 {
967         return ipv6_l3fwd_em_lookup_struct[socketid];
968 }