A program on cpumap or devmap cannot call the XDP RX metadata kfuncs, because they need a device-bound program. A program on veth can be bound to veth, but the veth callbacks return -ENODATA for an xdp_frame. So a program after the redirect cannot read the values that the program on the NIC stored.
Add three kfuncs that read the skb core extension area: bpf_xdp_skb_ext_load_rx_hash(), bpf_xdp_skb_ext_load_rx_vlan() and bpf_xdp_skb_ext_load_rx_csum(). Each returns -ENODATA when its entry is not stored. They are in the same kfunc set as the store kfuncs, so they are not device-bound and need no driver callback. A program on the NIC can also read back the values that it stored. The RX metadata kfuncs could return the stored values instead. This patch does not do that. Relaxing bpf_dev_bound_kfunc_check() for cpumap is not trivial, and it changes these kfuncs for existing programs, which read the values from the RX descriptor of the bound device. Signed-off-by: Tianyi Gao <[email protected]> --- net/core/xdp.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/net/core/xdp.c b/net/core/xdp.c index fdda072a5858..4df3984479a7 100644 --- a/net/core/xdp.c +++ b/net/core/xdp.c @@ -1081,6 +1081,15 @@ static const void *xdp_skb_ext_get(const struct xdp_skb_ext *ext, return (const void *)ext + off * XDP_SKB_EXT_ALIGN; } +static const void *xdp_skb_ext_load(const struct xdp_md *ctx, + enum xdp_skb_ext_id id) +{ + const struct xdp_buff *xdp = (const struct xdp_buff *)ctx; + const struct xdp_skb_ext *ext = xdp_buff_skb_ext(xdp); + + return ext ? xdp_skb_ext_get(ext, id) : NULL; +} + /** * xdp_skb_ext_to_skb - Set the stored RX metadata on an skb. * @skb: The skb that is built from the packet. @@ -1296,6 +1305,85 @@ __bpf_kfunc int bpf_xdp_skb_ext_del(struct xdp_md *ctx, enum xdp_skb_ext_id id) return 0; } +/** + * bpf_xdp_skb_ext_load_rx_hash - Load the stored RX hash of this packet. + * @ctx: XDP context pointer. + * @hash: Return value pointer. + * @rss_type: Return value pointer for RSS type, see &enum xdp_rss_hash_type. + * + * Return: + * * Returns 0 on success or ``-errno`` on error. + * * ``-ENODATA`` : the RX hash is not stored + */ +__bpf_kfunc int bpf_xdp_skb_ext_load_rx_hash(const struct xdp_md *ctx, + u32 *hash, + enum xdp_rss_hash_type *rss_type) +{ + const struct xdp_skb_ext_hash *entry; + + entry = xdp_skb_ext_load(ctx, XDP_SKB_EXT_HASH); + if (!entry) + return -ENODATA; + + *hash = entry->val; + *rss_type = entry->type; + + return 0; +} + +/** + * bpf_xdp_skb_ext_load_rx_vlan - Load the stored VLAN tag of this packet. + * @ctx: XDP context pointer. + * @vlan_proto: Return value pointer for the TPID, in network byte order. + * @vlan_tci: Return value pointer for the TCI, in host byte order. + * + * Return: + * * Returns 0 on success or ``-errno`` on error. + * * ``-ENODATA`` : the VLAN tag is not stored + */ +__bpf_kfunc int bpf_xdp_skb_ext_load_rx_vlan(const struct xdp_md *ctx, + __be16 *vlan_proto, u16 *vlan_tci) +{ + const struct xdp_skb_ext_vlan *entry; + + entry = xdp_skb_ext_load(ctx, XDP_SKB_EXT_VLAN); + if (!entry) + return -ENODATA; + + *vlan_proto = entry->proto; + *vlan_tci = entry->tci; + + return 0; +} + +/** + * bpf_xdp_skb_ext_load_rx_csum - Load the stored RX checksum status of this + * packet. + * @ctx: XDP context pointer. + * @ip_summed: Return value pointer, ``XDP_CHECKSUM_NONE`` or + * ``XDP_CHECKSUM_UNNECESSARY``. + * @csum_level: Return value pointer for the checksum level. + * + * Return: + * * Returns 0 on success or ``-errno`` on error. + * * ``-ENODATA`` : the checksum status is not stored + */ +__bpf_kfunc int bpf_xdp_skb_ext_load_rx_csum(const struct xdp_md *ctx, + enum xdp_checksum *ip_summed, + u8 *csum_level) +{ + const struct xdp_skb_ext_csum *entry; + + entry = xdp_skb_ext_load(ctx, XDP_SKB_EXT_CSUM); + if (!entry) + return -ENODATA; + + *ip_summed = BIT(entry->ip_summed); + *csum_level = entry->level; + + return 0; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(xdp_skb_ext_kfunc_ids) @@ -1303,6 +1391,9 @@ BTF_ID_FLAGS(func, bpf_xdp_skb_ext_store_rx_hash) BTF_ID_FLAGS(func, bpf_xdp_skb_ext_store_rx_vlan) BTF_ID_FLAGS(func, bpf_xdp_skb_ext_store_rx_csum) BTF_ID_FLAGS(func, bpf_xdp_skb_ext_del) +BTF_ID_FLAGS(func, bpf_xdp_skb_ext_load_rx_hash) +BTF_ID_FLAGS(func, bpf_xdp_skb_ext_load_rx_vlan) +BTF_ID_FLAGS(func, bpf_xdp_skb_ext_load_rx_csum) BTF_KFUNCS_END(xdp_skb_ext_kfunc_ids) static const struct btf_kfunc_id_set xdp_skb_ext_kfunc_set = { -- 2.56.0
