MAINTAINERS: EDAC: add Mauro and Borislav as interim patch collectors
[linux-2.6/btrfs-unstable.git] / net / rxrpc / ar-ack.c
blobcd97a0ce48d80f73efbf2476e272f737c787bad9
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>
18 #include <net/sock.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",
28 "IDL", "-?-"
31 if (reason >= ARRAY_SIZE(str))
32 reason = ARRAY_SIZE(str) - 1;
33 return str[reason];
36 static const s8 rxrpc_ack_priority[] = {
37 [0] = 0,
38 [RXRPC_ACK_DELAY] = 1,
39 [RXRPC_ACK_REQUESTED] = 2,
40 [RXRPC_ACK_IDLE] = 3,
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)
54 unsigned long expiry;
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),
61 immediate);
63 if (prior < rxrpc_ack_priority[call->ackr_reason]) {
64 if (immediate)
65 goto cancel_timer;
66 return;
69 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
70 * numbers */
71 if (prior == rxrpc_ack_priority[call->ackr_reason]) {
72 if (prior <= 4)
73 call->ackr_serial = serial;
74 if (immediate)
75 goto cancel_timer;
76 return;
79 call->ackr_reason = ack_reason;
80 call->ackr_serial = serial;
82 switch (ack_reason) {
83 case RXRPC_ACK_DELAY:
84 _debug("run delay timer");
85 call->ack_timer.expires = jiffies + rxrpc_ack_timeout * HZ;
86 add_timer(&call->ack_timer);
87 return;
89 case RXRPC_ACK_IDLE:
90 if (!immediate) {
91 _debug("run defer timer");
92 expiry = 1;
93 goto run_timer;
95 goto cancel_timer;
97 case RXRPC_ACK_REQUESTED:
98 if (!rxrpc_ack_defer)
99 goto cancel_timer;
100 if (!immediate || serial == cpu_to_be32(1)) {
101 _debug("run defer timer");
102 expiry = rxrpc_ack_defer;
103 goto run_timer;
106 default:
107 _debug("immediate ACK");
108 goto cancel_timer;
111 run_timer:
112 expiry += jiffies;
113 if (!timer_pending(&call->ack_timer) ||
114 time_after(call->ack_timer.expires, expiry))
115 mod_timer(&call->ack_timer, expiry);
116 return;
118 cancel_timer:
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)
151 resend = 0;
153 if (resend & 1) {
154 _debug("SET RESEND");
155 set_bit(RXRPC_CALL_RESEND, &call->events);
158 if (resend & 2) {
159 _debug("MODIFY RESEND TIMER");
160 set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
161 mod_timer(&call->resend_timer, resend_at);
162 } else {
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);
172 * resend packets
174 static void rxrpc_resend(struct rxrpc_call *call)
176 struct rxrpc_skb_priv *sp;
177 struct rxrpc_header *hdr;
178 struct sk_buff *txb;
179 unsigned long *p_txb, resend_at;
180 int loop, stop;
181 u8 resend;
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));
188 stop = 0;
189 resend = 0;
190 resend_at = 0;
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();
198 if (*p_txb & 1)
199 continue;
201 txb = (struct sk_buff *) *p_txb;
202 sp = rxrpc_skb(txb);
204 if (sp->need_resend) {
205 sp->need_resend = false;
207 /* each Tx packet has a new serial number */
208 sp->hdr.serial =
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) {
217 stop = 0;
218 sp->resend_at = jiffies + 3;
219 } else {
220 sp->resend_at =
221 jiffies + rxrpc_resend_timeout * HZ;
225 if (time_after_eq(jiffies + 1, sp->resend_at)) {
226 sp->need_resend = true;
227 resend |= 1;
228 } else if (resend & 2) {
229 if (time_before(sp->resend_at, resend_at))
230 resend_at = sp->resend_at;
231 } else {
232 resend_at = sp->resend_at;
233 resend |= 2;
237 rxrpc_set_resend(call, resend, resend_at);
238 _leave("");
242 * handle resend timer expiry
244 static void rxrpc_resend_timer(struct rxrpc_call *call)
246 struct rxrpc_skb_priv *sp;
247 struct sk_buff *txb;
248 unsigned long *p_txb, resend_at;
249 int loop;
250 u8 resend;
252 _enter("%d,%d,%d",
253 call->acks_tail, call->acks_unacked, call->acks_head);
255 if (call->state >= RXRPC_CALL_COMPLETE)
256 return;
258 resend = 0;
259 resend_at = 0;
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);
268 sp = rxrpc_skb(txb);
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;
276 resend |= 1;
277 } else if (resend & 2) {
278 if (time_before(sp->resend_at, resend_at))
279 resend_at = sp->resend_at;
280 } else {
281 resend_at = sp->resend_at;
282 resend |= 2;
286 rxrpc_set_resend(call, resend, resend_at);
287 _leave("");
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,
297 struct sk_buff *skb)
299 struct rxrpc_skb_priv *sp;
300 struct sk_buff *txb;
301 unsigned long *p_txb, resend_at;
302 int loop;
303 u8 sacks[RXRPC_MAXACKS], resend;
305 _enter("{%d,%d},{%d},",
306 call->acks_hard,
307 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz),
308 ack->nAcks);
310 if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0)
311 goto protocol_error;
313 resend = 0;
314 resend_at = 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);
320 sp = rxrpc_skb(txb);
322 switch (sacks[loop]) {
323 case RXRPC_ACK_TYPE_ACK:
324 sp->need_resend = false;
325 *p_txb |= 1;
326 break;
327 case RXRPC_ACK_TYPE_NACK:
328 sp->need_resend = true;
329 *p_txb &= ~1;
330 resend = 1;
331 break;
332 default:
333 _debug("Unsupported ACK type %d", sacks[loop]);
334 goto protocol_error;
338 smp_mb();
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);
350 sp = rxrpc_skb(txb);
352 if (*p_txb & 1) {
353 /* packet must have been discarded */
354 sp->need_resend = true;
355 *p_txb &= ~1;
356 resend |= 1;
357 } else if (sp->need_resend) {
359 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
360 sp->need_resend = true;
361 resend |= 1;
362 } else if (resend & 2) {
363 if (time_before(sp->resend_at, resend_at))
364 resend_at = sp->resend_at;
365 } else {
366 resend_at = sp->resend_at;
367 resend |= 2;
371 rxrpc_set_resend(call, resend, resend_at);
372 _leave(" = 0");
373 return 0;
375 protocol_error:
376 _leave(" = -EPROTO");
377 return -EPROTO;
381 * discard hard-ACK'd packets from the Tx window
383 static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard)
385 unsigned long _skb;
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);
397 old_tail = tail;
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;
402 call->acks_hard++;
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;
422 struct sk_buff *skb;
423 bool terminal;
424 int ret;
426 _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos);
428 spin_lock_bh(&call->lock);
430 ret = -ECONNRESET;
431 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
432 goto socket_unavailable;
434 skb = skb_dequeue(&call->rx_oos_queue);
435 if (skb) {
436 sp = rxrpc_skb(skb);
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);
445 } else {
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);
450 BUG_ON(ret < 0);
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);
456 if (skb)
457 call->rx_first_oos =
458 ntohl(rxrpc_skb(skb)->hdr.seq);
459 else
460 call->rx_first_oos = 0;
461 _debug("peek %p {%u}", skb, call->rx_first_oos);
465 ret = 0;
466 socket_unavailable:
467 spin_unlock_bh(&call->lock);
468 _leave(" = %d", ret);
469 return ret;
473 * insert an out of sequence packet into the buffer
475 static void rxrpc_insert_oos_packet(struct rxrpc_call *call,
476 struct sk_buff *skb)
478 struct rxrpc_skb_priv *sp, *psp;
479 struct sk_buff *p;
480 u32 seq;
482 sp = rxrpc_skb(skb);
483 seq = ntohl(sp->hdr.seq);
484 _enter(",,{%u}", seq);
486 skb->destructor = rxrpc_packet_destructor;
487 ASSERTCMP(sp->call, ==, NULL);
488 sp->call = call;
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) {
495 psp = rxrpc_skb(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);
500 goto inserted;
504 _debug("append oos #%u", seq);
505 skb_queue_tail(&call->rx_oos_queue, skb);
506 inserted:
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;
530 struct sk_buff *skb;
531 unsigned long _skb, *acks_window;
532 u8 winsz = call->acks_winsz;
533 int tail;
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;
542 smp_mb();
543 call->acks_tail = (call->acks_tail + 1) & (winsz - 1);
545 skb = (struct sk_buff *) _skb;
546 sp = rxrpc_skb(skb);
547 _debug("+++ clear Tx %u", ntohl(sp->hdr.seq));
548 rxrpc_free_skb(skb);
551 kfree(acks_window);
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;
562 unsigned int mtu;
564 if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) {
565 _leave(" [no ackinfo]");
566 return;
569 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
570 latest,
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);
579 peer->maxdata = mtu;
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,
590 u32 *_abort_code)
592 struct rxrpc_ackpacket ack;
593 struct rxrpc_skb_priv *sp;
594 struct sk_buff *skb;
595 bool post_ACK;
596 int latest;
597 u32 hard, tx;
599 _enter("");
601 process_further:
602 skb = skb_dequeue(&call->rx_queue);
603 if (!skb)
604 return -EAGAIN;
606 _net("deferred skb %p", skb);
608 sp = rxrpc_skb(skb);
610 _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state);
612 post_ACK = false;
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)
623 goto protocol_error;
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");
632 goto protocol_error;
634 if (!skb_pull(skb, sizeof(ack)))
635 BUG();
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 }",
642 latest,
643 ntohs(ack.maxSkew),
644 hard,
645 ntohl(ack.previousPacket),
646 ntohl(ack.serial),
647 rxrpc_acks(ack.reason),
648 ack.nAcks);
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);
662 goto discard;
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)
670 goto discard;
672 _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state);
674 if (hard > 0) {
675 if (hard - 1 > tx) {
676 _debug("hard-ACK'd packet %d not transmitted"
677 " (%d top)",
678 hard - 1, tx);
679 goto protocol_error;
682 if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY ||
683 call->state == RXRPC_CALL_SERVER_AWAIT_ACK) &&
684 hard > tx)
685 goto all_acked;
687 smp_rmb();
688 rxrpc_rotate_tx_window(call, hard - 1);
691 if (ack.nAcks > 0) {
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);
696 goto protocol_error;
699 if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0)
700 goto protocol_error;
702 goto discard;
704 /* complete ACK to process */
705 case RXRPC_PACKET_TYPE_ACKALL:
706 goto all_acked;
708 /* abort and busy are handled elsewhere */
709 case RXRPC_PACKET_TYPE_BUSY:
710 case RXRPC_PACKET_TYPE_ABORT:
711 BUG();
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:
717 BUG();
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 */
722 all_acked:
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;
729 break;
730 case RXRPC_CALL_SERVER_AWAIT_ACK:
731 _debug("srv complete");
732 call->state = RXRPC_CALL_COMPLETE;
733 post_ACK = true;
734 break;
735 case RXRPC_CALL_CLIENT_SEND_REQUEST:
736 case RXRPC_CALL_SERVER_RECV_REQUEST:
737 goto protocol_error_unlock; /* can't occur yet */
738 default:
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);
757 if (post_ACK) {
758 /* post the final ACK message for userspace to pick up */
759 _debug("post ACK");
760 skb->mark = RXRPC_SKB_MARK_FINAL_ACK;
761 sp->call = call;
762 rxrpc_get_call(call);
763 spin_lock_bh(&call->lock);
764 if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0)
765 BUG();
766 spin_unlock_bh(&call->lock);
767 goto process_further;
770 discard:
771 rxrpc_free_skb(skb);
772 goto process_further;
774 protocol_error_unlock:
775 write_unlock_bh(&call->state_lock);
776 protocol_error:
777 rxrpc_free_skb(skb);
778 _leave(" = -EPROTO");
779 return -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,
786 bool fatal)
788 struct rxrpc_skb_priv *sp;
789 struct sk_buff *skb;
790 int ret;
792 _enter("{%d,%lx},%u,%u,%d",
793 call->debug_id, call->flags, mark, error, fatal);
795 /* remove timers and things for fatal messages */
796 if (fatal) {
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]");
805 return 0;
808 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
809 skb = alloc_skb(0, GFP_NOFS);
810 if (!skb)
811 return -ENOMEM;
813 rxrpc_new_skb(skb);
815 skb->mark = mark;
817 sp = rxrpc_skb(skb);
818 memset(sp, 0, sizeof(*sp));
819 sp->error = error;
820 sp->call = call;
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);
826 BUG_ON(ret < 0);
829 return 0;
833 * handle background processing of incoming call packets and ACK / abort
834 * generation
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;
843 struct msghdr msg;
844 struct kvec iov[5];
845 unsigned long bits;
846 __be32 data, pad;
847 size_t len;
848 int genbit, loop, nbit, ioc, ret, mtu;
849 u32 abort_code = RX_PROTOCOL_ERROR;
850 u8 *acks = NULL;
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");
859 return;
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;
868 msg.msg_flags = 0;
870 hdr.epoch = call->conn->epoch;
871 hdr.cid = call->cid;
872 hdr.callNumber = call->call_id;
873 hdr.seq = 0;
874 hdr.type = RXRPC_PACKET_TYPE_ACK;
875 hdr.flags = call->conn->out_clientflag;
876 hdr.userStatus = 0;
877 hdr.securityIndex = call->conn->security_ix;
878 hdr._rsvd = 0;
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)) {
892 int error;
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,
902 error, true) < 0)
903 goto no_mem;
904 clear_bit(RXRPC_CALL_RCVD_ERROR, &call->events);
905 goto kill_ACKs;
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)
918 goto no_mem;
919 clear_bit(RXRPC_CALL_CONN_ABORT, &call->events);
920 goto kill_ACKs;
923 if (test_bit(RXRPC_CALL_REJECT_BUSY, &call->events)) {
924 hdr.type = RXRPC_PACKET_TYPE_BUSY;
925 genbit = RXRPC_CALL_REJECT_BUSY;
926 goto send_message;
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)
934 goto no_mem;
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;
940 goto send_message;
943 if (test_bit(RXRPC_CALL_ACK_FINAL, &call->events)) {
944 genbit = RXRPC_CALL_ACK_FINAL;
946 ack.bufferSpace = htons(8);
947 ack.maxSkew = 0;
948 ack.serial = 0;
949 ack.reason = RXRPC_ACK_IDLE;
950 ack.nAcks = 0;
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);
959 pad = 0;
961 iov[1].iov_base = &ack;
962 iov[1].iov_len = sizeof(ack);
963 iov[2].iov_base = &pad;
964 iov[2].iov_len = 3;
965 iov[3].iov_base = &ackinfo;
966 iov[3].iov_len = sizeof(ackinfo);
967 goto send_ACK;
970 if (call->events & ((1 << RXRPC_CALL_RCVD_BUSY) |
971 (1 << RXRPC_CALL_RCVD_ABORT))
973 u32 mark;
975 if (test_bit(RXRPC_CALL_RCVD_ABORT, &call->events))
976 mark = RXRPC_SKB_MARK_REMOTE_ABORT;
977 else
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)
983 goto no_mem;
985 clear_bit(RXRPC_CALL_RCVD_BUSY, &call->events);
986 clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
987 goto kill_ACKs;
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,
1006 ETIME, true) < 0)
1007 goto no_mem;
1009 clear_bit(RXRPC_CALL_LIFE_TIMER, &call->events);
1010 goto kill_ACKs;
1013 /* deal with assorted inbound messages */
1014 if (!skb_queue_empty(&call->rx_queue)) {
1015 switch (rxrpc_process_rx_queue(call, &abort_code)) {
1016 case 0:
1017 case -EAGAIN:
1018 break;
1019 case -ENOMEM:
1020 goto no_mem;
1021 case -EKEYEXPIRED:
1022 case -EKEYREJECTED:
1023 case -EPROTO:
1024 rxrpc_abort_call(call, abort_code);
1025 goto kill_ACKs;
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))
1033 rxrpc_resend(call);
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,
1051 GFP_NOFS);
1052 if (!acks)
1053 goto no_mem;
1055 //hdr.flags = RXRPC_SLOW_START_OK;
1056 ack.bufferSpace = htons(8);
1057 ack.maxSkew = 0;
1058 ack.serial = 0;
1059 ack.reason = 0;
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);
1067 ack.nAcks = 0;
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);
1073 if (bits & 1) {
1074 acks[nbit] = RXRPC_ACK_TYPE_ACK;
1075 ack.nAcks = nbit + 1;
1077 nbit++;
1080 call->ackr_reason = 0;
1081 spin_unlock_bh(&call->lock);
1083 pad = 0;
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;
1090 iov[3].iov_len = 3;
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:
1105 goto send_ACK;
1109 /* handle completion of security negotiations on an incoming
1110 * connection */
1111 if (test_and_clear_bit(RXRPC_CALL_SECURED, &call->events)) {
1112 _debug("secured");
1113 spin_lock_bh(&call->lock);
1115 if (call->state == RXRPC_CALL_SERVER_SECURING) {
1116 _debug("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,
1141 0, false) < 0)
1142 goto no_mem;
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)) {
1149 _debug("accepted");
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
1159 * queue */
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)
1163 break;
1164 goto maybe_reschedule;
1167 /* other events may have been raised since we started checking */
1168 goto maybe_reschedule;
1170 send_ACK_with_skew:
1171 ack.maxSkew = htons(atomic_read(&call->conn->hi_serial) -
1172 ntohl(ack.serial));
1173 send_ACK:
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 }",
1185 ntohl(hdr.serial),
1186 ntohs(ack.maxSkew),
1187 ntohl(ack.firstPacket),
1188 ntohl(ack.previousPacket),
1189 ntohl(ack.serial),
1190 rxrpc_acks(ack.reason),
1191 ack.nAcks);
1193 del_timer_sync(&call->ack_timer);
1194 if (ack.nAcks > 0)
1195 set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags);
1196 goto send_message_2;
1198 send_message:
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));
1203 send_message_2:
1205 len = iov[0].iov_len;
1206 ioc = 1;
1207 if (iov[4].iov_len) {
1208 ioc = 5;
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) {
1214 ioc = 4;
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) {
1219 ioc = 3;
1220 len += iov[2].iov_len;
1221 len += iov[1].iov_len;
1222 } else if (iov[1].iov_len) {
1223 ioc = 2;
1224 len += iov[1].iov_len;
1227 ret = kernel_sendmsg(call->conn->trans->local->socket,
1228 &msg, iov, ioc, len);
1229 if (ret < 0) {
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);
1235 goto error;
1238 switch (genbit) {
1239 case RXRPC_CALL_ABORT:
1240 clear_bit(genbit, &call->events);
1241 clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
1242 goto kill_ACKs;
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);
1249 goto kill_ACKs;
1251 default:
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);
1261 default:
1262 break;
1264 goto maybe_reschedule;
1267 kill_ACKs:
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);
1273 maybe_reschedule:
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);
1295 error:
1296 clear_bit(RXRPC_CALL_PROC_BUSY, &call->flags);
1297 kfree(acks);
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);
1308 _leave("");
1309 return;
1311 no_mem:
1312 _debug("out of memory");
1313 goto maybe_reschedule;