1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/circ_buf.h>
12 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/slab.h>
15 #include <linux/udp.h>
17 #include <net/af_rxrpc.h>
18 #include "ar-internal.h"
21 * Propose a PING ACK be sent.
23 void rxrpc_propose_ping(struct rxrpc_call *call, u32 serial,
24 enum rxrpc_propose_ack_trace why)
26 unsigned long now = jiffies;
27 unsigned long ping_at = now + rxrpc_idle_ack_delay;
29 if (time_before(ping_at, call->ping_at)) {
30 WRITE_ONCE(call->ping_at, ping_at);
31 rxrpc_reduce_call_timer(call, ping_at, now,
32 rxrpc_timer_set_for_ping);
33 trace_rxrpc_propose_ack(call, why, RXRPC_ACK_PING, serial);
38 * Propose a DELAY ACK be sent in the future.
40 void rxrpc_propose_delay_ACK(struct rxrpc_call *call, rxrpc_serial_t serial,
41 enum rxrpc_propose_ack_trace why)
43 unsigned long expiry = rxrpc_soft_ack_delay;
44 unsigned long now = jiffies, ack_at;
46 if (rxrpc_soft_ack_delay < expiry)
47 expiry = rxrpc_soft_ack_delay;
48 if (call->peer->srtt_us != 0)
49 ack_at = usecs_to_jiffies(call->peer->srtt_us >> 3);
53 ack_at += READ_ONCE(call->tx_backoff);
55 if (time_before(ack_at, call->delay_ack_at)) {
56 WRITE_ONCE(call->delay_ack_at, ack_at);
57 rxrpc_reduce_call_timer(call, ack_at, now,
58 rxrpc_timer_set_for_ack);
61 trace_rxrpc_propose_ack(call, why, RXRPC_ACK_DELAY, serial);
65 * Queue an ACK for immediate transmission.
67 void rxrpc_send_ACK(struct rxrpc_call *call, u8 ack_reason,
68 rxrpc_serial_t serial, enum rxrpc_propose_ack_trace why)
70 struct rxrpc_txbuf *txb;
72 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
75 rxrpc_inc_stat(call->rxnet, stat_tx_acks[ack_reason]);
77 txb = rxrpc_alloc_txbuf(call, RXRPC_PACKET_TYPE_ACK,
78 rcu_read_lock_held() ? GFP_ATOMIC | __GFP_NOWARN : GFP_NOFS);
86 txb->wire.type = RXRPC_PACKET_TYPE_ACK;
87 txb->wire.flags |= RXRPC_SLOW_START_OK;
88 txb->ack.bufferSpace = 0;
90 txb->ack.firstPacket = 0;
91 txb->ack.previousPacket = 0;
92 txb->ack.serial = htonl(serial);
93 txb->ack.reason = ack_reason;
96 trace_rxrpc_send_ack(call, why, ack_reason, serial);
97 rxrpc_send_ack_packet(call, txb);
98 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_ack_tx);
102 * Handle congestion being detected by the retransmit timeout.
104 static void rxrpc_congestion_timeout(struct rxrpc_call *call)
106 set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
110 * Perform retransmission of NAK'd and unack'd packets.
112 void rxrpc_resend(struct rxrpc_call *call, struct sk_buff *ack_skb)
114 struct rxrpc_ackpacket *ack = NULL;
115 struct rxrpc_txbuf *txb;
116 unsigned long resend_at;
117 rxrpc_seq_t transmitted = READ_ONCE(call->tx_transmitted);
118 ktime_t now, max_age, oldest, ack_ts;
119 bool unacked = false;
121 LIST_HEAD(retrans_queue);
123 _enter("{%d,%d}", call->acks_hard_ack, call->tx_top);
125 now = ktime_get_real();
126 max_age = ktime_sub_us(now, jiffies_to_usecs(call->peer->rto_j));
129 if (list_empty(&call->tx_buffer))
132 if (list_empty(&call->tx_buffer))
133 goto no_further_resend;
135 trace_rxrpc_resend(call, ack_skb);
136 txb = list_first_entry(&call->tx_buffer, struct rxrpc_txbuf, call_link);
138 /* Scan the soft ACK table without dropping the lock and resend any
139 * explicitly NAK'd packets.
142 ack = (void *)ack_skb->data + sizeof(struct rxrpc_wire_header);
144 for (i = 0; i < ack->nAcks; i++) {
147 if (ack->acks[i] & 1)
149 seq = ntohl(ack->firstPacket) + i;
150 if (after(txb->seq, transmitted))
152 if (after(txb->seq, seq))
153 continue; /* A new hard ACK probably came in */
154 list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
158 goto no_further_resend;
161 if (after(ntohl(txb->wire.serial), call->acks_highest_serial))
162 continue; /* Ack point not yet reached */
164 rxrpc_see_txbuf(txb, rxrpc_txbuf_see_unacked);
166 if (list_empty(&txb->tx_link)) {
167 list_add_tail(&txb->tx_link, &retrans_queue);
168 set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
171 trace_rxrpc_retransmit(call, txb->seq,
172 ktime_to_ns(ktime_sub(txb->last_sent,
175 if (list_is_last(&txb->call_link, &call->tx_buffer))
176 goto no_further_resend;
177 txb = list_next_entry(txb, call_link);
181 /* Fast-forward through the Tx queue to the point the peer says it has
182 * seen. Anything between the soft-ACK table and that point will get
183 * ACK'd or NACK'd in due course, so don't worry about it here; here we
184 * need to consider retransmitting anything beyond that point.
186 * Note that ACK for a packet can beat the update of tx_transmitted.
188 if (after_eq(READ_ONCE(call->acks_prev_seq), READ_ONCE(call->tx_transmitted)))
189 goto no_further_resend;
191 list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
192 if (before_eq(txb->seq, READ_ONCE(call->acks_prev_seq)))
194 if (after(txb->seq, READ_ONCE(call->tx_transmitted)))
195 break; /* Not transmitted yet */
197 if (ack && ack->reason == RXRPC_ACK_PING_RESPONSE &&
198 before(ntohl(txb->wire.serial), ntohl(ack->serial)))
199 goto do_resend; /* Wasn't accounted for by a more recent ping. */
201 if (ktime_after(txb->last_sent, max_age)) {
202 if (ktime_before(txb->last_sent, oldest))
203 oldest = txb->last_sent;
209 if (list_empty(&txb->tx_link)) {
210 list_add_tail(&txb->tx_link, &retrans_queue);
211 set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
212 rxrpc_inc_stat(call->rxnet, stat_tx_data_retrans);
218 resend_at = nsecs_to_jiffies(ktime_to_ns(ktime_sub(now, oldest)));
219 resend_at += jiffies + rxrpc_get_rto_backoff(call->peer,
220 !list_empty(&retrans_queue));
221 WRITE_ONCE(call->resend_at, resend_at);
224 rxrpc_congestion_timeout(call);
226 /* If there was nothing that needed retransmission then it's likely
227 * that an ACK got lost somewhere. Send a ping to find out instead of
228 * retransmitting data.
230 if (list_empty(&retrans_queue)) {
231 rxrpc_reduce_call_timer(call, resend_at, jiffies,
232 rxrpc_timer_set_for_resend);
233 ack_ts = ktime_sub(now, call->acks_latest_ts);
234 if (ktime_to_us(ack_ts) < (call->peer->srtt_us >> 3))
236 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
237 rxrpc_propose_ack_ping_for_lost_ack);
241 /* Retransmit the queue */
242 while ((txb = list_first_entry_or_null(&retrans_queue,
243 struct rxrpc_txbuf, tx_link))) {
244 list_del_init(&txb->tx_link);
245 rxrpc_transmit_one(call, txb);
253 * Start transmitting the reply to a service. This cancels the need to ACK the
254 * request if we haven't yet done so.
256 static void rxrpc_begin_service_reply(struct rxrpc_call *call)
258 unsigned long now = jiffies;
260 rxrpc_set_call_state(call, RXRPC_CALL_SERVER_SEND_REPLY);
261 WRITE_ONCE(call->delay_ack_at, now + MAX_JIFFY_OFFSET);
262 if (call->ackr_reason == RXRPC_ACK_DELAY)
263 call->ackr_reason = 0;
264 trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
268 * Close the transmission phase. After this point there is no more data to be
269 * transmitted in the call.
271 static void rxrpc_close_tx_phase(struct rxrpc_call *call)
273 _debug("________awaiting reply/ACK__________");
275 switch (__rxrpc_call_state(call)) {
276 case RXRPC_CALL_CLIENT_SEND_REQUEST:
277 rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_REPLY);
279 case RXRPC_CALL_SERVER_SEND_REPLY:
280 rxrpc_set_call_state(call, RXRPC_CALL_SERVER_AWAIT_ACK);
287 static bool rxrpc_tx_window_has_space(struct rxrpc_call *call)
289 unsigned int winsize = min_t(unsigned int, call->tx_winsize,
290 call->cong_cwnd + call->cong_extra);
291 rxrpc_seq_t window = call->acks_hard_ack, wtop = window + winsize;
292 rxrpc_seq_t tx_top = call->tx_top;
295 space = wtop - tx_top;
300 * Decant some if the sendmsg prepared queue into the transmission buffer.
302 static void rxrpc_decant_prepared_tx(struct rxrpc_call *call)
304 struct rxrpc_txbuf *txb;
306 if (!test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
307 if (list_empty(&call->tx_sendmsg))
309 rxrpc_expose_client_call(call);
312 while ((txb = list_first_entry_or_null(&call->tx_sendmsg,
313 struct rxrpc_txbuf, call_link))) {
314 spin_lock(&call->tx_lock);
315 list_del(&txb->call_link);
316 spin_unlock(&call->tx_lock);
318 call->tx_top = txb->seq;
319 list_add_tail(&txb->call_link, &call->tx_buffer);
321 if (txb->wire.flags & RXRPC_LAST_PACKET)
322 rxrpc_close_tx_phase(call);
324 rxrpc_transmit_one(call, txb);
326 if (!rxrpc_tx_window_has_space(call))
331 static void rxrpc_transmit_some_data(struct rxrpc_call *call)
333 switch (__rxrpc_call_state(call)) {
334 case RXRPC_CALL_SERVER_ACK_REQUEST:
335 if (list_empty(&call->tx_sendmsg))
337 rxrpc_begin_service_reply(call);
340 case RXRPC_CALL_SERVER_SEND_REPLY:
341 case RXRPC_CALL_CLIENT_SEND_REQUEST:
342 if (!rxrpc_tx_window_has_space(call))
344 if (list_empty(&call->tx_sendmsg)) {
345 rxrpc_inc_stat(call->rxnet, stat_tx_data_underflow);
348 rxrpc_decant_prepared_tx(call);
356 * Ping the other end to fill our RTT cache and to retrieve the rwind
357 * and MTU parameters.
359 static void rxrpc_send_initial_ping(struct rxrpc_call *call)
361 if (call->peer->rtt_count < 3 ||
362 ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
364 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
365 rxrpc_propose_ack_ping_for_params);
369 * Handle retransmission and deferred ACK/abort generation.
371 bool rxrpc_input_call_event(struct rxrpc_call *call, struct sk_buff *skb)
373 unsigned long now, next, t;
374 bool resend = false, expired = false;
377 rxrpc_see_call(call, rxrpc_call_see_input);
379 //printk("\n--------------------\n");
380 _enter("{%d,%s,%lx}",
381 call->debug_id, rxrpc_call_states[__rxrpc_call_state(call)],
384 if (__rxrpc_call_is_complete(call))
387 /* Handle abort request locklessly, vs rxrpc_propose_abort(). */
388 abort_code = smp_load_acquire(&call->send_abort);
390 rxrpc_abort_call(call, 0, call->send_abort, call->send_abort_err,
391 call->send_abort_why);
395 if (skb && skb->mark == RXRPC_SKB_MARK_ERROR)
398 /* If we see our async-event poke, check for timeout trippage. */
400 t = READ_ONCE(call->expect_rx_by);
401 if (time_after_eq(now, t)) {
402 trace_rxrpc_timer(call, rxrpc_timer_exp_normal, now);
406 t = READ_ONCE(call->expect_req_by);
407 if (__rxrpc_call_state(call) == RXRPC_CALL_SERVER_RECV_REQUEST &&
408 time_after_eq(now, t)) {
409 trace_rxrpc_timer(call, rxrpc_timer_exp_idle, now);
413 t = READ_ONCE(call->expect_term_by);
414 if (time_after_eq(now, t)) {
415 trace_rxrpc_timer(call, rxrpc_timer_exp_hard, now);
419 t = READ_ONCE(call->delay_ack_at);
420 if (time_after_eq(now, t)) {
421 trace_rxrpc_timer(call, rxrpc_timer_exp_ack, now);
422 cmpxchg(&call->delay_ack_at, t, now + MAX_JIFFY_OFFSET);
423 rxrpc_send_ACK(call, RXRPC_ACK_DELAY, 0,
424 rxrpc_propose_ack_ping_for_lost_ack);
427 t = READ_ONCE(call->ack_lost_at);
428 if (time_after_eq(now, t)) {
429 trace_rxrpc_timer(call, rxrpc_timer_exp_lost_ack, now);
430 cmpxchg(&call->ack_lost_at, t, now + MAX_JIFFY_OFFSET);
431 set_bit(RXRPC_CALL_EV_ACK_LOST, &call->events);
434 t = READ_ONCE(call->keepalive_at);
435 if (time_after_eq(now, t)) {
436 trace_rxrpc_timer(call, rxrpc_timer_exp_keepalive, now);
437 cmpxchg(&call->keepalive_at, t, now + MAX_JIFFY_OFFSET);
438 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
439 rxrpc_propose_ack_ping_for_keepalive);
442 t = READ_ONCE(call->ping_at);
443 if (time_after_eq(now, t)) {
444 trace_rxrpc_timer(call, rxrpc_timer_exp_ping, now);
445 cmpxchg(&call->ping_at, t, now + MAX_JIFFY_OFFSET);
446 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
447 rxrpc_propose_ack_ping_for_keepalive);
450 t = READ_ONCE(call->resend_at);
451 if (time_after_eq(now, t)) {
452 trace_rxrpc_timer(call, rxrpc_timer_exp_resend, now);
453 cmpxchg(&call->resend_at, t, now + MAX_JIFFY_OFFSET);
458 rxrpc_input_call_packet(call, skb);
460 rxrpc_transmit_some_data(call);
463 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
465 if (sp->hdr.type == RXRPC_PACKET_TYPE_ACK)
466 rxrpc_congestion_degrade(call);
469 if (test_and_clear_bit(RXRPC_CALL_EV_INITIAL_PING, &call->events))
470 rxrpc_send_initial_ping(call);
474 if (test_bit(RXRPC_CALL_RX_HEARD, &call->flags) &&
475 (int)call->conn->hi_serial - (int)call->rx_serial > 0) {
476 trace_rxrpc_call_reset(call);
477 rxrpc_abort_call(call, 0, RX_CALL_DEAD, -ECONNRESET,
478 rxrpc_abort_call_reset);
480 rxrpc_abort_call(call, 0, RX_CALL_TIMEOUT, -ETIME,
481 rxrpc_abort_call_timeout);
486 if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
487 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
488 rxrpc_propose_ack_ping_for_lost_ack);
490 if (resend && __rxrpc_call_state(call) != RXRPC_CALL_CLIENT_RECV_REPLY)
491 rxrpc_resend(call, NULL);
493 if (test_and_clear_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags))
494 rxrpc_send_ACK(call, RXRPC_ACK_IDLE, 0,
495 rxrpc_propose_ack_rx_idle);
497 if (call->ackr_nr_unacked > 2) {
498 if (call->peer->rtt_count < 3)
499 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
500 rxrpc_propose_ack_ping_for_rtt);
501 else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
503 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
504 rxrpc_propose_ack_ping_for_old_rtt);
506 rxrpc_send_ACK(call, RXRPC_ACK_IDLE, 0,
507 rxrpc_propose_ack_input_data);
510 /* Make sure the timer is restarted */
511 if (!__rxrpc_call_is_complete(call)) {
512 next = call->expect_rx_by;
514 #define set(T) { t = READ_ONCE(T); if (time_before(t, next)) next = t; }
516 set(call->expect_req_by);
517 set(call->expect_term_by);
518 set(call->delay_ack_at);
519 set(call->ack_lost_at);
520 set(call->resend_at);
521 set(call->keepalive_at);
525 if (time_after_eq(now, next))
526 rxrpc_poke_call(call, rxrpc_call_poke_timer_now);
528 rxrpc_reduce_call_timer(call, next, now, rxrpc_timer_restart);
532 if (__rxrpc_call_is_complete(call)) {
533 del_timer_sync(&call->timer);
534 if (!test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
535 rxrpc_disconnect_call(call);
537 call->security->free_call_crypto(call);
539 if (call->acks_hard_ack != call->tx_bottom)
540 rxrpc_shrink_call_tx_buffer(call);