On Wed, Aug 31, 2022 at 4:36 PM Harsh Modi <harshmodi@xxxxxxxxxx> wrote: > > eBPF currently does not have a good way to support more advanced > checksums like crc32c checksums. A bpf helper that allows users > to hash packet data from eBPF will use this. > > Currently, it only supports crc32c, however, additional support for > new hashes can be supported by adding an additional enum and > implementing the corresponding code in net/core/filter.c. > > Signed-off-by: Harsh Modi <harshmodi@xxxxxxxxxx> > --- > include/net/xdp.h | 3 + > include/uapi/linux/bpf.h | 33 ++++ > net/core/filter.c | 100 +++++++++++ > net/core/xdp.c | 51 ++++++ > scripts/bpf_doc.py | 2 + > tools/include/uapi/linux/bpf.h | 33 ++++ > .../selftests/bpf/prog_tests/packet_hash.c | 159 ++++++++++++++++++ > .../testing/selftests/bpf/progs/packet_hash.c | 125 ++++++++++++++ > 8 files changed, 506 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/packet_hash.c > create mode 100644 tools/testing/selftests/bpf/progs/packet_hash.c > > diff --git a/include/net/xdp.h b/include/net/xdp.h > index 04c852c7a77f..cbfec47e391d 100644 > --- a/include/net/xdp.h > +++ b/include/net/xdp.h > @@ -407,6 +407,9 @@ struct netdev_bpf; > void xdp_attachment_setup(struct xdp_attachment_info *info, > struct netdev_bpf *bpf); > > +__wsum __xdp_checksum(struct xdp_buff *xdp, int offset, int len, > + __wsum csum, const struct skb_checksum_ops *ops); > + > #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE > > #endif /* __LINUX_NET_XDP_H__ */ > diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h > index 962960a98835..c8313a13a948 100644 > --- a/include/uapi/linux/bpf.h > +++ b/include/uapi/linux/bpf.h > @@ -5386,6 +5386,25 @@ union bpf_attr { > * Return > * Current *ktime*. > * > + * int bpf_skb_packet_hash(struct sk_buff *skb, struct bpf_packet_hash_params *params, void *hash, u32 len) > + * Description > + * Hash the packet data based on the parameters set in *params*. > + * The hash will be set in *hash*. The value of *len* will be > + * dependent on the hash algorithm. > + * Currently only crc32c is supported. > + * > + * Return > + * 0 on success, or negative errno if there is an error. > + * > + * int bpf_xdp_packet_hash(struct xdp_buff *xdp, struct bpf_packet_hash_params *params, void *hash, u32 len) > + * Description > + * Hash the packet data based on the parameters set in *params*. > + * The hash will be set in *hash*. The value of *len* will be > + * dependent on the hash algorithm. > + * Currently only crc32c is supported. > + * > + * Return > + * 0 on success, or negative errno if there is an error. > */ What are your thoughts on having a more generic hashing helper? I'm thinking about something like bpf_dynptr_hash(enum bpf_hash hash_type, struct bpf_dynptr src, struct bpf_dynptr dst, u64 flags); where this helper would extend easily to other cases as well (for example, to malloc-type dynpts) instead of needing multiple helpers. > #define __BPF_FUNC_MAPPER(FN) \ > FN(unspec), \ > @@ -5597,6 +5616,8 @@ union bpf_attr { > FN(tcp_raw_check_syncookie_ipv4), \ > FN(tcp_raw_check_syncookie_ipv6), \ > FN(ktime_get_tai_ns), \ > + FN(skb_packet_hash), \ > + FN(xdp_packet_hash), \ > /* */ [...] > -- > 2.37.2.672.g94769d06f0-goog >