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