1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/queue.h>
16 #include <netinet/in.h>
18 #include <rte_debug.h>
19 #include <rte_ether.h>
20 #include <rte_ethdev.h>
21 #include <rte_cycles.h>
29 #include "l3fwd_event.h"
30 #include "em_route_parse.c"
32 #if defined(RTE_ARCH_X86) || defined(__ARM_FEATURE_CRC32)
37 #include <rte_hash_crc.h>
38 #define DEFAULT_HASH_FUNC rte_hash_crc
40 #include <rte_jhash.h>
41 #define DEFAULT_HASH_FUNC rte_jhash
44 #define IPV6_ADDR_LEN 16
46 union ipv4_5tuple_host {
59 #define XMM_NUM_IN_IPV6_5TUPLE 3
61 union ipv6_5tuple_host {
66 uint8_t ip_src[IPV6_ADDR_LEN];
67 uint8_t ip_dst[IPV6_ADDR_LEN];
72 xmm_t xmm[XMM_NUM_IN_IPV6_5TUPLE];
75 /* 198.18.0.0/16 are set aside for RFC2544 benchmarking (RFC5735).
76 * Use RFC863 Discard Protocol.
78 const struct ipv4_l3fwd_em_route ipv4_l3fwd_em_route_array[] = {
79 {{RTE_IPV4(198, 18, 0, 0), RTE_IPV4(198, 18, 0, 1), 9, 9, IPPROTO_UDP}, 0},
80 {{RTE_IPV4(198, 18, 1, 0), RTE_IPV4(198, 18, 1, 1), 9, 9, IPPROTO_UDP}, 1},
81 {{RTE_IPV4(198, 18, 2, 0), RTE_IPV4(198, 18, 2, 1), 9, 9, IPPROTO_UDP}, 2},
82 {{RTE_IPV4(198, 18, 3, 0), RTE_IPV4(198, 18, 3, 1), 9, 9, IPPROTO_UDP}, 3},
83 {{RTE_IPV4(198, 18, 4, 0), RTE_IPV4(198, 18, 4, 1), 9, 9, IPPROTO_UDP}, 4},
84 {{RTE_IPV4(198, 18, 5, 0), RTE_IPV4(198, 18, 5, 1), 9, 9, IPPROTO_UDP}, 5},
85 {{RTE_IPV4(198, 18, 6, 0), RTE_IPV4(198, 18, 6, 1), 9, 9, IPPROTO_UDP}, 6},
86 {{RTE_IPV4(198, 18, 7, 0), RTE_IPV4(198, 18, 7, 1), 9, 9, IPPROTO_UDP}, 7},
87 {{RTE_IPV4(198, 18, 8, 0), RTE_IPV4(198, 18, 8, 1), 9, 9, IPPROTO_UDP}, 8},
88 {{RTE_IPV4(198, 18, 9, 0), RTE_IPV4(198, 18, 9, 1), 9, 9, IPPROTO_UDP}, 9},
89 {{RTE_IPV4(198, 18, 10, 0), RTE_IPV4(198, 18, 10, 1), 9, 9, IPPROTO_UDP}, 10},
90 {{RTE_IPV4(198, 18, 11, 0), RTE_IPV4(198, 18, 11, 1), 9, 9, IPPROTO_UDP}, 11},
91 {{RTE_IPV4(198, 18, 12, 0), RTE_IPV4(198, 18, 12, 1), 9, 9, IPPROTO_UDP}, 12},
92 {{RTE_IPV4(198, 18, 13, 0), RTE_IPV4(198, 18, 13, 1), 9, 9, IPPROTO_UDP}, 13},
93 {{RTE_IPV4(198, 18, 14, 0), RTE_IPV4(198, 18, 14, 1), 9, 9, IPPROTO_UDP}, 14},
94 {{RTE_IPV4(198, 18, 15, 0), RTE_IPV4(198, 18, 15, 1), 9, 9, IPPROTO_UDP}, 15},
97 /* 2001:0200::/48 is IANA reserved range for IPv6 benchmarking (RFC5180).
98 * Use RFC863 Discard Protocol.
100 const struct ipv6_l3fwd_em_route ipv6_l3fwd_em_route_array[] = {
101 {{{32, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
102 {32, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 0},
103 {{{32, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
104 {32, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 1},
105 {{{32, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0},
106 {32, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 2},
107 {{{32, 1, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0},
108 {32, 1, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 3},
109 {{{32, 1, 2, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0},
110 {32, 1, 2, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 4},
111 {{{32, 1, 2, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0},
112 {32, 1, 2, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 5},
113 {{{32, 1, 2, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0},
114 {32, 1, 2, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 6},
115 {{{32, 1, 2, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0},
116 {32, 1, 2, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 7},
117 {{{32, 1, 2, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0},
118 {32, 1, 2, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 8},
119 {{{32, 1, 2, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0},
120 {32, 1, 2, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 9},
121 {{{32, 1, 2, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0},
122 {32, 1, 2, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 10},
123 {{{32, 1, 2, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0},
124 {32, 1, 2, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 11},
125 {{{32, 1, 2, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0},
126 {32, 1, 2, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 12},
127 {{{32, 1, 2, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0},
128 {32, 1, 2, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 13},
129 {{{32, 1, 2, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0},
130 {32, 1, 2, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 14},
131 {{{32, 1, 2, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0},
132 {32, 1, 2, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 1}, 9, 9, IPPROTO_UDP}, 15},
135 struct rte_hash *ipv4_l3fwd_em_lookup_struct[NB_SOCKETS];
136 struct rte_hash *ipv6_l3fwd_em_lookup_struct[NB_SOCKETS];
138 static inline uint32_t
139 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
142 const union ipv4_5tuple_host *k;
148 p = (const uint32_t *)&k->port_src;
151 init_val = rte_hash_crc_4byte(t, init_val);
152 init_val = rte_hash_crc_4byte(k->ip_src, init_val);
153 init_val = rte_hash_crc_4byte(k->ip_dst, init_val);
154 init_val = rte_hash_crc_4byte(*p, init_val);
156 init_val = rte_jhash_1word(t, init_val);
157 init_val = rte_jhash_1word(k->ip_src, init_val);
158 init_val = rte_jhash_1word(k->ip_dst, init_val);
159 init_val = rte_jhash_1word(*p, init_val);
165 static inline uint32_t
166 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
169 const union ipv6_5tuple_host *k;
173 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
174 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
179 p = (const uint32_t *)&k->port_src;
182 ip_src0 = (const uint32_t *) k->ip_src;
183 ip_src1 = (const uint32_t *)(k->ip_src+4);
184 ip_src2 = (const uint32_t *)(k->ip_src+8);
185 ip_src3 = (const uint32_t *)(k->ip_src+12);
186 ip_dst0 = (const uint32_t *) k->ip_dst;
187 ip_dst1 = (const uint32_t *)(k->ip_dst+4);
188 ip_dst2 = (const uint32_t *)(k->ip_dst+8);
189 ip_dst3 = (const uint32_t *)(k->ip_dst+12);
190 init_val = rte_hash_crc_4byte(t, init_val);
191 init_val = rte_hash_crc_4byte(*ip_src0, init_val);
192 init_val = rte_hash_crc_4byte(*ip_src1, init_val);
193 init_val = rte_hash_crc_4byte(*ip_src2, init_val);
194 init_val = rte_hash_crc_4byte(*ip_src3, init_val);
195 init_val = rte_hash_crc_4byte(*ip_dst0, init_val);
196 init_val = rte_hash_crc_4byte(*ip_dst1, init_val);
197 init_val = rte_hash_crc_4byte(*ip_dst2, init_val);
198 init_val = rte_hash_crc_4byte(*ip_dst3, init_val);
199 init_val = rte_hash_crc_4byte(*p, init_val);
201 init_val = rte_jhash_1word(t, init_val);
202 init_val = rte_jhash(k->ip_src,
203 sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
204 init_val = rte_jhash(k->ip_dst,
205 sizeof(uint8_t) * IPV6_ADDR_LEN, init_val);
206 init_val = rte_jhash_1word(*p, init_val);
211 static uint8_t ipv4_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
212 static uint8_t ipv6_l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
214 static rte_xmm_t mask0;
215 static rte_xmm_t mask1;
216 static rte_xmm_t mask2;
218 #if defined(__SSE2__)
220 em_mask_key(void *key, xmm_t mask)
222 __m128i data = _mm_loadu_si128((__m128i *)(key));
224 return _mm_and_si128(data, mask);
226 #elif defined(__ARM_NEON)
228 em_mask_key(void *key, xmm_t mask)
230 int32x4_t data = vld1q_s32((int32_t *)key);
232 return vandq_s32(data, mask);
234 #elif defined(__ALTIVEC__)
236 em_mask_key(void *key, xmm_t mask)
238 xmm_t data = vec_ld(0, (xmm_t *)(key));
240 return vec_and(data, mask);
242 #elif defined(RTE_ARCH_RISCV)
244 em_mask_key(void *key, xmm_t mask)
246 xmm_t data = vect_load_128(key);
248 return vect_and(data, mask);
251 #error No vector engine (SSE, NEON, ALTIVEC) available, check your toolchain
254 /* Performing hash-based lookups. 8< */
255 static inline uint16_t
256 em_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
259 union ipv4_5tuple_host key;
260 struct rte_hash *ipv4_l3fwd_lookup_struct =
261 (struct rte_hash *)lookup_struct;
263 ipv4_hdr = (uint8_t *)ipv4_hdr +
264 offsetof(struct rte_ipv4_hdr, time_to_live);
267 * Get 5 tuple: dst port, src port, dst IP address,
268 * src IP address and protocol.
270 key.xmm = em_mask_key(ipv4_hdr, mask0.x);
272 /* Find destination port */
273 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
274 return (ret < 0) ? portid : ipv4_l3fwd_out_if[ret];
276 /* >8 End of performing hash-based lookups. */
278 static inline uint16_t
279 em_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
282 union ipv6_5tuple_host key;
283 struct rte_hash *ipv6_l3fwd_lookup_struct =
284 (struct rte_hash *)lookup_struct;
286 ipv6_hdr = (uint8_t *)ipv6_hdr +
287 offsetof(struct rte_ipv6_hdr, payload_len);
288 void *data0 = ipv6_hdr;
289 void *data1 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t);
290 void *data2 = ((uint8_t *)ipv6_hdr) + sizeof(xmm_t) + sizeof(xmm_t);
292 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
293 key.xmm[0] = em_mask_key(data0, mask1.x);
296 * Get part of 5 tuple: dst IP address lower 96 bits
297 * and src IP address higher 32 bits.
299 #if defined RTE_ARCH_X86
300 key.xmm[1] = _mm_loadu_si128(data1);
302 key.xmm[1] = *(xmm_t *)data1;
306 * Get part of 5 tuple: dst port and src port
307 * and dst IP address higher 32 bits.
309 key.xmm[2] = em_mask_key(data2, mask2.x);
311 /* Find destination port */
312 ret = rte_hash_lookup(ipv6_l3fwd_lookup_struct, (const void *)&key);
313 return (ret < 0) ? portid : ipv6_l3fwd_out_if[ret];
316 #if defined RTE_ARCH_X86 || defined __ARM_NEON
317 #if defined(NO_HASH_MULTI_LOOKUP)
318 #include "l3fwd_em_sequential.h"
320 #include "l3fwd_em_hlm.h"
323 #include "l3fwd_em.h"
327 convert_ipv4_5tuple(struct ipv4_5tuple *key1,
328 union ipv4_5tuple_host *key2)
330 key2->ip_dst = rte_cpu_to_be_32(key1->ip_dst);
331 key2->ip_src = rte_cpu_to_be_32(key1->ip_src);
332 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
333 key2->port_src = rte_cpu_to_be_16(key1->port_src);
334 key2->proto = key1->proto;
340 convert_ipv6_5tuple(struct ipv6_5tuple *key1,
341 union ipv6_5tuple_host *key2)
345 for (i = 0; i < 16; i++) {
346 key2->ip_dst[i] = key1->ip_dst[i];
347 key2->ip_src[i] = key1->ip_src[i];
349 key2->port_dst = rte_cpu_to_be_16(key1->port_dst);
350 key2->port_src = rte_cpu_to_be_16(key1->port_src);
351 key2->proto = key1->proto;
357 #define BYTE_VALUE_MAX 256
358 #define ALL_32_BITS 0xffffffff
359 #define BIT_8_TO_15 0x0000ff00
362 populate_ipv4_flow_into_table(const struct rte_hash *h)
366 struct rte_eth_dev_info dev_info;
367 char srcbuf[INET6_ADDRSTRLEN];
368 char dstbuf[INET6_ADDRSTRLEN];
370 mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
371 ALL_32_BITS, ALL_32_BITS} };
373 for (i = 0; i < route_num_v4; i++) {
374 struct em_rule *entry;
375 union ipv4_5tuple_host newkey;
379 if ((1 << em_route_base_v4[i].if_out &
380 enabled_port_mask) == 0)
383 entry = &em_route_base_v4[i];
384 convert_ipv4_5tuple(&(entry->v4_key), &newkey);
385 ret = rte_hash_add_key(h, (void *) &newkey);
387 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
388 " to the l3fwd hash.\n", i);
390 ipv4_l3fwd_out_if[ret] = entry->if_out;
391 ret = rte_eth_dev_info_get(em_route_base_v4[i].if_out,
394 rte_exit(EXIT_FAILURE,
395 "Error during getting device (port %u) info: %s\n",
396 em_route_base_v4[i].if_out, strerror(-ret));
398 src.s_addr = htonl(em_route_base_v4[i].v4_key.ip_src);
399 dst.s_addr = htonl(em_route_base_v4[i].v4_key.ip_dst);
400 printf("EM: Adding route %s, %s, %d, %d, %d (%d) [%s]\n",
401 inet_ntop(AF_INET, &dst, dstbuf, sizeof(dstbuf)),
402 inet_ntop(AF_INET, &src, srcbuf, sizeof(srcbuf)),
403 em_route_base_v4[i].v4_key.port_dst,
404 em_route_base_v4[i].v4_key.port_src,
405 em_route_base_v4[i].v4_key.proto,
406 em_route_base_v4[i].if_out, dev_info.device->name);
408 printf("Hash: Adding 0x%" PRIx64 " keys\n",
409 (uint64_t)route_num_v4);
412 #define BIT_16_TO_23 0x00ff0000
414 populate_ipv6_flow_into_table(const struct rte_hash *h)
418 struct rte_eth_dev_info dev_info;
419 char srcbuf[INET6_ADDRSTRLEN];
420 char dstbuf[INET6_ADDRSTRLEN];
422 mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
423 ALL_32_BITS, ALL_32_BITS} };
425 mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
427 for (i = 0; i < route_num_v6; i++) {
428 struct em_rule *entry;
429 union ipv6_5tuple_host newkey;
431 if ((1 << em_route_base_v6[i].if_out &
432 enabled_port_mask) == 0)
435 entry = &em_route_base_v6[i];
436 convert_ipv6_5tuple(&(entry->v6_key), &newkey);
437 ret = rte_hash_add_key(h, (void *) &newkey);
439 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
440 " to the l3fwd hash.\n", i);
442 ipv6_l3fwd_out_if[ret] = entry->if_out;
443 ret = rte_eth_dev_info_get(em_route_base_v6[i].if_out,
446 rte_exit(EXIT_FAILURE,
447 "Error during getting device (port %u) info: %s\n",
448 em_route_base_v6[i].if_out, strerror(-ret));
450 printf("EM: Adding route %s, %s, %d, %d, %d (%d) [%s]\n",
451 inet_ntop(AF_INET6, em_route_base_v6[i].v6_key.ip_dst,
452 dstbuf, sizeof(dstbuf)),
453 inet_ntop(AF_INET6, em_route_base_v6[i].v6_key.ip_src,
454 srcbuf, sizeof(srcbuf)),
455 em_route_base_v6[i].v6_key.port_dst,
456 em_route_base_v6[i].v6_key.port_src,
457 em_route_base_v6[i].v6_key.proto,
458 em_route_base_v6[i].if_out, dev_info.device->name);
460 printf("Hash: Adding 0x%" PRIx64 "keys\n",
461 (uint64_t)route_num_v6);
465 * 1. IP packets without extension;
466 * 2. L4 payload should be either TCP or UDP.
469 em_check_ptype(int portid)
472 int ptype_l3_ipv4_ext = 0;
473 int ptype_l3_ipv6_ext = 0;
474 int ptype_l4_tcp = 0;
475 int ptype_l4_udp = 0;
476 uint32_t ptype_mask = RTE_PTYPE_L3_MASK | RTE_PTYPE_L4_MASK;
478 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
482 uint32_t ptypes[ret];
484 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
485 for (i = 0; i < ret; ++i) {
487 case RTE_PTYPE_L3_IPV4_EXT:
488 ptype_l3_ipv4_ext = 1;
490 case RTE_PTYPE_L3_IPV6_EXT:
491 ptype_l3_ipv6_ext = 1;
493 case RTE_PTYPE_L4_TCP:
496 case RTE_PTYPE_L4_UDP:
502 if (ptype_l3_ipv4_ext == 0)
503 printf("port %d cannot parse RTE_PTYPE_L3_IPV4_EXT\n", portid);
504 if (ptype_l3_ipv6_ext == 0)
505 printf("port %d cannot parse RTE_PTYPE_L3_IPV6_EXT\n", portid);
506 if (!ptype_l3_ipv4_ext || !ptype_l3_ipv6_ext)
509 if (ptype_l4_tcp == 0)
510 printf("port %d cannot parse RTE_PTYPE_L4_TCP\n", portid);
511 if (ptype_l4_udp == 0)
512 printf("port %d cannot parse RTE_PTYPE_L4_UDP\n", portid);
513 if (ptype_l4_tcp && ptype_l4_udp)
520 em_parse_ptype(struct rte_mbuf *m)
522 struct rte_ether_hdr *eth_hdr;
523 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
527 struct rte_ipv4_hdr *ipv4_hdr;
528 struct rte_ipv6_hdr *ipv6_hdr;
530 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
531 ether_type = eth_hdr->ether_type;
532 l3 = (uint8_t *)eth_hdr + sizeof(struct rte_ether_hdr);
533 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
534 ipv4_hdr = (struct rte_ipv4_hdr *)l3;
535 hdr_len = rte_ipv4_hdr_len(ipv4_hdr);
536 if (hdr_len == sizeof(struct rte_ipv4_hdr)) {
537 packet_type |= RTE_PTYPE_L3_IPV4;
538 if (ipv4_hdr->next_proto_id == IPPROTO_TCP)
539 packet_type |= RTE_PTYPE_L4_TCP;
540 else if (ipv4_hdr->next_proto_id == IPPROTO_UDP)
541 packet_type |= RTE_PTYPE_L4_UDP;
543 packet_type |= RTE_PTYPE_L3_IPV4_EXT;
544 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
545 ipv6_hdr = (struct rte_ipv6_hdr *)l3;
546 if (ipv6_hdr->proto == IPPROTO_TCP)
547 packet_type |= RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_TCP;
548 else if (ipv6_hdr->proto == IPPROTO_UDP)
549 packet_type |= RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_UDP;
551 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
554 m->packet_type = packet_type;
558 em_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
559 struct rte_mbuf *pkts[], uint16_t nb_pkts,
560 uint16_t max_pkts __rte_unused,
561 void *user_param __rte_unused)
565 for (i = 0; i < nb_pkts; ++i)
566 em_parse_ptype(pkts[i]);
571 /* main processing loop */
573 em_main_loop(__rte_unused void *dummy)
575 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
577 uint64_t prev_tsc, diff_tsc, cur_tsc;
581 struct lcore_conf *qconf;
582 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
583 US_PER_S * BURST_TX_DRAIN_US;
585 lcore_id = rte_lcore_id();
586 qconf = &lcore_conf[lcore_id];
588 const uint16_t n_rx_q = qconf->n_rx_queue;
589 const uint16_t n_tx_p = qconf->n_tx_port;
591 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
595 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
597 for (i = 0; i < n_rx_q; i++) {
599 portid = qconf->rx_queue_list[i].port_id;
600 queueid = qconf->rx_queue_list[i].queue_id;
602 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
603 lcore_id, portid, queueid);
606 cur_tsc = rte_rdtsc();
609 while (!force_quit) {
612 * TX burst queue drain
614 diff_tsc = cur_tsc - prev_tsc;
615 if (unlikely(diff_tsc > drain_tsc)) {
617 for (i = 0; i < n_tx_p; ++i) {
618 portid = qconf->tx_port_id[i];
619 if (qconf->tx_mbufs[portid].len == 0)
622 qconf->tx_mbufs[portid].len,
624 qconf->tx_mbufs[portid].len = 0;
631 * Read packet from RX queues
633 for (i = 0; i < n_rx_q; ++i) {
634 portid = qconf->rx_queue_list[i].port_id;
635 queueid = qconf->rx_queue_list[i].queue_id;
636 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
641 #if defined RTE_ARCH_X86 || defined __ARM_NEON
642 l3fwd_em_send_packets(nb_rx, pkts_burst,
645 l3fwd_em_no_opt_send_packets(nb_rx, pkts_burst,
650 cur_tsc = rte_rdtsc();
656 static __rte_always_inline void
657 em_event_loop_single(struct l3fwd_event_resources *evt_rsrc,
660 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
661 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
662 evt_rsrc->evq.nb_queues - 1];
663 const uint8_t event_d_id = evt_rsrc->event_d_id;
664 uint8_t deq = 0, enq = 0;
665 struct lcore_conf *lconf;
666 unsigned int lcore_id;
672 lcore_id = rte_lcore_id();
673 lconf = &lcore_conf[lcore_id];
675 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
676 while (!force_quit) {
677 deq = rte_event_dequeue_burst(event_d_id, event_p_id, &ev, 1,
682 struct rte_mbuf *mbuf = ev.mbuf;
684 #if defined RTE_ARCH_X86 || defined __ARM_NEON
685 mbuf->port = em_get_dst_port(lconf, mbuf, mbuf->port);
686 process_packet(mbuf, &mbuf->port);
688 l3fwd_em_simple_process(mbuf, lconf);
690 if (mbuf->port == BAD_PORT) {
691 rte_pktmbuf_free(mbuf);
695 if (flags & L3FWD_EVENT_TX_ENQ) {
696 ev.queue_id = tx_q_id;
697 ev.op = RTE_EVENT_OP_FORWARD;
699 enq = rte_event_enqueue_burst(
700 event_d_id, event_p_id, &ev, 1);
701 } while (!enq && !force_quit);
704 if (flags & L3FWD_EVENT_TX_DIRECT) {
705 rte_event_eth_tx_adapter_txq_set(mbuf, 0);
707 enq = rte_event_eth_tx_adapter_enqueue(
708 event_d_id, event_p_id, &ev, 1, 0);
709 } while (!enq && !force_quit);
713 l3fwd_event_worker_cleanup(event_d_id, event_p_id, &ev, enq, deq, 0);
716 static __rte_always_inline void
717 em_event_loop_burst(struct l3fwd_event_resources *evt_rsrc,
720 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
721 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
722 evt_rsrc->evq.nb_queues - 1];
723 const uint8_t event_d_id = evt_rsrc->event_d_id;
724 const uint16_t deq_len = evt_rsrc->deq_depth;
725 struct rte_event events[MAX_PKT_BURST];
726 int i, nb_enq = 0, nb_deq = 0;
727 struct lcore_conf *lconf;
728 unsigned int lcore_id;
733 lcore_id = rte_lcore_id();
735 lconf = &lcore_conf[lcore_id];
737 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
739 while (!force_quit) {
740 /* Read events from RX queues */
741 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
748 #if defined RTE_ARCH_X86 || defined __ARM_NEON
749 l3fwd_em_process_events(nb_deq, (struct rte_event **)&events,
752 l3fwd_em_no_opt_process_events(nb_deq,
753 (struct rte_event **)&events,
756 for (i = 0; i < nb_deq; i++) {
757 if (flags & L3FWD_EVENT_TX_ENQ) {
758 events[i].queue_id = tx_q_id;
759 events[i].op = RTE_EVENT_OP_FORWARD;
762 if (flags & L3FWD_EVENT_TX_DIRECT)
763 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
767 if (flags & L3FWD_EVENT_TX_ENQ) {
768 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
770 while (nb_enq < nb_deq && !force_quit)
771 nb_enq += rte_event_enqueue_burst(event_d_id,
772 event_p_id, events + nb_enq,
776 if (flags & L3FWD_EVENT_TX_DIRECT) {
777 nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
778 event_p_id, events, nb_deq, 0);
779 while (nb_enq < nb_deq && !force_quit)
780 nb_enq += rte_event_eth_tx_adapter_enqueue(
781 event_d_id, event_p_id,
787 l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
791 static __rte_always_inline void
792 em_event_loop(struct l3fwd_event_resources *evt_rsrc,
795 if (flags & L3FWD_EVENT_SINGLE)
796 em_event_loop_single(evt_rsrc, flags);
797 if (flags & L3FWD_EVENT_BURST)
798 em_event_loop_burst(evt_rsrc, flags);
802 em_event_main_loop_tx_d(__rte_unused void *dummy)
804 struct l3fwd_event_resources *evt_rsrc =
805 l3fwd_get_eventdev_rsrc();
807 em_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_SINGLE);
812 em_event_main_loop_tx_d_burst(__rte_unused void *dummy)
814 struct l3fwd_event_resources *evt_rsrc =
815 l3fwd_get_eventdev_rsrc();
817 em_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_BURST);
822 em_event_main_loop_tx_q(__rte_unused void *dummy)
824 struct l3fwd_event_resources *evt_rsrc =
825 l3fwd_get_eventdev_rsrc();
827 em_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_SINGLE);
832 em_event_main_loop_tx_q_burst(__rte_unused void *dummy)
834 struct l3fwd_event_resources *evt_rsrc =
835 l3fwd_get_eventdev_rsrc();
837 em_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_BURST);
841 /* Same eventdev loop for single and burst of vector */
842 static __rte_always_inline void
843 em_event_loop_vector(struct l3fwd_event_resources *evt_rsrc,
846 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
847 const uint8_t tx_q_id =
848 evt_rsrc->evq.event_q_id[evt_rsrc->evq.nb_queues - 1];
849 const uint8_t event_d_id = evt_rsrc->event_d_id;
850 const uint16_t deq_len = evt_rsrc->deq_depth;
851 struct rte_event events[MAX_PKT_BURST];
852 int i, nb_enq = 0, nb_deq = 0;
853 struct lcore_conf *lconf;
854 unsigned int lcore_id;
859 lcore_id = rte_lcore_id();
860 lconf = &lcore_conf[lcore_id];
862 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
864 while (!force_quit) {
865 /* Read events from RX queues */
866 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id, events,
873 for (i = 0; i < nb_deq; i++) {
874 if (flags & L3FWD_EVENT_TX_ENQ) {
875 events[i].queue_id = tx_q_id;
876 events[i].op = RTE_EVENT_OP_FORWARD;
879 #if defined RTE_ARCH_X86 || defined __ARM_NEON
880 l3fwd_em_process_event_vector(events[i].vec, lconf);
882 l3fwd_em_no_opt_process_event_vector(events[i].vec,
885 if (flags & L3FWD_EVENT_TX_DIRECT)
886 event_vector_txq_set(events[i].vec, 0);
889 if (flags & L3FWD_EVENT_TX_ENQ) {
890 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
892 while (nb_enq < nb_deq && !force_quit)
893 nb_enq += rte_event_enqueue_burst(
894 event_d_id, event_p_id, events + nb_enq,
898 if (flags & L3FWD_EVENT_TX_DIRECT) {
899 nb_enq = rte_event_eth_tx_adapter_enqueue(
900 event_d_id, event_p_id, events, nb_deq, 0);
901 while (nb_enq < nb_deq && !force_quit)
902 nb_enq += rte_event_eth_tx_adapter_enqueue(
903 event_d_id, event_p_id, events + nb_enq,
908 l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
913 em_event_main_loop_tx_d_vector(__rte_unused void *dummy)
915 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
917 em_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
922 em_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy)
924 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
926 em_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
931 em_event_main_loop_tx_q_vector(__rte_unused void *dummy)
933 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
935 em_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
940 em_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy)
942 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
944 em_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
948 /* Initialize exact match (hash) parameters. 8< */
950 setup_hash(const int socketid)
952 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
954 .entries = L3FWD_HASH_ENTRIES,
955 .key_len = sizeof(union ipv4_5tuple_host),
956 .hash_func = ipv4_hash_crc,
957 .hash_func_init_val = 0,
960 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
962 .entries = L3FWD_HASH_ENTRIES,
963 .key_len = sizeof(union ipv6_5tuple_host),
964 .hash_func = ipv6_hash_crc,
965 .hash_func_init_val = 0,
970 /* create ipv4 hash */
971 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
972 ipv4_l3fwd_hash_params.name = s;
973 ipv4_l3fwd_hash_params.socket_id = socketid;
974 ipv4_l3fwd_em_lookup_struct[socketid] =
975 rte_hash_create(&ipv4_l3fwd_hash_params);
976 if (ipv4_l3fwd_em_lookup_struct[socketid] == NULL)
977 rte_exit(EXIT_FAILURE,
978 "Unable to create the l3fwd hash on socket %d\n",
981 /* create ipv6 hash */
982 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
983 ipv6_l3fwd_hash_params.name = s;
984 ipv6_l3fwd_hash_params.socket_id = socketid;
985 ipv6_l3fwd_em_lookup_struct[socketid] =
986 rte_hash_create(&ipv6_l3fwd_hash_params);
987 if (ipv6_l3fwd_em_lookup_struct[socketid] == NULL)
988 rte_exit(EXIT_FAILURE,
989 "Unable to create the l3fwd hash on socket %d\n",
993 * Use data from ipv4/ipv6 l3fwd config file
994 * directly to initialize the hash table.
997 /* populate the ipv4 hash */
998 populate_ipv4_flow_into_table(
999 ipv4_l3fwd_em_lookup_struct[socketid]);
1001 /* populate the ipv6 hash */
1002 populate_ipv6_flow_into_table(
1003 ipv6_l3fwd_em_lookup_struct[socketid]);
1006 /* >8 End of initialization of hash parameters. */
1008 /* Return ipv4/ipv6 em fwd lookup struct. */
1010 em_get_ipv4_l3fwd_lookup_struct(const int socketid)
1012 return ipv4_l3fwd_em_lookup_struct[socketid];
1016 em_get_ipv6_l3fwd_lookup_struct(const int socketid)
1018 return ipv6_l3fwd_em_lookup_struct[socketid];