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