Logo AND Algorithmique Numérique Distribuée

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