Example #1
0
 def create_vif_interim_network(self, vif):
     net_name = self.get_vif_interim_net_name(vif['id'])
     # In a pooled environment, make the network shared in order to ensure
     # it can also be used in the target host while live migrating.
     # "assume_network_is_shared" flag does not affect environments where
     # storage pools are not used.
     network_rec = {'name_label': net_name,
                    'name_description': "interim network for vif[%s]"
                    % vif['id'],
                    'other_config': {'assume_network_is_shared': 'true'}}
     network_ref = network_utils.find_network_with_name_label(
         self._session, net_name)
     if network_ref:
         # already exist, just return
         # in some scenarios: e..g resize/migrate, it won't create new
         # interim network.
         return network_ref
     try:
         network_ref = self._session.network.create(network_rec)
     except Exception as e:
         LOG.warning("Failed to create interim network for vif %(if)s, "
                     "exception:%(exception)s",
                     {'if': vif, 'exception': e})
         raise exception.VirtualInterfacePlugException(
             _("Failed to create the interim network for vif"))
     return network_ref
Example #2
0
    def test_find_network_with_name_returns_none(self):
        session = mock.Mock()
        session.network.get_by_name_label.return_value = []

        result = network_utils.find_network_with_name_label(session, "label")

        self.assertIsNone(result)
Example #3
0
 def create_vif_interim_network(self, vif):
     net_name = self.get_vif_interim_net_name(vif['id'])
     # In a pooled environment, make the network shared in order to ensure
     # it can also be used in the target host while live migrating.
     # "assume_network_is_shared" flag does not affect environments where
     # storage pools are not used.
     network_rec = {
         'name_label': net_name,
         'name_description': "interim network for vif[%s]" % vif['id'],
         'other_config': {
             'assume_network_is_shared': 'true'
         }
     }
     network_ref = network_utils.find_network_with_name_label(
         self._session, net_name)
     if network_ref:
         # already exist, just return
         # in some scenarios: e..g resize/migrate, it won't create new
         # interim network.
         return network_ref
     try:
         network_ref = self._session.network.create(network_rec)
     except Exception as e:
         LOG.warning(
             "Failed to create interim network for vif %(if)s, "
             "exception:%(exception)s", {
                 'if': vif,
                 'exception': e
             })
         raise exception.VirtualInterfacePlugException(
             _("Failed to create the interim network for vif"))
     return network_ref
Example #4
0
 def create_vif_interim_network(self, vif):
     net_name = self.get_vif_interim_net_name(vif['id'])
     network_rec = {
         'name_label': net_name,
         'name_description': "interim network for vif",
         'other_config': {}
     }
     network_ref = network_utils.find_network_with_name_label(
         self._session, net_name)
     if network_ref:
         # already exist, just return
         # in some scenarios: e..g resize/migrate, it won't create new
         # interim network.
         return network_ref
     try:
         network_ref = self._session.network.create(network_rec)
     except Exception as e:
         LOG.warning(
             _LW("Failed to create interim network for vif %(if)s, "
                 "exception:%(exception)s"), {
                     'if': vif,
                     'exception': e
                 })
         raise exception.VirtualInterfacePlugException(
             _("Failed to create the interim network for vif"))
     return network_ref
Example #5
0
    def test_find_network_with_name_returns_none(self):
        session = mock.Mock()
        session.network.get_by_name_label.return_value = []

        result = network_utils.find_network_with_name_label(session, "label")

        self.assertIsNone(result)
Example #6
0
    def test_find_network_with_name_label_works(self):
        session = mock.Mock()
        session.network.get_by_name_label.return_value = ["net"]

        result = network_utils.find_network_with_name_label(session, "label")

        self.assertEqual("net", result)
        session.network.get_by_name_label.assert_called_once_with("label")
Example #7
0
    def test_find_network_with_name_label_works(self):
        session = mock.Mock()
        session.network.get_by_name_label.return_value = ["net"]

        result = network_utils.find_network_with_name_label(session, "label")

        self.assertEqual("net", result)
        session.network.get_by_name_label.assert_called_once_with("label")
Example #8
0
    def _ensure_vlan_bridge(self, network):
        """Ensure that a VLAN bridge exists."""

        vlan_num = network.get_meta('vlan')
        bridge = network['bridge']
        bridge_interface = (CONF.vlan_interface
                            or network.get_meta('bridge_interface'))
        # Check whether bridge already exists
        # Retrieve network whose name_label is "bridge"
        network_ref = network_utils.find_network_with_name_label(
            self._session, bridge)
        if network_ref is None:
            # If bridge does not exists
            # 1 - create network
            description = 'network for nova bridge %s' % bridge
            network_rec = {
                'name_label': bridge,
                'name_description': description,
                'other_config': {}
            }
            network_ref = self._session.call_xenapi('network.create',
                                                    network_rec)
            # 2 - find PIF for VLAN NOTE(salvatore-orlando): using double
            # quotes inside single quotes as xapi filter only support
            # tokens in double quotes
            expr = ('field "device" = "%s" and field "VLAN" = "-1"' %
                    bridge_interface)
            pifs = self._session.call_xenapi('PIF.get_all_records_where', expr)
            pif_ref = None
            # Multiple PIF are ok: we are dealing with a pool
            if len(pifs) == 0:
                raise Exception(
                    _('Found no PIF for device %s') % bridge_interface)
            for pif_ref in pifs.keys():
                self._session.call_xenapi('VLAN.create', pif_ref,
                                          str(vlan_num), network_ref)
        else:
            # Check VLAN tag is appropriate
            network_rec = self._session.call_xenapi('network.get_record',
                                                    network_ref)
            # Retrieve PIFs from network
            for pif_ref in network_rec['PIFs']:
                # Retrieve VLAN from PIF
                pif_rec = self._session.call_xenapi('PIF.get_record', pif_ref)
                pif_vlan = int(pif_rec['VLAN'])
                # Raise an exception if VLAN != vlan_num
                if pif_vlan != vlan_num:
                    raise Exception(
                        _("PIF %(pif_uuid)s for network "
                          "%(bridge)s has VLAN id %(pif_vlan)d. "
                          "Expected %(vlan_num)d"), {
                              'pif_uuid': pif_rec['uuid'],
                              'bridge': bridge,
                              'pif_vlan': pif_vlan,
                              'vlan_num': vlan_num
                          })

        return network_ref
Example #9
0
    def _ensure_vlan_bridge(self, network):
        """Ensure that a VLAN bridge exists."""

        vlan_num = network.get_meta('vlan')
        bridge = network['bridge']
        bridge_interface = (CONF.vlan_interface or
                            network.get_meta('bridge_interface'))
        # Check whether bridge already exists
        # Retrieve network whose name_label is "bridge"
        network_ref = network_utils.find_network_with_name_label(
                                        self._session, bridge)
        if network_ref is None:
            # If bridge does not exists
            # 1 - create network
            description = 'network for nova bridge %s' % bridge
            network_rec = {'name_label': bridge,
                           'name_description': description,
                           'other_config': {}}
            network_ref = self._session.call_xenapi('network.create',
                                                    network_rec)
            # 2 - find PIF for VLAN NOTE(salvatore-orlando): using double
            # quotes inside single quotes as xapi filter only support
            # tokens in double quotes
            expr = ('field "device" = "%s" and field "VLAN" = "-1"' %
                    bridge_interface)
            pifs = self._session.call_xenapi('PIF.get_all_records_where',
                                             expr)
            pif_ref = None
            # Multiple PIF are ok: we are dealing with a pool
            if len(pifs) == 0:
                raise Exception(_('Found no PIF for device %s') %
                                bridge_interface)
            for pif_ref in pifs.keys():
                self._session.call_xenapi('VLAN.create',
                                          pif_ref,
                                          str(vlan_num),
                                          network_ref)
        else:
            # Check VLAN tag is appropriate
            network_rec = self._session.call_xenapi('network.get_record',
                                                    network_ref)
            # Retrieve PIFs from network
            for pif_ref in network_rec['PIFs']:
                # Retrieve VLAN from PIF
                pif_rec = self._session.call_xenapi('PIF.get_record',
                                                    pif_ref)
                pif_vlan = int(pif_rec['VLAN'])
                # Raise an exception if VLAN != vlan_num
                if pif_vlan != vlan_num:
                    raise Exception(_("PIF %(pif_uuid)s for network "
                                      "%(bridge)s has VLAN id %(pif_vlan)d. "
                                      "Expected %(vlan_num)d"),
                                    {'pif_uuid': pif_rec['uuid'],
                                     'bridge': bridge,
                                     'pif_vlan': pif_vlan,
                                     'vlan_num': vlan_num})

        return network_ref
Example #10
0
 def _get_network_by_vif(self, vif_id):
     net_name = self.get_vif_interim_net_name(vif_id)
     network = network_utils.find_network_with_name_label(
         self._session, net_name)
     if network is None:
         LOG.debug("Failed to find network for vif id %(if)s",
                   {'if': vif_id})
         return
     return network
Example #11
0
 def _get_network_by_vif(self, vif_id):
     net_name = self.get_vif_interim_net_name(vif_id)
     network = network_utils.find_network_with_name_label(
         self._session, net_name)
     if network is None:
         LOG.debug("Failed to find network for vif id %(if)s",
                   {'if': vif_id})
         return
     return network
Example #12
0
    def unplug(self, instance, vif, vm_ref):
        """unplug vif:
        1. unplug and destroy vif.
        2. delete the patch port pair between the integration bridge and
           the interim network.
        3. destroy the interim network
        4. delete the OVS bridge service for the interim network
        """
        super(XenAPIOpenVswitchDriver, self).unplug(instance, vif, vm_ref)

        net_name = self.get_vif_interim_net_name(vif)
        network = network_utils.find_network_with_name_label(
            self._session, net_name)
        if network is None:
            return
        vifs = self._session.network.get_VIFs(network)
        if vifs:
            # only remove the interim network when it's empty.
            # for resize/migrate on local host, vifs on both of the
            # source and target VM will be connected to the same
            # interim network.
            return
        LOG.debug('destroying patch port pair for vif: vif_id=%(vif_id)s',
                  {'vif_id': vif['id']})
        bridge_name = self._session.network.get_bridge(network)
        patch_port1, patch_port2 = self._get_patch_port_pair_names(vif['id'])
        try:
            # delete the patch port pair
            self._ovs_del_port(bridge_name, patch_port1)
            self._ovs_del_port(CONF.xenserver.ovs_integration_bridge,
                               patch_port2)
        except Exception as e:
            LOG.warn(_LW("Failed to delete patch port pair for vif %(if)s,"
                         " exception:%(exception)s"),
                     {'if': vif, 'exception': e}, instance=instance)
            raise exception.VirtualInterfaceUnplugException(
                reason=_("Failed to delete patch port pair"))

        LOG.debug('destroying network: network=%(network)s,'
                  'bridge=%(br)s',
                  {'network': network, 'br': bridge_name})
        try:
            self._session.network.destroy(network)
            # delete bridge if it still exists.
            # As there is patch port existing on this bridge when destroying
            # the VM vif (which happens when shutdown the VM), the bridge
            # won't be destroyed automatically by XAPI. So let's destroy it
            # at here.
            self._ovs_del_br(bridge_name)
        except Exception as e:
            LOG.warn(_LW("Failed to delete bridge for vif %(if)s, "
                         "exception:%(exception)s"),
                     {'if': vif, 'exception': e}, instance=instance)
            raise exception.VirtualInterfaceUnplugException(
                reason=_("Failed to delete bridge"))
Example #13
0
    def delete_network_and_bridge(self, instance, vif):
        net_name = self.get_vif_interim_net_name(vif['id'])
        network = network_utils.find_network_with_name_label(
            self._session, net_name)
        if network is None:
            LOG.debug("Didn't find network by name %s", net_name,
                      instance=instance)
            return
        LOG.debug('destroying patch port pair for vif: vif_id=%(vif_id)s',
                  {'vif_id': vif['id']})
        bridge_name = self._session.network.get_bridge(network)
        patch_port1, tap_name = self._get_patch_port_pair_names(vif['id'])
        try:
            # delete the patch port pair
            host_network.ovs_del_port(self._session, bridge_name, patch_port1)
        except Exception as e:
            LOG.warning("Failed to delete patch port pair for vif %(if)s,"
                        " exception:%(exception)s",
                        {'if': vif, 'exception': e}, instance=instance)
            raise exception.VirtualInterfaceUnplugException(
                reason=_("Failed to delete patch port pair"))

        LOG.debug('destroying network: network=%(network)s,'
                  'bridge=%(br)s',
                  {'network': network, 'br': bridge_name})
        try:
            self._session.network.destroy(network)
            # delete bridge if it still exists.
            # As there is patch port existing on this bridge when destroying
            # the VM vif (which happens when shutdown the VM), the bridge
            # won't be destroyed automatically by XAPI. So let's destroy it
            # at here.
            host_network.ovs_del_br(self._session, bridge_name)

            qbr_name = self._get_qbr_name(vif['id'])
            qvb_name, qvo_name = self._get_veth_pair_names(vif['id'])
            if self._device_exists(qbr_name):
                # delete tap port, qvb port and qbr
                LOG.debug(
                    "destroy linux bridge %(qbr)s when unplug vif %(vif)s",
                    {'qbr': qbr_name, 'vif': vif['id']})
                self._delete_linux_port(qbr_name, tap_name)
                self._delete_linux_port(qbr_name, qvb_name)
                self._delete_linux_bridge(qbr_name)
            host_network.ovs_del_port(self._session,
                                      CONF.xenserver.ovs_integration_bridge,
                                      qvo_name)
        except Exception as e:
            LOG.warning("Failed to delete bridge for vif %(if)s, "
                        "exception:%(exception)s",
                        {'if': vif, 'exception': e}, instance=instance)
            raise exception.VirtualInterfaceUnplugException(
                reason=_("Failed to delete bridge"))
Example #14
0
File: vif.py Project: vmturbo/nova
    def delete_network_and_bridge(self, instance, vif):
        net_name = self.get_vif_interim_net_name(vif['id'])
        network = network_utils.find_network_with_name_label(
            self._session, net_name)
        if network is None:
            LOG.debug("Didn't find network by name %s", net_name,
                      instance=instance)
            return
        LOG.debug('destroying patch port pair for vif: vif_id=%(vif_id)s',
                  {'vif_id': vif['id']})
        bridge_name = self._session.network.get_bridge(network)
        patch_port1, tap_name = self._get_patch_port_pair_names(vif['id'])
        try:
            # delete the patch port pair
            self._ovs_del_port(bridge_name, patch_port1)
        except Exception as e:
            LOG.warning(_LW("Failed to delete patch port pair for vif %(if)s,"
                            " exception:%(exception)s"),
                        {'if': vif, 'exception': e}, instance=instance)
            raise exception.VirtualInterfaceUnplugException(
                reason=_("Failed to delete patch port pair"))

        LOG.debug('destroying network: network=%(network)s,'
                  'bridge=%(br)s',
                  {'network': network, 'br': bridge_name})
        try:
            self._session.network.destroy(network)
            # delete bridge if it still exists.
            # As there is patch port existing on this bridge when destroying
            # the VM vif (which happens when shutdown the VM), the bridge
            # won't be destroyed automatically by XAPI. So let's destroy it
            # at here.
            self._ovs_del_br(bridge_name)

            qbr_name = self._get_qbr_name(vif['id'])
            qvb_name, qvo_name = self._get_veth_pair_names(vif['id'])
            if self._device_exists(qbr_name):
                # delete tap port, qvb port and qbr
                LOG.debug(
                    "destroy linux bridge %(qbr)s when unplug vif %(vif)s",
                    {'qbr': qbr_name, 'vif': vif['id']})
                self._delete_linux_port(qbr_name, tap_name)
                self._delete_linux_port(qbr_name, qvb_name)
                self._delete_linux_bridge(qbr_name)
            self._ovs_del_port(CONF.xenserver.ovs_integration_bridge, qvo_name)
        except Exception as e:
            LOG.warning(_LW("Failed to delete bridge for vif %(if)s, "
                            "exception:%(exception)s"),
                        {'if': vif, 'exception': e}, instance=instance)
            raise exception.VirtualInterfaceUnplugException(
                reason=_("Failed to delete bridge"))
Example #15
0
 def create_vif_interim_network(self, vif):
     net_name = self.get_vif_interim_net_name(vif)
     network_rec = {'name_label': net_name,
                'name_description': "interim network for vif",
                'other_config': {}}
     network_ref = network_utils.find_network_with_name_label(
         self._session, net_name)
     if network_ref:
         # already exist, just return
         # in some scenarios: e..g resize/migrate, it won't create new
         # interim network.
         return network_ref
     try:
         network_ref = self._session.network.create(network_rec)
     except Exception as e:
         LOG.warning(_LW("Failed to create interim network for vif %(if)s, "
                         "exception:%(exception)s"),
                     {'if': vif, 'exception': e})
         raise exception.VirtualInterfacePlugException(
             _("Failed to create the interim network for vif"))
     return network_ref
Example #16
0
 def unplug(self, instance, vif, vm_ref):
     """unplug vif:
     1. delete the patch port pair between the integration bridge and
        the qbr linux bridge(if exist) and the interim network.
     2. destroy the interim network
     3. delete the OVS bridge service for the interim network
     4. delete linux bridge qbr and related ports if exist
     """
     super(XenAPIOpenVswitchDriver, self).unplug(instance, vif, vm_ref)
     net_name = self.get_vif_interim_net_name(vif['id'])
     network = network_utils.find_network_with_name_label(
         self._session, net_name)
     if network is None:
         return
     vifs = self._session.network.get_VIFs(network)
     if vifs:
         # only remove the interim network when it's empty.
         # for resize/migrate on local host, vifs on both of the
         # source and target VM will be connected to the same
         # interim network.
         return
     self.delete_network_and_bridge(instance, vif)
Example #17
0
File: vif.py Project: vmturbo/nova
 def unplug(self, instance, vif, vm_ref):
     """unplug vif:
     1. delete the patch port pair between the integration bridge and
        the qbr linux bridge(if exist) and the interim network.
     2. destroy the interim network
     3. delete the OVS bridge service for the interim network
     4. delete linux bridge qbr and related ports if exist
     """
     super(XenAPIOpenVswitchDriver, self).unplug(instance, vif, vm_ref)
     net_name = self.get_vif_interim_net_name(vif['id'])
     network = network_utils.find_network_with_name_label(
         self._session, net_name)
     if network is None:
         return
     vifs = self._session.network.get_VIFs(network)
     if vifs:
         # only remove the interim network when it's empty.
         # for resize/migrate on local host, vifs on both of the
         # source and target VM will be connected to the same
         # interim network.
         return
     self.delete_network_and_bridge(instance, vif)
Example #18
0
    def unplug(self, instance, vif, vm_ref):
        """unplug vif:
        1. delete the patch port pair between the integration bridge and
           the qbr linux bridge(if exist) and the interim network.
        2. destroy the interim network
        3. delete the OVS bridge service for the interim network
        4. delete linux bridge qbr and related ports if exist
        """
        super(XenAPIOpenVswitchDriver, self).unplug(instance, vif, vm_ref)

        net_name = self.get_vif_interim_net_name(vif)
        network = network_utils.find_network_with_name_label(
            self._session, net_name)
        if network is None:
            return
        vifs = self._session.network.get_VIFs(network)
        if vifs:
            # only remove the interim network when it's empty.
            # for resize/migrate on local host, vifs on both of the
            # source and target VM will be connected to the same
            # interim network.
            return
        LOG.debug('destroying patch port pair for vif: vif_id=%(vif_id)s',
                  {'vif_id': vif['id']})
        bridge_name = self._session.network.get_bridge(network)
        patch_port1, tap_name = self._get_patch_port_pair_names(vif['id'])
        try:
            # delete the patch port pair
            self._ovs_del_port(bridge_name, patch_port1)
        except Exception as e:
            LOG.warning(_LW("Failed to delete patch port pair for vif %(if)s,"
                            " exception:%(exception)s"), {
                                'if': vif,
                                'exception': e
                            },
                        instance=instance)
            raise exception.VirtualInterfaceUnplugException(
                reason=_("Failed to delete patch port pair"))

        LOG.debug('destroying network: network=%(network)s,'
                  'bridge=%(br)s', {
                      'network': network,
                      'br': bridge_name
                  })
        try:
            self._session.network.destroy(network)
            # delete bridge if it still exists.
            # As there is patch port existing on this bridge when destroying
            # the VM vif (which happens when shutdown the VM), the bridge
            # won't be destroyed automatically by XAPI. So let's destroy it
            # at here.
            self._ovs_del_br(bridge_name)

            qbr_name = self._get_qbr_name(vif['id'])
            qvb_name, qvo_name = self._get_veth_pair_names(vif['id'])
            if self._device_exists(qbr_name):
                # delete tap port, qvb port and qbr
                LOG.debug(
                    "destroy linux bridge %(qbr)s when unplug vif %(vif)s", {
                        'qbr': qbr_name,
                        'vif': vif['id']
                    })
                self._delete_linux_port(qbr_name, tap_name)
                self._delete_linux_port(qbr_name, qvb_name)
                self._delete_linux_bridge(qbr_name)
            self._ovs_del_port(CONF.xenserver.ovs_integration_bridge, qvo_name)
        except Exception as e:
            LOG.warning(_LW("Failed to delete bridge for vif %(if)s, "
                            "exception:%(exception)s"), {
                                'if': vif,
                                'exception': e
                            },
                        instance=instance)
            raise exception.VirtualInterfaceUnplugException(
                reason=_("Failed to delete bridge"))