Logo AND Algorithmique Numérique Distribuée

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