CSIT-1477: add 1n_tx2 VPP Device
[csit.git] / resources / tools / testbed-setup / ansible / roles / vpp_device / files / csit-initialize-vfs.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2019 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                 sriov_totalvfs=$(< "${pci_path}"/sriov_totalvfs)
32                 ;;
33             "stop" )
34                 sriov_totalvfs=0
35                 ;;
36         esac
37         echo ${sriov_totalvfs} > "${pci_path}"/sriov_numvfs
38         # SR-IOV 802.1Q isolation
39         case "${1:-start}" in
40             "start" )
41                 pf=$(basename "${pci_path}"/net/*)
42                 for vf in $(seq "${sriov_totalvfs}"); do
43                     # PCI address index in array (pairing siblings).
44                     if [[ -n ${PF_INDICES[@]} ]]
45                     then
46                         vlan_pf_idx=${PF_INDICES[$pci_addr]}
47                     else
48                         vlan_pf_idx=$(( pci_idx % (${#PCI_WHITELIST[@]} / 2) ))
49                     fi
50                     # 802.1Q base offset.
51                     vlan_bs_off=1100
52                     # 802.1Q PF PCI address offset.
53                     vlan_pf_off=$(( vlan_pf_idx * 100 + vlan_bs_off ))
54                     # 802.1Q VF PCI address offset.
55                     vlan_vf_off=$(( vlan_pf_off + vf - 1 ))
56                     # VLAN string.
57                     vlan_str="vlan ${vlan_vf_off}"
58                     # MAC string.
59                     mac5="$(printf '%x' ${pci_idx})"
60                     mac6="$(printf '%x' $(( vf - 1 )))"
61                     mac_str="mac ba:dc:0f:fe:${mac5}:${mac6}"
62                     # Set 802.1Q VLAN id and MAC address
63                     ip link set ${pf} vf $(( vf - 1 )) ${mac_str} ${vlan_str}
64                     ip link set ${pf} vf $(( vf - 1 )) trust on
65                     ip link set ${pf} vf $(( vf - 1 )) spoof off
66                 done
67                 pci_idx=$(( pci_idx + 1 ))
68                 ;;
69         esac
70         rmmod i40evf
71         modprobe i40evf
72     fi
73 done

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.