input_rack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* RACK-TLP [RFC8958] Implementation
  3. *
  4. * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include "ar-internal.h"
  9. static bool rxrpc_rack_sent_after(ktime_t t1, rxrpc_seq_t seq1,
  10. ktime_t t2, rxrpc_seq_t seq2)
  11. {
  12. if (ktime_after(t1, t2))
  13. return true;
  14. return t1 == t2 && after(seq1, seq2);
  15. }
  16. /*
  17. * Mark a packet lost.
  18. */
  19. static void rxrpc_rack_mark_lost(struct rxrpc_call *call,
  20. struct rxrpc_txqueue *tq, unsigned int ix)
  21. {
  22. if (__test_and_set_bit(ix, &tq->segment_lost)) {
  23. if (__test_and_clear_bit(ix, &tq->segment_retransmitted))
  24. call->tx_nr_resent--;
  25. } else {
  26. call->tx_nr_lost++;
  27. }
  28. tq->segment_xmit_ts[ix] = UINT_MAX;
  29. }
  30. /*
  31. * Get the transmission time of a packet in the Tx queue.
  32. */
  33. static ktime_t rxrpc_get_xmit_ts(const struct rxrpc_txqueue *tq, unsigned int ix)
  34. {
  35. if (tq->segment_xmit_ts[ix] == UINT_MAX)
  36. return KTIME_MAX;
  37. return ktime_add_us(tq->xmit_ts_base, tq->segment_xmit_ts[ix]);
  38. }
  39. /*
  40. * Get a bitmask of nack bits for a queue segment and mask off any that aren't
  41. * yet reported.
  42. */
  43. static unsigned long rxrpc_tq_nacks(const struct rxrpc_txqueue *tq)
  44. {
  45. unsigned long nacks = ~tq->segment_acked;
  46. if (tq->nr_reported_acks < RXRPC_NR_TXQUEUE)
  47. nacks &= (1UL << tq->nr_reported_acks) - 1;
  48. return nacks;
  49. }
  50. /*
  51. * Update the RACK state for the most recently sent packet that has been
  52. * delivered [RFC8958 6.2 Step 2].
  53. */
  54. static void rxrpc_rack_update(struct rxrpc_call *call,
  55. struct rxrpc_ack_summary *summary,
  56. struct rxrpc_txqueue *tq,
  57. unsigned int ix)
  58. {
  59. rxrpc_seq_t seq = tq->qbase + ix;
  60. ktime_t xmit_ts = rxrpc_get_xmit_ts(tq, ix);
  61. ktime_t rtt = ktime_sub(call->acks_latest_ts, xmit_ts);
  62. if (__test_and_clear_bit(ix, &tq->segment_lost))
  63. call->tx_nr_lost--;
  64. if (test_bit(ix, &tq->segment_retransmitted)) {
  65. /* Use Rx.serial instead of TCP.ACK.ts_option.echo_reply. */
  66. if (before(call->acks_highest_serial, tq->segment_serial[ix]))
  67. return;
  68. if (rtt < minmax_get(&call->min_rtt))
  69. return;
  70. }
  71. /* The RACK algorithm requires the segment ACKs to be traversed in
  72. * order of segment transmission - but the only thing this seems to
  73. * matter for is that RACK.rtt is set to the rtt of the most recently
  74. * transmitted segment. We should be able to achieve the same by only
  75. * setting RACK.rtt if the xmit time is greater.
  76. */
  77. if (ktime_after(xmit_ts, call->rack_rtt_ts)) {
  78. call->rack_rtt = rtt;
  79. call->rack_rtt_ts = xmit_ts;
  80. }
  81. if (rxrpc_rack_sent_after(xmit_ts, seq, call->rack_xmit_ts, call->rack_end_seq)) {
  82. call->rack_rtt = rtt;
  83. call->rack_xmit_ts = xmit_ts;
  84. call->rack_end_seq = seq;
  85. }
  86. }
  87. /*
  88. * Detect data segment reordering [RFC8958 6.2 Step 3].
  89. */
  90. static void rxrpc_rack_detect_reordering(struct rxrpc_call *call,
  91. struct rxrpc_ack_summary *summary,
  92. struct rxrpc_txqueue *tq,
  93. unsigned int ix)
  94. {
  95. rxrpc_seq_t seq = tq->qbase + ix;
  96. /* Track the highest sequence number so far ACK'd. This is not
  97. * necessarily the same as ack.firstPacket + ack.nAcks - 1 as the peer
  98. * could put a NACK in the last SACK slot.
  99. */
  100. if (after(seq, call->rack_fack))
  101. call->rack_fack = seq;
  102. else if (before(seq, call->rack_fack) &&
  103. test_bit(ix, &tq->segment_retransmitted))
  104. call->rack_reordering_seen = true;
  105. }
  106. void rxrpc_input_rack_one(struct rxrpc_call *call,
  107. struct rxrpc_ack_summary *summary,
  108. struct rxrpc_txqueue *tq,
  109. unsigned int ix)
  110. {
  111. rxrpc_rack_update(call, summary, tq, ix);
  112. rxrpc_rack_detect_reordering(call, summary, tq, ix);
  113. }
  114. void rxrpc_input_rack(struct rxrpc_call *call,
  115. struct rxrpc_ack_summary *summary,
  116. struct rxrpc_txqueue *tq,
  117. unsigned long new_acks)
  118. {
  119. while (new_acks) {
  120. unsigned int ix = __ffs(new_acks);
  121. __clear_bit(ix, &new_acks);
  122. rxrpc_input_rack_one(call, summary, tq, ix);
  123. }
  124. trace_rxrpc_rack_update(call, summary);
  125. }
  126. /*
  127. * Update the reordering window [RFC8958 6.2 Step 4]. Returns the updated
  128. * duration of the reordering window.
  129. *
  130. * Note that the Rx protocol doesn't have a 'DSACK option' per se, but ACKs can
  131. * be given a 'DUPLICATE' reason with the serial number referring to the
  132. * duplicated DATA packet. Rx does not inform as to whether this was a
  133. * reception of the same packet twice or of a retransmission of a packet we
  134. * already received (though this could be determined by the transmitter based
  135. * on the serial number).
  136. */
  137. static ktime_t rxrpc_rack_update_reo_wnd(struct rxrpc_call *call,
  138. struct rxrpc_ack_summary *summary)
  139. {
  140. rxrpc_seq_t snd_una = call->acks_lowest_nak; /* Lowest unack'd seq */
  141. rxrpc_seq_t snd_nxt = call->tx_transmitted + 1; /* Next seq to be sent */
  142. bool have_dsack_option = summary->ack_reason == RXRPC_ACK_DUPLICATE;
  143. int dup_thresh = 3;
  144. /* DSACK-based reordering window adaptation */
  145. if (!call->rack_dsack_round_none &&
  146. after_eq(snd_una, call->rack_dsack_round))
  147. call->rack_dsack_round_none = true;
  148. /* Grow the reordering window per round that sees DSACK. Reset the
  149. * window after 16 DSACK-free recoveries.
  150. */
  151. if (call->rack_dsack_round_none && have_dsack_option) {
  152. call->rack_dsack_round_none = false;
  153. call->rack_dsack_round = snd_nxt;
  154. call->rack_reo_wnd_mult++;
  155. call->rack_reo_wnd_persist = 16;
  156. } else if (summary->exiting_fast_or_rto_recovery) {
  157. call->rack_reo_wnd_persist--;
  158. if (call->rack_reo_wnd_persist <= 0)
  159. call->rack_reo_wnd_mult = 1;
  160. }
  161. if (!call->rack_reordering_seen) {
  162. if (summary->in_fast_or_rto_recovery)
  163. return 0;
  164. if (call->acks_nr_sacks >= dup_thresh)
  165. return 0;
  166. }
  167. return us_to_ktime(umin(call->rack_reo_wnd_mult * minmax_get(&call->min_rtt) / 4,
  168. call->srtt_us >> 3));
  169. }
  170. /*
  171. * Detect losses [RFC8958 6.2 Step 5].
  172. */
  173. static ktime_t rxrpc_rack_detect_loss(struct rxrpc_call *call,
  174. struct rxrpc_ack_summary *summary)
  175. {
  176. struct rxrpc_txqueue *tq;
  177. ktime_t timeout = 0, lost_after, now = ktime_get_real();
  178. call->rack_reo_wnd = rxrpc_rack_update_reo_wnd(call, summary);
  179. lost_after = ktime_add(call->rack_rtt, call->rack_reo_wnd);
  180. trace_rxrpc_rack_scan_loss(call);
  181. for (tq = call->tx_queue; tq; tq = tq->next) {
  182. unsigned long nacks = rxrpc_tq_nacks(tq);
  183. if (after(tq->qbase, call->tx_transmitted))
  184. break;
  185. trace_rxrpc_rack_scan_loss_tq(call, tq, nacks);
  186. /* Skip ones marked lost but not yet retransmitted */
  187. nacks &= ~tq->segment_lost | tq->segment_retransmitted;
  188. while (nacks) {
  189. unsigned int ix = __ffs(nacks);
  190. rxrpc_seq_t seq = tq->qbase + ix;
  191. ktime_t remaining;
  192. ktime_t xmit_ts = rxrpc_get_xmit_ts(tq, ix);
  193. __clear_bit(ix, &nacks);
  194. if (rxrpc_rack_sent_after(call->rack_xmit_ts, call->rack_end_seq,
  195. xmit_ts, seq)) {
  196. remaining = ktime_sub(ktime_add(xmit_ts, lost_after), now);
  197. if (remaining <= 0) {
  198. rxrpc_rack_mark_lost(call, tq, ix);
  199. trace_rxrpc_rack_detect_loss(call, summary, seq);
  200. } else {
  201. timeout = max(remaining, timeout);
  202. }
  203. }
  204. }
  205. }
  206. return timeout;
  207. }
  208. /*
  209. * Detect losses and set a timer to retry the detection [RFC8958 6.2 Step 5].
  210. */
  211. void rxrpc_rack_detect_loss_and_arm_timer(struct rxrpc_call *call,
  212. struct rxrpc_ack_summary *summary)
  213. {
  214. ktime_t timeout = rxrpc_rack_detect_loss(call, summary);
  215. if (timeout) {
  216. call->rack_timer_mode = RXRPC_CALL_RACKTIMER_RACK_REORDER;
  217. call->rack_timo_at = ktime_add(ktime_get_real(), timeout);
  218. trace_rxrpc_rack_timer(call, timeout, false);
  219. trace_rxrpc_timer_set(call, timeout, rxrpc_timer_trace_rack_reo);
  220. }
  221. }
  222. /*
  223. * Handle RACK-TLP RTO expiration [RFC8958 6.3].
  224. */
  225. static void rxrpc_rack_mark_losses_on_rto(struct rxrpc_call *call)
  226. {
  227. struct rxrpc_txqueue *tq;
  228. rxrpc_seq_t snd_una = call->acks_lowest_nak; /* Lowest unack'd seq */
  229. ktime_t lost_after = ktime_add(call->rack_rtt, call->rack_reo_wnd);
  230. ktime_t deadline = ktime_sub(ktime_get_real(), lost_after);
  231. for (tq = call->tx_queue; tq; tq = tq->next) {
  232. unsigned long unacked = ~tq->segment_acked;
  233. trace_rxrpc_rack_mark_loss_tq(call, tq);
  234. while (unacked) {
  235. unsigned int ix = __ffs(unacked);
  236. rxrpc_seq_t seq = tq->qbase + ix;
  237. ktime_t xmit_ts = rxrpc_get_xmit_ts(tq, ix);
  238. if (after(seq, call->tx_transmitted))
  239. return;
  240. __clear_bit(ix, &unacked);
  241. if (seq == snd_una ||
  242. ktime_before(xmit_ts, deadline))
  243. rxrpc_rack_mark_lost(call, tq, ix);
  244. }
  245. }
  246. }
  247. /*
  248. * Calculate the TLP loss probe timeout (PTO) [RFC8958 7.2].
  249. */
  250. ktime_t rxrpc_tlp_calc_pto(struct rxrpc_call *call, ktime_t now)
  251. {
  252. unsigned int flight_size = rxrpc_tx_in_flight(call);
  253. ktime_t rto_at = ktime_add(call->tx_last_sent,
  254. rxrpc_get_rto_backoff(call, false));
  255. ktime_t pto;
  256. if (call->rtt_count > 0) {
  257. /* Use 2*SRTT as the timeout. */
  258. pto = ns_to_ktime(call->srtt_us * NSEC_PER_USEC / 4);
  259. if (flight_size)
  260. pto = ktime_add(pto, call->tlp_max_ack_delay);
  261. } else {
  262. pto = NSEC_PER_SEC;
  263. }
  264. if (ktime_after(ktime_add(now, pto), rto_at))
  265. pto = ktime_sub(rto_at, now);
  266. return pto;
  267. }
  268. /*
  269. * Send a TLP loss probe on PTO expiration [RFC8958 7.3].
  270. */
  271. void rxrpc_tlp_send_probe(struct rxrpc_call *call)
  272. {
  273. unsigned int in_flight = rxrpc_tx_in_flight(call);
  274. if (after_eq(call->acks_hard_ack, call->tx_transmitted))
  275. return; /* Everything we transmitted has been acked. */
  276. /* There must be no other loss probe still in flight and we need to
  277. * have taken a new RTT sample since last probe or the start of
  278. * connection.
  279. */
  280. if (!call->tlp_serial &&
  281. call->tlp_rtt_taken != call->rtt_taken) {
  282. call->tlp_is_retrans = false;
  283. if (after(call->send_top, call->tx_transmitted) &&
  284. rxrpc_tx_window_space(call) > 0) {
  285. /* Transmit the lowest-sequence unsent DATA */
  286. call->tx_last_serial = 0;
  287. rxrpc_transmit_some_data(call, 1, rxrpc_txdata_tlp_new_data);
  288. call->tlp_serial = call->tx_last_serial;
  289. call->tlp_seq = call->tx_transmitted;
  290. trace_rxrpc_tlp_probe(call, rxrpc_tlp_probe_trace_transmit_new);
  291. in_flight = rxrpc_tx_in_flight(call);
  292. } else {
  293. /* Retransmit the highest-sequence DATA sent */
  294. call->tx_last_serial = 0;
  295. rxrpc_resend_tlp(call);
  296. call->tlp_is_retrans = true;
  297. trace_rxrpc_tlp_probe(call, rxrpc_tlp_probe_trace_retransmit);
  298. }
  299. } else {
  300. trace_rxrpc_tlp_probe(call, rxrpc_tlp_probe_trace_busy);
  301. }
  302. if (in_flight != 0) {
  303. ktime_t rto = rxrpc_get_rto_backoff(call, false);
  304. call->rack_timer_mode = RXRPC_CALL_RACKTIMER_RTO;
  305. call->rack_timo_at = ktime_add(ktime_get_real(), rto);
  306. trace_rxrpc_rack_timer(call, rto, false);
  307. trace_rxrpc_timer_set(call, rto, rxrpc_timer_trace_rack_rto);
  308. }
  309. }
  310. /*
  311. * Detect losses using the ACK of a TLP loss probe [RFC8958 7.4].
  312. */
  313. void rxrpc_tlp_process_ack(struct rxrpc_call *call, struct rxrpc_ack_summary *summary)
  314. {
  315. if (!call->tlp_serial || after(call->tlp_seq, call->acks_hard_ack))
  316. return;
  317. if (!call->tlp_is_retrans) {
  318. /* TLP of new data delivered */
  319. trace_rxrpc_tlp_ack(call, summary, rxrpc_tlp_ack_trace_new_data);
  320. call->tlp_serial = 0;
  321. } else if (summary->ack_reason == RXRPC_ACK_DUPLICATE &&
  322. summary->acked_serial == call->tlp_serial) {
  323. /* General Case: Detected packet losses using RACK [7.4.1] */
  324. trace_rxrpc_tlp_ack(call, summary, rxrpc_tlp_ack_trace_dup_acked);
  325. call->tlp_serial = 0;
  326. } else if (after(call->acks_hard_ack, call->tlp_seq)) {
  327. /* Repaired the single loss */
  328. trace_rxrpc_tlp_ack(call, summary, rxrpc_tlp_ack_trace_hard_beyond);
  329. call->tlp_serial = 0;
  330. // TODO: Invoke congestion control to react to the loss
  331. // event the probe has repaired
  332. } else if (summary->tlp_probe_acked) {
  333. trace_rxrpc_tlp_ack(call, summary, rxrpc_tlp_ack_trace_acked);
  334. /* Special Case: Detected a single loss repaired by the loss
  335. * probe [7.4.2]
  336. */
  337. call->tlp_serial = 0;
  338. } else {
  339. trace_rxrpc_tlp_ack(call, summary, rxrpc_tlp_ack_trace_incomplete);
  340. }
  341. }
  342. /*
  343. * Handle RACK timer expiration; returns true to request a resend.
  344. */
  345. void rxrpc_rack_timer_expired(struct rxrpc_call *call, ktime_t overran_by)
  346. {
  347. struct rxrpc_ack_summary summary = {};
  348. enum rxrpc_rack_timer_mode mode = call->rack_timer_mode;
  349. trace_rxrpc_rack_timer(call, overran_by, true);
  350. call->rack_timer_mode = RXRPC_CALL_RACKTIMER_OFF;
  351. switch (mode) {
  352. case RXRPC_CALL_RACKTIMER_RACK_REORDER:
  353. rxrpc_rack_detect_loss_and_arm_timer(call, &summary);
  354. break;
  355. case RXRPC_CALL_RACKTIMER_TLP_PTO:
  356. rxrpc_tlp_send_probe(call);
  357. break;
  358. case RXRPC_CALL_RACKTIMER_RTO:
  359. // Might need to poke the congestion algo in some way
  360. rxrpc_rack_mark_losses_on_rto(call);
  361. break;
  362. //case RXRPC_CALL_RACKTIMER_ZEROWIN:
  363. default:
  364. pr_warn("Unexpected rack timer %u", mode);
  365. }
  366. }