1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0-only
3
4VNI_GEN=$RANDOM
5NSIM_ID=$((RANDOM % 1024))
6NSIM_DEV_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_ID
7NSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_ID
8NSIM_NETDEV=
9HAS_ETHTOOL=
10STATIC_ENTRIES=
11EXIT_STATUS=0
12num_cases=0
13num_errors=0
14
15clean_up_devs=( )
16
17function err_cnt {
18    echo "ERROR:" $@
19    EXIT_STATUS=1
20    ((num_errors++))
21    ((num_cases++))
22}
23
24function pass_cnt {
25    ((num_cases++))
26}
27
28function cleanup_tuns {
29    for dev in "${clean_up_devs[@]}"; do
30	[ -e /sys/class/net/$dev ] && ip link del dev $dev
31    done
32    clean_up_devs=( )
33}
34
35function cleanup_nsim {
36    if [ -e $NSIM_DEV_SYS ]; then
37	echo $NSIM_ID > /sys/bus/netdevsim/del_device
38    fi
39}
40
41function cleanup {
42    cleanup_tuns
43    cleanup_nsim
44}
45
46trap cleanup EXIT
47
48function new_vxlan {
49    local dev=$1
50    local dstport=$2
51    local lower=$3
52    local ipver=$4
53    local flags=$5
54
55    local group ipfl
56
57    [ "$ipver" != '6' ] && group=239.1.1.1 || group=fff1::1
58    [ "$ipver" != '6' ] || ipfl="-6"
59
60    [[ ! "$flags" =~ "external" ]] && flags="$flags id $((VNI_GEN++))"
61
62    ip $ipfl link add $dev type vxlan \
63       group $group \
64       dev $lower \
65       dstport $dstport \
66       $flags
67
68    ip link set dev $dev up
69
70    clean_up_devs=("${clean_up_devs[@]}" $dev)
71
72    check_tables
73}
74
75function new_geneve {
76    local dev=$1
77    local dstport=$2
78    local ipver=$3
79    local flags=$4
80
81    local group ipfl
82
83    [ "$ipver" != '6' ] && remote=1.1.1.2 || group=::2
84    [ "$ipver" != '6' ] || ipfl="-6"
85
86    [[ ! "$flags" =~ "external" ]] && flags="$flags vni $((VNI_GEN++))"
87
88    ip $ipfl link add $dev type geneve \
89       remote $remote  \
90       dstport $dstport \
91       $flags
92
93    ip link set dev $dev up
94
95    clean_up_devs=("${clean_up_devs[@]}" $dev)
96
97    check_tables
98}
99
100function del_dev {
101    local dev=$1
102
103    ip link del dev $dev
104    check_tables
105}
106
107# Helpers for netdevsim port/type encoding
108function mke {
109    local port=$1
110    local type=$2
111
112    echo $((port << 16 | type))
113}
114
115function pre {
116    local val=$1
117
118    echo -e "port: $((val >> 16))\ttype: $((val & 0xffff))"
119}
120
121function pre_ethtool {
122    local val=$1
123    local port=$((val >> 16))
124    local type=$((val & 0xffff))
125
126    case $type in
127	1)
128	    type_name="vxlan"
129	    ;;
130	2)
131	    type_name="geneve"
132	    ;;
133	4)
134	    type_name="vxlan-gpe"
135	    ;;
136	*)
137	    type_name="bit X"
138	    ;;
139    esac
140
141    echo "port $port, $type_name"
142}
143
144function check_table {
145    local path=$NSIM_DEV_DFS/ports/$port/udp_ports_table$1
146    local -n expected=$2
147    local last=$3
148
149    read -a have < $path
150
151    if [ ${#expected[@]} -ne ${#have[@]} ]; then
152	echo "check_table: BAD NUMBER OF ITEMS"
153	return 0
154    fi
155
156    for i in "${!expected[@]}"; do
157	if [ -n "$HAS_ETHTOOL" -a ${expected[i]} -ne 0 ]; then
158	    pp_expected=`pre_ethtool ${expected[i]}`
159	    ethtool --show-tunnels $NSIM_NETDEV | grep "$pp_expected" >/dev/null
160	    if [ $? -ne 0 -a $last -ne 0 ]; then
161		err_cnt "ethtool table $1 on port $port: $pfx - $msg"
162		echo "       check_table: ethtool does not contain '$pp_expected'"
163		ethtool --show-tunnels $NSIM_NETDEV
164		return 0
165
166	    fi
167	fi
168
169	if [ ${expected[i]} != ${have[i]} ]; then
170	    if [ $last -ne 0 ]; then
171		err_cnt "table $1 on port $port: $pfx - $msg"
172		echo "       check_table: wrong entry $i"
173		echo "       expected: `pre ${expected[i]}`"
174		echo "       have:     `pre ${have[i]}`"
175		return 0
176	    fi
177	    return 1
178	fi
179    done
180
181    pass_cnt
182    return 0
183}
184
185function check_tables {
186    # Need retries in case we have workqueue making the changes
187    local retries=10
188
189    while ! check_table 0 exp0 $((retries == 0)); do
190	sleep 0.02
191	((retries--))
192    done
193    while ! check_table 1 exp1 $((retries == 0)); do
194	sleep 0.02
195	((retries--))
196    done
197
198    if [ -n "$HAS_ETHTOOL" -a -n "${STATIC_ENTRIES[0]}" ]; then
199	fail=0
200	for i in "${!STATIC_ENTRIES[@]}"; do
201	    pp_expected=`pre_ethtool ${STATIC_ENTRIES[i]}`
202	    cnt=$(ethtool --show-tunnels $NSIM_NETDEV | grep -c "$pp_expected")
203	    if [ $cnt -ne 1 ]; then
204		err_cnt "ethtool static entry: $pfx - $msg"
205		echo "       check_table: ethtool does not contain '$pp_expected'"
206		ethtool --show-tunnels $NSIM_NETDEV
207		fail=1
208	    fi
209	done
210	[ $fail == 0 ] && pass_cnt
211    fi
212}
213
214function print_table {
215    local path=$NSIM_DEV_DFS/ports/$port/udp_ports_table$1
216    read -a have < $path
217
218    tree $NSIM_DEV_DFS/
219
220    echo "Port $port table $1:"
221
222    for i in "${!have[@]}"; do
223	echo "    `pre ${have[i]}`"
224    done
225
226}
227
228function print_tables {
229    print_table 0
230    print_table 1
231}
232
233function get_netdev_name {
234    local -n old=$1
235
236    new=$(ls /sys/class/net)
237
238    for netdev in $new; do
239	for check in $old; do
240            [ $netdev == $check ] && break
241	done
242
243	if [ $netdev != $check ]; then
244	    echo $netdev
245	    break
246	fi
247    done
248}
249
250###
251### Code start
252###
253
254# Probe ethtool support
255ethtool -h | grep show-tunnels 2>&1 >/dev/null && HAS_ETHTOOL=y
256
257modprobe netdevsim
258
259# Basic test
260pfx="basic"
261
262for port in 0 1; do
263    old_netdevs=$(ls /sys/class/net)
264    if [ $port -eq 0 ]; then
265	echo $NSIM_ID > /sys/bus/netdevsim/new_device
266    else
267	echo 1 > $NSIM_DEV_DFS/udp_ports_open_only
268	echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
269	echo 1 > $NSIM_DEV_SYS/new_port
270    fi
271    NSIM_NETDEV=`get_netdev_name old_netdevs`
272
273    msg="new NIC device created"
274    exp0=( 0 0 0 0 )
275    exp1=( 0 0 0 0 )
276    check_tables
277
278    msg="VxLAN v4 devices"
279    exp0=( `mke 4789 1` 0 0 0 )
280    new_vxlan vxlan0 4789 $NSIM_NETDEV
281    new_vxlan vxlan1 4789 $NSIM_NETDEV
282
283    msg="VxLAN v4 devices go down"
284    exp0=( 0 0 0 0 )
285    ifconfig vxlan1 down
286    ifconfig vxlan0 down
287    check_tables
288
289    msg="VxLAN v6 devices"
290    exp0=( `mke 4789 1` 0 0 0 )
291    new_vxlan vxlanA 4789 $NSIM_NETDEV 6
292
293    for ifc in vxlan0 vxlan1; do
294	ifconfig $ifc up
295    done
296
297    new_vxlan vxlanB 4789 $NSIM_NETDEV 6
298
299    msg="another VxLAN v6 devices"
300    exp0=( `mke 4789 1` `mke 4790 1` 0 0 )
301    new_vxlan vxlanC 4790 $NSIM_NETDEV 6
302
303    msg="Geneve device"
304    exp1=( `mke 6081 2` 0 0 0 )
305    new_geneve gnv0 6081
306
307    msg="NIC device goes down"
308    ifconfig $NSIM_NETDEV down
309    if [ $port -eq 1 ]; then
310	exp0=( 0 0 0 0 )
311	exp1=( 0 0 0 0 )
312    fi
313    check_tables
314    msg="NIC device goes up again"
315    ifconfig $NSIM_NETDEV up
316    exp0=( `mke 4789 1` `mke 4790 1` 0 0 )
317    exp1=( `mke 6081 2` 0 0 0 )
318    check_tables
319
320    cleanup_tuns
321
322    msg="tunnels destroyed"
323    exp0=( 0 0 0 0 )
324    exp1=( 0 0 0 0 )
325    check_tables
326
327    modprobe -r geneve
328    modprobe -r vxlan
329    modprobe -r udp_tunnel
330
331    check_tables
332done
333
334modprobe -r netdevsim
335
336# Module tests
337pfx="module tests"
338
339if modinfo netdevsim | grep udp_tunnel >/dev/null; then
340    err_cnt "netdevsim depends on udp_tunnel"
341else
342    pass_cnt
343fi
344
345modprobe netdevsim
346
347old_netdevs=$(ls /sys/class/net)
348port=0
349echo $NSIM_ID > /sys/bus/netdevsim/new_device
350echo 0 > $NSIM_DEV_SYS/del_port
351echo 1000 > $NSIM_DEV_DFS/udp_ports_sleep
352echo 0 > $NSIM_DEV_SYS/new_port
353NSIM_NETDEV=`get_netdev_name old_netdevs`
354
355msg="create VxLANs"
356exp0=( 0 0 0 0 ) # sleep is longer than out wait
357new_vxlan vxlan0 10000 $NSIM_NETDEV
358
359modprobe -r vxlan
360modprobe -r udp_tunnel
361
362msg="remove tunnels"
363exp0=( 0 0 0 0 )
364check_tables
365
366msg="create VxLANs"
367exp0=( 0 0 0 0 ) # sleep is longer than out wait
368new_vxlan vxlan0 10000 $NSIM_NETDEV
369
370exp0=( 0 0 0 0 )
371
372modprobe -r netdevsim
373modprobe netdevsim
374
375# Overflow the table
376
377function overflow_table0 {
378    local pfx=$1
379
380    msg="create VxLANs 1/5"
381    exp0=( `mke 10000 1` 0 0 0 )
382    new_vxlan vxlan0 10000 $NSIM_NETDEV
383
384    msg="create VxLANs 2/5"
385    exp0=( `mke 10000 1` `mke 10001 1` 0 0 )
386    new_vxlan vxlan1 10001 $NSIM_NETDEV
387
388    msg="create VxLANs 3/5"
389    exp0=( `mke 10000 1` `mke 10001 1` `mke 10002 1` 0 )
390    new_vxlan vxlan2 10002 $NSIM_NETDEV
391
392    msg="create VxLANs 4/5"
393    exp0=( `mke 10000 1` `mke 10001 1` `mke 10002 1` `mke 10003 1` )
394    new_vxlan vxlan3 10003 $NSIM_NETDEV
395
396    msg="create VxLANs 5/5"
397    new_vxlan vxlan4 10004 $NSIM_NETDEV
398}
399
400function overflow_table1 {
401    local pfx=$1
402
403    msg="create GENEVE 1/5"
404    exp1=( `mke 20000 2` 0 0 0 )
405    new_geneve gnv0 20000
406
407    msg="create GENEVE 2/5"
408    exp1=( `mke 20000 2` `mke 20001 2` 0 0 )
409    new_geneve gnv1 20001
410
411    msg="create GENEVE 3/5"
412    exp1=( `mke 20000 2` `mke 20001 2` `mke 20002 2` 0 )
413    new_geneve gnv2 20002
414
415    msg="create GENEVE 4/5"
416    exp1=( `mke 20000 2` `mke 20001 2` `mke 20002 2` `mke 20003 2` )
417    new_geneve gnv3 20003
418
419    msg="create GENEVE 5/5"
420    new_geneve gnv4 20004
421}
422
423echo $NSIM_ID > /sys/bus/netdevsim/new_device
424echo 0 > $NSIM_DEV_SYS/del_port
425
426for port in 0 1; do
427    if [ $port -ne 0 ]; then
428	echo 1 > $NSIM_DEV_DFS/udp_ports_open_only
429	echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
430    fi
431
432    echo $port > $NSIM_DEV_SYS/new_port
433    ifconfig $NSIM_NETDEV up
434
435    overflow_table0 "overflow NIC table"
436    overflow_table1 "overflow NIC table"
437
438    msg="replace VxLAN in overflow table"
439    exp0=( `mke 10000 1` `mke 10004 1` `mke 10002 1` `mke 10003 1` )
440    del_dev vxlan1
441
442    msg="vacate VxLAN in overflow table"
443    exp0=( `mke 10000 1` `mke 10004 1` 0 `mke 10003 1` )
444    del_dev vxlan2
445
446    msg="replace GENEVE in overflow table"
447    exp1=( `mke 20000 2` `mke 20004 2` `mke 20002 2` `mke 20003 2` )
448    del_dev gnv1
449
450    msg="vacate GENEVE in overflow table"
451    exp1=( `mke 20000 2` `mke 20004 2` 0 `mke 20003 2` )
452    del_dev gnv2
453
454    msg="table sharing - share"
455    exp1=( `mke 20000 2` `mke 20004 2` `mke 30001 4` `mke 20003 2` )
456    new_vxlan vxlanG0 30001 $NSIM_NETDEV 4 "gpe external"
457
458    msg="table sharing - overflow"
459    new_vxlan vxlanG1 30002 $NSIM_NETDEV 4 "gpe external"
460    msg="table sharing - overflow v6"
461    new_vxlan vxlanG2 30002 $NSIM_NETDEV 6 "gpe external"
462
463    exp1=( `mke 20000 2` `mke 30002 4` `mke 30001 4` `mke 20003 2` )
464    del_dev gnv4
465
466    msg="destroy NIC"
467    echo $port > $NSIM_DEV_SYS/del_port
468
469    cleanup_tuns
470    exp0=( 0 0 0 0 )
471    exp1=( 0 0 0 0 )
472done
473
474cleanup_nsim
475
476# Sync all
477pfx="sync all"
478
479echo $NSIM_ID > /sys/bus/netdevsim/new_device
480echo 0 > $NSIM_DEV_SYS/del_port
481echo 1 > $NSIM_DEV_DFS/udp_ports_sync_all
482
483for port in 0 1; do
484    if [ $port -ne 0 ]; then
485	echo 1 > $NSIM_DEV_DFS/udp_ports_open_only
486	echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
487    fi
488
489    echo $port > $NSIM_DEV_SYS/new_port
490    ifconfig $NSIM_NETDEV up
491
492    overflow_table0 "overflow NIC table"
493    overflow_table1 "overflow NIC table"
494
495    msg="replace VxLAN in overflow table"
496    exp0=( `mke 10000 1` `mke 10004 1` `mke 10002 1` `mke 10003 1` )
497    del_dev vxlan1
498
499    msg="vacate VxLAN in overflow table"
500    exp0=( `mke 10000 1` `mke 10004 1` 0 `mke 10003 1` )
501    del_dev vxlan2
502
503    msg="replace GENEVE in overflow table"
504    exp1=( `mke 20000 2` `mke 20004 2` `mke 20002 2` `mke 20003 2` )
505    del_dev gnv1
506
507    msg="vacate GENEVE in overflow table"
508    exp1=( `mke 20000 2` `mke 20004 2` 0 `mke 20003 2` )
509    del_dev gnv2
510
511    msg="table sharing - share"
512    exp1=( `mke 20000 2` `mke 20004 2` `mke 30001 4` `mke 20003 2` )
513    new_vxlan vxlanG0 30001 $NSIM_NETDEV 4 "gpe external"
514
515    msg="table sharing - overflow"
516    new_vxlan vxlanG1 30002 $NSIM_NETDEV 4 "gpe external"
517    msg="table sharing - overflow v6"
518    new_vxlan vxlanG2 30002 $NSIM_NETDEV 6 "gpe external"
519
520    exp1=( `mke 20000 2` `mke 30002 4` `mke 30001 4` `mke 20003 2` )
521    del_dev gnv4
522
523    msg="destroy NIC"
524    echo $port > $NSIM_DEV_SYS/del_port
525
526    cleanup_tuns
527    exp0=( 0 0 0 0 )
528    exp1=( 0 0 0 0 )
529done
530
531cleanup_nsim
532
533# Destroy full NIC
534pfx="destroy full"
535
536echo $NSIM_ID > /sys/bus/netdevsim/new_device
537echo 0 > $NSIM_DEV_SYS/del_port
538
539for port in 0 1; do
540    if [ $port -ne 0 ]; then
541	echo 1 > $NSIM_DEV_DFS/udp_ports_open_only
542	echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
543    fi
544
545    echo $port > $NSIM_DEV_SYS/new_port
546    ifconfig $NSIM_NETDEV up
547
548    overflow_table0 "destroy NIC"
549    overflow_table1 "destroy NIC"
550
551    msg="destroy NIC"
552    echo $port > $NSIM_DEV_SYS/del_port
553
554    cleanup_tuns
555    exp0=( 0 0 0 0 )
556    exp1=( 0 0 0 0 )
557done
558
559cleanup_nsim
560
561# IPv4 only
562pfx="IPv4 only"
563
564echo $NSIM_ID > /sys/bus/netdevsim/new_device
565echo 0 > $NSIM_DEV_SYS/del_port
566echo 1 > $NSIM_DEV_DFS/udp_ports_ipv4_only
567
568for port in 0 1; do
569    if [ $port -ne 0 ]; then
570	echo 1 > $NSIM_DEV_DFS/udp_ports_open_only
571	echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
572    fi
573
574    echo $port > $NSIM_DEV_SYS/new_port
575    ifconfig $NSIM_NETDEV up
576
577    msg="create VxLANs v6"
578    new_vxlan vxlanA0 10000 $NSIM_NETDEV 6
579
580    msg="create VxLANs v6"
581    new_vxlan vxlanA1 10000 $NSIM_NETDEV 6
582
583    ip link set dev vxlanA0 down
584    ip link set dev vxlanA0 up
585    check_tables
586
587    msg="create VxLANs v4"
588    exp0=( `mke 10000 1` 0 0 0 )
589    new_vxlan vxlan0 10000 $NSIM_NETDEV
590
591    msg="down VxLANs v4"
592    exp0=( 0 0 0 0 )
593    ip link set dev vxlan0 down
594    check_tables
595
596    msg="up VxLANs v4"
597    exp0=( `mke 10000 1` 0 0 0 )
598    ip link set dev vxlan0 up
599    check_tables
600
601    msg="destroy VxLANs v4"
602    exp0=( 0 0 0 0 )
603    del_dev vxlan0
604
605    msg="recreate VxLANs v4"
606    exp0=( `mke 10000 1` 0 0 0 )
607    new_vxlan vxlan0 10000 $NSIM_NETDEV
608
609    del_dev vxlanA0
610    del_dev vxlanA1
611
612    msg="destroy NIC"
613    echo $port > $NSIM_DEV_SYS/del_port
614
615    cleanup_tuns
616    exp0=( 0 0 0 0 )
617    exp1=( 0 0 0 0 )
618done
619
620cleanup_nsim
621
622# Failures
623pfx="error injection"
624
625echo $NSIM_ID > /sys/bus/netdevsim/new_device
626echo 0 > $NSIM_DEV_SYS/del_port
627
628for port in 0 1; do
629    if [ $port -ne 0 ]; then
630	echo 1 > $NSIM_DEV_DFS/udp_ports_open_only
631	echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
632    fi
633
634    echo $port > $NSIM_DEV_SYS/new_port
635    ifconfig $NSIM_NETDEV up
636
637    echo 110 > $NSIM_DEV_DFS/ports/$port/udp_ports_inject_error
638
639    msg="1 - create VxLANs v6"
640    exp0=( 0 0 0 0 )
641    new_vxlan vxlanA0 10000 $NSIM_NETDEV 6
642
643    msg="1 - create VxLANs v4"
644    exp0=( `mke 10000 1` 0 0 0 )
645    new_vxlan vxlan0 10000 $NSIM_NETDEV
646
647    msg="1 - remove VxLANs v4"
648    del_dev vxlan0
649
650    msg="1 - remove VxLANs v6"
651    exp0=( 0 0 0 0 )
652    del_dev vxlanA0
653
654    msg="2 - create GENEVE"
655    exp1=( `mke 20000 2` 0 0 0 )
656    new_geneve gnv0 20000
657
658    msg="2 - destroy GENEVE"
659    echo 2 > $NSIM_DEV_DFS/ports/$port/udp_ports_inject_error
660    exp1=( `mke 20000 2` 0 0 0 )
661    del_dev gnv0
662
663    msg="2 - create second GENEVE"
664    exp1=( 0 `mke 20001 2` 0 0 )
665    new_geneve gnv0 20001
666
667    msg="destroy NIC"
668    echo $port > $NSIM_DEV_SYS/del_port
669
670    cleanup_tuns
671    exp0=( 0 0 0 0 )
672    exp1=( 0 0 0 0 )
673done
674
675cleanup_nsim
676
677# netdev flags
678pfx="netdev flags"
679
680echo $NSIM_ID > /sys/bus/netdevsim/new_device
681echo 0 > $NSIM_DEV_SYS/del_port
682
683for port in 0 1; do
684    if [ $port -ne 0 ]; then
685	echo 1 > $NSIM_DEV_DFS/udp_ports_open_only
686	echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
687    fi
688
689    echo $port > $NSIM_DEV_SYS/new_port
690    ifconfig $NSIM_NETDEV up
691
692    msg="create VxLANs v6"
693    exp0=( `mke 10000 1` 0 0 0 )
694    new_vxlan vxlanA0 10000 $NSIM_NETDEV 6
695
696    msg="create VxLANs v4"
697    new_vxlan vxlan0 10000 $NSIM_NETDEV
698
699    msg="turn off"
700    exp0=( 0 0 0 0 )
701    ethtool -K $NSIM_NETDEV rx-udp_tunnel-port-offload off
702    check_tables
703
704    msg="turn on"
705    exp0=( `mke 10000 1` 0 0 0 )
706    ethtool -K $NSIM_NETDEV rx-udp_tunnel-port-offload on
707    check_tables
708
709    msg="remove both"
710    del_dev vxlanA0
711    exp0=( 0 0 0 0 )
712    del_dev vxlan0
713    check_tables
714
715    ethtool -K $NSIM_NETDEV rx-udp_tunnel-port-offload off
716
717    msg="create VxLANs v4 - off"
718    exp0=( 0 0 0 0 )
719    new_vxlan vxlan0 10000 $NSIM_NETDEV
720
721    msg="created off - turn on"
722    exp0=( `mke 10000 1` 0 0 0 )
723    ethtool -K $NSIM_NETDEV rx-udp_tunnel-port-offload on
724    check_tables
725
726    msg="destroy NIC"
727    echo $port > $NSIM_DEV_SYS/del_port
728
729    cleanup_tuns
730    exp0=( 0 0 0 0 )
731    exp1=( 0 0 0 0 )
732done
733
734cleanup_nsim
735
736# device initiated reset
737pfx="reset notification"
738
739echo $NSIM_ID > /sys/bus/netdevsim/new_device
740echo 0 > $NSIM_DEV_SYS/del_port
741
742for port in 0 1; do
743    if [ $port -ne 0 ]; then
744	echo 1 > $NSIM_DEV_DFS/udp_ports_open_only
745	echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
746    fi
747
748    echo $port > $NSIM_DEV_SYS/new_port
749    ifconfig $NSIM_NETDEV up
750
751    msg="create VxLANs v6"
752    exp0=( `mke 10000 1` 0 0 0 )
753    new_vxlan vxlanA0 10000 $NSIM_NETDEV 6
754
755    msg="create VxLANs v4"
756    new_vxlan vxlan0 10000 $NSIM_NETDEV
757
758    echo 1 > $NSIM_DEV_DFS/ports/$port/udp_ports_reset
759    check_tables
760
761    msg="NIC device goes down"
762    ifconfig $NSIM_NETDEV down
763    if [ $port -eq 1 ]; then
764	exp0=( 0 0 0 0 )
765	exp1=( 0 0 0 0 )
766    fi
767    check_tables
768
769    echo 1 > $NSIM_DEV_DFS/ports/$port/udp_ports_reset
770    check_tables
771
772    msg="NIC device goes up again"
773    ifconfig $NSIM_NETDEV up
774    exp0=( `mke 10000 1` 0 0 0 )
775    check_tables
776
777    msg="remove both"
778    del_dev vxlanA0
779    exp0=( 0 0 0 0 )
780    del_dev vxlan0
781    check_tables
782
783    echo 1 > $NSIM_DEV_DFS/ports/$port/udp_ports_reset
784    check_tables
785
786    msg="destroy NIC"
787    echo $port > $NSIM_DEV_SYS/del_port
788
789    cleanup_tuns
790    exp0=( 0 0 0 0 )
791    exp1=( 0 0 0 0 )
792done
793
794cleanup_nsim
795
796# shared port tables
797pfx="table sharing"
798
799echo $NSIM_ID > /sys/bus/netdevsim/new_device
800echo 0 > $NSIM_DEV_SYS/del_port
801
802echo 0 > $NSIM_DEV_DFS/udp_ports_open_only
803echo 1 > $NSIM_DEV_DFS/udp_ports_sleep
804echo 1 > $NSIM_DEV_DFS/udp_ports_shared
805
806old_netdevs=$(ls /sys/class/net)
807echo 1 > $NSIM_DEV_SYS/new_port
808NSIM_NETDEV=`get_netdev_name old_netdevs`
809old_netdevs=$(ls /sys/class/net)
810echo 2 > $NSIM_DEV_SYS/new_port
811NSIM_NETDEV2=`get_netdev_name old_netdevs`
812
813msg="VxLAN v4 devices"
814exp0=( `mke 4789 1` 0 0 0 )
815exp1=( 0 0 0 0 )
816new_vxlan vxlan0 4789 $NSIM_NETDEV
817new_vxlan vxlan1 4789 $NSIM_NETDEV2
818
819msg="VxLAN v4 devices go down"
820exp0=( 0 0 0 0 )
821ifconfig vxlan1 down
822ifconfig vxlan0 down
823check_tables
824
825for ifc in vxlan0 vxlan1; do
826    ifconfig $ifc up
827done
828
829msg="VxLAN v6 device"
830exp0=( `mke 4789 1` `mke 4790 1` 0 0 )
831new_vxlan vxlanC 4790 $NSIM_NETDEV 6
832
833msg="Geneve device"
834exp1=( `mke 6081 2` 0 0 0 )
835new_geneve gnv0 6081
836
837msg="NIC device goes down"
838ifconfig $NSIM_NETDEV down
839check_tables
840
841msg="NIC device goes up again"
842ifconfig $NSIM_NETDEV up
843check_tables
844
845for i in `seq 2`; do
846    msg="turn feature off - 1, rep $i"
847    ethtool -K $NSIM_NETDEV rx-udp_tunnel-port-offload off
848    check_tables
849
850    msg="turn feature off - 2, rep $i"
851    exp0=( 0 0 0 0 )
852    exp1=( 0 0 0 0 )
853    ethtool -K $NSIM_NETDEV2 rx-udp_tunnel-port-offload off
854    check_tables
855
856    msg="turn feature on - 1, rep $i"
857    exp0=( `mke 4789 1` `mke 4790 1` 0 0 )
858    exp1=( `mke 6081 2` 0 0 0 )
859    ethtool -K $NSIM_NETDEV rx-udp_tunnel-port-offload on
860    check_tables
861
862    msg="turn feature on - 2, rep $i"
863    ethtool -K $NSIM_NETDEV2 rx-udp_tunnel-port-offload on
864    check_tables
865done
866
867msg="tunnels destroyed 1"
868cleanup_tuns
869exp0=( 0 0 0 0 )
870exp1=( 0 0 0 0 )
871check_tables
872
873overflow_table0 "overflow NIC table"
874
875msg="re-add a port"
876
877echo 2 > $NSIM_DEV_SYS/del_port
878echo 2 > $NSIM_DEV_SYS/new_port
879check_tables
880
881msg="replace VxLAN in overflow table"
882exp0=( `mke 10000 1` `mke 10004 1` `mke 10002 1` `mke 10003 1` )
883del_dev vxlan1
884
885msg="vacate VxLAN in overflow table"
886exp0=( `mke 10000 1` `mke 10004 1` 0 `mke 10003 1` )
887del_dev vxlan2
888
889echo 1 > $NSIM_DEV_DFS/ports/$port/udp_ports_reset
890check_tables
891
892msg="tunnels destroyed 2"
893cleanup_tuns
894exp0=( 0 0 0 0 )
895exp1=( 0 0 0 0 )
896check_tables
897
898echo 1 > $NSIM_DEV_SYS/del_port
899echo 2 > $NSIM_DEV_SYS/del_port
900
901cleanup_nsim
902
903# Static IANA port
904pfx="static IANA vxlan"
905
906echo $NSIM_ID > /sys/bus/netdevsim/new_device
907echo 0 > $NSIM_DEV_SYS/del_port
908
909echo 1 > $NSIM_DEV_DFS/udp_ports_static_iana_vxlan
910STATIC_ENTRIES=( `mke 4789 1` )
911
912port=1
913old_netdevs=$(ls /sys/class/net)
914echo $port > $NSIM_DEV_SYS/new_port
915NSIM_NETDEV=`get_netdev_name old_netdevs`
916
917msg="check empty"
918exp0=( 0 0 0 0 )
919exp1=( 0 0 0 0 )
920check_tables
921
922msg="add on static port"
923new_vxlan vxlan0 4789 $NSIM_NETDEV
924new_vxlan vxlan1 4789 $NSIM_NETDEV
925
926msg="add on different port"
927exp0=( `mke 4790 1` 0 0 0 )
928new_vxlan vxlan2 4790 $NSIM_NETDEV
929
930cleanup_tuns
931
932msg="tunnels destroyed"
933exp0=( 0 0 0 0 )
934exp1=( 0 0 0 0 )
935check_tables
936
937msg="different type"
938new_geneve gnv0	4789
939
940cleanup_tuns
941cleanup_nsim
942
943# END
944
945modprobe -r netdevsim
946
947if [ $num_errors -eq 0 ]; then
948    echo "PASSED all $num_cases checks"
949else
950    echo "FAILED $num_errors/$num_cases checks"
951fi
952
953exit $EXIT_STATUS
954