From db13fbf4a996726bd634f050c98be7a0762b2822 Mon Sep 17 00:00:00 2001 From: Hiten P Date: Fri, 8 Jun 2012 18:04:40 +0100 Subject: [PATCH] Consolidate TCP-specific common code into their own files. The TCP keepalive tuning code has been moved into the newly added files; this also allows future TCP-specific code to be added into these files, without bloating the IP level code and establishes a known file structure for other IP-based transports. Remember: this is a no-op change, hence no API or functionality was changed as part of this commit. --- src/Makefile.am | 2 + src/ip.cpp | 83 ---------------------------- src/ip.hpp | 6 --- src/tcp.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++ src/tcp.hpp | 38 +++++++++++++ src/tcp_connecter.cpp | 1 + src/tcp_listener.cpp | 1 + 7 files changed, 164 insertions(+), 89 deletions(-) create mode 100644 src/tcp.cpp create mode 100644 src/tcp.hpp diff --git a/src/Makefile.am b/src/Makefile.am index c69a556d..fc00d9ee 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -65,6 +65,7 @@ libzmq_la_SOURCES = \ stdint.hpp \ stream_engine.hpp \ sub.hpp \ + tcp.hpp \ tcp_address.hpp \ tcp_connecter.hpp \ tcp_listener.hpp \ @@ -123,6 +124,7 @@ libzmq_la_SOURCES = \ socket_base.cpp \ stream_engine.cpp \ sub.cpp \ + tcp.cpp \ tcp_address.cpp \ tcp_connecter.cpp \ tcp_listener.cpp \ diff --git a/src/ip.cpp b/src/ip.cpp index 7e9ed58d..62ee7b25 100644 --- a/src/ip.cpp +++ b/src/ip.cpp @@ -66,89 +66,6 @@ zmq::fd_t zmq::open_socket (int domain_, int type_, int protocol_) return s; } -void zmq::tune_tcp_socket (fd_t s_) -{ - // Disable Nagle's algorithm. We are doing data batching on 0MQ level, - // so using Nagle wouldn't improve throughput in anyway, but it would - // hurt latency. - int nodelay = 1; - int rc = setsockopt (s_, IPPROTO_TCP, TCP_NODELAY, (char*) &nodelay, - sizeof (int)); -#ifdef ZMQ_HAVE_WINDOWS - wsa_assert (rc != SOCKET_ERROR); -#else - errno_assert (rc == 0); -#endif - -#ifdef ZMQ_HAVE_OPENVMS - // Disable delayed acknowledgements as they hurt latency is serious manner. - int nodelack = 1; - rc = setsockopt (s_, IPPROTO_TCP, TCP_NODELACK, (char*) &nodelack, - sizeof (int)); - errno_assert (rc != SOCKET_ERROR); -#endif -} - -void zmq::tune_tcp_keepalives (fd_t s_, int keepalive_, int keepalive_cnt_, int keepalive_idle_, int keepalive_intvl_) -{ - // Tuning TCP keep-alives if platform allows it - // All values = -1 means skip and leave it for OS -#ifdef ZMQ_HAVE_SO_KEEPALIVE - if (keepalive_ != -1) { - int rc = setsockopt (s_, SOL_SOCKET, SO_KEEPALIVE, (char*) &keepalive_, sizeof (int)); -#ifdef ZMQ_HAVE_WINDOWS - wsa_assert (rc != SOCKET_ERROR); -#else - errno_assert (rc == 0); -#endif - -#ifdef ZMQ_HAVE_TCP_KEEPCNT - if (keepalive_cnt_ != -1) { - int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPCNT, &keepalive_cnt_, sizeof (int)); -#ifdef ZMQ_HAVE_WINDOWS - wsa_assert (rc != SOCKET_ERROR); -#else - errno_assert (rc == 0); -#endif - } -#endif // ZMQ_HAVE_TCP_KEEPCNT - -#ifdef ZMQ_HAVE_TCP_KEEPIDLE - if (keepalive_idle_ != -1) { - int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPIDLE, &keepalive_idle_, sizeof (int)); -#ifdef ZMQ_HAVE_WINDOWS - wsa_assert (rc != SOCKET_ERROR); -#else - errno_assert (rc == 0); -#endif - } -#else // ZMQ_HAVE_TCP_KEEPIDLE -#ifdef ZMQ_HAVE_TCP_KEEPALIVE - if (keepalive_idle_ != -1) { - int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPALIVE, &keepalive_idle_, sizeof (int)); -#ifdef ZMQ_HAVE_WINDOWS - wsa_assert (rc != SOCKET_ERROR); -#else - errno_assert (rc == 0); -#endif - } -#endif // ZMQ_HAVE_TCP_KEEPALIVE -#endif // ZMQ_HAVE_TCP_KEEPIDLE - -#ifdef ZMQ_HAVE_TCP_KEEPINTVL - if (keepalive_intvl_ != -1) { - int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPINTVL, &keepalive_intvl_, sizeof (int)); -#ifdef ZMQ_HAVE_WINDOWS - wsa_assert (rc != SOCKET_ERROR); -#else - errno_assert (rc == 0); -#endif - } -#endif // ZMQ_HAVE_TCP_KEEPINTVL - } -#endif // ZMQ_HAVE_SO_KEEPALIVE -} - void zmq::unblock_socket (fd_t s_) { #ifdef ZMQ_HAVE_WINDOWS diff --git a/src/ip.hpp b/src/ip.hpp index 1cf4824f..3e35a336 100644 --- a/src/ip.hpp +++ b/src/ip.hpp @@ -30,12 +30,6 @@ namespace zmq // Same as socket(2), but allows for transparent tweaking the options. fd_t open_socket (int domain_, int type_, int protocol_); - // Tunes the supplied TCP socket for the best latency. - void tune_tcp_socket (fd_t s_); - - // Tunes TCP keep-alives - void tune_tcp_keepalives (fd_t s_, int keepalive_, int keepalive_cnt_, int keepalive_idle_, int keepalive_intvl_); - // Sets the socket into non-blocking mode. void unblock_socket (fd_t s_); diff --git a/src/tcp.cpp b/src/tcp.cpp new file mode 100644 index 00000000..17a2cb71 --- /dev/null +++ b/src/tcp.cpp @@ -0,0 +1,122 @@ +/* + Copyright (c) 2010-2011 250bpm s.r.o. + Copyright (c) 2007-2009 iMatix Corporation + Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file + + This file is part of 0MQ. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + 0MQ is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#include "ip.hpp" +#include "tcp.hpp" +#include "err.hpp" +#include "platform.hpp" + +#if defined ZMQ_HAVE_WINDOWS +#include "windows.hpp" +#else +#include +#include +#include +#include +#include +#endif + +#if defined ZMQ_HAVE_OPENVMS +#include +#endif + +void zmq::tune_tcp_socket (fd_t s_) +{ + // Disable Nagle's algorithm. We are doing data batching on 0MQ level, + // so using Nagle wouldn't improve throughput in anyway, but it would + // hurt latency. + int nodelay = 1; + int rc = setsockopt (s_, IPPROTO_TCP, TCP_NODELAY, (char*) &nodelay, + sizeof (int)); +#ifdef ZMQ_HAVE_WINDOWS + wsa_assert (rc != SOCKET_ERROR); +#else + errno_assert (rc == 0); +#endif + +#ifdef ZMQ_HAVE_OPENVMS + // Disable delayed acknowledgements as they hurt latency is serious manner. + int nodelack = 1; + rc = setsockopt (s_, IPPROTO_TCP, TCP_NODELACK, (char*) &nodelack, + sizeof (int)); + errno_assert (rc != SOCKET_ERROR); +#endif +} + +void zmq::tune_tcp_keepalives (fd_t s_, int keepalive_, int keepalive_cnt_, int keepalive_idle_, int keepalive_intvl_) +{ + // Tuning TCP keep-alives if platform allows it + // All values = -1 means skip and leave it for OS +#ifdef ZMQ_HAVE_SO_KEEPALIVE + if (keepalive_ != -1) { + int rc = setsockopt (s_, SOL_SOCKET, SO_KEEPALIVE, (char*) &keepalive_, sizeof (int)); +#ifdef ZMQ_HAVE_WINDOWS + wsa_assert (rc != SOCKET_ERROR); +#else + errno_assert (rc == 0); +#endif + +#ifdef ZMQ_HAVE_TCP_KEEPCNT + if (keepalive_cnt_ != -1) { + int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPCNT, &keepalive_cnt_, sizeof (int)); +#ifdef ZMQ_HAVE_WINDOWS + wsa_assert (rc != SOCKET_ERROR); +#else + errno_assert (rc == 0); +#endif + } +#endif // ZMQ_HAVE_TCP_KEEPCNT + +#ifdef ZMQ_HAVE_TCP_KEEPIDLE + if (keepalive_idle_ != -1) { + int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPIDLE, &keepalive_idle_, sizeof (int)); +#ifdef ZMQ_HAVE_WINDOWS + wsa_assert (rc != SOCKET_ERROR); +#else + errno_assert (rc == 0); +#endif + } +#else // ZMQ_HAVE_TCP_KEEPIDLE +#ifdef ZMQ_HAVE_TCP_KEEPALIVE + if (keepalive_idle_ != -1) { + int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPALIVE, &keepalive_idle_, sizeof (int)); +#ifdef ZMQ_HAVE_WINDOWS + wsa_assert (rc != SOCKET_ERROR); +#else + errno_assert (rc == 0); +#endif + } +#endif // ZMQ_HAVE_TCP_KEEPALIVE +#endif // ZMQ_HAVE_TCP_KEEPIDLE + +#ifdef ZMQ_HAVE_TCP_KEEPINTVL + if (keepalive_intvl_ != -1) { + int rc = setsockopt (s_, IPPROTO_TCP, TCP_KEEPINTVL, &keepalive_intvl_, sizeof (int)); +#ifdef ZMQ_HAVE_WINDOWS + wsa_assert (rc != SOCKET_ERROR); +#else + errno_assert (rc == 0); +#endif + } +#endif // ZMQ_HAVE_TCP_KEEPINTVL + } +#endif // ZMQ_HAVE_SO_KEEPALIVE +} diff --git a/src/tcp.hpp b/src/tcp.hpp new file mode 100644 index 00000000..55989410 --- /dev/null +++ b/src/tcp.hpp @@ -0,0 +1,38 @@ +/* + Copyright (c) 2010-2011 250bpm s.r.o. + Copyright (c) 2007-2009 iMatix Corporation + Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file + + This file is part of 0MQ. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + 0MQ is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef __ZMQ_TCP_HPP_INCLUDED__ +#define __ZMQ_TCP_HPP_INCLUDED__ + +#include "fd.hpp" + +namespace zmq +{ + + // Tunes the supplied TCP socket for the best latency. + void tune_tcp_socket (fd_t s_); + + // Tunes TCP keep-alives + void tune_tcp_keepalives (fd_t s_, int keepalive_, int keepalive_cnt_, int keepalive_idle_, int keepalive_intvl_); + +} + +#endif diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp index 504268a1..407490c3 100644 --- a/src/tcp_connecter.cpp +++ b/src/tcp_connecter.cpp @@ -29,6 +29,7 @@ #include "random.hpp" #include "err.hpp" #include "ip.hpp" +#include "tcp.hpp" #include "address.hpp" #include "tcp_address.hpp" #include "session_base.hpp" diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index 789deca8..173a22f1 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -31,6 +31,7 @@ #include "config.hpp" #include "err.hpp" #include "ip.hpp" +#include "tcp.hpp" #include "socket_base.hpp" #ifdef ZMQ_HAVE_WINDOWS