add changelog
[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      SPDX-License-Identifier: AGPL3.0-or-later
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_NotificationContext *nc_no_pic;
40
41 /**
42  * Context for sending messages to performance clients with PIC.
43  */
44 static struct GNUNET_NotificationContext *nc_pic;
45
46
47 /**
48  * Transmit the given performance information to all performance
49  * clients.
50  *
51  * @param client 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_SERVICE_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   char *addrp;
85
86   if (NULL != prop)
87     GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
88   GNUNET_assert (msize < GNUNET_MAX_MESSAGE_SIZE);
89   msg = (struct PeerInformationMessage *) buf;
90   msg->header.size = htons (msize);
91   msg->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_PEER_INFORMATION);
92   msg->id = htonl (0);
93   msg->peer = *peer;
94   msg->address_length = htons (plugin_addr_len);
95   msg->address_active = ntohl ((uint32_t) active);
96   msg->plugin_name_length = htons (plugin_name_length);
97   msg->bandwidth_out = bandwidth_out;
98   msg->bandwidth_in = bandwidth_in;
99   if (NULL != prop)
100     GNUNET_ATS_properties_hton (&msg->properties,
101                                 prop);
102   else
103     memset (&msg->properties,
104             0,
105             sizeof(struct GNUNET_ATS_Properties));
106   msg->address_local_info = htonl (local_address_info);
107   addrp = (char *) &msg[1];
108   GNUNET_memcpy (addrp, plugin_addr, plugin_addr_len);
109   strcpy (&addrp[plugin_addr_len], plugin_name);
110   if (NULL == client)
111   {
112     GNUNET_notification_context_broadcast (nc_pic,
113                                            &msg->header,
114                                            GNUNET_YES);
115   }
116   else
117   {
118     struct GNUNET_MQ_Envelope *env;
119
120     env = GNUNET_MQ_msg_copy (&msg->header);
121     GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client),
122                     env);
123   }
124 }
125
126
127 /**
128  * Transmit the given performance information to all performance
129  * clients.
130  *
131  * @param peer peer for which this is an address suggestion
132  * @param plugin_name 0-termintated string specifying the transport plugin
133  * @param plugin_addr binary address for the plugin to use
134  * @param plugin_addr_len number of bytes in @a plugin_addr
135  * @param active #GNUNET_YES if this address is actively used
136  *        to maintain a connection to a peer;
137  *        #GNUNET_NO if the address is not actively used;
138  *        #GNUNET_SYSERR if this address is no longer available for ATS
139  * @param prop performance data for the address
140  * @param local_address_info information about the local flags for the address
141  * @param bandwidth_out assigned outbound bandwidth
142  * @param bandwidth_in assigned inbound bandwidth
143  */
144 void
145 GAS_performance_notify_all_clients (const struct GNUNET_PeerIdentity *peer,
146                                     const char *plugin_name,
147                                     const void *plugin_addr,
148                                     size_t plugin_addr_len,
149                                     int active,
150                                     const struct GNUNET_ATS_Properties *prop,
151                                     enum GNUNET_HELLO_AddressInfo
152                                     local_address_info,
153                                     struct GNUNET_BANDWIDTH_Value32NBO
154                                     bandwidth_out,
155                                     struct GNUNET_BANDWIDTH_Value32NBO
156                                     bandwidth_in)
157 {
158   GNUNET_break ((NULL == prop) ||
159                 (GNUNET_NT_UNSPECIFIED != prop->scope));
160   notify_client (NULL,
161                  peer,
162                  plugin_name,
163                  plugin_addr,
164                  plugin_addr_len,
165                  active,
166                  prop,
167                  local_address_info,
168                  bandwidth_out,
169                  bandwidth_in);
170   GNUNET_STATISTICS_update (GSA_stats,
171                             "# performance updates given to clients",
172                             1,
173                             GNUNET_NO);
174 }
175
176
177 /**
178  * Iterator for called from #GAS_addresses_get_peer_info()
179  *
180  * @param cls closure with the `struct GNUNET_SERVICE_Client *` to inform.
181  * @param id the peer id
182  * @param plugin_name plugin name
183  * @param plugin_addr address
184  * @param plugin_addr_len length of @a plugin_addr
185  * @param active is address actively used
186  * @param prop performance information
187  * @param local_address_info information about the local flags for the address
188  * @param bandwidth_out current outbound bandwidth assigned to address
189  * @param bandwidth_in current inbound bandwidth assigned to address
190  */
191 static void
192 peerinfo_it (void *cls,
193              const struct GNUNET_PeerIdentity *id,
194              const char *plugin_name,
195              const void *plugin_addr,
196              size_t plugin_addr_len,
197              int active,
198              const struct GNUNET_ATS_Properties *prop,
199              enum GNUNET_HELLO_AddressInfo local_address_info,
200              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
201              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
202 {
203   struct GNUNET_SERVICE_Client *client = cls;
204
205   if (NULL == id)
206     return;
207   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208               "Callback for peer `%s' plugin `%s' BW out %u, BW in %u \n",
209               GNUNET_i2s (id),
210               plugin_name,
211               (unsigned int) ntohl (bandwidth_out.value__),
212               (unsigned int) ntohl (bandwidth_in.value__));
213   GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
214   notify_client (client,
215                  id,
216                  plugin_name,
217                  plugin_addr,
218                  plugin_addr_len,
219                  active,
220                  prop,
221                  local_address_info,
222                  bandwidth_out,
223                  bandwidth_in);
224 }
225
226
227 /**
228  * Register a new performance client.
229  *
230  * @param client handle of the new client
231  * @param flag flag specifying the type of the client
232  */
233 void
234 GAS_performance_add_client (struct GNUNET_SERVICE_Client *client,
235                             enum StartFlag flag)
236 {
237   struct GNUNET_MQ_Handle *mq;
238
239   mq = GNUNET_SERVICE_client_get_mq (client);
240   if (START_FLAG_PERFORMANCE_WITH_PIC == flag)
241   {
242     GNUNET_notification_context_add (nc_pic,
243                                      mq);
244     GAS_addresses_get_peer_info (NULL,
245                                  &peerinfo_it,
246                                  client);
247   }
248   else
249   {
250     GNUNET_notification_context_add (nc_no_pic,
251                                      mq);
252   }
253 }
254
255
256 /**
257  * Initialize performance subsystem.
258  *
259  * @param server handle to our server
260  */
261 void
262 GAS_performance_init ()
263 {
264   nc_no_pic = GNUNET_notification_context_create (32);
265   nc_pic = GNUNET_notification_context_create (32);
266 }
267
268
269 /**
270  * Shutdown performance subsystem.
271  */
272 void
273 GAS_performance_done ()
274 {
275   GNUNET_notification_context_destroy (nc_no_pic);
276   nc_no_pic = NULL;
277   GNUNET_notification_context_destroy (nc_pic);
278   nc_pic = NULL;
279 }
280
281
282 /* end of gnunet-service-ats_performance.c */