Skip to content

Commit d25949a

Browse files
committed
nixos-modules: move tap/macvtap-up/down from host to microvm
1 parent 1547160 commit d25949a

File tree

5 files changed

+99
-59
lines changed

5 files changed

+99
-59
lines changed

lib/runner.nix

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
let
77
inherit (pkgs) lib;
88

9-
inherit (microvmConfig) virtiofsdScripts;
9+
inherit (microvmConfig) virtiofsdScripts tapScripts macvtapScripts;
1010

1111
inherit (import ./. { inherit lib; }) createVolumesScript makeMacvtap;
1212
inherit (makeMacvtap {
@@ -58,6 +58,7 @@ pkgs.buildPackages.runCommand "microvm-${microvmConfig.hypervisor}-${microvmConf
5858
passthru = {
5959
inherit canShutdown supportsNotifySocket tapMultiQueue;
6060
inherit (microvmConfig) hypervisor;
61+
inherit (hypervisorConfig) tapMultiQueue;
6162
};
6263
} ''
6364
mkdir -p $out/bin
@@ -79,6 +80,18 @@ pkgs.buildPackages.runCommand "microvm-${microvmConfig.hypervisor}-${microvmConf
7980
${lib.optionalString (virtiofsdScripts.shutdown != null) ''
8081
ln -s ${lib.getExe virtiofsdScripts.shutdown} $out/bin/virtiofsd-shutdown
8182
''}
83+
${lib.optionalString (tapScripts.up != null) ''
84+
ln -s ${lib.getExe tapScripts.up} $out/bin/tap-up
85+
''}
86+
${lib.optionalString (tapScripts.down != null) ''
87+
ln -s ${lib.getExe tapScripts.down} $out/bin/tap-down
88+
''}
89+
${lib.optionalString (macvtapScripts.up != null) ''
90+
ln -s ${lib.getExe macvtapScripts.up} $out/bin/macvtap-up
91+
''}
92+
${lib.optionalString (macvtapScripts.down != null) ''
93+
ln -s ${lib.getExe macvtapScripts.down} $out/bin/macvtap-down
94+
''}
8295
8396
mkdir -p $out/share/microvm
8497
ln -s ${toplevel} $out/share/microvm/system

nixos-modules/host/default.nix

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -130,82 +130,30 @@ in
130130
description = "Setup MicroVM '%i' TAP interfaces";
131131
before = [ "microvm@%i.service" ];
132132
partOf = [ "microvm@%i.service" ];
133-
unitConfig.ConditionPathExists = "${stateDir}/%i/current/share/microvm/tap-interfaces";
133+
unitConfig.ConditionPathExists = "${stateDir}/%i/current/bin/tap-up";
134134
restartIfChanged = false;
135135
serviceConfig = {
136136
Type = "oneshot";
137137
RemainAfterExit = true;
138-
ExecStop =
139-
let
140-
stopScript = pkgs.writeShellScript "stop-microvm-tap-interfaces" ''
141-
cd ${stateDir}/$1
142-
for id in $(cat current/share/microvm/tap-interfaces); do
143-
${pkgs.iproute2}/bin/ip tuntap del name $id mode tap
144-
done
145-
'';
146-
in "${stopScript} %i";
147138
SyslogIdentifier = "microvm-tap-interfaces@%i";
139+
ExecStart = "${stateDir}/%i/current/bin/tap-up";
140+
ExecStop = "${stateDir}/%i/booted/bin/tap-down";
148141
};
149-
# `ExecStart`
150-
scriptArgs = "%i";
151-
script = ''
152-
cd ${stateDir}/$1
153-
TAP_FLAGS="$(cat current/share/microvm/tap-flags)"
154-
155-
for id in $(cat current/share/microvm/tap-interfaces); do
156-
if [ -e /sys/class/net/$id ]; then
157-
${pkgs.iproute2}/bin/ip tuntap del name $id mode tap $TAP_FLAGS
158-
fi
159-
160-
${pkgs.iproute2}/bin/ip tuntap add name $id mode tap user ${user} $TAP_FLAGS
161-
${config.microvm.host.tapScript}
162-
done
163-
'';
164142
};
165143

166144
"microvm-macvtap-interfaces@" = {
167145
description = "Setup MicroVM '%i' MACVTAP interfaces";
168146
before = [ "microvm@%i.service" ];
169147
partOf = [ "microvm@%i.service" ];
170-
unitConfig.ConditionPathExists = "${stateDir}/%i/current/share/microvm/macvtap-interfaces";
148+
unitConfig.ConditionPathExists = "${stateDir}/%i/current/bin/macvtap-up";
171149
restartIfChanged = false;
172150
serviceConfig = {
173151
Type = "oneshot";
174152
RemainAfterExit = true;
175-
ExecStop =
176-
let
177-
stopScript = pkgs.writeShellScript "stop-microvm-tap-interfaces" ''
178-
cd ${stateDir}/$1
179-
cat current/share/microvm/macvtap-interfaces | while read -r line;do
180-
opts=( $line )
181-
id="''${opts[0]}"
182-
${pkgs.iproute2}/bin/ip link del name $id
183-
done
184-
'';
185-
in "${stopScript} %i";
186153
SyslogIdentifier = "microvm-macvtap-interfaces@%i";
154+
ExecStart = "${stateDir}/%i/current/bin/macvtap-up";
155+
ExecStop = "${stateDir}/%i/booted/bin/macvtap-down";
187156
};
188-
# `ExecStart`
189-
scriptArgs = "%i";
190-
script = ''
191-
cd ${stateDir}/$1
192-
i=0
193-
cat current/share/microvm/macvtap-interfaces | while read -r line;do
194-
opts=( $line )
195-
id="''${opts[0]}"
196-
mac="''${opts[1]}"
197-
link="''${opts[2]}"
198-
mode="''${opts[3]:+" mode ''${opts[3]}"}"
199-
if [ -e /sys/class/net/$id ]; then
200-
${pkgs.iproute2}/bin/ip link del name $id
201-
fi
202-
${pkgs.iproute2}/bin/ip link add link $link name $id address $mac type macvtap ''${mode[@]}
203-
${pkgs.iproute2}/bin/ip link set $id allmulticast on
204-
echo 1 > /proc/sys/net/ipv6/conf/$id/disable_ipv6
205-
${pkgs.iproute2}/bin/ip link set $id up
206-
${pkgs.coreutils-full}/bin/chown ${user}:${group} /dev/tap$(< /sys/class/net/$id/ifindex)
207-
done
208-
'';
209157
};
210158

211159

nixos-modules/microvm/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ in
1515
./asserts.nix
1616
./system.nix
1717
./mounts.nix
18+
./interfaces.nix
1819
./virtiofsd
1920
./graphics.nix
2021
./optimization.nix

nixos-modules/microvm/interfaces.nix

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{ config, lib, pkgs, ... }:
2+
3+
let
4+
inherit (config.networking) hostName;
5+
6+
interfacesByType = wantedType:
7+
builtins.filter ({ type, ... }: type == wantedType)
8+
config.microvm.interfaces;
9+
10+
tapInterfaces = interfacesByType "tap";
11+
macvtapInterfaces = interfacesByType "macvtap";
12+
13+
tapFlags = lib.concatStringsSep " " (
14+
[ "vnet_hdr" ] ++
15+
lib.optional config.microvm.declaredRunner.passthru.tapMultiQueue "multi_queue"
16+
);
17+
18+
# TODO: don't hardcode but obtain from host config
19+
user = "microvm";
20+
group = "kvm";
21+
in
22+
{
23+
microvm.tapScripts = lib.mkIf (tapInterfaces != []) {
24+
up = pkgs.writeShellScriptBin "microvm-${hostName}-tap-up" (''
25+
set -eou pipefail
26+
'' + lib.concatMapStrings ({ id, mac, ... }: ''
27+
if [ -e /sys/class/net/${id} ]; then
28+
${pkgs.iproute2}/bin/ip tuntap del name '${id}' mode tap ${tapFlags}
29+
fi
30+
31+
${pkgs.iproute2}/bin/ip tuntap add name '${id}' mode tap user '${user}' ${tapFlags}
32+
'') tapInterfaces);
33+
34+
down = pkgs.writeShellScriptBin "microvm-${hostName}-tap-down" (''
35+
set -ou pipefail
36+
'' + lib.concatMapStrings ({ id, mac, ... }: ''
37+
${pkgs.iproute2}/bin/ip tuntap del name '${id}' mode tap ${tapFlags}
38+
'') tapInterfaces);
39+
};
40+
41+
microvm.macvtapScripts = lib.mkIf (macvtapInterfaces != []) {
42+
up = pkgs.writeShellScriptBin "microvm-${hostName}-macvtap-up" (''
43+
set -eou pipefail
44+
'' + lib.concatMapStrings ({ id, mac, macvtap, ... }: ''
45+
if [ -e /sys/class/net/${id} ]; then
46+
${pkgs.iproute2}/bin/ip link del name '${id}'
47+
fi
48+
${pkgs.iproute2}/bin/ip link add link '${macvtap.link}' name '${id}' address '${mac}' type macvtap '${macvtap.mode}'
49+
${pkgs.iproute2}/bin/ip link set '${id}' allmulticast on
50+
echo 1 > "/proc/sys/net/ipv6/conf/${id}/disable_ipv6"
51+
${pkgs.iproute2}/bin/ip link set '${id}' up
52+
${pkgs.coreutils-full}/bin/chown '${user}:${group}' /dev/tap$(< "/sys/class/net/${id}/ifindex")
53+
'') macvtapInterfaces);
54+
55+
down = pkgs.writeShellScriptBin "microvm-${hostName}-macvtap-down" (''
56+
set -ou pipefail
57+
'' + lib.concatMapStrings ({ id, mac, ... }: ''
58+
${pkgs.iproute2}/bin/ip link del name '${id}'
59+
'') macvtapInterfaces);
60+
};
61+
}

nixos-modules/microvm/options.nix

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ let
66

77
hostName = config.networking.hostName or "$HOSTNAME";
88

9+
interfaceScriptOptions = type: with lib; {
10+
up = mkOption {
11+
description = "Generated script to create ${type} interfaces";
12+
type = with types; nullOr package;
13+
default = null;
14+
};
15+
down = mkOption {
16+
description = "Generated script to delete ${type} interfaces";
17+
type = with types; nullOr package;
18+
default = null;
19+
};
20+
};
21+
922
in
1023
{
1124
options.microvm = with lib; {
@@ -516,6 +529,7 @@ in
516529
defaultText = literalExpression ''"config.microvm.runner.''${config.microvm.hypervisor}"'';
517530
};
518531

532+
# TODO: just scripts
519533
virtiofsdScripts = {
520534
run = mkOption {
521535
description = "Generated script to run required virtiofsd instances";
@@ -535,6 +549,9 @@ in
535549
default = null;
536550
};
537551
};
552+
553+
tapScripts = interfaceScriptOptions "tap";
554+
macvtapScripts = interfaceScriptOptions "macvtap";
538555
};
539556

540557
config = lib.mkMerge [ {

0 commit comments

Comments
 (0)