1# In contrast to systemd-networkd-dhcpserver, this test configures
2# the router with a static DHCP lease for the client's MAC address.
3{ lib, ... }:
4{
5 name = "systemd-networkd-dhcpserver-static-leases";
6 meta = with lib.maintainers; {
7 maintainers = [ veehaitch ];
8 };
9 nodes = {
10 router = {
11 virtualisation.vlans = [ 1 ];
12 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
13 networking = {
14 useNetworkd = true;
15 useDHCP = false;
16 firewall.enable = false;
17 };
18 systemd.network = {
19 networks = {
20 # systemd-networkd will load the first network unit file
21 # that matches, ordered lexiographically by filename.
22 # /etc/systemd/network/{40-eth1,99-main}.network already
23 # exists. This network unit must be loaded for the test,
24 # however, hence why this network is named such.
25 "01-eth1" = {
26 name = "eth1";
27 networkConfig = {
28 DHCPServer = true;
29 Address = "10.0.0.1/24";
30 };
31 dhcpServerStaticLeases = [
32 {
33 MACAddress = "02:de:ad:be:ef:01";
34 Address = "10.0.0.10";
35 }
36 ];
37 };
38 };
39 };
40 };
41
42 client = {
43 virtualisation.vlans = [ 1 ];
44 systemd.services.systemd-networkd.environment.SYSTEMD_LOG_LEVEL = "debug";
45 systemd.network = {
46 enable = true;
47 links."10-eth1" = {
48 matchConfig.OriginalName = "eth1";
49 linkConfig.MACAddress = "02:de:ad:be:ef:01";
50 };
51 networks."40-eth1" = {
52 matchConfig.Name = "eth1";
53 networkConfig = {
54 DHCP = "ipv4";
55 IPv6AcceptRA = false;
56 };
57 # This setting is important to have the router assign the
58 # configured lease based on the client's MAC address. Also see:
59 # https://github.com/systemd/systemd/issues/21368#issuecomment-982193546
60 dhcpV4Config.ClientIdentifier = "mac";
61 linkConfig.RequiredForOnline = "routable";
62 };
63 };
64 networking = {
65 useDHCP = false;
66 firewall.enable = false;
67 interfaces.eth1 = lib.mkForce { };
68 };
69 };
70 };
71 testScript = ''
72 start_all()
73
74 with subtest("check router network configuration"):
75 router.systemctl("start systemd-networkd-wait-online.service")
76 router.wait_for_unit("systemd-networkd-wait-online.service")
77 eth1_status = router.succeed("networkctl status eth1")
78 assert "Network File: /etc/systemd/network/01-eth1.network" in eth1_status, \
79 "The router interface eth1 is not using the expected network file"
80 assert "10.0.0.1" in eth1_status, "Did not find expected router IPv4"
81
82 with subtest("check client network configuration"):
83 client.systemctl("start systemd-networkd-wait-online.service")
84 client.wait_for_unit("systemd-networkd-wait-online.service")
85 eth1_status = client.succeed("networkctl status eth1")
86 assert "Network File: /etc/systemd/network/40-eth1.network" in eth1_status, \
87 "The client interface eth1 is not using the expected network file"
88 assert "10.0.0.10" in eth1_status, "Did not find expected client IPv4"
89
90 with subtest("router and client can reach each other"):
91 client.wait_until_succeeds("ping -c 5 10.0.0.1")
92 router.wait_until_succeeds("ping -c 5 10.0.0.10")
93 '';
94}