]> git.proxmox.com Git - ovs.git/blame - lib/route-table.c
lockfile: Support \-delimited file names in lockfile_name().
[ovs.git] / lib / route-table.c
CommitLineData
a132aa96 1/*
41ca1e0a 2 * Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc.
a132aa96
EJ
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
19#include "route-table.h"
20
58718f40 21#include <errno.h>
a132aa96
EJ
22#include <arpa/inet.h>
23#include <sys/socket.h>
24#include <linux/rtnetlink.h>
25#include <net/if.h>
26
27#include "hash.h"
a132aa96 28#include "netlink.h"
45c8d3a1 29#include "netlink-notifier.h"
a132aa96
EJ
30#include "netlink-socket.h"
31#include "ofpbuf.h"
d9b4ebc5 32#include "ovs-router.h"
b46ccdf5 33#include "rtnetlink-link.h"
e6211adc 34#include "openvswitch/vlog.h"
a132aa96
EJ
35
36VLOG_DEFINE_THIS_MODULE(route_table);
37
38struct route_data {
39 /* Copied from struct rtmsg. */
40 unsigned char rtm_dst_len;
41
42 /* Extracted from Netlink attributes. */
d9b4ebc5 43 ovs_be32 rta_dst; /* 0 if missing. */
e0cc58f9 44 ovs_be32 rta_gw;
58718f40 45 char ifname[IFNAMSIZ]; /* Interface name. */
a132aa96
EJ
46};
47
48/* A digested version of a route message sent down by the kernel to indicate
49 * that a route has changed. */
50struct route_table_msg {
db2dede4 51 bool relevant; /* Should this message be processed? */
a132aa96
EJ
52 int nlmsg_type; /* e.g. RTM_NEWROUTE, RTM_DELROUTE. */
53 struct route_data rd; /* Data parsed from this message. */
54};
55
3c27dbe6 56static struct ovs_mutex route_table_mutex = OVS_MUTEX_INITIALIZER;
a132aa96
EJ
57static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
58
41ca1e0a
AW
59/* Global change number for route-table, which should be incremented
60 * every time route_table_reset() is called. */
61static uint64_t rt_change_seq;
62
0a811051 63static struct nln *nln = NULL;
a132aa96 64static struct route_table_msg rtmsg;
2ee6545f
EJ
65static struct nln_notifier *route_notifier = NULL;
66static struct nln_notifier *name_notifier = NULL;
f0e167f0
EJ
67
68static bool route_table_valid = false;
a132aa96
EJ
69
70static int route_table_reset(void);
f0e167f0 71static void route_table_handle_msg(const struct route_table_msg *);
a132aa96
EJ
72static bool route_table_parse(struct ofpbuf *, struct route_table_msg *);
73static void route_table_change(const struct route_table_msg *, void *);
a132aa96 74static void route_map_clear(void);
a132aa96 75
b46ccdf5 76static void name_table_init(void);
b46ccdf5 77static void name_table_change(const struct rtnetlink_link_change *, void *);
b46ccdf5 78
41ca1e0a
AW
79uint64_t
80route_table_get_change_seq(void)
81{
82 return rt_change_seq;
83}
84
a132aa96
EJ
85/* Users of the route_table module should register themselves with this
86 * function before making any other route_table function calls. */
87void
b772066f 88route_table_init(void)
3c27dbe6 89 OVS_EXCLUDED(route_table_mutex)
a132aa96 90{
3c27dbe6 91 ovs_mutex_lock(&route_table_mutex);
b772066f
PS
92 ovs_assert(!nln);
93 ovs_assert(!route_notifier);
a132aa96 94
b772066f
PS
95 ovs_router_init();
96 nln = nln_create(NETLINK_ROUTE, RTNLGRP_IPV4_ROUTE,
97 (nln_parse_func *) route_table_parse, &rtmsg);
2ee6545f 98
b772066f
PS
99 route_notifier =
100 nln_notifier_create(nln, (nln_notify_func *) route_table_change,
101 NULL);
a132aa96 102
b772066f
PS
103 route_table_reset();
104 name_table_init();
a132aa96 105
3c27dbe6 106 ovs_mutex_unlock(&route_table_mutex);
a132aa96
EJ
107}
108
109/* Run periodically to update the locally maintained routing table. */
110void
111route_table_run(void)
3c27dbe6 112 OVS_EXCLUDED(route_table_mutex)
a132aa96 113{
3c27dbe6 114 ovs_mutex_lock(&route_table_mutex);
0a811051 115 if (nln) {
18a23781
EJ
116 rtnetlink_link_run();
117 nln_run(nln);
41ca1e0a
AW
118
119 if (!route_table_valid) {
120 route_table_reset();
121 }
a132aa96 122 }
3c27dbe6 123 ovs_mutex_unlock(&route_table_mutex);
a132aa96
EJ
124}
125
126/* Causes poll_block() to wake up when route_table updates are required. */
127void
128route_table_wait(void)
3c27dbe6 129 OVS_EXCLUDED(route_table_mutex)
a132aa96 130{
3c27dbe6 131 ovs_mutex_lock(&route_table_mutex);
0a811051 132 if (nln) {
18a23781
EJ
133 rtnetlink_link_wait();
134 nln_wait(nln);
a132aa96 135 }
3c27dbe6 136 ovs_mutex_unlock(&route_table_mutex);
a132aa96
EJ
137}
138
139static int
140route_table_reset(void)
141{
a132aa96
EJ
142 struct nl_dump dump;
143 struct rtgenmsg *rtmsg;
d57695d7
JS
144 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
145 struct ofpbuf request, reply, buf;
a132aa96
EJ
146
147 route_map_clear();
f0e167f0 148 route_table_valid = true;
41ca1e0a 149 rt_change_seq++;
a132aa96 150
a132aa96
EJ
151 ofpbuf_init(&request, 0);
152
153 nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
154
155 rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
156 rtmsg->rtgen_family = AF_INET;
157
a88b4e04 158 nl_dump_start(&dump, NETLINK_ROUTE, &request);
896b3272 159 ofpbuf_uninit(&request);
a132aa96 160
d57695d7
JS
161 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
162 while (nl_dump_next(&dump, &reply, &buf)) {
a132aa96
EJ
163 struct route_table_msg msg;
164
165 if (route_table_parse(&reply, &msg)) {
f0e167f0 166 route_table_handle_msg(&msg);
a132aa96
EJ
167 }
168 }
d57695d7 169 ofpbuf_uninit(&buf);
a132aa96 170
a88b4e04 171 return nl_dump_done(&dump);
a132aa96
EJ
172}
173
174
175static bool
176route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
177{
178 bool parsed;
179
180 static const struct nl_policy policy[] = {
181 [RTA_DST] = { .type = NL_A_U32, .optional = true },
182 [RTA_OIF] = { .type = NL_A_U32, .optional = false },
e0cc58f9 183 [RTA_GATEWAY] = { .type = NL_A_U32, .optional = true },
a132aa96
EJ
184 };
185
37cd552e 186 struct nlattr *attrs[ARRAY_SIZE(policy)];
a132aa96
EJ
187
188 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
189 policy, attrs, ARRAY_SIZE(policy));
190
191 if (parsed) {
192 const struct rtmsg *rtm;
193 const struct nlmsghdr *nlmsg;
58718f40 194 int rta_oif; /* Output interface index. */
a132aa96 195
6fd6ed71 196 nlmsg = buf->data;
db5a1019 197 rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
a132aa96
EJ
198
199 if (rtm->rtm_family != AF_INET) {
200 VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
201 return false;
202 }
203
204 memset(change, 0, sizeof *change);
db2dede4
EJ
205 change->relevant = true;
206
207 if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
208 change->relevant = false;
209 }
210
211 if (rtm->rtm_type != RTN_UNICAST &&
212 rtm->rtm_type != RTN_LOCAL) {
213 change->relevant = false;
214 }
a132aa96
EJ
215 change->nlmsg_type = nlmsg->nlmsg_type;
216 change->rd.rtm_dst_len = rtm->rtm_dst_len;
58718f40
PS
217 rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
218
219 if (!if_indextoname(rta_oif, change->rd.ifname)) {
220 int error = errno;
221
222 VLOG_DBG_RL(&rl, "Could not find interface name[%u]: %s",
223 rta_oif, ovs_strerror(error));
224 return false;
225 }
a132aa96
EJ
226
227 if (attrs[RTA_DST]) {
d9b4ebc5 228 change->rd.rta_dst = nl_attr_get_be32(attrs[RTA_DST]);
a132aa96 229 }
e0cc58f9
PS
230 if (attrs[RTA_GATEWAY]) {
231 change->rd.rta_gw = nl_attr_get_be32(attrs[RTA_GATEWAY]);
232 }
233
a132aa96
EJ
234
235 } else {
236 VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
237 }
238
239 return parsed;
240}
241
242static void
f0e167f0
EJ
243route_table_change(const struct route_table_msg *change OVS_UNUSED,
244 void *aux OVS_UNUSED)
a132aa96 245{
f0e167f0
EJ
246 route_table_valid = false;
247}
248
249static void
250route_table_handle_msg(const struct route_table_msg *change)
251{
d9b4ebc5
PS
252 if (change->relevant && change->nlmsg_type == RTM_NEWROUTE) {
253 const struct route_data *rd = &change->rd;
a132aa96 254
e0cc58f9
PS
255 ovs_router_insert(rd->rta_dst, rd->rtm_dst_len,
256 rd->ifname, rd->rta_gw);
a132aa96
EJ
257 }
258}
259
a132aa96
EJ
260static void
261route_map_clear(void)
262{
d9b4ebc5 263 ovs_router_flush();
a132aa96
EJ
264}
265
88ffdc93
YT
266bool
267route_table_fallback_lookup(ovs_be32 ip_dst OVS_UNUSED,
268 char output_bridge[] OVS_UNUSED,
269 ovs_be32 *gw)
270{
271 *gw = 0;
272 return false;
273}
274
b46ccdf5
EJ
275\f
276/* name_table . */
277
278static void
279name_table_init(void)
280{
2ee6545f 281 name_notifier = rtnetlink_link_notifier_create(name_table_change, NULL);
b46ccdf5
EJ
282}
283
b46ccdf5
EJ
284
285static void
286name_table_change(const struct rtnetlink_link_change *change OVS_UNUSED,
287 void *aux OVS_UNUSED)
288{
289 /* Changes to interface status can cause routing table changes that some
290 * versions of the linux kernel do not advertise for some reason. */
291 route_table_valid = false;
b46ccdf5 292}