def test_switch_change_bonded_network_with_dhclient(self): with veth_pair() as (server, nic1): with dummy_device() as nic2: NETSETUP_SOURCE = { NET1_NAME: { 'bonding': BOND_NAME, 'bootproto': 'dhcp', 'blockingdhcp': True, 'switch': self.switch_type_source } } NETSETUP_TARGET = _change_switch_type(NETSETUP_SOURCE, self.switch_type_target) BONDSETUP_SOURCE = { BOND_NAME: { 'nics': [nic1, nic2], 'switch': self.switch_type_source } } BONDSETUP_TARGET = _change_switch_type(BONDSETUP_SOURCE, self.switch_type_target) addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO): with self.setupNetworks(NETSETUP_SOURCE, BONDSETUP_SOURCE, NOCHK): self.setupNetworks(NETSETUP_TARGET, BONDSETUP_TARGET, NOCHK) self.assertNetwork(NET1_NAME, NETSETUP_TARGET[NET1_NAME]) self.assertBond(BOND_NAME, BONDSETUP_TARGET[BOND_NAME])
def test_switch_change_bonded_network_with_dhclient(self): with veth_pair() as (server, nic1): with dummy_device() as nic2: NETSETUP_SOURCE = {NET1_NAME: { 'bonding': BOND_NAME, 'bootproto': 'dhcp', 'blockingdhcp': True, 'switch': self.switch_type_source}} NETSETUP_TARGET = _change_switch_type( NETSETUP_SOURCE, self.switch_type_target) BONDSETUP_SOURCE = {BOND_NAME: { 'nics': [nic1, nic2], 'switch': self.switch_type_source}} BONDSETUP_TARGET = _change_switch_type( BONDSETUP_SOURCE, self.switch_type_target) addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO): with self.setupNetworks( NETSETUP_SOURCE, BONDSETUP_SOURCE, NOCHK): self.setupNetworks( NETSETUP_TARGET, BONDSETUP_TARGET, NOCHK) self.assertNetwork( NET1_NAME, NETSETUP_TARGET[NET1_NAME]) self.assertBond(BOND_NAME, BONDSETUP_TARGET[BOND_NAME])
def test_local_auto_with_dynamic_address_from_ra(self): IPV6_NETADDRESS = '2001:1:1:1' IPV6_NETPREFIX_LEN = '64' with veth_pair() as (server, client): with dnsmasq_run(server, ipv6_slaac_prefix=IPV6_NETADDRESS + '::'): with wait_for_ipv6(client): ipwrapper.linkSet(client, ['up']) ipwrapper.linkSet(server, ['up']) ipwrapper.addrAdd(server, IPV6_NETADDRESS + '::1', IPV6_NETPREFIX_LEN, family=6) # Expecting link and global addresses on client iface # The addresses are given randomly, so we sort them ip_addrs = sorted(addresses.getIpAddrs()[client], key=lambda ip: ip['address']) self.assertEqual(2, len(ip_addrs)) self.assertTrue(addresses.is_dynamic(ip_addrs[0])) self.assertEqual('global', ip_addrs[0]['scope']) self.assertEqual(IPV6_NETADDRESS, ip_addrs[0]['address'][:len(IPV6_NETADDRESS)]) self.assertEqual('link', ip_addrs[1]['scope'])
def test_attach_dhcp_nic_to_ipless_network(self, switch): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) addrAdd(server, IPv6_ADDRESS, IPv6_CIDR, IpFamily.IPv6) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO, router=DHCPv4_GATEWAY): with dhclient_run(client): adapter.assertDhclient(client, family=IpFamily.IPv4) adapter.assertDhclient(client, family=IpFamily.IPv6) NETCREATE = { NETWORK_NAME: { 'nic': client, 'switch': switch } } with adapter.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = adapter.netinfo.nics[client] adapter.assertDisabledIPv4(nic_netinfo) adapter.assertDisabledIPv6(nic_netinfo) net_netinfo = adapter.netinfo.networks[NETWORK_NAME] adapter.assertDisabledIPv4(net_netinfo) adapter.assertDisabledIPv6(nic_netinfo)
def test_add_net_with_dhcp(self, switch, families, bridged): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) addrAdd(server, IPv6_ADDRESS, IPv6_CIDR, IpFamily.IPv6) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO, router=DHCPv4_GATEWAY): network_attrs = {'bridged': bridged, 'nic': client, 'blockingdhcp': True, 'switch': switch} if IpFamily.IPv4 in families: network_attrs['bootproto'] = 'dhcp' if IpFamily.IPv6 in families: network_attrs['dhcpv6'] = True netcreate = {NETWORK_NAME: network_attrs} with adapter.setupNetworks(netcreate, {}, NOCHK): adapter.assertNetworkIp( NETWORK_NAME, netcreate[NETWORK_NAME])
def test_sourceroute_add_over_existing_route(self): with dummy_device() as nic: addrAdd(nic, IPV4_ADDRESS, IPV4_MASK) with create_sourceroute(device=nic, ip=IPV4_ADDRESS, mask=IPV4_MASK, gateway=IPV4_GW): sourceroute.add(nic, IPV4_ADDRESS, IPV4_MASK, IPV4_GW)
def test_sourceroute_add_remove_and_read(self, nic0): addrAdd(nic0, IPV4_ADDRESS, IPV4_MASK) with create_sourceroute(device=nic0, ip=IPV4_ADDRESS, mask=IPV4_MASK, gateway=IPV4_GW): dsroute = DynamicSourceRoute(nic0, None, None, None) routes, rules = dsroute.current_srconfig() assert len(routes) == 2 assert len(rules) == 2 assert routes[0].to == '0.0.0.0/0' assert routes[0].device == nic0 assert routes[1].to == IPV4_NET assert routes[1].device == nic0 assert rules[0].to == IPV4_NET assert rules[0].table == IPV4_TABLE assert rules[0].iif == nic0 assert rules[0].prio == sourceroute.RULE_PRIORITY assert rules[1].src == IPV4_NET assert rules[1].table == IPV4_TABLE assert rules[1].prio == sourceroute.RULE_PRIORITY
def test_ovirtmgmtm_to_ovs(self): """ Test transformation of initial management network to OVS. # TODO: test it with ovirtmgmt and test-network # NOTE: without default route # TODO: more asserts """ with veth_pair() as (left, right): addrAdd(left, IP_ADDRESS, IP_CIDR) addrAdd(left, IPv6_ADDRESS, IPv6_CIDR, 6) linkSet(left, ['up']) with dnsmasq_run(left, DHCP_RANGE_FROM, DHCP_RANGE_TO, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO, IP_GATEWAY): network = { NETWORK_NAME: { 'nic': right, 'bootproto': 'dhcp', 'bridged': True, 'blockingdhcp': True } } options = NOCHK options['ovs'] = False try: status, msg = self.setupNetworks(network, {}, options) self.assertEqual(status, SUCCESS, msg) self.assertNetworkExists(NETWORK_NAME) options['ovs'] = True status, msg = self.setupNetworks(network, {}, options) self.assertEqual(status, SUCCESS, msg) self.assertNetworkExists(NETWORK_NAME) finally: dhcp.delete_dhclient_leases(NETWORK_NAME, True, False)
def _set_network_ip_config(ip_config): """Set IP configuration on Vdsm controlled OVS network""" iface = ip_config.top_dev ipv4 = ip_config.ipv4 ipv6 = ip_config.ipv6 port = ip_config.port blocking_dhcp = ip_config.blocking_dhcp net_dev = NetDevice(iface, iproute2, ipv4=ipv4, ipv6=ipv6, blockingdhcp=blocking_dhcp) DynamicSourceRoute.addInterfaceTracking(net_dev) ipwrapper.linkSet(iface, ['down']) if ipv4.address: ipwrapper.addrAdd(iface, ipv4.address, ipv4.netmask) if ipv4.gateway and ipv4.defaultRoute: ipwrapper.routeAdd(['default', 'via', ipv4.gateway]) if ipv6.address or ipv6.ipv6autoconf or ipv6.dhcpv6: sysctl.disable_ipv6(iface, disable=False) else: sysctl.disable_ipv6(iface) if ipv6.address: ipv6addr, ipv6netmask = ipv6.address.split('/') ipwrapper.addrAdd(iface, ipv6addr, ipv6netmask, family=6) if ipv6.gateway: ipwrapper.routeAdd(['default', 'via', ipv6.gateway], dev=iface, family=6) ipwrapper.linkSet(port, ['up']) ipwrapper.linkSet(iface, ['up']) if ipv4.bootproto == 'dhcp': _run_dhclient(iface, blocking_dhcp, ipv4.defaultRoute, 4) if ipv6.dhcpv6: _run_dhclient(iface, blocking_dhcp, ipv6.defaultRoute, 6) iproute2._addSourceRoute(net_dev)
def test_ovirtmgmtm_to_ovs(self): """ Test transformation of initial management network to OVS. # TODO: test it with ovirtmgmt and test-network # NOTE: without default route # TODO: more asserts """ with veth_pair() as (left, right): addrAdd(left, IP_ADDRESS, IP_CIDR) addrAdd(left, IPv6_ADDRESS, IPv6_CIDR, 6) linkSet(left, ['up']) with dnsmasq_run(left, DHCP_RANGE_FROM, DHCP_RANGE_TO, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO, IP_GATEWAY): network = { NETWORK_NAME: {'nic': right, 'bootproto': 'dhcp', 'bridged': True, 'blockingdhcp': True}} options = NOCHK options['ovs'] = False try: status, msg = self.setupNetworks(network, {}, options) self.assertEqual(status, SUCCESS, msg) self.assertNetworkExists(NETWORK_NAME) options['ovs'] = True status, msg = self.setupNetworks(network, {}, options) self.assertEqual(status, SUCCESS, msg) self.assertNetworkExists(NETWORK_NAME) finally: dhcp.delete_dhclient_leases(NETWORK_NAME, True, False)
def test_attach_dhcp_nic_to_dhcpv6_bridged_network(self, switch): if switch == 'ovs': pytest.xfail('IPv6 dynamic fails with OvS' 'see https://bugzilla.redhat.com/1773471') with veth_pair() as (server, client): addrAdd(server, IPv6_ADDRESS, IPv6_CIDR, IpFamily.IPv6) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO): with dhcp_client_run(client, family=IpFamily.IPv6): adapter.assertDhclient(client, family=IpFamily.IPv6) NETCREATE = { NETWORK_NAME: { 'nic': client, 'dhcpv6': True, 'blockingdhcp': True, 'switch': switch, } } with adapter.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = adapter.netinfo.nics[client] adapter.assertDisabledIPv6(nic_netinfo) adapter.assertNoDhclient(client, family=IpFamily.IPv6) net_netinfo = adapter.netinfo.networks[NETWORK_NAME] adapter.assertDHCPv6(net_netinfo) adapter.assertDhclient(NETWORK_NAME, family=IpFamily.IPv6)
def test_local_auto_with_static_address_without_ra_server(self, nic0): ipwrapper.addrAdd(nic0, '2001::88', IPV6_PREFIX_LENGTH, family=6) ip_addrs = addresses.getIpAddrs()[nic0] assert addresses.is_ipv6_local_auto(nic0) assert 2 == len(ip_addrs), ip_addrs assert addresses.is_ipv6(ip_addrs[0]) assert not addresses.is_dynamic(ip_addrs[0])
def test_attach_dhcp_nic_to_dhcpv4_bridged_network(self, switch): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO, router=DHCPv4_GATEWAY): with dhclient_run(client): adapter.assertDhclient(client, family=IpFamily.IPv4) NETCREATE = { NETWORK_NAME: { 'nic': client, 'bootproto': 'dhcp', 'blockingdhcp': True, 'switch': switch } } with adapter.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = adapter.netinfo.nics[client] adapter.assertDisabledIPv4(nic_netinfo) adapter.assertNoDhclient(client, family=IpFamily.IPv4) net_netinfo = adapter.netinfo.networks[NETWORK_NAME] adapter.assertDHCPv4(net_netinfo) adapter.assertDhclient(NETWORK_NAME, family=IpFamily.IPv4)
def test_add_net_with_dhcp(self, switch, families, bridged): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) addrAdd(server, IPv6_ADDRESS, IPv6_CIDR, IpFamily.IPv6) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO, router=DHCPv4_GATEWAY): network_attrs = { 'bridged': bridged, 'nic': client, 'blockingdhcp': True, 'switch': switch } if IpFamily.IPv4 in families: network_attrs['bootproto'] = 'dhcp' if IpFamily.IPv6 in families: network_attrs['dhcpv6'] = True netcreate = {NETWORK_NAME: network_attrs} with adapter.setupNetworks(netcreate, {}, NOCHK): adapter.assertNetworkIp(NETWORK_NAME, netcreate[NETWORK_NAME])
def test_sourceroute_add_remove_and_read(self): with dummy_device() as nic: addrAdd(nic, IPV4_ADDRESS, IPV4_MASK) with create_sourceroute(device=nic, ip=IPV4_ADDRESS, mask=IPV4_MASK, gateway=IPV4_GW): dsroute = DynamicSourceRoute(nic, None, None, None) routes, rules = dsroute.current_srconfig() self.assertEqual(2, len(routes), routes) self.assertEqual(2, len(rules), rules) self.assertEqual('0.0.0.0/0', routes[0].to) self.assertEqual(nic, routes[0].device) self.assertEqual(IPv4_NET, routes[1].to) self.assertEqual(nic, routes[1].device) self.assertEqual(IPv4_NET, rules[0].to) self.assertEqual(IPV4_TABLE, rules[0].table) self.assertEqual(nic, rules[0].iif) self.assertEqual(rules[0].prio, sourceroute.RULE_PRIORITY) self.assertEqual(IPv4_NET, rules[1].src) self.assertEqual(IPV4_TABLE, rules[1].table) self.assertEqual(rules[1].prio, sourceroute.RULE_PRIORITY)
def add(addr_data): with _translate_iproute2_exception(IPAddressAddError, addr_data): ipwrapper.addrAdd( addr_data.device, addr_data.address, addr_data.prefixlen, addr_data.family, )
def test_local_auto_with_static_address_without_ra_server(self): with dummy_device() as dev: ipwrapper.addrAdd(dev, '2001::88', '64', family=6) ip_addrs = addresses.getIpAddrs()[dev] assert addresses.is_ipv6_local_auto(dev) assert 2 == len(ip_addrs), ip_addrs assert addresses.is_ipv6(ip_addrs[0]) assert not addresses.is_dynamic(ip_addrs[0])
def test_attach_nic_with_ip_to_ipless_network(self): with dummy_device() as nic: addrAdd(nic, IPv4_ADDRESS, IPv4_PREFIX_LEN) NETCREATE = {NETWORK_NAME: {"nic": nic, "switch": self.switch}} with self.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = self.netinfo.nics[nic] self.assertDisabledIPv4(nic_netinfo)
def test_attach_nic_with_ip_to_ipless_network(self): with dummy_device() as nic: addrAdd(nic, IPv4_ADDRESS, IPv4_PREFIX_LEN) NETCREATE = {NETWORK_NAME: {'nic': nic, 'switch': self.switch}} with self.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = self.netinfo.nics[nic] self.assertDisabledIPv4(nic_netinfo)
def test_sourceroute_add_over_existing_route(self, nic0): addrAdd(nic0, IPV4_ADDRESS, IPV4_MASK) with create_sourceroute(device=nic0, ip=IPV4_ADDRESS, mask=IPV4_MASK, gateway=IPV4_GW): sourceroute.add(nic0, IPV4_ADDRESS, IPV4_MASK, IPV4_GW)
def test_local_auto_with_static_address_without_ra_server(self): with dummy_device() as dev: ipwrapper.addrAdd(dev, '2001::88', '64', family=6) ip_addrs = addresses.getIpAddrs()[dev] self.assertTrue(addresses.is_ipv6_local_auto(dev)) self.assertEqual(2, len(ip_addrs)) self.assertTrue(addresses.is_ipv6(ip_addrs[0])) self.assertTrue(not addresses.is_dynamic(ip_addrs[0]))
def test_local_auto_with_static_address_without_ra_server(self): with dummy_device() as dev: ipwrapper.addrAdd(dev, '2001::88', '64', family=6) ip_addrs = addresses.getIpAddrs()[dev] self.assertTrue(addresses.is_ipv6_local_auto(dev)) self.assertEqual(2, len(ip_addrs), ip_addrs) self.assertTrue(addresses.is_ipv6(ip_addrs[0])) self.assertTrue(not addresses.is_dynamic(ip_addrs[0]))
def test_attach_nic_with_ip_to_ipless_network(self, switch): with dummy_device() as nic: addrAdd(nic, IPv4_ADDRESS, IPv4_PREFIX_LEN) NETCREATE = {NETWORK_NAME: {'nic': nic, 'switch': switch}} with adapter.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = adapter.netinfo.nics[nic] adapter.assertDisabledIPv4(nic_netinfo)
def add_ip(self, ip_addr, prefix_len, family): try: addrAdd(self.dev_name, ip_addr, prefix_len, family) except IPRoute2Error as e: message = ( f'Failed to add the IPv{family} address {ip_addr}/{prefix_len}' f'to device {self.dev_name}: {e}') pytest.skip(message)
def test_iperf_upper_limit(self): # Upper limit is not an accurate measure. This is because it converges # over time and depends on current machine hardware (CPU). # Hence, it is hard to make hard assertions on it. The test should run # at least 60 seconds (the longer the better) and the user should # inspect the computed average rate and optionally the additional # traffic data that was collected in client.out in order to be # convinced QOS is working properly. limit_kbps = 1000 # 1 Mbps (in kbps) server_ip = '192.0.2.1' client_ip = '192.0.2.10' qos_out = {'ul': {'m2': limit_kbps}, 'ls': {'m2': limit_kbps}} # using a network namespace is essential since otherwise the kernel # short-circuits the traffic and bypasses the veth devices and the # classfull qdisc. with network_namespace( 'server_ns' ) as ns, bridge_device() as bridge, veth_pair() as ( server_peer, server_dev, ), veth_pair() as ( client_dev, client_peer, ): linkSet(server_peer, ['up']) linkSet(client_peer, ['up']) # iperf server and its veth peer lie in a separate network # namespace link_set_netns(server_dev, ns) bridge.addIf(server_peer) bridge.addIf(client_peer) linkSet(client_dev, ['up']) netns_exec(ns, ['ip', 'link', 'set', 'dev', server_dev, 'up']) addrAdd(client_dev, client_ip, 24) netns_exec( ns, [ 'ip', '-4', 'addr', 'add', 'dev', server_dev, '%s/24' % server_ip, ], ) qos.configure_outbound(qos_out, client_peer, None) with running(IperfServer(server_ip, network_ns=ns)): client = IperfClient(server_ip, client_ip, test_time=60) client.start() max_rate = max( [ float(interval['streams'][0]['bits_per_second']) // (2 ** 10) for interval in client.out['intervals'] ] ) self.assertTrue(0 < max_rate < limit_kbps * 1.5)
def test_attach_nic_with_ip_to_ipless_network(self, adapter, switch, nic0): addrAdd(nic0, IPv4_ADDRESS, IPv4_PREFIX_LEN) addrAdd(nic0, IPv6_ADDRESS, IPv6_PREFIX_LEN, family=6) NETCREATE = {NETWORK_NAME: {'nic': nic0, 'switch': switch}} with adapter.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = adapter.netinfo.nics[nic0] adapter.assertDisabledIPv4(nic_netinfo) adapter.assertDisabledIPv6(nic_netinfo)
def add(iface, ipv4, ipv6): if ipv4.address: ipwrapper.addrAdd(iface, ipv4.address, ipv4.netmask) if ipv4.gateway and ipv4.defaultRoute: ipwrapper.routeAdd(['default', 'via', ipv4.gateway]) if ipv6: _add_ipv6_address(iface, ipv6) elif ipv6_supported(): sysctl.disable_ipv6(iface)
def test_attach_nic_with_ip_as_a_slave_to_ipless_network(self): with dummy_devices(2) as (nic1, nic2): addrAdd(nic1, IPv4_ADDRESS, IPv4_PREFIX_LEN) NETCREATE = {NETWORK_NAME: {"bonding": BOND_NAME, "switch": self.switch}} BONDCREATE = {BOND_NAME: {"nics": [nic1, nic2], "switch": self.switch}} with self.setupNetworks(NETCREATE, BONDCREATE, NOCHK): nic_netinfo = self.netinfo.nics[nic1] self.assertDisabledIPv4(nic_netinfo)
def dynamic_ipv4_iface(): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO, router=IPv4_ADDRESS): yield client
def _add_ipv6_address(iface, ipv6): if ipv6.address: ipv6addr, ipv6netmask = ipv6.address.split('/') ipwrapper.addrAdd(iface, ipv6addr, ipv6netmask, family=6) if ipv6.gateway and ipv6.defaultRoute: _set_default_route(ipv6.gateway, family=6, dev=iface) if ipv6.ipv6autoconf is not None: with open('/proc/sys/net/ipv6/conf/%s/autoconf' % iface, 'w') as ipv6_autoconf: ipv6_autoconf.write('1' if ipv6.ipv6autoconf else '0')
def test_routes_device_to(self, ip_addr, ip_netmask, nic0): addr_in_net = ipaddress.ip_address(ip_addr) + 1 ip_version = addr_in_net.version ipwrapper.addrAdd(nic0, ip_addr, ip_netmask, family=ip_version) try: ipwrapper.linkSet(nic0, ['up']) assert routes.getRouteDeviceTo(str(addr_in_net)) == nic0 finally: ipwrapper.addrFlush(nic0, ip_version)
def testDhcpReplaceNicWithBridge(self): with veth_pair() as (left, right): addrAdd(left, IP_ADDRESS, IP_CIDR) addrAdd(left, IPv6_ADDRESS, IPv6_CIDR, 6) linkSet(left, ['up']) with dnsmasq_run(left, DHCP_RANGE_FROM, DHCP_RANGE_TO, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO, IP_GATEWAY): # first, a network without a bridge should get a certain # address network = { NETWORK_NAME: { 'nic': right, 'bridged': False, 'bootproto': 'dhcp', 'blockingdhcp': True } } try: status, msg = self.setupNetworks(network, {}, NOCHK) self.assertEqual(status, SUCCESS, msg) self.assertNetworkExists(NETWORK_NAME) test_net = self.vdsm_net.netinfo.networks[NETWORK_NAME] self.assertEqual(test_net['dhcpv4'], True) devs = self.vdsm_net.netinfo.nics device_name = right self.assertIn(device_name, devs) net_attrs = devs[device_name] self.assertEqual(net_attrs['dhcpv4'], True) self.assertEqual(test_net['gateway'], IP_GATEWAY) ip_addr = test_net['addr'] self.assertSourceRoutingConfiguration(device_name, ip_addr) # now, a bridged network should get the same address # (because dhclient should send the same dhcp-client- # identifier) network[NETWORK_NAME]['bridged'] = True status, msg = self.setupNetworks(network, {}, NOCHK) self.assertEqual(status, SUCCESS, msg) test_net = self.vdsm_net.netinfo.networks[NETWORK_NAME] self.assertEqual(ip_addr, test_net['addr']) network = {NETWORK_NAME: {'remove': True}} status, msg = self.setupNetworks(network, {}, NOCHK) self.assertEqual(status, SUCCESS, msg) self.assertNetworkDoesntExist(NETWORK_NAME) finally: dhcp.delete_dhclient_leases(right, True, False) dhcp.delete_dhclient_leases(NETWORK_NAME, True, False)
def test_ip_info(self): def get_ip_info(*a, **kw): """filter away ipv6 link local addresses that may or may not exist on the device depending on OS configuration""" ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs = \ addresses.getIpInfo(*a, **kw) return ipv4addr, ipv4netmask, ipv4addrs, ipv6addrs IP_ADDR = '192.0.2.2' IP_ADDR_SECOND = '192.0.2.3' IP_ADDR_GW = '192.0.2.1' IP_ADDR2 = '198.51.100.9' IP_ADDR3 = '198.51.100.11' IP_ADDR2_GW = '198.51.100.1' IPV6_ADDR = '2607:f0d0:1002:51::4' NET_MASK = '255.255.255.0' PREFIX_LENGTH = 24 IPV6_PREFIX_LENGTH = 64 IP_ADDR_CIDR = self._cidr_form(IP_ADDR, PREFIX_LENGTH) IP_ADDR_2ND_CIDR = self._cidr_form(IP_ADDR_SECOND, PREFIX_LENGTH) IP_ADDR2_CIDR = self._cidr_form(IP_ADDR2, PREFIX_LENGTH) IP_ADDR3_CIDR = self._cidr_form(IP_ADDR3, 32) IPV6_ADDR_CIDR = self._cidr_form(IPV6_ADDR, IPV6_PREFIX_LENGTH) with dummy_device() as device: with waitfor.waitfor_ipv4_addr(device, address=IP_ADDR_CIDR): ipwrapper.addrAdd(device, IP_ADDR, PREFIX_LENGTH) with waitfor.waitfor_ipv4_addr(device, address=IP_ADDR_2ND_CIDR): ipwrapper.addrAdd(device, IP_ADDR_SECOND, PREFIX_LENGTH) with waitfor.waitfor_ipv4_addr(device, address=IP_ADDR2_CIDR): ipwrapper.addrAdd(device, IP_ADDR2, PREFIX_LENGTH) with waitfor.waitfor_ipv6_addr(device, address=IPV6_ADDR_CIDR): ipwrapper.addrAdd( device, IPV6_ADDR, IPV6_PREFIX_LENGTH, family=6) # 32 bit addresses are reported slashless by netlink with waitfor.waitfor_ipv4_addr(device, address=IP_ADDR3): ipwrapper.addrAdd(device, IP_ADDR3, 32) self.assertEqual( get_ip_info(device), (IP_ADDR, NET_MASK, [IP_ADDR_CIDR, IP_ADDR2_CIDR, IP_ADDR3_CIDR, IP_ADDR_2ND_CIDR], [IPV6_ADDR_CIDR])) self.assertEqual( get_ip_info(device, ipv4_gateway=IP_ADDR_GW), (IP_ADDR, NET_MASK, [IP_ADDR_CIDR, IP_ADDR2_CIDR, IP_ADDR3_CIDR, IP_ADDR_2ND_CIDR], [IPV6_ADDR_CIDR])) self.assertEqual( get_ip_info(device, ipv4_gateway=IP_ADDR2_GW), (IP_ADDR2, NET_MASK, [IP_ADDR_CIDR, IP_ADDR2_CIDR, IP_ADDR3_CIDR, IP_ADDR_2ND_CIDR], [IPV6_ADDR_CIDR]))
def test_attach_nic_with_ip_as_a_slave_to_ipless_network(self, switch): with dummy_devices(2) as (nic1, nic2): addrAdd(nic1, IPv4_ADDRESS, IPv4_PREFIX_LEN) NETCREATE = { NETWORK_NAME: {'bonding': BOND_NAME, 'switch': switch}} BONDCREATE = { BOND_NAME: {'nics': [nic1, nic2], 'switch': switch}} with adapter.setupNetworks(NETCREATE, BONDCREATE, NOCHK): nic_netinfo = adapter.netinfo.nics[nic1] adapter.assertDisabledIPv4(nic_netinfo)
def set_ip(self, ipaddr, netmask, family=4): try: addrAdd(self.devName, ipaddr, netmask, family) except IPRoute2Error as e: message = ('Failed to add the IPv%s address %s/%s to device %s: %s' % (family, ipaddr, netmask, self.devName, e)) if family == 6: message += ("; NetworkManager may have set the sysctl " "disable_ipv6 flag on the device, please see e.g. " "RH BZ #1102064") raise SkipTest(message)
def test_attach_nic_with_ip_as_a_slave_to_ipless_network( self, adapter, switch, nic0, nic1): addrAdd(nic0, IPv4_ADDRESS, IPv4_PREFIX_LEN) addrAdd(nic0, IPv6_ADDRESS, IPv6_PREFIX_LEN, family=6) NETCREATE = {NETWORK_NAME: {'bonding': BOND_NAME, 'switch': switch}} BONDCREATE = {BOND_NAME: {'nics': [nic0, nic1], 'switch': switch}} with adapter.setupNetworks(NETCREATE, BONDCREATE, NOCHK): nic_netinfo = adapter.netinfo.nics[nic0] adapter.assertDisabledIPv4(nic_netinfo) adapter.assertDisabledIPv6(nic_netinfo)
def _add_ipv6_address(iface, ipv6): if ipv6.address: ipv6addr, ipv6netmask = ipv6.address.split('/') ipwrapper.addrAdd(iface, ipv6addr, ipv6netmask, family=6) if ipv6.gateway: ipwrapper.routeAdd(['default', 'via', ipv6.gateway], dev=iface, family=6) if ipv6.ipv6autoconf is not None: with open('/proc/sys/net/ipv6/conf/%s/autoconf' % iface, 'w') as ipv6_autoconf: ipv6_autoconf.write('1' if ipv6.ipv6autoconf else '0')
def test_attach_nic_with_ip_as_a_slave_to_ipless_network(self): with dummy_devices(2) as (nic1, nic2): addrAdd(nic1, IPv4_ADDRESS, IPv4_PREFIX_LEN) NETCREATE = { NETWORK_NAME: {'bonding': BOND_NAME, 'switch': self.switch}} BONDCREATE = { BOND_NAME: {'nics': [nic1, nic2], 'switch': self.switch}} with self.setupNetworks(NETCREATE, BONDCREATE, NOCHK): nic_netinfo = self.netinfo.nics[nic1] self.assertDisabledIPv4(nic_netinfo)
def dynamic_ipv6_iface(): if running_on_ovirt_ci(): pytest.skip('Using dnsmasq for ipv6 RA is unstable on CI') with veth_pair() as (server, client): ipwrapper.addrAdd(server, IPV6_ADDR1, IPV6_PREFIX_LENGTH, family=6) ipwrapper.linkSet(server, ['up']) with dnsmasq_run(server, ipv6_slaac_prefix=IPV6_NET_ADDR): with wait_for_ipv6(client): ipwrapper.linkSet(client, ['up']) yield client
def _test_add_net_with_dhcpv4(self, bridged=False): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO): netcreate = {NETWORK_NAME: { 'bridged': bridged, 'nic': client, 'blockingdhcp': True, 'bootproto': 'dhcp', 'switch': self.switch}} with self.setupNetworks(netcreate, {}, NOCHK): self.assertNetworkIp( NETWORK_NAME, netcreate[NETWORK_NAME])
def test_attach_dhcp_nic_to_ipless_network(self): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) linkSet(server, ["up"]) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO): with dhclient_run(client): self.assertDhclient(client, family=4) NETCREATE = {NETWORK_NAME: {"nic": client, "switch": self.switch}} with self.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = self.netinfo.nics[client] self.assertDisabledIPv4(nic_netinfo) net_netinfo = self.netinfo.networks[NETWORK_NAME] self.assertDisabledIPv4(net_netinfo)
def _add_ipv6_address(iface, ipv6): if ipv6.address: ipv6addr, ipv6netmask = ipv6.address.split('/') try: ipwrapper.addrAdd(iface, ipv6addr, ipv6netmask, family=6) except ipwrapper.IPRoute2AlreadyExistsError: logging.warning( 'IP address already exists: %s/%s', iface, ipv6addr) if ipv6.gateway and ipv6.defaultRoute: set_default_route(ipv6.gateway, family=6, dev=iface) if ipv6.ipv6autoconf is not None: with open('/proc/sys/net/ipv6/conf/%s/autoconf' % iface, 'w') as ipv6_autoconf: ipv6_autoconf.write('1' if ipv6.ipv6autoconf else '0')
def _test_add_net_with_dhcpv4(self, bridged=False): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) linkSet(server, ["up"]) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO): netcreate = { NETWORK_NAME: { "bridged": bridged, "nic": client, "blockingdhcp": True, "bootproto": "dhcp", "switch": self.switch, } } with self.setupNetworks(netcreate, {}, NOCHK): self.assertNetworkIp(NETWORK_NAME, netcreate[NETWORK_NAME])
def test_attach_dhcp_nic_to_dhcpv6_bridged_network(self, switch): with veth_pair() as (server, client): addrAdd(server, IPv6_ADDRESS, IPv6_CIDR, IpFamily.IPv6) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO): with dhclient_run(client, family=IpFamily.IPv6): adapter.assertDhclient(client, family=IpFamily.IPv6) NETCREATE = {NETWORK_NAME: { 'nic': client, 'dhcpv6': True, 'blockingdhcp': True, 'switch': switch}} with adapter.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = adapter.netinfo.nics[client] adapter.assertDisabledIPv6(nic_netinfo) adapter.assertNoDhclient(client, family=IpFamily.IPv6) net_netinfo = adapter.netinfo.networks[NETWORK_NAME] adapter.assertDHCPv6(net_netinfo) adapter.assertDhclient(NETWORK_NAME, family=IpFamily.IPv6)
def _setIpConfig(self, iface): ipv4 = iface.ipv4 ipv6 = iface.ipv6 if ipv4.address or ipv6.address: self.removeIpConfig(iface) if ipv4.address: ipwrapper.addrAdd(iface.name, ipv4.address, ipv4.netmask) if ipv4.gateway and ipv4.defaultRoute: ipwrapper.routeAdd(['default', 'via', ipv4.gateway]) if ipv6.address: ipv6addr, ipv6netmask = ipv6.address.split('/') ipwrapper.addrAdd(iface.name, ipv6addr, ipv6netmask, family=6) if ipv6.gateway: ipwrapper.routeAdd(['default', 'via', ipv6.gateway], dev=iface.name, family=6) if ipv6.ipv6autoconf is not None: with open('/proc/sys/net/ipv6/conf/%s/autoconf' % iface.name, 'w') as ipv6_autoconf: ipv6_autoconf.write('1' if ipv6.ipv6autoconf else '0')
def test_attach_dhcp_nic_to_ipless_network(self, switch): with veth_pair() as (server, client): addrAdd(server, IPv4_ADDRESS, IPv4_PREFIX_LEN) addrAdd(server, IPv6_ADDRESS, IPv6_CIDR, IpFamily.IPv6) linkSet(server, ['up']) with dnsmasq_run(server, DHCPv4_RANGE_FROM, DHCPv4_RANGE_TO, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO, router=DHCPv4_GATEWAY): with dhclient_run(client): adapter.assertDhclient(client, family=IpFamily.IPv4) adapter.assertDhclient(client, family=IpFamily.IPv6) NETCREATE = {NETWORK_NAME: { 'nic': client, 'switch': switch}} with adapter.setupNetworks(NETCREATE, {}, NOCHK): nic_netinfo = adapter.netinfo.nics[client] adapter.assertDisabledIPv4(nic_netinfo) adapter.assertDisabledIPv6(nic_netinfo) net_netinfo = adapter.netinfo.networks[NETWORK_NAME] adapter.assertDisabledIPv4(net_netinfo) adapter.assertDisabledIPv6(nic_netinfo)
def test_sourceroute_add_remove_and_read(self): with dummy_device() as nic: addrAdd(nic, IPV4_ADDRESS, IPV4_MASK) with create_sourceroute(device=nic, ip=IPV4_ADDRESS, mask=IPV4_MASK, gateway=IPV4_GW): dsroute = DynamicSourceRoute(nic, None, None, None) routes, rules = dsroute.current_srconfig() self.assertEqual(2, len(routes), routes) self.assertEqual(2, len(rules), rules) self.assertEqual('0.0.0.0/0', routes[0].to) self.assertEqual(nic, routes[0].device) self.assertEqual(IPv4_NET, routes[1].to) self.assertEqual(nic, routes[1].device) self.assertEqual(IPv4_NET, rules[0].to) self.assertEqual(IPV4_TABLE, rules[0].table) self.assertEqual(nic, rules[0].iif) self.assertEqual(IPv4_NET, rules[1].src) self.assertEqual(IPV4_TABLE, rules[1].table)
def test_iperf_upper_limit(self): # Upper limit is not an accurate measure. This is because it converges # over time and depends on current machine hardware (CPU). # Hence, it is hard to make hard assertions on it. The test should run # at least 60 seconds (the longer the better) and the user should # inspect the computed average rate and optionally the additional # traffic data that was collected in client.out in order to be # convinced QOS is working properly. limit_kbps = 1000 # 1 Mbps (in kbps) server_ip = '192.0.2.1' client_ip = '192.0.2.10' qos_out = {'ul': {'m2': limit_kbps}, 'ls': {'m2': limit_kbps}} # using a network namespace is essential since otherwise the kernel # short-circuits the traffic and bypasses the veth devices and the # classfull qdisc. with network_namespace('server_ns') as ns, bridge_device() as bridge, \ veth_pair() as (server_peer, server_dev), \ veth_pair() as (client_dev, client_peer): linkSet(server_peer, ['up']) linkSet(client_peer, ['up']) # iperf server and its veth peer lie in a separate network # namespace link_set_netns(server_dev, ns) bridge.addIf(server_peer) bridge.addIf(client_peer) linkSet(client_dev, ['up']) netns_exec(ns, ['ip', 'link', 'set', 'dev', server_dev, 'up']) addrAdd(client_dev, client_ip, 24) netns_exec(ns, ['ip', '-4', 'addr', 'add', 'dev', server_dev, '%s/24' % server_ip]) qos.configure_outbound(qos_out, client_peer, None) with running(IperfServer(server_ip, network_ns=ns)): client = IperfClient(server_ip, client_ip, test_time=60) client.start() max_rate = max([float( interval['streams'][0]['bits_per_second']) / (2**10) for interval in client.out['intervals']]) self.assertTrue(0 < max_rate < limit_kbps * 1.5)
def test_local_auto_with_dynamic_address_from_ra(self): IPV6_NETADDRESS = '2001:1:1:1' IPV6_NETPREFIX_LEN = '64' with veth_pair() as (server, client): ipwrapper.addrAdd(server, IPV6_NETADDRESS + '::1', IPV6_NETPREFIX_LEN, family=6) ipwrapper.linkSet(server, ['up']) with dnsmasq_run(server, ipv6_slaac_prefix=IPV6_NETADDRESS + '::'): with wait_for_ipv6(client): ipwrapper.linkSet(client, ['up']) # Expecting link and global addresses on client iface # The addresses are given randomly, so we sort them ip_addrs = sorted(addresses.getIpAddrs()[client], key=lambda ip: ip['address']) self.assertEqual(2, len(ip_addrs), ip_addrs) self.assertTrue(addresses.is_dynamic(ip_addrs[0])) self.assertEqual('global', ip_addrs[0]['scope']) self.assertEqual(IPV6_NETADDRESS, ip_addrs[0]['address'][:len(IPV6_NETADDRESS)]) self.assertEqual('link', ip_addrs[1]['scope'])
def test_ip_info(self): IPV4_ADDR1 = '192.168.99.2' IPV4_GATEWAY1 = '192.168.99.1' IPV4_ADDR2 = '192.168.199.2' IPV4_GATEWAY2 = '192.168.199.1' IPV4_ADDR3 = '192.168.200.2' IPV4_NETMASK = '255.255.255.0' IPV4_PREFIX_LENGTH = 24 IPV6_ADDR = '2607:f0d0:1002:51::4' IPV6_PREFIX_LENGTH = 64 IPV4_ADDR1_CIDR = self._cidr_form(IPV4_ADDR1, IPV4_PREFIX_LENGTH) IPV4_ADDR2_CIDR = self._cidr_form(IPV4_ADDR2, IPV4_PREFIX_LENGTH) IPV4_ADDR3_CIDR = self._cidr_form(IPV4_ADDR3, 32) IPV6_ADDR_CIDR = self._cidr_form(IPV6_ADDR, IPV6_PREFIX_LENGTH) with dummy_device() as device: with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR1_CIDR): ipwrapper.addrAdd(device, IPV4_ADDR1, IPV4_PREFIX_LENGTH) with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR2_CIDR): ipwrapper.addrAdd(device, IPV4_ADDR2, IPV4_PREFIX_LENGTH) with waitfor.waitfor_ipv6_addr(device, address=IPV6_ADDR_CIDR): ipwrapper.addrAdd( device, IPV6_ADDR, IPV6_PREFIX_LENGTH, family=6) # 32 bit addresses are reported slashless by netlink with waitfor.waitfor_ipv4_addr(device, address=IPV4_ADDR3): ipwrapper.addrAdd(device, IPV4_ADDR3, 32) self.assertEqual( addresses.getIpInfo(device), (IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR])) self.assertEqual( addresses.getIpInfo(device, ipv4_gateway=IPV4_GATEWAY1), (IPV4_ADDR1, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR])) self.assertEqual( addresses.getIpInfo(device, ipv4_gateway=IPV4_GATEWAY2), (IPV4_ADDR2, IPV4_NETMASK, [IPV4_ADDR1_CIDR, IPV4_ADDR2_CIDR, IPV4_ADDR3_CIDR], [IPV6_ADDR_CIDR]))
def testSetupNetworksAddDelDhcp(self, bridged, families): """Copied from networkTests.py, source_route checking changed from device_name to BRIDGE_NAME. """ def _assert_applied(network_name, requested, reported): self.assertNetworkExists(network_name) reported_network = reported.networks[network_name] if requested['bridged']: reported_devices = reported.bridges device_name = network_name else: # CHANGED: always bridged pass self.assertIn(device_name, reported_devices) reported_device = reported_devices[device_name] requested_dhcpv4 = requested['bootproto'] == 'dhcp' self.assertEqual(reported_network['dhcpv4'], requested_dhcpv4) self.assertEqual(reported_network['dhcpv6'], requested['dhcpv6']) self.assertEqual(reported_device['dhcpv4'], requested_dhcpv4) self.assertEqual(reported_device['dhcpv6'], requested['dhcpv6']) if requested_dhcpv4: self.assertEqual(reported_network['gateway'], IP_GATEWAY) # TODO: source routing not ready for IPv6 ip_addr = reported_network['addr'] self.assertSourceRoutingConfiguration(BRIDGE_NAME, # CHANGED ip_addr) return device_name, ip_addr return None, None with veth_pair() as (left, right): addrAdd(left, IP_ADDRESS, IP_CIDR) addrAdd(left, IPv6_ADDRESS, IPv6_CIDR, 6) linkSet(left, ['up']) with dnsmasq_run(left, DHCP_RANGE_FROM, DHCP_RANGE_TO, DHCPv6_RANGE_FROM, DHCPv6_RANGE_TO, IP_GATEWAY): dhcpv4 = 4 in families dhcpv6 = 6 in families bootproto = 'dhcp' if dhcpv4 else 'none' network = {NETWORK_NAME: {'nic': right, 'bridged': bridged, 'bootproto': bootproto, 'dhcpv6': dhcpv6, 'blockingdhcp': True}} try: status, msg = self.setupNetworks(network, {}, NOCHK) self.assertEqual(status, SUCCESS, msg) device_name, ip_addr = _assert_applied( NETWORK_NAME, network[NETWORK_NAME], self.vdsm_net.netinfo) # Do not report DHCP from (typically still valid) leases network[NETWORK_NAME]['bootproto'] = 'none' network[NETWORK_NAME]['dhcpv6'] = False status, msg = self.setupNetworks(network, {}, NOCHK) self.assertEqual(status, SUCCESS, msg) _assert_applied(NETWORK_NAME, network[NETWORK_NAME], self.vdsm_net.netinfo) network = {NETWORK_NAME: {'remove': True}} status, msg = self.setupNetworks(network, {}, NOCHK) self.assertEqual(status, SUCCESS, msg) self.assertNetworkDoesntExist(NETWORK_NAME) # Assert that routes and rules don't exist if dhcpv4: source_route = _get_source_route(device_name, ip_addr) for route in source_route._buildRoutes(): self.assertRouteDoesNotExist(route) for rule in source_route._buildRules(): self.assertRuleDoesNotExist(rule) finally: dhcp.delete_dhclient_leases( NETWORK_NAME if bridged else right, dhcpv4, dhcpv6)
def add(addr_data): with _translate_iproute2_exception(IPAddressAddError, addr_data): ipwrapper.addrAdd( addr_data.device, addr_data.address, addr_data.prefixlen, addr_data.family )
def _add_ipv4_address(iface, ipv4): ipwrapper.addrAdd(iface, ipv4.address, ipv4.netmask) if ipv4.gateway and ipv4.defaultRoute: _set_default_route(ipv4.gateway, family=4)