Skip to content

Commit f0e3be5

Browse files
committed
bpf: expose information about supported xdp metadata kfunc
JIRA: https://issues.redhat.com/browse/RHEL-31945 commit a9c2a60 Author: Stanislav Fomichev <sdf@google.com> Date: Wed Sep 13 10:13:49 2023 -0700 bpf: expose information about supported xdp metadata kfunc Add new xdp-rx-metadata-features member to netdev netlink which exports a bitmask of supported kfuncs. Most of the patch is autogenerated (headers), the only relevant part is netdev.yaml and the changes in netdev-genl.c to marshal into netlink. Example output on veth: $ ip link add veth0 type veth peer name veth1 # ifndex == 12 $ ./tools/net/ynl/samples/netdev 12 Select ifc ($ifindex; or 0 = dump; or -2 ntf check): 12 veth1[12] xdp-features (23): basic redirect rx-sg xdp-rx-metadata-features (3): timestamp hash xdp-zc-max-segs=0 Cc: netdev@vger.kernel.org Cc: Willem de Bruijn <willemb@google.com> Signed-off-by: Stanislav Fomichev <sdf@google.com> Link: https://lore.kernel.org/r/20230913171350.369987-3-sdf@google.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com>
1 parent 26daeb6 commit f0e3be5

File tree

8 files changed

+78
-5
lines changed

8 files changed

+78
-5
lines changed

Documentation/netlink/specs/netdev.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ definitions:
4242
doc:
4343
This feature informs if netdev implements non-linear XDP buffer
4444
support in ndo_xdp_xmit callback.
45+
-
46+
type: flags
47+
name: xdp-rx-metadata
48+
render-max: true
49+
entries:
50+
-
51+
name: timestamp
52+
doc:
53+
Device is capable of exposing receive HW timestamp via bpf_xdp_metadata_rx_timestamp().
54+
-
55+
name: hash
56+
doc:
57+
Device is capable of exposing receive packet hash via bpf_xdp_metadata_rx_hash().
4558

4659
attribute-sets:
4760
-
@@ -68,6 +81,13 @@ attribute-sets:
6881
type: u32
6982
checks:
7083
min: 1
84+
-
85+
name: xdp-rx-metadata-features
86+
doc: Bitmask of supported XDP receive metadata features.
87+
See Documentation/networking/xdp-rx-metadata.rst for more details.
88+
type: u64
89+
enum: xdp-rx-metadata
90+
enum-as-flags: true
7191

7292
operations:
7393
list:
@@ -84,6 +104,7 @@ operations:
84104
- ifindex
85105
- xdp-features
86106
- xdp-zc-max-segs
107+
- xdp-rx-metadata-features
87108
dump:
88109
reply: *dev-all
89110
-

Documentation/networking/xdp-rx-metadata.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ bpf_tail_call
105105
Adding programs that access metadata kfuncs to the ``BPF_MAP_TYPE_PROG_ARRAY``
106106
is currently not supported.
107107

108+
Supported Devices
109+
=================
110+
111+
It is possible to query which kfunc the particular netdev implements via
112+
netlink. See ``xdp-rx-metadata-features`` attribute set in
113+
``Documentation/netlink/specs/netdev.yaml``.
114+
108115
Example
109116
=======
110117

include/net/xdp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,22 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
422422
/* Define the relationship between xdp-rx-metadata kfunc and
423423
* various other entities:
424424
* - xdp_rx_metadata enum
425+
* - netdev netlink enum (Documentation/netlink/specs/netdev.yaml)
425426
* - kfunc name
426427
* - xdp_metadata_ops field
427428
*/
428429
#define XDP_METADATA_KFUNC_xxx \
429430
XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
431+
NETDEV_XDP_RX_METADATA_TIMESTAMP, \
430432
bpf_xdp_metadata_rx_timestamp, \
431433
xmo_rx_timestamp) \
432434
XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
435+
NETDEV_XDP_RX_METADATA_HASH, \
433436
bpf_xdp_metadata_rx_hash, \
434437
xmo_rx_hash) \
435438

436439
enum xdp_rx_metadata {
437-
#define XDP_METADATA_KFUNC(name, _, __) name,
440+
#define XDP_METADATA_KFUNC(name, _, __, ___) name,
438441
XDP_METADATA_KFUNC_xxx
439442
#undef XDP_METADATA_KFUNC
440443
MAX_XDP_METADATA_KFUNC,

include/uapi/linux/netdev.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,27 @@ enum netdev_xdp_act {
3838
NETDEV_XDP_ACT_MASK = 127,
3939
};
4040

41+
/**
42+
* enum netdev_xdp_rx_metadata
43+
* @NETDEV_XDP_RX_METADATA_TIMESTAMP: Device is capable of exposing receive HW
44+
* timestamp via bpf_xdp_metadata_rx_timestamp().
45+
* @NETDEV_XDP_RX_METADATA_HASH: Device is capable of exposing receive packet
46+
* hash via bpf_xdp_metadata_rx_hash().
47+
*/
48+
enum netdev_xdp_rx_metadata {
49+
NETDEV_XDP_RX_METADATA_TIMESTAMP = 1,
50+
NETDEV_XDP_RX_METADATA_HASH = 2,
51+
52+
/* private: */
53+
NETDEV_XDP_RX_METADATA_MASK = 3,
54+
};
55+
4156
enum {
4257
NETDEV_A_DEV_IFINDEX = 1,
4358
NETDEV_A_DEV_PAD,
4459
NETDEV_A_DEV_XDP_FEATURES,
4560
NETDEV_A_DEV_XDP_ZC_MAX_SEGS,
61+
NETDEV_A_DEV_XDP_RX_METADATA_FEATURES,
4662

4763
__NETDEV_A_DEV_MAX,
4864
NETDEV_A_DEV_MAX = (__NETDEV_A_DEV_MAX - 1)

kernel/bpf/offload.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog, u32 func_id)
846846
if (!ops)
847847
goto out;
848848

849-
#define XDP_METADATA_KFUNC(name, _, xmo) \
849+
#define XDP_METADATA_KFUNC(name, _, __, xmo) \
850850
if (func_id == bpf_xdp_metadata_kfunc_id(name)) p = ops->xmo;
851851
XDP_METADATA_KFUNC_xxx
852852
#undef XDP_METADATA_KFUNC

net/core/netdev-genl.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,32 @@
55
#include <linux/rtnetlink.h>
66
#include <net/net_namespace.h>
77
#include <net/sock.h>
8+
#include <net/xdp.h>
89

910
#include "netdev-genl-gen.h"
1011

1112
static int
1213
netdev_nl_dev_fill(struct net_device *netdev, struct sk_buff *rsp,
1314
const struct genl_info *info)
1415
{
16+
u64 xdp_rx_meta = 0;
1517
void *hdr;
1618

1719
hdr = genlmsg_iput(rsp, info);
1820
if (!hdr)
1921
return -EMSGSIZE;
2022

23+
#define XDP_METADATA_KFUNC(_, flag, __, xmo) \
24+
if (netdev->xdp_metadata_ops && netdev->xdp_metadata_ops->xmo) \
25+
xdp_rx_meta |= flag;
26+
XDP_METADATA_KFUNC_xxx
27+
#undef XDP_METADATA_KFUNC
28+
2129
if (nla_put_u32(rsp, NETDEV_A_DEV_IFINDEX, netdev->ifindex) ||
2230
nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_FEATURES,
23-
netdev->xdp_features, NETDEV_A_DEV_PAD)) {
31+
netdev->xdp_features, NETDEV_A_DEV_PAD) ||
32+
nla_put_u64_64bit(rsp, NETDEV_A_DEV_XDP_RX_METADATA_FEATURES,
33+
xdp_rx_meta, NETDEV_A_DEV_PAD)) {
2434
genlmsg_cancel(rsp, hdr);
2535
return -EINVAL;
2636
}

net/core/xdp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,
757757
__diag_pop();
758758

759759
BTF_SET8_START(xdp_metadata_kfunc_ids)
760-
#define XDP_METADATA_KFUNC(_, name, __) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
760+
#define XDP_METADATA_KFUNC(_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
761761
XDP_METADATA_KFUNC_xxx
762762
#undef XDP_METADATA_KFUNC
763763
BTF_SET8_END(xdp_metadata_kfunc_ids)
@@ -768,7 +768,7 @@ static const struct btf_kfunc_id_set xdp_metadata_kfunc_set = {
768768
};
769769

770770
BTF_ID_LIST(xdp_metadata_kfunc_ids_unsorted)
771-
#define XDP_METADATA_KFUNC(name, str, _) BTF_ID(func, str)
771+
#define XDP_METADATA_KFUNC(name, _, str, __) BTF_ID(func, str)
772772
XDP_METADATA_KFUNC_xxx
773773
#undef XDP_METADATA_KFUNC
774774

tools/include/uapi/linux/netdev.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,27 @@ enum netdev_xdp_act {
3838
NETDEV_XDP_ACT_MASK = 127,
3939
};
4040

41+
/**
42+
* enum netdev_xdp_rx_metadata
43+
* @NETDEV_XDP_RX_METADATA_TIMESTAMP: Device is capable of exposing receive HW
44+
* timestamp via bpf_xdp_metadata_rx_timestamp().
45+
* @NETDEV_XDP_RX_METADATA_HASH: Device is capable of exposing receive packet
46+
* hash via bpf_xdp_metadata_rx_hash().
47+
*/
48+
enum netdev_xdp_rx_metadata {
49+
NETDEV_XDP_RX_METADATA_TIMESTAMP = 1,
50+
NETDEV_XDP_RX_METADATA_HASH = 2,
51+
52+
/* private: */
53+
NETDEV_XDP_RX_METADATA_MASK = 3,
54+
};
55+
4156
enum {
4257
NETDEV_A_DEV_IFINDEX = 1,
4358
NETDEV_A_DEV_PAD,
4459
NETDEV_A_DEV_XDP_FEATURES,
4560
NETDEV_A_DEV_XDP_ZC_MAX_SEGS,
61+
NETDEV_A_DEV_XDP_RX_METADATA_FEATURES,
4662

4763
__NETDEV_A_DEV_MAX,
4864
NETDEV_A_DEV_MAX = (__NETDEV_A_DEV_MAX - 1)

0 commit comments

Comments
 (0)