-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
libbpf-tools/profile: Add support for PID-namespacing #5152
Open
ekyooo
wants to merge
2
commits into
iovisor:master
Choose a base branch
from
ekyooo:profile_pidns
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,10 @@ | |
#include <time.h> | ||
#include <linux/perf_event.h> | ||
#include <asm/unistd.h> | ||
#include <sys/utsname.h> | ||
#include <bpf/libbpf.h> | ||
#include <bpf/bpf.h> | ||
#include <sys/stat.h> | ||
#include "profile.h" | ||
#include "profile.skel.h" | ||
#include "trace_helpers.h" | ||
|
@@ -506,6 +508,40 @@ static int print_counts(int counts_map, int stack_map) | |
return ret; | ||
} | ||
|
||
static int kernel_at_least(int req_major, int req_minor) { | ||
struct utsname u; | ||
int major, minor; | ||
|
||
if (uname(&u) == -1) | ||
return -errno; | ||
|
||
if (sscanf(u.release, "%d.%d", &major, &minor) != 2) | ||
return -errno; | ||
|
||
return !((major > req_major) || (major == req_major && minor >= req_minor)); | ||
} | ||
|
||
static int set_pidns(const struct profile_bpf *obj) | ||
{ | ||
struct stat statbuf; | ||
|
||
/* | ||
* Available on Linux version 5.7 and above, which includes | ||
* the helper function bpf_get_ns_current_pid_tgid(). | ||
*/ | ||
if (kernel_at_least(5, 7)) | ||
return -EPERM; | ||
|
||
if (stat("/proc/self/ns/pid", &statbuf) == -1) | ||
return -errno; | ||
|
||
obj->rodata->use_pidns = true; | ||
obj->rodata->pidns_dev = statbuf.st_dev; | ||
obj->rodata->pidns_ino = statbuf.st_ino; | ||
|
||
return 0; | ||
} | ||
|
||
static void print_headers() | ||
{ | ||
int i; | ||
|
@@ -595,6 +631,10 @@ int main(int argc, char **argv) | |
env.perf_max_stack_depth * sizeof(unsigned long)); | ||
bpf_map__set_max_entries(obj->maps.stackmap, env.stack_storage_size); | ||
|
||
err = set_pidns(obj); | ||
if (err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let us do 'err && env.verbose' here. We want to print out the below information under verbose mode. |
||
fprintf(stderr, "failed to translate pidns: %s\n", strerror(-err)); | ||
|
||
err = profile_bpf__load(obj); | ||
if (err) { | ||
fprintf(stderr, "failed to load BPF programs\n"); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The problem with fixed version check is that they will not work on kernel where a specific patch could have been backported.
Is there a way to rather check for the existence of a given bpf helper?
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 bcc/tools, we have a few cases (including profile) checking kernel version for bpf_get_ns_current_pid_tgid() availability. But for libbpf-tools, I think we can do better.
See trace_helpers.c. There are a few probe_*() functions which tries to detect whether a particular feature (prog_type, map_type) is supported or not. I think we can have another probe function to check whether bpf_get_ns_current_pid_tgid() is supported or not.