def _calculate_update(self, ifaces): """ Calculates the iptables update to rewrite our chains. To avoid traversing lots of dispatch rules to find the right one, we build a tree of chains. Currently, the tree can only be two layers deep: a root chain and a layer of leaves. Interface names look like this: "prefix1234abc". The "prefix" part is always the same so we ignore it. We call "1234abc", the "suffix". The root chain contains two sorts of rules: * where there are multiple interfaces whose suffixes start with the same character, it contains a rule that matches on that prefix of the "suffix"(!) and directs the packet to a leaf chain for that prefix. * as an optimization, if there is only one interface whose suffix starts with a given character, it contains a dispatch rule for that exact interface name. For example, if we have interface names "tapA1" "tapB1" "tapB2", we'll get (in pseudo code): Root chain: if interface=="tapA1" then goto chain for endpoint tapA1 if interface.startswith("tapB") then goto leaf chain for prefix "tapB" tapB leaf chain: if interface=="tapB1" then goto chain for endpoint tapB1 if interface=="tapB2" then goto chain for endpoint tapB2 :param set[str] ifaces: The list of interfaces to generate a dispatch chain for. :returns Tuple: to_delete, deps, updates, new_leaf_chains: * set of leaf chains that are no longer needed for deletion * chain dependency dict. * chain updates dict. * complete set of leaf chains that are now required. """ # iptables update fragments/dependencies for the root chains. updates = defaultdict(list) root_to_upds = updates[self.chain_to_root] root_from_upds = updates[self.chain_from_root] dependencies = defaultdict(set) root_to_deps = dependencies[self.chain_to_root] root_from_deps = dependencies[self.chain_from_root] # Separate the interface names by their prefixes so we can count them # and decide whether to program a leaf chain or not. interfaces_by_prefix = defaultdict(set) iface_prefix = find_longest_prefix(ifaces) for iface in ifaces: ep_suffix = iface[len(iface_prefix):] prefix = ep_suffix[:1] interfaces_by_prefix[prefix].add(iface) # Spin through the interfaces by prefix. Either add them directly # to the root chain or create a leaf and add them there. new_leaf_chains = set() for prefix, interfaces in interfaces_by_prefix.iteritems(): use_root_chain = len(interfaces) == 1 if use_root_chain: # Optimization: there's only one interface with this prefix, # don't program a leaf chain. disp_to_chain = self.chain_to_root disp_from_chain = self.chain_from_root to_deps = root_to_deps from_deps = root_from_deps to_upds = root_to_upds from_upds = root_from_upds else: # There's more than one interface with this prefix, program # a leaf chain. disp_to_chain = self.chain_to_leaf + "-" + prefix disp_from_chain = self.chain_from_leaf + "-" + prefix to_upds = updates[disp_to_chain] from_upds = updates[disp_from_chain] to_deps = dependencies[disp_to_chain] from_deps = dependencies[disp_from_chain] new_leaf_chains.add(disp_from_chain) new_leaf_chains.add(disp_to_chain) # Root chain depends on its leaves. root_from_deps.add(disp_to_chain) root_to_deps.add(disp_from_chain) # Point root chain at prefix chain. iface_match = iface_prefix + prefix + "+" root_from_upds.append( "--append %s --in-interface %s --goto %s" % (self.chain_from_root, iface_match, disp_from_chain) ) root_to_upds.append( "--append %s --out-interface %s --goto %s" % (self.chain_to_root, iface_match, disp_to_chain) ) # Common processing, add the per-endpoint rules to whichever # chain we decided above. for iface in interfaces: # Add rule to leaf or global chain to direct traffic to the # endpoint-specific one. Note that we use --goto, which means # that the endpoint-specific chain will return to our parent # rather than to this chain. ep_suffix = interface_to_chain_suffix(self.config, iface) to_chain_name = CHAIN_TO_PREFIX + ep_suffix from_chain_name = CHAIN_FROM_PREFIX + ep_suffix from_upds.append("--append %s --in-interface %s --goto %s" % (disp_from_chain, iface, from_chain_name)) from_deps.add(from_chain_name) to_upds.append("--append %s --out-interface %s --goto %s" % (disp_to_chain, iface, to_chain_name)) to_deps.add(to_chain_name) if not use_root_chain: # Add a default drop to the end of the leaf chain. # Add a default drop to the end of the leaf chain. from_upds.extend( self.end_of_chain_rules(disp_from_chain, "From")) to_upds.extend( self.end_of_chain_rules(disp_to_chain, "To")) # Both TO and FROM chains end with a DROP so that interfaces that # we don't know about yet can't bypass our rules. root_from_upds.extend( self.end_of_chain_rules(self.chain_from_root, "From")) root_to_upds.extend( self.end_of_chain_rules(self.chain_to_root, "To")) chains_to_delete = self.programmed_leaf_chains - new_leaf_chains _log.debug("New chains: %s; to delete: %s", new_leaf_chains, chains_to_delete) return chains_to_delete, dependencies, updates, new_leaf_chains
def _apply_endpoint_update(self): pending_endpoint = self._pending_endpoint if pending_endpoint == self.endpoint: _log.debug("Endpoint hasn't changed, nothing to do") return # Calculate the set of IPs that we had before this update. Needed on # the update and delete code paths below. if self.endpoint: old_ips = set(futils.net_to_ip(n) for n in self.endpoint.get(self.nets_key, [])) old_nat_mappings = self.endpoint.get(self.nat_key, []) else: old_ips = set() old_nat_mappings = [] all_old_ips = old_ips | set([n["ext_ip"] for n in old_nat_mappings]) if pending_endpoint: # Update/create. if pending_endpoint['mac'] != self._mac: # Either we have not seen this MAC before, or it has changed. _log.debug("Endpoint MAC changed to %s", pending_endpoint["mac"]) self._mac = pending_endpoint['mac'] self._mac_changed = True # MAC change requires refresh of iptables rules and ARP table. self._iptables_in_sync = False self._device_in_sync = False if self.endpoint is None: # This is the first time we have seen the endpoint, so extract # the interface name and endpoint ID. self._iface_name = pending_endpoint["name"] self._suffix = interface_to_chain_suffix(self.config, self._iface_name) _log.debug("Learned interface name/suffix: %s/%s", self._iface_name, self._suffix) # First time through, need to program everything. self._iptables_in_sync = False self._device_in_sync = False if self._device_is_up is None: _log.debug("Learned interface name, checking if device " "is up.") self._device_is_up = ( devices.interface_exists(self._iface_name) and devices.interface_up(self._iface_name) ) # Check if the profile ID or IP addresses have changed, requiring # a refresh of the dataplane. profile_ids = set(pending_endpoint.get("profile_ids", [])) if profile_ids != self._explicit_profile_ids: # Profile ID update requires iptables update but not device # update. _log.debug("Profile IDs changed from %s to %s, need to update " "iptables", self._rules_ref_helper.required_refs, profile_ids) self._explicit_profile_ids = profile_ids self._iptables_in_sync = False self._profile_ids_dirty = True # Check for changes to values that require a device update. if self.endpoint: if self.endpoint.get("state") != pending_endpoint.get("state"): _log.debug("Desired interface state updated.") self._device_in_sync = False self._iptables_in_sync = False new_ips = set(futils.net_to_ip(n) for n in pending_endpoint.get(self.nets_key, [])) if old_ips != new_ips: # IP addresses have changed, need to update the routing # table. _log.debug("IP addresses changed, need to update routing") self._device_in_sync = False new_nat_mappings = pending_endpoint.get(self.nat_key, []) if old_nat_mappings != new_nat_mappings: _log.debug("NAT mappings have changed, refreshing.") self._device_in_sync = False self._iptables_in_sync = False all_new_ips = new_ips | set([n["ext_ip"] for n in new_nat_mappings]) if all_old_ips != all_new_ips: # Ensure we clean up any conntrack entries for IPs that # have been removed. _log.debug("Set of all IPs changed from %s to %s", all_old_ips, all_new_ips) self._removed_ips |= all_old_ips self._removed_ips -= all_new_ips else: # Delete of the endpoint. Need to resync everything. self._profile_ids_dirty = True self._iptables_in_sync = False self._device_in_sync = False self._removed_ips |= all_old_ips self.endpoint = pending_endpoint self._endpoint_update_pending = False self._pending_endpoint = None
def _apply_endpoint_update(self): pending_endpoint = self._pending_endpoint if pending_endpoint == self.endpoint: _log.debug("Endpoint hasn't changed, nothing to do") return # Calculate the set of IPs that we had before this update. Needed on # the update and delete code paths below. if self.endpoint: old_ips = set( futils.net_to_ip(n) for n in self.endpoint.get(self.nets_key, [])) old_nat_mappings = self.endpoint.get(self.nat_key, []) else: old_ips = set() old_nat_mappings = [] all_old_ips = old_ips | set([n["ext_ip"] for n in old_nat_mappings]) if pending_endpoint: # Update/create. if pending_endpoint.get('mac') != self._mac: # Either we have not seen this MAC before, or it has changed. _log.debug("Endpoint MAC changed to %s", pending_endpoint.get("mac")) self._mac = pending_endpoint.get('mac') self._mac_changed = True # MAC change requires refresh of iptables rules and ARP table. self._iptables_in_sync = False self._device_in_sync = False new_iface_name = pending_endpoint["name"] # Interface renames are handled in the EndpointManager by # simulating a delete then an add. We shouldn't see one here. assert (self.endpoint is None or self._iface_name == new_iface_name), ( "Unexpected change of interface name.") if self.endpoint is None: # This is the first time we have seen the endpoint, so extract # the interface name and endpoint ID. self._iface_name = new_iface_name self._suffix = interface_to_chain_suffix( self.config, self._iface_name) _log.debug("Learned interface name/suffix: %s/%s", self._iface_name, self._suffix) # First time through, need to program everything. self._iptables_in_sync = False self._device_in_sync = False if self._device_is_up is None: _log.debug("Learned interface name, checking if device " "is up.") self._device_is_up = ( devices.interface_exists(self._iface_name) and devices.interface_up(self._iface_name)) # Check if the profile ID or IP addresses have changed, requiring # a refresh of the dataplane. profile_ids = set(pending_endpoint.get("profile_ids", [])) if profile_ids != self._explicit_profile_ids: # Profile ID update requires iptables update but not device # update. _log.debug( "Profile IDs changed from %s to %s, need to update " "iptables", self._rules_ref_helper.required_refs, profile_ids) self._explicit_profile_ids = profile_ids self._iptables_in_sync = False self._profile_ids_dirty = True # Check for changes to values that require a device update. if self.endpoint: if self.endpoint.get("state") != pending_endpoint.get("state"): _log.debug("Desired interface state updated.") self._device_in_sync = False self._iptables_in_sync = False new_ips = set( futils.net_to_ip(n) for n in pending_endpoint.get(self.nets_key, [])) if old_ips != new_ips: # IP addresses have changed, need to update the routing # table. _log.debug("IP addresses changed, need to update routing") self._device_in_sync = False new_nat_mappings = pending_endpoint.get(self.nat_key, []) if old_nat_mappings != new_nat_mappings: _log.debug("NAT mappings have changed, refreshing.") self._device_in_sync = False self._iptables_in_sync = False all_new_ips = new_ips | set( [n["ext_ip"] for n in new_nat_mappings]) if all_old_ips != all_new_ips: # Ensure we clean up any conntrack entries for IPs that # have been removed. _log.debug("Set of all IPs changed from %s to %s", all_old_ips, all_new_ips) self._removed_ips |= all_old_ips self._removed_ips -= all_new_ips else: # Delete of the endpoint. Need to resync everything. self._profile_ids_dirty = True self._iptables_in_sync = False self._device_in_sync = False self._removed_ips |= all_old_ips self.endpoint = pending_endpoint self._endpoint_update_pending = False self._pending_endpoint = None