netlink-notifier: Support multiple groups.
[cascardo/ovs.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 <errno.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 "netlink.h"
29 #include "netlink-notifier.h"
30 #include "netlink-socket.h"
31 #include "openvswitch/ofpbuf.h"
32 #include "ovs-router.h"
33 #include "packets.h"
34 #include "rtnetlink.h"
35 #include "openvswitch/vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(route_table);
38
39 struct route_data {
40     /* Copied from struct rtmsg. */
41     unsigned char rtm_dst_len;
42
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. */
47 };
48
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. */
55 };
56
57 static struct ovs_mutex route_table_mutex = OVS_MUTEX_INITIALIZER;
58 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
59
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;
63
64 static struct nln *nln = NULL;
65 static struct route_table_msg rtmsg;
66 static struct nln_notifier *route_notifier = NULL;
67 static struct nln_notifier *route6_notifier = NULL;
68 static struct nln_notifier *name_notifier = NULL;
69
70 static bool route_table_valid = false;
71
72 static int route_table_reset(void);
73 static void route_table_handle_msg(const struct route_table_msg *);
74 static int route_table_parse(struct ofpbuf *, struct route_table_msg *);
75 static void route_table_change(const struct route_table_msg *, void *);
76 static void route_map_clear(void);
77
78 static void name_table_init(void);
79 static void name_table_change(const struct rtnetlink_change *, void *);
80
81 uint64_t
82 route_table_get_change_seq(void)
83 {
84     return rt_change_seq;
85 }
86
87 /* Users of the route_table module should register themselves with this
88  * function before making any other route_table function calls. */
89 void
90 route_table_init(void)
91     OVS_EXCLUDED(route_table_mutex)
92 {
93     ovs_mutex_lock(&route_table_mutex);
94     ovs_assert(!nln);
95     ovs_assert(!route_notifier);
96     ovs_assert(!route6_notifier);
97
98     ovs_router_init();
99     nln = nln_create(NETLINK_ROUTE, (nln_parse_func *) route_table_parse,
100                      &rtmsg);
101
102     route_notifier =
103         nln_notifier_create(nln, RTNLGRP_IPV4_ROUTE,
104                             (nln_notify_func *) route_table_change, NULL);
105     route6_notifier =
106         nln_notifier_create(nln, RTNLGRP_IPV6_ROUTE,
107                             (nln_notify_func *) route_table_change, NULL);
108
109     route_table_reset();
110     name_table_init();
111
112     ovs_mutex_unlock(&route_table_mutex);
113 }
114
115 /* Run periodically to update the locally maintained routing table. */
116 void
117 route_table_run(void)
118     OVS_EXCLUDED(route_table_mutex)
119 {
120     ovs_mutex_lock(&route_table_mutex);
121     if (nln) {
122         rtnetlink_run();
123         nln_run(nln);
124
125         if (!route_table_valid) {
126             route_table_reset();
127         }
128     }
129     ovs_mutex_unlock(&route_table_mutex);
130 }
131
132 /* Causes poll_block() to wake up when route_table updates are required. */
133 void
134 route_table_wait(void)
135     OVS_EXCLUDED(route_table_mutex)
136 {
137     ovs_mutex_lock(&route_table_mutex);
138     if (nln) {
139         rtnetlink_wait();
140         nln_wait(nln);
141     }
142     ovs_mutex_unlock(&route_table_mutex);
143 }
144
145 static int
146 route_table_reset(void)
147 {
148     struct nl_dump dump;
149     struct rtgenmsg *rtmsg;
150     uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
151     struct ofpbuf request, reply, buf;
152
153     route_map_clear();
154     route_table_valid = true;
155     rt_change_seq++;
156
157     ofpbuf_init(&request, 0);
158
159     nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
160
161     rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
162     rtmsg->rtgen_family = AF_UNSPEC;
163
164     nl_dump_start(&dump, NETLINK_ROUTE, &request);
165     ofpbuf_uninit(&request);
166
167     ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
168     while (nl_dump_next(&dump, &reply, &buf)) {
169         struct route_table_msg msg;
170
171         if (route_table_parse(&reply, &msg)) {
172             route_table_handle_msg(&msg);
173         }
174     }
175     ofpbuf_uninit(&buf);
176
177     return nl_dump_done(&dump);
178 }
179
180 /* Return RTNLGRP_IPV4_ROUTE or RTNLGRP_IPV6_ROUTE on success, 0 on parse
181  * error. */
182 static int
183 route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
184 {
185     bool parsed, ipv4 = false;
186
187     static const struct nl_policy policy[] = {
188         [RTA_DST] = { .type = NL_A_U32, .optional = true  },
189         [RTA_OIF] = { .type = NL_A_U32, .optional = false },
190         [RTA_GATEWAY] = { .type = NL_A_U32, .optional = true },
191     };
192
193     static const struct nl_policy policy6[] = {
194         [RTA_DST] = { .type = NL_A_IPV6, .optional = true },
195         [RTA_OIF] = { .type = NL_A_U32, .optional = true },
196         [RTA_GATEWAY] = { .type = NL_A_IPV6, .optional = true },
197     };
198
199     struct nlattr *attrs[ARRAY_SIZE(policy)];
200     const struct rtmsg *rtm;
201
202     rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
203
204     if (rtm->rtm_family == AF_INET) {
205         parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
206                                  policy, attrs, ARRAY_SIZE(policy));
207         ipv4 = true;
208     } else if (rtm->rtm_family == AF_INET6) {
209         parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
210                                  policy6, attrs, ARRAY_SIZE(policy6));
211     } else {
212         VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
213         return 0;
214     }
215
216     if (parsed) {
217         const struct nlmsghdr *nlmsg;
218         int rta_oif;      /* Output interface index. */
219
220         nlmsg = buf->data;
221
222         memset(change, 0, sizeof *change);
223         change->relevant = true;
224
225         if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
226             change->relevant = false;
227         }
228
229         if (rtm->rtm_type != RTN_UNICAST &&
230             rtm->rtm_type != RTN_LOCAL) {
231             change->relevant = false;
232         }
233         change->nlmsg_type     = nlmsg->nlmsg_type;
234         change->rd.rtm_dst_len = rtm->rtm_dst_len + (ipv4 ? 96 : 0);
235         if (attrs[RTA_OIF]) {
236             rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
237
238             if (!if_indextoname(rta_oif, change->rd.ifname)) {
239                 int error = errno;
240
241                 VLOG_DBG_RL(&rl, "Could not find interface name[%u]: %s",
242                             rta_oif, ovs_strerror(error));
243                 if (error == ENXIO) {
244                     change->relevant = false;
245                 } else {
246                     return 0;
247                 }
248             }
249         }
250
251         if (attrs[RTA_DST]) {
252             if (ipv4) {
253                 ovs_be32 dst;
254                 dst = nl_attr_get_be32(attrs[RTA_DST]);
255                 in6_addr_set_mapped_ipv4(&change->rd.rta_dst, dst);
256             } else {
257                 change->rd.rta_dst = nl_attr_get_in6_addr(attrs[RTA_DST]);
258             }
259         } else if (ipv4) {
260             in6_addr_set_mapped_ipv4(&change->rd.rta_dst, 0);
261         }
262         if (attrs[RTA_GATEWAY]) {
263             if (ipv4) {
264                 ovs_be32 gw;
265                 gw = nl_attr_get_be32(attrs[RTA_GATEWAY]);
266                 in6_addr_set_mapped_ipv4(&change->rd.rta_gw, gw);
267             } else {
268                 change->rd.rta_gw = nl_attr_get_in6_addr(attrs[RTA_GATEWAY]);
269             }
270         }
271     } else {
272         VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
273         return 0;
274     }
275
276     /* Success. */
277     return ipv4 ? RTNLGRP_IPV4_ROUTE : RTNLGRP_IPV6_ROUTE;
278 }
279
280 static void
281 route_table_change(const struct route_table_msg *change OVS_UNUSED,
282                    void *aux OVS_UNUSED)
283 {
284     route_table_valid = false;
285 }
286
287 static void
288 route_table_handle_msg(const struct route_table_msg *change)
289 {
290     if (change->relevant && change->nlmsg_type == RTM_NEWROUTE) {
291         const struct route_data *rd = &change->rd;
292
293         ovs_router_insert(&rd->rta_dst, rd->rtm_dst_len,
294                           rd->ifname, &rd->rta_gw);
295     }
296 }
297
298 static void
299 route_map_clear(void)
300 {
301     ovs_router_flush();
302 }
303
304 bool
305 route_table_fallback_lookup(const struct in6_addr *ip6_dst OVS_UNUSED,
306                             char name[] OVS_UNUSED,
307                             struct in6_addr *gw6)
308 {
309     *gw6 = in6addr_any;
310     return false;
311 }
312
313 \f
314 /* name_table . */
315
316 static void
317 name_table_init(void)
318 {
319     name_notifier = rtnetlink_notifier_create(name_table_change, NULL);
320 }
321
322
323 static void
324 name_table_change(const struct rtnetlink_change *change OVS_UNUSED,
325                   void *aux OVS_UNUSED)
326 {
327     /* Changes to interface status can cause routing table changes that some
328      * versions of the linux kernel do not advertise for some reason. */
329     route_table_valid = false;
330 }