Skip to content

Commit 03e69b5

Browse files
borkmanndavem330
authored andcommitted
ebpf: add prandom helper for packet sampling
This work is similar to commit 4cd3675 ("filter: added BPF random opcode") and adds a possibility for packet sampling in eBPF. Currently, this is only possible in classic BPF and useful to combine sampling with f.e. packet sockets, possible also with tc. Example function proto-type looks like: u32 (*prandom_u32)(void) = (void *)BPF_FUNC_get_prandom_u32; Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1202882 commit 03e69b5

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

include/linux/bpf.h

+2
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,6 @@ extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
154154
extern const struct bpf_func_proto bpf_map_update_elem_proto;
155155
extern const struct bpf_func_proto bpf_map_delete_elem_proto;
156156

157+
extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
158+
157159
#endif /* _LINUX_BPF_H */

include/uapi/linux/bpf.h

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ enum bpf_func_id {
165165
BPF_FUNC_map_lookup_elem, /* void *map_lookup_elem(&map, &key) */
166166
BPF_FUNC_map_update_elem, /* int map_update_elem(&map, &key, &value, flags) */
167167
BPF_FUNC_map_delete_elem, /* int map_delete_elem(&map, &key) */
168+
BPF_FUNC_get_prandom_u32, /* u32 prandom_u32(void) */
168169
__BPF_FUNC_MAX_ID,
169170
};
170171

kernel/bpf/core.c

+2
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ const struct bpf_func_proto bpf_map_lookup_elem_proto __weak;
661661
const struct bpf_func_proto bpf_map_update_elem_proto __weak;
662662
const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
663663

664+
const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
665+
664666
/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
665667
* skb_copy_bits(), so provide a weak definition of it for NET-less config.
666668
*/

kernel/bpf/helpers.c

+12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
#include <linux/bpf.h>
1313
#include <linux/rcupdate.h>
14+
#include <linux/random.h>
1415

1516
/* If kernel subsystem is allowing eBPF programs to call this function,
1617
* inside its own verifier_ops->get_func_proto() callback it should return
@@ -87,3 +88,14 @@ const struct bpf_func_proto bpf_map_delete_elem_proto = {
8788
.arg1_type = ARG_CONST_MAP_PTR,
8889
.arg2_type = ARG_PTR_TO_MAP_KEY,
8990
};
91+
92+
static u64 bpf_get_prandom_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
93+
{
94+
return prandom_u32();
95+
}
96+
97+
const struct bpf_func_proto bpf_get_prandom_u32_proto = {
98+
.func = bpf_get_prandom_u32,
99+
.gpl_only = false,
100+
.ret_type = RET_INTEGER,
101+
};

net/core/filter.c

+2
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
11391139
return &bpf_map_update_elem_proto;
11401140
case BPF_FUNC_map_delete_elem:
11411141
return &bpf_map_delete_elem_proto;
1142+
case BPF_FUNC_get_prandom_u32:
1143+
return &bpf_get_prandom_u32_proto;
11421144
default:
11431145
return NULL;
11441146
}

0 commit comments

Comments
 (0)