1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
34 #include "socket-util.h"
37 #include "ofp-print.h"
40 #define THIS_MODULE VLM_vconn_tcp
53 new_tcp_vconn(const char *name, int fd, struct vconn **vconnp)
55 struct tcp_vconn *tcp;
59 retval = set_nonblocking(fd);
61 VLOG_ERR("%s: set_nonblocking: %s", name, strerror(retval));
66 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
68 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
73 tcp = xmalloc(sizeof *tcp);
74 tcp->vconn.class = &tcp_vconn_class;
78 *vconnp = &tcp->vconn;
82 static struct tcp_vconn *
83 tcp_vconn_cast(struct vconn *vconn)
85 assert(vconn->class == &tcp_vconn_class);
86 return CONTAINER_OF(vconn, struct tcp_vconn, vconn);
91 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
94 const char *host_name;
95 const char *port_string;
96 struct sockaddr_in sin;
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);
107 fatal(0, "%s: bad peer name format", name);
110 memset(&sin, 0, sizeof sin);
111 sin.sin_family = AF_INET;
112 if (lookup_ip(host_name, &sin.sin_addr)) {
115 sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
117 fd = socket(AF_INET, SOCK_STREAM, 0);
119 VLOG_ERR("%s: socket: %s", name, strerror(errno));
123 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
126 VLOG_ERR("%s: connect: %s", name, strerror(error));
131 return new_tcp_vconn(name, fd, vconnp);
135 tcp_close(struct vconn *vconn)
137 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
143 tcp_prepoll(struct vconn *vconn, int want, struct pollfd *pfd)
145 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
147 if (want & WANT_RECV) {
148 pfd->events |= POLLIN;
150 if (want & WANT_SEND || tcp->txbuf) {
151 pfd->events |= POLLOUT;
156 tcp_postpoll(struct vconn *vconn, short int *revents)
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);
162 if (errno != EAGAIN) {
163 VLOG_ERR("send: %s", strerror(errno));
167 buffer_pull(tcp->txbuf, n);
168 if (tcp->txbuf->size == 0) {
169 buffer_delete(tcp->txbuf);
174 *revents &= ~POLLOUT;
180 tcp_recv(struct vconn *vconn, struct buffer **bufferp)
182 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
187 if (tcp->rxbuf == NULL) {
188 tcp->rxbuf = buffer_new(1564);
193 if (sizeof(struct ofp_header) > rx->size) {
194 want_bytes = sizeof(struct ofp_header) - rx->size;
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);
202 want_bytes = length - rx->size;
204 buffer_reserve_tailroom(rx, want_bytes);
206 retval = read(tcp->fd, buffer_tail(rx), want_bytes);
209 if (retval == want_bytes) {
210 if (rx->size > sizeof(struct ofp_header)) {
219 } else if (retval == 0) {
220 return rx->size ? EPROTO : EOF;
222 return retval ? errno : EAGAIN;
227 tcp_send(struct vconn *vconn, struct buffer *buffer)
229 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
236 retval = write(tcp->fd, buffer->data, buffer->size);
237 if (retval == buffer->size) {
238 buffer_delete(buffer);
240 } else if (retval >= 0 || errno == EAGAIN) {
243 buffer_pull(buffer, retval);
251 struct vconn_class tcp_vconn_class = {
255 .prepoll = tcp_prepoll,
256 .postpoll = tcp_postpoll,
269 static struct ptcp_vconn *
270 ptcp_vconn_cast(struct vconn *vconn)
272 assert(vconn->class == &ptcp_vconn_class);
273 return CONTAINER_OF(vconn, struct ptcp_vconn, vconn);
277 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
279 struct sockaddr_in sin;
280 struct ptcp_vconn *ptcp;
283 unsigned int yes = 1;
285 fd = socket(AF_INET, SOCK_STREAM, 0);
287 VLOG_ERR("%s: socket: %s", name, strerror(errno));
291 if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
292 VLOG_ERR("%s: setsockopt::SO_REUSEADDR: %s", name, strerror(errno));
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);
304 VLOG_ERR("%s: bind: %s", name, strerror(error));
309 retval = listen(fd, 10);
312 VLOG_ERR("%s: listen: %s", name, strerror(error));
317 retval = set_nonblocking(fd);
319 VLOG_ERR("%s: set_nonblocking: %s", name, strerror(retval));
324 ptcp = xmalloc(sizeof *ptcp);
325 ptcp->vconn.class = &ptcp_vconn_class;
327 *vconnp = &ptcp->vconn;
332 ptcp_close(struct vconn *vconn)
334 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
340 ptcp_prepoll(struct vconn *vconn, int want, struct pollfd *pfd)
342 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
344 if (want & WANT_ACCEPT) {
345 pfd->events |= POLLIN;
350 ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp)
352 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
355 new_fd = accept(ptcp->fd, NULL, NULL);
360 return new_tcp_vconn("tcp" /* FIXME */, new_fd, new_vconnp);
363 struct vconn_class ptcp_vconn_class = {
367 .prepoll = ptcp_prepoll,
368 .accept = ptcp_accept,