Logo AND Algorithmique Numérique Distribuée

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