netlink-socket: Add functions for joining and leaving multicast groups.
[sliver-openvswitch.git] / lib / route-table.c
1 /*
2  * Copyright (c) 2011 Nicira Networks.
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 <assert.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 "hmap.h"
29 #include "netlink.h"
30 #include "netlink-socket.h"
31 #include "ofpbuf.h"
32 #include "rtnetlink.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(route_table);
36
37 struct route_data {
38     /* Copied from struct rtmsg. */
39     unsigned char rtm_dst_len;
40
41     /* Extracted from Netlink attributes. */
42     uint32_t rta_dst; /* Destination in host byte order. 0 if missing. */
43     int rta_oif;      /* Output interface index. */
44 };
45
46 /* A digested version of a route message sent down by the kernel to indicate
47  * that a route has changed. */
48 struct route_table_msg {
49     bool relevant;        /* Should this message be processed? */
50     int nlmsg_type;       /* e.g. RTM_NEWROUTE, RTM_DELROUTE. */
51     struct route_data rd; /* Data parsed from this message. */
52 };
53
54 struct route_node {
55     struct hmap_node node; /* Node in route_map. */
56     struct route_data rd;  /* Data associated with this node. */
57 };
58
59 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
60
61 static unsigned int register_count = 0;
62 static struct rtnetlink *rtn = NULL;
63 static struct route_table_msg rtmsg;
64 static struct rtnetlink_notifier notifier;
65
66 static bool route_table_valid = false;
67 static struct hmap route_map;
68
69 static int route_table_reset(void);
70 static void route_table_handle_msg(const struct route_table_msg *);
71 static bool route_table_parse(struct ofpbuf *, struct route_table_msg *);
72 static void route_table_change(const struct route_table_msg *, void *);
73 static struct route_node *route_node_lookup(const struct route_data *);
74 static struct route_node *route_node_lookup_by_ip(uint32_t ip);
75 static void route_map_clear(void);
76 static uint32_t hash_route_data(const struct route_data *);
77
78 /* Populates 'ifindex' with the interface index traffic destined for 'ip' is
79  * likely to egress.  There is no hard guarantee that traffic destined for 'ip'
80  * will egress out the specified interface.  'ifindex' may refer to an
81  * interface which is not physical (such as a bridge port).
82  *
83  * Returns true if successful, otherwise false. */
84 bool
85 route_table_get_ifindex(ovs_be32 ip_, int *ifindex)
86 {
87     struct route_node *rn;
88     uint32_t ip = ntohl(ip_);
89
90     *ifindex = 0;
91
92     if (!route_table_valid) {
93         route_table_reset();
94     }
95
96     rn = route_node_lookup_by_ip(ip);
97
98     if (rn) {
99         *ifindex = rn->rd.rta_oif;
100         return true;
101     }
102
103     /* Choose a default route. */
104     HMAP_FOR_EACH(rn, node, &route_map) {
105         if (rn->rd.rta_dst == 0 && rn->rd.rtm_dst_len == 0) {
106             *ifindex = rn->rd.rta_oif;
107             return true;
108         }
109     }
110
111     return false;
112 }
113
114 /* Users of the route_table module should register themselves with this
115  * function before making any other route_table function calls. */
116 void
117 route_table_register(void)
118 {
119     if (!register_count) {
120         rtnetlink_parse_func *pf;
121         rtnetlink_notify_func *nf;
122
123         assert(!rtn);
124
125         pf = (rtnetlink_parse_func *)  route_table_parse;
126         nf = (rtnetlink_notify_func *) route_table_change;
127
128         rtn = rtnetlink_create(RTNLGRP_IPV4_ROUTE, pf, &rtmsg);
129         rtnetlink_notifier_register(rtn, &notifier, nf, NULL);
130
131         hmap_init(&route_map);
132         route_table_reset();
133     }
134
135     register_count++;
136 }
137
138 /* Users of the route_table module should unregister themselves with this
139  * function when they will no longer be making any more route_table fuction
140  * calls. */
141 void
142 route_table_unregister(void)
143 {
144     register_count--;
145
146     if (!register_count) {
147         rtnetlink_destroy(rtn);
148         rtn = NULL;
149
150         route_map_clear();
151         hmap_destroy(&route_map);
152     }
153 }
154
155 /* Run periodically to update the locally maintained routing table. */
156 void
157 route_table_run(void)
158 {
159     if (rtn) {
160         rtnetlink_notifier_run(rtn);
161     }
162 }
163
164 /* Causes poll_block() to wake up when route_table updates are required. */
165 void
166 route_table_wait(void)
167 {
168     if (rtn) {
169         rtnetlink_notifier_wait(rtn);
170     }
171 }
172
173 static int
174 route_table_reset(void)
175 {
176     int error;
177     struct nl_dump dump;
178     struct rtgenmsg *rtmsg;
179     struct ofpbuf request, reply;
180     static struct nl_sock *rtnl_sock;
181
182     route_map_clear();
183     route_table_valid = true;
184
185     error = nl_sock_create(NETLINK_ROUTE, &rtnl_sock);
186     if (error) {
187         VLOG_WARN_RL(&rl, "failed to reset routing table, "
188                      "cannot create RTNETLINK_ROUTE socket");
189         return error;
190     }
191
192     ofpbuf_init(&request, 0);
193
194     nl_msg_put_nlmsghdr(&request, sizeof *rtmsg, RTM_GETROUTE, NLM_F_REQUEST);
195
196     rtmsg = ofpbuf_put_zeros(&request, sizeof *rtmsg);
197     rtmsg->rtgen_family = AF_INET;
198
199     nl_dump_start(&dump, rtnl_sock, &request);
200
201     while (nl_dump_next(&dump, &reply)) {
202         struct route_table_msg msg;
203
204         if (route_table_parse(&reply, &msg)) {
205             route_table_handle_msg(&msg);
206         }
207     }
208
209     error = nl_dump_done(&dump);
210     nl_sock_destroy(rtnl_sock);
211
212     return error;
213 }
214
215
216 static bool
217 route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
218 {
219     bool parsed;
220
221     static const struct nl_policy policy[] = {
222         [RTA_DST] = { .type = NL_A_U32, .optional = true  },
223         [RTA_OIF] = { .type = NL_A_U32, .optional = false },
224     };
225
226     static struct nlattr *attrs[ARRAY_SIZE(policy)];
227
228     parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct rtmsg),
229                              policy, attrs, ARRAY_SIZE(policy));
230
231     if (parsed) {
232         const struct rtmsg *rtm;
233         const struct nlmsghdr *nlmsg;
234
235         nlmsg = buf->data;
236         rtm = (const struct rtmsg *) ((const char *) buf->data + NLMSG_HDRLEN);
237
238         if (rtm->rtm_family != AF_INET) {
239             VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
240             return false;
241         }
242
243         memset(change, 0, sizeof *change);
244         change->relevant = true;
245
246         if (rtm->rtm_scope == RT_SCOPE_NOWHERE) {
247             change->relevant = false;
248         }
249
250         if (rtm->rtm_type != RTN_UNICAST &&
251             rtm->rtm_type != RTN_LOCAL) {
252             change->relevant = false;
253         }
254
255         change->nlmsg_type     = nlmsg->nlmsg_type;
256         change->rd.rtm_dst_len = rtm->rtm_dst_len;
257         change->rd.rta_oif     = nl_attr_get_u32(attrs[RTA_OIF]);
258
259         if (attrs[RTA_DST]) {
260             change->rd.rta_dst = ntohl(nl_attr_get_be32(attrs[RTA_DST]));
261         }
262
263     } else {
264         VLOG_DBG_RL(&rl, "received unparseable rtnetlink route message");
265     }
266
267     return parsed;
268 }
269
270 static void
271 route_table_change(const struct route_table_msg *change OVS_UNUSED,
272                    void *aux OVS_UNUSED)
273 {
274     route_table_valid = false;
275 }
276
277 static void
278 route_table_handle_msg(const struct route_table_msg *change)
279 {
280     if (change->relevant && change->nlmsg_type == RTM_NEWROUTE &&
281         !route_node_lookup(&change->rd)) {
282         struct route_node *rn;
283
284         rn = xzalloc(sizeof *rn);
285         memcpy(&rn->rd, &change->rd, sizeof change->rd);
286
287         hmap_insert(&route_map, &rn->node, hash_route_data(&rn->rd));
288     }
289 }
290
291 static struct route_node *
292 route_node_lookup(const struct route_data *rd)
293 {
294     struct route_node *rn;
295
296     HMAP_FOR_EACH_WITH_HASH(rn, node, hash_route_data(rd), &route_map) {
297         if (!memcmp(&rn->rd, rd, sizeof *rd)) {
298             return rn;
299         }
300     }
301
302     return NULL;
303 }
304
305 static struct route_node *
306 route_node_lookup_by_ip(uint32_t ip)
307 {
308     int dst_len;
309     struct route_node *rn, *rn_ret;
310
311     dst_len = -1;
312     rn_ret  = NULL;
313
314     HMAP_FOR_EACH(rn, node, &route_map) {
315         uint32_t mask = 0xffffffff << (32 - rn->rd.rtm_dst_len);
316
317         if (rn->rd.rta_dst == 0 && rn->rd.rtm_dst_len == 0) {
318             /* Default route. */
319             continue;
320         }
321
322         if (rn->rd.rtm_dst_len > dst_len &&
323             (ip & mask) == (rn->rd.rta_dst & mask)) {
324             rn_ret  = rn;
325             dst_len = rn->rd.rtm_dst_len;
326         }
327     }
328
329     return rn_ret;
330 }
331
332 static void
333 route_map_clear(void)
334 {
335     struct route_node *rn, *rn_next;
336
337     HMAP_FOR_EACH_SAFE(rn, rn_next, node, &route_map) {
338         hmap_remove(&route_map, &rn->node);
339         free(rn);
340     }
341 }
342
343 static uint32_t
344 hash_route_data(const struct route_data *rd)
345 {
346     return hash_bytes(rd, sizeof *rd, 0);
347 }