From fbe9ac05d36380159a80bb98307246323aea422c Mon Sep 17 00:00:00 2001 From: "William A. Kennington III" Date: Wed, 26 Nov 2014 15:42:32 -0800 Subject: [PATCH] nixos/network-interfaces: Add maclvan support --- .../tasks/network-interfaces-scripted.nix | 24 ++++++++++++++ .../tasks/network-interfaces-systemd.nix | 12 +++++++ nixos/modules/tasks/network-interfaces.nix | 31 +++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/nixos/modules/tasks/network-interfaces-scripted.nix b/nixos/modules/tasks/network-interfaces-scripted.nix index 85bc697b0f4..cfa36ba2342 100644 --- a/nixos/modules/tasks/network-interfaces-scripted.nix +++ b/nixos/modules/tasks/network-interfaces-scripted.nix @@ -250,6 +250,29 @@ in postStop = destroyBond n; }); + createMacvlanDevice = n: v: nameValuePair "${n}-netdev" + (let + deps = [ (subsystemDevice v.interface) ]; + in + { description = "Vlan Interface ${n}"; + wantedBy = [ "network.target" (subsystemDevice n) ]; + bindsTo = deps; + after = deps; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + path = [ pkgs.iproute ]; + script = '' + # Remove Dead Interfaces + ip link show "${n}" >/dev/null 2>&1 && ip link delete "${n}" + ip link add link "${v.interface}" name "${n}" type macvlan \ + ${optionalString (v.mode != null) "mode ${v.mode}"} + ip link set "${n}" up + ''; + postStop = '' + ip link delete "${n}" + ''; + }); + createSitDevice = n: v: nameValuePair "${n}-netdev" (let deps = optional (v.dev != null) (subsystemDevice v.dev); @@ -303,6 +326,7 @@ in map createTunDevice (filter (i: i.virtual) interfaces)) // mapAttrs' createBridgeDevice cfg.bridges // mapAttrs' createBondDevice cfg.bonds + // mapAttrs' createMacvlanDevice cfg.macvlans // mapAttrs' createSitDevice cfg.sits // mapAttrs' createVlanDevice cfg.vlans // { diff --git a/nixos/modules/tasks/network-interfaces-systemd.nix b/nixos/modules/tasks/network-interfaces-systemd.nix index 690522a0fe7..334b24b5ad3 100644 --- a/nixos/modules/tasks/network-interfaces-systemd.nix +++ b/nixos/modules/tasks/network-interfaces-systemd.nix @@ -118,6 +118,18 @@ in networkConfig.Bond = name; } ]))); }))) + (mkMerge (flip mapAttrsToList cfg.macvlans (name: macvlan: { + netdevs."40-${name}" = { + netdevConfig = { + Name = name; + Kind = "macvlan"; + }; + macvlanConfig.Mode = macvlan.mode; + }; + networks."40-${macvlan.interface}" = (mkMerge [ (genericNetwork (mkOverride 999)) { + macvlan = [ name ]; + } ]); + }))) (mkMerge (flip mapAttrsToList cfg.sits (name: sit: { netdevs."40-${name}" = { netdevConfig = { diff --git a/nixos/modules/tasks/network-interfaces.nix b/nixos/modules/tasks/network-interfaces.nix index 080cb559cbb..f853d61cea4 100644 --- a/nixos/modules/tasks/network-interfaces.nix +++ b/nixos/modules/tasks/network-interfaces.nix @@ -428,6 +428,37 @@ in }; }; + networking.macvlans = mkOption { + type = types.attrsOf types.optionSet; + default = { }; + example = { + wan = { + interface = "enp2s0"; + mode = "vepa"; + }; + }; + description = '' + This option allows you to define macvlan interfaces which should + be automatically created. + ''; + options = { + + interface = mkOption { + example = "enp4s0"; + type = types.string; + description = "The interface the macvlan will transmit packets through."; + }; + + mode = mkOption { + default = null; + type = types.nullOr types.str; + example = "vepa"; + description = "The mode of the macvlan device."; + }; + + }; + }; + networking.sits = mkOption { type = types.attrsOf types.optionSet; default = { };