webGui: networking now allows multi bonds and multi bridge groups

This commit is contained in:
Eric Schultz
2017-05-05 10:18:30 -05:00
parent 516fcdc53d
commit 3471bf0f36
6 changed files with 213 additions and 154 deletions

View File

@@ -26,59 +26,63 @@ function ifname($name) {
}
return $name;
}
if ($run && file_exists($cfg)) {
$old = parse_ini_string(preg_replace('/^#/m',';',file_get_contents($cfg)));
if (isset($old['SYSNICS'])) {
// new syntax
$ifname = ifname($set);
} else {
// legacy syntax
if ($set=='eth0') $ifname = $old['BRIDGING']=='yes' ? ($old['BRNAME'] ?: 'br0') : ($old['BONDING']=='yes' ? ($old['BONDNAME'] ?: 'bond0') : $set);
}
function bond_nics(&$bond,$nic) {
$bond['BONDNICS'] = str_replace(',',' ',$bond['BONDNICS']);
return explode(' ',preg_replace("/$nic ?/","",$bond['BONDNICS']));
}
function bridge_nics(&$bridge,$nic) {
$bridge['BRNICS'] = str_replace(',',' ',$bridge['BRNICS']);
return explode(' ',preg_replace("/$nic ?/","",$bridge['BRNICS']));
}
// stop interface with existing (old) configuration
// don't execute when only interface description has changed
if ($run) exec("/etc/rc.d/rc.inet1 ".escapeshellarg("{$ifname}_stop")." >/dev/null");
if ($bonding = $ini['eth0']['BONDING']=='yes') {
$ini['eth0']['BONDNICS'] = str_replace(',',' ',$ini['eth0']['BONDNICS']);
$bond0 = explode(' ',trim(str_replace('eth0','',$ini['eth0']['BONDNICS'])));
// ensure additional NICs in bond are set free
if ($run && $set=='eth0') foreach ($bond0 as $nic) {
if (isset($old['SYSNICS'])) $nic = ifname($nic);
if ($nic && $nic!=$ifname) exec("/etc/rc.d/rc.inet1 ".escapeshellarg("{$nic}_stop")." >/dev/null");
if ($run) {
$old = [];
if (file_exists($cfg)) {
$old = parse_ini_string(preg_replace('/^#/m',';',file_get_contents($cfg)));
if (isset($old['SYSNICS'])) {
// new syntax
$ifname = ifname($set);
} else {
// legacy syntax
if ($set=='eth0') $ifname = $old['BRIDGING']=='yes' ? ($old['BRNAME'] ?: 'br0') : ($old['BONDING']=='yes' ? ($old['BONDNAME'] ?: 'bond0') : $set);
}
}
}
if ($bridging = $ini['eth0']['BRIDGING']=='yes') {
$ini['eth0']['BRNICS'] = str_replace(',',' ',$ini['eth0']['BRNICS']);
$br0 = explode(' ',trim(str_replace('eth0','',$ini['eth0']['BRNICS'])));
// ensure additional NICs in bridge are set free
if ($run && $set=='eth0' && !$bonding) foreach ($br0 as $nic) {
if (isset($old['SYSNICS'])) $nic = ifname($nic);
if ($nic && $nic!=$ifname) exec("/etc/rc.d/rc.inet1 ".escapeshellarg("{$nic}_stop")." >/dev/null");
exec("/etc/rc.d/rc.inet1 ".escapeshellarg("{$ifname}_stop")." >/dev/null");
if ($ini[$set]['BONDING']=='yes') {
// release additional NICs in bond
foreach (bond_nics($ini[$set],$set) as $nic) {
if (isset($old['SYSNICS'])) $nic = ifname($nic);
if ($nic && $nic!=$ifname) exec("/etc/rc.d/rc.inet1 ".escapeshellarg("{$nic}_stop")." >/dev/null");
}
} elseif ($ini[$set]['BRIDGING']=='yes') {
// release additional NICs in bridge
foreach (bridge_nics($ini[$set],$set) as $nic) {
if (isset($old['SYSNICS'])) $nic = ifname($nic);
if ($nic && $nic!=$ifname) exec("/etc/rc.d/rc.inet1 ".escapeshellarg("{$nic}_stop")." >/dev/null");
}
}
}
// create configuration file for all available interfaces
$i = 0; $new = []; $new[] = "# Generated settings:";
foreach ($ini as $name => $port) {
if ($bonding && in_array($name,$bond0)) continue;
if ($bridging && in_array($name,$br0)) continue;
$bridge = $port['BRIDGING']=='yes';
$bonding = $port['BONDING']=='yes';
$bridging = $port['BRIDGING']=='yes';
if ($bonding && in_array($name,bond_nics($port,$name))) continue;
if ($bridging && in_array($name,bridge_nics($port,$name))) continue;
$trunk = $port['TYPE']=='trunk';
$j = 0; $x0 = 0;
$iface = $bridge ? $port['BRNAME'] : ($bonding && $name=='eth0' ? $port['BONDNAME'] : $name);
$iface = $bridging ? $port['BRNAME'] : ($bonding ? $port['BONDNAME'] : $name);
$new[] = "IFNAME[$i]=\"$iface\"";
if ($set==$name) $ifname = $iface;
foreach ($port as $key => $val) {
if (preg_match('/^(TYPE|BONDING$|BRIDGING)/',$key)) continue;
if (!$bonding && preg_match('/^(BONDING_MODE|BONDING_MIIMON|BONDNICS|BONDNAME)/',$key)) continue;
if (!$bridge && preg_match('/^(BRSTP|BRFD|BRNICS|BRNAME)/',$key)) continue;
if (!$bridging && preg_match('/^(BRSTP|BRFD|BRNICS|BRNAME)/',$key)) continue;
list($item,$x) = explode(':',$key,2);
if ($trunk && $x>0 && preg_match('/^(VLANID|USE_DHCP|IPADDR6?|NETMASK6?|GATEWAY6?|METRIC6?|PRIVACY6|DESCRIPTION|PROTOCOL)/',$key)) {
if ($trunk && $x>0 && preg_match('/^(VLANID|USE_DHCP|IPADDR6?|NETMASK6?|GATEWAY6?|METRIC|PRIVACY6|DESCRIPTION|PROTOCOL)/',$key)) {
if ($x0 != $x) {$x0 = $x; $j++;}
$vlan = ",$j]";
} else $vlan = '';
@@ -95,6 +99,7 @@ file_put_contents($cfg,implode("\r\n",$new)."\r\n");
// don't execute when only interface description has changed
if ($run) {
exec("/etc/rc.d/rc.inet1 ".escapeshellarg("{$ifname}_start")." >/dev/null");
sleep(1);
exec("/usr/local/sbin/create_network_ini >/dev/null");
}
exit(0);