Commit Graph

81 Commits

Author SHA1 Message Date
Rodolfo Alonso Hernandez 016f5187a5 Retry ``set|get_link_attribute(s)`` if the interface is not present
After some interface operations (in particular the
``IpLinkCommand.set_ns`` operation), the network interface is
temporarily not present in the destination namespace. This patch
retries the interface "ip link set|show" command in that case.

Related-Bug: #1961740
Change-Id: I5a57cfc71ad59f1fe9ea65e19b1a32314d798729
2024-02-21 07:46:53 +00:00
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
Tamas Gergely Peter 3727103ce7 Fix get_link_devices() with index argument
If get_link_devices() is called with 'index' in kwargs, pass the
argument to ip.get_links() correctly

Closes-Bug: #1953139
Change-Id: I6ae2d8c2a27aef548dd186e495c8998bf4086a20
2023-01-26 12:32:38 -05: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
Zuul 2751d75383 Merge "Fix some pylint indentation warnings" 2022-12-20 20:45:51 +00:00
Brian Haley 86badcfe2d Fix some pylint indentation warnings
Running with a stricter .pylintrc generates a lot of
C0330 warnings (hanging/continued indentation). Fix
some remaining ones in miscellaneous directories.

Also cleanup any remaining code that I missed in this
series, or has changed since I started.

Trivialfix

Change-Id: I17b4779020a7bfb369c3e721ab6638cd4a6ab50c
2022-12-12 11:48:25 -05:00
Miro Tomaska 81980146cb Add and delete multiple ip addresses in one priv call
Created new add_ip_addresses privileged function
which takes an iterable of cidrs and adds them
in one privileged call. This is so we dont have to
take on additional priv overhead when calling
add_ip_address in a loop.
For parity, performed the same change on the
delete_ip_address function.

Closes-Bug: #1987281
Partial-Bug: #1981113
Change-Id: Ib1278af20c3b3b057712453cb249aba34b684a21
2022-12-03 22:24:45 -06:00
Rodolfo Alonso Hernandez 1b8d794a35 Format the protocol number to be passed to pyroute2
When a new IP route is created, before passing the route protocol,
find if it is a string and if this string is on the pyroute2 defined
protocols. In this case, pass the protocol number.

In the same way, when the IP route is returned, if the protocol is a
number, convert it to the corresponding protocol string.

Closes-Bug: #1988037
Change-Id: I4ca66d86705a55b2b63083c229629c16b6136283
2022-08-29 12:44:14 +02:00
Rodolfo Alonso Hernandez 800f863ccc Stop returning unneeded information from "pyroute2" method calls
"pyroute2" methods can include some objects that don't implement
any serialization method (e.g.: "nla_slot" [1]). In those methods
that require an output ("get_*", "list_*", etc.), the Neutron
IP library formats the output inside the privsep context only to
contain serializable objects.

However this library is also returning the blobs returned from
the "pyroute2" library, without parsing and formatting, from
methods that don't require an output ("set_*", "add_*", "delete_*",
etc.). This patch removes the "return" statement from those methods
because the output is not required and to avoid issues like those
reported in the related bug.

[1]8716b9b5c0/pyroute2/netlink/__init__.py (L1754)

Closes-Bug: #1986644
Change-Id: I491dbdabfda0ca010ca56355b71dfe150ed71a71
2022-08-10 19:27:52 +02:00
Rajesh Tailor 8ab5ee1d17 Fix remaining typos in comments and tests
Change-Id: I872422cffd1f9a2e59b5e18a86695e5cb6edc2cd
2022-07-06 21:20:27 +05:30
Rodolfo Alonso Hernandez 4dbcd76517 Fix "ip route" commands to work with pyroute2 APIv2
Fixed "ip route" commands to work with pyroute2 >= 0.6.10, that
introduces APIv2 for link, neighbour and route [1].

[1]bc0f5e2209

Closes-Bug: #1979031
Change-Id: Id2239b6827485a4d466b0916947428ceabef9139
2022-06-17 08:39:44 +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
LIU Yulong ac1597d009 [L3] Add some logs for router processing
In order to dig the real action of a ResourceUpdate, add logs for:
1. add/update router
2. delete router
3. delete namespace
4. agent extension router add/delete/update actions

Change-Id: I5c0ff485cd0c966afe535f8063deca6e410e012d
Related-bug: #1881995
2021-06-22 01:41:28 +00:00
Rodolfo Alonso Hernandez 074c131b57 Disable pep8 import check for pyroute2 library
Since version 0.6.2, pyroute2 library dynamically imports the needed
modules when loaded. A static analysis will fail when checking the
import references.

Change-Id: I5aaf9494a2d5c2533199e6b92d4df8fe785f83a3
Closes-Bug: #1930750
2021-06-03 20:06:44 +00:00
Rodolfo Alonso Hernandez c511964d70 Replace "get_routing_table" with "list_ip_routes"
"get_routing_table" uses "pyroute2.IPDB" that has been deprecated.
"list_ip_routes" has been improved to be able to read multipath
routes.

Closes-Bug: #1926476

Change-Id: I0299fa11a7afefbd2999f81cd4ed3beed572009c
2021-05-01 17:39:42 +00:00
Rodolfo Alonso Hernandez 2cf4314553 Implement multipath routing in route commands
Related-Bug: #1926476

Change-Id: I598da266905a5645b744d7ffcc47b417ff64a5e7
2021-04-30 08:34:48 +00:00
Rodolfo Alonso Hernandez 3cee5f7201 Implement namespace operations privsep context
This context has only "CAP_SYS_ADMIN" capability.

Change-Id: I5e17f1b567e09397de58b2b934d3cf2bc7589b6c
Story: #2007686
Task: #42240
2021-04-09 15:24:22 +00:00
Zuul 58c9912be0 Merge "Implement namespace creation method" 2021-04-05 21:46:25 +00:00
Rodolfo Alonso Hernandez 32193267f5 Replace "ip route" command in "dvr_local_router"
This is a leftover of the "ip route" command migration to Pyroute2.

A new paremeter, "proto", is added to the IP route add and list
commands. The default protocol used is "static".

Story: #2007686
Task: #41284
Related-Bug: #1492714

Change-Id: I319fd0611d3e8a3a09d6d4e077a17a622f74f51c
2021-03-15 16:05:28 +00:00
Zuul 2328cd7c84 Merge "Remove try block from privileged ip_lib module" 2021-03-06 19:19:11 +00:00
Rodolfo Alonso Hernandez eb56747851 Implement namespace creation method
Since [1], Pyroute forks the namespace creation to avoid calling
destructive routine "libc.unshare(CLONE_NEWNET)" from the main
process. This implementation uses sockets between both processes
to return any error feedback sent from the child process.

This patch implements the same fork without any communication. If
the child process raises an exception other than "OSError(EEXIST)",
the child process returns 1 that is read by the the main process,
that raises a "RuntimeError" exception.

Related-Bug: #1917487

[1]81db2c98a1

Change-Id: I0294586335a71d0757803843f675124bfb450967
2021-03-05 12:39:49 +00:00
Slawek Kaplonski 8d44265736 Remove try block from privileged ip_lib module
It was marked that this try..except.. block can be removed when we will
be using pyroute2 >= 0.5.13. Now we have 0.5.13 in the lower-constraints
already so it's time to remove it.

Change-Id: Ic15361e34a2a9c371954d2f0851d230b8f9feb1f
2021-02-24 16:54:42 +01:00
Rodolfo Alonso Hernandez 591715b86e Implement "ip neigh flush" with Pyroute2
Story: #2007686
Task: #41558

Change-Id: I00c676e234fd9f771d716def7e4388bf33004118
2021-02-19 14:17:34 +00:00
Rodolfo Alonso Hernandez 3df21ee472 Implement "FdbInterface" with Pyroute2
Change-Id: I4b03151ed10b1c5af36fd803e96e9d2f95e88247
Story: #2007686
Task: #41494
2020-12-24 17:00:06 +00:00
Zuul 8459b80a33 Merge "Bump pylint version to support python 3.8" 2020-08-21 02:44:55 +00:00
Zuul c1b1075e3d Merge "Support pyroute2 0.5.13" 2020-08-17 02:34:27 +00:00
Bernard Cafarelli cebdd77af8
Bump pylint version to support python 3.8
As spotted in Focal testing patch [0], pep8 test fails with many
C0321 false-positives, reported in pylint as current version does not
support python 3.8 [1]

Use a newer version of pylint and astroid, fixing or disabling some of
the new checks: no-else-*, unnecessary-comprehension, import-outside-toplevel

[0] https://review.opendev.org/#/c/738163/
[1] https://github.com/PyCQA/pylint/issues/2737

Change-Id: Ie646b7093aa8634fd950c136a0eba9adcf56591c
2020-08-06 16:00:30 +02:00
Rodolfo Alonso Hernandez e5ce193189 Support pyroute2 0.5.13
Since [1], pyroute2 0.5.13 is supported.

In this new version, "link_lookup" do not raise a NetlinkError
exception if the device does not exist; instead returns an empty
list.

This patch handles both implementations.

[1]https://review.opendev.org/#/c/743277/

Change-Id: I77ef374ecb776966ea13499755777e2d763d884b
Closes-Bug: #1890353
2020-08-05 17:36:01 +00:00
Rodolfo Alonso Hernandez 68e5e1b8fe Specify C shared library in Pyroute2 namespace context
Since [1], it's possible to specify the shared library to be used
when creating a Pyroute2 namespace context.

As commented in [2], "privsep" library makes use of eventlet to
implement multitasking. If the method executed returns the GIL,
nothing guarantees that the "eventlet" executor will return it
again to this task. This could lead to timeouts during the
execution of those methods.

From https://docs.python.org/3.6/library/ctypes.html#ctypes.PyDLL:
  "Instances of this class behave like CDLL instances, except that
   the Python GIL is not released during the function call, and
   after the function execution the Python error flag is checked."

[1]https://github.com/svinota/pyroute2/issues/702
[2]https://review.opendev.org/#/c/717017/

Change-Id: I6c9f9adba8b4433cc96704bb69dd4e0d4b154ebd
Related-Bug: #1870352
2020-07-29 12:28:46 +00:00
Zuul 5999716cfc Merge "Implement "BridgeDevice" with Pyroute2" 2020-07-10 00:36:48 +00:00
Rodolfo Alonso Hernandez 24b379ad4d Implement "BridgeDevice" with Pyroute2
Change-Id: I9e64a4d4b931a132d25434eaeb9dcec6ebf0e6f8
Story: #2007686
Task: #39975
2020-07-06 11:01:21 +00:00
Rodolfo Alonso Hernandez 2592fdb584 Move "get_cdll" to a common place in privileged.agent.linux
That method could be used outside the library where currently is
implemented. This patch relocates it in a common place for all
privileged.agent.linux libraries.

Change-Id: I5a6124eca3b57ee36479c106b62d101f538c12eb
Story: #2007686
Task: #40047
2020-06-19 14:04:23 +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
Rodolfo Alonso Hernandez 306280813f Replace ctype.CDLL by ctypes.PyDLL in linux.ip_lib
Some linux.ip_lib functions make use of "ctype.CDLL" methods
(create_netns, remove_netns). Those methods are called inside a
"privsep" context; that means the function reference and the
arguments are passed to a privileged context that will execute
the method.

"privsep" library makes use of eventlet to implement multitasking.
If the method executed returns the GIL, nothing guarantees that
the "eventlet" executor will return it again to this task. This
could lead to timeouts during the execution of those methods.

From https://docs.python.org/3.6/library/ctypes.html#ctypes.PyDLL:
  "Instances of this class behave like CDLL instances, except that
   the Python GIL is not released during the function call, and
   after the function execution the Python error flag is checked."

Change-Id: I36ef9bf59e9c93f50464457a5d9a968738844079
Closes-Bug: #1870352
2020-04-02 13:59:29 +00: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
Slawek Kaplonski 364fc1413b Remove locks from privileged ip_lib module
Because of issue with pyroute2.NetNS objects running in
threads we needed to lock privileged ip_lib functions
which are using this object to workaround the problem.
For details please check [1].

This problem was solved in pyroute 0.5.5. Now as we are using
0.5.7 we don't need those locks anymore.

[1] https://bugs.launchpad.net/neutron/+bug/1811515

Change-Id: I37da4025b93c8032164b0c3f12f400ed0d77c1ab
2019-12-13 15:44:26 +00:00
Zuul 7f0f1a0268 Merge "Avoid raising NetworkInterfaceNotFound exception in DHCP agent logs" 2019-12-11 23:17:12 +00:00
Rodolfo Alonso Hernandez af8a812240 Load the glibc library only once for Pyroute2
Load the glibc library only once, needed in the Pyroute2 methods to
create and delete a network namespace.

Change-Id: I95b7b7008f4788a98ef871c4b7aecea839ff2310
Closes-Bug: #1854462
2019-12-09 16:13:10 +00:00
Rodolfo Alonso Hernandez 8cc2765b5f Avoid raising NetworkInterfaceNotFound exception in DHCP agent logs
In "ip_lib.ensure_device_is_ready", before retrieving the interface
attributes, a check is done to know if the interface exists. In case
it does not exist, the exception "NetworkInterfaceNotFound" will not
be raised and written in the logs.

Change-Id: I4b9fd0885d850601717274a5058e042871211bbb
Closes-Bug: #1854723
2019-12-06 10:28:24 +00:00
Rodolfo Alonso Hernandez 9ad03dcf96 Catch FileNotFoundError when listing namespace PIDs
During the namespace PID listing, if a process is stopped, it will
disappear from "/proc/<pid>". When "os.stat(path)" is executed on
an unexistent path, returns "FileNotFoundError". This exception
should be catched and ignored.

Change-Id: Icde5e15bd97578f5ec8273f22ef8384502be1850
Related-Bug: #1841753
2019-11-05 15:21:30 +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
Rodolfo Alonso Hernandez 2a7030a6b7 Change ip_lib decorators order
Change the execution order of:
- @privileged.default.entrypoint
- @lockutils.synchronized("privileged-ip-lib")

"synchronized" decorator holds the execution of the function until
the lock is released. Using the current decorator ordering, this
active wait is done inside the privsep context. This can exhaust
the number of execution threads reserved for the privsep daemon.

Closes-Bug: #1833721

Change-Id: Ifcce954003e360f620f9131a36a08ab84cbe6193
2019-09-20 08:35:24 +00:00
Rodolfo Alonso Hernandez 085e9d8b3d Implement "list_ns_pids" inside Neutron
Instead of using Pyroute2.netns.ns_pids() [1], while [2] is not
solved, a method to list a namespace PIDs is implemented in this
patch.

This implementation is based on the Pyroute2 method.

[1]80f6e7fcdd/pyroute2/netns/__init__.py (L163)
[2]https://github.com/svinota/pyroute2/issues/633

Change-Id: Ic00c02035a0dabf1a1efd1995c84692649c6ad27
Related-Bug: #1841753
2019-09-11 07:07:01 +00:00
Rodolfo Alonso Hernandez be7bb4d0f5 Kill all processes running in a namespace before deletion
In "NamespaceFixture", before deleting the namespace, this patch
introduces a check to first kill all processes running on it.

Closes-Bug: #1838793

Change-Id: I27f3db33f2e7ab685523fd2d6922177d7c9cb71b
2019-08-21 09:03:54 +00:00
Adrian Chiris b51ba4b283 Add interface kind property
- Add a new property to IPDevice to allow us to identify
  the kind of the interface.

This change is required as an out of tree interface driver
which supports operations on a per-physnet basis
needs to be aware of the kind of interface an interface driver
created in order to correlate between an interface driver
and an interface created by it.

Change-Id: Icbdb011a639475f416ca1b98fdf3ce2f52482c7c
Partial-Bug: #1834176
2019-07-24 10:59:47 +03:00
Rodolfo Alonso Hernandez 429c77c574 Implement "ip route delete" command using Pyroute2
Change-Id: I960455d6a9bc1b633d485c42a26b3a254731558e
Related-Bug: #1492714
2019-07-01 10:37:19 +00:00