lib: iproute2: move fix_ipv6_route_metric API from LinkUtils to lib.iproute2

Signed-off-by: Julien Fortin <[email protected]>
This commit is contained in:
Julien Fortin
2019-05-09 17:13:42 +08:00
parent 865cf3fcbc
commit e4bdb4517e
4 changed files with 56 additions and 51 deletions
+1 -1
View File
@@ -485,7 +485,7 @@ class addressvirtual(Addon, moduleBase):
if not self.addressvirtual_with_route_metric or not self.ipcmd.addr_metric_support():
# if the system doesn't support ip addr set METRIC
# we need to do manually check the ordering of the ip6 routes
self.ipcmd.fix_ipv6_route_metric(ifaceobj, macvlan_ifname, ips)
self.iproute2.fix_ipv6_route_metric(ifaceobj, macvlan_ifname, ips)
except Exception as e:
self.logger.debug('fix_vrf_slave_ipv6_route_metric: failed: %s' % e)
-43
View File
@@ -1122,49 +1122,6 @@ class LinkUtils(utilsBase):
return None
def fix_ipv6_route_metric(self, ifaceobj, macvlan_ifacename, ips):
vrf_table = None
if ifaceobj.link_privflags & ifaceLinkPrivFlags.VRF_SLAVE:
try:
for upper_iface in ifaceobj.upperifaces:
vrf_table = self._get_vrf_id(upper_iface)
if vrf_table:
break
except:
pass
ip_route_del = []
for ip in ips:
ip_network_obj = IPNetwork(ip)
if type(ip_network_obj) == IPv6Network:
route_prefix = '%s/%d' % (ip_network_obj.network, ip_network_obj.prefixlen)
if vrf_table:
if LinkUtils.ipbatch and not LinkUtils.ipbatch_pause:
LinkUtils.add_to_batch('route del %s table %s dev %s' % (route_prefix, vrf_table, macvlan_ifacename))
else:
utils.exec_commandl([utils.ip_cmd, 'route', 'del', route_prefix, 'table', vrf_table, 'dev', macvlan_ifacename])
else:
if LinkUtils.ipbatch and not LinkUtils.ipbatch_pause:
LinkUtils.add_to_batch('route del %s dev %s' % (route_prefix, macvlan_ifacename))
else:
utils.exec_commandl([utils.ip_cmd, 'route', 'del', route_prefix, 'dev', macvlan_ifacename])
ip_route_del.append((route_prefix, vrf_table))
for ip, vrf_table in ip_route_del:
if vrf_table:
if LinkUtils.ipbatch and not LinkUtils.ipbatch_pause:
LinkUtils.add_to_batch('route add %s table %s dev %s proto kernel metric 9999' % (ip, vrf_table, macvlan_ifacename))
else:
utils.exec_commandl([utils.ip_cmd, 'route', 'add', ip, 'table', vrf_table, 'dev', macvlan_ifacename, 'proto', 'kernel' 'metric', '9999'])
else:
if LinkUtils.ipbatch and not LinkUtils.ipbatch_pause:
LinkUtils.add_to_batch('route add %s dev %s proto kernel metric 9999' % (ip, macvlan_ifacename))
else:
utils.exec_commandl([utils.ip_cmd, 'route', 'add', ip, 'dev', macvlan_ifacename, 'proto', 'kernel' 'metric', '9999'])
def link_create_vlan(self, vlan_device_name, vlan_raw_device, vlanid):
if self.link_exists(vlan_device_name):
return
+48
View File
@@ -27,17 +27,21 @@ import shlex
import signal
import subprocess
from ipaddr import IPNetwork
try:
from ifupdown2.lib.sysfs import Sysfs
from ifupdown2.lib.base_objects import Cache, Requirements
from ifupdown2.ifupdown.utils import utils
from ifupdown2.ifupdown.iface import ifaceLinkPrivFlags
from ifupdown2.nlmanager.nlpacket import Link
except ImportError:
from lib.sysfs import Sysfs
from lib.base_objects import Cache, Requirements
from ifupdown.utils import utils
from ifupdown.iface import ifaceLinkPrivFlags
from nlmanager.nlpacket import Link
@@ -513,3 +517,47 @@ class IPRoute2(Cache, Requirements):
cmd += " dev %s" % ifname
utils.exec_command(cmd)
def fix_ipv6_route_metric(self, ifaceobj, macvlan_ifacename, ips):
vrf_table = None
if ifaceobj.link_privflags & ifaceLinkPrivFlags.VRF_SLAVE:
try:
for upper_iface in ifaceobj.upperifaces:
vrf_table = self.cache.get_vrf_table(upper_iface)
if vrf_table:
break
except:
pass
ip_route_del = []
for ip in ips:
ip_network_obj = IPNetwork(ip)
if ip_network_obj.version == 6:
route_prefix = '%s/%d' % (ip_network_obj.network, ip_network_obj.prefixlen)
if vrf_table:
self.__execute_or_batch(
utils.ip_cmd,
"route del %s table %s dev %s" % (route_prefix, vrf_table, macvlan_ifacename)
)
else:
self.__execute_or_batch(
utils.ip_cmd,
"route del %s dev %s" % (route_prefix, macvlan_ifacename)
)
ip_route_del.append((route_prefix, vrf_table))
for ip, vrf_table in ip_route_del:
if vrf_table:
self.__execute_or_batch(
utils.ip_cmd,
"route add %s table %s dev %s proto kernel metric 9999" % (ip, vrf_table, macvlan_ifacename)
)
else:
self.__execute_or_batch(
utils.ip_cmd,
"route add %s dev %s proto kernel metric 9999" % (ip, macvlan_ifacename)
)
+7 -7
View File
@@ -743,6 +743,13 @@ class _NetlinkCache:
log.debug("get_vrf_table_map: %s" % str(e))
return vrf_table_map
def get_vrf_table(self, ifname):
try:
with self._cache_lock:
return self._link_cache[ifname].attributes[nlpacket.Link.IFLA_LINKINFO].value[nlpacket.Link.IFLA_INFO_DATA][nlpacket.Link.IFLA_VRF_TABLE]
except (KeyError, AttributeError):
return 0
##########################################################################
# BOND ###################################################################
##########################################################################
@@ -912,13 +919,6 @@ class _NetlinkCache:
except TypeError as e:
return self.__handle_type_error(inspect.currentframe().f_code.co_name, ifname, str(e), return_value=0)
# old
def get_vrf_table(self, ifname):
try:
with self._cache_lock:
return self._link_cache[ifname].attributes[nlpacket.Link.IFLA_LINKINFO].value[nlpacket.Link.IFLA_INFO_DATA][nlpacket.Link.IFLA_VRF_TABLE]
except (KeyError, AttributeError):
return 0
#####################################################
#####################################################