Logo AND Algorithmique Numérique Distribuée

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