Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a source file for model vivaldi. Write vivaldi_get_route.
[simgrid.git] / src / surf / surf_routing.c
1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <pcre.h>               /* regular expression library */
8
9 #include "simgrid/platf_interface.h" // platform creation API internal interface
10
11 #include "surf_routing_private.h"
12 #include "surf/surf_routing.h"
13 #include "surf/surfxml_parse_values.h"
14
15 xbt_lib_t host_lib;
16 int ROUTING_HOST_LEVEL; //Routing level
17 int     SURF_CPU_LEVEL;         //Surf cpu level
18 int SURF_WKS_LEVEL;             //Surf workstation level
19 int SIMIX_HOST_LEVEL;   //Simix level
20 int     MSG_HOST_LEVEL;         //Msg level
21 int     SD_HOST_LEVEL;          //Simdag level
22 int     COORD_HOST_LEVEL;       //Coordinates level
23 int NS3_HOST_LEVEL;             //host node for ns3
24
25 xbt_lib_t link_lib;
26 int SD_LINK_LEVEL;              //Simdag level
27 int SURF_LINK_LEVEL;    //Surf level
28 int NS3_LINK_LEVEL;             //link for ns3
29
30 xbt_lib_t as_router_lib;
31 int ROUTING_ASR_LEVEL;  //Routing level
32 int COORD_ASR_LEVEL;    //Coordinates level
33 int NS3_ASR_LEVEL;              //host node for ns3
34
35 static xbt_dict_t patterns = NULL;
36 static xbt_dict_t random_value = NULL;
37
38 /* Global vars */
39 routing_global_t global_routing = NULL;
40 routing_component_t current_routing = NULL;
41 model_type_t current_routing_model = NULL;
42
43 /* global parse functions */
44 xbt_dynar_t link_list = NULL;    /* temporary store of current list link of a route */
45 static const char *src = NULL;        /* temporary store the source name of a route */
46 static const char *dst = NULL;        /* temporary store the destination name of a route */
47 static char *gw_src = NULL;     /* temporary store the gateway source name of a route */
48 static char *gw_dst = NULL;     /* temporary store the gateway destination name of a route */
49 static double_f_cpvoid_t get_link_latency = NULL;
50 xbt_dict_t cluster_host_link = NULL; /* for tag cluster */
51
52 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
53
54 static void routing_parse_Speer(void);          /* peer bypass */
55 static void routing_parse_Srandom(void);        /* random bypass */
56 static void routing_parse_Erandom(void);        /* random bypass */
57
58 static char* replace_random_parameter(char * chaine);
59 static void clean_routing_after_parse(void);
60
61 /* this lines are only for replace use like index in the model table */
62 typedef enum {
63   SURF_MODEL_FULL = 0,
64   SURF_MODEL_FLOYD,
65   SURF_MODEL_DIJKSTRA,
66   SURF_MODEL_DIJKSTRACACHE,
67   SURF_MODEL_NONE,
68   SURF_MODEL_RULEBASED,
69   SURF_MODEL_VIVALDI,
70   SURF_MODEL_CLUSTER
71 } e_routing_types;
72
73 struct s_model_type routing_models[] = { {"Full",
74                                           "Full routing data (fast, large memory requirements, fully expressive)",
75                                           model_full_create,
76                                           model_full_load,
77                                           model_full_unload,
78                                           model_full_end},
79 {"Floyd",
80  "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
81  model_floyd_create, model_floyd_load, model_floyd_unload,
82  model_floyd_end},
83 {"Dijkstra",
84  "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
85  model_dijkstra_create, model_dijkstra_both_load,
86  model_dijkstra_both_unload, model_dijkstra_both_end},
87 {"DijkstraCache",
88  "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
89  model_dijkstracache_create, model_dijkstra_both_load,
90  model_dijkstra_both_unload, model_dijkstra_both_end},
91 {"none", "No routing (usable with Constant network only)",
92  model_none_create, model_none_load, model_none_unload, model_none_end},
93 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
94  model_none_load, model_none_unload, model_none_end},
95 {"Vivaldi", "Vivaldi routing", model_vivaldi_create,
96   model_none_load, model_none_unload, model_none_end},
97 {"Cluster", "Cluster routing", model_cluster_create,
98   model_none_load, model_none_unload, model_none_end},
99 {NULL, NULL, NULL, NULL, NULL, NULL}
100 };
101
102 /**
103  * \brief Add a "host" to the network element list
104  */
105 static void parse_S_host(const char *host_id, const char* coord)
106 {
107   network_element_info_t info = NULL;
108   if (current_routing->hierarchy == SURF_ROUTING_NULL)
109     current_routing->hierarchy = SURF_ROUTING_BASE;
110   xbt_assert(!xbt_lib_get_or_null(host_lib, host_id,ROUTING_HOST_LEVEL),
111               "Reading a host, processing unit \"%s\" already exists",
112               host_id);
113   xbt_assert(current_routing->set_processing_unit,
114               "no defined method \"set_processing_unit\" in \"%s\"",
115               current_routing->name);
116   (*(current_routing->set_processing_unit)) (current_routing, host_id);
117   info = xbt_new0(s_network_element_info_t, 1);
118   info->rc_component = current_routing;
119   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
120   xbt_lib_set(host_lib,host_id,ROUTING_HOST_LEVEL,(void *) info);
121   if (strcmp(coord,"")) {
122         if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
123     xbt_dynar_t ctn = xbt_str_split_str(coord, " ");
124     xbt_dynar_shrink(ctn, 0);
125     xbt_lib_set(host_lib,host_id,COORD_HOST_LEVEL,(void *) ctn);
126   }
127 }
128
129 static void parse_E_host(void)
130 {
131          xbt_dict_cursor_t cursor = NULL;
132           char *key;
133           char *elem;
134
135           xbt_dict_foreach(current_property_set, cursor, key, elem) {
136                   XBT_DEBUG("property : %s = %s",key,elem);
137                 }
138 }
139
140 /*
141  * \brief Add a host to the network element list from XML
142  */
143 static void parse_S_host_XML(sg_platf_host_cbarg_t h)
144 {
145         parse_S_host(h->V_host_id, h->V_host_coord);
146 }
147 static void parse_E_host_XML(void)
148 {
149         parse_E_host();
150 }
151
152 /*
153  * \brief Add a host to the network element list from lua script
154  */
155 static void parse_S_host_lua(const char *host_id, const char *coord)
156 {
157   parse_S_host(host_id, coord);
158 }
159
160
161 /**
162  * \brief Add a "router" to the network element list
163  */
164 static void parse_S_router(sg_platf_router_cbarg_t router)
165 {
166   network_element_info_t info = NULL;
167   if (current_routing->hierarchy == SURF_ROUTING_NULL)
168     current_routing->hierarchy = SURF_ROUTING_BASE;
169   xbt_assert(!xbt_lib_get_or_null(as_router_lib,router->V_router_id, ROUTING_ASR_LEVEL),
170               "Reading a router, processing unit \"%s\" already exists",
171               router->V_router_id);
172   xbt_assert(current_routing->set_processing_unit,
173               "no defined method \"set_processing_unit\" in \"%s\"",
174               current_routing->name);
175   (*(current_routing->set_processing_unit)) (current_routing,
176                   router->V_router_id);
177   info = xbt_new0(s_network_element_info_t, 1);
178   info->rc_component = current_routing;
179   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
180
181   xbt_lib_set(as_router_lib,router->V_router_id,ROUTING_ASR_LEVEL,(void *) info);
182   if (strcmp(A_surfxml_router_coordinates,"")) {
183         if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
184     xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_router_coordinates, " ");
185     xbt_dynar_shrink(ctn, 0);
186     xbt_lib_set(as_router_lib,router->V_router_id,COORD_ASR_LEVEL,(void *) ctn);
187   }
188 }
189
190 /**
191  * brief Add a "router" to the network element list from XML description
192  */
193 static void parse_S_router_lua(const char* router_id) {
194   s_sg_platf_router_cbarg_t router;
195   memset(&router,0,sizeof(router));
196         router.V_router_id = router_id;
197         router.V_router_coord = "";
198         return parse_S_router(&router);
199 }
200
201 /**
202  * \brief Set the endponints for a route
203  */
204 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
205 {
206   if (src != NULL && dst != NULL && link_list != NULL)
207     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
208            src_id, dst_id);
209   src = src_id;
210   dst = dst_id;
211   xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
212               "Some limits are null in the route between \"%s\" and \"%s\"",
213               src, dst);
214   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
215 }
216
217 /**
218  * \brief Set the endpoints for a route from XML
219  */
220 static void parse_S_route_new_and_endpoints_XML(void)
221 {
222   parse_S_route_new_and_endpoints(A_surfxml_route_src,
223                                   A_surfxml_route_dst);
224 }
225
226 /**
227  * \brief Set the endpoints for a route from lua
228  */
229 static void parse_S_route_new_and_endpoints_lua(const char *id_src, const char *id_dst)
230 {
231   parse_S_route_new_and_endpoints(id_src, id_dst);
232 }
233
234 /**
235  * \brief Set the endponints and gateways for a ASroute
236  */
237 static void parse_S_ASroute_new_and_endpoints(void)
238 {
239   if (src != NULL && dst != NULL && link_list != NULL)
240     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
241            A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
242   src = A_surfxml_ASroute_src;
243   dst = A_surfxml_ASroute_dst;
244   gw_src = A_surfxml_ASroute_gw_src;
245   gw_dst = A_surfxml_ASroute_gw_dst;
246   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
247               || strlen(gw_dst) > 0,
248               "Some limits are null in the route between \"%s\" and \"%s\"",
249               src, dst);
250   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
251 }
252
253 /**
254  * \brief Set the endponints for a bypassRoute
255  */
256 static void parse_S_bypassRoute_new_and_endpoints(void)
257 {
258   if (src != NULL && dst != NULL && link_list != NULL)
259     THROWF(arg_error, 0,
260            "Bypass Route between %s to %s can not be defined",
261            A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
262   src = A_surfxml_bypassRoute_src;
263   dst = A_surfxml_bypassRoute_dst;
264   gw_src = A_surfxml_bypassRoute_gw_src;
265   gw_dst = A_surfxml_bypassRoute_gw_dst;
266   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
267               || strlen(gw_dst) > 0,
268               "Some limits are null in the route between \"%s\" and \"%s\"",
269               src, dst);
270   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
271 }
272
273 /**
274  * \brief Set a new link on the actual list of link for a route or ASroute
275  */
276 static void parse_E_link_ctn_new_elem(const char *link_id)
277 {
278   char *val;
279   val = xbt_strdup(link_id);
280   xbt_dynar_push(link_list, &val);
281 }
282
283 /**
284  * \brief Set a new link on the actual list of link for a route or ASroute from XML
285  */
286
287 static void parse_E_link_ctn_new_elem_XML(void)
288 {
289   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
290     parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
291   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
292     char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
293     parse_E_link_ctn_new_elem(link_id);
294     free(link_id);
295   }
296   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
297     char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
298     parse_E_link_ctn_new_elem(link_id);
299     free(link_id);
300   }
301 }
302
303 /**
304  * \brief Set a new link on the actual list of link for a route or ASroute from lua
305  */
306 static void parse_E_link_c_ctn_new_elem_lua(const char *link_id)
307 {
308   parse_E_link_ctn_new_elem(link_id);
309 }
310
311 /**
312  * \brief Store the route by calling the set_route function of the current routing component
313  */
314 static void parse_E_route_store_route(void)
315 {
316   name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
317   route->generic_route.link_list = link_list;
318   xbt_assert(current_routing->set_route,
319               "no defined method \"set_route\" in \"%s\"",
320               current_routing->name);
321   (*(current_routing->set_route)) (current_routing, src, dst, route);
322   link_list = NULL;
323   src = NULL;
324   dst = NULL;
325 }
326
327 /**
328  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
329  */
330 static void parse_E_ASroute_store_route(void)
331 {
332   name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
333   e_route->generic_route.link_list = link_list;
334   e_route->src_gateway = xbt_strdup(gw_src);
335   e_route->dst_gateway = xbt_strdup(gw_dst);
336   xbt_assert(current_routing->set_ASroute,
337               "no defined method \"set_ASroute\" in \"%s\"",
338               current_routing->name);
339   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
340   link_list = NULL;
341   src = NULL;
342   dst = NULL;
343   gw_src = NULL;
344   gw_dst = NULL;
345 }
346
347 /**
348  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
349  */
350 static void parse_E_bypassRoute_store_route(void)
351 {
352   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
353   e_route->generic_route.link_list = link_list;
354   e_route->src_gateway = xbt_strdup(gw_src);
355   e_route->dst_gateway = xbt_strdup(gw_dst);
356   xbt_assert(current_routing->set_bypassroute,
357               "no defined method \"set_bypassroute\" in \"%s\"",
358               current_routing->name);
359   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
360                                          e_route);
361   link_list = NULL;
362   src = NULL;
363   dst = NULL;
364   gw_src = NULL;
365   gw_dst = NULL;
366 }
367
368 /**
369  * \brief Make a new routing component to the platform
370  *
371  * Add a new autonomous system to the platform. Any elements (such as host,
372  * router or sub-AS) added after this call and before the corresponding call
373  * to sg_platf_new_AS_close() will be added to this AS.
374  *
375  * Once this function was called, the configuration concerning the used
376  * models cannot be changed anymore.
377  *
378  * @param AS_id name of this autonomous system. Must be uniq in the platform
379  * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
380  */
381 void sg_platf_new_AS_open(const char *AS_id, const char *wanted_routing_type)
382 {
383   routing_component_t new_routing;
384   model_type_t model = NULL;
385   int cpt;
386
387   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag and before the first <AS>-like */
388
389   /* search the routing model */
390   for (cpt = 0; routing_models[cpt].name; cpt++)
391     if (!strcmp(wanted_routing_type, routing_models[cpt].name))
392       model = &routing_models[cpt];
393   /* if its not exist, error */
394   if (model == NULL) {
395     fprintf(stderr, "Routing model %s not found. Existing models:\n",
396             wanted_routing_type);
397     for (cpt = 0; routing_models[cpt].name; cpt++)
398         fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
399                 routing_models[cpt].desc);
400     xbt_die(NULL);
401   }
402
403   /* make a new routing component */
404   new_routing = (routing_component_t) (*(model->create)) ();
405   new_routing->routing = model;
406   new_routing->hierarchy = SURF_ROUTING_NULL;
407   new_routing->name = xbt_strdup(AS_id);
408   new_routing->routing_sons = xbt_dict_new();
409
410   if (current_routing == NULL && global_routing->root == NULL) {
411
412     /* it is the first one */
413     new_routing->routing_father = NULL;
414     global_routing->root = new_routing;
415
416   } else if (current_routing != NULL && global_routing->root != NULL) {
417
418     xbt_assert(!xbt_dict_get_or_null
419                 (current_routing->routing_sons, AS_id),
420                 "The AS \"%s\" already exists", AS_id);
421     /* it is a part of the tree */
422     new_routing->routing_father = current_routing;
423     /* set the father behavior */
424     if (current_routing->hierarchy == SURF_ROUTING_NULL)
425       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
426     /* add to the sons dictionary */
427     xbt_dict_set(current_routing->routing_sons, AS_id,
428                  (void *) new_routing, NULL);
429     /* add to the father element list */
430     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
431     /* unload the prev parse rules */
432     (*(current_routing->routing->unload)) ();
433
434   } else {
435     THROWF(arg_error, 0, "All defined components must be belong to a AS");
436   }
437   /* set the new parse rules */
438   (*(new_routing->routing->load)) ();
439   /* set the new current component of the tree */
440   current_routing = new_routing;
441 }
442
443 /**
444  * \brief Specify that the current description of AS is finished
445  *
446  * Once you've declared all the content of your AS, you have to close
447  * it with this call. Your AS is not usable until you call this function.
448  *
449  * @fixme: this call is not as robust as wanted: bad things WILL happen
450  * if you call it twice for the same AS, or if you forget calling it, or
451  * even if you add stuff to a closed AS
452  *
453  */
454 void sg_platf_new_AS_close() {
455
456   if (current_routing == NULL) {
457     THROWF(arg_error, 0, "Close an AS, but none was under construction");
458   } else {
459     network_element_info_t info = NULL;
460     xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
461                 "The AS \"%s\" already exists",current_routing->name);
462     info = xbt_new0(s_network_element_info_t, 1);
463     info->rc_component = current_routing->routing_father;
464     info->rc_type = SURF_NETWORK_ELEMENT_AS;
465     xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
466
467     (*(current_routing->routing->unload)) ();
468     (*(current_routing->routing->end)) ();
469     current_routing = current_routing->routing_father;
470     if (current_routing != NULL)
471       (*(current_routing->routing->load)) ();
472   }
473 }
474
475 /* Aux Business methods */
476
477 /**
478  * \brief Get the AS name of the element
479  *
480  * \param name the host name
481  *
482  */
483 static char* elements_As_name(const char *name)
484 {
485   routing_component_t as_comp;
486
487   /* (1) find the as where the host is located */
488   as_comp = ((network_element_info_t)
489             xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
490   return as_comp->name;
491 }
492
493
494 /**
495  * \brief Get the AS father and the first elements of the chain
496  *
497  * \param src the source host name 
498  * \param dst the destination host name
499  * 
500  * Get the common father of the to processing units, and the first different 
501  * father in the chain
502  */
503 static void elements_father(const char *src, const char *dst,
504                             routing_component_t *res_father,
505                             routing_component_t *res_src,
506                             routing_component_t *res_dst)
507 {
508   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
509 #define ELEMENTS_FATHER_MAXDEPTH 16 /* increase if it is not enough */
510   routing_component_t src_as, dst_as;
511   routing_component_t path_src[ELEMENTS_FATHER_MAXDEPTH];
512   routing_component_t path_dst[ELEMENTS_FATHER_MAXDEPTH];
513   int index_src = 0;
514   int index_dst = 0;
515   routing_component_t current;
516   routing_component_t current_src;
517   routing_component_t current_dst;
518   routing_component_t father;
519
520   /* (1) find the as where the src and dst are located */
521   network_element_info_t src_data = xbt_lib_get_or_null(host_lib, src,
522                                                         ROUTING_HOST_LEVEL);
523   network_element_info_t dst_data = xbt_lib_get_or_null(host_lib, dst,
524                                                         ROUTING_HOST_LEVEL);
525   if (!src_data)
526     src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
527   if (!dst_data)
528     dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
529   src_as = src_data->rc_component;
530   dst_as = dst_data->rc_component;
531
532   xbt_assert(src_as && dst_as,
533              "Ask for route \"from\"(%s) or \"to\"(%s) no found", src, dst);
534
535   /* (2) find the path to the root routing component */
536   for (current = src_as ; current != NULL ; current = current->routing_father) {
537     if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
538       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
539     path_src[index_src++] = current;
540   }
541   for (current = dst_as ; current != NULL ; current = current->routing_father) {
542     if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
543       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
544     path_dst[index_dst++] = current;
545   }
546
547   /* (3) find the common father */
548   do {
549     current_src = path_src[--index_src];
550     current_dst = path_dst[--index_dst];
551   } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
552
553   /* (4) they are not in the same routing component, make the path */
554   if (current_src == current_dst)
555     father = current_src;
556   else
557     father = path_src[index_src + 1];
558
559   /* (5) result generation */
560   *res_father = father;         /* first the common father of src and dst */
561   *res_src = current_src;       /* second the first different father of src */
562   *res_dst = current_dst;       /* three  the first different father of dst */
563
564 #undef ELEMENTS_FATHER_MAXDEPTH
565 }
566
567 /* Global Business methods */
568
569 /**
570  * \brief Recursive function for get_route_latency
571  *
572  * \param src the source host name 
573  * \param dst the destination host name
574  * \param *e_route the route where the links are stored
575  * \param *latency the latency, if needed
576  * 
577  * This function is called by "get_route" and "get_latency". It allows to walk
578  * recursively through the routing components tree.
579  */
580 static void _get_route_latency(const char *src, const char *dst,
581                                xbt_dynar_t *route, double *latency)
582 {
583   XBT_DEBUG("Solve route/latency  \"%s\" to \"%s\"", src, dst);
584   xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
585
586   routing_component_t common_father;
587   routing_component_t src_father;
588   routing_component_t dst_father;
589   elements_father(src, dst, &common_father, &src_father, &dst_father);
590
591   if (src_father == dst_father) { /* SURF_ROUTING_BASE */
592
593     route_extended_t e_route = NULL;
594     if (route) {
595       e_route = common_father->get_route(common_father, src, dst);
596       xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
597       *route = e_route->generic_route.link_list;
598     }
599     if (latency) {
600       *latency = common_father->get_latency(common_father, src, dst, e_route);
601       xbt_assert(*latency >= 0.0,
602                  "latency error on route between \"%s\" and \"%s\"", src, dst);
603     }
604     if (e_route) {
605       xbt_free(e_route->src_gateway);
606       xbt_free(e_route->dst_gateway);
607       xbt_free(e_route);
608     }
609
610   } else {                      /* SURF_ROUTING_RECURSIVE */
611
612     route_extended_t e_route_bypass = NULL;
613     if (common_father->get_bypass_route)
614       e_route_bypass = common_father->get_bypass_route(common_father, src, dst);
615
616     xbt_assert(!latency || !e_route_bypass,
617                "Bypass cannot work yet with get_latency");
618
619     route_extended_t e_route_cnt = e_route_bypass
620       ? e_route_bypass
621       : common_father->get_route(common_father,
622                                src_father->name, dst_father->name);
623
624     xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
625                src_father->name, dst_father->name);
626
627     xbt_assert((e_route_cnt->src_gateway == NULL) ==
628                (e_route_cnt->dst_gateway == NULL),
629                "bad gateway for route between \"%s\" and \"%s\"", src, dst);
630
631     if (route) {
632       *route = xbt_dynar_new(global_routing->size_of_link, NULL);
633     }
634     if (latency) {
635       *latency = common_father->get_latency(common_father,
636                                             src_father->name, dst_father->name,
637                                             e_route_cnt);
638       xbt_assert(*latency >= 0.0,
639                  "latency error on route between \"%s\" and \"%s\"",
640                  src_father->name, dst_father->name);
641     }
642
643     void *link;
644     unsigned int cpt;
645
646     if (strcmp(src, e_route_cnt->src_gateway)) {
647       double latency_src;
648       xbt_dynar_t route_src;
649
650       _get_route_latency(src, e_route_cnt->src_gateway,
651                          (route ? &route_src : NULL),
652                          (latency ? &latency_src : NULL));
653       if (route) {
654         xbt_assert(route_src, "no route between \"%s\" and \"%s\"",
655                    src, e_route_cnt->src_gateway);
656         xbt_dynar_foreach(route_src, cpt, link) {
657           xbt_dynar_push(*route, &link);
658         }
659         xbt_dynar_free(&route_src);
660       }
661       if (latency) {
662         xbt_assert(latency_src >= 0.0,
663                    "latency error on route between \"%s\" and \"%s\"",
664                    src, e_route_cnt->src_gateway);
665         *latency += latency_src;
666       }
667     }
668
669     if (route) {
670       xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
671         xbt_dynar_push(*route, &link);
672       }
673     }
674
675     if (strcmp(e_route_cnt->dst_gateway, dst)) {
676       double latency_dst;
677       xbt_dynar_t route_dst;
678
679       _get_route_latency(e_route_cnt->dst_gateway, dst,
680                          (route ? &route_dst : NULL),
681                          (latency ? &latency_dst : NULL));
682       if (route) {
683         xbt_assert(route_dst, "no route between \"%s\" and \"%s\"",
684                    e_route_cnt->dst_gateway, dst);
685         xbt_dynar_foreach(route_dst, cpt, link) {
686           xbt_dynar_push(*route, &link);
687         }
688         xbt_dynar_free(&route_dst);
689       }
690       if (latency) {
691         xbt_assert(latency_dst >= 0.0,
692                    "latency error on route between \"%s\" and \"%s\"",
693                    e_route_cnt->dst_gateway, dst);
694         *latency += latency_dst;
695       }
696     }
697
698     generic_free_extended_route(e_route_cnt);
699   }
700 }
701
702 /**
703  * \brief Generic function for get_route, get_route_no_cleanup, and get_latency
704  */
705 static void get_route_latency(const char *src, const char *dst,
706                               xbt_dynar_t *route, double *latency, int cleanup)
707 {
708   _get_route_latency(src, dst, route, latency);
709   xbt_assert(!route || *route, "no route between \"%s\" and \"%s\"", src, dst);
710   xbt_assert(!latency || *latency >= 0.0,
711              "latency error on route between \"%s\" and \"%s\"", src, dst);
712   if (route) {
713     xbt_dynar_free(&global_routing->last_route);
714     global_routing->last_route = cleanup ? *route : NULL;
715   }
716 }
717
718 /**
719  * \brief Generic method: find a route between hosts
720  *
721  * \param src the source host name 
722  * \param dst the destination host name
723  * 
724  * walk through the routing components tree and find a route between hosts
725  * by calling the differents "get_route" functions in each routing component.
726  * No need to free the returned dynar. It will be freed at the next call.
727  */
728 static xbt_dynar_t get_route(const char *src, const char *dst)
729 {
730   xbt_dynar_t route = NULL;
731   get_route_latency(src, dst, &route, NULL, 1);
732   return route;
733 }
734
735 /**
736  * \brief Generic method: find a route between hosts
737  *
738  * \param src the source host name
739  * \param dst the destination host name
740  *
741  * same as get_route, but return NULL if any exception is raised.
742  */
743 static xbt_dynar_t get_route_or_null(const char *src, const char *dst)
744 {
745   xbt_dynar_t route = NULL;
746   xbt_ex_t exception;
747   TRY {
748     get_route_latency(src, dst, &route, NULL, 1);
749   }CATCH(exception) {
750     xbt_ex_free(exception);
751     return NULL;
752   }
753   return route;
754 }
755
756 /**
757  * \brief Generic method: find a route between hosts
758  *
759  * \param src the source host name
760  * \param dst the destination host name
761  *
762  * walk through the routing components tree and find a route between hosts
763  * by calling the differents "get_route" functions in each routing component.
764  * Leaves the caller the responsability to clean the returned dynar.
765  */
766 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
767 {
768   xbt_dynar_t route = NULL;
769   get_route_latency(src, dst, &route, NULL, 0);
770   return route;
771 }
772
773 /*Get Latency*/
774 static double get_latency(const char *src, const char *dst)
775 {
776   double latency = -1.0;
777   get_route_latency(src, dst, NULL, &latency, 0);
778   return latency;
779 }
780
781 static int surf_parse_models_setup_already_called=0;
782 /* Call the last initialization functions, that must be called after the
783  * <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
784  */
785 void surf_parse_models_setup()
786 {
787   if (surf_parse_models_setup_already_called)
788     return;
789   surf_parse_models_setup_already_called=1;
790   routing_parse_Erandom();
791   surf_config_models_setup();
792 }
793
794
795 /**
796  * \brief Recursive function for finalize
797  *
798  * \param rc the source host name 
799  * 
800  * This fuction is call by "finalize". It allow to finalize the 
801  * AS or routing components. It delete all the structures.
802  */
803 static void _finalize(routing_component_t rc)
804 {
805   if (rc) {
806     xbt_dict_cursor_t cursor = NULL;
807     char *key;
808     routing_component_t elem;
809     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
810       _finalize(elem);
811     }
812     xbt_dict_t tmp_sons = rc->routing_sons;
813     char *tmp_name = rc->name;
814     xbt_dict_free(&tmp_sons);
815     xbt_free(tmp_name);
816     xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
817                 current_routing->name);
818     (*(rc->finalize)) (rc);
819   }
820 }
821
822 /**
823  * \brief Generic method: delete all the routing structures
824  * 
825  * walk through the routing components tree and delete the structures
826  * by calling the differents "finalize" functions in each routing component
827  */
828 static void finalize(void)
829 {
830   /* delete recursibly all the tree */
831   _finalize(global_routing->root);
832   /* delete last_route */
833   xbt_dynar_free(&(global_routing->last_route));
834   /* delete global routing structure */
835   xbt_free(global_routing);
836   /* make sure that we will reinit the models while loading the platf once reinited -- HACK but there is no proper surf_routing_init() */
837   surf_parse_models_setup_already_called = 0;
838 }
839
840 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
841 {
842   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
843
844   //adding my one link routes
845   unsigned int cpt;
846   void *link;
847   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
848   if (onelink_mine) {
849     xbt_dynar_foreach(onelink_mine, cpt, link) {
850       xbt_dynar_push(ret, &link);
851     }
852   }
853   //recursing
854   char *key;
855   xbt_dict_cursor_t cursor = NULL;
856   routing_component_t rc_child;
857   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
858     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
859     if (onelink_child) {
860       xbt_dynar_foreach(onelink_child, cpt, link) {
861         xbt_dynar_push(ret, &link);
862       }
863     }
864   }
865   return ret;
866 }
867
868 static xbt_dynar_t get_onelink_routes(void)
869 {
870   return recursive_get_onelink_routes(global_routing->root);
871 }
872
873 e_surf_network_element_type_t get_network_element_type(const char
874                                                               *name)
875 {
876   network_element_info_t rc = NULL;
877
878   rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
879   if(rc) return rc->rc_type;
880
881   rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
882   if(rc) return rc->rc_type;
883
884   return SURF_NETWORK_ELEMENT_NULL;
885 }
886
887 /**
888  * \brief Generic method: create the global routing schema
889  * 
890  * Make a global routing structure and set all the parsing functions.
891  */
892 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
893 {
894   /* config the uniq global routing */
895   global_routing = xbt_new0(s_routing_global_t, 1);
896   global_routing->root = NULL;
897   global_routing->get_route = get_route;
898   global_routing->get_route_or_null = get_route_or_null;
899   global_routing->get_latency = get_latency;
900   global_routing->get_route_no_cleanup = get_route_no_cleanup;
901   global_routing->get_onelink_routes = get_onelink_routes;
902   global_routing->get_route_latency = get_route_latency;
903   global_routing->get_network_element_type = get_network_element_type;
904   global_routing->finalize = finalize;
905   global_routing->loopback = loopback;
906   global_routing->size_of_link = size_of_links;
907   global_routing->last_route = NULL;
908   get_link_latency = get_link_latency_fun;
909   /* no current routing at moment */
910   current_routing = NULL;
911
912   /* parse generic elements */
913   sg_platf_host_add_cb(parse_S_host_XML);
914   surfxml_add_callback(ETag_surfxml_host_cb_list, &parse_E_host_XML);
915   sg_platf_router_add_cb(parse_S_router);
916
917   surfxml_add_callback(STag_surfxml_route_cb_list,
918                        &parse_S_route_new_and_endpoints_XML);
919   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
920                        &parse_S_ASroute_new_and_endpoints);
921   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
922                        &parse_S_bypassRoute_new_and_endpoints);
923
924   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
925                        &parse_E_link_ctn_new_elem_XML);
926
927   surfxml_add_callback(ETag_surfxml_route_cb_list,
928                        &parse_E_route_store_route);
929   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
930                        &parse_E_ASroute_store_route);
931   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
932                        &parse_E_bypassRoute_store_route);
933
934   surfxml_add_callback(STag_surfxml_cluster_cb_list,
935                        &routing_parse_Scluster);
936
937   surfxml_add_callback(STag_surfxml_peer_cb_list,
938                          &routing_parse_Speer);
939
940   surfxml_add_callback(ETag_surfxml_platform_cb_list,
941                                                   &clean_routing_after_parse);
942
943 #ifdef HAVE_TRACING
944   instr_routing_define_callbacks();
945 #endif
946 }
947
948 void surf_parse_add_callback_config(void)
949 {
950         surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties_XML);
951         surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
952 }
953
954 /* ************************************************** */
955 /* ********** PATERN FOR NEW ROUTING **************** */
956
957 /* The minimal configuration of a new routing model need the next functions,
958  * also you need to set at the start of the file, the new model in the model
959  * list. Remember keep the null ending of the list.
960  */
961 /*** Routing model structure ***/
962 // typedef struct {
963 //   s_routing_component_t generic_routing;
964 //   /* things that your routing model need */
965 // } s_routing_component_NEW_t,*routing_component_NEW_t;
966
967 /*** Parse routing model functions ***/
968 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
969 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
970 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
971 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
972 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
973
974 /*** Business methods ***/
975 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
976 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
977 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
978
979 /*** Creation routing model functions ***/
980 // static void* model_NEW_create(void) {
981 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
982 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
983 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
984 //   new_component->generic_routing.set_route = model_NEW_set_route;
985 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
986 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
987 //   new_component->generic_routing.get_route = NEW_get_route;
988 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
989 //   new_component->generic_routing.finalize = NEW_finalize;
990 //   /* initialization of internal structures */
991 //   return new_component;
992 // } /* mandatory */
993 // static void  model_NEW_load(void) {}   /* mandatory */
994 // static void  model_NEW_unload(void) {} /* mandatory */
995 // static void  model_NEW_end(void) {}    /* mandatory */
996
997 /* ************************************************************************** */
998 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
999
1000 void generic_set_processing_unit(routing_component_t rc,
1001                                         const char *name)
1002 {
1003   XBT_DEBUG("Load process unit \"%s\"", name);
1004   int *id = xbt_new0(int, 1);
1005   xbt_dict_t _to_index;
1006   _to_index = current_routing->to_index;
1007   *id = xbt_dict_length(_to_index);
1008   xbt_dict_set(_to_index, name, id, xbt_free);
1009 }
1010
1011 void generic_set_autonomous_system(routing_component_t rc,
1012                                           const char *name)
1013 {
1014   XBT_DEBUG("Load Autonomous system \"%s\"", name);
1015   int *id = xbt_new0(int, 1);
1016   xbt_dict_t _to_index;
1017   _to_index = current_routing->to_index;
1018   *id = xbt_dict_length(_to_index);
1019   xbt_dict_set(_to_index, name, id, xbt_free);
1020 }
1021
1022 int surf_pointer_resource_cmp(const void *a, const void *b)
1023 {
1024   return a != b;
1025 }
1026
1027 int surf_link_resource_cmp(const void *a, const void *b)
1028 {
1029   return !!memcmp(a,b,global_routing->size_of_link);
1030 }
1031
1032 void generic_set_bypassroute(routing_component_t rc,
1033                                     const char *src, const char *dst,
1034                                     route_extended_t e_route)
1035 {
1036   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
1037   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1038   char *route_name;
1039
1040   route_name = bprintf("%s#%s", src, dst);
1041   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
1042               "Invalid count of links, must be greater than zero (%s,%s)",
1043               src, dst);
1044   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
1045               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
1046               src, e_route->src_gateway, dst, e_route->dst_gateway);
1047
1048   route_extended_t new_e_route =
1049       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
1050   xbt_dynar_free(&(e_route->generic_route.link_list));
1051   xbt_free(e_route);
1052
1053   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
1054                (void (*)(void *)) generic_free_extended_route);
1055   xbt_free(route_name);
1056 }
1057
1058 /* ************************************************************************** */
1059 /* *********************** GENERIC BUSINESS METHODS ************************* */
1060
1061 double generic_get_link_latency(routing_component_t rc,
1062                                        const char *src, const char *dst,
1063                                        route_extended_t route)
1064 {
1065         int need_to_clean = route?0:1;
1066         void * link;
1067         unsigned int i;
1068         double latency = 0.0;
1069
1070         route = route?route:rc->get_route(rc,src,dst);
1071
1072         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1073                 latency += get_link_latency(link);
1074         }
1075         if(need_to_clean) generic_free_extended_route(route);
1076   return latency;
1077 }
1078
1079 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1080 {
1081   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1082 }
1083
1084 route_extended_t generic_get_bypassroute(routing_component_t rc,
1085                                                 const char *src,
1086                                                 const char *dst)
1087 {
1088   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1089   routing_component_t src_as, dst_as;
1090   int index_src, index_dst;
1091   xbt_dynar_t path_src = NULL;
1092   xbt_dynar_t path_dst = NULL;
1093   routing_component_t current = NULL;
1094   routing_component_t *current_src = NULL;
1095   routing_component_t *current_dst = NULL;
1096
1097   /* (1) find the as where the src and dst are located */
1098   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1099   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1100   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1101   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1102
1103   if(src_data == NULL || dst_data == NULL)
1104           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1105                      src, dst, rc->name);
1106
1107   src_as = ((network_element_info_t)src_data)->rc_component;
1108   dst_as = ((network_element_info_t)dst_data)->rc_component;
1109
1110   /* (2) find the path to the root routing component */
1111   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1112   current = src_as;
1113   while (current != NULL) {
1114     xbt_dynar_push(path_src, &current);
1115     current = current->routing_father;
1116   }
1117   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1118   current = dst_as;
1119   while (current != NULL) {
1120     xbt_dynar_push(path_dst, &current);
1121     current = current->routing_father;
1122   }
1123
1124   /* (3) find the common father */
1125   index_src = path_src->used - 1;
1126   index_dst = path_dst->used - 1;
1127   current_src = xbt_dynar_get_ptr(path_src, index_src);
1128   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1129   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1130     xbt_dynar_pop_ptr(path_src);
1131     xbt_dynar_pop_ptr(path_dst);
1132     index_src--;
1133     index_dst--;
1134     current_src = xbt_dynar_get_ptr(path_src, index_src);
1135     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1136   }
1137
1138   int max_index_src = path_src->used - 1;
1139   int max_index_dst = path_dst->used - 1;
1140
1141   int max_index = max(max_index_src, max_index_dst);
1142   int i, max;
1143
1144   route_extended_t e_route_bypass = NULL;
1145
1146   for (max = 0; max <= max_index; max++) {
1147     for (i = 0; i < max; i++) {
1148       if (i <= max_index_src && max <= max_index_dst) {
1149         char *route_name = bprintf("%s#%s",
1150                                    (*(routing_component_t *)
1151                                     (xbt_dynar_get_ptr
1152                                      (path_src, i)))->name,
1153                                    (*(routing_component_t *)
1154                                     (xbt_dynar_get_ptr
1155                                      (path_dst, max)))->name);
1156         e_route_bypass =
1157             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1158         xbt_free(route_name);
1159       }
1160       if (e_route_bypass)
1161         break;
1162       if (max <= max_index_src && i <= max_index_dst) {
1163         char *route_name = bprintf("%s#%s",
1164                                    (*(routing_component_t *)
1165                                     (xbt_dynar_get_ptr
1166                                      (path_src, max)))->name,
1167                                    (*(routing_component_t *)
1168                                     (xbt_dynar_get_ptr
1169                                      (path_dst, i)))->name);
1170         e_route_bypass =
1171             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1172         xbt_free(route_name);
1173       }
1174       if (e_route_bypass)
1175         break;
1176     }
1177
1178     if (e_route_bypass)
1179       break;
1180
1181     if (max <= max_index_src && max <= max_index_dst) {
1182       char *route_name = bprintf("%s#%s",
1183                                  (*(routing_component_t *)
1184                                   (xbt_dynar_get_ptr
1185                                    (path_src, max)))->name,
1186                                  (*(routing_component_t *)
1187                                   (xbt_dynar_get_ptr
1188                                    (path_dst, max)))->name);
1189       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1190       xbt_free(route_name);
1191     }
1192     if (e_route_bypass)
1193       break;
1194   }
1195
1196   xbt_dynar_free(&path_src);
1197   xbt_dynar_free(&path_dst);
1198
1199   route_extended_t new_e_route = NULL;
1200
1201   if (e_route_bypass) {
1202     void *link;
1203     unsigned int cpt = 0;
1204     new_e_route = xbt_new0(s_route_extended_t, 1);
1205     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1206     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1207     new_e_route->generic_route.link_list =
1208         xbt_dynar_new(global_routing->size_of_link, NULL);
1209     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1210       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1211     }
1212   }
1213
1214   return new_e_route;
1215 }
1216
1217 /* ************************************************************************** */
1218 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1219
1220 route_t
1221 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1222                            void *data, int order)
1223 {
1224
1225   char *link_name;
1226   route_t new_route;
1227   unsigned int cpt;
1228   xbt_dynar_t links = NULL, links_id = NULL;
1229
1230   new_route = xbt_new0(s_route_t, 1);
1231   new_route->link_list =
1232       xbt_dynar_new(global_routing->size_of_link, NULL);
1233
1234   xbt_assert(hierarchy == SURF_ROUTING_BASE,
1235               "the hierarchy type is not SURF_ROUTING_BASE");
1236
1237   links = ((route_t) data)->link_list;
1238
1239
1240   links_id = new_route->link_list;
1241
1242   xbt_dynar_foreach(links, cpt, link_name) {
1243
1244     void *link =
1245                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1246     if (link) {
1247       if (order)
1248         xbt_dynar_push(links_id, &link);
1249       else
1250         xbt_dynar_unshift(links_id, &link);
1251     } else
1252       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1253   }
1254
1255   return new_route;
1256 }
1257
1258 route_extended_t
1259 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1260                            void *data, int order)
1261 {
1262
1263   char *link_name;
1264   route_extended_t e_route, new_e_route;
1265   route_t route;
1266   unsigned int cpt;
1267   xbt_dynar_t links = NULL, links_id = NULL;
1268
1269   new_e_route = xbt_new0(s_route_extended_t, 1);
1270   new_e_route->generic_route.link_list =
1271       xbt_dynar_new(global_routing->size_of_link, NULL);
1272   new_e_route->src_gateway = NULL;
1273   new_e_route->dst_gateway = NULL;
1274
1275   xbt_assert(hierarchy == SURF_ROUTING_BASE
1276               || hierarchy == SURF_ROUTING_RECURSIVE,
1277               "the hierarchy type is not defined");
1278
1279   if (hierarchy == SURF_ROUTING_BASE) {
1280
1281     route = (route_t) data;
1282     links = route->link_list;
1283
1284   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1285
1286     e_route = (route_extended_t) data;
1287     xbt_assert(e_route->src_gateway
1288                 && e_route->dst_gateway, "bad gateway, is null");
1289     links = e_route->generic_route.link_list;
1290
1291     /* remeber not erase the gateway names */
1292     new_e_route->src_gateway = strdup(e_route->src_gateway);
1293     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1294   }
1295
1296   links_id = new_e_route->generic_route.link_list;
1297
1298   xbt_dynar_foreach(links, cpt, link_name) {
1299
1300     void *link =
1301                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1302     if (link) {
1303       if (order)
1304         xbt_dynar_push(links_id, &link);
1305       else
1306         xbt_dynar_unshift(links_id, &link);
1307     } else
1308       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1309   }
1310
1311   return new_e_route;
1312 }
1313
1314 void generic_free_route(route_t route)
1315 {
1316   if (route) {
1317     xbt_dynar_free(&(route->link_list));
1318     xbt_free(route);
1319   }
1320 }
1321
1322 void generic_free_extended_route(route_extended_t e_route)
1323 {
1324   if (e_route) {
1325     xbt_dynar_free(&(e_route->generic_route.link_list));
1326     if (e_route->src_gateway)
1327       xbt_free(e_route->src_gateway);
1328     if (e_route->dst_gateway)
1329       xbt_free(e_route->dst_gateway);
1330     xbt_free(e_route);
1331   }
1332 }
1333
1334 static routing_component_t generic_as_exist(routing_component_t find_from,
1335                                             routing_component_t to_find)
1336 {
1337   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1338   xbt_dict_cursor_t cursor = NULL;
1339   char *key;
1340   int found = 0;
1341   routing_component_t elem;
1342   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1343     if (to_find == elem || generic_as_exist(elem, to_find)) {
1344       found = 1;
1345       break;
1346     }
1347   }
1348   if (found)
1349     return to_find;
1350   return NULL;
1351 }
1352
1353 routing_component_t
1354 generic_autonomous_system_exist(routing_component_t rc, char *element)
1355 {
1356   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1357   routing_component_t element_as, result, elem;
1358   xbt_dict_cursor_t cursor = NULL;
1359   char *key;
1360   element_as = ((network_element_info_t)
1361                 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1362   result = ((routing_component_t) - 1);
1363   if (element_as != rc)
1364     result = generic_as_exist(rc, element_as);
1365
1366   int found = 0;
1367   if (result) {
1368     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1369       found = !strcmp(elem->name, element);
1370       if (found)
1371         break;
1372     }
1373     if (found)
1374       return element_as;
1375   }
1376   return NULL;
1377 }
1378
1379 routing_component_t
1380 generic_processing_units_exist(routing_component_t rc, char *element)
1381 {
1382   routing_component_t element_as;
1383   element_as = ((network_element_info_t)
1384                 xbt_lib_get_or_null(host_lib,
1385                  element, ROUTING_HOST_LEVEL))->rc_component;
1386   if (element_as == rc)
1387     return element_as;
1388   return generic_as_exist(rc, element_as);
1389 }
1390
1391 void generic_src_dst_check(routing_component_t rc, const char *src,
1392                                   const char *dst)
1393 {
1394
1395   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1396   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1397   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1398   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1399
1400   if(src_data == NULL || dst_data == NULL)
1401           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1402                      src, dst, rc->name);
1403
1404   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1405   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1406
1407   if(src_as != dst_as)
1408           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1409               src, src_as->name, dst, dst_as->name);
1410   if(rc != dst_as)
1411          xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1412      src,dst,src_as->name, dst_as->name,rc->name);
1413 }
1414
1415 void routing_parse_Scluster(void)
1416 {
1417   if(!cluster_host_link)
1418           cluster_host_link = xbt_dict_new();
1419
1420   static int AX_ptr = 0;
1421   char *host_id, *groups, *link_id = NULL;
1422
1423   s_sg_platf_host_cbarg_t host;
1424
1425   if( strcmp(struct_cluster->V_cluster_availability_file,"")
1426           || strcmp(struct_cluster->V_cluster_state_file,"") )
1427   {
1428           if(xbt_dict_size(patterns)==0)
1429                   patterns = xbt_dict_new();
1430           xbt_dict_set(patterns,"id",struct_cluster->V_cluster_id,NULL);
1431           xbt_dict_set(patterns,"prefix",struct_cluster->V_cluster_prefix,NULL);
1432           xbt_dict_set(patterns,"suffix",struct_cluster->V_cluster_suffix,NULL);
1433   }
1434
1435   unsigned int iter;
1436   int start, end, i;
1437   xbt_dynar_t radical_elements;
1438   xbt_dynar_t radical_ends;
1439
1440   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1441   static unsigned int surfxml_buffer_stack_stack[1024];
1442
1443   surfxml_buffer_stack_stack[0] = 0;
1444   surfxml_bufferstack_push(1);
1445
1446   SURFXML_BUFFER_SET(AS_id, struct_cluster->V_cluster_id);
1447   SURFXML_BUFFER_SET(AS_routing, "Cluster");
1448   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->V_cluster_id);
1449   SURFXML_START_TAG(AS);
1450
1451   //Make all hosts
1452   radical_elements = xbt_str_split(struct_cluster->V_cluster_radical, ",");
1453   xbt_dynar_foreach(radical_elements, iter, groups) {
1454     memset(&host,0,sizeof(host));
1455
1456     radical_ends = xbt_str_split(groups, "-");
1457     switch (xbt_dynar_length(radical_ends)) {
1458     case 1:
1459                 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1460                 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, start, struct_cluster->V_cluster_suffix);
1461                 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, start);
1462
1463                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1464                 host.V_host_id = host_id;
1465                 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1466                   xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1467                   char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1468                   xbt_str_varsubst(tmp_availability_file,patterns);
1469                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1470                   host.V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1471                   xbt_free(tmp_availability_file);
1472                 }
1473                 else
1474                 {
1475                   XBT_DEBUG("\tavailability_file=\"\"");
1476                 }
1477                 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1478                   char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1479                   xbt_str_varsubst(tmp_state_file,patterns);
1480                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1481                   host.V_host_state_trace = tmgr_trace_new(tmp_state_file);
1482                   xbt_free(tmp_state_file);
1483                 }
1484                 else
1485                 {
1486                   XBT_DEBUG("\tstate_file=\"\"");
1487                 }
1488
1489                 host.V_host_power_peak = struct_cluster->S_cluster_power;
1490                 host.V_host_power_scale = 1.0;
1491                 host.V_host_core = struct_cluster->S_cluster_core;
1492                 host.V_host_state_initial = SURF_RESOURCE_ON;
1493                 host.V_host_coord = "";
1494                 sg_platf_new_host(&host);
1495                 XBT_DEBUG("</host>");
1496
1497                 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1498                 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1499                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1500                 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1501                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1502
1503                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1504
1505                 struct_lnk = xbt_new0(s_surf_parsing_link_arg_t, 1);
1506                 struct_lnk->V_link_id = link_id;
1507                 struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bw;
1508                 struct_lnk->V_link_latency = struct_cluster->S_cluster_lat;
1509                 struct_lnk->V_link_bandwidth_file = NULL;
1510                 struct_lnk->V_link_latency_file = NULL;
1511                 struct_lnk->V_link_state_file = NULL;
1512                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
1513                 struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1514
1515                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1516                         struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1517                 else
1518                 {
1519                  if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1520                          struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1521                  else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1522                          struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1523                 }
1524                 surf_parse_link();
1525
1526                 ETag_surfxml_host();
1527                 ETag_surfxml_link();
1528
1529                 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1530                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
1531                         char* tmp_link =  bprintf("%s_UP",link_id);
1532                         info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1533                         free(tmp_link);
1534                         tmp_link =  bprintf("%s_DOWN",link_id);
1535                         info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1536                         free(tmp_link);
1537                 }
1538                 else{
1539                         info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1540                         info->link_down = info->link_up;
1541                 }
1542                 xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1543                 xbt_free(link_id);
1544                 xbt_free(host_id);
1545
1546                 break;
1547
1548     case 2:
1549
1550       start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1551       end=  surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1552       for (i = start; i <= end; i++) {
1553                 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, i, struct_cluster->V_cluster_suffix);
1554                 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, i);
1555
1556                 A_surfxml_host_state = A_surfxml_host_state_ON;
1557
1558                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1559                 host.V_host_id = host_id;
1560                 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1561                   xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1562                   char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1563                   xbt_str_varsubst(tmp_availability_file,patterns);
1564                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1565                   host.V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1566                   xbt_free(tmp_availability_file);
1567                 }
1568                 else
1569                 {
1570                   XBT_DEBUG("\tavailability_file=\"\"");
1571                 }
1572                 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1573                   char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1574                   xbt_str_varsubst(tmp_state_file,patterns);
1575                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1576                   host.V_host_state_trace = tmgr_trace_new(tmp_state_file);
1577                   xbt_free(tmp_state_file);
1578                 }
1579                 else
1580                 {
1581                   XBT_DEBUG("\tstate_file=\"\"");
1582                 }
1583
1584                 host.V_host_power_peak = struct_cluster->S_cluster_power;
1585                 host.V_host_power_scale = 1.0;
1586                 host.V_host_core = struct_cluster->S_cluster_core;
1587                 host.V_host_state_initial = SURF_RESOURCE_ON;
1588                 host.V_host_coord = "";
1589                 sg_platf_new_host(&host);
1590                 XBT_DEBUG("</host>");
1591
1592                 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1593                 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1594                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1595                 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1596                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1597
1598                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1599
1600                 struct_lnk = xbt_new0(s_surf_parsing_link_arg_t, 1);
1601                 struct_lnk->V_link_id = link_id;
1602                 struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bw;
1603                 struct_lnk->V_link_latency = struct_cluster->S_cluster_lat;
1604                 struct_lnk->V_link_bandwidth_file = NULL;
1605                 struct_lnk->V_link_latency_file = NULL;
1606                 struct_lnk->V_link_state_file = NULL;
1607                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
1608                 struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1609
1610                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1611                         struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1612                 else
1613                 {
1614                  if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1615                          struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1616                  else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1617                          struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1618                 }
1619                 surf_parse_link();
1620
1621                 ETag_surfxml_host();
1622                 ETag_surfxml_link();
1623
1624                 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1625                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
1626                         char* tmp_link =  bprintf("%s_UP",link_id);
1627                         info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1628                         free(tmp_link);
1629                         tmp_link =  bprintf("%s_DOWN",link_id);
1630                         info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1631                         free(tmp_link);
1632                 }
1633                 else{
1634                         info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1635                         info->link_down = info->link_up;
1636                 }
1637                 xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1638                 xbt_free(link_id);
1639                 xbt_free(host_id);
1640
1641       }
1642       break;
1643
1644     default:
1645       XBT_DEBUG("Malformed radical");
1646       break;
1647     }
1648
1649     xbt_dynar_free(&radical_ends);
1650   }
1651   xbt_dynar_free(&radical_elements);
1652
1653   //Make the router
1654   XBT_DEBUG(" ");
1655   XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->S_cluster_router_id);
1656   SURFXML_BUFFER_SET(router_id, struct_cluster->S_cluster_router_id);
1657   SURFXML_BUFFER_SET(router_coordinates, "");
1658   SURFXML_START_TAG(router);
1659   SURFXML_END_TAG(router);
1660
1661   //Make the backbone
1662   if( (struct_cluster->S_cluster_bb_bw!= 0)  && (struct_cluster->S_cluster_bb_lat!=0)  ){
1663           char *link_backbone = bprintf("%s_backbone", struct_cluster->V_cluster_id);
1664           XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->S_cluster_bb_bw, struct_cluster->S_cluster_bb_lat);
1665
1666           A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1667           if(AX_surfxml_cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
1668           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1669
1670           struct_lnk = xbt_new0(s_surf_parsing_link_arg_t, 1);
1671           struct_lnk->V_link_id = link_backbone;
1672           struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bb_bw;
1673           struct_lnk->V_link_latency = struct_cluster->S_cluster_bb_lat;
1674           struct_lnk->V_link_bandwidth_file = NULL;
1675           struct_lnk->V_link_latency_file = NULL;
1676           struct_lnk->V_link_state_file = NULL;
1677           struct_lnk->V_link_state = SURF_RESOURCE_ON;
1678           struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1679
1680           if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1681                   struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1682           else
1683                   struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1684
1685           surf_parse_link();
1686           ETag_surfxml_link();
1687
1688           surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1689           info->link_up   = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1690           info->link_down = info->link_up;
1691           xbt_dict_set(cluster_host_link,struct_cluster->V_cluster_id,info,xbt_free);
1692           free(link_backbone);
1693   }
1694
1695   XBT_DEBUG(" ");
1696
1697   char *new_suffix = xbt_strdup("");
1698
1699   radical_elements = xbt_str_split(struct_cluster->V_cluster_suffix, ".");
1700   xbt_dynar_foreach(radical_elements, iter, groups) {
1701     if (strcmp(groups, "")) {
1702       char *old_suffix = new_suffix;
1703       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1704       free(old_suffix);
1705     }
1706   }
1707
1708   xbt_dynar_free(&radical_elements);
1709   xbt_free(new_suffix);
1710
1711   if( strcmp(struct_cluster->V_cluster_availability_file,"")
1712                   || strcmp(struct_cluster->V_cluster_state_file,"") )
1713           xbt_dict_free(&patterns);
1714
1715   XBT_DEBUG("</AS>");
1716   SURFXML_END_TAG(AS);
1717   XBT_DEBUG(" ");
1718
1719   surfxml_bufferstack_pop(1);
1720 }
1721 /*
1722  * This function take a string and replace parameters from patterns dict.
1723  * It returns the new value.
1724  */
1725 static char* replace_random_parameter(char * string)
1726 {
1727   char *test_string = NULL;
1728
1729   if(xbt_dict_size(random_value)==0)
1730     return string;
1731
1732   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1733   test_string = bprintf("${%s}", string);
1734   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1735
1736   if (strcmp(test_string,"")) { //if not empty, keep this value.
1737     xbt_free(string);
1738     string = test_string;
1739   } //In other case take old value (without ${})
1740   else
1741         free(test_string);
1742   return string;
1743 }
1744
1745 static void clean_routing_after_parse(void)
1746 {
1747         xbt_dict_free(&random_value);
1748         xbt_dict_free(&patterns);
1749 }
1750
1751 static void routing_parse_Speer(void)
1752 {
1753   static int AX_ptr = 0;
1754   char *host_id = NULL;
1755   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1756
1757   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1758   static unsigned int surfxml_buffer_stack_stack[1024];
1759
1760   surfxml_buffer_stack_stack[0] = 0;
1761
1762   surfxml_bufferstack_push(1);
1763
1764   SURFXML_BUFFER_SET(AS_id, struct_peer->V_peer_id);
1765
1766   SURFXML_BUFFER_SET(AS_routing, "Full");
1767   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", struct_peer->V_peer_id);
1768
1769   SURFXML_START_TAG(AS);
1770
1771   XBT_DEBUG(" ");
1772   host_id = HOST_PEER(struct_peer->V_peer_id);
1773   router_id = ROUTER_PEER(struct_peer->V_peer_id);
1774   link_id_up = LINK_UP_PEER(struct_peer->V_peer_id);
1775   link_id_down = LINK_DOWN_PEER(struct_peer->V_peer_id);
1776
1777   link_router = bprintf("%s_link_router", struct_peer->V_peer_id);
1778   link_backbone = bprintf("%s_backbone", struct_peer->V_peer_id);
1779
1780   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, struct_peer->V_peer_power);
1781   A_surfxml_host_state = A_surfxml_host_state_ON;
1782   SURFXML_BUFFER_SET(host_id, host_id);
1783   SURFXML_BUFFER_SET(host_power, struct_peer->V_peer_power);
1784   SURFXML_BUFFER_SET(host_availability, "1.0");
1785   SURFXML_BUFFER_SET(host_availability_file, struct_peer->V_peer_availability_trace);
1786   SURFXML_BUFFER_SET(host_state_file, struct_peer->V_peer_state_trace);
1787   SURFXML_BUFFER_SET(host_coordinates, "");
1788   SURFXML_BUFFER_SET(host_core, "1.0");
1789   SURFXML_START_TAG(host);
1790   SURFXML_END_TAG(host);
1791
1792   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, struct_peer->V_peer_coord);
1793   SURFXML_BUFFER_SET(router_id, router_id);
1794   SURFXML_BUFFER_SET(router_coordinates, struct_peer->V_peer_coord);
1795   SURFXML_START_TAG(router);
1796   SURFXML_END_TAG(router);
1797
1798   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, struct_peer->V_peer_bw_in, struct_peer->V_peer_lat);
1799   A_surfxml_link_state = A_surfxml_link_state_ON;
1800   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1801   SURFXML_BUFFER_SET(link_id, link_id_up);
1802   SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_in);
1803   SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1804   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1805   SURFXML_BUFFER_SET(link_latency_file, "");
1806   SURFXML_BUFFER_SET(link_state_file, "");
1807   SURFXML_START_TAG(link);
1808   SURFXML_END_TAG(link);
1809
1810   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, struct_peer->V_peer_bw_out, struct_peer->V_peer_lat);
1811   A_surfxml_link_state = A_surfxml_link_state_ON;
1812   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1813   SURFXML_BUFFER_SET(link_id, link_id_down);
1814   SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_out);
1815   SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1816   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1817   SURFXML_BUFFER_SET(link_latency_file, "");
1818   SURFXML_BUFFER_SET(link_state_file, "");
1819   SURFXML_START_TAG(link);
1820   SURFXML_END_TAG(link);
1821
1822   XBT_DEBUG(" ");
1823
1824   // begin here
1825   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1826   XBT_DEBUG("symmetrical=\"NO\">");
1827   SURFXML_BUFFER_SET(route_src, host_id);
1828   SURFXML_BUFFER_SET(route_dst, router_id);
1829   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1830   SURFXML_START_TAG(route);
1831
1832   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1833   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1834   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1835   SURFXML_START_TAG(link_ctn);
1836   SURFXML_END_TAG(link_ctn);
1837
1838   XBT_DEBUG("</route>");
1839   SURFXML_END_TAG(route);
1840
1841   //Opposite Route
1842   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1843   XBT_DEBUG("symmetrical=\"NO\">");
1844   SURFXML_BUFFER_SET(route_src, router_id);
1845   SURFXML_BUFFER_SET(route_dst, host_id);
1846   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1847   SURFXML_START_TAG(route);
1848
1849   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1850   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1851   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1852   SURFXML_START_TAG(link_ctn);
1853   SURFXML_END_TAG(link_ctn);
1854
1855   XBT_DEBUG("</route>");
1856   SURFXML_END_TAG(route);
1857
1858   XBT_DEBUG("</AS>");
1859   SURFXML_END_TAG(AS);
1860   XBT_DEBUG(" ");
1861
1862   //xbt_dynar_free(&tab_elements_num);
1863         free(host_id);
1864         free(router_id);
1865         free(link_router);
1866         free(link_backbone);
1867         free(link_id_up);
1868         free(link_id_down);
1869   surfxml_bufferstack_pop(1);
1870 }
1871
1872 static void routing_parse_Srandom(void)
1873 {
1874           double mean, std, min, max, seed;
1875           char *random_id = A_surfxml_random_id;
1876           char *random_radical = A_surfxml_random_radical;
1877           char *rd_name = NULL;
1878           char *rd_value;
1879           mean = surf_parse_get_double(A_surfxml_random_mean);
1880           std  = surf_parse_get_double(A_surfxml_random_std_deviation);
1881           min  = surf_parse_get_double(A_surfxml_random_min);
1882           max  = surf_parse_get_double(A_surfxml_random_max);
1883           seed = surf_parse_get_double(A_surfxml_random_seed);
1884
1885           double res = 0;
1886           int i = 0;
1887           random_data_t random = xbt_new0(s_random_data_t, 1);
1888           char *tmpbuf;
1889
1890           xbt_dynar_t radical_elements;
1891           unsigned int iter;
1892           char *groups;
1893           int start, end;
1894           xbt_dynar_t radical_ends;
1895
1896           random->generator = A_surfxml_random_generator;
1897           random->seed = seed;
1898           random->min = min;
1899           random->max = max;
1900
1901           /* Check user stupidities */
1902           if (max < min)
1903             THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1904           if (mean < min)
1905             THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1906                    min);
1907           if (mean > max)
1908             THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1909                    max);
1910
1911           /* normalize the mean and standard deviation before storing */
1912           random->mean = (mean - min) / (max - min);
1913           random->std = std / (max - min);
1914
1915           if (random->mean * (1 - random->mean) < random->std * random->std)
1916             THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1917                    random->mean, random->std);
1918
1919           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1920           random_id,
1921           random->min,
1922           random->max,
1923           random->mean,
1924           random->std,
1925           random->generator,
1926           random->seed,
1927           random_radical);
1928
1929           if(xbt_dict_size(random_value)==0)
1930                   random_value = xbt_dict_new();
1931
1932           if(!strcmp(random_radical,""))
1933           {
1934                   res = random_generate(random);
1935                   rd_value = bprintf("%f",res);
1936                   xbt_dict_set(random_value, random_id, rd_value, free);
1937           }
1938           else
1939           {
1940                   radical_elements = xbt_str_split(random_radical, ",");
1941                   xbt_dynar_foreach(radical_elements, iter, groups) {
1942                         radical_ends = xbt_str_split(groups, "-");
1943                         switch (xbt_dynar_length(radical_ends)) {
1944                         case 1:
1945                                           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
1946                                           res = random_generate(random);
1947                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
1948                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1949                                           xbt_free(tmpbuf);
1950                                           break;
1951
1952                         case 2:
1953                               start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1954                               end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1955                                           for (i = start; i <= end; i++) {
1956                                                   xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
1957                                                   res = random_generate(random);
1958                           tmpbuf = bprintf("%s%d",random_id,i);
1959                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1960                           xbt_free(tmpbuf);
1961                                           }
1962                                           break;
1963                         default:
1964                                 XBT_INFO("Malformed radical");
1965                                 break;
1966                         }
1967                         res = random_generate(random);
1968                         rd_name  = bprintf("%s_router",random_id);
1969                         rd_value = bprintf("%f",res);
1970                         xbt_dict_set(random_value, rd_name, rd_value, free);
1971
1972                         xbt_dynar_free(&radical_ends);
1973                   }
1974                   free(rd_name);
1975                   xbt_dynar_free(&radical_elements);
1976           }
1977 }
1978
1979 static void routing_parse_Erandom(void)
1980 {
1981         /*xbt_dict_cursor_t cursor = NULL;
1982         char *key;
1983         char *elem;
1984
1985         xbt_dict_foreach(random_value, cursor, key, elem) {
1986           XBT_DEBUG("%s = %s",key,elem);
1987         }
1988 */
1989 }
1990
1991 /*
1992  * New methods to init the routing model component from the lua script
1993  */
1994
1995
1996 /*
1997  * add a host to the network element list
1998  */
1999
2000 void routing_add_host(const char *host_id)
2001 {
2002   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
2003 }
2004
2005 /*
2006  * Set a new link on the actual list of link for a route or ASroute
2007  */
2008 void routing_add_link(const char *link_id)
2009 {
2010   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
2011 }
2012
2013 /*
2014  *Set the endpoints for a route
2015  */
2016 void routing_set_route(const char *src_id, const char *dst_id)
2017 {
2018   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
2019 }
2020
2021 /*
2022  * Store the route by calling parse_E_route_store_route
2023  */
2024 void routing_store_route(void)
2025 {
2026   parse_E_route_store_route();
2027 }