c27033ee7555cffd51506fdc005b1897563d093e
[oweals/gnunet.git] / src / ats-tests / ats-testing.h
1 /*
2  This file is part of GNUnet.
3  (C) 2010-2013 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-tests/ats-testing.h
22  * @brief ats testing library: setup topology and provide logging to test ats
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_core_service.h"
31
32 #define TEST_ATS_PREFERENCE_DEFAULT 1.0
33
34 /**
35  * Message type sent for traffic generation
36  */
37 #define TEST_MESSAGE_TYPE_PING 12345
38
39 /**
40  * Message type sent as response during traffic generation
41  */
42 #define TEST_MESSAGE_TYPE_PONG 12346
43
44 /**
45  * Size of test messages
46  */
47 #define TEST_MESSAGE_SIZE 100
48
49 struct BenchmarkPartner;
50
51 struct BenchmarkPeer;
52
53 struct GNUNET_ATS_TEST_Topology;
54
55 struct TrafficGenerator;
56
57 struct LoggingHandle;
58
59
60 /**
61  * Callback to call when topology setup is completed
62  *
63  * @param cls the closure
64  * @param masters array of master peers
65  * @param slaves array of master peers
66  */
67 typedef void (*GNUNET_ATS_TEST_TopologySetupDoneCallback) (void *cls,
68     struct BenchmarkPeer *masters,
69     struct BenchmarkPeer *slaves);
70
71 /**
72  * Callback called when logging is required for the data contained
73  *
74  * @param cls the closure
75  * @param address an address
76  * @param address_active is address active
77  * @param bandwidth_out bandwidth outbound
78  * @param bandwidth_in bandwidth inbound
79  * @param ats ats information
80  * @param ats_count number of ats inforation
81  */
82 typedef void
83 (*GNUNET_ATS_TEST_LogRequest) (void *cls,
84     const struct GNUNET_HELLO_Address *address,
85     int address_active,
86     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
87     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
88     const struct GNUNET_ATS_Information *ats,
89     uint32_t ats_count);
90
91 /**
92  * Information we track for a peer in the testbed.
93  */
94 struct BenchmarkPeer
95 {
96   /**
97    * Handle with testbed.
98    */
99   struct GNUNET_TESTBED_Peer *peer;
100
101   /**
102    * Unique identifier
103    */
104   int no;
105
106   /**
107    * Is this peer a measter: GNUNET_YES/GNUNET_NO
108    */
109   int master;
110
111   /**
112    *  Peer ID
113    */
114   struct GNUNET_PeerIdentity id;
115
116   /**
117    * Testbed operation to get peer information
118    */
119   struct GNUNET_TESTBED_Operation *peer_id_op;
120
121   /**
122    * Testbed operation to connect to ATS performance service
123    */
124   struct GNUNET_TESTBED_Operation *ats_perf_op;
125
126   /**
127    * Testbed operation to connect to core
128    */
129   struct GNUNET_TESTBED_Operation *comm_op;
130
131   /**
132    * ATS performance handle
133    */
134   struct GNUNET_ATS_PerformanceHandle *ats_perf_handle;
135
136   /**
137    * Masters only:
138    * Testbed connect operations to connect masters to slaves
139    */
140   struct TestbedConnectOperation *core_connect_ops;
141
142   /**
143    *  Core handle
144    */
145   struct GNUNET_CORE_Handle *ch;
146
147   /**
148    *  Core handle
149    */
150   struct GNUNET_TRANSPORT_Handle *th;
151
152   /**
153    * Masters only:
154    * Peer to set ATS preferences for
155    */
156   struct BenchmarkPeer *pref_partner;
157
158   /**
159    * Masters only
160    * Progress task
161    */
162   GNUNET_SCHEDULER_TaskIdentifier ats_task;
163
164   /**
165    * Masters only
166    * Progress task
167    */
168   double pref_value;
169
170   /**
171    * Array of partners with num_slaves entries (if master) or
172    * num_master entries (if slave)
173    */
174   struct BenchmarkPartner *partners;
175
176   /**
177    * Number of partners
178    */
179   int num_partners;
180
181   /**
182    * Number of core connections
183    */
184   int core_connections;
185
186   /**
187    * Masters only:
188    * Number of connections to slave peers
189    */
190   int core_slave_connections;
191
192   /**
193    * Total number of messages this peer has sent
194    */
195   unsigned int total_messages_sent;
196
197   /**
198    * Total number of bytes this peer has sent
199    */
200   unsigned int total_bytes_sent;
201
202   /**
203    * Total number of messages this peer has received
204    */
205   unsigned int total_messages_received;
206
207   /**
208    * Total number of bytes this peer has received
209    */
210   unsigned int total_bytes_received;
211 };
212
213 struct TrafficGenerator
214 {
215   struct TrafficGenerator *prev;
216   struct TrafficGenerator *next;
217
218   struct BenchmarkPeer *src;
219   struct BenchmarkPartner *dest;
220   unsigned int rate;
221   GNUNET_SCHEDULER_TaskIdentifier send_task;
222   struct GNUNET_TIME_Absolute next_ping_transmission;
223   struct GNUNET_TIME_Relative delta;
224 };
225
226
227 /**
228  * Information about a benchmarking partner
229  */
230 struct BenchmarkPartner
231 {
232   /**
233    * The peer itself this partner belongs to
234    */
235   struct BenchmarkPeer *me;
236
237   /**
238    * The partner peer
239    */
240   struct BenchmarkPeer *dest;
241
242   /**
243    * Core transmit handles
244    */
245   struct GNUNET_CORE_TransmitHandle *cth;
246
247   /**
248    * Transport transmit handles
249    */
250   struct GNUNET_TRANSPORT_TransmitHandle *tth;
251
252   struct TrafficGenerator *tg;
253
254   /**
255    * Timestamp to calculate communication layer delay
256    */
257   struct GNUNET_TIME_Absolute last_message_sent;
258
259   /**
260    * Accumulated RTT for all messages
261    */
262   unsigned int total_app_rtt;
263
264   /**
265    * Number of messages sent to this partner
266    */
267   unsigned int messages_sent;
268
269   /**
270    * Number of bytes sent to this partner
271    */
272   unsigned int bytes_sent;
273
274   /**
275    * Number of messages received from this partner
276    */
277   unsigned int messages_received;
278
279   /**
280    * Number of bytes received from this partner
281    */
282   unsigned int bytes_received;
283
284   /* Current ATS properties */
285
286   uint32_t ats_distance;
287
288   uint32_t ats_delay;
289
290   uint32_t bandwidth_in;
291
292   uint32_t bandwidth_out;
293
294   uint32_t ats_utilization_up;
295
296   uint32_t ats_utilization_down;
297
298   uint32_t ats_network_type;
299
300   uint32_t ats_cost_wan;
301
302   uint32_t ats_cost_lan;
303
304   uint32_t ats_cost_wlan;
305 };
306
307 /**
308  * Overall state of the performance benchmark
309  */
310 struct BenchmarkState
311 {
312   /**
313    * Are we connected to ATS service of all peers: GNUNET_YES/NO
314    */
315   int connected_ATS_service;
316
317   /**
318    * Are we connected to CORE service of all peers: GNUNET_YES/NO
319    */
320   int connected_COMM_service;
321
322   /**
323    * Are we connected to all peers: GNUNET_YES/NO
324    */
325   int connected_PEERS;
326
327   /**
328    * Are we connected to all slave peers on CORE level: GNUNET_YES/NO
329    */
330   int connected_CORE;
331
332   /**
333    * Are we connected to CORE service of all peers: GNUNET_YES/NO
334    */
335   int benchmarking;
336 };
337
338
339 struct GNUNET_ATS_TEST_Topology
340 {
341   /**
342    * Shutdown task
343    */
344   GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
345
346   /**
347    * Progress task
348    */
349   GNUNET_SCHEDULER_TaskIdentifier progress_task;
350
351   /**
352    * Test result
353    */
354   int result;
355
356   /**Test core (GNUNET_YES) or transport (GNUNET_NO)
357    */
358   int test_core;
359
360   /**
361    * Solver string
362    */
363   char *solver;
364
365   /**
366    * Preference string
367    */
368   char *testname;
369
370   /**
371    * Preference string
372    */
373   char *pref_str;
374
375   /**
376    * ATS preference value
377    */
378   int pref_val;
379
380   /**
381    * Number master peers
382    */
383   unsigned int num_masters;
384
385   /**
386    * Array of master peers
387    */
388   struct BenchmarkPeer *mps;
389
390   /**
391    * Number slave peers
392    */
393   unsigned int num_slaves;
394
395   /**
396    * Array of slave peers
397    */
398   struct BenchmarkPeer *sps;
399
400   /**
401    * Benchmark duration
402    */
403   struct GNUNET_TIME_Relative perf_duration;
404
405   /**
406    * Logging frequency
407    */
408   struct GNUNET_TIME_Relative log_frequency;
409
410   /**
411    * Benchmark state
412    */
413   struct BenchmarkState state;
414
415   struct GNUNET_CORE_MessageHandler *handlers;
416
417   GNUNET_TRANSPORT_ReceiveCallback transport_recv_cb;
418
419   GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb;
420   GNUNET_ATS_AddressInformationCallback ats_perf_cb;
421   void *done_cb_cls;
422 };
423
424 enum OperationType
425 {
426   START_SEND,
427   STOP_SEND,
428   SET_RATE,
429   SET_PREFERENCE
430 };
431
432 struct Episode;
433
434 struct Experiment;
435
436 typedef void (*GNUNET_ATS_TESTING_EpisodeDoneCallback) (
437     struct Episode *e);
438
439 typedef void (*GNUNET_ATS_TESTING_ExperimentDoneCallback) (struct Experiment *e,
440     struct GNUNET_TIME_Relative duration,int success);
441
442 struct Operation
443 {
444   struct Operation *next;
445   struct Operation *prev;
446   long long unsigned int src_id;
447   long long unsigned int dest_id;
448   long long unsigned int value;
449   enum OperationType type;
450 };
451
452 struct Episode
453 {
454   int id;
455   struct Episode *next;
456   struct GNUNET_TIME_Relative duration;
457
458   struct Operation *head;
459   struct Operation *tail;
460 };
461
462
463 struct Experiment
464 {
465   char *name;
466   char *cfg_file;
467   unsigned long long int num_masters;
468   unsigned long long int num_slaves;
469   struct GNUNET_TIME_Relative max_duration;
470   struct GNUNET_TIME_Relative total_duration;
471   struct GNUNET_TIME_Absolute start_time;
472   unsigned int num_episodes;
473   struct Episode *start;
474
475   GNUNET_SCHEDULER_TaskIdentifier experiment_timeout_task;
476   GNUNET_SCHEDULER_TaskIdentifier episode_timeout_task;
477   struct Episode *cur;
478
479   GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb;
480   GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb;
481 };
482
483 /*
484  * Experiment related functions
485  */
486
487
488 /**
489  * Execute the specified experiment
490  *
491  * @param e the Experiment
492  * @param ep_done_cb a episode is completed
493  * @param e_done_cb the experiment is completed
494  */
495 void
496 GNUNET_ATS_TEST_experimentation_run (struct Experiment *e,
497     GNUNET_ATS_TESTING_EpisodeDoneCallback ep_done_cb,
498     GNUNET_ATS_TESTING_ExperimentDoneCallback e_done_cb);
499
500 /**
501  * Load an experiment from a file
502  *
503  * @param filename the file
504  * @return the Experiment or NULL on failure
505  */
506 struct Experiment *
507 GNUNET_ATS_TEST_experimentation_load (char *filename);
508
509
510 /**
511  * Stop an experiment
512  *
513  * @param e the experiment
514  */
515 void
516 GNUNET_ATS_TEST_experimentation_stop (struct Experiment *e);
517
518 /*
519  * Traffic related functions
520  */
521
522 void
523 GNUNET_ATS_TEST_traffic_handle_ping (struct BenchmarkPartner *p);
524
525 void
526 GNUNET_ATS_TEST_traffic_handle_pong (struct BenchmarkPartner *p);
527
528
529 struct TrafficGenerator *
530 GNUNET_ATS_TEST_generate_traffic_start (struct BenchmarkPeer *src,
531     struct BenchmarkPartner *dest,
532     unsigned int rate,
533     struct GNUNET_TIME_Relative duration);
534
535 void
536 GNUNET_ATS_TEST_generate_traffic_stop (struct TrafficGenerator *tg);
537
538 /**
539  * Stop all traffic generators
540  */
541 void
542 GNUNET_ATS_TEST_generate_traffic_stop_all ();
543
544
545 /*
546  * Logging related functions
547  */
548
549 /**
550  * Start logging
551  *
552  * @param log_frequency the logging frequency
553  * @param testname the testname
554  * @param masters the master peers used for benchmarking
555  * @param num_master the number of master peers
556  * @return the logging handle or NULL on error
557  */
558 struct LoggingHandle *
559 GNUNET_ATS_TEST_logging_start (struct GNUNET_TIME_Relative log_frequency,
560     char * testname,
561     struct BenchmarkPeer *masters,
562     int num_masters);
563
564 /**
565  * Stop logging
566  *
567  * @param l the logging handle
568  */
569 void
570 GNUNET_ATS_TEST_logging_clean_up (struct LoggingHandle *l);
571
572 /**
573  * Stop logging
574  *
575  * @param l the logging handle
576  */
577 void
578 GNUNET_ATS_TEST_logging_stop (struct LoggingHandle *l);
579
580 /**
581  * Log all data now
582  *
583  * @param llogging handle to use
584  */
585 void
586 GNUNET_ATS_TEST_logging_now (struct LoggingHandle *l);
587
588
589 /**
590  * Write logging data to file
591  *
592  * @param l logging handle to use
593  * @param test_name name of the current test
594  */
595 void
596 GNUNET_ATS_TEST_logging_write_to_file (struct LoggingHandle *h,
597     char *test_name);
598
599 /*
600  * Topology related functions
601  */
602
603 /**
604  * Create a topology for ats testing
605  *
606  * @param name test name
607  * @param cfg_file configuration file to use for the peers
608  * @param num_slaves number of slaves
609  * @param num_masters number of masters
610  * @param test_core connect to CORE service (GNUNET_YES) or transport (GNUNET_NO)
611  * @param done_cb function to call when topology is setup
612  * @param done_cb_cls cls for callback
613  * @param recv_cb callback to call when data are received
614  * @param perf_cb callback to call when performance info are received
615  */
616 void
617 GNUNET_ATS_TEST_create_topology (char *name, char *cfg_file,
618     unsigned int num_slaves,
619     unsigned int num_masters,
620     int test_core,
621     GNUNET_ATS_TEST_TopologySetupDoneCallback done_cb,
622     void *done_cb_cls,
623     GNUNET_TRANSPORT_ReceiveCallback recv_cb,
624     GNUNET_ATS_TEST_LogRequest ats_perf_cb);
625
626 /**
627  * Shutdown topology
628  */
629 void
630 GNUNET_ATS_TEST_shutdown_topology (void);
631
632 /* end of file ats-testing.h */