1 #!/bin/bash 1 #!/bin/bash 2 # SPDX-License-Identifier: GPL-2.0-only 2 # SPDX-License-Identifier: GPL-2.0-only 3 3 4 source ../../../net/net_helper.sh 4 source ../../../net/net_helper.sh 5 5 6 NSIM_DEV_1_ID=$((256 + RANDOM % 256)) 6 NSIM_DEV_1_ID=$((256 + RANDOM % 256)) 7 NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netd 7 NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID 8 NSIM_DEV_2_ID=$((512 + RANDOM % 256)) 8 NSIM_DEV_2_ID=$((512 + RANDOM % 256)) 9 NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netd 9 NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_2_ID 10 10 11 NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device 11 NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device 12 NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device 12 NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device 13 NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_devi 13 NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device 14 NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_ 14 NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_device 15 15 16 socat_check() 16 socat_check() 17 { 17 { 18 if [ ! -x "$(command -v socat)" ]; the 18 if [ ! -x "$(command -v socat)" ]; then 19 echo "socat command not found. 19 echo "socat command not found. Skipping test" 20 return 1 20 return 1 21 fi 21 fi 22 22 23 return 0 23 return 0 24 } 24 } 25 25 26 setup_ns() 26 setup_ns() 27 { 27 { 28 set -e 28 set -e 29 ip netns add nssv 29 ip netns add nssv 30 ip netns add nscl 30 ip netns add nscl 31 31 32 NSIM_DEV_1_NAME=$(find $NSIM_DEV_1_SYS 32 NSIM_DEV_1_NAME=$(find $NSIM_DEV_1_SYS/net -maxdepth 1 -type d ! \ 33 -path $NSIM_DEV_1_SYS/net -exe 33 -path $NSIM_DEV_1_SYS/net -exec basename {} \;) 34 NSIM_DEV_2_NAME=$(find $NSIM_DEV_2_SYS 34 NSIM_DEV_2_NAME=$(find $NSIM_DEV_2_SYS/net -maxdepth 1 -type d ! \ 35 -path $NSIM_DEV_2_SYS/net -exe 35 -path $NSIM_DEV_2_SYS/net -exec basename {} \;) 36 36 37 ip link set $NSIM_DEV_1_NAME netns nss 37 ip link set $NSIM_DEV_1_NAME netns nssv 38 ip link set $NSIM_DEV_2_NAME netns nsc 38 ip link set $NSIM_DEV_2_NAME netns nscl 39 39 40 ip netns exec nssv ip addr add '192.16 40 ip netns exec nssv ip addr add '192.168.1.1/24' dev $NSIM_DEV_1_NAME 41 ip netns exec nscl ip addr add '192.16 41 ip netns exec nscl ip addr add '192.168.1.2/24' dev $NSIM_DEV_2_NAME 42 42 43 ip netns exec nssv ip link set dev $NS 43 ip netns exec nssv ip link set dev $NSIM_DEV_1_NAME up 44 ip netns exec nscl ip link set dev $NS 44 ip netns exec nscl ip link set dev $NSIM_DEV_2_NAME up 45 set +e 45 set +e 46 } 46 } 47 47 48 cleanup_ns() 48 cleanup_ns() 49 { 49 { 50 ip netns del nscl 50 ip netns del nscl 51 ip netns del nssv 51 ip netns del nssv 52 } 52 } 53 53 54 ### 54 ### 55 ### Code start 55 ### Code start 56 ### 56 ### 57 57 58 socat_check || exit 4 58 socat_check || exit 4 59 59 60 modprobe netdevsim 60 modprobe netdevsim 61 61 62 # linking 62 # linking 63 63 64 echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW 64 echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW 65 echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW 65 echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW 66 udevadm settle 66 udevadm settle 67 67 68 setup_ns 68 setup_ns 69 69 70 NSIM_DEV_1_FD=$((256 + RANDOM % 256)) 70 NSIM_DEV_1_FD=$((256 + RANDOM % 256)) 71 exec {NSIM_DEV_1_FD}</var/run/netns/nssv 71 exec {NSIM_DEV_1_FD}</var/run/netns/nssv 72 NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys 72 NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex) 73 73 74 NSIM_DEV_2_FD=$((256 + RANDOM % 256)) 74 NSIM_DEV_2_FD=$((256 + RANDOM % 256)) 75 exec {NSIM_DEV_2_FD}</var/run/netns/nscl 75 exec {NSIM_DEV_2_FD}</var/run/netns/nscl 76 NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys 76 NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex) 77 77 78 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_D 78 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:2000" > $NSIM_DEV_SYS_LINK 2>/dev/null 79 if [ $? -eq 0 ]; then 79 if [ $? -eq 0 ]; then 80 echo "linking with non-existent netdev 80 echo "linking with non-existent netdevsim should fail" 81 cleanup_ns 81 cleanup_ns 82 exit 1 82 exit 1 83 fi 83 fi 84 84 85 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX 2000:$N 85 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX 2000:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null 86 if [ $? -eq 0 ]; then 86 if [ $? -eq 0 ]; then 87 echo "linking with non-existent netnsi 87 echo "linking with non-existent netnsid should fail" 88 cleanup_ns 88 cleanup_ns 89 exit 1 89 exit 1 90 fi 90 fi 91 91 92 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_D 92 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null 93 if [ $? -eq 0 ]; then 93 if [ $? -eq 0 ]; then 94 echo "linking with self should fail" 94 echo "linking with self should fail" 95 cleanup_ns 95 cleanup_ns 96 exit 1 96 exit 1 97 fi 97 fi 98 98 99 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_D 99 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 100 if [ $? -ne 0 ]; then 100 if [ $? -ne 0 ]; then 101 echo "linking netdevsim1 with netdevsi 101 echo "linking netdevsim1 with netdevsim2 should succeed" 102 cleanup_ns 102 cleanup_ns 103 exit 1 103 exit 1 104 fi 104 fi 105 105 106 # argument error checking 106 # argument error checking 107 107 108 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_D 108 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:a" > $NSIM_DEV_SYS_LINK 2>/dev/null 109 if [ $? -eq 0 ]; then 109 if [ $? -eq 0 ]; then 110 echo "invalid arg should fail" 110 echo "invalid arg should fail" 111 cleanup_ns 111 cleanup_ns 112 exit 1 112 exit 1 113 fi 113 fi 114 114 115 # send/recv packets 115 # send/recv packets 116 116 117 tmp_file=$(mktemp) 117 tmp_file=$(mktemp) 118 ip netns exec nssv socat TCP-LISTEN:1234,fork 118 ip netns exec nssv socat TCP-LISTEN:1234,fork $tmp_file & 119 pid=$! 119 pid=$! 120 res=0 120 res=0 121 121 122 wait_local_port_listen nssv 1234 tcp 122 wait_local_port_listen nssv 1234 tcp 123 123 124 echo "HI" | ip netns exec nscl socat STDIN TCP 124 echo "HI" | ip netns exec nscl socat STDIN TCP:192.168.1.1:1234 125 125 126 count=$(cat $tmp_file | wc -c) 126 count=$(cat $tmp_file | wc -c) 127 if [[ $count -ne 3 ]]; then 127 if [[ $count -ne 3 ]]; then 128 echo "expected 3 bytes, got $count" 128 echo "expected 3 bytes, got $count" 129 res=1 129 res=1 130 fi 130 fi 131 131 132 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSI 132 echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_UNLINK 133 133 134 echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_DEL 134 echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_DEL 135 135 136 kill $pid 136 kill $pid 137 echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_DEL 137 echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_DEL 138 138 139 cleanup_ns 139 cleanup_ns 140 140 141 modprobe -r netdevsim 141 modprobe -r netdevsim 142 142 143 exit $res 143 exit $res
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.