| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
"""Implements vlans, bridges using linux utilities.""" |
| 21 |
|
| 22 |
import glob |
| 23 |
import os |
| 24 |
import re |
| 25 |
|
| 26 |
from os_vif.internal.ip.api import ip as ip_lib |
| 27 |
from oslo_concurrency import processutils |
| 28 |
from oslo_log import log as logging |
| 29 |
from oslo_utils import excutils |
| 30 |
|
| 31 |
from vif_plug_ovs import exception |
| 32 |
from vif_plug_ovs import privsep |
| 33 |
|
| 34 |
LOG = logging.getLogger(__name__) |
| 35 |
|
| 36 |
VIRTFN_RE = re.compile(r"virtfn(\d+)") |
| 37 |
|
| 38 |
|
| 39 |
INT_RE = re.compile(r"^(\d+)$") |
| 40 |
|
| 41 |
VF_RE = re.compile(r"vf(\d+)", re.IGNORECASE) |
| 42 |
|
| 43 |
PF_RE = re.compile(r"pf(\d+)", re.IGNORECASE) |
| 44 |
|
| 45 |
PF_FUNC_RE = re.compile(r"\.(\d+)", 0) |
| 46 |
|
| 47 |
UPLINK_PORT_RE = re.compile(r"p(\d+)", re.IGNORECASE) |
| 48 |
|
| 49 |
_SRIOV_TOTALVFS = "sriov_totalvfs" |
| 50 |
NIC_NAME_LEN = 14 |
| 51 |
|
| 52 |
|
| 53 |
def _update_device_mtu(dev, mtu): |
| 54 |
if not mtu: |
| 55 |
return |
| 56 |
set_device_mtu(dev, mtu) |
| 57 |
|
| 58 |
|
| 59 |
@privsep.vif_plug.entrypoint |
| 60 |
def delete_net_dev(dev): |
| 61 |
"""Delete a network device only if it exists.""" |
| 62 |
if ip_lib.exists(dev): |
| 63 |
try: |
| 64 |
ip_lib.delete(dev, check_exit_code=[0, 2, 254]) |
| 65 |
LOG.debug("Net device removed: '%s'", dev) |
| 66 |
except processutils.ProcessExecutionError: |
| 67 |
with excutils.save_and_reraise_exception(): |
| 68 |
LOG.error("Failed removing net device: '%s'", dev) |
| 69 |
|
| 70 |
|
| 71 |
@privsep.vif_plug.entrypoint |
| 72 |
def create_veth_pair(dev1_name, dev2_name, mtu): |
| 73 |
"""Create a pair of veth devices with the specified names, |
| 74 |
deleting any previous devices with those names. |
| 75 |
""" |
| 76 |
for dev in [dev1_name, dev2_name]: |
| 77 |
delete_net_dev(dev) |
| 78 |
|
| 79 |
ip_lib.add(dev1_name, 'veth', peer=dev2_name) |
| 80 |
for dev in [dev1_name, dev2_name]: |
| 81 |
ip_lib.set(dev, state='up') |
| 82 |
ip_lib.set(dev, promisc='on') |
| 83 |
_update_device_mtu(dev, mtu) |
| 84 |
|
| 85 |
|
| 86 |
@privsep.vif_plug.entrypoint |
| 87 |
def update_veth_pair(dev1_name, dev2_name, mtu): |
| 88 |
"""Update a pair of veth devices with new configuration.""" |
| 89 |
for dev in [dev1_name, dev2_name]: |
| 90 |
_update_device_mtu(dev, mtu) |
| 91 |
|
| 92 |
|
| 93 |
@privsep.vif_plug.entrypoint |
| 94 |
def create_tap(dev, mtu, mac, multiqueue=False): |
| 95 |
"""Create a tap device with the specified configuration. |
| 96 |
|
| 97 |
Creates a tap device using pyroute2's netlink interface, with optional |
| 98 |
multiqueue support for better performance with multiple VCPUs. |
| 99 |
|
| 100 |
:param dev: Device name (string) |
| 101 |
:param mtu: MTU value (integer) |
| 102 |
:param mac: MAC address (string) |
| 103 |
:param multiqueue: Enable multiqueue support (boolean, default False) |
| 104 |
Requires Linux kernel 3.8+ |
| 105 |
""" |
| 106 |
|
| 107 |
|
| 108 |
ip_lib.add(dev, 'tuntap', mode='tap', multiqueue=multiqueue, |
| 109 |
check_exit_code=[0, 17]) |
| 110 |
|
| 111 |
ip_lib.set(dev, state='up', address=mac, check_exit_code=[0, 2, 254]) |
| 112 |
|
| 113 |
_update_device_mtu(dev, mtu) |
| 114 |
|
| 115 |
|
| 116 |
def _disable_ipv6(bridge): |
| 117 |
"""Disable ipv6 if available for bridge. Must be called from |
| 118 |
privsep context. |
| 119 |
""" |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
disv6 = ('/proc/sys/net/ipv6/conf/%s/disable_ipv6' % |
| 125 |
bridge) |
| 126 |
if os.path.exists(disv6): |
| 127 |
with open(disv6, 'w') as f: |
| 128 |
f.write('1') |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
def _arp_filtering(bridge): |
| 133 |
"""Prevent the bridge from replying to ARP messages with machine local IPs |
| 134 |
|
| 135 |
1. Reply only if the target IP address is local address configured on the |
| 136 |
incoming interface. |
| 137 |
2. Always use the best local address. |
| 138 |
""" |
| 139 |
arp_params = [('/proc/sys/net/ipv4/conf/%s/arp_ignore' % bridge, '1'), |
| 140 |
('/proc/sys/net/ipv4/conf/%s/arp_announce' % bridge, '2')] |
| 141 |
for parameter, value in arp_params: |
| 142 |
if os.path.exists(parameter): |
| 143 |
with open(parameter, 'w') as f: |
| 144 |
f.write(value) |
| 145 |
|
| 146 |
|
| 147 |
@privsep.vif_plug.entrypoint |
| 148 |
def ensure_bridge(bridge): |
| 149 |
if not ip_lib.exists(bridge): |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
ip_lib.add(bridge, 'bridge', ageing=0) |
| 154 |
_disable_ipv6(bridge) |
| 155 |
_arp_filtering(bridge) |
| 156 |
|
| 157 |
set_interface_state(bridge, 'up') |
| 158 |
|
| 159 |
|
| 160 |
@privsep.vif_plug.entrypoint |
| 161 |
def delete_bridge(bridge, dev): |
| 162 |
if ip_lib.exists(bridge): |
| 163 |
|
| 164 |
|
| 165 |
ip_lib.delete(bridge, check_exit_code=[0, 2, 254]) |
| 166 |
|
| 167 |
|
| 168 |
if dev and ip_lib.exists(dev): |
| 169 |
set_interface_state(dev, "down") |
| 170 |
|
| 171 |
|
| 172 |
@privsep.vif_plug.entrypoint |
| 173 |
def add_bridge_port(bridge, dev): |
| 174 |
ip_lib.set(dev, master=bridge) |
| 175 |
|
| 176 |
|
| 177 |
@privsep.vif_plug.entrypoint |
| 178 |
def set_device_mtu(dev, mtu): |
| 179 |
"""Set the device MTU.""" |
| 180 |
if ip_lib.exists(dev): |
| 181 |
ip_lib.set(dev, mtu=mtu, check_exit_code=[0, 2, 254]) |
| 182 |
|
| 183 |
|
| 184 |
@privsep.vif_plug.entrypoint |
| 185 |
def set_interface_state(interface_name, port_state): |
| 186 |
ip_lib.set(interface_name, state=port_state, check_exit_code=[0, 2, 254]) |
| 187 |
|
| 188 |
|
| 189 |
def _parse_vf_number(phys_port_name): |
| 190 |
"""Parses phys_port_name and returns VF number or None. |
| 191 |
|
| 192 |
To determine the VF number of a representor, parse phys_port_name |
| 193 |
in the following sequence and return the first valid match. If none |
| 194 |
match, then the representor is not for a VF. |
| 195 |
""" |
| 196 |
match = INT_RE.search(phys_port_name) |
| 197 |
if match: |
| 198 |
return match.group(1) |
| 199 |
match = VF_RE.search(phys_port_name) |
| 200 |
if match: |
| 201 |
return match.group(1) |
| 202 |
return None |
| 203 |
|
| 204 |
|
| 205 |
def _parse_pf_number(phys_port_name): |
| 206 |
"""Parses phys_port_name and returns PF number or None. |
| 207 |
|
| 208 |
To determine the PF number of a representor, parse phys_port_name in |
| 209 |
the following sequence and return the first valid match. If none |
| 210 |
match, then the representor is not for a PF. |
| 211 |
""" |
| 212 |
match = PF_RE.search(phys_port_name) |
| 213 |
if match: |
| 214 |
return match.group(1) |
| 215 |
return None |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
def get_function_by_ifname(ifname): |
| 220 |
"""Given the device name, returns the PCI address of a device |
| 221 |
and returns True if the address is in a physical function. |
| 222 |
""" |
| 223 |
dev_path = "/sys/class/net/%s/device" % ifname |
| 224 |
sriov_totalvfs = 0 |
| 225 |
if os.path.isdir(dev_path): |
| 226 |
try: |
| 227 |
|
| 228 |
dev_path_file = os.path.join(dev_path, _SRIOV_TOTALVFS) |
| 229 |
with open(dev_path_file, 'r') as fd: |
| 230 |
sriov_totalvfs = int(fd.readline().rstrip()) |
| 231 |
return (os.readlink(dev_path).strip("./"), |
| 232 |
sriov_totalvfs > 0) |
| 233 |
except (IOError, ValueError): |
| 234 |
return os.readlink(dev_path).strip("./"), False |
| 235 |
return None, False |
| 236 |
|
| 237 |
|
| 238 |
def _get_pf_func(pf_ifname): |
| 239 |
"""Gets PF function number using pf_ifname and returns function |
| 240 |
number or None. |
| 241 |
""" |
| 242 |
|
| 243 |
address_str, pf = get_function_by_ifname(pf_ifname) |
| 244 |
if not address_str: |
| 245 |
return None |
| 246 |
match = PF_FUNC_RE.search(address_str) |
| 247 |
if match: |
| 248 |
return match.group(1) |
| 249 |
return None |
| 250 |
|
| 251 |
|
| 252 |
def get_representor_port(pf_ifname, vf_num): |
| 253 |
"""Get the representor netdevice which is corresponding to the VF. |
| 254 |
|
| 255 |
This method gets PF interface name and number of VF. It iterates over all |
| 256 |
the interfaces under the PF location and looks for interface that has the |
| 257 |
VF number in the phys_port_name. That interface is the representor for |
| 258 |
the requested VF. |
| 259 |
""" |
| 260 |
|
| 261 |
pf_sw_id = None |
| 262 |
try: |
| 263 |
pf_sw_id = _get_phys_switch_id(pf_ifname) |
| 264 |
except (OSError, IOError): |
| 265 |
raise exception.RepresentorNotFound(ifname=pf_ifname, vf_num=vf_num) |
| 266 |
|
| 267 |
pf_subsystem_file = "/sys/class/net/%s/subsystem" % pf_ifname |
| 268 |
try: |
| 269 |
devices = os.listdir(pf_subsystem_file) |
| 270 |
except (OSError, IOError): |
| 271 |
raise exception.RepresentorNotFound(ifname=pf_ifname, vf_num=vf_num) |
| 272 |
|
| 273 |
ifname_pf_func = _get_pf_func(pf_ifname) |
| 274 |
if ifname_pf_func is None: |
| 275 |
raise exception.RepresentorNotFound(ifname=pf_ifname, vf_num=vf_num) |
| 276 |
|
| 277 |
for device in devices: |
| 278 |
try: |
| 279 |
device_sw_id = _get_phys_switch_id(device) |
| 280 |
if not device_sw_id or device_sw_id != pf_sw_id: |
| 281 |
continue |
| 282 |
except (OSError, IOError): |
| 283 |
continue |
| 284 |
|
| 285 |
try: |
| 286 |
phys_port_name = _get_phys_port_name(device) |
| 287 |
if phys_port_name is None: |
| 288 |
continue |
| 289 |
except (OSError, IOError): |
| 290 |
continue |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
rep_parent_pf_func = _parse_pf_number(phys_port_name) |
| 296 |
if rep_parent_pf_func is not None: |
| 297 |
if int(rep_parent_pf_func) != int(ifname_pf_func): |
| 298 |
continue |
| 299 |
|
| 300 |
representor_num = _parse_vf_number(phys_port_name) |
| 301 |
|
| 302 |
if representor_num is None: |
| 303 |
continue |
| 304 |
|
| 305 |
|
| 306 |
try: |
| 307 |
if int(representor_num) == int(vf_num): |
| 308 |
return device |
| 309 |
except (ValueError): |
| 310 |
continue |
| 311 |
|
| 312 |
raise exception.RepresentorNotFound(ifname=pf_ifname, vf_num=vf_num) |
| 313 |
|
| 314 |
|
| 315 |
def _get_sysfs_netdev_path(pci_addr, pf_interface): |
| 316 |
"""Get the sysfs path based on the PCI address of the device. |
| 317 |
|
| 318 |
Assumes a networking device - will not check for the existence of the path. |
| 319 |
""" |
| 320 |
if pf_interface: |
| 321 |
return "/sys/bus/pci/devices/%s/physfn/net" % (pci_addr) |
| 322 |
return "/sys/bus/pci/devices/%s/net" % (pci_addr) |
| 323 |
|
| 324 |
|
| 325 |
def _is_switchdev(netdev): |
| 326 |
"""Returns True if a netdev has a readable phys_switch_id""" |
| 327 |
try: |
| 328 |
phys_switch_id = _get_phys_switch_id(netdev) |
| 329 |
if phys_switch_id != "" and phys_switch_id is not None: |
| 330 |
return True |
| 331 |
except (OSError, IOError): |
| 332 |
return False |
| 333 |
return False |
| 334 |
|
| 335 |
|
| 336 |
def get_ifname_by_pci_address(pci_addr, pf_interface=False, switchdev=False): |
| 337 |
"""Get the interface name based on a VF's pci address |
| 338 |
|
| 339 |
:param pci_addr: the PCI address of the VF |
| 340 |
:param pf_interface: if True, look for the netdev of the parent PF |
| 341 |
:param switchdev: if True, ensure that phys_switch_id is valid |
| 342 |
|
| 343 |
:returns: netdev interface name |
| 344 |
|
| 345 |
The returned interface name is either the parent PF or that of the VF |
| 346 |
itself based on the argument of pf_interface. |
| 347 |
""" |
| 348 |
dev_path = _get_sysfs_netdev_path(pci_addr, pf_interface) |
| 349 |
try: |
| 350 |
devices = os.listdir(dev_path) |
| 351 |
|
| 352 |
|
| 353 |
if not switchdev: |
| 354 |
return devices[0] |
| 355 |
elif pf_interface: |
| 356 |
fallback_netdev = None |
| 357 |
for netdev in devices: |
| 358 |
|
| 359 |
if _is_switchdev(netdev): |
| 360 |
fallback_netdev = netdev if fallback_netdev is None \ |
| 361 |
else fallback_netdev |
| 362 |
phys_port_name = _get_phys_port_name(netdev) |
| 363 |
if phys_port_name is not None and \ |
| 364 |
UPLINK_PORT_RE.search(phys_port_name): |
| 365 |
return netdev |
| 366 |
|
| 367 |
|
| 368 |
if fallback_netdev is not None: |
| 369 |
return fallback_netdev |
| 370 |
|
| 371 |
except Exception: |
| 372 |
raise exception.PciDeviceNotFoundById(id=pci_addr) |
| 373 |
raise exception.PciDeviceNotFoundById(id=pci_addr) |
| 374 |
|
| 375 |
|
| 376 |
def get_vf_num_by_pci_address(pci_addr): |
| 377 |
"""Get the VF number based on a VF's pci address |
| 378 |
|
| 379 |
A VF is associated with an VF number, which ip link command uses to |
| 380 |
configure it. This number can be obtained from the PCI device filesystem. |
| 381 |
""" |
| 382 |
virtfns_path = "/sys/bus/pci/devices/%s/physfn/virtfn*" % (pci_addr) |
| 383 |
vf_num = None |
| 384 |
try: |
| 385 |
for vf_path in glob.iglob(virtfns_path): |
| 386 |
if re.search(pci_addr, os.readlink(vf_path)): |
| 387 |
t = VIRTFN_RE.search(vf_path) |
| 388 |
vf_num = t.group(1) |
| 389 |
break |
| 390 |
except Exception: |
| 391 |
pass |
| 392 |
if vf_num is None: |
| 393 |
raise exception.PciDeviceNotFoundById(id=pci_addr) |
| 394 |
return vf_num |
| 395 |
|
| 396 |
|
| 397 |
def get_dpdk_representor_port_name(port_id): |
| 398 |
devname = "vfr" + port_id |
| 399 |
return devname[:NIC_NAME_LEN] |
| 400 |
|
| 401 |
|
| 402 |
def get_pf_pci_from_vf(vf_pci): |
| 403 |
"""Get physical function PCI address of a VF |
| 404 |
|
| 405 |
:param vf_pci: the PCI address of the VF |
| 406 |
:return: the PCI address of the PF |
| 407 |
""" |
| 408 |
physfn_path = os.readlink("/sys/bus/pci/devices/%s/physfn" % vf_pci) |
| 409 |
return os.path.basename(physfn_path) |
| 410 |
|
| 411 |
|
| 412 |
def _get_phys_port_name(ifname): |
| 413 |
"""Get the interface name and return its phys_port_name |
| 414 |
|
| 415 |
:param ifname: The interface name |
| 416 |
:return: The phys_port_name of the given ifname |
| 417 |
""" |
| 418 |
phys_port_name_path = "/sys/class/net/%s/phys_port_name" % ifname |
| 419 |
|
| 420 |
if not os.path.isfile(phys_port_name_path): |
| 421 |
return None |
| 422 |
|
| 423 |
with open(phys_port_name_path, 'r') as fd: |
| 424 |
return fd.readline().strip() |
| 425 |
|
| 426 |
|
| 427 |
def _get_phys_switch_id(ifname): |
| 428 |
"""Get the interface name and return its phys_switch_id |
| 429 |
|
| 430 |
:param ifname: The interface name |
| 431 |
:return: The phys_switch_id of the given ifname |
| 432 |
""" |
| 433 |
phys_port_name_path = "/sys/class/net/%s/phys_switch_id" % ifname |
| 434 |
|
| 435 |
if not os.path.isfile(phys_port_name_path): |
| 436 |
return None |
| 437 |
|
| 438 |
with open(phys_port_name_path, 'r') as fd: |
| 439 |
return fd.readline().strip() |