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"
36 * We keep clients that are interested in performance in a linked list.
38 struct PerformanceClient
41 * Next in doubly-linked list.
43 struct PerformanceClient *next;
46 * Previous in doubly-linked list.
48 struct PerformanceClient *prev;
51 * Actual handle to the client.
53 struct GNUNET_SERVER_Client *client;
56 * Options for the client.
64 * We keep clients that are interested in performance in a linked list.
66 struct AddressIteration
69 * Actual handle to the client.
71 struct PerformanceClient *pc;
77 unsigned int msg_type;
82 * Head of linked list of all clients to this service.
84 static struct PerformanceClient *pc_head;
87 * Tail of linked list of all clients to this service.
89 static struct PerformanceClient *pc_tail;
92 * Context for sending messages to performance clients.
94 static struct GNUNET_SERVER_NotificationContext *nc;
98 * Find the performance client associated with the given handle.
100 * @param client server handle
101 * @return internal handle
103 static struct PerformanceClient *
104 find_client (struct GNUNET_SERVER_Client *client)
106 struct PerformanceClient *pc;
108 for (pc = pc_head; pc != NULL; pc = pc->next)
109 if (pc->client == client)
115 * Unregister a client (which may have been a performance client,
116 * but this is not assured).
118 * @param client handle of the (now dead) client
121 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
123 struct PerformanceClient *pc;
124 pc = find_client (client);
127 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
128 GNUNET_SERVER_client_drop (client);
133 * Transmit the given performance information to all performance
136 * @param pc performance client to send to
137 * @param peer peer for which this is an address suggestion
138 * @param plugin_name 0-termintated string specifying the transport plugin
139 * @param plugin_addr binary address for the plugin to use
140 * @param plugin_addr_len number of bytes in plugin_addr
141 * @param active is this address active
142 * @param atsi performance data for the address
143 * @param atsi_count number of performance records in 'ats'
144 * @param bandwidth_out assigned outbound bandwidth
145 * @param bandwidth_in assigned inbound bandwidth
148 GAS_performance_notify_client (struct PerformanceClient *pc,
149 const struct GNUNET_PeerIdentity *peer,
150 const char *plugin_name,
151 const void *plugin_addr, size_t plugin_addr_len,
153 const struct GNUNET_ATS_Information *atsi,
155 struct GNUNET_BANDWIDTH_Value32NBO
157 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
160 struct PeerInformationMessage *msg;
161 size_t plugin_name_length = strlen (plugin_name) + 1;
163 sizeof (struct PeerInformationMessage) +
164 atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
166 char buf[msize] GNUNET_ALIGN;
167 struct GNUNET_ATS_Information *atsp;
170 GNUNET_assert (NULL != pc);
171 if (NULL == find_client (pc->client))
172 return; /* Client disconnected */
174 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
175 GNUNET_assert (atsi_count <
176 GNUNET_SERVER_MAX_MESSAGE_SIZE /
177 sizeof (struct GNUNET_ATS_Information));
178 msg = (struct PeerInformationMessage *) buf;
179 msg->header.size = htons (msize);
180 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
181 msg->ats_count = htonl (atsi_count);
183 msg->address_length = htons (plugin_addr_len);
184 msg->address_active = ntohl (active);
185 msg->plugin_name_length = htons (plugin_name_length);
186 msg->bandwidth_out = bandwidth_out;
187 msg->bandwidth_in = bandwidth_in;
188 atsp = (struct GNUNET_ATS_Information *) &msg[1];
189 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
190 addrp = (char *) &atsp[atsi_count];
191 memcpy (addrp, plugin_addr, plugin_addr_len);
192 strcpy (&addrp[plugin_addr_len], plugin_name);
193 GNUNET_SERVER_notification_context_unicast (nc, pc->client, &msg->header,
199 * Transmit the given performance information to all performance
202 * @param peer peer for which this is an address suggestion
203 * @param plugin_name 0-termintated string specifying the transport plugin
204 * @param plugin_addr binary address for the plugin to use
205 * @param plugin_addr_len number of bytes in plugin_addr
206 * @param active is this address active
207 * @param atsi performance data for the address
208 * @param atsi_count number of performance records in 'ats'
209 * @param bandwidth_out assigned outbound bandwidth
210 * @param bandwidth_in assigned inbound bandwidth
213 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
214 const char *plugin_name,
215 const void *plugin_addr, size_t plugin_addr_len,
217 const struct GNUNET_ATS_Information *atsi,
219 struct GNUNET_BANDWIDTH_Value32NBO
221 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
223 struct PerformanceClient *pc;
225 for (pc = pc_head; pc != NULL; pc = pc->next)
226 if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
228 GAS_performance_notify_client (pc,
230 plugin_name, plugin_addr, plugin_addr_len,
233 bandwidth_out, bandwidth_in);
235 GNUNET_STATISTICS_update (GSA_stats,
236 "# performance updates given to clients", 1,
242 peerinfo_it (void *cls,
243 const struct GNUNET_PeerIdentity *id,
244 const char *plugin_name,
245 const void *plugin_addr, size_t plugin_addr_len,
247 const struct GNUNET_ATS_Information *atsi,
249 struct GNUNET_BANDWIDTH_Value32NBO
251 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
253 struct PerformanceClient *pc = cls;
254 GNUNET_assert (NULL != pc);
258 if (GNUNET_NO == active)
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Callback for peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
265 ntohl (bandwidth_out.value__),
266 ntohl (bandwidth_in.value__));
267 GAS_performance_notify_client(pc,
269 plugin_name, plugin_addr, plugin_addr_len,
272 bandwidth_out, bandwidth_in);
277 * Iterator for GAS_performance_add_client
279 * @param cls the client requesting information
284 const struct GNUNET_PeerIdentity *id)
286 struct PerformanceClient *pc = cls;
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
290 GAS_addresses_get_peer_info (id, &peerinfo_it, pc);
295 * Register a new performance client.
297 * @param client handle of the new client
298 * @param flag flag specifying the type of the client
301 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
304 struct PerformanceClient *pc;
305 GNUNET_break (NULL == find_client (client));
307 pc = GNUNET_malloc (sizeof (struct PerformanceClient));
310 GNUNET_SERVER_notification_context_add (nc, client);
311 GNUNET_SERVER_client_keep (client);
312 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
314 /* Send information about clients */
315 GAS_addresses_iterate_peers (&peer_it, pc);
318 static void transmit_req_addr (struct AddressIteration *ai,
319 const struct GNUNET_PeerIdentity *id,
320 const char *plugin_name,
321 const void *plugin_addr, size_t plugin_addr_len,
323 const struct GNUNET_ATS_Information *atsi,
325 struct GNUNET_BANDWIDTH_Value32NBO
327 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
331 struct GNUNET_ATS_Information *atsp;
332 struct PeerInformationMessage *msg;
334 size_t plugin_name_length;
337 if (NULL != plugin_name)
338 plugin_name_length = strlen (plugin_name) + 1;
340 plugin_name_length = 0;
341 msize = sizeof (struct PeerInformationMessage) +
342 atsi_count * sizeof (struct GNUNET_ATS_Information) +
343 plugin_addr_len + plugin_name_length;
344 char buf[msize] GNUNET_ALIGN;
346 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
347 GNUNET_assert (atsi_count <
348 GNUNET_SERVER_MAX_MESSAGE_SIZE /
349 sizeof (struct GNUNET_ATS_Information));
350 msg = (struct PeerInformationMessage *) buf;
351 msg->header.size = htons (msize);
352 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
353 msg->ats_count = htonl (atsi_count);
354 msg->id = htonl (ai->id);
358 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
359 msg->address_length = htons (plugin_addr_len);
360 msg->address_active = ntohl (active);
361 msg->plugin_name_length = htons (plugin_name_length);
362 msg->bandwidth_out = bandwidth_out;
363 msg->bandwidth_in = bandwidth_in;
364 atsp = (struct GNUNET_ATS_Information *) &msg[1];
365 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
366 addrp = (char *) &atsp[atsi_count];
367 if (NULL != plugin_addr)
368 memcpy (addrp, plugin_addr, plugin_addr_len);
369 if (NULL != plugin_name)
370 strcpy (&addrp[plugin_addr_len], plugin_name);
371 GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
376 req_addr_peerinfo_it (void *cls,
377 const struct GNUNET_PeerIdentity *id,
378 const char *plugin_name,
379 const void *plugin_addr, size_t plugin_addr_len,
381 const struct GNUNET_ATS_Information *atsi,
383 struct GNUNET_BANDWIDTH_Value32NBO
385 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
387 struct AddressIteration *ai = cls;
389 GNUNET_assert (NULL != ai);
390 GNUNET_assert (NULL != ai->pc);
391 if (NULL == find_client (ai->pc->client))
392 return; /* Client disconnected */
394 if ((NULL == id) && (NULL == id) && (NULL == id))
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Address iteration done\n");
400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401 "Callback for %s peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
402 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
405 ntohl (bandwidth_out.value__),
406 ntohl (bandwidth_in.value__));
408 /* Transmit result */
409 if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Sending result for %s peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
413 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
416 ntohl (bandwidth_out.value__),
417 ntohl (bandwidth_in.value__));
418 transmit_req_addr (cls,
421 plugin_addr, plugin_addr_len,
425 bandwidth_out, bandwidth_in);
431 * Iterator for GAS_handle_request_address_list
433 * @param cls the client requesting information
437 req_addr_peer_it (void *cls,
438 const struct GNUNET_PeerIdentity *id)
440 struct AddressIteration *ai = cls;
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
444 GAS_addresses_get_peer_info (id, &req_addr_peerinfo_it, ai);
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer iteration done\n");
453 * Handle 'address list request' messages from clients.
455 * @param cls unused, NULL
456 * @param client client that sent the request
457 * @param message the request message
460 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
461 const struct GNUNET_MessageHeader *message)
463 struct PerformanceClient *pc;
464 struct AddressIteration ai;
465 struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
466 struct GNUNET_PeerIdentity allzeros;
467 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_zero;
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
470 "ADDRESSLIST_REQUEST");
472 if (NULL == (pc = find_client(client)))
478 ai.all = ntohl (alrm->all);
479 ai.id = ntohl (alrm->id);
482 memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
483 bandwidth_zero.value__ = htonl (0);
484 if (0 == memcmp (&alrm->peer, &allzeros, sizeof (struct GNUNET_PeerIdentity)))
486 /* Return addresses for all peers */
487 GAS_addresses_iterate_peers (&req_addr_peer_it, &ai);
488 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
492 /* Return addresses for a specific peer */
493 GAS_addresses_get_peer_info (&alrm->peer, &req_addr_peerinfo_it, &ai);
494 transmit_req_addr (&ai, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, bandwidth_zero, bandwidth_zero);
496 GNUNET_SERVER_receive_done (client, GNUNET_OK);
502 * Handle 'reservation request' messages from clients.
504 * @param cls unused, NULL
505 * @param client client that sent the request
506 * @param message the request message
509 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
510 const struct GNUNET_MessageHeader *message)
512 const struct ReservationRequestMessage *msg =
513 (const struct ReservationRequestMessage *) message;
514 struct ReservationResultMessage result;
516 struct GNUNET_TIME_Relative res_delay;
518 if (NULL == find_client (client))
520 /* missing start message! */
522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
526 "RESERVATION_REQUEST");
527 amount = (int32_t) ntohl (msg->amount);
528 res_delay = GAS_reservations_reserve (&msg->peer, amount);
529 if (res_delay.rel_value > 0)
531 result.header.size = htons (sizeof (struct ReservationResultMessage));
532 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
533 result.amount = htonl (amount);
534 result.peer = msg->peer;
535 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
536 GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
538 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
540 GNUNET_SERVER_receive_done (client, GNUNET_OK);
545 * Handle 'preference change' messages from clients.
547 * @param cls unused, NULL
548 * @param client client that sent the request
549 * @param message the request message
552 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
553 const struct GNUNET_MessageHeader *message)
555 const struct ChangePreferenceMessage *msg;
556 const struct PreferenceInformation *pi;
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
562 "PREFERENCE_CHANGE");
563 msize = ntohs (message->size);
564 if (msize < sizeof (struct ChangePreferenceMessage))
567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
570 msg = (const struct ChangePreferenceMessage *) message;
571 nump = ntohl (msg->num_preferences);
573 sizeof (struct ChangePreferenceMessage) +
574 nump * sizeof (struct PreferenceInformation))
577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
580 GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
582 pi = (const struct PreferenceInformation *) &msg[1];
583 for (i = 0; i < nump; i++)
584 GAS_addresses_change_preference (&msg->peer,
585 (enum GNUNET_ATS_PreferenceKind)
586 ntohl (pi[i].preference_kind),
587 pi[i].preference_value);
588 GNUNET_SERVER_receive_done (client, GNUNET_OK);
593 * Initialize performance subsystem.
595 * @param server handle to our server
598 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
600 nc = GNUNET_SERVER_notification_context_create (server, 128);
605 * Shutdown performance subsystem.
608 GAS_performance_done ()
610 GNUNET_SERVER_notification_context_destroy (nc);
614 /* end of gnunet-service-ats_performance.c */