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