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