test_ats_mlp_averaging.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010,2011 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/test_ats_mlp.c
  19. * @brief test for the MLP solver
  20. * @author Christian Grothoff
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_statistics_service.h"
  26. #include "gnunet_ats_service.h"
  27. #include "gnunet-service-ats_addresses_mlp.h"
  28. #define MLP_MAX_EXEC_DURATION GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
  29. #define MLP_MAX_ITERATIONS INT_MAX
  30. static int ret;
  31. struct GNUNET_STATISTICS_Handle * stats;
  32. struct GNUNET_CONTAINER_MultiHashMap * addresses;
  33. struct GAS_MLP_Handle *mlp;
  34. static void
  35. create_address (struct ATS_Address *addr, char * plugin, int ats_count, struct GNUNET_ATS_Information *ats)
  36. {
  37. addr->solver_information = NULL;
  38. addr->next = NULL;
  39. addr->prev = NULL;
  40. addr->plugin = GNUNET_strdup (plugin);
  41. addr->ats_count = ats_count;
  42. addr->ats = ats;
  43. }
  44. static void
  45. set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
  46. {
  47. ats->type = type;
  48. ats->value = value;
  49. }
  50. static unsigned int
  51. load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
  52. {
  53. int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
  54. char * entry_in = NULL;
  55. char * entry_out = NULL;
  56. char * quota_out_str;
  57. char * quota_in_str;
  58. int c;
  59. for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
  60. {
  61. in_dest[c] = 0;
  62. out_dest[c] = 0;
  63. switch (quotas[c]) {
  64. case GNUNET_ATS_NET_UNSPECIFIED:
  65. entry_out = "UNSPECIFIED_QUOTA_OUT";
  66. entry_in = "UNSPECIFIED_QUOTA_IN";
  67. break;
  68. case GNUNET_ATS_NET_LOOPBACK:
  69. entry_out = "LOOPBACK_QUOTA_OUT";
  70. entry_in = "LOOPBACK_QUOTA_IN";
  71. break;
  72. case GNUNET_ATS_NET_LAN:
  73. entry_out = "LAN_QUOTA_OUT";
  74. entry_in = "LAN_QUOTA_IN";
  75. break;
  76. case GNUNET_ATS_NET_WAN:
  77. entry_out = "WAN_QUOTA_OUT";
  78. entry_in = "WAN_QUOTA_IN";
  79. break;
  80. case GNUNET_ATS_NET_WLAN:
  81. entry_out = "WLAN_QUOTA_OUT";
  82. entry_in = "WLAN_QUOTA_IN";
  83. break;
  84. default:
  85. break;
  86. }
  87. if ((entry_in == NULL) || (entry_out == NULL))
  88. continue;
  89. /* quota out */
  90. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
  91. {
  92. if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
  93. (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
  94. out_dest[c] = UINT32_MAX;
  95. GNUNET_free (quota_out_str);
  96. quota_out_str = NULL;
  97. }
  98. else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
  99. out_dest[c] = UINT32_MAX;
  100. else
  101. out_dest[c] = UINT32_MAX;
  102. /* quota in */
  103. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
  104. {
  105. if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
  106. (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
  107. in_dest[c] = UINT32_MAX;
  108. GNUNET_free (quota_in_str);
  109. quota_in_str = NULL;
  110. }
  111. else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
  112. {
  113. in_dest[c] = UINT32_MAX;
  114. }
  115. else
  116. {
  117. in_dest[c] = UINT32_MAX;
  118. }
  119. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
  120. }
  121. return GNUNET_ATS_NetworkTypeCount;
  122. }
  123. static void
  124. check (void *cls, char *const *args, const char *cfgfile,
  125. const struct GNUNET_CONFIGURATION_Handle *cfg)
  126. {
  127. #if !HAVE_LIBGLPK
  128. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
  129. ret = 1;
  130. return;
  131. #endif
  132. struct ATS_Address addr[10];
  133. struct ATS_Address *res[10];
  134. int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
  135. unsigned long long quotas_in[GNUNET_ATS_NetworkTypeCount];
  136. unsigned long long quotas_out[GNUNET_ATS_NetworkTypeCount];
  137. int quota_count;
  138. // struct MLP_information *mlpi;
  139. struct GAS_MLP_SolutionContext ctx;
  140. stats = GNUNET_STATISTICS_create("ats", cfg);
  141. addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
  142. quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
  143. mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
  144. mlp->auto_solve = GNUNET_NO;
  145. struct GNUNET_PeerIdentity p[10];
  146. /* Creating peer 1 */
  147. GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
  148. /* Creating peer 1 address 1 */
  149. addr[0].peer.hashPubKey = p[0].hashPubKey;
  150. struct GNUNET_ATS_Information a1_ats[3];
  151. set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
  152. set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 0);
  153. set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
  154. create_address (&addr[0], "dummy", 3, &a1_ats[0]);
  155. addr[0].atsp_network_type = GNUNET_ATS_NET_LAN;
  156. GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  157. /* Add peer 1 address 1 */
  158. GAS_mlp_address_update (mlp, addresses, &addr[0]);
  159. // mlpi = addr[0].mlp_information;
  160. GNUNET_assert (mlp != NULL);
  161. GNUNET_assert (mlp->addresses_in_problem == 1);
  162. /* Update an peer 1 address 1 */
  163. set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 20);
  164. GAS_mlp_address_update (mlp, addresses, &addr[0]);
  165. GNUNET_assert (mlp->addresses_in_problem == 1);
  166. /* Update an peer 1 address 1 */
  167. set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 10);
  168. GAS_mlp_address_update (mlp, addresses, &addr[0]);
  169. GNUNET_assert (mlp->addresses_in_problem == 1);
  170. /* Update an peer 1 address 1 */
  171. set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 10);
  172. GAS_mlp_address_update (mlp, addresses, &addr[0]);
  173. GNUNET_assert (mlp->addresses_in_problem == 1);
  174. /* Update an peer 1 address 1 */
  175. set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 30);
  176. GAS_mlp_address_update (mlp, addresses, &addr[0]);
  177. GNUNET_assert (mlp->addresses_in_problem == 1);
  178. GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
  179. GNUNET_assert (GNUNET_OK == ctx.lp_result);
  180. GNUNET_assert (GNUNET_OK == ctx.mlp_result);
  181. res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
  182. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  183. "Preferred address `%s' outbound bandwidth: %u Bps\n",
  184. res[0]->plugin,
  185. (unsigned int) ntohl (res[0]->assigned_bw_out.value__));
  186. /* Delete an address */
  187. GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
  188. GAS_mlp_address_delete (mlp, addresses, &addr[0]);
  189. GNUNET_assert (mlp->addresses_in_problem == 0);
  190. GAS_mlp_done (mlp);
  191. GNUNET_free (addr[0].plugin);
  192. GNUNET_CONTAINER_multihashmap_destroy (addresses);
  193. GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
  194. ret = 0;
  195. }
  196. int
  197. main (int argc, char *argv[])
  198. {
  199. static char *const argv2[] = { "test_ats_mlp",
  200. "-c",
  201. "test_ats_api.conf",
  202. "-L", "WARNING",
  203. NULL
  204. };
  205. static struct GNUNET_GETOPT_CommandLineOption options[] = {
  206. GNUNET_GETOPT_OPTION_END
  207. };
  208. GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
  209. "test_ats_mlp", "nohelp", options,
  210. &check, NULL);
  211. return ret;
  212. }
  213. /* end of file test_ats_api_bandwidth_consumption.c */