1 /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/circ_buf.h>
16 #include <linux/net.h>
17 #include <linux/skbuff.h>
18 #include <linux/slab.h>
19 #include <linux/udp.h>
21 #include <net/af_rxrpc.h>
22 #include "ar-internal.h"
27 void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why)
29 unsigned long t, now = jiffies;
31 read_lock_bh(&call->state_lock);
33 if (call->state < RXRPC_CALL_COMPLETE) {
35 if (time_before_eq(t, now))
38 if (time_after(call->resend_at, now) &&
39 time_before(call->resend_at, t))
42 if (time_after(call->ack_at, now) &&
43 time_before(call->ack_at, t))
46 if (call->timer.expires != t || !timer_pending(&call->timer)) {
47 mod_timer(&call->timer, t);
48 trace_rxrpc_timer(call, why, now);
53 read_unlock_bh(&call->state_lock);
57 * propose an ACK be sent
59 static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
60 u16 skew, u32 serial, bool immediate,
62 enum rxrpc_propose_ack_trace why)
64 enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
65 unsigned long now, ack_at, expiry = rxrpc_soft_ack_delay;
66 s8 prior = rxrpc_ack_priority[ack_reason];
68 /* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
69 * numbers, but we don't alter the timeout.
71 _debug("prior %u %u vs %u %u",
73 call->ackr_reason, rxrpc_ack_priority[call->ackr_reason]);
74 if (ack_reason == call->ackr_reason) {
75 if (RXRPC_ACK_UPDATEABLE & (1 << ack_reason)) {
76 outcome = rxrpc_propose_ack_update;
77 call->ackr_serial = serial;
78 call->ackr_skew = skew;
82 } else if (prior > rxrpc_ack_priority[call->ackr_reason]) {
83 call->ackr_reason = ack_reason;
84 call->ackr_serial = serial;
85 call->ackr_skew = skew;
87 outcome = rxrpc_propose_ack_subsume;
91 case RXRPC_ACK_REQUESTED:
92 if (rxrpc_requested_ack_delay < expiry)
93 expiry = rxrpc_requested_ack_delay;
99 if (rxrpc_soft_ack_delay < expiry)
100 expiry = rxrpc_soft_ack_delay;
105 if (rxrpc_idle_ack_delay < expiry)
106 expiry = rxrpc_idle_ack_delay;
115 if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
116 _debug("already scheduled");
117 } else if (immediate || expiry == 0) {
118 _debug("immediate ACK %lx", call->events);
119 if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events) &&
121 rxrpc_queue_call(call);
123 ack_at = now + expiry;
124 _debug("deferred ACK %ld < %ld", expiry, call->ack_at - now);
125 if (time_before(ack_at, call->ack_at)) {
126 call->ack_at = ack_at;
127 rxrpc_set_timer(call, rxrpc_timer_set_for_ack);
132 trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
133 background, outcome);
137 * propose an ACK be sent, locking the call structure
139 void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
140 u16 skew, u32 serial, bool immediate, bool background,
141 enum rxrpc_propose_ack_trace why)
143 spin_lock_bh(&call->lock);
144 __rxrpc_propose_ACK(call, ack_reason, skew, serial,
145 immediate, background, why);
146 spin_unlock_bh(&call->lock);
150 * Handle congestion being detected by the retransmit timeout.
152 static void rxrpc_congestion_timeout(struct rxrpc_call *call)
154 set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
158 * Perform retransmission of NAK'd and unack'd packets.
160 static void rxrpc_resend(struct rxrpc_call *call)
162 struct rxrpc_skb_priv *sp;
164 rxrpc_seq_t cursor, seq, top;
165 ktime_t now = ktime_get_real(), max_age, oldest, resend_at, ack_ts;
167 u8 annotation, anno_type, retrans = 0, unacked = 0;
169 _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
171 max_age = ktime_sub_ms(now, rxrpc_resend_timeout);
173 spin_lock_bh(&call->lock);
175 cursor = call->tx_hard_ack;
177 ASSERT(before_eq(cursor, top));
181 /* Scan the packet list without dropping the lock and decide which of
182 * the packets in the Tx buffer we're going to resend and what the new
183 * resend timeout will be.
186 for (seq = cursor + 1; before_eq(seq, top); seq++) {
187 ix = seq & RXRPC_RXTX_BUFF_MASK;
188 annotation = call->rxtx_annotations[ix];
189 anno_type = annotation & RXRPC_TX_ANNO_MASK;
190 annotation &= ~RXRPC_TX_ANNO_MASK;
191 if (anno_type == RXRPC_TX_ANNO_ACK)
194 skb = call->rxtx_buffer[ix];
195 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
198 if (anno_type == RXRPC_TX_ANNO_UNACK) {
199 if (ktime_after(skb->tstamp, max_age)) {
200 if (ktime_before(skb->tstamp, oldest))
201 oldest = skb->tstamp;
204 if (!(annotation & RXRPC_TX_ANNO_RESENT))
208 /* Okay, we need to retransmit a packet. */
209 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
211 trace_rxrpc_retransmit(call, seq, annotation | anno_type,
212 ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
215 resend_at = ktime_add_ms(oldest, rxrpc_resend_timeout);
216 call->resend_at = jiffies +
217 nsecs_to_jiffies(ktime_to_ns(ktime_sub(resend_at, now))) +
218 1; /* We have to make sure that the calculated jiffies value
219 * falls at or after the nsec value, or we shall loop
220 * ceaselessly because the timer times out, but we haven't
221 * reached the nsec timeout yet.
225 rxrpc_congestion_timeout(call);
227 /* If there was nothing that needed retransmission then it's likely
228 * that an ACK got lost somewhere. Send a ping to find out instead of
229 * retransmitting data.
232 rxrpc_set_timer(call, rxrpc_timer_set_for_resend);
233 spin_unlock_bh(&call->lock);
234 ack_ts = ktime_sub(now, call->acks_latest_ts);
235 if (ktime_to_ns(ack_ts) < call->peer->rtt)
237 rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, 0, true, false,
238 rxrpc_propose_ack_ping_for_lost_ack);
239 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
243 /* Now go through the Tx window and perform the retransmissions. We
244 * have to drop the lock for each send. If an ACK comes in whilst the
245 * lock is dropped, it may clear some of the retransmission markers for
246 * packets that it soft-ACKs.
248 for (seq = cursor + 1; before_eq(seq, top); seq++) {
249 ix = seq & RXRPC_RXTX_BUFF_MASK;
250 annotation = call->rxtx_annotations[ix];
251 anno_type = annotation & RXRPC_TX_ANNO_MASK;
252 if (anno_type != RXRPC_TX_ANNO_RETRANS)
255 skb = call->rxtx_buffer[ix];
256 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
257 spin_unlock_bh(&call->lock);
259 if (rxrpc_send_data_packet(call, skb) < 0) {
260 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
264 if (rxrpc_is_client_call(call))
265 rxrpc_expose_client_call(call);
267 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
268 spin_lock_bh(&call->lock);
270 /* We need to clear the retransmit state, but there are two
271 * things we need to be aware of: A new ACK/NAK might have been
272 * received and the packet might have been hard-ACK'd (in which
273 * case it will no longer be in the buffer).
275 if (after(seq, call->tx_hard_ack)) {
276 annotation = call->rxtx_annotations[ix];
277 anno_type = annotation & RXRPC_TX_ANNO_MASK;
278 if (anno_type == RXRPC_TX_ANNO_RETRANS ||
279 anno_type == RXRPC_TX_ANNO_NAK) {
280 annotation &= ~RXRPC_TX_ANNO_MASK;
281 annotation |= RXRPC_TX_ANNO_UNACK;
283 annotation |= RXRPC_TX_ANNO_RESENT;
284 call->rxtx_annotations[ix] = annotation;
287 if (after(call->tx_hard_ack, seq))
288 seq = call->tx_hard_ack;
292 spin_unlock_bh(&call->lock);
298 * Handle retransmission and deferred ACK/abort generation.
300 void rxrpc_process_call(struct work_struct *work)
302 struct rxrpc_call *call =
303 container_of(work, struct rxrpc_call, processor);
306 rxrpc_see_call(call);
308 //printk("\n--------------------\n");
309 _enter("{%d,%s,%lx}",
310 call->debug_id, rxrpc_call_states[call->state], call->events);
313 if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
314 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
318 if (call->state == RXRPC_CALL_COMPLETE) {
319 del_timer_sync(&call->timer);
324 if (time_after_eq(now, call->expire_at)) {
325 rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME);
326 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
330 if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events) ||
331 time_after_eq(now, call->ack_at)) {
332 call->ack_at = call->expire_at;
333 if (call->ackr_reason) {
334 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
339 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events) ||
340 time_after_eq(now, call->resend_at)) {
345 rxrpc_set_timer(call, rxrpc_timer_set_for_resend);
347 /* other events may have been raised since we started checking */
348 if (call->events && call->state < RXRPC_CALL_COMPLETE) {
349 __rxrpc_queue_call(call);
354 rxrpc_put_call(call, rxrpc_call_put);