blob: 732b82f540c562e5ded730748ac2f99063800fe9 [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* Management of Tx window, Tx resend, ACKs and out-of-sequence reception
2 *
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/module.h>
13#include <linux/circ_buf.h>
14#include <linux/net.h>
15#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070017#include <linux/udp.h>
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include "ar-internal.h"
21
David Howells5873c082014-02-07 18:58:44 +000022/*
23 * How long to wait before scheduling ACK generation after seeing a
24 * packet with RXRPC_REQUEST_ACK set (in jiffies).
25 */
26unsigned rxrpc_requested_ack_delay = 1;
27
28/*
29 * How long to wait before scheduling an ACK with subtype DELAY (in jiffies).
30 *
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.
34 */
35unsigned rxrpc_soft_ack_delay = 1 * HZ;
36
37/*
38 * How long to wait before scheduling an ACK with subtype IDLE (in jiffies).
39 *
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.
43 */
44unsigned rxrpc_idle_ack_delay = 1;
David Howells17926a72007-04-26 15:48:28 -070045
Dan Carpenter08d4d212014-01-20 13:28:59 +030046static const char *rxrpc_acks(u8 reason)
47{
48 static const char *const str[] = {
49 "---", "REQ", "DUP", "OOS", "WIN", "MEM", "PNG", "PNR", "DLY",
50 "IDL", "-?-"
51 };
52
53 if (reason >= ARRAY_SIZE(str))
54 reason = ARRAY_SIZE(str) - 1;
55 return str[reason];
56}
David Howells17926a72007-04-26 15:48:28 -070057
58static const s8 rxrpc_ack_priority[] = {
59 [0] = 0,
60 [RXRPC_ACK_DELAY] = 1,
61 [RXRPC_ACK_REQUESTED] = 2,
62 [RXRPC_ACK_IDLE] = 3,
63 [RXRPC_ACK_PING_RESPONSE] = 4,
64 [RXRPC_ACK_DUPLICATE] = 5,
65 [RXRPC_ACK_OUT_OF_SEQUENCE] = 6,
66 [RXRPC_ACK_EXCEEDS_WINDOW] = 7,
67 [RXRPC_ACK_NOSPACE] = 8,
68};
69
70/*
71 * propose an ACK be sent
72 */
David Howells4e36a952009-09-16 00:01:13 -070073void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
David Howells17926a72007-04-26 15:48:28 -070074 __be32 serial, bool immediate)
75{
76 unsigned long expiry;
77 s8 prior = rxrpc_ack_priority[ack_reason];
78
79 ASSERTCMP(prior, >, 0);
80
81 _enter("{%d},%s,%%%x,%u",
Dan Carpenter08d4d212014-01-20 13:28:59 +030082 call->debug_id, rxrpc_acks(ack_reason), ntohl(serial),
David Howells17926a72007-04-26 15:48:28 -070083 immediate);
84
85 if (prior < rxrpc_ack_priority[call->ackr_reason]) {
86 if (immediate)
87 goto cancel_timer;
88 return;
89 }
90
91 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
92 * numbers */
93 if (prior == rxrpc_ack_priority[call->ackr_reason]) {
94 if (prior <= 4)
95 call->ackr_serial = serial;
96 if (immediate)
97 goto cancel_timer;
98 return;
99 }
100
101 call->ackr_reason = ack_reason;
102 call->ackr_serial = serial;
103
104 switch (ack_reason) {
105 case RXRPC_ACK_DELAY:
106 _debug("run delay timer");
David Howells5873c082014-02-07 18:58:44 +0000107 expiry = rxrpc_soft_ack_delay;
108 goto run_timer;
David Howells17926a72007-04-26 15:48:28 -0700109
110 case RXRPC_ACK_IDLE:
111 if (!immediate) {
112 _debug("run defer timer");
David Howells5873c082014-02-07 18:58:44 +0000113 expiry = rxrpc_idle_ack_delay;
David Howells17926a72007-04-26 15:48:28 -0700114 goto run_timer;
115 }
116 goto cancel_timer;
117
118 case RXRPC_ACK_REQUESTED:
David Howells5873c082014-02-07 18:58:44 +0000119 expiry = rxrpc_requested_ack_delay;
120 if (!expiry)
David Howells17926a72007-04-26 15:48:28 -0700121 goto cancel_timer;
122 if (!immediate || serial == cpu_to_be32(1)) {
123 _debug("run defer timer");
David Howells17926a72007-04-26 15:48:28 -0700124 goto run_timer;
125 }
126
127 default:
128 _debug("immediate ACK");
129 goto cancel_timer;
130 }
131
132run_timer:
133 expiry += jiffies;
134 if (!timer_pending(&call->ack_timer) ||
135 time_after(call->ack_timer.expires, expiry))
136 mod_timer(&call->ack_timer, expiry);
137 return;
138
139cancel_timer:
140 _debug("cancel timer %%%u", ntohl(serial));
141 try_to_del_timer_sync(&call->ack_timer);
142 read_lock_bh(&call->state_lock);
143 if (call->state <= RXRPC_CALL_COMPLETE &&
144 !test_and_set_bit(RXRPC_CALL_ACK, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700145 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700146 read_unlock_bh(&call->state_lock);
147}
148
149/*
150 * propose an ACK be sent, locking the call structure
151 */
David Howells4e36a952009-09-16 00:01:13 -0700152void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
David Howells17926a72007-04-26 15:48:28 -0700153 __be32 serial, bool immediate)
154{
155 s8 prior = rxrpc_ack_priority[ack_reason];
156
157 if (prior > rxrpc_ack_priority[call->ackr_reason]) {
158 spin_lock_bh(&call->lock);
159 __rxrpc_propose_ACK(call, ack_reason, serial, immediate);
160 spin_unlock_bh(&call->lock);
161 }
162}
163
164/*
165 * set the resend timer
166 */
167static void rxrpc_set_resend(struct rxrpc_call *call, u8 resend,
168 unsigned long resend_at)
169{
170 read_lock_bh(&call->state_lock);
171 if (call->state >= RXRPC_CALL_COMPLETE)
172 resend = 0;
173
174 if (resend & 1) {
175 _debug("SET RESEND");
176 set_bit(RXRPC_CALL_RESEND, &call->events);
177 }
178
179 if (resend & 2) {
180 _debug("MODIFY RESEND TIMER");
181 set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
182 mod_timer(&call->resend_timer, resend_at);
183 } else {
184 _debug("KILL RESEND TIMER");
185 del_timer_sync(&call->resend_timer);
186 clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
187 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
188 }
189 read_unlock_bh(&call->state_lock);
190}
191
192/*
193 * resend packets
194 */
195static void rxrpc_resend(struct rxrpc_call *call)
196{
197 struct rxrpc_skb_priv *sp;
198 struct rxrpc_header *hdr;
199 struct sk_buff *txb;
200 unsigned long *p_txb, resend_at;
201 int loop, stop;
202 u8 resend;
203
204 _enter("{%d,%d,%d,%d},",
205 call->acks_hard, call->acks_unacked,
206 atomic_read(&call->sequence),
207 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
208
209 stop = 0;
210 resend = 0;
211 resend_at = 0;
212
213 for (loop = call->acks_tail;
214 loop != call->acks_head || stop;
215 loop = (loop + 1) & (call->acks_winsz - 1)
216 ) {
217 p_txb = call->acks_window + loop;
218 smp_read_barrier_depends();
219 if (*p_txb & 1)
220 continue;
221
222 txb = (struct sk_buff *) *p_txb;
223 sp = rxrpc_skb(txb);
224
225 if (sp->need_resend) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000226 sp->need_resend = false;
David Howells17926a72007-04-26 15:48:28 -0700227
228 /* each Tx packet has a new serial number */
229 sp->hdr.serial =
230 htonl(atomic_inc_return(&call->conn->serial));
231
232 hdr = (struct rxrpc_header *) txb->head;
233 hdr->serial = sp->hdr.serial;
234
235 _proto("Tx DATA %%%u { #%d }",
236 ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
237 if (rxrpc_send_packet(call->conn->trans, txb) < 0) {
238 stop = 0;
239 sp->resend_at = jiffies + 3;
240 } else {
241 sp->resend_at =
242 jiffies + rxrpc_resend_timeout * HZ;
243 }
244 }
245
246 if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000247 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700248 resend |= 1;
249 } else if (resend & 2) {
250 if (time_before(sp->resend_at, resend_at))
251 resend_at = sp->resend_at;
252 } else {
253 resend_at = sp->resend_at;
254 resend |= 2;
255 }
256 }
257
258 rxrpc_set_resend(call, resend, resend_at);
259 _leave("");
260}
261
262/*
263 * handle resend timer expiry
264 */
265static void rxrpc_resend_timer(struct rxrpc_call *call)
266{
267 struct rxrpc_skb_priv *sp;
268 struct sk_buff *txb;
269 unsigned long *p_txb, resend_at;
270 int loop;
271 u8 resend;
272
273 _enter("%d,%d,%d",
274 call->acks_tail, call->acks_unacked, call->acks_head);
275
David Howells3b5bac22010-08-04 02:34:17 +0000276 if (call->state >= RXRPC_CALL_COMPLETE)
277 return;
278
David Howells17926a72007-04-26 15:48:28 -0700279 resend = 0;
280 resend_at = 0;
281
282 for (loop = call->acks_unacked;
283 loop != call->acks_head;
284 loop = (loop + 1) & (call->acks_winsz - 1)
285 ) {
286 p_txb = call->acks_window + loop;
287 smp_read_barrier_depends();
288 txb = (struct sk_buff *) (*p_txb & ~1);
289 sp = rxrpc_skb(txb);
290
291 ASSERT(!(*p_txb & 1));
292
293 if (sp->need_resend) {
294 ;
295 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000296 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700297 resend |= 1;
298 } else if (resend & 2) {
299 if (time_before(sp->resend_at, resend_at))
300 resend_at = sp->resend_at;
301 } else {
302 resend_at = sp->resend_at;
303 resend |= 2;
304 }
305 }
306
307 rxrpc_set_resend(call, resend, resend_at);
308 _leave("");
309}
310
311/*
312 * process soft ACKs of our transmitted packets
313 * - these indicate packets the peer has or has not received, but hasn't yet
314 * given to the consumer, and so can still be discarded and re-requested
315 */
316static int rxrpc_process_soft_ACKs(struct rxrpc_call *call,
317 struct rxrpc_ackpacket *ack,
318 struct sk_buff *skb)
319{
320 struct rxrpc_skb_priv *sp;
321 struct sk_buff *txb;
322 unsigned long *p_txb, resend_at;
323 int loop;
324 u8 sacks[RXRPC_MAXACKS], resend;
325
326 _enter("{%d,%d},{%d},",
327 call->acks_hard,
328 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz),
329 ack->nAcks);
330
331 if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0)
332 goto protocol_error;
333
334 resend = 0;
335 resend_at = 0;
336 for (loop = 0; loop < ack->nAcks; loop++) {
337 p_txb = call->acks_window;
338 p_txb += (call->acks_tail + loop) & (call->acks_winsz - 1);
339 smp_read_barrier_depends();
340 txb = (struct sk_buff *) (*p_txb & ~1);
341 sp = rxrpc_skb(txb);
342
343 switch (sacks[loop]) {
344 case RXRPC_ACK_TYPE_ACK:
Rusty Russell3db1cd52011-12-19 13:56:45 +0000345 sp->need_resend = false;
David Howells17926a72007-04-26 15:48:28 -0700346 *p_txb |= 1;
347 break;
348 case RXRPC_ACK_TYPE_NACK:
Rusty Russell3db1cd52011-12-19 13:56:45 +0000349 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700350 *p_txb &= ~1;
351 resend = 1;
352 break;
353 default:
354 _debug("Unsupported ACK type %d", sacks[loop]);
355 goto protocol_error;
356 }
357 }
358
359 smp_mb();
360 call->acks_unacked = (call->acks_tail + loop) & (call->acks_winsz - 1);
361
362 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
363 * have been received or processed yet by the far end */
364 for (loop = call->acks_unacked;
365 loop != call->acks_head;
366 loop = (loop + 1) & (call->acks_winsz - 1)
367 ) {
368 p_txb = call->acks_window + loop;
369 smp_read_barrier_depends();
370 txb = (struct sk_buff *) (*p_txb & ~1);
371 sp = rxrpc_skb(txb);
372
373 if (*p_txb & 1) {
374 /* packet must have been discarded */
Rusty Russell3db1cd52011-12-19 13:56:45 +0000375 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700376 *p_txb &= ~1;
377 resend |= 1;
378 } else if (sp->need_resend) {
379 ;
380 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000381 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700382 resend |= 1;
383 } else if (resend & 2) {
384 if (time_before(sp->resend_at, resend_at))
385 resend_at = sp->resend_at;
386 } else {
387 resend_at = sp->resend_at;
388 resend |= 2;
389 }
390 }
391
392 rxrpc_set_resend(call, resend, resend_at);
393 _leave(" = 0");
394 return 0;
395
396protocol_error:
397 _leave(" = -EPROTO");
398 return -EPROTO;
399}
400
401/*
402 * discard hard-ACK'd packets from the Tx window
403 */
404static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard)
405{
David Howells17926a72007-04-26 15:48:28 -0700406 unsigned long _skb;
407 int tail = call->acks_tail, old_tail;
408 int win = CIRC_CNT(call->acks_head, tail, call->acks_winsz);
409
410 _enter("{%u,%u},%u", call->acks_hard, win, hard);
411
412 ASSERTCMP(hard - call->acks_hard, <=, win);
413
414 while (call->acks_hard < hard) {
415 smp_read_barrier_depends();
416 _skb = call->acks_window[tail] & ~1;
David Howells17926a72007-04-26 15:48:28 -0700417 rxrpc_free_skb((struct sk_buff *) _skb);
418 old_tail = tail;
419 tail = (tail + 1) & (call->acks_winsz - 1);
420 call->acks_tail = tail;
421 if (call->acks_unacked == old_tail)
422 call->acks_unacked = tail;
423 call->acks_hard++;
424 }
425
426 wake_up(&call->tx_waitq);
427}
428
429/*
430 * clear the Tx window in the event of a failure
431 */
432static void rxrpc_clear_tx_window(struct rxrpc_call *call)
433{
434 rxrpc_rotate_tx_window(call, atomic_read(&call->sequence));
435}
436
437/*
438 * drain the out of sequence received packet queue into the packet Rx queue
439 */
440static int rxrpc_drain_rx_oos_queue(struct rxrpc_call *call)
441{
442 struct rxrpc_skb_priv *sp;
443 struct sk_buff *skb;
444 bool terminal;
445 int ret;
446
447 _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos);
448
449 spin_lock_bh(&call->lock);
450
451 ret = -ECONNRESET;
452 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
453 goto socket_unavailable;
454
455 skb = skb_dequeue(&call->rx_oos_queue);
456 if (skb) {
457 sp = rxrpc_skb(skb);
458
459 _debug("drain OOS packet %d [%d]",
460 ntohl(sp->hdr.seq), call->rx_first_oos);
461
462 if (ntohl(sp->hdr.seq) != call->rx_first_oos) {
463 skb_queue_head(&call->rx_oos_queue, skb);
464 call->rx_first_oos = ntohl(rxrpc_skb(skb)->hdr.seq);
465 _debug("requeue %p {%u}", skb, call->rx_first_oos);
466 } else {
467 skb->mark = RXRPC_SKB_MARK_DATA;
468 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
469 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
470 ret = rxrpc_queue_rcv_skb(call, skb, true, terminal);
471 BUG_ON(ret < 0);
472 _debug("drain #%u", call->rx_data_post);
473 call->rx_data_post++;
474
475 /* find out what the next packet is */
476 skb = skb_peek(&call->rx_oos_queue);
477 if (skb)
478 call->rx_first_oos =
479 ntohl(rxrpc_skb(skb)->hdr.seq);
480 else
481 call->rx_first_oos = 0;
482 _debug("peek %p {%u}", skb, call->rx_first_oos);
483 }
484 }
485
486 ret = 0;
487socket_unavailable:
488 spin_unlock_bh(&call->lock);
489 _leave(" = %d", ret);
490 return ret;
491}
492
493/*
494 * insert an out of sequence packet into the buffer
495 */
496static void rxrpc_insert_oos_packet(struct rxrpc_call *call,
497 struct sk_buff *skb)
498{
499 struct rxrpc_skb_priv *sp, *psp;
500 struct sk_buff *p;
501 u32 seq;
502
503 sp = rxrpc_skb(skb);
504 seq = ntohl(sp->hdr.seq);
505 _enter(",,{%u}", seq);
506
507 skb->destructor = rxrpc_packet_destructor;
508 ASSERTCMP(sp->call, ==, NULL);
509 sp->call = call;
510 rxrpc_get_call(call);
511
512 /* insert into the buffer in sequence order */
513 spin_lock_bh(&call->lock);
514
515 skb_queue_walk(&call->rx_oos_queue, p) {
516 psp = rxrpc_skb(p);
517 if (ntohl(psp->hdr.seq) > seq) {
518 _debug("insert oos #%u before #%u",
519 seq, ntohl(psp->hdr.seq));
520 skb_insert(p, skb, &call->rx_oos_queue);
521 goto inserted;
522 }
523 }
524
525 _debug("append oos #%u", seq);
526 skb_queue_tail(&call->rx_oos_queue, skb);
527inserted:
528
529 /* we might now have a new front to the queue */
530 if (call->rx_first_oos == 0 || seq < call->rx_first_oos)
531 call->rx_first_oos = seq;
532
533 read_lock(&call->state_lock);
534 if (call->state < RXRPC_CALL_COMPLETE &&
535 call->rx_data_post == call->rx_first_oos) {
536 _debug("drain rx oos now");
537 set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events);
538 }
539 read_unlock(&call->state_lock);
540
541 spin_unlock_bh(&call->lock);
542 _leave(" [stored #%u]", call->rx_first_oos);
543}
544
545/*
546 * clear the Tx window on final ACK reception
547 */
548static void rxrpc_zap_tx_window(struct rxrpc_call *call)
549{
550 struct rxrpc_skb_priv *sp;
551 struct sk_buff *skb;
552 unsigned long _skb, *acks_window;
David Howells4e36a952009-09-16 00:01:13 -0700553 u8 winsz = call->acks_winsz;
David Howells17926a72007-04-26 15:48:28 -0700554 int tail;
555
556 acks_window = call->acks_window;
557 call->acks_window = NULL;
558
559 while (CIRC_CNT(call->acks_head, call->acks_tail, winsz) > 0) {
560 tail = call->acks_tail;
561 smp_read_barrier_depends();
562 _skb = acks_window[tail] & ~1;
563 smp_mb();
564 call->acks_tail = (call->acks_tail + 1) & (winsz - 1);
565
566 skb = (struct sk_buff *) _skb;
567 sp = rxrpc_skb(skb);
568 _debug("+++ clear Tx %u", ntohl(sp->hdr.seq));
569 rxrpc_free_skb(skb);
570 }
571
572 kfree(acks_window);
573}
574
575/*
David Howells224711d2007-05-04 12:41:11 -0700576 * process the extra information that may be appended to an ACK packet
577 */
578static void rxrpc_extract_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
Eric Dumazet95c96172012-04-15 05:58:06 +0000579 unsigned int latest, int nAcks)
David Howells224711d2007-05-04 12:41:11 -0700580{
581 struct rxrpc_ackinfo ackinfo;
582 struct rxrpc_peer *peer;
Eric Dumazet95c96172012-04-15 05:58:06 +0000583 unsigned int mtu;
David Howells224711d2007-05-04 12:41:11 -0700584
585 if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) {
586 _leave(" [no ackinfo]");
587 return;
588 }
589
590 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
591 latest,
592 ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU),
593 ntohl(ackinfo.rwind), ntohl(ackinfo.jumbo_max));
594
595 mtu = min(ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU));
596
597 peer = call->conn->trans->peer;
598 if (mtu < peer->maxdata) {
599 spin_lock_bh(&peer->lock);
600 peer->maxdata = mtu;
601 peer->mtu = mtu + peer->hdrsize;
602 spin_unlock_bh(&peer->lock);
603 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
604 }
605}
606
607/*
David Howells17926a72007-04-26 15:48:28 -0700608 * process packets in the reception queue
609 */
610static int rxrpc_process_rx_queue(struct rxrpc_call *call,
611 u32 *_abort_code)
612{
613 struct rxrpc_ackpacket ack;
614 struct rxrpc_skb_priv *sp;
615 struct sk_buff *skb;
616 bool post_ACK;
617 int latest;
618 u32 hard, tx;
619
620 _enter("");
621
622process_further:
623 skb = skb_dequeue(&call->rx_queue);
624 if (!skb)
625 return -EAGAIN;
626
627 _net("deferred skb %p", skb);
628
629 sp = rxrpc_skb(skb);
630
631 _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state);
632
633 post_ACK = false;
634
635 switch (sp->hdr.type) {
636 /* data packets that wind up here have been received out of
637 * order, need security processing or are jumbo packets */
638 case RXRPC_PACKET_TYPE_DATA:
639 _proto("OOSQ DATA %%%u { #%u }",
640 ntohl(sp->hdr.serial), ntohl(sp->hdr.seq));
641
642 /* secured packets must be verified and possibly decrypted */
643 if (rxrpc_verify_packet(call, skb, _abort_code) < 0)
644 goto protocol_error;
645
646 rxrpc_insert_oos_packet(call, skb);
647 goto process_further;
648
649 /* partial ACK to process */
650 case RXRPC_PACKET_TYPE_ACK:
651 if (skb_copy_bits(skb, 0, &ack, sizeof(ack)) < 0) {
652 _debug("extraction failure");
653 goto protocol_error;
654 }
655 if (!skb_pull(skb, sizeof(ack)))
656 BUG();
657
658 latest = ntohl(sp->hdr.serial);
659 hard = ntohl(ack.firstPacket);
660 tx = atomic_read(&call->sequence);
661
662 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
663 latest,
664 ntohs(ack.maxSkew),
665 hard,
666 ntohl(ack.previousPacket),
667 ntohl(ack.serial),
Dan Carpenter08d4d212014-01-20 13:28:59 +0300668 rxrpc_acks(ack.reason),
David Howells17926a72007-04-26 15:48:28 -0700669 ack.nAcks);
670
David Howells224711d2007-05-04 12:41:11 -0700671 rxrpc_extract_ackinfo(call, skb, latest, ack.nAcks);
672
David Howells17926a72007-04-26 15:48:28 -0700673 if (ack.reason == RXRPC_ACK_PING) {
674 _proto("Rx ACK %%%u PING Request", latest);
675 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
676 sp->hdr.serial, true);
677 }
678
679 /* discard any out-of-order or duplicate ACKs */
680 if (latest - call->acks_latest <= 0) {
681 _debug("discard ACK %d <= %d",
682 latest, call->acks_latest);
683 goto discard;
684 }
685 call->acks_latest = latest;
686
687 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
688 call->state != RXRPC_CALL_CLIENT_AWAIT_REPLY &&
689 call->state != RXRPC_CALL_SERVER_SEND_REPLY &&
690 call->state != RXRPC_CALL_SERVER_AWAIT_ACK)
691 goto discard;
692
693 _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state);
694
695 if (hard > 0) {
696 if (hard - 1 > tx) {
697 _debug("hard-ACK'd packet %d not transmitted"
698 " (%d top)",
699 hard - 1, tx);
700 goto protocol_error;
701 }
702
703 if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY ||
704 call->state == RXRPC_CALL_SERVER_AWAIT_ACK) &&
705 hard > tx)
706 goto all_acked;
707
708 smp_rmb();
709 rxrpc_rotate_tx_window(call, hard - 1);
710 }
711
712 if (ack.nAcks > 0) {
713 if (hard - 1 + ack.nAcks > tx) {
714 _debug("soft-ACK'd packet %d+%d not"
715 " transmitted (%d top)",
716 hard - 1, ack.nAcks, tx);
717 goto protocol_error;
718 }
719
720 if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0)
721 goto protocol_error;
722 }
723 goto discard;
724
725 /* complete ACK to process */
726 case RXRPC_PACKET_TYPE_ACKALL:
727 goto all_acked;
728
729 /* abort and busy are handled elsewhere */
730 case RXRPC_PACKET_TYPE_BUSY:
731 case RXRPC_PACKET_TYPE_ABORT:
732 BUG();
733
734 /* connection level events - also handled elsewhere */
735 case RXRPC_PACKET_TYPE_CHALLENGE:
736 case RXRPC_PACKET_TYPE_RESPONSE:
737 case RXRPC_PACKET_TYPE_DEBUG:
738 BUG();
739 }
740
741 /* if we've had a hard ACK that covers all the packets we've sent, then
742 * that ends that phase of the operation */
743all_acked:
744 write_lock_bh(&call->state_lock);
745 _debug("ack all %d", call->state);
746
747 switch (call->state) {
748 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
749 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
750 break;
751 case RXRPC_CALL_SERVER_AWAIT_ACK:
752 _debug("srv complete");
753 call->state = RXRPC_CALL_COMPLETE;
754 post_ACK = true;
755 break;
756 case RXRPC_CALL_CLIENT_SEND_REQUEST:
757 case RXRPC_CALL_SERVER_RECV_REQUEST:
758 goto protocol_error_unlock; /* can't occur yet */
759 default:
760 write_unlock_bh(&call->state_lock);
761 goto discard; /* assume packet left over from earlier phase */
762 }
763
764 write_unlock_bh(&call->state_lock);
765
766 /* if all the packets we sent are hard-ACK'd, then we can discard
767 * whatever we've got left */
768 _debug("clear Tx %d",
769 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
770
771 del_timer_sync(&call->resend_timer);
772 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
773 clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events);
774
775 if (call->acks_window)
776 rxrpc_zap_tx_window(call);
777
778 if (post_ACK) {
779 /* post the final ACK message for userspace to pick up */
780 _debug("post ACK");
781 skb->mark = RXRPC_SKB_MARK_FINAL_ACK;
782 sp->call = call;
783 rxrpc_get_call(call);
784 spin_lock_bh(&call->lock);
785 if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0)
786 BUG();
787 spin_unlock_bh(&call->lock);
788 goto process_further;
789 }
790
791discard:
792 rxrpc_free_skb(skb);
793 goto process_further;
794
795protocol_error_unlock:
796 write_unlock_bh(&call->state_lock);
797protocol_error:
798 rxrpc_free_skb(skb);
799 _leave(" = -EPROTO");
800 return -EPROTO;
801}
802
803/*
804 * post a message to the socket Rx queue for recvmsg() to pick up
805 */
806static int rxrpc_post_message(struct rxrpc_call *call, u32 mark, u32 error,
807 bool fatal)
808{
809 struct rxrpc_skb_priv *sp;
810 struct sk_buff *skb;
811 int ret;
812
813 _enter("{%d,%lx},%u,%u,%d",
814 call->debug_id, call->flags, mark, error, fatal);
815
816 /* remove timers and things for fatal messages */
817 if (fatal) {
818 del_timer_sync(&call->resend_timer);
819 del_timer_sync(&call->ack_timer);
820 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
821 }
822
823 if (mark != RXRPC_SKB_MARK_NEW_CALL &&
824 !test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
825 _leave("[no userid]");
826 return 0;
827 }
828
829 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
830 skb = alloc_skb(0, GFP_NOFS);
831 if (!skb)
832 return -ENOMEM;
833
834 rxrpc_new_skb(skb);
835
836 skb->mark = mark;
837
838 sp = rxrpc_skb(skb);
839 memset(sp, 0, sizeof(*sp));
840 sp->error = error;
841 sp->call = call;
842 rxrpc_get_call(call);
843
844 spin_lock_bh(&call->lock);
845 ret = rxrpc_queue_rcv_skb(call, skb, true, fatal);
846 spin_unlock_bh(&call->lock);
Julia Lawall163e3cb2008-02-17 18:42:03 -0800847 BUG_ON(ret < 0);
David Howells17926a72007-04-26 15:48:28 -0700848 }
849
850 return 0;
851}
852
853/*
854 * handle background processing of incoming call packets and ACK / abort
855 * generation
856 */
857void rxrpc_process_call(struct work_struct *work)
858{
859 struct rxrpc_call *call =
860 container_of(work, struct rxrpc_call, processor);
861 struct rxrpc_ackpacket ack;
862 struct rxrpc_ackinfo ackinfo;
863 struct rxrpc_header hdr;
864 struct msghdr msg;
865 struct kvec iov[5];
866 unsigned long bits;
David Howells224711d2007-05-04 12:41:11 -0700867 __be32 data, pad;
David Howells17926a72007-04-26 15:48:28 -0700868 size_t len;
David Howells224711d2007-05-04 12:41:11 -0700869 int genbit, loop, nbit, ioc, ret, mtu;
David Howells17926a72007-04-26 15:48:28 -0700870 u32 abort_code = RX_PROTOCOL_ERROR;
871 u8 *acks = NULL;
872
873 //printk("\n--------------------\n");
874 _enter("{%d,%s,%lx} [%lu]",
875 call->debug_id, rxrpc_call_states[call->state], call->events,
876 (jiffies - call->creation_jif) / (HZ / 10));
877
878 if (test_and_set_bit(RXRPC_CALL_PROC_BUSY, &call->flags)) {
879 _debug("XXXXXXXXXXXXX RUNNING ON MULTIPLE CPUS XXXXXXXXXXXXX");
880 return;
881 }
882
883 /* there's a good chance we're going to have to send a message, so set
884 * one up in advance */
885 msg.msg_name = &call->conn->trans->peer->srx.transport.sin;
886 msg.msg_namelen = sizeof(call->conn->trans->peer->srx.transport.sin);
887 msg.msg_control = NULL;
888 msg.msg_controllen = 0;
889 msg.msg_flags = 0;
890
891 hdr.epoch = call->conn->epoch;
892 hdr.cid = call->cid;
893 hdr.callNumber = call->call_id;
894 hdr.seq = 0;
895 hdr.type = RXRPC_PACKET_TYPE_ACK;
896 hdr.flags = call->conn->out_clientflag;
897 hdr.userStatus = 0;
898 hdr.securityIndex = call->conn->security_ix;
899 hdr._rsvd = 0;
900 hdr.serviceId = call->conn->service_id;
901
902 memset(iov, 0, sizeof(iov));
903 iov[0].iov_base = &hdr;
904 iov[0].iov_len = sizeof(hdr);
905
906 /* deal with events of a final nature */
907 if (test_bit(RXRPC_CALL_RELEASE, &call->events)) {
908 rxrpc_release_call(call);
909 clear_bit(RXRPC_CALL_RELEASE, &call->events);
910 }
911
912 if (test_bit(RXRPC_CALL_RCVD_ERROR, &call->events)) {
913 int error;
914
915 clear_bit(RXRPC_CALL_CONN_ABORT, &call->events);
916 clear_bit(RXRPC_CALL_REJECT_BUSY, &call->events);
917 clear_bit(RXRPC_CALL_ABORT, &call->events);
918
919 error = call->conn->trans->peer->net_error;
920 _debug("post net error %d", error);
921
922 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NET_ERROR,
923 error, true) < 0)
924 goto no_mem;
925 clear_bit(RXRPC_CALL_RCVD_ERROR, &call->events);
926 goto kill_ACKs;
927 }
928
929 if (test_bit(RXRPC_CALL_CONN_ABORT, &call->events)) {
930 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
931
932 clear_bit(RXRPC_CALL_REJECT_BUSY, &call->events);
933 clear_bit(RXRPC_CALL_ABORT, &call->events);
934
935 _debug("post conn abort");
936
937 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
938 call->conn->error, true) < 0)
939 goto no_mem;
940 clear_bit(RXRPC_CALL_CONN_ABORT, &call->events);
941 goto kill_ACKs;
942 }
943
944 if (test_bit(RXRPC_CALL_REJECT_BUSY, &call->events)) {
945 hdr.type = RXRPC_PACKET_TYPE_BUSY;
946 genbit = RXRPC_CALL_REJECT_BUSY;
947 goto send_message;
948 }
949
950 if (test_bit(RXRPC_CALL_ABORT, &call->events)) {
951 ASSERTCMP(call->state, >, RXRPC_CALL_COMPLETE);
952
953 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
954 ECONNABORTED, true) < 0)
955 goto no_mem;
956 hdr.type = RXRPC_PACKET_TYPE_ABORT;
957 data = htonl(call->abort_code);
958 iov[1].iov_base = &data;
959 iov[1].iov_len = sizeof(data);
960 genbit = RXRPC_CALL_ABORT;
961 goto send_message;
962 }
963
964 if (test_bit(RXRPC_CALL_ACK_FINAL, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700965 genbit = RXRPC_CALL_ACK_FINAL;
David Howells224711d2007-05-04 12:41:11 -0700966
967 ack.bufferSpace = htons(8);
968 ack.maxSkew = 0;
969 ack.serial = 0;
970 ack.reason = RXRPC_ACK_IDLE;
971 ack.nAcks = 0;
972 call->ackr_reason = 0;
973
974 spin_lock_bh(&call->lock);
975 ack.serial = call->ackr_serial;
976 ack.previousPacket = call->ackr_prev_seq;
977 ack.firstPacket = htonl(call->rx_data_eaten + 1);
978 spin_unlock_bh(&call->lock);
979
980 pad = 0;
981
982 iov[1].iov_base = &ack;
983 iov[1].iov_len = sizeof(ack);
984 iov[2].iov_base = &pad;
985 iov[2].iov_len = 3;
986 iov[3].iov_base = &ackinfo;
987 iov[3].iov_len = sizeof(ackinfo);
988 goto send_ACK;
David Howells17926a72007-04-26 15:48:28 -0700989 }
990
991 if (call->events & ((1 << RXRPC_CALL_RCVD_BUSY) |
992 (1 << RXRPC_CALL_RCVD_ABORT))
993 ) {
994 u32 mark;
995
996 if (test_bit(RXRPC_CALL_RCVD_ABORT, &call->events))
997 mark = RXRPC_SKB_MARK_REMOTE_ABORT;
998 else
999 mark = RXRPC_SKB_MARK_BUSY;
1000
1001 _debug("post abort/busy");
1002 rxrpc_clear_tx_window(call);
1003 if (rxrpc_post_message(call, mark, ECONNABORTED, true) < 0)
1004 goto no_mem;
1005
1006 clear_bit(RXRPC_CALL_RCVD_BUSY, &call->events);
1007 clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
1008 goto kill_ACKs;
1009 }
1010
1011 if (test_and_clear_bit(RXRPC_CALL_RCVD_ACKALL, &call->events)) {
1012 _debug("do implicit ackall");
1013 rxrpc_clear_tx_window(call);
1014 }
1015
1016 if (test_bit(RXRPC_CALL_LIFE_TIMER, &call->events)) {
1017 write_lock_bh(&call->state_lock);
1018 if (call->state <= RXRPC_CALL_COMPLETE) {
1019 call->state = RXRPC_CALL_LOCALLY_ABORTED;
1020 call->abort_code = RX_CALL_TIMEOUT;
1021 set_bit(RXRPC_CALL_ABORT, &call->events);
1022 }
1023 write_unlock_bh(&call->state_lock);
1024
1025 _debug("post timeout");
1026 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
1027 ETIME, true) < 0)
1028 goto no_mem;
1029
1030 clear_bit(RXRPC_CALL_LIFE_TIMER, &call->events);
1031 goto kill_ACKs;
1032 }
1033
1034 /* deal with assorted inbound messages */
1035 if (!skb_queue_empty(&call->rx_queue)) {
1036 switch (rxrpc_process_rx_queue(call, &abort_code)) {
1037 case 0:
1038 case -EAGAIN:
1039 break;
1040 case -ENOMEM:
1041 goto no_mem;
1042 case -EKEYEXPIRED:
1043 case -EKEYREJECTED:
1044 case -EPROTO:
1045 rxrpc_abort_call(call, abort_code);
1046 goto kill_ACKs;
1047 }
1048 }
1049
1050 /* handle resending */
1051 if (test_and_clear_bit(RXRPC_CALL_RESEND_TIMER, &call->events))
1052 rxrpc_resend_timer(call);
1053 if (test_and_clear_bit(RXRPC_CALL_RESEND, &call->events))
1054 rxrpc_resend(call);
1055
1056 /* consider sending an ordinary ACK */
1057 if (test_bit(RXRPC_CALL_ACK, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001058 _debug("send ACK: window: %d - %d { %lx }",
1059 call->rx_data_eaten, call->ackr_win_top,
1060 call->ackr_window[0]);
1061
1062 if (call->state > RXRPC_CALL_SERVER_ACK_REQUEST &&
1063 call->ackr_reason != RXRPC_ACK_PING_RESPONSE) {
1064 /* ACK by sending reply DATA packet in this state */
1065 clear_bit(RXRPC_CALL_ACK, &call->events);
1066 goto maybe_reschedule;
1067 }
1068
1069 genbit = RXRPC_CALL_ACK;
1070
1071 acks = kzalloc(call->ackr_win_top - call->rx_data_eaten,
1072 GFP_NOFS);
1073 if (!acks)
1074 goto no_mem;
1075
1076 //hdr.flags = RXRPC_SLOW_START_OK;
1077 ack.bufferSpace = htons(8);
1078 ack.maxSkew = 0;
1079 ack.serial = 0;
1080 ack.reason = 0;
1081
David Howells17926a72007-04-26 15:48:28 -07001082 spin_lock_bh(&call->lock);
1083 ack.reason = call->ackr_reason;
1084 ack.serial = call->ackr_serial;
1085 ack.previousPacket = call->ackr_prev_seq;
1086 ack.firstPacket = htonl(call->rx_data_eaten + 1);
1087
1088 ack.nAcks = 0;
1089 for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) {
1090 nbit = loop * BITS_PER_LONG;
1091 for (bits = call->ackr_window[loop]; bits; bits >>= 1
1092 ) {
1093 _debug("- l=%d n=%d b=%lx", loop, nbit, bits);
1094 if (bits & 1) {
1095 acks[nbit] = RXRPC_ACK_TYPE_ACK;
1096 ack.nAcks = nbit + 1;
1097 }
1098 nbit++;
1099 }
1100 }
1101 call->ackr_reason = 0;
1102 spin_unlock_bh(&call->lock);
1103
1104 pad = 0;
1105
1106 iov[1].iov_base = &ack;
1107 iov[1].iov_len = sizeof(ack);
1108 iov[2].iov_base = acks;
1109 iov[2].iov_len = ack.nAcks;
1110 iov[3].iov_base = &pad;
1111 iov[3].iov_len = 3;
1112 iov[4].iov_base = &ackinfo;
1113 iov[4].iov_len = sizeof(ackinfo);
1114
1115 switch (ack.reason) {
1116 case RXRPC_ACK_REQUESTED:
1117 case RXRPC_ACK_DUPLICATE:
1118 case RXRPC_ACK_OUT_OF_SEQUENCE:
1119 case RXRPC_ACK_EXCEEDS_WINDOW:
1120 case RXRPC_ACK_NOSPACE:
1121 case RXRPC_ACK_PING:
1122 case RXRPC_ACK_PING_RESPONSE:
1123 goto send_ACK_with_skew;
1124 case RXRPC_ACK_DELAY:
1125 case RXRPC_ACK_IDLE:
1126 goto send_ACK;
1127 }
1128 }
1129
1130 /* handle completion of security negotiations on an incoming
1131 * connection */
1132 if (test_and_clear_bit(RXRPC_CALL_SECURED, &call->events)) {
1133 _debug("secured");
1134 spin_lock_bh(&call->lock);
1135
1136 if (call->state == RXRPC_CALL_SERVER_SECURING) {
1137 _debug("securing");
1138 write_lock(&call->conn->lock);
1139 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1140 !test_bit(RXRPC_CALL_RELEASE, &call->events)) {
1141 _debug("not released");
1142 call->state = RXRPC_CALL_SERVER_ACCEPTING;
1143 list_move_tail(&call->accept_link,
1144 &call->socket->acceptq);
1145 }
1146 write_unlock(&call->conn->lock);
1147 read_lock(&call->state_lock);
1148 if (call->state < RXRPC_CALL_COMPLETE)
1149 set_bit(RXRPC_CALL_POST_ACCEPT, &call->events);
1150 read_unlock(&call->state_lock);
1151 }
1152
1153 spin_unlock_bh(&call->lock);
1154 if (!test_bit(RXRPC_CALL_POST_ACCEPT, &call->events))
1155 goto maybe_reschedule;
1156 }
1157
1158 /* post a notification of an acceptable connection to the app */
1159 if (test_bit(RXRPC_CALL_POST_ACCEPT, &call->events)) {
1160 _debug("post accept");
1161 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NEW_CALL,
1162 0, false) < 0)
1163 goto no_mem;
1164 clear_bit(RXRPC_CALL_POST_ACCEPT, &call->events);
1165 goto maybe_reschedule;
1166 }
1167
1168 /* handle incoming call acceptance */
1169 if (test_and_clear_bit(RXRPC_CALL_ACCEPTED, &call->events)) {
1170 _debug("accepted");
1171 ASSERTCMP(call->rx_data_post, ==, 0);
1172 call->rx_data_post = 1;
1173 read_lock_bh(&call->state_lock);
1174 if (call->state < RXRPC_CALL_COMPLETE)
1175 set_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events);
1176 read_unlock_bh(&call->state_lock);
1177 }
1178
1179 /* drain the out of sequence received packet queue into the packet Rx
1180 * queue */
1181 if (test_and_clear_bit(RXRPC_CALL_DRAIN_RX_OOS, &call->events)) {
1182 while (call->rx_data_post == call->rx_first_oos)
1183 if (rxrpc_drain_rx_oos_queue(call) < 0)
1184 break;
1185 goto maybe_reschedule;
1186 }
1187
1188 /* other events may have been raised since we started checking */
1189 goto maybe_reschedule;
1190
1191send_ACK_with_skew:
1192 ack.maxSkew = htons(atomic_read(&call->conn->hi_serial) -
1193 ntohl(ack.serial));
1194send_ACK:
David Howells224711d2007-05-04 12:41:11 -07001195 mtu = call->conn->trans->peer->if_mtu;
1196 mtu -= call->conn->trans->peer->hdrsize;
1197 ackinfo.maxMTU = htonl(mtu);
1198 ackinfo.rwind = htonl(32);
1199
1200 /* permit the peer to send us jumbo packets if it wants to */
1201 ackinfo.rxMTU = htonl(5692);
1202 ackinfo.jumbo_max = htonl(4);
1203
David Howells17926a72007-04-26 15:48:28 -07001204 hdr.serial = htonl(atomic_inc_return(&call->conn->serial));
1205 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
1206 ntohl(hdr.serial),
1207 ntohs(ack.maxSkew),
1208 ntohl(ack.firstPacket),
1209 ntohl(ack.previousPacket),
1210 ntohl(ack.serial),
Dan Carpenter08d4d212014-01-20 13:28:59 +03001211 rxrpc_acks(ack.reason),
David Howells17926a72007-04-26 15:48:28 -07001212 ack.nAcks);
1213
1214 del_timer_sync(&call->ack_timer);
1215 if (ack.nAcks > 0)
1216 set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags);
1217 goto send_message_2;
1218
1219send_message:
1220 _debug("send message");
1221
1222 hdr.serial = htonl(atomic_inc_return(&call->conn->serial));
1223 _proto("Tx %s %%%u", rxrpc_pkts[hdr.type], ntohl(hdr.serial));
1224send_message_2:
1225
1226 len = iov[0].iov_len;
1227 ioc = 1;
1228 if (iov[4].iov_len) {
1229 ioc = 5;
1230 len += iov[4].iov_len;
1231 len += iov[3].iov_len;
1232 len += iov[2].iov_len;
1233 len += iov[1].iov_len;
1234 } else if (iov[3].iov_len) {
1235 ioc = 4;
1236 len += iov[3].iov_len;
1237 len += iov[2].iov_len;
1238 len += iov[1].iov_len;
1239 } else if (iov[2].iov_len) {
1240 ioc = 3;
1241 len += iov[2].iov_len;
1242 len += iov[1].iov_len;
1243 } else if (iov[1].iov_len) {
1244 ioc = 2;
1245 len += iov[1].iov_len;
1246 }
1247
1248 ret = kernel_sendmsg(call->conn->trans->local->socket,
1249 &msg, iov, ioc, len);
1250 if (ret < 0) {
1251 _debug("sendmsg failed: %d", ret);
1252 read_lock_bh(&call->state_lock);
1253 if (call->state < RXRPC_CALL_DEAD)
David Howells651350d2007-04-26 15:50:17 -07001254 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001255 read_unlock_bh(&call->state_lock);
1256 goto error;
1257 }
1258
1259 switch (genbit) {
1260 case RXRPC_CALL_ABORT:
1261 clear_bit(genbit, &call->events);
1262 clear_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
1263 goto kill_ACKs;
1264
1265 case RXRPC_CALL_ACK_FINAL:
1266 write_lock_bh(&call->state_lock);
1267 if (call->state == RXRPC_CALL_CLIENT_FINAL_ACK)
1268 call->state = RXRPC_CALL_COMPLETE;
1269 write_unlock_bh(&call->state_lock);
1270 goto kill_ACKs;
1271
1272 default:
1273 clear_bit(genbit, &call->events);
1274 switch (call->state) {
1275 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
1276 case RXRPC_CALL_CLIENT_RECV_REPLY:
1277 case RXRPC_CALL_SERVER_RECV_REQUEST:
1278 case RXRPC_CALL_SERVER_ACK_REQUEST:
1279 _debug("start ACK timer");
1280 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY,
1281 call->ackr_serial, false);
1282 default:
1283 break;
1284 }
1285 goto maybe_reschedule;
1286 }
1287
1288kill_ACKs:
1289 del_timer_sync(&call->ack_timer);
1290 if (test_and_clear_bit(RXRPC_CALL_ACK_FINAL, &call->events))
1291 rxrpc_put_call(call);
1292 clear_bit(RXRPC_CALL_ACK, &call->events);
1293
1294maybe_reschedule:
1295 if (call->events || !skb_queue_empty(&call->rx_queue)) {
1296 read_lock_bh(&call->state_lock);
1297 if (call->state < RXRPC_CALL_DEAD)
David Howells651350d2007-04-26 15:50:17 -07001298 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001299 read_unlock_bh(&call->state_lock);
1300 }
1301
1302 /* don't leave aborted connections on the accept queue */
1303 if (call->state >= RXRPC_CALL_COMPLETE &&
1304 !list_empty(&call->accept_link)) {
1305 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
1306 call, call->events, call->flags,
1307 ntohl(call->conn->cid));
1308
1309 read_lock_bh(&call->state_lock);
1310 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
1311 !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
David Howells651350d2007-04-26 15:50:17 -07001312 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001313 read_unlock_bh(&call->state_lock);
1314 }
1315
1316error:
1317 clear_bit(RXRPC_CALL_PROC_BUSY, &call->flags);
1318 kfree(acks);
1319
1320 /* because we don't want two CPUs both processing the work item for one
1321 * call at the same time, we use a flag to note when it's busy; however
1322 * this means there's a race between clearing the flag and setting the
1323 * work pending bit and the work item being processed again */
1324 if (call->events && !work_pending(&call->processor)) {
1325 _debug("jumpstart %x", ntohl(call->conn->cid));
David Howells651350d2007-04-26 15:50:17 -07001326 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001327 }
1328
1329 _leave("");
1330 return;
1331
1332no_mem:
1333 _debug("out of memory");
1334 goto maybe_reschedule;
1335}