ar-ack.c 34 KB

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