From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F1ED9C433E3 for ; Thu, 16 Jul 2020 04:56:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C5B1020775 for ; Thu, 16 Jul 2020 04:56:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="E/KFSDaw" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726411AbgGPE4O (ORCPT ); Thu, 16 Jul 2020 00:56:14 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:47374 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725913AbgGPE4N (ORCPT ); Thu, 16 Jul 2020 00:56:13 -0400 Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 06G4tRS2002410 for ; Wed, 15 Jul 2020 21:56:12 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=cEK5Z30WJHfTP1J8+dE5xukADAfi32rO7do4e8b/JoE=; b=E/KFSDawbOLhsSN5ZuUWYRDFpZGa7/KPAw2XLTLjO01fTKStI4UTte9jYnZhrYHNCV4T giOp0zoVPiWF2zxSPiz1VuJnhuSeUZy47/b29Pbw2IeDLv+gP2Ud0v4ZG39uBe33NIis ZrM6VO/P+zTQpg8pZMt032NtTDYezVnnWug= Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com with ESMTP id 32a43f3brj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 15 Jul 2020 21:56:12 -0700 Received: from intmgw002.08.frc2.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:11d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Wed, 15 Jul 2020 21:56:12 -0700 Received: by devbig012.ftw2.facebook.com (Postfix, from userid 137359) id 7680A2EC422D; Wed, 15 Jul 2020 21:56:10 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Andrii Nakryiko Smtp-Origin-Hostname: devbig012.ftw2.facebook.com To: , , , , CC: , , Andrii Nakryiko Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH v3 bpf-next 3/9] bpf, xdp: extract commong XDP program attachment logic Date: Wed, 15 Jul 2020 21:55:55 -0700 Message-ID: <20200716045602.3896926-4-andriin@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200716045602.3896926-1-andriin@fb.com> References: <20200716045602.3896926-1-andriin@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-07-15_12:2020-07-15,2020-07-15 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 lowpriorityscore=0 phishscore=0 clxscore=1015 impostorscore=0 mlxlogscore=999 priorityscore=1501 spamscore=0 malwarescore=0 bulkscore=0 adultscore=0 suspectscore=8 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2007160037 X-FB-Internal: deliver Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Further refactor XDP attachment code. dev_change_xdp_fd() is split into t= wo parts: getting bpf_progs from FDs and attachment logic, working with bpf_progs. This makes attachment logic a bit more straightforward and prepares code for bpf_xdp_link inclusion, which will share the common log= ic. Signed-off-by: Andrii Nakryiko --- net/core/dev.c | 158 ++++++++++++++++++++++++++++--------------------- 1 file changed, 89 insertions(+), 69 deletions(-) diff --git a/net/core/dev.c b/net/core/dev.c index 5b57e8002283..6c315af8f588 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -8813,104 +8813,124 @@ static void dev_xdp_uninstall(struct net_device= *dev) } } =20 -/** - * dev_change_xdp_fd - set or clear a bpf program for a device rx path - * @dev: device - * @extack: netlink extended ack - * @fd: new program fd or negative value to clear - * @expected_fd: old program fd that userspace expects to replace or cle= ar - * @flags: xdp-related flags - * - * Set or clear a bpf program for a device - */ -int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *ex= tack, - int fd, int expected_fd, u32 flags) +static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack= *extack, + struct bpf_prog *new_prog, struct bpf_prog *old_prog, + u32 flags) { - const struct net_device_ops *ops =3D dev->netdev_ops; - enum bpf_xdp_mode mode =3D dev_xdp_mode(flags); - bool offload =3D mode =3D=3D XDP_MODE_HW; - u32 prog_id, expected_id =3D 0; - struct bpf_prog *prog; + struct bpf_prog *cur_prog; + enum bpf_xdp_mode mode; bpf_op_t bpf_op; int err; =20 ASSERT_RTNL(); =20 - bpf_op =3D dev_xdp_bpf_op(dev, mode); - if (!bpf_op) { - NL_SET_ERR_MSG(extack, "underlying driver does not support XDP in nati= ve mode"); - return -EOPNOTSUPP; + /* just one XDP mode bit should be set, zero defaults to SKB mode */ + if (hweight32(flags & XDP_FLAGS_MODES) > 1) { + NL_SET_ERR_MSG(extack, "Only one XDP mode flag can be set"); + return -EINVAL; + } + /* old_prog !=3D NULL implies XDP_FLAGS_REPLACE is set */ + if (old_prog && !(flags & XDP_FLAGS_REPLACE)) { + NL_SET_ERR_MSG(extack, "XDP_FLAGS_REPLACE is not specified"); + return -EINVAL; } =20 - prog_id =3D dev_xdp_prog_id(dev, mode); - if (flags & XDP_FLAGS_REPLACE) { - if (expected_fd >=3D 0) { - prog =3D bpf_prog_get_type_dev(expected_fd, - BPF_PROG_TYPE_XDP, - bpf_op =3D=3D ops->ndo_bpf); - if (IS_ERR(prog)) - return PTR_ERR(prog); - expected_id =3D prog->aux->id; - bpf_prog_put(prog); - } - - if (prog_id !=3D expected_id) { - NL_SET_ERR_MSG(extack, "Active program does not match expected"); - return -EEXIST; - } + mode =3D dev_xdp_mode(flags); + cur_prog =3D dev_xdp_prog(dev, mode); + if ((flags & XDP_FLAGS_REPLACE) && cur_prog !=3D old_prog) { + NL_SET_ERR_MSG(extack, "Active program does not match expected"); + return -EEXIST; } - if (fd >=3D 0) { + if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && cur_prog) { + NL_SET_ERR_MSG(extack, "XDP program already attached"); + return -EBUSY; + } + + if (new_prog) { + bool offload =3D mode =3D=3D XDP_MODE_HW; enum bpf_xdp_mode other_mode =3D mode =3D=3D XDP_MODE_SKB ? XDP_MODE_DRV : XDP_MODE_SKB; =20 - if (!offload && dev_xdp_prog_id(dev, other_mode)) { + if (!offload && dev_xdp_prog(dev, other_mode)) { NL_SET_ERR_MSG(extack, "Native and generic XDP can't be active at the= same time"); return -EEXIST; } - - if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) { - NL_SET_ERR_MSG(extack, "XDP program already attached"); - return -EBUSY; - } - - prog =3D bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, - bpf_op =3D=3D ops->ndo_bpf); - if (IS_ERR(prog)) - return PTR_ERR(prog); - - if (!offload && bpf_prog_is_dev_bound(prog->aux)) { + if (!offload && bpf_prog_is_dev_bound(new_prog->aux)) { NL_SET_ERR_MSG(extack, "Using device-bound program without HW_MODE fl= ag is not supported"); - bpf_prog_put(prog); return -EINVAL; } - - if (prog->expected_attach_type =3D=3D BPF_XDP_DEVMAP) { + if (new_prog->expected_attach_type =3D=3D BPF_XDP_DEVMAP) { NL_SET_ERR_MSG(extack, "BPF_XDP_DEVMAP programs can not be attached t= o a device"); - bpf_prog_put(prog); return -EINVAL; } + } =20 - /* prog->aux->id may be 0 for orphaned device-bound progs */ - if (prog->aux->id && prog->aux->id =3D=3D prog_id) { - bpf_prog_put(prog); - return 0; + /* don't call drivers if the effective program didn't change */ + if (new_prog !=3D cur_prog) { + bpf_op =3D dev_xdp_bpf_op(dev, mode); + if (!bpf_op) { + NL_SET_ERR_MSG(extack, "Underlying driver does not support XDP in nat= ive mode"); + return -EOPNOTSUPP; } - } else { - if (!prog_id) - return 0; - prog =3D NULL; - } =20 - err =3D dev_xdp_install(dev, mode, bpf_op, extack, flags, prog); - if (err < 0 && prog) { - bpf_prog_put(prog); - return err; + err =3D dev_xdp_install(dev, mode, bpf_op, extack, flags, new_prog); + if (err) + return err; } - dev_xdp_set_prog(dev, mode, prog); + + dev_xdp_set_prog(dev, mode, new_prog); + if (cur_prog) + bpf_prog_put(cur_prog); =20 return 0; } =20 +/** + * dev_change_xdp_fd - set or clear a bpf program for a device rx path + * @dev: device + * @extack: netlink extended ack + * @fd: new program fd or negative value to clear + * @expected_fd: old program fd that userspace expects to replace or cle= ar + * @flags: xdp-related flags + * + * Set or clear a bpf program for a device + */ +int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *ex= tack, + int fd, int expected_fd, u32 flags) +{ + enum bpf_xdp_mode mode =3D dev_xdp_mode(flags); + struct bpf_prog *new_prog =3D NULL, *old_prog =3D NULL; + int err; + + ASSERT_RTNL(); + + if (fd >=3D 0) { + new_prog =3D bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, + mode !=3D XDP_MODE_SKB); + if (IS_ERR(new_prog)) + return PTR_ERR(new_prog); + } + + if (expected_fd >=3D 0) { + old_prog =3D bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP, + mode !=3D XDP_MODE_SKB); + if (IS_ERR(old_prog)) { + err =3D PTR_ERR(old_prog); + old_prog =3D NULL; + goto err_out; + } + } + + err =3D dev_xdp_attach(dev, extack, new_prog, old_prog, flags); + +err_out: + if (err && new_prog) + bpf_prog_put(new_prog); + if (old_prog) + bpf_prog_put(old_prog); + return err; +} + /** * dev_new_index - allocate an ifindex * @net: the applicable net namespace --=20 2.24.1