HALF ECHO LEARN / REFERENCE COLLECTION
v262.2test/test-network/systemd-networkd-tests.py
Git blob: aaf8830b28df24259c9ca79d57929ab5449e46af
Lines 8001–9000 of 11,559
copy_network_unit(
'25-veth.netdev',
'25-ipv6-prefix.network',
'25-ipv6-prefix-veth-token-prefixstable-without-address.network',
)
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:degraded')
output = networkctl_status('veth99')
print(output)
self.assertIn('2002:da8:1:0:b47e:7975:fc7a:7d6e', output)
self.assertIn('2002:da8:2:0:f689:561a:8eda:7443', output)
def test_router_hop_limit(self):
copy_network_unit(
'25-veth-client.netdev',
'25-veth-router.netdev',
'26-bridge.netdev',
'25-veth-bridge.network',
'25-veth-client.network',
'25-veth-router-hop-limit.network',
'25-bridge99.network',
)
start_networkd()
self.wait_online(
'client:routable',
'client-p:enslaved',
'router:degraded',
'router-p:enslaved',
'bridge99:routable',
)
self.check_ipv6_sysctl_attr('client', 'hop_limit', '42')
with open(
os.path.join(network_unit_dir, '25-veth-router-hop-limit.network'), mode='a', encoding='utf-8'
) as f:
f.write('\n[IPv6SendRA]\nHopLimit=43\n')
networkctl_reload()
for _ in range(20):
output = read_ipv6_sysctl_attr('client', 'hop_limit')
if output == '43':
break
time.sleep(0.5)
self.check_ipv6_sysctl_attr('client', 'hop_limit', '43')
def check_router_preference(self, suffix, metric_1, preference_1, metric_2, preference_2):
self.wait_online('client:routable')
self.wait_address('client', f'2002:da8:1:99:1034:56ff:fe78:9a{suffix}/64', ipv='-6', timeout_sec=10)
self.wait_address('client', f'2002:da8:1:98:1034:56ff:fe78:9a{suffix}/64', ipv='-6', timeout_sec=10)
self.wait_route(
'client',
rf'default nhid [0-9]* via fe80::1034:56ff:fe78:9a99 proto ra metric {metric_1}',
ipv='-6',
timeout_sec=10,
)
self.wait_route(
'client',
rf'default nhid [0-9]* via fe80::1034:56ff:fe78:9a98 proto ra metric {metric_2}',
ipv='-6',
timeout_sec=10,
)
print('### ip -6 route show dev client default')
output = check_output('ip -6 route show dev client default')
print(output)
# fmt: off
self.assertRegex(output, rf'default nhid [0-9]* via fe80::1034:56ff:fe78:9a99 proto ra metric {metric_1} expires [0-9]*sec pref {preference_1}')
self.assertRegex(output, rf'default nhid [0-9]* via fe80::1034:56ff:fe78:9a98 proto ra metric {metric_2} expires [0-9]*sec pref {preference_2}')
# fmt: on
for i in [100, 200, 300, 512, 1024, 2048]:
if i not in [metric_1, metric_2]:
self.assertNotIn(f'metric {i} ', output)
for i in ['low', 'medium', 'high']:
if i not in [preference_1, preference_2]:
self.assertNotIn(f'pref {i}', output)
def test_router_preference(self):
copy_network_unit(
'25-veth-client.netdev',
'25-veth-router-high.netdev',
'25-veth-router-low.netdev',
'26-bridge.netdev',
'25-veth-bridge.network',
'25-veth-client.network',
'25-veth-router-high.network',
'25-veth-router-low.network',
'25-bridge99.network',
)
start_networkd()
self.wait_online(
'client-p:enslaved',
'router-high:degraded',
'router-high-p:enslaved',
'router-low:degraded',
'router-low-p:enslaved',
'bridge99:routable',
)
networkctl_reconfigure('client')
self.wait_online('client:routable')
self.check_router_preference('00', 512, 'high', 2048, 'low')
# change the map from preference to metric.
with open(os.path.join(network_unit_dir, '25-veth-client.network'), mode='a', encoding='utf-8') as f:
f.write('''
[Link]
MACAddress=12:34:56:78:9a:01
[IPv6AcceptRA]
RouteMetric=100:200:300
''')
networkctl_reload()
self.check_router_preference('01', 100, 'high', 300, 'low')
# swap the preference (for issue #28439)
with open(
os.path.join(network_unit_dir, '25-veth-router-high.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
[IPv6SendRA]
RouterPreference=low
''')
with open(
os.path.join(network_unit_dir, '25-veth-router-low.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
[IPv6SendRA]
RouterPreference=high
''')
networkctl_reload()
self.check_router_preference('01', 300, 'low', 100, 'high')
# Use the same preference, and check if the two routes are not coalesced. See issue #33470.
with open(
os.path.join(network_unit_dir, '25-veth-router-high.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
[IPv6SendRA]
RouterPreference=medium
''')
with open(
os.path.join(network_unit_dir, '25-veth-router-low.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
[IPv6SendRA]
RouterPreference=medium
''')
networkctl_reload()
self.check_router_preference('01', 200, 'medium', 200, 'medium')
# Use route options to configure default routes.
# The preference specified in the RA header should be ignored. See issue #33468.
with open(
os.path.join(network_unit_dir, '25-veth-router-high.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
[IPv6SendRA]
RouterPreference=high
[IPv6RoutePrefix]
Route=::/0
LifetimeSec=1200
''')
with open(
os.path.join(network_unit_dir, '25-veth-router-low.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
[IPv6SendRA]
RouterPreference=low
[IPv6RoutePrefix]
Route=::/0
LifetimeSec=1200
''')
networkctl_reload()
self.check_router_preference('01', 200, 'medium', 200, 'medium')
# Set zero lifetime to the route options.
# The preference specified in the RA header should be used.
with open(
os.path.join(network_unit_dir, '25-veth-router-high.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
LifetimeSec=0
''')
with open(
os.path.join(network_unit_dir, '25-veth-router-low.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
LifetimeSec=0
''')
networkctl_reload()
self.check_router_preference('01', 100, 'high', 300, 'low')
# Use route options with preference to configure default routes.
with open(
os.path.join(network_unit_dir, '25-veth-router-high.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
LifetimeSec=1200
Preference=low
''')
with open(
os.path.join(network_unit_dir, '25-veth-router-low.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
LifetimeSec=1200
Preference=high
''')
networkctl_reload()
self.check_router_preference('01', 300, 'low', 100, 'high')
# Set zero lifetime again to the route options.
with open(
os.path.join(network_unit_dir, '25-veth-router-high.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
LifetimeSec=0
''')
with open(
os.path.join(network_unit_dir, '25-veth-router-low.network'), mode='a', encoding='utf-8'
) as f:
f.write('''
LifetimeSec=0
''')
networkctl_reload()
self.check_router_preference('01', 100, 'high', 300, 'low')
def _test_ndisc_vs_static_route(self, manage_foreign_nexthops):
if not manage_foreign_nexthops:
copy_networkd_conf_dropin('networkd-manage-foreign-nexthops-no.conf')
copy_network_unit(
'25-veth.netdev',
'25-ipv6-prefix.network',
'25-ipv6-prefix-veth-static-route.network',
)
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:degraded')
# If a conflicting static route is already configured, do not override the static route.
print('### ip -6 route show dev veth99 default')
output = check_output('ip -6 route show dev veth99 default')
print(output)
self.assertIn('via fe80::1034:56ff:fe78:9abd proto static metric 256 pref medium', output)
if manage_foreign_nexthops:
self.assertRegex(output, r'default nhid [0-9]* via fe80::1034:56ff:fe78:9abd proto ra metric 256 expires [0-9]*sec pref medium') # fmt: skip
else:
self.assertNotIn('proto ra', output)
print('### ip -6 nexthop show dev veth99')
output = check_output('ip -6 nexthop show dev veth99')
print(output)
if manage_foreign_nexthops:
self.assertRegex(output, r'id [0-9]* via fe80::1034:56ff:fe78:9abd dev veth99 scope link proto ra') # fmt: skip
else:
self.assertEqual(output, '')
# Also check if the static route is protected from RA with zero lifetime
with open(os.path.join(network_unit_dir, '25-ipv6-prefix.network'), mode='a', encoding='utf-8') as f:
f.write('''
[Network]
IPv6SendRA=no
''')
networkctl_reload() # This makes veth-peer being reconfigured, and send RA with zero lifetime
self.wait_route_dropped(
'veth99',
r'default (nhid [0-9]* |)via fe80::1034:56ff:fe78:9abd proto ra metric 256',
ipv='-6',
timeout_sec=10,
)
print('### ip -6 route show dev veth99 default')
output = check_output('ip -6 route show dev veth99 default')
print(output)
self.assertIn('via fe80::1034:56ff:fe78:9abd proto static metric 256 pref medium', output)
self.assertNotIn('proto ra', output)
# Check if nexthop is removed.
print('### ip -6 nexthop show dev veth99')
output = check_output('ip -6 nexthop show dev veth99')
print(output)
self.assertEqual(output, '')
def test_ndisc_vs_static_route(self):
first = True
for manage_foreign_nexthops in [True, False]:
if first:
first = False
else:
self.tearDown()
print(f'### test_ndisc_vs_static_route(manage_foreign_nexthops={manage_foreign_nexthops})')
with self.subTest(manage_foreign_nexthops=manage_foreign_nexthops):
self._test_ndisc_vs_static_route(manage_foreign_nexthops)
# radvd supports captive portal since v2.20.
# https://github.com/radvd-project/radvd/commit/791179a7f730decbddb2290ef0e34aa85d71b1bc
@unittest.skipUnless(
radvd_check_config('captive-portal.conf'),
"Installed radvd doesn't support captive portals",
)
def test_captive_portal(self):
copy_network_unit(
'25-veth-client.netdev',
'25-veth-router-captive.netdev',
'26-bridge.netdev',
'25-veth-client-captive.network',
'25-veth-router-captive.network',
'25-veth-bridge-captive.network',
'25-bridge99.network',
)
start_networkd()
self.wait_online(
'bridge99:routable',
'client-p:enslaved',
'router-captive:degraded',
'router-captivep:enslaved',
)
start_radvd(config_file='captive-portal.conf')
networkctl_reconfigure('client')
self.wait_online('client:routable')
self.wait_address('client', '2002:da8:1:99:1034:56ff:fe78:9a00/64', ipv='-6', timeout_sec=10)
output = networkctl_status('client')
print(output)
self.assertIn('Captive Portal: http://systemd.io', output)
@unittest.skipUnless(
radvd_check_config('captive-portal.conf'),
"Installed radvd doesn't support captive portals",
)
def test_invalid_captive_portal(self):
def radvd_write_config(captive_portal_uri):
with open(
os.path.join(networkd_ci_temp_dir, 'radvd/bogus-captive-portal.conf'),
mode='w',
encoding='utf-8',
) as f:
f.write(f'''
interface router-captive {{
AdvSendAdvert on;
AdvCaptivePortalAPI "{captive_portal_uri}";
prefix 2002:da8:1:99::/64 {{
AdvOnLink on;
AdvAutonomous on;
}};
}};
''')
captive_portal_uris = [
'42ěščěškd ěšč ě s',
' ',
'🤔',
]
copy_network_unit(
'25-veth-client.netdev',
'25-veth-router-captive.netdev',
'26-bridge.netdev',
'25-veth-client-captive.network',
'25-veth-router-captive.network',
'25-veth-bridge-captive.network',
'25-bridge99.network',
)
start_networkd()
self.wait_online(
'bridge99:routable',
'client-p:enslaved',
'router-captive:degraded',
'router-captivep:enslaved',
)
for uri in captive_portal_uris:
print(f'Captive portal: {uri}')
radvd_write_config(uri)
stop_radvd()
start_radvd(config_file='bogus-captive-portal.conf')
networkctl_reconfigure('client')
self.wait_online('client:routable')
self.wait_address('client', '2002:da8:1:99:1034:56ff:fe78:9a00/64', ipv='-6', timeout_sec=10)
output = networkctl_status('client')
print(output)
self.assertNotIn('Captive Portal:', output)
class NetworkdDHCPServerTests(unittest.TestCase, Utilities):
def setUp(self):
setup_common()
def tearDown(self):
tear_down_common()
def check_dhcp_server(self, persist_leases='yes'):
output = networkctl_status('veth99')
print(output)
self.assertRegex(output, r'Address: 192.168.5.[0-9]* \(DHCPv4 via 192.168.5.1\)')
self.assertIn('Gateway: 192.168.5.3', output)
self.assertRegex(output, 'DNS: 192.168.5.1\n *192.168.5.10')
self.assertRegex(output, 'NTP: 192.168.5.1\n *192.168.5.11')
output = networkctl_status('veth-peer')
print(output)
self.assertRegex(output, 'Offered DHCP leases: 192.168.5.[0-9]*')
self.assertEqual(get_dhcp_server_property('veth-peer', 'PoolSize'), 'u 50')
self.assertEqual(get_dhcp_server_property('veth-peer', 'PoolOffset'), 'u 10')
if persist_leases == 'yes':
path = '/var/lib/systemd/network/dhcp-server-lease/veth-peer'
elif persist_leases == 'runtime':
path = '/run/systemd/netif/dhcp-server-lease/veth-peer'
else:
path = None
if path:
with open(path, encoding='utf-8') as f:
check_json(f.read())
def test_dhcp_server(self):
copy_network_unit('25-veth.netdev', '25-dhcp-client.network', '25-dhcp-server.network')
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
self.check_dhcp_server()
networkctl_reconfigure('veth-peer')
self.wait_online('veth-peer:routable')
for _ in range(10):
output = networkctl_status('veth-peer')
if 'Offered DHCP leases: 192.168.5.' in output:
break
time.sleep(0.2)
else:
self.fail()
def test_dhcp_server_persist_leases_no(self):
copy_networkd_conf_dropin('persist-leases-no.conf')
copy_network_unit('25-veth.netdev', '25-dhcp-client.network', '25-dhcp-server.network')
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
self.check_dhcp_server(persist_leases='no')
remove_networkd_conf_dropin('persist-leases-no.conf')
with open(os.path.join(network_unit_dir, '25-dhcp-server.network'), mode='a', encoding='utf-8') as f:
f.write('[DHCPServer]\nPersistLeases=no')
restart_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
self.check_dhcp_server(persist_leases='no')
def test_dhcp_server_persist_leases_runtime(self):
copy_networkd_conf_dropin('persist-leases-runtime.conf')
copy_network_unit('25-veth.netdev', '25-dhcp-client.network', '25-dhcp-server.network')
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
self.check_dhcp_server(persist_leases='runtime')
remove_networkd_conf_dropin('persist-leases-runtime.conf')
with open(os.path.join(network_unit_dir, '25-dhcp-server.network'), mode='a', encoding='utf-8') as f:
f.write('[DHCPServer]\nPersistLeases=runtime')
restart_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
self.check_dhcp_server(persist_leases='runtime')
def test_dhcp_server_null_server_address(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-client.network',
'25-dhcp-server-null-server-address.network',
)
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = check_output('ip --json address show dev veth-peer')
server_address = json.loads(output)[0]['addr_info'][0]['local']
print(server_address)
output = check_output('ip --json address show dev veth99')
client_address = json.loads(output)[0]['addr_info'][0]['local']
print(client_address)
output = networkctl_status('veth99')
print(output)
self.assertRegex(output, rf'Address: {client_address} \(DHCPv4 via {server_address}\)')
self.assertIn(f'Gateway: {server_address}', output)
self.assertIn(f'DNS: {server_address}', output)
self.assertIn(f'NTP: {server_address}', output)
output = networkctl_status('veth-peer')
self.assertIn(f'Offered DHCP leases: {client_address}', output)
# Check if the same addresses are used even if the service is restarted.
restart_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = check_output('ip -4 address show dev veth-peer')
print(output)
self.assertIn(f'{server_address}', output)
output = check_output('ip -4 address show dev veth99')
print(output)
self.assertIn(f'{client_address}', output)
output = networkctl_status('veth99')
print(output)
self.assertRegex(output, rf'Address: {client_address} \(DHCPv4 via {server_address}\)')
self.assertIn(f'Gateway: {server_address}', output)
self.assertIn(f'DNS: {server_address}', output)
self.assertIn(f'NTP: {server_address}', output)
output = networkctl_status('veth-peer')
self.assertIn(f'Offered DHCP leases: {client_address}', output)
def test_dhcp_server_with_uplink(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-client.network',
'25-dhcp-server-downstream.network',
'12-dummy.netdev',
'25-dhcp-server-uplink.network',
)
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = networkctl_status('veth99')
print(output)
self.assertRegex(output, r'Address: 192.168.5.[0-9]* \(DHCPv4 via 192.168.5.1\)')
self.assertIn('Gateway: 192.168.5.3', output)
self.assertIn('DNS: 192.168.5.1', output)
self.assertIn('NTP: 192.168.5.1', output)
def test_emit_router_timezone(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-client-timezone-router.network',
'25-dhcp-server-timezone-router.network',
)
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = networkctl_status('veth99')
print(output)
self.assertRegex(output, r'Address: 192.168.5.[0-9]* \(DHCPv4 via 192.168.5.1\)')
self.assertIn('Gateway: 192.168.5.1', output)
self.assertIn('Time Zone: Europe/Berlin', output)
def test_dhcp_server_static_lease_mac_by_network(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-client-static-lease.network',
'25-dhcp-server-static-lease.network',
)
copy_networkd_conf_dropin('10-dhcp-client-id-duid.conf')
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = networkctl_status('veth99')
print(output)
self.assertIn('Address: 10.1.1.200 (DHCPv4 via 10.1.1.1)', output)
self.assertIn('DHCPv4 Client ID: 12:34:56:78:9a:bc', output)
def test_dhcp_server_static_lease_mac_by_global(self):
copy_network_unit('25-veth.netdev', '25-dhcp-client.network', '25-dhcp-server-static-lease.network')
copy_networkd_conf_dropin('10-dhcp-client-id-mac.conf')
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = networkctl_status('veth99')
print(output)
self.assertIn('Address: 10.1.1.200 (DHCPv4 via 10.1.1.1)', output)
self.assertIn('DHCPv4 Client ID: 12:34:56:78:9a:bc', output)
def test_dhcp_server_static_lease_duid(self):
copy_network_unit('25-veth.netdev', '25-dhcp-client.network', '25-dhcp-server-static-lease.network')
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = networkctl_status('veth99')
print(output)
self.assertIn('Address: 10.1.1.200 (DHCPv4 via 10.1.1.1)', output)
self.assertRegex(output, 'DHCPv4 Client ID: IAID:[0-9a-z]*/DUID')
def test_dhcp_server_static_lease_hostname_simple(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-client-simple-hostname.network',
'25-dhcp-server-static-hostname.network',
)
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = networkctl_json('veth99')
check_json(output)
data = json.loads(output)
self.assertEqual(data['DHCPv4Client']['Lease']['Hostname'], 'simple-host')
def test_dhcp_server_static_lease_hostname_fqdn(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-client-fqdn-hostname.network',
'25-dhcp-server-static-hostname.network',
)
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = networkctl_json('veth99')
check_json(output)
data = json.loads(output)
self.assertEqual(data['DHCPv4Client']['Lease']['Hostname'], 'fqdn.example.com')
def test_dhcp_server_resolve_hook(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-client-resolve-hook.network',
'25-dhcp-server-resolve-hook.network',
)
start_networkd()
self.wait_online('veth99:routable', 'veth-peer:routable')
output = check_output('resolvectl query flummy._networkdtest')
print(output)
self.assertIn('192.168.5.2', output)
class NetworkdDHCPServerRelayAgentTests(unittest.TestCase, Utilities):
def setUp(self):
setup_common()
def tearDown(self):
tear_down_common()
def test_relay_agent(self):
check_output('ip netns add ns-server')
check_output('ip link add server type veth peer server-peer')
check_output('ip link set server netns ns-server')
check_output('ip netns exec ns-server ip link set server up')
check_output('ip netns exec ns-server ip address add 192.0.2.1/24 dev server')
start_dnsmasq(
namespace='ns-server',
interface='server',
ipv4_range='198.51.100.100,198.51.100.109',
ipv4_router='198.51.100.10',
)
copy_network_unit(
'25-agent-veth-client.netdev',
'25-agent-client.network',
'25-agent-client-peer.network',
'25-agent-server-peer.network',
)
start_networkd()
self.wait_online('client:routable')
output = networkctl_status('client')
print(output)
self.assertRegex(output, r'Address: 198\.51\.100\.10[0-9] \(DHCPv4 via 192\.0\.2\.1\)')
def test_relay_agent_on_bridge(self):
copy_network_unit(
'25-agent-bridge.netdev',
'25-agent-veth-client.netdev',
'25-agent-bridge.network',
'25-agent-bridge-port.network',
'25-agent-client.network',
)
start_networkd()
self.wait_online('bridge-relay:routable', 'client-peer:enslaved')
# For issue #30763.
self.check_networkd_log('bridge-relay: Relaying DHCPv4 messages')
def test_sd_dhcp_relay(self):
check_output('ip netns add ns-relay')
check_output('ip netns add ns-server')
check_output('ip link add relay-down type veth peer client')
check_output('ip link set relay-down netns ns-relay')
check_output('ip netns exec ns-relay ip link set relay-down up')
check_output('ip link add relay-up type veth peer server')
check_output('ip link set relay-up netns ns-relay')
check_output('ip netns exec ns-relay ip link set relay-up up')
check_output('ip link set server netns ns-server')
check_output('ip netns exec ns-server ip link set server up')
check_output('ip netns exec ns-server ip address add 192.0.2.1/24 dev server')
with tempfile.TemporaryDirectory() as tmp:
conf_dir = pathlib.Path(tmp) / 'run/systemd/networkd.conf.d'
unit_dir = pathlib.Path(tmp) / 'run/systemd/network'
netif_dir = pathlib.Path(tmp) / 'run/systemd/netif'
mkdir_p(conf_dir)
mkdir_p(unit_dir)
mkdir_p(netif_dir)
# Do not use shutil.chown(), as it fails when running with sanitizers.
check_output(f'chown systemd-network:systemd-network {netif_dir}')
cp(pathlib.Path(networkd_ci_temp_dir) / '25-dhcp-relay.conf', conf_dir)
cp(pathlib.Path(networkd_ci_temp_dir) / '25-dhcp-relay-downstream.network', unit_dir)
cp(pathlib.Path(networkd_ci_temp_dir) / '25-dhcp-relay-upstream.network', unit_dir)
cmd = [
'systemd-run',
'--unit=networkd-test-dhcp-relay.service',
'--service-type=notify-reload',
'-p', 'User=systemd-network',
'-p', 'FileDescriptorStoreMax=512',
'-p', 'AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_RAW CAP_BPF CAP_SYS_ADMIN',
'-p', 'CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_RAW CAP_BPF CAP_SYS_ADMIN',
'-p', f'BindPaths={conf_dir}:/run/systemd/networkd.conf.d',
'-p', f'BindPaths={unit_dir}:/run/systemd/network',
'-p', f'BindPaths={netif_dir}:/run/systemd/netif',
'-p', 'TemporaryFileSystem=/run/dbus',
'-p', 'TemporaryFileSystem=/run/systemd/report',
'-p', 'TemporaryFileSystem=/run/systemd/resolve.hook',
'-p', 'TemporaryFileSystem=/var/lib/systemd/network',
'-p', 'TemporaryFileSystem=/etc/systemd/network',
'-p', 'TemporaryFileSystem=/usr/lib/systemd/network',
'-p', 'ReadOnlyPaths=/sys',
'-p', 'NetworkNamespacePath=/run/netns/ns-relay',
] # fmt: skip
if enable_debug:
cmd += ['-p', 'Environment=SYSTEMD_LOG_LEVEL=debug']
if asan_options:
cmd += ['-p', f'Environment=ASAN_OPTIONS={asan_options}']
if lsan_options:
cmd += ['-p', f'Environment=LSAN_OPTIONS={lsan_options}']
if ubsan_options:
cmd += ['-p', f'Environment=UBSAN_OPTIONS={ubsan_options}']
if asan_options or lsan_options or ubsan_options:
cmd += ['-p', 'TimeoutStopFailureMode=abort']
cmd += [networkd_bin]
try:
check_output(*cmd)
start_dnsmasq(
namespace='ns-server',
interface='server',
ipv4_range='198.51.100.100,198.51.100.109',
ipv4_router='198.51.100.10',
)
copy_network_unit('25-dhcp-client-simple.network')
start_networkd()
self.wait_online('client:routable')
print('## ip -4 address show dev client scope global')
output = check_output('ip -4 address show dev client scope global')
print(output)
self.assertRegex(output, r'198\.51\.100\.10[0-9]/24')
print('## ip -4 route show dev client')
output = check_output('ip -4 route show dev client')
print(output)
# fmt: off
self.assertRegex(output, r'default via 198\.51\.100\.10 proto dhcp src 198\.51\.100\.10[0-9]')
self.assertRegex(output, r'198\.51\.100\.0/24 proto kernel scope link src 198\.51\.100\.10[0-9]')
self.assertRegex(output, r'198\.51\.100\.10 proto dhcp scope link src 198\.51\.100\.10[0-9]')
# fmt: on
print('## dnsmasq log')
output = read_dnsmasq_log_file()
print(output)
# The option 82 is logged as agent-info since dnsmasq-2.92, otherwise logged as agent-id.
self.assertRegex(output, r'option: 82 (agent-id|agent-info)')
print('## journal of networkd-test-dhcp-relay.service')
check_output('journalctl --sync')
output = check_output('journalctl --no-hostname --output=short-monotonic --unit=networkd-test-dhcp-relay.service -I') # fmt: skip
self.assertIn('relay-down: DHCPv4 relay: Received BOOTREQUEST', output)
self.assertIn('relay-up: DHCPv4 relay: Forwarded BOOTREQUEST', output)
self.assertIn('relay-up: DHCPv4 relay: Received BOOTREPLY', output)
self.assertIn('relay-down: DHCPv4 relay: Forwarded BOOTREPLY', output)
finally:
call_quiet('systemctl stop networkd-test-dhcp-relay.service')
call_quiet('systemctl reset-failed networkd-test-dhcp-relay.service')
class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
def setUp(self):
setup_common()
def tearDown(self):
tear_down_common()
def test_dhcp_client_ipv6_only(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-server-veth-peer.network',
'25-dhcp-client-ipv6-only.network',
)
start_networkd()
self.wait_online('veth-peer:carrier')
# information request mode
# The name ipv6-only option may not be supported by older dnsmasq
# start_dnsmasq('--dhcp-option=option:ipv6-only,300')
start_dnsmasq(
'--dhcp-option=108,00:00:02:00',
'--dhcp-option=option6:dns-server,[2600::ee]',
'--dhcp-option=option6:ntp-server,[2600::ff]',
ra_mode='ra-stateless',
)
self.wait_online('veth99:routable', 'veth-peer:routable')
# DHCPv6 REPLY for INFORMATION-REQUEST may be received after the link entered configured state.
# Let's wait for the expected DNS server being listed in the state file.
for _ in range(100):
output = read_link_state_file('veth99')
if 'DNS=2600::ee' in output:
break
time.sleep(0.2)
# Check link state file
print('## link state file')
output = read_link_state_file('veth99')
print(output)
self.assertIn('DNS=2600::ee', output)
self.assertIn('NTP=2600::ff', output)
# Check manager state file
print('## manager state file')
output = read_manager_state_file()
print(output)
self.assertRegex(output, 'DNS=.*2600::ee')
self.assertRegex(output, 'NTP=.*2600::ff')
print('## dnsmasq log')
output = read_dnsmasq_log_file()
print(output)
self.assertIn('DHCPINFORMATION-REQUEST(veth-peer)', output)
self.assertNotIn('DHCPSOLICIT(veth-peer)', output)
self.assertNotIn('DHCPADVERTISE(veth-peer)', output)
self.assertNotIn('DHCPREQUEST(veth-peer)', output)
self.assertNotIn('DHCPREPLY(veth-peer)', output)
# Check json format
check_json(networkctl_json('veth99'))
# solicit mode
stop_dnsmasq()
start_dnsmasq(
'--dhcp-option=108,00:00:02:00',
'--dhcp-option=option6:dns-server,[2600::ee]',
'--dhcp-option=option6:ntp-server,[2600::ff]',
)
networkctl_reconfigure('veth99')
self.wait_online('veth99:routable', 'veth-peer:routable')
# checking address
output = check_output('ip address show dev veth99 scope global')
print(output)
self.assertRegex(output, r'inet6 2600::[0-9a-f:]*/128 scope global dynamic noprefixroute')
self.assertNotIn('192.168.5', output)
# checking semi-static route
output = check_output('ip -6 route list dev veth99 2001:1234:5:9fff:ff:ff:ff:ff')
print(output)
self.assertRegex(output, 'via fe80::1034:56ff:fe78:9abd')
# Confirm that ipv6 token is not set in the kernel
output = check_output('ip token show dev veth99')
print(output)
self.assertRegex(output, 'token :: dev veth99')
# Make manager and link state file updated
resolvectl('revert', 'veth99')
# Check link state file
print('## link state file')
output = read_link_state_file('veth99')
print(output)
self.assertIn('DNS=2600::ee', output)
self.assertIn('NTP=2600::ff', output)
# Check manager state file
print('## manager state file')
output = read_manager_state_file()
print(output)
self.assertRegex(output, 'DNS=.*2600::ee')
self.assertRegex(output, 'NTP=.*2600::ff')
print('## dnsmasq log')
output = read_dnsmasq_log_file()
print(output)
self.assertNotIn('DHCPINFORMATION-REQUEST(veth-peer)', output)
self.assertIn('DHCPSOLICIT(veth-peer)', output)
self.assertNotIn('DHCPADVERTISE(veth-peer)', output)
self.assertNotIn('DHCPREQUEST(veth-peer)', output)
self.assertIn('DHCPREPLY(veth-peer)', output)
self.assertIn('sent size: 0 option: 14 rapid-commit', output)
# Check json format
check_json(networkctl_json('veth99'))
# Testing without rapid commit support
with open(
os.path.join(network_unit_dir, '25-dhcp-client-ipv6-only.network'), mode='a', encoding='utf-8'
) as f:
f.write('\n[DHCPv6]\nRapidCommit=no\n')
stop_dnsmasq()
start_dnsmasq(
'--dhcp-option=108,00:00:02:00',
'--dhcp-option=option6:dns-server,[2600::ee]',
'--dhcp-option=option6:ntp-server,[2600::ff]',
)
networkctl_reload()
self.wait_online('veth99:routable', 'veth-peer:routable')
# checking address
output = check_output('ip address show dev veth99 scope global')
print(output)
self.assertRegex(output, r'inet6 2600::[0-9a-f:]*/128 scope global dynamic noprefixroute')
self.assertNotIn('192.168.5', output)
# checking semi-static route
output = check_output('ip -6 route list dev veth99 2001:1234:5:9fff:ff:ff:ff:ff')
print(output)
self.assertRegex(output, 'via fe80::1034:56ff:fe78:9abd')
# Make manager and link state file updated
resolvectl('revert', 'veth99')
# Check link state file
print('## link state file')
output = read_link_state_file('veth99')
print(output)
self.assertIn('DNS=2600::ee', output)
self.assertIn('NTP=2600::ff', output)
# Check manager state file
print('## manager state file')
output = read_manager_state_file()
print(output)
self.assertRegex(output, 'DNS=.*2600::ee')
self.assertRegex(output, 'NTP=.*2600::ff')
print('## dnsmasq log')
output = read_dnsmasq_log_file()
print(output)
self.assertNotIn('DHCPINFORMATION-REQUEST(veth-peer)', output)
self.assertIn('DHCPSOLICIT(veth-peer)', output)
self.assertIn('DHCPADVERTISE(veth-peer)', output)
self.assertIn('DHCPREQUEST(veth-peer)', output)
self.assertIn('DHCPREPLY(veth-peer)', output)
self.assertNotIn('rapid-commit', output)
# Check json format
check_json(networkctl_json('veth99'))
def test_dhcp_client_ipv6_dbus_status(self):
copy_network_unit(
'25-veth.netdev',
'25-dhcp-server-veth-peer.network',
'25-dhcp-client-ipv6-only.network',
)
start_networkd()
self.wait_online('veth-peer:carrier')
# Note that at this point the DHCPv6 client has not been started because no RA (with managed
# bit set) has yet been received and the configuration does not include WithoutRA=true
state = get_dhcp6_client_state('veth99')
print(f'DHCPv6 client state = {state}')
self.assertEqual(state, 'stopped')
state = get_dhcp4_client_state('veth99')
print(f'DHCPv4 client state = {state}')
self.assertEqual(state, 'selecting')
start_dnsmasq('--dhcp-option=108,00:00:02:00')
self.wait_online('veth99:routable', 'veth-peer:routable')
state = get_dhcp6_client_state('veth99')
print(f'DHCPv6 client state = {state}')
self.assertEqual(state, 'bound')
# DHCPv4 client will stop after an DHCPOFFER message received, so we need to wait for a while.
for _ in range(100):
state = get_dhcp4_client_state('veth99')
if state == 'stopped':
break
time.sleep(0.2)