Netlink protocol specs
Subsystems (4)
Jakub Kicinski
maintainerv2 cover Netlink protocol specs
Hi!
I think the Netlink proto specs are far along enough to merge. Filling in all attribute types and quirks will be an ongoing effort but we have enough to cover FOU so it's somewhat complete.
I fully intend to continue polishing the code but at the same time I'd like to start helping others base their work on the specs (e.g. DPLL) and need to start working on some new families myself.
That's the progress / motivation for merging. The RFC [1] has more of a high level blurb, plus I created a lot of documentation, I'm not going to repeat it here. There was also the talk at LPC [2].
[1] https://lore.kernel.org/all/[email protected]/
[2] https://youtu.be/9QkXIQXkaQk?t=2562
v2:
- update docs
- change comment format in uAPI from // to /* */
- rename fou.c to make linker happy
Jakub Kicinski (7):
docs: add more netlink docs (incl. spec docs)
netlink: add schemas for YAML specs
net: add basic C code generators for Netlink
netlink: add a proto specification for FOU
net: fou: regenerate the uAPI from the spec
net: fou: rename the source for linking
net: fou: use policy and operation tables generated from the spec
Documentation/core-api/index.rst | 1 +
Documentation/core-api/netlink.rst | 99 +
Documentation/netlink/genetlink-c.yaml | 287 +++
Documentation/netlink/genetlink-legacy.yaml | 313 +++
Documentation/netlink/genetlink.yaml | 252 +++
Documentation/netlink/specs/fou.yaml | 128 ++
.../userspace-api/netlink/c-code-gen.rst | 104 +
.../netlink/genetlink-legacy.rst | 96 +
Documentation/userspace-api/netlink/index.rst | 5 +
Documentation/userspace-api/netlink/specs.rst | 410 ++++
MAINTAINERS | 3 +
include/uapi/linux/fou.h | 54 +-
net/ipv4/Makefile | 1 +
net/ipv4/fou-nl.c | 48 +
net/ipv4/fou-nl.h | 25 +
net/ipv4/{fou.c => fou_core.c} | 51 +-
tools/net/ynl/ynl-gen-c.py | 1998 +++++++++++++++++
tools/net/ynl/ynl-regen.sh | 30 +
18 files changed, 3835 insertions(+), 70 deletions(-)
create mode 100644 Documentation/core-api/netlink.rst
create mode 100644 Documentation/netlink/genetlink-c.yaml
create mode 100644 Documentation/netlink/genetlink-legacy.yaml
create mode 100644 Documentation/netlink/genetlink.yaml
create mode 100644 Documentation/netlink/specs/fou.yaml
create mode 100644 Documentation/userspace-api/netlink/c-code-gen.rst
create mode 100644 Documentation/userspace-api/netlink/genetlink-legacy.rst
create mode 100644 Documentation/userspace-api/netlink/specs.rst
create mode 100644 net/ipv4/fou-nl.c
create mode 100644 net/ipv4/fou-nl.h
rename net/ipv4/{fou.c => fou_core.c} (94%)
create mode 100755 tools/net/ynl/ynl-gen-c.py
create mode 100755 tools/net/ynl/ynl-regen.shSignature · 1 line
2.37.3
Jakub Kicinski
maintainerPATCH v2 2/7 netlink: add schemas for YAML specs
Add schemas for Netlink spec files. As described in the docs we have 4 "protocols" or compatibility levels, and each one comes with its own schema, but the more general / legacy schemas are superset of more modern ones: genetlink is the smallest followed by genetlink-c and genetlink-legacy. There is no schema for raw netlink, yet, I haven't found the time..
I don't know enough jsonschema to do inheritance or something but the repetition is not too bad. I hope.
--- Documentation/netlink/genetlink-c.yaml | 287 ++++++++++++++++++ Documentation/netlink/genetlink-legacy.yaml | 313 ++++++++++++++++++++ Documentation/netlink/genetlink.yaml | 252 ++++++++++++++++ 3 files changed, 852 insertions(+) create mode 100644 Documentation/netlink/genetlink-c.yaml create mode 100644 Documentation/netlink/genetlink-legacy.yaml create mode 100644 Documentation/netlink/genetlink.yaml
Documentation/netlink/genetlink-c.yaml new file +287 -0
@@ -0,0 +1,287 @@ +# SPDX-License-Identifier: GPL-2.0 +%YAML 1.2 +--- +$id: "http://kernel.org/schemas/netlink/genetlink-c.yaml#" +$schema: "http://kernel.org/meta-schemas/netlink/core.yaml#" + +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 + protocol: + description: Schema compatibility level. Default is "genetlink". + enum: [ genetlink, genetlink-c] + # Start genetlink-c + uapi-header: + description: Path to the uAPI header, default is linux/${family-name}.h + type: string + 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 + + definitions: + description: Array of type and constant definitions. + 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 ] + # 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: + type: string + # 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 + + 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 YAML. + type: string + # Strictly speaking 'name-prefix' and 'subset-of' should be mutually exclusive. + 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 + 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. + type: integer + 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 atttribute. + type: string + 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-len: + description: Min length for a binary attribute. + oneOf: + - type: string + pattern: ^[0-9A-Za-z_-]*( - 1)?$ + - type: integer + max-len: + description: Max length for a string or a binary attribute. + oneOf: + - type: string + pattern: ^[0-9A-Za-z_-]*( - 1)?$ + - type: integer + sub-type: *attr-type + 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. + "notify-split" has the notifications and request-response types in + different enums. + enum: [ unified, directional, notify-split ] + 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 + 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. + type: integer + 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 ] + 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 + reply: *subop-attr-list + 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
Documentation/netlink/genetlink-legacy.yaml new file +313 -0
313 lines not on this page · raw message
Documentation/netlink/genetlink.yaml new file +252 -0
252 lines not on this page · raw message
Signature · 1 line
2.37.3
Jakub Kicinski
maintainerPATCH v2 4/7 netlink: add a proto specification for FOU
In reply to Jakub KicinskiFOU has a reasonably modern Genetlink family. Add a spec.
--- Documentation/netlink/specs/fou.yaml | 128 +++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 Documentation/netlink/specs/fou.yaml
Documentation/netlink/specs/fou.yaml new file +128 -0
@@ -0,0 +1,128 @@ +name: fou + +protocol: genetlink-legacy + +doc: | + Foo-over-UDP. + +c-family-name: fou-genl-name +c-version-name: fou-genl-version +max-by-define: true +kernel-policy: global + +definitions: + - + type: enum + name: encap_type + name-prefix: fou-encap- + enum-name: + entries: [ unspec, direct, gue ] + +attribute-sets: + - + name: fou + name-prefix: fou-attr- + attributes: + - + name: unspec + type: unused + - + name: port + type: u16 + byte-order: big-endian + - + name: af + type: u8 + - + name: ipproto + type: u8 + - + name: type + type: u8 + - + name: remcsum_nopartial + type: flag + - + name: local_v4 + type: u32 + - + name: local_v6 + type: binary + checks: + min-len: 16 + - + name: peer_v4 + type: u32 + - + name: peer_v6 + type: binary + checks: + min-len: 16 + - + name: peer_port + type: u16 + byte-order: big-endian + - + name: ifindex + type: s32 + +operations: + list: + - + name: unspec + doc: unused + + - + name: add + doc: Add port. + attribute-set: fou + + dont-validate: [ strict, dump ] + flags: [ admin-perm ] + + do: + request: &all_attrs + attributes: + - port + - ipproto + - type + - remcsum_nopartial + - local_v4 + - peer_v4 + - local_v6 + - peer_v6 + - peer_port + - ifindex + + - + name: del + doc: Delete port. + attribute-set: fou + + dont-validate: [ strict, dump ] + flags: [ admin-perm ] + + do: + request: &select_attrs + attributes: + - af + - ifindex + - port + - peer_port + - local_v4 + - peer_v4 + - local_v6 + - peer_v6 + + - + name: get + doc: Get tunnel info. + attribute-set: fou + dont-validate: [ strict, dump ] + + do: + request: *select_attrs + reply: *all_attrs + + dump: + reply: *all_attrs
Signature · 1 line
2.37.3
Jakub Kicinski
maintainerPATCH v2 1/7 docs: add more netlink docs (incl. spec docs)
In reply to Jakub KicinskiAdd documentation about the upcoming Netlink protocol specs.
--
v2:
- plenty -> plenty of
- type: unspec -> type: unused
- ECHO edits from Guillaume
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/netlink.rst | 99 +++++ .../userspace-api/netlink/c-code-gen.rst | 104 +++++ .../netlink/genetlink-legacy.rst | 96 ++++ Documentation/userspace-api/netlink/index.rst | 5 + Documentation/userspace-api/netlink/specs.rst | 410 ++++++++++++++++++ MAINTAINERS | 2 + 7 files changed, 717 insertions(+) create mode 100644 Documentation/core-api/netlink.rst create mode 100644 Documentation/userspace-api/netlink/c-code-gen.rst create mode 100644 Documentation/userspace-api/netlink/genetlink-legacy.rst create mode 100644 Documentation/userspace-api/netlink/specs.rst
Documentation/core-api/index.rst +1 -0
@@ -122,6 +122,7 @@ Documents that don't fit elsewhere or which have yet to be categorized. :maxdepth: 1 librs + netlink .. only:: subproject and html
Documentation/core-api/netlink.rst new file +99 -0
99 lines not on this page · raw message
Documentation/userspace-api/netlink/c-code-gen.rst new file +104 -0
104 lines not on this page · raw message
Documentation/userspace-api/netlink/genetlink-legacy.rst new file +96 -0
96 lines not on this page · raw message
Documentation/userspace-api/netlink/index.rst +5 -0
@@ -10,3 +10,8 @@ Netlink documentation for users. :maxdepth: 2 intro + specs + c-code-gen + genetlink-legacy + +See also :ref:`Documentation/core-api/netlink.rst <kernel_netlink>`.
Documentation/userspace-api/netlink/specs.rst new file +410 -0
410 lines not on this page · raw message
MAINTAINERS +2 -0
@@ -14188,8 +14188,10 @@ Q: https://patchwork.kernel.org/project/netdevbpf/list/ B: mailto:[email protected] T: git git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git T: git git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git +F: Documentation/core-api/netlink.rst F: Documentation/networking/ F: Documentation/process/maintainer-netdev.rst +F: Documentation/userspace-api/netlink/ F: include/linux/in.h F: include/linux/net.h F: include/linux/netdevice.h
Signature · 1 line
2.37.3
Jakub Kicinski
maintainerPATCH v2 5/7 net: fou: regenerate the uAPI from the spec
In reply to Jakub KicinskiRegenerate the FOU uAPI header from the YAML spec.
The flags now come before attributes which use them, and the comments for type disappear (coders should look at the spec instead).
--- include/uapi/linux/fou.h | 54 +++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 28 deletions(-)
include/uapi/linux/fou.h +26 -28
@@ -1,32 +1,37 @@/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ -/* fou.h - FOU Interface */ +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/fou.yaml */ +/* YNL-GEN uapi header */ #ifndef _UAPI_LINUX_FOU_H#define _UAPI_LINUX_FOU_H -/* NETLINK_GENERIC related info -*/ #define FOU_GENL_NAME "fou" -#define FOU_GENL_VERSION 0x1 +#define FOU_GENL_VERSION 1enum { -FOU_ATTR_UNSPEC , -FOU_ATTR_PORT ,/* u16 */ -FOU_ATTR_AF ,/* u8 */ -FOU_ATTR_IPPROTO ,/* u8 */ -FOU_ATTR_TYPE ,/* u8 */ -FOU_ATTR_REMCSUM_NOPARTIAL ,/* flag */ -FOU_ATTR_LOCAL_V4 ,/* u32 */ -FOU_ATTR_LOCAL_V6 ,/* in6_addr */ -FOU_ATTR_PEER_V4 ,/* u32 */ -FOU_ATTR_PEER_V6 ,/* in6_addr */ -FOU_ATTR_PEER_PORT ,/* u16 */ -FOU_ATTR_IFINDEX ,/* s32 */ - - __FOU_ATTR_MAX, +FOU_ENCAP_UNSPEC , +FOU_ENCAP_DIRECT , +FOU_ENCAP_GUE , }; -#define FOU_ATTR_MAX (__FOU_ATTR_MAX - 1) +enum { +FOU_ATTR_UNSPEC , +FOU_ATTR_PORT , +FOU_ATTR_AF , +FOU_ATTR_IPPROTO , +FOU_ATTR_TYPE , +FOU_ATTR_REMCSUM_NOPARTIAL , +FOU_ATTR_LOCAL_V4 , +FOU_ATTR_LOCAL_V6 , +FOU_ATTR_PEER_V4 , +FOU_ATTR_PEER_V6 , +FOU_ATTR_PEER_PORT , +FOU_ATTR_IFINDEX , + + __FOU_ATTR_MAX +}; +#define FOU_ATTR_MAX (__FOU_ATTR_MAX - 1)enum { FOU_CMD_UNSPEC,
@@ -34,15 +39,8 @@ enum {FOU_CMD_DEL ,FOU_CMD_GET , - __FOU_CMD_MAX, + __FOU_CMD_MAX }; - -enum { -FOU_ENCAP_UNSPEC , -FOU_ENCAP_DIRECT , -FOU_ENCAP_GUE , -}; - -#define FOU_CMD_MAX (__FOU_CMD_MAX - 1) +#define FOU_CMD_MAX (__FOU_CMD_MAX - 1)#endif /* _UAPI_LINUX_FOU_H */
Signature · 1 line
2.37.3
Jakub Kicinski
maintainerPATCH v2 6/7 net: fou: rename the source for linking
In reply to Jakub KicinskiWe'll need to link two objects together to form the fou module. This means the source can't be called fou, the build system expects fou.o to be the combined object.
---
net/ipv4/Makefile | 1 +
net/ipv4/{fou.c => fou_core.c} | 0
2 files changed, 1 insertion(+)
rename net/ipv4/{fou.c => fou_core.c} (100%)net/ipv4/Makefile +1 -0
@@ -26,6 +26,7 @@ obj-$(CONFIG_IP_MROUTE) += ipmr.o obj-$(CONFIG_IP_MROUTE_COMMON) += ipmr_base.o obj-$(CONFIG_NET_IPIP) += ipip.o gre-y := gre_demux.o +fou-y := fou_core.o obj-$(CONFIG_NET_FOU) += fou.o obj-$(CONFIG_NET_IPGRE_DEMUX) += gre.o obj-$(CONFIG_NET_IPGRE) += ip_gre.o
net/ipv4/fou_core.c renamed from net/ipv4/fou.c +0 -0
Signature · 1 line
2.37.3
Jakub Kicinski
maintainerPATCH v2 3/7 net: add basic C code generators for Netlink
In reply to Jakub KicinskiCode generators to turn Netlink specs into C code. I'm definitely not proud of it.
The main generator is in Python, there's a bash script to regen all code-gen'ed files in tree after making spec changes.
--
v2: - use /* */ comments instead of //
--- MAINTAINERS | 1 + tools/net/ynl/ynl-gen-c.py | 1998 ++++++++++++++++++++++++++++++++++++ tools/net/ynl/ynl-regen.sh | 30 + 3 files changed, 2029 insertions(+) create mode 100755 tools/net/ynl/ynl-gen-c.py create mode 100755 tools/net/ynl/ynl-regen.sh
MAINTAINERS +1 -0
@@ -14203,6 +14203,7 @@ F: include/uapi/linux/netdevice.h F: lib/net_utils.c F: lib/random32.c F: net/ +F: tools/net/ F: tools/testing/selftests/net/ NETWORKING [IPSEC]
tools/net/ynl/ynl-gen-c.py new file +1998 -0
1,998 lines not on this page · raw message
tools/net/ynl/ynl-regen.sh new file +30 -0
@@ -0,0 +1,30 @@ +#!/bin/bash +# SPDX-License-Identifier: BSD-3-Clause + +TOOL=$(dirname $(realpath $0))/ynl-gen-c.py + +force= + +while [ ! -z "$1" ]; do + case "$1" in + -f ) force=yes; shift ;; + * ) echo "Unrecognized option '$1'"; exit 1 ;; + esac +done + +KDIR=$(dirname $(dirname $(dirname $(dirname $(realpath $0))))) + +files=$(git grep --files-with-matches '^/\* YNL-GEN \(kernel\|uapi\)') +for f in $files; do + # params: 0 1 2 3 + # $YAML YNL-GEN kernel $mode + params=( $(git grep -B1 -h '/\* YNL-GEN' $f | sed 's@/\*\(.*\)\*/@\1@') ) + + if [ $f -nt ${params[0]} -a -z "$force" ]; then + echo -e "\tSKIP $f" + continue + fi + + echo -e "\tGEN ${params[2]}\t$f" + $TOOL --mode ${params[2]} --${params[3]} --spec $KDIR/${params[0]} > $f +done
Signature · 1 line
2.37.3
Jakub Kicinski
maintainerPATCH v2 7/7 net: fou: use policy and operation tables generated from the spec
In reply to Jakub KicinskiGenerate and plug in the spec-based tables.
A little bit of renaming is needed in the FOU code.
--- net/ipv4/Makefile | 2 +- net/ipv4/fou-nl.c | 48 ++++++++++++++++++++++++++++++++++++++++++ net/ipv4/fou-nl.h | 25 ++++++++++++++++++++++ net/ipv4/fou_core.c | 51 ++++++++------------------------------------- 4 files changed, 83 insertions(+), 43 deletions(-) create mode 100644 net/ipv4/fou-nl.c create mode 100644 net/ipv4/fou-nl.h
net/ipv4/Makefile +1 -1
@@ -26,7 +26,7 @@ obj-$(CONFIG_IP_MROUTE) += ipmr.o obj-$(CONFIG_IP_MROUTE_COMMON) += ipmr_base.o obj-$(CONFIG_NET_IPIP) += ipip.o gre-y := gre_demux.o -fou-y := fou_core.o +fou-y := fou_core.o fou-nl.o obj-$(CONFIG_NET_FOU) += fou.o obj-$(CONFIG_NET_IPGRE_DEMUX) += gre.o obj-$(CONFIG_NET_IPGRE) += ip_gre.o
net/ipv4/fou-nl.c new file +48 -0
48 lines not on this page · raw message
net/ipv4/fou-nl.h new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/fou.yaml */ +/* YNL-GEN kernel header */ + +#ifndef _LINUX_FOU_GEN_H +#define _LINUX_FOU_GEN_H + +#include <net/netlink.h> +#include <net/genetlink.h> + +#include <linux/fou.h> + +// Global operation policy for fou +extern const struct nla_policy fou_policy[FOU_ATTR_IFINDEX +1 ]; + +// Ops table for fou +extern const struct genl_small_ops fou_ops[3 ]; + +int fou_add_doit (struct sk_buff *skb,struct genl_info *info); +int fou_del_doit (struct sk_buff *skb,struct genl_info *info); +int fou_get_doit (struct sk_buff *skb,struct genl_info *info); +int fou_get_dumpit (struct sk_buff *skb,struct netlink_callback *cb); + +#endif /* _LINUX_FOU_GEN_H */
net/ipv4/fou_core.c +9 -42
100 lines not on this page · raw message
Signature · 1 line
2.37.3
Re: [PATCH net-next v2 3/7] net: add basic C code generators for Netlink
In reply to Jakub KicinskiQuoted 10 lines · Jakub Kicinski
Code generators to turn Netlink specs into C code. I'm definitely not proud of it.
The main generator is in Python, there's a bash script to regen all code-gen'ed files in tree after making spec changes.
Signed-off-by Jakub Kicinski <[email protected]>--
v2: - use /* */ comments instead of //
Probably not a very interesting feedback, but there are still many comments generated in the // style.
For example in this block:
Quoted 35 lines
tools/net/ynl/ynl-gen-c.py new file +35
-0+ if args.mode == "kernel": + if args.header: + if parsed.kernel_policy == 'global': + cw.p(f"// Global operation policy for {parsed.name}") + + struct = Struct(parsed, parsed.global_policy_set, type_list=parsed.global_policy) + print_req_policy_fwd(cw, struct) + cw.nl() + + for op_name, op in parsed.ops.items(): + if parsed.kernel_policy == 'per-op' and 'do' in op and 'event' not in op: + cw.p(f"// {op.enum_name} - do") + ri = RenderInfo(cw, parsed, args.mode, op, op_name, "do") + print_req_policy_fwd(cw, ri.struct['request'], op=op) + cw.nl() + + print_kernel_op_table_fwd(parsed, cw) + else: + if parsed.kernel_policy == 'global': + cw.p(f"// Global operation policy for {parsed.name}") + + struct = Struct(parsed, parsed.global_policy_set, type_list=parsed.global_policy) + print_req_policy(cw, struct) + cw.nl() + + for op_name, op in parsed.ops.items(): + if parsed.kernel_policy == 'per-op': + for op_mode in {'do', 'dump'}: + if op_mode in op and 'request' in op[op_mode]: + cw.p(f"// {op.enum_name} - {op_mode}") + ri = RenderInfo(cw, parsed, args.mode, op, op_name, op_mode) + print_req_policy(cw, ri.struct['request'], op=op) + cw.nl() + + print_kernel_op_table(parsed, cw)
Jakub Kicinski
maintainerRe: [PATCH net-next v2 3/7] net: add basic C code generators for Netlink
Quoted 4 lines · On Thu, 6 Oct 2022 14:51:09 +0200 Guillaume Nault wrote:
Quoted 1 line
v2: - use /* */ comments instead of //
Probably not a very interesting feedback, but there are still many comments generated in the // style.
It's slightly unclear to me what our policy on comments is now. I can fix all up - the motivation for the change in v2 was that in uAPI apparently its completely forbidden to use anything that's not ANSI C :S
Gotta keep that compatibility with the all important Borland compiler or something?
Re: [PATCH net-next v2 3/7] net: add basic C code generators for Netlink
Quoted 10 lines · Jakub Kicinski
Quoted 4 lines · On Thu, 6 Oct 2022 14:51:09 +0200 Guillaume Nault wrote:
Quoted 1 line
v2: - use /* */ comments instead of //
Probably not a very interesting feedback, but there are still many comments generated in the // style.
It's slightly unclear to me what our policy on comments is now. I can fix all up - the motivation for the change in v2 was that in uAPI apparently its completely forbidden to use anything that's not ANSI C :S
I didn't realise the v2 comment was for uapi headers only. So I was surprised to see // comments in the generated files.
Quoted 2 lines
Gotta keep that compatibility with the all important Borland compiler or something?
Personnaly, I like the /* */ style, but I don't think my personnal taste should influence this patch set. I genuinely thought you wanted to convert all comments, hence my feedback. Feel free to ignore it :).
Re: [PATCH net-next v2 1/7] docs: add more netlink docs (incl. spec docs)
In reply to Jakub KicinskiQuoted 12 lines · Jakub Kicinski
+==============================
Documentation/userspace-api/netlink/c-code-gen.rst new file +11
-0+Netlink spec C code generation +============================== + +This document describes how Netlink specifications are used to render +C code (uAPI, policies etc.). It also defines the additional properties +allowed in older families by the ``genetlink-c`` protocol level, +to control the naming. + +For brevity this document refers to ``name`` properties of various +objects by the object type. For example ``$attr`` is the value +of ``name`` in an attribute, and ``$family`` is the name of the
Documentation/userspace-api/netlink/c-code-gen.rst line 24
Quoted 10 lines
Documentation/userspace-api/netlink/c-code-gen.rst new file +10
-0+family (the global ``name`` property). + +The upper case is used to denote literal values, e.g. ``$family-CMD`` +means the concatenation of ``$family``, a dash character, and the literal +``CMD``. + +The names of ``#defines`` and enum values are always converted to upper case, +and with dashes (``-``) replaced by underscores (``_``). + +If the constructured name is a C keyword, an extra underscore is
s/constructured/constructed/
[...]
Documentation/userspace-api/netlink/specs.rst line 134
Quoted 6 lines
Documentation/userspace-api/netlink/specs.rst new file +6
-0+header +~~~~~~ + +For C-compatible languages, header which already defines this value. +In case the definition is shared by multiple families (e.g. ``IFNAMSIZ``) +code generators for C-compabile languages may prefer to add an appropriate
s/C-compabile/C-compatible/