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