From patchwork Mon Dec 19 15:41:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Bianconi X-Patchwork-Id: 13076729 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 3706BC4332F for ; Mon, 19 Dec 2022 15:43:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232491AbiLSPn1 (ORCPT ); Mon, 19 Dec 2022 10:43:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232421AbiLSPmu (ORCPT ); Mon, 19 Dec 2022 10:42:50 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A058D12609; Mon, 19 Dec 2022 07:42:49 -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 ams.source.kernel.org (Postfix) with ESMTPS id 51F55B80EAA; Mon, 19 Dec 2022 15:42:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7428AC433D2; Mon, 19 Dec 2022 15:42:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671464567; bh=y6oFJlBwi66+CEvrDKGUOKWKRpfOH8XKz8B1jxaGaqY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lmmZLsTdqfy3in8F57kJGpkgb6fKCWhl1Gm5TJd/UWO75WV1nESrT7kYmfy4ifIjz JNAVcKolh4Nf/xSRyNtpwWRh4umfkRZYP17Edy0ryOfs5jMN9n8gLyTQZ5tJQThaab 6AankrXmqLI7pj9Duee4FUwkB8bIR5FaNIY4uV7jkVjpLi7iPuRW2hbJi/7yp7yam4 Ra/SKbduTekM6OkBRUo/+We5r0dbNemFce34BvyyUC36Lpd3hX2O+biQvWm116wHVD okKrQOgFHSl287KylITnq6BdevK2j7l1hAUeppzuBiDnUST8bdY+G0MXfTjIIu2GPs +NoaU0i0D4zPw== 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 7/8] bpf: devmap: check XDP features in bpf_map_update_elem and __xdp_enqueue Date: Mon, 19 Dec 2022 16:41:36 +0100 Message-Id: <9e15c15f2ee134f628fc286e123ec85e40617b35.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 When we update devmap element, the net_device whose ifindex is specified in map value must support ndo_xdp_xmit callback, which is indicated by the presence of XDP_F_REDIRECT_TARGET feature. Let's check for this feature and return an error if device cannot be used as a redirect target. Moreover check the device support xdp non-linear frame in __xdp_enqueue and is_valid_dst routines. This patch allows to perfrom XDP_REDIRECT on non-linear xdp buffers. Co-developed-by: Kumar Kartikeya Dwivedi Signed-off-by: Kumar Kartikeya Dwivedi Signed-off-by: Lorenzo Bianconi --- kernel/bpf/devmap.c | 25 +++++++++++++++++++++---- net/core/filter.c | 13 +++++-------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/kernel/bpf/devmap.c b/kernel/bpf/devmap.c index d01e4c55b376..16199eb5c7a6 100644 --- a/kernel/bpf/devmap.c +++ b/kernel/bpf/devmap.c @@ -474,7 +474,11 @@ static inline int __xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf, { int err; - if (!dev->netdev_ops->ndo_xdp_xmit) + if (!(dev->xdp_features & XDP_F_REDIRECT_TARGET)) + return -EOPNOTSUPP; + + if (unlikely(!(dev->xdp_features & XDP_F_FRAG_TARGET) && + xdp_frame_has_frags(xdpf))) return -EOPNOTSUPP; err = xdp_ok_fwd_dev(dev, xdp_get_frame_len(xdpf)); @@ -532,8 +536,14 @@ int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf, static bool is_valid_dst(struct bpf_dtab_netdev *obj, struct xdp_frame *xdpf) { - if (!obj || - !obj->dev->netdev_ops->ndo_xdp_xmit) + if (!obj) + return false; + + if (!(obj->dev->xdp_features & XDP_F_REDIRECT_TARGET)) + return false; + + if (unlikely(!(obj->dev->xdp_features & XDP_F_FRAG_TARGET) && + xdp_frame_has_frags(xdpf))) return false; if (xdp_ok_fwd_dev(obj->dev, xdp_get_frame_len(xdpf))) @@ -843,6 +853,7 @@ static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net, { struct bpf_prog *prog = NULL; struct bpf_dtab_netdev *dev; + int ret = -EINVAL; dev = bpf_map_kmalloc_node(&dtab->map, sizeof(*dev), GFP_NOWAIT | __GFP_NOWARN, @@ -854,6 +865,12 @@ static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net, if (!dev->dev) goto err_out; + /* Check if net_device can be used as a redirect target */ + if (!(READ_ONCE(dev->dev->xdp_features) & XDP_F_REDIRECT_TARGET)) { + ret = -EOPNOTSUPP; + goto err_put_dev; + } + if (val->bpf_prog.fd > 0) { prog = bpf_prog_get_type_dev(val->bpf_prog.fd, BPF_PROG_TYPE_XDP, false); @@ -882,7 +899,7 @@ static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net, dev_put(dev->dev); err_out: kfree(dev); - return ERR_PTR(-EINVAL); + return ERR_PTR(ret); } static int __dev_map_update_elem(struct net *net, struct bpf_map *map, diff --git a/net/core/filter.c b/net/core/filter.c index 929358677183..c2bd1935b55a 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4282,16 +4282,13 @@ int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp, struct bpf_redirect_info *ri = this_cpu_ptr(&bpf_redirect_info); enum bpf_map_type map_type = ri->map_type; - /* XDP_REDIRECT is not fully supported yet for xdp frags since - * not all XDP capable drivers can map non-linear xdp_frame in - * ndo_xdp_xmit. - */ - if (unlikely(xdp_buff_has_frags(xdp) && - map_type != BPF_MAP_TYPE_CPUMAP)) - return -EOPNOTSUPP; + if (map_type == BPF_MAP_TYPE_XSKMAP) { + /* XDP_REDIRECT is not supported AF_XDP yet. */ + if (unlikely(xdp_buff_has_frags(xdp))) + return -EOPNOTSUPP; - if (map_type == BPF_MAP_TYPE_XSKMAP) return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog); + } return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp), xdp_prog);