Commit 7d3851a3 authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Alexei Starovoitov
Browse files

selftests/bpf: Sanitize the SEC and inline usages in the bpf-tcp-cc tests



It is needed to remove the BPF_STRUCT_OPS usages from the tcp-cc tests
because it is defined in bpf_tcp_helpers.h which is going to be retired.
While at it, this patch consolidates all tcp-cc struct_ops programs to
use the SEC("struct_ops") + BPF_PROG().

It also removes the unnecessary __always_inline usages from the
tcp-cc tests.

Signed-off-by: default avatarMartin KaFai Lau <martin.lau@kernel.org>
Link: https://lore.kernel.org/r/20240509175026.3423614-5-martin.lau@linux.dev


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent cc5b18ce
Loading
Loading
Loading
Loading
+15 −13
Original line number Diff line number Diff line
@@ -17,10 +17,6 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#define BPF_STRUCT_OPS(name, args...) \
SEC("struct_ops/"#name) \
BPF_PROG(name, args)

#define USEC_PER_SEC 1000000UL
#define TCP_PACING_SS_RATIO (200)
#define TCP_PACING_CA_RATIO (120)
@@ -114,17 +110,20 @@ static bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
	return flag & FLAG_DATA_ACKED;
}

void BPF_STRUCT_OPS(bpf_cubic_init, struct sock *sk)
SEC("struct_ops")
void BPF_PROG(bpf_cubic_init, struct sock *sk)
{
	cubictcp_init(sk);
}

void BPF_STRUCT_OPS(bpf_cubic_cwnd_event, struct sock *sk, enum tcp_ca_event event)
SEC("struct_ops")
void BPF_PROG(bpf_cubic_cwnd_event, struct sock *sk, enum tcp_ca_event event)
{
	cubictcp_cwnd_event(sk, event);
}

void BPF_STRUCT_OPS(bpf_cubic_cong_control, struct sock *sk, __u32 ack, int flag,
SEC("struct_ops")
void BPF_PROG(bpf_cubic_cong_control, struct sock *sk, __u32 ack, int flag,
	      const struct rate_sample *rs)
{
	struct tcp_sock *tp = tcp_sk(sk);
@@ -151,23 +150,26 @@ void BPF_STRUCT_OPS(bpf_cubic_cong_control, struct sock *sk, __u32 ack, int flag
	tcp_update_pacing_rate(sk);
}

__u32 BPF_STRUCT_OPS(bpf_cubic_recalc_ssthresh, struct sock *sk)
SEC("struct_ops")
__u32 BPF_PROG(bpf_cubic_recalc_ssthresh, struct sock *sk)
{
	return cubictcp_recalc_ssthresh(sk);
}

void BPF_STRUCT_OPS(bpf_cubic_state, struct sock *sk, __u8 new_state)
SEC("struct_ops")
void BPF_PROG(bpf_cubic_state, struct sock *sk, __u8 new_state)
{
	cubictcp_state(sk, new_state);
}

void BPF_STRUCT_OPS(bpf_cubic_acked, struct sock *sk,
		const struct ack_sample *sample)
SEC("struct_ops")
void BPF_PROG(bpf_cubic_acked, struct sock *sk, const struct ack_sample *sample)
{
	cubictcp_acked(sk, sample);
}

__u32 BPF_STRUCT_OPS(bpf_cubic_undo_cwnd, struct sock *sk)
SEC("struct_ops")
__u32 BPF_PROG(bpf_cubic_undo_cwnd, struct sock *sk)
{
	return tcp_reno_undo_cwnd(sk);
}
+21 −21
Original line number Diff line number Diff line
@@ -91,7 +91,7 @@ struct bictcp {
	__u32	curr_rtt;	/* the minimum rtt of current round */
};

static inline void bictcp_reset(struct bictcp *ca)
static void bictcp_reset(struct bictcp *ca)
{
	ca->cnt = 0;
	ca->last_max_cwnd = 0;
@@ -112,7 +112,7 @@ extern unsigned long CONFIG_HZ __kconfig;
#define USEC_PER_SEC	1000000UL
#define USEC_PER_JIFFY	(USEC_PER_SEC / HZ)

static __always_inline __u64 div64_u64(__u64 dividend, __u64 divisor)
static __u64 div64_u64(__u64 dividend, __u64 divisor)
{
	return dividend / divisor;
}
@@ -120,7 +120,7 @@ static __always_inline __u64 div64_u64(__u64 dividend, __u64 divisor)
#define div64_ul div64_u64

#define BITS_PER_U64 (sizeof(__u64) * 8)
static __always_inline int fls64(__u64 x)
static int fls64(__u64 x)
{
	int num = BITS_PER_U64 - 1;

@@ -153,12 +153,12 @@ static __always_inline int fls64(__u64 x)
	return num + 1;
}

static __always_inline __u32 bictcp_clock_us(const struct sock *sk)
static __u32 bictcp_clock_us(const struct sock *sk)
{
	return tcp_sk(sk)->tcp_mstamp;
}

static __always_inline void bictcp_hystart_reset(struct sock *sk)
static void bictcp_hystart_reset(struct sock *sk)
{
	struct tcp_sock *tp = tcp_sk(sk);
	struct bictcp *ca = inet_csk_ca(sk);
@@ -169,8 +169,7 @@ static __always_inline void bictcp_hystart_reset(struct sock *sk)
	ca->sample_cnt = 0;
}

/* "struct_ops/" prefix is a requirement */
SEC("struct_ops/bpf_cubic_init")
SEC("struct_ops")
void BPF_PROG(bpf_cubic_init, struct sock *sk)
{
	struct bictcp *ca = inet_csk_ca(sk);
@@ -184,8 +183,7 @@ void BPF_PROG(bpf_cubic_init, struct sock *sk)
		tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
}

/* "struct_ops" prefix is a requirement */
SEC("struct_ops/bpf_cubic_cwnd_event")
SEC("struct_ops")
void BPF_PROG(bpf_cubic_cwnd_event, struct sock *sk, enum tcp_ca_event event)
{
	if (event == CA_EVENT_TX_START) {
@@ -230,7 +228,7 @@ static const __u8 v[] = {
 * Newton-Raphson iteration.
 * Avg err ~= 0.195%
 */
static __always_inline __u32 cubic_root(__u64 a)
static __u32 cubic_root(__u64 a)
{
	__u32 x, b, shift;

@@ -263,8 +261,7 @@ static __always_inline __u32 cubic_root(__u64 a)
/*
 * Compute congestion window to use.
 */
static __always_inline void bictcp_update(struct bictcp *ca, __u32 cwnd,
					  __u32 acked)
static void bictcp_update(struct bictcp *ca, __u32 cwnd, __u32 acked)
{
	__u32 delta, bic_target, max_cnt;
	__u64 offs, t;
@@ -377,8 +374,8 @@ static __always_inline void bictcp_update(struct bictcp *ca, __u32 cwnd,
	ca->cnt = max(ca->cnt, 2U);
}

/* Or simply use the BPF_STRUCT_OPS to avoid the SEC boiler plate. */
void BPF_STRUCT_OPS(bpf_cubic_cong_avoid, struct sock *sk, __u32 ack, __u32 acked)
SEC("struct_ops")
void BPF_PROG(bpf_cubic_cong_avoid, struct sock *sk, __u32 ack, __u32 acked)
{
	struct tcp_sock *tp = tcp_sk(sk);
	struct bictcp *ca = inet_csk_ca(sk);
@@ -397,7 +394,8 @@ void BPF_STRUCT_OPS(bpf_cubic_cong_avoid, struct sock *sk, __u32 ack, __u32 acke
	tcp_cong_avoid_ai(tp, ca->cnt, acked);
}

__u32 BPF_STRUCT_OPS(bpf_cubic_recalc_ssthresh, struct sock *sk)
SEC("struct_ops")
__u32 BPF_PROG(bpf_cubic_recalc_ssthresh, struct sock *sk)
{
	const struct tcp_sock *tp = tcp_sk(sk);
	struct bictcp *ca = inet_csk_ca(sk);
@@ -414,7 +412,8 @@ __u32 BPF_STRUCT_OPS(bpf_cubic_recalc_ssthresh, struct sock *sk)
	return max((tp->snd_cwnd * beta) / BICTCP_BETA_SCALE, 2U);
}

void BPF_STRUCT_OPS(bpf_cubic_state, struct sock *sk, __u8 new_state)
SEC("struct_ops")
void BPF_PROG(bpf_cubic_state, struct sock *sk, __u8 new_state)
{
	if (new_state == TCP_CA_Loss) {
		bictcp_reset(inet_csk_ca(sk));
@@ -433,7 +432,7 @@ void BPF_STRUCT_OPS(bpf_cubic_state, struct sock *sk, __u8 new_state)
 * We apply another 100% factor because @rate is doubled at this point.
 * We cap the cushion to 1ms.
 */
static __always_inline __u32 hystart_ack_delay(struct sock *sk)
static __u32 hystart_ack_delay(struct sock *sk)
{
	unsigned long rate;

@@ -444,7 +443,7 @@ static __always_inline __u32 hystart_ack_delay(struct sock *sk)
		   div64_ul((__u64)GSO_MAX_SIZE * 4 * USEC_PER_SEC, rate));
}

static __always_inline void hystart_update(struct sock *sk, __u32 delay)
static void hystart_update(struct sock *sk, __u32 delay)
{
	struct tcp_sock *tp = tcp_sk(sk);
	struct bictcp *ca = inet_csk_ca(sk);
@@ -492,8 +491,8 @@ static __always_inline void hystart_update(struct sock *sk, __u32 delay)

int bpf_cubic_acked_called = 0;

void BPF_STRUCT_OPS(bpf_cubic_acked, struct sock *sk,
		    const struct ack_sample *sample)
SEC("struct_ops")
void BPF_PROG(bpf_cubic_acked, struct sock *sk, const struct ack_sample *sample)
{
	const struct tcp_sock *tp = tcp_sk(sk);
	struct bictcp *ca = inet_csk_ca(sk);
@@ -524,7 +523,8 @@ void BPF_STRUCT_OPS(bpf_cubic_acked, struct sock *sk,

extern __u32 tcp_reno_undo_cwnd(struct sock *sk) __ksym;

__u32 BPF_STRUCT_OPS(bpf_cubic_undo_cwnd, struct sock *sk)
SEC("struct_ops")
__u32 BPF_PROG(bpf_cubic_undo_cwnd, struct sock *sk)
{
	return tcp_reno_undo_cwnd(sk);
}
+12 −14
Original line number Diff line number Diff line
@@ -48,8 +48,7 @@ struct dctcp {
static unsigned int dctcp_shift_g = 4; /* g = 1/2^4 */
static unsigned int dctcp_alpha_on_init = DCTCP_MAX_ALPHA;

static __always_inline void dctcp_reset(const struct tcp_sock *tp,
					struct dctcp *ca)
static void dctcp_reset(const struct tcp_sock *tp, struct dctcp *ca)
{
	ca->next_seq = tp->snd_nxt;

@@ -57,7 +56,7 @@ static __always_inline void dctcp_reset(const struct tcp_sock *tp,
	ca->old_delivered_ce = tp->delivered_ce;
}

SEC("struct_ops/dctcp_init")
SEC("struct_ops")
void BPF_PROG(dctcp_init, struct sock *sk)
{
	const struct tcp_sock *tp = tcp_sk(sk);
@@ -104,7 +103,7 @@ void BPF_PROG(dctcp_init, struct sock *sk)
	dctcp_reset(tp, ca);
}

SEC("struct_ops/dctcp_ssthresh")
SEC("struct_ops")
__u32 BPF_PROG(dctcp_ssthresh, struct sock *sk)
{
	struct dctcp *ca = inet_csk_ca(sk);
@@ -114,7 +113,7 @@ __u32 BPF_PROG(dctcp_ssthresh, struct sock *sk)
	return max(tp->snd_cwnd - ((tp->snd_cwnd * ca->dctcp_alpha) >> 11U), 2U);
}

SEC("struct_ops/dctcp_update_alpha")
SEC("struct_ops")
void BPF_PROG(dctcp_update_alpha, struct sock *sk, __u32 flags)
{
	const struct tcp_sock *tp = tcp_sk(sk);
@@ -144,7 +143,7 @@ void BPF_PROG(dctcp_update_alpha, struct sock *sk, __u32 flags)
	}
}

static __always_inline void dctcp_react_to_loss(struct sock *sk)
static void dctcp_react_to_loss(struct sock *sk)
{
	struct dctcp *ca = inet_csk_ca(sk);
	struct tcp_sock *tp = tcp_sk(sk);
@@ -153,7 +152,7 @@ static __always_inline void dctcp_react_to_loss(struct sock *sk)
	tp->snd_ssthresh = max(tp->snd_cwnd >> 1U, 2U);
}

SEC("struct_ops/dctcp_state")
SEC("struct_ops")
void BPF_PROG(dctcp_state, struct sock *sk, __u8 new_state)
{
	if (new_state == TCP_CA_Recovery &&
@@ -164,7 +163,7 @@ void BPF_PROG(dctcp_state, struct sock *sk, __u8 new_state)
	 */
}

static __always_inline void dctcp_ece_ack_cwr(struct sock *sk, __u32 ce_state)
static void dctcp_ece_ack_cwr(struct sock *sk, __u32 ce_state)
{
	struct tcp_sock *tp = tcp_sk(sk);

@@ -179,8 +178,7 @@ static __always_inline void dctcp_ece_ack_cwr(struct sock *sk, __u32 ce_state)
 * S:	0 <- last pkt was non-CE
 *	1 <- last pkt was CE
 */
static __always_inline
void dctcp_ece_ack_update(struct sock *sk, enum tcp_ca_event evt,
static void dctcp_ece_ack_update(struct sock *sk, enum tcp_ca_event evt,
				 __u32 *prior_rcv_nxt, __u32 *ce_state)
{
	__u32 new_ce_state = (evt == CA_EVENT_ECN_IS_CE) ? 1 : 0;
@@ -201,7 +199,7 @@ void dctcp_ece_ack_update(struct sock *sk, enum tcp_ca_event evt,
	dctcp_ece_ack_cwr(sk, new_ce_state);
}

SEC("struct_ops/dctcp_cwnd_event")
SEC("struct_ops")
void BPF_PROG(dctcp_cwnd_event, struct sock *sk, enum tcp_ca_event ev)
{
	struct dctcp *ca = inet_csk_ca(sk);
@@ -220,7 +218,7 @@ void BPF_PROG(dctcp_cwnd_event, struct sock *sk, enum tcp_ca_event ev)
	}
}

SEC("struct_ops/dctcp_cwnd_undo")
SEC("struct_ops")
__u32 BPF_PROG(dctcp_cwnd_undo, struct sock *sk)
{
	const struct dctcp *ca = inet_csk_ca(sk);
@@ -230,7 +228,7 @@ __u32 BPF_PROG(dctcp_cwnd_undo, struct sock *sk)

extern void tcp_reno_cong_avoid(struct sock *sk, __u32 ack, __u32 acked) __ksym;

SEC("struct_ops/dctcp_reno_cong_avoid")
SEC("struct_ops")
void BPF_PROG(dctcp_cong_avoid, struct sock *sk, __u32 ack, __u32 acked)
{
	tcp_reno_cong_avoid(sk, ack, acked);
+2 −1
Original line number Diff line number Diff line
@@ -13,7 +13,8 @@
char _license[] SEC("license") = "GPL";
const char cubic[] = "cubic";

void BPF_STRUCT_OPS(dctcp_nouse_release, struct sock *sk)
SEC("struct_ops")
void BPF_PROG(dctcp_nouse_release, struct sock *sk)
{
	bpf_setsockopt(sk, SOL_TCP, TCP_CONGESTION,
		       (void *)cubic, sizeof(cubic));
+2 −1
Original line number Diff line number Diff line
@@ -8,7 +8,8 @@

char _license[] SEC("license") = "X";

void BPF_STRUCT_OPS(nogpltcp_init, struct sock *sk)
SEC("struct_ops")
void BPF_PROG(nogpltcp_init, struct sock *sk)
{
}

Loading