mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-07-12 13:18:57 +03:00
nixos networking: add vswitch option
Add a configuration option for Open vSwitch that is similar to the option for the Linux kernel ethernet bridge.
This commit is contained in:
parent
000a2108ba
commit
59bc47c9ed
4 changed files with 123 additions and 1 deletions
|
@ -220,6 +220,45 @@ in
|
|||
'';
|
||||
});
|
||||
|
||||
createVswitchDevice = n: v: nameValuePair "${n}-netdev"
|
||||
(let
|
||||
managedInterfaces = filter (x: hasAttr x cfg.interfaces) v.interfaces;
|
||||
managedInterfaceServices = concatMap (i: [ "network-addresses-${i}.service" "network-link-${i}.service" ]) managedInterfaces;
|
||||
virtualInterfaces = filter (x: (hasAttr x cfg.interfaces) && cfg.interfaces.${x}.virtual) v.interfaces;
|
||||
virtualInterfaceServices = concatMap (i: [ "${i}-netdev.service" ]) virtualInterfaces;
|
||||
deps = map subsystemDevice v.interfaces;
|
||||
ofRules = pkgs.writeText "vswitch-${n}-openFlowRules" v.openFlowRules;
|
||||
in
|
||||
{ description = "Open vSwitch Interface ${n}";
|
||||
wantedBy = [ "network.target" "vswitchd.service" (subsystemDevice n) ];
|
||||
requires = optionals v.bindInterfaces (deps ++ managedInterfaceServices ++ virtualInterfaceServices);
|
||||
requiredBy = optionals v.bindInterfaces (managedInterfaceServices ++ virtualInterfaceServices);
|
||||
bindsTo = deps ++ [ "vswitchd.service" ];
|
||||
partOf = [ "vswitchd.service" ];
|
||||
after = [ "network-pre.target" "vswitchd.service" ] ++ deps ++ managedInterfaceServices ++ virtualInterfaceServices;
|
||||
before = [ "network-interfaces.target" (subsystemDevice n) ];
|
||||
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}
|
||||
'';
|
||||
});
|
||||
|
||||
createBondDevice = n: v: nameValuePair "${n}-netdev"
|
||||
(let
|
||||
deps = map subsystemDevice v.interfaces;
|
||||
|
@ -335,6 +374,7 @@ in
|
|||
map configureAddrs interfaces ++
|
||||
map createTunDevice (filter (i: i.virtual) interfaces))
|
||||
// mapAttrs' createBridgeDevice cfg.bridges
|
||||
// mapAttrs' createVswitchDevice cfg.vswitches
|
||||
// mapAttrs' createBondDevice cfg.bonds
|
||||
// mapAttrs' createMacvlanDevice cfg.macvlans
|
||||
// mapAttrs' createSitDevice cfg.sits
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue