bpf: add helper for retrieving current numa node id
authorDaniel Borkmann <daniel@iogearbox.net>
Fri, 21 Oct 2016 10:46:33 +0000 (12:46 +0200)
committerDavid S. Miller <davem@davemloft.net>
Sat, 22 Oct 2016 21:05:52 +0000 (17:05 -0400)
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 <edumazet@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/bpf/core.c
kernel/bpf/helpers.c
kernel/trace/bpf_trace.c
net/core/filter.c

index c201017b573079961e4ca156ca7ebccda225b0ca..edcd96ded8aafaaef9d37dcdb39df124cb1eb6c7 100644 (file)
@@ -319,6 +319,7 @@ extern const struct bpf_func_proto bpf_map_delete_elem_proto;
 
 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
+extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
 extern const struct bpf_func_proto bpf_tail_call_proto;
 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
index f09c70b97ecad2adeb627a1b9d8b8431cee7c695..374ef582ae18d6c46c3ae07005ee74ec0bc813de 100644 (file)
@@ -426,6 +426,12 @@ enum bpf_func_id {
         */
        BPF_FUNC_set_hash_invalid,
 
+       /**
+        * bpf_get_numa_node_id()
+        * Returns the id of the current NUMA node.
+        */
+       BPF_FUNC_get_numa_node_id,
+
        __BPF_FUNC_MAX_ID,
 };
 
index aa6d981541067d20f7dbe147ccd92532bfd97ee6..82a04143368ef0a29fe6c9d8a3321d098d95c758 100644 (file)
@@ -1043,6 +1043,7 @@ const struct bpf_func_proto bpf_map_delete_elem_proto __weak;
 
 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
+const struct bpf_func_proto bpf_get_numa_node_id_proto __weak;
 const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
 
 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
index 39918402e6e960b8d40c502d58fa81782b14a9ea..045cbe673356f90c365928475574bb108a057e0a 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/rcupdate.h>
 #include <linux/random.h>
 #include <linux/smp.h>
+#include <linux/topology.h>
 #include <linux/ktime.h>
 #include <linux/sched.h>
 #include <linux/uidgid.h>
@@ -92,6 +93,17 @@ const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
        .ret_type       = RET_INTEGER,
 };
 
+BPF_CALL_0(bpf_get_numa_node_id)
+{
+       return numa_node_id();
+}
+
+const struct bpf_func_proto bpf_get_numa_node_id_proto = {
+       .func           = bpf_get_numa_node_id,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+};
+
 BPF_CALL_0(bpf_ktime_get_ns)
 {
        /* NMI safe access to clock monotonic */
index 5dcb99281259e40e2b9b6e1b31f6b9d11d15c48b..fa77311dadb23b35d78cf9060daf1dc0cc72b855 100644 (file)
@@ -422,6 +422,8 @@ static const struct bpf_func_proto *tracing_func_proto(enum bpf_func_id func_id)
                return bpf_get_trace_printk_proto();
        case BPF_FUNC_get_smp_processor_id:
                return &bpf_get_smp_processor_id_proto;
+       case BPF_FUNC_get_numa_node_id:
+               return &bpf_get_numa_node_id_proto;
        case BPF_FUNC_perf_event_read:
                return &bpf_perf_event_read_proto;
        case BPF_FUNC_probe_write_user:
index 00351cdf7d0c3f5fe1657e02b96b7a90891e0385..cd9e2ba66b0e0bc32ba8b3f6fba701aa307ce669 100644 (file)
@@ -2492,6 +2492,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
                return &bpf_get_prandom_u32_proto;
        case BPF_FUNC_get_smp_processor_id:
                return &bpf_get_raw_smp_processor_id_proto;
+       case BPF_FUNC_get_numa_node_id:
+               return &bpf_get_numa_node_id_proto;
        case BPF_FUNC_tail_call:
                return &bpf_tail_call_proto;
        case BPF_FUNC_ktime_get_ns: