1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2021 Intel Corporation
8 #include <sys/socket.h>
15 #if defined RTE_ARCH_X86
16 #include "l3fwd_sse.h"
17 #elif defined __ARM_NEON
18 #include "l3fwd_neon.h"
19 #elif defined RTE_ARCH_PPC_64
20 #include "l3fwd_altivec.h"
22 #include "l3fwd_event.h"
23 #include "l3fwd_route.h"
25 /* Configure how many packets ahead to prefetch for fib. */
26 #define FIB_PREFETCH_OFFSET 4
28 /* A non-existent portid is needed to denote a default hop for fib. */
29 #define FIB_DEFAULT_HOP 999
32 * If the machine has SSE, NEON or PPC 64 then multiple packets
33 * can be sent at once if not only single packets will be sent
35 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
36 || defined RTE_ARCH_PPC_64
37 #define FIB_SEND_MULTI
40 static struct rte_fib *ipv4_l3fwd_fib_lookup_struct[NB_SOCKETS];
41 static struct rte_fib6 *ipv6_l3fwd_fib_lookup_struct[NB_SOCKETS];
43 /* Parse packet type and ip address. */
45 fib_parse_packet(struct rte_mbuf *mbuf,
46 uint32_t *ipv4, uint32_t *ipv4_cnt,
47 uint8_t ipv6[RTE_FIB6_IPV6_ADDR_SIZE],
48 uint32_t *ipv6_cnt, uint8_t *ip_type)
50 struct rte_ether_hdr *eth_hdr;
51 struct rte_ipv4_hdr *ipv4_hdr;
52 struct rte_ipv6_hdr *ipv6_hdr;
54 eth_hdr = rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr *);
56 if (mbuf->packet_type & RTE_PTYPE_L3_IPV4) {
57 ipv4_hdr = (struct rte_ipv4_hdr *)(eth_hdr + 1);
58 *ipv4 = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
59 /* Store type of packet in type_arr (IPv4=1, IPv6=0). */
65 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
66 rte_mov16(ipv6, (const uint8_t *)ipv6_hdr->dst_addr);
73 * If the machine does not have SSE, NEON or PPC 64 then the packets
74 * are sent one at a time using send_single_packet()
76 #if !defined FIB_SEND_MULTI
78 fib_send_single(int nb_tx, struct lcore_conf *qconf,
79 struct rte_mbuf **pkts_burst, uint16_t hops[nb_tx])
82 struct rte_ether_hdr *eth_hdr;
84 for (j = 0; j < nb_tx; j++) {
85 /* Run rfc1812 if packet is ipv4 and checks enabled. */
86 #if defined DO_RFC_1812_CHECKS
87 rfc1812_process((struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(
88 pkts_burst[j], struct rte_ether_hdr *) + 1),
89 &hops[j], pkts_burst[j]->packet_type);
92 /* Set MAC addresses. */
93 eth_hdr = rte_pktmbuf_mtod(pkts_burst[j],
94 struct rte_ether_hdr *);
95 *(uint64_t *)ð_hdr->d_addr = dest_eth_addr[hops[j]];
96 rte_ether_addr_copy(&ports_eth_addr[hops[j]],
99 /* Send single packet. */
100 send_single_packet(qconf, pkts_burst[j], hops[j]);
105 /* Bulk parse, fib lookup and send. */
107 fib_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
108 uint16_t portid, struct lcore_conf *qconf)
110 uint32_t ipv4_arr[nb_rx];
111 uint8_t ipv6_arr[nb_rx][RTE_FIB6_IPV6_ADDR_SIZE];
112 uint16_t hops[nb_rx];
113 uint64_t hopsv4[nb_rx], hopsv6[nb_rx];
114 uint8_t type_arr[nb_rx];
115 uint32_t ipv4_cnt = 0, ipv6_cnt = 0;
116 uint32_t ipv4_arr_assem = 0, ipv6_arr_assem = 0;
120 /* Prefetch first packets. */
121 for (i = 0; i < FIB_PREFETCH_OFFSET && i < nb_rx; i++)
122 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[i], void *));
124 /* Parse packet info and prefetch. */
125 for (i = 0; i < (nb_rx - FIB_PREFETCH_OFFSET); i++) {
126 /* Prefetch packet. */
127 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
128 i + FIB_PREFETCH_OFFSET], void *));
129 fib_parse_packet(pkts_burst[i],
130 &ipv4_arr[ipv4_cnt], &ipv4_cnt,
131 ipv6_arr[ipv6_cnt], &ipv6_cnt,
135 /* Parse remaining packet info. */
136 for (; i < nb_rx; i++)
137 fib_parse_packet(pkts_burst[i],
138 &ipv4_arr[ipv4_cnt], &ipv4_cnt,
139 ipv6_arr[ipv6_cnt], &ipv6_cnt,
142 /* Lookup IPv4 hops if IPv4 packets are present. */
143 if (likely(ipv4_cnt > 0))
144 rte_fib_lookup_bulk(qconf->ipv4_lookup_struct,
145 ipv4_arr, hopsv4, ipv4_cnt);
147 /* Lookup IPv6 hops if IPv6 packets are present. */
149 rte_fib6_lookup_bulk(qconf->ipv6_lookup_struct,
150 ipv6_arr, hopsv6, ipv6_cnt);
152 /* Add IPv4 and IPv6 hops to one array depending on type. */
153 for (i = 0; i < nb_rx; i++) {
155 nh = (uint16_t)hopsv4[ipv4_arr_assem++];
157 nh = (uint16_t)hopsv6[ipv6_arr_assem++];
158 hops[i] = nh != FIB_DEFAULT_HOP ? nh : portid;
161 #if defined FIB_SEND_MULTI
162 send_packets_multi(qconf, pkts_burst, hops, nb_rx);
164 fib_send_single(nb_rx, qconf, pkts_burst, hops);
168 /* Main fib processing loop. */
170 fib_main_loop(__rte_unused void *dummy)
172 struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
173 unsigned int lcore_id;
174 uint64_t prev_tsc, diff_tsc, cur_tsc;
178 struct lcore_conf *qconf;
179 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
180 US_PER_S * BURST_TX_DRAIN_US;
182 lcore_id = rte_lcore_id();
183 qconf = &lcore_conf[lcore_id];
185 if (qconf->n_rx_queue == 0) {
186 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
190 RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
192 for (i = 0; i < qconf->n_rx_queue; i++) {
194 portid = qconf->rx_queue_list[i].port_id;
195 queueid = qconf->rx_queue_list[i].queue_id;
197 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
198 lcore_id, portid, queueid);
201 cur_tsc = rte_rdtsc();
204 while (!force_quit) {
206 /* TX burst queue drain. */
207 diff_tsc = cur_tsc - prev_tsc;
208 if (unlikely(diff_tsc > drain_tsc)) {
210 for (i = 0; i < qconf->n_tx_port; ++i) {
211 portid = qconf->tx_port_id[i];
212 if (qconf->tx_mbufs[portid].len == 0)
215 qconf->tx_mbufs[portid].len,
217 qconf->tx_mbufs[portid].len = 0;
223 /* Read packet from RX queues. */
224 for (i = 0; i < qconf->n_rx_queue; ++i) {
225 portid = qconf->rx_queue_list[i].port_id;
226 queueid = qconf->rx_queue_list[i].queue_id;
227 nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
232 /* Use fib to lookup port IDs and transmit them. */
233 fib_send_packets(nb_rx, pkts_burst, portid, qconf);
236 cur_tsc = rte_rdtsc();
242 /* One eventdev loop for single and burst using fib. */
243 static __rte_always_inline void
244 fib_event_loop(struct l3fwd_event_resources *evt_rsrc,
247 const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
248 const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
249 evt_rsrc->evq.nb_queues - 1];
250 const uint8_t event_d_id = evt_rsrc->event_d_id;
251 const uint16_t deq_len = evt_rsrc->deq_depth;
252 struct rte_event events[MAX_PKT_BURST];
253 struct lcore_conf *lconf;
254 unsigned int lcore_id;
255 int nb_enq, nb_deq, i;
257 uint32_t ipv4_arr[MAX_PKT_BURST];
258 uint8_t ipv6_arr[MAX_PKT_BURST][RTE_FIB6_IPV6_ADDR_SIZE];
259 uint64_t hopsv4[MAX_PKT_BURST], hopsv6[MAX_PKT_BURST];
261 uint8_t type_arr[MAX_PKT_BURST];
262 uint32_t ipv4_cnt, ipv6_cnt;
263 uint32_t ipv4_arr_assem, ipv6_arr_assem;
268 lcore_id = rte_lcore_id();
270 lconf = &lcore_conf[lcore_id];
272 RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
274 while (!force_quit) {
275 /* Read events from RX queues. */
276 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
283 /* Reset counters. */
289 /* Prefetch first packets. */
290 for (i = 0; i < FIB_PREFETCH_OFFSET && i < nb_deq; i++)
291 rte_prefetch0(rte_pktmbuf_mtod(events[i].mbuf, void *));
293 /* Parse packet info and prefetch. */
294 for (i = 0; i < (nb_deq - FIB_PREFETCH_OFFSET); i++) {
295 if (flags & L3FWD_EVENT_TX_ENQ) {
296 events[i].queue_id = tx_q_id;
297 events[i].op = RTE_EVENT_OP_FORWARD;
300 if (flags & L3FWD_EVENT_TX_DIRECT)
301 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
304 /* Prefetch packet. */
305 rte_prefetch0(rte_pktmbuf_mtod(events[
306 i + FIB_PREFETCH_OFFSET].mbuf,
309 fib_parse_packet(events[i].mbuf,
310 &ipv4_arr[ipv4_cnt], &ipv4_cnt,
311 ipv6_arr[ipv6_cnt], &ipv6_cnt,
315 /* Parse remaining packet info. */
316 for (; i < nb_deq; i++) {
317 if (flags & L3FWD_EVENT_TX_ENQ) {
318 events[i].queue_id = tx_q_id;
319 events[i].op = RTE_EVENT_OP_FORWARD;
322 if (flags & L3FWD_EVENT_TX_DIRECT)
323 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
326 fib_parse_packet(events[i].mbuf,
327 &ipv4_arr[ipv4_cnt], &ipv4_cnt,
328 ipv6_arr[ipv6_cnt], &ipv6_cnt,
332 /* Lookup IPv4 hops if IPv4 packets are present. */
333 if (likely(ipv4_cnt > 0))
334 rte_fib_lookup_bulk(lconf->ipv4_lookup_struct,
335 ipv4_arr, hopsv4, ipv4_cnt);
337 /* Lookup IPv6 hops if IPv6 packets are present. */
339 rte_fib6_lookup_bulk(lconf->ipv6_lookup_struct,
340 ipv6_arr, hopsv6, ipv6_cnt);
342 /* Assign ports looked up in fib depending on IPv4 or IPv6 */
343 for (i = 0; i < nb_deq; i++) {
345 nh = (uint16_t)hopsv4[ipv4_arr_assem++];
347 nh = (uint16_t)hopsv6[ipv6_arr_assem++];
348 if (nh != FIB_DEFAULT_HOP)
349 events[i].mbuf->port = nh;
352 if (flags & L3FWD_EVENT_TX_ENQ) {
353 nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
355 while (nb_enq < nb_deq && !force_quit)
356 nb_enq += rte_event_enqueue_burst(event_d_id,
357 event_p_id, events + nb_enq,
361 if (flags & L3FWD_EVENT_TX_DIRECT) {
362 nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
363 event_p_id, events, nb_deq, 0);
364 while (nb_enq < nb_deq && !force_quit)
365 nb_enq += rte_event_eth_tx_adapter_enqueue(
366 event_d_id, event_p_id,
374 fib_event_main_loop_tx_d(__rte_unused void *dummy)
376 struct l3fwd_event_resources *evt_rsrc =
377 l3fwd_get_eventdev_rsrc();
379 fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
384 fib_event_main_loop_tx_d_burst(__rte_unused void *dummy)
386 struct l3fwd_event_resources *evt_rsrc =
387 l3fwd_get_eventdev_rsrc();
389 fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
394 fib_event_main_loop_tx_q(__rte_unused void *dummy)
396 struct l3fwd_event_resources *evt_rsrc =
397 l3fwd_get_eventdev_rsrc();
399 fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ);
404 fib_event_main_loop_tx_q_burst(__rte_unused void *dummy)
406 struct l3fwd_event_resources *evt_rsrc =
407 l3fwd_get_eventdev_rsrc();
409 fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ);
413 /* Function to setup fib. */
415 setup_fib(const int socketid)
417 struct rte_fib6_conf config;
418 struct rte_fib_conf config_ipv4;
422 char abuf[INET6_ADDRSTRLEN];
424 /* Create the fib IPv4 table. */
425 config_ipv4.type = RTE_FIB_DIR24_8;
426 config_ipv4.max_routes = (1 << 16);
427 config_ipv4.default_nh = FIB_DEFAULT_HOP;
428 config_ipv4.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
429 config_ipv4.dir24_8.num_tbl8 = (1 << 15);
430 snprintf(s, sizeof(s), "IPV4_L3FWD_FIB_%d", socketid);
431 ipv4_l3fwd_fib_lookup_struct[socketid] =
432 rte_fib_create(s, socketid, &config_ipv4);
433 if (ipv4_l3fwd_fib_lookup_struct[socketid] == NULL)
434 rte_exit(EXIT_FAILURE,
435 "Unable to create the l3fwd FIB table on socket %d\n",
438 /* Populate the fib ipv4 table. */
439 for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
442 /* Skip unused ports. */
443 if ((1 << ipv4_l3fwd_route_array[i].if_out &
444 enabled_port_mask) == 0)
447 ret = rte_fib_add(ipv4_l3fwd_fib_lookup_struct[socketid],
448 ipv4_l3fwd_route_array[i].ip,
449 ipv4_l3fwd_route_array[i].depth,
450 ipv4_l3fwd_route_array[i].if_out);
453 rte_exit(EXIT_FAILURE,
454 "Unable to add entry %u to the l3fwd FIB table on socket %d\n",
458 in.s_addr = htonl(ipv4_l3fwd_route_array[i].ip);
459 if (inet_ntop(AF_INET, &in, abuf, sizeof(abuf)) != NULL) {
460 printf("FIB: Adding route %s / %d (%d)\n",
462 ipv4_l3fwd_route_array[i].depth,
463 ipv4_l3fwd_route_array[i].if_out);
465 printf("FIB: IPv4 route added to port %d\n",
466 ipv4_l3fwd_route_array[i].if_out);
470 /* Create the fib IPv6 table. */
471 snprintf(s, sizeof(s), "IPV6_L3FWD_FIB_%d", socketid);
473 config.type = RTE_FIB6_TRIE;
474 config.max_routes = (1 << 16) - 1;
475 config.default_nh = FIB_DEFAULT_HOP;
476 config.trie.nh_sz = RTE_FIB6_TRIE_4B;
477 config.trie.num_tbl8 = (1 << 15);
478 ipv6_l3fwd_fib_lookup_struct[socketid] = rte_fib6_create(s, socketid,
480 if (ipv6_l3fwd_fib_lookup_struct[socketid] == NULL)
481 rte_exit(EXIT_FAILURE,
482 "Unable to create the l3fwd FIB table on socket %d\n",
485 /* Populate the fib IPv6 table. */
486 for (i = 0; i < RTE_DIM(ipv6_l3fwd_route_array); i++) {
488 /* Skip unused ports. */
489 if ((1 << ipv6_l3fwd_route_array[i].if_out &
490 enabled_port_mask) == 0)
493 ret = rte_fib6_add(ipv6_l3fwd_fib_lookup_struct[socketid],
494 ipv6_l3fwd_route_array[i].ip,
495 ipv6_l3fwd_route_array[i].depth,
496 ipv6_l3fwd_route_array[i].if_out);
499 rte_exit(EXIT_FAILURE,
500 "Unable to add entry %u to the l3fwd FIB table on socket %d\n",
504 if (inet_ntop(AF_INET6, ipv6_l3fwd_route_array[i].ip,
505 abuf, sizeof(abuf)) != NULL) {
506 printf("FIB: Adding route %s / %d (%d)\n",
508 ipv6_l3fwd_route_array[i].depth,
509 ipv6_l3fwd_route_array[i].if_out);
511 printf("FIB: IPv6 route added to port %d\n",
512 ipv6_l3fwd_route_array[i].if_out);
517 /* Return ipv4 fib lookup struct. */
519 fib_get_ipv4_l3fwd_lookup_struct(const int socketid)
521 return ipv4_l3fwd_fib_lookup_struct[socketid];
524 /* Return ipv6 fib lookup struct. */
526 fib_get_ipv6_l3fwd_lookup_struct(const int socketid)
528 return ipv6_l3fwd_fib_lookup_struct[socketid];