-
Notifications
You must be signed in to change notification settings - Fork 3.4k
pkg/bpf: add function wrappers for prog syscalls. #3899
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
Conversation
Can one of the admins verify this patch? |
7 similar comments
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
b1f8344
to
3bf9312
Compare
Ciliumbot is quite insistent that we verify this patch :) |
test-me-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.
Thanks for the contribution!
I think my main comment would be that if these are within the pkg/bpf
, then it may not be obvious that they apply to program types. It'd be nice to denote this somehow, ideally by abstracting the programs with proper Golang type arguments (rather than unsafe.pointer
) so that the callers can't mess up what they pass in, but perhaps also by adding Prog
into the name, eg GetProgFDByID()
.
Without a user in the codebase, it's hard to see whether there are other ways to improve this. Do you have a user in mind from within Cilium? A particular feature or something?
pkg/bpf/prog.go
Outdated
openFlags uint32 | ||
} | ||
|
||
// ProgInfo holds values from the upstram struct bpf_prog_info. |
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.
s/upstram/upstream/
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.
fixed :)
pkg/bpf/prog.go
Outdated
} | ||
|
||
// ProgInfo holds values from the upstram struct bpf_prog_info. | ||
// From: https://github.com/torvalds/linux/blob/master/include/uapi/linux/bpf.h#L1006 |
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.
Might be good to refer to a permalink, such as https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/bpf.h#L924 .
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 :)
pkg/bpf/prog.go
Outdated
} | ||
|
||
// GetObjInfoByFD gets the bpf program info from it's file descriptor. | ||
func GetObjInfoByFD(fd int, info unsafe.Pointer, size uintptr) error { |
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.
Can the info
argument here be a ProgInfo
pointer or something instead?
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.
done :)
pkg/bpf/prog.go
Outdated
} | ||
|
||
// GetNextID takes a program ID and returns the next program ID. | ||
func GetNextID(start, next unsafe.Pointer) error { |
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.
What are the actual types that users should be using here? 32-bit integers for the program ids? Perhaps we could change this to something like func GetNextID(start, next *uint32) error {
. Then the caller is guaranteed to provide the right parameter type.
pkg/bpf/prog.go
Outdated
@@ -67,3 +74,72 @@ func (t ProgType) String() string { | |||
|
|||
return "Unknown" | |||
} | |||
|
|||
type attrProg struct { |
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.
Could you add a comment similar to the one you have below, pointing to this?
https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/bpf.h#L299
pkg/bpf/prog.go
Outdated
Name string | ||
} | ||
|
||
type attrObjInfo struct { |
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.
Somehow I dropped my comment here. Similar to the above about union bpf_attr
, I think this is the link:
https://github.com/torvalds/linux/blob/v4.16/include/uapi/linux/bpf.h#L309
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 a lot for the patch, Jess! Two small review comments inline.
pkg/bpf/prog.go
Outdated
|
||
type attrProg struct { | ||
startID uint32 | ||
progID uint32 |
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.
In the uapi header the {start_id, prog_id, map_id} is a union, wouldn't this give wrong offset in GetNextID() and GetFDByID() as it currently is? Maybe this could just be named currID or objID as a single member here.
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, and added a comment much like the one you have here: https://github.com/cilium/cilium/blob/master/pkg/bpf/bpf.go#L120 :)
pkg/bpf/prog.go
Outdated
type ProgInfo struct { | ||
ProgType ProgType | ||
ID uint32 | ||
MapIDs uint64 |
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.
Would that be passed / used in GetObjInfoByFD() as info, since mapping to struct bpf_prog_info
is different? Note the __aligned_u64 is actually a pointer to a __u32 array, in the uapi header it's done as such to avoid having to implement the compat layer for it in kernel to cope with 32 bit uspace on 64 bit kernel. https://github.com/torvalds/linux/blob/v4.16/tools/bpf/bpftool/prog.c#L185
3bf9312
to
c3ac0f8
Compare
Thanks so much for the review :) I do not really have a use case for cilium per se... mostly I really like your bpf package and needed these other syscalls and thought why not :) |
ccb7da2
to
b53924e
Compare
Oh and this is what I am using it for :) https://github.com/jessfraz/bpfps |
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.
Some further minor comments after taking a second look. In particular I realised one of my bits of feedback was not very Go-like.
Otherwise LGTM.
pkg/bpf/prog.go
Outdated
|
||
// GetProgNextID takes a current program ID and a pointer to the next program ID | ||
// and fills the next program ID. | ||
func GetProgNextID(current uint32, next *uint32) error { |
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.
It occurred to me that my C background was showing through on my prior review here. In Go it's more natural to return (uint32, error)
rather than passing in a pointer.
pkg/bpf/prog.go
Outdated
} | ||
|
||
// GetObjInfoByFD gets the bpf program info from it's file descriptor. | ||
func GetObjInfoByFD(fd int) (ProgInfo, error) { |
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.
Maybe GetProgInfoByFD
?
pkg/bpf/prog.go
Outdated
return int(fd), nil | ||
} | ||
|
||
// GetObjInfoByFD gets the bpf program info from it's file descriptor. |
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.
s/it's/its/
b53924e
to
8ba46da
Compare
sweet updated :) |
This includes: - BPF_OBJ_GET_INFO_BY_FD - BPF_PROG_GET_FD_BY_ID - BPF_PROG_GET_NEXT_ID Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
8ba46da
to
54de2c5
Compare
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.
Awesome, thanks @jessfraz
test-me-please |
thanks!! |
This includes:
Happy to change anything if the naming/design is not right.
This change is