1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/queue.h>
16 #include <sys/socket.h>
17 #include <arpa/inet.h>
19 #include <rte_debug.h>
20 #include <rte_ether.h>
21 #include <rte_ethdev.h>
22 #include <rte_cycles.h>
31 #include "l3fwd_event.h"
33 #include "lpm_route_parse.c"
35 #define IPV4_L3FWD_LPM_MAX_RULES 1024
36 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
37 #define IPV6_L3FWD_LPM_MAX_RULES 1024
38 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
40 static struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
41 static struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
43 /* Performing LPM-based lookups. 8< */
44 static inline uint16_t
45 lpm_get_ipv4_dst_port(const struct rte_ipv4_hdr *ipv4_hdr,
47 struct rte_lpm *ipv4_l3fwd_lookup_struct)
49 uint32_t dst_ip = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
52 if (rte_lpm_lookup(ipv4_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
57 /* >8 End of performing LPM-based lookups. */
59 static inline uint16_t
60 lpm_get_ipv6_dst_port(const struct rte_ipv6_hdr *ipv6_hdr,
62 struct rte_lpm6 *ipv6_l3fwd_lookup_struct)
64 const uint8_t *dst_ip = ipv6_hdr->dst_addr;
67 if (rte_lpm6_lookup(ipv6_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
73 static __rte_always_inline uint16_t
74 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
77 struct rte_ipv6_hdr *ipv6_hdr;
78 struct rte_ipv4_hdr *ipv4_hdr;
79 struct rte_ether_hdr *eth_hdr;
81 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
83 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
84 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
86 return lpm_get_ipv4_dst_port(ipv4_hdr, portid,
87 qconf->ipv4_lookup_struct);
88 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
90 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
91 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
93 return lpm_get_ipv6_dst_port(ipv6_hdr, portid,
94 qconf->ipv6_lookup_struct);
101 * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
102 * precalculated. If packet is ipv6 dst_addr is taken directly from packet
103 * header and dst_ipv4 value is not used.
105 static __rte_always_inline uint16_t
106 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
107 uint32_t dst_ipv4, uint16_t portid)
110 struct rte_ipv6_hdr *ipv6_hdr;
111 struct rte_ether_hdr *eth_hdr;
113 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
114 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
115 dst_ipv4, &next_hop) == 0)
116 ? next_hop : portid);
118 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
120 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
121 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
123 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
124 ipv6_hdr->dst_addr, &next_hop) == 0)
125 ? next_hop : portid);
132 #if defined(RTE_ARCH_X86)
133 #include "l3fwd_lpm_sse.h"
134 #elif defined __ARM_NEON
135 #include "l3fwd_lpm_neon.h"
136 #elif defined(RTE_ARCH_PPC_64)
137 #include "l3fwd_lpm_altivec.h"
139 #include "l3fwd_lpm.h"
142 /* main processing loop */
144 lpm_main_loop(__rte_unused void *dummy)
146 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
148 uint64_t prev_tsc, diff_tsc, cur_tsc;
152 struct lcore_conf *qconf;
153 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
154 US_PER_S * BURST_TX_DRAIN_US;
156 lcore_id = rte_lcore_id();
157 qconf = &lcore_conf[lcore_id];
159 const uint16_t n_rx_q = qconf->n_rx_queue;
160 const uint16_t n_tx_p = qconf->n_tx_port;
162 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
166 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
168 for (i = 0; i < n_rx_q; i++) {
170 portid = qconf->rx_queue_list[i].port_id;
171 queueid = qconf->rx_queue_list[i].queue_id;
173 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
174 lcore_id, portid, queueid);
177 cur_tsc = rte_rdtsc();
180 while (!force_quit) {
183 * TX burst queue drain
185 diff_tsc = cur_tsc - prev_tsc;
186 if (unlikely(diff_tsc > drain_tsc)) {
188 for (i = 0; i < n_tx_p; ++i) {
189 portid = qconf->tx_port_id[i];
190 if (qconf->tx_mbufs[portid].len == 0)
193 qconf->tx_mbufs[portid].len,
195 qconf->tx_mbufs[portid].len = 0;
202 * Read packet from RX queues
204 for (i = 0; i < n_rx_q; ++i) {
205 portid = qconf->rx_queue_list[i].port_id;
206 queueid = qconf->rx_queue_list[i].queue_id;
207 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
212 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
213 || defined RTE_ARCH_PPC_64
214 l3fwd_lpm_send_packets(nb_rx, pkts_burst,
217 l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
222 cur_tsc = rte_rdtsc();
228 static __rte_always_inline uint16_t
229 lpm_process_event_pkt(const struct lcore_conf *lconf, struct rte_mbuf *mbuf)
231 mbuf->port = lpm_get_dst_port(lconf, mbuf, mbuf->port);
233 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
234 || defined RTE_ARCH_PPC_64
235 process_packet(mbuf, &mbuf->port);
238 struct rte_ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf,
239 struct rte_ether_hdr *);
240 #ifdef DO_RFC_1812_CHECKS
241 struct rte_ipv4_hdr *ipv4_hdr;
242 if (RTE_ETH_IS_IPV4_HDR(mbuf->packet_type)) {
243 /* Handle IPv4 headers.*/
244 ipv4_hdr = rte_pktmbuf_mtod_offset(mbuf,
245 struct rte_ipv4_hdr *,
246 sizeof(struct rte_ether_hdr));
248 if (is_valid_ipv4_pkt(ipv4_hdr, mbuf->pkt_len)
250 mbuf->port = BAD_PORT;
253 /* Update time to live and header checksum */
254 --(ipv4_hdr->time_to_live);
255 ++(ipv4_hdr->hdr_checksum);
259 *(uint64_t *)ð_hdr->dst_addr = dest_eth_addr[mbuf->port];
262 rte_ether_addr_copy(&ports_eth_addr[mbuf->port],
268 static __rte_always_inline void
269 lpm_event_loop_single(struct l3fwd_event_resources *evt_rsrc,
272 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
273 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
274 evt_rsrc->evq.nb_queues - 1];
275 const uint8_t event_d_id = evt_rsrc->event_d_id;
276 uint8_t enq = 0, deq = 0;
277 struct lcore_conf *lconf;
278 unsigned int lcore_id;
284 lcore_id = rte_lcore_id();
285 lconf = &lcore_conf[lcore_id];
287 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
288 while (!force_quit) {
289 deq = rte_event_dequeue_burst(event_d_id, event_p_id, &ev, 1,
294 if (lpm_process_event_pkt(lconf, ev.mbuf) == BAD_PORT) {
295 rte_pktmbuf_free(ev.mbuf);
299 if (flags & L3FWD_EVENT_TX_ENQ) {
300 ev.queue_id = tx_q_id;
301 ev.op = RTE_EVENT_OP_FORWARD;
303 enq = rte_event_enqueue_burst(
304 event_d_id, event_p_id, &ev, 1);
305 } while (!enq && !force_quit);
308 if (flags & L3FWD_EVENT_TX_DIRECT) {
309 rte_event_eth_tx_adapter_txq_set(ev.mbuf, 0);
311 enq = rte_event_eth_tx_adapter_enqueue(
312 event_d_id, event_p_id, &ev, 1, 0);
313 } while (!enq && !force_quit);
317 l3fwd_event_worker_cleanup(event_d_id, event_p_id, &ev, enq, deq, 0);
320 static __rte_always_inline void
321 lpm_event_loop_burst(struct l3fwd_event_resources *evt_rsrc,
324 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
325 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
326 evt_rsrc->evq.nb_queues - 1];
327 const uint8_t event_d_id = evt_rsrc->event_d_id;
328 const uint16_t deq_len = evt_rsrc->deq_depth;
329 struct rte_event events[MAX_PKT_BURST];
330 int i, nb_enq = 0, nb_deq = 0;
331 struct lcore_conf *lconf;
332 unsigned int lcore_id;
337 lcore_id = rte_lcore_id();
339 lconf = &lcore_conf[lcore_id];
341 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
343 while (!force_quit) {
344 /* Read events from RX queues */
345 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
352 for (i = 0; i < nb_deq; i++) {
353 if (flags & L3FWD_EVENT_TX_ENQ) {
354 events[i].queue_id = tx_q_id;
355 events[i].op = RTE_EVENT_OP_FORWARD;
358 if (flags & L3FWD_EVENT_TX_DIRECT)
359 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
362 lpm_process_event_pkt(lconf, events[i].mbuf);
365 if (flags & L3FWD_EVENT_TX_ENQ) {
366 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
368 while (nb_enq < nb_deq && !force_quit)
369 nb_enq += rte_event_enqueue_burst(event_d_id,
370 event_p_id, events + nb_enq,
374 if (flags & L3FWD_EVENT_TX_DIRECT) {
375 nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
376 event_p_id, events, nb_deq, 0);
377 while (nb_enq < nb_deq && !force_quit)
378 nb_enq += rte_event_eth_tx_adapter_enqueue(
379 event_d_id, event_p_id,
385 l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
389 static __rte_always_inline void
390 lpm_event_loop(struct l3fwd_event_resources *evt_rsrc,
393 if (flags & L3FWD_EVENT_SINGLE)
394 lpm_event_loop_single(evt_rsrc, flags);
395 if (flags & L3FWD_EVENT_BURST)
396 lpm_event_loop_burst(evt_rsrc, flags);
400 lpm_event_main_loop_tx_d(__rte_unused void *dummy)
402 struct l3fwd_event_resources *evt_rsrc =
403 l3fwd_get_eventdev_rsrc();
405 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_SINGLE);
410 lpm_event_main_loop_tx_d_burst(__rte_unused void *dummy)
412 struct l3fwd_event_resources *evt_rsrc =
413 l3fwd_get_eventdev_rsrc();
415 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_BURST);
420 lpm_event_main_loop_tx_q(__rte_unused void *dummy)
422 struct l3fwd_event_resources *evt_rsrc =
423 l3fwd_get_eventdev_rsrc();
425 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_SINGLE);
430 lpm_event_main_loop_tx_q_burst(__rte_unused void *dummy)
432 struct l3fwd_event_resources *evt_rsrc =
433 l3fwd_get_eventdev_rsrc();
435 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_BURST);
439 static __rte_always_inline void
440 lpm_process_event_vector(struct rte_event_vector *vec, struct lcore_conf *lconf)
442 struct rte_mbuf **mbufs = vec->mbufs;
445 /* Process first packet to init vector attributes */
446 lpm_process_event_pkt(lconf, mbufs[0]);
447 if (vec->attr_valid) {
448 if (mbufs[0]->port != BAD_PORT)
449 vec->port = mbufs[0]->port;
454 for (i = 1; i < vec->nb_elem; i++) {
455 lpm_process_event_pkt(lconf, mbufs[i]);
456 event_vector_attr_validate(vec, mbufs[i]);
460 /* Same eventdev loop for single and burst of vector */
461 static __rte_always_inline void
462 lpm_event_loop_vector(struct l3fwd_event_resources *evt_rsrc,
465 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
466 const uint8_t tx_q_id =
467 evt_rsrc->evq.event_q_id[evt_rsrc->evq.nb_queues - 1];
468 const uint8_t event_d_id = evt_rsrc->event_d_id;
469 const uint16_t deq_len = evt_rsrc->deq_depth;
470 struct rte_event events[MAX_PKT_BURST];
471 int i, nb_enq = 0, nb_deq = 0;
472 struct lcore_conf *lconf;
473 unsigned int lcore_id;
478 lcore_id = rte_lcore_id();
479 lconf = &lcore_conf[lcore_id];
481 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
483 while (!force_quit) {
484 /* Read events from RX queues */
485 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id, events,
492 for (i = 0; i < nb_deq; i++) {
493 if (flags & L3FWD_EVENT_TX_ENQ) {
494 events[i].queue_id = tx_q_id;
495 events[i].op = RTE_EVENT_OP_FORWARD;
498 lpm_process_event_vector(events[i].vec, lconf);
500 if (flags & L3FWD_EVENT_TX_DIRECT)
501 event_vector_txq_set(events[i].vec, 0);
504 if (flags & L3FWD_EVENT_TX_ENQ) {
505 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
507 while (nb_enq < nb_deq && !force_quit)
508 nb_enq += rte_event_enqueue_burst(
509 event_d_id, event_p_id, events + nb_enq,
513 if (flags & L3FWD_EVENT_TX_DIRECT) {
514 nb_enq = rte_event_eth_tx_adapter_enqueue(
515 event_d_id, event_p_id, events, nb_deq, 0);
516 while (nb_enq < nb_deq && !force_quit)
517 nb_enq += rte_event_eth_tx_adapter_enqueue(
518 event_d_id, event_p_id, events + nb_enq,
523 l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
528 lpm_event_main_loop_tx_d_vector(__rte_unused void *dummy)
530 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
532 lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
537 lpm_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy)
539 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
541 lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
546 lpm_event_main_loop_tx_q_vector(__rte_unused void *dummy)
548 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
550 lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
555 lpm_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy)
557 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
559 lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
564 setup_lpm(const int socketid)
566 struct rte_eth_dev_info dev_info;
567 struct rte_lpm6_config config;
568 struct rte_lpm_config config_ipv4;
572 char abuf[INET6_ADDRSTRLEN];
574 /* create the LPM table */
575 config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
576 config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
577 config_ipv4.flags = 0;
578 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
579 ipv4_l3fwd_lpm_lookup_struct[socketid] =
580 rte_lpm_create(s, socketid, &config_ipv4);
581 if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
582 rte_exit(EXIT_FAILURE,
583 "Unable to create the l3fwd LPM table on socket %d\n",
586 /* populate the LPM table */
587 for (i = 0; i < route_num_v4; i++) {
590 /* skip unused ports */
591 if ((1 << route_base_v4[i].if_out &
592 enabled_port_mask) == 0)
595 rte_eth_dev_info_get(route_base_v4[i].if_out,
597 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
599 route_base_v4[i].depth,
600 route_base_v4[i].if_out);
604 rte_exit(EXIT_FAILURE,
605 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
609 in.s_addr = htonl(route_base_v4[i].ip);
610 printf("LPM: Adding route %s / %d (%d) [%s]\n",
611 inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
612 route_base_v4[i].depth,
613 route_base_v4[i].if_out, dev_info.device->name);
616 /* create the LPM6 table */
617 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
619 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
620 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
622 ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
624 if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL) {
626 rte_exit(EXIT_FAILURE,
627 "Unable to create the l3fwd LPM table on socket %d\n",
631 /* populate the LPM table */
632 for (i = 0; i < route_num_v6; i++) {
634 /* skip unused ports */
635 if ((1 << route_base_v6[i].if_out &
636 enabled_port_mask) == 0)
639 rte_eth_dev_info_get(route_base_v6[i].if_out,
641 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
642 route_base_v6[i].ip_8,
643 route_base_v6[i].depth,
644 route_base_v6[i].if_out);
648 rte_exit(EXIT_FAILURE,
649 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
653 printf("LPM: Adding route %s / %d (%d) [%s]\n",
654 inet_ntop(AF_INET6, route_base_v6[i].ip_8, abuf,
656 route_base_v6[i].depth,
657 route_base_v6[i].if_out, dev_info.device->name);
662 lpm_check_ptype(int portid)
665 int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
666 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
668 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
672 uint32_t ptypes[ret];
674 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
675 for (i = 0; i < ret; ++i) {
676 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
678 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
682 if (ptype_l3_ipv4 == 0)
683 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
685 if (ptype_l3_ipv6 == 0)
686 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
688 if (ptype_l3_ipv4 && ptype_l3_ipv6)
696 lpm_parse_ptype(struct rte_mbuf *m)
698 struct rte_ether_hdr *eth_hdr;
699 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
702 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
703 ether_type = eth_hdr->ether_type;
704 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
705 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
706 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
707 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
709 m->packet_type = packet_type;
713 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
714 struct rte_mbuf *pkts[], uint16_t nb_pkts,
715 uint16_t max_pkts __rte_unused,
716 void *user_param __rte_unused)
720 if (unlikely(nb_pkts == 0))
722 rte_prefetch0(rte_pktmbuf_mtod(pkts[0], struct ether_hdr *));
723 for (i = 0; i < (unsigned int) (nb_pkts - 1); ++i) {
724 rte_prefetch0(rte_pktmbuf_mtod(pkts[i+1],
725 struct ether_hdr *));
726 lpm_parse_ptype(pkts[i]);
728 lpm_parse_ptype(pkts[i]);
733 /* Return ipv4/ipv6 lpm fwd lookup struct. */
735 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
737 return ipv4_l3fwd_lpm_lookup_struct[socketid];
741 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
743 return ipv6_l3fwd_lpm_lookup_struct[socketid];