2 * Copyright (c) 2014, 2015 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ovs-router.h"
21 #include <arpa/inet.h>
24 #include <sys/socket.h>
26 #include <netinet/in.h>
32 #include "classifier.h"
33 #include "command-line.h"
36 #include "dynamic-string.h"
40 #include "ovs-thread.h"
41 #include "route-table.h"
42 #include "tnl-ports.h"
46 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
47 static struct classifier cls;
49 struct ovs_router_entry {
51 char output_bridge[IFNAMSIZ];
53 struct in6_addr nw_addr;
58 static struct ovs_router_entry *
59 ovs_router_entry_cast(const struct cls_rule *cr)
61 if (offsetof(struct ovs_router_entry, cr) == 0) {
62 return CONTAINER_OF(cr, struct ovs_router_entry, cr);
64 return cr ? CONTAINER_OF(cr, struct ovs_router_entry, cr) : NULL;
69 ovs_router_lookup(const struct in6_addr *ip6_dst, char output_bridge[],
72 const struct cls_rule *cr;
73 struct flow flow = {.ipv6_dst = *ip6_dst};
75 cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
77 struct ovs_router_entry *p = ovs_router_entry_cast(cr);
79 ovs_strlcpy(output_bridge, p->output_bridge, IFNAMSIZ);
87 ovs_router_lookup4(ovs_be32 ip_dst, char output_bridge[], ovs_be32 *gw)
89 struct in6_addr ip6_dst = in6_addr_mapped_ipv4(ip_dst);
92 if (ovs_router_lookup(&ip6_dst, output_bridge, &gw6)) {
93 *gw = in6_addr_get_mapped_ipv4(&gw6);
96 return route_table_fallback_lookup(ip_dst, output_bridge, gw);
100 rt_entry_free(struct ovs_router_entry *p)
102 cls_rule_destroy(&p->cr);
106 static void rt_init_match(struct match *match, const struct in6_addr *ip6_dst,
110 struct in6_addr mask;
112 mask = ipv6_create_mask(plen);
114 dst = ipv6_addr_bitand(ip6_dst, &mask);
115 memset(match, 0, sizeof *match);
116 match->flow.ipv6_dst = dst;
117 match->wc.masks.ipv6_dst = mask;
121 ovs_router_insert__(uint8_t priority, const struct in6_addr *ip6_dst,
122 uint8_t plen, const char output_bridge[],
123 const struct in6_addr *gw)
125 const struct cls_rule *cr;
126 struct ovs_router_entry *p;
129 rt_init_match(&match, ip6_dst, plen);
131 p = xzalloc(sizeof *p);
132 ovs_strlcpy(p->output_bridge, output_bridge, sizeof p->output_bridge);
133 if (ipv6_addr_is_set(gw)) {
136 p->nw_addr = match.flow.ipv6_dst;
138 p->priority = priority;
139 /* Longest prefix matches first. */
140 cls_rule_init(&p->cr, &match, priority);
142 ovs_mutex_lock(&mutex);
143 cr = classifier_replace(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
144 ovs_mutex_unlock(&mutex);
147 /* An old rule with the same match was displaced. */
148 ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
150 tnl_port_map_insert_ipdev(output_bridge);
151 seq_change(tnl_conf_seq);
155 ovs_router_insert(const struct in6_addr *ip_dst, uint8_t plen,
156 const char output_bridge[], const struct in6_addr *gw)
158 ovs_router_insert__(plen, ip_dst, plen, output_bridge, gw);
163 __rt_entry_delete(const struct cls_rule *cr)
165 struct ovs_router_entry *p = ovs_router_entry_cast(cr);
167 tnl_port_map_delete_ipdev(p->output_bridge);
169 cr = classifier_remove(&cls, cr);
171 ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
178 rt_entry_delete(uint8_t priority, const struct in6_addr *ip6_dst, uint8_t plen)
180 const struct cls_rule *cr;
181 struct cls_rule rule;
185 rt_init_match(&match, ip6_dst, plen);
187 cls_rule_init(&rule, &match, priority);
189 /* Find the exact rule. */
190 cr = classifier_find_rule_exactly(&cls, &rule, CLS_MAX_VERSION);
192 ovs_mutex_lock(&mutex);
193 res = __rt_entry_delete(cr);
194 ovs_mutex_unlock(&mutex);
200 scan_ipv6_route(const char *s, struct in6_addr *addr, unsigned int *plen)
202 char *error = ipv6_parse_cidr(s, addr, plen);
211 scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
213 char *error = ip_parse_cidr(s, addr, plen);
222 ovs_router_add(struct unixctl_conn *conn, int argc,
223 const char *argv[], void *aux OVS_UNUSED)
230 if (scan_ipv4_route(argv[1], &ip, &plen)) {
232 if (argc > 3 && !ip_parse(argv[3], &gw)) {
233 unixctl_command_reply_error(conn, "Invalid gateway");
236 in6_addr_set_mapped_ipv4(&ip6, ip);
237 in6_addr_set_mapped_ipv4(&gw6, gw);
239 } else if (scan_ipv6_route(argv[1], &ip6, &plen)) {
241 if (argc > 3 && !ipv6_parse(argv[3], &gw6)) {
242 unixctl_command_reply_error(conn, "Invalid IPv6 gateway");
246 unixctl_command_reply_error(conn, "Invalid parameters");
249 ovs_router_insert__(plen + 32, &ip6, plen, argv[2], &gw6);
250 unixctl_command_reply(conn, "OK");
254 ovs_router_del(struct unixctl_conn *conn, int argc OVS_UNUSED,
255 const char *argv[], void *aux OVS_UNUSED)
261 if (scan_ipv4_route(argv[1], &ip, &plen)) {
262 in6_addr_set_mapped_ipv4(&ip6, ip);
264 } else if (!scan_ipv6_route(argv[1], &ip6, &plen)) {
265 unixctl_command_reply_error(conn, "Invalid parameters");
268 if (rt_entry_delete(plen + 32, &ip6, plen)) {
269 unixctl_command_reply(conn, "OK");
270 seq_change(tnl_conf_seq);
272 unixctl_command_reply_error(conn, "Not found");
277 ovs_router_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
278 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
280 struct ovs_router_entry *rt;
281 struct ds ds = DS_EMPTY_INITIALIZER;
283 ds_put_format(&ds, "Route Table:\n");
284 CLS_FOR_EACH(rt, cr, &cls) {
286 if (rt->priority == rt->plen) {
287 ds_put_format(&ds, "Cached: ");
289 ds_put_format(&ds, "User: ");
291 ipv6_format_mapped(&rt->nw_addr, &ds);
293 if (IN6_IS_ADDR_V4MAPPED(&rt->nw_addr)) {
296 ds_put_format(&ds, "/%"PRIu16" dev %s", plen, rt->output_bridge);
297 if (ipv6_addr_is_set(&rt->gw)) {
298 ds_put_format(&ds, " GW ");
299 ipv6_format_mapped(&rt->gw, &ds);
301 ds_put_format(&ds, "\n");
303 unixctl_command_reply(conn, ds_cstr(&ds));
308 ovs_router_lookup_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
309 const char *argv[], void *aux OVS_UNUSED)
314 char iface[IFNAMSIZ];
317 if (scan_ipv4_route(argv[1], &ip, &plen) && plen == 32) {
318 in6_addr_set_mapped_ipv4(&ip6, ip);
319 } else if (!(scan_ipv6_route(argv[1], &ip6, &plen) && plen == 128)) {
320 unixctl_command_reply_error(conn, "Invalid parameters");
324 if (ovs_router_lookup(&ip6, iface, &gw)) {
325 struct ds ds = DS_EMPTY_INITIALIZER;
326 ds_put_format(&ds, "gateway ");
327 ipv6_format_mapped(&ip6, &ds);
328 ds_put_format(&ds, "\ndev %s\n", iface);
329 unixctl_command_reply(conn, ds_cstr(&ds));
332 unixctl_command_reply_error(conn, "Not found");
337 ovs_router_flush(void)
339 struct ovs_router_entry *rt;
341 ovs_mutex_lock(&mutex);
342 classifier_defer(&cls);
343 CLS_FOR_EACH(rt, cr, &cls) {
344 if (rt->priority == rt->plen) {
345 __rt_entry_delete(&rt->cr);
348 classifier_publish(&cls);
349 ovs_mutex_unlock(&mutex);
350 seq_change(tnl_conf_seq);
353 /* May not be called more than once. */
355 ovs_router_init(void)
357 classifier_init(&cls, NULL);
358 unixctl_command_register("ovs/route/add", "ip_addr/prefix_len out_br_name gw", 2, 3,
359 ovs_router_add, NULL);
360 unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
361 unixctl_command_register("ovs/route/del", "ip_addr/prefix_len", 1, 1, ovs_router_del,
363 unixctl_command_register("ovs/route/lookup", "ip_addr", 1, 1,
364 ovs_router_lookup_cmd, NULL);