blob: 4f00476630b93587cc83a1894fbe09337c0e2712 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/module.h>
15#include <linux/circ_buf.h>
16#include <linux/net.h>
17#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070019#include <linux/udp.h>
20#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include "ar-internal.h"
23
David Howells5873c082014-02-07 18:58:44 +000024/*
David Howells248f2192016-09-08 11:10:12 +010025 * Set the timer
26 */
David Howellsdf0adc72016-09-26 22:12:49 +010027void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why,
28 ktime_t now)
David Howells248f2192016-09-08 11:10:12 +010029{
David Howellsdf0adc72016-09-26 22:12:49 +010030 unsigned long t_j, now_j = jiffies;
31 ktime_t t;
David Howells405dea12016-09-30 09:13:50 +010032 bool queue = false;
David Howells248f2192016-09-08 11:10:12 +010033
David Howells248f2192016-09-08 11:10:12 +010034 read_lock_bh(&call->state_lock);
35
36 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells01a88f72016-09-23 12:39:22 +010037 t = call->expire_at;
David Howellsdf0adc72016-09-26 22:12:49 +010038 if (!ktime_after(t, now))
David Howells01a88f72016-09-23 12:39:22 +010039 goto out;
40
David Howells405dea12016-09-30 09:13:50 +010041 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))
44 queue = true;
45 } else if (ktime_before(call->resend_at, t)) {
David Howells248f2192016-09-08 11:10:12 +010046 t = call->resend_at;
David Howells405dea12016-09-30 09:13:50 +010047 }
David Howells01a88f72016-09-23 12:39:22 +010048
David Howells405dea12016-09-30 09:13:50 +010049 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))
52 queue = true;
53 } else if (ktime_before(call->ack_at, t)) {
David Howells01a88f72016-09-23 12:39:22 +010054 t = call->ack_at;
David Howells405dea12016-09-30 09:13:50 +010055 }
David Howells01a88f72016-09-23 12:39:22 +010056
David Howellsdf0adc72016-09-26 22:12:49 +010057 t_j = nsecs_to_jiffies(ktime_to_ns(ktime_sub(t, now)));
58 t_j += jiffies;
59
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
63 * timeout yet.
64 */
65 t_j++;
66
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);
David Howells248f2192016-09-08 11:10:12 +010070 }
David Howells405dea12016-09-30 09:13:50 +010071
72 if (queue)
73 rxrpc_queue_call(call);
David Howells248f2192016-09-08 11:10:12 +010074 }
David Howells01a88f72016-09-23 12:39:22 +010075
76out:
David Howells248f2192016-09-08 11:10:12 +010077 read_unlock_bh(&call->state_lock);
78}
79
80/*
David Howells17926a72007-04-26 15:48:28 -070081 * propose an ACK be sent
82 */
David Howells248f2192016-09-08 11:10:12 +010083static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
84 u16 skew, u32 serial, bool immediate,
David Howells9c7ad432016-09-23 13:50:40 +010085 bool background,
86 enum rxrpc_propose_ack_trace why)
David Howells17926a72007-04-26 15:48:28 -070087{
David Howells9c7ad432016-09-23 13:50:40 +010088 enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
David Howellsdf0adc72016-09-26 22:12:49 +010089 unsigned int expiry = rxrpc_soft_ack_delay;
90 ktime_t now, ack_at;
David Howells17926a72007-04-26 15:48:28 -070091 s8 prior = rxrpc_ack_priority[ack_reason];
92
David Howells248f2192016-09-08 11:10:12 +010093 /* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
94 * numbers, but we don't alter the timeout.
95 */
96 _debug("prior %u %u vs %u %u",
97 ack_reason, prior,
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)) {
David Howells9c7ad432016-09-23 13:50:40 +0100101 outcome = rxrpc_propose_ack_update;
David Howells17926a72007-04-26 15:48:28 -0700102 call->ackr_serial = serial;
David Howells248f2192016-09-08 11:10:12 +0100103 call->ackr_skew = skew;
David Howells563ea7d2016-08-23 15:27:25 +0100104 }
David Howells248f2192016-09-08 11:10:12 +0100105 if (!immediate)
David Howells9c7ad432016-09-23 13:50:40 +0100106 goto trace;
David Howells248f2192016-09-08 11:10:12 +0100107 } 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;
David Howells9c7ad432016-09-23 13:50:40 +0100111 } else {
112 outcome = rxrpc_propose_ack_subsume;
David Howells17926a72007-04-26 15:48:28 -0700113 }
114
David Howells17926a72007-04-26 15:48:28 -0700115 switch (ack_reason) {
David Howells248f2192016-09-08 11:10:12 +0100116 case RXRPC_ACK_REQUESTED:
117 if (rxrpc_requested_ack_delay < expiry)
118 expiry = rxrpc_requested_ack_delay;
119 if (serial == 1)
120 immediate = false;
121 break;
122
David Howells17926a72007-04-26 15:48:28 -0700123 case RXRPC_ACK_DELAY:
David Howells248f2192016-09-08 11:10:12 +0100124 if (rxrpc_soft_ack_delay < expiry)
125 expiry = rxrpc_soft_ack_delay;
126 break;
David Howells17926a72007-04-26 15:48:28 -0700127
David Howells0d967962016-09-24 18:05:27 +0100128 case RXRPC_ACK_PING:
David Howells17926a72007-04-26 15:48:28 -0700129 case RXRPC_ACK_IDLE:
David Howells91c2c7b2016-09-13 22:36:21 +0100130 if (rxrpc_idle_ack_delay < expiry)
David Howells5873c082014-02-07 18:58:44 +0000131 expiry = rxrpc_idle_ack_delay;
David Howells248f2192016-09-08 11:10:12 +0100132 break;
David Howells17926a72007-04-26 15:48:28 -0700133
134 default:
David Howells248f2192016-09-08 11:10:12 +0100135 immediate = true;
136 break;
David Howells17926a72007-04-26 15:48:28 -0700137 }
138
David Howells248f2192016-09-08 11:10:12 +0100139 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) &&
144 background)
145 rxrpc_queue_call(call);
146 } else {
David Howellsdf0adc72016-09-26 22:12:49 +0100147 now = ktime_get_real();
148 ack_at = ktime_add_ms(now, expiry);
149 if (ktime_before(ack_at, call->ack_at)) {
David Howells248f2192016-09-08 11:10:12 +0100150 call->ack_at = ack_at;
David Howellsdf0adc72016-09-26 22:12:49 +0100151 rxrpc_set_timer(call, rxrpc_timer_set_for_ack, now);
David Howells248f2192016-09-08 11:10:12 +0100152 }
153 }
David Howells9c7ad432016-09-23 13:50:40 +0100154
155trace:
156 trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
157 background, outcome);
David Howells17926a72007-04-26 15:48:28 -0700158}
159
160/*
161 * propose an ACK be sent, locking the call structure
162 */
David Howells4e36a952009-09-16 00:01:13 -0700163void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
David Howells9c7ad432016-09-23 13:50:40 +0100164 u16 skew, u32 serial, bool immediate, bool background,
165 enum rxrpc_propose_ack_trace why)
David Howells17926a72007-04-26 15:48:28 -0700166{
David Howells248f2192016-09-08 11:10:12 +0100167 spin_lock_bh(&call->lock);
168 __rxrpc_propose_ACK(call, ack_reason, skew, serial,
David Howells9c7ad432016-09-23 13:50:40 +0100169 immediate, background, why);
David Howells248f2192016-09-08 11:10:12 +0100170 spin_unlock_bh(&call->lock);
David Howells17926a72007-04-26 15:48:28 -0700171}
172
173/*
David Howells57494342016-09-24 18:05:27 +0100174 * Handle congestion being detected by the retransmit timeout.
175 */
176static void rxrpc_congestion_timeout(struct rxrpc_call *call)
177{
178 set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
179}
180
181/*
David Howells248f2192016-09-08 11:10:12 +0100182 * Perform retransmission of NAK'd and unack'd packets.
David Howells17926a72007-04-26 15:48:28 -0700183 */
David Howellsdf0adc72016-09-26 22:12:49 +0100184static void rxrpc_resend(struct rxrpc_call *call, ktime_t now)
David Howells17926a72007-04-26 15:48:28 -0700185{
186 struct rxrpc_skb_priv *sp;
David Howells17926a72007-04-26 15:48:28 -0700187 struct sk_buff *skb;
David Howells248f2192016-09-08 11:10:12 +0100188 rxrpc_seq_t cursor, seq, top;
David Howellsdf0adc72016-09-26 22:12:49 +0100189 ktime_t max_age, oldest, ack_ts;
David Howells248f2192016-09-08 11:10:12 +0100190 int ix;
David Howells57494342016-09-24 18:05:27 +0100191 u8 annotation, anno_type, retrans = 0, unacked = 0;
David Howells17926a72007-04-26 15:48:28 -0700192
David Howells248f2192016-09-08 11:10:12 +0100193 _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
David Howells17926a72007-04-26 15:48:28 -0700194
David Howells50235c42016-09-22 00:29:31 +0100195 max_age = ktime_sub_ms(now, rxrpc_resend_timeout);
196
David Howells17926a72007-04-26 15:48:28 -0700197 spin_lock_bh(&call->lock);
198
David Howells248f2192016-09-08 11:10:12 +0100199 cursor = call->tx_hard_ack;
200 top = call->tx_top;
201 ASSERT(before_eq(cursor, top));
202 if (cursor == top)
203 goto out_unlock;
David Howells17926a72007-04-26 15:48:28 -0700204
David Howells248f2192016-09-08 11:10:12 +0100205 /* 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.
208 */
David Howells50235c42016-09-22 00:29:31 +0100209 oldest = now;
David Howellsdfa7d922016-09-17 10:49:12 +0100210 for (seq = cursor + 1; before_eq(seq, top); seq++) {
David Howells248f2192016-09-08 11:10:12 +0100211 ix = seq & RXRPC_RXTX_BUFF_MASK;
212 annotation = call->rxtx_annotations[ix];
David Howellsf07373e2016-09-22 00:29:32 +0100213 anno_type = annotation & RXRPC_TX_ANNO_MASK;
214 annotation &= ~RXRPC_TX_ANNO_MASK;
215 if (anno_type == RXRPC_TX_ANNO_ACK)
David Howells248f2192016-09-08 11:10:12 +0100216 continue;
217
218 skb = call->rxtx_buffer[ix];
David Howells71f3ca42016-09-17 10:49:14 +0100219 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
David Howells17926a72007-04-26 15:48:28 -0700220 sp = rxrpc_skb(skb);
221
David Howellsf07373e2016-09-22 00:29:32 +0100222 if (anno_type == RXRPC_TX_ANNO_UNACK) {
David Howells50235c42016-09-22 00:29:31 +0100223 if (ktime_after(skb->tstamp, max_age)) {
224 if (ktime_before(skb->tstamp, oldest))
225 oldest = skb->tstamp;
David Howells248f2192016-09-08 11:10:12 +0100226 continue;
227 }
David Howells57494342016-09-24 18:05:27 +0100228 if (!(annotation & RXRPC_TX_ANNO_RESENT))
229 unacked++;
David Howells17926a72007-04-26 15:48:28 -0700230 }
David Howells17926a72007-04-26 15:48:28 -0700231
David Howells248f2192016-09-08 11:10:12 +0100232 /* Okay, we need to retransmit a packet. */
David Howellsf07373e2016-09-22 00:29:32 +0100233 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
David Howells57494342016-09-24 18:05:27 +0100234 retrans++;
David Howellsc6672e32016-09-23 13:58:55 +0100235 trace_rxrpc_retransmit(call, seq, annotation | anno_type,
236 ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
David Howellsdfa7d922016-09-17 10:49:12 +0100237 }
David Howells17926a72007-04-26 15:48:28 -0700238
David Howellsdf0adc72016-09-26 22:12:49 +0100239 call->resend_at = ktime_add_ms(oldest, rxrpc_resend_timeout);
David Howells17926a72007-04-26 15:48:28 -0700240
David Howells57494342016-09-24 18:05:27 +0100241 if (unacked)
242 rxrpc_congestion_timeout(call);
243
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.
247 */
248 if (!retrans) {
David Howellsdf0adc72016-09-26 22:12:49 +0100249 rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now);
David Howells57494342016-09-24 18:05:27 +0100250 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)
253 goto out;
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);
257 goto out;
258 }
259
David Howells248f2192016-09-08 11:10:12 +0100260 /* 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.
264 */
David Howellsdfa7d922016-09-17 10:49:12 +0100265 for (seq = cursor + 1; before_eq(seq, top); seq++) {
David Howells248f2192016-09-08 11:10:12 +0100266 ix = seq & RXRPC_RXTX_BUFF_MASK;
267 annotation = call->rxtx_annotations[ix];
David Howellsf07373e2016-09-22 00:29:32 +0100268 anno_type = annotation & RXRPC_TX_ANNO_MASK;
269 if (anno_type != RXRPC_TX_ANNO_RETRANS)
David Howells248f2192016-09-08 11:10:12 +0100270 continue;
David Howells17926a72007-04-26 15:48:28 -0700271
David Howells248f2192016-09-08 11:10:12 +0100272 skb = call->rxtx_buffer[ix];
David Howells71f3ca42016-09-17 10:49:14 +0100273 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
David Howells248f2192016-09-08 11:10:12 +0100274 spin_unlock_bh(&call->lock);
David Howells248f2192016-09-08 11:10:12 +0100275
David Howellsa1767072016-09-29 22:37:15 +0100276 if (rxrpc_send_data_packet(call, skb, true) < 0) {
David Howells71f3ca42016-09-17 10:49:14 +0100277 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells248f2192016-09-08 11:10:12 +0100278 return;
279 }
280
281 if (rxrpc_is_client_call(call))
282 rxrpc_expose_client_call(call);
David Howells248f2192016-09-08 11:10:12 +0100283
David Howells71f3ca42016-09-17 10:49:14 +0100284 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells17926a72007-04-26 15:48:28 -0700285 spin_lock_bh(&call->lock);
David Howells17926a72007-04-26 15:48:28 -0700286
David Howells248f2192016-09-08 11:10:12 +0100287 /* 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).
291 */
David Howellsf07373e2016-09-22 00:29:32 +0100292 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;
299 }
300 annotation |= RXRPC_TX_ANNO_RESENT;
301 call->rxtx_annotations[ix] = annotation;
302 }
David Howells17926a72007-04-26 15:48:28 -0700303
David Howells248f2192016-09-08 11:10:12 +0100304 if (after(call->tx_hard_ack, seq))
305 seq = call->tx_hard_ack;
David Howellsdfa7d922016-09-17 10:49:12 +0100306 }
David Howells248f2192016-09-08 11:10:12 +0100307
308out_unlock:
309 spin_unlock_bh(&call->lock);
David Howells57494342016-09-24 18:05:27 +0100310out:
David Howells248f2192016-09-08 11:10:12 +0100311 _leave("");
David Howells17926a72007-04-26 15:48:28 -0700312}
313
314/*
David Howells248f2192016-09-08 11:10:12 +0100315 * Handle retransmission and deferred ACK/abort generation.
David Howells17926a72007-04-26 15:48:28 -0700316 */
317void rxrpc_process_call(struct work_struct *work)
318{
319 struct rxrpc_call *call =
320 container_of(work, struct rxrpc_call, processor);
David Howellsdf0adc72016-09-26 22:12:49 +0100321 ktime_t now;
David Howells17926a72007-04-26 15:48:28 -0700322
David Howellse34d4232016-08-30 09:49:29 +0100323 rxrpc_see_call(call);
324
David Howells17926a72007-04-26 15:48:28 -0700325 //printk("\n--------------------\n");
David Howells248f2192016-09-08 11:10:12 +0100326 _enter("{%d,%s,%lx}",
327 call->debug_id, rxrpc_call_states[call->state], call->events);
David Howells17926a72007-04-26 15:48:28 -0700328
David Howells248f2192016-09-08 11:10:12 +0100329recheck_state:
330 if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
331 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
332 goto recheck_state;
David Howells8d94aa32016-09-07 09:19:31 +0100333 }
334
David Howells248f2192016-09-08 11:10:12 +0100335 if (call->state == RXRPC_CALL_COMPLETE) {
336 del_timer_sync(&call->timer);
337 goto out_put;
David Howells17926a72007-04-26 15:48:28 -0700338 }
339
David Howellsdf0adc72016-09-26 22:12:49 +0100340 now = ktime_get_real();
341 if (ktime_before(call->expire_at, now)) {
David Howells5a429762016-09-06 22:19:51 +0100342 rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME);
David Howells248f2192016-09-08 11:10:12 +0100343 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells57494342016-09-24 18:05:27 +0100344 goto recheck_state;
David Howells17926a72007-04-26 15:48:28 -0700345 }
346
David Howells405dea12016-09-30 09:13:50 +0100347 if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events)) {
David Howells248f2192016-09-08 11:10:12 +0100348 call->ack_at = call->expire_at;
349 if (call->ackr_reason) {
350 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
351 goto recheck_state;
David Howells17926a72007-04-26 15:48:28 -0700352 }
353 }
354
David Howells405dea12016-09-30 09:13:50 +0100355 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events)) {
David Howellsdf0adc72016-09-26 22:12:49 +0100356 rxrpc_resend(call, now);
David Howells248f2192016-09-08 11:10:12 +0100357 goto recheck_state;
David Howells17926a72007-04-26 15:48:28 -0700358 }
359
David Howellsdf0adc72016-09-26 22:12:49 +0100360 rxrpc_set_timer(call, rxrpc_timer_set_for_resend, now);
David Howells17926a72007-04-26 15:48:28 -0700361
362 /* other events may have been raised since we started checking */
David Howells248f2192016-09-08 11:10:12 +0100363 if (call->events && call->state < RXRPC_CALL_COMPLETE) {
David Howells8d94aa32016-09-07 09:19:31 +0100364 __rxrpc_queue_call(call);
David Howells248f2192016-09-08 11:10:12 +0100365 goto out;
David Howells17926a72007-04-26 15:48:28 -0700366 }
367
David Howells248f2192016-09-08 11:10:12 +0100368out_put:
369 rxrpc_put_call(call, rxrpc_call_put);
370out:
David Howells17926a72007-04-26 15:48:28 -0700371 _leave("");
David Howells17926a72007-04-26 15:48:28 -0700372}