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=-9.9 required=3.0 tests=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,URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham 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 677F9C10F14 for ; Thu, 10 Oct 2019 18:18:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 44BAE21835 for ; Thu, 10 Oct 2019 18:18:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="ZSsu3ZMD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726898AbfJJSSA (ORCPT ); Thu, 10 Oct 2019 14:18:00 -0400 Received: from mail-lf1-f65.google.com ([209.85.167.65]:40467 "EHLO mail-lf1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726387AbfJJSR7 (ORCPT ); Thu, 10 Oct 2019 14:17:59 -0400 Received: by mail-lf1-f65.google.com with SMTP id d17so5118660lfa.7 for ; Thu, 10 Oct 2019 11:17:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Nrq6vT8ThDufF9K+7nL9VGNPc9YVjdR7P2owKQ14lDc=; b=ZSsu3ZMDge+NQSMNf/iO+b6NBaR8YGme1jtY4y2/SkDlQlnPHAD8tnYpGcEF7b6WNK 69UKX/SW0Dgov97vyXIeNPto11XLnE1jPGuOZhVo1z34ULNtjvQC2VkwlD7J0ifua6Vm o7GY7Lb7vbvrkUcfkxr+wrmCgANXTEiGKZ2yM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Nrq6vT8ThDufF9K+7nL9VGNPc9YVjdR7P2owKQ14lDc=; b=ApG/3QBXn+1uRedTUKVEOAmVC24ERVJ4K22Itoyv0oVHgK7jF3lXCOqNSvNsWTxyW+ qE2rtiyinRm+Mi23I/D7D3CMaxGSg+yTvIvTIs6QIFXzD5Xxy1iZhCoJ36y/reiPJ+ee I7eFhEUefr6yM5F2SgM4nIvK3h6JpzIyygi0STio4nnwuUVlmG5OLuLft5ea5ZgpNtb9 bKWO+jGfNgLP11bQ8qvqGJu/Pvy0vKqlUvyqyV/SO2/aI4UOiajt1wNVvRqR7NMZmgJ8 J9WiLOJW/80mJXXp7MOIICsFKeaydbRdi+NlKfxyqOTJKSP2yqpXkhaZdtfB/fpZ1rN+ sjZQ== X-Gm-Message-State: APjAAAVeSFBt+tQxhc6KG4HcsdkdH6R80jN6oSf1e4u1FEnpspvzgN6t kqWkBgtE681QnDrAv749DIxtKSw9HV0f6A== X-Google-Smtp-Source: APXvYqy4rEitWeumpON4tOgNsGPYfYNanxwQ41r37eaZ7BvBo8EhIm9zIS/eiY1a5a1gSHDZEMiYPw== X-Received: by 2002:a19:c188:: with SMTP id r130mr6895044lff.41.1570731476764; Thu, 10 Oct 2019 11:17:56 -0700 (PDT) Received: from cloudflare.com ([2a02:a310:c262:aa00:b35e:8938:2c2a:ba8b]) by smtp.gmail.com with ESMTPSA id s1sm1446556lfd.14.2019.10.10.11.17.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Oct 2019 11:17:56 -0700 (PDT) From: Jakub Sitnicki To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, kernel-team@cloudflare.com, Stanislav Fomichev Subject: [PATCH bpf-next v2 2/2] selftests/bpf: Check that flow dissector can be re-attached Date: Thu, 10 Oct 2019 20:17:50 +0200 Message-Id: <20191010181750.5964-3-jakub@cloudflare.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191010181750.5964-1-jakub@cloudflare.com> References: <20191010181750.5964-1-jakub@cloudflare.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: bpf-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Make sure a new flow dissector program can be attached to replace the old one with a single syscall. Also check that attaching the same program twice is prohibited. Signed-off-by: Jakub Sitnicki --- .../bpf/prog_tests/flow_dissector_reattach.c | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c diff --git a/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c b/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c new file mode 100644 index 000000000000..fa502720825c --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/flow_dissector_reattach.c @@ -0,1 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test that the flow_dissector program can be updated with a single + * syscall by attaching a new program that replaces the existing one. + * + * Corner case - the same program cannot be attached twice. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include +#include + +#include "test_progs.h" + +static bool is_attached(int netns) +{ + __u32 cnt; + int err; + + err = bpf_prog_query(netns, BPF_FLOW_DISSECTOR, 0, NULL, NULL, &cnt); + if (CHECK_FAIL(err)) { + perror("bpf_prog_query"); + return true; /* fail-safe */ + } + + return cnt > 0; +} + +static int load_prog(void) +{ + struct bpf_insn prog[] = { + BPF_MOV64_IMM(BPF_REG_0, BPF_OK), + BPF_EXIT_INSN(), + }; + int fd; + + fd = bpf_load_program(BPF_PROG_TYPE_FLOW_DISSECTOR, prog, + ARRAY_SIZE(prog), "GPL", 0, NULL, 0); + if (CHECK_FAIL(fd < 0)) + perror("bpf_load_program"); + + return fd; +} + +static void do_flow_dissector_reattach(void) +{ + int prog_fd[2] = { -1, -1 }; + int err; + + prog_fd[0] = load_prog(); + if (prog_fd[0] < 0) + return; + + prog_fd[1] = load_prog(); + if (prog_fd[1] < 0) + goto out_close; + + err = bpf_prog_attach(prog_fd[0], 0, BPF_FLOW_DISSECTOR, 0); + if (CHECK_FAIL(err)) { + perror("bpf_prog_attach-0"); + goto out_close; + } + + /* Expect success when attaching a different program */ + err = bpf_prog_attach(prog_fd[1], 0, BPF_FLOW_DISSECTOR, 0); + if (CHECK_FAIL(err)) { + perror("bpf_prog_attach-1"); + goto out_detach; + } + + /* Expect failure when attaching the same program twice */ + err = bpf_prog_attach(prog_fd[1], 0, BPF_FLOW_DISSECTOR, 0); + if (CHECK_FAIL(!err || errno != EINVAL)) + perror("bpf_prog_attach-2"); + +out_detach: + err = bpf_prog_detach(0, BPF_FLOW_DISSECTOR); + if (CHECK_FAIL(err)) + perror("bpf_prog_detach"); + +out_close: + close(prog_fd[1]); + close(prog_fd[0]); +} + +void test_flow_dissector_reattach(void) +{ + int init_net, err; + + init_net = open("/proc/1/ns/net", O_RDONLY); + if (CHECK_FAIL(init_net < 0)) { + perror("open(/proc/1/ns/net)"); + return; + } + + err = setns(init_net, CLONE_NEWNET); + if (CHECK_FAIL(err)) { + perror("setns(/proc/1/ns/net)"); + goto out_close; + } + + if (is_attached(init_net)) { + test__skip(); + printf("Can't test with flow dissector attached to init_net\n"); + return; + } + + /* First run tests in root network namespace */ + do_flow_dissector_reattach(); + + /* Then repeat tests in a non-root namespace */ + err = unshare(CLONE_NEWNET); + if (CHECK_FAIL(err)) { + perror("unshare(CLONE_NEWNET)"); + goto out_close; + } + do_flow_dissector_reattach(); + +out_close: + close(init_net); +} -- 2.20.1