ats_api_scheduling.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 ats/ats_api_scheduling.c
  18. * @brief automatic transport selection and outbound bandwidth determination
  19. * @author Christian Grothoff
  20. * @author Matthias Wachs
  21. *
  22. * TODO:
  23. * - we could avoid a linear scan over the
  24. * active addresses in some cases, so if
  25. * there is need, we can still optimize here
  26. * - we might want to split off the logic to
  27. * determine LAN vs. WAN, as it has nothing
  28. * to do with accessing the ATS service.
  29. */
  30. #include "platform.h"
  31. #include "gnunet_ats_service.h"
  32. #include "ats.h"
  33. /**
  34. * How frequently do we scan the interfaces for changes to the addresses?
  35. */
  36. #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply ( \
  37. GNUNET_TIME_UNIT_MINUTES, 2)
  38. #define LOG(kind, ...) GNUNET_log_from (kind, "ats-scheduling-api", __VA_ARGS__)
  39. /**
  40. * Session ID we use if there is no session / slot.
  41. */
  42. #define NOT_FOUND 0
  43. /**
  44. * Information we track per address, incoming or outgoing. It also
  45. * doesn't matter if we have a session, any address that ATS is
  46. * allowed to suggest right now should be tracked.
  47. */
  48. struct GNUNET_ATS_AddressRecord
  49. {
  50. /**
  51. * Scheduling handle this address record belongs to.
  52. */
  53. struct GNUNET_ATS_SchedulingHandle *sh;
  54. /**
  55. * Address data.
  56. */
  57. struct GNUNET_HELLO_Address *address;
  58. /**
  59. * Session handle. NULL if we have an address but no
  60. * active session for this address.
  61. */
  62. struct GNUNET_ATS_Session *session;
  63. /**
  64. * Performance data about the address.
  65. */
  66. struct GNUNET_ATS_PropertiesNBO properties;
  67. /**
  68. * Which slot (index) in the session array does
  69. * this record correspond to?
  70. * FIXME: a linear search on this is really crappy!
  71. * Maybe switch to a 64-bit global counter and be
  72. * done with it? Or does that then cause too much
  73. * trouble on the ATS-service side?
  74. */
  75. uint32_t slot;
  76. /**
  77. * We're about to destroy this address record, just ATS does
  78. * not know this yet. Once ATS confirms its destruction,
  79. * we can clean up.
  80. */
  81. int in_destroy;
  82. };
  83. /**
  84. * Handle to the ATS subsystem for bandwidth/transport scheduling information.
  85. */
  86. struct GNUNET_ATS_SchedulingHandle
  87. {
  88. /**
  89. * Our configuration.
  90. */
  91. const struct GNUNET_CONFIGURATION_Handle *cfg;
  92. /**
  93. * Callback to invoke on suggestions.
  94. */
  95. GNUNET_ATS_AddressSuggestionCallback suggest_cb;
  96. /**
  97. * Closure for @e suggest_cb.
  98. */
  99. void *suggest_cb_cls;
  100. /**
  101. * Message queue for sending requests to the ATS service.
  102. */
  103. struct GNUNET_MQ_Handle *mq;
  104. /**
  105. * Array of session objects (we need to translate them to numbers and back
  106. * for the protocol; the offset in the array is the session number on the
  107. * network). Index 0 is always NULL and reserved to represent the NULL pointer.
  108. * Unused entries are also NULL.
  109. */
  110. struct GNUNET_ATS_AddressRecord **session_array;
  111. /**
  112. * Task to trigger reconnect.
  113. */
  114. struct GNUNET_SCHEDULER_Task *task;
  115. /**
  116. * Reconnect backoff delay.
  117. */
  118. struct GNUNET_TIME_Relative backoff;
  119. /**
  120. * Size of the @e session_array.
  121. */
  122. unsigned int session_array_size;
  123. };
  124. /**
  125. * Re-establish the connection to the ATS service.
  126. *
  127. * @param sh handle to use to re-connect.
  128. */
  129. static void
  130. reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
  131. /**
  132. * Re-establish the connection to the ATS service.
  133. *
  134. * @param cls handle to use to re-connect.
  135. */
  136. static void
  137. reconnect_task (void *cls)
  138. {
  139. struct GNUNET_ATS_SchedulingHandle *sh = cls;
  140. sh->task = NULL;
  141. reconnect (sh);
  142. }
  143. /**
  144. * Disconnect from ATS and then reconnect.
  145. *
  146. * @param sh our handle
  147. */
  148. static void
  149. force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
  150. {
  151. if (NULL != sh->mq)
  152. {
  153. GNUNET_MQ_destroy (sh->mq);
  154. sh->mq = NULL;
  155. }
  156. sh->suggest_cb (sh->suggest_cb_cls,
  157. NULL, NULL, NULL,
  158. GNUNET_BANDWIDTH_ZERO,
  159. GNUNET_BANDWIDTH_ZERO);
  160. sh->backoff = GNUNET_TIME_STD_BACKOFF (sh->backoff);
  161. sh->task = GNUNET_SCHEDULER_add_delayed (sh->backoff,
  162. &reconnect_task,
  163. sh);
  164. }
  165. /**
  166. * Find the session object corresponding to the given session ID.
  167. *
  168. * @param sh our handle
  169. * @param session_id current session ID
  170. * @param peer peer the session belongs to
  171. * @return the session object (or NULL)
  172. */
  173. static struct GNUNET_ATS_AddressRecord *
  174. find_session (struct GNUNET_ATS_SchedulingHandle *sh,
  175. uint32_t session_id,
  176. const struct GNUNET_PeerIdentity *peer)
  177. {
  178. struct GNUNET_ATS_AddressRecord *ar;
  179. if (session_id >= sh->session_array_size)
  180. {
  181. GNUNET_break (0);
  182. return NULL;
  183. }
  184. if (0 == session_id)
  185. return NULL;
  186. ar = sh->session_array[session_id];
  187. if (NULL == ar)
  188. {
  189. GNUNET_break (0);
  190. return NULL;
  191. }
  192. if (NULL == ar->address)
  193. {
  194. /* address was destroyed in the meantime, this can happen
  195. as we communicate asynchronously with the ATS service. */
  196. return NULL;
  197. }
  198. if (0 != GNUNET_memcmp (peer,
  199. &ar->address->peer))
  200. {
  201. GNUNET_break (0);
  202. return NULL;
  203. }
  204. return ar;
  205. }
  206. /**
  207. * Get an available session ID.
  208. *
  209. * @param sh our handle
  210. * @return an unused slot, but never NOT_FOUND (0)
  211. */
  212. static uint32_t
  213. find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh)
  214. {
  215. static uint32_t off;
  216. uint32_t i;
  217. GNUNET_assert (0 != sh->session_array_size);
  218. i = 0;
  219. while (((NOT_FOUND == off) ||
  220. (NULL != sh->session_array[off % sh->session_array_size])) &&
  221. (i < sh->session_array_size))
  222. {
  223. off++;
  224. i++;
  225. }
  226. if ((NOT_FOUND != off % sh->session_array_size) &&
  227. (NULL == sh->session_array[off % sh->session_array_size]))
  228. return off;
  229. i = sh->session_array_size;
  230. GNUNET_array_grow (sh->session_array,
  231. sh->session_array_size,
  232. sh->session_array_size * 2);
  233. return i;
  234. }
  235. /**
  236. * Get the ID for the given session object.
  237. *
  238. * @param sh our handle
  239. * @param session session object
  240. * @param address the address we are looking for
  241. * @return the session id or NOT_FOUND for error
  242. */
  243. static uint32_t
  244. find_session_id (struct GNUNET_ATS_SchedulingHandle *sh,
  245. struct GNUNET_ATS_Session *session,
  246. const struct GNUNET_HELLO_Address *address)
  247. {
  248. uint32_t i;
  249. if (NULL == address)
  250. {
  251. GNUNET_break (0);
  252. return NOT_FOUND;
  253. }
  254. for (i = 1; i < sh->session_array_size; i++)
  255. if ((NULL != sh->session_array[i]) &&
  256. (GNUNET_NO == sh->session_array[i]->in_destroy) &&
  257. ((session == sh->session_array[i]->session) ||
  258. (NULL == sh->session_array[i]->session)) &&
  259. (0 == GNUNET_memcmp (&address->peer,
  260. &sh->session_array[i]->address->peer)) &&
  261. (0 == GNUNET_HELLO_address_cmp (address,
  262. sh->session_array[i]->address)))
  263. return i;
  264. return NOT_FOUND;
  265. }
  266. /**
  267. * Release the session slot from the session table (ATS service is
  268. * also done using it).
  269. *
  270. * @param sh our handle
  271. * @param session_id identifies session that is no longer valid
  272. */
  273. static void
  274. release_session (struct GNUNET_ATS_SchedulingHandle *sh,
  275. uint32_t session_id)
  276. {
  277. struct GNUNET_ATS_AddressRecord *ar;
  278. if (NOT_FOUND == session_id)
  279. return;
  280. if (session_id >= sh->session_array_size)
  281. {
  282. GNUNET_break (0);
  283. force_reconnect (sh);
  284. return;
  285. }
  286. /* this slot should have been removed from remove_session before */
  287. ar = sh->session_array[session_id];
  288. if (NULL != ar->session)
  289. {
  290. GNUNET_break (0);
  291. force_reconnect (sh);
  292. return;
  293. }
  294. GNUNET_HELLO_address_free (ar->address);
  295. GNUNET_free (ar);
  296. sh->session_array[session_id] = NULL;
  297. }
  298. /**
  299. * Type of a function to call when we receive a session release
  300. * message from the service.
  301. *
  302. * @param cls the `struct GNUNET_ATS_SchedulingHandle`
  303. * @param srm message received
  304. */
  305. static void
  306. handle_ats_session_release (void *cls,
  307. const struct GNUNET_ATS_SessionReleaseMessage *srm)
  308. {
  309. struct GNUNET_ATS_SchedulingHandle *sh = cls;
  310. /* Note: peer field in srm not necessary right now,
  311. but might be good to have in the future */
  312. release_session (sh,
  313. ntohl (srm->session_id));
  314. }
  315. /**
  316. * Type of a function to call when we receive a address suggestion
  317. * message from the service.
  318. *
  319. * @param cls the `struct GNUNET_ATS_SchedulingHandle`
  320. * @param m message received
  321. */
  322. static void
  323. handle_ats_address_suggestion (void *cls,
  324. const struct AddressSuggestionMessage *m)
  325. {
  326. struct GNUNET_ATS_SchedulingHandle *sh = cls;
  327. struct GNUNET_ATS_AddressRecord *ar;
  328. uint32_t session_id;
  329. session_id = ntohl (m->session_id);
  330. if (0 == session_id)
  331. {
  332. GNUNET_break (0);
  333. force_reconnect (sh);
  334. return;
  335. }
  336. ar = find_session (sh,
  337. session_id,
  338. &m->peer);
  339. if (NULL == ar)
  340. {
  341. GNUNET_break (0);
  342. force_reconnect (sh);
  343. return;
  344. }
  345. if (NULL == sh->suggest_cb)
  346. return;
  347. if (GNUNET_YES == ar->in_destroy)
  348. {
  349. /* ignore suggestion, as this address is dying, unless BW is 0,
  350. in that case signal 'disconnect' via BW 0 */
  351. if ((0 == ntohl (m->bandwidth_out.value__)) &&
  352. (0 == ntohl (m->bandwidth_in.value__)))
  353. {
  354. LOG (GNUNET_ERROR_TYPE_DEBUG,
  355. "ATS suggests disconnect from peer `%s' with BW %u/%u\n",
  356. GNUNET_i2s (&ar->address->peer),
  357. (unsigned int) ntohl (m->bandwidth_out.value__),
  358. (unsigned int) ntohl (m->bandwidth_in.value__));
  359. sh->suggest_cb (sh->suggest_cb_cls,
  360. &m->peer,
  361. NULL,
  362. NULL,
  363. m->bandwidth_out,
  364. m->bandwidth_in);
  365. }
  366. return;
  367. }
  368. if ((NULL == ar->session) &&
  369. (GNUNET_HELLO_address_check_option (ar->address,
  370. GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
  371. {
  372. GNUNET_break (0);
  373. return;
  374. }
  375. sh->backoff = GNUNET_TIME_UNIT_ZERO;
  376. LOG (GNUNET_ERROR_TYPE_DEBUG,
  377. "ATS suggests address slot %u for peer `%s' using plugin %s\n",
  378. ar->slot,
  379. GNUNET_i2s (&ar->address->peer),
  380. ar->address->transport_name);
  381. sh->suggest_cb (sh->suggest_cb_cls,
  382. &m->peer,
  383. ar->address,
  384. ar->session,
  385. m->bandwidth_out,
  386. m->bandwidth_in);
  387. }
  388. /**
  389. * We encountered an error handling the MQ to the
  390. * ATS service. Reconnect.
  391. *
  392. * @param cls the `struct GNUNET_ATS_SchedulingHandle`
  393. * @param error details about the error
  394. */
  395. static void
  396. error_handler (void *cls,
  397. enum GNUNET_MQ_Error error)
  398. {
  399. struct GNUNET_ATS_SchedulingHandle *sh = cls;
  400. LOG (GNUNET_ERROR_TYPE_DEBUG,
  401. "ATS connection died (code %d), reconnecting\n",
  402. (int) error);
  403. force_reconnect (sh);
  404. }
  405. /**
  406. * Generate and transmit the `struct AddressAddMessage` for the given
  407. * address record.
  408. *
  409. * @param sh the scheduling handle to use for transmission
  410. * @param ar the address to inform the ATS service about
  411. */
  412. static void
  413. send_add_address_message (struct GNUNET_ATS_SchedulingHandle *sh,
  414. const struct GNUNET_ATS_AddressRecord *ar)
  415. {
  416. struct GNUNET_MQ_Envelope *ev;
  417. struct AddressAddMessage *m;
  418. char *pm;
  419. size_t namelen;
  420. size_t msize;
  421. if (NULL == sh->mq)
  422. return; /* disconnected, skip for now */
  423. GNUNET_break (GNUNET_NT_UNSPECIFIED != ar->properties.scope);
  424. namelen = strlen (ar->address->transport_name) + 1;
  425. msize = ar->address->address_length + namelen;
  426. ev = GNUNET_MQ_msg_extra (m, msize, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
  427. m->peer = ar->address->peer;
  428. m->address_length = htons (ar->address->address_length);
  429. m->address_local_info = htonl ((uint32_t) ar->address->local_info);
  430. m->plugin_name_length = htons (namelen);
  431. m->session_id = htonl (ar->slot);
  432. m->properties = ar->properties;
  433. LOG (GNUNET_ERROR_TYPE_DEBUG,
  434. "Adding address for peer `%s', plugin `%s', session %p slot %u\n",
  435. GNUNET_i2s (&ar->address->peer),
  436. ar->address->transport_name,
  437. ar->session,
  438. ar->slot);
  439. pm = (char *) &m[1];
  440. GNUNET_memcpy (pm,
  441. ar->address->address,
  442. ar->address->address_length);
  443. if (NULL != ar->address->transport_name)
  444. GNUNET_memcpy (&pm[ar->address->address_length],
  445. ar->address->transport_name,
  446. namelen);
  447. GNUNET_MQ_send (sh->mq, ev);
  448. }
  449. /**
  450. * Re-establish the connection to the ATS service.
  451. *
  452. * @param sh handle to use to re-connect.
  453. */
  454. static void
  455. reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
  456. {
  457. struct GNUNET_MQ_MessageHandler handlers[] = {
  458. GNUNET_MQ_hd_fixed_size (ats_session_release,
  459. GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE,
  460. struct GNUNET_ATS_SessionReleaseMessage,
  461. sh),
  462. GNUNET_MQ_hd_fixed_size (ats_address_suggestion,
  463. GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION,
  464. struct AddressSuggestionMessage,
  465. sh),
  466. GNUNET_MQ_handler_end ()
  467. };
  468. struct GNUNET_MQ_Envelope *ev;
  469. struct ClientStartMessage *init;
  470. unsigned int i;
  471. struct GNUNET_ATS_AddressRecord *ar;
  472. GNUNET_assert (NULL == sh->mq);
  473. sh->mq = GNUNET_CLIENT_connect (sh->cfg,
  474. "ats",
  475. handlers,
  476. &error_handler,
  477. sh);
  478. if (NULL == sh->mq)
  479. {
  480. GNUNET_break (0);
  481. force_reconnect (sh);
  482. return;
  483. }
  484. ev = GNUNET_MQ_msg (init,
  485. GNUNET_MESSAGE_TYPE_ATS_START);
  486. init->start_flag = htonl (START_FLAG_SCHEDULING);
  487. GNUNET_MQ_send (sh->mq, ev);
  488. if (NULL == sh->mq)
  489. return;
  490. for (i = 0; i < sh->session_array_size; i++)
  491. {
  492. ar = sh->session_array[i];
  493. if (NULL == ar)
  494. continue;
  495. send_add_address_message (sh, ar);
  496. if (NULL == sh->mq)
  497. return;
  498. }
  499. }
  500. /**
  501. * Initialize the ATS subsystem.
  502. *
  503. * @param cfg configuration to use
  504. * @param suggest_cb notification to call whenever the suggestation changed
  505. * @param suggest_cb_cls closure for @a suggest_cb
  506. * @return ats context
  507. */
  508. struct GNUNET_ATS_SchedulingHandle *
  509. GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
  510. GNUNET_ATS_AddressSuggestionCallback suggest_cb,
  511. void *suggest_cb_cls)
  512. {
  513. struct GNUNET_ATS_SchedulingHandle *sh;
  514. sh = GNUNET_new (struct GNUNET_ATS_SchedulingHandle);
  515. sh->cfg = cfg;
  516. sh->suggest_cb = suggest_cb;
  517. sh->suggest_cb_cls = suggest_cb_cls;
  518. GNUNET_array_grow (sh->session_array,
  519. sh->session_array_size,
  520. 4);
  521. reconnect (sh);
  522. return sh;
  523. }
  524. /**
  525. * Client is done with ATS scheduling, release resources.
  526. *
  527. * @param sh handle to release
  528. */
  529. void
  530. GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
  531. {
  532. struct GNUNET_ATS_AddressRecord *ar;
  533. unsigned int i;
  534. if (NULL != sh->mq)
  535. {
  536. GNUNET_MQ_destroy (sh->mq);
  537. sh->mq = NULL;
  538. }
  539. if (NULL != sh->task)
  540. {
  541. GNUNET_SCHEDULER_cancel (sh->task);
  542. sh->task = NULL;
  543. }
  544. for (i = 0; i < sh->session_array_size; i++)
  545. {
  546. if (NULL != (ar = sh->session_array[i]))
  547. {
  548. GNUNET_HELLO_address_free (ar->address);
  549. GNUNET_free (ar);
  550. sh->session_array[i] = NULL;
  551. }
  552. }
  553. GNUNET_array_grow (sh->session_array,
  554. sh->session_array_size,
  555. 0);
  556. GNUNET_free (sh);
  557. }
  558. /**
  559. * We have a new address ATS should know. Addresses have to be added
  560. * with this function before they can be: updated, set in use and
  561. * destroyed.
  562. *
  563. * @param sh handle
  564. * @param address the address
  565. * @param session session handle, can be NULL
  566. * @param prop performance data for the address
  567. * @return handle to the address representation inside ATS, NULL
  568. * on error (i.e. ATS knows this exact address already)
  569. */
  570. struct GNUNET_ATS_AddressRecord *
  571. GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
  572. const struct GNUNET_HELLO_Address *address,
  573. struct GNUNET_ATS_Session *session,
  574. const struct GNUNET_ATS_Properties *prop)
  575. {
  576. struct GNUNET_ATS_AddressRecord *ar;
  577. size_t namelen;
  578. size_t msize;
  579. uint32_t s;
  580. if (NULL == address)
  581. {
  582. /* we need a valid address */
  583. GNUNET_break (0);
  584. return NULL;
  585. }
  586. GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
  587. namelen = strlen (address->transport_name) + 1;
  588. msize = address->address_length + namelen;
  589. if ((msize + sizeof(struct AddressUpdateMessage) >=
  590. GNUNET_MAX_MESSAGE_SIZE) ||
  591. (address->address_length >= GNUNET_MAX_MESSAGE_SIZE) ||
  592. (namelen >= GNUNET_MAX_MESSAGE_SIZE))
  593. {
  594. /* address too large for us, this should not happen */
  595. GNUNET_break (0);
  596. return NULL;
  597. }
  598. if (NOT_FOUND !=
  599. find_session_id (sh,
  600. session,
  601. address))
  602. {
  603. /* Already existing, nothing todo, but this should not happen */
  604. GNUNET_break (0);
  605. return NULL;
  606. }
  607. s = find_empty_session_slot (sh);
  608. ar = GNUNET_new (struct GNUNET_ATS_AddressRecord);
  609. ar->sh = sh;
  610. ar->slot = s;
  611. ar->session = session;
  612. ar->address = GNUNET_HELLO_address_copy (address);
  613. GNUNET_ATS_properties_hton (&ar->properties,
  614. prop);
  615. sh->session_array[s] = ar;
  616. send_add_address_message (sh, ar);
  617. return ar;
  618. }
  619. /**
  620. * An address was used to initiate a session.
  621. *
  622. * @param ar address record to update information for
  623. * @param session session handle
  624. */
  625. void
  626. GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
  627. struct GNUNET_ATS_Session *session)
  628. {
  629. GNUNET_break (NULL == ar->session);
  630. ar->session = session;
  631. }
  632. /**
  633. * A session was destroyed, disassociate it from the
  634. * given address record. If this was an incoming
  635. * addess, destroy the address as well.
  636. *
  637. * @param ar address record to update information for
  638. * @param session session handle
  639. * @return #GNUNET_YES if the @a ar was destroyed because
  640. * it was an incoming address,
  641. * #GNUNET_NO if the @ar was kept because we can
  642. * use it still to establish a new session
  643. */
  644. int
  645. GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
  646. struct GNUNET_ATS_Session *session)
  647. {
  648. GNUNET_assert (session == ar->session);
  649. ar->session = NULL;
  650. if (GNUNET_HELLO_address_check_option (ar->address,
  651. GNUNET_HELLO_ADDRESS_INFO_INBOUND))
  652. {
  653. GNUNET_ATS_address_destroy (ar);
  654. return GNUNET_YES;
  655. }
  656. return GNUNET_NO;
  657. }
  658. /**
  659. * We have updated performance statistics for a given address. Note
  660. * that this function can be called for addresses that are currently
  661. * in use as well as addresses that are valid but not actively in use.
  662. * Furthermore, the peer may not even be connected to us right now (in
  663. * which case the call may be ignored or the information may be stored
  664. * for later use). Update bandwidth assignments.
  665. *
  666. * @param ar address record to update information for
  667. * @param prop performance data for the address
  668. */
  669. void
  670. GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
  671. const struct GNUNET_ATS_Properties *prop)
  672. {
  673. struct GNUNET_ATS_SchedulingHandle *sh = ar->sh;
  674. struct GNUNET_MQ_Envelope *ev;
  675. struct AddressUpdateMessage *m;
  676. LOG (GNUNET_ERROR_TYPE_DEBUG,
  677. "Updating address for peer `%s', plugin `%s', session %p slot %u\n",
  678. GNUNET_i2s (&ar->address->peer),
  679. ar->address->transport_name,
  680. ar->session,
  681. ar->slot);
  682. GNUNET_break (GNUNET_NT_UNSPECIFIED != prop->scope);
  683. GNUNET_ATS_properties_hton (&ar->properties,
  684. prop);
  685. if (NULL == sh->mq)
  686. return; /* disconnected, skip for now */
  687. ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
  688. m->session_id = htonl (ar->slot);
  689. m->peer = ar->address->peer;
  690. m->properties = ar->properties;
  691. GNUNET_MQ_send (sh->mq,
  692. ev);
  693. }
  694. /**
  695. * An address got destroyed, stop using it as a valid address.
  696. *
  697. * @param ar address to destroy
  698. */
  699. void
  700. GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar)
  701. {
  702. struct GNUNET_ATS_SchedulingHandle *sh = ar->sh;
  703. struct GNUNET_MQ_Envelope *ev;
  704. struct AddressDestroyedMessage *m;
  705. LOG (GNUNET_ERROR_TYPE_DEBUG,
  706. "Deleting address for peer `%s', plugin `%s', slot %u session %p\n",
  707. GNUNET_i2s (&ar->address->peer),
  708. ar->address->transport_name,
  709. ar->slot,
  710. ar->session);
  711. GNUNET_break (NULL == ar->session);
  712. ar->session = NULL;
  713. ar->in_destroy = GNUNET_YES;
  714. if (NULL == sh->mq)
  715. return;
  716. ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
  717. m->session_id = htonl (ar->slot);
  718. m->peer = ar->address->peer;
  719. GNUNET_MQ_send (sh->mq, ev);
  720. }
  721. /* end of ats_api_scheduling.c */