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>
29 #include "netlink-notifier.h"
30 #include "netlink-socket.h"
32 #include "ovs-router.h"
34 #include "rtnetlink.h"
35 #include "openvswitch/vlog.h"
37 VLOG_DEFINE_THIS_MODULE(route_table);
40 /* Copied from struct rtmsg. */
41 unsigned char rtm_dst_len;
43 /* Extracted from Netlink attributes. */
44 struct in6_addr rta_dst; /* 0 if missing. */
45 struct in6_addr rta_gw;
46 char ifname[IFNAMSIZ]; /* Interface name. */
49 /* A digested version of a route message sent down by the kernel to indicate
50 * that a route has changed. */
51 struct route_table_msg {
52 bool relevant; /* Should this message be processed? */
53 int nlmsg_type; /* e.g. RTM_NEWROUTE, RTM_DELROUTE. */
54 struct route_data rd; /* Data parsed from this message. */
57 static struct ovs_mutex route_table_mutex = OVS_MUTEX_INITIALIZER;
58 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
60 /* Global change number for route-table, which should be incremented
61 * every time route_table_reset() is called. */
62 static uint64_t rt_change_seq;
64 static struct nln *nln = NULL;
65 static struct nln *nln6 = NULL;
66 static struct route_table_msg rtmsg;
67 static struct nln_notifier *route_notifier = NULL;
68 static struct nln_notifier *route6_notifier = NULL;
69 static struct nln_notifier *name_notifier = NULL;
71 static bool route_table_valid = false;
73 static int route_table_reset(void);
74 static void route_table_handle_msg(const struct route_table_msg *);
75 static bool route_table_parse(struct ofpbuf *, struct route_table_msg *);
76 static void route_table_change(const struct route_table_msg *, void *);
77 static void route_map_clear(void);
79 static void name_table_init(void);
80 static void name_table_change(const struct rtnetlink_change *, void *);
83 route_table_get_change_seq(void)
88 /* Users of the route_table module should register themselves with this
89 * function before making any other route_table function calls. */
91 route_table_init(void)
92 OVS_EXCLUDED(route_table_mutex)
94 ovs_mutex_lock(&route_table_mutex);
97 ovs_assert(!route_notifier);
98 ovs_assert(!route6_notifier);
101 nln = nln_create(NETLINK_ROUTE, RTNLGRP_IPV4_ROUTE,
102 (nln_parse_func *) route_table_parse, &rtmsg);
103 nln6 = nln_create(NETLINK_ROUTE, RTNLGRP_IPV6_ROUTE,
104 (nln_parse_func *) route_table_parse, &rtmsg);
107 nln_notifier_create(nln, (nln_notify_func *) route_table_change,
110 nln_notifier_create(nln6, (nln_notify_func *) route_table_change,
116 ovs_mutex_unlock(&route_table_mutex);
119 /* Run periodically to update the locally maintained routing table. */
121 route_table_run(void)
122 OVS_EXCLUDED(route_table_mutex)
124 ovs_mutex_lock(&route_table_mutex);
134 if (!route_table_valid) {
138 ovs_mutex_unlock(&route_table_mutex);
141 /* Causes poll_block() to wake up when route_table updates are required. */
143 route_table_wait(void)
144 OVS_EXCLUDED(route_table_mutex)
146 ovs_mutex_lock(&route_table_mutex);
156 ovs_mutex_unlock(&route_table_mutex);
160 route_table_reset(void)
163 struct rtgenmsg *rtmsg;
164 uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
165 struct ofpbuf request, reply, buf;
168 route_table_valid = true;
171 ofpbuf_init(&request, 0);
173 nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
175 rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
176 rtmsg->rtgen_family = AF_UNSPEC;
178 nl_dump_start(&dump, NETLINK_ROUTE, &request);
179 ofpbuf_uninit(&request);
181 ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
182 while (nl_dump_next(&dump, &reply, &buf)) {
183 struct route_table_msg msg;
185 if (route_table_parse(&reply, &msg)) {
186 route_table_handle_msg(&msg);
191 return nl_dump_done(&dump);
195 route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
197 bool parsed, ipv4 = false;
199 static const struct nl_policy policy[] = {
200 [RTA_DST] = { .type = NL_A_U32, .optional = true },
201 [RTA_OIF] = { .type = NL_A_U32, .optional = false },
202 [RTA_GATEWAY] = { .type = NL_A_U32, .optional = true },
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_GATEWAY] = { .type = NL_A_IPV6, .optional = true },
211 struct nlattr *attrs[ARRAY_SIZE(policy)];
212 const struct rtmsg *rtm;
214 rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
216 if (rtm->rtm_family == AF_INET) {
217 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
218 policy, attrs, ARRAY_SIZE(policy));
220 } else if (rtm->rtm_family == AF_INET6) {
221 parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
222 policy6, attrs, ARRAY_SIZE(policy6));
224 VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
229 const struct nlmsghdr *nlmsg;
230 int rta_oif; /* Output interface index. */
234 memset(change, 0, sizeof *change);
235 change->relevant = true;
237 if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
238 change->relevant = false;
241 if (rtm->rtm_type != RTN_UNICAST &&
242 rtm->rtm_type != RTN_LOCAL) {
243 change->relevant = false;
245 change->nlmsg_type = nlmsg->nlmsg_type;
246 change->rd.rtm_dst_len = rtm->rtm_dst_len + (ipv4 ? 96 : 0);
247 if (attrs[RTA_OIF]) {
248 rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
250 if (!if_indextoname(rta_oif, change->rd.ifname)) {
253 VLOG_DBG_RL(&rl, "Could not find interface name[%u]: %s",
254 rta_oif, ovs_strerror(error));
259 if (attrs[RTA_DST]) {
262 dst = nl_attr_get_be32(attrs[RTA_DST]);
263 in6_addr_set_mapped_ipv4(&change->rd.rta_dst, dst);
265 change->rd.rta_dst = nl_attr_get_in6_addr(attrs[RTA_DST]);
268 in6_addr_set_mapped_ipv4(&change->rd.rta_dst, 0);
270 if (attrs[RTA_GATEWAY]) {
273 gw = nl_attr_get_be32(attrs[RTA_GATEWAY]);
274 in6_addr_set_mapped_ipv4(&change->rd.rta_gw, gw);
276 change->rd.rta_gw = nl_attr_get_in6_addr(attrs[RTA_GATEWAY]);
282 VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
289 route_table_change(const struct route_table_msg *change OVS_UNUSED,
290 void *aux OVS_UNUSED)
292 route_table_valid = false;
296 route_table_handle_msg(const struct route_table_msg *change)
298 if (change->relevant && change->nlmsg_type == RTM_NEWROUTE) {
299 const struct route_data *rd = &change->rd;
301 ovs_router_insert(&rd->rta_dst, rd->rtm_dst_len,
302 rd->ifname, &rd->rta_gw);
307 route_map_clear(void)
313 route_table_fallback_lookup(ovs_be32 ip_dst OVS_UNUSED,
314 char output_bridge[] OVS_UNUSED,
325 name_table_init(void)
327 name_notifier = rtnetlink_notifier_create(name_table_change, NULL);
332 name_table_change(const struct rtnetlink_change *change OVS_UNUSED,
333 void *aux OVS_UNUSED)
335 /* Changes to interface status can cause routing table changes that some
336 * versions of the linux kernel do not advertise for some reason. */
337 route_table_valid = false;