vconn-stream: Factor out port defaults into public helper functions.
[sliver-openvswitch.git] / lib / vconn-stream.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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 <assert.h>
19 #include <errno.h>
20 #include <poll.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include "fatal-signal.h"
26 #include "leak-checker.h"
27 #include "ofpbuf.h"
28 #include "openflow/openflow.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
31 #include "stream.h"
32 #include "util.h"
33 #include "vconn-provider.h"
34 #include "vconn.h"
35
36 #include "vlog.h"
37 #define THIS_MODULE VLM_vconn_stream
38
39 /* Active stream socket vconn. */
40
41 struct vconn_stream
42 {
43     struct vconn vconn;
44     struct stream *stream;
45     struct ofpbuf *rxbuf;
46     struct ofpbuf *txbuf;
47 };
48
49 static struct vconn_class stream_vconn_class;
50
51 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
52
53 static void vconn_stream_clear_txbuf(struct vconn_stream *);
54
55 static struct vconn *
56 vconn_stream_new(struct stream *stream, int connect_status)
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));
63     s->stream = stream;
64     s->txbuf = NULL;
65     s->rxbuf = NULL;
66     s->vconn.remote_ip = stream_get_remote_ip(stream);
67     s->vconn.remote_port = stream_get_remote_port(stream);
68     s->vconn.local_ip = stream_get_local_ip(stream);
69     s->vconn.local_port = stream_get_local_port(stream);
70     return &s->vconn;
71 }
72
73 /* Creates a new vconn that will send and receive data on a stream named 'name'
74  * and stores a pointer to the vconn in '*vconnp'.
75  *
76  * Returns 0 if successful, otherwise a positive errno value. */
77 static int
78 vconn_stream_open(const char *name, char *suffix OVS_UNUSED,
79                   struct vconn **vconnp)
80 {
81     struct stream *stream;
82     int error;
83
84     error = stream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
85                                            &stream);
86
87     if (error && error != EAGAIN) {
88         return error;
89     }
90
91     *vconnp = vconn_stream_new(stream, error);
92     return 0;
93 }
94
95 static struct vconn_stream *
96 vconn_stream_cast(struct vconn *vconn)
97 {
98     return CONTAINER_OF(vconn, struct vconn_stream, vconn);
99 }
100
101 static void
102 vconn_stream_close(struct vconn *vconn)
103 {
104     struct vconn_stream *s = vconn_stream_cast(vconn);
105     stream_close(s->stream);
106     vconn_stream_clear_txbuf(s);
107     ofpbuf_delete(s->rxbuf);
108     free(s);
109 }
110
111 static int
112 vconn_stream_connect(struct vconn *vconn)
113 {
114     struct vconn_stream *s = vconn_stream_cast(vconn);
115     return stream_connect(s->stream);
116 }
117
118 static int
119 vconn_stream_recv(struct vconn *vconn, struct ofpbuf **bufferp)
120 {
121     struct vconn_stream *s = vconn_stream_cast(vconn);
122     struct ofpbuf *rx;
123     size_t want_bytes;
124     ssize_t retval;
125
126     if (s->rxbuf == NULL) {
127         s->rxbuf = ofpbuf_new(1564);
128     }
129     rx = s->rxbuf;
130
131 again:
132     if (sizeof(struct ofp_header) > rx->size) {
133         want_bytes = sizeof(struct ofp_header) - rx->size;
134     } else {
135         struct ofp_header *oh = rx->data;
136         size_t length = ntohs(oh->length);
137         if (length < sizeof(struct ofp_header)) {
138             VLOG_ERR_RL(&rl, "received too-short ofp_header (%zu bytes)",
139                         length);
140             return EPROTO;
141         }
142         want_bytes = length - rx->size;
143         if (!want_bytes) {
144             *bufferp = rx;
145             s->rxbuf = NULL;
146             return 0;
147         }
148     }
149     ofpbuf_prealloc_tailroom(rx, want_bytes);
150
151     retval = stream_recv(s->stream, ofpbuf_tail(rx), want_bytes);
152     if (retval > 0) {
153         rx->size += retval;
154         if (retval == want_bytes) {
155             if (rx->size > sizeof(struct ofp_header)) {
156                 *bufferp = rx;
157                 s->rxbuf = NULL;
158                 return 0;
159             } else {
160                 goto again;
161             }
162         }
163         return EAGAIN;
164     } else if (retval == 0) {
165         if (rx->size) {
166             VLOG_ERR_RL(&rl, "connection dropped mid-packet");
167             return EPROTO;
168         } else {
169             return EOF;
170         }
171     } else {
172         return -retval;
173     }
174 }
175
176 static void
177 vconn_stream_clear_txbuf(struct vconn_stream *s)
178 {
179     ofpbuf_delete(s->txbuf);
180     s->txbuf = NULL;
181 }
182
183 static int
184 vconn_stream_send(struct vconn *vconn, struct ofpbuf *buffer)
185 {
186     struct vconn_stream *s = vconn_stream_cast(vconn);
187     ssize_t retval;
188
189     if (s->txbuf) {
190         return EAGAIN;
191     }
192
193     retval = stream_send(s->stream, buffer->data, buffer->size);
194     if (retval == buffer->size) {
195         ofpbuf_delete(buffer);
196         return 0;
197     } else if (retval >= 0 || retval == -EAGAIN) {
198         leak_checker_claim(buffer);
199         s->txbuf = buffer;
200         if (retval > 0) {
201             ofpbuf_pull(buffer, retval);
202         }
203         return 0;
204     } else {
205         return -retval;
206     }
207 }
208
209 static void
210 vconn_stream_run(struct vconn *vconn)
211 {
212     struct vconn_stream *s = vconn_stream_cast(vconn);
213     ssize_t retval;
214
215     if (!s->txbuf) {
216         return;
217     }
218
219     retval = stream_send(s->stream, s->txbuf->data, s->txbuf->size);
220     if (retval < 0) {
221         if (retval != -EAGAIN) {
222             VLOG_ERR_RL(&rl, "send: %s", strerror(-retval));
223             vconn_stream_clear_txbuf(s);
224             return;
225         }
226     } else if (retval > 0) {
227         ofpbuf_pull(s->txbuf, retval);
228         if (!s->txbuf->size) {
229             vconn_stream_clear_txbuf(s);
230             return;
231         }
232     }
233 }
234
235 static void
236 vconn_stream_run_wait(struct vconn *vconn)
237 {
238     struct vconn_stream *s = vconn_stream_cast(vconn);
239
240     if (s->txbuf) {
241         stream_send_wait(s->stream);
242     }
243 }
244
245 static void
246 vconn_stream_wait(struct vconn *vconn, enum vconn_wait_type wait)
247 {
248     struct vconn_stream *s = vconn_stream_cast(vconn);
249     switch (wait) {
250     case WAIT_CONNECT:
251         stream_connect_wait(s->stream);
252         break;
253
254     case WAIT_SEND:
255         if (!s->txbuf) {
256             stream_send_wait(s->stream);
257         } else {
258             /* Nothing to do: need to drain txbuf first.
259              * vconn_stream_run_wait() will arrange to wake up when there room
260              * to send data, so there's no point in calling poll_fd_wait()
261              * redundantly here. */
262         }
263         break;
264
265     case WAIT_RECV:
266         stream_recv_wait(s->stream);
267         break;
268
269     default:
270         NOT_REACHED();
271     }
272 }
273 \f
274 /* Passive stream socket vconn. */
275
276 struct pvconn_pstream
277 {
278     struct pvconn pvconn;
279     struct pstream *pstream;
280 };
281
282 static struct pvconn_class pstream_pvconn_class;
283
284 static struct pvconn_pstream *
285 pvconn_pstream_cast(struct pvconn *pvconn)
286 {
287     return CONTAINER_OF(pvconn, struct pvconn_pstream, pvconn);
288 }
289
290 /* Creates a new pvconn named 'name' that will accept new connections using
291  * pstream_accept() and stores a pointer to the pvconn in '*pvconnp'.
292  *
293  * Returns 0 if successful, otherwise a positive errno value.  (The current
294  * implementation never fails.) */
295 static int
296 pvconn_pstream_listen(const char *name, char *suffix OVS_UNUSED,
297                       struct pvconn **pvconnp)
298 {
299     struct pvconn_pstream *ps;
300     struct pstream *pstream;
301     int error;
302
303     error = pstream_open_with_default_ports(name, OFP_TCP_PORT, OFP_SSL_PORT,
304                                             &pstream);
305     if (error) {
306         return error;
307     }
308
309     ps = xmalloc(sizeof *ps);
310     pvconn_init(&ps->pvconn, &pstream_pvconn_class, name);
311     ps->pstream = pstream;
312     *pvconnp = &ps->pvconn;
313     return 0;
314 }
315
316 static void
317 pvconn_pstream_close(struct pvconn *pvconn)
318 {
319     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
320     pstream_close(ps->pstream);
321     free(ps);
322 }
323
324 static int
325 pvconn_pstream_accept(struct pvconn *pvconn, struct vconn **new_vconnp)
326 {
327     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
328     struct stream *stream;
329     int error;
330
331     error = pstream_accept(ps->pstream, &stream);
332     if (error) {
333         if (error != EAGAIN) {
334             VLOG_DBG_RL(&rl, "%s: accept: %s",
335                         pstream_get_name(ps->pstream), strerror(error));
336         }
337         return error;
338     }
339
340     *new_vconnp = vconn_stream_new(stream, 0);
341     return 0;
342 }
343
344 static void
345 pvconn_pstream_wait(struct pvconn *pvconn)
346 {
347     struct pvconn_pstream *ps = pvconn_pstream_cast(pvconn);
348     pstream_wait(ps->pstream);
349 }
350 \f
351 /* Stream-based vconns and pvconns. */
352
353 #define DEFINE_VCONN_STREAM_CLASS(NAME)             \
354         struct vconn_class NAME##_vconn_class = {   \
355             #NAME,                                  \
356             vconn_stream_open,                      \
357             vconn_stream_close,                     \
358             vconn_stream_connect,                   \
359             vconn_stream_recv,                      \
360             vconn_stream_send,                      \
361             vconn_stream_run,                       \
362             vconn_stream_run_wait,                  \
363             vconn_stream_wait,                      \
364         };
365
366 #define DEFINE_PVCONN_STREAM_CLASS(NAME)            \
367         struct pvconn_class NAME##_pvconn_class = { \
368             #NAME,                                  \
369             pvconn_pstream_listen,                  \
370             pvconn_pstream_close,                   \
371             pvconn_pstream_accept,                  \
372             pvconn_pstream_wait                     \
373         };
374
375 static DEFINE_VCONN_STREAM_CLASS(stream);
376 static DEFINE_PVCONN_STREAM_CLASS(pstream);
377
378 DEFINE_VCONN_STREAM_CLASS(tcp);
379 DEFINE_PVCONN_STREAM_CLASS(ptcp);
380
381 DEFINE_VCONN_STREAM_CLASS(unix);
382 DEFINE_PVCONN_STREAM_CLASS(punix);
383
384 #ifdef HAVE_OPENSSL
385 DEFINE_VCONN_STREAM_CLASS(ssl);
386 DEFINE_PVCONN_STREAM_CLASS(pssl);
387 #endif