Netlink 规范支持原始 Netlink 系列¶
本文档描述了原始 Netlink 系列(如 NETLINK_ROUTE
)所需的附加属性,这些系列使用 netlink-raw
协议规范。
规范¶
netlink-raw 模式通过用于指定原始 netlink 系列使用的协议号和多播 ID 的属性扩展了 genetlink-legacy 模式。更多信息请参见 经典 Netlink。原始 netlink 系列还使用类型特定的子消息。
全局变量¶
protonum¶
protonum
属性用于指定打开 netlink 套接字时使用的协议号。
# 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
多播组属性¶
value¶
value
属性用于指定用于多播组注册的组 ID。
mcast-groups:
list:
-
name: rtnlgrp-ipv4-ifaddr
value: 5
-
name: rtnlgrp-ipv6-ifaddr
value: 9
-
name: rtnlgrp-mctp-ifaddr
value: 34
子消息¶
一些原始 netlink 系列,例如 rt-link 和 tc,使用属性嵌套作为抽象来携带模块特定信息。
概念上它看起来如下:
[OUTER NEST OR MESSAGE LEVEL]
[GENERIC ATTR 1]
[GENERIC ATTR 2]
[GENERIC ATTR 3]
[GENERIC ATTR - wrapper]
[MODULE SPECIFIC ATTR 1]
[MODULE SPECIFIC ATTR 2]
外层 GENERIC ATTRs
在核心(或 rt_link 或核心 TC)中定义,而特定驱动、TC 分类器、qdisc 等可以在 GENERIC ATTR - wrapper
中携带它们自己的信息。尽管上面的例子显示了属性嵌套在包装器中,但模块通常拥有完全的自由来定义嵌套的格式。实际上,包装器属性的有效载荷与 netlink 消息具有非常相似的特性。它可能包含固定头部/结构、netlink 属性,或两者皆有。由于这些共享特性,我们将包装器属性的有效载荷称为子消息。
子消息属性使用另一个属性的值作为选择器键,以选择正确的子消息格式。例如,如果已解码以下属性:
{ "kind": "gre" }
并且我们遇到以下属性规范:
-
name: data
type: sub-message
sub-message: linkinfo-data-msg
selector: kind
那么我们查找名为 linkinfo-data-msg
的子消息定义,并使用 kind
属性的值,即 gre
,作为键来选择子消息的正确格式:
sub-messages:
name: linkinfo-data-msg
formats:
-
value: bridge
attribute-set: linkinfo-bridge-attrs
-
value: gre
attribute-set: linkinfo-gre-attrs
-
value: geneve
attribute-set: linkinfo-geneve-attrs
这将把属性值解码为子消息,其中名为 linkinfo-gre-attrs
的属性集作为属性空间。
子消息可以有一个可选的 fixed-header
,后面跟着零个或多个来自 attribute-set
的属性。例如,以下 tc-options-msg
子消息定义了混合使用 fixed-header
、attribute-set
或两者结合的消息格式:
sub-messages:
-
name: tc-options-msg
formats:
-
value: bfifo
fixed-header: tc-fifo-qopt
-
value: cake
attribute-set: tc-cake-attrs
-
value: netem
fixed-header: tc-netem-qopt
attribute-set: tc-netem-attrs
请注意,选择器属性必须在任何依赖它的子消息属性之前出现在 netlink 消息中。
如果一个属性(例如 kind
)在多个嵌套级别上定义,那么子消息选择器将使用“最接近”选择器的值进行解析。例如,如果相同的属性名在一个嵌套的 attribute-set
中与子消息选择器一起定义,并且也在顶层 attribute-set
中定义,那么选择器将使用“最接近”选择器的值进行解析。如果该值在消息中不存在于规范定义的同一级别,则这是一个错误。
嵌套结构定义¶
许多原始 netlink 系列(如 tc)使用嵌套结构定义。netlink-raw
模式可以通过 struct
属性在结构定义中嵌入结构。例如,以下结构定义为 struct tc-tbf-qopt
的 rate
和 peakrate
成员都嵌入了 tc-ratespec
结构定义。
-
name: tc-tbf-qopt
type: struct
members:
-
name: rate
type: binary
struct: tc-ratespec
-
name: peakrate
type: binary
struct: tc-ratespec
-
name: limit
type: u32
-
name: buffer
type: u32
-
name: mtu
type: u32