1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RxRPC packet reception
4 * Copyright (C) 2007, 2016 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);
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 switch (call->cong_mode) {
62 case RXRPC_CALL_SLOW_START:
63 if (summary->nr_nacks > 0)
64 goto packet_loss_detected;
65 if (summary->cumulative_acks > 0)
67 if (cwnd >= call->cong_ssthresh) {
68 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
69 call->cong_tstamp = skb->tstamp;
73 case RXRPC_CALL_CONGEST_AVOIDANCE:
74 if (summary->nr_nacks > 0)
75 goto packet_loss_detected;
77 /* We analyse the number of packets that get ACK'd per RTT
78 * period and increase the window if we managed to fill it.
80 if (call->peer->rtt_count == 0)
82 if (ktime_before(skb->tstamp,
83 ktime_add_us(call->cong_tstamp,
84 call->peer->srtt_us >> 3)))
86 change = rxrpc_cong_rtt_window_end;
87 call->cong_tstamp = skb->tstamp;
88 if (cumulative_acks >= cwnd)
92 case RXRPC_CALL_PACKET_LOSS:
93 if (summary->nr_nacks == 0)
94 goto resume_normality;
96 if (summary->new_low_nack) {
97 change = rxrpc_cong_new_low_nack;
98 call->cong_dup_acks = 1;
99 if (call->cong_extra > 1)
100 call->cong_extra = 1;
101 goto send_extra_data;
104 call->cong_dup_acks++;
105 if (call->cong_dup_acks < 3)
106 goto send_extra_data;
108 change = rxrpc_cong_begin_retransmission;
109 call->cong_mode = RXRPC_CALL_FAST_RETRANSMIT;
110 call->cong_ssthresh = max_t(unsigned int,
111 summary->flight_size / 2, 2);
112 cwnd = call->cong_ssthresh + 3;
113 call->cong_extra = 0;
114 call->cong_dup_acks = 0;
118 case RXRPC_CALL_FAST_RETRANSMIT:
119 if (!summary->new_low_nack) {
120 if (summary->nr_new_acks == 0)
122 call->cong_dup_acks++;
123 if (call->cong_dup_acks == 2) {
124 change = rxrpc_cong_retransmit_again;
125 call->cong_dup_acks = 0;
129 change = rxrpc_cong_progress;
130 cwnd = call->cong_ssthresh;
131 if (summary->nr_nacks == 0)
132 goto resume_normality;
142 change = rxrpc_cong_cleared_nacks;
143 call->cong_dup_acks = 0;
144 call->cong_extra = 0;
145 call->cong_tstamp = skb->tstamp;
146 if (cwnd < call->cong_ssthresh)
147 call->cong_mode = RXRPC_CALL_SLOW_START;
149 call->cong_mode = RXRPC_CALL_CONGEST_AVOIDANCE;
153 if (cwnd >= RXRPC_TX_MAX_WINDOW)
154 cwnd = RXRPC_TX_MAX_WINDOW;
155 call->cong_cwnd = cwnd;
156 call->cong_cumul_acks = cumulative_acks;
157 trace_rxrpc_congest(call, summary, acked_serial, change);
158 if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
159 rxrpc_queue_call(call);
162 packet_loss_detected:
163 change = rxrpc_cong_saw_nack;
164 call->cong_mode = RXRPC_CALL_PACKET_LOSS;
165 call->cong_dup_acks = 0;
166 goto send_extra_data;
169 /* Send some previously unsent DATA if we have some to advance the ACK
172 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ||
173 summary->nr_acks != call->tx_top - call->acks_hard_ack) {
175 wake_up(&call->waitq);
177 goto out_no_clear_ca;
181 * Apply a hard ACK by advancing the Tx window.
183 static bool rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to,
184 struct rxrpc_ack_summary *summary)
186 struct rxrpc_txbuf *txb;
187 bool rot_last = false;
189 list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
190 if (before_eq(txb->seq, call->acks_hard_ack))
192 if (!test_bit(RXRPC_TXBUF_ACKED, &txb->flags))
193 summary->nr_rot_new_acks++;
194 if (test_bit(RXRPC_TXBUF_LAST, &txb->flags)) {
195 set_bit(RXRPC_CALL_TX_LAST, &call->flags);
203 set_bit(RXRPC_CALL_TX_ALL_ACKED, &call->flags);
205 _enter("%x,%x,%x,%d", to, call->acks_hard_ack, call->tx_top, rot_last);
207 if (call->acks_lowest_nak == call->acks_hard_ack) {
208 call->acks_lowest_nak = to;
209 } else if (before_eq(call->acks_lowest_nak, to)) {
210 summary->new_low_nack = true;
211 call->acks_lowest_nak = to;
214 smp_store_release(&call->acks_hard_ack, to);
216 trace_rxrpc_txqueue(call, (rot_last ?
217 rxrpc_txqueue_rotate_last :
218 rxrpc_txqueue_rotate));
219 wake_up(&call->waitq);
224 * End the transmission phase of a call.
226 * This occurs when we get an ACKALL packet, the first DATA packet of a reply,
227 * or a final ACK packet.
229 static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun,
230 const char *abort_why)
234 ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags));
236 write_lock(&call->state_lock);
240 case RXRPC_CALL_CLIENT_SEND_REQUEST:
241 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
243 call->state = state = RXRPC_CALL_CLIENT_RECV_REPLY;
245 call->state = state = RXRPC_CALL_CLIENT_AWAIT_REPLY;
248 case RXRPC_CALL_SERVER_AWAIT_ACK:
249 __rxrpc_call_completed(call);
257 write_unlock(&call->state_lock);
258 if (state == RXRPC_CALL_CLIENT_AWAIT_REPLY)
259 trace_rxrpc_txqueue(call, rxrpc_txqueue_await_reply);
261 trace_rxrpc_txqueue(call, rxrpc_txqueue_end);
266 write_unlock(&call->state_lock);
267 kdebug("end_tx %s", rxrpc_call_states[call->state]);
268 rxrpc_proto_abort(abort_why, call, call->tx_top);
273 * Begin the reply reception phase of a call.
275 static bool rxrpc_receiving_reply(struct rxrpc_call *call)
277 struct rxrpc_ack_summary summary = { 0 };
278 unsigned long now, timo;
279 rxrpc_seq_t top = READ_ONCE(call->tx_top);
281 if (call->ackr_reason) {
283 timo = now + MAX_JIFFY_OFFSET;
284 WRITE_ONCE(call->resend_at, timo);
285 WRITE_ONCE(call->delay_ack_at, timo);
286 trace_rxrpc_timer(call, rxrpc_timer_init_for_reply, now);
289 if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) {
290 if (!rxrpc_rotate_tx_window(call, top, &summary)) {
291 rxrpc_proto_abort("TXL", call, top);
295 return rxrpc_end_tx_phase(call, true, "ETD");
298 static void rxrpc_input_update_ack_window(struct rxrpc_call *call,
299 rxrpc_seq_t window, rxrpc_seq_t wtop)
301 atomic64_set_release(&call->ackr_window, ((u64)wtop) << 32 | window);
305 * Push a DATA packet onto the Rx queue.
307 static void rxrpc_input_queue_data(struct rxrpc_call *call, struct sk_buff *skb,
308 rxrpc_seq_t window, rxrpc_seq_t wtop,
309 enum rxrpc_receive_trace why)
311 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
312 bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
314 __skb_queue_tail(&call->recvmsg_queue, skb);
315 rxrpc_input_update_ack_window(call, window, wtop);
317 trace_rxrpc_receive(call, last ? why + 1 : why, sp->hdr.serial, sp->hdr.seq);
321 * Process a DATA packet.
323 static void rxrpc_input_data_one(struct rxrpc_call *call, struct sk_buff *skb)
325 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
327 rxrpc_serial_t serial = sp->hdr.serial;
328 u64 win = atomic64_read(&call->ackr_window);
329 rxrpc_seq_t window = lower_32_bits(win);
330 rxrpc_seq_t wtop = upper_32_bits(win);
331 rxrpc_seq_t wlimit = window + call->rx_winsize - 1;
332 rxrpc_seq_t seq = sp->hdr.seq;
333 bool last = sp->hdr.flags & RXRPC_LAST_PACKET;
336 rxrpc_inc_stat(call->rxnet, stat_rx_data);
337 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
338 rxrpc_inc_stat(call->rxnet, stat_rx_data_reqack);
339 if (sp->hdr.flags & RXRPC_JUMBO_PACKET)
340 rxrpc_inc_stat(call->rxnet, stat_rx_data_jumbo);
343 if (test_and_set_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
345 rxrpc_proto_abort("LSN", call, seq);
349 if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) &&
350 after_eq(seq, wtop)) {
351 pr_warn("Packet beyond last: c=%x q=%x window=%x-%x wlimit=%x\n",
352 call->debug_id, seq, window, wtop, wlimit);
353 rxrpc_proto_abort("LSA", call, seq);
358 if (after(seq, call->rx_highest_seq))
359 call->rx_highest_seq = seq;
361 trace_rxrpc_rx_data(call->debug_id, seq, serial, sp->hdr.flags);
363 if (before(seq, window)) {
364 ack_reason = RXRPC_ACK_DUPLICATE;
367 if (after(seq, wlimit)) {
368 ack_reason = RXRPC_ACK_EXCEEDS_WINDOW;
372 /* Queue the packet. */
374 rxrpc_seq_t reset_from;
375 bool reset_sack = false;
377 if (sp->hdr.flags & RXRPC_REQUEST_ACK)
378 ack_reason = RXRPC_ACK_REQUESTED;
379 /* Send an immediate ACK if we fill in a hole */
380 else if (!skb_queue_empty(&call->rx_oos_queue))
381 ack_reason = RXRPC_ACK_DELAY;
384 if (after(window, wtop))
387 spin_lock(&call->recvmsg_queue.lock);
388 rxrpc_input_queue_data(call, skb, window, wtop, rxrpc_receive_queue);
391 while ((oos = skb_peek(&call->rx_oos_queue))) {
392 struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
394 if (after(osp->hdr.seq, window))
397 __skb_unlink(oos, &call->rx_oos_queue);
398 last = osp->hdr.flags & RXRPC_LAST_PACKET;
406 rxrpc_input_queue_data(call, oos, window, wtop,
407 rxrpc_receive_queue_oos);
410 spin_unlock(&call->recvmsg_queue.lock);
414 call->ackr_sack_table[reset_from % RXRPC_SACK_SIZE] = 0;
415 } while (reset_from++, before(reset_from, window));
420 ack_reason = RXRPC_ACK_OUT_OF_SEQUENCE;
422 if (!call->ackr_sack_table[seq % RXRPC_SACK_SIZE]) {
423 call->ackr_sack_table[seq % RXRPC_SACK_SIZE] = 1;
427 if (after(seq + 1, wtop)) {
429 rxrpc_input_update_ack_window(call, window, wtop);
433 ack_reason = RXRPC_ACK_DUPLICATE;
437 skb_queue_walk(&call->rx_oos_queue, oos) {
438 struct rxrpc_skb_priv *osp = rxrpc_skb(oos);
440 if (after(osp->hdr.seq, seq)) {
441 __skb_queue_before(&call->rx_oos_queue, oos, skb);
446 __skb_queue_tail(&call->rx_oos_queue, skb);
448 trace_rxrpc_receive(call, last ? rxrpc_receive_oos_last : rxrpc_receive_oos,
449 sp->hdr.serial, sp->hdr.seq);
454 if (ack_reason < 0 &&
455 atomic_inc_return(&call->ackr_nr_unacked) > 2 &&
456 test_and_set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags)) {
457 ack_reason = RXRPC_ACK_IDLE;
458 } else if (ack_reason >= 0) {
459 set_bit(RXRPC_CALL_IDLE_ACK_PENDING, &call->flags);
463 rxrpc_send_ACK(call, ack_reason, serial,
464 rxrpc_propose_ack_input_data);
466 rxrpc_propose_delay_ACK(call, serial,
467 rxrpc_propose_ack_input_data);
470 rxrpc_free_skb(skb, rxrpc_skb_freed);
474 * Split a jumbo packet and file the bits separately.
476 static bool rxrpc_input_split_jumbo(struct rxrpc_call *call, struct sk_buff *skb)
478 struct rxrpc_jumbo_header jhdr;
479 struct rxrpc_skb_priv *sp = rxrpc_skb(skb), *jsp;
480 struct sk_buff *jskb;
481 unsigned int offset = sizeof(struct rxrpc_wire_header);
482 unsigned int len = skb->len - offset;
484 while (sp->hdr.flags & RXRPC_JUMBO_PACKET) {
485 if (len < RXRPC_JUMBO_SUBPKTLEN)
487 if (sp->hdr.flags & RXRPC_LAST_PACKET)
489 if (skb_copy_bits(skb, offset + RXRPC_JUMBO_DATALEN,
490 &jhdr, sizeof(jhdr)) < 0)
493 jskb = skb_clone(skb, GFP_ATOMIC);
495 kdebug("couldn't clone");
498 rxrpc_new_skb(jskb, rxrpc_skb_cloned_jumbo);
499 jsp = rxrpc_skb(jskb);
500 jsp->offset = offset;
501 jsp->len = RXRPC_JUMBO_DATALEN;
502 rxrpc_input_data_one(call, jskb);
504 sp->hdr.flags = jhdr.flags;
505 sp->hdr._rsvd = ntohs(jhdr._rsvd);
508 offset += RXRPC_JUMBO_SUBPKTLEN;
509 len -= RXRPC_JUMBO_SUBPKTLEN;
514 rxrpc_input_data_one(call, skb);
522 * Process a DATA packet, adding the packet to the Rx ring. The caller's
523 * packet ref must be passed on or discarded.
525 static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb)
527 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
528 enum rxrpc_call_state state;
529 rxrpc_serial_t serial = sp->hdr.serial;
530 rxrpc_seq_t seq0 = sp->hdr.seq;
532 _enter("{%llx,%x},{%u,%x}",
533 atomic64_read(&call->ackr_window), call->rx_highest_seq,
536 _proto("Rx DATA %%%u { #%u f=%02x }",
537 sp->hdr.serial, seq0, sp->hdr.flags);
539 state = READ_ONCE(call->state);
540 if (state >= RXRPC_CALL_COMPLETE) {
541 rxrpc_free_skb(skb, rxrpc_skb_freed);
545 /* Unshare the packet so that it can be modified for in-place
548 if (sp->hdr.securityIndex != 0) {
549 struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
551 rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
556 rxrpc_eaten_skb(skb, rxrpc_skb_received);
558 rxrpc_new_skb(skb, rxrpc_skb_unshared);
563 if (state == RXRPC_CALL_SERVER_RECV_REQUEST) {
564 unsigned long timo = READ_ONCE(call->next_req_timo);
565 unsigned long now, expect_req_by;
569 expect_req_by = now + timo;
570 WRITE_ONCE(call->expect_req_by, expect_req_by);
571 rxrpc_reduce_call_timer(call, expect_req_by, now,
572 rxrpc_timer_set_for_idle);
576 spin_lock(&call->input_lock);
578 /* Received data implicitly ACKs all of the request packets we sent
579 * when we're acting as a client.
581 if ((state == RXRPC_CALL_CLIENT_SEND_REQUEST ||
582 state == RXRPC_CALL_CLIENT_AWAIT_REPLY) &&
583 !rxrpc_receiving_reply(call))
586 if (!rxrpc_input_split_jumbo(call, skb)) {
587 rxrpc_proto_abort("VLD", call, sp->hdr.seq);
593 trace_rxrpc_notify_socket(call->debug_id, serial);
594 rxrpc_notify_socket(call);
596 spin_unlock(&call->input_lock);
597 rxrpc_free_skb(skb, rxrpc_skb_freed);
602 * See if there's a cached RTT probe to complete.
604 static void rxrpc_complete_rtt_probe(struct rxrpc_call *call,
606 rxrpc_serial_t acked_serial,
607 rxrpc_serial_t ack_serial,
608 enum rxrpc_rtt_rx_trace type)
610 rxrpc_serial_t orig_serial;
613 bool matched = false;
616 avail = READ_ONCE(call->rtt_avail);
617 smp_rmb(); /* Read avail bits before accessing data. */
619 for (i = 0; i < ARRAY_SIZE(call->rtt_serial); i++) {
620 if (!test_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &avail))
623 sent_at = call->rtt_sent_at[i];
624 orig_serial = call->rtt_serial[i];
626 if (orig_serial == acked_serial) {
627 clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
628 smp_mb(); /* Read data before setting avail bit */
629 set_bit(i, &call->rtt_avail);
630 if (type != rxrpc_rtt_rx_cancel)
631 rxrpc_peer_add_rtt(call, type, i, acked_serial, ack_serial,
634 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_cancel, i,
635 orig_serial, acked_serial, 0, 0);
639 /* If a later serial is being acked, then mark this slot as
642 if (after(acked_serial, orig_serial)) {
643 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_obsolete, i,
644 orig_serial, acked_serial, 0, 0);
645 clear_bit(i + RXRPC_CALL_RTT_PEND_SHIFT, &call->rtt_avail);
647 set_bit(i, &call->rtt_avail);
652 trace_rxrpc_rtt_rx(call, rxrpc_rtt_rx_lost, 9, 0, acked_serial, 0, 0);
656 * Process the response to a ping that we sent to find out if we lost an ACK.
658 * If we got back a ping response that indicates a lower tx_top than what we
659 * had at the time of the ping transmission, we adjudge all the DATA packets
660 * sent between the response tx_top and the ping-time tx_top to have been lost.
662 static void rxrpc_input_check_for_lost_ack(struct rxrpc_call *call)
664 struct rxrpc_txbuf *txb;
665 rxrpc_seq_t top, bottom;
668 bottom = READ_ONCE(call->acks_hard_ack) + 1;
669 top = READ_ONCE(call->acks_lost_top);
670 if (before(bottom, top)) {
671 list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
672 if (test_bit(RXRPC_TXBUF_ACKED, &txb->flags))
674 set_bit(RXRPC_TXBUF_RETRANS, &txb->flags);
679 if (resend && !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events))
680 rxrpc_queue_call(call);
684 * Process a ping response.
686 static void rxrpc_input_ping_response(struct rxrpc_call *call,
688 rxrpc_serial_t acked_serial,
689 rxrpc_serial_t ack_serial)
691 if (acked_serial == call->acks_lost_ping)
692 rxrpc_input_check_for_lost_ack(call);
696 * Process the extra information that may be appended to an ACK packet
698 static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
699 struct rxrpc_ackinfo *ackinfo)
701 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
702 struct rxrpc_peer *peer;
705 u32 rwind = ntohl(ackinfo->rwind);
707 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
709 ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU),
710 rwind, ntohl(ackinfo->jumbo_max));
712 if (rwind > RXRPC_TX_MAX_WINDOW)
713 rwind = RXRPC_TX_MAX_WINDOW;
714 if (call->tx_winsize != rwind) {
715 if (rwind > call->tx_winsize)
717 trace_rxrpc_rx_rwind_change(call, sp->hdr.serial, rwind, wake);
718 call->tx_winsize = rwind;
721 if (call->cong_ssthresh > rwind)
722 call->cong_ssthresh = rwind;
724 mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU));
727 if (mtu < peer->maxdata) {
728 spin_lock_bh(&peer->lock);
730 peer->mtu = mtu + peer->hdrsize;
731 spin_unlock_bh(&peer->lock);
732 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
736 wake_up(&call->waitq);
740 * Process individual soft ACKs.
742 * Each ACK in the array corresponds to one packet and can be either an ACK or
743 * a NAK. If we get find an explicitly NAK'd packet we resend immediately;
744 * packets that lie beyond the end of the ACK list are scheduled for resend by
745 * the timer on the basis that the peer might just not have processed them at
746 * the time the ACK was sent.
748 static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks,
749 rxrpc_seq_t seq, int nr_acks,
750 struct rxrpc_ack_summary *summary)
752 struct rxrpc_txbuf *txb;
754 list_for_each_entry_rcu(txb, &call->tx_buffer, call_link, false) {
755 if (before(txb->seq, seq))
757 if (after_eq(txb->seq, seq + nr_acks))
759 switch (acks[txb->seq - seq]) {
760 case RXRPC_ACK_TYPE_ACK:
762 if (test_bit(RXRPC_TXBUF_ACKED, &txb->flags))
764 /* A lot of the time the packet is going to
765 * have been ACK.'d already.
767 clear_bit(RXRPC_TXBUF_NACKED, &txb->flags);
768 set_bit(RXRPC_TXBUF_ACKED, &txb->flags);
769 summary->nr_new_acks++;
771 case RXRPC_ACK_TYPE_NACK:
772 if (!summary->nr_nacks &&
773 call->acks_lowest_nak != seq) {
774 call->acks_lowest_nak = seq;
775 summary->new_low_nack = true;
778 if (test_bit(RXRPC_TXBUF_NACKED, &txb->flags))
780 summary->nr_new_nacks++;
781 clear_bit(RXRPC_TXBUF_ACKED, &txb->flags);
782 set_bit(RXRPC_TXBUF_NACKED, &txb->flags);
783 set_bit(RXRPC_TXBUF_RETRANS, &txb->flags);
786 return rxrpc_proto_abort("SFT", call, 0);
792 * Return true if the ACK is valid - ie. it doesn't appear to have regressed
793 * with respect to the ack state conveyed by preceding ACKs.
795 static bool rxrpc_is_ack_valid(struct rxrpc_call *call,
796 rxrpc_seq_t first_pkt, rxrpc_seq_t prev_pkt)
798 rxrpc_seq_t base = READ_ONCE(call->acks_first_seq);
800 if (after(first_pkt, base))
801 return true; /* The window advanced */
803 if (before(first_pkt, base))
804 return false; /* firstPacket regressed */
806 if (after_eq(prev_pkt, call->acks_prev_seq))
807 return true; /* previousPacket hasn't regressed. */
809 /* Some rx implementations put a serial number in previousPacket. */
810 if (after_eq(prev_pkt, base + call->tx_winsize))
816 * Process an ACK packet.
818 * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet
819 * in the ACK array. Anything before that is hard-ACK'd and may be discarded.
821 * A hard-ACK means that a packet has been processed and may be discarded; a
822 * soft-ACK means that the packet may be discarded and retransmission
823 * requested. A phase is complete when all packets are hard-ACK'd.
825 static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb)
827 struct rxrpc_ack_summary summary = { 0 };
828 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
830 struct rxrpc_ackpacket ack;
831 struct rxrpc_ackinfo info;
832 u8 acks[RXRPC_MAXACKS];
834 rxrpc_serial_t ack_serial, acked_serial;
835 rxrpc_seq_t first_soft_ack, hard_ack, prev_pkt;
836 int nr_acks, offset, ioffset;
840 offset = sizeof(struct rxrpc_wire_header);
841 if (skb_copy_bits(skb, offset, &buf.ack, sizeof(buf.ack)) < 0) {
842 _debug("extraction failure");
843 return rxrpc_proto_abort("XAK", call, 0);
845 offset += sizeof(buf.ack);
847 ack_serial = sp->hdr.serial;
848 acked_serial = ntohl(buf.ack.serial);
849 first_soft_ack = ntohl(buf.ack.firstPacket);
850 prev_pkt = ntohl(buf.ack.previousPacket);
851 hard_ack = first_soft_ack - 1;
852 nr_acks = buf.ack.nAcks;
853 summary.ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ?
854 buf.ack.reason : RXRPC_ACK__INVALID);
856 trace_rxrpc_rx_ack(call, ack_serial, acked_serial,
857 first_soft_ack, prev_pkt,
858 summary.ack_reason, nr_acks);
859 rxrpc_inc_stat(call->rxnet, stat_rx_acks[buf.ack.reason]);
861 switch (buf.ack.reason) {
862 case RXRPC_ACK_PING_RESPONSE:
863 rxrpc_input_ping_response(call, skb->tstamp, acked_serial,
865 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
866 rxrpc_rtt_rx_ping_response);
868 case RXRPC_ACK_REQUESTED:
869 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
870 rxrpc_rtt_rx_requested_ack);
873 if (acked_serial != 0)
874 rxrpc_complete_rtt_probe(call, skb->tstamp, acked_serial, ack_serial,
875 rxrpc_rtt_rx_cancel);
879 if (buf.ack.reason == RXRPC_ACK_PING) {
880 _proto("Rx ACK %%%u PING Request", ack_serial);
881 rxrpc_send_ACK(call, RXRPC_ACK_PING_RESPONSE, ack_serial,
882 rxrpc_propose_ack_respond_to_ping);
883 } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) {
884 rxrpc_send_ACK(call, RXRPC_ACK_REQUESTED, ack_serial,
885 rxrpc_propose_ack_respond_to_ack);
888 /* If we get an EXCEEDS_WINDOW ACK from the server, it probably
889 * indicates that the client address changed due to NAT. The server
890 * lost the call because it switched to a different peer.
892 if (unlikely(buf.ack.reason == RXRPC_ACK_EXCEEDS_WINDOW) &&
893 first_soft_ack == 1 &&
895 rxrpc_is_client_call(call)) {
896 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
901 /* If we get an OUT_OF_SEQUENCE ACK from the server, that can also
902 * indicate a change of address. However, we can retransmit the call
903 * if we still have it buffered to the beginning.
905 if (unlikely(buf.ack.reason == RXRPC_ACK_OUT_OF_SEQUENCE) &&
906 first_soft_ack == 1 &&
908 call->acks_hard_ack == 0 &&
909 rxrpc_is_client_call(call)) {
910 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
915 /* Discard any out-of-order or duplicate ACKs (outside lock). */
916 if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
917 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
918 first_soft_ack, call->acks_first_seq,
919 prev_pkt, call->acks_prev_seq);
924 ioffset = offset + nr_acks + 3;
925 if (skb->len >= ioffset + sizeof(buf.info) &&
926 skb_copy_bits(skb, ioffset, &buf.info, sizeof(buf.info)) < 0)
927 return rxrpc_proto_abort("XAI", call, 0);
929 spin_lock(&call->input_lock);
931 /* Discard any out-of-order or duplicate ACKs (inside lock). */
932 if (!rxrpc_is_ack_valid(call, first_soft_ack, prev_pkt)) {
933 trace_rxrpc_rx_discard_ack(call->debug_id, ack_serial,
934 first_soft_ack, call->acks_first_seq,
935 prev_pkt, call->acks_prev_seq);
938 call->acks_latest_ts = skb->tstamp;
940 call->acks_first_seq = first_soft_ack;
941 call->acks_prev_seq = prev_pkt;
943 if (buf.ack.reason != RXRPC_ACK_PING &&
944 after(acked_serial, call->acks_highest_serial))
945 call->acks_highest_serial = acked_serial;
947 /* Parse rwind and mtu sizes if provided. */
949 rxrpc_input_ackinfo(call, skb, &buf.info);
951 if (first_soft_ack == 0) {
952 rxrpc_proto_abort("AK0", call, 0);
956 /* Ignore ACKs unless we are or have just been transmitting. */
957 switch (READ_ONCE(call->state)) {
958 case RXRPC_CALL_CLIENT_SEND_REQUEST:
959 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
960 case RXRPC_CALL_SERVER_SEND_REPLY:
961 case RXRPC_CALL_SERVER_AWAIT_ACK:
967 if (before(hard_ack, call->acks_hard_ack) ||
968 after(hard_ack, call->tx_top)) {
969 rxrpc_proto_abort("AKW", call, 0);
972 if (nr_acks > call->tx_top - hard_ack) {
973 rxrpc_proto_abort("AKN", call, 0);
977 if (after(hard_ack, call->acks_hard_ack)) {
978 if (rxrpc_rotate_tx_window(call, hard_ack, &summary)) {
979 rxrpc_end_tx_phase(call, false, "ETA");
985 if (skb_copy_bits(skb, offset, buf.acks, nr_acks) < 0) {
986 rxrpc_proto_abort("XSA", call, 0);
989 rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks,
993 if (test_bit(RXRPC_CALL_TX_LAST, &call->flags) &&
994 summary.nr_acks == call->tx_top - hard_ack &&
995 rxrpc_is_client_call(call))
996 rxrpc_propose_ping(call, ack_serial,
997 rxrpc_propose_ack_ping_for_lost_reply);
999 rxrpc_congestion_management(call, skb, &summary, acked_serial);
1001 spin_unlock(&call->input_lock);
1005 * Process an ACKALL packet.
1007 static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb)
1009 struct rxrpc_ack_summary summary = { 0 };
1010 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1012 _proto("Rx ACKALL %%%u", sp->hdr.serial);
1014 spin_lock(&call->input_lock);
1016 if (rxrpc_rotate_tx_window(call, call->tx_top, &summary))
1017 rxrpc_end_tx_phase(call, false, "ETL");
1019 spin_unlock(&call->input_lock);
1023 * Process an ABORT packet directed at a call.
1025 static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb)
1027 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1029 u32 abort_code = RX_CALL_DEAD;
1033 if (skb->len >= 4 &&
1034 skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
1035 &wtmp, sizeof(wtmp)) >= 0)
1036 abort_code = ntohl(wtmp);
1038 trace_rxrpc_rx_abort(call, sp->hdr.serial, abort_code);
1040 _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code);
1042 rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED,
1043 abort_code, -ECONNABORTED);
1047 * Process an incoming call packet.
1049 static void rxrpc_input_call_packet(struct rxrpc_call *call,
1050 struct sk_buff *skb)
1052 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
1055 _enter("%p,%p", call, skb);
1057 timo = READ_ONCE(call->next_rx_timo);
1059 unsigned long now = jiffies, expect_rx_by;
1061 expect_rx_by = now + timo;
1062 WRITE_ONCE(call->expect_rx_by, expect_rx_by);
1063 rxrpc_reduce_call_timer(call, expect_rx_by, now,
1064 rxrpc_timer_set_for_normal);
1067 switch (sp->hdr.type) {
1068 case RXRPC_PACKET_TYPE_DATA:
1069 rxrpc_input_data(call, skb);
1072 case RXRPC_PACKET_TYPE_ACK:
1073 rxrpc_input_ack(call, skb);
1076 case RXRPC_PACKET_TYPE_BUSY:
1077 _proto("Rx BUSY %%%u", sp->hdr.serial);
1079 /* Just ignore BUSY packets from the server; the retry and
1080 * lifespan timers will take care of business. BUSY packets
1081 * from the client don't make sense.
1085 case RXRPC_PACKET_TYPE_ABORT:
1086 rxrpc_input_abort(call, skb);
1089 case RXRPC_PACKET_TYPE_ACKALL:
1090 rxrpc_input_ackall(call, skb);
1097 rxrpc_free_skb(skb, rxrpc_skb_freed);
1103 * Handle a new service call on a channel implicitly completing the preceding
1104 * call on that channel. This does not apply to client conns.
1106 * TODO: If callNumber > call_id + 1, renegotiate security.
1108 static void rxrpc_input_implicit_end_call(struct rxrpc_sock *rx,
1109 struct rxrpc_connection *conn,
1110 struct rxrpc_call *call)
1112 switch (READ_ONCE(call->state)) {
1113 case RXRPC_CALL_SERVER_AWAIT_ACK:
1114 rxrpc_call_completed(call);
1116 case RXRPC_CALL_COMPLETE:
1119 if (rxrpc_abort_call("IMP", call, 0, RX_CALL_DEAD, -ESHUTDOWN)) {
1120 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
1121 rxrpc_queue_call(call);
1123 trace_rxrpc_improper_term(call);
1127 spin_lock(&rx->incoming_lock);
1128 __rxrpc_disconnect_call(conn, call);
1129 spin_unlock(&rx->incoming_lock);
1133 * post connection-level events to the connection
1134 * - this includes challenges, responses, some aborts and call terminal packet
1137 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
1138 struct sk_buff *skb)
1140 _enter("%p,%p", conn, skb);
1142 skb_queue_tail(&conn->rx_queue, skb);
1143 rxrpc_queue_conn(conn);
1147 * post endpoint-level events to the local endpoint
1148 * - this includes debug and version messages
1150 static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
1151 struct sk_buff *skb)
1153 _enter("%p,%p", local, skb);
1155 if (rxrpc_get_local_maybe(local)) {
1156 skb_queue_tail(&local->event_queue, skb);
1157 rxrpc_queue_local(local);
1159 rxrpc_free_skb(skb, rxrpc_skb_freed);
1164 * put a packet up for transport-level abort
1166 static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
1168 if (rxrpc_get_local_maybe(local)) {
1169 skb_queue_tail(&local->reject_queue, skb);
1170 rxrpc_queue_local(local);
1172 rxrpc_free_skb(skb, rxrpc_skb_freed);
1177 * Extract the wire header from a packet and translate the byte order.
1180 int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb)
1182 struct rxrpc_wire_header whdr;
1184 /* dig out the RxRPC connection details */
1185 if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) {
1186 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
1187 tracepoint_string("bad_hdr"));
1191 memset(sp, 0, sizeof(*sp));
1192 sp->hdr.epoch = ntohl(whdr.epoch);
1193 sp->hdr.cid = ntohl(whdr.cid);
1194 sp->hdr.callNumber = ntohl(whdr.callNumber);
1195 sp->hdr.seq = ntohl(whdr.seq);
1196 sp->hdr.serial = ntohl(whdr.serial);
1197 sp->hdr.flags = whdr.flags;
1198 sp->hdr.type = whdr.type;
1199 sp->hdr.userStatus = whdr.userStatus;
1200 sp->hdr.securityIndex = whdr.securityIndex;
1201 sp->hdr._rsvd = ntohs(whdr._rsvd);
1202 sp->hdr.serviceId = ntohs(whdr.serviceId);
1207 * handle data received on the local endpoint
1208 * - may be called in interrupt context
1210 * [!] Note that as this is called from the encap_rcv hook, the socket is not
1211 * held locked by the caller and nothing prevents sk_user_data on the UDP from
1212 * being cleared in the middle of processing this function.
1214 * Called with the RCU read lock held from the IP layer via UDP.
1216 int rxrpc_input_packet(struct sock *udp_sk, struct sk_buff *skb)
1218 struct rxrpc_local *local = rcu_dereference_sk_user_data(udp_sk);
1219 struct rxrpc_connection *conn;
1220 struct rxrpc_channel *chan;
1221 struct rxrpc_call *call = NULL;
1222 struct rxrpc_skb_priv *sp;
1223 struct rxrpc_peer *peer = NULL;
1224 struct rxrpc_sock *rx = NULL;
1225 unsigned int channel;
1227 _enter("%p", udp_sk);
1229 if (unlikely(!local)) {
1233 if (skb->tstamp == 0)
1234 skb->tstamp = ktime_get_real();
1236 rxrpc_new_skb(skb, rxrpc_skb_received);
1238 skb_pull(skb, sizeof(struct udphdr));
1240 /* The UDP protocol already released all skb resources;
1241 * we are free to add our own data there.
1243 sp = rxrpc_skb(skb);
1245 /* dig out the RxRPC connection details */
1246 if (rxrpc_extract_header(sp, skb) < 0)
1249 if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) {
1251 if ((lose++ & 7) == 7) {
1252 trace_rxrpc_rx_lose(sp);
1253 rxrpc_free_skb(skb, rxrpc_skb_lost);
1258 if (skb->tstamp == 0)
1259 skb->tstamp = ktime_get_real();
1260 trace_rxrpc_rx_packet(sp);
1262 switch (sp->hdr.type) {
1263 case RXRPC_PACKET_TYPE_VERSION:
1264 if (rxrpc_to_client(sp))
1266 rxrpc_post_packet_to_local(local, skb);
1269 case RXRPC_PACKET_TYPE_BUSY:
1270 if (rxrpc_to_server(sp))
1273 case RXRPC_PACKET_TYPE_ACK:
1274 case RXRPC_PACKET_TYPE_ACKALL:
1275 if (sp->hdr.callNumber == 0)
1278 case RXRPC_PACKET_TYPE_ABORT:
1281 case RXRPC_PACKET_TYPE_DATA:
1282 if (sp->hdr.callNumber == 0 ||
1286 /* Unshare the packet so that it can be modified for in-place
1289 if (sp->hdr.securityIndex != 0) {
1290 struct sk_buff *nskb = skb_unshare(skb, GFP_ATOMIC);
1292 rxrpc_eaten_skb(skb, rxrpc_skb_unshared_nomem);
1297 rxrpc_eaten_skb(skb, rxrpc_skb_received);
1299 rxrpc_new_skb(skb, rxrpc_skb_unshared);
1300 sp = rxrpc_skb(skb);
1305 case RXRPC_PACKET_TYPE_CHALLENGE:
1306 if (rxrpc_to_server(sp))
1309 case RXRPC_PACKET_TYPE_RESPONSE:
1310 if (rxrpc_to_client(sp))
1314 /* Packet types 9-11 should just be ignored. */
1315 case RXRPC_PACKET_TYPE_PARAMS:
1316 case RXRPC_PACKET_TYPE_10:
1317 case RXRPC_PACKET_TYPE_11:
1321 _proto("Rx Bad Packet Type %u", sp->hdr.type);
1325 if (sp->hdr.serviceId == 0)
1328 if (rxrpc_to_server(sp)) {
1329 /* Weed out packets to services we're not offering. Packets
1330 * that would begin a call are explicitly rejected and the rest
1331 * are just discarded.
1333 rx = rcu_dereference(local->service);
1334 if (!rx || (sp->hdr.serviceId != rx->srx.srx_service &&
1335 sp->hdr.serviceId != rx->second_service)) {
1336 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
1338 goto unsupported_service;
1343 conn = rxrpc_find_connection_rcu(local, skb, &peer);
1345 if (sp->hdr.securityIndex != conn->security_ix)
1346 goto wrong_security;
1348 if (sp->hdr.serviceId != conn->service_id) {
1351 if (!test_bit(RXRPC_CONN_PROBING_FOR_UPGRADE, &conn->flags))
1353 old_id = cmpxchg(&conn->service_id, conn->params.service_id,
1356 if (old_id != conn->params.service_id &&
1357 old_id != sp->hdr.serviceId)
1361 if (sp->hdr.callNumber == 0) {
1362 /* Connection-level packet */
1363 _debug("CONN %p {%d}", conn, conn->debug_id);
1364 rxrpc_post_packet_to_conn(conn, skb);
1368 if ((int)sp->hdr.serial - (int)conn->hi_serial > 0)
1369 conn->hi_serial = sp->hdr.serial;
1371 /* Call-bound packets are routed by connection channel. */
1372 channel = sp->hdr.cid & RXRPC_CHANNELMASK;
1373 chan = &conn->channels[channel];
1375 /* Ignore really old calls */
1376 if (sp->hdr.callNumber < chan->last_call)
1379 if (sp->hdr.callNumber == chan->last_call) {
1381 sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)
1384 /* For the previous service call, if completed
1385 * successfully, we discard all further packets.
1387 if (rxrpc_conn_is_service(conn) &&
1388 chan->last_type == RXRPC_PACKET_TYPE_ACK)
1391 /* But otherwise we need to retransmit the final packet
1392 * from data cached in the connection record.
1394 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA)
1395 trace_rxrpc_rx_data(chan->call_debug_id,
1399 rxrpc_post_packet_to_conn(conn, skb);
1403 call = rcu_dereference(chan->call);
1405 if (sp->hdr.callNumber > chan->call_id) {
1406 if (rxrpc_to_client(sp))
1409 rxrpc_input_implicit_end_call(rx, conn, call);
1414 if (sp->hdr.serviceId != call->service_id)
1415 call->service_id = sp->hdr.serviceId;
1416 if ((int)sp->hdr.serial - (int)call->rx_serial > 0)
1417 call->rx_serial = sp->hdr.serial;
1418 if (!test_bit(RXRPC_CALL_RX_HEARD, &call->flags))
1419 set_bit(RXRPC_CALL_RX_HEARD, &call->flags);
1423 if (!call || refcount_read(&call->ref) == 0) {
1424 if (rxrpc_to_client(sp) ||
1425 sp->hdr.type != RXRPC_PACKET_TYPE_DATA)
1427 if (sp->hdr.seq != 1)
1429 call = rxrpc_new_incoming_call(local, rx, skb);
1434 /* Process a call packet; this either discards or passes on the ref
1437 rxrpc_input_call_packet(call, skb);
1441 rxrpc_free_skb(skb, rxrpc_skb_freed);
1443 trace_rxrpc_rx_done(0, 0);
1447 trace_rxrpc_abort(0, "SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1448 RXKADINCONSISTENCY, EBADMSG);
1449 skb->priority = RXKADINCONSISTENCY;
1452 unsupported_service:
1453 trace_rxrpc_abort(0, "INV", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1454 RX_INVALID_OPERATION, EOPNOTSUPP);
1455 skb->priority = RX_INVALID_OPERATION;
1459 trace_rxrpc_abort(0, "UPG", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1460 RX_PROTOCOL_ERROR, EBADMSG);
1461 goto protocol_error;
1464 trace_rxrpc_abort(0, "BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq,
1465 RX_PROTOCOL_ERROR, EBADMSG);
1467 skb->priority = RX_PROTOCOL_ERROR;
1469 skb->mark = RXRPC_SKB_MARK_REJECT_ABORT;
1471 trace_rxrpc_rx_done(skb->mark, skb->priority);
1472 rxrpc_reject_packet(local, skb);
1473 _leave(" [badmsg]");