call.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  1. /* call.c: Rx call routines
  2. *
  3. * Copyright (C) 2002 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/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <rxrpc/rxrpc.h>
  15. #include <rxrpc/transport.h>
  16. #include <rxrpc/peer.h>
  17. #include <rxrpc/connection.h>
  18. #include <rxrpc/call.h>
  19. #include <rxrpc/message.h>
  20. #include "internal.h"
  21. __RXACCT_DECL(atomic_t rxrpc_call_count);
  22. __RXACCT_DECL(atomic_t rxrpc_message_count);
  23. LIST_HEAD(rxrpc_calls);
  24. DECLARE_RWSEM(rxrpc_calls_sem);
  25. unsigned rxrpc_call_rcv_timeout = HZ/3;
  26. static unsigned rxrpc_call_acks_timeout = HZ/3;
  27. static unsigned rxrpc_call_dfr_ack_timeout = HZ/20;
  28. static unsigned short rxrpc_call_max_resend = HZ/10;
  29. const char *rxrpc_call_states[] = {
  30. "COMPLETE",
  31. "ERROR",
  32. "SRVR_RCV_OPID",
  33. "SRVR_RCV_ARGS",
  34. "SRVR_GOT_ARGS",
  35. "SRVR_SND_REPLY",
  36. "SRVR_RCV_FINAL_ACK",
  37. "CLNT_SND_ARGS",
  38. "CLNT_RCV_REPLY",
  39. "CLNT_GOT_REPLY"
  40. };
  41. const char *rxrpc_call_error_states[] = {
  42. "NO_ERROR",
  43. "LOCAL_ABORT",
  44. "PEER_ABORT",
  45. "LOCAL_ERROR",
  46. "REMOTE_ERROR"
  47. };
  48. const char *rxrpc_pkts[] = {
  49. "?00",
  50. "data", "ack", "busy", "abort", "ackall", "chall", "resp", "debug",
  51. "?09", "?10", "?11", "?12", "?13", "?14", "?15"
  52. };
  53. static const char *rxrpc_acks[] = {
  54. "---", "REQ", "DUP", "SEQ", "WIN", "MEM", "PNG", "PNR", "DLY", "IDL",
  55. "-?-"
  56. };
  57. static const char _acktype[] = "NA-";
  58. static void rxrpc_call_receive_packet(struct rxrpc_call *call);
  59. static void rxrpc_call_receive_data_packet(struct rxrpc_call *call,
  60. struct rxrpc_message *msg);
  61. static void rxrpc_call_receive_ack_packet(struct rxrpc_call *call,
  62. struct rxrpc_message *msg);
  63. static void rxrpc_call_definitively_ACK(struct rxrpc_call *call,
  64. rxrpc_seq_t higest);
  65. static void rxrpc_call_resend(struct rxrpc_call *call, rxrpc_seq_t highest);
  66. static int __rxrpc_call_read_data(struct rxrpc_call *call);
  67. static int rxrpc_call_record_ACK(struct rxrpc_call *call,
  68. struct rxrpc_message *msg,
  69. rxrpc_seq_t seq,
  70. size_t count);
  71. static int rxrpc_call_flush(struct rxrpc_call *call);
  72. #define _state(call) \
  73. _debug("[[[ state %s ]]]", rxrpc_call_states[call->app_call_state]);
  74. static void rxrpc_call_default_attn_func(struct rxrpc_call *call)
  75. {
  76. wake_up(&call->waitq);
  77. }
  78. static void rxrpc_call_default_error_func(struct rxrpc_call *call)
  79. {
  80. wake_up(&call->waitq);
  81. }
  82. static void rxrpc_call_default_aemap_func(struct rxrpc_call *call)
  83. {
  84. switch (call->app_err_state) {
  85. case RXRPC_ESTATE_LOCAL_ABORT:
  86. call->app_abort_code = -call->app_errno;
  87. case RXRPC_ESTATE_PEER_ABORT:
  88. call->app_errno = -ECONNABORTED;
  89. default:
  90. break;
  91. }
  92. }
  93. static void __rxrpc_call_acks_timeout(unsigned long _call)
  94. {
  95. struct rxrpc_call *call = (struct rxrpc_call *) _call;
  96. _debug("ACKS TIMEOUT %05lu", jiffies - call->cjif);
  97. call->flags |= RXRPC_CALL_ACKS_TIMO;
  98. rxrpc_krxiod_queue_call(call);
  99. }
  100. static void __rxrpc_call_rcv_timeout(unsigned long _call)
  101. {
  102. struct rxrpc_call *call = (struct rxrpc_call *) _call;
  103. _debug("RCV TIMEOUT %05lu", jiffies - call->cjif);
  104. call->flags |= RXRPC_CALL_RCV_TIMO;
  105. rxrpc_krxiod_queue_call(call);
  106. }
  107. static void __rxrpc_call_ackr_timeout(unsigned long _call)
  108. {
  109. struct rxrpc_call *call = (struct rxrpc_call *) _call;
  110. _debug("ACKR TIMEOUT %05lu",jiffies - call->cjif);
  111. call->flags |= RXRPC_CALL_ACKR_TIMO;
  112. rxrpc_krxiod_queue_call(call);
  113. }
  114. /*****************************************************************************/
  115. /*
  116. * calculate a timeout based on an RTT value
  117. */
  118. static inline unsigned long __rxrpc_rtt_based_timeout(struct rxrpc_call *call,
  119. unsigned long val)
  120. {
  121. unsigned long expiry = call->conn->peer->rtt / (1000000 / HZ);
  122. expiry += 10;
  123. if (expiry < HZ / 25)
  124. expiry = HZ / 25;
  125. if (expiry > HZ)
  126. expiry = HZ;
  127. _leave(" = %lu jiffies", expiry);
  128. return jiffies + expiry;
  129. } /* end __rxrpc_rtt_based_timeout() */
  130. /*****************************************************************************/
  131. /*
  132. * create a new call record
  133. */
  134. static inline int __rxrpc_create_call(struct rxrpc_connection *conn,
  135. struct rxrpc_call **_call)
  136. {
  137. struct rxrpc_call *call;
  138. _enter("%p", conn);
  139. /* allocate and initialise a call record */
  140. call = (struct rxrpc_call *) get_zeroed_page(GFP_KERNEL);
  141. if (!call) {
  142. _leave(" ENOMEM");
  143. return -ENOMEM;
  144. }
  145. atomic_set(&call->usage, 1);
  146. init_waitqueue_head(&call->waitq);
  147. spin_lock_init(&call->lock);
  148. INIT_LIST_HEAD(&call->link);
  149. INIT_LIST_HEAD(&call->acks_pendq);
  150. INIT_LIST_HEAD(&call->rcv_receiveq);
  151. INIT_LIST_HEAD(&call->rcv_krxiodq_lk);
  152. INIT_LIST_HEAD(&call->app_readyq);
  153. INIT_LIST_HEAD(&call->app_unreadyq);
  154. INIT_LIST_HEAD(&call->app_link);
  155. INIT_LIST_HEAD(&call->app_attn_link);
  156. init_timer(&call->acks_timeout);
  157. call->acks_timeout.data = (unsigned long) call;
  158. call->acks_timeout.function = __rxrpc_call_acks_timeout;
  159. init_timer(&call->rcv_timeout);
  160. call->rcv_timeout.data = (unsigned long) call;
  161. call->rcv_timeout.function = __rxrpc_call_rcv_timeout;
  162. init_timer(&call->ackr_dfr_timo);
  163. call->ackr_dfr_timo.data = (unsigned long) call;
  164. call->ackr_dfr_timo.function = __rxrpc_call_ackr_timeout;
  165. call->conn = conn;
  166. call->ackr_win_bot = 1;
  167. call->ackr_win_top = call->ackr_win_bot + RXRPC_CALL_ACK_WINDOW_SIZE - 1;
  168. call->ackr_prev_seq = 0;
  169. call->app_mark = RXRPC_APP_MARK_EOF;
  170. call->app_attn_func = rxrpc_call_default_attn_func;
  171. call->app_error_func = rxrpc_call_default_error_func;
  172. call->app_aemap_func = rxrpc_call_default_aemap_func;
  173. call->app_scr_alloc = call->app_scratch;
  174. call->cjif = jiffies;
  175. _leave(" = 0 (%p)", call);
  176. *_call = call;
  177. return 0;
  178. } /* end __rxrpc_create_call() */
  179. /*****************************************************************************/
  180. /*
  181. * create a new call record for outgoing calls
  182. */
  183. int rxrpc_create_call(struct rxrpc_connection *conn,
  184. rxrpc_call_attn_func_t attn,
  185. rxrpc_call_error_func_t error,
  186. rxrpc_call_aemap_func_t aemap,
  187. struct rxrpc_call **_call)
  188. {
  189. DECLARE_WAITQUEUE(myself, current);
  190. struct rxrpc_call *call;
  191. int ret, cix, loop;
  192. _enter("%p", conn);
  193. /* allocate and initialise a call record */
  194. ret = __rxrpc_create_call(conn, &call);
  195. if (ret < 0) {
  196. _leave(" = %d", ret);
  197. return ret;
  198. }
  199. call->app_call_state = RXRPC_CSTATE_CLNT_SND_ARGS;
  200. if (attn)
  201. call->app_attn_func = attn;
  202. if (error)
  203. call->app_error_func = error;
  204. if (aemap)
  205. call->app_aemap_func = aemap;
  206. _state(call);
  207. spin_lock(&conn->lock);
  208. set_current_state(TASK_INTERRUPTIBLE);
  209. add_wait_queue(&conn->chanwait, &myself);
  210. try_again:
  211. /* try to find an unused channel */
  212. for (cix = 0; cix < 4; cix++)
  213. if (!conn->channels[cix])
  214. goto obtained_chan;
  215. /* no free channels - wait for one to become available */
  216. ret = -EINTR;
  217. if (signal_pending(current))
  218. goto error_unwait;
  219. spin_unlock(&conn->lock);
  220. schedule();
  221. set_current_state(TASK_INTERRUPTIBLE);
  222. spin_lock(&conn->lock);
  223. goto try_again;
  224. /* got a channel - now attach to the connection */
  225. obtained_chan:
  226. remove_wait_queue(&conn->chanwait, &myself);
  227. set_current_state(TASK_RUNNING);
  228. /* concoct a unique call number */
  229. next_callid:
  230. call->call_id = htonl(++conn->call_counter);
  231. for (loop = 0; loop < 4; loop++)
  232. if (conn->channels[loop] &&
  233. conn->channels[loop]->call_id == call->call_id)
  234. goto next_callid;
  235. rxrpc_get_connection(conn);
  236. conn->channels[cix] = call; /* assign _after_ done callid check loop */
  237. do_gettimeofday(&conn->atime);
  238. call->chan_ix = htonl(cix);
  239. spin_unlock(&conn->lock);
  240. down_write(&rxrpc_calls_sem);
  241. list_add_tail(&call->call_link, &rxrpc_calls);
  242. up_write(&rxrpc_calls_sem);
  243. __RXACCT(atomic_inc(&rxrpc_call_count));
  244. *_call = call;
  245. _leave(" = 0 (call=%p cix=%u)", call, cix);
  246. return 0;
  247. error_unwait:
  248. remove_wait_queue(&conn->chanwait, &myself);
  249. set_current_state(TASK_RUNNING);
  250. spin_unlock(&conn->lock);
  251. free_page((unsigned long) call);
  252. _leave(" = %d", ret);
  253. return ret;
  254. } /* end rxrpc_create_call() */
  255. /*****************************************************************************/
  256. /*
  257. * create a new call record for incoming calls
  258. */
  259. int rxrpc_incoming_call(struct rxrpc_connection *conn,
  260. struct rxrpc_message *msg,
  261. struct rxrpc_call **_call)
  262. {
  263. struct rxrpc_call *call;
  264. unsigned cix;
  265. int ret;
  266. cix = ntohl(msg->hdr.cid) & RXRPC_CHANNELMASK;
  267. _enter("%p,%u,%u", conn, ntohl(msg->hdr.callNumber), cix);
  268. /* allocate and initialise a call record */
  269. ret = __rxrpc_create_call(conn, &call);
  270. if (ret < 0) {
  271. _leave(" = %d", ret);
  272. return ret;
  273. }
  274. call->pkt_rcv_count = 1;
  275. call->app_call_state = RXRPC_CSTATE_SRVR_RCV_OPID;
  276. call->app_mark = sizeof(uint32_t);
  277. _state(call);
  278. /* attach to the connection */
  279. ret = -EBUSY;
  280. call->chan_ix = htonl(cix);
  281. call->call_id = msg->hdr.callNumber;
  282. spin_lock(&conn->lock);
  283. if (!conn->channels[cix] ||
  284. conn->channels[cix]->app_call_state == RXRPC_CSTATE_COMPLETE ||
  285. conn->channels[cix]->app_call_state == RXRPC_CSTATE_ERROR
  286. ) {
  287. conn->channels[cix] = call;
  288. rxrpc_get_connection(conn);
  289. ret = 0;
  290. }
  291. spin_unlock(&conn->lock);
  292. if (ret < 0) {
  293. free_page((unsigned long) call);
  294. call = NULL;
  295. }
  296. if (ret == 0) {
  297. down_write(&rxrpc_calls_sem);
  298. list_add_tail(&call->call_link, &rxrpc_calls);
  299. up_write(&rxrpc_calls_sem);
  300. __RXACCT(atomic_inc(&rxrpc_call_count));
  301. *_call = call;
  302. }
  303. _leave(" = %d [%p]", ret, call);
  304. return ret;
  305. } /* end rxrpc_incoming_call() */
  306. /*****************************************************************************/
  307. /*
  308. * free a call record
  309. */
  310. void rxrpc_put_call(struct rxrpc_call *call)
  311. {
  312. struct rxrpc_connection *conn = call->conn;
  313. struct rxrpc_message *msg;
  314. _enter("%p{u=%d}",call,atomic_read(&call->usage));
  315. /* sanity check */
  316. if (atomic_read(&call->usage) <= 0)
  317. BUG();
  318. /* to prevent a race, the decrement and the de-list must be effectively
  319. * atomic */
  320. spin_lock(&conn->lock);
  321. if (likely(!atomic_dec_and_test(&call->usage))) {
  322. spin_unlock(&conn->lock);
  323. _leave("");
  324. return;
  325. }
  326. if (conn->channels[ntohl(call->chan_ix)] == call)
  327. conn->channels[ntohl(call->chan_ix)] = NULL;
  328. spin_unlock(&conn->lock);
  329. wake_up(&conn->chanwait);
  330. rxrpc_put_connection(conn);
  331. /* clear the timers and dequeue from krxiod */
  332. del_timer_sync(&call->acks_timeout);
  333. del_timer_sync(&call->rcv_timeout);
  334. del_timer_sync(&call->ackr_dfr_timo);
  335. rxrpc_krxiod_dequeue_call(call);
  336. /* clean up the contents of the struct */
  337. if (call->snd_nextmsg)
  338. rxrpc_put_message(call->snd_nextmsg);
  339. if (call->snd_ping)
  340. rxrpc_put_message(call->snd_ping);
  341. while (!list_empty(&call->acks_pendq)) {
  342. msg = list_entry(call->acks_pendq.next,
  343. struct rxrpc_message, link);
  344. list_del(&msg->link);
  345. rxrpc_put_message(msg);
  346. }
  347. while (!list_empty(&call->rcv_receiveq)) {
  348. msg = list_entry(call->rcv_receiveq.next,
  349. struct rxrpc_message, link);
  350. list_del(&msg->link);
  351. rxrpc_put_message(msg);
  352. }
  353. while (!list_empty(&call->app_readyq)) {
  354. msg = list_entry(call->app_readyq.next,
  355. struct rxrpc_message, link);
  356. list_del(&msg->link);
  357. rxrpc_put_message(msg);
  358. }
  359. while (!list_empty(&call->app_unreadyq)) {
  360. msg = list_entry(call->app_unreadyq.next,
  361. struct rxrpc_message, link);
  362. list_del(&msg->link);
  363. rxrpc_put_message(msg);
  364. }
  365. module_put(call->owner);
  366. down_write(&rxrpc_calls_sem);
  367. list_del(&call->call_link);
  368. up_write(&rxrpc_calls_sem);
  369. __RXACCT(atomic_dec(&rxrpc_call_count));
  370. free_page((unsigned long) call);
  371. _leave(" [destroyed]");
  372. } /* end rxrpc_put_call() */
  373. /*****************************************************************************/
  374. /*
  375. * actually generate a normal ACK
  376. */
  377. static inline int __rxrpc_call_gen_normal_ACK(struct rxrpc_call *call,
  378. rxrpc_seq_t seq)
  379. {
  380. struct rxrpc_message *msg;
  381. struct kvec diov[3];
  382. __be32 aux[4];
  383. int delta, ret;
  384. /* ACKs default to DELAY */
  385. if (!call->ackr.reason)
  386. call->ackr.reason = RXRPC_ACK_DELAY;
  387. _proto("Rx %05lu Sending ACK { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
  388. jiffies - call->cjif,
  389. ntohs(call->ackr.maxSkew),
  390. ntohl(call->ackr.firstPacket),
  391. ntohl(call->ackr.previousPacket),
  392. ntohl(call->ackr.serial),
  393. rxrpc_acks[call->ackr.reason],
  394. call->ackr.nAcks);
  395. aux[0] = htonl(call->conn->peer->if_mtu); /* interface MTU */
  396. aux[1] = htonl(1444); /* max MTU */
  397. aux[2] = htonl(16); /* rwind */
  398. aux[3] = htonl(4); /* max packets */
  399. diov[0].iov_len = sizeof(struct rxrpc_ackpacket);
  400. diov[0].iov_base = &call->ackr;
  401. diov[1].iov_len = call->ackr_pend_cnt + 3;
  402. diov[1].iov_base = call->ackr_array;
  403. diov[2].iov_len = sizeof(aux);
  404. diov[2].iov_base = &aux;
  405. /* build and send the message */
  406. ret = rxrpc_conn_newmsg(call->conn,call, RXRPC_PACKET_TYPE_ACK,
  407. 3, diov, GFP_KERNEL, &msg);
  408. if (ret < 0)
  409. goto out;
  410. msg->seq = seq;
  411. msg->hdr.seq = htonl(seq);
  412. msg->hdr.flags |= RXRPC_SLOW_START_OK;
  413. ret = rxrpc_conn_sendmsg(call->conn, msg);
  414. rxrpc_put_message(msg);
  415. if (ret < 0)
  416. goto out;
  417. call->pkt_snd_count++;
  418. /* count how many actual ACKs there were at the front */
  419. for (delta = 0; delta < call->ackr_pend_cnt; delta++)
  420. if (call->ackr_array[delta] != RXRPC_ACK_TYPE_ACK)
  421. break;
  422. call->ackr_pend_cnt -= delta; /* all ACK'd to this point */
  423. /* crank the ACK window around */
  424. if (delta == 0) {
  425. /* un-ACK'd window */
  426. }
  427. else if (delta < RXRPC_CALL_ACK_WINDOW_SIZE) {
  428. /* partially ACK'd window
  429. * - shuffle down to avoid losing out-of-sequence packets
  430. */
  431. call->ackr_win_bot += delta;
  432. call->ackr_win_top += delta;
  433. memmove(&call->ackr_array[0],
  434. &call->ackr_array[delta],
  435. call->ackr_pend_cnt);
  436. memset(&call->ackr_array[call->ackr_pend_cnt],
  437. RXRPC_ACK_TYPE_NACK,
  438. sizeof(call->ackr_array) - call->ackr_pend_cnt);
  439. }
  440. else {
  441. /* fully ACK'd window
  442. * - just clear the whole thing
  443. */
  444. memset(&call->ackr_array,
  445. RXRPC_ACK_TYPE_NACK,
  446. sizeof(call->ackr_array));
  447. }
  448. /* clear this ACK */
  449. memset(&call->ackr, 0, sizeof(call->ackr));
  450. out:
  451. if (!call->app_call_state)
  452. printk("___ STATE 0 ___\n");
  453. return ret;
  454. } /* end __rxrpc_call_gen_normal_ACK() */
  455. /*****************************************************************************/
  456. /*
  457. * note the reception of a packet in the call's ACK records and generate an
  458. * appropriate ACK packet if necessary
  459. * - returns 0 if packet should be processed, 1 if packet should be ignored
  460. * and -ve on an error
  461. */
  462. static int rxrpc_call_generate_ACK(struct rxrpc_call *call,
  463. struct rxrpc_header *hdr,
  464. struct rxrpc_ackpacket *ack)
  465. {
  466. struct rxrpc_message *msg;
  467. rxrpc_seq_t seq;
  468. unsigned offset;
  469. int ret = 0, err;
  470. u8 special_ACK, do_ACK, force;
  471. _enter("%p,%p { seq=%d tp=%d fl=%02x }",
  472. call, hdr, ntohl(hdr->seq), hdr->type, hdr->flags);
  473. seq = ntohl(hdr->seq);
  474. offset = seq - call->ackr_win_bot;
  475. do_ACK = RXRPC_ACK_DELAY;
  476. special_ACK = 0;
  477. force = (seq == 1);
  478. if (call->ackr_high_seq < seq)
  479. call->ackr_high_seq = seq;
  480. /* deal with generation of obvious special ACKs first */
  481. if (ack && ack->reason == RXRPC_ACK_PING) {
  482. special_ACK = RXRPC_ACK_PING_RESPONSE;
  483. ret = 1;
  484. goto gen_ACK;
  485. }
  486. if (seq < call->ackr_win_bot) {
  487. special_ACK = RXRPC_ACK_DUPLICATE;
  488. ret = 1;
  489. goto gen_ACK;
  490. }
  491. if (seq >= call->ackr_win_top) {
  492. special_ACK = RXRPC_ACK_EXCEEDS_WINDOW;
  493. ret = 1;
  494. goto gen_ACK;
  495. }
  496. if (call->ackr_array[offset] != RXRPC_ACK_TYPE_NACK) {
  497. special_ACK = RXRPC_ACK_DUPLICATE;
  498. ret = 1;
  499. goto gen_ACK;
  500. }
  501. /* okay... it's a normal data packet inside the ACK window */
  502. call->ackr_array[offset] = RXRPC_ACK_TYPE_ACK;
  503. if (offset < call->ackr_pend_cnt) {
  504. }
  505. else if (offset > call->ackr_pend_cnt) {
  506. do_ACK = RXRPC_ACK_OUT_OF_SEQUENCE;
  507. call->ackr_pend_cnt = offset;
  508. goto gen_ACK;
  509. }
  510. if (hdr->flags & RXRPC_REQUEST_ACK) {
  511. do_ACK = RXRPC_ACK_REQUESTED;
  512. }
  513. /* generate an ACK on the final packet of a reply just received */
  514. if (hdr->flags & RXRPC_LAST_PACKET) {
  515. if (call->conn->out_clientflag)
  516. force = 1;
  517. }
  518. else if (!(hdr->flags & RXRPC_MORE_PACKETS)) {
  519. do_ACK = RXRPC_ACK_REQUESTED;
  520. }
  521. /* re-ACK packets previously received out-of-order */
  522. for (offset++; offset < RXRPC_CALL_ACK_WINDOW_SIZE; offset++)
  523. if (call->ackr_array[offset] != RXRPC_ACK_TYPE_ACK)
  524. break;
  525. call->ackr_pend_cnt = offset;
  526. /* generate an ACK if we fill up the window */
  527. if (call->ackr_pend_cnt >= RXRPC_CALL_ACK_WINDOW_SIZE)
  528. force = 1;
  529. gen_ACK:
  530. _debug("%05lu ACKs pend=%u norm=%s special=%s%s",
  531. jiffies - call->cjif,
  532. call->ackr_pend_cnt,
  533. rxrpc_acks[do_ACK],
  534. rxrpc_acks[special_ACK],
  535. force ? " immediate" :
  536. do_ACK == RXRPC_ACK_REQUESTED ? " merge-req" :
  537. hdr->flags & RXRPC_LAST_PACKET ? " finalise" :
  538. " defer"
  539. );
  540. /* send any pending normal ACKs if need be */
  541. if (call->ackr_pend_cnt > 0) {
  542. /* fill out the appropriate form */
  543. call->ackr.bufferSpace = htons(RXRPC_CALL_ACK_WINDOW_SIZE);
  544. call->ackr.maxSkew = htons(min(call->ackr_high_seq - seq,
  545. 65535U));
  546. call->ackr.firstPacket = htonl(call->ackr_win_bot);
  547. call->ackr.previousPacket = call->ackr_prev_seq;
  548. call->ackr.serial = hdr->serial;
  549. call->ackr.nAcks = call->ackr_pend_cnt;
  550. if (do_ACK == RXRPC_ACK_REQUESTED)
  551. call->ackr.reason = do_ACK;
  552. /* generate the ACK immediately if necessary */
  553. if (special_ACK || force) {
  554. err = __rxrpc_call_gen_normal_ACK(
  555. call, do_ACK == RXRPC_ACK_DELAY ? 0 : seq);
  556. if (err < 0) {
  557. ret = err;
  558. goto out;
  559. }
  560. }
  561. }
  562. if (call->ackr.reason == RXRPC_ACK_REQUESTED)
  563. call->ackr_dfr_seq = seq;
  564. /* start the ACK timer if not running if there are any pending deferred
  565. * ACKs */
  566. if (call->ackr_pend_cnt > 0 &&
  567. call->ackr.reason != RXRPC_ACK_REQUESTED &&
  568. !timer_pending(&call->ackr_dfr_timo)
  569. ) {
  570. unsigned long timo;
  571. timo = rxrpc_call_dfr_ack_timeout + jiffies;
  572. _debug("START ACKR TIMER for cj=%lu", timo - call->cjif);
  573. spin_lock(&call->lock);
  574. mod_timer(&call->ackr_dfr_timo, timo);
  575. spin_unlock(&call->lock);
  576. }
  577. else if ((call->ackr_pend_cnt == 0 ||
  578. call->ackr.reason == RXRPC_ACK_REQUESTED) &&
  579. timer_pending(&call->ackr_dfr_timo)
  580. ) {
  581. /* stop timer if no pending ACKs */
  582. _debug("CLEAR ACKR TIMER");
  583. del_timer_sync(&call->ackr_dfr_timo);
  584. }
  585. /* send a special ACK if one is required */
  586. if (special_ACK) {
  587. struct rxrpc_ackpacket ack;
  588. struct kvec diov[2];
  589. uint8_t acks[1] = { RXRPC_ACK_TYPE_ACK };
  590. /* fill out the appropriate form */
  591. ack.bufferSpace = htons(RXRPC_CALL_ACK_WINDOW_SIZE);
  592. ack.maxSkew = htons(min(call->ackr_high_seq - seq,
  593. 65535U));
  594. ack.firstPacket = htonl(call->ackr_win_bot);
  595. ack.previousPacket = call->ackr_prev_seq;
  596. ack.serial = hdr->serial;
  597. ack.reason = special_ACK;
  598. ack.nAcks = 0;
  599. _proto("Rx Sending s-ACK"
  600. " { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }",
  601. ntohs(ack.maxSkew),
  602. ntohl(ack.firstPacket),
  603. ntohl(ack.previousPacket),
  604. ntohl(ack.serial),
  605. rxrpc_acks[ack.reason],
  606. ack.nAcks);
  607. diov[0].iov_len = sizeof(struct rxrpc_ackpacket);
  608. diov[0].iov_base = &ack;
  609. diov[1].iov_len = sizeof(acks);
  610. diov[1].iov_base = acks;
  611. /* build and send the message */
  612. err = rxrpc_conn_newmsg(call->conn,call, RXRPC_PACKET_TYPE_ACK,
  613. hdr->seq ? 2 : 1, diov,
  614. GFP_KERNEL,
  615. &msg);
  616. if (err < 0) {
  617. ret = err;
  618. goto out;
  619. }
  620. msg->seq = seq;
  621. msg->hdr.seq = htonl(seq);
  622. msg->hdr.flags |= RXRPC_SLOW_START_OK;
  623. err = rxrpc_conn_sendmsg(call->conn, msg);
  624. rxrpc_put_message(msg);
  625. if (err < 0) {
  626. ret = err;
  627. goto out;
  628. }
  629. call->pkt_snd_count++;
  630. }
  631. out:
  632. if (hdr->seq)
  633. call->ackr_prev_seq = hdr->seq;
  634. _leave(" = %d", ret);
  635. return ret;
  636. } /* end rxrpc_call_generate_ACK() */
  637. /*****************************************************************************/
  638. /*
  639. * handle work to be done on a call
  640. * - includes packet reception and timeout processing
  641. */
  642. void rxrpc_call_do_stuff(struct rxrpc_call *call)
  643. {
  644. _enter("%p{flags=%lx}", call, call->flags);
  645. /* handle packet reception */
  646. if (call->flags & RXRPC_CALL_RCV_PKT) {
  647. _debug("- receive packet");
  648. call->flags &= ~RXRPC_CALL_RCV_PKT;
  649. rxrpc_call_receive_packet(call);
  650. }
  651. /* handle overdue ACKs */
  652. if (call->flags & RXRPC_CALL_ACKS_TIMO) {
  653. _debug("- overdue ACK timeout");
  654. call->flags &= ~RXRPC_CALL_ACKS_TIMO;
  655. rxrpc_call_resend(call, call->snd_seq_count);
  656. }
  657. /* handle lack of reception */
  658. if (call->flags & RXRPC_CALL_RCV_TIMO) {
  659. _debug("- reception timeout");
  660. call->flags &= ~RXRPC_CALL_RCV_TIMO;
  661. rxrpc_call_abort(call, -EIO);
  662. }
  663. /* handle deferred ACKs */
  664. if (call->flags & RXRPC_CALL_ACKR_TIMO ||
  665. (call->ackr.nAcks > 0 && call->ackr.reason == RXRPC_ACK_REQUESTED)
  666. ) {
  667. _debug("- deferred ACK timeout: cj=%05lu r=%s n=%u",
  668. jiffies - call->cjif,
  669. rxrpc_acks[call->ackr.reason],
  670. call->ackr.nAcks);
  671. call->flags &= ~RXRPC_CALL_ACKR_TIMO;
  672. if (call->ackr.nAcks > 0 &&
  673. call->app_call_state != RXRPC_CSTATE_ERROR) {
  674. /* generate ACK */
  675. __rxrpc_call_gen_normal_ACK(call, call->ackr_dfr_seq);
  676. call->ackr_dfr_seq = 0;
  677. }
  678. }
  679. _leave("");
  680. } /* end rxrpc_call_do_stuff() */
  681. /*****************************************************************************/
  682. /*
  683. * send an abort message at call or connection level
  684. * - must be called with call->lock held
  685. * - the supplied error code is sent as the packet data
  686. */
  687. static int __rxrpc_call_abort(struct rxrpc_call *call, int errno)
  688. {
  689. struct rxrpc_connection *conn = call->conn;
  690. struct rxrpc_message *msg;
  691. struct kvec diov[1];
  692. int ret;
  693. __be32 _error;
  694. _enter("%p{%08x},%p{%d},%d",
  695. conn, ntohl(conn->conn_id), call, ntohl(call->call_id), errno);
  696. /* if this call is already aborted, then just wake up any waiters */
  697. if (call->app_call_state == RXRPC_CSTATE_ERROR) {
  698. spin_unlock(&call->lock);
  699. call->app_error_func(call);
  700. _leave(" = 0");
  701. return 0;
  702. }
  703. rxrpc_get_call(call);
  704. /* change the state _with_ the lock still held */
  705. call->app_call_state = RXRPC_CSTATE_ERROR;
  706. call->app_err_state = RXRPC_ESTATE_LOCAL_ABORT;
  707. call->app_errno = errno;
  708. call->app_mark = RXRPC_APP_MARK_EOF;
  709. call->app_read_buf = NULL;
  710. call->app_async_read = 0;
  711. _state(call);
  712. /* ask the app to translate the error code */
  713. call->app_aemap_func(call);
  714. spin_unlock(&call->lock);
  715. /* flush any outstanding ACKs */
  716. del_timer_sync(&call->acks_timeout);
  717. del_timer_sync(&call->rcv_timeout);
  718. del_timer_sync(&call->ackr_dfr_timo);
  719. if (rxrpc_call_is_ack_pending(call))
  720. __rxrpc_call_gen_normal_ACK(call, 0);
  721. /* send the abort packet only if we actually traded some other
  722. * packets */
  723. ret = 0;
  724. if (call->pkt_snd_count || call->pkt_rcv_count) {
  725. /* actually send the abort */
  726. _proto("Rx Sending Call ABORT { data=%d }",
  727. call->app_abort_code);
  728. _error = htonl(call->app_abort_code);
  729. diov[0].iov_len = sizeof(_error);
  730. diov[0].iov_base = &_error;
  731. ret = rxrpc_conn_newmsg(conn, call, RXRPC_PACKET_TYPE_ABORT,
  732. 1, diov, GFP_KERNEL, &msg);
  733. if (ret == 0) {
  734. ret = rxrpc_conn_sendmsg(conn, msg);
  735. rxrpc_put_message(msg);
  736. }
  737. }
  738. /* tell the app layer to let go */
  739. call->app_error_func(call);
  740. rxrpc_put_call(call);
  741. _leave(" = %d", ret);
  742. return ret;
  743. } /* end __rxrpc_call_abort() */
  744. /*****************************************************************************/
  745. /*
  746. * send an abort message at call or connection level
  747. * - the supplied error code is sent as the packet data
  748. */
  749. int rxrpc_call_abort(struct rxrpc_call *call, int error)
  750. {
  751. spin_lock(&call->lock);
  752. return __rxrpc_call_abort(call, error);
  753. } /* end rxrpc_call_abort() */
  754. /*****************************************************************************/
  755. /*
  756. * process packets waiting for this call
  757. */
  758. static void rxrpc_call_receive_packet(struct rxrpc_call *call)
  759. {
  760. struct rxrpc_message *msg;
  761. struct list_head *_p;
  762. _enter("%p", call);
  763. rxrpc_get_call(call); /* must not go away too soon if aborted by
  764. * app-layer */
  765. while (!list_empty(&call->rcv_receiveq)) {
  766. /* try to get next packet */
  767. _p = NULL;
  768. spin_lock(&call->lock);
  769. if (!list_empty(&call->rcv_receiveq)) {
  770. _p = call->rcv_receiveq.next;
  771. list_del_init(_p);
  772. }
  773. spin_unlock(&call->lock);
  774. if (!_p)
  775. break;
  776. msg = list_entry(_p, struct rxrpc_message, link);
  777. _proto("Rx %05lu Received %s packet (%%%u,#%u,%c%c%c%c%c)",
  778. jiffies - call->cjif,
  779. rxrpc_pkts[msg->hdr.type],
  780. ntohl(msg->hdr.serial),
  781. msg->seq,
  782. msg->hdr.flags & RXRPC_JUMBO_PACKET ? 'j' : '-',
  783. msg->hdr.flags & RXRPC_MORE_PACKETS ? 'm' : '-',
  784. msg->hdr.flags & RXRPC_LAST_PACKET ? 'l' : '-',
  785. msg->hdr.flags & RXRPC_REQUEST_ACK ? 'r' : '-',
  786. msg->hdr.flags & RXRPC_CLIENT_INITIATED ? 'C' : 'S'
  787. );
  788. switch (msg->hdr.type) {
  789. /* deal with data packets */
  790. case RXRPC_PACKET_TYPE_DATA:
  791. /* ACK the packet if necessary */
  792. switch (rxrpc_call_generate_ACK(call, &msg->hdr,
  793. NULL)) {
  794. case 0: /* useful packet */
  795. rxrpc_call_receive_data_packet(call, msg);
  796. break;
  797. case 1: /* duplicate or out-of-window packet */
  798. break;
  799. default:
  800. rxrpc_put_message(msg);
  801. goto out;
  802. }
  803. break;
  804. /* deal with ACK packets */
  805. case RXRPC_PACKET_TYPE_ACK:
  806. rxrpc_call_receive_ack_packet(call, msg);
  807. break;
  808. /* deal with abort packets */
  809. case RXRPC_PACKET_TYPE_ABORT: {
  810. __be32 _dbuf, *dp;
  811. dp = skb_header_pointer(msg->pkt, msg->offset,
  812. sizeof(_dbuf), &_dbuf);
  813. if (dp == NULL)
  814. printk("Rx Received short ABORT packet\n");
  815. _proto("Rx Received Call ABORT { data=%d }",
  816. (dp ? ntohl(*dp) : 0));
  817. spin_lock(&call->lock);
  818. call->app_call_state = RXRPC_CSTATE_ERROR;
  819. call->app_err_state = RXRPC_ESTATE_PEER_ABORT;
  820. call->app_abort_code = (dp ? ntohl(*dp) : 0);
  821. call->app_errno = -ECONNABORTED;
  822. call->app_mark = RXRPC_APP_MARK_EOF;
  823. call->app_read_buf = NULL;
  824. call->app_async_read = 0;
  825. /* ask the app to translate the error code */
  826. call->app_aemap_func(call);
  827. _state(call);
  828. spin_unlock(&call->lock);
  829. call->app_error_func(call);
  830. break;
  831. }
  832. default:
  833. /* deal with other packet types */
  834. _proto("Rx Unsupported packet type %u (#%u)",
  835. msg->hdr.type, msg->seq);
  836. break;
  837. }
  838. rxrpc_put_message(msg);
  839. }
  840. out:
  841. rxrpc_put_call(call);
  842. _leave("");
  843. } /* end rxrpc_call_receive_packet() */
  844. /*****************************************************************************/
  845. /*
  846. * process next data packet
  847. * - as the next data packet arrives:
  848. * - it is queued on app_readyq _if_ it is the next one expected
  849. * (app_ready_seq+1)
  850. * - it is queued on app_unreadyq _if_ it is not the next one expected
  851. * - if a packet placed on app_readyq completely fills a hole leading up to
  852. * the first packet on app_unreadyq, then packets now in sequence are
  853. * tranferred to app_readyq
  854. * - the application layer can only see packets on app_readyq
  855. * (app_ready_qty bytes)
  856. * - the application layer is prodded every time a new packet arrives
  857. */
  858. static void rxrpc_call_receive_data_packet(struct rxrpc_call *call,
  859. struct rxrpc_message *msg)
  860. {
  861. const struct rxrpc_operation *optbl, *op;
  862. struct rxrpc_message *pmsg;
  863. struct list_head *_p;
  864. int ret, lo, hi, rmtimo;
  865. __be32 opid;
  866. _enter("%p{%u},%p{%u}", call, ntohl(call->call_id), msg, msg->seq);
  867. rxrpc_get_message(msg);
  868. /* add to the unready queue if we'd have to create a hole in the ready
  869. * queue otherwise */
  870. if (msg->seq != call->app_ready_seq + 1) {
  871. _debug("Call add packet %d to unreadyq", msg->seq);
  872. /* insert in seq order */
  873. list_for_each(_p, &call->app_unreadyq) {
  874. pmsg = list_entry(_p, struct rxrpc_message, link);
  875. if (pmsg->seq > msg->seq)
  876. break;
  877. }
  878. list_add_tail(&msg->link, _p);
  879. _leave(" [unreadyq]");
  880. return;
  881. }
  882. /* next in sequence - simply append into the call's ready queue */
  883. _debug("Call add packet %d to readyq (+%Zd => %Zd bytes)",
  884. msg->seq, msg->dsize, call->app_ready_qty);
  885. spin_lock(&call->lock);
  886. call->app_ready_seq = msg->seq;
  887. call->app_ready_qty += msg->dsize;
  888. list_add_tail(&msg->link, &call->app_readyq);
  889. /* move unready packets to the readyq if we got rid of a hole */
  890. while (!list_empty(&call->app_unreadyq)) {
  891. pmsg = list_entry(call->app_unreadyq.next,
  892. struct rxrpc_message, link);
  893. if (pmsg->seq != call->app_ready_seq + 1)
  894. break;
  895. /* next in sequence - just move list-to-list */
  896. _debug("Call transfer packet %d to readyq (+%Zd => %Zd bytes)",
  897. pmsg->seq, pmsg->dsize, call->app_ready_qty);
  898. call->app_ready_seq = pmsg->seq;
  899. call->app_ready_qty += pmsg->dsize;
  900. list_move_tail(&pmsg->link, &call->app_readyq);
  901. }
  902. /* see if we've got the last packet yet */
  903. if (!list_empty(&call->app_readyq)) {
  904. pmsg = list_entry(call->app_readyq.prev,
  905. struct rxrpc_message, link);
  906. if (pmsg->hdr.flags & RXRPC_LAST_PACKET) {
  907. call->app_last_rcv = 1;
  908. _debug("Last packet on readyq");
  909. }
  910. }
  911. switch (call->app_call_state) {
  912. /* do nothing if call already aborted */
  913. case RXRPC_CSTATE_ERROR:
  914. spin_unlock(&call->lock);
  915. _leave(" [error]");
  916. return;
  917. /* extract the operation ID from an incoming call if that's not
  918. * yet been done */
  919. case RXRPC_CSTATE_SRVR_RCV_OPID:
  920. spin_unlock(&call->lock);
  921. /* handle as yet insufficient data for the operation ID */
  922. if (call->app_ready_qty < 4) {
  923. if (call->app_last_rcv)
  924. /* trouble - last packet seen */
  925. rxrpc_call_abort(call, -EINVAL);
  926. _leave("");
  927. return;
  928. }
  929. /* pull the operation ID out of the buffer */
  930. ret = rxrpc_call_read_data(call, &opid, sizeof(opid), 0);
  931. if (ret < 0) {
  932. printk("Unexpected error from read-data: %d\n", ret);
  933. if (call->app_call_state != RXRPC_CSTATE_ERROR)
  934. rxrpc_call_abort(call, ret);
  935. _leave("");
  936. return;
  937. }
  938. call->app_opcode = ntohl(opid);
  939. /* locate the operation in the available ops table */
  940. optbl = call->conn->service->ops_begin;
  941. lo = 0;
  942. hi = call->conn->service->ops_end - optbl;
  943. while (lo < hi) {
  944. int mid = (hi + lo) / 2;
  945. op = &optbl[mid];
  946. if (call->app_opcode == op->id)
  947. goto found_op;
  948. if (call->app_opcode > op->id)
  949. lo = mid + 1;
  950. else
  951. hi = mid;
  952. }
  953. /* search failed */
  954. kproto("Rx Client requested operation %d from %s service",
  955. call->app_opcode, call->conn->service->name);
  956. rxrpc_call_abort(call, -EINVAL);
  957. _leave(" [inval]");
  958. return;
  959. found_op:
  960. _proto("Rx Client requested operation %s from %s service",
  961. op->name, call->conn->service->name);
  962. /* we're now waiting for the argument block (unless the call
  963. * was aborted) */
  964. spin_lock(&call->lock);
  965. if (call->app_call_state == RXRPC_CSTATE_SRVR_RCV_OPID ||
  966. call->app_call_state == RXRPC_CSTATE_SRVR_SND_REPLY) {
  967. if (!call->app_last_rcv)
  968. call->app_call_state =
  969. RXRPC_CSTATE_SRVR_RCV_ARGS;
  970. else if (call->app_ready_qty > 0)
  971. call->app_call_state =
  972. RXRPC_CSTATE_SRVR_GOT_ARGS;
  973. else
  974. call->app_call_state =
  975. RXRPC_CSTATE_SRVR_SND_REPLY;
  976. call->app_mark = op->asize;
  977. call->app_user = op->user;
  978. }
  979. spin_unlock(&call->lock);
  980. _state(call);
  981. break;
  982. case RXRPC_CSTATE_SRVR_RCV_ARGS:
  983. /* change state if just received last packet of arg block */
  984. if (call->app_last_rcv)
  985. call->app_call_state = RXRPC_CSTATE_SRVR_GOT_ARGS;
  986. spin_unlock(&call->lock);
  987. _state(call);
  988. break;
  989. case RXRPC_CSTATE_CLNT_RCV_REPLY:
  990. /* change state if just received last packet of reply block */
  991. rmtimo = 0;
  992. if (call->app_last_rcv) {
  993. call->app_call_state = RXRPC_CSTATE_CLNT_GOT_REPLY;
  994. rmtimo = 1;
  995. }
  996. spin_unlock(&call->lock);
  997. if (rmtimo) {
  998. del_timer_sync(&call->acks_timeout);
  999. del_timer_sync(&call->rcv_timeout);
  1000. del_timer_sync(&call->ackr_dfr_timo);
  1001. }
  1002. _state(call);
  1003. break;
  1004. default:
  1005. /* deal with data reception in an unexpected state */
  1006. printk("Unexpected state [[[ %u ]]]\n", call->app_call_state);
  1007. __rxrpc_call_abort(call, -EBADMSG);
  1008. _leave("");
  1009. return;
  1010. }
  1011. if (call->app_call_state == RXRPC_CSTATE_CLNT_RCV_REPLY &&
  1012. call->app_last_rcv)
  1013. BUG();
  1014. /* otherwise just invoke the data function whenever we can satisfy its desire for more
  1015. * data
  1016. */
  1017. _proto("Rx Received Op Data: st=%u qty=%Zu mk=%Zu%s",
  1018. call->app_call_state, call->app_ready_qty, call->app_mark,
  1019. call->app_last_rcv ? " last-rcvd" : "");
  1020. spin_lock(&call->lock);
  1021. ret = __rxrpc_call_read_data(call);
  1022. switch (ret) {
  1023. case 0:
  1024. spin_unlock(&call->lock);
  1025. call->app_attn_func(call);
  1026. break;
  1027. case -EAGAIN:
  1028. spin_unlock(&call->lock);
  1029. break;
  1030. case -ECONNABORTED:
  1031. spin_unlock(&call->lock);
  1032. break;
  1033. default:
  1034. __rxrpc_call_abort(call, ret);
  1035. break;
  1036. }
  1037. _state(call);
  1038. _leave("");
  1039. } /* end rxrpc_call_receive_data_packet() */
  1040. /*****************************************************************************/
  1041. /*
  1042. * received an ACK packet
  1043. */
  1044. static void rxrpc_call_receive_ack_packet(struct rxrpc_call *call,
  1045. struct rxrpc_message *msg)
  1046. {
  1047. struct rxrpc_ackpacket _ack, *ap;
  1048. rxrpc_serial_net_t serial;
  1049. rxrpc_seq_t seq;
  1050. int ret;
  1051. _enter("%p{%u},%p{%u}", call, ntohl(call->call_id), msg, msg->seq);
  1052. /* extract the basic ACK record */
  1053. ap = skb_header_pointer(msg->pkt, msg->offset, sizeof(_ack), &_ack);
  1054. if (ap == NULL) {
  1055. printk("Rx Received short ACK packet\n");
  1056. return;
  1057. }
  1058. msg->offset += sizeof(_ack);
  1059. serial = ap->serial;
  1060. seq = ntohl(ap->firstPacket);
  1061. _proto("Rx Received ACK %%%d { b=%hu m=%hu f=%u p=%u s=%u r=%s n=%u }",
  1062. ntohl(msg->hdr.serial),
  1063. ntohs(ap->bufferSpace),
  1064. ntohs(ap->maxSkew),
  1065. seq,
  1066. ntohl(ap->previousPacket),
  1067. ntohl(serial),
  1068. rxrpc_acks[ap->reason],
  1069. call->ackr.nAcks
  1070. );
  1071. /* check the other side isn't ACK'ing a sequence number I haven't sent
  1072. * yet */
  1073. if (ap->nAcks > 0 &&
  1074. (seq > call->snd_seq_count ||
  1075. seq + ap->nAcks - 1 > call->snd_seq_count)) {
  1076. printk("Received ACK (#%u-#%u) for unsent packet\n",
  1077. seq, seq + ap->nAcks - 1);
  1078. rxrpc_call_abort(call, -EINVAL);
  1079. _leave("");
  1080. return;
  1081. }
  1082. /* deal with RTT calculation */
  1083. if (serial) {
  1084. struct rxrpc_message *rttmsg;
  1085. /* find the prompting packet */
  1086. spin_lock(&call->lock);
  1087. if (call->snd_ping && call->snd_ping->hdr.serial == serial) {
  1088. /* it was a ping packet */
  1089. rttmsg = call->snd_ping;
  1090. call->snd_ping = NULL;
  1091. spin_unlock(&call->lock);
  1092. if (rttmsg) {
  1093. rttmsg->rttdone = 1;
  1094. rxrpc_peer_calculate_rtt(call->conn->peer,
  1095. rttmsg, msg);
  1096. rxrpc_put_message(rttmsg);
  1097. }
  1098. }
  1099. else {
  1100. struct list_head *_p;
  1101. /* it ought to be a data packet - look in the pending
  1102. * ACK list */
  1103. list_for_each(_p, &call->acks_pendq) {
  1104. rttmsg = list_entry(_p, struct rxrpc_message,
  1105. link);
  1106. if (rttmsg->hdr.serial == serial) {
  1107. if (rttmsg->rttdone)
  1108. /* never do RTT twice without
  1109. * resending */
  1110. break;
  1111. rttmsg->rttdone = 1;
  1112. rxrpc_peer_calculate_rtt(
  1113. call->conn->peer, rttmsg, msg);
  1114. break;
  1115. }
  1116. }
  1117. spin_unlock(&call->lock);
  1118. }
  1119. }
  1120. switch (ap->reason) {
  1121. /* deal with negative/positive acknowledgement of data
  1122. * packets */
  1123. case RXRPC_ACK_REQUESTED:
  1124. case RXRPC_ACK_DELAY:
  1125. case RXRPC_ACK_IDLE:
  1126. rxrpc_call_definitively_ACK(call, seq - 1);
  1127. case RXRPC_ACK_DUPLICATE:
  1128. case RXRPC_ACK_OUT_OF_SEQUENCE:
  1129. case RXRPC_ACK_EXCEEDS_WINDOW:
  1130. call->snd_resend_cnt = 0;
  1131. ret = rxrpc_call_record_ACK(call, msg, seq, ap->nAcks);
  1132. if (ret < 0)
  1133. rxrpc_call_abort(call, ret);
  1134. break;
  1135. /* respond to ping packets immediately */
  1136. case RXRPC_ACK_PING:
  1137. rxrpc_call_generate_ACK(call, &msg->hdr, ap);
  1138. break;
  1139. /* only record RTT on ping response packets */
  1140. case RXRPC_ACK_PING_RESPONSE:
  1141. if (call->snd_ping) {
  1142. struct rxrpc_message *rttmsg;
  1143. /* only do RTT stuff if the response matches the
  1144. * retained ping */
  1145. rttmsg = NULL;
  1146. spin_lock(&call->lock);
  1147. if (call->snd_ping &&
  1148. call->snd_ping->hdr.serial == ap->serial) {
  1149. rttmsg = call->snd_ping;
  1150. call->snd_ping = NULL;
  1151. }
  1152. spin_unlock(&call->lock);
  1153. if (rttmsg) {
  1154. rttmsg->rttdone = 1;
  1155. rxrpc_peer_calculate_rtt(call->conn->peer,
  1156. rttmsg, msg);
  1157. rxrpc_put_message(rttmsg);
  1158. }
  1159. }
  1160. break;
  1161. default:
  1162. printk("Unsupported ACK reason %u\n", ap->reason);
  1163. break;
  1164. }
  1165. _leave("");
  1166. } /* end rxrpc_call_receive_ack_packet() */
  1167. /*****************************************************************************/
  1168. /*
  1169. * record definitive ACKs for all messages up to and including the one with the
  1170. * 'highest' seq
  1171. */
  1172. static void rxrpc_call_definitively_ACK(struct rxrpc_call *call,
  1173. rxrpc_seq_t highest)
  1174. {
  1175. struct rxrpc_message *msg;
  1176. int now_complete;
  1177. _enter("%p{ads=%u},%u", call, call->acks_dftv_seq, highest);
  1178. while (call->acks_dftv_seq < highest) {
  1179. call->acks_dftv_seq++;
  1180. _proto("Definitive ACK on packet #%u", call->acks_dftv_seq);
  1181. /* discard those at front of queue until message with highest
  1182. * ACK is found */
  1183. spin_lock(&call->lock);
  1184. msg = NULL;
  1185. if (!list_empty(&call->acks_pendq)) {
  1186. msg = list_entry(call->acks_pendq.next,
  1187. struct rxrpc_message, link);
  1188. list_del_init(&msg->link); /* dequeue */
  1189. if (msg->state == RXRPC_MSG_SENT)
  1190. call->acks_pend_cnt--;
  1191. }
  1192. spin_unlock(&call->lock);
  1193. /* insanity check */
  1194. if (!msg)
  1195. panic("%s(): acks_pendq unexpectedly empty\n",
  1196. __FUNCTION__);
  1197. if (msg->seq != call->acks_dftv_seq)
  1198. panic("%s(): Packet #%u expected at front of acks_pendq"
  1199. " (#%u found)\n",
  1200. __FUNCTION__, call->acks_dftv_seq, msg->seq);
  1201. /* discard the message */
  1202. msg->state = RXRPC_MSG_DONE;
  1203. rxrpc_put_message(msg);
  1204. }
  1205. /* if all sent packets are definitively ACK'd then prod any sleepers just in case */
  1206. now_complete = 0;
  1207. spin_lock(&call->lock);
  1208. if (call->acks_dftv_seq == call->snd_seq_count) {
  1209. if (call->app_call_state != RXRPC_CSTATE_COMPLETE) {
  1210. call->app_call_state = RXRPC_CSTATE_COMPLETE;
  1211. _state(call);
  1212. now_complete = 1;
  1213. }
  1214. }
  1215. spin_unlock(&call->lock);
  1216. if (now_complete) {
  1217. del_timer_sync(&call->acks_timeout);
  1218. del_timer_sync(&call->rcv_timeout);
  1219. del_timer_sync(&call->ackr_dfr_timo);
  1220. call->app_attn_func(call);
  1221. }
  1222. _leave("");
  1223. } /* end rxrpc_call_definitively_ACK() */
  1224. /*****************************************************************************/
  1225. /*
  1226. * record the specified amount of ACKs/NAKs
  1227. */
  1228. static int rxrpc_call_record_ACK(struct rxrpc_call *call,
  1229. struct rxrpc_message *msg,
  1230. rxrpc_seq_t seq,
  1231. size_t count)
  1232. {
  1233. struct rxrpc_message *dmsg;
  1234. struct list_head *_p;
  1235. rxrpc_seq_t highest;
  1236. unsigned ix;
  1237. size_t chunk;
  1238. char resend, now_complete;
  1239. u8 acks[16];
  1240. _enter("%p{apc=%u ads=%u},%p,%u,%Zu",
  1241. call, call->acks_pend_cnt, call->acks_dftv_seq,
  1242. msg, seq, count);
  1243. /* handle re-ACK'ing of definitively ACK'd packets (may be out-of-order
  1244. * ACKs) */
  1245. if (seq <= call->acks_dftv_seq) {
  1246. unsigned delta = call->acks_dftv_seq - seq;
  1247. if (count <= delta) {
  1248. _leave(" = 0 [all definitively ACK'd]");
  1249. return 0;
  1250. }
  1251. seq += delta;
  1252. count -= delta;
  1253. msg->offset += delta;
  1254. }
  1255. highest = seq + count - 1;
  1256. resend = 0;
  1257. while (count > 0) {
  1258. /* extract up to 16 ACK slots at a time */
  1259. chunk = min(count, sizeof(acks));
  1260. count -= chunk;
  1261. memset(acks, 2, sizeof(acks));
  1262. if (skb_copy_bits(msg->pkt, msg->offset, &acks, chunk) < 0) {
  1263. printk("Rx Received short ACK packet\n");
  1264. _leave(" = -EINVAL");
  1265. return -EINVAL;
  1266. }
  1267. msg->offset += chunk;
  1268. /* check that the ACK set is valid */
  1269. for (ix = 0; ix < chunk; ix++) {
  1270. switch (acks[ix]) {
  1271. case RXRPC_ACK_TYPE_ACK:
  1272. break;
  1273. case RXRPC_ACK_TYPE_NACK:
  1274. resend = 1;
  1275. break;
  1276. default:
  1277. printk("Rx Received unsupported ACK state"
  1278. " %u\n", acks[ix]);
  1279. _leave(" = -EINVAL");
  1280. return -EINVAL;
  1281. }
  1282. }
  1283. _proto("Rx ACK of packets #%u-#%u "
  1284. "[%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c] (pend=%u)",
  1285. seq, (unsigned) (seq + chunk - 1),
  1286. _acktype[acks[0x0]],
  1287. _acktype[acks[0x1]],
  1288. _acktype[acks[0x2]],
  1289. _acktype[acks[0x3]],
  1290. _acktype[acks[0x4]],
  1291. _acktype[acks[0x5]],
  1292. _acktype[acks[0x6]],
  1293. _acktype[acks[0x7]],
  1294. _acktype[acks[0x8]],
  1295. _acktype[acks[0x9]],
  1296. _acktype[acks[0xA]],
  1297. _acktype[acks[0xB]],
  1298. _acktype[acks[0xC]],
  1299. _acktype[acks[0xD]],
  1300. _acktype[acks[0xE]],
  1301. _acktype[acks[0xF]],
  1302. call->acks_pend_cnt
  1303. );
  1304. /* mark the packets in the ACK queue as being provisionally
  1305. * ACK'd */
  1306. ix = 0;
  1307. spin_lock(&call->lock);
  1308. /* find the first packet ACK'd/NAK'd here */
  1309. list_for_each(_p, &call->acks_pendq) {
  1310. dmsg = list_entry(_p, struct rxrpc_message, link);
  1311. if (dmsg->seq == seq)
  1312. goto found_first;
  1313. _debug("- %u: skipping #%u", ix, dmsg->seq);
  1314. }
  1315. goto bad_queue;
  1316. found_first:
  1317. do {
  1318. _debug("- %u: processing #%u (%c) apc=%u",
  1319. ix, dmsg->seq, _acktype[acks[ix]],
  1320. call->acks_pend_cnt);
  1321. if (acks[ix] == RXRPC_ACK_TYPE_ACK) {
  1322. if (dmsg->state == RXRPC_MSG_SENT)
  1323. call->acks_pend_cnt--;
  1324. dmsg->state = RXRPC_MSG_ACKED;
  1325. }
  1326. else {
  1327. if (dmsg->state == RXRPC_MSG_ACKED)
  1328. call->acks_pend_cnt++;
  1329. dmsg->state = RXRPC_MSG_SENT;
  1330. }
  1331. ix++;
  1332. seq++;
  1333. _p = dmsg->link.next;
  1334. dmsg = list_entry(_p, struct rxrpc_message, link);
  1335. } while(ix < chunk &&
  1336. _p != &call->acks_pendq &&
  1337. dmsg->seq == seq);
  1338. if (ix < chunk)
  1339. goto bad_queue;
  1340. spin_unlock(&call->lock);
  1341. }
  1342. if (resend)
  1343. rxrpc_call_resend(call, highest);
  1344. /* if all packets are provisionally ACK'd, then wake up anyone who's
  1345. * waiting for that */
  1346. now_complete = 0;
  1347. spin_lock(&call->lock);
  1348. if (call->acks_pend_cnt == 0) {
  1349. if (call->app_call_state == RXRPC_CSTATE_SRVR_RCV_FINAL_ACK) {
  1350. call->app_call_state = RXRPC_CSTATE_COMPLETE;
  1351. _state(call);
  1352. }
  1353. now_complete = 1;
  1354. }
  1355. spin_unlock(&call->lock);
  1356. if (now_complete) {
  1357. _debug("- wake up waiters");
  1358. del_timer_sync(&call->acks_timeout);
  1359. del_timer_sync(&call->rcv_timeout);
  1360. del_timer_sync(&call->ackr_dfr_timo);
  1361. call->app_attn_func(call);
  1362. }
  1363. _leave(" = 0 (apc=%u)", call->acks_pend_cnt);
  1364. return 0;
  1365. bad_queue:
  1366. panic("%s(): acks_pendq in bad state (packet #%u absent)\n",
  1367. __FUNCTION__, seq);
  1368. } /* end rxrpc_call_record_ACK() */
  1369. /*****************************************************************************/
  1370. /*
  1371. * transfer data from the ready packet queue to the asynchronous read buffer
  1372. * - since this func is the only one going to look at packets queued on
  1373. * app_readyq, we don't need a lock to modify or access them, only to modify
  1374. * the queue pointers
  1375. * - called with call->lock held
  1376. * - the buffer must be in kernel space
  1377. * - returns:
  1378. * 0 if buffer filled
  1379. * -EAGAIN if buffer not filled and more data to come
  1380. * -EBADMSG if last packet received and insufficient data left
  1381. * -ECONNABORTED if the call has in an error state
  1382. */
  1383. static int __rxrpc_call_read_data(struct rxrpc_call *call)
  1384. {
  1385. struct rxrpc_message *msg;
  1386. size_t qty;
  1387. int ret;
  1388. _enter("%p{as=%d buf=%p qty=%Zu/%Zu}",
  1389. call,
  1390. call->app_async_read, call->app_read_buf,
  1391. call->app_ready_qty, call->app_mark);
  1392. /* check the state */
  1393. switch (call->app_call_state) {
  1394. case RXRPC_CSTATE_SRVR_RCV_ARGS:
  1395. case RXRPC_CSTATE_CLNT_RCV_REPLY:
  1396. if (call->app_last_rcv) {
  1397. printk("%s(%p,%p,%Zd):"
  1398. " Inconsistent call state (%s, last pkt)",
  1399. __FUNCTION__,
  1400. call, call->app_read_buf, call->app_mark,
  1401. rxrpc_call_states[call->app_call_state]);
  1402. BUG();
  1403. }
  1404. break;
  1405. case RXRPC_CSTATE_SRVR_RCV_OPID:
  1406. case RXRPC_CSTATE_SRVR_GOT_ARGS:
  1407. case RXRPC_CSTATE_CLNT_GOT_REPLY:
  1408. break;
  1409. case RXRPC_CSTATE_SRVR_SND_REPLY:
  1410. if (!call->app_last_rcv) {
  1411. printk("%s(%p,%p,%Zd):"
  1412. " Inconsistent call state (%s, not last pkt)",
  1413. __FUNCTION__,
  1414. call, call->app_read_buf, call->app_mark,
  1415. rxrpc_call_states[call->app_call_state]);
  1416. BUG();
  1417. }
  1418. _debug("Trying to read data from call in SND_REPLY state");
  1419. break;
  1420. case RXRPC_CSTATE_ERROR:
  1421. _leave(" = -ECONNABORTED");
  1422. return -ECONNABORTED;
  1423. default:
  1424. printk("reading in unexpected state [[[ %u ]]]\n",
  1425. call->app_call_state);
  1426. BUG();
  1427. }
  1428. /* handle the case of not having an async buffer */
  1429. if (!call->app_async_read) {
  1430. if (call->app_mark == RXRPC_APP_MARK_EOF) {
  1431. ret = call->app_last_rcv ? 0 : -EAGAIN;
  1432. }
  1433. else {
  1434. if (call->app_mark >= call->app_ready_qty) {
  1435. call->app_mark = RXRPC_APP_MARK_EOF;
  1436. ret = 0;
  1437. }
  1438. else {
  1439. ret = call->app_last_rcv ? -EBADMSG : -EAGAIN;
  1440. }
  1441. }
  1442. _leave(" = %d [no buf]", ret);
  1443. return 0;
  1444. }
  1445. while (!list_empty(&call->app_readyq) && call->app_mark > 0) {
  1446. msg = list_entry(call->app_readyq.next,
  1447. struct rxrpc_message, link);
  1448. /* drag as much data as we need out of this packet */
  1449. qty = min(call->app_mark, msg->dsize);
  1450. _debug("reading %Zu from skb=%p off=%lu",
  1451. qty, msg->pkt, msg->offset);
  1452. if (call->app_read_buf)
  1453. if (skb_copy_bits(msg->pkt, msg->offset,
  1454. call->app_read_buf, qty) < 0)
  1455. panic("%s: Failed to copy data from packet:"
  1456. " (%p,%p,%Zd)",
  1457. __FUNCTION__,
  1458. call, call->app_read_buf, qty);
  1459. /* if that packet is now empty, discard it */
  1460. call->app_ready_qty -= qty;
  1461. msg->dsize -= qty;
  1462. if (msg->dsize == 0) {
  1463. list_del_init(&msg->link);
  1464. rxrpc_put_message(msg);
  1465. }
  1466. else {
  1467. msg->offset += qty;
  1468. }
  1469. call->app_mark -= qty;
  1470. if (call->app_read_buf)
  1471. call->app_read_buf += qty;
  1472. }
  1473. if (call->app_mark == 0) {
  1474. call->app_async_read = 0;
  1475. call->app_mark = RXRPC_APP_MARK_EOF;
  1476. call->app_read_buf = NULL;
  1477. /* adjust the state if used up all packets */
  1478. if (list_empty(&call->app_readyq) && call->app_last_rcv) {
  1479. switch (call->app_call_state) {
  1480. case RXRPC_CSTATE_SRVR_RCV_OPID:
  1481. call->app_call_state = RXRPC_CSTATE_SRVR_SND_REPLY;
  1482. call->app_mark = RXRPC_APP_MARK_EOF;
  1483. _state(call);
  1484. del_timer_sync(&call->rcv_timeout);
  1485. break;
  1486. case RXRPC_CSTATE_SRVR_GOT_ARGS:
  1487. call->app_call_state = RXRPC_CSTATE_SRVR_SND_REPLY;
  1488. _state(call);
  1489. del_timer_sync(&call->rcv_timeout);
  1490. break;
  1491. default:
  1492. call->app_call_state = RXRPC_CSTATE_COMPLETE;
  1493. _state(call);
  1494. del_timer_sync(&call->acks_timeout);
  1495. del_timer_sync(&call->ackr_dfr_timo);
  1496. del_timer_sync(&call->rcv_timeout);
  1497. break;
  1498. }
  1499. }
  1500. _leave(" = 0");
  1501. return 0;
  1502. }
  1503. if (call->app_last_rcv) {
  1504. _debug("Insufficient data (%Zu/%Zu)",
  1505. call->app_ready_qty, call->app_mark);
  1506. call->app_async_read = 0;
  1507. call->app_mark = RXRPC_APP_MARK_EOF;
  1508. call->app_read_buf = NULL;
  1509. _leave(" = -EBADMSG");
  1510. return -EBADMSG;
  1511. }
  1512. _leave(" = -EAGAIN");
  1513. return -EAGAIN;
  1514. } /* end __rxrpc_call_read_data() */
  1515. /*****************************************************************************/
  1516. /*
  1517. * attempt to read the specified amount of data from the call's ready queue
  1518. * into the buffer provided
  1519. * - since this func is the only one going to look at packets queued on
  1520. * app_readyq, we don't need a lock to modify or access them, only to modify
  1521. * the queue pointers
  1522. * - if the buffer pointer is NULL, then data is merely drained, not copied
  1523. * - if flags&RXRPC_CALL_READ_BLOCK, then the function will wait until there is
  1524. * enough data or an error will be generated
  1525. * - note that the caller must have added the calling task to the call's wait
  1526. * queue beforehand
  1527. * - if flags&RXRPC_CALL_READ_ALL, then an error will be generated if this
  1528. * function doesn't read all available data
  1529. */
  1530. int rxrpc_call_read_data(struct rxrpc_call *call,
  1531. void *buffer, size_t size, int flags)
  1532. {
  1533. int ret;
  1534. _enter("%p{arq=%Zu},%p,%Zd,%x",
  1535. call, call->app_ready_qty, buffer, size, flags);
  1536. spin_lock(&call->lock);
  1537. if (unlikely(!!call->app_read_buf)) {
  1538. spin_unlock(&call->lock);
  1539. _leave(" = -EBUSY");
  1540. return -EBUSY;
  1541. }
  1542. call->app_mark = size;
  1543. call->app_read_buf = buffer;
  1544. call->app_async_read = 1;
  1545. call->app_read_count++;
  1546. /* read as much data as possible */
  1547. ret = __rxrpc_call_read_data(call);
  1548. switch (ret) {
  1549. case 0:
  1550. if (flags & RXRPC_CALL_READ_ALL &&
  1551. (!call->app_last_rcv || call->app_ready_qty > 0)) {
  1552. _leave(" = -EBADMSG");
  1553. __rxrpc_call_abort(call, -EBADMSG);
  1554. return -EBADMSG;
  1555. }
  1556. spin_unlock(&call->lock);
  1557. call->app_attn_func(call);
  1558. _leave(" = 0");
  1559. return ret;
  1560. case -ECONNABORTED:
  1561. spin_unlock(&call->lock);
  1562. _leave(" = %d [aborted]", ret);
  1563. return ret;
  1564. default:
  1565. __rxrpc_call_abort(call, ret);
  1566. _leave(" = %d", ret);
  1567. return ret;
  1568. case -EAGAIN:
  1569. spin_unlock(&call->lock);
  1570. if (!(flags & RXRPC_CALL_READ_BLOCK)) {
  1571. _leave(" = -EAGAIN");
  1572. return -EAGAIN;
  1573. }
  1574. /* wait for the data to arrive */
  1575. _debug("blocking for data arrival");
  1576. for (;;) {
  1577. set_current_state(TASK_INTERRUPTIBLE);
  1578. if (!call->app_async_read || signal_pending(current))
  1579. break;
  1580. schedule();
  1581. }
  1582. set_current_state(TASK_RUNNING);
  1583. if (signal_pending(current)) {
  1584. _leave(" = -EINTR");
  1585. return -EINTR;
  1586. }
  1587. if (call->app_call_state == RXRPC_CSTATE_ERROR) {
  1588. _leave(" = -ECONNABORTED");
  1589. return -ECONNABORTED;
  1590. }
  1591. _leave(" = 0");
  1592. return 0;
  1593. }
  1594. } /* end rxrpc_call_read_data() */
  1595. /*****************************************************************************/
  1596. /*
  1597. * write data to a call
  1598. * - the data may not be sent immediately if it doesn't fill a buffer
  1599. * - if we can't queue all the data for buffering now, siov[] will have been
  1600. * adjusted to take account of what has been sent
  1601. */
  1602. int rxrpc_call_write_data(struct rxrpc_call *call,
  1603. size_t sioc,
  1604. struct kvec *siov,
  1605. u8 rxhdr_flags,
  1606. gfp_t alloc_flags,
  1607. int dup_data,
  1608. size_t *size_sent)
  1609. {
  1610. struct rxrpc_message *msg;
  1611. struct kvec *sptr;
  1612. size_t space, size, chunk, tmp;
  1613. char *buf;
  1614. int ret;
  1615. _enter("%p,%Zu,%p,%02x,%x,%d,%p",
  1616. call, sioc, siov, rxhdr_flags, alloc_flags, dup_data,
  1617. size_sent);
  1618. *size_sent = 0;
  1619. size = 0;
  1620. ret = -EINVAL;
  1621. /* can't send more if we've sent last packet from this end */
  1622. switch (call->app_call_state) {
  1623. case RXRPC_CSTATE_SRVR_SND_REPLY:
  1624. case RXRPC_CSTATE_CLNT_SND_ARGS:
  1625. break;
  1626. case RXRPC_CSTATE_ERROR:
  1627. ret = call->app_errno;
  1628. default:
  1629. goto out;
  1630. }
  1631. /* calculate how much data we've been given */
  1632. sptr = siov;
  1633. for (; sioc > 0; sptr++, sioc--) {
  1634. if (!sptr->iov_len)
  1635. continue;
  1636. if (!sptr->iov_base)
  1637. goto out;
  1638. size += sptr->iov_len;
  1639. }
  1640. _debug("- size=%Zu mtu=%Zu", size, call->conn->mtu_size);
  1641. do {
  1642. /* make sure there's a message under construction */
  1643. if (!call->snd_nextmsg) {
  1644. /* no - allocate a message with no data yet attached */
  1645. ret = rxrpc_conn_newmsg(call->conn, call,
  1646. RXRPC_PACKET_TYPE_DATA,
  1647. 0, NULL, alloc_flags,
  1648. &call->snd_nextmsg);
  1649. if (ret < 0)
  1650. goto out;
  1651. _debug("- allocated new message [ds=%Zu]",
  1652. call->snd_nextmsg->dsize);
  1653. }
  1654. msg = call->snd_nextmsg;
  1655. msg->hdr.flags |= rxhdr_flags;
  1656. /* deal with zero-length terminal packet */
  1657. if (size == 0) {
  1658. if (rxhdr_flags & RXRPC_LAST_PACKET) {
  1659. ret = rxrpc_call_flush(call);
  1660. if (ret < 0)
  1661. goto out;
  1662. }
  1663. break;
  1664. }
  1665. /* work out how much space current packet has available */
  1666. space = call->conn->mtu_size - msg->dsize;
  1667. chunk = min(space, size);
  1668. _debug("- [before] space=%Zu chunk=%Zu", space, chunk);
  1669. while (!siov->iov_len)
  1670. siov++;
  1671. /* if we are going to have to duplicate the data then coalesce
  1672. * it too */
  1673. if (dup_data) {
  1674. /* don't allocate more that 1 page at a time */
  1675. if (chunk > PAGE_SIZE)
  1676. chunk = PAGE_SIZE;
  1677. /* allocate a data buffer and attach to the message */
  1678. buf = kmalloc(chunk, alloc_flags);
  1679. if (unlikely(!buf)) {
  1680. if (msg->dsize ==
  1681. sizeof(struct rxrpc_header)) {
  1682. /* discard an empty msg and wind back
  1683. * the seq counter */
  1684. rxrpc_put_message(msg);
  1685. call->snd_nextmsg = NULL;
  1686. call->snd_seq_count--;
  1687. }
  1688. ret = -ENOMEM;
  1689. goto out;
  1690. }
  1691. tmp = msg->dcount++;
  1692. set_bit(tmp, &msg->dfree);
  1693. msg->data[tmp].iov_base = buf;
  1694. msg->data[tmp].iov_len = chunk;
  1695. msg->dsize += chunk;
  1696. *size_sent += chunk;
  1697. size -= chunk;
  1698. /* load the buffer with data */
  1699. while (chunk > 0) {
  1700. tmp = min(chunk, siov->iov_len);
  1701. memcpy(buf, siov->iov_base, tmp);
  1702. buf += tmp;
  1703. siov->iov_base += tmp;
  1704. siov->iov_len -= tmp;
  1705. if (!siov->iov_len)
  1706. siov++;
  1707. chunk -= tmp;
  1708. }
  1709. }
  1710. else {
  1711. /* we want to attach the supplied buffers directly */
  1712. while (chunk > 0 &&
  1713. msg->dcount < RXRPC_MSG_MAX_IOCS) {
  1714. tmp = msg->dcount++;
  1715. msg->data[tmp].iov_base = siov->iov_base;
  1716. msg->data[tmp].iov_len = siov->iov_len;
  1717. msg->dsize += siov->iov_len;
  1718. *size_sent += siov->iov_len;
  1719. size -= siov->iov_len;
  1720. chunk -= siov->iov_len;
  1721. siov++;
  1722. }
  1723. }
  1724. _debug("- [loaded] chunk=%Zu size=%Zu", chunk, size);
  1725. /* dispatch the message when full, final or requesting ACK */
  1726. if (msg->dsize >= call->conn->mtu_size || rxhdr_flags) {
  1727. ret = rxrpc_call_flush(call);
  1728. if (ret < 0)
  1729. goto out;
  1730. }
  1731. } while(size > 0);
  1732. ret = 0;
  1733. out:
  1734. _leave(" = %d (%Zd queued, %Zd rem)", ret, *size_sent, size);
  1735. return ret;
  1736. } /* end rxrpc_call_write_data() */
  1737. /*****************************************************************************/
  1738. /*
  1739. * flush outstanding packets to the network
  1740. */
  1741. static int rxrpc_call_flush(struct rxrpc_call *call)
  1742. {
  1743. struct rxrpc_message *msg;
  1744. int ret = 0;
  1745. _enter("%p", call);
  1746. rxrpc_get_call(call);
  1747. /* if there's a packet under construction, then dispatch it now */
  1748. if (call->snd_nextmsg) {
  1749. msg = call->snd_nextmsg;
  1750. call->snd_nextmsg = NULL;
  1751. if (msg->hdr.flags & RXRPC_LAST_PACKET) {
  1752. msg->hdr.flags &= ~RXRPC_MORE_PACKETS;
  1753. if (call->app_call_state != RXRPC_CSTATE_CLNT_SND_ARGS)
  1754. msg->hdr.flags |= RXRPC_REQUEST_ACK;
  1755. }
  1756. else {
  1757. msg->hdr.flags |= RXRPC_MORE_PACKETS;
  1758. }
  1759. _proto("Sending DATA message { ds=%Zu dc=%u df=%02lu }",
  1760. msg->dsize, msg->dcount, msg->dfree);
  1761. /* queue and adjust call state */
  1762. spin_lock(&call->lock);
  1763. list_add_tail(&msg->link, &call->acks_pendq);
  1764. /* decide what to do depending on current state and if this is
  1765. * the last packet */
  1766. ret = -EINVAL;
  1767. switch (call->app_call_state) {
  1768. case RXRPC_CSTATE_SRVR_SND_REPLY:
  1769. if (msg->hdr.flags & RXRPC_LAST_PACKET) {
  1770. call->app_call_state =
  1771. RXRPC_CSTATE_SRVR_RCV_FINAL_ACK;
  1772. _state(call);
  1773. }
  1774. break;
  1775. case RXRPC_CSTATE_CLNT_SND_ARGS:
  1776. if (msg->hdr.flags & RXRPC_LAST_PACKET) {
  1777. call->app_call_state =
  1778. RXRPC_CSTATE_CLNT_RCV_REPLY;
  1779. _state(call);
  1780. }
  1781. break;
  1782. case RXRPC_CSTATE_ERROR:
  1783. ret = call->app_errno;
  1784. default:
  1785. spin_unlock(&call->lock);
  1786. goto out;
  1787. }
  1788. call->acks_pend_cnt++;
  1789. mod_timer(&call->acks_timeout,
  1790. __rxrpc_rtt_based_timeout(call,
  1791. rxrpc_call_acks_timeout));
  1792. spin_unlock(&call->lock);
  1793. ret = rxrpc_conn_sendmsg(call->conn, msg);
  1794. if (ret == 0)
  1795. call->pkt_snd_count++;
  1796. }
  1797. out:
  1798. rxrpc_put_call(call);
  1799. _leave(" = %d", ret);
  1800. return ret;
  1801. } /* end rxrpc_call_flush() */
  1802. /*****************************************************************************/
  1803. /*
  1804. * resend NAK'd or unacknowledged packets up to the highest one specified
  1805. */
  1806. static void rxrpc_call_resend(struct rxrpc_call *call, rxrpc_seq_t highest)
  1807. {
  1808. struct rxrpc_message *msg;
  1809. struct list_head *_p;
  1810. rxrpc_seq_t seq = 0;
  1811. _enter("%p,%u", call, highest);
  1812. _proto("Rx Resend required");
  1813. /* handle too many resends */
  1814. if (call->snd_resend_cnt >= rxrpc_call_max_resend) {
  1815. _debug("Aborting due to too many resends (rcv=%d)",
  1816. call->pkt_rcv_count);
  1817. rxrpc_call_abort(call,
  1818. call->pkt_rcv_count > 0 ? -EIO : -ETIMEDOUT);
  1819. _leave("");
  1820. return;
  1821. }
  1822. spin_lock(&call->lock);
  1823. call->snd_resend_cnt++;
  1824. for (;;) {
  1825. /* determine which the next packet we might need to ACK is */
  1826. if (seq <= call->acks_dftv_seq)
  1827. seq = call->acks_dftv_seq;
  1828. seq++;
  1829. if (seq > highest)
  1830. break;
  1831. /* look for the packet in the pending-ACK queue */
  1832. list_for_each(_p, &call->acks_pendq) {
  1833. msg = list_entry(_p, struct rxrpc_message, link);
  1834. if (msg->seq == seq)
  1835. goto found_msg;
  1836. }
  1837. panic("%s(%p,%d):"
  1838. " Inconsistent pending-ACK queue (ds=%u sc=%u sq=%u)\n",
  1839. __FUNCTION__, call, highest,
  1840. call->acks_dftv_seq, call->snd_seq_count, seq);
  1841. found_msg:
  1842. if (msg->state != RXRPC_MSG_SENT)
  1843. continue; /* only un-ACK'd packets */
  1844. rxrpc_get_message(msg);
  1845. spin_unlock(&call->lock);
  1846. /* send each message again (and ignore any errors we might
  1847. * incur) */
  1848. _proto("Resending DATA message { ds=%Zu dc=%u df=%02lu }",
  1849. msg->dsize, msg->dcount, msg->dfree);
  1850. if (rxrpc_conn_sendmsg(call->conn, msg) == 0)
  1851. call->pkt_snd_count++;
  1852. rxrpc_put_message(msg);
  1853. spin_lock(&call->lock);
  1854. }
  1855. /* reset the timeout */
  1856. mod_timer(&call->acks_timeout,
  1857. __rxrpc_rtt_based_timeout(call, rxrpc_call_acks_timeout));
  1858. spin_unlock(&call->lock);
  1859. _leave("");
  1860. } /* end rxrpc_call_resend() */
  1861. /*****************************************************************************/
  1862. /*
  1863. * handle an ICMP error being applied to a call
  1864. */
  1865. void rxrpc_call_handle_error(struct rxrpc_call *call, int local, int errno)
  1866. {
  1867. _enter("%p{%u},%d", call, ntohl(call->call_id), errno);
  1868. /* if this call is already aborted, then just wake up any waiters */
  1869. if (call->app_call_state == RXRPC_CSTATE_ERROR) {
  1870. call->app_error_func(call);
  1871. }
  1872. else {
  1873. /* tell the app layer what happened */
  1874. spin_lock(&call->lock);
  1875. call->app_call_state = RXRPC_CSTATE_ERROR;
  1876. _state(call);
  1877. if (local)
  1878. call->app_err_state = RXRPC_ESTATE_LOCAL_ERROR;
  1879. else
  1880. call->app_err_state = RXRPC_ESTATE_REMOTE_ERROR;
  1881. call->app_errno = errno;
  1882. call->app_mark = RXRPC_APP_MARK_EOF;
  1883. call->app_read_buf = NULL;
  1884. call->app_async_read = 0;
  1885. /* map the error */
  1886. call->app_aemap_func(call);
  1887. del_timer_sync(&call->acks_timeout);
  1888. del_timer_sync(&call->rcv_timeout);
  1889. del_timer_sync(&call->ackr_dfr_timo);
  1890. spin_unlock(&call->lock);
  1891. call->app_error_func(call);
  1892. }
  1893. _leave("");
  1894. } /* end rxrpc_call_handle_error() */