2018-12-29 11:16:51 +01:00
|
|
|
|
{ config, lib, utils, pkgs, ... }:
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
|
|
|
|
with utils;
|
2014-12-11 01:28:45 +01:00
|
|
|
|
with lib;
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
cfg = config.networking;
|
|
|
|
|
interfaces = attrValues cfg.interfaces;
|
|
|
|
|
|
|
|
|
|
interfaceIps = i:
|
2017-12-03 05:13:14 +01:00
|
|
|
|
i.ipv4.addresses
|
|
|
|
|
++ optionals cfg.enableIPv6 i.ipv6.addresses;
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
2021-11-04 00:19:54 +01:00
|
|
|
|
interfaceRoutes = i:
|
|
|
|
|
i.ipv4.routes
|
|
|
|
|
++ optionals cfg.enableIPv6 i.ipv6.routes;
|
|
|
|
|
|
2019-09-24 11:33:31 +02:00
|
|
|
|
dhcpStr = useDHCP: if useDHCP == true || useDHCP == null then "yes" else "no";
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
|
|
|
|
slaves =
|
|
|
|
|
concatLists (map (bond: bond.interfaces) (attrValues cfg.bonds))
|
|
|
|
|
++ concatLists (map (bridge: bridge.interfaces) (attrValues cfg.bridges))
|
|
|
|
|
++ map (sit: sit.dev) (attrValues cfg.sits)
|
2021-12-07 16:44:00 +01:00
|
|
|
|
++ map (gre: gre.dev) (attrValues cfg.greTunnels)
|
2018-12-29 11:16:51 +01:00
|
|
|
|
++ map (vlan: vlan.interface) (attrValues cfg.vlans)
|
|
|
|
|
# add dependency to physical or independently created vswitch member interface
|
|
|
|
|
# TODO: warn the user that any address configured on those interfaces will be useless
|
|
|
|
|
++ concatMap (i: attrNames (filterAttrs (_: config: config.type != "internal") i.interfaces)) (attrValues cfg.vswitches);
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
2023-10-03 01:19:48 +02:00
|
|
|
|
defaultGateways = mkMerge (forEach [ cfg.defaultGateway cfg.defaultGateway6 ] (gateway:
|
|
|
|
|
optionalAttrs (gateway != null && gateway.interface != null) {
|
|
|
|
|
networks."40-${gateway.interface}" = {
|
|
|
|
|
matchConfig.Name = gateway.interface;
|
|
|
|
|
routes = [{
|
2022-10-17 01:59:41 +02:00
|
|
|
|
routeConfig = {
|
2023-10-03 01:19:48 +02:00
|
|
|
|
Gateway = gateway.address;
|
|
|
|
|
} // optionalAttrs (gateway.metric != null) {
|
|
|
|
|
Metric = gateway.metric;
|
2022-10-17 01:59:41 +02:00
|
|
|
|
};
|
2023-10-03 01:19:48 +02:00
|
|
|
|
}];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
));
|
2022-10-17 01:59:41 +02:00
|
|
|
|
|
|
|
|
|
genericDhcpNetworks = initrd: mkIf cfg.useDHCP {
|
|
|
|
|
networks."99-ethernet-default-dhcp" = {
|
|
|
|
|
# We want to match physical ethernet interfaces as commonly
|
|
|
|
|
# found on laptops, desktops and servers, to provide an
|
|
|
|
|
# "out-of-the-box" setup that works for common cases. This
|
|
|
|
|
# heuristic isn't perfect (it could match interfaces with
|
|
|
|
|
# custom names that _happen_ to start with en or eth), but
|
|
|
|
|
# should be good enough to make the common case easy and can
|
|
|
|
|
# be overridden on a case-by-case basis using
|
|
|
|
|
# higher-priority networks or by disabling useDHCP.
|
|
|
|
|
|
|
|
|
|
# Type=ether matches veth interfaces as well, and this is
|
|
|
|
|
# more likely to result in interfaces being configured to
|
|
|
|
|
# use DHCP when they shouldn't.
|
|
|
|
|
|
|
|
|
|
matchConfig.Name = ["en*" "eth*"];
|
|
|
|
|
DHCP = "yes";
|
|
|
|
|
networkConfig.IPv6PrivacyExtensions = "kernel";
|
|
|
|
|
};
|
|
|
|
|
networks."99-wireless-client-dhcp" = {
|
|
|
|
|
# Like above, but this is much more likely to be correct.
|
|
|
|
|
matchConfig.WLANInterfaceType = "station";
|
|
|
|
|
DHCP = "yes";
|
|
|
|
|
networkConfig.IPv6PrivacyExtensions = "kernel";
|
|
|
|
|
# We also set the route metric to one more than the default
|
|
|
|
|
# of 1024, so that Ethernet is preferred if both are
|
|
|
|
|
# available.
|
|
|
|
|
dhcpV4Config.RouteMetric = 1025;
|
|
|
|
|
ipv6AcceptRAConfig.RouteMetric = 1025;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interfaceNetworks = mkMerge (forEach interfaces (i: {
|
|
|
|
|
netdevs = mkIf i.virtual ({
|
|
|
|
|
"40-${i.name}" = {
|
|
|
|
|
netdevConfig = {
|
|
|
|
|
Name = i.name;
|
|
|
|
|
Kind = i.virtualType;
|
|
|
|
|
};
|
|
|
|
|
"${i.virtualType}Config" = optionalAttrs (i.virtualOwner != null) {
|
|
|
|
|
User = i.virtualOwner;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
2023-10-03 02:26:13 +02:00
|
|
|
|
networks."40-${i.name}" = {
|
2022-10-17 01:59:41 +02:00
|
|
|
|
name = mkDefault i.name;
|
|
|
|
|
DHCP = mkForce (dhcpStr
|
2023-10-07 18:40:42 +02:00
|
|
|
|
(if i.useDHCP != null then i.useDHCP else (config.networking.useDHCP && i.ipv4.addresses == [ ])));
|
2022-10-17 01:59:41 +02:00
|
|
|
|
address = forEach (interfaceIps i)
|
|
|
|
|
(ip: "${ip.address}/${toString ip.prefixLength}");
|
|
|
|
|
routes = forEach (interfaceRoutes i)
|
|
|
|
|
(route: {
|
|
|
|
|
# Most of these route options have not been tested.
|
|
|
|
|
# Please fix or report any mistakes you may find.
|
|
|
|
|
routeConfig =
|
|
|
|
|
optionalAttrs (route.address != null && route.prefixLength != null) {
|
|
|
|
|
Destination = "${route.address}/${toString route.prefixLength}";
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? fastopen_no_cookie) {
|
|
|
|
|
FastOpenNoCookie = route.options.fastopen_no_cookie;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.via != null) {
|
|
|
|
|
Gateway = route.via;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.type != null) {
|
|
|
|
|
Type = route.type;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? onlink) {
|
|
|
|
|
GatewayOnLink = true;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? initrwnd) {
|
|
|
|
|
InitialAdvertisedReceiveWindow = route.options.initrwnd;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? initcwnd) {
|
|
|
|
|
InitialCongestionWindow = route.options.initcwnd;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? pref) {
|
|
|
|
|
IPv6Preference = route.options.pref;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? mtu) {
|
|
|
|
|
MTUBytes = route.options.mtu;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? metric) {
|
|
|
|
|
Metric = route.options.metric;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? src) {
|
|
|
|
|
PreferredSource = route.options.src;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? protocol) {
|
|
|
|
|
Protocol = route.options.protocol;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? quickack) {
|
|
|
|
|
QuickAck = route.options.quickack;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? scope) {
|
|
|
|
|
Scope = route.options.scope;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? from) {
|
|
|
|
|
Source = route.options.from;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? table) {
|
|
|
|
|
Table = route.options.table;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? advmss) {
|
|
|
|
|
TCPAdvertisedMaximumSegmentSize = route.options.advmss;
|
|
|
|
|
} //
|
|
|
|
|
optionalAttrs (route.options ? ttl-propagate) {
|
|
|
|
|
TTLPropagate = route.options.ttl-propagate == "enabled";
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
networkConfig.IPv6PrivacyExtensions = "kernel";
|
|
|
|
|
linkConfig = optionalAttrs (i.macAddress != null) {
|
|
|
|
|
MACAddress = i.macAddress;
|
|
|
|
|
} // optionalAttrs (i.mtu != null) {
|
|
|
|
|
MTUBytes = toString i.mtu;
|
|
|
|
|
};
|
2023-10-03 02:26:13 +02:00
|
|
|
|
};
|
2022-10-17 01:59:41 +02:00
|
|
|
|
}));
|
|
|
|
|
|
2023-08-25 19:11:40 +02:00
|
|
|
|
bridgeNetworks = mkMerge (flip mapAttrsToList cfg.bridges (name: bridge: {
|
|
|
|
|
netdevs."40-${name}" = {
|
|
|
|
|
netdevConfig = {
|
|
|
|
|
Name = name;
|
|
|
|
|
Kind = "bridge";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
networks = listToAttrs (forEach bridge.interfaces (bi:
|
2023-10-03 02:26:13 +02:00
|
|
|
|
nameValuePair "40-${bi}" {
|
2023-08-25 19:11:40 +02:00
|
|
|
|
DHCP = mkOverride 0 (dhcpStr false);
|
|
|
|
|
networkConfig.Bridge = name;
|
2023-10-03 02:26:13 +02:00
|
|
|
|
}));
|
2023-08-25 19:11:40 +02:00
|
|
|
|
}));
|
|
|
|
|
|
2023-08-25 16:44:43 +02:00
|
|
|
|
vlanNetworks = mkMerge (flip mapAttrsToList cfg.vlans (name: vlan: {
|
|
|
|
|
netdevs."40-${name}" = {
|
|
|
|
|
netdevConfig = {
|
|
|
|
|
Name = name;
|
|
|
|
|
Kind = "vlan";
|
|
|
|
|
};
|
|
|
|
|
vlanConfig.Id = vlan.id;
|
|
|
|
|
};
|
2023-10-03 02:26:13 +02:00
|
|
|
|
networks."40-${vlan.interface}" = {
|
2023-08-25 16:44:43 +02:00
|
|
|
|
vlan = [ name ];
|
2023-10-03 02:26:13 +02:00
|
|
|
|
};
|
2023-08-25 16:44:43 +02:00
|
|
|
|
}));
|
|
|
|
|
|
2014-11-20 11:37:48 +01:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
2022-10-17 01:59:41 +02:00
|
|
|
|
config = mkMerge [
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
2022-10-17 01:59:41 +02:00
|
|
|
|
(mkIf config.boot.initrd.network.enable {
|
|
|
|
|
# Note this is if initrd.network.enable, not if
|
|
|
|
|
# initrd.systemd.network.enable. By setting the latter and not the
|
|
|
|
|
# former, the user retains full control over the configuration.
|
2023-08-25 16:44:43 +02:00
|
|
|
|
boot.initrd.systemd.network = mkMerge [
|
2023-10-03 01:19:48 +02:00
|
|
|
|
defaultGateways
|
2023-08-25 16:44:43 +02:00
|
|
|
|
(genericDhcpNetworks true)
|
|
|
|
|
interfaceNetworks
|
2023-08-25 19:11:40 +02:00
|
|
|
|
bridgeNetworks
|
2023-08-25 16:44:43 +02:00
|
|
|
|
vlanNetworks
|
|
|
|
|
];
|
|
|
|
|
boot.initrd.availableKernelModules =
|
2023-08-25 19:11:40 +02:00
|
|
|
|
optional (cfg.bridges != {}) "bridge" ++
|
2023-08-25 16:44:43 +02:00
|
|
|
|
optional (cfg.vlans != {}) "8021q";
|
2022-10-17 01:59:41 +02:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
(mkIf cfg.useNetworkd {
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
|
|
|
|
assertions = [ {
|
|
|
|
|
assertion = cfg.defaultGatewayWindowSize == null;
|
|
|
|
|
message = "networking.defaultGatewayWindowSize is not supported by networkd.";
|
2017-01-14 02:14:16 +01:00
|
|
|
|
} {
|
2023-10-03 02:26:13 +02:00
|
|
|
|
assertion = cfg.defaultGateway != null -> cfg.defaultGateway.interface != null;
|
|
|
|
|
message = "networking.defaultGateway.interface is not optional when using networkd.";
|
2017-01-14 02:14:16 +01:00
|
|
|
|
} {
|
2023-10-03 02:26:13 +02:00
|
|
|
|
assertion = cfg.defaultGateway6 != null -> cfg.defaultGateway6.interface != null;
|
|
|
|
|
message = "networking.defaultGateway6.interface is not optional when using networkd.";
|
2015-02-24 22:09:06 +01:00
|
|
|
|
} ] ++ flip mapAttrsToList cfg.bridges (n: { rstp, ... }: {
|
|
|
|
|
assertion = !rstp;
|
|
|
|
|
message = "networking.bridges.${n}.rstp is not supported by networkd.";
|
2021-08-11 11:02:47 +02:00
|
|
|
|
}) ++ flip mapAttrsToList cfg.fooOverUDP (n: { local, ... }: {
|
|
|
|
|
assertion = local == null;
|
|
|
|
|
message = "networking.fooOverUDP.${n}.local is not supported by networkd.";
|
2015-02-24 22:09:06 +01:00
|
|
|
|
});
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
2016-09-13 07:55:17 +02:00
|
|
|
|
networking.dhcpcd.enable = mkDefault false;
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
|
|
|
|
systemd.network =
|
2022-10-17 01:59:41 +02:00
|
|
|
|
mkMerge [ {
|
2014-11-20 11:37:48 +01:00
|
|
|
|
enable = true;
|
|
|
|
|
}
|
2023-10-03 01:19:48 +02:00
|
|
|
|
defaultGateways
|
2022-10-17 01:59:41 +02:00
|
|
|
|
(genericDhcpNetworks false)
|
|
|
|
|
interfaceNetworks
|
2023-08-25 19:11:40 +02:00
|
|
|
|
bridgeNetworks
|
2014-11-20 11:37:48 +01:00
|
|
|
|
(mkMerge (flip mapAttrsToList cfg.bonds (name: bond: {
|
|
|
|
|
netdevs."40-${name}" = {
|
|
|
|
|
netdevConfig = {
|
|
|
|
|
Name = name;
|
|
|
|
|
Kind = "bond";
|
|
|
|
|
};
|
2017-02-03 02:20:47 +01:00
|
|
|
|
bondConfig = let
|
|
|
|
|
# manual mapping as of 2017-02-03
|
|
|
|
|
# man 5 systemd.netdev [BOND]
|
|
|
|
|
# to https://www.kernel.org/doc/Documentation/networking/bonding.txt
|
|
|
|
|
# driver options.
|
|
|
|
|
driverOptionMapping = let
|
|
|
|
|
trans = f: optName: { valTransform = f; optNames = [optName]; };
|
|
|
|
|
simp = trans id;
|
|
|
|
|
ms = trans (v: v + "ms");
|
|
|
|
|
in {
|
|
|
|
|
Mode = simp "mode";
|
2017-02-18 15:51:07 +01:00
|
|
|
|
TransmitHashPolicy = simp "xmit_hash_policy";
|
2017-02-03 02:20:47 +01:00
|
|
|
|
LACPTransmitRate = simp "lacp_rate";
|
|
|
|
|
MIIMonitorSec = ms "miimon";
|
|
|
|
|
UpDelaySec = ms "updelay";
|
|
|
|
|
DownDelaySec = ms "downdelay";
|
|
|
|
|
LearnPacketIntervalSec = simp "lp_interval";
|
|
|
|
|
AdSelect = simp "ad_select";
|
|
|
|
|
FailOverMACPolicy = simp "fail_over_mac";
|
|
|
|
|
ARPValidate = simp "arp_validate";
|
|
|
|
|
# apparently in ms for this value?! Upstream bug?
|
|
|
|
|
ARPIntervalSec = simp "arp_interval";
|
|
|
|
|
ARPIPTargets = simp "arp_ip_target";
|
|
|
|
|
ARPAllTargets = simp "arp_all_targets";
|
|
|
|
|
PrimaryReselectPolicy = simp "primary_reselect";
|
|
|
|
|
ResendIGMP = simp "resend_igmp";
|
|
|
|
|
PacketsPerSlave = simp "packets_per_slave";
|
|
|
|
|
GratuitousARP = { valTransform = id;
|
|
|
|
|
optNames = [ "num_grat_arp" "num_unsol_na" ]; };
|
|
|
|
|
AllSlavesActive = simp "all_slaves_active";
|
|
|
|
|
MinLinks = simp "min_links";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
do = bond.driverOptions;
|
|
|
|
|
assertNoUnknownOption = let
|
|
|
|
|
knownOptions = flatten (mapAttrsToList (_: kOpts: kOpts.optNames)
|
|
|
|
|
driverOptionMapping);
|
|
|
|
|
# options that apparently don’t exist in the networkd config
|
|
|
|
|
unknownOptions = [ "primary" ];
|
|
|
|
|
assertTrace = bool: msg: if bool then true else builtins.trace msg false;
|
|
|
|
|
in assert all (driverOpt: assertTrace
|
|
|
|
|
(elem driverOpt (knownOptions ++ unknownOptions))
|
|
|
|
|
"The bond.driverOption `${driverOpt}` cannot be mapped to the list of known networkd bond options. Please add it to the mapping above the assert or to `unknownOptions` should it not exist in networkd.")
|
|
|
|
|
(mapAttrsToList (k: _: k) do); "";
|
|
|
|
|
# get those driverOptions that have been set
|
|
|
|
|
filterSystemdOptions = filterAttrs (sysDOpt: kOpts:
|
2019-08-13 23:52:01 +02:00
|
|
|
|
any (kOpt: do ? ${kOpt}) kOpts.optNames);
|
2017-02-03 02:20:47 +01:00
|
|
|
|
# build final set of systemd options to bond values
|
|
|
|
|
buildOptionSet = mapAttrs (_: kOpts: with kOpts;
|
|
|
|
|
# we simply take the first set kernel bond option
|
|
|
|
|
# (one option has multiple names, which is silly)
|
2019-08-13 23:52:01 +02:00
|
|
|
|
head (map (optN: valTransform (do.${optN}))
|
2017-02-03 02:20:47 +01:00
|
|
|
|
# only map those that exist
|
2019-08-13 23:52:01 +02:00
|
|
|
|
(filter (o: do ? ${o}) optNames)));
|
2017-02-03 02:20:47 +01:00
|
|
|
|
in seq assertNoUnknownOption
|
|
|
|
|
(buildOptionSet (filterSystemdOptions driverOptionMapping));
|
|
|
|
|
|
2014-11-20 11:37:48 +01:00
|
|
|
|
};
|
2017-02-03 02:20:47 +01:00
|
|
|
|
|
2019-08-05 13:03:38 +02:00
|
|
|
|
networks = listToAttrs (forEach bond.interfaces (bi:
|
2023-10-03 02:26:13 +02:00
|
|
|
|
nameValuePair "40-${bi}" {
|
2014-11-20 11:37:48 +01:00
|
|
|
|
DHCP = mkOverride 0 (dhcpStr false);
|
|
|
|
|
networkConfig.Bond = name;
|
2023-10-03 02:26:13 +02:00
|
|
|
|
}));
|
2014-11-20 11:37:48 +01:00
|
|
|
|
})))
|
2014-11-27 00:42:32 +01:00
|
|
|
|
(mkMerge (flip mapAttrsToList cfg.macvlans (name: macvlan: {
|
|
|
|
|
netdevs."40-${name}" = {
|
|
|
|
|
netdevConfig = {
|
|
|
|
|
Name = name;
|
|
|
|
|
Kind = "macvlan";
|
|
|
|
|
};
|
2014-11-30 10:19:33 +01:00
|
|
|
|
macvlanConfig = optionalAttrs (macvlan.mode != null) { Mode = macvlan.mode; };
|
2014-11-27 00:42:32 +01:00
|
|
|
|
};
|
2023-10-03 02:26:13 +02:00
|
|
|
|
networks."40-${macvlan.interface}" = {
|
2014-11-27 00:42:32 +01:00
|
|
|
|
macvlan = [ name ];
|
2023-10-03 02:26:13 +02:00
|
|
|
|
};
|
2014-11-27 00:42:32 +01:00
|
|
|
|
})))
|
2021-08-11 11:02:47 +02:00
|
|
|
|
(mkMerge (flip mapAttrsToList cfg.fooOverUDP (name: fou: {
|
|
|
|
|
netdevs."40-${name}" = {
|
|
|
|
|
netdevConfig = {
|
|
|
|
|
Name = name;
|
|
|
|
|
Kind = "fou";
|
|
|
|
|
};
|
|
|
|
|
# unfortunately networkd cannot encode dependencies of netdevs on addresses/routes,
|
|
|
|
|
# so we cannot specify Local=, Peer=, PeerPort=. this looks like a missing feature
|
|
|
|
|
# in networkd.
|
|
|
|
|
fooOverUDPConfig = {
|
|
|
|
|
Port = fou.port;
|
|
|
|
|
Encapsulation = if fou.protocol != null then "FooOverUDP" else "GenericUDPEncapsulation";
|
|
|
|
|
} // (optionalAttrs (fou.protocol != null) {
|
|
|
|
|
Protocol = fou.protocol;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
})))
|
2014-11-20 11:37:48 +01:00
|
|
|
|
(mkMerge (flip mapAttrsToList cfg.sits (name: sit: {
|
|
|
|
|
netdevs."40-${name}" = {
|
|
|
|
|
netdevConfig = {
|
|
|
|
|
Name = name;
|
|
|
|
|
Kind = "sit";
|
|
|
|
|
};
|
|
|
|
|
tunnelConfig =
|
|
|
|
|
(optionalAttrs (sit.remote != null) {
|
|
|
|
|
Remote = sit.remote;
|
|
|
|
|
}) // (optionalAttrs (sit.local != null) {
|
|
|
|
|
Local = sit.local;
|
|
|
|
|
}) // (optionalAttrs (sit.ttl != null) {
|
|
|
|
|
TTL = sit.ttl;
|
2021-08-11 12:48:43 +02:00
|
|
|
|
}) // (optionalAttrs (sit.encapsulation != null) (
|
|
|
|
|
{
|
|
|
|
|
FooOverUDP = true;
|
|
|
|
|
Encapsulation =
|
|
|
|
|
if sit.encapsulation.type == "fou"
|
|
|
|
|
then "FooOverUDP"
|
|
|
|
|
else "GenericUDPEncapsulation";
|
|
|
|
|
FOUDestinationPort = sit.encapsulation.port;
|
|
|
|
|
} // (optionalAttrs (sit.encapsulation.sourcePort != null) {
|
|
|
|
|
FOUSourcePort = sit.encapsulation.sourcePort;
|
|
|
|
|
})));
|
2014-11-20 11:37:48 +01:00
|
|
|
|
};
|
|
|
|
|
networks = mkIf (sit.dev != null) {
|
2023-10-03 02:26:13 +02:00
|
|
|
|
"40-${sit.dev}" = {
|
2014-11-20 11:37:48 +01:00
|
|
|
|
tunnel = [ name ];
|
2023-10-03 02:26:13 +02:00
|
|
|
|
};
|
2014-11-20 11:37:48 +01:00
|
|
|
|
};
|
|
|
|
|
})))
|
2021-12-07 16:44:00 +01:00
|
|
|
|
(mkMerge (flip mapAttrsToList cfg.greTunnels (name: gre: {
|
|
|
|
|
netdevs."40-${name}" = {
|
|
|
|
|
netdevConfig = {
|
|
|
|
|
Name = name;
|
|
|
|
|
Kind = gre.type;
|
|
|
|
|
};
|
|
|
|
|
tunnelConfig =
|
|
|
|
|
(optionalAttrs (gre.remote != null) {
|
|
|
|
|
Remote = gre.remote;
|
|
|
|
|
}) // (optionalAttrs (gre.local != null) {
|
|
|
|
|
Local = gre.local;
|
2022-03-22 15:54:25 +01:00
|
|
|
|
}) // (optionalAttrs (gre.ttl != null) {
|
|
|
|
|
TTL = gre.ttl;
|
2021-12-07 16:44:00 +01:00
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
networks = mkIf (gre.dev != null) {
|
2023-10-03 02:26:13 +02:00
|
|
|
|
"40-${gre.dev}" = {
|
2021-12-07 16:44:00 +01:00
|
|
|
|
tunnel = [ name ];
|
2023-10-03 02:26:13 +02:00
|
|
|
|
};
|
2021-12-07 16:44:00 +01:00
|
|
|
|
};
|
|
|
|
|
})))
|
2023-08-25 16:44:43 +02:00
|
|
|
|
vlanNetworks
|
2014-11-20 11:37:48 +01:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
# We need to prefill the slaved devices with networking options
|
|
|
|
|
# This forces the network interface creator to initialize slaves.
|
|
|
|
|
networking.interfaces = listToAttrs (map (i: nameValuePair i { }) slaves);
|
|
|
|
|
|
2018-12-29 11:16:51 +01:00
|
|
|
|
systemd.services = let
|
|
|
|
|
# We must escape interfaces due to the systemd interpretation
|
|
|
|
|
subsystemDevice = interface:
|
|
|
|
|
"sys-subsystem-net-devices-${escapeSystemdPath interface}.device";
|
|
|
|
|
# support for creating openvswitch switches
|
|
|
|
|
createVswitchDevice = n: v: nameValuePair "${n}-netdev"
|
|
|
|
|
(let
|
|
|
|
|
deps = map subsystemDevice (attrNames (filterAttrs (_: config: config.type != "internal") v.interfaces));
|
|
|
|
|
ofRules = pkgs.writeText "vswitch-${n}-openFlowRules" v.openFlowRules;
|
|
|
|
|
in
|
|
|
|
|
{ description = "Open vSwitch Interface ${n}";
|
|
|
|
|
wantedBy = [ "network.target" (subsystemDevice n) ];
|
|
|
|
|
# and create bridge before systemd-networkd starts because it might create internal interfaces
|
|
|
|
|
before = [ "systemd-networkd.service" ];
|
|
|
|
|
# shutdown the bridge when network is shutdown
|
|
|
|
|
partOf = [ "network.target" ];
|
|
|
|
|
# requires ovs-vswitchd to be alive at all times
|
|
|
|
|
bindsTo = [ "ovs-vswitchd.service" ];
|
|
|
|
|
# start switch after physical interfaces and vswitch daemon
|
|
|
|
|
after = [ "network-pre.target" "ovs-vswitchd.service" ] ++ deps;
|
|
|
|
|
wants = deps; # if one or more interface fails, the switch should continue to run
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
serviceConfig.RemainAfterExit = true;
|
2021-03-14 17:05:16 +01:00
|
|
|
|
path = [ pkgs.iproute2 config.virtualisation.vswitch.package ];
|
2018-12-29 11:16:51 +01:00
|
|
|
|
preStart = ''
|
|
|
|
|
echo "Resetting Open vSwitch ${n}..."
|
|
|
|
|
ovs-vsctl --if-exists del-br ${n} -- add-br ${n} \
|
|
|
|
|
-- set bridge ${n} protocols=${concatStringsSep "," v.supportedOpenFlowVersions}
|
|
|
|
|
'';
|
|
|
|
|
script = ''
|
|
|
|
|
echo "Configuring Open vSwitch ${n}..."
|
|
|
|
|
ovs-vsctl ${concatStrings (mapAttrsToList (name: config: " -- add-port ${n} ${name}" + optionalString (config.vlan != null) " tag=${toString config.vlan}") v.interfaces)} \
|
|
|
|
|
${concatStrings (mapAttrsToList (name: config: optionalString (config.type != null) " -- set interface ${name} type=${config.type}") 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 --protocols=${v.openFlowVersion} add-flows ${n} ${ofRules}
|
|
|
|
|
'';
|
|
|
|
|
postStop = ''
|
|
|
|
|
echo "Cleaning Open vSwitch ${n}"
|
2023-05-20 04:11:38 +02:00
|
|
|
|
echo "Shutting down internal ${n} interface"
|
2023-10-29 18:27:16 +01:00
|
|
|
|
ip link set dev ${n} down || true
|
2018-12-29 11:16:51 +01:00
|
|
|
|
echo "Deleting flows for ${n}"
|
|
|
|
|
ovs-ofctl --protocols=${v.openFlowVersion} del-flows ${n} || true
|
|
|
|
|
echo "Deleting Open vSwitch ${n}"
|
|
|
|
|
ovs-vsctl --if-exists del-br ${n} || true
|
|
|
|
|
'';
|
|
|
|
|
});
|
|
|
|
|
in mapAttrs' createVswitchDevice cfg.vswitches
|
|
|
|
|
// {
|
|
|
|
|
"network-local-commands" = {
|
|
|
|
|
after = [ "systemd-networkd.service" ];
|
|
|
|
|
bindsTo = [ "systemd-networkd.service" ];
|
|
|
|
|
};
|
|
|
|
|
};
|
2022-10-17 01:59:41 +02:00
|
|
|
|
})
|
2014-11-20 11:37:48 +01:00
|
|
|
|
|
2022-10-17 01:59:41 +02:00
|
|
|
|
];
|
2014-11-20 11:37:48 +01:00
|
|
|
|
}
|