blob: 6c17a2f22a506bc4a445f9096d177a88ca0cc8da (
plain)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
#!/usr/bin/ksh
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# Copyright 2019 Joyent, Inc.
# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
#
#
# Functions shared across the network tests.
#
DEBUG=0
function dbg
{
typeset msg="$*"
if (($DEBUG == 1)); then
echo "DBG [$nt_tname]: $msg"
fi
}
function fail
{
typeset msg="$*"
echo "FAIL [$nt_tname]: $msg" >&2
exit 1
}
function maybe_fail
{
typeset msg=$1
if ((BAIL == 1)); then
fail "$msg"
else
dbg "$msg"
return 1
fi
}
function zone_exists
{
typeset name=$1
if (($# != 1)); then
fail "$0: incorrect number of args provided"
fi
dbg "checking for existence of zone: $name"
if zoneadm -z $name list > /dev/null 2>&1; then
dbg "found zone: $name"
return 0
else
dbg "zone not found: $name"
return 1
fi
}
function zone_running
{
typeset name=$1
typeset state=$(zoneadm -z $name list -p | awk -F: '{ print $3 }')
typeset err="zone $name is not running"
if (($# != 1)); then
fail "$0: incorrect number of args provided"
fi
dbg "check if zone $name is running"
dbg "state of zone $name: $state"
if [[ "$state" == "running" ]]; then
dbg "zone $name is running"
return 0
fi
maybe_fail "$err"
}
function simnet_exists
{
typeset name=$1
if (($# != 1)); then
fail "$0: incorrect number of args provided"
fi
if dladm show-simnet $name > /dev/null 2>&1; then
dbg "simnet $name found"
return 0
else
dbg "simnet $name not found"
return 1
fi
}
function create_simnet
{
typeset name=$1
typeset err="failed to create simnet $name"
if (($# != 1)); then
fail "$0: incorrect number of args provided"
fi
dbg "creating simnet $name"
if simnet_exists $name; then
dbg "simnet $name already exists"
maybe_fail "$err"
return 1
fi
if dladm create-simnet > /dev/null $name; then
dbg "created simnet $name"
return 0
fi
maybe_fail "$err"
}
function delete_simnet
{
typeset name=$1
typeset err="failed to delete simnet $name"
if (($# != 1)); then
fail "$0: incorrect number of args provided"
fi
dbg "deleting simnet $name"
if ! simnet_exists $name; then
dbg "simnet $name doesn't exist"
return 1
fi
if dladm delete-simnet $name; then
dbg "simnet $name deleted"
return 0
fi
maybe_fail "$err"
}
function link_simnets
{
typeset sim1=$1
typeset sim2=$2
typeset err="failed to link simnet $sim1 to $sim2"
if (($# != 2)); then
fail "$0: incorrect number of args provided"
fi
dbg "linking simnet $sim1 to $sim2"
if dladm modify-simnet -p $sim2 $sim1 > /dev/null; then
dbg "linked simnet $sim1 to $sim2"
return 0
fi
maybe_fail "$err"
}
function vnic_exists
{
typeset name=$1
typeset vid=$2
typeset over=$3
typeset zone=$4
if (($# != 4)); then
fail "$0: incorrect number of args provided"
fi
if dladm show-vnic $name > /dev/null 2>&1; then
typeset avid=$(dladm show-vnic -p -o vid $name)
typeset aover=$(dladm show-vnic -p -o over $name)
typeset azone=$(dladm show-linkprop -cp zone -o value $name)
if (($avid == $vid)) && [ $aover == $over ] && \
[ $azone == $zone ]
then
return 0
else
return 1
fi
else
return 1
fi
}
function create_vnic
{
typeset name=$1
typeset over=$2
typeset vid=$3
typeset zone=$4
typeset r=1
typeset vid_opt=""
typeset vnic_info="$name, vid: $vid, over: $over, zone: $zone"
typeset err="failed to create VNIC: $vnic_info"
if (($# != 4)); then
fail "$0: incorrect number of args provided"
fi
if ((vid != 0)); then
vid_opt="-v $vid"
fi
dbg "creating VNIC: $vnic_info"
if dladm create-vnic -l $over $vid_opt $name > /dev/null 2>&1 && \
dladm set-linkprop -t -p zone=$zone $name > /dev/null 2>&1
then
dbg "created VNIC: $vnic_info"
return 0
fi
maybe_fail "$err"
}
function delete_vnic
{
typeset name=$1
typeset vid=$2
typeset zone=$3
typeset vnic_info="$name, vid: $vid, zone: $zone"
typeset err1="failed to assign VNIC $name from $zone to GZ"
typeset err2="failed to delete VNIC: $vnic_info"
if (($# != 3)); then
fail "$0: incorrect number of args provided"
fi
dbg "assigning VNIC $name from $zone to GZ"
if ! dladm set-linkprop -t -p zone=global $name; then
maybe_fail "$err1"
return 1
fi
dbg "deleting VNIC: $vnic_info"
if dladm delete-vnic $name > /dev/null; then
dbg "deleted VNIC: $vnic_info"
return 0
fi
maybe_fail "$err2"
}
function create_addr
{
typeset zone=$1
typeset vnic=$2
typeset ip=$3
typeset ipname=${vnic}/v4
if (($# != 3)); then
fail "$0: incorrect number of args provided"
fi
if zlogin $zone ipadm create-addr -t -T static -a $ip \
$ipname > /dev/null
then
dbg "created addr $ipname ($ip) in zone $zone"
return 0
fi
maybe_fail "failed to create addr $ipname ($ip) in zone $zone"
}
function create_addr6
{
typeset zone=$1
typeset vnic=$2
typeset ip=$3
typeset ll_name=${vnic}/v6
typeset uni_name=${vnic}/v6add
typeset err1="failed to create link-local addr $ll_name in zone $zone"
typeset err2="failed to create unicast addr $uni_name in zone $zone"
if (($# != 3)); then
fail "$0: incorrect number of args provided"
fi
if zlogin $zone ipadm create-addr -t -T addrconf $ll_name; then
dbg "created link-local addr $ll_name in zone $zone"
else
maybe_fail "$err1"
return 1
fi
if zlogin $zone ipadm create-addr -t -T static -a $ip/64 $uni_name; then
dbg "created unicast addr $uni_name in zone $zone"
else
maybe_fail "$err2"
fi
}
function delete_addr
{
typeset zone=$1
typeset ifname=$2
typeset version=$3
typeset ipname=$ifname/$version
if (($# != 3)); then
fail "$0: incorrect number of args provided"
fi
if zlogin $zone ipadm show-addr $ipname > /dev/null 2>&1; then
if zlogin $zone ipadm delete-addr $ipname > /dev/null; then
dbg "deleted addr $ipname in zone $zone"
else
maybe_fail "failed to delete addr $ipname in zone $zone"
return 1
fi
else
dbg "addr $ipname doesn't exist in zone $zone"
fi
if [[ "v6" == "$version" ]]; then
typeset ipname=$ifname/v6add
typeset err="failed to delete addr $ipname in zone $zone"
if zlogin $zone ipadm show-addr $ipname > /dev/null 2>&1; then
if zlogin $zone ipadm delete-addr $ipname > /dev/null
then
dbg "deleted addr $ipname in zone $zone"
else
maybe_fail "$err"
fi
else
dbg "addr $ipname doesn't exist in zone $zone"
fi
fi
}
function delete_if
{
typeset zone=$1
typeset ifname=$2
typeset err="failed to delete interface $ifname in zone $zone"
if (($# != 2)); then
fail "$0: incorrect number of args provided"
fi
if zlogin $zone ipadm show-if $ifname > /dev/null 2>&1; then
if zlogin $zone ipadm delete-if $ifname > /dev/null; then
dbg "deleted interface $ifname in zone $zone"
else
maybe_fail "$err"
fi
else
dbg "interface $ifname doesn't exist in zone $zone"
fi
}
function ip_fwd_enable
{
typeset zone=$1
if (($# != 1)); then
fail "$0: incorrect number of args provided"
fi
if zlogin $zone routeadm -p ipv4-forwarding | \
egrep 'current=enabled' > /dev/null
then
dbg "IPv4 forwarding already enabled for $zone"
else
if zlogin $zone routeadm -ue ipv4-forwarding; then
dbg "enabled IPv4 forwarding for $zone"
else
maybe_fail "failed to enable IPv4 forwarding for $zone"
return 1
fi
fi
if zlogin $zone routeadm -p ipv6-forwarding | \
egrep 'current=enabled' > /dev/null
then
dbg "IPv6 forwarding already enabled for $zone"
else
if zlogin $zone routeadm -ue ipv6-forwarding; then
dbg "enabled IPv6 forwarding for $zone"
else
maybe_fail "failed to enable IPv6 forwarding for $zone"
fi
fi
}
function ip_fwd_disable
{
typeset zone=$1
if (($# != 1)); then
fail "$0: incorrect number of args provided"
fi
if zlogin $zone routeadm -p ipv4-forwarding | \
egrep 'current=disabled' > /dev/null
then
dbg "IPv4 forwarding already disabled for $zone"
else
if zlogin $zone routeadm -ud ipv4-forwarding; then
dbg "disabled IPv4 forwarding in $zone"
else
maybe_fail "failed to disable IPv4 forwarding in $zone"
return 1
fi
fi
if zlogin $zone routeadm -p ipv6-forwarding | \
egrep 'current=disabled' > /dev/null
then
dbg "IPv6 forwarding already disabled for $zone"
else
if zlogin $zone routeadm -ud ipv6-forwarding; then
dbg "disabled IPv6 forwarding in $zone"
else
maybe_fail "failed to disable IPv6 forwarding in $zone"
fi
fi
}
function add_route
{
typeset zone=$1
typeset dest=$2
typeset net=$3
typeset gateway=$4
if (($# != 4)); then
fail "$0: incorrect number of args provided"
fi
if zlogin $zone route -n add $net $gateway > /dev/null; then
dbg "added route $gateway => $net to $zone"
return 0
fi
maybe_fail "failed to add route $gateway => $net to $zone"
}
function add_route6
{
typeset zone=$1
typeset dest=$2
typeset net=$3
typeset gateway=$4
if (($# != 4)); then
fail "$0: incorrect number of args provided"
fi
if zlogin $zone route -n add -inet6 $net $gateway > /dev/null
then
dbg "added route $gateway => $net to $zone"
return 0
fi
maybe_fail "failed to add route $gateway => $net to $zone"
}
function rm_route
{
typeset zone=$1
typeset dest=$2
typeset net=$3
typeset gateway=$4
typeset gw=$(zlogin $zone route -n get $dest | \
grep gateway | awk '{ print $2 }')
typeset err="failed to remove route $gateway => $net from $zone"
if (($# != 4)); then
fail "$0: incorrect number of args provided"
fi
if [[ "$gw" == "$gateway" ]]; then
if zlogin $zone route -n delete $net $gateway > /dev/null
then
dbg "removed route $gateway => $net from $zone"
else
maybe_fail "$err"
fi
else
dbg "$zone already lacked route $gateway => $net"
fi
}
function rm_route6
{
typeset zone=$1
typeset dest=$2
typeset net=$3
typeset gateway=$4
typeset gw=$(zlogin $zone route -n get -inet6 $dest | \
grep gateway | awk '{ print $2 }')
typeset err="failed to remove route $gateway => $net from $zone"
if (($# != 4)); then
fail "$0: incorrect number of args provided"
fi
if [[ "$gw" == "$gateway" ]]; then
if zlogin $zone route -n delete -inet6 $net $gateway > /dev/null
then
dbg "removed route $gateway => $net from $zone"
else
maybe_fail "$err"
fi
else
dbg "$zone already lacked route $gateway => $net"
fi
}
function set_linkprop
{
typeset link=$1
typeset prop=$2
typeset val=$3
typeset err="failed to set $link prop: $prop=$val"
if (($# != 3)); then
fail "$0: incorrect number of args provided"
fi
dbg "attempt to set $link prop: $prop=$val"
if dladm set-linkprop -p $prop=$val $link; then
dbg "set $link prop: $prop=$val"
return 0
fi
maybe_fail "$err"
}
function ping
{
typeset zone=$1
typeset src=$2
typeset dst=$3
typeset info="$src -> $dst"
if (($# != 3)); then
fail "$0: incorrect number of args provided"
fi
dbg "ping: $info"
if zlogin $zone ping $dst > /dev/null 2>&1; then
dbg "successful ping: $info"
return 0
fi
maybe_fail "could not ping: $info"
}
function ping_udp
{
typeset client=$1
typeset client_ip=$2
typeset server_ip=$3
typeset size=$4
typeset num=$5
typeset info="$client_ip -> $server_ip (size: $size)"
if (($# != 5)); then
fail "$0: incorrect number of args provided"
fi
dbg "UDP ping: $info"
if zlogin $client ping -ns -U $server_ip $size $num > /dev/null; then
dbg "UDP ping passed: $info"
return 0
fi
maybe_fail "UDP ping failed: $info"
}
function start_server
{
typeset zone=$1
typeset type=$2
typeset ip=$3
typeset port=$4
typeset ofile=$5
if (($# != 5)); then
fail "$0: incorrect number of args provided"
fi
dbg "start server $rfile"
zlogin $zone \
/usr/bin/socat -u ${type}-LISTEN:$port,bind=[$ip],reuseaddr \
CREATE:$ofile &
listener_ppid=$!
dbg "listener PPID: $listener_ppid, zone $zone"
}
function wait_for_pid
{
typeset pid=$1
typeset seconds=$2
typeset s=0
if (($# != 2)); then
fail "$0: incorrect number of args provided"
fi
while true; do
if kill -0 $pid > /dev/null 2>&1; then
if ((seconds == s)); then
maybe_fail "timed out waiting for pid $pid"
return 1
fi
dbg "waiting for pid $pid"
sleep 1
((s++))
else
return 0
fi
done
}
|