2024-04-04 16:34:14 +02:00
|
|
|
interfaces:
|
2024-04-06 23:11:39 +02:00
|
|
|
{ name, lib, pkgs, config, ... }:
|
2024-04-04 16:34:14 +02:00
|
|
|
let cfg = config;
|
|
|
|
in with lib; {
|
|
|
|
options = {
|
2024-04-06 23:11:39 +02:00
|
|
|
deployement = {
|
|
|
|
targetHost = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
cmd = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
readOnly = true;
|
|
|
|
};
|
|
|
|
};
|
2024-04-04 16:34:14 +02:00
|
|
|
vlans = let
|
|
|
|
range_type.options = {
|
|
|
|
begin = mkOption { type = types.ints.unsigned; };
|
|
|
|
end = mkOption { type = types.ints.unsigned; };
|
|
|
|
};
|
2024-04-05 15:50:41 +02:00
|
|
|
vlan_type.options = {
|
|
|
|
ids = mkOption {
|
2024-04-05 19:40:35 +02:00
|
|
|
type = types.either
|
|
|
|
types.ints.unsigned
|
|
|
|
(types.listOf (types.either types.ints.unsigned (types.submodule range_type)));
|
2024-04-05 15:50:41 +02:00
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
management = mkOption {
|
2024-04-06 23:11:39 +02:00
|
|
|
# FIXME : support ipv4, either static or dhcp (with the coffee)
|
2024-04-05 15:50:41 +02:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
IP address with wich to permit management on this vlan.
|
|
|
|
Only one vlan can set an IP (this module limitation, not switch).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2024-04-04 16:34:14 +02:00
|
|
|
in mkOption {
|
2024-04-05 15:50:41 +02:00
|
|
|
type = types.attrsOf (types.submodule vlan_type);
|
2024-04-04 16:34:14 +02:00
|
|
|
};
|
|
|
|
interfaces = let
|
|
|
|
template = name: {
|
|
|
|
enable = mkEnableOption "the interface ${name}";
|
|
|
|
interface-mode = mkOption {
|
|
|
|
type = types.nullOr (types.enum [ "trunk" "access" ]);
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
vlans = mkOption {
|
|
|
|
type = let
|
|
|
|
vlan_type = types.either (types.strMatching "[^\n\r]+") (types.ints.unsigned);
|
|
|
|
in types.listOf vlan_type;
|
|
|
|
default = [ ];
|
|
|
|
};
|
2024-04-06 14:36:37 +02:00
|
|
|
# TODO: use this option
|
2024-04-04 16:34:14 +02:00
|
|
|
dhcp_trusted = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
management = mkOption {
|
2024-04-06 23:11:39 +02:00
|
|
|
# FIXME : support ipv6, either static or dhcp (with the coffee)
|
2024-04-04 18:17:36 +02:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2024-04-04 16:34:14 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
in builtins.listToAttrs (map (name: { inherit name; value = template name; }) interfaces);
|
|
|
|
};
|
|
|
|
|
2024-04-06 23:11:39 +02:00
|
|
|
config.deployement.cmd = let
|
2024-04-04 16:34:14 +02:00
|
|
|
intf_xmlGen = name: let
|
|
|
|
disable_flag = if !cfg.interfaces.${name}.enable then "<disable/>" else "";
|
|
|
|
# FIXME : need to enforce address in reality
|
2024-04-04 18:17:36 +02:00
|
|
|
mgmt_fam = if !builtins.isNull cfg.interfaces.${name}.management then ''
|
|
|
|
<inet>
|
|
|
|
<address>
|
2024-04-05 19:40:35 +02:00
|
|
|
<name>${cfg.interfaces.${name}.management}</name>
|
2024-04-04 18:17:36 +02:00
|
|
|
</address>
|
|
|
|
</inet>'' else "";
|
2024-04-04 16:34:14 +02:00
|
|
|
members = map (vlan: "<members>${builtins.toString vlan}</members>") cfg.interfaces.${name}.vlans;
|
|
|
|
eth_switch = if builtins.isNull cfg.interfaces.${name}.interface-mode then "" else ''
|
|
|
|
<ethernet-switching>
|
|
|
|
<interface-mode>${cfg.interfaces.${name}.interface-mode}</interface-mode>
|
|
|
|
<vlan>${builtins.concatStringsSep "" members}</vlan>
|
2024-04-05 19:40:35 +02:00
|
|
|
<storm-control><profile-name>default</profile-name></storm-control>
|
2024-04-04 16:34:14 +02:00
|
|
|
</ethernet-switching>'';
|
|
|
|
in ''
|
|
|
|
<interface>
|
|
|
|
<name>${name}</name>
|
|
|
|
${disable_flag}
|
|
|
|
<unit>
|
|
|
|
<name>0</name>
|
|
|
|
<family>
|
|
|
|
${mgmt_fam}
|
|
|
|
${eth_switch}
|
|
|
|
</family>
|
|
|
|
</unit>
|
2024-04-05 19:40:35 +02:00
|
|
|
</interface>
|
|
|
|
'';
|
2024-04-04 16:34:14 +02:00
|
|
|
interface_xmls = map intf_xmlGen interfaces;
|
2024-04-05 19:40:35 +02:00
|
|
|
rstp_gen = name:
|
|
|
|
if cfg.interfaces.${name}.enable && !builtins.isNull cfg.interfaces.${name}.interface-mode then
|
|
|
|
"<interface><name>${name}</name></interface>"
|
|
|
|
else "";
|
|
|
|
rstps = map rstp_gen interfaces;
|
2024-04-04 16:34:14 +02:00
|
|
|
vlan_trust_table = let
|
|
|
|
vlan_map = inter: vlan:
|
2024-04-04 18:17:36 +02:00
|
|
|
if builtins.isString vlan && cfg.interfaces.${inter}.enable then
|
2024-04-04 16:34:14 +02:00
|
|
|
if cfg.interfaces.${inter}.dhcp_trusted then
|
|
|
|
{ ${vlan}.trust = inter; }
|
|
|
|
else
|
|
|
|
{ ${vlan}.notrust = inter; }
|
|
|
|
else
|
|
|
|
{};
|
|
|
|
int_map = inter: map (vlan_map inter) cfg.interfaces.${inter}.vlans;
|
|
|
|
in builtins.zipAttrsWith
|
|
|
|
(vlan: values: builtins.zipAttrsWith (_: ints: ints ) values)
|
|
|
|
(builtins.concatMap int_map interfaces);
|
|
|
|
vlans = let
|
|
|
|
id_map = id: let
|
|
|
|
list =
|
|
|
|
if builtins.isInt id then
|
|
|
|
builtins.toString id
|
|
|
|
else
|
|
|
|
"${builtins.toString id.begin}-${builtins.toString id.end}";
|
|
|
|
in ''<vlan-id-list>${list}</vlan-id-list>'';
|
|
|
|
vlan_map = vlan: let
|
2024-04-05 19:40:35 +02:00
|
|
|
ids = if !builtins.isList cfg.vlans.${vlan}.ids then
|
|
|
|
[ "<vlan-id>${builtins.toString cfg.vlans.${vlan}.ids}</vlan-id>" ]
|
|
|
|
else map id_map cfg.vlans.${vlan}.ids;
|
2024-04-05 15:50:41 +02:00
|
|
|
mgmt_flag = if !builtins.isNull cfg.vlans.${vlan}.management
|
|
|
|
then "<l3-interface>irb.0</l3-interface>" else "";
|
2024-04-04 16:34:14 +02:00
|
|
|
in ''
|
|
|
|
<vlan>
|
|
|
|
<name>${vlan}</name>
|
2024-04-05 15:50:41 +02:00
|
|
|
${mgmt_flag}
|
2024-04-04 16:34:14 +02:00
|
|
|
${builtins.concatStringsSep "\n" ids}
|
|
|
|
</vlan>'';
|
|
|
|
in map vlan_map (builtins.attrNames cfg.vlans);
|
2024-04-05 15:50:41 +02:00
|
|
|
irb_intf = let
|
|
|
|
addresses = map (vlan: vlan.management) (builtins.attrValues cfg.vlans);
|
|
|
|
addr = builtins.foldl' (acc: addr: if !builtins.isNull addr then addr else acc) null addresses;
|
|
|
|
in if !builtins.isNull addr then ''
|
|
|
|
<interface>
|
|
|
|
<name>irb</name>
|
|
|
|
<unit>
|
|
|
|
<name>0</name>
|
|
|
|
<family>
|
2024-04-06 23:11:39 +02:00
|
|
|
<inet6>
|
2024-04-05 15:50:41 +02:00
|
|
|
<address><name>${addr}</name></address>
|
2024-04-06 23:11:39 +02:00
|
|
|
</inet6>
|
2024-04-05 15:50:41 +02:00
|
|
|
</family>
|
|
|
|
</unit>
|
|
|
|
</interface>
|
|
|
|
''
|
|
|
|
else "";
|
2024-04-06 14:36:37 +02:00
|
|
|
config = ''
|
|
|
|
<interfaces operation="replace">
|
|
|
|
${builtins.concatStringsSep "\n" interface_xmls}
|
|
|
|
${irb_intf}
|
|
|
|
</interfaces>
|
|
|
|
<protocols>
|
|
|
|
<rstp operation="replace">
|
|
|
|
${builtins.concatStringsSep "\n" rstps}
|
|
|
|
</rstp>
|
|
|
|
</protocols>
|
|
|
|
<vlans operation="replace">
|
|
|
|
${builtins.concatStringsSep "\n" vlans}
|
|
|
|
</vlans>
|
|
|
|
'';
|
2024-04-06 23:11:39 +02:00
|
|
|
rpc_requests = pkgs.writeText "config-${name}_rpc.xml" ''
|
2024-04-06 14:36:37 +02:00
|
|
|
<rpc>
|
|
|
|
<edit-config>
|
|
|
|
<config>
|
|
|
|
<configuration>
|
|
|
|
${config}
|
|
|
|
</configuration>
|
|
|
|
</config>
|
|
|
|
<target>
|
|
|
|
<candidate/>
|
|
|
|
</target>
|
|
|
|
</edit-config>
|
|
|
|
</rpc>
|
|
|
|
<rpc>
|
|
|
|
<commit/>
|
|
|
|
</rpc>
|
|
|
|
'';
|
2024-04-06 23:11:39 +02:00
|
|
|
in pkgs.writeShellScript "deploy-${name}.sh" ''
|
|
|
|
${pkgs.openssh}/bin/ssh ${cfg.deployement.targetHost} -p 830 -s netconf < ${rpc_requests}
|
2024-04-06 14:36:37 +02:00
|
|
|
''
|
|
|
|
;
|
2024-04-04 16:34:14 +02:00
|
|
|
}
|