1 import ./make-test-python.nix ({ pkgs, lib, ... }: let
2 inherit (import ./ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey;
5 virtualisation.vlans = [ vlan ];
14 networks."10-eth${toString vlan}" = {
15 matchConfig.Name = "eth${toString vlan}";
16 linkConfig.RequiredForOnline = "no";
18 Address = "192.168.${toString vlan}.${toString id}/24";
25 name = "systemd-networkd-vrf";
26 meta.maintainers = with lib.maintainers; [ ma27 ];
29 client = { pkgs, ... }: {
30 virtualisation.vlans = [ 1 2 ];
35 firewall.checkReversePath = "loose";
58 networks."10-vrf1" = {
59 matchConfig.Name = "vrf1";
60 networkConfig.IPForward = "yes";
62 { routeConfig = { Destination = "192.168.1.2"; Metric = 100; }; }
65 networks."10-vrf2" = {
66 matchConfig.Name = "vrf2";
67 networkConfig.IPForward = "yes";
69 { routeConfig = { Destination = "192.168.2.3"; Metric = 100; }; }
73 networks."10-eth1" = {
74 matchConfig.Name = "eth1";
75 linkConfig.RequiredForOnline = "no";
78 Address = "192.168.1.1/24";
82 networks."10-eth2" = {
83 matchConfig.Name = "eth2";
84 linkConfig.RequiredForOnline = "no";
87 Address = "192.168.2.1/24";
97 services.openssh.enable = true;
98 users.users.root.openssh.authorizedKeys.keys = [ snakeOilPublicKey ];
109 def compare(raw_json, to_compare):
110 data = json.loads(raw_json)
111 assert len(raw_json) >= len(to_compare)
112 for i, row in enumerate(to_compare):
114 assert len(row.keys()) > 0
115 for key, value in row.items():
116 assert value == actual[key], f"""
117 In entry {i}, value {key}: got: {actual[key]}, expected {value}
123 client.wait_for_unit("network.target")
124 node1.wait_for_unit("network.target")
125 node2.wait_for_unit("network.target")
126 node3.wait_for_unit("network.target")
128 # Check that networkd properly configures the main routing table
129 # and the routing tables for the VRF.
130 with subtest("check vrf routing tables"):
132 client.succeed("ip --json -4 route list"),
134 {"dst": "192.168.1.2", "dev": "vrf1", "metric": 100},
135 {"dst": "192.168.2.3", "dev": "vrf2", "metric": 100}
139 client.succeed("ip --json -4 route list table 23"),
141 {"dst": "192.168.1.0/24", "dev": "eth1", "prefsrc": "192.168.1.1"},
142 {"type": "local", "dst": "192.168.1.1", "dev": "eth1", "prefsrc": "192.168.1.1"},
143 {"type": "broadcast", "dev": "eth1", "prefsrc": "192.168.1.1", "dst": "192.168.1.255"}
147 client.succeed("ip --json -4 route list table 42"),
149 {"dst": "192.168.2.0/24", "dev": "eth2", "prefsrc": "192.168.2.1"},
150 {"type": "local", "dst": "192.168.2.1", "dev": "eth2", "prefsrc": "192.168.2.1"},
151 {"type": "broadcast", "dev": "eth2", "prefsrc": "192.168.2.1", "dst": "192.168.2.255"}
155 # Ensure that other nodes are reachable via ICMP through the VRF.
156 with subtest("icmp through vrf works"):
157 client.succeed("ping -c5 192.168.1.2")
158 client.succeed("ping -c5 192.168.2.3")
160 # Test whether TCP through a VRF IP is possible.
161 with subtest("tcp traffic through vrf works"):
162 node1.wait_for_open_port(22)
164 "cat ${snakeOilPrivateKey} > privkey.snakeoil"
166 client.succeed("chmod 600 privkey.snakeoil")
168 "ulimit -l 2048; ip vrf exec vrf1 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil root@192.168.1.2 true"
171 # Only configured routes through the VRF from the main routing table should
172 # work. Additional IPs are only reachable when binding to the vrf interface.
173 with subtest("only routes from main routing table work by default"):
174 client.fail("ping -c5 192.168.2.4")
175 client.succeed("ping -I vrf2 -c5 192.168.2.4")