public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Chen Hanxiao <chen_han_xiao@126.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] ns-tools/set_ipv4addr: Use ip command as default
Date: Thu, 10 Sep 2020 09:56:31 -0400	[thread overview]
Message-ID: <1599746191-2012-1-git-send-email-chen_han_xiao@126.com> (raw)

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(-)

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
-- 
1.8.3.1


             reply	other threads:[~2020-09-10 13:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-10 13:56 Chen Hanxiao [this message]
2020-09-11  8:37 ` [LTP] [PATCH] ns-tools/set_ipv4addr: Use ip command as default Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1599746191-2012-1-git-send-email-chen_han_xiao@126.com \
    --to=chen_han_xiao@126.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox