sigil/nixos-modules/hardware/nic.nix

120 lines
3.9 KiB
Nix

{ config, pkgs, lib, ... }:
with lib;
{
options = {
networking.interfaces = lib.mkOption {
type = with types;
attrsOf (submodule ({ ... }: {
options.genode = {
driver = mkOption { type = types.enum [ "ipxe" "virtio" ]; };
stack = mkOption {
type = with types; nullOr (enum [ "lwip" "lxip" ]);
default = "lwip";
};
};
}));
};
};
config =
# TODO: create drivers in both the first and second level inits
{
assertions = with builtins;
let
addrCheck = name: interface: {
assertion = lessThan (length interface.ipv4.addresses) 2;
message = "Genode interfaces do not support multihoming.";
};
routeCheck = name: interface: {
assertion = lessThan (length interface.ipv4.routes) 2;
message = "Genode interfaces do not support multiple routes.";
};
policyCheck = name: interface:
let
clientList = filter (x: x != null) (lib.mapAttrsToList
(childName: value:
if any (nic: nic == name) value.routeToNics then
childName
else
null) config.genode.core.children);
in {
assertion = clientList == [ ] || length clientList == 1;
message = "Multiple routes to Nic ${name}, ${clientList}";
};
in lib.lists.concatMap
(f: lib.mapAttrsToList f config.networking.interfaces) [
addrCheck
routeCheck
policyCheck
];
hardware.genode.platform.policies = let
mkPolicy = { name, platformPolicy }:
pkgs.writeText "${name}.policy.dhall" ''${platformPolicy} "${name}"'';
childPolicies = builtins.concatLists (lib.attrsets.mapAttrsToList
(child: childAttrs:
lib.attrsets.mapAttrsToList (uplink: uplinkAttrs:
mkPolicy {
name = "${child}-${uplink}.driver";
inherit (uplinkAttrs) platformPolicy;
}) childAttrs.uplinks) config.genode.core.children);
in childPolicies;
genode.core.supportChildren = let
mkUplinkDriver = { policyPrefix, driver, verbose }: {
package = with pkgs.genodePackages;
{
ipxe = ipxe_nic_drv;
virtio = virtio_nic_drv;
}.${driver};
configFile = pkgs.writeText "driver.dhall" ''
let Genode = env:DHALL_GENODE
let Init = Genode.Init
in λ(binary : Text)
Init.Child.flat
Init.Child.Attributes::{
, binary
, resources = Init.Resources::{ caps = 128, ram = Genode.units.MiB 4 }
, routes = [ Init.ServiceRoute.parent "IO_MEM" ]
, config = Init.Config::{
, attributes = toMap { verbose = "${
if verbose then "yes" else "no"
}" }
, policies =
[ Init.Config.Policy::{
, service = "Nic"
, label = Init.LabelSelector.prefix "${policyPrefix}"
}
]
}
}
'';
};
otherDrivers = builtins.concatLists (lib.attrsets.mapAttrsToList
(child: childAttrs:
lib.attrsets.mapAttrsToList (uplink: uplinkAttrs: {
name = "${child}-${uplink}.driver";
value = mkUplinkDriver {
policyPrefix = "${child} -> ${uplink}";
inherit (uplinkAttrs) driver verbose;
};
}) childAttrs.uplinks) config.genode.core.children);
in builtins.listToAttrs otherDrivers;
};
}