Add the ability to connect to a vconn asynchronously.
[openvswitch] / lib / vconn-tcp.c
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21
22 #include "vconn.h"
23 #include <assert.h>
24 #include <errno.h>
25 #include <netdb.h>
26 #include <poll.h>
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include "buffer.h"
34 #include "socket-util.h"
35 #include "util.h"
36 #include "openflow.h"
37 #include "ofp-print.h"
38 #include "poll-loop.h"
39
40 #include "vlog.h"
41 #define THIS_MODULE VLM_vconn_tcp
42
43 /* Active TCP. */
44
45 struct tcp_vconn
46 {
47     struct vconn vconn;
48     int fd;
49     struct buffer *rxbuf;
50     struct buffer *txbuf;
51     struct poll_waiter *tx_waiter;
52 };
53
54 static int
55 new_tcp_vconn(const char *name, int fd, int connect_status,
56               struct vconn **vconnp)
57 {
58     struct tcp_vconn *tcp;
59     int on = 1;
60     int retval;
61
62     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
63     if (retval) {
64         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
65         close(fd);
66         return errno;
67     }
68
69     tcp = xmalloc(sizeof *tcp);
70     tcp->vconn.class = &tcp_vconn_class;
71     tcp->vconn.connect_status = connect_status;
72     tcp->fd = fd;
73     tcp->txbuf = NULL;
74     tcp->tx_waiter = NULL;
75     tcp->rxbuf = NULL;
76     *vconnp = &tcp->vconn;
77     return 0;
78 }
79
80 static struct tcp_vconn *
81 tcp_vconn_cast(struct vconn *vconn)
82 {
83     assert(vconn->class == &tcp_vconn_class);
84     return CONTAINER_OF(vconn, struct tcp_vconn, vconn);
85 }
86
87
88 static int
89 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
90 {
91     char *save_ptr;
92     const char *host_name;
93     const char *port_string;
94     struct sockaddr_in sin;
95     int retval;
96     int fd;
97
98     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
99      * can cause segfaults here:
100      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
101      * Using "::" instead of the obvious ":" works around it. */
102     host_name = strtok_r(suffix, "::", &save_ptr);
103     port_string = strtok_r(NULL, "::", &save_ptr);
104     if (!host_name) {
105         fatal(0, "%s: bad peer name format", name);
106     }
107
108     memset(&sin, 0, sizeof sin);
109     sin.sin_family = AF_INET;
110     if (lookup_ip(host_name, &sin.sin_addr)) {
111         return ENOENT;
112     }
113     sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
114
115     fd = socket(AF_INET, SOCK_STREAM, 0);
116     if (fd < 0) {
117         VLOG_ERR("%s: socket: %s", name, strerror(errno));
118         return errno;
119     }
120
121     retval = set_nonblocking(fd);
122     if (retval) {
123         close(fd);
124         return retval;
125     }
126
127     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
128     if (retval < 0) {
129         if (errno == EINPROGRESS) {
130             return new_tcp_vconn(name, fd, EAGAIN, vconnp);
131         } else {
132             int error = errno;
133             VLOG_ERR("%s: connect: %s", name, strerror(error));
134             close(fd);
135             return error;
136         }
137     } else {
138         return new_tcp_vconn(name, fd, 0, vconnp);
139     }
140 }
141
142 static void
143 tcp_close(struct vconn *vconn)
144 {
145     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
146     poll_cancel(tcp->tx_waiter);
147     close(tcp->fd);
148     free(tcp);
149 }
150
151 static int
152 tcp_connect(struct vconn *vconn)
153 {
154     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
155     return check_connection_completion(tcp->fd);
156 }
157
158 static int
159 tcp_recv(struct vconn *vconn, struct buffer **bufferp)
160 {
161     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
162     struct buffer *rx;
163     size_t want_bytes;
164     ssize_t retval;
165
166     if (tcp->rxbuf == NULL) {
167         tcp->rxbuf = buffer_new(1564);
168     }
169     rx = tcp->rxbuf;
170
171 again:
172     if (sizeof(struct ofp_header) > rx->size) {
173         want_bytes = sizeof(struct ofp_header) - rx->size;
174     } else {
175         struct ofp_header *oh = rx->data;
176         size_t length = ntohs(oh->length);
177         if (length < sizeof(struct ofp_header)) {
178             VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
179             return EPROTO;
180         }
181         want_bytes = length - rx->size;
182     }
183     buffer_reserve_tailroom(rx, want_bytes);
184
185     retval = read(tcp->fd, buffer_tail(rx), want_bytes);
186     if (retval > 0) {
187         rx->size += retval;
188         if (retval == want_bytes) {
189             if (rx->size > sizeof(struct ofp_header)) {
190                 *bufferp = rx;
191                 tcp->rxbuf = NULL;
192                 return 0;
193             } else {
194                 goto again;
195             }
196         }
197         return EAGAIN;
198     } else if (retval == 0) {
199         return rx->size ? EPROTO : EOF;
200     } else {
201         return retval ? errno : EAGAIN;
202     }
203 }
204
205 static void
206 tcp_clear_txbuf(struct tcp_vconn *tcp)
207 {
208     buffer_delete(tcp->txbuf);
209     tcp->txbuf = NULL;
210     tcp->tx_waiter = NULL;
211 }
212
213 static void
214 tcp_do_tx(int fd UNUSED, short int revents UNUSED, void *vconn_)
215 {
216     struct vconn *vconn = vconn_;
217     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
218     ssize_t n = write(tcp->fd, tcp->txbuf->data, tcp->txbuf->size);
219     if (n < 0) {
220         if (errno != EAGAIN) {
221             VLOG_ERR("send: %s", strerror(errno));
222             tcp_clear_txbuf(tcp);
223             return;
224         }
225     } else if (n > 0) {
226         buffer_pull(tcp->txbuf, n);
227         if (!tcp->txbuf->size) {
228             tcp_clear_txbuf(tcp);
229             return;
230         }
231     }
232     tcp->tx_waiter = poll_fd_callback(tcp->fd, POLLOUT, tcp_do_tx, vconn);
233 }
234
235 static int
236 tcp_send(struct vconn *vconn, struct buffer *buffer)
237 {
238     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
239     ssize_t retval;
240
241     if (tcp->txbuf) {
242         return EAGAIN;
243     }
244
245     retval = write(tcp->fd, buffer->data, buffer->size);
246     if (retval == buffer->size) {
247         buffer_delete(buffer);
248         return 0;
249     } else if (retval >= 0 || errno == EAGAIN) {
250         tcp->txbuf = buffer;
251         if (retval > 0) {
252             buffer_pull(buffer, retval);
253         }
254         tcp->tx_waiter = poll_fd_callback(tcp->fd, POLLOUT, tcp_do_tx, vconn);
255         return 0;
256     } else {
257         return errno;
258     }
259 }
260
261 static void
262 tcp_wait(struct vconn *vconn, enum vconn_wait_type wait)
263 {
264     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
265     switch (wait) {
266     case WAIT_CONNECT:
267         poll_fd_wait(tcp->fd, POLLOUT, NULL);
268         break;
269
270     case WAIT_SEND:
271         if (!tcp->txbuf) {
272             poll_fd_wait(tcp->fd, POLLOUT, NULL);
273         } else {
274             /* Nothing to do: need to drain txbuf first. */
275         }
276         break;
277
278     case WAIT_RECV:
279         poll_fd_wait(tcp->fd, POLLIN, NULL);
280         break;
281
282     default:
283         NOT_REACHED();
284     }
285 }
286
287 struct vconn_class tcp_vconn_class = {
288     .name = "tcp",
289     .open = tcp_open,
290     .close = tcp_close,
291     .connect = tcp_connect,
292     .recv = tcp_recv,
293     .send = tcp_send,
294     .wait = tcp_wait,
295 };
296 \f
297 /* Passive TCP. */
298
299 struct ptcp_vconn
300 {
301     struct vconn vconn;
302     int fd;
303 };
304
305 static struct ptcp_vconn *
306 ptcp_vconn_cast(struct vconn *vconn)
307 {
308     assert(vconn->class == &ptcp_vconn_class);
309     return CONTAINER_OF(vconn, struct ptcp_vconn, vconn);
310 }
311
312 static int
313 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
314 {
315     struct sockaddr_in sin;
316     struct ptcp_vconn *ptcp;
317     int retval;
318     int fd;
319     unsigned int yes  = 1;
320
321     fd = socket(AF_INET, SOCK_STREAM, 0);
322     if (fd < 0) {
323         VLOG_ERR("%s: socket: %s", name, strerror(errno));
324         return errno;
325     }
326
327     if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
328         VLOG_ERR("%s: setsockopt::SO_REUSEADDR: %s", name, strerror(errno));
329         return errno;
330     }
331
332
333     memset(&sin, 0, sizeof sin);
334     sin.sin_family = AF_INET;
335     sin.sin_addr.s_addr = htonl(INADDR_ANY);
336     sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
337     retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
338     if (retval < 0) {
339         int error = errno;
340         VLOG_ERR("%s: bind: %s", name, strerror(error));
341         close(fd);
342         return error;
343     }
344
345     retval = listen(fd, 10);
346     if (retval < 0) {
347         int error = errno;
348         VLOG_ERR("%s: listen: %s", name, strerror(error));
349         close(fd);
350         return error;
351     }
352
353     retval = set_nonblocking(fd);
354     if (retval) {
355         close(fd);
356         return retval;
357     }
358
359     ptcp = xmalloc(sizeof *ptcp);
360     ptcp->vconn.class = &ptcp_vconn_class;
361     ptcp->vconn.connect_status = 0;
362     ptcp->fd = fd;
363     *vconnp = &ptcp->vconn;
364     return 0;
365 }
366
367 static void
368 ptcp_close(struct vconn *vconn)
369 {
370     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
371     close(ptcp->fd);
372     free(ptcp);
373 }
374
375 static int
376 ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp)
377 {
378     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
379     int new_fd;
380     int error;
381
382     new_fd = accept(ptcp->fd, NULL, NULL);
383     if (new_fd < 0) {
384         int error = errno;
385         if (error != EAGAIN) {
386             VLOG_DBG("accept: %s", strerror(error));
387         }
388         return error;
389     }
390
391     error = set_nonblocking(new_fd);
392     if (error) {
393         close(new_fd);
394         return error;
395     }
396
397     return new_tcp_vconn("tcp" /* FIXME */, new_fd, 0, new_vconnp);
398 }
399
400 static void
401 ptcp_wait(struct vconn *vconn, enum vconn_wait_type wait)
402 {
403     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
404     assert(wait == WAIT_ACCEPT);
405     poll_fd_wait(ptcp->fd, POLLIN, NULL);
406 }
407
408 struct vconn_class ptcp_vconn_class = {
409     .name = "ptcp",
410     .open = ptcp_open,
411     .close = ptcp_close,
412     .accept = ptcp_accept,
413     .wait = ptcp_wait
414 };
415