def setup(self, network): """Create and initialize a device for network's DHCP on this host.""" port = self.setup_dhcp_port(network) interface_name = network['interfacename'] LOG.debug("DHCP_PORT :%s", port) LOG.debug("DPCP_PORT_NAME: %s", interface_name) if ip_lib.ensure_device_is_ready(interface_name, namespace=network.namespace): LOG.debug('Reusing existing device: %s.', interface_name) else: LOG.debug("Reusing not existing device:%s", interface_name) self.driver.plug(network.id, port.id, interface_name, None, namespace=network.namespace) self.fill_dhcp_udp_checksums(namespace=network.namespace) tag = network.get("vlantag", None) if tag: if self.conf.ovs_use_veth: self.set_tag(interface_name.replace("ns-", "tap"), tag) else: self.set_tag(interface_name, tag) else: LOG.debug("No vlantag exists for network %s", network.id) ip_cidrs = [] for fixed_ip in port.fixed_ips: LOG.debug("fixed_ip.subnet:%s", fixed_ip.subnet) subnet = fixed_ip.subnet if not ipv6_utils.is_auto_address_subnet(subnet): net = netaddr.IPNetwork(subnet.cidr) ip_cidr = '%s/%s' % (fixed_ip.ip_address, net.prefixlen) ip_cidrs.append(ip_cidr) self.driver.init_l3(interface_name, ip_cidrs, namespace=network.namespace) # ensure that the dhcp interface is first in the list if network.namespace is None: device = ip_lib.IPDevice(interface_name) device.route.pullup_route(interface_name) if self.conf.use_namespaces: self._set_default_route(network, interface_name) return interface_name
def _make_subnet_interface_ip_map(self): ip_dev = ip_lib.IPDevice(self.interface_name, namespace=self.network.namespace) subnet_lookup = dict((netaddr.IPNetwork(subnet.cidr), subnet.id) for subnet in self.network.subnets) retval = {} for addr in ip_dev.addr.list(): ip_net = netaddr.IPNetwork(addr['cidr']) if ip_net in subnet_lookup: retval[subnet_lookup[ip_net]] = addr['cidr'].split('/')[0] return retval
def setup(self, network): """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) LOG.debug("port :%s", port) if ip_lib.ensure_device_is_ready(interface_name, namespace=network.namespace): LOG.debug('Reusing existing device: %s.', interface_name) else: LOG.debug("Reusing not existing device:%s", interface_name) self.driver.plug(network.id, port.id, interface_name, port.mac_address, namespace=network.namespace) self.fill_dhcp_udp_checksums(namespace=network.namespace) ip_cidrs = [] for fixed_ip in port.fixed_ips: LOG.debug("fixed_ip.subnet:%s", fixed_ip.subnet) subnet = fixed_ip.subnet if not ipv6_utils.is_auto_address_subnet(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_CIDR) self.driver.init_l3(interface_name, ip_cidrs, namespace=network.namespace) # ensure that the dhcp interface is first in the list if network.namespace is None: device = ip_lib.IPDevice(interface_name) device.route.pullup_route(interface_name) if self.conf.use_namespaces: self._set_default_route(network, interface_name) return interface_name
def _set_default_route(self, network, device_name): """Sets the default gateway for this dhcp namespace. This method is idempotent and will only adjust the route if adjusting it would change it from what it already is. This makes it safe to call and avoids unnecessary perturbation of the system. """ device = ip_lib.IPDevice(device_name, namespace=network.namespace) gateway = device.route.get_gateway() LOG.debug("gateway:%s", gateway) if gateway: gateway = gateway['gateway'] #LOG.debug("network:%s", network) for subnet in network.subnets: LOG.debug("subnet :%s", subnet) skip_subnet = ( subnet.ip_version != 4 or not subnet.enable_dhcp or subnet.gateway_ip is None) if skip_subnet: continue if gateway != subnet.gateway_ip: LOG.debug('Setting gateway for dhcp netns on net %(n)s to ' '%(ip)s', {'n': network.id, 'ip': subnet.gateway_ip}) device.route.add_gateway(subnet.gateway_ip) return # No subnets on the network have a valid gateway. Clean it up to avoid # confusion from seeing an invalid gateway here. if gateway is not None: LOG.debug('Removing gateway for dhcp netns on net %s', network.id) device.route.delete_gateway(gateway)