Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a31da5675369a5b333c60308da4d1f9a4830d926
[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 *cluster_availability_file = A_surfxml_cluster_availability_file;
1521   char *cluster_state_file = A_surfxml_cluster_state_file;
1522   char *host_id, *groups, *link_id = NULL;
1523   char *router_id = xbt_strdup(A_surfxml_cluster_router_id);
1524   char *availability_file = xbt_strdup(cluster_availability_file);
1525   char *state_file = xbt_strdup(cluster_state_file);
1526
1527   if(xbt_dict_size(patterns)==0)
1528           patterns = xbt_dict_new();
1529
1530   xbt_dict_set(patterns,"id",cluster_id,NULL);
1531   xbt_dict_set(patterns,"prefix",cluster_prefix,NULL);
1532   xbt_dict_set(patterns,"suffix",cluster_suffix,NULL);
1533
1534   cluster_power = replace_random_parameter(cluster_power);
1535   cluster_bw = replace_random_parameter(cluster_bw);
1536   cluster_lat = replace_random_parameter(cluster_lat);
1537
1538   char *route_src_dst;
1539   unsigned int iter;
1540   int start, end, i;
1541   xbt_dynar_t radical_elements;
1542   xbt_dynar_t radical_ends;
1543   int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
1544   int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
1545
1546   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1547   static unsigned int surfxml_buffer_stack_stack[1024];
1548
1549   surfxml_buffer_stack_stack[0] = 0;
1550
1551   surfxml_bufferstack_push(1);
1552
1553   SURFXML_BUFFER_SET(AS_id, cluster_id);
1554   SURFXML_BUFFER_SET(AS_routing, "RuleBased");
1555   XBT_DEBUG("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
1556   SURFXML_START_TAG(AS);
1557
1558   radical_elements = xbt_str_split(cluster_radical, ",");
1559   xbt_dynar_foreach(radical_elements, iter, groups) {
1560     radical_ends = xbt_str_split(groups, "-");
1561     switch (xbt_dynar_length(radical_ends)) {
1562     case 1:
1563       surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
1564       host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
1565       link_id = bprintf("%s_link_%d", cluster_id, start);
1566
1567       xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1568       XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, cluster_power);
1569       A_surfxml_host_state = A_surfxml_host_state_ON;
1570       SURFXML_BUFFER_SET(host_id, host_id);
1571       SURFXML_BUFFER_SET(host_power, cluster_power);
1572       SURFXML_BUFFER_SET(host_core, cluster_core);
1573       SURFXML_BUFFER_SET(host_availability, "1.0");
1574       SURFXML_BUFFER_SET(host_coordinates, "");
1575
1576       XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
1577       XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
1578       SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
1579       SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
1580       XBT_DEBUG("</host>");
1581       SURFXML_START_TAG(host);
1582       SURFXML_END_TAG(host);
1583
1584       XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
1585       A_surfxml_link_state = A_surfxml_link_state_ON;
1586       A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1587       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1588           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1589       if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1590           {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1591       SURFXML_BUFFER_SET(link_id, link_id);
1592       SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
1593       SURFXML_BUFFER_SET(link_latency, cluster_lat);
1594       SURFXML_BUFFER_SET(link_bandwidth_file, "");
1595       SURFXML_BUFFER_SET(link_latency_file, "");
1596       SURFXML_BUFFER_SET(link_state_file, "");
1597       SURFXML_START_TAG(link);
1598       SURFXML_END_TAG(link);
1599
1600       free(link_id);
1601       free(host_id);
1602       break;
1603
1604     case 2:
1605
1606       surf_parse_get_int(&start,
1607                          xbt_dynar_get_as(radical_ends, 0, char *));
1608       surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
1609       for (i = start; i <= end; i++) {
1610         host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
1611         link_id = bprintf("%s_link_%d", cluster_id, i);
1612
1613         xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1614         XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\">", host_id, cluster_power);
1615         A_surfxml_host_state = A_surfxml_host_state_ON;
1616         SURFXML_BUFFER_SET(host_id, host_id);
1617         SURFXML_BUFFER_SET(host_power, cluster_power);
1618         SURFXML_BUFFER_SET(host_core, cluster_core);
1619         SURFXML_BUFFER_SET(host_availability, "1.0");
1620         SURFXML_BUFFER_SET(host_coordinates, "");
1621
1622         XBT_DEBUG("\tavailability_file=\"%s\"",xbt_str_varsubst(availability_file,patterns));
1623         XBT_DEBUG("\tstate_file=\"%s\"",xbt_str_varsubst(state_file,patterns));
1624         SURFXML_BUFFER_SET(host_availability_file, xbt_str_varsubst(availability_file,patterns));
1625         SURFXML_BUFFER_SET(host_state_file, xbt_str_varsubst(state_file,patterns));
1626         XBT_DEBUG("</host>");
1627         SURFXML_START_TAG(host);
1628         SURFXML_END_TAG(host);
1629
1630         XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
1631         A_surfxml_link_state = A_surfxml_link_state_ON;
1632         A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1633         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1634             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FULLDUPLEX;}
1635         if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1636             {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1637         SURFXML_BUFFER_SET(link_id, link_id);
1638         SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
1639         SURFXML_BUFFER_SET(link_latency, cluster_lat);
1640         SURFXML_BUFFER_SET(link_bandwidth_file, "");
1641         SURFXML_BUFFER_SET(link_latency_file, "");
1642         SURFXML_BUFFER_SET(link_state_file, "");
1643         SURFXML_START_TAG(link);
1644         SURFXML_END_TAG(link);
1645
1646         free(link_id);
1647         free(host_id);
1648       }
1649       break;
1650
1651     default:
1652       XBT_DEBUG("Malformed radical");
1653       break;
1654     }
1655
1656     xbt_dynar_free(&radical_ends);
1657   }
1658   xbt_dynar_free(&radical_elements);
1659
1660   XBT_DEBUG(" ");
1661   if(!strcmp(router_id,""))
1662           router_id = bprintf("%s%s_router%s", cluster_prefix, cluster_id, cluster_suffix);
1663
1664   XBT_DEBUG("<router id=\"%s\"/>", router_id);
1665   SURFXML_BUFFER_SET(router_id, router_id);
1666   SURFXML_BUFFER_SET(router_coordinates, "");
1667   SURFXML_START_TAG(router);
1668   SURFXML_END_TAG(router);
1669
1670   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1671   char *link_backbone = bprintf("%s_backbone", cluster_id);
1672   XBT_DEBUG("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bb_bw, cluster_bb_lat);
1673   A_surfxml_link_state = A_surfxml_link_state_ON;
1674   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1675   if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
1676   {A_surfxml_link_sharing_policy =  A_surfxml_link_sharing_policy_FATPIPE;}
1677   SURFXML_BUFFER_SET(link_id, link_backbone);
1678   SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
1679   SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
1680   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1681   SURFXML_BUFFER_SET(link_latency_file, "");
1682   SURFXML_BUFFER_SET(link_state_file, "");
1683   SURFXML_START_TAG(link);
1684   SURFXML_END_TAG(link);
1685   free(link_backbone);
1686   }
1687
1688   XBT_DEBUG(" ");
1689
1690   char *new_suffix = xbt_strdup("");
1691
1692   radical_elements = xbt_str_split(cluster_suffix, ".");
1693   xbt_dynar_foreach(radical_elements, iter, groups) {
1694     if (strcmp(groups, "")) {
1695       char *old_suffix = new_suffix;
1696       new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1697       free(old_suffix);
1698     }
1699   }
1700   route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
1701   xbt_dynar_free(&radical_elements);
1702   free(new_suffix);
1703
1704   char *pcre_link_src = bprintf("%s_link_$1src", cluster_id);
1705   char *pcre_link_backbone = bprintf("%s_backbone", cluster_id);
1706   char *pcre_link_dst = bprintf("%s_link_$1dst", cluster_id);
1707
1708   //from router to router
1709   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, router_id);
1710   XBT_DEBUG("symmetrical=\"NO\">");
1711   SURFXML_BUFFER_SET(route_src, router_id);
1712   SURFXML_BUFFER_SET(route_dst, router_id);
1713   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1714   SURFXML_START_TAG(route);
1715
1716   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1717   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1718   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1719   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1720   SURFXML_START_TAG(link_ctn);
1721   SURFXML_END_TAG(link_ctn);
1722   }
1723
1724   XBT_DEBUG("</route>");
1725   SURFXML_END_TAG(route);
1726
1727   //from host to router
1728   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, router_id);
1729   XBT_DEBUG("symmetrical=\"NO\">");
1730   SURFXML_BUFFER_SET(route_src, route_src_dst);
1731   SURFXML_BUFFER_SET(route_dst, router_id);
1732   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1733   SURFXML_START_TAG(route);
1734
1735   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
1736   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
1737   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1738   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1739   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1740   SURFXML_START_TAG(link_ctn);
1741   SURFXML_END_TAG(link_ctn);
1742
1743   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1744   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1745   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1746   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1747   SURFXML_START_TAG(link_ctn);
1748   SURFXML_END_TAG(link_ctn);
1749   }
1750
1751   XBT_DEBUG("</route>");
1752   SURFXML_END_TAG(route);
1753
1754   //from router to host
1755   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, route_src_dst);
1756   XBT_DEBUG("symmetrical=\"NO\">");
1757   SURFXML_BUFFER_SET(route_src, router_id);
1758   SURFXML_BUFFER_SET(route_dst, route_src_dst);
1759   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1760   SURFXML_START_TAG(route);
1761
1762   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1763   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1764   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1765   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1766   SURFXML_START_TAG(link_ctn);
1767   SURFXML_END_TAG(link_ctn);
1768   }
1769
1770   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
1771   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
1772   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1773   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1774   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1775   SURFXML_START_TAG(link_ctn);
1776   SURFXML_END_TAG(link_ctn);
1777
1778   XBT_DEBUG("</route>");
1779   SURFXML_END_TAG(route);
1780
1781   //from host to host
1782   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
1783   XBT_DEBUG("symmetrical=\"NO\">");
1784   SURFXML_BUFFER_SET(route_src, route_src_dst);
1785   SURFXML_BUFFER_SET(route_dst, route_src_dst);
1786   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1787   SURFXML_START_TAG(route);
1788
1789   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_src);
1790   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_src);
1791   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1792   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1793   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
1794   SURFXML_START_TAG(link_ctn);
1795   SURFXML_END_TAG(link_ctn);
1796
1797   if(strcmp(cluster_bb_bw,"") && strcmp(cluster_bb_lat,"")){
1798   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_backbone);
1799   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_backbone);
1800   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1801   SURFXML_START_TAG(link_ctn);
1802   SURFXML_END_TAG(link_ctn);
1803   }
1804
1805   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", pcre_link_dst);
1806   SURFXML_BUFFER_SET(link_ctn_id, pcre_link_dst);
1807   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1808   if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1809   {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
1810   SURFXML_START_TAG(link_ctn);
1811   SURFXML_END_TAG(link_ctn);
1812
1813   XBT_DEBUG("</route>");
1814   SURFXML_END_TAG(route);
1815
1816   free(pcre_link_dst);
1817   free(pcre_link_backbone);
1818   free(pcre_link_src);
1819   free(route_src_dst);
1820
1821   xbt_free(cluster_bw);
1822   xbt_free(cluster_lat);
1823   xbt_free(cluster_power);
1824   xbt_free(availability_file);
1825   xbt_free(state_file);
1826
1827   free(router_id);
1828   xbt_dict_free(&patterns);
1829
1830   XBT_DEBUG("</AS>");
1831   SURFXML_END_TAG(AS);
1832   XBT_DEBUG(" ");
1833
1834   surfxml_bufferstack_pop(1);
1835 }
1836 /*
1837  * This function take a string and replace parameters from patterns dict.
1838  * It returns the new value.
1839  */
1840 static char* replace_random_parameter(char * string)
1841 {
1842   char *test_string = NULL;
1843
1844   if(xbt_dict_size(random_value)==0)
1845     return string;
1846
1847   string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1848   test_string = bprintf("${%s}", string);
1849   test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1850
1851   if (strcmp(test_string,"")) { //if not empty, keep this value.
1852     xbt_free(string);
1853     string = test_string;
1854   } //In other case take old value (without ${})
1855   else
1856         free(test_string);
1857   return string;
1858 }
1859
1860 static void clean_dict_random(void)
1861 {
1862         xbt_dict_free(&random_value);
1863         xbt_dict_free(&patterns);
1864 }
1865
1866 static void routing_parse_Speer(void)
1867 {
1868   static int AX_ptr = 0;
1869
1870   char *peer_id = A_surfxml_peer_id;
1871   char *peer_power = A_surfxml_peer_power;
1872   char *peer_bw_in = A_surfxml_peer_bw_in;
1873   char *peer_bw_out = A_surfxml_peer_bw_out;
1874   char *peer_lat = A_surfxml_peer_lat;
1875   char *peer_coord = A_surfxml_peer_coordinates;
1876   char *peer_state_file = A_surfxml_peer_state_file;
1877   char *peer_availability_file = A_surfxml_peer_availability_file;
1878
1879   char *host_id = NULL;
1880   char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1881
1882   static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1883   static unsigned int surfxml_buffer_stack_stack[1024];
1884
1885   surfxml_buffer_stack_stack[0] = 0;
1886
1887   surfxml_bufferstack_push(1);
1888
1889   SURFXML_BUFFER_SET(AS_id, peer_id);
1890
1891   SURFXML_BUFFER_SET(AS_routing, "Full");
1892   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer_id);
1893
1894   SURFXML_START_TAG(AS);
1895
1896   XBT_DEBUG(" ");
1897   host_id = bprintf("peer_%s", peer_id);
1898   router_id = bprintf("router_%s", peer_id);
1899   link_id_up = bprintf("link_%s_up", peer_id);
1900   link_id_down = bprintf("link_%s_down", peer_id);
1901
1902   link_router = bprintf("%s_link_router", peer_id);
1903   link_backbone = bprintf("%s_backbone", peer_id);
1904
1905   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, peer_power);
1906   A_surfxml_host_state = A_surfxml_host_state_ON;
1907   SURFXML_BUFFER_SET(host_id, host_id);
1908   SURFXML_BUFFER_SET(host_power, peer_power);
1909   SURFXML_BUFFER_SET(host_availability, "1.0");
1910   SURFXML_BUFFER_SET(host_availability_file, peer_availability_file);
1911   SURFXML_BUFFER_SET(host_state_file, peer_state_file);
1912   SURFXML_BUFFER_SET(host_coordinates, "");
1913   SURFXML_BUFFER_SET(host_core, "1.0");
1914   SURFXML_START_TAG(host);
1915   SURFXML_END_TAG(host);
1916
1917   XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer_coord);
1918   SURFXML_BUFFER_SET(router_id, router_id);
1919   SURFXML_BUFFER_SET(router_coordinates, peer_coord);
1920   SURFXML_START_TAG(router);
1921   SURFXML_END_TAG(router);
1922
1923   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, peer_bw_in, peer_lat);
1924   A_surfxml_link_state = A_surfxml_link_state_ON;
1925   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1926   SURFXML_BUFFER_SET(link_id, link_id_up);
1927   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_in);
1928   SURFXML_BUFFER_SET(link_latency, peer_lat);
1929   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1930   SURFXML_BUFFER_SET(link_latency_file, "");
1931   SURFXML_BUFFER_SET(link_state_file, "");
1932   SURFXML_START_TAG(link);
1933   SURFXML_END_TAG(link);
1934
1935   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, peer_bw_out, peer_lat);
1936   A_surfxml_link_state = A_surfxml_link_state_ON;
1937   A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1938   SURFXML_BUFFER_SET(link_id, link_id_down);
1939   SURFXML_BUFFER_SET(link_bandwidth, peer_bw_out);
1940   SURFXML_BUFFER_SET(link_latency, peer_lat);
1941   SURFXML_BUFFER_SET(link_bandwidth_file, "");
1942   SURFXML_BUFFER_SET(link_latency_file, "");
1943   SURFXML_BUFFER_SET(link_state_file, "");
1944   SURFXML_START_TAG(link);
1945   SURFXML_END_TAG(link);
1946
1947   XBT_DEBUG(" ");
1948
1949   // begin here
1950   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1951   XBT_DEBUG("symmetrical=\"NO\">");
1952   SURFXML_BUFFER_SET(route_src, host_id);
1953   SURFXML_BUFFER_SET(route_dst, router_id);
1954   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1955   SURFXML_START_TAG(route);
1956
1957   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1958   SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1959   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1960   SURFXML_START_TAG(link_ctn);
1961   SURFXML_END_TAG(link_ctn);
1962
1963   XBT_DEBUG("</route>");
1964   SURFXML_END_TAG(route);
1965
1966   //Opposite Route
1967   XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1968   XBT_DEBUG("symmetrical=\"NO\">");
1969   SURFXML_BUFFER_SET(route_src, router_id);
1970   SURFXML_BUFFER_SET(route_dst, host_id);
1971   A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1972   SURFXML_START_TAG(route);
1973
1974   XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1975   SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1976   A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1977   SURFXML_START_TAG(link_ctn);
1978   SURFXML_END_TAG(link_ctn);
1979
1980   XBT_DEBUG("</route>");
1981   SURFXML_END_TAG(route);
1982
1983   XBT_DEBUG("</AS>");
1984   SURFXML_END_TAG(AS);
1985   XBT_DEBUG(" ");
1986
1987   //xbt_dynar_free(&tab_elements_num);
1988         free(host_id);
1989         free(router_id);
1990         free(link_router);
1991         free(link_backbone);
1992         free(link_id_up);
1993         free(link_id_down);
1994   surfxml_bufferstack_pop(1);
1995 }
1996
1997 static void routing_parse_Srandom(void)
1998 {
1999           double mean, std, min, max, seed;
2000           char *random_id = A_surfxml_random_id;
2001           char *random_radical = A_surfxml_random_radical;
2002           char *rd_name = NULL;
2003           char *rd_value;
2004           surf_parse_get_double(&mean,A_surfxml_random_mean);
2005           surf_parse_get_double(&std,A_surfxml_random_std_deviation);
2006           surf_parse_get_double(&min,A_surfxml_random_min);
2007           surf_parse_get_double(&max,A_surfxml_random_max);
2008           surf_parse_get_double(&seed,A_surfxml_random_seed);
2009
2010           double res = 0;
2011           int i = 0;
2012           random_data_t random = xbt_new0(s_random_data_t, 1);
2013           char *tmpbuf;
2014
2015           xbt_dynar_t radical_elements;
2016           unsigned int iter;
2017           char *groups;
2018           int start, end;
2019           xbt_dynar_t radical_ends;
2020
2021           random->generator = A_surfxml_random_generator;
2022           random->seed = seed;
2023           random->min = min;
2024           random->max = max;
2025
2026           /* Check user stupidities */
2027           if (max < min)
2028             THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
2029           if (mean < min)
2030             THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
2031                    min);
2032           if (mean > max)
2033             THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
2034                    max);
2035
2036           /* normalize the mean and standard deviation before storing */
2037           random->mean = (mean - min) / (max - min);
2038           random->std = std / (max - min);
2039
2040           if (random->mean * (1 - random->mean) < random->std * random->std)
2041             THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
2042                    random->mean, random->std);
2043
2044           XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
2045           random_id,
2046           random->min,
2047           random->max,
2048           random->mean,
2049           random->std,
2050           random->generator,
2051           random->seed,
2052           random_radical);
2053
2054           if(xbt_dict_size(random_value)==0)
2055                   random_value = xbt_dict_new();
2056
2057           if(!strcmp(random_radical,""))
2058           {
2059                   res = random_generate(random);
2060                   rd_value = bprintf("%f",res);
2061                   xbt_dict_set(random_value, random_id, rd_value, free);
2062           }
2063           else
2064           {
2065                   radical_elements = xbt_str_split(random_radical, ",");
2066                   xbt_dynar_foreach(radical_elements, iter, groups) {
2067                         radical_ends = xbt_str_split(groups, "-");
2068                         switch (xbt_dynar_length(radical_ends)) {
2069                         case 1:
2070                                           xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
2071                                           res = random_generate(random);
2072                                           tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
2073                                           xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2074                                           xbt_free(tmpbuf);
2075                                           break;
2076
2077                         case 2:   surf_parse_get_int(&start,
2078                                                                                  xbt_dynar_get_as(radical_ends, 0, char *));
2079                                           surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
2080                                           for (i = start; i <= end; i++) {
2081                                                   xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
2082                                                   res = random_generate(random);
2083                           tmpbuf = bprintf("%s%d",random_id,i);
2084                                                   xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
2085                           xbt_free(tmpbuf);
2086                                           }
2087                                           break;
2088                         default:
2089                                 XBT_INFO("Malformed radical");
2090                                 break;
2091                         }
2092                         res = random_generate(random);
2093                         rd_name  = bprintf("%s_router",random_id);
2094                         rd_value = bprintf("%f",res);
2095                         xbt_dict_set(random_value, rd_name, rd_value, free);
2096
2097                         xbt_dynar_free(&radical_ends);
2098                   }
2099                   free(rd_name);
2100                   xbt_dynar_free(&radical_elements);
2101           }
2102 }
2103
2104 static void routing_parse_Erandom(void)
2105 {
2106         xbt_dict_cursor_t cursor = NULL;
2107         char *key;
2108         char *elem;
2109
2110         xbt_dict_foreach(random_value, cursor, key, elem) {
2111           XBT_DEBUG("%s = %s",key,elem);
2112         }
2113
2114 }
2115
2116
2117 /*
2118  * New methods to init the routing model component from the lua script
2119  */
2120
2121 /*
2122  * calling parse_S_AS_lua with lua values
2123  */
2124 void routing_AS_init(const char *AS_id, const char *AS_routing)
2125 {
2126   parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
2127 }
2128
2129 /*
2130  * calling parse_E_AS_lua to fisnish the creation of routing component
2131  */
2132 void routing_AS_end(const char *AS_id)
2133 {
2134   parse_E_AS_lua((char *) AS_id);
2135 }
2136
2137 /*
2138  * add a host to the network element list
2139  */
2140
2141 void routing_add_host(const char *host_id)
2142 {
2143   parse_S_host_lua((char *) host_id, (char*)""); // FIXME propagate coordinate system to lua
2144 }
2145
2146 /*
2147  * Set a new link on the actual list of link for a route or ASroute
2148  */
2149 void routing_add_link(const char *link_id)
2150 {
2151   parse_E_link_c_ctn_new_elem_lua((char *) link_id);
2152 }
2153
2154 /*
2155  *Set the endpoints for a route
2156  */
2157 void routing_set_route(const char *src_id, const char *dst_id)
2158 {
2159   parse_S_route_new_and_endpoints_lua(src_id, dst_id);
2160 }
2161
2162 /*
2163  * Store the route by calling parse_E_route_store_route
2164  */
2165 void routing_store_route(void)
2166 {
2167   parse_E_route_store_route();
2168 }