Exemple #1
0
    def plug(self, network_id, port_id, device_name, mac_address,
             bridge=None, namespace=None, prefix=None):
        """Plugin the interface."""
        if not ip_lib.device_exists(device_name,
                                    self.root_helper,
                                    namespace=namespace):
            ip = ip_lib.IPWrapper(self.root_helper)

            # Enable agent to define the prefix
            if prefix:
                tap_name = device_name.replace(prefix, 'tap')
            else:
                tap_name = device_name.replace(self.DEV_NAME_PREFIX, 'tap')
            root_veth, ns_veth = ip.add_veth(tap_name, device_name)
            ns_veth.link.set_address(mac_address)

            if self.conf.network_device_mtu:
                root_veth.link.set_mtu(self.conf.network_device_mtu)
                ns_veth.link.set_mtu(self.conf.network_device_mtu)

            if namespace:
                namespace_obj = ip.ensure_namespace(namespace)
                namespace_obj.add_device_to_namespace(ns_veth)

            root_veth.link.set_up()
            ns_veth.link.set_up()

        else:
            LOG.warning(_LW("Device %s already exists"), device_name)
Exemple #2
0
    def plug(self, network_id, port_id, device_name, mac_address,
             bridge=None, namespace=None, prefix=None):
        """Plug in the interface."""
        if not bridge:
            bridge = self.conf.ovs_integration_bridge

        self.check_bridge_exists(bridge)

        if not ip_lib.device_exists(device_name,
                                    self.root_helper,
                                    namespace=namespace):

            ip = ip_lib.IPWrapper(self.root_helper)
            tap_name = self._get_tap_name(device_name, prefix)

            if self.conf.ovs_use_veth:
                root_dev, ns_dev = ip.add_veth(tap_name, device_name)

            internal = not self.conf.ovs_use_veth
            self._ovs_add_port(bridge, tap_name, port_id, mac_address,
                               internal=internal)

            ns_dev = ip.device(device_name)
            ns_dev.link.set_address(mac_address)

            if self.conf.network_device_mtu:
                ns_dev.link.set_mtu(self.conf.network_device_mtu)
                if self.conf.ovs_use_veth:
                    root_dev.link.set_mtu(self.conf.network_device_mtu)

            if namespace:
                namespace_obj = ip.ensure_namespace(namespace)
                namespace_obj.add_device_to_namespace(ns_dev)

            ns_dev.link.set_up()
            if self.conf.ovs_use_veth:
                root_dev.link.set_up()
        else:
            LOG.warning(_LW("Device %s already exists"), device_name)
 def test_device_does_not_exist(self):
     with mock.patch.object(ip_lib.IPDevice, '_execute') as _execute:
         _execute.return_value = ''
         _execute.side_effect = RuntimeError
         self.assertFalse(ip_lib.device_exists('eth0'))
Exemple #4
0
 def check_bridge_exists(self, bridge):
     if not ip_lib.device_exists(bridge):
         raise Exception(_('Bridge %s does not exist') % bridge)
 def test_device_exists(self):
     with mock.patch.object(ip_lib.IPDevice, '_execute') as _execute:
         _execute.return_value = LINK_SAMPLE[1]
         self.assertTrue(ip_lib.device_exists('eth0'))
         _execute.assert_called_once_with('o', 'link', ('show', 'eth0'))
Exemple #6
0
    def _ensure_local_port(self, network_id, subnet_id, prefix,
                           network_type):
        driver = importutils.import_object(self.conf.interface_driver,
                                           self.conf)

        host_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, socket.gethostname()))

        name = 'ASTARA:RUG:%s' % network_type.upper()

        query_dict = dict(device_owner=DEVICE_OWNER_RUG,
                          device_id=host_id,
                          name=name,
                          network_id=network_id)

        ports = self.api_client.list_ports(**query_dict)['ports']

        if not ports and self.conf.legacy_fallback_mode:
            LOG.info(_LI('Attempting legacy query for %s.'), name)
            query_dict.update({
                'name': name.replace('ASTARA', 'AKANDA'),
                'device_owner': DEVICE_OWNER_RUG.replace('astara', 'akanda')
            })
            ports = self.api_client.list_ports(**query_dict)['ports']

        if ports and 'AKANDA' in ports[0]['name']:
            port = Port.from_dict(ports[0])
            LOG.info(
                _LI('migrating port to ASTARA for port %r and using local %s'),
                port,
                network_type
            )
            self.api_client.update_port(
                port.id,
                {
                    'port': {
                        'name': port.name.replace('AKANDA', 'ASTARA'),
                        'device_owner': DEVICE_OWNER_RUG
                    }
                }
            )
        elif ports:
            port = Port.from_dict(ports[0])
            LOG.info(_LI('already have local %s port, using %r'),
                     network_type, port)
        else:
            LOG.info(_LI('creating a new local %s port'), network_type)
            port_dict = {
                'admin_state_up': True,
                'network_id': network_id,
                'device_owner': DEVICE_OWNER_ROUTER_INT,  # lying here for IP
                'name': name,
                'device_id': host_id,
                'fixed_ips': [{
                    'subnet_id': subnet_id
                }],
                'binding:host_id': socket.gethostname()
            }
            port = Port.from_dict(
                self.api_client.create_port(dict(port=port_dict))['port'])

            # remove lie that enabled us pick IP on slaac subnet
            self.api_client.update_port(
                port.id,
                {'port': {'device_owner': DEVICE_OWNER_RUG}}
            )
            port.device_owner = DEVICE_OWNER_RUG

            LOG.info(_LI('new local %s port: %r'), network_type, port)

        # create the tap interface if it doesn't already exist
        if not ip_lib.device_exists(driver.get_device_name(port)):
            driver.plug(
                port.network_id,
                port.id,
                driver.get_device_name(port),
                port.mac_address)

            # add sleep to ensure that port is setup before use
            time.sleep(1)

        try:
            fixed_ip = [fip for fip in port.fixed_ips
                        if fip.subnet_id == subnet_id][0]
        except IndexError:
            raise MissingIPAllocation(port.id)

        ip_cidr = '%s/%s' % (fixed_ip.ip_address, prefix.split('/')[1])
        driver.init_l3(driver.get_device_name(port), [ip_cidr])
        return ip_cidr
Exemple #7
0
    def _ensure_local_port(self, network_id, subnet_id, prefix, network_type):
        driver = importutils.import_object(self.conf.interface_driver,
                                           self.conf)

        host_id = str(uuid.uuid5(uuid.NAMESPACE_DNS, socket.gethostname()))

        name = 'ASTARA:RUG:%s' % network_type.upper()

        query_dict = dict(device_owner=DEVICE_OWNER_RUG,
                          device_id=host_id,
                          name=name,
                          network_id=network_id)

        ports = self.api_client.list_ports(**query_dict)['ports']

        if not ports and self.conf.legacy_fallback_mode:
            LOG.info(_LI('Attempting legacy query for %s.'), name)
            query_dict.update({
                'name':
                name.replace('ASTARA', 'AKANDA'),
                'device_owner':
                DEVICE_OWNER_RUG.replace('astara', 'akanda')
            })
            ports = self.api_client.list_ports(**query_dict)['ports']

        if ports and 'AKANDA' in ports[0]['name']:
            port = Port.from_dict(ports[0])
            LOG.info(
                _LI('migrating port to ASTARA for port %r and using local %s'),
                port, network_type)
            self.api_client.update_port(
                port.id, {
                    'port': {
                        'name': port.name.replace('AKANDA', 'ASTARA'),
                        'device_owner': DEVICE_OWNER_RUG
                    }
                })
        elif ports:
            port = Port.from_dict(ports[0])
            LOG.info(_LI('already have local %s port, using %r'), network_type,
                     port)
        else:
            LOG.info(_LI('creating a new local %s port'), network_type)
            port_dict = {
                'admin_state_up': True,
                'network_id': network_id,
                'device_owner': DEVICE_OWNER_ROUTER_INT,  # lying here for IP
                'name': name,
                'device_id': host_id,
                'fixed_ips': [{
                    'subnet_id': subnet_id
                }],
                'binding:host_id': socket.gethostname()
            }
            port = Port.from_dict(
                self.api_client.create_port(dict(port=port_dict))['port'])

            # remove lie that enabled us pick IP on slaac subnet
            self.api_client.update_port(
                port.id, {'port': {
                    'device_owner': DEVICE_OWNER_RUG
                }})
            port.device_owner = DEVICE_OWNER_RUG

            LOG.info(_LI('new local %s port: %r'), network_type, port)

        # create the tap interface if it doesn't already exist
        if not ip_lib.device_exists(driver.get_device_name(port)):
            driver.plug(port.network_id, port.id, driver.get_device_name(port),
                        port.mac_address)

            # add sleep to ensure that port is setup before use
            time.sleep(1)

        try:
            fixed_ip = [
                fip for fip in port.fixed_ips if fip.subnet_id == subnet_id
            ][0]
        except IndexError:
            raise MissingIPAllocation(port.id)

        ip_cidr = '%s/%s' % (fixed_ip.ip_address, prefix.split('/')[1])
        driver.init_l3(driver.get_device_name(port), [ip_cidr])
        return ip_cidr
Exemple #8
0
 def test_device_does_not_exist(self):
     with mock.patch.object(ip_lib.IPDevice, '_execute') as _execute:
         _execute.return_value = ''
         _execute.side_effect = RuntimeError
         self.assertFalse(ip_lib.device_exists('eth0'))
Exemple #9
0
 def test_device_exists(self):
     with mock.patch.object(ip_lib.IPDevice, '_execute') as _execute:
         _execute.return_value = LINK_SAMPLE[1]
         self.assertTrue(ip_lib.device_exists('eth0'))
         _execute.assert_called_once_with('o', 'link', ('show', 'eth0'))