1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# Copyright (c) 2018 Facebook
4
5set -eu
6
7wait_for_ip()
8{
9	local _i
10	echo -n "Wait for testing link-local IP to become available "
11	for _i in $(seq ${MAX_PING_TRIES}); do
12		echo -n "."
13		if $PING6 -c 1 -W 1 ff02::1%${TEST_IF} >/dev/null 2>&1; then
14			echo " OK"
15			return
16		fi
17		sleep 1
18	done
19	echo 1>&2 "ERROR: Timeout waiting for test IP to become available."
20	exit 1
21}
22
23setup()
24{
25	# Create testing interfaces not to interfere with current environment.
26	ip link add dev ${TEST_IF} type veth peer name ${TEST_IF_PEER}
27	ip link set ${TEST_IF} up
28	ip link set ${TEST_IF_PEER} up
29
30	wait_for_ip
31
32	tc qdisc add dev ${TEST_IF} clsact
33	tc filter add dev ${TEST_IF} egress bpf obj ${BPF_PROG_OBJ} \
34		sec ${BPF_PROG_SECTION} da
35
36	BPF_PROG_ID=$(tc filter show dev ${TEST_IF} egress | \
37			awk '/ id / {sub(/.* id /, "", $0); print($1)}')
38}
39
40cleanup()
41{
42	ip link del ${TEST_IF} 2>/dev/null || :
43	ip link del ${TEST_IF_PEER} 2>/dev/null || :
44}
45
46main()
47{
48	trap cleanup EXIT 2 3 6 15
49	setup
50	${PROG} ${TEST_IF} ${BPF_PROG_ID}
51}
52
53DIR=$(dirname $0)
54TEST_IF="test_cgid_1"
55TEST_IF_PEER="test_cgid_2"
56MAX_PING_TRIES=5
57BPF_PROG_OBJ="${DIR}/test_skb_cgroup_id_kern.o"
58BPF_PROG_SECTION="cgroup_id_logger"
59BPF_PROG_ID=0
60PROG="${DIR}/test_skb_cgroup_id_user"
61type ping6 >/dev/null 2>&1 && PING6="ping6" || PING6="ping -6"
62
63main
64