t_capabilities.sh revision 1.1
1# $NetBSD: t_capabilities.sh,v 1.1 2020/06/27 06:57:44 jruoho Exp $
2#
3# Copyright (c) 2020 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Jukka Ruohonen.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30setcap() {
31
32	echo "Request: $1 $2 ($3)"
33	ifconfig $1 $2
34
35	if [ $4 -eq 1 ]; then
36
37		if [ ! $? -eq 0 ]; then
38			atf_fail "Failed to enable $3 for $1"
39		fi
40
41		if [ -z "$(ifconfig $1 | grep "enabled" | grep $3)" ]; then
42			atf_fail "Failed to enable $3 for $1"
43		fi
44
45		echo "Request: $1 -$2 (-$3)"
46		ifconfig $1 -$2
47
48		if [ ! $? -eq 0 ]; then
49			atf_fail "Failed to disable $3 for $1"
50		fi
51
52		if [ ! -z "$(ifconfig $1 | grep "enabled" | grep $3)" ]; then
53			atf_fail "Failed to disable $3 for $1"
54		fi
55	fi
56}
57
58parsecap() {
59
60	i=$1
61	checkrv=$3
62	x=$(echo $2 | sed "s/.*<//" | sed "s/>//")
63
64	export IFS=","
65
66	for y in $x; do
67
68		z=""
69
70		if [ $y = "TSO4" ]; then
71			z="tso4"
72		elif [ $y = "IP4CSUM_Rx" ]; then
73			z="ip4csum-rx"
74		elif [ $y = "IP4CSUM_Tx" ]; then
75			z="ip4csum-tx"
76		elif [ $y = "TCP4CSUM_Rx" ]; then
77			z="tcp4csum-rx"
78		elif [ $y = "TCP4CSUM_Tx" ]; then
79			z="tcp4csum-tx"
80		elif [ $y = "UDP4CSUM_Rx" ]; then
81			z="udp4csum-rx"
82		elif [ $y = "UDP4CSUM_Tx" ]; then
83			z="udp4csum-tx"
84		elif [ $y = "TCP6CSUM_Rx" ]; then
85			z="tcp6csum-rx"
86		elif [ $y = "TCP6CSUM_Tx" ]; then
87			z="tcp6csum-tx"
88		elif [ $y = "UDP6CSUM_Rx" ]; then
89			z="udp6csum-rx"
90		elif [ $y = "UDP6CSUM_Tx" ]; then
91			z="udp6csum-tx"
92		elif [ $y = "TSO6" ]; then
93			z="tso6"
94		fi
95
96		if [ -z $z ]; then
97			echo "Skipping unrecognized $y for $i"
98		else
99			setcap $i $z $y $checkrv
100		fi
101	done
102
103	unset IFS
104}
105
106atf_test_case basic
107basic_head() {
108	atf_set "require.user" "root"
109	atf_set "descr" "Test setting interface capabilities"
110}
111
112basic_body() {
113
114	for i in $(ifconfig -l); do
115
116		c=$(ifconfig $i | grep "capabilities")
117
118		if [ -z "$c" ]; then
119			echo "Skipping $i, no capabilities"
120			continue
121		fi
122
123		if [ ! -z "$(echo $i | grep ixg)" ]; then
124			echo "Skipping $i (PR kern/50933)"
125			continue
126		fi
127
128		enabled=$(ifconfig $i | grep "enabled")
129
130		for l in $c; do
131
132			if [ ! -z "$(echo $l | grep "ec_capabilities")" ]; then
133				continue
134			fi
135
136			parsecap $i $l 1
137		done
138
139		if [ ! -z "$enabled" ]; then
140
141			echo "Restoring capabilities for $i"
142
143			for l in $enabled; do
144
145				ec=$(echo $l | grep "ec_enabled")
146
147				if [ ! -z "$ec" ]; then
148					continue
149				fi
150
151				parsecap $i $l 0
152			done
153	       fi
154	done
155}
156
157atf_init_test_cases() {
158	atf_add_test_case basic
159}
160