datapath: Return EFBIG instead of EXFULL when no room in flow table
[sliver-openvswitch.git] / lib / vconn-tcp.c
1 /*
2  * Copyright (c) 2008, 2009 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "vconn.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include "packets.h"
28 #include "socket-util.h"
29 #include "util.h"
30 #include "openflow/openflow.h"
31 #include "vconn-provider.h"
32 #include "vconn-stream.h"
33
34 #include "vlog.h"
35 #define THIS_MODULE VLM_vconn_tcp
36
37 /* Active TCP. */
38
39 static int
40 new_tcp_vconn(const char *name, int fd, int connect_status,
41               const struct sockaddr_in *remote, struct vconn **vconnp)
42 {
43     struct sockaddr_in local;
44     socklen_t local_len = sizeof local;
45     int on = 1;
46     int retval;
47
48     /* Get the local IP and port information */
49     retval = getsockname(fd, (struct sockaddr *)&local, &local_len);
50     if (retval) {
51         memset(&local, 0, sizeof local);
52     }
53
54     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
55     if (retval) {
56         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
57         close(fd);
58         return errno;
59     }
60
61     retval = new_stream_vconn(name, fd, connect_status, true, vconnp);
62     if (!retval) {
63         struct vconn *vconn = *vconnp;
64         vconn_set_remote_ip(vconn, remote->sin_addr.s_addr);
65         vconn_set_remote_port(vconn, remote->sin_port);
66         vconn_set_local_ip(vconn, local.sin_addr.s_addr);
67         vconn_set_local_port(vconn, local.sin_port);
68     }
69     return retval;
70 }
71
72 static int
73 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
74 {
75     char *save_ptr = NULL;
76     const char *host_name;
77     const char *port_string;
78     struct sockaddr_in sin;
79     int retval;
80     int fd;
81
82     host_name = strtok_r(suffix, ":", &save_ptr);
83     port_string = strtok_r(NULL, ":", &save_ptr);
84     if (!host_name) {
85         ovs_error(0, "%s: bad peer name format", name);
86         return EAFNOSUPPORT;
87     }
88
89     memset(&sin, 0, sizeof sin);
90     sin.sin_family = AF_INET;
91     if (lookup_ip(host_name, &sin.sin_addr)) {
92         return ENOENT;
93     }
94     sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
95
96     fd = socket(AF_INET, SOCK_STREAM, 0);
97     if (fd < 0) {
98         VLOG_ERR("%s: socket: %s", name, strerror(errno));
99         return errno;
100     }
101
102     retval = set_nonblocking(fd);
103     if (retval) {
104         close(fd);
105         return retval;
106     }
107
108     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
109     if (retval < 0) {
110         if (errno == EINPROGRESS) {
111             return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
112         } else {
113             int error = errno;
114             VLOG_ERR("%s: connect: %s", name, strerror(error));
115             close(fd);
116             return error;
117         }
118     } else {
119         return new_tcp_vconn(name, fd, 0, &sin, vconnp);
120     }
121 }
122
123 struct vconn_class tcp_vconn_class = {
124     "tcp",                      /* name */
125     tcp_open,                   /* open */
126     NULL,                       /* close */
127     NULL,                       /* connect */
128     NULL,                       /* recv */
129     NULL,                       /* send */
130     NULL,                       /* wait */
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 pvconn **pvconnp)
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_pvconn("ptcp", fd, ptcp_accept, pvconnp);
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 pvconn_class ptcp_pvconn_class = {
191     "ptcp",
192     ptcp_open,
193     NULL,
194     NULL,
195     NULL
196 };
197