diff mbox series

[net-next,v7,3/4] netdevsim: add selftest for forwarding skb between connected ports

Message ID 20240127040354.944744-4-dw@davidwei.uk (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series netdevsim: link and forward skbs between ports | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 1 this patch: 0
netdev/cc_maintainers success CCed 0 of 0 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success net selftest script(s) already in Makefile
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 100 exceeds 80 columns WARNING: line length of 103 exceeds 80 columns WARNING: line length of 82 exceeds 80 columns WARNING: line length of 98 exceeds 80 columns WARNING: line length of 99 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

David Wei Jan. 27, 2024, 4:03 a.m. UTC
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/peer.sh   | 127 ++++++++++++++++++
 1 file changed, 127 insertions(+)
 create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.sh

Comments

Simon Horman Jan. 29, 2024, 8:34 p.m. UTC | #1
On Fri, Jan 26, 2024 at 08:03:53PM -0800, David Wei wrote:
> 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/peer.sh   | 127 ++++++++++++++++++
>  1 file changed, 127 insertions(+)
>  create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.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..05f3cefa53f3
> --- /dev/null
> +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh
> @@ -0,0 +1,127 @@
> +#!/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_1_DFS=/sys/kernel/debug/netdevsim/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_2_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_DEV_2_ID

nit: NSIM_DEV_1_DFS and SIM_DEV_2_DFS appear to be unused.

Flagged by shellcheck.

...
David Wei Jan. 30, 2024, 6:57 p.m. UTC | #2
On 2024-01-29 12:34, Simon Horman wrote:
> On Fri, Jan 26, 2024 at 08:03:53PM -0800, David Wei wrote:
>> 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/peer.sh   | 127 ++++++++++++++++++
>>  1 file changed, 127 insertions(+)
>>  create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.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..05f3cefa53f3
>> --- /dev/null
>> +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh
>> @@ -0,0 +1,127 @@
>> +#!/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_1_DFS=/sys/kernel/debug/netdevsim/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_2_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_DEV_2_ID
> 
> nit: NSIM_DEV_1_DFS and SIM_DEV_2_DFS appear to be unused.
> 
> Flagged by shellcheck.
> 
> ...

Hi Simon, thanks for flagging this, these were leftover from previous
changes. I'll remove them in the next version.
Simon Horman Feb. 2, 2024, 10:22 a.m. UTC | #3
On Tue, Jan 30, 2024 at 10:57:45AM -0800, David Wei wrote:
> On 2024-01-29 12:34, Simon Horman wrote:
> > On Fri, Jan 26, 2024 at 08:03:53PM -0800, David Wei wrote:
> >> 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/peer.sh   | 127 ++++++++++++++++++
> >>  1 file changed, 127 insertions(+)
> >>  create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.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..05f3cefa53f3
> >> --- /dev/null
> >> +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh
> >> @@ -0,0 +1,127 @@
> >> +#!/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_1_DFS=/sys/kernel/debug/netdevsim/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_2_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_DEV_2_ID
> > 
> > nit: NSIM_DEV_1_DFS and SIM_DEV_2_DFS appear to be unused.
> > 
> > Flagged by shellcheck.
> > 
> > ...
> 
> Hi Simon, thanks for flagging this, these were leftover from previous
> changes. I'll remove them in the next version.

Thanks David,

much appreciated.
diff mbox series

Patch

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..05f3cefa53f3
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh
@@ -0,0 +1,127 @@ 
+#!/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_1_DFS=/sys/kernel/debug/netdevsim/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_2_DFS=/sys/kernel/debug/netdevsim/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_NETNSID=$(ip netns list-id | grep nssv | awk '{print $2}')
+NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex)
+
+NSIM_DEV_2_NETNSID=$(ip netns list-id | grep nscl | awk '{print $2}')
+NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex)
+
+echo "$NSIM_DEV_1_NETNSID:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_NETNSID:20" > $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_NETNSID:$NSIM_DEV_1_IFIDX 20:$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_NETNSID:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_NETNSID:$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_NETNSID:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_NETNSID: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_NETNSID:$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