Hyunsun Moon | f9cac59 | 2020-11-10 00:46:41 -0800 | [diff] [blame] | 1 | #!/bin/bash -x |
| 2 | |
| 3 | set -o errexit |
| 4 | set -o pipefail |
| 5 | set -o nounset |
| 6 | |
| 7 | exec 3>&1 |
| 8 | exec &>>/var/log/$(basename $0).log |
| 9 | |
| 10 | PATH="$CNI_PATH:$(dirname "${BASH_SOURCE[0]}"):$PATH" |
| 11 | CNI_CONF=$(cat /dev/stdin) |
| 12 | |
| 13 | get_peer_name() { |
| 14 | echo "$1-vdev" |
| 15 | } |
| 16 | |
| 17 | get_mac_with_vfpci() { |
| 18 | local pf=$(readlink /sys/devices/pci*/*/$1/physfn | awk '{print substr($1,4)}') |
| 19 | local pfName=$(ls /sys/devices/pci*/*/$pf/net/ | head -1) |
| 20 | local idx=$(ls -l /sys/devices/pci*/*/$pf | awk -v vf=$1 'substr($11,4)==vf {print substr($9,7)}') |
| 21 | local mac=$(ip link show dev $pfName | awk -v idx="$idx" '$1=="vf" && $2==idx {print substr($4,1,17)}') |
| 22 | echo $mac |
| 23 | } |
| 24 | |
Hyunsun Moon | 6d8b21b | 2021-05-20 18:12:09 -0700 | [diff] [blame^] | 25 | add_vlan() { |
| 26 | vfpci=$(echo $CNI_CONF | jq -r '.deviceID') |
| 27 | local pf=$(readlink /sys/devices/pci*/*/$vfpci/physfn | awk '{print substr($1,4)}') |
| 28 | local pfName=$(ls /sys/devices/pci*/*/$pf/net/ | head -1) |
| 29 | local idx=$(ls -l /sys/devices/pci*/*/$pf | awk -v vf=$vfpci 'substr($11,4)==vf {print substr($9,7)}') |
| 30 | |
| 31 | vlan=$(echo $CNI_CONF | jq -r '.vlan // empty') |
| 32 | if [ ! -z "$vlan" ]; then |
| 33 | ip link set $pfName vf $idx vlan $vlan |
| 34 | fi |
| 35 | } |
| 36 | |
Hyunsun Moon | f9cac59 | 2020-11-10 00:46:41 -0800 | [diff] [blame] | 37 | ipam() { |
| 38 | local plugin=$(echo $CNI_CONF | jq -r '.ipam.type') |
| 39 | local res=$(echo $"$CNI_CONF" | "$plugin" | jq -c '.') |
| 40 | echo $res |
| 41 | } |
| 42 | |
| 43 | add_pair_ns() { |
| 44 | vfpci=$(echo $CNI_CONF | jq -r '.deviceID') |
Hyunsun Moon | 1711b2c | 2021-05-07 15:31:01 -0700 | [diff] [blame] | 45 | mac=$(echo $CNI_CONF | jq -r '.runtimeConfig.mac // empty') |
Hyunsun Moon | f9cac59 | 2020-11-10 00:46:41 -0800 | [diff] [blame] | 46 | if [ -z "$mac" ]; then |
| 47 | mac=$(get_mac_with_vfpci $vfpci) |
| 48 | fi |
| 49 | peer=$(get_peer_name $CNI_IFNAME) |
| 50 | ip=$1 |
| 51 | |
| 52 | mkdir -p /var/run/netns/ |
| 53 | ln -sfT $CNI_NETNS /var/run/netns/$CNI_CONTAINERID |
| 54 | |
| 55 | ip netns exec $CNI_CONTAINERID ip link add $CNI_IFNAME type veth peer name $peer |
| 56 | ip netns exec $CNI_CONTAINERID ip link set $CNI_IFNAME addr $mac up alias $vfpci |
| 57 | ip netns exec $CNI_CONTAINERID ip link set $peer up |
| 58 | ip netns exec $CNI_CONTAINERID ip addr add $ip dev $CNI_IFNAME |
| 59 | } |
| 60 | |
| 61 | delete_pair_ns() { |
| 62 | ip netns exec $CNI_CONTAINERID ip link del $CNI_IFNAME |
| 63 | } |
| 64 | |
Hyunsun Moon | 6d8b21b | 2021-05-20 18:12:09 -0700 | [diff] [blame^] | 65 | delete_vlan() { |
| 66 | vfpci=$(echo $CNI_CONF | jq -r '.deviceID') |
| 67 | local pf=$(readlink /sys/devices/pci*/*/$vfpci/physfn | awk '{print substr($1,4)}') |
| 68 | local pfName=$(ls /sys/devices/pci*/*/$pf/net/ | head -1) |
| 69 | local idx=$(ls -l /sys/devices/pci*/*/$pf | awk -v vf=$vfpci 'substr($11,4)==vf {print substr($9,7)}') |
| 70 | |
| 71 | vlan=$(echo $CNI_CONF | jq -r '.vlan // empty') |
| 72 | if [ ! -z "$vlan" ]; then |
| 73 | ip link set $pfName vf $idx vlan 0 |
| 74 | fi |
| 75 | } |
| 76 | |
Hyunsun Moon | f9cac59 | 2020-11-10 00:46:41 -0800 | [diff] [blame] | 77 | case $CNI_COMMAND in |
| 78 | ADD) |
| 79 | res=$(ipam) |
| 80 | ip=$(echo $res | jq -r '.ips[0].address') |
| 81 | add_pair_ns $ip |
Hyunsun Moon | 6d8b21b | 2021-05-20 18:12:09 -0700 | [diff] [blame^] | 82 | add_vlan |
Hyunsun Moon | f9cac59 | 2020-11-10 00:46:41 -0800 | [diff] [blame] | 83 | echo '{"cniVersion":"0.3.1"}' | jq -c --arg ip $ip '.ips[0].address = $ip' >&3 |
| 84 | ;; |
| 85 | DEL) |
| 86 | set +o errexit |
| 87 | ipam |
| 88 | delete_pair_ns |
Hyunsun Moon | 6d8b21b | 2021-05-20 18:12:09 -0700 | [diff] [blame^] | 89 | delete_vlan |
Hyunsun Moon | f9cac59 | 2020-11-10 00:46:41 -0800 | [diff] [blame] | 90 | set -o errexit |
| 91 | ;; |
| 92 | *) |
| 93 | echo "CNI_COMMAND=[ADD|DEL] only supported" |
| 94 | exit 1 |
| 95 | ;; |
| 96 | esac |