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

link: use BPF links to attach Tracing and LSM prog types #837

Merged
merged 3 commits into from
Mar 29, 2023

Conversation

mmat11
Copy link
Contributor

@mmat11 mmat11 commented Nov 3, 2022

Fallback to sys.RawTracepointOpen() when not possible.

I've exported the Program's AttachType since it's needed when creating the link, the alternative would be to have separate functions for the different attach types, eg. AttachFEntry, AttachFExit, etc. but this would be a breaking change.

@mmat11 mmat11 force-pushed the matt/tracing-cookie branch 2 times, most recently from a26dfe8 to 8d5fec1 Compare November 3, 2022 21:47
@mmat11 mmat11 marked this pull request as ready for review November 3, 2022 21:53
@mmat11 mmat11 marked this pull request as draft November 11, 2022 12:13
@mmat11 mmat11 force-pushed the matt/tracing-cookie branch 2 times, most recently from 1de69a6 to 74af42c Compare December 11, 2022 13:56
@mmat11 mmat11 marked this pull request as ready for review December 11, 2022 13:56
@mmat11 mmat11 force-pushed the matt/tracing-cookie branch 2 times, most recently from 6cf5ec9 to 178fefb Compare January 28, 2023 00:30
@mmat11 mmat11 force-pushed the matt/tracing-cookie branch 2 times, most recently from 3d3609a to 9120a11 Compare February 13, 2023 21:49
@mmat11
Copy link
Contributor Author

mmat11 commented Feb 13, 2023

ping @lmb this can be reviewed

@lmb
Copy link
Collaborator

lmb commented Feb 14, 2023

Hey, I'm currently onboarding at Isovalent, so this might take me a while to get to. How urgent is this?

@mmat11
Copy link
Contributor Author

mmat11 commented Feb 14, 2023

Hey, I'm currently onboarding at Isovalent, so this might take me a while to get to. How urgent is this?

not urgent at all, take your time!

return nil, fmt.Errorf("create tracing link: %w", err)
}
fallthrough
case ebpf.AttachNone:
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the reason the default attach type can't use bpf_link?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AttachType needs to be specified in LinkCreateTracingAttr

Copy link
Collaborator

Choose a reason for hiding this comment

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

https://elixir.bootlin.com/linux/v6.2.2/source/kernel/bpf/syscall.c#L4577

		ret = bpf_tracing_prog_attach(prog,
					      attr->link_create.target_fd,
					      attr->link_create.target_btf_id,
					      attr->link_create.tracing.cookie);

https://elixir.bootlin.com/linux/v6.2.2/source/kernel/bpf/syscall.c#L4595

			ret = bpf_tracing_prog_attach(prog,
						      attr->link_create.target_fd,
						      attr->link_create.target_btf_id,
						      attr->link_create.tracing.cookie);

https://elixir.bootlin.com/linux/v6.2.2/source/kernel/bpf/syscall.c#L3310

		return bpf_tracing_prog_attach(prog, 0, 0, 0);
  • Both RAW_TRACEPOINT_OPEN and BPF_LINK_CREATE return a link fd (on newer kernels)
  • But RAW_TRACEPOINT_OPEN doesn't allow specifying target or cookie

On new kernels, RAW_TRACEPOINT_OPEN == BPF_LINK_CREATE if target_fd, cookie, etc. are 0.

Thinking through the compatibility story here: seems like specifying AttachType on a kernel without LINK_CREATE will fail. So it's not possible to just always set AttachType if you want maximum compat, instead you should only set it if Cookie != 0. Can we do better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, to recap:

  • on new kernels it's always better to use LINK_CREATE as it behaves the same way if cookie etc. are 0
  • on old kernels we can fail and fallback (fallthrough) to RAW_TRACEPOINT_OPEN

seems like specifying AttachType on a kernel without LINK_CREATE will fail

this should be handled by fallthrough, right?

So it's not possible to just always set AttachType if you want maximum compat, instead you should only set it if Cookie != 0

could you clarify this point? If I set opts.AttachType on a kernel that doesn't support LINK_CREATE, I expect it to fail and fallback to RAW_TRACEPOINT_OPEN

Copy link
Collaborator

Choose a reason for hiding this comment

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

this should be handled by fallthrough, right?

yes, my bad, sorry!

  • on new kernels it's always better to use LINK_CREATE as it behaves the same way if cookie etc. are 0

Nit: seems to me like LINK_CREATE vs RAW_TRACEPOINT_OPEN makes no difference if cookie is 0. They end up calling the same function?

Agreed that we should prefer LINK_CREATE.

Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

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

Sorry it took me so long to review, please feel free to ping me earlier in the future!

return nil, fmt.Errorf("create tracing link: %w", err)
}
fallthrough
case ebpf.AttachNone:
Copy link
Collaborator

Choose a reason for hiding this comment

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

https://elixir.bootlin.com/linux/v6.2.2/source/kernel/bpf/syscall.c#L4577

		ret = bpf_tracing_prog_attach(prog,
					      attr->link_create.target_fd,
					      attr->link_create.target_btf_id,
					      attr->link_create.tracing.cookie);

https://elixir.bootlin.com/linux/v6.2.2/source/kernel/bpf/syscall.c#L4595

			ret = bpf_tracing_prog_attach(prog,
						      attr->link_create.target_fd,
						      attr->link_create.target_btf_id,
						      attr->link_create.tracing.cookie);

https://elixir.bootlin.com/linux/v6.2.2/source/kernel/bpf/syscall.c#L3310

		return bpf_tracing_prog_attach(prog, 0, 0, 0);
  • Both RAW_TRACEPOINT_OPEN and BPF_LINK_CREATE return a link fd (on newer kernels)
  • But RAW_TRACEPOINT_OPEN doesn't allow specifying target or cookie

On new kernels, RAW_TRACEPOINT_OPEN == BPF_LINK_CREATE if target_fd, cookie, etc. are 0.

Thinking through the compatibility story here: seems like specifying AttachType on a kernel without LINK_CREATE will fail. So it's not possible to just always set AttachType if you want maximum compat, instead you should only set it if Cookie != 0. Can we do better?

@mmat11 mmat11 force-pushed the matt/tracing-cookie branch from 9120a11 to 878b466 Compare March 24, 2023 10:40
@mmat11 mmat11 force-pushed the matt/tracing-cookie branch from 878b466 to 1a05c15 Compare March 24, 2023 10:42
Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

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

LGTM! @ti-mo want to take another look or can I merge?

@lmb lmb merged commit a162c6b into cilium:master Mar 29, 2023
@lmb
Copy link
Collaborator

lmb commented Mar 29, 2023

Timo is out, so going ahead. Thanks for your contribution as always! 🎉

@lmb lmb changed the title Use BPF links to attach Tracing and LSM prog types link: use BPF links to attach Tracing and LSM prog types Mar 29, 2023
@ti-mo
Copy link
Collaborator

ti-mo commented Mar 30, 2023

Thanks!

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.

3 participants