New upstream version 17.11.4
[deb_dpdk.git] / examples / l3fwd / l3fwd_lpm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <stdbool.h>
45
46 #include <rte_debug.h>
47 #include <rte_ether.h>
48 #include <rte_ethdev.h>
49 #include <rte_cycles.h>
50 #include <rte_mbuf.h>
51 #include <rte_ip.h>
52 #include <rte_tcp.h>
53 #include <rte_udp.h>
54 #include <rte_lpm.h>
55 #include <rte_lpm6.h>
56
57 #include "l3fwd.h"
58
59 struct ipv4_l3fwd_lpm_route {
60         uint32_t ip;
61         uint8_t  depth;
62         uint8_t  if_out;
63 };
64
65 struct ipv6_l3fwd_lpm_route {
66         uint8_t ip[16];
67         uint8_t  depth;
68         uint8_t  if_out;
69 };
70
71 static struct ipv4_l3fwd_lpm_route ipv4_l3fwd_lpm_route_array[] = {
72         {IPv4(1, 1, 1, 0), 24, 0},
73         {IPv4(2, 1, 1, 0), 24, 1},
74         {IPv4(3, 1, 1, 0), 24, 2},
75         {IPv4(4, 1, 1, 0), 24, 3},
76         {IPv4(5, 1, 1, 0), 24, 4},
77         {IPv4(6, 1, 1, 0), 24, 5},
78         {IPv4(7, 1, 1, 0), 24, 6},
79         {IPv4(8, 1, 1, 0), 24, 7},
80 };
81
82 static struct ipv6_l3fwd_lpm_route ipv6_l3fwd_lpm_route_array[] = {
83         {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 0},
84         {{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 1},
85         {{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 2},
86         {{4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 3},
87         {{5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 4},
88         {{6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 5},
89         {{7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 6},
90         {{8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 48, 7},
91 };
92
93 #define IPV4_L3FWD_LPM_NUM_ROUTES \
94         (sizeof(ipv4_l3fwd_lpm_route_array) / sizeof(ipv4_l3fwd_lpm_route_array[0]))
95 #define IPV6_L3FWD_LPM_NUM_ROUTES \
96         (sizeof(ipv6_l3fwd_lpm_route_array) / sizeof(ipv6_l3fwd_lpm_route_array[0]))
97
98 #define IPV4_L3FWD_LPM_MAX_RULES         1024
99 #define IPV4_L3FWD_LPM_NUMBER_TBL8S (1 << 8)
100 #define IPV6_L3FWD_LPM_MAX_RULES         1024
101 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
102
103 struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
104 struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
105
106 static inline uint16_t
107 lpm_get_ipv4_dst_port(void *ipv4_hdr, uint16_t portid, void *lookup_struct)
108 {
109         uint32_t next_hop;
110         struct rte_lpm *ipv4_l3fwd_lookup_struct =
111                 (struct rte_lpm *)lookup_struct;
112
113         return (uint16_t) ((rte_lpm_lookup(ipv4_l3fwd_lookup_struct,
114                 rte_be_to_cpu_32(((struct ipv4_hdr *)ipv4_hdr)->dst_addr),
115                 &next_hop) == 0) ? next_hop : portid);
116 }
117
118 static inline uint16_t
119 lpm_get_ipv6_dst_port(void *ipv6_hdr, uint16_t portid, void *lookup_struct)
120 {
121         uint32_t next_hop;
122         struct rte_lpm6 *ipv6_l3fwd_lookup_struct =
123                 (struct rte_lpm6 *)lookup_struct;
124
125         return (uint16_t) ((rte_lpm6_lookup(ipv6_l3fwd_lookup_struct,
126                         ((struct ipv6_hdr *)ipv6_hdr)->dst_addr,
127                         &next_hop) == 0) ?  next_hop : portid);
128 }
129
130 static __rte_always_inline uint16_t
131 lpm_get_dst_port(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
132                 uint16_t portid)
133 {
134         struct ipv6_hdr *ipv6_hdr;
135         struct ipv4_hdr *ipv4_hdr;
136         struct ether_hdr *eth_hdr;
137
138         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
139
140                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
141                 ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
142
143                 return lpm_get_ipv4_dst_port(ipv4_hdr, portid,
144                                              qconf->ipv4_lookup_struct);
145         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
146
147                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
148                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
149
150                 return lpm_get_ipv6_dst_port(ipv6_hdr, portid,
151                                              qconf->ipv6_lookup_struct);
152         }
153
154         return portid;
155 }
156
157 /*
158  * lpm_get_dst_port optimized routine for packets where dst_ipv4 is already
159  * precalculated. If packet is ipv6 dst_addr is taken directly from packet
160  * header and dst_ipv4 value is not used.
161  */
162 static __rte_always_inline uint16_t
163 lpm_get_dst_port_with_ipv4(const struct lcore_conf *qconf, struct rte_mbuf *pkt,
164         uint32_t dst_ipv4, uint16_t portid)
165 {
166         uint32_t next_hop;
167         struct ipv6_hdr *ipv6_hdr;
168         struct ether_hdr *eth_hdr;
169
170         if (RTE_ETH_IS_IPV4_HDR(pkt->packet_type)) {
171                 return (uint16_t) ((rte_lpm_lookup(qconf->ipv4_lookup_struct,
172                                                    dst_ipv4, &next_hop) == 0)
173                                    ? next_hop : portid);
174
175         } else if (RTE_ETH_IS_IPV6_HDR(pkt->packet_type)) {
176
177                 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
178                 ipv6_hdr = (struct ipv6_hdr *)(eth_hdr + 1);
179
180                 return (uint16_t) ((rte_lpm6_lookup(qconf->ipv6_lookup_struct,
181                                 ipv6_hdr->dst_addr, &next_hop) == 0)
182                                 ? next_hop : portid);
183
184         }
185
186         return portid;
187 }
188
189 #if defined(RTE_ARCH_X86)
190 #include "l3fwd_lpm_sse.h"
191 #elif defined RTE_MACHINE_CPUFLAG_NEON
192 #include "l3fwd_lpm_neon.h"
193 #elif defined(RTE_ARCH_PPC_64)
194 #include "l3fwd_lpm_altivec.h"
195 #else
196 #include "l3fwd_lpm.h"
197 #endif
198
199 /* main processing loop */
200 int
201 lpm_main_loop(__attribute__((unused)) void *dummy)
202 {
203         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
204         unsigned lcore_id;
205         uint64_t prev_tsc, diff_tsc, cur_tsc;
206         int i, nb_rx;
207         uint16_t portid;
208         uint8_t queueid;
209         struct lcore_conf *qconf;
210         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
211                 US_PER_S * BURST_TX_DRAIN_US;
212
213         prev_tsc = 0;
214
215         lcore_id = rte_lcore_id();
216         qconf = &lcore_conf[lcore_id];
217
218         if (qconf->n_rx_queue == 0) {
219                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
220                 return 0;
221         }
222
223         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
224
225         for (i = 0; i < qconf->n_rx_queue; i++) {
226
227                 portid = qconf->rx_queue_list[i].port_id;
228                 queueid = qconf->rx_queue_list[i].queue_id;
229                 RTE_LOG(INFO, L3FWD,
230                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
231                         lcore_id, portid, queueid);
232         }
233
234         while (!force_quit) {
235
236                 cur_tsc = rte_rdtsc();
237
238                 /*
239                  * TX burst queue drain
240                  */
241                 diff_tsc = cur_tsc - prev_tsc;
242                 if (unlikely(diff_tsc > drain_tsc)) {
243
244                         for (i = 0; i < qconf->n_tx_port; ++i) {
245                                 portid = qconf->tx_port_id[i];
246                                 if (qconf->tx_mbufs[portid].len == 0)
247                                         continue;
248                                 send_burst(qconf,
249                                         qconf->tx_mbufs[portid].len,
250                                         portid);
251                                 qconf->tx_mbufs[portid].len = 0;
252                         }
253
254                         prev_tsc = cur_tsc;
255                 }
256
257                 /*
258                  * Read packet from RX queues
259                  */
260                 for (i = 0; i < qconf->n_rx_queue; ++i) {
261                         portid = qconf->rx_queue_list[i].port_id;
262                         queueid = qconf->rx_queue_list[i].queue_id;
263                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
264                                 MAX_PKT_BURST);
265                         if (nb_rx == 0)
266                                 continue;
267
268 #if defined RTE_ARCH_X86 || defined RTE_MACHINE_CPUFLAG_NEON \
269                          || defined RTE_ARCH_PPC_64
270                         l3fwd_lpm_send_packets(nb_rx, pkts_burst,
271                                                 portid, qconf);
272 #else
273                         l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
274                                                         portid, qconf);
275 #endif /* X86 */
276                 }
277         }
278
279         return 0;
280 }
281
282 void
283 setup_lpm(const int socketid)
284 {
285         struct rte_lpm6_config config;
286         struct rte_lpm_config config_ipv4;
287         unsigned i;
288         int ret;
289         char s[64];
290
291         /* create the LPM table */
292         config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
293         config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
294         config_ipv4.flags = 0;
295         snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
296         ipv4_l3fwd_lpm_lookup_struct[socketid] =
297                         rte_lpm_create(s, socketid, &config_ipv4);
298         if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
299                 rte_exit(EXIT_FAILURE,
300                         "Unable to create the l3fwd LPM table on socket %d\n",
301                         socketid);
302
303         /* populate the LPM table */
304         for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) {
305
306                 /* skip unused ports */
307                 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
308                                 enabled_port_mask) == 0)
309                         continue;
310
311                 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
312                         ipv4_l3fwd_lpm_route_array[i].ip,
313                         ipv4_l3fwd_lpm_route_array[i].depth,
314                         ipv4_l3fwd_lpm_route_array[i].if_out);
315
316                 if (ret < 0) {
317                         rte_exit(EXIT_FAILURE,
318                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
319                                 i, socketid);
320                 }
321
322                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
323                         (unsigned)ipv4_l3fwd_lpm_route_array[i].ip,
324                         ipv4_l3fwd_lpm_route_array[i].depth,
325                         ipv4_l3fwd_lpm_route_array[i].if_out);
326         }
327
328         /* create the LPM6 table */
329         snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
330
331         config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
332         config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
333         config.flags = 0;
334         ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
335                                 &config);
336         if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL)
337                 rte_exit(EXIT_FAILURE,
338                         "Unable to create the l3fwd LPM table on socket %d\n",
339                         socketid);
340
341         /* populate the LPM table */
342         for (i = 0; i < IPV6_L3FWD_LPM_NUM_ROUTES; i++) {
343
344                 /* skip unused ports */
345                 if ((1 << ipv6_l3fwd_lpm_route_array[i].if_out &
346                                 enabled_port_mask) == 0)
347                         continue;
348
349                 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
350                         ipv6_l3fwd_lpm_route_array[i].ip,
351                         ipv6_l3fwd_lpm_route_array[i].depth,
352                         ipv6_l3fwd_lpm_route_array[i].if_out);
353
354                 if (ret < 0) {
355                         rte_exit(EXIT_FAILURE,
356                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
357                                 i, socketid);
358                 }
359
360                 printf("LPM: Adding route %s / %d (%d)\n",
361                         "IPV6",
362                         ipv6_l3fwd_lpm_route_array[i].depth,
363                         ipv6_l3fwd_lpm_route_array[i].if_out);
364         }
365 }
366
367 int
368 lpm_check_ptype(int portid)
369 {
370         int i, ret;
371         int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
372         uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
373
374         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
375         if (ret <= 0)
376                 return 0;
377
378         uint32_t ptypes[ret];
379
380         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
381         for (i = 0; i < ret; ++i) {
382                 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
383                         ptype_l3_ipv4 = 1;
384                 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
385                         ptype_l3_ipv6 = 1;
386         }
387
388         if (ptype_l3_ipv4 == 0)
389                 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
390
391         if (ptype_l3_ipv6 == 0)
392                 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
393
394         if (ptype_l3_ipv4 && ptype_l3_ipv6)
395                 return 1;
396
397         return 0;
398
399 }
400
401 static inline void
402 lpm_parse_ptype(struct rte_mbuf *m)
403 {
404         struct ether_hdr *eth_hdr;
405         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
406         uint16_t ether_type;
407
408         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
409         ether_type = eth_hdr->ether_type;
410         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
411                 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
412         else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
413                 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
414
415         m->packet_type = packet_type;
416 }
417
418 uint16_t
419 lpm_cb_parse_ptype(uint16_t port __rte_unused, uint16_t queue __rte_unused,
420                    struct rte_mbuf *pkts[], uint16_t nb_pkts,
421                    uint16_t max_pkts __rte_unused,
422                    void *user_param __rte_unused)
423 {
424         unsigned i;
425
426         for (i = 0; i < nb_pkts; ++i)
427                 lpm_parse_ptype(pkts[i]);
428
429         return nb_pkts;
430 }
431
432 /* Return ipv4/ipv6 lpm fwd lookup struct. */
433 void *
434 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
435 {
436         return ipv4_l3fwd_lpm_lookup_struct[socketid];
437 }
438
439 void *
440 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
441 {
442         return ipv6_l3fwd_lpm_lookup_struct[socketid];
443 }