call_event.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. void rxrpc_propose_ping(struct rxrpc_call *call, u32 serial,
  21. enum rxrpc_propose_ack_trace why)
  22. {
  23. ktime_t delay = ms_to_ktime(READ_ONCE(rxrpc_idle_ack_delay));
  24. ktime_t now = ktime_get_real();
  25. ktime_t ping_at = ktime_add(now, delay);
  26. trace_rxrpc_propose_ack(call, why, RXRPC_ACK_PING, serial);
  27. if (ktime_before(ping_at, call->ping_at)) {
  28. call->ping_at = ping_at;
  29. trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_ping);
  30. }
  31. }
  32. /*
  33. * Propose a DELAY ACK be sent in the future.
  34. */
  35. void rxrpc_propose_delay_ACK(struct rxrpc_call *call, rxrpc_serial_t serial,
  36. enum rxrpc_propose_ack_trace why)
  37. {
  38. ktime_t now = ktime_get_real(), delay;
  39. trace_rxrpc_propose_ack(call, why, RXRPC_ACK_DELAY, serial);
  40. if (call->srtt_us)
  41. delay = (call->srtt_us >> 3) * NSEC_PER_USEC;
  42. else
  43. delay = ms_to_ktime(READ_ONCE(rxrpc_soft_ack_delay));
  44. ktime_add_ms(delay, call->tx_backoff);
  45. call->delay_ack_at = ktime_add(now, delay);
  46. trace_rxrpc_timer_set(call, delay, rxrpc_timer_trace_delayed_ack);
  47. }
  48. /*
  49. * Retransmit one or more packets.
  50. */
  51. static bool rxrpc_retransmit_data(struct rxrpc_call *call,
  52. struct rxrpc_send_data_req *req)
  53. {
  54. struct rxrpc_txqueue *tq = req->tq;
  55. unsigned int ix = req->seq & RXRPC_TXQ_MASK;
  56. struct rxrpc_txbuf *txb = tq->bufs[ix];
  57. _enter("%x,%x,%x,%x", tq->qbase, req->seq, ix, txb->debug_id);
  58. req->retrans = true;
  59. trace_rxrpc_retransmit(call, req, txb);
  60. txb->flags |= RXRPC_TXBUF_RESENT;
  61. rxrpc_send_data_packet(call, req);
  62. rxrpc_inc_stat(call->rxnet, stat_tx_data_retrans);
  63. req->tq = NULL;
  64. req->n = 0;
  65. req->did_send = true;
  66. req->now = ktime_get_real();
  67. return true;
  68. }
  69. /*
  70. * Perform retransmission of NAK'd and unack'd packets.
  71. */
  72. static void rxrpc_resend(struct rxrpc_call *call)
  73. {
  74. struct rxrpc_send_data_req req = {
  75. .now = ktime_get_real(),
  76. .trace = rxrpc_txdata_retransmit,
  77. };
  78. struct rxrpc_txqueue *tq;
  79. _enter("{%d,%d}", call->tx_bottom, call->tx_top);
  80. trace_rxrpc_resend(call, call->acks_highest_serial);
  81. /* Scan the transmission queue, looking for lost packets. */
  82. for (tq = call->tx_queue; tq; tq = tq->next) {
  83. unsigned long lost = tq->segment_lost;
  84. if (after(tq->qbase, call->tx_transmitted))
  85. break;
  86. _debug("retr %16lx %u c=%08x [%x]",
  87. tq->segment_acked, tq->nr_reported_acks, call->debug_id, tq->qbase);
  88. _debug("lost %16lx", lost);
  89. trace_rxrpc_resend_lost(call, tq, lost);
  90. while (lost) {
  91. unsigned int ix = __ffs(lost);
  92. struct rxrpc_txbuf *txb = tq->bufs[ix];
  93. __clear_bit(ix, &lost);
  94. rxrpc_see_txbuf(txb, rxrpc_txbuf_see_lost);
  95. req.tq = tq;
  96. req.seq = tq->qbase + ix;
  97. req.n = 1;
  98. rxrpc_retransmit_data(call, &req);
  99. }
  100. }
  101. rxrpc_get_rto_backoff(call, req.did_send);
  102. _leave("");
  103. }
  104. /*
  105. * Resend the highest-seq DATA packet so far transmitted for RACK-TLP [RFC8985 7.3].
  106. */
  107. void rxrpc_resend_tlp(struct rxrpc_call *call)
  108. {
  109. struct rxrpc_send_data_req req = {
  110. .now = ktime_get_real(),
  111. .seq = call->tx_transmitted,
  112. .n = 1,
  113. .tlp_probe = true,
  114. .trace = rxrpc_txdata_tlp_retransmit,
  115. };
  116. /* There's a chance it'll be on the tail segment of the queue. */
  117. req.tq = READ_ONCE(call->tx_qtail);
  118. if (req.tq &&
  119. before(call->tx_transmitted, req.tq->qbase + RXRPC_NR_TXQUEUE)) {
  120. rxrpc_retransmit_data(call, &req);
  121. return;
  122. }
  123. for (req.tq = call->tx_queue; req.tq; req.tq = req.tq->next) {
  124. if (after_eq(call->tx_transmitted, req.tq->qbase) &&
  125. before(call->tx_transmitted, req.tq->qbase + RXRPC_NR_TXQUEUE)) {
  126. rxrpc_retransmit_data(call, &req);
  127. return;
  128. }
  129. }
  130. }
  131. /*
  132. * Start transmitting the reply to a service. This cancels the need to ACK the
  133. * request if we haven't yet done so.
  134. */
  135. static void rxrpc_begin_service_reply(struct rxrpc_call *call)
  136. {
  137. rxrpc_set_call_state(call, RXRPC_CALL_SERVER_SEND_REPLY);
  138. if (call->ackr_reason == RXRPC_ACK_DELAY)
  139. call->ackr_reason = 0;
  140. call->delay_ack_at = KTIME_MAX;
  141. trace_rxrpc_timer_can(call, rxrpc_timer_trace_delayed_ack);
  142. }
  143. /*
  144. * Close the transmission phase. After this point there is no more data to be
  145. * transmitted in the call.
  146. */
  147. static void rxrpc_close_tx_phase(struct rxrpc_call *call)
  148. {
  149. _debug("________awaiting reply/ACK__________");
  150. switch (__rxrpc_call_state(call)) {
  151. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  152. rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_REPLY);
  153. break;
  154. case RXRPC_CALL_SERVER_SEND_REPLY:
  155. rxrpc_set_call_state(call, RXRPC_CALL_SERVER_AWAIT_ACK);
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. /*
  162. * Transmit some as-yet untransmitted data, to a maximum of the supplied limit.
  163. */
  164. static void rxrpc_transmit_fresh_data(struct rxrpc_call *call, unsigned int limit,
  165. enum rxrpc_txdata_trace trace)
  166. {
  167. int space = rxrpc_tx_window_space(call);
  168. if (!test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
  169. if (call->send_top == call->tx_top)
  170. return;
  171. rxrpc_expose_client_call(call);
  172. }
  173. while (space > 0) {
  174. struct rxrpc_send_data_req req = {
  175. .now = ktime_get_real(),
  176. .seq = call->tx_transmitted + 1,
  177. .n = 0,
  178. .trace = trace,
  179. };
  180. struct rxrpc_txqueue *tq;
  181. struct rxrpc_txbuf *txb;
  182. rxrpc_seq_t send_top, seq;
  183. int limit = min(space, max(call->peer->pmtud_jumbo, 1));
  184. /* Order send_top before the contents of the new txbufs and
  185. * txqueue pointers
  186. */
  187. send_top = smp_load_acquire(&call->send_top);
  188. if (call->tx_top == send_top)
  189. break;
  190. trace_rxrpc_transmit(call, send_top, space);
  191. tq = call->tx_qtail;
  192. seq = call->tx_top;
  193. trace_rxrpc_tq(call, tq, seq, rxrpc_tq_decant);
  194. do {
  195. int ix;
  196. seq++;
  197. ix = seq & RXRPC_TXQ_MASK;
  198. if (!ix) {
  199. tq = tq->next;
  200. trace_rxrpc_tq(call, tq, seq, rxrpc_tq_decant_advance);
  201. }
  202. if (!req.tq)
  203. req.tq = tq;
  204. txb = tq->bufs[ix];
  205. req.n++;
  206. if (!txb->jumboable)
  207. break;
  208. } while (req.n < limit && before(seq, send_top));
  209. if (txb->flags & RXRPC_LAST_PACKET) {
  210. rxrpc_close_tx_phase(call);
  211. tq = NULL;
  212. }
  213. call->tx_qtail = tq;
  214. call->tx_top = seq;
  215. space -= req.n;
  216. rxrpc_send_data_packet(call, &req);
  217. }
  218. }
  219. void rxrpc_transmit_some_data(struct rxrpc_call *call, unsigned int limit,
  220. enum rxrpc_txdata_trace trace)
  221. {
  222. switch (__rxrpc_call_state(call)) {
  223. case RXRPC_CALL_SERVER_ACK_REQUEST:
  224. if (call->tx_bottom == READ_ONCE(call->send_top))
  225. return;
  226. rxrpc_begin_service_reply(call);
  227. fallthrough;
  228. case RXRPC_CALL_SERVER_SEND_REPLY:
  229. case RXRPC_CALL_CLIENT_SEND_REQUEST:
  230. if (!rxrpc_tx_window_space(call))
  231. return;
  232. if (call->tx_bottom == READ_ONCE(call->send_top)) {
  233. rxrpc_inc_stat(call->rxnet, stat_tx_data_underflow);
  234. return;
  235. }
  236. rxrpc_transmit_fresh_data(call, limit, trace);
  237. break;
  238. default:
  239. return;
  240. }
  241. }
  242. /*
  243. * Ping the other end to fill our RTT cache and to retrieve the rwind
  244. * and MTU parameters.
  245. */
  246. static void rxrpc_send_initial_ping(struct rxrpc_call *call)
  247. {
  248. if (call->rtt_count < 3 ||
  249. ktime_before(ktime_add_ms(call->rtt_last_req, 1000),
  250. ktime_get_real()))
  251. rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
  252. rxrpc_propose_ack_ping_for_params);
  253. }
  254. /*
  255. * Handle retransmission and deferred ACK/abort generation.
  256. */
  257. bool rxrpc_input_call_event(struct rxrpc_call *call)
  258. {
  259. struct sk_buff *skb;
  260. ktime_t now, t;
  261. bool did_receive = false, saw_ack = false;
  262. s32 abort_code;
  263. rxrpc_see_call(call, rxrpc_call_see_input);
  264. //printk("\n--------------------\n");
  265. _enter("{%d,%s,%lx}",
  266. call->debug_id, rxrpc_call_states[__rxrpc_call_state(call)],
  267. call->events);
  268. /* Handle abort request locklessly, vs rxrpc_propose_abort(). */
  269. abort_code = smp_load_acquire(&call->send_abort);
  270. if (abort_code) {
  271. rxrpc_abort_call(call, 0, call->send_abort, call->send_abort_err,
  272. call->send_abort_why);
  273. goto out;
  274. }
  275. do {
  276. skb = __skb_dequeue(&call->rx_queue);
  277. if (skb) {
  278. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  279. if (__rxrpc_call_is_complete(call) ||
  280. skb->mark == RXRPC_SKB_MARK_ERROR) {
  281. rxrpc_free_skb(skb, rxrpc_skb_put_call_rx);
  282. goto out;
  283. }
  284. saw_ack |= sp->hdr.type == RXRPC_PACKET_TYPE_ACK;
  285. rxrpc_input_call_packet(call, skb);
  286. rxrpc_free_skb(skb, rxrpc_skb_put_call_rx);
  287. did_receive = true;
  288. }
  289. t = ktime_sub(call->rack_timo_at, ktime_get_real());
  290. if (t <= 0) {
  291. trace_rxrpc_timer_exp(call, t,
  292. rxrpc_timer_trace_rack_off + call->rack_timer_mode);
  293. call->rack_timo_at = KTIME_MAX;
  294. rxrpc_rack_timer_expired(call, t);
  295. }
  296. } while (!skb_queue_empty(&call->rx_queue));
  297. /* If we see our async-event poke, check for timeout trippage. */
  298. now = ktime_get_real();
  299. t = ktime_sub(call->expect_rx_by, now);
  300. if (t <= 0) {
  301. trace_rxrpc_timer_exp(call, t, rxrpc_timer_trace_expect_rx);
  302. goto expired;
  303. }
  304. t = ktime_sub(call->expect_req_by, now);
  305. if (t <= 0) {
  306. call->expect_req_by = KTIME_MAX;
  307. if (__rxrpc_call_state(call) == RXRPC_CALL_SERVER_RECV_REQUEST) {
  308. trace_rxrpc_timer_exp(call, t, rxrpc_timer_trace_idle);
  309. goto expired;
  310. }
  311. }
  312. t = ktime_sub(READ_ONCE(call->expect_term_by), now);
  313. if (t <= 0) {
  314. trace_rxrpc_timer_exp(call, t, rxrpc_timer_trace_hard);
  315. goto expired;
  316. }
  317. t = ktime_sub(call->delay_ack_at, now);
  318. if (t <= 0) {
  319. trace_rxrpc_timer_exp(call, t, rxrpc_timer_trace_delayed_ack);
  320. call->delay_ack_at = KTIME_MAX;
  321. rxrpc_send_ACK(call, RXRPC_ACK_DELAY, 0,
  322. rxrpc_propose_ack_delayed_ack);
  323. }
  324. t = ktime_sub(call->ping_at, now);
  325. if (t <= 0) {
  326. trace_rxrpc_timer_exp(call, t, rxrpc_timer_trace_ping);
  327. call->ping_at = KTIME_MAX;
  328. rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
  329. rxrpc_propose_ack_ping_for_keepalive);
  330. }
  331. now = ktime_get_real();
  332. t = ktime_sub(call->keepalive_at, now);
  333. if (t <= 0) {
  334. trace_rxrpc_timer_exp(call, t, rxrpc_timer_trace_keepalive);
  335. call->keepalive_at = KTIME_MAX;
  336. rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
  337. rxrpc_propose_ack_ping_for_keepalive);
  338. }
  339. if (test_and_clear_bit(RXRPC_CALL_EV_INITIAL_PING, &call->events))
  340. rxrpc_send_initial_ping(call);
  341. rxrpc_transmit_some_data(call, UINT_MAX, rxrpc_txdata_new_data);
  342. if (saw_ack)
  343. rxrpc_congestion_degrade(call);
  344. if (did_receive &&
  345. (__rxrpc_call_state(call) == RXRPC_CALL_CLIENT_SEND_REQUEST ||
  346. __rxrpc_call_state(call) == RXRPC_CALL_SERVER_SEND_REPLY)) {
  347. t = ktime_sub(call->rack_timo_at, ktime_get_real());
  348. trace_rxrpc_rack(call, t);
  349. }
  350. /* Process events */
  351. if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
  352. rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
  353. rxrpc_propose_ack_ping_for_lost_ack);
  354. if (call->tx_nr_lost > 0 &&
  355. __rxrpc_call_state(call) != RXRPC_CALL_CLIENT_RECV_REPLY &&
  356. !test_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags))
  357. rxrpc_resend(call);
  358. if (test_and_clear_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags))
  359. rxrpc_send_ACK(call, RXRPC_ACK_IDLE, 0,
  360. rxrpc_propose_ack_rx_idle);
  361. if (call->ackr_nr_unacked > 2) {
  362. if (call->rtt_count < 3)
  363. rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
  364. rxrpc_propose_ack_ping_for_rtt);
  365. else if (ktime_before(ktime_add_ms(call->rtt_last_req, 1000),
  366. ktime_get_real()))
  367. rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
  368. rxrpc_propose_ack_ping_for_old_rtt);
  369. else
  370. rxrpc_send_ACK(call, RXRPC_ACK_IDLE, 0,
  371. rxrpc_propose_ack_input_data);
  372. }
  373. /* Make sure the timer is restarted */
  374. if (!__rxrpc_call_is_complete(call)) {
  375. ktime_t next = READ_ONCE(call->expect_term_by), delay;
  376. #define set(T) { ktime_t _t = (T); if (ktime_before(_t, next)) next = _t; }
  377. set(call->expect_req_by);
  378. set(call->expect_rx_by);
  379. set(call->delay_ack_at);
  380. set(call->rack_timo_at);
  381. set(call->keepalive_at);
  382. set(call->ping_at);
  383. now = ktime_get_real();
  384. delay = ktime_sub(next, now);
  385. if (delay <= 0) {
  386. rxrpc_poke_call(call, rxrpc_call_poke_timer_now);
  387. } else {
  388. unsigned long nowj = jiffies, delayj, nextj;
  389. delayj = umax(nsecs_to_jiffies(delay), 1);
  390. nextj = nowj + delayj;
  391. if (time_before(nextj, call->timer.expires) ||
  392. !timer_pending(&call->timer)) {
  393. trace_rxrpc_timer_restart(call, delay, delayj);
  394. timer_reduce(&call->timer, nextj);
  395. }
  396. }
  397. }
  398. out:
  399. if (__rxrpc_call_is_complete(call)) {
  400. timer_delete_sync(&call->timer);
  401. if (!test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
  402. rxrpc_disconnect_call(call);
  403. if (call->security)
  404. call->security->free_call_crypto(call);
  405. } else {
  406. if (did_receive &&
  407. call->peer->ackr_adv_pmtud &&
  408. call->peer->pmtud_pending)
  409. rxrpc_send_probe_for_pmtud(call);
  410. }
  411. _leave("");
  412. return true;
  413. expired:
  414. if (test_bit(RXRPC_CALL_RX_HEARD, &call->flags) &&
  415. (int)call->conn->hi_serial - (int)call->rx_serial > 0) {
  416. trace_rxrpc_call_reset(call);
  417. rxrpc_abort_call(call, 0, RX_CALL_DEAD, -ECONNRESET,
  418. rxrpc_abort_call_reset);
  419. } else {
  420. rxrpc_abort_call(call, 0, RX_CALL_TIMEOUT, -ETIME,
  421. rxrpc_abort_call_timeout);
  422. }
  423. goto out;
  424. }