summaryrefslogtreecommitdiff
path: root/src/udev/vyos_vf_name
blob: bd6c9d0cd31d19e183210da132d8c9973327adb1 (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
#!/bin/sh
# vyos_vf_name - pick a non-colliding VF interface name.
# Called by udev rules for VF devices that still have plain ethN names.
#
# Written as a POSIX shell script so it works in both initramfs (no Python3)
# and the normal rootfs udev context.
#
# Usage: vyos_vf_name <current_ifname>
# Prints the target vf_ethN name to stdout.

set -e

if [ -z "$1" ]; then
    exit 1
fi

IFNAME="$1"
LOCK_DIR="/run/udev"
LOCK_PATH="${LOCK_DIR}/vyos_vf_name.lock"

# Already normalized - return as-is.
case "$IFNAME" in
    vf_*)
        echo "$IFNAME"
        exit 0
        ;;
esac

mkdir -p "$LOCK_DIR"

# Acquire a lock to avoid race conditions when multiple VF devices are being
# renamed concurrently.
LOCK_ACQUIRED=0
I=0
while [ "$I" -lt 200 ]; do
    if mkdir "$LOCK_PATH" 2>/dev/null; then
        LOCK_ACQUIRED=1
        break
    fi
    I=$((I + 1))
    sleep 0.01
done

[ "$LOCK_ACQUIRED" -eq 1 ] || exit 1

cleanup() {
    rmdir "$LOCK_PATH" 2>/dev/null || true
}
trap cleanup EXIT INT TERM

# Preferred: preserve the original ethN index when vf_ethN is not yet taken.
case "$IFNAME" in
    eth*)
        PREFERRED="${IFNAME#eth}"
        # Validate it is a pure integer.
        case "$PREFERRED" in
            *[!0-9]*)
                ;;
            *)
                if ! [ -d "/sys/class/net/vf_eth${PREFERRED}" ]; then
                    echo "vf_eth${PREFERRED}"
                    exit 0
                fi
                ;;
        esac
        ;;
esac

# Fallback: allocate just above the highest ethN synthetic index present to
# avoid collisions.
MAX=-1
for D in /sys/class/net/eth*; do
    [ -d "$D" ] || continue
    N="${D##*/eth}"
    # Skip if not a pure integer (e.g. no match expands to literal path).
    case "$N" in *[!0-9]*) continue;; esac
    [ "$N" -gt "$MAX" ] && MAX="$N"
done

IDX=$((MAX + 1))
[ "$IDX" -lt 0 ] && IDX=0

# Skip any indices already occupied by existing vf_ethN interfaces.
while [ -d "/sys/class/net/vf_eth${IDX}" ]; do
    IDX=$((IDX + 1))
done

echo "vf_eth${IDX}"