]> git.proxmox.com Git - mirror_ovs.git/blob - lib/route-table.c
Don't shadow variables.
[mirror_ovs.git] / lib / route-table.c
1 /*
2 * Copyright (c) 2011, 2012, 2013, 2014, 2017 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
19 #include "route-table.h"
20
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <sys/socket.h>
26 #include <linux/rtnetlink.h>
27 #include <net/if.h>
28
29 #include "hash.h"
30 #include "netdev.h"
31 #include "netlink.h"
32 #include "netlink-notifier.h"
33 #include "netlink-socket.h"
34 #include "openvswitch/ofpbuf.h"
35 #include "ovs-router.h"
36 #include "packets.h"
37 #include "rtnetlink.h"
38 #include "tnl-ports.h"
39 #include "openvswitch/vlog.h"
40
41 /* Linux 2.6.36 added RTA_MARK, so define it just in case we're building with
42 * old headers. (We can't test for it with #ifdef because it's an enum.) */
43 #define RTA_MARK 16
44
45 VLOG_DEFINE_THIS_MODULE(route_table);
46
47 struct route_data {
48 /* Copied from struct rtmsg. */
49 unsigned char rtm_dst_len;
50
51 /* Extracted from Netlink attributes. */
52 struct in6_addr rta_dst; /* 0 if missing. */
53 struct in6_addr rta_gw;
54 char ifname[IFNAMSIZ]; /* Interface name. */
55 uint32_t mark;
56 };
57
58 /* A digested version of a route message sent down by the kernel to indicate
59 * that a route has changed. */
60 struct route_table_msg {
61 bool relevant; /* Should this message be processed? */
62 int nlmsg_type; /* e.g. RTM_NEWROUTE, RTM_DELROUTE. */
63 struct route_data rd; /* Data parsed from this message. */
64 };
65
66 static struct ovs_mutex route_table_mutex = OVS_MUTEX_INITIALIZER;
67 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
68
69 /* Global change number for route-table, which should be incremented
70 * every time route_table_reset() is called. */
71 static uint64_t rt_change_seq;
72
73 static struct nln *nln = NULL;
74 static struct route_table_msg rtmsg;
75 static struct nln_notifier *route_notifier = NULL;
76 static struct nln_notifier *route6_notifier = NULL;
77 static struct nln_notifier *name_notifier = NULL;
78
79 static bool route_table_valid = false;
80
81 static int route_table_reset(void);
82 static void route_table_handle_msg(const struct route_table_msg *);
83 static int route_table_parse(struct ofpbuf *, struct route_table_msg *);
84 static void route_table_change(const struct route_table_msg *, void *);
85 static void route_map_clear(void);
86
87 static void name_table_init(void);
88 static void name_table_change(const struct rtnetlink_change *, void *);
89
90 uint64_t
91 route_table_get_change_seq(void)
92 {
93 return rt_change_seq;
94 }
95
96 /* Users of the route_table module should register themselves with this
97 * function before making any other route_table function calls. */
98 void
99 route_table_init(void)
100 OVS_EXCLUDED(route_table_mutex)
101 {
102 ovs_mutex_lock(&route_table_mutex);
103 ovs_assert(!nln);
104 ovs_assert(!route_notifier);
105 ovs_assert(!route6_notifier);
106
107 ovs_router_init();
108 nln = nln_create(NETLINK_ROUTE, (nln_parse_func *) route_table_parse,
109 &rtmsg);
110
111 route_notifier =
112 nln_notifier_create(nln, RTNLGRP_IPV4_ROUTE,
113 (nln_notify_func *) route_table_change, NULL);
114 route6_notifier =
115 nln_notifier_create(nln, RTNLGRP_IPV6_ROUTE,
116 (nln_notify_func *) route_table_change, NULL);
117
118 route_table_reset();
119 name_table_init();
120
121 ovs_mutex_unlock(&route_table_mutex);
122 }
123
124 /* Run periodically to update the locally maintained routing table. */
125 void
126 route_table_run(void)
127 OVS_EXCLUDED(route_table_mutex)
128 {
129 ovs_mutex_lock(&route_table_mutex);
130 if (nln) {
131 rtnetlink_run();
132 nln_run(nln);
133
134 if (!route_table_valid) {
135 route_table_reset();
136 }
137 }
138 ovs_mutex_unlock(&route_table_mutex);
139 }
140
141 /* Causes poll_block() to wake up when route_table updates are required. */
142 void
143 route_table_wait(void)
144 OVS_EXCLUDED(route_table_mutex)
145 {
146 ovs_mutex_lock(&route_table_mutex);
147 if (nln) {
148 rtnetlink_wait();
149 nln_wait(nln);
150 }
151 ovs_mutex_unlock(&route_table_mutex);
152 }
153
154 static int
155 route_table_reset(void)
156 {
157 struct nl_dump dump;
158 struct rtgenmsg *rtgenmsg;
159 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
160 struct ofpbuf request, reply, buf;
161
162 route_map_clear();
163 netdev_get_addrs_list_flush();
164 route_table_valid = true;
165 rt_change_seq++;
166
167 ofpbuf_init(&request, 0);
168
169 nl_msg_put_nlmsghdr(&request, sizeof *rtgenmsg, RTM_GETROUTE,
170 NLM_F_REQUEST);
171
172 rtgenmsg = ofpbuf_put_zeros(&request, sizeof *rtgenmsg);
173 rtgenmsg->rtgen_family = AF_UNSPEC;
174
175 nl_dump_start(&dump, NETLINK_ROUTE, &request);
176 ofpbuf_uninit(&request);
177
178 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
179 while (nl_dump_next(&dump, &reply, &buf)) {
180 struct route_table_msg msg;
181
182 if (route_table_parse(&reply, &msg)) {
183 route_table_handle_msg(&msg);
184 }
185 }
186 ofpbuf_uninit(&buf);
187
188 return nl_dump_done(&dump);
189 }
190
191 /* Return RTNLGRP_IPV4_ROUTE or RTNLGRP_IPV6_ROUTE on success, 0 on parse
192 * error. */
193 static int
194 route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
195 {
196 bool parsed, ipv4 = false;
197
198 static const struct nl_policy policy[] = {
199 [RTA_DST] = { .type = NL_A_U32, .optional = true },
200 [RTA_OIF] = { .type = NL_A_U32, .optional = true },
201 [RTA_GATEWAY] = { .type = NL_A_U32, .optional = true },
202 [RTA_MARK] = { .type = NL_A_U32, .optional = true },
203 };
204
205 static const struct nl_policy policy6[] = {
206 [RTA_DST] = { .type = NL_A_IPV6, .optional = true },
207 [RTA_OIF] = { .type = NL_A_U32, .optional = true },
208 [RTA_MARK] = { .type = NL_A_U32, .optional = true },
209 [RTA_GATEWAY] = { .type = NL_A_IPV6, .optional = true },
210 };
211
212 struct nlattr *attrs[ARRAY_SIZE(policy)];
213 const struct rtmsg *rtm;
214
215 rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
216
217 if (rtm->rtm_family == AF_INET) {
218 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
219 policy, attrs, ARRAY_SIZE(policy));
220 ipv4 = true;
221 } else if (rtm->rtm_family == AF_INET6) {
222 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
223 policy6, attrs, ARRAY_SIZE(policy6));
224 } else {
225 VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
226 return 0;
227 }
228
229 if (parsed) {
230 const struct nlmsghdr *nlmsg;
231 int rta_oif; /* Output interface index. */
232
233 nlmsg = buf->data;
234
235 memset(change, 0, sizeof *change);
236 change->relevant = true;
237
238 if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
239 change->relevant = false;
240 }
241
242 if (rtm->rtm_type != RTN_UNICAST &&
243 rtm->rtm_type != RTN_LOCAL) {
244 change->relevant = false;
245 }
246 change->nlmsg_type = nlmsg->nlmsg_type;
247 change->rd.rtm_dst_len = rtm->rtm_dst_len + (ipv4 ? 96 : 0);
248 if (attrs[RTA_OIF]) {
249 rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
250
251 if (!if_indextoname(rta_oif, change->rd.ifname)) {
252 int error = errno;
253
254 VLOG_DBG_RL(&rl, "Could not find interface name[%u]: %s",
255 rta_oif, ovs_strerror(error));
256 if (error == ENXIO) {
257 change->relevant = false;
258 } else {
259 return 0;
260 }
261 }
262 }
263
264 if (attrs[RTA_DST]) {
265 if (ipv4) {
266 ovs_be32 dst;
267 dst = nl_attr_get_be32(attrs[RTA_DST]);
268 in6_addr_set_mapped_ipv4(&change->rd.rta_dst, dst);
269 } else {
270 change->rd.rta_dst = nl_attr_get_in6_addr(attrs[RTA_DST]);
271 }
272 } else if (ipv4) {
273 in6_addr_set_mapped_ipv4(&change->rd.rta_dst, 0);
274 }
275 if (attrs[RTA_GATEWAY]) {
276 if (ipv4) {
277 ovs_be32 gw;
278 gw = nl_attr_get_be32(attrs[RTA_GATEWAY]);
279 in6_addr_set_mapped_ipv4(&change->rd.rta_gw, gw);
280 } else {
281 change->rd.rta_gw = nl_attr_get_in6_addr(attrs[RTA_GATEWAY]);
282 }
283 }
284 if (attrs[RTA_MARK]) {
285 change->rd.mark = nl_attr_get_u32(attrs[RTA_MARK]);
286 }
287 } else {
288 VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
289 return 0;
290 }
291
292 /* Success. */
293 return ipv4 ? RTNLGRP_IPV4_ROUTE : RTNLGRP_IPV6_ROUTE;
294 }
295
296 static void
297 route_table_change(const struct route_table_msg *change OVS_UNUSED,
298 void *aux OVS_UNUSED)
299 {
300 route_table_valid = false;
301 }
302
303 static void
304 route_table_handle_msg(const struct route_table_msg *change)
305 {
306 if (change->relevant && change->nlmsg_type == RTM_NEWROUTE) {
307 const struct route_data *rd = &change->rd;
308
309 ovs_router_insert(rd->mark, &rd->rta_dst, rd->rtm_dst_len,
310 rd->ifname, &rd->rta_gw);
311 }
312 }
313
314 static void
315 route_map_clear(void)
316 {
317 ovs_router_flush();
318 }
319
320 bool
321 route_table_fallback_lookup(const struct in6_addr *ip6_dst OVS_UNUSED,
322 char name[] OVS_UNUSED,
323 struct in6_addr *gw6)
324 {
325 *gw6 = in6addr_any;
326 return false;
327 }
328
329 \f
330 /* name_table . */
331
332 static void
333 name_table_init(void)
334 {
335 name_notifier = rtnetlink_notifier_create(name_table_change, NULL);
336 }
337
338
339 static void
340 name_table_change(const struct rtnetlink_change *change,
341 void *aux OVS_UNUSED)
342 {
343 /* Changes to interface status can cause routing table changes that some
344 * versions of the linux kernel do not advertise for some reason. */
345 route_table_valid = false;
346
347 if (change && change->nlmsg_type == RTM_DELLINK) {
348 if (change->ifname) {
349 tnl_port_map_delete_ipdev(change->ifname);
350 }
351 }
352 }