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=-8.5 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_AGENT_MUTT 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 BDE50C282C6 for ; Sat, 26 Jan 2019 00:56:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8104D21902 for ; Sat, 26 Jan 2019 00:56:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=fomichev-me.20150623.gappssmtp.com header.i=@fomichev-me.20150623.gappssmtp.com header.b="dYIvAO/T" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729354AbfAZA4S (ORCPT ); Fri, 25 Jan 2019 19:56:18 -0500 Received: from mail-pg1-f194.google.com ([209.85.215.194]:43430 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725916AbfAZA4Q (ORCPT ); Fri, 25 Jan 2019 19:56:16 -0500 Received: by mail-pg1-f194.google.com with SMTP id v28so4874101pgk.10 for ; Fri, 25 Jan 2019 16:56:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fomichev-me.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=exHC9Pwzn9PHESb07Rw4cCMzZZrZjgAJUnkd4ShkqXU=; b=dYIvAO/TD1ZeHZieBzyLRmaUSzz2I0cXqxMB8hHZOy0zwVVCG/0WCG0wREWfV8h9fL J5/4ICCFbw6L3CcavZBy1eNMsybpSVFZLGQUoN+KqVM2fVv3Ufzu1NO9nGHiIe77Kv0a zhX+0mfZ8JuIfXAu9st9no2BDCOq3hElqR6rIW2zAaa64BqIOpsaOKkuzerSfMalqvcA H8WFUCY6OPlFTbuzQBvpYdj3c8fXzHvg++6EcKC48zHrp1Fs5JRKgf8xqo0WvxC0kpjt UceegBBJxBxM3EOPL5DTRItr+Ejv49IvOUgH2MWWbh3CagmadN0Y4Pg9F71MXOwk99os hDnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=exHC9Pwzn9PHESb07Rw4cCMzZZrZjgAJUnkd4ShkqXU=; b=taAX6SlRvo/kEJ/hRfEY23p/i3TGQpafW9BSJRMPD9v029IH6UcTD9NEYrugianFCZ g65bvgvj1p0q6OXAldx+3O4eBS5cWdLs+RLprU18IVDPni/tlHAsKlTtBT0VEOKxykPB o48rSGqPrazPq1639un8kPVWfJ24RcJAvusnh4YDW9tClnT0ihwduIDQ97lA62O1rj8w Mqb21JlvtY1MZ2VTOMmnOKPNS9gO+TaQ68zIwTR1afzNpcpFdgkdcbJN1TpsXpyDR+Ed 5Jy4M1PjJEKS1QZjWWgnDFhO2xfp577gG916qhdxZJbrZjpFg10NvkqtbtHpL1oHUzVi j3BA== X-Gm-Message-State: AJcUukfzoU0rZReufRVsZUDoXQxl0dtza4bPqEYl9mTmUI7gm/BISvzt RPb1irRYM0Y2VtoLbJs9UAnpMA== X-Google-Smtp-Source: ALg8bN4xZU+Sz49FpC9YOj52NA1IMZLOPFsWl7j7Ey3A7nxWuHic9D1wVJpca+UTLzszxPPrTJu4dw== X-Received: by 2002:a63:e80e:: with SMTP id s14mr11920220pgh.30.1548464175362; Fri, 25 Jan 2019 16:56:15 -0800 (PST) Received: from localhost ([2601:646:8f00:18d9:d0fa:7a4b:764f:de48]) by smtp.gmail.com with ESMTPSA id k14sm40057708pgs.52.2019.01.25.16.56.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 25 Jan 2019 16:56:14 -0800 (PST) Date: Fri, 25 Jan 2019 16:56:13 -0800 From: Stanislav Fomichev To: Song Liu Cc: Stanislav Fomichev , Networking , "David S . Miller" , Alexei Starovoitov , Daniel Borkmann Subject: Re: [PATCH bpf-next v2 2/3] bpf: add BPF_PROG_TEST_RUN support for flow dissector Message-ID: <20190126005613.GE26773@mini-arch> References: <20190124164953.29740-1-sdf@google.com> <20190124164953.29740-3-sdf@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.11.2 (2019-01-07) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On 01/25, Song Liu wrote: > On Thu, Jan 24, 2019 at 8:53 AM Stanislav Fomichev wrote: > > > > The input is packet data, the output is struct bpf_flow_key. This should > > make it easy to test flow dissector programs without elaborate > > setup. > > > > Signed-off-by: Stanislav Fomichev > > --- > > include/linux/bpf.h | 3 ++ > > net/bpf/test_run.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ > > net/core/filter.c | 1 + > > 3 files changed, 86 insertions(+) > > > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > index e734f163bd0b..701ef954a258 100644 > > --- a/include/linux/bpf.h > > +++ b/include/linux/bpf.h > > @@ -397,6 +397,9 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, > > union bpf_attr __user *uattr); > > int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr, > > union bpf_attr __user *uattr); > > +int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, > > + const union bpf_attr *kattr, > > + union bpf_attr __user *uattr); > > > > /* an array of programs to be executed under rcu_lock. > > * > > diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c > > index fa2644d276ef..2c5172b33209 100644 > > --- a/net/bpf/test_run.c > > +++ b/net/bpf/test_run.c > > @@ -240,3 +240,85 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, > > kfree(data); > > return ret; > > } > > + > > +int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog, > > + const union bpf_attr *kattr, > > + union bpf_attr __user *uattr) > > I think this function duplicates a lot of logic from bpf_prog_test_run_skb(). > Can we somehow reuse bpf_prog_test_run_skb()? I did that initially (https://marc.info/?l=linux-netdev&m=154830227529929&w=2), but then Alexey suggested that there is not much to reuse (plus can hinder the test performance of the existing types). > > Thanks, > Song > > > > +{ > > + u32 size = kattr->test.data_size_in; > > + u32 repeat = kattr->test.repeat; > > + struct bpf_flow_keys flow_keys; > > + u64 time_start, time_spent = 0; > > + struct bpf_skb_data_end *cb; > > + u32 retval, duration; > > + struct sk_buff *skb; > > + struct sock *sk; > > + void *data; > > + int ret; > > + u32 i; > > + > > + if (prog->type != BPF_PROG_TYPE_FLOW_DISSECTOR) > > + return -EINVAL; > > + > > + data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN, > > + SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); > > + if (IS_ERR(data)) > > + return PTR_ERR(data); > > + > > + sk = kzalloc(sizeof(*sk), GFP_USER); > > + if (!sk) { > > + kfree(data); > > + return -ENOMEM; > > + } > > + sock_net_set(sk, current->nsproxy->net_ns); > > + sock_init_data(NULL, sk); > > + > > + skb = build_skb(data, 0); > > + if (!skb) { > > + kfree(data); > > + kfree(sk); > > + return -ENOMEM; > > + } > > + skb->sk = sk; > > + > > + skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); > > + __skb_put(skb, size); > > + skb->protocol = eth_type_trans(skb, > > + current->nsproxy->net_ns->loopback_dev); > > + skb_reset_network_header(skb); > > + > > + cb = (struct bpf_skb_data_end *)skb->cb; > > + cb->qdisc_cb.flow_keys = &flow_keys; > > + > > + if (!repeat) > > + repeat = 1; > > + > > + time_start = ktime_get_ns(); > > + for (i = 0; i < repeat; i++) { > > + preempt_disable(); > > + rcu_read_lock(); > > + retval = __skb_flow_bpf_dissect(prog, skb, > > + &flow_keys_dissector, > > + &flow_keys); > > + rcu_read_unlock(); > > + preempt_enable(); > > + > > + if (need_resched()) { > > + if (signal_pending(current)) > > + break; > > + time_spent += ktime_get_ns() - time_start; > > + cond_resched(); > > + time_start = ktime_get_ns(); > > + } > > + } > > + time_spent += ktime_get_ns() - time_start; > > + do_div(time_spent, repeat); > > + duration = time_spent > U32_MAX ? U32_MAX : (u32)time_spent; > > + > > + ret = bpf_test_finish(kattr, uattr, &flow_keys, sizeof(flow_keys), > > + retval, duration); > > + > > + kfree_skb(skb); > > + kfree(sk); > > + return ret; > > +} > > diff --git a/net/core/filter.c b/net/core/filter.c > > index 2b3b436ef545..ff4641dae2be 100644 > > --- a/net/core/filter.c > > +++ b/net/core/filter.c > > @@ -7690,6 +7690,7 @@ const struct bpf_verifier_ops flow_dissector_verifier_ops = { > > }; > > > > const struct bpf_prog_ops flow_dissector_prog_ops = { > > + .test_run = bpf_prog_test_run_flow_dissector, > > }; > > > > int sk_detach_filter(struct sock *sk) > > -- > > 2.20.1.321.g9e740568ce-goog > >