Skip to content

Commit 85cc12f

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf_cubic'
Martin KaFai Lau says: ==================== This set adds bpf_cubic.c example. It was separated from the earlier BPF STRUCT_OPS series. Some highlights since the last post: 1. It is based on EricD recent fixes to the kernel tcp_cubic. [1] 2. The bpf jiffies reading helper is inlined by the verifier. Different from the earlier version, it only reads jiffies alone and does not do usecs/jiffies conversion. 3. The bpf .kconfig map is used to read CONFIG_HZ. [1]: https://patchwork.ozlabs.org/cover/1215066/ v3: - Remove __weak from CONFIG_HZ in patch 3. (Andrii) v2: - Move inlining to fixup_bpf_calls() in patch 1. (Daniel) - It is inlined for 64 BITS_PER_LONG and jit_requested as the map_gen_lookup(). Other cases could be considered together with map_gen_lookup() if needed. - Use usec resolution in bictcp_update() calculation in patch 3. usecs_to_jiffies() is then removed(). (Eric) ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 1b2fd38 + 6de4a9c commit 85cc12f

10 files changed

Lines changed: 641 additions & 2 deletions

File tree

‎include/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;
14141414
extern const struct bpf_func_proto bpf_strtol_proto;
14151415
extern const struct bpf_func_proto bpf_strtoul_proto;
14161416
extern const struct bpf_func_proto bpf_tcp_sock_proto;
1417+
extern const struct bpf_func_proto bpf_jiffies64_proto;
14171418

14181419
/* Shared helpers among cBPF and eBPF. */
14191420
void bpf_user_rnd_init_once(void);

‎include/uapi/linux/bpf.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2886,6 +2886,12 @@ union bpf_attr {
28862886
* **-EPERM** if no permission to send the *sig*.
28872887
*
28882888
* **-EAGAIN** if bpf program can try again.
2889+
*
2890+
* u64 bpf_jiffies64(void)
2891+
* Description
2892+
* Obtain the 64bit jiffies
2893+
* Return
2894+
* The 64 bit jiffies
28892895
*/
28902896
#define __BPF_FUNC_MAPPER(FN) \
28912897
FN(unspec), \
@@ -3005,7 +3011,8 @@ union bpf_attr {
30053011
FN(probe_read_user_str), \
30063012
FN(probe_read_kernel_str), \
30073013
FN(tcp_send_ack), \
3008-
FN(send_signal_thread),
3014+
FN(send_signal_thread), \
3015+
FN(jiffies64),
30093016

30103017
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
30113018
* function eBPF program intends to call

‎kernel/bpf/core.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,7 @@ const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
21372137
const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
21382138
const struct bpf_func_proto bpf_spin_lock_proto __weak;
21392139
const struct bpf_func_proto bpf_spin_unlock_proto __weak;
2140+
const struct bpf_func_proto bpf_jiffies64_proto __weak;
21402141

21412142
const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
21422143
const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;

‎kernel/bpf/helpers.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/uidgid.h>
1212
#include <linux/filter.h>
1313
#include <linux/ctype.h>
14+
#include <linux/jiffies.h>
1415

1516
#include "../../lib/kstrtox.h"
1617

@@ -312,6 +313,17 @@ void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
312313
preempt_enable();
313314
}
314315

316+
BPF_CALL_0(bpf_jiffies64)
317+
{
318+
return get_jiffies_64();
319+
}
320+
321+
const struct bpf_func_proto bpf_jiffies64_proto = {
322+
.func = bpf_jiffies64,
323+
.gpl_only = false,
324+
.ret_type = RET_INTEGER,
325+
};
326+
315327
#ifdef CONFIG_CGROUPS
316328
BPF_CALL_0(bpf_get_current_cgroup_id)
317329
{

‎kernel/bpf/verifier.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9445,6 +9445,30 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
94459445
goto patch_call_imm;
94469446
}
94479447

9448+
if (prog->jit_requested && BITS_PER_LONG == 64 &&
9449+
insn->imm == BPF_FUNC_jiffies64) {
9450+
struct bpf_insn ld_jiffies_addr[2] = {
9451+
BPF_LD_IMM64(BPF_REG_0,
9452+
(unsigned long)&jiffies),
9453+
};
9454+
9455+
insn_buf[0] = ld_jiffies_addr[0];
9456+
insn_buf[1] = ld_jiffies_addr[1];
9457+
insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0,
9458+
BPF_REG_0, 0);
9459+
cnt = 3;
9460+
9461+
new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
9462+
cnt);
9463+
if (!new_prog)
9464+
return -ENOMEM;
9465+
9466+
delta += cnt - 1;
9467+
env->prog = prog = new_prog;
9468+
insn = new_prog->insnsi + i + delta;
9469+
continue;
9470+
}
9471+
94489472
patch_call_imm:
94499473
fn = env->ops->get_func_proto(insn->imm, env->prog);
94509474
/* all functions that have prototype and verifier allowed

‎net/core/filter.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5923,6 +5923,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
59235923
return &bpf_spin_unlock_proto;
59245924
case BPF_FUNC_trace_printk:
59255925
return bpf_get_trace_printk_proto();
5926+
case BPF_FUNC_jiffies64:
5927+
return &bpf_jiffies64_proto;
59265928
default:
59275929
return NULL;
59285930
}

‎tools/include/uapi/linux/bpf.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2886,6 +2886,12 @@ union bpf_attr {
28862886
* **-EPERM** if no permission to send the *sig*.
28872887
*
28882888
* **-EAGAIN** if bpf program can try again.
2889+
*
2890+
* u64 bpf_jiffies64(void)
2891+
* Description
2892+
* Obtain the 64bit jiffies
2893+
* Return
2894+
* The 64 bit jiffies
28892895
*/
28902896
#define __BPF_FUNC_MAPPER(FN) \
28912897
FN(unspec), \
@@ -3005,7 +3011,8 @@ union bpf_attr {
30053011
FN(probe_read_user_str), \
30063012
FN(probe_read_kernel_str), \
30073013
FN(tcp_send_ack), \
3008-
FN(send_signal_thread),
3014+
FN(send_signal_thread), \
3015+
FN(jiffies64),
30093016

30103017
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
30113018
* function eBPF program intends to call

‎tools/testing/selftests/bpf/bpf_tcp_helpers.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,28 @@
66
#include <linux/types.h>
77
#include <bpf/bpf_helpers.h>
88
#include <bpf/bpf_core_read.h>
9+
#include "bpf_trace_helpers.h"
10+
11+
#define BPF_STRUCT_OPS(name, args...) \
12+
SEC("struct_ops/"#name) \
13+
BPF_PROG(name, args)
14+
15+
#define tcp_jiffies32 ((__u32)bpf_jiffies64())
916

1017
struct sock_common {
1118
unsigned char skc_state;
1219
} __attribute__((preserve_access_index));
1320

21+
enum sk_pacing {
22+
SK_PACING_NONE = 0,
23+
SK_PACING_NEEDED = 1,
24+
SK_PACING_FQ = 2,
25+
};
26+
1427
struct sock {
1528
struct sock_common __sk_common;
29+
unsigned long sk_pacing_rate;
30+
__u32 sk_pacing_status; /* see enum sk_pacing */
1631
} __attribute__((preserve_access_index));
1732

1833
struct inet_sock {
@@ -54,6 +69,7 @@ struct tcp_sock {
5469
__u32 max_packets_out;
5570
__u32 lsndtime;
5671
__u32 prior_cwnd;
72+
__u64 tcp_mstamp; /* most recent packet received/sent */
5773
} __attribute__((preserve_access_index));
5874

5975
static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk)

‎tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/err.h>
55
#include <test_progs.h>
66
#include "bpf_dctcp.skel.h"
7+
#include "bpf_cubic.skel.h"
78

89
#define min(a, b) ((a) < (b) ? (a) : (b))
910

@@ -158,6 +159,28 @@ static void do_test(const char *tcp_ca)
158159
close(fd);
159160
}
160161

162+
static void test_cubic(void)
163+
{
164+
struct bpf_cubic *cubic_skel;
165+
struct bpf_link *link;
166+
167+
cubic_skel = bpf_cubic__open_and_load();
168+
if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n"))
169+
return;
170+
171+
link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
172+
if (CHECK(IS_ERR(link), "bpf_map__attach_struct_ops", "err:%ld\n",
173+
PTR_ERR(link))) {
174+
bpf_cubic__destroy(cubic_skel);
175+
return;
176+
}
177+
178+
do_test("bpf_cubic");
179+
180+
bpf_link__destroy(link);
181+
bpf_cubic__destroy(cubic_skel);
182+
}
183+
161184
static void test_dctcp(void)
162185
{
163186
struct bpf_dctcp *dctcp_skel;
@@ -184,4 +207,6 @@ void test_bpf_tcp_ca(void)
184207
{
185208
if (test__start_subtest("dctcp"))
186209
test_dctcp();
210+
if (test__start_subtest("cubic"))
211+
test_cubic();
187212
}

0 commit comments

Comments
 (0)