Imported Upstream version 16.11
[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 #if defined(__SSE4_1__)
108 #include "l3fwd_lpm_sse.h"
109 #else
110 #include "l3fwd_lpm.h"
111 #endif
112
113 /* main processing loop */
114 int
115 lpm_main_loop(__attribute__((unused)) void *dummy)
116 {
117         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
118         unsigned lcore_id;
119         uint64_t prev_tsc, diff_tsc, cur_tsc;
120         int i, nb_rx;
121         uint8_t portid, queueid;
122         struct lcore_conf *qconf;
123         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
124                 US_PER_S * BURST_TX_DRAIN_US;
125
126         prev_tsc = 0;
127
128         lcore_id = rte_lcore_id();
129         qconf = &lcore_conf[lcore_id];
130
131         if (qconf->n_rx_queue == 0) {
132                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
133                 return 0;
134         }
135
136         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
137
138         for (i = 0; i < qconf->n_rx_queue; i++) {
139
140                 portid = qconf->rx_queue_list[i].port_id;
141                 queueid = qconf->rx_queue_list[i].queue_id;
142                 RTE_LOG(INFO, L3FWD,
143                         " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
144                         lcore_id, portid, queueid);
145         }
146
147         while (!force_quit) {
148
149                 cur_tsc = rte_rdtsc();
150
151                 /*
152                  * TX burst queue drain
153                  */
154                 diff_tsc = cur_tsc - prev_tsc;
155                 if (unlikely(diff_tsc > drain_tsc)) {
156
157                         for (i = 0; i < qconf->n_tx_port; ++i) {
158                                 portid = qconf->tx_port_id[i];
159                                 if (qconf->tx_mbufs[portid].len == 0)
160                                         continue;
161                                 send_burst(qconf,
162                                         qconf->tx_mbufs[portid].len,
163                                         portid);
164                                 qconf->tx_mbufs[portid].len = 0;
165                         }
166
167                         prev_tsc = cur_tsc;
168                 }
169
170                 /*
171                  * Read packet from RX queues
172                  */
173                 for (i = 0; i < qconf->n_rx_queue; ++i) {
174                         portid = qconf->rx_queue_list[i].port_id;
175                         queueid = qconf->rx_queue_list[i].queue_id;
176                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
177                                 MAX_PKT_BURST);
178                         if (nb_rx == 0)
179                                 continue;
180
181 #if defined(__SSE4_1__)
182                         l3fwd_lpm_send_packets(nb_rx, pkts_burst,
183                                                 portid, qconf);
184 #else
185                         l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
186                                                         portid, qconf);
187 #endif /* __SSE_4_1__ */
188                 }
189         }
190
191         return 0;
192 }
193
194 void
195 setup_lpm(const int socketid)
196 {
197         struct rte_lpm6_config config;
198         struct rte_lpm_config config_ipv4;
199         unsigned i;
200         int ret;
201         char s[64];
202
203         /* create the LPM table */
204         config_ipv4.max_rules = IPV4_L3FWD_LPM_MAX_RULES;
205         config_ipv4.number_tbl8s = IPV4_L3FWD_LPM_NUMBER_TBL8S;
206         config_ipv4.flags = 0;
207         snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
208         ipv4_l3fwd_lpm_lookup_struct[socketid] =
209                         rte_lpm_create(s, socketid, &config_ipv4);
210         if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
211                 rte_exit(EXIT_FAILURE,
212                         "Unable to create the l3fwd LPM table on socket %d\n",
213                         socketid);
214
215         /* populate the LPM table */
216         for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) {
217
218                 /* skip unused ports */
219                 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
220                                 enabled_port_mask) == 0)
221                         continue;
222
223                 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
224                         ipv4_l3fwd_lpm_route_array[i].ip,
225                         ipv4_l3fwd_lpm_route_array[i].depth,
226                         ipv4_l3fwd_lpm_route_array[i].if_out);
227
228                 if (ret < 0) {
229                         rte_exit(EXIT_FAILURE,
230                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
231                                 i, socketid);
232                 }
233
234                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
235                         (unsigned)ipv4_l3fwd_lpm_route_array[i].ip,
236                         ipv4_l3fwd_lpm_route_array[i].depth,
237                         ipv4_l3fwd_lpm_route_array[i].if_out);
238         }
239
240         /* create the LPM6 table */
241         snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
242
243         config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
244         config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
245         config.flags = 0;
246         ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
247                                 &config);
248         if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL)
249                 rte_exit(EXIT_FAILURE,
250                         "Unable to create the l3fwd LPM table on socket %d\n",
251                         socketid);
252
253         /* populate the LPM table */
254         for (i = 0; i < IPV6_L3FWD_LPM_NUM_ROUTES; i++) {
255
256                 /* skip unused ports */
257                 if ((1 << ipv6_l3fwd_lpm_route_array[i].if_out &
258                                 enabled_port_mask) == 0)
259                         continue;
260
261                 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
262                         ipv6_l3fwd_lpm_route_array[i].ip,
263                         ipv6_l3fwd_lpm_route_array[i].depth,
264                         ipv6_l3fwd_lpm_route_array[i].if_out);
265
266                 if (ret < 0) {
267                         rte_exit(EXIT_FAILURE,
268                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
269                                 i, socketid);
270                 }
271
272                 printf("LPM: Adding route %s / %d (%d)\n",
273                         "IPV6",
274                         ipv6_l3fwd_lpm_route_array[i].depth,
275                         ipv6_l3fwd_lpm_route_array[i].if_out);
276         }
277 }
278
279 int
280 lpm_check_ptype(int portid)
281 {
282         int i, ret;
283         int ptype_l3_ipv4 = 0, ptype_l3_ipv6 = 0;
284         uint32_t ptype_mask = RTE_PTYPE_L3_MASK;
285
286         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, NULL, 0);
287         if (ret <= 0)
288                 return 0;
289
290         uint32_t ptypes[ret];
291
292         ret = rte_eth_dev_get_supported_ptypes(portid, ptype_mask, ptypes, ret);
293         for (i = 0; i < ret; ++i) {
294                 if (ptypes[i] & RTE_PTYPE_L3_IPV4)
295                         ptype_l3_ipv4 = 1;
296                 if (ptypes[i] & RTE_PTYPE_L3_IPV6)
297                         ptype_l3_ipv6 = 1;
298         }
299
300         if (ptype_l3_ipv4 == 0)
301                 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
302
303         if (ptype_l3_ipv6 == 0)
304                 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
305
306         if (ptype_l3_ipv4 && ptype_l3_ipv6)
307                 return 1;
308
309         return 0;
310
311 }
312
313 static inline void
314 lpm_parse_ptype(struct rte_mbuf *m)
315 {
316         struct ether_hdr *eth_hdr;
317         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
318         uint16_t ether_type;
319
320         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
321         ether_type = eth_hdr->ether_type;
322         if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4))
323                 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
324         else if (ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6))
325                 packet_type |= RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
326
327         m->packet_type = packet_type;
328 }
329
330 uint16_t
331 lpm_cb_parse_ptype(uint8_t port __rte_unused, uint16_t queue __rte_unused,
332                    struct rte_mbuf *pkts[], uint16_t nb_pkts,
333                    uint16_t max_pkts __rte_unused,
334                    void *user_param __rte_unused)
335 {
336         unsigned i;
337
338         for (i = 0; i < nb_pkts; ++i)
339                 lpm_parse_ptype(pkts[i]);
340
341         return nb_pkts;
342 }
343
344 /* Return ipv4/ipv6 lpm fwd lookup struct. */
345 void *
346 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
347 {
348         return ipv4_l3fwd_lpm_lookup_struct[socketid];
349 }
350
351 void *
352 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
353 {
354         return ipv6_l3fwd_lpm_lookup_struct[socketid];
355 }