blob: a78a92fe5d7776bfa9f13e83c574fba4a12d726c [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 Howellsfc7ab6d2016-09-23 15:22:36 +010027void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why)
David Howells248f2192016-09-08 11:10:12 +010028{
29 unsigned long t, now = jiffies;
30
David Howells248f2192016-09-08 11:10:12 +010031 read_lock_bh(&call->state_lock);
32
33 if (call->state < RXRPC_CALL_COMPLETE) {
David Howells01a88f72016-09-23 12:39:22 +010034 t = call->expire_at;
35 if (time_before_eq(t, now))
36 goto out;
37
38 if (time_after(call->resend_at, now) &&
39 time_before(call->resend_at, t))
David Howells248f2192016-09-08 11:10:12 +010040 t = call->resend_at;
David Howells01a88f72016-09-23 12:39:22 +010041
42 if (time_after(call->ack_at, now) &&
43 time_before(call->ack_at, t))
44 t = call->ack_at;
45
46 if (call->timer.expires != t || !timer_pending(&call->timer)) {
David Howells248f2192016-09-08 11:10:12 +010047 mod_timer(&call->timer, t);
David Howellsfc7ab6d2016-09-23 15:22:36 +010048 trace_rxrpc_timer(call, why, now);
David Howells248f2192016-09-08 11:10:12 +010049 }
50 }
David Howells01a88f72016-09-23 12:39:22 +010051
52out:
David Howells248f2192016-09-08 11:10:12 +010053 read_unlock_bh(&call->state_lock);
54}
55
56/*
David Howells17926a72007-04-26 15:48:28 -070057 * propose an ACK be sent
58 */
David Howells248f2192016-09-08 11:10:12 +010059static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
60 u16 skew, u32 serial, bool immediate,
David Howells9c7ad432016-09-23 13:50:40 +010061 bool background,
62 enum rxrpc_propose_ack_trace why)
David Howells17926a72007-04-26 15:48:28 -070063{
David Howells9c7ad432016-09-23 13:50:40 +010064 enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
David Howells248f2192016-09-08 11:10:12 +010065 unsigned long now, ack_at, expiry = rxrpc_soft_ack_delay;
David Howells17926a72007-04-26 15:48:28 -070066 s8 prior = rxrpc_ack_priority[ack_reason];
67
David Howells248f2192016-09-08 11:10:12 +010068 /* Update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
69 * numbers, but we don't alter the timeout.
70 */
71 _debug("prior %u %u vs %u %u",
72 ack_reason, prior,
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)) {
David Howells9c7ad432016-09-23 13:50:40 +010076 outcome = rxrpc_propose_ack_update;
David Howells17926a72007-04-26 15:48:28 -070077 call->ackr_serial = serial;
David Howells248f2192016-09-08 11:10:12 +010078 call->ackr_skew = skew;
David Howells563ea7d2016-08-23 15:27:25 +010079 }
David Howells248f2192016-09-08 11:10:12 +010080 if (!immediate)
David Howells9c7ad432016-09-23 13:50:40 +010081 goto trace;
David Howells248f2192016-09-08 11:10:12 +010082 } 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;
David Howells9c7ad432016-09-23 13:50:40 +010086 } else {
87 outcome = rxrpc_propose_ack_subsume;
David Howells17926a72007-04-26 15:48:28 -070088 }
89
David Howells17926a72007-04-26 15:48:28 -070090 switch (ack_reason) {
David Howells248f2192016-09-08 11:10:12 +010091 case RXRPC_ACK_REQUESTED:
92 if (rxrpc_requested_ack_delay < expiry)
93 expiry = rxrpc_requested_ack_delay;
94 if (serial == 1)
95 immediate = false;
96 break;
97
David Howells17926a72007-04-26 15:48:28 -070098 case RXRPC_ACK_DELAY:
David Howells248f2192016-09-08 11:10:12 +010099 if (rxrpc_soft_ack_delay < expiry)
100 expiry = rxrpc_soft_ack_delay;
101 break;
David Howells17926a72007-04-26 15:48:28 -0700102
103 case RXRPC_ACK_IDLE:
David Howells91c2c7b2016-09-13 22:36:21 +0100104 if (rxrpc_idle_ack_delay < expiry)
David Howells5873c082014-02-07 18:58:44 +0000105 expiry = rxrpc_idle_ack_delay;
David Howells248f2192016-09-08 11:10:12 +0100106 break;
David Howells17926a72007-04-26 15:48:28 -0700107
108 default:
David Howells248f2192016-09-08 11:10:12 +0100109 immediate = true;
110 break;
David Howells17926a72007-04-26 15:48:28 -0700111 }
112
David Howells248f2192016-09-08 11:10:12 +0100113 now = jiffies;
114 if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
115 _debug("already scheduled");
116 } else if (immediate || expiry == 0) {
117 _debug("immediate ACK %lx", call->events);
118 if (!test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events) &&
119 background)
120 rxrpc_queue_call(call);
121 } else {
122 ack_at = now + expiry;
123 _debug("deferred ACK %ld < %ld", expiry, call->ack_at - now);
124 if (time_before(ack_at, call->ack_at)) {
125 call->ack_at = ack_at;
David Howellsfc7ab6d2016-09-23 15:22:36 +0100126 rxrpc_set_timer(call, rxrpc_timer_set_for_ack);
David Howells248f2192016-09-08 11:10:12 +0100127 }
128 }
David Howells9c7ad432016-09-23 13:50:40 +0100129
130trace:
131 trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
132 background, outcome);
David Howells17926a72007-04-26 15:48:28 -0700133}
134
135/*
136 * propose an ACK be sent, locking the call structure
137 */
David Howells4e36a952009-09-16 00:01:13 -0700138void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
David Howells9c7ad432016-09-23 13:50:40 +0100139 u16 skew, u32 serial, bool immediate, bool background,
140 enum rxrpc_propose_ack_trace why)
David Howells17926a72007-04-26 15:48:28 -0700141{
David Howells248f2192016-09-08 11:10:12 +0100142 spin_lock_bh(&call->lock);
143 __rxrpc_propose_ACK(call, ack_reason, skew, serial,
David Howells9c7ad432016-09-23 13:50:40 +0100144 immediate, background, why);
David Howells248f2192016-09-08 11:10:12 +0100145 spin_unlock_bh(&call->lock);
David Howells17926a72007-04-26 15:48:28 -0700146}
147
148/*
David Howells248f2192016-09-08 11:10:12 +0100149 * Perform retransmission of NAK'd and unack'd packets.
David Howells17926a72007-04-26 15:48:28 -0700150 */
151static void rxrpc_resend(struct rxrpc_call *call)
152{
153 struct rxrpc_skb_priv *sp;
David Howells17926a72007-04-26 15:48:28 -0700154 struct sk_buff *skb;
David Howells248f2192016-09-08 11:10:12 +0100155 rxrpc_seq_t cursor, seq, top;
David Howellsfc943f62016-09-22 00:29:32 +0100156 ktime_t now = ktime_get_real(), max_age, oldest, resend_at;
David Howells248f2192016-09-08 11:10:12 +0100157 int ix;
David Howellsf07373e2016-09-22 00:29:32 +0100158 u8 annotation, anno_type;
David Howells17926a72007-04-26 15:48:28 -0700159
David Howells248f2192016-09-08 11:10:12 +0100160 _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
David Howells17926a72007-04-26 15:48:28 -0700161
David Howells50235c42016-09-22 00:29:31 +0100162 max_age = ktime_sub_ms(now, rxrpc_resend_timeout);
163
David Howells17926a72007-04-26 15:48:28 -0700164 spin_lock_bh(&call->lock);
165
David Howells248f2192016-09-08 11:10:12 +0100166 cursor = call->tx_hard_ack;
167 top = call->tx_top;
168 ASSERT(before_eq(cursor, top));
169 if (cursor == top)
170 goto out_unlock;
David Howells17926a72007-04-26 15:48:28 -0700171
David Howells248f2192016-09-08 11:10:12 +0100172 /* Scan the packet list without dropping the lock and decide which of
173 * the packets in the Tx buffer we're going to resend and what the new
174 * resend timeout will be.
175 */
David Howells50235c42016-09-22 00:29:31 +0100176 oldest = now;
David Howellsdfa7d922016-09-17 10:49:12 +0100177 for (seq = cursor + 1; before_eq(seq, top); seq++) {
David Howells248f2192016-09-08 11:10:12 +0100178 ix = seq & RXRPC_RXTX_BUFF_MASK;
179 annotation = call->rxtx_annotations[ix];
David Howellsf07373e2016-09-22 00:29:32 +0100180 anno_type = annotation & RXRPC_TX_ANNO_MASK;
181 annotation &= ~RXRPC_TX_ANNO_MASK;
182 if (anno_type == RXRPC_TX_ANNO_ACK)
David Howells248f2192016-09-08 11:10:12 +0100183 continue;
184
185 skb = call->rxtx_buffer[ix];
David Howells71f3ca42016-09-17 10:49:14 +0100186 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
David Howells17926a72007-04-26 15:48:28 -0700187 sp = rxrpc_skb(skb);
188
David Howellsf07373e2016-09-22 00:29:32 +0100189 if (anno_type == RXRPC_TX_ANNO_UNACK) {
David Howells50235c42016-09-22 00:29:31 +0100190 if (ktime_after(skb->tstamp, max_age)) {
191 if (ktime_before(skb->tstamp, oldest))
192 oldest = skb->tstamp;
David Howells248f2192016-09-08 11:10:12 +0100193 continue;
194 }
David Howells17926a72007-04-26 15:48:28 -0700195 }
David Howells17926a72007-04-26 15:48:28 -0700196
David Howells248f2192016-09-08 11:10:12 +0100197 /* Okay, we need to retransmit a packet. */
David Howellsf07373e2016-09-22 00:29:32 +0100198 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
David Howellsc6672e32016-09-23 13:58:55 +0100199 trace_rxrpc_retransmit(call, seq, annotation | anno_type,
200 ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
David Howellsdfa7d922016-09-17 10:49:12 +0100201 }
David Howells17926a72007-04-26 15:48:28 -0700202
David Howells90bd6842016-09-23 12:39:23 +0100203 resend_at = ktime_sub(ktime_add_ms(oldest, rxrpc_resend_timeout), now);
David Howells50235c42016-09-22 00:29:31 +0100204 call->resend_at = jiffies + nsecs_to_jiffies(ktime_to_ns(resend_at));
David Howells17926a72007-04-26 15:48:28 -0700205
David Howells248f2192016-09-08 11:10:12 +0100206 /* Now go through the Tx window and perform the retransmissions. We
207 * have to drop the lock for each send. If an ACK comes in whilst the
208 * lock is dropped, it may clear some of the retransmission markers for
209 * packets that it soft-ACKs.
210 */
David Howellsdfa7d922016-09-17 10:49:12 +0100211 for (seq = cursor + 1; before_eq(seq, top); seq++) {
David Howells248f2192016-09-08 11:10:12 +0100212 ix = seq & RXRPC_RXTX_BUFF_MASK;
213 annotation = call->rxtx_annotations[ix];
David Howellsf07373e2016-09-22 00:29:32 +0100214 anno_type = annotation & RXRPC_TX_ANNO_MASK;
215 if (anno_type != RXRPC_TX_ANNO_RETRANS)
David Howells248f2192016-09-08 11:10:12 +0100216 continue;
David Howells17926a72007-04-26 15:48:28 -0700217
David Howells248f2192016-09-08 11:10:12 +0100218 skb = call->rxtx_buffer[ix];
David Howells71f3ca42016-09-17 10:49:14 +0100219 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
David Howells248f2192016-09-08 11:10:12 +0100220 spin_unlock_bh(&call->lock);
David Howells248f2192016-09-08 11:10:12 +0100221
David Howells5a924b82016-09-22 00:29:31 +0100222 if (rxrpc_send_data_packet(call, skb) < 0) {
David Howells71f3ca42016-09-17 10:49:14 +0100223 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells248f2192016-09-08 11:10:12 +0100224 return;
225 }
226
227 if (rxrpc_is_client_call(call))
228 rxrpc_expose_client_call(call);
David Howells248f2192016-09-08 11:10:12 +0100229
David Howells71f3ca42016-09-17 10:49:14 +0100230 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
David Howells17926a72007-04-26 15:48:28 -0700231 spin_lock_bh(&call->lock);
David Howells17926a72007-04-26 15:48:28 -0700232
David Howells248f2192016-09-08 11:10:12 +0100233 /* We need to clear the retransmit state, but there are two
234 * things we need to be aware of: A new ACK/NAK might have been
235 * received and the packet might have been hard-ACK'd (in which
236 * case it will no longer be in the buffer).
237 */
David Howellsf07373e2016-09-22 00:29:32 +0100238 if (after(seq, call->tx_hard_ack)) {
239 annotation = call->rxtx_annotations[ix];
240 anno_type = annotation & RXRPC_TX_ANNO_MASK;
241 if (anno_type == RXRPC_TX_ANNO_RETRANS ||
242 anno_type == RXRPC_TX_ANNO_NAK) {
243 annotation &= ~RXRPC_TX_ANNO_MASK;
244 annotation |= RXRPC_TX_ANNO_UNACK;
245 }
246 annotation |= RXRPC_TX_ANNO_RESENT;
247 call->rxtx_annotations[ix] = annotation;
248 }
David Howells17926a72007-04-26 15:48:28 -0700249
David Howells248f2192016-09-08 11:10:12 +0100250 if (after(call->tx_hard_ack, seq))
251 seq = call->tx_hard_ack;
David Howellsdfa7d922016-09-17 10:49:12 +0100252 }
David Howells248f2192016-09-08 11:10:12 +0100253
254out_unlock:
255 spin_unlock_bh(&call->lock);
256 _leave("");
David Howells17926a72007-04-26 15:48:28 -0700257}
258
259/*
David Howells248f2192016-09-08 11:10:12 +0100260 * Handle retransmission and deferred ACK/abort generation.
David Howells17926a72007-04-26 15:48:28 -0700261 */
262void rxrpc_process_call(struct work_struct *work)
263{
264 struct rxrpc_call *call =
265 container_of(work, struct rxrpc_call, processor);
David Howells248f2192016-09-08 11:10:12 +0100266 unsigned long now;
David Howells17926a72007-04-26 15:48:28 -0700267
David Howellse34d4232016-08-30 09:49:29 +0100268 rxrpc_see_call(call);
269
David Howells17926a72007-04-26 15:48:28 -0700270 //printk("\n--------------------\n");
David Howells248f2192016-09-08 11:10:12 +0100271 _enter("{%d,%s,%lx}",
272 call->debug_id, rxrpc_call_states[call->state], call->events);
David Howells17926a72007-04-26 15:48:28 -0700273
David Howells248f2192016-09-08 11:10:12 +0100274recheck_state:
275 if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
276 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
277 goto recheck_state;
David Howells8d94aa32016-09-07 09:19:31 +0100278 }
279
David Howells248f2192016-09-08 11:10:12 +0100280 if (call->state == RXRPC_CALL_COMPLETE) {
281 del_timer_sync(&call->timer);
282 goto out_put;
David Howells17926a72007-04-26 15:48:28 -0700283 }
284
David Howells248f2192016-09-08 11:10:12 +0100285 now = jiffies;
286 if (time_after_eq(now, call->expire_at)) {
David Howells5a429762016-09-06 22:19:51 +0100287 rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME);
David Howells248f2192016-09-08 11:10:12 +0100288 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700289 }
290
David Howells248f2192016-09-08 11:10:12 +0100291 if (test_and_clear_bit(RXRPC_CALL_EV_ACK, &call->events) ||
292 time_after_eq(now, call->ack_at)) {
293 call->ack_at = call->expire_at;
294 if (call->ackr_reason) {
295 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ACK);
296 goto recheck_state;
David Howells17926a72007-04-26 15:48:28 -0700297 }
298 }
299
David Howells248f2192016-09-08 11:10:12 +0100300 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events) ||
301 time_after_eq(now, call->resend_at)) {
David Howells17926a72007-04-26 15:48:28 -0700302 rxrpc_resend(call);
David Howells248f2192016-09-08 11:10:12 +0100303 goto recheck_state;
David Howells17926a72007-04-26 15:48:28 -0700304 }
305
David Howellsfc7ab6d2016-09-23 15:22:36 +0100306 rxrpc_set_timer(call, rxrpc_timer_set_for_resend);
David Howells17926a72007-04-26 15:48:28 -0700307
308 /* other events may have been raised since we started checking */
David Howells248f2192016-09-08 11:10:12 +0100309 if (call->events && call->state < RXRPC_CALL_COMPLETE) {
David Howells8d94aa32016-09-07 09:19:31 +0100310 __rxrpc_queue_call(call);
David Howells248f2192016-09-08 11:10:12 +0100311 goto out;
David Howells17926a72007-04-26 15:48:28 -0700312 }
313
David Howells248f2192016-09-08 11:10:12 +0100314out_put:
315 rxrpc_put_call(call, rxrpc_call_put);
316out:
David Howells17926a72007-04-26 15:48:28 -0700317 _leave("");
David Howells17926a72007-04-26 15:48:28 -0700318}