Skip to contentSkip to conversation

xdp: hints via kfuncs

PATCH Superseded Stanislav Fomichev 9 participants 61 messages Dec 6, 2022 – Dec 11, 2022

Subsystems (15)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 cover xdp: hints via kfuncs

Please see the first patch in the series for the overall design and use-cases.

Changes since v3:

- Rework prog->bound_netdev refcounting (Jakub/Marin)

Now it's based on the offload.c framework. It mostly fits, except I had to automatically insert a HT entry for the netdev. In the offloaded case, the netdev is added via a call to bpf_offload_dev_netdev_register from the driver init path; with a dev-bound programs, we have to manually add (and remove) the entry.

As suggested by Toke, I'm also prohibiting putting dev-bound programs into prog-array map; essentially prohibiting tail calling into it. I'm also disabling freplace of the dev-bound programs. Both of those restrictions can be loosened up eventually.
Note that we don't require maps to be dev-bound when the program is dev-bound.

Confirmed with the test_offload.py that the existing parts are still operational.

- Fix compile issues with CONFIG_NET=n and mlx5 driver ([email protected])

Changes since v2:

- Rework bpf_prog_aux->xdp_netdev refcnt (Martin)

Switched to dropping the count early, after loading / verification is done. At attach time, the pointer value is used only for comparing the actual netdev at attach vs netdev at load.

(potentially can be a problem if the same slub slot is reused for another netdev later on?)

- Use correct RX queue number in xdp_hw_metadata (Toke / Jakub)

- Fix wrongly placed '*cnt=0' in fixup_kfunc_call after merge (Toke)

- Fix sorted BTF_SET8_START (Toke)

Introduce old-school unsorted BTF_ID_LIST for lookup purposes.

- Zero-initialize mlx4_xdp_buff (Tariq)

- Separate common timestamp handling into mlx4_en_get_hwtstamp (Tariq)

- mlx5 patches (Toke)

Note, I've renamed the following for consistency with the rest:
- s/mlx5_xdp_ctx/mlx5_xdp_buff/
- s/mctx/mxbuf/

Changes since v1:

- Drop xdp->skb metadata path (Jakub)

No consensus yet on exposing xdp_skb_metadata in UAPI. Exploring whether everyone would be ok with kfunc to access that part.. Will follow up separately.

- Drop kfunc unrolling (Alexei)

Starting with simple code to resolve per-device ndo kfuncs. We can always go back to unrolling and keep the same kfuncs interface in the future.

- Add rx hash metadata (Toke)

Not adding the rest (csum/hash_type/etc), I'd like us to agree on the framework.

- use dev_get_by_index and add proper refcnt (Toke)

Changes since last RFC:

- drop ice/bnxt example implementation (Alexander)

-ENOHARDWARE to test

- fix/test mlx4 implementation

Confirmed that I get reasonable looking timestamp. The last patch in the series is the small xsk program that can be used to dump incoming metadata.

- bpf_push64/bpf_pop64 (Alexei)

x86_64+arm64(untested)+disassembler

- struct xdp_to_skb_metadata -> struct xdp_skb_metadata (Toke)

s/xdp_to_skb/xdp_skb/

- Documentation/bpf/xdp-rx-metadata.rst

Documents functionality, assumptions and limitations.

- bpf_xdp_metadata_export_to_skb returns true/false (Martin)

Plus xdp_md->skb_metadata field to access it.

- BPF_F_XDP_HAS_METADATA flag (Toke/Martin)

Drop magic, use the flag instead.

- drop __randomize_layout

Not sure it's possible to sanely expose it via UAPI. Because every .o potentially gets its own randomized layout, test_progs refuses to link.

- remove __net_timestamp in veth driver (John/Jesper)

Instead, calling ktime_get from the kfunc; enough for the selftests.

Future work on RX side:

- Support more devices besides veth and mlx4
- Support more metadata besides RX timestamp.
- Convert skb_metadata_set() callers to xdp_convert_skb_metadata() which handles extra xdp_skb_metadata

Prior art (to record pros/cons for different approaches):

- Stable UAPI approach:
https://lore.kernel.org/bpf/[email protected]/
- Metadata+BTF_ID appoach:
https://lore.kernel.org/bpf/166256538687.1434226.15760041133601409770.stgit@firesoul/
- v1:
https://lore.kernel.org/bpf/[email protected]/T/#t
- kfuncs v2 RFC:
https://lore.kernel.org/bpf/[email protected]/
- kfuncs v1 RFC:
https://lore.kernel.org/bpf/[email protected]/

Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>

Stanislav Fomichev (9):
bpf: Document XDP RX metadata
bpf: Rename bpf_{prog,map}_is_dev_bound to is_offloaded
bpf: XDP metadata RX kfuncs
veth: Introduce veth_xdp_buff wrapper for xdp_buff
veth: Support RX XDP metadata
selftests/bpf: Verify xdp_metadata xdp->af_xdp path
mlx4: Introduce mlx4_xdp_buff wrapper for xdp_buff
mxl4: Support RX XDP metadata
selftests/bpf: Simple program to dump XDP RX metadata

Toke Høiland-Jørgensen (3):
xsk: Add cb area to struct xdp_buff_xsk
mlx5: Introduce mlx5_xdp_buff wrapper for xdp_buff
mlx5: Support RX XDP metadata

 Documentation/bpf/xdp-rx-metadata.rst         |  90 ++++
 drivers/net/ethernet/mellanox/mlx4/en_clock.c |  13 +-
 .../net/ethernet/mellanox/mlx4/en_netdev.c    |  10 +
 drivers/net/ethernet/mellanox/mlx4/en_rx.c    |  68 ++-
 drivers/net/ethernet/mellanox/mlx4/mlx4_en.h  |   1 +
 drivers/net/ethernet/mellanox/mlx5/core/en.h  |  11 +-
 .../net/ethernet/mellanox/mlx5/core/en/xdp.c  |  32 +-
 .../net/ethernet/mellanox/mlx5/core/en/xdp.h  |  13 +-
 .../ethernet/mellanox/mlx5/core/en/xsk/rx.c   |  35 +-
 .../ethernet/mellanox/mlx5/core/en/xsk/rx.h   |   2 +
 .../net/ethernet/mellanox/mlx5/core/en_main.c |   4 +
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   |  98 ++---
 drivers/net/veth.c                            |  88 ++--
 include/linux/bpf.h                           |  26 +-
 include/linux/mlx4/device.h                   |   7 +
 include/linux/netdevice.h                     |   5 +
 include/net/xdp.h                             |  29 ++
 include/net/xsk_buff_pool.h                   |   5 +
 include/uapi/linux/bpf.h                      |   5 +
 kernel/bpf/arraymap.c                         |  17 +-
 kernel/bpf/core.c                             |   2 +-
 kernel/bpf/offload.c                          | 162 +++++--
 kernel/bpf/syscall.c                          |  25 +-
 kernel/bpf/verifier.c                         |  42 +-
 net/core/dev.c                                |   7 +-
 net/core/filter.c                             |   2 +-
 net/core/xdp.c                                |  58 +++
 tools/include/uapi/linux/bpf.h                |   5 +
 tools/testing/selftests/bpf/.gitignore        |   1 +
 tools/testing/selftests/bpf/Makefile          |   8 +-
 .../selftests/bpf/prog_tests/xdp_metadata.c   | 394 +++++++++++++++++
 .../selftests/bpf/progs/xdp_hw_metadata.c     |  93 ++++
 .../selftests/bpf/progs/xdp_metadata.c        |  70 +++
 .../selftests/bpf/progs/xdp_metadata2.c       |  15 +
 tools/testing/selftests/bpf/xdp_hw_metadata.c | 405 ++++++++++++++++++
 tools/testing/selftests/bpf/xdp_metadata.h    |   7 +
 36 files changed, 1688 insertions(+), 167 deletions(-)
 create mode 100644 Documentation/bpf/xdp-rx-metadata.rst
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
 create mode 100644 tools/testing/selftests/bpf/progs/xdp_hw_metadata.c
 create mode 100644 tools/testing/selftests/bpf/progs/xdp_metadata.c
 create mode 100644 tools/testing/selftests/bpf/progs/xdp_metadata2.c
 create mode 100644 tools/testing/selftests/bpf/xdp_hw_metadata.c
 create mode 100644 tools/testing/selftests/bpf/xdp_metadata.h
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#1 lore raw

PATCH v3 1/12 bpf: Document XDP RX metadata

Document all current use-cases and assumptions.

Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 Documentation/bpf/xdp-rx-metadata.rst | 90 +++++++++++++++++++++++++++
 1 file changed, 90 insertions(+)
 create mode 100644 Documentation/bpf/xdp-rx-metadata.rst
Documentation/bpf/xdp-rx-metadata.rst new file +90 -0
new file mode 100644
index 000000000000..498eae718275
@@ -0,0 +1,90 @@
+===============
+XDP RX Metadata
+===============
+
+XDP programs support creating and passing custom metadata via
+``bpf_xdp_adjust_meta``. This metadata can be consumed by the following
+entities:
+
+1. ``AF_XDP`` consumer.
+2. Kernel core stack via ``XDP_PASS``.
+3. Another device via ``bpf_redirect_map``.
+4. Other BPF programs via ``bpf_tail_call``.
+
+General Design
+==============
+
+XDP has access to a set of kfuncs to manipulate the metadata. Every
+device driver implements these kfuncs. The set of kfuncs is
+declared in ``include/net/xdp.h`` via ``XDP_METADATA_KFUNC_xxx``.
+
+Currently, the following kfuncs are supported. In the future, as more
+metadata is supported, this set will grow:
+
+- ``bpf_xdp_metadata_rx_timestamp_supported`` returns true/false to
+  indicate whether the device supports RX timestamps
+- ``bpf_xdp_metadata_rx_timestamp`` returns packet RX timestamp
+- ``bpf_xdp_metadata_rx_hash_supported`` returns true/false to
+  indicate whether the device supports RX hash
+- ``bpf_xdp_metadata_rx_hash`` returns packet RX hash
+
+Within the XDP frame, the metadata layout is as follows::
+
+  +----------+-----------------+------+
+  | headroom | custom metadata | data |
+  +----------+-----------------+------+
+             ^                 ^
+             |                 |
+   xdp_buff->data_meta   xdp_buff->data
+
+AF_XDP
+======
+
+``AF_XDP`` use-case implies that there is a contract between the BPF program
+that redirects XDP frames into the ``XSK`` and the final consumer.
+Thus the BPF program manually allocates a fixed number of
+bytes out of metadata via ``bpf_xdp_adjust_meta`` and calls a subset
+of kfuncs to populate it. User-space ``XSK`` consumer, looks
+at ``xsk_umem__get_data() - METADATA_SIZE`` to locate its metadata.
+
+Here is the ``AF_XDP`` consumer layout (note missing ``data_meta`` pointer)::
+
+  +----------+-----------------+------+
+  | headroom | custom metadata | data |
+  +----------+-----------------+------+
+                               ^
+                               |
+                        rx_desc->address
+
+XDP_PASS
+========
+
+This is the path where the packets processed by the XDP program are passed
+into the kernel. The kernel creates ``skb`` out of the ``xdp_buff`` contents.
+Currently, every driver has a custom kernel code to parse the descriptors and
+populate ``skb`` metadata when doing this ``xdp_buff->skb`` conversion.
+In the future, we'd like to support a case where XDP program can override
+some of that metadata.
+
+The plan of record is to make this path similar to ``bpf_redirect_map``
+so the program can control which metadata is passed to the skb layer.
+
+bpf_redirect_map
+================
+
+``bpf_redirect_map`` can redirect the frame to a different device.
+In this case we don't know ahead of time whether that final consumer
+will further redirect to an ``XSK`` or pass it to the kernel via ``XDP_PASS``.
+Additionally, the final consumer doesn't have access to the original
+hardware descriptor and can't access any of the original metadata.
+
+For this use-case, only custom metadata is currently supported. If
+the frame is eventually passed to the kernel, the skb created from such
+a frame won't have any skb metadata. The ``XSK`` consumer will only
+have access to the custom metadata.
+
+bpf_tail_call
+=============
+
+No special handling here. Tail-called program operates on the same context
+as the original one.
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
maintainer
#2 lore raw

Re: [PATCH bpf-next v3 01/12] bpf: Document XDP RX metadata

Documentation/bpf/xdp-rx-metadata.rst line 29

Quoted 6 lines · On Mon, 5 Dec 2022 18:45:43 -0800 Stanislav Fomichev wrote:

+- ``bpf_xdp_metadata_rx_timestamp_supported`` returns true/false to

Documentation/bpf/xdp-rx-metadata.rst new file +5 -0
+  indicate whether the device supports RX timestamps
+- ``bpf_xdp_metadata_rx_timestamp`` returns packet RX timestamp
+- ``bpf_xdp_metadata_rx_hash_supported`` returns true/false to
+  indicate whether the device supports RX hash
+- ``bpf_xdp_metadata_rx_hash`` returns packet RX hash

Would you mind pointing to the discussion about the separate _supported() kfuncs? I recall folks had concerns about the function call overhead, and now we have 2 calls per field? :S

#3 lore raw

Re: [PATCH bpf-next v3 01/12] bpf: Document XDP RX metadata

Quoted 9 lines · Jakub Kicinski

Documentation/bpf/xdp-rx-metadata.rst line 29

Quoted 6 lines · On Mon, 5 Dec 2022 18:45:43 -0800 Stanislav Fomichev wrote:

+- ``bpf_xdp_metadata_rx_timestamp_supported`` returns true/false to

Documentation/bpf/xdp-rx-metadata.rst new file +5 -0
+  indicate whether the device supports RX timestamps
+- ``bpf_xdp_metadata_rx_timestamp`` returns packet RX timestamp
+- ``bpf_xdp_metadata_rx_hash_supported`` returns true/false to
+  indicate whether the device supports RX hash
+- ``bpf_xdp_metadata_rx_hash`` returns packet RX hash
Quoted 3 lines

Would you mind pointing to the discussion about the separate _supported() kfuncs? I recall folks had concerns about the function call overhead, and now we have 2 calls per field? :S

Take a look at [0] and [1]. I'm still assuming that we might support some restricted set of kfuncs that can be unrolled so keeping this simple/separate apis.

0: https://lore.kernel.org/bpf/CAADnVQJMvPjXCtKNH+WCryPmukgbWTrJyHqxrnO=2YraZEukPg@mail.gmail.com
1: https://lore.kernel.org/bpf/Y4XZkZJHVvLgTIk9@lavr/

#4 lore raw

PATCH v3 2/12 bpf: Rename bpf_{prog,map}_is_dev_bound to is_offloaded

In reply to Stanislav Fomichev

BPF offloading infra will be reused to implement bound-but-not-offloaded bpf programs. Rename existing helpers for clarity. No functional changes.

Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 include/linux/bpf.h   |  8 ++++----
 kernel/bpf/core.c     |  4 ++--
 kernel/bpf/offload.c  |  4 ++--
 kernel/bpf/syscall.c  | 22 +++++++++++-----------
 kernel/bpf/verifier.c | 18 +++++++++---------
 net/core/dev.c        |  2 +-
 net/core/filter.c     |  2 +-
 7 files changed, 30 insertions(+), 30 deletions(-)
include/linux/bpf.h +4 -4
index 4920ac252754..d5d479dae118 100644
@@ -2481,12 +2481,12 @@ void unpriv_ebpf_notify(int new_state);
 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
 
-static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
+static inline bool bpf_prog_is_offloaded(const struct bpf_prog_aux *aux)
 {
 	return aux->offload_requested;
 }
 
-static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
+static inline bool bpf_map_is_offloaded(struct bpf_map *map)
 {
 	return unlikely(map->ops == &bpf_map_offload_ops);
 }
@@ -2513,12 +2513,12 @@ static inline int bpf_prog_offload_init(struct bpf_prog *prog,
 	return -EOPNOTSUPP;
 }
 
-static inline bool bpf_prog_is_dev_bound(struct bpf_prog_aux *aux)
+static inline bool bpf_prog_is_offloaded(struct bpf_prog_aux *aux)
 {
 	return false;
 }
 
-static inline bool bpf_map_is_dev_bound(struct bpf_map *map)
+static inline bool bpf_map_is_offloaded(struct bpf_map *map)
 {
 	return false;
 }
kernel/bpf/core.c +2 -2
index 2e57fc839a5c..641ab412ad7e 100644
@@ -2183,7 +2183,7 @@ struct bpf_prog *bpf_prog_select_runtime(struct bpf_prog *fp, int *err)
 	 * valid program, which in this case would simply not
 	 * be JITed, but falls back to the interpreter.
 	 */
-	if (!bpf_prog_is_dev_bound(fp->aux)) {
+	if (!bpf_prog_is_offloaded(fp->aux)) {
 		*err = bpf_prog_alloc_jited_linfo(fp);
 		if (*err)
 			return fp;
@@ -2554,7 +2554,7 @@ static void bpf_prog_free_deferred(struct work_struct *work)
 #endif
 	bpf_free_used_maps(aux);
 	bpf_free_used_btfs(aux);
-	if (bpf_prog_is_dev_bound(aux))
+	if (bpf_prog_is_offloaded(aux))
 		bpf_prog_offload_destroy(aux->prog);
 #ifdef CONFIG_PERF_EVENTS
 	if (aux->prog->has_callchain_buf)
kernel/bpf/offload.c +2 -2
index 13e4efc971e6..f5769a8ecbee 100644
@@ -549,7 +549,7 @@ static bool __bpf_offload_dev_match(struct bpf_prog *prog,
 	struct bpf_offload_netdev *ondev1, *ondev2;
 	struct bpf_prog_offload *offload;
 
-	if (!bpf_prog_is_dev_bound(prog->aux))
+	if (!bpf_prog_is_offloaded(prog->aux))
 		return false;
 
 	offload = prog->aux->offload;
@@ -581,7 +581,7 @@ bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map)
 	struct bpf_offloaded_map *offmap;
 	bool ret;
 
-	if (!bpf_map_is_dev_bound(map))
+	if (!bpf_map_is_offloaded(map))
 		return bpf_map_offload_neutral(map);
 	offmap = map_to_offmap(map);
 
kernel/bpf/syscall.c +11 -11
index 35972afb6850..13bc96035116 100644
@@ -181,7 +181,7 @@ static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
 	int err;
 
 	/* Need to create a kthread, thus must support schedule */
-	if (bpf_map_is_dev_bound(map)) {
+	if (bpf_map_is_offloaded(map)) {
 		return bpf_map_offload_update_elem(map, key, value, flags);
 	} else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
 		   map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
@@ -238,7 +238,7 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
 	void *ptr;
 	int err;
 
-	if (bpf_map_is_dev_bound(map))
+	if (bpf_map_is_offloaded(map))
 		return bpf_map_offload_lookup_elem(map, key, value);
 
 	bpf_disable_instrumentation();
@@ -1483,7 +1483,7 @@ static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr)
 		goto err_put;
 	}
 
-	if (bpf_map_is_dev_bound(map)) {
+	if (bpf_map_is_offloaded(map)) {
 		err = bpf_map_offload_delete_elem(map, key);
 		goto out;
 	} else if (IS_FD_PROG_ARRAY(map) ||
@@ -1547,7 +1547,7 @@ static int map_get_next_key(union bpf_attr *attr)
 	if (!next_key)
 		goto free_key;
 
-	if (bpf_map_is_dev_bound(map)) {
+	if (bpf_map_is_offloaded(map)) {
 		err = bpf_map_offload_get_next_key(map, key, next_key);
 		goto out;
 	}
@@ -1605,7 +1605,7 @@ int generic_map_delete_batch(struct bpf_map *map,
 				   map->key_size))
 			break;
 
-		if (bpf_map_is_dev_bound(map)) {
+		if (bpf_map_is_offloaded(map)) {
 			err = bpf_map_offload_delete_elem(map, key);
 			break;
 		}
@@ -1851,7 +1851,7 @@ static int map_lookup_and_delete_elem(union bpf_attr *attr)
 		   map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
 		   map->map_type == BPF_MAP_TYPE_LRU_HASH ||
 		   map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
-		if (!bpf_map_is_dev_bound(map)) {
+		if (!bpf_map_is_offloaded(map)) {
 			bpf_disable_instrumentation();
 			rcu_read_lock();
 			err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
@@ -1944,7 +1944,7 @@ static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
 	if (!ops)
 		return -EINVAL;
 
-	if (!bpf_prog_is_dev_bound(prog->aux))
+	if (!bpf_prog_is_offloaded(prog->aux))
 		prog->aux->ops = ops;
 	else
 		prog->aux->ops = &bpf_offload_prog_ops;
@@ -2255,7 +2255,7 @@ bool bpf_prog_get_ok(struct bpf_prog *prog,
 
 	if (prog->type != *attach_type)
 		return false;
-	if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
+	if (bpf_prog_is_offloaded(prog->aux) && !attach_drv)
 		return false;
 
 	return true;
@@ -2598,7 +2598,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
 	atomic64_set(&prog->aux->refcnt, 1);
 	prog->gpl_compatible = is_gpl ? 1 : 0;
 
-	if (bpf_prog_is_dev_bound(prog->aux)) {
+	if (bpf_prog_is_offloaded(prog->aux)) {
 		err = bpf_prog_offload_init(prog, attr);
 		if (err)
 			goto free_prog_sec;
@@ -3997,7 +3997,7 @@ static int bpf_prog_get_info_by_fd(struct file *file,
 			return -EFAULT;
 	}
 
-	if (bpf_prog_is_dev_bound(prog->aux)) {
+	if (bpf_prog_is_offloaded(prog->aux)) {
 		err = bpf_prog_offload_info_fill(&info, prog);
 		if (err)
 			return err;
@@ -4225,7 +4225,7 @@ static int bpf_map_get_info_by_fd(struct file *file,
 	}
 	info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
 
-	if (bpf_map_is_dev_bound(map)) {
+	if (bpf_map_is_offloaded(map)) {
 		err = bpf_map_offload_info_fill(&info, map);
 		if (err)
 			return err;
kernel/bpf/verifier.c +9 -9
index 1d51bd9596da..fc4e313a4d2e 100644
@@ -13648,7 +13648,7 @@ static int do_check(struct bpf_verifier_env *env)
 			env->prev_log_len = env->log.len_used;
 		}
 
-		if (bpf_prog_is_dev_bound(env->prog->aux)) {
+		if (bpf_prog_is_offloaded(env->prog->aux)) {
 			err = bpf_prog_offload_verify_insn(env, env->insn_idx,
 							   env->prev_insn_idx);
 			if (err)
@@ -14128,7 +14128,7 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,
 		}
 	}
 
-	if ((bpf_prog_is_dev_bound(prog->aux) || bpf_map_is_dev_bound(map)) &&
+	if ((bpf_prog_is_offloaded(prog->aux) || bpf_map_is_offloaded(map)) &&
 	    !bpf_offload_prog_map_match(prog, map)) {
 		verbose(env, "offload device mismatch between prog and map\n");
 		return -EINVAL;
@@ -14609,7 +14609,7 @@ static int verifier_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
 	unsigned int orig_prog_len = env->prog->len;
 	int err;
 
-	if (bpf_prog_is_dev_bound(env->prog->aux))
+	if (bpf_prog_is_offloaded(env->prog->aux))
 		bpf_prog_offload_remove_insns(env, off, cnt);
 
 	err = bpf_remove_insns(env->prog, off, cnt);
@@ -14690,7 +14690,7 @@ static void opt_hard_wire_dead_code_branches(struct bpf_verifier_env *env)
 		else
 			continue;
 
-		if (bpf_prog_is_dev_bound(env->prog->aux))
+		if (bpf_prog_is_offloaded(env->prog->aux))
 			bpf_prog_offload_replace_insn(env, i, &ja);
 
 		memcpy(insn, &ja, sizeof(ja));
@@ -14873,7 +14873,7 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env)
 		}
 	}
 
-	if (bpf_prog_is_dev_bound(env->prog->aux))
+	if (bpf_prog_is_offloaded(env->prog->aux))
 		return 0;
 
 	insn = env->prog->insnsi + delta;
@@ -15273,7 +15273,7 @@ static int fixup_call_args(struct bpf_verifier_env *env)
 	int err = 0;
 
 	if (env->prog->jit_requested &&
-	    !bpf_prog_is_dev_bound(env->prog->aux)) {
+	    !bpf_prog_is_offloaded(env->prog->aux)) {
 		err = jit_subprogs(env);
 		if (err == 0)
 			return 0;
@@ -16747,7 +16747,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr)
 	if (ret < 0)
 		goto skip_full_check;
 
-	if (bpf_prog_is_dev_bound(env->prog->aux)) {
+	if (bpf_prog_is_offloaded(env->prog->aux)) {
 		ret = bpf_prog_offload_verifier_prep(env->prog);
 		if (ret)
 			goto skip_full_check;
@@ -16760,7 +16760,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr)
 	ret = do_check_subprogs(env);
 	ret = ret ?: do_check_main(env);
 
-	if (ret == 0 && bpf_prog_is_dev_bound(env->prog->aux))
+	if (ret == 0 && bpf_prog_is_offloaded(env->prog->aux))
 		ret = bpf_prog_offload_finalize(env);
 
 skip_full_check:
@@ -16795,7 +16795,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr)
 	/* do 32-bit optimization after insn patching has done so those patched
 	 * insns could be handled correctly.
 	 */
-	if (ret == 0 && !bpf_prog_is_dev_bound(env->prog->aux)) {
+	if (ret == 0 && !bpf_prog_is_offloaded(env->prog->aux)) {
 		ret = opt_subreg_zext_lo32_rnd_hi32(env, attr);
 		env->prog->aux->verifier_zext = bpf_jit_needs_zext() ? !ret
 								     : false;
net/core/dev.c +1 -1
index 7627c475d991..5b221568dfd4 100644
@@ -9224,7 +9224,7 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
 			NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the same time");
 			return -EEXIST;
 		}
-		if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) {
+		if (!offload && bpf_prog_is_offloaded(new_prog->aux)) {
 			NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
 			return -EINVAL;
 		}
net/core/filter.c +1 -1
index 8607136b6e2c..d89c30ba2623 100644
@@ -8719,7 +8719,7 @@ static bool xdp_is_valid_access(int off, int size,
 	}
 
 	if (type == BPF_WRITE) {
-		if (bpf_prog_is_dev_bound(prog->aux)) {
+		if (bpf_prog_is_offloaded(prog->aux)) {
 			switch (off) {
 			case offsetof(struct xdp_md, rx_queue_index):
 				return __is_valid_xdp_access(off, size);
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
maintainer Reviewed-by on v3 2/12
#5 lore raw

Re: [PATCH bpf-next v3 02/12] bpf: Rename bpf_{prog,map}_is_dev_bound to is_offloaded

Quoted 17 lines · On Mon, 5 Dec 2022 18:45:44 -0800 Stanislav Fomichev wrote:

BPF offloading infra will be reused to implement bound-but-not-offloaded bpf programs. Rename existing helpers for clarity. No functional changes.

Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
Quoted 1 line
#6 lore raw

PATCH v3 3/12 bpf: XDP metadata RX kfuncs

In reply to Stanislav Fomichev

There is an ndo handler per kfunc, the verifier replaces a call to the generic kfunc with a call to the per-device one.

For XDP, we define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible metatada kfuncs. Not all devices have to implement them. If kfunc is not supported by the target device, the default implementation is called instead.

Upon loading, if BPF_F_XDP_HAS_METADATA is passed via prog_flags, we treat prog_index as target device for kfunc resolution.

Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 include/linux/bpf.h            |  20 +++-
 include/linux/netdevice.h      |   5 +
 include/net/xdp.h              |  29 ++++++
 include/uapi/linux/bpf.h       |   5 +
 kernel/bpf/arraymap.c          |  17 +++-
 kernel/bpf/core.c              |   2 +-
 kernel/bpf/offload.c           | 162 ++++++++++++++++++++++++++++-----
 kernel/bpf/syscall.c           |   7 +-
 kernel/bpf/verifier.c          |  24 ++++-
 net/core/dev.c                 |   5 +
 net/core/xdp.c                 |  58 ++++++++++++
 tools/include/uapi/linux/bpf.h |   5 +
 12 files changed, 304 insertions(+), 35 deletions(-)
include/linux/bpf.h +19 -1
index d5d479dae118..b46b60f4eae1 100644
@@ -1261,7 +1261,8 @@ struct bpf_prog_aux {
 	enum bpf_prog_type saved_dst_prog_type;
 	enum bpf_attach_type saved_dst_attach_type;
 	bool verifier_zext; /* Zero extensions has been inserted by verifier. */
-	bool offload_requested;
+	bool dev_bound; /* Program is bound to the netdev. */
+	bool offload_requested; /* Program is bound and offloaded to the netdev. */
 	bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
 	bool func_proto_unreliable;
 	bool sleepable;
@@ -2476,10 +2477,18 @@ void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
 				       struct net_device *netdev);
 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
 
+void *bpf_offload_resolve_kfunc(struct bpf_prog *prog, u32 func_id);
+
 void unpriv_ebpf_notify(int new_state);
 
 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
+void bpf_offload_bound_netdev_unregister(struct net_device *dev);
+
+static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
+{
+	return aux->dev_bound;
+}
 
 static inline bool bpf_prog_is_offloaded(const struct bpf_prog_aux *aux)
 {
@@ -2513,6 +2522,15 @@ static inline int bpf_prog_offload_init(struct bpf_prog *prog,
 	return -EOPNOTSUPP;
 }
 
+static inline void bpf_offload_bound_netdev_unregister(struct net_device *dev)
+{
+}
+
+static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
+{
+	return false;
+}
+
 static inline bool bpf_prog_is_offloaded(struct bpf_prog_aux *aux)
 {
 	return false;
include/linux/netdevice.h +5 -0
index 5aa35c58c342..2eabb9157767 100644
@@ -74,6 +74,7 @@ struct udp_tunnel_nic_info;
 struct udp_tunnel_nic;
 struct bpf_prog;
 struct xdp_buff;
+struct xdp_md;
 
 void synchronize_net(void);
 void netdev_set_default_ethtool_ops(struct net_device *dev,
@@ -1611,6 +1612,10 @@ struct net_device_ops {
 	ktime_t			(*ndo_get_tstamp)(struct net_device *dev,
 						  const struct skb_shared_hwtstamps *hwtstamps,
 						  bool cycles);
+	bool			(*ndo_xdp_rx_timestamp_supported)(const struct xdp_md *ctx);
+	u64			(*ndo_xdp_rx_timestamp)(const struct xdp_md *ctx);
+	bool			(*ndo_xdp_rx_hash_supported)(const struct xdp_md *ctx);
+	u32			(*ndo_xdp_rx_hash)(const struct xdp_md *ctx);
 };
 
 /**
include/net/xdp.h +29 -0
index 55dbc68bfffc..c24aba5c363b 100644
@@ -409,4 +409,33 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
 
 #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
 
+#define XDP_METADATA_KFUNC_xxx	\
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP_SUPPORTED, \
+			   bpf_xdp_metadata_rx_timestamp_supported) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
+			   bpf_xdp_metadata_rx_timestamp) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH_SUPPORTED, \
+			   bpf_xdp_metadata_rx_hash_supported) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
+			   bpf_xdp_metadata_rx_hash) \
+
+enum {
+#define XDP_METADATA_KFUNC(name, str) name,
+XDP_METADATA_KFUNC_xxx
+#undef XDP_METADATA_KFUNC
+MAX_XDP_METADATA_KFUNC,
+};
+
+#ifdef CONFIG_NET
+u32 xdp_metadata_kfunc_id(int id);
+#else
+static inline u32 xdp_metadata_kfunc_id(int id) { return 0; }
+#endif
+
+struct xdp_md;
+bool bpf_xdp_metadata_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx);
+bool bpf_xdp_metadata_rx_hash_supported(const struct xdp_md *ctx);
+u32 bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx);
+
 #endif /* __LINUX_NET_XDP_H__ */
include/uapi/linux/bpf.h +5 -0
index f89de51a45db..790650a81f2b 100644
@@ -1156,6 +1156,11 @@ enum bpf_link_type {
  */
 #define BPF_F_XDP_HAS_FRAGS	(1U << 5)
 
+/* If BPF_F_XDP_HAS_METADATA is used in BPF_PROG_LOAD command, the loaded
+ * program becomes device-bound but can access it's XDP metadata.
+ */
+#define BPF_F_XDP_HAS_METADATA	(1U << 6)
+
 /* link_create.kprobe_multi.flags used in LINK_CREATE command for
  * BPF_TRACE_KPROBE_MULTI attach type to create return probe.
  */
kernel/bpf/arraymap.c +13 -4
index 484706959556..9b190b72ffce 100644
@@ -881,12 +881,21 @@ static void *prog_fd_array_get_ptr(struct bpf_map *map,
 	if (IS_ERR(prog))
 		return prog;
 
-	if (!bpf_prog_map_compatible(map, prog)) {
-		bpf_prog_put(prog);
-		return ERR_PTR(-EINVAL);
-	}
+	/* When tail-calling from a non-dev-bound program to a dev-bound one,
+	 * XDP metadata helpers should be disabled. Until it's implemented,
+	 * prohibit adding dev-bound programs to tail-call maps.
+	 */
+	if (bpf_prog_is_dev_bound(prog->aux))
+		goto err;
+
+	if (!bpf_prog_map_compatible(map, prog))
+		goto err;
 
 	return prog;
+
+err:
+	bpf_prog_put(prog);
+	return ERR_PTR(-EINVAL);
 }
 
 static void prog_fd_array_put_ptr(void *ptr)
kernel/bpf/core.c +1 -1
index 641ab412ad7e..71c6dc081f62 100644
@@ -2554,7 +2554,7 @@ static void bpf_prog_free_deferred(struct work_struct *work)
 #endif
 	bpf_free_used_maps(aux);
 	bpf_free_used_btfs(aux);
-	if (bpf_prog_is_offloaded(aux))
+	if (bpf_prog_is_dev_bound(aux))
 		bpf_prog_offload_destroy(aux->prog);
 #ifdef CONFIG_PERF_EVENTS
 	if (aux->prog->has_callchain_buf)
kernel/bpf/offload.c +137 -25

279 lines not on this page · raw message

kernel/bpf/syscall.c +4 -3
index 13bc96035116..b345a273f7d0 100644
@@ -2491,7 +2491,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
 				 BPF_F_TEST_STATE_FREQ |
 				 BPF_F_SLEEPABLE |
 				 BPF_F_TEST_RND_HI32 |
-				 BPF_F_XDP_HAS_FRAGS))
+				 BPF_F_XDP_HAS_FRAGS |
+				 BPF_F_XDP_HAS_METADATA))
 		return -EINVAL;
 
 	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
@@ -2575,7 +2576,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
 	prog->aux->attach_btf = attach_btf;
 	prog->aux->attach_btf_id = attr->attach_btf_id;
 	prog->aux->dst_prog = dst_prog;
-	prog->aux->offload_requested = !!attr->prog_ifindex;
+	prog->aux->dev_bound = !!attr->prog_ifindex;
 	prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
 	prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
 
@@ -2598,7 +2599,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
 	atomic64_set(&prog->aux->refcnt, 1);
 	prog->gpl_compatible = is_gpl ? 1 : 0;
 
-	if (bpf_prog_is_offloaded(prog->aux)) {
+	if (bpf_prog_is_dev_bound(prog->aux)) {
 		err = bpf_prog_offload_init(prog, attr);
 		if (err)
 			goto free_prog_sec;
kernel/bpf/verifier.c +23 -1
index fc4e313a4d2e..00951a59ee26 100644
@@ -15323,6 +15323,24 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		return -EINVAL;
 	}
 
+	*cnt = 0;
+
+	if (resolve_prog_type(env->prog) == BPF_PROG_TYPE_XDP) {
+		if (bpf_prog_is_offloaded(env->prog->aux)) {
+			verbose(env, "no metadata kfuncs offload\n");
+			return -EINVAL;
+		}
+
+		if (bpf_prog_is_dev_bound(env->prog->aux)) {
+			void *p = bpf_offload_resolve_kfunc(env->prog, insn->imm);
+
+			if (p) {
+				insn->imm = BPF_CALL_IMM(p);
+				return 0;
+			}
+		}
+	}
+
 	/* insn->imm has the btf func_id. Replace it with
 	 * an address (relative to __bpf_base_call).
 	 */
@@ -15333,7 +15351,6 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		return -EFAULT;
 	}
 
-	*cnt = 0;
 	insn->imm = desc->imm;
 	if (insn->off)
 		return 0;
@@ -16340,6 +16357,11 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
 	if (tgt_prog) {
 		struct bpf_prog_aux *aux = tgt_prog->aux;
 
+		if (bpf_prog_is_dev_bound(tgt_prog->aux)) {
+			bpf_log(log, "Replacing device-bound programs not supported\n");
+			return -EINVAL;
+		}
+
 		for (i = 0; i < aux->func_info_cnt; i++)
 			if (aux->func_info[i].type_id == btf_id) {
 				subprog = i;
net/core/dev.c +5 -0
index 5b221568dfd4..862e03fcffa6 100644
@@ -9228,6 +9228,10 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
 			NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");
 			return -EINVAL;
 		}
+		if (bpf_prog_is_dev_bound(new_prog->aux) && !bpf_offload_dev_match(new_prog, dev)) {
+			NL_SET_ERR_MSG(extack, "Cannot attach to a different target device");
+			return -EINVAL;
+		}
 		if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
 			NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
 			return -EINVAL;
@@ -10813,6 +10817,7 @@ void unregister_netdevice_many_notify(struct list_head *head,
 		/* Shutdown queueing discipline. */
 		dev_shutdown(dev);
 
+		bpf_offload_bound_netdev_unregister(dev);
 		dev_xdp_uninstall(dev);
 
 		netdev_offload_xstats_disable_all(dev);
net/core/xdp.c +58 -0

67 lines not on this page · raw message

tools/include/uapi/linux/bpf.h +5 -0
index f89de51a45db..790650a81f2b 100644
@@ -1156,6 +1156,11 @@ enum bpf_link_type {
  */
 #define BPF_F_XDP_HAS_FRAGS	(1U << 5)
 
+/* If BPF_F_XDP_HAS_METADATA is used in BPF_PROG_LOAD command, the loaded
+ * program becomes device-bound but can access it's XDP metadata.
+ */
+#define BPF_F_XDP_HAS_METADATA	(1U << 6)
+
 /* link_create.kprobe_multi.flags used in LINK_CREATE command for
  * BPF_TRACE_KPROBE_MULTI attach type to create return probe.
  */
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#7 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 5 lines · Stanislav Fomichev
kernel/bpf/verifier.c +0 -0
index fc4e313a4d2e..00951a59ee26 100644

kernel/bpf/verifier.c static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 15332

Quoted 10 lines
kernel/bpf/verifier.c +7 -0
@@ -15323,6 +15323,24 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 		return -EINVAL;
 	}
 
+	*cnt = 0;
+
+	if (resolve_prog_type(env->prog) == BPF_PROG_TYPE_XDP) {
+		if (bpf_prog_is_offloaded(env->prog->aux)) {
+			verbose(env, "no metadata kfuncs offload\n");
+			return -EINVAL;
+		}

If I'm reading this correctly than this error will trigger for any XDP prog trying to use a kfunc?

I was hoping that BPF CI can prove my point, but it failed to build your newly added xdp_hw_metadata.c test.

#8 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 10 lines · Alexei Starovoitov
Quoted 8 lines · Stanislav Fomichev
kernel/bpf/verifier.c +0 -0
index fc4e313a4d2e..00951a59ee26 100644
@@ -15323,6 +15323,24 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
              return -EINVAL;
      }
 
Quoted 10 lines

kernel/bpf/verifier.c static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 15332

Quoted 7 lines
kernel/bpf/verifier.c +7 -0
+     *cnt = 0;
+
+     if (resolve_prog_type(env->prog) == BPF_PROG_TYPE_XDP) {
+             if (bpf_prog_is_offloaded(env->prog->aux)) {
+                     verbose(env, "no metadata kfuncs offload\n");
+                     return -EINVAL;
+             }

If I'm reading this correctly than this error will trigger for any XDP prog trying to use a kfunc?

bpf_prog_is_offloaded() should return true only when the program is fully offloaded to the device (like nfp). So here the intent is to reject kfunc programs because nft should somehow implement them first. Unless I'm not setting offload_requested somewhere, not sure I see the problem. LMK if I missed something.

Quoted 2 lines

I was hoping that BPF CI can prove my point, but it failed to build your newly added xdp_hw_metadata.c test.

Ugh, will take a look, thank you!

#9 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 23 lines · Stanislav Fomichev
Quoted 10 lines · Alexei Starovoitov
Quoted 8 lines · Stanislav Fomichev
kernel/bpf/verifier.c +0 -0
index fc4e313a4d2e..00951a59ee26 100644
@@ -15323,6 +15323,24 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
               return -EINVAL;
       }
 
Quoted 10 lines

kernel/bpf/verifier.c static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 15332

Quoted 7 lines
kernel/bpf/verifier.c +7 -0
+     *cnt = 0;
+
+     if (resolve_prog_type(env->prog) == BPF_PROG_TYPE_XDP) {
+             if (bpf_prog_is_offloaded(env->prog->aux)) {
+                     verbose(env, "no metadata kfuncs offload\n");
+                     return -EINVAL;
+             }

If I'm reading this correctly than this error will trigger for any XDP prog trying to use a kfunc?

Quoted 5 lines

bpf_prog_is_offloaded() should return true only when the program is fully offloaded to the device (like nfp). So here the intent is to reject kfunc programs because nft should somehow implement them first. Unless I'm not setting offload_requested somewhere, not sure I see the problem. LMK if I missed something.

It errors out for all kfunc here though. or it meant to error out for the XDP_METADATA_KFUNC_* only?

#10 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 25 lines · Martin KaFai Lau
Quoted 23 lines · Stanislav Fomichev
Quoted 20 lines · Alexei Starovoitov
Quoted 5 lines · Stanislav Fomichev
kernel/bpf/verifier.c +0 -0
index fc4e313a4d2e..00951a59ee26 100644

kernel/bpf/verifier.c static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 15332

Quoted 10 lines
kernel/bpf/verifier.c +7 -0
@@ -15323,6 +15323,24 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
               return -EINVAL;
       }
 
+     *cnt = 0;
+
+     if (resolve_prog_type(env->prog) == BPF_PROG_TYPE_XDP) {
+             if (bpf_prog_is_offloaded(env->prog->aux)) {
+                     verbose(env, "no metadata kfuncs offload\n");
+                     return -EINVAL;
+             }

If I'm reading this correctly than this error will trigger for any XDP prog trying to use a kfunc?

Quoted 8 lines
Quoted 5 lines

bpf_prog_is_offloaded() should return true only when the program is fully offloaded to the device (like nfp). So here the intent is to reject kfunc programs because nft should somehow implement them first. Unless I'm not setting offload_requested somewhere, not sure I see the problem. LMK if I missed something.

It errors out for all kfunc here though. or it meant to error out for the XDP_METADATA_KFUNC_* only?

Ah, good point, I was somewhat assuming that xdp doesn't use kfuncs right now and I can just assume that kfunc == metadata_kfunc. Will make this more selective, thanks!

#11 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

In reply to Stanislav Fomichev
Quoted 16 lines · Stanislav Fomichev
include/net/xdp.h +8 -0
index 55dbc68bfffc..c24aba5c363b 100644
@@ -409,4 +409,33 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
 
  #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
 
+#define XDP_METADATA_KFUNC_xxx	\
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP_SUPPORTED, \
+			   bpf_xdp_metadata_rx_timestamp_supported) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
+			   bpf_xdp_metadata_rx_timestamp) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH_SUPPORTED, \
+			   bpf_xdp_metadata_rx_hash_supported) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \

include/net/xdp.h void xdp_attachment_setup(struct xdp_attachment_info *info, · line 429

Quoted 10 lines
include/net/xdp.h +10 -0
+			   bpf_xdp_metadata_rx_hash) \
+
+enum {
+#define XDP_METADATA_KFUNC(name, str) name,
+XDP_METADATA_KFUNC_xxx
+#undef XDP_METADATA_KFUNC
+MAX_XDP_METADATA_KFUNC,
+};
+
+#ifdef CONFIG_NET

I think this is no longer needed because xdp_metadata_kfunc_id() is only used in offload.c which should be CONFIG_NET only.

Quoted 25 lines
include/net/xdp.h +11 -0
+u32 xdp_metadata_kfunc_id(int id);
+#else
+static inline u32 xdp_metadata_kfunc_id(int id) { return 0; }
+#endif
+
+struct xdp_md;
+bool bpf_xdp_metadata_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx);
+bool bpf_xdp_metadata_rx_hash_supported(const struct xdp_md *ctx);
+u32 bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx);
+
  #endif /* __LINUX_NET_XDP_H__ */
include/uapi/linux/bpf.h +5 -0
index f89de51a45db..790650a81f2b 100644
@@ -1156,6 +1156,11 @@ enum bpf_link_type {
   */
  #define BPF_F_XDP_HAS_FRAGS	(1U << 5)
 
+/* If BPF_F_XDP_HAS_METADATA is used in BPF_PROG_LOAD command, the loaded
+ * program becomes device-bound but can access it's XDP metadata.
+ */
+#define BPF_F_XDP_HAS_METADATA	(1U << 6)
+

[ ... ]

Quoted 46 lines
kernel/bpf/offload.c +12 -2
index f5769a8ecbee..bad8bab916eb 100644
@@ -41,7 +41,7 @@ struct bpf_offload_dev {
  struct bpf_offload_netdev {
  	struct rhash_head l;
  	struct net_device *netdev;
-	struct bpf_offload_dev *offdev;
+	struct bpf_offload_dev *offdev; /* NULL when bound-only */
  	struct list_head progs;
  	struct list_head maps;
  	struct list_head offdev_netdevs;
@@ -58,6 +58,12 @@ static const struct rhashtable_params offdevs_params = {
  static struct rhashtable offdevs;
  static bool offdevs_inited;
 
+static int __bpf_offload_init(void);
+static int __bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
+					     struct net_device *netdev);
+static void __bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
+						struct net_device *netdev);
+
  static int bpf_dev_offload_check(struct net_device *netdev)
  {
  	if (!netdev)
@@ -87,13 +93,17 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
  	    attr->prog_type != BPF_PROG_TYPE_XDP)
  		return -EINVAL;
 
-	if (attr->prog_flags)
+	if (attr->prog_flags & ~BPF_F_XDP_HAS_METADATA)
  		return -EINVAL;
 
  	offload = kzalloc(sizeof(*offload), GFP_USER);
  	if (!offload)
  		return -ENOMEM;
 
+	err = __bpf_offload_init();
+	if (err)
+		return err;
+
  	offload->prog = prog;
 
  	offload->netdev = dev_get_by_index(current->nsproxy->net_ns,

kernel/bpf/offload.c int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) · line 116

Quoted 5 lines
kernel/bpf/offload.c +2 -0
@@ -102,11 +112,25 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
  	if (err)
  		goto err_maybe_put;
 
+	prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_HAS_METADATA);
+

If I read the set correctly, bpf prog can either use metadata kfunc or offload but not both. It is fine to start with only supporting metadata kfunc when there is no offload but will be useful to understand the reason. I assume an offloaded bpf prog should still be able to call the bpf helpers like adjust_head/tail and the same should go for any kfunc?

Also, the BPF_F_XDP_HAS_METADATA feels like it is acting more like BPF_F_XDP_DEV_BOUND_ONLY.

kernel/bpf/offload.c int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) · line 120

Quoted 6 lines
kernel/bpf/offload.c +1 -2
  	down_write(&bpf_devs_lock);
  	ondev = bpf_offload_find_netdev(offload->netdev);
  	if (!ondev) {
-		err = -EINVAL;
-		goto err_unlock;
+		if (!prog->aux->offload_requested) {

nit. bpf_prog_is_offloaded(prog->aux)

Quoted 41 lines
kernel/bpf/offload.c +27 -0
+			/* When only binding to the device, explicitly
+			 * create an entry in the hashtable. See related
+			 * maybe_remove_bound_netdev.
+			 */
+			err = __bpf_offload_dev_netdev_register(NULL, offload->netdev);
+			if (err)
+				goto err_unlock;
+			ondev = bpf_offload_find_netdev(offload->netdev);
+		}
+		if (!ondev) {
+			err = -EINVAL;
+			goto err_unlock;
+		}
  	}
  	offload->offdev = ondev->offdev;
  	prog->aux->offload = offload;
@@ -209,6 +233,19 @@ bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
  	up_read(&bpf_devs_lock);
  }
 
+static void maybe_remove_bound_netdev(struct net_device *dev)
+{
+	struct bpf_offload_netdev *ondev;
+
+	rtnl_lock();
+	down_write(&bpf_devs_lock);
+	ondev = bpf_offload_find_netdev(dev);
+	if (ondev && !ondev->offdev && list_empty(&ondev->progs))
+		__bpf_offload_dev_netdev_unregister(NULL, dev);
+	up_write(&bpf_devs_lock);
+	rtnl_unlock();
+}
+
  static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
  {
  	struct bpf_prog_offload *offload = prog->aux->offload;
@@ -226,10 +263,17 @@ static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
 
  void bpf_prog_offload_destroy(struct bpf_prog *prog)
  {
+	struct net_device *netdev = NULL;

kernel/bpf/offload.c static void __bpf_prog_offload_destroy(struct bpf_prog *prog) · line 275

Quoted 10 lines
kernel/bpf/offload.c +6 -1
+
  	down_write(&bpf_devs_lock);
-	if (prog->aux->offload)
+	if (prog->aux->offload) {
+		netdev = prog->aux->offload->netdev;
  		__bpf_prog_offload_destroy(prog);
+	}
  	up_write(&bpf_devs_lock);
+
+	if (netdev)

May be I have missed a refcnt or lock somewhere. Is it possible that netdev may have been freed?

Quoted 3 lines
kernel/bpf/offload.c +1 -0
+		maybe_remove_bound_netdev(netdev);
  }
 

[ ... ]

Quoted 62 lines
kernel/bpf/offload.c +25 -0
+void *bpf_offload_resolve_kfunc(struct bpf_prog *prog, u32 func_id)
+{
+	const struct net_device_ops *netdev_ops;
+	void *p = NULL;
+
+	down_read(&bpf_devs_lock);
+	if (!prog->aux->offload || !prog->aux->offload->netdev)
+		goto out;
+
+	netdev_ops = prog->aux->offload->netdev->netdev_ops;
+
+	if (func_id == xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_TIMESTAMP_SUPPORTED))
+		p = netdev_ops->ndo_xdp_rx_timestamp_supported;
+	else if (func_id == xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_TIMESTAMP))
+		p = netdev_ops->ndo_xdp_rx_timestamp;
+	else if (func_id == xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_HASH_SUPPORTED))
+		p = netdev_ops->ndo_xdp_rx_hash_supported;
+	else if (func_id == xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_HASH))
+		p = netdev_ops->ndo_xdp_rx_hash;
+	/* fallback to default kfunc when not supported by netdev */
+out:
+	up_read(&bpf_devs_lock);
+
+	return p;
+}
kernel/bpf/syscall.c +4 -3
index 13bc96035116..b345a273f7d0 100644
@@ -2491,7 +2491,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
  				 BPF_F_TEST_STATE_FREQ |
  				 BPF_F_SLEEPABLE |
  				 BPF_F_TEST_RND_HI32 |
-				 BPF_F_XDP_HAS_FRAGS))
+				 BPF_F_XDP_HAS_FRAGS |
+				 BPF_F_XDP_HAS_METADATA))
  		return -EINVAL;
 
  	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
@@ -2575,7 +2576,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
  	prog->aux->attach_btf = attach_btf;
  	prog->aux->attach_btf_id = attr->attach_btf_id;
  	prog->aux->dst_prog = dst_prog;
-	prog->aux->offload_requested = !!attr->prog_ifindex;
+	prog->aux->dev_bound = !!attr->prog_ifindex;
  	prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
  	prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
 
@@ -2598,7 +2599,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
  	atomic64_set(&prog->aux->refcnt, 1);
  	prog->gpl_compatible = is_gpl ? 1 : 0;
 
-	if (bpf_prog_is_offloaded(prog->aux)) {
+	if (bpf_prog_is_dev_bound(prog->aux)) {
  		err = bpf_prog_offload_init(prog, attr);
  		if (err)
  			goto free_prog_sec;
kernel/bpf/verifier.c +0 -0
index fc4e313a4d2e..00951a59ee26 100644

kernel/bpf/verifier.c static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 15328

Quoted 6 lines
kernel/bpf/verifier.c +3 -0
@@ -15323,6 +15323,24 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
  		return -EINVAL;
  	}
 
+	*cnt = 0;
+
+	if (resolve_prog_type(env->prog) == BPF_PROG_TYPE_XDP) {

hmmm...does it need BPF_PROG_TYPE_XDP check? Is the below bpf_prog_is_dev_bound() and the eariler 'register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_metadata_kfunc_set)' good enough?

Quoted 15 lines
kernel/bpf/verifier.c +15 -0
+		if (bpf_prog_is_offloaded(env->prog->aux)) {
+			verbose(env, "no metadata kfuncs offload\n");
+			return -EINVAL;
+		}
+
+		if (bpf_prog_is_dev_bound(env->prog->aux)) {
+			void *p = bpf_offload_resolve_kfunc(env->prog, insn->imm);
+
+			if (p) {
+				insn->imm = BPF_CALL_IMM(p);
+				return 0;
+			}
+		}
+	}
+
#12 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 23 lines · Martin KaFai Lau
Quoted 21 lines · Stanislav Fomichev
include/net/xdp.h +13 -0
index 55dbc68bfffc..c24aba5c363b 100644
@@ -409,4 +409,33 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
 
  #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
 
+#define XDP_METADATA_KFUNC_xxx       \
+     XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP_SUPPORTED, \
+                        bpf_xdp_metadata_rx_timestamp_supported) \
+     XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
+                        bpf_xdp_metadata_rx_timestamp) \
+     XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH_SUPPORTED, \
+                        bpf_xdp_metadata_rx_hash_supported) \
+     XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
+                        bpf_xdp_metadata_rx_hash) \
+
+enum {
+#define XDP_METADATA_KFUNC(name, str) name,
+XDP_METADATA_KFUNC_xxx
Quoted 8 lines

include/net/xdp.h void xdp_attachment_setup(struct xdp_attachment_info *info, · line 429

Quoted 5 lines
include/net/xdp.h +5 -0
+#undef XDP_METADATA_KFUNC
+MAX_XDP_METADATA_KFUNC,
+};
+
+#ifdef CONFIG_NET

I think this is no longer needed because xdp_metadata_kfunc_id() is only used in offload.c which should be CONFIG_NET only.

Seems to be the case. At least my build tests with weird configs work, thank you!

Quoted 77 lines
Quoted 25 lines
include/net/xdp.h +11 -0
+u32 xdp_metadata_kfunc_id(int id);
+#else
+static inline u32 xdp_metadata_kfunc_id(int id) { return 0; }
+#endif
+
+struct xdp_md;
+bool bpf_xdp_metadata_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx);
+bool bpf_xdp_metadata_rx_hash_supported(const struct xdp_md *ctx);
+u32 bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx);
+
  #endif /* __LINUX_NET_XDP_H__ */
include/uapi/linux/bpf.h +5 -0
index f89de51a45db..790650a81f2b 100644
@@ -1156,6 +1156,11 @@ enum bpf_link_type {
   */
  #define BPF_F_XDP_HAS_FRAGS (1U << 5)
 
+/* If BPF_F_XDP_HAS_METADATA is used in BPF_PROG_LOAD command, the loaded
+ * program becomes device-bound but can access it's XDP metadata.
+ */
+#define BPF_F_XDP_HAS_METADATA       (1U << 6)
+

[ ... ]

Quoted 49 lines
kernel/bpf/offload.c +12 -2
index f5769a8ecbee..bad8bab916eb 100644
@@ -41,7 +41,7 @@ struct bpf_offload_dev {
  struct bpf_offload_netdev {
      struct rhash_head l;
      struct net_device *netdev;
-     struct bpf_offload_dev *offdev;
+     struct bpf_offload_dev *offdev; /* NULL when bound-only */
      struct list_head progs;
      struct list_head maps;
      struct list_head offdev_netdevs;
@@ -58,6 +58,12 @@ static const struct rhashtable_params offdevs_params = {
  static struct rhashtable offdevs;
  static bool offdevs_inited;
 
+static int __bpf_offload_init(void);
+static int __bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
+                                          struct net_device *netdev);
+static void __bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
+                                             struct net_device *netdev);
+
  static int bpf_dev_offload_check(struct net_device *netdev)
  {
      if (!netdev)
@@ -87,13 +93,17 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
          attr->prog_type != BPF_PROG_TYPE_XDP)
              return -EINVAL;
 
-     if (attr->prog_flags)
+     if (attr->prog_flags & ~BPF_F_XDP_HAS_METADATA)
              return -EINVAL;
 
      offload = kzalloc(sizeof(*offload), GFP_USER);
      if (!offload)
              return -ENOMEM;
 
+     err = __bpf_offload_init();
+     if (err)
+             return err;
+
      offload->prog = prog;
 
      offload->netdev = dev_get_by_index(current->nsproxy->net_ns,
@@ -102,11 +112,25 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
      if (err)
              goto err_maybe_put;
 
Quoted 8 lines

kernel/bpf/offload.c int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) · line 116

Quoted 2 lines
kernel/bpf/offload.c +2 -0
+     prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_HAS_METADATA);
+

If I read the set correctly, bpf prog can either use metadata kfunc or offload but not both. It is fine to start with only supporting metadata kfunc when there is no offload but will be useful to understand the reason. I assume an offloaded bpf prog should still be able to call the bpf helpers like adjust_head/tail and the same should go for any kfunc?

Yes, I'm assuming there should be some work on the offloaded device drivers to support metadata kfuncs.
Offloaded kfuncs, in general, seem hard (how do we call kernel func from the device-offloaded prog?); so refusing kfuncs early for the offloaded case seems fair for now?

Quoted 2 lines

Also, the BPF_F_XDP_HAS_METADATA feels like it is acting more like BPF_F_XDP_DEV_BOUND_ONLY.

SG. Seems like a better option in case in the future binding to devices might give some other nice perks besides the metadata..

Quoted 8 lines

kernel/bpf/offload.c int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) · line 120

Quoted 6 lines
kernel/bpf/offload.c +1 -2
      down_write(&bpf_devs_lock);
      ondev = bpf_offload_find_netdev(offload->netdev);
      if (!ondev) {
-             err = -EINVAL;
-             goto err_unlock;
+             if (!prog->aux->offload_requested) {

nit. bpf_prog_is_offloaded(prog->aux)

Thx!

Quoted 38 lines
Quoted 38 lines
kernel/bpf/offload.c +26 -0
+                     /* When only binding to the device, explicitly
+                      * create an entry in the hashtable. See related
+                      * maybe_remove_bound_netdev.
+                      */
+                     err = __bpf_offload_dev_netdev_register(NULL, offload->netdev);
+                     if (err)
+                             goto err_unlock;
+                     ondev = bpf_offload_find_netdev(offload->netdev);
+             }
+             if (!ondev) {
+                     err = -EINVAL;
+                     goto err_unlock;
+             }
      }
      offload->offdev = ondev->offdev;
      prog->aux->offload = offload;
@@ -209,6 +233,19 @@ bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
      up_read(&bpf_devs_lock);
  }
 
+static void maybe_remove_bound_netdev(struct net_device *dev)
+{
+     struct bpf_offload_netdev *ondev;
+
+     rtnl_lock();
+     down_write(&bpf_devs_lock);
+     ondev = bpf_offload_find_netdev(dev);
+     if (ondev && !ondev->offdev && list_empty(&ondev->progs))
+             __bpf_offload_dev_netdev_unregister(NULL, dev);
+     up_write(&bpf_devs_lock);
+     rtnl_unlock();
+}
+
  static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
  {
      struct bpf_prog_offload *offload = prog->aux->offload;
@@ -226,10 +263,17 @@ static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
 
Quoted 16 lines

kernel/bpf/offload.c static void __bpf_prog_offload_destroy(struct bpf_prog *prog) · line 275

Quoted 13 lines
kernel/bpf/offload.c +7 -1
  void bpf_prog_offload_destroy(struct bpf_prog *prog)
  {
+     struct net_device *netdev = NULL;
+
      down_write(&bpf_devs_lock);
-     if (prog->aux->offload)
+     if (prog->aux->offload) {
+             netdev = prog->aux->offload->netdev;
              __bpf_prog_offload_destroy(prog);
+     }
      up_write(&bpf_devs_lock);
+
+     if (netdev)

May be I have missed a refcnt or lock somewhere. Is it possible that netdev may have been freed?

Yeah, with the offload framework, there are no refcnts. We put an "offloaded" device into a separate hashtable (protected by rtnl/semaphore).
maybe_remove_bound_netdev will re-grab the locks (due to ordering:
rtnl->bpf_devs_lock) and remove the device from the hashtable if it's still there.
At least this is how, I think, it should work; LMK if something is still fishy here...

Or is the concern here that somebody might allocate new netdev reusing the same address? I think I have enough checks in maybe_remove_bound_netdev to guard against that. Or, at least, to make it safe :-)

Quoted 71 lines
Quoted 3 lines
kernel/bpf/offload.c +1 -0
+             maybe_remove_bound_netdev(netdev);
  }
 

[ ... ]

Quoted 65 lines
kernel/bpf/offload.c +25 -0
+void *bpf_offload_resolve_kfunc(struct bpf_prog *prog, u32 func_id)
+{
+     const struct net_device_ops *netdev_ops;
+     void *p = NULL;
+
+     down_read(&bpf_devs_lock);
+     if (!prog->aux->offload || !prog->aux->offload->netdev)
+             goto out;
+
+     netdev_ops = prog->aux->offload->netdev->netdev_ops;
+
+     if (func_id == xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_TIMESTAMP_SUPPORTED))
+             p = netdev_ops->ndo_xdp_rx_timestamp_supported;
+     else if (func_id == xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_TIMESTAMP))
+             p = netdev_ops->ndo_xdp_rx_timestamp;
+     else if (func_id == xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_HASH_SUPPORTED))
+             p = netdev_ops->ndo_xdp_rx_hash_supported;
+     else if (func_id == xdp_metadata_kfunc_id(XDP_METADATA_KFUNC_RX_HASH))
+             p = netdev_ops->ndo_xdp_rx_hash;
+     /* fallback to default kfunc when not supported by netdev */
+out:
+     up_read(&bpf_devs_lock);
+
+     return p;
+}
kernel/bpf/syscall.c +4 -3
index 13bc96035116..b345a273f7d0 100644
@@ -2491,7 +2491,8 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
                               BPF_F_TEST_STATE_FREQ |
                               BPF_F_SLEEPABLE |
                               BPF_F_TEST_RND_HI32 |
-                              BPF_F_XDP_HAS_FRAGS))
+                              BPF_F_XDP_HAS_FRAGS |
+                              BPF_F_XDP_HAS_METADATA))
              return -EINVAL;
 
      if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
@@ -2575,7 +2576,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
      prog->aux->attach_btf = attach_btf;
      prog->aux->attach_btf_id = attr->attach_btf_id;
      prog->aux->dst_prog = dst_prog;
-     prog->aux->offload_requested = !!attr->prog_ifindex;
+     prog->aux->dev_bound = !!attr->prog_ifindex;
      prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
      prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
 
@@ -2598,7 +2599,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
      atomic64_set(&prog->aux->refcnt, 1);
      prog->gpl_compatible = is_gpl ? 1 : 0;
 
-     if (bpf_prog_is_offloaded(prog->aux)) {
+     if (bpf_prog_is_dev_bound(prog->aux)) {
              err = bpf_prog_offload_init(prog, attr);
              if (err)
                      goto free_prog_sec;
kernel/bpf/verifier.c +0 -0
index fc4e313a4d2e..00951a59ee26 100644
@@ -15323,6 +15323,24 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
              return -EINVAL;
      }
 
Quoted 7 lines

kernel/bpf/verifier.c static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 15328

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+     *cnt = 0;
+
+     if (resolve_prog_type(env->prog) == BPF_PROG_TYPE_XDP) {

hmmm...does it need BPF_PROG_TYPE_XDP check? Is the below bpf_prog_is_dev_bound() and the eariler 'register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP, &xdp_metadata_kfunc_set)' good enough?

Should be enough, yeah, will drop. I was being a bit defensive here in case we have non-xdp device-bound programs in the future.

Quoted 17 lines
Quoted 15 lines
kernel/bpf/verifier.c +15 -0
+             if (bpf_prog_is_offloaded(env->prog->aux)) {
+                     verbose(env, "no metadata kfuncs offload\n");
+                     return -EINVAL;
+             }
+
+             if (bpf_prog_is_dev_bound(env->prog->aux)) {
+                     void *p = bpf_offload_resolve_kfunc(env->prog, insn->imm);
+
+                     if (p) {
+                             insn->imm = BPF_CALL_IMM(p);
+                             return 0;
+                     }
+             }
+     }
+
#13 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 13 lines · Stanislav Fomichev
Quoted 4 lines
Quoted 4 lines
kernel/bpf/offload.c +0 -0
@@ -102,11 +112,25 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
       if (err)
               goto err_maybe_put;
 
Quoted 8 lines

kernel/bpf/offload.c int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) · line 116

Quoted 2 lines
kernel/bpf/offload.c +2 -0
+     prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_HAS_METADATA);
+

If I read the set correctly, bpf prog can either use metadata kfunc or offload but not both. It is fine to start with only supporting metadata kfunc when there is no offload but will be useful to understand the reason. I assume an offloaded bpf prog should still be able to call the bpf helpers like adjust_head/tail and the same should go for any kfunc?

Quoted 5 lines

Yes, I'm assuming there should be some work on the offloaded device drivers to support metadata kfuncs.
Offloaded kfuncs, in general, seem hard (how do we call kernel func from the device-offloaded prog?); so refusing kfuncs early for the offloaded case seems fair for now?

Ah, ok. I was actually thinking the HW offloaded prog can just use the software ndo_* kfunc (like other bpf-helpers). From skimming some bpf_prog_offload_ops:prepare implementation, I think you are right and it seems BPF_PSEUDO_KFUNC_CALL has not been recognized yet.

[ ... ]

Quoted 28 lines
Quoted 18 lines
Quoted 5 lines
kernel/bpf/offload.c +1 -0
@@ -226,10 +263,17 @@ static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
 
   void bpf_prog_offload_destroy(struct bpf_prog *prog)
   {
+     struct net_device *netdev = NULL;

kernel/bpf/offload.c static void __bpf_prog_offload_destroy(struct bpf_prog *prog) · line 275

Quoted 10 lines
kernel/bpf/offload.c +6 -1
+
       down_write(&bpf_devs_lock);
-     if (prog->aux->offload)
+     if (prog->aux->offload) {
+             netdev = prog->aux->offload->netdev;
               __bpf_prog_offload_destroy(prog);
+     }
       up_write(&bpf_devs_lock);
+
+     if (netdev)

May be I have missed a refcnt or lock somewhere. Is it possible that netdev may have been freed?

Yeah, with the offload framework, there are no refcnts. We put an "offloaded" device into a separate hashtable (protected by rtnl/semaphore).
maybe_remove_bound_netdev will re-grab the locks (due to ordering:
rtnl->bpf_devs_lock) and remove the device from the hashtable if it's still there.
At least this is how, I think, it should work; LMK if something is still fishy here...

Quoted 4 lines

Or is the concern here that somebody might allocate new netdev reusing the same address? I think I have enough checks in maybe_remove_bound_netdev to guard against that. Or, at least, to make it safe :-)

Race is ok because ondev needs to be removed anyway when '!ondev->offdev && list_empty(&ondev->progs)'? hmmm... tricky, please add a comment. :)

Why it cannot be done together in the bpf_devs_lock above? The above cannot take an extra rtnl_lock before bpf_devs_lock?

#14 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 61 lines · Martin KaFai Lau
Quoted 13 lines · Stanislav Fomichev
Quoted 4 lines
Quoted 4 lines
kernel/bpf/offload.c +0 -0
@@ -102,11 +112,25 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
       if (err)
               goto err_maybe_put;
 
Quoted 8 lines

kernel/bpf/offload.c int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) · line 116

Quoted 2 lines
kernel/bpf/offload.c +2 -0
+     prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_HAS_METADATA);
+

If I read the set correctly, bpf prog can either use metadata kfunc or offload but not both. It is fine to start with only supporting metadata kfunc when there is no offload but will be useful to understand the reason. I assume an offloaded bpf prog should still be able to call the bpf helpers like adjust_head/tail and the same should go for any kfunc?

Quoted 5 lines

Yes, I'm assuming there should be some work on the offloaded device drivers to support metadata kfuncs.
Offloaded kfuncs, in general, seem hard (how do we call kernel func from the device-offloaded prog?); so refusing kfuncs early for the offloaded case seems fair for now?

Ah, ok. I was actually thinking the HW offloaded prog can just use the software ndo_* kfunc (like other bpf-helpers). From skimming some bpf_prog_offload_ops:prepare implementation, I think you are right and it seems BPF_PSEUDO_KFUNC_CALL has not been recognized yet.

[ ... ]

Quoted 28 lines
Quoted 18 lines
Quoted 5 lines
kernel/bpf/offload.c +1 -0
@@ -226,10 +263,17 @@ static void __bpf_prog_offload_destroy(struct bpf_prog *prog)
 
   void bpf_prog_offload_destroy(struct bpf_prog *prog)
   {
+     struct net_device *netdev = NULL;

kernel/bpf/offload.c static void __bpf_prog_offload_destroy(struct bpf_prog *prog) · line 275

Quoted 10 lines
kernel/bpf/offload.c +6 -1
+
       down_write(&bpf_devs_lock);
-     if (prog->aux->offload)
+     if (prog->aux->offload) {
+             netdev = prog->aux->offload->netdev;
               __bpf_prog_offload_destroy(prog);
+     }
       up_write(&bpf_devs_lock);
+
+     if (netdev)

May be I have missed a refcnt or lock somewhere. Is it possible that netdev may have been freed?

Yeah, with the offload framework, there are no refcnts. We put an "offloaded" device into a separate hashtable (protected by rtnl/semaphore).
maybe_remove_bound_netdev will re-grab the locks (due to ordering:
rtnl->bpf_devs_lock) and remove the device from the hashtable if it's still there.
At least this is how, I think, it should work; LMK if something is still fishy here...

Quoted 4 lines

Or is the concern here that somebody might allocate new netdev reusing the same address? I think I have enough checks in maybe_remove_bound_netdev to guard against that. Or, at least, to make it safe :-)

Quoted 5 lines

Race is ok because ondev needs to be removed anyway when '!ondev->offdev && list_empty(&ondev->progs)'? hmmm... tricky, please add a comment. :)

Why it cannot be done together in the bpf_devs_lock above? The above cannot take an extra rtnl_lock before bpf_devs_lock?

Hm, let's take an extra rtln to avoid this complexity, agree. I guess I was trying to avoid taking it, but this path is still 'dev_bound == true' protected, so shouldn't affect the rest of the progs.

maintainer
#15 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

In reply to Stanislav Fomichev

The offload tests still pass after this, right?
TBH I don't remember this code well enough to spot major issues.

Quoted 12 lines · On Mon, 5 Dec 2022 18:45:45 -0800 Stanislav Fomichev wrote:

There is an ndo handler per kfunc, the verifier replaces a call to the generic kfunc with a call to the per-device one.

For XDP, we define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible metatada kfuncs. Not all devices have to implement them. If kfunc is not supported by the target device, the default implementation is called instead.

Upon loading, if BPF_F_XDP_HAS_METADATA is passed via prog_flags, we treat prog_index as target device for kfunc resolution.

include/linux/bpf.h void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, · line 2480

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -2476,10 +2477,18 @@ void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
 				       struct net_device *netdev);
 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
 
+void *bpf_offload_resolve_kfunc(struct bpf_prog *prog, u32 func_id);

There seems to be some mis-naming going on. I expected:

offloaded =~ nfp
dev_bound == XDP w/ funcs

*_offload_resolve_kfunc looks misnamed? Unless you want to resolve for HW offload?

include/linux/bpf.h void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, · line 2486

Quoted 5 lines
include/linux/bpf.h +1 -0
 void unpriv_ebpf_notify(int new_state);
 
 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
+void bpf_offload_bound_netdev_unregister(struct net_device *dev);

ditto: offload_bound is a mix of terms no?

include/linux/netdevice.h struct net_device_ops { · line 1619

Quoted 9 lines
include/linux/netdevice.h +4 -0
@@ -1611,6 +1612,10 @@ struct net_device_ops {
 	ktime_t			(*ndo_get_tstamp)(struct net_device *dev,
 						  const struct skb_shared_hwtstamps *hwtstamps,
 						  bool cycles);
+	bool			(*ndo_xdp_rx_timestamp_supported)(const struct xdp_md *ctx);
+	u64			(*ndo_xdp_rx_timestamp)(const struct xdp_md *ctx);
+	bool			(*ndo_xdp_rx_hash_supported)(const struct xdp_md *ctx);
+	u32			(*ndo_xdp_rx_hash)(const struct xdp_md *ctx);
 };

Is this on the fast path? Can we do an indirection?
Put these ops in their own struct and add a pointer to that struct in net_device_ops? Purely for grouping reasons because the netdev ops are getting orders of magnitude past the size where you can actually find stuff in this struct.

kernel/bpf/core.c static void bpf_prog_free_deferred(struct work_struct *work) · line 2558

Quoted 5 lines
kernel/bpf/core.c +1 -1
 	bpf_free_used_maps(aux);
 	bpf_free_used_btfs(aux);
-	if (bpf_prog_is_offloaded(aux))
+	if (bpf_prog_is_dev_bound(aux))
 		bpf_prog_offload_destroy(aux->prog);

This also looks a touch like a mix of terms (condition vs function called).

kernel/bpf/offload.c static const struct rhashtable_params offdevs_params = { · line 65

Quoted 5 lines
kernel/bpf/offload.c +5 -0
+static int __bpf_offload_init(void);
+static int __bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
+					     struct net_device *netdev);
+static void __bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
+						struct net_device *netdev);

fwd declarations are yuck

Quoted 8 lines

static int bpf_dev_offload_check(struct net_device *netdev) {

kernel/bpf/offload.c +0 -1
 	if (!netdev)
@@ -87,13 +93,17 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
 	    attr->prog_type != BPF_PROG_TYPE_XDP)
 		return -EINVAL;
 
-	if (attr->prog_flags)

kernel/bpf/offload.c int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) · line 105

Quoted 10 lines
kernel/bpf/offload.c +4 -0
+	if (attr->prog_flags & ~BPF_F_XDP_HAS_METADATA)
 		return -EINVAL;
 
 	offload = kzalloc(sizeof(*offload), GFP_USER);
 	if (!offload)
 		return -ENOMEM;
 
+	err = __bpf_offload_init();
+	if (err)
+		return err;

leaks offload

kernel/bpf/offload.c bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) · line 237

Quoted 6 lines
kernel/bpf/offload.c +2 -0
@@ -209,6 +233,19 @@ bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
 	up_read(&bpf_devs_lock);
 }
 
+static void maybe_remove_bound_netdev(struct net_device *dev)
+{

func name prefix ?

Quoted 11 lines
kernel/bpf/offload.c +1 -3
-struct bpf_offload_dev *
-bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv)
+static int __bpf_offload_init(void)
 {
-	struct bpf_offload_dev *offdev;
 	int err;
 
 	down_write(&bpf_devs_lock);
@@ -680,12 +740,25 @@ bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv)
 		err = rhashtable_init(&offdevs, &offdevs_params);
 		if (err) {

kernel/bpf/offload.c bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv) · line 750

Quoted 10 lines
kernel/bpf/offload.c +3 -1
 			up_write(&bpf_devs_lock);
-			return ERR_PTR(err);
+			return err;
 		}
 		offdevs_inited = true;
 	}
 	up_write(&bpf_devs_lock);
 
+	return 0;
+}

Would late_initcall() or some such not work for this?

net/core/dev.c static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack · line 9228

Quoted 6 lines
net/core/dev.c +0 -0
index 5b221568dfd4..862e03fcffa6 100644
@@ -9228,6 +9228,10 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
 			NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");

extack should get updated here, I reckon, maybe in previous patch

net/core/dev.c static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack · line 9231

Quoted 3 lines
net/core/dev.c +1 -0
 			return -EINVAL;
 		}
+		if (bpf_prog_is_dev_bound(new_prog->aux) && !bpf_offload_dev_match(new_prog, dev)) {

bound_dev_match() ?

net/core/dev.c static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack · line 9232

Quoted 1 line
net/core/dev.c +1 -0
+			NL_SET_ERR_MSG(extack, "Cannot attach to a different target device");

different than.. ?

Quoted 5 lines
net/core/dev.c +2 -0
+			return -EINVAL;
+		}
 		if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
 			NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
 			return -EINVAL;
#16 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 2 lines · Jakub Kicinski

The offload tests still pass after this, right?

Yeah, had to bring them back in shape just for the purpose of making sure they're still happy:
https://lore.kernel.org/bpf/[email protected]/

Quoted 1 line

TBH I don't remember this code well enough to spot major issues.

No worries! Appreciate the review and the comments on consistency; I'm also mostly unaware how this whole offloading works :-)

Quoted 20 lines · On Mon, 5 Dec 2022 18:45:45 -0800 Stanislav Fomichev wrote:
Quoted 12 lines

There is an ndo handler per kfunc, the verifier replaces a call to the generic kfunc with a call to the per-device one.

For XDP, we define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible metatada kfuncs. Not all devices have to implement them. If kfunc is not supported by the target device, the default implementation is called instead.

Upon loading, if BPF_F_XDP_HAS_METADATA is passed via prog_flags, we treat prog_index as target device for kfunc resolution.

include/linux/bpf.h void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, · line 2480

Quoted 4 lines
include/linux/bpf.h +1 -0
@@ -2476,10 +2477,18 @@ void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
                                     struct net_device *netdev);
 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
 
+void *bpf_offload_resolve_kfunc(struct bpf_prog *prog, u32 func_id);

There seems to be some mis-naming going on. I expected:

Quoted 5 lines

offloaded =~ nfp
dev_bound == XDP w/ funcs

*_offload_resolve_kfunc looks misnamed? Unless you want to resolve for HW offload?

Yeah, I had the same expectations, but I was also assuming that this bpf_offload_resolve_kfunc might also at some point handle offloaded metadata kfuncs.
But looking at it again, agree that the following looks a bit off:

if (bpf_prog_is_dev_bound()) {
xxx = bpf_offload_resolve_kfunc()
}

Let me use the dev_bound prefix more consistently here and in the other places you've pointed out.

Quoted 7 lines

include/linux/bpf.h void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, · line 2486

Quoted 5 lines
include/linux/bpf.h +1 -0
 void unpriv_ebpf_notify(int new_state);
 
 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
+void bpf_offload_bound_netdev_unregister(struct net_device *dev);

ditto: offload_bound is a mix of terms no?

Ack, will do bpf_dev_bound_netdev_unregister here, thanks!

Quoted 11 lines

include/linux/netdevice.h struct net_device_ops { · line 1619

Quoted 9 lines
include/linux/netdevice.h +4 -0
@@ -1611,6 +1612,10 @@ struct net_device_ops {
      ktime_t                 (*ndo_get_tstamp)(struct net_device *dev,
                                                const struct skb_shared_hwtstamps *hwtstamps,
                                                bool cycles);
+     bool                    (*ndo_xdp_rx_timestamp_supported)(const struct xdp_md *ctx);
+     u64                     (*ndo_xdp_rx_timestamp)(const struct xdp_md *ctx);
+     bool                    (*ndo_xdp_rx_hash_supported)(const struct xdp_md *ctx);
+     u32                     (*ndo_xdp_rx_hash)(const struct xdp_md *ctx);
 };

Is this on the fast path? Can we do an indirection?

No, we resolve them at load time from "generic"
bpf_xdp_metadata_rx_<xxx> to ndo_xdp_rx_<xxx>.

Quoted 4 lines

Put these ops in their own struct and add a pointer to that struct in net_device_ops? Purely for grouping reasons because the netdev ops are getting orders of magnitude past the size where you can actually find stuff in this struct.

Oh, great idea, will do!

Quoted 8 lines

kernel/bpf/core.c static void bpf_prog_free_deferred(struct work_struct *work) · line 2558

Quoted 5 lines
kernel/bpf/core.c +1 -1
      bpf_free_used_maps(aux);
      bpf_free_used_btfs(aux);
-     if (bpf_prog_is_offloaded(aux))
+     if (bpf_prog_is_dev_bound(aux))
              bpf_prog_offload_destroy(aux->prog);

This also looks a touch like a mix of terms (condition vs function called).

Here, not sure, open to suggestions. These bpf_prog_offload_init/bpf_prog_offload_destroy are generic enough (now) that I'm calling them for both dev_bound/offloaded.

The following paths trigger for both offloaded/dev_bound cases:

if (bpf_prog_is_dev_bound()) bpf_prog_offload_init(); if (bpf_prog_is_dev_bound()) bpf_prog_offload_destroy();

Do you think it's worth it having completely separate dev_bound/offloaded paths? Or, alternatively, can rename to bpf_prog_dev_bound_{init,destroy} but still handle both cases?

Quoted 7 lines

kernel/bpf/offload.c static const struct rhashtable_params offdevs_params = { · line 65

Quoted 5 lines
kernel/bpf/offload.c +5 -0
+static int __bpf_offload_init(void);
+static int __bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
+                                          struct net_device *netdev);
+static void __bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
+                                             struct net_device *netdev);

fwd declarations are yuck

SG, will move them here instead.

Quoted 15 lines
Quoted 15 lines

static int bpf_dev_offload_check(struct net_device *netdev) {
if (!netdev)

kernel/bpf/offload.c +1 -1
@@ -87,13 +93,17 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
          attr->prog_type != BPF_PROG_TYPE_XDP)
              return -EINVAL;
 
-     if (attr->prog_flags)
+     if (attr->prog_flags & ~BPF_F_XDP_HAS_METADATA)
              return -EINVAL;
 
      offload = kzalloc(sizeof(*offload), GFP_USER);
      if (!offload)
              return -ENOMEM;
 
Quoted 5 lines

kernel/bpf/offload.c int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) · line 105

Quoted 3 lines
kernel/bpf/offload.c +3 -0
+     err = __bpf_offload_init();
+     if (err)
+             return err;

leaks offload

Oops, let me actually move this to late_initcall as you suggest below.

Quoted 8 lines

kernel/bpf/offload.c bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) · line 237

Quoted 6 lines
kernel/bpf/offload.c +2 -0
@@ -209,6 +233,19 @@ bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt)
      up_read(&bpf_devs_lock);
 }
 
+static void maybe_remove_bound_netdev(struct net_device *dev)
+{

func name prefix ?

Good point, will rename to bpf_dev_bound_try_remove_netdev.

Quoted 19 lines
Quoted 19 lines
kernel/bpf/offload.c +2 -4
-struct bpf_offload_dev *
-bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv)
+static int __bpf_offload_init(void)
 {
-     struct bpf_offload_dev *offdev;
      int err;
 
      down_write(&bpf_devs_lock);
@@ -680,12 +740,25 @@ bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv)
              err = rhashtable_init(&offdevs, &offdevs_params);
              if (err) {
                      up_write(&bpf_devs_lock);
-                     return ERR_PTR(err);
+                     return err;
              }
              offdevs_inited = true;
      }
      up_write(&bpf_devs_lock);
 
Quoted 4 lines

Review bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv) · line 750

Quoted 2 lines
(quoted diff) +2 -0
+     return 0;
+}

Would late_initcall() or some such not work for this?

Agreed, let's move it to the initcall instead.

Quoted 8 lines

net/core/dev.c static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack · line 9228

Quoted 6 lines
net/core/dev.c +0 -0
index 5b221568dfd4..862e03fcffa6 100644
@@ -9228,6 +9228,10 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
                      NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE flag is not supported");

extack should get updated here, I reckon, maybe in previous patch

Oh, thanks for spotting, will fix.

Quoted 5 lines

net/core/dev.c static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack · line 9231

Quoted 3 lines
net/core/dev.c +1 -0
                      return -EINVAL;
              }
+             if (bpf_prog_is_dev_bound(new_prog->aux) && !bpf_offload_dev_match(new_prog, dev)) {

bound_dev_match() ?

Right, so this is another case where it works for both cases. Maybe rename to bpf_dev_bound_match and use for both offloaded/dev_bound? Or do you prefer completely separate paths?

Quoted 3 lines

net/core/dev.c static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack · line 9232

Quoted 1 line
net/core/dev.c +1 -0
+                     NL_SET_ERR_MSG(extack, "Cannot attach to a different target device");

different than.. ?

Borrowing from netdevsim, lmk if the following won't work here:

"Program bound to different device"

Quoted 5 lines
Quoted 5 lines
net/core/dev.c +2 -0
+                     return -EINVAL;
+             }
              if (new_prog->expected_attach_type == BPF_XDP_DEVMAP) {
                      NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached to a device");
                      return -EINVAL;
maintainer
#17 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 13 lines · On Thu, 8 Dec 2022 11:07:43 -0800 Stanislav Fomichev wrote:
Quoted 8 lines

kernel/bpf/core.c static void bpf_prog_free_deferred(struct work_struct *work) · line 2558

Quoted 5 lines
      bpf_free_used_maps(aux);
      bpf_free_used_btfs(aux);
kernel/bpf/core.c +1 -1
-     if (bpf_prog_is_offloaded(aux))
+     if (bpf_prog_is_dev_bound(aux))
              bpf_prog_offload_destroy(aux->prog);  

This also looks a touch like a mix of terms (condition vs function called).

Here, not sure, open to suggestions. These bpf_prog_offload_init/bpf_prog_offload_destroy are generic enough (now) that I'm calling them for both dev_bound/offloaded.

Quoted 8 lines

The following paths trigger for both offloaded/dev_bound cases:

if (bpf_prog_is_dev_bound()) bpf_prog_offload_init(); if (bpf_prog_is_dev_bound()) bpf_prog_offload_destroy();

Do you think it's worth it having completely separate dev_bound/offloaded paths? Or, alternatively, can rename to bpf_prog_dev_bound_{init,destroy} but still handle both cases?

Any offload should be bound, right? So I think functions which handle both can use the bound naming scheme, only the offload-specific ones should explicitly use offload?

#18 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 20 lines · Jakub Kicinski
Quoted 18 lines · On Thu, 8 Dec 2022 11:07:43 -0800 Stanislav Fomichev wrote:
Quoted 8 lines

kernel/bpf/core.c static void bpf_prog_free_deferred(struct work_struct *work) · line 2558

Quoted 5 lines
      bpf_free_used_maps(aux);
      bpf_free_used_btfs(aux);
kernel/bpf/core.c +1 -1
-     if (bpf_prog_is_offloaded(aux))
+     if (bpf_prog_is_dev_bound(aux))
              bpf_prog_offload_destroy(aux->prog);

This also looks a touch like a mix of terms (condition vs function called).

Here, not sure, open to suggestions. These bpf_prog_offload_init/bpf_prog_offload_destroy are generic enough (now) that I'm calling them for both dev_bound/offloaded.

The following paths trigger for both offloaded/dev_bound cases:

if (bpf_prog_is_dev_bound()) bpf_prog_offload_init(); if (bpf_prog_is_dev_bound()) bpf_prog_offload_destroy();

Quoted 7 lines
Quoted 3 lines

Do you think it's worth it having completely separate dev_bound/offloaded paths? Or, alternatively, can rename to bpf_prog_dev_bound_{init,destroy} but still handle both cases?

Any offload should be bound, right? So I think functions which handle both can use the bound naming scheme, only the offload-specific ones should explicitly use offload?

Agreed. Will rename the common ones to dev_offload!

#19 lore raw

Re: [xdp-hints] [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

In reply to Stanislav Fomichev
Quoted 7 lines · Stanislav Fomichev

There is an ndo handler per kfunc, the verifier replaces a call to the generic kfunc with a call to the per-device one.

For XDP, we define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible metatada kfuncs. Not all devices have to implement them. If kfunc is not supported by the target device, the default implementation is called instead.

So one unfortunate consequence of this "fallback to the default implementation" is that it's really easy to get a step wrong and end up with something that doesn't work. Specifically, if you load an XDP program that calls the metadata kfuncs, but don't set the ifindex and flag on load, the kfunc resolution will work just fine, but you'll end up calling the default kfunc implementations (and get no data). I ran into this multiple times just now when playing around with it and implementing the freplace support.

So I really think it would be a better user experience if we completely block (with a nice error message!) the calling of the metadata kfuncs if the program is not device-bound...

Another UX thing I ran into is that libbpf will bail out if it can't find the kfunc in the kernel vmlinux, even if the code calling the function is behind an always-false if statement (which would be eliminated as dead code from the verifier). This makes it a bit hard to conditionally use them. Should libbpf just allow the load without performing the relocation (and let the verifier worry about it), or should we have a bpf_core_kfunc_exists() macro to use for checking? Maybe both?

Quoted 2 lines

Upon loading, if BPF_F_XDP_HAS_METADATA is passed via prog_flags, we treat prog_index as target device for kfunc resolution.

[...]

kernel/bpf/arraymap.c static void *prog_fd_array_get_ptr(struct bpf_map *map, · line 892

Quoted 13 lines
kernel/bpf/arraymap.c +9 -4
-	if (!bpf_prog_map_compatible(map, prog)) {
-		bpf_prog_put(prog);
-		return ERR_PTR(-EINVAL);
-	}
+	/* When tail-calling from a non-dev-bound program to a dev-bound one,
+	 * XDP metadata helpers should be disabled. Until it's implemented,
+	 * prohibit adding dev-bound programs to tail-call maps.
+	 */
+	if (bpf_prog_is_dev_bound(prog->aux))
+		goto err;
+
+	if (!bpf_prog_map_compatible(map, prog))
+		goto err;

I think it's better to move the new check into bpf_prog_map_compatible() itself; that way it'll cover cpumaps and devmaps as well :)

-Toke

#20 lore raw

Re: [xdp-hints] [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 20 lines · Toke Høiland-Jørgensen
Quoted 7 lines · Stanislav Fomichev

There is an ndo handler per kfunc, the verifier replaces a call to the generic kfunc with a call to the per-device one.

For XDP, we define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible metatada kfuncs. Not all devices have to implement them. If kfunc is not supported by the target device, the default implementation is called instead.

So one unfortunate consequence of this "fallback to the default implementation" is that it's really easy to get a step wrong and end up with something that doesn't work. Specifically, if you load an XDP program that calls the metadata kfuncs, but don't set the ifindex and flag on load, the kfunc resolution will work just fine, but you'll end up calling the default kfunc implementations (and get no data). I ran into this multiple times just now when playing around with it and implementing the freplace support.

Quoted 3 lines

So I really think it would be a better user experience if we completely block (with a nice error message!) the calling of the metadata kfuncs if the program is not device-bound...

Oh, right, that's a good point. Having defaults for dev-bound only makes total sense.

Quoted 8 lines

Another UX thing I ran into is that libbpf will bail out if it can't find the kfunc in the kernel vmlinux, even if the code calling the function is behind an always-false if statement (which would be eliminated as dead code from the verifier). This makes it a bit hard to conditionally use them. Should libbpf just allow the load without performing the relocation (and let the verifier worry about it), or should we have a bpf_core_kfunc_exists() macro to use for checking? Maybe both?

I'm not sure how libbpf can allow the load without performing the relocation; maybe I'm missing something.
IIUC, libbpf uses the kfunc name (from the relocation?) and replaces it with the kfunc id, right?

Having bpf_core_kfunc_exists would help, but this probably needs compiler work first to preserve some of the kfunc traces in vmlinux.h?

So yeah, I don't have any good ideas/suggestions here on how to make it all magically work :-(

Quoted 19 lines
Quoted 2 lines

Upon loading, if BPF_F_XDP_HAS_METADATA is passed via prog_flags, we treat prog_index as target device for kfunc resolution.

[...]

kernel/bpf/arraymap.c static void *prog_fd_array_get_ptr(struct bpf_map *map, · line 892

Quoted 13 lines
kernel/bpf/arraymap.c +9 -4
-     if (!bpf_prog_map_compatible(map, prog)) {
-             bpf_prog_put(prog);
-             return ERR_PTR(-EINVAL);
-     }
+     /* When tail-calling from a non-dev-bound program to a dev-bound one,
+      * XDP metadata helpers should be disabled. Until it's implemented,
+      * prohibit adding dev-bound programs to tail-call maps.
+      */
+     if (bpf_prog_is_dev_bound(prog->aux))
+             goto err;
+
+     if (!bpf_prog_map_compatible(map, prog))
+             goto err;
Quoted 2 lines

I think it's better to move the new check into bpf_prog_map_compatible() itself; that way it'll cover cpumaps and devmaps as well :)

Will do, thanks!

Quoted 2 lines

-Toke

#21 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 9 lines · Stanislav Fomichev
Quoted 8 lines

Another UX thing I ran into is that libbpf will bail out if it can't find the kfunc in the kernel vmlinux, even if the code calling the function is behind an always-false if statement (which would be eliminated as dead code from the verifier). This makes it a bit hard to conditionally use them. Should libbpf just allow the load without performing the relocation (and let the verifier worry about it), or should we have a bpf_core_kfunc_exists() macro to use for checking? Maybe both?

Quoted 4 lines

I'm not sure how libbpf can allow the load without performing the relocation; maybe I'm missing something.
IIUC, libbpf uses the kfunc name (from the relocation?) and replaces it with the kfunc id, right?

Yeah, so if it can't find the kfunc in vmlinux, just write an id of 0. This will trip the check at the top of fixup_kfunc_call() in the verifier, but if the code is hidden behind an always-false branch (an rodata variable set to zero, say) the instructions should get eliminated before they reach that point. That way you can at least turn it off at runtime (after having done some kind of feature detection) without having to compile it out of your program entirely.

Quoted 2 lines

Having bpf_core_kfunc_exists would help, but this probably needs compiler work first to preserve some of the kfunc traces in vmlinux.h?

I am not sure how the existing macros work, TBH. Hopefully someone else can chime in :)

-Toke

#22 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 25 lines · Toke Høiland-Jørgensen
Quoted 9 lines · Stanislav Fomichev
Quoted 8 lines

Another UX thing I ran into is that libbpf will bail out if it can't find the kfunc in the kernel vmlinux, even if the code calling the function is behind an always-false if statement (which would be eliminated as dead code from the verifier). This makes it a bit hard to conditionally use them. Should libbpf just allow the load without performing the relocation (and let the verifier worry about it), or should we have a bpf_core_kfunc_exists() macro to use for checking? Maybe both?

Quoted 4 lines

I'm not sure how libbpf can allow the load without performing the relocation; maybe I'm missing something.
IIUC, libbpf uses the kfunc name (from the relocation?) and replaces it with the kfunc id, right?

Yeah, so if it can't find the kfunc in vmlinux, just write an id of 0. This will trip the check at the top of fixup_kfunc_call() in the verifier, but if the code is hidden behind an always-false branch (an rodata variable set to zero, say) the instructions should get eliminated before they reach that point. That way you can at least turn it off at runtime (after having done some kind of feature detection) without having to compile it out of your program entirely.

Quoted 5 lines
Quoted 2 lines

Having bpf_core_kfunc_exists would help, but this probably needs compiler work first to preserve some of the kfunc traces in vmlinux.h?

I am not sure how the existing macros work, TBH. Hopefully someone else can chime in :)

+1

I think we need to poke Andrii as a follow up :-)

Quoted 2 lines

-Toke

#23 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 26 lines · Stanislav Fomichev
Quoted 25 lines · Toke Høiland-Jørgensen
Quoted 9 lines · Stanislav Fomichev
Quoted 8 lines

Another UX thing I ran into is that libbpf will bail out if it can't find the kfunc in the kernel vmlinux, even if the code calling the function is behind an always-false if statement (which would be eliminated as dead code from the verifier). This makes it a bit hard to conditionally use them. Should libbpf just allow the load without performing the relocation (and let the verifier worry about it), or should we have a bpf_core_kfunc_exists() macro to use for checking? Maybe both?

Quoted 4 lines

I'm not sure how libbpf can allow the load without performing the relocation; maybe I'm missing something.
IIUC, libbpf uses the kfunc name (from the relocation?) and replaces it with the kfunc id, right?

Yeah, so if it can't find the kfunc in vmlinux, just write an id of 0. This will trip the check at the top of fixup_kfunc_call() in the verifier, but if the code is hidden behind an always-false branch (an rodata variable set to zero, say) the instructions should get eliminated before they reach that point. That way you can at least turn it off at runtime (after having done some kind of feature detection) without having to compile it out of your program entirely.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Having bpf_core_kfunc_exists would help, but this probably needs compiler work first to preserve some of the kfunc traces in vmlinux.h?

hmm.... if I follow correctly, it wants the libbpf to accept a bpf prog using a kfunc that does not exist in the running kernel?

Have you tried "__weak":

extern void dummy_kfunc(void) __ksym __weak;

SEC("tc")
int load(struct __sk_buff *skb)

{
	if (dummy_kfunc) {
		dummy_kfunc();
		return TC_ACT_SHOT;
	}
	return TC_ACT_UNSPEC;
}
#24 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 32 lines · Martin KaFai Lau
Quoted 26 lines · Stanislav Fomichev
Quoted 25 lines · Toke Høiland-Jørgensen
Quoted 9 lines · Stanislav Fomichev
Quoted 8 lines

Another UX thing I ran into is that libbpf will bail out if it can't find the kfunc in the kernel vmlinux, even if the code calling the function is behind an always-false if statement (which would be eliminated as dead code from the verifier). This makes it a bit hard to conditionally use them. Should libbpf just allow the load without performing the relocation (and let the verifier worry about it), or should we have a bpf_core_kfunc_exists() macro to use for checking? Maybe both?

Quoted 4 lines

I'm not sure how libbpf can allow the load without performing the relocation; maybe I'm missing something.
IIUC, libbpf uses the kfunc name (from the relocation?) and replaces it with the kfunc id, right?

Yeah, so if it can't find the kfunc in vmlinux, just write an id of 0. This will trip the check at the top of fixup_kfunc_call() in the verifier, but if the code is hidden behind an always-false branch (an rodata variable set to zero, say) the instructions should get eliminated before they reach that point. That way you can at least turn it off at runtime (after having done some kind of feature detection) without having to compile it out of your program entirely.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Having bpf_core_kfunc_exists would help, but this probably needs compiler work first to preserve some of the kfunc traces in vmlinux.h?

hmm.... if I follow correctly, it wants the libbpf to accept a bpf prog using a kfunc that does not exist in the running kernel?

Quoted 8 lines

Have you tried "__weak":

extern void dummy_kfunc(void) __ksym __weak;

SEC("tc")
int load(struct __sk_buff *skb)
{
    if (dummy_kfunc) {

Sadly, won't work. only VAR is supported on ld.

Quoted 6 lines

        dummy_kfunc();
        return TC_ACT_SHOT;
    }
    return TC_ACT_UNSPEC;
}

#25 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

In reply to Stanislav Fomichev
Quoted 11 lines · Stanislav Fomichev

There is an ndo handler per kfunc, the verifier replaces a call to the generic kfunc with a call to the per-device one.

For XDP, we define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible metatada kfuncs. Not all devices have to implement them. If kfunc is not supported by the target device, the default implementation is called instead.

Upon loading, if BPF_F_XDP_HAS_METADATA is passed via prog_flags, we treat prog_index as target device for kfunc resolution.

[...cut...]

Quoted 13 lines
include/linux/netdevice.h +1 -0
index 5aa35c58c342..2eabb9157767 100644
@@ -74,6 +74,7 @@ struct udp_tunnel_nic_info;
  struct udp_tunnel_nic;
  struct bpf_prog;
  struct xdp_buff;
+struct xdp_md;
 
  void synchronize_net(void);
  void netdev_set_default_ethtool_ops(struct net_device *dev,

include/linux/netdevice.h struct net_device_ops { · line 1620

Quoted 9 lines
include/linux/netdevice.h +4 -0
@@ -1611,6 +1612,10 @@ struct net_device_ops {
  	ktime_t			(*ndo_get_tstamp)(struct net_device *dev,
  						  const struct skb_shared_hwtstamps *hwtstamps,
  						  bool cycles);
+	bool			(*ndo_xdp_rx_timestamp_supported)(const struct xdp_md *ctx);
+	u64			(*ndo_xdp_rx_timestamp)(const struct xdp_md *ctx);
+	bool			(*ndo_xdp_rx_hash_supported)(const struct xdp_md *ctx);
+	u32			(*ndo_xdp_rx_hash)(const struct xdp_md *ctx);
  };
 

Would it make sense to add a 'flags' parameter to ndo_xdp_rx_timestamp and ndo_xdp_rx_hash ?

E.g. we could have a "STORE" flag that asks the kernel to store this information for later. This will be helpful for both the SKB and redirect use-cases.
For redirect e.g into a veth, then BPF-prog can use the same function bpf_xdp_metadata_rx_hash() to receive the RX-hash, as it can obtain the "stored" value (from the BPF-prog that did the redirect).

(p.s. Hopefully a const 'flags' variable can be optimized when unrolling to eliminate store instructions when flags==0)

Quoted 38 lines
include/linux/netdevice.h +0 -0
  /**
include/net/xdp.h +29 -0
index 55dbc68bfffc..c24aba5c363b 100644
@@ -409,4 +409,33 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
 
  #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
 
+#define XDP_METADATA_KFUNC_xxx	\
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP_SUPPORTED, \
+			   bpf_xdp_metadata_rx_timestamp_supported) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
+			   bpf_xdp_metadata_rx_timestamp) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH_SUPPORTED, \
+			   bpf_xdp_metadata_rx_hash_supported) \
+	XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
+			   bpf_xdp_metadata_rx_hash) \
+
+enum {
+#define XDP_METADATA_KFUNC(name, str) name,
+XDP_METADATA_KFUNC_xxx
+#undef XDP_METADATA_KFUNC
+MAX_XDP_METADATA_KFUNC,
+};
+
+#ifdef CONFIG_NET
+u32 xdp_metadata_kfunc_id(int id);
+#else
+static inline u32 xdp_metadata_kfunc_id(int id) { return 0; }
+#endif
+
+struct xdp_md;
+bool bpf_xdp_metadata_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx);
+bool bpf_xdp_metadata_rx_hash_supported(const struct xdp_md *ctx);
+u32 bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx);
+
#26 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 49 lines · Jesper Dangaard Brouer
Quoted 11 lines · Stanislav Fomichev

There is an ndo handler per kfunc, the verifier replaces a call to the generic kfunc with a call to the per-device one.

For XDP, we define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible metatada kfuncs. Not all devices have to implement them. If kfunc is not supported by the target device, the default implementation is called instead.

Upon loading, if BPF_F_XDP_HAS_METADATA is passed via prog_flags, we treat prog_index as target device for kfunc resolution.

[...cut...]

Quoted 13 lines
include/linux/netdevice.h +1 -0
index 5aa35c58c342..2eabb9157767 100644
@@ -74,6 +74,7 @@ struct udp_tunnel_nic_info;
  struct udp_tunnel_nic;
  struct bpf_prog;
  struct xdp_buff;
+struct xdp_md;
 
  void synchronize_net(void);
  void netdev_set_default_ethtool_ops(struct net_device *dev,

include/linux/netdevice.h struct net_device_ops { · line 1620

Quoted 9 lines
include/linux/netdevice.h +4 -0
@@ -1611,6 +1612,10 @@ struct net_device_ops {
      ktime_t                 (*ndo_get_tstamp)(struct net_device *dev,
                                                const struct skb_shared_hwtstamps *hwtstamps,
                                                bool cycles);
+     bool                    (*ndo_xdp_rx_timestamp_supported)(const struct xdp_md *ctx);
+     u64                     (*ndo_xdp_rx_timestamp)(const struct xdp_md *ctx);
+     bool                    (*ndo_xdp_rx_hash_supported)(const struct xdp_md *ctx);
+     u32                     (*ndo_xdp_rx_hash)(const struct xdp_md *ctx);
  };
 

Would it make sense to add a 'flags' parameter to ndo_xdp_rx_timestamp and ndo_xdp_rx_hash ?

E.g. we could have a "STORE" flag that asks the kernel to store this information for later. This will be helpful for both the SKB and redirect use-cases.
For redirect e.g into a veth, then BPF-prog can use the same function bpf_xdp_metadata_rx_hash() to receive the RX-hash, as it can obtain the "stored" value (from the BPF-prog that did the redirect).

Quoted 2 lines

(p.s. Hopefully a const 'flags' variable can be optimized when unrolling to eliminate store instructions when flags==0)

Are we concerned that doing this without a flag and with another function call will be expensive?
For xdp->skb path, I was hoping we would be to do something like:

timestamp = bpf_xdp_metadata_rx_hash(ctx); bpf_xdp_metadata_export_rx_hash_to_skb(ctx, timestamp);

This should also let the users adjust the metadata before storing it. Am I missing something here? Why would the flag be preferable?

Quoted 39 lines
Quoted 38 lines
include/linux/netdevice.h +0 -0
  /**
include/net/xdp.h +29 -0
index 55dbc68bfffc..c24aba5c363b 100644
@@ -409,4 +409,33 @@ void xdp_attachment_setup(struct xdp_attachment_info *info,
 
  #define DEV_MAP_BULK_SIZE XDP_BULK_QUEUE_SIZE
 
+#define XDP_METADATA_KFUNC_xxx       \
+     XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP_SUPPORTED, \
+                        bpf_xdp_metadata_rx_timestamp_supported) \
+     XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_TIMESTAMP, \
+                        bpf_xdp_metadata_rx_timestamp) \
+     XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH_SUPPORTED, \
+                        bpf_xdp_metadata_rx_hash_supported) \
+     XDP_METADATA_KFUNC(XDP_METADATA_KFUNC_RX_HASH, \
+                        bpf_xdp_metadata_rx_hash) \
+
+enum {
+#define XDP_METADATA_KFUNC(name, str) name,
+XDP_METADATA_KFUNC_xxx
+#undef XDP_METADATA_KFUNC
+MAX_XDP_METADATA_KFUNC,
+};
+
+#ifdef CONFIG_NET
+u32 xdp_metadata_kfunc_id(int id);
+#else
+static inline u32 xdp_metadata_kfunc_id(int id) { return 0; }
+#endif
+
+struct xdp_md;
+bool bpf_xdp_metadata_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx);
+bool bpf_xdp_metadata_rx_hash_supported(const struct xdp_md *ctx);
+u32 bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx);
+
#27 lore raw

Re: [PATCH bpf-next v3 03/12] bpf: XDP metadata RX kfuncs

Quoted 51 lines · Stanislav Fomichev
Quoted 49 lines · Jesper Dangaard Brouer
Quoted 11 lines · Stanislav Fomichev

There is an ndo handler per kfunc, the verifier replaces a call to the generic kfunc with a call to the per-device one.

For XDP, we define a new kfunc set (xdp_metadata_kfunc_ids) which implements all possible metatada kfuncs. Not all devices have to implement them. If kfunc is not supported by the target device, the default implementation is called instead.

Upon loading, if BPF_F_XDP_HAS_METADATA is passed via prog_flags, we treat prog_index as target device for kfunc resolution.

[...cut...]

Quoted 13 lines
include/linux/netdevice.h +1 -0
index 5aa35c58c342..2eabb9157767 100644
@@ -74,6 +74,7 @@ struct udp_tunnel_nic_info;
   struct udp_tunnel_nic;
   struct bpf_prog;
   struct xdp_buff;
+struct xdp_md;
 
   void synchronize_net(void);
   void netdev_set_default_ethtool_ops(struct net_device *dev,

include/linux/netdevice.h struct net_device_ops { · line 1620

Quoted 9 lines
include/linux/netdevice.h +4 -0
@@ -1611,6 +1612,10 @@ struct net_device_ops {
       ktime_t                 (*ndo_get_tstamp)(struct net_device *dev,
                                                 const struct skb_shared_hwtstamps *hwtstamps,
                                                 bool cycles);
+     bool                    (*ndo_xdp_rx_timestamp_supported)(const struct xdp_md *ctx);
+     u64                     (*ndo_xdp_rx_timestamp)(const struct xdp_md *ctx);
+     bool                    (*ndo_xdp_rx_hash_supported)(const struct xdp_md *ctx);
+     u32                     (*ndo_xdp_rx_hash)(const struct xdp_md *ctx);
   };
 

Would it make sense to add a 'flags' parameter to ndo_xdp_rx_timestamp and ndo_xdp_rx_hash ?

E.g. we could have a "STORE" flag that asks the kernel to store this information for later. This will be helpful for both the SKB and redirect use-cases.
For redirect e.g into a veth, then BPF-prog can use the same function bpf_xdp_metadata_rx_hash() to receive the RX-hash, as it can obtain the "stored" value (from the BPF-prog that did the redirect).

Quoted 5 lines
Quoted 2 lines

(p.s. Hopefully a const 'flags' variable can be optimized when unrolling to eliminate store instructions when flags==0)

Are we concerned that doing this without a flag and with another function call will be expensive?

Yes, but if we can unroll (to avoid the function calls) it would be more flexible and explicit API with below instead.

Quoted 7 lines

For xdp->skb path, I was hoping we would be to do something like:

timestamp = bpf_xdp_metadata_rx_hash(ctx); bpf_xdp_metadata_export_rx_hash_to_skb(ctx, timestamp);

This should also let the users adjust the metadata before storing it. Am I missing something here? Why would the flag be preferable?

I do like this ability to let the users adjust the metadata before storing it. This would be a more flexible API for the BPF-programmer. I like your "export" suggestion. The main concern for me was performance overhead of the extra function call, which I guess can be removed via unrolling later.
Unrolling these 'export' functions might be easier to accept from a maintainer perspective, as it is not device driver specific, thus we can place that in the core BPF code.

--Jesper

#28 lore raw

PATCH v3 4/12 veth: Introduce veth_xdp_buff wrapper for xdp_buff

In reply to Stanislav Fomichev

No functional changes. Boilerplate to allow stuffing more data after xdp_buff.

Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 drivers/net/veth.c | 56 +++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 25 deletions(-)
drivers/net/veth.c +31 -25
index ac7c0653695f..04ffd8cb2945 100644
@@ -116,6 +116,10 @@ static struct {
 	{ "peer_ifindex" },
 };
 
+struct veth_xdp_buff {
+	struct xdp_buff xdp;
+};
+
 static int veth_get_link_ksettings(struct net_device *dev,
 				   struct ethtool_link_ksettings *cmd)
 {
@@ -592,23 +596,24 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 	rcu_read_lock();
 	xdp_prog = rcu_dereference(rq->xdp_prog);
 	if (likely(xdp_prog)) {
-		struct xdp_buff xdp;
+		struct veth_xdp_buff vxbuf;
+		struct xdp_buff *xdp = &vxbuf.xdp;
 		u32 act;
 
-		xdp_convert_frame_to_buff(frame, &xdp);
-		xdp.rxq = &rq->xdp_rxq;
+		xdp_convert_frame_to_buff(frame, xdp);
+		xdp->rxq = &rq->xdp_rxq;
 
-		act = bpf_prog_run_xdp(xdp_prog, &xdp);
+		act = bpf_prog_run_xdp(xdp_prog, xdp);
 
 		switch (act) {
 		case XDP_PASS:
-			if (xdp_update_frame_from_buff(&xdp, frame))
+			if (xdp_update_frame_from_buff(xdp, frame))
 				goto err_xdp;
 			break;
 		case XDP_TX:
 			orig_frame = *frame;
-			xdp.rxq->mem = frame->mem;
-			if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
+			xdp->rxq->mem = frame->mem;
+			if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
 				trace_xdp_exception(rq->dev, xdp_prog, act);
 				frame = &orig_frame;
 				stats->rx_drops++;
@@ -619,8 +624,8 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 			goto xdp_xmit;
 		case XDP_REDIRECT:
 			orig_frame = *frame;
-			xdp.rxq->mem = frame->mem;
-			if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
+			xdp->rxq->mem = frame->mem;
+			if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
 				frame = &orig_frame;
 				stats->rx_drops++;
 				goto err_xdp;
@@ -801,7 +806,8 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 {
 	void *orig_data, *orig_data_end;
 	struct bpf_prog *xdp_prog;
-	struct xdp_buff xdp;
+	struct veth_xdp_buff vxbuf;
+	struct xdp_buff *xdp = &vxbuf.xdp;
 	u32 act, metalen;
 	int off;
 
@@ -815,22 +821,22 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	}
 
 	__skb_push(skb, skb->data - skb_mac_header(skb));
-	if (veth_convert_skb_to_xdp_buff(rq, &xdp, &skb))
+	if (veth_convert_skb_to_xdp_buff(rq, xdp, &skb))
 		goto drop;
 
-	orig_data = xdp.data;
-	orig_data_end = xdp.data_end;
+	orig_data = xdp->data;
+	orig_data_end = xdp->data_end;
 
-	act = bpf_prog_run_xdp(xdp_prog, &xdp);
+	act = bpf_prog_run_xdp(xdp_prog, xdp);
 
 	switch (act) {
 	case XDP_PASS:
 		break;
 	case XDP_TX:
-		veth_xdp_get(&xdp);
+		veth_xdp_get(xdp);
 		consume_skb(skb);
-		xdp.rxq->mem = rq->xdp_mem;
-		if (unlikely(veth_xdp_tx(rq, &xdp, bq) < 0)) {
+		xdp->rxq->mem = rq->xdp_mem;
+		if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
 			trace_xdp_exception(rq->dev, xdp_prog, act);
 			stats->rx_drops++;
 			goto err_xdp;
@@ -839,10 +845,10 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 		rcu_read_unlock();
 		goto xdp_xmit;
 	case XDP_REDIRECT:
-		veth_xdp_get(&xdp);
+		veth_xdp_get(xdp);
 		consume_skb(skb);
-		xdp.rxq->mem = rq->xdp_mem;
-		if (xdp_do_redirect(rq->dev, &xdp, xdp_prog)) {
+		xdp->rxq->mem = rq->xdp_mem;
+		if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
 			stats->rx_drops++;
 			goto err_xdp;
 		}
@@ -862,7 +868,7 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	rcu_read_unlock();
 
 	/* check if bpf_xdp_adjust_head was used */
-	off = orig_data - xdp.data;
+	off = orig_data - xdp->data;
 	if (off > 0)
 		__skb_push(skb, off);
 	else if (off < 0)
@@ -871,21 +877,21 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	skb_reset_mac_header(skb);
 
 	/* check if bpf_xdp_adjust_tail was used */
-	off = xdp.data_end - orig_data_end;
+	off = xdp->data_end - orig_data_end;
 	if (off != 0)
 		__skb_put(skb, off); /* positive on grow, negative on shrink */
 
 	/* XDP frag metadata (e.g. nr_frags) are updated in eBPF helpers
 	 * (e.g. bpf_xdp_adjust_tail), we need to update data_len here.
 	 */
-	if (xdp_buff_has_frags(&xdp))
+	if (xdp_buff_has_frags(xdp))
 		skb->data_len = skb_shinfo(skb)->xdp_frags_size;
 	else
 		skb->data_len = 0;
 
 	skb->protocol = eth_type_trans(skb, rq->dev);
 
-	metalen = xdp.data - xdp.data_meta;
+	metalen = xdp->data - xdp->data_meta;
 	if (metalen)
 		skb_metadata_set(skb, metalen);
 out:
@@ -898,7 +904,7 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	return NULL;
 err_xdp:
 	rcu_read_unlock();
-	xdp_return_buff(&xdp);
+	xdp_return_buff(xdp);
 xdp_xmit:
 	return NULL;
 }
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#29 lore raw

PATCH v3 5/12 veth: Support RX XDP metadata

In reply to Stanislav Fomichev

The goal is to enable end-to-end testing of the metadata for AF_XDP.

Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 drivers/net/veth.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
drivers/net/veth.c +32 -0
index 04ffd8cb2945..d15302672493 100644
@@ -118,6 +118,7 @@ static struct {
 
 struct veth_xdp_buff {
 	struct xdp_buff xdp;
+	struct sk_buff *skb;
 };
 
 static int veth_get_link_ksettings(struct net_device *dev,
@@ -602,6 +603,7 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 
 		xdp_convert_frame_to_buff(frame, xdp);
 		xdp->rxq = &rq->xdp_rxq;
+		vxbuf.skb = NULL;
 
 		act = bpf_prog_run_xdp(xdp_prog, xdp);
 
@@ -823,6 +825,7 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	__skb_push(skb, skb->data - skb_mac_header(skb));
 	if (veth_convert_skb_to_xdp_buff(rq, xdp, &skb))
 		goto drop;
+	vxbuf.skb = skb;
 
 	orig_data = xdp->data;
 	orig_data_end = xdp->data_end;
@@ -1601,6 +1604,30 @@ static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
 	}
 }
 
+static bool veth_xdp_rx_timestamp_supported(const struct xdp_md *ctx)
+{
+	return true;
+}
+
+static u64 veth_xdp_rx_timestamp(const struct xdp_md *ctx)
+{
+	return ktime_get_mono_fast_ns();
+}
+
+static bool veth_xdp_rx_hash_supported(const struct xdp_md *ctx)
+{
+	return true;
+}
+
+static u32 veth_xdp_rx_hash(const struct xdp_md *ctx)
+{
+	struct veth_xdp_buff *_ctx = (void *)ctx;
+
+	if (_ctx->skb)
+		return skb_get_hash(_ctx->skb);
+	return 0;
+}
+
 static const struct net_device_ops veth_netdev_ops = {
 	.ndo_init            = veth_dev_init,
 	.ndo_open            = veth_open,
@@ -1620,6 +1647,11 @@ static const struct net_device_ops veth_netdev_ops = {
 	.ndo_bpf		= veth_xdp,
 	.ndo_xdp_xmit		= veth_ndo_xdp_xmit,
 	.ndo_get_peer_dev	= veth_peer_dev,
+
+	.ndo_xdp_rx_timestamp_supported = veth_xdp_rx_timestamp_supported,
+	.ndo_xdp_rx_timestamp	= veth_xdp_rx_timestamp,
+	.ndo_xdp_rx_hash_supported = veth_xdp_rx_hash_supported,
+	.ndo_xdp_rx_hash	= veth_xdp_rx_hash,
 };
 
 #define VETH_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HW_CSUM | \
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#30 lore raw

PATCH v3 6/12 selftests/bpf: Verify xdp_metadata xdp->af_xdp path

In reply to Stanislav Fomichev

- create new netns
- create veth pair (veTX+veRX)
- setup AF_XDP socket for both interfaces
- attach bpf to veRX
- send packet via veTX
- verify the packet has expected metadata at veRX

Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../selftests/bpf/prog_tests/xdp_metadata.c   | 394 ++++++++++++++++++
 .../selftests/bpf/progs/xdp_metadata.c        |  70 ++++
 .../selftests/bpf/progs/xdp_metadata2.c       |  15 +
 tools/testing/selftests/bpf/xdp_metadata.h    |   7 +
 5 files changed, 487 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
 create mode 100644 tools/testing/selftests/bpf/progs/xdp_metadata.c
 create mode 100644 tools/testing/selftests/bpf/progs/xdp_metadata2.c
 create mode 100644 tools/testing/selftests/bpf/xdp_metadata.h
tools/testing/selftests/bpf/Makefile +1 -1
index 6a0f043dc410..4eed22fa3681 100644
@@ -527,7 +527,7 @@ TRUNNER_BPF_PROGS_DIR := progs
 TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c	\
 			 network_helpers.c testing_helpers.c		\
 			 btf_helpers.c flow_dissector_load.h		\
-			 cap_helpers.c
+			 cap_helpers.c xsk.c
 TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko	\
 		       $(OUTPUT)/liburandom_read.so			\
 		       $(OUTPUT)/xdp_synproxy				\
tools/testing/selftests/bpf/prog_tests/xdp_metadata.c new file +394 -0

394 lines not on this page · raw message

tools/testing/selftests/bpf/progs/xdp_metadata.c new file +70 -0

70 lines not on this page · raw message

tools/testing/selftests/bpf/progs/xdp_metadata2.c new file +15 -0
new file mode 100644
index 000000000000..bec1732ade62
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+
+#include "xdp_metadata.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_endian.h>
+
+SEC("freplace/rx")
+int freplace_rx(struct xdp_md *ctx)
+{
+	return XDP_PASS;
+}
+
+char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/xdp_metadata.h new file +7 -0
new file mode 100644
index 000000000000..c4892d122b7f
@@ -0,0 +1,7 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#pragma once
+
+struct xdp_meta {
+	__u64 rx_timestamp;
+	__u32 rx_hash;
+};
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#31 lore raw

PATCH v3 7/12 mlx4: Introduce mlx4_xdp_buff wrapper for xdp_buff

In reply to Stanislav Fomichev

No functional changes. Boilerplate to allow stuffing more data after xdp_buff.

Cc Tariq Toukan <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 drivers/net/ethernet/mellanox/mlx4/en_rx.c | 26 +++++++++++++---------
 1 file changed, 15 insertions(+), 11 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_rx.c +15 -11
index 8f762fc170b3..9c114fc723e3 100644
@@ -661,9 +661,14 @@ static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
 #define MLX4_CQE_STATUS_IP_ANY (MLX4_CQE_STATUS_IPV4)
 #endif
 
+struct mlx4_xdp_buff {
+	struct xdp_buff xdp;
+};
+
 int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
 {
 	struct mlx4_en_priv *priv = netdev_priv(dev);
+	struct mlx4_xdp_buff mxbuf = {};
 	int factor = priv->cqe_factor;
 	struct mlx4_en_rx_ring *ring;
 	struct bpf_prog *xdp_prog;
@@ -671,7 +676,6 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 	bool doorbell_pending;
 	bool xdp_redir_flush;
 	struct mlx4_cqe *cqe;
-	struct xdp_buff xdp;
 	int polled = 0;
 	int index;
 
@@ -681,7 +685,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 	ring = priv->rx_ring[cq_ring];
 
 	xdp_prog = rcu_dereference_bh(ring->xdp_prog);
-	xdp_init_buff(&xdp, priv->frag_info[0].frag_stride, &ring->xdp_rxq);
+	xdp_init_buff(&mxbuf.xdp, priv->frag_info[0].frag_stride, &ring->xdp_rxq);
 	doorbell_pending = false;
 	xdp_redir_flush = false;
 
@@ -776,24 +780,24 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 						priv->frag_info[0].frag_size,
 						DMA_FROM_DEVICE);
 
-			xdp_prepare_buff(&xdp, va - frags[0].page_offset,
+			xdp_prepare_buff(&mxbuf.xdp, va - frags[0].page_offset,
 					 frags[0].page_offset, length, false);
-			orig_data = xdp.data;
+			orig_data = mxbuf.xdp.data;
 
-			act = bpf_prog_run_xdp(xdp_prog, &xdp);
+			act = bpf_prog_run_xdp(xdp_prog, &mxbuf.xdp);
 
-			length = xdp.data_end - xdp.data;
-			if (xdp.data != orig_data) {
-				frags[0].page_offset = xdp.data -
-					xdp.data_hard_start;
-				va = xdp.data;
+			length = mxbuf.xdp.data_end - mxbuf.xdp.data;
+			if (mxbuf.xdp.data != orig_data) {
+				frags[0].page_offset = mxbuf.xdp.data -
+					mxbuf.xdp.data_hard_start;
+				va = mxbuf.xdp.data;
 			}
 
 			switch (act) {
 			case XDP_PASS:
 				break;
 			case XDP_REDIRECT:
-				if (likely(!xdp_do_redirect(dev, &xdp, xdp_prog))) {
+				if (likely(!xdp_do_redirect(dev, &mxbuf.xdp, xdp_prog))) {
 					ring->xdp_redirect++;
 					xdp_redir_flush = true;
 					frags[0].page = NULL;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#32 lore raw

Re: [PATCH bpf-next v3 07/12] mlx4: Introduce mlx4_xdp_buff wrapper for xdp_buff

Quoted 25 lines · Stanislav Fomichev

No functional changes. Boilerplate to allow stuffing more data after xdp_buff.

Cc Tariq Toukan <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
  drivers/net/ethernet/mellanox/mlx4/en_rx.c | 26 +++++++++++++---------
  1 file changed, 15 insertions(+), 11 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_rx.c +0 -0
index 8f762fc170b3..9c114fc723e3 100644

drivers/net/ethernet/mellanox/mlx4/en_rx.c static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va, · line 667

Quoted 7 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +4 -0
@@ -661,9 +661,14 @@ static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
  #define MLX4_CQE_STATUS_IP_ANY (MLX4_CQE_STATUS_IPV4)
  #endif
 
+struct mlx4_xdp_buff {
+	struct xdp_buff xdp;
+};
+

Prefer name with 'en', struct mlx4_en_xdp_buff.

Quoted 58 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +11 -11
  int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
  {
  	struct mlx4_en_priv *priv = netdev_priv(dev);
+	struct mlx4_xdp_buff mxbuf = {};
  	int factor = priv->cqe_factor;
  	struct mlx4_en_rx_ring *ring;
  	struct bpf_prog *xdp_prog;
@@ -671,7 +676,6 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
  	bool doorbell_pending;
  	bool xdp_redir_flush;
  	struct mlx4_cqe *cqe;
-	struct xdp_buff xdp;
  	int polled = 0;
  	int index;
 
@@ -681,7 +685,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
  	ring = priv->rx_ring[cq_ring];
 
  	xdp_prog = rcu_dereference_bh(ring->xdp_prog);
-	xdp_init_buff(&xdp, priv->frag_info[0].frag_stride, &ring->xdp_rxq);
+	xdp_init_buff(&mxbuf.xdp, priv->frag_info[0].frag_stride, &ring->xdp_rxq);
  	doorbell_pending = false;
  	xdp_redir_flush = false;
 
@@ -776,24 +780,24 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
  						priv->frag_info[0].frag_size,
  						DMA_FROM_DEVICE);
 
-			xdp_prepare_buff(&xdp, va - frags[0].page_offset,
+			xdp_prepare_buff(&mxbuf.xdp, va - frags[0].page_offset,
  					 frags[0].page_offset, length, false);
-			orig_data = xdp.data;
+			orig_data = mxbuf.xdp.data;
 
-			act = bpf_prog_run_xdp(xdp_prog, &xdp);
+			act = bpf_prog_run_xdp(xdp_prog, &mxbuf.xdp);
 
-			length = xdp.data_end - xdp.data;
-			if (xdp.data != orig_data) {
-				frags[0].page_offset = xdp.data -
-					xdp.data_hard_start;
-				va = xdp.data;
+			length = mxbuf.xdp.data_end - mxbuf.xdp.data;
+			if (mxbuf.xdp.data != orig_data) {
+				frags[0].page_offset = mxbuf.xdp.data -
+					mxbuf.xdp.data_hard_start;
+				va = mxbuf.xdp.data;
  			}
 
  			switch (act) {
  			case XDP_PASS:
  				break;
  			case XDP_REDIRECT:
-				if (likely(!xdp_do_redirect(dev, &xdp, xdp_prog))) {
+				if (likely(!xdp_do_redirect(dev, &mxbuf.xdp, xdp_prog))) {
  					ring->xdp_redirect++;
  					xdp_redir_flush = true;
  					frags[0].page = NULL;
#33 lore raw

Re: [PATCH bpf-next v3 07/12] mlx4: Introduce mlx4_xdp_buff wrapper for xdp_buff

Quoted 32 lines · Tariq Toukan
Quoted 28 lines · Stanislav Fomichev

No functional changes. Boilerplate to allow stuffing more data after xdp_buff.

Cc Tariq Toukan <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
  drivers/net/ethernet/mellanox/mlx4/en_rx.c | 26 +++++++++++++---------
  1 file changed, 15 insertions(+), 11 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_rx.c +0 -0
index 8f762fc170b3..9c114fc723e3 100644
@@ -661,9 +661,14 @@ static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
  #define MLX4_CQE_STATUS_IP_ANY (MLX4_CQE_STATUS_IPV4)
  #endif
 
Quoted 6 lines

drivers/net/ethernet/mellanox/mlx4/en_rx.c static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va, · line 667

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +4 -0
+struct mlx4_xdp_buff {
+     struct xdp_buff xdp;
+};
+

Prefer name with 'en', struct mlx4_en_xdp_buff.

Sure, will rename!

Quoted 58 lines
Quoted 58 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +11 -11
  int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
  {
      struct mlx4_en_priv *priv = netdev_priv(dev);
+     struct mlx4_xdp_buff mxbuf = {};
      int factor = priv->cqe_factor;
      struct mlx4_en_rx_ring *ring;
      struct bpf_prog *xdp_prog;
@@ -671,7 +676,6 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
      bool doorbell_pending;
      bool xdp_redir_flush;
      struct mlx4_cqe *cqe;
-     struct xdp_buff xdp;
      int polled = 0;
      int index;
 
@@ -681,7 +685,7 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
      ring = priv->rx_ring[cq_ring];
 
      xdp_prog = rcu_dereference_bh(ring->xdp_prog);
-     xdp_init_buff(&xdp, priv->frag_info[0].frag_stride, &ring->xdp_rxq);
+     xdp_init_buff(&mxbuf.xdp, priv->frag_info[0].frag_stride, &ring->xdp_rxq);
      doorbell_pending = false;
      xdp_redir_flush = false;
 
@@ -776,24 +780,24 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
                                              priv->frag_info[0].frag_size,
                                              DMA_FROM_DEVICE);
 
-                     xdp_prepare_buff(&xdp, va - frags[0].page_offset,
+                     xdp_prepare_buff(&mxbuf.xdp, va - frags[0].page_offset,
                                       frags[0].page_offset, length, false);
-                     orig_data = xdp.data;
+                     orig_data = mxbuf.xdp.data;
 
-                     act = bpf_prog_run_xdp(xdp_prog, &xdp);
+                     act = bpf_prog_run_xdp(xdp_prog, &mxbuf.xdp);
 
-                     length = xdp.data_end - xdp.data;
-                     if (xdp.data != orig_data) {
-                             frags[0].page_offset = xdp.data -
-                                     xdp.data_hard_start;
-                             va = xdp.data;
+                     length = mxbuf.xdp.data_end - mxbuf.xdp.data;
+                     if (mxbuf.xdp.data != orig_data) {
+                             frags[0].page_offset = mxbuf.xdp.data -
+                                     mxbuf.xdp.data_hard_start;
+                             va = mxbuf.xdp.data;
                      }
 
                      switch (act) {
                      case XDP_PASS:
                              break;
                      case XDP_REDIRECT:
-                             if (likely(!xdp_do_redirect(dev, &xdp, xdp_prog))) {
+                             if (likely(!xdp_do_redirect(dev, &mxbuf.xdp, xdp_prog))) {
                                      ring->xdp_redirect++;
                                      xdp_redir_flush = true;
                                      frags[0].page = NULL;
#34 lore raw

PATCH v3 8/12 mxl4: Support RX XDP metadata

In reply to Stanislav Fomichev

RX timestamp and hash for now. Tested using the prog from the next patch.

Also enabling xdp metadata support; don't see why it's disabled, there is enough headroom..

Cc Tariq Toukan <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 drivers/net/ethernet/mellanox/mlx4/en_clock.c | 13 +++++--
 .../net/ethernet/mellanox/mlx4/en_netdev.c    | 10 +++++
 drivers/net/ethernet/mellanox/mlx4/en_rx.c    | 38 ++++++++++++++++++-
 drivers/net/ethernet/mellanox/mlx4/mlx4_en.h  |  1 +
 include/linux/mlx4/device.h                   |  7 ++++
 5 files changed, 64 insertions(+), 5 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_clock.c +9 -4
index 98b5ffb4d729..9e3b76182088 100644
@@ -58,9 +58,7 @@ u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
 	return hi | lo;
 }
 
-void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
-			    struct skb_shared_hwtstamps *hwts,
-			    u64 timestamp)
+u64 mlx4_en_get_hwtstamp(struct mlx4_en_dev *mdev, u64 timestamp)
 {
 	unsigned int seq;
 	u64 nsec;
@@ -70,8 +68,15 @@ void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
 		nsec = timecounter_cyc2time(&mdev->clock, timestamp);
 	} while (read_seqretry(&mdev->clock_lock, seq));
 
+	return ns_to_ktime(nsec);
+}
+
+void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
+			    struct skb_shared_hwtstamps *hwts,
+			    u64 timestamp)
+{
 	memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
-	hwts->hwtstamp = ns_to_ktime(nsec);
+	hwts->hwtstamp = mlx4_en_get_hwtstamp(mdev, timestamp);
 }
 
 /**
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +10 -0
index 8800d3f1f55c..1cb63746a851 100644
@@ -2855,6 +2855,11 @@ static const struct net_device_ops mlx4_netdev_ops = {
 	.ndo_features_check	= mlx4_en_features_check,
 	.ndo_set_tx_maxrate	= mlx4_en_set_tx_maxrate,
 	.ndo_bpf		= mlx4_xdp,
+
+	.ndo_xdp_rx_timestamp_supported = mlx4_xdp_rx_timestamp_supported,
+	.ndo_xdp_rx_timestamp	= mlx4_xdp_rx_timestamp,
+	.ndo_xdp_rx_hash_supported = mlx4_xdp_rx_hash_supported,
+	.ndo_xdp_rx_hash	= mlx4_xdp_rx_hash,
 };
 
 static const struct net_device_ops mlx4_netdev_ops_master = {
@@ -2887,6 +2892,11 @@ static const struct net_device_ops mlx4_netdev_ops_master = {
 	.ndo_features_check	= mlx4_en_features_check,
 	.ndo_set_tx_maxrate	= mlx4_en_set_tx_maxrate,
 	.ndo_bpf		= mlx4_xdp,
+
+	.ndo_xdp_rx_timestamp_supported = mlx4_xdp_rx_timestamp_supported,
+	.ndo_xdp_rx_timestamp	= mlx4_xdp_rx_timestamp,
+	.ndo_xdp_rx_hash_supported = mlx4_xdp_rx_hash_supported,
+	.ndo_xdp_rx_hash	= mlx4_xdp_rx_hash,
 };
 
 struct mlx4_en_bond {
drivers/net/ethernet/mellanox/mlx4/en_rx.c +37 -1
index 9c114fc723e3..1b8e1b2d8729 100644
@@ -663,8 +663,40 @@ static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
 
 struct mlx4_xdp_buff {
 	struct xdp_buff xdp;
+	struct mlx4_cqe *cqe;
+	struct mlx4_en_dev *mdev;
+	struct mlx4_en_rx_ring *ring;
+	struct net_device *dev;
 };
 
+bool mlx4_xdp_rx_timestamp_supported(const struct xdp_md *ctx)
+{
+	struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+	return _ctx->ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL;
+}
+
+u64 mlx4_xdp_rx_timestamp(const struct xdp_md *ctx)
+{
+	struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+	return mlx4_en_get_hwtstamp(_ctx->mdev, mlx4_en_get_cqe_ts(_ctx->cqe));
+}
+
+bool mlx4_xdp_rx_hash_supported(const struct xdp_md *ctx)
+{
+	struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+	return _ctx->dev->features & NETIF_F_RXHASH;
+}
+
+u32 mlx4_xdp_rx_hash(const struct xdp_md *ctx)
+{
+	struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+	return be32_to_cpu(_ctx->cqe->immed_rss_invalid);
+}
+
 int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
 {
 	struct mlx4_en_priv *priv = netdev_priv(dev);
@@ -781,8 +813,12 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
 						DMA_FROM_DEVICE);
 
 			xdp_prepare_buff(&mxbuf.xdp, va - frags[0].page_offset,
-					 frags[0].page_offset, length, false);
+					 frags[0].page_offset, length, true);
 			orig_data = mxbuf.xdp.data;
+			mxbuf.cqe = cqe;
+			mxbuf.mdev = priv->mdev;
+			mxbuf.ring = ring;
+			mxbuf.dev = dev;
 
 			act = bpf_prog_run_xdp(xdp_prog, &mxbuf.xdp);
 
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +1 -0
index e132ff4c82f2..b7c0d4899ad7 100644
@@ -792,6 +792,7 @@ int mlx4_en_netdev_event(struct notifier_block *this,
  * Functions for time stamping
  */
 u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe);
+u64 mlx4_en_get_hwtstamp(struct mlx4_en_dev *mdev, u64 timestamp);
 void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
 			    struct skb_shared_hwtstamps *hwts,
 			    u64 timestamp);
include/linux/mlx4/device.h +7 -0
index 6646634a0b9d..d5904da1d490 100644
@@ -1585,4 +1585,11 @@ static inline int mlx4_get_num_reserved_uar(struct mlx4_dev *dev)
 	/* The first 128 UARs are used for EQ doorbells */
 	return (128 >> (PAGE_SHIFT - dev->uar_page_shift));
 }
+
+struct xdp_md;
+bool mlx4_xdp_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 mlx4_xdp_rx_timestamp(const struct xdp_md *ctx);
+bool mlx4_xdp_rx_hash_supported(const struct xdp_md *ctx);
+u32 mlx4_xdp_rx_hash(const struct xdp_md *ctx);
+
 #endif /* MLX4_DEVICE_H */
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#35 lore raw

Re: [PATCH bpf-next v3 08/12] mxl4: Support RX XDP metadata

Typo in title mxl4 -> mlx4.
Preferably: net/mlx4_en.

Quoted 164 lines · Stanislav Fomichev

RX timestamp and hash for now. Tested using the prog from the next patch.

Also enabling xdp metadata support; don't see why it's disabled, there is enough headroom..

Cc Tariq Toukan <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
  drivers/net/ethernet/mellanox/mlx4/en_clock.c | 13 +++++--
  .../net/ethernet/mellanox/mlx4/en_netdev.c    | 10 +++++
  drivers/net/ethernet/mellanox/mlx4/en_rx.c    | 38 ++++++++++++++++++-
  drivers/net/ethernet/mellanox/mlx4/mlx4_en.h  |  1 +
  include/linux/mlx4/device.h                   |  7 ++++
  5 files changed, 64 insertions(+), 5 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_clock.c +9 -4
index 98b5ffb4d729..9e3b76182088 100644
@@ -58,9 +58,7 @@ u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
  	return hi | lo;
  }
 
-void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
-			    struct skb_shared_hwtstamps *hwts,
-			    u64 timestamp)
+u64 mlx4_en_get_hwtstamp(struct mlx4_en_dev *mdev, u64 timestamp)
  {
  	unsigned int seq;
  	u64 nsec;
@@ -70,8 +68,15 @@ void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
  		nsec = timecounter_cyc2time(&mdev->clock, timestamp);
  	} while (read_seqretry(&mdev->clock_lock, seq));
 
+	return ns_to_ktime(nsec);
+}
+
+void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
+			    struct skb_shared_hwtstamps *hwts,
+			    u64 timestamp)
+{
  	memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
-	hwts->hwtstamp = ns_to_ktime(nsec);
+	hwts->hwtstamp = mlx4_en_get_hwtstamp(mdev, timestamp);
  }
 
  /**
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +10 -0
index 8800d3f1f55c..1cb63746a851 100644
@@ -2855,6 +2855,11 @@ static const struct net_device_ops mlx4_netdev_ops = {
  	.ndo_features_check	= mlx4_en_features_check,
  	.ndo_set_tx_maxrate	= mlx4_en_set_tx_maxrate,
  	.ndo_bpf		= mlx4_xdp,
+
+	.ndo_xdp_rx_timestamp_supported = mlx4_xdp_rx_timestamp_supported,
+	.ndo_xdp_rx_timestamp	= mlx4_xdp_rx_timestamp,
+	.ndo_xdp_rx_hash_supported = mlx4_xdp_rx_hash_supported,
+	.ndo_xdp_rx_hash	= mlx4_xdp_rx_hash,
  };
 
  static const struct net_device_ops mlx4_netdev_ops_master = {
@@ -2887,6 +2892,11 @@ static const struct net_device_ops mlx4_netdev_ops_master = {
  	.ndo_features_check	= mlx4_en_features_check,
  	.ndo_set_tx_maxrate	= mlx4_en_set_tx_maxrate,
  	.ndo_bpf		= mlx4_xdp,
+
+	.ndo_xdp_rx_timestamp_supported = mlx4_xdp_rx_timestamp_supported,
+	.ndo_xdp_rx_timestamp	= mlx4_xdp_rx_timestamp,
+	.ndo_xdp_rx_hash_supported = mlx4_xdp_rx_hash_supported,
+	.ndo_xdp_rx_hash	= mlx4_xdp_rx_hash,
  };
 
  struct mlx4_en_bond {
drivers/net/ethernet/mellanox/mlx4/en_rx.c +37 -1
index 9c114fc723e3..1b8e1b2d8729 100644
@@ -663,8 +663,40 @@ static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
 
  struct mlx4_xdp_buff {
  	struct xdp_buff xdp;
+	struct mlx4_cqe *cqe;
+	struct mlx4_en_dev *mdev;
+	struct mlx4_en_rx_ring *ring;
+	struct net_device *dev;
  };
 
+bool mlx4_xdp_rx_timestamp_supported(const struct xdp_md *ctx)
+{
+	struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+	return _ctx->ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL;
+}
+
+u64 mlx4_xdp_rx_timestamp(const struct xdp_md *ctx)
+{
+	struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+	return mlx4_en_get_hwtstamp(_ctx->mdev, mlx4_en_get_cqe_ts(_ctx->cqe));
+}
+
+bool mlx4_xdp_rx_hash_supported(const struct xdp_md *ctx)
+{
+	struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+	return _ctx->dev->features & NETIF_F_RXHASH;
+}
+
+u32 mlx4_xdp_rx_hash(const struct xdp_md *ctx)
+{
+	struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+	return be32_to_cpu(_ctx->cqe->immed_rss_invalid);
+}
+
  int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
  {
  	struct mlx4_en_priv *priv = netdev_priv(dev);
@@ -781,8 +813,12 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
  						DMA_FROM_DEVICE);
 
  			xdp_prepare_buff(&mxbuf.xdp, va - frags[0].page_offset,
-					 frags[0].page_offset, length, false);
+					 frags[0].page_offset, length, true);
  			orig_data = mxbuf.xdp.data;
+			mxbuf.cqe = cqe;
+			mxbuf.mdev = priv->mdev;
+			mxbuf.ring = ring;
+			mxbuf.dev = dev;
 
  			act = bpf_prog_run_xdp(xdp_prog, &mxbuf.xdp);
 
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +1 -0
index e132ff4c82f2..b7c0d4899ad7 100644
@@ -792,6 +792,7 @@ int mlx4_en_netdev_event(struct notifier_block *this,
   * Functions for time stamping
   */
  u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe);
+u64 mlx4_en_get_hwtstamp(struct mlx4_en_dev *mdev, u64 timestamp);
  void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
  			    struct skb_shared_hwtstamps *hwts,
  			    u64 timestamp);
include/linux/mlx4/device.h +0 -0
index 6646634a0b9d..d5904da1d490 100644

include/linux/mlx4/device.h static inline int mlx4_get_num_reserved_uar(struct mlx4_dev *dev) · line 1594

Quoted 10 lines
include/linux/mlx4/device.h +7 -0
@@ -1585,4 +1585,11 @@ static inline int mlx4_get_num_reserved_uar(struct mlx4_dev *dev)
  	/* The first 128 UARs are used for EQ doorbells */
  	return (128 >> (PAGE_SHIFT - dev->uar_page_shift));
  }
+
+struct xdp_md;
+bool mlx4_xdp_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 mlx4_xdp_rx_timestamp(const struct xdp_md *ctx);
+bool mlx4_xdp_rx_hash_supported(const struct xdp_md *ctx);
+u32 mlx4_xdp_rx_hash(const struct xdp_md *ctx);
+

These are ethernet only functions, not known to the mlx4 core driver. Please move to mlx4_en.h, and use mlx4_en_xdp_*() prefix.

Quoted 1 line
include/linux/mlx4/device.h +0 -0
  #endif /* MLX4_DEVICE_H */
#36 lore raw

Re: [PATCH bpf-next v3 08/12] mxl4: Support RX XDP metadata

Quoted 3 lines · Tariq Toukan

Typo in title mxl4 -> mlx4.
Preferably: net/mlx4_en.

Oh, I always have to fight with this. Somehow mxl feels more natural :-) Thanks for spotting, will use net/mlx4_en instead. (presumably the same should be for mlx5?)

Quoted 170 lines · Stanislav Fomichev
Quoted 169 lines

RX timestamp and hash for now. Tested using the prog from the next patch.

Also enabling xdp metadata support; don't see why it's disabled, there is enough headroom..

Cc Tariq Toukan <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
  drivers/net/ethernet/mellanox/mlx4/en_clock.c | 13 +++++--
  .../net/ethernet/mellanox/mlx4/en_netdev.c    | 10 +++++
  drivers/net/ethernet/mellanox/mlx4/en_rx.c    | 38 ++++++++++++++++++-
  drivers/net/ethernet/mellanox/mlx4/mlx4_en.h  |  1 +
  include/linux/mlx4/device.h                   |  7 ++++
  5 files changed, 64 insertions(+), 5 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_clock.c +9 -4
index 98b5ffb4d729..9e3b76182088 100644
@@ -58,9 +58,7 @@ u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
      return hi | lo;
  }
 
-void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
-                         struct skb_shared_hwtstamps *hwts,
-                         u64 timestamp)
+u64 mlx4_en_get_hwtstamp(struct mlx4_en_dev *mdev, u64 timestamp)
  {
      unsigned int seq;
      u64 nsec;
@@ -70,8 +68,15 @@ void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
              nsec = timecounter_cyc2time(&mdev->clock, timestamp);
      } while (read_seqretry(&mdev->clock_lock, seq));
 
+     return ns_to_ktime(nsec);
+}
+
+void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
+                         struct skb_shared_hwtstamps *hwts,
+                         u64 timestamp)
+{
      memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
-     hwts->hwtstamp = ns_to_ktime(nsec);
+     hwts->hwtstamp = mlx4_en_get_hwtstamp(mdev, timestamp);
  }
 
  /**
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +10 -0
index 8800d3f1f55c..1cb63746a851 100644
@@ -2855,6 +2855,11 @@ static const struct net_device_ops mlx4_netdev_ops = {
      .ndo_features_check     = mlx4_en_features_check,
      .ndo_set_tx_maxrate     = mlx4_en_set_tx_maxrate,
      .ndo_bpf                = mlx4_xdp,
+
+     .ndo_xdp_rx_timestamp_supported = mlx4_xdp_rx_timestamp_supported,
+     .ndo_xdp_rx_timestamp   = mlx4_xdp_rx_timestamp,
+     .ndo_xdp_rx_hash_supported = mlx4_xdp_rx_hash_supported,
+     .ndo_xdp_rx_hash        = mlx4_xdp_rx_hash,
  };
 
  static const struct net_device_ops mlx4_netdev_ops_master = {
@@ -2887,6 +2892,11 @@ static const struct net_device_ops mlx4_netdev_ops_master = {
      .ndo_features_check     = mlx4_en_features_check,
      .ndo_set_tx_maxrate     = mlx4_en_set_tx_maxrate,
      .ndo_bpf                = mlx4_xdp,
+
+     .ndo_xdp_rx_timestamp_supported = mlx4_xdp_rx_timestamp_supported,
+     .ndo_xdp_rx_timestamp   = mlx4_xdp_rx_timestamp,
+     .ndo_xdp_rx_hash_supported = mlx4_xdp_rx_hash_supported,
+     .ndo_xdp_rx_hash        = mlx4_xdp_rx_hash,
  };
 
  struct mlx4_en_bond {
drivers/net/ethernet/mellanox/mlx4/en_rx.c +37 -1
index 9c114fc723e3..1b8e1b2d8729 100644
@@ -663,8 +663,40 @@ static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
 
  struct mlx4_xdp_buff {
      struct xdp_buff xdp;
+     struct mlx4_cqe *cqe;
+     struct mlx4_en_dev *mdev;
+     struct mlx4_en_rx_ring *ring;
+     struct net_device *dev;
  };
 
+bool mlx4_xdp_rx_timestamp_supported(const struct xdp_md *ctx)
+{
+     struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+     return _ctx->ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL;
+}
+
+u64 mlx4_xdp_rx_timestamp(const struct xdp_md *ctx)
+{
+     struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+     return mlx4_en_get_hwtstamp(_ctx->mdev, mlx4_en_get_cqe_ts(_ctx->cqe));
+}
+
+bool mlx4_xdp_rx_hash_supported(const struct xdp_md *ctx)
+{
+     struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+     return _ctx->dev->features & NETIF_F_RXHASH;
+}
+
+u32 mlx4_xdp_rx_hash(const struct xdp_md *ctx)
+{
+     struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+     return be32_to_cpu(_ctx->cqe->immed_rss_invalid);
+}
+
  int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
  {
      struct mlx4_en_priv *priv = netdev_priv(dev);
@@ -781,8 +813,12 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
                                              DMA_FROM_DEVICE);
 
                      xdp_prepare_buff(&mxbuf.xdp, va - frags[0].page_offset,
-                                      frags[0].page_offset, length, false);
+                                      frags[0].page_offset, length, true);
                      orig_data = mxbuf.xdp.data;
+                     mxbuf.cqe = cqe;
+                     mxbuf.mdev = priv->mdev;
+                     mxbuf.ring = ring;
+                     mxbuf.dev = dev;
 
                      act = bpf_prog_run_xdp(xdp_prog, &mxbuf.xdp);
 
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +1 -0
index e132ff4c82f2..b7c0d4899ad7 100644
@@ -792,6 +792,7 @@ int mlx4_en_netdev_event(struct notifier_block *this,
   * Functions for time stamping
   */
  u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe);
+u64 mlx4_en_get_hwtstamp(struct mlx4_en_dev *mdev, u64 timestamp);
  void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
                          struct skb_shared_hwtstamps *hwts,
                          u64 timestamp);
include/linux/mlx4/device.h +2 -0
index 6646634a0b9d..d5904da1d490 100644
@@ -1585,4 +1585,11 @@ static inline int mlx4_get_num_reserved_uar(struct mlx4_dev *dev)
      /* The first 128 UARs are used for EQ doorbells */
      return (128 >> (PAGE_SHIFT - dev->uar_page_shift));
  }
+
+struct xdp_md;
Quoted 8 lines

include/linux/mlx4/device.h static inline int mlx4_get_num_reserved_uar(struct mlx4_dev *dev) · line 1594

Quoted 5 lines
include/linux/mlx4/device.h +5 -0
+bool mlx4_xdp_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 mlx4_xdp_rx_timestamp(const struct xdp_md *ctx);
+bool mlx4_xdp_rx_hash_supported(const struct xdp_md *ctx);
+u32 mlx4_xdp_rx_hash(const struct xdp_md *ctx);
+

These are ethernet only functions, not known to the mlx4 core driver. Please move to mlx4_en.h, and use mlx4_en_xdp_*() prefix.

For sure, thanks for the review!

Quoted 1 line
Quoted 1 line
include/linux/mlx4/device.h +0 -0
  #endif /* MLX4_DEVICE_H */
#37 lore raw

Re: [PATCH bpf-next v3 08/12] mxl4: Support RX XDP metadata

Quoted 9 lines · Stanislav Fomichev
Quoted 3 lines · Tariq Toukan

Typo in title mxl4 -> mlx4.
Preferably: net/mlx4_en.

Oh, I always have to fight with this. Somehow mxl feels more natural :-) Thanks for spotting, will use net/mlx4_en instead. (presumably the same should be for mlx5?)

For the newer mlx5 driver we use a shorter form, net/mlx5e.

Quoted 182 lines
Quoted 170 lines · Stanislav Fomichev
Quoted 169 lines

RX timestamp and hash for now. Tested using the prog from the next patch.

Also enabling xdp metadata support; don't see why it's disabled, there is enough headroom..

Cc Tariq Toukan <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
   drivers/net/ethernet/mellanox/mlx4/en_clock.c | 13 +++++--
   .../net/ethernet/mellanox/mlx4/en_netdev.c    | 10 +++++
   drivers/net/ethernet/mellanox/mlx4/en_rx.c    | 38 ++++++++++++++++++-
   drivers/net/ethernet/mellanox/mlx4/mlx4_en.h  |  1 +
   include/linux/mlx4/device.h                   |  7 ++++
   5 files changed, 64 insertions(+), 5 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_clock.c +9 -4
index 98b5ffb4d729..9e3b76182088 100644
@@ -58,9 +58,7 @@ u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe)
       return hi | lo;
   }
 
-void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
-                         struct skb_shared_hwtstamps *hwts,
-                         u64 timestamp)
+u64 mlx4_en_get_hwtstamp(struct mlx4_en_dev *mdev, u64 timestamp)
   {
       unsigned int seq;
       u64 nsec;
@@ -70,8 +68,15 @@ void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
               nsec = timecounter_cyc2time(&mdev->clock, timestamp);
       } while (read_seqretry(&mdev->clock_lock, seq));
 
+     return ns_to_ktime(nsec);
+}
+
+void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
+                         struct skb_shared_hwtstamps *hwts,
+                         u64 timestamp)
+{
       memset(hwts, 0, sizeof(struct skb_shared_hwtstamps));
-     hwts->hwtstamp = ns_to_ktime(nsec);
+     hwts->hwtstamp = mlx4_en_get_hwtstamp(mdev, timestamp);
   }
 
   /**
drivers/net/ethernet/mellanox/mlx4/en_netdev.c +10 -0
index 8800d3f1f55c..1cb63746a851 100644
@@ -2855,6 +2855,11 @@ static const struct net_device_ops mlx4_netdev_ops = {
       .ndo_features_check     = mlx4_en_features_check,
       .ndo_set_tx_maxrate     = mlx4_en_set_tx_maxrate,
       .ndo_bpf                = mlx4_xdp,
+
+     .ndo_xdp_rx_timestamp_supported = mlx4_xdp_rx_timestamp_supported,
+     .ndo_xdp_rx_timestamp   = mlx4_xdp_rx_timestamp,
+     .ndo_xdp_rx_hash_supported = mlx4_xdp_rx_hash_supported,
+     .ndo_xdp_rx_hash        = mlx4_xdp_rx_hash,
   };
 
   static const struct net_device_ops mlx4_netdev_ops_master = {
@@ -2887,6 +2892,11 @@ static const struct net_device_ops mlx4_netdev_ops_master = {
       .ndo_features_check     = mlx4_en_features_check,
       .ndo_set_tx_maxrate     = mlx4_en_set_tx_maxrate,
       .ndo_bpf                = mlx4_xdp,
+
+     .ndo_xdp_rx_timestamp_supported = mlx4_xdp_rx_timestamp_supported,
+     .ndo_xdp_rx_timestamp   = mlx4_xdp_rx_timestamp,
+     .ndo_xdp_rx_hash_supported = mlx4_xdp_rx_hash_supported,
+     .ndo_xdp_rx_hash        = mlx4_xdp_rx_hash,
   };
 
   struct mlx4_en_bond {
drivers/net/ethernet/mellanox/mlx4/en_rx.c +37 -1
index 9c114fc723e3..1b8e1b2d8729 100644
@@ -663,8 +663,40 @@ static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
 
   struct mlx4_xdp_buff {
       struct xdp_buff xdp;
+     struct mlx4_cqe *cqe;
+     struct mlx4_en_dev *mdev;
+     struct mlx4_en_rx_ring *ring;
+     struct net_device *dev;
   };
 
+bool mlx4_xdp_rx_timestamp_supported(const struct xdp_md *ctx)
+{
+     struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+     return _ctx->ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL;
+}
+
+u64 mlx4_xdp_rx_timestamp(const struct xdp_md *ctx)
+{
+     struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+     return mlx4_en_get_hwtstamp(_ctx->mdev, mlx4_en_get_cqe_ts(_ctx->cqe));
+}
+
+bool mlx4_xdp_rx_hash_supported(const struct xdp_md *ctx)
+{
+     struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+     return _ctx->dev->features & NETIF_F_RXHASH;
+}
+
+u32 mlx4_xdp_rx_hash(const struct xdp_md *ctx)
+{
+     struct mlx4_xdp_buff *_ctx = (void *)ctx;
+
+     return be32_to_cpu(_ctx->cqe->immed_rss_invalid);
+}
+
   int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
   {
       struct mlx4_en_priv *priv = netdev_priv(dev);
@@ -781,8 +813,12 @@ int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int bud
                                               DMA_FROM_DEVICE);
 
                       xdp_prepare_buff(&mxbuf.xdp, va - frags[0].page_offset,
-                                      frags[0].page_offset, length, false);
+                                      frags[0].page_offset, length, true);
                       orig_data = mxbuf.xdp.data;
+                     mxbuf.cqe = cqe;
+                     mxbuf.mdev = priv->mdev;
+                     mxbuf.ring = ring;
+                     mxbuf.dev = dev;
 
                       act = bpf_prog_run_xdp(xdp_prog, &mxbuf.xdp);
 
drivers/net/ethernet/mellanox/mlx4/mlx4_en.h +1 -0
index e132ff4c82f2..b7c0d4899ad7 100644
@@ -792,6 +792,7 @@ int mlx4_en_netdev_event(struct notifier_block *this,
    * Functions for time stamping
    */
   u64 mlx4_en_get_cqe_ts(struct mlx4_cqe *cqe);
+u64 mlx4_en_get_hwtstamp(struct mlx4_en_dev *mdev, u64 timestamp);
   void mlx4_en_fill_hwtstamps(struct mlx4_en_dev *mdev,
                           struct skb_shared_hwtstamps *hwts,
                           u64 timestamp);
include/linux/mlx4/device.h +2 -0
index 6646634a0b9d..d5904da1d490 100644
@@ -1585,4 +1585,11 @@ static inline int mlx4_get_num_reserved_uar(struct mlx4_dev *dev)
       /* The first 128 UARs are used for EQ doorbells */
       return (128 >> (PAGE_SHIFT - dev->uar_page_shift));
   }
+
+struct xdp_md;
Quoted 8 lines

include/linux/mlx4/device.h static inline int mlx4_get_num_reserved_uar(struct mlx4_dev *dev) · line 1594

Quoted 5 lines
include/linux/mlx4/device.h +5 -0
+bool mlx4_xdp_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 mlx4_xdp_rx_timestamp(const struct xdp_md *ctx);
+bool mlx4_xdp_rx_hash_supported(const struct xdp_md *ctx);
+u32 mlx4_xdp_rx_hash(const struct xdp_md *ctx);
+

These are ethernet only functions, not known to the mlx4 core driver. Please move to mlx4_en.h, and use mlx4_en_xdp_*() prefix.

For sure, thanks for the review!

Quoted 1 line
Quoted 1 line
include/linux/mlx4/device.h +0 -0
   #endif /* MLX4_DEVICE_H */
#38 lore raw

PATCH v3 9/12 xsk: Add cb area to struct xdp_buff_xsk

In reply to Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Add an area after the xdp_buff in struct xdp_buff_xsk that drivers can use to stash extra information to use in metadata kfuncs. The maximum size of 24 bytes means the full xdp_buff_xsk structure will take up exactly two cache lines (with the cb field spanning both). Also add a macro drivers can use to check their own wrapping structs against the available size.

Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 include/net/xsk_buff_pool.h | 5 +++++
 1 file changed, 5 insertions(+)
include/net/xsk_buff_pool.h +5 -0
index f787c3f524b0..3e952e569418 100644
@@ -19,8 +19,11 @@ struct xdp_sock;
 struct device;
 struct page;
 
+#define XSK_PRIV_MAX 24
+
 struct xdp_buff_xsk {
 	struct xdp_buff xdp;
+	u8 cb[XSK_PRIV_MAX];
 	dma_addr_t dma;
 	dma_addr_t frame_dma;
 	struct xsk_buff_pool *pool;
@@ -28,6 +31,8 @@ struct xdp_buff_xsk {
 	struct list_head free_list_node;
 };
 
+#define XSK_CHECK_PRIV_TYPE(t) BUILD_BUG_ON(sizeof(t) > offsetofend(struct xdp_buff_xsk, cb))
+
 struct xsk_dma_map {
 	dma_addr_t *dma_pages;
 	struct device *dev;
Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#39 lore raw

PATCH v3 10/12 mlx5: Introduce mlx5_xdp_buff wrapper for xdp_buff

In reply to Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Preparation for implementing HW metadata kfuncs. No functional change.

Cc Saeed Mahameed <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 drivers/net/ethernet/mellanox/mlx5/core/en.h  |  1 +
 .../net/ethernet/mellanox/mlx5/core/en/xdp.c  |  3 +-
 .../net/ethernet/mellanox/mlx5/core/en/xdp.h  |  6 +-
 .../ethernet/mellanox/mlx5/core/en/xsk/rx.c   | 25 +++++----
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 56 +++++++++----------
 5 files changed, 49 insertions(+), 42 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en.h +1 -0
index ff5b302531d5..cdbaac5f6d25 100644
@@ -469,6 +469,7 @@ struct mlx5e_txqsq {
 union mlx5e_alloc_unit {
 	struct page *page;
 	struct xdp_buff *xsk;
+	struct mlx5_xdp_buff *mxbuf;
 };
 
 /* XDP packets can be transmitted in different ways. On completion, we need to
drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c +2 -1
index 20507ef2f956..db49b813bcb5 100644
@@ -158,8 +158,9 @@ mlx5e_xmit_xdp_buff(struct mlx5e_xdpsq *sq, struct mlx5e_rq *rq,
 
 /* returns true if packet was consumed by xdp */
 bool mlx5e_xdp_handle(struct mlx5e_rq *rq, struct page *page,
-		      struct bpf_prog *prog, struct xdp_buff *xdp)
+		      struct bpf_prog *prog, struct mlx5_xdp_buff *mxbuf)
 {
+	struct xdp_buff *xdp = &mxbuf->xdp;
 	u32 act;
 	int err;
 
drivers/net/ethernet/mellanox/mlx5/core/en/xdp.h +5 -1
index bc2d9034af5b..a33b448d542d 100644
@@ -44,10 +44,14 @@
 	(MLX5E_XDP_INLINE_WQE_MAX_DS_CNT * MLX5_SEND_WQE_DS - \
 	 sizeof(struct mlx5_wqe_inline_seg))
 
+struct mlx5_xdp_buff {
+	struct xdp_buff xdp;
+};
+
 struct mlx5e_xsk_param;
 int mlx5e_xdp_max_mtu(struct mlx5e_params *params, struct mlx5e_xsk_param *xsk);
 bool mlx5e_xdp_handle(struct mlx5e_rq *rq, struct page *page,
-		      struct bpf_prog *prog, struct xdp_buff *xdp);
+		      struct bpf_prog *prog, struct mlx5_xdp_buff *mlctx);
 void mlx5e_xdp_mpwqe_complete(struct mlx5e_xdpsq *sq);
 bool mlx5e_poll_xdpsq_cq(struct mlx5e_cq *cq);
 void mlx5e_free_xdpsq_descs(struct mlx5e_xdpsq *sq);
drivers/net/ethernet/mellanox/mlx5/core/en/xsk/rx.c +13 -12
index c91b54d9ff27..5e88dc61824e 100644
@@ -22,6 +22,7 @@ int mlx5e_xsk_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
 		goto err;
 
 	BUILD_BUG_ON(sizeof(wi->alloc_units[0]) != sizeof(wi->alloc_units[0].xsk));
+	XSK_CHECK_PRIV_TYPE(struct mlx5_xdp_buff);
 	batch = xsk_buff_alloc_batch(rq->xsk_pool, (struct xdp_buff **)wi->alloc_units,
 				     rq->mpwqe.pages_per_wqe);
 
@@ -233,7 +234,7 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
 						    u32 head_offset,
 						    u32 page_idx)
 {
-	struct xdp_buff *xdp = wi->alloc_units[page_idx].xsk;
+	struct mlx5_xdp_buff *mxbuf = wi->alloc_units[page_idx].mxbuf;
 	struct bpf_prog *prog;
 
 	/* Check packet size. Note LRO doesn't use linear SKB */
@@ -249,9 +250,9 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
 	 */
 	WARN_ON_ONCE(head_offset);
 
-	xsk_buff_set_size(xdp, cqe_bcnt);
-	xsk_buff_dma_sync_for_cpu(xdp, rq->xsk_pool);
-	net_prefetch(xdp->data);
+	xsk_buff_set_size(&mxbuf->xdp, cqe_bcnt);
+	xsk_buff_dma_sync_for_cpu(&mxbuf->xdp, rq->xsk_pool);
+	net_prefetch(mxbuf->xdp.data);
 
 	/* Possible flows:
 	 * - XDP_REDIRECT to XSKMAP:
@@ -269,7 +270,7 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
 	 */
 
 	prog = rcu_dereference(rq->xdp_prog);
-	if (likely(prog && mlx5e_xdp_handle(rq, NULL, prog, xdp))) {
+	if (likely(prog && mlx5e_xdp_handle(rq, NULL, prog, mxbuf))) {
 		if (likely(__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags)))
 			__set_bit(page_idx, wi->xdp_xmit_bitmap); /* non-atomic */
 		return NULL; /* page/packet was consumed by XDP */
@@ -278,14 +279,14 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
 	/* XDP_PASS: copy the data from the UMEM to a new SKB and reuse the
 	 * frame. On SKB allocation failure, NULL is returned.
 	 */
-	return mlx5e_xsk_construct_skb(rq, xdp);
+	return mlx5e_xsk_construct_skb(rq, &mxbuf->xdp);
 }
 
 struct sk_buff *mlx5e_xsk_skb_from_cqe_linear(struct mlx5e_rq *rq,
 					      struct mlx5e_wqe_frag_info *wi,
 					      u32 cqe_bcnt)
 {
-	struct xdp_buff *xdp = wi->au->xsk;
+	struct mlx5_xdp_buff *mxbuf = wi->au->mxbuf;
 	struct bpf_prog *prog;
 
 	/* wi->offset is not used in this function, because xdp->data and the
@@ -295,17 +296,17 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_linear(struct mlx5e_rq *rq,
 	 */
 	WARN_ON_ONCE(wi->offset);
 
-	xsk_buff_set_size(xdp, cqe_bcnt);
-	xsk_buff_dma_sync_for_cpu(xdp, rq->xsk_pool);
-	net_prefetch(xdp->data);
+	xsk_buff_set_size(&mxbuf->xdp, cqe_bcnt);
+	xsk_buff_dma_sync_for_cpu(&mxbuf->xdp, rq->xsk_pool);
+	net_prefetch(mxbuf->xdp.data);
 
 	prog = rcu_dereference(rq->xdp_prog);
-	if (likely(prog && mlx5e_xdp_handle(rq, NULL, prog, xdp)))
+	if (likely(prog && mlx5e_xdp_handle(rq, NULL, prog, mxbuf)))
 		return NULL; /* page/packet was consumed by XDP */
 
 	/* XDP_PASS: copy the data from the UMEM to a new SKB. The frame reuse
 	 * will be handled by mlx5e_free_rx_wqe.
 	 * On SKB allocation failure, NULL is returned.
 	 */
-	return mlx5e_xsk_construct_skb(rq, xdp);
+	return mlx5e_xsk_construct_skb(rq, &mxbuf->xdp);
 }
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +28 -28

139 lines not on this page · raw message

Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#40 lore raw

PATCH v3 11/12 mlx5: Support RX XDP metadata

In reply to Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

Cc Saeed Mahameed <[email protected]>
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 drivers/net/ethernet/mellanox/mlx5/core/en.h  | 10 +++-
 .../net/ethernet/mellanox/mlx5/core/en/xdp.c  | 29 +++++++++++
 .../net/ethernet/mellanox/mlx5/core/en/xdp.h  |  7 +++
 .../ethernet/mellanox/mlx5/core/en/xsk/rx.c   | 10 ++++
 .../ethernet/mellanox/mlx5/core/en/xsk/rx.h   |  2 +
 .../net/ethernet/mellanox/mlx5/core/en_main.c |  4 ++
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 48 ++++++++++---------
 7 files changed, 85 insertions(+), 25 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en.h +8 -2
index cdbaac5f6d25..8337ff0cacd5 100644
@@ -627,10 +627,11 @@ struct mlx5e_rq;
 typedef void (*mlx5e_fp_handle_rx_cqe)(struct mlx5e_rq*, struct mlx5_cqe64*);
 typedef struct sk_buff *
 (*mlx5e_fp_skb_from_cqe_mpwrq)(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
-			       u16 cqe_bcnt, u32 head_offset, u32 page_idx);
+			       struct mlx5_cqe64 *cqe, u16 cqe_bcnt,
+			       u32 head_offset, u32 page_idx);
 typedef struct sk_buff *
 (*mlx5e_fp_skb_from_cqe)(struct mlx5e_rq *rq, struct mlx5e_wqe_frag_info *wi,
-			 u32 cqe_bcnt);
+			 struct mlx5_cqe64 *cqe, u32 cqe_bcnt);
 typedef bool (*mlx5e_fp_post_rx_wqes)(struct mlx5e_rq *rq);
 typedef void (*mlx5e_fp_dealloc_wqe)(struct mlx5e_rq*, u16);
 typedef void (*mlx5e_fp_shampo_dealloc_hd)(struct mlx5e_rq*, u16, u16, bool);
@@ -1036,6 +1037,11 @@ int mlx5e_vlan_rx_kill_vid(struct net_device *dev, __always_unused __be16 proto,
 			   u16 vid);
 void mlx5e_timestamp_init(struct mlx5e_priv *priv);
 
+static inline bool mlx5e_rx_hw_stamp(struct hwtstamp_config *config)
+{
+	return config->rx_filter == HWTSTAMP_FILTER_ALL;
+}
+
 struct mlx5e_xsk_param;
 
 struct mlx5e_rq_param;
drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c +29 -0
index db49b813bcb5..2a4700b3695a 100644
@@ -156,6 +156,35 @@ mlx5e_xmit_xdp_buff(struct mlx5e_xdpsq *sq, struct mlx5e_rq *rq,
 	return true;
 }
 
+bool mlx5e_xdp_rx_timestamp_supported(const struct xdp_md *ctx)
+{
+	const struct mlx5_xdp_buff *_ctx = (void *)ctx;
+
+	return mlx5e_rx_hw_stamp(_ctx->rq->tstamp);
+}
+
+u64 mlx5e_xdp_rx_timestamp(const struct xdp_md *ctx)
+{
+	const struct mlx5_xdp_buff *_ctx = (void *)ctx;
+
+	return mlx5e_cqe_ts_to_ns(_ctx->rq->ptp_cyc2time,
+				  _ctx->rq->clock, get_cqe_ts(_ctx->cqe));
+}
+
+bool mlx5e_xdp_rx_hash_supported(const struct xdp_md *ctx)
+{
+	const struct mlx5_xdp_buff *_ctx = (void *)ctx;
+
+	return _ctx->xdp.rxq->dev->features & NETIF_F_RXHASH;
+}
+
+u32 mlx5e_xdp_rx_hash(const struct xdp_md *ctx)
+{
+	const struct mlx5_xdp_buff *_ctx = (void *)ctx;
+
+	return be32_to_cpu(_ctx->cqe->rss_hash_result);
+}
+
 /* returns true if packet was consumed by xdp */
 bool mlx5e_xdp_handle(struct mlx5e_rq *rq, struct page *page,
 		      struct bpf_prog *prog, struct mlx5_xdp_buff *mxbuf)
drivers/net/ethernet/mellanox/mlx5/core/en/xdp.h +7 -0
index a33b448d542d..a5fc30b07617 100644
@@ -46,6 +46,8 @@
 
 struct mlx5_xdp_buff {
 	struct xdp_buff xdp;
+	struct mlx5_cqe64 *cqe;
+	struct mlx5e_rq *rq;
 };
 
 struct mlx5e_xsk_param;
@@ -60,6 +62,11 @@ void mlx5e_xdp_rx_poll_complete(struct mlx5e_rq *rq);
 int mlx5e_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **frames,
 		   u32 flags);
 
+bool mlx5e_xdp_rx_hash_supported(const struct xdp_md *ctx);
+u32 mlx5e_xdp_rx_hash(const struct xdp_md *ctx);
+bool mlx5e_xdp_rx_timestamp_supported(const struct xdp_md *ctx);
+u64 mlx5e_xdp_rx_timestamp(const struct xdp_md *ctx);
+
 INDIRECT_CALLABLE_DECLARE(bool mlx5e_xmit_xdp_frame_mpwqe(struct mlx5e_xdpsq *sq,
 							  struct mlx5e_xmit_data *xdptxd,
 							  struct skb_shared_info *sinfo,
drivers/net/ethernet/mellanox/mlx5/core/en/xsk/rx.c +10 -0
index 5e88dc61824e..05cf7987585a 100644
@@ -49,6 +49,7 @@ int mlx5e_xsk_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
 			umr_wqe->inline_mtts[i] = (struct mlx5_mtt) {
 				.ptag = cpu_to_be64(addr | MLX5_EN_WR),
 			};
+			wi->alloc_units[i].mxbuf->rq = rq;
 		}
 	} else if (unlikely(rq->mpwqe.umr_mode == MLX5E_MPWRQ_UMR_MODE_UNALIGNED)) {
 		for (i = 0; i < batch; i++) {
@@ -58,6 +59,7 @@ int mlx5e_xsk_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
 				.key = rq->mkey_be,
 				.va = cpu_to_be64(addr),
 			};
+			wi->alloc_units[i].mxbuf->rq = rq;
 		}
 	} else if (likely(rq->mpwqe.umr_mode == MLX5E_MPWRQ_UMR_MODE_TRIPLE)) {
 		u32 mapping_size = 1 << (rq->mpwqe.page_shift - 2);
@@ -81,6 +83,7 @@ int mlx5e_xsk_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
 				.key = rq->mkey_be,
 				.va = cpu_to_be64(rq->wqe_overflow.addr),
 			};
+			wi->alloc_units[i].mxbuf->rq = rq;
 		}
 	} else {
 		__be32 pad_size = cpu_to_be32((1 << rq->mpwqe.page_shift) -
@@ -100,6 +103,7 @@ int mlx5e_xsk_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
 				.va = cpu_to_be64(rq->wqe_overflow.addr),
 				.bcount = pad_size,
 			};
+			wi->alloc_units[i].mxbuf->rq = rq;
 		}
 	}
 
@@ -230,6 +234,7 @@ static struct sk_buff *mlx5e_xsk_construct_skb(struct mlx5e_rq *rq, struct xdp_b
 
 struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
 						    struct mlx5e_mpw_info *wi,
+						    struct mlx5_cqe64 *cqe,
 						    u16 cqe_bcnt,
 						    u32 head_offset,
 						    u32 page_idx)
@@ -250,6 +255,8 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
 	 */
 	WARN_ON_ONCE(head_offset);
 
+	/* mxbuf->rq is set on allocation, but cqe is per-packet so set it here */
+	mxbuf->cqe = cqe;
 	xsk_buff_set_size(&mxbuf->xdp, cqe_bcnt);
 	xsk_buff_dma_sync_for_cpu(&mxbuf->xdp, rq->xsk_pool);
 	net_prefetch(mxbuf->xdp.data);
@@ -284,6 +291,7 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
 
 struct sk_buff *mlx5e_xsk_skb_from_cqe_linear(struct mlx5e_rq *rq,
 					      struct mlx5e_wqe_frag_info *wi,
+					      struct mlx5_cqe64 *cqe,
 					      u32 cqe_bcnt)
 {
 	struct mlx5_xdp_buff *mxbuf = wi->au->mxbuf;
@@ -296,6 +304,8 @@ struct sk_buff *mlx5e_xsk_skb_from_cqe_linear(struct mlx5e_rq *rq,
 	 */
 	WARN_ON_ONCE(wi->offset);
 
+	/* mxbuf->rq is set on allocation, but cqe is per-packet so set it here */
+	mxbuf->cqe = cqe;
 	xsk_buff_set_size(&mxbuf->xdp, cqe_bcnt);
 	xsk_buff_dma_sync_for_cpu(&mxbuf->xdp, rq->xsk_pool);
 	net_prefetch(mxbuf->xdp.data);
drivers/net/ethernet/mellanox/mlx5/core/en/xsk/rx.h +2 -0
index 087c943bd8e9..cefc0ef6105d 100644
@@ -13,11 +13,13 @@ int mlx5e_xsk_alloc_rx_wqes_batched(struct mlx5e_rq *rq, u16 ix, int wqe_bulk);
 int mlx5e_xsk_alloc_rx_wqes(struct mlx5e_rq *rq, u16 ix, int wqe_bulk);
 struct sk_buff *mlx5e_xsk_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq,
 						    struct mlx5e_mpw_info *wi,
+						    struct mlx5_cqe64 *cqe,
 						    u16 cqe_bcnt,
 						    u32 head_offset,
 						    u32 page_idx);
 struct sk_buff *mlx5e_xsk_skb_from_cqe_linear(struct mlx5e_rq *rq,
 					      struct mlx5e_wqe_frag_info *wi,
+					      struct mlx5_cqe64 *cqe,
 					      u32 cqe_bcnt);
 
 #endif /* __MLX5_EN_XSK_RX_H__ */
drivers/net/ethernet/mellanox/mlx5/core/en_main.c +4 -0
index 217c8a478977..967a82bf34b4 100644
@@ -4891,6 +4891,10 @@ const struct net_device_ops mlx5e_netdev_ops = {
 	.ndo_tx_timeout          = mlx5e_tx_timeout,
 	.ndo_bpf		 = mlx5e_xdp,
 	.ndo_xdp_xmit            = mlx5e_xdp_xmit,
+	.ndo_xdp_rx_timestamp_supported = mlx5e_xdp_rx_timestamp_supported,
+	.ndo_xdp_rx_timestamp    = mlx5e_xdp_rx_timestamp,
+	.ndo_xdp_rx_hash_supported = mlx5e_xdp_rx_hash_supported,
+	.ndo_xdp_rx_hash         = mlx5e_xdp_rx_hash,
 	.ndo_xsk_wakeup          = mlx5e_xsk_wakeup,
 #ifdef CONFIG_MLX5_EN_ARFS
 	.ndo_rx_flow_steer	 = mlx5e_rx_flow_steer,
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +25 -23

154 lines not on this page · raw message

Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#41 lore raw

Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

-Toke

#42 lore raw

Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 28 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

Quoted 8 lines

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

#43 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

-Toke

#44 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

#45 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 60 lines · Alexei Starovoitov
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

Quoted 7 lines

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

Is that "planning" or "actively working on"? Just trying to get a sense of the time frames here, as this sounds neat, but also something that could potentially require quite a bit of fiddling with the build system to get to work? :)

-Toke

#46 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 71 lines · Toke Høiland-Jørgensen
Quoted 60 lines · Alexei Starovoitov
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

Quoted 7 lines

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

Quoted 4 lines

Is that "planning" or "actively working on"? Just trying to get a sense of the time frames here, as this sounds neat, but also something that could potentially require quite a bit of fiddling with the build system to get to work? :)

"planning", but regardless how long it takes I'd rather not add any more tech debt in the form of manual bpf asm generation. We have too much of it already: gen_lookup, convert_ctx_access, etc.

#47 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 72 lines · Alexei Starovoitov
Quoted 71 lines · Toke Høiland-Jørgensen
Quoted 67 lines · Alexei Starovoitov
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

Quoted 8 lines
Quoted 4 lines

Is that "planning" or "actively working on"? Just trying to get a sense of the time frames here, as this sounds neat, but also something that could potentially require quite a bit of fiddling with the build system to get to work? :)

"planning", but regardless how long it takes I'd rather not add any more tech debt in the form of manual bpf asm generation. We have too much of it already: gen_lookup, convert_ctx_access, etc.

Right, I'm no fan of the manual ASM stuff either. However, if we're stuck with the function call overhead for the foreseeable future, maybe we should think about other ways of cutting down the number of function calls needed?

One thing I can think of is to get rid of the individual _supported() kfuncs and instead have a single one that lets you query multiple features at once, like:

__u64 features_supported, features_wanted = XDP_META_RX_HASH | XDP_META_TIMESTAMP;

features_supported = bpf_xdp_metadata_query_features(ctx, features_wanted);

if (features_supported & XDP_META_RX_HASH)
hash = bpf_xdp_metadata_rx_hash(ctx);

...etc

-Toke

#48 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 93 lines · Toke Høiland-Jørgensen
Quoted 72 lines · Alexei Starovoitov
Quoted 71 lines · Toke Høiland-Jørgensen
Quoted 67 lines · Alexei Starovoitov
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

Quoted 8 lines
Quoted 4 lines

Is that "planning" or "actively working on"? Just trying to get a sense of the time frames here, as this sounds neat, but also something that could potentially require quite a bit of fiddling with the build system to get to work? :)

"planning", but regardless how long it takes I'd rather not add any more tech debt in the form of manual bpf asm generation. We have too much of it already: gen_lookup, convert_ctx_access, etc.

Right, I'm no fan of the manual ASM stuff either. However, if we're stuck with the function call overhead for the foreseeable future, maybe we should think about other ways of cutting down the number of function calls needed?

One thing I can think of is to get rid of the individual _supported() kfuncs and instead have a single one that lets you query multiple features at once, like:

Quoted 8 lines

__u64 features_supported, features_wanted = XDP_META_RX_HASH | XDP_META_TIMESTAMP;

features_supported = bpf_xdp_metadata_query_features(ctx, features_wanted);

if (features_supported & XDP_META_RX_HASH)
hash = bpf_xdp_metadata_rx_hash(ctx);

...etc

I'm not too happy about having the bitmasks tbh :-(
If we want to get rid of the cost of those _supported calls, maybe we can do some kind of libbpf-like probing? That would require loading a program + waiting for some packet though :-(

Or maybe they can just be cached for now?

if (unlikely(!got_first_packet)) {
have_hash = bpf_xdp_metadata_rx_hash_supported(); have_timestamp = bpf_xdp_metadata_rx_timestamp_supported(); got_first_packet = true;
}

if (have_hash) {}
if (have_timestamp) {}

That should hopefully work until generic inlining infra?

Quoted 2 lines

-Toke

#49 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 108 lines · Stanislav Fomichev
Quoted 93 lines · Toke Høiland-Jørgensen
Quoted 72 lines · Alexei Starovoitov
Quoted 71 lines · Toke Høiland-Jørgensen
Quoted 67 lines · Alexei Starovoitov
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

Quoted 8 lines
Quoted 4 lines

Is that "planning" or "actively working on"? Just trying to get a sense of the time frames here, as this sounds neat, but also something that could potentially require quite a bit of fiddling with the build system to get to work? :)

"planning", but regardless how long it takes I'd rather not add any more tech debt in the form of manual bpf asm generation. We have too much of it already: gen_lookup, convert_ctx_access, etc.

Right, I'm no fan of the manual ASM stuff either. However, if we're stuck with the function call overhead for the foreseeable future, maybe we should think about other ways of cutting down the number of function calls needed?

One thing I can think of is to get rid of the individual _supported() kfuncs and instead have a single one that lets you query multiple features at once, like:

Quoted 8 lines

__u64 features_supported, features_wanted = XDP_META_RX_HASH | XDP_META_TIMESTAMP;

features_supported = bpf_xdp_metadata_query_features(ctx, features_wanted);

if (features_supported & XDP_META_RX_HASH)
hash = bpf_xdp_metadata_rx_hash(ctx);

...etc

I'm not too happy about having the bitmasks tbh :-(
If we want to get rid of the cost of those _supported calls, maybe we can do some kind of libbpf-like probing? That would require loading a program + waiting for some packet though :-(

Quoted 7 lines

Or maybe they can just be cached for now?

if (unlikely(!got_first_packet)) {
have_hash = bpf_xdp_metadata_rx_hash_supported(); have_timestamp = bpf_xdp_metadata_rx_timestamp_supported(); got_first_packet = true;
}

hash/timestap/csum is per packet .. vlan as well depending how you look at it..

Sorry I haven't been following the progress of xdp meta data, but why did we drop the idea of btf and driver copying metdata in front of the xdp frame ?

hopefully future HW generations will do that for free ..

if btf is the problem then each vendor can provide a bpf func(s) that would parse the metdata inside of the xdp/bpf prog domain to help programs extract the vendor specific data..

Quoted 8 lines

if (have_hash) {}
if (have_timestamp) {}

That should hopefully work until generic inlining infra?

Quoted 2 lines

-Toke

#50 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 111 lines · Saeed Mahameed
Quoted 110 lines · Stanislav Fomichev
Quoted 93 lines · Toke Høiland-Jørgensen
Quoted 72 lines · Alexei Starovoitov
Quoted 71 lines · Toke Høiland-Jørgensen
Quoted 67 lines · Alexei Starovoitov
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP:         25,678,262 pps / 38.94 ns/pkt XDP_DROP + read metadata:  23,924,109 pps / 41.80 ns/pkt Overhead:                   1,754,153 pps /  2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

Quoted 8 lines
Quoted 4 lines

Is that "planning" or "actively working on"? Just trying to get a sense of the time frames here, as this sounds neat, but also something that could potentially require quite a bit of fiddling with the build system to get to work? :)

"planning", but regardless how long it takes I'd rather not add any more tech debt in the form of manual bpf asm generation. We have too much of it already: gen_lookup, convert_ctx_access, etc.

Right, I'm no fan of the manual ASM stuff either. However, if we're stuck with the function call overhead for the foreseeable future, maybe we should think about other ways of cutting down the number of function calls needed?

One thing I can think of is to get rid of the individual _supported() kfuncs and instead have a single one that lets you query multiple features at once, like:

Quoted 8 lines

__u64 features_supported, features_wanted = XDP_META_RX_HASH | XDP_META_TIMESTAMP;

features_supported = bpf_xdp_metadata_query_features(ctx, features_wanted);

if (features_supported & XDP_META_RX_HASH)
  hash = bpf_xdp_metadata_rx_hash(ctx);

...etc

I'm not too happy about having the bitmasks tbh :-(
If we want to get rid of the cost of those _supported calls, maybe we can do some kind of libbpf-like probing? That would require loading a program + waiting for some packet though :-(

Or maybe they can just be cached for now?

Quoted 8 lines
Quoted 5 lines

if (unlikely(!got_first_packet)) {
 have_hash = bpf_xdp_metadata_rx_hash_supported(); have_timestamp = bpf_xdp_metadata_rx_timestamp_supported(); got_first_packet = true;
}

hash/timestap/csum is per packet .. vlan as well depending how you look at it..

True, we cannot cache this as it is *per packet* info.

Quoted 4 lines

Sorry I haven't been following the progress of xdp meta data, but why did we drop the idea of btf and driver copying metdata in front of the xdp frame ?

It took me some time to understand this new approach, and why it makes sense. This is my understanding of the design direction change:

This approach gives more control to the XDP BPF-prog to pick and choose which XDP hints are relevant for the specific use-case. BPF-prog can also skip storing hints anywhere and just read+react on value (that e.g. comes from RX-desc).

For the use-cases redirect, AF_XDP, chained BPF-progs, XDP-to-TC, SKB-creation, we *do* need to store hints somewhere, as RX-desc will be out-of-scope. I this patchset hand-waves and says BPF-prog can just manually store this in a prog custom layout in metadata area. I'm not super happy with ignoring/hand-waving all these use-case, but I hope/think we later can extend this some more structure to support these use-cases better (with this patchset as a foundation).

I actually like this kfunc design, because the BPF-prog's get an intuitive API, and on driver side we can hide the details of howto extract the HW hints.

Quoted 1 line

hopefully future HW generations will do that for free ..

True. I think it is worth repeating, that the approach of storing HW hints in metadata area (in-front of packet data) was to allow future HW generations to write this. Thus, eliminating the 6 ns (that I showed it cost), and then it would be up-to XDP BPF-prog to pick and choose which to read, like this patchset already offers.

This patchset isn't incompatible with future HW generations doing this, as the kfunc would hide the details and point to this area instead of the RX-desc. While we get the "store for free" from hardware, I do worry that reading this memory area (which will part of DMA area) is going to be slower than reading from RX-desc.

Quoted 4 lines

if btf is the problem then each vendor can provide a bpf func(s) that would parse the metdata inside of the xdp/bpf prog domain to help programs extract the vendor specific data..

In some sense, if unroll will becomes a thing, then this patchset is partly doing this.

I did imagine that after/followup on XDP-hints with BTF patchset, we would allow drivers to load an BPF-prog that changed/selected which HW hints were relevant, to reduce those 6 ns overhead we introduced.

--Jesper

#51 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 18 lines · Jesper Dangaard Brouer
Quoted 2 lines

hash/timestap/csum is per packet .. vlan as well depending how you look at it..

True, we cannot cache this as it is *per packet* info.

Quoted 4 lines

Sorry I haven't been following the progress of xdp meta data, but why did we drop the idea of btf and driver copying metdata in front of the xdp frame ?

It took me some time to understand this new approach, and why it makes sense. This is my understanding of the design direction change:

This approach gives more control to the XDP BPF-prog to pick and choose which XDP hints are relevant for the specific use-case. BPF-prog can also skip storing hints anywhere and just read+react on value (that e.g. comes from RX-desc).

Quoted 7 lines

For the use-cases redirect, AF_XDP, chained BPF-progs, XDP-to-TC, SKB-creation, we *do* need to store hints somewhere, as RX-desc will be out-of-scope. I this patchset hand-waves and says BPF-prog can just manually store this in a prog custom layout in metadata area. I'm not super happy with ignoring/hand-waving all these use-case, but I hope/think we later can extend this some more structure to support these use-cases better (with this patchset as a foundation).

I don't think this approach "hand-waves" the need to store the metadata, it just declares it out of scope :)

Which makes sense, because "accessing the metadata" and "storing it for later use" are two different problems, where the second one build on top of the first one. I.e., once we have a way to access the data, we can build upon that to have a way to store it somewhere.

Quoted 3 lines

I actually like this kfunc design, because the BPF-prog's get an intuitive API, and on driver side we can hide the details of howto extract the HW hints.

+1

Quoted 8 lines
Quoted 1 line

hopefully future HW generations will do that for free ..

True. I think it is worth repeating, that the approach of storing HW hints in metadata area (in-front of packet data) was to allow future HW generations to write this. Thus, eliminating the 6 ns (that I showed it cost), and then it would be up-to XDP BPF-prog to pick and choose which to read, like this patchset already offers.

Quoted 5 lines

This patchset isn't incompatible with future HW generations doing this, as the kfunc would hide the details and point to this area instead of the RX-desc. While we get the "store for free" from hardware, I do worry that reading this memory area (which will part of DMA area) is going to be slower than reading from RX-desc.

Agreed (choked on the "isn't incompatible" double negative at first). If the hardware stores the data next to the packet data, the kfuncs can just read them from there. If it turns out that we can even make the layout for some fields the same across drivers, we could even have the generic kfunc implementations just read this area (which also nicely solves the "storage" problem).

-Toke

#52 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

In reply to Jesper Dangaard Brouer
Quoted 154 lines · Jesper Dangaard Brouer
Quoted 114 lines · Saeed Mahameed
Quoted 113 lines · Stanislav Fomichev
Quoted 96 lines · Toke Høiland-Jørgensen
Quoted 72 lines · Alexei Starovoitov
Quoted 71 lines · Toke Høiland-Jørgensen
Quoted 67 lines · Alexei Starovoitov
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

Quoted 8 lines
Quoted 4 lines

Is that "planning" or "actively working on"? Just trying to get a sense of the time frames here, as this sounds neat, but also something that could potentially require quite a bit of fiddling with the build system to get to work? :)

"planning", but regardless how long it takes I'd rather not add any more tech debt in the form of manual bpf asm generation. We have too much of it already: gen_lookup, convert_ctx_access, etc.

Right, I'm no fan of the manual ASM stuff either. However, if we're stuck with the function call overhead for the foreseeable future, maybe we should think about other ways of cutting down the number of function calls needed?

One thing I can think of is to get rid of the individual _supported() kfuncs and instead have a single one that lets you query multiple features at once, like:

__u64 features_supported, features_wanted = XDP_META_RX_HASH | XDP_META_TIMESTAMP;

Quoted 7 lines

features_supported = bpf_xdp_metadata_query_features(ctx, features_wanted);

if (features_supported & XDP_META_RX_HASH)
hash = bpf_xdp_metadata_rx_hash(ctx);

...etc

I'm not too happy about having the bitmasks tbh :-(
If we want to get rid of the cost of those _supported calls, maybe we can do some kind of libbpf-like probing? That would require loading a program + waiting for some packet though :-(

Or maybe they can just be cached for now?

Quoted 8 lines
Quoted 5 lines

if (unlikely(!got_first_packet)) {
have_hash = bpf_xdp_metadata_rx_hash_supported(); have_timestamp = bpf_xdp_metadata_rx_timestamp_supported(); got_first_packet = true;
}

hash/timestap/csum is per packet .. vlan as well depending how you look at it..

True, we cannot cache this as it is *per packet* info.

Quoted 4 lines

Sorry I haven't been following the progress of xdp meta data, but why did we drop the idea of btf and driver copying metdata in front of the xdp frame ?

It took me some time to understand this new approach, and why it makes sense. This is my understanding of the design direction change:

This approach gives more control to the XDP BPF-prog to pick and choose which XDP hints are relevant for the specific use-case. BPF-prog can also skip storing hints anywhere and just read+react on value (that e.g. comes from RX-desc).

For the use-cases redirect, AF_XDP, chained BPF-progs, XDP-to-TC, SKB-creation, we *do* need to store hints somewhere, as RX-desc will be out-of-scope. I this patchset hand-waves and says BPF-prog can just manually store this in a prog custom layout in metadata area. I'm not super happy with ignoring/hand-waving all these use-case, but I hope/think we later can extend this some more structure to support these use-cases better (with this patchset as a foundation).

I actually like this kfunc design, because the BPF-prog's get an intuitive API, and on driver side we can hide the details of howto extract the HW hints.

Quoted 7 lines
Quoted 1 line

hopefully future HW generations will do that for free ..

True. I think it is worth repeating, that the approach of storing HW hints in metadata area (in-front of packet data) was to allow future HW generations to write this. Thus, eliminating the 6 ns (that I showed it cost), and then it would be up-to XDP BPF-prog to pick and choose which to read, like this patchset already offers.

As a hope for future generators of hw, being able to choose a cpu to interrupt from a LPM table would be great. I keep hoping to find a card that can do this already...

Also I would like to thank everyone working on this project so far for what you've
accomplished. We're now pushing 20Gbit (through a vlan even) through libreqos.io for thousands of ISP subscribers using all this great stuff, on 16 cores at only 24% of cpu through CAKE and also successfully monitoring TCP RTTs at this scale via ebpf pping.

( https://www.yahoo.com/now/libreqoe-releases-version-1-3-214700756.html ) "Our hat is off to the creators of CAKE and the new Linux XDP and eBPF subsystems!"

In our case, timestamp, and *3* hashes, are needed for cake, and interrupting the right cpu would be great...

Quoted 21 lines

This patchset isn't incompatible with future HW generations doing this, as the kfunc would hide the details and point to this area instead of the RX-desc. While we get the "store for free" from hardware, I do worry that reading this memory area (which will part of DMA area) is going to be slower than reading from RX-desc.

Quoted 4 lines

if btf is the problem then each vendor can provide a bpf func(s) that would parse the metdata inside of the xdp/bpf prog domain to help programs extract the vendor specific data..

In some sense, if unroll will becomes a thing, then this patchset is partly doing this.

I did imagine that after/followup on XDP-hints with BTF patchset, we would allow drivers to load an BPF-prog that changed/selected which HW hints were relevant, to reduce those 6 ns overhead we introduced.

--Jesper

Signature · 3 lines
This song goes out to all the folk that thought Stadia would work:
https://www.linkedin.com/posts/dtaht_the-mushroom-song-activity-6981366665607352320-FXtz
Dave Täht CEO, TekLibre, LLC
#53 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

In reply to Stanislav Fomichev
Quoted 101 lines · Stanislav Fomichev
Quoted 100 lines · Toke Høiland-Jørgensen
Quoted 72 lines · Alexei Starovoitov
Quoted 71 lines · Toke Høiland-Jørgensen
Quoted 67 lines · Alexei Starovoitov
Quoted 54 lines · Toke Høiland-Jørgensen
Quoted 35 lines · Stanislav Fomichev
Quoted 34 lines · Toke Høiland-Jørgensen
Quoted 5 lines · Stanislav Fomichev

From: Toke Høiland-Jørgensen <[email protected]>

Support RX hash and timestamp metadata kfuncs. We need to pass in the cqe pointer to the mlx5e_skb_from* functions so it can be retrieved from the XDP ctx to do this.

So I finally managed to get enough ducks in row to actually benchmark this. With the caveat that I suddenly can't get the timestamp support to work (it was working in an earlier version, but now timestamp_supported() just returns false). I'm not sure if this is an issue with the enablement patch, or if I just haven't gotten the hardware configured properly. I'll investigate some more, but figured I'd post these results now:

Baseline XDP_DROP: 25,678,262 pps / 38.94 ns/pkt
XDP_DROP + read metadata: 23,924,109 pps / 41.80 ns/pkt
Overhead: 1,754,153 pps / 2.86 ns/pkt

As per the above, this is with calling three kfuncs/pkt (metadata_supported(), rx_hash_supported() and rx_hash()). So that's ~0.95 ns per function call, which is a bit less, but not far off from the ~1.2 ns that I'm used to. The tests where I accidentally called the default kfuncs cut off ~1.3 ns for one less kfunc call, so it's definitely in that ballpark.

I'm not doing anything with the data, just reading it into an on-stack buffer, so this is the smallest possible delta from just getting the data out of the driver. I did confirm that the call instructions are still in the BPF program bytecode when it's dumped back out from the kernel.

Quoted 8 lines
Quoted 2 lines

-Toke

Oh, that's great, thanks for running the numbers! Will definitely reference them in v4!
Presumably, we should be able to at least unroll most of the _supported callbacks if we want, they should be relatively easy; but the numbers look fine as is?

Well, this is for one (and a half) piece of metadata. If we extrapolate it adds up quickly. Say we add csum and vlan tags, say, and maybe another callback to get the type of hash (l3/l4). Those would probably be relevant for most packets in a fairly common setup. Extrapolating from the ~1 ns/call figure, that's 8 ns/pkt, which is 20% of the baseline of 39 ns.

Quoted 4 lines

So in that sense I still think unrolling makes sense. At least for the _supported() calls, as eating a whole function call just for that is probably a bit much (which I think was also Jakub's point in a sibling thread somewhere).

imo the overhead is tiny enough that we can wait until generic 'kfunc inlining' infra is ready.

We're planning to dual-compile some_kernel_file.c into native arch and into bpf arch.
Then the verifier will automatically inline bpf asm of corresponding kfunc.

Quoted 8 lines
Quoted 4 lines

Is that "planning" or "actively working on"? Just trying to get a sense of the time frames here, as this sounds neat, but also something that could potentially require quite a bit of fiddling with the build system to get to work? :)

"planning", but regardless how long it takes I'd rather not add any more tech debt in the form of manual bpf asm generation. We have too much of it already: gen_lookup, convert_ctx_access, etc.

Right, I'm no fan of the manual ASM stuff either. However, if we're stuck with the function call overhead for the foreseeable future, maybe we should think about other ways of cutting down the number of function calls needed?

One thing I can think of is to get rid of the individual _supported() kfuncs and instead have a single one that lets you query multiple features at once, like:

__u64 features_supported, features_wanted = XDP_META_RX_HASH | XDP_META_TIMESTAMP;

features_supported = bpf_xdp_metadata_query_features(ctx, features_wanted);

if (features_supported & XDP_META_RX_HASH)
hash = bpf_xdp_metadata_rx_hash(ctx);

Quoted 6 lines
Quoted 1 line

...etc

I'm not too happy about having the bitmasks tbh :-(
If we want to get rid of the cost of those _supported calls, maybe we can do some kind of libbpf-like probing? That would require loading a program + waiting for some packet though :-(

If we expect the program to do out of band probing, we could just get rid of the _supported() functions entirely?

I mean, to me, the whole point of having the separate _supported() function for each item was to have a lower-overhead way of checking if the metadata item was supported. But if the overhead is not actually lower (because both incur a function call), why have them at all? Then we could just change the implementation from this:

bool mlx5e_xdp_rx_hash_supported(const struct xdp_md *ctx)

{
	const struct mlx5_xdp_buff *_ctx = (void *)ctx;

	return _ctx->xdp.rxq->dev->features & NETIF_F_RXHASH;
}

u32 mlx5e_xdp_rx_hash(const struct xdp_md *ctx)

{
	const struct mlx5_xdp_buff *_ctx = (void *)ctx;

	return be32_to_cpu(_ctx->cqe->rss_hash_result);
}

to this:

u32 mlx5e_xdp_rx_hash(const struct xdp_md *ctx)

{
	const struct mlx5_xdp_buff *_ctx = (void *)ctx;

	if (!(_ctx->xdp.rxq->dev->features & NETIF_F_RXHASH))
                return 0;

	return be32_to_cpu(_ctx->cqe->rss_hash_result);
}

-Toke

maintainer
#54 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 26 lines · On Fri, 09 Dec 2022 15:42:37 +0100 Toke Høiland-Jørgensen wrote:

If we expect the program to do out of band probing, we could just get rid of the _supported() functions entirely?

I mean, to me, the whole point of having the separate _supported() function for each item was to have a lower-overhead way of checking if the metadata item was supported. But if the overhead is not actually lower (because both incur a function call), why have them at all? Then we could just change the implementation from this:

bool mlx5e_xdp_rx_hash_supported(const struct xdp_md *ctx)

{
	const struct mlx5_xdp_buff *_ctx = (void *)ctx;

	return _ctx->xdp.rxq->dev->features & NETIF_F_RXHASH;
}

u32 mlx5e_xdp_rx_hash(const struct xdp_md *ctx)

{
	const struct mlx5_xdp_buff *_ctx = (void *)ctx;

	return be32_to_cpu(_ctx->cqe->rss_hash_result);
}

to this:

u32 mlx5e_xdp_rx_hash(const struct xdp_md *ctx)

Quoted 8 lines
{
	const struct mlx5_xdp_buff *_ctx = (void *)ctx;

	if (!(_ctx->xdp.rxq->dev->features & NETIF_F_RXHASH))
                return 0;

	return be32_to_cpu(_ctx->cqe->rss_hash_result);
}

Are there no corner cases? E.g. in case of an L2 frame you'd then expect a hash of 0? Rather than no hash?

If I understand we went for the _supported() thing to make inlining the check easier than inlining the actual read of the field. But we're told inlining is a bit of a wait.. so isn't the motivation for the _supported() pretty much gone? And we should we go back to returning an error from the actual read?

Is partial inlining hard? (inline just the check and generate a full call for the read, ending up with the same code as with _supported())

#55 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 37 lines · Jakub Kicinski
Quoted 26 lines · On Fri, 09 Dec 2022 15:42:37 +0100 Toke Høiland-Jørgensen wrote:

If we expect the program to do out of band probing, we could just get rid of the _supported() functions entirely?

I mean, to me, the whole point of having the separate _supported() function for each item was to have a lower-overhead way of checking if the metadata item was supported. But if the overhead is not actually lower (because both incur a function call), why have them at all? Then we could just change the implementation from this:

bool mlx5e_xdp_rx_hash_supported(const struct xdp_md *ctx)

{
      const struct mlx5_xdp_buff *_ctx = (void *)ctx;

      return _ctx->xdp.rxq->dev->features & NETIF_F_RXHASH;
}

u32 mlx5e_xdp_rx_hash(const struct xdp_md *ctx)

{
      const struct mlx5_xdp_buff *_ctx = (void *)ctx;

      return be32_to_cpu(_ctx->cqe->rss_hash_result);
}

to this:

u32 mlx5e_xdp_rx_hash(const struct xdp_md *ctx)

Quoted 8 lines
{
      const struct mlx5_xdp_buff *_ctx = (void *)ctx;

      if (!(_ctx->xdp.rxq->dev->features & NETIF_F_RXHASH))
                return 0;

      return be32_to_cpu(_ctx->cqe->rss_hash_result);
}
Quoted 8 lines

Are there no corner cases? E.g. in case of an L2 frame you'd then expect a hash of 0? Rather than no hash?

If I understand we went for the _supported() thing to make inlining the check easier than inlining the actual read of the field. But we're told inlining is a bit of a wait.. so isn't the motivation for the _supported() pretty much gone? And we should we go back to returning an error from the actual read?

Seems fair, we can always bring those _supported() calls back in the future when the inlining is available and having those separate calls shows clear benefit.
Then let's go back to a more conventional form below?

int mlx5e_xdp_rx_hash(const struct xdp_md *ctx, u32 *timestamp)

{
      const struct mlx5_xdp_buff *_ctx = (void *)ctx;

       if (!(_ctx->xdp.rxq->dev->features & NETIF_F_RXHASH))
                 return -EOPNOTSUPP;

       *timestamp = be32_to_cpu(_ctx->cqe->rss_hash_result);
       return 0;

}

Quoted 2 lines

Is partial inlining hard? (inline just the check and generate a full call for the read, ending up with the same code as with _supported())

I'm assuming you're suggesting to do this partial inlining manually (as in, writing the code to output this bytecode)?
This probably also falls into the "manual bpf asm generation tech debt" bucket? LMK if I missed your point.

maintainer
#56 lore raw

Re: [xdp-hints] Re: [PATCH bpf-next v3 11/12] mlx5: Support RX XDP metadata

Quoted 7 lines · On Fri, 9 Dec 2022 09:46:20 -0800 Stanislav Fomichev wrote:
Quoted 2 lines

Is partial inlining hard? (inline just the check and generate a full call for the read, ending up with the same code as with _supported())

I'm assuming you're suggesting to do this partial inlining manually (as in, writing the code to output this bytecode)?
This probably also falls into the "manual bpf asm generation tech debt" bucket? LMK if I missed your point.

Maybe just ignore that, I'm not sure how the unrolling of the _supported() calls was expected to work in the first place.

#57 lore raw

PATCH v3 12/12 selftests/bpf: Simple program to dump XDP RX metadata

In reply to Stanislav Fomichev

To be used for verification of driver implementations. Note that the skb path is gone from the series, but I'm still keeping the implementation for any possible future work.

$ xdp_hw_metadata <ifname>

On the other machine:

$ echo -n xdp | nc -u -q1 <target> 9091 # for AF_XDP
$ echo -n skb | nc -u -q1 <target> 9092 # for skb

Sample output:

  # xdp
  xsk_ring_cons__peek: 1
  0x19f9090: rx_desc[0]->addr=100000000008000 addr=8100 comp_addr=8000
  rx_timestamp_supported: 1
  rx_timestamp: 1667850075063948829
  0x19f9090: complete idx=8 addr=8000
  # skb
  found skb hwtstamp = 1668314052.854274681

Decoding:

  # xdp
  rx_timestamp=1667850075.063948829
  $ date -d @1667850075
  Mon Nov  7 11:41:15 AM PST 2022
  $ date
  Mon Nov  7 11:42:05 AM PST 2022
  # skb
  $ date -d @1668314052
  Sat Nov 12 08:34:12 PM PST 2022
  $ date
  Sat Nov 12 08:37:06 PM PST 2022
Cc John Fastabend <[email protected]>
Cc David Ahern <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Willem de Bruijn <[email protected]>
Cc Jesper Dangaard Brouer <[email protected]>
Cc Anatoly Burakov <[email protected]>
Cc Alexander Lobakin <[email protected]>
Cc Magnus Karlsson <[email protected]>
Cc Maryam Tahhan <[email protected]>
---
 tools/testing/selftests/bpf/.gitignore        |   1 +
 tools/testing/selftests/bpf/Makefile          |   6 +-
 .../selftests/bpf/progs/xdp_hw_metadata.c     |  93 ++++
 tools/testing/selftests/bpf/xdp_hw_metadata.c | 405 ++++++++++++++++++
 4 files changed, 504 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/progs/xdp_hw_metadata.c
 create mode 100644 tools/testing/selftests/bpf/xdp_hw_metadata.c
tools/testing/selftests/bpf/.gitignore +1 -0
index 07d2d0a8c5cb..01e3baeefd4f 100644
@@ -46,3 +46,4 @@ test_cpp
 xskxceiver
 xdp_redirect_multi
 xdp_synproxy
+xdp_hw_metadata
tools/testing/selftests/bpf/Makefile +5 -1
index 4eed22fa3681..189b39b0e5d0 100644
@@ -83,7 +83,7 @@ TEST_PROGS_EXTENDED := with_addr.sh \
 TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
 	flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
 	test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
-	xskxceiver xdp_redirect_multi xdp_synproxy veristat
+	xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata
 
 TEST_CUSTOM_PROGS = $(OUTPUT)/urandom_read $(OUTPUT)/sign-file
 TEST_GEN_FILES += liburandom_read.so
@@ -241,6 +241,9 @@ $(OUTPUT)/test_maps: $(TESTING_HELPERS)
 $(OUTPUT)/test_verifier: $(TESTING_HELPERS) $(CAP_HELPERS)
 $(OUTPUT)/xsk.o: $(BPFOBJ)
 $(OUTPUT)/xskxceiver: $(OUTPUT)/xsk.o
+$(OUTPUT)/xdp_hw_metadata: $(OUTPUT)/xsk.o $(OUTPUT)/xdp_hw_metadata.skel.h
+$(OUTPUT)/xdp_hw_metadata: $(OUTPUT)/network_helpers.o
+$(OUTPUT)/xdp_hw_metadata: LDFLAGS += -static
 
 BPFTOOL ?= $(DEFAULT_BPFTOOL)
 $(DEFAULT_BPFTOOL): $(wildcard $(BPFTOOLDIR)/*.[ch] $(BPFTOOLDIR)/Makefile)    \
@@ -383,6 +386,7 @@ linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o
 test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o
 test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o
 test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o
+xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o
 
 LINKED_BPF_SRCS := $(patsubst %.bpf.o,%.c,$(foreach skel,$(LINKED_SKELS),$($(skel)-deps)))
 
tools/testing/selftests/bpf/progs/xdp_hw_metadata.c new file +93 -0

93 lines not on this page · raw message

tools/testing/selftests/bpf/xdp_hw_metadata.c new file +405 -0

405 lines not on this page · raw message

Signature · 1 line
2.39.0.rc0.267.gcb52ba06e7-goog
#58 lore raw
In reply to Stanislav Fomichev
Quoted 13 lines · Stanislav Fomichev

Please see the first patch in the series for the overall design and use-cases.

Changes since v3:

- Rework prog->bound_netdev refcounting (Jakub/Marin)

Now it's based on the offload.c framework. It mostly fits, except I had to automatically insert a HT entry for the netdev. In the offloaded case, the netdev is added via a call to bpf_offload_dev_netdev_register from the driver init path; with a dev-bound programs, we have to manually add (and remove) the entry.

Quoted 4 lines

As suggested by Toke, I'm also prohibiting putting dev-bound programs into prog-array map; essentially prohibiting tail calling into it. I'm also disabling freplace of the dev-bound programs. Both of those restrictions can be loosened up eventually.

I thought it would be a shame that we don't support at least freplace programs from the get-go (as that would exclude libxdp from taking advantage of this). So see below for a patch implementing this :)

-Toke

commit 3abb333e5fd2e8a0920b77013499bdae0ee3db43

Author: Toke Høiland-Jørgensen <[email protected]>
Date:   Thu Dec 8 23:10:54 2022 +0100

bpf: Support consuming XDP HW metadata from fext programs

Instead of rejecting the attaching of PROG_TYPE_EXT programs to XDP programs that consume HW metadata, implement support for propagating the offload information. The extension program doesn't need to set a flag or ifindex, it these will just be propagated from the target by the verifier. We need to create a separate offload object for the extension program, though, since it can be reattached to a different program later (which means we can't just inhering the offload information from the target).

An additional check is added on attach that the new target is compatible with the offload information in the extension prog.

include/linux/bpf.h +1 -0
index b46b60f4eae1..cfa5c847cf2c 100644
@@ -2482,6 +2482,7 @@ void *bpf_offload_resolve_kfunc(struct bpf_prog *prog, u32 func_id);
 void unpriv_ebpf_notify(int new_state);
 
 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
+int __bpf_prog_offload_init(struct bpf_prog *prog, struct net_device *netdev);
 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
 void bpf_offload_bound_netdev_unregister(struct net_device *dev);
 
kernel/bpf/offload.c +37 -21

92 lines not on this page · raw message

kernel/bpf/syscall.c +8 -0
index b345a273f7d0..606e6de5f716 100644
@@ -3021,6 +3021,14 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
 			goto out_put_prog;
 		}
 
+		if (bpf_prog_is_dev_bound(tgt_prog->aux) &&
+		    (bpf_prog_is_offloaded(tgt_prog->aux) ||
+		     !bpf_prog_is_dev_bound(prog->aux) ||
+		     !bpf_offload_dev_match(prog, tgt_prog->aux->offload->netdev))) {
+			err = -EINVAL;
+			goto out_put_prog;
+		}
+
 		key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
 	}
 
kernel/bpf/verifier.c +13 -6
index bc8d9b8d4f47..d92e28dd220e 100644
@@ -16379,11 +16379,6 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
 	if (tgt_prog) {
 		struct bpf_prog_aux *aux = tgt_prog->aux;
 
-		if (bpf_prog_is_dev_bound(tgt_prog->aux)) {
-			bpf_log(log, "Replacing device-bound programs not supported\n");
-			return -EINVAL;
-		}
-
 		for (i = 0; i < aux->func_info_cnt; i++)
 			if (aux->func_info[i].type_id == btf_id) {
 				subprog = i;
@@ -16644,10 +16639,22 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
 	if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) {
 		/* to make freplace equivalent to their targets, they need to
 		 * inherit env->ops and expected_attach_type for the rest of the
-		 * verification
+		 * verification; we also need to propagate the prog offload data
+		 * for resolving kfuncs.
 		 */
 		env->ops = bpf_verifier_ops[tgt_prog->type];
 		prog->expected_attach_type = tgt_prog->expected_attach_type;
+
+		if (bpf_prog_is_dev_bound(tgt_prog->aux)) {
+			if (bpf_prog_is_offloaded(tgt_prog->aux))
+				return -EINVAL;
+
+			prog->aux->dev_bound = true;
+			ret = __bpf_prog_offload_init(prog,
+						      tgt_prog->aux->offload->netdev);
+			if (ret)
+				return ret;
+		}
 	}
 
 	/* store info about the attachment target that will be used later */
#59 lore raw
Quoted 21 lines · Toke Høiland-Jørgensen
Quoted 13 lines · Stanislav Fomichev

Please see the first patch in the series for the overall design and use-cases.

Changes since v3:

- Rework prog->bound_netdev refcounting (Jakub/Marin)

Now it's based on the offload.c framework. It mostly fits, except I had to automatically insert a HT entry for the netdev. In the offloaded case, the netdev is added via a call to bpf_offload_dev_netdev_register from the driver init path; with a dev-bound programs, we have to manually add (and remove) the entry.

Quoted 4 lines

As suggested by Toke, I'm also prohibiting putting dev-bound programs into prog-array map; essentially prohibiting tail calling into it. I'm also disabling freplace of the dev-bound programs. Both of those restrictions can be loosened up eventually.

Quoted 5 lines

I thought it would be a shame that we don't support at least freplace programs from the get-go (as that would exclude libxdp from taking advantage of this). So see below for a patch implementing this :)

-Toke

Damn, now I need to write a selftest :-)
But seriously, thank you for taking care of this, will try to include preserving SoB!

Quoted 189 lines

commit 3abb333e5fd2e8a0920b77013499bdae0ee3db43

Author: Toke Høiland-Jørgensen <[email protected]>
Date:   Thu Dec 8 23:10:54 2022 +0100

bpf: Support consuming XDP HW metadata from fext programs

Instead of rejecting the attaching of PROG_TYPE_EXT programs to XDP programs that consume HW metadata, implement support for propagating the offload information. The extension program doesn't need to set a flag or ifindex, it these will just be propagated from the target by the verifier. We need to create a separate offload object for the extension program, though, since it can be reattached to a different program later (which means we can't just inhering the offload information from the target).

An additional check is added on attach that the new target is compatible with the offload information in the extension prog.

include/linux/bpf.h +1 -0
index b46b60f4eae1..cfa5c847cf2c 100644
@@ -2482,6 +2482,7 @@ void *bpf_offload_resolve_kfunc(struct bpf_prog *prog, u32 func_id);
 void unpriv_ebpf_notify(int new_state);
 
 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
+int __bpf_prog_offload_init(struct bpf_prog *prog, struct net_device *netdev);
 int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr);
 void bpf_offload_bound_netdev_unregister(struct net_device *dev);
 
kernel/bpf/offload.c +37 -21
index bad8bab916eb..b059a7b53457 100644
@@ -83,36 +83,25 @@ bpf_offload_find_netdev(struct net_device *netdev)
        return rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params);
 }
 
-int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
+int __bpf_prog_offload_init(struct bpf_prog *prog, struct net_device *netdev)
 {
        struct bpf_offload_netdev *ondev;
        struct bpf_prog_offload *offload;
        int err;
 
-       if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS &&
-           attr->prog_type != BPF_PROG_TYPE_XDP)
+       if (!netdev)
                return -EINVAL;
 
-       if (attr->prog_flags & ~BPF_F_XDP_HAS_METADATA)
-               return -EINVAL;
+       err = __bpf_offload_init();
+       if (err)
+               return err;
 
        offload = kzalloc(sizeof(*offload), GFP_USER);
        if (!offload)
                return -ENOMEM;
 
-       err = __bpf_offload_init();
-       if (err)
-               return err;
-
        offload->prog = prog;
-
-       offload->netdev = dev_get_by_index(current->nsproxy->net_ns,
-                                          attr->prog_ifindex);
-       err = bpf_dev_offload_check(offload->netdev);
-       if (err)
-               goto err_maybe_put;
-
-       prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_HAS_METADATA);
+       offload->netdev = netdev;
 
        down_write(&bpf_devs_lock);
        ondev = bpf_offload_find_netdev(offload->netdev);
@@ -135,19 +124,46 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
        offload->offdev = ondev->offdev;
        prog->aux->offload = offload;
        list_add_tail(&offload->offloads, &ondev->progs);
-       dev_put(offload->netdev);
        up_write(&bpf_devs_lock);
 
        return 0;
 err_unlock:
        up_write(&bpf_devs_lock);
-err_maybe_put:
-       if (offload->netdev)
-               dev_put(offload->netdev);
        kfree(offload);
        return err;
 }
 
+int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
+{
+       struct net_device *netdev;
+       int err;
+
+       if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS &&
+           attr->prog_type != BPF_PROG_TYPE_XDP)
+               return -EINVAL;
+
+       if (attr->prog_flags & ~BPF_F_XDP_HAS_METADATA)
+               return -EINVAL;
+
+       netdev = dev_get_by_index(current->nsproxy->net_ns, attr->prog_ifindex);
+       if (!netdev)
+               return -EINVAL;
+
+       err = bpf_dev_offload_check(netdev);
+       if (err)
+               goto out;
+
+       prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_HAS_METADATA);
+
+       err = __bpf_prog_offload_init(prog, netdev);
+       if (err)
+               goto out;
+
+out:
+       dev_put(netdev);
+       return err;
+}
+
 int bpf_prog_offload_verifier_prep(struct bpf_prog *prog)
 {
        struct bpf_prog_offload *offload;
kernel/bpf/syscall.c +8 -0
index b345a273f7d0..606e6de5f716 100644
@@ -3021,6 +3021,14 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
                        goto out_put_prog;
                }
 
+               if (bpf_prog_is_dev_bound(tgt_prog->aux) &&
+                   (bpf_prog_is_offloaded(tgt_prog->aux) ||
+                    !bpf_prog_is_dev_bound(prog->aux) ||
+                    !bpf_offload_dev_match(prog, tgt_prog->aux->offload->netdev))) {
+                       err = -EINVAL;
+                       goto out_put_prog;
+               }
+
                key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
        }
 
kernel/bpf/verifier.c +13 -6
index bc8d9b8d4f47..d92e28dd220e 100644
@@ -16379,11 +16379,6 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,
        if (tgt_prog) {
                struct bpf_prog_aux *aux = tgt_prog->aux;
 
-               if (bpf_prog_is_dev_bound(tgt_prog->aux)) {
-                       bpf_log(log, "Replacing device-bound programs not supported\n");
-                       return -EINVAL;
-               }
-
                for (i = 0; i < aux->func_info_cnt; i++)
                        if (aux->func_info[i].type_id == btf_id) {
                                subprog = i;
@@ -16644,10 +16639,22 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
        if (tgt_prog && prog->type == BPF_PROG_TYPE_EXT) {
                /* to make freplace equivalent to their targets, they need to
                 * inherit env->ops and expected_attach_type for the rest of the
-                * verification
+                * verification; we also need to propagate the prog offload data
+                * for resolving kfuncs.
                 */
                env->ops = bpf_verifier_ops[tgt_prog->type];
                prog->expected_attach_type = tgt_prog->expected_attach_type;
+
+               if (bpf_prog_is_dev_bound(tgt_prog->aux)) {
+                       if (bpf_prog_is_offloaded(tgt_prog->aux))
+                               return -EINVAL;
+
+                       prog->aux->dev_bound = true;
+                       ret = __bpf_prog_offload_init(prog,
+                                                     tgt_prog->aux->offload->netdev);
+                       if (ret)
+                               return ret;
+               }
        }
 
        /* store info about the attachment target that will be used later */
#60 lore raw
Quoted 26 lines · Stanislav Fomichev
Quoted 25 lines · Toke Høiland-Jørgensen
Quoted 13 lines · Stanislav Fomichev

Please see the first patch in the series for the overall design and use-cases.

Changes since v3:

- Rework prog->bound_netdev refcounting (Jakub/Marin)

Now it's based on the offload.c framework. It mostly fits, except I had to automatically insert a HT entry for the netdev. In the offloaded case, the netdev is added via a call to bpf_offload_dev_netdev_register from the driver init path; with a dev-bound programs, we have to manually add (and remove) the entry.

Quoted 4 lines

As suggested by Toke, I'm also prohibiting putting dev-bound programs into prog-array map; essentially prohibiting tail calling into it. I'm also disabling freplace of the dev-bound programs. Both of those restrictions can be loosened up eventually.

I thought it would be a shame that we don't support at least freplace programs from the get-go (as that would exclude libxdp from taking advantage of this). So see below for a patch implementing this :)

Quoted 5 lines
Quoted 1 line

-Toke

Damn, now I need to write a selftest :-)
But seriously, thank you for taking care of this, will try to include preserving SoB!

Cool, thanks! I just realised I made on mistake in the attach check, though:

[...]

Quoted 8 lines
Quoted 8 lines
kernel/bpf/syscall.c +0 -0
index b345a273f7d0..606e6de5f716 100644
@@ -3021,6 +3021,14 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog,
                        goto out_put_prog;
                }
 
Quoted 4 lines

kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3027

Quoted 4 lines
kernel/bpf/syscall.c +4 -0
+               if (bpf_prog_is_dev_bound(tgt_prog->aux) &&
+                   (bpf_prog_is_offloaded(tgt_prog->aux) ||
+                    !bpf_prog_is_dev_bound(prog->aux) ||
+                    !bpf_offload_dev_match(prog, tgt_prog->aux->offload->netdev))) {

This should switch the order of the is_dev_bound() checks, like:

+ if (bpf_prog_is_dev_bound(prog->aux) &&
+ (bpf_prog_is_offloaded(tgt_prog->aux) ||
+ !bpf_prog_is_dev_bound(tgt_prog->aux) ||
+ !bpf_offload_dev_match(prog, tgt_prog->aux->offload->netdev))) {

I.e., first check bpf_prog_is_dev_bound(prog->aux) (the program being attached), and only perform the other checks if we're attaching something that has been verified as being dev-bound. It should be fine to attach a non-devbound function to a devbound parent program (since that non-devbound function can't call any of the kfuncs).

-Toke