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