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
This commit is contained in:
Rodolfo Alonso Hernandez 2022-08-29 12:44:14 +02:00
parent 7dfe41ab8f
commit 1b8d794a35
2 changed files with 5 additions and 1 deletions

View File

@ -1544,8 +1544,10 @@ def list_ip_routes(namespace, ip_version, scope=None, via=None, table=None,
return get_attr(device, 'IFLA_IFNAME')
def get_proto(proto_number):
if proto_number in rtnl.rt_proto:
if isinstance(proto_number, int) and proto_number in rtnl.rt_proto:
return rtnl.rt_proto[proto_number]
elif isinstance(proto_number, str) and proto_number.isnumeric():
return rtnl.rt_proto[int(proto_number)]
elif str(proto_number) in constants.IP_PROTOCOL_NUM_TO_NAME_MAP:
return constants.IP_PROTOCOL_NUM_TO_NAME_MAP[str(proto_number)]

View File

@ -717,6 +717,8 @@ def _make_pyroute2_route_args(namespace, ip_version, cidr, device, via, table,
if metric:
args['priority'] = int(metric)
if protocol:
if isinstance(protocol, str) and protocol in rtnl.rt_proto:
protocol = rtnl.rt_proto[protocol]
args['proto'] = protocol
if isinstance(via, (list, tuple)):
args['multipath'] = []