Files
xdp-project-bpf-examples/pkt-loop-filter/get-bond-active.kern.c
Toke Høiland-Jørgensen 0306ff7cca pkt-loop-filter: Handle old type of net->net_cookie
The type of the net->net_cookie field member was changed in kernel 5.12
with commit 3d368ab87cf6 ("net: initialize net->net_cookie at netns setup").
Older versions of the kernel devices net->net_cookie as an atomic64_t
instead of a u64. This causes CO-RE reading of the field to fail due to the
type mismatch. Handle this by adding CO-RE checks for the old type as well
and using the CO-RE facility to check for the right type at load time.

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
2022-07-08 14:46:14 +02:00

31 lines
896 B
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
#include <linux/bpf.h>
#include <asm/ptrace.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_core_read.h>
#include "bpf-defs.h"
int bond_ifindex = 0;
int active_slave_ifindex = 0;
volatile const int netns_cookie = INIT_NS;
SEC("kprobe/bond_select_active_slave")
int BPF_KPROBE(handle_select_slave, struct bonding *bond)
{
struct net_device *dev = BPF_PROBE_READ(bond, dev);
struct net *net = BPF_CORE_READ(dev, nd_net.net);
int ifindex = BPF_CORE_READ(dev, ifindex);
__u64 cookie = read_net_cookie(net);
if (cookie == netns_cookie && ifindex == bond_ifindex) {
struct net_device *active_dev = BPF_PROBE_READ(bond, curr_active_slave, dev);
active_slave_ifindex = BPF_CORE_READ(active_dev, ifindex);
}
return 0;
}
char _license[] SEC("license") = "GPL";