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