]> git.karo-electronics.de Git - karo-tx-linux.git/blob - samples/bpf/test_cgrp2_sock2.sh
samples/bpf: run cleanup routines when receiving SIGTERM
[karo-tx-linux.git] / samples / bpf / test_cgrp2_sock2.sh
1 #!/bin/bash
2
3 function config_device {
4         ip netns add at_ns0
5         ip link add veth0 type veth peer name veth0b
6         ip link set veth0b up
7         ip link set veth0 netns at_ns0
8         ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
9         ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
10         ip netns exec at_ns0 ip link set dev veth0 up
11         ip addr add 172.16.1.101/24 dev veth0b
12         ip addr add 2401:db00::2/64 dev veth0b nodad
13 }
14
15 function config_cgroup {
16         rm -rf /tmp/cgroupv2
17         mkdir -p /tmp/cgroupv2
18         mount -t cgroup2 none /tmp/cgroupv2
19         mkdir -p /tmp/cgroupv2/foo
20         echo $$ >> /tmp/cgroupv2/foo/cgroup.procs
21 }
22
23
24 function attach_bpf {
25         test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1
26         [ $? -ne 0 ] && exit 1
27 }
28
29 function cleanup {
30         ip link del veth0b
31         ip netns delete at_ns0
32         umount /tmp/cgroupv2
33         rm -rf /tmp/cgroupv2
34 }
35
36 cleanup 2>/dev/null
37
38 set -e
39 config_device
40 config_cgroup
41 set +e
42
43 #
44 # Test 1 - fail ping6
45 #
46 attach_bpf 0
47 ping -c1 -w1 172.16.1.100
48 if [ $? -ne 0 ]; then
49         echo "ping failed when it should succeed"
50         cleanup
51         exit 1
52 fi
53
54 ping6 -c1 -w1 2401:db00::1
55 if [ $? -eq 0 ]; then
56         echo "ping6 succeeded when it should not"
57         cleanup
58         exit 1
59 fi
60
61 #
62 # Test 2 - fail ping
63 #
64 attach_bpf 1
65 ping6 -c1 -w1 2401:db00::1
66 if [ $? -ne 0 ]; then
67         echo "ping6 failed when it should succeed"
68         cleanup
69         exit 1
70 fi
71
72 ping -c1 -w1 172.16.1.100
73 if [ $? -eq 0 ]; then
74         echo "ping succeeded when it should not"
75         cleanup
76         exit 1
77 fi
78
79 cleanup
80 echo
81 echo "*** PASS ***"