From 82fddfbb651911c7cb6818428ff7604c9745499c Mon Sep 17 00:00:00 2001 From: sridhargaddam Date: Mon, 22 Jun 2015 14:13:56 +0000 Subject: [PATCH] Fix ip_lib get_gateway for default gateway on an iface Currently get_gateway() of IpRouteCommand class always assumes the presence of gateway_ip when default_route is seen. Since we can also have interface routes without the gw_ip, this patch fixes the issue by parsing the output accordingly. Closes-Bug: #1467531 Change-Id: Icf988994b61cbdeb1261c5a0887f29ced41ada07 --- neutron/agent/linux/dhcp.py | 2 +- neutron/agent/linux/ip_lib.py | 16 ++++++++++------ neutron/tests/unit/agent/linux/test_ip_lib.py | 8 +++++++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/neutron/agent/linux/dhcp.py b/neutron/agent/linux/dhcp.py index 084a67d41..0de9a21b6 100644 --- a/neutron/agent/linux/dhcp.py +++ b/neutron/agent/linux/dhcp.py @@ -911,7 +911,7 @@ class DeviceManager(object): device = ip_lib.IPDevice(device_name, namespace=network.namespace) gateway = device.route.get_gateway() if gateway: - gateway = gateway['gateway'] + gateway = gateway.get('gateway') for subnet in network.subnets: skip_subnet = ( diff --git a/neutron/agent/linux/ip_lib.py b/neutron/agent/linux/ip_lib.py index 6e317947a..dbb6e60a4 100644 --- a/neutron/agent/linux/ip_lib.py +++ b/neutron/agent/linux/ip_lib.py @@ -19,6 +19,7 @@ import os from oslo_config import cfg from oslo_log import log as logging from oslo_utils import excutils +import re from neutron.agent.common import utils from neutron.common import exceptions @@ -36,6 +37,8 @@ OPTS = [ LOOPBACK_DEVNAME = 'lo' SYS_NET_PATH = '/sys/class/net' +DEFAULT_GW_PATTERN = re.compile(r"via (\S+)") +METRIC_PATTERN = re.compile(r"metric (\S+)") class AddressNotReady(exceptions.NeutronException): @@ -531,12 +534,13 @@ class IpRouteCommand(IpDeviceCommandBase): route_list_lines if x.strip().startswith('default')), None) if default_route_line: - gateway_index = 2 - parts = default_route_line.split() - retval = dict(gateway=parts[gateway_index]) - if 'metric' in parts: - metric_index = parts.index('metric') + 1 - retval.update(metric=int(parts[metric_index])) + retval = dict() + gateway = DEFAULT_GW_PATTERN.search(default_route_line) + if gateway: + retval.update(gateway=gateway.group(1)) + metric = METRIC_PATTERN.search(default_route_line) + if metric: + retval.update(metric=int(metric.group(1))) return retval diff --git a/neutron/tests/unit/agent/linux/test_ip_lib.py b/neutron/tests/unit/agent/linux/test_ip_lib.py index 51ac34cfe..f2f879d70 100644 --- a/neutron/tests/unit/agent/linux/test_ip_lib.py +++ b/neutron/tests/unit/agent/linux/test_ip_lib.py @@ -144,6 +144,10 @@ GATEWAY_SAMPLE6 = (""" default via 192.168.99.1 proto static metric 100 """) +GATEWAY_SAMPLE7 = (""" +default dev qg-31cd36 metric 1 +""") + IPv6_GATEWAY_SAMPLE1 = (""" default via 2001:470:9:1224:4508:b885:5fb:740b metric 100 2001:db8::/64 proto kernel scope link src 2001:470:9:1224:dfcc:aaff:feb9:76ce @@ -782,7 +786,9 @@ class TestIpRouteCommand(TestIPCmdBase): 'expected': {'gateway': '192.168.99.1'}}, {'sample': GATEWAY_SAMPLE6, 'expected': {'gateway': '192.168.99.1', - 'metric': 100}}] + 'metric': 100}}, + {'sample': GATEWAY_SAMPLE7, + 'expected': {'metric': 1}}] def test_add_gateway(self): self.route_cmd.add_gateway(self.gateway, self.metric, self.table) -- 2.45.2