All of lore.kernel.org
 help / color / mirror / Atom feed
* libbpf attaching a raw socket
@ 2021-08-12  1:36 Mohan Parthasarathy
  2021-08-13 17:22 ` Yonghong Song
  0 siblings, 1 reply; 4+ messages in thread
From: Mohan Parthasarathy @ 2021-08-12  1:36 UTC (permalink / raw)
  To: bpf

Hi,

I looked in the samples and header files, but could not find an
example for this. How does one convert this from bcc to libbpf. This
is the userspace code.

bpf = BPF(src_file = "socket_filter.c", debug=0)
socket_filter = bpf.load_func("socket_filter", BPF.SOCKET_FILTER)
BPF.attach_raw_socket(socket_filter, "eth2")
socket_fd = socket_filter.sock

I can do the following to set the type:

err = bpf_program__set_socket_filter(obj);

Is there any sample I can follow or any header files where I can look
for attaching a socket to the bpf code.

Thanks
Mohan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: libbpf attaching a raw socket
  2021-08-12  1:36 libbpf attaching a raw socket Mohan Parthasarathy
@ 2021-08-13 17:22 ` Yonghong Song
  2021-08-13 19:01   ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Yonghong Song @ 2021-08-13 17:22 UTC (permalink / raw)
  To: Mohan Parthasarathy, bpf



On 8/11/21 6:36 PM, Mohan Parthasarathy wrote:
> Hi,
> 
> I looked in the samples and header files, but could not find an
> example for this. How does one convert this from bcc to libbpf. This
> is the userspace code.
> 
> bpf = BPF(src_file = "socket_filter.c", debug=0)
> socket_filter = bpf.load_func("socket_filter", BPF.SOCKET_FILTER)
> BPF.attach_raw_socket(socket_filter, "eth2")
> socket_fd = socket_filter.sock
> 
> I can do the following to set the type:
> 
> err = bpf_program__set_socket_filter(obj);
> 
> Is there any sample I can follow or any header files where I can look
> for attaching a socket to the bpf code.

There are a few examples in linux/samples/bpf directory:

[~/work/bpf-next/samples/bpf] grep SOCKET_F *.c
cookie_uid_helper_example.c:    prog_fd = 
bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
fds_example.c:          return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
sockex1_user.c: if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
sockex2_user.c: if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
sock_example.c: prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, 
prog, insns_cnt,

They should provide an example how to attach a socket_filter program
to a socket.

> 
> Thanks
> Mohan
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: libbpf attaching a raw socket
  2021-08-13 17:22 ` Yonghong Song
@ 2021-08-13 19:01   ` Andrii Nakryiko
  2021-08-16  4:30     ` Mohan Parthasarathy
  0 siblings, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2021-08-13 19:01 UTC (permalink / raw)
  To: Yonghong Song; +Cc: Mohan Parthasarathy, bpf, Kumar Kartikeya Dwivedi

On Fri, Aug 13, 2021 at 10:23 AM Yonghong Song <yhs@fb.com> wrote:
>
>
>
> On 8/11/21 6:36 PM, Mohan Parthasarathy wrote:
> > Hi,
> >
> > I looked in the samples and header files, but could not find an
> > example for this. How does one convert this from bcc to libbpf. This
> > is the userspace code.
> >
> > bpf = BPF(src_file = "socket_filter.c", debug=0)
> > socket_filter = bpf.load_func("socket_filter", BPF.SOCKET_FILTER)
> > BPF.attach_raw_socket(socket_filter, "eth2")
> > socket_fd = socket_filter.sock
> >
> > I can do the following to set the type:
> >
> > err = bpf_program__set_socket_filter(obj);
> >
> > Is there any sample I can follow or any header files where I can look
> > for attaching a socket to the bpf code.
>
> There are a few examples in linux/samples/bpf directory:
>
> [~/work/bpf-next/samples/bpf] grep SOCKET_F *.c
> cookie_uid_helper_example.c:    prog_fd =
> bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
> fds_example.c:          return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
> sockex1_user.c: if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
> sockex2_user.c: if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
> sock_example.c: prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,

just please don't use bpf_load_program and bpf_prog_load, use proper
bpf_object APIs or BPF skeleton for loading programs.

> prog, insns_cnt,
>
> They should provide an example how to attach a socket_filter program
> to a socket.

You can probably also look at recently added bpf_tc_* APIs in
tools/testing/selftests/bpf/prog_tests/tc_bpf.c. I've CC'ed Kumar who
added those APIs recently.

>
> >
> > Thanks
> > Mohan
> >

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: libbpf attaching a raw socket
  2021-08-13 19:01   ` Andrii Nakryiko
@ 2021-08-16  4:30     ` Mohan Parthasarathy
  0 siblings, 0 replies; 4+ messages in thread
From: Mohan Parthasarathy @ 2021-08-16  4:30 UTC (permalink / raw)
  To: Andrii Nakryiko; +Cc: Yonghong Song, bpf, Kumar Kartikeya Dwivedi

Hi,

I see examples both under

- samples/bpf/sock*
-tools/testing/selftests/bpf/progs

The programs under bpf seem to include bpf_legacy.h for load_byte etc.
Is this still valid or this will go away in the future?

Thanks
Mohan

On Fri, Aug 13, 2021 at 12:01 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Aug 13, 2021 at 10:23 AM Yonghong Song <yhs@fb.com> wrote:
> >
> >
> >
> > On 8/11/21 6:36 PM, Mohan Parthasarathy wrote:
> > > Hi,
> > >
> > > I looked in the samples and header files, but could not find an
> > > example for this. How does one convert this from bcc to libbpf. This
> > > is the userspace code.
> > >
> > > bpf = BPF(src_file = "socket_filter.c", debug=0)
> > > socket_filter = bpf.load_func("socket_filter", BPF.SOCKET_FILTER)
> > > BPF.attach_raw_socket(socket_filter, "eth2")
> > > socket_fd = socket_filter.sock
> > >
> > > I can do the following to set the type:
> > >
> > > err = bpf_program__set_socket_filter(obj);
> > >
> > > Is there any sample I can follow or any header files where I can look
> > > for attaching a socket to the bpf code.
> >
> > There are a few examples in linux/samples/bpf directory:
> >
> > [~/work/bpf-next/samples/bpf] grep SOCKET_F *.c
> > cookie_uid_helper_example.c:    prog_fd =
> > bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER, prog,
> > fds_example.c:          return bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
> > sockex1_user.c: if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
> > sockex2_user.c: if (bpf_prog_load(filename, BPF_PROG_TYPE_SOCKET_FILTER,
> > sock_example.c: prog_fd = bpf_load_program(BPF_PROG_TYPE_SOCKET_FILTER,
>
> just please don't use bpf_load_program and bpf_prog_load, use proper
> bpf_object APIs or BPF skeleton for loading programs.
>
> > prog, insns_cnt,
> >
> > They should provide an example how to attach a socket_filter program
> > to a socket.
>
> You can probably also look at recently added bpf_tc_* APIs in
> tools/testing/selftests/bpf/prog_tests/tc_bpf.c. I've CC'ed Kumar who
> added those APIs recently.
>
> >
> > >
> > > Thanks
> > > Mohan
> > >

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-16  4:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-12  1:36 libbpf attaching a raw socket Mohan Parthasarathy
2021-08-13 17:22 ` Yonghong Song
2021-08-13 19:01   ` Andrii Nakryiko
2021-08-16  4:30     ` Mohan Parthasarathy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.