| 1 |
# Copyright (C) 2018 AT&T |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
|
| 16 |
# |
| 17 |
# This class implements a utility functions for SRIOV NIC Switch Driver |
| 18 |
# |
| 19 |
import os |
| 20 |
from oslo_log import log as logging |
| 21 |
|
| 22 |
from neutron.agent.linux import utils |
| 23 |
from neutron_lib.api.definitions import portbindings |
| 24 |
from neutron_taas.services.taas.drivers.linux import sriov_nic_exceptions \ |
| 25 |
as taas_exc |
| 26 |
|
| 27 |
import glob |
| 28 |
import re |
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
|
| 32 |
|
| 33 |
class SriovNicUtils: |
| 34 |
# |
| 35 |
# Initializes internal state for specified # keys |
| 36 |
# |
| 37 |
def __init__(self): |
| 38 |
LOG.debug("SriovNicUtils: init called") |
| 39 |
|
| 40 |
# |
| 41 |
# Returns specified key-value affilation, if it exists. |
| 42 |
# |
| 43 |
def execute_sysfs_command(self, command, ts_port_params, |
| 44 |
src_port_params, |
| 45 |
common_vlans_ranges_str, |
| 46 |
vf_to_vf_all_vlans, direction): |
| 47 |
"""Execute the SRIOV NIC Switch Driver's SysFs command. |
| 48 |
|
| 49 |
# Mirror traffic from VF0 to VF3 on interface p2p1, ex. |
| 50 |
echo add 3 > /sys/class/net/p2p1/device/sriov/0/ingress_mirror |
| 51 |
echo add 3 > /sys/class/net/p2p1/device/sriov/0/egress_mirror |
| 52 |
|
| 53 |
# Remove traffic mirroring from VF0 to VF3 on interface p2p1, ex. |
| 54 |
echo rem 3 > /sys/class/net/p2p1/device/sriov/0/ingress_mirror |
| 55 |
echo rem 3 > /sys/class/net/p2p1/device/sriov/0/egress_mirror |
| 56 |
|
| 57 |
# Add VLANs 2,6,18-22 to Mirror traffic to VF3 (port p2p1), ex. |
| 58 |
echo add 2,6,18-22 > /sys/class/net/p2p1/device/sriov/3/vlan_mirror |
| 59 |
|
| 60 |
# Remove VLANs 2,6,18-22 to Mirror traffic to VF3 (port p2p1), ex. |
| 61 |
echo rem 2,6,18-22 > /sys/class/net/p2p1/device/sriov/3/vlan_mirror |
| 62 |
|
| 63 |
# Remove all VLANs from mirroring at VF3, ex. |
| 64 |
echo rem 0-4095 > /sys/class/net/p1p1/device/sriov/3/vlan_mirror |
| 65 |
""" |
| 66 |
LOG.debug("TaaS sysfs command params %(command)s, " |
| 67 |
"ts_port_params %(ts_port_params)s, " |
| 68 |
"src_port_params %(src_port_params)s, " |
| 69 |
"common_vlans_ranges_str %(common_vlans_ranges_str)s; " |
| 70 |
"vf_to_vf_all_vlans %(vf_to_vf_all_vlans)s; " |
| 71 |
"direction %(direction)s; ", |
| 72 |
{'command': command, |
| 73 |
'ts_port_params': ts_port_params, |
| 74 |
'src_port_params': src_port_params, |
| 75 |
'common_vlans_ranges_str': common_vlans_ranges_str, |
| 76 |
'vf_to_vf_all_vlans': vf_to_vf_all_vlans, |
| 77 |
'direction': direction}) |
| 78 |
if vf_to_vf_all_vlans: |
| 79 |
if direction in ['OUT', 'BOTH']: |
| 80 |
commit_cmd = ['i40e_sysfs_command', |
| 81 |
ts_port_params['pf_device'], |
| 82 |
src_port_params['vf_index'], |
| 83 |
'egress_mirror', |
| 84 |
command, |
| 85 |
ts_port_params['vf_index']] |
| 86 |
|
| 87 |
try: |
| 88 |
LOG.info("TaaS executing sysfs_command %(command)s", |
| 89 |
{'command': commit_cmd}) |
| 90 |
utils.execute(commit_cmd, run_as_root=True, |
| 91 |
privsep_exec=True) |
| 92 |
except (OSError, RuntimeError, IndexError, ValueError) as e: |
| 93 |
LOG.error("Exception while executing Sysfs command " |
| 94 |
"Exception: %s", e) |
| 95 |
return |
| 96 |
|
| 97 |
if direction in ['IN', 'BOTH']: |
| 98 |
commit_cmd = ['i40e_sysfs_command', |
| 99 |
ts_port_params['pf_device'], |
| 100 |
src_port_params['vf_index'], |
| 101 |
'ingress_mirror', |
| 102 |
command, |
| 103 |
ts_port_params['vf_index']] |
| 104 |
|
| 105 |
try: |
| 106 |
LOG.info("TaaS executing sysfs_command %(command)s", |
| 107 |
{'command': commit_cmd}) |
| 108 |
utils.execute(commit_cmd, run_as_root=True, |
| 109 |
privsep_exec=True) |
| 110 |
except (OSError, RuntimeError, IndexError, ValueError) as e: |
| 111 |
LOG.error("Exception while executing Sysfs command " |
| 112 |
"Exception: %s", e) |
| 113 |
return |
| 114 |
else: |
| 115 |
if direction != 'BOTH': |
| 116 |
LOG.warning("SRIOV NIC Switch driver only supports" |
| 117 |
"direction=BOTH for specific VLANs' mirroring") |
| 118 |
|
| 119 |
commit_cmd = ['i40e_sysfs_command', |
| 120 |
ts_port_params['pf_device'], |
| 121 |
ts_port_params['vf_index'], |
| 122 |
'vlan_mirror', |
| 123 |
command, |
| 124 |
common_vlans_ranges_str] |
| 125 |
|
| 126 |
try: |
| 127 |
LOG.info("TaaS executing sysfs_command %(command)s", |
| 128 |
{'command': commit_cmd}) |
| 129 |
utils.execute(commit_cmd, run_as_root=True, |
| 130 |
privsep_exec=True) |
| 131 |
except (OSError, RuntimeError, IndexError, ValueError) as e: |
| 132 |
LOG.error("Exception while executing Sysfs command " |
| 133 |
"Exception: %s", e) |
| 134 |
return |
| 135 |
|
| 136 |
def _get_sysfs_netdev_path(self, pci_addr, pf_interface): |
| 137 |
"""Get the sysfs path based on the PCI address of the device. |
| 138 |
|
| 139 |
Assumes a networking device - will not check for the existence |
| 140 |
of the path. |
| 141 |
""" |
| 142 |
if pf_interface: |
| 143 |
return "/sys/bus/pci/devices/%s/physfn/net" % pci_addr |
| 144 |
return "/sys/bus/pci/devices/%s/net" % pci_addr |
| 145 |
|
| 146 |
def get_ifname_by_pci_address(self, pci_addr, pf_interface=False): |
| 147 |
"""Get the interface name based on a VF's pci address. |
| 148 |
|
| 149 |
The returned interface name is either the parent PF's or that of |
| 150 |
the VF itself based on the argument of pf_interface. |
| 151 |
""" |
| 152 |
dev_path = self._get_sysfs_netdev_path(pci_addr, pf_interface) |
| 153 |
try: |
| 154 |
dev_info = os.listdir(dev_path) |
| 155 |
return dev_info.pop() |
| 156 |
except Exception: |
| 157 |
raise taas_exc.PciDeviceNotFoundById(id=pci_addr) |
| 158 |
|
| 159 |
def get_mac_by_pci_address(self, pci_addr, pf_interface=False): |
| 160 |
"""Get the MAC address of the nic based on its PCI address. |
| 161 |
|
| 162 |
Raises PciDeviceNotFoundById in case the pci device is not a NIC |
| 163 |
""" |
| 164 |
dev_path = self._get_sysfs_netdev_path(pci_addr, pf_interface) |
| 165 |
if_name = self.get_ifname_by_pci_address(pci_addr, pf_interface) |
| 166 |
addr_file = os.path.join(dev_path, if_name, 'address') |
| 167 |
|
| 168 |
try: |
| 169 |
with open(addr_file, encoding="utf-8") as f: |
| 170 |
mac = next(f).strip() |
| 171 |
return mac |
| 172 |
except (OSError, StopIteration) as e: |
| 173 |
LOG.warning("Could not find the expected sysfs file for " |
| 174 |
"determining the MAC address of the PCI device " |
| 175 |
"%(addr)s. May not be a NIC. Error: %(e)s", |
| 176 |
{'addr': pci_addr, 'e': e}) |
| 177 |
raise taas_exc.PciDeviceNotFoundById(id=pci_addr) |
| 178 |
|
| 179 |
def get_vf_num_by_pci_address(self, pci_addr): |
| 180 |
"""Get the VF number based on a VF's pci address |
| 181 |
|
| 182 |
A VF is associated with an VF number, which ip link command uses to |
| 183 |
configure it. This can be obtained from the PCI device filesystem. |
| 184 |
""" |
| 185 |
VIRTFN_RE = re.compile(r"virtfn(\d+)") |
| 186 |
virtfns_path = "/sys/bus/pci/devices/%s/physfn/virtfn*" % (pci_addr) |
| 187 |
vf_num = None |
| 188 |
LOG.debug("TaaS: pci_addr: %(pci_addr)s " |
| 189 |
"virtfns_path: %(virtfns_path)s", |
| 190 |
{'pci_addr': pci_addr, |
| 191 |
'virtfns_path': virtfns_path}) |
| 192 |
try: |
| 193 |
for vf_path in glob.iglob(virtfns_path): |
| 194 |
if re.search(pci_addr, os.readlink(vf_path)): |
| 195 |
t = VIRTFN_RE.search(vf_path) |
| 196 |
vf_num = t.group(1) |
| 197 |
break |
| 198 |
except Exception: |
| 199 |
pass |
| 200 |
if vf_num is None: |
| 201 |
LOG.warning("TaaS: No net device was found for pci: %(pci_addr)s " |
| 202 |
"virtfns_path: %(virtfns_path)s", |
| 203 |
{'pci_addr': pci_addr, |
| 204 |
'virtfns_path': virtfns_path}) |
| 205 |
raise taas_exc.PciDeviceNotFoundById(id=pci_addr) |
| 206 |
return vf_num |
| 207 |
|
| 208 |
def get_net_name_by_vf_pci_address(self, vfaddress, pf_interface=False): |
| 209 |
"""Given the VF PCI address, returns the net device name. |
| 210 |
|
| 211 |
Every VF is associated to a PCI network device. This function |
| 212 |
returns the libvirt name given to this network device; e.g.: |
| 213 |
|
| 214 |
<device> |
| 215 |
<name>net_enp8s0f0_90_e2_ba_5e_a6_40</name> |
| 216 |
... |
| 217 |
|
| 218 |
In the libvirt parser information tree, the network device stores the |
| 219 |
network capabilities associated to this device. |
| 220 |
""" |
| 221 |
LOG.debug("TaaS: vfaddr: %(vfaddr)s ", |
| 222 |
{'vfaddr': vfaddress}) |
| 223 |
try: |
| 224 |
mac = self.get_mac_by_pci_address(vfaddress, |
| 225 |
pf_interface).split(':') |
| 226 |
ifname = self.get_ifname_by_pci_address(vfaddress, pf_interface) |
| 227 |
LOG.debug("TaaS: mac: %(mac)s, ifname: %(ifname)s", |
| 228 |
{'mac': mac, 'ifname': ifname}) |
| 229 |
return ("net_%(ifname)s_%(mac)s" % |
| 230 |
{'ifname': ifname, 'mac': '_'.join(mac)}) |
| 231 |
except Exception: |
| 232 |
LOG.warning("No net device was found for VF %(vfaddress)s", |
| 233 |
{'vfaddress': vfaddress}) |
| 234 |
return |
| 235 |
|
| 236 |
def get_sriov_port_params(self, sriov_port): |
| 237 |
"""Returns a dict of common SRIOV parameters for a given SRIOV port |
| 238 |
|
| 239 |
""" |
| 240 |
LOG.debug("TaaS: sriov_port %(id)s; ", |
| 241 |
{'id': sriov_port['id']}) |
| 242 |
|
| 243 |
port_mac = sriov_port['mac_address'] |
| 244 |
|
| 245 |
pci_slot = None |
| 246 |
src_vlans = None |
| 247 |
|
| 248 |
if sriov_port.get(portbindings.PROFILE): |
| 249 |
pci_slot = sriov_port[portbindings.PROFILE].get('pci_slot') |
| 250 |
|
| 251 |
if sriov_port.get(portbindings.VIF_DETAILS): |
| 252 |
src_vlans = sriov_port[portbindings.VIF_DETAILS].get('vlan') |
| 253 |
|
| 254 |
LOG.debug("TaaS: pci_slot %(pci_slot)s; " |
| 255 |
"src_vlans %(src_vlans)s; ", |
| 256 |
{'pci_slot': pci_slot, |
| 257 |
'src_vlans': src_vlans}) |
| 258 |
|
| 259 |
if not pci_slot: |
| 260 |
LOG.error("No PCI Slot for sriov_port %(id)s with MAC %(mac)s; ", |
| 261 |
{'id': sriov_port['id'], 'mac': port_mac}) |
| 262 |
return |
| 263 |
|
| 264 |
vf_index = self.get_vf_num_by_pci_address(pci_slot) |
| 265 |
|
| 266 |
pf_device = self.get_ifname_by_pci_address(pci_slot, True) |
| 267 |
|
| 268 |
return {'mac': port_mac, 'pci_slot': pci_slot, |
| 269 |
'vf_index': vf_index, 'pf_device': pf_device, |
| 270 |
'src_vlans': src_vlans} |