ovs-router: Add missing ds_destroy after ds was put.
[cascardo/ovs.git] / lib / ovs-router.c
1 /*
2  * Copyright (c) 2014, 2015 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/socket.h>
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "classifier.h"
30 #include "command-line.h"
31 #include "compiler.h"
32 #include "dpif.h"
33 #include "dynamic-string.h"
34 #include "netdev.h"
35 #include "packets.h"
36 #include "seq.h"
37 #include "ovs-router.h"
38 #include "ovs-thread.h"
39 #include "route-table.h"
40 #include "tnl-ports.h"
41 #include "unixctl.h"
42 #include "util.h"
43
44 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
45 static struct classifier cls;
46
47 struct ovs_router_entry {
48     struct cls_rule cr;
49     char output_bridge[IFNAMSIZ];
50     ovs_be32 gw;
51     ovs_be32 nw_addr;
52     uint8_t plen;
53     uint8_t priority;
54 };
55
56 static struct ovs_router_entry *
57 ovs_router_entry_cast(const struct cls_rule *cr)
58 {
59     if (offsetof(struct ovs_router_entry, cr) == 0) {
60         return CONTAINER_OF(cr, struct ovs_router_entry, cr);
61     } else {
62         return cr ? CONTAINER_OF(cr, struct ovs_router_entry, cr) : NULL;
63     }
64 }
65
66 bool
67 ovs_router_lookup(ovs_be32 ip_dst, char output_bridge[], ovs_be32 *gw)
68 {
69     const struct cls_rule *cr;
70     struct flow flow = {.nw_dst = ip_dst};
71
72     cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
73     if (cr) {
74         struct ovs_router_entry *p = ovs_router_entry_cast(cr);
75
76         ovs_strlcpy(output_bridge, p->output_bridge, IFNAMSIZ);
77         *gw = p->gw;
78         return true;
79     }
80     return route_table_fallback_lookup(ip_dst, output_bridge, gw);
81 }
82
83 static void
84 rt_entry_free(struct ovs_router_entry *p)
85 {
86     cls_rule_destroy(&p->cr);
87     free(p);
88 }
89
90 static void rt_init_match(struct match *match, ovs_be32 ip_dst, uint8_t plen)
91 {
92     ovs_be32 mask;
93
94     mask = be32_prefix_mask(plen);
95
96     ip_dst &= mask; /* Clear out insignificant bits. */
97     memset(match, 0, sizeof *match);
98     match->flow.nw_dst = ip_dst;
99     match->wc.masks.nw_dst = mask;
100 }
101
102 static void
103 ovs_router_insert__(uint8_t priority, ovs_be32 ip_dst, uint8_t plen,
104                     const char output_bridge[],
105                     ovs_be32 gw)
106 {
107     const struct cls_rule *cr;
108     struct ovs_router_entry *p;
109     struct match match;
110
111     rt_init_match(&match, ip_dst, plen);
112
113     p = xzalloc(sizeof *p);
114     ovs_strlcpy(p->output_bridge, output_bridge, sizeof p->output_bridge);
115     p->gw = gw;
116     p->nw_addr = match.flow.nw_dst;
117     p->plen = plen;
118     p->priority = priority;
119     /* Longest prefix matches first. */
120     cls_rule_init(&p->cr, &match, priority);
121
122     ovs_mutex_lock(&mutex);
123     cr = classifier_replace(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
124     ovs_mutex_unlock(&mutex);
125
126     if (cr) {
127         /* An old rule with the same match was displaced. */
128         ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
129     }
130     tnl_port_map_insert_ipdev(output_bridge);
131     seq_change(tnl_conf_seq);
132 }
133
134 void
135 ovs_router_insert(ovs_be32 ip_dst, uint8_t plen, const char output_bridge[],
136                   ovs_be32 gw)
137 {
138     ovs_router_insert__(plen, ip_dst, plen, output_bridge, gw);
139 }
140
141
142 static bool
143 __rt_entry_delete(const struct cls_rule *cr)
144 {
145     struct ovs_router_entry *p = ovs_router_entry_cast(cr);
146
147     tnl_port_map_delete_ipdev(p->output_bridge);
148     /* Remove it. */
149     cr = classifier_remove(&cls, cr);
150     if (cr) {
151         ovsrcu_postpone(rt_entry_free, ovs_router_entry_cast(cr));
152         return true;
153     }
154     return false;
155 }
156
157 static bool
158 rt_entry_delete(uint8_t priority, ovs_be32 ip_dst, uint8_t plen)
159 {
160     const struct cls_rule *cr;
161     struct cls_rule rule;
162     struct match match;
163     bool res = false;
164
165     rt_init_match(&match, ip_dst, plen);
166
167     cls_rule_init(&rule, &match, priority);
168
169     /* Find the exact rule. */
170     cr = classifier_find_rule_exactly(&cls, &rule, CLS_MAX_VERSION);
171     if (cr) {
172         ovs_mutex_lock(&mutex);
173         res = __rt_entry_delete(cr);
174         ovs_mutex_unlock(&mutex);
175     }
176     return res;
177 }
178
179 static bool
180 scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
181 {
182     int len, max_plen, n;
183     int slen = strlen(s);
184     uint8_t *ip = (uint8_t *)addr;
185
186     *addr = htonl(0);
187     if (!ovs_scan(s, "%"SCNu8"%n", &ip[0], &n)) {
188         return false;
189     }
190     len = n;
191     max_plen = 8;
192     for (int i = 1; i < 4; i++) {
193         if (ovs_scan(s + len, ".%"SCNu8"%n", &ip[i], &n)) {
194             len += n;
195             max_plen += 8;
196         } else {
197             break;
198         }
199     }
200     if (len == slen && max_plen == 32) {
201         *plen = 32;
202         return true;
203     }
204     if (ovs_scan(s + len, "/%u%n", plen, &n)
205         && len + n == slen && *plen <= max_plen) {
206         return true;
207     }
208     return false;
209 }
210
211 static void
212 ovs_router_add(struct unixctl_conn *conn, int argc,
213               const char *argv[], void *aux OVS_UNUSED)
214 {
215     ovs_be32 ip, gw;
216     unsigned int plen;
217
218     if (scan_ipv4_route(argv[1], &ip, &plen)) {
219         if (argc > 3) {
220             inet_pton(AF_INET, argv[3], (struct in_addr *)&gw);
221         } else {
222             gw = 0;
223         }
224         ovs_router_insert__(plen + 32, ip, plen, argv[2], gw);
225         unixctl_command_reply(conn, "OK");
226     } else {
227         unixctl_command_reply(conn, "Invalid parameters");
228     }
229 }
230
231 static void
232 ovs_router_del(struct unixctl_conn *conn, int argc OVS_UNUSED,
233               const char *argv[], void *aux OVS_UNUSED)
234 {
235     ovs_be32 ip;
236     unsigned int plen;
237
238     if (scan_ipv4_route(argv[1], &ip, &plen)) {
239
240         if (rt_entry_delete(plen + 32, ip, plen)) {
241             unixctl_command_reply(conn, "OK");
242             seq_change(tnl_conf_seq);
243         } else {
244             unixctl_command_reply(conn, "Not found");
245         }
246     } else {
247         unixctl_command_reply(conn, "Invalid parameters");
248     }
249 }
250
251 static void
252 ovs_router_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
253                const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
254 {
255     struct ovs_router_entry *rt;
256     struct ds ds = DS_EMPTY_INITIALIZER;
257
258     ds_put_format(&ds, "Route Table:\n");
259     CLS_FOR_EACH(rt, cr, &cls) {
260         if (rt->priority == rt->plen) {
261             ds_put_format(&ds, "Cached: ");
262         } else {
263             ds_put_format(&ds, "User: ");
264         }
265         ds_put_format(&ds, IP_FMT"/%"PRIu16" dev %s",
266                       IP_ARGS(rt->nw_addr), rt->plen,
267                       rt->output_bridge);
268         if (rt->gw) {
269             ds_put_format(&ds, " GW "IP_FMT, IP_ARGS(rt->gw));
270         }
271         ds_put_format(&ds, "\n");
272     }
273     unixctl_command_reply(conn, ds_cstr(&ds));
274     ds_destroy(&ds);
275 }
276
277 static void
278 ovs_router_lookup_cmd(struct unixctl_conn *conn, int argc OVS_UNUSED,
279                       const char *argv[], void *aux OVS_UNUSED)
280 {
281     ovs_be32 ip;
282     unsigned int plen;
283
284     if (scan_ipv4_route(argv[1], &ip, &plen) && plen == 32) {
285         char iface[IFNAMSIZ];
286         ovs_be32 gw;
287
288         if (ovs_router_lookup(ip, iface, &gw)) {
289             struct ds ds = DS_EMPTY_INITIALIZER;
290
291             ds_put_format(&ds, "gateway " IP_FMT "\n", IP_ARGS(gw));
292             ds_put_format(&ds, "dev %s\n", iface);
293             unixctl_command_reply(conn, ds_cstr(&ds));
294             ds_destroy(&ds);
295         } else {
296             unixctl_command_reply(conn, "Not found");
297         }
298     } else {
299         unixctl_command_reply(conn, "Invalid parameters");
300     }
301 }
302
303 void
304 ovs_router_flush(void)
305 {
306     struct ovs_router_entry *rt;
307
308     ovs_mutex_lock(&mutex);
309     classifier_defer(&cls);
310     CLS_FOR_EACH(rt, cr, &cls) {
311         if (rt->priority == rt->plen) {
312             __rt_entry_delete(&rt->cr);
313         }
314     }
315     classifier_publish(&cls);
316     ovs_mutex_unlock(&mutex);
317     seq_change(tnl_conf_seq);
318 }
319
320 /* May not be called more than once. */
321 void
322 ovs_router_init(void)
323 {
324     classifier_init(&cls, NULL);
325     unixctl_command_register("ovs/route/add", "ipv4_addr/prefix_len out_br_name gw", 2, 3,
326                              ovs_router_add, NULL);
327     unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show, NULL);
328     unixctl_command_register("ovs/route/del", "ipv4_addr/prefix_len", 1, 1, ovs_router_del,
329                              NULL);
330     unixctl_command_register("ovs/route/lookup", "ipv4_addr", 1, 1,
331                              ovs_router_lookup_cmd, NULL);
332 }