fixing resource leaks
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.h
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_addresses.h
23  * @brief ats service address management
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #ifndef GNUNET_SERVICE_ATS_ADDRESSES_H
28 #define GNUNET_SERVICE_ATS_ADDRESSES_H
29
30 #include "gnunet_util_lib.h"
31 #include "gnunet_ats_service.h"
32 #include "gnunet-service-ats.h"
33 #include "gnunet_statistics_service.h"
34 #include "ats.h"
35
36 /**
37  * NOTE: Do not change this documentation. This documentation is based on
38  * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
39  * use build_txt.sh to generate plaintext output
40  *
41  *   1 ATS addresses : ATS address management
42  *
43  *    This ATS addresses ("addresses") component manages the addresses known to
44  *    ATS service and suggests addresses to transport service when it is
45  *    interested in address suggestion for a peer. ATS addresses also
46  *    instantiates the bandwidth assignment mechanism (solver), notifies it
47  *    about changes to addresses and forwards changes to bandwidth assignments
48  *    to transport, depending if transport is interested in this change.
49  *
50  *     1.1 Input data
51  *
52  *       1.1.1 Addresses
53  *
54  *    Addresses are added by specifying peer ID, plugin, address, address length
55  *    and session, if available. ATS information can be specified if available.
56  *
57  *       1.1.2 Networks
58  *
59  *    ATS specifies a fix set of networks an address can belong to. For each
60  *    network an inbound and outbound quota will be specified. The available
61  *    networks and addtional helper varaibles are defined in
62  *    gnunet_ats_service.h. At the moment 5 networks are defined:
63  *      * GNUNET_ATS_NET_UNSPECIFIED
64  *      * GNUNET_ATS_NET_LOOPBACK
65  *      * GNUNET_ATS_NET_LAN
66  *      * GNUNET_ATS_NET_WAN
67  *      * GNUNET_ATS_NET_WLAN
68  *
69  *    The total number of networks defined is stored in
70  *    GNUNET_ATS_NetworkTypeCount GNUNET_ATS_NetworkType can be used array
71  *    initializer for an int array, while GNUNET_ATS_NetworkType is an
72  *    initializer for a char array containing a string description of all
73  *    networks
74  *
75  *       1.1.3 Quotas
76  *
77  *    An inbound and outbound quota for each of the networks mentioned in 1.1.2
78  *    is loaded from ats configuration during initialization. This quota defines
79  *    to total amount of inbound and outbound traffic allowed for a specific
80  *    network. The configuration values used are in section ats:
81  *      * "NETWORK"_QUOTA_IN = <value>
82  *      * "NETWORK"_QUOTA_IN = <value>
83  *
84  *    You can specify quotas by setting the <value> to a:
85  *      * unrestricted: unlimited
86  *      * number of bytes: e.g. 10240
87  *      * fancy value: e.g. 64 Kib
88  *
89  *    unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
90  *    the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
91  *    are:
92  *      * GNUNET_ATS_DefaultBandwidth: 65536
93  *      * GNUNET_ATS_MaxBandwidth: UINT32_MAX
94  *      * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
95  *
96  *    Details of loading quotas and default values will be described on
97  *
98  *       1.1.4 Preference values
99  *
100  *     1.2 Data structures used
101  *
102  *    Addresse uses struct ATS_Address for each address. The structs are stored
103  *    in a linked list and provides a pointer void *solver_information for the
104  *    solver to store address specific information. It provides the int values
105  *    active which is set to GNUNET_YES if the address is select for transport
106  *    use and used, representing that transport service is actively using this
107  *    address. Address information are stored in peer, addr, addr_len, plugin.
108  *
109  *     1.3 Initialization
110  *
111  *    During initialization a hashmap to store addresses is created. The quotas
112  *    for all networks defined for ATS are loaded from configuration. For each
113  *    network first the logic will check if the string
114  *    GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
115  *    the configured value as a fancy size and if this fails it will try to use
116  *    it as a value_number. If no configuration value is found it will assign
117  *    GNUNET_ATS_DefaultBandwidth. The most important step is to load the
118  *    configured solver using configuration "[ats]:MODE". Current solvers are
119  *    MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
120  *
121  *     1.4 Solver API
122  *
123  *    Solver functions:
124  *      * s_init: init the solver with required information
125  *      * s_add: add a new address
126  *      * s_update: update ATS values or session for an address
127  *      * s_get: get prefered address for a peer
128  *      * s_del: delete an address
129  *      * s_pref: change preference value for a peer
130  *      * s_done: shutdown solver
131  *
132  *    Callbacks: addresses provides a bandwidth_changed_cb callback to the
133  *    solver which is called when bandwidth assigned to peer has changed
134  *
135  *     1.5 Shutdown
136  *
137  *    During shutdown all addresses are freed and the solver told to shutdown
138  *
139  *     1.6 Addresses and sessions
140  *
141  *    Addresses consist of the address itself and a numerical session. When a
142  *    new address without a session is added it has no session, so it gets
143  *    session 0 assigned. When an address with a session is added and an address
144  *    object with session 0 is found, this object is updated with the session
145  *    otherwise a new address object with this session assigned is created.
146  *
147  *       1.6.1 Terminology
148  *
149  *    Addresses a1,a2 with session s1, s2 are "exact" if:
150  *    (a1 == a2)&&(s1 == s2)
151  *    Addresses a1,a2 with session s1, s2 are "equivalent" if:
152  *    (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
153  *
154  *     1.7 Address management
155  *
156  *    Transport service notifies ATS about changes to the addresses known to
157  *    him.
158  *
159  *       1.7.1 Adding an address
160  *
161  *    When transport learns a new address it tells ATS and ATS is telling
162  *    addresses about it using GAS_address_add. If not known to addresses it
163  *    creates a new address object and calls solver's s_add. ATS information are
164  *    deserialized and solver is notified about the session and ATS information
165  *    using s_update.
166  *
167  *       1.7.2 Updating an address
168  *
169  *    Addresses does an lookup up for the existing address with the given
170  *    session. If disassembles included ATS information and notifies the solver
171  *    using s_update about the update.
172  *
173  *       1.7.3 Deleting an address
174  *
175  *    Addresses does an lookup for the exact address and session and if removes
176  *    this address. If session != 0 the session is set to 0 and the address is
177  *    kept. If session == 0, the addresses is removed.
178  *
179  *       1.7.4 Requesting an address suggestion
180  *
181  *    The address client issues a request address message to be notified about
182  *    address suggestions for a specific peer. Addresses asks the solver with
183  *    s_get. If no address is available, it will not send a response, otherwise
184  *    it will respond with the choosen address.
185  *
186  *       1.7.5 Address suggestions
187  *
188  *    Addresses will notify the client automatically on any bandwidth_changed_cb
189  *    by the solver if a address suggestion request is pending. If no address is
190  *    available it will not respond at all If the client is not interested
191  *    anymore, it has to cancel the address suggestion request.
192  *
193  *       1.7.6 Suggestions blocks and reset
194  *
195  *    After suggesting an address it is blocked for ATS_BLOCKING_DELTA sec. to
196  *    prevent the client from being thrashed. If the client requires immediately
197  *    it can reset this block using GAS_addresses_handle_backoff_reset.
198  *
199  *       1.7.7 Marking address in use
200  *
201  *    The client can notify addresses that it successfully uses an address and
202  *    wants this address to be kept by calling GSA_address_in_use. Adresses will
203  *    mark the address as used an notify the solver about the use.
204  *
205  *       1.7.8 Address lifecycle
206  *
207  *      * (add address)
208  *      * (updated address) || (address in use)
209  *      * (delete address)
210  *
211  *     1.8 Bandwidth assignment
212  *
213  *    The addresses are used to perform resource allocation operations. ATS
214  *    addresses takes care of instantiating the solver configured and notifies
215  *    the respective solver about address changes and receives changes to the
216  *    bandwidth assignment from the solver. The current bandwidth assignment is
217  *    sent to transport. The specific solvers will be described in the specific
218  *    section.
219  *
220  *     1.9 Changing peer preferences
221  *
222  *    The bandwidth assigned to a peer can be influenced by setting a preference
223  *    for a peer. The prefernce will be given to to the solver with s_pref which
224  *    has to take care of the preference value
225
226  */
227
228 /**
229  * Available ressource assignment modes
230  */
231 enum ATS_Mode
232 {
233   /*
234    * proportional mode:
235    *
236    * Assign each peer an equal amount of bandwidth (bw)
237    *
238    * bw_per_peer = bw_total / #active addresses
239    */
240   MODE_PROPORTIONAL,
241
242   /*
243    * MLP mode:
244    *
245    * Solve ressource assignment as an optimization problem
246    * Uses an mixed integer programming solver
247    */
248   MODE_MLP,
249
250   /*
251    * Reinforcement Learning mode:
252    *
253    * Solve resource assignment using a learning agent
254    */
255   MODE_RIL
256 };
257
258
259 /*
260  * How long will address suggestions blocked after a suggestion
261  */
262 #define ATS_BLOCKING_DELTA GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
263
264 /**
265  * Information provided by ATS normalization
266  */
267 struct GAS_NormalizationInfo
268 {
269   /**
270    * Next index to use in averaging queue
271    */
272   unsigned int avg_queue_index;
273
274   /**
275    * Averaging queue
276    */
277   uint32_t atsi_abs[GAS_normalization_queue_length];
278
279   /**
280    * Averaged ATSI values from queue
281    */
282   uint32_t avg;
283
284   /**
285    * Normalized values from queue to a range of values [1.0...2.0]
286    */
287   double norm;
288 };
289
290 /**
291  * Address with additional information
292  */
293 struct ATS_Address
294 {
295   /**
296    * Next element in DLL
297    */
298   struct ATS_Address *next;
299
300   /**
301    * Previous element in DLL
302    */
303   struct ATS_Address *prev;
304
305   /**
306    * Peer ID
307    */
308   struct GNUNET_PeerIdentity peer;
309
310   /**
311    * Session ID, 0 if no session is given
312    */
313   uint32_t session_id;
314
315   uint32_t local_address_info;
316
317   /**
318    * Address
319    */
320   const void *addr;
321
322   /**
323    * Address length
324    */
325   size_t addr_len;
326
327   /**
328    * Plugin name
329    */
330   char *plugin;
331
332   /**
333    * Solver specific information for this address
334    */
335   void *solver_information;
336
337   /**
338    * ATS performance information for this address
339    */
340   struct GNUNET_ATS_Information *atsi;
341
342   /**
343    * ATS performance information for this address
344    */
345   uint32_t atsi_count;
346
347   /**
348    * Inbound bandwidth assigned by solver in NBO
349    */
350   struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_in;
351
352   /**
353    * Outbound bandwidth assigned by solver in NBO
354    */
355   struct GNUNET_BANDWIDTH_Value32NBO assigned_bw_out;
356
357   /**
358    * Blocking interval
359    */
360   struct GNUNET_TIME_Relative block_interval;
361
362   /**
363    * Time when address can be suggested again
364    */
365   struct GNUNET_TIME_Absolute blocked_until;
366
367   /**
368    * Time when address had last activity (update, in uses)
369    */
370   struct GNUNET_TIME_Absolute t_last_activity;
371
372   /**
373    * Time when address was added
374    */
375   struct GNUNET_TIME_Absolute t_added;
376
377   /**
378    * Is this the active address for this peer?
379    */
380   int active;
381
382   /**
383    * Is this the address for this peer in use?
384    */
385   int used;
386
387   /**
388    * Normalized ATS performance information for this address
389    * Each entry can be accessed using the GNUNET_ATS_QualityProperties avg_queue_index
390    */
391   struct GAS_NormalizationInfo atsin[GNUNET_ATS_QualityPropertiesCount];
392 };
393
394
395 /**
396  * Handle for ATS address component
397  */
398 struct GAS_Addresses_Handle;
399
400 /**
401  * Initialize address subsystem. The addresses subsystem manages the addresses
402  * known and current performance information. It has a solver component
403  * responsible for the resource allocation. It tells the solver about changes
404  * and receives updates when the solver changes the ressource allocation.
405  *
406  * @param cfg configuration to use
407  * @param stats the statistics handle to use
408  * @return an address handle
409  */
410 struct GAS_Addresses_Handle *
411 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
412     const struct GNUNET_STATISTICS_Handle *stats);
413
414 /**
415  * Shutdown address subsystem.
416  *
417  * @param handle the address handle to shutdown
418  */
419 void
420 GAS_addresses_done (struct GAS_Addresses_Handle *handle);
421
422 /**
423  * Add a new address for a peer.
424  *
425  * @param handle the address handle to use
426  * @param peer peer
427  * @param plugin_name transport plugin name
428  * @param plugin_addr plugin address
429  * @param plugin_addr_len length of the plugin address
430  * @param local_address_info the local address for the address
431  * @param session_id session id, can be 0
432  * @param atsi performance information for this address
433  * @param atsi_count number of performance information contained
434  */
435 void
436 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
437     const struct GNUNET_PeerIdentity *peer,
438     const char *plugin_name,
439     const void *plugin_addr,
440     size_t plugin_addr_len,
441     uint32_t local_address_info,
442     uint32_t session_id,
443     const struct GNUNET_ATS_Information *atsi,
444     uint32_t atsi_count);
445
446 /**
447  * Notification about active use of an address.
448  * in_use == GNUNET_YES:
449  *      This address is used to maintain an active connection with a peer.
450  * in_use == GNUNET_NO:
451  *      This address is no longer used to maintain an active connection with a peer.
452  *
453  * Note: can only be called with in_use == GNUNET_NO if called with GNUNET_YES
454  * before
455  *
456  * @param handle the address handle to use
457  * @param peer peer
458  * @param plugin_name transport plugin name
459  * @param plugin_addr plugin address
460  * @param plugin_addr_len length of the plugin address
461  * @param local_address_info the local address for the address
462  * @param session_id session id, can be 0
463  * @param in_use GNUNET_YES if GNUNET_NO
464  * @return GNUNET_SYSERR on failure (address unknown ...)
465  */
466 int
467 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
468     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
469     const void *plugin_addr, size_t plugin_addr_len,
470     uint32_t local_address_info,
471     uint32_t session_id,
472     int in_use);
473
474 /**
475  * Update an address with a session or performance information for a peer.
476  *
477  * If an address was added without a session it will be updated with the
478  * session
479  *
480  * @param handle the address handle to use
481  * @param peer peer
482  * @param plugin_name transport plugin name
483  * @param plugin_addr plugin address
484  * @param plugin_addr_len length of the plugin address
485  * @param local_address_info the local address for the address
486  * @param session_id session id, can be 0
487  * @param atsi performance information for this address
488  * @param atsi_count number of performance information contained
489  */
490 void
491 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
492     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
493     const void *plugin_addr, size_t plugin_addr_len,
494     uint32_t local_address_info, uint32_t session_id,
495     const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count);
496
497 /**
498  * Remove an address or just a session for a peer.
499  *
500  * @param handle the address handle to use
501  * @param peer peer
502  * @param plugin_name transport plugin name
503  * @param plugin_addr plugin address
504  * @param plugin_addr_len length of the plugin address
505  * @param session_id session id, can be 0
506  * @param local_address_info the local address for the address
507  */
508 void
509 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
510     const struct GNUNET_PeerIdentity *peer, const char *plugin_name,
511     const void *plugin_addr, size_t plugin_addr_len,
512     uint32_t local_address_info, uint32_t session_id);
513
514 /**
515  * Remove all addresses
516  *
517  * @param handle the address handle to use
518  */
519 void
520 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle);
521
522 /**
523  * Request address suggestions for a peer
524  *
525  * @param handle the address handle
526  * @param peer the peer id
527  */
528 void
529 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
530     const struct GNUNET_PeerIdentity *peer);
531
532 /**
533  * Cancel address suggestions for a peer
534  *
535  * @param handle the address handle
536  * @param peer the peer id
537  */
538 void
539 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
540     const struct GNUNET_PeerIdentity *peer);
541
542 /**
543  * Reset suggestion backoff for a peer
544  *
545  * Suggesting addresses is blocked for ATS_BLOCKING_DELTA. Blocking can be
546  * reset using this function
547  *
548  * @param handle the address handle
549  * @param peer the peer id
550  */
551 void
552 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
553     const struct GNUNET_PeerIdentity *peer);
554
555
556 /**
557  * A performance client disconnected
558  *
559  * @param handle address handle
560  * @param client the client
561  */
562
563 void
564 GAS_addresses_preference_client_disconnect (struct GAS_Addresses_Handle *handle,
565     void *client);
566
567 /**
568  * Change the preference for a peer
569  *
570  * @param handle the address handle
571  * @param client the client sending this request
572  * @param peer the peer id
573  * @param kind the preference kind to change
574  * @param score_abs the new preference score
575  */
576 void
577 GAS_addresses_preference_change (struct GAS_Addresses_Handle *handle,
578     void *client, const struct GNUNET_PeerIdentity *peer,
579     enum GNUNET_ATS_PreferenceKind kind, float score_abs);
580
581 /**
582  * Application feedback on how good preference requirements are fulfilled
583  * for a specific preference in the given time scope [now - scope .. now]
584  *
585  * An application notifies ATS if (and only if) it has feedback information
586  * for a specific property. This value is valid until the feedback score is
587  * updated by the application.
588  *
589  * If the application has no feedback for this preference kind the application
590  * will not explicitly call.
591  *
592  * @param handle the address handle
593  * @param application the application sending this request
594  * @param peer the peer id
595  * @param scope the time interval this valid for: [now - scope .. now]
596  * @param kind the preference kind this feedback is intended for
597  * @param score_abs the new preference score
598  */
599 void
600 GAS_addresses_preference_feedback (struct GAS_Addresses_Handle *handle,
601     void *application, const struct GNUNET_PeerIdentity *peer,
602     const struct GNUNET_TIME_Relative scope,
603     enum GNUNET_ATS_PreferenceKind kind, float score_abs);
604
605 /**
606  * Iterator for GAS_addresses_iterate_peers
607  *
608  * @param p_it_cls closure
609  * @param id the peer id
610  */
611 typedef void
612 (*GNUNET_ATS_Peer_Iterator) (void *p_it_cls,
613     const struct GNUNET_PeerIdentity *id);
614
615 /**
616  * Return all peers currently known to ATS
617  *
618  * @param handle the address handle to use
619  * @param p_it the iterator to call for every peer
620  * @param p_it_cls the closure for the iterator
621  */
622 void
623 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle,
624     GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls);
625
626 /**
627  * Iterator for GAS_addresses_get_peer_info
628  *
629  * @param p_it_cls closure closure
630  * @param id the peer id
631  * @param plugin_name plugin name
632  * @param plugin_addr address
633  * @param plugin_addr_len address length
634  * @param address_active is address actively used
635  * @param atsi ats performance information
636  * @param atsi_count number of ats performance elements
637  * @param bandwidth_out current outbound bandwidth assigned to address
638  * @param bandwidth_in current inbound bandwidth assigned to address
639  */
640 typedef void
641 (*GNUNET_ATS_PeerInfo_Iterator) (void *p_it_cls,
642     const struct GNUNET_PeerIdentity *id, const char *plugin_name,
643     const void *plugin_addr, size_t plugin_addr_len, const int address_active,
644     const struct GNUNET_ATS_Information *atsi, uint32_t atsi_count,
645     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
646     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in);
647
648 /**
649  * Return information all peers currently known to ATS
650  *
651  * @param handle the address handle to use
652  * @param peer the respective peer
653  * @param pi_it the iterator to call for every peer
654  * @param pi_it_cls the closure for the iterator
655  */
656 void
657 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
658     const struct GNUNET_PeerIdentity *peer, GNUNET_ATS_PeerInfo_Iterator pi_it,
659     void *pi_it_cls);
660
661 #endif
662
663 /* end of gnunet-service-ats_addresses.h */