Esempio n. 1
0
def addNetwork(network, vlan=None, bonding=None, nics=None, ipaddr=None,
               netmask=None, prefix=None, mtu=None, gateway=None,
               ipv6addr=None, ipv6gateway=None, force=False,
               configurator=None, bondingOptions=None, bridged=True,
               _netinfo=None, qosInbound=None, qosOutbound=None, **options):
    nics = nics or ()
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()
    bridged = utils.tobool(bridged)
    vlan = _vlanToInternalRepresentation(vlan)

    if mtu:
        mtu = int(mtu)

    if prefix:
        if netmask:
            raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
                                     'Both PREFIX and NETMASK supplied')
        else:
            try:
                netmask = netinfo.prefix2netmask(int(prefix))
            except ValueError as ve:
                raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Bad prefix: %s" %
                                         ve)

    if not utils.tobool(force):
        logging.debug('validating network...')
        if network in _netinfo.networks:
            raise ConfigNetworkError(ne.ERR_USED_BRIDGE,
                                     'Network already exists')
        if bonding:
            _validateInterNetworkCompatibility(_netinfo, vlan, bonding,
                                               bridged)
        else:
            for nic in nics:
                _validateInterNetworkCompatibility(_netinfo, vlan, nic,
                                                   bridged)

    logging.info("Adding network %s with vlan=%s, bonding=%s, nics=%s,"
                 " bondingOptions=%s, mtu=%s, bridged=%s, options=%s",
                 network, vlan, bonding, nics, bondingOptions,
                 mtu, bridged, options)

    if configurator is None:
        configurator = Ifcfg()

    bootproto = options.pop('bootproto', None)

    defaultRoute = network == constants.MANAGEMENT_NETWORK

    netEnt = objectivizeNetwork(network if bridged else None, vlan, bonding,
                                bondingOptions, nics, mtu, ipaddr, netmask,
                                gateway, bootproto, ipv6addr, ipv6gateway,
                                _netinfo=_netinfo, configurator=configurator,
                                defaultRoute=defaultRoute, **options)

    netEnt.configure(**options)
    configurator.configureLibvirtNetwork(network, netEnt,
                                         qosInbound=qosInbound,
                                         qosOutbound=qosOutbound)
Esempio n. 2
0
def addNetwork(network, vlan=None, bonding=None, nics=None, ipaddr=None,
               netmask=None, prefix=None, mtu=None, gateway=None, force=False,
               configurator=None, bondingOptions=None, bridged=True,
               _netinfo=None, qosInbound=None, qosOutbound=None, **options):
    nics = nics or ()
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()
    bridged = utils.tobool(bridged)

    if mtu:
        mtu = int(mtu)

    if prefix:
        if netmask:
            raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
                                     'Both PREFIX and NETMASK supplied')
        else:
            try:
                netmask = netinfo.prefix2netmask(int(prefix))
            except ValueError as ve:
                raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Bad prefix: %s" %
                                         ve)

    if not utils.tobool(force):
        logging.debug('validating network...')
        if network in _netinfo.networks:
            raise ConfigNetworkError(ne.ERR_USED_BRIDGE,
                                     'Network already exists')
        if bonding:
            _validateInterNetworkCompatibility(_netinfo, vlan, bonding,
                                               bridged)
        else:
            for nic in nics:
                _validateInterNetworkCompatibility(_netinfo, vlan, nic,
                                                   bridged)
    logging.info("Adding network %s with vlan=%s, bonding=%s, nics=%s,"
                 " bondingOptions=%s, mtu=%s, bridged=%s, options=%s",
                 network, vlan, bonding, nics, bondingOptions,
                 mtu, bridged, options)

    if configurator is None:
        configurator = Ifcfg()

    bootproto = options.pop('bootproto', None)

    netEnt = objectivizeNetwork(network if bridged else None, vlan, bonding,
                                bondingOptions, nics, mtu, ipaddr, netmask,
                                gateway, bootproto, _netinfo, configurator,
                                **options)

    # libvirt net addition must be done before creation so that on dhcp ifup
    # the dhcp hook will already see the network as belonging to vdsm.
    configurator.configureLibvirtNetwork(network, netEnt,
                                         qosInbound=qosInbound,
                                         qosOutbound=qosOutbound)
    netEnt.configure(**options)
Esempio n. 3
0
 def testNetmaskConversions(self):
     path = os.path.join(os.path.dirname(__file__), "netmaskconversions")
     with open(path) as netmaskFile:
         for line in netmaskFile:
             if line.startswith('#'):
                 continue
             bitmask, address = [value.strip() for value in line.split()]
             self.assertEqual(netinfo.prefix2netmask(int(bitmask)), address)
     self.assertRaises(ValueError, netinfo.prefix2netmask, -1)
     self.assertRaises(ValueError, netinfo.prefix2netmask, 33)
Esempio n. 4
0
 def testNetmaskConversions(self):
     path = os.path.join(os.path.dirname(__file__), "netmaskconversions")
     with open(path) as netmaskFile:
         for line in netmaskFile:
             if line.startswith('#'):
                 continue
             bitmask, address = [value.strip() for value in line.split()]
             self.assertEqual(netinfo.prefix2netmask(int(bitmask)),
                              address)
     self.assertRaises(ValueError, netinfo.prefix2netmask, -1)
     self.assertRaises(ValueError, netinfo.prefix2netmask, 33)
Esempio n. 5
0
def addNetwork(network, vlan=None, bonding=None, nics=None, ipaddr=None,
               netmask=None, prefix=None, mtu=None, gateway=None, dhcpv6=None,
               ipv6addr=None, ipv6gateway=None, ipv6autoconf=None, force=False,
               configurator=None, bondingOptions=None, bridged=True,
               _netinfo=None, qosOutbound=None, defaultRoute=None,
               blockingdhcp=False, **options):
    nics = nics or ()
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()
    bridged = utils.tobool(bridged)
    if dhcpv6 is not None:
        dhcpv6 = utils.tobool(dhcpv6)
    if ipv6autoconf is not None:
        ipv6autoconf = utils.tobool(ipv6autoconf)
    vlan = _vlanToInternalRepresentation(vlan)

    if mtu:
        mtu = int(mtu)

    if network == '':
        raise ConfigNetworkError(ne.ERR_BAD_BRIDGE,
                                 'Empty network names are not valid')
    if prefix:
        if netmask:
            raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
                                     'Both PREFIX and NETMASK supplied')
        else:
            try:
                netmask = netinfo.prefix2netmask(int(prefix))
            except ValueError as ve:
                raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Bad prefix: %s" %
                                         ve)

    if not utils.tobool(force):
        logging.debug('validating network...')
        if network in _netinfo.networks:
            raise ConfigNetworkError(ne.ERR_USED_BRIDGE,
                                     'Network already exists')
        if bonding:
            _validateInterNetworkCompatibility(_netinfo, vlan, bonding,
                                               bridged, qosOutbound)
        else:
            for nic in nics:
                _validateInterNetworkCompatibility(_netinfo, vlan, nic,
                                                   bridged, qosOutbound)

    # defaultRoute is set either explicitly by the client, OR if we're adding
    # the management network.
    # TODO: When oVirt 3.3 is deprecated, change the default to False and
    #       remove reference to constants.LEGACY_MANAGEMENT_NETWORKS
    if defaultRoute is None:
        defaultRoute = network in constants.LEGACY_MANAGEMENT_NETWORKS

    logging.info("Adding network %s with vlan=%s, bonding=%s, nics=%s,"
                 " bondingOptions=%s, mtu=%s, bridged=%s, defaultRoute=%s,"
                 "options=%s", network, vlan, bonding, nics, bondingOptions,
                 mtu, bridged, defaultRoute, options)

    if configurator is None:
        configurator = ConfiguratorClass()

    bootproto = options.pop('bootproto', None)

    netEnt = objectivizeNetwork(
        bridge=network if bridged else None, vlan=vlan, bonding=bonding,
        bondingOptions=bondingOptions, nics=nics, mtu=mtu, ipaddr=ipaddr,
        netmask=netmask, gateway=gateway, bootproto=bootproto, dhcpv6=dhcpv6,
        blockingdhcp=blockingdhcp, ipv6addr=ipv6addr, ipv6gateway=ipv6gateway,
        ipv6autoconf=ipv6autoconf, defaultRoute=defaultRoute,
        _netinfo=_netinfo, configurator=configurator, opts=options)

    netEnt.configure(**options)
    configurator.configureLibvirtNetwork(network, netEnt)
    if qosOutbound is not None:
        configurator.configureQoS(qosOutbound, netEnt)
Esempio n. 6
0
def addNetwork(network, vlan=None, bonding=None, nics=None, ipaddr=None,
               netmask=None, prefix=None, mtu=None, gateway=None,
               ipv6addr=None, ipv6gateway=None, force=False,
               configurator=None, bondingOptions=None, bridged=True,
               _netinfo=None, qosInbound=None, qosOutbound=None,
               defaultRoute=None, **options):
    nics = nics or ()
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()
    bridged = utils.tobool(bridged)
    vlan = _vlanToInternalRepresentation(vlan)

    if mtu:
        mtu = int(mtu)

    if prefix:
        if netmask:
            raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
                                     'Both PREFIX and NETMASK supplied')
        else:
            try:
                netmask = netinfo.prefix2netmask(int(prefix))
            except ValueError as ve:
                raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Bad prefix: %s" %
                                         ve)

    if not utils.tobool(force):
        logging.debug('validating network...')
        if network in _netinfo.networks:
            raise ConfigNetworkError(ne.ERR_USED_BRIDGE,
                                     'Network already exists')
        if bonding:
            _validateInterNetworkCompatibility(_netinfo, vlan, bonding,
                                               bridged)
        else:
            for nic in nics:
                _validateInterNetworkCompatibility(_netinfo, vlan, nic,
                                                   bridged)

    # defaultRoute is set either explicitly by the client, OR if we're adding
    # the management network.
    # TODO: When oVirt 3.3 is deprecated, change the default to False and
    #       remove reference to constants.LEGACY_MANAGEMENT_NETWORKS
    if defaultRoute is None:
        defaultRoute = network in constants.LEGACY_MANAGEMENT_NETWORKS

    logging.info("Adding network %s with vlan=%s, bonding=%s, nics=%s,"
                 " bondingOptions=%s, mtu=%s, bridged=%s, defaultRoute=%s,"
                 "options=%s", network, vlan, bonding, nics, bondingOptions,
                 mtu, bridged, defaultRoute, options)

    if configurator is None:
        configurator = ConfiguratorClass()

    bootproto = options.pop('bootproto', None)

    netEnt = objectivizeNetwork(network if bridged else None, vlan, bonding,
                                bondingOptions, nics, mtu, ipaddr, netmask,
                                gateway, bootproto, ipv6addr, ipv6gateway,
                                defaultRoute=defaultRoute, _netinfo=_netinfo,
                                configurator=configurator, **options)

    netEnt.configure(**options)
    configurator.configureLibvirtNetwork(network, netEnt,
                                         qosInbound=qosInbound,
                                         qosOutbound=qosOutbound)
Esempio n. 7
0
def addNetwork(network,
               vlan=None,
               bonding=None,
               nics=None,
               ipaddr=None,
               netmask=None,
               prefix=None,
               mtu=None,
               gateway=None,
               force=False,
               configurator=None,
               bondingOptions=None,
               bridged=True,
               _netinfo=None,
               qosInbound=None,
               qosOutbound=None,
               **options):
    nics = nics or ()
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()
    bridged = utils.tobool(bridged)

    if mtu:
        mtu = int(mtu)

    if prefix:
        if netmask:
            raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
                                     'Both PREFIX and NETMASK supplied')
        else:
            try:
                netmask = netinfo.prefix2netmask(int(prefix))
            except ValueError as ve:
                raise ConfigNetworkError(ne.ERR_BAD_ADDR,
                                         "Bad prefix: %s" % ve)

    if not utils.tobool(force):
        logging.debug('validating network...')
        if network in _netinfo.networks:
            raise ConfigNetworkError(ne.ERR_USED_BRIDGE,
                                     'Network already exists')
        if bonding:
            _validateInterNetworkCompatibility(_netinfo, vlan, bonding,
                                               bridged)
        else:
            for nic in nics:
                _validateInterNetworkCompatibility(_netinfo, vlan, nic,
                                                   bridged)

    logging.info(
        "Adding network %s with vlan=%s, bonding=%s, nics=%s,"
        " bondingOptions=%s, mtu=%s, bridged=%s, options=%s", network, vlan,
        bonding, nics, bondingOptions, mtu, bridged, options)

    if configurator is None:
        configurator = Ifcfg()

    bootproto = options.pop('bootproto', None)

    defaultRoute = network == constants.MANAGEMENT_NETWORK

    netEnt = objectivizeNetwork(network if bridged else None,
                                vlan,
                                bonding,
                                bondingOptions,
                                nics,
                                mtu,
                                ipaddr,
                                netmask,
                                gateway,
                                bootproto,
                                _netinfo,
                                configurator,
                                defaultRoute=defaultRoute,
                                **options)

    netEnt.configure(**options)
    configurator.configureLibvirtNetwork(network,
                                         netEnt,
                                         qosInbound=qosInbound,
                                         qosOutbound=qosOutbound)
Esempio n. 8
0
File: api.py Progetto: Caez83/vdsm
def _addNetwork(network, vlan=None, bonding=None, nics=None, ipaddr=None,
                netmask=None, prefix=None, mtu=None, gateway=None, dhcpv6=None,
                ipv6addr=None, ipv6gateway=None, ipv6autoconf=None,
                force=False, configurator=None, bondingOptions=None,
                bridged=True, _netinfo=None, hostQos=None,
                defaultRoute=None, blockingdhcp=False, **options):
    nics = nics or ()
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()
    bridged = utils.tobool(bridged)
    if dhcpv6 is not None:
        dhcpv6 = utils.tobool(dhcpv6)
    if ipv6autoconf is not None:
        ipv6autoconf = utils.tobool(ipv6autoconf)
    vlan = _vlanToInternalRepresentation(vlan)

    if mtu:
        mtu = int(mtu)

    if network == '':
        raise ConfigNetworkError(ne.ERR_BAD_BRIDGE,
                                 'Empty network names are not valid')
    if prefix:
        if netmask:
            raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
                                     'Both PREFIX and NETMASK supplied')
        else:
            try:
                netmask = netinfo.prefix2netmask(int(prefix))
            except ValueError as ve:
                raise ConfigNetworkError(ne.ERR_BAD_ADDR, "Bad prefix: %s" %
                                         ve)

    if not utils.tobool(force):
        logging.debug('validating network...')
        if network in _netinfo.networks:
            raise ConfigNetworkError(
                ne.ERR_USED_BRIDGE, 'Network already exists (%s)' % (network,))
        if bonding:
            _validateInterNetworkCompatibility(_netinfo, vlan, bonding)
        else:
            for nic in nics:
                _validateInterNetworkCompatibility(_netinfo, vlan, nic)

    # defaultRoute is set either explicitly by the client, OR if we're adding
    # the management network.
    # TODO: When oVirt 3.3 is deprecated, change the default to False and
    #       remove reference to constants.LEGACY_MANAGEMENT_NETWORKS
    if defaultRoute is None:
        defaultRoute = network in constants.LEGACY_MANAGEMENT_NETWORKS

    logging.info("Adding network %s with vlan=%s, bonding=%s, nics=%s,"
                 " bondingOptions=%s, mtu=%s, bridged=%s, defaultRoute=%s,"
                 "options=%s", network, vlan, bonding, nics, bondingOptions,
                 mtu, bridged, defaultRoute, options)

    if configurator is None:
        configurator = ConfiguratorClass()

    bootproto = options.pop('bootproto', None)

    net_ent = _objectivizeNetwork(
        bridge=network if bridged else None, vlan=vlan, bonding=bonding,
        bondingOptions=bondingOptions, nics=nics, mtu=mtu, ipaddr=ipaddr,
        netmask=netmask, gateway=gateway, bootproto=bootproto, dhcpv6=dhcpv6,
        blockingdhcp=blockingdhcp, ipv6addr=ipv6addr, ipv6gateway=ipv6gateway,
        ipv6autoconf=ipv6autoconf, defaultRoute=defaultRoute,
        _netinfo=_netinfo, configurator=configurator, opts=options)

    if bridged and network in _netinfo.bridges:
        net_ent_to_configure = net_ent.port
        logging.info("Bridge %s already exists.", network)
        if mtu:
            # The bridge already exists and we attach a new underlying device
            # to it. We need to make sure that the bridge MTU configuration is
            # updated.
            configurator.configApplier.setIfaceMtu(network, mtu)
            # We must also update the vms` tap devices (the bridge ports in
            # this case) so that their MTU is synced with the bridge
            _update_bridge_ports_mtu(net_ent.name, mtu)
    else:
        net_ent_to_configure = net_ent

    if net_ent_to_configure is not None:
        logging.info("Configuring device %s", net_ent_to_configure)
        net_ent_to_configure.configure(**options)
    configurator.configureLibvirtNetwork(network, net_ent)
    if hostQos is not None:
        configurator.configureQoS(hostQos, net_ent)
Esempio n. 9
0
def _addNetwork(network,
                vlan=None,
                bonding=None,
                nics=None,
                ipaddr=None,
                netmask=None,
                prefix=None,
                mtu=None,
                gateway=None,
                dhcpv6=None,
                ipv6addr=None,
                ipv6gateway=None,
                ipv6autoconf=None,
                force=False,
                configurator=None,
                bondingOptions=None,
                bridged=True,
                _netinfo=None,
                hostQos=None,
                defaultRoute=None,
                blockingdhcp=False,
                **options):
    nics = nics or ()
    if _netinfo is None:
        _netinfo = netinfo.NetInfo()
    bridged = utils.tobool(bridged)
    if dhcpv6 is not None:
        dhcpv6 = utils.tobool(dhcpv6)
    if ipv6autoconf is not None:
        ipv6autoconf = utils.tobool(ipv6autoconf)
    vlan = _vlanToInternalRepresentation(vlan)

    if mtu:
        mtu = int(mtu)

    if network == '':
        raise ConfigNetworkError(ne.ERR_BAD_BRIDGE,
                                 'Empty network names are not valid')
    if prefix:
        if netmask:
            raise ConfigNetworkError(ne.ERR_BAD_PARAMS,
                                     'Both PREFIX and NETMASK supplied')
        else:
            try:
                netmask = netinfo.prefix2netmask(int(prefix))
            except ValueError as ve:
                raise ConfigNetworkError(ne.ERR_BAD_ADDR,
                                         "Bad prefix: %s" % ve)

    if not utils.tobool(force):
        logging.debug('validating network...')
        if network in _netinfo.networks:
            raise ConfigNetworkError(
                ne.ERR_USED_BRIDGE,
                'Network already exists (%s)' % (network, ))
        if bonding:
            _validateInterNetworkCompatibility(_netinfo, vlan, bonding)
        else:
            for nic in nics:
                _validateInterNetworkCompatibility(_netinfo, vlan, nic)

    # defaultRoute is set either explicitly by the client, OR if we're adding
    # the management network.
    # REQUIRED_FOR: clusterLevel<=3.3
    #       remove reference to constants.LEGACY_MANAGEMENT_NETWORKS
    if defaultRoute is None:
        defaultRoute = network in constants.LEGACY_MANAGEMENT_NETWORKS

    logging.info(
        "Adding network %s with vlan=%s, bonding=%s, nics=%s,"
        " bondingOptions=%s, mtu=%s, bridged=%s, defaultRoute=%s,"
        "options=%s", network, vlan, bonding, nics, bondingOptions, mtu,
        bridged, defaultRoute, options)

    if configurator is None:
        configurator = ConfiguratorClass()

    bootproto = options.pop('bootproto', None)

    net_ent = _objectivizeNetwork(bridge=network if bridged else None,
                                  vlan_id=vlan,
                                  bonding=bonding,
                                  bondingOptions=bondingOptions,
                                  nics=nics,
                                  mtu=mtu,
                                  ipaddr=ipaddr,
                                  netmask=netmask,
                                  gateway=gateway,
                                  bootproto=bootproto,
                                  dhcpv6=dhcpv6,
                                  blockingdhcp=blockingdhcp,
                                  ipv6addr=ipv6addr,
                                  ipv6gateway=ipv6gateway,
                                  ipv6autoconf=ipv6autoconf,
                                  defaultRoute=defaultRoute,
                                  _netinfo=_netinfo,
                                  configurator=configurator,
                                  opts=options)

    if bridged and network in _netinfo.bridges:
        net_ent_to_configure = net_ent.port
        logging.info("Bridge %s already exists.", network)
        if mtu:
            # The bridge already exists and we attach a new underlying device
            # to it. We need to make sure that the bridge MTU configuration is
            # updated.
            configurator.configApplier.setIfaceMtu(network, mtu)
            # We must also update the vms` tap devices (the bridge ports in
            # this case) so that their MTU is synced with the bridge
            _update_bridge_ports_mtu(net_ent.name, mtu)
    else:
        net_ent_to_configure = net_ent

    if net_ent_to_configure is not None:
        logging.info("Configuring device %s", net_ent_to_configure)
        net_ent_to_configure.configure(**options)
    configurator.configureLibvirtNetwork(network, net_ent)
    if hostQos is not None:
        configurator.configureQoS(hostQos, net_ent)