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