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