From e9dc86ed9bc6b4bc387639619d552215a1e189b6 Mon Sep 17 00:00:00 2001 From: pran1990 Date: Mon, 28 Apr 2014 18:40:22 -0700 Subject: [PATCH] Enable configuring tcp keepalive Added a function set_tcp_keepalive in network_utils.py to set configuration of tcp keepalive parameters. Projects can call this function and set values in them, if needed. Also added unittests to test this function. Implements blueprint configure-tcp-keepalive Change-Id: I8b978a604f29953911c2fe4bad74e28e5ba74c35 --- openstack/common/network_utils.py | 62 +++++++++++++++++++++++++++++++ tests/unit/test_network_utils.py | 18 +++++++++ 2 files changed, 80 insertions(+) diff --git a/openstack/common/network_utils.py b/openstack/common/network_utils.py index 731bccb5..68e6d23a 100644 --- a/openstack/common/network_utils.py +++ b/openstack/common/network_utils.py @@ -17,8 +17,15 @@ Network-related utilities and helper functions. """ +import socket + from six.moves.urllib import parse +from openstack.common.gettextutils import _LW +from openstack.common import log as logging + +LOG = logging.getLogger(__name__) + def parse_host_port(address, default_port=None): """Interpret a string as a host:port pair. @@ -100,3 +107,58 @@ def urlsplit(url, scheme='', allow_fragments=True): path, query = path.split('?', 1) return ModifiedSplitResult(scheme, netloc, path, query, fragment) + + +def set_tcp_keepalive(sock, tcp_keepalive=True, + tcp_keepidle=None, + tcp_keepalive_interval=None, + tcp_keepalive_count=None): + """Set values for tcp keepalive parameters + + This function configures tcp keepalive parameters if users wish to do + so. + :param tcp_keepalive: Boolean, turn on or off tcp_keepalive. If users are + not sure, this should be True, and default values will be used. + + :param tcp_keepidle: time to wait before starting to send keepalive probes + + :param tcp_keepalive_interval: time between successive probes, once the + initial wait time is over + + :param tcp_keepalive_count: number of probes to send before the connection + is killed + """ + + # NOTE(praneshp): Despite keepalive being a tcp concept, the level is + # still SOL_SOCKET. This is a quirk. + if isinstance(tcp_keepalive, bool): + sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, tcp_keepalive) + else: + raise TypeError("tcp_keepalive must be a boolean") + + if not tcp_keepalive: + return + + # These options aren't available in the OS X version of eventlet, + # Idle + Count * Interval effectively gives you the total timeout. + if tcp_keepidle is not None: + if hasattr(socket, 'TCP_KEEPIDLE'): + sock.setsockopt(socket.IPPROTO_TCP, + socket.TCP_KEEPIDLE, + tcp_keepidle) + else: + LOG.warning(_LW('tcp_keepidle not available on your system')) + if tcp_keepalive_interval is not None: + if hasattr(socket, 'TCP_KEEPINTVL'): + sock.setsockopt(socket.IPPROTO_TCP, + socket.TCP_KEEPINTVL, + tcp_keepalive_interval) + else: + LOG.warning(_LW('tcp_keepintvl not available on your system')) + if tcp_keepalive_count is not None: + if hasattr(socket, 'TCP_KEEPCNT'): + sock.setsockopt(socket.IPPROTO_TCP, + socket.TCP_KEEPCNT, + tcp_keepalive_count) + else: + LOG.warning(_LW('tcp_keepknt not available on your system')) diff --git a/tests/unit/test_network_utils.py b/tests/unit/test_network_utils.py index 27b87b84..5865396f 100644 --- a/tests/unit/test_network_utils.py +++ b/tests/unit/test_network_utils.py @@ -13,6 +13,9 @@ # License for the specific language governing permissions and limitations # under the License. +import socket + +import mock from oslotest import base as test_base from openstack.common import network_utils @@ -99,3 +102,18 @@ class NetworkUtilsTest(test_base.BaseTestCase): self.assertEqual(result.port, 1234) self.assertEqual(result.query, 'ab') self.assertEqual(result.fragment, '12') + + def test_set_tcp_keepalive(self): + mock_sock = mock.Mock() + network_utils.set_tcp_keepalive(mock_sock, True, 100, 10, 5) + calls = [ + mock.call.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True), + mock.call.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 100), + mock.call.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 10), + mock.call.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5) + ] + mock_sock.assert_has_calls(calls) + + mock_sock.reset_mock() + network_utils.set_tcp_keepalive(mock_sock, False) + self.assertEqual(1, len(mock_sock.mock_calls))