netdev-vport: Checks tunnel status change when route-table is reset.
[sliver-openvswitch.git] / lib / route-table.c
1 /*
2  * Copyright (c) 2011, 2012, 2013, 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
19 #include "route-table.h"
20
21 #include <arpa/inet.h>
22 #include <sys/socket.h>
23 #include <linux/rtnetlink.h>
24 #include <net/if.h>
25
26 #include "hash.h"
27 #include "hmap.h"
28 #include "netlink.h"
29 #include "netlink-notifier.h"
30 #include "netlink-socket.h"
31 #include "ofpbuf.h"
32 #include "rtnetlink-link.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(route_table);
36
37 struct route_data {
38     /* Copied from struct rtmsg. */
39     unsigned char rtm_dst_len;
40
41     /* Extracted from Netlink attributes. */
42     uint32_t rta_dst; /* Destination in host byte order. 0 if missing. */
43     int rta_oif;      /* Output interface index. */
44 };
45
46 /* A digested version of a route message sent down by the kernel to indicate
47  * that a route has changed. */
48 struct route_table_msg {
49     bool relevant;        /* Should this message be processed? */
50     int nlmsg_type;       /* e.g. RTM_NEWROUTE, RTM_DELROUTE. */
51     struct route_data rd; /* Data parsed from this message. */
52 };
53
54 struct route_node {
55     struct hmap_node node; /* Node in route_map. */
56     struct route_data rd;  /* Data associated with this node. */
57 };
58
59 struct name_node {
60     struct hmap_node node; /* Node in name_map. */
61     uint32_t ifi_index;    /* Kernel interface index. */
62
63     char ifname[IFNAMSIZ]; /* Interface name. */
64 };
65
66 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
67
68 /* Global change number for route-table, which should be incremented
69  * every time route_table_reset() is called.  */
70 static uint64_t rt_change_seq;
71
72 static unsigned int register_count = 0;
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 *name_notifier = NULL;
77
78 static bool route_table_valid = false;
79 static bool name_table_valid = false;
80 static struct hmap route_map;
81 static struct hmap name_map;
82
83 static int route_table_reset(void);
84 static void route_table_handle_msg(const struct route_table_msg *);
85 static bool route_table_parse(struct ofpbuf *, struct route_table_msg *);
86 static void route_table_change(const struct route_table_msg *, void *);
87 static struct route_node *route_node_lookup(const struct route_data *);
88 static struct route_node *route_node_lookup_by_ip(uint32_t ip);
89 static void route_map_clear(void);
90 static uint32_t hash_route_data(const struct route_data *);
91
92 static void name_table_init(void);
93 static void name_table_uninit(void);
94 static int name_table_reset(void);
95 static void name_table_change(const struct rtnetlink_link_change *, void *);
96 static void name_map_clear(void);
97 static struct name_node *name_node_lookup(int ifi_index);
98
99 /* Populates 'name' with the name of the interface traffic destined for 'ip'
100  * is likely to egress out of (see route_table_get_ifindex).
101  *
102  * Returns true if successful, otherwise false. */
103 bool
104 route_table_get_name(ovs_be32 ip, char name[IFNAMSIZ])
105 {
106     int ifindex;
107
108     if (!name_table_valid) {
109         name_table_reset();
110     }
111
112     if (route_table_get_ifindex(ip, &ifindex)) {
113         struct name_node *nn;
114
115         nn = name_node_lookup(ifindex);
116         if (nn) {
117             ovs_strlcpy(name, nn->ifname, IFNAMSIZ);
118             return true;
119         }
120     }
121
122     return false;
123 }
124
125 /* Populates 'ifindex' with the interface index traffic destined for 'ip' is
126  * likely to egress.  There is no hard guarantee that traffic destined for 'ip'
127  * will egress out the specified interface.  'ifindex' may refer to an
128  * interface which is not physical (such as a bridge port).
129  *
130  * Returns true if successful, otherwise false. */
131 bool
132 route_table_get_ifindex(ovs_be32 ip_, int *ifindex)
133 {
134     struct route_node *rn;
135     uint32_t ip = ntohl(ip_);
136
137     *ifindex = 0;
138
139     if (!route_table_valid) {
140         route_table_reset();
141     }
142
143     rn = route_node_lookup_by_ip(ip);
144
145     if (rn) {
146         *ifindex = rn->rd.rta_oif;
147         return true;
148     }
149
150     /* Choose a default route. */
151     HMAP_FOR_EACH(rn, node, &route_map) {
152         if (rn->rd.rta_dst == 0 && rn->rd.rtm_dst_len == 0) {
153             *ifindex = rn->rd.rta_oif;
154             return true;
155         }
156     }
157
158     return false;
159 }
160
161 uint64_t
162 route_table_get_change_seq(void)
163 {
164     return rt_change_seq;
165 }
166
167 /* Users of the route_table module should register themselves with this
168  * function before making any other route_table function calls. */
169 void
170 route_table_register(void)
171 {
172     if (!register_count) {
173         ovs_assert(!nln);
174         ovs_assert(!route_notifier);
175
176         nln = nln_create(NETLINK_ROUTE, RTNLGRP_IPV4_ROUTE,
177                          (nln_parse_func *) route_table_parse, &rtmsg);
178
179         route_notifier =
180             nln_notifier_create(nln, (nln_notify_func *) route_table_change,
181                                 NULL);
182
183         hmap_init(&route_map);
184         route_table_reset();
185         name_table_init();
186     }
187
188     register_count++;
189 }
190
191 /* Users of the route_table module should unregister themselves with this
192  * function when they will no longer be making any more route_table fuction
193  * calls. */
194 void
195 route_table_unregister(void)
196 {
197     register_count--;
198
199     if (!register_count) {
200         nln_notifier_destroy(route_notifier);
201         route_notifier = NULL;
202         nln_destroy(nln);
203         nln = NULL;
204
205         route_map_clear();
206         hmap_destroy(&route_map);
207         name_table_uninit();
208     }
209 }
210
211 /* Run periodically to update the locally maintained routing table. */
212 void
213 route_table_run(void)
214 {
215     if (nln) {
216         rtnetlink_link_run();
217         nln_run(nln);
218
219         if (!route_table_valid) {
220             route_table_reset();
221         }
222     }
223 }
224
225 /* Causes poll_block() to wake up when route_table updates are required. */
226 void
227 route_table_wait(void)
228 {
229     if (nln) {
230         rtnetlink_link_wait();
231         nln_wait(nln);
232     }
233 }
234
235 static int
236 route_table_reset(void)
237 {
238     struct nl_dump dump;
239     struct rtgenmsg *rtmsg;
240     uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
241     struct ofpbuf request, reply, buf;
242
243     route_map_clear();
244     route_table_valid = true;
245     rt_change_seq++;
246
247     ofpbuf_init(&request, 0);
248
249     nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
250
251     rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
252     rtmsg->rtgen_family = AF_INET;
253
254     nl_dump_start(&dump, NETLINK_ROUTE, &request);
255     ofpbuf_uninit(&request);
256
257     ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
258     while (nl_dump_next(&dump, &reply, &buf)) {
259         struct route_table_msg msg;
260
261         if (route_table_parse(&reply, &msg)) {
262             route_table_handle_msg(&msg);
263         }
264     }
265     ofpbuf_uninit(&buf);
266
267     return nl_dump_done(&dump);
268 }
269
270
271 static bool
272 route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
273 {
274     bool parsed;
275
276     static const struct nl_policy policy[] = {
277         [RTA_DST] = { .type = NL_A_U32, .optional = true  },
278         [RTA_OIF] = { .type = NL_A_U32, .optional = false },
279     };
280
281     struct nlattr *attrs[ARRAY_SIZE(policy)];
282
283     parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
284                              policy, attrs, ARRAY_SIZE(policy));
285
286     if (parsed) {
287         const struct rtmsg *rtm;
288         const struct nlmsghdr *nlmsg;
289
290         nlmsg = ofpbuf_data(buf);
291         rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
292
293         if (rtm->rtm_family != AF_INET) {
294             VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
295             return false;
296         }
297
298         memset(change, 0, sizeof *change);
299         change->relevant = true;
300
301         if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
302             change->relevant = false;
303         }
304
305         if (rtm->rtm_type != RTN_UNICAST &&
306             rtm->rtm_type != RTN_LOCAL) {
307             change->relevant = false;
308         }
309
310         change->nlmsg_type     = nlmsg->nlmsg_type;
311         change->rd.rtm_dst_len = rtm->rtm_dst_len;
312         change->rd.rta_oif     = nl_attr_get_u32(attrs[RTA_OIF]);
313
314         if (attrs[RTA_DST]) {
315             change->rd.rta_dst = ntohl(nl_attr_get_be32(attrs[RTA_DST]));
316         }
317
318     } else {
319         VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
320     }
321
322     return parsed;
323 }
324
325 static void
326 route_table_change(const struct route_table_msg *change OVS_UNUSED,
327                    void *aux OVS_UNUSED)
328 {
329     route_table_valid = false;
330 }
331
332 static void
333 route_table_handle_msg(const struct route_table_msg *change)
334 {
335     if (change->relevant && change->nlmsg_type == RTM_NEWROUTE &&
336         !route_node_lookup(&change->rd)) {
337         struct route_node *rn;
338
339         rn = xzalloc(sizeof *rn);
340         memcpy(&rn->rd, &change->rd, sizeof change->rd);
341
342         hmap_insert(&route_map, &rn->node, hash_route_data(&rn->rd));
343     }
344 }
345
346 static struct route_node *
347 route_node_lookup(const struct route_data *rd)
348 {
349     struct route_node *rn;
350
351     HMAP_FOR_EACH_WITH_HASH(rn, node, hash_route_data(rd), &route_map) {
352         if (!memcmp(&rn->rd, rd, sizeof *rd)) {
353             return rn;
354         }
355     }
356
357     return NULL;
358 }
359
360 static struct route_node *
361 route_node_lookup_by_ip(uint32_t ip)
362 {
363     int dst_len;
364     struct route_node *rn, *rn_ret;
365
366     dst_len = -1;
367     rn_ret  = NULL;
368
369     HMAP_FOR_EACH(rn, node, &route_map) {
370         uint32_t mask = 0xffffffff << (32 - rn->rd.rtm_dst_len);
371
372         if (rn->rd.rta_dst == 0 && rn->rd.rtm_dst_len == 0) {
373             /* Default route. */
374             continue;
375         }
376
377         if (rn->rd.rtm_dst_len > dst_len &&
378             (ip & mask) == (rn->rd.rta_dst & mask)) {
379             rn_ret  = rn;
380             dst_len = rn->rd.rtm_dst_len;
381         }
382     }
383
384     return rn_ret;
385 }
386
387 static void
388 route_map_clear(void)
389 {
390     struct route_node *rn, *rn_next;
391
392     HMAP_FOR_EACH_SAFE(rn, rn_next, node, &route_map) {
393         hmap_remove(&route_map, &rn->node);
394         free(rn);
395     }
396 }
397
398 static uint32_t
399 hash_route_data(const struct route_data *rd)
400 {
401     return hash_bytes(rd, sizeof *rd, 0);
402 }
403 \f
404 /* name_table . */
405
406 static void
407 name_table_init(void)
408 {
409     hmap_init(&name_map);
410     name_notifier = rtnetlink_link_notifier_create(name_table_change, NULL);
411     name_table_valid = false;
412 }
413
414 static void
415 name_table_uninit(void)
416 {
417     rtnetlink_link_notifier_destroy(name_notifier);
418     name_notifier = NULL;
419     name_map_clear();
420     hmap_destroy(&name_map);
421 }
422
423 static int
424 name_table_reset(void)
425 {
426     struct nl_dump dump;
427     struct rtgenmsg *rtmsg;
428     uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
429     struct ofpbuf request, reply, buf;
430
431     name_table_valid = true;
432     name_map_clear();
433
434     ofpbuf_init(&request, 0);
435     nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETLINK, NLM_F_REQUEST);
436     rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
437     rtmsg->rtgen_family = AF_INET;
438
439     nl_dump_start(&dump, NETLINK_ROUTE, &request);
440     ofpbuf_uninit(&request);
441
442     ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
443     while (nl_dump_next(&dump, &reply, &buf)) {
444         struct rtnetlink_link_change change;
445
446         if (rtnetlink_link_parse(&reply, &change)
447             && change.nlmsg_type == RTM_NEWLINK
448             && !name_node_lookup(change.ifi_index)) {
449             struct name_node *nn;
450
451             nn = xzalloc(sizeof *nn);
452             nn->ifi_index = change.ifi_index;
453             ovs_strlcpy(nn->ifname, change.ifname, IFNAMSIZ);
454             hmap_insert(&name_map, &nn->node, hash_int(nn->ifi_index, 0));
455         }
456     }
457     ofpbuf_uninit(&buf);
458     return nl_dump_done(&dump);
459 }
460
461 static void
462 name_table_change(const struct rtnetlink_link_change *change OVS_UNUSED,
463                   void *aux OVS_UNUSED)
464 {
465     /* Changes to interface status can cause routing table changes that some
466      * versions of the linux kernel do not advertise for some reason. */
467     route_table_valid = false;
468     name_table_valid = false;
469 }
470
471 static struct name_node *
472 name_node_lookup(int ifi_index)
473 {
474     struct name_node *nn;
475
476     HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifi_index, 0), &name_map) {
477         if (nn->ifi_index == ifi_index) {
478             return nn;
479         }
480     }
481
482     return NULL;
483 }
484
485 static void
486 name_map_clear(void)
487 {
488     struct name_node *nn, *nn_next;
489
490     HMAP_FOR_EACH_SAFE(nn, nn_next, node, &name_map) {
491         hmap_remove(&name_map, &nn->node);
492         free(nn);
493     }
494 }