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 "l3fwd_route.h"
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 static inline uint16_t
44 lpm_get_ipv4_dst_port(const struct rte_ipv4_hdr *ipv4_hdr,
46 struct rte_lpm *ipv4_l3fwd_lookup_struct)
48 uint32_t dst_ip = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
51 if (rte_lpm_lookup(ipv4_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
57 static inline uint16_t
58 lpm_get_ipv6_dst_port(const struct rte_ipv6_hdr *ipv6_hdr,
60 struct rte_lpm6 *ipv6_l3fwd_lookup_struct)
62 const uint8_t *dst_ip = ipv6_hdr->dst_addr;
65 if (rte_lpm6_lookup(ipv6_l3fwd_lookup_struct, dst_ip, &next_hop) == 0)
71 static __rte_always_inline uint16_t
72 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
75 struct rte_ipv6_hdr *ipv6_hdr;
76 struct rte_ipv4_hdr *ipv4_hdr;
77 struct rte_ether_hdr *eth_hdr;
79 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
81 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
82 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
84 return lpm_get_ipv4_dst_port(ipv4_hdr, portid,
85 qconf->ipv4_lookup_struct);
86 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
88 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
89 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
91 return lpm_get_ipv6_dst_port(ipv6_hdr, portid,
92 qconf->ipv6_lookup_struct);
99 * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
100 * precalculated. If packet is ipv6 dst_addr is taken directly from packet
101 * header and dst_ipv4 value is not used.
103 static __rte_always_inline uint16_t
104 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
105 uint32_t dst_ipv4, uint16_t portid)
108 struct rte_ipv6_hdr *ipv6_hdr;
109 struct rte_ether_hdr *eth_hdr;
111 if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
112 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
113 dst_ipv4, &next_hop) == 0)
114 ? next_hop : portid);
116 } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
118 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
119 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
121 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
122 ipv6_hdr->dst_addr, &next_hop) == 0)
123 ? next_hop : portid);
130 #if defined(RTE_ARCH_X86)
131 #include "l3fwd_lpm_sse.h"
132 #elif defined __ARM_NEON
133 #include "l3fwd_lpm_neon.h"
134 #elif defined(RTE_ARCH_PPC_64)
135 #include "l3fwd_lpm_altivec.h"
137 #include "l3fwd_lpm.h"
140 /* main processing loop */
142 lpm_main_loop(__rte_unused void *dummy)
144 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
146 uint64_t prev_tsc, diff_tsc, cur_tsc;
150 struct lcore_conf *qconf;
151 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
152 US_PER_S * BURST_TX_DRAIN_US;
154 lcore_id = rte_lcore_id();
155 qconf = &lcore_conf[lcore_id];
157 if (qconf->n_rx_queue == 0) {
158 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
162 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
164 for (i = 0; i < qconf->n_rx_queue; i++) {
166 portid = qconf->rx_queue_list[i].port_id;
167 queueid = qconf->rx_queue_list[i].queue_id;
169 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
170 lcore_id, portid, queueid);
173 cur_tsc = rte_rdtsc();
176 while (!force_quit) {
179 * TX burst queue drain
181 diff_tsc = cur_tsc - prev_tsc;
182 if (unlikely(diff_tsc > drain_tsc)) {
184 for (i = 0; i < qconf->n_tx_port; ++i) {
185 portid = qconf->tx_port_id[i];
186 if (qconf->tx_mbufs[portid].len == 0)
189 qconf->tx_mbufs[portid].len,
191 qconf->tx_mbufs[portid].len = 0;
198 * Read packet from RX queues
200 for (i = 0; i < qconf->n_rx_queue; ++i) {
201 portid = qconf->rx_queue_list[i].port_id;
202 queueid = qconf->rx_queue_list[i].queue_id;
203 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
208 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
209 || defined RTE_ARCH_PPC_64
210 l3fwd_lpm_send_packets(nb_rx, pkts_burst,
213 l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
218 cur_tsc = rte_rdtsc();
224 static __rte_always_inline uint16_t
225 lpm_process_event_pkt(const struct lcore_conf *lconf, struct rte_mbuf *mbuf)
227 mbuf->port = lpm_get_dst_port(lconf, mbuf, mbuf->port);
229 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
230 || defined RTE_ARCH_PPC_64
231 process_packet(mbuf, &mbuf->port);
234 struct rte_ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbuf,
235 struct rte_ether_hdr *);
236 #ifdef DO_RFC_1812_CHECKS
237 struct rte_ipv4_hdr *ipv4_hdr;
238 if (RTE_ETH_IS_IPV4_HDR(mbuf->packet_type)) {
239 /* Handle IPv4 headers.*/
240 ipv4_hdr = rte_pktmbuf_mtod_offset(mbuf,
241 struct rte_ipv4_hdr *,
242 sizeof(struct rte_ether_hdr));
244 if (is_valid_ipv4_pkt(ipv4_hdr, mbuf->pkt_len)
246 mbuf->port = BAD_PORT;
249 /* Update time to live and header checksum */
250 --(ipv4_hdr->time_to_live);
251 ++(ipv4_hdr->hdr_checksum);
255 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[mbuf->port];
258 rte_ether_addr_copy(&ports_eth_addr[mbuf->port],
264 static __rte_always_inline void
265 lpm_event_loop_single(struct l3fwd_event_resources *evt_rsrc,
268 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
269 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
270 evt_rsrc->evq.nb_queues - 1];
271 const uint8_t event_d_id = evt_rsrc->event_d_id;
272 struct lcore_conf *lconf;
273 unsigned int lcore_id;
279 lcore_id = rte_lcore_id();
280 lconf = &lcore_conf[lcore_id];
282 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
283 while (!force_quit) {
284 if (!rte_event_dequeue_burst(event_d_id, event_p_id, &ev, 1, 0))
287 if (lpm_process_event_pkt(lconf, ev.mbuf) == BAD_PORT) {
288 rte_pktmbuf_free(ev.mbuf);
292 if (flags & L3FWD_EVENT_TX_ENQ) {
293 ev.queue_id = tx_q_id;
294 ev.op = RTE_EVENT_OP_FORWARD;
295 while (rte_event_enqueue_burst(event_d_id, event_p_id,
296 &ev, 1) && !force_quit)
300 if (flags & L3FWD_EVENT_TX_DIRECT) {
301 rte_event_eth_tx_adapter_txq_set(ev.mbuf, 0);
302 while (!rte_event_eth_tx_adapter_enqueue(event_d_id,
303 event_p_id, &ev, 1, 0) &&
310 static __rte_always_inline void
311 lpm_event_loop_burst(struct l3fwd_event_resources *evt_rsrc,
314 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
315 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
316 evt_rsrc->evq.nb_queues - 1];
317 const uint8_t event_d_id = evt_rsrc->event_d_id;
318 const uint16_t deq_len = evt_rsrc->deq_depth;
319 struct rte_event events[MAX_PKT_BURST];
320 struct lcore_conf *lconf;
321 unsigned int lcore_id;
322 int i, nb_enq, nb_deq;
327 lcore_id = rte_lcore_id();
329 lconf = &lcore_conf[lcore_id];
331 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
333 while (!force_quit) {
334 /* Read events from RX queues */
335 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
342 for (i = 0; i < nb_deq; i++) {
343 if (flags & L3FWD_EVENT_TX_ENQ) {
344 events[i].queue_id = tx_q_id;
345 events[i].op = RTE_EVENT_OP_FORWARD;
348 if (flags & L3FWD_EVENT_TX_DIRECT)
349 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
352 lpm_process_event_pkt(lconf, events[i].mbuf);
355 if (flags & L3FWD_EVENT_TX_ENQ) {
356 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
358 while (nb_enq < nb_deq && !force_quit)
359 nb_enq += rte_event_enqueue_burst(event_d_id,
360 event_p_id, events + nb_enq,
364 if (flags & L3FWD_EVENT_TX_DIRECT) {
365 nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
366 event_p_id, events, nb_deq, 0);
367 while (nb_enq < nb_deq && !force_quit)
368 nb_enq += rte_event_eth_tx_adapter_enqueue(
369 event_d_id, event_p_id,
376 static __rte_always_inline void
377 lpm_event_loop(struct l3fwd_event_resources *evt_rsrc,
380 if (flags & L3FWD_EVENT_SINGLE)
381 lpm_event_loop_single(evt_rsrc, flags);
382 if (flags & L3FWD_EVENT_BURST)
383 lpm_event_loop_burst(evt_rsrc, flags);
387 lpm_event_main_loop_tx_d(__rte_unused void *dummy)
389 struct l3fwd_event_resources *evt_rsrc =
390 l3fwd_get_eventdev_rsrc();
392 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_SINGLE);
397 lpm_event_main_loop_tx_d_burst(__rte_unused void *dummy)
399 struct l3fwd_event_resources *evt_rsrc =
400 l3fwd_get_eventdev_rsrc();
402 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT | L3FWD_EVENT_BURST);
407 lpm_event_main_loop_tx_q(__rte_unused void *dummy)
409 struct l3fwd_event_resources *evt_rsrc =
410 l3fwd_get_eventdev_rsrc();
412 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_SINGLE);
417 lpm_event_main_loop_tx_q_burst(__rte_unused void *dummy)
419 struct l3fwd_event_resources *evt_rsrc =
420 l3fwd_get_eventdev_rsrc();
422 lpm_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ | L3FWD_EVENT_BURST);
427 setup_lpm(const int socketid)
429 struct rte_lpm6_config config;
430 struct rte_lpm_config config_ipv4;
434 char abuf[INET6_ADDRSTRLEN];
436 /* create the LPM table */
437 config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
438 config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
439 config_ipv4.flags = 0;
440 snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
441 ipv4_l3fwd_lpm_lookup_struct[socketid] =
442 rte_lpm_create(s, socketid, &config_ipv4);
443 if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
444 rte_exit(EXIT_FAILURE,
445 "Unable to create the l3fwd LPM table on socket %d\n",
448 /* populate the LPM table */
449 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
452 /* skip unused ports */
453 if ((1 << ipv4_l3fwd_route_array[i].if_out &
454 enabled_port_mask) == 0)
457 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
458 ipv4_l3fwd_route_array[i].ip,
459 ipv4_l3fwd_route_array[i].depth,
460 ipv4_l3fwd_route_array[i].if_out);
463 rte_exit(EXIT_FAILURE,
464 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
468 in.s_addr = htonl(ipv4_l3fwd_route_array[i].ip);
469 printf("LPM: Adding route %s / %d (%d)\n",
470 inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
471 ipv4_l3fwd_route_array[i].depth,
472 ipv4_l3fwd_route_array[i].if_out);
475 /* create the LPM6 table */
476 snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
478 config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
479 config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
481 ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
483 if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL)
484 rte_exit(EXIT_FAILURE,
485 "Unable to create the l3fwd LPM table on socket %d\n",
488 /* populate the LPM table */
489 for (i = 0; i < RTE_DIM(ipv6_l3fwd_route_array); i++) {
491 /* skip unused ports */
492 if ((1 << ipv6_l3fwd_route_array[i].if_out &
493 enabled_port_mask) == 0)
496 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
497 ipv6_l3fwd_route_array[i].ip,
498 ipv6_l3fwd_route_array[i].depth,
499 ipv6_l3fwd_route_array[i].if_out);
502 rte_exit(EXIT_FAILURE,
503 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
507 printf("LPM: Adding route %s / %d (%d)\n",
508 inet_ntop(AF_INET6, ipv6_l3fwd_route_array[i].ip,
510 ipv6_l3fwd_route_array[i].depth,
511 ipv6_l3fwd_route_array[i].if_out);
516 lpm_check_ptype(int portid)
519 int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
520 uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
522 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
526 uint32_t ptypes[ret];
528 ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
529 for (i = 0; i < ret; ++i) {
530 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
532 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
536 if (ptype_l3_ipv4 == 0)
537 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
539 if (ptype_l3_ipv6 == 0)
540 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
542 if (ptype_l3_ipv4 && ptype_l3_ipv6)
550 lpm_parse_ptype(struct rte_mbuf *m)
552 struct rte_ether_hdr *eth_hdr;
553 uint32_t packet_type = RTE_PTYPE_UNKNOWN;
556 eth_hdr = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
557 ether_type = eth_hdr->ether_type;
558 if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))
559 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
560 else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))
561 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
563 m->packet_type = packet_type;
567 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
568 struct rte_mbuf *pkts[], uint16_t nb_pkts,
569 uint16_t max_pkts __rte_unused,
570 void *user_param __rte_unused)
574 if (unlikely(nb_pkts == 0))
576 rte_prefetch0(rte_pktmbuf_mtod(pkts[0], struct ether_hdr *));
577 for (i = 0; i < (unsigned int) (nb_pkts - 1); ++i) {
578 rte_prefetch0(rte_pktmbuf_mtod(pkts[i+1],
579 struct ether_hdr *));
580 lpm_parse_ptype(pkts[i]);
582 lpm_parse_ptype(pkts[i]);
587 /* Return ipv4/ipv6 lpm fwd lookup struct. */
589 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
591 return ipv4_l3fwd_lpm_lookup_struct[socketid];
595 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
597 return ipv6_l3fwd_lpm_lookup_struct[socketid];