c7d7689a942b8209a905a92fb6b90bec592ae845
[dpdk.git] / examples / l3fwd / l3fwd_fib.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2021 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <sys/socket.h>
9 #include <arpa/inet.h>
10
11 #include <rte_fib.h>
12 #include <rte_fib6.h>
13
14 #include "l3fwd.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"
21 #endif
22 #include "l3fwd_event.h"
23 #include "l3fwd_route.h"
24
25 /* Configure how many packets ahead to prefetch for fib. */
26 #define FIB_PREFETCH_OFFSET 4
27
28 /* A non-existent portid is needed to denote a default hop for fib. */
29 #define FIB_DEFAULT_HOP 999
30
31 /*
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
34  */
35 #if defined RTE_ARCH_X86 || defined __ARM_NEON \
36                 || defined RTE_ARCH_PPC_64
37 #define FIB_SEND_MULTI
38 #endif
39
40 static struct rte_fib *ipv4_l3fwd_fib_lookup_struct[NB_SOCKETS];
41 static struct rte_fib6 *ipv6_l3fwd_fib_lookup_struct[NB_SOCKETS];
42
43 /* Parse packet type and ip address. */
44 static inline void
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)
49 {
50         struct rte_ether_hdr *eth_hdr;
51         struct rte_ipv4_hdr *ipv4_hdr;
52         struct rte_ipv6_hdr *ipv6_hdr;
53
54         eth_hdr = rte_pktmbuf_mtod(mbuf, struct rte_ether_hdr *);
55         /* IPv4 */
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). */
60                 *ip_type = 1;
61                 (*ipv4_cnt)++;
62         }
63         /* IPv6 */
64         else {
65                 ipv6_hdr = (struct rte_ipv6_hdr *)(eth_hdr + 1);
66                 rte_mov16(ipv6, (const uint8_t *)ipv6_hdr->dst_addr);
67                 *ip_type = 0;
68                 (*ipv6_cnt)++;
69         }
70 }
71
72 /*
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()
75  */
76 #if !defined FIB_SEND_MULTI
77 static inline void
78 fib_send_single(int nb_tx, struct lcore_conf *qconf,
79                 struct rte_mbuf **pkts_burst, uint16_t hops[nb_tx])
80 {
81         int32_t j;
82         struct rte_ether_hdr *eth_hdr;
83
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);
90 #endif
91
92                 /* Set MAC addresses. */
93                 eth_hdr = rte_pktmbuf_mtod(pkts_burst[j],
94                                 struct rte_ether_hdr *);
95                 *(uint64_t *)&eth_hdr->d_addr = dest_eth_addr[hops[j]];
96                 rte_ether_addr_copy(&ports_eth_addr[hops[j]],
97                                 &eth_hdr->s_addr);
98
99                 /* Send single packet. */
100                 send_single_packet(qconf, pkts_burst[j], hops[j]);
101         }
102 }
103 #endif
104
105 /* Bulk parse, fib lookup and send. */
106 static inline void
107 fib_send_packets(int nb_rx, struct rte_mbuf **pkts_burst,
108                 uint16_t portid, struct lcore_conf *qconf)
109 {
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;
117         uint16_t nh;
118         int32_t i;
119
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 *));
123
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,
132                                 &type_arr[i]);
133         }
134
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,
140                                 &type_arr[i]);
141
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);
146
147         /* Lookup IPv6 hops if IPv6 packets are present. */
148         if (ipv6_cnt > 0)
149                 rte_fib6_lookup_bulk(qconf->ipv6_lookup_struct,
150                                 ipv6_arr, hopsv6, ipv6_cnt);
151
152         /* Add IPv4 and IPv6 hops to one array depending on type. */
153         for (i = 0; i < nb_rx; i++) {
154                 if (type_arr[i])
155                         nh = (uint16_t)hopsv4[ipv4_arr_assem++];
156                 else
157                         nh = (uint16_t)hopsv6[ipv6_arr_assem++];
158                 hops[i] = nh != FIB_DEFAULT_HOP ? nh : portid;
159         }
160
161 #if defined FIB_SEND_MULTI
162         send_packets_multi(qconf, pkts_burst, hops, nb_rx);
163 #else
164         fib_send_single(nb_rx, qconf, pkts_burst, hops);
165 #endif
166 }
167
168 /* Main fib processing loop. */
169 int
170 fib_main_loop(__rte_unused void *dummy)
171 {
172         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
173         unsigned int lcore_id;
174         uint64_t prev_tsc, diff_tsc, cur_tsc;
175         int i, nb_rx;
176         uint16_t portid;
177         uint8_t queueid;
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;
181
182         prev_tsc = 0;
183
184         lcore_id = rte_lcore_id();
185         qconf = &lcore_conf[lcore_id];
186
187         if (qconf->n_rx_queue == 0) {
188                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
189                 return 0;
190         }
191
192         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
193
194         for (i = 0; i < qconf->n_rx_queue; i++) {
195
196                 portid = qconf->rx_queue_list[i].port_id;
197                 queueid = qconf->rx_queue_list[i].queue_id;
198                 RTE_LOG(INFO, L3FWD,
199                                 " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
200                                 lcore_id, portid, queueid);
201         }
202
203         while (!force_quit) {
204
205                 cur_tsc = rte_rdtsc();
206
207                 /* TX burst queue drain. */
208                 diff_tsc = cur_tsc - prev_tsc;
209                 if (unlikely(diff_tsc > drain_tsc)) {
210
211                         for (i = 0; i < qconf->n_tx_port; ++i) {
212                                 portid = qconf->tx_port_id[i];
213                                 if (qconf->tx_mbufs[portid].len == 0)
214                                         continue;
215                                 send_burst(qconf,
216                                         qconf->tx_mbufs[portid].len,
217                                         portid);
218                                 qconf->tx_mbufs[portid].len = 0;
219                         }
220
221                         prev_tsc = cur_tsc;
222                 }
223
224                 /* Read packet from RX queues. */
225                 for (i = 0; i < qconf->n_rx_queue; ++i) {
226                         portid = qconf->rx_queue_list[i].port_id;
227                         queueid = qconf->rx_queue_list[i].queue_id;
228                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
229                                         MAX_PKT_BURST);
230                         if (nb_rx == 0)
231                                 continue;
232
233                         /* Use fib to lookup port IDs and transmit them. */
234                         fib_send_packets(nb_rx, pkts_burst,     portid, qconf);
235                 }
236         }
237
238         return 0;
239 }
240
241 /* One eventdev loop for single and burst using fib. */
242 static __rte_always_inline void
243 fib_event_loop(struct l3fwd_event_resources *evt_rsrc,
244                 const uint8_t flags)
245 {
246         const int event_p_id = l3fwd_get_free_event_port(evt_rsrc);
247         const uint8_t tx_q_id = evt_rsrc->evq.event_q_id[
248                         evt_rsrc->evq.nb_queues - 1];
249         const uint8_t event_d_id = evt_rsrc->event_d_id;
250         const uint16_t deq_len = evt_rsrc->deq_depth;
251         struct rte_event events[MAX_PKT_BURST];
252         struct lcore_conf *lconf;
253         unsigned int lcore_id;
254         int nb_enq, nb_deq, i;
255
256         uint32_t ipv4_arr[MAX_PKT_BURST];
257         uint8_t ipv6_arr[MAX_PKT_BURST][RTE_FIB6_IPV6_ADDR_SIZE];
258         uint64_t hopsv4[MAX_PKT_BURST], hopsv6[MAX_PKT_BURST];
259         uint16_t nh;
260         uint8_t type_arr[MAX_PKT_BURST];
261         uint32_t ipv4_cnt, ipv6_cnt;
262         uint32_t ipv4_arr_assem, ipv6_arr_assem;
263
264         if (event_p_id < 0)
265                 return;
266
267         lcore_id = rte_lcore_id();
268
269         lconf = &lcore_conf[lcore_id];
270
271         RTE_LOG(INFO, L3FWD, "entering %s on lcore %u\n", __func__, lcore_id);
272
273         while (!force_quit) {
274                 /* Read events from RX queues. */
275                 nb_deq = rte_event_dequeue_burst(event_d_id, event_p_id,
276                                 events, deq_len, 0);
277                 if (nb_deq == 0) {
278                         rte_pause();
279                         continue;
280                 }
281
282                 /* Reset counters. */
283                 ipv4_cnt = 0;
284                 ipv6_cnt = 0;
285                 ipv4_arr_assem = 0;
286                 ipv6_arr_assem = 0;
287
288                 /* Prefetch first packets. */
289                 for (i = 0; i < FIB_PREFETCH_OFFSET && i < nb_deq; i++)
290                         rte_prefetch0(rte_pktmbuf_mtod(events[i].mbuf, void *));
291
292                 /* Parse packet info and prefetch. */
293                 for (i = 0; i < (nb_deq - FIB_PREFETCH_OFFSET); i++) {
294                         if (flags & L3FWD_EVENT_TX_ENQ) {
295                                 events[i].queue_id = tx_q_id;
296                                 events[i].op = RTE_EVENT_OP_FORWARD;
297                         }
298
299                         if (flags & L3FWD_EVENT_TX_DIRECT)
300                                 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
301                                                 0);
302
303                         /* Prefetch packet. */
304                         rte_prefetch0(rte_pktmbuf_mtod(events[
305                                         i + FIB_PREFETCH_OFFSET].mbuf,
306                                         void *));
307
308                         fib_parse_packet(events[i].mbuf,
309                                         &ipv4_arr[ipv4_cnt], &ipv4_cnt,
310                                         ipv6_arr[ipv6_cnt], &ipv6_cnt,
311                                         &type_arr[i]);
312                 }
313
314                 /* Parse remaining packet info. */
315                 for (; i < nb_deq; i++) {
316                         if (flags & L3FWD_EVENT_TX_ENQ) {
317                                 events[i].queue_id = tx_q_id;
318                                 events[i].op = RTE_EVENT_OP_FORWARD;
319                         }
320
321                         if (flags & L3FWD_EVENT_TX_DIRECT)
322                                 rte_event_eth_tx_adapter_txq_set(events[i].mbuf,
323                                                 0);
324
325                         fib_parse_packet(events[i].mbuf,
326                                         &ipv4_arr[ipv4_cnt], &ipv4_cnt,
327                                         ipv6_arr[ipv6_cnt], &ipv6_cnt,
328                                         &type_arr[i]);
329                 }
330
331                 /* Lookup IPv4 hops if IPv4 packets are present. */
332                 if (likely(ipv4_cnt > 0))
333                         rte_fib_lookup_bulk(lconf->ipv4_lookup_struct,
334                                         ipv4_arr, hopsv4, ipv4_cnt);
335
336                 /* Lookup IPv6 hops if IPv6 packets are present. */
337                 if (ipv6_cnt > 0)
338                         rte_fib6_lookup_bulk(lconf->ipv6_lookup_struct,
339                                         ipv6_arr, hopsv6, ipv6_cnt);
340
341                 /* Assign ports looked up in fib depending on IPv4 or IPv6 */
342                 for (i = 0; i < nb_deq; i++) {
343                         if (type_arr[i])
344                                 nh = (uint16_t)hopsv4[ipv4_arr_assem++];
345                         else
346                                 nh = (uint16_t)hopsv6[ipv6_arr_assem++];
347                         if (nh != FIB_DEFAULT_HOP)
348                                 events[i].mbuf->port = nh;
349                 }
350
351                 if (flags & L3FWD_EVENT_TX_ENQ) {
352                         nb_enq = rte_event_enqueue_burst(event_d_id, event_p_id,
353                                         events, nb_deq);
354                         while (nb_enq < nb_deq && !force_quit)
355                                 nb_enq += rte_event_enqueue_burst(event_d_id,
356                                                 event_p_id, events + nb_enq,
357                                                 nb_deq - nb_enq);
358                 }
359
360                 if (flags & L3FWD_EVENT_TX_DIRECT) {
361                         nb_enq = rte_event_eth_tx_adapter_enqueue(event_d_id,
362                                         event_p_id, events, nb_deq, 0);
363                         while (nb_enq < nb_deq && !force_quit)
364                                 nb_enq += rte_event_eth_tx_adapter_enqueue(
365                                                 event_d_id, event_p_id,
366                                                 events + nb_enq,
367                                                 nb_deq - nb_enq, 0);
368                 }
369         }
370 }
371
372 int __rte_noinline
373 fib_event_main_loop_tx_d(__rte_unused void *dummy)
374 {
375         struct l3fwd_event_resources *evt_rsrc =
376                         l3fwd_get_eventdev_rsrc();
377
378         fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
379         return 0;
380 }
381
382 int __rte_noinline
383 fib_event_main_loop_tx_d_burst(__rte_unused void *dummy)
384 {
385         struct l3fwd_event_resources *evt_rsrc =
386                         l3fwd_get_eventdev_rsrc();
387
388         fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_DIRECT);
389         return 0;
390 }
391
392 int __rte_noinline
393 fib_event_main_loop_tx_q(__rte_unused void *dummy)
394 {
395         struct l3fwd_event_resources *evt_rsrc =
396                         l3fwd_get_eventdev_rsrc();
397
398         fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ);
399         return 0;
400 }
401
402 int __rte_noinline
403 fib_event_main_loop_tx_q_burst(__rte_unused void *dummy)
404 {
405         struct l3fwd_event_resources *evt_rsrc =
406                         l3fwd_get_eventdev_rsrc();
407
408         fib_event_loop(evt_rsrc, L3FWD_EVENT_TX_ENQ);
409         return 0;
410 }
411
412 /* Function to setup fib. */
413 void
414 setup_fib(const int socketid)
415 {
416         struct rte_fib6_conf config;
417         struct rte_fib_conf config_ipv4;
418         unsigned int i;
419         int ret;
420         char s[64];
421         char abuf[INET6_ADDRSTRLEN];
422
423         /* Create the fib IPv4 table. */
424         config_ipv4.type = RTE_FIB_DIR24_8;
425         config_ipv4.max_routes = (1 << 16);
426         config_ipv4.default_nh = FIB_DEFAULT_HOP;
427         config_ipv4.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
428         config_ipv4.dir24_8.num_tbl8 = (1 << 15);
429         snprintf(s, sizeof(s), "IPV4_L3FWD_FIB_%d", socketid);
430         ipv4_l3fwd_fib_lookup_struct[socketid] =
431                         rte_fib_create(s, socketid, &config_ipv4);
432         if (ipv4_l3fwd_fib_lookup_struct[socketid] == NULL)
433                 rte_exit(EXIT_FAILURE,
434                         "Unable to create the l3fwd FIB table on socket %d\n",
435                         socketid);
436
437         /* Populate the fib ipv4 table. */
438         for (i = 0; i < RTE_DIM(ipv4_l3fwd_route_array); i++) {
439                 struct in_addr in;
440
441                 /* Skip unused ports. */
442                 if ((1 << ipv4_l3fwd_route_array[i].if_out &
443                                 enabled_port_mask) == 0)
444                         continue;
445
446                 ret = rte_fib_add(ipv4_l3fwd_fib_lookup_struct[socketid],
447                         ipv4_l3fwd_route_array[i].ip,
448                         ipv4_l3fwd_route_array[i].depth,
449                         ipv4_l3fwd_route_array[i].if_out);
450
451                 if (ret < 0) {
452                         rte_exit(EXIT_FAILURE,
453                                         "Unable to add entry %u to the l3fwd FIB table on socket %d\n",
454                                         i, socketid);
455                 }
456
457                 in.s_addr = htonl(ipv4_l3fwd_route_array[i].ip);
458                 if (inet_ntop(AF_INET, &in, abuf, sizeof(abuf)) != NULL) {
459                         printf("FIB: Adding route %s / %d (%d)\n",
460                                 abuf,
461                                 ipv4_l3fwd_route_array[i].depth,
462                                 ipv4_l3fwd_route_array[i].if_out);
463                 } else {
464                         printf("FIB: IPv4 route added to port %d\n",
465                                 ipv4_l3fwd_route_array[i].if_out);
466                 }
467         }
468
469         /* Create the fib IPv6 table. */
470         snprintf(s, sizeof(s), "IPV6_L3FWD_FIB_%d", socketid);
471
472         config.type = RTE_FIB6_TRIE;
473         config.max_routes = (1 << 16) - 1;
474         config.default_nh = FIB_DEFAULT_HOP;
475         config.trie.nh_sz = RTE_FIB6_TRIE_4B;
476         config.trie.num_tbl8 = (1 << 15);
477         ipv6_l3fwd_fib_lookup_struct[socketid] = rte_fib6_create(s, socketid,
478                         &config);
479         if (ipv6_l3fwd_fib_lookup_struct[socketid] == NULL)
480                 rte_exit(EXIT_FAILURE,
481                                 "Unable to create the l3fwd FIB table on socket %d\n",
482                                 socketid);
483
484         /* Populate the fib IPv6 table. */
485         for (i = 0; i < RTE_DIM(ipv6_l3fwd_route_array); i++) {
486
487                 /* Skip unused ports. */
488                 if ((1 << ipv6_l3fwd_route_array[i].if_out &
489                                 enabled_port_mask) == 0)
490                         continue;
491
492                 ret = rte_fib6_add(ipv6_l3fwd_fib_lookup_struct[socketid],
493                         ipv6_l3fwd_route_array[i].ip,
494                         ipv6_l3fwd_route_array[i].depth,
495                         ipv6_l3fwd_route_array[i].if_out);
496
497                 if (ret < 0) {
498                         rte_exit(EXIT_FAILURE,
499                                         "Unable to add entry %u to the l3fwd FIB table on socket %d\n",
500                                         i, socketid);
501                 }
502
503                 if (inet_ntop(AF_INET6, ipv6_l3fwd_route_array[i].ip,
504                                 abuf, sizeof(abuf)) != NULL) {
505                         printf("FIB: Adding route %s / %d (%d)\n",
506                                 abuf,
507                                 ipv6_l3fwd_route_array[i].depth,
508                                 ipv6_l3fwd_route_array[i].if_out);
509                 } else {
510                         printf("FIB: IPv6 route added to port %d\n",
511                                 ipv6_l3fwd_route_array[i].if_out);
512                 }
513         }
514 }
515
516 /* Return ipv4 fib lookup struct. */
517 void *
518 fib_get_ipv4_l3fwd_lookup_struct(const int socketid)
519 {
520         return ipv4_l3fwd_fib_lookup_struct[socketid];
521 }
522
523 /* Return ipv6 fib lookup struct. */
524 void *
525 fib_get_ipv6_l3fwd_lookup_struct(const int socketid)
526 {
527         return ipv6_l3fwd_fib_lookup_struct[socketid];
528 }