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