diff mbox series

ns-tools/set_ipv4addr: Use ip command as default

Message ID 1599746191-2012-1-git-send-email-chen_han_xiao@126.com
State Rejected
Headers show
Series ns-tools/set_ipv4addr: Use ip command as default | expand

Commit Message

Chen Hanxiao Sept. 10, 2020, 1:56 p.m. UTC
From: Chen Hanxiao <chenhx.fnst@cn.fujitsu.com>

Set iproute as the default as ifconfig
has been deprecated on some of the distributions.

Signed-off-by: Chen Hanxiao <chenhx.fnst@cn.fujitsu.com>
---
 testcases/network/stress/ns-tools/set_ipv4addr | 40 +++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 7 deletions(-)

Comments

Petr Vorel Sept. 11, 2020, 8:37 a.m. UTC | #1
Hi Chen,

> Set iproute as the default as ifconfig
> has been deprecated on some of the distributions.

>  testcases/network/stress/ns-tools/set_ipv4addr | 40 +++++++++++++++++++++-----

Thanks for your patch. While I appreciate you work on LTP networking tests, I'm
against this change for a simple reason: everything which uses ns-tools needs to
be rewritten to use new LTP network shell API (tst_net.sh). Only very simple
fixes for really broken things like 4cf2f2bfd should be accepted.

Thus, please put your effort into rewriting these tests.
FYI Although it's a low priority for me I plan to work on these (I have wip at
least for rmmod). Feel free to join this effort.
See https://github.com/linux-test-project/ltp/issues/310

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/network/stress/ns-tools/set_ipv4addr b/testcases/network/stress/ns-tools/set_ipv4addr
index 1ec0769..baf5d4a 100644
--- a/testcases/network/stress/ns-tools/set_ipv4addr
+++ b/testcases/network/stress/ns-tools/set_ipv4addr
@@ -57,6 +57,8 @@  export LTPROOT
 # Check the environmanet variable for the test
 . check_envval || exit 1
 
+CMD="${CMD:-ip}"
+
 # Arguments
 if [ $# -ne 4 ]; then
     echo "Usage: $0 host_type link_num network_portion host_portion" >&2
@@ -77,17 +79,41 @@  fi
 addr=${network_part}.${host_part}
 netmask=`echo $network_part | sed "s/[[:digit:]]*/255/g"`.`echo $host_part | sed "s/[[:digit:]]*/0/g"`
 broadcast=${network_part}.`echo $host_part | sed "s/[[:digit:]]*/255/g"`
+prefix=0
+
+netmask_to_prefix()
+{
+    bits=0
+    for octet in $(echo $1| sed 's/\./ /g'); do
+        binbits=$(echo "obase=2; ibase=10; ${octet}"| bc | sed 's/0//g')
+        let bits+=${#binbits}
+    done
+    prefix=$bits
+}
+
+netmask_to_prefix $netmask
 
 # Assign IPv4 address to the interface belongs the link_num Test Link
 ifname=`get_ifname $host_type $link_num` || exit 1
 
-if [ $host_type = lhost ]; then
-    ifconfig $ifname up
-    ifconfig $ifname $addr netmask $netmask broadcast $broadcast
-    ret=$?
-else
-    ret=`$LTP_RSH $RHOST '( PATH=/sbin:/usr/sbin:$PATH ; ifconfig '$ifname' up && ifconfig '$ifname $addr' netmask '$netmask' broadcast '$broadcast' ) >/dev/null 2>&1; echo $?'`
-fi
+case $CMD in
+    ip)
+        if [ $host_type = lhost ]; then
+            ip link set $ifname up
+            ip address add $addr'/'$prefix dev $ifname broadcast $broadcast
+            ret=$?
+        else
+            ret=`$LTP_RSH $RHOST '( PATH=/sbin:/usr/sbin:$PATH ; ip link set '$ifname' up && ip address add '$addr'/'$prefix' dev '$ifname' broadcast '$broadcast' ) >/dev/null 2>&1; echo $?'`
+        fi ;;
+ifconfig)
+        if [ $host_type = lhost ]; then
+            ifconfig $ifname up
+            ifconfig $ifname $addr netmask $netmask broadcast $broadcast
+            ret=$?
+        else
+            ret=`$LTP_RSH $RHOST '( PATH=/sbin:/usr/sbin:$PATH ; ifconfig '$ifname' up && ifconfig '$ifname $addr' netmask '$netmask' broadcast '$broadcast' ) >/dev/null 2>&1; echo $?'`
+        fi ;;
+esac
 
 if [ $ret -gt 0 ]; then
     echo "Cannot set $addr to $ifname" >&2