Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# #-- stat_values.test --#
# source the master var file when it's there
[ -f ../.tpkg.var.master ] && source ../.tpkg.var.master
# use .tpkg.var.test for in test variable passing
[ -f .tpkg.var.test ] && source .tpkg.var.test
# We need kill_pid for the serve-expired-client-timeout test
. ../common.sh

PRE="../.."

# Individual thread stats.
STATS_IGNORE_THREAD="\
^thread"

# Histogram stats.
STATS_IGNORE_HISTOGRAM="\
^histogram"

# Time dependent stats.
STATS_IGNORE_TIME_SPECIFIC="\
^total.recursion.time.avg=
^total.recursion.time.median=
^time.now=
^time.up=
^time.elapsed="

# Usage dependent stats.
STATS_IGNORE_USAGE_SPECIFIC="\
^total.requestlist.avg=
^total.requestlist.max=
^total.requestlist.overwritten=
^total.requestlist.exceeded=
^total.requestlist.current.all=
^total.requestlist.current.user=
^total.tcpusage=
^mem\."

# Stats to ignore by default.
STATS_IGNORE_DEFAULT="\
$STATS_IGNORE_THREAD
$STATS_IGNORE_HISTOGRAM
$STATS_IGNORE_TIME_SPECIFIC
$STATS_IGNORE_USAGE_SPECIFIC"

# Various files to be used while testing.
STATS_FILE=stats.$$
EXPECTED_STATS_FILE=expected_stats.$$
IGNORE_REGEX_FILE=ignore_regex.$$
FILTERED_STATS_FILE=filtered_stats.$$
FOUND_STATS_FILE=found_stats.$$
REST_STATS_FILE=rest_stats.$$

DEBUG=0

# Write stats to $STATS_FILE.
# Call this when you want to get stats from unbound.
get_stats () {
	echo "> Getting stats"
	echo "$PRE/unbound-control -c ub.conf stats"
	$PRE/unbound-control -c ub.conf stats > $STATS_FILE
	if test $? -ne 0; then
		echo "wrong exit value after success"
		exit 1
	fi
}

# Set the expected stat values by writing to $EXPECTED_STATS_FILE.
# sort is used for proper diff later.
set_expected_stats () {
	echo "$1" | sort > $EXPECTED_STATS_FILE
}

# Set the regex to ignore stats by writing to $IGNORE_REGEX_FILE.
set_ignore_regex_stats () {
	echo "$1" > $IGNORE_REGEX_FILE
}

# Filter the stats by removing any matched regex from $IGNORE_REGEX_FILE,
# sorts and writes the left over stats to $FILTERED_STATS_FILE.
filter_stats () {
	grep -v -f $IGNORE_REGEX_FILE $STATS_FILE | sort > $FILTERED_STATS_FILE
}

# Check that the stats in $FILTERED_STATS_FILE include the expected stats in
# $EXPECTED_STATS_FILE.
check_expected_stats () {
	echo "> Checking expected stats"
	grep -F -x -f $EXPECTED_STATS_FILE $FILTERED_STATS_FILE > $FOUND_STATS_FILE
	if test $DEBUG -ne 0; then
		echo "Found:"
		cat $FOUND_STATS_FILE
	fi
	if diff $EXPECTED_STATS_FILE $FOUND_STATS_FILE; then
		echo "OK"
	else
		echo "! bad expected stats:"
        cat $FILTERED_STATS_FILE
		exit 1
	fi
}

# Check that the rest (unspecified) stats are all 0 (no surprises).
check_rest_stats () {
	echo "> Checking rest stats"
	grep -F -x -v -f $EXPECTED_STATS_FILE $FILTERED_STATS_FILE > $REST_STATS_FILE
	if test $DEBUG -ne 0; then
		echo "Rest:"
		cat $REST_STATS_FILE
	fi
	if grep -v "=0$" $REST_STATS_FILE; then
		echo "! bad rest stats"
		exit 1
	else
		echo "OK"
	fi
}

# Main function to check stats by:
# - Getting stats from unbound
# - Filtering out the stats we are not interested in
# - Checking that the expected stats are part of the filtered stats
# - The rest of the stats have 0 values.
check_stats () {
	set_expected_stats "$1"
	if test $DEBUG -ne 0; then
		echo "Expected:"
		cat $EXPECTED_STATS_FILE
	fi
	get_stats
	filter_stats
	if test $DEBUG -ne 0; then
		echo "Filtered:"
		cat $FILTERED_STATS_FILE
	fi
	check_expected_stats
	check_rest_stats
}

# Convenient function to set an option through unbound-control.
set_ub_option () {
	name=$1
	value=$2
	echo "$PRE/unbound-control -c ub.conf set_option $name: $value"
	$PRE/unbound-control -c ub.conf set_option $name: $value
	if test $? -ne 0; then
		echo "wrong exit value after success"
		exit 1
	fi
}

# Convenient function to exit the test.
end () {
	echo "> cat logfiles"
	cat fwd.log
	cat unbound.log
	if test $1 -eq 1; then
		echo "Not OK"
	else
		echo "> OK"
	fi
	exit $1
}

# Ignore all run specific stats.
set_ignore_regex_stats "$STATS_IGNORE_DEFAULT"

# Check if the server is up.
echo "> dig 1ttl.example.com."
dig @127.0.0.1 -p $UNBOUND_PORT 1ttl.example.com. | tee outfile
echo "> check answer"
if grep "1.1.1.1" outfile; then
	echo "OK"
else
	end 1
fi

echo
echo "[ Check initial stats based on first query. ]"
check_stats "\
total.num.queries=1
total.num.cachemiss=1
total.num.recursivereplies=1
num.query.type.A=1
num.query.class.IN=1
num.query.opcode.QUERY=1
num.query.flags.RD=1
num.query.flags.AD=1
num.query.edns.present=1
num.query.udpout=1
msg.cache.count=1
rrset.cache.count=1
infra.cache.count=1
num.answer.rcode.NOERROR=1"

echo
echo "[ Check stat reset. ]"
check_stats "\
msg.cache.count=1
rrset.cache.count=1
infra.cache.count=1"


echo
echo "[ Enable serve-expired and check. ]"
set_ub_option serve-expired yes
sleep 2  # make sure the TTL has expired.
echo "> dig 1ttl.example.com."
dig @127.0.0.1 -p $UNBOUND_PORT 1ttl.example.com. | tee outfile
echo "> check answer"
if grep "1.1.1.1" outfile; then
	echo "OK"
else
	end 1
fi
check_stats "\
total.num.queries=1
total.num.expired=1
total.num.cachehits=1
total.num.prefetch=1
num.answer.rcode.NOERROR=1
num.query.class.IN=1
num.query.edns.present=1
num.query.flags.AD=1
num.query.flags.RD=1
num.query.opcode.QUERY=1
num.query.type.A=1
num.query.udpout=1
msg.cache.count=1
rrset.cache.count=1
infra.cache.count=1"


echo
echo "[ Enable serve-expired-client-timeout and check. ]"
set_ub_option serve-expired-client-timeout 1
echo "> dig servfail.expired."
dig @127.0.0.1 -p $UNBOUND_PORT servfail.expired. | tee outfile
echo "> check answer"
if grep "192.0.2.1" outfile; then
	echo "OK"
else
	end 1
fi
check_stats "\
total.num.queries=1
total.num.cachemiss=1
total.num.recursivereplies=1
num.query.type.A=1
num.query.class.IN=1
num.query.opcode.QUERY=1
num.query.flags.RD=1
num.query.flags.AD=1
num.query.edns.present=1
num.query.udpout=1
msg.cache.count=2
rrset.cache.count=2
infra.cache.count=2
num.answer.rcode.NOERROR=1"
kill_pid $FWD_EXPIRED_PID  # kill the expired forwarder to force a servfail from upstream.
sleep 2  # make sure the TTL has expired.
echo "> dig servfail.expired."
dig @127.0.0.1 -p $UNBOUND_PORT servfail.expired. | tee outfile
echo "> check answer"
if grep "192.0.2.1" outfile; then
	echo "OK"
else
	end 1
fi
sleep 1  # make sure the outgoing UDP (and the edns1xx0 retry) are accounted for.
check_stats "\
total.num.queries=1
total.num.expired=1
total.num.recursivereplies=1
num.answer.rcode.NOERROR=1
num.query.class.IN=1
num.query.edns.present=1
num.query.flags.AD=1
num.query.flags.RD=1
num.query.opcode.QUERY=1
num.query.type.A=1
num.query.udpout=2
total.num.cachemiss=1
msg.cache.count=2
rrset.cache.count=2
infra.cache.count=2"


# Disable serve-expired
set_ub_option serve-expired no


echo
echo "[ Check REFUSED; try without RD flag. ]"
echo "> dig somethingelse.example.com."
dig @127.0.0.1 -p $UNBOUND_PORT +nordflag somethingelse.example.com. | tee outfile
echo "> check answer"
if grep "REFUSED" outfile; then
	echo "OK"
else
	end 1
fi
check_stats "\
num.answer.rcode.REFUSED=1
total.num.cachehits=1
num.query.class.IN=1
num.query.edns.present=1
num.query.flags.AD=1
num.query.opcode.QUERY=1
num.query.type.A=1
total.num.queries=1
msg.cache.count=2
rrset.cache.count=2
infra.cache.count=2"


echo
echo "[ Check the AD flag. ]"
echo "> dig www.example.com."
dig @127.0.0.1 -p $UNBOUND_PORT +noadflag www.example.com. | tee outfile
echo "> check answer"
if grep "10.20.30.40" outfile; then
	echo "OK"
else
	end 1
fi
check_stats "\
num.query.flags.AD=0
total.num.cachemiss=1
num.answer.rcode.NOERROR=1
num.query.class.IN=1
num.query.edns.present=1
num.query.flags.RD=1
num.query.opcode.QUERY=1
num.query.type.A=1
num.query.udpout=1
total.num.queries=1
total.num.recursivereplies=1
msg.cache.count=3
rrset.cache.count=3
infra.cache.count=2"

echo
echo "[ Check local zone. ]"
echo "> dig www.local.zone."
dig @127.0.0.1 -p $UNBOUND_PORT www.local.zone. | tee outfile
echo "> check answer"
if grep "192.0.2.1" outfile; then
	echo "OK"
else
	end 1
fi
check_stats "\
num.answer.rcode.NOERROR=1
total.num.cachehits=1
num.query.class.IN=1
num.query.edns.present=1
num.query.flags.AD=1
num.query.flags.RD=1
num.query.opcode.QUERY=1
num.query.type.A=1
total.num.queries=1
msg.cache.count=3
rrset.cache.count=3
infra.cache.count=2"


echo
echo "[ Check NXDOMAIN (with local data). ]"
echo "> dig mail.local.zone."
dig @127.0.0.1 -p $UNBOUND_PORT mail.local.zone. | tee outfile
echo "> check answer"
if grep "NXDOMAIN" outfile; then
	echo "OK"
else
	end 1
fi
check_stats "\
num.answer.rcode.NXDOMAIN=1
total.num.cachehits=1
num.query.class.IN=1
num.query.edns.present=1
num.query.flags.AD=1
num.query.flags.RD=1
num.query.opcode.QUERY=1
num.query.type.A=1
total.num.queries=1
msg.cache.count=3
rrset.cache.count=3
infra.cache.count=2"


echo
echo "[ Check CHAOS. ]"
echo "> dig id.server. ch txt"
dig @127.0.0.1 -p $UNBOUND_PORT id.server. ch txt | tee outfile
echo "> check answer"
if grep "stat_values" outfile; then
	echo "OK"
else
	end 1
fi
check_stats "\
num.query.class.CH=1
total.num.cachehits=1
num.answer.rcode.NOERROR=1
num.query.edns.present=1
num.query.flags.AD=1
num.query.flags.RD=1
num.query.opcode.QUERY=1
num.query.type.TXT=1
total.num.queries=1
msg.cache.count=3
rrset.cache.count=3
infra.cache.count=2"


end 0