Skip to content

Commit b83c2a8

Browse files
spikehNipaLocal
authored and
NipaLocal
committed
netdevsim: add selftest for forwarding skb between connected ports
Connect two netdevsim ports in different namespaces together, then send packets between them using socat. Signed-off-by: David Wei <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 8eab49f commit b83c2a8

File tree

1 file changed

+111
-0
lines changed
  • tools/testing/selftests/drivers/net/netdevsim

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0-only
3+
4+
NSIM_DEV_SYS=/sys/bus/netdevsim
5+
NSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim
6+
7+
socat_check()
8+
{
9+
if [ ! -x "$(command -v socat)" ]; then
10+
echo "socat command not found. Skipping test"
11+
return 1
12+
fi
13+
14+
return 0
15+
}
16+
17+
setup_ns()
18+
{
19+
set -e
20+
ip netns add nssv
21+
ip netns add nscl
22+
23+
ip link set eni1np1 netns nssv
24+
ip link set eni2np1 netns nscl
25+
26+
ip netns exec nssv ip addr add '192.168.1.1/24' dev eni1np1
27+
ip netns exec nscl ip addr add '192.168.1.2/24' dev eni2np1
28+
29+
ip netns exec nssv ip link set dev eni1np1 up
30+
ip netns exec nscl ip link set dev eni2np1 up
31+
set +e
32+
}
33+
34+
cleanup_ns()
35+
{
36+
ip netns del nscl
37+
ip netns del nssv
38+
}
39+
40+
###
41+
### Code start
42+
###
43+
44+
modprobe netdevsim
45+
46+
# linking
47+
48+
echo 1 > ${NSIM_DEV_SYS}/new_device
49+
50+
echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
51+
if [ $? -eq 0 ]; then
52+
echo "linking with non-existent netdevsim should fail"
53+
exit 1
54+
fi
55+
56+
echo 2 > /sys/bus/netdevsim/new_device
57+
58+
echo "2 0" > ${NSIM_DEV_DFS}1/ports/0/peer
59+
if [ $? -ne 0 ]; then
60+
echo "linking netdevsim1 port0 with netdevsim2 port0 should succeed"
61+
exit 1
62+
fi
63+
64+
# argument error checking
65+
66+
echo "2 1" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
67+
if [ $? -eq 0 ]; then
68+
echo "linking with non-existent port in a netdevsim should fail"
69+
exit 1
70+
fi
71+
72+
echo "1 0" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
73+
if [ $? -eq 0 ]; then
74+
echo "linking with self should fail"
75+
exit 1
76+
fi
77+
78+
echo "2 a" > ${NSIM_DEV_DFS}1/ports/0/peer 2>/dev/null
79+
if [ $? -eq 0 ]; then
80+
echo "invalid arg should fail"
81+
exit 1
82+
fi
83+
84+
# send/recv packets
85+
86+
socat_check || exit 1
87+
88+
setup_ns
89+
90+
tmp_file=$(mktemp)
91+
ip netns exec nssv socat TCP-LISTEN:1234,fork $tmp_file &
92+
pid=$!
93+
94+
echo "HI" | ip netns exec nscl socat STDIN TCP:192.168.1.1:1234
95+
96+
count=$(cat $tmp_file | wc -c)
97+
if [[ $count -ne 3 ]]; then
98+
echo "expected 3 bytes, got $count"
99+
exit 1
100+
fi
101+
102+
echo 2 > ${NSIM_DEV_SYS}/del_device
103+
104+
kill $pid
105+
echo 1 > ${NSIM_DEV_SYS}/del_device
106+
107+
cleanup_ns
108+
109+
modprobe -r netdevsim
110+
111+
exit 0

0 commit comments

Comments
 (0)