1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0-only
3
4NSIM_ID=$((RANDOM % 1024))
5NSIM_DEV_SYS=/sys/bus/netdevsim/devices/netdevsim$NSIM_ID
6NSIM_DEV_DFS=/sys/kernel/debug/netdevsim/netdevsim$NSIM_ID/ports/0
7NSIM_NETDEV=
8num_passes=0
9num_errors=0
10
11function cleanup_nsim {
12    if [ -e $NSIM_DEV_SYS ]; then
13	echo $NSIM_ID > /sys/bus/netdevsim/del_device
14    fi
15}
16
17function cleanup {
18    cleanup_nsim
19}
20
21trap cleanup EXIT
22
23function get_netdev_name {
24    local -n old=$1
25
26    new=$(ls /sys/class/net)
27
28    for netdev in $new; do
29	for check in $old; do
30            [ $netdev == $check ] && break
31	done
32
33	if [ $netdev != $check ]; then
34	    echo $netdev
35	    break
36	fi
37    done
38}
39
40function check {
41    local code=$1
42    local str=$2
43    local exp_str=$3
44
45    if [ $code -ne 0 ]; then
46	((num_errors++))
47	return
48    fi
49
50    if [ "$str" != "$exp_str"  ]; then
51	echo -e "Expected: '$exp_str', got '$str'"
52	((num_errors++))
53	return
54    fi
55
56    ((num_passes++))
57}
58
59function make_netdev {
60    # Make a netdevsim
61    old_netdevs=$(ls /sys/class/net)
62
63    if ! $(lsmod | grep -q netdevsim); then
64	modprobe netdevsim
65    fi
66
67    echo $NSIM_ID > /sys/bus/netdevsim/new_device
68    echo `get_netdev_name old_netdevs`
69}
70