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