History

ebpf: add skb->hash to offset map for usage in {cls, act}_bpf or filters

Add skb->hash to the __sk_buff offset map, so it can be accessed from an eBPF program. We currently already do this for classic BPF filters, but not yet on eBPF, it might be useful as a demuxer in combination with helpers like bpf_clone_redirect(), toy example: __section("cls-lb") int ingress_main(struct __sk_buff *skb) { unsigned int which = 3 + (skb->hash & 7); /* bpf_skb_store_bytes(skb, ...); */ /* bpf_l{3,4}_csum_replace(skb, ...); */ bpf_clone_redirect(skb, which, 0); return -1; } I was thinking whether to add skb_get_hash(), but then concluded the raw skb->hash seems fine in this case: we can directly access the hash w/o extra eBPF helper function call, it's filled out by many NICs on ingress, and in case the entropy level would not be sufficient, people can still implement their own specific sw fallback hash mix anyway.

Daniel Borkmann committed 11 years ago

Commit ba7591d1 parent c46309c2 files · +8 −0Browse files

Unified · Split

2 files
  1. include/uapi/linux/bpf.h +1 −0
  2. net/core/filter.c +7 −0
include/uapi/linux/bpf.h+1−0
@@ -290,6 +290,7 @@ struct __sk_buff { · 289 unmodified lines
290290 __u32 ifindex;
291291 __u32 tc_index;
292292 __u32 cb[5];
293+ __u32 hash;
293294 };
294295
295296 struct bpf_tunnel_key {
net/core/filter.c+7−0
@@ -1711,6 +1711,13 @@ static u32 bpf_net_convert_ctx_access(enum bpf_access_type type, int dst_reg, · 1710 unmodified lines
17111711 offsetof(struct net_device, ifindex));
17121712 break;
17131713
1714+ case offsetof(struct __sk_buff, hash):
1715+ BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, hash) != 4);
1716+
1717+ *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1718+ offsetof(struct sk_buff, hash));
1719+ break;
1720+
17141721 case offsetof(struct __sk_buff, mark):
17151722 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
17161723