Add the ability to connect to a vconn asynchronously.
[sliver-openvswitch.git] / lib / vconn.c
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
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:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
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
19  * IN THE SOFTWARE.
20  */
21
22 #include "vconn.h"
23 #include <assert.h>
24 #include <errno.h>
25 #include <netinet/in.h>
26 #include <poll.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "buffer.h"
30 #include "flow.h"
31 #include "openflow.h"
32 #include "poll-loop.h"
33 #include "util.h"
34
35 #define THIS_MODULE VLM_vconn
36 #include "vlog.h"
37
38 static struct vconn_class *vconn_classes[] = {
39     &tcp_vconn_class,
40     &ptcp_vconn_class,
41 #ifdef HAVE_NETLINK
42     &netlink_vconn_class,
43 #endif
44 #ifdef HAVE_OPENSSL
45     &ssl_vconn_class,
46     &pssl_vconn_class,
47 #endif
48 };
49
50 /* Check the validity of the vconn class structures. */
51 static void
52 check_vconn_classes(void)
53 {
54 #ifndef NDEBUG
55     size_t i;
56
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);
62         assert(class->accept
63                ? !class->recv && !class->send
64                :  class->recv && class->send);
65         assert(class->wait != NULL);
66     }
67 #endif
68 }
69
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.
73  *
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
76  * pointer.  */
77 int
78 vconn_open(const char *name, struct vconn **vconnp)
79 {
80     size_t prefix_len;
81     size_t i;
82
83     check_vconn_classes();
84
85     prefix_len = strcspn(name, ":");
86     if (prefix_len == strlen(name)) {
87         fatal(0, "`%s' not correct format for peer name", name);
88     }
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);
95             free(suffix_copy);
96             if (retval) {
97                 *vconnp = NULL;
98             } else {
99                 assert((*vconnp)->connect_status != EAGAIN
100                        || (*vconnp)->class->connect);
101             }
102             return retval;
103         }
104     }
105     fatal(0, "unknown peer type `%.*s'", (int) prefix_len, name);
106     abort();
107 }
108
109 int
110 vconn_open_block(const char *name, struct vconn **vconnp)
111 {
112     struct vconn *vconn;
113     int error;
114
115     error = vconn_open(name, &vconn);
116     while (error == EAGAIN) {
117         vconn_connect_wait(vconn);
118         poll_block();
119         error = vconn_connect(vconn);
120         assert(error != EINPROGRESS);
121     }
122     if (error) {
123         vconn_close(vconn);
124         *vconnp = NULL;
125     } else {
126         *vconnp = vconn;
127     }
128     return error;
129 }
130
131 /* Closes 'vconn'. */
132 void
133 vconn_close(struct vconn *vconn)
134 {
135     if (vconn != NULL) {
136         (vconn->class->close)(vconn);
137     }
138 }
139
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. */
144 bool
145 vconn_is_passive(const struct vconn *vconn)
146 {
147     return vconn->class->accept != NULL;
148 }
149
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. */
154 int
155 vconn_connect(struct vconn *vconn)
156 {
157     if (vconn->connect_status == EAGAIN) {
158         vconn->connect_status = (vconn->class->connect)(vconn);
159         assert(vconn->connect_status != EINPROGRESS);
160     }
161     return vconn->connect_status;
162 }
163
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.
167  *
168  * vconn_accept will not block waiting for a connection.  If no connection is
169  * ready to be accepted, it returns EAGAIN immediately. */
170 int
171 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
172 {
173     int retval;
174
175     retval = (vconn->class->accept)(vconn, new_vconn);
176
177     if (retval) {
178         *new_vconn = NULL;
179     } else {
180         assert((*new_vconn)->connect_status != EAGAIN
181                || (*new_vconn)->class->connect);
182     }
183     return retval;
184 }
185
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.
191  *
192  * vconn_recv will not block waiting for a packet to arrive.  If no packets
193  * have been received, it returns EAGAIN immediately. */
194 int
195 vconn_recv(struct vconn *vconn, struct buffer **msgp)
196 {
197     int retval = vconn_connect(vconn);
198     if (!retval) {
199         retval = (vconn->class->recv)(vconn, msgp);
200     }
201     if (retval) {
202         *msgp = NULL;
203     }
204     return retval;
205 }
206
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
211  * transmission.
212  *
213  * Returns a positive errno value on failure, in which case the caller
214  * retains ownership of 'msg'.
215  *
216  * vconn_send will not block.  If 'msg' cannot be immediately accepted for
217  * transmission, it returns EAGAIN immediately. */
218 int
219 vconn_send(struct vconn *vconn, struct buffer *msg)
220 {
221     int retval = vconn_connect(vconn);
222     if (!retval) {
223         retval = (vconn->class->send)(vconn, msg);
224     }
225     return retval;
226 }
227
228 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
229 int
230 vconn_send_block(struct vconn *vconn, struct buffer *msg)
231 {
232     int retval;
233     while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
234         vconn_send_wait(vconn);
235         poll_block();
236     }
237     return retval;
238 }
239
240 void
241 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
242 {
243     int connect_status;
244
245     assert(vconn_is_passive(vconn)
246            ? wait == WAIT_ACCEPT || wait == WAIT_CONNECT
247            : wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
248
249     connect_status = vconn_connect(vconn);
250     if (connect_status) {
251         if (connect_status == EAGAIN) {
252             wait = WAIT_CONNECT;
253         } else {
254             poll_immediate_wake();
255             return;
256         }
257     }
258
259     (vconn->class->wait)(vconn, wait);
260 }
261
262 void
263 vconn_connect_wait(struct vconn *vconn)
264 {
265     vconn_wait(vconn, WAIT_CONNECT);
266 }
267
268 void
269 vconn_accept_wait(struct vconn *vconn)
270 {
271     vconn_wait(vconn, WAIT_ACCEPT);
272 }
273
274 void
275 vconn_recv_wait(struct vconn *vconn)
276 {
277     vconn_wait(vconn, WAIT_RECV);
278 }
279
280 void
281 vconn_send_wait(struct vconn *vconn)
282 {
283     vconn_wait(vconn, WAIT_SEND);
284 }
285
286 struct buffer *
287 make_add_simple_flow(const struct flow *flow,
288                      uint32_t buffer_id, uint16_t out_port)
289 {
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);
316     return out;
317 }
318
319 struct buffer *
320 make_unbuffered_packet_out(const struct buffer *packet,
321                            uint16_t in_port, uint16_t out_port)
322 {
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);
335     return out;
336 }
337
338 struct buffer *
339 make_buffered_packet_out(uint32_t buffer_id,
340                          uint16_t in_port, uint16_t out_port)
341 {
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);
356     return out;
357 }
358