Logo AND Algorithmique Numérique Distribuée

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