bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Eelco Chaudron" <echaudro@redhat.com>
To: "Andrii Nakryiko" <andrii.nakryiko@gmail.com>
Cc: "Jakub Sitnicki" <jakub@cloudflare.com>,
	bpf <bpf@vger.kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Networking <netdev@vger.kernel.org>,
	"Alexei Starovoitov" <ast@kernel.org>,
	"Daniel Borkmann" <daniel@iogearbox.net>,
	"Martin Lau" <kafai@fb.com>, "Song Liu" <songliubraving@fb.com>,
	"Yonghong Song" <yhs@fb.com>, "Andrii Nakryiko" <andriin@fb.com>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>
Subject: Re: [PATCH bpf-next v4 2/3] libbpf: Add support for dynamic program attach target
Date: Thu, 20 Feb 2020 14:21:08 +0100	[thread overview]
Message-ID: <90FEACCF-CF15-4694-9F51-4E3F6817439F@redhat.com> (raw)
In-Reply-To: <CAEf4BzY3cwPvj9=wo_GJxN=1=5fJL1RuhjEfey3N09GOL0YYfw@mail.gmail.com>



On 19 Feb 2020, at 18:41, Andrii Nakryiko wrote:

> On Wed, Feb 19, 2020 at 3:06 AM Eelco Chaudron <echaudro@redhat.com> wrote:
>>
>>
>>
>> On 18 Feb 2020, at 22:24, Andrii Nakryiko wrote:
>>
>>> On Tue, Feb 18, 2020 at 8:34 AM Jakub Sitnicki <jakub@cloudflare.com>
>>> wrote:
>>>>
>>>> Hey Eelco,
>>>>
>>>> On Mon, Feb 17, 2020 at 12:43 PM GMT, Eelco Chaudron wrote:
>>>>> Currently when you want to attach a trace program to a bpf program
>>>>> the section name needs to match the tracepoint/function semantics.
>>>>>
>>>>> However the addition of the bpf_program__set_attach_target() API
>>>>> allows you to specify the tracepoint/function dynamically.
>>>>>
>>>>> The call flow would look something like this:
>>>>>
>>>>>   xdp_fd = bpf_prog_get_fd_by_id(id);
>>>>>   trace_obj = bpf_object__open_file("func.o", NULL);
>>>>>   prog = bpf_object__find_program_by_title(trace_obj,
>>>>>                                            "fentry/myfunc");
>>>>>   bpf_program__set_expected_attach_type(prog, BPF_TRACE_FENTRY);
>>>>>   bpf_program__set_attach_target(prog, xdp_fd,
>>>>>                                  "xdpfilt_blk_all");
>>>>>   bpf_object__load(trace_obj)
>>>>>
>>>>> Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
>>>>> Signed-off-by: Eelco Chaudron <echaudro@redhat.com>
>>>>> ---
>>>>>  tools/lib/bpf/libbpf.c   |   34 ++++++++++++++++++++++++++++++----
>>>>>  tools/lib/bpf/libbpf.h   |    4 ++++
>>>>>  tools/lib/bpf/libbpf.map |    2 ++
>>>>>  3 files changed, 36 insertions(+), 4 deletions(-)
>>>>>
>>>>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>>>>> index 514b1a524abb..0c25d78fb5d8 100644
>>>>> --- a/tools/lib/bpf/libbpf.c
>>>>> +++ b/tools/lib/bpf/libbpf.c
>>>>
>>>> [...]
>>>>
>>>>> @@ -8132,6 +8133,31 @@ void bpf_program__bpil_offs_to_addr(struct
>>>>> bpf_prog_info_linear *info_linear)
>>>>>       }
>>>>>  }
>>>>>
>>>>> +int bpf_program__set_attach_target(struct bpf_program *prog,
>>>>> +                                int attach_prog_fd,
>>>>> +                                const char *attach_func_name)
>>>>> +{
>>>>> +     int btf_id;
>>>>> +
>>>>> +     if (!prog || attach_prog_fd < 0 || !attach_func_name)
>>>>> +             return -EINVAL;
>>>>> +
>>>>> +     if (attach_prog_fd)
>>>>> +             btf_id = libbpf_find_prog_btf_id(attach_func_name,
>>>>> +                                              attach_prog_fd);
>>>>> +     else
>>>>> +             btf_id = __find_vmlinux_btf_id(prog->obj->btf_vmlinux,
>>>>> +                                            attach_func_name,
>>>>> +
>>>>> prog->expected_attach_type);
>>>>> +
>>>>> +     if (btf_id <= 0)
>>>>> +             return btf_id;
>>>>
>>>> Looks like we can get 0 as return value on both error and success
>>>> (below)?  Is that intentional?
>>>
>>> Neither libbpf_find_prog_btf_id nor __find_vmlinux_btf_id are going to
>>> return 0 on failure. But I do agree that if (btf_id < 0) check would
>>> be better here.
>>
>> Is see in theory btf__find_by_name_kind() could return 0:
>>
>>         if (kind == BTF_KIND_UNKN || !strcmp(type_name, "void"))
>>                 return 0;
>>
>> But for our case, this will not happen and is invalid, so what about
>> just to make sure its future proof?:
>>
>>    if (btf_id <= 0)
>>          return btf_id ? btf_id : -ENOENT;
>
> I don't see how void can be the right attach type, so I'd keep it
> simple: if (btf_id < 0) return btf_id.
> If it so happens that 0 is returned, it will fail at attach time anyways.

Ok, will send out a v5 later today…

>>> With that minor nit:
>>>
>>> Acked-by: Andrii Nakryiko <andriin@fb.com>
>>>
>>>>
>>>>> +
>>>>> +     prog->attach_btf_id = btf_id;
>>>>> +     prog->attach_prog_fd = attach_prog_fd;
>>>>> +     return 0;
>>>>> +}
>>>>> +
>>>>>  int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz)
>>>>>  {
>>>>>       int err = 0, n, len, start, end = -1;
>>>>
>>>> [...]
>>


  reply	other threads:[~2020-02-20 13:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-17 12:42 [PATCH bpf-next v4 0/3] libbpf: Add support for dynamic program attach target Eelco Chaudron
2020-02-17 12:43 ` [PATCH bpf-next v4 1/3] libbpf: Bump libpf current version to v0.0.8 Eelco Chaudron
2020-02-17 12:43 ` [PATCH bpf-next v4 2/3] libbpf: Add support for dynamic program attach target Eelco Chaudron
2020-02-18 16:34   ` Jakub Sitnicki
2020-02-18 21:24     ` Andrii Nakryiko
2020-02-19 11:06       ` Eelco Chaudron
2020-02-19 17:41         ` Andrii Nakryiko
2020-02-20 13:21           ` Eelco Chaudron [this message]
2020-02-17 12:43 ` [PATCH bpf-next v4 3/3] selftests/bpf: update xdp_bpf2bpf test to use new set_attach_target API Eelco Chaudron
2020-02-18 21:21   ` Andrii Nakryiko
2020-02-19 10:54     ` Eelco Chaudron

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=90FEACCF-CF15-4694-9F51-4E3F6817439F@redhat.com \
    --to=echaudro@redhat.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=jakub@cloudflare.com \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=toke@redhat.com \
    --cc=yhs@fb.com \
    /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).