ar-transport.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* RxRPC point-to-point transport session management
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/net.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/slab.h>
  15. #include <net/sock.h>
  16. #include <net/af_rxrpc.h>
  17. #include "ar-internal.h"
  18. /*
  19. * Time after last use at which transport record is cleaned up.
  20. */
  21. unsigned rxrpc_transport_expiry = 3600 * 24;
  22. static void rxrpc_transport_reaper(struct work_struct *work);
  23. static LIST_HEAD(rxrpc_transports);
  24. static DEFINE_RWLOCK(rxrpc_transport_lock);
  25. static DECLARE_DELAYED_WORK(rxrpc_transport_reap, rxrpc_transport_reaper);
  26. /*
  27. * allocate a new transport session manager
  28. */
  29. static struct rxrpc_transport *rxrpc_alloc_transport(struct rxrpc_local *local,
  30. struct rxrpc_peer *peer,
  31. gfp_t gfp)
  32. {
  33. struct rxrpc_transport *trans;
  34. _enter("");
  35. trans = kzalloc(sizeof(struct rxrpc_transport), gfp);
  36. if (trans) {
  37. trans->local = local;
  38. trans->peer = peer;
  39. INIT_LIST_HEAD(&trans->link);
  40. trans->bundles = RB_ROOT;
  41. trans->client_conns = RB_ROOT;
  42. trans->server_conns = RB_ROOT;
  43. skb_queue_head_init(&trans->error_queue);
  44. spin_lock_init(&trans->client_lock);
  45. rwlock_init(&trans->conn_lock);
  46. atomic_set(&trans->usage, 1);
  47. trans->debug_id = atomic_inc_return(&rxrpc_debug_id);
  48. if (peer->srx.transport.family == AF_INET) {
  49. switch (peer->srx.transport_type) {
  50. case SOCK_DGRAM:
  51. INIT_WORK(&trans->error_handler,
  52. rxrpc_UDP_error_handler);
  53. break;
  54. default:
  55. BUG();
  56. break;
  57. }
  58. } else {
  59. BUG();
  60. }
  61. }
  62. _leave(" = %p", trans);
  63. return trans;
  64. }
  65. /*
  66. * obtain a transport session for the nominated endpoints
  67. */
  68. struct rxrpc_transport *rxrpc_get_transport(struct rxrpc_local *local,
  69. struct rxrpc_peer *peer,
  70. gfp_t gfp)
  71. {
  72. struct rxrpc_transport *trans, *candidate;
  73. const char *new = "old";
  74. int usage;
  75. _enter("{%pI4+%hu},{%pI4+%hu},",
  76. &local->srx.transport.sin.sin_addr,
  77. ntohs(local->srx.transport.sin.sin_port),
  78. &peer->srx.transport.sin.sin_addr,
  79. ntohs(peer->srx.transport.sin.sin_port));
  80. /* search the transport list first */
  81. read_lock_bh(&rxrpc_transport_lock);
  82. list_for_each_entry(trans, &rxrpc_transports, link) {
  83. if (trans->local == local && trans->peer == peer)
  84. goto found_extant_transport;
  85. }
  86. read_unlock_bh(&rxrpc_transport_lock);
  87. /* not yet present - create a candidate for a new record and then
  88. * redo the search */
  89. candidate = rxrpc_alloc_transport(local, peer, gfp);
  90. if (!candidate) {
  91. _leave(" = -ENOMEM");
  92. return ERR_PTR(-ENOMEM);
  93. }
  94. write_lock_bh(&rxrpc_transport_lock);
  95. list_for_each_entry(trans, &rxrpc_transports, link) {
  96. if (trans->local == local && trans->peer == peer)
  97. goto found_extant_second;
  98. }
  99. /* we can now add the new candidate to the list */
  100. trans = candidate;
  101. candidate = NULL;
  102. usage = atomic_read(&trans->usage);
  103. rxrpc_get_local(trans->local);
  104. atomic_inc(&trans->peer->usage);
  105. list_add_tail(&trans->link, &rxrpc_transports);
  106. write_unlock_bh(&rxrpc_transport_lock);
  107. new = "new";
  108. success:
  109. _net("TRANSPORT %s %d local %d -> peer %d",
  110. new,
  111. trans->debug_id,
  112. trans->local->debug_id,
  113. trans->peer->debug_id);
  114. _leave(" = %p {u=%d}", trans, usage);
  115. return trans;
  116. /* we found the transport in the list immediately */
  117. found_extant_transport:
  118. usage = atomic_inc_return(&trans->usage);
  119. read_unlock_bh(&rxrpc_transport_lock);
  120. goto success;
  121. /* we found the transport on the second time through the list */
  122. found_extant_second:
  123. usage = atomic_inc_return(&trans->usage);
  124. write_unlock_bh(&rxrpc_transport_lock);
  125. kfree(candidate);
  126. goto success;
  127. }
  128. /*
  129. * find the transport connecting two endpoints
  130. */
  131. struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *local,
  132. struct rxrpc_peer *peer)
  133. {
  134. struct rxrpc_transport *trans;
  135. _enter("{%pI4+%hu},{%pI4+%hu},",
  136. &local->srx.transport.sin.sin_addr,
  137. ntohs(local->srx.transport.sin.sin_port),
  138. &peer->srx.transport.sin.sin_addr,
  139. ntohs(peer->srx.transport.sin.sin_port));
  140. /* search the transport list */
  141. read_lock_bh(&rxrpc_transport_lock);
  142. list_for_each_entry(trans, &rxrpc_transports, link) {
  143. if (trans->local == local && trans->peer == peer)
  144. goto found_extant_transport;
  145. }
  146. read_unlock_bh(&rxrpc_transport_lock);
  147. _leave(" = NULL");
  148. return NULL;
  149. found_extant_transport:
  150. atomic_inc(&trans->usage);
  151. read_unlock_bh(&rxrpc_transport_lock);
  152. _leave(" = %p", trans);
  153. return trans;
  154. }
  155. /*
  156. * release a transport session
  157. */
  158. void rxrpc_put_transport(struct rxrpc_transport *trans)
  159. {
  160. _enter("%p{u=%d}", trans, atomic_read(&trans->usage));
  161. ASSERTCMP(atomic_read(&trans->usage), >, 0);
  162. trans->put_time = ktime_get_seconds();
  163. if (unlikely(atomic_dec_and_test(&trans->usage))) {
  164. _debug("zombie");
  165. /* let the reaper determine the timeout to avoid a race with
  166. * overextending the timeout if the reaper is running at the
  167. * same time */
  168. rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
  169. }
  170. _leave("");
  171. }
  172. /*
  173. * clean up a transport session
  174. */
  175. static void rxrpc_cleanup_transport(struct rxrpc_transport *trans)
  176. {
  177. _net("DESTROY TRANS %d", trans->debug_id);
  178. rxrpc_purge_queue(&trans->error_queue);
  179. rxrpc_put_local(trans->local);
  180. rxrpc_put_peer(trans->peer);
  181. kfree(trans);
  182. }
  183. /*
  184. * reap dead transports that have passed their expiry date
  185. */
  186. static void rxrpc_transport_reaper(struct work_struct *work)
  187. {
  188. struct rxrpc_transport *trans, *_p;
  189. unsigned long now, earliest, reap_time;
  190. LIST_HEAD(graveyard);
  191. _enter("");
  192. now = ktime_get_seconds();
  193. earliest = ULONG_MAX;
  194. /* extract all the transports that have been dead too long */
  195. write_lock_bh(&rxrpc_transport_lock);
  196. list_for_each_entry_safe(trans, _p, &rxrpc_transports, link) {
  197. _debug("reap TRANS %d { u=%d t=%ld }",
  198. trans->debug_id, atomic_read(&trans->usage),
  199. (long) now - (long) trans->put_time);
  200. if (likely(atomic_read(&trans->usage) > 0))
  201. continue;
  202. reap_time = trans->put_time + rxrpc_transport_expiry;
  203. if (reap_time <= now)
  204. list_move_tail(&trans->link, &graveyard);
  205. else if (reap_time < earliest)
  206. earliest = reap_time;
  207. }
  208. write_unlock_bh(&rxrpc_transport_lock);
  209. if (earliest != ULONG_MAX) {
  210. _debug("reschedule reaper %ld", (long) earliest - now);
  211. ASSERTCMP(earliest, >, now);
  212. rxrpc_queue_delayed_work(&rxrpc_transport_reap,
  213. (earliest - now) * HZ);
  214. }
  215. /* then destroy all those pulled out */
  216. while (!list_empty(&graveyard)) {
  217. trans = list_entry(graveyard.next, struct rxrpc_transport,
  218. link);
  219. list_del_init(&trans->link);
  220. ASSERTCMP(atomic_read(&trans->usage), ==, 0);
  221. rxrpc_cleanup_transport(trans);
  222. }
  223. _leave("");
  224. }
  225. /*
  226. * preemptively destroy all the transport session records rather than waiting
  227. * for them to time out
  228. */
  229. void __exit rxrpc_destroy_all_transports(void)
  230. {
  231. _enter("");
  232. rxrpc_transport_expiry = 0;
  233. cancel_delayed_work(&rxrpc_transport_reap);
  234. rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
  235. _leave("");
  236. }