Logo AND Algorithmique Numérique Distribuée

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