mirror of
https://github.com/xdp-project/bpf-examples.git
synced 2024-05-06 15:54:53 +00:00
Add a small utility that uses a kprobe to extract the currently active slave ifindex from a bond interface. This value is normally only exported to userspace for bond types where it can be explicitly set, but the bond driver has an internal notion of an active interface regardless of the bond type. We can extract this value with a kprobe by attaching to a function in the bond driver and triggering an operation that causes this function to be called. Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
30 lines
867 B
C
30 lines
867 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);
|
|
int ifindex = BPF_CORE_READ(dev, ifindex);
|
|
__u64 cookie = BPF_CORE_READ(dev, nd_net.net, net_cookie);
|
|
|
|
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";
|