Logo AND Algorithmique Numérique Distribuée

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