Logo AND Algorithmique Numérique Distribuée

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