Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
375a7c2a82578af7f8f0ffc934b55f986b3e5e68
[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     xbt_ex_free(exception);
822     return NULL;
823   }
824   return route;
825 }
826
827 /**
828  * \brief Generic method: find a route between hosts
829  *
830  * \param src the source host name
831  * \param dst the destination host name
832  *
833  * walk through the routing components tree and find a route between hosts
834  * by calling the differents "get_route" functions in each routing component.
835  * Leaves the caller the responsability to clean the returned dynar.
836  */
837 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
838 {
839   xbt_dynar_t route = NULL;
840   get_route_latency(src, dst, &route, NULL, 0);
841   return route;
842 }
843
844 /*Get Latency*/
845 static double get_latency(const char *src, const char *dst)
846 {
847   double latency = -1.0;
848   get_route_latency(src, dst, NULL, &latency, 0);
849   return latency;
850 }
851
852 /**
853  * \brief Recursive function for finalize
854  *
855  * \param rc the source host name 
856  * 
857  * This fuction is call by "finalize". It allow to finalize the 
858  * AS or routing components. It delete all the structures.
859  */
860 static void _finalize(routing_component_t rc)
861 {
862   if (rc) {
863     xbt_dict_cursor_t cursor = NULL;
864     char *key;
865     routing_component_t elem;
866     xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
867       _finalize(elem);
868     }
869     xbt_dict_t tmp_sons = rc->routing_sons;
870     char *tmp_name = rc->name;
871     xbt_dict_free(&tmp_sons);
872     xbt_free(tmp_name);
873     xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
874                 current_routing->name);
875     (*(rc->finalize)) (rc);
876   }
877 }
878
879 /**
880  * \brief Generic method: delete all the routing structures
881  * 
882  * walk through the routing components tree and delete the structures
883  * by calling the differents "finalize" functions in each routing component
884  */
885 static void finalize(void)
886 {
887   /* delete recursibly all the tree */
888   _finalize(global_routing->root);
889   /* delete last_route */
890   xbt_dynar_free(&(global_routing->last_route));
891   /* delete global routing structure */
892   xbt_free(global_routing);
893 }
894
895 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
896 {
897   xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
898
899   //adding my one link routes
900   unsigned int cpt;
901   void *link;
902   xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
903   if (onelink_mine) {
904     xbt_dynar_foreach(onelink_mine, cpt, link) {
905       xbt_dynar_push(ret, &link);
906     }
907   }
908   //recursing
909   char *key;
910   xbt_dict_cursor_t cursor = NULL;
911   routing_component_t rc_child;
912   xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
913     xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
914     if (onelink_child) {
915       xbt_dynar_foreach(onelink_child, cpt, link) {
916         xbt_dynar_push(ret, &link);
917       }
918     }
919   }
920   return ret;
921 }
922
923 static xbt_dynar_t get_onelink_routes(void)
924 {
925   return recursive_get_onelink_routes(global_routing->root);
926 }
927
928 e_surf_network_element_type_t get_network_element_type(const char
929                                                               *name)
930 {
931   network_element_info_t rc = NULL;
932
933   rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
934   if(rc) return rc->rc_type;
935
936   rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
937   if(rc) return rc->rc_type;
938
939   return SURF_NETWORK_ELEMENT_NULL;
940 }
941
942 /**
943  * \brief Generic method: create the global routing schema
944  * 
945  * Make a global routing structure and set all the parsing functions.
946  */
947 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
948 {
949   /* config the uniq global routing */
950   global_routing = xbt_new0(s_routing_global_t, 1);
951   global_routing->root = NULL;
952   global_routing->get_route = get_route;
953   global_routing->get_route_or_null = get_route_or_null;
954   global_routing->get_latency = get_latency;
955   global_routing->get_route_no_cleanup = get_route_no_cleanup;
956   global_routing->get_onelink_routes = get_onelink_routes;
957   global_routing->get_route_latency = get_route_latency;
958   global_routing->get_network_element_type = get_network_element_type;
959   global_routing->finalize = finalize;
960   global_routing->loopback = loopback;
961   global_routing->size_of_link = size_of_links;
962   global_routing->last_route = NULL;
963   get_link_latency = get_link_latency_fun;
964   /* no current routing at moment */
965   current_routing = NULL;
966
967   /* parse generic elements */
968   surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
969   surfxml_add_callback(ETag_surfxml_host_cb_list, &parse_E_host_XML);
970   surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router_XML);
971
972   surfxml_add_callback(STag_surfxml_route_cb_list,
973                        &parse_S_route_new_and_endpoints_XML);
974   surfxml_add_callback(STag_surfxml_ASroute_cb_list,
975                        &parse_S_ASroute_new_and_endpoints);
976   surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
977                        &parse_S_bypassRoute_new_and_endpoints);
978
979   surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
980                        &parse_E_link_ctn_new_elem_XML);
981
982   surfxml_add_callback(ETag_surfxml_route_cb_list,
983                        &parse_E_route_store_route);
984   surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
985                        &parse_E_ASroute_store_route);
986   surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
987                        &parse_E_bypassRoute_store_route);
988
989   surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
990   surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
991
992   surfxml_add_callback(STag_surfxml_cluster_cb_list,
993                        &routing_parse_Scluster);
994
995   surfxml_add_callback(STag_surfxml_peer_cb_list,
996                          &routing_parse_Speer);
997
998   surfxml_add_callback(ETag_surfxml_platform_cb_list,
999                                                   &clean_dict_random);
1000
1001 #ifdef HAVE_TRACING
1002   instr_routing_define_callbacks();
1003 #endif
1004 }
1005
1006 void surf_parse_add_callback_config(void)
1007 {
1008         surfxml_add_callback(STag_surfxml_config_cb_list, &routing_parse_Sconfig);
1009         surfxml_add_callback(ETag_surfxml_config_cb_list, &routing_parse_Econfig);
1010         surfxml_add_callback(STag_surfxml_prop_cb_list, &parse_properties_XML);
1011         surfxml_add_callback(STag_surfxml_AS_cb_list, &surf_parse_models_setup);
1012         surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
1013 }
1014
1015 void surf_parse_models_setup()
1016 {
1017         routing_parse_Erandom();
1018         surfxml_del_callback(STag_surfxml_AS_cb_list, surf_parse_models_setup);
1019         surf_config_models_setup(platform_filename);
1020         free(platform_filename);
1021 }
1022
1023 /* ************************************************** */
1024 /* ********** PATERN FOR NEW ROUTING **************** */
1025
1026 /* The minimal configuration of a new routing model need the next functions,
1027  * also you need to set at the start of the file, the new model in the model
1028  * list. Remember keep the null ending of the list.
1029  */
1030 /*** Routing model structure ***/
1031 // typedef struct {
1032 //   s_routing_component_t generic_routing;
1033 //   /* things that your routing model need */
1034 // } s_routing_component_NEW_t,*routing_component_NEW_t;
1035
1036 /*** Parse routing model functions ***/
1037 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
1038 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
1039 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
1040 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
1041 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
1042
1043 /*** Business methods ***/
1044 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1045 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
1046 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
1047
1048 /*** Creation routing model functions ***/
1049 // static void* model_NEW_create(void) {
1050 //   routing_component_NEW_t new_component =  xbt_new0(s_routing_component_NEW_t,1);
1051 //   new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
1052 //   new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
1053 //   new_component->generic_routing.set_route = model_NEW_set_route;
1054 //   new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
1055 //   new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
1056 //   new_component->generic_routing.get_route = NEW_get_route;
1057 //   new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
1058 //   new_component->generic_routing.finalize = NEW_finalize;
1059 //   /* initialization of internal structures */
1060 //   return new_component;
1061 // } /* mandatory */
1062 // static void  model_NEW_load(void) {}   /* mandatory */
1063 // static void  model_NEW_unload(void) {} /* mandatory */
1064 // static void  model_NEW_end(void) {}    /* mandatory */
1065
1066 /* ************************************************************************** */
1067 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
1068
1069 void generic_set_processing_unit(routing_component_t rc,
1070                                         const char *name)
1071 {
1072   XBT_DEBUG("Load process unit \"%s\"", name);
1073   int *id = xbt_new0(int, 1);
1074   xbt_dict_t _to_index;
1075   _to_index = current_routing->to_index;
1076   *id = xbt_dict_length(_to_index);
1077   xbt_dict_set(_to_index, name, id, xbt_free);
1078 }
1079
1080 void generic_set_autonomous_system(routing_component_t rc,
1081                                           const char *name)
1082 {
1083   XBT_DEBUG("Load Autonomous system \"%s\"", name);
1084   int *id = xbt_new0(int, 1);
1085   xbt_dict_t _to_index;
1086   _to_index = current_routing->to_index;
1087   *id = xbt_dict_length(_to_index);
1088   xbt_dict_set(_to_index, name, id, xbt_free);
1089 }
1090
1091 int surf_pointer_resource_cmp(const void *a, const void *b)
1092 {
1093   return a != b;
1094 }
1095
1096 int surf_link_resource_cmp(const void *a, const void *b)
1097 {
1098   return !!memcmp(a,b,global_routing->size_of_link);
1099 }
1100
1101 void generic_set_bypassroute(routing_component_t rc,
1102                                     const char *src, const char *dst,
1103                                     route_extended_t e_route)
1104 {
1105   XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
1106   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1107   char *route_name;
1108
1109   route_name = bprintf("%s#%s", src, dst);
1110   xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
1111               "Invalid count of links, must be greater than zero (%s,%s)",
1112               src, dst);
1113   xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
1114               "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
1115               src, e_route->src_gateway, dst, e_route->dst_gateway);
1116
1117   route_extended_t new_e_route =
1118       generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
1119   xbt_dynar_free(&(e_route->generic_route.link_list));
1120   xbt_free(e_route);
1121
1122   xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
1123                (void (*)(void *)) generic_free_extended_route);
1124   xbt_free(route_name);
1125 }
1126
1127 /* ************************************************************************** */
1128 /* *********************** GENERIC BUSINESS METHODS ************************* */
1129
1130 double generic_get_link_latency(routing_component_t rc,
1131                                        const char *src, const char *dst,
1132                                        route_extended_t route)
1133 {
1134         int need_to_clean = route?0:1;
1135         void * link;
1136         unsigned int i;
1137         double latency = 0.0;
1138
1139         route = route?route:rc->get_route(rc,src,dst);
1140
1141         xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1142                 latency += get_link_latency(link);
1143         }
1144         if(need_to_clean) generic_free_extended_route(route);
1145   return latency;
1146 }
1147
1148 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1149 {
1150   xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1151 }
1152
1153 route_extended_t generic_get_bypassroute(routing_component_t rc,
1154                                                 const char *src,
1155                                                 const char *dst)
1156 {
1157   xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1158   routing_component_t src_as, dst_as;
1159   int index_src, index_dst;
1160   xbt_dynar_t path_src = NULL;
1161   xbt_dynar_t path_dst = NULL;
1162   routing_component_t current = NULL;
1163   routing_component_t *current_src = NULL;
1164   routing_component_t *current_dst = NULL;
1165
1166   /* (1) find the as where the src and dst are located */
1167   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1168   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1169   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1170   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1171
1172   if(src_data == NULL || dst_data == NULL)
1173           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1174                      src, dst, rc->name);
1175
1176   src_as = ((network_element_info_t)src_data)->rc_component;
1177   dst_as = ((network_element_info_t)dst_data)->rc_component;
1178
1179   /* (2) find the path to the root routing component */
1180   path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1181   current = src_as;
1182   while (current != NULL) {
1183     xbt_dynar_push(path_src, &current);
1184     current = current->routing_father;
1185   }
1186   path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1187   current = dst_as;
1188   while (current != NULL) {
1189     xbt_dynar_push(path_dst, &current);
1190     current = current->routing_father;
1191   }
1192
1193   /* (3) find the common father */
1194   index_src = path_src->used - 1;
1195   index_dst = path_dst->used - 1;
1196   current_src = xbt_dynar_get_ptr(path_src, index_src);
1197   current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1198   while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1199     xbt_dynar_pop_ptr(path_src);
1200     xbt_dynar_pop_ptr(path_dst);
1201     index_src--;
1202     index_dst--;
1203     current_src = xbt_dynar_get_ptr(path_src, index_src);
1204     current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1205   }
1206
1207   int max_index_src = path_src->used - 1;
1208   int max_index_dst = path_dst->used - 1;
1209
1210   int max_index = max(max_index_src, max_index_dst);
1211   int i, max;
1212
1213   route_extended_t e_route_bypass = NULL;
1214
1215   for (max = 0; max <= max_index; max++) {
1216     for (i = 0; i < max; i++) {
1217       if (i <= max_index_src && max <= max_index_dst) {
1218         char *route_name = bprintf("%s#%s",
1219                                    (*(routing_component_t *)
1220                                     (xbt_dynar_get_ptr
1221                                      (path_src, i)))->name,
1222                                    (*(routing_component_t *)
1223                                     (xbt_dynar_get_ptr
1224                                      (path_dst, max)))->name);
1225         e_route_bypass =
1226             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1227         xbt_free(route_name);
1228       }
1229       if (e_route_bypass)
1230         break;
1231       if (max <= max_index_src && i <= max_index_dst) {
1232         char *route_name = bprintf("%s#%s",
1233                                    (*(routing_component_t *)
1234                                     (xbt_dynar_get_ptr
1235                                      (path_src, max)))->name,
1236                                    (*(routing_component_t *)
1237                                     (xbt_dynar_get_ptr
1238                                      (path_dst, i)))->name);
1239         e_route_bypass =
1240             xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1241         xbt_free(route_name);
1242       }
1243       if (e_route_bypass)
1244         break;
1245     }
1246
1247     if (e_route_bypass)
1248       break;
1249
1250     if (max <= max_index_src && max <= max_index_dst) {
1251       char *route_name = bprintf("%s#%s",
1252                                  (*(routing_component_t *)
1253                                   (xbt_dynar_get_ptr
1254                                    (path_src, max)))->name,
1255                                  (*(routing_component_t *)
1256                                   (xbt_dynar_get_ptr
1257                                    (path_dst, max)))->name);
1258       e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1259       xbt_free(route_name);
1260     }
1261     if (e_route_bypass)
1262       break;
1263   }
1264
1265   xbt_dynar_free(&path_src);
1266   xbt_dynar_free(&path_dst);
1267
1268   route_extended_t new_e_route = NULL;
1269
1270   if (e_route_bypass) {
1271     void *link;
1272     unsigned int cpt = 0;
1273     new_e_route = xbt_new0(s_route_extended_t, 1);
1274     new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1275     new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1276     new_e_route->generic_route.link_list =
1277         xbt_dynar_new(global_routing->size_of_link, NULL);
1278     xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1279       xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1280     }
1281   }
1282
1283   return new_e_route;
1284 }
1285
1286 /* ************************************************************************** */
1287 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1288
1289 route_t
1290 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1291                            void *data, int order)
1292 {
1293
1294   char *link_name;
1295   route_t new_route;
1296   unsigned int cpt;
1297   xbt_dynar_t links = NULL, links_id = NULL;
1298
1299   new_route = xbt_new0(s_route_t, 1);
1300   new_route->link_list =
1301       xbt_dynar_new(global_routing->size_of_link, NULL);
1302
1303   xbt_assert(hierarchy == SURF_ROUTING_BASE,
1304               "the hierarchy type is not SURF_ROUTING_BASE");
1305
1306   links = ((route_t) data)->link_list;
1307
1308
1309   links_id = new_route->link_list;
1310
1311   xbt_dynar_foreach(links, cpt, link_name) {
1312
1313     void *link =
1314                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1315     if (link) {
1316       if (order)
1317         xbt_dynar_push(links_id, &link);
1318       else
1319         xbt_dynar_unshift(links_id, &link);
1320     } else
1321       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1322   }
1323
1324   return new_route;
1325 }
1326
1327 route_extended_t
1328 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1329                            void *data, int order)
1330 {
1331
1332   char *link_name;
1333   route_extended_t e_route, new_e_route;
1334   route_t route;
1335   unsigned int cpt;
1336   xbt_dynar_t links = NULL, links_id = NULL;
1337
1338   new_e_route = xbt_new0(s_route_extended_t, 1);
1339   new_e_route->generic_route.link_list =
1340       xbt_dynar_new(global_routing->size_of_link, NULL);
1341   new_e_route->src_gateway = NULL;
1342   new_e_route->dst_gateway = NULL;
1343
1344   xbt_assert(hierarchy == SURF_ROUTING_BASE
1345               || hierarchy == SURF_ROUTING_RECURSIVE,
1346               "the hierarchy type is not defined");
1347
1348   if (hierarchy == SURF_ROUTING_BASE) {
1349
1350     route = (route_t) data;
1351     links = route->link_list;
1352
1353   } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1354
1355     e_route = (route_extended_t) data;
1356     xbt_assert(e_route->src_gateway
1357                 && e_route->dst_gateway, "bad gateway, is null");
1358     links = e_route->generic_route.link_list;
1359
1360     /* remeber not erase the gateway names */
1361     new_e_route->src_gateway = strdup(e_route->src_gateway);
1362     new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1363   }
1364
1365   links_id = new_e_route->generic_route.link_list;
1366
1367   xbt_dynar_foreach(links, cpt, link_name) {
1368
1369     void *link =
1370                 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1371     if (link) {
1372       if (order)
1373         xbt_dynar_push(links_id, &link);
1374       else
1375         xbt_dynar_unshift(links_id, &link);
1376     } else
1377       THROWF(mismatch_error, 0, "Link %s not found", link_name);
1378   }
1379
1380   return new_e_route;
1381 }
1382
1383 void generic_free_route(route_t route)
1384 {
1385   if (route) {
1386     xbt_dynar_free(&(route->link_list));
1387     xbt_free(route);
1388   }
1389 }
1390
1391 void generic_free_extended_route(route_extended_t e_route)
1392 {
1393   if (e_route) {
1394     xbt_dynar_free(&(e_route->generic_route.link_list));
1395     if (e_route->src_gateway)
1396       xbt_free(e_route->src_gateway);
1397     if (e_route->dst_gateway)
1398       xbt_free(e_route->dst_gateway);
1399     xbt_free(e_route);
1400   }
1401 }
1402
1403 static routing_component_t generic_as_exist(routing_component_t find_from,
1404                                             routing_component_t to_find)
1405 {
1406   //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1407   xbt_dict_cursor_t cursor = NULL;
1408   char *key;
1409   int found = 0;
1410   routing_component_t elem;
1411   xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1412     if (to_find == elem || generic_as_exist(elem, to_find)) {
1413       found = 1;
1414       break;
1415     }
1416   }
1417   if (found)
1418     return to_find;
1419   return NULL;
1420 }
1421
1422 routing_component_t
1423 generic_autonomous_system_exist(routing_component_t rc, char *element)
1424 {
1425   //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1426   routing_component_t element_as, result, elem;
1427   xbt_dict_cursor_t cursor = NULL;
1428   char *key;
1429   element_as = ((network_element_info_t)
1430                 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1431   result = ((routing_component_t) - 1);
1432   if (element_as != rc)
1433     result = generic_as_exist(rc, element_as);
1434
1435   int found = 0;
1436   if (result) {
1437     xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1438       found = !strcmp(elem->name, element);
1439       if (found)
1440         break;
1441     }
1442     if (found)
1443       return element_as;
1444   }
1445   return NULL;
1446 }
1447
1448 routing_component_t
1449 generic_processing_units_exist(routing_component_t rc, char *element)
1450 {
1451   routing_component_t element_as;
1452   element_as = ((network_element_info_t)
1453                 xbt_lib_get_or_null(host_lib,
1454                  element, ROUTING_HOST_LEVEL))->rc_component;
1455   if (element_as == rc)
1456     return element_as;
1457   return generic_as_exist(rc, element_as);
1458 }
1459
1460 void generic_src_dst_check(routing_component_t rc, const char *src,
1461                                   const char *dst)
1462 {
1463
1464   void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1465   void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1466   if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1467   if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1468
1469   if(src_data == NULL || dst_data == NULL)
1470           xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1471                      src, dst, rc->name);
1472
1473   routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1474   routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1475
1476   if(src_as != dst_as)
1477           xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1478               src, src_as->name, dst, dst_as->name);
1479   if(rc != dst_as)
1480          xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1481      src,dst,src_as->name, dst_as->name,rc->name);
1482 }
1483
1484 static void routing_parse_Sconfig(void)
1485 {
1486   XBT_DEBUG("START configuration name = %s",A_surfxml_config_id);
1487 }
1488
1489 static void routing_parse_Econfig(void)
1490 {
1491   xbt_dict_cursor_t cursor = NULL;
1492   char *key;
1493   char *elem;
1494   char *cfg;
1495   xbt_dict_foreach(current_property_set, cursor, key, elem) {
1496           cfg = bprintf("%s:%s",key,elem);
1497           if(xbt_cfg_is_default_value(_surf_cfg_set, key))
1498                   xbt_cfg_set_parse(_surf_cfg_set, cfg);
1499           else
1500                   XBT_INFO("The custom configuration '%s' is already define by user!",key);
1501           free(cfg);
1502   }
1503   XBT_DEBUG("End configuration name = %s",A_surfxml_config_id);
1504 }
1505
1506 void routing_parse_Scluster(void)
1507 {
1508   static int AX_ptr = 0;
1509
1510   char *cluster_id = A_surfxml_cluster_id;
1511   char *cluster_prefix = A_surfxml_cluster_prefix;
1512   char *cluster_suffix = A_surfxml_cluster_suffix;
1513   char *cluster_radical = A_surfxml_cluster_radical;
1514   char *cluster_core = A_surfxml_cluster_core;
1515   char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1516   char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1517   char *cluster_power = xbt_strdup(A_surfxml_cluster_power);
1518   char *cluster_bw = xbt_strdup(A_surfxml_cluster_bw);
1519   char *cluster_lat = xbt_strdup(A_surfxml_cluster_lat);
1520   char *host_id, *groups, *link_id = NULL;
1521   char *router_id = xbt_strdup(A_surfxml_cluster_router_id);
1522   char *availability_file = xbt_strdup(A_surfxml_cluster_availability_file);
1523   char *state_file = xbt_strdup(A_surfxml_cluster_state_file);
1524
1525   if(xbt_dict_size(patterns)==0)
1526           patterns = xbt_dict_new();
1527
1528   xbt_dict_set(patterns,"id",cluster_id,NULL);
1529   xbt_dict_set(patterns,"prefix",cluster_prefix,NULL);
1530   xbt_dict_set(patterns,"suffix",cluster_suffix,NULL);
1531
1532   cluster_power = replace_random_parameter(cluster_power);
1533   cluster_bw = replace_random_parameter(cluster_bw);
1534   cluster_lat = replace_random_parameter(cluster_lat);
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, xbt_dynar_get_as(radical_ends, 0, char *));
1562       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
1563       link_id = bprintf("%s_link_%d", cluster_id, start);
1564
1565       xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1566       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, cluster_power);
1567       A_surfxml_host_state = A_surfxml_host_state_ON;
1568       SURFXML_BUFFER_SET(host_id, host_id);
1569       SURFXML_BUFFER_SET(host_power, cluster_power);
1570       SURFXML_BUFFER_SET(host_core, cluster_core);
1571       SURFXML_BUFFER_SET(host_availability, "1.0");
1572       SURFXML_BUFFER_SET(host_coordinates, "");
1573
1574       char* tmp_availability_file = xbt_strdup(availability_file);
1575       char* tmp_state_file = xbt_strdup(state_file);
1576       XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(tmp_availability_file,patterns));
1577       XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(tmp_state_file,patterns));
1578       SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(tmp_availability_file,patterns));
1579       SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(tmp_state_file,patterns));
1580       xbt_free(tmp_availability_file);
1581       xbt_free(tmp_state_file);
1582       XBT_DEBUG("</host>");
1583       SURFXML_START_TAG(host);
1584       SURFXML_END_TAG(host);
1585
1586       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
1587       A_surfxml_link_state = A_surfxml_link_state_ON;
1588       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1589       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1590           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1591       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1592           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1593       SURFXML_BUFFER_SET(link_id, link_id);
1594       SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
1595       SURFXML_BUFFER_SET(link_latency, cluster_lat);
1596       SURFXML_BUFFER_SET(link_bandwidth_file, "");
1597       SURFXML_BUFFER_SET(link_latency_file, "");
1598       SURFXML_BUFFER_SET(link_state_file, "");
1599       SURFXML_START_TAG(link);
1600       SURFXML_END_TAG(link);
1601
1602       free(link_id);
1603       free(host_id);
1604       break;
1605
1606     case 2:
1607
1608       surf_parse_get_int(&start,
1609                          xbt_dynar_get_as(radical_ends, 0, char *));
1610       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
1611       for (i = start; i <= end; i++) {
1612         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
1613         link_id = bprintf("%s_link_%d", cluster_id, i);
1614
1615         xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1616         XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, cluster_power);
1617         A_surfxml_host_state = A_surfxml_host_state_ON;
1618         SURFXML_BUFFER_SET(host_id, host_id);
1619         SURFXML_BUFFER_SET(host_power, cluster_power);
1620         SURFXML_BUFFER_SET(host_core, cluster_core);
1621         SURFXML_BUFFER_SET(host_availability, "1.0");
1622         SURFXML_BUFFER_SET(host_coordinates, "");
1623
1624         char* tmp_availability_file = xbt_strdup(availability_file);
1625         char* tmp_state_file = xbt_strdup(state_file);
1626         XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(tmp_availability_file,patterns));
1627         XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(tmp_state_file,patterns));
1628         SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(tmp_availability_file,patterns));
1629         SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(tmp_state_file,patterns));
1630         xbt_free(tmp_availability_file);
1631         xbt_free(tmp_state_file);
1632         XBT_DEBUG("</host>");
1633         SURFXML_START_TAG(host);
1634         SURFXML_END_TAG(host);
1635
1636         XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
1637         A_surfxml_link_state = A_surfxml_link_state_ON;
1638         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1639         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1640             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1641         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1642             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1643         SURFXML_BUFFER_SET(link_id, link_id);
1644         SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
1645         SURFXML_BUFFER_SET(link_latency, cluster_lat);
1646         SURFXML_BUFFER_SET(link_bandwidth_file, "");
1647         SURFXML_BUFFER_SET(link_latency_file, "");
1648         SURFXML_BUFFER_SET(link_state_file, "");
1649         SURFXML_START_TAG(link);
1650         SURFXML_END_TAG(link);
1651
1652         free(link_id);
1653         free(host_id);
1654       }
1655       break;
1656
1657     default:
1658       XBT_DEBUG("Malformed radical");
1659       break;
1660     }
1661
1662     xbt_dynar_free(&radical_ends);
1663   }
1664   xbt_dynar_free(&radical_elements);
1665
1666   XBT_DEBUG(" ");
1667   if(!strcmp(router_id,""))
1668           router_id = bprintf("%s%s_router%s", cluster_prefix, cluster_id, cluster_suffix);
1669
1670   XBT_DEBUG("<router id=\"%s\"/>", router_id);
1671   SURFXML_BUFFER_SET(router_id, router_id);
1672   SURFXML_BUFFER_SET(router_coordinates, "");
1673   SURFXML_START_TAG(router);
1674   SURFXML_END_TAG(router);
1675
1676   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1677   char *link_backbone = bprintf("%s_backbone", cluster_id);
1678   XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bb_bw, cluster_bb_lat);
1679   A_surfxml_link_state = A_surfxml_link_state_ON;
1680   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1681   if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
1682   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1683   SURFXML_BUFFER_SET(link_id, link_backbone);
1684   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
1685   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
1686   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1687   SURFXML_BUFFER_SET(link_latency_file, "");
1688   SURFXML_BUFFER_SET(link_state_file, "");
1689   SURFXML_START_TAG(link);
1690   SURFXML_END_TAG(link);
1691   free(link_backbone);
1692   }
1693
1694   XBT_DEBUG(" ");
1695
1696   char *new_suffix = xbt_strdup("");
1697
1698   radical_elements = xbt_str_split(cluster_suffix, ".");
1699   xbt_dynar_foreach(radical_elements, iter, groups) {
1700     if (strcmp(groups, "")) {
1701       char *old_suffix = new_suffix;
1702       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1703       free(old_suffix);
1704     }
1705   }
1706   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
1707   xbt_dynar_free(&radical_elements);
1708   xbt_free(new_suffix);
1709
1710   char *pcre_link_src = bprintf("%s_link_$1src", cluster_id);
1711   char *pcre_link_backbone = bprintf("%s_backbone", cluster_id);
1712   char *pcre_link_dst = bprintf("%s_link_$1dst", cluster_id);
1713
1714   //from router to router
1715   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, router_id);
1716   XBT_DEBUG("symmetrical=\"NO\">");
1717   SURFXML_BUFFER_SET(route_src, router_id);
1718   SURFXML_BUFFER_SET(route_dst, router_id);
1719   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1720   SURFXML_START_TAG(route);
1721
1722   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1723   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1724   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1725   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1726   SURFXML_START_TAG(link_ctn);
1727   SURFXML_END_TAG(link_ctn);
1728   }
1729
1730   XBT_DEBUG("</route>");
1731   SURFXML_END_TAG(route);
1732
1733   //from host to router
1734   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, router_id);
1735   XBT_DEBUG("symmetrical=\"NO\">");
1736   SURFXML_BUFFER_SET(route_src, route_src_dst);
1737   SURFXML_BUFFER_SET(route_dst, router_id);
1738   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1739   SURFXML_START_TAG(route);
1740
1741   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
1742   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
1743   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1744   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1745   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1746   SURFXML_START_TAG(link_ctn);
1747   SURFXML_END_TAG(link_ctn);
1748
1749   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1750   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1751   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1752   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1753   SURFXML_START_TAG(link_ctn);
1754   SURFXML_END_TAG(link_ctn);
1755   }
1756
1757   XBT_DEBUG("</route>");
1758   SURFXML_END_TAG(route);
1759
1760   //from router to host
1761   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, route_src_dst);
1762   XBT_DEBUG("symmetrical=\"NO\">");
1763   SURFXML_BUFFER_SET(route_src, router_id);
1764   SURFXML_BUFFER_SET(route_dst, route_src_dst);
1765   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1766   SURFXML_START_TAG(route);
1767
1768   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1769   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1770   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1771   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1772   SURFXML_START_TAG(link_ctn);
1773   SURFXML_END_TAG(link_ctn);
1774   }
1775
1776   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
1777   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
1778   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1779   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1780   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1781   SURFXML_START_TAG(link_ctn);
1782   SURFXML_END_TAG(link_ctn);
1783
1784   XBT_DEBUG("</route>");
1785   SURFXML_END_TAG(route);
1786
1787   //from host to host
1788   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
1789   XBT_DEBUG("symmetrical=\"NO\">");
1790   SURFXML_BUFFER_SET(route_src, route_src_dst);
1791   SURFXML_BUFFER_SET(route_dst, route_src_dst);
1792   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1793   SURFXML_START_TAG(route);
1794
1795   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
1796   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
1797   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1798   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1799   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1800   SURFXML_START_TAG(link_ctn);
1801   SURFXML_END_TAG(link_ctn);
1802
1803   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1804   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1805   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1806   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1807   SURFXML_START_TAG(link_ctn);
1808   SURFXML_END_TAG(link_ctn);
1809   }
1810
1811   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
1812   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
1813   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1814   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1815   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
1816   SURFXML_START_TAG(link_ctn);
1817   SURFXML_END_TAG(link_ctn);
1818
1819   XBT_DEBUG("</route>");
1820   SURFXML_END_TAG(route);
1821
1822   free(pcre_link_dst);
1823   free(pcre_link_backbone);
1824   free(pcre_link_src);
1825   free(route_src_dst);
1826
1827   xbt_free(cluster_bw);
1828   xbt_free(cluster_lat);
1829   xbt_free(cluster_power);
1830   xbt_free(availability_file);
1831   xbt_free(state_file);
1832
1833   xbt_free(router_id);
1834   xbt_dict_free(&patterns);
1835
1836   XBT_DEBUG("</AS>");
1837   SURFXML_END_TAG(AS);
1838   XBT_DEBUG(" ");
1839
1840   surfxml_bufferstack_pop(1);
1841 }
1842 /*
1843  * This function take a string and replace parameters from patterns dict.
1844  * It returns the new value.
1845  */
1846 static char* replace_random_parameter(char * string)
1847 {
1848   char *test_string = NULL;
1849
1850   if(xbt_dict_size(random_value)==0)
1851     return string;
1852
1853   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1854   test_string = bprintf("${%s}", string);
1855   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1856
1857   if (strcmp(test_string,"")) { //if not empty, keep this value.
1858     xbt_free(string);
1859     string = test_string;
1860   } //In other case take old value (without ${})
1861   else
1862         free(test_string);
1863   return string;
1864 }
1865
1866 static void clean_dict_random(void)
1867 {
1868         xbt_dict_free(&random_value);
1869         xbt_dict_free(&patterns);
1870 }
1871
1872 static void routing_parse_Speer(void)
1873 {
1874   static int AX_ptr = 0;
1875
1876   char *peer_id = A_surfxml_peer_id;
1877   char *peer_power = A_surfxml_peer_power;
1878   char *peer_bw_in = A_surfxml_peer_bw_in;
1879   char *peer_bw_out = A_surfxml_peer_bw_out;
1880   char *peer_lat = A_surfxml_peer_lat;
1881   char *peer_coord = A_surfxml_peer_coordinates;
1882   char *peer_state_file = A_surfxml_peer_state_file;
1883   char *peer_availability_file = A_surfxml_peer_availability_file;
1884
1885   char *host_id = NULL;
1886   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1887
1888   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1889   static unsigned int surfxml_buffer_stack_stack[1024];
1890
1891   surfxml_buffer_stack_stack[0] = 0;
1892
1893   surfxml_bufferstack_push(1);
1894
1895   SURFXML_BUFFER_SET(AS_id, peer_id);
1896
1897   SURFXML_BUFFER_SET(AS_routing, "Full");
1898   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer_id);
1899
1900   SURFXML_START_TAG(AS);
1901
1902   XBT_DEBUG(" ");
1903   host_id = bprintf("peer_%s", peer_id);
1904   router_id = bprintf("router_%s", peer_id);
1905   link_id_up = bprintf("link_%s_up", peer_id);
1906   link_id_down = bprintf("link_%s_down", peer_id);
1907
1908   link_router = bprintf("%s_link_router", peer_id);
1909   link_backbone = bprintf("%s_backbone", peer_id);
1910
1911   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, peer_power);
1912   A_surfxml_host_state = A_surfxml_host_state_ON;
1913   SURFXML_BUFFER_SET(host_id, host_id);
1914   SURFXML_BUFFER_SET(host_power, peer_power);
1915   SURFXML_BUFFER_SET(host_availability, "1.0");
1916   SURFXML_BUFFER_SET(host_availability_file, peer_availability_file);
1917   SURFXML_BUFFER_SET(host_state_file, peer_state_file);
1918   SURFXML_BUFFER_SET(host_coordinates, "");
1919   SURFXML_BUFFER_SET(host_core, "1.0");
1920   SURFXML_START_TAG(host);
1921   SURFXML_END_TAG(host);
1922
1923   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer_coord);
1924   SURFXML_BUFFER_SET(router_id, router_id);
1925   SURFXML_BUFFER_SET(router_coordinates, peer_coord);
1926   SURFXML_START_TAG(router);
1927   SURFXML_END_TAG(router);
1928
1929   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, peer_bw_in, peer_lat);
1930   A_surfxml_link_state = A_surfxml_link_state_ON;
1931   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1932   SURFXML_BUFFER_SET(link_id, link_id_up);
1933   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_in);
1934   SURFXML_BUFFER_SET(link_latency, peer_lat);
1935   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1936   SURFXML_BUFFER_SET(link_latency_file, "");
1937   SURFXML_BUFFER_SET(link_state_file, "");
1938   SURFXML_START_TAG(link);
1939   SURFXML_END_TAG(link);
1940
1941   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, peer_bw_out, peer_lat);
1942   A_surfxml_link_state = A_surfxml_link_state_ON;
1943   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1944   SURFXML_BUFFER_SET(link_id, link_id_down);
1945   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_out);
1946   SURFXML_BUFFER_SET(link_latency, peer_lat);
1947   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1948   SURFXML_BUFFER_SET(link_latency_file, "");
1949   SURFXML_BUFFER_SET(link_state_file, "");
1950   SURFXML_START_TAG(link);
1951   SURFXML_END_TAG(link);
1952
1953   XBT_DEBUG(" ");
1954
1955   // begin here
1956   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1957   XBT_DEBUG("symmetrical=\"NO\">");
1958   SURFXML_BUFFER_SET(route_src, host_id);
1959   SURFXML_BUFFER_SET(route_dst, router_id);
1960   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1961   SURFXML_START_TAG(route);
1962
1963   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1964   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1965   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1966   SURFXML_START_TAG(link_ctn);
1967   SURFXML_END_TAG(link_ctn);
1968
1969   XBT_DEBUG("</route>");
1970   SURFXML_END_TAG(route);
1971
1972   //Opposite Route
1973   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1974   XBT_DEBUG("symmetrical=\"NO\">");
1975   SURFXML_BUFFER_SET(route_src, router_id);
1976   SURFXML_BUFFER_SET(route_dst, host_id);
1977   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1978   SURFXML_START_TAG(route);
1979
1980   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1981   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1982   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1983   SURFXML_START_TAG(link_ctn);
1984   SURFXML_END_TAG(link_ctn);
1985
1986   XBT_DEBUG("</route>");
1987   SURFXML_END_TAG(route);
1988
1989   XBT_DEBUG("</AS>");
1990   SURFXML_END_TAG(AS);
1991   XBT_DEBUG(" ");
1992
1993   //xbt_dynar_free(&tab_elements_num);
1994         free(host_id);
1995         free(router_id);
1996         free(link_router);
1997         free(link_backbone);
1998         free(link_id_up);
1999         free(link_id_down);
2000   surfxml_bufferstack_pop(1);
2001 }
2002
2003 static void routing_parse_Srandom(void)
2004 {
2005           double mean, std, min, max, seed;
2006           char *random_id = A_surfxml_random_id;
2007           char *random_radical = A_surfxml_random_radical;
2008           char *rd_name = NULL;
2009           char *rd_value;
2010           surf_parse_get_double(&mean,A_surfxml_random_mean);
2011           surf_parse_get_double(&std,A_surfxml_random_std_deviation);
2012           surf_parse_get_double(&min,A_surfxml_random_min);
2013           surf_parse_get_double(&max,A_surfxml_random_max);
2014           surf_parse_get_double(&seed,A_surfxml_random_seed);
2015
2016           double res = 0;
2017           int i = 0;
2018           random_data_t random = xbt_new0(s_random_data_t, 1);
2019           char *tmpbuf;
2020
2021           xbt_dynar_t radical_elements;
2022           unsigned int iter;
2023           char *groups;
2024           int start, end;
2025           xbt_dynar_t radical_ends;
2026
2027           random->generator = A_surfxml_random_generator;
2028           random->seed = seed;
2029           random->min = min;
2030           random->max = max;
2031
2032           /* Check user stupidities */
2033           if (max < min)
2034             THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
2035           if (mean < min)
2036             THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
2037                    min);
2038           if (mean > max)
2039             THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
2040                    max);
2041
2042           /* normalize the mean and standard deviation before storing */
2043           random->mean = (mean - min) / (max - min);
2044           random->std = std / (max - min);
2045
2046           if (random->mean * (1 - random->mean) < random->std * random->std)
2047             THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
2048                    random->mean, random->std);
2049
2050           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
2051           random_id,
2052           random->min,
2053           random->max,
2054           random->mean,
2055           random->std,
2056           random->generator,
2057           random->seed,
2058           random_radical);
2059
2060           if(xbt_dict_size(random_value)==0)
2061                   random_value = xbt_dict_new();
2062
2063           if(!strcmp(random_radical,""))
2064           {
2065                   res = random_generate(random);
2066                   rd_value = bprintf("%f",res);
2067                   xbt_dict_set(random_value, random_id, rd_value, free);
2068           }
2069           else
2070           {
2071                   radical_elements = xbt_str_split(random_radical, ",");
2072                   xbt_dynar_foreach(radical_elements, iter, groups) {
2073                         radical_ends = xbt_str_split(groups, "-");
2074                         switch (xbt_dynar_length(radical_ends)) {
2075                         case 1:
2076                                           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
2077                                           res = random_generate(random);
2078                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
2079                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2080                                           xbt_free(tmpbuf);
2081                                           break;
2082
2083                         case 2:   surf_parse_get_int(&start,
2084                                                                                  xbt_dynar_get_as(radical_ends, 0, char *));
2085                                           surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
2086                                           for (i = start; i <= end; i++) {
2087                                                   xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
2088                                                   res = random_generate(random);
2089                           tmpbuf = bprintf("%s%d",random_id,i);
2090                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2091                           xbt_free(tmpbuf);
2092                                           }
2093                                           break;
2094                         default:
2095                                 XBT_INFO("Malformed radical");
2096                                 break;
2097                         }
2098                         res = random_generate(random);
2099                         rd_name  = bprintf("%s_router",random_id);
2100                         rd_value = bprintf("%f",res);
2101                         xbt_dict_set(random_value, rd_name, rd_value, free);
2102
2103                         xbt_dynar_free(&radical_ends);
2104                   }
2105                   free(rd_name);
2106                   xbt_dynar_free(&radical_elements);
2107           }
2108 }
2109
2110 static void routing_parse_Erandom(void)
2111 {
2112         xbt_dict_cursor_t cursor = NULL;
2113         char *key;
2114         char *elem;
2115
2116         xbt_dict_foreach(random_value, cursor, key, elem) {
2117           XBT_DEBUG("%s = %s",key,elem);
2118         }
2119
2120 }
2121
2122
2123 /*
2124  * New methods to init the routing model component from the lua script
2125  */
2126
2127 /*
2128  * calling parse_S_AS_lua with lua values
2129  */
2130 void routing_AS_init(const char *AS_id, const char *AS_routing)
2131 {
2132   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
2133 }
2134
2135 /*
2136  * calling parse_E_AS_lua to fisnish the creation of routing component
2137  */
2138 void routing_AS_end(const char *AS_id)
2139 {
2140   parse_E_AS_lua((char *) AS_id);
2141 }
2142
2143 /*
2144  * add a host to the network element list
2145  */
2146
2147 void routing_add_host(const char *host_id)
2148 {
2149   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
2150 }
2151
2152 /*
2153  * Set a new link on the actual list of link for a route or ASroute
2154  */
2155 void routing_add_link(const char *link_id)
2156 {
2157   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
2158 }
2159
2160 /*
2161  *Set the endpoints for a route
2162  */
2163 void routing_set_route(const char *src_id, const char *dst_id)
2164 {
2165   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
2166 }
2167
2168 /*
2169  * Store the route by calling parse_E_route_store_route
2170  */
2171 void routing_store_route(void)
2172 {
2173   parse_E_route_store_route();
2174 }