1 /* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
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 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 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 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 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 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 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 __be32 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), ntohl(serial),
105 if (prior < rxrpc_ack_priority[call->ackr_reason]) {
111 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
113 if (prior == rxrpc_ack_priority[call->ackr_reason]) {
115 call->ackr_serial = serial;
121 call->ackr_reason = ack_reason;
122 call->ackr_serial = serial;
124 switch (ack_reason) {
125 case RXRPC_ACK_DELAY:
126 _debug("run delay timer");
127 expiry = rxrpc_soft_ack_delay;
132 _debug("run defer timer");
133 expiry = rxrpc_idle_ack_delay;
138 case RXRPC_ACK_REQUESTED:
139 expiry = rxrpc_requested_ack_delay;
142 if (!immediate || serial == cpu_to_be32(1)) {
143 _debug("run defer timer");
148 _debug("immediate ACK");
154 if (!timer_pending(&call->ack_timer) ||
155 time_after(call->ack_timer.expires, expiry))
156 mod_timer(&call->ack_timer, expiry);
160 _debug("cancel timer %%%u", ntohl(serial));
161 try_to_del_timer_sync(&call->ack_timer);
162 read_lock_bh(&call->state_lock);
163 if (call->state <= RXRPC_CALL_COMPLETE &&
164 !test_and_set_bit(RXRPC_CALL_ACK, &call->events))
165 rxrpc_queue_call(call);
166 read_unlock_bh(&call->state_lock);
170 * propose an ACK be sent, locking the call structure
172 void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
173 __be32 serial, bool immediate)
175 s8 prior = rxrpc_ack_priority[ack_reason];
177 if (prior > rxrpc_ack_priority[call->ackr_reason]) {
178 spin_lock_bh(&call->lock);
179 __rxrpc_propose_ACK(call, ack_reason, serial, immediate);
180 spin_unlock_bh(&call->lock);
185 * set the resend timer
187 static void rxrpc_set_resend(struct rxrpc_call *call, u8 resend,
188 unsigned long resend_at)
190 read_lock_bh(&call->state_lock);
191 if (call->state >= RXRPC_CALL_COMPLETE)
195 _debug("SET RESEND");
196 set_bit(RXRPC_CALL_RESEND, &call->events);
200 _debug("MODIFY RESEND TIMER");
201 set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
202 mod_timer(&call->resend_timer, resend_at);
204 _debug("KILL RESEND TIMER");
205 del_timer_sync(&call->resend_timer);
206 clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
207 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
209 read_unlock_bh(&call->state_lock);
215 static void rxrpc_resend(struct rxrpc_call *call)
217 struct rxrpc_skb_priv *sp;
218 struct rxrpc_header *hdr;
220 unsigned long *p_txb, resend_at;
225 _enter("{%d,%d,%d,%d},",
226 call->acks_hard, call->acks_unacked,
227 atomic_read(&call->sequence),
228 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
234 for (loop = call->acks_tail;
235 loop != call->acks_head || stop;
236 loop = (loop + 1) & (call->acks_winsz - 1)
238 p_txb = call->acks_window + loop;
239 smp_read_barrier_depends();
243 txb = (struct sk_buff *) *p_txb;
246 if (sp->need_resend) {
247 sp->need_resend = false;
249 /* each Tx packet has a new serial number */
251 htonl(atomic_inc_return(&call->conn->serial));
253 hdr = (struct rxrpc_header *) txb->head;
254 hdr->serial = sp->hdr.serial;
256 _proto("Tx DATA %%%u { #%d }",
257 ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
258 if (rxrpc_send_packet(call->conn->trans, txb) < 0) {
260 sp->resend_at = jiffies + 3;
263 jiffies + rxrpc_resend_timeout;
267 if (time_after_eq(jiffies + 1, sp->resend_at)) {
268 sp->need_resend = true;
270 } else if (resend & 2) {
271 if (time_before(sp->resend_at, resend_at))
272 resend_at = sp->resend_at;
274 resend_at = sp->resend_at;
279 rxrpc_set_resend(call, resend, resend_at);
284 * handle resend timer expiry
286 static void rxrpc_resend_timer(struct rxrpc_call *call)
288 struct rxrpc_skb_priv *sp;
290 unsigned long *p_txb, resend_at;
295 call->acks_tail, call->acks_unacked, call->acks_head);
297 if (call->state >= RXRPC_CALL_COMPLETE)
303 for (loop = call->acks_unacked;
304 loop != call->acks_head;
305 loop = (loop + 1) & (call->acks_winsz - 1)
307 p_txb = call->acks_window + loop;
308 smp_read_barrier_depends();
309 txb = (struct sk_buff *) (*p_txb & ~1);
312 ASSERT(!(*p_txb & 1));
314 if (sp->need_resend) {
316 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
317 sp->need_resend = true;
319 } else if (resend & 2) {
320 if (time_before(sp->resend_at, resend_at))
321 resend_at = sp->resend_at;
323 resend_at = sp->resend_at;
328 rxrpc_set_resend(call, resend, resend_at);
333 * process soft ACKs of our transmitted packets
334 * - these indicate packets the peer has or has not received, but hasn't yet
335 * given to the consumer, and so can still be discarded and re-requested
337 static int rxrpc_process_soft_ACKs(struct rxrpc_call *call,
338 struct rxrpc_ackpacket *ack,
341 struct rxrpc_skb_priv *sp;
343 unsigned long *p_txb, resend_at;
345 u8 sacks[RXRPC_MAXACKS], resend;
347 _enter("{%d,%d},{%d},",
349 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz),
352 if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0)
357 for (loop = 0; loop < ack->nAcks; loop++) {
358 p_txb = call->acks_window;
359 p_txb += (call->acks_tail + loop) & (call->acks_winsz - 1);
360 smp_read_barrier_depends();
361 txb = (struct sk_buff *) (*p_txb & ~1);
364 switch (sacks[loop]) {
365 case RXRPC_ACK_TYPE_ACK:
366 sp->need_resend = false;
369 case RXRPC_ACK_TYPE_NACK:
370 sp->need_resend = true;
375 _debug("Unsupported ACK type %d", sacks[loop]);
381 call->acks_unacked = (call->acks_tail + loop) & (call->acks_winsz - 1);
383 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
384 * have been received or processed yet by the far end */
385 for (loop = call->acks_unacked;
386 loop != call->acks_head;
387 loop = (loop + 1) & (call->acks_winsz - 1)
389 p_txb = call->acks_window + loop;
390 smp_read_barrier_depends();
391 txb = (struct sk_buff *) (*p_txb & ~1);
395 /* packet must have been discarded */
396 sp->need_resend = true;
399 } else if (sp->need_resend) {
401 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
402 sp->need_resend = true;
404 } else if (resend & 2) {
405 if (time_before(sp->resend_at, resend_at))
406 resend_at = sp->resend_at;
408 resend_at = sp->resend_at;
413 rxrpc_set_resend(call, resend, resend_at);
418 _leave(" = -EPROTO");
423 * discard hard-ACK'd packets from the Tx window
425 static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard)
428 int tail = call->acks_tail, old_tail;
429 int win = CIRC_CNT(call->acks_head, tail, call->acks_winsz);
431 _enter("{%u,%u},%u", call->acks_hard, win, hard);
433 ASSERTCMP(hard - call->acks_hard, <=, win);
435 while (call->acks_hard < hard) {
436 smp_read_barrier_depends();
437 _skb = call->acks_window[tail] & ~1;
438 rxrpc_free_skb((struct sk_buff *) _skb);
440 tail = (tail + 1) & (call->acks_winsz - 1);
441 call->acks_tail = tail;
442 if (call->acks_unacked == old_tail)
443 call->acks_unacked = tail;
447 wake_up(&call->tx_waitq);
451 * clear the Tx window in the event of a failure
453 static void rxrpc_clear_tx_window(struct rxrpc_call *call)
455 rxrpc_rotate_tx_window(call, atomic_read(&call->sequence));
459 * drain the out of sequence received packet queue into the packet Rx queue
461 static int rxrpc_drain_rx_oos_queue(struct rxrpc_call *call)
463 struct rxrpc_skb_priv *sp;
468 _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos);
470 spin_lock_bh(&call->lock);
473 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
474 goto socket_unavailable;
476 skb = skb_dequeue(&call->rx_oos_queue);
480 _debug("drain OOS packet %d [%d]",
481 ntohl(sp->hdr.seq), call->rx_first_oos);
483 if (ntohl(sp->hdr.seq) != call->rx_first_oos) {
484 skb_queue_head(&call->rx_oos_queue, skb);
485 call->rx_first_oos = ntohl(rxrpc_skb(skb)->hdr.seq);
486 _debug("requeue %p {%u}", skb, call->rx_first_oos);
488 skb->mark = RXRPC_SKB_MARK_DATA;
489 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
490 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
491 ret = rxrpc_queue_rcv_skb(call, skb, true, terminal);
493 _debug("drain #%u", call->rx_data_post);
494 call->rx_data_post++;
496 /* find out what the next packet is */
497 skb = skb_peek(&call->rx_oos_queue);
500 ntohl(rxrpc_skb(skb)->hdr.seq);
502 call->rx_first_oos = 0;
503 _debug("peek %p {%u}", skb, call->rx_first_oos);
509 spin_unlock_bh(&call->lock);
510 _leave(" = %d", ret);
515 * insert an out of sequence packet into the buffer
517 static void rxrpc_insert_oos_packet(struct rxrpc_call *call,
520 struct rxrpc_skb_priv *sp, *psp;
525 seq = ntohl(sp->hdr.seq);
526 _enter(",,{%u}", seq);
528 skb->destructor = rxrpc_packet_destructor;
529 ASSERTCMP(sp->call, ==, NULL);
531 rxrpc_get_call(call);
533 /* insert into the buffer in sequence order */
534 spin_lock_bh(&call->lock);
536 skb_queue_walk(&call->rx_oos_queue, p) {
538 if (ntohl(psp->hdr.seq) > seq) {
539 _debug("insert oos #%u before #%u",
540 seq, ntohl(psp->hdr.seq));
541 skb_insert(p, skb, &call->rx_oos_queue);
546 _debug("append oos #%u", seq);
547 skb_queue_tail(&call->rx_oos_queue, skb);
550 /* we might now have a new front to the queue */
551 if (call->rx_first_oos == 0 || seq < call->rx_first_oos)
552 call->rx_first_oos = seq;
554 read_lock(&call->state_lock);
555 if (call->state < RXRPC_CALL_COMPLETE &&
556 call->rx_data_post == call->rx_first_oos) {
557 _debug("drain rx oos now");
558 set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events);
560 read_unlock(&call->state_lock);
562 spin_unlock_bh(&call->lock);
563 _leave(" [stored #%u]", call->rx_first_oos);
567 * clear the Tx window on final ACK reception
569 static void rxrpc_zap_tx_window(struct rxrpc_call *call)
571 struct rxrpc_skb_priv *sp;
573 unsigned long _skb, *acks_window;
574 u8 winsz = call->acks_winsz;
577 acks_window = call->acks_window;
578 call->acks_window = NULL;
580 while (CIRC_CNT(call->acks_head, call->acks_tail, winsz) > 0) {
581 tail = call->acks_tail;
582 smp_read_barrier_depends();
583 _skb = acks_window[tail] & ~1;
585 call->acks_tail = (call->acks_tail + 1) & (winsz - 1);
587 skb = (struct sk_buff *) _skb;
589 _debug("+++ clear Tx %u", ntohl(sp->hdr.seq));
597 * process the extra information that may be appended to an ACK packet
599 static void rxrpc_extract_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
600 unsigned int latest, int nAcks)
602 struct rxrpc_ackinfo ackinfo;
603 struct rxrpc_peer *peer;
606 if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) {
607 _leave(" [no ackinfo]");
611 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
613 ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU),
614 ntohl(ackinfo.rwind), ntohl(ackinfo.jumbo_max));
616 mtu = min(ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU));
618 peer = call->conn->trans->peer;
619 if (mtu < peer->maxdata) {
620 spin_lock_bh(&peer->lock);
622 peer->mtu = mtu + peer->hdrsize;
623 spin_unlock_bh(&peer->lock);
624 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
629 * process packets in the reception queue
631 static int rxrpc_process_rx_queue(struct rxrpc_call *call,
634 struct rxrpc_ackpacket ack;
635 struct rxrpc_skb_priv *sp;
644 skb = skb_dequeue(&call->rx_queue);
648 _net("deferred skb %p", skb);
652 _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state);
656 switch (sp->hdr.type) {
657 /* data packets that wind up here have been received out of
658 * order, need security processing or are jumbo packets */
659 case RXRPC_PACKET_TYPE_DATA:
660 _proto("OOSQ DATA %%%u { #%u }",
661 ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
663 /* secured packets must be verified and possibly decrypted */
664 if (rxrpc_verify_packet(call, skb, _abort_code) < 0)
667 rxrpc_insert_oos_packet(call, skb);
668 goto process_further;
670 /* partial ACK to process */
671 case RXRPC_PACKET_TYPE_ACK:
672 if (skb_copy_bits(skb, 0, &ack, sizeof(ack)) < 0) {
673 _debug("extraction failure");
676 if (!skb_pull(skb, sizeof(ack)))
679 latest = ntohl(sp->hdr.serial);
680 hard = ntohl(ack.firstPacket);
681 tx = atomic_read(&call->sequence);
683 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
687 ntohl(ack.previousPacket),
689 rxrpc_acks(ack.reason),
692 rxrpc_extract_ackinfo(call, skb, latest, ack.nAcks);
694 if (ack.reason == RXRPC_ACK_PING) {
695 _proto("Rx ACK %%%u PING Request", latest);
696 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
697 sp->hdr.serial, true);
700 /* discard any out-of-order or duplicate ACKs */
701 if (latest - call->acks_latest <= 0) {
702 _debug("discard ACK %d <= %d",
703 latest, call->acks_latest);
706 call->acks_latest = latest;
708 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
709 call->state != RXRPC_CALL_CLIENT_AWAIT_REPLY &&
710 call->state != RXRPC_CALL_SERVER_SEND_REPLY &&
711 call->state != RXRPC_CALL_SERVER_AWAIT_ACK)
714 _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state);
718 _debug("hard-ACK'd packet %d not transmitted"
724 if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY ||
725 call->state == RXRPC_CALL_SERVER_AWAIT_ACK) &&
730 rxrpc_rotate_tx_window(call, hard - 1);
734 if (hard - 1 + ack.nAcks > tx) {
735 _debug("soft-ACK'd packet %d+%d not"
736 " transmitted (%d top)",
737 hard - 1, ack.nAcks, tx);
741 if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0)
746 /* complete ACK to process */
747 case RXRPC_PACKET_TYPE_ACKALL:
750 /* abort and busy are handled elsewhere */
751 case RXRPC_PACKET_TYPE_BUSY:
752 case RXRPC_PACKET_TYPE_ABORT:
755 /* connection level events - also handled elsewhere */
756 case RXRPC_PACKET_TYPE_CHALLENGE:
757 case RXRPC_PACKET_TYPE_RESPONSE:
758 case RXRPC_PACKET_TYPE_DEBUG:
762 /* if we've had a hard ACK that covers all the packets we've sent, then
763 * that ends that phase of the operation */
765 write_lock_bh(&call->state_lock);
766 _debug("ack all %d", call->state);
768 switch (call->state) {
769 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
770 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
772 case RXRPC_CALL_SERVER_AWAIT_ACK:
773 _debug("srv complete");
774 call->state = RXRPC_CALL_COMPLETE;
777 case RXRPC_CALL_CLIENT_SEND_REQUEST:
778 case RXRPC_CALL_SERVER_RECV_REQUEST:
779 goto protocol_error_unlock; /* can't occur yet */
781 write_unlock_bh(&call->state_lock);
782 goto discard; /* assume packet left over from earlier phase */
785 write_unlock_bh(&call->state_lock);
787 /* if all the packets we sent are hard-ACK'd, then we can discard
788 * whatever we've got left */
789 _debug("clear Tx %d",
790 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
792 del_timer_sync(&call->resend_timer);
793 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
794 clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
796 if (call->acks_window)
797 rxrpc_zap_tx_window(call);
800 /* post the final ACK message for userspace to pick up */
802 skb->mark = RXRPC_SKB_MARK_FINAL_ACK;
804 rxrpc_get_call(call);
805 spin_lock_bh(&call->lock);
806 if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0)
808 spin_unlock_bh(&call->lock);
809 goto process_further;
814 goto process_further;
816 protocol_error_unlock:
817 write_unlock_bh(&call->state_lock);
820 _leave(" = -EPROTO");
825 * post a message to the socket Rx queue for recvmsg() to pick up
827 static int rxrpc_post_message(struct rxrpc_call *call, u32 mark, u32 error,
830 struct rxrpc_skb_priv *sp;
834 _enter("{%d,%lx},%u,%u,%d",
835 call->debug_id, call->flags, mark, error, fatal);
837 /* remove timers and things for fatal messages */
839 del_timer_sync(&call->resend_timer);
840 del_timer_sync(&call->ack_timer);
841 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
844 if (mark != RXRPC_SKB_MARK_NEW_CALL &&
845 !test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
846 _leave("[no userid]");
850 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
851 skb = alloc_skb(0, GFP_NOFS);
860 memset(sp, 0, sizeof(*sp));
863 rxrpc_get_call(call);
865 spin_lock_bh(&call->lock);
866 ret = rxrpc_queue_rcv_skb(call, skb, true, fatal);
867 spin_unlock_bh(&call->lock);
875 * handle background processing of incoming call packets and ACK / abort
878 void rxrpc_process_call(struct work_struct *work)
880 struct rxrpc_call *call =
881 container_of(work, struct rxrpc_call, processor);
882 struct rxrpc_ackpacket ack;
883 struct rxrpc_ackinfo ackinfo;
884 struct rxrpc_header hdr;
890 int genbit, loop, nbit, ioc, ret, mtu;
891 u32 abort_code = RX_PROTOCOL_ERROR;
894 //printk("\n--------------------\n");
895 _enter("{%d,%s,%lx} [%lu]",
896 call->debug_id, rxrpc_call_states[call->state], call->events,
897 (jiffies - call->creation_jif) / (HZ / 10));
899 if (test_and_set_bit(RXRPC_CALL_PROC_BUSY, &call->flags)) {
900 _debug("XXXXXXXXXXXXX RUNNING ON MULTIPLE CPUS XXXXXXXXXXXXX");
904 /* there's a good chance we're going to have to send a message, so set
905 * one up in advance */
906 msg.msg_name = &call->conn->trans->peer->srx.transport.sin;
907 msg.msg_namelen = sizeof(call->conn->trans->peer->srx.transport.sin);
908 msg.msg_control = NULL;
909 msg.msg_controllen = 0;
912 hdr.epoch = call->conn->epoch;
914 hdr.callNumber = call->call_id;
916 hdr.type = RXRPC_PACKET_TYPE_ACK;
917 hdr.flags = call->conn->out_clientflag;
919 hdr.securityIndex = call->conn->security_ix;
921 hdr.serviceId = call->conn->service_id;
923 memset(iov, 0, sizeof(iov));
924 iov[0].iov_base = &hdr;
925 iov[0].iov_len = sizeof(hdr);
927 /* deal with events of a final nature */
928 if (test_bit(RXRPC_CALL_RELEASE, &call->events)) {
929 rxrpc_release_call(call);
930 clear_bit(RXRPC_CALL_RELEASE, &call->events);
933 if (test_bit(RXRPC_CALL_RCVD_ERROR, &call->events)) {
936 clear_bit(RXRPC_CALL_CONN_ABORT, &call->events);
937 clear_bit(RXRPC_CALL_REJECT_BUSY, &call->events);
938 clear_bit(RXRPC_CALL_ABORT, &call->events);
940 error = call->conn->trans->peer->net_error;
941 _debug("post net error %d", error);
943 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NET_ERROR,
946 clear_bit(RXRPC_CALL_RCVD_ERROR, &call->events);
950 if (test_bit(RXRPC_CALL_CONN_ABORT, &call->events)) {
951 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
953 clear_bit(RXRPC_CALL_REJECT_BUSY, &call->events);
954 clear_bit(RXRPC_CALL_ABORT, &call->events);
956 _debug("post conn abort");
958 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
959 call->conn->error, true) < 0)
961 clear_bit(RXRPC_CALL_CONN_ABORT, &call->events);
965 if (test_bit(RXRPC_CALL_REJECT_BUSY, &call->events)) {
966 hdr.type = RXRPC_PACKET_TYPE_BUSY;
967 genbit = RXRPC_CALL_REJECT_BUSY;
971 if (test_bit(RXRPC_CALL_ABORT, &call->events)) {
972 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
974 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
975 ECONNABORTED, true) < 0)
977 hdr.type = RXRPC_PACKET_TYPE_ABORT;
978 data = htonl(call->abort_code);
979 iov[1].iov_base = &data;
980 iov[1].iov_len = sizeof(data);
981 genbit = RXRPC_CALL_ABORT;
985 if (test_bit(RXRPC_CALL_ACK_FINAL, &call->events)) {
986 genbit = RXRPC_CALL_ACK_FINAL;
988 ack.bufferSpace = htons(8);
991 ack.reason = RXRPC_ACK_IDLE;
993 call->ackr_reason = 0;
995 spin_lock_bh(&call->lock);
996 ack.serial = call->ackr_serial;
997 ack.previousPacket = call->ackr_prev_seq;
998 ack.firstPacket = htonl(call->rx_data_eaten + 1);
999 spin_unlock_bh(&call->lock);
1003 iov[1].iov_base = &ack;
1004 iov[1].iov_len = sizeof(ack);
1005 iov[2].iov_base = &pad;
1007 iov[3].iov_base = &ackinfo;
1008 iov[3].iov_len = sizeof(ackinfo);
1012 if (call->events & ((1 << RXRPC_CALL_RCVD_BUSY) |
1013 (1 << RXRPC_CALL_RCVD_ABORT))
1017 if (test_bit(RXRPC_CALL_RCVD_ABORT, &call->events))
1018 mark = RXRPC_SKB_MARK_REMOTE_ABORT;
1020 mark = RXRPC_SKB_MARK_BUSY;
1022 _debug("post abort/busy");
1023 rxrpc_clear_tx_window(call);
1024 if (rxrpc_post_message(call, mark, ECONNABORTED, true) < 0)
1027 clear_bit(RXRPC_CALL_RCVD_BUSY, &call->events);
1028 clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
1032 if (test_and_clear_bit(RXRPC_CALL_RCVD_ACKALL, &call->events)) {
1033 _debug("do implicit ackall");
1034 rxrpc_clear_tx_window(call);
1037 if (test_bit(RXRPC_CALL_LIFE_TIMER, &call->events)) {
1038 write_lock_bh(&call->state_lock);
1039 if (call->state <= RXRPC_CALL_COMPLETE) {
1040 call->state = RXRPC_CALL_LOCALLY_ABORTED;
1041 call->abort_code = RX_CALL_TIMEOUT;
1042 set_bit(RXRPC_CALL_ABORT, &call->events);
1044 write_unlock_bh(&call->state_lock);
1046 _debug("post timeout");
1047 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
1051 clear_bit(RXRPC_CALL_LIFE_TIMER, &call->events);
1055 /* deal with assorted inbound messages */
1056 if (!skb_queue_empty(&call->rx_queue)) {
1057 switch (rxrpc_process_rx_queue(call, &abort_code)) {
1066 rxrpc_abort_call(call, abort_code);
1071 /* handle resending */
1072 if (test_and_clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events))
1073 rxrpc_resend_timer(call);
1074 if (test_and_clear_bit(RXRPC_CALL_RESEND, &call->events))
1077 /* consider sending an ordinary ACK */
1078 if (test_bit(RXRPC_CALL_ACK, &call->events)) {
1079 _debug("send ACK: window: %d - %d { %lx }",
1080 call->rx_data_eaten, call->ackr_win_top,
1081 call->ackr_window[0]);
1083 if (call->state > RXRPC_CALL_SERVER_ACK_REQUEST &&
1084 call->ackr_reason != RXRPC_ACK_PING_RESPONSE) {
1085 /* ACK by sending reply DATA packet in this state */
1086 clear_bit(RXRPC_CALL_ACK, &call->events);
1087 goto maybe_reschedule;
1090 genbit = RXRPC_CALL_ACK;
1092 acks = kzalloc(call->ackr_win_top - call->rx_data_eaten,
1097 //hdr.flags = RXRPC_SLOW_START_OK;
1098 ack.bufferSpace = htons(8);
1103 spin_lock_bh(&call->lock);
1104 ack.reason = call->ackr_reason;
1105 ack.serial = call->ackr_serial;
1106 ack.previousPacket = call->ackr_prev_seq;
1107 ack.firstPacket = htonl(call->rx_data_eaten + 1);
1110 for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) {
1111 nbit = loop * BITS_PER_LONG;
1112 for (bits = call->ackr_window[loop]; bits; bits >>= 1
1114 _debug("- l=%d n=%d b=%lx", loop, nbit, bits);
1116 acks[nbit] = RXRPC_ACK_TYPE_ACK;
1117 ack.nAcks = nbit + 1;
1122 call->ackr_reason = 0;
1123 spin_unlock_bh(&call->lock);
1127 iov[1].iov_base = &ack;
1128 iov[1].iov_len = sizeof(ack);
1129 iov[2].iov_base = acks;
1130 iov[2].iov_len = ack.nAcks;
1131 iov[3].iov_base = &pad;
1133 iov[4].iov_base = &ackinfo;
1134 iov[4].iov_len = sizeof(ackinfo);
1136 switch (ack.reason) {
1137 case RXRPC_ACK_REQUESTED:
1138 case RXRPC_ACK_DUPLICATE:
1139 case RXRPC_ACK_OUT_OF_SEQUENCE:
1140 case RXRPC_ACK_EXCEEDS_WINDOW:
1141 case RXRPC_ACK_NOSPACE:
1142 case RXRPC_ACK_PING:
1143 case RXRPC_ACK_PING_RESPONSE:
1144 goto send_ACK_with_skew;
1145 case RXRPC_ACK_DELAY:
1146 case RXRPC_ACK_IDLE:
1151 /* handle completion of security negotiations on an incoming
1153 if (test_and_clear_bit(RXRPC_CALL_SECURED, &call->events)) {
1155 spin_lock_bh(&call->lock);
1157 if (call->state == RXRPC_CALL_SERVER_SECURING) {
1159 write_lock(&call->conn->lock);
1160 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1161 !test_bit(RXRPC_CALL_RELEASE, &call->events)) {
1162 _debug("not released");
1163 call->state = RXRPC_CALL_SERVER_ACCEPTING;
1164 list_move_tail(&call->accept_link,
1165 &call->socket->acceptq);
1167 write_unlock(&call->conn->lock);
1168 read_lock(&call->state_lock);
1169 if (call->state < RXRPC_CALL_COMPLETE)
1170 set_bit(RXRPC_CALL_POST_ACCEPT, &call->events);
1171 read_unlock(&call->state_lock);
1174 spin_unlock_bh(&call->lock);
1175 if (!test_bit(RXRPC_CALL_POST_ACCEPT, &call->events))
1176 goto maybe_reschedule;
1179 /* post a notification of an acceptable connection to the app */
1180 if (test_bit(RXRPC_CALL_POST_ACCEPT, &call->events)) {
1181 _debug("post accept");
1182 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NEW_CALL,
1185 clear_bit(RXRPC_CALL_POST_ACCEPT, &call->events);
1186 goto maybe_reschedule;
1189 /* handle incoming call acceptance */
1190 if (test_and_clear_bit(RXRPC_CALL_ACCEPTED, &call->events)) {
1192 ASSERTCMP(call->rx_data_post, ==, 0);
1193 call->rx_data_post = 1;
1194 read_lock_bh(&call->state_lock);
1195 if (call->state < RXRPC_CALL_COMPLETE)
1196 set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events);
1197 read_unlock_bh(&call->state_lock);
1200 /* drain the out of sequence received packet queue into the packet Rx
1202 if (test_and_clear_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events)) {
1203 while (call->rx_data_post == call->rx_first_oos)
1204 if (rxrpc_drain_rx_oos_queue(call) < 0)
1206 goto maybe_reschedule;
1209 /* other events may have been raised since we started checking */
1210 goto maybe_reschedule;
1213 ack.maxSkew = htons(atomic_read(&call->conn->hi_serial) -
1216 mtu = call->conn->trans->peer->if_mtu;
1217 mtu -= call->conn->trans->peer->hdrsize;
1218 ackinfo.maxMTU = htonl(mtu);
1219 ackinfo.rwind = htonl(rxrpc_rx_window_size);
1221 /* permit the peer to send us jumbo packets if it wants to */
1222 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
1223 ackinfo.jumbo_max = htonl(rxrpc_rx_jumbo_max);
1225 hdr.serial = htonl(atomic_inc_return(&call->conn->serial));
1226 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
1229 ntohl(ack.firstPacket),
1230 ntohl(ack.previousPacket),
1232 rxrpc_acks(ack.reason),
1235 del_timer_sync(&call->ack_timer);
1237 set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags);
1238 goto send_message_2;
1241 _debug("send message");
1243 hdr.serial = htonl(atomic_inc_return(&call->conn->serial));
1244 _proto("Tx %s %%%u", rxrpc_pkts[hdr.type], ntohl(hdr.serial));
1247 len = iov[0].iov_len;
1249 if (iov[4].iov_len) {
1251 len += iov[4].iov_len;
1252 len += iov[3].iov_len;
1253 len += iov[2].iov_len;
1254 len += iov[1].iov_len;
1255 } else if (iov[3].iov_len) {
1257 len += iov[3].iov_len;
1258 len += iov[2].iov_len;
1259 len += iov[1].iov_len;
1260 } else if (iov[2].iov_len) {
1262 len += iov[2].iov_len;
1263 len += iov[1].iov_len;
1264 } else if (iov[1].iov_len) {
1266 len += iov[1].iov_len;
1269 ret = kernel_sendmsg(call->conn->trans->local->socket,
1270 &msg, iov, ioc, len);
1272 _debug("sendmsg failed: %d", ret);
1273 read_lock_bh(&call->state_lock);
1274 if (call->state < RXRPC_CALL_DEAD)
1275 rxrpc_queue_call(call);
1276 read_unlock_bh(&call->state_lock);
1281 case RXRPC_CALL_ABORT:
1282 clear_bit(genbit, &call->events);
1283 clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
1286 case RXRPC_CALL_ACK_FINAL:
1287 write_lock_bh(&call->state_lock);
1288 if (call->state == RXRPC_CALL_CLIENT_FINAL_ACK)
1289 call->state = RXRPC_CALL_COMPLETE;
1290 write_unlock_bh(&call->state_lock);
1294 clear_bit(genbit, &call->events);
1295 switch (call->state) {
1296 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
1297 case RXRPC_CALL_CLIENT_RECV_REPLY:
1298 case RXRPC_CALL_SERVER_RECV_REQUEST:
1299 case RXRPC_CALL_SERVER_ACK_REQUEST:
1300 _debug("start ACK timer");
1301 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY,
1302 call->ackr_serial, false);
1306 goto maybe_reschedule;
1310 del_timer_sync(&call->ack_timer);
1311 if (test_and_clear_bit(RXRPC_CALL_ACK_FINAL, &call->events))
1312 rxrpc_put_call(call);
1313 clear_bit(RXRPC_CALL_ACK, &call->events);
1316 if (call->events || !skb_queue_empty(&call->rx_queue)) {
1317 read_lock_bh(&call->state_lock);
1318 if (call->state < RXRPC_CALL_DEAD)
1319 rxrpc_queue_call(call);
1320 read_unlock_bh(&call->state_lock);
1323 /* don't leave aborted connections on the accept queue */
1324 if (call->state >= RXRPC_CALL_COMPLETE &&
1325 !list_empty(&call->accept_link)) {
1326 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
1327 call, call->events, call->flags,
1328 ntohl(call->conn->cid));
1330 read_lock_bh(&call->state_lock);
1331 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1332 !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
1333 rxrpc_queue_call(call);
1334 read_unlock_bh(&call->state_lock);
1338 clear_bit(RXRPC_CALL_PROC_BUSY, &call->flags);
1341 /* because we don't want two CPUs both processing the work item for one
1342 * call at the same time, we use a flag to note when it's busy; however
1343 * this means there's a race between clearing the flag and setting the
1344 * work pending bit and the work item being processed again */
1345 if (call->events && !work_pending(&call->processor)) {
1346 _debug("jumpstart %x", ntohl(call->conn->cid));
1347 rxrpc_queue_call(call);
1354 _debug("out of memory");
1355 goto maybe_reschedule;