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