Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ebpf: allow bpf_ktime_get_ns_proto also for networking

As this is already exported from tracing side via commit d9847d310ab4
("tracing: Allow BPF programs to call bpf_ktime_get_ns()"), we might
as well want to move it to the core, so also networking users can make
use of it, e.g. to measure diffs for certain flows from ingress/egress.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Cc: Alexei Starovoitov <ast@plumgrid.com>
Cc: Ingo Molnar <mingo@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Daniel Borkmann and committed by
David S. Miller
17ca8cbf a24c85ab

+17 -12
+1
include/linux/bpf.h
··· 186 186 extern const struct bpf_func_proto bpf_get_prandom_u32_proto; 187 187 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto; 188 188 extern const struct bpf_func_proto bpf_tail_call_proto; 189 + extern const struct bpf_func_proto bpf_ktime_get_ns_proto; 189 190 190 191 #endif /* _LINUX_BPF_H */
+1
kernel/bpf/core.c
··· 734 734 735 735 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak; 736 736 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak; 737 + const struct bpf_func_proto bpf_ktime_get_ns_proto __weak; 737 738 738 739 /* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call 739 740 * skb_copy_bits(), so provide a weak definition of it for NET-less config.
+13
kernel/bpf/helpers.c
··· 13 13 #include <linux/rcupdate.h> 14 14 #include <linux/random.h> 15 15 #include <linux/smp.h> 16 + #include <linux/ktime.h> 16 17 17 18 /* If kernel subsystem is allowing eBPF programs to call this function, 18 19 * inside its own verifier_ops->get_func_proto() callback it should return ··· 110 109 const struct bpf_func_proto bpf_get_smp_processor_id_proto = { 111 110 .func = bpf_get_smp_processor_id, 112 111 .gpl_only = false, 112 + .ret_type = RET_INTEGER, 113 + }; 114 + 115 + static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) 116 + { 117 + /* NMI safe access to clock monotonic */ 118 + return ktime_get_mono_fast_ns(); 119 + } 120 + 121 + const struct bpf_func_proto bpf_ktime_get_ns_proto = { 122 + .func = bpf_ktime_get_ns, 123 + .gpl_only = true, 113 124 .ret_type = RET_INTEGER, 114 125 };
-12
kernel/trace/bpf_trace.c
··· 79 79 .arg3_type = ARG_ANYTHING, 80 80 }; 81 81 82 - static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) 83 - { 84 - /* NMI safe access to clock monotonic */ 85 - return ktime_get_mono_fast_ns(); 86 - } 87 - 88 - static const struct bpf_func_proto bpf_ktime_get_ns_proto = { 89 - .func = bpf_ktime_get_ns, 90 - .gpl_only = true, 91 - .ret_type = RET_INTEGER, 92 - }; 93 - 94 82 /* 95 83 * limited trace_printk() 96 84 * only %d %u %x %ld %lu %lx %lld %llu %llx %p conversion specifiers allowed
+2
net/core/filter.c
··· 1423 1423 return &bpf_get_smp_processor_id_proto; 1424 1424 case BPF_FUNC_tail_call: 1425 1425 return &bpf_tail_call_proto; 1426 + case BPF_FUNC_ktime_get_ns: 1427 + return &bpf_ktime_get_ns_proto; 1426 1428 default: 1427 1429 return NULL; 1428 1430 }