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