Make vconn-tcp log an error when the connection drops mid-packet.
[sliver-openvswitch.git] / lib / vconn-tcp.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 "vconn.h"
35 #include <assert.h>
36 #include <errno.h>
37 #include <netdb.h>
38 #include <poll.h>
39 #include <sys/types.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include "buffer.h"
46 #include "socket-util.h"
47 #include "util.h"
48 #include "openflow.h"
49 #include "ofp-print.h"
50 #include "poll-loop.h"
51
52 #include "vlog.h"
53 #define THIS_MODULE VLM_vconn_tcp
54
55 /* Active TCP. */
56
57 struct tcp_vconn
58 {
59     struct vconn vconn;
60     int fd;
61     struct buffer *rxbuf;
62     struct buffer *txbuf;
63     struct poll_waiter *tx_waiter;
64 };
65
66 static int
67 new_tcp_vconn(const char *name, int fd, int connect_status,
68               struct vconn **vconnp)
69 {
70     struct tcp_vconn *tcp;
71     int on = 1;
72     int retval;
73
74     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
75     if (retval) {
76         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
77         close(fd);
78         return errno;
79     }
80
81     tcp = xmalloc(sizeof *tcp);
82     tcp->vconn.class = &tcp_vconn_class;
83     tcp->vconn.connect_status = connect_status;
84     tcp->fd = fd;
85     tcp->txbuf = NULL;
86     tcp->tx_waiter = NULL;
87     tcp->rxbuf = NULL;
88     *vconnp = &tcp->vconn;
89     return 0;
90 }
91
92 static struct tcp_vconn *
93 tcp_vconn_cast(struct vconn *vconn)
94 {
95     assert(vconn->class == &tcp_vconn_class);
96     return CONTAINER_OF(vconn, struct tcp_vconn, vconn);
97 }
98
99
100 static int
101 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
102 {
103     char *save_ptr;
104     const char *host_name;
105     const char *port_string;
106     struct sockaddr_in sin;
107     int retval;
108     int fd;
109
110     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
111      * can cause segfaults here:
112      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
113      * Using "::" instead of the obvious ":" works around it. */
114     host_name = strtok_r(suffix, "::", &save_ptr);
115     port_string = strtok_r(NULL, "::", &save_ptr);
116     if (!host_name) {
117         fatal(0, "%s: bad peer name format", name);
118     }
119
120     memset(&sin, 0, sizeof sin);
121     sin.sin_family = AF_INET;
122     if (lookup_ip(host_name, &sin.sin_addr)) {
123         return ENOENT;
124     }
125     sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
126
127     fd = socket(AF_INET, SOCK_STREAM, 0);
128     if (fd < 0) {
129         VLOG_ERR("%s: socket: %s", name, strerror(errno));
130         return errno;
131     }
132
133     retval = set_nonblocking(fd);
134     if (retval) {
135         close(fd);
136         return retval;
137     }
138
139     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
140     if (retval < 0) {
141         if (errno == EINPROGRESS) {
142             return new_tcp_vconn(name, fd, EAGAIN, vconnp);
143         } else {
144             int error = errno;
145             VLOG_ERR("%s: connect: %s", name, strerror(error));
146             close(fd);
147             return error;
148         }
149     } else {
150         return new_tcp_vconn(name, fd, 0, vconnp);
151     }
152 }
153
154 static void
155 tcp_close(struct vconn *vconn)
156 {
157     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
158     poll_cancel(tcp->tx_waiter);
159     close(tcp->fd);
160     free(tcp);
161 }
162
163 static int
164 tcp_connect(struct vconn *vconn)
165 {
166     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
167     return check_connection_completion(tcp->fd);
168 }
169
170 static int
171 tcp_recv(struct vconn *vconn, struct buffer **bufferp)
172 {
173     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
174     struct buffer *rx;
175     size_t want_bytes;
176     ssize_t retval;
177
178     if (tcp->rxbuf == NULL) {
179         tcp->rxbuf = buffer_new(1564);
180     }
181     rx = tcp->rxbuf;
182
183 again:
184     if (sizeof(struct ofp_header) > rx->size) {
185         want_bytes = sizeof(struct ofp_header) - rx->size;
186     } else {
187         struct ofp_header *oh = rx->data;
188         size_t length = ntohs(oh->length);
189         if (length < sizeof(struct ofp_header)) {
190             VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
191             return EPROTO;
192         }
193         want_bytes = length - rx->size;
194         if (!want_bytes) {
195             *bufferp = rx;
196             tcp->rxbuf = NULL;
197             return 0;
198         }
199     }
200     buffer_reserve_tailroom(rx, want_bytes);
201
202     retval = read(tcp->fd, buffer_tail(rx), want_bytes);
203     if (retval > 0) {
204         rx->size += retval;
205         if (retval == want_bytes) {
206             if (rx->size > sizeof(struct ofp_header)) {
207                 *bufferp = rx;
208                 tcp->rxbuf = NULL;
209                 return 0;
210             } else {
211                 goto again;
212             }
213         }
214         return EAGAIN;
215     } else if (retval == 0) {
216         if (rx->size) {
217             VLOG_ERR("connection dropped mid-packet");
218             return EPROTO;
219         } else {
220             return EOF; 
221         }
222     } else {
223         return retval ? errno : EAGAIN;
224     }
225 }
226
227 static void
228 tcp_clear_txbuf(struct tcp_vconn *tcp)
229 {
230     buffer_delete(tcp->txbuf);
231     tcp->txbuf = NULL;
232     tcp->tx_waiter = NULL;
233 }
234
235 static void
236 tcp_do_tx(int fd UNUSED, short int revents UNUSED, void *vconn_)
237 {
238     struct vconn *vconn = vconn_;
239     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
240     ssize_t n = write(tcp->fd, tcp->txbuf->data, tcp->txbuf->size);
241     if (n < 0) {
242         if (errno != EAGAIN) {
243             VLOG_ERR("send: %s", strerror(errno));
244             tcp_clear_txbuf(tcp);
245             return;
246         }
247     } else if (n > 0) {
248         buffer_pull(tcp->txbuf, n);
249         if (!tcp->txbuf->size) {
250             tcp_clear_txbuf(tcp);
251             return;
252         }
253     }
254     tcp->tx_waiter = poll_fd_callback(tcp->fd, POLLOUT, tcp_do_tx, vconn);
255 }
256
257 static int
258 tcp_send(struct vconn *vconn, struct buffer *buffer)
259 {
260     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
261     ssize_t retval;
262
263     if (tcp->txbuf) {
264         return EAGAIN;
265     }
266
267     retval = write(tcp->fd, buffer->data, buffer->size);
268     if (retval == buffer->size) {
269         buffer_delete(buffer);
270         return 0;
271     } else if (retval >= 0 || errno == EAGAIN) {
272         tcp->txbuf = buffer;
273         if (retval > 0) {
274             buffer_pull(buffer, retval);
275         }
276         tcp->tx_waiter = poll_fd_callback(tcp->fd, POLLOUT, tcp_do_tx, vconn);
277         return 0;
278     } else {
279         return errno;
280     }
281 }
282
283 static void
284 tcp_wait(struct vconn *vconn, enum vconn_wait_type wait)
285 {
286     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
287     switch (wait) {
288     case WAIT_CONNECT:
289         poll_fd_wait(tcp->fd, POLLOUT);
290         break;
291
292     case WAIT_SEND:
293         if (!tcp->txbuf) {
294             poll_fd_wait(tcp->fd, POLLOUT);
295         } else {
296             /* Nothing to do: need to drain txbuf first. */
297         }
298         break;
299
300     case WAIT_RECV:
301         poll_fd_wait(tcp->fd, POLLIN);
302         break;
303
304     default:
305         NOT_REACHED();
306     }
307 }
308
309 struct vconn_class tcp_vconn_class = {
310     .name = "tcp",
311     .open = tcp_open,
312     .close = tcp_close,
313     .connect = tcp_connect,
314     .recv = tcp_recv,
315     .send = tcp_send,
316     .wait = tcp_wait,
317 };
318 \f
319 /* Passive TCP. */
320
321 struct ptcp_vconn
322 {
323     struct vconn vconn;
324     int fd;
325 };
326
327 static struct ptcp_vconn *
328 ptcp_vconn_cast(struct vconn *vconn)
329 {
330     assert(vconn->class == &ptcp_vconn_class);
331     return CONTAINER_OF(vconn, struct ptcp_vconn, vconn);
332 }
333
334 static int
335 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
336 {
337     struct sockaddr_in sin;
338     struct ptcp_vconn *ptcp;
339     int retval;
340     int fd;
341     unsigned int yes  = 1;
342
343     fd = socket(AF_INET, SOCK_STREAM, 0);
344     if (fd < 0) {
345         VLOG_ERR("%s: socket: %s", name, strerror(errno));
346         return errno;
347     }
348
349     if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
350         VLOG_ERR("%s: setsockopt::SO_REUSEADDR: %s", name, strerror(errno));
351         return errno;
352     }
353
354
355     memset(&sin, 0, sizeof sin);
356     sin.sin_family = AF_INET;
357     sin.sin_addr.s_addr = htonl(INADDR_ANY);
358     sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
359     retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
360     if (retval < 0) {
361         int error = errno;
362         VLOG_ERR("%s: bind: %s", name, strerror(error));
363         close(fd);
364         return error;
365     }
366
367     retval = listen(fd, 10);
368     if (retval < 0) {
369         int error = errno;
370         VLOG_ERR("%s: listen: %s", name, strerror(error));
371         close(fd);
372         return error;
373     }
374
375     retval = set_nonblocking(fd);
376     if (retval) {
377         close(fd);
378         return retval;
379     }
380
381     ptcp = xmalloc(sizeof *ptcp);
382     ptcp->vconn.class = &ptcp_vconn_class;
383     ptcp->vconn.connect_status = 0;
384     ptcp->fd = fd;
385     *vconnp = &ptcp->vconn;
386     return 0;
387 }
388
389 static void
390 ptcp_close(struct vconn *vconn)
391 {
392     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
393     close(ptcp->fd);
394     free(ptcp);
395 }
396
397 static int
398 ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp)
399 {
400     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
401     int new_fd;
402     int error;
403
404     new_fd = accept(ptcp->fd, NULL, NULL);
405     if (new_fd < 0) {
406         int error = errno;
407         if (error != EAGAIN) {
408             VLOG_DBG("accept: %s", strerror(error));
409         }
410         return error;
411     }
412
413     error = set_nonblocking(new_fd);
414     if (error) {
415         close(new_fd);
416         return error;
417     }
418
419     return new_tcp_vconn("tcp" /* FIXME */, new_fd, 0, new_vconnp);
420 }
421
422 static void
423 ptcp_wait(struct vconn *vconn, enum vconn_wait_type wait)
424 {
425     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
426     assert(wait == WAIT_ACCEPT);
427     poll_fd_wait(ptcp->fd, POLLIN);
428 }
429
430 struct vconn_class ptcp_vconn_class = {
431     .name = "ptcp",
432     .open = ptcp_open,
433     .close = ptcp_close,
434     .accept = ptcp_accept,
435     .wait = ptcp_wait
436 };
437