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