From patchwork Mon Dec 19 15:41:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Bianconi X-Patchwork-Id: 13076727 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 414E9C4332F for ; Mon, 19 Dec 2022 15:42:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232388AbiLSPmp (ORCPT ); Mon, 19 Dec 2022 10:42:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232363AbiLSPml (ORCPT ); Mon, 19 Dec 2022 10:42:41 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7C30412083; Mon, 19 Dec 2022 07:42:40 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 17BFD6101A; Mon, 19 Dec 2022 15:42:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0148FC433EF; Mon, 19 Dec 2022 15:42:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671464559; bh=/GvW1CBUtaSQShWzPvNHf0BP6LO6rDP67Uq5eKJRcw8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aD8H3WWbpD36LwAUsVy8GUnEZXJd70+H5AiURKCFAenW8xRZ50lmvGSCQKOWP7Y1R FN34kYUYYVXuiVThLcwt/tAvCTlGewF0XFFxCo+bbae3KOla4+YOulDcvu+ZwE/0d2 VRlGyRamtEugVoWy7gvEPKnuDyFM9x97aN94CNC4B0brWP8I6aHDYirIO6IZZBD80d KgFWZGCLDCo3eazn/q9cUN4ZBczB8iCHxBrfOoSyiDffL5qZcOFRSbPpFmmtXmgo6U Zn1SxS0TIuPVUYpwDmZdtgPx5c3WbbcauXtjV/Xi+TtmUQtNekfy5AX+XQKKfQVGmA n1f3BmhrF0S1w== From: Lorenzo Bianconi To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, davem@davemloft.net, kuba@kernel.org, hawk@kernel.org, pabeni@redhat.com, edumazet@google.com, toke@redhat.com, memxor@gmail.com, alardam@gmail.com, saeedm@nvidia.com, anthony.l.nguyen@intel.com, gospo@broadcom.com, vladimir.oltean@nxp.com, nbd@nbd.name, john@phrozen.org, leon@kernel.org, simon.horman@corigine.com, aelior@marvell.com, christophe.jaillet@wanadoo.fr, ecree.xilinx@gmail.com, grygorii.strashko@ti.com, mst@redhat.com, bjorn@kernel.org, magnus.karlsson@intel.com, maciej.fijalkowski@intel.com, intel-wired-lan@lists.osuosl.org, lorenzo.bianconi@redhat.com Subject: [RFC bpf-next 5/8] xsk: add check for full support of XDP in bind Date: Mon, 19 Dec 2022 16:41:34 +0100 Message-Id: <8522db2faf32eb9e37862cd5bd6ba98bec3fa2d8.1671462951.git.lorenzo@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC From: Marek Majtyka Add check for full support of XDP in AF_XDP socket bind. To be able to use an AF_XDP socket with zero-copy, there needs to be support for both XDP_REDIRECT in the driver (XDP native mode) and the driver needs to support zero-copy. The problem is that there are drivers out there that only support XDP partially, so it is possible to successfully load the XDP program in native mode, but it will still not be able to support zero-copy as it does not have XDP_REDIRECT support. We can now alleviate this problem by using the new XDP netdev capability that signifies if full XDP support is indeed present. This check can be triggered by a new bind flag called XDP_CHECK_NATIVE_MODE. To simplify usage, this check is triggered automatically from inside libbpf library via turning on the new XDP_CHECK_NATIVE_MODE flag if and only if the driver mode is selected for the socket. As a result, the xsk_bind function decides if the native mode for a given interface makes sense or not using xdp netdev feature flags. Eventually the xsk socket is bound or an error is returned. Apart from this change and to catch all invalid inputs in a single place, an additional check is set to forbid skb mode and zero copy settings at the same time as that combination makes no sense. Signed-off-by: Marek Majtyka Signed-off-by: Lorenzo Bianconi --- include/uapi/linux/if_xdp.h | 1 + net/xdp/xsk.c | 4 ++-- net/xdp/xsk_buff_pool.c | 17 ++++++++++++++++- tools/include/uapi/linux/if_xdp.h | 1 + tools/testing/selftests/bpf/xsk.c | 3 +++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/if_xdp.h b/include/uapi/linux/if_xdp.h index a78a8096f4ce..8f47754dacce 100644 --- a/include/uapi/linux/if_xdp.h +++ b/include/uapi/linux/if_xdp.h @@ -25,6 +25,7 @@ * application. */ #define XDP_USE_NEED_WAKEUP (1 << 3) +#define XDP_CHECK_NATIVE_MODE (1 << 4) /* Flags for xsk_umem_config flags */ #define XDP_UMEM_UNALIGNED_CHUNK_FLAG (1 << 0) diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index 9f0561b67c12..76e9a9e99559 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -889,7 +889,7 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) flags = sxdp->sxdp_flags; if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY | - XDP_USE_NEED_WAKEUP)) + XDP_USE_NEED_WAKEUP | XDP_CHECK_NATIVE_MODE)) return -EINVAL; rtnl_lock(); @@ -917,7 +917,7 @@ static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) struct socket *sock; if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) || - (flags & XDP_USE_NEED_WAKEUP)) { + (flags & XDP_USE_NEED_WAKEUP) || (flags & XDP_CHECK_NATIVE_MODE)) { /* Cannot specify flags for shared sockets. */ err = -EINVAL; goto out_unlock; diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c index 7afd12dd69cc..02f569893c02 100644 --- a/net/xdp/xsk_buff_pool.c +++ b/net/xdp/xsk_buff_pool.c @@ -143,7 +143,7 @@ static void xp_disable_drv_zc(struct xsk_buff_pool *pool) int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *netdev, u16 queue_id, u16 flags) { - bool force_zc, force_copy; + bool force_zc, force_copy, force_check; struct netdev_bpf bpf; int err = 0; @@ -151,10 +151,24 @@ int xp_assign_dev(struct xsk_buff_pool *pool, force_zc = flags & XDP_ZEROCOPY; force_copy = flags & XDP_COPY; + force_check = flags & XDP_CHECK_NATIVE_MODE; + if (force_zc && force_copy) return -EINVAL; + if (!(flags & XDP_SHARED_UMEM)) { + if (force_check) { + /* forbid driver mode without full XDP support */ + if (!(XDP_F_REDIRECT & netdev->xdp_features)) + return -EOPNOTSUPP; + } else { + /* forbid skb mode and zero copy */ + if (force_zc) + return -EINVAL; + } + } + if (xsk_get_pool_from_qid(netdev, queue_id)) return -EBUSY; @@ -222,6 +236,7 @@ int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs, return -EINVAL; flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY; + flags |= XDP_SHARED_UMEM; if (umem_xs->pool->uses_need_wakeup) flags |= XDP_USE_NEED_WAKEUP; diff --git a/tools/include/uapi/linux/if_xdp.h b/tools/include/uapi/linux/if_xdp.h index a78a8096f4ce..8f47754dacce 100644 --- a/tools/include/uapi/linux/if_xdp.h +++ b/tools/include/uapi/linux/if_xdp.h @@ -25,6 +25,7 @@ * application. */ #define XDP_USE_NEED_WAKEUP (1 << 3) +#define XDP_CHECK_NATIVE_MODE (1 << 4) /* Flags for xsk_umem_config flags */ #define XDP_UMEM_UNALIGNED_CHUNK_FLAG (1 << 0) diff --git a/tools/testing/selftests/bpf/xsk.c b/tools/testing/selftests/bpf/xsk.c index 39d349509ba4..d6b9349000d2 100644 --- a/tools/testing/selftests/bpf/xsk.c +++ b/tools/testing/selftests/bpf/xsk.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -1130,6 +1131,8 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, sxdp.sxdp_shared_umem_fd = umem->fd; } else { sxdp.sxdp_flags = xsk->config.bind_flags; + if (xsk->config.xdp_flags & XDP_FLAGS_DRV_MODE) + sxdp.sxdp_flags |= XDP_CHECK_NATIVE_MODE; } err = bind(xsk->fd, (struct sockaddr *)&sxdp, sizeof(sxdp));