Skip to content

Commit 35bf34b

Browse files
committed
Merge branch 'netlink-add-display-hint-to-ynl'
Donald Hunter says: ==================== netlink: add display-hint to ynl Add a display-hint property to the netlink schema, to be used by generic netlink clients as hints about how to display attribute values. A display-hint on an attribute definition is intended for letting a client such as ynl know that, for example, a u32 should be rendered as an ipv4 address. The display-hint enumeration includes a small number of networking domain-specific value types. ==================== Link: https://lore.kernel.org/r/20230623201928.14275-1-donald.hunter@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents 2ffecf1 + 334f39c commit 35bf34b

6 files changed

Lines changed: 168 additions & 6 deletions

File tree

Documentation/netlink/genetlink-c.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ properties:
195195
description: Max length for a string or a binary attribute.
196196
$ref: '#/$defs/len-or-define'
197197
sub-type: *attr-type
198+
display-hint: &display-hint
199+
description: |
200+
Optional format indicator that is intended only for choosing
201+
the right formatting mechanism when displaying values of this
202+
type.
203+
enum: [ hex, mac, fddi, ipv4, ipv6, uuid ]
198204
# Start genetlink-c
199205
name-prefix:
200206
type: string

Documentation/netlink/genetlink-legacy.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ properties:
119119
name:
120120
type: string
121121
type:
122-
enum: [ u8, u16, u32, u64, s8, s16, s32, s64, string ]
122+
description: The netlink attribute type
123+
enum: [ u8, u16, u32, u64, s8, s16, s32, s64, string, binary ]
123124
len:
124125
$ref: '#/$defs/len-or-define'
125126
byte-order:
@@ -130,6 +131,12 @@ properties:
130131
enum:
131132
description: Name of the enum type used for the attribute.
132133
type: string
134+
display-hint: &display-hint
135+
description: |
136+
Optional format indicator that is intended only for choosing
137+
the right formatting mechanism when displaying values of this
138+
type.
139+
enum: [ hex, mac, fddi, ipv4, ipv6, uuid ]
133140
# End genetlink-legacy
134141

135142
attribute-sets:
@@ -179,6 +186,7 @@ properties:
179186
name:
180187
type: string
181188
type: &attr-type
189+
description: The netlink attribute type
182190
enum: [ unused, pad, flag, binary, u8, u16, u32, u64, s32, s64,
183191
string, nest, array-nest, nest-type-value ]
184192
doc:
@@ -226,6 +234,7 @@ properties:
226234
description: Max length for a string or a binary attribute.
227235
$ref: '#/$defs/len-or-define'
228236
sub-type: *attr-type
237+
display-hint: *display-hint
229238
# Start genetlink-c
230239
name-prefix:
231240
type: string

Documentation/netlink/genetlink.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ properties:
168168
description: Max length for a string or a binary attribute.
169169
$ref: '#/$defs/len-or-define'
170170
sub-type: *attr-type
171+
display-hint: &display-hint
172+
description: |
173+
Optional format indicator that is intended only for choosing
174+
the right formatting mechanism when displaying values of this
175+
type.
176+
enum: [ hex, mac, fddi, ipv4, ipv6, uuid ]
171177

172178
# Make sure name-prefix does not appear in subsets (subsets inherit naming)
173179
dependencies:

Documentation/netlink/specs/ovs_flow.yaml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ definitions:
3333
name: n-bytes
3434
type: u64
3535
doc: Number of matched bytes.
36+
-
37+
name: ovs-key-ethernet
38+
type: struct
39+
members:
40+
-
41+
name: eth-src
42+
type: binary
43+
len: 6
44+
display-hint: mac
45+
-
46+
name: eth-dst
47+
type: binary
48+
len: 6
49+
display-hint: mac
3650
-
3751
name: ovs-key-mpls
3852
type: struct
@@ -49,10 +63,12 @@ definitions:
4963
name: ipv4-src
5064
type: u32
5165
byte-order: big-endian
66+
display-hint: ipv4
5267
-
5368
name: ipv4-dst
5469
type: u32
5570
byte-order: big-endian
71+
display-hint: ipv4
5672
-
5773
name: ipv4-proto
5874
type: u8
@@ -66,6 +82,45 @@ definitions:
6682
name: ipv4-frag
6783
type: u8
6884
enum: ovs-frag-type
85+
-
86+
name: ovs-key-ipv6
87+
type: struct
88+
members:
89+
-
90+
name: ipv6-src
91+
type: binary
92+
len: 16
93+
byte-order: big-endian
94+
display-hint: ipv6
95+
-
96+
name: ipv6-dst
97+
type: binary
98+
len: 16
99+
byte-order: big-endian
100+
display-hint: ipv6
101+
-
102+
name: ipv6-label
103+
type: u32
104+
byte-order: big-endian
105+
-
106+
name: ipv6-proto
107+
type: u8
108+
-
109+
name: ipv6-tclass
110+
type: u8
111+
-
112+
name: ipv6-hlimit
113+
type: u8
114+
-
115+
name: ipv6-frag
116+
type: u8
117+
-
118+
name: ovs-key-ipv6-exthdrs
119+
type: struct
120+
members:
121+
-
122+
name: hdrs
123+
type: u16
69124
-
70125
name: ovs-frag-type
71126
name-prefix: ovs-frag-type-
@@ -129,6 +184,51 @@ definitions:
129184
-
130185
name: icmp-code
131186
type: u8
187+
-
188+
name: ovs-key-arp
189+
type: struct
190+
members:
191+
-
192+
name: arp-sip
193+
type: u32
194+
byte-order: big-endian
195+
-
196+
name: arp-tip
197+
type: u32
198+
byte-order: big-endian
199+
-
200+
name: arp-op
201+
type: u16
202+
byte-order: big-endian
203+
-
204+
name: arp-sha
205+
type: binary
206+
len: 6
207+
display-hint: mac
208+
-
209+
name: arp-tha
210+
type: binary
211+
len: 6
212+
display-hint: mac
213+
-
214+
name: ovs-key-nd
215+
type: struct
216+
members:
217+
-
218+
name: nd_target
219+
type: binary
220+
len: 16
221+
byte-order: big-endian
222+
-
223+
name: nd-sll
224+
type: binary
225+
len: 6
226+
display-hint: mac
227+
-
228+
name: nd-tll
229+
type: binary
230+
len: 6
231+
display-hint: mac
132232
-
133233
name: ovs-key-ct-tuple-ipv4
134234
type: struct
@@ -345,6 +445,7 @@ attribute-sets:
345445
value of the OVS_FLOW_ATTR_KEY attribute. Optional for all
346446
requests. Present in notifications if the flow was created with this
347447
attribute.
448+
display-hint: uuid
348449
-
349450
name: ufid-flags
350451
type: u32
@@ -374,6 +475,7 @@ attribute-sets:
374475
-
375476
name: ethernet
376477
type: binary
478+
struct: ovs-key-ethernet
377479
doc: struct ovs_key_ethernet
378480
-
379481
name: vlan
@@ -390,6 +492,7 @@ attribute-sets:
390492
-
391493
name: ipv6
392494
type: binary
495+
struct: ovs-key-ipv6
393496
doc: struct ovs_key_ipv6
394497
-
395498
name: tcp
@@ -410,10 +513,12 @@ attribute-sets:
410513
-
411514
name: arp
412515
type: binary
516+
struct: ovs-key-arp
413517
doc: struct ovs_key_arp
414518
-
415519
name: nd
416520
type: binary
521+
struct: ovs-key-nd
417522
doc: struct ovs_key_nd
418523
-
419524
name: skb-mark
@@ -457,6 +562,7 @@ attribute-sets:
457562
-
458563
name: ct-labels
459564
type: binary
565+
display-hint: hex
460566
doc: 16-octet connection tracking label
461567
-
462568
name: ct-orig-tuple-ipv4
@@ -486,6 +592,7 @@ attribute-sets:
486592
-
487593
name: ipv6-exthdrs
488594
type: binary
595+
struct: ovs-key-ipv6-exthdrs
489596
doc: struct ovs_key_ipv6_exthdr
490597
-
491598
name: action-attrs

tools/net/ynl/lib/nlspec.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ class SpecAttr(SpecElement):
154154
is_multi bool, attr may repeat multiple times
155155
struct_name string, name of struct definition
156156
sub_type string, name of sub type
157+
len integer, optional byte length of binary types
158+
display_hint string, hint to help choose format specifier
159+
when displaying the value
157160
"""
158161
def __init__(self, family, attr_set, yaml, value):
159162
super().__init__(family, yaml)
@@ -164,6 +167,8 @@ def __init__(self, family, attr_set, yaml, value):
164167
self.struct_name = yaml.get('struct')
165168
self.sub_type = yaml.get('sub-type')
166169
self.byte_order = yaml.get('byte-order')
170+
self.len = yaml.get('len')
171+
self.display_hint = yaml.get('display-hint')
167172

168173

169174
class SpecAttrSet(SpecElement):
@@ -229,12 +234,17 @@ class SpecStructMember(SpecElement):
229234
type string, type of the member attribute
230235
byte_order string or None for native byte order
231236
enum string, name of the enum definition
237+
len integer, optional byte length of binary types
238+
display_hint string, hint to help choose format specifier
239+
when displaying the value
232240
"""
233241
def __init__(self, family, yaml):
234242
super().__init__(family, yaml)
235243
self.type = yaml['type']
236244
self.byte_order = yaml.get('byte-order')
237245
self.enum = yaml.get('enum')
246+
self.len = yaml.get('len')
247+
self.display_hint = yaml.get('display-hint')
238248

239249

240250
class SpecStruct(SpecElement):

tools/net/ynl/lib/ynl.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import struct
99
from struct import Struct
1010
import yaml
11+
import ipaddress
12+
import uuid
1113

1214
from .nlspec import SpecFamily
1315

@@ -105,6 +107,20 @@ def get_format(cls, attr_type, byte_order=None):
105107
else format.little
106108
return format.native
107109

110+
@classmethod
111+
def formatted_string(cls, raw, display_hint):
112+
if display_hint == 'mac':
113+
formatted = ':'.join('%02x' % b for b in raw)
114+
elif display_hint == 'hex':
115+
formatted = bytes.hex(raw, ' ')
116+
elif display_hint in [ 'ipv4', 'ipv6' ]:
117+
formatted = format(ipaddress.ip_address(raw))
118+
elif display_hint == 'uuid':
119+
formatted = str(uuid.UUID(bytes=raw))
120+
else:
121+
formatted = raw
122+
return formatted
123+
108124
def as_scalar(self, attr_type, byte_order=None):
109125
format = self.get_format(attr_type, byte_order)
110126
return format.unpack(self.raw)[0]
@@ -124,10 +140,16 @@ def as_struct(self, members):
124140
offset = 0
125141
for m in members:
126142
# TODO: handle non-scalar members
127-
format = self.get_format(m.type, m.byte_order)
128-
decoded = format.unpack_from(self.raw, offset)
129-
offset += format.size
130-
value[m.name] = decoded[0]
143+
if m.type == 'binary':
144+
decoded = self.raw[offset:offset+m['len']]
145+
offset += m['len']
146+
elif m.type in NlAttr.type_formats:
147+
format = self.get_format(m.type, m.byte_order)
148+
[ decoded ] = format.unpack_from(self.raw, offset)
149+
offset += format.size
150+
if m.display_hint:
151+
decoded = self.formatted_string(decoded, m.display_hint)
152+
value[m.name] = decoded
131153
return value
132154

133155
def __repr__(self):
@@ -385,7 +407,7 @@ def _add_attr(self, space, name, value):
385407
elif attr["type"] == 'string':
386408
attr_payload = str(value).encode('ascii') + b'\x00'
387409
elif attr["type"] == 'binary':
388-
attr_payload = value
410+
attr_payload = bytes.fromhex(value)
389411
elif attr['type'] in NlAttr.type_formats:
390412
format = NlAttr.get_format(attr['type'], attr.byte_order)
391413
attr_payload = format.pack(int(value))
@@ -421,6 +443,8 @@ def _decode_binary(self, attr, attr_spec):
421443
decoded = attr.as_c_array(attr_spec.sub_type)
422444
else:
423445
decoded = attr.as_bin()
446+
if attr_spec.display_hint:
447+
decoded = NlAttr.formatted_string(decoded, attr_spec.display_hint)
424448
return decoded
425449

426450
def _decode(self, attrs, space):

0 commit comments

Comments
 (0)