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;
92 in6_addr_set_mapped_ipv4(&ip6_dst, ip_dst);
93 if (ovs_router_lookup(&ip6_dst, output_bridge, &gw6)) {
94 *gw = in6_addr_get_mapped_ipv4(&gw6);
97 return route_table_fallback_lookup(ip_dst, output_bridge, gw);
101 rt_entry_free(struct ovs_router_entry *p)
103 cls_rule_destroy(&p->cr);
107 static void rt_init_match(struct match *match, const struct in6_addr *ip6_dst,
111 struct in6_addr mask;
113 mask = ipv6_create_mask(plen);
115 dst = ipv6_addr_bitand(ip6_dst, &mask);
116 memset(match, 0, sizeof *match);
117 match->flow.ipv6_dst = dst;
118 match->wc.masks.ipv6_dst = mask;
122 ovs_router_insert__(uint8_t priority, const struct in6_addr *ip6_dst,
123 uint8_t plen, const char output_bridge[],
124 const struct in6_addr *gw)
126 const struct cls_rule *cr;
127 struct ovs_router_entry *p;
130 rt_init_match(&match, ip6_dst, plen);
132 p = xzalloc(sizeof *p);
133 ovs_strlcpy(p->output_bridge, output_bridge, sizeof p->output_bridge);
134 if (ipv6_addr_is_set(gw)) {
137 p->nw_addr = match.flow.ipv6_dst;
139 p->priority = priority;
140 /* Longest prefix matches first. */
141 cls_rule_init(&p->cr, &match, priority);
143 ovs_mutex_lock(&mutex);
144 cr = classifier_replace(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
145 ovs_mutex_unlock(&mutex);
148 /* An old rule with the same match was displaced. */
149 ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
151 tnl_port_map_insert_ipdev(output_bridge);
152 seq_change(tnl_conf_seq);
156 ovs_router_insert(const struct in6_addr *ip_dst, uint8_t plen,
157 const char output_bridge[], const struct in6_addr *gw)
159 ovs_router_insert__(plen, ip_dst, plen, output_bridge, gw);
164 __rt_entry_delete(const struct cls_rule *cr)
166 struct ovs_router_entry *p = ovs_router_entry_cast(cr);
168 tnl_port_map_delete_ipdev(p->output_bridge);
170 cr = classifier_remove(&cls, cr);
172 ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
179 rt_entry_delete(uint8_t priority, const struct in6_addr *ip6_dst, uint8_t plen)
181 const struct cls_rule *cr;
182 struct cls_rule rule;
186 rt_init_match(&match, ip6_dst, plen);
188 cls_rule_init(&rule, &match, priority);
190 /* Find the exact rule. */
191 cr = classifier_find_rule_exactly(&cls, &rule, CLS_MAX_VERSION);
193 ovs_mutex_lock(&mutex);
194 res = __rt_entry_delete(cr);
195 ovs_mutex_unlock(&mutex);
201 scan_ipv6_route(const char *s, struct in6_addr *addr, unsigned int *plen)
204 int slen = strlen(s);
205 char ipv6_s[IPV6_SCAN_LEN + 1];
207 if (ovs_scan(s, IPV6_SCAN_FMT"%n", ipv6_s, &len)
208 && inet_pton(AF_INET6, ipv6_s, addr) == 1) {
213 if (ovs_scan(s + len, "/%u%n", plen, &n)
214 && len + n == slen && *plen <= 128) {
222 scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
224 int len, max_plen, n;
225 int slen = strlen(s);
226 uint8_t *ip = (uint8_t *)addr;
229 if (!ovs_scan(s, "%"SCNu8"%n", &ip[0], &n)) {
234 for (int i = 1; i < 4; i++) {
235 if (ovs_scan(s + len, ".%"SCNu8"%n", &ip[i], &n)) {
242 if (len == slen && max_plen == 32) {
246 if (ovs_scan(s + len, "/%u%n", plen, &n)
247 && len + n == slen && *plen <= max_plen) {
254 ovs_router_add(struct unixctl_conn *conn, int argc,
255 const char *argv[], void *aux OVS_UNUSED)
262 if (scan_ipv4_route(argv[1], &ip, &plen)) {
264 inet_pton(AF_INET, argv[3], (struct in_addr *)&gw);
268 in6_addr_set_mapped_ipv4(&ip6, ip);
269 in6_addr_set_mapped_ipv4(&gw6, gw);
271 } else if (scan_ipv6_route(argv[1], &ip6, &plen)) {
273 inet_pton(AF_INET6, argv[3], &gw6);
278 unixctl_command_reply(conn, "Invalid parameters");
280 ovs_router_insert__(plen + 32, &ip6, plen, argv[2], &gw6);
281 unixctl_command_reply(conn, "OK");
285 ovs_router_del(struct unixctl_conn *conn, int argc OVS_UNUSED,
286 const char *argv[], void *aux OVS_UNUSED)
292 if (scan_ipv4_route(argv[1], &ip, &plen)) {
293 in6_addr_set_mapped_ipv4(&ip6, ip);
295 } else if (!scan_ipv6_route(argv[1], &ip6, &plen)) {
296 unixctl_command_reply(conn, "Invalid parameters");
298 if (rt_entry_delete(plen + 32, &ip6, plen)) {
299 unixctl_command_reply(conn, "OK");
300 seq_change(tnl_conf_seq);
302 unixctl_command_reply(conn, "Not found");
307 ovs_router_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
308 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
310 struct ovs_router_entry *rt;
311 struct ds ds = DS_EMPTY_INITIALIZER;
313 ds_put_format(&ds, "Route Table:\n");
314 CLS_FOR_EACH(rt, cr, &cls) {
316 if (rt->priority == rt->plen) {
317 ds_put_format(&ds, "Cached: ");
319 ds_put_format(&ds, "User: ");
321 print_ipv6_mapped(&ds, &rt->nw_addr);
323 if (IN6_IS_ADDR_V4MAPPED(&rt->nw_addr)) {
326 ds_put_format(&ds, "/%"PRIu16" dev %s", plen, rt->output_bridge);
327 if (ipv6_addr_is_set(&rt->gw)) {
328 ds_put_format(&ds, " GW ");
329 print_ipv6_mapped(&ds, &rt->gw);
331 ds_put_format(&ds, "\n");
333 unixctl_command_reply(conn, ds_cstr(&ds));
338 ovs_router_lookup_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
339 const char *argv[], void *aux OVS_UNUSED)
344 char iface[IFNAMSIZ];
347 if (scan_ipv4_route(argv[1], &ip, &plen) && plen == 32) {
348 in6_addr_set_mapped_ipv4(&ip6, ip);
349 } else if (!(scan_ipv6_route(argv[1], &ip6, &plen) && plen == 128)) {
350 unixctl_command_reply(conn, "Invalid parameters");
353 if (ovs_router_lookup(&ip6, iface, &gw)) {
354 struct ds ds = DS_EMPTY_INITIALIZER;
355 ds_put_format(&ds, "gateway ");
356 print_ipv6_mapped(&ds, &ip6);
357 ds_put_format(&ds, "\ndev %s\n", iface);
358 unixctl_command_reply(conn, ds_cstr(&ds));
361 unixctl_command_reply(conn, "Not found");
366 ovs_router_flush(void)
368 struct ovs_router_entry *rt;
370 ovs_mutex_lock(&mutex);
371 classifier_defer(&cls);
372 CLS_FOR_EACH(rt, cr, &cls) {
373 if (rt->priority == rt->plen) {
374 __rt_entry_delete(&rt->cr);
377 classifier_publish(&cls);
378 ovs_mutex_unlock(&mutex);
379 seq_change(tnl_conf_seq);
382 /* May not be called more than once. */
384 ovs_router_init(void)
386 classifier_init(&cls, NULL);
387 unixctl_command_register("ovs/route/add", "ip_addr/prefix_len out_br_name gw", 2, 3,
388 ovs_router_add, NULL);
389 unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
390 unixctl_command_register("ovs/route/del", "ip_addr/prefix_len", 1, 1, ovs_router_del,
392 unixctl_command_register("ovs/route/lookup", "ip_addr", 1, 1,
393 ovs_router_lookup_cmd, NULL);