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_common.h"
32 #include "l3fwd_event.h"
34 #include "lpm_route_parse.c"
36 #define IPV4_L3FWD_LPM_MAX_RULES 1024
37 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
38 #define IPV6_L3FWD_LPM_MAX_RULES 1024
39 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
41 static struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
42 static struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
44 /* Performing LPM-based lookups. 8< */
45 static inline uint16_t
46 lpm_get_ipv4_dst_port(const struct rte_ipv4_hdr *ipv4_hdr,
48 struct rte_lpm *ipv4_l3fwd_lookup_struct)
50 uint32_t dst_ip = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
53 if (rte_lpm_lookup(ipv4_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
58 /* >8 End of performing LPM-based lookups. */
60 static inline uint16_t
61 lpm_get_ipv6_dst_port(const struct rte_ipv6_hdr *ipv6_hdr,
63 struct rte_lpm6 *ipv6_l3fwd_lookup_struct)
65 const uint8_t *dst_ip = ipv6_hdr->dst_addr;
68 if (rte_lpm6_lookup(ipv6_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
74 static __rte_always_inline uint16_t
75 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
78 struct rte_ipv6_hdr *ipv6_hdr;
79 struct rte_ipv4_hdr *ipv4_hdr;
80 struct rte_ether_hdr *eth_hdr;
82 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
84 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
85 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
87 return lpm_get_ipv4_dst_port(ipv4_hdr, portid,
88 qconf->ipv4_lookup_struct);
89 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
91 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
92 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
94 return lpm_get_ipv6_dst_port(ipv6_hdr, portid,
95 qconf->ipv6_lookup_struct);
102 * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
103 * precalculated. If packet is ipv6 dst_addr is taken directly from packet
104 * header and dst_ipv4 value is not used.
106 static __rte_always_inline uint16_t
107 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
108 uint32_t dst_ipv4, uint16_t portid)
111 struct rte_ipv6_hdr *ipv6_hdr;
112 struct rte_ether_hdr *eth_hdr;
114 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
115 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
116 dst_ipv4, &next_hop) == 0)
117 ? next_hop : portid);
119 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
121 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
122 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
124 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
125 ipv6_hdr->dst_addr, &next_hop) == 0)
126 ? next_hop : portid);
133 #if defined(RTE_ARCH_X86)
134 #include "l3fwd_lpm_sse.h"
135 #elif defined __ARM_NEON
136 #include "l3fwd_lpm_neon.h"
137 #elif defined(RTE_ARCH_PPC_64)
138 #include "l3fwd_lpm_altivec.h"
140 #include "l3fwd_lpm.h"
143 /* main processing loop */
145 lpm_main_loop(__rte_unused void *dummy)
147 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
149 uint64_t prev_tsc, diff_tsc, cur_tsc;
153 struct lcore_conf *qconf;
154 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
155 US_PER_S * BURST_TX_DRAIN_US;
157 lcore_id = rte_lcore_id();
158 qconf = &lcore_conf[lcore_id];
160 const uint16_t n_rx_q = qconf->n_rx_queue;
161 const uint16_t n_tx_p = qconf->n_tx_port;
163 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
167 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
169 for (i = 0; i < n_rx_q; i++) {
171 portid = qconf->rx_queue_list[i].port_id;
172 queueid = qconf->rx_queue_list[i].queue_id;
174 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
175 lcore_id, portid, queueid);
178 cur_tsc = rte_rdtsc();
181 while (!force_quit) {
184 * TX burst queue drain
186 diff_tsc = cur_tsc - prev_tsc;
187 if (unlikely(diff_tsc > drain_tsc)) {
189 for (i = 0; i < n_tx_p; ++i) {
190 portid = qconf->tx_port_id[i];
191 if (qconf->tx_mbufs[portid].len == 0)
194 qconf->tx_mbufs[portid].len,
196 qconf->tx_mbufs[portid].len = 0;
203 * Read packet from RX queues
205 for (i = 0; i < n_rx_q; ++i) {
206 portid = qconf->rx_queue_list[i].port_id;
207 queueid = qconf->rx_queue_list[i].queue_id;
208 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
213 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
214 || defined RTE_ARCH_PPC_64
215 l3fwd_lpm_send_packets(nb_rx, pkts_burst,
218 l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
223 cur_tsc = rte_rdtsc();
229 static __rte_always_inline uint16_t
230 lpm_process_event_pkt(const struct lcore_conf *lconf, struct rte_mbuf *mbuf)
232 mbuf->port = lpm_get_dst_port(lconf, mbuf, mbuf->port);
234 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
235 || defined RTE_ARCH_PPC_64
236 process_packet(mbuf, &mbuf->port);
239 struct rte_ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf,
240 struct rte_ether_hdr *);
243 *(uint64_t *)ð_hdr->dst_addr = dest_eth_addr[mbuf->port];
246 rte_ether_addr_copy(&ports_eth_addr[mbuf->port],
249 rfc1812_process(rte_pktmbuf_mtod_offset(mbuf, struct rte_ipv4_hdr *,
250 sizeof(struct rte_ether_hdr)),
251 &mbuf->port, mbuf->packet_type);
256 static __rte_always_inline void
257 lpm_event_loop_single(struct l3fwd_event_resources *evt_rsrc,
260 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
261 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
262 evt_rsrc->evq.nb_queues - 1];
263 const uint8_t event_d_id = evt_rsrc->event_d_id;
264 uint8_t enq = 0, deq = 0;
265 struct lcore_conf *lconf;
266 unsigned int lcore_id;
272 lcore_id = rte_lcore_id();
273 lconf = &lcore_conf[lcore_id];
275 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
276 while (!force_quit) {
277 deq = rte_event_dequeue_burst(event_d_id, event_p_id, &ev, 1,
282 if (lpm_process_event_pkt(lconf, ev.mbuf) == BAD_PORT) {
283 rte_pktmbuf_free(ev.mbuf);
287 if (flags & L3FWD_EVENT_TX_ENQ) {
288 ev.queue_id = tx_q_id;
289 ev.op = RTE_EVENT_OP_FORWARD;
291 enq = rte_event_enqueue_burst(
292 event_d_id, event_p_id, &ev, 1);
293 } while (!enq && !force_quit);
296 if (flags & L3FWD_EVENT_TX_DIRECT) {
297 rte_event_eth_tx_adapter_txq_set(ev.mbuf, 0);
299 enq = rte_event_eth_tx_adapter_enqueue(
300 event_d_id, event_p_id, &ev, 1, 0);
301 } while (!enq && !force_quit);
305 l3fwd_event_worker_cleanup(event_d_id, event_p_id, &ev, enq, deq, 0);
308 static __rte_always_inline void
309 lpm_event_loop_burst(struct l3fwd_event_resources *evt_rsrc,
312 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
313 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
314 evt_rsrc->evq.nb_queues - 1];
315 const uint8_t event_d_id = evt_rsrc->event_d_id;
316 const uint16_t deq_len = evt_rsrc->deq_depth;
317 struct rte_event events[MAX_PKT_BURST];
318 int i, nb_enq = 0, nb_deq = 0;
319 struct lcore_conf *lconf;
320 unsigned int lcore_id;
325 lcore_id = rte_lcore_id();
327 lconf = &lcore_conf[lcore_id];
329 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
331 while (!force_quit) {
332 /* Read events from RX queues */
333 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
340 for (i = 0; i < nb_deq; i++) {
341 if (flags & L3FWD_EVENT_TX_ENQ) {
342 events[i].queue_id = tx_q_id;
343 events[i].op = RTE_EVENT_OP_FORWARD;
346 if (flags & L3FWD_EVENT_TX_DIRECT)
347 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
350 lpm_process_event_pkt(lconf, events[i].mbuf);
353 if (flags & L3FWD_EVENT_TX_ENQ) {
354 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
356 while (nb_enq < nb_deq && !force_quit)
357 nb_enq += rte_event_enqueue_burst(event_d_id,
358 event_p_id, events + nb_enq,
362 if (flags & L3FWD_EVENT_TX_DIRECT) {
363 nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
364 event_p_id, events, nb_deq, 0);
365 while (nb_enq < nb_deq && !force_quit)
366 nb_enq += rte_event_eth_tx_adapter_enqueue(
367 event_d_id, event_p_id,
373 l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
377 static __rte_always_inline void
378 lpm_event_loop(struct l3fwd_event_resources *evt_rsrc,
381 if (flags & L3FWD_EVENT_SINGLE)
382 lpm_event_loop_single(evt_rsrc, flags);
383 if (flags & L3FWD_EVENT_BURST)
384 lpm_event_loop_burst(evt_rsrc, flags);
388 lpm_event_main_loop_tx_d(__rte_unused void *dummy)
390 struct l3fwd_event_resources *evt_rsrc =
391 l3fwd_get_eventdev_rsrc();
393 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_SINGLE);
398 lpm_event_main_loop_tx_d_burst(__rte_unused void *dummy)
400 struct l3fwd_event_resources *evt_rsrc =
401 l3fwd_get_eventdev_rsrc();
403 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_BURST);
408 lpm_event_main_loop_tx_q(__rte_unused void *dummy)
410 struct l3fwd_event_resources *evt_rsrc =
411 l3fwd_get_eventdev_rsrc();
413 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_SINGLE);
418 lpm_event_main_loop_tx_q_burst(__rte_unused void *dummy)
420 struct l3fwd_event_resources *evt_rsrc =
421 l3fwd_get_eventdev_rsrc();
423 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_BURST);
427 static __rte_always_inline void
428 lpm_process_event_vector(struct rte_event_vector *vec, struct lcore_conf *lconf)
430 struct rte_mbuf **mbufs = vec->mbufs;
433 /* Process first packet to init vector attributes */
434 lpm_process_event_pkt(lconf, mbufs[0]);
435 if (vec->attr_valid) {
436 if (mbufs[0]->port != BAD_PORT)
437 vec->port = mbufs[0]->port;
442 for (i = 1; i < vec->nb_elem; i++) {
443 lpm_process_event_pkt(lconf, mbufs[i]);
444 event_vector_attr_validate(vec, mbufs[i]);
448 /* Same eventdev loop for single and burst of vector */
449 static __rte_always_inline void
450 lpm_event_loop_vector(struct l3fwd_event_resources *evt_rsrc,
453 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
454 const uint8_t tx_q_id =
455 evt_rsrc->evq.event_q_id[evt_rsrc->evq.nb_queues - 1];
456 const uint8_t event_d_id = evt_rsrc->event_d_id;
457 const uint16_t deq_len = evt_rsrc->deq_depth;
458 struct rte_event events[MAX_PKT_BURST];
459 int i, nb_enq = 0, nb_deq = 0;
460 struct lcore_conf *lconf;
461 unsigned int lcore_id;
466 lcore_id = rte_lcore_id();
467 lconf = &lcore_conf[lcore_id];
469 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
471 while (!force_quit) {
472 /* Read events from RX queues */
473 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id, events,
480 for (i = 0; i < nb_deq; i++) {
481 if (flags & L3FWD_EVENT_TX_ENQ) {
482 events[i].queue_id = tx_q_id;
483 events[i].op = RTE_EVENT_OP_FORWARD;
486 lpm_process_event_vector(events[i].vec, lconf);
488 if (flags & L3FWD_EVENT_TX_DIRECT)
489 event_vector_txq_set(events[i].vec, 0);
492 if (flags & L3FWD_EVENT_TX_ENQ) {
493 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
495 while (nb_enq < nb_deq && !force_quit)
496 nb_enq += rte_event_enqueue_burst(
497 event_d_id, event_p_id, events + nb_enq,
501 if (flags & L3FWD_EVENT_TX_DIRECT) {
502 nb_enq = rte_event_eth_tx_adapter_enqueue(
503 event_d_id, event_p_id, events, nb_deq, 0);
504 while (nb_enq < nb_deq && !force_quit)
505 nb_enq += rte_event_eth_tx_adapter_enqueue(
506 event_d_id, event_p_id, events + nb_enq,
511 l3fwd_event_worker_cleanup(event_d_id, event_p_id, events, nb_enq,
516 lpm_event_main_loop_tx_d_vector(__rte_unused void *dummy)
518 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
520 lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
525 lpm_event_main_loop_tx_d_burst_vector(__rte_unused void *dummy)
527 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
529 lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
534 lpm_event_main_loop_tx_q_vector(__rte_unused void *dummy)
536 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
538 lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
543 lpm_event_main_loop_tx_q_burst_vector(__rte_unused void *dummy)
545 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
547 lpm_event_loop_vector(evt_rsrc, L3FWD_EVENT_TX_ENQ);
552 setup_lpm(const int socketid)
554 struct rte_eth_dev_info dev_info;
555 struct rte_lpm6_config config;
556 struct rte_lpm_config config_ipv4;
560 char abuf[INET6_ADDRSTRLEN];
562 /* create the LPM table */
563 config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
564 config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
565 config_ipv4.flags = 0;
566 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
567 ipv4_l3fwd_lpm_lookup_struct[socketid] =
568 rte_lpm_create(s, socketid, &config_ipv4);
569 if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
570 rte_exit(EXIT_FAILURE,
571 "Unable to create the l3fwd LPM table on socket %d\n",
574 /* populate the LPM table */
575 for (i = 0; i < route_num_v4; i++) {
578 /* skip unused ports */
579 if ((1 << route_base_v4[i].if_out &
580 enabled_port_mask) == 0)
583 rte_eth_dev_info_get(route_base_v4[i].if_out,
585 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
587 route_base_v4[i].depth,
588 route_base_v4[i].if_out);
592 rte_exit(EXIT_FAILURE,
593 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
597 in.s_addr = htonl(route_base_v4[i].ip);
598 printf("LPM: Adding route %s / %d (%d) [%s]\n",
599 inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
600 route_base_v4[i].depth,
601 route_base_v4[i].if_out, dev_info.device->name);
604 /* create the LPM6 table */
605 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
607 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
608 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
610 ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
612 if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL) {
614 rte_exit(EXIT_FAILURE,
615 "Unable to create the l3fwd LPM table on socket %d\n",
619 /* populate the LPM table */
620 for (i = 0; i < route_num_v6; i++) {
622 /* skip unused ports */
623 if ((1 << route_base_v6[i].if_out &
624 enabled_port_mask) == 0)
627 rte_eth_dev_info_get(route_base_v6[i].if_out,
629 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
630 route_base_v6[i].ip_8,
631 route_base_v6[i].depth,
632 route_base_v6[i].if_out);
636 rte_exit(EXIT_FAILURE,
637 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
641 printf("LPM: Adding route %s / %d (%d) [%s]\n",
642 inet_ntop(AF_INET6, route_base_v6[i].ip_8, abuf,
644 route_base_v6[i].depth,
645 route_base_v6[i].if_out, dev_info.device->name);
650 lpm_check_ptype(int portid)
653 int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
654 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
656 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
660 uint32_t ptypes[ret];
662 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
663 for (i = 0; i < ret; ++i) {
664 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
666 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
670 if (ptype_l3_ipv4 == 0)
671 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
673 if (ptype_l3_ipv6 == 0)
674 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
676 if (ptype_l3_ipv4 && ptype_l3_ipv6)
684 lpm_parse_ptype(struct rte_mbuf *m)
686 struct rte_ether_hdr *eth_hdr;
687 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
690 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
691 ether_type = eth_hdr->ether_type;
692 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
693 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
694 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
695 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
697 m->packet_type = packet_type;
701 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
702 struct rte_mbuf *pkts[], uint16_t nb_pkts,
703 uint16_t max_pkts __rte_unused,
704 void *user_param __rte_unused)
708 if (unlikely(nb_pkts == 0))
710 rte_prefetch0(rte_pktmbuf_mtod(pkts[0], struct ether_hdr *));
711 for (i = 0; i < (unsigned int) (nb_pkts - 1); ++i) {
712 rte_prefetch0(rte_pktmbuf_mtod(pkts[i+1],
713 struct ether_hdr *));
714 lpm_parse_ptype(pkts[i]);
716 lpm_parse_ptype(pkts[i]);
721 /* Return ipv4/ipv6 lpm fwd lookup struct. */
723 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
725 return ipv4_l3fwd_lpm_lookup_struct[socketid];
729 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
731 return ipv6_l3fwd_lpm_lookup_struct[socketid];