22e9207f3a6e61d452fd50561e5482be2c97fc0c
[oweals/gnunet.git] / src / testbed / testbed_api_topology.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 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 /**
22  * @file testbed/testbed_api_topology.c
23  * @brief topology-generation functions
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_testbed_service.h"
28 #include "testbed_api.h"
29 #include "testbed_api_peers.h"
30 #include "testbed_api_operations.h"
31
32 /**
33  * Generic loggins shorthand
34  */
35 #define LOG(kind,...)                                           \
36   GNUNET_log_from (kind, "testbed-api-topology", __VA_ARGS__)
37
38 /**
39  * Representation of an overlay link
40  */
41 struct OverlayLink
42 {
43   /**
44    * position of peer A's handle in peers array
45    */
46   uint32_t A;
47
48   /**
49    * position of peer B's handle in peers array
50    */
51   uint32_t B;
52
53 };
54
55
56 /**
57  * Context information for topology operations
58  */
59 struct TopologyContext
60 {
61   /**
62    * The array of peers
63    */
64   struct GNUNET_TESTBED_Peer **peers;
65
66   /**
67    * An array of links; this array is of size link_array_size
68    */
69   struct OverlayLink *link_array;
70
71   /**
72    * An array of operations resulting from the links we try to establish; the
73    * number of operations in this array is equal to link_array_size (1 link = 1
74    * operation)
75    */
76   struct GNUNET_TESTBED_Operation **link_ops;
77
78   /**
79    * The operation closure
80    */
81   void *op_cls;
82
83   /**
84    * The size of the link array
85    */
86   unsigned int link_array_size;  
87   
88 };
89
90
91 /**
92  * Callback to be called when an overlay_link operation complete
93  *
94  * @param cls element of the link_op array which points to the corresponding operation
95  * @param op the operation that has been finished
96  * @param emsg error message in case the operation has failed; will be NULL if
97  *          operation has executed successfully.
98  */
99 static void 
100 overlay_link_completed (void *cls,
101                         struct GNUNET_TESTBED_Operation *op, 
102                         const char *emsg)
103 {
104   struct GNUNET_TESTBED_Operation **link_op = cls;
105
106   GNUNET_assert (*link_op == op);
107   GNUNET_TESTBED_operation_done (op);
108   *link_op = NULL;
109   if (NULL != emsg)
110   {
111     LOG (GNUNET_ERROR_TYPE_WARNING,
112          "Error while establishing a link: %s\n", emsg);
113     return;
114   }
115 }
116
117
118
119 /**
120  * Function called when a overlay connect operation is ready
121  *
122  * @param cls the Topology context
123  */
124 static void
125 opstart_overlay_configure_topology (void *cls)
126 {
127   struct TopologyContext *tc = cls;
128   unsigned int p;
129   
130   tc->link_ops = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operations *)
131                                 * tc->link_array_size);
132   for (p = 0; p < tc->link_array_size; p++)
133   {
134     tc->link_ops[p] =
135         GNUNET_TESTBED_overlay_connect (tc->op_cls, &overlay_link_completed,
136                                         &tc->link_ops[p],
137                                         tc->peers[tc->link_array[p].A],
138                                         tc->peers[tc->link_array[p].B]);                                                  
139   }
140 }
141
142
143 /**
144  * Callback which will be called when overlay connect operation is released
145  *
146  * @param cls the Topology context
147  */
148 static void
149 oprelease_overlay_configure_topology (void *cls)
150 {
151   struct TopologyContext *tc = cls;
152   unsigned int p;
153   
154   if (NULL != tc->link_ops)
155   {
156     for (p = 0; p < tc->link_array_size; p++)
157       if (NULL != tc->link_ops[p])
158         GNUNET_TESTBED_operation_cancel (tc->link_ops[p]);      
159     GNUNET_free (tc->link_ops);    
160   }
161   GNUNET_free_non_null (tc->link_array);
162   GNUNET_free (tc);
163 }
164
165
166 /**
167  * Configure overall network topology to have a particular shape.
168  *
169  * @param op_cls closure argument to give with the operation event
170  * @param num_peers number of peers in 'peers'
171  * @param peers array of 'num_peers' with the peers to configure
172  * @param topo desired underlay topology to use
173  * @param ap topology-specific options
174  * @return handle to the operation, NULL if configuring the topology
175  *         is not allowed at this time
176  */
177 struct GNUNET_TESTBED_Operation *
178 GNUNET_TESTBED_underlay_configure_topology_va (void *op_cls,
179                                                unsigned int num_peers,
180                                                struct GNUNET_TESTBED_Peer
181                                                **peers,
182                                                enum
183                                                GNUNET_TESTBED_TopologyOption
184                                                topo, va_list ap)
185 {
186   GNUNET_break (0);
187   return NULL;
188 }
189
190
191 /**
192  * Configure overall network topology to have a particular shape.
193  *
194  * @param op_cls closure argument to give with the operation event
195  * @param num_peers number of peers in 'peers'
196  * @param peers array of 'num_peers' with the peers to configure
197  * @param topo desired underlay topology to use
198  * @param ... topology-specific options
199  * @return handle to the operation, NULL if configuring the topology
200  *         is not allowed at this time
201  */
202 struct GNUNET_TESTBED_Operation *
203 GNUNET_TESTBED_underlay_configure_topology (void *op_cls,
204                                             unsigned int num_peers,
205                                             struct GNUNET_TESTBED_Peer **peers,
206                                             enum GNUNET_TESTBED_TopologyOption
207                                             topo, ...)
208 {
209   GNUNET_break (0);
210   return NULL;
211 }
212
213
214 /**
215  * All peers must have been started before calling this function.
216  * This function then connects the given peers in the P2P overlay
217  * using the given topology.
218  *
219  * @param op_cls closure argument to give with the operation event
220  * @param num_peers number of peers in 'peers'
221  * @param peers array of 'num_peers' with the peers to configure
222  * @param topo desired underlay topology to use
223  * @param va topology-specific options
224  * @return handle to the operation, NULL if connecting these
225  *         peers is fundamentally not possible at this time (peers
226  *         not running or underlay disallows) or if num_peers is less than 2
227  */
228 struct GNUNET_TESTBED_Operation *
229 GNUNET_TESTBED_overlay_configure_topology_va (void *op_cls,
230                                               unsigned int num_peers,
231                                               struct GNUNET_TESTBED_Peer **peers,
232                                               enum GNUNET_TESTBED_TopologyOption
233                                               topo, va_list va)
234 {
235   struct TopologyContext *tc;
236   struct GNUNET_TESTBED_Operation *op;
237   struct GNUNET_TESTBED_Controller *c;
238   unsigned int cnt;
239
240   if (num_peers < 2)
241     return NULL;
242   c = peers[0]->controller;
243   tc = GNUNET_malloc (sizeof (struct TopologyContext));
244   tc->peers = peers;
245   tc->op_cls = tc->op_cls;
246   switch (topo)
247   {
248   case GNUNET_TESTBED_TOPOLOGY_LINE:
249     tc->link_array_size = num_peers - 1;
250     tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) *
251                                     tc->link_array_size);
252     for (cnt=1; cnt < num_peers; cnt++)
253     {
254       tc->link_array[cnt-1].A = cnt-1;
255       tc->link_array[cnt-1].B = cnt;
256     }
257     break;
258   case GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI:
259     tc->link_array_size = va_arg (va, unsigned int);
260     tc->link_array = GNUNET_malloc (sizeof (struct OverlayLink) *
261                                     tc->link_array_size);
262     for (cnt = 0; cnt < tc->link_array_size; cnt++)
263     {
264       uint32_t A_rand;
265       uint32_t B_rand;
266       
267       do {
268         A_rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
269                                            num_peers);
270         B_rand = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
271                                            num_peers);
272       } while (A_rand == B_rand);      
273       tc->link_array[cnt].A = A_rand;
274       tc->link_array[cnt].B = B_rand;
275     }
276     break;
277   default:
278     GNUNET_break (0);
279     return NULL;
280   }
281   op = GNUNET_TESTBED_operation_create_ (tc,
282                                          &opstart_overlay_configure_topology,
283                                          &oprelease_overlay_configure_topology);
284   GNUNET_TESTBED_operation_queue_insert_
285       (c->opq_parallel_topology_config_operations, op);
286   return op;
287 }
288
289
290 /**
291  * All peers must have been started before calling this function.
292  * This function then connects the given peers in the P2P overlay
293  * using the given topology.
294  *
295  * @param op_cls closure argument to give with the operation event
296  * @param num_peers number of peers in 'peers'
297  * @param peers array of 'num_peers' with the peers to configure
298  * @param topo desired underlay topology to use
299  * @param ... topology-specific options
300  * @return handle to the operation, NULL if connecting these
301  *         peers is fundamentally not possible at this time (peers
302  *         not running or underlay disallows) or if num_peers is less than 2
303  */
304 struct GNUNET_TESTBED_Operation *
305 GNUNET_TESTBED_overlay_configure_topology (void *op_cls, unsigned int num_peers,
306                                            struct GNUNET_TESTBED_Peer **peers,
307                                            enum GNUNET_TESTBED_TopologyOption
308                                            topo, ...)
309 {
310   struct GNUNET_TESTBED_Operation *op;
311   va_list vargs;
312
313   va_start (vargs, topo);
314   op = GNUNET_TESTBED_overlay_configure_topology_va (op_cls, num_peers, peers,
315                                                      topo, vargs);
316   va_end (vargs);
317   return op;
318 }
319
320 /* end of testbed_api_topology.c */