[1/3] fff-network: Fix/improve setup of one- and two-port devices

Submitted by Adrian Schmutzler on April 16, 2019, 9:42 p.m.

Details

Message ID 20190416214234.55480-2-freifunk@adrianschmutzler.de
State Superseded
Headers show

Commit Message

Adrian Schmutzler April 16, 2019, 9:42 p.m.
This will be merged into the relevant patches when a new
patchset is sent.

Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
---
 .../files/etc/uci-defaults/22b-config-ports        |  2 +-
 .../files/lib/functions/fff/networksetup           | 30 ++++++++++++----------
 .../fff/fff-network/files/usr/sbin/setoneport      |  5 +++-
 .../fff/fff-network/files/usr/sbin/settwoeth       | 15 ++++++++---
 4 files changed, 34 insertions(+), 18 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/packages/fff/fff-network/files/etc/uci-defaults/22b-config-ports b/src/packages/fff/fff-network/files/etc/uci-defaults/22b-config-ports
index 372a6c59..c1d8381e 100644
--- a/src/packages/fff/fff-network/files/etc/uci-defaults/22b-config-ports
+++ b/src/packages/fff/fff-network/files/etc/uci-defaults/22b-config-ports
@@ -74,7 +74,7 @@  case "$BOARD" in
 	ubnt-nano-m)
 		setupOnePort "eth0" "WAN"
 		setup2ndEth "eth1" "CLIENT"
-		uci -q set "fff.ui.portsetup=twoeth"
+		uci set "fff.ui.portsetup=twoeth"
 		;;
 	cpe210-v2|\
 	cpe210-v3|\
diff --git a/src/packages/fff/fff-network/files/lib/functions/fff/networksetup b/src/packages/fff/fff-network/files/lib/functions/fff/networksetup
index 0478cf59..fca772a6 100644
--- a/src/packages/fff/fff-network/files/lib/functions/fff/networksetup
+++ b/src/packages/fff/fff-network/files/lib/functions/fff/networksetup
@@ -151,12 +151,12 @@  setupOnePort() {
 	elif [ "$ETHMODE" = "CLIENT" ] ; then
 		disableAutoConf "$DEV"
 		uci set network.mesh.ifname="bat0 $DEV"
-		uci set network.wan.ifname="eth1" # eth1 because it is default in config file
+		uci del network.wan.ifname
 		uci del network.ethmesh.ifname
 	else # default=BATMAN
 		disableAutoConf "$DEV"
 		uci set network.mesh.ifname="bat0"
-		uci set network.wan.ifname="eth1" # eth1 because it is default in config file
+		uci del network.wan.ifname
 		uci set network.ethmesh.ifname="$DEV"
 	fi
 	uci commit network
@@ -179,18 +179,22 @@  setup2ndEth() {
 	uci set "network.$DEV=interface"
 	uci set "network.$DEV.ifname=$DEV"
 
-	local meshif="$(uci -q get network.mesh.ifname | sed "s/ *$DEV//")"
-	local ethmeshif="$(uci -q get network.ethmesh.ifname | sed "s/ *$DEV//")"
+	local meshif="$(uci -q get network.mesh.ifname)"
+
 	if [ "$ETHMODE" = "CLIENT" ] ; then
-		uci set network.mesh.ifname="$meshif $DEV"
-		if [ -n "$ethmesif" ]; then
-			uci set network.ethmesh.ifname="$ethmesif"
-		else
-			uci del network.ethmesh.ifname
-		fi
-	else # default=BATMAN
-		uci set network.mesh.ifname="$meshif"
-		uci set network.ethmesh.ifname="$ethmesif $DEV"
+		# Only write if not only there
+		echo "$meshif" | grep -q "$DEV" || uci set network.mesh.ifname="$meshif $DEV"
+	else
+		# Remove if there
+		uci set network.mesh.ifname="$(echo "$meshif" | sed "s/ *$DEV//")"
+	fi
+
+	if [ "$ETHMODE" = "BATMAN" ] ; then
+		# Always overwrite (if both ports are set to BATMAN, only the second will be)
+		uci set network.ethmesh.ifname="$DEV"
+	else
+		uci -q get network.ethmesh.ifname | grep -q "$DEV" && uci del network.ethmesh.ifname
 	fi
+
 	uci commit network
 }
diff --git a/src/packages/fff/fff-network/files/usr/sbin/setoneport b/src/packages/fff/fff-network/files/usr/sbin/setoneport
index 6812bf17..37387827 100755
--- a/src/packages/fff/fff-network/files/usr/sbin/setoneport
+++ b/src/packages/fff/fff-network/files/usr/sbin/setoneport
@@ -10,9 +10,12 @@  fi
 . /lib/functions/fff/networksetup
 
 ETHMODE=$1
+DEV=$2
+
+[ -n "$DEV" ] || DEV="eth0"
 
 if [ "$ETHMODE" = "WAN" ] || [ "$ETHMODE" = "CLIENT" ] || [ "$ETHMODE" = "BATMAN" ]; then
-	setupOnePort "eth0" "$ETHMODE"
+	setupOnePort "$DEV" "$ETHMODE"
 	/etc/init.d/network restart
 	echo "Port updated successfully."
 else
diff --git a/src/packages/fff/fff-network/files/usr/sbin/settwoeth b/src/packages/fff/fff-network/files/usr/sbin/settwoeth
index b2489789..8f82c1ee 100755
--- a/src/packages/fff/fff-network/files/usr/sbin/settwoeth
+++ b/src/packages/fff/fff-network/files/usr/sbin/settwoeth
@@ -1,7 +1,7 @@ 
 #!/bin/sh
 
 if ! [ "$(uci -q get fff.ui.portsetup)" = "twoeth" ] ; then
-	echo "Wrong device. This is for Nanostation M only!"
+	echo "Wrong device. This is for devices with two ethX without switch!"
 	exit 1
 fi
 
@@ -9,6 +9,11 @@  fi
 
 ETHMODE1=$1
 ETHMODE2=$2
+DEV1=$3
+DEV2=$4
+
+[ -n "$DEV1" ] || DEV1="eth0"
+[ -n "$DEV2" ] || DEV2="eth1"
 
 if ! ([ "$ETHMODE1" = "WAN" ] || [ "$ETHMODE1" = "CLIENT" ] || [ "$ETHMODE1" = "BATMAN" ]); then
 	echo "Wrong port mode for first port. Choose one of: WAN, CLIENT, BATMAN"
@@ -18,9 +23,13 @@  if ! ([ "$ETHMODE2" = "CLIENT" ] || [ "$ETHMODE2" = "BATMAN" ]); then
 	echo "Wrong port mode for second port. Choose one of: CLIENT, BATMAN"
 	exit 1
 fi
+if [ "$ETHMODE1" = "BATMAN" ] && [ "$ETHMODE2" = "BATMAN" ]; then
+	echo "Only one port can be set to BATMAN!"
+	exit 1
+fi
 
-setupOnePort "eth0" "$ETHMODE1"
-setup2ndEth "eth1" "$ETHMODE2"
+setupOnePort "$DEV1" "$ETHMODE1"
+setup2ndEth "$DEV2" "$ETHMODE2"
 /etc/init.d/network restart
 echo "Port updated successfully."
 

Comments

Christian Dresel April 22, 2019, 7:54 a.m.
hi

ich kann mir das mergen nicht mehr wirklich vorstellen wo das nun wie
drinnen landet. Ich würde das gerne mal komplett sehen bevor ich da ein
Review drauf werfe. Ansonsten sieht es aber plausibel aus:

Acked-by: Christian Dresel <fff@chrisi01.de>

Gruß

Christian

On 16.04.19 23:42, Adrian Schmutzler wrote:
> This will be merged into the relevant patches when a new
> patchset is sent.
>
> Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
> ---
>  .../files/etc/uci-defaults/22b-config-ports        |  2 +-
>  .../files/lib/functions/fff/networksetup           | 30 ++++++++++++----------
>  .../fff/fff-network/files/usr/sbin/setoneport      |  5 +++-
>  .../fff/fff-network/files/usr/sbin/settwoeth       | 15 ++++++++---
>  4 files changed, 34 insertions(+), 18 deletions(-)
>
> diff --git a/src/packages/fff/fff-network/files/etc/uci-defaults/22b-config-ports b/src/packages/fff/fff-network/files/etc/uci-defaults/22b-config-ports
> index 372a6c59..c1d8381e 100644
> --- a/src/packages/fff/fff-network/files/etc/uci-defaults/22b-config-ports
> +++ b/src/packages/fff/fff-network/files/etc/uci-defaults/22b-config-ports
> @@ -74,7 +74,7 @@ case "$BOARD" in
>  	ubnt-nano-m)
>  		setupOnePort "eth0" "WAN"
>  		setup2ndEth "eth1" "CLIENT"
> -		uci -q set "fff.ui.portsetup=twoeth"
> +		uci set "fff.ui.portsetup=twoeth"
>  		;;
>  	cpe210-v2|\
>  	cpe210-v3|\
> diff --git a/src/packages/fff/fff-network/files/lib/functions/fff/networksetup b/src/packages/fff/fff-network/files/lib/functions/fff/networksetup
> index 0478cf59..fca772a6 100644
> --- a/src/packages/fff/fff-network/files/lib/functions/fff/networksetup
> +++ b/src/packages/fff/fff-network/files/lib/functions/fff/networksetup
> @@ -151,12 +151,12 @@ setupOnePort() {
>  	elif [ "$ETHMODE" = "CLIENT" ] ; then
>  		disableAutoConf "$DEV"
>  		uci set network.mesh.ifname="bat0 $DEV"
> -		uci set network.wan.ifname="eth1" # eth1 because it is default in config file
> +		uci del network.wan.ifname
>  		uci del network.ethmesh.ifname
>  	else # default=BATMAN
>  		disableAutoConf "$DEV"
>  		uci set network.mesh.ifname="bat0"
> -		uci set network.wan.ifname="eth1" # eth1 because it is default in config file
> +		uci del network.wan.ifname
>  		uci set network.ethmesh.ifname="$DEV"
>  	fi
>  	uci commit network
> @@ -179,18 +179,22 @@ setup2ndEth() {
>  	uci set "network.$DEV=interface"
>  	uci set "network.$DEV.ifname=$DEV"
>  
> -	local meshif="$(uci -q get network.mesh.ifname | sed "s/ *$DEV//")"
> -	local ethmeshif="$(uci -q get network.ethmesh.ifname | sed "s/ *$DEV//")"
> +	local meshif="$(uci -q get network.mesh.ifname)"
> +
>  	if [ "$ETHMODE" = "CLIENT" ] ; then
> -		uci set network.mesh.ifname="$meshif $DEV"
> -		if [ -n "$ethmesif" ]; then
> -			uci set network.ethmesh.ifname="$ethmesif"
> -		else
> -			uci del network.ethmesh.ifname
> -		fi
> -	else # default=BATMAN
> -		uci set network.mesh.ifname="$meshif"
> -		uci set network.ethmesh.ifname="$ethmesif $DEV"
> +		# Only write if not only there
> +		echo "$meshif" | grep -q "$DEV" || uci set network.mesh.ifname="$meshif $DEV"
> +	else
> +		# Remove if there
> +		uci set network.mesh.ifname="$(echo "$meshif" | sed "s/ *$DEV//")"
> +	fi
> +
> +	if [ "$ETHMODE" = "BATMAN" ] ; then
> +		# Always overwrite (if both ports are set to BATMAN, only the second will be)
> +		uci set network.ethmesh.ifname="$DEV"
> +	else
> +		uci -q get network.ethmesh.ifname | grep -q "$DEV" && uci del network.ethmesh.ifname
>  	fi
> +
>  	uci commit network
>  }
> diff --git a/src/packages/fff/fff-network/files/usr/sbin/setoneport b/src/packages/fff/fff-network/files/usr/sbin/setoneport
> index 6812bf17..37387827 100755
> --- a/src/packages/fff/fff-network/files/usr/sbin/setoneport
> +++ b/src/packages/fff/fff-network/files/usr/sbin/setoneport
> @@ -10,9 +10,12 @@ fi
>  . /lib/functions/fff/networksetup
>  
>  ETHMODE=$1
> +DEV=$2
> +
> +[ -n "$DEV" ] || DEV="eth0"
>  
>  if [ "$ETHMODE" = "WAN" ] || [ "$ETHMODE" = "CLIENT" ] || [ "$ETHMODE" = "BATMAN" ]; then
> -	setupOnePort "eth0" "$ETHMODE"
> +	setupOnePort "$DEV" "$ETHMODE"
>  	/etc/init.d/network restart
>  	echo "Port updated successfully."
>  else
> diff --git a/src/packages/fff/fff-network/files/usr/sbin/settwoeth b/src/packages/fff/fff-network/files/usr/sbin/settwoeth
> index b2489789..8f82c1ee 100755
> --- a/src/packages/fff/fff-network/files/usr/sbin/settwoeth
> +++ b/src/packages/fff/fff-network/files/usr/sbin/settwoeth
> @@ -1,7 +1,7 @@
>  #!/bin/sh
>  
>  if ! [ "$(uci -q get fff.ui.portsetup)" = "twoeth" ] ; then
> -	echo "Wrong device. This is for Nanostation M only!"
> +	echo "Wrong device. This is for devices with two ethX without switch!"
>  	exit 1
>  fi
>  
> @@ -9,6 +9,11 @@ fi
>  
>  ETHMODE1=$1
>  ETHMODE2=$2
> +DEV1=$3
> +DEV2=$4
> +
> +[ -n "$DEV1" ] || DEV1="eth0"
> +[ -n "$DEV2" ] || DEV2="eth1"
>  
>  if ! ([ "$ETHMODE1" = "WAN" ] || [ "$ETHMODE1" = "CLIENT" ] || [ "$ETHMODE1" = "BATMAN" ]); then
>  	echo "Wrong port mode for first port. Choose one of: WAN, CLIENT, BATMAN"
> @@ -18,9 +23,13 @@ if ! ([ "$ETHMODE2" = "CLIENT" ] || [ "$ETHMODE2" = "BATMAN" ]); then
>  	echo "Wrong port mode for second port. Choose one of: CLIENT, BATMAN"
>  	exit 1
>  fi
> +if [ "$ETHMODE1" = "BATMAN" ] && [ "$ETHMODE2" = "BATMAN" ]; then
> +	echo "Only one port can be set to BATMAN!"
> +	exit 1
> +fi
>  
> -setupOnePort "eth0" "$ETHMODE1"
> -setup2ndEth "eth1" "$ETHMODE2"
> +setupOnePort "$DEV1" "$ETHMODE1"
> +setup2ndEth "$DEV2" "$ETHMODE2"
>  /etc/init.d/network restart
>  echo "Port updated successfully."
>