examples/l3fwd: format IP addresses for printing
[dpdk.git] / examples / l3fwd / l3fwd_lpm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <sys/queue.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <getopt.h>
15 #include <stdbool.h>
16 #include <arpa/inet.h>
17
18 #include <rte_debug.h>
19 #include <rte_ether.h>
20 #include <rte_ethdev.h>
21 #include <rte_cycles.h>
22 #include <rte_mbuf.h>
23 #include <rte_ip.h>
24 #include <rte_tcp.h>
25 #include <rte_udp.h>
26 #include <rte_lpm.h>
27 #include <rte_lpm6.h>
28
29 #include "l3fwd.h"
30
31 struct ipv4_l3fwd_lpm_route {
32         uint32_t ip;
33         uint8_t  depth;
34         uint8_t  if_out;
35 };
36
37 struct ipv6_l3fwd_lpm_route {
38         uint8_t ip[16];
39         uint8_t  depth;
40         uint8_t  if_out;
41 };
42
43 /* 192.18.0.0/16 are set aside for RFC2544 benchmarking. */
44 static struct ipv4_l3fwd_lpm_route ipv4_l3fwd_lpm_route_array[] = {
45         {IPv4(192, 18, 0, 0), 24, 0},
46         {IPv4(192, 18, 1, 0), 24, 1},
47         {IPv4(192, 18, 2, 0), 24, 2},
48         {IPv4(192, 18, 3, 0), 24, 3},
49         {IPv4(192, 18, 4, 0), 24, 4},
50         {IPv4(192, 18, 5, 0), 24, 5},
51         {IPv4(192, 18, 6, 0), 24, 6},
52         {IPv4(192, 18, 7, 0), 24, 7},
53 };
54
55 /* 2001:0200::/48 is IANA reserved range for IPv6 benchmarking (RFC5180) */
56 static struct ipv6_l3fwd_lpm_route ipv6_l3fwd_lpm_route_array[] = {
57         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 48, 0},
58         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0}, 48, 1},
59         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0}, 48, 2},
60         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0}, 48, 3},
61         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0}, 48, 4},
62         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0}, 48, 5},
63         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0}, 48, 6},
64         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0}, 48, 7},
65 };
66
67 #define IPV4_L3FWD_LPM_NUM_ROUTES \
68         (sizeof(ipv4_l3fwd_lpm_route_array) / sizeof(ipv4_l3fwd_lpm_route_array[0]))
69 #define IPV6_L3FWD_LPM_NUM_ROUTES \
70         (sizeof(ipv6_l3fwd_lpm_route_array) / sizeof(ipv6_l3fwd_lpm_route_array[0]))
71
72 #define IPV4_L3FWD_LPM_MAX_RULES         1024
73 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
74 #define IPV6_L3FWD_LPM_MAX_RULES         1024
75 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
76
77 struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
78 struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
79
80 static inline uint16_t
81 lpm_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
82 {
83         uint32_t next_hop;
84         struct rte_lpm *ipv4_l3fwd_lookup_struct =
85                 (struct rte_lpm *)lookup_struct;
86
87         return (uint16_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
88                 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
89                 &next_hop) == 0) ? next_hop : portid);
90 }
91
92 static inline uint16_t
93 lpm_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
94 {
95         uint32_t next_hop;
96         struct rte_lpm6 *ipv6_l3fwd_lookup_struct =
97                 (struct rte_lpm6 *)lookup_struct;
98
99         return (uint16_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
100                         ((struct ipv6_hdr *)ipv6_hdr)->dst_addr,
101                         &next_hop) == 0) ?  next_hop : portid);
102 }
103
104 static __rte_always_inline uint16_t
105 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
106                 uint16_t portid)
107 {
108         struct ipv6_hdr *ipv6_hdr;
109         struct ipv4_hdr *ipv4_hdr;
110         struct ether_hdr *eth_hdr;
111
112         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
113
114                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
115                 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
116
117                 return lpm_get_ipv4_dst_port(ipv4_hdr, portid,
118                                              qconf->ipv4_lookup_struct);
119         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
120
121                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
122                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
123
124                 return lpm_get_ipv6_dst_port(ipv6_hdr, portid,
125                                              qconf->ipv6_lookup_struct);
126         }
127
128         return portid;
129 }
130
131 /*
132  * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
133  * precalculated. If packet is ipv6 dst_addr is taken directly from packet
134  * header and dst_ipv4 value is not used.
135  */
136 static __rte_always_inline uint16_t
137 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
138         uint32_t dst_ipv4, uint16_t portid)
139 {
140         uint32_t next_hop;
141         struct ipv6_hdr *ipv6_hdr;
142         struct ether_hdr *eth_hdr;
143
144         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
145                 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
146                                                    dst_ipv4, &next_hop) == 0)
147                                    ? next_hop : portid);
148
149         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
150
151                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
152                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
153
154                 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
155                                 ipv6_hdr->dst_addr, &next_hop) == 0)
156                                 ? next_hop : portid);
157
158         }
159
160         return portid;
161 }
162
163 #if defined(RTE_ARCH_X86)
164 #include "l3fwd_lpm_sse.h"
165 #elif defined RTE_MACHINE_CPUFLAG_NEON
166 #include "l3fwd_lpm_neon.h"
167 #elif defined(RTE_ARCH_PPC_64)
168 #include "l3fwd_lpm_altivec.h"
169 #else
170 #include "l3fwd_lpm.h"
171 #endif
172
173 /* main processing loop */
174 int
175 lpm_main_loop(__attribute__((unused)) void *dummy)
176 {
177         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
178         unsigned lcore_id;
179         uint64_t prev_tsc, diff_tsc, cur_tsc;
180         int i, nb_rx;
181         uint16_t portid;
182         uint8_t queueid;
183         struct lcore_conf *qconf;
184         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
185                 US_PER_S * BURST_TX_DRAIN_US;
186
187         prev_tsc = 0;
188
189         lcore_id = rte_lcore_id();
190         qconf = &lcore_conf[lcore_id];
191
192         if (qconf->n_rx_queue == 0) {
193                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
194                 return 0;
195         }
196
197         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
198
199         for (i = 0; i < qconf->n_rx_queue; i++) {
200
201                 portid = qconf->rx_queue_list[i].port_id;
202                 queueid = qconf->rx_queue_list[i].queue_id;
203                 RTE_LOG(INFO, L3FWD,
204                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
205                         lcore_id, portid, queueid);
206         }
207
208         while (!force_quit) {
209
210                 cur_tsc = rte_rdtsc();
211
212                 /*
213                  * TX burst queue drain
214                  */
215                 diff_tsc = cur_tsc - prev_tsc;
216                 if (unlikely(diff_tsc > drain_tsc)) {
217
218                         for (i = 0; i < qconf->n_tx_port; ++i) {
219                                 portid = qconf->tx_port_id[i];
220                                 if (qconf->tx_mbufs[portid].len == 0)
221                                         continue;
222                                 send_burst(qconf,
223                                         qconf->tx_mbufs[portid].len,
224                                         portid);
225                                 qconf->tx_mbufs[portid].len = 0;
226                         }
227
228                         prev_tsc = cur_tsc;
229                 }
230
231                 /*
232                  * Read packet from RX queues
233                  */
234                 for (i = 0; i < qconf->n_rx_queue; ++i) {
235                         portid = qconf->rx_queue_list[i].port_id;
236                         queueid = qconf->rx_queue_list[i].queue_id;
237                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
238                                 MAX_PKT_BURST);
239                         if (nb_rx == 0)
240                                 continue;
241
242 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON \
243                          || defined RTE_ARCH_PPC_64
244                         l3fwd_lpm_send_packets(nb_rx, pkts_burst,
245                                                 portid, qconf);
246 #else
247                         l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
248                                                         portid, qconf);
249 #endif /* X86 */
250                 }
251         }
252
253         return 0;
254 }
255
256 void
257 setup_lpm(const int socketid)
258 {
259         struct rte_lpm6_config config;
260         struct rte_lpm_config config_ipv4;
261         unsigned i;
262         int ret;
263         char s[64];
264         char abuf[INET6_ADDRSTRLEN];
265
266         /* create the LPM table */
267         config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
268         config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
269         config_ipv4.flags = 0;
270         snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
271         ipv4_l3fwd_lpm_lookup_struct[socketid] =
272                         rte_lpm_create(s, socketid, &config_ipv4);
273         if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
274                 rte_exit(EXIT_FAILURE,
275                         "Unable to create the l3fwd LPM table on socket %d\n",
276                         socketid);
277
278         /* populate the LPM table */
279         for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) {
280                 struct in_addr in;
281
282                 /* skip unused ports */
283                 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
284                                 enabled_port_mask) == 0)
285                         continue;
286
287                 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
288                         ipv4_l3fwd_lpm_route_array[i].ip,
289                         ipv4_l3fwd_lpm_route_array[i].depth,
290                         ipv4_l3fwd_lpm_route_array[i].if_out);
291
292                 if (ret < 0) {
293                         rte_exit(EXIT_FAILURE,
294                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
295                                 i, socketid);
296                 }
297
298                 in.s_addr = htonl(ipv4_l3fwd_lpm_route_array[i].ip);
299                 printf("LPM: Adding route %s / %d (%d)\n",
300                        inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
301                         ipv4_l3fwd_lpm_route_array[i].depth,
302                         ipv4_l3fwd_lpm_route_array[i].if_out);
303         }
304
305         /* create the LPM6 table */
306         snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
307
308         config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
309         config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
310         config.flags = 0;
311         ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
312                                 &config);
313         if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL)
314                 rte_exit(EXIT_FAILURE,
315                         "Unable to create the l3fwd LPM table on socket %d\n",
316                         socketid);
317
318         /* populate the LPM table */
319         for (i = 0; i < IPV6_L3FWD_LPM_NUM_ROUTES; i++) {
320
321                 /* skip unused ports */
322                 if ((1 << ipv6_l3fwd_lpm_route_array[i].if_out &
323                                 enabled_port_mask) == 0)
324                         continue;
325
326                 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
327                         ipv6_l3fwd_lpm_route_array[i].ip,
328                         ipv6_l3fwd_lpm_route_array[i].depth,
329                         ipv6_l3fwd_lpm_route_array[i].if_out);
330
331                 if (ret < 0) {
332                         rte_exit(EXIT_FAILURE,
333                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
334                                 i, socketid);
335                 }
336
337                 printf("LPM: Adding route %s / %d (%d)\n",
338                        inet_ntop(AF_INET6, ipv6_l3fwd_lpm_route_array[i].ip,
339                                  abuf, sizeof(abuf)),
340                        ipv6_l3fwd_lpm_route_array[i].depth,
341                        ipv6_l3fwd_lpm_route_array[i].if_out);
342         }
343 }
344
345 int
346 lpm_check_ptype(int portid)
347 {
348         int i, ret;
349         int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
350         uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
351
352         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
353         if (ret <= 0)
354                 return 0;
355
356         uint32_t ptypes[ret];
357
358         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
359         for (i = 0; i < ret; ++i) {
360                 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
361                         ptype_l3_ipv4 = 1;
362                 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
363                         ptype_l3_ipv6 = 1;
364         }
365
366         if (ptype_l3_ipv4 == 0)
367                 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
368
369         if (ptype_l3_ipv6 == 0)
370                 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
371
372         if (ptype_l3_ipv4 && ptype_l3_ipv6)
373                 return 1;
374
375         return 0;
376
377 }
378
379 static inline void
380 lpm_parse_ptype(struct rte_mbuf *m)
381 {
382         struct ether_hdr *eth_hdr;
383         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
384         uint16_t ether_type;
385
386         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
387         ether_type = eth_hdr->ether_type;
388         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
389                 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
390         else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
391                 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
392
393         m->packet_type = packet_type;
394 }
395
396 uint16_t
397 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
398                    struct rte_mbuf *pkts[], uint16_t nb_pkts,
399                    uint16_t max_pkts __rte_unused,
400                    void *user_param __rte_unused)
401 {
402         unsigned i;
403
404         for (i = 0; i < nb_pkts; ++i)
405                 lpm_parse_ptype(pkts[i]);
406
407         return nb_pkts;
408 }
409
410 /* Return ipv4/ipv6 lpm fwd lookup struct. */
411 void *
412 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
413 {
414         return ipv4_l3fwd_lpm_lookup_struct[socketid];
415 }
416
417 void *
418 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
419 {
420         return ipv6_l3fwd_lpm_lookup_struct[socketid];
421 }