Skip to contentSkip to conversation

tools/net/ynl: Add support for netlink-raw families

PATCH Superseded Donald Hunter 4 participants 16 messages Jul 25, 2023 – Jul 26, 2023

Subsystems (3)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Donald Hunter

author maintainer
#0 lore raw

cover tools/net/ynl: Add support for netlink-raw families

This patchset adds support for netlink-raw families such as rtnetlink.

The first patch contains the schema definition. The second patch extends ynl to support netlink-raw The third patch adds rtnetlink addr and route message types

The second patch depends on "tools: ynl-gen: fix parse multi-attr enum attribute":

https://patchwork.kernel.org/project/netdevbpf/list/?series=769229

The netlink-raw schema is very similar to genetlink-legacy and I thought about making the changes there and symlinking to it. On balance I thought that might be problematic for accurate schema validation.

rtnetlink doesn't seem to fit into unified or directional message enumeration models. It seems like an 'explicit' model would be useful, to require the schema author to specify the message ids directly. The patch supports commands and it supports notifications, but it's currently hard to support both simultaneously from the same netlink-raw spec. I plan to work on this in a future patchset.

There is not yet support for notifications because ynl currently doesn't support defining 'event' properties on a 'do' operation. I plan to work on this in a future patch.

The link message types are a work in progress that I plan to submit in a future patchset. Links contain different nested attributes dependent on the type of link. Decoding these will need some kind of attr-space selection based on the value of another attribute in the message.

Donald Hunter (3):
doc/netlink: Add a schema for netlink-raw families tools/net/ynl: Add support for netlink-raw families doc/netlink: Add specs for addr and route rtnetlink message types

 Documentation/netlink/netlink-raw.yaml    | 414 ++++++++++++++++++++++
 Documentation/netlink/specs/rt_addr.yaml  | 179 ++++++++++
 Documentation/netlink/specs/rt_route.yaml | 192 ++++++++++
 tools/net/ynl/lib/nlspec.py               |  25 ++
 tools/net/ynl/lib/ynl.py                  | 185 +++++++---
 5 files changed, 941 insertions(+), 54 deletions(-)
 create mode 100644 Documentation/netlink/netlink-raw.yaml
 create mode 100644 Documentation/netlink/specs/rt_addr.yaml
 create mode 100644 Documentation/netlink/specs/rt_route.yaml
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/3 doc/netlink: Add a schema for netlink-raw families

This schema is largely a copy of the genetlink-legacy schema with the following additions:

- a top-level protonum property, e.g. 0 (for NETLINK_ROUTE)
- add netlink-raw to the list of protocols supported by the schema
- add an id property to mcast-group definitions

---
 Documentation/netlink/netlink-raw.yaml | 414 +++++++++++++++++++++++++
 1 file changed, 414 insertions(+)
 create mode 100644 Documentation/netlink/netlink-raw.yaml
Documentation/netlink/netlink-raw.yaml new file +414 -0
new file mode 100644
index 000000000000..34af9c7f94a8
@@ -0,0 +1,414 @@
+# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+%YAML 1.2
+---
+$id: http://kernel.org/schemas/netlink/genetlink-legacy.yaml#
+$schema: https://json-schema.org/draft-07/schema
+
+# Common defines
+$defs:
+  uint:
+    type: integer
+    minimum: 0
+  len-or-define:
+    type: [ string, integer ]
+    pattern: ^[0-9A-Za-z_]+( - 1)?$
+    minimum: 0
+
+# Schema for specs
+title: Protocol
+description: Specification of a genetlink protocol
+type: object
+required: [ name, doc, attribute-sets, operations ]
+additionalProperties: False
+properties:
+  name:
+    description: Name of the genetlink family.
+    type: string
+  doc:
+    type: string
+  version:
+    description: Generic Netlink family version. Default is 1.
+    type: integer
+    minimum: 1
+  protocol:
+    description: Schema compatibility level. Default is "genetlink".
+    enum: [ genetlink, genetlink-c, genetlink-legacy, netlink-raw ] # Trim
+  # Start netlink-raw
+  protonum:
+    description: Protocol number to use for netlink-raw
+    type: integer
+  # End netlink-raw
+  uapi-header:
+    description: Path to the uAPI header, default is linux/${family-name}.h
+    type: string
+  # Start genetlink-c
+  c-family-name:
+    description: Name of the define for the family name.
+    type: string
+  c-version-name:
+    description: Name of the define for the verion of the family.
+    type: string
+  max-by-define:
+    description: Makes the number of attributes and commands be specified by a define, not an enum value.
+    type: boolean
+  # End genetlink-c
+  # Start genetlink-legacy
+  kernel-policy:
+    description: |
+      Defines if the input policy in the kernel is global, per-operation, or split per operation type.
+      Default is split.
+    enum: [ split, per-op, global ]
+  # End genetlink-legacy
+
+  definitions:
+    description: List of type and constant definitions (enums, flags, defines).
+    type: array
+    items:
+      type: object
+      required: [ type, name ]
+      additionalProperties: False
+      properties:
+        name:
+          type: string
+        header:
+          description: For C-compatible languages, header which already defines this value.
+          type: string
+        type:
+          enum: [ const, enum, flags, struct ] # Trim
+        doc:
+          type: string
+        # For const
+        value:
+          description: For const - the value.
+          type: [ string, integer ]
+        # For enum and flags
+        value-start:
+          description: For enum or flags the literal initializer for the first value.
+          type: [ string, integer ]
+        entries:
+          description: For enum or flags array of values.
+          type: array
+          items:
+            oneOf:
+              - type: string
+              - type: object
+                required: [ name ]
+                additionalProperties: False
+                properties:
+                  name:
+                    type: string
+                  value:
+                    type: integer
+                  doc:
+                    type: string
+        render-max:
+          description: Render the max members for this enum.
+          type: boolean
+        # Start genetlink-c
+        enum-name:
+          description: Name for enum, if empty no name will be used.
+          type: [ string, "null" ]
+        name-prefix:
+          description: For enum the prefix of the values, optional.
+          type: string
+        # End genetlink-c
+        # Start genetlink-legacy
+        members:
+          description: List of struct members. Only scalars and strings members allowed.
+          type: array
+          items:
+            type: object
+            required: [ name, type ]
+            additionalProperties: False
+            properties:
+              name:
+                type: string
+              type:
+                description: The netlink attribute type
+                enum: [ u8, u16, u32, u64, s8, s16, s32, s64, string, binary ]
+              len:
+                $ref: '#/$defs/len-or-define'
+              byte-order:
+                enum: [ little-endian, big-endian ]
+              doc:
+                description: Documentation for the struct member attribute.
+                type: string
+              enum:
+                description: Name of the enum type used for the attribute.
+                type: string
+              enum-as-flags:
+                description: |
+                  Treat the enum as flags. In most cases enum is either used as flags or as values.
+                  Sometimes, however, both forms are necessary, in which case header contains the enum
+                  form while specific attributes may request to convert the values into a bitfield.
+                type: boolean
+              display-hint: &display-hint
+                description: |
+                  Optional format indicator that is intended only for choosing
+                  the right formatting mechanism when displaying values of this
+                  type.
+                enum: [ hex, mac, fddi, ipv4, ipv6, uuid ]
+        # End genetlink-legacy
+
+  attribute-sets:
+    description: Definition of attribute spaces for this family.
+    type: array
+    items:
+      description: Definition of a single attribute space.
+      type: object
+      required: [ name, attributes ]
+      additionalProperties: False
+      properties:
+        name:
+          description: |
+            Name used when referring to this space in other definitions, not used outside of the spec.
+          type: string
+        name-prefix:
+          description: |
+            Prefix for the C enum name of the attributes. Default family[name]-set[name]-a-
+          type: string
+        enum-name:
+          description: Name for the enum type of the attribute.
+          type: string
+        doc:
+          description: Documentation of the space.
+          type: string
+        subset-of:
+          description: |
+            Name of another space which this is a logical part of. Sub-spaces can be used to define
+            a limited group of attributes which are used in a nest.
+          type: string
+        # Start genetlink-c
+        attr-cnt-name:
+          description: The explicit name for constant holding the count of attributes (last attr + 1).
+          type: string
+        attr-max-name:
+          description: The explicit name for last member of attribute enum.
+          type: string
+        # End genetlink-c
+        attributes:
+          description: List of attributes in the space.
+          type: array
+          items:
+            type: object
+            required: [ name, type ]
+            additionalProperties: False
+            properties:
+              name:
+                type: string
+              type: &attr-type
+                description: The netlink attribute type
+                enum: [ unused, pad, flag, binary, u8, u16, u32, u64, s32, s64,
+                        string, nest, array-nest, nest-type-value ]
+              doc:
+                description: Documentation of the attribute.
+                type: string
+              value:
+                description: Value for the enum item representing this attribute in the uAPI.
+                $ref: '#/$defs/uint'
+              type-value:
+                description: Name of the value extracted from the type of a nest-type-value attribute.
+                type: array
+                items:
+                  type: string
+              byte-order:
+                enum: [ little-endian, big-endian ]
+              multi-attr:
+                type: boolean
+              nested-attributes:
+                description: Name of the space (sub-space) used inside the attribute.
+                type: string
+              enum:
+                description: Name of the enum type used for the attribute.
+                type: string
+              enum-as-flags:
+                description: |
+                  Treat the enum as flags. In most cases enum is either used as flags or as values.
+                  Sometimes, however, both forms are necessary, in which case header contains the enum
+                  form while specific attributes may request to convert the values into a bitfield.
+                type: boolean
+              checks:
+                description: Kernel input validation.
+                type: object
+                additionalProperties: False
+                properties:
+                  flags-mask:
+                    description: Name of the flags constant on which to base mask (unsigned scalar types only).
+                    type: string
+                  min:
+                    description: Min value for an integer attribute.
+                    type: integer
+                  min-len:
+                    description: Min length for a binary attribute.
+                    $ref: '#/$defs/len-or-define'
+                  max-len:
+                    description: Max length for a string or a binary attribute.
+                    $ref: '#/$defs/len-or-define'
+              sub-type: *attr-type
+              display-hint: *display-hint
+              # Start genetlink-c
+              name-prefix:
+                type: string
+              # End genetlink-c
+              # Start genetlink-legacy
+              struct:
+                description: Name of the struct type used for the attribute.
+                type: string
+              # End genetlink-legacy
+
+      # Make sure name-prefix does not appear in subsets (subsets inherit naming)
+      dependencies:
+        name-prefix:
+          not:
+            required: [ subset-of ]
+        subset-of:
+          not:
+            required: [ name-prefix ]
+
+  operations:
+    description: Operations supported by the protocol.
+    type: object
+    required: [ list ]
+    additionalProperties: False
+    properties:
+      enum-model:
+        description: |
+          The model of assigning values to the operations.
+          "unified" is the recommended model where all message types belong
+          to a single enum.
+          "directional" has the messages sent to the kernel and from the kernel
+          enumerated separately.
+        enum: [ unified, directional ] # Trim
+      name-prefix:
+        description: |
+          Prefix for the C enum name of the command. The name is formed by concatenating
+          the prefix with the upper case name of the command, with dashes replaced by underscores.
+        type: string
+      enum-name:
+        description: Name for the enum type with commands.
+        type: string
+      async-prefix:
+        description: Same as name-prefix but used to render notifications and events to separate enum.
+        type: string
+      async-enum:
+        description: Name for the enum type with notifications/events.
+        type: string
+      # Start genetlink-legacy
+      fixed-header: &fixed-header
+        description: |
+          Name of the structure defining the optional fixed-length protocol
+          header. This header is placed in a message after the netlink and
+          genetlink headers and before any attributes.
+        type: string
+      # End genetlink-legacy
+      list:
+        description: List of commands
+        type: array
+        items:
+          type: object
+          additionalProperties: False
+          required: [ name, doc ]
+          properties:
+            name:
+              description: Name of the operation, also defining its C enum value in uAPI.
+              type: string
+            doc:
+              description: Documentation for the command.
+              type: string
+            value:
+              description: Value for the enum in the uAPI.
+              $ref: '#/$defs/uint'
+            attribute-set:
+              description: |
+                Attribute space from which attributes directly in the requests and replies
+                to this command are defined.
+              type: string
+            flags: &cmd_flags
+              description: Command flags.
+              type: array
+              items:
+                enum: [ admin-perm ]
+            dont-validate:
+              description: Kernel attribute validation flags.
+              type: array
+              items:
+                enum: [ strict, dump ]
+            # Start genetlink-legacy
+            fixed-header: *fixed-header
+            # End genetlink-legacy
+            do: &subop-type
+              description: Main command handler.
+              type: object
+              additionalProperties: False
+              properties:
+                request: &subop-attr-list
+                  description: Definition of the request message for a given command.
+                  type: object
+                  additionalProperties: False
+                  properties:
+                    attributes:
+                      description: |
+                        Names of attributes from the attribute-set (not full attribute
+                        definitions, just names).
+                      type: array
+                      items:
+                        type: string
+                    # Start genetlink-legacy
+                    value:
+                      description: |
+                        ID of this message if value for request and response differ,
+                        i.e. requests and responses have different message enums.
+                      $ref: '#/$defs/uint'
+                    # End genetlink-legacy
+                reply: *subop-attr-list
+                pre:
+                  description: Hook for a function to run before the main callback (pre_doit or start).
+                  type: string
+                post:
+                  description: Hook for a function to run after the main callback (post_doit or done).
+                  type: string
+            dump: *subop-type
+            notify:
+              description: Name of the command sharing the reply type with this notification.
+              type: string
+            event:
+              type: object
+              additionalProperties: False
+              properties:
+                attributes:
+                  description: Explicit list of the attributes for the notification.
+                  type: array
+                  items:
+                    type: string
+            mcgrp:
+              description: Name of the multicast group generating given notification.
+              type: string
+  mcast-groups:
+    description: List of multicast groups.
+    type: object
+    required: [ list ]
+    additionalProperties: False
+    properties:
+      list:
+        description: List of groups.
+        type: array
+        items:
+          type: object
+          required: [ name ]
+          additionalProperties: False
+          properties:
+            name:
+              description: |
+                The name for the group, used to form the define and the value of the define.
+              type: string
+            # Start genetlink-c
+            c-define-name:
+              description: Override for the name of the define in C uAPI.
+              type: string
+            # End genetlink-c
+            flags: *cmd_flags
+            # Start netlink-raw
+            id:
+              description: Id of the netlink multicast group
+              type: integer
+            # End netlink-raw
Signature · 1 line
2.41.0
maintainer
#2 lore raw

Re: [PATCH net-next v1 1/3] doc/netlink: Add a schema for netlink-raw families

Quoted 1 line · On Tue, 25 Jul 2023 17:22:03 +0100 Donald Hunter wrote:

- add an id property to mcast-group definitions

nit: my first thought would be to call it "value" rather than "id" to stick with the naming we have to other objects. Any pros/cons you see?

Other than that the big ask would be to update the documentation :)

author maintainer
#3 lore raw

Re: [PATCH net-next v1 1/3] doc/netlink: Add a schema for netlink-raw families

Quoted 7 lines · Jakub Kicinski
Quoted 1 line · On Tue, 25 Jul 2023 17:22:03 +0100 Donald Hunter wrote:

- add an id property to mcast-group definitions

nit: my first thought would be to call it "value" rather than "id" to stick with the naming we have to other objects. Any pros/cons you see?

Yep, that makes sense. I wasn't sure about "id" anyway but it didn't occur to me to align with "value".

Quoted 1 line

Other than that the big ask would be to update the documentation :)

Do you mind if I update the docs as a follow up patch? I'm on holiday for the next 10 days - I should have time to respin this patchset but maybe not to work on docs as well.

maintainer
#4 lore raw

Re: [PATCH net-next v1 1/3] doc/netlink: Add a schema for netlink-raw families

Quoted 5 lines · On Wed, 26 Jul 2023 22:48:54 +0100 Donald Hunter wrote:
Quoted 1 line

Other than that the big ask would be to update the documentation :)

Do you mind if I update the docs as a follow up patch? I'm on holiday for the next 10 days - I should have time to respin this patchset but maybe not to work on docs as well.

Ugh, we had some issues with people posting stuff while / before taking time off recently (napi netlink, sendpage, tcx). I'd rather wait until you're back.

author maintainer
#5 lore raw

PATCH 2/3 tools/net/ynl: Add support for netlink-raw families

In reply to Donald Hunter

Refactor the ynl code to encapsulate protocol-family specifics into NetlinkProtocolFamily and GenlProtocolFamily.

---
 tools/net/ynl/lib/nlspec.py |  25 +++++
 tools/net/ynl/lib/ynl.py    | 185 +++++++++++++++++++++++++-----------
 2 files changed, 156 insertions(+), 54 deletions(-)
tools/net/ynl/lib/nlspec.py +25 -0
index 0ff0d18666b2..6072c9f35223 100644
@@ -322,6 +322,20 @@ class SpecOperation(SpecElement):
             self.attr_set = self.family.attr_sets[attr_set_name]
 
 
+class SpecMcastGroup(SpecElement):
+    """Netlink Multicast Group
+
+    Information about a multicast group.
+
+    Attributes:
+        id        numerical id of this multicast group for netlink-raw
+        yaml      raw spec as loaded from the spec file
+    """
+    def __init__(self, family, yaml):
+        super().__init__(family, yaml)
+        self.id = self.yaml.get('id')
+
+
 class SpecFamily(SpecElement):
     """ Netlink Family Spec class.
 
@@ -343,6 +357,7 @@ class SpecFamily(SpecElement):
         ntfs       dict of all async events
         consts     dict of all constants/enums
         fixed_header  string, optional name of family default fixed header struct
+        mcast_groups  dict of all multicast groups (index by name)
     """
     def __init__(self, spec_path, schema_path=None, exclude_ops=None):
         with open(spec_path, "r") as stream:
@@ -384,6 +399,7 @@ class SpecFamily(SpecElement):
         self.ops = collections.OrderedDict()
         self.ntfs = collections.OrderedDict()
         self.consts = collections.OrderedDict()
+        self.mcast_groups = collections.OrderedDict()
 
         last_exception = None
         while len(self._resolution_list) > 0:
@@ -416,6 +432,9 @@ class SpecFamily(SpecElement):
     def new_operation(self, elem, req_val, rsp_val):
         return SpecOperation(self, elem, req_val, rsp_val)
 
+    def new_mcast_group(self, elem):
+        return SpecMcastGroup(self, elem)
+
     def add_unresolved(self, elem):
         self._resolution_list.append(elem)
 
@@ -512,3 +531,9 @@ class SpecFamily(SpecElement):
                 self.ops[op.name] = op
             elif op.is_async:
                 self.ntfs[op.name] = op
+
+        mcgs = self.yaml.get('mcast-groups')
+        if mcgs:
+            for elem in mcgs['list']:
+                mcg = self.new_mcast_group(elem)
+                self.mcast_groups[elem['name']] = mcg
tools/net/ynl/lib/ynl.py +131 -54

301 lines not on this page · raw message

Signature · 1 line
2.41.0
maintainer
#6 lore raw
Quoted 5 lines · On Tue, 25 Jul 2023 17:22:04 +0100 Donald Hunter wrote:

Refactor the ynl code to encapsulate protocol-family specifics into NetlinkProtocolFamily and GenlProtocolFamily.

tools/net/ynl/lib/nlspec.py +2 -0
+class SpecMcastGroup(SpecElement):
+    """Netlink Multicast Group

tools/net/ynl/lib/nlspec.py class SpecOperation(SpecElement): · line 336

Quoted 10 lines
tools/net/ynl/lib/nlspec.py +10 -0
+
+    Information about a multicast group.
+
+    Attributes:
+        id        numerical id of this multicast group for netlink-raw
+        yaml      raw spec as loaded from the spec file
+    """
+    def __init__(self, family, yaml):
+        super().__init__(family, yaml)
+        self.id = self.yaml.get('id')

name, too?

Quoted 30 lines

class SpecFamily(SpecElement):
""" Netlink Family Spec class.

tools/net/ynl/lib/nlspec.py +5 -0
@@ -343,6 +357,7 @@ class SpecFamily(SpecElement):
         ntfs       dict of all async events
         consts     dict of all constants/enums
         fixed_header  string, optional name of family default fixed header struct
+        mcast_groups  dict of all multicast groups (index by name)
     """
     def __init__(self, spec_path, schema_path=None, exclude_ops=None):
         with open(spec_path, "r") as stream:
@@ -384,6 +399,7 @@ class SpecFamily(SpecElement):
         self.ops = collections.OrderedDict()
         self.ntfs = collections.OrderedDict()
         self.consts = collections.OrderedDict()
+        self.mcast_groups = collections.OrderedDict()
 
         last_exception = None
         while len(self._resolution_list) > 0:
@@ -416,6 +432,9 @@ class SpecFamily(SpecElement):
     def new_operation(self, elem, req_val, rsp_val):
         return SpecOperation(self, elem, req_val, rsp_val)
 
+    def new_mcast_group(self, elem):
+        return SpecMcastGroup(self, elem)
+
     def add_unresolved(self, elem):
         self._resolution_list.append(elem)
 

tools/net/ynl/lib/nlspec.py class SpecFamily(SpecElement): · line 539

Quoted 9 lines
tools/net/ynl/lib/nlspec.py +6 -0
@@ -512,3 +531,9 @@ class SpecFamily(SpecElement):
                 self.ops[op.name] = op
             elif op.is_async:
                 self.ntfs[op.name] = op
+
+        mcgs = self.yaml.get('mcast-groups')
+        if mcgs:
+            for elem in mcgs['list']:
+                mcg = self.new_mcast_group(elem)
+                self.mcast_groups[elem['name']] = mcg

Could you factor out the mcgroup changes to a separate patch?

Quoted 20 lines
tools/net/ynl/lib/ynl.py +5 -0
index 3e2ade2194cd..7e877c43e10f 100644
@@ -25,6 +25,7 @@ class Netlink:
     NETLINK_ADD_MEMBERSHIP = 1
     NETLINK_CAP_ACK = 10
     NETLINK_EXT_ACK = 11
+    NETLINK_GET_STRICT_CHK = 12
 
     # Netlink message
     NLMSG_ERROR = 2
@@ -153,6 +154,21 @@ class NlAttr:
             value[m.name] = decoded
         return value
 
+    @classmethod
+    def decode_enum(cls, raw, attr_spec, consts):
+        enum = consts[attr_spec['enum']]
+        if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:

tools/net/ynl/lib/ynl.py class NlAttr: · line 170

Quoted 10 lines
tools/net/ynl/lib/ynl.py +10 -0
+            i = 0
+            value = set()
+            while raw:
+                if raw & 1:
+                    value.add(enum.entries_by_val[i].name)
+                raw >>= 1
+                i += 1
+        else:
+            value = enum.entries_by_val[raw].name
+        return value

This doesn't always operates on netlink attributes, technically, so how about we make it a standalone function, not a member of NlAttr? Or should we move it to a parent class, NetlinkProtocolFamily?

Quoted 7 lines
tools/net/ynl/lib/ynl.py +7 -0
+    def decode_fixed_header(self, consts, op):
+        fixed_header_members = consts[op.fixed_header].members
+        self.fixed_header_attrs = dict()
+        offset = 0
+        for m in fixed_header_members:
+            format = NlAttr.get_format(m.type, m.byte_order)
+            [ value ] = format.unpack_from(self.raw, offset)

tools/net/ynl/lib/ynl.py class NlMsg: · line 265

Quoted 10 lines
tools/net/ynl/lib/ynl.py +10 -0
+            offset += format.size
+
+            if m.enum:
+                value = NlAttr.decode_enum(value, m, consts)
+
+            self.fixed_header_attrs[m.name] = value
+        self.raw = self.raw[offset:]
+
+    def cmd(self):
+        return self.nl_type

And perhaps the pure code moves could be a separate patch for ease of review?

Quoted 7 lines
     def __repr__(self):
         msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl_type}\n"
         if self.error:
tools/net/ynl/lib/ynl.py +0 -0
@@ -318,23 +353,21 @@ def _genl_load_families():
 
 
 class GenlMsg:

tools/net/ynl/lib/ynl.py def _genl_load_families(): · line 361

Quoted 10 lines
tools/net/ynl/lib/ynl.py +5 -4
-    def __init__(self, nl_msg, fixed_header_members=[]):
-        self.nl = nl_msg
+    def __init__(self, nl_msg, ynl=None):
+        self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
+        nl_msg.raw = nl_msg.raw[4:]
 
-        self.hdr = nl_msg.raw[0:4]
-        offset = 4
+        if ynl:
+            op = ynl.rsp_by_value[self.genl_cmd]

Took me a while to figure out why ynl gets passed here :S I'm not sure what the best structure of inheritance is but I think we should at the very least *not* call genl vs raw-nl "family".

NetlinkProtocolFamily -> NetlinkProtocol
GenlProtocolFamily -> GenlProtocol

and store them in YnlFamily to self.nlproto or self.protocol or some such.

tools/net/ynl/lib/ynl.py class YnlFamily(SpecFamily): · line 502

Quoted 8 lines
tools/net/ynl/lib/ynl.py +3 -1
+            if op.fixed_header:
+                nl_msg.decode_fixed_header(ynl.consts, op)
 
     def _decode_binary(self, attr, attr_spec):
         if attr_spec.struct_name:
             members = self.consts[attr_spec.struct_name]
-            decoded = attr.as_struct(members)
+            decoded = attr.as_struct(members, self.consts)

I applied the series on top of Arkadiusz's fixes and this line throws an "as_struct takes 2 arguments, 3 given" exception.

Quoted 3 lines
tools/net/ynl/lib/ynl.py +0 -0
             for m in members:
                 if m.enum:
                      decoded[m.name] = self._decode_enum(decoded[m.name], m)
author maintainer
#7 lore raw
Quoted 5 lines · Jakub Kicinski
Quoted 2 lines · On Tue, 25 Jul 2023 17:22:04 +0100 Donald Hunter wrote:

Refactor the ynl code to encapsulate protocol-family specifics into NetlinkProtocolFamily and GenlProtocolFamily.

Quoted 14 lines

tools/net/ynl/lib/nlspec.py class SpecOperation(SpecElement): · line 336

Quoted 12 lines
tools/net/ynl/lib/nlspec.py +12 -0
+class SpecMcastGroup(SpecElement):
+    """Netlink Multicast Group
+
+    Information about a multicast group.
+
+    Attributes:
+        id        numerical id of this multicast group for netlink-raw
+        yaml      raw spec as loaded from the spec file
+    """
+    def __init__(self, family, yaml):
+        super().__init__(family, yaml)
+        self.id = self.yaml.get('id')

name, too?

Ack.

Quoted 7 lines

tools/net/ynl/lib/nlspec.py class SpecFamily(SpecElement): · line 539

Quoted 5 lines
tools/net/ynl/lib/nlspec.py +5 -0
+        mcgs = self.yaml.get('mcast-groups')
+        if mcgs:
+            for elem in mcgs['list']:
+                mcg = self.new_mcast_group(elem)
+                self.mcast_groups[elem['name']] = mcg

Could you factor out the mcgroup changes to a separate patch?

Will do.

Quoted 26 lines
Quoted 26 lines
tools/net/ynl/lib/ynl.py +11 -0
index 3e2ade2194cd..7e877c43e10f 100644
@@ -25,6 +25,7 @@ class Netlink:
     NETLINK_ADD_MEMBERSHIP = 1
     NETLINK_CAP_ACK = 10
     NETLINK_EXT_ACK = 11
+    NETLINK_GET_STRICT_CHK = 12
 
     # Netlink message
     NLMSG_ERROR = 2
@@ -153,6 +154,21 @@ class NlAttr:
             value[m.name] = decoded
         return value
 
+    @classmethod
+    def decode_enum(cls, raw, attr_spec, consts):
+        enum = consts[attr_spec['enum']]
+        if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
+            i = 0
+            value = set()
+            while raw:
+                if raw & 1:
+                    value.add(enum.entries_by_val[i].name)
+                raw >>= 1
Quoted 8 lines

tools/net/ynl/lib/ynl.py class NlAttr: · line 170

Quoted 4 lines
tools/net/ynl/lib/ynl.py +4 -0
+                i += 1
+        else:
+            value = enum.entries_by_val[raw].name
+        return value

This doesn't always operates on netlink attributes, technically, so how about we make it a standalone function, not a member of NlAttr? Or should we move it to a parent class, NetlinkProtocolFamily?

Fair point. I'll maybe go for standalone just now but will think on it some more first.

Quoted 13 lines
Quoted 13 lines
tools/net/ynl/lib/ynl.py +12 -0
+    def decode_fixed_header(self, consts, op):
+        fixed_header_members = consts[op.fixed_header].members
+        self.fixed_header_attrs = dict()
+        offset = 0
+        for m in fixed_header_members:
+            format = NlAttr.get_format(m.type, m.byte_order)
+            [ value ] = format.unpack_from(self.raw, offset)
+            offset += format.size
+
+            if m.enum:
+                value = NlAttr.decode_enum(value, m, consts)
+
Quoted 8 lines

tools/net/ynl/lib/ynl.py class NlMsg: · line 265

Quoted 5 lines
tools/net/ynl/lib/ynl.py +5 -0
+            self.fixed_header_attrs[m.name] = value
+        self.raw = self.raw[offset:]
+
+    def cmd(self):
+        return self.nl_type

And perhaps the pure code moves could be a separate patch for ease of review?

Ack.

Quoted 23 lines
Quoted 7 lines
     def __repr__(self):
         msg = f"nl_len = {self.nl_len} ({len(self.raw)}) nl_flags = 0x{self.nl_flags:x} nl_type = {self.nl_type}\n"
         if self.error:
tools/net/ynl/lib/ynl.py +0 -0
@@ -318,23 +353,21 @@ def _genl_load_families():
 
 
 class GenlMsg:

tools/net/ynl/lib/ynl.py def _genl_load_families(): · line 361

Quoted 10 lines
tools/net/ynl/lib/ynl.py +5 -4
-    def __init__(self, nl_msg, fixed_header_members=[]):
-        self.nl = nl_msg
+    def __init__(self, nl_msg, ynl=None):
+        self.genl_cmd, self.genl_version, _ = struct.unpack_from("BBH", nl_msg.raw, 0)
+        nl_msg.raw = nl_msg.raw[4:]
 
-        self.hdr = nl_msg.raw[0:4]
-        offset = 4
+        if ynl:
+            op = ynl.rsp_by_value[self.genl_cmd]

Took me a while to figure out why ynl gets passed here :S I'm not sure what the best structure of inheritance is but I think we should at the very least *not* call genl vs raw-nl "family".

Quoted 2 lines

NetlinkProtocolFamily -> NetlinkProtocol
GenlProtocolFamily -> GenlProtocol

Yeah, agreed. "Family" is way too overloaded already :-)

Quoted 2 lines

and store them in YnlFamily to self.nlproto or self.protocol or some such.

Ack. Just a note that I have been wondering about refactoring this from "YnlFamily is a Spec" to "Ynl has n Specs" so that we could do multi spec notification handling. If we did this, then passing a SpecContext around would look more natural maybe.

Quoted 11 lines

tools/net/ynl/lib/ynl.py class YnlFamily(SpecFamily): · line 502

Quoted 8 lines
tools/net/ynl/lib/ynl.py +3 -1
+            if op.fixed_header:
+                nl_msg.decode_fixed_header(ynl.consts, op)
 
     def _decode_binary(self, attr, attr_spec):
         if attr_spec.struct_name:
             members = self.consts[attr_spec.struct_name]
-            decoded = attr.as_struct(members)
+            decoded = attr.as_struct(members, self.consts)

I applied the series on top of Arkadiusz's fixes and this line throws an "as_struct takes 2 arguments, 3 given" exception.

Ah, my bad. Looks like I missed a fix for that from the patchset.

Quoted 4 lines
Quoted 3 lines
tools/net/ynl/lib/ynl.py +0 -0
             for m in members:
                 if m.enum:
                      decoded[m.name] = self._decode_enum(decoded[m.name], m)
maintainer
#8 lore raw
Quoted 7 lines · On Wed, 26 Jul 2023 23:01:12 +0100 Donald Hunter wrote:
Quoted 2 lines

and store them in YnlFamily to self.nlproto or self.protocol or some such.

Ack. Just a note that I have been wondering about refactoring this from "YnlFamily is a Spec" to "Ynl has n Specs" so that we could do multi spec notification handling. If we did this, then passing a SpecContext around would look more natural maybe.

Ynl with multiple specs is doable from the technical standpoint, I think the API to expose from such a library may be a bigger challenge. And I'm not sure if it's worth the complexity in practice.

Quoted 4 lines
Quoted 2 lines

I applied the series on top of Arkadiusz's fixes and this line throws an "as_struct takes 2 arguments, 3 given" exception.

Ah, my bad. Looks like I missed a fix for that from the patchset.

FWIW I ended up merging the fixes to net, but they should be in net-next by tomorrow afternoon.

author maintainer
#9 lore raw

PATCH 3/3 doc/netlink: Add specs for addr and route rtnetlink message types

In reply to Donald Hunter

Add netlink-raw specs for the following rtnetlink messages:
- newaddr, deladdr, getaddr (dump)
- getroute (dump)

---
 Documentation/netlink/specs/rt_addr.yaml  | 179 ++++++++++++++++++++
 Documentation/netlink/specs/rt_route.yaml | 192 ++++++++++++++++++++++
 2 files changed, 371 insertions(+)
 create mode 100644 Documentation/netlink/specs/rt_addr.yaml
 create mode 100644 Documentation/netlink/specs/rt_route.yaml
Documentation/netlink/specs/rt_addr.yaml new file +179 -0
new file mode 100644
index 000000000000..f97ae7c35e44
@@ -0,0 +1,179 @@
+# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
+
+name: rt-addr
+protocol: netlink-raw
+protonum: 0
+
+doc:
+  Address configuration over rtnetlink.
+
+definitions:
+  -
+    name: ifaddrmsg
+    type: struct
+    members:
+      -
+        name: ifa-family
+        type: u8
+      -
+        name: ifa-prefixlen
+        type: u8
+      -
+        name: ifa-flags
+        type: u8
+        enum: ifa-flags
+        enum-as-flags: true
+      -
+        name: ifa-scope
+        type: u8
+      -
+        name: ifa-index
+        type: u32
+  -
+    name: ifa-cacheinfo
+    type: struct
+    members:
+      -
+        name: ifa-prefered
+        type: u32
+      -
+        name: ifa-valid
+        type: u32
+      -
+        name: cstamp
+        type: u32
+      -
+        name: tstamp
+        type: u32
+
+  -
+    name: ifa-flags
+    type: flags
+    entries:
+      -
+        name: secondary
+      -
+        name: nodad
+      -
+        name: optimistic
+      -
+        name: dadfailed
+      -
+        name: homeaddress
+      -
+        name: deprecated
+      -
+        name: tentative
+      -
+        name: permanent
+      -
+        name: managetempaddr
+      -
+        name: noprefixroute
+      -
+        name: mcautojoin
+      -
+        name: stable-privacy
+
+attribute-sets:
+  -
+    name: addr-attrs
+    attributes:
+      -
+        name: ifa-address
+        type: binary
+        display-hint: ipv4
+      -
+        name: ifa-local
+        type: binary
+        display-hint: ipv4
+      -
+        name: ifa-label
+        type: string
+      -
+        name: ifa-broadcast
+        type: binary
+        display-hint: ipv4
+      -
+        name: ifa-anycast
+        type: binary
+      -
+        name: ifa-cacheinfo
+        type: binary
+        struct: ifa-cacheinfo
+      -
+        name: ifa-multicast
+        type: binary
+      -
+        name: ifa-flags
+        type: u32
+        enum: ifa-flags
+        enum-as-flags: true
+      -
+        name: ifa-rt-priority
+        type: u32
+      -
+        name: ifa-target-netnsid
+        type: binary
+      -
+        name: ifa-proto
+        type: u8
+
+
+operations:
+  fixed-header: ifaddrmsg
+  enum-model: directional
+  list:
+    -
+      name: newaddr
+      doc: Add new address
+      attribute-set: addr-attrs
+      do:
+        request:
+          value: 20
+          attributes: &ifaddr-all
+            - ifa-family
+            - ifa-flags
+            - ifa-prefixlen
+            - ifa-scope
+            - ifa-index
+            - ifa-address
+            - ifa-label
+            - ifa-local
+            - ifa-cacheinfo
+    -
+      name: deladdr
+      doc: Remove address
+      attribute-set: addr-attrs
+      do:
+        request:
+          value: 21
+          attributes:
+            - ifa-family
+            - ifa-flags
+            - ifa-prefixlen
+            - ifa-scope
+            - ifa-index
+            - ifa-address
+            - ifa-local
+    -
+      name: getaddr
+      doc: Dump address information.
+      attribute-set: addr-attrs
+      dump:
+        request:
+          value: 22
+          attributes:
+            - index
+        reply:
+          value: 20
+          attributes: *ifaddr-all
+
+mcast-groups:
+  list:
+    -
+      name: rtnlgrp-ipv4-ifaddr
+      id: 5
+    -
+      name: rtnlgrp-ipv6-ifaddr
+      id: 9
Documentation/netlink/specs/rt_route.yaml new file +192 -0

192 lines not on this page · raw message

Signature · 1 line
2.41.0
maintainer
#10 lore raw
In reply to Donald Hunter
Quoted 5 lines · On Tue, 25 Jul 2023 17:22:02 +0100 Donald Hunter wrote:

This patchset adds support for netlink-raw families such as rtnetlink.

The first patch contains the schema definition. The second patch extends ynl to support netlink-raw The third patch adds rtnetlink addr and route message types

Haven't gotten to it today, unfortunately, but very exciting!

#11 lore raw
In reply to Donald Hunter
Quoted 31 lines · Donald Hunter

This patchset adds support for netlink-raw families such as rtnetlink.

The first patch contains the schema definition. The second patch extends ynl to support netlink-raw The third patch adds rtnetlink addr and route message types

The second patch depends on "tools: ynl-gen: fix parse multi-attr enum attribute":

https://patchwork.kernel.org/project/netdevbpf/list/?series=769229

The netlink-raw schema is very similar to genetlink-legacy and I thought about making the changes there and symlinking to it. On balance I thought that might be problematic for accurate schema validation.

rtnetlink doesn't seem to fit into unified or directional message enumeration models. It seems like an 'explicit' model would be useful, to require the schema author to specify the message ids directly. The patch supports commands and it supports notifications, but it's currently hard to support both simultaneously from the same netlink-raw spec. I plan to work on this in a future patchset.

There is not yet support for notifications because ynl currently doesn't support defining 'event' properties on a 'do' operation. I plan to work on this in a future patch.

The link message types are a work in progress that I plan to submit in a future patchset. Links contain different nested attributes dependent on the type of link. Decoding these will need some kind of attr-space selection based on the value of another attribute in the message.

Quoted 4 lines

Donald Hunter (3):
doc/netlink: Add a schema for netlink-raw families tools/net/ynl: Add support for netlink-raw families doc/netlink: Add specs for addr and route rtnetlink message types

Hi Donald,

unfortunately this series doesn't apply to current net-next. Please consider rebasing and reposting.

Signature · 1 line
pw-bot: changes-requested
#12 lore raw
Quoted 38 lines · Simon Horman
Quoted 31 lines · Donald Hunter

This patchset adds support for netlink-raw families such as rtnetlink.

The first patch contains the schema definition. The second patch extends ynl to support netlink-raw The third patch adds rtnetlink addr and route message types

The second patch depends on "tools: ynl-gen: fix parse multi-attr enum attribute":

https://patchwork.kernel.org/project/netdevbpf/list/?series=769229

The netlink-raw schema is very similar to genetlink-legacy and I thought about making the changes there and symlinking to it. On balance I thought that might be problematic for accurate schema validation.

rtnetlink doesn't seem to fit into unified or directional message enumeration models. It seems like an 'explicit' model would be useful, to require the schema author to specify the message ids directly. The patch supports commands and it supports notifications, but it's currently hard to support both simultaneously from the same netlink-raw spec. I plan to work on this in a future patchset.

There is not yet support for notifications because ynl currently doesn't support defining 'event' properties on a 'do' operation. I plan to work on this in a future patch.

The link message types are a work in progress that I plan to submit in a future patchset. Links contain different nested attributes dependent on the type of link. Decoding these will need some kind of attr-space selection based on the value of another attribute in the message.

Quoted 4 lines

Donald Hunter (3):
doc/netlink: Add a schema for netlink-raw families tools/net/ynl: Add support for netlink-raw families doc/netlink: Add specs for addr and route rtnetlink message types

Quoted 4 lines

Hi Donald,

unfortunately this series doesn't apply to current net-next. Please consider rebasing and reposting.

Hi Simon,

As I mentioned in the cover letter, it depends on:
"tools: ynl-gen: fix parse multi-attr enum attribute"
https://patchwork.kernel.org/project/netdevbpf/list/?series=769229

Should I wait for that and repost?

Thanks,
Donald.

#13 lore raw
Quoted 44 lines · Donald Hunter
Quoted 38 lines · Simon Horman
Quoted 31 lines · Donald Hunter

This patchset adds support for netlink-raw families such as rtnetlink.

The first patch contains the schema definition. The second patch extends ynl to support netlink-raw The third patch adds rtnetlink addr and route message types

The second patch depends on "tools: ynl-gen: fix parse multi-attr enum attribute":

https://patchwork.kernel.org/project/netdevbpf/list/?series=769229

The netlink-raw schema is very similar to genetlink-legacy and I thought about making the changes there and symlinking to it. On balance I thought that might be problematic for accurate schema validation.

rtnetlink doesn't seem to fit into unified or directional message enumeration models. It seems like an 'explicit' model would be useful, to require the schema author to specify the message ids directly. The patch supports commands and it supports notifications, but it's currently hard to support both simultaneously from the same netlink-raw spec. I plan to work on this in a future patchset.

There is not yet support for notifications because ynl currently doesn't support defining 'event' properties on a 'do' operation. I plan to work on this in a future patch.

The link message types are a work in progress that I plan to submit in a future patchset. Links contain different nested attributes dependent on the type of link. Decoding these will need some kind of attr-space selection based on the value of another attribute in the message.

Quoted 4 lines

Donald Hunter (3):
doc/netlink: Add a schema for netlink-raw families tools/net/ynl: Add support for netlink-raw families doc/netlink: Add specs for addr and route rtnetlink message types

Quoted 4 lines

Hi Donald,

unfortunately this series doesn't apply to current net-next. Please consider rebasing and reposting.

Quoted 7 lines

Hi Simon,

As I mentioned in the cover letter, it depends on:
"tools: ynl-gen: fix parse multi-attr enum attribute"
https://patchwork.kernel.org/project/netdevbpf/list/?series=769229

Should I wait for that and repost?

Sorry my bad. I guess this is fine as-is unless Jakub says otherwise.

Signature · 1 line
pw-bot: under-review
maintainer
#14 lore raw
Quoted 7 lines · On Wed, 26 Jul 2023 15:16:11 +0200 Simon Horman wrote:
Quoted 5 lines

As I mentioned in the cover letter, it depends on:
"tools: ynl-gen: fix parse multi-attr enum attribute"
https://patchwork.kernel.org/project/netdevbpf/list/?series=769229

Should I wait for that and repost?

Sorry my bad. I guess this is fine as-is unless Jakub says otherwise.

Right, just to be 100% clear, please don't repost, yet. I'll review it as is since it's of particular interest to me :)
Simon is right that we don't accept patches which have yet-to-be-applied dependencies, tho.

#15 lore raw
Quoted 6 lines · Jakub Kicinski
Quoted 4 lines · On Wed, 26 Jul 2023 15:16:11 +0200 Simon Horman wrote:
Quoted 4 lines

As I mentioned in the cover letter, it depends on:
"tools: ynl-gen: fix parse multi-attr enum attribute"
https://patchwork.kernel.org/project/netdevbpf/list/?series=769229

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Should I wait for that and repost?

Sorry my bad. I guess this is fine as-is unless Jakub says otherwise.

Right, just to be 100% clear, please don't repost, yet. I'll review it as is since it's of particular interest to me :)
Simon is right that we don't accept patches which have yet-to-be-applied dependencies, tho.

Ack, noted. Apologies for rushing it.