Commit 5447b080 authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'tools-net-ynl-add-support-for-netlink-raw-families'

Donald Hunter says:

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

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

Patch 1 fixes a typo in existing schemas
Patch 2 contains the schema definition
Patches 3 & 4 update the schema documentation
Patches 5 - 9 extends ynl
Patches 10 - 12 add several netlink-raw specs

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 force the schema author to specify the message ids directly.

There is not yet support for notifications because ynl currently doesn't
support defining 'event' properties on a 'do' operation. The message ids
are shared so ops need to be both sync and async. I plan to look at this
in a future patch.

The link and route messages contain different nested attributes
dependent on the type of link or route. Decoding these will need some
kind of attr-space selection that uses the value of another attribute as
the selector key. These nested attributes have been left with type
'binary' for now.
====================

Link: https://lore.kernel.org/r/20230825122756.7603-1-donald.hunter@gmail.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents 75d6d8b5 023289b4
......@@ -67,10 +67,11 @@ Globals
kernel-policy
~~~~~~~~~~~~~
Defines if the kernel validation policy is per operation (``per-op``)
or for the entire family (``global``). New families should use ``per-op``
(default) to be able to narrow down the attributes accepted by a specific
command.
Defines whether the kernel validation policy is ``global`` i.e. the same for all
operations of the family, defined for each operation individually - ``per-op``,
or separately for each operation and operation type (do vs dump) - ``split``.
New families should use ``per-op`` (default) to be able to narrow down the
attributes accepted by a specific command.
checks
------
......
......@@ -41,7 +41,7 @@ properties:
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.
description: Name of the define for the version 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.
......
......@@ -41,7 +41,7 @@ properties:
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.
description: Name of the define for the version 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.
......
This diff is collapsed.
# 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:
- ifa-index
reply:
value: 20
attributes: *ifaddr-all
mcast-groups:
list:
-
name: rtnlgrp-ipv4-ifaddr
value: 5
-
name: rtnlgrp-ipv6-ifaddr
value: 9
This diff is collapsed.
# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
name: rt-route
protocol: netlink-raw
protonum: 0
doc:
Route configuration over rtnetlink.
definitions:
-
name: rtm-type
name-prefix: rtn-
type: enum
entries:
- unspec
- unicast
- local
- broadcast
- anycast
- multicast
- blackhole
- unreachable
- prohibit
- throw
- nat
- xresolve
-
name: rtmsg
type: struct
members:
-
name: rtm-family
type: u8
-
name: rtm-dst-len
type: u8
-
name: rtm-src-len
type: u8
-
name: rtm-tos
type: u8
-
name: rtm-table
type: u8
-
name: rtm-protocol
type: u8
-
name: rtm-scope
type: u8
-
name: rtm-type
type: u8
enum: rtm-type
-
name: rtm-flags
type: u32
-
name: rta-cacheinfo
type: struct
members:
-
name: rta-clntref
type: u32
-
name: rta-lastuse
type: u32
-
name: rta-expires
type: u32
-
name: rta-error
type: u32
-
name: rta-used
type: u32
attribute-sets:
-
name: route-attrs
attributes:
-
name: rta-dst
type: binary
display-hint: ipv4
-
name: rta-src
type: binary
display-hint: ipv4
-
name: rta-iif
type: u32
-
name: rta-oif
type: u32
-
name: rta-gateway
type: binary
display-hint: ipv4
-
name: rta-priority
type: u32
-
name: rta-prefsrc
type: binary
display-hint: ipv4
-
name: rta-metrics
type: nest
nested-attributes: rta-metrics
-
name: rta-multipath
type: binary
-
name: rta-protoinfo # not used
type: binary
-
name: rta-flow
type: u32
-
name: rta-cacheinfo
type: binary
struct: rta-cacheinfo
-
name: rta-session # not used
type: binary
-
name: rta-mp-algo # not used
type: binary
-
name: rta-table
type: u32
-
name: rta-mark
type: u32
-
name: rta-mfc-stats
type: binary
-
name: rta-via
type: binary
-
name: rta-newdst
type: binary
-
name: rta-pref
type: u8
-
name: rta-encap-type
type: u16
-
name: rta-encap
type: binary # tunnel specific nest
-
name: rta-expires
type: u32
-
name: rta-pad
type: binary
-
name: rta-uid
type: u32
-
name: rta-ttl-propagate
type: u8
-
name: rta-ip-proto
type: u8
-
name: rta-sport
type: u16
-
name: rta-dport
type: u16
-
name: rta-nh-id
type: u32
-
name: rta-metrics
attributes:
-
name: rtax-unspec
type: unused
value: 0
-
name: rtax-lock
type: u32
-
name: rtax-mtu
type: u32
-
name: rtax-window
type: u32
-
name: rtax-rtt
type: u32
-
name: rtax-rttvar
type: u32
-
name: rtax-ssthresh
type: u32
-
name: rtax-cwnd
type: u32
-
name: rtax-advmss
type: u32
-
name: rtax-reordering
type: u32
-
name: rtax-hoplimit
type: u32
-
name: rtax-initcwnd
type: u32
-
name: rtax-features
type: u32
-
name: rtax-rto-min
type: u32
-
name: rtax-initrwnd
type: u32
-
name: rtax-quickack
type: u32
-
name: rtax-cc-algo
type: string
-
name: rtax-fastopen-no-cookie
type: u32
operations:
enum-model: directional
list:
-
name: getroute
doc: Dump route information.
attribute-set: route-attrs
fixed-header: rtmsg
do:
request:
value: 26
attributes:
- rtm-family
- rta-src
- rtm-src-len
- rta-dst
- rtm-dst-len
- rta-iif
- rta-oif
- rta-ip-proto
- rta-sport
- rta-dport
- rta-mark
- rta-uid
reply:
value: 24
attributes: &all-route-attrs
- rtm-family
- rtm-dst-len
- rtm-src-len
- rtm-tos
- rtm-table
- rtm-protocol
- rtm-scope
- rtm-type
- rtm-flags
- rta-dst
- rta-src
- rta-iif
- rta-oif
- rta-gateway
- rta-priority
- rta-prefsrc
- rta-metrics
- rta-multipath
- rta-flow
- rta-cacheinfo
- rta-table
- rta-mark
- rta-mfc-stats
- rta-via
- rta-newdst
- rta-pref
- rta-encap-type
- rta-encap
- rta-expires
- rta-pad
- rta-uid
- rta-ttl-propagate
- rta-ip-proto
- rta-sport
- rta-dport
- rta-nh-id
dump:
request:
value: 26
attributes:
- rtm-family
reply:
value: 24
attributes: *all-route-attrs
-
name: newroute
doc: Create a new route
attribute-set: route-attrs
fixed-header: rtmsg
do:
request:
value: 24
attributes: *all-route-attrs
-
name: delroute
doc: Delete an existing route
attribute-set: route-attrs
fixed-header: rtmsg
do:
request:
value: 25
attributes: *all-route-attrs
......@@ -8,11 +8,8 @@ This document describes the many additional quirks and properties
required to describe older Generic Netlink families which form
the ``genetlink-legacy`` protocol level.
The spec is a work in progress, some of the quirks are just documented
for future reference.
Specification (defined)
=======================
Specification
=============
Attribute type nests
--------------------
......@@ -156,16 +153,27 @@ it will be allocated 3 for the request (``a`` is the previous operation
with a request section and the value of 2) and 8 for response (``c`` is
the previous operation in the "from-kernel" direction).
Other quirks (todo)
===================
Other quirks
============
Structures
----------
Legacy families can define C structures both to be used as the contents of
an attribute and as a fixed message header. Structures are defined in
``definitions`` and referenced in operations or attributes. Note that
structures defined in YAML are implicitly packed according to C
``definitions`` and referenced in operations or attributes.
members
~~~~~~~
- ``name`` - The attribute name of the struct member
- ``type`` - One of the scalar types ``u8``, ``u16``, ``u32``, ``u64``, ``s8``,
``s16``, ``s32``, ``s64``, ``string`` or ``binary``.
- ``byte-order`` - ``big-endian`` or ``little-endian``
- ``doc``, ``enum``, ``enum-as-flags``, ``display-hint`` - Same as for
:ref:`attribute definitions <attribute_properties>`
Note that structures defined in YAML are implicitly packed according to C
conventions. For example, the following struct is 4 bytes, not 6 bytes:
.. code-block:: c
......
......@@ -14,5 +14,6 @@ Netlink documentation for users.
specs
c-code-gen
genetlink-legacy
netlink-raw
See also :ref:`Documentation/core-api/netlink.rst <kernel_netlink>`.
.. SPDX-License-Identifier: BSD-3-Clause
======================================================
Netlink specification support for raw Netlink families
======================================================
This document describes the additional properties required by raw Netlink
families such as ``NETLINK_ROUTE`` which use the ``netlink-raw`` protocol
specification.
Specification
=============
The netlink-raw schema extends the :doc:`genetlink-legacy <genetlink-legacy>`
schema with properties that are needed to specify the protocol numbers and
multicast IDs used by raw netlink families. See :ref:`classic_netlink` for more
information.
Globals
-------
protonum
~~~~~~~~
The ``protonum`` property is used to specify the protocol number to use when
opening a netlink socket.
.. code-block:: yaml
# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
name: rt-addr
protocol: netlink-raw
protonum: 0 # part of the NETLINK_ROUTE protocol
Multicast group properties
--------------------------
value
~~~~~
The ``value`` property is used to specify the group ID to use for multicast
group registration.
.. code-block:: yaml
mcast-groups:
list:
-
name: rtnlgrp-ipv4-ifaddr
value: 5
-
name: rtnlgrp-ipv6-ifaddr
value: 9
-
name: rtnlgrp-mctp-ifaddr
value: 34
......@@ -68,6 +68,10 @@ The following sections describe the properties of the most modern ``genetlink``
schema. See the documentation of :doc:`genetlink-c <c-code-gen>`
for information on how C names are derived from name properties.
See also :ref:`Documentation/core-api/netlink.rst <kernel_netlink>` for
information on the Netlink specification properties that are only relevant to
the kernel space and not part of the user space API.
genetlink
=========
......@@ -180,6 +184,8 @@ attributes
List of attributes in the set.
.. _attribute_properties:
Attribute properties
--------------------
......@@ -264,6 +270,13 @@ a C array of u32 values can be specified with ``type: binary`` and
``sub-type: u32``. Binary types and legacy array formats are described in
more detail in :doc:`genetlink-legacy`.
display-hint
~~~~~~~~~~~~
Optional format indicator that is intended only for choosing the right
formatting mechanism when displaying values of this type. Currently supported
hints are ``hex``, ``mac``, ``fddi``, ``ipv4``, ``ipv6`` and ``uuid``.
operations
----------
......
......@@ -6,7 +6,7 @@ import json
import pprint
import time
from lib import YnlFamily
from lib import YnlFamily, Netlink
def main():
......@@ -19,6 +19,14 @@ def main():
parser.add_argument('--dump', dest='dump', type=str)
parser.add_argument('--sleep', dest='sleep', type=int)
parser.add_argument('--subscribe', dest='ntf', type=str)
parser.add_argument('--replace', dest='flags', action='append_const',
const=Netlink.NLM_F_REPLACE)
parser.add_argument('--excl', dest='flags', action='append_const',
const=Netlink.NLM_F_EXCL)
parser.add_argument('--create', dest='flags', action='append_const',
const=Netlink.NLM_F_CREATE)
parser.add_argument('--append', dest='flags', action='append_const',
const=Netlink.NLM_F_APPEND)
args = parser.parse_args()
if args.no_schema:
......@@ -37,7 +45,7 @@ def main():
time.sleep(args.sleep)
if args.do:
reply = ynl.do(args.do, attrs)
reply = ynl.do(args.do, attrs, args.flags)
pprint.PrettyPrinter().pprint(reply)
if args.dump:
reply = ynl.dump(args.dump, attrs)
......
......@@ -2,7 +2,7 @@
from .nlspec import SpecAttr, SpecAttrSet, SpecEnumEntry, SpecEnumSet, \
SpecFamily, SpecOperation
from .ynl import YnlFamily
from .ynl import YnlFamily, Netlink
__all__ = ["SpecAttr", "SpecAttrSet", "SpecEnumEntry", "SpecEnumSet",
"SpecFamily", "SpecOperation", "YnlFamily"]
"SpecFamily", "SpecOperation", "YnlFamily", "Netlink"]
......@@ -322,6 +322,26 @@ class SpecOperation(SpecElement):
self.attr_set = self.family.attr_sets[attr_set_name]
class SpecMcastGroup(SpecElement):
"""Netlink Multicast Group
Information about a multicast group.
Value is only used for classic netlink families that use the
netlink-raw schema. Genetlink families use dynamic ID allocation
where the ids of multicast groups get resolved at runtime. Value
will be None for genetlink families.
Attributes:
name name of the mulitcast group
value integer id of this multicast group for netlink-raw or None
yaml raw spec as loaded from the spec file
"""
def __init__(self, family, yaml):
super().__init__(family, yaml)
self.value = self.yaml.get('value')
class SpecFamily(SpecElement):
""" Netlink Family Spec class.
......@@ -343,6 +363,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 +405,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 +438,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 +537,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
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment