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