def plug_midonet(self, instance, vif): """Plug into MidoNet's network port This accomplishes binding of the vif to a MidoNet virtual port """ LOG.info('2222') if_local_name = 'tap%s' % vif['id'][:11] if_remote_name = 'ns%s' % vif['id'][:11] port_id = network.get_ovs_interfaceid(vif) # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) utils.execute('mm-ctl', '--bind-port', port_id, if_local_name, run_as_root=True) except Exception: LOG.exception("Failed to configure network") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def plug_ovs(self, instance, vif): if_local_name = 'tap%s' % vif['id'][:11] if_remote_name = 'ns%s' % vif['id'][:11] bridge = vif['network']['bridge'] # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) linux_net.create_ovs_vif_port(bridge, if_local_name, network.get_ovs_interfaceid(vif), vif['address'], instance['uuid']) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: LOG.exception("Failed to configure network") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def plug(self, instance, vif): vif_type = vif['type'] LOG.debug('Plug vif_type=%(vif_type)s instance=%(instance)s ' 'vif=%(vif)s', {'vif_type': vif_type, 'instance': instance, 'vif': vif}) if_local_name = 'veth%s' % vif['id'][:8] if_remote_name = 'ns%s' % vif['id'][:8] # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) utils.execute('ip', 'link', 'set', if_remote_name, 'address', vif['address'], run_as_root=True) except Exception: LOG.exception("Failed to configure network") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def plug_iovisor(self, instance, vif): """Plug docker vif into IOvisor Creates a port on IOvisor and onboards the interface """ if_local_name = 'tap%s' % vif['id'][:11] if_remote_name = 'ns%s' % vif['id'][:11] iface_id = vif['id'] net_id = vif['network']['id'] tenant_id = instance['project_id'] # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) utils.execute('ifc_ctl', 'gateway', 'add_port', if_local_name, run_as_root=True) utils.execute('ifc_ctl', 'gateway', 'ifup', if_local_name, 'access_vm', vif['network']['label'] + "_" + iface_id, vif['address'], 'pgtag2=%s' % net_id, 'pgtag1=%s' % tenant_id, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: LOG.exception("Failed to configure network on IOvisor") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def plug_bridge(self, instance, vif): LOG.info('5555') if_local_name = 'tap%s' % vif['id'][:11] if_remote_name = 'ns%s' % vif['id'][:11] bridge = vif['network']['bridge'] gateway = network.find_gateway(instance, vif['network']) vlan = vif.get('vlan') if vlan is not None: iface = (CONF.vlan_interface or vif['network'].get_meta('bridge_interface')) linux_net.LinuxBridgeInterfaceDriver.ensure_vlan_bridge( vlan, bridge, iface, net_attrs=vif, mtu=vif.get('mtu')) iface = 'vlan%s' % vlan else: iface = (CONF.flat_interface or vif['network'].get_meta('bridge_interface')) LOG.debug('Ensuring bridge for %s - %s' % (iface, bridge)) linux_net.LinuxBridgeInterfaceDriver.ensure_bridge( bridge, iface, net_attrs=vif, gateway=gateway) # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all # associated resources (remove from the bridge, its pair, etc...) utils.execute('ip', 'link', 'set', if_local_name, 'address', self._fe_random_mac(), run_as_root=True) utils.execute('brctl', 'addif', bridge, if_local_name, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: LOG.exception("Failed to configure network") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def _create_veth_pair(self, if_local_name, if_remote_name, bridge): undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all # associated resources (remove from the bridge, its pair, etc...) utils.execute('ip', 'link', 'set', if_local_name, 'address', self._fe_random_mac(), run_as_root=True) utils.execute('brctl', 'addif', bridge, if_local_name, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: LOG.exception("Failed to configure network") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg)
def attach(self, instance, vif, container_id): vif_type = vif['type'] LOG.debug( 'Attach vif_type=%(vif_type)s instance=%(instance)s ' 'vif=%(vif)s', { 'vif_type': vif_type, 'instance': instance, 'vif': vif }) if_local_name = 'veth%s' % vif['id'][:8] if_remote_name = 'ns%s' % vif['id'][:8] undo_mgr = utils.UndoManager() undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) ipv4_address = '0.0.0.0' ipv6_address = None if 'subnets' in vif['network']: subnets = vif['network']['subnets'] for subnet in subnets: ips = subnet['ips'][0] if (ips['version'] == 4): if ips['address'] is not None: ipv4_address = ips['address'] ipv4_netmask = subnet['cidr'].split('/')[1] ipv4_gateway = subnet['gateway']['address'] if (ips['version'] == 6): if ips['address'] is not None: ipv6_address = ips['address'] ipv6_netmask = subnet['cidr'].split('/')[1] ipv6_gateway = subnet['gateway']['address'] params = { 'ip_address': ipv4_address, 'vn_id': vif['network']['id'], 'display_name': instance['display_name'], 'hostname': instance['hostname'], 'host': instance['host'], 'vm_project_id': instance['project_id'], 'port_type': 'NovaVMPort', 'ip6_address': ipv6_address, } try: utils.execute('ip', 'link', 'set', if_remote_name, 'netns', container_id, run_as_root=True) result = self._vrouter_client.add_port(instance['uuid'], vif['id'], if_local_name, vif['address'], **params) if not result: # follow the exception path raise RuntimeError('add_port returned %s' % str(result)) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: LOG.exception("Failed to attach the network") msg = _('Failed to attach the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance) try: utils.execute('ip', 'netns', 'exec', container_id, 'ip', 'link', 'set', if_remote_name, 'address', vif['address'], run_as_root=True) if ipv6_address: ip = ipv6_address + "/" + ipv6_netmask gateway = ipv6_gateway utils.execute('ip', 'netns', 'exec', container_id, 'ifconfig', if_remote_name, 'inet6', 'add', ip, run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ip', '-6', 'route', 'replace', 'default', 'via', gateway, 'dev', if_remote_name, run_as_root=True) if ipv4_address != '0.0.0.0': ip = ipv4_address + "/" + ipv4_netmask gateway = ipv4_gateway utils.execute('ip', 'netns', 'exec', container_id, 'ifconfig', if_remote_name, ip, run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ip', 'route', 'replace', 'default', 'via', gateway, 'dev', if_remote_name, run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ip', 'link', 'set', if_remote_name, 'up', run_as_root=True) except Exception: LOG.exception(_("Failed to attach vif"), instance=instance)
def plug_ovs_hybrid(self, instance, vif): """Plug using hybrid strategy Create a per-VIF linux bridge, then link that bridge to the OVS integration bridge via a veth device, setting up the other end of the veth device just like a normal OVS port. Then boot the VIF on the linux bridge. and connect the tap port to linux bridge """ if_local_name = 'tap%s' % vif['id'][:11] if_remote_name = 'ns%s' % vif['id'][:11] iface_id = self.get_ovs_interfaceid(vif) br_name = self.get_br_name(vif['id']) v1_name, v2_name = self.get_veth_pair_names(vif['id']) # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: if not linux_net.device_exists(br_name): utils.execute('brctl', 'addbr', br_name, run_as_root=True) # Incase of failure undo the Steps # Deleting/Undoing the interface will delete all # associated resources undo_mgr.undo_with(lambda: utils.execute( 'brctl', 'delbr', br_name, run_as_root=True)) # LOG.exception('Throw Test exception with bridgename %s' # % br_name) utils.execute('brctl', 'setfd', br_name, 0, run_as_root=True) utils.execute('brctl', 'stp', br_name, 'off', run_as_root=True) utils.execute( 'tee', ('/sys/class/net/%s/bridge/multicast_snooping' % br_name), process_input='0', run_as_root=True, check_exit_code=[0, 1]) if not linux_net.device_exists(v2_name): linux_net._create_veth_pair(v1_name, v2_name) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', v1_name, run_as_root=True)) utils.execute('ip', 'link', 'set', br_name, 'up', run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'set', br_name, 'down', run_as_root=True)) # Deleting/Undoing the interface will delete all # associated resources (remove from the bridge, its # pair, etc...) utils.execute('brctl', 'addif', br_name, v1_name, run_as_root=True) linux_net.create_ovs_vif_port(self.get_bridge_name(vif), v2_name, iface_id, vif['address'], instance['uuid']) undo_mgr.undo_with( lambda: utils.execute('ovs-vsctl', 'del-port', self.get_bridge_name(vif), v2_name, run_as_root=True)) utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # Deleting/Undoing the interface will delete all # associated resources (remove from the bridge, its pair, etc...) utils.execute('brctl', 'addif', br_name, if_local_name, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: msg = "Failed to configure Network." \ " Rolling back the network interfaces %s %s %s %s " % ( br_name, if_local_name, v1_name, v2_name) undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def _setup_network(self, instance, network_info): if not network_info: return container_id = self.find_container_by_name(instance['name']).get('id') if not container_id: return network_info = network_info[0]['network'] netns_path = '/var/run/netns' if not os.path.exists(netns_path): utils.execute('mkdir', '-p', netns_path, run_as_root=True) nspid = self._find_container_pid(container_id) if not nspid: msg = _('Cannot find any PID under container "{0}"') raise RuntimeError(msg.format(container_id)) netns_path = os.path.join(netns_path, container_id) utils.execute('ln', '-sf', '/proc/{0}/ns/net'.format(nspid), '/var/run/netns/{0}'.format(container_id), run_as_root=True) rand = random.randint(0, 100000) if_local_name = 'pvnetl{0}'.format(rand) if_remote_name = 'pvnetr{0}'.format(rand) bridge = network_info['bridge'] ip = self._find_fixed_ip(network_info['subnets']) if not ip: raise RuntimeError(_('Cannot set fixed ip')) undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all associated # resources (remove from the bridge, its pair, etc...) utils.execute('brctl', 'addif', bridge, if_local_name, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) utils.execute('ip', 'link', 'set', if_remote_name, 'netns', nspid, run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ifconfig', if_remote_name, ip, run_as_root=True) except Exception: msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def plug_bridge(self, instance, vif, container_id): if_local_name = 'tap%s' % vif['id'][:11] if_remote_name = 'ns%s' % vif['id'][:11] bridge = vif['network']['bridge'] gateway = network.find_gateway(instance, vif['network']) ip = network.find_fixed_ip(instance, vif['network']) # Device already exists so return. if linux_net.device_exists(if_local_name): return undo_mgr = utils.UndoManager() try: utils.execute('ip', 'link', 'add', 'name', if_local_name, 'type', 'veth', 'peer', 'name', if_remote_name, run_as_root=True) undo_mgr.undo_with(lambda: utils.execute( 'ip', 'link', 'delete', if_local_name, run_as_root=True)) # NOTE(samalba): Deleting the interface will delete all # associated resources (remove from the bridge, its pair, etc...) utils.execute('brctl', 'addif', bridge, if_local_name, run_as_root=True) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) utils.execute('ip', 'link', 'set', if_remote_name, 'netns', container_id, run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ip', 'link', 'set', if_remote_name, 'address', vif['address'], run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ifconfig', if_remote_name, ip, run_as_root=True) utils.execute('ip', 'netns', 'exec', container_id, 'ip', 'route', 'replace', 'default', 'via', gateway, 'dev', if_remote_name, run_as_root=True) except Exception: LOG.exception("Failed to configure network") msg = _('Failed to setup the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance)
def attach(self, instance, vif, container_id): if_local_name = 'veth%s' % vif['id'][:8] if_remote_name = 'ns%s' % vif['id'][:8] undo_mgr = utils.UndoManager() ipv4_address = '0.0.0.0' ipv6_address = None if 'subnets' in vif['network']: subnets = vif['network']['subnets'] for subnet in subnets: ips = subnet['ips'][0] if (ips['version'] == 4): if ips['address'] is not None: ipv4_address = ips['address'] if (ips['version'] == 6): if ips['address'] is not None: ipv6_address = ips['address'] params = { 'ip_address': ipv4_address, 'vn_id': vif['network']['id'], 'display_name': instance['display_name'], 'hostname': instance['hostname'], 'host': instance['host'], 'vm_project_id': instance['project_id'], 'port_type': 'NovaVMPort', 'ip6_address': ipv6_address, } try: utils.execute('ip', 'link', 'set', if_remote_name, 'netns', container_id, run_as_root=True) result = self._vrouter_client.add_port(instance['uuid'], vif['id'], if_local_name, vif['address'], **params) if not result: # follow the exception path raise RuntimeError('add_port returned %s' % str(result)) utils.execute('ip', 'link', 'set', if_local_name, 'up', run_as_root=True) except Exception: LOG.exception("Failed to attach the network") msg = _('Failed to attach the network, rolling back') undo_mgr.rollback_and_reraise(msg=msg, instance=instance) # TODO(NetNS): attempt DHCP client; fallback to manual config if the # container doesn't have an working dhcpclient utils.execute('ip', 'netns', 'exec', container_id, 'dhclient', if_remote_name, run_as_root=True)