summaryrefslogtreecommitdiff
path: root/contrib/scripts/test-create-many-device-setup.sh
blob: 230833ff2496c980d4d8c54d4317bd2de57b4ed6 (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
#!/bin/bash

set -x

die() {
    printf '%s\n' "$*" >&1
    exit 1
}

ARG_OP="$1"
shift
test -n "$ARG_OP" || die "specify the operation (setup, cleanup)"

test "$USER" = root || die "must run as root"

NUM_DEVS="${NUM_DEVS:-50}"


DNSMASQ_PIDFILE="/tmp/nm-test-create-many-device-setup.dnsmasq.pid"
NM_TEST_CONF="/etc/NetworkManager/conf.d/99-my-test.conf"
TEST_NETNS="T"


_dnsmasq_kill() {
    pkill -F "$DNSMASQ_PIDFILE"
    rm -rf "$DNSMASQ_PIDFILE"
}

_link_delete_all() {
    ip link | sed -n 's/^[0-9]\+:.*\(t-[^@:]\+\)@.*/\1/p' | xargs -n 1 ip link delete
}

cleanup_base() {
    ip netns delete "$TEST_NETNS"
    _dnsmasq_kill
    _link_delete_all
    rm -rf "$NM_TEST_CONF"
    rm -rf /run/NetworkManager/system-connections/c-*.nmconnection
}

cmd_cleanup() {
    systemctl stop NetworkManager
    cleanup_base
    systemctl unmask NetworkManager-dispatcher
    systemctl enable NetworkManager-dispatcher
    systemctl start NetworkManager
}

cmd_setup() {

    systemctl stop NetworkManager
    systemctl mask NetworkManager-dispatcher
    systemctl stop NetworkManager-dispatcher

    cleanup_base

    ip netns add "$TEST_NETNS"
    ip --netns "$TEST_NETNS" link add t-br0 type bridge
    ip --netns "$TEST_NETNS" link set t-br0 type bridge stp_state 0
    ip --netns "$TEST_NETNS" link set t-br0 up
    ip --netns "$TEST_NETNS" addr add 172.16.0.1/16 dev t-br0
    ip netns exec "$TEST_NETNS" \
        dnsmasq \
            --conf-file=/dev/null \
            --pid-file="$DNSMASQ_PIDFILE" \
            --no-hosts \
            --keep-in-foreground \
            --bind-interfaces \
            --except-interface=lo \
            --clear-on-reload \
            --listen-address=172.16.0.1 \
            --dhcp-range=172.16.1.1,172.16.20.1,60 \
            --no-ping \
            &
    disown
    for i in `seq "$NUM_DEVS"`; do
        ip --netns "$TEST_NETNS" link add t-a$i type veth peer t-b$i
        ip --netns "$TEST_NETNS" link set t-a$i up
        ip --netns "$TEST_NETNS" link set t-b$i up master t-br0
    done

    cat <<EOF > "$NM_TEST_CONF"
[main]
dhcp=internal
no-auto-default=interface-name:t-a*
[device-99-my-test]
match-device=interface-name:t-a*
managed=1
[logging]
level=INFO
[connectivity]
enabled=0
EOF

    systemctl start NetworkManager

    for i in `seq "$NUM_DEVS"`; do
      ip --netns "$TEST_NETNS" link set t-a$i netns $$
    done

    for i in `seq "$NUM_DEVS"`; do
        nmcli connection add save no type ethernet con-name c-a$i ifname t-a$i autoconnect no ipv4.method auto ipv6.method auto
    done
}


case "$ARG_OP" in
    "setup")
        cmd_setup
        ;;
    "cleanup")
        cmd_cleanup
        ;;
    *)
        die "Unknown command \"$ARG_OP\""
        ;;
esac