Skip to content

Commit fc45c5b

Browse files
fomichevMartin KaFai Lau
authored andcommitted
bpf: make it easier to add new metadata kfunc
No functional changes. Instead of having hand-crafted code in bpf_dev_bound_resolve_kfunc, move kfunc <> xmo handler relationship into XDP_METADATA_KFUNC_xxx. This way, any time new kfunc is added, we don't have to touch bpf_dev_bound_resolve_kfunc. Also document XDP_METADATA_KFUNC_xxx arguments since we now have more than two and it might be confusing what is what. 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-2-sdf@google.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent d609f3d commit fc45c5b

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

include/net/xdp.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,22 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
383383

384384
#define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
385385

386+
/* Define the relationship between xdp-rx-metadata kfunc and
387+
* various other entities:
388+
* - xdp_rx_metadata enum
389+
* - kfunc name
390+
* - xdp_metadata_ops field
391+
*/
386392
#define XDP_METADATA_KFUNC_xxx \
387393
XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
388-
bpf_xdp_metadata_rx_timestamp) \
394+
bpf_xdp_metadata_rx_timestamp, \
395+
xmo_rx_timestamp) \
389396
XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
390-
bpf_xdp_metadata_rx_hash) \
397+
bpf_xdp_metadata_rx_hash, \
398+
xmo_rx_hash) \
391399

392-
enum {
393-
#define XDP_METADATA_KFUNC(name, _) name,
400+
enum xdp_rx_metadata {
401+
#define XDP_METADATA_KFUNC(name, _, __) name,
394402
XDP_METADATA_KFUNC_xxx
395403
#undef XDP_METADATA_KFUNC
396404
MAX_XDP_METADATA_KFUNC,

kernel/bpf/offload.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,11 @@ void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog, u32 func_id)
845845
if (!ops)
846846
goto out;
847847

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

net/core/xdp.c

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

743743
BTF_SET8_START(xdp_metadata_kfunc_ids)
744-
#define XDP_METADATA_KFUNC(_, name) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
744+
#define XDP_METADATA_KFUNC(_, name, __) BTF_ID_FLAGS(func, name, KF_TRUSTED_ARGS)
745745
XDP_METADATA_KFUNC_xxx
746746
#undef XDP_METADATA_KFUNC
747747
BTF_SET8_END(xdp_metadata_kfunc_ids)
@@ -752,7 +752,7 @@ static const struct btf_kfunc_id_set xdp_metadata_kfunc_set = {
752752
};
753753

754754
BTF_ID_LIST(xdp_metadata_kfunc_ids_unsorted)
755-
#define XDP_METADATA_KFUNC(name, str) BTF_ID(func, str)
755+
#define XDP_METADATA_KFUNC(name, str, _) BTF_ID(func, str)
756756
XDP_METADATA_KFUNC_xxx
757757
#undef XDP_METADATA_KFUNC
758758

0 commit comments

Comments
 (0)