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"
31 #if defined(RTE_ARCH_X86) || defined(__ARM_FEATURE_CRC32)
36 #include <rte_hash_crc.h>
37 #define DEFAULT_HASH_FUNC rte_hash_crc
39 #include <rte_jhash.h>
40 #define DEFAULT_HASH_FUNC rte_jhash
43 #define IPV6_ADDR_LEN 16
53 union ipv4_5tuple_host {
66 #define XMM_NUM_IN_IPV6_5TUPLE 3
69 uint8_t ip_dst[IPV6_ADDR_LEN];
70 uint8_t ip_src[IPV6_ADDR_LEN];
76 union ipv6_5tuple_host {
81 uint8_t ip_src[IPV6_ADDR_LEN];
82 uint8_t ip_dst[IPV6_ADDR_LEN];
87 xmm_t xmm[XMM_NUM_IN_IPV6_5TUPLE];
92 struct ipv4_l3fwd_em_route {
93 struct ipv4_5tuple key;
97 struct ipv6_l3fwd_em_route {
98 struct ipv6_5tuple key;
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},
109 static struct ipv6_l3fwd_em_route ipv6_l3fwd_em_route_array[] = {
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},
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},
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},
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},
131 struct rte_hash *ipv4_l3fwd_em_lookup_struct[NB_SOCKETS];
132 struct rte_hash *ipv6_l3fwd_em_lookup_struct[NB_SOCKETS];
134 static inline uint32_t
135 ipv4_hash_crc(const void *data, __rte_unused uint32_t data_len,
138 const union ipv4_5tuple_host *k;
144 p = (const uint32_t *)&k->port_src;
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);
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);
161 static inline uint32_t
162 ipv6_hash_crc(const void *data, __rte_unused uint32_t data_len,
165 const union ipv6_5tuple_host *k;
169 const uint32_t *ip_src0, *ip_src1, *ip_src2, *ip_src3;
170 const uint32_t *ip_dst0, *ip_dst1, *ip_dst2, *ip_dst3;
175 p = (const uint32_t *)&k->port_src;
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);
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);
207 #define IPV4_L3FWD_EM_NUM_ROUTES RTE_DIM(ipv4_l3fwd_em_route_array)
209 #define IPV6_L3FWD_EM_NUM_ROUTES RTE_DIM(ipv6_l3fwd_em_route_array)
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);
243 #error No vector engine (SSE, NEON, ALTIVEC) available, check your toolchain
246 /* Performing hash-based lookups. 8< */
247 static inline uint16_t
248 em_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
251 union ipv4_5tuple_host key;
252 struct rte_hash *ipv4_l3fwd_lookup_struct =
253 (struct rte_hash *)lookup_struct;
255 ipv4_hdr = (uint8_t *)ipv4_hdr +
256 offsetof(struct rte_ipv4_hdr, time_to_live);
259 * Get 5 tuple: dst port, src port, dst IP address,
260 * src IP address and protocol.
262 key.xmm = em_mask_key(ipv4_hdr, mask0.x);
264 /* Find destination port */
265 ret = rte_hash_lookup(ipv4_l3fwd_lookup_struct, (const void *)&key);
266 return (ret < 0) ? portid : ipv4_l3fwd_out_if[ret];
268 /* >8 End of performing hash-based lookups. */
270 static inline uint16_t
271 em_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
274 union ipv6_5tuple_host key;
275 struct rte_hash *ipv6_l3fwd_lookup_struct =
276 (struct rte_hash *)lookup_struct;
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);
284 /* Get part of 5 tuple: src IP address lower 96 bits and protocol */
285 key.xmm[0] = em_mask_key(data0, mask1.x);
288 * Get part of 5 tuple: dst IP address lower 96 bits
289 * and src IP address higher 32 bits.
291 #if defined RTE_ARCH_X86
292 key.xmm[1] = _mm_loadu_si128(data1);
294 key.xmm[1] = *(xmm_t *)data1;
298 * Get part of 5 tuple: dst port and src port
299 * and dst IP address higher 32 bits.
301 key.xmm[2] = em_mask_key(data2, mask2.x);
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];
308 #if defined RTE_ARCH_X86 || defined __ARM_NEON
309 #if defined(NO_HASH_MULTI_LOOKUP)
310 #include "l3fwd_em_sequential.h"
312 #include "l3fwd_em_hlm.h"
315 #include "l3fwd_em.h"
319 convert_ipv4_5tuple(struct ipv4_5tuple *key1,
320 union ipv4_5tuple_host *key2)
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;
332 convert_ipv6_5tuple(struct ipv6_5tuple *key1,
333 union ipv6_5tuple_host *key2)
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];
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;
349 #define BYTE_VALUE_MAX 256
350 #define ALL_32_BITS 0xffffffff
351 #define BIT_8_TO_15 0x0000ff00
354 populate_ipv4_few_flow_into_table(const struct rte_hash *h)
359 mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
360 ALL_32_BITS, ALL_32_BITS} };
362 for (i = 0; i < IPV4_L3FWD_EM_NUM_ROUTES; i++) {
363 struct ipv4_l3fwd_em_route entry;
364 union ipv4_5tuple_host newkey;
366 entry = ipv4_l3fwd_em_route_array[i];
367 convert_ipv4_5tuple(&entry.key, &newkey);
368 ret = rte_hash_add_key(h, (void *) &newkey);
370 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
371 " to the l3fwd hash.\n", i);
373 ipv4_l3fwd_out_if[ret] = entry.if_out;
375 printf("Hash: Adding 0x%" PRIx64 " keys\n",
376 (uint64_t)IPV4_L3FWD_EM_NUM_ROUTES);
379 #define BIT_16_TO_23 0x00ff0000
381 populate_ipv6_few_flow_into_table(const struct rte_hash *h)
386 mask1 = (rte_xmm_t){.u32 = {BIT_16_TO_23, ALL_32_BITS,
387 ALL_32_BITS, ALL_32_BITS} };
389 mask2 = (rte_xmm_t){.u32 = {ALL_32_BITS, ALL_32_BITS, 0, 0} };
391 for (i = 0; i < IPV6_L3FWD_EM_NUM_ROUTES; i++) {
392 struct ipv6_l3fwd_em_route entry;
393 union ipv6_5tuple_host newkey;
395 entry = ipv6_l3fwd_em_route_array[i];
396 convert_ipv6_5tuple(&entry.key, &newkey);
397 ret = rte_hash_add_key(h, (void *) &newkey);
399 rte_exit(EXIT_FAILURE, "Unable to add entry %" PRIu32
400 " to the l3fwd hash.\n", i);
402 ipv6_l3fwd_out_if[ret] = entry.if_out;
404 printf("Hash: Adding 0x%" PRIx64 "keys\n",
405 (uint64_t)IPV6_L3FWD_EM_NUM_ROUTES);
408 #define NUMBER_PORT_USED 4
410 populate_ipv4_many_flow_into_table(const struct rte_hash *h,
411 unsigned int nr_flow)
415 mask0 = (rte_xmm_t){.u32 = {BIT_8_TO_15, ALL_32_BITS,
416 ALL_32_BITS, ALL_32_BITS} };
418 for (i = 0; i < nr_flow; i++) {
419 struct ipv4_l3fwd_em_route entry;
420 union ipv4_5tuple_host newkey;
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));
429 /* Create the ipv4 exact match flow */
430 memset(&entry, 0, sizeof(entry));
431 switch (i & (NUMBER_PORT_USED - 1)) {
433 entry = ipv4_l3fwd_em_route_array[0];
434 entry.key.ip_dst = RTE_IPV4(101, c, b, a);
437 entry = ipv4_l3fwd_em_route_array[1];
438 entry.key.ip_dst = RTE_IPV4(201, c, b, a);
441 entry = ipv4_l3fwd_em_route_array[2];
442 entry.key.ip_dst = RTE_IPV4(111, c, b, a);
445 entry = ipv4_l3fwd_em_route_array[3];
446 entry.key.ip_dst = RTE_IPV4(211, c, b, a);
449 convert_ipv4_5tuple(&entry.key, &newkey);
450 int32_t ret = rte_hash_add_key(h, (void *) &newkey);
453 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
455 ipv4_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
458 printf("Hash: Adding 0x%x keys\n", nr_flow);
462 populate_ipv6_many_flow_into_table(const struct rte_hash *h,
463 unsigned int nr_flow)
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} };
471 for (i = 0; i < nr_flow; i++) {
472 struct ipv6_l3fwd_em_route entry;
473 union ipv6_5tuple_host newkey;
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));
482 /* Create the ipv6 exact match flow */
483 memset(&entry, 0, sizeof(entry));
484 switch (i & (NUMBER_PORT_USED - 1)) {
486 entry = ipv6_l3fwd_em_route_array[0];
489 entry = ipv6_l3fwd_em_route_array[1];
492 entry = ipv6_l3fwd_em_route_array[2];
495 entry = ipv6_l3fwd_em_route_array[3];
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);
505 rte_exit(EXIT_FAILURE, "Unable to add entry %u\n", i);
507 ipv6_l3fwd_out_if[ret] = (uint8_t) entry.if_out;
510 printf("Hash: Adding 0x%x keys\n", nr_flow);
514 * 1. IP packets without extension;
515 * 2. L4 payload should be either TCP or UDP.
518 em_check_ptype(int portid)
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;
527 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
531 uint32_t ptypes[ret];
533 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
534 for (i = 0; i < ret; ++i) {
536 case RTE_PTYPE_L3_IPV4_EXT:
537 ptype_l3_ipv4_ext = 1;
539 case RTE_PTYPE_L3_IPV6_EXT:
540 ptype_l3_ipv6_ext = 1;
542 case RTE_PTYPE_L4_TCP:
545 case RTE_PTYPE_L4_UDP:
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)
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)
569 em_parse_ptype(struct rte_mbuf *m)
571 struct rte_ether_hdr *eth_hdr;
572 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
576 struct rte_ipv4_hdr *ipv4_hdr;
577 struct rte_ipv6_hdr *ipv6_hdr;
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 = rte_ipv4_hdr_len(ipv4_hdr);
585 if (hdr_len == sizeof(struct rte_ipv4_hdr)) {
586 packet_type |= RTE_PTYPE_L3_IPV4;
587 if (ipv4_hdr->next_proto_id == IPPROTO_TCP)
588 packet_type |= RTE_PTYPE_L4_TCP;
589 else if (ipv4_hdr->next_proto_id == IPPROTO_UDP)
590 packet_type |= RTE_PTYPE_L4_UDP;
592 packet_type |= RTE_PTYPE_L3_IPV4_EXT;
593 } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
594 ipv6_hdr = (struct rte_ipv6_hdr *)l3;
595 if (ipv6_hdr->proto == IPPROTO_TCP)
596 packet_type |= RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_TCP;
597 else if (ipv6_hdr->proto == IPPROTO_UDP)
598 packet_type |= RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_UDP;
600 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
603 m->packet_type = packet_type;
607 em_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
608 struct rte_mbuf *pkts[], uint16_t nb_pkts,
609 uint16_t max_pkts __rte_unused,
610 void *user_param __rte_unused)
614 for (i = 0; i < nb_pkts; ++i)
615 em_parse_ptype(pkts[i]);
620 /* main processing loop */
622 em_main_loop(__rte_unused void *dummy)
624 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
626 uint64_t prev_tsc, diff_tsc, cur_tsc;
630 struct lcore_conf *qconf;
631 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
632 US_PER_S * BURST_TX_DRAIN_US;
634 lcore_id = rte_lcore_id();
635 qconf = &lcore_conf[lcore_id];
637 const uint16_t n_rx_q = qconf->n_rx_queue;
638 const uint16_t n_tx_p = qconf->n_tx_port;
640 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
644 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
646 for (i = 0; i < n_rx_q; i++) {
648 portid = qconf->rx_queue_list[i].port_id;
649 queueid = qconf->rx_queue_list[i].queue_id;
651 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
652 lcore_id, portid, queueid);
655 cur_tsc = rte_rdtsc();
658 while (!force_quit) {
661 * TX burst queue drain
663 diff_tsc = cur_tsc - prev_tsc;
664 if (unlikely(diff_tsc > drain_tsc)) {
666 for (i = 0; i < n_tx_p; ++i) {
667 portid = qconf->tx_port_id[i];
668 if (qconf->tx_mbufs[portid].len == 0)
671 qconf->tx_mbufs[portid].len,
673 qconf->tx_mbufs[portid].len = 0;
680 * Read packet from RX queues
682 for (i = 0; i < n_rx_q; ++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,
690 #if defined RTE_ARCH_X86 || defined __ARM_NEON
691 l3fwd_em_send_packets(nb_rx, pkts_burst,
694 l3fwd_em_no_opt_send_packets(nb_rx, pkts_burst,
699 cur_tsc = rte_rdtsc();
705 static __rte_always_inline void
706 em_event_loop_single(struct l3fwd_event_resources *evt_rsrc,
709 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
710 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
711 evt_rsrc->evq.nb_queues - 1];
712 const uint8_t event_d_id = evt_rsrc->event_d_id;
713 struct lcore_conf *lconf;
714 unsigned int lcore_id;
720 lcore_id = rte_lcore_id();
721 lconf = &lcore_conf[lcore_id];
723 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
724 while (!force_quit) {
725 if (!rte_event_dequeue_burst(event_d_id, event_p_id, &ev, 1, 0))
728 struct rte_mbuf *mbuf = ev.mbuf;
730 #if defined RTE_ARCH_X86 || defined __ARM_NEON
731 mbuf->port = em_get_dst_port(lconf, mbuf, mbuf->port);
732 process_packet(mbuf, &mbuf->port);
734 l3fwd_em_simple_process(mbuf, lconf);
736 if (mbuf->port == BAD_PORT) {
737 rte_pktmbuf_free(mbuf);
741 if (flags & L3FWD_EVENT_TX_ENQ) {
742 ev.queue_id = tx_q_id;
743 ev.op = RTE_EVENT_OP_FORWARD;
744 while (rte_event_enqueue_burst(event_d_id, event_p_id,
745 &ev, 1) && !force_quit)
749 if (flags & L3FWD_EVENT_TX_DIRECT) {
750 rte_event_eth_tx_adapter_txq_set(mbuf, 0);
751 while (!rte_event_eth_tx_adapter_enqueue(event_d_id,
752 event_p_id, &ev, 1, 0) &&
759 static __rte_always_inline void
760 em_event_loop_burst(struct l3fwd_event_resources *evt_rsrc,
763 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
764 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
765 evt_rsrc->evq.nb_queues - 1];
766 const uint8_t event_d_id = evt_rsrc->event_d_id;
767 const uint16_t deq_len = evt_rsrc->deq_depth;
768 struct rte_event events[MAX_PKT_BURST];
769 struct lcore_conf *lconf;
770 unsigned int lcore_id;
771 int i, nb_enq, nb_deq;
776 lcore_id = rte_lcore_id();
778 lconf = &lcore_conf[lcore_id];
780 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
782 while (!force_quit) {
783 /* Read events from RX queues */
784 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
791 #if defined RTE_ARCH_X86 || defined __ARM_NEON
792 l3fwd_em_process_events(nb_deq, (struct rte_event **)&events,
795 l3fwd_em_no_opt_process_events(nb_deq,
796 (struct rte_event **)&events,
799 for (i = 0; i < nb_deq; i++) {
800 if (flags & L3FWD_EVENT_TX_ENQ) {
801 events[i].queue_id = tx_q_id;
802 events[i].op = RTE_EVENT_OP_FORWARD;
805 if (flags & L3FWD_EVENT_TX_DIRECT)
806 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
810 if (flags & L3FWD_EVENT_TX_ENQ) {
811 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
813 while (nb_enq < nb_deq && !force_quit)
814 nb_enq += rte_event_enqueue_burst(event_d_id,
815 event_p_id, events + nb_enq,
819 if (flags & L3FWD_EVENT_TX_DIRECT) {
820 nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
821 event_p_id, events, nb_deq, 0);
822 while (nb_enq < nb_deq && !force_quit)
823 nb_enq += rte_event_eth_tx_adapter_enqueue(
824 event_d_id, event_p_id,
831 static __rte_always_inline void
832 em_event_loop(struct l3fwd_event_resources *evt_rsrc,
835 if (flags & L3FWD_EVENT_SINGLE)
836 em_event_loop_single(evt_rsrc, flags);
837 if (flags & L3FWD_EVENT_BURST)
838 em_event_loop_burst(evt_rsrc, flags);
842 em_event_main_loop_tx_d(__rte_unused void *dummy)
844 struct l3fwd_event_resources *evt_rsrc =
845 l3fwd_get_eventdev_rsrc();
847 em_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_SINGLE);
852 em_event_main_loop_tx_d_burst(__rte_unused void *dummy)
854 struct l3fwd_event_resources *evt_rsrc =
855 l3fwd_get_eventdev_rsrc();
857 em_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_BURST);
862 em_event_main_loop_tx_q(__rte_unused void *dummy)
864 struct l3fwd_event_resources *evt_rsrc =
865 l3fwd_get_eventdev_rsrc();
867 em_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_SINGLE);
872 em_event_main_loop_tx_q_burst(__rte_unused void *dummy)
874 struct l3fwd_event_resources *evt_rsrc =
875 l3fwd_get_eventdev_rsrc();
877 em_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_BURST);
881 /* Same eventdev loop for single and burst of vector */
882 static __rte_always_inline void
883 em_event_loop_vector(struct l3fwd_event_resources *evt_rsrc,
886 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
887 const uint8_t tx_q_id =
888 evt_rsrc->evq.event_q_id[evt_rsrc->evq.nb_queues - 1];
889 const uint8_t event_d_id = evt_rsrc->event_d_id;
890 const uint16_t deq_len = evt_rsrc->deq_depth;
891 struct rte_event events[MAX_PKT_BURST];
892 struct lcore_conf *lconf;
893 unsigned int lcore_id;
894 int i, nb_enq, nb_deq;
899 lcore_id = rte_lcore_id();
900 lconf = &lcore_conf[lcore_id];
902 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
904 while (!force_quit) {
905 /* Read events from RX queues */
906 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id, events,
913 for (i = 0; i < nb_deq; i++) {
914 if (flags & L3FWD_EVENT_TX_ENQ) {
915 events[i].queue_id = tx_q_id;
916 events[i].op = RTE_EVENT_OP_FORWARD;
919 #if defined RTE_ARCH_X86 || defined __ARM_NEON
920 l3fwd_em_process_event_vector(events[i].vec, lconf);
922 l3fwd_em_no_opt_process_event_vector(events[i].vec,
925 if (flags & L3FWD_EVENT_TX_DIRECT)
926 event_vector_txq_set(events[i].vec, 0);
929 if (flags & L3FWD_EVENT_TX_ENQ) {
930 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
932 while (nb_enq < nb_deq && !force_quit)
933 nb_enq += rte_event_enqueue_burst(
934 event_d_id, event_p_id, events + nb_enq,
938 if (flags & L3FWD_EVENT_TX_DIRECT) {
939 nb_enq = rte_event_eth_tx_adapter_enqueue(
940 event_d_id, event_p_id, events, nb_deq, 0);
941 while (nb_enq < nb_deq && !force_quit)
942 nb_enq += rte_event_eth_tx_adapter_enqueue(
943 event_d_id, event_p_id, events + nb_enq,
950 em_event_main_loop_tx_d_vector(__rte_unused void *dummy)
952 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
954 em_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
959 em_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy)
961 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
963 em_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
968 em_event_main_loop_tx_q_vector(__rte_unused void *dummy)
970 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
972 em_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
977 em_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy)
979 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
981 em_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
985 /* Initialize exact match (hash) parameters. 8< */
987 setup_hash(const int socketid)
989 struct rte_hash_parameters ipv4_l3fwd_hash_params = {
991 .entries = L3FWD_HASH_ENTRIES,
992 .key_len = sizeof(union ipv4_5tuple_host),
993 .hash_func = ipv4_hash_crc,
994 .hash_func_init_val = 0,
997 struct rte_hash_parameters ipv6_l3fwd_hash_params = {
999 .entries = L3FWD_HASH_ENTRIES,
1000 .key_len = sizeof(union ipv6_5tuple_host),
1001 .hash_func = ipv6_hash_crc,
1002 .hash_func_init_val = 0,
1007 /* create ipv4 hash */
1008 snprintf(s, sizeof(s), "ipv4_l3fwd_hash_%d", socketid);
1009 ipv4_l3fwd_hash_params.name = s;
1010 ipv4_l3fwd_hash_params.socket_id = socketid;
1011 ipv4_l3fwd_em_lookup_struct[socketid] =
1012 rte_hash_create(&ipv4_l3fwd_hash_params);
1013 if (ipv4_l3fwd_em_lookup_struct[socketid] == NULL)
1014 rte_exit(EXIT_FAILURE,
1015 "Unable to create the l3fwd hash on socket %d\n",
1018 /* create ipv6 hash */
1019 snprintf(s, sizeof(s), "ipv6_l3fwd_hash_%d", socketid);
1020 ipv6_l3fwd_hash_params.name = s;
1021 ipv6_l3fwd_hash_params.socket_id = socketid;
1022 ipv6_l3fwd_em_lookup_struct[socketid] =
1023 rte_hash_create(&ipv6_l3fwd_hash_params);
1024 if (ipv6_l3fwd_em_lookup_struct[socketid] == NULL)
1025 rte_exit(EXIT_FAILURE,
1026 "Unable to create the l3fwd hash on socket %d\n",
1029 if (hash_entry_number != HASH_ENTRY_NUMBER_DEFAULT) {
1030 /* For testing hash matching with a large number of flows we
1031 * generate millions of IP 5-tuples with an incremented dst
1032 * address to initialize the hash table. */
1034 /* populate the ipv4 hash */
1035 populate_ipv4_many_flow_into_table(
1036 ipv4_l3fwd_em_lookup_struct[socketid],
1039 /* populate the ipv6 hash */
1040 populate_ipv6_many_flow_into_table(
1041 ipv6_l3fwd_em_lookup_struct[socketid],
1046 * Use data in ipv4/ipv6 l3fwd lookup table
1047 * directly to initialize the hash table.
1050 /* populate the ipv4 hash */
1051 populate_ipv4_few_flow_into_table(
1052 ipv4_l3fwd_em_lookup_struct[socketid]);
1054 /* populate the ipv6 hash */
1055 populate_ipv6_few_flow_into_table(
1056 ipv6_l3fwd_em_lookup_struct[socketid]);
1060 /* >8 End of initialization of hash parameters. */
1062 /* Return ipv4/ipv6 em fwd lookup struct. */
1064 em_get_ipv4_l3fwd_lookup_struct(const int socketid)
1066 return ipv4_l3fwd_em_lookup_struct[socketid];
1070 em_get_ipv6_l3fwd_lookup_struct(const int socketid)
1072 return ipv6_l3fwd_em_lookup_struct[socketid];