Logo AND Algorithmique Numérique Distribuée

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