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;
83 static struct GAS_Addresses_Handle *GSA_addresses;
86 * Head of linked list of all clients to this service.
88 static struct PerformanceClient *pc_head;
91 * Tail of linked list of all clients to this service.
93 static struct PerformanceClient *pc_tail;
96 * Context for sending messages to performance clients.
98 static struct GNUNET_SERVER_NotificationContext *nc;
102 * Find the performance client associated with the given handle.
104 * @param client server handle
105 * @return internal handle
107 static struct PerformanceClient *
108 find_client (struct GNUNET_SERVER_Client *client)
110 struct PerformanceClient *pc;
112 for (pc = pc_head; pc != NULL; pc = pc->next)
113 if (pc->client == client)
119 * Unregister a client (which may have been a performance client,
120 * but this is not assured).
122 * @param client handle of the (now dead) client
125 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
127 struct PerformanceClient *pc;
128 pc = find_client (client);
131 GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
132 GNUNET_SERVER_client_drop (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 if (GNUNET_NO == active)
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267 "Callback for peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
270 ntohl (bandwidth_out.value__),
271 ntohl (bandwidth_in.value__));
272 GAS_performance_notify_client(pc,
274 plugin_name, plugin_addr, plugin_addr_len,
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_SERVER_client_keep (client);
317 GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
319 /* Send information about clients */
320 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)
336 struct GNUNET_ATS_Information *atsp;
337 struct PeerInformationMessage *msg;
339 size_t plugin_name_length;
342 if (NULL != plugin_name)
343 plugin_name_length = strlen (plugin_name) + 1;
345 plugin_name_length = 0;
346 msize = sizeof (struct PeerInformationMessage) +
347 atsi_count * sizeof (struct GNUNET_ATS_Information) +
348 plugin_addr_len + plugin_name_length;
349 char buf[msize] GNUNET_ALIGN;
351 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
352 GNUNET_assert (atsi_count <
353 GNUNET_SERVER_MAX_MESSAGE_SIZE /
354 sizeof (struct GNUNET_ATS_Information));
355 msg = (struct PeerInformationMessage *) buf;
356 msg->header.size = htons (msize);
357 msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
358 msg->ats_count = htonl (atsi_count);
359 msg->id = htonl (ai->id);
363 memset (&msg->peer, '\0', sizeof (struct GNUNET_PeerIdentity));
364 msg->address_length = htons (plugin_addr_len);
365 msg->address_active = ntohl (active);
366 msg->plugin_name_length = htons (plugin_name_length);
367 msg->bandwidth_out = bandwidth_out;
368 msg->bandwidth_in = bandwidth_in;
369 atsp = (struct GNUNET_ATS_Information *) &msg[1];
370 memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
371 addrp = (char *) &atsp[atsi_count];
372 if (NULL != plugin_addr)
373 memcpy (addrp, plugin_addr, plugin_addr_len);
374 if (NULL != plugin_name)
375 strcpy (&addrp[plugin_addr_len], plugin_name);
376 GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
381 req_addr_peerinfo_it (void *cls,
382 const struct GNUNET_PeerIdentity *id,
383 const char *plugin_name,
384 const void *plugin_addr, size_t plugin_addr_len,
386 const struct GNUNET_ATS_Information *atsi,
388 struct GNUNET_BANDWIDTH_Value32NBO
390 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
392 struct AddressIteration *ai = cls;
394 GNUNET_assert (NULL != ai);
395 GNUNET_assert (NULL != ai->pc);
396 if (NULL == find_client (ai->pc->client))
397 return; /* Client disconnected */
399 if ((NULL == id) && (NULL == plugin_name) && (NULL == plugin_addr))
401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
402 "Address iteration done\n");
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Callback for %s peer `%s' plugin `%s' BW out %u, BW in %u \n",
407 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
410 (unsigned int) ntohl (bandwidth_out.value__),
411 (unsigned int) ntohl (bandwidth_in.value__));
413 /* Transmit result */
414 if ((GNUNET_YES == ai->all) || (GNUNET_YES == active))
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "Sending result for %s peer `%s' plugin `%s' BW out %u, BW in %u \n",
418 (active == GNUNET_YES) ? "ACTIVE" : "INACTIVE",
421 (unsigned int) ntohl (bandwidth_out.value__),
422 (unsigned int) ntohl (bandwidth_in.value__));
423 transmit_req_addr (cls,
426 plugin_addr, plugin_addr_len,
430 bandwidth_out, bandwidth_in);
436 * Iterator for GAS_handle_request_address_list
438 * @param cls the client requesting information
442 req_addr_peer_it (void *cls,
443 const struct GNUNET_PeerIdentity *id)
445 struct AddressIteration *ai = cls;
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
449 GAS_addresses_get_peer_info (GSA_addresses, id, &req_addr_peerinfo_it, ai);
453 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_monitor (void *cls,
507 struct GNUNET_SERVER_Client *client,
508 const struct GNUNET_MessageHeader *message)
510 struct MonitorMessage *mm = (struct MonitorMessage *) message;
514 msg_size = ntohs (message->size);
515 if (msg_size < sizeof (struct MonitorMessage))
520 switch (ntohl (mm->op)) {
522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received `START' message\n");
525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received `STOP' message\n");
536 * Handle 'reservation request' messages from clients.
538 * @param cls unused, NULL
539 * @param client client that sent the request
540 * @param message the request message
543 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
544 const struct GNUNET_MessageHeader *message)
546 const struct ReservationRequestMessage *msg =
547 (const struct ReservationRequestMessage *) message;
548 struct ReservationResultMessage result;
550 struct GNUNET_TIME_Relative res_delay;
552 if (NULL == find_client (client))
554 /* missing start message! */
556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
560 "RESERVATION_REQUEST");
561 amount = (int32_t) ntohl (msg->amount);
562 res_delay = GAS_reservations_reserve (&msg->peer, amount);
563 if (res_delay.rel_value > 0)
565 result.header.size = htons (sizeof (struct ReservationResultMessage));
566 result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
567 result.amount = htonl (amount);
568 result.peer = msg->peer;
569 result.res_delay = GNUNET_TIME_relative_hton (res_delay);
570 GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
572 GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
574 GNUNET_SERVER_receive_done (client, GNUNET_OK);
579 * Handle 'preference change' messages from clients.
581 * @param cls unused, NULL
582 * @param client client that sent the request
583 * @param message the request message
586 GAS_handle_preference_change (void *cls,
587 struct GNUNET_SERVER_Client *client,
588 const struct GNUNET_MessageHeader *message)
590 const struct ChangePreferenceMessage *msg;
591 const struct PreferenceInformation *pi;
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
597 "PREFERENCE_CHANGE");
598 msize = ntohs (message->size);
599 if (msize < sizeof (struct ChangePreferenceMessage))
602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
605 msg = (const struct ChangePreferenceMessage *) message;
606 nump = ntohl (msg->num_preferences);
608 sizeof (struct ChangePreferenceMessage) +
609 nump * sizeof (struct PreferenceInformation))
612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
615 GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
617 pi = (const struct PreferenceInformation *) &msg[1];
618 for (i = 0; i < nump; i++)
619 GAS_addresses_change_preference (GSA_addresses,
622 (enum GNUNET_ATS_PreferenceKind)
623 ntohl (pi[i].preference_kind),
624 pi[i].preference_value);
625 GNUNET_SERVER_receive_done (client, GNUNET_OK);
630 * Initialize performance subsystem.
632 * @param server handle to our server
633 * @param addresses the address handle to use
636 GAS_performance_init (struct GNUNET_SERVER_Handle *server,
637 struct GAS_Addresses_Handle *addresses)
639 GSA_addresses = addresses;
640 nc = GNUNET_SERVER_notification_context_create (server, 128);
645 * Shutdown performance subsystem.
648 GAS_performance_done ()
650 GNUNET_SERVER_notification_context_destroy (nc);
654 /* end of gnunet-service-ats_performance.c */