1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
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:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
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
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.
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
47 #include "socket-util.h"
50 #include "ofp-print.h"
52 #include "poll-loop.h"
55 #define THIS_MODULE VLM_vconn_tcp
65 struct poll_waiter *tx_waiter;
69 new_tcp_vconn(const char *name, int fd, int connect_status,
70 const struct sockaddr_in *sin, struct vconn **vconnp)
72 struct tcp_vconn *tcp;
76 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
78 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
83 tcp = xmalloc(sizeof *tcp);
84 tcp->vconn.class = &tcp_vconn_class;
85 tcp->vconn.connect_status = connect_status;
86 tcp->vconn.ip = sin->sin_addr.s_addr;
89 tcp->tx_waiter = NULL;
91 *vconnp = &tcp->vconn;
95 static struct tcp_vconn *
96 tcp_vconn_cast(struct vconn *vconn)
98 assert(vconn->class == &tcp_vconn_class);
99 return CONTAINER_OF(vconn, struct tcp_vconn, vconn);
104 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
107 const char *host_name;
108 const char *port_string;
109 struct sockaddr_in sin;
113 /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
114 * can cause segfaults here:
115 * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
116 * Using "::" instead of the obvious ":" works around it. */
117 host_name = strtok_r(suffix, "::", &save_ptr);
118 port_string = strtok_r(NULL, "::", &save_ptr);
120 fatal(0, "%s: bad peer name format", name);
123 memset(&sin, 0, sizeof sin);
124 sin.sin_family = AF_INET;
125 if (lookup_ip(host_name, &sin.sin_addr)) {
128 sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
130 fd = socket(AF_INET, SOCK_STREAM, 0);
132 VLOG_ERR("%s: socket: %s", name, strerror(errno));
136 retval = set_nonblocking(fd);
142 retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
144 if (errno == EINPROGRESS) {
145 return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
148 VLOG_ERR("%s: connect: %s", name, strerror(error));
153 return new_tcp_vconn(name, fd, 0, &sin, vconnp);
158 tcp_close(struct vconn *vconn)
160 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
161 poll_cancel(tcp->tx_waiter);
167 tcp_connect(struct vconn *vconn)
169 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
170 return check_connection_completion(tcp->fd);
174 tcp_recv(struct vconn *vconn, struct buffer **bufferp)
176 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
181 if (tcp->rxbuf == NULL) {
182 tcp->rxbuf = buffer_new(1564);
187 if (sizeof(struct ofp_header) > rx->size) {
188 want_bytes = sizeof(struct ofp_header) - rx->size;
190 struct ofp_header *oh = rx->data;
191 size_t length = ntohs(oh->length);
192 if (length < sizeof(struct ofp_header)) {
193 VLOG_ERR("received too-short ofp_header (%zu bytes)", length);
196 want_bytes = length - rx->size;
203 buffer_reserve_tailroom(rx, want_bytes);
205 retval = read(tcp->fd, buffer_tail(rx), want_bytes);
208 if (retval == want_bytes) {
209 if (rx->size > sizeof(struct ofp_header)) {
218 } else if (retval == 0) {
220 VLOG_ERR("connection dropped mid-packet");
226 return retval ? errno : EAGAIN;
231 tcp_clear_txbuf(struct tcp_vconn *tcp)
233 buffer_delete(tcp->txbuf);
235 tcp->tx_waiter = NULL;
239 tcp_do_tx(int fd UNUSED, short int revents UNUSED, void *vconn_)
241 struct vconn *vconn = vconn_;
242 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
243 ssize_t n = write(tcp->fd, tcp->txbuf->data, tcp->txbuf->size);
245 if (errno != EAGAIN) {
246 VLOG_ERR("send: %s", strerror(errno));
247 tcp_clear_txbuf(tcp);
251 buffer_pull(tcp->txbuf, n);
252 if (!tcp->txbuf->size) {
253 tcp_clear_txbuf(tcp);
257 tcp->tx_waiter = poll_fd_callback(tcp->fd, POLLOUT, tcp_do_tx, vconn);
261 tcp_send(struct vconn *vconn, struct buffer *buffer)
263 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
270 retval = write(tcp->fd, buffer->data, buffer->size);
271 if (retval == buffer->size) {
272 buffer_delete(buffer);
274 } else if (retval >= 0 || errno == EAGAIN) {
277 buffer_pull(buffer, retval);
279 tcp->tx_waiter = poll_fd_callback(tcp->fd, POLLOUT, tcp_do_tx, vconn);
287 tcp_wait(struct vconn *vconn, enum vconn_wait_type wait)
289 struct tcp_vconn *tcp = tcp_vconn_cast(vconn);
292 poll_fd_wait(tcp->fd, POLLOUT);
297 poll_fd_wait(tcp->fd, POLLOUT);
299 /* Nothing to do: need to drain txbuf first. */
304 poll_fd_wait(tcp->fd, POLLIN);
312 struct vconn_class tcp_vconn_class = {
316 .connect = tcp_connect,
330 static struct ptcp_vconn *
331 ptcp_vconn_cast(struct vconn *vconn)
333 assert(vconn->class == &ptcp_vconn_class);
334 return CONTAINER_OF(vconn, struct ptcp_vconn, vconn);
338 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
340 struct sockaddr_in sin;
341 struct ptcp_vconn *ptcp;
344 unsigned int yes = 1;
346 fd = socket(AF_INET, SOCK_STREAM, 0);
348 VLOG_ERR("%s: socket: %s", name, strerror(errno));
352 if ( setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
353 VLOG_ERR("%s: setsockopt::SO_REUSEADDR: %s", name, strerror(errno));
358 memset(&sin, 0, sizeof sin);
359 sin.sin_family = AF_INET;
360 sin.sin_addr.s_addr = htonl(INADDR_ANY);
361 sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
362 retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
365 VLOG_ERR("%s: bind: %s", name, strerror(error));
370 retval = listen(fd, 10);
373 VLOG_ERR("%s: listen: %s", name, strerror(error));
378 retval = set_nonblocking(fd);
384 ptcp = xmalloc(sizeof *ptcp);
385 ptcp->vconn.class = &ptcp_vconn_class;
386 ptcp->vconn.connect_status = 0;
388 *vconnp = &ptcp->vconn;
393 ptcp_close(struct vconn *vconn)
395 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
401 ptcp_accept(struct vconn *vconn, struct vconn **new_vconnp)
403 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
404 struct sockaddr_in sin;
405 socklen_t sin_len = sizeof sin;
410 new_fd = accept(ptcp->fd, &sin, &sin_len);
413 if (error != EAGAIN) {
414 VLOG_DBG("accept: %s", strerror(error));
419 error = set_nonblocking(new_fd);
425 sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin.sin_addr));
426 if (sin.sin_port != htons(OFP_TCP_PORT)) {
427 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin.sin_port));
429 return new_tcp_vconn(name, new_fd, 0, &sin, new_vconnp);
433 ptcp_wait(struct vconn *vconn, enum vconn_wait_type wait)
435 struct ptcp_vconn *ptcp = ptcp_vconn_cast(vconn);
436 assert(wait == WAIT_ACCEPT);
437 poll_fd_wait(ptcp->fd, POLLIN);
440 struct vconn_class ptcp_vconn_class = {
444 .accept = ptcp_accept,