summary refs log tree commit diff
path: root/nixos/modules
diff options
context:
space:
mode:
authorWilliam A. Kennington III <william@wkennington.com>2014-11-25 01:53:37 -0800
committerWilliam A. Kennington III <william@wkennington.com>2014-11-26 11:22:03 -0800
commit2c42738231a0f4839c973a8fe9eddd883e70c47a (patch)
treede1d592d4b3e54935bb1e85cdd7009cb978dc890 /nixos/modules
parentc417012c1b1ed3793ac3bd106bfc840d9aad08c7 (diff)
downloadnixlib-2c42738231a0f4839c973a8fe9eddd883e70c47a.tar
nixlib-2c42738231a0f4839c973a8fe9eddd883e70c47a.tar.gz
nixlib-2c42738231a0f4839c973a8fe9eddd883e70c47a.tar.bz2
nixlib-2c42738231a0f4839c973a8fe9eddd883e70c47a.tar.lz
nixlib-2c42738231a0f4839c973a8fe9eddd883e70c47a.tar.xz
nixlib-2c42738231a0f4839c973a8fe9eddd883e70c47a.tar.zst
nixlib-2c42738231a0f4839c973a8fe9eddd883e70c47a.zip
nixos/network-interfaces: Refactor scripts for reliability and dependency removal
Diffstat (limited to 'nixos/modules')
-rw-r--r--nixos/modules/tasks/network-interfaces-scripted.nix96
-rw-r--r--nixos/modules/tasks/network-interfaces.nix3
2 files changed, 48 insertions, 51 deletions
diff --git a/nixos/modules/tasks/network-interfaces-scripted.nix b/nixos/modules/tasks/network-interfaces-scripted.nix
index 4910a391523f..1ca629c186ca 100644
--- a/nixos/modules/tasks/network-interfaces-scripted.nix
+++ b/nixos/modules/tasks/network-interfaces-scripted.nix
@@ -23,6 +23,20 @@ let
       prefixLength = i.ipv6PrefixLength;
     };
 
+  destroyBond = i: ''
+    while true; do
+      UPDATED=1
+      SLAVES=$(ip link | grep 'master ${i}' | awk -F: '{print $2}')
+      for I in $SLAVES; do
+        UPDATED=0
+        ip link set "$I" nomaster
+      done
+      [ "$UPDATED" -eq "1" ] && break
+    done
+    ip link set "${i}" down || true
+    ip link del "${i}" || true
+  '';
+
 in
 
 {
@@ -174,34 +188,31 @@ in
             after = deps;
             serviceConfig.Type = "oneshot";
             serviceConfig.RemainAfterExit = true;
-            path = [ pkgs.bridge_utils pkgs.iproute ];
-            script =
-              ''
-                # Remove Dead Interfaces
-                ip link show "${n}" >/dev/null 2>&1 && ip link delete "${n}"
-
-                brctl addbr "${n}"
+            path = [ pkgs.iproute ];
+            script = ''
+              # Remove Dead Interfaces
+              echo "Removing old bridge ${n}..."
+              ip link show "${n}" >/dev/null 2>&1 && ip link del "${n}"
 
-                # Set bridge's hello time to 0 to avoid startup delays.
-                brctl setfd "${n}" 0
+              echo "Adding bridge ${n}..."
+              ip link add name "${n}" type bridge
 
-                ${flip concatMapStrings v.interfaces (i: ''
-                  brctl addif "${n}" "${i}"
-                  ip link set "${i}" up
-                  ip addr flush dev "${i}"
+              # Set bridge's hello time to 0 to avoid startup delays.
+              echo 0 >"/sys/class/net/${n}/bridge/hello_time"
+              echo 0 >"/sys/class/net/${n}/bridge/forward_delay"
 
-                  echo "bringing up network device ${n}..."
-                  ip link set "${n}" up
-                '')}
+              # Enslave child interfaces
+              ${flip concatMapStrings v.interfaces (i: ''
+                ip link set "${i}" master "${n}"
+                ip link set "${i}" up
+              '')}
 
-                # !!! Should delete (brctl delif) any interfaces that
-                # no longer belong to the bridge.
-              '';
-            postStop =
-              ''
-                ip link set "${n}" down
-                brctl delbr "${n}"
-              '';
+              ip link set "${n}" up
+            '';
+            postStop = ''
+              ip link set "${n}" down || true
+              ip link del "${n}" || true
+            '';
           });
 
         createBondDevice = n: v: nameValuePair "${n}-netdev"
@@ -215,39 +226,28 @@ in
             before = [ "${n}-cfg.service" ];
             serviceConfig.Type = "oneshot";
             serviceConfig.RemainAfterExit = true;
-            path = [ pkgs.ifenslave pkgs.iproute ];
+            path = [ pkgs.iproute ];
             script = ''
-              ip link add name "${n}" type bond
-
-              # !!! There must be a better way to wait for the interface
-              while [ ! -d /sys/class/net/${n} ]; do sleep 0.1; done;
+              echo "Destroying old bond ${n}..."
+              ${destroyBond n}
 
-              # Ensure the link is down so that we can set options
-              ip link set "${n}" down
+              echo "Creating new bond ${n}..."
+              ip link add name "${n}" type bond \
+                ${optionalString (v.mode != null) "mode ${toString v.mode}"} \
+                ${optionalString (v.miimon != null) "miimon ${toString v.miimon}"} \
+                ${optionalString (v.xmit_hash_policy != null) "xmit_hash_policy ${toString v.xmit_hash_policy}"} \
+                ${optionalString (v.lacp_rate != null) "lacp_rate ${toString v.lacp_rate}"}
 
-              # Set the miimon and mode options
-              ${optionalString (v.miimon != null)
-                "echo \"${toString v.miimon}\" >/sys/class/net/${n}/bonding/miimon"}
-              ${optionalString (v.mode != null)
-                "echo \"${v.mode}\" >/sys/class/net/${n}/bonding/mode"}
-              ${optionalString (v.lacp_rate != null)
-                "echo \"${v.lacp_rate}\" >/sys/class/net/${n}/bonding/lacp_rate"}
-              ${optionalString (v.xmit_hash_policy != null)
-                "echo \"${v.xmit_hash_policy}\" >/sys/class/net/${n}/bonding/xmit_hash_policy"}
+              # !!! There must be a better way to wait for the interface
+              while [ ! -d "/sys/class/net/${n}" ]; do sleep 0.1; done;
 
               # Bring up the bond and enslave the specified interfaces
               ip link set "${n}" up
               ${flip concatMapStrings v.interfaces (i: ''
-                ifenslave "${n}" "${i}"
-              '')}
-            '';
-            postStop = ''
-              ${flip concatMapStrings v.interfaces (i: ''
-                ifenslave -d "${n}" "${i}" >/dev/null 2>&1 || true
+                ip link set "${i}" master "${n}"
               '')}
-              ip link set "${n}" down >/dev/null 2>&1 || true
-              ip link del "${n}" >/dev/null 2>&1 || true
             '';
+            postStop = destroyBond n;
           });
 
         createSitDevice = n: v: nameValuePair "${n}-netdev"
diff --git a/nixos/modules/tasks/network-interfaces.nix b/nixos/modules/tasks/network-interfaces.nix
index ca6240ac0949..ce1e28936fa1 100644
--- a/nixos/modules/tasks/network-interfaces.nix
+++ b/nixos/modules/tasks/network-interfaces.nix
@@ -624,9 +624,6 @@ in
         pkgs.rfkill
         pkgs.openresolv
       ]
-      ++ optional (cfg.bridges != {}) pkgs.bridge_utils
-      ++ optional hasVirtuals pkgs.tunctl
-      ++ optional cfg.enableIPv6 pkgs.ndisc6;
 
     systemd.services = {
       network-local-commands = {