peer.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* peer.c: Rx RPC peer management
  2. *
  3. * Copyright (C) 2002 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/sched.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <rxrpc/rxrpc.h>
  15. #include <rxrpc/transport.h>
  16. #include <rxrpc/peer.h>
  17. #include <rxrpc/connection.h>
  18. #include <rxrpc/call.h>
  19. #include <rxrpc/message.h>
  20. #include <linux/udp.h>
  21. #include <linux/ip.h>
  22. #include <net/sock.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/div64.h>
  25. #include "internal.h"
  26. __RXACCT_DECL(atomic_t rxrpc_peer_count);
  27. LIST_HEAD(rxrpc_peers);
  28. DECLARE_RWSEM(rxrpc_peers_sem);
  29. unsigned long rxrpc_peer_timeout = 12 * 60 * 60;
  30. static void rxrpc_peer_do_timeout(struct rxrpc_peer *peer);
  31. static void __rxrpc_peer_timeout(rxrpc_timer_t *timer)
  32. {
  33. struct rxrpc_peer *peer =
  34. list_entry(timer, struct rxrpc_peer, timeout);
  35. _debug("Rx PEER TIMEOUT [%p{u=%d}]", peer, atomic_read(&peer->usage));
  36. rxrpc_peer_do_timeout(peer);
  37. }
  38. static const struct rxrpc_timer_ops rxrpc_peer_timer_ops = {
  39. .timed_out = __rxrpc_peer_timeout,
  40. };
  41. /*****************************************************************************/
  42. /*
  43. * create a peer record
  44. */
  45. static int __rxrpc_create_peer(struct rxrpc_transport *trans, __be32 addr,
  46. struct rxrpc_peer **_peer)
  47. {
  48. struct rxrpc_peer *peer;
  49. _enter("%p,%08x", trans, ntohl(addr));
  50. /* allocate and initialise a peer record */
  51. peer = kzalloc(sizeof(struct rxrpc_peer), GFP_KERNEL);
  52. if (!peer) {
  53. _leave(" = -ENOMEM");
  54. return -ENOMEM;
  55. }
  56. atomic_set(&peer->usage, 1);
  57. INIT_LIST_HEAD(&peer->link);
  58. INIT_LIST_HEAD(&peer->proc_link);
  59. INIT_LIST_HEAD(&peer->conn_idlist);
  60. INIT_LIST_HEAD(&peer->conn_active);
  61. INIT_LIST_HEAD(&peer->conn_graveyard);
  62. spin_lock_init(&peer->conn_gylock);
  63. init_waitqueue_head(&peer->conn_gy_waitq);
  64. rwlock_init(&peer->conn_idlock);
  65. rwlock_init(&peer->conn_lock);
  66. atomic_set(&peer->conn_count, 0);
  67. spin_lock_init(&peer->lock);
  68. rxrpc_timer_init(&peer->timeout, &rxrpc_peer_timer_ops);
  69. peer->addr.s_addr = addr;
  70. peer->trans = trans;
  71. peer->ops = trans->peer_ops;
  72. __RXACCT(atomic_inc(&rxrpc_peer_count));
  73. *_peer = peer;
  74. _leave(" = 0 (%p)", peer);
  75. return 0;
  76. } /* end __rxrpc_create_peer() */
  77. /*****************************************************************************/
  78. /*
  79. * find a peer record on the specified transport
  80. * - returns (if successful) with peer record usage incremented
  81. * - resurrects it from the graveyard if found there
  82. */
  83. int rxrpc_peer_lookup(struct rxrpc_transport *trans, __be32 addr,
  84. struct rxrpc_peer **_peer)
  85. {
  86. struct rxrpc_peer *peer, *candidate = NULL;
  87. struct list_head *_p;
  88. int ret;
  89. _enter("%p{%hu},%08x", trans, trans->port, ntohl(addr));
  90. /* [common case] search the transport's active list first */
  91. read_lock(&trans->peer_lock);
  92. list_for_each(_p, &trans->peer_active) {
  93. peer = list_entry(_p, struct rxrpc_peer, link);
  94. if (peer->addr.s_addr == addr)
  95. goto found_active;
  96. }
  97. read_unlock(&trans->peer_lock);
  98. /* [uncommon case] not active - create a candidate for a new record */
  99. ret = __rxrpc_create_peer(trans, addr, &candidate);
  100. if (ret < 0) {
  101. _leave(" = %d", ret);
  102. return ret;
  103. }
  104. /* search the active list again, just in case it appeared whilst we
  105. * were busy */
  106. write_lock(&trans->peer_lock);
  107. list_for_each(_p, &trans->peer_active) {
  108. peer = list_entry(_p, struct rxrpc_peer, link);
  109. if (peer->addr.s_addr == addr)
  110. goto found_active_second_chance;
  111. }
  112. /* search the transport's graveyard list */
  113. spin_lock(&trans->peer_gylock);
  114. list_for_each(_p, &trans->peer_graveyard) {
  115. peer = list_entry(_p, struct rxrpc_peer, link);
  116. if (peer->addr.s_addr == addr)
  117. goto found_in_graveyard;
  118. }
  119. spin_unlock(&trans->peer_gylock);
  120. /* we can now add the new candidate to the list
  121. * - tell the application layer that this peer has been added
  122. */
  123. rxrpc_get_transport(trans);
  124. peer = candidate;
  125. candidate = NULL;
  126. if (peer->ops && peer->ops->adding) {
  127. ret = peer->ops->adding(peer);
  128. if (ret < 0) {
  129. write_unlock(&trans->peer_lock);
  130. __RXACCT(atomic_dec(&rxrpc_peer_count));
  131. kfree(peer);
  132. rxrpc_put_transport(trans);
  133. _leave(" = %d", ret);
  134. return ret;
  135. }
  136. }
  137. atomic_inc(&trans->peer_count);
  138. make_active:
  139. list_add_tail(&peer->link, &trans->peer_active);
  140. success_uwfree:
  141. write_unlock(&trans->peer_lock);
  142. if (candidate) {
  143. __RXACCT(atomic_dec(&rxrpc_peer_count));
  144. kfree(candidate);
  145. }
  146. if (list_empty(&peer->proc_link)) {
  147. down_write(&rxrpc_peers_sem);
  148. list_add_tail(&peer->proc_link, &rxrpc_peers);
  149. up_write(&rxrpc_peers_sem);
  150. }
  151. success:
  152. *_peer = peer;
  153. _leave(" = 0 (%p{u=%d cc=%d})",
  154. peer,
  155. atomic_read(&peer->usage),
  156. atomic_read(&peer->conn_count));
  157. return 0;
  158. /* handle the peer being found in the active list straight off */
  159. found_active:
  160. rxrpc_get_peer(peer);
  161. read_unlock(&trans->peer_lock);
  162. goto success;
  163. /* handle resurrecting a peer from the graveyard */
  164. found_in_graveyard:
  165. rxrpc_get_peer(peer);
  166. rxrpc_get_transport(peer->trans);
  167. rxrpc_krxtimod_del_timer(&peer->timeout);
  168. list_del_init(&peer->link);
  169. spin_unlock(&trans->peer_gylock);
  170. goto make_active;
  171. /* handle finding the peer on the second time through the active
  172. * list */
  173. found_active_second_chance:
  174. rxrpc_get_peer(peer);
  175. goto success_uwfree;
  176. } /* end rxrpc_peer_lookup() */
  177. /*****************************************************************************/
  178. /*
  179. * finish with a peer record
  180. * - it gets sent to the graveyard from where it can be resurrected or timed
  181. * out
  182. */
  183. void rxrpc_put_peer(struct rxrpc_peer *peer)
  184. {
  185. struct rxrpc_transport *trans = peer->trans;
  186. _enter("%p{cc=%d a=%08x}",
  187. peer,
  188. atomic_read(&peer->conn_count),
  189. ntohl(peer->addr.s_addr));
  190. /* sanity check */
  191. if (atomic_read(&peer->usage) <= 0)
  192. BUG();
  193. write_lock(&trans->peer_lock);
  194. spin_lock(&trans->peer_gylock);
  195. if (likely(!atomic_dec_and_test(&peer->usage))) {
  196. spin_unlock(&trans->peer_gylock);
  197. write_unlock(&trans->peer_lock);
  198. _leave("");
  199. return;
  200. }
  201. /* move to graveyard queue */
  202. list_del(&peer->link);
  203. write_unlock(&trans->peer_lock);
  204. list_add_tail(&peer->link, &trans->peer_graveyard);
  205. BUG_ON(!list_empty(&peer->conn_active));
  206. rxrpc_krxtimod_add_timer(&peer->timeout, rxrpc_peer_timeout * HZ);
  207. spin_unlock(&trans->peer_gylock);
  208. rxrpc_put_transport(trans);
  209. _leave(" [killed]");
  210. } /* end rxrpc_put_peer() */
  211. /*****************************************************************************/
  212. /*
  213. * handle a peer timing out in the graveyard
  214. * - called from krxtimod
  215. */
  216. static void rxrpc_peer_do_timeout(struct rxrpc_peer *peer)
  217. {
  218. struct rxrpc_transport *trans = peer->trans;
  219. _enter("%p{u=%d cc=%d a=%08x}",
  220. peer,
  221. atomic_read(&peer->usage),
  222. atomic_read(&peer->conn_count),
  223. ntohl(peer->addr.s_addr));
  224. BUG_ON(atomic_read(&peer->usage) < 0);
  225. /* remove from graveyard if still dead */
  226. spin_lock(&trans->peer_gylock);
  227. if (atomic_read(&peer->usage) == 0)
  228. list_del_init(&peer->link);
  229. else
  230. peer = NULL;
  231. spin_unlock(&trans->peer_gylock);
  232. if (!peer) {
  233. _leave("");
  234. return; /* resurrected */
  235. }
  236. /* clear all connections on this peer */
  237. rxrpc_conn_clearall(peer);
  238. BUG_ON(!list_empty(&peer->conn_active));
  239. BUG_ON(!list_empty(&peer->conn_graveyard));
  240. /* inform the application layer */
  241. if (peer->ops && peer->ops->discarding)
  242. peer->ops->discarding(peer);
  243. if (!list_empty(&peer->proc_link)) {
  244. down_write(&rxrpc_peers_sem);
  245. list_del(&peer->proc_link);
  246. up_write(&rxrpc_peers_sem);
  247. }
  248. __RXACCT(atomic_dec(&rxrpc_peer_count));
  249. kfree(peer);
  250. /* if the graveyard is now empty, wake up anyone waiting for that */
  251. if (atomic_dec_and_test(&trans->peer_count))
  252. wake_up(&trans->peer_gy_waitq);
  253. _leave(" [destroyed]");
  254. } /* end rxrpc_peer_do_timeout() */
  255. /*****************************************************************************/
  256. /*
  257. * clear all peer records from a transport endpoint
  258. */
  259. void rxrpc_peer_clearall(struct rxrpc_transport *trans)
  260. {
  261. DECLARE_WAITQUEUE(myself,current);
  262. struct rxrpc_peer *peer;
  263. int err;
  264. _enter("%p",trans);
  265. /* there shouldn't be any active peers remaining */
  266. BUG_ON(!list_empty(&trans->peer_active));
  267. /* manually timeout all peers in the graveyard */
  268. spin_lock(&trans->peer_gylock);
  269. while (!list_empty(&trans->peer_graveyard)) {
  270. peer = list_entry(trans->peer_graveyard.next,
  271. struct rxrpc_peer, link);
  272. _debug("Clearing peer %p\n", peer);
  273. err = rxrpc_krxtimod_del_timer(&peer->timeout);
  274. spin_unlock(&trans->peer_gylock);
  275. if (err == 0)
  276. rxrpc_peer_do_timeout(peer);
  277. spin_lock(&trans->peer_gylock);
  278. }
  279. spin_unlock(&trans->peer_gylock);
  280. /* wait for the the peer graveyard to be completely cleared */
  281. set_current_state(TASK_UNINTERRUPTIBLE);
  282. add_wait_queue(&trans->peer_gy_waitq, &myself);
  283. while (atomic_read(&trans->peer_count) != 0) {
  284. schedule();
  285. set_current_state(TASK_UNINTERRUPTIBLE);
  286. }
  287. remove_wait_queue(&trans->peer_gy_waitq, &myself);
  288. set_current_state(TASK_RUNNING);
  289. _leave("");
  290. } /* end rxrpc_peer_clearall() */
  291. /*****************************************************************************/
  292. /*
  293. * calculate and cache the Round-Trip-Time for a message and its response
  294. */
  295. void rxrpc_peer_calculate_rtt(struct rxrpc_peer *peer,
  296. struct rxrpc_message *msg,
  297. struct rxrpc_message *resp)
  298. {
  299. unsigned long long rtt;
  300. int loop;
  301. _enter("%p,%p,%p", peer, msg, resp);
  302. /* calculate the latest RTT */
  303. rtt = resp->stamp.tv_sec - msg->stamp.tv_sec;
  304. rtt *= 1000000UL;
  305. rtt += resp->stamp.tv_usec - msg->stamp.tv_usec;
  306. /* add to cache */
  307. peer->rtt_cache[peer->rtt_point] = rtt;
  308. peer->rtt_point++;
  309. peer->rtt_point %= RXRPC_RTT_CACHE_SIZE;
  310. if (peer->rtt_usage < RXRPC_RTT_CACHE_SIZE)
  311. peer->rtt_usage++;
  312. /* recalculate RTT */
  313. rtt = 0;
  314. for (loop = peer->rtt_usage - 1; loop >= 0; loop--)
  315. rtt += peer->rtt_cache[loop];
  316. do_div(rtt, peer->rtt_usage);
  317. peer->rtt = rtt;
  318. _leave(" RTT=%lu.%lums",
  319. (long) (peer->rtt / 1000), (long) (peer->rtt % 1000));
  320. } /* end rxrpc_peer_calculate_rtt() */