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"
34 struct PerformanceMonitorClient
36 struct PerformanceMonitorClient *next;
37 struct PerformanceMonitorClient *prev;
39 struct GNUNET_SERVER_Client *client;
45 * We keep clients that are interested in performance in a linked list.
47 struct PerformanceClient
50 * Next in doubly-linked list.
52 struct PerformanceClient *next;
55 * Previous in doubly-linked list.
57 struct PerformanceClient *prev;
59 struct PerformanceMonitorClient *pm_head;
61 struct PerformanceMonitorClient *pm_tail;
64 * Actual handle to the client.
66 struct GNUNET_SERVER_Client *client;
69 * Options for the client.
77 * We keep clients that are interested in performance in a linked list.
79 struct AddressIteration
82 * Actual handle to the client.
84 struct PerformanceClient *pc;
90 unsigned int msg_type;
96 static struct GAS_Addresses_Handle *GSA_addresses;
99 * Head of linked list of all clients to this service.
101 static struct PerformanceClient *pc_head;
104 * Tail of linked list of all clients to this service.
106 static struct PerformanceClient *pc_tail;
109 * Context for sending messages to performance clients.
111 static struct GNUNET_SERVER_NotificationContext *nc;
115 * Find the performance client associated with the given handle.
117 * @param client server handle
118 * @return internal handle
120 static struct PerformanceClient *
121 find_client (struct GNUNET_SERVER_Client *client)
123 struct PerformanceClient *pc;
125 for (pc = pc_head; pc != NULL; pc = pc->next)
126 if (pc->client == client)
132 * Unregister a client (which may have been a performance client,
133 * but this is not assured).
135 * @param client handle of the (now dead) client
138 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
140 struct PerformanceClient *pc;
141 struct PerformanceMonitorClient *cur;
142 struct PerformanceMonitorClient *next;
143 pc = find_client (client);
147 while (NULL != (cur = next))
150 GNUNET_CONTAINER_DLL_remove (pc->pm_head, pc->pm_tail, cur);
153 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
154 GNUNET_SERVER_client_drop (client);
159 * Transmit the given performance information to all performance
162 * @param pc performance client to send to
163 * @param peer peer for which this is an address suggestion
164 * @param plugin_name 0-termintated string specifying the transport plugin
165 * @param plugin_addr binary address for the plugin to use
166 * @param plugin_addr_len number of bytes in plugin_addr
167 * @param active is this address active
168 * @param atsi performance data for the address
169 * @param atsi_count number of performance records in 'ats'
170 * @param bandwidth_out assigned outbound bandwidth
171 * @param bandwidth_in assigned inbound bandwidth
174 GAS_performance_notify_client (struct PerformanceClient *pc,
175 const struct GNUNET_PeerIdentity *peer,
176 const char *plugin_name,
177 const void *plugin_addr, size_t plugin_addr_len,
179 const struct GNUNET_ATS_Information *atsi,
181 struct GNUNET_BANDWIDTH_Value32NBO
183 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
186 struct PeerInformationMessage *msg;
187 size_t plugin_name_length = strlen (plugin_name) + 1;
189 sizeof (struct PeerInformationMessage) +
190 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
192 char buf[msize] GNUNET_ALIGN;
193 struct GNUNET_ATS_Information *atsp;
196 GNUNET_assert (NULL != pc);
197 if (NULL == find_client (pc->client))
198 return; /* Client disconnected */
200 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
201 GNUNET_assert (atsi_count <
202 GNUNET_SERVER_MAX_MESSAGE_SIZE /
203 sizeof (struct GNUNET_ATS_Information));
204 msg = (struct PeerInformationMessage *) buf;
205 msg->header.size = htons (msize);
206 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
208 msg->ats_count = htonl (atsi_count);
210 msg->address_length = htons (plugin_addr_len);
211 msg->address_active = ntohl (active);
212 msg->plugin_name_length = htons (plugin_name_length);
213 msg->bandwidth_out = bandwidth_out;
214 msg->bandwidth_in = bandwidth_in;
215 atsp = (struct GNUNET_ATS_Information *) &msg[1];
216 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
217 addrp = (char *) &atsp[atsi_count];
218 memcpy (addrp, plugin_addr, plugin_addr_len);
219 strcpy (&addrp[plugin_addr_len], plugin_name);
220 GNUNET_SERVER_notification_context_unicast (nc, pc->client, &msg->header,
226 * Transmit the given performance information to all performance
229 * @param peer peer for which this is an address suggestion
230 * @param plugin_name 0-termintated string specifying the transport plugin
231 * @param plugin_addr binary address for the plugin to use
232 * @param plugin_addr_len number of bytes in plugin_addr
233 * @param active is this address active
234 * @param atsi performance data for the address
235 * @param atsi_count number of performance records in 'ats'
236 * @param bandwidth_out assigned outbound bandwidth
237 * @param bandwidth_in assigned inbound bandwidth
240 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
241 const char *plugin_name,
242 const void *plugin_addr, size_t plugin_addr_len,
244 const struct GNUNET_ATS_Information *atsi,
246 struct GNUNET_BANDWIDTH_Value32NBO
248 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
250 struct PerformanceClient *pc;
252 for (pc = pc_head; pc != NULL; pc = pc->next)
253 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
255 GAS_performance_notify_client (pc,
257 plugin_name, plugin_addr, plugin_addr_len,
260 bandwidth_out, bandwidth_in);
262 GNUNET_STATISTICS_update (GSA_stats,
263 "# performance updates given to clients", 1,
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
278 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
280 struct PerformanceClient *pc = cls;
281 GNUNET_assert (NULL != pc);
285 if (GNUNET_NO == active)
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Callback for peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
292 ntohl (bandwidth_out.value__),
293 ntohl (bandwidth_in.value__));
294 GAS_performance_notify_client(pc,
296 plugin_name, plugin_addr, plugin_addr_len,
299 bandwidth_out, bandwidth_in);
304 * Iterator for GAS_performance_add_client
306 * @param cls the client requesting information
311 const struct GNUNET_PeerIdentity *id)
313 struct PerformanceClient *pc = cls;
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
317 GAS_addresses_get_peer_info (GSA_addresses, id, &peerinfo_it, pc);
322 * Register a new performance client.
324 * @param client handle of the new client
325 * @param flag flag specifying the type of the client
328 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
331 struct PerformanceClient *pc;
332 GNUNET_break (NULL == find_client (client));
334 pc = GNUNET_malloc (sizeof (struct PerformanceClient));
337 GNUNET_SERVER_notification_context_add (nc, client);
338 GNUNET_SERVER_client_keep (client);
339 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
341 /* Send information about clients */
342 GAS_addresses_iterate_peers (GSA_addresses, &peer_it, pc);
345 static void transmit_req_addr (struct AddressIteration *ai,
346 const struct GNUNET_PeerIdentity *id,
347 const char *plugin_name,
348 const void *plugin_addr, size_t plugin_addr_len,
350 const struct GNUNET_ATS_Information *atsi,
352 struct GNUNET_BANDWIDTH_Value32NBO
354 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
358 struct GNUNET_ATS_Information *atsp;
359 struct PeerInformationMessage *msg;
361 size_t plugin_name_length;
364 if (NULL != plugin_name)
365 plugin_name_length = strlen (plugin_name) + 1;
367 plugin_name_length = 0;
368 msize = sizeof (struct PeerInformationMessage) +
369 atsi_count * sizeof (struct GNUNET_ATS_Information) +
370 plugin_addr_len + plugin_name_length;
371 char buf[msize] GNUNET_ALIGN;
373 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
374 GNUNET_assert (atsi_count <
375 GNUNET_SERVER_MAX_MESSAGE_SIZE /
376 sizeof (struct GNUNET_ATS_Information));
377 msg = (struct PeerInformationMessage *) buf;
378 msg->header.size = htons (msize);
379 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
380 msg->ats_count = htonl (atsi_count);
381 msg->id = htonl (ai->id);
385 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
386 msg->address_length = htons (plugin_addr_len);
387 msg->address_active = ntohl (active);
388 msg->plugin_name_length = htons (plugin_name_length);
389 msg->bandwidth_out = bandwidth_out;
390 msg->bandwidth_in = bandwidth_in;
391 atsp = (struct GNUNET_ATS_Information *) &msg[1];
392 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
393 addrp = (char *) &atsp[atsi_count];
394 if (NULL != plugin_addr)
395 memcpy (addrp, plugin_addr, plugin_addr_len);
396 if (NULL != plugin_name)
397 strcpy (&addrp[plugin_addr_len], plugin_name);
398 GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
403 req_addr_peerinfo_it (void *cls,
404 const struct GNUNET_PeerIdentity *id,
405 const char *plugin_name,
406 const void *plugin_addr, size_t plugin_addr_len,
408 const struct GNUNET_ATS_Information *atsi,
410 struct GNUNET_BANDWIDTH_Value32NBO
412 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
414 struct AddressIteration *ai = cls;
416 GNUNET_assert (NULL != ai);
417 GNUNET_assert (NULL != ai->pc);
418 if (NULL == find_client (ai->pc->client))
419 return; /* Client disconnected */
421 if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Address iteration done\n");
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u \n",
429 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
432 (unsigned int) ntohl (bandwidth_out.value__),
433 (unsigned int) ntohl (bandwidth_in.value__));
435 /* Transmit result */
436 if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u \n",
440 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
443 (unsigned int) ntohl (bandwidth_out.value__),
444 (unsigned int) ntohl (bandwidth_in.value__));
445 transmit_req_addr (cls,
448 plugin_addr, plugin_addr_len,
452 bandwidth_out, bandwidth_in);
458 * Iterator for GAS_handle_request_address_list
460 * @param cls the client requesting information
464 req_addr_peer_it (void *cls,
465 const struct GNUNET_PeerIdentity *id)
467 struct AddressIteration *ai = cls;
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
471 GAS_addresses_get_peer_info (GSA_addresses, id, &req_addr_peerinfo_it, ai);
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer iteration done\n");
480 * Handle 'address list request' messages from clients.
482 * @param cls unused, NULL
483 * @param client client that sent the request
484 * @param message the request message
487 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
488 const struct GNUNET_MessageHeader *message)
490 struct PerformanceClient *pc;
491 struct AddressIteration ai;
492 struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
493 struct GNUNET_PeerIdentity allzeros;
494 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
497 "ADDRESSLIST_REQUEST");
499 if (NULL == (pc = find_client(client)))
505 ai.all = ntohl (alrm->all);
506 ai.id = ntohl (alrm->id);
509 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
510 bandwidth_zero.value__ = htonl (0);
511 if (0 == memcmp (&alrm->peer, &allzeros, sizeof (struct GNUNET_PeerIdentity)))
513 /* Return addresses for all peers */
514 GAS_addresses_iterate_peers (GSA_addresses, &req_addr_peer_it, &ai);
515 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
519 /* Return addresses for a specific peer */
520 GAS_addresses_get_peer_info (GSA_addresses, &alrm->peer, &req_addr_peerinfo_it, &ai);
521 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
528 GAS_handle_performance_update (struct GNUNET_PeerIdentity *peer,
529 struct GNUNET_ATS_Information *ats,
532 struct PerformanceClient *cur;
533 struct PerformanceMonitorClient *curm;
534 struct MonitorResponseMessage *mrm;
537 msglen = sizeof (struct MonitorResponseMessage) +
538 ats_count * sizeof (struct GNUNET_ATS_Information);
539 mrm = GNUNET_malloc (msglen);
541 mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
542 mrm->header.size = htons (msglen);
543 mrm->ats_count = htonl (ats_count);
546 for (cur = pc_head; NULL != cur; cur = cur->next)
547 for (curm = cur->pm_head; NULL != curm; curm = curm->next)
549 /* Notify client about update */
551 GNUNET_SERVER_notification_context_unicast (nc,
553 (struct GNUNET_MessageHeader *) mrm,
561 mon_peerinfo_it (void *cls,
562 const struct GNUNET_PeerIdentity *id,
563 const char *plugin_name,
564 const void *plugin_addr, size_t plugin_addr_len,
566 const struct GNUNET_ATS_Information *atsi,
568 struct GNUNET_BANDWIDTH_Value32NBO
570 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
572 struct PerformanceMonitorClient *pmc = cls;
573 struct MonitorResponseMessage *mrm;
577 return; /* last callback */
579 msglen = sizeof (struct MonitorResponseMessage) +
580 atsi_count * sizeof (struct GNUNET_ATS_Information);
581 mrm = GNUNET_malloc (msglen);
583 mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
584 mrm->header.size = htons (msglen);
585 mrm->ats_count = htonl (atsi_count);
589 /* Send initial information about peers to client */
591 GNUNET_SERVER_notification_context_unicast (nc,
593 (struct GNUNET_MessageHeader *) mrm,
600 * Iterator for GAS_handle_monitor
602 * @param cls the performance monitoring client requesting information
606 mon_peer_it (void *cls,
607 const struct GNUNET_PeerIdentity *id)
609 struct PerformanceMonitorClient *pmc = cls;
611 GAS_addresses_get_peer_info (GSA_addresses, id, &mon_peerinfo_it, pmc);
616 GAS_handle_monitor (void *cls,
617 struct GNUNET_SERVER_Client *client,
618 const struct GNUNET_MessageHeader *message)
620 struct PerformanceClient *pc;
621 struct PerformanceMonitorClient *pmc;
622 struct MonitorMessage *mm = (struct MonitorMessage *) message;
627 pc = find_client (client);
634 msg_size = ntohs (message->size);
635 if (msg_size < sizeof (struct MonitorMessage))
637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
644 for (pmc = pc->pm_head; NULL != pmc; pmc = pmc->next)
645 if ((pmc->id == id) && (client == pmc->client))
648 if (GNUNET_YES == op)
650 /* Start monitoring */
654 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
655 return; /* Duplicate*/
657 pmc = GNUNET_malloc (sizeof (struct PerformanceMonitorClient));
658 pmc->client = client;
660 GNUNET_CONTAINER_DLL_insert (pc->pm_head, pc->pm_tail, pmc);
661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
662 "Added performance monitoring client %p id %u\n",
665 /* Return all values here */
666 GAS_addresses_iterate_peers (GSA_addresses, &mon_peer_it, pc);
669 else if (GNUNET_NO == op)
671 /* Stop monitoring */
675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
676 return; /* Not existing */
678 GNUNET_CONTAINER_DLL_remove (pc->pm_head, pc->pm_tail, pmc);
680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
681 "Removed performance monitoring client %p id %u\n",
687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
690 GNUNET_SERVER_receive_done (client, GNUNET_OK);
695 * Handle 'reservation request' messages from clients.
697 * @param cls unused, NULL
698 * @param client client that sent the request
699 * @param message the request message
702 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
703 const struct GNUNET_MessageHeader *message)
705 const struct ReservationRequestMessage *msg =
706 (const struct ReservationRequestMessage *) message;
707 struct ReservationResultMessage result;
709 struct GNUNET_TIME_Relative res_delay;
711 if (NULL == find_client (client))
713 /* missing start message! */
715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
719 "RESERVATION_REQUEST");
720 amount = (int32_t) ntohl (msg->amount);
721 res_delay = GAS_reservations_reserve (&msg->peer, amount);
722 if (res_delay.rel_value > 0)
724 result.header.size = htons (sizeof (struct ReservationResultMessage));
725 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
726 result.amount = htonl (amount);
727 result.peer = msg->peer;
728 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
729 GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
731 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
733 GNUNET_SERVER_receive_done (client, GNUNET_OK);
738 * Handle 'preference change' messages from clients.
740 * @param cls unused, NULL
741 * @param client client that sent the request
742 * @param message the request message
745 GAS_handle_preference_change (void *cls,
746 struct GNUNET_SERVER_Client *client,
747 const struct GNUNET_MessageHeader *message)
749 const struct ChangePreferenceMessage *msg;
750 const struct PreferenceInformation *pi;
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
756 "PREFERENCE_CHANGE");
757 msize = ntohs (message->size);
758 if (msize < sizeof (struct ChangePreferenceMessage))
761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
764 msg = (const struct ChangePreferenceMessage *) message;
765 nump = ntohl (msg->num_preferences);
767 sizeof (struct ChangePreferenceMessage) +
768 nump * sizeof (struct PreferenceInformation))
771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
774 GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
776 pi = (const struct PreferenceInformation *) &msg[1];
777 for (i = 0; i < nump; i++)
778 GAS_addresses_change_preference (GSA_addresses,
781 (enum GNUNET_ATS_PreferenceKind)
782 ntohl (pi[i].preference_kind),
783 pi[i].preference_value);
784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
789 * Initialize performance subsystem.
791 * @param server handle to our server
792 * @param addresses the address handle to use
795 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
796 struct GAS_Addresses_Handle *addresses)
798 GSA_addresses = addresses;
799 nc = GNUNET_SERVER_notification_context_create (server, 128);
804 * Shutdown performance subsystem.
807 GAS_performance_done ()
809 GNUNET_SERVER_notification_context_destroy (nc);
813 /* end of gnunet-service-ats_performance.c */