ar-transport.c 7.1 KB

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