Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2869337pxk; Mon, 28 Sep 2020 02:10:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwRTQtlDCn/h0t4yty6yYGCd+XkwRX0JurOaw5ytesnv6l1RCwl8duBFGaAhJc50MaaTMEl X-Received: by 2002:aa7:c256:: with SMTP id y22mr617766edo.16.1601284213389; Mon, 28 Sep 2020 02:10:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601284213; cv=none; d=google.com; s=arc-20160816; b=i1wrsWgC2lpKRm7bmG7BnfJtqj9C+4KxdD0FWJVAtzmMJkUwZvt8Av0BGIdnNn5DkP m+lck/ndPsPOguwbqY/Fsr8JlsvfamGxmmaTs9QhEwJ/VI6ZCpx45ReC6x5ZWo7FP6nm niTbEjPlkaKK+SxL6YZH+Umky5RVeADeZ82qiWs4fwdY0UJ+ABXplsi05ZMt4hxckRId 9RMykngZRvlVOeg5hxLG+xGykugOPAsr9dmfSMm31ZxrvmIZXfHM7l1yNuhYaejR4zDT 8F0mFBEDlsOBtFYPYbUWrM3/QgFse5A1Cg6gAZPcIPXy60WqQC0Yn+pK+OJEOntMb1ch b7iQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7RJH7pZMCVV5iroyiFLpiFRNTiQn0SSX+69PgDO+op4=; b=O1xRZ563+NZeui1syR/r0061D/ugTjsdxDeq5HkRxKj+6lWjBkxs7kTNLoT1I1UcQD I5j8M6YF0tUgZL9YFbiYspKiAPuzFQURRtUNdQ14LeYqA7uBiwvj5vS5uInWl9jYaYSt ox8O+gk4SywEs9DzxpxJ5dO/CUWl6pTcFpXc4tWemXkLZ/dV+WvaLBpEothAkv1Lmsmf ZLYnEtSPeS+QO3eY7gocAhRkTKopxtpnbwPpA4r5ModDtW9OhFHhvvGr0+XWhZsd2YMZ dffFFQ2Ci1o8Ke2Dpc5F9RKgUe7KtQSnjCCoiVHyBSJyfnLATgX1EJ4y3u5Qiga+1Nqo 5g4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=AEutUWEm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n23si189197edw.337.2020.09.28.02.09.50; Mon, 28 Sep 2020 02:10:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=AEutUWEm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726812AbgI1JIs (ORCPT + 99 others); Mon, 28 Sep 2020 05:08:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726798AbgI1JIr (ORCPT ); Mon, 28 Sep 2020 05:08:47 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E33CC0613CF for ; Mon, 28 Sep 2020 02:08:46 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id s12so333547wrw.11 for ; Mon, 28 Sep 2020 02:08:46 -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=7RJH7pZMCVV5iroyiFLpiFRNTiQn0SSX+69PgDO+op4=; b=AEutUWEmIeCuK/u0tEo45iEG6yEV0wkOBtVH6ukqPTAnsGzMjPzkYDLn3q3zMwP0UC 5iN2FUSdU+/ROYZro9QQyxMBGKCxwcL24Yp08ZmEeqCID+FtIDhTLeRARkrWeZrPV+o1 A0inX15FwgHvN2xLp8OtH7fgexKh7qcylxJYM= 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=7RJH7pZMCVV5iroyiFLpiFRNTiQn0SSX+69PgDO+op4=; b=cqgpKO9KGDpAvXLkyyhaDiszkVBe3ex6TctAQo8gCB6Cannsjl2udfTA5mHe4TV48d a5xcnQ4q0rcrGY3CKWY+tTcCg0BSPMi+nYsXnvzBMn0I+reALo9tpr18O3Mp/RpdTM4m rfmu024O5umK+KS9hGyLFXrU0bRij+41izPM2i2rLy3CL08iyJp/ZjvxLjU/hC6c64HM VPa3AJSALOq+2P3DlTW/l+N4G3Ig6K7dkzHd4kfy8l5aKw7Mnh9Ogq/K4CqAtpTOOpZg qF9ium4Dyce3g5Hw8be4P/b+4abqf9OK8Lx3oPq23U7Xzk/ObhEnVkoRO9I/xNYa4SJG fsew== X-Gm-Message-State: AOAM533LHpA2T/Q2RFBzKzO4LPqokLznqKULHgSOgicT7amjpf68QeYA RnyOaq3UbjzNZ3y4GRp12YNonQ== X-Received: by 2002:adf:e8c3:: with SMTP id k3mr576807wrn.228.1601284125220; Mon, 28 Sep 2020 02:08:45 -0700 (PDT) Received: from antares.lan (1.f.1.6.a.e.6.5.a.0.3.2.4.7.4.0.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:474:230a:56ea:61f1]) by smtp.gmail.com with ESMTPSA id u13sm479631wrm.77.2020.09.28.02.08.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 02:08:44 -0700 (PDT) From: Lorenz Bauer To: kafai@fb.com, Shuah Khan , Alexei Starovoitov , Daniel Borkmann Cc: kernel-team@cloudflare.com, Lorenz Bauer , linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v2 4/4] selftest: bpf: Test copying a sockmap and sockhash Date: Mon, 28 Sep 2020 10:08:05 +0100 Message-Id: <20200928090805.23343-5-lmb@cloudflare.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200928090805.23343-1-lmb@cloudflare.com> References: <20200928090805.23343-1-lmb@cloudflare.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Since we can now call map_update_elem(sockmap) from bpf_iter context it's possible to copy a sockmap or sockhash in the kernel. Add a selftest which exercises this. Signed-off-by: Lorenz Bauer --- .../selftests/bpf/prog_tests/sockmap_basic.c | 14 +++++----- .../selftests/bpf/progs/bpf_iter_sockmap.c | 27 +++++++++++++++---- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c index e8a4bfb4d9f4..854a508e81ce 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c @@ -194,7 +194,7 @@ static void test_sockmap_invalid_update(void) test_sockmap_invalid_update__destroy(skel); } -static void test_sockmap_iter(enum bpf_map_type map_type) +static void test_sockmap_copy(enum bpf_map_type map_type) { DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); int err, len, src_fd, iter_fd, duration = 0; @@ -242,7 +242,7 @@ static void test_sockmap_iter(enum bpf_map_type map_type) linfo.map.map_fd = src_fd; opts.link_info = &linfo; opts.link_info_len = sizeof(linfo); - link = bpf_program__attach_iter(skel->progs.count_elems, &opts); + link = bpf_program__attach_iter(skel->progs.copy, &opts); if (CHECK(IS_ERR(link), "attach_iter", "attach_iter failed\n")) goto out; @@ -265,6 +265,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type) skel->bss->socks, num_sockets)) goto close_iter; + compare_cookies(src, skel->maps.dst); + close_iter: close(iter_fd); free_link: @@ -294,8 +296,8 @@ void test_sockmap_basic(void) test_sockmap_update(BPF_MAP_TYPE_SOCKHASH); if (test__start_subtest("sockmap update in unsafe context")) test_sockmap_invalid_update(); - if (test__start_subtest("sockmap iter")) - test_sockmap_iter(BPF_MAP_TYPE_SOCKMAP); - if (test__start_subtest("sockhash iter")) - test_sockmap_iter(BPF_MAP_TYPE_SOCKHASH); + if (test__start_subtest("sockmap copy")) + test_sockmap_copy(BPF_MAP_TYPE_SOCKMAP); + if (test__start_subtest("sockhash copy")) + test_sockmap_copy(BPF_MAP_TYPE_SOCKHASH); } diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c b/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c index 1af7555f6057..f3af0e30cead 100644 --- a/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c +++ b/tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c @@ -22,21 +22,38 @@ struct { __type(value, __u64); } sockhash SEC(".maps"); +struct { + __uint(type, BPF_MAP_TYPE_SOCKHASH); + __uint(max_entries, 64); + __type(key, __u32); + __type(value, __u64); +} dst SEC(".maps"); + __u32 elems = 0; __u32 socks = 0; SEC("iter/sockmap") -int count_elems(struct bpf_iter__sockmap *ctx) +int copy(struct bpf_iter__sockmap *ctx) { struct sock *sk = ctx->sk; __u32 tmp, *key = ctx->key; int ret; - if (key) - elems++; + if (!key) + return 0; - if (sk) + elems++; + + /* We need a temporary buffer on the stack, since the verifier doesn't + * let us use the pointer from the context as an argument to the helper. + */ + tmp = *key; + + if (sk) { socks++; + return bpf_map_update_elem(&dst, &tmp, sk, 0) != 0; + } - return 0; + ret = bpf_map_delete_elem(&dst, &tmp); + return ret && ret != -ENOENT; } -- 2.25.1