Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f6a55decbcb08fddc809ccf7f47ae10b8150a181
[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(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
1512   if( strcmp(struct_cluster->V_cluster_availability_file,"")
1513           || strcmp(struct_cluster->V_cluster_state_file,"") )
1514   {
1515           if(xbt_dict_size(patterns)==0)
1516                   patterns = xbt_dict_new();
1517           XBT_INFO("'%s' '%s'",A_surfxml_cluster_state_file,struct_cluster->V_cluster_availability_file);
1518           xbt_dict_set(patterns,"id",struct_cluster->V_cluster_id,NULL);
1519           xbt_dict_set(patterns,"prefix",struct_cluster->V_cluster_prefix,NULL);
1520           xbt_dict_set(patterns,"suffix",struct_cluster->V_cluster_suffix,NULL);
1521   }
1522
1523   char *route_src_dst;
1524   unsigned int iter;
1525   int start, end, i;
1526   xbt_dynar_t radical_elements;
1527   xbt_dynar_t radical_ends;
1528   int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
1529   int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
1530   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1531           xbt_dict_set(cluster_host_link,struct_cluster->V_cluster_id,bprintf("fullduplex"),free);
1532
1533   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1534   static unsigned int surfxml_buffer_stack_stack[1024];
1535
1536   surfxml_buffer_stack_stack[0] = 0;
1537
1538   surfxml_bufferstack_push(1);
1539
1540   SURFXML_BUFFER_SET(AS_id, struct_cluster->V_cluster_id);
1541   SURFXML_BUFFER_SET(AS_routing, "Cluster");
1542   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->V_cluster_id);
1543   SURFXML_START_TAG(AS);
1544
1545   radical_elements = xbt_str_split(struct_cluster->V_cluster_radical, ",");
1546   xbt_dynar_foreach(radical_elements, iter, groups) {
1547     radical_ends = xbt_str_split(groups, "-");
1548     switch (xbt_dynar_length(radical_ends)) {
1549     case 1:
1550                 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
1551                 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, start, struct_cluster->V_cluster_suffix);
1552                 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, start);
1553
1554                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1555                 struct_host = xbt_new0(s_hostSG_t, 1);
1556                 struct_host->V_host_id = host_id;
1557                 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1558                   xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1559                   char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1560                   xbt_str_varsubst(tmp_availability_file,patterns);
1561                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1562                   struct_host->V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1563                   xbt_free(tmp_availability_file);
1564                 }
1565                 else
1566                 {
1567                   XBT_DEBUG("\tavailability_file=\"\"");
1568                 }
1569                 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1570                   char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1571                   xbt_str_varsubst(tmp_state_file,patterns);
1572                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1573                   struct_host->V_host_state_trace = tmgr_trace_new(tmp_state_file);
1574                   xbt_free(tmp_state_file);
1575                 }
1576                 else
1577                 {
1578                   XBT_DEBUG("\tstate_file=\"\"");
1579                 }
1580
1581                 struct_host->V_host_power_peak = struct_cluster->S_cluster_power;
1582                 struct_host->V_host_power_scale = 1.0;
1583                 struct_host->V_host_core = struct_cluster->S_cluster_core;
1584                 struct_host->V_host_state_initial = SURF_RESOURCE_ON;
1585                 struct_host->V_host_coord = "";
1586                 STag_surfxml_host_cluster();
1587                 XBT_DEBUG("</host>");
1588
1589                 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1590                 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1591                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1592                 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1593                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1594
1595                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1596
1597                 struct_lnk = xbt_new0(s_link_t, 1);
1598                 struct_lnk->V_link_id = link_id;
1599                 struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bw;
1600                 struct_lnk->V_link_latency = struct_cluster->S_cluster_lat;
1601                 struct_lnk->V_link_bandwidth_file = NULL;
1602                 struct_lnk->V_link_latency_file = NULL;
1603                 struct_lnk->V_link_state_file = NULL;
1604                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
1605                 struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1606
1607                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1608                         struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1609                 else
1610                 {
1611                  if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1612                          struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1613                  else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1614                          struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1615                 }
1616                 STag_surfxml_link_cluster();
1617
1618                 xbt_dict_set(cluster_host_link,host_id,strdup(link_id),free);
1619 //              XBT_INFO("key '%s' Value '%s'",host_id,link_id);
1620                 ETag_surfxml_host();
1621                 ETag_surfxml_link();
1622
1623                 break;
1624
1625     case 2:
1626
1627       surf_parse_get_int(&start,
1628                          xbt_dynar_get_as(radical_ends, 0, char *));
1629       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
1630       for (i = start; i <= end; i++) {
1631                 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, i, struct_cluster->V_cluster_suffix);
1632                 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, i);
1633
1634                 A_surfxml_host_state = A_surfxml_host_state_ON;
1635
1636                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1637                 struct_host = xbt_new0(s_hostSG_t, 1);
1638                 struct_host->V_host_id = host_id;
1639                 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1640                   xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1641                   char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1642                   xbt_str_varsubst(tmp_availability_file,patterns);
1643                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1644                   struct_host->V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1645                   xbt_free(tmp_availability_file);
1646                 }
1647                 else
1648                 {
1649                   XBT_DEBUG("\tavailability_file=\"\"");
1650                 }
1651                 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1652                   char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1653                   xbt_str_varsubst(tmp_state_file,patterns);
1654                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1655                   struct_host->V_host_state_trace = tmgr_trace_new(tmp_state_file);
1656                   xbt_free(tmp_state_file);
1657                 }
1658                 else
1659                 {
1660                   XBT_DEBUG("\tstate_file=\"\"");
1661                 }
1662
1663                 struct_host->V_host_power_peak = struct_cluster->S_cluster_power;
1664                 struct_host->V_host_power_scale = 1.0;
1665                 struct_host->V_host_core = struct_cluster->S_cluster_core;
1666                 struct_host->V_host_state_initial = SURF_RESOURCE_ON;
1667                 struct_host->V_host_coord = "";
1668                 STag_surfxml_host_cluster();
1669                 XBT_DEBUG("</host>");
1670
1671                 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1672                 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1673                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1674                 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1675                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1676
1677                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1678
1679                 struct_lnk = xbt_new0(s_link_t, 1);
1680                 struct_lnk->V_link_id = link_id;
1681                 struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bw;
1682                 struct_lnk->V_link_latency = struct_cluster->S_cluster_lat;
1683                 struct_lnk->V_link_bandwidth_file = NULL;
1684                 struct_lnk->V_link_latency_file = NULL;
1685                 struct_lnk->V_link_state_file = NULL;
1686                 struct_lnk->V_link_state = SURF_RESOURCE_ON;
1687                 struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1688
1689                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1690                         struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1691                 else
1692                 {
1693                  if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1694                          struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1695                  else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1696                          struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1697                 }
1698                 STag_surfxml_link_cluster();
1699
1700                 xbt_dict_set(cluster_host_link,host_id,strdup(link_id),free);
1701 //              XBT_INFO("key '%s' Value '%s'",host_id,link_id);
1702
1703                 ETag_surfxml_host();
1704                 ETag_surfxml_link();
1705
1706       }
1707       break;
1708
1709     default:
1710       XBT_DEBUG("Malformed radical");
1711       break;
1712     }
1713
1714     xbt_dynar_free(&radical_ends);
1715   }
1716   xbt_dynar_free(&radical_elements);
1717
1718   XBT_DEBUG(" ");
1719   XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->S_cluster_router_id);
1720   SURFXML_BUFFER_SET(router_id, struct_cluster->S_cluster_router_id);
1721   SURFXML_BUFFER_SET(router_coordinates, "");
1722   SURFXML_START_TAG(router);
1723   SURFXML_END_TAG(router);
1724
1725   if( (struct_cluster->S_cluster_bb_bw!= 0)  && (struct_cluster->S_cluster_bb_lat!=0)  ){
1726           char *link_backbone = bprintf("%s_backbone", struct_cluster->V_cluster_id);
1727           XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->S_cluster_bb_bw, struct_cluster->S_cluster_bb_lat);
1728
1729           A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1730           if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
1731           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1732
1733           struct_lnk = xbt_new0(s_link_t, 1);
1734           struct_lnk->V_link_id = link_backbone;
1735           struct_lnk->V_link_bandwidth = struct_cluster->S_cluster_bb_bw;
1736           struct_lnk->V_link_latency = struct_cluster->S_cluster_bb_lat;
1737           struct_lnk->V_link_bandwidth_file = NULL;
1738           struct_lnk->V_link_latency_file = NULL;
1739           struct_lnk->V_link_state_file = NULL;
1740           struct_lnk->V_link_state = SURF_RESOURCE_ON;
1741           struct_lnk->V_link_sharing_policy = A_surfxml_link_sharing_policy;
1742
1743           if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1744                   struct_lnk->V_policy_initial_link = SURF_LINK_SHARED;
1745           else
1746           {
1747            if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1748                    struct_lnk->V_policy_initial_link = SURF_LINK_FATPIPE;
1749            else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1750                    struct_lnk->V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1751           }
1752
1753           STag_surfxml_link_cluster();
1754           ETag_surfxml_link();
1755   }
1756
1757   XBT_DEBUG(" ");
1758
1759   char *new_suffix = xbt_strdup("");
1760
1761   radical_elements = xbt_str_split(struct_cluster->V_cluster_suffix, ".");
1762   xbt_dynar_foreach(radical_elements, iter, groups) {
1763     if (strcmp(groups, "")) {
1764       char *old_suffix = new_suffix;
1765       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1766       free(old_suffix);
1767     }
1768   }
1769
1770   xbt_dynar_free(&radical_elements);
1771   xbt_free(new_suffix);
1772
1773   if( strcmp(struct_cluster->V_cluster_availability_file,"")
1774                   || strcmp(struct_cluster->V_cluster_state_file,"") )
1775           xbt_dict_free(&patterns);
1776
1777   XBT_DEBUG("</AS>");
1778   SURFXML_END_TAG(AS);
1779   XBT_DEBUG(" ");
1780
1781   surfxml_bufferstack_pop(1);
1782
1783 //  xbt_dict_cursor_t cursor = NULL;
1784 //  char *key,*data;
1785 //
1786 //  XBT_INFO("\n");
1787 //  xbt_dict_foreach(cluster_host_link,cursor,key,data){
1788 //        XBT_INFO("%s : %s",key,data);
1789 //  }
1790 }
1791 /*
1792  * This function take a string and replace parameters from patterns dict.
1793  * It returns the new value.
1794  */
1795 static char* replace_random_parameter(char * string)
1796 {
1797   char *test_string = NULL;
1798
1799   if(xbt_dict_size(random_value)==0)
1800     return string;
1801
1802   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1803   test_string = bprintf("${%s}", string);
1804   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1805
1806   if (strcmp(test_string,"")) { //if not empty, keep this value.
1807     xbt_free(string);
1808     string = test_string;
1809   } //In other case take old value (without ${})
1810   else
1811         free(test_string);
1812   return string;
1813 }
1814
1815 static void clean_routing_after_parse(void)
1816 {
1817         xbt_dict_free(&random_value);
1818         xbt_dict_free(&patterns);
1819 }
1820
1821 static void routing_parse_Speer(void)
1822 {
1823   static int AX_ptr = 0;
1824   char *host_id = NULL;
1825   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1826
1827   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1828   static unsigned int surfxml_buffer_stack_stack[1024];
1829
1830   surfxml_buffer_stack_stack[0] = 0;
1831
1832   surfxml_bufferstack_push(1);
1833
1834   SURFXML_BUFFER_SET(AS_id, struct_peer->V_peer_id);
1835
1836   SURFXML_BUFFER_SET(AS_routing, "Full");
1837   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", struct_peer->V_peer_id);
1838
1839   SURFXML_START_TAG(AS);
1840
1841   XBT_DEBUG(" ");
1842   host_id = bprintf("peer_%s", struct_peer->V_peer_id);
1843   router_id = bprintf("router_%s", struct_peer->V_peer_id);
1844   link_id_up = bprintf("link_%s_up", struct_peer->V_peer_id);
1845   link_id_down = bprintf("link_%s_down", struct_peer->V_peer_id);
1846
1847   link_router = bprintf("%s_link_router", struct_peer->V_peer_id);
1848   link_backbone = bprintf("%s_backbone", struct_peer->V_peer_id);
1849
1850   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, struct_peer->V_peer_power);
1851   A_surfxml_host_state = A_surfxml_host_state_ON;
1852   SURFXML_BUFFER_SET(host_id, host_id);
1853   SURFXML_BUFFER_SET(host_power, struct_peer->V_peer_power);
1854   SURFXML_BUFFER_SET(host_availability, "1.0");
1855   SURFXML_BUFFER_SET(host_availability_file, struct_peer->V_peer_availability_trace);
1856   SURFXML_BUFFER_SET(host_state_file, struct_peer->V_peer_state_trace);
1857   SURFXML_BUFFER_SET(host_coordinates, "");
1858   SURFXML_BUFFER_SET(host_core, "1.0");
1859   SURFXML_START_TAG(host);
1860   SURFXML_END_TAG(host);
1861
1862   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, struct_peer->V_peer_coord);
1863   SURFXML_BUFFER_SET(router_id, router_id);
1864   SURFXML_BUFFER_SET(router_coordinates, struct_peer->V_peer_coord);
1865   SURFXML_START_TAG(router);
1866   SURFXML_END_TAG(router);
1867
1868   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, struct_peer->V_peer_bw_in, struct_peer->V_peer_lat);
1869   A_surfxml_link_state = A_surfxml_link_state_ON;
1870   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1871   SURFXML_BUFFER_SET(link_id, link_id_up);
1872   SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_in);
1873   SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1874   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1875   SURFXML_BUFFER_SET(link_latency_file, "");
1876   SURFXML_BUFFER_SET(link_state_file, "");
1877   SURFXML_START_TAG(link);
1878   SURFXML_END_TAG(link);
1879
1880   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, struct_peer->V_peer_bw_out, struct_peer->V_peer_lat);
1881   A_surfxml_link_state = A_surfxml_link_state_ON;
1882   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1883   SURFXML_BUFFER_SET(link_id, link_id_down);
1884   SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_out);
1885   SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1886   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1887   SURFXML_BUFFER_SET(link_latency_file, "");
1888   SURFXML_BUFFER_SET(link_state_file, "");
1889   SURFXML_START_TAG(link);
1890   SURFXML_END_TAG(link);
1891
1892   XBT_DEBUG(" ");
1893
1894   // begin here
1895   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1896   XBT_DEBUG("symmetrical=\"NO\">");
1897   SURFXML_BUFFER_SET(route_src, host_id);
1898   SURFXML_BUFFER_SET(route_dst, router_id);
1899   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1900   SURFXML_START_TAG(route);
1901
1902   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1903   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1904   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1905   SURFXML_START_TAG(link_ctn);
1906   SURFXML_END_TAG(link_ctn);
1907
1908   XBT_DEBUG("</route>");
1909   SURFXML_END_TAG(route);
1910
1911   //Opposite Route
1912   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1913   XBT_DEBUG("symmetrical=\"NO\">");
1914   SURFXML_BUFFER_SET(route_src, router_id);
1915   SURFXML_BUFFER_SET(route_dst, host_id);
1916   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1917   SURFXML_START_TAG(route);
1918
1919   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1920   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1921   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1922   SURFXML_START_TAG(link_ctn);
1923   SURFXML_END_TAG(link_ctn);
1924
1925   XBT_DEBUG("</route>");
1926   SURFXML_END_TAG(route);
1927
1928   XBT_DEBUG("</AS>");
1929   SURFXML_END_TAG(AS);
1930   XBT_DEBUG(" ");
1931
1932   //xbt_dynar_free(&tab_elements_num);
1933         free(host_id);
1934         free(router_id);
1935         free(link_router);
1936         free(link_backbone);
1937         free(link_id_up);
1938         free(link_id_down);
1939   surfxml_bufferstack_pop(1);
1940 }
1941
1942 static void routing_parse_Srandom(void)
1943 {
1944           double mean, std, min, max, seed;
1945           char *random_id = A_surfxml_random_id;
1946           char *random_radical = A_surfxml_random_radical;
1947           char *rd_name = NULL;
1948           char *rd_value;
1949           surf_parse_get_double(&mean,A_surfxml_random_mean);
1950           surf_parse_get_double(&std,A_surfxml_random_std_deviation);
1951           surf_parse_get_double(&min,A_surfxml_random_min);
1952           surf_parse_get_double(&max,A_surfxml_random_max);
1953           surf_parse_get_double(&seed,A_surfxml_random_seed);
1954
1955           double res = 0;
1956           int i = 0;
1957           random_data_t random = xbt_new0(s_random_data_t, 1);
1958           char *tmpbuf;
1959
1960           xbt_dynar_t radical_elements;
1961           unsigned int iter;
1962           char *groups;
1963           int start, end;
1964           xbt_dynar_t radical_ends;
1965
1966           random->generator = A_surfxml_random_generator;
1967           random->seed = seed;
1968           random->min = min;
1969           random->max = max;
1970
1971           /* Check user stupidities */
1972           if (max < min)
1973             THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1974           if (mean < min)
1975             THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1976                    min);
1977           if (mean > max)
1978             THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1979                    max);
1980
1981           /* normalize the mean and standard deviation before storing */
1982           random->mean = (mean - min) / (max - min);
1983           random->std = std / (max - min);
1984
1985           if (random->mean * (1 - random->mean) < random->std * random->std)
1986             THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1987                    random->mean, random->std);
1988
1989           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1990           random_id,
1991           random->min,
1992           random->max,
1993           random->mean,
1994           random->std,
1995           random->generator,
1996           random->seed,
1997           random_radical);
1998
1999           if(xbt_dict_size(random_value)==0)
2000                   random_value = xbt_dict_new();
2001
2002           if(!strcmp(random_radical,""))
2003           {
2004                   res = random_generate(random);
2005                   rd_value = bprintf("%f",res);
2006                   xbt_dict_set(random_value, random_id, rd_value, free);
2007           }
2008           else
2009           {
2010                   radical_elements = xbt_str_split(random_radical, ",");
2011                   xbt_dynar_foreach(radical_elements, iter, groups) {
2012                         radical_ends = xbt_str_split(groups, "-");
2013                         switch (xbt_dynar_length(radical_ends)) {
2014                         case 1:
2015                                           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
2016                                           res = random_generate(random);
2017                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
2018                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2019                                           xbt_free(tmpbuf);
2020                                           break;
2021
2022                         case 2:   surf_parse_get_int(&start,
2023                                                                                  xbt_dynar_get_as(radical_ends, 0, char *));
2024                                           surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
2025                                           for (i = start; i <= end; i++) {
2026                                                   xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
2027                                                   res = random_generate(random);
2028                           tmpbuf = bprintf("%s%d",random_id,i);
2029                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2030                           xbt_free(tmpbuf);
2031                                           }
2032                                           break;
2033                         default:
2034                                 XBT_INFO("Malformed radical");
2035                                 break;
2036                         }
2037                         res = random_generate(random);
2038                         rd_name  = bprintf("%s_router",random_id);
2039                         rd_value = bprintf("%f",res);
2040                         xbt_dict_set(random_value, rd_name, rd_value, free);
2041
2042                         xbt_dynar_free(&radical_ends);
2043                   }
2044                   free(rd_name);
2045                   xbt_dynar_free(&radical_elements);
2046           }
2047 }
2048
2049 static void routing_parse_Erandom(void)
2050 {
2051         /*xbt_dict_cursor_t cursor = NULL;
2052         char *key;
2053         char *elem;
2054
2055         xbt_dict_foreach(random_value, cursor, key, elem) {
2056           XBT_DEBUG("%s = %s",key,elem);
2057         }
2058 */
2059 }
2060
2061 /*
2062  * New methods to init the routing model component from the lua script
2063  */
2064
2065
2066 /*
2067  * add a host to the network element list
2068  */
2069
2070 void routing_add_host(const char *host_id)
2071 {
2072   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
2073 }
2074
2075 /*
2076  * Set a new link on the actual list of link for a route or ASroute
2077  */
2078 void routing_add_link(const char *link_id)
2079 {
2080   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
2081 }
2082
2083 /*
2084  *Set the endpoints for a route
2085  */
2086 void routing_set_route(const char *src_id, const char *dst_id)
2087 {
2088   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
2089 }
2090
2091 /*
2092  * Store the route by calling parse_E_route_store_route
2093  */
2094 void routing_store_route(void)
2095 {
2096   parse_E_route_store_route();
2097 }