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;
230 for (pc = pc_head; pc != NULL; pc = pc->next)
231 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
233 GAS_performance_notify_client (pc,
235 plugin_name, plugin_addr, plugin_addr_len,
238 bandwidth_out, bandwidth_in);
240 GNUNET_STATISTICS_update (GSA_stats,
241 "# performance updates given to clients", 1,
247 peerinfo_it (void *cls,
248 const struct GNUNET_PeerIdentity *id,
249 const char *plugin_name,
250 const void *plugin_addr, size_t plugin_addr_len,
252 const struct GNUNET_ATS_Information *atsi,
254 struct GNUNET_BANDWIDTH_Value32NBO
256 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
258 struct PerformanceClient *pc = cls;
259 GNUNET_assert (NULL != pc);
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
267 (unsigned int) ntohl (bandwidth_out.value__),
268 (unsigned int) ntohl (bandwidth_in.value__));
269 GAS_performance_notify_client(pc,
276 bandwidth_out, bandwidth_in);
281 * Iterator for GAS_performance_add_client
283 * @param cls the client requesting information
288 const struct GNUNET_PeerIdentity *id)
290 struct PerformanceClient *pc = cls;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
294 GAS_addresses_get_peer_info (GSA_addresses, id, &peerinfo_it, pc);
299 * Register a new performance client.
301 * @param client handle of the new client
302 * @param flag flag specifying the type of the client
305 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
308 struct PerformanceClient *pc;
309 GNUNET_break (NULL == find_client (client));
311 pc = GNUNET_new (struct PerformanceClient);
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding performance client %s PIC\n",
316 (flag == START_FLAG_PERFORMANCE_WITH_PIC) ? "with" : "without");
318 GNUNET_SERVER_notification_context_add (nc, client);
319 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
321 /* Send information about clients */
322 GAS_addresses_iterate_peers (GSA_addresses, &peer_it, pc);
326 static void transmit_req_addr (struct AddressIteration *ai,
327 const struct GNUNET_PeerIdentity *id,
328 const char *plugin_name,
329 const void *plugin_addr, size_t plugin_addr_len,
331 const struct GNUNET_ATS_Information *atsi,
333 struct GNUNET_BANDWIDTH_Value32NBO
335 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
338 struct GNUNET_ATS_Information *atsp;
339 struct PeerInformationMessage *msg;
341 size_t plugin_name_length;
344 if (NULL != plugin_name)
345 plugin_name_length = strlen (plugin_name) + 1;
347 plugin_name_length = 0;
348 msize = sizeof (struct PeerInformationMessage) +
349 atsi_count * sizeof (struct GNUNET_ATS_Information) +
350 plugin_addr_len + plugin_name_length;
351 char buf[msize] GNUNET_ALIGN;
353 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
354 GNUNET_assert (atsi_count <
355 GNUNET_SERVER_MAX_MESSAGE_SIZE /
356 sizeof (struct GNUNET_ATS_Information));
357 msg = (struct PeerInformationMessage *) buf;
358 msg->header.size = htons (msize);
359 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
360 msg->ats_count = htonl (atsi_count);
361 msg->id = htonl (ai->id);
365 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
366 msg->address_length = htons (plugin_addr_len);
367 msg->address_active = ntohl (active);
368 msg->plugin_name_length = htons (plugin_name_length);
369 msg->bandwidth_out = bandwidth_out;
370 msg->bandwidth_in = bandwidth_in;
371 atsp = (struct GNUNET_ATS_Information *) &msg[1];
372 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
373 addrp = (char *) &atsp[atsi_count];
374 if (NULL != plugin_addr)
375 memcpy (addrp, plugin_addr, plugin_addr_len);
376 if (NULL != plugin_name)
377 strcpy (&addrp[plugin_addr_len], plugin_name);
378 GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
383 req_addr_peerinfo_it (void *cls,
384 const struct GNUNET_PeerIdentity *id,
385 const char *plugin_name,
386 const void *plugin_addr, size_t plugin_addr_len,
388 const struct GNUNET_ATS_Information *atsi,
390 struct GNUNET_BANDWIDTH_Value32NBO
392 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
394 struct AddressIteration *ai = cls;
396 GNUNET_assert (NULL != ai);
397 GNUNET_assert (NULL != ai->pc);
398 if (NULL == find_client (ai->pc->client))
399 return; /* Client disconnected */
401 if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404 "Address iteration done\n");
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
409 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
412 (unsigned int) ntohl (bandwidth_out.value__),
413 (unsigned int) ntohl (bandwidth_in.value__));
415 /* Transmit result */
416 if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
420 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
423 (unsigned int) ntohl (bandwidth_out.value__),
424 (unsigned int) ntohl (bandwidth_in.value__));
425 transmit_req_addr (cls,
428 plugin_addr, plugin_addr_len,
432 bandwidth_out, bandwidth_in);
438 * Iterator for GAS_handle_request_address_list
440 * @param cls the client requesting information
444 req_addr_peer_it (void *cls,
445 const struct GNUNET_PeerIdentity *id)
447 struct AddressIteration *ai = cls;
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
451 GAS_addresses_get_peer_info (GSA_addresses, id, &req_addr_peerinfo_it, ai);
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer iteration done\n");
461 * Handle 'address list request' messages from clients.
463 * @param cls unused, NULL
464 * @param client client that sent the request
465 * @param message the request message
468 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
469 const struct GNUNET_MessageHeader *message)
471 struct PerformanceClient *pc;
472 struct AddressIteration ai;
473 struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
474 struct GNUNET_PeerIdentity allzeros;
475 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
478 "ADDRESSLIST_REQUEST");
480 if (NULL == (pc = find_client(client)))
486 ai.all = ntohl (alrm->all);
487 ai.id = ntohl (alrm->id);
490 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
491 bandwidth_zero.value__ = htonl (0);
492 if (0 == memcmp (&alrm->peer, &allzeros, sizeof (struct GNUNET_PeerIdentity)))
494 /* Return addresses for all peers */
495 GAS_addresses_iterate_peers (GSA_addresses, &req_addr_peer_it, &ai);
496 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
500 /* Return addresses for a specific peer */
501 GAS_addresses_get_peer_info (GSA_addresses, &alrm->peer, &req_addr_peerinfo_it, &ai);
502 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
504 GNUNET_SERVER_receive_done (client, GNUNET_OK);
509 GAS_handle_performance_update (struct GNUNET_PeerIdentity *peer,
510 const char *plugin_name, const void *plugin_addr, size_t plugin_addr_len,
511 const int active, struct GNUNET_ATS_Information *ats, uint32_t ats_count,
512 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
513 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
516 GAS_performance_notify_all_clients (peer, plugin_name, plugin_addr,
517 plugin_addr_len, active, ats, ats_count, bandwidth_out, bandwidth_in);
520 struct PerformanceClient *cur;
521 struct PerformanceMonitorClient *curm;
522 struct MonitorResponseMessage *mrm;
525 msglen = sizeof (struct MonitorResponseMessage) +
526 ats_count * sizeof (struct GNUNET_ATS_Information);
527 mrm = GNUNET_malloc (msglen);
529 mrm->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_MONITOR_RESPONSE);
530 mrm->header.size = htons (msglen);
531 mrm->ats_count = htonl (ats_count);
533 memcpy (&mrm[1], ats, ats_count * sizeof (struct GNUNET_ATS_Information));
535 for (cur = pc_head; NULL != cur; cur = cur->next)
536 for (curm = cur->pm_head; NULL != curm; curm = curm->next)
538 /* Notify client about update */
539 mrm->id = htonl (curm->id);
540 GNUNET_SERVER_notification_context_unicast (nc,
542 (struct GNUNET_MessageHeader *) mrm,
551 * Handle 'reservation request' messages from clients.
553 * @param cls unused, NULL
554 * @param client client that sent the request
555 * @param message the request message
558 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
559 const struct GNUNET_MessageHeader *message)
561 const struct ReservationRequestMessage *msg =
562 (const struct ReservationRequestMessage *) message;
563 struct ReservationResultMessage result;
565 struct GNUNET_TIME_Relative res_delay;
567 if (NULL == find_client (client))
569 /* missing start message! */
571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
575 "RESERVATION_REQUEST");
576 amount = (int32_t) ntohl (msg->amount);
577 res_delay = GAS_reservations_reserve (&msg->peer, amount);
578 if (res_delay.rel_value_us > 0)
580 result.header.size = htons (sizeof (struct ReservationResultMessage));
581 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
582 result.amount = htonl (amount);
583 result.peer = msg->peer;
584 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
585 GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
587 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
589 GNUNET_SERVER_receive_done (client, GNUNET_OK);
594 * Handle 'preference change' messages from clients.
596 * @param cls unused, NULL
597 * @param client client that sent the request
598 * @param message the request message
601 GAS_handle_preference_change (void *cls,
602 struct GNUNET_SERVER_Client *client,
603 const struct GNUNET_MessageHeader *message)
605 const struct ChangePreferenceMessage *msg;
606 const struct PreferenceInformation *pi;
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
612 "PREFERENCE_CHANGE");
613 msize = ntohs (message->size);
614 if (msize < sizeof (struct ChangePreferenceMessage))
617 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
620 msg = (const struct ChangePreferenceMessage *) message;
621 nump = ntohl (msg->num_preferences);
623 sizeof (struct ChangePreferenceMessage) +
624 nump * sizeof (struct PreferenceInformation))
627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
630 GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
632 pi = (const struct PreferenceInformation *) &msg[1];
633 for (i = 0; i < nump; i++)
634 GAS_addresses_preference_change (GSA_addresses,
637 (enum GNUNET_ATS_PreferenceKind)
638 ntohl (pi[i].preference_kind),
639 pi[i].preference_value);
640 GNUNET_SERVER_receive_done (client, GNUNET_OK);
645 * Handle 'preference feedback' messages from clients.
647 * @param cls unused, NULL
648 * @param client client that sent the request
649 * @param message the request message
652 GAS_handle_preference_feedback (void *cls,
653 struct GNUNET_SERVER_Client *client,
654 const struct GNUNET_MessageHeader *message)
656 const struct FeedbackPreferenceMessage *msg;
657 const struct PreferenceInformation *pi;
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
663 "PREFERENCE_FEEDBACK");
664 msize = ntohs (message->size);
665 if (msize < sizeof (struct FeedbackPreferenceMessage))
668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
671 msg = (const struct FeedbackPreferenceMessage *) message;
672 nump = ntohl (msg->num_feedback);
674 sizeof (struct FeedbackPreferenceMessage) +
675 nump * sizeof (struct PreferenceInformation))
678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
681 GNUNET_STATISTICS_update (GSA_stats, "# preference feedbacks requests processed",
683 pi = (const struct PreferenceInformation *) &msg[1];
684 for (i = 0; i < nump; i++)
685 GAS_addresses_preference_feedback (GSA_addresses,
688 GNUNET_TIME_relative_ntoh(msg->scope),
689 (enum GNUNET_ATS_PreferenceKind)
690 ntohl (pi[i].preference_kind),
691 pi[i].preference_value);
692 GNUNET_SERVER_receive_done (client, GNUNET_OK);
698 * Initialize performance subsystem.
700 * @param server handle to our server
701 * @param addresses the address handle to use
704 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
705 struct GAS_Addresses_Handle *addresses)
707 GSA_addresses = addresses;
708 nc = GNUNET_SERVER_notification_context_create (server, 128);
713 * Shutdown performance subsystem.
716 GAS_performance_done ()
718 GNUNET_SERVER_notification_context_destroy (nc);
722 /* end of gnunet-service-ats_performance.c */