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, 240, 0), 24, 0 },
67 { IPv4(192, 168, 241, 0), 24, 1 },
71 { IPv4(192, 168, 115, 0), 24, 2 },
72 { IPv4(192, 168, 116, 0), 24, 3 },
73 { IPv4(192, 168, 65, 0), 24, 2 },
74 { IPv4(192, 168, 66, 0), 24, 3 },
76 { IPv4(192, 168, 210, 0), 24, 2 },
77 { IPv4(192, 168, 211, 0), 24, 3 },
79 { IPv4(192, 168, 245, 0), 24, 2 },
80 { IPv4(192, 168, 246, 0), 24, 3 },
83 static struct ip6_route rt_ip6_ep0[] = {
86 { { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
87 0x22, 0x22, 0x22, 0x22, 0x22, 0x55, 0x55 }, 116, 0 },
88 { { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
89 0x22, 0x22, 0x22, 0x22, 0x22, 0x66, 0x66 }, 116, 1 },
92 { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
93 0xaa, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
94 { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbb,
95 0xbb, 0xbb, 0xbb, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
96 { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
97 0x55, 0x55, 0x55, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
98 { { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66,
99 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
102 static struct ip4_route rt_ip4_ep1[] = {
105 { IPv4(172, 16, 1, 5), 32, 0 },
106 { IPv4(172, 16, 1, 6), 32, 1 },
108 { IPv4(192, 168, 245, 0), 24, 0 },
109 { IPv4(192, 168, 246, 0), 24, 1 },
113 { IPv4(192, 168, 105, 0), 24, 2 },
114 { IPv4(192, 168, 106, 0), 24, 3 },
115 { IPv4(192, 168, 55, 0), 24, 2 },
116 { IPv4(192, 168, 56, 0), 24, 3 },
118 { IPv4(192, 168, 200, 0), 24, 2 },
119 { IPv4(192, 168, 201, 0), 24, 3 },
121 { IPv4(192, 168, 240, 0), 24, 2 },
122 { IPv4(192, 168, 241, 0), 24, 3 },
125 static struct ip6_route rt_ip6_ep1[] = {
128 { { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
129 0x11, 0x11, 0x11, 0x11, 0x11, 0x55, 0x55 }, 116, 0 },
130 { { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
131 0x11, 0x11, 0x11, 0x11, 0x11, 0x66, 0x66 }, 116, 1 },
135 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
136 0xaa, 0xaa, 0xaa, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
137 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbb,
138 0xbb, 0xbb, 0xbb, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
139 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
140 0x55, 0x55, 0x55, 0x00, 0x00, 0x00, 0x00 }, 116, 2 },
141 { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66,
142 0x66, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00 }, 116, 3 },
146 rt_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t ep)
152 struct rte_lpm6 *lpm6;
153 struct ip4_route *rt;
154 struct ip6_route *rt6;
156 uint32_t nb_routes, nb_routes6;
157 struct rte_lpm_config conf = { 0 };
158 struct rte_lpm6_config conf6 = { 0 };
161 rte_exit(EXIT_FAILURE, "NULL context.\n");
163 if (ctx->rt_ip4 != NULL)
164 rte_exit(EXIT_FAILURE, "IPv4 Routing Table for socket %u "
165 "already initialized\n", socket_id);
167 if (ctx->rt_ip6 != NULL)
168 rte_exit(EXIT_FAILURE, "IPv6 Routing Table for socket %u "
169 "already initialized\n", socket_id);
171 printf("Creating IPv4 Routing Table (RT) context with %u max routes\n",
176 nb_routes = RTE_DIM(rt_ip4_ep0);
178 nb_routes6 = RTE_DIM(rt_ip6_ep0);
179 } else if (ep == 1) {
181 nb_routes = RTE_DIM(rt_ip4_ep1);
183 nb_routes6 = RTE_DIM(rt_ip6_ep1);
185 rte_exit(EXIT_FAILURE, "Invalid EP value %u. Only 0 or 1 "
188 /* create the LPM table */
189 snprintf(name, sizeof(name), "%s_%u", "rt_ip4", socket_id);
190 conf.max_rules = RT_IPV4_MAX_RULES;
191 conf.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
192 lpm = rte_lpm_create(name, socket_id, &conf);
194 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
195 "on socket %d\n", name, socket_id);
197 /* populate the LPM table */
198 for (i = 0; i < nb_routes; i++) {
199 ret = rte_lpm_add(lpm, rt[i].ip, rt[i].depth, rt[i].if_out);
201 rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
202 "LPM table on socket %d\n", i, name, socket_id);
204 uint32_t_to_char(rt[i].ip, &a, &b, &c, &d);
205 printf("LPM: Adding route %hhu.%hhu.%hhu.%hhu/%hhu (%hhu)\n",
206 a, b, c, d, rt[i].depth, rt[i].if_out);
209 snprintf(name, sizeof(name), "%s_%u", "rt_ip6", socket_id);
210 conf6.max_rules = RT_IPV6_MAX_RULES;
211 conf6.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
212 lpm6 = rte_lpm6_create(name, socket_id, &conf6);
214 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
215 "on socket %d\n", name, socket_id);
217 /* populate the LPM table */
218 for (i = 0; i < nb_routes6; i++) {
219 ret = rte_lpm6_add(lpm6, rt6[i].ip, rt6[i].depth,
222 rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
223 "LPM table on socket %d\n", i, name, socket_id);
225 printf("LPM6: Adding route "
226 " %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hhx (%hhx)\n",
227 (uint16_t)((rt6[i].ip[0] << 8) | rt6[i].ip[1]),
228 (uint16_t)((rt6[i].ip[2] << 8) | rt6[i].ip[3]),
229 (uint16_t)((rt6[i].ip[4] << 8) | rt6[i].ip[5]),
230 (uint16_t)((rt6[i].ip[6] << 8) | rt6[i].ip[7]),
231 (uint16_t)((rt6[i].ip[8] << 8) | rt6[i].ip[9]),
232 (uint16_t)((rt6[i].ip[10] << 8) | rt6[i].ip[11]),
233 (uint16_t)((rt6[i].ip[12] << 8) | rt6[i].ip[13]),
234 (uint16_t)((rt6[i].ip[14] << 8) | rt6[i].ip[15]),
235 rt6[i].depth, rt6[i].if_out);
238 ctx->rt_ip4 = (struct rt_ctx *)lpm;
239 ctx->rt_ip6 = (struct rt_ctx *)lpm6;