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"
22 static unsigned int rxrpc_ack_defer
= 1;
24 static const char *rxrpc_acks(u8 reason
)
26 static const char *const str
[] = {
27 "---", "REQ", "DUP", "OOS", "WIN", "MEM", "PNG", "PNR", "DLY",
31 if (reason
>= ARRAY_SIZE(str
))
32 reason
= ARRAY_SIZE(str
) - 1;
36 static const s8 rxrpc_ack_priority
[] = {
38 [RXRPC_ACK_DELAY
] = 1,
39 [RXRPC_ACK_REQUESTED
] = 2,
41 [RXRPC_ACK_PING_RESPONSE
] = 4,
42 [RXRPC_ACK_DUPLICATE
] = 5,
43 [RXRPC_ACK_OUT_OF_SEQUENCE
] = 6,
44 [RXRPC_ACK_EXCEEDS_WINDOW
] = 7,
45 [RXRPC_ACK_NOSPACE
] = 8,
49 * propose an ACK be sent
51 void __rxrpc_propose_ACK(struct rxrpc_call
*call
, u8 ack_reason
,
52 __be32 serial
, bool immediate
)
55 s8 prior
= rxrpc_ack_priority
[ack_reason
];
57 ASSERTCMP(prior
, >, 0);
59 _enter("{%d},%s,%%%x,%u",
60 call
->debug_id
, rxrpc_acks(ack_reason
), ntohl(serial
),
63 if (prior
< rxrpc_ack_priority
[call
->ackr_reason
]) {
69 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
71 if (prior
== rxrpc_ack_priority
[call
->ackr_reason
]) {
73 call
->ackr_serial
= serial
;
79 call
->ackr_reason
= ack_reason
;
80 call
->ackr_serial
= serial
;
84 _debug("run delay timer");
85 call
->ack_timer
.expires
= jiffies
+ rxrpc_ack_timeout
* HZ
;
86 add_timer(&call
->ack_timer
);
91 _debug("run defer timer");
97 case RXRPC_ACK_REQUESTED
:
100 if (!immediate
|| serial
== cpu_to_be32(1)) {
101 _debug("run defer timer");
102 expiry
= rxrpc_ack_defer
;
107 _debug("immediate ACK");
113 if (!timer_pending(&call
->ack_timer
) ||
114 time_after(call
->ack_timer
.expires
, expiry
))
115 mod_timer(&call
->ack_timer
, expiry
);
119 _debug("cancel timer %%%u", ntohl(serial
));
120 try_to_del_timer_sync(&call
->ack_timer
);
121 read_lock_bh(&call
->state_lock
);
122 if (call
->state
<= RXRPC_CALL_COMPLETE
&&
123 !test_and_set_bit(RXRPC_CALL_ACK
, &call
->events
))
124 rxrpc_queue_call(call
);
125 read_unlock_bh(&call
->state_lock
);
129 * propose an ACK be sent, locking the call structure
131 void rxrpc_propose_ACK(struct rxrpc_call
*call
, u8 ack_reason
,
132 __be32 serial
, bool immediate
)
134 s8 prior
= rxrpc_ack_priority
[ack_reason
];
136 if (prior
> rxrpc_ack_priority
[call
->ackr_reason
]) {
137 spin_lock_bh(&call
->lock
);
138 __rxrpc_propose_ACK(call
, ack_reason
, serial
, immediate
);
139 spin_unlock_bh(&call
->lock
);
144 * set the resend timer
146 static void rxrpc_set_resend(struct rxrpc_call
*call
, u8 resend
,
147 unsigned long resend_at
)
149 read_lock_bh(&call
->state_lock
);
150 if (call
->state
>= RXRPC_CALL_COMPLETE
)
154 _debug("SET RESEND");
155 set_bit(RXRPC_CALL_RESEND
, &call
->events
);
159 _debug("MODIFY RESEND TIMER");
160 set_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
161 mod_timer(&call
->resend_timer
, resend_at
);
163 _debug("KILL RESEND TIMER");
164 del_timer_sync(&call
->resend_timer
);
165 clear_bit(RXRPC_CALL_RESEND_TIMER
, &call
->events
);
166 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
168 read_unlock_bh(&call
->state_lock
);
174 static void rxrpc_resend(struct rxrpc_call
*call
)
176 struct rxrpc_skb_priv
*sp
;
177 struct rxrpc_header
*hdr
;
179 unsigned long *p_txb
, resend_at
;
183 _enter("{%d,%d,%d,%d},",
184 call
->acks_hard
, call
->acks_unacked
,
185 atomic_read(&call
->sequence
),
186 CIRC_CNT(call
->acks_head
, call
->acks_tail
, call
->acks_winsz
));
192 for (loop
= call
->acks_tail
;
193 loop
!= call
->acks_head
|| stop
;
194 loop
= (loop
+ 1) & (call
->acks_winsz
- 1)
196 p_txb
= call
->acks_window
+ loop
;
197 smp_read_barrier_depends();
201 txb
= (struct sk_buff
*) *p_txb
;
204 if (sp
->need_resend
) {
205 sp
->need_resend
= false;
207 /* each Tx packet has a new serial number */
209 htonl(atomic_inc_return(&call
->conn
->serial
));
211 hdr
= (struct rxrpc_header
*) txb
->head
;
212 hdr
->serial
= sp
->hdr
.serial
;
214 _proto("Tx DATA %%%u { #%d }",
215 ntohl(sp
->hdr
.serial
), ntohl(sp
->hdr
.seq
));
216 if (rxrpc_send_packet(call
->conn
->trans
, txb
) < 0) {
218 sp
->resend_at
= jiffies
+ 3;
221 jiffies
+ rxrpc_resend_timeout
* HZ
;
225 if (time_after_eq(jiffies
+ 1, sp
->resend_at
)) {
226 sp
->need_resend
= true;
228 } else if (resend
& 2) {
229 if (time_before(sp
->resend_at
, resend_at
))
230 resend_at
= sp
->resend_at
;
232 resend_at
= sp
->resend_at
;
237 rxrpc_set_resend(call
, resend
, resend_at
);
242 * handle resend timer expiry
244 static void rxrpc_resend_timer(struct rxrpc_call
*call
)
246 struct rxrpc_skb_priv
*sp
;
248 unsigned long *p_txb
, resend_at
;
253 call
->acks_tail
, call
->acks_unacked
, call
->acks_head
);
255 if (call
->state
>= RXRPC_CALL_COMPLETE
)
261 for (loop
= call
->acks_unacked
;
262 loop
!= call
->acks_head
;
263 loop
= (loop
+ 1) & (call
->acks_winsz
- 1)
265 p_txb
= call
->acks_window
+ loop
;
266 smp_read_barrier_depends();
267 txb
= (struct sk_buff
*) (*p_txb
& ~1);
270 ASSERT(!(*p_txb
& 1));
272 if (sp
->need_resend
) {
274 } else if (time_after_eq(jiffies
+ 1, sp
->resend_at
)) {
275 sp
->need_resend
= true;
277 } else if (resend
& 2) {
278 if (time_before(sp
->resend_at
, resend_at
))
279 resend_at
= sp
->resend_at
;
281 resend_at
= sp
->resend_at
;
286 rxrpc_set_resend(call
, resend
, resend_at
);
291 * process soft ACKs of our transmitted packets
292 * - these indicate packets the peer has or has not received, but hasn't yet
293 * given to the consumer, and so can still be discarded and re-requested
295 static int rxrpc_process_soft_ACKs(struct rxrpc_call
*call
,
296 struct rxrpc_ackpacket
*ack
,
299 struct rxrpc_skb_priv
*sp
;
301 unsigned long *p_txb
, resend_at
;
303 u8 sacks
[RXRPC_MAXACKS
], resend
;
305 _enter("{%d,%d},{%d},",
307 CIRC_CNT(call
->acks_head
, call
->acks_tail
, call
->acks_winsz
),
310 if (skb_copy_bits(skb
, 0, sacks
, ack
->nAcks
) < 0)
315 for (loop
= 0; loop
< ack
->nAcks
; loop
++) {
316 p_txb
= call
->acks_window
;
317 p_txb
+= (call
->acks_tail
+ loop
) & (call
->acks_winsz
- 1);
318 smp_read_barrier_depends();
319 txb
= (struct sk_buff
*) (*p_txb
& ~1);
322 switch (sacks
[loop
]) {
323 case RXRPC_ACK_TYPE_ACK
:
324 sp
->need_resend
= false;
327 case RXRPC_ACK_TYPE_NACK
:
328 sp
->need_resend
= true;
333 _debug("Unsupported ACK type %d", sacks
[loop
]);
339 call
->acks_unacked
= (call
->acks_tail
+ loop
) & (call
->acks_winsz
- 1);
341 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
342 * have been received or processed yet by the far end */
343 for (loop
= call
->acks_unacked
;
344 loop
!= call
->acks_head
;
345 loop
= (loop
+ 1) & (call
->acks_winsz
- 1)
347 p_txb
= call
->acks_window
+ loop
;
348 smp_read_barrier_depends();
349 txb
= (struct sk_buff
*) (*p_txb
& ~1);
353 /* packet must have been discarded */
354 sp
->need_resend
= true;
357 } else if (sp
->need_resend
) {
359 } else if (time_after_eq(jiffies
+ 1, sp
->resend_at
)) {
360 sp
->need_resend
= true;
362 } else if (resend
& 2) {
363 if (time_before(sp
->resend_at
, resend_at
))
364 resend_at
= sp
->resend_at
;
366 resend_at
= sp
->resend_at
;
371 rxrpc_set_resend(call
, resend
, resend_at
);
376 _leave(" = -EPROTO");
381 * discard hard-ACK'd packets from the Tx window
383 static void rxrpc_rotate_tx_window(struct rxrpc_call
*call
, u32 hard
)
386 int tail
= call
->acks_tail
, old_tail
;
387 int win
= CIRC_CNT(call
->acks_head
, tail
, call
->acks_winsz
);
389 _enter("{%u,%u},%u", call
->acks_hard
, win
, hard
);
391 ASSERTCMP(hard
- call
->acks_hard
, <=, win
);
393 while (call
->acks_hard
< hard
) {
394 smp_read_barrier_depends();
395 _skb
= call
->acks_window
[tail
] & ~1;
396 rxrpc_free_skb((struct sk_buff
*) _skb
);
398 tail
= (tail
+ 1) & (call
->acks_winsz
- 1);
399 call
->acks_tail
= tail
;
400 if (call
->acks_unacked
== old_tail
)
401 call
->acks_unacked
= tail
;
405 wake_up(&call
->tx_waitq
);
409 * clear the Tx window in the event of a failure
411 static void rxrpc_clear_tx_window(struct rxrpc_call
*call
)
413 rxrpc_rotate_tx_window(call
, atomic_read(&call
->sequence
));
417 * drain the out of sequence received packet queue into the packet Rx queue
419 static int rxrpc_drain_rx_oos_queue(struct rxrpc_call
*call
)
421 struct rxrpc_skb_priv
*sp
;
426 _enter("{%d,%d}", call
->rx_data_post
, call
->rx_first_oos
);
428 spin_lock_bh(&call
->lock
);
431 if (test_bit(RXRPC_CALL_RELEASED
, &call
->flags
))
432 goto socket_unavailable
;
434 skb
= skb_dequeue(&call
->rx_oos_queue
);
438 _debug("drain OOS packet %d [%d]",
439 ntohl(sp
->hdr
.seq
), call
->rx_first_oos
);
441 if (ntohl(sp
->hdr
.seq
) != call
->rx_first_oos
) {
442 skb_queue_head(&call
->rx_oos_queue
, skb
);
443 call
->rx_first_oos
= ntohl(rxrpc_skb(skb
)->hdr
.seq
);
444 _debug("requeue %p {%u}", skb
, call
->rx_first_oos
);
446 skb
->mark
= RXRPC_SKB_MARK_DATA
;
447 terminal
= ((sp
->hdr
.flags
& RXRPC_LAST_PACKET
) &&
448 !(sp
->hdr
.flags
& RXRPC_CLIENT_INITIATED
));
449 ret
= rxrpc_queue_rcv_skb(call
, skb
, true, terminal
);
451 _debug("drain #%u", call
->rx_data_post
);
452 call
->rx_data_post
++;
454 /* find out what the next packet is */
455 skb
= skb_peek(&call
->rx_oos_queue
);
458 ntohl(rxrpc_skb(skb
)->hdr
.seq
);
460 call
->rx_first_oos
= 0;
461 _debug("peek %p {%u}", skb
, call
->rx_first_oos
);
467 spin_unlock_bh(&call
->lock
);
468 _leave(" = %d", ret
);
473 * insert an out of sequence packet into the buffer
475 static void rxrpc_insert_oos_packet(struct rxrpc_call
*call
,
478 struct rxrpc_skb_priv
*sp
, *psp
;
483 seq
= ntohl(sp
->hdr
.seq
);
484 _enter(",,{%u}", seq
);
486 skb
->destructor
= rxrpc_packet_destructor
;
487 ASSERTCMP(sp
->call
, ==, NULL
);
489 rxrpc_get_call(call
);
491 /* insert into the buffer in sequence order */
492 spin_lock_bh(&call
->lock
);
494 skb_queue_walk(&call
->rx_oos_queue
, p
) {
496 if (ntohl(psp
->hdr
.seq
) > seq
) {
497 _debug("insert oos #%u before #%u",
498 seq
, ntohl(psp
->hdr
.seq
));
499 skb_insert(p
, skb
, &call
->rx_oos_queue
);
504 _debug("append oos #%u", seq
);
505 skb_queue_tail(&call
->rx_oos_queue
, skb
);
508 /* we might now have a new front to the queue */
509 if (call
->rx_first_oos
== 0 || seq
< call
->rx_first_oos
)
510 call
->rx_first_oos
= seq
;
512 read_lock(&call
->state_lock
);
513 if (call
->state
< RXRPC_CALL_COMPLETE
&&
514 call
->rx_data_post
== call
->rx_first_oos
) {
515 _debug("drain rx oos now");
516 set_bit(RXRPC_CALL_DRAIN_RX_OOS
, &call
->events
);
518 read_unlock(&call
->state_lock
);
520 spin_unlock_bh(&call
->lock
);
521 _leave(" [stored #%u]", call
->rx_first_oos
);
525 * clear the Tx window on final ACK reception
527 static void rxrpc_zap_tx_window(struct rxrpc_call
*call
)
529 struct rxrpc_skb_priv
*sp
;
531 unsigned long _skb
, *acks_window
;
532 u8 winsz
= call
->acks_winsz
;
535 acks_window
= call
->acks_window
;
536 call
->acks_window
= NULL
;
538 while (CIRC_CNT(call
->acks_head
, call
->acks_tail
, winsz
) > 0) {
539 tail
= call
->acks_tail
;
540 smp_read_barrier_depends();
541 _skb
= acks_window
[tail
] & ~1;
543 call
->acks_tail
= (call
->acks_tail
+ 1) & (winsz
- 1);
545 skb
= (struct sk_buff
*) _skb
;
547 _debug("+++ clear Tx %u", ntohl(sp
->hdr
.seq
));
555 * process the extra information that may be appended to an ACK packet
557 static void rxrpc_extract_ackinfo(struct rxrpc_call
*call
, struct sk_buff
*skb
,
558 unsigned int latest
, int nAcks
)
560 struct rxrpc_ackinfo ackinfo
;
561 struct rxrpc_peer
*peer
;
564 if (skb_copy_bits(skb
, nAcks
+ 3, &ackinfo
, sizeof(ackinfo
)) < 0) {
565 _leave(" [no ackinfo]");
569 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
571 ntohl(ackinfo
.rxMTU
), ntohl(ackinfo
.maxMTU
),
572 ntohl(ackinfo
.rwind
), ntohl(ackinfo
.jumbo_max
));
574 mtu
= min(ntohl(ackinfo
.rxMTU
), ntohl(ackinfo
.maxMTU
));
576 peer
= call
->conn
->trans
->peer
;
577 if (mtu
< peer
->maxdata
) {
578 spin_lock_bh(&peer
->lock
);
580 peer
->mtu
= mtu
+ peer
->hdrsize
;
581 spin_unlock_bh(&peer
->lock
);
582 _net("Net MTU %u (maxdata %u)", peer
->mtu
, peer
->maxdata
);
587 * process packets in the reception queue
589 static int rxrpc_process_rx_queue(struct rxrpc_call
*call
,
592 struct rxrpc_ackpacket ack
;
593 struct rxrpc_skb_priv
*sp
;
602 skb
= skb_dequeue(&call
->rx_queue
);
606 _net("deferred skb %p", skb
);
610 _debug("process %s [st %d]", rxrpc_pkts
[sp
->hdr
.type
], call
->state
);
614 switch (sp
->hdr
.type
) {
615 /* data packets that wind up here have been received out of
616 * order, need security processing or are jumbo packets */
617 case RXRPC_PACKET_TYPE_DATA
:
618 _proto("OOSQ DATA %%%u { #%u }",
619 ntohl(sp
->hdr
.serial
), ntohl(sp
->hdr
.seq
));
621 /* secured packets must be verified and possibly decrypted */
622 if (rxrpc_verify_packet(call
, skb
, _abort_code
) < 0)
625 rxrpc_insert_oos_packet(call
, skb
);
626 goto process_further
;
628 /* partial ACK to process */
629 case RXRPC_PACKET_TYPE_ACK
:
630 if (skb_copy_bits(skb
, 0, &ack
, sizeof(ack
)) < 0) {
631 _debug("extraction failure");
634 if (!skb_pull(skb
, sizeof(ack
)))
637 latest
= ntohl(sp
->hdr
.serial
);
638 hard
= ntohl(ack
.firstPacket
);
639 tx
= atomic_read(&call
->sequence
);
641 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
645 ntohl(ack
.previousPacket
),
647 rxrpc_acks(ack
.reason
),
650 rxrpc_extract_ackinfo(call
, skb
, latest
, ack
.nAcks
);
652 if (ack
.reason
== RXRPC_ACK_PING
) {
653 _proto("Rx ACK %%%u PING Request", latest
);
654 rxrpc_propose_ACK(call
, RXRPC_ACK_PING_RESPONSE
,
655 sp
->hdr
.serial
, true);
658 /* discard any out-of-order or duplicate ACKs */
659 if (latest
- call
->acks_latest
<= 0) {
660 _debug("discard ACK %d <= %d",
661 latest
, call
->acks_latest
);
664 call
->acks_latest
= latest
;
666 if (call
->state
!= RXRPC_CALL_CLIENT_SEND_REQUEST
&&
667 call
->state
!= RXRPC_CALL_CLIENT_AWAIT_REPLY
&&
668 call
->state
!= RXRPC_CALL_SERVER_SEND_REPLY
&&
669 call
->state
!= RXRPC_CALL_SERVER_AWAIT_ACK
)
672 _debug("Tx=%d H=%u S=%d", tx
, call
->acks_hard
, call
->state
);
676 _debug("hard-ACK'd packet %d not transmitted"
682 if ((call
->state
== RXRPC_CALL_CLIENT_AWAIT_REPLY
||
683 call
->state
== RXRPC_CALL_SERVER_AWAIT_ACK
) &&
688 rxrpc_rotate_tx_window(call
, hard
- 1);
692 if (hard
- 1 + ack
.nAcks
> tx
) {
693 _debug("soft-ACK'd packet %d+%d not"
694 " transmitted (%d top)",
695 hard
- 1, ack
.nAcks
, tx
);
699 if (rxrpc_process_soft_ACKs(call
, &ack
, skb
) < 0)
704 /* complete ACK to process */
705 case RXRPC_PACKET_TYPE_ACKALL
:
708 /* abort and busy are handled elsewhere */
709 case RXRPC_PACKET_TYPE_BUSY
:
710 case RXRPC_PACKET_TYPE_ABORT
:
713 /* connection level events - also handled elsewhere */
714 case RXRPC_PACKET_TYPE_CHALLENGE
:
715 case RXRPC_PACKET_TYPE_RESPONSE
:
716 case RXRPC_PACKET_TYPE_DEBUG
:
720 /* if we've had a hard ACK that covers all the packets we've sent, then
721 * that ends that phase of the operation */
723 write_lock_bh(&call
->state_lock
);
724 _debug("ack all %d", call
->state
);
726 switch (call
->state
) {
727 case RXRPC_CALL_CLIENT_AWAIT_REPLY
:
728 call
->state
= RXRPC_CALL_CLIENT_RECV_REPLY
;
730 case RXRPC_CALL_SERVER_AWAIT_ACK
:
731 _debug("srv complete");
732 call
->state
= RXRPC_CALL_COMPLETE
;
735 case RXRPC_CALL_CLIENT_SEND_REQUEST
:
736 case RXRPC_CALL_SERVER_RECV_REQUEST
:
737 goto protocol_error_unlock
; /* can't occur yet */
739 write_unlock_bh(&call
->state_lock
);
740 goto discard
; /* assume packet left over from earlier phase */
743 write_unlock_bh(&call
->state_lock
);
745 /* if all the packets we sent are hard-ACK'd, then we can discard
746 * whatever we've got left */
747 _debug("clear Tx %d",
748 CIRC_CNT(call
->acks_head
, call
->acks_tail
, call
->acks_winsz
));
750 del_timer_sync(&call
->resend_timer
);
751 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
752 clear_bit(RXRPC_CALL_RESEND_TIMER
, &call
->events
);
754 if (call
->acks_window
)
755 rxrpc_zap_tx_window(call
);
758 /* post the final ACK message for userspace to pick up */
760 skb
->mark
= RXRPC_SKB_MARK_FINAL_ACK
;
762 rxrpc_get_call(call
);
763 spin_lock_bh(&call
->lock
);
764 if (rxrpc_queue_rcv_skb(call
, skb
, true, true) < 0)
766 spin_unlock_bh(&call
->lock
);
767 goto process_further
;
772 goto process_further
;
774 protocol_error_unlock
:
775 write_unlock_bh(&call
->state_lock
);
778 _leave(" = -EPROTO");
783 * post a message to the socket Rx queue for recvmsg() to pick up
785 static int rxrpc_post_message(struct rxrpc_call
*call
, u32 mark
, u32 error
,
788 struct rxrpc_skb_priv
*sp
;
792 _enter("{%d,%lx},%u,%u,%d",
793 call
->debug_id
, call
->flags
, mark
, error
, fatal
);
795 /* remove timers and things for fatal messages */
797 del_timer_sync(&call
->resend_timer
);
798 del_timer_sync(&call
->ack_timer
);
799 clear_bit(RXRPC_CALL_RUN_RTIMER
, &call
->flags
);
802 if (mark
!= RXRPC_SKB_MARK_NEW_CALL
&&
803 !test_bit(RXRPC_CALL_HAS_USERID
, &call
->flags
)) {
804 _leave("[no userid]");
808 if (!test_bit(RXRPC_CALL_TERMINAL_MSG
, &call
->flags
)) {
809 skb
= alloc_skb(0, GFP_NOFS
);
818 memset(sp
, 0, sizeof(*sp
));
821 rxrpc_get_call(call
);
823 spin_lock_bh(&call
->lock
);
824 ret
= rxrpc_queue_rcv_skb(call
, skb
, true, fatal
);
825 spin_unlock_bh(&call
->lock
);
833 * handle background processing of incoming call packets and ACK / abort
836 void rxrpc_process_call(struct work_struct
*work
)
838 struct rxrpc_call
*call
=
839 container_of(work
, struct rxrpc_call
, processor
);
840 struct rxrpc_ackpacket ack
;
841 struct rxrpc_ackinfo ackinfo
;
842 struct rxrpc_header hdr
;
848 int genbit
, loop
, nbit
, ioc
, ret
, mtu
;
849 u32 abort_code
= RX_PROTOCOL_ERROR
;
852 //printk("\n--------------------\n");
853 _enter("{%d,%s,%lx} [%lu]",
854 call
->debug_id
, rxrpc_call_states
[call
->state
], call
->events
,
855 (jiffies
- call
->creation_jif
) / (HZ
/ 10));
857 if (test_and_set_bit(RXRPC_CALL_PROC_BUSY
, &call
->flags
)) {
858 _debug("XXXXXXXXXXXXX RUNNING ON MULTIPLE CPUS XXXXXXXXXXXXX");
862 /* there's a good chance we're going to have to send a message, so set
863 * one up in advance */
864 msg
.msg_name
= &call
->conn
->trans
->peer
->srx
.transport
.sin
;
865 msg
.msg_namelen
= sizeof(call
->conn
->trans
->peer
->srx
.transport
.sin
);
866 msg
.msg_control
= NULL
;
867 msg
.msg_controllen
= 0;
870 hdr
.epoch
= call
->conn
->epoch
;
872 hdr
.callNumber
= call
->call_id
;
874 hdr
.type
= RXRPC_PACKET_TYPE_ACK
;
875 hdr
.flags
= call
->conn
->out_clientflag
;
877 hdr
.securityIndex
= call
->conn
->security_ix
;
879 hdr
.serviceId
= call
->conn
->service_id
;
881 memset(iov
, 0, sizeof(iov
));
882 iov
[0].iov_base
= &hdr
;
883 iov
[0].iov_len
= sizeof(hdr
);
885 /* deal with events of a final nature */
886 if (test_bit(RXRPC_CALL_RELEASE
, &call
->events
)) {
887 rxrpc_release_call(call
);
888 clear_bit(RXRPC_CALL_RELEASE
, &call
->events
);
891 if (test_bit(RXRPC_CALL_RCVD_ERROR
, &call
->events
)) {
894 clear_bit(RXRPC_CALL_CONN_ABORT
, &call
->events
);
895 clear_bit(RXRPC_CALL_REJECT_BUSY
, &call
->events
);
896 clear_bit(RXRPC_CALL_ABORT
, &call
->events
);
898 error
= call
->conn
->trans
->peer
->net_error
;
899 _debug("post net error %d", error
);
901 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_NET_ERROR
,
904 clear_bit(RXRPC_CALL_RCVD_ERROR
, &call
->events
);
908 if (test_bit(RXRPC_CALL_CONN_ABORT
, &call
->events
)) {
909 ASSERTCMP(call
->state
, >, RXRPC_CALL_COMPLETE
);
911 clear_bit(RXRPC_CALL_REJECT_BUSY
, &call
->events
);
912 clear_bit(RXRPC_CALL_ABORT
, &call
->events
);
914 _debug("post conn abort");
916 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_LOCAL_ERROR
,
917 call
->conn
->error
, true) < 0)
919 clear_bit(RXRPC_CALL_CONN_ABORT
, &call
->events
);
923 if (test_bit(RXRPC_CALL_REJECT_BUSY
, &call
->events
)) {
924 hdr
.type
= RXRPC_PACKET_TYPE_BUSY
;
925 genbit
= RXRPC_CALL_REJECT_BUSY
;
929 if (test_bit(RXRPC_CALL_ABORT
, &call
->events
)) {
930 ASSERTCMP(call
->state
, >, RXRPC_CALL_COMPLETE
);
932 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_LOCAL_ERROR
,
933 ECONNABORTED
, true) < 0)
935 hdr
.type
= RXRPC_PACKET_TYPE_ABORT
;
936 data
= htonl(call
->abort_code
);
937 iov
[1].iov_base
= &data
;
938 iov
[1].iov_len
= sizeof(data
);
939 genbit
= RXRPC_CALL_ABORT
;
943 if (test_bit(RXRPC_CALL_ACK_FINAL
, &call
->events
)) {
944 genbit
= RXRPC_CALL_ACK_FINAL
;
946 ack
.bufferSpace
= htons(8);
949 ack
.reason
= RXRPC_ACK_IDLE
;
951 call
->ackr_reason
= 0;
953 spin_lock_bh(&call
->lock
);
954 ack
.serial
= call
->ackr_serial
;
955 ack
.previousPacket
= call
->ackr_prev_seq
;
956 ack
.firstPacket
= htonl(call
->rx_data_eaten
+ 1);
957 spin_unlock_bh(&call
->lock
);
961 iov
[1].iov_base
= &ack
;
962 iov
[1].iov_len
= sizeof(ack
);
963 iov
[2].iov_base
= &pad
;
965 iov
[3].iov_base
= &ackinfo
;
966 iov
[3].iov_len
= sizeof(ackinfo
);
970 if (call
->events
& ((1 << RXRPC_CALL_RCVD_BUSY
) |
971 (1 << RXRPC_CALL_RCVD_ABORT
))
975 if (test_bit(RXRPC_CALL_RCVD_ABORT
, &call
->events
))
976 mark
= RXRPC_SKB_MARK_REMOTE_ABORT
;
978 mark
= RXRPC_SKB_MARK_BUSY
;
980 _debug("post abort/busy");
981 rxrpc_clear_tx_window(call
);
982 if (rxrpc_post_message(call
, mark
, ECONNABORTED
, true) < 0)
985 clear_bit(RXRPC_CALL_RCVD_BUSY
, &call
->events
);
986 clear_bit(RXRPC_CALL_RCVD_ABORT
, &call
->events
);
990 if (test_and_clear_bit(RXRPC_CALL_RCVD_ACKALL
, &call
->events
)) {
991 _debug("do implicit ackall");
992 rxrpc_clear_tx_window(call
);
995 if (test_bit(RXRPC_CALL_LIFE_TIMER
, &call
->events
)) {
996 write_lock_bh(&call
->state_lock
);
997 if (call
->state
<= RXRPC_CALL_COMPLETE
) {
998 call
->state
= RXRPC_CALL_LOCALLY_ABORTED
;
999 call
->abort_code
= RX_CALL_TIMEOUT
;
1000 set_bit(RXRPC_CALL_ABORT
, &call
->events
);
1002 write_unlock_bh(&call
->state_lock
);
1004 _debug("post timeout");
1005 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_LOCAL_ERROR
,
1009 clear_bit(RXRPC_CALL_LIFE_TIMER
, &call
->events
);
1013 /* deal with assorted inbound messages */
1014 if (!skb_queue_empty(&call
->rx_queue
)) {
1015 switch (rxrpc_process_rx_queue(call
, &abort_code
)) {
1024 rxrpc_abort_call(call
, abort_code
);
1029 /* handle resending */
1030 if (test_and_clear_bit(RXRPC_CALL_RESEND_TIMER
, &call
->events
))
1031 rxrpc_resend_timer(call
);
1032 if (test_and_clear_bit(RXRPC_CALL_RESEND
, &call
->events
))
1035 /* consider sending an ordinary ACK */
1036 if (test_bit(RXRPC_CALL_ACK
, &call
->events
)) {
1037 _debug("send ACK: window: %d - %d { %lx }",
1038 call
->rx_data_eaten
, call
->ackr_win_top
,
1039 call
->ackr_window
[0]);
1041 if (call
->state
> RXRPC_CALL_SERVER_ACK_REQUEST
&&
1042 call
->ackr_reason
!= RXRPC_ACK_PING_RESPONSE
) {
1043 /* ACK by sending reply DATA packet in this state */
1044 clear_bit(RXRPC_CALL_ACK
, &call
->events
);
1045 goto maybe_reschedule
;
1048 genbit
= RXRPC_CALL_ACK
;
1050 acks
= kzalloc(call
->ackr_win_top
- call
->rx_data_eaten
,
1055 //hdr.flags = RXRPC_SLOW_START_OK;
1056 ack
.bufferSpace
= htons(8);
1061 spin_lock_bh(&call
->lock
);
1062 ack
.reason
= call
->ackr_reason
;
1063 ack
.serial
= call
->ackr_serial
;
1064 ack
.previousPacket
= call
->ackr_prev_seq
;
1065 ack
.firstPacket
= htonl(call
->rx_data_eaten
+ 1);
1068 for (loop
= 0; loop
< RXRPC_ACKR_WINDOW_ASZ
; loop
++) {
1069 nbit
= loop
* BITS_PER_LONG
;
1070 for (bits
= call
->ackr_window
[loop
]; bits
; bits
>>= 1
1072 _debug("- l=%d n=%d b=%lx", loop
, nbit
, bits
);
1074 acks
[nbit
] = RXRPC_ACK_TYPE_ACK
;
1075 ack
.nAcks
= nbit
+ 1;
1080 call
->ackr_reason
= 0;
1081 spin_unlock_bh(&call
->lock
);
1085 iov
[1].iov_base
= &ack
;
1086 iov
[1].iov_len
= sizeof(ack
);
1087 iov
[2].iov_base
= acks
;
1088 iov
[2].iov_len
= ack
.nAcks
;
1089 iov
[3].iov_base
= &pad
;
1091 iov
[4].iov_base
= &ackinfo
;
1092 iov
[4].iov_len
= sizeof(ackinfo
);
1094 switch (ack
.reason
) {
1095 case RXRPC_ACK_REQUESTED
:
1096 case RXRPC_ACK_DUPLICATE
:
1097 case RXRPC_ACK_OUT_OF_SEQUENCE
:
1098 case RXRPC_ACK_EXCEEDS_WINDOW
:
1099 case RXRPC_ACK_NOSPACE
:
1100 case RXRPC_ACK_PING
:
1101 case RXRPC_ACK_PING_RESPONSE
:
1102 goto send_ACK_with_skew
;
1103 case RXRPC_ACK_DELAY
:
1104 case RXRPC_ACK_IDLE
:
1109 /* handle completion of security negotiations on an incoming
1111 if (test_and_clear_bit(RXRPC_CALL_SECURED
, &call
->events
)) {
1113 spin_lock_bh(&call
->lock
);
1115 if (call
->state
== RXRPC_CALL_SERVER_SECURING
) {
1117 write_lock(&call
->conn
->lock
);
1118 if (!test_bit(RXRPC_CALL_RELEASED
, &call
->flags
) &&
1119 !test_bit(RXRPC_CALL_RELEASE
, &call
->events
)) {
1120 _debug("not released");
1121 call
->state
= RXRPC_CALL_SERVER_ACCEPTING
;
1122 list_move_tail(&call
->accept_link
,
1123 &call
->socket
->acceptq
);
1125 write_unlock(&call
->conn
->lock
);
1126 read_lock(&call
->state_lock
);
1127 if (call
->state
< RXRPC_CALL_COMPLETE
)
1128 set_bit(RXRPC_CALL_POST_ACCEPT
, &call
->events
);
1129 read_unlock(&call
->state_lock
);
1132 spin_unlock_bh(&call
->lock
);
1133 if (!test_bit(RXRPC_CALL_POST_ACCEPT
, &call
->events
))
1134 goto maybe_reschedule
;
1137 /* post a notification of an acceptable connection to the app */
1138 if (test_bit(RXRPC_CALL_POST_ACCEPT
, &call
->events
)) {
1139 _debug("post accept");
1140 if (rxrpc_post_message(call
, RXRPC_SKB_MARK_NEW_CALL
,
1143 clear_bit(RXRPC_CALL_POST_ACCEPT
, &call
->events
);
1144 goto maybe_reschedule
;
1147 /* handle incoming call acceptance */
1148 if (test_and_clear_bit(RXRPC_CALL_ACCEPTED
, &call
->events
)) {
1150 ASSERTCMP(call
->rx_data_post
, ==, 0);
1151 call
->rx_data_post
= 1;
1152 read_lock_bh(&call
->state_lock
);
1153 if (call
->state
< RXRPC_CALL_COMPLETE
)
1154 set_bit(RXRPC_CALL_DRAIN_RX_OOS
, &call
->events
);
1155 read_unlock_bh(&call
->state_lock
);
1158 /* drain the out of sequence received packet queue into the packet Rx
1160 if (test_and_clear_bit(RXRPC_CALL_DRAIN_RX_OOS
, &call
->events
)) {
1161 while (call
->rx_data_post
== call
->rx_first_oos
)
1162 if (rxrpc_drain_rx_oos_queue(call
) < 0)
1164 goto maybe_reschedule
;
1167 /* other events may have been raised since we started checking */
1168 goto maybe_reschedule
;
1171 ack
.maxSkew
= htons(atomic_read(&call
->conn
->hi_serial
) -
1174 mtu
= call
->conn
->trans
->peer
->if_mtu
;
1175 mtu
-= call
->conn
->trans
->peer
->hdrsize
;
1176 ackinfo
.maxMTU
= htonl(mtu
);
1177 ackinfo
.rwind
= htonl(32);
1179 /* permit the peer to send us jumbo packets if it wants to */
1180 ackinfo
.rxMTU
= htonl(5692);
1181 ackinfo
.jumbo_max
= htonl(4);
1183 hdr
.serial
= htonl(atomic_inc_return(&call
->conn
->serial
));
1184 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
1187 ntohl(ack
.firstPacket
),
1188 ntohl(ack
.previousPacket
),
1190 rxrpc_acks(ack
.reason
),
1193 del_timer_sync(&call
->ack_timer
);
1195 set_bit(RXRPC_CALL_TX_SOFT_ACK
, &call
->flags
);
1196 goto send_message_2
;
1199 _debug("send message");
1201 hdr
.serial
= htonl(atomic_inc_return(&call
->conn
->serial
));
1202 _proto("Tx %s %%%u", rxrpc_pkts
[hdr
.type
], ntohl(hdr
.serial
));
1205 len
= iov
[0].iov_len
;
1207 if (iov
[4].iov_len
) {
1209 len
+= iov
[4].iov_len
;
1210 len
+= iov
[3].iov_len
;
1211 len
+= iov
[2].iov_len
;
1212 len
+= iov
[1].iov_len
;
1213 } else if (iov
[3].iov_len
) {
1215 len
+= iov
[3].iov_len
;
1216 len
+= iov
[2].iov_len
;
1217 len
+= iov
[1].iov_len
;
1218 } else if (iov
[2].iov_len
) {
1220 len
+= iov
[2].iov_len
;
1221 len
+= iov
[1].iov_len
;
1222 } else if (iov
[1].iov_len
) {
1224 len
+= iov
[1].iov_len
;
1227 ret
= kernel_sendmsg(call
->conn
->trans
->local
->socket
,
1228 &msg
, iov
, ioc
, len
);
1230 _debug("sendmsg failed: %d", ret
);
1231 read_lock_bh(&call
->state_lock
);
1232 if (call
->state
< RXRPC_CALL_DEAD
)
1233 rxrpc_queue_call(call
);
1234 read_unlock_bh(&call
->state_lock
);
1239 case RXRPC_CALL_ABORT
:
1240 clear_bit(genbit
, &call
->events
);
1241 clear_bit(RXRPC_CALL_RCVD_ABORT
, &call
->events
);
1244 case RXRPC_CALL_ACK_FINAL
:
1245 write_lock_bh(&call
->state_lock
);
1246 if (call
->state
== RXRPC_CALL_CLIENT_FINAL_ACK
)
1247 call
->state
= RXRPC_CALL_COMPLETE
;
1248 write_unlock_bh(&call
->state_lock
);
1252 clear_bit(genbit
, &call
->events
);
1253 switch (call
->state
) {
1254 case RXRPC_CALL_CLIENT_AWAIT_REPLY
:
1255 case RXRPC_CALL_CLIENT_RECV_REPLY
:
1256 case RXRPC_CALL_SERVER_RECV_REQUEST
:
1257 case RXRPC_CALL_SERVER_ACK_REQUEST
:
1258 _debug("start ACK timer");
1259 rxrpc_propose_ACK(call
, RXRPC_ACK_DELAY
,
1260 call
->ackr_serial
, false);
1264 goto maybe_reschedule
;
1268 del_timer_sync(&call
->ack_timer
);
1269 if (test_and_clear_bit(RXRPC_CALL_ACK_FINAL
, &call
->events
))
1270 rxrpc_put_call(call
);
1271 clear_bit(RXRPC_CALL_ACK
, &call
->events
);
1274 if (call
->events
|| !skb_queue_empty(&call
->rx_queue
)) {
1275 read_lock_bh(&call
->state_lock
);
1276 if (call
->state
< RXRPC_CALL_DEAD
)
1277 rxrpc_queue_call(call
);
1278 read_unlock_bh(&call
->state_lock
);
1281 /* don't leave aborted connections on the accept queue */
1282 if (call
->state
>= RXRPC_CALL_COMPLETE
&&
1283 !list_empty(&call
->accept_link
)) {
1284 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
1285 call
, call
->events
, call
->flags
,
1286 ntohl(call
->conn
->cid
));
1288 read_lock_bh(&call
->state_lock
);
1289 if (!test_bit(RXRPC_CALL_RELEASED
, &call
->flags
) &&
1290 !test_and_set_bit(RXRPC_CALL_RELEASE
, &call
->events
))
1291 rxrpc_queue_call(call
);
1292 read_unlock_bh(&call
->state_lock
);
1296 clear_bit(RXRPC_CALL_PROC_BUSY
, &call
->flags
);
1299 /* because we don't want two CPUs both processing the work item for one
1300 * call at the same time, we use a flag to note when it's busy; however
1301 * this means there's a race between clearing the flag and setting the
1302 * work pending bit and the work item being processed again */
1303 if (call
->events
&& !work_pending(&call
->processor
)) {
1304 _debug("jumpstart %x", ntohl(call
->conn
->cid
));
1305 rxrpc_queue_call(call
);
1312 _debug("out of memory");
1313 goto maybe_reschedule
;