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

BCC cross compilation support #1569

Merged
merged 2 commits into from
Feb 8, 2018

Conversation

joelagnel
Copy link
Contributor

These patches make it possible to build eBPF programs from a custom kernel source directory and a custom user-specified ARCH. This support is one of the pieces for the effort to make BCC work across systems using BPFd.

@yonghong-song
Copy link
Collaborator

[buildbot, ok to test]

const char *archenv = getenv("ARCH");
if (!archenv)
archenv = getenv("BCC_ARCH");

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 difference between ARCH and BCC_ARCH? Should we use an environment variable or a cmake macro definition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CMake macro definition would make it static then right? We want it to be dynamic. This is indeed how the kernel is built in a cross compiler environment as well. One does something like this in kernel build directory, for example:

ARCH=arm64 make zImage

Similarly, we need BCC to adapt to the user's cross compile environment like the kernel does.

Copy link
Collaborator

Choose a reason for hiding this comment

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

okay, env seems fine here. What is the different between ARCH and BCC_ARCH?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just thought of giving BCC_ARCH as an additional option since most (all?) env vars or macros are prefixed with BCC_. There's no difference between them.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we just one, e.g., BCC_TARGET_ARCH? Having two is not really necessary here, in my opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I agree we can have one. Hope you agree that lets just keep ARCH because this what the kernel uses as well so it will simplify the kernel developer's workflow here.

archenv = getenv("BCC_ARCH");

if (archenv) {
if (getenv("BCC_KBUILD_DEBUG"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use one of existing debug flags for output here? For example, debug=4 for preprocessor result can be used since this impacts the clang commands?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll look into that, thanks

Copy link
Contributor Author

@joelagnel joelagnel Feb 3, 2018

Choose a reason for hiding this comment

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

Sure, I'll reuse DEBUG_PREPROCESSOR to print the chosen ARCH. Can I also reuse DEBUG_PREPROCESSOR to print the kernel source directory path or does another flag make more sense?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can just reuse DEBUG_PREPROCESSOR to print the kernel source directoryy path.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you think of a way to put the code dealing different architectures in one place (header is okay), so the main.c file will be clean?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yonghong-song didn't follow your comment. When you say main.c, do you mean the /virtual/main.c file?

Also the code dealing with different architectures is already in a separate header (helpers.h). Do you mean the code shared between loader.cc and b_frontend_action.cc should be made common? I think we can do that using function pointers and enums. So we could define one enum per arch, and pass a function pointer callback to generic code that handles different arches, which would call then call the callback after detecting the ARCH. That would keep the "arch detection logic" separate from the code that "uses the arch and does different things".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that I would keep helpers.h as is in the original patch though since that code isn't shared by the CC code (unless I missed something).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry for confusion. yes, I do mean the code shared between loader.cc and b_frontend_action.cc should be made common.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it.

string kdir = string(KERNEL_MODULES_DIR) + "/" + un.release;
auto kernel_path_info = get_kernel_path_info (kdir);
string kdir, kpath;
const char *kpath_env = ::getenv("BCC_KERNEL_SOURCE");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again about env variable vs. cmake macro? I would prefer cmake macro to make the code less dependent on external factors. If use cmake macro, maybe KERNEL_SOURCE_DIR as the macro name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same reason as above, BCC should adapt to different kernel sources that the same BCC installation can point to dynamically. We shouldn't restrict this to a statically known path.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay with me for this.

Joel Fernandes added 2 commits February 6, 2018 11:52
Many developers have kernel sources at an specific location. Allow
clang to build from there by allowing to specify a single absolute
path to the kernel sources through a new env var BCC_KERNEL_SOURCE.

Signed-off-by: Joel Fernandes <[email protected]>
BCC at the moment builds eBPF only considering the local architecture
instead of the one that the user's target kernel is running on.
For cross-compiler environments, the ARCH environment variable is
used to specify which ARCH to build the kernel for. In this patch we
add support to read ARCH and if that's not set, then fallback to
detecting based on local architecture.

This patch borrows some code from a patch doing a similar thing for
eBPF samples in the kenrel that I submitted recently [1]

[1] https://patchwork.kernel.org/patch/9961801/

Signed-off-by: Joel Fernandes <[email protected]>
@joelagnel
Copy link
Contributor Author

Simplified the patch, its much cleaner. Let me know comments if any.

Copy link
Collaborator

@yonghong-song yonghong-song left a comment

Choose a reason for hiding this comment

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

LGTM. Some documentations are needed about how to use it (e.g., additional mandatory env variables), and some tests are needed as well. This can be done when more functionalities are merged.

@joelagnel
Copy link
Contributor Author

Yes, I agree. thanks

@yonghong-song yonghong-song merged commit e01c993 into iovisor:master Feb 8, 2018
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.

None yet

2 participants