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>
46 #define RT_IPV4_MAX_RULES 1024
47 #define RT_IPV6_MAX_RULES 1024
61 struct ip4_route rt_ip4[RT_IPV4_MAX_RULES];
64 struct ip6_route rt_ip6[RT_IPV4_MAX_RULES];
68 parse_rt_tokens(char **tokens, uint32_t n_tokens,
69 struct parse_status *status)
72 uint32_t *n_rts = NULL;
73 struct ip4_route *route_ipv4 = NULL;
74 struct ip6_route *route_ipv6 = NULL;
76 if (strcmp(tokens[0], "ipv4") == 0) {
78 route_ipv4 = &rt_ip4[*n_rts];
80 APP_CHECK(*n_rts <= RT_IPV4_MAX_RULES - 1, status,
81 "too many rt rules, abort insertion\n");
82 if (status->status < 0)
85 } else if (strcmp(tokens[0], "ipv6") == 0) {
87 route_ipv6 = &rt_ip6[*n_rts];
89 APP_CHECK(*n_rts <= RT_IPV6_MAX_RULES - 1, status,
90 "too many rt rules, abort insertion\n");
91 if (status->status < 0)
94 APP_CHECK(0, status, "unrecognized input \"%s\"",
99 for (ti = 1; ti < n_tokens; ti++) {
100 if (strcmp(tokens[ti], "dst") == 0) {
101 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
102 if (status->status < 0)
105 if (route_ipv4 != NULL) {
109 APP_CHECK(parse_ipv4_addr(tokens[ti],
110 &ip, &depth) == 0, status,
111 "unrecognized input \"%s\", "
112 "expect valid ipv4 addr",
114 if (status->status < 0)
116 route_ipv4->ip = rte_bswap32(
117 (uint32_t)ip.s_addr);
118 route_ipv4->depth = (uint8_t)depth;
123 APP_CHECK(parse_ipv6_addr(tokens[ti],
124 &ip, &depth) == 0, status,
125 "unrecognized input \"%s\", "
126 "expect valid ipv6 address",
128 if (status->status < 0)
130 memcpy(route_ipv6->ip, ip.s6_addr, 16);
131 route_ipv6->depth = (uint8_t)depth;
135 if (strcmp(tokens[ti], "port") == 0) {
136 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
137 if (status->status < 0)
139 APP_CHECK_TOKEN_IS_NUM(tokens, ti, status);
140 if (status->status < 0)
142 if (route_ipv4 != NULL)
143 route_ipv4->if_out = atoi(tokens[ti]);
145 route_ipv6->if_out = atoi(tokens[ti]);
153 rt_init(struct socket_ctx *ctx, int32_t socket_id)
159 struct rte_lpm6 *lpm6;
161 struct rte_lpm_config conf = { 0 };
162 struct rte_lpm6_config conf6 = { 0 };
165 rte_exit(EXIT_FAILURE, "NULL context.\n");
167 if (ctx->rt_ip4 != NULL)
168 rte_exit(EXIT_FAILURE, "IPv4 Routing Table for socket %u "
169 "already initialized\n", socket_id);
171 if (ctx->rt_ip6 != NULL)
172 rte_exit(EXIT_FAILURE, "IPv6 Routing Table for socket %u "
173 "already initialized\n", socket_id);
175 if (nb_rt_ip4 == 0 && nb_rt_ip6 == 0)
176 RTE_LOG(WARNING, IPSEC, "No Routing rule specified\n");
178 printf("Creating IPv4 Routing Table (RT) context with %u max routes\n",
181 /* create the LPM table */
182 snprintf(name, sizeof(name), "%s_%u", "rt_ip4", socket_id);
183 conf.max_rules = RT_IPV4_MAX_RULES;
184 conf.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
185 lpm = rte_lpm_create(name, socket_id, &conf);
187 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
188 "on socket %d\n", name, socket_id);
190 /* populate the LPM table */
191 for (i = 0; i < nb_rt_ip4; i++) {
192 ret = rte_lpm_add(lpm, rt_ip4[i].ip, rt_ip4[i].depth,
195 rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
196 "LPM table on socket %d\n", i, name, socket_id);
198 uint32_t_to_char(rt_ip4[i].ip, &a, &b, &c, &d);
199 printf("LPM: Adding route %hhu.%hhu.%hhu.%hhu/%hhu (%hhu)\n",
200 a, b, c, d, rt_ip4[i].depth,
204 snprintf(name, sizeof(name), "%s_%u", "rt_ip6", socket_id);
205 conf6.max_rules = RT_IPV6_MAX_RULES;
206 conf6.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
207 lpm6 = rte_lpm6_create(name, socket_id, &conf6);
209 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
210 "on socket %d\n", name, socket_id);
212 /* populate the LPM table */
213 for (i = 0; i < nb_rt_ip6; i++) {
214 ret = rte_lpm6_add(lpm6, rt_ip6[i].ip, rt_ip6[i].depth,
217 rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
218 "LPM table on socket %d\n", i, name, socket_id);
220 printf("LPM6: Adding route "
221 " %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hhx (%hhx)\n",
222 (uint16_t)((rt_ip6[i].ip[0] << 8) | rt_ip6[i].ip[1]),
223 (uint16_t)((rt_ip6[i].ip[2] << 8) | rt_ip6[i].ip[3]),
224 (uint16_t)((rt_ip6[i].ip[4] << 8) | rt_ip6[i].ip[5]),
225 (uint16_t)((rt_ip6[i].ip[6] << 8) | rt_ip6[i].ip[7]),
226 (uint16_t)((rt_ip6[i].ip[8] << 8) | rt_ip6[i].ip[9]),
227 (uint16_t)((rt_ip6[i].ip[10] << 8) | rt_ip6[i].ip[11]),
228 (uint16_t)((rt_ip6[i].ip[12] << 8) | rt_ip6[i].ip[13]),
229 (uint16_t)((rt_ip6[i].ip[14] << 8) | rt_ip6[i].ip[15]),
230 rt_ip6[i].depth, rt_ip6[i].if_out);
233 ctx->rt_ip4 = (struct rt_ctx *)lpm;
234 ctx->rt_ip6 = (struct rt_ctx *)lpm6;