Commit Graph

16 Commits

Author SHA1 Message Date
Brian Haley 16875b5f92 Catch non-existent entry failures better in ip_lib
The privileged/agent/linux/ip_lib.py code was not always
catching "entry does not exist" type errors when deleting
entries, and most of the callers were not catching it either,
which could lead to random failures.

Add code in the IP route, rule and bridge fdb code to catch
these errors and not raise on them, other exceptions will
still be raised.

Also fixed delete_neigh_entry() to not raise when the
given namespace does not exist to make it like all the
other calls in the file.

Added or modified functional tests for above cases.

Change-Id: I083649ab1b9a9057ee276a7f3ba069eb667db870
Closes-bug: #2030804
2023-08-24 11:22:31 -04:00
Rodolfo Alonso Hernandez 1d84aaf877 Bump the minimum pyroute2 version to 0.7.3
This patch also removes the pylint disablement message control
statements and imports NetNS and IPRoute from the new locations
in pyroute2.

Trivial-Fix

Change-Id: I298a7da767473c236ddf03c5702a2904d4870284
2023-01-18 04:43:09 +01:00
Rodolfo Alonso Hernandez 14d0ff0317 Add ``devlink.get_port`` method for devlink ports
Added ``devlink.get_port`` method that provides information about
a devlink port [1]. It is used to retrieve information about a port
representor connected to a local OVS instance (aka: hardware offloaded
ports). This method reports the PF PCI address, the PF index, the VF
index and the PF name; the PF name will be used to enforce the QoS
policies on the SR-IOV parent device (similar to what is done in the
ML2/SRIOV agent).

[1]https://www.kernel.org/doc/html/latest/networking/devlink/devlink-port.html

Related-Bug: #1998608
Change-Id: I34daf554cabcf17cb6371d510d5827457012516d
2022-12-12 20:08:06 +01:00
Dr. Jens Harbott d01700034a Fix tests involving pyroute2.netlink module
pyroute2 0.6.11 did some further refactoring of their module imports,
so using pyroute2.netlink.some_func() doesn't work anymore. Since we
do "from pyroute2 import netlink" anyway, use the imported module
everywhere.

Also, mock.patch()ing pyroute2.netlink.rtnl.tcmsg.common.tick_in_usec
doesn't work anymore, but also doesn't seem to be necessary for the
tests to succeed, so just drop it.

Closes-Bug: 1978564
Change-Id: Ie06b987f7efbff8057293893e42bee7b682b9ba1
2022-06-14 11:47:56 +00:00
elajkat 74a9e832d7 Add retry for privsep get_link_devices
pyroute 0.6.6 introduced a new exception NetlinkDumpInterrupted which
is raised when NLM_F_DUMP_INTR is set in the flags during dump of
devices.
The suggestion from pyroute developers is to retry in case of this
exception (see [1]).

[1]: https://github.com/svinota/pyroute2/issues/874#issuecomment-1063139555

Closes-Bug: #1962608

Change-Id: Ie195ad596fd148708fc30946bde964d52444afee
2022-03-17 10:27:31 +01:00
Rodolfo Alonso Hernandez cdff281f64 [SR-IOV] Fix QoS extension to set min/max values
"ip link" commands allow to define VF rates independently.
That means, first "rate" (max BW) can be set and in a second
command "min" (min BW) (check LP bug description).

However Pyroute2 command to set the VF rates requires to set both.
If one value is missing ("min_tx_rate", "max_tx_rate"), the library
sets this value to 0; in other words, the value is deleted.

The Pyroute2 structures are built depending on the parameter names.
In this case, {'vf': {'rate': ...}} will create a "vf_rate" [1]
nla structure, that requires "min_tx_rate" and "max_tx_rate".
This is part of the full structure passed to the "iproute" library
[2].

This is an example of code that only sets the "max_tx_rate" for
the 15th VF of "enp196s0f0":
  $ from neutron.plugins.ml2.drivers.mech_sriov.agent import pci_lib
    pci = pci_lib.PciDeviceIPWrapper("enp196s0f0")
    pci.set_vf_rate(15, {'max_tx_rate': 10})

The "msg" [3] (structure passed to "iproute" library) is this:
https://paste.opendev.org/show/b2FZBOebGOCHMrYhPr6X/. The
"min_tx_rate" is set to the default value 0.

This patch reads first the existing rates ("min_tx_rate",
"max_tx_rate") and populates the command parameters accordingly.

[1]a9564dff8e/pyroute2.core/pr2modules/netlink/rtnl/ifinfmsg/__init__.py (L712-L717)
[2]c8d9d92544/ip/ipaddress.c (L454-L470)
[3]a9564dff8e/pyroute2.core/pr2modules/iproute/linux.py (L1499)

Closes-Bug: #1962844
Change-Id: Ibbb6d938355440c42850812e368224b76b1fce19
2022-03-04 11:43:08 +00:00
Rodolfo Alonso Hernandez 899953de6b Add a privsep context only for link commands
This new context will have only two capabilities: CAP_NET_ADMIN
and CAP_SYS_ADMIN (for operations inside namespaces).

Change-Id: If9273db1a7ccdce3a81f68fce78408830e9c3d42
2021-07-16 15:10:03 +00:00
Rodolfo Alonso Hernandez c5d8fd6329 Use pyroute2 for SRIOV VF commands
Recent changes in some versions of iproute2 CLI output (v4.18),
have invalidated the regular expression used to parse the
"ip link" output.

To solve this problem and avoid future ones, pyroute2 is used to
retrieve the virtual functions information and set the VF attributes
(spoofcheck, min_tx_rate, max_tx_rate and link_state).

pyroute2 extended the "ip link" support to retrieve this information,
adding "ext_mask=1" in the get command. If no virtual functions are
present in this particular network interface, the added method,
"get_link_vfs",  will return an empty list.

The set commands can return a "InterfaceOperationNotSupported" in
case the operation is not supported. For min_tx_rate, if the driver
does not support to set a minimum bandwidth, an "InvalidArgument"
(from a pyroute2.NetlinkError(22)) exception will be raised.

Change-Id: I680da4f64bd114f1caecaaeedbf8a4b1915a0849
Closes-Bug: #1878042
2020-05-27 16:58:02 +00:00
Brian Haley 7594bb0627 Remove the dependency on the "mock" package
Now that we are python3 only, we should move to using the built
in version of mock that supports all of our testing needs and
remove the dependency on the "mock" package.

This patch moves all references to "import mock" to
"from unittest import mock". It also cleans up some new line
inconsistency.

Fixed an inconsistency in the OVSBridge.deferred() definition
as it needs to also have an *args argument.

Fixed an issue where an l3-agent test was mocking
functools.partial, causing a python3.8 failure.

Unit tests only, removing from tests/base.py affects
functional tests which need additional work.

Change-Id: I40e8a8410840c3774c72ae1a8054574445d66ece
2020-04-28 18:05:37 -04:00
Rodolfo Alonso Hernandez 43e150e690 Ensure netlink.nla_slot tuple key is a string
Ensure Pyroute2 netlink.nla_slot first element ("name"), is properly
converted to a string value. E.g.:
  In:  (b'IFA_ADDRESS', '192.168.30.20')
  Out: ('IFA_ADDRESS', '192.168.30.20')

Python2 compatibility checks are removed from ip_lib.make_serializable.

Change-Id: I87efe6cf8734bae6523106944e99fbd7db9ef4d5
Related-Bug: #1846360
2020-02-28 12:31:39 +00:00
Rodolfo Alonso Hernandez ecef65b9b5 Do not use exceptions in get_link_id() to control the code flow
Instead of using exceptions as control flow, check the Pyroute2 command
result and only raise the Neutron exception if needed. This will also
reduce the traceback log in case of raising NetworkInterfaceNotFound.

Although in Python the use of exception for this is common, this is
usually considered an antipattern.

Change-Id: I0e8bb3b0f6a46f2bac75e38c6ac6cdd094247f89
Closes-Bug: #1849449
2019-10-25 16:28:37 +00:00
Rodolfo Alonso Hernandez 592b446299 Convert Pyroute2 byte results to strings
Pyroute2 can return information structures with values as bytes
instead of strings. In order to unify the output, those byte
parameters will be converted to string. This is needed in case of
having dictionaries with keys as bytes instead of strings.

Change-Id: Ic665b2c5e28d06e21190f06d9f3f8e8f8c3c792d
Closes-Bug: #1846360
2019-10-15 17:25:03 +00:00
Miguel Angel Ajo 7458575cfb Convert missing exception on device.link.delete()
Once we started using oslo.privsep the call to device.link.delete()
should return RuntimeError when the device can't be handled by ip link
for example, when it's an ovs internal device.

Closes-Bug: #1776469

Change-Id: Ibf4b0bbb54aef38fc569036880668c745cb5c096
2018-06-12 16:05:48 +02:00
Sławek Kapłoński 56324c12aa Fix potential race condition in privileged ip_lib module
Functions like _run_iproute_{link,addr,neigh} are not atomic and
work in two steps.
First it tries to get device index and in second step calls specified
command for this device.
It might happen sometimes that device exists during first of those
steps but not exists during second step. Such case causes raising
pyroute2.NetlinkError exception which isn't properly handled in
Neutron code which uses ip_lib module.

This patch fixes it by catching pyroute2.NetlinkError exception
and raising NetworkInterfaceNotFound.
This is subclass of RuntimeError and all callers of ip_lib can handle
it properly is needed.

Change-Id: I568ef183466f5ff2f2c30ed74a7dc52db41ba577
Closes-Bug: #1763329
2018-04-13 09:39:29 +02:00
Sławek Kapłoński 53054ad463 Switch create/delete interfaces to pyroute2
Create and delete network interfaces in ip_lib.py module
now uses pyroute2 library.
Only exception is creation of veth currently as there is no
way to create veth pair and put one end of such veth
in another namespace in one call.

Related-Bug: #1492714

Change-Id: I0a658d91f173fb705b5987a1174bda6a6570468d
2018-03-13 11:02:56 +01:00
Sławek Kapłoński e0223edf88 Switch ip addr add/del/flush commands to use pyroute2
This patch replaces usage of "ip addr add/del/flush" commands
with pyroute2 library.
This also switches from rootwrap to privsep when doing those
actions.

This patch adds also UT for _run_iproute_neigh/addr functions
from privileged module.

Change-Id: I6f4df391ec1899f8a4b10b50735dc9a79fa8483f
Related-Bug: #1492714
2018-03-09 23:09:25 +01:00