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