get more information
[oweals/gnunet.git] / src / ats / test_ats_mlp.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file ats/test_ats_mlp.c
22  * @brief test for the MLP solver
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_statistics_service.h"
30 #include "gnunet_ats_service.h"
31 #include "gnunet-service-ats_addresses_mlp.h"
32
33 #define MLP_MAX_EXEC_DURATION   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3)
34 #define MLP_MAX_ITERATIONS      INT_MAX
35
36
37 static int ret;
38
39 struct GNUNET_STATISTICS_Handle * stats;
40
41 struct GNUNET_CONTAINER_MultiHashMap * addresses;
42
43 struct GAS_MLP_Handle *mlp;
44
45
46 static void
47 create_address (struct ATS_Address *addr, char * plugin, int ats_count, struct GNUNET_ATS_Information *ats)
48 {
49   addr->solver_information = NULL;
50   addr->next = NULL;
51   addr->prev = NULL;
52   addr->plugin = GNUNET_strdup (plugin);
53   addr->ats_count = ats_count;
54   addr->ats = ats;
55 }
56
57 static void
58 set_ats (struct GNUNET_ATS_Information *ats, uint32_t type, uint32_t value)
59 {
60   ats->type = type;
61   ats->value = value;
62 }
63
64
65 unsigned int
66 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
67 {
68   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
69   char * entry_in = NULL;
70   char * entry_out = NULL;
71   char * quota_out_str;
72   char * quota_in_str;
73   int c;
74
75   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
76   {
77     in_dest[c] = 0;
78     out_dest[c] = 0;
79     switch (quotas[c]) {
80       case GNUNET_ATS_NET_UNSPECIFIED:
81         entry_out = "UNSPECIFIED_QUOTA_OUT";
82         entry_in = "UNSPECIFIED_QUOTA_IN";
83         break;
84       case GNUNET_ATS_NET_LOOPBACK:
85         entry_out = "LOOPBACK_QUOTA_OUT";
86         entry_in = "LOOPBACK_QUOTA_IN";
87         break;
88       case GNUNET_ATS_NET_LAN:
89         entry_out = "LAN_QUOTA_OUT";
90         entry_in = "LAN_QUOTA_IN";
91         break;
92       case GNUNET_ATS_NET_WAN:
93         entry_out = "WAN_QUOTA_OUT";
94         entry_in = "WAN_QUOTA_IN";
95         break;
96       case GNUNET_ATS_NET_WLAN:
97         entry_out = "WLAN_QUOTA_OUT";
98         entry_in = "WLAN_QUOTA_IN";
99         break;
100       default:
101         break;
102     }
103
104     if ((entry_in == NULL) || (entry_out == NULL))
105       continue;
106
107     /* quota out */
108     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
109     {
110       if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
111           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
112         out_dest[c] = UINT32_MAX;
113
114       GNUNET_free (quota_out_str);
115       quota_out_str = NULL;
116     }
117     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
118       out_dest[c] = UINT32_MAX;
119     else
120       out_dest[c] = UINT32_MAX;
121
122     /* quota in */
123     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
124     {
125       if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
126           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
127         in_dest[c] = UINT32_MAX;
128
129       GNUNET_free (quota_in_str);
130       quota_in_str = NULL;
131     }
132     else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
133     {
134       in_dest[c] = UINT32_MAX;
135     }
136     else
137     {
138         in_dest[c] = UINT32_MAX;
139     }
140     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
141
142   }
143   return GNUNET_ATS_NetworkTypeCount;
144 }
145
146
147
148 static void
149 check (void *cls, char *const *args, const char *cfgfile,
150        const struct GNUNET_CONFIGURATION_Handle *cfg)
151 {
152 #if !HAVE_LIBGLPK
153   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "GLPK not installed!");
154   ret = 1;
155   return;
156 #endif
157   struct ATS_Address addr[10];
158   struct ATS_Address *res[10];
159   struct GAS_MLP_SolutionContext ctx;
160   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
161   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
162   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
163   int quota_count;
164
165   stats = GNUNET_STATISTICS_create("ats", cfg);
166
167   addresses = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
168
169   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
170   mlp = GAS_mlp_init (cfg, NULL, quotas, quotas_in, quotas_out, quota_count);
171   mlp->auto_solve = GNUNET_NO;
172
173   struct GNUNET_PeerIdentity p[10];
174
175   /* Creating peer 1 */
176   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[0].hashPubKey);
177   /* Creating peer 2 */
178   GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &p[1].hashPubKey);
179
180   /* Creating peer 1 address 1 */
181   addr[0].peer.hashPubKey = p[0].hashPubKey;
182   struct GNUNET_ATS_Information a1_ats[3];
183   set_ats (&a1_ats[0], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
184   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
185   set_ats (&a1_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
186   create_address (&addr[0], "dummy", 3, &a1_ats[0]);
187   addr[0].atsp_network_type = GNUNET_ATS_NET_WAN;
188
189   /* Creating peer 1  address 2 */
190   addr[1].peer.hashPubKey = p[0].hashPubKey;
191   struct GNUNET_ATS_Information a2_ats[3];
192   set_ats (&a2_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
193   set_ats (&a2_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
194   set_ats (&a2_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
195   create_address (&addr[1], "dummy2", 3, &a2_ats[0]);
196   addr[1].atsp_network_type = GNUNET_ATS_NET_LAN;
197
198   /* Creating peer 2  address 1 */
199   addr[2].peer.hashPubKey = p[1].hashPubKey;
200   struct GNUNET_ATS_Information a3_ats[3];
201   set_ats (&a3_ats[1], GNUNET_ATS_QUALITY_NET_DISTANCE, 1);
202   set_ats (&a3_ats[0], GNUNET_ATS_QUALITY_NET_DELAY, 1);
203   set_ats (&a3_ats[2], GNUNET_ATS_ARRAY_TERMINATOR, 0);
204   create_address (&addr[2], "dummy3", 3, &a3_ats[0]);
205   addr[2].atsp_network_type = GNUNET_ATS_NET_LAN;
206
207   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[0], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
208
209   /* Add peer 1 address 1 */
210   GAS_mlp_address_update (mlp, addresses, &addr[0]);
211
212   GNUNET_assert (mlp != NULL);
213   GNUNET_assert (mlp->addr_in_problem == 1);
214
215   /* Update an peer 1 address 1  */
216   set_ats (&a1_ats[1], GNUNET_ATS_QUALITY_NET_DELAY, 1);
217   GAS_mlp_address_update (mlp, addresses, &addr[0]);
218   GNUNET_assert (mlp->addr_in_problem == 1);
219
220   /* Add peer 1 address 2 */
221   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[0].peer.hashPubKey, &addr[1], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
222   GAS_mlp_address_update (mlp, addresses, &addr[1]);
223   GNUNET_assert (mlp->addr_in_problem == 2);
224
225   /* Add peer 2 address 1 */
226   GNUNET_CONTAINER_multihashmap_put(addresses, &addr[2].peer.hashPubKey, &addr[2], GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
227   GAS_mlp_address_update (mlp, addresses, &addr[2]);
228   GNUNET_assert (mlp->addr_in_problem == 3);
229
230   GNUNET_assert (GNUNET_OK == GAS_mlp_solve_problem(mlp, &ctx));
231   GNUNET_assert (GNUNET_OK == ctx.lp_result);
232   GNUNET_assert (GNUNET_OK == ctx.mlp_result);
233
234   res[0] = GAS_mlp_get_preferred_address(mlp, addresses, &p[0]);
235   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[0]->plugin, res[0]->assigned_bw_out);
236   res[1] = GAS_mlp_get_preferred_address(mlp, addresses, &p[1]);
237   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Preferred address `%s' outbound bandwidth: %u Bps\n",res[1]->plugin, res[1]->assigned_bw_out);
238
239   /* Delete an address */
240   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[0].peer.hashPubKey, &addr[0]);
241   GAS_mlp_address_delete (mlp, addresses, &addr[0]);
242   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[1].peer.hashPubKey, &addr[1]);
243   GAS_mlp_address_delete (mlp, addresses, &addr[1]);
244   GNUNET_CONTAINER_multihashmap_remove (addresses, &addr[2].peer.hashPubKey, &addr[2]);
245   GAS_mlp_address_delete (mlp, addresses, &addr[2]);
246
247   GNUNET_assert (mlp->addr_in_problem == 0);
248
249   GAS_mlp_done (mlp);
250
251   GNUNET_free (addr[0].plugin);
252   GNUNET_free (addr[1].plugin);
253   GNUNET_CONTAINER_multihashmap_destroy (addresses);
254   GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
255
256   ret = 0;
257   return;
258 }
259
260
261 int
262 main (int argc, char *argv[])
263 {
264
265   static char *const argv2[] = { "test_ats_mlp",
266     "-c",
267     "test_ats_api.conf",
268     "-L", "WARNING",
269     NULL
270   };
271
272   static struct GNUNET_GETOPT_CommandLineOption options[] = {
273     GNUNET_GETOPT_OPTION_END
274   };
275
276   GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
277                       "test_ats_mlp", "nohelp", options,
278                       &check, NULL);
279
280
281   return ret;
282 }
283
284 /* end of file test_ats_api_bandwidth_consumption.c */