2 This file is part of GNUnet.
3 (C) 2011 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 ats/gnunet-service-ats_performance.c
23 * @brief ats service, interaction with 'performance' API
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet-service-ats.h"
29 #include "gnunet-service-ats_addresses.h"
30 #include "gnunet-service-ats_performance.h"
31 #include "gnunet-service-ats_reservations.h"
35 * We keep clients that are interested in performance in a linked list.
37 struct PerformanceClient
40 * Next in doubly-linked list.
42 struct PerformanceClient *next;
45 * Previous in doubly-linked list.
47 struct PerformanceClient *prev;
50 * Actual handle to the client.
52 struct GNUNET_SERVER_Client *client;
55 * Options for the client.
63 * We keep clients that are interested in performance in a linked list.
65 struct AddressIteration
68 * Actual handle to the client.
70 struct PerformanceClient *pc;
76 unsigned int msg_type;
82 static struct GAS_Addresses_Handle *GSA_addresses;
85 * Head of linked list of all clients to this service.
87 static struct PerformanceClient *pc_head;
90 * Tail of linked list of all clients to this service.
92 static struct PerformanceClient *pc_tail;
95 * Context for sending messages to performance clients.
97 static struct GNUNET_SERVER_NotificationContext *nc;
101 * Find the performance client associated with the given handle.
103 * @param client server handle
104 * @return internal handle
106 static struct PerformanceClient *
107 find_client (struct GNUNET_SERVER_Client *client)
109 struct PerformanceClient *pc;
111 for (pc = pc_head; pc != NULL; pc = pc->next)
112 if (pc->client == client)
119 * Unregister a client (which may have been a performance client,
120 * but this is not assured).
122 * @param client handle of the (now dead) client
125 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
127 struct PerformanceClient *pc;
129 pc = find_client (client);
132 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
133 GAS_addresses_preference_client_disconnect (GSA_addresses, client);
139 * Transmit the given performance information to all performance
142 * @param pc performance client to send to
143 * @param peer peer for which this is an address suggestion
144 * @param plugin_name 0-termintated string specifying the transport plugin
145 * @param plugin_addr binary address for the plugin to use
146 * @param plugin_addr_len number of bytes in plugin_addr
147 * @param active #GNUNET_YES if this address is actively used
148 * to maintain a connection to a peer;
149 * #GNUNET_NO if the address is not actively used;
150 * #GNUNET_SYSERR if this address is no longer available for ATS
151 * @param atsi performance data for the address
152 * @param atsi_count number of performance records in @a atsi
153 * @param bandwidth_out assigned outbound bandwidth
154 * @param bandwidth_in assigned inbound bandwidth
157 GAS_performance_notify_client (struct PerformanceClient *pc,
158 const struct GNUNET_PeerIdentity *peer,
159 const char *plugin_name,
160 const void *plugin_addr,
161 size_t plugin_addr_len,
163 const struct GNUNET_ATS_Information *atsi,
165 struct GNUNET_BANDWIDTH_Value32NBO
167 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
170 struct PeerInformationMessage *msg;
171 size_t plugin_name_length = strlen (plugin_name) + 1;
173 sizeof (struct PeerInformationMessage) +
174 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
176 char buf[msize] GNUNET_ALIGN;
177 struct GNUNET_ATS_Information *atsp;
180 GNUNET_assert (NULL != pc);
181 if (NULL == find_client (pc->client))
182 return; /* Client disconnected */
184 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
185 GNUNET_assert (atsi_count <
186 GNUNET_SERVER_MAX_MESSAGE_SIZE /
187 sizeof (struct GNUNET_ATS_Information));
188 msg = (struct PeerInformationMessage *) buf;
189 msg->header.size = htons (msize);
190 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
192 msg->ats_count = htonl (atsi_count);
194 msg->address_length = htons (plugin_addr_len);
195 msg->address_active = ntohl ((uint32_t) active);
196 msg->plugin_name_length = htons (plugin_name_length);
197 msg->bandwidth_out = bandwidth_out;
198 msg->bandwidth_in = bandwidth_in;
199 atsp = (struct GNUNET_ATS_Information *) &msg[1];
200 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
201 addrp = (char *) &atsp[atsi_count];
202 memcpy (addrp, plugin_addr, plugin_addr_len);
203 strcpy (&addrp[plugin_addr_len], plugin_name);
204 GNUNET_SERVER_notification_context_unicast (nc,
212 * Transmit the given performance information to all performance
215 * @param peer peer for which this is an address suggestion
216 * @param plugin_name 0-termintated string specifying the transport plugin
217 * @param plugin_addr binary address for the plugin to use
218 * @param plugin_addr_len number of bytes in @a plugin_addr
219 * @param active #GNUNET_YES if this address is actively used
220 * to maintain a connection to a peer;
221 * #GNUNET_NO if the address is not actively used;
222 * #GNUNET_SYSERR if this address is no longer available for ATS
223 * @param atsi performance data for the address
224 * @param atsi_count number of performance records in @a atsi
225 * @param bandwidth_out assigned outbound bandwidth
226 * @param bandwidth_in assigned inbound bandwidth
229 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
230 const char *plugin_name,
231 const void *plugin_addr,
232 size_t plugin_addr_len,
234 const struct GNUNET_ATS_Information *atsi,
236 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
237 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
239 struct PerformanceClient *pc;
241 for (pc = pc_head; pc != NULL; pc = pc->next)
242 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
244 GAS_performance_notify_client (pc,
251 bandwidth_out, bandwidth_in);
253 GNUNET_STATISTICS_update (GSA_stats,
254 "# performance updates given to clients", 1,
261 * Iterator for called from #GAS_addresses_get_peer_info()
263 * @param cls closure with the `struct PerformanceClient *`
264 * @param id the peer id
265 * @param plugin_name plugin name
266 * @param plugin_addr address
267 * @param plugin_addr_len length of @a plugin_addr
268 * @param active is address actively used
269 * @param atsi ats performance information
270 * @param atsi_count number of ats performance elements in @a atsi
271 * @param bandwidth_out current outbound bandwidth assigned to address
272 * @param bandwidth_in current inbound bandwidth assigned to address
275 peerinfo_it (void *cls,
276 const struct GNUNET_PeerIdentity *id,
277 const char *plugin_name,
278 const void *plugin_addr,
279 size_t plugin_addr_len,
281 const struct GNUNET_ATS_Information *atsi,
283 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
284 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
286 struct PerformanceClient *pc = cls;
288 GNUNET_assert (NULL != pc);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
295 (unsigned int) ntohl (bandwidth_out.value__),
296 (unsigned int) ntohl (bandwidth_in.value__));
297 GAS_performance_notify_client(pc,
304 bandwidth_out, bandwidth_in);
309 * Iterator for #GAS_performance_add_client()
311 * @param cls the client requesting information
316 const struct GNUNET_PeerIdentity *id)
318 struct PerformanceClient *pc = cls;
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Callback for peer `%s'\n",
325 GAS_addresses_get_peer_info (GSA_addresses, id,
332 * Register a new performance client.
334 * @param client handle of the new client
335 * @param flag flag specifying the type of the client
338 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
341 struct PerformanceClient *pc;
342 GNUNET_break (NULL == find_client (client));
344 pc = GNUNET_new (struct PerformanceClient);
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Adding performance client %s PIC\n",
350 (flag == START_FLAG_PERFORMANCE_WITH_PIC) ? "with" : "without");
352 GNUNET_SERVER_notification_context_add (nc, client);
353 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
355 /* Send information about clients */
356 GAS_addresses_iterate_peers (GSA_addresses,
366 transmit_req_addr (struct AddressIteration *ai,
367 const struct GNUNET_PeerIdentity *id,
368 const char *plugin_name,
369 const void *plugin_addr, size_t plugin_addr_len,
371 const struct GNUNET_ATS_Information *atsi,
373 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
374 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
377 struct GNUNET_ATS_Information *atsp;
378 struct PeerInformationMessage *msg;
380 size_t plugin_name_length;
383 if (NULL != plugin_name)
384 plugin_name_length = strlen (plugin_name) + 1;
386 plugin_name_length = 0;
387 msize = sizeof (struct PeerInformationMessage) +
388 atsi_count * sizeof (struct GNUNET_ATS_Information) +
389 plugin_addr_len + plugin_name_length;
390 char buf[msize] GNUNET_ALIGN;
392 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
393 GNUNET_assert (atsi_count <
394 GNUNET_SERVER_MAX_MESSAGE_SIZE /
395 sizeof (struct GNUNET_ATS_Information));
396 msg = (struct PeerInformationMessage *) buf;
397 msg->header.size = htons (msize);
398 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
399 msg->ats_count = htonl (atsi_count);
400 msg->id = htonl (ai->id);
404 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
405 msg->address_length = htons (plugin_addr_len);
406 msg->address_active = ntohl (active);
407 msg->plugin_name_length = htons (plugin_name_length);
408 msg->bandwidth_out = bandwidth_out;
409 msg->bandwidth_in = bandwidth_in;
410 atsp = (struct GNUNET_ATS_Information *) &msg[1];
411 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
412 addrp = (char *) &atsp[atsi_count];
413 if (NULL != plugin_addr)
414 memcpy (addrp, plugin_addr, plugin_addr_len);
415 if (NULL != plugin_name)
416 strcpy (&addrp[plugin_addr_len], plugin_name);
417 GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
423 * Iterator for #GAS_addresses_get_peer_info()
425 * @param cls closure with our `struct AddressIteration *`
426 * @param id the peer id
427 * @param plugin_name plugin name
428 * @param plugin_addr address
429 * @param plugin_addr_len length of @a plugin_addr
430 * @param active is address actively used
431 * @param atsi ats performance information
432 * @param atsi_count number of ats performance elements in @a atsi
433 * @param bandwidth_out current outbound bandwidth assigned to address
434 * @param bandwidth_in current inbound bandwidth assigned to address
437 req_addr_peerinfo_it (void *cls,
438 const struct GNUNET_PeerIdentity *id,
439 const char *plugin_name,
440 const void *plugin_addr, size_t plugin_addr_len,
442 const struct GNUNET_ATS_Information *atsi,
444 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
445 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
447 struct AddressIteration *ai = cls;
449 GNUNET_assert (NULL != ai);
450 GNUNET_assert (NULL != ai->pc);
451 if (NULL == find_client (ai->pc->client))
452 return; /* Client disconnected */
454 if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Address iteration done\n");
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
462 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
465 (unsigned int) ntohl (bandwidth_out.value__),
466 (unsigned int) ntohl (bandwidth_in.value__));
468 /* Transmit result */
469 if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
472 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
473 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
476 (unsigned int) ntohl (bandwidth_out.value__),
477 (unsigned int) ntohl (bandwidth_in.value__));
478 transmit_req_addr (cls,
481 plugin_addr, plugin_addr_len,
485 bandwidth_out, bandwidth_in);
491 * Iterator for GAS_handle_request_address_list
493 * @param cls the client requesting information, a `struct AddressIteration *`
497 req_addr_peer_it (void *cls,
498 const struct GNUNET_PeerIdentity *id)
500 struct AddressIteration *ai = cls;
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Peer iteration done\n");
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Callback for peer `%s'\n",
511 GAS_addresses_get_peer_info (GSA_addresses, id,
512 &req_addr_peerinfo_it, ai);
517 * Handle 'address list request' messages from clients.
519 * @param cls unused, NULL
520 * @param client client that sent the request
521 * @param message the request message
524 GAS_handle_request_address_list (void *cls,
525 struct GNUNET_SERVER_Client *client,
526 const struct GNUNET_MessageHeader *message)
528 struct PerformanceClient *pc;
529 struct AddressIteration ai;
530 struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
531 struct GNUNET_PeerIdentity allzeros;
532 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Received `%s' message\n",
536 "ADDRESSLIST_REQUEST");
537 if (NULL == (pc = find_client(client)))
543 ai.all = ntohl (alrm->all);
544 ai.id = ntohl (alrm->id);
547 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
548 bandwidth_zero.value__ = htonl (0);
549 if (0 == memcmp (&alrm->peer,
551 sizeof (struct GNUNET_PeerIdentity)))
553 /* Return addresses for all peers */
554 GAS_addresses_iterate_peers (GSA_addresses, &req_addr_peer_it, &ai);
555 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0,
556 bandwidth_zero, bandwidth_zero);
560 /* Return addresses for a specific peer */
561 GAS_addresses_get_peer_info (GSA_addresses,
563 &req_addr_peerinfo_it, &ai);
564 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0,
565 bandwidth_zero, bandwidth_zero);
567 GNUNET_SERVER_receive_done (client, GNUNET_OK);
575 GAS_handle_performance_update (struct GNUNET_PeerIdentity *peer,
576 const char *plugin_name,
577 const void *plugin_addr, size_t plugin_addr_len,
579 struct GNUNET_ATS_Information *ats,
581 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
582 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
585 GAS_performance_notify_all_clients (peer,
595 struct PerformanceClient *cur;
596 struct PerformanceMonitorClient *curm;
597 struct MonitorResponseMessage *mrm;
600 msglen = sizeof (struct MonitorResponseMessage) +
601 ats_count * sizeof (struct GNUNET_ATS_Information);
602 mrm = GNUNET_malloc (msglen);
604 mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
605 mrm->header.size = htons (msglen);
606 mrm->ats_count = htonl (ats_count);
608 memcpy (&mrm[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
610 for (cur = pc_head; NULL != cur; cur = cur->next)
611 for (curm = cur->pm_head; NULL != curm; curm = curm->next)
613 /* Notify client about update */
614 mrm->id = htonl (curm->id);
615 GNUNET_SERVER_notification_context_unicast (nc,
617 (struct GNUNET_MessageHeader *) mrm,
626 * Handle 'reservation request' messages from clients.
628 * @param cls unused, NULL
629 * @param client client that sent the request
630 * @param message the request message
633 GAS_handle_reservation_request (void *cls,
634 struct GNUNET_SERVER_Client *client,
635 const struct GNUNET_MessageHeader *message)
637 const struct ReservationRequestMessage *msg =
638 (const struct ReservationRequestMessage *) message;
639 struct ReservationResultMessage result;
641 struct GNUNET_TIME_Relative res_delay;
643 if (NULL == find_client (client))
645 /* missing start message! */
647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
651 "Received `%s' message\n",
652 "RESERVATION_REQUEST");
653 amount = (int32_t) ntohl (msg->amount);
654 res_delay = GAS_reservations_reserve (&msg->peer, amount);
655 if (res_delay.rel_value_us > 0)
657 result.header.size = htons (sizeof (struct ReservationResultMessage));
658 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
659 result.amount = htonl (amount);
660 result.peer = msg->peer;
661 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
662 GNUNET_STATISTICS_update (GSA_stats,
663 "# reservation requests processed", 1,
665 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
667 GNUNET_SERVER_receive_done (client, GNUNET_OK);
672 * Handle 'preference change' messages from clients.
674 * @param cls unused, NULL
675 * @param client client that sent the request
676 * @param message the request message
679 GAS_handle_preference_change (void *cls,
680 struct GNUNET_SERVER_Client *client,
681 const struct GNUNET_MessageHeader *message)
683 const struct ChangePreferenceMessage *msg;
684 const struct PreferenceInformation *pi;
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
690 "PREFERENCE_CHANGE");
691 msize = ntohs (message->size);
692 if (msize < sizeof (struct ChangePreferenceMessage))
695 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
698 msg = (const struct ChangePreferenceMessage *) message;
699 nump = ntohl (msg->num_preferences);
701 sizeof (struct ChangePreferenceMessage) +
702 nump * sizeof (struct PreferenceInformation))
705 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
708 GNUNET_STATISTICS_update (GSA_stats,
709 "# preference change requests processed",
711 pi = (const struct PreferenceInformation *) &msg[1];
712 for (i = 0; i < nump; i++)
713 GAS_addresses_preference_change (GSA_addresses,
716 (enum GNUNET_ATS_PreferenceKind)
717 ntohl (pi[i].preference_kind),
718 pi[i].preference_value);
719 GNUNET_SERVER_receive_done (client, GNUNET_OK);
724 * Handle 'preference feedback' messages from clients.
726 * @param cls unused, NULL
727 * @param client client that sent the request
728 * @param message the request message
731 GAS_handle_preference_feedback (void *cls,
732 struct GNUNET_SERVER_Client *client,
733 const struct GNUNET_MessageHeader *message)
735 const struct FeedbackPreferenceMessage *msg;
736 const struct PreferenceInformation *pi;
741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
742 "Received `%s' message\n",
743 "PREFERENCE_FEEDBACK");
744 msize = ntohs (message->size);
745 if (msize < sizeof (struct FeedbackPreferenceMessage))
748 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
751 msg = (const struct FeedbackPreferenceMessage *) message;
752 nump = ntohl (msg->num_feedback);
754 sizeof (struct FeedbackPreferenceMessage) +
755 nump * sizeof (struct PreferenceInformation))
758 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
761 GNUNET_STATISTICS_update (GSA_stats,
762 "# preference feedbacks requests processed",
764 pi = (const struct PreferenceInformation *) &msg[1];
765 for (i = 0; i < nump; i++)
766 GAS_addresses_preference_feedback (GSA_addresses,
769 GNUNET_TIME_relative_ntoh(msg->scope),
770 (enum GNUNET_ATS_PreferenceKind)
771 ntohl (pi[i].preference_kind),
772 pi[i].preference_value);
773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
779 * Initialize performance subsystem.
781 * @param server handle to our server
782 * @param addresses the address handle to use
785 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
786 struct GAS_Addresses_Handle *addresses)
788 GSA_addresses = addresses;
789 nc = GNUNET_SERVER_notification_context_create (server, 128);
794 * Shutdown performance subsystem.
797 GAS_performance_done ()
799 GNUNET_SERVER_notification_context_destroy (nc);
803 /* end of gnunet-service-ats_performance.c */