Skip to content

Commit 2d0e30c

Browse files
borkmanndavem330
authored andcommitted
bpf: add helper for retrieving current numa node id
Use case is mainly for soreuseport to select sockets for the local numa node, but since generic, lets also add this for other networking and tracing program types. Suggested-by: Eric Dumazet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Alexei Starovoitov <[email protected]> Acked-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent a10b91b commit 2d0e30c

File tree

6 files changed

+24
-0
lines changed

6 files changed

+24
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ extern const struct bpf_func_proto bpf_map_delete_elem_proto;
319319

320320
extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
321321
extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
322+
extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
322323
extern const struct bpf_func_proto bpf_tail_call_proto;
323324
extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
324325
extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;

include/uapi/linux/bpf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,12 @@ enum bpf_func_id {
426426
*/
427427
BPF_FUNC_set_hash_invalid,
428428

429+
/**
430+
* bpf_get_numa_node_id()
431+
* Returns the id of the current NUMA node.
432+
*/
433+
BPF_FUNC_get_numa_node_id,
434+
429435
__BPF_FUNC_MAX_ID,
430436
};
431437

kernel/bpf/core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,7 @@ const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
10431043

10441044
const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
10451045
const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
1046+
const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
10461047
const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
10471048

10481049
const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;

kernel/bpf/helpers.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/rcupdate.h>
1414
#include <linux/random.h>
1515
#include <linux/smp.h>
16+
#include <linux/topology.h>
1617
#include <linux/ktime.h>
1718
#include <linux/sched.h>
1819
#include <linux/uidgid.h>
@@ -92,6 +93,17 @@ const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
9293
.ret_type = RET_INTEGER,
9394
};
9495

96+
BPF_CALL_0(bpf_get_numa_node_id)
97+
{
98+
return numa_node_id();
99+
}
100+
101+
const struct bpf_func_proto bpf_get_numa_node_id_proto = {
102+
.func = bpf_get_numa_node_id,
103+
.gpl_only = false,
104+
.ret_type = RET_INTEGER,
105+
};
106+
95107
BPF_CALL_0(bpf_ktime_get_ns)
96108
{
97109
/* NMI safe access to clock monotonic */

kernel/trace/bpf_trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
422422
return bpf_get_trace_printk_proto();
423423
case BPF_FUNC_get_smp_processor_id:
424424
return &bpf_get_smp_processor_id_proto;
425+
case BPF_FUNC_get_numa_node_id:
426+
return &bpf_get_numa_node_id_proto;
425427
case BPF_FUNC_perf_event_read:
426428
return &bpf_perf_event_read_proto;
427429
case BPF_FUNC_probe_write_user:

net/core/filter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
24922492
return &bpf_get_prandom_u32_proto;
24932493
case BPF_FUNC_get_smp_processor_id:
24942494
return &bpf_get_raw_smp_processor_id_proto;
2495+
case BPF_FUNC_get_numa_node_id:
2496+
return &bpf_get_numa_node_id_proto;
24952497
case BPF_FUNC_tail_call:
24962498
return &bpf_tail_call_proto;
24972499
case BPF_FUNC_ktime_get_ns:

0 commit comments

Comments
 (0)