Linux Audio

Check our new training course

Embedded Linux training

Mar 10-20, 2025, special US time zones
Register
Loading...
v6.8
  1#!/bin/bash
  2# SPDX-License-Identifier: GPL-2.0
  3
  4BPFFS=/sys/fs/bpf
  5MY_DIR=$(dirname $0)
  6TEST=$MY_DIR/test_cgrp2_sock2
  7LINK_PIN=$BPFFS/test_cgrp2_sock2
  8BPF_PROG=$MY_DIR/sock_flags.bpf.o
  9
 10function config_device {
 11	ip netns add at_ns0
 12	ip link add veth0 type veth peer name veth0b
 
 13	ip link set veth0 netns at_ns0
 14	ip netns exec at_ns0 sysctl -q net.ipv6.conf.veth0.disable_ipv6=0
 15	ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
 16	ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
 17	ip netns exec at_ns0 ip link set dev veth0 up
 18	sysctl -q net.ipv6.conf.veth0b.disable_ipv6=0
 19	ip addr add 172.16.1.101/24 dev veth0b
 20	ip addr add 2401:db00::2/64 dev veth0b nodad
 21	ip link set veth0b up
 22}
 23
 24function config_cgroup {
 25	rm -rf /tmp/cgroupv2
 26	mkdir -p /tmp/cgroupv2
 27	mount -t cgroup2 none /tmp/cgroupv2
 28	mkdir -p /tmp/cgroupv2/foo
 29	echo $$ >> /tmp/cgroupv2/foo/cgroup.procs
 30}
 31
 32function config_bpffs {
 33	if mount | grep $BPFFS > /dev/null; then
 34		echo "bpffs already mounted"
 35	else
 36		echo "bpffs not mounted. Mounting..."
 37		mount -t bpf none $BPFFS
 38	fi
 39}
 40
 41function attach_bpf {
 42	$TEST /tmp/cgroupv2/foo $BPF_PROG $1
 43	[ $? -ne 0 ] && exit 1
 44}
 45
 46function cleanup {
 47	rm -rf $LINK_PIN
 
 
 48	ip link del veth0b
 49	ip netns delete at_ns0
 50	umount /tmp/cgroupv2
 51	rm -rf /tmp/cgroupv2
 52}
 53
 54cleanup 2>/dev/null
 55
 56set -e
 57config_device
 58config_cgroup
 59config_bpffs
 60set +e
 61
 62#
 63# Test 1 - fail ping6
 64#
 65attach_bpf 0
 66ping -c1 -w1 172.16.1.100
 67if [ $? -ne 0 ]; then
 68	echo "ping failed when it should succeed"
 69	cleanup
 70	exit 1
 71fi
 72
 73ping6 -c1 -w1 2401:db00::1
 74if [ $? -eq 0 ]; then
 75	echo "ping6 succeeded when it should not"
 76	cleanup
 77	exit 1
 78fi
 79
 80rm -rf $LINK_PIN
 81sleep 1                 # Wait for link detach
 82
 83#
 84# Test 2 - fail ping
 85#
 86attach_bpf 1
 87ping6 -c1 -w1 2401:db00::1
 88if [ $? -ne 0 ]; then
 89	echo "ping6 failed when it should succeed"
 90	cleanup
 91	exit 1
 92fi
 93
 94ping -c1 -w1 172.16.1.100
 95if [ $? -eq 0 ]; then
 96	echo "ping succeeded when it should not"
 97	cleanup
 98	exit 1
 99fi
100
101cleanup
102echo
103echo "*** PASS ***"
v4.17
 1#!/bin/bash
 2# SPDX-License-Identifier: GPL-2.0
 3
 
 
 
 
 
 
 4function config_device {
 5	ip netns add at_ns0
 6	ip link add veth0 type veth peer name veth0b
 7	ip link set veth0b up
 8	ip link set veth0 netns at_ns0
 
 9	ip netns exec at_ns0 ip addr add 172.16.1.100/24 dev veth0
10	ip netns exec at_ns0 ip addr add 2401:db00::1/64 dev veth0 nodad
11	ip netns exec at_ns0 ip link set dev veth0 up
 
12	ip addr add 172.16.1.101/24 dev veth0b
13	ip addr add 2401:db00::2/64 dev veth0b nodad
 
14}
15
16function config_cgroup {
17	rm -rf /tmp/cgroupv2
18	mkdir -p /tmp/cgroupv2
19	mount -t cgroup2 none /tmp/cgroupv2
20	mkdir -p /tmp/cgroupv2/foo
21	echo $$ >> /tmp/cgroupv2/foo/cgroup.procs
22}
23
 
 
 
 
 
 
 
 
24
25function attach_bpf {
26	test_cgrp2_sock2 /tmp/cgroupv2/foo sock_flags_kern.o $1
27	[ $? -ne 0 ] && exit 1
28}
29
30function cleanup {
31	if [ -d /tmp/cgroupv2/foo ]; then
32		test_cgrp2_sock -d /tmp/cgroupv2/foo
33	fi
34	ip link del veth0b
35	ip netns delete at_ns0
36	umount /tmp/cgroupv2
37	rm -rf /tmp/cgroupv2
38}
39
40cleanup 2>/dev/null
41
42set -e
43config_device
44config_cgroup
 
45set +e
46
47#
48# Test 1 - fail ping6
49#
50attach_bpf 0
51ping -c1 -w1 172.16.1.100
52if [ $? -ne 0 ]; then
53	echo "ping failed when it should succeed"
54	cleanup
55	exit 1
56fi
57
58ping6 -c1 -w1 2401:db00::1
59if [ $? -eq 0 ]; then
60	echo "ping6 succeeded when it should not"
61	cleanup
62	exit 1
63fi
 
 
 
64
65#
66# Test 2 - fail ping
67#
68attach_bpf 1
69ping6 -c1 -w1 2401:db00::1
70if [ $? -ne 0 ]; then
71	echo "ping6 failed when it should succeed"
72	cleanup
73	exit 1
74fi
75
76ping -c1 -w1 172.16.1.100
77if [ $? -eq 0 ]; then
78	echo "ping succeeded when it should not"
79	cleanup
80	exit 1
81fi
82
83cleanup
84echo
85echo "*** PASS ***"