def iface(device, vfid=None): """ Iface factory """ interface = IfaceHybrid() interface.device = device interface._is_dpdk_type = dpdk.is_dpdk(device) interface.vfid = vfid return interface
def prepare_setup(self, nets): """Prepare networks for creation""" sb_max_mtu_map = {} for net, attrs in six.iteritems(nets): nic = attrs.get('nic') bond = attrs.get('bonding') sb = nic or bond if not dpdk.is_dpdk(sb): self._acquired_ifaces.add(sb) sb_exists = sb in self._ovs_info.bridges_by_sb bridge = self._get_ovs_bridge(sb, sb_exists) self._create_nb(bridge, net) vlan = attrs.get('vlan') if vlan is not None: self._set_vlan(net, vlan) self._prepare_network_sb_mtu( sb, sb_exists, attrs['mtu'], sb_max_mtu_map) # FIXME: What about an existing bond? if nic is not None and vlan is None: self._copy_nic_hwaddr_to_nb(net, nic) self._ovs_info.northbounds_by_sb.setdefault(sb, set()) self._ovs_info.northbounds_by_sb[sb].add(net) self._set_networks_mtu(nets, sb_max_mtu_map)
def validate_network_setup(networks, bondings): for network, networkAttrs in six.iteritems(networks): if networkAttrs.get('remove', False): _validate_network_remove(networkAttrs) elif 'vlan' in networkAttrs: Vlan.validateTag(networkAttrs['vlan']) _validate_nic_not_dpdk(networkAttrs.get('nic', None)) currentNicsSet = set(netinfo_nics.nics()) for bonding, bondingAttrs in six.iteritems(bondings): if 'options' in bondingAttrs: Bond.validateOptions(bondingAttrs['options']) if bondingAttrs.get('remove', False): continue nics = bondingAttrs.get('nics', None) if not nics: raise ConfigNetworkError(ne.ERR_BAD_PARAMS, 'Must specify nics for bonding') if not set(nics).issubset(currentNicsSet): raise ConfigNetworkError(ne.ERR_BAD_NIC, 'Unknown nics in: %r' % list(nics)) for nic in nics: if dpdk.is_dpdk(nic): raise ConfigNetworkError( ne.ERR_BAD_NIC, '%s is a dpdk device and not supported as ' 'a slave of bond' % nic)
def _set_network_mtu(self): for sb, nbs in six.viewitems(self._ovs_info.northbounds_by_sb): if dpdk.is_dpdk(sb): continue max_nb_mtu = max(_get_mtu(nb) for nb in nbs) sb_mtu = _get_mtu(sb) if max_nb_mtu and sb_mtu != max_nb_mtu: self._set_mtu(sb, max_nb_mtu)
def _validate_bond_addition(nics, current_nics): for nic in nics: _validate_nic_exists(nic, current_nics) if dpdk.is_dpdk(nic): raise ne.ConfigNetworkError( ne.ERR_BAD_NIC, '%s is a dpdk device and not supported as a slave of bond' % nic)
def _get_ovs_bridge(self, sb): if sb in self._ovs_info.bridges_by_sb: bridge = self._ovs_info.bridges_by_sb[sb] else: dpdk_enabled = dpdk.is_dpdk(sb) bridge = self._create_bridge(dpdk_enabled) self._ovs_info.bridges_by_sb[sb] = bridge self._create_sb_nic(bridge, sb, dpdk_enabled) return bridge
def _validate_bond_addition(nics, kernel_nics): for nic in nics: if nic not in kernel_nics: raise ne.ConfigNetworkError( ne.ERR_BAD_NIC, 'Nic %s does not exist' % nic) if dpdk.is_dpdk(nic): raise ne.ConfigNetworkError( ne.ERR_BAD_NIC, '%s is a dpdk device and not supported as a slave of bond' % nic)
def speed(nic_name): """Return the nic speed if it is a legal value, 0 otherwise.""" interface = iface(nic_name) if interface.is_oper_up(): if dpdk.is_dpdk(nic_name): return dpdk.speed(nic_name) try: return read_speed_using_sysfs(nic_name) except Exception: logging.debug('cannot read %s speed', nic_name) return 0
def setup_ovs_ip_config(nets2add, nets2remove): # TODO: This should be moved to network/api.py when we solve rollback # transactions. for net in nets2remove: _drop_dhcp_config(net) for net, attrs in six.iteritems(nets2add): sb = attrs.get('bonding') or attrs.get('nic') if not dpdk.is_dpdk(sb): address.disable_ipv6(sb) _set_static_ip_config(net, attrs) _set_dhcp_config(net, attrs)
def up(dev, admin_blocking=True, oper_blocking=False): """ Set link state to UP, optionally blocking on the action. :param dev: iface name. :param admin_blocking: Block until the administrative state changes to UP. :param oper_blocking: Block until the link is operational. admin state is at kernel level, while link state is at driver level. """ if dpdk.is_dpdk(dev): dpdk.up(dev) return if admin_blocking: _up_blocking(dev, oper_blocking) else: ipwrapper.linkSet(dev, [STATE_UP])
def speed(nic_name): """Returns the nic speed if it is a legal value, nicName refers to a nic and nic is UP, 0 otherwise.""" try: if dpdk.is_dpdk(nic_name) and operstate(nic_name) == OPERSTATE_UP: return dpdk.speed(nic_name) if operstate(nic_name) == OPERSTATE_UP: with io.open('/sys/class/net/%s/speed' % nic_name) as speedFile: s = int(speedFile.read()) # the device may have been disabled/downed after checking # so we validate the return value as sysfs may return # special values to indicate the device is down/disabled if s not in (2**16 - 1, 2**32 - 1) and s > 0: return s except IOError as ose: if ose.errno == errno.EINVAL: return _ibHackedSpeed(nic_name) else: logging.exception('cannot read %s nic speed', nic_name) except Exception: logging.exception('cannot read %s speed', nic_name) return 0
def speed(nic_name): """Returns the nic speed if it is a legal value, nicName refers to a nic and nic is UP, 0 otherwise.""" try: if dpdk.is_dpdk(nic_name) and operstate(nic_name) == OPERSTATE_UP: return dpdk.speed(nic_name) if operstate(nic_name) == OPERSTATE_UP: with io.open('/sys/class/net/%s/speed' % nic_name) as speedFile: s = int(speedFile.read()) # the device may have been disabled/downed after checking # so we validate the return value as sysfs may return # special values to indicate the device is down/disabled if s not in (2 ** 16 - 1, 2 ** 32 - 1) and s > 0: return s except IOError as ose: if ose.errno == errno.EINVAL: return _ibHackedSpeed(nic_name) else: logging.exception('cannot read %s nic speed', nic_name) except Exception: logging.exception('cannot read %s speed', nic_name) return 0
def add(self, nets): with self._transaction: for net, attrs in six.iteritems(nets): nic = attrs.get('nic') bond = attrs.get('bonding') sb = nic or bond if not dpdk.is_dpdk(sb): self._acquired_ifaces.add(sb) bridge = self._get_ovs_bridge(sb) self._create_nb(bridge, net) vlan = attrs.get('vlan') if vlan is not None: self._set_vlan(net, vlan) # FIXME: What about an existing bond? if nic is not None and vlan is None: self._copy_nic_hwaddr_to_nb(net, nic) self._ovs_info.northbounds_by_sb.setdefault(sb, set()) self._ovs_info.northbounds_by_sb[sb].add(net) yield
def operstate(nic_name): if dpdk.is_dpdk(nic_name): return dpdk.operstate(nic_name) with io.open('/sys/class/net/%s/operstate' % nic_name) as operstateFile: return operstateFile.read().strip()
def exists(self): if dpdk.is_dpdk(self._dev): return self._dev in dpdk.get_dpdk_devices() return os.path.exists(os.path.join(NET_PATH, self._dev))
def getLink(dev): """Returns the Link object for the specified dev.""" if dpdk.is_dpdk(dev): return dpdk.link_info(dev) return Link.fromDict(link.get_link(dev))
def _validate_nic_not_dpdk(nic): if nic and dpdk.is_dpdk(nic): raise ConfigNetworkError( ne.ERR_BAD_NIC, '%s is a dpdk device and supported only with OVS' % nic, )
def test_is_dpdk_true(self): self.assertTrue(dpdk.is_dpdk(dev_name='dpdk0'))
def test_is_dpdk_true(self): assert dpdk.is_dpdk(dev_name='dpdk0')
def mac_address(dev): if dpdk.is_dpdk(dev): return dpdk.link_info(dev)['address'] return get_link(dev)['address']
def is_oper_up(dev): if dpdk.is_dpdk(dev): return dpdk.is_oper_up(dev) return is_link_up(get_link(dev)['flags'], check_oper_status=True)
def down(dev): if dpdk.is_dpdk(dev): dpdk.down(dev) return ipwrapper.linkSet(dev, ['down'])
def _validate_nic_not_dpdk(nic): if nic and dpdk.is_dpdk(nic): raise ConfigNetworkError( ne.ERR_BAD_NIC, '%s is a dpdk device and supported only with OVS' % nic)
def test_is_dpdk_false(self): self.assertFalse(dpdk.is_dpdk(dev_name='not_dpdk_dev'))
def oper_up(self): if dpdk.is_dpdk(self.name): return dpdk.is_oper_up(self.name) return bool( link.get_link(self.name)['flags'] & libnl.IfaceStatus.IFF_RUNNING)
def test_is_dpdk_false(self): assert not dpdk.is_dpdk(dev_name='not_dpdk_dev')
def _get_mac(iface): if dpdk.is_dpdk(iface): return dpdk.link_info(iface)['address'] return link.get_link(iface)['address']