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.
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
150 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
153 struct PeerInformationMessage *msg;
154 size_t plugin_name_length = strlen (plugin_name) + 1;
156 sizeof (struct PeerInformationMessage) +
157 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
159 char buf[msize] GNUNET_ALIGN;
160 struct GNUNET_ATS_Information *atsp;
163 GNUNET_assert (NULL != pc);
164 if (NULL == find_client (pc->client))
165 return; /* Client disconnected */
167 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
168 GNUNET_assert (atsi_count <
169 GNUNET_SERVER_MAX_MESSAGE_SIZE /
170 sizeof (struct GNUNET_ATS_Information));
171 msg = (struct PeerInformationMessage *) buf;
172 msg->header.size = htons (msize);
173 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
175 msg->ats_count = htonl (atsi_count);
177 msg->address_length = htons (plugin_addr_len);
178 msg->address_active = ntohl ((uint32_t) active);
179 msg->plugin_name_length = htons (plugin_name_length);
180 msg->bandwidth_out = bandwidth_out;
181 msg->bandwidth_in = bandwidth_in;
182 atsp = (struct GNUNET_ATS_Information *) &msg[1];
183 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
184 addrp = (char *) &atsp[atsi_count];
185 memcpy (addrp, plugin_addr, plugin_addr_len);
186 strcpy (&addrp[plugin_addr_len], plugin_name);
187 GNUNET_SERVER_notification_context_unicast (nc,
195 * Transmit the given performance information to all performance
198 * @param peer peer for which this is an address suggestion
199 * @param plugin_name 0-termintated string specifying the transport plugin
200 * @param plugin_addr binary address for the plugin to use
201 * @param plugin_addr_len number of bytes in @a plugin_addr
202 * @param active #GNUNET_YES if this address is actively used
203 * to maintain a connection to a peer;
204 * #GNUNET_NO if the address is not actively used;
205 * #GNUNET_SYSERR if this address is no longer available for ATS
206 * @param atsi performance data for the address
207 * @param atsi_count number of performance records in @a atsi
208 * @param bandwidth_out assigned outbound bandwidth
209 * @param bandwidth_in assigned inbound bandwidth
212 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
213 const char *plugin_name,
214 const void *plugin_addr,
215 size_t plugin_addr_len,
217 const struct GNUNET_ATS_Information *atsi,
219 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
220 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
222 struct PerformanceClient *pc;
224 for (pc = pc_head; pc != NULL; pc = pc->next)
225 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
227 GAS_performance_notify_client (pc,
234 bandwidth_out, bandwidth_in);
236 GNUNET_STATISTICS_update (GSA_stats,
237 "# performance updates given to clients", 1,
244 * Iterator for called from #GAS_addresses_get_peer_info()
246 * @param cls closure with the `struct PerformanceClient *`
247 * @param id the peer id
248 * @param plugin_name plugin name
249 * @param plugin_addr address
250 * @param plugin_addr_len length of @a plugin_addr
251 * @param active is address actively used
252 * @param atsi ats performance information
253 * @param atsi_count number of ats performance elements in @a atsi
254 * @param bandwidth_out current outbound bandwidth assigned to address
255 * @param bandwidth_in current inbound bandwidth assigned to address
258 peerinfo_it (void *cls,
259 const struct GNUNET_PeerIdentity *id,
260 const char *plugin_name,
261 const void *plugin_addr,
262 size_t plugin_addr_len,
264 const struct GNUNET_ATS_Information *atsi,
266 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
267 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
269 struct PerformanceClient *pc = cls;
271 GNUNET_assert (NULL != pc);
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
278 (unsigned int) ntohl (bandwidth_out.value__),
279 (unsigned int) ntohl (bandwidth_in.value__));
280 GAS_performance_notify_client (pc,
293 * Register a new performance client.
295 * @param client handle of the new client
296 * @param flag flag specifying the type of the client
299 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
302 struct PerformanceClient *pc;
304 GNUNET_break (NULL == find_client (client));
305 pc = GNUNET_new (struct PerformanceClient);
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Adding performance client %s PIC\n",
311 (flag == START_FLAG_PERFORMANCE_WITH_PIC) ? "with" : "without");
313 GNUNET_SERVER_notification_context_add (nc,
315 GNUNET_CONTAINER_DLL_insert (pc_head,
318 GAS_addresses_get_peer_info (GSA_addresses,
326 * Information we need for the callbacks to return a list of addresses
327 * back to the client.
329 struct AddressIteration
332 * Actual handle to the client.
334 struct PerformanceClient *pc;
337 * Are we sending all addresses, or only those that are active?
342 * Which ID should be included in the response?
350 * Send a #GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE with the
351 * given address details to the client identified in @a ai.
353 * @param ai our address information context (identifies the client)
354 * @param id the peer id this address is for
355 * @param plugin_name name of the plugin that supports this address
356 * @param plugin_addr address
357 * @param plugin_addr_len length of @a plugin_addr
358 * @param active #GNUNET_YES if this address is actively used
359 * @param atsi ats performance information
360 * @param atsi_count number of ats performance elements in @a atsi
361 * @param bandwidth_out current outbound bandwidth assigned to address
362 * @param bandwidth_in current inbound bandwidth assigned to address
365 transmit_req_addr (struct AddressIteration *ai,
366 const struct GNUNET_PeerIdentity *id,
367 const char *plugin_name,
368 const void *plugin_addr,
369 size_t plugin_addr_len,
371 const struct GNUNET_ATS_Information *atsi,
373 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
374 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
377 struct GNUNET_ATS_Information *atsp;
378 struct PeerInformationMessage *msg;
380 size_t plugin_name_length;
383 if (NULL != plugin_name)
384 plugin_name_length = strlen (plugin_name) + 1;
386 plugin_name_length = 0;
387 msize = sizeof (struct PeerInformationMessage) +
388 atsi_count * sizeof (struct GNUNET_ATS_Information) +
389 plugin_addr_len + plugin_name_length;
390 char buf[msize] GNUNET_ALIGN;
392 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
393 GNUNET_assert (atsi_count <
394 GNUNET_SERVER_MAX_MESSAGE_SIZE /
395 sizeof (struct GNUNET_ATS_Information));
396 msg = (struct PeerInformationMessage *) buf;
397 msg->header.size = htons (msize);
398 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
399 msg->ats_count = htonl (atsi_count);
400 msg->id = htonl (ai->id);
404 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
405 msg->address_length = htons (plugin_addr_len);
406 msg->address_active = ntohl (active);
407 msg->plugin_name_length = htons (plugin_name_length);
408 msg->bandwidth_out = bandwidth_out;
409 msg->bandwidth_in = bandwidth_in;
410 atsp = (struct GNUNET_ATS_Information *) &msg[1];
411 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
412 addrp = (char *) &atsp[atsi_count];
413 if (NULL != plugin_addr)
414 memcpy (addrp, plugin_addr, plugin_addr_len);
415 if (NULL != plugin_name)
416 strcpy (&addrp[plugin_addr_len], plugin_name);
417 GNUNET_SERVER_notification_context_unicast (nc,
425 * Iterator for #GAS_addresses_get_peer_info(), called with peer-specific
426 * information to be passed back to the client.
428 * @param cls closure with our `struct AddressIteration *`
429 * @param id the peer id
430 * @param plugin_name plugin name
431 * @param plugin_addr address
432 * @param plugin_addr_len length of @a plugin_addr
433 * @param active is address actively used
434 * @param atsi ats performance information
435 * @param atsi_count number of ats performance elements in @a atsi
436 * @param bandwidth_out current outbound bandwidth assigned to address
437 * @param bandwidth_in current inbound bandwidth assigned to address
440 req_addr_peerinfo_it (void *cls,
441 const struct GNUNET_PeerIdentity *id,
442 const char *plugin_name,
443 const void *plugin_addr,
444 size_t plugin_addr_len,
446 const struct GNUNET_ATS_Information *atsi,
448 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
449 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
451 struct AddressIteration *ai = cls;
454 (NULL == plugin_name) &&
455 (NULL == plugin_addr) )
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458 "Address iteration done for one peer\n");
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
463 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
466 (unsigned int) ntohl (bandwidth_out.value__),
467 (unsigned int) ntohl (bandwidth_in.value__));
469 /* Transmit result (either if address is active, or if
470 client wanted all addresses) */
471 if ( (GNUNET_YES == ai->all) ||
472 (GNUNET_YES == active))
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u\n",
476 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
479 (unsigned int) ntohl (bandwidth_out.value__),
480 (unsigned int) ntohl (bandwidth_in.value__));
481 transmit_req_addr (ai,
484 plugin_addr, plugin_addr_len,
495 * Handle 'address list request' messages from clients.
497 * @param cls unused, NULL
498 * @param client client that sent the request
499 * @param message the request message
502 GAS_handle_request_address_list (void *cls,
503 struct GNUNET_SERVER_Client *client,
504 const struct GNUNET_MessageHeader *message)
506 struct PerformanceClient *pc;
507 struct AddressIteration ai;
508 const struct AddressListRequestMessage *alrm;
509 struct GNUNET_PeerIdentity allzeros;
510 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Received `%s' message\n",
514 "ADDRESSLIST_REQUEST");
515 if (NULL == (pc = find_client(client)))
520 alrm = (const struct AddressListRequestMessage *) message;
521 ai.all = ntohl (alrm->all);
522 ai.id = ntohl (alrm->id);
525 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
526 bandwidth_zero.value__ = htonl (0);
527 if (0 == memcmp (&alrm->peer,
529 sizeof (struct GNUNET_PeerIdentity)))
531 /* Return addresses for all peers */
532 GAS_addresses_get_peer_info (GSA_addresses,
534 &req_addr_peerinfo_it,
539 /* Return addresses for a specific peer */
540 GAS_addresses_get_peer_info (GSA_addresses,
542 &req_addr_peerinfo_it,
545 transmit_req_addr (&ai,
551 GNUNET_SERVER_receive_done (client,
557 * Handle 'reservation request' messages from clients.
559 * @param cls unused, NULL
560 * @param client client that sent the request
561 * @param message the request message
564 GAS_handle_reservation_request (void *cls,
565 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,
582 "Received `%s' message\n",
583 "RESERVATION_REQUEST");
584 amount = (int32_t) ntohl (msg->amount);
585 res_delay = GAS_reservations_reserve (&msg->peer, amount);
586 if (res_delay.rel_value_us > 0)
588 result.header.size = htons (sizeof (struct ReservationResultMessage));
589 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
590 result.amount = htonl (amount);
591 result.peer = msg->peer;
592 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
593 GNUNET_STATISTICS_update (GSA_stats,
594 "# 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,
640 "# preference change requests processed",
642 pi = (const struct PreferenceInformation *) &msg[1];
643 for (i = 0; i < nump; i++)
644 GAS_addresses_preference_change (GSA_addresses,
647 (enum GNUNET_ATS_PreferenceKind)
648 ntohl (pi[i].preference_kind),
649 pi[i].preference_value);
650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
655 * Handle 'preference feedback' messages from clients.
657 * @param cls unused, NULL
658 * @param client client that sent the request
659 * @param message the request message
662 GAS_handle_preference_feedback (void *cls,
663 struct GNUNET_SERVER_Client *client,
664 const struct GNUNET_MessageHeader *message)
666 const struct FeedbackPreferenceMessage *msg;
667 const struct PreferenceInformation *pi;
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
673 "Received `%s' message\n",
674 "PREFERENCE_FEEDBACK");
675 msize = ntohs (message->size);
676 if (msize < sizeof (struct FeedbackPreferenceMessage))
679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
682 msg = (const struct FeedbackPreferenceMessage *) message;
683 nump = ntohl (msg->num_feedback);
685 sizeof (struct FeedbackPreferenceMessage) +
686 nump * sizeof (struct PreferenceInformation))
689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
692 GNUNET_STATISTICS_update (GSA_stats,
693 "# preference feedbacks requests processed",
695 pi = (const struct PreferenceInformation *) &msg[1];
696 for (i = 0; i < nump; i++)
697 GAS_addresses_preference_feedback (GSA_addresses,
700 GNUNET_TIME_relative_ntoh(msg->scope),
701 (enum GNUNET_ATS_PreferenceKind)
702 ntohl (pi[i].preference_kind),
703 pi[i].preference_value);
704 GNUNET_SERVER_receive_done (client, GNUNET_OK);
710 * Initialize performance subsystem.
712 * @param server handle to our server
713 * @param addresses the address handle to use
716 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
717 struct GAS_Addresses_Handle *addresses)
719 GSA_addresses = addresses;
720 nc = GNUNET_SERVER_notification_context_create (server, 128);
725 * Shutdown performance subsystem.
728 GAS_performance_done ()
730 GNUNET_SERVER_notification_context_destroy (nc);
734 /* end of gnunet-service-ats_performance.c */