gnunet-service-testbed_connectionpool.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008--2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file testbed/gnunet-service-testbed_connectionpool.c
  18. * @brief connection pooling for connections to peers' services
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "gnunet-service-testbed.h"
  22. #include "gnunet-service-testbed_connectionpool.h"
  23. #include "testbed_api_operations.h"
  24. #include "gnunet_transport_service.h"
  25. /**
  26. * Redefine LOG with a changed log component string
  27. */
  28. #ifdef LOG
  29. #undef LOG
  30. #endif
  31. #define LOG(kind, ...) \
  32. GNUNET_log_from (kind, "testbed-connectionpool", __VA_ARGS__)
  33. /**
  34. * Time to expire a cache entry
  35. */
  36. #define CACHE_EXPIRY \
  37. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
  38. /**
  39. * The request handle for obtaining a pooled connection
  40. */
  41. struct GST_ConnectionPool_GetHandle;
  42. /**
  43. * A pooled connection
  44. */
  45. struct PooledConnection
  46. {
  47. /**
  48. * Next ptr for placing this object in the DLL of least recently used pooled
  49. * connections
  50. */
  51. struct PooledConnection *next;
  52. /**
  53. * Prev ptr for placing this object in the DLL of the least recently used
  54. * pooled connections
  55. */
  56. struct PooledConnection *prev;
  57. /**
  58. * The transport handle to the peer corresponding to this entry; can be NULL
  59. */
  60. struct GNUNET_TRANSPORT_CoreHandle *handle_transport;
  61. /**
  62. * The core handle to the peer corresponding to this entry; can be NULL
  63. */
  64. struct GNUNET_CORE_Handle *handle_core;
  65. /**
  66. * The ATS handle to the peer correspondign to this entry; can be NULL.
  67. */
  68. struct GNUNET_ATS_ConnectivityHandle *handle_ats_connectivity;
  69. /**
  70. * The operation handle for transport handle
  71. */
  72. struct GNUNET_TESTBED_Operation *op_transport;
  73. /**
  74. * The operation handle for core handle
  75. */
  76. struct GNUNET_TESTBED_Operation *op_core;
  77. /**
  78. * The operation handle for ATS handle
  79. */
  80. struct GNUNET_TESTBED_Operation *op_ats_connectivity;
  81. /**
  82. * The peer identity of this peer. Will be set upon opening a connection to
  83. * the peers CORE service. Will be NULL until then and after the CORE
  84. * connection is closed
  85. */
  86. struct GNUNET_PeerIdentity *peer_identity;
  87. /**
  88. * The configuration of the peer. Should be not NULL as long as the
  89. * core_handle or transport_handle are valid
  90. */
  91. struct GNUNET_CONFIGURATION_Handle *cfg;
  92. /**
  93. * DLL head for the queue to serve notifications when a peer is connected
  94. */
  95. struct GST_ConnectionPool_GetHandle *head_notify;
  96. /**
  97. * DLL tail for the queue to serve notifications when a peer is connected
  98. */
  99. struct GST_ConnectionPool_GetHandle *tail_notify;
  100. /**
  101. * DLL head for the queue of #GST_ConnectionPool_GetHandle requests that are
  102. * waiting for this connection to be opened
  103. */
  104. struct GST_ConnectionPool_GetHandle *head_waiting;
  105. /**
  106. * DLL tail for the queue of #GST_ConnectionPool_GetHandle requests that are
  107. * waiting for this connection to be opened
  108. */
  109. struct GST_ConnectionPool_GetHandle *tail_waiting;
  110. /**
  111. * The task to expire this connection from the connection pool
  112. */
  113. struct GNUNET_SCHEDULER_Task *expire_task;
  114. /**
  115. * The task to notify a waiting #GST_ConnectionPool_GetHandle object
  116. */
  117. struct GNUNET_SCHEDULER_Task *notify_task;
  118. /**
  119. * Number of active requests using this pooled connection
  120. */
  121. unsigned int demand;
  122. /**
  123. * Is this entry in LRU
  124. */
  125. int in_lru;
  126. /**
  127. * Is this entry present in the connection pool
  128. */
  129. int in_pool;
  130. /**
  131. * The index of this peer
  132. */
  133. uint32_t index;
  134. };
  135. /**
  136. * The request handle for obtaining a pooled connection
  137. */
  138. struct GST_ConnectionPool_GetHandle
  139. {
  140. /**
  141. * The next ptr for inclusion in the notification DLLs. At first the object
  142. * is placed in the waiting DLL of the corresponding #PooledConnection
  143. * object. After the handle is opened it is moved to the notification DLL if
  144. * @p connect_notify_cb and @p target are not NULL
  145. */
  146. struct GST_ConnectionPool_GetHandle *next;
  147. /**
  148. * The prev ptr for inclusion in the notification DLLs
  149. */
  150. struct GST_ConnectionPool_GetHandle *prev;
  151. /**
  152. * The pooled connection object this handle corresponds to
  153. */
  154. struct PooledConnection *entry;
  155. /**
  156. * The cache callback to call when a handle is available
  157. */
  158. GST_connection_pool_connection_ready_cb cb;
  159. /**
  160. * The closure for the above callback
  161. */
  162. void *cb_cls;
  163. /**
  164. * The peer identity of the target peer. When this target peer is connected,
  165. * call the notify callback
  166. */
  167. const struct GNUNET_PeerIdentity *target;
  168. /**
  169. * The callback to be called for serving notification that the target peer is
  170. * connected
  171. */
  172. GST_connection_pool_peer_connect_notify connect_notify_cb;
  173. /**
  174. * The closure for the notify callback
  175. */
  176. void *connect_notify_cb_cls;
  177. /**
  178. * The service we want to connect to
  179. */
  180. enum GST_ConnectionPool_Service service;
  181. /**
  182. * Did we call the pool_connection_ready_cb already?
  183. */
  184. int connection_ready_called;
  185. /**
  186. * Are we waiting for any peer connect notifications?
  187. */
  188. int notify_waiting;
  189. };
  190. /**
  191. * A hashmap for quickly finding connections in the connection pool
  192. */
  193. static struct GNUNET_CONTAINER_MultiHashMap32 *map;
  194. /**
  195. * DLL head for maitaining the least recently used #PooledConnection objects.
  196. * The head is the least recently used object.
  197. */
  198. static struct PooledConnection *head_lru;
  199. /**
  200. * DLL tail for maitaining the least recently used #PooledConnection objects
  201. */
  202. static struct PooledConnection *tail_lru;
  203. /**
  204. * DLL head for maintaining #PooledConnection objects that are not added into
  205. * the connection pool as it was full at the time the object's creation
  206. * FIXME
  207. */
  208. static struct PooledConnection *head_not_pooled;
  209. /**
  210. * DLL tail for maintaining #PooledConnection objects that are not added into
  211. * the connection pool as it was full at the time the object's creation
  212. */
  213. static struct PooledConnection *tail_not_pooled;
  214. /**
  215. * The maximum number of entries that can be present in the connection pool
  216. */
  217. static unsigned int max_size;
  218. /**
  219. * Cancel the expiration task of the give #PooledConnection object
  220. *
  221. * @param entry the #PooledConnection object
  222. */
  223. static void
  224. expire_task_cancel (struct PooledConnection *entry);
  225. /**
  226. * Destroy a #PooledConnection object
  227. *
  228. * @param entry the #PooledConnection object
  229. */
  230. static void
  231. destroy_pooled_connection (struct PooledConnection *entry)
  232. {
  233. GNUNET_assert ((NULL == entry->head_notify) && (NULL == entry->tail_notify));
  234. GNUNET_assert ((NULL == entry->head_waiting) &&
  235. (NULL == entry->tail_waiting));
  236. GNUNET_assert (0 == entry->demand);
  237. expire_task_cancel (entry);
  238. if (entry->in_lru)
  239. GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
  240. if (entry->in_pool)
  241. GNUNET_assert (
  242. GNUNET_OK ==
  243. GNUNET_CONTAINER_multihashmap32_remove (map, entry->index, entry));
  244. if (NULL != entry->notify_task)
  245. {
  246. GNUNET_SCHEDULER_cancel (entry->notify_task);
  247. entry->notify_task = NULL;
  248. }
  249. LOG_DEBUG ("Cleaning up handles of a pooled connection\n");
  250. if (NULL != entry->handle_transport)
  251. GNUNET_assert (NULL != entry->op_transport);
  252. if (NULL != entry->op_transport)
  253. {
  254. GNUNET_TESTBED_operation_done (entry->op_transport);
  255. entry->op_transport = NULL;
  256. }
  257. if (NULL != entry->handle_ats_connectivity)
  258. GNUNET_assert (NULL != entry->op_ats_connectivity);
  259. if (NULL != entry->op_ats_connectivity)
  260. {
  261. GNUNET_TESTBED_operation_done (entry->op_ats_connectivity);
  262. entry->op_ats_connectivity = NULL;
  263. }
  264. if (NULL != entry->op_core)
  265. {
  266. GNUNET_TESTBED_operation_done (entry->op_core);
  267. entry->op_core = NULL;
  268. }
  269. GNUNET_assert (NULL == entry->handle_core);
  270. GNUNET_assert (NULL == entry->handle_ats_connectivity);
  271. GNUNET_assert (NULL == entry->handle_transport);
  272. GNUNET_CONFIGURATION_destroy (entry->cfg);
  273. GNUNET_free (entry);
  274. }
  275. /**
  276. * Expire a #PooledConnection object
  277. *
  278. * @param cls the #PooledConnection object
  279. */
  280. static void
  281. expire (void *cls)
  282. {
  283. struct PooledConnection *entry = cls;
  284. entry->expire_task = NULL;
  285. destroy_pooled_connection (entry);
  286. }
  287. /**
  288. * Cancel the expiration task of the give #PooledConnection object
  289. *
  290. * @param entry the #PooledConnection object
  291. */
  292. static void
  293. expire_task_cancel (struct PooledConnection *entry)
  294. {
  295. if (NULL != entry->expire_task)
  296. {
  297. GNUNET_SCHEDULER_cancel (entry->expire_task);
  298. entry->expire_task = NULL;
  299. }
  300. }
  301. /**
  302. * Function to add a #PooledConnection object into LRU and begin the expiry task
  303. *
  304. * @param entry the #PooledConnection object
  305. */
  306. static void
  307. add_to_lru (struct PooledConnection *entry)
  308. {
  309. GNUNET_assert (0 == entry->demand);
  310. GNUNET_assert (! entry->in_lru);
  311. GNUNET_CONTAINER_DLL_insert_tail (head_lru, tail_lru, entry);
  312. entry->in_lru = GNUNET_YES;
  313. GNUNET_assert (NULL == entry->expire_task);
  314. entry->expire_task =
  315. GNUNET_SCHEDULER_add_delayed (CACHE_EXPIRY, &expire, entry);
  316. }
  317. /**
  318. * Function to find a #GST_ConnectionPool_GetHandle which is waiting for one of
  319. * the handles in given entry which are now available.
  320. *
  321. * @param entry the pooled connection whose active list has to be searched
  322. * @param head the starting list element in the GSTCacheGetHandle where the
  323. * search has to be begin
  324. * @return a suitable GSTCacheGetHandle whose handle ready notify callback
  325. * hasn't been called yet. NULL if no such suitable GSTCacheGetHandle
  326. * is found
  327. */
  328. static struct GST_ConnectionPool_GetHandle *
  329. search_waiting (const struct PooledConnection *entry,
  330. struct GST_ConnectionPool_GetHandle *head)
  331. {
  332. struct GST_ConnectionPool_GetHandle *gh;
  333. for (gh = head; NULL != gh; gh = gh->next)
  334. {
  335. switch (gh->service)
  336. {
  337. case GST_CONNECTIONPOOL_SERVICE_CORE:
  338. if (NULL == entry->handle_core)
  339. continue;
  340. if (NULL == entry->peer_identity)
  341. continue; /* CORE connection isn't ready yet */
  342. break;
  343. case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
  344. if (NULL == entry->handle_transport)
  345. continue;
  346. break;
  347. case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
  348. if (NULL == entry->handle_ats_connectivity)
  349. continue;
  350. break;
  351. }
  352. break;
  353. }
  354. return gh;
  355. }
  356. /**
  357. * A handle in the #PooledConnection object pointed by @a cls is ready and there
  358. * is a #GST_ConnectionPool_GetHandle object waiting in the waiting list. This
  359. * function retrieves that object and calls the handle ready callback. It
  360. * further schedules itself if there are similar waiting objects which can be
  361. * notified.
  362. *
  363. * @param cls the #PooledConnection object
  364. */
  365. static void
  366. connection_ready (void *cls)
  367. {
  368. struct PooledConnection *entry = cls;
  369. struct GST_ConnectionPool_GetHandle *gh;
  370. struct GST_ConnectionPool_GetHandle *gh_next;
  371. GNUNET_assert (NULL != entry->notify_task);
  372. entry->notify_task = NULL;
  373. gh = search_waiting (entry, entry->head_waiting);
  374. GNUNET_assert (NULL != gh);
  375. gh_next = NULL;
  376. if (NULL != gh->next)
  377. gh_next = search_waiting (entry, gh->next);
  378. GNUNET_CONTAINER_DLL_remove (entry->head_waiting, entry->tail_waiting, gh);
  379. gh->connection_ready_called = 1;
  380. if (NULL != gh_next)
  381. entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
  382. if ((NULL != gh->target) && (NULL != gh->connect_notify_cb))
  383. {
  384. GNUNET_CONTAINER_DLL_insert_tail (entry->head_notify,
  385. entry->tail_notify,
  386. gh);
  387. gh->notify_waiting = 1;
  388. }
  389. LOG_DEBUG ("Connection ready for handle type %u\n", gh->service);
  390. gh->cb (gh->cb_cls,
  391. entry->handle_core,
  392. entry->handle_transport,
  393. entry->handle_ats_connectivity,
  394. entry->peer_identity,
  395. entry->cfg);
  396. }
  397. /**
  398. * Function called from peer connect notify callbacks from CORE and TRANSPORT
  399. * connections. This function calls the pending peer connect notify callbacks
  400. * which are queued in an entry.
  401. *
  402. * @param cls the #PooledConnection object
  403. * @param peer the peer that connected
  404. * @param service the service where this notification has originated
  405. */
  406. static void
  407. peer_connect_notify_cb (void *cls,
  408. const struct GNUNET_PeerIdentity *peer,
  409. const enum GST_ConnectionPool_Service service)
  410. {
  411. struct PooledConnection *entry = cls;
  412. struct GST_ConnectionPool_GetHandle *gh;
  413. struct GST_ConnectionPool_GetHandle *gh_next;
  414. GST_connection_pool_peer_connect_notify cb;
  415. void *cb_cls;
  416. for (gh = entry->head_notify; NULL != gh;)
  417. {
  418. GNUNET_assert (NULL != gh->target);
  419. GNUNET_assert (NULL != gh->connect_notify_cb);
  420. GNUNET_assert (gh->connection_ready_called);
  421. if (service != gh->service)
  422. {
  423. gh = gh->next;
  424. continue;
  425. }
  426. if (0 != memcmp (gh->target, peer, sizeof (struct GNUNET_PeerIdentity)))
  427. {
  428. gh = gh->next;
  429. continue;
  430. }
  431. cb = gh->connect_notify_cb;
  432. cb_cls = gh->connect_notify_cb_cls;
  433. gh_next = gh->next;
  434. GNUNET_CONTAINER_DLL_remove (entry->head_notify, entry->tail_notify, gh);
  435. gh->notify_waiting = 0;
  436. LOG_DEBUG ("Peer connected to peer %u at service %u\n",
  437. entry->index,
  438. gh->service);
  439. gh = gh_next;
  440. cb (cb_cls, peer);
  441. }
  442. }
  443. /**
  444. * Function called to notify transport users that another
  445. * peer connected to us.
  446. *
  447. * @param cls the #PooledConnection object
  448. * @param peer the peer that connected
  449. * @param mq queue for sending data to @a peer
  450. * @return NULL
  451. */
  452. static void *
  453. transport_peer_connect_notify_cb (void *cls,
  454. const struct GNUNET_PeerIdentity *peer,
  455. struct GNUNET_MQ_Handle *mq)
  456. {
  457. struct PooledConnection *entry = cls;
  458. peer_connect_notify_cb (entry, peer, GST_CONNECTIONPOOL_SERVICE_TRANSPORT);
  459. return NULL;
  460. }
  461. /**
  462. * Function called when resources for opening a connection to TRANSPORT are
  463. * available.
  464. *
  465. * @param cls the #PooledConnection object
  466. */
  467. static void
  468. opstart_get_handle_transport (void *cls)
  469. {
  470. struct PooledConnection *entry = cls;
  471. GNUNET_assert (NULL != entry);
  472. LOG_DEBUG ("Opening a transport connection to peer %u\n", entry->index);
  473. entry->handle_transport =
  474. GNUNET_TRANSPORT_core_connect (entry->cfg,
  475. NULL,
  476. NULL,
  477. entry,
  478. &transport_peer_connect_notify_cb,
  479. NULL,
  480. NULL);
  481. if (NULL == entry->handle_transport)
  482. {
  483. GNUNET_break (0);
  484. return;
  485. }
  486. if (0 == entry->demand)
  487. return;
  488. if (NULL != entry->notify_task)
  489. return;
  490. if (NULL != search_waiting (entry, entry->head_waiting))
  491. {
  492. entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
  493. return;
  494. }
  495. }
  496. /**
  497. * Function called when the operation responsible for opening a TRANSPORT
  498. * connection is marked as done.
  499. *
  500. * @param cls the cache entry
  501. */
  502. static void
  503. oprelease_get_handle_transport (void *cls)
  504. {
  505. struct PooledConnection *entry = cls;
  506. if (NULL == entry->handle_transport)
  507. return;
  508. GNUNET_TRANSPORT_core_disconnect (entry->handle_transport);
  509. entry->handle_transport = NULL;
  510. }
  511. /**
  512. * Method called whenever a given peer connects at CORE level
  513. *
  514. * @param cls the #PooledConnection object
  515. * @param peer peer identity this notification is about
  516. * @param mq message queue for talking to @a peer
  517. * @return peer
  518. */
  519. static void *
  520. core_peer_connect_cb (void *cls,
  521. const struct GNUNET_PeerIdentity *peer,
  522. struct GNUNET_MQ_Handle *mq)
  523. {
  524. struct PooledConnection *entry = cls;
  525. peer_connect_notify_cb (entry, peer, GST_CONNECTIONPOOL_SERVICE_CORE);
  526. return (void *) peer;
  527. }
  528. /**
  529. * Function called after #GNUNET_CORE_connect() has succeeded (or failed
  530. * for good). Note that the private key of the peer is intentionally
  531. * not exposed here; if you need it, your process should try to read
  532. * the private key file directly (which should work if you are
  533. * authorized...). Implementations of this function must not call
  534. * #GNUNET_CORE_disconnect() (other than by scheduling a new task to
  535. * do this later).
  536. *
  537. * @param cls the #PooledConnection object
  538. * @param my_identity ID of this peer, NULL if we failed
  539. */
  540. static void
  541. core_startup_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity)
  542. {
  543. struct PooledConnection *entry = cls;
  544. if (NULL == my_identity)
  545. {
  546. GNUNET_break (0);
  547. return;
  548. }
  549. GNUNET_assert (NULL == entry->peer_identity);
  550. entry->peer_identity = GNUNET_new (struct GNUNET_PeerIdentity);
  551. *entry->peer_identity = *my_identity;
  552. if (0 == entry->demand)
  553. return;
  554. if (NULL != entry->notify_task)
  555. return;
  556. if (NULL != search_waiting (entry, entry->head_waiting))
  557. {
  558. entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
  559. return;
  560. }
  561. }
  562. /**
  563. * Function called when resources for opening a connection to CORE are
  564. * available.
  565. *
  566. * @param cls the #PooledConnection object
  567. */
  568. static void
  569. opstart_get_handle_core (void *cls)
  570. {
  571. struct PooledConnection *entry = cls;
  572. GNUNET_assert (NULL != entry);
  573. LOG_DEBUG ("Opening a CORE connection to peer %u\n", entry->index);
  574. entry->handle_core =
  575. GNUNET_CORE_connect (entry->cfg,
  576. entry, /* closure */
  577. &core_startup_cb, /* core startup notify */
  578. &core_peer_connect_cb, /* peer connect notify */
  579. NULL, /* peer disconnect notify */
  580. NULL);
  581. }
  582. /**
  583. * Function called when the operation responsible for opening a CORE
  584. * connection is marked as done.
  585. *
  586. * @param cls the #PooledConnection object
  587. */
  588. static void
  589. oprelease_get_handle_core (void *cls)
  590. {
  591. struct PooledConnection *entry = cls;
  592. if (NULL == entry->handle_core)
  593. return;
  594. GNUNET_CORE_disconnect (entry->handle_core);
  595. entry->handle_core = NULL;
  596. GNUNET_free_non_null (entry->peer_identity);
  597. entry->peer_identity = NULL;
  598. }
  599. /**
  600. * Function called when resources for opening a connection to ATS are
  601. * available.
  602. *
  603. * @param cls the #PooledConnection object
  604. */
  605. static void
  606. opstart_get_handle_ats_connectivity (void *cls)
  607. {
  608. struct PooledConnection *entry = cls;
  609. entry->handle_ats_connectivity = GNUNET_ATS_connectivity_init (entry->cfg);
  610. }
  611. /**
  612. * Function called when the operation responsible for opening a ATS
  613. * connection is marked as done.
  614. *
  615. * @param cls the #PooledConnection object
  616. */
  617. static void
  618. oprelease_get_handle_ats_connectivity (void *cls)
  619. {
  620. struct PooledConnection *entry = cls;
  621. if (NULL == entry->handle_ats_connectivity)
  622. return;
  623. GNUNET_ATS_connectivity_done (entry->handle_ats_connectivity);
  624. entry->handle_ats_connectivity = NULL;
  625. }
  626. /**
  627. * This function will be called for every #PooledConnection object in @p map
  628. *
  629. * @param cls NULL
  630. * @param key current key code
  631. * @param value the #PooledConnection object
  632. * @return #GNUNET_YES if we should continue to
  633. * iterate,
  634. * #GNUNET_NO if not.
  635. */
  636. static int
  637. cleanup_iterator (void *cls, uint32_t key, void *value)
  638. {
  639. struct PooledConnection *entry = value;
  640. GNUNET_assert (NULL != entry);
  641. destroy_pooled_connection (entry);
  642. return GNUNET_YES;
  643. }
  644. /**
  645. * Initialise the connection pool.
  646. *
  647. * @param size the size of the connection pool. Each entry in the connection
  648. * pool can handle a connection to each of the services enumerated in
  649. * #GST_ConnectionPool_Service
  650. */
  651. void
  652. GST_connection_pool_init (unsigned int size)
  653. {
  654. max_size = size;
  655. if (0 == max_size)
  656. return;
  657. GNUNET_assert (NULL == map);
  658. map = GNUNET_CONTAINER_multihashmap32_create (((size * 3) / 4) + 1);
  659. }
  660. /**
  661. * Cleanup the connection pool
  662. */
  663. void
  664. GST_connection_pool_destroy ()
  665. {
  666. struct PooledConnection *entry;
  667. if (NULL != map)
  668. {
  669. GNUNET_assert (
  670. GNUNET_SYSERR !=
  671. GNUNET_CONTAINER_multihashmap32_iterate (map, &cleanup_iterator, NULL));
  672. GNUNET_CONTAINER_multihashmap32_destroy (map);
  673. map = NULL;
  674. }
  675. while (NULL != (entry = head_lru))
  676. {
  677. GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
  678. destroy_pooled_connection (entry);
  679. }
  680. GNUNET_assert (NULL == head_not_pooled);
  681. }
  682. /**
  683. * Get a connection handle to @a service. If the connection is opened before
  684. * and the connection handle is present in the connection pool, it is returned
  685. * through @a cb. @a peer_id is used for the lookup in the connection pool. If
  686. * the connection handle is not present in the connection pool, a new connection
  687. * handle is opened for the @a service using @a cfg. Additionally, @a target,
  688. * @a connect_notify_cb can be specified to get notified when @a target is
  689. * connected at @a service.
  690. *
  691. * @note @a connect_notify_cb will not be called if @a target is
  692. * already connected @a service level. Use
  693. * GNUNET_TRANSPORT_check_peer_connected() or a similar function from the
  694. * respective @a service's API to check if the target peer is already connected
  695. * or not. @a connect_notify_cb will be called only once or never (in case @a
  696. * target cannot be connected or is already connected).
  697. *
  698. * @param peer_id the index of the peer
  699. * @param cfg the configuration with which the transport handle has to be
  700. * created if it was not present in the cache
  701. * @param service the service of interest
  702. * @param cb the callback to notify when the transport handle is available
  703. * @param cb_cls the closure for @a cb
  704. * @param target the peer identify of the peer whose connection to our TRANSPORT
  705. * subsystem will be notified through the @a connect_notify_cb. Can be
  706. * NULL
  707. * @param connect_notify_cb the callback to call when the @a target peer is
  708. * connected. This callback will only be called once or never again (in
  709. * case the target peer cannot be connected). Can be NULL
  710. * @param connect_notify_cb_cls the closure for @a connect_notify_cb
  711. * @return the handle which can be used cancel or mark that the handle is no
  712. * longer being used
  713. */
  714. struct GST_ConnectionPool_GetHandle *
  715. GST_connection_pool_get_handle (
  716. unsigned int peer_id,
  717. const struct GNUNET_CONFIGURATION_Handle *cfg,
  718. enum GST_ConnectionPool_Service service,
  719. GST_connection_pool_connection_ready_cb cb,
  720. void *cb_cls,
  721. const struct GNUNET_PeerIdentity *target,
  722. GST_connection_pool_peer_connect_notify connect_notify_cb,
  723. void *connect_notify_cb_cls)
  724. {
  725. struct GST_ConnectionPool_GetHandle *gh;
  726. struct PooledConnection *entry;
  727. struct GNUNET_TESTBED_Operation *op;
  728. void *handle;
  729. uint32_t peer_id32;
  730. peer_id32 = (uint32_t) peer_id;
  731. handle = NULL;
  732. entry = NULL;
  733. if (NULL != map)
  734. entry = GNUNET_CONTAINER_multihashmap32_get (map, peer_id32);
  735. if (NULL != entry)
  736. {
  737. if (entry->in_lru)
  738. {
  739. GNUNET_assert (0 == entry->demand);
  740. expire_task_cancel (entry);
  741. GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
  742. entry->in_lru = GNUNET_NO;
  743. }
  744. switch (service)
  745. {
  746. case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
  747. handle = entry->handle_transport;
  748. if (NULL != handle)
  749. LOG_DEBUG ("Found TRANSPORT handle for peer %u\n", entry->index);
  750. break;
  751. case GST_CONNECTIONPOOL_SERVICE_CORE:
  752. handle = entry->handle_core;
  753. if (NULL != handle)
  754. LOG_DEBUG ("Found CORE handle for peer %u\n", entry->index);
  755. break;
  756. case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
  757. handle = entry->handle_ats_connectivity;
  758. if (NULL != handle)
  759. LOG_DEBUG ("Found ATS CONNECTIVITY handle for peer %u\n", entry->index);
  760. break;
  761. }
  762. }
  763. else
  764. {
  765. entry = GNUNET_new (struct PooledConnection);
  766. entry->index = peer_id32;
  767. if ((NULL != map) &&
  768. (GNUNET_CONTAINER_multihashmap32_size (map) < max_size))
  769. {
  770. GNUNET_assert (GNUNET_OK ==
  771. GNUNET_CONTAINER_multihashmap32_put (
  772. map,
  773. entry->index,
  774. entry,
  775. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
  776. entry->in_pool = GNUNET_YES;
  777. }
  778. else
  779. {
  780. GNUNET_CONTAINER_DLL_insert_tail (head_not_pooled,
  781. tail_not_pooled,
  782. entry);
  783. }
  784. entry->cfg = GNUNET_CONFIGURATION_dup (cfg);
  785. }
  786. entry->demand++;
  787. gh = GNUNET_new (struct GST_ConnectionPool_GetHandle);
  788. gh->entry = entry;
  789. gh->cb = cb;
  790. gh->cb_cls = cb_cls;
  791. gh->target = target;
  792. gh->connect_notify_cb = connect_notify_cb;
  793. gh->connect_notify_cb_cls = connect_notify_cb_cls;
  794. gh->service = service;
  795. GNUNET_CONTAINER_DLL_insert (entry->head_waiting, entry->tail_waiting, gh);
  796. if (NULL != handle)
  797. {
  798. if (NULL == entry->notify_task)
  799. {
  800. if (NULL != search_waiting (entry, entry->head_waiting))
  801. entry->notify_task =
  802. GNUNET_SCHEDULER_add_now (&connection_ready, entry);
  803. }
  804. return gh;
  805. }
  806. op = NULL;
  807. switch (gh->service)
  808. {
  809. case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
  810. if (NULL != entry->op_transport)
  811. return gh; /* Operation pending */
  812. op = GNUNET_TESTBED_operation_create_ (entry,
  813. &opstart_get_handle_transport,
  814. &oprelease_get_handle_transport);
  815. entry->op_transport = op;
  816. break;
  817. case GST_CONNECTIONPOOL_SERVICE_CORE:
  818. if (NULL != entry->op_core)
  819. return gh; /* Operation pending */
  820. op = GNUNET_TESTBED_operation_create_ (entry,
  821. &opstart_get_handle_core,
  822. &oprelease_get_handle_core);
  823. entry->op_core = op;
  824. break;
  825. case GST_CONNECTIONPOOL_SERVICE_ATS_CONNECTIVITY:
  826. if (NULL != entry->op_ats_connectivity)
  827. return gh; /* Operation pending */
  828. op =
  829. GNUNET_TESTBED_operation_create_ (entry,
  830. &opstart_get_handle_ats_connectivity,
  831. &oprelease_get_handle_ats_connectivity);
  832. entry->op_ats_connectivity = op;
  833. break;
  834. }
  835. GNUNET_TESTBED_operation_queue_insert_ (GST_opq_openfds, op);
  836. GNUNET_TESTBED_operation_begin_wait_ (op);
  837. return gh;
  838. }
  839. /**
  840. * Relinquish a #GST_ConnectionPool_GetHandle object. If the connection
  841. * associated with the object is currently being used by other
  842. * #GST_ConnectionPool_GetHandle objects, it is left in the connection pool. If
  843. * no other objects are using the connection and the connection pool is not full
  844. * then it is placed in a LRU queue. If the connection pool is full, then
  845. * connections from the LRU queue are evicted and closed to create place for
  846. * this connection. If the connection pool if full and the LRU queue is empty,
  847. * then the connection is closed.
  848. *
  849. * @param gh the handle
  850. */
  851. void
  852. GST_connection_pool_get_handle_done (struct GST_ConnectionPool_GetHandle *gh)
  853. {
  854. struct PooledConnection *entry;
  855. if (NULL == gh)
  856. return;
  857. entry = gh->entry;
  858. LOG_DEBUG ("Cleaning up get handle %p for service %u, peer %u\n",
  859. gh,
  860. gh->service,
  861. entry->index);
  862. if (! gh->connection_ready_called)
  863. {
  864. GNUNET_CONTAINER_DLL_remove (entry->head_waiting, entry->tail_waiting, gh);
  865. if ((NULL == search_waiting (entry, entry->head_waiting)) &&
  866. (NULL != entry->notify_task))
  867. {
  868. GNUNET_SCHEDULER_cancel (entry->notify_task);
  869. entry->notify_task = NULL;
  870. }
  871. }
  872. if (gh->notify_waiting)
  873. {
  874. GNUNET_CONTAINER_DLL_remove (entry->head_notify, entry->tail_notify, gh);
  875. gh->notify_waiting = 0;
  876. }
  877. GNUNET_free (gh);
  878. gh = NULL;
  879. GNUNET_assert (! entry->in_lru);
  880. if (! entry->in_pool)
  881. GNUNET_CONTAINER_DLL_remove (head_not_pooled, tail_not_pooled, entry);
  882. if (NULL != map)
  883. {
  884. if (GNUNET_YES ==
  885. GNUNET_CONTAINER_multihashmap32_contains (map, entry->index))
  886. goto unallocate;
  887. if (GNUNET_CONTAINER_multihashmap32_size (map) == max_size)
  888. {
  889. if (NULL == head_lru)
  890. goto unallocate;
  891. destroy_pooled_connection (head_lru);
  892. }
  893. GNUNET_assert (GNUNET_OK ==
  894. GNUNET_CONTAINER_multihashmap32_put (
  895. map,
  896. entry->index,
  897. entry,
  898. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  899. entry->in_pool = GNUNET_YES;
  900. }
  901. unallocate:
  902. GNUNET_assert (0 < entry->demand);
  903. entry->demand--;
  904. if (0 != entry->demand)
  905. return;
  906. if (entry->in_pool)
  907. {
  908. add_to_lru (entry);
  909. return;
  910. }
  911. destroy_pooled_connection (entry);
  912. }