xref: /linux/tools/testing/selftests/drivers/net/netdevsim/peer.sh (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0-only
3
4source ../../../net/net_helper.sh
5
6NSIM_DEV_1_ID=$((256 + RANDOM % 256))
7NSIM_DEV_1_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_1_ID
8NSIM_DEV_2_ID=$((512 + RANDOM % 256))
9NSIM_DEV_2_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_DEV_2_ID
10
11NSIM_DEV_SYS_NEW=/sys/bus/netdevsim/new_device
12NSIM_DEV_SYS_DEL=/sys/bus/netdevsim/del_device
13NSIM_DEV_SYS_LINK=/sys/bus/netdevsim/link_device
14NSIM_DEV_SYS_UNLINK=/sys/bus/netdevsim/unlink_device
15
16socat_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
26setup_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
48cleanup_ns()
49{
50	ip netns del nscl
51	ip netns del nssv
52}
53
54###
55### Code start
56###
57
58socat_check || exit 4
59
60modprobe netdevsim
61
62# linking
63
64echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_NEW
65echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_NEW
66udevadm settle
67
68setup_ns
69
70NSIM_DEV_1_FD=$((256 + RANDOM % 256))
71exec {NSIM_DEV_1_FD}</var/run/netns/nssv
72NSIM_DEV_1_IFIDX=$(ip netns exec nssv cat /sys/class/net/$NSIM_DEV_1_NAME/ifindex)
73
74NSIM_DEV_2_FD=$((256 + RANDOM % 256))
75exec {NSIM_DEV_2_FD}</var/run/netns/nscl
76NSIM_DEV_2_IFIDX=$(ip netns exec nscl cat /sys/class/net/$NSIM_DEV_2_NAME/ifindex)
77
78echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:2000" > $NSIM_DEV_SYS_LINK 2>/dev/null
79if [ $? -eq 0 ]; then
80	echo "linking with non-existent netdevsim should fail"
81	cleanup_ns
82	exit 1
83fi
84
85echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX 2000:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null
86if [ $? -eq 0 ]; then
87	echo "linking with non-existent netnsid should fail"
88	cleanup_ns
89	exit 1
90fi
91
92echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_LINK 2>/dev/null
93if [ $? -eq 0 ]; then
94	echo "linking with self should fail"
95	cleanup_ns
96	exit 1
97fi
98
99echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:$NSIM_DEV_2_IFIDX" > $NSIM_DEV_SYS_LINK
100if [ $? -ne 0 ]; then
101	echo "linking netdevsim1 with netdevsim2 should succeed"
102	cleanup_ns
103	exit 1
104fi
105
106# argument error checking
107
108echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX $NSIM_DEV_2_FD:a" > $NSIM_DEV_SYS_LINK 2>/dev/null
109if [ $? -eq 0 ]; then
110	echo "invalid arg should fail"
111	cleanup_ns
112	exit 1
113fi
114
115# send/recv packets
116
117tmp_file=$(mktemp)
118ip netns exec nssv socat TCP-LISTEN:1234,fork $tmp_file &
119pid=$!
120res=0
121
122wait_local_port_listen nssv 1234 tcp
123
124echo "HI" | ip netns exec nscl socat STDIN TCP:192.168.1.1:1234
125
126count=$(cat $tmp_file | wc -c)
127if [[ $count -ne 3 ]]; then
128	echo "expected 3 bytes, got $count"
129	res=1
130fi
131
132echo "$NSIM_DEV_1_FD:$NSIM_DEV_1_IFIDX" > $NSIM_DEV_SYS_UNLINK
133
134echo $NSIM_DEV_2_ID > $NSIM_DEV_SYS_DEL
135
136kill $pid
137echo $NSIM_DEV_1_ID > $NSIM_DEV_SYS_DEL
138
139cleanup_ns
140
141modprobe -r netdevsim
142
143exit $res
144