rconn: Wake up immediately if we drain the send queue.
[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 <errno.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include "packets.h"
45 #include "socket-util.h"
46 #include "util.h"
47 #include "openflow.h"
48 #include "vconn-stream.h"
49
50 #include "vlog.h"
51 #define THIS_MODULE VLM_vconn_tcp
52
53 /* Active TCP. */
54
55 static int
56 new_tcp_vconn(const char *name, int fd, int connect_status,
57               const struct sockaddr_in *sin, struct vconn **vconnp)
58 {
59     int on = 1;
60     int retval;
61
62     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
63     if (retval) {
64         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
65         close(fd);
66         return errno;
67     }
68
69     return new_stream_vconn(name, fd, connect_status, sin->sin_addr.s_addr,
70                             vconnp);
71 }
72
73 static int
74 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
75 {
76     char *save_ptr;
77     const char *host_name;
78     const char *port_string;
79     struct sockaddr_in sin;
80     int retval;
81     int fd;
82
83     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
84      * can cause segfaults here:
85      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
86      * Using "::" instead of the obvious ":" works around it. */
87     host_name = strtok_r(suffix, "::", &save_ptr);
88     port_string = strtok_r(NULL, "::", &save_ptr);
89     if (!host_name) {
90         error(0, "%s: bad peer name format", name);
91         return EAFNOSUPPORT;
92     }
93
94     memset(&sin, 0, sizeof sin);
95     sin.sin_family = AF_INET;
96     if (lookup_ip(host_name, &sin.sin_addr)) {
97         return ENOENT;
98     }
99     sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
100
101     fd = socket(AF_INET, SOCK_STREAM, 0);
102     if (fd < 0) {
103         VLOG_ERR("%s: socket: %s", name, strerror(errno));
104         return errno;
105     }
106
107     retval = set_nonblocking(fd);
108     if (retval) {
109         close(fd);
110         return retval;
111     }
112
113     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
114     if (retval < 0) {
115         if (errno == EINPROGRESS) {
116             return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
117         } else {
118             int error = errno;
119             VLOG_ERR("%s: connect: %s", name, strerror(error));
120             close(fd);
121             return error;
122         }
123     } else {
124         return new_tcp_vconn(name, fd, 0, &sin, vconnp);
125     }
126 }
127
128 struct vconn_class tcp_vconn_class = {
129     .name = "tcp",
130     .open = tcp_open,
131 };
132 \f
133 /* Passive TCP. */
134
135 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
136                        struct vconn **vconnp);
137
138 static int
139 ptcp_open(const char *name, char *suffix, struct vconn **vconnp)
140 {
141     struct sockaddr_in sin;
142     int retval;
143     int fd;
144     unsigned int yes  = 1;
145
146     fd = socket(AF_INET, SOCK_STREAM, 0);
147     if (fd < 0) {
148         VLOG_ERR("%s: socket: %s", name, strerror(errno));
149         return errno;
150     }
151
152     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
153         VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
154         return errno;
155     }
156
157     memset(&sin, 0, sizeof sin);
158     sin.sin_family = AF_INET;
159     sin.sin_addr.s_addr = htonl(INADDR_ANY);
160     sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
161     retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
162     if (retval < 0) {
163         int error = errno;
164         VLOG_ERR("%s: bind: %s", name, strerror(error));
165         close(fd);
166         return error;
167     }
168
169     return new_pstream_vconn("ptcp", fd, ptcp_accept, vconnp);
170 }
171
172 static int
173 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
174             struct vconn **vconnp)
175 {
176     const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
177     char name[128];
178
179     if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
180         sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
181         if (sin->sin_port != htons(OFP_TCP_PORT)) {
182             sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
183         }
184     } else {
185         strcpy(name, "tcp");
186     }
187     return new_tcp_vconn(name, fd, 0, sin, vconnp);
188 }
189
190 struct vconn_class ptcp_vconn_class = {
191     .name = "ptcp",
192     .open = ptcp_open,
193 };
194