ofproto: Define official OpenFlow port number.
[sliver-openvswitch.git] / lib / vconn-stream.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include <errno.h>
19 #include <poll.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include "fatal-signal.h"
25 #include "ofpbuf.h"
26 #include "openflow/openflow.h"
27 #include "poll-loop.h"
28 #include "socket-util.h"
29 #include "stream.h"
30 #include "util.h"
31 #include "vconn-provider.h"
32 #include "vconn.h"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(vconn_stream);
36
37 /* Active stream socket vconn. */
38
39 struct vconn_stream
40 {
41     struct vconn vconn;
42     struct stream *stream;
43     struct ofpbuf *rxbuf;
44     struct ofpbuf *txbuf;
45     int n_packets;
46 };
47
48 static const struct vconn_class stream_vconn_class;
49
50 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
51
52 static void vconn_stream_clear_txbuf(struct vconn_stream *);
53
54 static struct vconn *
55 vconn_stream_new(struct stream *stream, int connect_status,
56                  uint32_t allowed_versions)
57 {
58     struct vconn_stream *s;
59
60     s = xmalloc(sizeof *s);
61     vconn_init(&s->vconn, &stream_vconn_class, connect_status,
62                stream_get_name(stream), allowed_versions);
63     s->stream = stream;
64     s->txbuf = NULL;
65     s->rxbuf = NULL;
66     s->n_packets = 0;
67     s->vconn.remote_ip = stream_get_remote_ip(stream);
68     s->vconn.remote_port = stream_get_remote_port(stream);
69     s->vconn.local_ip = stream_get_local_ip(stream);
70     s->vconn.local_port = stream_get_local_port(stream);
71     return &s->vconn;
72 }
73
74 /* Creates a new vconn that will send and receive data on a stream named 'name'
75  * and stores a pointer to the vconn in '*vconnp'.
76  *
77  * Returns 0 if successful, otherwise a positive errno value. */
78 static int
79 vconn_stream_open(const char *name, uint32_t allowed_versions,
80                   char *suffix OVS_UNUSED, struct vconn **vconnp, uint8_t dscp)
81 {
82     struct stream *stream;
83     int error;
84
85     error = stream_open_with_default_port(name, OFP_OLD_PORT, &stream, dscp);
86     if (!error) {
87         error = stream_connect(stream);
88         if (!error || error == EAGAIN) {
89             *vconnp = vconn_stream_new(stream, error, allowed_versions);
90             return 0;
91         }
92     }
93
94     stream_close(stream);
95     return error;
96 }
97
98 static struct vconn_stream *
99 vconn_stream_cast(struct vconn *vconn)
100 {
101     return CONTAINER_OF(vconn, struct vconn_stream, vconn);
102 }
103
104 static void
105 vconn_stream_close(struct vconn *vconn)
106 {
107     struct vconn_stream *s = vconn_stream_cast(vconn);
108
109     if ((vconn->error == EPROTO || s->n_packets < 1) && s->rxbuf) {
110         stream_report_content(s->rxbuf->data, s->rxbuf->size, STREAM_OPENFLOW,
111                               THIS_MODULE, vconn_get_name(vconn));
112     }
113
114     stream_close(s->stream);
115     vconn_stream_clear_txbuf(s);
116     ofpbuf_delete(s->rxbuf);
117     free(s);
118 }
119
120 static int
121 vconn_stream_connect(struct vconn *vconn)
122 {
123     struct vconn_stream *s = vconn_stream_cast(vconn);
124     return stream_connect(s->stream);
125 }
126
127 static int
128 vconn_stream_recv__(struct vconn_stream *s, int rx_len)
129 {
130     struct ofpbuf *rx = s->rxbuf;
131     int want_bytes, retval;
132
133     want_bytes = rx_len - rx->size;
134     ofpbuf_prealloc_tailroom(rx, want_bytes);
135     retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
136     if (retval > 0) {
137         rx->size += retval;
138         return retval == want_bytes ? 0 : EAGAIN;
139     } else if (retval == 0) {
140         if (rx->size) {
141             VLOG_ERR_RL(&rl, "connection dropped mid-packet");
142             return EPROTO;
143         }
144         return EOF;
145     } else {
146         return -retval;
147     }
148 }
149
150 static int
151 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
152 {
153     struct vconn_stream *s = vconn_stream_cast(vconn);
154     const struct ofp_header *oh;
155     int rx_len;
156
157     /* Allocate new receive buffer if we don't have one. */
158     if (s->rxbuf == NULL) {
159         s->rxbuf = ofpbuf_new(1564);
160     }
161
162     /* Read ofp_header. */
163     if (s->rxbuf->size < sizeof(struct ofp_header)) {
164         int retval = vconn_stream_recv__(s, sizeof(struct ofp_header));
165         if (retval) {
166             return retval;
167         }
168     }
169
170     /* Read payload. */
171     oh = s->rxbuf->data;
172     rx_len = ntohs(oh->length);
173     if (rx_len < sizeof(struct ofp_header)) {
174         VLOG_ERR_RL(&rl, "received too-short ofp_header (%d bytes)", rx_len);
175         return EPROTO;
176     } else if (s->rxbuf->size < rx_len) {
177         int retval = vconn_stream_recv__(s, rx_len);
178         if (retval) {
179             return retval;
180         }
181     }
182
183     s->n_packets++;
184     *bufferp = s->rxbuf;
185     s->rxbuf = NULL;
186     return 0;
187 }
188
189 static void
190 vconn_stream_clear_txbuf(struct vconn_stream *s)
191 {
192     ofpbuf_delete(s->txbuf);
193     s->txbuf = NULL;
194 }
195
196 static int
197 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
198 {
199     struct vconn_stream *s = vconn_stream_cast(vconn);
200     ssize_t retval;
201
202     if (s->txbuf) {
203         return EAGAIN;
204     }
205
206     retval = stream_send(s->stream, buffer->data, buffer->size);
207     if (retval == buffer->size) {
208         ofpbuf_delete(buffer);
209         return 0;
210     } else if (retval >= 0 || retval == -EAGAIN) {
211         s->txbuf = buffer;
212         if (retval > 0) {
213             ofpbuf_pull(buffer, retval);
214         }
215         return 0;
216     } else {
217         return -retval;
218     }
219 }
220
221 static void
222 vconn_stream_run(struct vconn *vconn)
223 {
224     struct vconn_stream *s = vconn_stream_cast(vconn);
225     ssize_t retval;
226
227     stream_run(s->stream);
228     if (!s->txbuf) {
229         return;
230     }
231
232     retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
233     if (retval < 0) {
234         if (retval != -EAGAIN) {
235             VLOG_ERR_RL(&rl, "send: %s", ovs_strerror(-retval));
236             vconn_stream_clear_txbuf(s);
237             return;
238         }
239     } else if (retval > 0) {
240         ofpbuf_pull(s->txbuf, retval);
241         if (!s->txbuf->size) {
242             vconn_stream_clear_txbuf(s);
243             return;
244         }
245     }
246 }
247
248 static void
249 vconn_stream_run_wait(struct vconn *vconn)
250 {
251     struct vconn_stream *s = vconn_stream_cast(vconn);
252
253     stream_run_wait(s->stream);
254     if (s->txbuf) {
255         stream_send_wait(s->stream);
256     }
257 }
258
259 static void
260 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
261 {
262     struct vconn_stream *s = vconn_stream_cast(vconn);
263     switch (wait) {
264     case WAIT_CONNECT:
265         stream_connect_wait(s->stream);
266         break;
267
268     case WAIT_SEND:
269         if (!s->txbuf) {
270             stream_send_wait(s->stream);
271         } else {
272             /* Nothing to do: need to drain txbuf first.
273              * vconn_stream_run_wait() will arrange to wake up when there room
274              * to send data, so there's no point in calling poll_fd_wait()
275              * redundantly here. */
276         }
277         break;
278
279     case WAIT_RECV:
280         stream_recv_wait(s->stream);
281         break;
282
283     default:
284         NOT_REACHED();
285     }
286 }
287 \f
288 /* Passive stream socket vconn. */
289
290 struct pvconn_pstream
291 {
292     struct pvconn pvconn;
293     struct pstream *pstream;
294 };
295
296 static const struct pvconn_class pstream_pvconn_class;
297
298 static struct pvconn_pstream *
299 pvconn_pstream_cast(struct pvconn *pvconn)
300 {
301     return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
302 }
303
304 /* Creates a new pvconn named 'name' that will accept new connections using
305  * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
306  *
307  * Returns 0 if successful, otherwise a positive errno value.  (The current
308  * implementation never fails.) */
309 static int
310 pvconn_pstream_listen(const char *name, uint32_t allowed_versions,
311                       char *suffix OVS_UNUSED, struct pvconn **pvconnp,
312                       uint8_t dscp)
313 {
314     struct pvconn_pstream *ps;
315     struct pstream *pstream;
316     int error;
317
318     error = pstream_open_with_default_port(name, OFP_OLD_PORT,
319                                            &pstream, dscp);
320     if (error) {
321         return error;
322     }
323
324     ps = xmalloc(sizeof *ps);
325     pvconn_init(&ps->pvconn, &pstream_pvconn_class, name, allowed_versions);
326     ps->pstream = pstream;
327     *pvconnp = &ps->pvconn;
328     return 0;
329 }
330
331 static void
332 pvconn_pstream_close(struct pvconn *pvconn)
333 {
334     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
335     pstream_close(ps->pstream);
336     free(ps);
337 }
338
339 static int
340 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
341 {
342     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
343     struct stream *stream;
344     int error;
345
346     error = pstream_accept(ps->pstream, &stream);
347     if (error) {
348         if (error != EAGAIN) {
349             VLOG_DBG_RL(&rl, "%s: accept: %s",
350                         pstream_get_name(ps->pstream), ovs_strerror(error));
351         }
352         return error;
353     }
354
355     *new_vconnp = vconn_stream_new(stream, 0, pvconn->allowed_versions);
356     return 0;
357 }
358
359 static void
360 pvconn_pstream_wait(struct pvconn *pvconn)
361 {
362     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
363     pstream_wait(ps->pstream);
364 }
365 \f
366 /* Stream-based vconns and pvconns. */
367
368 #define STREAM_INIT(NAME)                           \
369     {                                               \
370             NAME,                                   \
371             vconn_stream_open,                      \
372             vconn_stream_close,                     \
373             vconn_stream_connect,                   \
374             vconn_stream_recv,                      \
375             vconn_stream_send,                      \
376             vconn_stream_run,                       \
377             vconn_stream_run_wait,                  \
378             vconn_stream_wait,                      \
379     }
380
381 #define PSTREAM_INIT(NAME)                          \
382     {                                               \
383             NAME,                                   \
384             pvconn_pstream_listen,                  \
385             pvconn_pstream_close,                   \
386             pvconn_pstream_accept,                  \
387             pvconn_pstream_wait                     \
388     }
389
390 static const struct vconn_class stream_vconn_class = STREAM_INIT("stream");
391 static const struct pvconn_class pstream_pvconn_class = PSTREAM_INIT("pstream");
392
393 const struct vconn_class tcp_vconn_class = STREAM_INIT("tcp");
394 const struct pvconn_class ptcp_pvconn_class = PSTREAM_INIT("ptcp");
395
396 const struct vconn_class unix_vconn_class = STREAM_INIT("unix");
397 const struct pvconn_class punix_pvconn_class = PSTREAM_INIT("punix");
398
399 #ifdef HAVE_OPENSSL
400 const struct vconn_class ssl_vconn_class = STREAM_INIT("ssl");
401 const struct pvconn_class pssl_pvconn_class = PSTREAM_INIT("pssl");
402 #endif