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