On Windows platform, TCP_NODELAY can only be set when TCP is established.
(This is an observed behavior and not written in any MSDN documentation.)
The current code does not create any problems while running unit tests
(because connections get established immediately) but is reportedly
observed while connecting to a different machine.
commit
8b76839(Move setsockopt TCP_NODELAY to when TCP is connected.)
made changes to call setsockopt with TCP_NODELAY after TCP is connected
only in lib/stream-ssl.c. We need the same change for stream-tcp too and
this commit does that.
Currently, a failure of setting TCP_NODELAY results in reporting
the error and then closing the socket. This commit changes that
behavior such that an error is reported if setting TCP_NODELAY
fails, but the connection itself is not torn down.
Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
#include <fcntl.h>
#include <net/if.h>
#include <netdb.h>
+#include <netinet/tcp.h>
#include <poll.h>
#include <stddef.h>
#include <stdio.h>
}
}
+void
+setsockopt_tcp_nodelay(int fd)
+{
+ int on = 1;
+ int retval;
+
+ retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
+ if (retval) {
+ retval = sock_errno();
+ VLOG_ERR("setsockopt(TCP_NODELAY): %s", sock_strerror(retval));
+ }
+}
+
int
set_dscp(int fd, uint8_t dscp)
{
int set_nonblocking(int fd);
void xset_nonblocking(int fd);
+void setsockopt_tcp_nodelay(int fd);
int set_dscp(int fd, uint8_t dscp);
int lookup_ip(const char *host_name, struct in_addr *address);
{
struct stream stream;
int fd;
+ int fd_type;
};
static const struct stream_class stream_fd_class;
/* Creates a new stream named 'name' that will send and receive data on 'fd'
* and stores a pointer to the stream in '*streamp'. Initial connection status
- * 'connect_status' is interpreted as described for stream_init().
+ * 'connect_status' is interpreted as described for stream_init(). 'fd_type'
+ * tells whether the socket is TCP or Unix domain socket.
*
* Returns 0 if successful, otherwise a positive errno value. (The current
* implementation never fails.) */
int
-new_fd_stream(const char *name, int fd, int connect_status,
+new_fd_stream(const char *name, int fd, int connect_status, int fd_type,
struct stream **streamp)
{
struct stream_fd *s;
s = xmalloc(sizeof *s);
stream_init(&s->stream, &stream_fd_class, connect_status, name);
s->fd = fd;
+ s->fd_type = fd_type;
*streamp = &s->stream;
return 0;
}
fd_connect(struct stream *stream)
{
struct stream_fd *s = stream_fd_cast(stream);
- return check_connection_completion(s->fd);
+ int retval = check_connection_completion(s->fd);
+ if (retval == 0 && s->fd_type == AF_INET) {
+ setsockopt_tcp_nodelay(s->fd);
+ }
+ return retval;
}
static ssize_t
struct sockaddr_storage;
int new_fd_stream(const char *name, int fd, int connect_status,
- struct stream **streamp);
+ int fd_type, struct stream **streamp);
int new_fd_pstream(const char *name, int fd,
int (*accept_cb)(int fd, const struct sockaddr_storage *ss,
size_t ss_len, struct stream **),
}
}
-static int
-setsockopt_tcp_nodelay(int fd)
-{
- int on = 1;
- int retval;
-
- retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
- if (retval) {
- retval = sock_errno();
- VLOG_ERR("setsockopt(TCP_NODELAY): %s", sock_strerror(retval));
- }
- return retval;
-}
-
static int
new_ssl_stream(const char *name, int fd, enum session_type type,
enum ssl_state state, struct stream **streamp)
* On windows platforms, this can only be called upon TCP connected.
*/
if (state == STATE_SSL_CONNECTING) {
- retval = setsockopt_tcp_nodelay(fd);
- if (retval) {
- goto error;
- }
+ setsockopt_tcp_nodelay(fd);
}
/* Create and configure OpenSSL stream. */
return retval;
}
sslv->state = STATE_SSL_CONNECTING;
- retval = setsockopt_tcp_nodelay(sslv->fd);
- if (retval) {
- return retval;
- }
+ setsockopt_tcp_nodelay(sslv->fd);
/* Fall through. */
case STATE_SSL_CONNECTING:
#include <inttypes.h>
#include <sys/types.h>
#include <netinet/in.h>
-#include <netinet/tcp.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
new_tcp_stream(const char *name, int fd, int connect_status,
struct stream **streamp)
{
- int on = 1;
- int retval;
-
- retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
- if (retval) {
- int error = sock_errno();
- VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s",
- name, sock_strerror(error));
- closesocket(fd);
- return error;
+ if (connect_status == 0) {
+ setsockopt_tcp_nodelay(fd);
}
- return new_fd_stream(name, fd, connect_status, streamp);
+ return new_fd_stream(name, fd, connect_status, AF_INET, streamp);
}
static int
}
free(connect_path);
- return new_fd_stream(name, fd, check_connection_completion(fd), streamp);
+ return new_fd_stream(name, fd, check_connection_completion(fd),
+ AF_UNIX, streamp);
}
const struct stream_class unix_stream_class = {
} else {
strcpy(name, "unix");
}
- return new_fd_stream(name, fd, 0, streamp);
+ return new_fd_stream(name, fd, 0, AF_UNIX, streamp);
}
const struct pstream_class punix_pstream_class = {