examples/l3fwd: modularize
[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 IPV6_L3FWD_LPM_MAX_RULES         1024
102 #define IPV6_L3FWD_LPM_NUMBER_TBL8S (1 << 16)
103
104 /* Used to mark destination port as 'invalid'. */
105 #define BAD_PORT        ((uint16_t)-1)
106
107 #define FWDSTEP 4
108
109 /* replace first 12B of the ethernet header. */
110 #define MASK_ETH        0x3f
111
112 struct rte_lpm *ipv4_l3fwd_lpm_lookup_struct[NB_SOCKETS];
113 struct rte_lpm6 *ipv6_l3fwd_lpm_lookup_struct[NB_SOCKETS];
114
115 /*
116  * Include header file if SSE4_1 is enabled for
117  * buffer optimization i.e. ENABLE_MULTI_BUFFER_OPTIMIZE=1.
118  */
119 #if defined(__SSE4_1__)
120 #include "l3fwd_lpm_sse.h"
121 #else
122 #include "l3fwd_lpm.h"
123 #endif
124
125 /* main processing loop */
126 int
127 lpm_main_loop(__attribute__((unused)) void *dummy)
128 {
129         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
130         unsigned lcore_id;
131         uint64_t prev_tsc, diff_tsc, cur_tsc;
132         int i, nb_rx;
133         uint8_t portid, queueid;
134         struct lcore_conf *qconf;
135         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
136                 US_PER_S * BURST_TX_DRAIN_US;
137
138         prev_tsc = 0;
139
140         lcore_id = rte_lcore_id();
141         qconf = &lcore_conf[lcore_id];
142
143         if (qconf->n_rx_queue == 0) {
144                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
145                 return 0;
146         }
147
148         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
149
150         for (i = 0; i < qconf->n_rx_queue; i++) {
151
152                 portid = qconf->rx_queue_list[i].port_id;
153                 queueid = qconf->rx_queue_list[i].queue_id;
154                 RTE_LOG(INFO, L3FWD,
155                         " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
156                         lcore_id, portid, queueid);
157         }
158
159         while (!force_quit) {
160
161                 cur_tsc = rte_rdtsc();
162
163                 /*
164                  * TX burst queue drain
165                  */
166                 diff_tsc = cur_tsc - prev_tsc;
167                 if (unlikely(diff_tsc > drain_tsc)) {
168
169                         /*
170                          * This could be optimized (use queueid instead of
171                          * portid), but it is not called so often
172                          */
173                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
174                                 if (qconf->tx_mbufs[portid].len == 0)
175                                         continue;
176                                 send_burst(qconf,
177                                         qconf->tx_mbufs[portid].len,
178                                         portid);
179                                 qconf->tx_mbufs[portid].len = 0;
180                         }
181
182                         prev_tsc = cur_tsc;
183                 }
184
185                 /*
186                  * Read packet from RX queues
187                  */
188                 for (i = 0; i < qconf->n_rx_queue; ++i) {
189                         portid = qconf->rx_queue_list[i].port_id;
190                         queueid = qconf->rx_queue_list[i].queue_id;
191                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst,
192                                 MAX_PKT_BURST);
193                         if (nb_rx == 0)
194                                 continue;
195
196                         /*
197                          * For SSE4_1 use ENABLE_MULTI_BUFFER_OPTIMIZE=1
198                          * code.
199                          */
200 #if defined(__SSE4_1__)
201                         l3fwd_lpm_send_packets(nb_rx, pkts_burst,
202                                                 portid, qconf);
203 #else
204                         l3fwd_lpm_no_opt_send_packets(nb_rx, pkts_burst,
205                                                         portid, qconf);
206 #endif /* __SSE_4_1__ */
207                 }
208         }
209
210         return 0;
211 }
212
213 void
214 setup_lpm(const int socketid)
215 {
216         struct rte_lpm6_config config;
217         unsigned i;
218         int ret;
219         char s[64];
220
221         /* create the LPM table */
222         snprintf(s, sizeof(s), "IPV4_L3FWD_LPM_%d", socketid);
223         ipv4_l3fwd_lpm_lookup_struct[socketid] = rte_lpm_create(s, socketid,
224                                 IPV4_L3FWD_LPM_MAX_RULES, 0);
225         if (ipv4_l3fwd_lpm_lookup_struct[socketid] == NULL)
226                 rte_exit(EXIT_FAILURE,
227                         "Unable to create the l3fwd LPM table on socket %d\n",
228                         socketid);
229
230         /* populate the LPM table */
231         for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) {
232
233                 /* skip unused ports */
234                 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
235                                 enabled_port_mask) == 0)
236                         continue;
237
238                 ret = rte_lpm_add(ipv4_l3fwd_lpm_lookup_struct[socketid],
239                         ipv4_l3fwd_lpm_route_array[i].ip,
240                         ipv4_l3fwd_lpm_route_array[i].depth,
241                         ipv4_l3fwd_lpm_route_array[i].if_out);
242
243                 if (ret < 0) {
244                         rte_exit(EXIT_FAILURE,
245                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
246                                 i, socketid);
247                 }
248
249                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
250                         (unsigned)ipv4_l3fwd_lpm_route_array[i].ip,
251                         ipv4_l3fwd_lpm_route_array[i].depth,
252                         ipv4_l3fwd_lpm_route_array[i].if_out);
253         }
254
255         /* create the LPM6 table */
256         snprintf(s, sizeof(s), "IPV6_L3FWD_LPM_%d", socketid);
257
258         config.max_rules = IPV6_L3FWD_LPM_MAX_RULES;
259         config.number_tbl8s = IPV6_L3FWD_LPM_NUMBER_TBL8S;
260         config.flags = 0;
261         ipv6_l3fwd_lpm_lookup_struct[socketid] = rte_lpm6_create(s, socketid,
262                                 &config);
263         if (ipv6_l3fwd_lpm_lookup_struct[socketid] == NULL)
264                 rte_exit(EXIT_FAILURE,
265                         "Unable to create the l3fwd LPM table on socket %d\n",
266                         socketid);
267
268         /* populate the LPM table */
269         for (i = 0; i < IPV6_L3FWD_LPM_NUM_ROUTES; i++) {
270
271                 /* skip unused ports */
272                 if ((1 << ipv6_l3fwd_lpm_route_array[i].if_out &
273                                 enabled_port_mask) == 0)
274                         continue;
275
276                 ret = rte_lpm6_add(ipv6_l3fwd_lpm_lookup_struct[socketid],
277                         ipv6_l3fwd_lpm_route_array[i].ip,
278                         ipv6_l3fwd_lpm_route_array[i].depth,
279                         ipv6_l3fwd_lpm_route_array[i].if_out);
280
281                 if (ret < 0) {
282                         rte_exit(EXIT_FAILURE,
283                                 "Unable to add entry %u to the l3fwd LPM table on socket %d\n",
284                                 i, socketid);
285                 }
286
287                 printf("LPM: Adding route %s / %d (%d)\n",
288                         "IPV6",
289                         ipv6_l3fwd_lpm_route_array[i].depth,
290                         ipv6_l3fwd_lpm_route_array[i].if_out);
291         }
292 }
293
294 /* Return ipv4/ipv6 lpm fwd lookup struct. */
295 void *
296 lpm_get_ipv4_l3fwd_lookup_struct(const int socketid)
297 {
298         return ipv4_l3fwd_lpm_lookup_struct[socketid];
299 }
300
301 void *
302 lpm_get_ipv6_l3fwd_lookup_struct(const int socketid)
303 {
304         return ipv6_l3fwd_lpm_lookup_struct[socketid];
305 }