2
0
Fork 0

nixos: cleanup networking, initialize eth0 during tests

This commit is contained in:
Ehmry - 2020-11-10 16:01:58 +01:00
parent f8df320507
commit 9a8939dc1a
2 changed files with 250 additions and 171 deletions

View File

@ -3,7 +3,9 @@
with lib; with lib;
{ {
options.networking.interfaces = lib.mkOption { options = {
networking.interfaces = lib.mkOption {
type = with types; type = with types;
attrsOf (submodule ({ ... }: { attrsOf (submodule ({ ... }: {
options.genode = { options.genode = {
@ -11,7 +13,7 @@ with lib;
driver = mkOption { type = types.enum [ "ipxe" "virtio" ]; }; driver = mkOption { type = types.enum [ "ipxe" "virtio" ]; };
stack = mkOption { stack = mkOption {
type = types.enum [ "lwip" "lxip" ]; type = with types; nullOr (enum [ "lwip" "lxip" ]);
default = "lwip"; default = "lwip";
}; };
@ -19,27 +21,88 @@ with lib;
})); }));
}; };
config.genode.init.children = let hardware.genode.platform.policies = lib.mkOption {
inherit (builtins) toFile; type = with types; listOf path;
default = [ ];
description = ''
List of policies to append to the Genode platform driver.
Type is Init.Config.Policy.Type.
'';
};
};
config = {
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.";
};
in lib.mapAttrsToList addrCheck config.networking.interfaces
++ lib.mapAttrsToList routeCheck config.networking.interfaces;
hardware.genode.platform.policies = lib.lists.imap0 (i: name:
builtins.toFile (name + ".platform-policy.dhall") ''
let Genode = env:DHALL_GENODE
in Genode.Init.Config.Policy::{
, service = "Platform"
, label = Genode.Init.LabelSelector.prefix "${name}.driver"
, content =
[ Genode.Prelude.XML.leaf
{ name = "pci"
, attributes = toMap {
, class = "ETHERNET"
, index = "${toString i}"
}
}
]
}
'') (builtins.attrNames config.networking.interfaces);
genode.core.basePackages = with pkgs.genodePackages; [
acpi_drv
platform_drv
];
genode.init.children = let
nics = mapAttrs' (name: interface: nics = mapAttrs' (name: interface:
let name' = "nic." + name; let name' = name + ".driver";
in { in {
name = name'; name = name';
value = { value = let
inputs = with pkgs.genodePackages; binary = with pkgs.genodePackages;
{ {
ipxe = [ ipxe_nic_drv ]; ipxe = ipxe_nic_drv;
virtio = [ virtio_nic_drv ]; virtio = virtio_nic_drv;
}.${interface.genode.driver}; }.${interface.genode.driver};
configFile = toFile "${name'}.dhall" '' in {
inputs = [ binary ];
configFile = let
policies = if interface.genode.stack == null then
"[] : List Init.Config.Policy.Type"
else ''
[ Init.Config.Policy::{
, service = "Nic"
, label = Init.LabelSelector.prefix "${name}.sockets"
}
]
'';
in pkgs.writeText "${name'}.dhall" ''
let Genode = env:DHALL_GENODE let Genode = env:DHALL_GENODE
let Init = Genode.Init let Init = Genode.Init
in Init.Child.flat in Init.Child.flat
Init.Child.Attributes::{ Init.Child.Attributes::{
, binary = "virtio_pci_nic" , binary = "${binary.pname}"
, provides = [ "Nic" ] , provides = [ "Nic" ]
, resources = Init.Resources::{ , resources = Init.Resources::{
, caps = 128 , caps = 128
@ -47,13 +110,8 @@ with lib;
} }
, routes = [ Init.ServiceRoute.parent "IO_MEM" ] , routes = [ Init.ServiceRoute.parent "IO_MEM" ]
, config = Init.Config::{ , config = Init.Config::{
, policies = , attributes = toMap { verbose = "true" }
[ Init.Config.Policy::{ , policies = ${policies}
, service = "Nic"
, label =
Init.LabelSelector.prefix "sockets.${name}"
}
]
} }
} }
''; '';
@ -61,24 +119,53 @@ with lib;
}) config.networking.interfaces; }) config.networking.interfaces;
sockets = mapAttrs' (name: interface: sockets = mapAttrs' (name: interface:
let name' = "sockets." + name; let name' = name + ".sockets";
in { in {
name = name'; name = name';
value = { value = if interface.genode.stack == null then
null
else {
inputs = with pkgs.genodePackages; inputs = with pkgs.genodePackages;
{ {
lwip = [ vfs_lwip ]; lwip = [ vfs_lwip ];
lxip = [ vfs_lixp ]; lxip = [ vfs_lixp ];
}.${interface.genode.stack}; }.${interface.genode.stack};
configFile = let ipv4 = builtins.head interface.ipv4.addresses; configFile = let
in toFile "${name'}.dhall" '' binary = "${pkgs.genodePackages.vfs}/bin/vfs";
settings = with builtins;
lib.optionals (interface.ipv4.addresses != [ ])
(let addr = head interface.ipv4.addresses;
in [
{
name = "ip_addr";
value = addr.address;
}
{
name = "netmask";
value = if addr.prefixLength == 24 then
"255.255.255.0"
else
throw "missing prefix to netmask conversion";
}
]) ++ lib.optional (interface.ipv4.routes != [ ])
(let route = head interface.ipv4.routes;
in {
name = "gateway";
value = route.address;
}) ++ lib.optional (interface.useDHCP != null) {
name = "dhcp";
value = if interface.useDHCP then "true" else "false";
};
settingsMap = map ({ name, value }:
''{ mapKey = "${name}", mapValue = "${value}" }'') settings;
in pkgs.writeText "${name'}.dhall" ''
let Genode = env:DHALL_GENODE let Genode = env:DHALL_GENODE
let Init = Genode.Init let Init = Genode.Init
in Init.Child.flat in Init.Child.flat
Init.Child.Attributes::{ Init.Child.Attributes::{
, binary = "vfs" , binary = "${binary}"
, provides = [ "File_system" ] , provides = [ "File_system" ]
, resources = Init.Resources::{ caps = 128, ram = Genode.units.MiB 16 } , resources = Init.Resources::{ caps = 128, ram = Genode.units.MiB 16 }
, config = Init.Config::{ , config = Init.Config::{
@ -98,14 +185,12 @@ with lib;
, content = , content =
[ XML.leaf [ XML.leaf
{ name = "lwip" { name = "lwip"
, attributes = toMap , attributes = [
{ ip_addr = "${ipv4.address}", netmask = "${ ${
if ipv4.prefixLength == 24 then builtins.concatStringsSep ", "
"255.255.255.0" settingsMap
else }
throw ] : Genode.Prelude.Map.Type Text Text
"missing prefix to netmask conversion"
}" }
} }
] ]
} }
@ -116,58 +201,16 @@ with lib;
}; };
}) config.networking.interfaces; }) config.networking.interfaces;
in nics // sockets // { in nics // (lib.filterAttrs (n: v: v != null) sockets) // {
platform_drv = {
inputs = [ pkgs.genodePackages.platform_drv ];
configFile = let
policies = concatMapStrings (name: ''
Init.Config.Policy::{
, service = "Platform"
, label = Init.LabelSelector.prefix "nic.${name}"
, content =
[ Genode.Prelude.XML.leaf
{ name = "pci", attributes = toMap { class = "ETHERNET" } }
]
}
'') (builtins.attrNames config.networking.interfaces);
in toFile "platform_drv.dhall" ''
let Genode = env:DHALL_GENODE
let Init = Genode.Init
let label = \(_ : Text) -> { local = _, route = _ }
in Init.Child.flat
Init.Child.Attributes::{
, binary = "platform_drv"
, resources = Init.Resources::{
, caps = 800
, ram = Genode.units.MiB 4
, constrainPhys = True
}
, reportRoms = [ label "acpi" ]
, provides = [ "Platform" ]
, routes =
[ Init.ServiceRoute.parent "IRQ"
, Init.ServiceRoute.parent "IO_MEM"
, Init.ServiceRoute.parent "IO_PORT"
]
, config = Init.Config::{
, policies = [ ${policies} ]
}
}
'';
};
acpi_drv = { acpi_drv = {
inputs = [ pkgs.genodePackages.acpi_drv ]; coreROMs = [ "acpi_drv" ];
configFile = toFile "acpi_drv.dhall" '' configFile = pkgs.writeText "acpi_drv.dhall" ''
let Genode = env:DHALL_GENODE let Genode = env:DHALL_GENODE
let Init = Genode.Init let Init = Genode.Init
let label = \(_ : Text) -> { local = _, route = _ } let label = λ(_ : Text) { local = _, route = _ }
in Init.Child.flat in Init.Child.flat
Init.Child.Attributes::{ Init.Child.Attributes::{
@ -177,7 +220,7 @@ with lib;
, ram = Genode.units.MiB 4 , ram = Genode.units.MiB 4
, constrainPhys = True , constrainPhys = True
} }
, romReports = [ label "acpi" ] , romReports = [ label "acpi", label "smbios_table" ]
, routes = , routes =
[ Init.ServiceRoute.parent "IRQ" [ Init.ServiceRoute.parent "IRQ"
, Init.ServiceRoute.parent "IO_MEM" , Init.ServiceRoute.parent "IO_MEM"
@ -187,6 +230,40 @@ with lib;
''; '';
}; };
platform_drv = {
coreROMs = [ "platform_drv" ];
configFile = let
policies = map (policy: ", ${policy}")
config.hardware.genode.platform.policies;
in pkgs.writeText "platform_drv.dhall" ''
let Genode = env:DHALL_GENODE
let Init = Genode.Init
in Init.Child.flat
Init.Child.Attributes::{
, binary = "platform_drv"
, resources = Init.Resources::{
, caps = 800
, ram = Genode.units.MiB 4
, constrainPhys = True
}
, reportRoms = let label = "acpi" in [ { local = label, route = label } ]
, provides = [ "Platform" ]
, routes =
[ Init.ServiceRoute.parent "IRQ"
, Init.ServiceRoute.parent "IO_MEM"
, Init.ServiceRoute.parent "IO_PORT"
]
, config = Init.Config::{
, policies = [ ${toString policies} ]
}
}
'';
};
};
}; };
} }

View File

@ -79,7 +79,9 @@ rec {
config = { config = {
networking.hostName = mkDefault m.fst; networking.hostName = mkDefault m.fst;
networking.interfaces = listToAttrs interfaces; networking.interfaces = {
eth0.genode.driver = "virtio";
} // listToAttrs interfaces;
networking.primaryIPAddress = optionalString (interfaces != [ ]) networking.primaryIPAddress = optionalString (interfaces != [ ])
(head (head interfaces).value.ipv4.addresses).address; (head (head interfaces).value.ipv4.addresses).address;