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