Get rid of unused parameter to rate_limit_start().
[sliver-openvswitch.git] / lib / dpif.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "dpif.h"
36
37 #include <ctype.h>
38 #include <errno.h>
39 #include <inttypes.h>
40 #include <netinet/in.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "netlink.h"
45 #include "netlink-protocol.h"
46 #include "ofpbuf.h"
47 #include "openflow/openflow-netlink.h"
48 #include "openflow/openflow.h"
49 #include "packets.h"
50 #include "util.h"
51 #include "xtoxll.h"
52
53 #include "vlog.h"
54 #define THIS_MODULE VLM_dpif
55
56 /* Not really much point in logging many dpif errors. */
57 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 60);
58
59 /* The Generic Netlink family number used for OpenFlow. */
60 static int openflow_family;
61
62 static int lookup_openflow_multicast_group(int dp_idx, int *multicast_group);
63 static int send_mgmt_command(struct dpif *, int command,
64                              const char *netdev);
65
66 /* Opens the local datapath numbered 'dp_idx', initializing 'dp'.  If
67  * 'subscribe' is true, listens for asynchronous messages (packet-in, etc.)
68  * from the datapath; otherwise, 'dp' will receive only replies to explicitly
69  * initiated requests. */
70 int
71 dpif_open(int dp_idx, bool subscribe, struct dpif *dp)
72 {
73     struct nl_sock *sock;
74     int multicast_group = 0;
75     int retval;
76
77     retval = nl_lookup_genl_family(DP_GENL_FAMILY_NAME, &openflow_family);
78     if (retval) {
79         return retval;
80     }
81
82     if (subscribe) {
83         retval = lookup_openflow_multicast_group(dp_idx, &multicast_group);
84         if (retval) {
85             return retval;
86         }
87     }
88
89     /* Specify a large so_rcvbuf size because we occasionally need to be able
90      * to retrieve large collections of flow records. */
91     retval = nl_sock_create(NETLINK_GENERIC, multicast_group, 0,
92                             4 * 1024u * 1024, &sock);
93     if (retval) {
94         return retval;
95     }
96
97     dp->dp_idx = dp_idx;
98     dp->sock = sock;
99     return 0;
100 }
101
102 /* Closes 'dp'. */
103 void
104 dpif_close(struct dpif *dp) 
105 {
106     nl_sock_destroy(dp->sock);
107 }
108
109 static const struct nl_policy openflow_policy[] = {
110     [DP_GENL_A_DP_IDX] = { .type = NL_A_U32 },
111     [DP_GENL_A_OPENFLOW] = { .type = NL_A_UNSPEC,
112                               .min_len = sizeof(struct ofp_header),
113                               .max_len = 65535 },
114 };
115
116 /* Tries to receive an openflow message from the kernel on 'sock'.  If
117  * successful, stores the received message into '*msgp' and returns 0.  The
118  * caller is responsible for destroying the message with ofpbuf_delete().  On
119  * failure, returns a positive errno value and stores a null pointer into
120  * '*msgp'.
121  *
122  * Only Netlink messages with embedded OpenFlow messages are accepted.  Other
123  * Netlink messages provoke errors.
124  *
125  * If 'wait' is true, dpif_recv_openflow waits for a message to be ready;
126  * otherwise, returns EAGAIN if the 'sock' receive buffer is empty. */
127 int
128 dpif_recv_openflow(struct dpif *dp, struct ofpbuf **bufferp,
129                         bool wait) 
130 {
131     struct nlattr *attrs[ARRAY_SIZE(openflow_policy)];
132     struct ofpbuf *buffer;
133     struct ofp_header *oh;
134     uint16_t ofp_len;
135     int retval;
136
137     buffer = *bufferp = NULL;
138     do {
139         ofpbuf_delete(buffer);
140         retval = nl_sock_recv(dp->sock, &buffer, wait);
141     } while (retval == ENOBUFS
142              || (!retval
143                  && (nl_msg_nlmsgerr(buffer, NULL)
144                      || nl_msg_nlmsghdr(buffer)->nlmsg_type == NLMSG_DONE)));
145     if (retval) {
146         if (retval != EAGAIN) {
147             VLOG_WARN_RL(&rl, "dpif_recv_openflow: %s", strerror(retval)); 
148         }
149         return retval;
150     }
151
152     if (nl_msg_genlmsghdr(buffer) == NULL) {
153         VLOG_DBG_RL(&rl, "received packet too short for Generic Netlink");
154         goto error;
155     }
156     if (nl_msg_nlmsghdr(buffer)->nlmsg_type != openflow_family) {
157         VLOG_DBG_RL(&rl, "received type (%"PRIu16") != openflow family (%d)",
158                     nl_msg_nlmsghdr(buffer)->nlmsg_type, openflow_family);
159         goto error;
160     }
161
162     if (!nl_policy_parse(buffer, openflow_policy, attrs,
163                          ARRAY_SIZE(openflow_policy))) {
164         goto error;
165     }
166     if (nl_attr_get_u32(attrs[DP_GENL_A_DP_IDX]) != dp->dp_idx) {
167         VLOG_WARN_RL(&rl, "received dp_idx (%"PRIu32") differs from expected "
168                      "(%d)", nl_attr_get_u32(attrs[DP_GENL_A_DP_IDX]),
169                      dp->dp_idx);
170         goto error;
171     }
172
173     oh = buffer->data = (void *) nl_attr_get(attrs[DP_GENL_A_OPENFLOW]);
174     buffer->size = nl_attr_get_size(attrs[DP_GENL_A_OPENFLOW]);
175     ofp_len = ntohs(oh->length);
176     if (ofp_len != buffer->size) {
177         VLOG_WARN_RL(&rl,
178                      "ofp_header.length %"PRIu16" != attribute length %zu\n",
179                      ofp_len, buffer->size);
180         buffer->size = MIN(ofp_len, buffer->size);
181     }
182     *bufferp = buffer;
183     return 0;
184
185 error:
186     ofpbuf_delete(buffer);
187     return EPROTO;
188 }
189
190 /* Encapsulates 'msg', which must contain an OpenFlow message, in a Netlink
191  * message, and sends it to the OpenFlow kernel module via 'sock'.
192  *
193  * Returns 0 if successful, otherwise a positive errno value.  If
194  * 'wait' is true, then the send will wait until buffer space is ready;
195  * otherwise, returns EAGAIN if the 'sock' send buffer is full.
196  *
197  * If the send is successful, then the kernel module will receive it, but there
198  * is no guarantee that any reply will not be dropped (see nl_sock_transact()
199  * for details). 
200  */
201 int
202 dpif_send_openflow(struct dpif *dp, struct ofpbuf *buffer, bool wait) 
203 {
204     struct ofp_header *oh;
205     unsigned int dump_flag;
206     struct ofpbuf hdr;
207     struct nlattr *nla;
208     uint32_t fixed_buffer[64 / 4];
209     struct iovec iov[3];
210     int pad_bytes;
211     int n_iov;
212     int retval;
213
214     /* The reply to OFPT_STATS_REQUEST may be multiple segments long, so we
215      * need to specify NLM_F_DUMP in the request. */
216     oh = ofpbuf_at_assert(buffer, 0, sizeof *oh);
217     dump_flag = oh->type == OFPT_STATS_REQUEST ? NLM_F_DUMP : 0;
218
219     ofpbuf_use(&hdr, fixed_buffer, sizeof fixed_buffer);
220     nl_msg_put_genlmsghdr(&hdr, dp->sock, 32, openflow_family,
221                           NLM_F_REQUEST | dump_flag, DP_GENL_C_OPENFLOW, 1);
222     nl_msg_put_u32(&hdr, DP_GENL_A_DP_IDX, dp->dp_idx);
223     nla = ofpbuf_put_uninit(&hdr, sizeof *nla);
224     nla->nla_len = sizeof *nla + buffer->size;
225     nla->nla_type = DP_GENL_A_OPENFLOW;
226     pad_bytes = NLA_ALIGN(nla->nla_len) - nla->nla_len;
227     nl_msg_nlmsghdr(&hdr)->nlmsg_len = hdr.size + buffer->size + pad_bytes;
228     n_iov = 2;
229     iov[0].iov_base = hdr.data;
230     iov[0].iov_len = hdr.size;
231     iov[1].iov_base = buffer->data;
232     iov[1].iov_len = buffer->size;
233     if (pad_bytes) {
234         static char zeros[NLA_ALIGNTO];
235         n_iov++;
236         iov[2].iov_base = zeros;
237         iov[2].iov_len = pad_bytes; 
238     }
239     retval = nl_sock_sendv(dp->sock, iov, n_iov, false);
240     if (retval && retval != EAGAIN) {
241         VLOG_WARN_RL(&rl, "dpif_send_openflow: %s", strerror(retval));
242     }
243     return retval;
244 }
245
246 /* Creates the datapath represented by 'dp'.  Returns 0 if successful,
247  * otherwise a positive errno value. */
248 int
249 dpif_add_dp(struct dpif *dp)
250 {
251     return send_mgmt_command(dp, DP_GENL_C_ADD_DP, NULL);
252 }
253
254 /* Destroys the datapath represented by 'dp'.  Returns 0 if successful,
255  * otherwise a positive errno value. */
256 int
257 dpif_del_dp(struct dpif *dp) 
258 {
259     return send_mgmt_command(dp, DP_GENL_C_DEL_DP, NULL);
260 }
261
262 /* Adds the Ethernet device named 'netdev' to this datapath.  Returns 0 if
263  * successful, otherwise a positive errno value. */
264 int
265 dpif_add_port(struct dpif *dp, const char *netdev)
266 {
267     return send_mgmt_command(dp, DP_GENL_C_ADD_PORT, netdev);
268 }
269
270 /* Removes the Ethernet device named 'netdev' from this datapath.  Returns 0
271  * if successful, otherwise a positive errno value. */
272 int
273 dpif_del_port(struct dpif *dp, const char *netdev)
274 {
275     return send_mgmt_command(dp, DP_GENL_C_DEL_PORT, netdev);
276 }
277 \f
278 static const struct nl_policy openflow_multicast_policy[] = {
279     [DP_GENL_A_DP_IDX] = { .type = NL_A_U32 },
280     [DP_GENL_A_MC_GROUP] = { .type = NL_A_U32 },
281 };
282
283 /* Looks up the Netlink multicast group used by datapath 'dp_idx'.  If
284  * successful, stores the multicast group in '*multicast_group' and returns 0.
285  * Otherwise, returns a positve errno value. */
286 static int
287 lookup_openflow_multicast_group(int dp_idx, int *multicast_group) 
288 {
289     struct nl_sock *sock;
290     struct ofpbuf request, *reply;
291     struct nlattr *attrs[ARRAY_SIZE(openflow_multicast_policy)];
292     int retval;
293
294     retval = nl_sock_create(NETLINK_GENERIC, 0, 0, 0, &sock);
295     if (retval) {
296         return retval;
297     }
298     ofpbuf_init(&request, 0);
299     nl_msg_put_genlmsghdr(&request, sock, 0, openflow_family, NLM_F_REQUEST,
300                           DP_GENL_C_QUERY_DP, 1);
301     nl_msg_put_u32(&request, DP_GENL_A_DP_IDX, dp_idx);
302     retval = nl_sock_transact(sock, &request, &reply);
303     ofpbuf_uninit(&request);
304     if (retval) {
305         nl_sock_destroy(sock);
306         return retval;
307     }
308     if (!nl_policy_parse(reply, openflow_multicast_policy, attrs,
309                          ARRAY_SIZE(openflow_multicast_policy))) {
310         nl_sock_destroy(sock);
311         ofpbuf_delete(reply);
312         return EPROTO;
313     }
314     *multicast_group = nl_attr_get_u32(attrs[DP_GENL_A_MC_GROUP]);
315     nl_sock_destroy(sock);
316     ofpbuf_delete(reply);
317
318     return 0;
319 }
320
321 /* Sends the given 'command' to datapath 'dp'.  If 'netdev' is nonnull, adds it
322  * to the command as the port name attribute.  Returns 0 if successful,
323  * otherwise a positive errno value. */
324 static int
325 send_mgmt_command(struct dpif *dp, int command, const char *netdev) 
326 {
327     struct ofpbuf request, *reply;
328     int retval;
329
330     ofpbuf_init(&request, 0);
331     nl_msg_put_genlmsghdr(&request, dp->sock, 32, openflow_family,
332                           NLM_F_REQUEST | NLM_F_ACK, command, 1);
333     nl_msg_put_u32(&request, DP_GENL_A_DP_IDX, dp->dp_idx);
334     if (netdev) {
335         nl_msg_put_string(&request, DP_GENL_A_PORTNAME, netdev);
336     }
337     retval = nl_sock_transact(dp->sock, &request, &reply);
338     ofpbuf_uninit(&request);
339     ofpbuf_delete(reply);
340
341     return retval;
342 }