1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include <netinet/in.h>
32 #include "poll-loop.h"
35 #define THIS_MODULE VLM_vconn
38 static struct vconn_class *vconn_classes[] = {
50 /* Check the validity of the vconn class structures. */
52 check_vconn_classes(void)
57 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
58 struct vconn_class *class = vconn_classes[i];
59 assert(class->name != NULL);
60 assert(class->open != NULL);
61 assert(class->close != NULL);
63 ? !class->recv && !class->send
64 : class->recv && class->send);
65 assert(class->wait != NULL);
70 /* Attempts to connect to an OpenFlow device. 'name' is a connection name in
71 * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
72 * vconn class-specific.
74 * Returns 0 if successful, otherwise a positive errno value. If successful,
75 * stores a pointer to the new connection in '*vconnp', otherwise a null
78 vconn_open(const char *name, struct vconn **vconnp)
83 check_vconn_classes();
85 prefix_len = strcspn(name, ":");
86 if (prefix_len == strlen(name)) {
87 fatal(0, "`%s' not correct format for peer name", name);
89 for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
90 struct vconn_class *class = vconn_classes[i];
91 if (strlen(class->name) == prefix_len
92 && !memcmp(class->name, name, prefix_len)) {
93 char *suffix_copy = xstrdup(name + prefix_len + 1);
94 int retval = class->open(name, suffix_copy, vconnp);
99 assert((*vconnp)->connect_status != EAGAIN
100 || (*vconnp)->class->connect);
105 fatal(0, "unknown peer type `%.*s'", (int) prefix_len, name);
110 vconn_open_block(const char *name, struct vconn **vconnp)
115 error = vconn_open(name, &vconn);
116 while (error == EAGAIN) {
117 vconn_connect_wait(vconn);
119 error = vconn_connect(vconn);
120 assert(error != EINPROGRESS);
131 /* Closes 'vconn'. */
133 vconn_close(struct vconn *vconn)
136 (vconn->class->close)(vconn);
140 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
141 * wait for connections to arrive, not to transfer data. Returns false if
142 * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
143 * to wait for new connections to arrive. */
145 vconn_is_passive(const struct vconn *vconn)
147 return vconn->class->accept != NULL;
150 /* Tries to complete the connection on 'vconn', which must be an active
151 * vconn. If 'vconn''s connection is complete, returns 0 if the connection
152 * was successful or a positive errno value if it failed. If the
153 * connection is still in progress, returns EAGAIN. */
155 vconn_connect(struct vconn *vconn)
157 if (vconn->connect_status == EAGAIN) {
158 vconn->connect_status = (vconn->class->connect)(vconn);
159 assert(vconn->connect_status != EINPROGRESS);
161 return vconn->connect_status;
164 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
165 * If successful, stores the new connection in '*new_vconn' and returns 0.
166 * Otherwise, returns a positive errno value.
168 * vconn_accept will not block waiting for a connection. If no connection is
169 * ready to be accepted, it returns EAGAIN immediately. */
171 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
175 retval = (vconn->class->accept)(vconn, new_vconn);
180 assert((*new_vconn)->connect_status != EAGAIN
181 || (*new_vconn)->class->connect);
186 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
187 * vconn. If successful, stores the received message into '*msgp' and returns
188 * 0. The caller is responsible for destroying the message with
189 * buffer_delete(). On failure, returns a positive errno value and stores a
190 * null pointer into '*msgp'. On normal connection close, returns EOF.
192 * vconn_recv will not block waiting for a packet to arrive. If no packets
193 * have been received, it returns EAGAIN immediately. */
195 vconn_recv(struct vconn *vconn, struct buffer **msgp)
197 int retval = vconn_connect(vconn);
199 retval = (vconn->class->recv)(vconn, msgp);
207 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
208 * vconn. If successful, returns 0, in which case ownership of 'msg' is
209 * transferred to the vconn. Success does not guarantee that 'msg' has been or
210 * ever will be delivered to the peer, only that it has been queued for
213 * Returns a positive errno value on failure, in which case the caller
214 * retains ownership of 'msg'.
216 * vconn_send will not block. If 'msg' cannot be immediately accepted for
217 * transmission, it returns EAGAIN immediately. */
219 vconn_send(struct vconn *vconn, struct buffer *msg)
221 int retval = vconn_connect(vconn);
223 retval = (vconn->class->send)(vconn, msg);
228 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
230 vconn_send_block(struct vconn *vconn, struct buffer *msg)
233 while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
234 vconn_send_wait(vconn);
241 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
245 assert(vconn_is_passive(vconn)
246 ? wait == WAIT_ACCEPT || wait == WAIT_CONNECT
247 : wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
249 connect_status = vconn_connect(vconn);
250 if (connect_status) {
251 if (connect_status == EAGAIN) {
254 poll_immediate_wake();
259 (vconn->class->wait)(vconn, wait);
263 vconn_connect_wait(struct vconn *vconn)
265 vconn_wait(vconn, WAIT_CONNECT);
269 vconn_accept_wait(struct vconn *vconn)
271 vconn_wait(vconn, WAIT_ACCEPT);
275 vconn_recv_wait(struct vconn *vconn)
277 vconn_wait(vconn, WAIT_RECV);
281 vconn_send_wait(struct vconn *vconn)
283 vconn_wait(vconn, WAIT_SEND);
287 make_add_simple_flow(const struct flow *flow,
288 uint32_t buffer_id, uint16_t out_port)
290 struct ofp_flow_mod *ofm;
291 size_t size = sizeof *ofm + sizeof ofm->actions[0];
292 struct buffer *out = buffer_new(size);
293 ofm = buffer_put_uninit(out, size);
294 memset(ofm, 0, size);
295 ofm->header.version = OFP_VERSION;
296 ofm->header.type = OFPT_FLOW_MOD;
297 ofm->header.length = htons(size);
298 ofm->match.wildcards = htons(0);
299 ofm->match.in_port = flow->in_port;
300 memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
301 memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
302 ofm->match.dl_vlan = flow->dl_vlan;
303 ofm->match.dl_type = flow->dl_type;
304 ofm->match.nw_src = flow->nw_src;
305 ofm->match.nw_dst = flow->nw_dst;
306 ofm->match.nw_proto = flow->nw_proto;
307 ofm->match.tp_src = flow->tp_src;
308 ofm->match.tp_dst = flow->tp_dst;
309 ofm->command = htons(OFPFC_ADD);
310 ofm->max_idle = htons(60);
311 ofm->buffer_id = htonl(buffer_id);
312 ofm->group_id = htonl(0);
313 ofm->actions[0].type = htons(OFPAT_OUTPUT);
314 ofm->actions[0].arg.output.max_len = htons(0);
315 ofm->actions[0].arg.output.port = htons(out_port);
320 make_unbuffered_packet_out(const struct buffer *packet,
321 uint16_t in_port, uint16_t out_port)
323 struct ofp_packet_out *opo;
324 size_t size = sizeof *opo + packet->size;
325 struct buffer *out = buffer_new(size);
326 opo = buffer_put_uninit(out, size);
327 memset(opo, 0, sizeof *opo);
328 opo->header.version = OFP_VERSION;
329 opo->header.type = OFPT_PACKET_OUT;
330 opo->header.length = htons(size);
331 opo->buffer_id = htonl(UINT32_MAX);
332 opo->in_port = htons(in_port);
333 opo->out_port = htons(out_port);
334 memcpy(opo->u.data, packet->data, packet->size);
339 make_buffered_packet_out(uint32_t buffer_id,
340 uint16_t in_port, uint16_t out_port)
342 struct ofp_packet_out *opo;
343 size_t size = sizeof *opo + sizeof opo->u.actions[0];
344 struct buffer *out = buffer_new(size);
345 opo = buffer_put_uninit(out, size);
346 memset(opo, 0, size);
347 opo->header.version = OFP_VERSION;
348 opo->header.type = OFPT_PACKET_OUT;
349 opo->header.length = htons(size);
350 opo->buffer_id = htonl(buffer_id);
351 opo->in_port = htons(in_port);
352 opo->out_port = htons(out_port);
353 opo->u.actions[0].type = htons(OFPAT_OUTPUT);
354 opo->u.actions[0].arg.output.max_len = htons(0);
355 opo->u.actions[0].arg.output.port = htons(out_port);