]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/rxrpc/call_event.c
rxrpc: Make Tx loss-injection go through normal return and adjust tracing
[mirror_ubuntu-zesty-kernel.git] / net / rxrpc / call_event.c
CommitLineData
17926a79
DH
1/* 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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
14#include <linux/module.h>
15#include <linux/circ_buf.h>
16#include <linux/net.h>
17#include <linux/skbuff.h>
5a0e3ad6 18#include <linux/slab.h>
17926a79
DH
19#include <linux/udp.h>
20#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include "ar-internal.h"
23
17926a79 24/*
248f219c 25 * Set the timer
17926a79 26 */
fc7ab6d2 27void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why)
17926a79 28{
248f219c 29 unsigned long t, now = jiffies;
17926a79 30
248f219c 31 read_lock_bh(&call->state_lock);
17926a79 32
248f219c 33 if (call->state < RXRPC_CALL_COMPLETE) {
01a88f7f
DH
34 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))
248f219c 40 t = call->resend_at;
01a88f7f
DH
41
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)) {
248f219c 47 mod_timer(&call->timer, t);
fc7ab6d2 48 trace_rxrpc_timer(call, why, now);
17926a79 49 }
17926a79 50 }
01a88f7f
DH
51
52out:
17926a79
DH
53 read_unlock_bh(&call->state_lock);
54}
55
56/*
248f219c 57 * propose an ACK be sent
17926a79 58 */
248f219c
DH
59static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
60 u16 skew, u32 serial, bool immediate,
9c7ad434
DH
61 bool background,
62 enum rxrpc_propose_ack_trace why)
17926a79 63{
9c7ad434 64 enum rxrpc_propose_ack_outcome outcome = rxrpc_propose_ack_use;
248f219c 65 unsigned long now, ack_at, expiry = rxrpc_soft_ack_delay;
17926a79
DH
66 s8 prior = rxrpc_ack_priority[ack_reason];
67
248f219c
DH
68 /* 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)) {
9c7ad434 76 outcome = rxrpc_propose_ack_update;
248f219c
DH
77 call->ackr_serial = serial;
78 call->ackr_skew = skew;
17926a79 79 }
248f219c 80 if (!immediate)
9c7ad434 81 goto trace;
248f219c
DH
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;
9c7ad434
DH
86 } else {
87 outcome = rxrpc_propose_ack_subsume;
17926a79
DH
88 }
89
248f219c
DH
90 switch (ack_reason) {
91 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;
17926a79 97
248f219c
DH
98 case RXRPC_ACK_DELAY:
99 if (rxrpc_soft_ack_delay < expiry)
100 expiry = rxrpc_soft_ack_delay;
101 break;
17926a79 102
0d967960 103 case RXRPC_ACK_PING:
248f219c 104 case RXRPC_ACK_IDLE:
91c2c7b6 105 if (rxrpc_idle_ack_delay < expiry)
248f219c
DH
106 expiry = rxrpc_idle_ack_delay;
107 break;
17926a79 108
248f219c
DH
109 default:
110 immediate = true;
111 break;
17926a79
DH
112 }
113
248f219c
DH
114 now = jiffies;
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) &&
120 background)
121 rxrpc_queue_call(call);
122 } else {
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;
fc7ab6d2 127 rxrpc_set_timer(call, rxrpc_timer_set_for_ack);
17926a79
DH
128 }
129 }
9c7ad434
DH
130
131trace:
132 trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
133 background, outcome);
17926a79
DH
134}
135
136/*
248f219c 137 * propose an ACK be sent, locking the call structure
17926a79 138 */
248f219c 139void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
9c7ad434
DH
140 u16 skew, u32 serial, bool immediate, bool background,
141 enum rxrpc_propose_ack_trace why)
17926a79 142{
248f219c
DH
143 spin_lock_bh(&call->lock);
144 __rxrpc_propose_ACK(call, ack_reason, skew, serial,
9c7ad434 145 immediate, background, why);
248f219c 146 spin_unlock_bh(&call->lock);
17926a79
DH
147}
148
57494343
DH
149/*
150 * Handle congestion being detected by the retransmit timeout.
151 */
152static void rxrpc_congestion_timeout(struct rxrpc_call *call)
153{
154 set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
155}
156
17926a79 157/*
248f219c 158 * Perform retransmission of NAK'd and unack'd packets.
17926a79 159 */
248f219c 160static void rxrpc_resend(struct rxrpc_call *call)
17926a79
DH
161{
162 struct rxrpc_skb_priv *sp;
163 struct sk_buff *skb;
248f219c 164 rxrpc_seq_t cursor, seq, top;
57494343 165 ktime_t now = ktime_get_real(), max_age, oldest, resend_at, ack_ts;
248f219c 166 int ix;
57494343 167 u8 annotation, anno_type, retrans = 0, unacked = 0;
17926a79 168
248f219c 169 _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
17926a79 170
50235c4b
DH
171 max_age = ktime_sub_ms(now, rxrpc_resend_timeout);
172
17926a79
DH
173 spin_lock_bh(&call->lock);
174
248f219c
DH
175 cursor = call->tx_hard_ack;
176 top = call->tx_top;
177 ASSERT(before_eq(cursor, top));
178 if (cursor == top)
179 goto out_unlock;
180
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.
184 */
50235c4b 185 oldest = now;
dfa7d920 186 for (seq = cursor + 1; before_eq(seq, top); seq++) {
248f219c
DH
187 ix = seq & RXRPC_RXTX_BUFF_MASK;
188 annotation = call->rxtx_annotations[ix];
f07373ea
DH
189 anno_type = annotation & RXRPC_TX_ANNO_MASK;
190 annotation &= ~RXRPC_TX_ANNO_MASK;
191 if (anno_type == RXRPC_TX_ANNO_ACK)
248f219c 192 continue;
17926a79 193
248f219c 194 skb = call->rxtx_buffer[ix];
71f3ca40 195 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
17926a79
DH
196 sp = rxrpc_skb(skb);
197
f07373ea 198 if (anno_type == RXRPC_TX_ANNO_UNACK) {
50235c4b
DH
199 if (ktime_after(skb->tstamp, max_age)) {
200 if (ktime_before(skb->tstamp, oldest))
201 oldest = skb->tstamp;
248f219c 202 continue;
33c40e24 203 }
57494343
DH
204 if (!(annotation & RXRPC_TX_ANNO_RESENT))
205 unacked++;
17926a79 206 }
17926a79 207
248f219c 208 /* Okay, we need to retransmit a packet. */
f07373ea 209 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
57494343 210 retrans++;
c6672e3f
DH
211 trace_rxrpc_retransmit(call, seq, annotation | anno_type,
212 ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
dfa7d920 213 }
248f219c 214
df0562a7
DH
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.
222 */
248f219c 223
57494343
DH
224 if (unacked)
225 rxrpc_congestion_timeout(call);
226
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.
230 */
231 if (!retrans) {
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)
236 goto out;
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);
240 goto out;
241 }
242
248f219c
DH
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.
247 */
dfa7d920 248 for (seq = cursor + 1; before_eq(seq, top); seq++) {
248f219c
DH
249 ix = seq & RXRPC_RXTX_BUFF_MASK;
250 annotation = call->rxtx_annotations[ix];
f07373ea
DH
251 anno_type = annotation & RXRPC_TX_ANNO_MASK;
252 if (anno_type != RXRPC_TX_ANNO_RETRANS)
248f219c 253 continue;
17926a79 254
248f219c 255 skb = call->rxtx_buffer[ix];
71f3ca40 256 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
17926a79 257 spin_unlock_bh(&call->lock);
17926a79 258
a1767077 259 if (rxrpc_send_data_packet(call, skb, true) < 0) {
71f3ca40 260 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
248f219c
DH
261 return;
262 }
17926a79 263
248f219c
DH
264 if (rxrpc_is_client_call(call))
265 rxrpc_expose_client_call(call);
17926a79 266
71f3ca40 267 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
17926a79 268 spin_lock_bh(&call->lock);
17926a79 269
248f219c
DH
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).
274 */
f07373ea
DH
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;
282 }
283 annotation |= RXRPC_TX_ANNO_RESENT;
284 call->rxtx_annotations[ix] = annotation;
285 }
248f219c
DH
286
287 if (after(call->tx_hard_ack, seq))
288 seq = call->tx_hard_ack;
dfa7d920 289 }
248f219c
DH
290
291out_unlock:
292 spin_unlock_bh(&call->lock);
57494343 293out:
248f219c 294 _leave("");
17926a79
DH
295}
296
297/*
248f219c 298 * Handle retransmission and deferred ACK/abort generation.
17926a79
DH
299 */
300void rxrpc_process_call(struct work_struct *work)
301{
302 struct rxrpc_call *call =
303 container_of(work, struct rxrpc_call, processor);
248f219c 304 unsigned long now;
17926a79 305
e34d4234
DH
306 rxrpc_see_call(call);
307
17926a79 308 //printk("\n--------------------\n");
248f219c
DH
309 _enter("{%d,%s,%lx}",
310 call->debug_id, rxrpc_call_states[call->state], call->events);
17926a79 311
248f219c
DH
312recheck_state:
313 if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
314 rxrpc_send_call_packet(call, RXRPC_PACKET_TYPE_ABORT);
315 goto recheck_state;
17926a79
DH
316 }
317
248f219c
DH
318 if (call->state == RXRPC_CALL_COMPLETE) {
319 del_timer_sync(&call->timer);
320 goto out_put;
17926a79
DH
321 }
322
248f219c
DH
323 now = jiffies;
324 if (time_after_eq(now, call->expire_at)) {
5a42976d 325 rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, ETIME);
248f219c 326 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
57494343 327 goto recheck_state;
17926a79
DH
328 }
329
248f219c
DH
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);
335 goto recheck_state;
17926a79
DH
336 }
337 }
338
248f219c
DH
339 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events) ||
340 time_after_eq(now, call->resend_at)) {
17926a79 341 rxrpc_resend(call);
248f219c 342 goto recheck_state;
17926a79
DH
343 }
344
fc7ab6d2 345 rxrpc_set_timer(call, rxrpc_timer_set_for_resend);
17926a79
DH
346
347 /* other events may have been raised since we started checking */
248f219c 348 if (call->events && call->state < RXRPC_CALL_COMPLETE) {
8d94aa38 349 __rxrpc_queue_call(call);
248f219c 350 goto out;
17926a79
DH
351 }
352
248f219c
DH
353out_put:
354 rxrpc_put_call(call, rxrpc_call_put);
355out:
17926a79 356 _leave("");
17926a79 357}