Logo AND Algorithmique Numérique Distribuée

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