18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2019 Facebook */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci/* WARNING: This implemenation is not necessarily the same
58c2ecf20Sopenharmony_ci * as the tcp_dctcp.c.  The purpose is mainly for testing
68c2ecf20Sopenharmony_ci * the kernel BPF logic.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <stddef.h>
108c2ecf20Sopenharmony_ci#include <linux/bpf.h>
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci#include <linux/stddef.h>
138c2ecf20Sopenharmony_ci#include <linux/tcp.h>
148c2ecf20Sopenharmony_ci#include <bpf/bpf_helpers.h>
158c2ecf20Sopenharmony_ci#include <bpf/bpf_tracing.h>
168c2ecf20Sopenharmony_ci#include "bpf_tcp_helpers.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cichar _license[] SEC("license") = "GPL";
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciint stg_result = 0;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct {
238c2ecf20Sopenharmony_ci	__uint(type, BPF_MAP_TYPE_SK_STORAGE);
248c2ecf20Sopenharmony_ci	__uint(map_flags, BPF_F_NO_PREALLOC);
258c2ecf20Sopenharmony_ci	__type(key, int);
268c2ecf20Sopenharmony_ci	__type(value, int);
278c2ecf20Sopenharmony_ci} sk_stg_map SEC(".maps");
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DCTCP_MAX_ALPHA	1024U
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistruct dctcp {
328c2ecf20Sopenharmony_ci	__u32 old_delivered;
338c2ecf20Sopenharmony_ci	__u32 old_delivered_ce;
348c2ecf20Sopenharmony_ci	__u32 prior_rcv_nxt;
358c2ecf20Sopenharmony_ci	__u32 dctcp_alpha;
368c2ecf20Sopenharmony_ci	__u32 next_seq;
378c2ecf20Sopenharmony_ci	__u32 ce_state;
388c2ecf20Sopenharmony_ci	__u32 loss_cwnd;
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic unsigned int dctcp_shift_g = 4; /* g = 1/2^4 */
428c2ecf20Sopenharmony_cistatic unsigned int dctcp_alpha_on_init = DCTCP_MAX_ALPHA;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic __always_inline void dctcp_reset(const struct tcp_sock *tp,
458c2ecf20Sopenharmony_ci					struct dctcp *ca)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	ca->next_seq = tp->snd_nxt;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	ca->old_delivered = tp->delivered;
508c2ecf20Sopenharmony_ci	ca->old_delivered_ce = tp->delivered_ce;
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciSEC("struct_ops/dctcp_init")
548c2ecf20Sopenharmony_civoid BPF_PROG(dctcp_init, struct sock *sk)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	const struct tcp_sock *tp = tcp_sk(sk);
578c2ecf20Sopenharmony_ci	struct dctcp *ca = inet_csk_ca(sk);
588c2ecf20Sopenharmony_ci	int *stg;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ca->prior_rcv_nxt = tp->rcv_nxt;
618c2ecf20Sopenharmony_ci	ca->dctcp_alpha = min(dctcp_alpha_on_init, DCTCP_MAX_ALPHA);
628c2ecf20Sopenharmony_ci	ca->loss_cwnd = 0;
638c2ecf20Sopenharmony_ci	ca->ce_state = 0;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	stg = bpf_sk_storage_get(&sk_stg_map, (void *)tp, NULL, 0);
668c2ecf20Sopenharmony_ci	if (stg) {
678c2ecf20Sopenharmony_ci		stg_result = *stg;
688c2ecf20Sopenharmony_ci		bpf_sk_storage_delete(&sk_stg_map, (void *)tp);
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci	dctcp_reset(tp, ca);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ciSEC("struct_ops/dctcp_ssthresh")
748c2ecf20Sopenharmony_ci__u32 BPF_PROG(dctcp_ssthresh, struct sock *sk)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct dctcp *ca = inet_csk_ca(sk);
778c2ecf20Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	ca->loss_cwnd = tp->snd_cwnd;
808c2ecf20Sopenharmony_ci	return max(tp->snd_cwnd - ((tp->snd_cwnd * ca->dctcp_alpha) >> 11U), 2U);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciSEC("struct_ops/dctcp_update_alpha")
848c2ecf20Sopenharmony_civoid BPF_PROG(dctcp_update_alpha, struct sock *sk, __u32 flags)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	const struct tcp_sock *tp = tcp_sk(sk);
878c2ecf20Sopenharmony_ci	struct dctcp *ca = inet_csk_ca(sk);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* Expired RTT */
908c2ecf20Sopenharmony_ci	if (!before(tp->snd_una, ca->next_seq)) {
918c2ecf20Sopenharmony_ci		__u32 delivered_ce = tp->delivered_ce - ca->old_delivered_ce;
928c2ecf20Sopenharmony_ci		__u32 alpha = ca->dctcp_alpha;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci		/* alpha = (1 - g) * alpha + g * F */
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		alpha -= min_not_zero(alpha, alpha >> dctcp_shift_g);
978c2ecf20Sopenharmony_ci		if (delivered_ce) {
988c2ecf20Sopenharmony_ci			__u32 delivered = tp->delivered - ca->old_delivered;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci			/* If dctcp_shift_g == 1, a 32bit value would overflow
1018c2ecf20Sopenharmony_ci			 * after 8 M packets.
1028c2ecf20Sopenharmony_ci			 */
1038c2ecf20Sopenharmony_ci			delivered_ce <<= (10 - dctcp_shift_g);
1048c2ecf20Sopenharmony_ci			delivered_ce /= max(1U, delivered);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci			alpha = min(alpha + delivered_ce, DCTCP_MAX_ALPHA);
1078c2ecf20Sopenharmony_ci		}
1088c2ecf20Sopenharmony_ci		ca->dctcp_alpha = alpha;
1098c2ecf20Sopenharmony_ci		dctcp_reset(tp, ca);
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic __always_inline void dctcp_react_to_loss(struct sock *sk)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct dctcp *ca = inet_csk_ca(sk);
1168c2ecf20Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	ca->loss_cwnd = tp->snd_cwnd;
1198c2ecf20Sopenharmony_ci	tp->snd_ssthresh = max(tp->snd_cwnd >> 1U, 2U);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ciSEC("struct_ops/dctcp_state")
1238c2ecf20Sopenharmony_civoid BPF_PROG(dctcp_state, struct sock *sk, __u8 new_state)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	if (new_state == TCP_CA_Recovery &&
1268c2ecf20Sopenharmony_ci	    new_state != BPF_CORE_READ_BITFIELD(inet_csk(sk), icsk_ca_state))
1278c2ecf20Sopenharmony_ci		dctcp_react_to_loss(sk);
1288c2ecf20Sopenharmony_ci	/* We handle RTO in dctcp_cwnd_event to ensure that we perform only
1298c2ecf20Sopenharmony_ci	 * one loss-adjustment per RTT.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic __always_inline void dctcp_ece_ack_cwr(struct sock *sk, __u32 ce_state)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	if (ce_state == 1)
1388c2ecf20Sopenharmony_ci		tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
1398c2ecf20Sopenharmony_ci	else
1408c2ecf20Sopenharmony_ci		tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* Minimal DCTP CE state machine:
1448c2ecf20Sopenharmony_ci *
1458c2ecf20Sopenharmony_ci * S:	0 <- last pkt was non-CE
1468c2ecf20Sopenharmony_ci *	1 <- last pkt was CE
1478c2ecf20Sopenharmony_ci */
1488c2ecf20Sopenharmony_cistatic __always_inline
1498c2ecf20Sopenharmony_civoid dctcp_ece_ack_update(struct sock *sk, enum tcp_ca_event evt,
1508c2ecf20Sopenharmony_ci			  __u32 *prior_rcv_nxt, __u32 *ce_state)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	__u32 new_ce_state = (evt == CA_EVENT_ECN_IS_CE) ? 1 : 0;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (*ce_state != new_ce_state) {
1558c2ecf20Sopenharmony_ci		/* CE state has changed, force an immediate ACK to
1568c2ecf20Sopenharmony_ci		 * reflect the new CE state. If an ACK was delayed,
1578c2ecf20Sopenharmony_ci		 * send that first to reflect the prior CE state.
1588c2ecf20Sopenharmony_ci		 */
1598c2ecf20Sopenharmony_ci		if (inet_csk(sk)->icsk_ack.pending & ICSK_ACK_TIMER) {
1608c2ecf20Sopenharmony_ci			dctcp_ece_ack_cwr(sk, *ce_state);
1618c2ecf20Sopenharmony_ci			bpf_tcp_send_ack(sk, *prior_rcv_nxt);
1628c2ecf20Sopenharmony_ci		}
1638c2ecf20Sopenharmony_ci		inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci	*prior_rcv_nxt = tcp_sk(sk)->rcv_nxt;
1668c2ecf20Sopenharmony_ci	*ce_state = new_ce_state;
1678c2ecf20Sopenharmony_ci	dctcp_ece_ack_cwr(sk, new_ce_state);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ciSEC("struct_ops/dctcp_cwnd_event")
1718c2ecf20Sopenharmony_civoid BPF_PROG(dctcp_cwnd_event, struct sock *sk, enum tcp_ca_event ev)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct dctcp *ca = inet_csk_ca(sk);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	switch (ev) {
1768c2ecf20Sopenharmony_ci	case CA_EVENT_ECN_IS_CE:
1778c2ecf20Sopenharmony_ci	case CA_EVENT_ECN_NO_CE:
1788c2ecf20Sopenharmony_ci		dctcp_ece_ack_update(sk, ev, &ca->prior_rcv_nxt, &ca->ce_state);
1798c2ecf20Sopenharmony_ci		break;
1808c2ecf20Sopenharmony_ci	case CA_EVENT_LOSS:
1818c2ecf20Sopenharmony_ci		dctcp_react_to_loss(sk);
1828c2ecf20Sopenharmony_ci		break;
1838c2ecf20Sopenharmony_ci	default:
1848c2ecf20Sopenharmony_ci		/* Don't care for the rest. */
1858c2ecf20Sopenharmony_ci		break;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ciSEC("struct_ops/dctcp_cwnd_undo")
1908c2ecf20Sopenharmony_ci__u32 BPF_PROG(dctcp_cwnd_undo, struct sock *sk)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	const struct dctcp *ca = inet_csk_ca(sk);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	return max(tcp_sk(sk)->snd_cwnd, ca->loss_cwnd);
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciSEC("struct_ops/tcp_reno_cong_avoid")
1988c2ecf20Sopenharmony_civoid BPF_PROG(tcp_reno_cong_avoid, struct sock *sk, __u32 ack, __u32 acked)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (!tcp_is_cwnd_limited(sk))
2038c2ecf20Sopenharmony_ci		return;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/* In "safe" area, increase. */
2068c2ecf20Sopenharmony_ci	if (tcp_in_slow_start(tp)) {
2078c2ecf20Sopenharmony_ci		acked = tcp_slow_start(tp, acked);
2088c2ecf20Sopenharmony_ci		if (!acked)
2098c2ecf20Sopenharmony_ci			return;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci	/* In dangerous area, increase slowly. */
2128c2ecf20Sopenharmony_ci	tcp_cong_avoid_ai(tp, tp->snd_cwnd, acked);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciSEC(".struct_ops")
2168c2ecf20Sopenharmony_cistruct tcp_congestion_ops dctcp_nouse = {
2178c2ecf20Sopenharmony_ci	.init		= (void *)dctcp_init,
2188c2ecf20Sopenharmony_ci	.set_state	= (void *)dctcp_state,
2198c2ecf20Sopenharmony_ci	.flags		= TCP_CONG_NEEDS_ECN,
2208c2ecf20Sopenharmony_ci	.name		= "bpf_dctcp_nouse",
2218c2ecf20Sopenharmony_ci};
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ciSEC(".struct_ops")
2248c2ecf20Sopenharmony_cistruct tcp_congestion_ops dctcp = {
2258c2ecf20Sopenharmony_ci	.init		= (void *)dctcp_init,
2268c2ecf20Sopenharmony_ci	.in_ack_event   = (void *)dctcp_update_alpha,
2278c2ecf20Sopenharmony_ci	.cwnd_event	= (void *)dctcp_cwnd_event,
2288c2ecf20Sopenharmony_ci	.ssthresh	= (void *)dctcp_ssthresh,
2298c2ecf20Sopenharmony_ci	.cong_avoid	= (void *)tcp_reno_cong_avoid,
2308c2ecf20Sopenharmony_ci	.undo_cwnd	= (void *)dctcp_cwnd_undo,
2318c2ecf20Sopenharmony_ci	.set_state	= (void *)dctcp_state,
2328c2ecf20Sopenharmony_ci	.flags		= TCP_CONG_NEEDS_ECN,
2338c2ecf20Sopenharmony_ci	.name		= "bpf_dctcp",
2348c2ecf20Sopenharmony_ci};
235