12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
28c3e34a4SDavid Howells /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
38c3e34a4SDavid Howells *
48c3e34a4SDavid Howells * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
58c3e34a4SDavid Howells * Written by David Howells (dhowells@redhat.com)
68c3e34a4SDavid Howells */
78c3e34a4SDavid Howells
88c3e34a4SDavid Howells #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
98c3e34a4SDavid Howells
108c3e34a4SDavid Howells #include <linux/module.h>
118c3e34a4SDavid Howells #include <linux/circ_buf.h>
128c3e34a4SDavid Howells #include <linux/net.h>
138c3e34a4SDavid Howells #include <linux/skbuff.h>
148c3e34a4SDavid Howells #include <linux/slab.h>
158c3e34a4SDavid Howells #include <linux/udp.h>
168c3e34a4SDavid Howells #include <net/sock.h>
178c3e34a4SDavid Howells #include <net/af_rxrpc.h>
188c3e34a4SDavid Howells #include "ar-internal.h"
198c3e34a4SDavid Howells
208c3e34a4SDavid Howells /*
21a5af7e1fSDavid Howells * Propose a PING ACK be sent.
22a5af7e1fSDavid Howells */
rxrpc_propose_ping(struct rxrpc_call * call,u32 serial,enum rxrpc_propose_ack_trace why)2372f0c6fbSDavid Howells void rxrpc_propose_ping(struct rxrpc_call *call, u32 serial,
2472f0c6fbSDavid Howells enum rxrpc_propose_ack_trace why)
25a5af7e1fSDavid Howells {
26a158bdd3SDavid Howells unsigned long now = jiffies;
27a158bdd3SDavid Howells unsigned long ping_at = now + rxrpc_idle_ack_delay;
28a5af7e1fSDavid Howells
29a158bdd3SDavid Howells if (time_before(ping_at, call->ping_at)) {
30a158bdd3SDavid Howells WRITE_ONCE(call->ping_at, ping_at);
31a158bdd3SDavid Howells rxrpc_reduce_call_timer(call, ping_at, now,
32a158bdd3SDavid Howells rxrpc_timer_set_for_ping);
33530403d9SDavid Howells trace_rxrpc_propose_ack(call, why, RXRPC_ACK_PING, serial);
34a5af7e1fSDavid Howells }
35a5af7e1fSDavid Howells }
36a5af7e1fSDavid Howells
37a5af7e1fSDavid Howells /*
38530403d9SDavid Howells * Propose a DELAY ACK be sent in the future.
398c3e34a4SDavid Howells */
rxrpc_propose_delay_ACK(struct rxrpc_call * call,rxrpc_serial_t serial,enum rxrpc_propose_ack_trace why)404e76bd40SDavid Howells void rxrpc_propose_delay_ACK(struct rxrpc_call *call, rxrpc_serial_t serial,
41530403d9SDavid Howells enum rxrpc_propose_ack_trace why)
428c3e34a4SDavid Howells {
43beb8e5e4SDavid Howells unsigned long expiry = rxrpc_soft_ack_delay;
4472f0c6fbSDavid Howells unsigned long now = jiffies, ack_at;
458c3e34a4SDavid Howells
46248f219cSDavid Howells if (rxrpc_soft_ack_delay < expiry)
478c3e34a4SDavid Howells expiry = rxrpc_soft_ack_delay;
48c410bf01SDavid Howells if (call->peer->srtt_us != 0)
49c410bf01SDavid Howells ack_at = usecs_to_jiffies(call->peer->srtt_us >> 3);
50beb8e5e4SDavid Howells else
51beb8e5e4SDavid Howells ack_at = expiry;
52beb8e5e4SDavid Howells
53c7e86acfSDavid Howells ack_at += READ_ONCE(call->tx_backoff);
54282ef472SGustavo A. R. Silva ack_at += now;
55530403d9SDavid Howells if (time_before(ack_at, call->delay_ack_at)) {
56530403d9SDavid Howells WRITE_ONCE(call->delay_ack_at, ack_at);
57a158bdd3SDavid Howells rxrpc_reduce_call_timer(call, ack_at, now,
58a158bdd3SDavid Howells rxrpc_timer_set_for_ack);
59248f219cSDavid Howells }
609c7ad434SDavid Howells
61530403d9SDavid Howells trace_rxrpc_propose_ack(call, why, RXRPC_ACK_DELAY, serial);
628c3e34a4SDavid Howells }
638c3e34a4SDavid Howells
648c3e34a4SDavid Howells /*
6572f0c6fbSDavid Howells * Queue an ACK for immediate transmission.
6672f0c6fbSDavid Howells */
rxrpc_send_ACK(struct rxrpc_call * call,u8 ack_reason,rxrpc_serial_t serial,enum rxrpc_propose_ack_trace why)6772f0c6fbSDavid Howells void rxrpc_send_ACK(struct rxrpc_call *call, u8 ack_reason,
6872f0c6fbSDavid Howells rxrpc_serial_t serial, enum rxrpc_propose_ack_trace why)
6972f0c6fbSDavid Howells {
7072f0c6fbSDavid Howells struct rxrpc_txbuf *txb;
7172f0c6fbSDavid Howells
7272f0c6fbSDavid Howells if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
7372f0c6fbSDavid Howells return;
7472f0c6fbSDavid Howells
7572f0c6fbSDavid Howells rxrpc_inc_stat(call->rxnet, stat_tx_acks[ack_reason]);
7672f0c6fbSDavid Howells
7772f0c6fbSDavid Howells txb = rxrpc_alloc_txbuf(call, RXRPC_PACKET_TYPE_ACK,
78446b3e14SDavid Howells rcu_read_lock_held() ? GFP_ATOMIC | __GFP_NOWARN : GFP_NOFS);
7972f0c6fbSDavid Howells if (!txb) {
8072f0c6fbSDavid Howells kleave(" = -ENOMEM");
8172f0c6fbSDavid Howells return;
8272f0c6fbSDavid Howells }
8372f0c6fbSDavid Howells
8472f0c6fbSDavid Howells txb->ack_why = why;
8572f0c6fbSDavid Howells txb->wire.seq = 0;
8672f0c6fbSDavid Howells txb->wire.type = RXRPC_PACKET_TYPE_ACK;
8772f0c6fbSDavid Howells txb->wire.flags |= RXRPC_SLOW_START_OK;
8872f0c6fbSDavid Howells txb->ack.bufferSpace = 0;
8972f0c6fbSDavid Howells txb->ack.maxSkew = 0;
9072f0c6fbSDavid Howells txb->ack.firstPacket = 0;
9172f0c6fbSDavid Howells txb->ack.previousPacket = 0;
9272f0c6fbSDavid Howells txb->ack.serial = htonl(serial);
9372f0c6fbSDavid Howells txb->ack.reason = ack_reason;
9472f0c6fbSDavid Howells txb->ack.nAcks = 0;
9572f0c6fbSDavid Howells
9672f0c6fbSDavid Howells trace_rxrpc_send_ack(call, why, ack_reason, serial);
97b0346843SDavid Howells rxrpc_send_ack_packet(call, txb);
98b0346843SDavid Howells rxrpc_put_txbuf(txb, rxrpc_txbuf_put_ack_tx);
9972f0c6fbSDavid Howells }
10072f0c6fbSDavid Howells
10172f0c6fbSDavid Howells /*
10257494343SDavid Howells * Handle congestion being detected by the retransmit timeout.
10357494343SDavid Howells */
rxrpc_congestion_timeout(struct rxrpc_call * call)10457494343SDavid Howells static void rxrpc_congestion_timeout(struct rxrpc_call *call)
10557494343SDavid Howells {
10657494343SDavid Howells set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
10757494343SDavid Howells }
10857494343SDavid Howells
10957494343SDavid Howells /*
110248f219cSDavid Howells * Perform retransmission of NAK'd and unack'd packets.
1118c3e34a4SDavid Howells */
rxrpc_resend(struct rxrpc_call * call,struct sk_buff * ack_skb)1125e6ef4f1SDavid Howells void rxrpc_resend(struct rxrpc_call *call, struct sk_buff *ack_skb)
1138c3e34a4SDavid Howells {
114d57a3a15SDavid Howells struct rxrpc_ackpacket *ack = NULL;
115*024b2511SDavid Howells struct rxrpc_skb_priv *sp;
116a4ea4c47SDavid Howells struct rxrpc_txbuf *txb;
1172c13c05cSDavid Howells unsigned long resend_at;
118a4ea4c47SDavid Howells rxrpc_seq_t transmitted = READ_ONCE(call->tx_transmitted);
119c410bf01SDavid Howells ktime_t now, max_age, oldest, ack_ts;
120a4ea4c47SDavid Howells bool unacked = false;
121d57a3a15SDavid Howells unsigned int i;
122a4ea4c47SDavid Howells LIST_HEAD(retrans_queue);
1238c3e34a4SDavid Howells
124a4ea4c47SDavid Howells _enter("{%d,%d}", call->acks_hard_ack, call->tx_top);
1258c3e34a4SDavid Howells
126a158bdd3SDavid Howells now = ktime_get_real();
127214a9dc7SDavid Howells max_age = ktime_sub_us(now, jiffies_to_usecs(call->peer->rto_j));
128d57a3a15SDavid Howells oldest = now;
129d57a3a15SDavid Howells
130d57a3a15SDavid Howells if (list_empty(&call->tx_buffer))
131d57a3a15SDavid Howells goto no_resend;
13250235c4bSDavid Howells
133d57a3a15SDavid Howells if (list_empty(&call->tx_buffer))
134d57a3a15SDavid Howells goto no_further_resend;
135d57a3a15SDavid Howells
1365e6ef4f1SDavid Howells trace_rxrpc_resend(call, ack_skb);
137d57a3a15SDavid Howells txb = list_first_entry(&call->tx_buffer, struct rxrpc_txbuf, call_link);
138d57a3a15SDavid Howells
139d57a3a15SDavid Howells /* Scan the soft ACK table without dropping the lock and resend any
140d57a3a15SDavid Howells * explicitly NAK'd packets.
141d57a3a15SDavid Howells */
1425e6ef4f1SDavid Howells if (ack_skb) {
143*024b2511SDavid Howells sp = rxrpc_skb(ack_skb);
1445e6ef4f1SDavid Howells ack = (void *)ack_skb->data + sizeof(struct rxrpc_wire_header);
1455e6ef4f1SDavid Howells
146*024b2511SDavid Howells for (i = 0; i < sp->nr_acks; i++) {
147d57a3a15SDavid Howells rxrpc_seq_t seq;
148d57a3a15SDavid Howells
149d57a3a15SDavid Howells if (ack->acks[i] & 1)
1508c3e34a4SDavid Howells continue;
151*024b2511SDavid Howells seq = sp->first_ack + i;
152a4ea4c47SDavid Howells if (after(txb->seq, transmitted))
153a4ea4c47SDavid Howells break;
154d57a3a15SDavid Howells if (after(txb->seq, seq))
155d57a3a15SDavid Howells continue; /* A new hard ACK probably came in */
156d57a3a15SDavid Howells list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
157d57a3a15SDavid Howells if (txb->seq == seq)
158d57a3a15SDavid Howells goto found_txb;
159d57a3a15SDavid Howells }
160d57a3a15SDavid Howells goto no_further_resend;
161d57a3a15SDavid Howells
162d57a3a15SDavid Howells found_txb:
163d57a3a15SDavid Howells if (after(ntohl(txb->wire.serial), call->acks_highest_serial))
164d57a3a15SDavid Howells continue; /* Ack point not yet reached */
1658c3e34a4SDavid Howells
166a4ea4c47SDavid Howells rxrpc_see_txbuf(txb, rxrpc_txbuf_see_unacked);
1678c3e34a4SDavid Howells
168d57a3a15SDavid Howells if (list_empty(&txb->tx_link)) {
169d57a3a15SDavid Howells list_add_tail(&txb->tx_link, &retrans_queue);
170d57a3a15SDavid Howells set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
171d57a3a15SDavid Howells }
172d57a3a15SDavid Howells
173d57a3a15SDavid Howells trace_rxrpc_retransmit(call, txb->seq,
174d57a3a15SDavid Howells ktime_to_ns(ktime_sub(txb->last_sent,
175d57a3a15SDavid Howells max_age)));
176d57a3a15SDavid Howells
177d57a3a15SDavid Howells if (list_is_last(&txb->call_link, &call->tx_buffer))
178d57a3a15SDavid Howells goto no_further_resend;
179d57a3a15SDavid Howells txb = list_next_entry(txb, call_link);
180d57a3a15SDavid Howells }
181d57a3a15SDavid Howells }
182d57a3a15SDavid Howells
183d57a3a15SDavid Howells /* Fast-forward through the Tx queue to the point the peer says it has
184d57a3a15SDavid Howells * seen. Anything between the soft-ACK table and that point will get
185d57a3a15SDavid Howells * ACK'd or NACK'd in due course, so don't worry about it here; here we
186d57a3a15SDavid Howells * need to consider retransmitting anything beyond that point.
187d57a3a15SDavid Howells *
188d57a3a15SDavid Howells * Note that ACK for a packet can beat the update of tx_transmitted.
189d57a3a15SDavid Howells */
190d57a3a15SDavid Howells if (after_eq(READ_ONCE(call->acks_prev_seq), READ_ONCE(call->tx_transmitted)))
191d57a3a15SDavid Howells goto no_further_resend;
192d57a3a15SDavid Howells
193d57a3a15SDavid Howells list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
194d57a3a15SDavid Howells if (before_eq(txb->seq, READ_ONCE(call->acks_prev_seq)))
195d57a3a15SDavid Howells continue;
196d57a3a15SDavid Howells if (after(txb->seq, READ_ONCE(call->tx_transmitted)))
197d57a3a15SDavid Howells break; /* Not transmitted yet */
198d57a3a15SDavid Howells
199d57a3a15SDavid Howells if (ack && ack->reason == RXRPC_ACK_PING_RESPONSE &&
200d57a3a15SDavid Howells before(ntohl(txb->wire.serial), ntohl(ack->serial)))
201d57a3a15SDavid Howells goto do_resend; /* Wasn't accounted for by a more recent ping. */
202d57a3a15SDavid Howells
203a4ea4c47SDavid Howells if (ktime_after(txb->last_sent, max_age)) {
204a4ea4c47SDavid Howells if (ktime_before(txb->last_sent, oldest))
205a4ea4c47SDavid Howells oldest = txb->last_sent;
206248f219cSDavid Howells continue;
207248f219cSDavid Howells }
208d57a3a15SDavid Howells
209d57a3a15SDavid Howells do_resend:
210a4ea4c47SDavid Howells unacked = true;
211d57a3a15SDavid Howells if (list_empty(&txb->tx_link)) {
212d57a3a15SDavid Howells list_add_tail(&txb->tx_link, &retrans_queue);
213d57a3a15SDavid Howells set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
214d57a3a15SDavid Howells rxrpc_inc_stat(call->rxnet, stat_tx_data_retrans);
215d57a3a15SDavid Howells }
216dfa7d920SDavid Howells }
217248f219cSDavid Howells
218d57a3a15SDavid Howells no_further_resend:
219d57a3a15SDavid Howells no_resend:
22059299aa1SMarc Dionne resend_at = nsecs_to_jiffies(ktime_to_ns(ktime_sub(now, oldest)));
221a4ea4c47SDavid Howells resend_at += jiffies + rxrpc_get_rto_backoff(call->peer,
222a4ea4c47SDavid Howells !list_empty(&retrans_queue));
223a158bdd3SDavid Howells WRITE_ONCE(call->resend_at, resend_at);
224248f219cSDavid Howells
22557494343SDavid Howells if (unacked)
22657494343SDavid Howells rxrpc_congestion_timeout(call);
22757494343SDavid Howells
22857494343SDavid Howells /* If there was nothing that needed retransmission then it's likely
22957494343SDavid Howells * that an ACK got lost somewhere. Send a ping to find out instead of
23057494343SDavid Howells * retransmitting data.
23157494343SDavid Howells */
232a4ea4c47SDavid Howells if (list_empty(&retrans_queue)) {
2335e6ef4f1SDavid Howells rxrpc_reduce_call_timer(call, resend_at, jiffies,
234a158bdd3SDavid Howells rxrpc_timer_set_for_resend);
23557494343SDavid Howells ack_ts = ktime_sub(now, call->acks_latest_ts);
236c410bf01SDavid Howells if (ktime_to_us(ack_ts) < (call->peer->srtt_us >> 3))
23757494343SDavid Howells goto out;
23872f0c6fbSDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
23957494343SDavid Howells rxrpc_propose_ack_ping_for_lost_ack);
24057494343SDavid Howells goto out;
24157494343SDavid Howells }
24257494343SDavid Howells
2435e6ef4f1SDavid Howells /* Retransmit the queue */
244a4ea4c47SDavid Howells while ((txb = list_first_entry_or_null(&retrans_queue,
245a4ea4c47SDavid Howells struct rxrpc_txbuf, tx_link))) {
246a4ea4c47SDavid Howells list_del_init(&txb->tx_link);
2475e6ef4f1SDavid Howells rxrpc_transmit_one(call, txb);
248248f219cSDavid Howells }
249248f219cSDavid Howells
25057494343SDavid Howells out:
2518c3e34a4SDavid Howells _leave("");
2528c3e34a4SDavid Howells }
2538c3e34a4SDavid Howells
2542d689424SDavid Howells /*
2552d689424SDavid Howells * Start transmitting the reply to a service. This cancels the need to ACK the
2562d689424SDavid Howells * request if we haven't yet done so.
2572d689424SDavid Howells */
rxrpc_begin_service_reply(struct rxrpc_call * call)2582d689424SDavid Howells static void rxrpc_begin_service_reply(struct rxrpc_call *call)
2592d689424SDavid Howells {
26096b4059fSDavid Howells unsigned long now = jiffies;
2612d689424SDavid Howells
26296b4059fSDavid Howells rxrpc_set_call_state(call, RXRPC_CALL_SERVER_SEND_REPLY);
2632d689424SDavid Howells WRITE_ONCE(call->delay_ack_at, now + MAX_JIFFY_OFFSET);
2642d689424SDavid Howells if (call->ackr_reason == RXRPC_ACK_DELAY)
2652d689424SDavid Howells call->ackr_reason = 0;
2662d689424SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
2672d689424SDavid Howells }
2682d689424SDavid Howells
2692d689424SDavid Howells /*
2702d689424SDavid Howells * Close the transmission phase. After this point there is no more data to be
2712d689424SDavid Howells * transmitted in the call.
2722d689424SDavid Howells */
rxrpc_close_tx_phase(struct rxrpc_call * call)2732d689424SDavid Howells static void rxrpc_close_tx_phase(struct rxrpc_call *call)
2742d689424SDavid Howells {
2752d689424SDavid Howells _debug("________awaiting reply/ACK__________");
2762d689424SDavid Howells
27796b4059fSDavid Howells switch (__rxrpc_call_state(call)) {
2782d689424SDavid Howells case RXRPC_CALL_CLIENT_SEND_REQUEST:
27996b4059fSDavid Howells rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_REPLY);
2802d689424SDavid Howells break;
2812d689424SDavid Howells case RXRPC_CALL_SERVER_SEND_REPLY:
28296b4059fSDavid Howells rxrpc_set_call_state(call, RXRPC_CALL_SERVER_AWAIT_ACK);
2832d689424SDavid Howells break;
2842d689424SDavid Howells default:
2852d689424SDavid Howells break;
2862d689424SDavid Howells }
2872d689424SDavid Howells }
2882d689424SDavid Howells
rxrpc_tx_window_has_space(struct rxrpc_call * call)289cf37b598SDavid Howells static bool rxrpc_tx_window_has_space(struct rxrpc_call *call)
290cf37b598SDavid Howells {
291cf37b598SDavid Howells unsigned int winsize = min_t(unsigned int, call->tx_winsize,
292cf37b598SDavid Howells call->cong_cwnd + call->cong_extra);
293cf37b598SDavid Howells rxrpc_seq_t window = call->acks_hard_ack, wtop = window + winsize;
294cf37b598SDavid Howells rxrpc_seq_t tx_top = call->tx_top;
295cf37b598SDavid Howells int space;
296cf37b598SDavid Howells
297cf37b598SDavid Howells space = wtop - tx_top;
298cf37b598SDavid Howells return space > 0;
299cf37b598SDavid Howells }
300cf37b598SDavid Howells
301cf37b598SDavid Howells /*
302cf37b598SDavid Howells * Decant some if the sendmsg prepared queue into the transmission buffer.
303cf37b598SDavid Howells */
rxrpc_decant_prepared_tx(struct rxrpc_call * call)304cf37b598SDavid Howells static void rxrpc_decant_prepared_tx(struct rxrpc_call *call)
305cf37b598SDavid Howells {
306cf37b598SDavid Howells struct rxrpc_txbuf *txb;
307cf37b598SDavid Howells
308a343b174SDavid Howells if (!test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
309a343b174SDavid Howells if (list_empty(&call->tx_sendmsg))
310a343b174SDavid Howells return;
311cf37b598SDavid Howells rxrpc_expose_client_call(call);
312a343b174SDavid Howells }
313cf37b598SDavid Howells
314cf37b598SDavid Howells while ((txb = list_first_entry_or_null(&call->tx_sendmsg,
315cf37b598SDavid Howells struct rxrpc_txbuf, call_link))) {
316cf37b598SDavid Howells spin_lock(&call->tx_lock);
317cf37b598SDavid Howells list_del(&txb->call_link);
318cf37b598SDavid Howells spin_unlock(&call->tx_lock);
319cf37b598SDavid Howells
320cf37b598SDavid Howells call->tx_top = txb->seq;
321cf37b598SDavid Howells list_add_tail(&txb->call_link, &call->tx_buffer);
322cf37b598SDavid Howells
3232d689424SDavid Howells if (txb->wire.flags & RXRPC_LAST_PACKET)
3242d689424SDavid Howells rxrpc_close_tx_phase(call);
3252d689424SDavid Howells
326cf37b598SDavid Howells rxrpc_transmit_one(call, txb);
327cf37b598SDavid Howells
328cf37b598SDavid Howells if (!rxrpc_tx_window_has_space(call))
329cf37b598SDavid Howells break;
330cf37b598SDavid Howells }
331cf37b598SDavid Howells }
332cf37b598SDavid Howells
rxrpc_transmit_some_data(struct rxrpc_call * call)333cf37b598SDavid Howells static void rxrpc_transmit_some_data(struct rxrpc_call *call)
334cf37b598SDavid Howells {
33596b4059fSDavid Howells switch (__rxrpc_call_state(call)) {
336cf37b598SDavid Howells case RXRPC_CALL_SERVER_ACK_REQUEST:
337cf37b598SDavid Howells if (list_empty(&call->tx_sendmsg))
338cf37b598SDavid Howells return;
3392d689424SDavid Howells rxrpc_begin_service_reply(call);
340cf37b598SDavid Howells fallthrough;
341cf37b598SDavid Howells
342cf37b598SDavid Howells case RXRPC_CALL_SERVER_SEND_REPLY:
343cf37b598SDavid Howells case RXRPC_CALL_CLIENT_SEND_REQUEST:
344cf37b598SDavid Howells if (!rxrpc_tx_window_has_space(call))
345cf37b598SDavid Howells return;
34632cf8edbSDavid Howells if (list_empty(&call->tx_sendmsg)) {
34732cf8edbSDavid Howells rxrpc_inc_stat(call->rxnet, stat_tx_data_underflow);
348cf37b598SDavid Howells return;
34932cf8edbSDavid Howells }
350cf37b598SDavid Howells rxrpc_decant_prepared_tx(call);
351cf37b598SDavid Howells break;
352cf37b598SDavid Howells default:
353cf37b598SDavid Howells return;
354cf37b598SDavid Howells }
355cf37b598SDavid Howells }
356cf37b598SDavid Howells
3578c3e34a4SDavid Howells /*
3585e6ef4f1SDavid Howells * Ping the other end to fill our RTT cache and to retrieve the rwind
3595e6ef4f1SDavid Howells * and MTU parameters.
3605e6ef4f1SDavid Howells */
rxrpc_send_initial_ping(struct rxrpc_call * call)3615e6ef4f1SDavid Howells static void rxrpc_send_initial_ping(struct rxrpc_call *call)
3625e6ef4f1SDavid Howells {
3635e6ef4f1SDavid Howells if (call->peer->rtt_count < 3 ||
3645e6ef4f1SDavid Howells ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
3655e6ef4f1SDavid Howells ktime_get_real()))
3665e6ef4f1SDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
3675e6ef4f1SDavid Howells rxrpc_propose_ack_ping_for_params);
3685e6ef4f1SDavid Howells }
3695e6ef4f1SDavid Howells
3705e6ef4f1SDavid Howells /*
371248f219cSDavid Howells * Handle retransmission and deferred ACK/abort generation.
3728c3e34a4SDavid Howells */
rxrpc_input_call_event(struct rxrpc_call * call,struct sk_buff * skb)37357af281eSDavid Howells bool rxrpc_input_call_event(struct rxrpc_call *call, struct sk_buff *skb)
3748c3e34a4SDavid Howells {
375a158bdd3SDavid Howells unsigned long now, next, t;
3765e6ef4f1SDavid Howells bool resend = false, expired = false;
377a343b174SDavid Howells s32 abort_code;
3788c3e34a4SDavid Howells
379cb0fc0c9SDavid Howells rxrpc_see_call(call, rxrpc_call_see_input);
380e34d4234SDavid Howells
3818c3e34a4SDavid Howells //printk("\n--------------------\n");
382248f219cSDavid Howells _enter("{%d,%s,%lx}",
38396b4059fSDavid Howells call->debug_id, rxrpc_call_states[__rxrpc_call_state(call)],
38496b4059fSDavid Howells call->events);
3858c3e34a4SDavid Howells
38696b4059fSDavid Howells if (__rxrpc_call_is_complete(call))
3873feda9d6SDavid Howells goto out;
3888c3e34a4SDavid Howells
389a343b174SDavid Howells /* Handle abort request locklessly, vs rxrpc_propose_abort(). */
390a343b174SDavid Howells abort_code = smp_load_acquire(&call->send_abort);
391a343b174SDavid Howells if (abort_code) {
39257af281eSDavid Howells rxrpc_abort_call(call, 0, call->send_abort, call->send_abort_err,
39357af281eSDavid Howells call->send_abort_why);
394a343b174SDavid Howells goto out;
395a343b174SDavid Howells }
396a343b174SDavid Howells
3975e6ef4f1SDavid Howells if (skb && skb->mark == RXRPC_SKB_MARK_ERROR)
3985e6ef4f1SDavid Howells goto out;
3995e6ef4f1SDavid Howells
4005e6ef4f1SDavid Howells /* If we see our async-event poke, check for timeout trippage. */
401a158bdd3SDavid Howells now = jiffies;
402a158bdd3SDavid Howells t = READ_ONCE(call->expect_rx_by);
403a158bdd3SDavid Howells if (time_after_eq(now, t)) {
404a158bdd3SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_exp_normal, now);
4055e6ef4f1SDavid Howells expired = true;
406a158bdd3SDavid Howells }
407a158bdd3SDavid Howells
408a158bdd3SDavid Howells t = READ_ONCE(call->expect_req_by);
40996b4059fSDavid Howells if (__rxrpc_call_state(call) == RXRPC_CALL_SERVER_RECV_REQUEST &&
410a158bdd3SDavid Howells time_after_eq(now, t)) {
411a158bdd3SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_exp_idle, now);
4125e6ef4f1SDavid Howells expired = true;
413a158bdd3SDavid Howells }
414a158bdd3SDavid Howells
415a158bdd3SDavid Howells t = READ_ONCE(call->expect_term_by);
416a158bdd3SDavid Howells if (time_after_eq(now, t)) {
417a158bdd3SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_exp_hard, now);
4185e6ef4f1SDavid Howells expired = true;
419a158bdd3SDavid Howells }
420a158bdd3SDavid Howells
421530403d9SDavid Howells t = READ_ONCE(call->delay_ack_at);
422a158bdd3SDavid Howells if (time_after_eq(now, t)) {
423a158bdd3SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_exp_ack, now);
424530403d9SDavid Howells cmpxchg(&call->delay_ack_at, t, now + MAX_JIFFY_OFFSET);
425200cb50bSDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_DELAY, 0,
42672f0c6fbSDavid Howells rxrpc_propose_ack_ping_for_lost_ack);
427a158bdd3SDavid Howells }
428a158bdd3SDavid Howells
429bd1fdf8cSDavid Howells t = READ_ONCE(call->ack_lost_at);
430bd1fdf8cSDavid Howells if (time_after_eq(now, t)) {
431bd1fdf8cSDavid Howells trace_rxrpc_timer(call, rxrpc_timer_exp_lost_ack, now);
432bd1fdf8cSDavid Howells cmpxchg(&call->ack_lost_at, t, now + MAX_JIFFY_OFFSET);
433bd1fdf8cSDavid Howells set_bit(RXRPC_CALL_EV_ACK_LOST, &call->events);
434bd1fdf8cSDavid Howells }
435bd1fdf8cSDavid Howells
436415f44e4SDavid Howells t = READ_ONCE(call->keepalive_at);
437415f44e4SDavid Howells if (time_after_eq(now, t)) {
438415f44e4SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_exp_keepalive, now);
439415f44e4SDavid Howells cmpxchg(&call->keepalive_at, t, now + MAX_JIFFY_OFFSET);
44072f0c6fbSDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
441415f44e4SDavid Howells rxrpc_propose_ack_ping_for_keepalive);
442415f44e4SDavid Howells }
443415f44e4SDavid Howells
444a158bdd3SDavid Howells t = READ_ONCE(call->ping_at);
445a158bdd3SDavid Howells if (time_after_eq(now, t)) {
446a158bdd3SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_exp_ping, now);
447a158bdd3SDavid Howells cmpxchg(&call->ping_at, t, now + MAX_JIFFY_OFFSET);
44872f0c6fbSDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
44972f0c6fbSDavid Howells rxrpc_propose_ack_ping_for_keepalive);
450a158bdd3SDavid Howells }
451a158bdd3SDavid Howells
452a158bdd3SDavid Howells t = READ_ONCE(call->resend_at);
453a158bdd3SDavid Howells if (time_after_eq(now, t)) {
454a158bdd3SDavid Howells trace_rxrpc_timer(call, rxrpc_timer_exp_resend, now);
455a158bdd3SDavid Howells cmpxchg(&call->resend_at, t, now + MAX_JIFFY_OFFSET);
4565e6ef4f1SDavid Howells resend = true;
457a158bdd3SDavid Howells }
458a158bdd3SDavid Howells
4595e6ef4f1SDavid Howells if (skb)
4605e6ef4f1SDavid Howells rxrpc_input_call_packet(call, skb);
4615e6ef4f1SDavid Howells
462cf37b598SDavid Howells rxrpc_transmit_some_data(call);
463cf37b598SDavid Howells
4645086d9a9SDavid Howells if (skb) {
4655086d9a9SDavid Howells struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
4665086d9a9SDavid Howells
4675086d9a9SDavid Howells if (sp->hdr.type == RXRPC_PACKET_TYPE_ACK)
4685086d9a9SDavid Howells rxrpc_congestion_degrade(call);
4695086d9a9SDavid Howells }
4705086d9a9SDavid Howells
4715e6ef4f1SDavid Howells if (test_and_clear_bit(RXRPC_CALL_EV_INITIAL_PING, &call->events))
4725e6ef4f1SDavid Howells rxrpc_send_initial_ping(call);
4735e6ef4f1SDavid Howells
474a158bdd3SDavid Howells /* Process events */
4755e6ef4f1SDavid Howells if (expired) {
4761a025028SDavid Howells if (test_bit(RXRPC_CALL_RX_HEARD, &call->flags) &&
4771a025028SDavid Howells (int)call->conn->hi_serial - (int)call->rx_serial > 0) {
4781a025028SDavid Howells trace_rxrpc_call_reset(call);
47957af281eSDavid Howells rxrpc_abort_call(call, 0, RX_CALL_DEAD, -ECONNRESET,
48057af281eSDavid Howells rxrpc_abort_call_reset);
4811a025028SDavid Howells } else {
48257af281eSDavid Howells rxrpc_abort_call(call, 0, RX_CALL_TIMEOUT, -ETIME,
48357af281eSDavid Howells rxrpc_abort_call_timeout);
4841a025028SDavid Howells }
4855e6ef4f1SDavid Howells goto out;
4868c3e34a4SDavid Howells }
4878c3e34a4SDavid Howells
4885e6ef4f1SDavid Howells if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
48972f0c6fbSDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
490bd1fdf8cSDavid Howells rxrpc_propose_ack_ping_for_lost_ack);
491a5af7e1fSDavid Howells
49296b4059fSDavid Howells if (resend && __rxrpc_call_state(call) != RXRPC_CALL_CLIENT_RECV_REPLY)
4935e6ef4f1SDavid Howells rxrpc_resend(call, NULL);
4945e6ef4f1SDavid Howells
4955e6ef4f1SDavid Howells if (test_and_clear_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags))
4965e6ef4f1SDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_IDLE, 0,
4975e6ef4f1SDavid Howells rxrpc_propose_ack_rx_idle);
4985e6ef4f1SDavid Howells
4995bbf9533SDavid Howells if (call->ackr_nr_unacked > 2) {
50084e28aa5SDavid Howells if (call->peer->rtt_count < 3)
50184e28aa5SDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
50284e28aa5SDavid Howells rxrpc_propose_ack_ping_for_rtt);
50384e28aa5SDavid Howells else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
50484e28aa5SDavid Howells ktime_get_real()))
50584e28aa5SDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
50684e28aa5SDavid Howells rxrpc_propose_ack_ping_for_old_rtt);
50784e28aa5SDavid Howells else
5085e6ef4f1SDavid Howells rxrpc_send_ACK(call, RXRPC_ACK_IDLE, 0,
5095e6ef4f1SDavid Howells rxrpc_propose_ack_input_data);
51084e28aa5SDavid Howells }
5118c3e34a4SDavid Howells
512a158bdd3SDavid Howells /* Make sure the timer is restarted */
51396b4059fSDavid Howells if (!__rxrpc_call_is_complete(call)) {
514a158bdd3SDavid Howells next = call->expect_rx_by;
515a158bdd3SDavid Howells
516a158bdd3SDavid Howells #define set(T) { t = READ_ONCE(T); if (time_before(t, next)) next = t; }
517a158bdd3SDavid Howells
518a158bdd3SDavid Howells set(call->expect_req_by);
519a158bdd3SDavid Howells set(call->expect_term_by);
520530403d9SDavid Howells set(call->delay_ack_at);
521bd1fdf8cSDavid Howells set(call->ack_lost_at);
522a158bdd3SDavid Howells set(call->resend_at);
523415f44e4SDavid Howells set(call->keepalive_at);
524a158bdd3SDavid Howells set(call->ping_at);
525a158bdd3SDavid Howells
526a158bdd3SDavid Howells now = jiffies;
527a158bdd3SDavid Howells if (time_after_eq(now, next))
5285e6ef4f1SDavid Howells rxrpc_poke_call(call, rxrpc_call_poke_timer_now);
529a158bdd3SDavid Howells
530a158bdd3SDavid Howells rxrpc_reduce_call_timer(call, next, now, rxrpc_timer_restart);
5315e6ef4f1SDavid Howells }
5328c3e34a4SDavid Howells
533248f219cSDavid Howells out:
53496b4059fSDavid Howells if (__rxrpc_call_is_complete(call)) {
5355e6ef4f1SDavid Howells del_timer_sync(&call->timer);
53603fc55adSDavid Howells if (!test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
53703fc55adSDavid Howells rxrpc_disconnect_call(call);
53803fc55adSDavid Howells if (call->security)
53903fc55adSDavid Howells call->security->free_call_crypto(call);
54003fc55adSDavid Howells }
5415e6ef4f1SDavid Howells if (call->acks_hard_ack != call->tx_bottom)
5425e6ef4f1SDavid Howells rxrpc_shrink_call_tx_buffer(call);
5438c3e34a4SDavid Howells _leave("");
54457af281eSDavid Howells return true;
5458c3e34a4SDavid Howells }
546