Skip to content

Commit 17ca8cb

Browse files
borkmanndavem330
authored andcommitted
ebpf: allow bpf_ktime_get_ns_proto also for networking
As this is already exported from tracing side via commit d9847d3 ("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>
1 parent a24c85a commit 17ca8cb

5 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,6 @@ extern const struct bpf_func_proto bpf_map_delete_elem_proto;
186186
extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
187187
extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
188188
extern const struct bpf_func_proto bpf_tail_call_proto;
189+
extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
189190

190191
#endif /* _LINUX_BPF_H */

‎kernel/bpf/core.c‎

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

735735
const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
736736
const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
737+
const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
737738

738739
/* To execute LD_ABS/LD_IND instructions __bpf_prog_run() may call
739740
* skb_copy_bits(), so provide a weak definition of it for NET-less config.

‎kernel/bpf/helpers.c‎

Lines changed: 13 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/ktime.h>
1617

1718
/* If kernel subsystem is allowing eBPF programs to call this function,
1819
* inside its own verifier_ops->get_func_proto() callback it should return
@@ -111,3 +112,15 @@ const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
111112
.gpl_only = false,
112113
.ret_type = RET_INTEGER,
113114
};
115+
116+
static u64 bpf_ktime_get_ns(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
117+
{
118+
/* NMI safe access to clock monotonic */
119+
return ktime_get_mono_fast_ns();
120+
}
121+
122+
const struct bpf_func_proto bpf_ktime_get_ns_proto = {
123+
.func = bpf_ktime_get_ns,
124+
.gpl_only = true,
125+
.ret_type = RET_INTEGER,
126+
};

‎kernel/trace/bpf_trace.c‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,6 @@ static const struct bpf_func_proto bpf_probe_read_proto = {
7979
.arg3_type = ARG_ANYTHING,
8080
};
8181

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-
9482
/*
9583
* limited trace_printk()
9684
* only %d %u %x %ld %lu %lx %lld %llu %llx %p conversion specifiers allowed

‎net/core/filter.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
14231423
return &bpf_get_smp_processor_id_proto;
14241424
case BPF_FUNC_tail_call:
14251425
return &bpf_tail_call_proto;
1426+
case BPF_FUNC_ktime_get_ns:
1427+
return &bpf_ktime_get_ns_proto;
14261428
default:
14271429
return NULL;
14281430
}

0 commit comments

Comments
 (0)