2 * Copyright (c) 2014 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.
18 #include <arpa/inet.h>
21 #include <sys/socket.h>
23 #include <netinet/in.h>
29 #include "classifier.h"
30 #include "command-line.h"
33 #include "dynamic-string.h"
37 #include "ovs-router.h"
38 #include "ovs-router-linux.h"
42 static struct classifier cls;
44 struct ovs_router_entry {
46 char output_bridge[IFNAMSIZ];
53 static struct ovs_router_entry *
54 ovs_router_entry_cast(const struct cls_rule *cr)
56 if (offsetof(struct ovs_router_entry, cr) == 0) {
57 return CONTAINER_OF(cr, struct ovs_router_entry, cr);
59 return cr ? CONTAINER_OF(cr, struct ovs_router_entry, cr) : NULL;
64 ovs_router_lookup(ovs_be32 ip_dst, char output_bridge[], ovs_be32 *gw)
66 const struct cls_rule *cr;
67 struct flow flow = {.nw_dst = ip_dst};
69 cr = classifier_lookup(&cls, &flow, NULL);
71 struct ovs_router_entry *p = ovs_router_entry_cast(cr);
73 strncpy(output_bridge, p->output_bridge, IFNAMSIZ);
81 rt_entry_free(struct ovs_router_entry *p)
83 cls_rule_destroy(&p->cr);
87 static void rt_init_match(struct match *match, ovs_be32 ip_dst, uint8_t plen)
91 mask = be32_prefix_mask(plen);
93 ip_dst &= mask; /* Clear out insignificant bits. */
94 memset(match, 0, sizeof *match);
95 match->flow.nw_dst = ip_dst;
96 match->wc.masks.nw_dst = mask;
100 ovs_router_insert__(uint8_t priority, ovs_be32 ip_dst, uint8_t plen,
101 const char output_bridge[],
104 const struct cls_rule *cr;
105 struct ovs_router_entry *p;
108 rt_init_match(&match, ip_dst, plen);
110 p = xzalloc(sizeof *p);
111 strncpy(p->output_bridge, output_bridge, IFNAMSIZ);
113 p->nw_addr = match.flow.nw_dst;
115 p->priority = priority;
116 cls_rule_init(&p->cr, &match, priority); /* Longest prefix matches first. */
118 cr = classifier_replace(&cls, &p->cr);
120 /* An old rule with the same match was displaced. */
121 ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
123 seq_change(tnl_conf_seq);
127 ovs_router_insert(ovs_be32 ip_dst, uint8_t plen, const char output_bridge[],
130 ovs_router_insert__(plen, ip_dst, plen, output_bridge, gw);
134 rt_entry_delete(uint8_t priority, ovs_be32 ip_dst, uint8_t plen)
136 const struct cls_rule *cr;
137 struct cls_rule rule;
140 rt_init_match(&match, ip_dst, plen);
142 cls_rule_init(&rule, &match, priority);
144 /* Find the exact rule. */
145 cr = classifier_find_rule_exactly(&cls, &rule);
148 cr = classifier_remove(&cls, cr);
151 ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
159 scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
161 int len, max_plen, n;
162 int slen = strlen(s);
163 uint8_t *ip = (uint8_t *)addr;
166 if (!ovs_scan(s, "%"SCNu8"%n", &ip[0], &n)) {
171 for (int i = 1; i < 4; i++) {
172 if (ovs_scan(s + len, ".%"SCNu8"%n", &ip[i], &n)) {
179 if (len == slen && max_plen == 32) {
183 if (ovs_scan(s + len, "/%u%n", plen, &n)
184 && len + n == slen && *plen <= max_plen) {
191 ovs_router_add(struct unixctl_conn *conn, int argc,
192 const char *argv[], void *aux OVS_UNUSED)
197 if (scan_ipv4_route(argv[1], &ip, &plen)) {
199 inet_pton(AF_INET, argv[3], (struct in_addr *)&gw);
203 ovs_router_insert__(plen + 32, ip, plen, argv[2], gw);
204 unixctl_command_reply(conn, "OK");
206 unixctl_command_reply(conn, "Invalid parameters");
211 ovs_router_del(struct unixctl_conn *conn, int argc OVS_UNUSED,
212 const char *argv[], void *aux OVS_UNUSED)
217 if (scan_ipv4_route(argv[1], &ip, &plen)) {
219 if (rt_entry_delete(plen + 32, ip, plen)) {
220 unixctl_command_reply(conn, "OK");
221 seq_change(tnl_conf_seq);
223 unixctl_command_reply(conn, "Not found");
226 unixctl_command_reply(conn, "Invalid parameters");
231 ovs_router_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
232 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
234 struct ovs_router_entry *rt;
235 struct ds ds = DS_EMPTY_INITIALIZER;
237 ds_put_format(&ds, "Route Table:\n");
238 CLS_FOR_EACH(rt, cr, &cls) {
239 if (rt->priority == rt->plen) {
240 ds_put_format(&ds, "Cached: ");
242 ds_put_format(&ds, "User: ");
244 ds_put_format(&ds, IP_FMT"/%"PRIu16" dev %s",
245 IP_ARGS(rt->nw_addr), rt->plen,
248 ds_put_format(&ds, " GW "IP_FMT, IP_ARGS(rt->gw));
250 ds_put_format(&ds, "\n");
252 unixctl_command_reply(conn, ds_cstr(&ds));
257 ovs_router_flush(void)
259 struct ovs_router_entry *rt;
261 CLS_FOR_EACH(rt, cr, &cls) {
262 if (rt->priority == rt->plen) {
263 classifier_remove(&cls, &rt->cr);
266 seq_change(tnl_conf_seq);
269 /* May not be called more than once. */
271 ovs_router_unixctl_register(void)
273 classifier_init(&cls, NULL);
274 unixctl_command_register("ovs/route/add", "ipv4_addr/prefix_len out_br_name gw", 2, 3,
275 ovs_router_add, NULL);
276 unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
277 unixctl_command_register("ovs/route/del", "ipv4_addr/prefix_len", 1, 1, ovs_router_del,