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