1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
39 #include <netinet/in.h>
45 #include "netlink-protocol.h"
46 #include "ofp-print.h"
47 #include "openflow-netlink.h"
54 #define THIS_MODULE VLM_dpif
56 /* The Generic Netlink family number used for OpenFlow. */
57 static int openflow_family;
59 static int lookup_openflow_multicast_group(int dp_idx, int *multicast_group);
60 static int send_mgmt_command(struct dpif *, int command,
63 /* Opens the local datapath numbered 'dp_idx', initializing 'dp'. If
64 * 'subscribe' is true, listens for asynchronous messages (packet-in, etc.)
65 * from the datapath; otherwise, 'dp' will receive only replies to explicitly
66 * initiated requests. */
68 dpif_open(int dp_idx, bool subscribe, struct dpif *dp)
71 int multicast_group = 0;
74 retval = nl_lookup_genl_family(DP_GENL_FAMILY_NAME, &openflow_family);
80 retval = lookup_openflow_multicast_group(dp_idx, &multicast_group);
86 /* Specify a large so_rcvbuf size because we occasionally need to be able
87 * to retrieve large collections of flow records. */
88 retval = nl_sock_create(NETLINK_GENERIC, multicast_group, 0,
89 4 * 1024u * 1024, &sock);
101 dpif_close(struct dpif *dp)
103 nl_sock_destroy(dp->sock);
106 static const struct nl_policy openflow_policy[] = {
107 [DP_GENL_A_DP_IDX] = { .type = NL_A_U32 },
108 [DP_GENL_A_OPENFLOW] = { .type = NL_A_UNSPEC,
109 .min_len = sizeof(struct ofp_header),
110 .max_len = OFP_MAXLEN },
113 /* Tries to receive an openflow message from the kernel on 'sock'. If
114 * successful, stores the received message into '*msgp' and returns 0. The
115 * caller is responsible for destroying the message with buffer_delete(). On
116 * failure, returns a positive errno value and stores a null pointer into
119 * Only Netlink messages with embedded OpenFlow messages are accepted. Other
120 * Netlink messages provoke errors.
122 * If 'wait' is true, dpif_recv_openflow waits for a message to be ready;
123 * otherwise, returns EAGAIN if the 'sock' receive buffer is empty. */
125 dpif_recv_openflow(struct dpif *dp, struct buffer **bufferp,
128 struct nlattr *attrs[ARRAY_SIZE(openflow_policy)];
129 struct buffer *buffer;
130 struct ofp_header *oh;
136 retval = nl_sock_recv(dp->sock, &buffer, wait);
137 } while (retval == ENOBUFS || (!retval && nl_msg_nlmsgerr(buffer, NULL)));
139 if (retval != EAGAIN) {
140 VLOG_WARN("dpif_recv_openflow: %s", strerror(retval));
145 if (nl_msg_genlmsghdr(buffer) == NULL) {
146 VLOG_DBG("received packet too short for Generic Netlink");
149 if (nl_msg_nlmsghdr(buffer)->nlmsg_type != openflow_family) {
150 VLOG_DBG("received type (%"PRIu16") != openflow family (%d)",
151 nl_msg_nlmsghdr(buffer)->nlmsg_type, openflow_family);
155 if (!nl_policy_parse(buffer, openflow_policy, attrs,
156 ARRAY_SIZE(openflow_policy))) {
159 if (nl_attr_get_u32(attrs[DP_GENL_A_DP_IDX]) != dp->dp_idx) {
160 VLOG_WARN("received dp_idx (%"PRIu32") differs from expected (%d)",
161 nl_attr_get_u32(attrs[DP_GENL_A_DP_IDX]), dp->dp_idx);
165 oh = buffer->data = (void *) nl_attr_get(attrs[DP_GENL_A_OPENFLOW]);
166 buffer->size = nl_attr_get_size(attrs[DP_GENL_A_OPENFLOW]);
167 ofp_len = ntohs(oh->length);
168 if (ofp_len != buffer->size) {
169 VLOG_WARN("ofp_header.length %"PRIu16" != attribute length %zu\n",
170 ofp_len, buffer->size);
171 buffer->size = MIN(ofp_len, buffer->size);
177 buffer_delete(buffer);
181 /* Encapsulates 'msg', which must contain an OpenFlow message, in a Netlink
182 * message, and sends it to the OpenFlow kernel module via 'sock'.
184 * Returns 0 if successful, otherwise a positive errno value. If
185 * 'wait' is true, then the send will wait until buffer space is ready;
186 * otherwise, returns EAGAIN if the 'sock' send buffer is full.
188 * If the send is successful, then the kernel module will receive it, but there
189 * is no guarantee that any reply will not be dropped (see nl_sock_transact()
193 dpif_send_openflow(struct dpif *dp, struct buffer *buffer, bool wait)
195 struct ofp_header *oh;
196 unsigned int dump_flag;
199 uint32_t fixed_buffer[64 / 4];
205 /* The reply to OFPT_FLOW_STATS_REQUEST may be multiple segments long, so
206 * we need to specify NLM_F_DUMP in the request. */
207 oh = buffer_at_assert(buffer, 0, sizeof *oh);
208 dump_flag = oh->type == OFPT_FLOW_STATS_REQUEST ? NLM_F_DUMP : 0;
210 buffer_use(&hdr, fixed_buffer, sizeof fixed_buffer);
211 nl_msg_put_genlmsghdr(&hdr, dp->sock, 32, openflow_family,
212 NLM_F_REQUEST | dump_flag, DP_GENL_C_OPENFLOW, 1);
213 nl_msg_put_u32(&hdr, DP_GENL_A_DP_IDX, dp->dp_idx);
214 nla = buffer_put_uninit(&hdr, sizeof *nla);
215 nla->nla_len = sizeof *nla + buffer->size;
216 nla->nla_type = DP_GENL_A_OPENFLOW;
217 pad_bytes = NLA_ALIGN(nla->nla_len) - nla->nla_len;
218 nl_msg_nlmsghdr(&hdr)->nlmsg_len = hdr.size + buffer->size + pad_bytes;
220 iov[0].iov_base = hdr.data;
221 iov[0].iov_len = hdr.size;
222 iov[1].iov_base = buffer->data;
223 iov[1].iov_len = buffer->size;
225 static char zeros[NLA_ALIGNTO];
227 iov[2].iov_base = zeros;
228 iov[2].iov_len = pad_bytes;
230 retval = nl_sock_sendv(dp->sock, iov, n_iov, false);
231 if (retval && retval != EAGAIN) {
232 VLOG_WARN("dpif_send_openflow: %s", strerror(retval));
237 /* Creates the datapath represented by 'dp'. Returns 0 if successful,
238 * otherwise a positive errno value. */
240 dpif_add_dp(struct dpif *dp)
242 return send_mgmt_command(dp, DP_GENL_C_ADD_DP, NULL);
245 /* Destroys the datapath represented by 'dp'. Returns 0 if successful,
246 * otherwise a positive errno value. */
248 dpif_del_dp(struct dpif *dp)
250 return send_mgmt_command(dp, DP_GENL_C_DEL_DP, NULL);
253 /* Adds the Ethernet device named 'netdev' to this datapath. Returns 0 if
254 * successful, otherwise a positive errno value. */
256 dpif_add_port(struct dpif *dp, const char *netdev)
258 return send_mgmt_command(dp, DP_GENL_C_ADD_PORT, netdev);
261 /* Removes the Ethernet device named 'netdev' from this datapath. Returns 0
262 * if successful, otherwise a positive errno value. */
264 dpif_del_port(struct dpif *dp, const char *netdev)
266 return send_mgmt_command(dp, DP_GENL_C_DEL_PORT, netdev);
269 /* Tells dp to send num_packets up through netlink for benchmarking*/
271 dpif_benchmark_nl(struct dpif *dp, uint32_t num_packets, uint32_t packet_size)
273 struct buffer request;
276 buffer_init(&request, 0);
277 nl_msg_put_genlmsghdr(&request, dp->sock, 0, openflow_family,
278 NLM_F_REQUEST, DP_GENL_C_BENCHMARK_NL, 1);
279 nl_msg_put_u32(&request, DP_GENL_A_DP_IDX, dp->dp_idx);
280 nl_msg_put_u32(&request, DP_GENL_A_NPACKETS, num_packets);
281 nl_msg_put_u32(&request, DP_GENL_A_PSIZE, packet_size);
282 retval = nl_sock_send(dp->sock, &request, true);
283 buffer_uninit(&request);
288 static const struct nl_policy openflow_multicast_policy[] = {
289 [DP_GENL_A_DP_IDX] = { .type = NL_A_U32 },
290 [DP_GENL_A_MC_GROUP] = { .type = NL_A_U32 },
293 /* Looks up the Netlink multicast group used by datapath 'dp_idx'. If
294 * successful, stores the multicast group in '*multicast_group' and returns 0.
295 * Otherwise, returns a positve errno value. */
297 lookup_openflow_multicast_group(int dp_idx, int *multicast_group)
299 struct nl_sock *sock;
300 struct buffer request, *reply;
301 struct nlattr *attrs[ARRAY_SIZE(openflow_multicast_policy)];
304 retval = nl_sock_create(NETLINK_GENERIC, 0, 0, 0, &sock);
308 buffer_init(&request, 0);
309 nl_msg_put_genlmsghdr(&request, sock, 0, openflow_family, NLM_F_REQUEST,
310 DP_GENL_C_QUERY_DP, 1);
311 nl_msg_put_u32(&request, DP_GENL_A_DP_IDX, dp_idx);
312 retval = nl_sock_transact(sock, &request, &reply);
313 buffer_uninit(&request);
315 nl_sock_destroy(sock);
318 if (!nl_policy_parse(reply, openflow_multicast_policy, attrs,
319 ARRAY_SIZE(openflow_multicast_policy))) {
320 nl_sock_destroy(sock);
321 buffer_delete(reply);
324 *multicast_group = nl_attr_get_u32(attrs[DP_GENL_A_MC_GROUP]);
325 nl_sock_destroy(sock);
326 buffer_delete(reply);
331 /* Sends the given 'command' to datapath 'dp'. If 'netdev' is nonnull, adds it
332 * to the command as the port name attribute. Returns 0 if successful,
333 * otherwise a positive errno value. */
335 send_mgmt_command(struct dpif *dp, int command, const char *netdev)
337 struct buffer request, *reply;
340 buffer_init(&request, 0);
341 nl_msg_put_genlmsghdr(&request, dp->sock, 32, openflow_family,
342 NLM_F_REQUEST | NLM_F_ACK, command, 1);
343 nl_msg_put_u32(&request, DP_GENL_A_DP_IDX, dp->dp_idx);
345 nl_msg_put_string(&request, DP_GENL_A_PORTNAME, netdev);
347 retval = nl_sock_transact(dp->sock, &request, &reply);
348 buffer_uninit(&request);
349 buffer_delete(reply);