Skip to content

Conversation

jessfraz
Copy link
Contributor

@jessfraz jessfraz commented Apr 25, 2018

This includes:

  • BPF_OBJ_GET_INFO_BY_FD
  • BPF_PROG_GET_FD_BY_ID
  • BPF_PROG_GET_NEXT_ID

Happy to change anything if the naming/design is not right.


This change is Reviewable

@jessfraz jessfraz requested a review from a team April 25, 2018 21:29
@ciliumbot
Copy link

Can one of the admins verify this patch?

7 similar comments
@ciliumbot
Copy link

Can one of the admins verify this patch?

@ciliumbot
Copy link

Can one of the admins verify this patch?

@ciliumbot
Copy link

Can one of the admins verify this patch?

@ciliumbot
Copy link

Can one of the admins verify this patch?

@ciliumbot
Copy link

Can one of the admins verify this patch?

@ciliumbot
Copy link

Can one of the admins verify this patch?

@ciliumbot
Copy link

Can one of the admins verify this patch?

@jessfraz jessfraz changed the title pg/bpf: add function wrappers for prog syscalls. pkg/bpf: add function wrappers for prog syscalls. Apr 25, 2018
@jessfraz jessfraz force-pushed the add-prog-syscalls branch 2 times, most recently from b1f8344 to 3bf9312 Compare April 25, 2018 21:32
@ianvernon
Copy link
Member

Ciliumbot is quite insistent that we verify this patch :)

@ianvernon
Copy link
Member

test-me-please

@tgraf tgraf added kind/enhancement This would improve or streamline existing functionality. priority/1.1 labels Apr 25, 2018
Copy link
Member

@joestringer joestringer left a 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.
Copy link
Member

Choose a reason for hiding this comment

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

s/upstram/upstream/

Copy link
Contributor Author

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
Copy link
Member

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 .

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 :)

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 {
Copy link
Member

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?

Copy link
Contributor Author

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 {
Copy link
Member

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 {
Copy link
Member

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 {
Copy link
Member

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

Copy link
Member

@borkmann borkmann left a 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
Copy link
Member

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.

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, 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
Copy link
Member

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

@jessfraz jessfraz force-pushed the add-prog-syscalls branch from 3bf9312 to c3ac0f8 Compare April 26, 2018 13:42
@jessfraz
Copy link
Contributor Author

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 :)

@jessfraz jessfraz force-pushed the add-prog-syscalls branch 4 times, most recently from ccb7da2 to b53924e Compare April 26, 2018 14:39
@jessfraz
Copy link
Contributor Author

Oh and this is what I am using it for :) https://github.com/jessfraz/bpfps

Copy link
Member

@joestringer joestringer left a 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 {
Copy link
Member

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) {
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

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

s/it's/its/

@jessfraz jessfraz force-pushed the add-prog-syscalls branch from b53924e to 8ba46da Compare April 26, 2018 16:57
@jessfraz
Copy link
Contributor Author

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>
@jessfraz jessfraz force-pushed the add-prog-syscalls branch from 8ba46da to 54de2c5 Compare April 26, 2018 17:00
Copy link
Member

@tgraf tgraf left a comment

Choose a reason for hiding this comment

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

Awesome, thanks @jessfraz

@tgraf
Copy link
Member

tgraf commented Apr 26, 2018

test-me-please

@tgraf tgraf merged commit 597871e into cilium:master Apr 26, 2018
@jessfraz jessfraz deleted the add-prog-syscalls branch April 26, 2018 20:03
@jessfraz
Copy link
Contributor Author

thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement This would improve or streamline existing functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants