netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf <bpf@vger.kernel.org>, Networking <netdev@vger.kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kernel Team <kernel-team@fb.com>, Martin KaFai Lau <kafai@fb.com>
Subject: Re: [PATCH bpf-next v3 05/15] bpf: add bpf_skc_to_tcp6_sock() helper
Date: Tue, 23 Jun 2020 13:46:22 -0700	[thread overview]
Message-ID: <051dab4c-bbde-320e-c2bf-da63a7994fc4@fb.com> (raw)
In-Reply-To: <CAEf4BzYMG7xu2ot-8OVJjYG7w14OciKgN=hZombOqo=7d5oUNQ@mail.gmail.com>



On 6/23/20 1:11 PM, Andrii Nakryiko wrote:
> On Tue, Jun 23, 2020 at 12:47 PM Yonghong Song <yhs@fb.com> wrote:
>>
>>
>>
>> On 6/23/20 11:23 AM, Andrii Nakryiko wrote:
>>> On Tue, Jun 23, 2020 at 7:52 AM Yonghong Song <yhs@fb.com> wrote:
>>>>
>>>>
>>>>
>>>> On 6/22/20 11:39 PM, Andrii Nakryiko wrote:
>>>>> On Mon, Jun 22, 2020 at 5:38 PM Yonghong Song <yhs@fb.com> wrote:
>>>>>>
>>>>>> The helper is used in tracing programs to cast a socket
>>>>>> pointer to a tcp6_sock pointer.
>>>>>> The return value could be NULL if the casting is illegal.
>>>>>>
>>>>>> A new helper return type RET_PTR_TO_BTF_ID_OR_NULL is added
>>>>>> so the verifier is able to deduce proper return types for the helper.
>>>>>>
>>>>>> Different from the previous BTF_ID based helpers,
>>>>>> the bpf_skc_to_tcp6_sock() argument can be several possible
>>>>>> btf_ids. More specifically, all possible socket data structures
>>>>>> with sock_common appearing in the first in the memory layout.
>>>>>> This patch only added socket types related to tcp and udp.
>>>>>>
>>>>>> All possible argument btf_id and return value btf_id
>>>>>> for helper bpf_skc_to_tcp6_sock() are pre-calculcated and
>>>>>> cached. In the future, it is even possible to precompute
>>>>>> these btf_id's at kernel build time.
>>>>>>
>>>>>> Acked-by: Martin KaFai Lau <kafai@fb.com>
>>>>>> Signed-off-by: Yonghong Song <yhs@fb.com>
>>>>>> ---
>>>>>
>>>>> Looks good to me as is, but see a few suggestions, they will probably
>>>>> save me time at some point as well :)
>>>>>
>>>>> Acked-by: Andrii Nakryiko <andriin@fb.com>
>>>>>
>>>>>
>>>>>>     include/linux/bpf.h            | 12 +++++
>>>>>>     include/uapi/linux/bpf.h       |  9 +++-
>>>>>>     kernel/bpf/btf.c               |  1 +
>>>>>>     kernel/bpf/verifier.c          | 43 +++++++++++++-----
>>>>>>     kernel/trace/bpf_trace.c       |  2 +
>>>>>>     net/core/filter.c              | 80 ++++++++++++++++++++++++++++++++++
>>>>>>     scripts/bpf_helpers_doc.py     |  2 +
>>>>>>     tools/include/uapi/linux/bpf.h |  9 +++-
>>>>>>     8 files changed, 146 insertions(+), 12 deletions(-)
>>>>>>
>>>>>
>>>>> [...]
>>>>>
>>>>>> @@ -4815,6 +4826,18 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insn
>>>>>>                    regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL;
>>>>>>                    regs[BPF_REG_0].id = ++env->id_gen;
>>>>>>                    regs[BPF_REG_0].mem_size = meta.mem_size;
>>>>>> +       } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL) {
>>>>>> +               int ret_btf_id;
>>>>>> +
>>>>>> +               mark_reg_known_zero(env, regs, BPF_REG_0);
>>>>>> +               regs[BPF_REG_0].type = PTR_TO_BTF_ID_OR_NULL;
>>>>>> +               ret_btf_id = *fn->ret_btf_id;
[...]
>>>
>>>>
>>>>>
>>>>>> +               if (ret_btf_id == 0) {
>>>>>
>>>>> This also has to be struct/union (after typedef/mods stripping, of
>>>>> course). Or are there other cases?
>>>>
>>>> This is an "int". The func_proto difinition is below:
>>>> int *ret_btf_id; /* return value btf_id */
>>>
>>> I meant the BTF type itself that this btf_id points to. Is there any
>>> use case where this won't be a pointer to struct/union and instead
>>> something like a pointer to an int?
>>
>> Maybe you misunderstood. The mechanism is similar to the argument btf_id
>> encoding in func_proto's:
>>
>> static int bpf_seq_printf_btf_ids[5];
>> ...
>>           .btf_id         = bpf_seq_printf_btf_ids,
>>
>> func_proto->ret_btf_id will be a pointer to int which encodes the
>> btf_id, not the btf_type.
> 
> I understand that. Say it points to value 25. BTF type with ID=25 is
> going to be BTF_KIND_PTR -> BTF_KIND_STRUCT. I was wondering if we
> want/need to check that it's always BTF_KIND_PTR -> (modifier)* ->
> BTF_KIND_STRUCT/BTF_KIND_UNION. That's it.

Just to be clear. The ret_btf_id returned here is the btf id is the
type id of the pointee, so in this case it is BTF_KIND_STRUCT/....

These id's are pre-calculated and stored in memory. Unless the whole
thing is mess up, there is no need to check...

> 
>>
>>>
>>>>
>>>>>
>>>>>> +                       verbose(env, "invalid return type %d of func %s#%d\n",
>>>>>> +                               fn->ret_type, func_id_name(func_id), func_id);
>>>>>> +                       return -EINVAL;
>>>>>> +               }
>>>>>> +               regs[BPF_REG_0].btf_id = ret_btf_id;
>>>>>>            } else {
>>>>>>                    verbose(env, "unknown return type %d of func %s#%d\n",
>>>>>>                            fn->ret_type, func_id_name(func_id), func_id);
>>>>>
>>>>> [...]
>>>>>
>>>>>> +void init_btf_sock_ids(struct btf *btf)
>>>>>> +{
>>>>>> +       int i, btf_id;
>>>>>> +
>>>>>> +       for (i = 0; i < MAX_BTF_SOCK_TYPE; i++) {
>>>>>> +               btf_id = btf_find_by_name_kind(btf, bpf_sock_types[i],
>>>>>> +                                              BTF_KIND_STRUCT);
>>>>>> +               if (btf_id > 0)
>>>>>> +                       btf_sock_ids[i] = btf_id;
>>>>>> +       }
>>>>>> +}
>>>>>
>>>>> This will hopefully go away with Jiri's work on static BTF IDs, right?
>>>>> So looking forward to that :)
>>>>
>>>> Yes. That's the plan.
>>>>
>>>>>
>>>>>> +
>>>>>> +static bool check_arg_btf_id(u32 btf_id, u32 arg)
>>>>>> +{
>>>>>> +       int i;
>>>>>> +
>>>>>> +       /* only one argument, no need to check arg */
>>>>>> +       for (i = 0; i < MAX_BTF_SOCK_TYPE; i++)
>>>>>> +               if (btf_sock_ids[i] == btf_id)
>>>>>> +                       return true;
>>>>>> +       return false;
>>>>>> +}
>>>>>> +
>>>>>
>>>>> [...]
>>>>>

  reply	other threads:[~2020-06-23 20:53 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-23  0:36 [PATCH bpf-next v3 00/15] implement bpf iterator for tcp and udp sockets Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 01/15] net: bpf: add bpf_seq_afinfo in tcp_iter_state Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 02/15] net: bpf: implement bpf iterator for tcp Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 03/15] bpf: support 'X' in bpf_seq_printf() helper Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 04/15] bpf: allow tracing programs to use bpf_jiffies64() helper Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 05/15] bpf: add bpf_skc_to_tcp6_sock() helper Yonghong Song
2020-06-23  5:46   ` kernel test robot
2020-06-23  5:53   ` kernel test robot
2020-06-23  6:39   ` Andrii Nakryiko
2020-06-23 14:52     ` Yonghong Song
2020-06-23 18:23       ` Andrii Nakryiko
2020-06-23 19:45         ` Yonghong Song
2020-06-23 20:11           ` Andrii Nakryiko
2020-06-23 20:46             ` Yonghong Song [this message]
2020-06-23  0:36 ` [PATCH bpf-next v3 06/15] bpf: add bpf_skc_to_{tcp,tcp_timewait,tcp_request}_sock() helpers Yonghong Song
2020-06-23  5:18   ` kernel test robot
2020-06-23  6:39   ` kernel test robot
2020-06-23  0:36 ` [PATCH bpf-next v3 07/15] net: bpf: add bpf_seq_afinfo in udp_iter_state Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 08/15] net: bpf: implement bpf iterator for udp Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 09/15] bpf: add bpf_skc_to_udp6_sock() helper Yonghong Song
2020-06-23  1:47   ` Eric Dumazet
2020-06-23  2:22     ` Yonghong Song
2020-06-23 16:27       ` Eric Dumazet
2020-06-23 17:03         ` Yonghong Song
2020-06-23 22:11           ` Eric Dumazet
2020-06-23 22:44             ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 10/15] bpf/selftests: move newer bpf_iter_* type redefining to a new header file Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 11/15] tools/bpf: refactor some net macros to libbpf bpf_tracing_net.h Yonghong Song
2020-06-23  6:45   ` Andrii Nakryiko
2020-06-23 14:56     ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 12/15] tools/libbpf: add more common macros to bpf_tracing_net.h Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 13/15] tools/bpf: selftests: implement sample tcp/tcp6 bpf_iter programs Yonghong Song
2020-06-23  6:56   ` Andrii Nakryiko
2020-06-23 15:03     ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 14/15] tools/bpf: add udp4/udp6 bpf iterator Yonghong Song
2020-06-23  6:57   ` Andrii Nakryiko
2020-06-23 15:03     ` Yonghong Song
2020-06-23  0:36 ` [PATCH bpf-next v3 15/15] bpf/selftests: add tcp/udp iterator programs to selftests Yonghong Song
2020-06-23  6:59   ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=051dab4c-bbde-320e-c2bf-da63a7994fc4@fb.com \
    --to=yhs@fb.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).