Logo AND Algorithmique Numérique Distribuée

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