gnunet-service-ats_addresses.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011-2014 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file ats/gnunet-service-ats_addresses.h
  18. * @brief ats service address management
  19. * @author Matthias Wachs
  20. * @author Christian Grothoff
  21. */
  22. #ifndef GNUNET_SERVICE_ATS_ADDRESSES_H
  23. #define GNUNET_SERVICE_ATS_ADDRESSES_H
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_ats_service.h"
  26. #include "gnunet-service-ats.h"
  27. #include "ats.h"
  28. /**
  29. * NOTE: Do not change this documentation. This documentation is based on
  30. * gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
  31. * use build_txt.sh to generate plaintext output
  32. *
  33. * 1 ATS addresses : ATS address management
  34. *
  35. * This ATS addresses ("addresses") component manages the addresses known to
  36. * ATS service and suggests addresses to transport service when it is
  37. * interested in address suggestion for a peer. ATS addresses also
  38. * instantiates the bandwidth assignment mechanism (solver), notifies it
  39. * about changes to addresses and forwards changes to bandwidth assignments
  40. * to transport, depending if transport is interested in this change.
  41. *
  42. * 1.1 Input data
  43. *
  44. * 1.1.1 Addresses
  45. *
  46. * Addresses are added by specifying peer ID, plugin, address, address length
  47. * and session, if available. ATS information can be specified if available.
  48. *
  49. * 1.1.2 Networks
  50. *
  51. * ATS specifies a fix set of networks an address can belong to. For each
  52. * network an inbound and outbound quota will be specified. The available
  53. * networks and addtional helper varaibles are defined in
  54. * gnunet_ats_service.h. At the moment 5 networks are defined:
  55. * * GNUNET_NT_UNSPECIFIED
  56. * * GNUNET_NT_LOOPBACK
  57. * * GNUNET_NT_LAN
  58. * * GNUNET_NT_WAN
  59. * * GNUNET_NT_WLAN
  60. *
  61. * The total number of networks defined is stored in
  62. * GNUNET_NT_COUNT GNUNET_ATS_NetworkType can be used array
  63. * initializer for an int array, while GNUNET_ATS_NetworkType is an
  64. * initializer for a char array containing a string description of all
  65. * networks
  66. *
  67. * 1.1.3 Quotas
  68. *
  69. * An inbound and outbound quota for each of the networks mentioned in 1.1.2
  70. * is loaded from ats configuration during initialization. This quota defines
  71. * to total amount of inbound and outbound traffic allowed for a specific
  72. * network. The configuration values used are in section ats:
  73. * * "NETWORK"_QUOTA_IN = <value>
  74. * * "NETWORK"_QUOTA_IN = <value>
  75. *
  76. * You can specify quotas by setting the <value> to a:
  77. * * unrestricted: unlimited
  78. * * number of bytes: e.g. 10240
  79. * * fancy value: e.g. 64 Kib
  80. *
  81. * unlimited is defined as GNUNET_ATS_MaxBandwidthString and equivalent to
  82. * the value GNUNET_ATS_MaxBandwidth Important predefined values for quotas
  83. * are:
  84. * * GNUNET_ATS_DefaultBandwidth: 65536
  85. * * GNUNET_ATS_MaxBandwidth: UINT32_MAX
  86. * * GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT: 1024
  87. *
  88. * Details of loading quotas and default values will be described on
  89. *
  90. * 1.1.4 Preference values
  91. *
  92. * 1.2 Data structures used
  93. *
  94. * Addresse uses struct ATS_Address for each address. The structs are stored
  95. * in a linked list and provides a pointer void *solver_information for the
  96. * solver to store address specific information. It provides the int values
  97. * active which is set to GNUNET_YES if the address is select for transport
  98. * use and used, representing that transport service is actively using this
  99. * address. Address information are stored in peer, addr, addr_len, plugin.
  100. *
  101. * 1.3 Initialization
  102. *
  103. * During initialization a hashmap to store addresses is created. The quotas
  104. * for all networks defined for ATS are loaded from configuration. For each
  105. * network first the logic will check if the string
  106. * GNUNET_ATS_MaxBandwidthString is configured, if not it will try to convert
  107. * the configured value as a fancy size and if this fails it will try to use
  108. * it as a value_number. If no configuration value is found it will assign
  109. * GNUNET_ATS_DefaultBandwidth. The most important step is to load the
  110. * configured solver using configuration "[ats]:MODE". Current solvers are
  111. * MODE_PROPORTIONAL, MODE_MLP. Interaction is done using a solver API
  112. *
  113. * 1.4 Solver API
  114. *
  115. * Solver functions:
  116. * * s_init: init the solver with required information
  117. * * s_add: add a new address
  118. * * s_update: update ATS values or session for an address
  119. * * s_get: get prefered address for a peer
  120. * * s_del: delete an address
  121. * * s_pref: change preference value for a peer
  122. * * s_done: shutdown solver
  123. *
  124. * Callbacks: addresses provides a bandwidth_changed_cb callback to the
  125. * solver which is called when bandwidth assigned to peer has changed
  126. *
  127. * 1.5 Shutdown
  128. *
  129. * During shutdown all addresses are freed and the solver told to shutdown
  130. *
  131. * 1.6 Addresses and sessions
  132. *
  133. * Addresses consist of the address itself and a numerical session. When a
  134. * new address without a session is added it has no session, so it gets
  135. * session 0 assigned. When an address with a session is added and an address
  136. * object with session 0 is found, this object is updated with the session
  137. * otherwise a new address object with this session assigned is created.
  138. *
  139. * 1.6.1 Terminology
  140. *
  141. * Addresses a1,a2 with session s1, s2 are "exact" if:
  142. * (a1 == a2)&&(s1 == s2)
  143. * Addresses a1,a2 with session s1, s2 are "equivalent" if:
  144. * (a1 == a2)&&((s1 == s2)||(s1 == 0)||(s2 == 0)
  145. *
  146. * 1.7 Address management
  147. *
  148. * Transport service notifies ATS about changes to the addresses known to
  149. * it.
  150. *
  151. * 1.7.1 Adding an address
  152. *
  153. * When transport learns a new address it tells ATS and ATS is telling
  154. * addresses about it using GAS_address_add. If not known to addresses it
  155. * creates a new address object and calls solver's s_add. ATS information are
  156. * deserialized and solver is notified about the session and ATS information
  157. * using s_update.
  158. *
  159. * 1.7.2 Updating an address
  160. *
  161. * Addresses does an lookup up for the existing address with the given
  162. * session. If disassembles included ATS information and notifies the solver
  163. * using s_update about the update.
  164. *
  165. * 1.7.3 Deleting an address
  166. *
  167. * Addresses does an lookup for the exact address and session and if removes
  168. * this address. If session != 0 the session is set to 0 and the address is
  169. * kept. If session == 0, the addresses is removed.
  170. *
  171. * 1.7.4 Requesting an address suggestion
  172. *
  173. * The address client issues a request address message to be notified about
  174. * address suggestions for a specific peer. Addresses asks the solver with
  175. * s_get. If no address is available, it will not send a response, otherwise
  176. * it will respond with the choosen address.
  177. *
  178. * 1.7.5 Address suggestions
  179. *
  180. * Addresses will notify the client automatically on any bandwidth_changed_cb
  181. * by the solver if a address suggestion request is pending. If no address is
  182. * available it will not respond at all If the client is not interested
  183. * anymore, it has to cancel the address suggestion request.
  184. *
  185. * 1.7.6 Suggestions blocks and reset
  186. *
  187. * After suggesting an address it is blocked for ATS_BLOCKING_DELTA sec. to
  188. * prevent the client from being thrashed. If the client requires immediately
  189. * it can reset this block using GAS_addresses_handle_backoff_reset.
  190. *
  191. * 1.7.7 Address lifecycle
  192. *
  193. * * (add address)
  194. * * (updated address)
  195. * * (delete address)
  196. *
  197. * 1.8 Bandwidth assignment
  198. *
  199. * The addresses are used to perform resource allocation operations. ATS
  200. * addresses takes care of instantiating the solver configured and notifies
  201. * the respective solver about address changes and receives changes to the
  202. * bandwidth assignment from the solver. The current bandwidth assignment is
  203. * sent to transport. The specific solvers will be described in the specific
  204. * section.
  205. *
  206. * 1.9 Changing peer preferences
  207. *
  208. * The bandwidth assigned to a peer can be influenced by setting a preference
  209. * for a peer. The prefernce will be given to to the solver with s_pref which
  210. * has to take care of the preference value
  211. */
  212. /*
  213. * How long will address suggestions blocked after a suggestion
  214. */
  215. #define ATS_BLOCKING_DELTA GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
  216. /**
  217. * Information provided by ATS normalization
  218. */
  219. struct GAS_NormalizationInfo {
  220. /**
  221. * Next index to use in averaging queue
  222. */
  223. unsigned int avg_queue_index;
  224. /**
  225. * Averaging queue
  226. */
  227. uint64_t atsi_abs[GAS_normalization_queue_length];
  228. /**
  229. * Averaged ATSI values from queue
  230. */
  231. uint64_t avg;
  232. /**
  233. * Normalized values from queue to a range of values [1.0...2.0]
  234. */
  235. double norm;
  236. };
  237. /**
  238. * Address with additional information
  239. */
  240. struct ATS_Address {
  241. /**
  242. * Peer ID this address is for.
  243. */
  244. struct GNUNET_PeerIdentity peer;
  245. /**
  246. * Address (in plugin-specific binary format).
  247. */
  248. const void *addr;
  249. /**
  250. * Plugin name
  251. */
  252. char *plugin;
  253. /**
  254. * Solver-specific information for this address
  255. */
  256. void *solver_information;
  257. /**
  258. * ATS performance information for this address
  259. */
  260. struct GNUNET_ATS_Properties properties;
  261. /**
  262. * Time when address had last activity (update, in uses)
  263. */
  264. struct GNUNET_TIME_Absolute t_last_activity;
  265. /**
  266. * Time when address was added
  267. */
  268. struct GNUNET_TIME_Absolute t_added;
  269. /**
  270. * Address length, number of bytes in @e addr.
  271. */
  272. size_t addr_len;
  273. /**
  274. * Session ID, can never be 0.
  275. */
  276. uint32_t session_id;
  277. /**
  278. * Field to store local flags.
  279. */
  280. enum GNUNET_HELLO_AddressInfo local_address_info;
  281. /**
  282. * ATS performance information for this address, size of the @e atsi array.
  283. */
  284. uint32_t atsi_count;
  285. /**
  286. * Inbound bandwidth assigned by solver
  287. */
  288. uint32_t assigned_bw_in;
  289. /**
  290. * Outbound bandwidth assigned by solver
  291. */
  292. uint32_t assigned_bw_out;
  293. /**
  294. * Inbound bandwidth assigned by solver in NBO
  295. */
  296. uint32_t last_notified_bw_in;
  297. /**
  298. * Outbound bandwidth assigned by solver in NBO
  299. */
  300. uint32_t last_notified_bw_out;
  301. /**
  302. * Is this the active address for this peer?
  303. */
  304. int active;
  305. /**
  306. * Normalized delay information for this address.
  307. */
  308. struct GAS_NormalizationInfo norm_delay;
  309. /**
  310. * Normalized distance information for this address.
  311. */
  312. struct GAS_NormalizationInfo norm_distance;
  313. /**
  314. * Normalized utilization inbound for this address.
  315. */
  316. struct GAS_NormalizationInfo norm_utilization_in;
  317. /**
  318. * Normalized utilization outbound for this address.
  319. */
  320. struct GAS_NormalizationInfo norm_utilization_out;
  321. };
  322. /**
  323. * A multipeermap mapping peer identities to `struct ATS_Address`.
  324. */
  325. extern struct GNUNET_CONTAINER_MultiPeerMap *GSA_addresses;
  326. /**
  327. * Initialize address subsystem. The addresses subsystem manages the addresses
  328. * known and current performance information.
  329. */
  330. void
  331. GAS_addresses_init(void);
  332. /**
  333. * Shutdown address subsystem.
  334. */
  335. void
  336. GAS_addresses_done(void);
  337. /**
  338. * Add a new address for a peer.
  339. *
  340. * @param peer peer
  341. * @param plugin_name transport plugin name
  342. * @param plugin_addr plugin address
  343. * @param plugin_addr_len length of the @a plugin_addr
  344. * @param local_address_info the local address for the address
  345. * @param session_id session id, can never be 0.
  346. * @param prop performance information for this address
  347. */
  348. void
  349. GAS_addresses_add(const struct GNUNET_PeerIdentity *peer,
  350. const char *plugin_name,
  351. const void *plugin_addr,
  352. size_t plugin_addr_len,
  353. uint32_t local_address_info,
  354. uint32_t session_id,
  355. const struct GNUNET_ATS_Properties *prop);
  356. /**
  357. * Update an address with new performance information for a peer.
  358. *
  359. * @param peer peer
  360. * @param session_id session id, can never be 0
  361. * @param prop performance information for this address
  362. */
  363. void
  364. GAS_addresses_update(const struct GNUNET_PeerIdentity *peer,
  365. uint32_t session_id,
  366. const struct GNUNET_ATS_Properties *prop);
  367. /**
  368. * Remove an address for a peer.
  369. *
  370. * @param peer peer
  371. * @param session_id session id, can never be 0
  372. */
  373. void
  374. GAS_addresses_destroy(const struct GNUNET_PeerIdentity *peer,
  375. uint32_t session_id);
  376. /**
  377. * Remove all addresses.
  378. */
  379. void
  380. GAS_addresses_destroy_all(void);
  381. /**
  382. * Iterator for #GAS_addresses_get_peer_info()
  383. *
  384. * @param cls closure
  385. * @param id the peer id
  386. * @param plugin_name plugin name
  387. * @param plugin_addr address
  388. * @param plugin_addr_len length of @a plugin_addr
  389. * @param address_active is address actively used
  390. * @param atsi ats performance information
  391. * @param local_address_info flags for the address
  392. * @param bandwidth_out current outbound bandwidth assigned to address
  393. * @param bandwidth_in current inbound bandwidth assigned to address
  394. */
  395. typedef void
  396. (*GNUNET_ATS_PeerInfo_Iterator) (void *cls,
  397. const struct GNUNET_PeerIdentity *id,
  398. const char *plugin_name,
  399. const void *plugin_addr,
  400. size_t plugin_addr_len,
  401. const int address_active,
  402. const struct GNUNET_ATS_Properties *prop,
  403. enum GNUNET_HELLO_AddressInfo local_address_info,
  404. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  405. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in);
  406. /**
  407. * Return information all peers currently known to ATS
  408. *
  409. * @param peer the respective peer
  410. * @param pi_it the iterator to call for every peer
  411. * @param pi_it_cls the closure for @a pi_it
  412. */
  413. void
  414. GAS_addresses_get_peer_info(const struct GNUNET_PeerIdentity *peer,
  415. GNUNET_ATS_PeerInfo_Iterator pi_it,
  416. void *pi_it_cls);
  417. /**
  418. * Handle 'address list request' messages from clients.
  419. *
  420. * @param client client that sent the request
  421. * @param alrm the request message
  422. */
  423. void
  424. GAS_handle_request_address_list(struct GNUNET_SERVICE_Client *client,
  425. const struct AddressListRequestMessage *alrm);
  426. #endif
  427. /* end of gnunet-service-ats_addresses.h */