Reputation: 422
I want to use the kfunc bpf_xdp_metadata_rx_hash
to get the flow hash in an XDP program as follows:
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
enum xdp_rss_hash_type *rss_type) __ksym;
SEC("xdp")
int dummy(struct xdp_md *ctx)
{
// get hash
u32 hash;
enum xdp_rss_hash_type hash_type;
if (bpf_xdp_metadata_rx_hash(ctx, &hash, &hash_type)) {
bpf_printk("xdp: ERROR failed to read hash");
return XDP_PASS;
}
bpf_printk("xdp: hash=%x, type=%x", hash, hash_type);
return XDP_PASS;
}
char _license[] SEC("license") = "GPL";
I have tried to attach my XDP program to my NIC (using ip
tool), but get the following error:
$ sudo ip link set enp129s0f0np0 xdp obj dummy.bpf.o sec xdp
libbpf: prog 'dummy': BPF program load failed: Invalid argument
libbpf: prog 'dummy': -- BEGIN PROG LOAD LOG --
metadata kfuncs require device-bound program
processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0
-- END PROG LOAD LOG --
libbpf: prog 'dummy': failed to load: -22
libbpf: failed to load object 'dummy.bpf.o'
How can I make my XDP program device-bound?
Upvotes: 2
Views: 11
Reputation: 422
From the official BPF documentation:
There are two ways of attaching XDP programs to network devices, the legacy way of doing is is via a netlink socket... The modern and recommended way is to use BPF links...
The ip
tool uses netlink to attach XDP programs. This method doesn't support XDP metadata kfuncs.
The bpftool
uses the now-recommended BPF link method to attach BPF programs. To support XDP metadata kfuncs, one can attach an XDP program as follows:
$ sudo bpftool prog load dummy.bpf.o /sys/fs/bpf/dummy type xdp xdpmeta_dev eth0
$ sudo bpftool net attach xdp name dummy dev eth0
Upvotes: 3