1#!/bin/sh
2
3. lib/generic.sh
4
5DEV="$(rand_dev)"
6ts_ip "$0" "Add $DEV dummy interface" link add dev $DEV type dummy
7ts_ip "$0" "Enable $DEV" link set $DEV up
8ts_tc "pedit" "Add ingress qdisc" qdisc add dev $DEV ingress
9
10
11do_pedit() {
12	ts_tc "pedit" "Drop ingress qdisc" \
13		qdisc del dev $DEV ingress
14	ts_tc "pedit" "Add ingress qdisc" \
15		qdisc add dev $DEV ingress
16	ts_tc "pedit" "Add pedit action $*" \
17		filter add dev $DEV parent ffff: \
18		u32 match u32 0 0 \
19		action pedit munge $@
20	ts_tc "pedit" "Show ingress filters" \
21		filter show dev $DEV parent ffff:
22}
23
24do_pedit offset 12 u32 set 0x12345678
25test_on "key #0  at 12: val 12345678 mask 00000000"
26do_pedit offset 12 u16 set 0x1234
27test_on "key #0  at 12: val 12340000 mask 0000ffff"
28do_pedit offset 14 u16 set 0x1234
29test_on "key #0  at 12: val 00001234 mask ffff0000"
30do_pedit offset 12 u8 set 0x23
31test_on "key #0  at 12: val 23000000 mask 00ffffff"
32do_pedit offset 13 u8 set 0x23
33test_on "key #0  at 12: val 00230000 mask ff00ffff"
34do_pedit offset 14 u8 set 0x23
35test_on "key #0  at 12: val 00002300 mask ffff00ff"
36do_pedit offset 15 u8 set 0x23
37test_on "key #0  at 12: val 00000023 mask ffffff00"
38
39do_pedit offset 13 u8 invert
40test_on "key #0  at 12: val 00ff0000 mask ffffffff"
41do_pedit offset 13 u8 clear
42test_on "key #0  at 12: val 00000000 mask ff00ffff"
43do_pedit offset 13 u8 preserve
44test_on "key #0  at 12: val 00000000 mask ffffffff"
45
46# the following set of tests has been auto-generated by running this little
47# shell script:
48#
49# do_it() {
50#	echo "do_pedit $@"
51#	tc qd del dev veth0 ingress >/dev/null 2>&1
52#	tc qd add dev veth0 ingress >/dev/null 2>&1
53#	tc filter add dev veth0 parent ffff: u32 \
54#		match u32 0 0 \
55#		action pedit munge $@ >/dev/null 2>&1
56#	tc filter show dev veth0 parent ffff: | \
57#		sed -n 's/^[\t ]*\(key #0.*\)/test_on "\1"/p'
58# }
59#
60# do_it_all() { # (field, val1 [, val2, ...])
61#	local field=$1
62#	shift
63#	for val in $@; do
64#		do_it ip $field set $val
65#	done
66#	for i in preserve invert clear; do
67#		do_it ip $field $i
68#	done
69# }
70#
71# do_it_all ihl 0x04 0x40
72# do_it_all src 1.2.3.4
73# do_it_all dst 1.2.3.4
74# do_it_all tos 0x1 0x10
75# do_it_all protocol 0x23
76# do_it_all nofrag 0x23 0xf4
77# do_it_all firstfrag 0x03 0xfa
78# do_it_all ce 0x23 0x04 0xf3
79# do_it_all df 0x23 0x04 0xf3
80# do_it_all mf 0x23 0x04 0xf3
81# do_it_all dport 0x1234
82# do_it_all sport 0x1234
83# do_it_all icmp_type 0x23
84# do_it_all icmp_code 0x23
85
86do_pedit ip ihl set 0x04
87test_on "key #0  at 0: val 04000000 mask f0ffffff"
88do_pedit ip ihl set 0x40
89test_on "key #0  at 0: val 00000000 mask f0ffffff"
90do_pedit ip ihl preserve
91test_on "key #0  at 0: val 00000000 mask ffffffff"
92do_pedit ip ihl invert
93test_on "key #0  at 0: val 0f000000 mask ffffffff"
94do_pedit ip ihl clear
95test_on "key #0  at 0: val 00000000 mask f0ffffff"
96do_pedit ip src set 1.2.3.4
97test_on "key #0  at 12: val 01020304 mask 00000000"
98do_pedit ip src preserve
99test_on "key #0  at 12: val 00000000 mask ffffffff"
100do_pedit ip src invert
101test_on "key #0  at 12: val ffffffff mask ffffffff"
102do_pedit ip src clear
103test_on "key #0  at 12: val 00000000 mask 00000000"
104do_pedit ip dst set 1.2.3.4
105test_on "key #0  at 16: val 01020304 mask 00000000"
106do_pedit ip dst preserve
107test_on "key #0  at 16: val 00000000 mask ffffffff"
108do_pedit ip dst invert
109test_on "key #0  at 16: val ffffffff mask ffffffff"
110do_pedit ip dst clear
111test_on "key #0  at 16: val 00000000 mask 00000000"
112do_pedit ip tos set 0x1
113test_on "key #0  at 0: val 00010000 mask ff00ffff"
114do_pedit ip tos set 0x10
115test_on "key #0  at 0: val 00100000 mask ff00ffff"
116do_pedit ip tos preserve
117test_on "key #0  at 0: val 00000000 mask ffffffff"
118do_pedit ip tos invert
119test_on "key #0  at 0: val 00ff0000 mask ffffffff"
120do_pedit ip tos clear
121test_on "key #0  at 0: val 00000000 mask ff00ffff"
122do_pedit ip protocol set 0x23
123test_on "key #0  at 8: val 00230000 mask ff00ffff"
124do_pedit ip protocol preserve
125test_on "key #0  at 8: val 00000000 mask ffffffff"
126do_pedit ip protocol invert
127test_on "key #0  at 8: val 00ff0000 mask ffffffff"
128do_pedit ip protocol clear
129test_on "key #0  at 8: val 00000000 mask ff00ffff"
130do_pedit ip nofrag set 0x23
131test_on "key #0  at 4: val 00002300 mask ffffc0ff"
132do_pedit ip nofrag set 0xf4
133test_on "key #0  at 4: val 00003400 mask ffffc0ff"
134do_pedit ip nofrag preserve
135test_on "key #0  at 4: val 00000000 mask ffffffff"
136do_pedit ip nofrag invert
137test_on "key #0  at 4: val 00003f00 mask ffffffff"
138do_pedit ip nofrag clear
139test_on "key #0  at 4: val 00000000 mask ffffc0ff"
140do_pedit ip firstfrag set 0x03
141test_on "key #0  at 4: val 00000300 mask ffffe0ff"
142do_pedit ip firstfrag set 0xfa
143test_on "key #0  at 4: val 00001a00 mask ffffe0ff"
144do_pedit ip firstfrag preserve
145test_on "key #0  at 4: val 00000000 mask ffffffff"
146do_pedit ip firstfrag invert
147test_on "key #0  at 4: val 00001f00 mask ffffffff"
148do_pedit ip firstfrag clear
149test_on "key #0  at 4: val 00000000 mask ffffe0ff"
150do_pedit ip ce set 0x23
151test_on "key #0  at 4: val 00000000 mask ffff7fff"
152do_pedit ip ce set 0x04
153test_on "key #0  at 4: val 00000000 mask ffff7fff"
154do_pedit ip ce set 0xf3
155test_on "key #0  at 4: val 00008000 mask ffff7fff"
156do_pedit ip ce preserve
157test_on "key #0  at 4: val 00000000 mask ffffffff"
158do_pedit ip ce invert
159test_on "key #0  at 4: val 00008000 mask ffffffff"
160do_pedit ip ce clear
161test_on "key #0  at 4: val 00000000 mask ffff7fff"
162do_pedit ip df set 0x23
163test_on "key #0  at 4: val 00000000 mask ffffbfff"
164do_pedit ip df set 0x04
165test_on "key #0  at 4: val 00000000 mask ffffbfff"
166do_pedit ip df set 0xf3
167test_on "key #0  at 4: val 00004000 mask ffffbfff"
168do_pedit ip df preserve
169test_on "key #0  at 4: val 00000000 mask ffffffff"
170do_pedit ip df invert
171test_on "key #0  at 4: val 00004000 mask ffffffff"
172do_pedit ip df clear
173test_on "key #0  at 4: val 00000000 mask ffffbfff"
174do_pedit ip mf set 0x23
175test_on "key #0  at 4: val 00002000 mask ffffdfff"
176do_pedit ip mf set 0x04
177test_on "key #0  at 4: val 00000000 mask ffffdfff"
178do_pedit ip mf set 0xf3
179test_on "key #0  at 4: val 00002000 mask ffffdfff"
180do_pedit ip mf preserve
181test_on "key #0  at 4: val 00000000 mask ffffffff"
182do_pedit ip mf invert
183test_on "key #0  at 4: val 00002000 mask ffffffff"
184do_pedit ip mf clear
185test_on "key #0  at 4: val 00000000 mask ffffdfff"
186do_pedit ip dport set 0x1234
187test_on "key #0  at 20: val 00001234 mask ffff0000"
188do_pedit ip dport preserve
189test_on "key #0  at 20: val 00000000 mask ffffffff"
190do_pedit ip dport invert
191test_on "key #0  at 20: val 0000ffff mask ffffffff"
192do_pedit ip dport clear
193test_on "key #0  at 20: val 00000000 mask ffff0000"
194do_pedit ip sport set 0x1234
195test_on "key #0  at 20: val 12340000 mask 0000ffff"
196do_pedit ip sport preserve
197test_on "key #0  at 20: val 00000000 mask ffffffff"
198do_pedit ip sport invert
199test_on "key #0  at 20: val ffff0000 mask ffffffff"
200do_pedit ip sport clear
201test_on "key #0  at 20: val 00000000 mask 0000ffff"
202do_pedit ip icmp_type set 0x23
203test_on "key #0  at 20: val 23000000 mask 00ffffff"
204do_pedit ip icmp_type preserve
205test_on "key #0  at 20: val 00000000 mask ffffffff"
206do_pedit ip icmp_type invert
207test_on "key #0  at 20: val ff000000 mask ffffffff"
208do_pedit ip icmp_type clear
209test_on "key #0  at 20: val 00000000 mask 00ffffff"
210do_pedit ip icmp_code set 0x23
211test_on "key #0  at 20: val 23000000 mask 00ffffff"
212do_pedit ip icmp_code preserve
213test_on "key #0  at 20: val 00000000 mask ffffffff"
214do_pedit ip icmp_code invert
215test_on "key #0  at 20: val ff000000 mask ffffffff"
216do_pedit ip icmp_code clear
217test_on "key #0  at 20: val 00000000 mask 00ffffff"
218