def initialize(self, service_dir): super(NetworkResourceService, self).initialize(service_dir) # The <svcroot>/vips directory is used to allocate/de-allocate # container vips. vips_dir = os.path.join(service_dir, self._VIPS_DIR) # Initialize vips self._vips = vipfile.VipMgr(vips_dir, self._service_rsrc_dir) # Clear all environment assignments here. They will be re-assigned # below. for containers_set in set(_SET_BY_ENVIRONMENT.values()): iptables.create_set(containers_set, set_type='hash:ip', family='inet', hashsize=1024, maxelem=65536) need_init = False try: netdev.link_set_up(self._TM_DEV0) netdev.link_set_up(self._TM_DEV1) netdev.link_set_up(self._TMBR_DEV) except subproc.CalledProcessError: need_init = True if need_init: # Reset the bridge self._bridge_initialize() # These two are also done here because they are idempotent # Disable bridge forward delay netdev.bridge_setfd(self._TMBR_DEV, 0) # Enable route_localnet so that we can redirect traffic from the # container to the node's loopback address. netdev.dev_conf_route_localnet_set(self._TM_DEV0, True) # Read bridge status self._bridge_mtu = netdev.dev_mtu(self._TMBR_DEV) # Read current status self._devices = {} for device in netdev.bridge_brif(self._TMBR_DEV): # Ignore local device that is used pass external traffic into the # Treadmill container network. if device == self._TM_DEV1: continue dev_info = _device_info(device) self._devices[dev_info['alias']] = dev_info # Read the currently assigned vIPs for (ip, resource) in self._vips.list(): self._devices.setdefault(resource, {})['ip'] = ip # Mark all the above information as stale for device in self._devices: self._devices[device]['stale'] = True
def _device_info(device): """Gather a given device information. """ return { 'device': device, 'mtu': netdev.dev_mtu(device), 'speed': netdev.dev_speed(device), 'alias': netdev.dev_alias(device), }
def test_dev_mtu(self): """Test device MTU read. """ mock_handle = io.open.return_value mock_handle.read.return_value = '1234\n' res = netdev.dev_mtu('foo') io.open.assert_called_with('/sys/class/net/foo/mtu') self.assertEqual(res, 1234)
def test_dev_mtu(self, mock_open): """Test device MTU read. """ mock_file = mock_open.return_value mock_filectx = mock_file.__enter__.return_value mock_filectx.read.return_value = '1234\n' res = netdev.dev_mtu('foo') mock_open.assert_called_with('/sys/class/net/foo/mtu') self.assertEqual(res, 1234)
def synchronize(self): """Cleanup state resource. """ for app_unique_name in six.viewkeys(self._devices.copy()): if not self._devices[app_unique_name].get('stale', False): continue # This is a stale device, destroy it. self.on_delete_request(app_unique_name) # Reset the container environment sets to the IP we have now cleaned # up. This is more complex than expected because multiple environment # can be merged in the same set in _SET_BY_ENVIRONMENT. container_env_ips = {} for set_name in set(_SET_BY_ENVIRONMENT.values()): key = sorted( [ env for env in _SET_BY_ENVIRONMENT if _SET_BY_ENVIRONMENT[env] == set_name ] ) container_env_ips[tuple(key)] = set() for set_envs, set_ips in six.viewitems(container_env_ips): for device in six.viewvalues(self._devices): if device['environment'] not in set_envs: continue set_ips.add(device['ip']) for set_envs, set_ips in six.viewitems(container_env_ips): iptables.atomic_set( _SET_BY_ENVIRONMENT[set_envs[0]], set_ips, set_type='hash:ip', family='inet', hashsize=1024, maxelem=65536 ) # It is now safe to clean up all remaining vIPs without resource. self._vips.garbage_collect() # Read bridge status self._bridge_mtu = netdev.dev_mtu(self._TMBR_DEV)
def __init__(self, ext_device, ext_ip=None, ext_mtu=None, ext_speed=None): super(NetworkResourceService, self).__init__() self._vips = None self._devices = {} self._bridge_mtu = 0 self.ext_device = ext_device # Read external device info if ext_mtu is None: self.ext_mtu = netdev.dev_mtu(ext_device) else: self.ext_mtu = ext_mtu if ext_speed is None: self.ext_speed = netdev.dev_speed(ext_device) else: self.ext_speed = ext_speed if ext_ip is None: self.ext_ip = _device_ip(ext_device) else: self.ext_ip = ext_ip