diff mbox series

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

Message ID 20231210010448.816126-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/cc_maintainers fail 3 maintainers not CCed: davidhwei@meta.com linux-kselftest@vger.kernel.org shuah@kernel.org
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: From:/Signed-off-by: email address mismatch: 'From: David Wei <davidhwei@meta.com>' != 'Signed-off-by: David Wei <dw@davidwei.uk>' WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
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 Dec. 10, 2023, 1:04 a.m. UTC
From: David Wei <davidhwei@meta.com>

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   | 111 ++++++++++++++++++
 1 file changed, 111 insertions(+)
 create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.sh

Comments

Jakub Kicinski Dec. 12, 2023, 8:29 p.m. UTC | #1
On Sat,  9 Dec 2023 17:04:48 -0800 David Wei wrote:
> From: David Wei <davidhwei@meta.com>
> 
> 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   | 111 ++++++++++++++++++
>  1 file changed, 111 insertions(+)
>  create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.sh

You need to include this script in the Makefile so it gets run

> 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..d1d59a932174
> --- /dev/null
> +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh
> @@ -0,0 +1,111 @@
> +#!/bin/bash
> +# SPDX-License-Identifier: GPL-2.0-only
> +
> +NSIM_DEV_SYS=/sys/bus/netdevsim
> +NSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim
> +
> +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
> +
> +	ip link set eni1np1 netns nssv
> +	ip link set eni2np1 netns nscl

This assumes you have systemd renaming interfaces.
I can find out what the netdev is called from sysfs.
After you create the nsim device in its sysfs dir
there will be a dir "net" and in it you'll have
a subdir with the same name as the netdev.

> +	ip netns exec nssv ip addr add '192.168.1.1/24' dev eni1np1
> +	ip netns exec nscl ip addr add '192.168.1.2/24' dev eni2np1
> +
> +	ip netns exec nssv ip link set dev eni1np1 up
> +	ip netns exec nscl ip link set dev eni2np1 up
> +	set +e
> +}
> +
> +cleanup_ns()
> +{
> +	ip netns del nscl
> +	ip netns del nssv
> +}
> +
> +###
> +### Code start
> +###
> +
> +modprobe netdevsim
> +
> +# linking
> +
> +echo 1 > ${NSIM_DEV_SYS}/new_device

Use $RANDOM to generate a random device ID.

> +echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
> +if [ $? -eq 0 ]; then
> +	echo "linking with non-existent netdevsim should fail"
> +	exit 1
> +fi
> +
> +echo 2 > /sys/bus/netdevsim/new_device
> +
> +echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer
> +if [ $? -ne 0 ]; then
> +	echo "linking netdevsim1 port0 with netdevsim2 port0 should succeed"
> +	exit 1
> +fi
> +
> +# argument error checking
> +
> +echo "2 1" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
> +if [ $? -eq 0 ]; then
> +	echo "linking with non-existent port in a netdevsim should fail"
> +	exit 1
> +fi
> +
> +echo "1 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
> +if [ $? -eq 0 ]; then
> +	echo "linking with self should fail"
> +	exit 1
> +fi
> +
> +echo "2 a" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
> +if [ $? -eq 0 ]; then
> +	echo "invalid arg should fail"
> +	exit 1
> +fi
> +
> +# send/recv packets
> +
> +socat_check || exit 1

There's a magic return value for skipping kselftests, 1 means fail.
David Wei Dec. 13, 2023, 12:38 a.m. UTC | #2
On 2023-12-12 12:29, Jakub Kicinski wrote:
> On Sat,  9 Dec 2023 17:04:48 -0800 David Wei wrote:
>> From: David Wei <davidhwei@meta.com>
>>
>> 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   | 111 ++++++++++++++++++
>>  1 file changed, 111 insertions(+)
>>  create mode 100755 tools/testing/selftests/drivers/net/netdevsim/peer.sh
> 
> You need to include this script in the Makefile so it gets run

Thanks, will address.

> 
>> 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..d1d59a932174
>> --- /dev/null
>> +++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh
>> @@ -0,0 +1,111 @@
>> +#!/bin/bash
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +
>> +NSIM_DEV_SYS=/sys/bus/netdevsim
>> +NSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim
>> +
>> +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
>> +
>> +	ip link set eni1np1 netns nssv
>> +	ip link set eni2np1 netns nscl
> 
> This assumes you have systemd renaming interfaces.
> I can find out what the netdev is called from sysfs.
> After you create the nsim device in its sysfs dir
> there will be a dir "net" and in it you'll have
> a subdir with the same name as the netdev.

Thanks, will fix.

> 
>> +	ip netns exec nssv ip addr add '192.168.1.1/24' dev eni1np1
>> +	ip netns exec nscl ip addr add '192.168.1.2/24' dev eni2np1
>> +
>> +	ip netns exec nssv ip link set dev eni1np1 up
>> +	ip netns exec nscl ip link set dev eni2np1 up
>> +	set +e
>> +}
>> +
>> +cleanup_ns()
>> +{
>> +	ip netns del nscl
>> +	ip netns del nssv
>> +}
>> +
>> +###
>> +### Code start
>> +###
>> +
>> +modprobe netdevsim
>> +
>> +# linking
>> +
>> +echo 1 > ${NSIM_DEV_SYS}/new_device
> 
> Use $RANDOM to generate a random device ID.

Will do.

> 
>> +echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
>> +if [ $? -eq 0 ]; then
>> +	echo "linking with non-existent netdevsim should fail"
>> +	exit 1
>> +fi
>> +
>> +echo 2 > /sys/bus/netdevsim/new_device
>> +
>> +echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer
>> +if [ $? -ne 0 ]; then
>> +	echo "linking netdevsim1 port0 with netdevsim2 port0 should succeed"
>> +	exit 1
>> +fi
>> +
>> +# argument error checking
>> +
>> +echo "2 1" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
>> +if [ $? -eq 0 ]; then
>> +	echo "linking with non-existent port in a netdevsim should fail"
>> +	exit 1
>> +fi
>> +
>> +echo "1 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
>> +if [ $? -eq 0 ]; then
>> +	echo "linking with self should fail"
>> +	exit 1
>> +fi
>> +
>> +echo "2 a" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
>> +if [ $? -eq 0 ]; then
>> +	echo "invalid arg should fail"
>> +	exit 1
>> +fi
>> +
>> +# send/recv packets
>> +
>> +socat_check || exit 1
> 
> There's a magic return value for skipping kselftests, 1 means fail.

Thanks, will fix.
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..d1d59a932174
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/netdevsim/peer.sh
@@ -0,0 +1,111 @@ 
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+
+NSIM_DEV_SYS=/sys/bus/netdevsim
+NSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim
+
+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
+
+	ip link set eni1np1 netns nssv
+	ip link set eni2np1 netns nscl
+
+	ip netns exec nssv ip addr add '192.168.1.1/24' dev eni1np1
+	ip netns exec nscl ip addr add '192.168.1.2/24' dev eni2np1
+
+	ip netns exec nssv ip link set dev eni1np1 up
+	ip netns exec nscl ip link set dev eni2np1 up
+	set +e
+}
+
+cleanup_ns()
+{
+	ip netns del nscl
+	ip netns del nssv
+}
+
+###
+### Code start
+###
+
+modprobe netdevsim
+
+# linking
+
+echo 1 > ${NSIM_DEV_SYS}/new_device
+
+echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
+if [ $? -eq 0 ]; then
+	echo "linking with non-existent netdevsim should fail"
+	exit 1
+fi
+
+echo 2 > /sys/bus/netdevsim/new_device
+
+echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer
+if [ $? -ne 0 ]; then
+	echo "linking netdevsim1 port0 with netdevsim2 port0 should succeed"
+	exit 1
+fi
+
+# argument error checking
+
+echo "2 1" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
+if [ $? -eq 0 ]; then
+	echo "linking with non-existent port in a netdevsim should fail"
+	exit 1
+fi
+
+echo "1 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
+if [ $? -eq 0 ]; then
+	echo "linking with self should fail"
+	exit 1
+fi
+
+echo "2 a" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
+if [ $? -eq 0 ]; then
+	echo "invalid arg should fail"
+	exit 1
+fi
+
+# send/recv packets
+
+socat_check || exit 1
+
+setup_ns
+
+tmp_file=$(mktemp)
+ip netns exec nssv socat TCP-LISTEN:1234,fork $tmp_file &
+pid=$!
+
+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"
+	exit 1
+fi
+
+echo 2 > ${NSIM_DEV_SYS}/del_device
+
+kill $pid
+echo 1 > ${NSIM_DEV_SYS}/del_device
+
+cleanup_ns
+
+modprobe -r netdevsim
+
+exit 0