From patchwork Mon May 9 00:51:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: shaozhengchao X-Patchwork-Id: 12842981 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 4A0B8C4332F for ; Mon, 9 May 2022 01:29:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233986AbiEIBau (ORCPT ); Sun, 8 May 2022 21:30:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46650 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235817AbiEIAyF (ORCPT ); Sun, 8 May 2022 20:54:05 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C274E65CA; Sun, 8 May 2022 17:50:12 -0700 (PDT) Received: from dggpeml500026.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4KxMsJ4HqXzXdlb; Mon, 9 May 2022 08:45:24 +0800 (CST) Received: from huawei.com (10.175.101.6) by dggpeml500026.china.huawei.com (7.185.36.106) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Mon, 9 May 2022 08:50:08 +0800 From: Zhengchao Shao To: , , , , , , , , , , , , , CC: , , Subject: [PATCH bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd Date: Mon, 9 May 2022 08:51:05 +0800 Message-ID: <20220509005105.271089-1-shaozhengchao@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500026.china.huawei.com (7.185.36.106) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Before detach the prog, we should check detach prog exist or not. Signed-off-by: Zhengchao Shao --- samples/bpf/xdp_fwd_user.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c index 1828487bae9a..a273ede3fd73 100644 --- a/samples/bpf/xdp_fwd_user.c +++ b/samples/bpf/xdp_fwd_user.c @@ -49,7 +49,18 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name) static int do_detach(int idx, const char *name) { - int err; + int err = 1; + __u32 curr_prog_id; + + if (bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id)) { + printf("ERROR: bpf_xdp_query_id failed\n"); + return err; + } + + if (!curr_prog_id) { + printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n", xdp_flags, name); + return err; + } err = bpf_xdp_detach(idx, xdp_flags, NULL); if (err < 0)