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 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;
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 */
250 htonl(atomic_inc_return(&call->conn->serial));
252 hdr = (struct rxrpc_header *) txb->head;
253 hdr->serial = sp->hdr.serial;
255 _proto("Tx DATA %%%u { #%d }",
256 ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
257 if (rxrpc_send_packet(call->conn->trans, txb) < 0) {
259 sp->resend_at = jiffies + 3;
262 jiffies + rxrpc_resend_timeout * HZ;
266 if (time_after_eq(jiffies + 1, sp->resend_at)) {
267 sp->need_resend = true;
269 } else if (resend & 2) {
270 if (time_before(sp->resend_at, resend_at))
271 resend_at = sp->resend_at;
273 resend_at = sp->resend_at;
278 rxrpc_set_resend(call, resend, resend_at);
283 * handle resend timer expiry
285 static void rxrpc_resend_timer(struct rxrpc_call *call)
287 struct rxrpc_skb_priv *sp;
289 unsigned long *p_txb, resend_at;
294 call->acks_tail, call->acks_unacked, call->acks_head);
296 if (call->state >= RXRPC_CALL_COMPLETE)
302 for (loop = call->acks_unacked;
303 loop != call->acks_head;
304 loop = (loop + 1) & (call->acks_winsz - 1)
306 p_txb = call->acks_window + loop;
307 smp_read_barrier_depends();
308 txb = (struct sk_buff *) (*p_txb & ~1);
311 ASSERT(!(*p_txb & 1));
313 if (sp->need_resend) {
315 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
316 sp->need_resend = true;
318 } else if (resend & 2) {
319 if (time_before(sp->resend_at, resend_at))
320 resend_at = sp->resend_at;
322 resend_at = sp->resend_at;
327 rxrpc_set_resend(call, resend, resend_at);
332 * process soft ACKs of our transmitted packets
333 * - these indicate packets the peer has or has not received, but hasn't yet
334 * given to the consumer, and so can still be discarded and re-requested
336 static int rxrpc_process_soft_ACKs(struct rxrpc_call *call,
337 struct rxrpc_ackpacket *ack,
340 struct rxrpc_skb_priv *sp;
342 unsigned long *p_txb, resend_at;
344 u8 sacks[RXRPC_MAXACKS], resend;
346 _enter("{%d,%d},{%d},",
348 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz),
351 if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0)
356 for (loop = 0; loop < ack->nAcks; loop++) {
357 p_txb = call->acks_window;
358 p_txb += (call->acks_tail + loop) & (call->acks_winsz - 1);
359 smp_read_barrier_depends();
360 txb = (struct sk_buff *) (*p_txb & ~1);
363 switch (sacks[loop]) {
364 case RXRPC_ACK_TYPE_ACK:
365 sp->need_resend = false;
368 case RXRPC_ACK_TYPE_NACK:
369 sp->need_resend = true;
374 _debug("Unsupported ACK type %d", sacks[loop]);
380 call->acks_unacked = (call->acks_tail + loop) & (call->acks_winsz - 1);
382 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
383 * have been received or processed yet by the far end */
384 for (loop = call->acks_unacked;
385 loop != call->acks_head;
386 loop = (loop + 1) & (call->acks_winsz - 1)
388 p_txb = call->acks_window + loop;
389 smp_read_barrier_depends();
390 txb = (struct sk_buff *) (*p_txb & ~1);
394 /* packet must have been discarded */
395 sp->need_resend = true;
398 } else if (sp->need_resend) {
400 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
401 sp->need_resend = true;
403 } else if (resend & 2) {
404 if (time_before(sp->resend_at, resend_at))
405 resend_at = sp->resend_at;
407 resend_at = sp->resend_at;
412 rxrpc_set_resend(call, resend, resend_at);
417 _leave(" = -EPROTO");
422 * discard hard-ACK'd packets from the Tx window
424 static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard)
427 int tail = call->acks_tail, old_tail;
428 int win = CIRC_CNT(call->acks_head, tail, call->acks_winsz);
430 _enter("{%u,%u},%u", call->acks_hard, win, hard);
432 ASSERTCMP(hard - call->acks_hard, <=, win);
434 while (call->acks_hard < hard) {
435 smp_read_barrier_depends();
436 _skb = call->acks_window[tail] & ~1;
437 rxrpc_free_skb((struct sk_buff *) _skb);
439 tail = (tail + 1) & (call->acks_winsz - 1);
440 call->acks_tail = tail;
441 if (call->acks_unacked == old_tail)
442 call->acks_unacked = tail;
446 wake_up(&call->tx_waitq);
450 * clear the Tx window in the event of a failure
452 static void rxrpc_clear_tx_window(struct rxrpc_call *call)
454 rxrpc_rotate_tx_window(call, atomic_read(&call->sequence));
458 * drain the out of sequence received packet queue into the packet Rx queue
460 static int rxrpc_drain_rx_oos_queue(struct rxrpc_call *call)
462 struct rxrpc_skb_priv *sp;
467 _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos);
469 spin_lock_bh(&call->lock);
472 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
473 goto socket_unavailable;
475 skb = skb_dequeue(&call->rx_oos_queue);
479 _debug("drain OOS packet %d [%d]",
480 ntohl(sp->hdr.seq), call->rx_first_oos);
482 if (ntohl(sp->hdr.seq) != call->rx_first_oos) {
483 skb_queue_head(&call->rx_oos_queue, skb);
484 call->rx_first_oos = ntohl(rxrpc_skb(skb)->hdr.seq);
485 _debug("requeue %p {%u}", skb, call->rx_first_oos);
487 skb->mark = RXRPC_SKB_MARK_DATA;
488 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
489 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
490 ret = rxrpc_queue_rcv_skb(call, skb, true, terminal);
492 _debug("drain #%u", call->rx_data_post);
493 call->rx_data_post++;
495 /* find out what the next packet is */
496 skb = skb_peek(&call->rx_oos_queue);
499 ntohl(rxrpc_skb(skb)->hdr.seq);
501 call->rx_first_oos = 0;
502 _debug("peek %p {%u}", skb, call->rx_first_oos);
508 spin_unlock_bh(&call->lock);
509 _leave(" = %d", ret);
514 * insert an out of sequence packet into the buffer
516 static void rxrpc_insert_oos_packet(struct rxrpc_call *call,
519 struct rxrpc_skb_priv *sp, *psp;
524 seq = ntohl(sp->hdr.seq);
525 _enter(",,{%u}", seq);
527 skb->destructor = rxrpc_packet_destructor;
528 ASSERTCMP(sp->call, ==, NULL);
530 rxrpc_get_call(call);
532 /* insert into the buffer in sequence order */
533 spin_lock_bh(&call->lock);
535 skb_queue_walk(&call->rx_oos_queue, p) {
537 if (ntohl(psp->hdr.seq) > seq) {
538 _debug("insert oos #%u before #%u",
539 seq, ntohl(psp->hdr.seq));
540 skb_insert(p, skb, &call->rx_oos_queue);
545 _debug("append oos #%u", seq);
546 skb_queue_tail(&call->rx_oos_queue, skb);
549 /* we might now have a new front to the queue */
550 if (call->rx_first_oos == 0 || seq < call->rx_first_oos)
551 call->rx_first_oos = seq;
553 read_lock(&call->state_lock);
554 if (call->state < RXRPC_CALL_COMPLETE &&
555 call->rx_data_post == call->rx_first_oos) {
556 _debug("drain rx oos now");
557 set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events);
559 read_unlock(&call->state_lock);
561 spin_unlock_bh(&call->lock);
562 _leave(" [stored #%u]", call->rx_first_oos);
566 * clear the Tx window on final ACK reception
568 static void rxrpc_zap_tx_window(struct rxrpc_call *call)
570 struct rxrpc_skb_priv *sp;
572 unsigned long _skb, *acks_window;
573 u8 winsz = call->acks_winsz;
576 acks_window = call->acks_window;
577 call->acks_window = NULL;
579 while (CIRC_CNT(call->acks_head, call->acks_tail, winsz) > 0) {
580 tail = call->acks_tail;
581 smp_read_barrier_depends();
582 _skb = acks_window[tail] & ~1;
584 call->acks_tail = (call->acks_tail + 1) & (winsz - 1);
586 skb = (struct sk_buff *) _skb;
588 _debug("+++ clear Tx %u", ntohl(sp->hdr.seq));
596 * process the extra information that may be appended to an ACK packet
598 static void rxrpc_extract_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
599 unsigned int latest, int nAcks)
601 struct rxrpc_ackinfo ackinfo;
602 struct rxrpc_peer *peer;
605 if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) {
606 _leave(" [no ackinfo]");
610 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
612 ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU),
613 ntohl(ackinfo.rwind), ntohl(ackinfo.jumbo_max));
615 mtu = min(ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU));
617 peer = call->conn->trans->peer;
618 if (mtu < peer->maxdata) {
619 spin_lock_bh(&peer->lock);
621 peer->mtu = mtu + peer->hdrsize;
622 spin_unlock_bh(&peer->lock);
623 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
628 * process packets in the reception queue
630 static int rxrpc_process_rx_queue(struct rxrpc_call *call,
633 struct rxrpc_ackpacket ack;
634 struct rxrpc_skb_priv *sp;
643 skb = skb_dequeue(&call->rx_queue);
647 _net("deferred skb %p", skb);
651 _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state);
655 switch (sp->hdr.type) {
656 /* data packets that wind up here have been received out of
657 * order, need security processing or are jumbo packets */
658 case RXRPC_PACKET_TYPE_DATA:
659 _proto("OOSQ DATA %%%u { #%u }",
660 ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
662 /* secured packets must be verified and possibly decrypted */
663 if (rxrpc_verify_packet(call, skb, _abort_code) < 0)
666 rxrpc_insert_oos_packet(call, skb);
667 goto process_further;
669 /* partial ACK to process */
670 case RXRPC_PACKET_TYPE_ACK:
671 if (skb_copy_bits(skb, 0, &ack, sizeof(ack)) < 0) {
672 _debug("extraction failure");
675 if (!skb_pull(skb, sizeof(ack)))
678 latest = ntohl(sp->hdr.serial);
679 hard = ntohl(ack.firstPacket);
680 tx = atomic_read(&call->sequence);
682 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
686 ntohl(ack.previousPacket),
688 rxrpc_acks(ack.reason),
691 rxrpc_extract_ackinfo(call, skb, latest, ack.nAcks);
693 if (ack.reason == RXRPC_ACK_PING) {
694 _proto("Rx ACK %%%u PING Request", latest);
695 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
696 sp->hdr.serial, true);
699 /* discard any out-of-order or duplicate ACKs */
700 if (latest - call->acks_latest <= 0) {
701 _debug("discard ACK %d <= %d",
702 latest, call->acks_latest);
705 call->acks_latest = latest;
707 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
708 call->state != RXRPC_CALL_CLIENT_AWAIT_REPLY &&
709 call->state != RXRPC_CALL_SERVER_SEND_REPLY &&
710 call->state != RXRPC_CALL_SERVER_AWAIT_ACK)
713 _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state);
717 _debug("hard-ACK'd packet %d not transmitted"
723 if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY ||
724 call->state == RXRPC_CALL_SERVER_AWAIT_ACK) &&
729 rxrpc_rotate_tx_window(call, hard - 1);
733 if (hard - 1 + ack.nAcks > tx) {
734 _debug("soft-ACK'd packet %d+%d not"
735 " transmitted (%d top)",
736 hard - 1, ack.nAcks, tx);
740 if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0)
745 /* complete ACK to process */
746 case RXRPC_PACKET_TYPE_ACKALL:
749 /* abort and busy are handled elsewhere */
750 case RXRPC_PACKET_TYPE_BUSY:
751 case RXRPC_PACKET_TYPE_ABORT:
754 /* connection level events - also handled elsewhere */
755 case RXRPC_PACKET_TYPE_CHALLENGE:
756 case RXRPC_PACKET_TYPE_RESPONSE:
757 case RXRPC_PACKET_TYPE_DEBUG:
761 /* if we've had a hard ACK that covers all the packets we've sent, then
762 * that ends that phase of the operation */
764 write_lock_bh(&call->state_lock);
765 _debug("ack all %d", call->state);
767 switch (call->state) {
768 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
769 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
771 case RXRPC_CALL_SERVER_AWAIT_ACK:
772 _debug("srv complete");
773 call->state = RXRPC_CALL_COMPLETE;
776 case RXRPC_CALL_CLIENT_SEND_REQUEST:
777 case RXRPC_CALL_SERVER_RECV_REQUEST:
778 goto protocol_error_unlock; /* can't occur yet */
780 write_unlock_bh(&call->state_lock);
781 goto discard; /* assume packet left over from earlier phase */
784 write_unlock_bh(&call->state_lock);
786 /* if all the packets we sent are hard-ACK'd, then we can discard
787 * whatever we've got left */
788 _debug("clear Tx %d",
789 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
791 del_timer_sync(&call->resend_timer);
792 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
793 clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
795 if (call->acks_window)
796 rxrpc_zap_tx_window(call);
799 /* post the final ACK message for userspace to pick up */
801 skb->mark = RXRPC_SKB_MARK_FINAL_ACK;
803 rxrpc_get_call(call);
804 spin_lock_bh(&call->lock);
805 if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0)
807 spin_unlock_bh(&call->lock);
808 goto process_further;
813 goto process_further;
815 protocol_error_unlock:
816 write_unlock_bh(&call->state_lock);
819 _leave(" = -EPROTO");
824 * post a message to the socket Rx queue for recvmsg() to pick up
826 static int rxrpc_post_message(struct rxrpc_call *call, u32 mark, u32 error,
829 struct rxrpc_skb_priv *sp;
833 _enter("{%d,%lx},%u,%u,%d",
834 call->debug_id, call->flags, mark, error, fatal);
836 /* remove timers and things for fatal messages */
838 del_timer_sync(&call->resend_timer);
839 del_timer_sync(&call->ack_timer);
840 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
843 if (mark != RXRPC_SKB_MARK_NEW_CALL &&
844 !test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
845 _leave("[no userid]");
849 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
850 skb = alloc_skb(0, GFP_NOFS);
859 memset(sp, 0, sizeof(*sp));
862 rxrpc_get_call(call);
864 spin_lock_bh(&call->lock);
865 ret = rxrpc_queue_rcv_skb(call, skb, true, fatal);
866 spin_unlock_bh(&call->lock);
874 * handle background processing of incoming call packets and ACK / abort
877 void rxrpc_process_call(struct work_struct *work)
879 struct rxrpc_call *call =
880 container_of(work, struct rxrpc_call, processor);
881 struct rxrpc_ackpacket ack;
882 struct rxrpc_ackinfo ackinfo;
883 struct rxrpc_header hdr;
889 int genbit, loop, nbit, ioc, ret, mtu;
890 u32 abort_code = RX_PROTOCOL_ERROR;
893 //printk("\n--------------------\n");
894 _enter("{%d,%s,%lx} [%lu]",
895 call->debug_id, rxrpc_call_states[call->state], call->events,
896 (jiffies - call->creation_jif) / (HZ / 10));
898 if (test_and_set_bit(RXRPC_CALL_PROC_BUSY, &call->flags)) {
899 _debug("XXXXXXXXXXXXX RUNNING ON MULTIPLE CPUS XXXXXXXXXXXXX");
903 /* there's a good chance we're going to have to send a message, so set
904 * one up in advance */
905 msg.msg_name = &call->conn->trans->peer->srx.transport.sin;
906 msg.msg_namelen = sizeof(call->conn->trans->peer->srx.transport.sin);
907 msg.msg_control = NULL;
908 msg.msg_controllen = 0;
911 hdr.epoch = call->conn->epoch;
913 hdr.callNumber = call->call_id;
915 hdr.type = RXRPC_PACKET_TYPE_ACK;
916 hdr.flags = call->conn->out_clientflag;
918 hdr.securityIndex = call->conn->security_ix;
920 hdr.serviceId = call->conn->service_id;
922 memset(iov, 0, sizeof(iov));
923 iov[0].iov_base = &hdr;
924 iov[0].iov_len = sizeof(hdr);
926 /* deal with events of a final nature */
927 if (test_bit(RXRPC_CALL_RELEASE, &call->events)) {
928 rxrpc_release_call(call);
929 clear_bit(RXRPC_CALL_RELEASE, &call->events);
932 if (test_bit(RXRPC_CALL_RCVD_ERROR, &call->events)) {
935 clear_bit(RXRPC_CALL_CONN_ABORT, &call->events);
936 clear_bit(RXRPC_CALL_REJECT_BUSY, &call->events);
937 clear_bit(RXRPC_CALL_ABORT, &call->events);
939 error = call->conn->trans->peer->net_error;
940 _debug("post net error %d", error);
942 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NET_ERROR,
945 clear_bit(RXRPC_CALL_RCVD_ERROR, &call->events);
949 if (test_bit(RXRPC_CALL_CONN_ABORT, &call->events)) {
950 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
952 clear_bit(RXRPC_CALL_REJECT_BUSY, &call->events);
953 clear_bit(RXRPC_CALL_ABORT, &call->events);
955 _debug("post conn abort");
957 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
958 call->conn->error, true) < 0)
960 clear_bit(RXRPC_CALL_CONN_ABORT, &call->events);
964 if (test_bit(RXRPC_CALL_REJECT_BUSY, &call->events)) {
965 hdr.type = RXRPC_PACKET_TYPE_BUSY;
966 genbit = RXRPC_CALL_REJECT_BUSY;
970 if (test_bit(RXRPC_CALL_ABORT, &call->events)) {
971 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
973 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
974 ECONNABORTED, true) < 0)
976 hdr.type = RXRPC_PACKET_TYPE_ABORT;
977 data = htonl(call->abort_code);
978 iov[1].iov_base = &data;
979 iov[1].iov_len = sizeof(data);
980 genbit = RXRPC_CALL_ABORT;
984 if (test_bit(RXRPC_CALL_ACK_FINAL, &call->events)) {
985 genbit = RXRPC_CALL_ACK_FINAL;
987 ack.bufferSpace = htons(8);
990 ack.reason = RXRPC_ACK_IDLE;
992 call->ackr_reason = 0;
994 spin_lock_bh(&call->lock);
995 ack.serial = call->ackr_serial;
996 ack.previousPacket = call->ackr_prev_seq;
997 ack.firstPacket = htonl(call->rx_data_eaten + 1);
998 spin_unlock_bh(&call->lock);
1002 iov[1].iov_base = &ack;
1003 iov[1].iov_len = sizeof(ack);
1004 iov[2].iov_base = &pad;
1006 iov[3].iov_base = &ackinfo;
1007 iov[3].iov_len = sizeof(ackinfo);
1011 if (call->events & ((1 << RXRPC_CALL_RCVD_BUSY) |
1012 (1 << RXRPC_CALL_RCVD_ABORT))
1016 if (test_bit(RXRPC_CALL_RCVD_ABORT, &call->events))
1017 mark = RXRPC_SKB_MARK_REMOTE_ABORT;
1019 mark = RXRPC_SKB_MARK_BUSY;
1021 _debug("post abort/busy");
1022 rxrpc_clear_tx_window(call);
1023 if (rxrpc_post_message(call, mark, ECONNABORTED, true) < 0)
1026 clear_bit(RXRPC_CALL_RCVD_BUSY, &call->events);
1027 clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
1031 if (test_and_clear_bit(RXRPC_CALL_RCVD_ACKALL, &call->events)) {
1032 _debug("do implicit ackall");
1033 rxrpc_clear_tx_window(call);
1036 if (test_bit(RXRPC_CALL_LIFE_TIMER, &call->events)) {
1037 write_lock_bh(&call->state_lock);
1038 if (call->state <= RXRPC_CALL_COMPLETE) {
1039 call->state = RXRPC_CALL_LOCALLY_ABORTED;
1040 call->abort_code = RX_CALL_TIMEOUT;
1041 set_bit(RXRPC_CALL_ABORT, &call->events);
1043 write_unlock_bh(&call->state_lock);
1045 _debug("post timeout");
1046 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
1050 clear_bit(RXRPC_CALL_LIFE_TIMER, &call->events);
1054 /* deal with assorted inbound messages */
1055 if (!skb_queue_empty(&call->rx_queue)) {
1056 switch (rxrpc_process_rx_queue(call, &abort_code)) {
1065 rxrpc_abort_call(call, abort_code);
1070 /* handle resending */
1071 if (test_and_clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events))
1072 rxrpc_resend_timer(call);
1073 if (test_and_clear_bit(RXRPC_CALL_RESEND, &call->events))
1076 /* consider sending an ordinary ACK */
1077 if (test_bit(RXRPC_CALL_ACK, &call->events)) {
1078 _debug("send ACK: window: %d - %d { %lx }",
1079 call->rx_data_eaten, call->ackr_win_top,
1080 call->ackr_window[0]);
1082 if (call->state > RXRPC_CALL_SERVER_ACK_REQUEST &&
1083 call->ackr_reason != RXRPC_ACK_PING_RESPONSE) {
1084 /* ACK by sending reply DATA packet in this state */
1085 clear_bit(RXRPC_CALL_ACK, &call->events);
1086 goto maybe_reschedule;
1089 genbit = RXRPC_CALL_ACK;
1091 acks = kzalloc(call->ackr_win_top - call->rx_data_eaten,
1096 //hdr.flags = RXRPC_SLOW_START_OK;
1097 ack.bufferSpace = htons(8);
1102 spin_lock_bh(&call->lock);
1103 ack.reason = call->ackr_reason;
1104 ack.serial = call->ackr_serial;
1105 ack.previousPacket = call->ackr_prev_seq;
1106 ack.firstPacket = htonl(call->rx_data_eaten + 1);
1109 for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) {
1110 nbit = loop * BITS_PER_LONG;
1111 for (bits = call->ackr_window[loop]; bits; bits >>= 1
1113 _debug("- l=%d n=%d b=%lx", loop, nbit, bits);
1115 acks[nbit] = RXRPC_ACK_TYPE_ACK;
1116 ack.nAcks = nbit + 1;
1121 call->ackr_reason = 0;
1122 spin_unlock_bh(&call->lock);
1126 iov[1].iov_base = &ack;
1127 iov[1].iov_len = sizeof(ack);
1128 iov[2].iov_base = acks;
1129 iov[2].iov_len = ack.nAcks;
1130 iov[3].iov_base = &pad;
1132 iov[4].iov_base = &ackinfo;
1133 iov[4].iov_len = sizeof(ackinfo);
1135 switch (ack.reason) {
1136 case RXRPC_ACK_REQUESTED:
1137 case RXRPC_ACK_DUPLICATE:
1138 case RXRPC_ACK_OUT_OF_SEQUENCE:
1139 case RXRPC_ACK_EXCEEDS_WINDOW:
1140 case RXRPC_ACK_NOSPACE:
1141 case RXRPC_ACK_PING:
1142 case RXRPC_ACK_PING_RESPONSE:
1143 goto send_ACK_with_skew;
1144 case RXRPC_ACK_DELAY:
1145 case RXRPC_ACK_IDLE:
1150 /* handle completion of security negotiations on an incoming
1152 if (test_and_clear_bit(RXRPC_CALL_SECURED, &call->events)) {
1154 spin_lock_bh(&call->lock);
1156 if (call->state == RXRPC_CALL_SERVER_SECURING) {
1158 write_lock(&call->conn->lock);
1159 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1160 !test_bit(RXRPC_CALL_RELEASE, &call->events)) {
1161 _debug("not released");
1162 call->state = RXRPC_CALL_SERVER_ACCEPTING;
1163 list_move_tail(&call->accept_link,
1164 &call->socket->acceptq);
1166 write_unlock(&call->conn->lock);
1167 read_lock(&call->state_lock);
1168 if (call->state < RXRPC_CALL_COMPLETE)
1169 set_bit(RXRPC_CALL_POST_ACCEPT, &call->events);
1170 read_unlock(&call->state_lock);
1173 spin_unlock_bh(&call->lock);
1174 if (!test_bit(RXRPC_CALL_POST_ACCEPT, &call->events))
1175 goto maybe_reschedule;
1178 /* post a notification of an acceptable connection to the app */
1179 if (test_bit(RXRPC_CALL_POST_ACCEPT, &call->events)) {
1180 _debug("post accept");
1181 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NEW_CALL,
1184 clear_bit(RXRPC_CALL_POST_ACCEPT, &call->events);
1185 goto maybe_reschedule;
1188 /* handle incoming call acceptance */
1189 if (test_and_clear_bit(RXRPC_CALL_ACCEPTED, &call->events)) {
1191 ASSERTCMP(call->rx_data_post, ==, 0);
1192 call->rx_data_post = 1;
1193 read_lock_bh(&call->state_lock);
1194 if (call->state < RXRPC_CALL_COMPLETE)
1195 set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events);
1196 read_unlock_bh(&call->state_lock);
1199 /* drain the out of sequence received packet queue into the packet Rx
1201 if (test_and_clear_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events)) {
1202 while (call->rx_data_post == call->rx_first_oos)
1203 if (rxrpc_drain_rx_oos_queue(call) < 0)
1205 goto maybe_reschedule;
1208 /* other events may have been raised since we started checking */
1209 goto maybe_reschedule;
1212 ack.maxSkew = htons(atomic_read(&call->conn->hi_serial) -
1215 mtu = call->conn->trans->peer->if_mtu;
1216 mtu -= call->conn->trans->peer->hdrsize;
1217 ackinfo.maxMTU = htonl(mtu);
1218 ackinfo.rwind = htonl(rxrpc_rx_window_size);
1220 /* permit the peer to send us jumbo packets if it wants to */
1221 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
1222 ackinfo.jumbo_max = htonl(rxrpc_rx_jumbo_max);
1224 hdr.serial = htonl(atomic_inc_return(&call->conn->serial));
1225 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
1228 ntohl(ack.firstPacket),
1229 ntohl(ack.previousPacket),
1231 rxrpc_acks(ack.reason),
1234 del_timer_sync(&call->ack_timer);
1236 set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags);
1237 goto send_message_2;
1240 _debug("send message");
1242 hdr.serial = htonl(atomic_inc_return(&call->conn->serial));
1243 _proto("Tx %s %%%u", rxrpc_pkts[hdr.type], ntohl(hdr.serial));
1246 len = iov[0].iov_len;
1248 if (iov[4].iov_len) {
1250 len += iov[4].iov_len;
1251 len += iov[3].iov_len;
1252 len += iov[2].iov_len;
1253 len += iov[1].iov_len;
1254 } else if (iov[3].iov_len) {
1256 len += iov[3].iov_len;
1257 len += iov[2].iov_len;
1258 len += iov[1].iov_len;
1259 } else if (iov[2].iov_len) {
1261 len += iov[2].iov_len;
1262 len += iov[1].iov_len;
1263 } else if (iov[1].iov_len) {
1265 len += iov[1].iov_len;
1268 ret = kernel_sendmsg(call->conn->trans->local->socket,
1269 &msg, iov, ioc, len);
1271 _debug("sendmsg failed: %d", ret);
1272 read_lock_bh(&call->state_lock);
1273 if (call->state < RXRPC_CALL_DEAD)
1274 rxrpc_queue_call(call);
1275 read_unlock_bh(&call->state_lock);
1280 case RXRPC_CALL_ABORT:
1281 clear_bit(genbit, &call->events);
1282 clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
1285 case RXRPC_CALL_ACK_FINAL:
1286 write_lock_bh(&call->state_lock);
1287 if (call->state == RXRPC_CALL_CLIENT_FINAL_ACK)
1288 call->state = RXRPC_CALL_COMPLETE;
1289 write_unlock_bh(&call->state_lock);
1293 clear_bit(genbit, &call->events);
1294 switch (call->state) {
1295 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
1296 case RXRPC_CALL_CLIENT_RECV_REPLY:
1297 case RXRPC_CALL_SERVER_RECV_REQUEST:
1298 case RXRPC_CALL_SERVER_ACK_REQUEST:
1299 _debug("start ACK timer");
1300 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY,
1301 call->ackr_serial, false);
1305 goto maybe_reschedule;
1309 del_timer_sync(&call->ack_timer);
1310 if (test_and_clear_bit(RXRPC_CALL_ACK_FINAL, &call->events))
1311 rxrpc_put_call(call);
1312 clear_bit(RXRPC_CALL_ACK, &call->events);
1315 if (call->events || !skb_queue_empty(&call->rx_queue)) {
1316 read_lock_bh(&call->state_lock);
1317 if (call->state < RXRPC_CALL_DEAD)
1318 rxrpc_queue_call(call);
1319 read_unlock_bh(&call->state_lock);
1322 /* don't leave aborted connections on the accept queue */
1323 if (call->state >= RXRPC_CALL_COMPLETE &&
1324 !list_empty(&call->accept_link)) {
1325 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
1326 call, call->events, call->flags,
1327 ntohl(call->conn->cid));
1329 read_lock_bh(&call->state_lock);
1330 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1331 !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
1332 rxrpc_queue_call(call);
1333 read_unlock_bh(&call->state_lock);
1337 clear_bit(RXRPC_CALL_PROC_BUSY, &call->flags);
1340 /* because we don't want two CPUs both processing the work item for one
1341 * call at the same time, we use a flag to note when it's busy; however
1342 * this means there's a race between clearing the flag and setting the
1343 * work pending bit and the work item being processed again */
1344 if (call->events && !work_pending(&call->processor)) {
1345 _debug("jumpstart %x", ntohl(call->conn->cid));
1346 rxrpc_queue_call(call);
1353 _debug("out of memory");
1354 goto maybe_reschedule;