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

libbpf tcptop: Fix "unrecognized bpf_ld_imm64 insn" on 4.19 #4987

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions libbpf-tools/tcptop.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
#define AF_INET 2 /* Internet IP Protocol */
#define AF_INET6 10 /* IP version 6 */

const volatile bool filter_cg = false;
const volatile pid_t target_pid = -1;
const volatile int target_family = -1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The above coding pattern has been used in most of libbpf-tools bpf programs. I would rather to keep this. Changing to use map does not sound good compared to more elegant way in the above. 4.19 is quite old. Maybe you could folk the repo and make change in your private repo?

struct {
__uint(type, BPF_MAP_TYPE_ARRAY);
__uint(max_entries, 1);
__type(key, u32);
__type(value, struct filter_cfg_t);
} filter_cfg_map SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_CGROUP_ARRAY);
Expand All @@ -32,20 +35,25 @@ struct {

static int probe_ip(bool receiving, struct sock *sk, size_t size)
{
int zero = 0;
struct filter_cfg_t *args = bpf_map_lookup_elem(&filter_cfg_map, &zero);
if (!args)
return 0;

struct ip_key_t ip_key = {};
struct traffic_t *trafficp;
u16 family;
u32 pid;

if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
if (args->filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;

pid = bpf_get_current_pid_tgid() >> 32;
if (target_pid != -1 && target_pid != pid)
if (args->target_pid != -1 && args->target_pid != pid)
return 0;

family = BPF_CORE_READ(sk, __sk_common.skc_family);
if (target_family != -1 && target_family != family)
if (args->target_family != -1 && args->target_family != family)
return 0;

/* drop */
Expand Down
16 changes: 12 additions & 4 deletions libbpf-tools/tcptop.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ int main(int argc, char **argv)
struct tcptop_bpf *obj;
int family;
int cgfd = -1;
int zero = 0;
int err;

err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
Expand All @@ -359,18 +360,25 @@ int main(int argc, char **argv)
return 1;
}

obj->rodata->target_pid = target_pid;
obj->rodata->target_family = family;
obj->rodata->filter_cg = cgroup_filtering;
struct filter_cfg_t filter_cfg = {
.filter_cg = cgroup_filtering,
.target_pid = target_pid,
.target_family = family,
};

err = tcptop_bpf__load(obj);
if (err) {
warn("failed to load BPF object: %d\n", err);
goto cleanup;
}

int filter_cfg_fd = bpf_map__fd(obj->maps.filter_cfg_map);
if (bpf_map_update_elem(filter_cfg_fd, &zero, &filter_cfg, BPF_ANY)) {
warn("Failed to update filter config map\n");
goto cleanup;
}

if (cgroup_filtering) {
int zero = 0;
int cg_map_fd = bpf_map__fd(obj->maps.cgroup_map);

cgfd = open(cgroup_path, O_RDONLY);
Expand Down
6 changes: 6 additions & 0 deletions libbpf-tools/tcptop.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

#define TASK_COMM_LEN 16

struct filter_cfg_t {
bool filter_cg;
pid_t target_pid;
int target_family;
};

struct ip_key_t {
unsigned __int128 saddr;
unsigned __int128 daddr;
Expand Down
5 changes: 3 additions & 2 deletions tests/python/test_tools_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ def test_offcputime(self):
self.run_with_duration("offcputime.py 1")

@skipUnless(kernel_version_ge(4,6), "requires kernel >= 4.6")
@mayFail("This fails on github actions environment, and needs to be fixed")
def test_offwaketime(self):
self.run_with_duration("offwaketime.py 1", timeout=30)
self.run_with_duration("offwaketime.py 2", timeout=30)

@skipUnless(kernel_version_ge(4,9), "requires kernel >= 4.9")
def test_oomkill(self):
Expand Down Expand Up @@ -324,7 +325,7 @@ def test_solisten(self):
@skipUnless(kernel_version_ge(4,4), "requires kernel >= 4.4")
@mayFail("This fails on github actions environment, and needs to be fixed")
def test_sslsniff(self):
self.run_with_int("sslsniff.py")
self.run_with_int("sslsniff.py --no-nss")

@skipUnless(kernel_version_ge(4,6), "requires kernel >= 4.6")
def test_stackcount(self):
Expand Down
Loading