@@ -10,7 +10,7 @@ let
1010
1111 slaves = concatMap (i: i.interfaces) (attrValues cfg.bonds)
1212 ++ concatMap (i: i.interfaces) (attrValues cfg.bridges)
13− ++ concatMap (i: i.interfaces) (attrValues cfg.vswitches)
13+ ++ concatMap (i: attrNames (filterAttrs (_: config: config.type != "internal") i.interfaces)) (attrValues cfg.vswitches)
1414 ++ concatMap (i: [i.interface]) (attrValues cfg.macvlans)
1515 ++ concatMap (i: [i.interface]) (attrValues cfg.vlans);
1616
@@ -336,34 +336,47 @@ let
336336
337337 createVswitchDevice = n: v: nameValuePair "${n}-netdev"
338338 (let
339− deps = concatLists (map deviceDependency v.interfaces);
339+ deps = concatLists (map deviceDependency (attrNames (filterAttrs (_: config: config.type != "internal") v.interfaces)));
340+ internalConfigs = concatMap (i: ["network-link-${i}.service" "network-addresses-${i}.service"]) (attrNames (filterAttrs (_: config: config.type == "internal") v.interfaces));
340341 ofRules = pkgs.writeText "vswitch-${n}-openFlowRules" v.openFlowRules;
341342 in
342343 { description = "Open vSwitch Interface ${n}";
343− wantedBy = [ "network-setup.service" "vswitchd.service" ] ++ deps;
344− bindsTo = [ "vswitchd.service" (subsystemDevice n) ] ++ deps;
345− partOf = [ "network-setup.service" "vswitchd.service" ];
346− after = [ "network-pre.target" "vswitchd.service" ] ++ deps;
347− before = [ "network-setup.service" ];
344+ wantedBy = [ "network-setup.service" (subsystemDevice n) ] ++ internalConfigs;
345+ # before = [ "network-setup.service" ];
346+ # should work without internalConfigs dependencies because address/link configuration depends
347+ # on the device, which is created by ovs-vswitchd with type=internal, but it does not...
348+ before = [ "network-setup.service" ] ++ internalConfigs;
349+ partOf = [ "network-setup.service" ]; # shutdown the bridge when network is shutdown
350+ bindsTo = [ "ovs-vswitchd.service" ]; # requires ovs-vswitchd to be alive at all times
351+ after = [ "network-pre.target" "ovs-vswitchd.service" ] ++ deps; # start switch after physical interfaces and vswitch daemon
352+ wants = deps; # if one or more interface fails, the switch should continue to run
348353 serviceConfig.Type = "oneshot";
349354 serviceConfig.RemainAfterExit = true;
350355 path = [ pkgs.iproute config.virtualisation.vswitch.package ];
356+ preStart = ''
357+ echo "Resetting Open vSwitch ${n}..."
358+ ovs-vsctl --if-exists del-br ${n} -- add-br ${n} \
359+ -- set bridge ${n} protocols=${concatStringsSep "," v.supportedOpenFlowVersions}
360+ '';
351361 script = ''
352− echo "Removing old Open vSwitch ${n}..."
353− ovs-vsctl --if-exists del-br ${n}
354−
355− echo "Adding Open vSwitch ${n}..."
356− ovs-vsctl -- add-br ${n} ${concatMapStrings (i: " -- add-port ${n} ${i}") v.interfaces} \
362+ echo "Configuring Open vSwitch ${n}..."
363+ ovs-vsctl ${concatStrings (mapAttrsToList (name: config: " -- add-port ${n} ${name}" + optionalString (config.vlan != null) " tag=${toString config.vlan}") v.interfaces)} \
364+ ${concatStrings (mapAttrsToList (name: config: optionalString (config.type != null) " -- set interface ${name} type=${config.type}") v.interfaces)} \
357365 ${concatMapStrings (x: " -- set-controller ${n} " + x) v.controllers} \
358366 ${concatMapStrings (x: " -- " + x) (splitString "\n" v.extraOvsctlCmds)}
359367
368+
360369 echo "Adding OpenFlow rules for Open vSwitch ${n}..."
361− ovs-ofctl add-flows ${n} ${ofRules}
370+ ovs-ofctl --protocols=${v.openFlowVersion} add-flows ${n} ${ofRules}
362371 '';
363372 postStop = ''
373+ echo "Cleaning Open vSwitch ${n}"
374+ echo "Shuting down internal ${n} interface"
364375 ip link set ${n} down || true
365− ovs-ofctl del-flows ${n} || true
366− ovs-vsctl --if-exists del-br ${n}
376+ echo "Deleting flows for ${n}"
377+ ovs-ofctl --protocols=${v.openFlowVersion} del-flows ${n} || true
378+ echo "Deleting Open vSwitch ${n}"
379+ ovs-vsctl --if-exists del-br ${n} || true
367380 '';
368381 });
369382
@@ -476,9 +489,9 @@ let
476489 # Remove Dead Interfaces
477490 ip link show "${n}" >/dev/null 2>&1 && ip link delete "${n}"
478491 ip link add link "${v.interface}" name "${n}" type vlan id "${toString v.id}"
479−
480− # We try to bring up the logical VLAN interface. If the master
481− # interface the logical interface is dependent upon is not up yet we will
492+
493+ # We try to bring up the logical VLAN interface. If the master
494+ # interface the logical interface is dependent upon is not up yet we will
482495 # fail to immediately bring up the logical interface. The resulting logical
483496 # interface will brought up later when the master interface is up.
484497 ip link set "${n}" up || true