Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bcc-python: support attach_func() and detach_func() #3479

Merged
merged 5 commits into from
Jun 14, 2021
Merged

bcc-python: support attach_func() and detach_func() #3479

merged 5 commits into from
Jun 14, 2021

Conversation

chenyuezhou
Copy link
Contributor

Add attach_func() and detach_func().

Add a usage sample.(sockmap.py)

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

@@ -1847,6 +1849,42 @@ Examples in situ:
[search /examples](https://github.com/iovisor/bcc/search?q=attach_xdp+path%3Aexamples+language%3Apython&type=Code),
[search /tools](https://github.com/iovisor/bcc/search?q=attach_xdp+path%3Atools+language%3Apython&type=Code)

### 10. attach_func()

Syntax: ```BPF.attach_func(fn, attach_type [, attachable_fd [, flags)```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can have a signature like

BPF.attach_func(fn, attachable_fd, attach_type [, flags])

Basically make attachable_fd mandatory and flags optional for python APIs.
The only case that an attchable_fd is not needed (must be 0) is for net_namespace. I think it is okay to require it and this makes argument sequence consistent with C++ APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted.


Syntax: ```BPF.attach_func(fn, attach_type [, attachable_fd [, flags)```

Attaches a BPF function of the specified type.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Attaches a BPF function of the specified type to a particular attachable_fd. if the attach_type is BPF_FLOW_DISSECTOR, the function is expected to attach to current net namespace and attachable_fd must be 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, adjusted.


### 11. detach_func()

Syntax: ```BPF.detach_func(fn, detach_type [, target_fd)```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, let us do BPF.detach_func(fn, attachable_fd, attach_type), similar to C++ APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted.

func_sock_redir = bpf.load_func("bpf_redir", bpf.SK_MSG)
# raise if error
fd = os.open(args.cgroup, os.O_RDONLY)
map_fd = lib.bpf_table_fd(bpf.module, "sock_hash")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a failure like below with python3.6,

$ sudo python3.6 sockmap.py -c /sys/fs/cgroup/system.slice/tmp.service/
Traceback (most recent call last):
  File "sockmap.py", line 115, in <module>
    map_fd = lib.bpf_table_fd(bpf.module, "sock_hash")
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

Changing "sock_hash" to b"sock_hash" fixed the issue. Probably some python byte array thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed.

if not isinstance(fn, BPF.Function):
raise Exception("arg 1 must be of type BPF.Function")

res = lib.bpf_prog_attach(fn.fd, attachable_fd, attach_type, flags)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we should add bpf_prog_attach/detach2 to libbcc.py to make it explicit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

Copy link
Collaborator

@yonghong-song yonghong-song left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just a few minor comments. Thanks!

@@ -1847,6 +1849,42 @@ Examples in situ:
[search /examples](https://github.com/iovisor/bcc/search?q=attach_xdp+path%3Aexamples+language%3Apython&type=Code),
[search /tools](https://github.com/iovisor/bcc/search?q=attach_xdp+path%3Atools+language%3Apython&type=Code)

### 10. attach_func()

Syntax: ```BPF.attach_func(fn, attachable_fd, attach_type [, flags)```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer we add ']' after '[, flags', so change to [, flags]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted.



examples = """examples:
./sockmap.py /root/cgroup # attach to /root/cgroup
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

./sockmap.py -c /root/cgroup ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adjusted.


bpf_text = '''
#include <net/sock.h>
#include <bcc/proto.h>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include <bcc/proto.h> is not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed.

@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

@yonghong-song yonghong-song merged commit 5934161 into iovisor:master Jun 14, 2021
davemarchevsky added a commit to davemarchevsky/bcc that referenced this pull request Jun 16, 2021
In iovisor#3479, the `bpf_attach_type` enum was pulled into the `BPF` class so
that its members could be used in `attach_func` and `detach_func`
functions introduced to the Python API.

Unfortunately this caused a redefinition of BPF.XDP, which was similarly
pulled in from `bpf_prog_type` enum, thus breaking program loading
(iovisor#3489).

Let's pull these enum- and flag-type class variables out into their own
wrapper classes. For backwards compatibility, keep them all (except for
`bpf_attach_type`, which was merged 2 days ago) defined in the BPF
class, but add a comment to not continue doing this.
yonghong-song pushed a commit that referenced this pull request Jun 16, 2021
In #3479, the `bpf_attach_type` enum was pulled into the `BPF` class so
that its members could be used in `attach_func` and `detach_func`
functions introduced to the Python API.

Unfortunately this caused a redefinition of BPF.XDP, which was similarly
pulled in from `bpf_prog_type` enum, thus breaking program loading
(#3489).

Let's pull these enum- and flag-type class variables out into their own
wrapper classes. For backwards compatibility, keep them all (except for
`bpf_attach_type`, which was merged 2 days ago) defined in the BPF
class, but add a comment to not continue doing this.
brho pushed a commit to brho/bcc that referenced this pull request Nov 3, 2021
In iovisor#3479, the `bpf_attach_type` enum was pulled into the `BPF` class so
that its members could be used in `attach_func` and `detach_func`
functions introduced to the Python API.

Unfortunately this caused a redefinition of BPF.XDP, which was similarly
pulled in from `bpf_prog_type` enum, thus breaking program loading
(iovisor#3489).

Let's pull these enum- and flag-type class variables out into their own
wrapper classes. For backwards compatibility, keep them all (except for
`bpf_attach_type`, which was merged 2 days ago) defined in the BPF
class, but add a comment to not continue doing this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants