Commit Graph

116 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
Zuul 30cc63a74a Merge "Add and delete multiple ip addresses in one priv call" 2022-12-19 17:34:00 +00: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
Brian Haley 81330921d8 Fix some pylint indentation warnings
Running with a stricter .pylintrc generates a lot of
C0330 warnings (hanging/continued indentation). Fix
the ones in neutron/agent and neutron/privileged/agent.

Trivialfix

Change-Id: Ib94257481e62b99b3d7648ae5137af5411b4867a
2022-12-01 16:22:24 +00: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 8127221479 Check a namespace existence by checking only its own directory
To check the existance of a namespace, instead of listing the
namespaces directory (by default "/var/run/netns"), this patch
directly checks the existence of the namespace directory, using
"os.path.exists".

This check is faster than listing the whole directory and avoids
timeout problems as reported in the related bug.

Closes-Bug: #1947974
Change-Id: I558d50d28378beb3710d98a2113ff9549c82ae17
2021-10-25 09:59:32 +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 f616f84e95 Implement conntrack command privsep context
This context has only "CAP_NET_ADMIN" capability.

Story: #2007686
Task: #42240

Change-Id: I8522c9c1e2243ea471d51fa50d04db476655e6d0
2021-04-09 15:36:03 +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 19eb12bd29 Revert "Implement "kill" method using os.kill()"
This reverts commit 4b21111eb1.

Reason for revert: This method is unstable and prone to timeouts

Change-Id: I6064d60e4d63b085046aace7683d766a79dd22da
2021-03-25 22:05:58 +00:00
Rodolfo Alonso Hernandez 4b21111eb1 Implement "kill" method using os.kill()
Implement the "kill" method (send a signal to a process) using the
Python native library "os".

In functional tests, "RootHelperProcess.kill" method should not fail if
the process does not exist.

Closes-Bug: #1843446
Closes-Bug: #1843418

Change-Id: Iee97a83779dd3e20eb3a223fb8557a94b8f15dc0
2021-03-22 08:58:20 +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 c89c1f53db Remove rootwrap execution (1)
Replace rootwrap execution with privsep context execution.
This series of patches will progressively replace any
rootwrap call.

This patch replaces some "IpNetnsCommand" command execution
methods.

Change-Id: Ic5fdf221a2a2cd0951539b0e040d2a941feee287
Story: #2007686
Task: #41558
2021-02-06 16:22:43 +00:00
Mitya_Eremeev c156c7d883 Proper log translation
The main idea of the commit is to fix code
according with the latest oslo.i18n requirements
https://docs.openstack.org/oslo.i18n/latest/

1. removed log translation if log is not seen by users
   in raised exception or api call response.
2. keep translated log if it's used in raised exception.
3. removed log message 'Error while reading %s'
   which was "dead" (unused) code in the function
   "_get_value_from_conf_file"
   of module "agent/linux/dhcp.py".

Partial-Bug: 1600788
Change-Id: Ifb5455336b06c2c87a930b816c90b4a766856b1e
2021-01-21 19:07:04 +03:00
Rodolfo Alonso Hernandez 3df21ee472 Implement "FdbInterface" with Pyroute2
Change-Id: I4b03151ed10b1c5af36fd803e96e9d2f95e88247
Story: #2007686
Task: #41494
2020-12-24 17:00:06 +00:00
Rodolfo Alonso Hernandez 0a0f647ea0 Delete HA metadata proxy PID and config with elevated privileges
Both files cannot be deleted with the default permissions because
those files are created by the "root" user.

Change-Id: I73dd37b3104fac8d3172f520f71cffd85d040c4b
Closes-Bug: #1907695
2020-12-13 21:50:31 +00:00
Rodolfo Alonso Hernandez 55f5c78053 Remove "ovs-vsctl" support from rootwrap
This command is executed from scripts and in sanity checks, but not
from any Neutron service.

Change-Id: If82e89bf7b233559513ab44eadebb445648f0684
Story: #2007686
Task: #41282
2020-11-23 16:23:36 +00:00
Steve Baker 2864957ca5 Fix dhcp_release when client_id is specified
Appending a string to a list results in each character being appended
as list items, this results in an invalid release command.

This change appends the client_id string to the list instead.

Change-Id: I71a114308e3b68f6daf6e1a202a47b6a453bb81a
Closes-Bug: #1896850
2020-09-24 12:01:12 +12: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
Zuul 8441737127 Merge "Migrate "ethtool" to oslo.privsep" 2020-08-14 22:58:44 +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
Zuul bffd23658e Merge "Migrate "dhcp_release" to oslo.privsep" 2020-07-08 16:01:21 +00:00
Rodolfo Alonso Hernandez b52e2e6f16 Migrate "ethtool" to oslo.privsep
Story: #2007686
Task: #40290

Change-Id: I78cc06c635e806b50ca2cc631732d55e430dd2f1
2020-07-07 17:45:54 +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 0c1818fbb0 Migrate "netstat" to oslo.privsep
Change-Id: If9e4c1513553c4bd10fd3b91c28c4d3f806ed816
Story: #2007686
Task: #40047
2020-06-19 14:59:11 +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