06b99231284d2c6e18e62fc4f71a5e0a4dfcfa2e
[csit.git] / resources / tools / testbed-setup / ansible / roles / vpp_device / files / csit-initialize-vfs.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2018 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 # Add Intel Corporation Ethernet Controller 10G X550T to blacklist.
21 # See http://pci-ids.ucw.cz/v2.2/pci.ids for more info.
22 pci_blacklist=($(lspci -Dmmd ':1563:0200' | cut -f1 -d' '))
23
24 # Add Intel Corporation Ethernet Controller X710 for 10GbE SFP+ to whitelist.
25 # See http://pci-ids.ucw.cz/v2.2/pci.ids for more info.
26 pci_whitelist=($(lspci -Dmmd ':1572:0200' | cut -f1 -d' '))
27
28 # Initilize whitelisted NICs with maximum number of VFs.
29 pci_idx=0
30 for pci_addr in ${pci_whitelist[@]}; do
31     if ! [[ ${pci_blacklist[*]} =~ "${pci_addr}" ]]; then
32         pci_path="/sys/bus/pci/devices/${pci_addr}"
33         # SR-IOV initialization
34         case "${1:-start}" in
35             "start" )
36                 sriov_totalvfs=$(< "${pci_path}"/sriov_totalvfs)
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                     vlan_pf_idx=$(( pci_idx % (${#pci_whitelist[@]} / 2) ))
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