Merge branch '40GbE' of git://git.kernel.org/pub/scm/linux/kernel/git/jkirsher/next...
[cascardo/linux.git] / net / rxrpc / call_event.c
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
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
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>
20 #include <net/sock.h>
21 #include <net/af_rxrpc.h>
22 #include "ar-internal.h"
23
24 /*
25  * Set the timer
26  */
27 void rxrpc_set_timer(struct rxrpc_call *call, enum rxrpc_timer_trace why)
28 {
29         unsigned long t, now = jiffies;
30
31         read_lock_bh(&call->state_lock);
32
33         if (call->state < RXRPC_CALL_COMPLETE) {
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))
40                         t = call->resend_at;
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)) {
47                         mod_timer(&call->timer, t);
48                         trace_rxrpc_timer(call, why, now);
49                 }
50         }
51
52 out:
53         read_unlock_bh(&call->state_lock);
54 }
55
56 /*
57  * propose an ACK be sent
58  */
59 static void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
60                                 u16 skew, u32 serial, bool immediate,
61                                 bool background,
62                                 enum rxrpc_propose_ack_trace why)
63 {
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];
67
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)) {
76                         outcome = rxrpc_propose_ack_update;
77                         call->ackr_serial = serial;
78                         call->ackr_skew = skew;
79                 }
80                 if (!immediate)
81                         goto trace;
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;
86         } else {
87                 outcome = rxrpc_propose_ack_subsume;
88         }
89
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;
97
98         case RXRPC_ACK_DELAY:
99                 if (rxrpc_soft_ack_delay < expiry)
100                         expiry = rxrpc_soft_ack_delay;
101                 break;
102
103         case RXRPC_ACK_PING:
104         case RXRPC_ACK_IDLE:
105                 if (rxrpc_idle_ack_delay < expiry)
106                         expiry = rxrpc_idle_ack_delay;
107                 break;
108
109         default:
110                 immediate = true;
111                 break;
112         }
113
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;
127                         rxrpc_set_timer(call, rxrpc_timer_set_for_ack);
128                 }
129         }
130
131 trace:
132         trace_rxrpc_propose_ack(call, why, ack_reason, serial, immediate,
133                                 background, outcome);
134 }
135
136 /*
137  * propose an ACK be sent, locking the call structure
138  */
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)
142 {
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);
147 }
148
149 /*
150  * Handle congestion being detected by the retransmit timeout.
151  */
152 static void rxrpc_congestion_timeout(struct rxrpc_call *call)
153 {
154         set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
155 }
156
157 /*
158  * Perform retransmission of NAK'd and unack'd packets.
159  */
160 static void rxrpc_resend(struct rxrpc_call *call)
161 {
162         struct rxrpc_skb_priv *sp;
163         struct sk_buff *skb;
164         rxrpc_seq_t cursor, seq, top;
165         ktime_t now = ktime_get_real(), max_age, oldest, resend_at, ack_ts;
166         int ix;
167         u8 annotation, anno_type, retrans = 0, unacked = 0;
168
169         _enter("{%d,%d}", call->tx_hard_ack, call->tx_top);
170
171         max_age = ktime_sub_ms(now, rxrpc_resend_timeout);
172
173         spin_lock_bh(&call->lock);
174
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          */
185         oldest = now;
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)
192                         continue;
193
194                 skb = call->rxtx_buffer[ix];
195                 rxrpc_see_skb(skb, rxrpc_skb_tx_seen);
196                 sp = rxrpc_skb(skb);
197
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;
202                                 continue;
203                         }
204                         if (!(annotation & RXRPC_TX_ANNO_RESENT))
205                                 unacked++;
206                 }
207
208                 /* Okay, we need to retransmit a packet. */
209                 call->rxtx_annotations[ix] = RXRPC_TX_ANNO_RETRANS | annotation;
210                 retrans++;
211                 trace_rxrpc_retransmit(call, seq, annotation | anno_type,
212                                        ktime_to_ns(ktime_sub(skb->tstamp, max_age)));
213         }
214
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                     */
223
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
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          */
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)
253                         continue;
254
255                 skb = call->rxtx_buffer[ix];
256                 rxrpc_get_skb(skb, rxrpc_skb_tx_got);
257                 spin_unlock_bh(&call->lock);
258
259                 if (rxrpc_send_data_packet(call, skb) < 0) {
260                         rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
261                         return;
262                 }
263
264                 if (rxrpc_is_client_call(call))
265                         rxrpc_expose_client_call(call);
266
267                 rxrpc_free_skb(skb, rxrpc_skb_tx_freed);
268                 spin_lock_bh(&call->lock);
269
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                  */
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                 }
286
287                 if (after(call->tx_hard_ack, seq))
288                         seq = call->tx_hard_ack;
289         }
290
291 out_unlock:
292         spin_unlock_bh(&call->lock);
293 out:
294         _leave("");
295 }
296
297 /*
298  * Handle retransmission and deferred ACK/abort generation.
299  */
300 void rxrpc_process_call(struct work_struct *work)
301 {
302         struct rxrpc_call *call =
303                 container_of(work, struct rxrpc_call, processor);
304         unsigned long now;
305
306         rxrpc_see_call(call);
307
308         //printk("\n--------------------\n");
309         _enter("{%d,%s,%lx}",
310                call->debug_id, rxrpc_call_states[call->state], call->events);
311
312 recheck_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;
316         }
317
318         if (call->state == RXRPC_CALL_COMPLETE) {
319                 del_timer_sync(&call->timer);
320                 goto out_put;
321         }
322
323         now = jiffies;
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);
327                 goto recheck_state;
328         }
329
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;
336                 }
337         }
338
339         if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events) ||
340             time_after_eq(now, call->resend_at)) {
341                 rxrpc_resend(call);
342                 goto recheck_state;
343         }
344
345         rxrpc_set_timer(call, rxrpc_timer_set_for_resend);
346
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);
350                 goto out;
351         }
352
353 out_put:
354         rxrpc_put_call(call, rxrpc_call_put);
355 out:
356         _leave("");
357 }