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);
136 * Transmit the given performance information to all performance
139 * @param pc performance client to send to
140 * @param peer peer for which this is an address suggestion
141 * @param plugin_name 0-termintated string specifying the transport plugin
142 * @param plugin_addr binary address for the plugin to use
143 * @param plugin_addr_len number of bytes in plugin_addr
144 * @param active is this address active
145 * @param atsi performance data for the address
146 * @param atsi_count number of performance records in 'ats'
147 * @param bandwidth_out assigned outbound bandwidth
148 * @param bandwidth_in assigned inbound bandwidth
151 GAS_performance_notify_client (struct PerformanceClient *pc,
152 const struct GNUNET_PeerIdentity *peer,
153 const char *plugin_name,
154 const void *plugin_addr, size_t plugin_addr_len,
156 const struct GNUNET_ATS_Information *atsi,
158 struct GNUNET_BANDWIDTH_Value32NBO
160 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
163 struct PeerInformationMessage *msg;
164 size_t plugin_name_length = strlen (plugin_name) + 1;
166 sizeof (struct PeerInformationMessage) +
167 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
169 char buf[msize] GNUNET_ALIGN;
170 struct GNUNET_ATS_Information *atsp;
173 GNUNET_assert (NULL != pc);
174 if (NULL == find_client (pc->client))
175 return; /* Client disconnected */
177 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
178 GNUNET_assert (atsi_count <
179 GNUNET_SERVER_MAX_MESSAGE_SIZE /
180 sizeof (struct GNUNET_ATS_Information));
181 msg = (struct PeerInformationMessage *) buf;
182 msg->header.size = htons (msize);
183 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
185 msg->ats_count = htonl (atsi_count);
187 msg->address_length = htons (plugin_addr_len);
188 msg->address_active = ntohl (active);
189 msg->plugin_name_length = htons (plugin_name_length);
190 msg->bandwidth_out = bandwidth_out;
191 msg->bandwidth_in = bandwidth_in;
192 atsp = (struct GNUNET_ATS_Information *) &msg[1];
193 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
194 addrp = (char *) &atsp[atsi_count];
195 memcpy (addrp, plugin_addr, plugin_addr_len);
196 strcpy (&addrp[plugin_addr_len], plugin_name);
197 GNUNET_SERVER_notification_context_unicast (nc, pc->client, &msg->header,
203 * Transmit the given performance information to all performance
206 * @param peer peer for which this is an address suggestion
207 * @param plugin_name 0-termintated string specifying the transport plugin
208 * @param plugin_addr binary address for the plugin to use
209 * @param plugin_addr_len number of bytes in plugin_addr
210 * @param active is this address active
211 * @param atsi performance data for the address
212 * @param atsi_count number of performance records in 'ats'
213 * @param bandwidth_out assigned outbound bandwidth
214 * @param bandwidth_in assigned inbound bandwidth
217 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
218 const char *plugin_name,
219 const void *plugin_addr, size_t plugin_addr_len,
221 const struct GNUNET_ATS_Information *atsi,
223 struct GNUNET_BANDWIDTH_Value32NBO
225 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
227 struct PerformanceClient *pc;
228 for (pc = pc_head; pc != NULL; pc = pc->next)
229 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
231 GAS_performance_notify_client (pc,
233 plugin_name, plugin_addr, plugin_addr_len,
236 bandwidth_out, bandwidth_in);
238 GNUNET_STATISTICS_update (GSA_stats,
239 "# performance updates given to clients", 1,
245 peerinfo_it (void *cls,
246 const struct GNUNET_PeerIdentity *id,
247 const char *plugin_name,
248 const void *plugin_addr, size_t plugin_addr_len,
250 const struct GNUNET_ATS_Information *atsi,
252 struct GNUNET_BANDWIDTH_Value32NBO
254 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
256 struct PerformanceClient *pc = cls;
257 GNUNET_assert (NULL != pc);
261 if (GNUNET_NO == active)
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Callback for peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
268 ntohl (bandwidth_out.value__),
269 ntohl (bandwidth_in.value__));
270 GAS_performance_notify_client(pc,
277 bandwidth_out, bandwidth_in);
282 * Iterator for GAS_performance_add_client
284 * @param cls the client requesting information
289 const struct GNUNET_PeerIdentity *id)
291 struct PerformanceClient *pc = cls;
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
295 GAS_addresses_get_peer_info (GSA_addresses, id, &peerinfo_it, pc);
300 * Register a new performance client.
302 * @param client handle of the new client
303 * @param flag flag specifying the type of the client
306 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
309 struct PerformanceClient *pc;
310 GNUNET_break (NULL == find_client (client));
312 pc = GNUNET_malloc (sizeof (struct PerformanceClient));
315 GNUNET_SERVER_notification_context_add (nc, client);
316 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
318 /* Send information about clients */
319 GAS_addresses_iterate_peers (GSA_addresses, &peer_it, pc);
323 static void transmit_req_addr (struct AddressIteration *ai,
324 const struct GNUNET_PeerIdentity *id,
325 const char *plugin_name,
326 const void *plugin_addr, size_t plugin_addr_len,
328 const struct GNUNET_ATS_Information *atsi,
330 struct GNUNET_BANDWIDTH_Value32NBO
332 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
335 struct GNUNET_ATS_Information *atsp;
336 struct PeerInformationMessage *msg;
338 size_t plugin_name_length;
341 if (NULL != plugin_name)
342 plugin_name_length = strlen (plugin_name) + 1;
344 plugin_name_length = 0;
345 msize = sizeof (struct PeerInformationMessage) +
346 atsi_count * sizeof (struct GNUNET_ATS_Information) +
347 plugin_addr_len + plugin_name_length;
348 char buf[msize] GNUNET_ALIGN;
350 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
351 GNUNET_assert (atsi_count <
352 GNUNET_SERVER_MAX_MESSAGE_SIZE /
353 sizeof (struct GNUNET_ATS_Information));
354 msg = (struct PeerInformationMessage *) buf;
355 msg->header.size = htons (msize);
356 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
357 msg->ats_count = htonl (atsi_count);
358 msg->id = htonl (ai->id);
362 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
363 msg->address_length = htons (plugin_addr_len);
364 msg->address_active = ntohl (active);
365 msg->plugin_name_length = htons (plugin_name_length);
366 msg->bandwidth_out = bandwidth_out;
367 msg->bandwidth_in = bandwidth_in;
368 atsp = (struct GNUNET_ATS_Information *) &msg[1];
369 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
370 addrp = (char *) &atsp[atsi_count];
371 if (NULL != plugin_addr)
372 memcpy (addrp, plugin_addr, plugin_addr_len);
373 if (NULL != plugin_name)
374 strcpy (&addrp[plugin_addr_len], plugin_name);
375 GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
380 req_addr_peerinfo_it (void *cls,
381 const struct GNUNET_PeerIdentity *id,
382 const char *plugin_name,
383 const void *plugin_addr, size_t plugin_addr_len,
385 const struct GNUNET_ATS_Information *atsi,
387 struct GNUNET_BANDWIDTH_Value32NBO
389 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
391 struct AddressIteration *ai = cls;
393 GNUNET_assert (NULL != ai);
394 GNUNET_assert (NULL != ai->pc);
395 if (NULL == find_client (ai->pc->client))
396 return; /* Client disconnected */
398 if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "Address iteration done\n");
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u \n",
406 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
409 (unsigned int) ntohl (bandwidth_out.value__),
410 (unsigned int) ntohl (bandwidth_in.value__));
412 /* Transmit result */
413 if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u \n",
417 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
420 (unsigned int) ntohl (bandwidth_out.value__),
421 (unsigned int) ntohl (bandwidth_in.value__));
422 transmit_req_addr (cls,
425 plugin_addr, plugin_addr_len,
429 bandwidth_out, bandwidth_in);
435 * Iterator for GAS_handle_request_address_list
437 * @param cls the client requesting information
441 req_addr_peer_it (void *cls,
442 const struct GNUNET_PeerIdentity *id)
444 struct AddressIteration *ai = cls;
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
448 GAS_addresses_get_peer_info (GSA_addresses, id, &req_addr_peerinfo_it, ai);
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer iteration done\n");
458 * Handle 'address list request' messages from clients.
460 * @param cls unused, NULL
461 * @param client client that sent the request
462 * @param message the request message
465 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
466 const struct GNUNET_MessageHeader *message)
468 struct PerformanceClient *pc;
469 struct AddressIteration ai;
470 struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
471 struct GNUNET_PeerIdentity allzeros;
472 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
475 "ADDRESSLIST_REQUEST");
477 if (NULL == (pc = find_client(client)))
483 ai.all = ntohl (alrm->all);
484 ai.id = ntohl (alrm->id);
487 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
488 bandwidth_zero.value__ = htonl (0);
489 if (0 == memcmp (&alrm->peer, &allzeros, sizeof (struct GNUNET_PeerIdentity)))
491 /* Return addresses for all peers */
492 GAS_addresses_iterate_peers (GSA_addresses, &req_addr_peer_it, &ai);
493 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
497 /* Return addresses for a specific peer */
498 GAS_addresses_get_peer_info (GSA_addresses, &alrm->peer, &req_addr_peerinfo_it, &ai);
499 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
501 GNUNET_SERVER_receive_done (client, GNUNET_OK);
506 GAS_handle_performance_update (struct GNUNET_PeerIdentity *peer,
507 const char *plugin_name,
508 const void *plugin_addr,
509 size_t plugin_addr_len,
511 struct GNUNET_ATS_Information *ats,
513 struct GNUNET_BANDWIDTH_Value32NBO
515 struct GNUNET_BANDWIDTH_Value32NBO
519 GAS_performance_notify_all_clients (peer,
521 plugin_addr, plugin_addr_len,
524 bandwidth_out, bandwidth_in);
527 struct PerformanceClient *cur;
528 struct PerformanceMonitorClient *curm;
529 struct MonitorResponseMessage *mrm;
532 msglen = sizeof (struct MonitorResponseMessage) +
533 ats_count * sizeof (struct GNUNET_ATS_Information);
534 mrm = GNUNET_malloc (msglen);
536 mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
537 mrm->header.size = htons (msglen);
538 mrm->ats_count = htonl (ats_count);
540 memcpy (&mrm[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
542 for (cur = pc_head; NULL != cur; cur = cur->next)
543 for (curm = cur->pm_head; NULL != curm; curm = curm->next)
545 /* Notify client about update */
546 mrm->id = htonl (curm->id);
547 GNUNET_SERVER_notification_context_unicast (nc,
549 (struct GNUNET_MessageHeader *) mrm,
558 * Handle 'reservation request' messages from clients.
560 * @param cls unused, NULL
561 * @param client client that sent the request
562 * @param message the request message
565 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
566 const struct GNUNET_MessageHeader *message)
568 const struct ReservationRequestMessage *msg =
569 (const struct ReservationRequestMessage *) message;
570 struct ReservationResultMessage result;
572 struct GNUNET_TIME_Relative res_delay;
574 if (NULL == find_client (client))
576 /* missing start message! */
578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
582 "RESERVATION_REQUEST");
583 amount = (int32_t) ntohl (msg->amount);
584 res_delay = GAS_reservations_reserve (&msg->peer, amount);
585 if (res_delay.rel_value_us > 0)
587 result.header.size = htons (sizeof (struct ReservationResultMessage));
588 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
589 result.amount = htonl (amount);
590 result.peer = msg->peer;
591 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
592 GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
594 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
596 GNUNET_SERVER_receive_done (client, GNUNET_OK);
601 * Handle 'preference change' messages from clients.
603 * @param cls unused, NULL
604 * @param client client that sent the request
605 * @param message the request message
608 GAS_handle_preference_change (void *cls,
609 struct GNUNET_SERVER_Client *client,
610 const struct GNUNET_MessageHeader *message)
612 const struct ChangePreferenceMessage *msg;
613 const struct PreferenceInformation *pi;
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
619 "PREFERENCE_CHANGE");
620 msize = ntohs (message->size);
621 if (msize < sizeof (struct ChangePreferenceMessage))
624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
627 msg = (const struct ChangePreferenceMessage *) message;
628 nump = ntohl (msg->num_preferences);
630 sizeof (struct ChangePreferenceMessage) +
631 nump * sizeof (struct PreferenceInformation))
634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
637 GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
639 pi = (const struct PreferenceInformation *) &msg[1];
640 for (i = 0; i < nump; i++)
641 GAS_addresses_change_preference (GSA_addresses,
644 (enum GNUNET_ATS_PreferenceKind)
645 ntohl (pi[i].preference_kind),
646 pi[i].preference_value);
647 GNUNET_SERVER_receive_done (client, GNUNET_OK);
652 * Handle 'preference feedback' messages from clients.
654 * @param cls unused, NULL
655 * @param client client that sent the request
656 * @param message the request message
659 GAS_handle_preference_feedback (void *cls,
660 struct GNUNET_SERVER_Client *client,
661 const struct GNUNET_MessageHeader *message)
663 const struct FeedbackPreferenceMessage *msg;
664 const struct PreferenceInformation *pi;
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
670 "PREFERENCE_FEEDBACK");
671 msize = ntohs (message->size);
672 if (msize < sizeof (struct FeedbackPreferenceMessage))
675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
678 msg = (const struct FeedbackPreferenceMessage *) message;
679 nump = ntohl (msg->num_feedback);
681 sizeof (struct FeedbackPreferenceMessage) +
682 nump * sizeof (struct PreferenceInformation))
685 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
688 GNUNET_STATISTICS_update (GSA_stats, "# preference feedbacks requests processed",
690 pi = (const struct PreferenceInformation *) &msg[1];
691 for (i = 0; i < nump; i++)
692 GAS_addresses_preference_feedback (GSA_addresses,
695 GNUNET_TIME_relative_ntoh(msg->scope),
696 (enum GNUNET_ATS_PreferenceKind)
697 ntohl (pi[i].preference_kind),
698 pi[i].preference_value);
699 GNUNET_SERVER_receive_done (client, GNUNET_OK);
705 * Initialize performance subsystem.
707 * @param server handle to our server
708 * @param addresses the address handle to use
711 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
712 struct GAS_Addresses_Handle *addresses)
714 GSA_addresses = addresses;
715 nc = GNUNET_SERVER_notification_context_create (server, 128);
720 * Shutdown performance subsystem.
723 GAS_performance_done ()
725 GNUNET_SERVER_notification_context_destroy (nc);
729 /* end of gnunet-service-ats_performance.c */