Logo AND Algorithmique Numérique Distribuée

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