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