rxrpc: Fix delayed ACKs to not set the reference serial number
[platform/kernel/linux-starfive.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         if (rxrpc_soft_ack_delay < expiry)
47                 expiry = rxrpc_soft_ack_delay;
48         if (call->peer->srtt_us != 0)
49                 ack_at = usecs_to_jiffies(call->peer->srtt_us >> 3);
50         else
51                 ack_at = expiry;
52
53         ack_at += READ_ONCE(call->tx_backoff);
54         ack_at += now;
55         if (time_before(ack_at, call->delay_ack_at)) {
56                 WRITE_ONCE(call->delay_ack_at, ack_at);
57                 rxrpc_reduce_call_timer(call, ack_at, now,
58                                         rxrpc_timer_set_for_ack);
59         }
60
61         trace_rxrpc_propose_ack(call, why, RXRPC_ACK_DELAY, serial);
62 }
63
64 /*
65  * Queue an ACK for immediate transmission.
66  */
67 void rxrpc_send_ACK(struct rxrpc_call *call, u8 ack_reason,
68                     rxrpc_serial_t serial, enum rxrpc_propose_ack_trace why)
69 {
70         struct rxrpc_txbuf *txb;
71
72         if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
73                 return;
74
75         rxrpc_inc_stat(call->rxnet, stat_tx_acks[ack_reason]);
76
77         txb = rxrpc_alloc_txbuf(call, RXRPC_PACKET_TYPE_ACK,
78                                 rcu_read_lock_held() ? GFP_ATOMIC | __GFP_NOWARN : GFP_NOFS);
79         if (!txb) {
80                 kleave(" = -ENOMEM");
81                 return;
82         }
83
84         txb->ack_why            = why;
85         txb->wire.seq           = 0;
86         txb->wire.type          = RXRPC_PACKET_TYPE_ACK;
87         txb->wire.flags         |= RXRPC_SLOW_START_OK;
88         txb->ack.bufferSpace    = 0;
89         txb->ack.maxSkew        = 0;
90         txb->ack.firstPacket    = 0;
91         txb->ack.previousPacket = 0;
92         txb->ack.serial         = htonl(serial);
93         txb->ack.reason         = ack_reason;
94         txb->ack.nAcks          = 0;
95
96         trace_rxrpc_send_ack(call, why, ack_reason, serial);
97         rxrpc_send_ack_packet(call, txb);
98         rxrpc_put_txbuf(txb, rxrpc_txbuf_put_ack_tx);
99 }
100
101 /*
102  * Handle congestion being detected by the retransmit timeout.
103  */
104 static void rxrpc_congestion_timeout(struct rxrpc_call *call)
105 {
106         set_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags);
107 }
108
109 /*
110  * Perform retransmission of NAK'd and unack'd packets.
111  */
112 void rxrpc_resend(struct rxrpc_call *call, struct sk_buff *ack_skb)
113 {
114         struct rxrpc_ackpacket *ack = NULL;
115         struct rxrpc_txbuf *txb;
116         unsigned long resend_at;
117         rxrpc_seq_t transmitted = READ_ONCE(call->tx_transmitted);
118         ktime_t now, max_age, oldest, ack_ts;
119         bool unacked = false;
120         unsigned int i;
121         LIST_HEAD(retrans_queue);
122
123         _enter("{%d,%d}", call->acks_hard_ack, call->tx_top);
124
125         now = ktime_get_real();
126         max_age = ktime_sub_us(now, jiffies_to_usecs(call->peer->rto_j));
127         oldest = now;
128
129         if (list_empty(&call->tx_buffer))
130                 goto no_resend;
131
132         if (list_empty(&call->tx_buffer))
133                 goto no_further_resend;
134
135         trace_rxrpc_resend(call, ack_skb);
136         txb = list_first_entry(&call->tx_buffer, struct rxrpc_txbuf, call_link);
137
138         /* Scan the soft ACK table without dropping the lock and resend any
139          * explicitly NAK'd packets.
140          */
141         if (ack_skb) {
142                 ack = (void *)ack_skb->data + sizeof(struct rxrpc_wire_header);
143
144                 for (i = 0; i < ack->nAcks; i++) {
145                         rxrpc_seq_t seq;
146
147                         if (ack->acks[i] & 1)
148                                 continue;
149                         seq = ntohl(ack->firstPacket) + i;
150                         if (after(txb->seq, transmitted))
151                                 break;
152                         if (after(txb->seq, seq))
153                                 continue; /* A new hard ACK probably came in */
154                         list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
155                                 if (txb->seq == seq)
156                                         goto found_txb;
157                         }
158                         goto no_further_resend;
159
160                 found_txb:
161                         if (after(ntohl(txb->wire.serial), call->acks_highest_serial))
162                                 continue; /* Ack point not yet reached */
163
164                         rxrpc_see_txbuf(txb, rxrpc_txbuf_see_unacked);
165
166                         if (list_empty(&txb->tx_link)) {
167                                 list_add_tail(&txb->tx_link, &retrans_queue);
168                                 set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
169                         }
170
171                         trace_rxrpc_retransmit(call, txb->seq,
172                                                ktime_to_ns(ktime_sub(txb->last_sent,
173                                                                      max_age)));
174
175                         if (list_is_last(&txb->call_link, &call->tx_buffer))
176                                 goto no_further_resend;
177                         txb = list_next_entry(txb, call_link);
178                 }
179         }
180
181         /* Fast-forward through the Tx queue to the point the peer says it has
182          * seen.  Anything between the soft-ACK table and that point will get
183          * ACK'd or NACK'd in due course, so don't worry about it here; here we
184          * need to consider retransmitting anything beyond that point.
185          *
186          * Note that ACK for a packet can beat the update of tx_transmitted.
187          */
188         if (after_eq(READ_ONCE(call->acks_prev_seq), READ_ONCE(call->tx_transmitted)))
189                 goto no_further_resend;
190
191         list_for_each_entry_from(txb, &call->tx_buffer, call_link) {
192                 if (before_eq(txb->seq, READ_ONCE(call->acks_prev_seq)))
193                         continue;
194                 if (after(txb->seq, READ_ONCE(call->tx_transmitted)))
195                         break; /* Not transmitted yet */
196
197                 if (ack && ack->reason == RXRPC_ACK_PING_RESPONSE &&
198                     before(ntohl(txb->wire.serial), ntohl(ack->serial)))
199                         goto do_resend; /* Wasn't accounted for by a more recent ping. */
200
201                 if (ktime_after(txb->last_sent, max_age)) {
202                         if (ktime_before(txb->last_sent, oldest))
203                                 oldest = txb->last_sent;
204                         continue;
205                 }
206
207         do_resend:
208                 unacked = true;
209                 if (list_empty(&txb->tx_link)) {
210                         list_add_tail(&txb->tx_link, &retrans_queue);
211                         set_bit(RXRPC_TXBUF_RESENT, &txb->flags);
212                         rxrpc_inc_stat(call->rxnet, stat_tx_data_retrans);
213                 }
214         }
215
216 no_further_resend:
217 no_resend:
218         resend_at = nsecs_to_jiffies(ktime_to_ns(ktime_sub(now, oldest)));
219         resend_at += jiffies + rxrpc_get_rto_backoff(call->peer,
220                                                      !list_empty(&retrans_queue));
221         WRITE_ONCE(call->resend_at, resend_at);
222
223         if (unacked)
224                 rxrpc_congestion_timeout(call);
225
226         /* If there was nothing that needed retransmission then it's likely
227          * that an ACK got lost somewhere.  Send a ping to find out instead of
228          * retransmitting data.
229          */
230         if (list_empty(&retrans_queue)) {
231                 rxrpc_reduce_call_timer(call, resend_at, jiffies,
232                                         rxrpc_timer_set_for_resend);
233                 ack_ts = ktime_sub(now, call->acks_latest_ts);
234                 if (ktime_to_us(ack_ts) < (call->peer->srtt_us >> 3))
235                         goto out;
236                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
237                                rxrpc_propose_ack_ping_for_lost_ack);
238                 goto out;
239         }
240
241         /* Retransmit the queue */
242         while ((txb = list_first_entry_or_null(&retrans_queue,
243                                                struct rxrpc_txbuf, tx_link))) {
244                 list_del_init(&txb->tx_link);
245                 rxrpc_transmit_one(call, txb);
246         }
247
248 out:
249         _leave("");
250 }
251
252 /*
253  * Start transmitting the reply to a service.  This cancels the need to ACK the
254  * request if we haven't yet done so.
255  */
256 static void rxrpc_begin_service_reply(struct rxrpc_call *call)
257 {
258         unsigned long now = jiffies;
259
260         rxrpc_set_call_state(call, RXRPC_CALL_SERVER_SEND_REPLY);
261         WRITE_ONCE(call->delay_ack_at, now + MAX_JIFFY_OFFSET);
262         if (call->ackr_reason == RXRPC_ACK_DELAY)
263                 call->ackr_reason = 0;
264         trace_rxrpc_timer(call, rxrpc_timer_init_for_send_reply, now);
265 }
266
267 /*
268  * Close the transmission phase.  After this point there is no more data to be
269  * transmitted in the call.
270  */
271 static void rxrpc_close_tx_phase(struct rxrpc_call *call)
272 {
273         _debug("________awaiting reply/ACK__________");
274
275         switch (__rxrpc_call_state(call)) {
276         case RXRPC_CALL_CLIENT_SEND_REQUEST:
277                 rxrpc_set_call_state(call, RXRPC_CALL_CLIENT_AWAIT_REPLY);
278                 break;
279         case RXRPC_CALL_SERVER_SEND_REPLY:
280                 rxrpc_set_call_state(call, RXRPC_CALL_SERVER_AWAIT_ACK);
281                 break;
282         default:
283                 break;
284         }
285 }
286
287 static bool rxrpc_tx_window_has_space(struct rxrpc_call *call)
288 {
289         unsigned int winsize = min_t(unsigned int, call->tx_winsize,
290                                      call->cong_cwnd + call->cong_extra);
291         rxrpc_seq_t window = call->acks_hard_ack, wtop = window + winsize;
292         rxrpc_seq_t tx_top = call->tx_top;
293         int space;
294
295         space = wtop - tx_top;
296         return space > 0;
297 }
298
299 /*
300  * Decant some if the sendmsg prepared queue into the transmission buffer.
301  */
302 static void rxrpc_decant_prepared_tx(struct rxrpc_call *call)
303 {
304         struct rxrpc_txbuf *txb;
305
306         if (!test_bit(RXRPC_CALL_EXPOSED, &call->flags)) {
307                 if (list_empty(&call->tx_sendmsg))
308                         return;
309                 rxrpc_expose_client_call(call);
310         }
311
312         while ((txb = list_first_entry_or_null(&call->tx_sendmsg,
313                                                struct rxrpc_txbuf, call_link))) {
314                 spin_lock(&call->tx_lock);
315                 list_del(&txb->call_link);
316                 spin_unlock(&call->tx_lock);
317
318                 call->tx_top = txb->seq;
319                 list_add_tail(&txb->call_link, &call->tx_buffer);
320
321                 if (txb->wire.flags & RXRPC_LAST_PACKET)
322                         rxrpc_close_tx_phase(call);
323
324                 rxrpc_transmit_one(call, txb);
325
326                 if (!rxrpc_tx_window_has_space(call))
327                         break;
328         }
329 }
330
331 static void rxrpc_transmit_some_data(struct rxrpc_call *call)
332 {
333         switch (__rxrpc_call_state(call)) {
334         case RXRPC_CALL_SERVER_ACK_REQUEST:
335                 if (list_empty(&call->tx_sendmsg))
336                         return;
337                 rxrpc_begin_service_reply(call);
338                 fallthrough;
339
340         case RXRPC_CALL_SERVER_SEND_REPLY:
341         case RXRPC_CALL_CLIENT_SEND_REQUEST:
342                 if (!rxrpc_tx_window_has_space(call))
343                         return;
344                 if (list_empty(&call->tx_sendmsg)) {
345                         rxrpc_inc_stat(call->rxnet, stat_tx_data_underflow);
346                         return;
347                 }
348                 rxrpc_decant_prepared_tx(call);
349                 break;
350         default:
351                 return;
352         }
353 }
354
355 /*
356  * Ping the other end to fill our RTT cache and to retrieve the rwind
357  * and MTU parameters.
358  */
359 static void rxrpc_send_initial_ping(struct rxrpc_call *call)
360 {
361         if (call->peer->rtt_count < 3 ||
362             ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
363                          ktime_get_real()))
364                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
365                                rxrpc_propose_ack_ping_for_params);
366 }
367
368 /*
369  * Handle retransmission and deferred ACK/abort generation.
370  */
371 bool rxrpc_input_call_event(struct rxrpc_call *call, struct sk_buff *skb)
372 {
373         unsigned long now, next, t;
374         bool resend = false, expired = false;
375         s32 abort_code;
376
377         rxrpc_see_call(call, rxrpc_call_see_input);
378
379         //printk("\n--------------------\n");
380         _enter("{%d,%s,%lx}",
381                call->debug_id, rxrpc_call_states[__rxrpc_call_state(call)],
382                call->events);
383
384         if (__rxrpc_call_is_complete(call))
385                 goto out;
386
387         /* Handle abort request locklessly, vs rxrpc_propose_abort(). */
388         abort_code = smp_load_acquire(&call->send_abort);
389         if (abort_code) {
390                 rxrpc_abort_call(call, 0, call->send_abort, call->send_abort_err,
391                                  call->send_abort_why);
392                 goto out;
393         }
394
395         if (skb && skb->mark == RXRPC_SKB_MARK_ERROR)
396                 goto out;
397
398         /* If we see our async-event poke, check for timeout trippage. */
399         now = jiffies;
400         t = READ_ONCE(call->expect_rx_by);
401         if (time_after_eq(now, t)) {
402                 trace_rxrpc_timer(call, rxrpc_timer_exp_normal, now);
403                 expired = true;
404         }
405
406         t = READ_ONCE(call->expect_req_by);
407         if (__rxrpc_call_state(call) == RXRPC_CALL_SERVER_RECV_REQUEST &&
408             time_after_eq(now, t)) {
409                 trace_rxrpc_timer(call, rxrpc_timer_exp_idle, now);
410                 expired = true;
411         }
412
413         t = READ_ONCE(call->expect_term_by);
414         if (time_after_eq(now, t)) {
415                 trace_rxrpc_timer(call, rxrpc_timer_exp_hard, now);
416                 expired = true;
417         }
418
419         t = READ_ONCE(call->delay_ack_at);
420         if (time_after_eq(now, t)) {
421                 trace_rxrpc_timer(call, rxrpc_timer_exp_ack, now);
422                 cmpxchg(&call->delay_ack_at, t, now + MAX_JIFFY_OFFSET);
423                 rxrpc_send_ACK(call, RXRPC_ACK_DELAY, 0,
424                                rxrpc_propose_ack_ping_for_lost_ack);
425         }
426
427         t = READ_ONCE(call->ack_lost_at);
428         if (time_after_eq(now, t)) {
429                 trace_rxrpc_timer(call, rxrpc_timer_exp_lost_ack, now);
430                 cmpxchg(&call->ack_lost_at, t, now + MAX_JIFFY_OFFSET);
431                 set_bit(RXRPC_CALL_EV_ACK_LOST, &call->events);
432         }
433
434         t = READ_ONCE(call->keepalive_at);
435         if (time_after_eq(now, t)) {
436                 trace_rxrpc_timer(call, rxrpc_timer_exp_keepalive, now);
437                 cmpxchg(&call->keepalive_at, t, now + MAX_JIFFY_OFFSET);
438                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
439                                rxrpc_propose_ack_ping_for_keepalive);
440         }
441
442         t = READ_ONCE(call->ping_at);
443         if (time_after_eq(now, t)) {
444                 trace_rxrpc_timer(call, rxrpc_timer_exp_ping, now);
445                 cmpxchg(&call->ping_at, t, now + MAX_JIFFY_OFFSET);
446                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
447                                rxrpc_propose_ack_ping_for_keepalive);
448         }
449
450         t = READ_ONCE(call->resend_at);
451         if (time_after_eq(now, t)) {
452                 trace_rxrpc_timer(call, rxrpc_timer_exp_resend, now);
453                 cmpxchg(&call->resend_at, t, now + MAX_JIFFY_OFFSET);
454                 resend = true;
455         }
456
457         if (skb)
458                 rxrpc_input_call_packet(call, skb);
459
460         rxrpc_transmit_some_data(call);
461
462         if (skb) {
463                 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
464
465                 if (sp->hdr.type == RXRPC_PACKET_TYPE_ACK)
466                         rxrpc_congestion_degrade(call);
467         }
468
469         if (test_and_clear_bit(RXRPC_CALL_EV_INITIAL_PING, &call->events))
470                 rxrpc_send_initial_ping(call);
471
472         /* Process events */
473         if (expired) {
474                 if (test_bit(RXRPC_CALL_RX_HEARD, &call->flags) &&
475                     (int)call->conn->hi_serial - (int)call->rx_serial > 0) {
476                         trace_rxrpc_call_reset(call);
477                         rxrpc_abort_call(call, 0, RX_CALL_DEAD, -ECONNRESET,
478                                          rxrpc_abort_call_reset);
479                 } else {
480                         rxrpc_abort_call(call, 0, RX_CALL_TIMEOUT, -ETIME,
481                                          rxrpc_abort_call_timeout);
482                 }
483                 goto out;
484         }
485
486         if (test_and_clear_bit(RXRPC_CALL_EV_ACK_LOST, &call->events))
487                 rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
488                                rxrpc_propose_ack_ping_for_lost_ack);
489
490         if (resend && __rxrpc_call_state(call) != RXRPC_CALL_CLIENT_RECV_REPLY)
491                 rxrpc_resend(call, NULL);
492
493         if (test_and_clear_bit(RXRPC_CALL_RX_IS_IDLE, &call->flags))
494                 rxrpc_send_ACK(call, RXRPC_ACK_IDLE, 0,
495                                rxrpc_propose_ack_rx_idle);
496
497         if (call->ackr_nr_unacked > 2) {
498                 if (call->peer->rtt_count < 3)
499                         rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
500                                        rxrpc_propose_ack_ping_for_rtt);
501                 else if (ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000),
502                                       ktime_get_real()))
503                         rxrpc_send_ACK(call, RXRPC_ACK_PING, 0,
504                                        rxrpc_propose_ack_ping_for_old_rtt);
505                 else
506                         rxrpc_send_ACK(call, RXRPC_ACK_IDLE, 0,
507                                        rxrpc_propose_ack_input_data);
508         }
509
510         /* Make sure the timer is restarted */
511         if (!__rxrpc_call_is_complete(call)) {
512                 next = call->expect_rx_by;
513
514 #define set(T) { t = READ_ONCE(T); if (time_before(t, next)) next = t; }
515
516                 set(call->expect_req_by);
517                 set(call->expect_term_by);
518                 set(call->delay_ack_at);
519                 set(call->ack_lost_at);
520                 set(call->resend_at);
521                 set(call->keepalive_at);
522                 set(call->ping_at);
523
524                 now = jiffies;
525                 if (time_after_eq(now, next))
526                         rxrpc_poke_call(call, rxrpc_call_poke_timer_now);
527
528                 rxrpc_reduce_call_timer(call, next, now, rxrpc_timer_restart);
529         }
530
531 out:
532         if (__rxrpc_call_is_complete(call)) {
533                 del_timer_sync(&call->timer);
534                 if (!test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
535                         rxrpc_disconnect_call(call);
536                 if (call->security)
537                         call->security->free_call_crypto(call);
538         }
539         if (call->acks_hard_ack != call->tx_bottom)
540                 rxrpc_shrink_call_tx_buffer(call);
541         _leave("");
542         return true;
543 }