4 * Copyright(c) 2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #include <sys/types.h>
40 #include <rte_errno.h>
45 #define RT_IPV4_MAX_RULES 1024
46 #define RT_IPV6_MAX_RULES 1024
60 static struct ip4_route rt_ip4_ep0[] = {
63 { IPv4(172, 16, 2, 5), 32, 0 },
64 { IPv4(172, 16, 2, 6), 32, 1 },
66 { IPv4(192, 168, 175, 0), 24, 0 },
67 { IPv4(192, 168, 176, 0), 24, 1 },
69 { IPv4(192, 168, 240, 0), 24, 0 },
70 { IPv4(192, 168, 241, 0), 24, 1 },
74 { IPv4(192, 168, 115, 0), 24, 2 },
75 { IPv4(192, 168, 116, 0), 24, 3 },
76 { IPv4(192, 168, 65, 0), 24, 2 },
77 { IPv4(192, 168, 66, 0), 24, 3 },
79 { IPv4(192, 168, 185, 0), 24, 2 },
80 { IPv4(192, 168, 186, 0), 24, 3 },
82 { IPv4(192, 168, 210, 0), 24, 2 },
83 { IPv4(192, 168, 211, 0), 24, 3 },
85 { IPv4(192, 168, 245, 0), 24, 2 },
86 { IPv4(192, 168, 246, 0), 24, 3 },
89 static struct ip6_route rt_ip6_ep0[] = {
92 { { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
93 0x22, 0x22, 0x22, 0x22, 0x22, 0x55, 0x55 }, 116, 0 },
94 { { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
95 0x22, 0x22, 0x22, 0x22, 0x22, 0x66, 0x66 }, 116, 1 },
97 { { 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 116, 0 },
99 { { 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11,
100 0x11, 0x11, 0x11, 0x00, 0x00, 0x00, 0x00 }, 116, 1 },
103 { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
104 0xaa, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
105 { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbb,
106 0xbb, 0xbb, 0xbb, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
107 { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
108 0x55, 0x55, 0x55, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
109 { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66,
110 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
112 { { 0xff, 0xff, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
114 { { 0xff, 0xff, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11,
115 0x11, 0x11, 0x11, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
118 static struct ip4_route rt_ip4_ep1[] = {
121 { IPv4(172, 16, 1, 5), 32, 0 },
122 { IPv4(172, 16, 1, 6), 32, 1 },
124 { IPv4(192, 168, 185, 0), 24, 0 },
125 { IPv4(192, 168, 186, 0), 24, 1 },
127 { IPv4(192, 168, 245, 0), 24, 0 },
128 { IPv4(192, 168, 246, 0), 24, 1 },
132 { IPv4(192, 168, 105, 0), 24, 2 },
133 { IPv4(192, 168, 106, 0), 24, 3 },
134 { IPv4(192, 168, 55, 0), 24, 2 },
135 { IPv4(192, 168, 56, 0), 24, 3 },
137 { IPv4(192, 168, 175, 0), 24, 2 },
138 { IPv4(192, 168, 176, 0), 24, 3 },
140 { IPv4(192, 168, 200, 0), 24, 2 },
141 { IPv4(192, 168, 201, 0), 24, 3 },
143 { IPv4(192, 168, 240, 0), 24, 2 },
144 { IPv4(192, 168, 241, 0), 24, 3 },
147 static struct ip6_route rt_ip6_ep1[] = {
150 { { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
151 0x11, 0x11, 0x11, 0x11, 0x11, 0x55, 0x55 }, 116, 0 },
152 { { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
153 0x11, 0x11, 0x11, 0x11, 0x11, 0x66, 0x66 }, 116, 1 },
155 { { 0xff, 0xff, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 116, 0 },
157 { { 0xff, 0xff, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11,
158 0x11, 0x11, 0x11, 0x00, 0x00, 0x00, 0x00 }, 116, 1 },
162 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
163 0xaa, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
164 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbb,
165 0xbb, 0xbb, 0xbb, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
166 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
167 0x55, 0x55, 0x55, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
168 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66,
169 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
171 { { 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x00,
172 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
173 { { 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11,
174 0x11, 0x11, 0x11, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
178 rt_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t ep)
184 struct rte_lpm6 *lpm6;
185 struct ip4_route *rt;
186 struct ip6_route *rt6;
188 uint32_t nb_routes, nb_routes6;
189 struct rte_lpm_config conf = { 0 };
190 struct rte_lpm6_config conf6 = { 0 };
193 rte_exit(EXIT_FAILURE, "NULL context.\n");
195 if (ctx->rt_ip4 != NULL)
196 rte_exit(EXIT_FAILURE, "IPv4 Routing Table for socket %u "
197 "already initialized\n", socket_id);
199 if (ctx->rt_ip6 != NULL)
200 rte_exit(EXIT_FAILURE, "IPv6 Routing Table for socket %u "
201 "already initialized\n", socket_id);
203 printf("Creating IPv4 Routing Table (RT) context with %u max routes\n",
208 nb_routes = RTE_DIM(rt_ip4_ep0);
210 nb_routes6 = RTE_DIM(rt_ip6_ep0);
211 } else if (ep == 1) {
213 nb_routes = RTE_DIM(rt_ip4_ep1);
215 nb_routes6 = RTE_DIM(rt_ip6_ep1);
217 rte_exit(EXIT_FAILURE, "Invalid EP value %u. Only 0 or 1 "
220 /* create the LPM table */
221 snprintf(name, sizeof(name), "%s_%u", "rt_ip4", socket_id);
222 conf.max_rules = RT_IPV4_MAX_RULES;
223 conf.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
224 lpm = rte_lpm_create(name, socket_id, &conf);
226 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
227 "on socket %d\n", name, socket_id);
229 /* populate the LPM table */
230 for (i = 0; i < nb_routes; i++) {
231 ret = rte_lpm_add(lpm, rt[i].ip, rt[i].depth, rt[i].if_out);
233 rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
234 "LPM table on socket %d\n", i, name, socket_id);
236 uint32_t_to_char(rt[i].ip, &a, &b, &c, &d);
237 printf("LPM: Adding route %hhu.%hhu.%hhu.%hhu/%hhu (%hhu)\n",
238 a, b, c, d, rt[i].depth, rt[i].if_out);
241 snprintf(name, sizeof(name), "%s_%u", "rt_ip6", socket_id);
242 conf6.max_rules = RT_IPV6_MAX_RULES;
243 conf6.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
244 lpm6 = rte_lpm6_create(name, socket_id, &conf6);
246 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
247 "on socket %d\n", name, socket_id);
249 /* populate the LPM table */
250 for (i = 0; i < nb_routes6; i++) {
251 ret = rte_lpm6_add(lpm6, rt6[i].ip, rt6[i].depth,
254 rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
255 "LPM table on socket %d\n", i, name, socket_id);
257 printf("LPM6: Adding route "
258 " %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hhx (%hhx)\n",
259 (uint16_t)((rt6[i].ip[0] << 8) | rt6[i].ip[1]),
260 (uint16_t)((rt6[i].ip[2] << 8) | rt6[i].ip[3]),
261 (uint16_t)((rt6[i].ip[4] << 8) | rt6[i].ip[5]),
262 (uint16_t)((rt6[i].ip[6] << 8) | rt6[i].ip[7]),
263 (uint16_t)((rt6[i].ip[8] << 8) | rt6[i].ip[9]),
264 (uint16_t)((rt6[i].ip[10] << 8) | rt6[i].ip[11]),
265 (uint16_t)((rt6[i].ip[12] << 8) | rt6[i].ip[13]),
266 (uint16_t)((rt6[i].ip[14] << 8) | rt6[i].ip[15]),
267 rt6[i].depth, rt6[i].if_out);
270 ctx->rt_ip4 = (struct rt_ctx *)lpm;
271 ctx->rt_ip6 = (struct rt_ctx *)lpm6;