determine network scope for ATS even if we do not yet have a session and only have...
[oweals/gnunet.git] / src / ats / gnunet-service-ats_performance.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011-2015 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file ats/gnunet-service-ats_performance.c
22  * @brief ats service, interaction with 'performance' API
23  * @author Matthias Wachs
24  * @author Christian Grothoff
25  *
26  * TODO:
27  * - simplify functions by passing a `struct GNUNET_HELLO_Address`
28  */
29 #include "platform.h"
30 #include "gnunet-service-ats.h"
31 #include "gnunet-service-ats_addresses.h"
32 #include "gnunet-service-ats_performance.h"
33 #include "ats.h"
34
35
36 /**
37  * Context for sending messages to performance clients without PIC.
38  */
39 static struct GNUNET_SERVER_NotificationContext *nc_no_pic;
40
41 /**
42  * Context for sending messages to performance clients with PIC.
43  */
44 static struct GNUNET_SERVER_NotificationContext *nc_pic;
45
46
47 /**
48  * Transmit the given performance information to all performance
49  * clients.
50  *
51  * @param pc client to send to, NULL for all
52  * @param peer peer for which this is an address suggestion
53  * @param plugin_name 0-termintated string specifying the transport plugin
54  * @param plugin_addr binary address for the plugin to use
55  * @param plugin_addr_len number of bytes in plugin_addr
56  * @param active #GNUNET_YES if this address is actively used
57  *        to maintain a connection to a peer;
58  *        #GNUNET_NO if the address is not actively used;
59  *        #GNUNET_SYSERR if this address is no longer available for ATS
60  * @param prop performance data for the address
61  * @param local_address_info information about the local flags for the address
62  * @param bandwidth_out assigned outbound bandwidth
63  * @param bandwidth_in assigned inbound bandwidth
64  */
65 static void
66 notify_client (struct GNUNET_SERVER_Client *client,
67                const struct GNUNET_PeerIdentity *peer,
68                const char *plugin_name,
69                const void *plugin_addr,
70                size_t plugin_addr_len,
71                int active,
72                const struct GNUNET_ATS_Properties *prop,
73                enum GNUNET_HELLO_AddressInfo local_address_info,
74                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
75                struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
76 {
77   struct PeerInformationMessage *msg;
78   size_t plugin_name_length = strlen (plugin_name) + 1;
79   size_t msize =
80     sizeof (struct PeerInformationMessage) +
81     plugin_addr_len +
82     plugin_name_length;
83   char buf[msize] GNUNET_ALIGN;
84   struct GNUNET_SERVER_NotificationContext **uc;
85   struct GNUNET_SERVER_NotificationContext *nc;
86   char *addrp;
87
88   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
89   GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
90   msg = (struct PeerInformationMessage *) buf;
91   msg->header.size = htons (msize);
92   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
93   msg->id = htonl (0);
94   msg->peer = *peer;
95   msg->address_length = htons (plugin_addr_len);
96   msg->address_active = ntohl ((uint32_t) active);
97   msg->plugin_name_length = htons (plugin_name_length);
98   msg->bandwidth_out = bandwidth_out;
99   msg->bandwidth_in = bandwidth_in;
100   if (NULL != prop)
101     GNUNET_ATS_properties_hton (&msg->properties,
102                                 prop);
103   else
104     memset (&msg->properties,
105             0,
106             sizeof (struct GNUNET_ATS_Properties));
107   msg->address_local_info = htonl (local_address_info);
108   addrp = (char *) &msg[1];
109   memcpy (addrp, plugin_addr, plugin_addr_len);
110   strcpy (&addrp[plugin_addr_len], plugin_name);
111   if (NULL == client)
112   {
113     GNUNET_SERVER_notification_context_broadcast (nc_pic,
114                                                   &msg->header,
115                                                   GNUNET_YES);
116   }
117   else
118   {
119     uc = GNUNET_SERVER_client_get_user_context (client,
120                                                  struct GNUNET_SERVER_NotificationContext *);
121     if (NULL == uc)
122     {
123       GNUNET_break (0);
124       return;
125     }
126     nc = *uc;
127     GNUNET_SERVER_notification_context_unicast (nc,
128                                                 client,
129                                                 &msg->header,
130                                                 GNUNET_YES);
131   }
132 }
133
134
135 /**
136  * Transmit the given performance information to all performance
137  * clients.
138  *
139  * @param peer peer for which this is an address suggestion
140  * @param plugin_name 0-termintated string specifying the transport plugin
141  * @param plugin_addr binary address for the plugin to use
142  * @param plugin_addr_len number of bytes in @a plugin_addr
143  * @param active #GNUNET_YES if this address is actively used
144  *        to maintain a connection to a peer;
145  *        #GNUNET_NO if the address is not actively used;
146  *        #GNUNET_SYSERR if this address is no longer available for ATS
147  * @param prop performance data for the address
148  * @param local_address_info information about the local flags for the address
149  * @param bandwidth_out assigned outbound bandwidth
150  * @param bandwidth_in assigned inbound bandwidth
151  */
152 void
153 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
154                                     const char *plugin_name,
155                                     const void *plugin_addr,
156                                     size_t plugin_addr_len,
157                                     int active,
158                                     const struct GNUNET_ATS_Properties *prop,
159                                     enum GNUNET_HELLO_AddressInfo local_address_info,
160                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
161                                     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
162 {
163   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
164   notify_client (NULL,
165                  peer,
166                  plugin_name,
167                  plugin_addr,
168                  plugin_addr_len,
169                  active,
170                  prop,
171                  local_address_info,
172                  bandwidth_out,
173                  bandwidth_in);
174   GNUNET_STATISTICS_update (GSA_stats,
175                             "# performance updates given to clients",
176                             1,
177                             GNUNET_NO);
178 }
179
180
181 /**
182  * Iterator for called from #GAS_addresses_get_peer_info()
183  *
184  * @param cls closure with the `struct GNUNET_SERVER_Client *` to inform.
185  * @param id the peer id
186  * @param plugin_name plugin name
187  * @param plugin_addr address
188  * @param plugin_addr_len length of @a plugin_addr
189  * @param active is address actively used
190  * @param prop performance information
191  * @param local_address_info information about the local flags for the address
192  * @param bandwidth_out current outbound bandwidth assigned to address
193  * @param bandwidth_in current inbound bandwidth assigned to address
194  */
195 static void
196 peerinfo_it (void *cls,
197              const struct GNUNET_PeerIdentity *id,
198              const char *plugin_name,
199              const void *plugin_addr,
200              size_t plugin_addr_len,
201              int active,
202              const struct GNUNET_ATS_Properties *prop,
203              enum GNUNET_HELLO_AddressInfo local_address_info,
204              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
205              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
206 {
207   struct GNUNET_SERVER_Client *client = cls;
208
209   if (NULL == id)
210     return;
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212               "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
213               GNUNET_i2s (id),
214               plugin_name,
215               (unsigned int) ntohl (bandwidth_out.value__),
216               (unsigned int) ntohl (bandwidth_in.value__));
217   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
218   notify_client (client,
219                  id,
220                  plugin_name,
221                  plugin_addr,
222                  plugin_addr_len,
223                  active,
224                  prop,
225                  local_address_info,
226                  bandwidth_out,
227                  bandwidth_in);
228 }
229
230
231 /**
232  * Register a new performance client.
233  *
234  * @param client handle of the new client
235  * @param flag flag specifying the type of the client
236  */
237 void
238 GAS_performance_add_client (struct GNUNET_SERVER_Client *client,
239                             enum StartFlag flag)
240 {
241   if (START_FLAG_PERFORMANCE_WITH_PIC == flag)
242   {
243     GNUNET_SERVER_notification_context_add (nc_pic,
244                                             client);
245     GNUNET_SERVER_client_set_user_context (client,
246                                            &nc_pic);
247     GAS_addresses_get_peer_info (NULL,
248                                  &peerinfo_it,
249                                  client);
250   }
251   else
252   {
253     GNUNET_SERVER_notification_context_add (nc_no_pic,
254                                             client);
255     GNUNET_SERVER_client_set_user_context (client,
256                                            &nc_no_pic);
257   }
258 }
259
260
261 /**
262  * Initialize performance subsystem.
263  *
264  * @param server handle to our server
265  */
266 void
267 GAS_performance_init (struct GNUNET_SERVER_Handle *server)
268 {
269   nc_no_pic = GNUNET_SERVER_notification_context_create (server, 32);
270   nc_pic = GNUNET_SERVER_notification_context_create (server, 32);
271 }
272
273
274 /**
275  * Shutdown performance subsystem.
276  */
277 void
278 GAS_performance_done ()
279 {
280   GNUNET_SERVER_notification_context_destroy (nc_no_pic);
281   nc_no_pic = NULL;
282   GNUNET_SERVER_notification_context_destroy (nc_pic);
283   nc_pic = NULL;
284 }
285
286 /* end of gnunet-service-ats_performance.c */