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 call->ackr_serial = serial;
48 if (rxrpc_soft_ack_delay < expiry)
49 expiry = rxrpc_soft_ack_delay;
50 if (call->peer->srtt_us != 0)
51 ack_at = usecs_to_jiffies(call->peer->srtt_us >> 3);
55 ack_at += READ_ONCE(call->tx_backoff);
57 if (time_before(ack_at, call->delay_ack_at)) {
58 WRITE_ONCE(call->delay_ack_at, ack_at);
59 rxrpc_reduce_call_timer(call, ack_at, now,
60 rxrpc_timer_set_for_ack);
63 trace_rxrpc_propose_ack(call, why, RXRPC_ACK_DELAY, serial);
67 * Queue an ACK for immediate transmission.
69 void rxrpc_send_ACK(struct rxrpc_call *call, u8 ack_reason,
70 rxrpc_serial_t serial, enum rxrpc_propose_ack_trace why)
72 struct rxrpc_local *local = call->conn->local;
73 struct rxrpc_txbuf *txb;
75 if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
77 if (ack_reason == RXRPC_ACK_DELAY &&
78 test_and_set_bit(RXRPC_CALL_DELAY_ACK_PENDING, &call->flags)) {
79 trace_rxrpc_drop_ack(call, why, ack_reason, serial, false);
83 rxrpc_inc_stat(call->rxnet, stat_tx_acks[ack_reason]);
85 txb = rxrpc_alloc_txbuf(call, RXRPC_PACKET_TYPE_ACK,
86 rcu_read_lock_held() ? GFP_ATOMIC | __GFP_NOWARN : GFP_NOFS);
94 txb->wire.type = RXRPC_PACKET_TYPE_ACK;
95 txb->wire.flags |= RXRPC_SLOW_START_OK;
96 txb->ack.bufferSpace = 0;
98 txb->ack.firstPacket = 0;
99 txb->ack.previousPacket = 0;
100 txb->ack.serial = htonl(serial);
101 txb->ack.reason = ack_reason;
104 if (!rxrpc_try_get_call(call, rxrpc_call_get_send_ack)) {
105 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_nomem);
109 spin_lock_bh(&local->ack_tx_lock);
110 list_add_tail(&txb->tx_link, &local->ack_tx_queue);
111 spin_unlock_bh(&local->ack_tx_lock);
112 trace_rxrpc_send_ack(call, why, ack_reason, serial);
114 if (!rcu_read_lock_held()) {
115 rxrpc_transmit_ack_packets(call->peer->local);
117 rxrpc_get_local(local, rxrpc_local_get_queue);
118 rxrpc_queue_local(local);
123 * Handle congestion being detected by the retransmit timeout.
125 static void rxrpc_congestion_timeout(struct rxrpc_call *call)
127 set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
131 * Perform retransmission of NAK'd and unack'd packets.
133 static void rxrpc_resend(struct rxrpc_call *call, unsigned long now_j)
135 struct rxrpc_ackpacket *ack = NULL;
136 struct rxrpc_txbuf *txb;
137 struct sk_buff *ack_skb = NULL;
138 unsigned long resend_at;
139 rxrpc_seq_t transmitted = READ_ONCE(call->tx_transmitted);
140 ktime_t now, max_age, oldest, ack_ts;
141 bool unacked = false;
143 LIST_HEAD(retrans_queue);
145 _enter("{%d,%d}", call->acks_hard_ack, call->tx_top);
147 now = ktime_get_real();
148 max_age = ktime_sub_us(now, jiffies_to_usecs(call->peer->rto_j));
151 /* See if there's an ACK saved with a soft-ACK table in it. */
152 if (call->acks_soft_tbl) {
153 spin_lock_bh(&call->acks_ack_lock);
154 ack_skb = call->acks_soft_tbl;
156 rxrpc_get_skb(ack_skb, rxrpc_skb_get_ack);
157 ack = (void *)ack_skb->data + sizeof(struct rxrpc_wire_header);
159 spin_unlock_bh(&call->acks_ack_lock);
162 if (list_empty(&call->tx_buffer))
165 spin_lock(&call->tx_lock);
167 if (list_empty(&call->tx_buffer))
168 goto no_further_resend;
170 trace_rxrpc_resend(call);
171 txb = list_first_entry(&call->tx_buffer, struct rxrpc_txbuf, call_link);
173 /* Scan the soft ACK table without dropping the lock and resend any
174 * explicitly NAK'd packets.
177 for (i = 0; i < ack->nAcks; i++) {
180 if (ack->acks[i] & 1)
182 seq = ntohl(ack->firstPacket) + i;
183 if (after(txb->seq, transmitted))
185 if (after(txb->seq, seq))
186 continue; /* A new hard ACK probably came in */
187 list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
191 goto no_further_resend;
194 if (after(ntohl(txb->wire.serial), call->acks_highest_serial))
195 continue; /* Ack point not yet reached */
197 rxrpc_see_txbuf(txb, rxrpc_txbuf_see_unacked);
199 if (list_empty(&txb->tx_link)) {
200 rxrpc_get_txbuf(txb, rxrpc_txbuf_get_retrans);
201 list_add_tail(&txb->tx_link, &retrans_queue);
202 set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
205 trace_rxrpc_retransmit(call, txb->seq,
206 ktime_to_ns(ktime_sub(txb->last_sent,
209 if (list_is_last(&txb->call_link, &call->tx_buffer))
210 goto no_further_resend;
211 txb = list_next_entry(txb, call_link);
215 /* Fast-forward through the Tx queue to the point the peer says it has
216 * seen. Anything between the soft-ACK table and that point will get
217 * ACK'd or NACK'd in due course, so don't worry about it here; here we
218 * need to consider retransmitting anything beyond that point.
220 * Note that ACK for a packet can beat the update of tx_transmitted.
222 if (after_eq(READ_ONCE(call->acks_prev_seq), READ_ONCE(call->tx_transmitted)))
223 goto no_further_resend;
225 list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
226 if (before_eq(txb->seq, READ_ONCE(call->acks_prev_seq)))
228 if (after(txb->seq, READ_ONCE(call->tx_transmitted)))
229 break; /* Not transmitted yet */
231 if (ack && ack->reason == RXRPC_ACK_PING_RESPONSE &&
232 before(ntohl(txb->wire.serial), ntohl(ack->serial)))
233 goto do_resend; /* Wasn't accounted for by a more recent ping. */
235 if (ktime_after(txb->last_sent, max_age)) {
236 if (ktime_before(txb->last_sent, oldest))
237 oldest = txb->last_sent;
243 if (list_empty(&txb->tx_link)) {
244 rxrpc_get_txbuf(txb, rxrpc_txbuf_get_retrans);
245 list_add_tail(&txb->tx_link, &retrans_queue);
246 set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
247 rxrpc_inc_stat(call->rxnet, stat_tx_data_retrans);
252 spin_unlock(&call->tx_lock);
254 rxrpc_free_skb(ack_skb, rxrpc_skb_put_ack);
256 resend_at = nsecs_to_jiffies(ktime_to_ns(ktime_sub(now, oldest)));
257 resend_at += jiffies + rxrpc_get_rto_backoff(call->peer,
258 !list_empty(&retrans_queue));
259 WRITE_ONCE(call->resend_at, resend_at);
262 rxrpc_congestion_timeout(call);
264 /* If there was nothing that needed retransmission then it's likely
265 * that an ACK got lost somewhere. Send a ping to find out instead of
266 * retransmitting data.
268 if (list_empty(&retrans_queue)) {
269 rxrpc_reduce_call_timer(call, resend_at, now_j,
270 rxrpc_timer_set_for_resend);
271 ack_ts = ktime_sub(now, call->acks_latest_ts);
272 if (ktime_to_us(ack_ts) < (call->peer->srtt_us >> 3))
274 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
275 rxrpc_propose_ack_ping_for_lost_ack);
279 while ((txb = list_first_entry_or_null(&retrans_queue,
280 struct rxrpc_txbuf, tx_link))) {
281 list_del_init(&txb->tx_link);
282 rxrpc_send_data_packet(call, txb);
283 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_trans);
285 trace_rxrpc_retransmit(call, txb->seq,
286 ktime_to_ns(ktime_sub(txb->last_sent,
294 static bool rxrpc_tx_window_has_space(struct rxrpc_call *call)
296 unsigned int winsize = min_t(unsigned int, call->tx_winsize,
297 call->cong_cwnd + call->cong_extra);
298 rxrpc_seq_t window = call->acks_hard_ack, wtop = window + winsize;
299 rxrpc_seq_t tx_top = call->tx_top;
302 space = wtop - tx_top;
307 * Decant some if the sendmsg prepared queue into the transmission buffer.
309 static void rxrpc_decant_prepared_tx(struct rxrpc_call *call)
311 struct rxrpc_txbuf *txb;
313 if (rxrpc_is_client_call(call) &&
314 !test_bit(RXRPC_CALL_EXPOSED, &call->flags))
315 rxrpc_expose_client_call(call);
317 while ((txb = list_first_entry_or_null(&call->tx_sendmsg,
318 struct rxrpc_txbuf, call_link))) {
319 spin_lock(&call->tx_lock);
320 list_del(&txb->call_link);
321 spin_unlock(&call->tx_lock);
323 call->tx_top = txb->seq;
324 list_add_tail(&txb->call_link, &call->tx_buffer);
326 rxrpc_transmit_one(call, txb);
328 // TODO: Drain the transmission buffers. Do this somewhere better
329 if (after(call->acks_hard_ack, call->tx_bottom + 16))
330 rxrpc_shrink_call_tx_buffer(call);
332 if (!rxrpc_tx_window_has_space(call))
337 static void rxrpc_transmit_some_data(struct rxrpc_call *call)
339 switch (call->state) {
340 case RXRPC_CALL_SERVER_ACK_REQUEST:
341 if (list_empty(&call->tx_sendmsg))
345 case RXRPC_CALL_SERVER_SEND_REPLY:
346 case RXRPC_CALL_SERVER_AWAIT_ACK:
347 case RXRPC_CALL_CLIENT_SEND_REQUEST:
348 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
349 if (!rxrpc_tx_window_has_space(call))
351 if (list_empty(&call->tx_sendmsg))
353 rxrpc_decant_prepared_tx(call);
361 * Handle retransmission and deferred ACK/abort generation.
363 void rxrpc_process_call(struct work_struct *work)
365 struct rxrpc_call *call =
366 container_of(work, struct rxrpc_call, processor);
367 unsigned long now, next, t;
368 unsigned int iterations = 0;
369 rxrpc_serial_t ackr_serial;
371 rxrpc_see_call(call, rxrpc_call_see_input);
373 //printk("\n--------------------\n");
374 _enter("{%d,%s,%lx}",
375 call->debug_id, rxrpc_call_states[call->state], call->events);
378 if (call->acks_hard_ack != call->tx_bottom)
379 rxrpc_shrink_call_tx_buffer(call);
381 /* Limit the number of times we do this before returning to the manager */
382 if (!rxrpc_tx_window_has_space(call) ||
383 list_empty(&call->tx_sendmsg)) {
389 if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
390 rxrpc_send_abort_packet(call);
394 if (call->state == RXRPC_CALL_COMPLETE) {
395 del_timer_sync(&call->timer);
399 /* Work out if any timeouts tripped */
401 t = READ_ONCE(call->expect_rx_by);
402 if (time_after_eq(now, t)) {
403 trace_rxrpc_timer(call, rxrpc_timer_exp_normal, now);
404 set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
407 t = READ_ONCE(call->expect_req_by);
408 if (call->state == RXRPC_CALL_SERVER_RECV_REQUEST &&
409 time_after_eq(now, t)) {
410 trace_rxrpc_timer(call, rxrpc_timer_exp_idle, now);
411 set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
414 t = READ_ONCE(call->expect_term_by);
415 if (time_after_eq(now, t)) {
416 trace_rxrpc_timer(call, rxrpc_timer_exp_hard, now);
417 set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
420 t = READ_ONCE(call->delay_ack_at);
421 if (time_after_eq(now, t)) {
422 trace_rxrpc_timer(call, rxrpc_timer_exp_ack, now);
423 cmpxchg(&call->delay_ack_at, t, now + MAX_JIFFY_OFFSET);
424 ackr_serial = xchg(&call->ackr_serial, 0);
425 rxrpc_send_ACK(call, RXRPC_ACK_DELAY, ackr_serial,
426 rxrpc_propose_ack_ping_for_lost_ack);
429 t = READ_ONCE(call->ack_lost_at);
430 if (time_after_eq(now, t)) {
431 trace_rxrpc_timer(call, rxrpc_timer_exp_lost_ack, now);
432 cmpxchg(&call->ack_lost_at, t, now + MAX_JIFFY_OFFSET);
433 set_bit(RXRPC_CALL_EV_ACK_LOST, &call->events);
436 t = READ_ONCE(call->keepalive_at);
437 if (time_after_eq(now, t)) {
438 trace_rxrpc_timer(call, rxrpc_timer_exp_keepalive, now);
439 cmpxchg(&call->keepalive_at, t, now + MAX_JIFFY_OFFSET);
440 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
441 rxrpc_propose_ack_ping_for_keepalive);
444 t = READ_ONCE(call->ping_at);
445 if (time_after_eq(now, t)) {
446 trace_rxrpc_timer(call, rxrpc_timer_exp_ping, now);
447 cmpxchg(&call->ping_at, t, now + MAX_JIFFY_OFFSET);
448 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
449 rxrpc_propose_ack_ping_for_keepalive);
452 t = READ_ONCE(call->resend_at);
453 if (time_after_eq(now, t)) {
454 trace_rxrpc_timer(call, rxrpc_timer_exp_resend, now);
455 cmpxchg(&call->resend_at, t, now + MAX_JIFFY_OFFSET);
456 set_bit(RXRPC_CALL_EV_RESEND, &call->events);
459 rxrpc_transmit_some_data(call);
462 if (test_and_clear_bit(RXRPC_CALL_EV_EXPIRED, &call->events)) {
463 if (test_bit(RXRPC_CALL_RX_HEARD, &call->flags) &&
464 (int)call->conn->hi_serial - (int)call->rx_serial > 0) {
465 trace_rxrpc_call_reset(call);
466 rxrpc_abort_call("EXP", call, 0, RX_CALL_DEAD, -ECONNRESET);
468 rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, -ETIME);
470 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
474 if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events)) {
475 call->acks_lost_top = call->tx_top;
476 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
477 rxrpc_propose_ack_ping_for_lost_ack);
480 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events) &&
481 call->state != RXRPC_CALL_CLIENT_RECV_REPLY) {
482 rxrpc_resend(call, now);
486 /* Make sure the timer is restarted */
487 next = call->expect_rx_by;
489 #define set(T) { t = READ_ONCE(T); if (time_before(t, next)) next = t; }
491 set(call->expect_req_by);
492 set(call->expect_term_by);
493 set(call->delay_ack_at);
494 set(call->ack_lost_at);
495 set(call->resend_at);
496 set(call->keepalive_at);
500 if (time_after_eq(now, next))
503 rxrpc_reduce_call_timer(call, next, now, rxrpc_timer_restart);
505 /* other events may have been raised since we started checking */
514 if (call->state < RXRPC_CALL_COMPLETE)
515 rxrpc_queue_call(call, rxrpc_call_queue_requeue);