Skip to content

Commit 3681c87

Browse files
committed
Merge: CNB95: bpf: expose information about netdev xdp-metadata kfunc support
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/4041 JIRA: https://issues.redhat.com/browse/RHEL-31945 Tested: compile only Depends: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/3939 Commits: ``` 2e06c57 ("xdp: use trusted arguments in XDP hints kfuncs") fc45c5b ("bpf: make it easier to add new metadata kfunc") a9c2a60 ("bpf: expose information about supported xdp metadata kfunc") 0c6c9b1 ("tools: ynl: extend netdev sample to dump xdp-rx-metadata-features") 0629f22 ("ynl: netdev: drop unnecessary enum-as-flags") 9fea94d ("tools: ynl: fix converting flags to names after recent cleanup") ``` Signed-off-by: Jose Ignacio Tornos Martinez <jtornosm@redhat.com> Approved-by: Felix Maurer <fmaurer@redhat.com> Approved-by: Petr Oros <poros@redhat.com> Approved-by: Ivan Vecera <ivecera@redhat.com> Merged-by: Lucas Zampieri <lzampier@redhat.com>
2 parents da1aeb2 + 8da6a82 commit 3681c87

File tree

13 files changed

+123
-15
lines changed

13 files changed

+123
-15
lines changed

Documentation/netlink/specs/netdev.yaml

Lines changed: 20 additions & 1 deletion
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
-
@@ -61,13 +74,18 @@ attribute-sets:
6174
doc: Bitmask of enabled xdp-features.
6275
type: u64
6376
enum: xdp-act
64-
enum-as-flags: true
6577
-
6678
name: xdp-zc-max-segs
6779
doc: max fragment count supported by ZC driver
6880
type: u32
6981
checks:
7082
min: 1
83+
-
84+
name: xdp-rx-metadata-features
85+
doc: Bitmask of supported XDP receive metadata features.
86+
See Documentation/networking/xdp-rx-metadata.rst for more details.
87+
type: u64
88+
enum: xdp-rx-metadata
7189

7290
operations:
7391
list:
@@ -84,6 +102,7 @@ operations:
84102
- ifindex
85103
- xdp-features
86104
- xdp-zc-max-segs
105+
- xdp-rx-metadata-features
87106
dump:
88107
reply: *dev-all
89108
-

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: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,25 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
419419

420420
#define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
421421

422+
/* Define the relationship between xdp-rx-metadata kfunc and
423+
* various other entities:
424+
* - xdp_rx_metadata enum
425+
* - netdev netlink enum (Documentation/netlink/specs/netdev.yaml)
426+
* - kfunc name
427+
* - xdp_metadata_ops field
428+
*/
422429
#define XDP_METADATA_KFUNC_xxx \
423430
XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
424-
bpf_xdp_metadata_rx_timestamp) \
431+
NETDEV_XDP_RX_METADATA_TIMESTAMP, \
432+
bpf_xdp_metadata_rx_timestamp, \
433+
xmo_rx_timestamp) \
425434
XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
426-
bpf_xdp_metadata_rx_hash) \
435+
NETDEV_XDP_RX_METADATA_HASH, \
436+
bpf_xdp_metadata_rx_hash, \
437+
xmo_rx_hash) \
427438

428-
enum {
429-
#define XDP_METADATA_KFUNC(name, _) name,
439+
enum xdp_rx_metadata {
440+
#define XDP_METADATA_KFUNC(name, _, __, ___) name,
430441
XDP_METADATA_KFUNC_xxx
431442
#undef XDP_METADATA_KFUNC
432443
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,10 +846,11 @@ void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog, u32 func_id)
846846
if (!ops)
847847
goto out;
848848

849-
if (func_id == bpf_xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_TIMESTAMP))
850-
p = ops->xmo_rx_timestamp;
851-
else if (func_id == bpf_xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_HASH))
852-
p = ops->xmo_rx_hash;
849+
#define XDP_METADATA_KFUNC(name, _, __, xmo) \
850+
if (func_id == bpf_xdp_metadata_kfunc_id(name)) p = ops->xmo;
851+
XDP_METADATA_KFUNC_xxx
852+
#undef XDP_METADATA_KFUNC
853+
853854
out:
854855
up_read(&bpf_devs_lock);
855856

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
@@ -755,7 +755,7 @@ __bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash,
755755
__bpf_kfunc_end_defs();
756756

757757
BTF_SET8_START(xdp_metadata_kfunc_ids)
758-
#define XDP_METADATA_KFUNC(_, name) BTF_ID_FLAGS(func, name, 0)
758+
#define XDP_METADATA_KFUNC(_, __, name, ___) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
759759
XDP_METADATA_KFUNC_xxx
760760
#undef XDP_METADATA_KFUNC
761761
BTF_SET8_END(xdp_metadata_kfunc_ids)
@@ -766,7 +766,7 @@ static const struct btf_kfunc_id_set xdp_metadata_kfunc_set = {
766766
};
767767

768768
BTF_ID_LIST(xdp_metadata_kfunc_ids_unsorted)
769-
#define XDP_METADATA_KFUNC(name, str) BTF_ID(func, str)
769+
#define XDP_METADATA_KFUNC(name, _, str, __) BTF_ID(func, str)
770770
XDP_METADATA_KFUNC_xxx
771771
#undef XDP_METADATA_KFUNC
772772

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)

tools/net/ynl/generated/netdev-user.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,26 @@ const char *netdev_xdp_act_str(enum netdev_xdp_act value)
4545
return netdev_xdp_act_strmap[value];
4646
}
4747

48+
static const char * const netdev_xdp_rx_metadata_strmap[] = {
49+
[0] = "timestamp",
50+
[1] = "hash",
51+
};
52+
53+
const char *netdev_xdp_rx_metadata_str(enum netdev_xdp_rx_metadata value)
54+
{
55+
value = ffs(value) - 1;
56+
if (value < 0 || value >= (int)MNL_ARRAY_SIZE(netdev_xdp_rx_metadata_strmap))
57+
return NULL;
58+
return netdev_xdp_rx_metadata_strmap[value];
59+
}
60+
4861
/* Policies */
4962
struct ynl_policy_attr netdev_dev_policy[NETDEV_A_DEV_MAX + 1] = {
5063
[NETDEV_A_DEV_IFINDEX] = { .name = "ifindex", .type = YNL_PT_U32, },
5164
[NETDEV_A_DEV_PAD] = { .name = "pad", .type = YNL_PT_IGNORE, },
5265
[NETDEV_A_DEV_XDP_FEATURES] = { .name = "xdp-features", .type = YNL_PT_U64, },
5366
[NETDEV_A_DEV_XDP_ZC_MAX_SEGS] = { .name = "xdp-zc-max-segs", .type = YNL_PT_U32, },
67+
[NETDEV_A_DEV_XDP_RX_METADATA_FEATURES] = { .name = "xdp-rx-metadata-features", .type = YNL_PT_U64, },
5468
};
5569

5670
struct ynl_policy_nest netdev_dev_nest = {
@@ -97,6 +111,11 @@ int netdev_dev_get_rsp_parse(const struct nlmsghdr *nlh, void *data)
97111
return MNL_CB_ERROR;
98112
dst->_present.xdp_zc_max_segs = 1;
99113
dst->xdp_zc_max_segs = mnl_attr_get_u32(attr);
114+
} else if (type == NETDEV_A_DEV_XDP_RX_METADATA_FEATURES) {
115+
if (ynl_attr_validate(yarg, attr))
116+
return MNL_CB_ERROR;
117+
dst->_present.xdp_rx_metadata_features = 1;
118+
dst->xdp_rx_metadata_features = mnl_attr_get_u64(attr);
100119
}
101120
}
102121

tools/net/ynl/generated/netdev-user.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern const struct ynl_family ynl_netdev_family;
1818
/* Enums */
1919
const char *netdev_op_str(int op);
2020
const char *netdev_xdp_act_str(enum netdev_xdp_act value);
21+
const char *netdev_xdp_rx_metadata_str(enum netdev_xdp_rx_metadata value);
2122

2223
/* Common nested types */
2324
/* ============== NETDEV_CMD_DEV_GET ============== */
@@ -48,11 +49,13 @@ struct netdev_dev_get_rsp {
4849
__u32 ifindex:1;
4950
__u32 xdp_features:1;
5051
__u32 xdp_zc_max_segs:1;
52+
__u32 xdp_rx_metadata_features:1;
5153
} _present;
5254

5355
__u32 ifindex;
5456
__u64 xdp_features;
5557
__u32 xdp_zc_max_segs;
58+
__u64 xdp_rx_metadata_features;
5659
};
5760

5861
void netdev_dev_get_rsp_free(struct netdev_dev_get_rsp *rsp);

0 commit comments

Comments
 (0)