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