2014-11-20 10:37:48 +00:00
|
|
|
|
{ config, lib, pkgs, utils, ... }:
|
|
|
|
|
|
|
|
|
|
with utils;
|
2014-12-11 00:28:45 +00:00
|
|
|
|
with lib;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
cfg = config.networking;
|
|
|
|
|
interfaces = attrValues cfg.interfaces;
|
|
|
|
|
|
2017-09-24 15:54:48 +01:00
|
|
|
|
slaves = concatMap (i: i.interfaces) (attrValues cfg.bonds)
|
|
|
|
|
++ concatMap (i: i.interfaces) (attrValues cfg.bridges)
|
|
|
|
|
++ concatMap (i: i.interfaces) (attrValues cfg.vswitches)
|
|
|
|
|
++ concatMap (i: [i.interface]) (attrValues cfg.macvlans)
|
|
|
|
|
++ concatMap (i: [i.interface]) (attrValues cfg.vlans);
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
# We must escape interfaces due to the systemd interpretation
|
|
|
|
|
subsystemDevice = interface:
|
|
|
|
|
"sys-subsystem-net-devices-${escapeSystemdPath interface}.device";
|
|
|
|
|
|
|
|
|
|
interfaceIps = i:
|
2017-12-03 04:13:14 +00:00
|
|
|
|
i.ipv4.addresses
|
|
|
|
|
++ optionals cfg.enableIPv6 i.ipv6.addresses;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
destroyBond = i: ''
|
|
|
|
|
while true; do
|
|
|
|
|
UPDATED=1
|
|
|
|
|
SLAVES=$(ip link | grep 'master ${i}' | awk -F: '{print $2}')
|
|
|
|
|
for I in $SLAVES; do
|
|
|
|
|
UPDATED=0
|
|
|
|
|
ip link set "$I" nomaster
|
|
|
|
|
done
|
|
|
|
|
[ "$UPDATED" -eq "1" ] && break
|
|
|
|
|
done
|
2014-11-30 06:34:50 +00:00
|
|
|
|
ip link set "${i}" down 2>/dev/null || true
|
|
|
|
|
ip link del "${i}" 2>/dev/null || true
|
2014-11-25 09:53:37 +00:00
|
|
|
|
'';
|
|
|
|
|
|
2017-02-02 21:21:03 +00:00
|
|
|
|
# warn that these attributes are deprecated (2017-2-2)
|
|
|
|
|
# Should be removed in the release after next
|
|
|
|
|
bondDeprecation = rec {
|
|
|
|
|
deprecated = [ "lacp_rate" "miimon" "mode" "xmit_hash_policy" ];
|
|
|
|
|
filterDeprecated = bond: (filterAttrs (attrName: attr:
|
|
|
|
|
elem attrName deprecated && attr != null) bond);
|
|
|
|
|
};
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2017-02-02 21:21:03 +00:00
|
|
|
|
bondWarnings =
|
|
|
|
|
let oneBondWarnings = bondName: bond:
|
|
|
|
|
mapAttrsToList (bondText bondName) (bondDeprecation.filterDeprecated bond);
|
|
|
|
|
bondText = bondName: optName: _:
|
|
|
|
|
"${bondName}.${optName} is deprecated, use ${bondName}.driverOptions";
|
|
|
|
|
in {
|
|
|
|
|
warnings = flatten (mapAttrsToList oneBondWarnings cfg.bonds);
|
|
|
|
|
};
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2017-02-02 21:21:03 +00:00
|
|
|
|
normalConfig = {
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
|
|
|
|
systemd.services =
|
|
|
|
|
let
|
|
|
|
|
|
2016-09-30 09:45:48 +01:00
|
|
|
|
deviceDependency = dev:
|
2017-02-09 22:08:48 +00:00
|
|
|
|
# Use systemd service if we manage device creation, else
|
|
|
|
|
# trust udev when not in a container
|
2017-02-18 03:54:15 +00:00
|
|
|
|
if (hasAttr dev (filterAttrs (k: v: v.virtual) cfg.interfaces)) ||
|
|
|
|
|
(hasAttr dev cfg.bridges) ||
|
2017-02-09 22:08:48 +00:00
|
|
|
|
(hasAttr dev cfg.bonds) ||
|
|
|
|
|
(hasAttr dev cfg.macvlans) ||
|
|
|
|
|
(hasAttr dev cfg.sits) ||
|
|
|
|
|
(hasAttr dev cfg.vlans) ||
|
2018-04-03 21:00:12 +01:00
|
|
|
|
(hasAttr dev cfg.vswitches)
|
2017-02-09 22:08:48 +00:00
|
|
|
|
then [ "${dev}-netdev.service" ]
|
2017-08-30 06:59:27 +01:00
|
|
|
|
else optional (dev != null && dev != "lo" && !config.boot.isContainer) (subsystemDevice dev);
|
2016-09-30 09:45:48 +01:00
|
|
|
|
|
2017-09-18 13:46:37 +01:00
|
|
|
|
hasDefaultGatewaySet = (cfg.defaultGateway != null && cfg.defaultGateway.address != "")
|
2018-02-19 20:30:49 +00:00
|
|
|
|
|| (cfg.enableIPv6 && cfg.defaultGateway6 != null && cfg.defaultGateway6.address != "");
|
2017-09-18 13:46:37 +01:00
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
networkLocalCommands = {
|
|
|
|
|
after = [ "network-setup.service" ];
|
|
|
|
|
bindsTo = [ "network-setup.service" ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
networkSetup =
|
|
|
|
|
{ description = "Networking Setup";
|
|
|
|
|
|
2016-09-09 04:25:56 +01:00
|
|
|
|
after = [ "network-pre.target" "systemd-udevd.service" "systemd-sysctl.service" ];
|
|
|
|
|
before = [ "network.target" "shutdown.target" ];
|
|
|
|
|
wants = [ "network.target" ];
|
2018-09-23 21:55:59 +01:00
|
|
|
|
# exclude bridges from the partOf relationship to fix container networking bug #47210
|
|
|
|
|
partOf = map (i: "network-addresses-${i.name}.service") (filter (i: !(hasAttr i.name cfg.bridges)) interfaces);
|
2016-09-09 04:25:56 +01:00
|
|
|
|
conflicts = [ "shutdown.target" ];
|
2017-09-18 13:46:37 +01:00
|
|
|
|
wantedBy = [ "multi-user.target" ] ++ optional hasDefaultGatewaySet "network-online.target";
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
|
|
|
|
unitConfig.ConditionCapability = "CAP_NET_ADMIN";
|
|
|
|
|
|
|
|
|
|
path = [ pkgs.iproute ];
|
|
|
|
|
|
2016-09-09 04:25:56 +01:00
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
RemainAfterExit = true;
|
|
|
|
|
};
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2016-10-02 13:00:12 +01:00
|
|
|
|
unitConfig.DefaultDependencies = false;
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
script =
|
2014-12-01 09:18:26 +00:00
|
|
|
|
''
|
2019-03-09 11:42:14 +00:00
|
|
|
|
${optionalString (!config.environment.etc?"resolv.conf") ''
|
|
|
|
|
# Set the static DNS configuration, if given.
|
|
|
|
|
${pkgs.openresolv}/sbin/resolvconf -m 1 -a static <<EOF
|
|
|
|
|
${optionalString (cfg.nameservers != [] && cfg.domain != null) ''
|
|
|
|
|
domain ${cfg.domain}
|
|
|
|
|
''}
|
|
|
|
|
${optionalString (cfg.search != []) ("search " + concatStringsSep " " cfg.search)}
|
|
|
|
|
${flip concatMapStrings cfg.nameservers (ns: ''
|
|
|
|
|
nameserver ${ns}
|
|
|
|
|
'')}
|
|
|
|
|
EOF
|
2014-11-20 10:37:48 +00:00
|
|
|
|
''}
|
2014-12-01 09:18:26 +00:00
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
# Set the default gateway.
|
2017-01-14 01:14:16 +00:00
|
|
|
|
${optionalString (cfg.defaultGateway != null && cfg.defaultGateway.address != "") ''
|
2017-09-01 05:24:37 +01:00
|
|
|
|
${optionalString (cfg.defaultGateway.interface != null) ''
|
|
|
|
|
ip route replace ${cfg.defaultGateway.address} dev ${cfg.defaultGateway.interface} ${optionalString (cfg.defaultGateway.metric != null)
|
|
|
|
|
"metric ${toString cfg.defaultGateway.metric}"
|
|
|
|
|
} proto static
|
|
|
|
|
''}
|
|
|
|
|
ip route replace default ${optionalString (cfg.defaultGateway.metric != null)
|
2017-02-09 22:09:07 +00:00
|
|
|
|
"metric ${toString cfg.defaultGateway.metric}"
|
|
|
|
|
} via "${cfg.defaultGateway.address}" ${
|
2014-11-20 10:37:48 +00:00
|
|
|
|
optionalString (cfg.defaultGatewayWindowSize != null)
|
2017-01-14 01:14:16 +00:00
|
|
|
|
"window ${toString cfg.defaultGatewayWindowSize}"} ${
|
|
|
|
|
optionalString (cfg.defaultGateway.interface != null)
|
2017-09-01 05:24:37 +01:00
|
|
|
|
"dev ${cfg.defaultGateway.interface}"} proto static
|
2014-11-20 10:37:48 +00:00
|
|
|
|
''}
|
2017-01-14 01:14:16 +00:00
|
|
|
|
${optionalString (cfg.defaultGateway6 != null && cfg.defaultGateway6.address != "") ''
|
2017-09-01 05:24:37 +01:00
|
|
|
|
${optionalString (cfg.defaultGateway6.interface != null) ''
|
|
|
|
|
ip -6 route replace ${cfg.defaultGateway6.address} dev ${cfg.defaultGateway6.interface} ${optionalString (cfg.defaultGateway6.metric != null)
|
|
|
|
|
"metric ${toString cfg.defaultGateway6.metric}"
|
|
|
|
|
} proto static
|
|
|
|
|
''}
|
|
|
|
|
ip -6 route replace default ${optionalString (cfg.defaultGateway6.metric != null)
|
2017-02-09 22:09:07 +00:00
|
|
|
|
"metric ${toString cfg.defaultGateway6.metric}"
|
|
|
|
|
} via "${cfg.defaultGateway6.address}" ${
|
2014-12-18 01:54:06 +00:00
|
|
|
|
optionalString (cfg.defaultGatewayWindowSize != null)
|
2017-01-14 01:14:16 +00:00
|
|
|
|
"window ${toString cfg.defaultGatewayWindowSize}"} ${
|
|
|
|
|
optionalString (cfg.defaultGateway6.interface != null)
|
2017-09-01 05:24:37 +01:00
|
|
|
|
"dev ${cfg.defaultGateway6.interface}"} proto static
|
2014-12-18 01:54:06 +00:00
|
|
|
|
''}
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-24 20:06:07 +00:00
|
|
|
|
# For each interface <foo>, create a job ‘network-addresses-<foo>.service"
|
|
|
|
|
# that performs static address configuration. It has a "wants"
|
2014-11-20 10:37:48 +00:00
|
|
|
|
# dependency on ‘<foo>.service’, which is supposed to create
|
|
|
|
|
# the interface and need not exist (i.e. for hardware
|
|
|
|
|
# interfaces). It has a binds-to dependency on the actual
|
|
|
|
|
# network device, so it only gets started after the interface
|
|
|
|
|
# has appeared, and it's stopped when the interface
|
|
|
|
|
# disappears.
|
2014-11-24 20:06:07 +00:00
|
|
|
|
configureAddrs = i:
|
2014-11-20 10:37:48 +00:00
|
|
|
|
let
|
|
|
|
|
ips = interfaceIps i;
|
|
|
|
|
in
|
2014-11-24 20:06:07 +00:00
|
|
|
|
nameValuePair "network-addresses-${i.name}"
|
2015-04-20 15:12:19 +01:00
|
|
|
|
{ description = "Address configuration of ${i.name}";
|
2017-09-24 14:55:02 +01:00
|
|
|
|
wantedBy = [
|
|
|
|
|
"network-setup.service"
|
|
|
|
|
"network-link-${i.name}.service"
|
|
|
|
|
"network.target"
|
|
|
|
|
];
|
2016-09-09 04:25:56 +01:00
|
|
|
|
# order before network-setup because the routes that are configured
|
|
|
|
|
# there may need ip addresses configured
|
|
|
|
|
before = [ "network-setup.service" ];
|
2016-09-30 09:45:48 +01:00
|
|
|
|
bindsTo = deviceDependency i.name;
|
|
|
|
|
after = [ "network-pre.target" ] ++ (deviceDependency i.name);
|
2014-11-20 10:37:48 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2017-04-04 13:18:49 +01:00
|
|
|
|
# Restart rather than stop+start this unit to prevent the
|
|
|
|
|
# network from dying during switch-to-configuration.
|
|
|
|
|
stopIfChanged = false;
|
2014-11-26 19:19:31 +00:00
|
|
|
|
path = [ pkgs.iproute ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
script =
|
|
|
|
|
''
|
2017-04-04 13:18:49 +01:00
|
|
|
|
state="/run/nixos/network/addresses/${i.name}"
|
2017-07-27 15:23:20 +01:00
|
|
|
|
mkdir -p $(dirname "$state")
|
2017-04-04 13:18:49 +01:00
|
|
|
|
|
2017-07-27 15:23:20 +01:00
|
|
|
|
${flip concatMapStrings ips (ip:
|
|
|
|
|
let
|
|
|
|
|
cidr = "${ip.address}/${toString ip.prefixLength}";
|
|
|
|
|
in
|
|
|
|
|
''
|
|
|
|
|
echo "${cidr}" >> $state
|
|
|
|
|
echo -n "adding address ${cidr}... "
|
|
|
|
|
if out=$(ip addr add "${cidr}" dev "${i.name}" 2>&1); then
|
|
|
|
|
echo "done"
|
|
|
|
|
elif ! echo "$out" | grep "File exists" >/dev/null 2>&1; then
|
2018-03-30 02:18:18 +01:00
|
|
|
|
echo "'ip addr add "${cidr}" dev "${i.name}"' failed: $out"
|
2017-07-27 15:23:20 +01:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
''
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
state="/run/nixos/network/routes/${i.name}"
|
2017-04-04 13:18:49 +01:00
|
|
|
|
mkdir -p $(dirname "$state")
|
|
|
|
|
|
2017-12-03 04:13:14 +00:00
|
|
|
|
${flip concatMapStrings (i.ipv4.routes ++ i.ipv6.routes) (route:
|
2017-07-27 15:23:20 +01:00
|
|
|
|
let
|
|
|
|
|
cidr = "${route.address}/${toString route.prefixLength}";
|
2017-07-28 15:45:58 +01:00
|
|
|
|
via = optionalString (route.via != null) ''via "${route.via}"'';
|
|
|
|
|
options = concatStrings (mapAttrsToList (name: val: "${name} ${val} ") route.options);
|
2017-07-27 15:23:20 +01:00
|
|
|
|
in
|
|
|
|
|
''
|
|
|
|
|
echo "${cidr}" >> $state
|
|
|
|
|
echo -n "adding route ${cidr}... "
|
2018-05-22 02:15:22 +01:00
|
|
|
|
if out=$(ip route add "${cidr}" ${options} ${via} dev "${i.name}" proto static 2>&1); then
|
2017-07-27 15:23:20 +01:00
|
|
|
|
echo "done"
|
|
|
|
|
elif ! echo "$out" | grep "File exists" >/dev/null 2>&1; then
|
2018-03-30 02:18:18 +01:00
|
|
|
|
echo "'ip route add "${cidr}" ${options} ${via} dev "${i.name}"' failed: $out"
|
2017-07-27 15:23:20 +01:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
''
|
|
|
|
|
)}
|
|
|
|
|
'';
|
2017-04-04 13:18:49 +01:00
|
|
|
|
preStop = ''
|
2017-07-27 15:23:20 +01:00
|
|
|
|
state="/run/nixos/network/routes/${i.name}"
|
|
|
|
|
while read cidr; do
|
|
|
|
|
echo -n "deleting route $cidr... "
|
|
|
|
|
ip route del "$cidr" dev "${i.name}" >/dev/null 2>&1 && echo "done" || echo "failed"
|
|
|
|
|
done < "$state"
|
|
|
|
|
rm -f "$state"
|
|
|
|
|
|
2017-04-04 13:18:49 +01:00
|
|
|
|
state="/run/nixos/network/addresses/${i.name}"
|
2017-07-27 15:23:20 +01:00
|
|
|
|
while read cidr; do
|
|
|
|
|
echo -n "deleting address $cidr... "
|
|
|
|
|
ip addr del "$cidr" dev "${i.name}" >/dev/null 2>&1 && echo "done" || echo "failed"
|
2017-04-04 13:18:49 +01:00
|
|
|
|
done < "$state"
|
|
|
|
|
rm -f "$state"
|
|
|
|
|
'';
|
2014-11-20 10:37:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createTunDevice = i: nameValuePair "${i.name}-netdev"
|
|
|
|
|
{ description = "Virtual Network Interface ${i.name}";
|
2016-09-09 04:25:56 +01:00
|
|
|
|
bindsTo = [ "dev-net-tun.device" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "dev-net-tun.device" "network-pre.target" ];
|
2016-09-09 04:25:56 +01:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice i.name) ];
|
|
|
|
|
partOf = [ "network-setup.service" ];
|
2017-09-24 15:01:07 +01:00
|
|
|
|
before = [ "network-setup.service" ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
path = [ pkgs.iproute ];
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
Type = "oneshot";
|
|
|
|
|
RemainAfterExit = true;
|
|
|
|
|
};
|
|
|
|
|
script = ''
|
2018-01-31 04:51:09 +00:00
|
|
|
|
ip tuntap add dev "${i.name}" mode "${i.virtualType}" user "${i.virtualOwner}"
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
2017-02-09 22:08:48 +00:00
|
|
|
|
ip link del ${i.name} || true
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createBridgeDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 13:52:46 +01:00
|
|
|
|
deps = concatLists (map deviceDependency v.interfaces);
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "Bridge Interface ${n}";
|
2016-09-09 04:25:56 +01:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2015-01-07 22:34:15 +00:00
|
|
|
|
bindsTo = deps ++ optional v.rstp "mstpd.service";
|
2016-09-09 04:25:56 +01:00
|
|
|
|
partOf = [ "network-setup.service" ] ++ optional v.rstp "mstpd.service";
|
2016-09-07 11:09:20 +01:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps ++ optional v.rstp "mstpd.service"
|
2014-12-02 01:19:06 +00:00
|
|
|
|
++ concatMap (i: [ "network-addresses-${i}.service" "network-link-${i}.service" ]) v.interfaces;
|
2017-09-24 15:01:07 +01:00
|
|
|
|
before = [ "network-setup.service" ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2015-01-14 00:10:39 +00:00
|
|
|
|
path = [ pkgs.iproute ];
|
2014-11-25 09:53:37 +00:00
|
|
|
|
script = ''
|
|
|
|
|
# Remove Dead Interfaces
|
|
|
|
|
echo "Removing old bridge ${n}..."
|
|
|
|
|
ip link show "${n}" >/dev/null 2>&1 && ip link del "${n}"
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
echo "Adding bridge ${n}..."
|
|
|
|
|
ip link add name "${n}" type bridge
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
# Enslave child interfaces
|
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
|
|
|
|
ip link set "${i}" master "${n}"
|
|
|
|
|
ip link set "${i}" up
|
|
|
|
|
'')}
|
2017-02-11 15:46:55 +00:00
|
|
|
|
# Save list of enslaved interfaces
|
|
|
|
|
echo "${flip concatMapStrings v.interfaces (i: ''
|
|
|
|
|
${i}
|
|
|
|
|
'')}" > /run/${n}.interfaces
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2017-07-27 06:49:18 +01:00
|
|
|
|
${optionalString config.virtualisation.libvirtd.enable ''
|
|
|
|
|
# Enslave dynamically added interfaces which may be lost on nixos-rebuild
|
2017-08-01 11:30:58 +01:00
|
|
|
|
for uri in qemu:///system lxc:///; do
|
|
|
|
|
for dom in $(${pkgs.libvirt}/bin/virsh -c $uri list --name); do
|
|
|
|
|
${pkgs.libvirt}/bin/virsh -c $uri dumpxml "$dom" | \
|
2017-08-21 17:47:59 +01:00
|
|
|
|
${pkgs.xmlstarlet}/bin/xmlstarlet sel -t -m "//domain/devices/interface[@type='bridge'][source/@bridge='${n}'][target/@dev]" -v "concat('ip link set ',target/@dev,' master ',source/@bridge,';')" | \
|
2017-08-01 11:30:58 +01:00
|
|
|
|
${pkgs.bash}/bin/bash
|
|
|
|
|
done
|
2017-07-27 06:49:18 +01:00
|
|
|
|
done
|
|
|
|
|
''}
|
|
|
|
|
|
2015-01-14 00:10:39 +00:00
|
|
|
|
# Enable stp on the interface
|
2015-01-07 22:34:15 +00:00
|
|
|
|
${optionalString v.rstp ''
|
2015-01-14 00:10:39 +00:00
|
|
|
|
echo 2 >/sys/class/net/${n}/bridge/stp_state
|
2015-01-07 22:34:15 +00:00
|
|
|
|
''}
|
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
ip link set "${n}" up
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
ip link set "${n}" down || true
|
|
|
|
|
ip link del "${n}" || true
|
2017-02-11 15:46:55 +00:00
|
|
|
|
rm -f /run/${n}.interfaces
|
2014-11-25 09:53:37 +00:00
|
|
|
|
'';
|
2017-02-11 15:46:55 +00:00
|
|
|
|
reload = ''
|
|
|
|
|
# Un-enslave child interfaces (old list of interfaces)
|
|
|
|
|
for interface in `cat /run/${n}.interfaces`; do
|
|
|
|
|
ip link set "$interface" nomaster up
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Enslave child interfaces (new list of interfaces)
|
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
|
|
|
|
ip link set "${i}" master "${n}"
|
|
|
|
|
ip link set "${i}" up
|
|
|
|
|
'')}
|
|
|
|
|
# Save list of enslaved interfaces
|
|
|
|
|
echo "${flip concatMapStrings v.interfaces (i: ''
|
|
|
|
|
${i}
|
|
|
|
|
'')}" > /run/${n}.interfaces
|
|
|
|
|
|
|
|
|
|
# (Un-)set stp on the bridge
|
|
|
|
|
echo ${if v.rstp then "2" else "0"} > /sys/class/net/${n}/bridge/stp_state
|
|
|
|
|
'';
|
|
|
|
|
reloadIfChanged = true;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
});
|
|
|
|
|
|
2015-09-22 14:49:17 +01:00
|
|
|
|
createVswitchDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 13:52:46 +01:00
|
|
|
|
deps = concatLists (map deviceDependency v.interfaces);
|
2015-09-22 14:49:17 +01:00
|
|
|
|
ofRules = pkgs.writeText "vswitch-${n}-openFlowRules" v.openFlowRules;
|
|
|
|
|
in
|
|
|
|
|
{ description = "Open vSwitch Interface ${n}";
|
2016-09-09 04:25:56 +01:00
|
|
|
|
wantedBy = [ "network-setup.service" "vswitchd.service" ] ++ deps;
|
2015-10-05 12:23:58 +01:00
|
|
|
|
bindsTo = [ "vswitchd.service" (subsystemDevice n) ] ++ deps;
|
2016-09-09 04:25:56 +01:00
|
|
|
|
partOf = [ "network-setup.service" "vswitchd.service" ];
|
2015-10-05 12:23:58 +01:00
|
|
|
|
after = [ "network-pre.target" "vswitchd.service" ] ++ deps;
|
2016-09-09 04:25:56 +01:00
|
|
|
|
before = [ "network-setup.service" ];
|
2015-09-22 14:49:17 +01:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
|
|
|
|
path = [ pkgs.iproute config.virtualisation.vswitch.package ];
|
|
|
|
|
script = ''
|
|
|
|
|
echo "Removing old Open vSwitch ${n}..."
|
|
|
|
|
ovs-vsctl --if-exists del-br ${n}
|
|
|
|
|
|
|
|
|
|
echo "Adding Open vSwitch ${n}..."
|
|
|
|
|
ovs-vsctl -- add-br ${n} ${concatMapStrings (i: " -- add-port ${n} ${i}") v.interfaces} \
|
|
|
|
|
${concatMapStrings (x: " -- set-controller ${n} " + x) v.controllers} \
|
|
|
|
|
${concatMapStrings (x: " -- " + x) (splitString "\n" v.extraOvsctlCmds)}
|
|
|
|
|
|
|
|
|
|
echo "Adding OpenFlow rules for Open vSwitch ${n}..."
|
|
|
|
|
ovs-ofctl add-flows ${n} ${ofRules}
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
ip link set ${n} down || true
|
|
|
|
|
ovs-ofctl del-flows ${n} || true
|
|
|
|
|
ovs-vsctl --if-exists del-br ${n}
|
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
createBondDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 13:52:46 +01:00
|
|
|
|
deps = concatLists (map deviceDependency v.interfaces);
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "Bond Interface ${n}";
|
2016-09-09 04:25:56 +01:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 04:25:56 +01:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps
|
|
|
|
|
++ concatMap (i: [ "network-addresses-${i}.service" "network-link-${i}.service" ]) v.interfaces;
|
2017-09-24 15:01:07 +01:00
|
|
|
|
before = [ "network-setup.service" ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2014-11-28 06:53:50 +00:00
|
|
|
|
path = [ pkgs.iproute pkgs.gawk ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
script = ''
|
2014-11-25 09:53:37 +00:00
|
|
|
|
echo "Destroying old bond ${n}..."
|
|
|
|
|
${destroyBond n}
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
echo "Creating new bond ${n}..."
|
|
|
|
|
ip link add name "${n}" type bond \
|
2017-02-02 21:21:03 +00:00
|
|
|
|
${let opts = (mapAttrs (const toString)
|
|
|
|
|
(bondDeprecation.filterDeprecated v))
|
|
|
|
|
// v.driverOptions;
|
|
|
|
|
in concatStringsSep "\n"
|
|
|
|
|
(mapAttrsToList (set: val: " ${set} ${val} \\") opts)}
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
2014-11-25 09:53:37 +00:00
|
|
|
|
# !!! There must be a better way to wait for the interface
|
|
|
|
|
while [ ! -d "/sys/class/net/${n}" ]; do sleep 0.1; done;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
|
|
|
|
|
# Bring up the bond and enslave the specified interfaces
|
|
|
|
|
ip link set "${n}" up
|
|
|
|
|
${flip concatMapStrings v.interfaces (i: ''
|
2014-11-30 06:34:50 +00:00
|
|
|
|
ip link set "${i}" down
|
2014-11-25 09:53:37 +00:00
|
|
|
|
ip link set "${i}" master "${n}"
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'')}
|
|
|
|
|
'';
|
2014-11-25 09:53:37 +00:00
|
|
|
|
postStop = destroyBond n;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
});
|
|
|
|
|
|
2014-11-26 23:42:32 +00:00
|
|
|
|
createMacvlanDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 09:45:48 +01:00
|
|
|
|
deps = deviceDependency v.interface;
|
2014-11-26 23:42:32 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "Vlan Interface ${n}";
|
2016-09-09 04:25:56 +01:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-26 23:42:32 +00:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 04:25:56 +01:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
2017-09-24 15:01:07 +01:00
|
|
|
|
before = [ "network-setup.service" ];
|
2014-11-26 23:42:32 +00:00
|
|
|
|
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 = ''
|
2017-02-09 22:08:48 +00:00
|
|
|
|
ip link delete "${n}" || true
|
2014-11-26 23:42:32 +00:00
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
createSitDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-10-11 22:18:02 +01:00
|
|
|
|
deps = deviceDependency v.dev;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "6-to-4 Tunnel Interface ${n}";
|
2016-09-09 04:25:56 +01:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 04:25:56 +01:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
2017-09-24 15:01:07 +01:00
|
|
|
|
before = [ "network-setup.service" ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
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 name "${n}" type sit \
|
|
|
|
|
${optionalString (v.remote != null) "remote \"${v.remote}\""} \
|
|
|
|
|
${optionalString (v.local != null) "local \"${v.local}\""} \
|
|
|
|
|
${optionalString (v.ttl != null) "ttl ${toString v.ttl}"} \
|
|
|
|
|
${optionalString (v.dev != null) "dev \"${v.dev}\""}
|
|
|
|
|
ip link set "${n}" up
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
2017-02-09 22:08:48 +00:00
|
|
|
|
ip link delete "${n}" || true
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
createVlanDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
2016-09-30 09:45:48 +01:00
|
|
|
|
deps = deviceDependency v.interface;
|
2014-11-20 10:37:48 +00:00
|
|
|
|
in
|
|
|
|
|
{ description = "Vlan Interface ${n}";
|
2016-09-09 04:25:56 +01:00
|
|
|
|
wantedBy = [ "network-setup.service" (subsystemDevice n) ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
bindsTo = deps;
|
2016-09-09 04:25:56 +01:00
|
|
|
|
partOf = [ "network-setup.service" ];
|
2014-12-02 01:19:06 +00:00
|
|
|
|
after = [ "network-pre.target" ] ++ deps;
|
2017-09-24 15:01:07 +01:00
|
|
|
|
before = [ "network-setup.service" ];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
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 vlan id "${toString v.id}"
|
2018-08-13 21:38:32 +01:00
|
|
|
|
|
|
|
|
|
# We try to bring up the logical VLAN interface. If the master
|
|
|
|
|
# interface the logical interface is dependent upon is not up yet we will
|
|
|
|
|
# fail to immediately bring up the logical interface. The resulting logical
|
|
|
|
|
# interface will brought up later when the master interface is up.
|
2018-08-02 11:10:10 +01:00
|
|
|
|
ip link set "${n}" up || true
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
2017-02-09 22:08:48 +00:00
|
|
|
|
ip link delete "${n}" || true
|
2014-11-20 10:37:48 +00:00
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
in listToAttrs (
|
2014-11-24 20:06:07 +00:00
|
|
|
|
map configureAddrs interfaces ++
|
2014-11-20 10:37:48 +00:00
|
|
|
|
map createTunDevice (filter (i: i.virtual) interfaces))
|
|
|
|
|
// mapAttrs' createBridgeDevice cfg.bridges
|
2015-09-22 14:49:17 +01:00
|
|
|
|
// mapAttrs' createVswitchDevice cfg.vswitches
|
2014-11-20 10:37:48 +00:00
|
|
|
|
// mapAttrs' createBondDevice cfg.bonds
|
2014-11-26 23:42:32 +00:00
|
|
|
|
// mapAttrs' createMacvlanDevice cfg.macvlans
|
2014-11-20 10:37:48 +00:00
|
|
|
|
// mapAttrs' createSitDevice cfg.sits
|
|
|
|
|
// mapAttrs' createVlanDevice cfg.vlans
|
|
|
|
|
// {
|
|
|
|
|
"network-setup" = networkSetup;
|
|
|
|
|
"network-local-commands" = networkLocalCommands;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
services.udev.extraRules =
|
|
|
|
|
''
|
|
|
|
|
KERNEL=="tun", TAG+="systemd"
|
|
|
|
|
'';
|
|
|
|
|
|
2017-02-02 21:21:03 +00:00
|
|
|
|
|
2014-11-20 10:37:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-02-02 21:21:03 +00:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
config = mkMerge [
|
|
|
|
|
bondWarnings
|
|
|
|
|
(mkIf (!cfg.useNetworkd) normalConfig)
|
2017-09-24 15:54:48 +01:00
|
|
|
|
{ # Ensure slave interfaces are brought up
|
|
|
|
|
networking.interfaces = genAttrs slaves (i: {});
|
|
|
|
|
}
|
2017-02-02 21:21:03 +00:00
|
|
|
|
];
|
2014-11-20 10:37:48 +00:00
|
|
|
|
}
|