Skip to content
/ gobpf Public
forked from iovisor/gobpf
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

Add function SetKprobeForSection to change section name in module #1

Merged
merged 14 commits into from
Sep 18, 2019

Conversation

shang-wang
Copy link

@shang-wang shang-wang commented Sep 12, 2019

Currently gobpf uses the name defined in SEC() header of each function to bind to the right eBPF API. This creates some inflexibility when we need two implementations of a certain API. For example:

In kernels later than v4.1.0, tcp_sendmsg is defined as

int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)

In kernels earlier than v4.1.0, the definition is

int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, size_t size)

With the change in this pull request we could override the names in any mappings that are stored in module. This means that we can have a custom name in SEC() header but still map the implementation to an API that we want. For instance, SEC("kprobe/tcp_sendmsg_v2") can map to kprobe/tcp_sendmsg if we call SetKprobeForSection("tcp_sendmsg_v2", "tcp_sendmsg").

When we enable the kprobe later on, just do

m.EnableKprobe("kprobe/tcp_sendmsg_v2", maxActive)

and underlying probe would bind to tcp_sendmsg API correctly.

elf/module.go Outdated Show resolved Hide resolved
@shang-wang shang-wang changed the title Add function UpdateSecName to change section name in module Add function UpdateKprobeSecName to change section name in module Sep 16, 2019
elf/module.go Outdated Show resolved Hide resolved
"testing"
)

func TestUpdateKprobeSecName(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

I think what we want to test is the side effect of this, not the Name field.

So basically we want to have a Kprobe handler under an arbitrary name SEC("foobar"), attach that handler to Kprobe by doing something like

m.UpdateKprobeSecName("foobar,"kprobe/whatever")
m.EnableKprobe("foobar")

and finally verify that your handler was executed when whatever ran.

Copy link
Author

Choose a reason for hiding this comment

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

I was thinking about the same thing, but the direct result of calling EnableKprobe() is not easy to check(requires mount of debug, etc), that's why the overall tests in elf is lacking.

Choose a reason for hiding this comment

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

How about adding a new function EnableKProbeForFunction(secName string, funcName string, isRetProbe bool, string maxactive) that doesn't tie the function to the name of the entry in b.probes ? You could implement EnableKProbe in terms of EnableKProbeForFunction.

Copy link
Author

@shang-wang shang-wang Sep 16, 2019

Choose a reason for hiding this comment

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

@leeavital there are other ways that EnableKprobe is used(https://github.com/DataDog/gobpf/blob/master/elf/module.go#L373), if you don't change the actual kprobe name, you might ended up with surprises for the inconsistencies. Does that make sense?

Choose a reason for hiding this comment

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

Won't EnableKrpobes break anyway? If there is a kprobe named kprobe/udp_sendmsg/rhel, the it will try to attach that kprobe to a function named udp_sendmsg/rhel which won't exist.

Copy link
Author

Choose a reason for hiding this comment

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

It won't break if you call "update" first, but it's hard to call both EnableKprobe and EnableKProbeForFunction in a for loop in EnableKprobes if you want to enable certain kprobe differently.

Copy link
Member

Choose a reason for hiding this comment

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

Looking at your other PR I'm starting to think that a cleaner way to do this would be not changing the API of the package at all and establishing another convention like:

SEC("kprobe/tcp_sendmsg/arbitrary_sufix")

which would generate a handler that will get attached kprobe/tcp_sendmsg. We would simply dismiss everything after the second /.

Copy link
Member

@p-lambert p-lambert left a comment

Choose a reason for hiding this comment

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

Left a couple of nits, but I think this is fine to merge.

elf/module.go Outdated

// UpdateKprobeNameForHandler takes a handler and try to find the corresponding kprobe from a previously loaded mapping,
// if found then update the kprobe name to the value of newKprobeName
func (b *Module) UpdateKprobeNameForHandler(handler, newKprobeName string) error {

Choose a reason for hiding this comment

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

we should make it explicit in the function name and documentation that handler is a section name.

Copy link
Author

Choose a reason for hiding this comment

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

UpdateKprobeNameForSecHandler? It cannot be any longer 🤣

Choose a reason for hiding this comment

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

How about SetKprobeForSection ?

Copy link
Author

Choose a reason for hiding this comment

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

OK that could work. Although given that they mix all terms together, nothing seems to be able remove the confusion completely.

@shang-wang shang-wang changed the title Add function UpdateKprobeSecName to change section name in module Add function SetKprobeForSection to change section name in module Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants