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