Update copyright on all non-GPL files
[sliver-openvswitch.git] / lib / vconn.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 "vconn.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <netinet/in.h>
38 #include <poll.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include "buffer.h"
42 #include "flow.h"
43 #include "openflow.h"
44 #include "poll-loop.h"
45 #include "util.h"
46
47 #define THIS_MODULE VLM_vconn
48 #include "vlog.h"
49
50 static struct vconn_class *vconn_classes[] = {
51     &tcp_vconn_class,
52     &ptcp_vconn_class,
53 #ifdef HAVE_NETLINK
54     &netlink_vconn_class,
55 #endif
56 #ifdef HAVE_OPENSSL
57     &ssl_vconn_class,
58     &pssl_vconn_class,
59 #endif
60 };
61
62 /* Check the validity of the vconn class structures. */
63 static void
64 check_vconn_classes(void)
65 {
66 #ifndef NDEBUG
67     size_t i;
68
69     for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
70         struct vconn_class *class = vconn_classes[i];
71         assert(class->name != NULL);
72         assert(class->open != NULL);
73         assert(class->close != NULL);
74         assert(class->accept
75                ? !class->recv && !class->send
76                :  class->recv && class->send);
77         assert(class->wait != NULL);
78     }
79 #endif
80 }
81
82 /* Attempts to connect to an OpenFlow device.  'name' is a connection name in
83  * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
84  * vconn class-specific.
85  *
86  * Returns 0 if successful, otherwise a positive errno value.  If successful,
87  * stores a pointer to the new connection in '*vconnp', otherwise a null
88  * pointer.  */
89 int
90 vconn_open(const char *name, struct vconn **vconnp)
91 {
92     size_t prefix_len;
93     size_t i;
94
95     check_vconn_classes();
96
97     prefix_len = strcspn(name, ":");
98     if (prefix_len == strlen(name)) {
99         fatal(0, "`%s' not correct format for peer name", name);
100     }
101     for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
102         struct vconn_class *class = vconn_classes[i];
103         if (strlen(class->name) == prefix_len
104             && !memcmp(class->name, name, prefix_len)) {
105             char *suffix_copy = xstrdup(name + prefix_len + 1);
106             int retval = class->open(name, suffix_copy, vconnp);
107             free(suffix_copy);
108             if (retval) {
109                 *vconnp = NULL;
110             } else {
111                 assert((*vconnp)->connect_status != EAGAIN
112                        || (*vconnp)->class->connect);
113             }
114             return retval;
115         }
116     }
117     fatal(0, "unknown peer type `%.*s'", (int) prefix_len, name);
118     abort();
119 }
120
121 int
122 vconn_open_block(const char *name, struct vconn **vconnp)
123 {
124     struct vconn *vconn;
125     int error;
126
127     error = vconn_open(name, &vconn);
128     while (error == EAGAIN) {
129         vconn_connect_wait(vconn);
130         poll_block();
131         error = vconn_connect(vconn);
132         assert(error != EINPROGRESS);
133     }
134     if (error) {
135         vconn_close(vconn);
136         *vconnp = NULL;
137     } else {
138         *vconnp = vconn;
139     }
140     return error;
141 }
142
143 /* Closes 'vconn'. */
144 void
145 vconn_close(struct vconn *vconn)
146 {
147     if (vconn != NULL) {
148         (vconn->class->close)(vconn);
149     }
150 }
151
152 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
153  * wait for connections to arrive, not to transfer data.  Returns false if
154  * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
155  * to wait for new connections to arrive. */
156 bool
157 vconn_is_passive(const struct vconn *vconn)
158 {
159     return vconn->class->accept != NULL;
160 }
161
162 /* Tries to complete the connection on 'vconn', which must be an active
163  * vconn.  If 'vconn''s connection is complete, returns 0 if the connection
164  * was successful or a positive errno value if it failed.  If the
165  * connection is still in progress, returns EAGAIN. */
166 int
167 vconn_connect(struct vconn *vconn)
168 {
169     if (vconn->connect_status == EAGAIN) {
170         vconn->connect_status = (vconn->class->connect)(vconn);
171         assert(vconn->connect_status != EINPROGRESS);
172     }
173     return vconn->connect_status;
174 }
175
176 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
177  * If successful, stores the new connection in '*new_vconn' and returns 0.
178  * Otherwise, returns a positive errno value.
179  *
180  * vconn_accept will not block waiting for a connection.  If no connection is
181  * ready to be accepted, it returns EAGAIN immediately. */
182 int
183 vconn_accept(struct vconn *vconn, struct vconn **new_vconn)
184 {
185     int retval;
186
187     retval = (vconn->class->accept)(vconn, new_vconn);
188
189     if (retval) {
190         *new_vconn = NULL;
191     } else {
192         assert((*new_vconn)->connect_status != EAGAIN
193                || (*new_vconn)->class->connect);
194     }
195     return retval;
196 }
197
198 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
199  * vconn.  If successful, stores the received message into '*msgp' and returns
200  * 0.  The caller is responsible for destroying the message with
201  * buffer_delete().  On failure, returns a positive errno value and stores a
202  * null pointer into '*msgp'.  On normal connection close, returns EOF.
203  *
204  * vconn_recv will not block waiting for a packet to arrive.  If no packets
205  * have been received, it returns EAGAIN immediately. */
206 int
207 vconn_recv(struct vconn *vconn, struct buffer **msgp)
208 {
209     int retval = vconn_connect(vconn);
210     if (!retval) {
211         retval = (vconn->class->recv)(vconn, msgp);
212     }
213     if (retval) {
214         *msgp = NULL;
215     }
216     return retval;
217 }
218
219 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
220  * vconn.  If successful, returns 0, in which case ownership of 'msg' is
221  * transferred to the vconn.  Success does not guarantee that 'msg' has been or
222  * ever will be delivered to the peer, only that it has been queued for
223  * transmission.
224  *
225  * Returns a positive errno value on failure, in which case the caller
226  * retains ownership of 'msg'.
227  *
228  * vconn_send will not block.  If 'msg' cannot be immediately accepted for
229  * transmission, it returns EAGAIN immediately. */
230 int
231 vconn_send(struct vconn *vconn, struct buffer *msg)
232 {
233     int retval = vconn_connect(vconn);
234     if (!retval) {
235         retval = (vconn->class->send)(vconn, msg);
236     }
237     return retval;
238 }
239
240 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
241 int
242 vconn_send_block(struct vconn *vconn, struct buffer *msg)
243 {
244     int retval;
245     while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
246         vconn_send_wait(vconn);
247         poll_block();
248     }
249     return retval;
250 }
251
252 void
253 vconn_wait(struct vconn *vconn, enum vconn_wait_type wait)
254 {
255     int connect_status;
256
257     assert(vconn_is_passive(vconn)
258            ? wait == WAIT_ACCEPT || wait == WAIT_CONNECT
259            : wait == WAIT_CONNECT || wait == WAIT_RECV || wait == WAIT_SEND);
260
261     connect_status = vconn_connect(vconn);
262     if (connect_status) {
263         if (connect_status == EAGAIN) {
264             wait = WAIT_CONNECT;
265         } else {
266             poll_immediate_wake();
267             return;
268         }
269     }
270
271     (vconn->class->wait)(vconn, wait);
272 }
273
274 void
275 vconn_connect_wait(struct vconn *vconn)
276 {
277     vconn_wait(vconn, WAIT_CONNECT);
278 }
279
280 void
281 vconn_accept_wait(struct vconn *vconn)
282 {
283     vconn_wait(vconn, WAIT_ACCEPT);
284 }
285
286 void
287 vconn_recv_wait(struct vconn *vconn)
288 {
289     vconn_wait(vconn, WAIT_RECV);
290 }
291
292 void
293 vconn_send_wait(struct vconn *vconn)
294 {
295     vconn_wait(vconn, WAIT_SEND);
296 }
297
298 struct buffer *
299 make_add_simple_flow(const struct flow *flow,
300                      uint32_t buffer_id, uint16_t out_port)
301 {
302     struct ofp_flow_mod *ofm;
303     size_t size = sizeof *ofm + sizeof ofm->actions[0];
304     struct buffer *out = buffer_new(size);
305     ofm = buffer_put_uninit(out, size);
306     memset(ofm, 0, size);
307     ofm->header.version = OFP_VERSION;
308     ofm->header.type = OFPT_FLOW_MOD;
309     ofm->header.length = htons(size);
310     ofm->match.wildcards = htons(0);
311     ofm->match.in_port = flow->in_port;
312     memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
313     memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
314     ofm->match.dl_vlan = flow->dl_vlan;
315     ofm->match.dl_type = flow->dl_type;
316     ofm->match.nw_src = flow->nw_src;
317     ofm->match.nw_dst = flow->nw_dst;
318     ofm->match.nw_proto = flow->nw_proto;
319     ofm->match.tp_src = flow->tp_src;
320     ofm->match.tp_dst = flow->tp_dst;
321     ofm->command = htons(OFPFC_ADD);
322     ofm->max_idle = htons(60);
323     ofm->buffer_id = htonl(buffer_id);
324     ofm->group_id = htonl(0);
325     ofm->actions[0].type = htons(OFPAT_OUTPUT);
326     ofm->actions[0].arg.output.max_len = htons(0);
327     ofm->actions[0].arg.output.port = htons(out_port);
328     return out;
329 }
330
331 struct buffer *
332 make_unbuffered_packet_out(const struct buffer *packet,
333                            uint16_t in_port, uint16_t out_port)
334 {
335     struct ofp_packet_out *opo;
336     size_t size = sizeof *opo + packet->size;
337     struct buffer *out = buffer_new(size);
338     opo = buffer_put_uninit(out, size);
339     memset(opo, 0, sizeof *opo);
340     opo->header.version = OFP_VERSION;
341     opo->header.type = OFPT_PACKET_OUT;
342     opo->header.length = htons(size);
343     opo->buffer_id = htonl(UINT32_MAX);
344     opo->in_port = htons(in_port);
345     opo->out_port = htons(out_port);
346     memcpy(opo->u.data, packet->data, packet->size);
347     return out;
348 }
349
350 struct buffer *
351 make_buffered_packet_out(uint32_t buffer_id,
352                          uint16_t in_port, uint16_t out_port)
353 {
354     struct ofp_packet_out *opo;
355     size_t size = sizeof *opo + sizeof opo->u.actions[0];
356     struct buffer *out = buffer_new(size);
357     opo = buffer_put_uninit(out, size);
358     memset(opo, 0, size);
359     opo->header.version = OFP_VERSION;
360     opo->header.type = OFPT_PACKET_OUT;
361     opo->header.length = htons(size);
362     opo->buffer_id = htonl(buffer_id);
363     opo->in_port = htons(in_port);
364     opo->out_port = htons(out_port);
365     opo->u.actions[0].type = htons(OFPAT_OUTPUT);
366     opo->u.actions[0].arg.output.max_len = htons(0);
367     opo->u.actions[0].arg.output.port = htons(out_port);
368     return out;
369 }
370