summaryrefslogtreecommitdiff
path: root/examples/dispatcher/geneve
blob: b4569902b7b956601225949e689aee6ad238808f (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
#!/bin/sh
# NetworkManager device handler for geneve interfaces.
#
# Put this script in "/etc/NetworkManager/dispatcher.d/device" and
# create a generic connection with:
#
#   nmcli connection add type generic \
#                        con-name geneve1 \
#                        ifname geneve1 \
#                        generic.device-handler geneve \
#                        connection.autoconnect no
#
# Then add the following parameters at the bottom of file
# /etc/NetworkManager/system-connections/geneve1 , and do a "nmcli
# connection reload".
#
#   [user]
#   geneve.remote=172.25.14.15
#   geneve.vni=5555
#   geneve.dstport=6082
#
# Now, when activating connection "geneve1", NetworkManager calls this
# script to create the device according to parameters in the user
# settings, and then performs IP configuration on it.
#
# This script will use the following properties from the [user] setting:
#
#  - geneve.remote     (required)
#  - geneve.vni        (required)
#  - geneve.ttl
#  - geneve.dstport
#
# See the GENEVE section of "man ip-link" for more details.

ifname=$1
action=$2

require()
{
    if ! command -v "$1" > /dev/null ; then
        echo "ERROR='$1' is not installed"
        exit 1
    fi
}

get_iplink_param()
{
    ip -j -d link show "$1" | jq -r .[0].linkinfo.info_data."$2"
}

require jq

if [ "$action" = device-add ]; then
    # Create the interface here and then write a line to stdout
    # containing "IFINDEX=" followed by the ifindex of the interface
    # just created, so that NetworkManager can manage it and configure
    # IPs on the interface. The name of the returned ifindex must be
    # the same as "$ifname".

    vni=$CONNECTION_USER_GENEVE__VNI
    remote=$CONNECTION_USER_GENEVE__REMOTE
    ttl=$CONNECTION_USER_GENEVE__TTL
    dstport=$CONNECTION_USER_GENEVE__DSTPORT

    if [ -z "$vni" ] || [ -z "$remote" ]; then
        echo "ERROR=Missing VNI or remote"
        exit 2
    fi

    if [ -d /sys/class/net/"$ifname" ]; then
        # If the interface already exists, reuse it after checking
        # that the parameters are compatible.
        # NOTE: it's not strictly necessary to handle an already
        # existing interface, but if the script doesn't, it won't be
        # possible to re-activate the connection when it's up.

        if [ "$vni" != "$(get_iplink_param "$ifname" id)" ]; then
            echo "ERROR=The link already exists with different VNI"
            exit 3
        fi
        if [ "$remote" != "$(get_iplink_param "$ifname" remote)" ]; then
            echo "ERROR=The link already exists with different remote"
            exit 3
        fi
        if [ -n "$ttl" ] && [ "$ttl" != "$(get_iplink_param "$ifname" ttl)" ]; then
            echo "ERROR=The link already exists with different TTL"
            exit 3
        fi
        if [ -n "$dstport" ] && [ "$dstport" != "$(get_iplink_param "$ifname" port)" ]; then
            echo "ERROR=The link already exists with different dstport"
            exit 3
        fi
        echo IFINDEX="$(cat /sys/class/net/"$ifname"/ifindex)"
        exit 0
    fi

    # The interface doesn't exist, create it

    if ! err=$(ip link add "$ifname" type geneve vni "$vni" remote "$remote" \
         ${tos:+tos "$tos"} \
         ${ttl:+ttl "$ttl"} \
         ${dstport:+dstport "$dstport"} 2>&1); then
        echo "ERROR=Failed creating the interface: $err"
        exit 4
    fi

    echo IFINDEX="$(cat /sys/class/net/"$ifname"/ifindex)"
    exit 0
elif [ "$action" = device-delete ]; then
    # NM automatically deletes the link on deactivation,
    # it's not necessary to do it here. The "device-delete"
    # action can be used to perform additional operations.
    exit 0
fi

exit 5