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;
157 tcp_postpoll(struct vconn *vconn, short int *revents)
159 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
160 if (*revents & POLLOUT && tcp->txbuf) {
161 ssize_t n = write(tcp->fd, tcp->txbuf->data, tcp->txbuf->size);
163 if (errno != EAGAIN) {
164 VLOG_ERR("send: %s", strerror(errno));
168 buffer_pull(tcp->txbuf, n);
169 if (tcp->txbuf->size == 0) {
170 buffer_delete(tcp->txbuf);
175 *revents &= ~POLLOUT;
181 tcp_recv(struct vconn *vconn, struct buffer **bufferp)
183 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
188 if (tcp->rxbuf == NULL) {
189 tcp->rxbuf = buffer_new(1564);
194 if (sizeof(struct ofp_header) > rx->size) {
195 want_bytes = sizeof(struct ofp_header) - rx->size;
197 struct ofp_header *oh = rx->data;
198 size_t length = ntohs(oh->length);
199 if (length < sizeof(struct ofp_header)) {
200 VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
203 want_bytes = length - rx->size;
205 buffer_reserve_tailroom(rx, want_bytes);
207 retval = read(tcp->fd, buffer_tail(rx), want_bytes);
210 if (retval == want_bytes) {
211 if (rx->size > sizeof(struct ofp_header)) {
220 } else if (retval == 0) {
221 return rx->size ? EPROTO : EOF;
223 return retval ? errno : EAGAIN;
228 tcp_send(struct vconn *vconn, struct buffer *buffer)
230 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
237 retval = write(tcp->fd, buffer->data, buffer->size);
238 if (retval == buffer->size) {
239 buffer_delete(buffer);
241 } else if (retval >= 0 || errno == EAGAIN) {
244 buffer_pull(buffer, retval);
252 struct vconn_class tcp_vconn_class = {
256 .prepoll = tcp_prepoll,
257 .postpoll = tcp_postpoll,
270 static struct ptcp_vconn *
271 ptcp_vconn_cast(struct vconn *vconn)
273 assert(vconn->class == &ptcp_vconn_class);
274 return CONTAINER_OF(vconn, struct ptcp_vconn, vconn);
278 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
280 struct sockaddr_in sin;
281 struct ptcp_vconn *ptcp;
284 unsigned int yes = 1;
286 fd = socket(AF_INET, SOCK_STREAM, 0);
288 VLOG_ERR("%s: socket: %s", name, strerror(errno));
292 if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
293 VLOG_ERR("%s: setsockopt::SO_REUSEADDR: %s", name, strerror(errno));
298 memset(&sin, 0, sizeof sin);
299 sin.sin_family = AF_INET;
300 sin.sin_addr.s_addr = htonl(INADDR_ANY);
301 sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
302 retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
305 VLOG_ERR("%s: bind: %s", name, strerror(error));
310 retval = listen(fd, 10);
313 VLOG_ERR("%s: listen: %s", name, strerror(error));
318 retval = set_nonblocking(fd);
320 VLOG_ERR("%s: set_nonblocking: %s", name, strerror(retval));
325 ptcp = xmalloc(sizeof *ptcp);
326 ptcp->vconn.class = &ptcp_vconn_class;
328 *vconnp = &ptcp->vconn;
333 ptcp_close(struct vconn *vconn)
335 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
341 ptcp_prepoll(struct vconn *vconn, int want, struct pollfd *pfd)
343 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
345 if (want & WANT_ACCEPT) {
346 pfd->events |= POLLIN;
352 ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp)
354 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
357 new_fd = accept(ptcp->fd, NULL, NULL);
362 return new_tcp_vconn("tcp" /* FIXME */, new_fd, new_vconnp);
365 struct vconn_class ptcp_vconn_class = {
369 .prepoll = ptcp_prepoll,
370 .accept = ptcp_accept,