ar-transport.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. usage = atomic_read(&trans->usage);
  100. rxrpc_get_local(trans->local);
  101. atomic_inc(&trans->peer->usage);
  102. list_add_tail(&trans->link, &rxrpc_transports);
  103. write_unlock_bh(&rxrpc_transport_lock);
  104. new = "new";
  105. success:
  106. _net("TRANSPORT %s %d local %d -> peer %d",
  107. new,
  108. trans->debug_id,
  109. trans->local->debug_id,
  110. trans->peer->debug_id);
  111. _leave(" = %p {u=%d}", trans, usage);
  112. return trans;
  113. /* we found the transport in the list immediately */
  114. found_extant_transport:
  115. usage = atomic_inc_return(&trans->usage);
  116. read_unlock_bh(&rxrpc_transport_lock);
  117. goto success;
  118. /* we found the transport on the second time through the list */
  119. found_extant_second:
  120. usage = atomic_inc_return(&trans->usage);
  121. write_unlock_bh(&rxrpc_transport_lock);
  122. kfree(candidate);
  123. goto success;
  124. }
  125. /*
  126. * find the transport connecting two endpoints
  127. */
  128. struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *local,
  129. struct rxrpc_peer *peer)
  130. {
  131. struct rxrpc_transport *trans;
  132. _enter("{%pI4+%hu},{%pI4+%hu},",
  133. &local->srx.transport.sin.sin_addr,
  134. ntohs(local->srx.transport.sin.sin_port),
  135. &peer->srx.transport.sin.sin_addr,
  136. ntohs(peer->srx.transport.sin.sin_port));
  137. /* search the transport list */
  138. read_lock_bh(&rxrpc_transport_lock);
  139. list_for_each_entry(trans, &rxrpc_transports, link) {
  140. if (trans->local == local && trans->peer == peer)
  141. goto found_extant_transport;
  142. }
  143. read_unlock_bh(&rxrpc_transport_lock);
  144. _leave(" = NULL");
  145. return NULL;
  146. found_extant_transport:
  147. atomic_inc(&trans->usage);
  148. read_unlock_bh(&rxrpc_transport_lock);
  149. _leave(" = %p", trans);
  150. return trans;
  151. }
  152. /*
  153. * release a transport session
  154. */
  155. void rxrpc_put_transport(struct rxrpc_transport *trans)
  156. {
  157. _enter("%p{u=%d}", trans, atomic_read(&trans->usage));
  158. ASSERTCMP(atomic_read(&trans->usage), >, 0);
  159. trans->put_time = get_seconds();
  160. if (unlikely(atomic_dec_and_test(&trans->usage))) {
  161. _debug("zombie");
  162. /* let the reaper determine the timeout to avoid a race with
  163. * overextending the timeout if the reaper is running at the
  164. * same time */
  165. rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
  166. }
  167. _leave("");
  168. }
  169. /*
  170. * clean up a transport session
  171. */
  172. static void rxrpc_cleanup_transport(struct rxrpc_transport *trans)
  173. {
  174. _net("DESTROY TRANS %d", trans->debug_id);
  175. rxrpc_purge_queue(&trans->error_queue);
  176. rxrpc_put_local(trans->local);
  177. rxrpc_put_peer(trans->peer);
  178. kfree(trans);
  179. }
  180. /*
  181. * reap dead transports that have passed their expiry date
  182. */
  183. static void rxrpc_transport_reaper(struct work_struct *work)
  184. {
  185. struct rxrpc_transport *trans, *_p;
  186. unsigned long now, earliest, reap_time;
  187. LIST_HEAD(graveyard);
  188. _enter("");
  189. now = get_seconds();
  190. earliest = ULONG_MAX;
  191. /* extract all the transports that have been dead too long */
  192. write_lock_bh(&rxrpc_transport_lock);
  193. list_for_each_entry_safe(trans, _p, &rxrpc_transports, link) {
  194. _debug("reap TRANS %d { u=%d t=%ld }",
  195. trans->debug_id, atomic_read(&trans->usage),
  196. (long) now - (long) trans->put_time);
  197. if (likely(atomic_read(&trans->usage) > 0))
  198. continue;
  199. reap_time = trans->put_time + rxrpc_transport_timeout;
  200. if (reap_time <= now)
  201. list_move_tail(&trans->link, &graveyard);
  202. else if (reap_time < earliest)
  203. earliest = reap_time;
  204. }
  205. write_unlock_bh(&rxrpc_transport_lock);
  206. if (earliest != ULONG_MAX) {
  207. _debug("reschedule reaper %ld", (long) earliest - now);
  208. ASSERTCMP(earliest, >, now);
  209. rxrpc_queue_delayed_work(&rxrpc_transport_reap,
  210. (earliest - now) * HZ);
  211. }
  212. /* then destroy all those pulled out */
  213. while (!list_empty(&graveyard)) {
  214. trans = list_entry(graveyard.next, struct rxrpc_transport,
  215. link);
  216. list_del_init(&trans->link);
  217. ASSERTCMP(atomic_read(&trans->usage), ==, 0);
  218. rxrpc_cleanup_transport(trans);
  219. }
  220. _leave("");
  221. }
  222. /*
  223. * preemptively destroy all the transport session records rather than waiting
  224. * for them to time out
  225. */
  226. void __exit rxrpc_destroy_all_transports(void)
  227. {
  228. _enter("");
  229. rxrpc_transport_timeout = 0;
  230. cancel_delayed_work(&rxrpc_transport_reap);
  231. rxrpc_queue_delayed_work(&rxrpc_transport_reap, 0);
  232. _leave("");
  233. }