2 * Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc.
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 char ifname[IFNAMSIZ]; /* Interface name. */
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. */
60 static struct ovs_mutex route_table_mutex = OVS_MUTEX_INITIALIZER;
61 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
63 /* Global change number for route-table, which should be incremented
64 * every time route_table_reset() is called. */
65 static uint64_t rt_change_seq;
67 static unsigned int register_count = 0;
68 static struct nln *nln = NULL;
69 static struct route_table_msg rtmsg;
70 static struct nln_notifier *route_notifier = NULL;
71 static struct nln_notifier *name_notifier = NULL;
73 static bool route_table_valid = false;
74 static struct hmap route_map;
76 static int route_table_reset(void);
77 static void route_table_handle_msg(const struct route_table_msg *);
78 static bool route_table_parse(struct ofpbuf *, struct route_table_msg *);
79 static void route_table_change(const struct route_table_msg *, void *);
80 static struct route_node *route_node_lookup(const struct route_data *);
81 static struct route_node *route_node_lookup_by_ip(uint32_t ip);
82 static void route_map_clear(void);
83 static uint32_t hash_route_data(const struct route_data *);
85 static void name_table_init(void);
86 static void name_table_uninit(void);
87 static void name_table_change(const struct rtnetlink_link_change *, void *);
89 /* Populates 'name' with the name of the interface traffic destined for 'ip'
90 * is likely to egress out of.
92 * Returns true if successful, otherwise false. */
94 route_table_get_name(ovs_be32 ip_, char name[IFNAMSIZ])
95 OVS_REQUIRES(route_table_mutex)
97 struct route_node *rn;
98 uint32_t ip = ntohl(ip_);
101 ovs_mutex_lock(&route_table_mutex);
102 if (!route_table_valid) {
106 rn = route_node_lookup_by_ip(ip);
109 ovs_strlcpy(name, rn->rd.ifname, IFNAMSIZ);
114 /* Choose a default route. */
115 HMAP_FOR_EACH(rn, node, &route_map) {
116 if (rn->rd.rta_dst == 0 && rn->rd.rtm_dst_len == 0) {
117 ovs_strlcpy(name, rn->rd.ifname, IFNAMSIZ);
124 ovs_mutex_unlock(&route_table_mutex);
129 route_table_get_change_seq(void)
131 return rt_change_seq;
134 /* Users of the route_table module should register themselves with this
135 * function before making any other route_table function calls. */
137 route_table_register(void)
138 OVS_EXCLUDED(route_table_mutex)
140 ovs_mutex_lock(&route_table_mutex);
141 if (!register_count) {
143 ovs_assert(!route_notifier);
145 nln = nln_create(NETLINK_ROUTE, RTNLGRP_IPV4_ROUTE,
146 (nln_parse_func *) route_table_parse, &rtmsg);
149 nln_notifier_create(nln, (nln_notify_func *) route_table_change,
152 hmap_init(&route_map);
158 ovs_mutex_unlock(&route_table_mutex);
161 /* Users of the route_table module should unregister themselves with this
162 * function when they will no longer be making any more route_table fuction
165 route_table_unregister(void)
166 OVS_EXCLUDED(route_table_mutex)
168 ovs_mutex_lock(&route_table_mutex);
171 if (!register_count) {
172 nln_notifier_destroy(route_notifier);
173 route_notifier = NULL;
178 hmap_destroy(&route_map);
181 ovs_mutex_unlock(&route_table_mutex);
184 /* Run periodically to update the locally maintained routing table. */
186 route_table_run(void)
187 OVS_EXCLUDED(route_table_mutex)
189 ovs_mutex_lock(&route_table_mutex);
191 rtnetlink_link_run();
194 if (!route_table_valid) {
198 ovs_mutex_unlock(&route_table_mutex);
201 /* Causes poll_block() to wake up when route_table updates are required. */
203 route_table_wait(void)
204 OVS_EXCLUDED(route_table_mutex)
206 ovs_mutex_lock(&route_table_mutex);
208 rtnetlink_link_wait();
211 ovs_mutex_unlock(&route_table_mutex);
215 route_table_reset(void)
218 struct rtgenmsg *rtmsg;
219 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
220 struct ofpbuf request, reply, buf;
223 route_table_valid = true;
226 ofpbuf_init(&request, 0);
228 nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
230 rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
231 rtmsg->rtgen_family = AF_INET;
233 nl_dump_start(&dump, NETLINK_ROUTE, &request);
234 ofpbuf_uninit(&request);
236 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
237 while (nl_dump_next(&dump, &reply, &buf)) {
238 struct route_table_msg msg;
240 if (route_table_parse(&reply, &msg)) {
241 route_table_handle_msg(&msg);
246 return nl_dump_done(&dump);
251 route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
255 static const struct nl_policy policy[] = {
256 [RTA_DST] = { .type = NL_A_U32, .optional = true },
257 [RTA_OIF] = { .type = NL_A_U32, .optional = false },
260 struct nlattr *attrs[ARRAY_SIZE(policy)];
262 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
263 policy, attrs, ARRAY_SIZE(policy));
266 const struct rtmsg *rtm;
267 const struct nlmsghdr *nlmsg;
268 int rta_oif; /* Output interface index. */
270 nlmsg = ofpbuf_data(buf);
271 rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
273 if (rtm->rtm_family != AF_INET) {
274 VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
278 memset(change, 0, sizeof *change);
279 change->relevant = true;
281 if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
282 change->relevant = false;
285 if (rtm->rtm_type != RTN_UNICAST &&
286 rtm->rtm_type != RTN_LOCAL) {
287 change->relevant = false;
289 change->nlmsg_type = nlmsg->nlmsg_type;
290 change->rd.rtm_dst_len = rtm->rtm_dst_len;
291 rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
293 if (!if_indextoname(rta_oif, change->rd.ifname)) {
296 VLOG_DBG_RL(&rl, "Could not find interface name[%u]: %s",
297 rta_oif, ovs_strerror(error));
301 if (attrs[RTA_DST]) {
302 change->rd.rta_dst = ntohl(nl_attr_get_be32(attrs[RTA_DST]));
306 VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
313 route_table_change(const struct route_table_msg *change OVS_UNUSED,
314 void *aux OVS_UNUSED)
316 route_table_valid = false;
320 route_table_handle_msg(const struct route_table_msg *change)
322 if (change->relevant && change->nlmsg_type == RTM_NEWROUTE &&
323 !route_node_lookup(&change->rd)) {
324 struct route_node *rn;
326 rn = xzalloc(sizeof *rn);
327 memcpy(&rn->rd, &change->rd, sizeof change->rd);
329 hmap_insert(&route_map, &rn->node, hash_route_data(&rn->rd));
333 static struct route_node *
334 route_node_lookup(const struct route_data *rd)
336 struct route_node *rn;
338 HMAP_FOR_EACH_WITH_HASH(rn, node, hash_route_data(rd), &route_map) {
339 if (!memcmp(&rn->rd, rd, sizeof *rd)) {
347 static struct route_node *
348 route_node_lookup_by_ip(uint32_t ip)
351 struct route_node *rn, *rn_ret;
356 HMAP_FOR_EACH(rn, node, &route_map) {
357 uint32_t mask = 0xffffffff << (32 - rn->rd.rtm_dst_len);
359 if (rn->rd.rta_dst == 0 && rn->rd.rtm_dst_len == 0) {
364 if (rn->rd.rtm_dst_len > dst_len &&
365 (ip & mask) == (rn->rd.rta_dst & mask)) {
367 dst_len = rn->rd.rtm_dst_len;
375 route_map_clear(void)
377 struct route_node *rn, *rn_next;
379 HMAP_FOR_EACH_SAFE(rn, rn_next, node, &route_map) {
380 hmap_remove(&route_map, &rn->node);
386 hash_route_data(const struct route_data *rd)
388 return hash_bytes(rd, sizeof *rd, 0);
394 name_table_init(void)
396 name_notifier = rtnetlink_link_notifier_create(name_table_change, NULL);
400 name_table_uninit(void)
402 rtnetlink_link_notifier_destroy(name_notifier);
403 name_notifier = NULL;
407 name_table_change(const struct rtnetlink_link_change *change OVS_UNUSED,
408 void *aux OVS_UNUSED)
410 /* Changes to interface status can cause routing table changes that some
411 * versions of the linux kernel do not advertise for some reason. */
412 route_table_valid = false;