Skip to contentSkip to conversation

bpf: Add bpf_[skb|xdp]_packet_hash.

PATCH In review Harsh Modi 3 participants 5 messages Aug 31, 2022 – Sep 8, 2022 1 message not in the archive

Subsystems (6)
lore
Share & tools
lore

Harsh Modi

author
#0 lore raw

PATCH 1/1 bpf: Add bpf_[skb|xdp]_packet_hash.

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.

---
 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
include/net/xdp.h +3 -0
index 04c852c7a77f..cbfec47e391d 100644
@@ -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__ */
include/uapi/linux/bpf.h +33 -0
index 962960a98835..c8313a13a948 100644
@@ -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.
  */
 #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),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -6920,4 +6941,16 @@ struct bpf_core_relo {
 	enum bpf_core_relo_kind kind;
 };
 
+enum bpf_hash {
+	BPF_HASH_UNSPEC = 0,
+	BPF_CRC32C,
+};
+
+struct bpf_packet_hash_params {
+	enum bpf_hash hash;
+	__u32 initial;
+	__u32 offset;
+	__u32 len;
+};
+
 #endif /* _UAPI__LINUX_BPF_H__ */
net/core/filter.c +100 -0

130 lines not on this page · raw message

net/core/xdp.c +51 -0

54 lines not on this page · raw message

scripts/bpf_doc.py +2 -0
index d5c389df6045..c0aa87331d8e 100755
@@ -640,6 +640,7 @@ class PrinterHelpers(Printer):
 
     type_fwds = [
             'struct bpf_fib_lookup',
+            'struct bpf_packet_hash_params',
             'struct bpf_sk_lookup',
             'struct bpf_perf_event_data',
             'struct bpf_perf_event_value',
@@ -697,6 +698,7 @@ class PrinterHelpers(Printer):
             '__wsum',
 
             'struct bpf_fib_lookup',
+            'struct bpf_packet_hash_params',
             'struct bpf_perf_event_data',
             'struct bpf_perf_event_value',
             'struct bpf_pidns_info',
tools/include/uapi/linux/bpf.h +33 -0
index f4ba82a1eace..5e53f6a57a24 100644
@@ -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.
  */
 #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),		\
 	/* */
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
@@ -6920,4 +6941,16 @@ struct bpf_core_relo {
 	enum bpf_core_relo_kind kind;
 };
 
+enum bpf_hash {
+	BPF_HASH_UNSPEC = 0,
+	BPF_CRC32C,
+};
+
+struct bpf_packet_hash_params {
+	enum bpf_hash hash;
+	__u32 initial;	/* initial value of the hash */
+	__u32 offset;	/* packet offset */
+	__u32 len;	/* length of the packet to hash. */
+};
+
 #endif /* _UAPI__LINUX_BPF_H__ */
tools/testing/selftests/bpf/prog_tests/packet_hash.c new file +159 -0

159 lines not on this page · raw message

tools/testing/selftests/bpf/progs/packet_hash.c new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
2.37.2.672.g94769d06f0-goog
#1 lore raw
Thank you for the patch! Yet something to improve:

Hi Harsh,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Harsh-Modi/bpf-Add-bpf_-skb-xdp-_packet_hash/20220901-073646
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: x86_64-randconfig-a001 (https://download.01.org/0day-ci/archive/20220902/[email protected]/config)
compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/078b031d06a73f3fc17333d808db554ec4753a90
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Harsh-Modi/bpf-Add-bpf_-skb-xdp-_packet_hash/20220901-073646 git checkout 078b031d06a73f3fc17333d808db554ec4753a90
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

>> ld.lld: error: undefined symbol: crc32c
   >>> referenced by filter.c
   >>>               core/filter.o:(crc32c_csum_update) in archive net/built-in.a
Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#2 lore raw
In reply to Harsh Modi
Thank you for the patch! Yet something to improve:

Hi Harsh,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on bpf-next/master]

url:    https://github.com/intel-lab-lkp/linux/commits/Harsh-Modi/bpf-Add-bpf_-skb-xdp-_packet_hash/20220901-073646
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
config: um-x86_64_defconfig (https://download.01.org/0day-ci/archive/20220903/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-5) 11.3.0
reproduce (this is a W=1 build):
        # https://github.com/intel-lab-lkp/linux/commit/078b031d06a73f3fc17333d808db554ec4753a90
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Harsh-Modi/bpf-Add-bpf_-skb-xdp-_packet_hash/20220901-073646
        git checkout 078b031d06a73f3fc17333d808db554ec4753a90
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        make W=1 O=build_dir ARCH=um SUBARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

/usr/bin/ld: warning: arch/x86/um/vdso/vdso.o: missing .note.GNU-stack section implies executable stack /usr/bin/ld: NOTE: This behaviour is deprecated and will be removed in a future version of the linker /usr/bin/ld: warning: .tmp_vmlinux.kallsyms1 has a LOAD segment with RWX permissions /usr/bin/ld: net/core/filter.o: in function `crc32c_csum_update':

>> filter.c:(.text+0x61fd): undefined reference to `crc32c'

collect2: error: ld returned 1 exit status

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#3 lore raw
In reply to Harsh Modi
Quoted 55 lines · Harsh Modi

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.

---
 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
include/net/xdp.h +3 -0
index 04c852c7a77f..cbfec47e391d 100644
@@ -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__ */
include/uapi/linux/bpf.h +10 -0
index 962960a98835..c8313a13a948 100644
@@ -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.
+ *

include/uapi/linux/bpf.h union bpf_attr { · line 5408

Quoted 10 lines
include/uapi/linux/bpf.h +9 -0
+ * 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.

Quoted 9 lines
include/uapi/linux/bpf.h +2 -0
 #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),            \
        /* */

[...]

Quoted 3 lines

--
2.37.2.672.g94769d06f0-goog

A message that is not in the archive (sent off-list or not yet received): CAO5Vyb_gHECvaJLhO-4V9Uov8GMkbUxyJ383dyWdP5QJFhdJnw@mail.gmail.com

#5 lore raw
Quoted 4 lines · Harsh Modi
Quoted 2 lines

What are your thoughts on having a more generic hashing helper?

I didn't make the connection that dynptrs could also reference packet data... Would dynptrs also handle multi buffer support in both skb and xdp or would we have to pull all the data into a contiguous location first?

skb and xdp dynptrs haven't been merged in yet, the latest thread on
this is here: https://lore.kernel.org/bpf/[email protected]/

Dynptrs can handle multibuffer support in skb and xdp for the program, it will pull in non-linear data as part of bpf_dynptr_write() if the write is to a frag in the skb