- changes
[oweals/gnunet.git] / src / ats / gnunet-service-ats_performance.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file ats/gnunet-service-ats_performance.c
23  * @brief ats service, interaction with 'performance' API
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
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"
32 #include "ats.h"
33
34
35 /**
36  * We keep clients that are interested in performance in a linked list.
37  */
38 struct PerformanceClient
39 {
40   /**
41    * Next in doubly-linked list.
42    */
43   struct PerformanceClient *next;
44
45   /**
46    * Previous in doubly-linked list.
47    */
48   struct PerformanceClient *prev;
49
50   /**
51    * Actual handle to the client.
52    */
53   struct GNUNET_SERVER_Client *client;
54
55   /**
56    * Options for the client.
57    */
58   enum StartFlag flag;
59
60 };
61
62
63 /**
64  * We keep clients that are interested in performance in a linked list.
65  */
66 struct AddressIteration
67 {
68   /**
69    * Actual handle to the client.
70    */
71   struct PerformanceClient *pc;
72
73   int all;
74
75   unsigned int msg_type;
76 };
77
78
79 /**
80  * Head of linked list of all clients to this service.
81  */
82 static struct PerformanceClient *pc_head;
83
84 /**
85  * Tail of linked list of all clients to this service.
86  */
87 static struct PerformanceClient *pc_tail;
88
89 /**
90  * Context for sending messages to performance clients.
91  */
92 static struct GNUNET_SERVER_NotificationContext *nc;
93
94
95 /**
96  * Find the performance client associated with the given handle.
97  *
98  * @param client server handle
99  * @return internal handle
100  */
101 static struct PerformanceClient *
102 find_client (struct GNUNET_SERVER_Client *client)
103 {
104   struct PerformanceClient *pc;
105
106   for (pc = pc_head; pc != NULL; pc = pc->next)
107     if (pc->client == client)
108       return pc;
109   return NULL;
110 }
111
112 /**
113  * Unregister a client (which may have been a performance client,
114  * but this is not assured).
115  *
116  * @param client handle of the (now dead) client
117  */
118 void
119 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
120 {
121   struct PerformanceClient *pc;
122   pc = find_client (client);
123   if (NULL == pc)
124     return;
125   GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
126   GNUNET_SERVER_client_drop (client);
127   GNUNET_free (pc);
128 }
129
130 /**
131  * Transmit the given performance information to all performance
132  * clients.
133  *
134  * @param pc performance client to send to
135  * @param peer peer for which this is an address suggestion
136  * @param plugin_name 0-termintated string specifying the transport plugin
137  * @param plugin_addr binary address for the plugin to use
138  * @param plugin_addr_len number of bytes in plugin_addr
139  * @param atsi performance data for the address
140  * @param atsi_count number of performance records in 'ats'
141  * @param bandwidth_out assigned outbound bandwidth
142  * @param bandwidth_in assigned inbound bandwidth
143  */
144 void
145 GAS_performance_notify_client (struct PerformanceClient *pc,
146                                const struct GNUNET_PeerIdentity *peer,
147                                const char *plugin_name,
148                                const void *plugin_addr, size_t plugin_addr_len,
149                                const int active,
150                                const struct GNUNET_ATS_Information *atsi,
151                                uint32_t atsi_count,
152                                struct GNUNET_BANDWIDTH_Value32NBO
153                                bandwidth_out,
154                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
155 {
156
157   struct PeerInformationMessage *msg;
158   size_t plugin_name_length = strlen (plugin_name) + 1;
159   size_t msize =
160       sizeof (struct PeerInformationMessage) +
161       atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
162       plugin_name_length;
163   char buf[msize] GNUNET_ALIGN;
164   struct GNUNET_ATS_Information *atsp;
165   char *addrp;
166
167   GNUNET_assert (NULL != pc);
168   if (NULL == find_client (pc->client))
169     return; /* Client disconnected */
170
171   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
172   GNUNET_assert (atsi_count <
173                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
174                  sizeof (struct GNUNET_ATS_Information));
175   msg = (struct PeerInformationMessage *) buf;
176   msg->header.size = htons (msize);
177   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
178   msg->ats_count = htonl (atsi_count);
179   msg->peer = *peer;
180   msg->address_length = htons (plugin_addr_len);
181   msg->address_active = ntohl (active);
182   msg->plugin_name_length = htons (plugin_name_length);
183   msg->bandwidth_out = bandwidth_out;
184   msg->bandwidth_in = bandwidth_in;
185   atsp = (struct GNUNET_ATS_Information *) &msg[1];
186   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
187   addrp = (char *) &atsp[atsi_count];
188   memcpy (addrp, plugin_addr, plugin_addr_len);
189   strcpy (&addrp[plugin_addr_len], plugin_name);
190   GNUNET_SERVER_notification_context_unicast (nc, pc->client, &msg->header,
191                                               GNUNET_YES);
192 }
193
194
195 /**
196  * Transmit the given performance information to all performance
197  * clients.
198  *
199  * @param peer peer for which this is an address suggestion
200  * @param plugin_name 0-termintated string specifying the transport plugin
201  * @param plugin_addr binary address for the plugin to use
202  * @param plugin_addr_len number of bytes in plugin_addr
203  * @param atsi performance data for the address
204  * @param atsi_count number of performance records in 'ats'
205  * @param bandwidth_out assigned outbound bandwidth
206  * @param bandwidth_in assigned inbound bandwidth
207  */
208 void
209 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
210                                 const char *plugin_name,
211                                 const void *plugin_addr, size_t plugin_addr_len,
212                                 const int active,
213                                 const struct GNUNET_ATS_Information *atsi,
214                                 uint32_t atsi_count,
215                                 struct GNUNET_BANDWIDTH_Value32NBO
216                                 bandwidth_out,
217                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
218 {
219   struct PerformanceClient *pc;
220
221   for (pc = pc_head; pc != NULL; pc = pc->next)
222     if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
223     {
224         GAS_performance_notify_client (pc,
225                                        peer,
226                                        plugin_name, plugin_addr, plugin_addr_len,
227                                        active,
228                                        atsi, atsi_count,
229                                        bandwidth_out, bandwidth_in);
230     }
231   GNUNET_STATISTICS_update (GSA_stats,
232                             "# performance updates given to clients", 1,
233                             GNUNET_NO);
234 }
235
236
237 static void
238 peerinfo_it (void *cls,
239              const struct GNUNET_PeerIdentity *id,
240              const char *plugin_name,
241              const void *plugin_addr, size_t plugin_addr_len,
242              const int active,
243              const struct GNUNET_ATS_Information *atsi,
244              uint32_t atsi_count,
245              struct GNUNET_BANDWIDTH_Value32NBO
246              bandwidth_out,
247              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
248 {
249   struct PerformanceClient *pc = cls;
250   GNUNET_assert (NULL != pc);
251   if (NULL == id)
252     return;
253
254   if (GNUNET_NO == active)
255     return;
256
257   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258               "Callback for peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
259               GNUNET_i2s (id),
260               plugin_name,
261               ntohl (bandwidth_out.value__),
262               ntohl (bandwidth_in.value__));
263   GAS_performance_notify_client(pc,
264                                 id,
265                                 plugin_name, plugin_addr, plugin_addr_len,
266                                 active,
267                                 atsi, atsi_count,
268                                 bandwidth_out, bandwidth_in);
269 }
270
271
272 /**
273  * Iterator for GAS_performance_add_client
274  *
275  * @param cls the client requesting information
276  * @param id result
277  */
278 static void
279 peer_it (void *cls,
280          const struct GNUNET_PeerIdentity *id)
281 {
282   struct PerformanceClient *pc = cls;
283   if (NULL != id)
284   {
285     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
286     GAS_addresses_get_peer_info (id, &peerinfo_it, pc);
287   }
288 }
289
290 /**
291  * Register a new performance client.
292  *
293  * @param client handle of the new client
294  * @param flag flag specifying the type of the client
295  */
296 void
297 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
298                             enum StartFlag flag)
299 {
300   struct PerformanceClient *pc;
301   GNUNET_break (NULL == find_client (client));
302
303   pc = GNUNET_malloc (sizeof (struct PerformanceClient));
304   pc->client = client;
305   pc->flag = flag;
306   GNUNET_SERVER_notification_context_add (nc, client);
307   GNUNET_SERVER_client_keep (client);
308   GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
309
310   /* Send information about clients */
311   GAS_addresses_iterate_peers (&peer_it, pc);
312 }
313
314
315
316 static void
317 req_addr_peerinfo_it (void *cls,
318              const struct GNUNET_PeerIdentity *id,
319              const char *plugin_name,
320              const void *plugin_addr, size_t plugin_addr_len,
321              const int active,
322              const struct GNUNET_ATS_Information *atsi,
323              uint32_t atsi_count,
324              struct GNUNET_BANDWIDTH_Value32NBO
325              bandwidth_out,
326              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
327 {
328   struct AddressIteration *ai = cls;
329   struct PeerInformationMessage *msg;
330   size_t plugin_name_length;
331   size_t msize;
332   struct GNUNET_ATS_Information *atsp;
333   char *addrp;
334
335
336   GNUNET_assert (NULL != ai);
337   GNUNET_assert (NULL != ai->pc);
338   if (NULL == find_client (ai->pc->client))
339     return; /* Client disconnected */
340
341   if ((NULL == id) && (NULL == id) && (NULL == id))
342   {
343       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344                   "Address iteration done\n");
345       return;
346   }
347   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
348               "Callback for peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
349               GNUNET_i2s (id),
350               plugin_name,
351               ntohl (bandwidth_out.value__),
352               ntohl (bandwidth_in.value__));
353
354   /* Transmit result */
355
356   plugin_name_length = strlen (plugin_name) + 1;
357   msize = sizeof (struct PeerInformationMessage) +
358           atsi_count * sizeof (struct GNUNET_ATS_Information) +
359           plugin_addr_len + plugin_name_length;
360   char buf[msize] GNUNET_ALIGN;
361
362   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
363   GNUNET_assert (atsi_count <
364                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
365                  sizeof (struct GNUNET_ATS_Information));
366   msg = (struct PeerInformationMessage *) buf;
367   msg->header.size = htons (msize);
368   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESSLIST_RESPONSE);
369   msg->ats_count = htonl (atsi_count);
370   msg->peer = *id;
371   msg->address_length = htons (plugin_addr_len);
372   msg->address_active = ntohl (active);
373   msg->plugin_name_length = htons (plugin_name_length);
374   msg->bandwidth_out = bandwidth_out;
375   msg->bandwidth_in = bandwidth_in;
376   atsp = (struct GNUNET_ATS_Information *) &msg[1];
377   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
378   addrp = (char *) &atsp[atsi_count];
379   memcpy (addrp, plugin_addr, plugin_addr_len);
380   strcpy (&addrp[plugin_addr_len], plugin_name);
381   GNUNET_SERVER_notification_context_unicast (nc, ai->pc->client, &msg->header,
382                                               GNUNET_YES);
383 }
384
385
386 /**
387  * Iterator for GAS_handle_request_address_list
388  *
389  * @param cls the client requesting information
390  * @param id result
391  */
392 static void
393 req_addr_peer_it (void *cls,
394          const struct GNUNET_PeerIdentity *id)
395 {
396   struct AddressIteration *ai = cls;
397   if (NULL != id)
398   {
399     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Callback for peer `%s'\n", GNUNET_i2s (id));
400     GAS_addresses_get_peer_info (id, &req_addr_peerinfo_it, ai);
401   }
402   else
403   {
404       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer iteration done\n");
405   }
406 }
407
408 /**
409  * Handle 'address list request' messages from clients.
410  *
411  * @param cls unused, NULL
412  * @param client client that sent the request
413  * @param message the request message
414  */
415 void
416 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
417                                  const struct GNUNET_MessageHeader *message)
418 {
419   struct PerformanceClient *pc;
420   struct AddressIteration ai;
421   struct AddressListRequestMessage * alrm = (struct AddressListRequestMessage *) message;
422   struct GNUNET_PeerIdentity allzeros;
423
424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
425               "ADDRESSLIST_REQUEST");
426
427   if (NULL == (pc = find_client(client)))
428   {
429       GNUNET_break (0);
430       return;
431   }
432
433   ai.all = ntohl (alrm->all);
434   ai.pc = pc;
435
436   memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
437   if (0 == memcmp (&alrm->peer, &allzeros, sizeof (struct GNUNET_PeerIdentity)))
438   {
439       /* Return addresses for all peers */
440       GAS_addresses_iterate_peers (&req_addr_peer_it, &ai);
441   }
442   else
443   {
444       /* Return addresses for a specific peer */
445       GAS_addresses_get_peer_info (&alrm->peer, &req_addr_peerinfo_it, &ai);
446   }
447 }
448
449
450
451 /**
452  * Handle 'reservation request' messages from clients.
453  *
454  * @param cls unused, NULL
455  * @param client client that sent the request
456  * @param message the request message
457  */
458 void
459 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
460                                 const struct GNUNET_MessageHeader *message)
461 {
462   const struct ReservationRequestMessage *msg =
463       (const struct ReservationRequestMessage *) message;
464   struct ReservationResultMessage result;
465   int32_t amount;
466   struct GNUNET_TIME_Relative res_delay;
467
468   if (NULL == find_client (client))
469   {
470     /* missing start message! */
471     GNUNET_break (0);
472     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
473     return;
474   }
475   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
476               "RESERVATION_REQUEST");
477   amount = (int32_t) ntohl (msg->amount);
478   res_delay = GAS_reservations_reserve (&msg->peer, amount);
479   if (res_delay.rel_value > 0)
480     amount = 0;
481   result.header.size = htons (sizeof (struct ReservationResultMessage));
482   result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
483   result.amount = htonl (amount);
484   result.peer = msg->peer;
485   result.res_delay = GNUNET_TIME_relative_hton (res_delay);
486   GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
487                             GNUNET_NO);
488   GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
489                                               GNUNET_NO);
490   GNUNET_SERVER_receive_done (client, GNUNET_OK);
491 }
492
493
494 /**
495  * Handle 'preference change' messages from clients.
496  *
497  * @param cls unused, NULL
498  * @param client client that sent the request
499  * @param message the request message
500  */
501 void
502 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
503                               const struct GNUNET_MessageHeader *message)
504 {
505   const struct ChangePreferenceMessage *msg;
506   const struct PreferenceInformation *pi;
507   uint16_t msize;
508   uint32_t nump;
509   uint32_t i;
510
511   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
512               "PREFERENCE_CHANGE");
513   msize = ntohs (message->size);
514   if (msize < sizeof (struct ChangePreferenceMessage))
515   {
516     GNUNET_break (0);
517     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
518     return;
519   }
520   msg = (const struct ChangePreferenceMessage *) message;
521   nump = ntohl (msg->num_preferences);
522   if (msize !=
523       sizeof (struct ChangePreferenceMessage) +
524       nump * sizeof (struct PreferenceInformation))
525   {
526     GNUNET_break (0);
527     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
528     return;
529   }
530   GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
531                             1, GNUNET_NO);
532   pi = (const struct PreferenceInformation *) &msg[1];
533   for (i = 0; i < nump; i++)
534     GAS_addresses_change_preference (&msg->peer,
535                                      (enum GNUNET_ATS_PreferenceKind)
536                                      ntohl (pi[i].preference_kind),
537                                      pi[i].preference_value);
538   GNUNET_SERVER_receive_done (client, GNUNET_OK);
539 }
540
541
542 /**
543  * Initialize performance subsystem.
544  *
545  * @param server handle to our server
546  */
547 void
548 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
549 {
550   nc = GNUNET_SERVER_notification_context_create (server, 128);
551 }
552
553
554 /**
555  * Shutdown performance subsystem.
556  */
557 void
558 GAS_performance_done ()
559 {
560   GNUNET_SERVER_notification_context_destroy (nc);
561   nc = NULL;
562 }
563
564 /* end of gnunet-service-ats_performance.c */