plugin_ats_proportional.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011-2014 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file ats/plugin_ats_proportional.c
  19. * @brief ATS proportional solver
  20. * @author Matthias Wachs
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_statistics_service.h"
  25. #include "gnunet_ats_plugin.h"
  26. #include "gnunet_ats_service.h"
  27. #include "gnunet-service-ats_addresses.h"
  28. #define PROP_STABILITY_FACTOR 1.25
  29. #define LOG(kind,...) GNUNET_log_from (kind, "ats-proportional",__VA_ARGS__)
  30. /**
  31. *
  32. * NOTE: Do not change this documentation. This documentation is based
  33. * on gnunet.org:/vcs/fsnsg/ats-paper.git/tech-doku/ats-tech-guide.tex
  34. * use build_txt.sh to generate plaintext output
  35. *
  36. * ATS addresses : proportional solver
  37. *
  38. * The proportional solver ("proportional") distributes the available
  39. * bandwidth fair over all the addresses influenced by the
  40. * preference values. For each available network type an in- and
  41. * outbound quota is configured and the bandwidth available in
  42. * these networks is distributed over the addresses. The solver
  43. * first assigns every addresses the minimum amount of bandwidth
  44. * #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT and then distributes the
  45. * remaining bandwidth available according to the preference
  46. * values. For each peer only a single address gets bandwidth
  47. * assigned and only one address marked as active. The most
  48. * important functionality for the solver is implemented in: *
  49. * find_address_it is an hashmap iterator returning the prefered
  50. * address for an peer * update_quota_per_network distributes
  51. * available bandwidth for a network over active addresses
  52. *
  53. * Changes to addresses automatically have an impact on the the
  54. * bandwidth assigned to other addresses in the same network since
  55. * the solver distributes the remaining bandwidth over the
  56. * addresses in the network. When changes to the addresses occur,
  57. * the solver first performs the changes, like adding or deleting
  58. * addresses, and then updates bandwidth assignment for the
  59. * affected network. Bandwidth assignment is only recalculated on
  60. * demand when an address is requested by a client for a peer or
  61. * when the addresses available have changed or an address changed
  62. * the network it is located in. When the bandwidth assignment has
  63. * changed the callback is called with the new bandwidth
  64. * assignments. The bandwidth distribution for a network is
  65. * recalculated due to: * address suggestion requests * address
  66. * deletions * address switching networks during address update *
  67. * preference changes
  68. *
  69. * 3.1 Data structures used
  70. *
  71. * For each ATS network (e.g. WAN, LAN, loopback) a struct Network
  72. * is used to specify network related information as total adresses
  73. * and active addresses in this network and the configured in- and
  74. * outbound quota. Each network also contains a list of addresses
  75. * added to the solver located in this network. The proportional
  76. * solver uses the addresses' solver_information field to store the
  77. * proportional network it belongs to for each address.
  78. *
  79. * 3.2 Initializing
  80. *
  81. * When the proportional solver is initialized the solver creates a
  82. * new solver handle and initializes the network structures with
  83. * the quotas passed from addresses and returns the handle solver.
  84. *
  85. * 3.3 Adding an address
  86. *
  87. * When a new address is added to the solver using s_add, a lookup
  88. * for the network for this address is done and the address is
  89. * enqueued in in the linked list of the network.
  90. *
  91. * 3.4 Updating an address
  92. *
  93. * The main purpose of address updates is to update the ATS
  94. * information for addresse selection. Important for the proportional
  95. * solver is when an address switches network it is located
  96. * in. This is common because addresses added by transport's
  97. * validation mechanism are commonly located in
  98. * #GNUNET_ATS_NET_UNSPECIFIED. Addresses in validation are located
  99. * in this network type and only if a connection is successful on
  100. * return of payload data transport switches to the real network
  101. * the address is located in. When an address changes networks it
  102. * is first of all removed from the old network using the solver
  103. * API function #GAS_proportional_address_delete() and the network in
  104. * the address struct is updated. A lookup for the respective new
  105. * proportional network is done and stored in the addresse's
  106. * solver_information field. Next the address is re-added to the
  107. * solver using the solver API function
  108. * #GAS_proportional_address_add(). If the address was marked as in
  109. * active, the solver checks if bandwidth is available in the
  110. * network and if yes sets the address to active and updates the
  111. * bandwidth distribution in this network. If no bandwidth is
  112. * available it sets the bandwidth for this address to 0 and tries
  113. * to suggest an alternative address. If an alternative address was
  114. * found, addresses' callback is called for this address.
  115. *
  116. * 3.5 Deleting an address
  117. *
  118. * When an address is removed from the solver, it removes the
  119. * respective address from the network and if the address was
  120. * marked as active, it updates the bandwidth distribution for this
  121. * network.
  122. *
  123. * 3.6 Requesting addresses
  124. *
  125. * When an address is requested for a peer the solver performs a
  126. * lookup for the peer entry in addresses address hashmap and
  127. * selects the best address. The selection of the most suitable
  128. * address is done in the find_address_it hashmap iterator
  129. * described in detail in section 3.7. If no address is returned,
  130. * no address can be suggested at the moment. If the address
  131. * returned is marked as active, the solver can return this
  132. * address. If the address is not marked as active, the solver
  133. * checks if another address belongign to this peer is marked as
  134. * active and marks the address as inactive, updates the bandwidth
  135. * for this address to 0, call the bandwidth changed callback for
  136. * this address due to the change and updates quota assignment for
  137. * the addresse's network. the now in-active address is belonging
  138. * to. The solver marks the new address as active and updates the
  139. * bandwidth assignment for this network.
  140. *
  141. * 3.7 Choosing addresses
  142. *
  143. * Choosing the best possible address for suggestion is done by
  144. * iterating over all addresses of a peer stored in addresses'
  145. * hashmap and using the hashmap iterator find_address_it to select
  146. * the best available address. Several checks are done when an
  147. * address is selected. First if this address is currently blocked
  148. * by addresses from being suggested. An address is blocked for the
  149. * duration of #ATS_BLOCKING_DELTA when it is suggested to
  150. * transport. Next it is checked if at least
  151. * #GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT bytes bandwidth is available
  152. * in the addresse's network, because suggesting an address without
  153. * bandwidth does not make sense. This also ensures that all active
  154. * addresses in this network get at least the minimum amount of
  155. * bandwidth assigned. In the next step the solver ensures that for
  156. * tcp connections inbound connections are prefered over outbound
  157. * connections. In the next stet the solver ensures that
  158. * connections are prefered in the following order: * connections
  159. * are already established and have bandwidth assigned *
  160. * connections with a shorter distance * connectes have a shorter
  161. * latency
  162. *
  163. * 3.8 Changing preferences
  164. *
  165. * 3.9 Shutdown
  166. *
  167. * During shutdown all network entries and aging processes are
  168. * destroyed and freed.
  169. *
  170. *
  171. * OLD DOCUMENTATION
  172. *
  173. * This solver assigns in and outbound bandwidth equally for all
  174. * addresses in specific network type (WAN, LAN) based on configured
  175. * in and outbound quota for this network.
  176. *
  177. * The solver is notified by addresses about changes to the addresses
  178. * and recalculates the bandwith assigned if required. The solver
  179. * notifies addresses by calling the GAS_bandwidth_changed_cb
  180. * callback.
  181. *
  182. * - Initialization
  183. *
  184. *
  185. *
  186. *
  187. * For each peer only a single is selected and marked as "active" in the address
  188. * struct.
  189. *
  190. * E.g.:
  191. *
  192. * You have the networks WAN and LAN and quotas
  193. * WAN_TOTAL_IN, WAN_TOTAL_OUT
  194. * LAN_TOTAL_IN, LAN_TOTAL_OUT
  195. *
  196. * If you have x addresses in the network segment LAN, the quotas are
  197. * QUOTA_PER_ADDRESS = LAN_TOTAL_OUT / x
  198. *
  199. * Quotas are automatically recalculated and reported back when addresses are
  200. * - requested
  201. *
  202. */
  203. #define PROPORTIONALITY_FACTOR 2.0
  204. /**
  205. * A handle for the proportional solver
  206. */
  207. struct GAS_PROPORTIONAL_Handle
  208. {
  209. /**
  210. * Our execution environment.
  211. */
  212. struct GNUNET_ATS_PluginEnvironment *env;
  213. /**
  214. * Hashmap containing all valid addresses
  215. */
  216. struct GNUNET_CONTAINER_MultiPeerMap *addresses;
  217. /**
  218. * Pending address requests
  219. */
  220. struct GNUNET_CONTAINER_MultiPeerMap *requests;
  221. /**
  222. * Bulk lock
  223. */
  224. int bulk_lock;
  225. /**
  226. * Number of changes while solver was locked
  227. */
  228. int bulk_requests;
  229. /**
  230. * Total number of addresses for solver
  231. */
  232. unsigned int total_addresses;
  233. /**
  234. * Number of active addresses for solver
  235. */
  236. unsigned int active_addresses;
  237. /**
  238. * Networks array
  239. */
  240. struct Network *network_entries;
  241. /**
  242. * Number of networks
  243. */
  244. unsigned int network_count;
  245. /**
  246. * Proportionality factor
  247. */
  248. double prop_factor;
  249. /**
  250. * Stability factor
  251. */
  252. double stability_factor;
  253. };
  254. /**
  255. * Representation of a network
  256. */
  257. struct Network
  258. {
  259. /**
  260. * ATS network type
  261. */
  262. enum GNUNET_ATS_Network_Type type;
  263. /**
  264. * Network description
  265. */
  266. const char *desc;
  267. /**
  268. * Total inbound quota
  269. */
  270. unsigned long long total_quota_in;
  271. /**
  272. * Total outbound quota
  273. */
  274. unsigned long long total_quota_out;
  275. /**
  276. * Number of active addresses for this network
  277. */
  278. unsigned int active_addresses;
  279. /**
  280. * Number of total addresses for this network
  281. */
  282. unsigned int total_addresses;
  283. /**
  284. * String for statistics total addresses
  285. */
  286. char *stat_total;
  287. /**
  288. * String for statistics active addresses
  289. */
  290. char *stat_active;
  291. /**
  292. * Linked list of addresses in this network: head
  293. */
  294. struct AddressWrapper *head;
  295. /**
  296. * Linked list of addresses in this network: tail
  297. */
  298. struct AddressWrapper *tail;
  299. };
  300. /**
  301. * Address information stored in the solver
  302. */
  303. struct AddressSolverInformation
  304. {
  305. /**
  306. * Network scope this address is in
  307. */
  308. struct Network *network;
  309. /**
  310. * Inbound quota
  311. */
  312. uint32_t calculated_quota_in;
  313. /**
  314. * Outbound quota
  315. */
  316. uint32_t calculated_quota_out;
  317. /**
  318. * When was this address activated
  319. */
  320. struct GNUNET_TIME_Absolute activated;
  321. };
  322. /**
  323. * Wrapper for addresses to store them in network's linked list
  324. */
  325. struct AddressWrapper
  326. {
  327. /**
  328. * Next in DLL
  329. */
  330. struct AddressWrapper *next;
  331. /**
  332. * Previous in DLL
  333. */
  334. struct AddressWrapper *prev;
  335. /**
  336. * The address
  337. */
  338. struct ATS_Address *addr;
  339. };
  340. /**
  341. * Function used to unload the plugin.
  342. *
  343. * @param cls return value from #libgnunet_plugin_ats_proportional_init()
  344. */
  345. void *
  346. libgnunet_plugin_ats_proportional_done (void *cls)
  347. {
  348. struct GNUNET_ATS_SolverFunctions *sf = cls;
  349. struct GAS_PROPORTIONAL_Handle *s = sf->cls;
  350. struct AddressWrapper *cur;
  351. struct AddressWrapper *next;
  352. int c;
  353. for (c = 0; c < s->network_count; c++)
  354. {
  355. if (s->network_entries[c].total_addresses > 0)
  356. {
  357. LOG(GNUNET_ERROR_TYPE_DEBUG,
  358. "Had %u addresses for network `%s' not deleted during shutdown\n",
  359. s->network_entries[c].total_addresses, s->network_entries[c].desc);
  360. //GNUNET_break(0);
  361. }
  362. if (s->network_entries[c].active_addresses > 0)
  363. {
  364. LOG(GNUNET_ERROR_TYPE_DEBUG,
  365. "Had %u active addresses for network `%s' not deleted during shutdown\n",
  366. s->network_entries[c].active_addresses, s->network_entries[c].desc);
  367. //GNUNET_break(0);
  368. }
  369. next = s->network_entries[c].head;
  370. while (NULL != (cur = next))
  371. {
  372. next = cur->next;
  373. GNUNET_CONTAINER_DLL_remove(s->network_entries[c].head,
  374. s->network_entries[c].tail, cur);
  375. GNUNET_free_non_null (cur->addr->solver_information);
  376. GNUNET_free(cur);
  377. }
  378. GNUNET_free(s->network_entries[c].stat_total);
  379. GNUNET_free(s->network_entries[c].stat_active);
  380. }
  381. if (s->total_addresses > 0)
  382. {
  383. LOG(GNUNET_ERROR_TYPE_DEBUG,
  384. "Had %u addresses not deleted during shutdown\n", s->total_addresses);
  385. // GNUNET_break(0);
  386. }
  387. if (s->active_addresses > 0)
  388. {
  389. LOG(GNUNET_ERROR_TYPE_DEBUG,
  390. "Had %u active addresses not deleted during shutdown\n",
  391. s->active_addresses);
  392. // GNUNET_break (0);
  393. }
  394. GNUNET_free (s->network_entries);
  395. GNUNET_CONTAINER_multipeermap_destroy (s->requests);
  396. GNUNET_free (s);
  397. return NULL;
  398. }
  399. /**
  400. * Test if bandwidth is available in this network to add an additional address
  401. *
  402. * @param net the network type to update
  403. * @return #GNUNET_YES or #GNUNET_NO
  404. */
  405. static int
  406. is_bandwidth_available_in_network (struct Network *net)
  407. {
  408. GNUNET_assert(NULL != net);
  409. unsigned int na = net->active_addresses + 1;
  410. uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
  411. if (((net->total_quota_in / na) > min_bw)
  412. && ((net->total_quota_out / na) > min_bw))
  413. {
  414. LOG(GNUNET_ERROR_TYPE_DEBUG,
  415. "Enough bandwidth available for %u active addresses in network `%s'\n",
  416. na, net->desc);
  417. return GNUNET_YES;
  418. }
  419. LOG(GNUNET_ERROR_TYPE_DEBUG,
  420. "Not enough bandwidth available for %u active addresses in network `%s'\n",
  421. na, net->desc);
  422. return GNUNET_NO;
  423. }
  424. /**
  425. * Update bandwidth assigned to peers in this network
  426. *
  427. * @param s the solver handle
  428. * @param net the network type to update
  429. * this address
  430. */
  431. static void
  432. distribute_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
  433. struct Network *net)
  434. {
  435. struct AddressSolverInformation *asi;
  436. struct AddressWrapper *cur_address;
  437. unsigned long long remaining_quota_in = 0;
  438. unsigned long long quota_out_used = 0;
  439. unsigned long long remaining_quota_out = 0;
  440. unsigned long long quota_in_used = 0;
  441. int count_addresses;
  442. uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__);
  443. double relative_peer_prefence;
  444. double sum_relative_peer_prefences; /* Important: has to be double not float due to precision */
  445. double cur_pref; /* Important: has to be double not float due to precision */
  446. double peer_weight;
  447. double total_weight;
  448. const double *peer_relative_prefs = NULL; /* Important: has to be double not float due to precision */
  449. uint32_t assigned_quota_in = 0;
  450. uint32_t assigned_quota_out = 0;
  451. LOG (GNUNET_ERROR_TYPE_INFO,
  452. "Recalculate quota for network type `%s' for %u addresses (in/out): %llu/%llu \n",
  453. net->desc,
  454. net->active_addresses,
  455. net->total_quota_in,
  456. net->total_quota_in);
  457. if (net->active_addresses == 0)
  458. {
  459. return; /* no addresses to update */
  460. }
  461. /* Idea
  462. * Assign every peer in network minimum Bandwidth
  463. * Distribute bandwidth left according to preference
  464. */
  465. if ((net->active_addresses * min_bw) > net->total_quota_in)
  466. {
  467. GNUNET_break(0);
  468. return;
  469. }
  470. if ((net->active_addresses * min_bw) > net->total_quota_out)
  471. {
  472. GNUNET_break(0);
  473. return;
  474. }
  475. remaining_quota_in = net->total_quota_in - (net->active_addresses * min_bw);
  476. remaining_quota_out = net->total_quota_out - (net->active_addresses * min_bw);
  477. LOG(GNUNET_ERROR_TYPE_DEBUG, "Remaining bandwidth : (in/out): %llu/%llu \n",
  478. remaining_quota_in, remaining_quota_out);
  479. sum_relative_peer_prefences = 0.0;
  480. /* Calculate sum of relative preference for active addresses in this network */
  481. count_addresses = 0;
  482. for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
  483. {
  484. if (GNUNET_YES != cur_address->addr->active)
  485. continue;
  486. GNUNET_assert( NULL != (peer_relative_prefs = s->env->get_preferences (s->env->cls,
  487. &cur_address->addr->peer)));
  488. relative_peer_prefence = 0.0;
  489. relative_peer_prefence += peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
  490. sum_relative_peer_prefences += relative_peer_prefence;
  491. count_addresses ++;
  492. }
  493. if (count_addresses != net->active_addresses)
  494. {
  495. GNUNET_break (0);
  496. LOG (GNUNET_ERROR_TYPE_WARNING,
  497. "%s: Counted %u active addresses, but network says to have %u active addresses\n",
  498. net->desc, count_addresses, net->active_addresses);
  499. for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
  500. {
  501. if (GNUNET_YES != cur_address->addr->active)
  502. continue;
  503. LOG (GNUNET_ERROR_TYPE_WARNING,
  504. "Active: `%s' `%s' length %u\n",
  505. GNUNET_i2s (&cur_address->addr->peer),
  506. cur_address->addr->plugin,
  507. cur_address->addr->addr_len);
  508. }
  509. }
  510. LOG (GNUNET_ERROR_TYPE_INFO,
  511. "Total relative preference %.3f for %u addresses in network %s\n",
  512. sum_relative_peer_prefences, net->active_addresses, net->desc);
  513. for (cur_address = net->head; NULL != cur_address; cur_address = cur_address->next)
  514. {
  515. if (GNUNET_YES == cur_address->addr->active)
  516. {
  517. GNUNET_assert( NULL != (peer_relative_prefs =
  518. s->env->get_preferences (s->env->cls,
  519. &cur_address->addr->peer)));
  520. cur_pref = peer_relative_prefs[GNUNET_ATS_PREFERENCE_BANDWIDTH];
  521. total_weight = net->active_addresses +
  522. s->prop_factor * sum_relative_peer_prefences;
  523. peer_weight = (1.0 + (s->prop_factor * cur_pref));
  524. assigned_quota_in = min_bw
  525. + ((peer_weight / total_weight) * remaining_quota_in);
  526. assigned_quota_out = min_bw
  527. + ((peer_weight / total_weight) * remaining_quota_out);
  528. LOG (GNUNET_ERROR_TYPE_INFO,
  529. "New quota for peer `%s' with weight (cur/total) %.3f/%.3f (in/out): %llu / %llu\n",
  530. GNUNET_i2s (&cur_address->addr->peer), peer_weight, total_weight,
  531. assigned_quota_in, assigned_quota_out);
  532. }
  533. else
  534. {
  535. assigned_quota_in = 0;
  536. assigned_quota_out = 0;
  537. }
  538. quota_in_used += assigned_quota_in;
  539. quota_out_used += assigned_quota_out;
  540. /* Prevent overflow due to rounding errors */
  541. if (assigned_quota_in > UINT32_MAX)
  542. assigned_quota_in = UINT32_MAX;
  543. if (assigned_quota_out > UINT32_MAX)
  544. assigned_quota_out = UINT32_MAX;
  545. /* Compare to current bandwidth assigned */
  546. asi = cur_address->addr->solver_information;
  547. asi->calculated_quota_in = assigned_quota_in;
  548. asi->calculated_quota_out = assigned_quota_out;
  549. }
  550. LOG(GNUNET_ERROR_TYPE_DEBUG,
  551. "Total bandwidth assigned is (in/out): %llu /%llu\n", quota_in_used,
  552. quota_out_used);
  553. if (quota_out_used > net->total_quota_out + 1) /* +1 is required due to rounding errors */
  554. {
  555. LOG(GNUNET_ERROR_TYPE_ERROR,
  556. "Total outbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
  557. net->active_addresses, quota_out_used, net->total_quota_out);
  558. }
  559. if (quota_in_used > net->total_quota_in + 1) /* +1 is required due to rounding errors */
  560. {
  561. LOG(GNUNET_ERROR_TYPE_ERROR,
  562. "Total inbound bandwidth assigned is larger than allowed (used/allowed) for %u active addresses: %llu / %llu\n",
  563. net->active_addresses, quota_in_used, net->total_quota_in);
  564. }
  565. }
  566. /**
  567. * Context for finding the best address* Linked list of addresses in this network: head
  568. */
  569. struct FindBestAddressCtx
  570. {
  571. /**
  572. * The solver handle
  573. */
  574. struct GAS_PROPORTIONAL_Handle *s;
  575. /**
  576. * The currently best address
  577. */
  578. struct ATS_Address *best;
  579. };
  580. /**
  581. * Find index of a ATS property type in the array.
  582. */
  583. static int
  584. find_property_index (uint32_t type)
  585. {
  586. int existing_types[] = GNUNET_ATS_QualityProperties;
  587. int c;
  588. for (c = 0; c < GNUNET_ATS_QualityPropertiesCount; c++)
  589. if (existing_types[c] == type)
  590. return c;
  591. return GNUNET_SYSERR;
  592. }
  593. /**
  594. * Find a "good" address to use for a peer by iterating over the addresses for this peer.
  595. * If we already have an existing address, we stick to it.
  596. * Otherwise, we pick by lowest distance and then by lowest latency.
  597. *
  598. * @param cls the `struct FindBestAddressCtx *' where we store the result
  599. * @param key unused
  600. * @param value another `struct ATS_Address*` to consider using
  601. * @return #GNUNET_OK (continue to iterate)
  602. */
  603. static int
  604. find_best_address_it (void *cls,
  605. const struct GNUNET_PeerIdentity *key,
  606. void *value)
  607. {
  608. struct FindBestAddressCtx *ctx = cls;
  609. struct ATS_Address *current = value;
  610. struct ATS_Address *current_best = current;
  611. struct AddressSolverInformation *asi;
  612. struct GNUNET_TIME_Relative active_time;
  613. struct GNUNET_TIME_Relative min_active_time;
  614. const double *norm_prop_cur;
  615. const double *norm_prop_best;
  616. double best_delay;
  617. double best_distance;
  618. double cur_delay;
  619. double cur_distance;
  620. int index;
  621. current_best = NULL;
  622. asi = current->solver_information;
  623. if (NULL == asi)
  624. {
  625. GNUNET_break (0);
  626. return GNUNET_OK;
  627. }
  628. if (GNUNET_NO == is_bandwidth_available_in_network (asi->network))
  629. {
  630. return GNUNET_OK; /* There's no bandwidth available in this network */
  631. }
  632. if (NULL != ctx->best)
  633. {
  634. /* Compare current addresses with denominated 'best' address */
  635. current_best = ctx->best;
  636. }
  637. else
  638. {
  639. /* We do not have a 'best' address so take this address */
  640. LOG (GNUNET_ERROR_TYPE_DEBUG,
  641. "Setting initial address %p\n",
  642. current);
  643. current_best = current;
  644. goto end;
  645. }
  646. if (GNUNET_YES == current->active)
  647. {
  648. GNUNET_assert (asi->activated.abs_value_us != GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us);
  649. active_time = GNUNET_TIME_absolute_get_duration (asi->activated);
  650. min_active_time.rel_value_us = ((double) GNUNET_TIME_UNIT_SECONDS.rel_value_us) *
  651. ctx->s->stability_factor;
  652. if (active_time.rel_value_us <= min_active_time.rel_value_us)
  653. {
  654. /* Keep active address for stability reasons */
  655. ctx->best = current;
  656. return GNUNET_NO;
  657. }
  658. }
  659. /* Now compare ATS information */
  660. norm_prop_cur = ctx->s->env->get_property (ctx->s->env->cls,
  661. current);
  662. index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
  663. cur_distance = norm_prop_cur[index];
  664. index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
  665. cur_delay = norm_prop_cur[index];
  666. norm_prop_best = ctx->s->env->get_property (ctx->s->env->cls,
  667. ctx->best);
  668. index = find_property_index (GNUNET_ATS_QUALITY_NET_DISTANCE);
  669. best_distance = norm_prop_best[index];
  670. index = find_property_index (GNUNET_ATS_QUALITY_NET_DELAY);
  671. best_delay = norm_prop_best[index];
  672. /* user shorter distance */
  673. if (cur_distance < best_distance)
  674. {
  675. if (GNUNET_NO == ctx->best->active)
  676. {
  677. current_best = current; /* Use current */
  678. }
  679. else if ((best_distance / cur_distance) > ctx->s->stability_factor)
  680. {
  681. /* Best and active address performs worse */
  682. current_best = current;
  683. }
  684. }
  685. else
  686. {
  687. /* Use current best */
  688. current_best = ctx->best;
  689. }
  690. /* User connection with less delay */
  691. if (cur_delay < best_delay)
  692. {
  693. if (GNUNET_NO == ctx->best->active)
  694. {
  695. current_best = current; /* Use current */
  696. }
  697. else if ((best_delay / cur_delay) > ctx->s->stability_factor)
  698. {
  699. /* Best and active address performs worse */
  700. current_best = current;
  701. }
  702. }
  703. else
  704. {
  705. /* Use current best */
  706. current_best = ctx->best;
  707. }
  708. end:
  709. ctx->best = current_best;
  710. return GNUNET_OK;
  711. }
  712. /**
  713. * Find the currently best address for a peer from the set of addresses available
  714. * or return NULL of no address is available
  715. *
  716. * @param s the proportional handle
  717. * @param addresses the address hashmap
  718. * @param id the peer id
  719. * @return the address or NULL
  720. */
  721. struct ATS_Address *
  722. get_best_address (struct GAS_PROPORTIONAL_Handle *s,
  723. struct GNUNET_CONTAINER_MultiPeerMap *addresses,
  724. const struct GNUNET_PeerIdentity *id)
  725. {
  726. struct FindBestAddressCtx fba_ctx;
  727. fba_ctx.best = NULL;
  728. fba_ctx.s = s;
  729. GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
  730. id,
  731. &find_best_address_it,
  732. &fba_ctx);
  733. return fba_ctx.best;
  734. }
  735. /**
  736. * Hashmap Iterator to find current active address for peer
  737. *
  738. * @param cls last active address
  739. * @param key peer's key
  740. * @param value address to check
  741. * @return #GNUNET_NO on double active address else #GNUNET_YES;
  742. */
  743. static int
  744. get_active_address_it (void *cls,
  745. const struct GNUNET_PeerIdentity *key,
  746. void *value)
  747. {
  748. struct ATS_Address **dest = cls;
  749. struct ATS_Address *aa = (struct ATS_Address *) value;
  750. LOG (GNUNET_ERROR_TYPE_INFO,
  751. "Checking address %p\n", aa);
  752. if (GNUNET_YES == aa->active)
  753. {
  754. LOG (GNUNET_ERROR_TYPE_INFO,
  755. "Address %p is active\n", aa);
  756. if (NULL != (*dest))
  757. {
  758. /* should never happen */
  759. LOG (GNUNET_ERROR_TYPE_ERROR,
  760. "Multiple active addresses for peer `%s'\n",
  761. GNUNET_i2s (&aa->peer));
  762. GNUNET_break(0);
  763. return GNUNET_NO;
  764. }
  765. (*dest) = aa;
  766. }
  767. return GNUNET_OK;
  768. }
  769. /**
  770. * Find current active address for peer
  771. *
  772. * @param solver the solver handle
  773. * @param addresses the address set
  774. * @param peer the peer
  775. * @return active address or NULL
  776. */
  777. static struct ATS_Address *
  778. get_active_address (void *solver,
  779. const struct GNUNET_CONTAINER_MultiPeerMap * addresses,
  780. const struct GNUNET_PeerIdentity *peer)
  781. {
  782. static struct ATS_Address *dest;
  783. dest = NULL;
  784. GNUNET_CONTAINER_multipeermap_get_multiple (addresses, peer,
  785. &get_active_address_it,
  786. &dest);
  787. return dest;
  788. }
  789. /**
  790. * Lookup network struct by type
  791. *
  792. * @param s the solver handle
  793. * @param type the network type
  794. * @return the network struct
  795. */
  796. static struct Network *
  797. get_network (struct GAS_PROPORTIONAL_Handle *s,
  798. enum GNUNET_ATS_Network_Type type)
  799. {
  800. if (type >= s->env->network_count)
  801. {
  802. GNUNET_break (0);
  803. return NULL;
  804. }
  805. return &s->network_entries[type];
  806. }
  807. /**
  808. * Increase address count in network
  809. *
  810. * @param s the solver handle
  811. * @param net the network type
  812. * @param total increase total addresses
  813. * @param active increase active addresses
  814. */
  815. static void
  816. address_increment (struct GAS_PROPORTIONAL_Handle *s,
  817. struct Network *net,
  818. int total,
  819. int active)
  820. {
  821. if (GNUNET_YES == total)
  822. {
  823. s->total_addresses++;
  824. net->total_addresses++;
  825. GNUNET_STATISTICS_update (s->env->stats,
  826. "# ATS addresses total", 1, GNUNET_NO);
  827. GNUNET_STATISTICS_update (s->env->stats,
  828. net->stat_total, 1, GNUNET_NO);
  829. }
  830. if (GNUNET_YES == active)
  831. {
  832. net->active_addresses++;
  833. s->active_addresses++;
  834. GNUNET_STATISTICS_update (s->env->stats,
  835. "# ATS active addresses total", 1,
  836. GNUNET_NO);
  837. GNUNET_STATISTICS_update (s->env->stats,
  838. net->stat_active, 1, GNUNET_NO);
  839. }
  840. }
  841. /**
  842. * Decrease address count in network
  843. *
  844. * @param s the solver handle
  845. * @param net the network type
  846. * @param total decrease total addresses
  847. * @param active decrease active addresses
  848. */
  849. static int
  850. addresse_decrement (struct GAS_PROPORTIONAL_Handle *s,
  851. struct Network *net,
  852. int total,
  853. int active)
  854. {
  855. int res = GNUNET_OK;
  856. if (GNUNET_YES == total)
  857. {
  858. if (s->total_addresses < 1)
  859. {
  860. GNUNET_break(0);
  861. res = GNUNET_SYSERR;
  862. }
  863. else
  864. {
  865. s->total_addresses--;
  866. GNUNET_STATISTICS_update (s->env->stats,
  867. "# ATS addresses total", -1,
  868. GNUNET_NO);
  869. }
  870. if (net->total_addresses < 1)
  871. {
  872. GNUNET_break(0);
  873. res = GNUNET_SYSERR;
  874. }
  875. else
  876. {
  877. net->total_addresses--;
  878. GNUNET_STATISTICS_update (s->env->stats,
  879. net->stat_total, -1, GNUNET_NO);
  880. }
  881. }
  882. if (GNUNET_YES == active)
  883. {
  884. if (net->active_addresses < 1)
  885. {
  886. GNUNET_break(0);
  887. res = GNUNET_SYSERR;
  888. }
  889. else
  890. {
  891. net->active_addresses--;
  892. GNUNET_STATISTICS_update (s->env->stats,
  893. net->stat_active, -1, GNUNET_NO);
  894. }
  895. if (s->active_addresses < 1)
  896. {
  897. GNUNET_break(0);
  898. res = GNUNET_SYSERR;
  899. }
  900. else
  901. {
  902. s->active_addresses--;
  903. GNUNET_STATISTICS_update (s->env->stats,
  904. "# ATS addresses total", -1,
  905. GNUNET_NO);
  906. }
  907. }
  908. return res;
  909. }
  910. /**
  911. * Compares addresses
  912. *
  913. * @param a address a
  914. * @param b address b
  915. * @return GNUNET_YES if equal, GNUNET_NO else
  916. */
  917. static int
  918. address_eq (struct ATS_Address *a, struct ATS_Address *b)
  919. {
  920. GNUNET_assert (NULL != a);
  921. GNUNET_assert (NULL != b);
  922. if (0 != strcmp(a->plugin, b->plugin))
  923. return GNUNET_NO;
  924. if (a->addr_len != b->addr_len)
  925. return GNUNET_NO;
  926. if (0 != memcmp (a->addr, b->addr, b->addr_len))
  927. return GNUNET_NO;
  928. if (a->session_id != b->session_id)
  929. return GNUNET_NO;
  930. return GNUNET_YES;
  931. }
  932. /**
  933. * Notify bandwidth changes to addresses
  934. *
  935. * @param s solver handle
  936. * @param net the network to propagate changes in
  937. */
  938. static void
  939. propagate_bandwidth (struct GAS_PROPORTIONAL_Handle *s,
  940. struct Network *net)
  941. {
  942. struct AddressWrapper *cur;
  943. struct AddressSolverInformation *asi;
  944. for (cur = net->head; NULL != cur; cur = cur->next)
  945. {
  946. asi = cur->addr->solver_information;
  947. if ( (cur->addr->assigned_bw_in != asi->calculated_quota_in) ||
  948. (cur->addr->assigned_bw_out != asi->calculated_quota_out) )
  949. {
  950. cur->addr->assigned_bw_in = asi->calculated_quota_in;
  951. cur->addr->assigned_bw_out = asi->calculated_quota_out;
  952. /* Reset for next iteration */
  953. asi->calculated_quota_in = 0;
  954. asi->calculated_quota_out = 0;
  955. LOG (GNUNET_ERROR_TYPE_DEBUG,
  956. "Bandwidth for %s address %p for peer `%s' changed to %u/%u\n",
  957. (GNUNET_NO == cur->addr->active) ? "inactive" : "active",
  958. cur->addr,
  959. GNUNET_i2s (&cur->addr->peer),
  960. cur->addr->assigned_bw_in,
  961. cur->addr->assigned_bw_out);
  962. /* Notify on change */
  963. if ((GNUNET_YES == cur->addr->active))
  964. {
  965. s->env->bandwidth_changed_cb (s->env->cls,
  966. cur->addr);
  967. }
  968. }
  969. }
  970. }
  971. /**
  972. * Distribibute bandwidth
  973. *
  974. * @param s the solver handle
  975. * @param n the network, can be NULL for all network
  976. */
  977. static void
  978. distribute_bandwidth_in_network (struct GAS_PROPORTIONAL_Handle *s,
  979. struct Network *n)
  980. {
  981. if (GNUNET_YES == s->bulk_lock)
  982. {
  983. s->bulk_requests++;
  984. return;
  985. }
  986. if (NULL != n)
  987. {
  988. LOG (GNUNET_ERROR_TYPE_INFO,
  989. "Redistributing bandwidth in network %s with %u active and %u total addresses\n",
  990. GNUNET_ATS_print_network_type(n->type),
  991. n->active_addresses, n->total_addresses);
  992. if (NULL != s->env->info_cb)
  993. s->env->info_cb(s->env->cls, GAS_OP_SOLVE_START,
  994. GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
  995. /* Distribute */
  996. distribute_bandwidth(s, n);
  997. if (NULL != s->env->info_cb)
  998. s->env->info_cb(s->env->cls, GAS_OP_SOLVE_STOP,
  999. GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
  1000. if (NULL != s->env->info_cb)
  1001. s->env->info_cb(s->env->cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
  1002. GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
  1003. /* Do propagation */
  1004. propagate_bandwidth (s, n);
  1005. if (NULL != s->env->info_cb)
  1006. s->env->info_cb(s->env->cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
  1007. GAS_STAT_SUCCESS, GAS_INFO_PROP_SINGLE);
  1008. }
  1009. else
  1010. {
  1011. int i;
  1012. if (NULL != s->env->info_cb)
  1013. s->env->info_cb(s->env->cls, GAS_OP_SOLVE_START,
  1014. GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
  1015. for (i = 0; i < s->network_count; i++)
  1016. {
  1017. /* Distribute */
  1018. distribute_bandwidth(s, &s->network_entries[i]);
  1019. }
  1020. if (NULL != s->env->info_cb)
  1021. s->env->info_cb(s->env->cls, GAS_OP_SOLVE_STOP,
  1022. GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
  1023. if (NULL != s->env->info_cb)
  1024. s->env->info_cb(s->env->cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START,
  1025. GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
  1026. for (i = 0; i < s->network_count; i++)
  1027. {
  1028. /* Do propagation */
  1029. propagate_bandwidth(s, &s->network_entries[i]);
  1030. }
  1031. if (NULL != s->env->info_cb)
  1032. s->env->info_cb(s->env->cls, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP,
  1033. GAS_STAT_SUCCESS, GAS_INFO_PROP_ALL);
  1034. }
  1035. }
  1036. /**
  1037. * Update active address for a peer:
  1038. * Check if active address exists and what the best address is, if addresses
  1039. * are different switch
  1040. *
  1041. * @param s solver handle
  1042. * @param peer the peer to check
  1043. * return the new address or NULL if no update was performed
  1044. */
  1045. static struct ATS_Address *
  1046. update_active_address (struct GAS_PROPORTIONAL_Handle *s,
  1047. const struct GNUNET_PeerIdentity *peer)
  1048. {
  1049. struct ATS_Address *best_address;
  1050. struct ATS_Address *current_address;
  1051. struct AddressSolverInformation *asi;
  1052. struct Network *net;
  1053. LOG (GNUNET_ERROR_TYPE_INFO,
  1054. "Updating active address for peer `%s'\n",
  1055. GNUNET_i2s (peer));
  1056. /* Find active address */
  1057. current_address = get_active_address (s,
  1058. s->addresses,
  1059. peer);
  1060. LOG (GNUNET_ERROR_TYPE_INFO,
  1061. "Peer `%s' has active address %p\n",
  1062. GNUNET_i2s (peer),
  1063. current_address);
  1064. /* Find best address */
  1065. best_address = get_best_address (s,
  1066. s->addresses,
  1067. peer);
  1068. LOG (GNUNET_ERROR_TYPE_INFO,
  1069. "Peer `%s' has best address %p\n",
  1070. GNUNET_i2s (peer),
  1071. best_address);
  1072. if (NULL != current_address)
  1073. {
  1074. if ( (NULL == best_address) ||
  1075. ( (NULL != best_address) &&
  1076. (GNUNET_NO == address_eq (current_address,
  1077. best_address)) ) )
  1078. {
  1079. /* We switch to a new address (or to none),
  1080. mark old address as inactive */
  1081. LOG (GNUNET_ERROR_TYPE_INFO,
  1082. "Disabling previous %s address %p for peer `%s'\n",
  1083. (GNUNET_NO == current_address->active) ? "inactive" : "active",
  1084. current_address,
  1085. GNUNET_i2s (peer));
  1086. asi = current_address->solver_information;
  1087. GNUNET_assert (NULL != asi);
  1088. net = asi->network;
  1089. asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
  1090. current_address->active = GNUNET_NO; /* No active any longer */
  1091. current_address->assigned_bw_in = 0; /* no bandwidth assigned */
  1092. current_address->assigned_bw_out = 0; /* no bandwidth assigned */
  1093. if (GNUNET_SYSERR == addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
  1094. GNUNET_break(0);
  1095. /* Update network of previous address */
  1096. distribute_bandwidth_in_network (s, net);
  1097. }
  1098. if (NULL == best_address)
  1099. {
  1100. /* We previously had an active address, but now we cannot suggest one
  1101. * Therefore we have to disconnect the peer */
  1102. LOG (GNUNET_ERROR_TYPE_INFO,
  1103. "Disconnecting peer `%s' with previous address %p\n",
  1104. GNUNET_i2s (peer),
  1105. current_address);
  1106. s->env->bandwidth_changed_cb (s->env->cls,
  1107. current_address);
  1108. }
  1109. }
  1110. if (NULL == best_address)
  1111. {
  1112. LOG (GNUNET_ERROR_TYPE_INFO,
  1113. "Cannot suggest address for peer `%s'\n",
  1114. GNUNET_i2s (peer));
  1115. return NULL;
  1116. }
  1117. LOG (GNUNET_ERROR_TYPE_INFO,
  1118. "Suggesting new address %p for peer `%s'\n",
  1119. best_address,
  1120. GNUNET_i2s (peer));
  1121. if ( (NULL != current_address) &&
  1122. (GNUNET_YES == address_eq (best_address, current_address)) )
  1123. {
  1124. GNUNET_break (GNUNET_NO != current_address->active);
  1125. return best_address; /* Same same */
  1126. }
  1127. asi = best_address->solver_information;
  1128. GNUNET_assert (NULL != asi);
  1129. net = asi->network;
  1130. /* Mark address as active */
  1131. asi->activated = GNUNET_TIME_absolute_get ();
  1132. best_address->active = GNUNET_YES;
  1133. address_increment (s, net, GNUNET_NO, GNUNET_YES);
  1134. LOG (GNUNET_ERROR_TYPE_INFO,
  1135. "Address %p for peer `%s' is now active\n",
  1136. best_address,
  1137. GNUNET_i2s (peer));
  1138. /* Distribute bandwidth */
  1139. distribute_bandwidth_in_network (s, net);
  1140. return best_address;
  1141. }
  1142. /**
  1143. * Changes the preferences for a peer in the problem
  1144. *
  1145. * @param solver the solver handle
  1146. * @param peer the peer to change the preference for
  1147. * @param kind the kind to change the preference
  1148. * @param pref_rel the normalized preference value for this kind over all clients
  1149. */
  1150. static void
  1151. GAS_proportional_address_change_preference (void *solver,
  1152. const struct GNUNET_PeerIdentity *peer,
  1153. enum GNUNET_ATS_PreferenceKind kind,
  1154. double pref_rel)
  1155. {
  1156. struct GAS_PROPORTIONAL_Handle *s = solver;
  1157. struct ATS_Address *best_address;
  1158. struct ATS_Address *active_address;
  1159. struct AddressSolverInformation *asi;
  1160. if (GNUNET_NO ==
  1161. GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
  1162. return; /* Peer is not requested */
  1163. /* This peer is requested, find best address */
  1164. active_address = get_active_address (s, s->addresses, peer);
  1165. best_address = update_active_address (s, peer);
  1166. if ((NULL != best_address) && ((NULL != active_address) &&
  1167. (GNUNET_YES == address_eq (active_address, best_address))))
  1168. {
  1169. asi = best_address->solver_information;
  1170. GNUNET_assert (NULL != asi);
  1171. /* We sticked to the same address, therefore redistribute */
  1172. distribute_bandwidth_in_network (s, asi->network);
  1173. }
  1174. }
  1175. /**
  1176. * Get application feedback for a peer
  1177. *
  1178. * @param solver the solver handle
  1179. * @param application the application
  1180. * @param peer the peer to change the preference for
  1181. * @param scope the time interval for this feedback: [now - scope .. now]
  1182. * @param kind the kind to change the preference
  1183. * @param score the score
  1184. */
  1185. static void
  1186. GAS_proportional_address_preference_feedback (void *solver,
  1187. struct GNUNET_SERVER_Client *application,
  1188. const struct GNUNET_PeerIdentity *peer,
  1189. const struct GNUNET_TIME_Relative scope,
  1190. enum GNUNET_ATS_PreferenceKind kind,
  1191. double score)
  1192. {
  1193. struct GAS_PROPORTIONAL_Handle *s = solver;
  1194. GNUNET_assert(NULL != peer);
  1195. GNUNET_assert(NULL != s);
  1196. }
  1197. /**
  1198. * Get the preferred address for a specific peer
  1199. *
  1200. * @param solver the solver handle
  1201. * @param peer the identity of the peer
  1202. * @return best address
  1203. */
  1204. static const struct ATS_Address *
  1205. GAS_proportional_get_preferred_address (void *solver,
  1206. const struct GNUNET_PeerIdentity *peer)
  1207. {
  1208. struct GAS_PROPORTIONAL_Handle *s = solver;
  1209. const struct ATS_Address *best_address;
  1210. /* Add to list of pending requests */
  1211. if (GNUNET_NO ==
  1212. GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
  1213. {
  1214. GNUNET_assert (GNUNET_OK ==
  1215. GNUNET_CONTAINER_multipeermap_put (s->requests,
  1216. peer,
  1217. NULL,
  1218. GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
  1219. LOG (GNUNET_ERROR_TYPE_INFO,
  1220. "Start suggesting addresses for peer `%s'\n",
  1221. GNUNET_i2s (peer));
  1222. }
  1223. best_address = update_active_address (s, peer);
  1224. if (s->bulk_lock > 0)
  1225. return NULL; /* Do not suggest since bulk lock is pending */
  1226. return best_address;
  1227. }
  1228. /**
  1229. * Stop notifying about address and bandwidth changes for this peer
  1230. *
  1231. * @param solver the solver handle
  1232. * @param peer the peer
  1233. */
  1234. static void
  1235. GAS_proportional_stop_get_preferred_address (void *solver,
  1236. const struct GNUNET_PeerIdentity *peer)
  1237. {
  1238. struct GAS_PROPORTIONAL_Handle *s = solver;
  1239. struct ATS_Address *cur;
  1240. struct AddressSolverInformation *asi;
  1241. struct Network *cur_net;
  1242. if (GNUNET_YES ==
  1243. GNUNET_CONTAINER_multipeermap_contains (s->requests, peer))
  1244. {
  1245. GNUNET_assert (GNUNET_OK ==
  1246. GNUNET_CONTAINER_multipeermap_remove (s->requests,
  1247. peer,
  1248. NULL));
  1249. LOG (GNUNET_ERROR_TYPE_INFO,
  1250. "Stop suggesting addresses for peer `%s'\n",
  1251. GNUNET_i2s (peer));
  1252. }
  1253. cur = get_active_address (s,
  1254. s->addresses,
  1255. peer);
  1256. if (NULL != cur)
  1257. {
  1258. LOG (GNUNET_ERROR_TYPE_INFO,
  1259. "Disabling %s address %p for peer `%s'\n",
  1260. (GNUNET_NO == cur->active) ? "inactive" : "active",
  1261. cur,
  1262. GNUNET_i2s (&cur->peer));
  1263. /* Disabling current address */
  1264. asi = cur->solver_information;
  1265. cur_net = asi->network ;
  1266. asi->activated = GNUNET_TIME_UNIT_ZERO_ABS;
  1267. cur->active = GNUNET_NO; /* No active any longer */
  1268. cur->assigned_bw_in = 0; /* no bandwidth assigned */
  1269. cur->assigned_bw_out = 0; /* no bandwidth assigned */
  1270. if (GNUNET_SYSERR ==
  1271. addresse_decrement (s, cur_net, GNUNET_NO, GNUNET_YES))
  1272. GNUNET_break(0);
  1273. distribute_bandwidth_in_network (s, cur_net);
  1274. }
  1275. }
  1276. /**
  1277. * Remove an address from the solver
  1278. *
  1279. * @param solver the solver handle
  1280. * @param address the address to remove
  1281. * @param session_only delete only session not whole address
  1282. */
  1283. static void
  1284. GAS_proportional_address_delete (void *solver,
  1285. struct ATS_Address *address,
  1286. int session_only)
  1287. {
  1288. struct GAS_PROPORTIONAL_Handle *s = solver;
  1289. struct Network *net;
  1290. struct AddressWrapper *aw;
  1291. struct AddressSolverInformation *asi;
  1292. /* Remove an adress completely, we have to:
  1293. * - Remove from specific network
  1294. * - Decrease number of total addresses
  1295. * - If active:
  1296. * - decrease number of active addreses
  1297. * - update quotas
  1298. */
  1299. asi = address->solver_information;
  1300. if (NULL == asi)
  1301. {
  1302. GNUNET_break (0);
  1303. return;
  1304. }
  1305. net = asi->network;
  1306. if (GNUNET_NO == session_only)
  1307. {
  1308. LOG (GNUNET_ERROR_TYPE_INFO,
  1309. "Deleting %s address %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
  1310. (GNUNET_NO == address->active) ? "inactive" : "active",
  1311. address,
  1312. GNUNET_i2s (&address->peer),
  1313. net->desc,
  1314. net->total_addresses,
  1315. net->active_addresses);
  1316. /* Remove address */
  1317. addresse_decrement (s, net, GNUNET_YES, GNUNET_NO);
  1318. for (aw = net->head; NULL != aw; aw = aw->next)
  1319. {
  1320. if (aw->addr == address)
  1321. break;
  1322. }
  1323. if (NULL == aw)
  1324. {
  1325. GNUNET_break(0);
  1326. return;
  1327. }
  1328. GNUNET_CONTAINER_DLL_remove (net->head,
  1329. net->tail,
  1330. aw);
  1331. GNUNET_free (aw);
  1332. }
  1333. else
  1334. {
  1335. /* Remove session only: remove if active and update */
  1336. LOG (GNUNET_ERROR_TYPE_INFO,
  1337. "Deleting %s session %p for peer `%s' from network `%s' (total: %u/ active: %u)\n",
  1338. (GNUNET_NO == address->active) ? "inactive" : "active",
  1339. address,
  1340. GNUNET_i2s (&address->peer),
  1341. net->desc,
  1342. net->total_addresses,
  1343. net->active_addresses);
  1344. }
  1345. if (GNUNET_YES == address->active)
  1346. {
  1347. /* Address was active, remove from network and update quotas*/
  1348. address->active = GNUNET_NO;
  1349. address->assigned_bw_in = 0;
  1350. address->assigned_bw_out = 0;
  1351. asi->calculated_quota_in = 0;
  1352. asi->calculated_quota_out = 0;
  1353. if (GNUNET_SYSERR ==
  1354. addresse_decrement (s, net, GNUNET_NO, GNUNET_YES))
  1355. GNUNET_break(0);
  1356. distribute_bandwidth_in_network (s, net);
  1357. if (NULL ==
  1358. update_active_address (s, &address->peer))
  1359. {
  1360. /* No alternative address found, disconnect peer */
  1361. LOG (GNUNET_ERROR_TYPE_INFO,
  1362. "Disconnecting peer `%s' after deleting previous address %p\n",
  1363. GNUNET_i2s (&address->peer),
  1364. address);
  1365. s->env->bandwidth_changed_cb (s->env->cls,
  1366. address);
  1367. }
  1368. }
  1369. if (GNUNET_NO == session_only)
  1370. {
  1371. GNUNET_free_non_null (address->solver_information);
  1372. address->solver_information = NULL;
  1373. }
  1374. LOG (GNUNET_ERROR_TYPE_INFO,
  1375. "After deleting address now total %u and active %u addresses in network `%s'\n",
  1376. net->total_addresses,
  1377. net->active_addresses,
  1378. net->desc);
  1379. }
  1380. /**
  1381. * Start a bulk operation
  1382. *
  1383. * @param solver the solver
  1384. */
  1385. static void
  1386. GAS_proportional_bulk_start (void *solver)
  1387. {
  1388. struct GAS_PROPORTIONAL_Handle *s = solver;
  1389. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1390. "Locking solver for bulk operation ...\n");
  1391. GNUNET_assert (NULL != solver);
  1392. s->bulk_lock++;
  1393. }
  1394. /**
  1395. * Bulk operation done
  1396. */
  1397. static void
  1398. GAS_proportional_bulk_stop (void *solver)
  1399. {
  1400. struct GAS_PROPORTIONAL_Handle *s = solver;
  1401. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1402. "Unlocking solver from bulk operation ...\n");
  1403. if (s->bulk_lock < 1)
  1404. {
  1405. GNUNET_break(0);
  1406. return;
  1407. }
  1408. s->bulk_lock--;
  1409. if ((0 == s->bulk_lock) && (0 < s->bulk_requests))
  1410. {
  1411. LOG(GNUNET_ERROR_TYPE_INFO, "No lock pending, recalculating\n");
  1412. distribute_bandwidth_in_network (s, NULL);
  1413. s->bulk_requests = 0;
  1414. }
  1415. }
  1416. /**
  1417. * Add a new single address to a network
  1418. *
  1419. * @param solver the solver Handle
  1420. * @param address the address to add
  1421. * @param network network type of this address
  1422. */
  1423. static void
  1424. GAS_proportional_address_add (void *solver,
  1425. struct ATS_Address *address,
  1426. uint32_t network);
  1427. /**
  1428. * Transport properties for this address have changed
  1429. *
  1430. * @param solver solver handle
  1431. * @param address the address
  1432. * @param type the ATSI type in HBO
  1433. * @param abs_value the absolute value of the property
  1434. * @param rel_value the normalized value
  1435. */
  1436. static void
  1437. GAS_proportional_address_property_changed (void *solver,
  1438. struct ATS_Address *address,
  1439. uint32_t type,
  1440. uint32_t abs_value,
  1441. double rel_value)
  1442. {
  1443. struct GAS_PROPORTIONAL_Handle *s = solver;
  1444. struct Network *n;
  1445. struct AddressSolverInformation *asi;
  1446. struct ATS_Address *best_address;
  1447. struct ATS_Address *active_address;
  1448. asi = address->solver_information;
  1449. if (NULL == asi)
  1450. {
  1451. GNUNET_break(0);
  1452. return;
  1453. }
  1454. n = asi->network;
  1455. if (NULL == n)
  1456. {
  1457. GNUNET_break(0);
  1458. return;
  1459. }
  1460. LOG(GNUNET_ERROR_TYPE_INFO,
  1461. "Property `%s' for peer `%s' address %p changed to %.2f \n",
  1462. GNUNET_ATS_print_property_type (type), GNUNET_i2s (&address->peer),
  1463. address, rel_value);
  1464. if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
  1465. return; /* Peer is not requested */
  1466. /* This peer is requested, find active and best address */
  1467. active_address = get_active_address(s, s->addresses, &address->peer);
  1468. best_address = update_active_address (s,
  1469. &address->peer);
  1470. if ((NULL != best_address) && ((NULL != active_address) &&
  1471. (GNUNET_YES == address_eq (active_address, best_address))))
  1472. {
  1473. asi = best_address->solver_information;
  1474. GNUNET_assert (NULL != asi);
  1475. /* We sticked to the same address, therefore redistribute */
  1476. distribute_bandwidth_in_network (s, asi->network);
  1477. }
  1478. }
  1479. /**
  1480. * Add a new single address to a network
  1481. *
  1482. * @param solver the solver Handle
  1483. * @param address the address to add
  1484. * @param network network type of this address
  1485. */
  1486. static void
  1487. GAS_proportional_address_add (void *solver,
  1488. struct ATS_Address *address,
  1489. enum GNUNET_ATS_Network_Type network)
  1490. {
  1491. struct GAS_PROPORTIONAL_Handle *s = solver;
  1492. struct Network *net;
  1493. struct AddressWrapper *aw;
  1494. struct AddressSolverInformation *asi;
  1495. net = get_network (s, network);
  1496. if (NULL == net)
  1497. {
  1498. GNUNET_break (0);
  1499. LOG (GNUNET_ERROR_TYPE_ERROR,
  1500. "Unknown network %u `%s' for new address %p for peer `%s'\n",
  1501. network,
  1502. GNUNET_ATS_print_network_type (network),
  1503. address,
  1504. GNUNET_i2s (&address->peer));
  1505. return;
  1506. }
  1507. aw = GNUNET_new (struct AddressWrapper);
  1508. aw->addr = address;
  1509. GNUNET_CONTAINER_DLL_insert(net->head, net->tail, aw);
  1510. address_increment (s, net, GNUNET_YES, GNUNET_NO);
  1511. asi = GNUNET_new (struct AddressSolverInformation);
  1512. asi->network = net;
  1513. asi->calculated_quota_in = 0;
  1514. asi->calculated_quota_out = 0;
  1515. aw->addr->solver_information = asi;
  1516. LOG(GNUNET_ERROR_TYPE_INFO,
  1517. "Adding new address %p for peer `%s', now total %u and active %u addresses in network `%s'\n",
  1518. address, GNUNET_i2s(&address->peer), net->total_addresses, net->active_addresses, net->desc);
  1519. if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (s->requests, &address->peer))
  1520. return; /* Peer is not requested */
  1521. /* This peer is requested, find best address */
  1522. update_active_address (s, &address->peer);
  1523. }
  1524. /**
  1525. * Function invoked when the plugin is loaded.
  1526. *
  1527. * @param[in,out] cls the `struct GNUNET_ATS_PluginEnvironment *` to use;
  1528. * modified to return the API functions (ugh).
  1529. * @return the `struct GAS_PROPORTIONAL_Handle` to pass as a closure
  1530. */
  1531. void *
  1532. libgnunet_plugin_ats_proportional_init (void *cls)
  1533. {
  1534. static struct GNUNET_ATS_SolverFunctions sf;
  1535. struct GNUNET_ATS_PluginEnvironment *env = cls;
  1536. struct GAS_PROPORTIONAL_Handle *s;
  1537. struct Network * cur;
  1538. float f_tmp;
  1539. int c;
  1540. GNUNET_assert (NULL != env);
  1541. GNUNET_assert (NULL != env->cfg);
  1542. GNUNET_assert (NULL != env->bandwidth_changed_cb);
  1543. GNUNET_assert (NULL != env->get_preferences);
  1544. GNUNET_assert (NULL != env->get_property);
  1545. s = GNUNET_new (struct GAS_PROPORTIONAL_Handle);
  1546. s->env = env;
  1547. sf.cls = s;
  1548. sf.s_add = &GAS_proportional_address_add;
  1549. sf.s_address_update_property = &GAS_proportional_address_property_changed;
  1550. sf.s_get = &GAS_proportional_get_preferred_address;
  1551. sf.s_get_stop = &GAS_proportional_stop_get_preferred_address;
  1552. sf.s_pref = &GAS_proportional_address_change_preference;
  1553. sf.s_feedback = &GAS_proportional_address_preference_feedback;
  1554. sf.s_del = &GAS_proportional_address_delete;
  1555. sf.s_bulk_start = &GAS_proportional_bulk_start;
  1556. sf.s_bulk_stop = &GAS_proportional_bulk_stop;
  1557. s->network_entries = GNUNET_malloc (env->network_count * sizeof (struct Network));
  1558. /* Init */
  1559. s->active_addresses = 0;
  1560. s->total_addresses = 0;
  1561. s->bulk_lock = GNUNET_NO;
  1562. s->addresses = env->addresses;
  1563. s->requests = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
  1564. s->stability_factor = PROP_STABILITY_FACTOR;
  1565. if (GNUNET_SYSERR !=
  1566. GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  1567. "PROP_STABILITY_FACTOR", &f_tmp))
  1568. {
  1569. if ((f_tmp < 1.0) || (f_tmp > 2.0))
  1570. {
  1571. LOG (GNUNET_ERROR_TYPE_ERROR,
  1572. _("Invalid %s configuration %f \n"),
  1573. "PROP_STABILITY_FACTOR",
  1574. f_tmp);
  1575. }
  1576. else
  1577. {
  1578. s->stability_factor = f_tmp;
  1579. LOG (GNUNET_ERROR_TYPE_INFO,
  1580. "Using %s of %.3f\n",
  1581. "PROP_STABILITY_FACTOR", f_tmp);
  1582. }
  1583. }
  1584. s->prop_factor = PROPORTIONALITY_FACTOR;
  1585. if (GNUNET_SYSERR !=
  1586. GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  1587. "PROP_PROPORTIONALITY_FACTOR",
  1588. &f_tmp))
  1589. {
  1590. if (f_tmp < 1.0)
  1591. {
  1592. LOG (GNUNET_ERROR_TYPE_ERROR,
  1593. _("Invalid %s configuration %f \n"),
  1594. "PROP_PROPORTIONALITY_FACTOR", f_tmp);
  1595. }
  1596. else
  1597. {
  1598. s->prop_factor = f_tmp;
  1599. LOG (GNUNET_ERROR_TYPE_INFO,
  1600. "Using %s of %.3f\n",
  1601. "PROP_PROPORTIONALITY_FACTOR", f_tmp);
  1602. }
  1603. }
  1604. for (c = 0; c < env->network_count; c++)
  1605. {
  1606. cur = &s->network_entries[c];
  1607. cur->type = c;
  1608. cur->total_quota_in = env->in_quota[c];
  1609. cur->total_quota_out = env->out_quota[c];
  1610. cur->desc = GNUNET_ATS_print_network_type (c);
  1611. GNUNET_asprintf (&cur->stat_total,
  1612. "# ATS addresses %s total",
  1613. cur->desc);
  1614. GNUNET_asprintf (&cur->stat_active,
  1615. "# ATS active addresses %s total",
  1616. cur->desc);
  1617. LOG (GNUNET_ERROR_TYPE_INFO,
  1618. "Added network %u `%s' (%llu/%llu)\n",
  1619. c,
  1620. cur->desc,
  1621. cur->total_quota_in,
  1622. cur->total_quota_out);
  1623. }
  1624. return &sf;
  1625. }
  1626. /* end of plugin_ats_proportional.c */