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