Antoni Segura Puimedon has uploaded a new change for review.
Change subject: netinfo: reduce netinfo instantiation to be linear
......................................................................
netinfo: reduce netinfo instantiation to be linear
Change-Id: I424e0ba9a99951432dea0365e12a02b7fb7a5bf7
Signed-off-by: Antoni S. Puimedon <asegurap(a)redhat.com>
---
M lib/vdsm/netinfo.py
M vdsm/netconf/ifcfg.py
M vdsm/netconf/iproute2.py
M vdsm/netmodels.py
4 files changed, 38 insertions(+), 24 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/77/23577/1
diff --git a/lib/vdsm/netinfo.py b/lib/vdsm/netinfo.py
index 094c089..5d18ab0 100644
--- a/lib/vdsm/netinfo.py
+++ b/lib/vdsm/netinfo.py
@@ -43,7 +43,7 @@
from .ipwrapper import linkShowDev
from .utils import anyFnmatch
from .netconfpersistence import RunningConfig
-from .netlink import iter_addrs
+from .netlink import iter_addrs, iter_links
NET_CONF_DIR = '/etc/sysconfig/network-scripts/'
@@ -813,3 +813,23 @@
if iface == vdict['iface']:
users.add(v)
return users
+
+
+def anyIfaceUsers(iface):
+ if os.path.exists(os.path.join(NET_PATH, iface, 'brport')): # Is it a port
+ return True
+ for linkDict in iter_links():
+ if linkDict['name'] == iface and 'master' in linkDict: # Is it a
slave
+ return True
+ if linkDict.get('device') == iface: # Does it back a vlan
+ return True
+ for name, info in networks().iteritems():
+ if info.get('iface') == iface:
+ return True
+ return False
+
+
+def vlanDevsForIface(iface):
+ for linkDict in iter_links():
+ if linkDict.get('device') == iface:
+ yield linkDict['name']
diff --git a/vdsm/netconf/ifcfg.py b/vdsm/netconf/ifcfg.py
index c5e6074..0d8a0e1 100644
--- a/vdsm/netconf/ifcfg.py
+++ b/vdsm/netconf/ifcfg.py
@@ -166,16 +166,15 @@
self.configApplier.removeVlan(vlan.name)
vlan.device.remove()
- def _ifaceDownAndCleanup(self, iface, _netinfo):
+ def _ifaceDownAndCleanup(self, iface):
"""Returns True iff the iface is to be removed."""
DynamicSourceRoute.addInterfaceTracking(iface)
ifdown(iface.name)
self._removeSourceRoute(iface)
- return not _netinfo.ifaceUsers(iface.name)
+ return not netinfo.anyIfaceUsers(iface.name)
def removeBond(self, bonding):
- _netinfo = netinfo.NetInfo()
- to_be_removed = self._ifaceDownAndCleanup(bonding, _netinfo)
+ to_be_removed = self._ifaceDownAndCleanup(bonding)
if to_be_removed:
if bonding.destroyOnMasterRemoval:
self.configApplier.removeBonding(bonding.name)
@@ -189,16 +188,15 @@
ifup(bonding.name)
else:
self._setNewMtu(bonding,
- _netinfo.getVlanDevsForIface(bonding.name))
+ netinfo.vlanDevsForIface(bonding.name))
ifup(bonding.name)
def removeNic(self, nic):
- _netinfo = netinfo.NetInfo()
- to_be_removed = self._ifaceDownAndCleanup(nic, _netinfo)
+ to_be_removed = self._ifaceDownAndCleanup(nic)
if to_be_removed:
self.configApplier.removeNic(nic.name)
else:
- self._setNewMtu(nic, _netinfo.getVlanDevsForIface(nic.name))
+ self._setNewMtu(nic, netinfo.vlanDevsForIface(nic.name))
ifup(nic.name)
def _getFilePath(self, fileType, device):
@@ -604,7 +602,7 @@
if bond.bridge:
conf += 'BRIDGE=%s\n' % pipes.quote(bond.bridge.name)
- ipconfig, mtu = self._getIfaceConfValues(bond, netinfo.NetInfo())
+ ipconfig, mtu = self._getIfaceConfValues(bond)
self._createConfFile(conf, bond.name, ipconfig, mtu, **opts)
# create the bonding device to avoid initscripts noise
@@ -614,9 +612,9 @@
def addNic(self, nic, **opts):
""" Create ifcfg-* file with proper fields for NIC
"""
- _netinfo = netinfo.NetInfo()
conf = ''
if _hwaddr_required():
+ _netinfo = netinfo.NetInfo()
hwaddr = (_netinfo.nics[nic.name].get('permhwaddr') or
_netinfo.nics[nic.name]['hwaddr'])
@@ -626,17 +624,17 @@
if nic.bond:
conf += 'MASTER=%s\nSLAVE=yes\n' % pipes.quote(nic.bond.name)
- ipconfig, mtu = self._getIfaceConfValues(nic, _netinfo)
+ ipconfig, mtu = self._getIfaceConfValues(nic)
self._createConfFile(conf, nic.name, ipconfig, mtu, **opts)
@staticmethod
- def _getIfaceConfValues(iface, _netinfo):
+ def _getIfaceConfValues(iface):
ipaddr, netmask, gateway, defaultRoute, ipv6addr, ipv6gateway, \
ipv6defaultRoute, bootproto, async, ipv6autoconf, dhcpv6 = \
iface.ipConfig
defaultRoute = ConfigWriter._toIfcfgFormat(defaultRoute)
mtu = iface.mtu
- if _netinfo.ifaceUsers(iface.name):
+ if netinfo.anyIfaceUsers(iface.name):
confParams = netinfo.getIfaceCfg(iface.name)
if not ipaddr and bootproto != 'dhcp':
ipaddr = confParams.get('IPADDR', None)
diff --git a/vdsm/netconf/iproute2.py b/vdsm/netconf/iproute2.py
index 86727d3..508c0c0 100644
--- a/vdsm/netconf/iproute2.py
+++ b/vdsm/netconf/iproute2.py
@@ -131,8 +131,7 @@
self.configApplier.removeBond(bonding)
def removeBond(self, bonding):
- _netinfo = netinfo.NetInfo()
- toBeRemoved = not _netinfo.ifaceUsers(bonding.name)
+ toBeRemoved = not netinfo.anyIfaceUsers(bonding.name)
if toBeRemoved:
if bonding.master is None:
@@ -146,12 +145,10 @@
netinfo.DEFAULT_MTU)
self.configApplier.ifdown(bonding)
else:
- self._setNewMtu(bonding,
- _netinfo.getVlanDevsForIface(bonding.name))
+ self._setNewMtu(bonding, netinfo.vlanDevsForIface(bonding.name))
def removeNic(self, nic):
- _netinfo = netinfo.NetInfo()
- toBeRemoved = not _netinfo.ifaceUsers(nic.name)
+ toBeRemoved = not netinfo.anyIfaceUsers(nic.name)
if toBeRemoved:
if nic.master is None:
@@ -161,8 +158,7 @@
netinfo.DEFAULT_MTU)
self.configApplier.ifdown(nic)
else:
- self._setNewMtu(nic,
- _netinfo.getVlanDevsForIface(nic.name))
+ self._setNewMtu(nic, netinfo.vlanDevsForIface(nic.name))
@staticmethod
def configureSourceRoute(routes, rules, device):
diff --git a/vdsm/netmodels.py b/vdsm/netmodels.py
index 5fde4e0..25b6963 100644
--- a/vdsm/netmodels.py
+++ b/vdsm/netmodels.py
@@ -89,7 +89,7 @@
# in a limited condition, we should not touch the nic config
if (self.vlan and
netinfo.operstate(self.name) == netinfo.OPERSTATE_UP and
- netinfo.NetInfo().ifaceUsers(self.name) and
+ netinfo.anyIfaceUsers(self.name) and
self.mtu <= netinfo.getMtu(self.name)):
return
@@ -195,7 +195,7 @@
if (self.vlan and
self.name in netinfo.bondings() and
netinfo.operstate(self.name) == netinfo.OPERSTATE_UP and
- netinfo.NetInfo().ifaceUsers(self.name) and
+ netinfo.anyIfaceUsers(self.name) and
self.mtu <= netinfo.getMtu(self.name) and
self.areOptionsApplied() and
frozenset(slave.name for slave in self.slaves) ==
--
To view, visit
http://gerrit.ovirt.org/23577
To unsubscribe, visit
http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I424e0ba9a99951432dea0365e12a02b7fb7a5bf7
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Antoni Segura Puimedon <asegurap(a)redhat.com>