Message ID | 20240210003240.847392-4-dw@davidwei.uk (mailing list archive) |
---|---|
State | Superseded |
Delegated to: | Netdev Maintainers |
Headers | show |
Series | netdevsim: link and forward skbs between ports | expand |
On 10/02/2024 01:32, David Wei wrote: > +### > +### Code start > +### > + > +modprobe netdevsim > + > +# linking > + > +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW > +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW > + > +setup_ns > + > +NSIM_DEV_1_FD=$((RANDOM % 1024)) > +exec {NSIM_DEV_1_FD}</var/run/netns/nssv > +NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex) > + > +NSIM_DEV_2_FD=$((RANDOM % 1024)) > +exec {NSIM_DEV_2_FD}</var/run/netns/nscl > +NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex) > + > +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:2000" > $NSIM_DEV_SYS_LINK 2>/dev/null > +if [ $? -eq 0 ]; then > + echo "linking with non-existent netdevsim should fail" > + exit 1 > +fi > + > +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX 2000:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null > +if [ $? -eq 0 ]; then > + echo "linking with non-existent netnsid should fail" > + exit 1 > +fi > + > +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null > +if [ $? -eq 0 ]; then > + echo "linking with self should fail" > + exit 1 > +fi > + > +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK > +if [ $? -ne 0 ]; then > + echo "linking netdevsim1 with netdevsim2 should succeed" > + exit 1 > +fi > + > +# argument error checking > + > +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:a" > $NSIM_DEV_SYS_LINK 2>/dev/null > +if [ $? -eq 0 ]; then > + echo "invalid arg should fail" > + exit 1 > +fi > + > +# send/recv packets > + > +socat_check || exit 4 This check will cause the script to exit without cleaning up the devices and namespaces. Move it to the top, or cleanup on error Thanks, Maciek
On 10/02/2024 09:48, Maciek Machnikowski wrote: > > On 10/02/2024 01:32, David Wei wrote: >> +### >> +### Code start >> +### >> + >> +modprobe netdevsim >> + >> +# linking >> + >> +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW >> +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW >> + >> +setup_ns >> + >> +NSIM_DEV_1_FD=$((RANDOM % 1024)) >> +exec {NSIM_DEV_1_FD}</var/run/netns/nssv >> +NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex) >> + >> +NSIM_DEV_2_FD=$((RANDOM % 1024)) >> +exec {NSIM_DEV_2_FD}</var/run/netns/nscl >> +NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex) Can we change these to: NSIM_DEV_1_FD=$((256+(RANDOM % 256))) NSIM_DEV_2_FD=$((512+(RANDOM % 256))) to prevent a 1/1024 chance of drawing the same indexes? >> + >> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:2000" > $NSIM_DEV_SYS_LINK 2>/dev/null >> +if [ $? -eq 0 ]; then >> + echo "linking with non-existent netdevsim should fail" >> + exit 1 >> +fi >> + >> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX 2000:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null >> +if [ $? -eq 0 ]; then >> + echo "linking with non-existent netnsid should fail" >> + exit 1 >> +fi >> + >> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null >> +if [ $? -eq 0 ]; then >> + echo "linking with self should fail" >> + exit 1 >> +fi >> + >> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK >> +if [ $? -ne 0 ]; then >> + echo "linking netdevsim1 with netdevsim2 should succeed" >> + exit 1 >> +fi >> + >> +# argument error checking >> + >> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:a" > $NSIM_DEV_SYS_LINK 2>/dev/null >> +if [ $? -eq 0 ]; then >> + echo "invalid arg should fail" >> + exit 1 >> +fi >> + >> +# send/recv packets >> + >> +socat_check || exit 4 > > This check will cause the script to exit without cleaning up the devices > and namespaces. Move it to the top, or cleanup on error > > Thanks, > Maciek >
On 2024-02-10 10:53, Maciek Machnikowski wrote: > > > On 10/02/2024 09:48, Maciek Machnikowski wrote: >> >> On 10/02/2024 01:32, David Wei wrote: >>> +### >>> +### Code start >>> +### >>> + >>> +modprobe netdevsim >>> + >>> +# linking >>> + >>> +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW >>> +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW >>> + >>> +setup_ns >>> + >>> +NSIM_DEV_1_FD=$((RANDOM % 1024)) >>> +exec {NSIM_DEV_1_FD}</var/run/netns/nssv >>> +NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex) >>> + >>> +NSIM_DEV_2_FD=$((RANDOM % 1024)) >>> +exec {NSIM_DEV_2_FD}</var/run/netns/nscl >>> +NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex) > Can we change these to: > NSIM_DEV_1_FD=$((256+(RANDOM % 256))) > NSIM_DEV_2_FD=$((512+(RANDOM % 256))) > > to prevent a 1/1024 chance of drawing the same indexes? Yes, that is a good idea, thank you. > >>> + >>> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:2000" > $NSIM_DEV_SYS_LINK 2>/dev/null >>> +if [ $? -eq 0 ]; then >>> + echo "linking with non-existent netdevsim should fail" >>> + exit 1 >>> +fi >>> + >>> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX 2000:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null >>> +if [ $? -eq 0 ]; then >>> + echo "linking with non-existent netnsid should fail" >>> + exit 1 >>> +fi >>> + >>> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null >>> +if [ $? -eq 0 ]; then >>> + echo "linking with self should fail" >>> + exit 1 >>> +fi >>> + >>> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK >>> +if [ $? -ne 0 ]; then >>> + echo "linking netdevsim1 with netdevsim2 should succeed" >>> + exit 1 >>> +fi >>> + >>> +# argument error checking >>> + >>> +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:a" > $NSIM_DEV_SYS_LINK 2>/dev/null >>> +if [ $? -eq 0 ]; then >>> + echo "invalid arg should fail" >>> + exit 1 >>> +fi >>> + >>> +# send/recv packets >>> + >>> +socat_check || exit 4 >> >> This check will cause the script to exit without cleaning up the devices >> and namespaces. Move it to the top, or cleanup on error >> >> Thanks, >> Maciek >>
diff --git a/tools/testing/selftests/drivers/net/netdevsim/Makefile b/tools/testing/selftests/drivers/net/netdevsim/Makefile index 7a29a05bea8b..5bace0b7fb57 100644 --- a/tools/testing/selftests/drivers/net/netdevsim/Makefile +++ b/tools/testing/selftests/drivers/net/netdevsim/Makefile @@ -10,6 +10,7 @@ TEST_PROGS = devlink.sh \ fib.sh \ hw_stats_l3.sh \ nexthop.sh \ + peer.sh \ psample.sh \ tc-mq-visibility.sh \ udp_tunnel_nic.sh \ diff --git a/tools/testing/selftests/drivers/net/netdevsim/peer.sh b/tools/testing/selftests/drivers/net/netdevsim/peer.sh new file mode 100755 index 000000000000..07f46d195954 --- /dev/null +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh @@ -0,0 +1,133 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only + +NSIM_DEV_1_ID=$((RANDOM % 1024)) +NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID +NSIM_DEV_2_ID=$((RANDOM % 1024)) +NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_2_ID + +NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device +NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device +NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device +NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_device + +socat_check() +{ + if [ ! -x "$(command -v socat)" ]; then + echo "socat command not found. Skipping test" + return 1 + fi + + return 0 +} + +setup_ns() +{ + set -e + ip netns add nssv + ip netns add nscl + + NSIM_DEV_1_NAME=$(find $NSIM_DEV_1_SYS/net -maxdepth 1 -type d ! \ + -path $NSIM_DEV_1_SYS/net -exec basename {} \;) + NSIM_DEV_2_NAME=$(find $NSIM_DEV_2_SYS/net -maxdepth 1 -type d ! \ + -path $NSIM_DEV_2_SYS/net -exec basename {} \;) + + ip link set $NSIM_DEV_1_NAME netns nssv + ip link set $NSIM_DEV_2_NAME netns nscl + + ip netns exec nssv ip addr add '192.168.1.1/24' dev $NSIM_DEV_1_NAME + ip netns exec nscl ip addr add '192.168.1.2/24' dev $NSIM_DEV_2_NAME + + ip netns exec nssv ip link set dev $NSIM_DEV_1_NAME up + ip netns exec nscl ip link set dev $NSIM_DEV_2_NAME up + set +e +} + +cleanup_ns() +{ + ip netns del nscl + ip netns del nssv +} + +### +### Code start +### + +modprobe netdevsim + +# linking + +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW + +setup_ns + +NSIM_DEV_1_FD=$((RANDOM % 1024)) +exec {NSIM_DEV_1_FD}</var/run/netns/nssv +NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex) + +NSIM_DEV_2_FD=$((RANDOM % 1024)) +exec {NSIM_DEV_2_FD}</var/run/netns/nscl +NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex) + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:2000" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netdevsim should fail" + exit 1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX 2000:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with non-existent netnsid should fail" + exit 1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "linking with self should fail" + exit 1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK +if [ $? -ne 0 ]; then + echo "linking netdevsim1 with netdevsim2 should succeed" + exit 1 +fi + +# argument error checking + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:a" > $NSIM_DEV_SYS_LINK 2>/dev/null +if [ $? -eq 0 ]; then + echo "invalid arg should fail" + exit 1 +fi + +# send/recv packets + +socat_check || exit 4 + +tmp_file=$(mktemp) +ip netns exec nssv socat TCP-LISTEN:1234,fork $tmp_file & +pid=$! +res=0 + +echo "HI" | ip netns exec nscl socat STDIN TCP:192.168.1.1:1234 + +count=$(cat $tmp_file | wc -c) +if [[ $count -ne 3 ]]; then + echo "expected 3 bytes, got $count" + res=1 +fi + +echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_UNLINK + +echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_DEL + +kill $pid +echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_DEL + +cleanup_ns + +modprobe -r netdevsim + +exit $res
Connect two netdevsim ports in different namespaces together, then send packets between them using socat. Signed-off-by: David Wei <dw@davidwei.uk> --- .../selftests/drivers/net/netdevsim/Makefile | 1 + .../selftests/drivers/net/netdevsim/peer.sh | 133 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.sh