Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

694 Zeilen
20KB

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TCP ROCCET: An RTT-Oriented CUBIC Congestion Control
  4. * Extension for 5G and Beyond Networks
  5. *
  6. * TCP ROCCET is a new TCP congestion control
  7. * algorithm suited for current cellular 5G NR beyond networks.
  8. * It extends the kernel default congestion control CUBIC
  9. * and improves its performance, and additionally solves an
  10. * unwanted side effects of CUBIC’s implementation.
  11. * ROCCET uses its own Slow Start, called LAUNCH, where loss
  12. * is not considered as a congestion event.
  13. * The congestion avoidance phase, called ORBITER, uses
  14. * CUBIC's window growth function and adds, based on RTT
  15. * and ACK rate, congestion events.
  16. *
  17. * A peer-reviewed paper on TCP ROCCET will be presented at the WONS 2026 conference.
  18. * A draft of the paper is available here:
  19. * https://arxiv.org/abs/2510.25281
  20. *
  21. *
  22. * Further information about CUBIC:
  23. * TCP CUBIC: Binary Increase Congestion control for TCP v2.3
  24. * Home page:
  25. * http://netsrv.csc.ncsu.edu/twiki/bin/view/Main/BIC
  26. * This is from the implementation of CUBIC TCP in
  27. * Sangtae Ha, Injong Rhee and Lisong Xu,
  28. * "CUBIC: A New TCP-Friendly High-Speed TCP Variant"
  29. * in ACM SIGOPS Operating System Review, July 2008.
  30. * Available from:
  31. * http://netsrv.csc.ncsu.edu/export/cubic_a_new_tcp_2008.pdf
  32. *
  33. * CUBIC integrates a new slow start algorithm, called HyStart.
  34. * The details of HyStart are presented in
  35. * Sangtae Ha and Injong Rhee,
  36. * "Taming the Elephants: New TCP Slow Start", NCSU TechReport 2008.
  37. * Available from:
  38. * http://netsrv.csc.ncsu.edu/export/hystart_techreport_2008.pdf
  39. *
  40. * All testing results are available from:
  41. * http://netsrv.csc.ncsu.edu/wiki/index.php/TCP_Testing
  42. *
  43. * Unless CUBIC is enabled and congestion window is large
  44. * this behaves the same as the original Reno.
  45. */
  46. #include "tcp_roccet.h"
  47. #include "linux/printk.h"
  48. #include <linux/btf.h>
  49. #include <linux/btf_ids.h>
  50. #include <linux/math64.h>
  51. #include <linux/mm.h>
  52. #include <linux/module.h>
  53. #include <net/tcp.h>
  54. /* Scale factor beta calculation (max_cwnd = snd_cwnd * beta) */
  55. #define BICTCP_BETA_SCALE 1024
  56. #define BICTCP_HZ 10 /* BIC HZ 2^10 = 1024 */
  57. /* Alpha value for the sRrTT multiplied by 100.
  58. * Here 20 represents a value of 0.2
  59. */
  60. #define ROCCET_ALPHA_TIMES_100 20
  61. /* The amount of seconds ROCCET stores a minRTT.
  62. * Enable "calculate_min_rtt" first.
  63. */
  64. #define ROCCET_RTT_LOOKBACK_S 10
  65. /* Parameters that are specific to the ROCCET-Algorithm */
  66. static int sr_rtt_upper_bound __read_mostly = 100;
  67. static int ack_rate_diff_ss __read_mostly = 10;
  68. static int ack_rate_diff_ca __read_mostly = 200;
  69. static int calculate_min_rtt __read_mostly = 0;
  70. static int ignore_loss __read_mostly = 0;
  71. static int roccet_minRTT_interpolation_factor __read_mostly = 70;
  72. module_param(sr_rtt_upper_bound, int, 0644);
  73. MODULE_PARM_DESC(sr_rtt_upper_bound, "ROCCET's upper bound for srRTT.");
  74. module_param(ack_rate_diff_ss, int, 0644);
  75. MODULE_PARM_DESC(ack_rate_diff_ss,
  76. "ROCCET's threshold to exit slow start if ACK-rate defer by "
  77. "given amount of segments.");
  78. module_param(ack_rate_diff_ca, int, 0644);
  79. MODULE_PARM_DESC(ack_rate_diff_ca,
  80. "ROCCET's threshold for ack-rate and cum_cwnd, in percantage "
  81. "of the current cwnd.");
  82. module_param(calculate_min_rtt, int, 0644);
  83. MODULE_PARM_DESC(calculate_min_rtt,
  84. "Calculate min RTT if no lower RTT occurs after 10 sec.");
  85. module_param(ignore_loss, int, 0644);
  86. MODULE_PARM_DESC(ignore_loss, "Ignore loss as a congestion event.");
  87. module_param(roccet_minRTT_interpolation_factor, int, 0644);
  88. MODULE_PARM_DESC(
  89. roccet_minRTT_interpolation_factor,
  90. "ROCCET factor for interpolating the current RTT with the last minRTT "
  91. "(minRTT = (factor * currRTT + (100-factor) * minRTT) / 100)");
  92. static int fast_convergence __read_mostly = 1;
  93. static int beta __read_mostly = 717; /* = 717/1024 (BICTCP_BETA_SCALE) */
  94. static int initial_ssthresh __read_mostly;
  95. static int bic_scale __read_mostly = 41;
  96. static int tcp_friendliness __read_mostly = 1;
  97. static u32 cube_rtt_scale __read_mostly;
  98. static u32 beta_scale __read_mostly;
  99. static u64 cube_factor __read_mostly;
  100. /* Note parameters that are used for precomputing scale factors are read-only */
  101. module_param(fast_convergence, int, 0644);
  102. MODULE_PARM_DESC(fast_convergence, "turn on/off fast convergence");
  103. module_param(beta, int, 0644);
  104. MODULE_PARM_DESC(beta, "beta for multiplicative increase");
  105. module_param(initial_ssthresh, int, 0644);
  106. MODULE_PARM_DESC(initial_ssthresh, "initial value of slow start threshold");
  107. module_param(bic_scale, int, 0444);
  108. MODULE_PARM_DESC(
  109. bic_scale,
  110. "scale (scaled by 1024) value for bic function (bic_scale/1024)");
  111. module_param(tcp_friendliness, int, 0644);
  112. MODULE_PARM_DESC(tcp_friendliness, "turn on/off tcp friendliness");
  113. static inline void roccettcp_reset(struct roccettcp *ca)
  114. {
  115. memset(ca, 0, offsetof(struct roccettcp, curr_rtt));
  116. ca->bw_limit.sum_cwnd = 1;
  117. ca->bw_limit.sum_acked = 1;
  118. ca->bw_limit.next_check = 0;
  119. ca->curr_min_rtt_timed.rtt = ~0U;
  120. ca->curr_min_rtt_timed.time = ~0U;
  121. ca->ece_srrtt = 0;
  122. ca->ece_cwnd = 2;
  123. }
  124. static inline void update_min_rtt(struct sock *sk)
  125. {
  126. struct roccettcp *ca = inet_csk_ca(sk);
  127. u32 now = jiffies_to_usecs(tcp_jiffies32);
  128. if (now - ca->curr_min_rtt_timed.time >
  129. ROCCET_RTT_LOOKBACK_S * USEC_PER_SEC &&
  130. calculate_min_rtt) {
  131. u32 new_min_rtt = max(ca->curr_rtt, 1);
  132. u32 old_min_rtt = ca->curr_min_rtt_timed.rtt;
  133. u32 interpolated_min_rtt =
  134. (new_min_rtt * roccet_minRTT_interpolation_factor +
  135. old_min_rtt *
  136. (100 - roccet_minRTT_interpolation_factor)) /
  137. 100;
  138. ca->curr_min_rtt_timed.rtt = interpolated_min_rtt;
  139. ca->curr_min_rtt_timed.time = now;
  140. }
  141. /* Check if new lower min RTT was found. If so, set it directly */
  142. if (ca->curr_rtt < ca->curr_min_rtt_timed.rtt) {
  143. ca->curr_min_rtt_timed.rtt = max(ca->curr_rtt, 1);
  144. ca->curr_min_rtt_timed.time = now;
  145. }
  146. }
  147. /* Return difference between last and current ack rate.
  148. */
  149. static inline int get_ack_rate_diff(struct roccettcp *ca)
  150. {
  151. return ca->ack_rate.last_rate - ca->ack_rate.curr_rate;
  152. }
  153. /* Update ack rate sampled by 100ms.
  154. */
  155. static inline void update_ack_rate(struct sock *sk)
  156. {
  157. struct roccettcp *ca = inet_csk_ca(sk);
  158. u32 now = jiffies_to_usecs(tcp_jiffies32);
  159. u32 interval = USEC_PER_MSEC * 100;
  160. if ((u32)(now - ca->ack_rate.last_rate_time) >= interval) {
  161. ca->ack_rate.last_rate_time = now;
  162. ca->ack_rate.last_rate = ca->ack_rate.curr_rate;
  163. ca->ack_rate.curr_rate = ca->ack_rate.cnt;
  164. ca->ack_rate.cnt = 0;
  165. } else {
  166. ca->ack_rate.cnt += 1;
  167. }
  168. }
  169. /* Compute srRTT.
  170. */
  171. static inline void update_srrtt(struct sock *sk)
  172. {
  173. struct roccettcp *ca = inet_csk_ca(sk);
  174. if (ca->curr_min_rtt_timed.rtt == 0)
  175. return;
  176. /* Calculate the new rRTT (Scaled by 100).
  177. * 100 * ((sRTT - sRTT_min) / sRTT_min)
  178. */
  179. u32 rRTT = (100 * (ca->curr_rtt - ca->curr_min_rtt_timed.rtt)) /
  180. ca->curr_min_rtt_timed.rtt;
  181. // (1 - alpha) * srRTT + alpha * rRTT
  182. ca->curr_srRTT = ((100 - ROCCET_ALPHA_TIMES_100) * ca->curr_srRTT +
  183. ROCCET_ALPHA_TIMES_100 * rRTT) /
  184. 100;
  185. }
  186. __bpf_kfunc static void roccettcp_init(struct sock *sk)
  187. {
  188. struct roccettcp *ca = inet_csk_ca(sk);
  189. roccettcp_reset(ca);
  190. if (initial_ssthresh)
  191. tcp_sk(sk)->snd_ssthresh = initial_ssthresh;
  192. /* Initial roccet paramters */
  193. ca->roccet_last_event_time_us = 0;
  194. ca->curr_min_rtt = ~0U;
  195. ca->ack_rate.last_rate = 0;
  196. ca->ack_rate.last_rate_time = 0;
  197. ca->ack_rate.curr_rate = 0;
  198. ca->ack_rate.cnt = 0;
  199. }
  200. __bpf_kfunc static void roccettcp_cwnd_event(struct sock *sk,
  201. enum tcp_ca_event event)
  202. {
  203. if (event == CA_EVENT_TX_START) {
  204. struct roccettcp *ca = inet_csk_ca(sk);
  205. u32 now = tcp_jiffies32;
  206. s32 delta;
  207. delta = now - tcp_sk(sk)->lsndtime;
  208. /* We were application limited (idle) for a while.
  209. * Shift epoch_start to keep cwnd growth to cubic curve.
  210. */
  211. if (ca->epoch_start && delta > 0) {
  212. ca->epoch_start += delta;
  213. if (after(ca->epoch_start, now))
  214. ca->epoch_start = now;
  215. }
  216. return;
  217. }
  218. }
  219. /* calculate the cubic root of x using a table lookup followed by one
  220. * Newton-Raphson iteration.
  221. * Avg err ~= 0.195%
  222. */
  223. static u32 cubic_root(u64 a)
  224. {
  225. u32 x, b, shift;
  226. /* cbrt(x) MSB values for x MSB values in [0..63].
  227. * Precomputed then refined by hand - Willy Tarreau
  228. *
  229. * For x in [0..63],
  230. * v = cbrt(x << 18) - 1
  231. * cbrt(x) = (v[x] + 10) >> 6
  232. */
  233. static const u8 v[] = {
  234. /* 0x00 */ 0, 54, 54, 54, 118, 118, 118, 118,
  235. /* 0x08 */ 123, 129, 134, 138, 143, 147, 151, 156,
  236. /* 0x10 */ 157, 161, 164, 168, 170, 173, 176, 179,
  237. /* 0x18 */ 181, 185, 187, 190, 192, 194, 197, 199,
  238. /* 0x20 */ 200, 202, 204, 206, 209, 211, 213, 215,
  239. /* 0x28 */ 217, 219, 221, 222, 224, 225, 227, 229,
  240. /* 0x30 */ 231, 232, 234, 236, 237, 239, 240, 242,
  241. /* 0x38 */ 244, 245, 246, 248, 250, 251, 252, 254,
  242. };
  243. b = fls64(a);
  244. if (b < 7) {
  245. /* a in [0..63] */
  246. return ((u32)v[(u32)a] + 35) >> 6;
  247. }
  248. b = ((b * 84) >> 8) - 1;
  249. shift = (a >> (b * 3));
  250. x = ((u32)(((u32)v[shift] + 10) << b)) >> 6;
  251. /* Newton-Raphson iteration
  252. * 2
  253. * x = ( 2 * x + a / x ) / 3
  254. * k+1 k k
  255. */
  256. x = (2 * x + (u32)div64_u64(a, (u64)x * (u64)(x - 1)));
  257. x = ((x * 341) >> 10);
  258. return x;
  259. }
  260. /* Compute congestion window to use.
  261. */
  262. static inline void bictcp_update(struct roccettcp *ca, u32 cwnd, u32 acked)
  263. {
  264. u32 delta, bic_target, max_cnt;
  265. u64 offs, t;
  266. ca->ack_cnt += acked; /* count the number of ACKed packets */
  267. if (ca->last_cwnd == cwnd &&
  268. (s32)(tcp_jiffies32 - ca->last_time) <= HZ / 32)
  269. return;
  270. /* The CUBIC function can update ca->cnt at most once per jiffy.
  271. * On all cwnd reduction events, ca->epoch_start is set to 0,
  272. * which will force a recalculation of ca->cnt.
  273. */
  274. if (ca->epoch_start && tcp_jiffies32 == ca->last_time)
  275. goto tcp_friendliness;
  276. ca->last_cwnd = cwnd;
  277. ca->last_time = tcp_jiffies32;
  278. if (ca->epoch_start == 0) {
  279. ca->epoch_start = tcp_jiffies32; /* record beginning */
  280. ca->ack_cnt = acked; /* start counting */
  281. ca->tcp_cwnd = cwnd; /* syn with cubic */
  282. if (ca->last_max_cwnd <= cwnd) {
  283. ca->bic_K = 0;
  284. ca->bic_origin_point = cwnd;
  285. } else {
  286. /* Compute new K based on
  287. * (wmax-cwnd) * (srtt>>3 / HZ) / c * 2^(3*bictcp_HZ)
  288. */
  289. ca->bic_K = cubic_root(cube_factor *
  290. (ca->last_max_cwnd - cwnd));
  291. ca->bic_origin_point = ca->last_max_cwnd;
  292. }
  293. }
  294. /* cubic function - calc */
  295. /* calculate c * time^3 / rtt,
  296. * while considering overflow in calculation of time^3
  297. * (so time^3 is done by using 64 bit)
  298. * and without the support of division of 64bit numbers
  299. * (so all divisions are done by using 32 bit)
  300. * also NOTE the unit of those veriables
  301. * time = (t - K) / 2^bictcp_HZ
  302. * c = bic_scale >> 10
  303. * rtt = (srtt >> 3) / HZ
  304. * !!! The following code does not have overflow problems,
  305. * if the cwnd < 1 million packets !!!
  306. */
  307. t = (s32)(tcp_jiffies32 - ca->epoch_start);
  308. t += usecs_to_jiffies(ca->delay_min);
  309. /* change the unit from HZ to bictcp_HZ */
  310. t <<= BICTCP_HZ;
  311. do_div(t, HZ);
  312. if (t < ca->bic_K) /* t - K */
  313. offs = ca->bic_K - t;
  314. else
  315. offs = t - ca->bic_K;
  316. /* c/rtt * (t-K)^3 */
  317. delta = (cube_rtt_scale * offs * offs * offs) >> (10 + 3 * BICTCP_HZ);
  318. if (t < ca->bic_K) /* below origin*/
  319. bic_target = ca->bic_origin_point - delta;
  320. else /* above origin*/
  321. bic_target = ca->bic_origin_point + delta;
  322. /* cubic function - calc bictcp_cnt*/
  323. if (bic_target > cwnd) {
  324. ca->cnt = cwnd / (bic_target - cwnd);
  325. } else {
  326. ca->cnt = 100 * cwnd; /* very small increment*/
  327. }
  328. /* The initial growth of cubic function may be too conservative
  329. * when the available bandwidth is still unknown.
  330. */
  331. if (ca->last_max_cwnd == 0 && ca->cnt > 20)
  332. ca->cnt = 20; /* increase cwnd 5% per RTT */
  333. tcp_friendliness:
  334. /* TCP Friendly */
  335. if (tcp_friendliness) {
  336. u32 scale = beta_scale;
  337. delta = (cwnd * scale) >> 3;
  338. while (ca->ack_cnt > delta) { /* update tcp cwnd */
  339. ca->ack_cnt -= delta;
  340. ca->tcp_cwnd++;
  341. }
  342. if (ca->tcp_cwnd > cwnd) { /* if bic is slower than tcp */
  343. delta = ca->tcp_cwnd - cwnd;
  344. max_cnt = cwnd / delta;
  345. if (ca->cnt > max_cnt)
  346. ca->cnt = max_cnt;
  347. }
  348. }
  349. /* The maximum rate of cwnd increase CUBIC allows is 1 packet per
  350. * 2 packets ACKed, meaning cwnd grows at 1.5x per RTT.
  351. */
  352. ca->cnt = max(ca->cnt, 2U);
  353. }
  354. __bpf_kfunc static void roccettcp_cong_avoid(struct sock *sk, u32 ack,
  355. u32 acked)
  356. {
  357. struct tcp_sock *tp = tcp_sk(sk);
  358. struct roccettcp *ca = inet_csk_ca(sk);
  359. u32 now = jiffies_to_usecs(tcp_jiffies32);
  360. u32 bw_limit_detect = 0;
  361. u32 roccet_xj;
  362. u32 jitter;
  363. if (ca->last_rtt > ca->curr_rtt) {
  364. jitter = ca->last_rtt - ca->curr_rtt;
  365. } else {
  366. jitter = ca->curr_rtt - ca->last_rtt;
  367. }
  368. /* Update roccet paramters */
  369. update_ack_rate(sk);
  370. update_min_rtt(sk);
  371. update_srrtt(sk);
  372. /* Reset ECE handling if we already have more bandwidth
  373. * than we received the last ECE.
  374. */
  375. if(ca->ece_srrtt > 0){
  376. if(tcp_snd_cwnd(tp) >= ca->ece_cwnd){
  377. ca->ece_srrtt = 0;
  378. }
  379. }
  380. /* ROCCET drain.
  381. * Do not increase the cwnd for 100ms after a roccet congestion event
  382. */
  383. if (now - ca->roccet_last_event_time_us <= 100 * USEC_PER_MSEC)
  384. return;
  385. /* LAUNCH: Detect an exit point for tcp slow start
  386. * in networks with large buffers of multiple BDP
  387. * Like in cellular networks (5G, ...).
  388. * Or exit LAUNCH if cwnd is too large for application layer
  389. * data rate.
  390. */
  391. if ((tcp_in_slow_start(tp) && ca->curr_srRTT > sr_rtt_upper_bound &&
  392. get_ack_rate_diff(ca) >= ack_rate_diff_ss) ||
  393. (!tcp_is_cwnd_limited(sk) && tcp_in_slow_start(tp))
  394. ) {
  395. ca->epoch_start = 0;
  396. /* Handle inital slow start. Here we observe the most problems */
  397. if (tp->snd_ssthresh == TCP_INFINITE_SSTHRESH) {
  398. tcp_sk(sk)->snd_ssthresh = tcp_snd_cwnd(tp) / 2;
  399. tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) / 2);
  400. } else {
  401. tcp_sk(sk)->snd_ssthresh =
  402. tcp_snd_cwnd(tp) - (tcp_snd_cwnd(tp) / 3);
  403. tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) -
  404. (tcp_snd_cwnd(tp) / 3));
  405. }
  406. ca->roccet_last_event_time_us = now;
  407. return;
  408. }
  409. if (tcp_in_slow_start(tp)) {
  410. acked = tcp_slow_start(tp, acked);
  411. if (!acked)
  412. return;
  413. }
  414. if (ca->bw_limit.next_check == 0)
  415. ca->bw_limit.next_check = now + 5 * ca->curr_rtt;
  416. ca->bw_limit.sum_cwnd += tcp_snd_cwnd(tp);
  417. ca->bw_limit.sum_acked += acked;
  418. if (ca->bw_limit.next_check < now) {
  419. /* We send more data as we got acked in the last 5 RTTs */
  420. if ((ca->bw_limit.sum_cwnd * 100) / ca->bw_limit.sum_acked >=
  421. ack_rate_diff_ca)
  422. bw_limit_detect = 1;
  423. /* reset struct and set next end of period */
  424. ca->bw_limit.sum_cwnd = 1;
  425. /* set to 1 to avoid division by zero */
  426. ca->bw_limit.sum_acked = 1;
  427. ca->bw_limit.next_check = now + 5 * ca->curr_rtt;
  428. }
  429. /* Respects the jitter of the connection and add it on top of the upper bound
  430. * for the srRTT
  431. */
  432. roccet_xj = ((jitter * 100) / ca->curr_min_rtt_timed.rtt) +
  433. sr_rtt_upper_bound;
  434. if (roccet_xj < sr_rtt_upper_bound)
  435. roccet_xj = sr_rtt_upper_bound;
  436. /* This is true if we recently received an ECE bit.
  437. * Therefore we should respect the srRTT at this piont.
  438. */
  439. if(ca->ece_srrtt < roccet_xj && ca->ece_srrtt > 0)
  440. roccet_xj = ca->ece_srrtt;
  441. if (ca->curr_srRTT > roccet_xj && (bw_limit_detect || ca->ece_srrtt > 0)) {
  442. ca->epoch_start = 0;
  443. ca->roccet_last_event_time_us = now;
  444. ca->cnt = 100 * tcp_snd_cwnd(tp);
  445. /* Set Wmax if cwnd is larger than the old Wmax */
  446. if (tcp_snd_cwnd(tp) > ca->last_max_cwnd)
  447. ca->last_max_cwnd = tcp_snd_cwnd(tp);
  448. tcp_snd_cwnd_set(tp, min(tp->snd_cwnd_clamp,
  449. max((tcp_snd_cwnd(tp) * beta) / BICTCP_BETA_SCALE, 2U)));
  450. tp->snd_ssthresh = tcp_snd_cwnd(tp);
  451. return;
  452. }
  453. /* Terminates this function if cwnd is not fully utilized.
  454. * In mobile networks like 5G, this termination causes the cwnd to be frozen at
  455. * an excessively high value. This is because slow start or HyStart massively
  456. * exceed the available bandwidth and leave the cwnd at an excessively high
  457. * value. The cwnd cannot therefore be fully utilized because it is limited by
  458. * the connection capacity.
  459. */
  460. if (!tcp_is_cwnd_limited(sk))
  461. return;
  462. bictcp_update(ca, tcp_snd_cwnd(tp), acked);
  463. tcp_cong_avoid_ai(tp, max(1, ca->cnt), acked);
  464. }
  465. __bpf_kfunc static u32 roccettcp_recalc_ssthresh(struct sock *sk)
  466. {
  467. const struct tcp_sock *tp = tcp_sk(sk);
  468. struct roccettcp *ca = inet_csk_ca(sk);
  469. if (ignore_loss)
  470. return tcp_snd_cwnd(tp);
  471. /* Don't exit slow start if loss occurs. */
  472. if (tcp_in_slow_start(tp))
  473. return tcp_snd_cwnd(tp);
  474. ca->epoch_start = 0; /* end of epoch */
  475. /* Wmax and fast convergence */
  476. if (tcp_snd_cwnd(tp) < ca->last_max_cwnd && fast_convergence)
  477. ca->last_max_cwnd =
  478. (tcp_snd_cwnd(tp) * (BICTCP_BETA_SCALE + beta)) /
  479. (2 * BICTCP_BETA_SCALE);
  480. else
  481. ca->last_max_cwnd = tcp_snd_cwnd(tp);
  482. return max((tcp_snd_cwnd(tp) * beta) / BICTCP_BETA_SCALE, 2U);
  483. }
  484. __bpf_kfunc static void roccettcp_state(struct sock *sk, u8 new_state)
  485. {
  486. struct roccettcp *ca = inet_csk_ca(sk);
  487. if (new_state == TCP_CA_Loss)
  488. roccettcp_reset(ca);
  489. }
  490. __bpf_kfunc static void roccettcp_acked(struct sock *sk,
  491. const struct ack_sample *sample)
  492. {
  493. struct roccettcp *ca = inet_csk_ca(sk);
  494. /* Some calls are for duplicates without timestamps */
  495. if (sample->rtt_us < 0)
  496. return;
  497. /* Discard delay samples right after fast recovery */
  498. if (ca->epoch_start && (s32)(tcp_jiffies32 - ca->epoch_start) < HZ)
  499. return;
  500. u32 delay = sample->rtt_us;
  501. if (delay == 0)
  502. delay = 1;
  503. /* first time call or link delay decreases */
  504. if (ca->delay_min == 0 || ca->delay_min > delay)
  505. ca->delay_min = delay;
  506. /* Get valid sample for roccet */
  507. if (sample->rtt_us > 0) {
  508. ca->last_rtt = ca->curr_rtt;
  509. ca->curr_rtt = sample->rtt_us;
  510. }
  511. }
  512. __bpf_kfunc static void roccet_in_ack_event(struct sock *sk, u32 flags)
  513. {
  514. struct tcp_sock *tp = tcp_sk(sk);
  515. struct roccettcp *ca = inet_csk_ca(sk);
  516. /* Handle ECE bit.
  517. * Pocessing of ECE events is done in roccettcp_cong_avoid()
  518. */
  519. if (flags & CA_ACK_ECE) {
  520. ca->ece_srrtt = ca->curr_srRTT;
  521. ca->ece_cwnd = tcp_snd_cwnd(tp);
  522. }
  523. }
  524. static struct tcp_congestion_ops roccet_tcp __read_mostly = {
  525. .init = roccettcp_init,
  526. .ssthresh = roccettcp_recalc_ssthresh,
  527. .cong_avoid = roccettcp_cong_avoid,
  528. .set_state = roccettcp_state,
  529. .undo_cwnd = tcp_reno_undo_cwnd,
  530. .cwnd_event = roccettcp_cwnd_event,
  531. .pkts_acked = roccettcp_acked,
  532. .in_ack_event = roccet_in_ack_event,
  533. .owner = THIS_MODULE,
  534. .name = "roccet",
  535. };
  536. BTF_KFUNCS_START(tcp_roccet_check_kfunc_ids)
  537. BTF_ID_FLAGS(func, roccettcp_init)
  538. BTF_ID_FLAGS(func, roccettcp_recalc_ssthresh)
  539. BTF_ID_FLAGS(func, roccettcp_cong_avoid)
  540. BTF_ID_FLAGS(func, roccettcp_state)
  541. BTF_ID_FLAGS(func, roccettcp_cwnd_event)
  542. BTF_ID_FLAGS(func, roccettcp_acked)
  543. BTF_KFUNCS_END(tcp_roccet_check_kfunc_ids)
  544. static const struct btf_kfunc_id_set tcp_roccet_kfunc_set = {
  545. .owner = THIS_MODULE,
  546. .set = &tcp_roccet_check_kfunc_ids,
  547. };
  548. static int __init roccettcp_register(void)
  549. {
  550. int ret;
  551. BUILD_BUG_ON(sizeof(struct roccettcp) > ICSK_CA_PRIV_SIZE);
  552. /* Precompute a bunch of the scaling factors that are used per-packet
  553. * based on SRTT of 100ms
  554. */
  555. beta_scale =
  556. 8 * (BICTCP_BETA_SCALE + beta) / 3 / (BICTCP_BETA_SCALE - beta);
  557. cube_rtt_scale = (bic_scale * 10); /* 1024*c/rtt */
  558. /* calculate the "K" for (wmax-cwnd) = c/rtt * K^3
  559. * so K = cubic_root( (wmax-cwnd)*rtt/c )
  560. * the unit of K is bictcp_HZ=2^10, not HZ
  561. *
  562. * c = bic_scale >> 10
  563. * rtt = 100ms
  564. *
  565. * the following code has been designed and tested for
  566. * cwnd < 1 million packets
  567. * RTT < 100 seconds
  568. * HZ < 1,000,00 (corresponding to 10 nano-second)
  569. */
  570. /* 1/c * 2^2*bictcp_HZ * srtt */
  571. cube_factor = 1ull << (10 + 3 * BICTCP_HZ); /* 2^40 */
  572. /* divide by bic_scale and by constant Srtt (100ms) */
  573. do_div(cube_factor, bic_scale * 10);
  574. ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
  575. &tcp_roccet_kfunc_set);
  576. if (ret < 0)
  577. return ret;
  578. return tcp_register_congestion_control(&roccet_tcp);
  579. }
  580. static void __exit roccettcp_unregister(void)
  581. {
  582. tcp_unregister_congestion_control(&roccet_tcp);
  583. }
  584. module_init(roccettcp_register);
  585. module_exit(roccettcp_unregister);
  586. MODULE_AUTHOR("Lukas Prause, Tim Füchsel");
  587. MODULE_LICENSE("GPL");
  588. MODULE_DESCRIPTION("ROCCET TCP");
  589. MODULE_VERSION("1.0");