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