Logo AND Algorithmique Numérique Distribuée

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