Logo AND Algorithmique Numérique Distribuée

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