Skip to content

Commit ded092c

Browse files
borkmanndavem330
authored andcommitted
bpf: add bpf_set_hash helper for tc progs
Allow for tc BPF programs to set a skb->hash, apart from clearing and triggering a recalc that we have right now. It allows for BPF to implement a custom hashing routine for skb_get_hash(). Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 966789f commit ded092c

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Diff for: include/uapi/linux/bpf.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ union bpf_attr {
513513
* Get the owner uid of the socket stored inside sk_buff.
514514
* @skb: pointer to skb
515515
* Return: uid of the socket owner on success or overflowuid if failed.
516+
*
517+
* u32 bpf_set_hash(skb, hash)
518+
* Set full skb->hash.
519+
* @skb: pointer to skb
520+
* @hash: hash to set
516521
*/
517522
#define __BPF_FUNC_MAPPER(FN) \
518523
FN(unspec), \
@@ -562,7 +567,8 @@ union bpf_attr {
562567
FN(xdp_adjust_head), \
563568
FN(probe_read_str), \
564569
FN(get_socket_cookie), \
565-
FN(get_socket_uid),
570+
FN(get_socket_uid), \
571+
FN(set_hash),
566572

567573
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
568574
* function eBPF program intends to call

Diff for: net/core/filter.c

+20
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,24 @@ static const struct bpf_func_proto bpf_set_hash_invalid_proto = {
18741874
.arg1_type = ARG_PTR_TO_CTX,
18751875
};
18761876

1877+
BPF_CALL_2(bpf_set_hash, struct sk_buff *, skb, u32, hash)
1878+
{
1879+
/* Set user specified hash as L4(+), so that it gets returned
1880+
* on skb_get_hash() call unless BPF prog later on triggers a
1881+
* skb_clear_hash().
1882+
*/
1883+
__skb_set_sw_hash(skb, hash, true);
1884+
return 0;
1885+
}
1886+
1887+
static const struct bpf_func_proto bpf_set_hash_proto = {
1888+
.func = bpf_set_hash,
1889+
.gpl_only = false,
1890+
.ret_type = RET_INTEGER,
1891+
.arg1_type = ARG_PTR_TO_CTX,
1892+
.arg2_type = ARG_ANYTHING,
1893+
};
1894+
18771895
BPF_CALL_3(bpf_skb_vlan_push, struct sk_buff *, skb, __be16, vlan_proto,
18781896
u16, vlan_tci)
18791897
{
@@ -2744,6 +2762,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id)
27442762
return &bpf_get_hash_recalc_proto;
27452763
case BPF_FUNC_set_hash_invalid:
27462764
return &bpf_set_hash_invalid_proto;
2765+
case BPF_FUNC_set_hash:
2766+
return &bpf_set_hash_proto;
27472767
case BPF_FUNC_perf_event_output:
27482768
return &bpf_skb_event_output_proto;
27492769
case BPF_FUNC_get_smp_processor_id:

Diff for: tools/include/uapi/linux/bpf.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ union bpf_attr {
513513
* Get the owner uid of the socket stored inside sk_buff.
514514
* @skb: pointer to skb
515515
* Return: uid of the socket owner on success or overflowuid if failed.
516+
*
517+
* u32 bpf_set_hash(skb, hash)
518+
* Set full skb->hash.
519+
* @skb: pointer to skb
520+
* @hash: hash to set
516521
*/
517522
#define __BPF_FUNC_MAPPER(FN) \
518523
FN(unspec), \
@@ -562,7 +567,8 @@ union bpf_attr {
562567
FN(xdp_adjust_head), \
563568
FN(probe_read_str), \
564569
FN(get_socket_cookie), \
565-
FN(get_socket_uid),
570+
FN(get_socket_uid), \
571+
FN(set_hash),
566572

567573
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
568574
* function eBPF program intends to call

0 commit comments

Comments
 (0)