Ondřej Svoboda has uploaded a new change for review. Change subject: sourceroute: pass IPv4 configuration using an existing container class ......................................................................
sourceroute: pass IPv4 configuration using an existing container class Change-Id: I5166fe3074ae6318adfafdbacaa1a6a607a646c6 Signed-off-by: Ondřej Svoboda <osvob...@redhat.com> --- M tests/functional/networkTests.py M vdsm/network/configurators/__init__.py M vdsm/network/sourceroute.py M vdsm/network/sourceroutethread.py 4 files changed, 43 insertions(+), 35 deletions(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/07/41607/1 diff --git a/tests/functional/networkTests.py b/tests/functional/networkTests.py index 5486cac..63666a7 100644 --- a/tests/functional/networkTests.py +++ b/tests/functional/networkTests.py @@ -39,6 +39,7 @@ from network import api, errors, sourceroute, tc from network.configurators.ifcfg import Ifcfg +from network.models import IPv4 from hookValidation import ValidatesHook from testlib import (VdsmTestCase as TestCaseBase, namedTemporaryDir, @@ -190,8 +191,8 @@ def _get_source_route(device_name, ipv4_address): - return sourceroute.StaticSourceRoute( - device_name, ipv4_address, IP_MASK, IP_GATEWAY) + ipv4 = IPv4(ipv4_address, IP_MASK, IP_GATEWAY) + return sourceroute.StaticSourceRoute(device_name, ipv4) @expandPermutations diff --git a/vdsm/network/configurators/__init__.py b/vdsm/network/configurators/__init__.py index 5c58a94..05a98ea 100644 --- a/vdsm/network/configurators/__init__.py +++ b/vdsm/network/configurators/__init__.py @@ -122,23 +122,22 @@ def removeQoS(self, top_device): qos.remove_outbound(top_device) - def _addSourceRoute(self, netEnt): - ipv4 = netEnt.ipv4 + def _addSourceRoute(self, device): + ipv4 = device.ipv4 # bootproto is None for both static and no bootproto - if ipv4.bootproto != 'dhcp' and netEnt.master is None: + if ipv4.bootproto != 'dhcp' and device.master is None: logging.debug("Adding source route: name=%s, addr=%s, netmask=%s, " - "gateway=%s" % (netEnt.name, ipv4.address, + "gateway=%s" % (device.name, ipv4.address, ipv4.netmask, ipv4.gateway)) if (ipv4.gateway in (None, '0.0.0.0') or not ipv4.address or not ipv4.netmask): logging.warning( 'invalid input for source routing: name=%s, ' 'addr=%s, netmask=%s, gateway=%s', - netEnt.name, ipv4.address, ipv4.netmask, + device.name, ipv4.address, ipv4.netmask, ipv4.gateway) else: - source_route = StaticSourceRoute(netEnt.name, ipv4.address, - ipv4.netmask, ipv4.gateway) + source_route = StaticSourceRoute(device.name, device.ipv4) source_route.prepare_configuration() self.configureSourceRoute(source_route) diff --git a/vdsm/network/sourceroute.py b/vdsm/network/sourceroute.py index e4ecf3c..9f6079d 100644 --- a/vdsm/network/sourceroute.py +++ b/vdsm/network/sourceroute.py @@ -28,45 +28,48 @@ from vdsm.ipwrapper import Route, routeShowTable, Rule, ruleList from vdsm.utils import rmFile +from .models import IPv4 + TRACKED_INTERFACES_FOLDER = P_VDSM_RUN + 'trackedInterfaces' class StaticSourceRoute(object): - def __init__(self, device, ipaddr=None, mask=None, gateway=None): + def __init__(self, device, ipv4=None): self.device = device - self._ipaddr = ipaddr - self._mask = mask - self._gateway = gateway - self._table = str(self._generateTableId(ipaddr)) if ipaddr else None - self._network = self._parse_network(ipaddr, mask) + self.ipv4 = ipv4 or IPv4() + self._generate_table_id() + self._parse_network() - def _parse_network(self, ipaddr, mask): - if not ipaddr or not mask: - return None - network = netaddr.IPNetwork('%s/%s' % (ipaddr, mask)) - return "%s/%s" % (network.network, network.prefixlen) + def _parse_network(self): + if not self.ipv4.address or not self.ipv4.netmask: + self.network = None + network = netaddr.IPNetwork('%s/%s' % (self.ipv4.address, + self.ipv4.netmask)) + self.network = '%s/%s' % (network.network, network.prefixlen) - def _generateTableId(self, ipaddr): + def _generate_table_id(self): # TODO: Future proof for IPv6 - return netaddr.IPAddress(ipaddr).value + if not self.ipv4.address: + self.table = None + self.table = str(netaddr.IPAddress(self.ipv4.address).value) def _buildRoutes(self): - return [Route(network='0.0.0.0/0', via=self._gateway, - device=self.device, table=self._table), - Route(network=self._network, via=self._ipaddr, - device=self.device, table=self._table)] + return [Route(network='0.0.0.0/0', via=self.ipv4.gateway, + device=self.device, table=self.table), + Route(network=self.network, via=self.ipv4.address, + device=self.device, table=self.table)] def _buildRules(self): - return [Rule(source=self._network, table=self._table), - Rule(destination=self._network, table=self._table, + return [Rule(source=self.network, table=self.table), + Rule(destination=self.network, table=self.table, srcDevice=self.device)] def prepare_configuration(self): logging.info('Configuring gateway - IP: %s, network: %s, subnet: %s, ' - 'gateway: %s, table: %s, device: %s', self._ipaddr, - self._network, self._mask, self._gateway, self._table, - self.device) + 'gateway: %s, table: %s, device: %s', self.ipv4.address, + self.network, self.ipv4.netmask, self.ipv4.gateway, + self.table, self.device) self.routes = self._buildRoutes() self.rules = self._buildRules() diff --git a/vdsm/network/sourceroutethread.py b/vdsm/network/sourceroutethread.py index 2a54376..7cc127c 100644 --- a/vdsm/network/sourceroutethread.py +++ b/vdsm/network/sourceroutethread.py @@ -26,6 +26,7 @@ from vdsm import utils from .configurators.iproute2 import Iproute2 +from .models import IPv4 from .sourceroute import DynamicSourceRoute @@ -50,10 +51,14 @@ logging.error('invalid DHCP response %s', sourceRouteContents) else: - source_route = DynamicSourceRoute(device, ip, mask, - gateway) - source_route.prepare_configuration() - configurator.configureSourceRoute(source_route) + try: + ipv4 = IPv4(ip, mask, gateway) + except: + logging.exception('Invalid DHCP response') + else: + source_route = DynamicSourceRoute(device, ipv4) + source_route.prepare_configuration() + configurator.configureSourceRoute(source_route) else: source_route = DynamicSourceRoute(device) if source_route.prepare_removal(): -- To view, visit https://gerrit.ovirt.org/41607 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5166fe3074ae6318adfafdbacaa1a6a607a646c6 Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Ondřej Svoboda <osvob...@redhat.com> _______________________________________________ vdsm-patches mailing list vdsm-patches@lists.fedorahosted.org https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches