Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cb103a7a2bef68e1e3e3de8c9375c23803a88728
[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(sg_platf_peer_cbarg_t peer);          /* 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(router->coord,"")) {
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(router->coord, " ");
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   sg_platf_peer_add_cb(routing_parse_Speer); // FIXME: inline in the sg_platf_new_peer instead
901   sg_platf_postparse_add_cb(clean_routing_after_parse);
902
903 #ifdef HAVE_TRACING
904   instr_routing_define_callbacks();
905 #endif
906 }
907
908 void surf_parse_add_callback_config(void)
909 {
910   surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
911 }
912
913
914 /* ************************************************** */
915 /* ********** PATERN FOR NEW ROUTING **************** */
916
917 /* The minimal configuration of a new routing model need the next functions,
918  * also you need to set at the start of the file, the new model in the model
919  * list. Remember keep the null ending of the list.
920  */
921 /*** Routing model structure ***/
922 // typedef struct {
923 //   s_routing_component_t generic_routing;
924 //   /* things that your routing model need */
925 // } s_routing_component_NEW_t,*routing_component_NEW_t;
926
927 /*** Parse routing model functions ***/
928 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
929 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
930 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
931 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
932 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
933
934 /*** Business methods ***/
935 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
936 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
937 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
938
939 /*** Creation routing model functions ***/
940 // static void* model_NEW_create(void) {
941 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
942 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
943 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
944 //   new_component->generic_routing.set_route = model_NEW_set_route;
945 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
946 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
947 //   new_component->generic_routing.get_route = NEW_get_route;
948 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
949 //   new_component->generic_routing.finalize = NEW_finalize;
950 //   /* initialization of internal structures */
951 //   return new_component;
952 // } /* mandatory */
953 // static void  model_NEW_load(void) {}   /* mandatory */
954 // static void  model_NEW_unload(void) {} /* mandatory */
955 // static void  model_NEW_end(void) {}    /* mandatory */
956
957 /* ************************************************************************** */
958 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
959
960 void generic_set_processing_unit(routing_component_t rc,
961                                         const char *name)
962 {
963   XBT_DEBUG("Load process unit \"%s\"", name);
964   int *id = xbt_new0(int, 1);
965   xbt_dict_t _to_index;
966   _to_index = current_routing->to_index;
967   *id = xbt_dict_length(_to_index);
968   xbt_dict_set(_to_index, name, id, xbt_free);
969 }
970
971 void generic_set_autonomous_system(routing_component_t rc,
972                                           const char *name)
973 {
974   XBT_DEBUG("Load Autonomous system \"%s\"", name);
975   int *id = xbt_new0(int, 1);
976   xbt_dict_t _to_index;
977   _to_index = current_routing->to_index;
978   *id = xbt_dict_length(_to_index);
979   xbt_dict_set(_to_index, name, id, xbt_free);
980 }
981
982 int surf_pointer_resource_cmp(const void *a, const void *b)
983 {
984   return a != b;
985 }
986
987 int surf_link_resource_cmp(const void *a, const void *b)
988 {
989   return !!memcmp(a,b,global_routing->size_of_link);
990 }
991
992 void generic_set_bypassroute(routing_component_t rc,
993                                     const char *src, const char *dst,
994                                     route_extended_t e_route)
995 {
996   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
997   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
998   char *route_name;
999
1000   route_name = bprintf("%s#%s", src, dst);
1001   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
1002               "Invalid count of links, must be greater than zero (%s,%s)",
1003               src, dst);
1004   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
1005               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
1006               src, e_route->src_gateway, dst, e_route->dst_gateway);
1007
1008   route_extended_t new_e_route =
1009       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
1010   xbt_dynar_free(&(e_route->generic_route.link_list));
1011   xbt_free(e_route);
1012
1013   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
1014                (void (*)(void *)) generic_free_extended_route);
1015   xbt_free(route_name);
1016 }
1017
1018 /* ************************************************************************** */
1019 /* *********************** GENERIC BUSINESS METHODS ************************* */
1020
1021 double generic_get_link_latency(routing_component_t rc,
1022                                        const char *src, const char *dst,
1023                                        route_extended_t route)
1024 {
1025         int need_to_clean = route?0:1;
1026         void * link;
1027         unsigned int i;
1028         double latency = 0.0;
1029
1030         route = route?route:rc->get_route(rc,src,dst);
1031
1032         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1033                 latency += get_link_latency(link);
1034         }
1035         if(need_to_clean) generic_free_extended_route(route);
1036   return latency;
1037 }
1038
1039 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1040 {
1041   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1042 }
1043
1044 route_extended_t generic_get_bypassroute(routing_component_t rc,
1045                                                 const char *src,
1046                                                 const char *dst)
1047 {
1048   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1049   routing_component_t src_as, dst_as;
1050   int index_src, index_dst;
1051   xbt_dynar_t path_src = NULL;
1052   xbt_dynar_t path_dst = NULL;
1053   routing_component_t current = NULL;
1054   routing_component_t *current_src = NULL;
1055   routing_component_t *current_dst = NULL;
1056
1057   /* (1) find the as where the src and dst are located */
1058   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1059   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1060   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1061   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1062
1063   if(src_data == NULL || dst_data == NULL)
1064           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1065                      src, dst, rc->name);
1066
1067   src_as = ((network_element_info_t)src_data)->rc_component;
1068   dst_as = ((network_element_info_t)dst_data)->rc_component;
1069
1070   /* (2) find the path to the root routing component */
1071   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1072   current = src_as;
1073   while (current != NULL) {
1074     xbt_dynar_push(path_src, &current);
1075     current = current->routing_father;
1076   }
1077   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1078   current = dst_as;
1079   while (current != NULL) {
1080     xbt_dynar_push(path_dst, &current);
1081     current = current->routing_father;
1082   }
1083
1084   /* (3) find the common father */
1085   index_src = path_src->used - 1;
1086   index_dst = path_dst->used - 1;
1087   current_src = xbt_dynar_get_ptr(path_src, index_src);
1088   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1089   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1090     xbt_dynar_pop_ptr(path_src);
1091     xbt_dynar_pop_ptr(path_dst);
1092     index_src--;
1093     index_dst--;
1094     current_src = xbt_dynar_get_ptr(path_src, index_src);
1095     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1096   }
1097
1098   int max_index_src = path_src->used - 1;
1099   int max_index_dst = path_dst->used - 1;
1100
1101   int max_index = max(max_index_src, max_index_dst);
1102   int i, max;
1103
1104   route_extended_t e_route_bypass = NULL;
1105
1106   for (max = 0; max <= max_index; max++) {
1107     for (i = 0; i < max; i++) {
1108       if (i <= max_index_src && max <= max_index_dst) {
1109         char *route_name = bprintf("%s#%s",
1110                                    (*(routing_component_t *)
1111                                     (xbt_dynar_get_ptr
1112                                      (path_src, i)))->name,
1113                                    (*(routing_component_t *)
1114                                     (xbt_dynar_get_ptr
1115                                      (path_dst, max)))->name);
1116         e_route_bypass =
1117             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1118         xbt_free(route_name);
1119       }
1120       if (e_route_bypass)
1121         break;
1122       if (max <= max_index_src && i <= max_index_dst) {
1123         char *route_name = bprintf("%s#%s",
1124                                    (*(routing_component_t *)
1125                                     (xbt_dynar_get_ptr
1126                                      (path_src, max)))->name,
1127                                    (*(routing_component_t *)
1128                                     (xbt_dynar_get_ptr
1129                                      (path_dst, i)))->name);
1130         e_route_bypass =
1131             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1132         xbt_free(route_name);
1133       }
1134       if (e_route_bypass)
1135         break;
1136     }
1137
1138     if (e_route_bypass)
1139       break;
1140
1141     if (max <= max_index_src && max <= max_index_dst) {
1142       char *route_name = bprintf("%s#%s",
1143                                  (*(routing_component_t *)
1144                                   (xbt_dynar_get_ptr
1145                                    (path_src, max)))->name,
1146                                  (*(routing_component_t *)
1147                                   (xbt_dynar_get_ptr
1148                                    (path_dst, max)))->name);
1149       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1150       xbt_free(route_name);
1151     }
1152     if (e_route_bypass)
1153       break;
1154   }
1155
1156   xbt_dynar_free(&path_src);
1157   xbt_dynar_free(&path_dst);
1158
1159   route_extended_t new_e_route = NULL;
1160
1161   if (e_route_bypass) {
1162     void *link;
1163     unsigned int cpt = 0;
1164     new_e_route = xbt_new0(s_route_extended_t, 1);
1165     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1166     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1167     new_e_route->generic_route.link_list =
1168         xbt_dynar_new(global_routing->size_of_link, NULL);
1169     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1170       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1171     }
1172   }
1173
1174   return new_e_route;
1175 }
1176
1177 /* ************************************************************************** */
1178 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1179
1180 route_t
1181 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1182                            void *data, int order)
1183 {
1184
1185   char *link_name;
1186   route_t new_route;
1187   unsigned int cpt;
1188   xbt_dynar_t links = NULL, links_id = NULL;
1189
1190   new_route = xbt_new0(s_route_t, 1);
1191   new_route->link_list =
1192       xbt_dynar_new(global_routing->size_of_link, NULL);
1193
1194   xbt_assert(hierarchy == SURF_ROUTING_BASE,
1195               "the hierarchy type is not SURF_ROUTING_BASE");
1196
1197   links = ((route_t) data)->link_list;
1198
1199
1200   links_id = new_route->link_list;
1201
1202   xbt_dynar_foreach(links, cpt, link_name) {
1203
1204     void *link =
1205                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1206     if (link) {
1207       if (order)
1208         xbt_dynar_push(links_id, &link);
1209       else
1210         xbt_dynar_unshift(links_id, &link);
1211     } else
1212       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1213   }
1214
1215   return new_route;
1216 }
1217
1218 route_extended_t
1219 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1220                            void *data, int order)
1221 {
1222
1223   char *link_name;
1224   route_extended_t e_route, new_e_route;
1225   route_t route;
1226   unsigned int cpt;
1227   xbt_dynar_t links = NULL, links_id = NULL;
1228
1229   new_e_route = xbt_new0(s_route_extended_t, 1);
1230   new_e_route->generic_route.link_list =
1231       xbt_dynar_new(global_routing->size_of_link, NULL);
1232   new_e_route->src_gateway = NULL;
1233   new_e_route->dst_gateway = NULL;
1234
1235   xbt_assert(hierarchy == SURF_ROUTING_BASE
1236               || hierarchy == SURF_ROUTING_RECURSIVE,
1237               "the hierarchy type is not defined");
1238
1239   if (hierarchy == SURF_ROUTING_BASE) {
1240
1241     route = (route_t) data;
1242     links = route->link_list;
1243
1244   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1245
1246     e_route = (route_extended_t) data;
1247     xbt_assert(e_route->src_gateway
1248                 && e_route->dst_gateway, "bad gateway, is null");
1249     links = e_route->generic_route.link_list;
1250
1251     /* remeber not erase the gateway names */
1252     new_e_route->src_gateway = strdup(e_route->src_gateway);
1253     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1254   }
1255
1256   links_id = new_e_route->generic_route.link_list;
1257
1258   xbt_dynar_foreach(links, cpt, link_name) {
1259
1260     void *link =
1261                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1262     if (link) {
1263       if (order)
1264         xbt_dynar_push(links_id, &link);
1265       else
1266         xbt_dynar_unshift(links_id, &link);
1267     } else
1268       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1269   }
1270
1271   return new_e_route;
1272 }
1273
1274 void generic_free_route(route_t route)
1275 {
1276   if (route) {
1277     xbt_dynar_free(&(route->link_list));
1278     xbt_free(route);
1279   }
1280 }
1281
1282 void generic_free_extended_route(route_extended_t e_route)
1283 {
1284   if (e_route) {
1285     xbt_dynar_free(&(e_route->generic_route.link_list));
1286     if (e_route->src_gateway)
1287       xbt_free(e_route->src_gateway);
1288     if (e_route->dst_gateway)
1289       xbt_free(e_route->dst_gateway);
1290     xbt_free(e_route);
1291   }
1292 }
1293
1294 static routing_component_t generic_as_exist(routing_component_t find_from,
1295                                             routing_component_t to_find)
1296 {
1297   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1298   xbt_dict_cursor_t cursor = NULL;
1299   char *key;
1300   int found = 0;
1301   routing_component_t elem;
1302   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1303     if (to_find == elem || generic_as_exist(elem, to_find)) {
1304       found = 1;
1305       break;
1306     }
1307   }
1308   if (found)
1309     return to_find;
1310   return NULL;
1311 }
1312
1313 routing_component_t
1314 generic_autonomous_system_exist(routing_component_t rc, char *element)
1315 {
1316   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1317   routing_component_t element_as, result, elem;
1318   xbt_dict_cursor_t cursor = NULL;
1319   char *key;
1320   element_as = ((network_element_info_t)
1321                 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1322   result = ((routing_component_t) - 1);
1323   if (element_as != rc)
1324     result = generic_as_exist(rc, element_as);
1325
1326   int found = 0;
1327   if (result) {
1328     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1329       found = !strcmp(elem->name, element);
1330       if (found)
1331         break;
1332     }
1333     if (found)
1334       return element_as;
1335   }
1336   return NULL;
1337 }
1338
1339 routing_component_t
1340 generic_processing_units_exist(routing_component_t rc, char *element)
1341 {
1342   routing_component_t element_as;
1343   element_as = ((network_element_info_t)
1344                 xbt_lib_get_or_null(host_lib,
1345                  element, ROUTING_HOST_LEVEL))->rc_component;
1346   if (element_as == rc)
1347     return element_as;
1348   return generic_as_exist(rc, element_as);
1349 }
1350
1351 void generic_src_dst_check(routing_component_t rc, const char *src,
1352                                   const char *dst)
1353 {
1354
1355   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1356   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1357   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1358   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1359
1360   if(src_data == NULL || dst_data == NULL)
1361           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1362                      src, dst, rc->name);
1363
1364   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1365   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1366
1367   if(src_as != dst_as)
1368           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1369               src, src_as->name, dst, dst_as->name);
1370   if(rc != dst_as)
1371          xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1372      src,dst,src_as->name, dst_as->name,rc->name);
1373 }
1374
1375 void routing_parse_Scluster(void)
1376 {
1377   if(!cluster_host_link)
1378           cluster_host_link = xbt_dict_new();
1379
1380   static int AX_ptr = 0;
1381   char *host_id, *groups, *link_id = NULL;
1382
1383   s_sg_platf_host_cbarg_t host;
1384   s_sg_platf_link_cbarg_t link;
1385
1386   if( strcmp(struct_cluster->availability_trace,"")
1387           || strcmp(struct_cluster->state_trace,"") )
1388   {
1389           if(xbt_dict_size(patterns)==0)
1390                   patterns = xbt_dict_new();
1391           xbt_dict_set(patterns,"id",xbt_strdup(struct_cluster->id),free);
1392           xbt_dict_set(patterns,"prefix",xbt_strdup(struct_cluster->prefix),free);
1393           xbt_dict_set(patterns,"suffix",xbt_strdup(struct_cluster->suffix),free);
1394   }
1395
1396   unsigned int iter;
1397   int start, end, i;
1398   xbt_dynar_t radical_elements;
1399   xbt_dynar_t radical_ends;
1400
1401   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1402   static unsigned int surfxml_buffer_stack_stack[1024];
1403
1404   surfxml_buffer_stack_stack[0] = 0;
1405   surfxml_bufferstack_push(1);
1406
1407   SURFXML_BUFFER_SET(AS_id, struct_cluster->id);
1408   SURFXML_BUFFER_SET(AS_routing, "Cluster");
1409   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->id);
1410   SURFXML_START_TAG(AS);
1411
1412   //Make all hosts
1413   radical_elements = xbt_str_split(struct_cluster->radical, ",");
1414   xbt_dynar_foreach(radical_elements, iter, groups) {
1415     memset(&host,0,sizeof(host));
1416
1417     radical_ends = xbt_str_split(groups, "-");
1418     switch (xbt_dynar_length(radical_ends)) {
1419     case 1:
1420                 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1421                 host_id = bprintf("%s%d%s", struct_cluster->prefix, start, struct_cluster->suffix);
1422                 link_id = bprintf("%s_link_%d", struct_cluster->id, start);
1423
1424                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1425                 host.id = host_id;
1426                 if(strcmp(struct_cluster->availability_trace,"")){
1427                   xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1428                   char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1429                   xbt_str_varsubst(tmp_availability_file,patterns);
1430                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1431                   host.power_trace = tmgr_trace_new(tmp_availability_file);
1432                   xbt_free(tmp_availability_file);
1433                 }
1434                 else
1435                 {
1436                   XBT_DEBUG("\tavailability_file=\"\"");
1437                 }
1438                 if(strcmp(struct_cluster->state_trace,"")){
1439                   char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1440                   xbt_str_varsubst(tmp_state_file,patterns);
1441                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1442                   host.state_trace = tmgr_trace_new(tmp_state_file);
1443                   xbt_free(tmp_state_file);
1444                 }
1445                 else
1446                 {
1447                   XBT_DEBUG("\tstate_file=\"\"");
1448                 }
1449
1450                 host.power_peak = struct_cluster->power;
1451                 host.power_scale = 1.0;
1452                 host.core_amount = struct_cluster->core_amount;
1453                 host.initial_state = SURF_RESOURCE_ON;
1454                 host.coord = "";
1455                 sg_platf_new_host(&host);
1456                 XBT_DEBUG("</host>");
1457
1458
1459                 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1460                 if(struct_cluster->sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1461                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1462                 if(struct_cluster->sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1463                 {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1464
1465                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1466
1467                 memset(&link,0,sizeof(link));
1468                 link.id = link_id;
1469                 link.bandwidth = struct_cluster->bw;
1470                 link.latency = struct_cluster->lat;
1471                 link.state = SURF_RESOURCE_ON;
1472
1473                 switch (A_surfxml_link_sharing_policy) {
1474                 case A_surfxml_link_sharing_policy_SHARED:
1475                         link.policy = SURF_LINK_SHARED;
1476                         break;
1477                 case A_surfxml_link_sharing_policy_FATPIPE:
1478                   link.policy = SURF_LINK_FATPIPE;
1479                   break;
1480                 case A_surfxml_link_sharing_policy_FULLDUPLEX:
1481                   link.policy = SURF_LINK_FULLDUPLEX;
1482                   break;
1483                 case AU_surfxml_link_sharing_policy:
1484                   surf_parse_error(bprintf("Invalid sharing policy in cluster %s (please report this bug, this shouldn't happen)",struct_cluster->id));
1485                 }
1486
1487                 sg_platf_new_link(&link);
1488
1489                 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1490                 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
1491                         char* tmp_link =  bprintf("%s_UP",link_id);
1492                         info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1493                         free(tmp_link);
1494                         tmp_link =  bprintf("%s_DOWN",link_id);
1495                         info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1496                         free(tmp_link);
1497                 }
1498                 else{
1499                         info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1500                         info->link_down = info->link_up;
1501                 }
1502                 xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1503                 xbt_free(link_id);
1504                 xbt_free(host_id);
1505
1506                 break;
1507
1508     case 2:
1509
1510       start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1511       end=  surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1512       for (i = start; i <= end; i++) {
1513                 host_id = bprintf("%s%d%s", struct_cluster->prefix, i, struct_cluster->suffix);
1514                 link_id = bprintf("%s_link_%d", struct_cluster->id, i);
1515
1516                 A_surfxml_host_state = A_surfxml_host_state_ON;
1517
1518                 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1519                 host.id = host_id;
1520                 if(strcmp(struct_cluster->availability_trace,"")){
1521                   xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1522                   char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1523                   xbt_str_varsubst(tmp_availability_file,patterns);
1524                   XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1525                   host.power_trace = tmgr_trace_new(tmp_availability_file);
1526                   xbt_free(tmp_availability_file);
1527                 }
1528                 else
1529                 {
1530                   XBT_DEBUG("\tavailability_file=\"\"");
1531                 }
1532                 if(strcmp(struct_cluster->state_trace,"")){
1533                   char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1534                   xbt_str_varsubst(tmp_state_file,patterns);
1535                   XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1536                   host.state_trace = tmgr_trace_new(tmp_state_file);
1537                   xbt_free(tmp_state_file);
1538                 }
1539                 else
1540                 {
1541                   XBT_DEBUG("\tstate_file=\"\"");
1542                 }
1543
1544                 host.power_peak = struct_cluster->power;
1545                 host.power_scale = 1.0;
1546                 host.core_amount = struct_cluster->core_amount;
1547                 host.initial_state = SURF_RESOURCE_ON;
1548                 host.coord = "";
1549                 sg_platf_new_host(&host);
1550                 XBT_DEBUG("</host>");
1551
1552                 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1553
1554                 memset(&link,0,sizeof(link));
1555                 link.id = link_id;
1556                 link.bandwidth = struct_cluster->bw;
1557                 link.latency = struct_cluster->lat;
1558                 link.state = SURF_RESOURCE_ON;
1559
1560                 switch (struct_cluster->sharing_policy) {
1561                 case A_surfxml_cluster_sharing_policy_SHARED:
1562       link.policy = SURF_LINK_SHARED;
1563       break;
1564                 case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1565                   link.policy = SURF_LINK_FULLDUPLEX;
1566                   break;
1567                 case A_surfxml_cluster_sharing_policy_FATPIPE:
1568                   link.policy = SURF_LINK_FATPIPE;
1569                   break;
1570                 default:
1571                   surf_parse_error(bprintf("Invalid cluster sharing policy for cluster %s",struct_cluster->id));
1572                 }
1573                 sg_platf_new_link(&link);
1574
1575                 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1576                 if (link.policy == SURF_LINK_FULLDUPLEX) {
1577                         char* tmp_link =  bprintf("%s_UP",link_id);
1578                         info->link_up   = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1579                         free(tmp_link);
1580                         tmp_link =  bprintf("%s_DOWN",link_id);
1581                         info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1582                         free(tmp_link);
1583                 }
1584                 else{
1585                         info->link_up   = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1586                         info->link_down = info->link_up;
1587                 }
1588                 xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1589                 xbt_free(link_id);
1590                 xbt_free(host_id);
1591
1592       }
1593       break;
1594
1595     default:
1596       XBT_DEBUG("Malformed radical");
1597       break;
1598     }
1599
1600     xbt_dynar_free(&radical_ends);
1601   }
1602   xbt_dynar_free(&radical_elements);
1603
1604   // 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
1605   XBT_DEBUG(" ");
1606   XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->router_id);
1607   s_sg_platf_router_cbarg_t router;
1608   char *newid=NULL;
1609   memset(&router,0,sizeof(router));
1610   router.id = struct_cluster->router_id;
1611   router.coord = "";
1612   if (!router.id || !strcmp(router.id,""))
1613     router.id = newid = bprintf("%s%s_router%s", struct_cluster->prefix, struct_cluster->id, struct_cluster->suffix);
1614   sg_platf_new_router(&router);
1615   if (newid)
1616     free(newid);
1617
1618   //Make the backbone
1619   if( (struct_cluster->bb_bw!= 0)  && (struct_cluster->bb_lat!=0)  ){
1620           char *link_backbone = bprintf("%s_backbone", struct_cluster->id);
1621           XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->bb_bw, struct_cluster->bb_lat);
1622
1623           memset(&link,0,sizeof(link));
1624           link.id = link_backbone;
1625           link.bandwidth = struct_cluster->bb_bw;
1626           link.latency = struct_cluster->bb_lat;
1627           link.state = SURF_RESOURCE_ON;
1628
1629           switch (AX_surfxml_cluster_bb_sharing_policy) {
1630           case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
1631             link.policy = SURF_LINK_FATPIPE;
1632             break;
1633           case A_surfxml_cluster_bb_sharing_policy_SHARED:
1634              link.policy = SURF_LINK_SHARED;
1635              break;
1636           default:
1637             surf_parse_error(bprintf("Invalid bb sharing policy in cluster %s",struct_cluster->id));
1638           }
1639
1640           sg_platf_new_link(&link);
1641
1642           surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1643           info->link_up   = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1644           info->link_down = info->link_up;
1645           xbt_dict_set(cluster_host_link,struct_cluster->id,info,xbt_free);
1646           free(link_backbone);
1647   }
1648
1649   XBT_DEBUG(" ");
1650
1651   char *new_suffix = xbt_strdup("");
1652
1653   radical_elements = xbt_str_split(struct_cluster->suffix, ".");
1654   xbt_dynar_foreach(radical_elements, iter, groups) {
1655     if (strcmp(groups, "")) {
1656       char *old_suffix = new_suffix;
1657       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1658       free(old_suffix);
1659     }
1660   }
1661
1662   xbt_dynar_free(&radical_elements);
1663   xbt_free(new_suffix);
1664
1665   if( strcmp(struct_cluster->availability_trace,"")
1666                   || strcmp(struct_cluster->state_trace,"") )
1667           xbt_dict_free(&patterns);
1668
1669   XBT_DEBUG("</AS>");
1670   SURFXML_END_TAG(AS);
1671   XBT_DEBUG(" ");
1672
1673   surfxml_bufferstack_pop(1);
1674 }
1675 /*
1676  * This function take a string and replace parameters from patterns dict.
1677  * It returns the new value.
1678  */
1679 static char* replace_random_parameter(char * string)
1680 {
1681   char *test_string = NULL;
1682
1683   if(xbt_dict_size(random_value)==0)
1684     return string;
1685
1686   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1687   test_string = bprintf("${%s}", string);
1688   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1689
1690   if (strcmp(test_string,"")) { //if not empty, keep this value.
1691     xbt_free(string);
1692     string = test_string;
1693   } //In other case take old value (without ${})
1694   else
1695         free(test_string);
1696   return string;
1697 }
1698
1699 static void clean_routing_after_parse(void)
1700 {
1701         xbt_dict_free(&random_value);
1702         xbt_dict_free(&patterns);
1703 }
1704
1705 static void routing_parse_Speer(sg_platf_peer_cbarg_t peer)
1706 {
1707   static int AX_ptr = 0;
1708   char *host_id = NULL;
1709   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1710
1711   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1712   static unsigned int surfxml_buffer_stack_stack[1024];
1713
1714   surfxml_buffer_stack_stack[0] = 0;
1715
1716   surfxml_bufferstack_push(1);
1717
1718   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer->id);
1719   sg_platf_new_AS_open(peer->id, "Full");
1720
1721   XBT_DEBUG(" ");
1722   host_id = HOST_PEER(peer->id);
1723   router_id = ROUTER_PEER(peer->id);
1724   link_id_up = LINK_UP_PEER(peer->id);
1725   link_id_down = LINK_DOWN_PEER(peer->id);
1726
1727   link_router = bprintf("%s_link_router", peer->id);
1728   link_backbone = bprintf("%s_backbone", peer->id);
1729
1730   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->power);
1731   s_sg_platf_host_cbarg_t host;
1732   memset(&host,0,sizeof(host));
1733   host.initial_state = SURF_RESOURCE_ON;
1734   host.id = host_id;
1735   host.power_peak = peer->power;
1736   host.power_scale = 1.0;
1737   host.power_trace = peer->availability_trace;
1738   host.state_trace = peer->state_trace;
1739   host.core_amount = 1;
1740   sg_platf_new_host(&host);
1741
1742
1743   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer->coord);
1744   s_sg_platf_router_cbarg_t router;
1745   memset(&router,0,sizeof(router));
1746   router.id = router_id;
1747   router.coord = peer->coord;
1748   sg_platf_new_router(&router);
1749
1750   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_up, peer->bw_in, peer->lat);
1751   s_sg_platf_link_cbarg_t link;
1752   memset(&link,0,sizeof(link));
1753   link.state = SURF_RESOURCE_ON;
1754   link.policy = SURF_LINK_SHARED;
1755   link.id = link_id_up;
1756   link.bandwidth = peer->bw_in;
1757   link.latency = peer->lat;
1758   sg_platf_new_link(&link);
1759
1760   // FIXME: dealing with full duplex is not the role of this piece of code, I'd say [Mt]
1761   // Instead, it should be created fullduplex, and the models will do what's needed in this case
1762   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_down, peer->bw_out, peer->lat);
1763   link.id = link_id_down;
1764   sg_platf_new_link(&link);
1765
1766   XBT_DEBUG(" ");
1767
1768   // begin here
1769   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1770   XBT_DEBUG("symmetrical=\"NO\">");
1771   SURFXML_BUFFER_SET(route_src, host_id);
1772   SURFXML_BUFFER_SET(route_dst, router_id);
1773   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1774   SURFXML_START_TAG(route);
1775
1776   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1777   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1778   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1779   SURFXML_START_TAG(link_ctn);
1780   SURFXML_END_TAG(link_ctn);
1781
1782   XBT_DEBUG("</route>");
1783   SURFXML_END_TAG(route);
1784
1785   //Opposite Route
1786   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1787   XBT_DEBUG("symmetrical=\"NO\">");
1788   SURFXML_BUFFER_SET(route_src, router_id);
1789   SURFXML_BUFFER_SET(route_dst, host_id);
1790   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1791   SURFXML_START_TAG(route);
1792
1793   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1794   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1795   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1796   SURFXML_START_TAG(link_ctn);
1797   SURFXML_END_TAG(link_ctn);
1798
1799   XBT_DEBUG("</route>");
1800   SURFXML_END_TAG(route);
1801
1802   XBT_DEBUG("</AS>");
1803   sg_platf_new_AS_close();
1804   XBT_DEBUG(" ");
1805
1806   //xbt_dynar_free(&tab_elements_num);
1807         free(host_id);
1808         free(router_id);
1809         free(link_router);
1810         free(link_backbone);
1811         free(link_id_up);
1812         free(link_id_down);
1813   surfxml_bufferstack_pop(1);
1814 }
1815
1816 static void routing_parse_Srandom(void)
1817 {
1818     double mean, std, min, max, seed;
1819     char *random_id = A_surfxml_random_id;
1820     char *random_radical = A_surfxml_random_radical;
1821     char *rd_name = NULL;
1822     char *rd_value;
1823     mean = surf_parse_get_double(A_surfxml_random_mean);
1824     std  = surf_parse_get_double(A_surfxml_random_std_deviation);
1825     min  = surf_parse_get_double(A_surfxml_random_min);
1826     max  = surf_parse_get_double(A_surfxml_random_max);
1827     seed = surf_parse_get_double(A_surfxml_random_seed);
1828
1829     double res = 0;
1830     int i = 0;
1831     random_data_t random = xbt_new0(s_random_data_t, 1);
1832           char *tmpbuf;
1833
1834     xbt_dynar_t radical_elements;
1835     unsigned int iter;
1836     char *groups;
1837     int start, end;
1838     xbt_dynar_t radical_ends;
1839
1840     random->generator = A_surfxml_random_generator;
1841     random->seed = seed;
1842     random->min = min;
1843     random->max = max;
1844
1845     /* Check user stupidities */
1846     if (max < min)
1847       THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1848     if (mean < min)
1849       THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1850        min);
1851     if (mean > max)
1852       THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1853        max);
1854
1855     /* normalize the mean and standard deviation before storing */
1856     random->mean = (mean - min) / (max - min);
1857     random->std = std / (max - min);
1858
1859     if (random->mean * (1 - random->mean) < random->std * random->std)
1860       THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1861        random->mean, random->std);
1862
1863     XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1864     random_id,
1865     random->min,
1866     random->max,
1867     random->mean,
1868     random->std,
1869     random->generator,
1870     random->seed,
1871     random_radical);
1872
1873     if(xbt_dict_size(random_value)==0)
1874       random_value = xbt_dict_new();
1875
1876     if(!strcmp(random_radical,""))
1877     {
1878       res = random_generate(random);
1879       rd_value = bprintf("%f",res);
1880       xbt_dict_set(random_value, random_id, rd_value, free);
1881     }
1882     else
1883     {
1884       radical_elements = xbt_str_split(random_radical, ",");
1885       xbt_dynar_foreach(radical_elements, iter, groups) {
1886       radical_ends = xbt_str_split(groups, "-");
1887       switch (xbt_dynar_length(radical_ends)) {
1888       case 1:
1889             xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
1890             res = random_generate(random);
1891                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
1892                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1893                                           xbt_free(tmpbuf);
1894             break;
1895
1896       case 2:
1897             start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1898             end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1899             for (i = start; i <= end; i++) {
1900               xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
1901               res = random_generate(random);
1902                           tmpbuf = bprintf("%s%d",random_id,i);
1903               xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1904                           xbt_free(tmpbuf);
1905             }
1906             break;
1907       default:
1908         XBT_INFO("Malformed radical");
1909         break;
1910       }
1911       res = random_generate(random);
1912       rd_name  = bprintf("%s_router",random_id);
1913       rd_value = bprintf("%f",res);
1914       xbt_dict_set(random_value, rd_name, rd_value, free);
1915
1916       xbt_dynar_free(&radical_ends);
1917       }
1918       free(rd_name);
1919       xbt_dynar_free(&radical_elements);
1920     }
1921 }
1922