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