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