Logo AND Algorithmique Numérique Distribuée

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