call_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. * Set the timer
  23. */
  24. void __rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why,
  25. ktime_t now)
  26. {
  27. unsigned long t_j, now_j = jiffies;
  28. ktime_t t;
  29. bool queue = false;
  30. if (call->state < RXRPC_CALL_COMPLETE) {
  31. t = call->expire_at;
  32. if (!ktime_after(t, now)) {
  33. trace_rxrpc_timer(call, why, now, now_j);
  34. queue = true;
  35. goto out;
  36. }
  37. if (!ktime_after(call->resend_at, now)) {
  38. call->resend_at = call->expire_at;
  39. if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
  40. queue = true;
  41. } else if (ktime_before(call->resend_at, t)) {
  42. t = call->resend_at;
  43. }
  44. if (!ktime_after(call->ack_at, now)) {
  45. call->ack_at = call->expire_at;
  46. if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
  47. queue = true;
  48. } else if (ktime_before(call->ack_at, t)) {
  49. t = call->ack_at;
  50. }
  51. if (!ktime_after(call->ping_at, now)) {
  52. call->ping_at = call->expire_at;
  53. if (!test_and_set_bit(RXRPC_CALL_EV_PING, &call->events))
  54. queue = true;
  55. } else if (ktime_before(call->ping_at, t)) {
  56. t = call->ping_at;
  57. }
  58. t_j = nsecs_to_jiffies(ktime_to_ns(ktime_sub(t, now)));
  59. t_j += jiffies;
  60. /* We have to make sure that the calculated jiffies value falls
  61. * at or after the nsec value, or we may loop ceaselessly
  62. * because the timer times out, but we haven't reached the nsec
  63. * timeout yet.
  64. */
  65. t_j++;
  66. if (call->timer.expires != t_j || !timer_pending(&call->timer)) {
  67. mod_timer(&call->timer, t_j);
  68. trace_rxrpc_timer(call, why, now, now_j);
  69. }
  70. }
  71. out:
  72. if (queue)
  73. rxrpc_queue_call(call);
  74. }
  75. /*
  76. * Set the timer
  77. */
  78. void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why,
  79. ktime_t now)
  80. {
  81. read_lock_bh(&call->state_lock);
  82. __rxrpc_set_timer(call, why, now);
  83. read_unlock_bh(&call->state_lock);
  84. }
  85. /*
  86. * Propose a PING ACK be sent.
  87. */
  88. static void rxrpc_propose_ping(struct rxrpc_call *call,
  89. bool immediate, bool background)
  90. {
  91. if (immediate) {
  92. if (background &&
  93. !test_and_set_bit(RXRPC_CALL_EV_PING, &call->events))
  94. rxrpc_queue_call(call);
  95. } else {
  96. ktime_t now = ktime_get_real();
  97. ktime_t ping_at = ktime_add_ms(now, rxrpc_idle_ack_delay);
  98. if (ktime_before(ping_at, call->ping_at)) {
  99. call->ping_at = ping_at;
  100. rxrpc_set_timer(call, rxrpc_timer_set_for_ping, now);
  101. }
  102. }
  103. }
  104. /*
  105. * propose an ACK be sent
  106. */
  107. static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
  108. u16 skew, u32 serial, bool immediate,
  109. bool background,
  110. enum rxrpc_propose_ack_trace why)
  111. {
  112. enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
  113. unsigned int expiry = rxrpc_soft_ack_delay;
  114. ktime_t now, ack_at;
  115. s8 prior = rxrpc_ack_priority[ack_reason];
  116. /* Pings are handled specially because we don't want to accidentally
  117. * lose a ping response by subsuming it into a ping.
  118. */
  119. if (ack_reason == RXRPC_ACK_PING) {
  120. rxrpc_propose_ping(call, immediate, background);
  121. goto trace;
  122. }
  123. /* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
  124. * numbers, but we don't alter the timeout.
  125. */
  126. _debug("prior %u %u vs %u %u",
  127. ack_reason, prior,
  128. call->ackr_reason, rxrpc_ack_priority[call->ackr_reason]);
  129. if (ack_reason == call->ackr_reason) {
  130. if (RXRPC_ACK_UPDATEABLE & (1 << ack_reason)) {
  131. outcome = rxrpc_propose_ack_update;
  132. call->ackr_serial = serial;
  133. call->ackr_skew = skew;
  134. }
  135. if (!immediate)
  136. goto trace;
  137. } else if (prior > rxrpc_ack_priority[call->ackr_reason]) {
  138. call->ackr_reason = ack_reason;
  139. call->ackr_serial = serial;
  140. call->ackr_skew = skew;
  141. } else {
  142. outcome = rxrpc_propose_ack_subsume;
  143. }
  144. switch (ack_reason) {
  145. case RXRPC_ACK_REQUESTED:
  146. if (rxrpc_requested_ack_delay < expiry)
  147. expiry = rxrpc_requested_ack_delay;
  148. if (serial == 1)
  149. immediate = false;
  150. break;
  151. case RXRPC_ACK_DELAY:
  152. if (rxrpc_soft_ack_delay < expiry)
  153. expiry = rxrpc_soft_ack_delay;
  154. break;
  155. case RXRPC_ACK_IDLE:
  156. if (rxrpc_idle_ack_delay < expiry)
  157. expiry = rxrpc_idle_ack_delay;
  158. break;
  159. default:
  160. immediate = true;
  161. break;
  162. }
  163. if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
  164. _debug("already scheduled");
  165. } else if (immediate || expiry == 0) {
  166. _debug("immediate ACK %lx", call->events);
  167. if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events) &&
  168. background)
  169. rxrpc_queue_call(call);
  170. } else {
  171. now = ktime_get_real();
  172. ack_at = ktime_add_ms(now, expiry);
  173. if (ktime_before(ack_at, call->ack_at)) {
  174. call->ack_at = ack_at;
  175. rxrpc_set_timer(call, rxrpc_timer_set_for_ack, now);
  176. }
  177. }
  178. trace:
  179. trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
  180. background, outcome);
  181. }
  182. /*
  183. * propose an ACK be sent, locking the call structure
  184. */
  185. void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
  186. u16 skew, u32 serial, bool immediate, bool background,
  187. enum rxrpc_propose_ack_trace why)
  188. {
  189. spin_lock_bh(&call->lock);
  190. __rxrpc_propose_ACK(call, ack_reason, skew, serial,
  191. immediate, background, why);
  192. spin_unlock_bh(&call->lock);
  193. }
  194. /*
  195. * Handle congestion being detected by the retransmit timeout.
  196. */
  197. static void rxrpc_congestion_timeout(struct rxrpc_call *call)
  198. {
  199. set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
  200. }
  201. /*
  202. * Perform retransmission of NAK'd and unack'd packets.
  203. */
  204. static void rxrpc_resend(struct rxrpc_call *call, ktime_t now)
  205. {
  206. struct rxrpc_skb_priv *sp;
  207. struct sk_buff *skb;
  208. rxrpc_seq_t cursor, seq, top;
  209. ktime_t max_age, oldest, ack_ts;
  210. int ix;
  211. u8 annotation, anno_type, retrans = 0, unacked = 0;
  212. _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
  213. max_age = ktime_sub_ms(now, rxrpc_resend_timeout);
  214. spin_lock_bh(&call->lock);
  215. cursor = call->tx_hard_ack;
  216. top = call->tx_top;
  217. ASSERT(before_eq(cursor, top));
  218. if (cursor == top)
  219. goto out_unlock;
  220. /* Scan the packet list without dropping the lock and decide which of
  221. * the packets in the Tx buffer we're going to resend and what the new
  222. * resend timeout will be.
  223. */
  224. oldest = now;
  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. annotation &= ~RXRPC_TX_ANNO_MASK;
  230. if (anno_type == RXRPC_TX_ANNO_ACK)
  231. continue;
  232. skb = call->rxtx_buffer[ix];
  233. rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
  234. sp = rxrpc_skb(skb);
  235. if (anno_type == RXRPC_TX_ANNO_UNACK) {
  236. if (ktime_after(skb->tstamp, max_age)) {
  237. if (ktime_before(skb->tstamp, oldest))
  238. oldest = skb->tstamp;
  239. continue;
  240. }
  241. if (!(annotation & RXRPC_TX_ANNO_RESENT))
  242. unacked++;
  243. }
  244. /* Okay, we need to retransmit a packet. */
  245. call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
  246. retrans++;
  247. trace_rxrpc_retransmit(call, seq, annotation | anno_type,
  248. ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
  249. }
  250. call->resend_at = ktime_add_ms(oldest, rxrpc_resend_timeout);
  251. if (unacked)
  252. rxrpc_congestion_timeout(call);
  253. /* If there was nothing that needed retransmission then it's likely
  254. * that an ACK got lost somewhere. Send a ping to find out instead of
  255. * retransmitting data.
  256. */
  257. if (!retrans) {
  258. rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now);
  259. spin_unlock_bh(&call->lock);
  260. ack_ts = ktime_sub(now, call->acks_latest_ts);
  261. if (ktime_to_ns(ack_ts) < call->peer->rtt)
  262. goto out;
  263. rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, 0, true, false,
  264. rxrpc_propose_ack_ping_for_lost_ack);
  265. rxrpc_send_ack_packet(call, true);
  266. goto out;
  267. }
  268. /* Now go through the Tx window and perform the retransmissions. We
  269. * have to drop the lock for each send. If an ACK comes in whilst the
  270. * lock is dropped, it may clear some of the retransmission markers for
  271. * packets that it soft-ACKs.
  272. */
  273. for (seq = cursor + 1; before_eq(seq, top); seq++) {
  274. ix = seq & RXRPC_RXTX_BUFF_MASK;
  275. annotation = call->rxtx_annotations[ix];
  276. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  277. if (anno_type != RXRPC_TX_ANNO_RETRANS)
  278. continue;
  279. skb = call->rxtx_buffer[ix];
  280. rxrpc_get_skb(skb, rxrpc_skb_tx_got);
  281. spin_unlock_bh(&call->lock);
  282. if (rxrpc_send_data_packet(call, skb, true) < 0) {
  283. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  284. return;
  285. }
  286. if (rxrpc_is_client_call(call))
  287. rxrpc_expose_client_call(call);
  288. rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
  289. spin_lock_bh(&call->lock);
  290. /* We need to clear the retransmit state, but there are two
  291. * things we need to be aware of: A new ACK/NAK might have been
  292. * received and the packet might have been hard-ACK'd (in which
  293. * case it will no longer be in the buffer).
  294. */
  295. if (after(seq, call->tx_hard_ack)) {
  296. annotation = call->rxtx_annotations[ix];
  297. anno_type = annotation & RXRPC_TX_ANNO_MASK;
  298. if (anno_type == RXRPC_TX_ANNO_RETRANS ||
  299. anno_type == RXRPC_TX_ANNO_NAK) {
  300. annotation &= ~RXRPC_TX_ANNO_MASK;
  301. annotation |= RXRPC_TX_ANNO_UNACK;
  302. }
  303. annotation |= RXRPC_TX_ANNO_RESENT;
  304. call->rxtx_annotations[ix] = annotation;
  305. }
  306. if (after(call->tx_hard_ack, seq))
  307. seq = call->tx_hard_ack;
  308. }
  309. out_unlock:
  310. spin_unlock_bh(&call->lock);
  311. out:
  312. _leave("");
  313. }
  314. /*
  315. * Handle retransmission and deferred ACK/abort generation.
  316. */
  317. void rxrpc_process_call(struct work_struct *work)
  318. {
  319. struct rxrpc_call *call =
  320. container_of(work, struct rxrpc_call, processor);
  321. ktime_t now;
  322. rxrpc_see_call(call);
  323. //printk("\n--------------------\n");
  324. _enter("{%d,%s,%lx}",
  325. call->debug_id, rxrpc_call_states[call->state], call->events);
  326. recheck_state:
  327. if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
  328. rxrpc_send_abort_packet(call);
  329. goto recheck_state;
  330. }
  331. if (call->state == RXRPC_CALL_COMPLETE) {
  332. del_timer_sync(&call->timer);
  333. rxrpc_notify_socket(call);
  334. goto out_put;
  335. }
  336. now = ktime_get_real();
  337. if (ktime_before(call->expire_at, now)) {
  338. rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME);
  339. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  340. goto recheck_state;
  341. }
  342. if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events)) {
  343. if (call->ackr_reason) {
  344. rxrpc_send_ack_packet(call, false);
  345. goto recheck_state;
  346. }
  347. }
  348. if (test_and_clear_bit(RXRPC_CALL_EV_PING, &call->events)) {
  349. rxrpc_send_ack_packet(call, true);
  350. goto recheck_state;
  351. }
  352. if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events)) {
  353. rxrpc_resend(call, now);
  354. goto recheck_state;
  355. }
  356. rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now);
  357. /* other events may have been raised since we started checking */
  358. if (call->events && call->state < RXRPC_CALL_COMPLETE) {
  359. __rxrpc_queue_call(call);
  360. goto out;
  361. }
  362. out_put:
  363. rxrpc_put_call(call, rxrpc_call_put);
  364. out:
  365. _leave("");
  366. }