1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
11 #include <rte_errno.h>
17 #define RT_IPV4_MAX_RULES 1024
18 #define RT_IPV6_MAX_RULES 1024
32 struct ip4_route rt_ip4[RT_IPV4_MAX_RULES];
35 struct ip6_route rt_ip6[RT_IPV4_MAX_RULES];
39 parse_rt_tokens(char **tokens, uint32_t n_tokens,
40 struct parse_status *status)
43 uint32_t *n_rts = NULL;
44 struct ip4_route *route_ipv4 = NULL;
45 struct ip6_route *route_ipv6 = NULL;
47 if (strcmp(tokens[0], "ipv4") == 0) {
49 route_ipv4 = &rt_ip4[*n_rts];
51 APP_CHECK(*n_rts <= RT_IPV4_MAX_RULES - 1, status,
52 "too many rt rules, abort insertion\n");
53 if (status->status < 0)
56 } else if (strcmp(tokens[0], "ipv6") == 0) {
58 route_ipv6 = &rt_ip6[*n_rts];
60 APP_CHECK(*n_rts <= RT_IPV6_MAX_RULES - 1, status,
61 "too many rt rules, abort insertion\n");
62 if (status->status < 0)
65 APP_CHECK(0, status, "unrecognized input \"%s\"",
70 for (ti = 1; ti < n_tokens; ti++) {
71 if (strcmp(tokens[ti], "dst") == 0) {
72 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
73 if (status->status < 0)
76 if (route_ipv4 != NULL) {
80 APP_CHECK(parse_ipv4_addr(tokens[ti],
81 &ip, &depth) == 0, status,
82 "unrecognized input \"%s\", "
83 "expect valid ipv4 addr",
85 if (status->status < 0)
87 route_ipv4->ip = rte_bswap32(
89 route_ipv4->depth = (uint8_t)depth;
94 APP_CHECK(parse_ipv6_addr(tokens[ti],
95 &ip, &depth) == 0, status,
96 "unrecognized input \"%s\", "
97 "expect valid ipv6 address",
99 if (status->status < 0)
101 memcpy(route_ipv6->ip, ip.s6_addr, 16);
102 route_ipv6->depth = (uint8_t)depth;
106 if (strcmp(tokens[ti], "port") == 0) {
107 INCREMENT_TOKEN_INDEX(ti, n_tokens, status);
108 if (status->status < 0)
110 APP_CHECK_TOKEN_IS_NUM(tokens, ti, status);
111 if (status->status < 0)
113 if (route_ipv4 != NULL)
114 route_ipv4->if_out = atoi(tokens[ti]);
116 route_ipv6->if_out = atoi(tokens[ti]);
124 rt_init(struct socket_ctx *ctx, int32_t socket_id)
130 struct rte_lpm6 *lpm6;
132 struct rte_lpm_config conf = { 0 };
133 struct rte_lpm6_config conf6 = { 0 };
136 rte_exit(EXIT_FAILURE, "NULL context.\n");
138 if (ctx->rt_ip4 != NULL)
139 rte_exit(EXIT_FAILURE, "IPv4 Routing Table for socket %u "
140 "already initialized\n", socket_id);
142 if (ctx->rt_ip6 != NULL)
143 rte_exit(EXIT_FAILURE, "IPv6 Routing Table for socket %u "
144 "already initialized\n", socket_id);
146 if (nb_rt_ip4 == 0 && nb_rt_ip6 == 0)
147 RTE_LOG(WARNING, IPSEC, "No Routing rule specified\n");
149 printf("Creating IPv4 Routing Table (RT) context with %u max routes\n",
152 /* create the LPM table */
153 snprintf(name, sizeof(name), "%s_%u", "rt_ip4", socket_id);
154 conf.max_rules = RT_IPV4_MAX_RULES;
155 conf.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
156 lpm = rte_lpm_create(name, socket_id, &conf);
158 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
159 "on socket %d\n", name, socket_id);
161 /* populate the LPM table */
162 for (i = 0; i < nb_rt_ip4; i++) {
163 ret = rte_lpm_add(lpm, rt_ip4[i].ip, rt_ip4[i].depth,
166 rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
167 "LPM table on socket %d\n", i, name, socket_id);
169 uint32_t_to_char(rt_ip4[i].ip, &a, &b, &c, &d);
170 printf("LPM: Adding route %hhu.%hhu.%hhu.%hhu/%hhu (%hhu)\n",
171 a, b, c, d, rt_ip4[i].depth,
175 snprintf(name, sizeof(name), "%s_%u", "rt_ip6", socket_id);
176 conf6.max_rules = RT_IPV6_MAX_RULES;
177 conf6.number_tbl8s = RTE_LPM_TBL8_NUM_ENTRIES;
178 lpm6 = rte_lpm6_create(name, socket_id, &conf6);
180 rte_exit(EXIT_FAILURE, "Unable to create %s LPM table "
181 "on socket %d\n", name, socket_id);
183 /* populate the LPM table */
184 for (i = 0; i < nb_rt_ip6; i++) {
185 ret = rte_lpm6_add(lpm6, rt_ip6[i].ip, rt_ip6[i].depth,
188 rte_exit(EXIT_FAILURE, "Fail to add entry num %u to %s "
189 "LPM table on socket %d\n", i, name, socket_id);
191 printf("LPM6: Adding route "
192 " %hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hhx (%hhx)\n",
193 (uint16_t)((rt_ip6[i].ip[0] << 8) | rt_ip6[i].ip[1]),
194 (uint16_t)((rt_ip6[i].ip[2] << 8) | rt_ip6[i].ip[3]),
195 (uint16_t)((rt_ip6[i].ip[4] << 8) | rt_ip6[i].ip[5]),
196 (uint16_t)((rt_ip6[i].ip[6] << 8) | rt_ip6[i].ip[7]),
197 (uint16_t)((rt_ip6[i].ip[8] << 8) | rt_ip6[i].ip[9]),
198 (uint16_t)((rt_ip6[i].ip[10] << 8) | rt_ip6[i].ip[11]),
199 (uint16_t)((rt_ip6[i].ip[12] << 8) | rt_ip6[i].ip[13]),
200 (uint16_t)((rt_ip6[i].ip[14] << 8) | rt_ip6[i].ip[15]),
201 rt_ip6[i].depth, rt_ip6[i].if_out);
204 ctx->rt_ip4 = (struct rt_ctx *)lpm;
205 ctx->rt_ip6 = (struct rt_ctx *)lpm6;