]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/ovs-router.c
2 * Copyright (c) 2014, 2015 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
21 #include <sys/socket.h>
23 #include <netinet/in.h>
29 #include "classifier.h"
30 #include "command-line.h"
33 #include "dynamic-string.h"
37 #include "ovs-router.h"
38 #include "ovs-thread.h"
39 #include "route-table.h"
43 static struct ovs_mutex mutex
= OVS_MUTEX_INITIALIZER
;
44 static struct classifier cls
;
46 struct ovs_router_entry
{
48 char output_bridge
[IFNAMSIZ
];
55 static struct ovs_router_entry
*
56 ovs_router_entry_cast(const struct cls_rule
*cr
)
58 if (offsetof(struct ovs_router_entry
, cr
) == 0) {
59 return CONTAINER_OF(cr
, struct ovs_router_entry
, cr
);
61 return cr
? CONTAINER_OF(cr
, struct ovs_router_entry
, cr
) : NULL
;
66 ovs_router_lookup(ovs_be32 ip_dst
, char output_bridge
[], ovs_be32
*gw
)
68 const struct cls_rule
*cr
;
69 struct flow flow
= {.nw_dst
= ip_dst
};
71 cr
= classifier_lookup(&cls
, &flow
, NULL
);
73 struct ovs_router_entry
*p
= ovs_router_entry_cast(cr
);
75 strncpy(output_bridge
, p
->output_bridge
, IFNAMSIZ
);
79 return route_table_fallback_lookup(ip_dst
, output_bridge
, gw
);
83 rt_entry_free(struct ovs_router_entry
*p
)
85 cls_rule_destroy(&p
->cr
);
89 static void rt_init_match(struct match
*match
, ovs_be32 ip_dst
, uint8_t plen
)
93 mask
= be32_prefix_mask(plen
);
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
;
102 ovs_router_insert__(uint8_t priority
, ovs_be32 ip_dst
, uint8_t plen
,
103 const char output_bridge
[],
106 const struct cls_rule
*cr
;
107 struct ovs_router_entry
*p
;
110 rt_init_match(&match
, ip_dst
, plen
);
112 p
= xzalloc(sizeof *p
);
113 strncpy(p
->output_bridge
, output_bridge
, IFNAMSIZ
);
115 p
->nw_addr
= match
.flow
.nw_dst
;
117 p
->priority
= priority
;
118 cls_rule_init(&p
->cr
, &match
, priority
); /* Longest prefix matches first. */
120 ovs_mutex_lock(&mutex
);
121 cr
= classifier_replace(&cls
, &p
->cr
, NULL
, 0);
122 ovs_mutex_unlock(&mutex
);
125 /* An old rule with the same match was displaced. */
126 ovsrcu_postpone(rt_entry_free
, ovs_router_entry_cast(cr
));
128 seq_change(tnl_conf_seq
);
132 ovs_router_insert(ovs_be32 ip_dst
, uint8_t plen
, const char output_bridge
[],
135 ovs_router_insert__(plen
, ip_dst
, plen
, output_bridge
, gw
);
139 rt_entry_delete(uint8_t priority
, ovs_be32 ip_dst
, uint8_t plen
)
141 const struct cls_rule
*cr
;
142 struct cls_rule rule
;
145 rt_init_match(&match
, ip_dst
, plen
);
147 cls_rule_init(&rule
, &match
, priority
);
149 /* Find the exact rule. */
150 cr
= classifier_find_rule_exactly(&cls
, &rule
);
153 ovs_mutex_lock(&mutex
);
154 cr
= classifier_remove(&cls
, cr
);
155 ovs_mutex_unlock(&mutex
);
158 ovsrcu_postpone(rt_entry_free
, ovs_router_entry_cast(cr
));
166 scan_ipv4_route(const char *s
, ovs_be32
*addr
, unsigned int *plen
)
168 int len
, max_plen
, n
;
169 int slen
= strlen(s
);
170 uint8_t *ip
= (uint8_t *)addr
;
173 if (!ovs_scan(s
, "%"SCNu8
"%n", &ip
[0], &n
)) {
178 for (int i
= 1; i
< 4; i
++) {
179 if (ovs_scan(s
+ len
, ".%"SCNu8
"%n", &ip
[i
], &n
)) {
186 if (len
== slen
&& max_plen
== 32) {
190 if (ovs_scan(s
+ len
, "/%u%n", plen
, &n
)
191 && len
+ n
== slen
&& *plen
<= max_plen
) {
198 ovs_router_add(struct unixctl_conn
*conn
, int argc
,
199 const char *argv
[], void *aux OVS_UNUSED
)
204 if (scan_ipv4_route(argv
[1], &ip
, &plen
)) {
206 inet_pton(AF_INET
, argv
[3], (struct in_addr
*)&gw
);
210 ovs_router_insert__(plen
+ 32, ip
, plen
, argv
[2], gw
);
211 unixctl_command_reply(conn
, "OK");
213 unixctl_command_reply(conn
, "Invalid parameters");
218 ovs_router_del(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
219 const char *argv
[], void *aux OVS_UNUSED
)
224 if (scan_ipv4_route(argv
[1], &ip
, &plen
)) {
226 if (rt_entry_delete(plen
+ 32, ip
, plen
)) {
227 unixctl_command_reply(conn
, "OK");
228 seq_change(tnl_conf_seq
);
230 unixctl_command_reply(conn
, "Not found");
233 unixctl_command_reply(conn
, "Invalid parameters");
238 ovs_router_show(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
239 const char *argv
[] OVS_UNUSED
, void *aux OVS_UNUSED
)
241 struct ovs_router_entry
*rt
;
242 struct ds ds
= DS_EMPTY_INITIALIZER
;
244 ds_put_format(&ds
, "Route Table:\n");
245 CLS_FOR_EACH(rt
, cr
, &cls
) {
246 if (rt
->priority
== rt
->plen
) {
247 ds_put_format(&ds
, "Cached: ");
249 ds_put_format(&ds
, "User: ");
251 ds_put_format(&ds
, IP_FMT
"/%"PRIu16
" dev %s",
252 IP_ARGS(rt
->nw_addr
), rt
->plen
,
255 ds_put_format(&ds
, " GW "IP_FMT
, IP_ARGS(rt
->gw
));
257 ds_put_format(&ds
, "\n");
259 unixctl_command_reply(conn
, ds_cstr(&ds
));
264 ovs_router_lookup_cmd(struct unixctl_conn
*conn
, int argc OVS_UNUSED
,
265 const char *argv
[], void *aux OVS_UNUSED
)
270 if (scan_ipv4_route(argv
[1], &ip
, &plen
) && plen
== 32) {
271 char iface
[IFNAMSIZ
];
274 if (ovs_router_lookup(ip
, iface
, &gw
)) {
275 struct ds ds
= DS_EMPTY_INITIALIZER
;
277 ds_put_format(&ds
, "gateway " IP_FMT
"\n", IP_ARGS(gw
));
278 ds_put_format(&ds
, "dev %s\n", iface
);
279 unixctl_command_reply(conn
, ds_cstr(&ds
));
281 unixctl_command_reply(conn
, "Not found");
284 unixctl_command_reply(conn
, "Invalid parameters");
289 ovs_router_flush(void)
291 struct ovs_router_entry
*rt
;
293 ovs_mutex_lock(&mutex
);
294 classifier_defer(&cls
);
295 CLS_FOR_EACH(rt
, cr
, &cls
) {
296 if (rt
->priority
== rt
->plen
) {
297 if (classifier_remove(&cls
, &rt
->cr
)) {
298 ovsrcu_postpone(rt_entry_free
, rt
);
302 classifier_publish(&cls
);
303 ovs_mutex_unlock(&mutex
);
304 seq_change(tnl_conf_seq
);
307 /* May not be called more than once. */
309 ovs_router_init(void)
311 classifier_init(&cls
, NULL
);
312 unixctl_command_register("ovs/route/add", "ipv4_addr/prefix_len out_br_name gw", 2, 3,
313 ovs_router_add
, NULL
);
314 unixctl_command_register("ovs/route/show", "", 0, 0, ovs_router_show
, NULL
);
315 unixctl_command_register("ovs/route/del", "ipv4_addr/prefix_len", 1, 1, ovs_router_del
,
317 unixctl_command_register("ovs/route/lookup", "ipv4_addr", 1, 1,
318 ovs_router_lookup_cmd
, NULL
);