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)
118 * Unregister a client (which may have been a performance client,
119 * but this is not assured).
121 * @param client handle of the (now dead) client
124 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
126 struct PerformanceClient *pc;
128 pc = find_client (client);
131 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
132 GAS_addresses_preference_client_disconnect (GSA_addresses, client);
137 * Transmit the given performance information to all performance
140 * @param pc performance client to send to
141 * @param peer peer for which this is an address suggestion
142 * @param plugin_name 0-termintated string specifying the transport plugin
143 * @param plugin_addr binary address for the plugin to use
144 * @param plugin_addr_len number of bytes in plugin_addr
145 * @param active is this address active
146 * @param atsi performance data for the address
147 * @param atsi_count number of performance records in 'ats'
148 * @param bandwidth_out assigned outbound bandwidth
149 * @param bandwidth_in assigned inbound bandwidth
152 GAS_performance_notify_client (struct PerformanceClient *pc,
153 const struct GNUNET_PeerIdentity *peer,
154 const char *plugin_name,
155 const void *plugin_addr, size_t plugin_addr_len,
157 const struct GNUNET_ATS_Information *atsi,
159 struct GNUNET_BANDWIDTH_Value32NBO
161 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
164 struct PeerInformationMessage *msg;
165 size_t plugin_name_length = strlen (plugin_name) + 1;
167 sizeof (struct PeerInformationMessage) +
168 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
170 char buf[msize] GNUNET_ALIGN;
171 struct GNUNET_ATS_Information *atsp;
174 GNUNET_assert (NULL != pc);
175 if (NULL == find_client (pc->client))
176 return; /* Client disconnected */
178 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
179 GNUNET_assert (atsi_count <
180 GNUNET_SERVER_MAX_MESSAGE_SIZE /
181 sizeof (struct GNUNET_ATS_Information));
182 msg = (struct PeerInformationMessage *) buf;
183 msg->header.size = htons (msize);
184 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
186 msg->ats_count = htonl (atsi_count);
188 msg->address_length = htons (plugin_addr_len);
189 msg->address_active = ntohl (active);
190 msg->plugin_name_length = htons (plugin_name_length);
191 msg->bandwidth_out = bandwidth_out;
192 msg->bandwidth_in = bandwidth_in;
193 atsp = (struct GNUNET_ATS_Information *) &msg[1];
194 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
195 addrp = (char *) &atsp[atsi_count];
196 memcpy (addrp, plugin_addr, plugin_addr_len);
197 strcpy (&addrp[plugin_addr_len], plugin_name);
198 GNUNET_SERVER_notification_context_unicast (nc, pc->client, &msg->header,
204 * Transmit the given performance information to all performance
207 * @param peer peer for which this is an address suggestion
208 * @param plugin_name 0-termintated string specifying the transport plugin
209 * @param plugin_addr binary address for the plugin to use
210 * @param plugin_addr_len number of bytes in plugin_addr
211 * @param active is this address active
212 * @param atsi performance data for the address
213 * @param atsi_count number of performance records in 'ats'
214 * @param bandwidth_out assigned outbound bandwidth
215 * @param bandwidth_in assigned inbound bandwidth
218 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
219 const char *plugin_name,
220 const void *plugin_addr, size_t plugin_addr_len,
222 const struct GNUNET_ATS_Information *atsi,
224 struct GNUNET_BANDWIDTH_Value32NBO
226 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
228 struct PerformanceClient *pc;
229 for (pc = pc_head; pc != NULL; pc = pc->next)
230 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
232 GAS_performance_notify_client (pc,
234 plugin_name, plugin_addr, plugin_addr_len,
237 bandwidth_out, bandwidth_in);
239 GNUNET_STATISTICS_update (GSA_stats,
240 "# performance updates given to clients", 1,
246 peerinfo_it (void *cls,
247 const struct GNUNET_PeerIdentity *id,
248 const char *plugin_name,
249 const void *plugin_addr, size_t plugin_addr_len,
251 const struct GNUNET_ATS_Information *atsi,
253 struct GNUNET_BANDWIDTH_Value32NBO
255 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
257 struct PerformanceClient *pc = cls;
258 GNUNET_assert (NULL != pc);
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "Callback for peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
266 ntohl (bandwidth_out.value__),
267 ntohl (bandwidth_in.value__));
268 GAS_performance_notify_client(pc,
275 bandwidth_out, bandwidth_in);
280 * Iterator for GAS_performance_add_client
282 * @param cls the client requesting information
287 const struct GNUNET_PeerIdentity *id)
289 struct PerformanceClient *pc = cls;
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
293 GAS_addresses_get_peer_info (GSA_addresses, id, &peerinfo_it, pc);
298 * Register a new performance client.
300 * @param client handle of the new client
301 * @param flag flag specifying the type of the client
304 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
307 struct PerformanceClient *pc;
308 GNUNET_break (NULL == find_client (client));
310 pc = GNUNET_new (struct PerformanceClient);
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding performance client %s PIC\n",
315 (flag == START_FLAG_PERFORMANCE_WITH_PIC) ? "with" : "without");
317 GNUNET_SERVER_notification_context_add (nc, client);
318 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
320 /* Send information about clients */
321 GAS_addresses_iterate_peers (GSA_addresses, &peer_it, pc);
325 static void transmit_req_addr (struct AddressIteration *ai,
326 const struct GNUNET_PeerIdentity *id,
327 const char *plugin_name,
328 const void *plugin_addr, size_t plugin_addr_len,
330 const struct GNUNET_ATS_Information *atsi,
332 struct GNUNET_BANDWIDTH_Value32NBO
334 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
337 struct GNUNET_ATS_Information *atsp;
338 struct PeerInformationMessage *msg;
340 size_t plugin_name_length;
343 if (NULL != plugin_name)
344 plugin_name_length = strlen (plugin_name) + 1;
346 plugin_name_length = 0;
347 msize = sizeof (struct PeerInformationMessage) +
348 atsi_count * sizeof (struct GNUNET_ATS_Information) +
349 plugin_addr_len + plugin_name_length;
350 char buf[msize] GNUNET_ALIGN;
352 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
353 GNUNET_assert (atsi_count <
354 GNUNET_SERVER_MAX_MESSAGE_SIZE /
355 sizeof (struct GNUNET_ATS_Information));
356 msg = (struct PeerInformationMessage *) buf;
357 msg->header.size = htons (msize);
358 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
359 msg->ats_count = htonl (atsi_count);
360 msg->id = htonl (ai->id);
364 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
365 msg->address_length = htons (plugin_addr_len);
366 msg->address_active = ntohl (active);
367 msg->plugin_name_length = htons (plugin_name_length);
368 msg->bandwidth_out = bandwidth_out;
369 msg->bandwidth_in = bandwidth_in;
370 atsp = (struct GNUNET_ATS_Information *) &msg[1];
371 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
372 addrp = (char *) &atsp[atsi_count];
373 if (NULL != plugin_addr)
374 memcpy (addrp, plugin_addr, plugin_addr_len);
375 if (NULL != plugin_name)
376 strcpy (&addrp[plugin_addr_len], plugin_name);
377 GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
382 req_addr_peerinfo_it (void *cls,
383 const struct GNUNET_PeerIdentity *id,
384 const char *plugin_name,
385 const void *plugin_addr, size_t plugin_addr_len,
387 const struct GNUNET_ATS_Information *atsi,
389 struct GNUNET_BANDWIDTH_Value32NBO
391 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
393 struct AddressIteration *ai = cls;
395 GNUNET_assert (NULL != ai);
396 GNUNET_assert (NULL != ai->pc);
397 if (NULL == find_client (ai->pc->client))
398 return; /* Client disconnected */
400 if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Address iteration done\n");
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u \n",
408 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
411 (unsigned int) ntohl (bandwidth_out.value__),
412 (unsigned int) ntohl (bandwidth_in.value__));
414 /* Transmit result */
415 if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u \n",
419 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
422 (unsigned int) ntohl (bandwidth_out.value__),
423 (unsigned int) ntohl (bandwidth_in.value__));
424 transmit_req_addr (cls,
427 plugin_addr, plugin_addr_len,
431 bandwidth_out, bandwidth_in);
437 * Iterator for GAS_handle_request_address_list
439 * @param cls the client requesting information
443 req_addr_peer_it (void *cls,
444 const struct GNUNET_PeerIdentity *id)
446 struct AddressIteration *ai = cls;
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
450 GAS_addresses_get_peer_info (GSA_addresses, id, &req_addr_peerinfo_it, ai);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer iteration done\n");
460 * Handle 'address list request' messages from clients.
462 * @param cls unused, NULL
463 * @param client client that sent the request
464 * @param message the request message
467 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
468 const struct GNUNET_MessageHeader *message)
470 struct PerformanceClient *pc;
471 struct AddressIteration ai;
472 struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
473 struct GNUNET_PeerIdentity allzeros;
474 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
477 "ADDRESSLIST_REQUEST");
479 if (NULL == (pc = find_client(client)))
485 ai.all = ntohl (alrm->all);
486 ai.id = ntohl (alrm->id);
489 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
490 bandwidth_zero.value__ = htonl (0);
491 if (0 == memcmp (&alrm->peer, &allzeros, sizeof (struct GNUNET_PeerIdentity)))
493 /* Return addresses for all peers */
494 GAS_addresses_iterate_peers (GSA_addresses, &req_addr_peer_it, &ai);
495 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
499 /* Return addresses for a specific peer */
500 GAS_addresses_get_peer_info (GSA_addresses, &alrm->peer, &req_addr_peerinfo_it, &ai);
501 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
503 GNUNET_SERVER_receive_done (client, GNUNET_OK);
508 GAS_handle_performance_update (struct GNUNET_PeerIdentity *peer,
509 const char *plugin_name,
510 const void *plugin_addr,
511 size_t plugin_addr_len,
513 struct GNUNET_ATS_Information *ats,
515 struct GNUNET_BANDWIDTH_Value32NBO
517 struct GNUNET_BANDWIDTH_Value32NBO
521 GAS_performance_notify_all_clients (peer,
523 plugin_addr, plugin_addr_len,
526 bandwidth_out, bandwidth_in);
529 struct PerformanceClient *cur;
530 struct PerformanceMonitorClient *curm;
531 struct MonitorResponseMessage *mrm;
534 msglen = sizeof (struct MonitorResponseMessage) +
535 ats_count * sizeof (struct GNUNET_ATS_Information);
536 mrm = GNUNET_malloc (msglen);
538 mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
539 mrm->header.size = htons (msglen);
540 mrm->ats_count = htonl (ats_count);
542 memcpy (&mrm[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
544 for (cur = pc_head; NULL != cur; cur = cur->next)
545 for (curm = cur->pm_head; NULL != curm; curm = curm->next)
547 /* Notify client about update */
548 mrm->id = htonl (curm->id);
549 GNUNET_SERVER_notification_context_unicast (nc,
551 (struct GNUNET_MessageHeader *) mrm,
560 * Handle 'reservation request' messages from clients.
562 * @param cls unused, NULL
563 * @param client client that sent the request
564 * @param message the request message
567 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
568 const struct GNUNET_MessageHeader *message)
570 const struct ReservationRequestMessage *msg =
571 (const struct ReservationRequestMessage *) message;
572 struct ReservationResultMessage result;
574 struct GNUNET_TIME_Relative res_delay;
576 if (NULL == find_client (client))
578 /* missing start message! */
580 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
584 "RESERVATION_REQUEST");
585 amount = (int32_t) ntohl (msg->amount);
586 res_delay = GAS_reservations_reserve (&msg->peer, amount);
587 if (res_delay.rel_value_us > 0)
589 result.header.size = htons (sizeof (struct ReservationResultMessage));
590 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
591 result.amount = htonl (amount);
592 result.peer = msg->peer;
593 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
594 GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
596 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
598 GNUNET_SERVER_receive_done (client, GNUNET_OK);
603 * Handle 'preference change' messages from clients.
605 * @param cls unused, NULL
606 * @param client client that sent the request
607 * @param message the request message
610 GAS_handle_preference_change (void *cls,
611 struct GNUNET_SERVER_Client *client,
612 const struct GNUNET_MessageHeader *message)
614 const struct ChangePreferenceMessage *msg;
615 const struct PreferenceInformation *pi;
620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
621 "PREFERENCE_CHANGE");
622 msize = ntohs (message->size);
623 if (msize < sizeof (struct ChangePreferenceMessage))
626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
629 msg = (const struct ChangePreferenceMessage *) message;
630 nump = ntohl (msg->num_preferences);
632 sizeof (struct ChangePreferenceMessage) +
633 nump * sizeof (struct PreferenceInformation))
636 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
639 GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
641 pi = (const struct PreferenceInformation *) &msg[1];
642 for (i = 0; i < nump; i++)
643 GAS_addresses_preference_change (GSA_addresses,
646 (enum GNUNET_ATS_PreferenceKind)
647 ntohl (pi[i].preference_kind),
648 pi[i].preference_value);
649 GNUNET_SERVER_receive_done (client, GNUNET_OK);
654 * Handle 'preference feedback' messages from clients.
656 * @param cls unused, NULL
657 * @param client client that sent the request
658 * @param message the request message
661 GAS_handle_preference_feedback (void *cls,
662 struct GNUNET_SERVER_Client *client,
663 const struct GNUNET_MessageHeader *message)
665 const struct FeedbackPreferenceMessage *msg;
666 const struct PreferenceInformation *pi;
671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
672 "PREFERENCE_FEEDBACK");
673 msize = ntohs (message->size);
674 if (msize < sizeof (struct FeedbackPreferenceMessage))
677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
680 msg = (const struct FeedbackPreferenceMessage *) message;
681 nump = ntohl (msg->num_feedback);
683 sizeof (struct FeedbackPreferenceMessage) +
684 nump * sizeof (struct PreferenceInformation))
687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
690 GNUNET_STATISTICS_update (GSA_stats, "# preference feedbacks requests processed",
692 pi = (const struct PreferenceInformation *) &msg[1];
693 for (i = 0; i < nump; i++)
694 GAS_addresses_preference_feedback (GSA_addresses,
697 GNUNET_TIME_relative_ntoh(msg->scope),
698 (enum GNUNET_ATS_PreferenceKind)
699 ntohl (pi[i].preference_kind),
700 pi[i].preference_value);
701 GNUNET_SERVER_receive_done (client, GNUNET_OK);
707 * Initialize performance subsystem.
709 * @param server handle to our server
710 * @param addresses the address handle to use
713 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
714 struct GAS_Addresses_Handle *addresses)
716 GSA_addresses = addresses;
717 nc = GNUNET_SERVER_notification_context_create (server, 128);
722 * Shutdown performance subsystem.
725 GAS_performance_done ()
727 GNUNET_SERVER_notification_context_destroy (nc);
731 /* end of gnunet-service-ats_performance.c */