1 /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/circ_buf.h>
14 #include <linux/net.h>
15 #include <linux/skbuff.h>
16 #include <linux/slab.h>
17 #include <linux/udp.h>
19 #include <net/af_rxrpc.h>
20 #include "ar-internal.h"
23 * How long to wait before scheduling ACK generation after seeing a
24 * packet with RXRPC_REQUEST_ACK set (in jiffies).
26 unsigned int rxrpc_requested_ack_delay
= 1;
29 * How long to wait before scheduling an ACK with subtype DELAY (in jiffies).
31 * We use this when we've received new data packets. If those packets aren't
32 * all consumed within this time we will send a DELAY ACK if an ACK was not
33 * requested to let the sender know it doesn't need to resend.
35 unsigned int rxrpc_soft_ack_delay
= 1 * HZ
;
38 * How long to wait before scheduling an ACK with subtype IDLE (in jiffies).
40 * We use this when we've consumed some previously soft-ACK'd packets when
41 * further packets aren't immediately received to decide when to send an IDLE
42 * ACK let the other end know that it can free up its Tx buffer space.
44 unsigned int rxrpc_idle_ack_delay
= 0.5 * HZ
;
47 * Receive window size in packets. This indicates the maximum number of
48 * unconsumed received packets we're willing to retain in memory. Once this
49 * limit is hit, we should generate an EXCEEDS_WINDOW ACK and discard further
52 unsigned int rxrpc_rx_window_size
= 32;
55 * Maximum Rx MTU size. This indicates to the sender the size of jumbo packet
56 * made by gluing normal packets together that we're willing to handle.
58 unsigned int rxrpc_rx_mtu
= 5692;
61 * The maximum number of fragments in a received jumbo packet that we tell the
62 * sender that we're willing to handle.
64 unsigned int rxrpc_rx_jumbo_max
= 4;
66 static const char *rxrpc_acks(u8 reason
)
68 static const char *const str
[] = {
69 "---", "REQ", "DUP", "OOS", "WIN", "MEM", "PNG", "PNR", "DLY",
73 if (reason
>= ARRAY_SIZE(str
))
74 reason
= ARRAY_SIZE(str
) - 1;
78 static const s8 rxrpc_ack_priority
[] = {
80 [RXRPC_ACK_DELAY
] = 1,
81 [RXRPC_ACK_REQUESTED
] = 2,
83 [RXRPC_ACK_PING_RESPONSE
] = 4,
84 [RXRPC_ACK_DUPLICATE
] = 5,
85 [RXRPC_ACK_OUT_OF_SEQUENCE
] = 6,
86 [RXRPC_ACK_EXCEEDS_WINDOW
] = 7,
87 [RXRPC_ACK_NOSPACE
] = 8,
91 * propose an ACK be sent
93 void __rxrpc_propose_ACK(struct rxrpc_call
*call
, u8 ack_reason
,
94 u32 serial
, bool immediate
)
97 s8 prior
= rxrpc_ack_priority
[ack_reason
];
99 ASSERTCMP(prior
, >, 0);
101 _enter("{%d},%s,%%%x,%u",
102 call
->debug_id
, rxrpc_acks(ack_reason
), serial
, immediate
);
104 if (prior
< rxrpc_ack_priority
[call
->ackr_reason
]) {
110 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
112 if (prior
== rxrpc_ack_priority
[call
->ackr_reason
]) {
114 call
->ackr_serial
= serial
;
120 call
->ackr_reason
= ack_reason
;
121 call
->ackr_serial
= serial
;
123 switch (ack_reason
) {
124 case RXRPC_ACK_DELAY
:
125 _debug("run delay timer");
126 expiry
= rxrpc_soft_ack_delay
;
131 _debug("run defer timer");
132 expiry
= rxrpc_idle_ack_delay
;
137 case RXRPC_ACK_REQUESTED
:
138 expiry
= rxrpc_requested_ack_delay
;
141 if (!immediate
|| serial
== 1) {
142 _debug("run defer timer");
147 _debug("immediate ACK");
153 if (!timer_pending(&call
->ack_timer
) ||
154 time_after(call
->ack_timer
.expires
, expiry
))
155 mod_timer(&call
->ack_timer
, expiry
);
159 _debug("cancel timer %%%u", serial
);
160 try_to_del_timer_sync(&call
->ack_timer
);
161 read_lock_bh(&call
->state_lock
);
162 if (call
->state
<= RXRPC_CALL_COMPLETE
&&
163 !test_and_set_bit(RXRPC_CALL_EV_ACK
, &call
->events
))
164 rxrpc_queue_call(call
);
165 read_unlock_bh(&call
->state_lock
);
169 * propose an ACK be sent, locking the call structure
171 void rxrpc_propose_ACK(struct rxrpc_call
*call
, u8 ack_reason
,
172 u32 serial
, bool immediate
)
174 s8 prior
= rxrpc_ack_priority
[ack_reason
];
176 if (prior
> rxrpc_ack_priority
[call
->ackr_reason
]) {
177 spin_lock_bh(&call
->lock
);
178 __rxrpc_propose_ACK(call
, ack_reason
, serial
, immediate
);
179 spin_unlock_bh(&call
->lock
);
184 * set the resend timer
186 static void rxrpc_set_resend(struct rxrpc_call
*call
, u8 resend
,
187 unsigned long resend_at
)
189 read_lock_bh(&call
->state_lock
);
190 if (call
->state
>= RXRPC_CALL_COMPLETE
)
194 _debug("SET RESEND");
195 set_bit(RXRPC_CALL_EV_RESEND
, &call
->events
);
199 _debug("MODIFY RESEND TIMER");
200 set_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
201 mod_timer(&call
->resend_timer
, resend_at
);
203 _debug("KILL RESEND TIMER");
204 del_timer_sync(&call
->resend_timer
);
205 clear_bit(RXRPC_CALL_EV_RESEND_TIMER
, &call
->events
);
206 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
208 read_unlock_bh(&call
->state_lock
);
214 static void rxrpc_resend(struct rxrpc_call
*call
)
216 struct rxrpc_wire_header
*whdr
;
217 struct rxrpc_skb_priv
*sp
;
219 unsigned long *p_txb
, resend_at
;
224 _enter("{%d,%d,%d,%d},",
225 call
->acks_hard
, call
->acks_unacked
,
226 atomic_read(&call
->sequence
),
227 CIRC_CNT(call
->acks_head
, call
->acks_tail
, call
->acks_winsz
));
233 for (loop
= call
->acks_tail
;
234 loop
!= call
->acks_head
|| stop
;
235 loop
= (loop
+ 1) & (call
->acks_winsz
- 1)
237 p_txb
= call
->acks_window
+ loop
;
238 smp_read_barrier_depends();
242 txb
= (struct sk_buff
*) *p_txb
;
245 if (sp
->need_resend
) {
246 sp
->need_resend
= false;
248 /* each Tx packet has a new serial number */
249 sp
->hdr
.serial
= atomic_inc_return(&call
->conn
->serial
);
251 whdr
= (struct rxrpc_wire_header
*)txb
->head
;
252 whdr
->serial
= htonl(sp
->hdr
.serial
);
254 _proto("Tx DATA %%%u { #%d }",
255 sp
->hdr
.serial
, sp
->hdr
.seq
);
256 if (rxrpc_send_packet(call
->conn
->trans
, txb
) < 0) {
258 sp
->resend_at
= jiffies
+ 3;
261 jiffies
+ rxrpc_resend_timeout
;
265 if (time_after_eq(jiffies
+ 1, sp
->resend_at
)) {
266 sp
->need_resend
= true;
268 } else if (resend
& 2) {
269 if (time_before(sp
->resend_at
, resend_at
))
270 resend_at
= sp
->resend_at
;
272 resend_at
= sp
->resend_at
;
277 rxrpc_set_resend(call
, resend
, resend_at
);
282 * handle resend timer expiry
284 static void rxrpc_resend_timer(struct rxrpc_call
*call
)
286 struct rxrpc_skb_priv
*sp
;
288 unsigned long *p_txb
, resend_at
;
293 call
->acks_tail
, call
->acks_unacked
, call
->acks_head
);
295 if (call
->state
>= RXRPC_CALL_COMPLETE
)
301 for (loop
= call
->acks_unacked
;
302 loop
!= call
->acks_head
;
303 loop
= (loop
+ 1) & (call
->acks_winsz
- 1)
305 p_txb
= call
->acks_window
+ loop
;
306 smp_read_barrier_depends();
307 txb
= (struct sk_buff
*) (*p_txb
& ~1);
310 ASSERT(!(*p_txb
& 1));
312 if (sp
->need_resend
) {
314 } else if (time_after_eq(jiffies
+ 1, sp
->resend_at
)) {
315 sp
->need_resend
= true;
317 } else if (resend
& 2) {
318 if (time_before(sp
->resend_at
, resend_at
))
319 resend_at
= sp
->resend_at
;
321 resend_at
= sp
->resend_at
;
326 rxrpc_set_resend(call
, resend
, resend_at
);
331 * process soft ACKs of our transmitted packets
332 * - these indicate packets the peer has or has not received, but hasn't yet
333 * given to the consumer, and so can still be discarded and re-requested
335 static int rxrpc_process_soft_ACKs(struct rxrpc_call
*call
,
336 struct rxrpc_ackpacket
*ack
,
339 struct rxrpc_skb_priv
*sp
;
341 unsigned long *p_txb
, resend_at
;
343 u8 sacks
[RXRPC_MAXACKS
], resend
;
345 _enter("{%d,%d},{%d},",
347 CIRC_CNT(call
->acks_head
, call
->acks_tail
, call
->acks_winsz
),
350 if (skb_copy_bits(skb
, 0, sacks
, ack
->nAcks
) < 0)
355 for (loop
= 0; loop
< ack
->nAcks
; loop
++) {
356 p_txb
= call
->acks_window
;
357 p_txb
+= (call
->acks_tail
+ loop
) & (call
->acks_winsz
- 1);
358 smp_read_barrier_depends();
359 txb
= (struct sk_buff
*) (*p_txb
& ~1);
362 switch (sacks
[loop
]) {
363 case RXRPC_ACK_TYPE_ACK
:
364 sp
->need_resend
= false;
367 case RXRPC_ACK_TYPE_NACK
:
368 sp
->need_resend
= true;
373 _debug("Unsupported ACK type %d", sacks
[loop
]);
379 call
->acks_unacked
= (call
->acks_tail
+ loop
) & (call
->acks_winsz
- 1);
381 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
382 * have been received or processed yet by the far end */
383 for (loop
= call
->acks_unacked
;
384 loop
!= call
->acks_head
;
385 loop
= (loop
+ 1) & (call
->acks_winsz
- 1)
387 p_txb
= call
->acks_window
+ loop
;
388 smp_read_barrier_depends();
389 txb
= (struct sk_buff
*) (*p_txb
& ~1);
393 /* packet must have been discarded */
394 sp
->need_resend
= true;
397 } else if (sp
->need_resend
) {
399 } else if (time_after_eq(jiffies
+ 1, sp
->resend_at
)) {
400 sp
->need_resend
= true;
402 } else if (resend
& 2) {
403 if (time_before(sp
->resend_at
, resend_at
))
404 resend_at
= sp
->resend_at
;
406 resend_at
= sp
->resend_at
;
411 rxrpc_set_resend(call
, resend
, resend_at
);
416 _leave(" = -EPROTO");
421 * discard hard-ACK'd packets from the Tx window
423 static void rxrpc_rotate_tx_window(struct rxrpc_call
*call
, u32 hard
)
426 int tail
= call
->acks_tail
, old_tail
;
427 int win
= CIRC_CNT(call
->acks_head
, tail
, call
->acks_winsz
);
429 kenter("{%u,%u},%u", call
->acks_hard
, win
, hard
);
431 ASSERTCMP(hard
- call
->acks_hard
, <=, win
);
433 while (call
->acks_hard
< hard
) {
434 smp_read_barrier_depends();
435 _skb
= call
->acks_window
[tail
] & ~1;
436 rxrpc_free_skb((struct sk_buff
*) _skb
);
438 tail
= (tail
+ 1) & (call
->acks_winsz
- 1);
439 call
->acks_tail
= tail
;
440 if (call
->acks_unacked
== old_tail
)
441 call
->acks_unacked
= tail
;
445 wake_up(&call
->tx_waitq
);
449 * clear the Tx window in the event of a failure
451 static void rxrpc_clear_tx_window(struct rxrpc_call
*call
)
453 rxrpc_rotate_tx_window(call
, atomic_read(&call
->sequence
));
457 * drain the out of sequence received packet queue into the packet Rx queue
459 static int rxrpc_drain_rx_oos_queue(struct rxrpc_call
*call
)
461 struct rxrpc_skb_priv
*sp
;
466 _enter("{%d,%d}", call
->rx_data_post
, call
->rx_first_oos
);
468 spin_lock_bh(&call
->lock
);
471 if (test_bit(RXRPC_CALL_RELEASED
, &call
->flags
))
472 goto socket_unavailable
;
474 skb
= skb_dequeue(&call
->rx_oos_queue
);
478 _debug("drain OOS packet %d [%d]",
479 sp
->hdr
.seq
, call
->rx_first_oos
);
481 if (sp
->hdr
.seq
!= call
->rx_first_oos
) {
482 skb_queue_head(&call
->rx_oos_queue
, skb
);
483 call
->rx_first_oos
= rxrpc_skb(skb
)->hdr
.seq
;
484 _debug("requeue %p {%u}", skb
, call
->rx_first_oos
);
486 skb
->mark
= RXRPC_SKB_MARK_DATA
;
487 terminal
= ((sp
->hdr
.flags
& RXRPC_LAST_PACKET
) &&
488 !(sp
->hdr
.flags
& RXRPC_CLIENT_INITIATED
));
489 ret
= rxrpc_queue_rcv_skb(call
, skb
, true, terminal
);
491 _debug("drain #%u", call
->rx_data_post
);
492 call
->rx_data_post
++;
494 /* find out what the next packet is */
495 skb
= skb_peek(&call
->rx_oos_queue
);
497 call
->rx_first_oos
= rxrpc_skb(skb
)->hdr
.seq
;
499 call
->rx_first_oos
= 0;
500 _debug("peek %p {%u}", skb
, call
->rx_first_oos
);
506 spin_unlock_bh(&call
->lock
);
507 _leave(" = %d", ret
);
512 * insert an out of sequence packet into the buffer
514 static void rxrpc_insert_oos_packet(struct rxrpc_call
*call
,
517 struct rxrpc_skb_priv
*sp
, *psp
;
523 _enter(",,{%u}", seq
);
525 skb
->destructor
= rxrpc_packet_destructor
;
526 ASSERTCMP(sp
->call
, ==, NULL
);
528 rxrpc_get_call(call
);
530 /* insert into the buffer in sequence order */
531 spin_lock_bh(&call
->lock
);
533 skb_queue_walk(&call
->rx_oos_queue
, p
) {
535 if (psp
->hdr
.seq
> seq
) {
536 _debug("insert oos #%u before #%u", seq
, psp
->hdr
.seq
);
537 skb_insert(p
, skb
, &call
->rx_oos_queue
);
542 _debug("append oos #%u", seq
);
543 skb_queue_tail(&call
->rx_oos_queue
, skb
);
546 /* we might now have a new front to the queue */
547 if (call
->rx_first_oos
== 0 || seq
< call
->rx_first_oos
)
548 call
->rx_first_oos
= seq
;
550 read_lock(&call
->state_lock
);
551 if (call
->state
< RXRPC_CALL_COMPLETE
&&
552 call
->rx_data_post
== call
->rx_first_oos
) {
553 _debug("drain rx oos now");
554 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS
, &call
->events
);
556 read_unlock(&call
->state_lock
);
558 spin_unlock_bh(&call
->lock
);
559 _leave(" [stored #%u]", call
->rx_first_oos
);
563 * clear the Tx window on final ACK reception
565 static void rxrpc_zap_tx_window(struct rxrpc_call
*call
)
567 struct rxrpc_skb_priv
*sp
;
569 unsigned long _skb
, *acks_window
;
570 u8 winsz
= call
->acks_winsz
;
573 acks_window
= call
->acks_window
;
574 call
->acks_window
= NULL
;
576 while (CIRC_CNT(call
->acks_head
, call
->acks_tail
, winsz
) > 0) {
577 tail
= call
->acks_tail
;
578 smp_read_barrier_depends();
579 _skb
= acks_window
[tail
] & ~1;
581 call
->acks_tail
= (call
->acks_tail
+ 1) & (winsz
- 1);
583 skb
= (struct sk_buff
*) _skb
;
585 _debug("+++ clear Tx %u", sp
->hdr
.seq
);
593 * process the extra information that may be appended to an ACK packet
595 static void rxrpc_extract_ackinfo(struct rxrpc_call
*call
, struct sk_buff
*skb
,
596 unsigned int latest
, int nAcks
)
598 struct rxrpc_ackinfo ackinfo
;
599 struct rxrpc_peer
*peer
;
602 if (skb_copy_bits(skb
, nAcks
+ 3, &ackinfo
, sizeof(ackinfo
)) < 0) {
603 _leave(" [no ackinfo]");
607 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
609 ntohl(ackinfo
.rxMTU
), ntohl(ackinfo
.maxMTU
),
610 ntohl(ackinfo
.rwind
), ntohl(ackinfo
.jumbo_max
));
612 mtu
= min(ntohl(ackinfo
.rxMTU
), ntohl(ackinfo
.maxMTU
));
614 peer
= call
->conn
->trans
->peer
;
615 if (mtu
< peer
->maxdata
) {
616 spin_lock_bh(&peer
->lock
);
618 peer
->mtu
= mtu
+ peer
->hdrsize
;
619 spin_unlock_bh(&peer
->lock
);
620 _net("Net MTU %u (maxdata %u)", peer
->mtu
, peer
->maxdata
);
625 * process packets in the reception queue
627 static int rxrpc_process_rx_queue(struct rxrpc_call
*call
,
630 struct rxrpc_ackpacket ack
;
631 struct rxrpc_skb_priv
*sp
;
640 skb
= skb_dequeue(&call
->rx_queue
);
644 _net("deferred skb %p", skb
);
648 _debug("process %s [st %d]", rxrpc_pkts
[sp
->hdr
.type
], call
->state
);
652 switch (sp
->hdr
.type
) {
653 /* data packets that wind up here have been received out of
654 * order, need security processing or are jumbo packets */
655 case RXRPC_PACKET_TYPE_DATA
:
656 _proto("OOSQ DATA %%%u { #%u }", sp
->hdr
.serial
, sp
->hdr
.seq
);
658 /* secured packets must be verified and possibly decrypted */
659 if (rxrpc_verify_packet(call
, skb
, _abort_code
) < 0)
662 rxrpc_insert_oos_packet(call
, skb
);
663 goto process_further
;
665 /* partial ACK to process */
666 case RXRPC_PACKET_TYPE_ACK
:
667 if (skb_copy_bits(skb
, 0, &ack
, sizeof(ack
)) < 0) {
668 _debug("extraction failure");
671 if (!skb_pull(skb
, sizeof(ack
)))
674 latest
= sp
->hdr
.serial
;
675 hard
= ntohl(ack
.firstPacket
);
676 tx
= atomic_read(&call
->sequence
);
678 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
682 ntohl(ack
.previousPacket
),
684 rxrpc_acks(ack
.reason
),
687 rxrpc_extract_ackinfo(call
, skb
, latest
, ack
.nAcks
);
689 if (ack
.reason
== RXRPC_ACK_PING
) {
690 _proto("Rx ACK %%%u PING Request", latest
);
691 rxrpc_propose_ACK(call
, RXRPC_ACK_PING_RESPONSE
,
692 sp
->hdr
.serial
, true);
695 /* discard any out-of-order or duplicate ACKs */
696 if (latest
- call
->acks_latest
<= 0) {
697 _debug("discard ACK %d <= %d",
698 latest
, call
->acks_latest
);
701 call
->acks_latest
= latest
;
703 if (call
->state
!= RXRPC_CALL_CLIENT_SEND_REQUEST
&&
704 call
->state
!= RXRPC_CALL_CLIENT_AWAIT_REPLY
&&
705 call
->state
!= RXRPC_CALL_SERVER_SEND_REPLY
&&
706 call
->state
!= RXRPC_CALL_SERVER_AWAIT_ACK
)
709 _debug("Tx=%d H=%u S=%d", tx
, call
->acks_hard
, call
->state
);
713 _debug("hard-ACK'd packet %d not transmitted"
719 if ((call
->state
== RXRPC_CALL_CLIENT_AWAIT_REPLY
||
720 call
->state
== RXRPC_CALL_SERVER_AWAIT_ACK
) &&
722 call
->acks_hard
= tx
;
727 rxrpc_rotate_tx_window(call
, hard
- 1);
731 if (hard
- 1 + ack
.nAcks
> tx
) {
732 _debug("soft-ACK'd packet %d+%d not"
733 " transmitted (%d top)",
734 hard
- 1, ack
.nAcks
, tx
);
738 if (rxrpc_process_soft_ACKs(call
, &ack
, skb
) < 0)
743 /* complete ACK to process */
744 case RXRPC_PACKET_TYPE_ACKALL
:
747 /* abort and busy are handled elsewhere */
748 case RXRPC_PACKET_TYPE_BUSY
:
749 case RXRPC_PACKET_TYPE_ABORT
:
752 /* connection level events - also handled elsewhere */
753 case RXRPC_PACKET_TYPE_CHALLENGE
:
754 case RXRPC_PACKET_TYPE_RESPONSE
:
755 case RXRPC_PACKET_TYPE_DEBUG
:
759 /* if we've had a hard ACK that covers all the packets we've sent, then
760 * that ends that phase of the operation */
762 write_lock_bh(&call
->state_lock
);
763 _debug("ack all %d", call
->state
);
765 switch (call
->state
) {
766 case RXRPC_CALL_CLIENT_AWAIT_REPLY
:
767 call
->state
= RXRPC_CALL_CLIENT_RECV_REPLY
;
769 case RXRPC_CALL_SERVER_AWAIT_ACK
:
770 _debug("srv complete");
771 call
->state
= RXRPC_CALL_COMPLETE
;
774 case RXRPC_CALL_CLIENT_SEND_REQUEST
:
775 case RXRPC_CALL_SERVER_RECV_REQUEST
:
776 goto protocol_error_unlock
; /* can't occur yet */
778 write_unlock_bh(&call
->state_lock
);
779 goto discard
; /* assume packet left over from earlier phase */
782 write_unlock_bh(&call
->state_lock
);
784 /* if all the packets we sent are hard-ACK'd, then we can discard
785 * whatever we've got left */
786 _debug("clear Tx %d",
787 CIRC_CNT(call
->acks_head
, call
->acks_tail
, call
->acks_winsz
));
789 del_timer_sync(&call
->resend_timer
);
790 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
791 clear_bit(RXRPC_CALL_EV_RESEND_TIMER
, &call
->events
);
793 if (call
->acks_window
)
794 rxrpc_zap_tx_window(call
);
797 /* post the final ACK message for userspace to pick up */
799 skb
->mark
= RXRPC_SKB_MARK_FINAL_ACK
;
801 rxrpc_get_call(call
);
802 spin_lock_bh(&call
->lock
);
803 if (rxrpc_queue_rcv_skb(call
, skb
, true, true) < 0)
805 spin_unlock_bh(&call
->lock
);
806 goto process_further
;
811 goto process_further
;
813 protocol_error_unlock
:
814 write_unlock_bh(&call
->state_lock
);
817 _leave(" = -EPROTO");
822 * post a message to the socket Rx queue for recvmsg() to pick up
824 static int rxrpc_post_message(struct rxrpc_call
*call
, u32 mark
, u32 error
,
827 struct rxrpc_skb_priv
*sp
;
831 _enter("{%d,%lx},%u,%u,%d",
832 call
->debug_id
, call
->flags
, mark
, error
, fatal
);
834 /* remove timers and things for fatal messages */
836 del_timer_sync(&call
->resend_timer
);
837 del_timer_sync(&call
->ack_timer
);
838 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
841 if (mark
!= RXRPC_SKB_MARK_NEW_CALL
&&
842 !test_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
)) {
843 _leave("[no userid]");
847 if (!test_bit(RXRPC_CALL_TERMINAL_MSG
, &call
->flags
)) {
848 skb
= alloc_skb(0, GFP_NOFS
);
857 memset(sp
, 0, sizeof(*sp
));
860 rxrpc_get_call(call
);
862 spin_lock_bh(&call
->lock
);
863 ret
= rxrpc_queue_rcv_skb(call
, skb
, true, fatal
);
864 spin_unlock_bh(&call
->lock
);
872 * handle background processing of incoming call packets and ACK / abort
875 void rxrpc_process_call(struct work_struct
*work
)
877 struct rxrpc_call
*call
=
878 container_of(work
, struct rxrpc_call
, processor
);
879 struct rxrpc_wire_header whdr
;
880 struct rxrpc_ackpacket ack
;
881 struct rxrpc_ackinfo ackinfo
;
884 enum rxrpc_call_event genbit
;
888 int loop
, nbit
, ioc
, ret
, mtu
;
889 u32 serial
, abort_code
= RX_PROTOCOL_ERROR
;
892 //printk("\n--------------------\n");
893 _enter("{%d,%s,%lx} [%lu]",
894 call
->debug_id
, rxrpc_call_states
[call
->state
], call
->events
,
895 (jiffies
- call
->creation_jif
) / (HZ
/ 10));
897 if (test_and_set_bit(RXRPC_CALL_PROC_BUSY
, &call
->flags
)) {
898 _debug("XXXXXXXXXXXXX RUNNING ON MULTIPLE CPUS XXXXXXXXXXXXX");
902 /* there's a good chance we're going to have to send a message, so set
903 * one up in advance */
904 msg
.msg_name
= &call
->conn
->trans
->peer
->srx
.transport
.sin
;
905 msg
.msg_namelen
= sizeof(call
->conn
->trans
->peer
->srx
.transport
.sin
);
906 msg
.msg_control
= NULL
;
907 msg
.msg_controllen
= 0;
910 whdr
.epoch
= htonl(call
->conn
->epoch
);
911 whdr
.cid
= htonl(call
->cid
);
912 whdr
.callNumber
= htonl(call
->call_id
);
914 whdr
.type
= RXRPC_PACKET_TYPE_ACK
;
915 whdr
.flags
= call
->conn
->out_clientflag
;
917 whdr
.securityIndex
= call
->conn
->security_ix
;
919 whdr
.serviceId
= htons(call
->service_id
);
921 memset(iov
, 0, sizeof(iov
));
922 iov
[0].iov_base
= &whdr
;
923 iov
[0].iov_len
= sizeof(whdr
);
925 /* deal with events of a final nature */
926 if (test_bit(RXRPC_CALL_EV_RELEASE
, &call
->events
)) {
927 rxrpc_release_call(call
);
928 clear_bit(RXRPC_CALL_EV_RELEASE
, &call
->events
);
931 if (test_bit(RXRPC_CALL_EV_RCVD_ERROR
, &call
->events
)) {
934 clear_bit(RXRPC_CALL_EV_CONN_ABORT
, &call
->events
);
935 clear_bit(RXRPC_CALL_EV_REJECT_BUSY
, &call
->events
);
936 clear_bit(RXRPC_CALL_EV_ABORT
, &call
->events
);
938 error
= call
->conn
->trans
->peer
->net_error
;
939 _debug("post net error %d", error
);
941 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_NET_ERROR
,
944 clear_bit(RXRPC_CALL_EV_RCVD_ERROR
, &call
->events
);
948 if (test_bit(RXRPC_CALL_EV_CONN_ABORT
, &call
->events
)) {
949 ASSERTCMP(call
->state
, >, RXRPC_CALL_COMPLETE
);
951 clear_bit(RXRPC_CALL_EV_REJECT_BUSY
, &call
->events
);
952 clear_bit(RXRPC_CALL_EV_ABORT
, &call
->events
);
954 _debug("post conn abort");
956 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_LOCAL_ERROR
,
957 call
->conn
->error
, true) < 0)
959 clear_bit(RXRPC_CALL_EV_CONN_ABORT
, &call
->events
);
963 if (test_bit(RXRPC_CALL_EV_REJECT_BUSY
, &call
->events
)) {
964 whdr
.type
= RXRPC_PACKET_TYPE_BUSY
;
965 genbit
= RXRPC_CALL_EV_REJECT_BUSY
;
969 if (test_bit(RXRPC_CALL_EV_ABORT
, &call
->events
)) {
970 ASSERTCMP(call
->state
, >, RXRPC_CALL_COMPLETE
);
972 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_LOCAL_ERROR
,
973 ECONNABORTED
, true) < 0)
975 whdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
976 data
= htonl(call
->abort_code
);
977 iov
[1].iov_base
= &data
;
978 iov
[1].iov_len
= sizeof(data
);
979 genbit
= RXRPC_CALL_EV_ABORT
;
983 if (test_bit(RXRPC_CALL_EV_ACK_FINAL
, &call
->events
)) {
984 genbit
= RXRPC_CALL_EV_ACK_FINAL
;
986 ack
.bufferSpace
= htons(8);
989 ack
.reason
= RXRPC_ACK_IDLE
;
991 call
->ackr_reason
= 0;
993 spin_lock_bh(&call
->lock
);
994 ack
.serial
= htonl(call
->ackr_serial
);
995 ack
.previousPacket
= htonl(call
->ackr_prev_seq
);
996 ack
.firstPacket
= htonl(call
->rx_data_eaten
+ 1);
997 spin_unlock_bh(&call
->lock
);
1001 iov
[1].iov_base
= &ack
;
1002 iov
[1].iov_len
= sizeof(ack
);
1003 iov
[2].iov_base
= &pad
;
1005 iov
[3].iov_base
= &ackinfo
;
1006 iov
[3].iov_len
= sizeof(ackinfo
);
1010 if (call
->events
& ((1 << RXRPC_CALL_EV_RCVD_BUSY
) |
1011 (1 << RXRPC_CALL_EV_RCVD_ABORT
))
1015 if (test_bit(RXRPC_CALL_EV_RCVD_ABORT
, &call
->events
))
1016 mark
= RXRPC_SKB_MARK_REMOTE_ABORT
;
1018 mark
= RXRPC_SKB_MARK_BUSY
;
1020 _debug("post abort/busy");
1021 rxrpc_clear_tx_window(call
);
1022 if (rxrpc_post_message(call
, mark
, ECONNABORTED
, true) < 0)
1025 clear_bit(RXRPC_CALL_EV_RCVD_BUSY
, &call
->events
);
1026 clear_bit(RXRPC_CALL_EV_RCVD_ABORT
, &call
->events
);
1030 if (test_and_clear_bit(RXRPC_CALL_EV_RCVD_ACKALL
, &call
->events
)) {
1031 _debug("do implicit ackall");
1032 rxrpc_clear_tx_window(call
);
1035 if (test_bit(RXRPC_CALL_EV_LIFE_TIMER
, &call
->events
)) {
1036 write_lock_bh(&call
->state_lock
);
1037 if (call
->state
<= RXRPC_CALL_COMPLETE
) {
1038 call
->state
= RXRPC_CALL_LOCALLY_ABORTED
;
1039 call
->abort_code
= RX_CALL_TIMEOUT
;
1040 set_bit(RXRPC_CALL_EV_ABORT
, &call
->events
);
1042 write_unlock_bh(&call
->state_lock
);
1044 _debug("post timeout");
1045 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_LOCAL_ERROR
,
1049 clear_bit(RXRPC_CALL_EV_LIFE_TIMER
, &call
->events
);
1053 /* deal with assorted inbound messages */
1054 if (!skb_queue_empty(&call
->rx_queue
)) {
1055 switch (rxrpc_process_rx_queue(call
, &abort_code
)) {
1064 rxrpc_abort_call(call
, abort_code
);
1069 /* handle resending */
1070 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND_TIMER
, &call
->events
))
1071 rxrpc_resend_timer(call
);
1072 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND
, &call
->events
))
1075 /* consider sending an ordinary ACK */
1076 if (test_bit(RXRPC_CALL_EV_ACK
, &call
->events
)) {
1077 _debug("send ACK: window: %d - %d { %lx }",
1078 call
->rx_data_eaten
, call
->ackr_win_top
,
1079 call
->ackr_window
[0]);
1081 if (call
->state
> RXRPC_CALL_SERVER_ACK_REQUEST
&&
1082 call
->ackr_reason
!= RXRPC_ACK_PING_RESPONSE
) {
1083 /* ACK by sending reply DATA packet in this state */
1084 clear_bit(RXRPC_CALL_EV_ACK
, &call
->events
);
1085 goto maybe_reschedule
;
1088 genbit
= RXRPC_CALL_EV_ACK
;
1090 acks
= kzalloc(call
->ackr_win_top
- call
->rx_data_eaten
,
1095 //hdr.flags = RXRPC_SLOW_START_OK;
1096 ack
.bufferSpace
= htons(8);
1099 spin_lock_bh(&call
->lock
);
1100 ack
.reason
= call
->ackr_reason
;
1101 ack
.serial
= htonl(call
->ackr_serial
);
1102 ack
.previousPacket
= htonl(call
->ackr_prev_seq
);
1103 ack
.firstPacket
= htonl(call
->rx_data_eaten
+ 1);
1106 for (loop
= 0; loop
< RXRPC_ACKR_WINDOW_ASZ
; loop
++) {
1107 nbit
= loop
* BITS_PER_LONG
;
1108 for (bits
= call
->ackr_window
[loop
]; bits
; bits
>>= 1
1110 _debug("- l=%d n=%d b=%lx", loop
, nbit
, bits
);
1112 acks
[nbit
] = RXRPC_ACK_TYPE_ACK
;
1113 ack
.nAcks
= nbit
+ 1;
1118 call
->ackr_reason
= 0;
1119 spin_unlock_bh(&call
->lock
);
1123 iov
[1].iov_base
= &ack
;
1124 iov
[1].iov_len
= sizeof(ack
);
1125 iov
[2].iov_base
= acks
;
1126 iov
[2].iov_len
= ack
.nAcks
;
1127 iov
[3].iov_base
= &pad
;
1129 iov
[4].iov_base
= &ackinfo
;
1130 iov
[4].iov_len
= sizeof(ackinfo
);
1132 switch (ack
.reason
) {
1133 case RXRPC_ACK_REQUESTED
:
1134 case RXRPC_ACK_DUPLICATE
:
1135 case RXRPC_ACK_OUT_OF_SEQUENCE
:
1136 case RXRPC_ACK_EXCEEDS_WINDOW
:
1137 case RXRPC_ACK_NOSPACE
:
1138 case RXRPC_ACK_PING
:
1139 case RXRPC_ACK_PING_RESPONSE
:
1140 goto send_ACK_with_skew
;
1141 case RXRPC_ACK_DELAY
:
1142 case RXRPC_ACK_IDLE
:
1147 /* handle completion of security negotiations on an incoming
1149 if (test_and_clear_bit(RXRPC_CALL_EV_SECURED
, &call
->events
)) {
1151 spin_lock_bh(&call
->lock
);
1153 if (call
->state
== RXRPC_CALL_SERVER_SECURING
) {
1155 write_lock(&call
->conn
->lock
);
1156 if (!test_bit(RXRPC_CALL_RELEASED
, &call
->flags
) &&
1157 !test_bit(RXRPC_CALL_EV_RELEASE
, &call
->events
)) {
1158 _debug("not released");
1159 call
->state
= RXRPC_CALL_SERVER_ACCEPTING
;
1160 list_move_tail(&call
->accept_link
,
1161 &call
->socket
->acceptq
);
1163 write_unlock(&call
->conn
->lock
);
1164 read_lock(&call
->state_lock
);
1165 if (call
->state
< RXRPC_CALL_COMPLETE
)
1166 set_bit(RXRPC_CALL_EV_POST_ACCEPT
, &call
->events
);
1167 read_unlock(&call
->state_lock
);
1170 spin_unlock_bh(&call
->lock
);
1171 if (!test_bit(RXRPC_CALL_EV_POST_ACCEPT
, &call
->events
))
1172 goto maybe_reschedule
;
1175 /* post a notification of an acceptable connection to the app */
1176 if (test_bit(RXRPC_CALL_EV_POST_ACCEPT
, &call
->events
)) {
1177 _debug("post accept");
1178 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_NEW_CALL
,
1181 clear_bit(RXRPC_CALL_EV_POST_ACCEPT
, &call
->events
);
1182 goto maybe_reschedule
;
1185 /* handle incoming call acceptance */
1186 if (test_and_clear_bit(RXRPC_CALL_EV_ACCEPTED
, &call
->events
)) {
1188 ASSERTCMP(call
->rx_data_post
, ==, 0);
1189 call
->rx_data_post
= 1;
1190 read_lock_bh(&call
->state_lock
);
1191 if (call
->state
< RXRPC_CALL_COMPLETE
)
1192 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS
, &call
->events
);
1193 read_unlock_bh(&call
->state_lock
);
1196 /* drain the out of sequence received packet queue into the packet Rx
1198 if (test_and_clear_bit(RXRPC_CALL_EV_DRAIN_RX_OOS
, &call
->events
)) {
1199 while (call
->rx_data_post
== call
->rx_first_oos
)
1200 if (rxrpc_drain_rx_oos_queue(call
) < 0)
1202 goto maybe_reschedule
;
1205 /* other events may have been raised since we started checking */
1206 goto maybe_reschedule
;
1209 ack
.maxSkew
= htons(atomic_read(&call
->conn
->hi_serial
) -
1212 mtu
= call
->conn
->trans
->peer
->if_mtu
;
1213 mtu
-= call
->conn
->trans
->peer
->hdrsize
;
1214 ackinfo
.maxMTU
= htonl(mtu
);
1215 ackinfo
.rwind
= htonl(rxrpc_rx_window_size
);
1217 /* permit the peer to send us jumbo packets if it wants to */
1218 ackinfo
.rxMTU
= htonl(rxrpc_rx_mtu
);
1219 ackinfo
.jumbo_max
= htonl(rxrpc_rx_jumbo_max
);
1221 serial
= atomic_inc_return(&call
->conn
->serial
);
1222 whdr
.serial
= htonl(serial
);
1223 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
1226 ntohl(ack
.firstPacket
),
1227 ntohl(ack
.previousPacket
),
1229 rxrpc_acks(ack
.reason
),
1232 del_timer_sync(&call
->ack_timer
);
1234 set_bit(RXRPC_CALL_TX_SOFT_ACK
, &call
->flags
);
1235 goto send_message_2
;
1238 _debug("send message");
1240 serial
= atomic_inc_return(&call
->conn
->serial
);
1241 whdr
.serial
= htonl(serial
);
1242 _proto("Tx %s %%%u", rxrpc_pkts
[whdr
.type
], serial
);
1245 len
= iov
[0].iov_len
;
1247 if (iov
[4].iov_len
) {
1249 len
+= iov
[4].iov_len
;
1250 len
+= iov
[3].iov_len
;
1251 len
+= iov
[2].iov_len
;
1252 len
+= iov
[1].iov_len
;
1253 } else if (iov
[3].iov_len
) {
1255 len
+= iov
[3].iov_len
;
1256 len
+= iov
[2].iov_len
;
1257 len
+= iov
[1].iov_len
;
1258 } else if (iov
[2].iov_len
) {
1260 len
+= iov
[2].iov_len
;
1261 len
+= iov
[1].iov_len
;
1262 } else if (iov
[1].iov_len
) {
1264 len
+= iov
[1].iov_len
;
1267 ret
= kernel_sendmsg(call
->conn
->trans
->local
->socket
,
1268 &msg
, iov
, ioc
, len
);
1270 _debug("sendmsg failed: %d", ret
);
1271 read_lock_bh(&call
->state_lock
);
1272 if (call
->state
< RXRPC_CALL_DEAD
)
1273 rxrpc_queue_call(call
);
1274 read_unlock_bh(&call
->state_lock
);
1279 case RXRPC_CALL_EV_ABORT
:
1280 clear_bit(genbit
, &call
->events
);
1281 clear_bit(RXRPC_CALL_EV_RCVD_ABORT
, &call
->events
);
1284 case RXRPC_CALL_EV_ACK_FINAL
:
1285 write_lock_bh(&call
->state_lock
);
1286 if (call
->state
== RXRPC_CALL_CLIENT_FINAL_ACK
)
1287 call
->state
= RXRPC_CALL_COMPLETE
;
1288 write_unlock_bh(&call
->state_lock
);
1292 clear_bit(genbit
, &call
->events
);
1293 switch (call
->state
) {
1294 case RXRPC_CALL_CLIENT_AWAIT_REPLY
:
1295 case RXRPC_CALL_CLIENT_RECV_REPLY
:
1296 case RXRPC_CALL_SERVER_RECV_REQUEST
:
1297 case RXRPC_CALL_SERVER_ACK_REQUEST
:
1298 _debug("start ACK timer");
1299 rxrpc_propose_ACK(call
, RXRPC_ACK_DELAY
,
1300 call
->ackr_serial
, false);
1304 goto maybe_reschedule
;
1308 del_timer_sync(&call
->ack_timer
);
1309 if (test_and_clear_bit(RXRPC_CALL_EV_ACK_FINAL
, &call
->events
))
1310 rxrpc_put_call(call
);
1311 clear_bit(RXRPC_CALL_EV_ACK
, &call
->events
);
1314 if (call
->events
|| !skb_queue_empty(&call
->rx_queue
)) {
1315 read_lock_bh(&call
->state_lock
);
1316 if (call
->state
< RXRPC_CALL_DEAD
)
1317 rxrpc_queue_call(call
);
1318 read_unlock_bh(&call
->state_lock
);
1321 /* don't leave aborted connections on the accept queue */
1322 if (call
->state
>= RXRPC_CALL_COMPLETE
&&
1323 !list_empty(&call
->accept_link
)) {
1324 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
1325 call
, call
->events
, call
->flags
, call
->conn
->cid
);
1327 read_lock_bh(&call
->state_lock
);
1328 if (!test_bit(RXRPC_CALL_RELEASED
, &call
->flags
) &&
1329 !test_and_set_bit(RXRPC_CALL_EV_RELEASE
, &call
->events
))
1330 rxrpc_queue_call(call
);
1331 read_unlock_bh(&call
->state_lock
);
1335 clear_bit(RXRPC_CALL_PROC_BUSY
, &call
->flags
);
1338 /* because we don't want two CPUs both processing the work item for one
1339 * call at the same time, we use a flag to note when it's busy; however
1340 * this means there's a race between clearing the flag and setting the
1341 * work pending bit and the work item being processed again */
1342 if (call
->events
&& !work_pending(&call
->processor
)) {
1343 _debug("jumpstart %x", call
->conn
->cid
);
1344 rxrpc_queue_call(call
);
1351 _debug("out of memory");
1352 goto maybe_reschedule
;