Skip to content

Commit

Permalink
libbpf tcptop: Fix "unrecognized bpf_ld_imm64 insn" on 4.19
Browse files Browse the repository at this point in the history
Fix the BPF load error `unrecognized bpf_ld_imm64 insn`
on 4.19 by converting rodata config variables to a map.

tcptop can use CO-RE including 4.19 when a detached vmlinux BTF
file is provided, generated from a DWARF vmlinux using pahole:
pahole -J;objcopy --only-section=.BTF vmlinux /boot/vmlinux-$(uname -r)

Signed-off-by: Bernhard Kaindl <[email protected]>
  • Loading branch information
bernhardkaindl committed May 8, 2024
1 parent 07e3102 commit c237605
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
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;
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

0 comments on commit c237605

Please sign in to comment.