28d96354945829260511d97e146d39022dca545d
[sliver-openvswitch.git] / lib / vconn-stream.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 <config.h>
35 #include "vconn-stream.h"
36 #include <assert.h>
37 #include <errno.h>
38 #include <poll.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include "buffer.h"
44 #include "util.h"
45 #include "openflow.h"
46 #include "poll-loop.h"
47 #include "socket-util.h"
48 #include "vconn.h"
49
50 #include "vlog.h"
51 #define THIS_MODULE VLM_vconn_stream
52
53 /* Active stream socket vconn. */
54
55 struct stream_vconn
56 {
57     struct vconn vconn;
58     int fd;
59     struct buffer *rxbuf;
60     struct buffer *txbuf;
61     struct poll_waiter *tx_waiter;
62 };
63
64 static struct vconn_class stream_vconn_class;
65
66 int
67 new_stream_vconn(const char *name, int fd, int connect_status,
68                  uint32_t ip, struct vconn **vconnp)
69 {
70     struct stream_vconn *s;
71
72     s = xmalloc(sizeof *s);
73     s->vconn.class = &stream_vconn_class;
74     s->vconn.connect_status = connect_status;
75     s->vconn.ip = ip;
76     s->fd = fd;
77     s->txbuf = NULL;
78     s->tx_waiter = NULL;
79     s->rxbuf = NULL;
80     *vconnp = &s->vconn;
81     return 0;
82 }
83
84 static struct stream_vconn *
85 stream_vconn_cast(struct vconn *vconn)
86 {
87     assert(vconn->class == &stream_vconn_class);
88     return CONTAINER_OF(vconn, struct stream_vconn, vconn);
89 }
90
91 static void
92 stream_close(struct vconn *vconn)
93 {
94     struct stream_vconn *s = stream_vconn_cast(vconn);
95     poll_cancel(s->tx_waiter);
96     close(s->fd);
97     free(s);
98 }
99
100 static int
101 stream_connect(struct vconn *vconn)
102 {
103     struct stream_vconn *s = stream_vconn_cast(vconn);
104     return check_connection_completion(s->fd);
105 }
106
107 static int
108 stream_recv(struct vconn *vconn, struct buffer **bufferp)
109 {
110     struct stream_vconn *s = stream_vconn_cast(vconn);
111     struct buffer *rx;
112     size_t want_bytes;
113     ssize_t retval;
114
115     if (s->rxbuf == NULL) {
116         s->rxbuf = buffer_new(1564);
117     }
118     rx = s->rxbuf;
119
120 again:
121     if (sizeof(struct ofp_header) > rx->size) {
122         want_bytes = sizeof(struct ofp_header) - rx->size;
123     } else {
124         struct ofp_header *oh = rx->data;
125         size_t length = ntohs(oh->length);
126         if (length < sizeof(struct ofp_header)) {
127             VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
128             return EPROTO;
129         }
130         want_bytes = length - rx->size;
131         if (!want_bytes) {
132             *bufferp = rx;
133             s->rxbuf = NULL;
134             return 0;
135         }
136     }
137     buffer_prealloc_tailroom(rx, want_bytes);
138
139     retval = read(s->fd, buffer_tail(rx), want_bytes);
140     if (retval > 0) {
141         rx->size += retval;
142         if (retval == want_bytes) {
143             if (rx->size > sizeof(struct ofp_header)) {
144                 *bufferp = rx;
145                 s->rxbuf = NULL;
146                 return 0;
147             } else {
148                 goto again;
149             }
150         }
151         return EAGAIN;
152     } else if (retval == 0) {
153         if (rx->size) {
154             VLOG_ERR("connection dropped mid-packet");
155             return EPROTO;
156         } else {
157             return EOF;
158         }
159     } else {
160         return retval ? errno : EAGAIN;
161     }
162 }
163
164 static void
165 stream_clear_txbuf(struct stream_vconn *s)
166 {
167     buffer_delete(s->txbuf);
168     s->txbuf = NULL;
169     s->tx_waiter = NULL;
170 }
171
172 static void
173 stream_do_tx(int fd UNUSED, short int revents UNUSED, void *vconn_)
174 {
175     struct vconn *vconn = vconn_;
176     struct stream_vconn *s = stream_vconn_cast(vconn);
177     ssize_t n = write(s->fd, s->txbuf->data, s->txbuf->size);
178     if (n < 0) {
179         if (errno != EAGAIN) {
180             VLOG_ERR("send: %s", strerror(errno));
181             stream_clear_txbuf(s);
182             return;
183         }
184     } else if (n > 0) {
185         buffer_pull(s->txbuf, n);
186         if (!s->txbuf->size) {
187             stream_clear_txbuf(s);
188             return;
189         }
190     }
191     s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
192 }
193
194 static int
195 stream_send(struct vconn *vconn, struct buffer *buffer)
196 {
197     struct stream_vconn *s = stream_vconn_cast(vconn);
198     ssize_t retval;
199
200     if (s->txbuf) {
201         return EAGAIN;
202     }
203
204     retval = write(s->fd, buffer->data, buffer->size);
205     if (retval == buffer->size) {
206         buffer_delete(buffer);
207         return 0;
208     } else if (retval >= 0 || errno == EAGAIN) {
209         s->txbuf = buffer;
210         if (retval > 0) {
211             buffer_pull(buffer, retval);
212         }
213         s->tx_waiter = poll_fd_callback(s->fd, POLLOUT, stream_do_tx, vconn);
214         return 0;
215     } else {
216         return errno;
217     }
218 }
219
220 static void
221 stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
222 {
223     struct stream_vconn *s = stream_vconn_cast(vconn);
224     switch (wait) {
225     case WAIT_CONNECT:
226         poll_fd_wait(s->fd, POLLOUT);
227         break;
228
229     case WAIT_SEND:
230         if (!s->txbuf) {
231             poll_fd_wait(s->fd, POLLOUT);
232         } else {
233             /* Nothing to do: need to drain txbuf first. */
234         }
235         break;
236
237     case WAIT_RECV:
238         poll_fd_wait(s->fd, POLLIN);
239         break;
240
241     default:
242         NOT_REACHED();
243     }
244 }
245
246 static struct vconn_class stream_vconn_class = {
247     .name = "stream",
248     .close = stream_close,
249     .connect = stream_connect,
250     .recv = stream_recv,
251     .send = stream_send,
252     .wait = stream_wait,
253 };
254 \f
255 /* Passive stream socket vconn. */
256
257 struct pstream_vconn
258 {
259     struct vconn vconn;
260     int fd;
261     int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
262                      struct vconn **);
263 };
264
265 static struct vconn_class pstream_vconn_class;
266
267 static struct pstream_vconn *
268 pstream_vconn_cast(struct vconn *vconn)
269 {
270     assert(vconn->class == &pstream_vconn_class);
271     return CONTAINER_OF(vconn, struct pstream_vconn, vconn);
272 }
273
274 int
275 new_pstream_vconn(const char *name, int fd,
276                   int (*accept_cb)(int fd, const struct sockaddr *,
277                                    size_t sa_len, struct vconn **),
278                   struct vconn **vconnp)
279 {
280     struct pstream_vconn *ps;
281     int retval;
282
283     retval = set_nonblocking(fd);
284     if (retval) {
285         close(fd);
286         return retval;
287     }
288
289     if (listen(fd, 10) < 0) {
290         int error = errno;
291         VLOG_ERR("%s: listen: %s", name, strerror(error));
292         close(fd);
293         return error;
294     }
295
296     ps = xmalloc(sizeof *ps);
297     ps->vconn.class = &pstream_vconn_class;
298     ps->vconn.connect_status = 0;
299     ps->fd = fd;
300     ps->accept_cb = accept_cb;
301     *vconnp = &ps->vconn;
302     return 0;
303 }
304
305 static void
306 pstream_close(struct vconn *vconn)
307 {
308     struct pstream_vconn *ps = pstream_vconn_cast(vconn);
309     close(ps->fd);
310     free(ps);
311 }
312
313 static int
314 pstream_accept(struct vconn *vconn, struct vconn **new_vconnp)
315 {
316     struct pstream_vconn *ps = pstream_vconn_cast(vconn);
317     struct sockaddr_storage ss;
318     socklen_t ss_len = sizeof ss;
319     int new_fd;
320     int retval;
321
322     new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
323     if (new_fd < 0) {
324         int retval = errno;
325         if (retval != EAGAIN) {
326             VLOG_DBG("accept: %s", strerror(retval));
327         }
328         return retval;
329     }
330
331     retval = set_nonblocking(new_fd);
332     if (retval) {
333         close(new_fd);
334         return retval;
335     }
336
337     return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
338                          new_vconnp);
339 }
340
341 static void
342 pstream_wait(struct vconn *vconn, enum vconn_wait_type wait)
343 {
344     struct pstream_vconn *ps = pstream_vconn_cast(vconn);
345     assert(wait == WAIT_ACCEPT);
346     poll_fd_wait(ps->fd, POLLIN);
347 }
348
349 static struct vconn_class pstream_vconn_class = {
350     .name = "pstream",
351     .close = pstream_close,
352     .accept = pstream_accept,
353     .wait = pstream_wait
354 };