1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Processing of received RxRPC packets
4 * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include "ar-internal.h"
12 static void rxrpc_proto_abort(const char *why,
13 struct rxrpc_call *call, rxrpc_seq_t seq)
15 if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, -EBADMSG)) {
16 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
17 rxrpc_queue_call(call, rxrpc_call_queue_abort);
22 * Do TCP-style congestion management [RFC 5681].
24 static void rxrpc_congestion_management(struct rxrpc_call *call,
26 struct rxrpc_ack_summary *summary,
27 rxrpc_serial_t acked_serial)
29 enum rxrpc_congest_change change = rxrpc_cong_no_change;
30 unsigned int cumulative_acks = call->cong_cumul_acks;
31 unsigned int cwnd = call->cong_cwnd;
34 summary->flight_size =
35 (call->tx_top - call->acks_hard_ack) - summary->nr_acks;
37 if (test_and_clear_bit(RXRPC_CALL_RETRANS_TIMEOUT, &call->flags)) {
38 summary->retrans_timeo = true;
39 call->cong_ssthresh = max_t(unsigned int,
40 summary->flight_size / 2, 2);
42 if (cwnd >= call->cong_ssthresh &&
43 call->cong_mode == RXRPC_CALL_SLOW_START) {
44 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
45 call->cong_tstamp = skb->tstamp;
50 cumulative_acks += summary->nr_new_acks;
51 cumulative_acks += summary->nr_rot_new_acks;
52 if (cumulative_acks > 255)
53 cumulative_acks = 255;
55 summary->mode = call->cong_mode;
56 summary->cwnd = call->cong_cwnd;
57 summary->ssthresh = call->cong_ssthresh;
58 summary->cumulative_acks = cumulative_acks;
59 summary->dup_acks = call->cong_dup_acks;
61 /* If we haven't transmitted anything for >1RTT, we should reset the
62 * congestion management state.
64 if ((call->cong_mode == RXRPC_CALL_SLOW_START ||
65 call->cong_mode == RXRPC_CALL_CONGEST_AVOIDANCE) &&
66 ktime_before(ktime_add_us(call->tx_last_sent,
67 call->peer->srtt_us >> 3),
70 change = rxrpc_cong_idle_reset;
71 summary->mode = RXRPC_CALL_SLOW_START;
72 if (RXRPC_TX_SMSS > 2190)
74 else if (RXRPC_TX_SMSS > 1095)
80 switch (call->cong_mode) {
81 case RXRPC_CALL_SLOW_START:
82 if (summary->saw_nacks)
83 goto packet_loss_detected;
84 if (summary->cumulative_acks > 0)
86 if (cwnd >= call->cong_ssthresh) {
87 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
88 call->cong_tstamp = skb->tstamp;
92 case RXRPC_CALL_CONGEST_AVOIDANCE:
93 if (summary->saw_nacks)
94 goto packet_loss_detected;
96 /* We analyse the number of packets that get ACK'd per RTT
97 * period and increase the window if we managed to fill it.
99 if (call->peer->rtt_count == 0)
101 if (ktime_before(skb->tstamp,
102 ktime_add_us(call->cong_tstamp,
103 call->peer->srtt_us >> 3)))
104 goto out_no_clear_ca;
105 change = rxrpc_cong_rtt_window_end;
106 call->cong_tstamp = skb->tstamp;
107 if (cumulative_acks >= cwnd)
111 case RXRPC_CALL_PACKET_LOSS:
112 if (!summary->saw_nacks)
113 goto resume_normality;
115 if (summary->new_low_nack) {
116 change = rxrpc_cong_new_low_nack;
117 call->cong_dup_acks = 1;
118 if (call->cong_extra > 1)
119 call->cong_extra = 1;
120 goto send_extra_data;
123 call->cong_dup_acks++;
124 if (call->cong_dup_acks < 3)
125 goto send_extra_data;
127 change = rxrpc_cong_begin_retransmission;
128 call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
129 call->cong_ssthresh = max_t(unsigned int,
130 summary->flight_size / 2, 2);
131 cwnd = call->cong_ssthresh + 3;
132 call->cong_extra = 0;
133 call->cong_dup_acks = 0;
137 case RXRPC_CALL_FAST_RETRANSMIT:
138 if (!summary->new_low_nack) {
139 if (summary->nr_new_acks == 0)
141 call->cong_dup_acks++;
142 if (call->cong_dup_acks == 2) {
143 change = rxrpc_cong_retransmit_again;
144 call->cong_dup_acks = 0;
148 change = rxrpc_cong_progress;
149 cwnd = call->cong_ssthresh;
150 if (!summary->saw_nacks)
151 goto resume_normality;
161 change = rxrpc_cong_cleared_nacks;
162 call->cong_dup_acks = 0;
163 call->cong_extra = 0;
164 call->cong_tstamp = skb->tstamp;
165 if (cwnd < call->cong_ssthresh)
166 call->cong_mode = RXRPC_CALL_SLOW_START;
168 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
172 if (cwnd >= RXRPC_TX_MAX_WINDOW)
173 cwnd = RXRPC_TX_MAX_WINDOW;
174 call->cong_cwnd = cwnd;
175 call->cong_cumul_acks = cumulative_acks;
176 trace_rxrpc_congest(call, summary, acked_serial, change);
177 if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
178 rxrpc_queue_call(call, rxrpc_call_queue_resend);
181 packet_loss_detected:
182 change = rxrpc_cong_saw_nack;
183 call->cong_mode = RXRPC_CALL_PACKET_LOSS;
184 call->cong_dup_acks = 0;
185 goto send_extra_data;
188 /* Send some previously unsent DATA if we have some to advance the ACK
191 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
192 summary->nr_acks != call->tx_top - call->acks_hard_ack) {
194 wake_up(&call->waitq);
196 goto out_no_clear_ca;
200 * Apply a hard ACK by advancing the Tx window.
202 static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
203 struct rxrpc_ack_summary *summary)
205 struct rxrpc_txbuf *txb;
206 bool rot_last = false;
208 list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
209 if (before_eq(txb->seq, call->acks_hard_ack))
211 summary->nr_rot_new_acks++;
212 if (test_bit(RXRPC_TXBUF_LAST, &txb->flags)) {
213 set_bit(RXRPC_CALL_TX_LAST, &call->flags);
221 set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
223 _enter("%x,%x,%x,%d", to, call->acks_hard_ack, call->tx_top, rot_last);
225 if (call->acks_lowest_nak == call->acks_hard_ack) {
226 call->acks_lowest_nak = to;
227 } else if (after(to, call->acks_lowest_nak)) {
228 summary->new_low_nack = true;
229 call->acks_lowest_nak = to;
232 smp_store_release(&call->acks_hard_ack, to);
234 trace_rxrpc_txqueue(call, (rot_last ?
235 rxrpc_txqueue_rotate_last :
236 rxrpc_txqueue_rotate));
237 wake_up(&call->waitq);
242 * End the transmission phase of a call.
244 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
245 * or a final ACK packet.
247 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
248 const char *abort_why)
252 ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
254 write_lock(&call->state_lock);
258 case RXRPC_CALL_CLIENT_SEND_REQUEST:
259 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
261 call->state = state = RXRPC_CALL_CLIENT_RECV_REPLY;
263 call->state = state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
266 case RXRPC_CALL_SERVER_AWAIT_ACK:
267 __rxrpc_call_completed(call);
275 write_unlock(&call->state_lock);
276 if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
277 trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
279 trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
284 write_unlock(&call->state_lock);
285 kdebug("end_tx %s", rxrpc_call_states[call->state]);
286 rxrpc_proto_abort(abort_why, call, call->tx_top);
291 * Begin the reply reception phase of a call.
293 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
295 struct rxrpc_ack_summary summary = { 0 };
296 unsigned long now, timo;
297 rxrpc_seq_t top = READ_ONCE(call->tx_top);
299 if (call->ackr_reason) {
301 timo = now + MAX_JIFFY_OFFSET;
302 WRITE_ONCE(call->resend_at, timo);
303 WRITE_ONCE(call->delay_ack_at, timo);
304 trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
307 if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
308 if (!rxrpc_rotate_tx_window(call, top, &summary)) {
309 rxrpc_proto_abort("TXL", call, top);
313 return rxrpc_end_tx_phase(call, true, "ETD");
316 static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
317 rxrpc_seq_t window, rxrpc_seq_t wtop)
319 atomic64_set_release(&call->ackr_window, ((u64)wtop) << 32 | window);
323 * Push a DATA packet onto the Rx queue.
325 static void rxrpc_input_queue_data(struct rxrpc_call *call, struct sk_buff *skb,
326 rxrpc_seq_t window, rxrpc_seq_t wtop,
327 enum rxrpc_receive_trace why)
329 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
330 bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
332 __skb_queue_tail(&call->recvmsg_queue, skb);
333 rxrpc_input_update_ack_window(call, window, wtop);
335 trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
339 * Process a DATA packet.
341 static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb)
343 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
345 rxrpc_serial_t serial = sp->hdr.serial;
346 u64 win = atomic64_read(&call->ackr_window);
347 rxrpc_seq_t window = lower_32_bits(win);
348 rxrpc_seq_t wtop = upper_32_bits(win);
349 rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
350 rxrpc_seq_t seq = sp->hdr.seq;
351 bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
354 rxrpc_inc_stat(call->rxnet, stat_rx_data);
355 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
356 rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
357 if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
358 rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
361 if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
363 rxrpc_proto_abort("LSN", call, seq);
367 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
368 after_eq(seq, wtop)) {
369 pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
370 call->debug_id, seq, window, wtop, wlimit);
371 rxrpc_proto_abort("LSA", call, seq);
376 if (after(seq, call->rx_highest_seq))
377 call->rx_highest_seq = seq;
379 trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
381 if (before(seq, window)) {
382 ack_reason = RXRPC_ACK_DUPLICATE;
385 if (after(seq, wlimit)) {
386 ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
390 /* Queue the packet. */
392 rxrpc_seq_t reset_from;
393 bool reset_sack = false;
395 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
396 ack_reason = RXRPC_ACK_REQUESTED;
397 /* Send an immediate ACK if we fill in a hole */
398 else if (!skb_queue_empty(&call->rx_oos_queue))
399 ack_reason = RXRPC_ACK_DELAY;
402 if (after(window, wtop))
405 spin_lock(&call->recvmsg_queue.lock);
406 rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
409 while ((oos = skb_peek(&call->rx_oos_queue))) {
410 struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
412 if (after(osp->hdr.seq, window))
415 __skb_unlink(oos, &call->rx_oos_queue);
416 last = osp->hdr.flags & RXRPC_LAST_PACKET;
424 rxrpc_input_queue_data(call, oos, window, wtop,
425 rxrpc_receive_queue_oos);
428 spin_unlock(&call->recvmsg_queue.lock);
432 call->ackr_sack_table[reset_from % RXRPC_SACK_SIZE] = 0;
433 } while (reset_from++, before(reset_from, window));
438 ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;
440 if (!call->ackr_sack_table[seq % RXRPC_SACK_SIZE]) {
441 call->ackr_sack_table[seq % RXRPC_SACK_SIZE] = 1;
445 if (after(seq + 1, wtop)) {
447 rxrpc_input_update_ack_window(call, window, wtop);
451 ack_reason = RXRPC_ACK_DUPLICATE;
455 skb_queue_walk(&call->rx_oos_queue, oos) {
456 struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
458 if (after(osp->hdr.seq, seq)) {
459 __skb_queue_before(&call->rx_oos_queue, oos, skb);
464 __skb_queue_tail(&call->rx_oos_queue, skb);
466 trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
467 sp->hdr.serial, sp->hdr.seq);
472 if (ack_reason < 0 &&
473 atomic_inc_return(&call->ackr_nr_unacked) > 2 &&
474 test_and_set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags)) {
475 ack_reason = RXRPC_ACK_IDLE;
476 } else if (ack_reason >= 0) {
477 set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags);
481 rxrpc_send_ACK(call, ack_reason, serial,
482 rxrpc_propose_ack_input_data);
484 rxrpc_propose_delay_ACK(call, serial,
485 rxrpc_propose_ack_input_data);
488 rxrpc_free_skb(skb, rxrpc_skb_put_input);
492 * Split a jumbo packet and file the bits separately.
494 static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
496 struct rxrpc_jumbo_header jhdr;
497 struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
498 struct sk_buff *jskb;
499 unsigned int offset = sizeof(struct rxrpc_wire_header);
500 unsigned int len = skb->len - offset;
502 while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
503 if (len < RXRPC_JUMBO_SUBPKTLEN)
505 if (sp->hdr.flags & RXRPC_LAST_PACKET)
507 if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
508 &jhdr, sizeof(jhdr)) < 0)
511 jskb = skb_clone(skb, GFP_ATOMIC);
513 kdebug("couldn't clone");
516 rxrpc_new_skb(jskb, rxrpc_skb_new_jumbo_subpacket);
517 jsp = rxrpc_skb(jskb);
518 jsp->offset = offset;
519 jsp->len = RXRPC_JUMBO_DATALEN;
520 rxrpc_input_data_one(call, jskb);
522 sp->hdr.flags = jhdr.flags;
523 sp->hdr._rsvd = ntohs(jhdr._rsvd);
526 offset += RXRPC_JUMBO_SUBPKTLEN;
527 len -= RXRPC_JUMBO_SUBPKTLEN;
532 rxrpc_input_data_one(call, skb);
540 * Process a DATA packet, adding the packet to the Rx ring. The caller's
541 * packet ref must be passed on or discarded.
543 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
545 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
546 enum rxrpc_call_state state;
547 rxrpc_serial_t serial = sp->hdr.serial;
548 rxrpc_seq_t seq0 = sp->hdr.seq;
550 _enter("{%llx,%x},{%u,%x}",
551 atomic64_read(&call->ackr_window), call->rx_highest_seq,
554 state = READ_ONCE(call->state);
555 if (state >= RXRPC_CALL_COMPLETE) {
556 rxrpc_free_skb(skb, rxrpc_skb_put_input);
560 /* Unshare the packet so that it can be modified for in-place
563 if (sp->hdr.securityIndex != 0) {
564 struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
566 rxrpc_eaten_skb(skb, rxrpc_skb_eaten_by_unshare_nomem);
571 rxrpc_eaten_skb(skb, rxrpc_skb_eaten_by_unshare);
573 rxrpc_new_skb(skb, rxrpc_skb_new_unshared);
578 if (state == RXRPC_CALL_SERVER_RECV_REQUEST) {
579 unsigned long timo = READ_ONCE(call->next_req_timo);
580 unsigned long now, expect_req_by;
584 expect_req_by = now + timo;
585 WRITE_ONCE(call->expect_req_by, expect_req_by);
586 rxrpc_reduce_call_timer(call, expect_req_by, now,
587 rxrpc_timer_set_for_idle);
591 /* Received data implicitly ACKs all of the request packets we sent
592 * when we're acting as a client.
594 if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
595 state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
596 !rxrpc_receiving_reply(call))
599 if (!rxrpc_input_split_jumbo(call, skb)) {
600 rxrpc_proto_abort("VLD", call, sp->hdr.seq);
606 trace_rxrpc_notify_socket(call->debug_id, serial);
607 rxrpc_notify_socket(call);
608 rxrpc_free_skb(skb, rxrpc_skb_put_input);
613 * See if there's a cached RTT probe to complete.
615 static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
617 rxrpc_serial_t acked_serial,
618 rxrpc_serial_t ack_serial,
619 enum rxrpc_rtt_rx_trace type)
621 rxrpc_serial_t orig_serial;
624 bool matched = false;
627 avail = READ_ONCE(call->rtt_avail);
628 smp_rmb(); /* Read avail bits before accessing data. */
630 for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
631 if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
634 sent_at = call->rtt_sent_at[i];
635 orig_serial = call->rtt_serial[i];
637 if (orig_serial == acked_serial) {
638 clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
639 smp_mb(); /* Read data before setting avail bit */
640 set_bit(i, &call->rtt_avail);
641 if (type != rxrpc_rtt_rx_cancel)
642 rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
645 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_cancel, i,
646 orig_serial, acked_serial, 0, 0);
650 /* If a later serial is being acked, then mark this slot as
653 if (after(acked_serial, orig_serial)) {
654 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
655 orig_serial, acked_serial, 0, 0);
656 clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
658 set_bit(i, &call->rtt_avail);
663 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
667 * Process the response to a ping that we sent to find out if we lost an ACK.
669 * If we got back a ping response that indicates a lower tx_top than what we
670 * had at the time of the ping transmission, we adjudge all the DATA packets
671 * sent between the response tx_top and the ping-time tx_top to have been lost.
673 static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
675 if (after(call->acks_lost_top, call->acks_prev_seq) &&
676 !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
677 rxrpc_queue_call(call, rxrpc_call_queue_resend);
681 * Process a ping response.
683 static void rxrpc_input_ping_response(struct rxrpc_call *call,
685 rxrpc_serial_t acked_serial,
686 rxrpc_serial_t ack_serial)
688 if (acked_serial == call->acks_lost_ping)
689 rxrpc_input_check_for_lost_ack(call);
693 * Process the extra information that may be appended to an ACK packet
695 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
696 struct rxrpc_ackinfo *ackinfo)
698 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
699 struct rxrpc_peer *peer;
702 u32 rwind = ntohl(ackinfo->rwind);
704 if (rwind > RXRPC_TX_MAX_WINDOW)
705 rwind = RXRPC_TX_MAX_WINDOW;
706 if (call->tx_winsize != rwind) {
707 if (rwind > call->tx_winsize)
709 trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
710 call->tx_winsize = rwind;
713 if (call->cong_ssthresh > rwind)
714 call->cong_ssthresh = rwind;
716 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
719 if (mtu < peer->maxdata) {
720 spin_lock_bh(&peer->lock);
722 peer->mtu = mtu + peer->hdrsize;
723 spin_unlock_bh(&peer->lock);
727 wake_up(&call->waitq);
731 * Process individual soft ACKs.
733 * Each ACK in the array corresponds to one packet and can be either an ACK or
734 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
735 * packets that lie beyond the end of the ACK list are scheduled for resend by
736 * the timer on the basis that the peer might just not have processed them at
737 * the time the ACK was sent.
739 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
740 rxrpc_seq_t seq, int nr_acks,
741 struct rxrpc_ack_summary *summary)
745 for (i = 0; i < nr_acks; i++) {
746 if (acks[i] == RXRPC_ACK_TYPE_ACK) {
748 summary->nr_new_acks++;
750 if (!summary->saw_nacks &&
751 call->acks_lowest_nak != seq + i) {
752 call->acks_lowest_nak = seq + i;
753 summary->new_low_nack = true;
755 summary->saw_nacks = true;
761 * Return true if the ACK is valid - ie. it doesn't appear to have regressed
762 * with respect to the ack state conveyed by preceding ACKs.
764 static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
765 rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
767 rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
769 if (after(first_pkt, base))
770 return true; /* The window advanced */
772 if (before(first_pkt, base))
773 return false; /* firstPacket regressed */
775 if (after_eq(prev_pkt, call->acks_prev_seq))
776 return true; /* previousPacket hasn't regressed. */
778 /* Some rx implementations put a serial number in previousPacket. */
779 if (after_eq(prev_pkt, base + call->tx_winsize))
785 * Process an ACK packet.
787 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
788 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
790 * A hard-ACK means that a packet has been processed and may be discarded; a
791 * soft-ACK means that the packet may be discarded and retransmission
792 * requested. A phase is complete when all packets are hard-ACK'd.
794 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
796 struct rxrpc_ack_summary summary = { 0 };
797 struct rxrpc_ackpacket ack;
798 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
799 struct rxrpc_ackinfo info;
800 struct sk_buff *skb_old = NULL, *skb_put = skb;
801 rxrpc_serial_t ack_serial, acked_serial;
802 rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
803 int nr_acks, offset, ioffset;
807 offset = sizeof(struct rxrpc_wire_header);
808 if (skb_copy_bits(skb, offset, &ack, sizeof(ack)) < 0) {
809 rxrpc_proto_abort("XAK", call, 0);
812 offset += sizeof(ack);
814 ack_serial = sp->hdr.serial;
815 acked_serial = ntohl(ack.serial);
816 first_soft_ack = ntohl(ack.firstPacket);
817 prev_pkt = ntohl(ack.previousPacket);
818 hard_ack = first_soft_ack - 1;
820 summary.ack_reason = (ack.reason < RXRPC_ACK__INVALID ?
821 ack.reason : RXRPC_ACK__INVALID);
823 trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
824 first_soft_ack, prev_pkt,
825 summary.ack_reason, nr_acks);
826 rxrpc_inc_stat(call->rxnet, stat_rx_acks[ack.reason]);
828 switch (ack.reason) {
829 case RXRPC_ACK_PING_RESPONSE:
830 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
831 rxrpc_rtt_rx_ping_response);
833 case RXRPC_ACK_REQUESTED:
834 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
835 rxrpc_rtt_rx_requested_ack);
838 if (acked_serial != 0)
839 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
840 rxrpc_rtt_rx_cancel);
844 if (ack.reason == RXRPC_ACK_PING) {
845 rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
846 rxrpc_propose_ack_respond_to_ping);
847 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
848 rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
849 rxrpc_propose_ack_respond_to_ack);
852 /* If we get an EXCEEDS_WINDOW ACK from the server, it probably
853 * indicates that the client address changed due to NAT. The server
854 * lost the call because it switched to a different peer.
856 if (unlikely(ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
857 first_soft_ack == 1 &&
859 rxrpc_is_client_call(call)) {
860 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
865 /* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
866 * indicate a change of address. However, we can retransmit the call
867 * if we still have it buffered to the beginning.
869 if (unlikely(ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
870 first_soft_ack == 1 &&
872 call->acks_hard_ack == 0 &&
873 rxrpc_is_client_call(call)) {
874 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
879 /* Discard any out-of-order or duplicate ACKs (outside lock). */
880 if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
881 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
882 first_soft_ack, call->acks_first_seq,
883 prev_pkt, call->acks_prev_seq);
888 ioffset = offset + nr_acks + 3;
889 if (skb->len >= ioffset + sizeof(info) &&
890 skb_copy_bits(skb, ioffset, &info, sizeof(info)) < 0) {
891 rxrpc_proto_abort("XAI", call, 0);
898 /* Discard any out-of-order or duplicate ACKs (inside lock). */
899 if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
900 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
901 first_soft_ack, call->acks_first_seq,
902 prev_pkt, call->acks_prev_seq);
905 call->acks_latest_ts = skb->tstamp;
907 call->acks_first_seq = first_soft_ack;
908 call->acks_prev_seq = prev_pkt;
910 switch (ack.reason) {
913 case RXRPC_ACK_PING_RESPONSE:
914 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
918 if (after(acked_serial, call->acks_highest_serial))
919 call->acks_highest_serial = acked_serial;
923 /* Parse rwind and mtu sizes if provided. */
925 rxrpc_input_ackinfo(call, skb, &info);
927 if (first_soft_ack == 0) {
928 rxrpc_proto_abort("AK0", call, 0);
932 /* Ignore ACKs unless we are or have just been transmitting. */
933 switch (READ_ONCE(call->state)) {
934 case RXRPC_CALL_CLIENT_SEND_REQUEST:
935 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
936 case RXRPC_CALL_SERVER_SEND_REPLY:
937 case RXRPC_CALL_SERVER_AWAIT_ACK:
943 if (before(hard_ack, call->acks_hard_ack) ||
944 after(hard_ack, call->tx_top)) {
945 rxrpc_proto_abort("AKW", call, 0);
948 if (nr_acks > call->tx_top - hard_ack) {
949 rxrpc_proto_abort("AKN", call, 0);
953 if (after(hard_ack, call->acks_hard_ack)) {
954 if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
955 rxrpc_end_tx_phase(call, false, "ETA");
961 if (offset > (int)skb->len - nr_acks) {
962 rxrpc_proto_abort("XSA", call, 0);
966 spin_lock(&call->acks_ack_lock);
967 skb_old = call->acks_soft_tbl;
968 call->acks_soft_tbl = skb;
969 spin_unlock(&call->acks_ack_lock);
971 rxrpc_input_soft_acks(call, skb->data + offset, first_soft_ack,
974 } else if (call->acks_soft_tbl) {
975 spin_lock(&call->acks_ack_lock);
976 skb_old = call->acks_soft_tbl;
977 call->acks_soft_tbl = NULL;
978 spin_unlock(&call->acks_ack_lock);
981 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
982 summary.nr_acks == call->tx_top - hard_ack &&
983 rxrpc_is_client_call(call))
984 rxrpc_propose_ping(call, ack_serial,
985 rxrpc_propose_ack_ping_for_lost_reply);
987 rxrpc_congestion_management(call, skb, &summary, acked_serial);
989 rxrpc_free_skb(skb_put, rxrpc_skb_put_input);
990 rxrpc_free_skb(skb_old, rxrpc_skb_put_ack);
994 * Process an ACKALL packet.
996 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
998 struct rxrpc_ack_summary summary = { 0 };
1000 if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
1001 rxrpc_end_tx_phase(call, false, "ETL");
1005 * Process an ABORT packet directed at a call.
1007 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
1009 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1011 trace_rxrpc_rx_abort(call, sp->hdr.serial, skb->priority);
1013 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1014 skb->priority, -ECONNABORTED);
1018 * Process an incoming call packet.
1020 void rxrpc_input_call_event(struct rxrpc_call *call, struct sk_buff *skb)
1022 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1025 _enter("%p,%p", call, skb);
1027 timo = READ_ONCE(call->next_rx_timo);
1029 unsigned long now = jiffies, expect_rx_by;
1031 expect_rx_by = now + timo;
1032 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
1033 rxrpc_reduce_call_timer(call, expect_rx_by, now,
1034 rxrpc_timer_set_for_normal);
1037 switch (sp->hdr.type) {
1038 case RXRPC_PACKET_TYPE_DATA:
1039 rxrpc_input_data(call, skb);
1042 case RXRPC_PACKET_TYPE_ACK:
1043 rxrpc_input_ack(call, skb);
1046 case RXRPC_PACKET_TYPE_BUSY:
1047 /* Just ignore BUSY packets from the server; the retry and
1048 * lifespan timers will take care of business. BUSY packets
1049 * from the client don't make sense.
1053 case RXRPC_PACKET_TYPE_ABORT:
1054 rxrpc_input_abort(call, skb);
1057 case RXRPC_PACKET_TYPE_ACKALL:
1058 rxrpc_input_ackall(call, skb);
1065 rxrpc_free_skb(skb, rxrpc_skb_put_input);
1071 * Handle a new service call on a channel implicitly completing the preceding
1072 * call on that channel. This does not apply to client conns.
1074 * TODO: If callNumber > call_id + 1, renegotiate security.
1076 void rxrpc_input_implicit_end_call(struct rxrpc_sock *rx,
1077 struct rxrpc_connection *conn,
1078 struct rxrpc_call *call)
1080 switch (READ_ONCE(call->state)) {
1081 case RXRPC_CALL_SERVER_AWAIT_ACK:
1082 rxrpc_call_completed(call);
1084 case RXRPC_CALL_COMPLETE:
1087 if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
1088 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
1089 rxrpc_queue_call(call, rxrpc_call_queue_abort);
1091 trace_rxrpc_improper_term(call);
1095 spin_lock(&rx->incoming_lock);
1096 __rxrpc_disconnect_call(conn, call);
1097 spin_unlock(&rx->incoming_lock);