rxrpc: Move DATA transmission into call processor work item
[platform/kernel/linux-rpi.git] / net / rxrpc / call_event.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/module.h>
11 #include <linux/circ_buf.h>
12 #include <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/slab.h>
15 #include <linux/udp.h>
16 #include <net/sock.h>
17 #include <net/af_rxrpc.h>
18 #include "ar-internal.h"
19
20 /*
21  * Propose a PING ACK be sent.
22  */
23 void rxrpc_propose_ping(struct rxrpc_call *call, u32 serial,
24                         enum rxrpc_propose_ack_trace why)
25 {
26         unsigned long now = jiffies;
27         unsigned long ping_at = now + rxrpc_idle_ack_delay;
28
29         if (time_before(ping_at, call->ping_at)) {
30                 WRITE_ONCE(call->ping_at, ping_at);
31                 rxrpc_reduce_call_timer(call, ping_at, now,
32                                         rxrpc_timer_set_for_ping);
33                 trace_rxrpc_propose_ack(call, why, RXRPC_ACK_PING, serial);
34         }
35 }
36
37 /*
38  * Propose a DELAY ACK be sent in the future.
39  */
40 void rxrpc_propose_delay_ACK(struct rxrpc_call *call, rxrpc_serial_t serial,
41                              enum rxrpc_propose_ack_trace why)
42 {
43         unsigned long expiry = rxrpc_soft_ack_delay;
44         unsigned long now = jiffies, ack_at;
45
46         call->ackr_serial = serial;
47
48         if (rxrpc_soft_ack_delay < expiry)
49                 expiry = rxrpc_soft_ack_delay;
50         if (call->peer->srtt_us != 0)
51                 ack_at = usecs_to_jiffies(call->peer->srtt_us >> 3);
52         else
53                 ack_at = expiry;
54
55         ack_at += READ_ONCE(call->tx_backoff);
56         ack_at += now;
57         if (time_before(ack_at, call->delay_ack_at)) {
58                 WRITE_ONCE(call->delay_ack_at, ack_at);
59                 rxrpc_reduce_call_timer(call, ack_at, now,
60                                         rxrpc_timer_set_for_ack);
61         }
62
63         trace_rxrpc_propose_ack(call, why, RXRPC_ACK_DELAY, serial);
64 }
65
66 /*
67  * Queue an ACK for immediate transmission.
68  */
69 void rxrpc_send_ACK(struct rxrpc_call *call, u8 ack_reason,
70                     rxrpc_serial_t serial, enum rxrpc_propose_ack_trace why)
71 {
72         struct rxrpc_local *local = call->conn->local;
73         struct rxrpc_txbuf *txb;
74
75         if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
76                 return;
77         if (ack_reason == RXRPC_ACK_DELAY &&
78             test_and_set_bit(RXRPC_CALL_DELAY_ACK_PENDING, &call->flags)) {
79                 trace_rxrpc_drop_ack(call, why, ack_reason, serial, false);
80                 return;
81         }
82
83         rxrpc_inc_stat(call->rxnet, stat_tx_acks[ack_reason]);
84
85         txb = rxrpc_alloc_txbuf(call, RXRPC_PACKET_TYPE_ACK,
86                                 rcu_read_lock_held() ? GFP_ATOMIC | __GFP_NOWARN : GFP_NOFS);
87         if (!txb) {
88                 kleave(" = -ENOMEM");
89                 return;
90         }
91
92         txb->ack_why            = why;
93         txb->wire.seq           = 0;
94         txb->wire.type          = RXRPC_PACKET_TYPE_ACK;
95         txb->wire.flags         |= RXRPC_SLOW_START_OK;
96         txb->ack.bufferSpace    = 0;
97         txb->ack.maxSkew        = 0;
98         txb->ack.firstPacket    = 0;
99         txb->ack.previousPacket = 0;
100         txb->ack.serial         = htonl(serial);
101         txb->ack.reason         = ack_reason;
102         txb->ack.nAcks          = 0;
103
104         if (!rxrpc_try_get_call(call, rxrpc_call_get_send_ack)) {
105                 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_nomem);
106                 return;
107         }
108
109         spin_lock_bh(&local->ack_tx_lock);
110         list_add_tail(&txb->tx_link, &local->ack_tx_queue);
111         spin_unlock_bh(&local->ack_tx_lock);
112         trace_rxrpc_send_ack(call, why, ack_reason, serial);
113
114         if (!rcu_read_lock_held()) {
115                 rxrpc_transmit_ack_packets(call->peer->local);
116         } else {
117                 rxrpc_get_local(local, rxrpc_local_get_queue);
118                 rxrpc_queue_local(local);
119         }
120 }
121
122 /*
123  * Handle congestion being detected by the retransmit timeout.
124  */
125 static void rxrpc_congestion_timeout(struct rxrpc_call *call)
126 {
127         set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
128 }
129
130 /*
131  * Perform retransmission of NAK'd and unack'd packets.
132  */
133 static void rxrpc_resend(struct rxrpc_call *call, unsigned long now_j)
134 {
135         struct rxrpc_ackpacket *ack = NULL;
136         struct rxrpc_txbuf *txb;
137         struct sk_buff *ack_skb = NULL;
138         unsigned long resend_at;
139         rxrpc_seq_t transmitted = READ_ONCE(call->tx_transmitted);
140         ktime_t now, max_age, oldest, ack_ts;
141         bool unacked = false;
142         unsigned int i;
143         LIST_HEAD(retrans_queue);
144
145         _enter("{%d,%d}", call->acks_hard_ack, call->tx_top);
146
147         now = ktime_get_real();
148         max_age = ktime_sub_us(now, jiffies_to_usecs(call->peer->rto_j));
149         oldest = now;
150
151         /* See if there's an ACK saved with a soft-ACK table in it. */
152         if (call->acks_soft_tbl) {
153                 spin_lock_bh(&call->acks_ack_lock);
154                 ack_skb = call->acks_soft_tbl;
155                 if (ack_skb) {
156                         rxrpc_get_skb(ack_skb, rxrpc_skb_get_ack);
157                         ack = (void *)ack_skb->data + sizeof(struct rxrpc_wire_header);
158                 }
159                 spin_unlock_bh(&call->acks_ack_lock);
160         }
161
162         if (list_empty(&call->tx_buffer))
163                 goto no_resend;
164
165         spin_lock(&call->tx_lock);
166
167         if (list_empty(&call->tx_buffer))
168                 goto no_further_resend;
169
170         trace_rxrpc_resend(call);
171         txb = list_first_entry(&call->tx_buffer, struct rxrpc_txbuf, call_link);
172
173         /* Scan the soft ACK table without dropping the lock and resend any
174          * explicitly NAK'd packets.
175          */
176         if (ack) {
177                 for (i = 0; i < ack->nAcks; i++) {
178                         rxrpc_seq_t seq;
179
180                         if (ack->acks[i] & 1)
181                                 continue;
182                         seq = ntohl(ack->firstPacket) + i;
183                         if (after(txb->seq, transmitted))
184                                 break;
185                         if (after(txb->seq, seq))
186                                 continue; /* A new hard ACK probably came in */
187                         list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
188                                 if (txb->seq == seq)
189                                         goto found_txb;
190                         }
191                         goto no_further_resend;
192
193                 found_txb:
194                         if (after(ntohl(txb->wire.serial), call->acks_highest_serial))
195                                 continue; /* Ack point not yet reached */
196
197                         rxrpc_see_txbuf(txb, rxrpc_txbuf_see_unacked);
198
199                         if (list_empty(&txb->tx_link)) {
200                                 rxrpc_get_txbuf(txb, rxrpc_txbuf_get_retrans);
201                                 list_add_tail(&txb->tx_link, &retrans_queue);
202                                 set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
203                         }
204
205                         trace_rxrpc_retransmit(call, txb->seq,
206                                                ktime_to_ns(ktime_sub(txb->last_sent,
207                                                                      max_age)));
208
209                         if (list_is_last(&txb->call_link, &call->tx_buffer))
210                                 goto no_further_resend;
211                         txb = list_next_entry(txb, call_link);
212                 }
213         }
214
215         /* Fast-forward through the Tx queue to the point the peer says it has
216          * seen.  Anything between the soft-ACK table and that point will get
217          * ACK'd or NACK'd in due course, so don't worry about it here; here we
218          * need to consider retransmitting anything beyond that point.
219          *
220          * Note that ACK for a packet can beat the update of tx_transmitted.
221          */
222         if (after_eq(READ_ONCE(call->acks_prev_seq), READ_ONCE(call->tx_transmitted)))
223                 goto no_further_resend;
224
225         list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
226                 if (before_eq(txb->seq, READ_ONCE(call->acks_prev_seq)))
227                         continue;
228                 if (after(txb->seq, READ_ONCE(call->tx_transmitted)))
229                         break; /* Not transmitted yet */
230
231                 if (ack && ack->reason == RXRPC_ACK_PING_RESPONSE &&
232                     before(ntohl(txb->wire.serial), ntohl(ack->serial)))
233                         goto do_resend; /* Wasn't accounted for by a more recent ping. */
234
235                 if (ktime_after(txb->last_sent, max_age)) {
236                         if (ktime_before(txb->last_sent, oldest))
237                                 oldest = txb->last_sent;
238                         continue;
239                 }
240
241         do_resend:
242                 unacked = true;
243                 if (list_empty(&txb->tx_link)) {
244                         rxrpc_get_txbuf(txb, rxrpc_txbuf_get_retrans);
245                         list_add_tail(&txb->tx_link, &retrans_queue);
246                         set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
247                         rxrpc_inc_stat(call->rxnet, stat_tx_data_retrans);
248                 }
249         }
250
251 no_further_resend:
252         spin_unlock(&call->tx_lock);
253 no_resend:
254         rxrpc_free_skb(ack_skb, rxrpc_skb_put_ack);
255
256         resend_at = nsecs_to_jiffies(ktime_to_ns(ktime_sub(now, oldest)));
257         resend_at += jiffies + rxrpc_get_rto_backoff(call->peer,
258                                                      !list_empty(&retrans_queue));
259         WRITE_ONCE(call->resend_at, resend_at);
260
261         if (unacked)
262                 rxrpc_congestion_timeout(call);
263
264         /* If there was nothing that needed retransmission then it's likely
265          * that an ACK got lost somewhere.  Send a ping to find out instead of
266          * retransmitting data.
267          */
268         if (list_empty(&retrans_queue)) {
269                 rxrpc_reduce_call_timer(call, resend_at, now_j,
270                                         rxrpc_timer_set_for_resend);
271                 ack_ts = ktime_sub(now, call->acks_latest_ts);
272                 if (ktime_to_us(ack_ts) < (call->peer->srtt_us >> 3))
273                         goto out;
274                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
275                                rxrpc_propose_ack_ping_for_lost_ack);
276                 goto out;
277         }
278
279         while ((txb = list_first_entry_or_null(&retrans_queue,
280                                                struct rxrpc_txbuf, tx_link))) {
281                 list_del_init(&txb->tx_link);
282                 rxrpc_send_data_packet(call, txb);
283                 rxrpc_put_txbuf(txb, rxrpc_txbuf_put_trans);
284
285                 trace_rxrpc_retransmit(call, txb->seq,
286                                        ktime_to_ns(ktime_sub(txb->last_sent,
287                                                              max_age)));
288         }
289
290 out:
291         _leave("");
292 }
293
294 static bool rxrpc_tx_window_has_space(struct rxrpc_call *call)
295 {
296         unsigned int winsize = min_t(unsigned int, call->tx_winsize,
297                                      call->cong_cwnd + call->cong_extra);
298         rxrpc_seq_t window = call->acks_hard_ack, wtop = window + winsize;
299         rxrpc_seq_t tx_top = call->tx_top;
300         int space;
301
302         space = wtop - tx_top;
303         return space > 0;
304 }
305
306 /*
307  * Decant some if the sendmsg prepared queue into the transmission buffer.
308  */
309 static void rxrpc_decant_prepared_tx(struct rxrpc_call *call)
310 {
311         struct rxrpc_txbuf *txb;
312
313         if (rxrpc_is_client_call(call) &&
314             !test_bit(RXRPC_CALL_EXPOSED, &call->flags))
315                 rxrpc_expose_client_call(call);
316
317         while ((txb = list_first_entry_or_null(&call->tx_sendmsg,
318                                                struct rxrpc_txbuf, call_link))) {
319                 spin_lock(&call->tx_lock);
320                 list_del(&txb->call_link);
321                 spin_unlock(&call->tx_lock);
322
323                 call->tx_top = txb->seq;
324                 list_add_tail(&txb->call_link, &call->tx_buffer);
325
326                 rxrpc_transmit_one(call, txb);
327
328                 // TODO: Drain the transmission buffers.  Do this somewhere better
329                 if (after(call->acks_hard_ack, call->tx_bottom + 16))
330                         rxrpc_shrink_call_tx_buffer(call);
331
332                 if (!rxrpc_tx_window_has_space(call))
333                         break;
334         }
335 }
336
337 static void rxrpc_transmit_some_data(struct rxrpc_call *call)
338 {
339         switch (call->state) {
340         case RXRPC_CALL_SERVER_ACK_REQUEST:
341                 if (list_empty(&call->tx_sendmsg))
342                         return;
343                 fallthrough;
344
345         case RXRPC_CALL_SERVER_SEND_REPLY:
346         case RXRPC_CALL_SERVER_AWAIT_ACK:
347         case RXRPC_CALL_CLIENT_SEND_REQUEST:
348         case RXRPC_CALL_CLIENT_AWAIT_REPLY:
349                 if (!rxrpc_tx_window_has_space(call))
350                         return;
351                 if (list_empty(&call->tx_sendmsg))
352                         return;
353                 rxrpc_decant_prepared_tx(call);
354                 break;
355         default:
356                 return;
357         }
358 }
359
360 /*
361  * Handle retransmission and deferred ACK/abort generation.
362  */
363 void rxrpc_process_call(struct work_struct *work)
364 {
365         struct rxrpc_call *call =
366                 container_of(work, struct rxrpc_call, processor);
367         unsigned long now, next, t;
368         unsigned int iterations = 0;
369         rxrpc_serial_t ackr_serial;
370
371         rxrpc_see_call(call, rxrpc_call_see_input);
372
373         //printk("\n--------------------\n");
374         _enter("{%d,%s,%lx}",
375                call->debug_id, rxrpc_call_states[call->state], call->events);
376
377 recheck_state:
378         if (call->acks_hard_ack != call->tx_bottom)
379                 rxrpc_shrink_call_tx_buffer(call);
380
381         /* Limit the number of times we do this before returning to the manager */
382         if (!rxrpc_tx_window_has_space(call) ||
383             list_empty(&call->tx_sendmsg)) {
384                 iterations++;
385                 if (iterations > 5)
386                         goto requeue;
387         }
388
389         if (test_and_clear_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
390                 rxrpc_send_abort_packet(call);
391                 goto recheck_state;
392         }
393
394         if (call->state == RXRPC_CALL_COMPLETE) {
395                 del_timer_sync(&call->timer);
396                 goto out;
397         }
398
399         /* Work out if any timeouts tripped */
400         now = jiffies;
401         t = READ_ONCE(call->expect_rx_by);
402         if (time_after_eq(now, t)) {
403                 trace_rxrpc_timer(call, rxrpc_timer_exp_normal, now);
404                 set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
405         }
406
407         t = READ_ONCE(call->expect_req_by);
408         if (call->state == RXRPC_CALL_SERVER_RECV_REQUEST &&
409             time_after_eq(now, t)) {
410                 trace_rxrpc_timer(call, rxrpc_timer_exp_idle, now);
411                 set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
412         }
413
414         t = READ_ONCE(call->expect_term_by);
415         if (time_after_eq(now, t)) {
416                 trace_rxrpc_timer(call, rxrpc_timer_exp_hard, now);
417                 set_bit(RXRPC_CALL_EV_EXPIRED, &call->events);
418         }
419
420         t = READ_ONCE(call->delay_ack_at);
421         if (time_after_eq(now, t)) {
422                 trace_rxrpc_timer(call, rxrpc_timer_exp_ack, now);
423                 cmpxchg(&call->delay_ack_at, t, now + MAX_JIFFY_OFFSET);
424                 ackr_serial = xchg(&call->ackr_serial, 0);
425                 rxrpc_send_ACK(call, RXRPC_ACK_DELAY, ackr_serial,
426                                rxrpc_propose_ack_ping_for_lost_ack);
427         }
428
429         t = READ_ONCE(call->ack_lost_at);
430         if (time_after_eq(now, t)) {
431                 trace_rxrpc_timer(call, rxrpc_timer_exp_lost_ack, now);
432                 cmpxchg(&call->ack_lost_at, t, now + MAX_JIFFY_OFFSET);
433                 set_bit(RXRPC_CALL_EV_ACK_LOST, &call->events);
434         }
435
436         t = READ_ONCE(call->keepalive_at);
437         if (time_after_eq(now, t)) {
438                 trace_rxrpc_timer(call, rxrpc_timer_exp_keepalive, now);
439                 cmpxchg(&call->keepalive_at, t, now + MAX_JIFFY_OFFSET);
440                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
441                                rxrpc_propose_ack_ping_for_keepalive);
442         }
443
444         t = READ_ONCE(call->ping_at);
445         if (time_after_eq(now, t)) {
446                 trace_rxrpc_timer(call, rxrpc_timer_exp_ping, now);
447                 cmpxchg(&call->ping_at, t, now + MAX_JIFFY_OFFSET);
448                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
449                                rxrpc_propose_ack_ping_for_keepalive);
450         }
451
452         t = READ_ONCE(call->resend_at);
453         if (time_after_eq(now, t)) {
454                 trace_rxrpc_timer(call, rxrpc_timer_exp_resend, now);
455                 cmpxchg(&call->resend_at, t, now + MAX_JIFFY_OFFSET);
456                 set_bit(RXRPC_CALL_EV_RESEND, &call->events);
457         }
458
459         rxrpc_transmit_some_data(call);
460
461         /* Process events */
462         if (test_and_clear_bit(RXRPC_CALL_EV_EXPIRED, &call->events)) {
463                 if (test_bit(RXRPC_CALL_RX_HEARD, &call->flags) &&
464                     (int)call->conn->hi_serial - (int)call->rx_serial > 0) {
465                         trace_rxrpc_call_reset(call);
466                         rxrpc_abort_call("EXP", call, 0, RX_CALL_DEAD, -ECONNRESET);
467                 } else {
468                         rxrpc_abort_call("EXP", call, 0, RX_CALL_TIMEOUT, -ETIME);
469                 }
470                 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
471                 goto recheck_state;
472         }
473
474         if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events)) {
475                 call->acks_lost_top = call->tx_top;
476                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
477                                rxrpc_propose_ack_ping_for_lost_ack);
478         }
479
480         if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events) &&
481             call->state != RXRPC_CALL_CLIENT_RECV_REPLY) {
482                 rxrpc_resend(call, now);
483                 goto recheck_state;
484         }
485
486         /* Make sure the timer is restarted */
487         next = call->expect_rx_by;
488
489 #define set(T) { t = READ_ONCE(T); if (time_before(t, next)) next = t; }
490
491         set(call->expect_req_by);
492         set(call->expect_term_by);
493         set(call->delay_ack_at);
494         set(call->ack_lost_at);
495         set(call->resend_at);
496         set(call->keepalive_at);
497         set(call->ping_at);
498
499         now = jiffies;
500         if (time_after_eq(now, next))
501                 goto recheck_state;
502
503         rxrpc_reduce_call_timer(call, next, now, rxrpc_timer_restart);
504
505         /* other events may have been raised since we started checking */
506         if (call->events)
507                 goto requeue;
508
509 out:
510         _leave("");
511         return;
512
513 requeue:
514         if (call->state < RXRPC_CALL_COMPLETE)
515                 rxrpc_queue_call(call, rxrpc_call_queue_requeue);
516         goto out;
517 }