2 This file is part of GNUnet.
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/gnunet-service-testbed_connectionpool.c
23 * @brief connection pooling for connections to peers' services
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
27 #include "gnunet-service-testbed.h"
28 #include "gnunet-service-testbed_connectionpool.h"
29 #include "testbed_api_operations.h"
32 * Redefine LOG with a changed log component string
37 #define LOG(kind,...) \
38 GNUNET_log_from (kind, "testbed-connectionpool", __VA_ARGS__)
42 * Time to expire a cache entry
44 #define CACHE_EXPIRY \
45 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
49 * The request handle for obtaining a pooled connection
51 struct GST_ConnectionPool_GetHandle;
57 struct PooledConnection
60 * Next ptr for placing this object in the DLL of least recently used pooled
63 struct PooledConnection *next;
66 * Prev ptr for placing this object in the DLL of the least recently used
69 struct PooledConnection *prev;
72 * The transport handle to the peer corresponding to this entry; can be NULL
74 struct GNUNET_TRANSPORT_Handle *handle_transport;
77 * The core handle to the peer corresponding to this entry; can be NULL
79 struct GNUNET_CORE_Handle *handle_core;
82 * The operation handle for transport handle
84 struct GNUNET_TESTBED_Operation *op_transport;
87 * The operation handle for core handle
89 struct GNUNET_TESTBED_Operation *op_core;
92 * The peer identity of this peer. Will be set upon opening a connection to
93 * the peers CORE service. Will be NULL until then and after the CORE
94 * connection is closed
96 struct GNUNET_PeerIdentity *peer_identity;
99 * The configuration of the peer. Should be not NULL as long as the core_handle
100 * or transport_handle are valid
102 struct GNUNET_CONFIGURATION_Handle *cfg;
105 * DLL head for the queue to serve notifications when a peer is connected
107 struct GST_ConnectionPool_GetHandle *head_notify;
110 * DLL tail for the queue to serve notifications when a peer is connected
112 struct GST_ConnectionPool_GetHandle *tail_notify;
115 * DLL head for the queue of #GST_ConnectionPool_GetHandle requests that are
116 * waiting for this connection to be opened
118 struct GST_ConnectionPool_GetHandle *head_waiting;
121 * DLL tail for the queue of #GST_ConnectionPool_GetHandle requests that are
122 * waiting for this connection to be opened
124 struct GST_ConnectionPool_GetHandle *tail_waiting;
127 * The task to expire this connection from the connection pool
129 GNUNET_SCHEDULER_TaskIdentifier expire_task;
132 * The task to notify a waiting #GST_ConnectionPool_GetHandle object
134 GNUNET_SCHEDULER_TaskIdentifier notify_task;
137 * Number of active requests using this pooled connection
142 * Is this entry in LRU
147 * Is this entry present in the connection pool
152 * The index of this peer
159 * The request handle for obtaining a pooled connection
161 struct GST_ConnectionPool_GetHandle
164 * The next ptr for inclusion in the notification DLLs. At first the object
165 * is placed in the waiting DLL of the corresponding #PooledConnection
166 * object. After the handle is opened it is moved to the notification DLL if
167 * @p connect_notify_cb and @p target are not NULL
169 struct GST_ConnectionPool_GetHandle *next;
172 * The prev ptr for inclusion in the notification DLLs
174 struct GST_ConnectionPool_GetHandle *prev;
177 * The pooled connection object this handle corresponds to
179 struct PooledConnection *entry;
182 * The cache callback to call when a handle is available
184 GST_connection_pool_connection_ready_cb cb;
187 * The closure for the above callback
192 * The peer identity of the target peer. When this target peer is connected,
193 * call the notify callback
195 const struct GNUNET_PeerIdentity *target;
198 * The callback to be called for serving notification that the target peer is
201 GST_connection_pool_peer_connect_notify connect_notify_cb;
204 * The closure for the notify callback
206 void *connect_notify_cb_cls;
209 * The service we want to connect to
211 enum GST_ConnectionPool_Service service;
214 * Did we call the pool_connection_ready_cb already?
216 int connection_ready_called;
221 * A hashmap for quickly finding connections in the connection pool
223 static struct GNUNET_CONTAINER_MultiHashMap32 *map;
226 * DLL head for maitaining the least recently used #PooledConnection objects.
227 * The head is the least recently used object.
229 static struct PooledConnection *head_lru;
232 * DLL tail for maitaining the least recently used #PooledConnection objects
234 static struct PooledConnection *tail_lru;
237 * DLL head for maintaining #PooledConnection objects that are not added into
238 * the connection pool as it was full at the time the object's creation
241 static struct PooledConnection *head_not_pooled;
244 * DLL tail for maintaining #PooledConnection objects that are not added into
245 * the connection pool as it was full at the time the object's creation
247 static struct PooledConnection *tail_not_pooled;
250 * The maximum number of entries that can be present in the connection pool
252 static unsigned int max_size;
256 * Destroy a #PooledConnection object
258 * @param entry the #PooledConnection object
261 destroy_pooled_connection (struct PooledConnection *entry)
263 GNUNET_assert ((NULL == entry->head_notify) && (NULL == entry->tail_notify));
264 GNUNET_assert ((NULL == entry->head_waiting) && (NULL ==
265 entry->tail_waiting));
266 GNUNET_assert (0 == entry->demand);
267 GNUNET_free_non_null (entry->peer_identity);
269 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
271 GNUNET_assert (GNUNET_OK ==
272 GNUNET_CONTAINER_multihashmap32_remove (map,
275 LOG_DEBUG ("Cleaning up handles of a pooled connection\n");
276 if (NULL != entry->handle_transport)
277 GNUNET_assert (NULL != entry->op_transport);
278 if (NULL != entry->op_transport)
280 GNUNET_TESTBED_operation_done (entry->op_transport);
281 entry->op_transport = NULL;
283 if (NULL != entry->op_core)
285 GNUNET_TESTBED_operation_done (entry->op_core);
286 entry->op_core = NULL;
288 GNUNET_assert (NULL == entry->handle_core);
289 GNUNET_assert (NULL == entry->handle_transport);
290 GNUNET_CONFIGURATION_destroy (entry->cfg);
296 * Expire a #PooledConnection object
298 * @param cls the #PooledConnection object
299 * @param tc scheduler task context
302 expire (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
304 struct PooledConnection *entry = cls;
306 entry->expire_task = GNUNET_SCHEDULER_NO_TASK;
307 destroy_pooled_connection (entry);
312 expire_task_cancel (struct PooledConnection *entry)
314 if (GNUNET_SCHEDULER_NO_TASK != entry->expire_task)
316 GNUNET_SCHEDULER_cancel (entry->expire_task);
317 entry->expire_task = GNUNET_SCHEDULER_NO_TASK;
323 * Function to add a #PooledConnection object into LRU and begin the expiry task
325 * @param entry the #PooledConnection object
328 add_to_lru (struct PooledConnection *entry)
330 GNUNET_assert (0 == entry->demand);
331 GNUNET_assert (!entry->in_lru);
332 GNUNET_CONTAINER_DLL_insert_tail (head_lru, tail_lru, entry);
333 entry->in_lru = GNUNET_YES;
334 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == entry->expire_task);
335 entry->expire_task = GNUNET_SCHEDULER_add_delayed (CACHE_EXPIRY,
341 * Function to find a #GST_ConnectionPool_GetHandle which is waiting for one of
342 * the handles in given entry which are now available.
344 * @param entry the pooled connection whose active list has to be searched
345 * @param head the starting list element in the GSTCacheGetHandle where the
346 * search has to be begin
347 * @return a suitable GSTCacheGetHandle whose handle ready notify callback
348 * hasn't been called yet. NULL if no such suitable GSTCacheGetHandle
351 static struct GST_ConnectionPool_GetHandle *
352 search_waiting (const struct PooledConnection *entry,
353 struct GST_ConnectionPool_GetHandle *head)
355 struct GST_ConnectionPool_GetHandle *gh;
357 for (gh = head; NULL != gh; gh = gh->next)
361 case GST_CONNECTIONPOOL_SERVICE_CORE:
362 if (NULL == entry->handle_core)
364 if (NULL == entry->peer_identity)
365 continue; /* CORE connection isn't ready yet */
367 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
368 if (NULL == entry->handle_transport)
379 * A handle in the #PooledConnection object pointed by @a cls is ready and there
380 * is a #GST_ConnectionPool_GetHandle object waiting in the waiting list. This
381 * function retrieves that object and calls the handle ready callback. It
382 * further schedules itself if there are similar waiting objects which can be notified.
384 * @param cls the #PooledConnection object
385 * @param tc the task context from scheduler
388 connection_ready (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
390 struct PooledConnection *entry = cls;
391 struct GST_ConnectionPool_GetHandle *gh;
392 struct GST_ConnectionPool_GetHandle *gh_next;
394 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != entry->notify_task);
395 entry->notify_task = GNUNET_SCHEDULER_NO_TASK;
396 gh = search_waiting (entry, entry->head_waiting);
397 GNUNET_assert (NULL != gh);
399 if (NULL != gh->next)
400 gh_next = search_waiting (entry, gh->next);
401 GNUNET_CONTAINER_DLL_remove (entry->head_waiting, entry->tail_waiting, gh);
402 gh->connection_ready_called = GNUNET_YES;
404 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
405 if ( (NULL != gh->target) && (NULL != gh->connect_notify_cb) )
406 GNUNET_CONTAINER_DLL_insert_tail (entry->head_notify, entry->tail_notify, gh);
407 LOG_DEBUG ("Calling notify for handle type %u\n", gh->service);
408 gh->cb (gh->cb_cls, entry->handle_core, entry->handle_transport,
409 entry->peer_identity);
414 * Function called from peer connect notify callbacks from CORE and TRANSPORT
415 * connections. This function calls the pendning peer connect notify callbacks
416 * which are queued in an entry.
418 * @param cls the #PooledConnection object
419 * @param peer the peer that connected
420 * @param service the service where this notification has originated
423 peer_connect_notify_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
424 const enum GST_ConnectionPool_Service service)
426 struct PooledConnection *entry = cls;
427 struct GST_ConnectionPool_GetHandle *gh;
428 struct GST_ConnectionPool_GetHandle *gh_next;
429 GST_connection_pool_peer_connect_notify cb;
432 for (gh = entry->head_notify; NULL != gh;)
434 GNUNET_assert (NULL != gh->target);
435 GNUNET_assert (NULL != gh->connect_notify_cb);
436 GNUNET_assert (gh->connection_ready_called);
437 if (service != gh->service)
442 if (0 != memcmp (gh->target, peer, sizeof (struct GNUNET_PeerIdentity)))
447 cb = gh->connect_notify_cb;
448 cb_cls = gh->connect_notify_cb_cls;
450 GNUNET_CONTAINER_DLL_remove (entry->head_notify, entry->tail_notify, gh);
458 * Function called to notify transport users that another
459 * peer connected to us.
461 * @param cls the #PooledConnection object
462 * @param peer the peer that connected
465 transport_peer_connect_notify_cb (void *cls,
466 const struct GNUNET_PeerIdentity *peer)
468 struct PooledConnection *entry = cls;
470 peer_connect_notify_cb (entry, peer, GST_CONNECTIONPOOL_SERVICE_TRANSPORT);
475 * Function called when resources for opening a connection to TRANSPORT are
478 * @param cls the #PooledConnection object
481 opstart_get_handle_transport (void *cls)
483 struct PooledConnection *entry = cls;
485 GNUNET_assert (NULL != entry);
486 LOG_DEBUG ("Opening a transport connection to peer %u\n", entry->index);
487 entry->handle_transport =
488 GNUNET_TRANSPORT_connect (entry->cfg, NULL, entry, NULL,
489 &transport_peer_connect_notify_cb, NULL);
490 if (NULL == entry->handle_transport)
495 if (0 == entry->demand)
497 if (GNUNET_SCHEDULER_NO_TASK != entry->notify_task)
499 if (NULL != search_waiting (entry, entry->head_waiting))
501 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
508 * Function called when the operation responsible for opening a TRANSPORT
509 * connection is marked as done.
511 * @param cls the cache entry
514 oprelease_get_handle_transport (void *cls)
516 struct PooledConnection *entry = cls;
518 if (NULL == entry->handle_transport)
520 GNUNET_TRANSPORT_disconnect (entry->handle_transport);
521 entry->handle_transport = NULL;
526 * Method called whenever a given peer connects at CORE level
528 * @param cls the #PooledConnection object
529 * @param peer peer identity this notification is about
532 core_peer_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
534 struct PooledConnection *entry = cls;
536 peer_connect_notify_cb (entry, peer, GST_CONNECTIONPOOL_SERVICE_CORE);
541 * Function called after GNUNET_CORE_connect has succeeded (or failed
542 * for good). Note that the private key of the peer is intentionally
543 * not exposed here; if you need it, your process should try to read
544 * the private key file directly (which should work if you are
545 * authorized...). Implementations of this function must not call
546 * GNUNET_CORE_disconnect (other than by scheduling a new task to
549 * @param cls the #PooledConnection object
550 * @param my_identity ID of this peer, NULL if we failed
553 core_startup_cb (void *cls,
554 const struct GNUNET_PeerIdentity *my_identity)
556 struct PooledConnection *entry = cls;
558 if (NULL == my_identity)
563 GNUNET_assert (NULL == entry->peer_identity);
564 entry->peer_identity = GNUNET_new (struct GNUNET_PeerIdentity);
565 memcpy (entry->peer_identity,
567 sizeof (struct GNUNET_PeerIdentity));
568 if (0 == entry->demand)
570 if (GNUNET_SCHEDULER_NO_TASK != entry->notify_task)
572 if (NULL != search_waiting (entry, entry->head_waiting))
574 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
581 * Function called when resources for opening a connection to CORE are
584 * @param cls the #PooledConnection object
587 opstart_get_handle_core (void *cls)
589 struct PooledConnection *entry = cls;
590 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
594 GNUNET_assert (NULL != entry);
595 LOG_DEBUG ("Opening a CORE connection to peer %u\n", entry->index);
597 GNUNET_CORE_connect (entry->cfg, entry, /* closure */
598 &core_startup_cb, /* core startup notify */
599 &core_peer_connect_cb, /* peer connect notify */
600 NULL, /* peer disconnect notify */
601 NULL, /* inbound notify */
602 GNUNET_NO, /* inbound header only? */
603 NULL, /* outbound notify */
604 GNUNET_NO, /* outbound header only? */
610 * Function called when the operation responsible for opening a TRANSPORT
611 * connection is marked as done.
613 * @param cls the #PooledConnection object
616 oprelease_get_handle_core (void *cls)
618 struct PooledConnection *entry = cls;
620 if (NULL == entry->handle_core)
622 GNUNET_CORE_disconnect (entry->handle_core);
623 entry->handle_core = NULL;
624 GNUNET_free_non_null (entry->peer_identity);
625 entry->peer_identity = NULL;
630 * This function will be called for every #PooledConnection object in @p map
633 * @param key current key code
634 * @param value the #PooledConnection object
635 * @return #GNUNET_YES if we should continue to
640 cleanup_iterator (void *cls,
644 struct PooledConnection *entry = value;
646 GNUNET_assert (NULL != entry);
647 GNUNET_assert (GNUNET_OK ==
648 GNUNET_CONTAINER_multihashmap32_remove (map, key, entry));
650 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
651 destroy_pooled_connection (entry);
657 * Initialise the connection pool.
659 * @param size the size of the connection pool. Each entry in the connection
660 * pool can handle a connection to each of the services enumerated in
661 * #GST_ConnectionPool_Service
664 GST_connection_pool_init (unsigned int size)
669 GNUNET_assert (NULL == map);
670 map = GNUNET_CONTAINER_multihashmap32_create (((size * 3) / 4) + 1);
675 * Cleanup the connection pool
678 GST_connection_pool_destroy ()
680 struct PooledConnection *entry;
684 GNUNET_assert (GNUNET_SYSERR !=
685 GNUNET_CONTAINER_multihashmap32_iterate (map,
688 GNUNET_CONTAINER_multihashmap32_destroy (map);
691 while (NULL != (entry = head_lru))
693 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
694 destroy_pooled_connection (entry);
700 * Get a connection handle to @a service. If the connection is opened before
701 * and the connection handle is present in the connection pool, it is returned
702 * through @a cb. @a peer_id is used for the lookup in the connection pool. If
703 * the connection handle is not present in the connection pool, a new connection
704 * handle is opened for the @a service using @a cfg. Additionally, @a target,
705 * @a connect_notify_cb can be specified to get notified when @a target is
706 * connected at @a service.
708 * @note @a connect_notify_cb will not be called if @a target is
709 * already connected @a service level. Use
710 * GNUNET_TRANSPORT_check_neighbour_connected() or a similar function from the
711 * respective @a service's API to check if the target peer is already connected or
712 * not. @a connect_notify_cb will be called only once or never (in case @a target
713 * cannot be connected or is already connected).
715 * @param peer_id the index of the peer
716 * @param cfg the configuration with which the transport handle has to be
717 * created if it was not present in the cache
718 * @param service the service of interest
719 * @param cb the callback to notify when the transport handle is available
720 * @param cb_cls the closure for @a cb
721 * @param target the peer identify of the peer whose connection to our TRANSPORT
722 * subsystem will be notified through the @a connect_notify_cb. Can be NULL
723 * @param connect_notify_cb the callback to call when the @a target peer is
724 * connected. This callback will only be called once or never again (in
725 * case the target peer cannot be connected). Can be NULL
726 * @param connect_notify_cb_cls the closure for @a connect_notify_cb
727 * @return the handle which can be used cancel or mark that the handle is no
730 struct GST_ConnectionPool_GetHandle *
731 GST_connection_pool_get_handle (unsigned int peer_id,
732 const struct GNUNET_CONFIGURATION_Handle *cfg,
733 enum GST_ConnectionPool_Service service,
734 GST_connection_pool_connection_ready_cb cb,
736 const struct GNUNET_PeerIdentity *target,
737 GST_connection_pool_peer_connect_notify connect_notify_cb,
738 void *connect_notify_cb_cls)
740 struct GST_ConnectionPool_GetHandle *gh;
741 struct PooledConnection *entry;
742 struct GNUNET_TESTBED_Operation *op;
746 peer_id32 = (uint32_t) peer_id;
749 entry = GNUNET_CONTAINER_multihashmap32_get (map, peer_id32);
754 GNUNET_assert (0 == entry->demand);
755 expire_task_cancel (entry);
756 GNUNET_CONTAINER_DLL_remove (head_lru, tail_lru, entry);
757 entry->in_lru = GNUNET_NO;
761 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
762 handle = entry->handle_transport;
764 LOG_DEBUG ("Found TRANSPORT handle for peer %u\n",
767 case GST_CONNECTIONPOOL_SERVICE_CORE:
768 handle = entry->handle_core;
770 LOG_DEBUG ("Found CORE handle for peer %u\n",
777 entry = GNUNET_new (struct PooledConnection);
778 entry->index = peer_id32;
780 && (GNUNET_CONTAINER_multihashmap32_size (map) < max_size))
782 GNUNET_assert (GNUNET_OK ==
783 GNUNET_CONTAINER_multihashmap32_put (map,
786 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
787 entry->in_pool = GNUNET_YES;
791 GNUNET_CONTAINER_DLL_insert_tail (head_not_pooled, tail_not_pooled, entry);
793 entry->cfg = GNUNET_CONFIGURATION_dup (cfg);
796 gh = GNUNET_new (struct GST_ConnectionPool_GetHandle);
801 gh->connect_notify_cb = connect_notify_cb;
802 gh->connect_notify_cb_cls = connect_notify_cb_cls;
803 gh->service = service;
804 GNUNET_CONTAINER_DLL_insert (entry->head_waiting, entry->tail_waiting, gh);
807 if (GNUNET_SCHEDULER_NO_TASK == entry->notify_task)
809 if (NULL != search_waiting (entry, entry->head_waiting))
810 entry->notify_task = GNUNET_SCHEDULER_add_now (&connection_ready, entry);
817 case GST_CONNECTIONPOOL_SERVICE_TRANSPORT:
818 if (NULL != entry->op_transport)
819 return gh; /* Operation pending */
820 op = GNUNET_TESTBED_operation_create_ (entry, &opstart_get_handle_transport,
821 &oprelease_get_handle_transport);
822 entry->op_transport = op;
824 case GST_CONNECTIONPOOL_SERVICE_CORE:
825 if (NULL != entry->op_core)
826 return gh; /* Operation pending */
827 op = GNUNET_TESTBED_operation_create_ (entry, &opstart_get_handle_core,
828 &oprelease_get_handle_core);
832 GNUNET_TESTBED_operation_queue_insert_ (GST_opq_openfds, op);
833 GNUNET_TESTBED_operation_begin_wait_ (op);
839 * Relinquish a #GST_ConnectionPool_GetHandle object. If the connection
840 * associated with the object is currently being used by other
841 * #GST_ConnectionPool_GetHandle objects, it is left in the connection pool. If
842 * no other objects are using the connection and the connection pool is not full
843 * then it is placed in a LRU queue. If the connection pool is full, then
844 * connections from the LRU queue are evicted and closed to create place for this
845 * connection. If the connection pool if full and the LRU queue is empty, then
846 * the connection is closed.
848 * @param gh the handle
851 GST_connection_pool_get_handle_done (struct GST_ConnectionPool_GetHandle *gh)
853 struct PooledConnection *entry;
856 if (!gh->connection_ready_called)
857 GNUNET_CONTAINER_DLL_remove (entry->head_waiting, entry->tail_waiting, gh);
858 else if ((NULL != gh->next) || (NULL != gh->prev))
859 GNUNET_CONTAINER_DLL_remove (entry->head_notify, entry->head_notify, gh);
862 GNUNET_assert (!entry->in_lru);
863 if ( (!entry->in_pool) && (NULL != map) )
865 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap32_contains (map,
868 if ((GNUNET_CONTAINER_multihashmap32_size (map) == max_size)
869 && (NULL == head_lru))
871 destroy_pooled_connection (head_lru);
872 GNUNET_CONTAINER_DLL_remove (head_not_pooled, tail_not_pooled, entry);
873 GNUNET_assert (GNUNET_OK ==
874 GNUNET_CONTAINER_multihashmap32_put (map,
877 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
878 entry->in_pool = GNUNET_YES;
881 GNUNET_assert (0 < entry->demand);
883 if (0 != entry->demand)
890 destroy_pooled_connection (entry);