gnunet_ats_service.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2015 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
  18. * Automatic transport selection and outbound bandwidth determination
  19. *
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. *
  23. * @defgroup ats ATS service
  24. * Automatic Transport Selection and outbound bandwidth determination
  25. *
  26. * @see [Documentation](https://gnunet.org/ats-subsystem)
  27. *
  28. * @{
  29. */
  30. #ifndef GNUNET_ATS_SERVICE_H
  31. #define GNUNET_ATS_SERVICE_H
  32. #include "gnunet_constants.h"
  33. #include "gnunet_util_lib.h"
  34. #include "gnunet_hello_lib.h"
  35. /**
  36. * Default bandwidth assigned to a network : 64 KB/s
  37. */
  38. #define GNUNET_ATS_DefaultBandwidth 65536
  39. /**
  40. * Undefined value for an `enum GNUNET_ATS_Property`
  41. */
  42. #define GNUNET_ATS_VALUE_UNDEFINED UINT32_MAX
  43. /**
  44. * String representation for GNUNET_ATS_VALUE_UNDEFINED
  45. */
  46. #define GNUNET_ATS_VALUE_UNDEFINED_STR "undefined"
  47. /**
  48. * Maximum bandwidth assigned to a network : 4095 MB/s
  49. */
  50. #define GNUNET_ATS_MaxBandwidth UINT32_MAX
  51. /**
  52. * Textual equivalent for GNUNET_ATS_MaxBandwidth
  53. */
  54. #define GNUNET_ATS_MaxBandwidthString "unlimited"
  55. /**
  56. * ATS performance characteristics for an address.
  57. */
  58. struct GNUNET_ATS_Properties
  59. {
  60. /**
  61. * Delay. Time between when the time packet is sent and the packet
  62. * arrives. FOREVER if we did not measure yet.
  63. */
  64. struct GNUNET_TIME_Relative delay;
  65. /**
  66. * Actual traffic on this connection from this peer to the other peer.
  67. * Includes transport overhead.
  68. *
  69. * Unit: [bytes/second]
  70. */
  71. uint32_t utilization_out;
  72. /**
  73. * Actual traffic on this connection from the other peer to this peer.
  74. * Includes transport overhead.
  75. *
  76. * Unit: [bytes/second]
  77. */
  78. uint32_t utilization_in;
  79. /**
  80. * Distance on network layer (required for distance-vector routing)
  81. * in hops. Zero for direct connections (i.e. plain TCP/UDP).
  82. */
  83. unsigned int distance;
  84. /**
  85. * Which network scope does the respective address belong to?
  86. * This property does not change.
  87. */
  88. enum GNUNET_NetworkType scope;
  89. };
  90. /**
  91. * ATS performance characteristics for an address in
  92. * network byte order (for IPC).
  93. */
  94. struct GNUNET_ATS_PropertiesNBO
  95. {
  96. /**
  97. * Actual traffic on this connection from this peer to the other peer.
  98. * Includes transport overhead.
  99. *
  100. * Unit: [bytes/second]
  101. */
  102. uint32_t utilization_out GNUNET_PACKED;
  103. /**
  104. * Actual traffic on this connection from the other peer to this peer.
  105. * Includes transport overhead.
  106. *
  107. * Unit: [bytes/second]
  108. */
  109. uint32_t utilization_in GNUNET_PACKED;
  110. /**
  111. * Which network scope does the respective address belong to?
  112. * This property does not change.
  113. */
  114. uint32_t scope GNUNET_PACKED;
  115. /**
  116. * Distance on network layer (required for distance-vector routing)
  117. * in hops. Zero for direct connections (i.e. plain TCP/UDP).
  118. */
  119. uint32_t distance GNUNET_PACKED;
  120. /**
  121. * Delay. Time between when the time packet is sent and the packet
  122. * arrives. FOREVER if we did not measure yet.
  123. */
  124. struct GNUNET_TIME_RelativeNBO delay;
  125. };
  126. /* ********************* LAN Characterization library ************************ */
  127. /* Note: these functions do not really communicate with the ATS service */
  128. /**
  129. * Convert ATS properties from host to network byte order.
  130. *
  131. * @param nbo[OUT] value written
  132. * @param hbo value read
  133. */
  134. void
  135. GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
  136. const struct GNUNET_ATS_Properties *hbo);
  137. /**
  138. * Convert ATS properties from network to host byte order.
  139. *
  140. * @param hbo[OUT] value written
  141. * @param nbo value read
  142. */
  143. void
  144. GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
  145. const struct GNUNET_ATS_PropertiesNBO *nbo);
  146. /* ********************Connection Suggestion API ***************************** */
  147. /**
  148. * Handle to the ATS subsystem for making suggestions about
  149. * connections the peer would like to have.
  150. */
  151. struct GNUNET_ATS_ConnectivityHandle;
  152. /**
  153. * Handle for address suggestion requests.
  154. */
  155. struct GNUNET_ATS_ConnectivitySuggestHandle;
  156. /**
  157. * Initialize the ATS connectivity suggestion client handle.
  158. *
  159. * @param cfg configuration to use
  160. * @return ats connectivity handle, NULL on error
  161. */
  162. struct GNUNET_ATS_ConnectivityHandle *
  163. GNUNET_ATS_connectivity_init (const struct GNUNET_CONFIGURATION_Handle *cfg);
  164. /**
  165. * Shutdown ATS connectivity suggestion client.
  166. *
  167. * @param ch handle to destroy
  168. */
  169. void
  170. GNUNET_ATS_connectivity_done (struct GNUNET_ATS_ConnectivityHandle *ch);
  171. /**
  172. * We would like to establish a new connection with a peer. ATS
  173. * should suggest a good address to begin with.
  174. *
  175. * @param ch handle
  176. * @param peer identity of the peer we need an address for
  177. * @param strength how urgent is the need for such a suggestion
  178. * @return suggestion handle, NULL if request is already pending
  179. */
  180. struct GNUNET_ATS_ConnectivitySuggestHandle *
  181. GNUNET_ATS_connectivity_suggest (struct GNUNET_ATS_ConnectivityHandle *ch,
  182. const struct GNUNET_PeerIdentity *peer,
  183. uint32_t strength);
  184. /**
  185. * We no longer care about being connected to a peer.
  186. *
  187. * @param sh handle
  188. */
  189. void
  190. GNUNET_ATS_connectivity_suggest_cancel (struct GNUNET_ATS_ConnectivitySuggestHandle *sh);
  191. /* ******************************** Scheduling API ***************************** */
  192. /**
  193. * Handle to the ATS subsystem for bandwidth/transport scheduling information.
  194. */
  195. struct GNUNET_ATS_SchedulingHandle;
  196. /**
  197. * Opaque session handle, defined by plugins. Contents not known to ATS.
  198. */
  199. struct GNUNET_ATS_Session;
  200. /**
  201. * Signature of a function called by ATS with the current bandwidth
  202. * and address preferences as determined by ATS. If our connection
  203. * to ATS dies and thus all suggestions become invalid, this function
  204. * is called ONCE with all arguments (except @a cls) being NULL/0.
  205. *
  206. * @param cls closure
  207. * @param peer for which we suggest an address, NULL if ATS connection died
  208. * @param address suggested address (including peer identity of the peer),
  209. * may be NULL to signal disconnect from peer
  210. * @param session session to use, NULL to establish a new outgoing session
  211. * @param bandwidth_out assigned outbound bandwidth for the connection,
  212. * 0 to signal disconnect
  213. * @param bandwidth_in assigned inbound bandwidth for the connection,
  214. * 0 to signal disconnect
  215. */
  216. typedef void
  217. (*GNUNET_ATS_AddressSuggestionCallback) (void *cls,
  218. const struct GNUNET_PeerIdentity *peer,
  219. const struct GNUNET_HELLO_Address *address,
  220. struct GNUNET_ATS_Session *session,
  221. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  222. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in);
  223. /**
  224. * Initialize the ATS scheduling subsystem.
  225. *
  226. * @param cfg configuration to use
  227. * @param suggest_cb notification to call whenever the suggestation changed
  228. * @param suggest_cb_cls closure for @a suggest_cb
  229. * @return ats context
  230. */
  231. struct GNUNET_ATS_SchedulingHandle *
  232. GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
  233. GNUNET_ATS_AddressSuggestionCallback suggest_cb,
  234. void *suggest_cb_cls);
  235. /**
  236. * Client is done with ATS scheduling, release resources.
  237. *
  238. * @param sh handle to release
  239. */
  240. void
  241. GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh);
  242. /**
  243. * Handle used within ATS to track an address.
  244. */
  245. struct GNUNET_ATS_AddressRecord;
  246. /**
  247. * We have a new address ATS should know. Addresses have to be added with this
  248. * function before they can be: updated, set in use and destroyed
  249. *
  250. * @param sh handle
  251. * @param address the address
  252. * @param session session handle (if available, i.e. for incoming connections)
  253. * @param prop performance data for the address
  254. * @return handle to the address representation inside ATS, NULL
  255. * on error (i.e. ATS knows this exact address already, or
  256. * address is invalid)
  257. */
  258. struct GNUNET_ATS_AddressRecord *
  259. GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
  260. const struct GNUNET_HELLO_Address *address,
  261. struct GNUNET_ATS_Session *session,
  262. const struct GNUNET_ATS_Properties *prop);
  263. /**
  264. * An address was used to initiate a session.
  265. *
  266. * @param ar address record to update information for
  267. * @param session session handle
  268. */
  269. void
  270. GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
  271. struct GNUNET_ATS_Session *session);
  272. /**
  273. * A @a session was destroyed, disassociate it from the given address
  274. * record. If this was an incoming addess, destroys the address as
  275. * well.
  276. *
  277. * @param ar address record to update information for
  278. * @param session session handle
  279. * @return #GNUNET_YES if the @a ar was destroyed because
  280. * it was an incoming address,
  281. * #GNUNET_NO if the @ar was kept because we can
  282. * use it still to establish a new session
  283. */
  284. int
  285. GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
  286. struct GNUNET_ATS_Session *session);
  287. /**
  288. * We have updated performance statistics for a given address. Note
  289. * that this function can be called for addresses that are currently
  290. * in use as well as addresses that are valid but not actively in use.
  291. * Furthermore, the peer may not even be connected to us right now (@a
  292. * session value of NULL used to signal disconnect, or somehow we
  293. * otherwise got updated on @a ats information). Based on the
  294. * information provided, ATS may update bandwidth assignments and
  295. * suggest to switch addresses.
  296. *
  297. * @param ar address record to update information for
  298. * @param prop performance data for the address
  299. */
  300. void
  301. GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
  302. const struct GNUNET_ATS_Properties *prop);
  303. /**
  304. * An address got destroyed, stop using it as a valid address.
  305. *
  306. * @param ar address record to destroy, it's validation has
  307. * expired and ATS may no longer use it
  308. */
  309. void
  310. GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar);
  311. /* ******************************** Performance API ***************************** */
  312. /**
  313. * ATS Handle to obtain and/or modify performance information.
  314. */
  315. struct GNUNET_ATS_PerformanceHandle;
  316. /**
  317. * Signature of a function that is called with QoS information about an address.
  318. *
  319. * @param cls closure
  320. * @param address the address, NULL if ATS service was disconnected or
  321. * when the iteration is completed in the case of
  322. * #GNUNET_ATS_performance_list_addresses()
  323. * @param address_active #GNUNET_YES if this address is actively used
  324. * to maintain a connection to a peer;
  325. * #GNUNET_NO if the address is not actively used;
  326. * #GNUNET_SYSERR if this address is no longer available for ATS
  327. * @param bandwidth_out assigned outbound bandwidth for the connection
  328. * @param bandwidth_in assigned inbound bandwidth for the connection
  329. * @param prop performance data for the address
  330. */
  331. typedef void
  332. (*GNUNET_ATS_AddressInformationCallback) (void *cls,
  333. const struct GNUNET_HELLO_Address *address,
  334. int address_active,
  335. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  336. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  337. const struct GNUNET_ATS_Properties *prop);
  338. /**
  339. * Handle for an address listing operation
  340. */
  341. struct GNUNET_ATS_AddressListHandle;
  342. /**
  343. * Get handle to access performance API of the ATS subsystem.
  344. *
  345. * @param cfg configuration to use
  346. * @param addr_info_cb callback called when performance characteristics for
  347. * an address change
  348. * @param addr_info_cb_cls closure for @a addr_info_cb
  349. * @return ats performance context
  350. */
  351. struct GNUNET_ATS_PerformanceHandle *
  352. GNUNET_ATS_performance_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
  353. GNUNET_ATS_AddressInformationCallback addr_info_cb,
  354. void *addr_info_cb_cls);
  355. /**
  356. * Get information about addresses known to the ATS subsystem.
  357. *
  358. * @param ph the performance handle to use
  359. * @param peer peer idm can be NULL for all peers
  360. * @param all #GNUNET_YES to get information about all addresses or #GNUNET_NO to
  361. * get only address currently used
  362. * @param infocb callback to call with the addresses,
  363. * will callback with address == NULL when done
  364. * @param infocb_cls closure for @a infocb
  365. * @return handle to abort the operation
  366. */
  367. struct GNUNET_ATS_AddressListHandle *
  368. GNUNET_ATS_performance_list_addresses (struct GNUNET_ATS_PerformanceHandle *ph,
  369. const struct GNUNET_PeerIdentity *peer,
  370. int all,
  371. GNUNET_ATS_AddressInformationCallback infocb,
  372. void *infocb_cls);
  373. /**
  374. * Cancel a pending address listing operation
  375. *
  376. * @param alh the `struct GNUNET_ATS_AddressListHandle` handle to cancel
  377. */
  378. void
  379. GNUNET_ATS_performance_list_addresses_cancel (struct GNUNET_ATS_AddressListHandle *alh);
  380. /**
  381. * Client is done using the ATS performance subsystem, release resources.
  382. *
  383. * @param ph handle
  384. */
  385. void
  386. GNUNET_ATS_performance_done (struct GNUNET_ATS_PerformanceHandle *ph);
  387. /**
  388. * Function called with reservation result.
  389. *
  390. * @param cls closure
  391. * @param peer identifies the peer
  392. * @param amount set to the amount that was actually reserved or unreserved;
  393. * either the full requested amount or zero (no partial reservations)
  394. * @param res_delay if the reservation could not be satisfied (amount was 0), how
  395. * long should the client wait until re-trying?
  396. */
  397. typedef void
  398. (*GNUNET_ATS_ReservationCallback) (void *cls,
  399. const struct GNUNET_PeerIdentity *peer,
  400. int32_t amount,
  401. struct GNUNET_TIME_Relative res_delay);
  402. /**
  403. * Context that can be used to cancel a peer information request.
  404. */
  405. struct GNUNET_ATS_ReservationContext;
  406. /**
  407. * Reserve inbound bandwidth from the given peer. ATS will look at
  408. * the current amount of traffic we receive from the peer and ensure
  409. * that the peer could add 'amount' of data to its stream.
  410. *
  411. * @param ph performance handle
  412. * @param peer identifies the peer
  413. * @param amount reserve N bytes for receiving, negative
  414. * amounts can be used to undo a (recent) reservation;
  415. * @param rcb function to call with the resulting reservation information
  416. * @param rcb_cls closure for @a rcb
  417. * @return NULL on error
  418. * @deprecated will be replaced soon
  419. */
  420. struct GNUNET_ATS_ReservationContext *
  421. GNUNET_ATS_reserve_bandwidth (struct GNUNET_ATS_PerformanceHandle *ph,
  422. const struct GNUNET_PeerIdentity *peer,
  423. int32_t amount,
  424. GNUNET_ATS_ReservationCallback rcb,
  425. void *rcb_cls);
  426. /**
  427. * Cancel request for reserving bandwidth.
  428. *
  429. * @param rc context returned by the original GNUNET_ATS_reserve_bandwidth call
  430. */
  431. void
  432. GNUNET_ATS_reserve_bandwidth_cancel (struct GNUNET_ATS_ReservationContext *rc);
  433. /**
  434. * ATS preference types as array initializer
  435. */
  436. #define GNUNET_ATS_PreferenceType {GNUNET_ATS_PREFERENCE_BANDWIDTH, GNUNET_ATS_PREFERENCE_LATENCY, GNUNET_ATS_PREFERENCE_END}
  437. /**
  438. * ATS preference types as string array initializer
  439. */
  440. #define GNUNET_ATS_PreferenceTypeString {"BANDWIDTH", "LATENCY", "END" }
  441. /**
  442. * Enum defining all known preference categories.
  443. */
  444. enum GNUNET_ATS_PreferenceKind
  445. {
  446. /**
  447. * Change the peer's bandwidth value (value per byte of bandwidth in
  448. * the goal function) to the given amount. The argument is followed
  449. * by a double value giving the desired value (can be negative).
  450. * Preference changes are forgotten if peers disconnect.
  451. */
  452. GNUNET_ATS_PREFERENCE_BANDWIDTH = 0,
  453. /**
  454. * Change the peer's latency value to the given amount. The
  455. * argument is followed by a double value giving the desired value
  456. * (can be negative). The absolute score in the goal function is
  457. * the inverse of the latency in microseconds (minimum: 1
  458. * microsecond) multiplied by the latency preferences.
  459. */
  460. GNUNET_ATS_PREFERENCE_LATENCY = 1,
  461. /**
  462. * End of preference list.
  463. */
  464. GNUNET_ATS_PREFERENCE_END = 2
  465. };
  466. /**
  467. * Convert a GNUNET_ATS_PreferenceType to a string
  468. *
  469. * @param type the preference type
  470. * @return a string or NULL if invalid
  471. */
  472. const char *
  473. GNUNET_ATS_print_preference_type (enum GNUNET_ATS_PreferenceKind type);
  474. /**
  475. * Change preferences for the given peer. Preference changes are forgotten if peers
  476. * disconnect.
  477. *
  478. * @param ph performance handle @param peer identifies the peer
  479. * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the
  480. * desired changes
  481. */
  482. void
  483. GNUNET_ATS_performance_change_preference (struct GNUNET_ATS_PerformanceHandle *ph,
  484. const struct GNUNET_PeerIdentity *peer,
  485. ...);
  486. /**
  487. * Application feedback on how good preference requirements are fulfilled
  488. * for the preferences included in the given time scope [now - scope .. now]
  489. *
  490. * An application notifies ATS if (and only if) it has feedback information
  491. * for specific properties. This values are valid until the feedback scores are
  492. * updated by the application.
  493. *
  494. * If the application has no feedback for this preference kind the application
  495. * will not explicitly call for this property and will not include it in this
  496. * function call.
  497. *
  498. * @param ph performance handle
  499. * @param scope the time interval this valid for: [now - scope .. now]
  500. * @param peer identifies the peer
  501. * @param ... #GNUNET_ATS_PREFERENCE_END-terminated specification of the desired changes
  502. */
  503. void
  504. GNUNET_ATS_performance_give_feedback (struct GNUNET_ATS_PerformanceHandle *ph,
  505. const struct GNUNET_PeerIdentity *peer,
  506. const struct GNUNET_TIME_Relative scope,
  507. ...);
  508. #endif
  509. /** @} */ /* end of group */
  510. /* end of file gnunet-service-transport_ats.h */