Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp888039pxb; Thu, 19 Nov 2020 16:55:13 -0800 (PST) X-Google-Smtp-Source: ABdhPJwq0XOwZjd0niTr+UKluuCfOFqTgfLAzi+S4301zL2AkBEyVkMhBtiFVm2yoecfKlSiXfbl X-Received: by 2002:a50:a40c:: with SMTP id u12mr20406273edb.337.1605833713018; Thu, 19 Nov 2020 16:55:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605833713; cv=none; d=google.com; s=arc-20160816; b=0RuiuDPtxox9SKQcJFzzJ1KFKlk0q74hEPCkyfExlKdfzE/bcAolyhDcJuPJygsRFJ NijRLHt0SwyYSKasIyslgH7AkJXQ6DZIlyfAwOxBEsRaCupoXGCpsGhCseRuEGLwvnYd UZjuFus5XobZnlWy7sahMPqwgWtnZXxi80Z1d4kZI3pJxzjNuzBhb4ARk1SAzEZl739R bLg1Xaim1LTSjLIy5f6wLOOPmbBOpQIT+LCavdo6WfvIWXJw7mTQVPSa+ICW260/Ks3A D9ovIdg3WO/fyckzjyANXZWTmbg7yCYektdNfbe+zJ3NcCC7cO9EdJW+ltbQz+coc8UN 2bmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=mKrN4he/vrkvSrt1zels/CsZiFdIdrq/rbFyu4ivPdo=; b=DGhZtpJ6GPk91MlOWzdd4zf4T4EDwgFwa5Q1S2SLljTz70YORpp2nb1vD4sayhmOtg AXjOJciF+NMN4eueFu1OYfCJIOPK/l0sDsFzslZrIIASiofgompWDuR++XPNuLdbUKPV U8p4ZFIcPWRug5GwM1uwqr4LuwiHU8YRlkrDJbgYNOEa1NQJowyjCKuGHGjM87QfhtGx UBAlab2tFhOAy7Z8290fJJ/pRS+SLceeD07rtOOF3kmGsDjeVYsTQrMGEV79+RYI/mF6 c6wjtVBrYpJgVVnd6X2eU78RBNzdRxttKYIDkzZcy+FGCSr7YfoJ14XPgMFGD3HW57lA bRaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=G0OBorvP; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j10si849065ejn.26.2020.11.19.16.54.49; Thu, 19 Nov 2020 16:55:13 -0800 (PST) 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=@chromium.org header.s=google header.b=G0OBorvP; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726314AbgKTAwA (ORCPT + 99 others); Thu, 19 Nov 2020 19:52:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725887AbgKTAwA (ORCPT ); Thu, 19 Nov 2020 19:52:00 -0500 Received: from mail-lj1-x241.google.com (mail-lj1-x241.google.com [IPv6:2a00:1450:4864:20::241]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D86DC061A04 for ; Thu, 19 Nov 2020 16:51:58 -0800 (PST) Received: by mail-lj1-x241.google.com with SMTP id p12so8263731ljc.9 for ; Thu, 19 Nov 2020 16:51:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=mKrN4he/vrkvSrt1zels/CsZiFdIdrq/rbFyu4ivPdo=; b=G0OBorvPg6QLTXy0J826VdaMJhGCVThSwjj+bJlGsE1FzCeRy34fdo3fP95cwfZsBV pBdW9+mgCmyROiX0rFTyOO6Epo96f0KfNBCnhp/WjIiKcA4VOYKxL/6QgtZrFzwAVUy9 EGB7Cjv5dLrkBNexClj4soBKqMbASC8/nIHqk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=mKrN4he/vrkvSrt1zels/CsZiFdIdrq/rbFyu4ivPdo=; b=msgi6vhAq9J6Xmt327aOH53m6T1vcL0b6YJHhkY/YVTHYlGouTUt0fndNnA778gcOY tLyLFecZ/8OuAMcuM3J/C45h5lgKLKLc1aZ/f3Non74llbrijX4kbDZT2DxbO8ykjLKW SWyGMqT7UmDtznBNDZDkC1uu+tMqIGuvuyyCYgO0LFpOxjjNQ6nysmHMJXPt73O1AkYW Xsmu5mSxXr8tNZSHj7vLlEjb00Z9avOcIgjsWsxcy181HCJe7jnrlqSXzetpME2tzMxu X1cPSBTLi6YhjH3Q1ut3f/FCEZd+1vDR8O0GveJ5gu31WSI+gelpaGlwF/yKNbg4SAdW oBIg== X-Gm-Message-State: AOAM533sCAW8edWozAc8B34oaFF73SEodtyhX1TkjgMSToZuswYAVjTW /dwAxKTKS+eX//erCCpHymwZStG/2OmQIp16xQ1tiA== X-Received: by 2002:a2e:85c6:: with SMTP id h6mr7382215ljj.110.1605833516848; Thu, 19 Nov 2020 16:51:56 -0800 (PST) MIME-Version: 1.0 References: <20201119162654.2410685-1-revest@chromium.org> <20201119162654.2410685-5-revest@chromium.org> <20201120003217.pnqu66467punkjln@kafai-mbp.dhcp.thefacebook.com> In-Reply-To: <20201120003217.pnqu66467punkjln@kafai-mbp.dhcp.thefacebook.com> From: KP Singh Date: Fri, 20 Nov 2020 01:51:46 +0100 Message-ID: Subject: Re: [PATCH v2 5/5] bpf: Add an iterator selftest for bpf_sk_storage_get To: Martin KaFai Lau Cc: Florent Revest , bpf , Al Viro , "David S. Miller" , Jakub Kicinski , Alexei Starovoitov , Daniel Borkmann , Yonghong Song , Andrii Nakryiko , Florent Revest , open list , Networking Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Nov 20, 2020 at 1:32 AM Martin KaFai Lau wrote: > > On Thu, Nov 19, 2020 at 05:26:54PM +0100, Florent Revest wrote: > > From: Florent Revest > > > > The eBPF program iterates over all files and tasks. For all socket > > files, it stores the tgid of the last task it encountered with a handle > > to that socket. This is a heuristic for finding the "owner" of a socket > > similar to what's done by lsof, ss, netstat or fuser. Potentially, this > > information could be used from a cgroup_skb/*gress hook to try to > > associate network traffic with processes. > > > > The test makes sure that a socket it created is tagged with prog_tests's > > pid. > > > > Signed-off-by: Florent Revest > > --- > > .../selftests/bpf/prog_tests/bpf_iter.c | 35 +++++++++++++++++++ > > .../progs/bpf_iter_bpf_sk_storage_helpers.c | 26 ++++++++++++++ > > 2 files changed, 61 insertions(+) > > > > diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > > index bb4a638f2e6f..4d0626003c03 100644 > > --- a/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > > +++ b/tools/testing/selftests/bpf/prog_tests/bpf_iter.c > > @@ -975,6 +975,39 @@ static void test_bpf_sk_storage_delete(void) > > bpf_iter_bpf_sk_storage_helpers__destroy(skel); > > } > > > > +/* The BPF program stores in every socket the tgid of a task owning a handle to > > + * it. The test verifies that a locally-created socket is tagged with its pid > > + */ > > +static void test_bpf_sk_storage_get(void) > > +{ > > + struct bpf_iter_bpf_sk_storage_helpers *skel; > > + int err, map_fd, val = -1; > > + int sock_fd = -1; > > + > > + skel = bpf_iter_bpf_sk_storage_helpers__open_and_load(); > > + if (CHECK(!skel, "bpf_iter_bpf_sk_storage_helpers__open_and_load", > > + "skeleton open_and_load failed\n")) > > + return; > > + > > + sock_fd = socket(AF_INET6, SOCK_STREAM, 0); > > + if (CHECK(sock_fd < 0, "socket", "errno: %d\n", errno)) > > + goto out; > > + > > + do_dummy_read(skel->progs.fill_socket_owners); > > + > > + map_fd = bpf_map__fd(skel->maps.sk_stg_map); > > + > > + err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); > > + CHECK(err || val != getpid(), "bpf_map_lookup_elem", > > + "map value wasn't set correctly (expected %d, got %d, err=%d)\n", > > + getpid(), val, err); > > + > > + if (sock_fd >= 0) > > + close(sock_fd); > > +out: > > + bpf_iter_bpf_sk_storage_helpers__destroy(skel); > > +} > > + > > static void test_bpf_sk_storage_map(void) > > { > > DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); > > @@ -1131,6 +1164,8 @@ void test_bpf_iter(void) > > test_bpf_sk_storage_map(); > > if (test__start_subtest("bpf_sk_storage_delete")) > > test_bpf_sk_storage_delete(); > > + if (test__start_subtest("bpf_sk_storage_get")) > > + test_bpf_sk_storage_get(); > > if (test__start_subtest("rdonly-buf-out-of-bound")) > > test_rdonly_buf_out_of_bound(); > > if (test__start_subtest("buf-neg-offset")) > > diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_bpf_sk_storage_helpers.c b/tools/testing/selftests/bpf/progs/bpf_iter_bpf_sk_storage_helpers.c > > index 01ff3235e413..7206fd6f09ab 100644 > > --- a/tools/testing/selftests/bpf/progs/bpf_iter_bpf_sk_storage_helpers.c > > +++ b/tools/testing/selftests/bpf/progs/bpf_iter_bpf_sk_storage_helpers.c > > @@ -21,3 +21,29 @@ int delete_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map *ctx) > > > > return 0; > > } > > + > > +SEC("iter/task_file") > > +int fill_socket_owners(struct bpf_iter__task_file *ctx) > > +{ > > + struct task_struct *task = ctx->task; > > + struct file *file = ctx->file; > > + struct socket *sock; > > + int *sock_tgid; > > + > > + if (!task || !file || task->tgid != task->pid) > > + return 0; > > + > > + sock = bpf_sock_from_file(file); > > + if (!sock) > > + return 0; > > + > > + sock_tgid = bpf_sk_storage_get(&sk_stg_map, sock->sk, 0, > > + BPF_SK_STORAGE_GET_F_CREATE); > Does it affect all sk(s) in the system? Can it be limited to > the sk that the test is testing? Yeah, one such way would be to set the socket storage on the socket from userspace and then "search" for the socket in the iterator and mark it as found in a shared global variable.