ar-transport.c 7.1 KB

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