-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
bcc-python: support attach_func() and detach_func() #3479
Conversation
[buildbot, test this please] |
docs/reference_guide.md
Outdated
@@ -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)``` |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjusted.
docs/reference_guide.md
Outdated
|
||
Syntax: ```BPF.attach_func(fn, attach_type [, attachable_fd [, flags)``` | ||
|
||
Attaches a BPF function of the specified type. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, adjusted.
docs/reference_guide.md
Outdated
|
||
### 11. detach_func() | ||
|
||
Syntax: ```BPF.detach_func(fn, detach_type [, target_fd)``` |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjusted.
examples/networking/sockmap.py
Outdated
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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added.
[buildbot, test this please] |
There was a problem hiding this 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!
docs/reference_guide.md
Outdated
@@ -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)``` |
There was a problem hiding this comment.
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]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjusted.
examples/networking/sockmap.py
Outdated
|
||
|
||
examples = """examples: | ||
./sockmap.py /root/cgroup # attach to /root/cgroup |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjusted.
examples/networking/sockmap.py
Outdated
|
||
bpf_text = ''' | ||
#include <net/sock.h> | ||
#include <bcc/proto.h> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed.
[buildbot, test this please] |
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.
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.
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.
- support attach_func() and detach_func(). - add an sockmap issue to demonstrate using these two functions.
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.
Add
attach_func()
anddetach_func()
.Add a usage sample.(
sockmap.py
)