blob: 94c7751fd99a5adcdf69b10fcee28dc810b224e3 [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
Joe Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/module.h>
15#include <linux/circ_buf.h>
16#include <linux/net.h>
17#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070019#include <linux/udp.h>
20#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include "ar-internal.h"
23
David Howells5873c082014-02-07 18:58:44 +000024/*
David Howells17926a72007-04-26 15:48:28 -070025 * propose an ACK be sent
26 */
David Howells4e36a952009-09-16 00:01:13 -070027void __rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
David Howells563ea7d2016-08-23 15:27:25 +010028 u16 skew, u32 serial, bool immediate)
David Howells17926a72007-04-26 15:48:28 -070029{
30 unsigned long expiry;
31 s8 prior = rxrpc_ack_priority[ack_reason];
32
33 ASSERTCMP(prior, >, 0);
34
35 _enter("{%d},%s,%%%x,%u",
David Howells0d12f8a2016-03-04 15:53:46 +000036 call->debug_id, rxrpc_acks(ack_reason), serial, immediate);
David Howells17926a72007-04-26 15:48:28 -070037
38 if (prior < rxrpc_ack_priority[call->ackr_reason]) {
39 if (immediate)
40 goto cancel_timer;
41 return;
42 }
43
44 /* update DELAY, IDLE, REQUESTED and PING_RESPONSE ACK serial
45 * numbers */
46 if (prior == rxrpc_ack_priority[call->ackr_reason]) {
David Howells563ea7d2016-08-23 15:27:25 +010047 if (prior <= 4) {
48 call->ackr_skew = skew;
David Howells17926a72007-04-26 15:48:28 -070049 call->ackr_serial = serial;
David Howells563ea7d2016-08-23 15:27:25 +010050 }
David Howells17926a72007-04-26 15:48:28 -070051 if (immediate)
52 goto cancel_timer;
53 return;
54 }
55
56 call->ackr_reason = ack_reason;
57 call->ackr_serial = serial;
58
59 switch (ack_reason) {
60 case RXRPC_ACK_DELAY:
61 _debug("run delay timer");
David Howells5873c082014-02-07 18:58:44 +000062 expiry = rxrpc_soft_ack_delay;
63 goto run_timer;
David Howells17926a72007-04-26 15:48:28 -070064
65 case RXRPC_ACK_IDLE:
66 if (!immediate) {
67 _debug("run defer timer");
David Howells5873c082014-02-07 18:58:44 +000068 expiry = rxrpc_idle_ack_delay;
David Howells17926a72007-04-26 15:48:28 -070069 goto run_timer;
70 }
71 goto cancel_timer;
72
73 case RXRPC_ACK_REQUESTED:
David Howells5873c082014-02-07 18:58:44 +000074 expiry = rxrpc_requested_ack_delay;
75 if (!expiry)
David Howells17926a72007-04-26 15:48:28 -070076 goto cancel_timer;
David Howells0d12f8a2016-03-04 15:53:46 +000077 if (!immediate || serial == 1) {
David Howells17926a72007-04-26 15:48:28 -070078 _debug("run defer timer");
David Howells17926a72007-04-26 15:48:28 -070079 goto run_timer;
80 }
81
82 default:
83 _debug("immediate ACK");
84 goto cancel_timer;
85 }
86
87run_timer:
88 expiry += jiffies;
89 if (!timer_pending(&call->ack_timer) ||
90 time_after(call->ack_timer.expires, expiry))
91 mod_timer(&call->ack_timer, expiry);
92 return;
93
94cancel_timer:
David Howells0d12f8a2016-03-04 15:53:46 +000095 _debug("cancel timer %%%u", serial);
David Howells17926a72007-04-26 15:48:28 -070096 try_to_del_timer_sync(&call->ack_timer);
97 read_lock_bh(&call->state_lock);
David Howellsf5c17aa2016-08-30 09:49:28 +010098 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +000099 !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700100 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700101 read_unlock_bh(&call->state_lock);
102}
103
104/*
105 * propose an ACK be sent, locking the call structure
106 */
David Howells4e36a952009-09-16 00:01:13 -0700107void rxrpc_propose_ACK(struct rxrpc_call *call, u8 ack_reason,
David Howells563ea7d2016-08-23 15:27:25 +0100108 u16 skew, u32 serial, bool immediate)
David Howells17926a72007-04-26 15:48:28 -0700109{
110 s8 prior = rxrpc_ack_priority[ack_reason];
111
112 if (prior > rxrpc_ack_priority[call->ackr_reason]) {
113 spin_lock_bh(&call->lock);
David Howells563ea7d2016-08-23 15:27:25 +0100114 __rxrpc_propose_ACK(call, ack_reason, skew, serial, immediate);
David Howells17926a72007-04-26 15:48:28 -0700115 spin_unlock_bh(&call->lock);
116 }
117}
118
119/*
120 * set the resend timer
121 */
122static void rxrpc_set_resend(struct rxrpc_call *call, u8 resend,
123 unsigned long resend_at)
124{
125 read_lock_bh(&call->state_lock);
David Howellsf5c17aa2016-08-30 09:49:28 +0100126 if (call->state == RXRPC_CALL_COMPLETE)
David Howells17926a72007-04-26 15:48:28 -0700127 resend = 0;
128
129 if (resend & 1) {
130 _debug("SET RESEND");
David Howells4c198ad2016-03-04 15:53:46 +0000131 set_bit(RXRPC_CALL_EV_RESEND, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700132 }
133
134 if (resend & 2) {
135 _debug("MODIFY RESEND TIMER");
136 set_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
137 mod_timer(&call->resend_timer, resend_at);
138 } else {
139 _debug("KILL RESEND TIMER");
140 del_timer_sync(&call->resend_timer);
David Howells4c198ad2016-03-04 15:53:46 +0000141 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700142 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
143 }
144 read_unlock_bh(&call->state_lock);
145}
146
147/*
148 * resend packets
149 */
150static void rxrpc_resend(struct rxrpc_call *call)
151{
David Howells0d12f8a2016-03-04 15:53:46 +0000152 struct rxrpc_wire_header *whdr;
David Howells17926a72007-04-26 15:48:28 -0700153 struct rxrpc_skb_priv *sp;
David Howells17926a72007-04-26 15:48:28 -0700154 struct sk_buff *txb;
155 unsigned long *p_txb, resend_at;
Florian Westphalc03ae532015-02-28 11:51:36 +0100156 bool stop;
157 int loop;
David Howells17926a72007-04-26 15:48:28 -0700158 u8 resend;
159
160 _enter("{%d,%d,%d,%d},",
161 call->acks_hard, call->acks_unacked,
162 atomic_read(&call->sequence),
163 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
164
Florian Westphalc03ae532015-02-28 11:51:36 +0100165 stop = false;
David Howells17926a72007-04-26 15:48:28 -0700166 resend = 0;
167 resend_at = 0;
168
169 for (loop = call->acks_tail;
170 loop != call->acks_head || stop;
171 loop = (loop + 1) & (call->acks_winsz - 1)
172 ) {
173 p_txb = call->acks_window + loop;
174 smp_read_barrier_depends();
175 if (*p_txb & 1)
176 continue;
177
178 txb = (struct sk_buff *) *p_txb;
179 sp = rxrpc_skb(txb);
180
181 if (sp->need_resend) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000182 sp->need_resend = false;
David Howells17926a72007-04-26 15:48:28 -0700183
184 /* each Tx packet has a new serial number */
David Howells0d12f8a2016-03-04 15:53:46 +0000185 sp->hdr.serial = atomic_inc_return(&call->conn->serial);
David Howells17926a72007-04-26 15:48:28 -0700186
David Howells0d12f8a2016-03-04 15:53:46 +0000187 whdr = (struct rxrpc_wire_header *)txb->head;
188 whdr->serial = htonl(sp->hdr.serial);
David Howells17926a72007-04-26 15:48:28 -0700189
190 _proto("Tx DATA %%%u { #%d }",
David Howells0d12f8a2016-03-04 15:53:46 +0000191 sp->hdr.serial, sp->hdr.seq);
David Howells985a5c82016-06-17 11:53:37 +0100192 if (rxrpc_send_data_packet(call->conn, txb) < 0) {
Florian Westphalc03ae532015-02-28 11:51:36 +0100193 stop = true;
David Howells17926a72007-04-26 15:48:28 -0700194 sp->resend_at = jiffies + 3;
195 } else {
David Howells45025bc2016-08-24 07:30:52 +0100196 if (rxrpc_is_client_call(call))
197 rxrpc_expose_client_call(call);
David Howells17926a72007-04-26 15:48:28 -0700198 sp->resend_at =
Florian Westphal765dd3b2015-02-28 11:51:37 +0100199 jiffies + rxrpc_resend_timeout;
David Howells17926a72007-04-26 15:48:28 -0700200 }
201 }
202
203 if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000204 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700205 resend |= 1;
206 } else if (resend & 2) {
207 if (time_before(sp->resend_at, resend_at))
208 resend_at = sp->resend_at;
209 } else {
210 resend_at = sp->resend_at;
211 resend |= 2;
212 }
213 }
214
215 rxrpc_set_resend(call, resend, resend_at);
216 _leave("");
217}
218
219/*
220 * handle resend timer expiry
221 */
222static void rxrpc_resend_timer(struct rxrpc_call *call)
223{
224 struct rxrpc_skb_priv *sp;
225 struct sk_buff *txb;
226 unsigned long *p_txb, resend_at;
227 int loop;
228 u8 resend;
229
230 _enter("%d,%d,%d",
231 call->acks_tail, call->acks_unacked, call->acks_head);
232
David Howellsf5c17aa2016-08-30 09:49:28 +0100233 if (call->state == RXRPC_CALL_COMPLETE)
David Howells3b5bac22010-08-04 02:34:17 +0000234 return;
235
David Howells17926a72007-04-26 15:48:28 -0700236 resend = 0;
237 resend_at = 0;
238
239 for (loop = call->acks_unacked;
240 loop != call->acks_head;
241 loop = (loop + 1) & (call->acks_winsz - 1)
242 ) {
243 p_txb = call->acks_window + loop;
244 smp_read_barrier_depends();
245 txb = (struct sk_buff *) (*p_txb & ~1);
246 sp = rxrpc_skb(txb);
247
248 ASSERT(!(*p_txb & 1));
249
250 if (sp->need_resend) {
251 ;
252 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000253 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700254 resend |= 1;
255 } else if (resend & 2) {
256 if (time_before(sp->resend_at, resend_at))
257 resend_at = sp->resend_at;
258 } else {
259 resend_at = sp->resend_at;
260 resend |= 2;
261 }
262 }
263
264 rxrpc_set_resend(call, resend, resend_at);
265 _leave("");
266}
267
268/*
269 * process soft ACKs of our transmitted packets
270 * - these indicate packets the peer has or has not received, but hasn't yet
271 * given to the consumer, and so can still be discarded and re-requested
272 */
273static int rxrpc_process_soft_ACKs(struct rxrpc_call *call,
274 struct rxrpc_ackpacket *ack,
275 struct sk_buff *skb)
276{
277 struct rxrpc_skb_priv *sp;
278 struct sk_buff *txb;
279 unsigned long *p_txb, resend_at;
280 int loop;
281 u8 sacks[RXRPC_MAXACKS], resend;
282
283 _enter("{%d,%d},{%d},",
284 call->acks_hard,
285 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz),
286 ack->nAcks);
287
288 if (skb_copy_bits(skb, 0, sacks, ack->nAcks) < 0)
289 goto protocol_error;
290
291 resend = 0;
292 resend_at = 0;
293 for (loop = 0; loop < ack->nAcks; loop++) {
294 p_txb = call->acks_window;
295 p_txb += (call->acks_tail + loop) & (call->acks_winsz - 1);
296 smp_read_barrier_depends();
297 txb = (struct sk_buff *) (*p_txb & ~1);
298 sp = rxrpc_skb(txb);
299
300 switch (sacks[loop]) {
301 case RXRPC_ACK_TYPE_ACK:
Rusty Russell3db1cd52011-12-19 13:56:45 +0000302 sp->need_resend = false;
David Howells17926a72007-04-26 15:48:28 -0700303 *p_txb |= 1;
304 break;
305 case RXRPC_ACK_TYPE_NACK:
Rusty Russell3db1cd52011-12-19 13:56:45 +0000306 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700307 *p_txb &= ~1;
308 resend = 1;
309 break;
310 default:
311 _debug("Unsupported ACK type %d", sacks[loop]);
312 goto protocol_error;
313 }
314 }
315
316 smp_mb();
317 call->acks_unacked = (call->acks_tail + loop) & (call->acks_winsz - 1);
318
319 /* anything not explicitly ACK'd is implicitly NACK'd, but may just not
320 * have been received or processed yet by the far end */
321 for (loop = call->acks_unacked;
322 loop != call->acks_head;
323 loop = (loop + 1) & (call->acks_winsz - 1)
324 ) {
325 p_txb = call->acks_window + loop;
326 smp_read_barrier_depends();
327 txb = (struct sk_buff *) (*p_txb & ~1);
328 sp = rxrpc_skb(txb);
329
330 if (*p_txb & 1) {
331 /* packet must have been discarded */
Rusty Russell3db1cd52011-12-19 13:56:45 +0000332 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700333 *p_txb &= ~1;
334 resend |= 1;
335 } else if (sp->need_resend) {
336 ;
337 } else if (time_after_eq(jiffies + 1, sp->resend_at)) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000338 sp->need_resend = true;
David Howells17926a72007-04-26 15:48:28 -0700339 resend |= 1;
340 } else if (resend & 2) {
341 if (time_before(sp->resend_at, resend_at))
342 resend_at = sp->resend_at;
343 } else {
344 resend_at = sp->resend_at;
345 resend |= 2;
346 }
347 }
348
349 rxrpc_set_resend(call, resend, resend_at);
350 _leave(" = 0");
351 return 0;
352
353protocol_error:
354 _leave(" = -EPROTO");
355 return -EPROTO;
356}
357
358/*
359 * discard hard-ACK'd packets from the Tx window
360 */
361static void rxrpc_rotate_tx_window(struct rxrpc_call *call, u32 hard)
362{
David Howells17926a72007-04-26 15:48:28 -0700363 unsigned long _skb;
364 int tail = call->acks_tail, old_tail;
365 int win = CIRC_CNT(call->acks_head, tail, call->acks_winsz);
366
David Howells8f7e6e72016-04-07 17:23:09 +0100367 _enter("{%u,%u},%u", call->acks_hard, win, hard);
David Howells17926a72007-04-26 15:48:28 -0700368
369 ASSERTCMP(hard - call->acks_hard, <=, win);
370
371 while (call->acks_hard < hard) {
372 smp_read_barrier_depends();
373 _skb = call->acks_window[tail] & ~1;
David Howells17926a72007-04-26 15:48:28 -0700374 rxrpc_free_skb((struct sk_buff *) _skb);
375 old_tail = tail;
376 tail = (tail + 1) & (call->acks_winsz - 1);
377 call->acks_tail = tail;
378 if (call->acks_unacked == old_tail)
379 call->acks_unacked = tail;
380 call->acks_hard++;
381 }
382
David Howells45025bc2016-08-24 07:30:52 +0100383 wake_up(&call->waitq);
David Howells17926a72007-04-26 15:48:28 -0700384}
385
386/*
387 * clear the Tx window in the event of a failure
388 */
389static void rxrpc_clear_tx_window(struct rxrpc_call *call)
390{
391 rxrpc_rotate_tx_window(call, atomic_read(&call->sequence));
392}
393
394/*
395 * drain the out of sequence received packet queue into the packet Rx queue
396 */
397static int rxrpc_drain_rx_oos_queue(struct rxrpc_call *call)
398{
399 struct rxrpc_skb_priv *sp;
400 struct sk_buff *skb;
401 bool terminal;
402 int ret;
403
404 _enter("{%d,%d}", call->rx_data_post, call->rx_first_oos);
405
406 spin_lock_bh(&call->lock);
407
408 ret = -ECONNRESET;
409 if (test_bit(RXRPC_CALL_RELEASED, &call->flags))
410 goto socket_unavailable;
411
412 skb = skb_dequeue(&call->rx_oos_queue);
413 if (skb) {
David Howellsdf844fd2016-08-23 15:27:24 +0100414 rxrpc_see_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700415 sp = rxrpc_skb(skb);
416
417 _debug("drain OOS packet %d [%d]",
David Howells0d12f8a2016-03-04 15:53:46 +0000418 sp->hdr.seq, call->rx_first_oos);
David Howells17926a72007-04-26 15:48:28 -0700419
David Howells0d12f8a2016-03-04 15:53:46 +0000420 if (sp->hdr.seq != call->rx_first_oos) {
David Howells17926a72007-04-26 15:48:28 -0700421 skb_queue_head(&call->rx_oos_queue, skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000422 call->rx_first_oos = rxrpc_skb(skb)->hdr.seq;
David Howells17926a72007-04-26 15:48:28 -0700423 _debug("requeue %p {%u}", skb, call->rx_first_oos);
424 } else {
425 skb->mark = RXRPC_SKB_MARK_DATA;
426 terminal = ((sp->hdr.flags & RXRPC_LAST_PACKET) &&
427 !(sp->hdr.flags & RXRPC_CLIENT_INITIATED));
428 ret = rxrpc_queue_rcv_skb(call, skb, true, terminal);
429 BUG_ON(ret < 0);
430 _debug("drain #%u", call->rx_data_post);
431 call->rx_data_post++;
432
433 /* find out what the next packet is */
434 skb = skb_peek(&call->rx_oos_queue);
David Howellsdf844fd2016-08-23 15:27:24 +0100435 rxrpc_see_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700436 if (skb)
David Howells0d12f8a2016-03-04 15:53:46 +0000437 call->rx_first_oos = rxrpc_skb(skb)->hdr.seq;
David Howells17926a72007-04-26 15:48:28 -0700438 else
439 call->rx_first_oos = 0;
440 _debug("peek %p {%u}", skb, call->rx_first_oos);
441 }
442 }
443
444 ret = 0;
445socket_unavailable:
446 spin_unlock_bh(&call->lock);
447 _leave(" = %d", ret);
448 return ret;
449}
450
451/*
452 * insert an out of sequence packet into the buffer
453 */
454static void rxrpc_insert_oos_packet(struct rxrpc_call *call,
455 struct sk_buff *skb)
456{
457 struct rxrpc_skb_priv *sp, *psp;
458 struct sk_buff *p;
459 u32 seq;
460
461 sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000462 seq = sp->hdr.seq;
David Howells17926a72007-04-26 15:48:28 -0700463 _enter(",,{%u}", seq);
464
465 skb->destructor = rxrpc_packet_destructor;
466 ASSERTCMP(sp->call, ==, NULL);
467 sp->call = call;
468 rxrpc_get_call(call);
David Howells372ee162016-08-03 14:11:40 +0100469 atomic_inc(&call->skb_count);
David Howells17926a72007-04-26 15:48:28 -0700470
471 /* insert into the buffer in sequence order */
472 spin_lock_bh(&call->lock);
473
474 skb_queue_walk(&call->rx_oos_queue, p) {
475 psp = rxrpc_skb(p);
David Howells0d12f8a2016-03-04 15:53:46 +0000476 if (psp->hdr.seq > seq) {
477 _debug("insert oos #%u before #%u", seq, psp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700478 skb_insert(p, skb, &call->rx_oos_queue);
479 goto inserted;
480 }
481 }
482
483 _debug("append oos #%u", seq);
484 skb_queue_tail(&call->rx_oos_queue, skb);
485inserted:
486
487 /* we might now have a new front to the queue */
488 if (call->rx_first_oos == 0 || seq < call->rx_first_oos)
489 call->rx_first_oos = seq;
490
491 read_lock(&call->state_lock);
492 if (call->state < RXRPC_CALL_COMPLETE &&
493 call->rx_data_post == call->rx_first_oos) {
494 _debug("drain rx oos now");
David Howells4c198ad2016-03-04 15:53:46 +0000495 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700496 }
497 read_unlock(&call->state_lock);
498
499 spin_unlock_bh(&call->lock);
500 _leave(" [stored #%u]", call->rx_first_oos);
501}
502
503/*
504 * clear the Tx window on final ACK reception
505 */
506static void rxrpc_zap_tx_window(struct rxrpc_call *call)
507{
508 struct rxrpc_skb_priv *sp;
509 struct sk_buff *skb;
510 unsigned long _skb, *acks_window;
David Howells4e36a952009-09-16 00:01:13 -0700511 u8 winsz = call->acks_winsz;
David Howells17926a72007-04-26 15:48:28 -0700512 int tail;
513
514 acks_window = call->acks_window;
515 call->acks_window = NULL;
516
517 while (CIRC_CNT(call->acks_head, call->acks_tail, winsz) > 0) {
518 tail = call->acks_tail;
519 smp_read_barrier_depends();
520 _skb = acks_window[tail] & ~1;
521 smp_mb();
522 call->acks_tail = (call->acks_tail + 1) & (winsz - 1);
523
524 skb = (struct sk_buff *) _skb;
525 sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000526 _debug("+++ clear Tx %u", sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700527 rxrpc_free_skb(skb);
528 }
529
530 kfree(acks_window);
531}
532
533/*
David Howells224711d2007-05-04 12:41:11 -0700534 * process the extra information that may be appended to an ACK packet
535 */
536static void rxrpc_extract_ackinfo(struct rxrpc_call *call, struct sk_buff *skb,
Eric Dumazet95c96172012-04-15 05:58:06 +0000537 unsigned int latest, int nAcks)
David Howells224711d2007-05-04 12:41:11 -0700538{
539 struct rxrpc_ackinfo ackinfo;
540 struct rxrpc_peer *peer;
Eric Dumazet95c96172012-04-15 05:58:06 +0000541 unsigned int mtu;
David Howells224711d2007-05-04 12:41:11 -0700542
543 if (skb_copy_bits(skb, nAcks + 3, &ackinfo, sizeof(ackinfo)) < 0) {
544 _leave(" [no ackinfo]");
545 return;
546 }
547
548 _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }",
549 latest,
550 ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU),
551 ntohl(ackinfo.rwind), ntohl(ackinfo.jumbo_max));
552
553 mtu = min(ntohl(ackinfo.rxMTU), ntohl(ackinfo.maxMTU));
554
David Howells85f32272016-04-04 14:00:36 +0100555 peer = call->conn->params.peer;
David Howells224711d2007-05-04 12:41:11 -0700556 if (mtu < peer->maxdata) {
557 spin_lock_bh(&peer->lock);
558 peer->maxdata = mtu;
559 peer->mtu = mtu + peer->hdrsize;
560 spin_unlock_bh(&peer->lock);
561 _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata);
562 }
563}
564
565/*
David Howells17926a72007-04-26 15:48:28 -0700566 * process packets in the reception queue
567 */
568static int rxrpc_process_rx_queue(struct rxrpc_call *call,
569 u32 *_abort_code)
570{
571 struct rxrpc_ackpacket ack;
572 struct rxrpc_skb_priv *sp;
573 struct sk_buff *skb;
574 bool post_ACK;
575 int latest;
576 u32 hard, tx;
577
578 _enter("");
579
580process_further:
581 skb = skb_dequeue(&call->rx_queue);
582 if (!skb)
583 return -EAGAIN;
584
David Howellsdf844fd2016-08-23 15:27:24 +0100585 rxrpc_see_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700586 _net("deferred skb %p", skb);
587
588 sp = rxrpc_skb(skb);
589
590 _debug("process %s [st %d]", rxrpc_pkts[sp->hdr.type], call->state);
591
592 post_ACK = false;
593
594 switch (sp->hdr.type) {
595 /* data packets that wind up here have been received out of
596 * order, need security processing or are jumbo packets */
597 case RXRPC_PACKET_TYPE_DATA:
David Howells0d12f8a2016-03-04 15:53:46 +0000598 _proto("OOSQ DATA %%%u { #%u }", sp->hdr.serial, sp->hdr.seq);
David Howells17926a72007-04-26 15:48:28 -0700599
600 /* secured packets must be verified and possibly decrypted */
David Howellse0e4d822016-04-07 17:23:58 +0100601 if (call->conn->security->verify_packet(call, skb,
602 _abort_code) < 0)
David Howells17926a72007-04-26 15:48:28 -0700603 goto protocol_error;
604
605 rxrpc_insert_oos_packet(call, skb);
606 goto process_further;
607
608 /* partial ACK to process */
609 case RXRPC_PACKET_TYPE_ACK:
610 if (skb_copy_bits(skb, 0, &ack, sizeof(ack)) < 0) {
611 _debug("extraction failure");
612 goto protocol_error;
613 }
614 if (!skb_pull(skb, sizeof(ack)))
615 BUG();
616
David Howells0d12f8a2016-03-04 15:53:46 +0000617 latest = sp->hdr.serial;
David Howells17926a72007-04-26 15:48:28 -0700618 hard = ntohl(ack.firstPacket);
619 tx = atomic_read(&call->sequence);
620
621 _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
622 latest,
623 ntohs(ack.maxSkew),
624 hard,
625 ntohl(ack.previousPacket),
626 ntohl(ack.serial),
Dan Carpenter08d4d212014-01-20 13:28:59 +0300627 rxrpc_acks(ack.reason),
David Howells17926a72007-04-26 15:48:28 -0700628 ack.nAcks);
629
David Howells224711d2007-05-04 12:41:11 -0700630 rxrpc_extract_ackinfo(call, skb, latest, ack.nAcks);
631
David Howells17926a72007-04-26 15:48:28 -0700632 if (ack.reason == RXRPC_ACK_PING) {
633 _proto("Rx ACK %%%u PING Request", latest);
634 rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE,
David Howells563ea7d2016-08-23 15:27:25 +0100635 skb->priority, sp->hdr.serial, true);
David Howells17926a72007-04-26 15:48:28 -0700636 }
637
638 /* discard any out-of-order or duplicate ACKs */
639 if (latest - call->acks_latest <= 0) {
640 _debug("discard ACK %d <= %d",
641 latest, call->acks_latest);
642 goto discard;
643 }
644 call->acks_latest = latest;
645
646 if (call->state != RXRPC_CALL_CLIENT_SEND_REQUEST &&
647 call->state != RXRPC_CALL_CLIENT_AWAIT_REPLY &&
648 call->state != RXRPC_CALL_SERVER_SEND_REPLY &&
649 call->state != RXRPC_CALL_SERVER_AWAIT_ACK)
650 goto discard;
651
652 _debug("Tx=%d H=%u S=%d", tx, call->acks_hard, call->state);
653
654 if (hard > 0) {
655 if (hard - 1 > tx) {
656 _debug("hard-ACK'd packet %d not transmitted"
657 " (%d top)",
658 hard - 1, tx);
659 goto protocol_error;
660 }
661
662 if ((call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY ||
663 call->state == RXRPC_CALL_SERVER_AWAIT_ACK) &&
David Howells33c40e22015-11-24 14:41:59 +0000664 hard > tx) {
665 call->acks_hard = tx;
David Howells17926a72007-04-26 15:48:28 -0700666 goto all_acked;
David Howells33c40e22015-11-24 14:41:59 +0000667 }
David Howells17926a72007-04-26 15:48:28 -0700668
669 smp_rmb();
670 rxrpc_rotate_tx_window(call, hard - 1);
671 }
672
673 if (ack.nAcks > 0) {
674 if (hard - 1 + ack.nAcks > tx) {
675 _debug("soft-ACK'd packet %d+%d not"
676 " transmitted (%d top)",
677 hard - 1, ack.nAcks, tx);
678 goto protocol_error;
679 }
680
681 if (rxrpc_process_soft_ACKs(call, &ack, skb) < 0)
682 goto protocol_error;
683 }
684 goto discard;
685
686 /* complete ACK to process */
687 case RXRPC_PACKET_TYPE_ACKALL:
688 goto all_acked;
689
690 /* abort and busy are handled elsewhere */
691 case RXRPC_PACKET_TYPE_BUSY:
692 case RXRPC_PACKET_TYPE_ABORT:
693 BUG();
694
695 /* connection level events - also handled elsewhere */
696 case RXRPC_PACKET_TYPE_CHALLENGE:
697 case RXRPC_PACKET_TYPE_RESPONSE:
698 case RXRPC_PACKET_TYPE_DEBUG:
699 BUG();
700 }
701
702 /* if we've had a hard ACK that covers all the packets we've sent, then
703 * that ends that phase of the operation */
704all_acked:
705 write_lock_bh(&call->state_lock);
706 _debug("ack all %d", call->state);
707
708 switch (call->state) {
709 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
710 call->state = RXRPC_CALL_CLIENT_RECV_REPLY;
711 break;
712 case RXRPC_CALL_SERVER_AWAIT_ACK:
713 _debug("srv complete");
David Howellsf5c17aa2016-08-30 09:49:28 +0100714 __rxrpc_call_completed(call);
David Howells17926a72007-04-26 15:48:28 -0700715 post_ACK = true;
716 break;
717 case RXRPC_CALL_CLIENT_SEND_REQUEST:
718 case RXRPC_CALL_SERVER_RECV_REQUEST:
719 goto protocol_error_unlock; /* can't occur yet */
720 default:
721 write_unlock_bh(&call->state_lock);
722 goto discard; /* assume packet left over from earlier phase */
723 }
724
725 write_unlock_bh(&call->state_lock);
726
727 /* if all the packets we sent are hard-ACK'd, then we can discard
728 * whatever we've got left */
729 _debug("clear Tx %d",
730 CIRC_CNT(call->acks_head, call->acks_tail, call->acks_winsz));
731
732 del_timer_sync(&call->resend_timer);
733 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
David Howells4c198ad2016-03-04 15:53:46 +0000734 clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700735
736 if (call->acks_window)
737 rxrpc_zap_tx_window(call);
738
739 if (post_ACK) {
740 /* post the final ACK message for userspace to pick up */
741 _debug("post ACK");
742 skb->mark = RXRPC_SKB_MARK_FINAL_ACK;
743 sp->call = call;
744 rxrpc_get_call(call);
David Howells372ee162016-08-03 14:11:40 +0100745 atomic_inc(&call->skb_count);
David Howells17926a72007-04-26 15:48:28 -0700746 spin_lock_bh(&call->lock);
747 if (rxrpc_queue_rcv_skb(call, skb, true, true) < 0)
748 BUG();
749 spin_unlock_bh(&call->lock);
750 goto process_further;
751 }
752
753discard:
754 rxrpc_free_skb(skb);
755 goto process_further;
756
757protocol_error_unlock:
758 write_unlock_bh(&call->state_lock);
759protocol_error:
760 rxrpc_free_skb(skb);
761 _leave(" = -EPROTO");
762 return -EPROTO;
763}
764
765/*
766 * post a message to the socket Rx queue for recvmsg() to pick up
767 */
768static int rxrpc_post_message(struct rxrpc_call *call, u32 mark, u32 error,
769 bool fatal)
770{
771 struct rxrpc_skb_priv *sp;
772 struct sk_buff *skb;
773 int ret;
774
775 _enter("{%d,%lx},%u,%u,%d",
776 call->debug_id, call->flags, mark, error, fatal);
777
778 /* remove timers and things for fatal messages */
779 if (fatal) {
780 del_timer_sync(&call->resend_timer);
781 del_timer_sync(&call->ack_timer);
782 clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
783 }
784
785 if (mark != RXRPC_SKB_MARK_NEW_CALL &&
786 !test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
787 _leave("[no userid]");
788 return 0;
789 }
790
791 if (!test_bit(RXRPC_CALL_TERMINAL_MSG, &call->flags)) {
792 skb = alloc_skb(0, GFP_NOFS);
793 if (!skb)
794 return -ENOMEM;
795
796 rxrpc_new_skb(skb);
797
798 skb->mark = mark;
799
800 sp = rxrpc_skb(skb);
801 memset(sp, 0, sizeof(*sp));
802 sp->error = error;
803 sp->call = call;
804 rxrpc_get_call(call);
David Howells372ee162016-08-03 14:11:40 +0100805 atomic_inc(&call->skb_count);
David Howells17926a72007-04-26 15:48:28 -0700806
807 spin_lock_bh(&call->lock);
808 ret = rxrpc_queue_rcv_skb(call, skb, true, fatal);
809 spin_unlock_bh(&call->lock);
Julia Lawall163e3cb2008-02-17 18:42:03 -0800810 BUG_ON(ret < 0);
David Howells17926a72007-04-26 15:48:28 -0700811 }
812
813 return 0;
814}
815
816/*
817 * handle background processing of incoming call packets and ACK / abort
818 * generation
819 */
820void rxrpc_process_call(struct work_struct *work)
821{
822 struct rxrpc_call *call =
823 container_of(work, struct rxrpc_call, processor);
David Howells0d12f8a2016-03-04 15:53:46 +0000824 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -0700825 struct rxrpc_ackpacket ack;
826 struct rxrpc_ackinfo ackinfo;
David Howells17926a72007-04-26 15:48:28 -0700827 struct msghdr msg;
828 struct kvec iov[5];
David Howells5b8848d2016-03-04 15:53:46 +0000829 enum rxrpc_call_event genbit;
David Howells17926a72007-04-26 15:48:28 -0700830 unsigned long bits;
David Howells224711d2007-05-04 12:41:11 -0700831 __be32 data, pad;
David Howells17926a72007-04-26 15:48:28 -0700832 size_t len;
David Howells5b8848d2016-03-04 15:53:46 +0000833 int loop, nbit, ioc, ret, mtu;
David Howells0d12f8a2016-03-04 15:53:46 +0000834 u32 serial, abort_code = RX_PROTOCOL_ERROR;
David Howells17926a72007-04-26 15:48:28 -0700835 u8 *acks = NULL;
836
837 //printk("\n--------------------\n");
838 _enter("{%d,%s,%lx} [%lu]",
839 call->debug_id, rxrpc_call_states[call->state], call->events,
840 (jiffies - call->creation_jif) / (HZ / 10));
841
David Howellsf9dc5752016-08-08 10:27:26 +0100842 if (!call->conn)
843 goto skip_msg_init;
844
David Howells17926a72007-04-26 15:48:28 -0700845 /* there's a good chance we're going to have to send a message, so set
846 * one up in advance */
David Howells85f32272016-04-04 14:00:36 +0100847 msg.msg_name = &call->conn->params.peer->srx.transport;
848 msg.msg_namelen = call->conn->params.peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700849 msg.msg_control = NULL;
850 msg.msg_controllen = 0;
851 msg.msg_flags = 0;
852
David Howells19ffa012016-04-04 14:00:36 +0100853 whdr.epoch = htonl(call->conn->proto.epoch);
David Howells0d12f8a2016-03-04 15:53:46 +0000854 whdr.cid = htonl(call->cid);
855 whdr.callNumber = htonl(call->call_id);
856 whdr.seq = 0;
857 whdr.type = RXRPC_PACKET_TYPE_ACK;
858 whdr.flags = call->conn->out_clientflag;
859 whdr.userStatus = 0;
860 whdr.securityIndex = call->conn->security_ix;
861 whdr._rsvd = 0;
862 whdr.serviceId = htons(call->service_id);
David Howells17926a72007-04-26 15:48:28 -0700863
864 memset(iov, 0, sizeof(iov));
David Howells0d12f8a2016-03-04 15:53:46 +0000865 iov[0].iov_base = &whdr;
866 iov[0].iov_len = sizeof(whdr);
David Howellsf9dc5752016-08-08 10:27:26 +0100867skip_msg_init:
David Howells17926a72007-04-26 15:48:28 -0700868
869 /* deal with events of a final nature */
David Howells4c198ad2016-03-04 15:53:46 +0000870 if (test_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events)) {
David Howellsf66d7492016-04-04 14:00:34 +0100871 enum rxrpc_skb_mark mark;
David Howells17926a72007-04-26 15:48:28 -0700872 int error;
873
David Howells4c198ad2016-03-04 15:53:46 +0000874 clear_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
875 clear_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events);
876 clear_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700877
David Howellsf5c17aa2016-08-30 09:49:28 +0100878 if (call->completion == RXRPC_CALL_NETWORK_ERROR) {
David Howellsf66d7492016-04-04 14:00:34 +0100879 mark = RXRPC_SKB_MARK_NET_ERROR;
880 _debug("post net error %d", error);
881 } else {
882 mark = RXRPC_SKB_MARK_LOCAL_ERROR;
David Howellsf66d7492016-04-04 14:00:34 +0100883 _debug("post net local error %d", error);
884 }
David Howells17926a72007-04-26 15:48:28 -0700885
David Howellsf5c17aa2016-08-30 09:49:28 +0100886 if (rxrpc_post_message(call, mark, call->error, true) < 0)
David Howells17926a72007-04-26 15:48:28 -0700887 goto no_mem;
David Howells4c198ad2016-03-04 15:53:46 +0000888 clear_bit(RXRPC_CALL_EV_RCVD_ERROR, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700889 goto kill_ACKs;
890 }
891
David Howells4c198ad2016-03-04 15:53:46 +0000892 if (test_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events)) {
David Howellsf5c17aa2016-08-30 09:49:28 +0100893 ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
David Howells17926a72007-04-26 15:48:28 -0700894
David Howells4c198ad2016-03-04 15:53:46 +0000895 clear_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events);
896 clear_bit(RXRPC_CALL_EV_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700897
898 _debug("post conn abort");
899
900 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
David Howellsf5c17aa2016-08-30 09:49:28 +0100901 call->error, true) < 0)
David Howells17926a72007-04-26 15:48:28 -0700902 goto no_mem;
David Howells4c198ad2016-03-04 15:53:46 +0000903 clear_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700904 goto kill_ACKs;
905 }
906
David Howells4c198ad2016-03-04 15:53:46 +0000907 if (test_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events)) {
David Howells0d12f8a2016-03-04 15:53:46 +0000908 whdr.type = RXRPC_PACKET_TYPE_BUSY;
David Howells4c198ad2016-03-04 15:53:46 +0000909 genbit = RXRPC_CALL_EV_REJECT_BUSY;
David Howells17926a72007-04-26 15:48:28 -0700910 goto send_message;
911 }
912
David Howells4c198ad2016-03-04 15:53:46 +0000913 if (test_bit(RXRPC_CALL_EV_ABORT, &call->events)) {
David Howellsf5c17aa2016-08-30 09:49:28 +0100914 ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
David Howells17926a72007-04-26 15:48:28 -0700915
916 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
David Howellsf5c17aa2016-08-30 09:49:28 +0100917 call->error, true) < 0)
David Howells17926a72007-04-26 15:48:28 -0700918 goto no_mem;
David Howells0d12f8a2016-03-04 15:53:46 +0000919 whdr.type = RXRPC_PACKET_TYPE_ABORT;
David Howellsf5c17aa2016-08-30 09:49:28 +0100920 data = htonl(call->abort_code);
David Howells17926a72007-04-26 15:48:28 -0700921 iov[1].iov_base = &data;
922 iov[1].iov_len = sizeof(data);
David Howells4c198ad2016-03-04 15:53:46 +0000923 genbit = RXRPC_CALL_EV_ABORT;
David Howells17926a72007-04-26 15:48:28 -0700924 goto send_message;
925 }
926
David Howells4c198ad2016-03-04 15:53:46 +0000927 if (test_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events)) {
928 genbit = RXRPC_CALL_EV_ACK_FINAL;
David Howells224711d2007-05-04 12:41:11 -0700929
930 ack.bufferSpace = htons(8);
931 ack.maxSkew = 0;
932 ack.serial = 0;
933 ack.reason = RXRPC_ACK_IDLE;
934 ack.nAcks = 0;
935 call->ackr_reason = 0;
936
937 spin_lock_bh(&call->lock);
David Howells0d12f8a2016-03-04 15:53:46 +0000938 ack.serial = htonl(call->ackr_serial);
939 ack.previousPacket = htonl(call->ackr_prev_seq);
940 ack.firstPacket = htonl(call->rx_data_eaten + 1);
David Howells224711d2007-05-04 12:41:11 -0700941 spin_unlock_bh(&call->lock);
942
943 pad = 0;
944
945 iov[1].iov_base = &ack;
946 iov[1].iov_len = sizeof(ack);
947 iov[2].iov_base = &pad;
948 iov[2].iov_len = 3;
949 iov[3].iov_base = &ackinfo;
950 iov[3].iov_len = sizeof(ackinfo);
951 goto send_ACK;
David Howells17926a72007-04-26 15:48:28 -0700952 }
953
David Howells4c198ad2016-03-04 15:53:46 +0000954 if (call->events & ((1 << RXRPC_CALL_EV_RCVD_BUSY) |
955 (1 << RXRPC_CALL_EV_RCVD_ABORT))
David Howells17926a72007-04-26 15:48:28 -0700956 ) {
957 u32 mark;
958
David Howells4c198ad2016-03-04 15:53:46 +0000959 if (test_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700960 mark = RXRPC_SKB_MARK_REMOTE_ABORT;
961 else
962 mark = RXRPC_SKB_MARK_BUSY;
963
964 _debug("post abort/busy");
965 rxrpc_clear_tx_window(call);
966 if (rxrpc_post_message(call, mark, ECONNABORTED, true) < 0)
967 goto no_mem;
968
David Howells4c198ad2016-03-04 15:53:46 +0000969 clear_bit(RXRPC_CALL_EV_RCVD_BUSY, &call->events);
970 clear_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700971 goto kill_ACKs;
972 }
973
David Howells4c198ad2016-03-04 15:53:46 +0000974 if (test_and_clear_bit(RXRPC_CALL_EV_RCVD_ACKALL, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700975 _debug("do implicit ackall");
976 rxrpc_clear_tx_window(call);
977 }
978
David Howells4c198ad2016-03-04 15:53:46 +0000979 if (test_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events)) {
David Howellsf5c17aa2016-08-30 09:49:28 +0100980 rxrpc_abort_call(call, RX_CALL_TIMEOUT, ETIME);
David Howells17926a72007-04-26 15:48:28 -0700981
982 _debug("post timeout");
983 if (rxrpc_post_message(call, RXRPC_SKB_MARK_LOCAL_ERROR,
984 ETIME, true) < 0)
985 goto no_mem;
986
David Howells4c198ad2016-03-04 15:53:46 +0000987 clear_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
David Howells17926a72007-04-26 15:48:28 -0700988 goto kill_ACKs;
989 }
990
991 /* deal with assorted inbound messages */
992 if (!skb_queue_empty(&call->rx_queue)) {
David Howellsf5c17aa2016-08-30 09:49:28 +0100993 ret = rxrpc_process_rx_queue(call, &abort_code);
994 switch (ret) {
David Howells17926a72007-04-26 15:48:28 -0700995 case 0:
996 case -EAGAIN:
997 break;
998 case -ENOMEM:
999 goto no_mem;
1000 case -EKEYEXPIRED:
1001 case -EKEYREJECTED:
1002 case -EPROTO:
David Howellsf5c17aa2016-08-30 09:49:28 +01001003 rxrpc_abort_call(call, abort_code, -ret);
David Howells17926a72007-04-26 15:48:28 -07001004 goto kill_ACKs;
1005 }
1006 }
1007
1008 /* handle resending */
David Howells4c198ad2016-03-04 15:53:46 +00001009 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
David Howells17926a72007-04-26 15:48:28 -07001010 rxrpc_resend_timer(call);
David Howells4c198ad2016-03-04 15:53:46 +00001011 if (test_and_clear_bit(RXRPC_CALL_EV_RESEND, &call->events))
David Howells17926a72007-04-26 15:48:28 -07001012 rxrpc_resend(call);
1013
1014 /* consider sending an ordinary ACK */
David Howells4c198ad2016-03-04 15:53:46 +00001015 if (test_bit(RXRPC_CALL_EV_ACK, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001016 _debug("send ACK: window: %d - %d { %lx }",
1017 call->rx_data_eaten, call->ackr_win_top,
1018 call->ackr_window[0]);
1019
1020 if (call->state > RXRPC_CALL_SERVER_ACK_REQUEST &&
1021 call->ackr_reason != RXRPC_ACK_PING_RESPONSE) {
1022 /* ACK by sending reply DATA packet in this state */
David Howells4c198ad2016-03-04 15:53:46 +00001023 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001024 goto maybe_reschedule;
1025 }
1026
David Howells4c198ad2016-03-04 15:53:46 +00001027 genbit = RXRPC_CALL_EV_ACK;
David Howells17926a72007-04-26 15:48:28 -07001028
1029 acks = kzalloc(call->ackr_win_top - call->rx_data_eaten,
1030 GFP_NOFS);
1031 if (!acks)
1032 goto no_mem;
1033
1034 //hdr.flags = RXRPC_SLOW_START_OK;
1035 ack.bufferSpace = htons(8);
1036 ack.maxSkew = 0;
David Howells17926a72007-04-26 15:48:28 -07001037
David Howells17926a72007-04-26 15:48:28 -07001038 spin_lock_bh(&call->lock);
David Howells0d12f8a2016-03-04 15:53:46 +00001039 ack.reason = call->ackr_reason;
1040 ack.serial = htonl(call->ackr_serial);
1041 ack.previousPacket = htonl(call->ackr_prev_seq);
David Howells17926a72007-04-26 15:48:28 -07001042 ack.firstPacket = htonl(call->rx_data_eaten + 1);
1043
1044 ack.nAcks = 0;
1045 for (loop = 0; loop < RXRPC_ACKR_WINDOW_ASZ; loop++) {
1046 nbit = loop * BITS_PER_LONG;
1047 for (bits = call->ackr_window[loop]; bits; bits >>= 1
1048 ) {
1049 _debug("- l=%d n=%d b=%lx", loop, nbit, bits);
1050 if (bits & 1) {
1051 acks[nbit] = RXRPC_ACK_TYPE_ACK;
1052 ack.nAcks = nbit + 1;
1053 }
1054 nbit++;
1055 }
1056 }
1057 call->ackr_reason = 0;
1058 spin_unlock_bh(&call->lock);
1059
1060 pad = 0;
1061
1062 iov[1].iov_base = &ack;
1063 iov[1].iov_len = sizeof(ack);
1064 iov[2].iov_base = acks;
1065 iov[2].iov_len = ack.nAcks;
1066 iov[3].iov_base = &pad;
1067 iov[3].iov_len = 3;
1068 iov[4].iov_base = &ackinfo;
1069 iov[4].iov_len = sizeof(ackinfo);
1070
1071 switch (ack.reason) {
1072 case RXRPC_ACK_REQUESTED:
1073 case RXRPC_ACK_DUPLICATE:
1074 case RXRPC_ACK_OUT_OF_SEQUENCE:
1075 case RXRPC_ACK_EXCEEDS_WINDOW:
1076 case RXRPC_ACK_NOSPACE:
1077 case RXRPC_ACK_PING:
1078 case RXRPC_ACK_PING_RESPONSE:
1079 goto send_ACK_with_skew;
1080 case RXRPC_ACK_DELAY:
1081 case RXRPC_ACK_IDLE:
1082 goto send_ACK;
1083 }
1084 }
1085
1086 /* handle completion of security negotiations on an incoming
1087 * connection */
David Howells4c198ad2016-03-04 15:53:46 +00001088 if (test_and_clear_bit(RXRPC_CALL_EV_SECURED, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001089 _debug("secured");
1090 spin_lock_bh(&call->lock);
1091
1092 if (call->state == RXRPC_CALL_SERVER_SECURING) {
1093 _debug("securing");
David Howells30b515f2016-06-28 16:58:36 +01001094 write_lock(&call->socket->call_lock);
David Howells17926a72007-04-26 15:48:28 -07001095 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
David Howells4c198ad2016-03-04 15:53:46 +00001096 !test_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001097 _debug("not released");
1098 call->state = RXRPC_CALL_SERVER_ACCEPTING;
1099 list_move_tail(&call->accept_link,
1100 &call->socket->acceptq);
1101 }
David Howells30b515f2016-06-28 16:58:36 +01001102 write_unlock(&call->socket->call_lock);
David Howells17926a72007-04-26 15:48:28 -07001103 read_lock(&call->state_lock);
1104 if (call->state < RXRPC_CALL_COMPLETE)
David Howells4c198ad2016-03-04 15:53:46 +00001105 set_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001106 read_unlock(&call->state_lock);
1107 }
1108
1109 spin_unlock_bh(&call->lock);
David Howells4c198ad2016-03-04 15:53:46 +00001110 if (!test_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events))
David Howells17926a72007-04-26 15:48:28 -07001111 goto maybe_reschedule;
1112 }
1113
1114 /* post a notification of an acceptable connection to the app */
David Howells4c198ad2016-03-04 15:53:46 +00001115 if (test_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001116 _debug("post accept");
1117 if (rxrpc_post_message(call, RXRPC_SKB_MARK_NEW_CALL,
1118 0, false) < 0)
1119 goto no_mem;
David Howells4c198ad2016-03-04 15:53:46 +00001120 clear_bit(RXRPC_CALL_EV_POST_ACCEPT, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001121 goto maybe_reschedule;
1122 }
1123
1124 /* handle incoming call acceptance */
David Howells4c198ad2016-03-04 15:53:46 +00001125 if (test_and_clear_bit(RXRPC_CALL_EV_ACCEPTED, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001126 _debug("accepted");
1127 ASSERTCMP(call->rx_data_post, ==, 0);
1128 call->rx_data_post = 1;
1129 read_lock_bh(&call->state_lock);
1130 if (call->state < RXRPC_CALL_COMPLETE)
David Howells4c198ad2016-03-04 15:53:46 +00001131 set_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001132 read_unlock_bh(&call->state_lock);
1133 }
1134
1135 /* drain the out of sequence received packet queue into the packet Rx
1136 * queue */
David Howells4c198ad2016-03-04 15:53:46 +00001137 if (test_and_clear_bit(RXRPC_CALL_EV_DRAIN_RX_OOS, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -07001138 while (call->rx_data_post == call->rx_first_oos)
1139 if (rxrpc_drain_rx_oos_queue(call) < 0)
1140 break;
1141 goto maybe_reschedule;
1142 }
1143
David Howellse653cfe2016-04-04 14:00:38 +01001144 if (test_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
1145 rxrpc_release_call(call);
1146 clear_bit(RXRPC_CALL_EV_RELEASE, &call->events);
1147 }
1148
David Howells17926a72007-04-26 15:48:28 -07001149 /* other events may have been raised since we started checking */
1150 goto maybe_reschedule;
1151
1152send_ACK_with_skew:
David Howells563ea7d2016-08-23 15:27:25 +01001153 ack.maxSkew = htons(call->ackr_skew);
David Howells17926a72007-04-26 15:48:28 -07001154send_ACK:
David Howells85f32272016-04-04 14:00:36 +01001155 mtu = call->conn->params.peer->if_mtu;
1156 mtu -= call->conn->params.peer->hdrsize;
David Howells224711d2007-05-04 12:41:11 -07001157 ackinfo.maxMTU = htonl(mtu);
David Howells817913d2014-02-07 18:10:30 +00001158 ackinfo.rwind = htonl(rxrpc_rx_window_size);
David Howells224711d2007-05-04 12:41:11 -07001159
1160 /* permit the peer to send us jumbo packets if it wants to */
David Howells817913d2014-02-07 18:10:30 +00001161 ackinfo.rxMTU = htonl(rxrpc_rx_mtu);
1162 ackinfo.jumbo_max = htonl(rxrpc_rx_jumbo_max);
David Howells224711d2007-05-04 12:41:11 -07001163
David Howells0d12f8a2016-03-04 15:53:46 +00001164 serial = atomic_inc_return(&call->conn->serial);
1165 whdr.serial = htonl(serial);
David Howells17926a72007-04-26 15:48:28 -07001166 _proto("Tx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
David Howells0d12f8a2016-03-04 15:53:46 +00001167 serial,
David Howells17926a72007-04-26 15:48:28 -07001168 ntohs(ack.maxSkew),
1169 ntohl(ack.firstPacket),
1170 ntohl(ack.previousPacket),
1171 ntohl(ack.serial),
Dan Carpenter08d4d212014-01-20 13:28:59 +03001172 rxrpc_acks(ack.reason),
David Howells17926a72007-04-26 15:48:28 -07001173 ack.nAcks);
1174
1175 del_timer_sync(&call->ack_timer);
1176 if (ack.nAcks > 0)
1177 set_bit(RXRPC_CALL_TX_SOFT_ACK, &call->flags);
1178 goto send_message_2;
1179
1180send_message:
1181 _debug("send message");
1182
David Howells0d12f8a2016-03-04 15:53:46 +00001183 serial = atomic_inc_return(&call->conn->serial);
1184 whdr.serial = htonl(serial);
1185 _proto("Tx %s %%%u", rxrpc_pkts[whdr.type], serial);
David Howells17926a72007-04-26 15:48:28 -07001186send_message_2:
1187
1188 len = iov[0].iov_len;
1189 ioc = 1;
1190 if (iov[4].iov_len) {
1191 ioc = 5;
1192 len += iov[4].iov_len;
1193 len += iov[3].iov_len;
1194 len += iov[2].iov_len;
1195 len += iov[1].iov_len;
1196 } else if (iov[3].iov_len) {
1197 ioc = 4;
1198 len += iov[3].iov_len;
1199 len += iov[2].iov_len;
1200 len += iov[1].iov_len;
1201 } else if (iov[2].iov_len) {
1202 ioc = 3;
1203 len += iov[2].iov_len;
1204 len += iov[1].iov_len;
1205 } else if (iov[1].iov_len) {
1206 ioc = 2;
1207 len += iov[1].iov_len;
1208 }
1209
David Howells85f32272016-04-04 14:00:36 +01001210 ret = kernel_sendmsg(call->conn->params.local->socket,
David Howells17926a72007-04-26 15:48:28 -07001211 &msg, iov, ioc, len);
1212 if (ret < 0) {
1213 _debug("sendmsg failed: %d", ret);
1214 read_lock_bh(&call->state_lock);
1215 if (call->state < RXRPC_CALL_DEAD)
David Howells651350d2007-04-26 15:50:17 -07001216 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001217 read_unlock_bh(&call->state_lock);
1218 goto error;
1219 }
1220
1221 switch (genbit) {
David Howells4c198ad2016-03-04 15:53:46 +00001222 case RXRPC_CALL_EV_ABORT:
David Howells17926a72007-04-26 15:48:28 -07001223 clear_bit(genbit, &call->events);
David Howells4c198ad2016-03-04 15:53:46 +00001224 clear_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001225 goto kill_ACKs;
1226
David Howells4c198ad2016-03-04 15:53:46 +00001227 case RXRPC_CALL_EV_ACK_FINAL:
David Howellsf5c17aa2016-08-30 09:49:28 +01001228 rxrpc_call_completed(call);
David Howells17926a72007-04-26 15:48:28 -07001229 goto kill_ACKs;
1230
1231 default:
1232 clear_bit(genbit, &call->events);
1233 switch (call->state) {
1234 case RXRPC_CALL_CLIENT_AWAIT_REPLY:
1235 case RXRPC_CALL_CLIENT_RECV_REPLY:
1236 case RXRPC_CALL_SERVER_RECV_REQUEST:
1237 case RXRPC_CALL_SERVER_ACK_REQUEST:
1238 _debug("start ACK timer");
1239 rxrpc_propose_ACK(call, RXRPC_ACK_DELAY,
David Howells563ea7d2016-08-23 15:27:25 +01001240 call->ackr_skew, call->ackr_serial,
1241 false);
David Howells17926a72007-04-26 15:48:28 -07001242 default:
1243 break;
1244 }
1245 goto maybe_reschedule;
1246 }
1247
1248kill_ACKs:
1249 del_timer_sync(&call->ack_timer);
David Howells4c198ad2016-03-04 15:53:46 +00001250 if (test_and_clear_bit(RXRPC_CALL_EV_ACK_FINAL, &call->events))
David Howells17926a72007-04-26 15:48:28 -07001251 rxrpc_put_call(call);
David Howells4c198ad2016-03-04 15:53:46 +00001252 clear_bit(RXRPC_CALL_EV_ACK, &call->events);
David Howells17926a72007-04-26 15:48:28 -07001253
1254maybe_reschedule:
1255 if (call->events || !skb_queue_empty(&call->rx_queue)) {
1256 read_lock_bh(&call->state_lock);
1257 if (call->state < RXRPC_CALL_DEAD)
David Howells651350d2007-04-26 15:50:17 -07001258 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001259 read_unlock_bh(&call->state_lock);
1260 }
1261
1262 /* don't leave aborted connections on the accept queue */
1263 if (call->state >= RXRPC_CALL_COMPLETE &&
1264 !list_empty(&call->accept_link)) {
1265 _debug("X unlinking once-pending call %p { e=%lx f=%lx c=%x }",
David Howells19ffa012016-04-04 14:00:36 +01001266 call, call->events, call->flags, call->conn->proto.cid);
David Howells17926a72007-04-26 15:48:28 -07001267
1268 read_lock_bh(&call->state_lock);
1269 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
David Howells4c198ad2016-03-04 15:53:46 +00001270 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
David Howells651350d2007-04-26 15:50:17 -07001271 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001272 read_unlock_bh(&call->state_lock);
1273 }
1274
1275error:
David Howells17926a72007-04-26 15:48:28 -07001276 kfree(acks);
1277
1278 /* because we don't want two CPUs both processing the work item for one
1279 * call at the same time, we use a flag to note when it's busy; however
1280 * this means there's a race between clearing the flag and setting the
1281 * work pending bit and the work item being processed again */
1282 if (call->events && !work_pending(&call->processor)) {
David Howells19ffa012016-04-04 14:00:36 +01001283 _debug("jumpstart %x", call->conn->proto.cid);
David Howells651350d2007-04-26 15:50:17 -07001284 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -07001285 }
1286
1287 _leave("");
1288 return;
1289
1290no_mem:
1291 _debug("out of memory");
1292 goto maybe_reschedule;
1293}