[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241203130655.45293-5-donald.hunter@gmail.com>
Date: Tue, 3 Dec 2024 13:06:52 +0000
From: Donald Hunter <donald.hunter@...il.com>
To: netdev@...r.kernel.org,
Jakub Kicinski <kuba@...nel.org>,
"David S. Miller" <davem@...emloft.net>,
Eric Dumazet <edumazet@...gle.com>,
Paolo Abeni <pabeni@...hat.com>,
Simon Horman <horms@...nel.org>,
Johannes Berg <johannes@...solutions.net>,
linux-wireless@...r.kernel.org
Cc: donald.hunter@...hat.com,
Donald Hunter <donald.hunter@...il.com>
Subject: [PATCH net-next v1 4/7] tools/net/ynl: accept IP string inputs
The ynl tool uses display-hint to know when to format IP addresses in
printed output, but not to parse IP addresses from --json input. Add
support for parsing ipv4 and ipv6 strings.
Signed-off-by: Donald Hunter <donald.hunter@...il.com>
---
tools/net/ynl/lib/ynl.py | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index f07a8404f71a..c861c1a7d933 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -536,9 +536,11 @@ class YnlFamily(SpecFamily):
try:
return int(value)
except (ValueError, TypeError) as e:
- if 'enum' not in attr_spec:
- raise e
- return self._encode_enum(attr_spec, value)
+ if 'enum' in attr_spec:
+ return self._encode_enum(attr_spec, value)
+ if attr_spec.display_hint:
+ return self._from_string(value, attr_spec.display_hint, attr_spec['type'])
+ raise e
def _add_attr(self, space, name, value, search_attrs):
try:
@@ -571,7 +573,10 @@ class YnlFamily(SpecFamily):
if isinstance(value, bytes):
attr_payload = value
elif isinstance(value, str):
- attr_payload = bytes.fromhex(value)
+ if attr.display_hint:
+ attr_payload = self._from_string(value, attr.display_hint, attr['type'])
+ else:
+ attr_payload = bytes.fromhex(value)
elif isinstance(value, dict) and attr.struct_name:
attr_payload = self._encode_struct(attr.struct_name, value)
else:
@@ -899,6 +904,17 @@ class YnlFamily(SpecFamily):
formatted = raw
return formatted
+ def _from_string(self, string, display_hint, type):
+ if display_hint in ['ipv4', 'ipv6']:
+ ip = ipaddress.ip_address(string)
+ if type == 'binary':
+ raw = ip.packed
+ else:
+ raw = int(ip)
+ else:
+ raw = string
+ return raw
+
def handle_ntf(self, decoded):
msg = dict()
if self.include_raw:
--
2.47.1
Powered by blists - more mailing lists