1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
SEC("kprobe")
int BPF_KPROBE(k_xdp_install, struct net_device *dev, struct netdev_bpf *bpf)
{
struct xdp_install_args *args;
args = get_install_args();
if (!args)
return BPF_OK;
if (!is_setup_xdp(bpf)) {
clear_install_args(args);
return BPF_OK;
}
args->dev = dev;
args->bpf = bpf;
return BPF_OK;
}
static __always_inline void
output_netdev(struct event *ev, struct net_device *dev)
{
BPF_CORE_READ_INTO(&ev->ifindex, dev, ifindex);
bpf_probe_read_kernel_str(ev->ifname, sizeof(ev->ifname), dev->name);
}
static __always_inline void
output_bpf(struct event *ev, struct netdev_bpf *bpf)
{
BPF_CORE_READ_INTO(&ev->prog_id, bpf, prog, aux, id);
bpf_probe_read_kernel_str(ev->prog, sizeof(ev->prog), BPF_CORE_READ(bpf, prog, aux, name));
}
static __always_inline void
output_errmsg(struct event *ev, int retval, struct netdev_bpf *bpf)
{
if (retval == 0)
return;
bpf_probe_read_kernel_str(ev->errmsg, sizeof(ev->errmsg), ev->errmsg);
}
SEC("kretprobe")
int BPF_KRETPROBE(kr_xdp_install, int retval)
{
struct xdp_install_args *args;
args = get_install_args();
if (!args || !args->dev || !args->bpf)
return BPF_OK;
struct event ev = {};
output_netdev(&ev, args->dev);
output_bpf(&ev, args->bpf);
ev.retval = retval;
output_errmsg(&ev, retval, args->bpf);
bpf_perf_event_output(ctx, &events, BPF_F_CURRENT_CPU, &ev, sizeof(ev));
return BPF_OK;
}
|