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 is this address active
148 * @param atsi performance data for the address
149 * @param atsi_count number of performance records in 'ats'
150 * @param bandwidth_out assigned outbound bandwidth
151 * @param bandwidth_in assigned inbound bandwidth
154 GAS_performance_notify_client (struct PerformanceClient *pc,
155 const struct GNUNET_PeerIdentity *peer,
156 const char *plugin_name,
157 const void *plugin_addr,
158 size_t plugin_addr_len,
160 const struct GNUNET_ATS_Information *atsi,
162 struct GNUNET_BANDWIDTH_Value32NBO
164 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
167 struct PeerInformationMessage *msg;
168 size_t plugin_name_length = strlen (plugin_name) + 1;
170 sizeof (struct PeerInformationMessage) +
171 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
173 char buf[msize] GNUNET_ALIGN;
174 struct GNUNET_ATS_Information *atsp;
177 GNUNET_assert (NULL != pc);
178 if (NULL == find_client (pc->client))
179 return; /* Client disconnected */
181 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
182 GNUNET_assert (atsi_count <
183 GNUNET_SERVER_MAX_MESSAGE_SIZE /
184 sizeof (struct GNUNET_ATS_Information));
185 msg = (struct PeerInformationMessage *) buf;
186 msg->header.size = htons (msize);
187 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
189 msg->ats_count = htonl (atsi_count);
191 msg->address_length = htons (plugin_addr_len);
192 msg->address_active = ntohl (active);
193 msg->plugin_name_length = htons (plugin_name_length);
194 msg->bandwidth_out = bandwidth_out;
195 msg->bandwidth_in = bandwidth_in;
196 atsp = (struct GNUNET_ATS_Information *) &msg[1];
197 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
198 addrp = (char *) &atsp[atsi_count];
199 memcpy (addrp, plugin_addr, plugin_addr_len);
200 strcpy (&addrp[plugin_addr_len], plugin_name);
201 GNUNET_SERVER_notification_context_unicast (nc,
209 * Transmit the given performance information to all performance
212 * @param peer peer for which this is an address suggestion
213 * @param plugin_name 0-termintated string specifying the transport plugin
214 * @param plugin_addr binary address for the plugin to use
215 * @param plugin_addr_len number of bytes in @a plugin_addr
216 * @param active is this address active
217 * @param atsi performance data for the address
218 * @param atsi_count number of performance records in @a atsi
219 * @param bandwidth_out assigned outbound bandwidth
220 * @param bandwidth_in assigned inbound bandwidth
223 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
224 const char *plugin_name,
225 const void *plugin_addr,
226 size_t plugin_addr_len,
228 const struct GNUNET_ATS_Information *atsi,
230 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
231 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
233 struct PerformanceClient *pc;
235 for (pc = pc_head; pc != NULL; pc = pc->next)
236 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
238 GAS_performance_notify_client (pc,
245 bandwidth_out, bandwidth_in);
247 GNUNET_STATISTICS_update (GSA_stats,
248 "# performance updates given to clients", 1,
255 * Iterator for called from #GAS_addresses_get_peer_info()
257 * @param p_it_cls closure with the `struct PerformanceClient *`
258 * @param id the peer id
259 * @param plugin_name plugin name
260 * @param plugin_addr address
261 * @param plugin_addr_len length of @a plugin_addr
262 * @param address_active is address actively used
263 * @param atsi ats performance information
264 * @param atsi_count number of ats performance elements in @a atsi
265 * @param bandwidth_out current outbound bandwidth assigned to address
266 * @param bandwidth_in current inbound bandwidth assigned to address
269 peerinfo_it (void *cls,
270 const struct GNUNET_PeerIdentity *id,
271 const char *plugin_name,
272 const void *plugin_addr, size_t plugin_addr_len,
274 const struct GNUNET_ATS_Information *atsi,
276 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
277 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
279 struct PerformanceClient *pc = cls;
281 GNUNET_assert (NULL != pc);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
285 "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
288 (unsigned int) ntohl (bandwidth_out.value__),
289 (unsigned int) ntohl (bandwidth_in.value__));
290 GAS_performance_notify_client(pc,
297 bandwidth_out, bandwidth_in);
302 * Iterator for #GAS_performance_add_client()
304 * @param cls the client requesting information
309 const struct GNUNET_PeerIdentity *id)
311 struct PerformanceClient *pc = cls;
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Callback for peer `%s'\n",
318 GAS_addresses_get_peer_info (GSA_addresses, id,
325 * Register a new performance client.
327 * @param client handle of the new client
328 * @param flag flag specifying the type of the client
331 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
334 struct PerformanceClient *pc;
335 GNUNET_break (NULL == find_client (client));
337 pc = GNUNET_new (struct PerformanceClient);
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Adding performance client %s PIC\n",
343 (flag == START_FLAG_PERFORMANCE_WITH_PIC) ? "with" : "without");
345 GNUNET_SERVER_notification_context_add (nc, client);
346 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
348 /* Send information about clients */
349 GAS_addresses_iterate_peers (GSA_addresses,
359 transmit_req_addr (struct AddressIteration *ai,
360 const struct GNUNET_PeerIdentity *id,
361 const char *plugin_name,
362 const void *plugin_addr, size_t plugin_addr_len,
364 const struct GNUNET_ATS_Information *atsi,
366 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
367 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
370 struct GNUNET_ATS_Information *atsp;
371 struct PeerInformationMessage *msg;
373 size_t plugin_name_length;
376 if (NULL != plugin_name)
377 plugin_name_length = strlen (plugin_name) + 1;
379 plugin_name_length = 0;
380 msize = sizeof (struct PeerInformationMessage) +
381 atsi_count * sizeof (struct GNUNET_ATS_Information) +
382 plugin_addr_len + plugin_name_length;
383 char buf[msize] GNUNET_ALIGN;
385 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
386 GNUNET_assert (atsi_count <
387 GNUNET_SERVER_MAX_MESSAGE_SIZE /
388 sizeof (struct GNUNET_ATS_Information));
389 msg = (struct PeerInformationMessage *) buf;
390 msg->header.size = htons (msize);
391 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
392 msg->ats_count = htonl (atsi_count);
393 msg->id = htonl (ai->id);
397 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
398 msg->address_length = htons (plugin_addr_len);
399 msg->address_active = ntohl (active);
400 msg->plugin_name_length = htons (plugin_name_length);
401 msg->bandwidth_out = bandwidth_out;
402 msg->bandwidth_in = bandwidth_in;
403 atsp = (struct GNUNET_ATS_Information *) &msg[1];
404 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
405 addrp = (char *) &atsp[atsi_count];
406 if (NULL != plugin_addr)
407 memcpy (addrp, plugin_addr, plugin_addr_len);
408 if (NULL != plugin_name)
409 strcpy (&addrp[plugin_addr_len], plugin_name);
410 GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
416 * Iterator for #GAS_addresses_get_peer_info()
418 * @param p_it_cls closure with our `struct AddressIteration *`
419 * @param id the peer id
420 * @param plugin_name plugin name
421 * @param plugin_addr address
422 * @param plugin_addr_len length of @a plugin_addr
423 * @param address_active is address actively used
424 * @param atsi ats performance information
425 * @param atsi_count number of ats performance elements in @a atsi
426 * @param bandwidth_out current outbound bandwidth assigned to address
427 * @param bandwidth_in current inbound bandwidth assigned to address
430 req_addr_peerinfo_it (void *cls,
431 const struct GNUNET_PeerIdentity *id,
432 const char *plugin_name,
433 const void *plugin_addr, size_t plugin_addr_len,
435 const struct GNUNET_ATS_Information *atsi,
437 struct GNUNET_BANDWIDTH_Value32NBO
439 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
441 struct AddressIteration *ai = cls;
443 GNUNET_assert (NULL != ai);
444 GNUNET_assert (NULL != ai->pc);
445 if (NULL == find_client (ai->pc->client))
446 return; /* Client disconnected */
448 if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "Address iteration done\n");
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
456 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
459 (unsigned int) ntohl (bandwidth_out.value__),
460 (unsigned int) ntohl (bandwidth_in.value__));
462 /* Transmit result */
463 if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
467 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
470 (unsigned int) ntohl (bandwidth_out.value__),
471 (unsigned int) ntohl (bandwidth_in.value__));
472 transmit_req_addr (cls,
475 plugin_addr, plugin_addr_len,
479 bandwidth_out, bandwidth_in);
485 * Iterator for GAS_handle_request_address_list
487 * @param cls the client requesting information, a `struct AddressIteration *`
491 req_addr_peer_it (void *cls,
492 const struct GNUNET_PeerIdentity *id)
494 struct AddressIteration *ai = cls;
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
499 "Peer iteration done\n");
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Callback for peer `%s'\n",
505 GAS_addresses_get_peer_info (GSA_addresses, id,
506 &req_addr_peerinfo_it, ai);
511 * Handle 'address list request' messages from clients.
513 * @param cls unused, NULL
514 * @param client client that sent the request
515 * @param message the request message
518 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
519 const struct GNUNET_MessageHeader *message)
521 struct PerformanceClient *pc;
522 struct AddressIteration ai;
523 struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
524 struct GNUNET_PeerIdentity allzeros;
525 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Received `%s' message\n",
529 "ADDRESSLIST_REQUEST");
530 if (NULL == (pc = find_client(client)))
536 ai.all = ntohl (alrm->all);
537 ai.id = ntohl (alrm->id);
540 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
541 bandwidth_zero.value__ = htonl (0);
542 if (0 == memcmp (&alrm->peer,
544 sizeof (struct GNUNET_PeerIdentity)))
546 /* Return addresses for all peers */
547 GAS_addresses_iterate_peers (GSA_addresses, &req_addr_peer_it, &ai);
548 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0,
549 bandwidth_zero, bandwidth_zero);
553 /* Return addresses for a specific peer */
554 GAS_addresses_get_peer_info (GSA_addresses,
556 &req_addr_peerinfo_it, &ai);
557 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0,
558 bandwidth_zero, bandwidth_zero);
560 GNUNET_SERVER_receive_done (client, GNUNET_OK);
568 GAS_handle_performance_update (struct GNUNET_PeerIdentity *peer,
569 const char *plugin_name,
570 const void *plugin_addr, size_t plugin_addr_len,
572 struct GNUNET_ATS_Information *ats,
574 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
575 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
578 GAS_performance_notify_all_clients (peer, plugin_name, plugin_addr,
579 plugin_addr_len, active, ats, ats_count, bandwidth_out, bandwidth_in);
582 struct PerformanceClient *cur;
583 struct PerformanceMonitorClient *curm;
584 struct MonitorResponseMessage *mrm;
587 msglen = sizeof (struct MonitorResponseMessage) +
588 ats_count * sizeof (struct GNUNET_ATS_Information);
589 mrm = GNUNET_malloc (msglen);
591 mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
592 mrm->header.size = htons (msglen);
593 mrm->ats_count = htonl (ats_count);
595 memcpy (&mrm[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
597 for (cur = pc_head; NULL != cur; cur = cur->next)
598 for (curm = cur->pm_head; NULL != curm; curm = curm->next)
600 /* Notify client about update */
601 mrm->id = htonl (curm->id);
602 GNUNET_SERVER_notification_context_unicast (nc,
604 (struct GNUNET_MessageHeader *) mrm,
613 * Handle 'reservation request' messages from clients.
615 * @param cls unused, NULL
616 * @param client client that sent the request
617 * @param message the request message
620 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
621 const struct GNUNET_MessageHeader *message)
623 const struct ReservationRequestMessage *msg =
624 (const struct ReservationRequestMessage *) message;
625 struct ReservationResultMessage result;
627 struct GNUNET_TIME_Relative res_delay;
629 if (NULL == find_client (client))
631 /* missing start message! */
633 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
637 "Received `%s' message\n",
638 "RESERVATION_REQUEST");
639 amount = (int32_t) ntohl (msg->amount);
640 res_delay = GAS_reservations_reserve (&msg->peer, amount);
641 if (res_delay.rel_value_us > 0)
643 result.header.size = htons (sizeof (struct ReservationResultMessage));
644 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
645 result.amount = htonl (amount);
646 result.peer = msg->peer;
647 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
648 GNUNET_STATISTICS_update (GSA_stats,
649 "# reservation requests processed", 1,
651 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
653 GNUNET_SERVER_receive_done (client, GNUNET_OK);
658 * Handle 'preference change' messages from clients.
660 * @param cls unused, NULL
661 * @param client client that sent the request
662 * @param message the request message
665 GAS_handle_preference_change (void *cls,
666 struct GNUNET_SERVER_Client *client,
667 const struct GNUNET_MessageHeader *message)
669 const struct ChangePreferenceMessage *msg;
670 const struct PreferenceInformation *pi;
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
676 "PREFERENCE_CHANGE");
677 msize = ntohs (message->size);
678 if (msize < sizeof (struct ChangePreferenceMessage))
681 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
684 msg = (const struct ChangePreferenceMessage *) message;
685 nump = ntohl (msg->num_preferences);
687 sizeof (struct ChangePreferenceMessage) +
688 nump * sizeof (struct PreferenceInformation))
691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
694 GNUNET_STATISTICS_update (GSA_stats,
695 "# preference change requests processed",
697 pi = (const struct PreferenceInformation *) &msg[1];
698 for (i = 0; i < nump; i++)
699 GAS_addresses_preference_change (GSA_addresses,
702 (enum GNUNET_ATS_PreferenceKind)
703 ntohl (pi[i].preference_kind),
704 pi[i].preference_value);
705 GNUNET_SERVER_receive_done (client, GNUNET_OK);
710 * Handle 'preference feedback' messages from clients.
712 * @param cls unused, NULL
713 * @param client client that sent the request
714 * @param message the request message
717 GAS_handle_preference_feedback (void *cls,
718 struct GNUNET_SERVER_Client *client,
719 const struct GNUNET_MessageHeader *message)
721 const struct FeedbackPreferenceMessage *msg;
722 const struct PreferenceInformation *pi;
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728 "Received `%s' message\n",
729 "PREFERENCE_FEEDBACK");
730 msize = ntohs (message->size);
731 if (msize < sizeof (struct FeedbackPreferenceMessage))
734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
737 msg = (const struct FeedbackPreferenceMessage *) message;
738 nump = ntohl (msg->num_feedback);
740 sizeof (struct FeedbackPreferenceMessage) +
741 nump * sizeof (struct PreferenceInformation))
744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
747 GNUNET_STATISTICS_update (GSA_stats,
748 "# preference feedbacks requests processed",
750 pi = (const struct PreferenceInformation *) &msg[1];
751 for (i = 0; i < nump; i++)
752 GAS_addresses_preference_feedback (GSA_addresses,
755 GNUNET_TIME_relative_ntoh(msg->scope),
756 (enum GNUNET_ATS_PreferenceKind)
757 ntohl (pi[i].preference_kind),
758 pi[i].preference_value);
759 GNUNET_SERVER_receive_done (client, GNUNET_OK);
765 * Initialize performance subsystem.
767 * @param server handle to our server
768 * @param addresses the address handle to use
771 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
772 struct GAS_Addresses_Handle *addresses)
774 GSA_addresses = addresses;
775 nc = GNUNET_SERVER_notification_context_create (server, 128);
780 * Shutdown performance subsystem.
783 GAS_performance_done ()
785 GNUNET_SERVER_notification_context_destroy (nc);
789 /* end of gnunet-service-ats_performance.c */