call_event.c 13 KB

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