def _plug(self, namespace, port, reuse_existing=True): self.plugin_rpc.plug_vip_port(port['id']) interface_name = self.vif_driver.get_device_name(Wrap(port)) if ip_lib.device_exists(interface_name, self.root_helper, namespace): if not reuse_existing: raise exceptions.PreexistingDeviceFailure( dev_name=interface_name ) else: self.vif_driver.plug( port['network_id'], port['id'], interface_name, port['mac_address'], namespace=namespace ) cidrs = [ '%s/%s' % (ip['ip_address'], netaddr.IPNetwork(ip['subnet']['cidr']).prefixlen) for ip in port['fixed_ips'] ] self.vif_driver.init_l3(interface_name, cidrs, namespace=namespace) gw_ip = port['fixed_ips'][0]['subnet'].get('gateway_ip') if gw_ip: cmd = ['route', 'add', 'default', 'gw', gw_ip] ip_wrapper = ip_lib.IPWrapper(self.root_helper, namespace=namespace) ip_wrapper.netns.execute(cmd, check_exit_code=False)
def _plug(self, namespace, port, reuse_existing=True): self.plugin_rpc.plug_vip_port(port['id']) ## 更新DB vip创建时创建的port的信息 interface_name = self.vif_driver.get_device_name(Wrap(port)) #interface_name=u'tap88837aa4-c3' # #判断在命名空间是否存在设备,存在则跳过 if ip_lib.device_exists(interface_name, self.root_helper, namespace): if not reuse_existing: raise exceptions.PreexistingDeviceFailure( dev_name=interface_name ) else: ##不存在这个设备,利用vif_driver进行plug self.vif_driver.plug( port['network_id'], port['id'], interface_name, port['mac_address'], namespace=namespace ) cidrs = [ '%s/%s' % (ip['ip_address'], netaddr.IPNetwork(ip['subnet']['cidr']).prefixlen) for ip in port['fixed_ips'] ] #cidrs=[u'11.11.11.11/24'] self.vif_driver.init_l3(interface_name, cidrs, namespace=namespace) #为网卡设置L3初始化 gw_ip = port['fixed_ips'][0]['subnet'].get('gateway_ip') # 分别出有网关IP和无网关IP的情况 if not gw_ip: host_routes = port['fixed_ips'][0]['subnet'].get('host_routes', []) for host_route in host_routes: if host_route['destination'] == "0.0.0.0/0": gw_ip = host_route['nexthop'] break if gw_ip: # 设置默认网关IP cmd = ['route', 'add', 'default', 'gw', gw_ip] ip_wrapper = ip_lib.IPWrapper(self.root_helper, namespace=namespace) ip_wrapper.netns.execute(cmd, check_exit_code=False) #ip netns exec qlbaas-3195b1f3-a453-4c35-a4a2-aa63b2678765 route add default gw 11.11.11.1 # When delete and re-add the same vip, we need to # 当删除或重添加同样的vip时,我们需要发送gratuitous ARP # send gratuitous ARP to flush the ARP cache in the Router. gratuitous_arp = self.conf.haproxy.send_gratuitous_arp if gratuitous_arp > 0: for ip in port['fixed_ips']: cmd_arping = ['arping', '-U', '-I', interface_name, '-c', gratuitous_arp, ip['ip_address']] ip_wrapper.netns.execute(cmd_arping, check_exit_code=False)
def _plug_instance_port(self, namespace, port, reuse_existing=True): #RPC to update db for vip information self.plugin_rpc.plug_vip_port(port['id']) devices_added = set() devices_added.add(port['id']) interface_name = self.vif_driver.get_device_name(Wrap(port)) if ip_lib.device_exists(interface_name, self.root_helper, namespace): if not reuse_existing: raise exceptions.PreexistingDeviceFailure( dev_name=interface_name) else: self.vif_driver.plug(port['network_id'], port['id'], interface_name, port['mac_address'], namespace=namespace) LOG.debug( _(' _plug_instance_port for %(port)s fixed_ips %(fixed_ips)s '), { 'port': port, 'fixed_ips': port['fixed_ips'] }) cidrs = [ '%s/%s' % (ip['ip_address'], netaddr.IPNetwork( ip['subnet']['cidr']).prefixlen) for ip in port['fixed_ips'] ] self.vif_driver.init_l3(interface_name, cidrs, namespace=namespace) gw_ip = port['fixed_ips'][0]['subnet'].get('gateway_ip') if not gw_ip: host_routes = port['fixed_ips'][0]['subnet'].get('host_routes', []) for host_route in host_routes: if host_route['destination'] == "0.0.0.0/0": gw_ip = host_route['nexthop'] break if gw_ip: cmd = ['route', 'add', 'default', 'gw', gw_ip] ip_wrapper = ip_lib.IPWrapper(self.root_helper, namespace=namespace) ip_wrapper.netns.execute(cmd, check_exit_code=False) # When delete and re-add the same vip, we need to # send gratuitous ARP to flush the ARP cache in the Router. gratuitous_arp = self.conf.haproxy.send_gratuitous_arp if gratuitous_arp > 0: for ip in port['fixed_ips']: cmd_arping = [ 'arping', '-U', '-I', interface_name, '-c', gratuitous_arp, ip['ip_address'] ] ip_wrapper.netns.execute(cmd_arping, check_exit_code=False) self.setup_port_filters(devices_added)
def _plug(self, namespace, port, vip_address, reuse_existing=True): self.plugin_rpc.plug_vip_port(port.id) interface_name = self.vif_driver.get_device_name(port) if ip_lib.device_exists(interface_name, namespace=namespace): if not reuse_existing: raise exceptions.PreexistingDeviceFailure( dev_name=interface_name ) else: self.vif_driver.plug( port.network_id, port.id, interface_name, port.mac_address, namespace=namespace ) cidrs = [ '%s/%s' % (ip.ip_address, netaddr.IPNetwork(ip.subnet.cidr).prefixlen) for ip in port.fixed_ips ] self.vif_driver.init_l3(interface_name, cidrs, namespace=namespace) # Haproxy socket binding to IPv6 VIP address will fail if this address # is not yet ready(i.e tentative address). if netaddr.IPAddress(vip_address).version == 6: device = ip_lib.IPDevice(interface_name, namespace=namespace) device.addr.wait_until_address_ready(vip_address) gw_ip = port.fixed_ips[0].subnet.gateway_ip if not gw_ip: host_routes = port.fixed_ips[0].subnet.host_routes for host_route in host_routes: if host_route.destination == "0.0.0.0/0": gw_ip = host_route.nexthop break else: cmd = ['route', 'add', 'default', 'gw', gw_ip] ip_wrapper = ip_lib.IPWrapper(namespace=namespace) ip_wrapper.netns.execute(cmd, check_exit_code=False) # When delete and re-add the same vip, we need to # send gratuitous ARP to flush the ARP cache in the Router. gratuitous_arp = self.conf.haproxy.send_gratuitous_arp if gratuitous_arp > 0: for ip in port.fixed_ips: cmd_arping = ['arping', '-U', '-I', interface_name, '-c', gratuitous_arp, ip.ip_address] ip_wrapper.netns.execute(cmd_arping, check_exit_code=False)
def setup(self, network, reuse_existing=False): """Create and initialize a device for network's DHCP on this host.""" port = self.setup_dhcp_port(network) interface_name = self.get_interface_name(network, port) if self.conf.use_namespaces: namespace = NS_PREFIX + network.id else: namespace = None if ip_lib.device_exists(interface_name, self.root_helper, namespace): if not reuse_existing: raise exceptions.PreexistingDeviceFailure( dev_name=interface_name) LOG.debug(_('Reusing existing device: %s.'), interface_name) else: self.driver.plug(network.id, port.id, interface_name, port.mac_address, namespace=namespace) ip_cidrs = [] for fixed_ip in port.fixed_ips: subnet = fixed_ip.subnet net = netaddr.IPNetwork(subnet.cidr) ip_cidr = '%s/%s' % (fixed_ip.ip_address, net.prefixlen) ip_cidrs.append(ip_cidr) if (self.conf.enable_isolated_metadata and self.conf.use_namespaces): ip_cidrs.append(METADATA_DEFAULT_IP) self.driver.init_l3(interface_name, ip_cidrs, namespace=namespace) # ensure that the dhcp interface is first in the list if namespace is None: device = ip_lib.IPDevice(interface_name, self.root_helper) device.route.pullup_route(interface_name) if self.conf.use_namespaces: self._set_default_route(network) return interface_name
def _plug(self, context, namespace, port, reuse_existing=True): self._plug_vip_port(context, port) interface_name = self.vif_driver.get_device_name(port) if ip_lib.device_exists(interface_name, namespace): if not reuse_existing: raise exceptions.PreexistingDeviceFailure( dev_name=interface_name) else: self.vif_driver.plug(port.network_id, port.id, interface_name, port.mac_address, namespace=namespace) self._populate_subnets(context, port) cidrs = [ '%s/%s' % (ip.ip_address, netaddr.IPNetwork(ip.subnet['cidr']).prefixlen) for ip in port.fixed_ips ] self.vif_driver.init_l3(interface_name, cidrs, namespace=namespace) gw_ip = port.fixed_ips[0].subnet.get('gateway_ip') if not gw_ip: host_routes = port.fixed_ips[0].subnet.get('host_routes', []) for host_route in host_routes: if host_route['destination'] == "0.0.0.0/0": gw_ip = host_route['nexthop'] break if gw_ip: cmd = ['route', 'add', 'default', 'gw', gw_ip] ip_wrapper = ip_lib.IPWrapper(namespace=namespace) ip_wrapper.netns.execute(cmd, check_exit_code=False) # When delete and re-add the same vip, we need to # send gratuitous ARP to flush the ARP cache in the Router. gratuitous_arp = self.conf.haproxy.send_gratuitous_arp if gratuitous_arp > 0: for ip in port.fixed_ips: cmd_arping = [ 'arping', '-U', '-I', interface_name, '-c', gratuitous_arp, ip.ip_address ] ip_wrapper.netns.execute(cmd_arping, check_exit_code=False)