route-table: Use classifier to store routing table.
[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 "ofpbuf.h"
32 #include "ovs-router.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     ovs_be32 rta_dst; /* 0 if missing. */
44     char ifname[IFNAMSIZ]; /* Interface name. */
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 static struct ovs_mutex route_table_mutex = OVS_MUTEX_INITIALIZER;
56 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
57
58 /* Global change number for route-table, which should be incremented
59  * every time route_table_reset() is called.  */
60 static uint64_t rt_change_seq;
61
62 static unsigned int register_count = 0;
63 static struct nln *nln = NULL;
64 static struct route_table_msg rtmsg;
65 static struct nln_notifier *route_notifier = NULL;
66 static struct nln_notifier *name_notifier = NULL;
67
68 static bool route_table_valid = false;
69
70 static int route_table_reset(void);
71 static void route_table_handle_msg(const struct route_table_msg *);
72 static bool route_table_parse(struct ofpbuf *, struct route_table_msg *);
73 static void route_table_change(const struct route_table_msg *, void *);
74 static void route_map_clear(void);
75
76 static void name_table_init(void);
77 static void name_table_uninit(void);
78 static void name_table_change(const struct rtnetlink_link_change *, void *);
79
80 uint64_t
81 route_table_get_change_seq(void)
82 {
83     return rt_change_seq;
84 }
85
86 /* Users of the route_table module should register themselves with this
87  * function before making any other route_table function calls. */
88 void
89 route_table_register(void)
90     OVS_EXCLUDED(route_table_mutex)
91 {
92     ovs_mutex_lock(&route_table_mutex);
93     if (!register_count) {
94         ovs_assert(!nln);
95         ovs_assert(!route_notifier);
96
97         nln = nln_create(NETLINK_ROUTE, RTNLGRP_IPV4_ROUTE,
98                          (nln_parse_func *) route_table_parse, &rtmsg);
99
100         route_notifier =
101             nln_notifier_create(nln, (nln_notify_func *) route_table_change,
102                                 NULL);
103
104         route_table_reset();
105         name_table_init();
106     }
107
108     register_count++;
109     ovs_mutex_unlock(&route_table_mutex);
110 }
111
112 /* Users of the route_table module should unregister themselves with this
113  * function when they will no longer be making any more route_table fuction
114  * calls. */
115 void
116 route_table_unregister(void)
117     OVS_EXCLUDED(route_table_mutex)
118 {
119     ovs_mutex_lock(&route_table_mutex);
120     register_count--;
121
122     if (!register_count) {
123         nln_notifier_destroy(route_notifier);
124         route_notifier = NULL;
125         nln_destroy(nln);
126         nln = NULL;
127
128         route_map_clear();
129         name_table_uninit();
130     }
131     ovs_mutex_unlock(&route_table_mutex);
132 }
133
134 /* Run periodically to update the locally maintained routing table. */
135 void
136 route_table_run(void)
137     OVS_EXCLUDED(route_table_mutex)
138 {
139     ovs_mutex_lock(&route_table_mutex);
140     if (nln) {
141         rtnetlink_link_run();
142         nln_run(nln);
143
144         if (!route_table_valid) {
145             route_table_reset();
146         }
147     }
148     ovs_mutex_unlock(&route_table_mutex);
149 }
150
151 /* Causes poll_block() to wake up when route_table updates are required. */
152 void
153 route_table_wait(void)
154     OVS_EXCLUDED(route_table_mutex)
155 {
156     ovs_mutex_lock(&route_table_mutex);
157     if (nln) {
158         rtnetlink_link_wait();
159         nln_wait(nln);
160     }
161     ovs_mutex_unlock(&route_table_mutex);
162 }
163
164 static int
165 route_table_reset(void)
166 {
167     struct nl_dump dump;
168     struct rtgenmsg *rtmsg;
169     uint64_t reply_stub[NL_DUMP_BUFSIZE / 8];
170     struct ofpbuf request, reply, buf;
171
172     route_map_clear();
173     route_table_valid = true;
174     rt_change_seq++;
175
176     ofpbuf_init(&request, 0);
177
178     nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
179
180     rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
181     rtmsg->rtgen_family = AF_INET;
182
183     nl_dump_start(&dump, NETLINK_ROUTE, &request);
184     ofpbuf_uninit(&request);
185
186     ofpbuf_use_stub(&buf, reply_stub, sizeof reply_stub);
187     while (nl_dump_next(&dump, &reply, &buf)) {
188         struct route_table_msg msg;
189
190         if (route_table_parse(&reply, &msg)) {
191             route_table_handle_msg(&msg);
192         }
193     }
194     ofpbuf_uninit(&buf);
195
196     return nl_dump_done(&dump);
197 }
198
199
200 static bool
201 route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
202 {
203     bool parsed;
204
205     static const struct nl_policy policy[] = {
206         [RTA_DST] = { .type = NL_A_U32, .optional = true  },
207         [RTA_OIF] = { .type = NL_A_U32, .optional = false },
208     };
209
210     struct nlattr *attrs[ARRAY_SIZE(policy)];
211
212     parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
213                              policy, attrs, ARRAY_SIZE(policy));
214
215     if (parsed) {
216         const struct rtmsg *rtm;
217         const struct nlmsghdr *nlmsg;
218         int rta_oif;      /* Output interface index. */
219
220         nlmsg = ofpbuf_data(buf);
221         rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
222
223         if (rtm->rtm_family != AF_INET) {
224             VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
225             return false;
226         }
227
228         memset(change, 0, sizeof *change);
229         change->relevant = true;
230
231         if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
232             change->relevant = false;
233         }
234
235         if (rtm->rtm_type != RTN_UNICAST &&
236             rtm->rtm_type != RTN_LOCAL) {
237             change->relevant = false;
238         }
239         change->nlmsg_type     = nlmsg->nlmsg_type;
240         change->rd.rtm_dst_len = rtm->rtm_dst_len;
241         rta_oif = nl_attr_get_u32(attrs[RTA_OIF]);
242
243         if (!if_indextoname(rta_oif, change->rd.ifname)) {
244             int error = errno;
245
246             VLOG_DBG_RL(&rl, "Could not find interface name[%u]: %s",
247                         rta_oif, ovs_strerror(error));
248             return false;
249         }
250
251         if (attrs[RTA_DST]) {
252             change->rd.rta_dst = nl_attr_get_be32(attrs[RTA_DST]);
253         }
254
255     } else {
256         VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
257     }
258
259     return parsed;
260 }
261
262 static void
263 route_table_change(const struct route_table_msg *change OVS_UNUSED,
264                    void *aux OVS_UNUSED)
265 {
266     route_table_valid = false;
267 }
268
269 static void
270 route_table_handle_msg(const struct route_table_msg *change)
271 {
272     if (change->relevant && change->nlmsg_type == RTM_NEWROUTE) {
273         const struct route_data *rd = &change->rd;
274
275         ovs_router_insert(rd->rta_dst, rd->rtm_dst_len, rd->ifname, 0);
276     }
277 }
278
279 static void
280 route_map_clear(void)
281 {
282     ovs_router_flush();
283 }
284
285 \f
286 /* name_table . */
287
288 static void
289 name_table_init(void)
290 {
291     name_notifier = rtnetlink_link_notifier_create(name_table_change, NULL);
292 }
293
294 static void
295 name_table_uninit(void)
296 {
297     rtnetlink_link_notifier_destroy(name_notifier);
298     name_notifier = NULL;
299 }
300
301 static void
302 name_table_change(const struct rtnetlink_link_change *change OVS_UNUSED,
303                   void *aux OVS_UNUSED)
304 {
305     /* Changes to interface status can cause routing table changes that some
306      * versions of the linux kernel do not advertise for some reason. */
307     route_table_valid = false;
308 }