Logo AND Algorithmique Numérique Distribuée

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