]> AND Public Git Repository - simgrid.git/blob - src/surf/surf_routing.c
Logo AND Algorithmique Numérique Distribuée

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