call_event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 += READ_ONCE(call->tx_backoff);
  113. ack_at += now;
  114. if (time_before(ack_at, call->ack_at)) {
  115. WRITE_ONCE(call->ack_at, ack_at);
  116. rxrpc_reduce_call_timer(call, ack_at, now,
  117. rxrpc_timer_set_for_ack);
  118. }
  119. }
  120. trace:
  121. trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
  122. background, outcome);
  123. }
  124. /*
  125. * propose an ACK be sent, locking the call structure
  126. */
  127. void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
  128. u16 skew, u32 serial, bool immediate, bool background,
  129. enum rxrpc_propose_ack_trace why)
  130. {
  131. spin_lock_bh(&call->lock);
  132. __rxrpc_propose_ACK(call, ack_reason, skew, serial,
  133. immediate, background, why);
  134. spin_unlock_bh(&call->lock);
  135. }
  136. /*
  137. * Handle congestion being detected by the retransmit timeout.
  138. */
  139. static void rxrpc_congestion_timeout(struct rxrpc_call *call)
  140. {
  141. set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
  142. }
  143. /*
  144. * Perform retransmission of NAK'd and unack'd packets.
  145. */
  146. static void rxrpc_resend(struct rxrpc_call *call, unsigned long now_j)
  147. {
  148. struct sk_buff *skb;
  149. unsigned long resend_at;
  150. rxrpc_seq_t cursor, seq, top;
  151. ktime_t now, max_age, oldest, ack_ts, timeout, min_timeo;
  152. int ix;
  153. u8 annotation, anno_type, retrans = 0, unacked = 0;
  154. _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
  155. if (call->peer->rtt_usage > 1)
  156. timeout = ns_to_ktime(call->peer->rtt * 3 / 2);
  157. else
  158. timeout = ms_to_ktime(rxrpc_resend_timeout);
  159. min_timeo = ns_to_ktime((1000000000 / HZ) * 4);
  160. if (ktime_before(timeout, min_timeo))
  161. timeout = min_timeo;
  162. now = ktime_get_real();
  163. max_age = ktime_sub(now, timeout);
  164. spin_lock_bh(&call->lock);
  165. cursor = call->tx_hard_ack;
  166. top = call->tx_top;
  167. ASSERT(before_eq(cursor, top));
  168. if (cursor == top)
  169. goto out_unlock;
  170. /* Scan the packet list without dropping the lock and decide which of
  171. * the packets in the Tx buffer we're going to resend and what the new
  172. * resend timeout will be.
  173. */
  174. trace_rxrpc_resend(call, (cursor + 1) & RXRPC_RXTX_BUFF_MASK);
  175. oldest = now;
  176. for (seq = cursor + 1; before_eq(seq, top); seq++) {
  177. ix = seq & RXRPC_RXTX_BUFF_MASK;
  178. annotation = call->rxtx_annotations[ix];
  179. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  180. annotation &= ~RXRPC_TX_ANNO_MASK;
  181. if (anno_type == RXRPC_TX_ANNO_ACK)
  182. continue;
  183. skb = call->rxtx_buffer[ix];
  184. rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
  185. if (anno_type == RXRPC_TX_ANNO_UNACK) {
  186. if (ktime_after(skb->tstamp, max_age)) {
  187. if (ktime_before(skb->tstamp, oldest))
  188. oldest = skb->tstamp;
  189. continue;
  190. }
  191. if (!(annotation & RXRPC_TX_ANNO_RESENT))
  192. unacked++;
  193. }
  194. /* Okay, we need to retransmit a packet. */
  195. call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
  196. retrans++;
  197. trace_rxrpc_retransmit(call, seq, annotation | anno_type,
  198. ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
  199. }
  200. resend_at = nsecs_to_jiffies(ktime_to_ns(ktime_sub(now, oldest)));
  201. resend_at += jiffies + rxrpc_resend_timeout;
  202. WRITE_ONCE(call->resend_at, resend_at);
  203. if (unacked)
  204. rxrpc_congestion_timeout(call);
  205. /* If there was nothing that needed retransmission then it's likely
  206. * that an ACK got lost somewhere. Send a ping to find out instead of
  207. * retransmitting data.
  208. */
  209. if (!retrans) {
  210. rxrpc_reduce_call_timer(call, resend_at, now_j,
  211. rxrpc_timer_set_for_resend);
  212. spin_unlock_bh(&call->lock);
  213. ack_ts = ktime_sub(now, call->acks_latest_ts);
  214. if (ktime_to_ns(ack_ts) < call->peer->rtt)
  215. goto out;
  216. rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, 0, true, false,
  217. rxrpc_propose_ack_ping_for_lost_ack);
  218. rxrpc_send_ack_packet(call, true, NULL);
  219. goto out;
  220. }
  221. /* Now go through the Tx window and perform the retransmissions. We
  222. * have to drop the lock for each send. If an ACK comes in whilst the
  223. * lock is dropped, it may clear some of the retransmission markers for
  224. * packets that it soft-ACKs.
  225. */
  226. for (seq = cursor + 1; before_eq(seq, top); seq++) {
  227. ix = seq & RXRPC_RXTX_BUFF_MASK;
  228. annotation = call->rxtx_annotations[ix];
  229. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  230. if (anno_type != RXRPC_TX_ANNO_RETRANS)
  231. continue;
  232. skb = call->rxtx_buffer[ix];
  233. rxrpc_get_skb(skb, rxrpc_skb_tx_got);
  234. spin_unlock_bh(&call->lock);
  235. if (rxrpc_send_data_packet(call, skb, true) < 0) {
  236. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  237. return;
  238. }
  239. if (rxrpc_is_client_call(call))
  240. rxrpc_expose_client_call(call);
  241. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  242. spin_lock_bh(&call->lock);
  243. /* We need to clear the retransmit state, but there are two
  244. * things we need to be aware of: A new ACK/NAK might have been
  245. * received and the packet might have been hard-ACK'd (in which
  246. * case it will no longer be in the buffer).
  247. */
  248. if (after(seq, call->tx_hard_ack)) {
  249. annotation = call->rxtx_annotations[ix];
  250. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  251. if (anno_type == RXRPC_TX_ANNO_RETRANS ||
  252. anno_type == RXRPC_TX_ANNO_NAK) {
  253. annotation &= ~RXRPC_TX_ANNO_MASK;
  254. annotation |= RXRPC_TX_ANNO_UNACK;
  255. }
  256. annotation |= RXRPC_TX_ANNO_RESENT;
  257. call->rxtx_annotations[ix] = annotation;
  258. }
  259. if (after(call->tx_hard_ack, seq))
  260. seq = call->tx_hard_ack;
  261. }
  262. out_unlock:
  263. spin_unlock_bh(&call->lock);
  264. out:
  265. _leave("");
  266. }
  267. /*
  268. * Handle retransmission and deferred ACK/abort generation.
  269. */
  270. void rxrpc_process_call(struct work_struct *work)
  271. {
  272. struct rxrpc_call *call =
  273. container_of(work, struct rxrpc_call, processor);
  274. rxrpc_serial_t *send_ack;
  275. unsigned long now, next, t;
  276. unsigned int iterations = 0;
  277. rxrpc_see_call(call);
  278. //printk("\n--------------------\n");
  279. _enter("{%d,%s,%lx}",
  280. call->debug_id, rxrpc_call_states[call->state], call->events);
  281. recheck_state:
  282. /* Limit the number of times we do this before returning to the manager */
  283. iterations++;
  284. if (iterations > 5)
  285. goto requeue;
  286. if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
  287. rxrpc_send_abort_packet(call);
  288. goto recheck_state;
  289. }
  290. if (call->state == RXRPC_CALL_COMPLETE) {
  291. del_timer_sync(&call->timer);
  292. rxrpc_notify_socket(call);
  293. goto out_put;
  294. }
  295. /* Work out if any timeouts tripped */
  296. now = jiffies;
  297. t = READ_ONCE(call->expect_rx_by);
  298. if (time_after_eq(now, t)) {
  299. trace_rxrpc_timer(call, rxrpc_timer_exp_normal, now);
  300. set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
  301. }
  302. t = READ_ONCE(call->expect_req_by);
  303. if (call->state == RXRPC_CALL_SERVER_RECV_REQUEST &&
  304. time_after_eq(now, t)) {
  305. trace_rxrpc_timer(call, rxrpc_timer_exp_idle, now);
  306. set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
  307. }
  308. t = READ_ONCE(call->expect_term_by);
  309. if (time_after_eq(now, t)) {
  310. trace_rxrpc_timer(call, rxrpc_timer_exp_hard, now);
  311. set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
  312. }
  313. t = READ_ONCE(call->ack_at);
  314. if (time_after_eq(now, t)) {
  315. trace_rxrpc_timer(call, rxrpc_timer_exp_ack, now);
  316. cmpxchg(&call->ack_at, t, now + MAX_JIFFY_OFFSET);
  317. set_bit(RXRPC_CALL_EV_ACK, &call->events);
  318. }
  319. t = READ_ONCE(call->ack_lost_at);
  320. if (time_after_eq(now, t)) {
  321. trace_rxrpc_timer(call, rxrpc_timer_exp_lost_ack, now);
  322. cmpxchg(&call->ack_lost_at, t, now + MAX_JIFFY_OFFSET);
  323. set_bit(RXRPC_CALL_EV_ACK_LOST, &call->events);
  324. }
  325. t = READ_ONCE(call->keepalive_at);
  326. if (time_after_eq(now, t)) {
  327. trace_rxrpc_timer(call, rxrpc_timer_exp_keepalive, now);
  328. cmpxchg(&call->keepalive_at, t, now + MAX_JIFFY_OFFSET);
  329. rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, 0, true, true,
  330. rxrpc_propose_ack_ping_for_keepalive);
  331. set_bit(RXRPC_CALL_EV_PING, &call->events);
  332. }
  333. t = READ_ONCE(call->ping_at);
  334. if (time_after_eq(now, t)) {
  335. trace_rxrpc_timer(call, rxrpc_timer_exp_ping, now);
  336. cmpxchg(&call->ping_at, t, now + MAX_JIFFY_OFFSET);
  337. set_bit(RXRPC_CALL_EV_PING, &call->events);
  338. }
  339. t = READ_ONCE(call->resend_at);
  340. if (time_after_eq(now, t)) {
  341. trace_rxrpc_timer(call, rxrpc_timer_exp_resend, now);
  342. cmpxchg(&call->resend_at, t, now + MAX_JIFFY_OFFSET);
  343. set_bit(RXRPC_CALL_EV_RESEND, &call->events);
  344. }
  345. /* Process events */
  346. if (test_and_clear_bit(RXRPC_CALL_EV_EXPIRED, &call->events)) {
  347. if (test_bit(RXRPC_CALL_RX_HEARD, &call->flags) &&
  348. (int)call->conn->hi_serial - (int)call->rx_serial > 0) {
  349. trace_rxrpc_call_reset(call);
  350. rxrpc_abort_call("EXP", call, 0, RX_USER_ABORT, -ECONNRESET);
  351. } else {
  352. rxrpc_abort_call("EXP", call, 0, RX_USER_ABORT, -ETIME);
  353. }
  354. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  355. goto recheck_state;
  356. }
  357. send_ack = NULL;
  358. if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events)) {
  359. call->acks_lost_top = call->tx_top;
  360. rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, 0, true, false,
  361. rxrpc_propose_ack_ping_for_lost_ack);
  362. send_ack = &call->acks_lost_ping;
  363. }
  364. if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events) ||
  365. send_ack) {
  366. if (call->ackr_reason) {
  367. rxrpc_send_ack_packet(call, false, send_ack);
  368. goto recheck_state;
  369. }
  370. }
  371. if (test_and_clear_bit(RXRPC_CALL_EV_PING, &call->events)) {
  372. rxrpc_send_ack_packet(call, true, NULL);
  373. goto recheck_state;
  374. }
  375. if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events)) {
  376. rxrpc_resend(call, now);
  377. goto recheck_state;
  378. }
  379. /* Make sure the timer is restarted */
  380. next = call->expect_rx_by;
  381. #define set(T) { t = READ_ONCE(T); if (time_before(t, next)) next = t; }
  382. set(call->expect_req_by);
  383. set(call->expect_term_by);
  384. set(call->ack_at);
  385. set(call->ack_lost_at);
  386. set(call->resend_at);
  387. set(call->keepalive_at);
  388. set(call->ping_at);
  389. now = jiffies;
  390. if (time_after_eq(now, next))
  391. goto recheck_state;
  392. rxrpc_reduce_call_timer(call, next, now, rxrpc_timer_restart);
  393. /* other events may have been raised since we started checking */
  394. if (call->events && call->state < RXRPC_CALL_COMPLETE)
  395. goto requeue;
  396. out_put:
  397. rxrpc_put_call(call, rxrpc_call_put);
  398. out:
  399. _leave("");
  400. return;
  401. requeue:
  402. __rxrpc_queue_call(call);
  403. goto out;
  404. }