2 * Copyright (c) 2011 Nicira Networks.
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.
19 #include "route-table.h"
22 #include <arpa/inet.h>
23 #include <sys/socket.h>
24 #include <linux/rtnetlink.h>
30 #include "netlink-notifier.h"
31 #include "netlink-socket.h"
33 #include "rtnetlink-link.h"
36 VLOG_DEFINE_THIS_MODULE(route_table);
39 /* Copied from struct rtmsg. */
40 unsigned char rtm_dst_len;
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. */
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. */
56 struct hmap_node node; /* Node in route_map. */
57 struct route_data rd; /* Data associated with this node. */
61 struct hmap_node node; /* Node in name_map. */
62 uint32_t ifi_index; /* Kernel interface index. */
64 char ifname[IFNAMSIZ]; /* Interface name. */
67 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
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;
73 static struct nln_notifier name_notifier;
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;
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 *);
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);
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).
99 * Returns true if successful, otherwise false. */
101 route_table_get_name(ovs_be32 ip, char name[IFNAMSIZ])
105 if (!name_table_valid) {
109 if (route_table_get_ifindex(ip, &ifindex)) {
110 struct name_node *nn;
112 nn = name_node_lookup(ifindex);
114 ovs_strlcpy(name, nn->ifname, IFNAMSIZ);
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).
127 * Returns true if successful, otherwise false. */
129 route_table_get_ifindex(ovs_be32 ip_, int *ifindex)
131 struct route_node *rn;
132 uint32_t ip = ntohl(ip_);
136 if (!route_table_valid) {
140 rn = route_node_lookup_by_ip(ip);
143 *ifindex = rn->rd.rta_oif;
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;
158 /* Users of the route_table module should register themselves with this
159 * function before making any other route_table function calls. */
161 route_table_register(void)
163 if (!register_count) {
166 nln = nln_create(NETLINK_ROUTE, RTNLGRP_IPV4_ROUTE,
167 (nln_parse_func *) route_table_parse, &rtmsg);
168 nln_notifier_register(nln, &route_notifier,
169 (nln_notify_func *) route_table_change, NULL);
171 hmap_init(&route_map);
179 /* Users of the route_table module should unregister themselves with this
180 * function when they will no longer be making any more route_table fuction
183 route_table_unregister(void)
187 if (!register_count) {
192 hmap_destroy(&route_map);
197 /* Run periodically to update the locally maintained routing table. */
199 route_table_run(void)
202 rtnetlink_link_notifier_run();
203 nln_notifier_run(nln);
207 /* Causes poll_block() to wake up when route_table updates are required. */
209 route_table_wait(void)
212 rtnetlink_link_notifier_wait();
213 nln_notifier_wait(nln);
218 route_table_reset(void)
222 struct rtgenmsg *rtmsg;
223 struct ofpbuf request, reply;
224 static struct nl_sock *rtnl_sock;
227 route_table_valid = true;
229 error = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
231 VLOG_WARN_RL(&rl, "failed to reset routing table, "
232 "cannot create RTNETLINK_ROUTE socket");
236 ofpbuf_init(&request, 0);
238 nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
240 rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
241 rtmsg->rtgen_family = AF_INET;
243 nl_dump_start(&dump, rtnl_sock, &request);
245 while (nl_dump_next(&dump, &reply)) {
246 struct route_table_msg msg;
248 if (route_table_parse(&reply, &msg)) {
249 route_table_handle_msg(&msg);
253 error = nl_dump_done(&dump);
254 nl_sock_destroy(rtnl_sock);
261 route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
265 static const struct nl_policy policy[] = {
266 [RTA_DST] = { .type = NL_A_U32, .optional = true },
267 [RTA_OIF] = { .type = NL_A_U32, .optional = false },
270 static struct nlattr *attrs[ARRAY_SIZE(policy)];
272 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
273 policy, attrs, ARRAY_SIZE(policy));
276 const struct rtmsg *rtm;
277 const struct nlmsghdr *nlmsg;
280 rtm = (const struct rtmsg *) ((const char *) buf->data + NLMSG_HDRLEN);
282 if (rtm->rtm_family != AF_INET) {
283 VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
287 memset(change, 0, sizeof *change);
288 change->relevant = true;
290 if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
291 change->relevant = false;
294 if (rtm->rtm_type != RTN_UNICAST &&
295 rtm->rtm_type != RTN_LOCAL) {
296 change->relevant = false;
299 change->nlmsg_type = nlmsg->nlmsg_type;
300 change->rd.rtm_dst_len = rtm->rtm_dst_len;
301 change->rd.rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
303 if (attrs[RTA_DST]) {
304 change->rd.rta_dst = ntohl(nl_attr_get_be32(attrs[RTA_DST]));
308 VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
315 route_table_change(const struct route_table_msg *change OVS_UNUSED,
316 void *aux OVS_UNUSED)
318 route_table_valid = false;
322 route_table_handle_msg(const struct route_table_msg *change)
324 if (change->relevant && change->nlmsg_type == RTM_NEWROUTE &&
325 !route_node_lookup(&change->rd)) {
326 struct route_node *rn;
328 rn = xzalloc(sizeof *rn);
329 memcpy(&rn->rd, &change->rd, sizeof change->rd);
331 hmap_insert(&route_map, &rn->node, hash_route_data(&rn->rd));
335 static struct route_node *
336 route_node_lookup(const struct route_data *rd)
338 struct route_node *rn;
340 HMAP_FOR_EACH_WITH_HASH(rn, node, hash_route_data(rd), &route_map) {
341 if (!memcmp(&rn->rd, rd, sizeof *rd)) {
349 static struct route_node *
350 route_node_lookup_by_ip(uint32_t ip)
353 struct route_node *rn, *rn_ret;
358 HMAP_FOR_EACH(rn, node, &route_map) {
359 uint32_t mask = 0xffffffff << (32 - rn->rd.rtm_dst_len);
361 if (rn->rd.rta_dst == 0 && rn->rd.rtm_dst_len == 0) {
366 if (rn->rd.rtm_dst_len > dst_len &&
367 (ip & mask) == (rn->rd.rta_dst & mask)) {
369 dst_len = rn->rd.rtm_dst_len;
377 route_map_clear(void)
379 struct route_node *rn, *rn_next;
381 HMAP_FOR_EACH_SAFE(rn, rn_next, node, &route_map) {
382 hmap_remove(&route_map, &rn->node);
388 hash_route_data(const struct route_data *rd)
390 return hash_bytes(rd, sizeof *rd, 0);
396 name_table_init(void)
398 hmap_init(&name_map);
399 rtnetlink_link_notifier_register(&name_notifier, name_table_change, NULL);
400 name_table_valid = false;
404 name_table_uninit(void)
406 rtnetlink_link_notifier_unregister(&name_notifier);
408 hmap_destroy(&name_map);
412 name_table_reset(void)
416 struct rtgenmsg *rtmsg;
417 struct ofpbuf request, reply;
418 static struct nl_sock *rtnl_sock;
420 name_table_valid = true;
422 error = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
424 VLOG_WARN_RL(&rl, "failed to create NETLINK_ROUTE socket");
428 ofpbuf_init(&request, 0);
429 nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETLINK, NLM_F_REQUEST);
430 rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
431 rtmsg->rtgen_family = AF_INET;
433 nl_dump_start(&dump, rtnl_sock, &request);
434 while (nl_dump_next(&dump, &reply)) {
435 struct rtnetlink_link_change change;
437 if (rtnetlink_link_parse(&reply, &change)
438 && change.nlmsg_type == RTM_NEWLINK
439 && !name_node_lookup(change.ifi_index)) {
440 struct name_node *nn;
442 nn = xzalloc(sizeof *nn);
443 nn->ifi_index = change.ifi_index;
444 ovs_strlcpy(nn->ifname, change.ifname, IFNAMSIZ);
445 hmap_insert(&name_map, &nn->node, hash_int(nn->ifi_index, 0));
448 nl_sock_destroy(rtnl_sock);
449 return nl_dump_done(&dump);
453 name_table_change(const struct rtnetlink_link_change *change OVS_UNUSED,
454 void *aux OVS_UNUSED)
456 /* Changes to interface status can cause routing table changes that some
457 * versions of the linux kernel do not advertise for some reason. */
458 route_table_valid = false;
459 name_table_valid = false;
462 static struct name_node *
463 name_node_lookup(int ifi_index)
465 struct name_node *nn;
467 HMAP_FOR_EACH_WITH_HASH(nn, node, hash_int(ifi_index, 0), &name_map) {
468 if (nn->ifi_index == ifi_index) {
479 struct name_node *nn, *nn_next;
481 HMAP_FOR_EACH_SAFE(nn, nn_next, node, &name_map) {
482 hmap_remove(&name_map, &nn->node);