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