Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a new module allowing to interact with the platform directly
[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.h" // platform creation API
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 void routing_parse_Sconfig(void);        /* config Tag */
59 static void routing_parse_Econfig(void);        /* config Tag */
60
61 static char* replace_random_parameter(char * chaine);
62 static void clean_routing_after_parse(void);
63
64 /* this lines are only for replace use like index in the model table */
65 typedef enum {
66   SURF_MODEL_FULL = 0,
67   SURF_MODEL_FLOYD,
68   SURF_MODEL_DIJKSTRA,
69   SURF_MODEL_DIJKSTRACACHE,
70   SURF_MODEL_NONE,
71   SURF_MODEL_RULEBASED,
72   SURF_MODEL_VIVALDI,
73   SURF_MODEL_CLUSTER
74 } e_routing_types;
75
76 struct s_model_type routing_models[] = { {"Full",
77                                           "Full routing data (fast, large memory requirements, fully expressive)",
78                                           model_full_create,
79                                           model_full_load,
80                                           model_full_unload,
81                                           model_full_end},
82 {"Floyd",
83  "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
84  model_floyd_create, model_floyd_load, model_floyd_unload,
85  model_floyd_end},
86 {"Dijkstra",
87  "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
88  model_dijkstra_create, model_dijkstra_both_load,
89  model_dijkstra_both_unload, model_dijkstra_both_end},
90 {"DijkstraCache",
91  "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
92  model_dijkstracache_create, model_dijkstra_both_load,
93  model_dijkstra_both_unload, model_dijkstra_both_end},
94 {"none", "No routing (usable with Constant network only)",
95  model_none_create, model_none_load, model_none_unload, model_none_end},
96 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
97  model_rulebased_load, model_rulebased_unload, model_rulebased_end},
98 {"Vivaldi", "Vivaldi routing", model_rulebased_create,
99   model_rulebased_load, model_rulebased_unload, model_rulebased_end},
100 {"Cluster", "Cluster routing", model_cluster_create,
101   model_rulebased_load, model_rulebased_unload, model_rulebased_end},
102 {NULL, NULL, NULL, NULL, NULL, NULL}
103 };
104
105 static double euclidean_dist_comp(int index, xbt_dynar_t src, xbt_dynar_t dst)
106 {
107         double src_coord, dst_coord;
108
109         src_coord = atof(xbt_dynar_get_as(src, index, char *));
110         dst_coord = atof(xbt_dynar_get_as(dst, index, char *));
111
112         return (src_coord-dst_coord)*(src_coord-dst_coord);
113
114 }
115
116 static double base_vivaldi_get_latency (const char *src, const char *dst)
117 {
118   double euclidean_dist;
119   xbt_dynar_t src_ctn, dst_ctn;
120   src_ctn = xbt_lib_get_or_null(host_lib, src, COORD_HOST_LEVEL);
121   if(!src_ctn) src_ctn = xbt_lib_get_or_null(as_router_lib, src, COORD_ASR_LEVEL);
122   dst_ctn = xbt_lib_get_or_null(host_lib, dst, COORD_HOST_LEVEL);
123   if(!dst_ctn) dst_ctn = xbt_lib_get_or_null(as_router_lib, dst, COORD_ASR_LEVEL);
124
125   if(dst_ctn == NULL || src_ctn == NULL)
126   xbt_die("Coord src '%s' :%p   dst '%s' :%p",src,src_ctn,dst,dst_ctn);
127
128   euclidean_dist = sqrt (euclidean_dist_comp(0,src_ctn,dst_ctn)+euclidean_dist_comp(1,src_ctn,dst_ctn))
129                                  + fabs(atof(xbt_dynar_get_as(src_ctn, 2, char *)))+fabs(atof(xbt_dynar_get_as(dst_ctn, 2, char *)));
130
131   xbt_assert(euclidean_dist>=0, "Euclidean Dist is less than 0\"%s\" and \"%.2f\"", src, euclidean_dist);
132
133   //From .ms to .s
134   return euclidean_dist / 1000;
135 }
136
137 static double vivaldi_get_link_latency (routing_component_t rc,const char *src, const char *dst, route_extended_t e_route)
138 {
139   if(get_network_element_type(src) == SURF_NETWORK_ELEMENT_AS) {
140           int need_to_clean = e_route?0:1;
141           double latency;
142           e_route = e_route?e_route:(*(rc->get_route)) (rc, src, dst);
143           latency = base_vivaldi_get_latency(e_route->src_gateway,e_route->dst_gateway);
144           if(need_to_clean) generic_free_extended_route(e_route);
145           return latency;
146   } else {
147           return base_vivaldi_get_latency(src,dst);
148   }
149 }
150
151 /**
152  * \brief Add a "host" to the network element list
153  */
154 static void parse_S_host(const char *host_id, const char* coord)
155 {
156   network_element_info_t info = NULL;
157   if (current_routing->hierarchy == SURF_ROUTING_NULL)
158     current_routing->hierarchy = SURF_ROUTING_BASE;
159   xbt_assert(!xbt_lib_get_or_null(host_lib, host_id,ROUTING_HOST_LEVEL),
160               "Reading a host, processing unit \"%s\" already exists",
161               host_id);
162   xbt_assert(current_routing->set_processing_unit,
163               "no defined method \"set_processing_unit\" in \"%s\"",
164               current_routing->name);
165   (*(current_routing->set_processing_unit)) (current_routing, host_id);
166   info = xbt_new0(s_network_element_info_t, 1);
167   info->rc_component = current_routing;
168   info->rc_type = SURF_NETWORK_ELEMENT_HOST;
169   xbt_lib_set(host_lib,host_id,ROUTING_HOST_LEVEL,(void *) info);
170   if (strcmp(coord,"")) {
171         if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
172     xbt_dynar_t ctn = xbt_str_split_str(coord, " ");
173     xbt_dynar_shrink(ctn, 0);
174     xbt_lib_set(host_lib,host_id,COORD_HOST_LEVEL,(void *) ctn);
175   }
176 }
177
178 static void parse_E_host(void)
179 {
180          xbt_dict_cursor_t cursor = NULL;
181           char *key;
182           char *elem;
183
184           xbt_dict_foreach(current_property_set, cursor, key, elem) {
185                   XBT_DEBUG("property : %s = %s",key,elem);
186                 }
187 }
188
189 /*
190  * \brief Add a host to the network element list from XML
191  */
192 static void parse_S_host_XML(surf_parsing_host_arg_t h)
193 {
194         parse_S_host(h->V_host_id, h->V_host_coord);
195 }
196 static void parse_E_host_XML(void)
197 {
198         parse_E_host();
199 }
200
201 /*
202  * \brief Add a host to the network element list from lua script
203  */
204 static void parse_S_host_lua(const char *host_id, const char *coord)
205 {
206   parse_S_host(host_id, coord);
207 }
208
209
210 /**
211  * \brief Add a "router" to the network element list
212  */
213 static void parse_S_router(void)
214 {
215   network_element_info_t info = NULL;
216   if (current_routing->hierarchy == SURF_ROUTING_NULL)
217     current_routing->hierarchy = SURF_ROUTING_BASE;
218   xbt_assert(!xbt_lib_get_or_null(as_router_lib,struct_router->V_router_id, ROUTING_ASR_LEVEL),
219               "Reading a router, processing unit \"%s\" already exists",
220               struct_router->V_router_id);
221   xbt_assert(current_routing->set_processing_unit,
222               "no defined method \"set_processing_unit\" in \"%s\"",
223               current_routing->name);
224   (*(current_routing->set_processing_unit)) (current_routing,
225                   struct_router->V_router_id);
226   info = xbt_new0(s_network_element_info_t, 1);
227   info->rc_component = current_routing;
228   info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
229
230   xbt_lib_set(as_router_lib,struct_router->V_router_id,ROUTING_ASR_LEVEL,(void *) info);
231   if (strcmp(A_surfxml_router_coordinates,"")) {
232         if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
233     xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_router_coordinates, " ");
234     xbt_dynar_shrink(ctn, 0);
235     xbt_lib_set(as_router_lib,struct_router->V_router_id,COORD_ASR_LEVEL,(void *) ctn);
236   }
237 }
238
239 /**
240  * brief Add a "router" to the network element list from XML description
241  */
242 static void parse_S_router_XML(void)
243 {
244         return parse_S_router();
245 }
246
247 /**
248  * brief Add a "router" to the network element list from XML description
249  */
250 static void parse_S_router_lua(const char* router_id)
251 {
252         struct_router->V_router_id = xbt_strdup(router_id);
253         struct_router->V_router_coord = xbt_strdup("");
254         return parse_S_router();
255 }
256
257 /**
258  * \brief Set the endponints for a route
259  */
260 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
261 {
262   if (src != NULL && dst != NULL && link_list != NULL)
263     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
264            src_id, dst_id);
265   src = src_id;
266   dst = dst_id;
267   xbt_assert(strlen(src) > 0 || strlen(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 the endpoints for a route from XML
275  */
276 static void parse_S_route_new_and_endpoints_XML(void)
277 {
278   parse_S_route_new_and_endpoints(A_surfxml_route_src,
279                                   A_surfxml_route_dst);
280 }
281
282 /**
283  * \brief Set the endpoints for a route from lua
284  */
285 static void parse_S_route_new_and_endpoints_lua(const char *id_src, const char *id_dst)
286 {
287   parse_S_route_new_and_endpoints(id_src, id_dst);
288 }
289
290 /**
291  * \brief Set the endponints and gateways for a ASroute
292  */
293 static void parse_S_ASroute_new_and_endpoints(void)
294 {
295   if (src != NULL && dst != NULL && link_list != NULL)
296     THROWF(arg_error, 0, "Route between %s to %s can not be defined",
297            A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
298   src = A_surfxml_ASroute_src;
299   dst = A_surfxml_ASroute_dst;
300   gw_src = A_surfxml_ASroute_gw_src;
301   gw_dst = A_surfxml_ASroute_gw_dst;
302   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
303               || strlen(gw_dst) > 0,
304               "Some limits are null in the route between \"%s\" and \"%s\"",
305               src, dst);
306   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
307 }
308
309 /**
310  * \brief Set the endponints for a bypassRoute
311  */
312 static void parse_S_bypassRoute_new_and_endpoints(void)
313 {
314   if (src != NULL && dst != NULL && link_list != NULL)
315     THROWF(arg_error, 0,
316            "Bypass Route between %s to %s can not be defined",
317            A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
318   src = A_surfxml_bypassRoute_src;
319   dst = A_surfxml_bypassRoute_dst;
320   gw_src = A_surfxml_bypassRoute_gw_src;
321   gw_dst = A_surfxml_bypassRoute_gw_dst;
322   xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
323               || strlen(gw_dst) > 0,
324               "Some limits are null in the route between \"%s\" and \"%s\"",
325               src, dst);
326   link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
327 }
328
329 /**
330  * \brief Set a new link on the actual list of link for a route or ASroute
331  */
332 static void parse_E_link_ctn_new_elem(const char *link_id)
333 {
334   char *val;
335   val = xbt_strdup(link_id);
336   xbt_dynar_push(link_list, &val);
337 }
338
339 /**
340  * \brief Set a new link on the actual list of link for a route or ASroute from XML
341  */
342
343 static void parse_E_link_ctn_new_elem_XML(void)
344 {
345   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
346     parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
347   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
348     char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
349     parse_E_link_ctn_new_elem(link_id);
350     free(link_id);
351   }
352   if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
353     char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
354     parse_E_link_ctn_new_elem(link_id);
355     free(link_id);
356   }
357 }
358
359 /**
360  * \brief Set a new link on the actual list of link for a route or ASroute from lua
361  */
362 static void parse_E_link_c_ctn_new_elem_lua(const char *link_id)
363 {
364   parse_E_link_ctn_new_elem(link_id);
365 }
366
367 /**
368  * \brief Store the route by calling the set_route function of the current routing component
369  */
370 static void parse_E_route_store_route(void)
371 {
372   name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
373   route->generic_route.link_list = link_list;
374   xbt_assert(current_routing->set_route,
375               "no defined method \"set_route\" in \"%s\"",
376               current_routing->name);
377   (*(current_routing->set_route)) (current_routing, src, dst, route);
378   link_list = NULL;
379   src = NULL;
380   dst = NULL;
381 }
382
383 /**
384  * \brief Store the ASroute by calling the set_ASroute function of the current routing component
385  */
386 static void parse_E_ASroute_store_route(void)
387 {
388   name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
389   e_route->generic_route.link_list = link_list;
390   e_route->src_gateway = xbt_strdup(gw_src);
391   e_route->dst_gateway = xbt_strdup(gw_dst);
392   xbt_assert(current_routing->set_ASroute,
393               "no defined method \"set_ASroute\" in \"%s\"",
394               current_routing->name);
395   (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
396   link_list = NULL;
397   src = NULL;
398   dst = NULL;
399   gw_src = NULL;
400   gw_dst = NULL;
401 }
402
403 /**
404  * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
405  */
406 static void parse_E_bypassRoute_store_route(void)
407 {
408   route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
409   e_route->generic_route.link_list = link_list;
410   e_route->src_gateway = xbt_strdup(gw_src);
411   e_route->dst_gateway = xbt_strdup(gw_dst);
412   xbt_assert(current_routing->set_bypassroute,
413               "no defined method \"set_bypassroute\" in \"%s\"",
414               current_routing->name);
415   (*(current_routing->set_bypassroute)) (current_routing, src, dst,
416                                          e_route);
417   link_list = NULL;
418   src = NULL;
419   dst = NULL;
420   gw_src = NULL;
421   gw_dst = NULL;
422 }
423
424 /**
425  * \brief Make a new routing component to the platform
426  *
427  * Add a new autonomous system to the platform. Any elements (such as host,
428  * router or sub-AS) added after this call and before the corresponding call
429  * to sg_platf_new_AS_close() will be added to this AS.
430  *
431  * Once this function was called, the configuration concerning the used
432  * models cannot be changed anymore.
433  *
434  * @param AS_id name of this autonomous system. Must be uniq in the platform
435  * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
436  */
437 void sg_platf_new_AS_open(const char *AS_id, const char *wanted_routing_type)
438 {
439   routing_component_t new_routing;
440   model_type_t model = NULL;
441   int cpt;
442
443   surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag and before the first <AS>-like */
444
445   /* search the routing model */
446   for (cpt = 0; routing_models[cpt].name; cpt++)
447     if (!strcmp(wanted_routing_type, routing_models[cpt].name))
448       model = &routing_models[cpt];
449   /* if its not exist, error */
450   if (model == NULL) {
451     fprintf(stderr, "Routing model %s not found. Existing models:\n",
452             wanted_routing_type);
453     for (cpt = 0; routing_models[cpt].name; cpt++)
454         fprintf(stderr, "   %s: %s\n", routing_models[cpt].name,
455                 routing_models[cpt].desc);
456     xbt_die(NULL);
457   }
458
459   /* make a new routing component */
460   new_routing = (routing_component_t) (*(model->create)) ();
461   new_routing->routing = model;
462   new_routing->hierarchy = SURF_ROUTING_NULL;
463   new_routing->name = xbt_strdup(AS_id);
464   new_routing->routing_sons = xbt_dict_new();
465
466   /* Hack for Vivaldi */
467   if(!strcmp(model->name,"Vivaldi"))
468         new_routing->get_latency = vivaldi_get_link_latency;
469
470   if (current_routing == NULL && global_routing->root == NULL) {
471
472     /* it is the first one */
473     new_routing->routing_father = NULL;
474     global_routing->root = new_routing;
475
476   } else if (current_routing != NULL && global_routing->root != NULL) {
477
478     xbt_assert(!xbt_dict_get_or_null
479                 (current_routing->routing_sons, AS_id),
480                 "The AS \"%s\" already exists", AS_id);
481     /* it is a part of the tree */
482     new_routing->routing_father = current_routing;
483     /* set the father behavior */
484     if (current_routing->hierarchy == SURF_ROUTING_NULL)
485       current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
486     /* add to the sons dictionary */
487     xbt_dict_set(current_routing->routing_sons, AS_id,
488                  (void *) new_routing, NULL);
489     /* add to the father element list */
490     (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
491     /* unload the prev parse rules */
492     (*(current_routing->routing->unload)) ();
493
494   } else {
495     THROWF(arg_error, 0, "All defined components must be belong to a AS");
496   }
497   /* set the new parse rules */
498   (*(new_routing->routing->load)) ();
499   /* set the new current component of the tree */
500   current_routing = new_routing;
501 }
502
503 /**
504  * \brief Specify that the current description of AS is finished
505  *
506  * Once you've declared all the content of your AS, you have to close
507  * it with this call. Your AS is not usable until you call this function.
508  *
509  * @fixme: this call is not as robust as wanted: bad things WILL happen
510  * if you call it twice for the same AS, or if you forget calling it, or
511  * even if you add stuff to a closed AS
512  *
513  */
514 void sg_platf_new_AS_close() {
515
516   if (current_routing == NULL) {
517     THROWF(arg_error, 0, "Close an AS, but none was under construction");
518   } else {
519     network_element_info_t info = NULL;
520     xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
521                 "The AS \"%s\" already exists",current_routing->name);
522     info = xbt_new0(s_network_element_info_t, 1);
523     info->rc_component = current_routing->routing_father;
524     info->rc_type = SURF_NETWORK_ELEMENT_AS;
525     xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
526
527     (*(current_routing->routing->unload)) ();
528     (*(current_routing->routing->end)) ();
529     current_routing = current_routing->routing_father;
530     if (current_routing != NULL)
531       (*(current_routing->routing->load)) ();
532   }
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_cluster_cb_list,
995                        &routing_parse_Scluster);
996
997   surfxml_add_callback(STag_surfxml_peer_cb_list,
998                          &routing_parse_Speer);
999
1000   surfxml_add_callback(ETag_surfxml_platform_cb_list,
1001                                                   &clean_routing_after_parse);
1002
1003 #ifdef HAVE_TRACING
1004   instr_routing_define_callbacks();
1005 #endif
1006 }
1007
1008 void surf_parse_add_callback_config(void)
1009 {
1010         surfxml_add_callback(STag_surfxml_config_cb_list, &routing_parse_Sconfig);
1011         surfxml_add_callback(ETag_surfxml_config_cb_list, &routing_parse_Econfig);
1012         surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties_XML);
1013         surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
1014 }
1015
1016 /* ************************************************** */
1017 /* ********** PATERN FOR NEW ROUTING **************** */
1018
1019 /* The minimal configuration of a new routing model need the next functions,
1020  * also you need to set at the start of the file, the new model in the model
1021  * list. Remember keep the null ending of the list.
1022  */
1023 /*** Routing model structure ***/
1024 // typedef struct {
1025 //   s_routing_component_t generic_routing;
1026 //   /* things that your routing model need */
1027 // } s_routing_component_NEW_t,*routing_component_NEW_t;
1028
1029 /*** Parse routing model functions ***/
1030 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
1031 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
1032 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
1033 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
1034 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
1035
1036 /*** Business methods ***/
1037 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1038 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1039 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
1040
1041 /*** Creation routing model functions ***/
1042 // static void* model_NEW_create(void) {
1043 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
1044 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
1045 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
1046 //   new_component->generic_routing.set_route = model_NEW_set_route;
1047 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
1048 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
1049 //   new_component->generic_routing.get_route = NEW_get_route;
1050 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
1051 //   new_component->generic_routing.finalize = NEW_finalize;
1052 //   /* initialization of internal structures */
1053 //   return new_component;
1054 // } /* mandatory */
1055 // static void  model_NEW_load(void) {}   /* mandatory */
1056 // static void  model_NEW_unload(void) {} /* mandatory */
1057 // static void  model_NEW_end(void) {}    /* mandatory */
1058
1059 /* ************************************************************************** */
1060 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
1061
1062 void generic_set_processing_unit(routing_component_t rc,
1063                                         const char *name)
1064 {
1065   XBT_DEBUG("Load process unit \"%s\"", name);
1066   int *id = xbt_new0(int, 1);
1067   xbt_dict_t _to_index;
1068   _to_index = current_routing->to_index;
1069   *id = xbt_dict_length(_to_index);
1070   xbt_dict_set(_to_index, name, id, xbt_free);
1071 }
1072
1073 void generic_set_autonomous_system(routing_component_t rc,
1074                                           const char *name)
1075 {
1076   XBT_DEBUG("Load Autonomous system \"%s\"", name);
1077   int *id = xbt_new0(int, 1);
1078   xbt_dict_t _to_index;
1079   _to_index = current_routing->to_index;
1080   *id = xbt_dict_length(_to_index);
1081   xbt_dict_set(_to_index, name, id, xbt_free);
1082 }
1083
1084 int surf_pointer_resource_cmp(const void *a, const void *b)
1085 {
1086   return a != b;
1087 }
1088
1089 int surf_link_resource_cmp(const void *a, const void *b)
1090 {
1091   return !!memcmp(a,b,global_routing->size_of_link);
1092 }
1093
1094 void generic_set_bypassroute(routing_component_t rc,
1095                                     const char *src, const char *dst,
1096                                     route_extended_t e_route)
1097 {
1098   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
1099   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1100   char *route_name;
1101
1102   route_name = bprintf("%s#%s", src, dst);
1103   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
1104               "Invalid count of links, must be greater than zero (%s,%s)",
1105               src, dst);
1106   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
1107               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
1108               src, e_route->src_gateway, dst, e_route->dst_gateway);
1109
1110   route_extended_t new_e_route =
1111       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
1112   xbt_dynar_free(&(e_route->generic_route.link_list));
1113   xbt_free(e_route);
1114
1115   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
1116                (void (*)(void *)) generic_free_extended_route);
1117   xbt_free(route_name);
1118 }
1119
1120 /* ************************************************************************** */
1121 /* *********************** GENERIC BUSINESS METHODS ************************* */
1122
1123 double generic_get_link_latency(routing_component_t rc,
1124                                        const char *src, const char *dst,
1125                                        route_extended_t route)
1126 {
1127         int need_to_clean = route?0:1;
1128         void * link;
1129         unsigned int i;
1130         double latency = 0.0;
1131
1132         route = route?route:rc->get_route(rc,src,dst);
1133
1134         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1135                 latency += get_link_latency(link);
1136         }
1137         if(need_to_clean) generic_free_extended_route(route);
1138   return latency;
1139 }
1140
1141 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1142 {
1143   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1144 }
1145
1146 route_extended_t generic_get_bypassroute(routing_component_t rc,
1147                                                 const char *src,
1148                                                 const char *dst)
1149 {
1150   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1151   routing_component_t src_as, dst_as;
1152   int index_src, index_dst;
1153   xbt_dynar_t path_src = NULL;
1154   xbt_dynar_t path_dst = NULL;
1155   routing_component_t current = NULL;
1156   routing_component_t *current_src = NULL;
1157   routing_component_t *current_dst = NULL;
1158
1159   /* (1) find the as where the src and dst are located */
1160   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1161   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1162   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1163   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1164
1165   if(src_data == NULL || dst_data == NULL)
1166           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1167                      src, dst, rc->name);
1168
1169   src_as = ((network_element_info_t)src_data)->rc_component;
1170   dst_as = ((network_element_info_t)dst_data)->rc_component;
1171
1172   /* (2) find the path to the root routing component */
1173   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1174   current = src_as;
1175   while (current != NULL) {
1176     xbt_dynar_push(path_src, &current);
1177     current = current->routing_father;
1178   }
1179   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1180   current = dst_as;
1181   while (current != NULL) {
1182     xbt_dynar_push(path_dst, &current);
1183     current = current->routing_father;
1184   }
1185
1186   /* (3) find the common father */
1187   index_src = path_src->used - 1;
1188   index_dst = path_dst->used - 1;
1189   current_src = xbt_dynar_get_ptr(path_src, index_src);
1190   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1191   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1192     xbt_dynar_pop_ptr(path_src);
1193     xbt_dynar_pop_ptr(path_dst);
1194     index_src--;
1195     index_dst--;
1196     current_src = xbt_dynar_get_ptr(path_src, index_src);
1197     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1198   }
1199
1200   int max_index_src = path_src->used - 1;
1201   int max_index_dst = path_dst->used - 1;
1202
1203   int max_index = max(max_index_src, max_index_dst);
1204   int i, max;
1205
1206   route_extended_t e_route_bypass = NULL;
1207
1208   for (max = 0; max <= max_index; max++) {
1209     for (i = 0; i < max; i++) {
1210       if (i <= max_index_src && max <= max_index_dst) {
1211         char *route_name = bprintf("%s#%s",
1212                                    (*(routing_component_t *)
1213                                     (xbt_dynar_get_ptr
1214                                      (path_src, i)))->name,
1215                                    (*(routing_component_t *)
1216                                     (xbt_dynar_get_ptr
1217                                      (path_dst, max)))->name);
1218         e_route_bypass =
1219             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1220         xbt_free(route_name);
1221       }
1222       if (e_route_bypass)
1223         break;
1224       if (max <= max_index_src && i <= max_index_dst) {
1225         char *route_name = bprintf("%s#%s",
1226                                    (*(routing_component_t *)
1227                                     (xbt_dynar_get_ptr
1228                                      (path_src, max)))->name,
1229                                    (*(routing_component_t *)
1230                                     (xbt_dynar_get_ptr
1231                                      (path_dst, i)))->name);
1232         e_route_bypass =
1233             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1234         xbt_free(route_name);
1235       }
1236       if (e_route_bypass)
1237         break;
1238     }
1239
1240     if (e_route_bypass)
1241       break;
1242
1243     if (max <= max_index_src && max <= max_index_dst) {
1244       char *route_name = bprintf("%s#%s",
1245                                  (*(routing_component_t *)
1246                                   (xbt_dynar_get_ptr
1247                                    (path_src, max)))->name,
1248                                  (*(routing_component_t *)
1249                                   (xbt_dynar_get_ptr
1250                                    (path_dst, max)))->name);
1251       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1252       xbt_free(route_name);
1253     }
1254     if (e_route_bypass)
1255       break;
1256   }
1257
1258   xbt_dynar_free(&path_src);
1259   xbt_dynar_free(&path_dst);
1260
1261   route_extended_t new_e_route = NULL;
1262
1263   if (e_route_bypass) {
1264     void *link;
1265     unsigned int cpt = 0;
1266     new_e_route = xbt_new0(s_route_extended_t, 1);
1267     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1268     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1269     new_e_route->generic_route.link_list =
1270         xbt_dynar_new(global_routing->size_of_link, NULL);
1271     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1272       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1273     }
1274   }
1275
1276   return new_e_route;
1277 }
1278
1279 /* ************************************************************************** */
1280 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1281
1282 route_t
1283 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1284                            void *data, int order)
1285 {
1286
1287   char *link_name;
1288   route_t new_route;
1289   unsigned int cpt;
1290   xbt_dynar_t links = NULL, links_id = NULL;
1291
1292   new_route = xbt_new0(s_route_t, 1);
1293   new_route->link_list =
1294       xbt_dynar_new(global_routing->size_of_link, NULL);
1295
1296   xbt_assert(hierarchy == SURF_ROUTING_BASE,
1297               "the hierarchy type is not SURF_ROUTING_BASE");
1298
1299   links = ((route_t) data)->link_list;
1300
1301
1302   links_id = new_route->link_list;
1303
1304   xbt_dynar_foreach(links, cpt, link_name) {
1305
1306     void *link =
1307                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1308     if (link) {
1309       if (order)
1310         xbt_dynar_push(links_id, &link);
1311       else
1312         xbt_dynar_unshift(links_id, &link);
1313     } else
1314       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1315   }
1316
1317   return new_route;
1318 }
1319
1320 route_extended_t
1321 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1322                            void *data, int order)
1323 {
1324
1325   char *link_name;
1326   route_extended_t e_route, new_e_route;
1327   route_t route;
1328   unsigned int cpt;
1329   xbt_dynar_t links = NULL, links_id = NULL;
1330
1331   new_e_route = xbt_new0(s_route_extended_t, 1);
1332   new_e_route->generic_route.link_list =
1333       xbt_dynar_new(global_routing->size_of_link, NULL);
1334   new_e_route->src_gateway = NULL;
1335   new_e_route->dst_gateway = NULL;
1336
1337   xbt_assert(hierarchy == SURF_ROUTING_BASE
1338               || hierarchy == SURF_ROUTING_RECURSIVE,
1339               "the hierarchy type is not defined");
1340
1341   if (hierarchy == SURF_ROUTING_BASE) {
1342
1343     route = (route_t) data;
1344     links = route->link_list;
1345
1346   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1347
1348     e_route = (route_extended_t) data;
1349     xbt_assert(e_route->src_gateway
1350                 && e_route->dst_gateway, "bad gateway, is null");
1351     links = e_route->generic_route.link_list;
1352
1353     /* remeber not erase the gateway names */
1354     new_e_route->src_gateway = strdup(e_route->src_gateway);
1355     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1356   }
1357
1358   links_id = new_e_route->generic_route.link_list;
1359
1360   xbt_dynar_foreach(links, cpt, link_name) {
1361
1362     void *link =
1363                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1364     if (link) {
1365       if (order)
1366         xbt_dynar_push(links_id, &link);
1367       else
1368         xbt_dynar_unshift(links_id, &link);
1369     } else
1370       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1371   }
1372
1373   return new_e_route;
1374 }
1375
1376 void generic_free_route(route_t route)
1377 {
1378   if (route) {
1379     xbt_dynar_free(&(route->link_list));
1380     xbt_free(route);
1381   }
1382 }
1383
1384 void generic_free_extended_route(route_extended_t e_route)
1385 {
1386   if (e_route) {
1387     xbt_dynar_free(&(e_route->generic_route.link_list));
1388     if (e_route->src_gateway)
1389       xbt_free(e_route->src_gateway);
1390     if (e_route->dst_gateway)
1391       xbt_free(e_route->dst_gateway);
1392     xbt_free(e_route);
1393   }
1394 }
1395
1396 static routing_component_t generic_as_exist(routing_component_t find_from,
1397                                             routing_component_t to_find)
1398 {
1399   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1400   xbt_dict_cursor_t cursor = NULL;
1401   char *key;
1402   int found = 0;
1403   routing_component_t elem;
1404   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1405     if (to_find == elem || generic_as_exist(elem, to_find)) {
1406       found = 1;
1407       break;
1408     }
1409   }
1410   if (found)
1411     return to_find;
1412   return NULL;
1413 }
1414
1415 routing_component_t
1416 generic_autonomous_system_exist(routing_component_t rc, char *element)
1417 {
1418   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1419   routing_component_t element_as, result, elem;
1420   xbt_dict_cursor_t cursor = NULL;
1421   char *key;
1422   element_as = ((network_element_info_t)
1423                 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1424   result = ((routing_component_t) - 1);
1425   if (element_as != rc)
1426     result = generic_as_exist(rc, element_as);
1427
1428   int found = 0;
1429   if (result) {
1430     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1431       found = !strcmp(elem->name, element);
1432       if (found)
1433         break;
1434     }
1435     if (found)
1436       return element_as;
1437   }
1438   return NULL;
1439 }
1440
1441 routing_component_t
1442 generic_processing_units_exist(routing_component_t rc, char *element)
1443 {
1444   routing_component_t element_as;
1445   element_as = ((network_element_info_t)
1446                 xbt_lib_get_or_null(host_lib,
1447                  element, ROUTING_HOST_LEVEL))->rc_component;
1448   if (element_as == rc)
1449     return element_as;
1450   return generic_as_exist(rc, element_as);
1451 }
1452
1453 void generic_src_dst_check(routing_component_t rc, const char *src,
1454                                   const char *dst)
1455 {
1456
1457   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1458   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1459   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1460   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1461
1462   if(src_data == NULL || dst_data == NULL)
1463           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1464                      src, dst, rc->name);
1465
1466   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1467   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1468
1469   if(src_as != dst_as)
1470           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1471               src, src_as->name, dst, dst_as->name);
1472   if(rc != dst_as)
1473          xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1474      src,dst,src_as->name, dst_as->name,rc->name);
1475 }
1476
1477 static void routing_parse_Sconfig(void)
1478 {
1479   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
1480 }
1481
1482 static void routing_parse_Econfig(void)
1483 {
1484   xbt_dict_cursor_t cursor = NULL;
1485   char *key;
1486   char *elem;
1487   char *cfg;
1488   xbt_dict_foreach(current_property_set, cursor, key, elem) {
1489           cfg = bprintf("%s:%s",key,elem);
1490           if(xbt_cfg_is_default_value(_surf_cfg_set, key))
1491                   xbt_cfg_set_parse(_surf_cfg_set, cfg);
1492           else
1493                   XBT_INFO("The custom configuration '%s' is already defined by user!",key);
1494           free(cfg);
1495   }
1496   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
1497 }
1498
1499 static void parse_create_host_link(int i)
1500 {
1501         char *host_id, *link_id = NULL;
1502         s_surf_parsing_host_arg_t host;
1503         memset(&host,0,sizeof(host));
1504
1505         host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, i, struct_cluster->V_cluster_suffix);
1506         link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, i);
1507
1508         A_surfxml_host_state = A_surfxml_host_state_ON;
1509
1510         XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1511         host.V_host_id = xbt_strdup(host_id);
1512         if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1513           xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1514           char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1515           xbt_str_varsubst(tmp_availability_file,patterns);
1516           XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1517           host.V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1518           xbt_free(tmp_availability_file);
1519         }
1520         else
1521         {
1522           XBT_DEBUG("\tavailability_file=\"\"");
1523         }
1524         if(strcmp(struct_cluster->V_cluster_state_file,"")){
1525           char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1526           xbt_str_varsubst(tmp_state_file,patterns);
1527           XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1528           host.V_host_state_trace = tmgr_trace_new(tmp_state_file);
1529           xbt_free(tmp_state_file);
1530         }
1531         else
1532         {
1533           XBT_DEBUG("\tstate_file=\"\"");
1534         }
1535
1536         host.V_host_power_peak = struct_cluster->S_cluster_power;
1537         host.V_host_power_scale = 1.0;
1538         host.V_host_core = struct_cluster->S_cluster_core;
1539         host.V_host_state_initial = SURF_RESOURCE_ON;
1540         host.V_host_coord = "";
1541         surf_parse_host(&host);
1542         XBT_DEBUG("</host>");
1543
1544         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1545         if(AX_surfxml_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1546         {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1547         if(AX_surfxml_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1548         {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1549
1550         XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1551
1552         struct_lnk = xbt_new0(s_surf_parsing_link_arg_t, 1);
1553         struct_lnk->V_link_id = xbt_strdup(link_id);
1554         struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bw;
1555         struct_lnk->V_link_latency = struct_cluster->S_cluster_lat;
1556         struct_lnk->V_link_bandwidth_file = NULL;
1557         struct_lnk->V_link_latency_file = NULL;
1558         struct_lnk->V_link_state_file = NULL;
1559         struct_lnk->V_link_state = SURF_RESOURCE_ON;
1560         struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1561
1562         if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1563                 struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1564         else
1565         {
1566          if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1567                  struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1568          else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1569                  struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1570         }
1571         surf_parse_link();
1572
1573         ETag_surfxml_host();
1574         ETag_surfxml_link();
1575
1576         surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1577
1578         if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
1579                 char* tmp_link =  bprintf("%s_UP",link_id);
1580                 info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1581                 free(tmp_link);
1582                 tmp_link =  bprintf("%s_DOWN",link_id);
1583                 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1584                 free(tmp_link);
1585         }
1586         else{
1587                 info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1588                 info->link_down = info->link_up;
1589         }
1590         xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1591         xbt_free(link_id);
1592         xbt_free(host_id);
1593 }
1594
1595 void routing_parse_Scluster(void)
1596 {
1597   if(!cluster_host_link)
1598           cluster_host_link = xbt_dict_new();
1599
1600   static int AX_ptr = 0;
1601   char *host_id, *groups, *link_id = NULL;
1602
1603   s_surf_parsing_host_arg_t host;
1604
1605   if( strcmp(struct_cluster->V_cluster_availability_file,"")
1606           || strcmp(struct_cluster->V_cluster_state_file,"") )
1607   {
1608           if(xbt_dict_size(patterns)==0)
1609                   patterns = xbt_dict_new();
1610           xbt_dict_set(patterns,"id",struct_cluster->V_cluster_id,NULL);
1611           xbt_dict_set(patterns,"prefix",struct_cluster->V_cluster_prefix,NULL);
1612           xbt_dict_set(patterns,"suffix",struct_cluster->V_cluster_suffix,NULL);
1613   }
1614
1615   unsigned int iter;
1616   int start, end, i;
1617   xbt_dynar_t radical_elements;
1618   xbt_dynar_t radical_ends;
1619
1620   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1621   static unsigned int surfxml_buffer_stack_stack[1024];
1622
1623   surfxml_buffer_stack_stack[0] = 0;
1624   surfxml_bufferstack_push(1);
1625
1626   SURFXML_BUFFER_SET(AS_id, struct_cluster->V_cluster_id);
1627   SURFXML_BUFFER_SET(AS_routing, "Cluster");
1628   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->V_cluster_id);
1629   SURFXML_START_TAG(AS);
1630
1631   //Make all hosts
1632   radical_elements = xbt_str_split(struct_cluster->V_cluster_radical, ",");
1633   xbt_dynar_foreach(radical_elements, iter, groups) {
1634     memset(&host,0,sizeof(host));
1635
1636     radical_ends = xbt_str_split(groups, "-");
1637     switch (xbt_dynar_length(radical_ends)) {
1638     case 1:
1639                 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
1640                 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, start, struct_cluster->V_cluster_suffix);
1641                 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, start);
1642
1643                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1644                 host.V_host_id = host_id;
1645                 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1646                   xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1647                   char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1648                   xbt_str_varsubst(tmp_availability_file,patterns);
1649                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1650                   host.V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1651                   xbt_free(tmp_availability_file);
1652                 }
1653                 else
1654                 {
1655                   XBT_DEBUG("\tavailability_file=\"\"");
1656                 }
1657                 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1658                   char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1659                   xbt_str_varsubst(tmp_state_file,patterns);
1660                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1661                   host.V_host_state_trace = tmgr_trace_new(tmp_state_file);
1662                   xbt_free(tmp_state_file);
1663                 }
1664                 else
1665                 {
1666                   XBT_DEBUG("\tstate_file=\"\"");
1667                 }
1668
1669                 host.V_host_power_peak = struct_cluster->S_cluster_power;
1670                 host.V_host_power_scale = 1.0;
1671                 host.V_host_core = struct_cluster->S_cluster_core;
1672                 host.V_host_state_initial = SURF_RESOURCE_ON;
1673                 host.V_host_coord = "";
1674                 surf_parse_host(&host);
1675                 XBT_DEBUG("</host>");
1676
1677                 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1678                 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1679                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1680                 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1681                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1682
1683                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1684
1685                 struct_lnk = xbt_new0(s_surf_parsing_link_arg_t, 1);
1686                 struct_lnk->V_link_id = link_id;
1687                 struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bw;
1688                 struct_lnk->V_link_latency = struct_cluster->S_cluster_lat;
1689                 struct_lnk->V_link_bandwidth_file = NULL;
1690                 struct_lnk->V_link_latency_file = NULL;
1691                 struct_lnk->V_link_state_file = NULL;
1692                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
1693                 struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1694
1695                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1696                         struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1697                 else
1698                 {
1699                  if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1700                          struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1701                  else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1702                          struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1703                 }
1704                 surf_parse_link();
1705
1706                 xbt_dict_set(cluster_host_link,host_id,strdup(link_id),free);
1707 //              XBT_INFO("key '%s' Value '%s'",host_id,link_id);
1708                 ETag_surfxml_host();
1709                 ETag_surfxml_link();
1710
1711                 break;
1712
1713     case 2:
1714
1715       surf_parse_get_int(&start,
1716                          xbt_dynar_get_as(radical_ends, 0, char *));
1717       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
1718       for (i = start; i <= end; i++) {
1719                 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, i, struct_cluster->V_cluster_suffix);
1720                 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, i);
1721
1722                 A_surfxml_host_state = A_surfxml_host_state_ON;
1723
1724                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1725                 host.V_host_id = host_id;
1726                 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1727                   xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1728                   char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1729                   xbt_str_varsubst(tmp_availability_file,patterns);
1730                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1731                   host.V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1732                   xbt_free(tmp_availability_file);
1733                 }
1734                 else
1735                 {
1736                   XBT_DEBUG("\tavailability_file=\"\"");
1737                 }
1738                 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1739                   char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1740                   xbt_str_varsubst(tmp_state_file,patterns);
1741                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1742                   host.V_host_state_trace = tmgr_trace_new(tmp_state_file);
1743                   xbt_free(tmp_state_file);
1744                 }
1745                 else
1746                 {
1747                   XBT_DEBUG("\tstate_file=\"\"");
1748                 }
1749
1750                 host.V_host_power_peak = struct_cluster->S_cluster_power;
1751                 host.V_host_power_scale = 1.0;
1752                 host.V_host_core = struct_cluster->S_cluster_core;
1753                 host.V_host_state_initial = SURF_RESOURCE_ON;
1754                 host.V_host_coord = "";
1755                 surf_parse_host(&host);
1756                 XBT_DEBUG("</host>");
1757
1758                 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1759                 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1760                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1761                 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1762                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1763
1764                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1765
1766                 struct_lnk = xbt_new0(s_surf_parsing_link_arg_t, 1);
1767                 struct_lnk->V_link_id = link_id;
1768                 struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bw;
1769                 struct_lnk->V_link_latency = struct_cluster->S_cluster_lat;
1770                 struct_lnk->V_link_bandwidth_file = NULL;
1771                 struct_lnk->V_link_latency_file = NULL;
1772                 struct_lnk->V_link_state_file = NULL;
1773                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
1774                 struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1775
1776                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1777                         struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1778                 else
1779                 {
1780                  if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1781                          struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1782                  else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1783                          struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1784                 }
1785                 surf_parse_link();
1786
1787                 xbt_dict_set(cluster_host_link,host_id,strdup(link_id),free);
1788 //              XBT_INFO("key '%s' Value '%s'",host_id,link_id);
1789
1790                 ETag_surfxml_host();
1791                 ETag_surfxml_link();
1792
1793       }
1794       break;
1795
1796     default:
1797       XBT_DEBUG("Malformed radical");
1798       break;
1799     }
1800
1801     xbt_dynar_free(&radical_ends);
1802   }
1803   xbt_dynar_free(&radical_elements);
1804
1805   //Make the router
1806   XBT_DEBUG(" ");
1807   XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->S_cluster_router_id);
1808   SURFXML_BUFFER_SET(router_id, struct_cluster->S_cluster_router_id);
1809   SURFXML_BUFFER_SET(router_coordinates, "");
1810   SURFXML_START_TAG(router);
1811   SURFXML_END_TAG(router);
1812
1813   //Make the backbone
1814   if( (struct_cluster->S_cluster_bb_bw!= 0)  && (struct_cluster->S_cluster_bb_lat!=0)  ){
1815           char *link_backbone = bprintf("%s_backbone", struct_cluster->V_cluster_id);
1816           XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->S_cluster_bb_bw, struct_cluster->S_cluster_bb_lat);
1817
1818           A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1819           if(AX_surfxml_cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
1820           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1821
1822           struct_lnk = xbt_new0(s_surf_parsing_link_arg_t, 1);
1823           struct_lnk->V_link_id = xbt_strdup(link_backbone);
1824           struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bb_bw;
1825           struct_lnk->V_link_latency = struct_cluster->S_cluster_bb_lat;
1826           struct_lnk->V_link_bandwidth_file = NULL;
1827           struct_lnk->V_link_latency_file = NULL;
1828           struct_lnk->V_link_state_file = NULL;
1829           struct_lnk->V_link_state = SURF_RESOURCE_ON;
1830           struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1831
1832           if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1833                   struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1834           else
1835                   struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1836
1837           surf_parse_link();
1838           ETag_surfxml_link();
1839
1840           surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1841           info->link_up   = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1842           info->link_down = info->link_up;
1843           xbt_dict_set(cluster_host_link,struct_cluster->V_cluster_id,info,xbt_free);
1844           free(link_backbone);
1845   }
1846
1847   XBT_DEBUG(" ");
1848
1849   char *new_suffix = xbt_strdup("");
1850
1851   radical_elements = xbt_str_split(struct_cluster->V_cluster_suffix, ".");
1852   xbt_dynar_foreach(radical_elements, iter, groups) {
1853     if (strcmp(groups, "")) {
1854       char *old_suffix = new_suffix;
1855       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1856       free(old_suffix);
1857     }
1858   }
1859
1860   xbt_dynar_free(&radical_elements);
1861   xbt_free(new_suffix);
1862
1863   if( strcmp(struct_cluster->V_cluster_availability_file,"")
1864                   || strcmp(struct_cluster->V_cluster_state_file,"") )
1865           xbt_dict_free(&patterns);
1866
1867   XBT_DEBUG("</AS>");
1868   SURFXML_END_TAG(AS);
1869   XBT_DEBUG(" ");
1870
1871   surfxml_bufferstack_pop(1);
1872 }
1873 /*
1874  * This function take a string and replace parameters from patterns dict.
1875  * It returns the new value.
1876  */
1877 static char* replace_random_parameter(char * string)
1878 {
1879   char *test_string = NULL;
1880
1881   if(xbt_dict_size(random_value)==0)
1882     return string;
1883
1884   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1885   test_string = bprintf("${%s}", string);
1886   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1887
1888   if (strcmp(test_string,"")) { //if not empty, keep this value.
1889     xbt_free(string);
1890     string = test_string;
1891   } //In other case take old value (without ${})
1892   else
1893         free(test_string);
1894   return string;
1895 }
1896
1897 static void clean_routing_after_parse(void)
1898 {
1899         xbt_dict_free(&random_value);
1900         xbt_dict_free(&patterns);
1901 }
1902
1903 static void routing_parse_Speer(void)
1904 {
1905   static int AX_ptr = 0;
1906   char *host_id = NULL;
1907   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1908
1909   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1910   static unsigned int surfxml_buffer_stack_stack[1024];
1911
1912   surfxml_buffer_stack_stack[0] = 0;
1913
1914   surfxml_bufferstack_push(1);
1915
1916   SURFXML_BUFFER_SET(AS_id, struct_peer->V_peer_id);
1917
1918   SURFXML_BUFFER_SET(AS_routing, "Full");
1919   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", struct_peer->V_peer_id);
1920
1921   SURFXML_START_TAG(AS);
1922
1923   XBT_DEBUG(" ");
1924   host_id = bprintf("peer_%s", struct_peer->V_peer_id);
1925   router_id = bprintf("router_%s", struct_peer->V_peer_id);
1926   link_id_up = bprintf("link_%s_up", struct_peer->V_peer_id);
1927   link_id_down = bprintf("link_%s_down", struct_peer->V_peer_id);
1928
1929   link_router = bprintf("%s_link_router", struct_peer->V_peer_id);
1930   link_backbone = bprintf("%s_backbone", struct_peer->V_peer_id);
1931
1932   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, struct_peer->V_peer_power);
1933   A_surfxml_host_state = A_surfxml_host_state_ON;
1934   SURFXML_BUFFER_SET(host_id, host_id);
1935   SURFXML_BUFFER_SET(host_power, struct_peer->V_peer_power);
1936   SURFXML_BUFFER_SET(host_availability, "1.0");
1937   SURFXML_BUFFER_SET(host_availability_file, struct_peer->V_peer_availability_trace);
1938   SURFXML_BUFFER_SET(host_state_file, struct_peer->V_peer_state_trace);
1939   SURFXML_BUFFER_SET(host_coordinates, "");
1940   SURFXML_BUFFER_SET(host_core, "1.0");
1941   SURFXML_START_TAG(host);
1942   SURFXML_END_TAG(host);
1943
1944   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, struct_peer->V_peer_coord);
1945   SURFXML_BUFFER_SET(router_id, router_id);
1946   SURFXML_BUFFER_SET(router_coordinates, struct_peer->V_peer_coord);
1947   SURFXML_START_TAG(router);
1948   SURFXML_END_TAG(router);
1949
1950   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, struct_peer->V_peer_bw_in, struct_peer->V_peer_lat);
1951   A_surfxml_link_state = A_surfxml_link_state_ON;
1952   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1953   SURFXML_BUFFER_SET(link_id, link_id_up);
1954   SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_in);
1955   SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1956   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1957   SURFXML_BUFFER_SET(link_latency_file, "");
1958   SURFXML_BUFFER_SET(link_state_file, "");
1959   SURFXML_START_TAG(link);
1960   SURFXML_END_TAG(link);
1961
1962   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, struct_peer->V_peer_bw_out, struct_peer->V_peer_lat);
1963   A_surfxml_link_state = A_surfxml_link_state_ON;
1964   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1965   SURFXML_BUFFER_SET(link_id, link_id_down);
1966   SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_out);
1967   SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1968   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1969   SURFXML_BUFFER_SET(link_latency_file, "");
1970   SURFXML_BUFFER_SET(link_state_file, "");
1971   SURFXML_START_TAG(link);
1972   SURFXML_END_TAG(link);
1973
1974   XBT_DEBUG(" ");
1975
1976   // begin here
1977   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1978   XBT_DEBUG("symmetrical=\"NO\">");
1979   SURFXML_BUFFER_SET(route_src, host_id);
1980   SURFXML_BUFFER_SET(route_dst, router_id);
1981   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1982   SURFXML_START_TAG(route);
1983
1984   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1985   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1986   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1987   SURFXML_START_TAG(link_ctn);
1988   SURFXML_END_TAG(link_ctn);
1989
1990   XBT_DEBUG("</route>");
1991   SURFXML_END_TAG(route);
1992
1993   //Opposite Route
1994   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1995   XBT_DEBUG("symmetrical=\"NO\">");
1996   SURFXML_BUFFER_SET(route_src, router_id);
1997   SURFXML_BUFFER_SET(route_dst, host_id);
1998   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1999   SURFXML_START_TAG(route);
2000
2001   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
2002   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
2003   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
2004   SURFXML_START_TAG(link_ctn);
2005   SURFXML_END_TAG(link_ctn);
2006
2007   XBT_DEBUG("</route>");
2008   SURFXML_END_TAG(route);
2009
2010   XBT_DEBUG("</AS>");
2011   SURFXML_END_TAG(AS);
2012   XBT_DEBUG(" ");
2013
2014   //xbt_dynar_free(&tab_elements_num);
2015         free(host_id);
2016         free(router_id);
2017         free(link_router);
2018         free(link_backbone);
2019         free(link_id_up);
2020         free(link_id_down);
2021   surfxml_bufferstack_pop(1);
2022 }
2023
2024 static void routing_parse_Srandom(void)
2025 {
2026           double mean, std, min, max, seed;
2027           char *random_id = A_surfxml_random_id;
2028           char *random_radical = A_surfxml_random_radical;
2029           char *rd_name = NULL;
2030           char *rd_value;
2031           surf_parse_get_double(&mean,A_surfxml_random_mean);
2032           surf_parse_get_double(&std,A_surfxml_random_std_deviation);
2033           surf_parse_get_double(&min,A_surfxml_random_min);
2034           surf_parse_get_double(&max,A_surfxml_random_max);
2035           surf_parse_get_double(&seed,A_surfxml_random_seed);
2036
2037           double res = 0;
2038           int i = 0;
2039           random_data_t random = xbt_new0(s_random_data_t, 1);
2040           char *tmpbuf;
2041
2042           xbt_dynar_t radical_elements;
2043           unsigned int iter;
2044           char *groups;
2045           int start, end;
2046           xbt_dynar_t radical_ends;
2047
2048           random->generator = A_surfxml_random_generator;
2049           random->seed = seed;
2050           random->min = min;
2051           random->max = max;
2052
2053           /* Check user stupidities */
2054           if (max < min)
2055             THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
2056           if (mean < min)
2057             THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
2058                    min);
2059           if (mean > max)
2060             THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
2061                    max);
2062
2063           /* normalize the mean and standard deviation before storing */
2064           random->mean = (mean - min) / (max - min);
2065           random->std = std / (max - min);
2066
2067           if (random->mean * (1 - random->mean) < random->std * random->std)
2068             THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
2069                    random->mean, random->std);
2070
2071           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
2072           random_id,
2073           random->min,
2074           random->max,
2075           random->mean,
2076           random->std,
2077           random->generator,
2078           random->seed,
2079           random_radical);
2080
2081           if(xbt_dict_size(random_value)==0)
2082                   random_value = xbt_dict_new();
2083
2084           if(!strcmp(random_radical,""))
2085           {
2086                   res = random_generate(random);
2087                   rd_value = bprintf("%f",res);
2088                   xbt_dict_set(random_value, random_id, rd_value, free);
2089           }
2090           else
2091           {
2092                   radical_elements = xbt_str_split(random_radical, ",");
2093                   xbt_dynar_foreach(radical_elements, iter, groups) {
2094                         radical_ends = xbt_str_split(groups, "-");
2095                         switch (xbt_dynar_length(radical_ends)) {
2096                         case 1:
2097                                           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
2098                                           res = random_generate(random);
2099                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
2100                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2101                                           xbt_free(tmpbuf);
2102                                           break;
2103
2104                         case 2:   surf_parse_get_int(&start,
2105                                                                                  xbt_dynar_get_as(radical_ends, 0, char *));
2106                                           surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
2107                                           for (i = start; i <= end; i++) {
2108                                                   xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
2109                                                   res = random_generate(random);
2110                           tmpbuf = bprintf("%s%d",random_id,i);
2111                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2112                           xbt_free(tmpbuf);
2113                                           }
2114                                           break;
2115                         default:
2116                                 XBT_INFO("Malformed radical");
2117                                 break;
2118                         }
2119                         res = random_generate(random);
2120                         rd_name  = bprintf("%s_router",random_id);
2121                         rd_value = bprintf("%f",res);
2122                         xbt_dict_set(random_value, rd_name, rd_value, free);
2123
2124                         xbt_dynar_free(&radical_ends);
2125                   }
2126                   free(rd_name);
2127                   xbt_dynar_free(&radical_elements);
2128           }
2129 }
2130
2131 static void routing_parse_Erandom(void)
2132 {
2133         /*xbt_dict_cursor_t cursor = NULL;
2134         char *key;
2135         char *elem;
2136
2137         xbt_dict_foreach(random_value, cursor, key, elem) {
2138           XBT_DEBUG("%s = %s",key,elem);
2139         }
2140 */
2141 }
2142
2143 /*
2144  * New methods to init the routing model component from the lua script
2145  */
2146
2147
2148 /*
2149  * add a host to the network element list
2150  */
2151
2152 void routing_add_host(const char *host_id)
2153 {
2154   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
2155 }
2156
2157 /*
2158  * Set a new link on the actual list of link for a route or ASroute
2159  */
2160 void routing_add_link(const char *link_id)
2161 {
2162   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
2163 }
2164
2165 /*
2166  *Set the endpoints for a route
2167  */
2168 void routing_set_route(const char *src_id, const char *dst_id)
2169 {
2170   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
2171 }
2172
2173 /*
2174  * Store the route by calling parse_E_route_store_route
2175  */
2176 void routing_store_route(void)
2177 {
2178   parse_E_route_store_route();
2179 }