call_event.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
  3. *
  4. * Copyright (C) 2007 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 <linux/module.h>
  9. #include <linux/circ_buf.h>
  10. #include <linux/net.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/slab.h>
  13. #include <linux/udp.h>
  14. #include <net/sock.h>
  15. #include <net/af_rxrpc.h>
  16. #include "ar-internal.h"
  17. /*
  18. * Propose a PING ACK be sent.
  19. */
  20. static void rxrpc_propose_ping(struct rxrpc_call *call,
  21. bool immediate, bool background)
  22. {
  23. if (immediate) {
  24. if (background &&
  25. !test_and_set_bit(RXRPC_CALL_EV_PING, &call->events))
  26. rxrpc_queue_call(call);
  27. } else {
  28. unsigned long now = jiffies;
  29. unsigned long ping_at = now + rxrpc_idle_ack_delay;
  30. if (time_before(ping_at, call->ping_at)) {
  31. WRITE_ONCE(call->ping_at, ping_at);
  32. rxrpc_reduce_call_timer(call, ping_at, now,
  33. rxrpc_timer_set_for_ping);
  34. }
  35. }
  36. }
  37. /*
  38. * propose an ACK be sent
  39. */
  40. static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
  41. u32 serial, bool immediate, bool background,
  42. enum rxrpc_propose_ack_trace why)
  43. {
  44. enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
  45. unsigned long expiry = rxrpc_soft_ack_delay;
  46. s8 prior = rxrpc_ack_priority[ack_reason];
  47. /* Pings are handled specially because we don't want to accidentally
  48. * lose a ping response by subsuming it into a ping.
  49. */
  50. if (ack_reason == RXRPC_ACK_PING) {
  51. rxrpc_propose_ping(call, immediate, background);
  52. goto trace;
  53. }
  54. /* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
  55. * numbers, but we don't alter the timeout.
  56. */
  57. _debug("prior %u %u vs %u %u",
  58. ack_reason, prior,
  59. call->ackr_reason, rxrpc_ack_priority[call->ackr_reason]);
  60. if (ack_reason == call->ackr_reason) {
  61. if (RXRPC_ACK_UPDATEABLE & (1 << ack_reason)) {
  62. outcome = rxrpc_propose_ack_update;
  63. call->ackr_serial = serial;
  64. }
  65. if (!immediate)
  66. goto trace;
  67. } else if (prior > rxrpc_ack_priority[call->ackr_reason]) {
  68. call->ackr_reason = ack_reason;
  69. call->ackr_serial = serial;
  70. } else {
  71. outcome = rxrpc_propose_ack_subsume;
  72. }
  73. switch (ack_reason) {
  74. case RXRPC_ACK_REQUESTED:
  75. if (rxrpc_requested_ack_delay < expiry)
  76. expiry = rxrpc_requested_ack_delay;
  77. if (serial == 1)
  78. immediate = false;
  79. break;
  80. case RXRPC_ACK_DELAY:
  81. if (rxrpc_soft_ack_delay < expiry)
  82. expiry = rxrpc_soft_ack_delay;
  83. break;
  84. case RXRPC_ACK_IDLE:
  85. if (rxrpc_idle_ack_delay < expiry)
  86. expiry = rxrpc_idle_ack_delay;
  87. break;
  88. default:
  89. immediate = true;
  90. break;
  91. }
  92. if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
  93. _debug("already scheduled");
  94. } else if (immediate || expiry == 0) {
  95. _debug("immediate ACK %lx", call->events);
  96. if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events) &&
  97. background)
  98. rxrpc_queue_call(call);
  99. } else {
  100. unsigned long now = jiffies, ack_at;
  101. if (call->peer->srtt_us != 0)
  102. ack_at = usecs_to_jiffies(call->peer->srtt_us >> 3);
  103. else
  104. ack_at = expiry;
  105. ack_at += READ_ONCE(call->tx_backoff);
  106. ack_at += now;
  107. if (time_before(ack_at, call->ack_at)) {
  108. WRITE_ONCE(call->ack_at, ack_at);
  109. rxrpc_reduce_call_timer(call, ack_at, now,
  110. rxrpc_timer_set_for_ack);
  111. }
  112. }
  113. trace:
  114. trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
  115. background, outcome);
  116. }
  117. /*
  118. * propose an ACK be sent, locking the call structure
  119. */
  120. void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
  121. u32 serial, bool immediate, bool background,
  122. enum rxrpc_propose_ack_trace why)
  123. {
  124. spin_lock_bh(&call->lock);
  125. __rxrpc_propose_ACK(call, ack_reason, serial,
  126. immediate, background, why);
  127. spin_unlock_bh(&call->lock);
  128. }
  129. /*
  130. * Handle congestion being detected by the retransmit timeout.
  131. */
  132. static void rxrpc_congestion_timeout(struct rxrpc_call *call)
  133. {
  134. set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
  135. }
  136. /*
  137. * Perform retransmission of NAK'd and unack'd packets.
  138. */
  139. static void rxrpc_resend(struct rxrpc_call *call, unsigned long now_j)
  140. {
  141. struct sk_buff *skb;
  142. unsigned long resend_at;
  143. rxrpc_seq_t cursor, seq, top;
  144. ktime_t now, max_age, oldest, ack_ts;
  145. int ix;
  146. u8 annotation, anno_type, retrans = 0, unacked = 0;
  147. _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
  148. now = ktime_get_real();
  149. max_age = ktime_sub(now, jiffies_to_usecs(call->peer->rto_j));
  150. spin_lock_bh(&call->lock);
  151. cursor = call->tx_hard_ack;
  152. top = call->tx_top;
  153. ASSERT(before_eq(cursor, top));
  154. if (cursor == top)
  155. goto out_unlock;
  156. /* Scan the packet list without dropping the lock and decide which of
  157. * the packets in the Tx buffer we're going to resend and what the new
  158. * resend timeout will be.
  159. */
  160. trace_rxrpc_resend(call, (cursor + 1) & RXRPC_RXTX_BUFF_MASK);
  161. oldest = now;
  162. for (seq = cursor + 1; before_eq(seq, top); seq++) {
  163. ix = seq & RXRPC_RXTX_BUFF_MASK;
  164. annotation = call->rxtx_annotations[ix];
  165. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  166. annotation &= ~RXRPC_TX_ANNO_MASK;
  167. if (anno_type == RXRPC_TX_ANNO_ACK)
  168. continue;
  169. skb = call->rxtx_buffer[ix];
  170. rxrpc_see_skb(skb, rxrpc_skb_seen);
  171. if (anno_type == RXRPC_TX_ANNO_UNACK) {
  172. if (ktime_after(skb->tstamp, max_age)) {
  173. if (ktime_before(skb->tstamp, oldest))
  174. oldest = skb->tstamp;
  175. continue;
  176. }
  177. if (!(annotation & RXRPC_TX_ANNO_RESENT))
  178. unacked++;
  179. }
  180. /* Okay, we need to retransmit a packet. */
  181. call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
  182. retrans++;
  183. trace_rxrpc_retransmit(call, seq, annotation | anno_type,
  184. ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
  185. }
  186. resend_at = nsecs_to_jiffies(ktime_to_ns(ktime_sub(now, oldest)));
  187. resend_at += jiffies + rxrpc_get_rto_backoff(call->peer, retrans);
  188. WRITE_ONCE(call->resend_at, resend_at);
  189. if (unacked)
  190. rxrpc_congestion_timeout(call);
  191. /* If there was nothing that needed retransmission then it's likely
  192. * that an ACK got lost somewhere. Send a ping to find out instead of
  193. * retransmitting data.
  194. */
  195. if (!retrans) {
  196. rxrpc_reduce_call_timer(call, resend_at, now_j,
  197. rxrpc_timer_set_for_resend);
  198. spin_unlock_bh(&call->lock);
  199. ack_ts = ktime_sub(now, call->acks_latest_ts);
  200. if (ktime_to_us(ack_ts) < (call->peer->srtt_us >> 3))
  201. goto out;
  202. rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, true, false,
  203. rxrpc_propose_ack_ping_for_lost_ack);
  204. rxrpc_send_ack_packet(call, true, NULL);
  205. goto out;
  206. }
  207. /* Now go through the Tx window and perform the retransmissions. We
  208. * have to drop the lock for each send. If an ACK comes in whilst the
  209. * lock is dropped, it may clear some of the retransmission markers for
  210. * packets that it soft-ACKs.
  211. */
  212. for (seq = cursor + 1; before_eq(seq, top); seq++) {
  213. ix = seq & RXRPC_RXTX_BUFF_MASK;
  214. annotation = call->rxtx_annotations[ix];
  215. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  216. if (anno_type != RXRPC_TX_ANNO_RETRANS)
  217. continue;
  218. /* We need to reset the retransmission state, but we need to do
  219. * so before we drop the lock as a new ACK/NAK may come in and
  220. * confuse things
  221. */
  222. annotation &= ~RXRPC_TX_ANNO_MASK;
  223. annotation |= RXRPC_TX_ANNO_UNACK | RXRPC_TX_ANNO_RESENT;
  224. call->rxtx_annotations[ix] = annotation;
  225. skb = call->rxtx_buffer[ix];
  226. if (!skb)
  227. continue;
  228. rxrpc_get_skb(skb, rxrpc_skb_got);
  229. spin_unlock_bh(&call->lock);
  230. if (rxrpc_send_data_packet(call, skb, true) < 0) {
  231. rxrpc_free_skb(skb, rxrpc_skb_freed);
  232. return;
  233. }
  234. if (rxrpc_is_client_call(call))
  235. rxrpc_expose_client_call(call);
  236. rxrpc_free_skb(skb, rxrpc_skb_freed);
  237. spin_lock_bh(&call->lock);
  238. if (after(call->tx_hard_ack, seq))
  239. seq = call->tx_hard_ack;
  240. }
  241. out_unlock:
  242. spin_unlock_bh(&call->lock);
  243. out:
  244. _leave("");
  245. }
  246. /*
  247. * Handle retransmission and deferred ACK/abort generation.
  248. */
  249. void rxrpc_process_call(struct work_struct *work)
  250. {
  251. struct rxrpc_call *call =
  252. container_of(work, struct rxrpc_call, processor);
  253. rxrpc_serial_t *send_ack;
  254. unsigned long now, next, t;
  255. unsigned int iterations = 0;
  256. rxrpc_see_call(call);
  257. //printk("\n--------------------\n");
  258. _enter("{%d,%s,%lx}",
  259. call->debug_id, rxrpc_call_states[call->state], call->events);
  260. recheck_state:
  261. /* Limit the number of times we do this before returning to the manager */
  262. iterations++;
  263. if (iterations > 5)
  264. goto requeue;
  265. if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
  266. rxrpc_send_abort_packet(call);
  267. goto recheck_state;
  268. }
  269. if (call->state == RXRPC_CALL_COMPLETE) {
  270. rxrpc_delete_call_timer(call);
  271. goto out_put;
  272. }
  273. /* Work out if any timeouts tripped */
  274. now = jiffies;
  275. t = READ_ONCE(call->expect_rx_by);
  276. if (time_after_eq(now, t)) {
  277. trace_rxrpc_timer(call, rxrpc_timer_exp_normal, now);
  278. set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
  279. }
  280. t = READ_ONCE(call->expect_req_by);
  281. if (call->state == RXRPC_CALL_SERVER_RECV_REQUEST &&
  282. time_after_eq(now, t)) {
  283. trace_rxrpc_timer(call, rxrpc_timer_exp_idle, now);
  284. set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
  285. }
  286. t = READ_ONCE(call->expect_term_by);
  287. if (time_after_eq(now, t)) {
  288. trace_rxrpc_timer(call, rxrpc_timer_exp_hard, now);
  289. set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
  290. }
  291. t = READ_ONCE(call->ack_at);
  292. if (time_after_eq(now, t)) {
  293. trace_rxrpc_timer(call, rxrpc_timer_exp_ack, now);
  294. cmpxchg(&call->ack_at, t, now + MAX_JIFFY_OFFSET);
  295. set_bit(RXRPC_CALL_EV_ACK, &call->events);
  296. }
  297. t = READ_ONCE(call->ack_lost_at);
  298. if (time_after_eq(now, t)) {
  299. trace_rxrpc_timer(call, rxrpc_timer_exp_lost_ack, now);
  300. cmpxchg(&call->ack_lost_at, t, now + MAX_JIFFY_OFFSET);
  301. set_bit(RXRPC_CALL_EV_ACK_LOST, &call->events);
  302. }
  303. t = READ_ONCE(call->keepalive_at);
  304. if (time_after_eq(now, t)) {
  305. trace_rxrpc_timer(call, rxrpc_timer_exp_keepalive, now);
  306. cmpxchg(&call->keepalive_at, t, now + MAX_JIFFY_OFFSET);
  307. rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, true, true,
  308. rxrpc_propose_ack_ping_for_keepalive);
  309. set_bit(RXRPC_CALL_EV_PING, &call->events);
  310. }
  311. t = READ_ONCE(call->ping_at);
  312. if (time_after_eq(now, t)) {
  313. trace_rxrpc_timer(call, rxrpc_timer_exp_ping, now);
  314. cmpxchg(&call->ping_at, t, now + MAX_JIFFY_OFFSET);
  315. set_bit(RXRPC_CALL_EV_PING, &call->events);
  316. }
  317. t = READ_ONCE(call->resend_at);
  318. if (time_after_eq(now, t)) {
  319. trace_rxrpc_timer(call, rxrpc_timer_exp_resend, now);
  320. cmpxchg(&call->resend_at, t, now + MAX_JIFFY_OFFSET);
  321. set_bit(RXRPC_CALL_EV_RESEND, &call->events);
  322. }
  323. /* Process events */
  324. if (test_and_clear_bit(RXRPC_CALL_EV_EXPIRED, &call->events)) {
  325. if (test_bit(RXRPC_CALL_RX_HEARD, &call->flags) &&
  326. (int)call->conn->hi_serial - (int)call->rx_serial > 0) {
  327. trace_rxrpc_call_reset(call);
  328. rxrpc_abort_call("EXP", call, 0, RX_USER_ABORT, -ECONNRESET);
  329. } else {
  330. rxrpc_abort_call("EXP", call, 0, RX_USER_ABORT, -ETIME);
  331. }
  332. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  333. goto recheck_state;
  334. }
  335. send_ack = NULL;
  336. if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events)) {
  337. call->acks_lost_top = call->tx_top;
  338. rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, true, false,
  339. rxrpc_propose_ack_ping_for_lost_ack);
  340. send_ack = &call->acks_lost_ping;
  341. }
  342. if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events) ||
  343. send_ack) {
  344. if (call->ackr_reason) {
  345. rxrpc_send_ack_packet(call, false, send_ack);
  346. goto recheck_state;
  347. }
  348. }
  349. if (test_and_clear_bit(RXRPC_CALL_EV_PING, &call->events)) {
  350. rxrpc_send_ack_packet(call, true, NULL);
  351. goto recheck_state;
  352. }
  353. if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events)) {
  354. rxrpc_resend(call, now);
  355. goto recheck_state;
  356. }
  357. /* Make sure the timer is restarted */
  358. next = call->expect_rx_by;
  359. #define set(T) { t = READ_ONCE(T); if (time_before(t, next)) next = t; }
  360. set(call->expect_req_by);
  361. set(call->expect_term_by);
  362. set(call->ack_at);
  363. set(call->ack_lost_at);
  364. set(call->resend_at);
  365. set(call->keepalive_at);
  366. set(call->ping_at);
  367. now = jiffies;
  368. if (time_after_eq(now, next))
  369. goto recheck_state;
  370. rxrpc_reduce_call_timer(call, next, now, rxrpc_timer_restart);
  371. /* other events may have been raised since we started checking */
  372. if (call->events && call->state < RXRPC_CALL_COMPLETE)
  373. goto requeue;
  374. out_put:
  375. rxrpc_put_call(call, rxrpc_call_put);
  376. out:
  377. _leave("");
  378. return;
  379. requeue:
  380. __rxrpc_queue_call(call);
  381. goto out;
  382. }