Skip to content

Commit d5e726d

Browse files
Stanislav Fomichevborkmann
authored andcommitted
xsk: Require XDP_UMEM_TX_METADATA_LEN to actuate tx_metadata_len
Julian reports that commit 341ac98 ("xsk: Support tx_metadata_len") can break existing use cases which don't zero-initialize xdp_umem_reg padding. Introduce new XDP_UMEM_TX_METADATA_LEN to make sure we interpret the padding as tx_metadata_len only when being explicitly asked. Fixes: 341ac98 ("xsk: Support tx_metadata_len") Reported-by: Julian Schindel <[email protected]> Signed-off-by: Stanislav Fomichev <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Maciej Fijalkowski <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent fa5ef65 commit d5e726d

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

Documentation/networking/xsk-tx-metadata.rst

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ metadata on the receive side.
1111
General Design
1212
==============
1313

14-
The headroom for the metadata is reserved via ``tx_metadata_len`` in
15-
``struct xdp_umem_reg``. The metadata length is therefore the same for
16-
every socket that shares the same umem. The metadata layout is a fixed UAPI,
17-
refer to ``union xsk_tx_metadata`` in ``include/uapi/linux/if_xdp.h``.
18-
Thus, generally, the ``tx_metadata_len`` field above should contain
19-
``sizeof(union xsk_tx_metadata)``.
14+
The headroom for the metadata is reserved via ``tx_metadata_len`` and
15+
``XDP_UMEM_TX_METADATA_LEN`` flag in ``struct xdp_umem_reg``. The metadata
16+
length is therefore the same for every socket that shares the same umem.
17+
The metadata layout is a fixed UAPI, refer to ``union xsk_tx_metadata`` in
18+
``include/uapi/linux/if_xdp.h``. Thus, generally, the ``tx_metadata_len``
19+
field above should contain ``sizeof(union xsk_tx_metadata)``.
20+
21+
Note that in the original implementation the ``XDP_UMEM_TX_METADATA_LEN``
22+
flag was not required. Applications might attempt to create a umem
23+
with a flag first and if it fails, do another attempt without a flag.
2024

2125
The headroom and the metadata itself should be located right before
2226
``xdp_desc->addr`` in the umem frame. Within a frame, the metadata

include/uapi/linux/if_xdp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
*/
4242
#define XDP_UMEM_TX_SW_CSUM (1 << 1)
4343

44+
/* Request to reserve tx_metadata_len bytes of per-chunk metadata.
45+
*/
46+
#define XDP_UMEM_TX_METADATA_LEN (1 << 2)
47+
4448
struct sockaddr_xdp {
4549
__u16 sxdp_family;
4650
__u16 sxdp_flags;

net/xdp/xdp_umem.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ static int xdp_umem_account_pages(struct xdp_umem *umem)
151151
#define XDP_UMEM_FLAGS_VALID ( \
152152
XDP_UMEM_UNALIGNED_CHUNK_FLAG | \
153153
XDP_UMEM_TX_SW_CSUM | \
154+
XDP_UMEM_TX_METADATA_LEN | \
154155
0)
155156

156157
static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
@@ -204,8 +205,11 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
204205
if (headroom >= chunk_size - XDP_PACKET_HEADROOM)
205206
return -EINVAL;
206207

207-
if (mr->tx_metadata_len >= 256 || mr->tx_metadata_len % 8)
208-
return -EINVAL;
208+
if (mr->flags & XDP_UMEM_TX_METADATA_LEN) {
209+
if (mr->tx_metadata_len >= 256 || mr->tx_metadata_len % 8)
210+
return -EINVAL;
211+
umem->tx_metadata_len = mr->tx_metadata_len;
212+
}
209213

210214
umem->size = size;
211215
umem->headroom = headroom;
@@ -215,7 +219,6 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
215219
umem->pgs = NULL;
216220
umem->user = NULL;
217221
umem->flags = mr->flags;
218-
umem->tx_metadata_len = mr->tx_metadata_len;
219222

220223
INIT_LIST_HEAD(&umem->xsk_dma_list);
221224
refcount_set(&umem->users, 1);

0 commit comments

Comments
 (0)