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,
30 unsigned long t_j, now_j = jiffies;
34 read_lock_bh(&call->state_lock);
36 if (call->state < RXRPC_CALL_COMPLETE) {
38 if (!ktime_after(t, now))
41 if (!ktime_after(call->resend_at, now)) {
42 call->resend_at = call->expire_at;
43 if (!test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
45 } else if (ktime_before(call->resend_at, t)) {
49 if (!ktime_after(call->ack_at, now)) {
50 call->ack_at = call->expire_at;
51 if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
53 } else if (ktime_before(call->ack_at, t)) {
57 t_j = nsecs_to_jiffies(ktime_to_ns(ktime_sub(t, now)));
60 /* We have to make sure that the calculated jiffies value falls
61 * at or after the nsec value, or we may loop ceaselessly
62 * because the timer times out, but we haven't reached the nsec
67 if (call->timer.expires != t_j || !timer_pending(&call->timer)) {
68 mod_timer(&call->timer, t_j);
69 trace_rxrpc_timer(call, why, now, now_j);
73 rxrpc_queue_call(call);
77 read_unlock_bh(&call->state_lock);
81 * propose an ACK be sent
83 static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
84 u16 skew, u32 serial, bool immediate,
86 enum rxrpc_propose_ack_trace why)
88 enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
89 unsigned int expiry = rxrpc_soft_ack_delay;
91 s8 prior = rxrpc_ack_priority[ack_reason];
93 /* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
94 * numbers, but we don't alter the timeout.
96 _debug("prior %u %u vs %u %u",
98 call->ackr_reason, rxrpc_ack_priority[call->ackr_reason]);
99 if (ack_reason == call->ackr_reason) {
100 if (RXRPC_ACK_UPDATEABLE & (1 << ack_reason)) {
101 outcome = rxrpc_propose_ack_update;
102 call->ackr_serial = serial;
103 call->ackr_skew = skew;
107 } else if (prior > rxrpc_ack_priority[call->ackr_reason]) {
108 call->ackr_reason = ack_reason;
109 call->ackr_serial = serial;
110 call->ackr_skew = skew;
112 outcome = rxrpc_propose_ack_subsume;
115 switch (ack_reason) {
116 case RXRPC_ACK_REQUESTED:
117 if (rxrpc_requested_ack_delay < expiry)
118 expiry = rxrpc_requested_ack_delay;
123 case RXRPC_ACK_DELAY:
124 if (rxrpc_soft_ack_delay < expiry)
125 expiry = rxrpc_soft_ack_delay;
130 if (rxrpc_idle_ack_delay < expiry)
131 expiry = rxrpc_idle_ack_delay;
139 if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
140 _debug("already scheduled");
141 } else if (immediate || expiry == 0) {
142 _debug("immediate ACK %lx", call->events);
143 if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events) &&
145 rxrpc_queue_call(call);
147 now = ktime_get_real();
148 ack_at = ktime_add_ms(now, expiry);
149 if (ktime_before(ack_at, call->ack_at)) {
150 call->ack_at = ack_at;
151 rxrpc_set_timer(call, rxrpc_timer_set_for_ack, now);
156 trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
157 background, outcome);
161 * propose an ACK be sent, locking the call structure
163 void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
164 u16 skew, u32 serial, bool immediate, bool background,
165 enum rxrpc_propose_ack_trace why)
167 spin_lock_bh(&call->lock);
168 __rxrpc_propose_ACK(call, ack_reason, skew, serial,
169 immediate, background, why);
170 spin_unlock_bh(&call->lock);
174 * Handle congestion being detected by the retransmit timeout.
176 static void rxrpc_congestion_timeout(struct rxrpc_call *call)
178 set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
182 * Perform retransmission of NAK'd and unack'd packets.
184 static void rxrpc_resend(struct rxrpc_call *call, ktime_t now)
186 struct rxrpc_skb_priv *sp;
188 rxrpc_seq_t cursor, seq, top;
189 ktime_t max_age, oldest, ack_ts;
191 u8 annotation, anno_type, retrans = 0, unacked = 0;
193 _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
195 max_age = ktime_sub_ms(now, rxrpc_resend_timeout);
197 spin_lock_bh(&call->lock);
199 cursor = call->tx_hard_ack;
201 ASSERT(before_eq(cursor, top));
205 /* Scan the packet list without dropping the lock and decide which of
206 * the packets in the Tx buffer we're going to resend and what the new
207 * resend timeout will be.
210 for (seq = cursor + 1; before_eq(seq, top); seq++) {
211 ix = seq & RXRPC_RXTX_BUFF_MASK;
212 annotation = call->rxtx_annotations[ix];
213 anno_type = annotation & RXRPC_TX_ANNO_MASK;
214 annotation &= ~RXRPC_TX_ANNO_MASK;
215 if (anno_type == RXRPC_TX_ANNO_ACK)
218 skb = call->rxtx_buffer[ix];
219 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
222 if (anno_type == RXRPC_TX_ANNO_UNACK) {
223 if (ktime_after(skb->tstamp, max_age)) {
224 if (ktime_before(skb->tstamp, oldest))
225 oldest = skb->tstamp;
228 if (!(annotation & RXRPC_TX_ANNO_RESENT))
232 /* Okay, we need to retransmit a packet. */
233 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
235 trace_rxrpc_retransmit(call, seq, annotation | anno_type,
236 ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
239 call->resend_at = ktime_add_ms(oldest, rxrpc_resend_timeout);
242 rxrpc_congestion_timeout(call);
244 /* If there was nothing that needed retransmission then it's likely
245 * that an ACK got lost somewhere. Send a ping to find out instead of
246 * retransmitting data.
249 rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now);
250 spin_unlock_bh(&call->lock);
251 ack_ts = ktime_sub(now, call->acks_latest_ts);
252 if (ktime_to_ns(ack_ts) < call->peer->rtt)
254 rxrpc_propose_ACK(call, RXRPC_ACK_PING, 0, 0, true, false,
255 rxrpc_propose_ack_ping_for_lost_ack);
256 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
260 /* Now go through the Tx window and perform the retransmissions. We
261 * have to drop the lock for each send. If an ACK comes in whilst the
262 * lock is dropped, it may clear some of the retransmission markers for
263 * packets that it soft-ACKs.
265 for (seq = cursor + 1; before_eq(seq, top); seq++) {
266 ix = seq & RXRPC_RXTX_BUFF_MASK;
267 annotation = call->rxtx_annotations[ix];
268 anno_type = annotation & RXRPC_TX_ANNO_MASK;
269 if (anno_type != RXRPC_TX_ANNO_RETRANS)
272 skb = call->rxtx_buffer[ix];
273 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
274 spin_unlock_bh(&call->lock);
276 if (rxrpc_send_data_packet(call, skb, true) < 0) {
277 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
281 if (rxrpc_is_client_call(call))
282 rxrpc_expose_client_call(call);
284 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
285 spin_lock_bh(&call->lock);
287 /* We need to clear the retransmit state, but there are two
288 * things we need to be aware of: A new ACK/NAK might have been
289 * received and the packet might have been hard-ACK'd (in which
290 * case it will no longer be in the buffer).
292 if (after(seq, call->tx_hard_ack)) {
293 annotation = call->rxtx_annotations[ix];
294 anno_type = annotation & RXRPC_TX_ANNO_MASK;
295 if (anno_type == RXRPC_TX_ANNO_RETRANS ||
296 anno_type == RXRPC_TX_ANNO_NAK) {
297 annotation &= ~RXRPC_TX_ANNO_MASK;
298 annotation |= RXRPC_TX_ANNO_UNACK;
300 annotation |= RXRPC_TX_ANNO_RESENT;
301 call->rxtx_annotations[ix] = annotation;
304 if (after(call->tx_hard_ack, seq))
305 seq = call->tx_hard_ack;
309 spin_unlock_bh(&call->lock);
315 * Handle retransmission and deferred ACK/abort generation.
317 void rxrpc_process_call(struct work_struct *work)
319 struct rxrpc_call *call =
320 container_of(work, struct rxrpc_call, processor);
323 rxrpc_see_call(call);
325 //printk("\n--------------------\n");
326 _enter("{%d,%s,%lx}",
327 call->debug_id, rxrpc_call_states[call->state], call->events);
330 if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
331 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
335 if (call->state == RXRPC_CALL_COMPLETE) {
336 del_timer_sync(&call->timer);
340 now = ktime_get_real();
341 if (ktime_before(call->expire_at, now)) {
342 rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME);
343 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
347 if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events)) {
348 call->ack_at = call->expire_at;
349 if (call->ackr_reason) {
350 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
355 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events)) {
356 rxrpc_resend(call, now);
360 rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now);
362 /* other events may have been raised since we started checking */
363 if (call->events && call->state < RXRPC_CALL_COMPLETE) {
364 __rxrpc_queue_call(call);
369 rxrpc_put_call(call, rxrpc_call_put);