Logo AND Algorithmique Numérique Distribuée

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