Skip to content

Commit 13c5c24

Browse files
borkmanndavem330
authored andcommitted
bpf: add bpf_get_hash_recalc helper
If skb_clear_hash() was invoked due to mangling of relevant headers and BPF program needs skb->hash later on, we can add a helper to trigger hash recalculation via bpf_get_hash_recalc(). The helper will return the newly retrieved hash directly, but later access can also be done via skb context again through skb->hash directly (inline) without needing to call the helper once more. Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6fd980a commit 13c5c24

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,15 @@ enum bpf_func_id {
348348
* < 0 error
349349
*/
350350
BPF_FUNC_skb_in_cgroup,
351+
352+
/**
353+
* bpf_get_hash_recalc(skb)
354+
* Retrieve and possibly recalculate skb->hash.
355+
* @skb: pointer to skb
356+
* Return: hash
357+
*/
358+
BPF_FUNC_get_hash_recalc,
359+
351360
__BPF_FUNC_MAX_ID,
352361
};
353362

net/core/filter.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,23 @@ static const struct bpf_func_proto bpf_get_route_realm_proto = {
17291729
.arg1_type = ARG_PTR_TO_CTX,
17301730
};
17311731

1732+
static u64 bpf_get_hash_recalc(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
1733+
{
1734+
/* If skb_clear_hash() was called due to mangling, we can
1735+
* trigger SW recalculation here. Later access to hash
1736+
* can then use the inline skb->hash via context directly
1737+
* instead of calling this helper again.
1738+
*/
1739+
return skb_get_hash((struct sk_buff *) (unsigned long) r1);
1740+
}
1741+
1742+
static const struct bpf_func_proto bpf_get_hash_recalc_proto = {
1743+
.func = bpf_get_hash_recalc,
1744+
.gpl_only = false,
1745+
.ret_type = RET_INTEGER,
1746+
.arg1_type = ARG_PTR_TO_CTX,
1747+
};
1748+
17321749
static u64 bpf_skb_vlan_push(u64 r1, u64 r2, u64 vlan_tci, u64 r4, u64 r5)
17331750
{
17341751
struct sk_buff *skb = (struct sk_buff *) (long) r1;
@@ -2337,6 +2354,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)
23372354
return &bpf_redirect_proto;
23382355
case BPF_FUNC_get_route_realm:
23392356
return &bpf_get_route_realm_proto;
2357+
case BPF_FUNC_get_hash_recalc:
2358+
return &bpf_get_hash_recalc_proto;
23402359
case BPF_FUNC_perf_event_output:
23412360
return bpf_get_event_output_proto();
23422361
case BPF_FUNC_get_smp_processor_id:

0 commit comments

Comments
 (0)