2 This file is part of GNUnet.
3 (C) 2011-2014 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.
65 static struct GAS_Addresses_Handle *GSA_addresses;
68 * Head of linked list of all clients to this service.
70 static struct PerformanceClient *pc_head;
73 * Tail of linked list of all clients to this service.
75 static struct PerformanceClient *pc_tail;
78 * Context for sending messages to performance clients.
80 static struct GNUNET_SERVER_NotificationContext *nc;
84 * Find the performance client associated with the given handle.
86 * @param client server handle
87 * @return internal handle
89 static struct PerformanceClient *
90 find_client (struct GNUNET_SERVER_Client *client)
92 struct PerformanceClient *pc;
94 for (pc = pc_head; pc != NULL; pc = pc->next)
95 if (pc->client == client)
102 * Unregister a client (which may have been a performance client,
103 * but this is not assured).
105 * @param client handle of the (now dead) client
108 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
110 struct PerformanceClient *pc;
112 pc = find_client (client);
115 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
116 GAS_addresses_preference_client_disconnect (GSA_addresses, client);
122 * Transmit the given performance information to all performance
125 * @param pc performance client to send to
126 * @param peer peer for which this is an address suggestion
127 * @param plugin_name 0-termintated string specifying the transport plugin
128 * @param plugin_addr binary address for the plugin to use
129 * @param plugin_addr_len number of bytes in plugin_addr
130 * @param active #GNUNET_YES if this address is actively used
131 * to maintain a connection to a peer;
132 * #GNUNET_NO if the address is not actively used;
133 * #GNUNET_SYSERR if this address is no longer available for ATS
134 * @param atsi performance data for the address
135 * @param atsi_count number of performance records in @a atsi
136 * @param bandwidth_out assigned outbound bandwidth
137 * @param bandwidth_in assigned inbound bandwidth
140 GAS_performance_notify_client (struct PerformanceClient *pc,
141 const struct GNUNET_PeerIdentity *peer,
142 const char *plugin_name,
143 const void *plugin_addr,
144 size_t plugin_addr_len,
146 const struct GNUNET_ATS_Information *atsi,
148 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
149 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
152 struct PeerInformationMessage *msg;
153 size_t plugin_name_length = strlen (plugin_name) + 1;
155 sizeof (struct PeerInformationMessage) +
156 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
158 char buf[msize] GNUNET_ALIGN;
159 struct GNUNET_ATS_Information *atsp;
162 GNUNET_assert (NULL != pc);
163 if (NULL == find_client (pc->client))
164 return; /* Client disconnected */
166 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
167 GNUNET_assert (atsi_count <
168 GNUNET_SERVER_MAX_MESSAGE_SIZE /
169 sizeof (struct GNUNET_ATS_Information));
170 msg = (struct PeerInformationMessage *) buf;
171 msg->header.size = htons (msize);
172 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
174 msg->ats_count = htonl (atsi_count);
176 msg->address_length = htons (plugin_addr_len);
177 msg->address_active = ntohl ((uint32_t) active);
178 msg->plugin_name_length = htons (plugin_name_length);
179 msg->bandwidth_out = bandwidth_out;
180 msg->bandwidth_in = bandwidth_in;
181 atsp = (struct GNUNET_ATS_Information *) &msg[1];
182 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
183 addrp = (char *) &atsp[atsi_count];
184 memcpy (addrp, plugin_addr, plugin_addr_len);
185 strcpy (&addrp[plugin_addr_len], plugin_name);
186 GNUNET_SERVER_notification_context_unicast (nc,
194 * Transmit the given performance information to all performance
197 * @param peer peer for which this is an address suggestion
198 * @param plugin_name 0-termintated string specifying the transport plugin
199 * @param plugin_addr binary address for the plugin to use
200 * @param plugin_addr_len number of bytes in @a plugin_addr
201 * @param active #GNUNET_YES if this address is actively used
202 * to maintain a connection to a peer;
203 * #GNUNET_NO if the address is not actively used;
204 * #GNUNET_SYSERR if this address is no longer available for ATS
205 * @param atsi performance data for the address
206 * @param atsi_count number of performance records in @a atsi
207 * @param bandwidth_out assigned outbound bandwidth
208 * @param bandwidth_in assigned inbound bandwidth
211 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
212 const char *plugin_name,
213 const void *plugin_addr,
214 size_t plugin_addr_len,
216 const struct GNUNET_ATS_Information *atsi,
218 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
219 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
221 struct PerformanceClient *pc;
223 for (pc = pc_head; pc != NULL; pc = pc->next)
224 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
226 GAS_performance_notify_client (pc,
233 bandwidth_out, bandwidth_in);
235 GNUNET_STATISTICS_update (GSA_stats,
236 "# performance updates given to clients", 1,
243 * Iterator for called from #GAS_addresses_get_peer_info()
245 * @param cls closure with the `struct PerformanceClient *`
246 * @param id the peer id
247 * @param plugin_name plugin name
248 * @param plugin_addr address
249 * @param plugin_addr_len length of @a plugin_addr
250 * @param active is address actively used
251 * @param atsi ats performance information
252 * @param atsi_count number of ats performance elements in @a atsi
253 * @param bandwidth_out current outbound bandwidth assigned to address
254 * @param bandwidth_in current inbound bandwidth assigned to address
257 peerinfo_it (void *cls,
258 const struct GNUNET_PeerIdentity *id,
259 const char *plugin_name,
260 const void *plugin_addr,
261 size_t plugin_addr_len,
263 const struct GNUNET_ATS_Information *atsi,
265 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
266 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
268 struct PerformanceClient *pc = cls;
270 GNUNET_assert (NULL != pc);
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
277 (unsigned int) ntohl (bandwidth_out.value__),
278 (unsigned int) ntohl (bandwidth_in.value__));
279 GAS_performance_notify_client (pc,
292 * Register a new performance client.
294 * @param client handle of the new client
295 * @param flag flag specifying the type of the client
298 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
301 struct PerformanceClient *pc;
303 GNUNET_break (NULL == find_client (client));
304 pc = GNUNET_new (struct PerformanceClient);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "Adding performance client %s PIC\n",
310 (flag == START_FLAG_PERFORMANCE_WITH_PIC) ? "with" : "without");
312 GNUNET_SERVER_notification_context_add (nc,
314 GNUNET_CONTAINER_DLL_insert (pc_head,
317 GAS_addresses_get_peer_info (GSA_addresses,
325 * Information we need for the callbacks to return a list of addresses
326 * back to the client.
328 struct AddressIteration
331 * Actual handle to the client.
333 struct PerformanceClient *pc;
336 * Are we sending all addresses, or only those that are active?
341 * Which ID should be included in the response?
349 * Send a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE with the
350 * given address details to the client identified in @a ai.
352 * @param ai our address information context (identifies the client)
353 * @param id the peer id this address is for
354 * @param plugin_name name of the plugin that supports this address
355 * @param plugin_addr address
356 * @param plugin_addr_len length of @a plugin_addr
357 * @param active #GNUNET_YES if this address is actively used
358 * @param atsi ats performance information
359 * @param atsi_count number of ats performance elements in @a atsi
360 * @param bandwidth_out current outbound bandwidth assigned to address
361 * @param bandwidth_in current inbound bandwidth assigned to address
364 transmit_req_addr (struct AddressIteration *ai,
365 const struct GNUNET_PeerIdentity *id,
366 const char *plugin_name,
367 const void *plugin_addr,
368 size_t plugin_addr_len,
370 const struct GNUNET_ATS_Information *atsi,
372 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
373 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
376 struct GNUNET_ATS_Information *atsp;
377 struct PeerInformationMessage *msg;
379 size_t plugin_name_length;
382 if (NULL != plugin_name)
383 plugin_name_length = strlen (plugin_name) + 1;
385 plugin_name_length = 0;
386 msize = sizeof (struct PeerInformationMessage) +
387 atsi_count * sizeof (struct GNUNET_ATS_Information) +
388 plugin_addr_len + plugin_name_length;
389 char buf[msize] GNUNET_ALIGN;
391 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
392 GNUNET_assert (atsi_count <
393 GNUNET_SERVER_MAX_MESSAGE_SIZE /
394 sizeof (struct GNUNET_ATS_Information));
395 msg = (struct PeerInformationMessage *) buf;
396 msg->header.size = htons (msize);
397 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
398 msg->ats_count = htonl (atsi_count);
399 msg->id = htonl (ai->id);
403 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
404 msg->address_length = htons (plugin_addr_len);
405 msg->address_active = ntohl (active);
406 msg->plugin_name_length = htons (plugin_name_length);
407 msg->bandwidth_out = bandwidth_out;
408 msg->bandwidth_in = bandwidth_in;
409 atsp = (struct GNUNET_ATS_Information *) &msg[1];
410 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
411 addrp = (char *) &atsp[atsi_count];
412 if (NULL != plugin_addr)
413 memcpy (addrp, plugin_addr, plugin_addr_len);
414 if (NULL != plugin_name)
415 strcpy (&addrp[plugin_addr_len], plugin_name);
416 GNUNET_SERVER_notification_context_unicast (nc,
424 * Iterator for #GAS_addresses_get_peer_info(), called with peer-specific
425 * information to be passed back to the client.
427 * @param cls closure with our `struct AddressIteration *`
428 * @param id the peer id
429 * @param plugin_name plugin name
430 * @param plugin_addr address
431 * @param plugin_addr_len length of @a plugin_addr
432 * @param active is address actively used
433 * @param atsi ats performance information
434 * @param atsi_count number of ats performance elements in @a atsi
435 * @param bandwidth_out current outbound bandwidth assigned to address
436 * @param bandwidth_in current inbound bandwidth assigned to address
439 req_addr_peerinfo_it (void *cls,
440 const struct GNUNET_PeerIdentity *id,
441 const char *plugin_name,
442 const void *plugin_addr,
443 size_t plugin_addr_len,
445 const struct GNUNET_ATS_Information *atsi,
447 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
448 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
450 struct AddressIteration *ai = cls;
453 (NULL == plugin_name) &&
454 (NULL == plugin_addr) )
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Address iteration done for one peer\n");
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
461 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
462 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
465 (unsigned int) ntohl (bandwidth_out.value__),
466 (unsigned int) ntohl (bandwidth_in.value__));
468 /* Transmit result (either if address is active, or if
469 client wanted all addresses) */
470 if ( (GNUNET_YES == ai->all) ||
471 (GNUNET_YES == active))
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
475 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
478 (unsigned int) ntohl (bandwidth_out.value__),
479 (unsigned int) ntohl (bandwidth_in.value__));
480 transmit_req_addr (ai,
483 plugin_addr, plugin_addr_len,
494 * Handle 'address list request' messages from clients.
496 * @param cls unused, NULL
497 * @param client client that sent the request
498 * @param message the request message
501 GAS_handle_request_address_list (void *cls,
502 struct GNUNET_SERVER_Client *client,
503 const struct GNUNET_MessageHeader *message)
505 struct PerformanceClient *pc;
506 struct AddressIteration ai;
507 const struct AddressListRequestMessage *alrm;
508 struct GNUNET_PeerIdentity allzeros;
509 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
512 "Received `%s' message\n",
513 "ADDRESSLIST_REQUEST");
514 if (NULL == (pc = find_client(client)))
519 alrm = (const struct AddressListRequestMessage *) message;
520 ai.all = ntohl (alrm->all);
521 ai.id = ntohl (alrm->id);
524 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
525 bandwidth_zero.value__ = htonl (0);
526 if (0 == memcmp (&alrm->peer,
528 sizeof (struct GNUNET_PeerIdentity)))
530 /* Return addresses for all peers */
531 GAS_addresses_get_peer_info (GSA_addresses,
533 &req_addr_peerinfo_it,
538 /* Return addresses for a specific peer */
539 GAS_addresses_get_peer_info (GSA_addresses,
541 &req_addr_peerinfo_it,
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Finished handling `%s' message\n",
546 "ADDRESSLIST_REQUEST");
547 transmit_req_addr (&ai,
553 GNUNET_SERVER_receive_done (client,
559 * Handle 'reservation request' messages from clients.
561 * @param cls unused, NULL
562 * @param client client that sent the request
563 * @param message the request message
566 GAS_handle_reservation_request (void *cls,
567 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,
584 "Received `%s' message\n",
585 "RESERVATION_REQUEST");
586 amount = (int32_t) ntohl (msg->amount);
587 res_delay = GAS_reservations_reserve (&msg->peer, amount);
588 if (res_delay.rel_value_us > 0)
590 result.header.size = htons (sizeof (struct ReservationResultMessage));
591 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
592 result.amount = htonl (amount);
593 result.peer = msg->peer;
594 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
595 GNUNET_STATISTICS_update (GSA_stats,
596 "# reservation requests processed", 1,
598 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
600 GNUNET_SERVER_receive_done (client, GNUNET_OK);
605 * Handle 'preference change' messages from clients.
607 * @param cls unused, NULL
608 * @param client client that sent the request
609 * @param message the request message
612 GAS_handle_preference_change (void *cls,
613 struct GNUNET_SERVER_Client *client,
614 const struct GNUNET_MessageHeader *message)
616 const struct ChangePreferenceMessage *msg;
617 const struct PreferenceInformation *pi;
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
623 "PREFERENCE_CHANGE");
624 msize = ntohs (message->size);
625 if (msize < sizeof (struct ChangePreferenceMessage))
628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
631 msg = (const struct ChangePreferenceMessage *) message;
632 nump = ntohl (msg->num_preferences);
634 sizeof (struct ChangePreferenceMessage) +
635 nump * sizeof (struct PreferenceInformation))
638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
641 GNUNET_STATISTICS_update (GSA_stats,
642 "# preference change requests processed",
644 pi = (const struct PreferenceInformation *) &msg[1];
645 for (i = 0; i < nump; i++)
646 GAS_addresses_preference_change (GSA_addresses,
649 (enum GNUNET_ATS_PreferenceKind)
650 ntohl (pi[i].preference_kind),
651 pi[i].preference_value);
652 GNUNET_SERVER_receive_done (client, GNUNET_OK);
657 * Handle 'preference feedback' messages from clients.
659 * @param cls unused, NULL
660 * @param client client that sent the request
661 * @param message the request message
664 GAS_handle_preference_feedback (void *cls,
665 struct GNUNET_SERVER_Client *client,
666 const struct GNUNET_MessageHeader *message)
668 const struct FeedbackPreferenceMessage *msg;
669 const struct PreferenceInformation *pi;
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Received `%s' message\n",
676 "PREFERENCE_FEEDBACK");
677 msize = ntohs (message->size);
678 if (msize < sizeof (struct FeedbackPreferenceMessage))
681 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
684 msg = (const struct FeedbackPreferenceMessage *) message;
685 nump = ntohl (msg->num_feedback);
687 sizeof (struct FeedbackPreferenceMessage) +
688 nump * sizeof (struct PreferenceInformation))
691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
694 GNUNET_STATISTICS_update (GSA_stats,
695 "# preference feedbacks requests processed",
697 pi = (const struct PreferenceInformation *) &msg[1];
698 for (i = 0; i < nump; i++)
699 GAS_addresses_preference_feedback (GSA_addresses,
702 GNUNET_TIME_relative_ntoh(msg->scope),
703 (enum GNUNET_ATS_PreferenceKind)
704 ntohl (pi[i].preference_kind),
705 pi[i].preference_value);
706 GNUNET_SERVER_receive_done (client, GNUNET_OK);
712 * Initialize performance subsystem.
714 * @param server handle to our server
715 * @param addresses the address handle to use
718 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
719 struct GAS_Addresses_Handle *addresses)
721 GSA_addresses = addresses;
722 nc = GNUNET_SERVER_notification_context_create (server, 128);
727 * Shutdown performance subsystem.
730 GAS_performance_done ()
732 GNUNET_SERVER_notification_context_destroy (nc);
736 /* end of gnunet-service-ats_performance.c */