Support SSL in secchan and controller.
[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 "util.h"
33
34 static struct vconn_class *vconn_classes[] = {
35     &tcp_vconn_class,
36     &ptcp_vconn_class,
37 #ifdef HAVE_NETLINK
38     &netlink_vconn_class,
39 #endif
40 #ifdef HAVE_OPENSSL
41     &ssl_vconn_class,
42     &pssl_vconn_class,
43 #endif
44 };
45
46 /* Check the validity of the vconn class structures. */
47 static void
48 check_vconn_classes(void) 
49 {
50 #ifndef NDEBUG
51     size_t i;
52
53     for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
54         struct vconn_class *class = vconn_classes[i];
55         assert(class->name != NULL);
56         assert(class->open != NULL);
57         assert(class->close != NULL);
58         assert(class->prepoll != NULL);
59         assert(class->accept
60                ? !class->recv && !class->send
61                : class->recv && class->send);
62     }
63 #endif
64 }
65
66 /* Attempts to connect to an OpenFlow device.  'name' is a connection name in
67  * the form "TYPE:ARGS", where TYPE is the vconn class's name and ARGS are
68  * vconn class-specific.
69  *
70  * Returns 0 if successful, otherwise a positive errno value.  If successful,
71  * stores a pointer to the new connection in '*vconnp', otherwise a null
72  * pointer.  */
73 int
74 vconn_open(const char *name, struct vconn **vconnp) 
75 {
76     size_t prefix_len;
77     size_t i;
78
79     check_vconn_classes();
80
81     prefix_len = strcspn(name, ":");
82     if (prefix_len == strlen(name)) {
83         fatal(0, "`%s' not correct format for peer name", name);
84     }
85     for (i = 0; i < ARRAY_SIZE(vconn_classes); i++) {
86         struct vconn_class *class = vconn_classes[i];
87         if (strlen(class->name) == prefix_len
88             && !memcmp(class->name, name, prefix_len)) {
89             char *suffix_copy = xstrdup(name + prefix_len + 1);
90             int retval = class->open(name, suffix_copy, vconnp);
91             free(suffix_copy);
92             if (retval) {
93                 *vconnp = NULL;
94             }
95             return retval;
96         }
97     }
98     fatal(0, "unknown peer type `%.*s'", (int) prefix_len, name);
99     abort();
100 }
101
102 /* Closes 'vconn'. */
103 void
104 vconn_close(struct vconn *vconn) 
105 {
106     if (vconn != NULL) {
107         (vconn->class->close)(vconn);
108     }
109 }
110
111 /* Returns true if 'vconn' is a passive vconn, that is, its purpose is to
112  * wait for connections to arrive, not to transfer data.  Returns false if
113  * 'vconn' is an active vconn, that is, its purpose is to transfer data, not
114  * to wait for new connections to arrive. */
115 bool
116 vconn_is_passive(const struct vconn *vconn) 
117 {
118     return vconn->class->accept != NULL;
119 }
120
121 /* Initializes 'pfd->fd' and 'pfd->events' appropriately so that poll() will
122  * wake up when the connection becomes available for the operations specified
123  * in 'want', or for performing the vconn's needed internal processing.
124  *
125  * Normally returns false.  Returns true to indicate that no blocking should
126  * happen in poll() because the connection is available for some operation
127  * specified in 'want' but that status cannot be detected via poll() and thus
128  * poll() could block forever otherwise. */
129 bool
130 vconn_prepoll(struct vconn *vconn, int want, struct pollfd *pollfd)
131 {
132     return (vconn->class->prepoll)(vconn, want, pollfd);
133 }
134
135 /* Perform any internal processing needed by the connections.  The vconn file
136  * descriptor's status, as reported by poll(), must be provided in '*revents'.
137  *
138  * The postpoll function adjusts '*revents' to reflect the status of the
139  * connection from the caller's point of view.  That is, upon return '*revents
140  * & POLLIN' indicates that a packet is (potentially) ready to be read (for an
141  * active vconn) or a new connection is ready to be accepted (for a passive
142  * vconn) and '*revents & POLLOUT' indicates that a packet is (potentially)
143  * ready to be written. */
144 void
145 vconn_postpoll(struct vconn *vconn, short int *revents) 
146 {
147     if (vconn->class->postpoll) {
148         (vconn->class->postpoll)(vconn, revents);
149     } 
150 }
151
152 /* Tries to accept a new connection on 'vconn', which must be a passive vconn.
153  * If successful, stores the new connection in '*new_vconn' and returns 0.
154  * Otherwise, returns a positive errno value.
155  *
156  * vconn_accept will not block waiting for a connection.  If no connection is
157  * ready to be accepted, it returns EAGAIN immediately. */
158 int
159 vconn_accept(struct vconn *vconn, struct vconn **new_vconn) 
160 {
161     int retval = (vconn->class->accept)(vconn, new_vconn);
162     if (retval) {
163         *new_vconn = NULL;
164     }
165     return retval;
166 }
167
168 /* Tries to receive an OpenFlow message from 'vconn', which must be an active
169  * vconn.  If successful, stores the received message into '*msgp' and returns
170  * 0.  The caller is responsible for destroying the message with
171  * buffer_delete().  On failure, returns a positive errno value and stores a
172  * null pointer into '*msgp'.  On normal connection close, returns EOF.
173  *
174  * vconn_recv will not block waiting for a packet to arrive.  If no packets
175  * have been received, it returns EAGAIN immediately. */
176 int
177 vconn_recv(struct vconn *vconn, struct buffer **msgp) 
178 {
179     int retval = (vconn->class->recv)(vconn, msgp);
180     if (retval) {
181         *msgp = NULL;
182     }
183     return retval;
184 }
185
186 /* Tries to queue 'msg' for transmission on 'vconn', which must be an active
187  * vconn.  If successful, returns 0, in which case ownership of 'msg' is
188  * transferred to the vconn.  Success does not guarantee that 'msg' has been or
189  * ever will be delivered to the peer, only that it has been queued for
190  * transmission.
191  *
192  * Returns a positive errno value on failure, in which case the caller
193  * retains ownership of 'msg'.
194  *
195  * vconn_send will not block.  If 'msg' cannot be immediately accepted for
196  * transmission, it returns EAGAIN immediately. */
197 int
198 vconn_send(struct vconn *vconn, struct buffer *msg) 
199 {
200     return (vconn->class->send)(vconn, msg);
201 }
202
203 /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
204 int
205 vconn_send_wait(struct vconn *vconn, struct buffer *msg) 
206 {
207     int retval;
208     while ((retval = vconn_send(vconn, msg)) == EAGAIN) {
209         struct pollfd pfd;
210
211         pfd.fd = -1;
212         pfd.events = 0;
213         vconn_prepoll(vconn, WANT_SEND, &pfd);
214         do {
215             retval = poll(&pfd, 1, -1);
216         } while (retval < 0 && errno == EINTR);
217         if (retval < 0) {
218             return errno;
219         }
220         assert(retval == 1);
221         vconn_postpoll(vconn, &pfd.revents);
222     }
223     return retval;
224 }
225
226 struct buffer *
227 make_add_simple_flow(const struct flow *flow,
228                      uint32_t buffer_id, uint16_t out_port) 
229 {
230     struct ofp_flow_mod *ofm;
231     size_t size = sizeof *ofm + sizeof ofm->actions[0];
232     struct buffer *out = buffer_new(size);
233     ofm = buffer_put_uninit(out, size);
234     memset(ofm, 0, size);
235     ofm->header.version = OFP_VERSION;
236     ofm->header.type = OFPT_FLOW_MOD;
237     ofm->header.length = htons(size);
238     ofm->match.wildcards = htons(0);
239     ofm->match.in_port = flow->in_port;
240     memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
241     memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
242     ofm->match.dl_vlan = flow->dl_vlan;
243     ofm->match.dl_type = flow->dl_type;
244     ofm->match.nw_src = flow->nw_src;
245     ofm->match.nw_dst = flow->nw_dst;
246     ofm->match.nw_proto = flow->nw_proto;
247     ofm->match.tp_src = flow->tp_src;
248     ofm->match.tp_dst = flow->tp_dst;
249     ofm->command = htons(OFPFC_ADD);
250     ofm->max_idle = htons(60);
251     ofm->buffer_id = htonl(buffer_id);
252     ofm->group_id = htonl(0);
253     ofm->actions[0].type = htons(OFPAT_OUTPUT);
254     ofm->actions[0].arg.output.max_len = htons(0);
255     ofm->actions[0].arg.output.port = htons(out_port);
256     return out;
257 }
258
259 struct buffer *
260 make_unbuffered_packet_out(const struct buffer *packet,
261                            uint16_t in_port, uint16_t out_port)
262 {
263     struct ofp_packet_out *opo;
264     size_t size = sizeof *opo + packet->size;
265     struct buffer *out = buffer_new(size);
266     opo = buffer_put_uninit(out, size);
267     memset(opo, 0, sizeof *opo);
268     opo->header.version = OFP_VERSION;
269     opo->header.type = OFPT_PACKET_OUT;
270     opo->header.length = htons(size);
271     opo->buffer_id = htonl(UINT32_MAX);
272     opo->in_port = htons(in_port);
273     opo->out_port = htons(out_port);
274     memcpy(opo->u.data, packet->data, packet->size);
275     return out;
276 }
277
278 struct buffer *
279 make_buffered_packet_out(uint32_t buffer_id,
280                          uint16_t in_port, uint16_t out_port)
281 {
282     struct ofp_packet_out *opo;
283     size_t size = sizeof *opo + sizeof opo->u.actions[0];
284     struct buffer *out = buffer_new(size);
285     opo = buffer_put_uninit(out, size);
286     memset(opo, 0, size);
287     opo->header.version = OFP_VERSION;
288     opo->header.type = OFPT_PACKET_OUT;
289     opo->header.length = htons(size);
290     opo->buffer_id = htonl(buffer_id);
291     opo->in_port = htons(in_port);
292     opo->out_port = htons(out_port);
293     opo->u.actions[0].type = htons(OFPAT_OUTPUT);
294     opo->u.actions[0].arg.output.max_len = htons(0);
295     opo->u.actions[0].arg.output.port = htons(out_port);
296     return out;
297 }
298