Logo AND Algorithmique Numérique Distribuée

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