afa84ae15af003c2a1cee28c41c5493ad28a86f4
[csit.git] / fdio.infra.ansible / roles / vpp_device / files / csit-initialize-vfs.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2021 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # CSIT SRIOV VF initialization and isolation.
17
18 set -euo pipefail
19
20 SCRIPT_DIR="$(dirname $(readlink -e "${BASH_SOURCE[0]}"))"
21 source "${SCRIPT_DIR}/csit-initialize-vfs-data.sh"
22
23 # Initilize whitelisted NICs with maximum number of VFs.
24 pci_idx=0
25 for pci_addr in ${PCI_WHITELIST[@]}; do
26     if ! [[ ${PCI_BLACKLIST[*]} =~ "${pci_addr}" ]]; then
27         pci_path="/sys/bus/pci/devices/${pci_addr}"
28         # SR-IOV initialization
29         case "${1:-start}" in
30             "start" )
31                 if [ $(< "${pci_path}"/sriov_totalvfs) -gt 128 ]
32                 then
33                     sriov_totalvfs=128
34                 else
35                     sriov_totalvfs=$(< "${pci_path}"/sriov_totalvfs)
36                 fi
37                 ;;
38             "stop" )
39                 sriov_totalvfs=0
40                 ;;
41         esac
42         echo ${sriov_totalvfs} > "${pci_path}"/sriov_numvfs
43         # SR-IOV 802.1Q isolation
44         case "${1:-start}" in
45             "start" )
46                 pf=$(basename "${pci_path}"/net/*)
47                 for vf in $(seq "${sriov_totalvfs}"); do
48                     # PCI address index in array (pairing siblings).
49                     if [[ -n ${PF_INDICES[@]} ]]
50                     then
51                         vlan_pf_idx=${PF_INDICES[$pci_addr]}
52                     else
53                         vlan_pf_idx=$(( pci_idx % (${#PCI_WHITELIST[@]} / 2) ))
54                     fi
55                     # 802.1Q base offset.
56                     vlan_bs_off=1100
57                     # 802.1Q PF PCI address offset.
58                     vlan_pf_off=$(( vlan_pf_idx * 100 + vlan_bs_off ))
59                     # 802.1Q VF PCI address offset.
60                     vlan_vf_off=$(( vlan_pf_off + vf - 1 ))
61                     # VLAN string.
62                     vlan_str="vlan ${vlan_vf_off}"
63                     # MAC string.
64                     mac5="$(printf '%x' ${pci_idx})"
65                     mac6="$(printf '%x' $(( vf - 1 )))"
66                     mac_str="mac ba:dc:0f:fe:${mac5}:${mac6}"
67                     # Set 802.1Q VLAN id and MAC address
68                     ip link set ${pf} vf $(( vf - 1 )) ${mac_str} ${vlan_str}
69                     ip link set ${pf} vf $(( vf - 1 )) trust on
70                     ip link set ${pf} vf $(( vf - 1 )) spoof off
71                     sleep .5
72                 done
73                 pci_idx=$(( pci_idx + 1 ))
74                 ;;
75         esac
76     fi
77 done