Initial import
[sliver-openvswitch.git] / 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
39 #include "vlog.h"
40 #define THIS_MODULE VLM_vconn_tcp
41
42 /* Active TCP. */
43
44 struct tcp_vconn
45 {
46     struct vconn vconn;
47     int fd;
48     struct buffer *rxbuf;
49     struct buffer *txbuf;
50 };
51
52 static int
53 new_tcp_vconn(const char *name, int fd, struct vconn **vconnp) 
54 {
55     struct tcp_vconn *tcp;
56     int on = 1;
57     int retval;
58
59     retval = set_nonblocking(fd);
60     if (retval) {
61         VLOG_ERR("%s: set_nonblocking: %s", name, strerror(retval));
62         close(fd);
63         return retval;
64     }
65
66     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
67     if (retval) {
68         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
69         close(fd);
70         return errno;
71     }
72
73     tcp = xmalloc(sizeof *tcp);
74     tcp->vconn.class = &tcp_vconn_class;
75     tcp->fd = fd;
76     tcp->txbuf = NULL;
77     tcp->rxbuf = NULL;
78     *vconnp = &tcp->vconn;
79     return 0;
80 }
81
82 static struct tcp_vconn *
83 tcp_vconn_cast(struct vconn *vconn) 
84 {
85     assert(vconn->class == &tcp_vconn_class);
86     return CONTAINER_OF(vconn, struct tcp_vconn, vconn); 
87 }
88
89
90 static int
91 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
92 {
93     char *save_ptr;
94     const char *host_name;
95     const char *port_string;
96     struct sockaddr_in sin;
97     int retval;
98     int fd;
99
100     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
101      * can cause segfaults here:
102      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
103      * Using "::" instead of the obvious ":" works around it. */
104     host_name = strtok_r(suffix, "::", &save_ptr);
105     port_string = strtok_r(NULL, "::", &save_ptr);
106     if (!host_name) {
107         fatal(0, "%s: bad peer name format", name);
108     }
109
110     memset(&sin, 0, sizeof sin);
111     sin.sin_family = AF_INET;
112     if (lookup_ip(host_name, &sin.sin_addr)) {
113         return ENOENT;
114     }
115     sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
116
117     fd = socket(AF_INET, SOCK_STREAM, 0);
118     if (fd < 0) {
119         VLOG_ERR("%s: socket: %s", name, strerror(errno));
120         return errno;
121     }
122
123     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
124     if (retval < 0) {
125         int error = errno;
126         VLOG_ERR("%s: connect: %s", name, strerror(error));
127         close(fd);
128         return error;
129     }
130
131     return new_tcp_vconn(name, fd, vconnp);
132 }
133
134 static void
135 tcp_close(struct vconn *vconn) 
136 {
137     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
138     close(tcp->fd);
139     free(tcp);
140 }
141
142 static void
143 tcp_prepoll(struct vconn *vconn, int want, struct pollfd *pfd) 
144 {
145     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
146     pfd->fd = tcp->fd;
147     if (want & WANT_RECV) {
148         pfd->events |= POLLIN;
149     }
150     if (want & WANT_SEND || tcp->txbuf) {
151         pfd->events |= POLLOUT;
152     }
153 }
154
155 static void
156 tcp_postpoll(struct vconn *vconn, short int *revents)
157 {
158     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
159     if (*revents & POLLOUT && tcp->txbuf) {
160         ssize_t n = write(tcp->fd, tcp->txbuf->data, tcp->txbuf->size);
161         if (n < 0) {
162             if (errno != EAGAIN) {
163                 VLOG_ERR("send: %s", strerror(errno));
164                 *revents |= POLLERR;
165             }
166         } else if (n > 0) {
167             buffer_pull(tcp->txbuf, n);
168             if (tcp->txbuf->size == 0) {
169                 buffer_delete(tcp->txbuf);
170                 tcp->txbuf = NULL;
171             }
172         }
173         if (tcp->txbuf) {
174             *revents &= ~POLLOUT;
175         }
176     }
177 }
178
179 static int
180 tcp_recv(struct vconn *vconn, struct buffer **bufferp)
181 {
182     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
183     struct buffer *rx;
184     size_t want_bytes;
185     ssize_t retval;
186
187     if (tcp->rxbuf == NULL) {
188         tcp->rxbuf = buffer_new(1564);
189     }
190     rx = tcp->rxbuf;
191
192 again:
193     if (sizeof(struct ofp_header) > rx->size) {
194         want_bytes = sizeof(struct ofp_header) - rx->size;
195     } else {
196         struct ofp_header *oh = rx->data;
197         size_t length = ntohs(oh->length);
198         if (length < sizeof(struct ofp_header)) {
199             VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
200             return EPROTO;
201         }
202         want_bytes = length - rx->size;
203     }
204     buffer_reserve_tailroom(rx, want_bytes);
205
206     retval = read(tcp->fd, buffer_tail(rx), want_bytes);
207     if (retval > 0) {
208         rx->size += retval;
209         if (retval == want_bytes) {
210             if (rx->size > sizeof(struct ofp_header)) {
211                 *bufferp = rx;
212                 tcp->rxbuf = NULL;
213                 return 0;
214             } else {
215                 goto again;
216             }
217         }
218         return EAGAIN;
219     } else if (retval == 0) {
220         return rx->size ? EPROTO : EOF;
221     } else {
222         return retval ? errno : EAGAIN;
223     }
224 }
225
226 static int
227 tcp_send(struct vconn *vconn, struct buffer *buffer) 
228 {
229     struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
230     ssize_t retval;
231
232     if (tcp->txbuf) {
233         return EAGAIN;
234     }
235
236     retval = write(tcp->fd, buffer->data, buffer->size);
237     if (retval == buffer->size) {
238         buffer_delete(buffer);
239         return 0;
240     } else if (retval >= 0 || errno == EAGAIN) {
241         tcp->txbuf = buffer;
242         if (retval > 0) {
243             buffer_pull(buffer, retval);
244         }
245         return 0;
246     } else {
247         return errno;
248     }
249 }
250
251 struct vconn_class tcp_vconn_class = {
252     .name = "tcp",
253     .open = tcp_open,
254     .close = tcp_close,
255     .prepoll = tcp_prepoll,
256     .postpoll = tcp_postpoll,
257     .recv = tcp_recv,
258     .send = tcp_send,
259 };
260 \f
261 /* Passive TCP. */
262
263 struct ptcp_vconn
264 {
265     struct vconn vconn;
266     int fd;
267 };
268
269 static struct ptcp_vconn *
270 ptcp_vconn_cast(struct vconn *vconn) 
271 {
272     assert(vconn->class == &ptcp_vconn_class);
273     return CONTAINER_OF(vconn, struct ptcp_vconn, vconn); 
274 }
275
276 static int
277 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
278 {
279     struct sockaddr_in sin;
280     struct ptcp_vconn *ptcp;
281     int retval;
282     int fd;
283     unsigned int yes  = 1;
284
285     fd = socket(AF_INET, SOCK_STREAM, 0);
286     if (fd < 0) {
287         VLOG_ERR("%s: socket: %s", name, strerror(errno));
288         return errno;
289     }
290
291     if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
292         VLOG_ERR("%s: setsockopt::SO_REUSEADDR: %s", name, strerror(errno));
293         return errno;
294     }
295
296
297     memset(&sin, 0, sizeof sin);
298     sin.sin_family = AF_INET;
299     sin.sin_addr.s_addr = htonl(INADDR_ANY);
300     sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
301     retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
302     if (retval < 0) {
303         int error = errno;
304         VLOG_ERR("%s: bind: %s", name, strerror(error));
305         close(fd);
306         return error;
307     }
308
309     retval = listen(fd, 10);
310     if (retval < 0) {
311         int error = errno;
312         VLOG_ERR("%s: listen: %s", name, strerror(error));
313         close(fd);
314         return error;
315     }
316
317     retval = set_nonblocking(fd);
318     if (retval) {
319         VLOG_ERR("%s: set_nonblocking: %s", name, strerror(retval));
320         close(fd);
321         return retval;
322     }
323
324     ptcp = xmalloc(sizeof *ptcp);
325     ptcp->vconn.class = &ptcp_vconn_class;
326     ptcp->fd = fd;
327     *vconnp = &ptcp->vconn;
328     return 0;
329 }
330
331 static void
332 ptcp_close(struct vconn *vconn) 
333 {
334     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
335     close(ptcp->fd);
336     free(ptcp);
337 }
338
339 static void
340 ptcp_prepoll(struct vconn *vconn, int want, struct pollfd *pfd) 
341 {
342     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
343     pfd->fd = ptcp->fd;
344     if (want & WANT_ACCEPT) {
345         pfd->events |= POLLIN;
346     }
347 }
348
349 static int
350 ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp) 
351 {
352     struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
353     int new_fd;
354
355     new_fd = accept(ptcp->fd, NULL, NULL);
356     if (new_fd < 0) {
357         return errno;
358     }
359
360     return new_tcp_vconn("tcp" /* FIXME */, new_fd, new_vconnp);
361 }
362
363 struct vconn_class ptcp_vconn_class = {
364     .name = "ptcp",
365     .open = ptcp_open,
366     .close = ptcp_close,
367     .prepoll = ptcp_prepoll,
368     .accept = ptcp_accept,
369 };
370