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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/circ_buf.h>
16 #include <linux/net.h>
17 #include <linux/skbuff.h>
18 #include <linux/slab.h>
19 #include <linux/udp.h>
21 #include <net/af_rxrpc.h>
22 #include "ar-internal.h"
25 * propose an ACK be sent
27 void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
28 u32 serial, bool immediate)
31 s8 prior = rxrpc_ack_priority[ack_reason];
33 ASSERTCMP(prior, >, 0);
35 _enter("{%d},%s,%%%x,%u",
36 call->debug_id, rxrpc_acks(ack_reason), serial, immediate);
38 if (prior < rxrpc_ack_priority[call->ackr_reason]) {
44 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
46 if (prior == rxrpc_ack_priority[call->ackr_reason]) {
48 call->ackr_serial = serial;
54 call->ackr_reason = ack_reason;
55 call->ackr_serial = serial;
59 _debug("run delay timer");
60 expiry = rxrpc_soft_ack_delay;
65 _debug("run defer timer");
66 expiry = rxrpc_idle_ack_delay;
71 case RXRPC_ACK_REQUESTED:
72 expiry = rxrpc_requested_ack_delay;
75 if (!immediate || serial == 1) {
76 _debug("run defer timer");
81 _debug("immediate ACK");
87 if (!timer_pending(&call->ack_timer) ||
88 time_after(call->ack_timer.expires, expiry))
89 mod_timer(&call->ack_timer, expiry);
93 _debug("cancel timer %%%u", serial);
94 try_to_del_timer_sync(&call->ack_timer);
95 read_lock_bh(&call->state_lock);
96 if (call->state <= RXRPC_CALL_COMPLETE &&
97 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
98 rxrpc_queue_call(call);
99 read_unlock_bh(&call->state_lock);
103 * propose an ACK be sent, locking the call structure
105 void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
106 u32 serial, bool immediate)
108 s8 prior = rxrpc_ack_priority[ack_reason];
110 if (prior > rxrpc_ack_priority[call->ackr_reason]) {
111 spin_lock_bh(&call->lock);
112 __rxrpc_propose_ACK(call, ack_reason, serial, immediate);
113 spin_unlock_bh(&call->lock);
118 * set the resend timer
120 static void rxrpc_set_resend(struct rxrpc_call *call, u8 resend,
121 unsigned long resend_at)
123 read_lock_bh(&call->state_lock);
124 if (call->state >= RXRPC_CALL_COMPLETE)
128 _debug("SET RESEND");
129 set_bit(RXRPC_CALL_EV_RESEND, &call->events);
133 _debug("MODIFY RESEND TIMER");
134 set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
135 mod_timer(&call->resend_timer, resend_at);
137 _debug("KILL RESEND TIMER");
138 del_timer_sync(&call->resend_timer);
139 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
140 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
142 read_unlock_bh(&call->state_lock);
148 static void rxrpc_resend(struct rxrpc_call *call)
150 struct rxrpc_wire_header *whdr;
151 struct rxrpc_skb_priv *sp;
153 unsigned long *p_txb, resend_at;
158 _enter("{%d,%d,%d,%d},",
159 call->acks_hard, call->acks_unacked,
160 atomic_read(&call->sequence),
161 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
167 for (loop = call->acks_tail;
168 loop != call->acks_head || stop;
169 loop = (loop + 1) & (call->acks_winsz - 1)
171 p_txb = call->acks_window + loop;
172 smp_read_barrier_depends();
176 txb = (struct sk_buff *) *p_txb;
179 if (sp->need_resend) {
180 sp->need_resend = false;
182 /* each Tx packet has a new serial number */
183 sp->hdr.serial = atomic_inc_return(&call->conn->serial);
185 whdr = (struct rxrpc_wire_header *)txb->head;
186 whdr->serial = htonl(sp->hdr.serial);
188 _proto("Tx DATA %%%u { #%d }",
189 sp->hdr.serial, sp->hdr.seq);
190 if (rxrpc_send_data_packet(call->conn, txb) < 0) {
192 sp->resend_at = jiffies + 3;
195 jiffies + rxrpc_resend_timeout;
199 if (time_after_eq(jiffies + 1, sp->resend_at)) {
200 sp->need_resend = true;
202 } else if (resend & 2) {
203 if (time_before(sp->resend_at, resend_at))
204 resend_at = sp->resend_at;
206 resend_at = sp->resend_at;
211 rxrpc_set_resend(call, resend, resend_at);
216 * handle resend timer expiry
218 static void rxrpc_resend_timer(struct rxrpc_call *call)
220 struct rxrpc_skb_priv *sp;
222 unsigned long *p_txb, resend_at;
227 call->acks_tail, call->acks_unacked, call->acks_head);
229 if (call->state >= RXRPC_CALL_COMPLETE)
235 for (loop = call->acks_unacked;
236 loop != call->acks_head;
237 loop = (loop + 1) & (call->acks_winsz - 1)
239 p_txb = call->acks_window + loop;
240 smp_read_barrier_depends();
241 txb = (struct sk_buff *) (*p_txb & ~1);
244 ASSERT(!(*p_txb & 1));
246 if (sp->need_resend) {
248 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
249 sp->need_resend = true;
251 } else if (resend & 2) {
252 if (time_before(sp->resend_at, resend_at))
253 resend_at = sp->resend_at;
255 resend_at = sp->resend_at;
260 rxrpc_set_resend(call, resend, resend_at);
265 * process soft ACKs of our transmitted packets
266 * - these indicate packets the peer has or has not received, but hasn't yet
267 * given to the consumer, and so can still be discarded and re-requested
269 static int rxrpc_process_soft_ACKs(struct rxrpc_call *call,
270 struct rxrpc_ackpacket *ack,
273 struct rxrpc_skb_priv *sp;
275 unsigned long *p_txb, resend_at;
277 u8 sacks[RXRPC_MAXACKS], resend;
279 _enter("{%d,%d},{%d},",
281 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz),
284 if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0)
289 for (loop = 0; loop < ack->nAcks; loop++) {
290 p_txb = call->acks_window;
291 p_txb += (call->acks_tail + loop) & (call->acks_winsz - 1);
292 smp_read_barrier_depends();
293 txb = (struct sk_buff *) (*p_txb & ~1);
296 switch (sacks[loop]) {
297 case RXRPC_ACK_TYPE_ACK:
298 sp->need_resend = false;
301 case RXRPC_ACK_TYPE_NACK:
302 sp->need_resend = true;
307 _debug("Unsupported ACK type %d", sacks[loop]);
313 call->acks_unacked = (call->acks_tail + loop) & (call->acks_winsz - 1);
315 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
316 * have been received or processed yet by the far end */
317 for (loop = call->acks_unacked;
318 loop != call->acks_head;
319 loop = (loop + 1) & (call->acks_winsz - 1)
321 p_txb = call->acks_window + loop;
322 smp_read_barrier_depends();
323 txb = (struct sk_buff *) (*p_txb & ~1);
327 /* packet must have been discarded */
328 sp->need_resend = true;
331 } else if (sp->need_resend) {
333 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
334 sp->need_resend = true;
336 } else if (resend & 2) {
337 if (time_before(sp->resend_at, resend_at))
338 resend_at = sp->resend_at;
340 resend_at = sp->resend_at;
345 rxrpc_set_resend(call, resend, resend_at);
350 _leave(" = -EPROTO");
355 * discard hard-ACK'd packets from the Tx window
357 static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard)
360 int tail = call->acks_tail, old_tail;
361 int win = CIRC_CNT(call->acks_head, tail, call->acks_winsz);
363 _enter("{%u,%u},%u", call->acks_hard, win, hard);
365 ASSERTCMP(hard - call->acks_hard, <=, win);
367 while (call->acks_hard < hard) {
368 smp_read_barrier_depends();
369 _skb = call->acks_window[tail] & ~1;
370 rxrpc_free_skb((struct sk_buff *) _skb);
372 tail = (tail + 1) & (call->acks_winsz - 1);
373 call->acks_tail = tail;
374 if (call->acks_unacked == old_tail)
375 call->acks_unacked = tail;
379 wake_up(&call->tx_waitq);
383 * clear the Tx window in the event of a failure
385 static void rxrpc_clear_tx_window(struct rxrpc_call *call)
387 rxrpc_rotate_tx_window(call, atomic_read(&call->sequence));
391 * drain the out of sequence received packet queue into the packet Rx queue
393 static int rxrpc_drain_rx_oos_queue(struct rxrpc_call *call)
395 struct rxrpc_skb_priv *sp;
400 _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos);
402 spin_lock_bh(&call->lock);
405 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
406 goto socket_unavailable;
408 skb = skb_dequeue(&call->rx_oos_queue);
412 _debug("drain OOS packet %d [%d]",
413 sp->hdr.seq, call->rx_first_oos);
415 if (sp->hdr.seq != call->rx_first_oos) {
416 skb_queue_head(&call->rx_oos_queue, skb);
417 call->rx_first_oos = rxrpc_skb(skb)->hdr.seq;
418 _debug("requeue %p {%u}", skb, call->rx_first_oos);
420 skb->mark = RXRPC_SKB_MARK_DATA;
421 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
422 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
423 ret = rxrpc_queue_rcv_skb(call, skb, true, terminal);
425 _debug("drain #%u", call->rx_data_post);
426 call->rx_data_post++;
428 /* find out what the next packet is */
429 skb = skb_peek(&call->rx_oos_queue);
431 call->rx_first_oos = rxrpc_skb(skb)->hdr.seq;
433 call->rx_first_oos = 0;
434 _debug("peek %p {%u}", skb, call->rx_first_oos);
440 spin_unlock_bh(&call->lock);
441 _leave(" = %d", ret);
446 * insert an out of sequence packet into the buffer
448 static void rxrpc_insert_oos_packet(struct rxrpc_call *call,
451 struct rxrpc_skb_priv *sp, *psp;
457 _enter(",,{%u}", seq);
459 skb->destructor = rxrpc_packet_destructor;
460 ASSERTCMP(sp->call, ==, NULL);
462 rxrpc_get_call(call);
464 /* insert into the buffer in sequence order */
465 spin_lock_bh(&call->lock);
467 skb_queue_walk(&call->rx_oos_queue, p) {
469 if (psp->hdr.seq > seq) {
470 _debug("insert oos #%u before #%u", seq, psp->hdr.seq);
471 skb_insert(p, skb, &call->rx_oos_queue);
476 _debug("append oos #%u", seq);
477 skb_queue_tail(&call->rx_oos_queue, skb);
480 /* we might now have a new front to the queue */
481 if (call->rx_first_oos == 0 || seq < call->rx_first_oos)
482 call->rx_first_oos = seq;
484 read_lock(&call->state_lock);
485 if (call->state < RXRPC_CALL_COMPLETE &&
486 call->rx_data_post == call->rx_first_oos) {
487 _debug("drain rx oos now");
488 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events);
490 read_unlock(&call->state_lock);
492 spin_unlock_bh(&call->lock);
493 _leave(" [stored #%u]", call->rx_first_oos);
497 * clear the Tx window on final ACK reception
499 static void rxrpc_zap_tx_window(struct rxrpc_call *call)
501 struct rxrpc_skb_priv *sp;
503 unsigned long _skb, *acks_window;
504 u8 winsz = call->acks_winsz;
507 acks_window = call->acks_window;
508 call->acks_window = NULL;
510 while (CIRC_CNT(call->acks_head, call->acks_tail, winsz) > 0) {
511 tail = call->acks_tail;
512 smp_read_barrier_depends();
513 _skb = acks_window[tail] & ~1;
515 call->acks_tail = (call->acks_tail + 1) & (winsz - 1);
517 skb = (struct sk_buff *) _skb;
519 _debug("+++ clear Tx %u", sp->hdr.seq);
527 * process the extra information that may be appended to an ACK packet
529 static void rxrpc_extract_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
530 unsigned int latest, int nAcks)
532 struct rxrpc_ackinfo ackinfo;
533 struct rxrpc_peer *peer;
536 if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) {
537 _leave(" [no ackinfo]");
541 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
543 ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU),
544 ntohl(ackinfo.rwind), ntohl(ackinfo.jumbo_max));
546 mtu = min(ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU));
548 peer = call->conn->params.peer;
549 if (mtu < peer->maxdata) {
550 spin_lock_bh(&peer->lock);
552 peer->mtu = mtu + peer->hdrsize;
553 spin_unlock_bh(&peer->lock);
554 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
559 * process packets in the reception queue
561 static int rxrpc_process_rx_queue(struct rxrpc_call *call,
564 struct rxrpc_ackpacket ack;
565 struct rxrpc_skb_priv *sp;
574 skb = skb_dequeue(&call->rx_queue);
578 _net("deferred skb %p", skb);
582 _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state);
586 switch (sp->hdr.type) {
587 /* data packets that wind up here have been received out of
588 * order, need security processing or are jumbo packets */
589 case RXRPC_PACKET_TYPE_DATA:
590 _proto("OOSQ DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
592 /* secured packets must be verified and possibly decrypted */
593 if (call->conn->security->verify_packet(call, skb,
597 rxrpc_insert_oos_packet(call, skb);
598 goto process_further;
600 /* partial ACK to process */
601 case RXRPC_PACKET_TYPE_ACK:
602 if (skb_copy_bits(skb, 0, &ack, sizeof(ack)) < 0) {
603 _debug("extraction failure");
606 if (!skb_pull(skb, sizeof(ack)))
609 latest = sp->hdr.serial;
610 hard = ntohl(ack.firstPacket);
611 tx = atomic_read(&call->sequence);
613 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
617 ntohl(ack.previousPacket),
619 rxrpc_acks(ack.reason),
622 rxrpc_extract_ackinfo(call, skb, latest, ack.nAcks);
624 if (ack.reason == RXRPC_ACK_PING) {
625 _proto("Rx ACK %%%u PING Request", latest);
626 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
627 sp->hdr.serial, true);
630 /* discard any out-of-order or duplicate ACKs */
631 if (latest - call->acks_latest <= 0) {
632 _debug("discard ACK %d <= %d",
633 latest, call->acks_latest);
636 call->acks_latest = latest;
638 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
639 call->state != RXRPC_CALL_CLIENT_AWAIT_REPLY &&
640 call->state != RXRPC_CALL_SERVER_SEND_REPLY &&
641 call->state != RXRPC_CALL_SERVER_AWAIT_ACK)
644 _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state);
648 _debug("hard-ACK'd packet %d not transmitted"
654 if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY ||
655 call->state == RXRPC_CALL_SERVER_AWAIT_ACK) &&
657 call->acks_hard = tx;
662 rxrpc_rotate_tx_window(call, hard - 1);
666 if (hard - 1 + ack.nAcks > tx) {
667 _debug("soft-ACK'd packet %d+%d not"
668 " transmitted (%d top)",
669 hard - 1, ack.nAcks, tx);
673 if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0)
678 /* complete ACK to process */
679 case RXRPC_PACKET_TYPE_ACKALL:
682 /* abort and busy are handled elsewhere */
683 case RXRPC_PACKET_TYPE_BUSY:
684 case RXRPC_PACKET_TYPE_ABORT:
687 /* connection level events - also handled elsewhere */
688 case RXRPC_PACKET_TYPE_CHALLENGE:
689 case RXRPC_PACKET_TYPE_RESPONSE:
690 case RXRPC_PACKET_TYPE_DEBUG:
694 /* if we've had a hard ACK that covers all the packets we've sent, then
695 * that ends that phase of the operation */
697 write_lock_bh(&call->state_lock);
698 _debug("ack all %d", call->state);
700 switch (call->state) {
701 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
702 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
704 case RXRPC_CALL_SERVER_AWAIT_ACK:
705 _debug("srv complete");
706 call->state = RXRPC_CALL_COMPLETE;
709 case RXRPC_CALL_CLIENT_SEND_REQUEST:
710 case RXRPC_CALL_SERVER_RECV_REQUEST:
711 goto protocol_error_unlock; /* can't occur yet */
713 write_unlock_bh(&call->state_lock);
714 goto discard; /* assume packet left over from earlier phase */
717 write_unlock_bh(&call->state_lock);
719 /* if all the packets we sent are hard-ACK'd, then we can discard
720 * whatever we've got left */
721 _debug("clear Tx %d",
722 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
724 del_timer_sync(&call->resend_timer);
725 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
726 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
728 if (call->acks_window)
729 rxrpc_zap_tx_window(call);
732 /* post the final ACK message for userspace to pick up */
734 skb->mark = RXRPC_SKB_MARK_FINAL_ACK;
736 rxrpc_get_call(call);
737 spin_lock_bh(&call->lock);
738 if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0)
740 spin_unlock_bh(&call->lock);
741 goto process_further;
746 goto process_further;
748 protocol_error_unlock:
749 write_unlock_bh(&call->state_lock);
752 _leave(" = -EPROTO");
757 * post a message to the socket Rx queue for recvmsg() to pick up
759 static int rxrpc_post_message(struct rxrpc_call *call, u32 mark, u32 error,
762 struct rxrpc_skb_priv *sp;
766 _enter("{%d,%lx},%u,%u,%d",
767 call->debug_id, call->flags, mark, error, fatal);
769 /* remove timers and things for fatal messages */
771 del_timer_sync(&call->resend_timer);
772 del_timer_sync(&call->ack_timer);
773 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
776 if (mark != RXRPC_SKB_MARK_NEW_CALL &&
777 !test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
778 _leave("[no userid]");
782 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
783 skb = alloc_skb(0, GFP_NOFS);
792 memset(sp, 0, sizeof(*sp));
795 rxrpc_get_call(call);
797 spin_lock_bh(&call->lock);
798 ret = rxrpc_queue_rcv_skb(call, skb, true, fatal);
799 spin_unlock_bh(&call->lock);
807 * handle background processing of incoming call packets and ACK / abort
810 void rxrpc_process_call(struct work_struct *work)
812 struct rxrpc_call *call =
813 container_of(work, struct rxrpc_call, processor);
814 struct rxrpc_wire_header whdr;
815 struct rxrpc_ackpacket ack;
816 struct rxrpc_ackinfo ackinfo;
819 enum rxrpc_call_event genbit;
823 int loop, nbit, ioc, ret, mtu;
824 u32 serial, abort_code = RX_PROTOCOL_ERROR;
827 //printk("\n--------------------\n");
828 _enter("{%d,%s,%lx} [%lu]",
829 call->debug_id, rxrpc_call_states[call->state], call->events,
830 (jiffies - call->creation_jif) / (HZ / 10));
832 if (test_and_set_bit(RXRPC_CALL_PROC_BUSY, &call->flags)) {
833 _debug("XXXXXXXXXXXXX RUNNING ON MULTIPLE CPUS XXXXXXXXXXXXX");
837 /* there's a good chance we're going to have to send a message, so set
838 * one up in advance */
839 msg.msg_name = &call->conn->params.peer->srx.transport;
840 msg.msg_namelen = call->conn->params.peer->srx.transport_len;
841 msg.msg_control = NULL;
842 msg.msg_controllen = 0;
845 whdr.epoch = htonl(call->conn->proto.epoch);
846 whdr.cid = htonl(call->cid);
847 whdr.callNumber = htonl(call->call_id);
849 whdr.type = RXRPC_PACKET_TYPE_ACK;
850 whdr.flags = call->conn->out_clientflag;
852 whdr.securityIndex = call->conn->security_ix;
854 whdr.serviceId = htons(call->service_id);
856 memset(iov, 0, sizeof(iov));
857 iov[0].iov_base = &whdr;
858 iov[0].iov_len = sizeof(whdr);
860 /* deal with events of a final nature */
861 if (test_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events)) {
862 enum rxrpc_skb_mark mark;
865 clear_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
866 clear_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events);
867 clear_bit(RXRPC_CALL_EV_ABORT, &call->events);
869 error = call->error_report;
870 if (error < RXRPC_LOCAL_ERROR_OFFSET) {
871 mark = RXRPC_SKB_MARK_NET_ERROR;
872 _debug("post net error %d", error);
874 mark = RXRPC_SKB_MARK_LOCAL_ERROR;
875 error -= RXRPC_LOCAL_ERROR_OFFSET;
876 _debug("post net local error %d", error);
879 if (rxrpc_post_message(call, mark, error, true) < 0)
881 clear_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events);
885 if (test_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events)) {
886 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
888 clear_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events);
889 clear_bit(RXRPC_CALL_EV_ABORT, &call->events);
891 _debug("post conn abort");
893 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
894 call->conn->error, true) < 0)
896 clear_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
900 if (test_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events)) {
901 whdr.type = RXRPC_PACKET_TYPE_BUSY;
902 genbit = RXRPC_CALL_EV_REJECT_BUSY;
906 if (test_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
907 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
909 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
910 ECONNABORTED, true) < 0)
912 whdr.type = RXRPC_PACKET_TYPE_ABORT;
913 data = htonl(call->local_abort);
914 iov[1].iov_base = &data;
915 iov[1].iov_len = sizeof(data);
916 genbit = RXRPC_CALL_EV_ABORT;
920 if (test_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events)) {
921 genbit = RXRPC_CALL_EV_ACK_FINAL;
923 ack.bufferSpace = htons(8);
926 ack.reason = RXRPC_ACK_IDLE;
928 call->ackr_reason = 0;
930 spin_lock_bh(&call->lock);
931 ack.serial = htonl(call->ackr_serial);
932 ack.previousPacket = htonl(call->ackr_prev_seq);
933 ack.firstPacket = htonl(call->rx_data_eaten + 1);
934 spin_unlock_bh(&call->lock);
938 iov[1].iov_base = &ack;
939 iov[1].iov_len = sizeof(ack);
940 iov[2].iov_base = &pad;
942 iov[3].iov_base = &ackinfo;
943 iov[3].iov_len = sizeof(ackinfo);
947 if (call->events & ((1 << RXRPC_CALL_EV_RCVD_BUSY) |
948 (1 << RXRPC_CALL_EV_RCVD_ABORT))
952 if (test_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events))
953 mark = RXRPC_SKB_MARK_REMOTE_ABORT;
955 mark = RXRPC_SKB_MARK_BUSY;
957 _debug("post abort/busy");
958 rxrpc_clear_tx_window(call);
959 if (rxrpc_post_message(call, mark, ECONNABORTED, true) < 0)
962 clear_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
963 clear_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
967 if (test_and_clear_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events)) {
968 _debug("do implicit ackall");
969 rxrpc_clear_tx_window(call);
972 if (test_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events)) {
973 write_lock_bh(&call->state_lock);
974 if (call->state <= RXRPC_CALL_COMPLETE) {
975 call->state = RXRPC_CALL_LOCALLY_ABORTED;
976 call->local_abort = RX_CALL_TIMEOUT;
977 set_bit(RXRPC_CALL_EV_ABORT, &call->events);
979 write_unlock_bh(&call->state_lock);
981 _debug("post timeout");
982 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
986 clear_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
990 /* deal with assorted inbound messages */
991 if (!skb_queue_empty(&call->rx_queue)) {
992 switch (rxrpc_process_rx_queue(call, &abort_code)) {
1001 rxrpc_abort_call(call, abort_code);
1006 /* handle resending */
1007 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
1008 rxrpc_resend_timer(call);
1009 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events))
1012 /* consider sending an ordinary ACK */
1013 if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
1014 _debug("send ACK: window: %d - %d { %lx }",
1015 call->rx_data_eaten, call->ackr_win_top,
1016 call->ackr_window[0]);
1018 if (call->state > RXRPC_CALL_SERVER_ACK_REQUEST &&
1019 call->ackr_reason != RXRPC_ACK_PING_RESPONSE) {
1020 /* ACK by sending reply DATA packet in this state */
1021 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
1022 goto maybe_reschedule;
1025 genbit = RXRPC_CALL_EV_ACK;
1027 acks = kzalloc(call->ackr_win_top - call->rx_data_eaten,
1032 //hdr.flags = RXRPC_SLOW_START_OK;
1033 ack.bufferSpace = htons(8);
1036 spin_lock_bh(&call->lock);
1037 ack.reason = call->ackr_reason;
1038 ack.serial = htonl(call->ackr_serial);
1039 ack.previousPacket = htonl(call->ackr_prev_seq);
1040 ack.firstPacket = htonl(call->rx_data_eaten + 1);
1043 for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) {
1044 nbit = loop * BITS_PER_LONG;
1045 for (bits = call->ackr_window[loop]; bits; bits >>= 1
1047 _debug("- l=%d n=%d b=%lx", loop, nbit, bits);
1049 acks[nbit] = RXRPC_ACK_TYPE_ACK;
1050 ack.nAcks = nbit + 1;
1055 call->ackr_reason = 0;
1056 spin_unlock_bh(&call->lock);
1060 iov[1].iov_base = &ack;
1061 iov[1].iov_len = sizeof(ack);
1062 iov[2].iov_base = acks;
1063 iov[2].iov_len = ack.nAcks;
1064 iov[3].iov_base = &pad;
1066 iov[4].iov_base = &ackinfo;
1067 iov[4].iov_len = sizeof(ackinfo);
1069 switch (ack.reason) {
1070 case RXRPC_ACK_REQUESTED:
1071 case RXRPC_ACK_DUPLICATE:
1072 case RXRPC_ACK_OUT_OF_SEQUENCE:
1073 case RXRPC_ACK_EXCEEDS_WINDOW:
1074 case RXRPC_ACK_NOSPACE:
1075 case RXRPC_ACK_PING:
1076 case RXRPC_ACK_PING_RESPONSE:
1077 goto send_ACK_with_skew;
1078 case RXRPC_ACK_DELAY:
1079 case RXRPC_ACK_IDLE:
1084 /* handle completion of security negotiations on an incoming
1086 if (test_and_clear_bit(RXRPC_CALL_EV_SECURED, &call->events)) {
1088 spin_lock_bh(&call->lock);
1090 if (call->state == RXRPC_CALL_SERVER_SECURING) {
1092 write_lock(&call->socket->call_lock);
1093 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1094 !test_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
1095 _debug("not released");
1096 call->state = RXRPC_CALL_SERVER_ACCEPTING;
1097 list_move_tail(&call->accept_link,
1098 &call->socket->acceptq);
1100 write_unlock(&call->socket->call_lock);
1101 read_lock(&call->state_lock);
1102 if (call->state < RXRPC_CALL_COMPLETE)
1103 set_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events);
1104 read_unlock(&call->state_lock);
1107 spin_unlock_bh(&call->lock);
1108 if (!test_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events))
1109 goto maybe_reschedule;
1112 /* post a notification of an acceptable connection to the app */
1113 if (test_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events)) {
1114 _debug("post accept");
1115 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NEW_CALL,
1118 clear_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events);
1119 goto maybe_reschedule;
1122 /* handle incoming call acceptance */
1123 if (test_and_clear_bit(RXRPC_CALL_EV_ACCEPTED, &call->events)) {
1125 ASSERTCMP(call->rx_data_post, ==, 0);
1126 call->rx_data_post = 1;
1127 read_lock_bh(&call->state_lock);
1128 if (call->state < RXRPC_CALL_COMPLETE)
1129 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events);
1130 read_unlock_bh(&call->state_lock);
1133 /* drain the out of sequence received packet queue into the packet Rx
1135 if (test_and_clear_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events)) {
1136 while (call->rx_data_post == call->rx_first_oos)
1137 if (rxrpc_drain_rx_oos_queue(call) < 0)
1139 goto maybe_reschedule;
1142 if (test_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
1143 rxrpc_release_call(call);
1144 clear_bit(RXRPC_CALL_EV_RELEASE, &call->events);
1147 /* other events may have been raised since we started checking */
1148 goto maybe_reschedule;
1151 ack.maxSkew = htons(atomic_read(&call->conn->hi_serial) -
1154 mtu = call->conn->params.peer->if_mtu;
1155 mtu -= call->conn->params.peer->hdrsize;
1156 ackinfo.maxMTU = htonl(mtu);
1157 ackinfo.rwind = htonl(rxrpc_rx_window_size);
1159 /* permit the peer to send us jumbo packets if it wants to */
1160 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
1161 ackinfo.jumbo_max = htonl(rxrpc_rx_jumbo_max);
1163 serial = atomic_inc_return(&call->conn->serial);
1164 whdr.serial = htonl(serial);
1165 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
1168 ntohl(ack.firstPacket),
1169 ntohl(ack.previousPacket),
1171 rxrpc_acks(ack.reason),
1174 del_timer_sync(&call->ack_timer);
1176 set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags);
1177 goto send_message_2;
1180 _debug("send message");
1182 serial = atomic_inc_return(&call->conn->serial);
1183 whdr.serial = htonl(serial);
1184 _proto("Tx %s %%%u", rxrpc_pkts[whdr.type], serial);
1187 len = iov[0].iov_len;
1189 if (iov[4].iov_len) {
1191 len += iov[4].iov_len;
1192 len += iov[3].iov_len;
1193 len += iov[2].iov_len;
1194 len += iov[1].iov_len;
1195 } else if (iov[3].iov_len) {
1197 len += iov[3].iov_len;
1198 len += iov[2].iov_len;
1199 len += iov[1].iov_len;
1200 } else if (iov[2].iov_len) {
1202 len += iov[2].iov_len;
1203 len += iov[1].iov_len;
1204 } else if (iov[1].iov_len) {
1206 len += iov[1].iov_len;
1209 ret = kernel_sendmsg(call->conn->params.local->socket,
1210 &msg, iov, ioc, len);
1212 _debug("sendmsg failed: %d", ret);
1213 read_lock_bh(&call->state_lock);
1214 if (call->state < RXRPC_CALL_DEAD)
1215 rxrpc_queue_call(call);
1216 read_unlock_bh(&call->state_lock);
1221 case RXRPC_CALL_EV_ABORT:
1222 clear_bit(genbit, &call->events);
1223 clear_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
1226 case RXRPC_CALL_EV_ACK_FINAL:
1227 write_lock_bh(&call->state_lock);
1228 if (call->state == RXRPC_CALL_CLIENT_FINAL_ACK)
1229 call->state = RXRPC_CALL_COMPLETE;
1230 write_unlock_bh(&call->state_lock);
1234 clear_bit(genbit, &call->events);
1235 switch (call->state) {
1236 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
1237 case RXRPC_CALL_CLIENT_RECV_REPLY:
1238 case RXRPC_CALL_SERVER_RECV_REQUEST:
1239 case RXRPC_CALL_SERVER_ACK_REQUEST:
1240 _debug("start ACK timer");
1241 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY,
1242 call->ackr_serial, false);
1246 goto maybe_reschedule;
1250 del_timer_sync(&call->ack_timer);
1251 if (test_and_clear_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events))
1252 rxrpc_put_call(call);
1253 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
1256 if (call->events || !skb_queue_empty(&call->rx_queue)) {
1257 read_lock_bh(&call->state_lock);
1258 if (call->state < RXRPC_CALL_DEAD)
1259 rxrpc_queue_call(call);
1260 read_unlock_bh(&call->state_lock);
1263 /* don't leave aborted connections on the accept queue */
1264 if (call->state >= RXRPC_CALL_COMPLETE &&
1265 !list_empty(&call->accept_link)) {
1266 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
1267 call, call->events, call->flags, call->conn->proto.cid);
1269 read_lock_bh(&call->state_lock);
1270 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1271 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
1272 rxrpc_queue_call(call);
1273 read_unlock_bh(&call->state_lock);
1277 clear_bit(RXRPC_CALL_PROC_BUSY, &call->flags);
1280 /* because we don't want two CPUs both processing the work item for one
1281 * call at the same time, we use a flag to note when it's busy; however
1282 * this means there's a race between clearing the flag and setting the
1283 * work pending bit and the work item being processed again */
1284 if (call->events && !work_pending(&call->processor)) {
1285 _debug("jumpstart %x", call->conn->proto.cid);
1286 rxrpc_queue_call(call);
1293 _debug("out of memory");
1294 goto maybe_reschedule;