7800ea46e2379d255cef9c2236edb7f0abb36ea1
[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  * Head of linked list of all clients to this service.
65  */
66 static struct PerformanceClient *pc_head;
67
68 /**
69  * Tail of linked list of all clients to this service.
70  */
71 static struct PerformanceClient *pc_tail;
72
73 /**
74  * Context for sending messages to performance clients.
75  */
76 static struct GNUNET_SERVER_NotificationContext *nc;
77
78
79 /**
80  * Find the performance client associated with the given handle.
81  *
82  * @param client server handle
83  * @return internal handle
84  */
85 static struct PerformanceClient *
86 find_client (struct GNUNET_SERVER_Client *client)
87 {
88   struct PerformanceClient *pc;
89
90   for (pc = pc_head; pc != NULL; pc = pc->next)
91     if (pc->client == client)
92       return pc;
93   return NULL;
94 }
95
96 /**
97  * Unregister a client (which may have been a performance client,
98  * but this is not assured).
99  *
100  * @param client handle of the (now dead) client
101  */
102 void
103 GAS_performance_remove_client (struct GNUNET_SERVER_Client *client)
104 {
105   struct PerformanceClient *pc;
106   pc = find_client (client);
107   if (NULL == pc)
108     return;
109   GNUNET_CONTAINER_DLL_remove (pc_head, pc_tail, pc);
110   GNUNET_SERVER_client_drop (client);
111   GNUNET_free (pc);
112 }
113
114 /**
115  * Transmit the given performance information to all performance
116  * clients.
117  *
118  * @param pc performance client to send to
119  * @param peer peer for which this is an address suggestion
120  * @param plugin_name 0-termintated string specifying the transport plugin
121  * @param plugin_addr binary address for the plugin to use
122  * @param plugin_addr_len number of bytes in plugin_addr
123  * @param atsi performance data for the address
124  * @param atsi_count number of performance records in 'ats'
125  * @param bandwidth_out assigned outbound bandwidth
126  * @param bandwidth_in assigned inbound bandwidth
127  */
128 void
129 GAS_performance_notify_client (struct PerformanceClient *pc,
130                                const struct GNUNET_PeerIdentity *peer,
131                                const char *plugin_name,
132                                const void *plugin_addr, size_t plugin_addr_len,
133                                const struct GNUNET_ATS_Information *atsi,
134                                uint32_t atsi_count,
135                                struct GNUNET_BANDWIDTH_Value32NBO
136                                bandwidth_out,
137                                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
138 {
139
140   struct PeerInformationMessage *msg;
141   size_t plugin_name_length = strlen (plugin_name) + 1;
142   size_t msize =
143       sizeof (struct PeerInformationMessage) +
144       atsi_count * sizeof (struct GNUNET_ATS_Information) + plugin_addr_len +
145       plugin_name_length;
146   char buf[msize] GNUNET_ALIGN;
147   struct GNUNET_ATS_Information *atsp;
148   char *addrp;
149
150   GNUNET_assert (NULL != pc);
151   if (NULL == find_client (pc->client))
152     return; /* Client disconnected */
153
154   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
155   GNUNET_assert (atsi_count <
156                  GNUNET_SERVER_MAX_MESSAGE_SIZE /
157                  sizeof (struct GNUNET_ATS_Information));
158   msg = (struct PeerInformationMessage *) buf;
159   msg->header.size = htons (msize);
160   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
161   msg->ats_count = htonl (atsi_count);
162   msg->peer = *peer;
163   msg->address_length = htons (plugin_addr_len);
164   msg->plugin_name_length = htons (plugin_name_length);
165   msg->bandwidth_out = bandwidth_out;
166   msg->bandwidth_in = bandwidth_in;
167   atsp = (struct GNUNET_ATS_Information *) &msg[1];
168   memcpy (atsp, atsi, sizeof (struct GNUNET_ATS_Information) * atsi_count);
169   addrp = (char *) &atsp[atsi_count];
170   memcpy (addrp, plugin_addr, plugin_addr_len);
171   strcpy (&addrp[plugin_addr_len], plugin_name);
172   GNUNET_SERVER_notification_context_unicast (nc, pc->client, &msg->header,
173                                               GNUNET_YES);
174 }
175
176
177 /**
178  * Transmit the given performance information to all performance
179  * clients.
180  *
181  * @param peer peer for which this is an address suggestion
182  * @param plugin_name 0-termintated string specifying the transport plugin
183  * @param plugin_addr binary address for the plugin to use
184  * @param plugin_addr_len number of bytes in plugin_addr
185  * @param atsi performance data for the address
186  * @param atsi_count number of performance records in 'ats'
187  * @param bandwidth_out assigned outbound bandwidth
188  * @param bandwidth_in assigned inbound bandwidth
189  */
190 void
191 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
192                                 const char *plugin_name,
193                                 const void *plugin_addr, size_t plugin_addr_len,
194                                 const struct GNUNET_ATS_Information *atsi,
195                                 uint32_t atsi_count,
196                                 struct GNUNET_BANDWIDTH_Value32NBO
197                                 bandwidth_out,
198                                 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
199 {
200   struct PerformanceClient *pc;
201
202   for (pc = pc_head; pc != NULL; pc = pc->next)
203     if (pc->flag == START_FLAG_PERFORMANCE_WITH_PIC)
204     {
205         GAS_performance_notify_client (pc,
206                                        peer,
207                                        plugin_name, plugin_addr, plugin_addr_len,
208                                        atsi, atsi_count,
209                                        bandwidth_out, bandwidth_in);
210     }
211
212   GNUNET_STATISTICS_update (GSA_stats,
213                             "# performance updates given to clients", 1,
214                             GNUNET_NO);
215 }
216
217
218 static void
219 peerinfo_it (void *cls,
220              const struct GNUNET_PeerIdentity *id,
221              const char *plugin_name,
222              const void *plugin_addr, size_t plugin_addr_len,
223              const struct GNUNET_ATS_Information *atsi,
224              uint32_t atsi_count,
225              struct GNUNET_BANDWIDTH_Value32NBO
226              bandwidth_out,
227              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
228 {
229   struct PerformanceClient *pc = cls;
230   GNUNET_assert (NULL != pc);
231   if (NULL == id)
232     return;
233   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234               "Callback for peer `%s' plugin `%s' BW out %llu, BW in %llu \n",
235               GNUNET_i2s (id),
236               plugin_name,
237               ntohl (bandwidth_out.value__),
238               ntohl (bandwidth_in.value__));
239   GAS_performance_notify_client(pc,
240                                 id,
241                                 plugin_name, plugin_addr, plugin_addr_len,
242                                 atsi, atsi_count,
243                                 bandwidth_out, bandwidth_in);
244 }
245
246
247 /**
248  * Iterator for GAS_performance_add_client
249  *
250  * @param cls the client requesting information
251  * @param id result
252  */
253 static void
254 peer_it (void *cls,
255          const struct GNUNET_PeerIdentity *id)
256 {
257   struct PerformanceClient *pc = cls;
258   if (NULL != id)
259   {
260     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer `%s'\n", GNUNET_i2s (id));
261     GAS_addresses_get_peer_info (id, &peerinfo_it, pc);
262   }
263 }
264
265 /**
266  * Register a new performance client.
267  *
268  * @param client handle of the new client
269  * @param flag flag specifying the type of the client
270  */
271 void
272 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
273                             enum StartFlag flag)
274 {
275   struct PerformanceClient *pc;
276
277   GNUNET_break (NULL == find_client (client));
278   pc = GNUNET_malloc (sizeof (struct PerformanceClient));
279   pc->client = client;
280   pc->flag = flag;
281   GNUNET_SERVER_notification_context_add (nc, client);
282   GNUNET_SERVER_client_keep (client);
283   GNUNET_CONTAINER_DLL_insert (pc_head, pc_tail, pc);
284
285   /* Send information about clients */
286   GAS_addresses_iterate_peers (&peer_it, pc);
287 }
288
289 /**
290  * Handle 'address list request' messages from clients.
291  *
292  * @param cls unused, NULL
293  * @param client client that sent the request
294  * @param message the request message
295  */
296 void
297 GAS_handle_request_address_list (void *cls, struct GNUNET_SERVER_Client *client,
298                                  const struct GNUNET_MessageHeader *message)
299 {
300   struct PerformanceClient *pc;
301   struct AddressListRequestMessage * alrm = cls;
302   struct GNUNET_PeerIdentity allzeros;
303
304   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
305               "ADDRESSLIST_REQUEST");
306
307   if (NULL == (pc = find_client(client)))
308   {
309       GNUNET_break (0);
310       return;
311   }
312
313   memset (&allzeros, '\0', sizeof (struct GNUNET_PeerIdentity));
314   if (0 == memcmp (&alrm->peer, &allzeros, sizeof (struct GNUNET_PeerIdentity)))
315   {
316       /* Return addresses for all peers */
317       GAS_addresses_iterate_peers (&peer_it, pc);
318   }
319   else
320   {
321       /* Return addresses for a specific peer */
322       GAS_addresses_get_peer_info (&alrm->peer, &peerinfo_it, pc);
323   }
324
325
326 }
327
328
329
330 /**
331  * Handle 'reservation request' messages from clients.
332  *
333  * @param cls unused, NULL
334  * @param client client that sent the request
335  * @param message the request message
336  */
337 void
338 GAS_handle_reservation_request (void *cls, struct GNUNET_SERVER_Client *client,
339                                 const struct GNUNET_MessageHeader *message)
340 {
341   const struct ReservationRequestMessage *msg =
342       (const struct ReservationRequestMessage *) message;
343   struct ReservationResultMessage result;
344   int32_t amount;
345   struct GNUNET_TIME_Relative res_delay;
346
347   if (NULL == find_client (client))
348   {
349     /* missing start message! */
350     GNUNET_break (0);
351     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
352     return;
353   }
354   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
355               "RESERVATION_REQUEST");
356   amount = (int32_t) ntohl (msg->amount);
357   res_delay = GAS_reservations_reserve (&msg->peer, amount);
358   if (res_delay.rel_value > 0)
359     amount = 0;
360   result.header.size = htons (sizeof (struct ReservationResultMessage));
361   result.header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESERVATION_RESULT);
362   result.amount = htonl (amount);
363   result.peer = msg->peer;
364   result.res_delay = GNUNET_TIME_relative_hton (res_delay);
365   GNUNET_STATISTICS_update (GSA_stats, "# reservation requests processed", 1,
366                             GNUNET_NO);
367   GNUNET_SERVER_notification_context_unicast (nc, client, &result.header,
368                                               GNUNET_NO);
369   GNUNET_SERVER_receive_done (client, GNUNET_OK);
370 }
371
372
373 /**
374  * Handle 'preference change' messages from clients.
375  *
376  * @param cls unused, NULL
377  * @param client client that sent the request
378  * @param message the request message
379  */
380 void
381 GAS_handle_preference_change (void *cls, struct GNUNET_SERVER_Client *client,
382                               const struct GNUNET_MessageHeader *message)
383 {
384   const struct ChangePreferenceMessage *msg;
385   const struct PreferenceInformation *pi;
386   uint16_t msize;
387   uint32_t nump;
388   uint32_t i;
389
390   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n",
391               "PREFERENCE_CHANGE");
392   msize = ntohs (message->size);
393   if (msize < sizeof (struct ChangePreferenceMessage))
394   {
395     GNUNET_break (0);
396     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
397     return;
398   }
399   msg = (const struct ChangePreferenceMessage *) message;
400   nump = ntohl (msg->num_preferences);
401   if (msize !=
402       sizeof (struct ChangePreferenceMessage) +
403       nump * sizeof (struct PreferenceInformation))
404   {
405     GNUNET_break (0);
406     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
407     return;
408   }
409   GNUNET_STATISTICS_update (GSA_stats, "# preference change requests processed",
410                             1, GNUNET_NO);
411   pi = (const struct PreferenceInformation *) &msg[1];
412   for (i = 0; i < nump; i++)
413     GAS_addresses_change_preference (&msg->peer,
414                                      (enum GNUNET_ATS_PreferenceKind)
415                                      ntohl (pi[i].preference_kind),
416                                      pi[i].preference_value);
417   GNUNET_SERVER_receive_done (client, GNUNET_OK);
418 }
419
420
421 /**
422  * Initialize performance subsystem.
423  *
424  * @param server handle to our server
425  */
426 void
427 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
428 {
429   nc = GNUNET_SERVER_notification_context_create (server, 128);
430 }
431
432
433 /**
434  * Shutdown performance subsystem.
435  */
436 void
437 GAS_performance_done ()
438 {
439   GNUNET_SERVER_notification_context_destroy (nc);
440   nc = NULL;
441 }
442
443 /* end of gnunet-service-ats_performance.c */