ar-call.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  1. /* RxRPC individual remote procedure call handling
  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/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/circ_buf.h>
  14. #include <linux/hashtable.h>
  15. #include <linux/spinlock_types.h>
  16. #include <net/sock.h>
  17. #include <net/af_rxrpc.h>
  18. #include "ar-internal.h"
  19. /*
  20. * Maximum lifetime of a call (in jiffies).
  21. */
  22. unsigned int rxrpc_max_call_lifetime = 60 * HZ;
  23. /*
  24. * Time till dead call expires after last use (in jiffies).
  25. */
  26. unsigned int rxrpc_dead_call_expiry = 2 * HZ;
  27. const char *const rxrpc_call_states[NR__RXRPC_CALL_STATES] = {
  28. [RXRPC_CALL_CLIENT_SEND_REQUEST] = "ClSndReq",
  29. [RXRPC_CALL_CLIENT_AWAIT_REPLY] = "ClAwtRpl",
  30. [RXRPC_CALL_CLIENT_RECV_REPLY] = "ClRcvRpl",
  31. [RXRPC_CALL_CLIENT_FINAL_ACK] = "ClFnlACK",
  32. [RXRPC_CALL_SERVER_SECURING] = "SvSecure",
  33. [RXRPC_CALL_SERVER_ACCEPTING] = "SvAccept",
  34. [RXRPC_CALL_SERVER_RECV_REQUEST] = "SvRcvReq",
  35. [RXRPC_CALL_SERVER_ACK_REQUEST] = "SvAckReq",
  36. [RXRPC_CALL_SERVER_SEND_REPLY] = "SvSndRpl",
  37. [RXRPC_CALL_SERVER_AWAIT_ACK] = "SvAwtACK",
  38. [RXRPC_CALL_COMPLETE] = "Complete",
  39. [RXRPC_CALL_SERVER_BUSY] = "SvBusy ",
  40. [RXRPC_CALL_REMOTELY_ABORTED] = "RmtAbort",
  41. [RXRPC_CALL_LOCALLY_ABORTED] = "LocAbort",
  42. [RXRPC_CALL_NETWORK_ERROR] = "NetError",
  43. [RXRPC_CALL_DEAD] = "Dead ",
  44. };
  45. struct kmem_cache *rxrpc_call_jar;
  46. LIST_HEAD(rxrpc_calls);
  47. DEFINE_RWLOCK(rxrpc_call_lock);
  48. static void rxrpc_destroy_call(struct work_struct *work);
  49. static void rxrpc_call_life_expired(unsigned long _call);
  50. static void rxrpc_dead_call_expired(unsigned long _call);
  51. static void rxrpc_ack_time_expired(unsigned long _call);
  52. static void rxrpc_resend_time_expired(unsigned long _call);
  53. static DEFINE_SPINLOCK(rxrpc_call_hash_lock);
  54. static DEFINE_HASHTABLE(rxrpc_call_hash, 10);
  55. /*
  56. * Hash function for rxrpc_call_hash
  57. */
  58. static unsigned long rxrpc_call_hashfunc(
  59. u8 in_clientflag,
  60. u32 cid,
  61. u32 call_id,
  62. u32 epoch,
  63. u16 service_id,
  64. sa_family_t proto,
  65. void *localptr,
  66. unsigned int addr_size,
  67. const u8 *peer_addr)
  68. {
  69. const u16 *p;
  70. unsigned int i;
  71. unsigned long key;
  72. _enter("");
  73. key = (unsigned long)localptr;
  74. /* We just want to add up the __be32 values, so forcing the
  75. * cast should be okay.
  76. */
  77. key += epoch;
  78. key += service_id;
  79. key += call_id;
  80. key += (cid & RXRPC_CIDMASK) >> RXRPC_CIDSHIFT;
  81. key += cid & RXRPC_CHANNELMASK;
  82. key += in_clientflag;
  83. key += proto;
  84. /* Step through the peer address in 16-bit portions for speed */
  85. for (i = 0, p = (const u16 *)peer_addr; i < addr_size >> 1; i++, p++)
  86. key += *p;
  87. _leave(" key = 0x%lx", key);
  88. return key;
  89. }
  90. /*
  91. * Add a call to the hashtable
  92. */
  93. static void rxrpc_call_hash_add(struct rxrpc_call *call)
  94. {
  95. unsigned long key;
  96. unsigned int addr_size = 0;
  97. _enter("");
  98. switch (call->proto) {
  99. case AF_INET:
  100. addr_size = sizeof(call->peer_ip.ipv4_addr);
  101. break;
  102. case AF_INET6:
  103. addr_size = sizeof(call->peer_ip.ipv6_addr);
  104. break;
  105. default:
  106. break;
  107. }
  108. key = rxrpc_call_hashfunc(call->in_clientflag, call->cid,
  109. call->call_id, call->epoch,
  110. call->service_id, call->proto,
  111. call->conn->trans->local, addr_size,
  112. call->peer_ip.ipv6_addr);
  113. /* Store the full key in the call */
  114. call->hash_key = key;
  115. spin_lock(&rxrpc_call_hash_lock);
  116. hash_add_rcu(rxrpc_call_hash, &call->hash_node, key);
  117. spin_unlock(&rxrpc_call_hash_lock);
  118. _leave("");
  119. }
  120. /*
  121. * Remove a call from the hashtable
  122. */
  123. static void rxrpc_call_hash_del(struct rxrpc_call *call)
  124. {
  125. _enter("");
  126. spin_lock(&rxrpc_call_hash_lock);
  127. hash_del_rcu(&call->hash_node);
  128. spin_unlock(&rxrpc_call_hash_lock);
  129. _leave("");
  130. }
  131. /*
  132. * Find a call in the hashtable and return it, or NULL if it
  133. * isn't there.
  134. */
  135. struct rxrpc_call *rxrpc_find_call_hash(
  136. struct rxrpc_host_header *hdr,
  137. void *localptr,
  138. sa_family_t proto,
  139. const void *peer_addr)
  140. {
  141. unsigned long key;
  142. unsigned int addr_size = 0;
  143. struct rxrpc_call *call = NULL;
  144. struct rxrpc_call *ret = NULL;
  145. u8 in_clientflag = hdr->flags & RXRPC_CLIENT_INITIATED;
  146. _enter("");
  147. switch (proto) {
  148. case AF_INET:
  149. addr_size = sizeof(call->peer_ip.ipv4_addr);
  150. break;
  151. case AF_INET6:
  152. addr_size = sizeof(call->peer_ip.ipv6_addr);
  153. break;
  154. default:
  155. break;
  156. }
  157. key = rxrpc_call_hashfunc(in_clientflag, hdr->cid, hdr->callNumber,
  158. hdr->epoch, hdr->serviceId,
  159. proto, localptr, addr_size,
  160. peer_addr);
  161. hash_for_each_possible_rcu(rxrpc_call_hash, call, hash_node, key) {
  162. if (call->hash_key == key &&
  163. call->call_id == hdr->callNumber &&
  164. call->cid == hdr->cid &&
  165. call->in_clientflag == in_clientflag &&
  166. call->service_id == hdr->serviceId &&
  167. call->proto == proto &&
  168. call->local == localptr &&
  169. memcmp(call->peer_ip.ipv6_addr, peer_addr,
  170. addr_size) == 0 &&
  171. call->epoch == hdr->epoch) {
  172. ret = call;
  173. break;
  174. }
  175. }
  176. _leave(" = %p", ret);
  177. return ret;
  178. }
  179. /*
  180. * allocate a new call
  181. */
  182. static struct rxrpc_call *rxrpc_alloc_call(gfp_t gfp)
  183. {
  184. struct rxrpc_call *call;
  185. call = kmem_cache_zalloc(rxrpc_call_jar, gfp);
  186. if (!call)
  187. return NULL;
  188. call->acks_winsz = 16;
  189. call->acks_window = kmalloc(call->acks_winsz * sizeof(unsigned long),
  190. gfp);
  191. if (!call->acks_window) {
  192. kmem_cache_free(rxrpc_call_jar, call);
  193. return NULL;
  194. }
  195. setup_timer(&call->lifetimer, &rxrpc_call_life_expired,
  196. (unsigned long) call);
  197. setup_timer(&call->deadspan, &rxrpc_dead_call_expired,
  198. (unsigned long) call);
  199. setup_timer(&call->ack_timer, &rxrpc_ack_time_expired,
  200. (unsigned long) call);
  201. setup_timer(&call->resend_timer, &rxrpc_resend_time_expired,
  202. (unsigned long) call);
  203. INIT_WORK(&call->destroyer, &rxrpc_destroy_call);
  204. INIT_WORK(&call->processor, &rxrpc_process_call);
  205. INIT_LIST_HEAD(&call->accept_link);
  206. skb_queue_head_init(&call->rx_queue);
  207. skb_queue_head_init(&call->rx_oos_queue);
  208. init_waitqueue_head(&call->tx_waitq);
  209. spin_lock_init(&call->lock);
  210. rwlock_init(&call->state_lock);
  211. atomic_set(&call->usage, 1);
  212. call->debug_id = atomic_inc_return(&rxrpc_debug_id);
  213. call->state = RXRPC_CALL_CLIENT_SEND_REQUEST;
  214. memset(&call->sock_node, 0xed, sizeof(call->sock_node));
  215. call->rx_data_expect = 1;
  216. call->rx_data_eaten = 0;
  217. call->rx_first_oos = 0;
  218. call->ackr_win_top = call->rx_data_eaten + 1 + rxrpc_rx_window_size;
  219. call->creation_jif = jiffies;
  220. return call;
  221. }
  222. /*
  223. * allocate a new client call and attempt to get a connection slot for it
  224. */
  225. static struct rxrpc_call *rxrpc_alloc_client_call(
  226. struct rxrpc_sock *rx,
  227. struct rxrpc_transport *trans,
  228. struct rxrpc_conn_bundle *bundle,
  229. gfp_t gfp)
  230. {
  231. struct rxrpc_call *call;
  232. int ret;
  233. _enter("");
  234. ASSERT(rx != NULL);
  235. ASSERT(trans != NULL);
  236. ASSERT(bundle != NULL);
  237. call = rxrpc_alloc_call(gfp);
  238. if (!call)
  239. return ERR_PTR(-ENOMEM);
  240. sock_hold(&rx->sk);
  241. call->socket = rx;
  242. call->rx_data_post = 1;
  243. ret = rxrpc_connect_call(rx, trans, bundle, call, gfp);
  244. if (ret < 0) {
  245. kmem_cache_free(rxrpc_call_jar, call);
  246. return ERR_PTR(ret);
  247. }
  248. /* Record copies of information for hashtable lookup */
  249. call->proto = rx->proto;
  250. call->local = trans->local;
  251. switch (call->proto) {
  252. case AF_INET:
  253. call->peer_ip.ipv4_addr =
  254. trans->peer->srx.transport.sin.sin_addr.s_addr;
  255. break;
  256. case AF_INET6:
  257. memcpy(call->peer_ip.ipv6_addr,
  258. trans->peer->srx.transport.sin6.sin6_addr.in6_u.u6_addr8,
  259. sizeof(call->peer_ip.ipv6_addr));
  260. break;
  261. }
  262. call->epoch = call->conn->epoch;
  263. call->service_id = call->conn->service_id;
  264. call->in_clientflag = call->conn->in_clientflag;
  265. /* Add the new call to the hashtable */
  266. rxrpc_call_hash_add(call);
  267. spin_lock(&call->conn->trans->peer->lock);
  268. list_add(&call->error_link, &call->conn->trans->peer->error_targets);
  269. spin_unlock(&call->conn->trans->peer->lock);
  270. call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
  271. add_timer(&call->lifetimer);
  272. _leave(" = %p", call);
  273. return call;
  274. }
  275. /*
  276. * set up a call for the given data
  277. * - called in process context with IRQs enabled
  278. */
  279. struct rxrpc_call *rxrpc_get_client_call(struct rxrpc_sock *rx,
  280. struct rxrpc_transport *trans,
  281. struct rxrpc_conn_bundle *bundle,
  282. unsigned long user_call_ID,
  283. int create,
  284. gfp_t gfp)
  285. {
  286. struct rxrpc_call *call, *candidate;
  287. struct rb_node *p, *parent, **pp;
  288. _enter("%p,%d,%d,%lx,%d",
  289. rx, trans ? trans->debug_id : -1, bundle ? bundle->debug_id : -1,
  290. user_call_ID, create);
  291. /* search the extant calls first for one that matches the specified
  292. * user ID */
  293. read_lock(&rx->call_lock);
  294. p = rx->calls.rb_node;
  295. while (p) {
  296. call = rb_entry(p, struct rxrpc_call, sock_node);
  297. if (user_call_ID < call->user_call_ID)
  298. p = p->rb_left;
  299. else if (user_call_ID > call->user_call_ID)
  300. p = p->rb_right;
  301. else
  302. goto found_extant_call;
  303. }
  304. read_unlock(&rx->call_lock);
  305. if (!create || !trans)
  306. return ERR_PTR(-EBADSLT);
  307. /* not yet present - create a candidate for a new record and then
  308. * redo the search */
  309. candidate = rxrpc_alloc_client_call(rx, trans, bundle, gfp);
  310. if (IS_ERR(candidate)) {
  311. _leave(" = %ld", PTR_ERR(candidate));
  312. return candidate;
  313. }
  314. candidate->user_call_ID = user_call_ID;
  315. __set_bit(RXRPC_CALL_HAS_USERID, &candidate->flags);
  316. write_lock(&rx->call_lock);
  317. pp = &rx->calls.rb_node;
  318. parent = NULL;
  319. while (*pp) {
  320. parent = *pp;
  321. call = rb_entry(parent, struct rxrpc_call, sock_node);
  322. if (user_call_ID < call->user_call_ID)
  323. pp = &(*pp)->rb_left;
  324. else if (user_call_ID > call->user_call_ID)
  325. pp = &(*pp)->rb_right;
  326. else
  327. goto found_extant_second;
  328. }
  329. /* second search also failed; add the new call */
  330. call = candidate;
  331. candidate = NULL;
  332. rxrpc_get_call(call);
  333. rb_link_node(&call->sock_node, parent, pp);
  334. rb_insert_color(&call->sock_node, &rx->calls);
  335. write_unlock(&rx->call_lock);
  336. write_lock_bh(&rxrpc_call_lock);
  337. list_add_tail(&call->link, &rxrpc_calls);
  338. write_unlock_bh(&rxrpc_call_lock);
  339. _net("CALL new %d on CONN %d", call->debug_id, call->conn->debug_id);
  340. _leave(" = %p [new]", call);
  341. return call;
  342. /* we found the call in the list immediately */
  343. found_extant_call:
  344. rxrpc_get_call(call);
  345. read_unlock(&rx->call_lock);
  346. _leave(" = %p [extant %d]", call, atomic_read(&call->usage));
  347. return call;
  348. /* we found the call on the second time through the list */
  349. found_extant_second:
  350. rxrpc_get_call(call);
  351. write_unlock(&rx->call_lock);
  352. rxrpc_put_call(candidate);
  353. _leave(" = %p [second %d]", call, atomic_read(&call->usage));
  354. return call;
  355. }
  356. /*
  357. * set up an incoming call
  358. * - called in process context with IRQs enabled
  359. */
  360. struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *rx,
  361. struct rxrpc_connection *conn,
  362. struct rxrpc_host_header *hdr,
  363. gfp_t gfp)
  364. {
  365. struct rxrpc_call *call, *candidate;
  366. struct rb_node **p, *parent;
  367. u32 call_id;
  368. _enter(",%d,,%x", conn->debug_id, gfp);
  369. ASSERT(rx != NULL);
  370. candidate = rxrpc_alloc_call(gfp);
  371. if (!candidate)
  372. return ERR_PTR(-EBUSY);
  373. candidate->socket = rx;
  374. candidate->conn = conn;
  375. candidate->cid = hdr->cid;
  376. candidate->call_id = hdr->callNumber;
  377. candidate->channel = hdr->cid & RXRPC_CHANNELMASK;
  378. candidate->rx_data_post = 0;
  379. candidate->state = RXRPC_CALL_SERVER_ACCEPTING;
  380. if (conn->security_ix > 0)
  381. candidate->state = RXRPC_CALL_SERVER_SECURING;
  382. write_lock_bh(&conn->lock);
  383. /* set the channel for this call */
  384. call = conn->channels[candidate->channel];
  385. _debug("channel[%u] is %p", candidate->channel, call);
  386. if (call && call->call_id == hdr->callNumber) {
  387. /* already set; must've been a duplicate packet */
  388. _debug("extant call [%d]", call->state);
  389. ASSERTCMP(call->conn, ==, conn);
  390. read_lock(&call->state_lock);
  391. switch (call->state) {
  392. case RXRPC_CALL_LOCALLY_ABORTED:
  393. if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
  394. rxrpc_queue_call(call);
  395. case RXRPC_CALL_REMOTELY_ABORTED:
  396. read_unlock(&call->state_lock);
  397. goto aborted_call;
  398. default:
  399. rxrpc_get_call(call);
  400. read_unlock(&call->state_lock);
  401. goto extant_call;
  402. }
  403. }
  404. if (call) {
  405. /* it seems the channel is still in use from the previous call
  406. * - ditch the old binding if its call is now complete */
  407. _debug("CALL: %u { %s }",
  408. call->debug_id, rxrpc_call_states[call->state]);
  409. if (call->state >= RXRPC_CALL_COMPLETE) {
  410. conn->channels[call->channel] = NULL;
  411. } else {
  412. write_unlock_bh(&conn->lock);
  413. kmem_cache_free(rxrpc_call_jar, candidate);
  414. _leave(" = -EBUSY");
  415. return ERR_PTR(-EBUSY);
  416. }
  417. }
  418. /* check the call number isn't duplicate */
  419. _debug("check dup");
  420. call_id = hdr->callNumber;
  421. p = &conn->calls.rb_node;
  422. parent = NULL;
  423. while (*p) {
  424. parent = *p;
  425. call = rb_entry(parent, struct rxrpc_call, conn_node);
  426. /* The tree is sorted in order of the __be32 value without
  427. * turning it into host order.
  428. */
  429. if (call_id < call->call_id)
  430. p = &(*p)->rb_left;
  431. else if (call_id > call->call_id)
  432. p = &(*p)->rb_right;
  433. else
  434. goto old_call;
  435. }
  436. /* make the call available */
  437. _debug("new call");
  438. call = candidate;
  439. candidate = NULL;
  440. rb_link_node(&call->conn_node, parent, p);
  441. rb_insert_color(&call->conn_node, &conn->calls);
  442. conn->channels[call->channel] = call;
  443. sock_hold(&rx->sk);
  444. atomic_inc(&conn->usage);
  445. write_unlock_bh(&conn->lock);
  446. spin_lock(&conn->trans->peer->lock);
  447. list_add(&call->error_link, &conn->trans->peer->error_targets);
  448. spin_unlock(&conn->trans->peer->lock);
  449. write_lock_bh(&rxrpc_call_lock);
  450. list_add_tail(&call->link, &rxrpc_calls);
  451. write_unlock_bh(&rxrpc_call_lock);
  452. /* Record copies of information for hashtable lookup */
  453. call->proto = rx->proto;
  454. call->local = conn->trans->local;
  455. switch (call->proto) {
  456. case AF_INET:
  457. call->peer_ip.ipv4_addr =
  458. conn->trans->peer->srx.transport.sin.sin_addr.s_addr;
  459. break;
  460. case AF_INET6:
  461. memcpy(call->peer_ip.ipv6_addr,
  462. conn->trans->peer->srx.transport.sin6.sin6_addr.in6_u.u6_addr8,
  463. sizeof(call->peer_ip.ipv6_addr));
  464. break;
  465. default:
  466. break;
  467. }
  468. call->epoch = conn->epoch;
  469. call->service_id = conn->service_id;
  470. call->in_clientflag = conn->in_clientflag;
  471. /* Add the new call to the hashtable */
  472. rxrpc_call_hash_add(call);
  473. _net("CALL incoming %d on CONN %d", call->debug_id, call->conn->debug_id);
  474. call->lifetimer.expires = jiffies + rxrpc_max_call_lifetime;
  475. add_timer(&call->lifetimer);
  476. _leave(" = %p {%d} [new]", call, call->debug_id);
  477. return call;
  478. extant_call:
  479. write_unlock_bh(&conn->lock);
  480. kmem_cache_free(rxrpc_call_jar, candidate);
  481. _leave(" = %p {%d} [extant]", call, call ? call->debug_id : -1);
  482. return call;
  483. aborted_call:
  484. write_unlock_bh(&conn->lock);
  485. kmem_cache_free(rxrpc_call_jar, candidate);
  486. _leave(" = -ECONNABORTED");
  487. return ERR_PTR(-ECONNABORTED);
  488. old_call:
  489. write_unlock_bh(&conn->lock);
  490. kmem_cache_free(rxrpc_call_jar, candidate);
  491. _leave(" = -ECONNRESET [old]");
  492. return ERR_PTR(-ECONNRESET);
  493. }
  494. /*
  495. * find an extant server call
  496. * - called in process context with IRQs enabled
  497. */
  498. struct rxrpc_call *rxrpc_find_server_call(struct rxrpc_sock *rx,
  499. unsigned long user_call_ID)
  500. {
  501. struct rxrpc_call *call;
  502. struct rb_node *p;
  503. _enter("%p,%lx", rx, user_call_ID);
  504. /* search the extant calls for one that matches the specified user
  505. * ID */
  506. read_lock(&rx->call_lock);
  507. p = rx->calls.rb_node;
  508. while (p) {
  509. call = rb_entry(p, struct rxrpc_call, sock_node);
  510. if (user_call_ID < call->user_call_ID)
  511. p = p->rb_left;
  512. else if (user_call_ID > call->user_call_ID)
  513. p = p->rb_right;
  514. else
  515. goto found_extant_call;
  516. }
  517. read_unlock(&rx->call_lock);
  518. _leave(" = NULL");
  519. return NULL;
  520. /* we found the call in the list immediately */
  521. found_extant_call:
  522. rxrpc_get_call(call);
  523. read_unlock(&rx->call_lock);
  524. _leave(" = %p [%d]", call, atomic_read(&call->usage));
  525. return call;
  526. }
  527. /*
  528. * detach a call from a socket and set up for release
  529. */
  530. void rxrpc_release_call(struct rxrpc_call *call)
  531. {
  532. struct rxrpc_connection *conn = call->conn;
  533. struct rxrpc_sock *rx = call->socket;
  534. _enter("{%d,%d,%d,%d}",
  535. call->debug_id, atomic_read(&call->usage),
  536. atomic_read(&call->ackr_not_idle),
  537. call->rx_first_oos);
  538. spin_lock_bh(&call->lock);
  539. if (test_and_set_bit(RXRPC_CALL_RELEASED, &call->flags))
  540. BUG();
  541. spin_unlock_bh(&call->lock);
  542. /* dissociate from the socket
  543. * - the socket's ref on the call is passed to the death timer
  544. */
  545. _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
  546. write_lock_bh(&rx->call_lock);
  547. if (!list_empty(&call->accept_link)) {
  548. _debug("unlinking once-pending call %p { e=%lx f=%lx }",
  549. call, call->events, call->flags);
  550. ASSERT(!test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
  551. list_del_init(&call->accept_link);
  552. sk_acceptq_removed(&rx->sk);
  553. } else if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
  554. rb_erase(&call->sock_node, &rx->calls);
  555. memset(&call->sock_node, 0xdd, sizeof(call->sock_node));
  556. clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
  557. }
  558. write_unlock_bh(&rx->call_lock);
  559. /* free up the channel for reuse */
  560. spin_lock(&conn->trans->client_lock);
  561. write_lock_bh(&conn->lock);
  562. write_lock(&call->state_lock);
  563. if (conn->channels[call->channel] == call)
  564. conn->channels[call->channel] = NULL;
  565. if (conn->out_clientflag && conn->bundle) {
  566. conn->avail_calls++;
  567. switch (conn->avail_calls) {
  568. case 1:
  569. list_move_tail(&conn->bundle_link,
  570. &conn->bundle->avail_conns);
  571. case 2 ... RXRPC_MAXCALLS - 1:
  572. ASSERT(conn->channels[0] == NULL ||
  573. conn->channels[1] == NULL ||
  574. conn->channels[2] == NULL ||
  575. conn->channels[3] == NULL);
  576. break;
  577. case RXRPC_MAXCALLS:
  578. list_move_tail(&conn->bundle_link,
  579. &conn->bundle->unused_conns);
  580. ASSERT(conn->channels[0] == NULL &&
  581. conn->channels[1] == NULL &&
  582. conn->channels[2] == NULL &&
  583. conn->channels[3] == NULL);
  584. break;
  585. default:
  586. printk(KERN_ERR "RxRPC: conn->avail_calls=%d\n",
  587. conn->avail_calls);
  588. BUG();
  589. }
  590. }
  591. spin_unlock(&conn->trans->client_lock);
  592. if (call->state < RXRPC_CALL_COMPLETE &&
  593. call->state != RXRPC_CALL_CLIENT_FINAL_ACK) {
  594. _debug("+++ ABORTING STATE %d +++\n", call->state);
  595. call->state = RXRPC_CALL_LOCALLY_ABORTED;
  596. call->abort_code = RX_CALL_DEAD;
  597. set_bit(RXRPC_CALL_EV_ABORT, &call->events);
  598. rxrpc_queue_call(call);
  599. }
  600. write_unlock(&call->state_lock);
  601. write_unlock_bh(&conn->lock);
  602. /* clean up the Rx queue */
  603. if (!skb_queue_empty(&call->rx_queue) ||
  604. !skb_queue_empty(&call->rx_oos_queue)) {
  605. struct rxrpc_skb_priv *sp;
  606. struct sk_buff *skb;
  607. _debug("purge Rx queues");
  608. spin_lock_bh(&call->lock);
  609. while ((skb = skb_dequeue(&call->rx_queue)) ||
  610. (skb = skb_dequeue(&call->rx_oos_queue))) {
  611. sp = rxrpc_skb(skb);
  612. if (sp->call) {
  613. ASSERTCMP(sp->call, ==, call);
  614. rxrpc_put_call(call);
  615. sp->call = NULL;
  616. }
  617. skb->destructor = NULL;
  618. spin_unlock_bh(&call->lock);
  619. _debug("- zap %s %%%u #%u",
  620. rxrpc_pkts[sp->hdr.type],
  621. sp->hdr.serial, sp->hdr.seq);
  622. rxrpc_free_skb(skb);
  623. spin_lock_bh(&call->lock);
  624. }
  625. spin_unlock_bh(&call->lock);
  626. ASSERTCMP(call->state, !=, RXRPC_CALL_COMPLETE);
  627. }
  628. del_timer_sync(&call->resend_timer);
  629. del_timer_sync(&call->ack_timer);
  630. del_timer_sync(&call->lifetimer);
  631. call->deadspan.expires = jiffies + rxrpc_dead_call_expiry;
  632. add_timer(&call->deadspan);
  633. _leave("");
  634. }
  635. /*
  636. * handle a dead call being ready for reaping
  637. */
  638. static void rxrpc_dead_call_expired(unsigned long _call)
  639. {
  640. struct rxrpc_call *call = (struct rxrpc_call *) _call;
  641. _enter("{%d}", call->debug_id);
  642. write_lock_bh(&call->state_lock);
  643. call->state = RXRPC_CALL_DEAD;
  644. write_unlock_bh(&call->state_lock);
  645. rxrpc_put_call(call);
  646. }
  647. /*
  648. * mark a call as to be released, aborting it if it's still in progress
  649. * - called with softirqs disabled
  650. */
  651. static void rxrpc_mark_call_released(struct rxrpc_call *call)
  652. {
  653. bool sched;
  654. write_lock(&call->state_lock);
  655. if (call->state < RXRPC_CALL_DEAD) {
  656. sched = false;
  657. if (call->state < RXRPC_CALL_COMPLETE) {
  658. _debug("abort call %p", call);
  659. call->state = RXRPC_CALL_LOCALLY_ABORTED;
  660. call->abort_code = RX_CALL_DEAD;
  661. if (!test_and_set_bit(RXRPC_CALL_EV_ABORT, &call->events))
  662. sched = true;
  663. }
  664. if (!test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
  665. sched = true;
  666. if (sched)
  667. rxrpc_queue_call(call);
  668. }
  669. write_unlock(&call->state_lock);
  670. }
  671. /*
  672. * release all the calls associated with a socket
  673. */
  674. void rxrpc_release_calls_on_socket(struct rxrpc_sock *rx)
  675. {
  676. struct rxrpc_call *call;
  677. struct rb_node *p;
  678. _enter("%p", rx);
  679. read_lock_bh(&rx->call_lock);
  680. /* mark all the calls as no longer wanting incoming packets */
  681. for (p = rb_first(&rx->calls); p; p = rb_next(p)) {
  682. call = rb_entry(p, struct rxrpc_call, sock_node);
  683. rxrpc_mark_call_released(call);
  684. }
  685. /* kill the not-yet-accepted incoming calls */
  686. list_for_each_entry(call, &rx->secureq, accept_link) {
  687. rxrpc_mark_call_released(call);
  688. }
  689. list_for_each_entry(call, &rx->acceptq, accept_link) {
  690. rxrpc_mark_call_released(call);
  691. }
  692. read_unlock_bh(&rx->call_lock);
  693. _leave("");
  694. }
  695. /*
  696. * release a call
  697. */
  698. void __rxrpc_put_call(struct rxrpc_call *call)
  699. {
  700. ASSERT(call != NULL);
  701. _enter("%p{u=%d}", call, atomic_read(&call->usage));
  702. ASSERTCMP(atomic_read(&call->usage), >, 0);
  703. if (atomic_dec_and_test(&call->usage)) {
  704. _debug("call %d dead", call->debug_id);
  705. ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
  706. rxrpc_queue_work(&call->destroyer);
  707. }
  708. _leave("");
  709. }
  710. /*
  711. * clean up a call
  712. */
  713. static void rxrpc_cleanup_call(struct rxrpc_call *call)
  714. {
  715. _net("DESTROY CALL %d", call->debug_id);
  716. ASSERT(call->socket);
  717. memset(&call->sock_node, 0xcd, sizeof(call->sock_node));
  718. del_timer_sync(&call->lifetimer);
  719. del_timer_sync(&call->deadspan);
  720. del_timer_sync(&call->ack_timer);
  721. del_timer_sync(&call->resend_timer);
  722. ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
  723. ASSERTCMP(call->events, ==, 0);
  724. if (work_pending(&call->processor)) {
  725. _debug("defer destroy");
  726. rxrpc_queue_work(&call->destroyer);
  727. return;
  728. }
  729. if (call->conn) {
  730. spin_lock(&call->conn->trans->peer->lock);
  731. list_del(&call->error_link);
  732. spin_unlock(&call->conn->trans->peer->lock);
  733. write_lock_bh(&call->conn->lock);
  734. rb_erase(&call->conn_node, &call->conn->calls);
  735. write_unlock_bh(&call->conn->lock);
  736. rxrpc_put_connection(call->conn);
  737. }
  738. /* Remove the call from the hash */
  739. rxrpc_call_hash_del(call);
  740. if (call->acks_window) {
  741. _debug("kill Tx window %d",
  742. CIRC_CNT(call->acks_head, call->acks_tail,
  743. call->acks_winsz));
  744. smp_mb();
  745. while (CIRC_CNT(call->acks_head, call->acks_tail,
  746. call->acks_winsz) > 0) {
  747. struct rxrpc_skb_priv *sp;
  748. unsigned long _skb;
  749. _skb = call->acks_window[call->acks_tail] & ~1;
  750. sp = rxrpc_skb((struct sk_buff *)_skb);
  751. _debug("+++ clear Tx %u", sp->hdr.seq);
  752. rxrpc_free_skb((struct sk_buff *)_skb);
  753. call->acks_tail =
  754. (call->acks_tail + 1) & (call->acks_winsz - 1);
  755. }
  756. kfree(call->acks_window);
  757. }
  758. rxrpc_free_skb(call->tx_pending);
  759. rxrpc_purge_queue(&call->rx_queue);
  760. ASSERT(skb_queue_empty(&call->rx_oos_queue));
  761. sock_put(&call->socket->sk);
  762. kmem_cache_free(rxrpc_call_jar, call);
  763. }
  764. /*
  765. * destroy a call
  766. */
  767. static void rxrpc_destroy_call(struct work_struct *work)
  768. {
  769. struct rxrpc_call *call =
  770. container_of(work, struct rxrpc_call, destroyer);
  771. _enter("%p{%d,%d,%p}",
  772. call, atomic_read(&call->usage), call->channel, call->conn);
  773. ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
  774. write_lock_bh(&rxrpc_call_lock);
  775. list_del_init(&call->link);
  776. write_unlock_bh(&rxrpc_call_lock);
  777. rxrpc_cleanup_call(call);
  778. _leave("");
  779. }
  780. /*
  781. * preemptively destroy all the call records from a transport endpoint rather
  782. * than waiting for them to time out
  783. */
  784. void __exit rxrpc_destroy_all_calls(void)
  785. {
  786. struct rxrpc_call *call;
  787. _enter("");
  788. write_lock_bh(&rxrpc_call_lock);
  789. while (!list_empty(&rxrpc_calls)) {
  790. call = list_entry(rxrpc_calls.next, struct rxrpc_call, link);
  791. _debug("Zapping call %p", call);
  792. list_del_init(&call->link);
  793. switch (atomic_read(&call->usage)) {
  794. case 0:
  795. ASSERTCMP(call->state, ==, RXRPC_CALL_DEAD);
  796. break;
  797. case 1:
  798. if (del_timer_sync(&call->deadspan) != 0 &&
  799. call->state != RXRPC_CALL_DEAD)
  800. rxrpc_dead_call_expired((unsigned long) call);
  801. if (call->state != RXRPC_CALL_DEAD)
  802. break;
  803. default:
  804. printk(KERN_ERR "RXRPC:"
  805. " Call %p still in use (%d,%d,%s,%lx,%lx)!\n",
  806. call, atomic_read(&call->usage),
  807. atomic_read(&call->ackr_not_idle),
  808. rxrpc_call_states[call->state],
  809. call->flags, call->events);
  810. if (!skb_queue_empty(&call->rx_queue))
  811. printk(KERN_ERR"RXRPC: Rx queue occupied\n");
  812. if (!skb_queue_empty(&call->rx_oos_queue))
  813. printk(KERN_ERR"RXRPC: OOS queue occupied\n");
  814. break;
  815. }
  816. write_unlock_bh(&rxrpc_call_lock);
  817. cond_resched();
  818. write_lock_bh(&rxrpc_call_lock);
  819. }
  820. write_unlock_bh(&rxrpc_call_lock);
  821. _leave("");
  822. }
  823. /*
  824. * handle call lifetime being exceeded
  825. */
  826. static void rxrpc_call_life_expired(unsigned long _call)
  827. {
  828. struct rxrpc_call *call = (struct rxrpc_call *) _call;
  829. if (call->state >= RXRPC_CALL_COMPLETE)
  830. return;
  831. _enter("{%d}", call->debug_id);
  832. read_lock_bh(&call->state_lock);
  833. if (call->state < RXRPC_CALL_COMPLETE) {
  834. set_bit(RXRPC_CALL_EV_LIFE_TIMER, &call->events);
  835. rxrpc_queue_call(call);
  836. }
  837. read_unlock_bh(&call->state_lock);
  838. }
  839. /*
  840. * handle resend timer expiry
  841. * - may not take call->state_lock as this can deadlock against del_timer_sync()
  842. */
  843. static void rxrpc_resend_time_expired(unsigned long _call)
  844. {
  845. struct rxrpc_call *call = (struct rxrpc_call *) _call;
  846. _enter("{%d}", call->debug_id);
  847. if (call->state >= RXRPC_CALL_COMPLETE)
  848. return;
  849. clear_bit(RXRPC_CALL_RUN_RTIMER, &call->flags);
  850. if (!test_and_set_bit(RXRPC_CALL_EV_RESEND_TIMER, &call->events))
  851. rxrpc_queue_call(call);
  852. }
  853. /*
  854. * handle ACK timer expiry
  855. */
  856. static void rxrpc_ack_time_expired(unsigned long _call)
  857. {
  858. struct rxrpc_call *call = (struct rxrpc_call *) _call;
  859. _enter("{%d}", call->debug_id);
  860. if (call->state >= RXRPC_CALL_COMPLETE)
  861. return;
  862. read_lock_bh(&call->state_lock);
  863. if (call->state < RXRPC_CALL_COMPLETE &&
  864. !test_and_set_bit(RXRPC_CALL_EV_ACK, &call->events))
  865. rxrpc_queue_call(call);
  866. read_unlock_bh(&call->state_lock);
  867. }