Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
acb39d7db52afeee1aacb6fb84f660706a80c678
[simgrid.git] / src / surf / surf_routing.cpp
1 /* Copyright (c) 2009-2011, 2013-2015. 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 "surf_routing.hpp"
8 #include "surf_routing_private.hpp"
9 #include "surf_routing_cluster.hpp"
10
11 #include "simgrid/platf_interface.h"    // platform creation API internal interface
12 #include "simgrid/sg_config.h"
13 #include "storage_interface.hpp"
14 #include "src/surf/platform.hpp"
15 #include "surf/surfxml_parse_values.h"
16 #include "src/surf/surf_routing_vivaldi.hpp"
17
18 /*************
19  * Callbacks *
20  *************/
21
22 namespace simgrid {
23 namespace surf {
24
25 simgrid::xbt::signal<void(simgrid::surf::NetCard*)> netcardCreatedCallbacks;
26 simgrid::xbt::signal<void(simgrid::surf::As*)> asCreatedCallbacks;
27
28 }
29 }
30
31 /**
32  * @ingroup SURF_build_api
33  * @brief A library containing all known hosts
34  */
35 xbt_dict_t host_list;
36
37 int COORD_HOST_LEVEL=0;         //Coordinates level
38
39 int MSG_FILE_LEVEL;             //Msg file level
40
41 int SIMIX_STORAGE_LEVEL;        //Simix storage level
42 int MSG_STORAGE_LEVEL;          //Msg storage level
43
44 xbt_lib_t as_router_lib;
45 int ROUTING_ASR_LEVEL;          //Routing level
46 int COORD_ASR_LEVEL;            //Coordinates level
47 int NS3_ASR_LEVEL;              //host node for ns3
48 int ROUTING_PROP_ASR_LEVEL;     //Where the properties are stored
49
50 /** @brief Retrieve a netcard from its name
51  *
52  * Netcards are the thing that connect host or routers to the network
53  */
54 simgrid::surf::NetCard *sg_netcard_by_name_or_null(const char *name)
55 {
56   sg_host_t h = sg_host_by_name(name);
57   simgrid::surf::NetCard *net_elm = h==NULL?NULL: h->pimpl_netcard;
58   if (!net_elm)
59     net_elm = (simgrid::surf::NetCard*) xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
60   return net_elm;
61 }
62
63 /* Global vars */
64 simgrid::surf::RoutingPlatf *routing_platf = NULL;
65
66 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
67
68 /** The current AS in the parsing */
69 static simgrid::surf::As *current_routing = NULL;
70 simgrid::surf::As* routing_get_current()
71 {
72   return current_routing;
73 }
74
75 /* this lines are only for replace use like index in the model table */
76 typedef enum {
77   SURF_MODEL_FULL = 0,
78   SURF_MODEL_FLOYD,
79   SURF_MODEL_DIJKSTRA,
80   SURF_MODEL_DIJKSTRACACHE,
81   SURF_MODEL_NONE,
82   SURF_MODEL_VIVALDI,
83   SURF_MODEL_CLUSTER,
84   SURF_MODEL_TORUS_CLUSTER,
85   SURF_MODEL_FAT_TREE_CLUSTER,
86 } e_routing_types;
87
88 struct s_model_type routing_models[] = {
89   {model_full_create},
90   {model_floyd_create},
91   {model_dijkstra_create},
92   {model_dijkstracache_create},
93   {model_none_create},
94   {model_vivaldi_create},
95   {model_cluster_create},
96   {model_torus_cluster_create},
97   {model_fat_tree_cluster_create},
98   {NULL}
99 };
100
101 /** @brief Add a link connecting an host to the rest of its AS (which must be cluster or vivaldi) */
102 void sg_platf_new_hostlink(sg_platf_host_link_cbarg_t netcard_arg)
103 {
104   simgrid::surf::NetCard *netcard = sg_host_by_name(netcard_arg->id)->pimpl_netcard;
105   xbt_assert(netcard, "Host '%s' not found!", netcard_arg->id);
106   xbt_assert(dynamic_cast<simgrid::surf::AsCluster*>(current_routing) ||
107              dynamic_cast<simgrid::surf::AsVivaldi*>(current_routing),
108       "Only hosts from Cluster and Vivaldi ASes can get a host_link.");
109
110   s_surf_parsing_link_up_down_t link_up_down;
111   link_up_down.link_up = Link::byName(netcard_arg->link_up);
112   link_up_down.link_down = Link::byName(netcard_arg->link_down);
113
114   xbt_assert(link_up_down.link_up, "Link '%s' not found!",netcard_arg->link_up);
115   xbt_assert(link_up_down.link_down, "Link '%s' not found!",netcard_arg->link_down);
116
117   // If dynar is is greater than netcard id and if the host_link is already defined
118   if((int)xbt_dynar_length(current_routing->p_linkUpDownList) > netcard->getId() &&
119       xbt_dynar_get_as(current_routing->p_linkUpDownList, netcard->getId(), void*))
120   surf_parse_error("Host_link for '%s' is already defined!",netcard_arg->id);
121
122   XBT_DEBUG("Push Host_link for host '%s' to position %d", netcard->getName(), netcard->getId());
123   xbt_dynar_set_as(current_routing->p_linkUpDownList, netcard->getId(), s_surf_parsing_link_up_down_t, link_up_down);
124 }
125
126 void sg_platf_new_trace(sg_platf_trace_cbarg_t trace)
127 {
128   tmgr_trace_t tmgr_trace;
129   if (!trace->file || strcmp(trace->file, "") != 0) {
130     tmgr_trace = tmgr_trace_new_from_file(trace->file);
131   } else {
132     xbt_assert(strcmp(trace->pc_data, ""),
133         "Trace '%s' must have either a content, or point to a file on disk.",trace->id);
134     tmgr_trace = tmgr_trace_new_from_string(trace->id, trace->pc_data, trace->periodicity);
135   }
136   xbt_dict_set(traces_set_list, trace->id, (void *) tmgr_trace, NULL);
137 }
138
139 /**
140  * \brief Make a new routing component to the platform
141  *
142  * Add a new autonomous system to the platform. Any elements (such as host,
143  * router or sub-AS) added after this call and before the corresponding call
144  * to sg_platf_new_AS_close() will be added to this AS.
145  *
146  * Once this function was called, the configuration concerning the used
147  * models cannot be changed anymore.
148  *
149  * @param AS_id name of this autonomous system. Must be unique in the platform
150  * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
151  */
152 void routing_AS_begin(sg_platf_AS_cbarg_t AS)
153 {
154   XBT_DEBUG("routing_AS_begin");
155   routing_model_description_t model = NULL;
156
157   xbt_assert(NULL == xbt_lib_get_or_null(as_router_lib, AS->id, ROUTING_ASR_LEVEL),
158       "Refusing to create a second AS called \"%s\".", AS->id);
159
160   _sg_cfg_init_status = 2; /* horrible hack: direct access to the global
161                             * controlling the level of configuration to prevent
162                             * any further config */
163
164   /* search the routing model */
165   switch(AS->routing){
166     case A_surfxml_AS_routing_Cluster:               model = &routing_models[SURF_MODEL_CLUSTER];break;
167     case A_surfxml_AS_routing_ClusterTorus:    model = &routing_models[SURF_MODEL_TORUS_CLUSTER];break;
168     case A_surfxml_AS_routing_ClusterFatTree:  model = &routing_models[SURF_MODEL_FAT_TREE_CLUSTER];break;
169     case A_surfxml_AS_routing_Dijkstra:              model = &routing_models[SURF_MODEL_DIJKSTRA];break;
170     case A_surfxml_AS_routing_DijkstraCache:         model = &routing_models[SURF_MODEL_DIJKSTRACACHE];break;
171     case A_surfxml_AS_routing_Floyd:                 model = &routing_models[SURF_MODEL_FLOYD];break;
172     case A_surfxml_AS_routing_Full:                  model = &routing_models[SURF_MODEL_FULL];break;
173     case A_surfxml_AS_routing_None:                  model = &routing_models[SURF_MODEL_NONE];break;
174     case A_surfxml_AS_routing_Vivaldi:               model = &routing_models[SURF_MODEL_VIVALDI];break;
175     default: xbt_die("Not a valid model!!!");
176     break;
177   }
178
179   /* make a new routing component */
180   simgrid::surf::As *new_as = model->create();
181
182   new_as->p_hierarchy = SURF_ROUTING_NULL;
183   new_as->p_name = xbt_strdup(AS->id);
184
185   simgrid::surf::NetCard *netcard = new simgrid::surf::NetCardImpl(new_as->p_name, SURF_NETWORK_ELEMENT_AS, current_routing);
186   if (current_routing == NULL && routing_platf->p_root == NULL) {
187
188     /* it is the first one */
189     new_as->p_routingFather = NULL;
190     routing_platf->p_root = new_as;
191     netcard->setId(-1);
192   } else if (current_routing != NULL && routing_platf->p_root != NULL) {
193
194     xbt_assert(!xbt_dict_get_or_null
195                (current_routing->p_routingSons, AS->id),
196                "The AS \"%s\" already exists", AS->id);
197     /* it is a part of the tree */
198     new_as->p_routingFather = current_routing;
199     /* set the father behavior */
200     if (current_routing->p_hierarchy == SURF_ROUTING_NULL)
201       current_routing->p_hierarchy = SURF_ROUTING_RECURSIVE;
202     /* add to the sons dictionary */
203     xbt_dict_set(current_routing->p_routingSons, AS->id,
204                  (void *) new_as, NULL);
205     /* add to the father element list */
206     netcard->setId(current_routing->parseAS(netcard));
207   } else {
208     THROWF(arg_error, 0, "All defined components must belong to a AS");
209   }
210
211   xbt_lib_set(as_router_lib, netcard->getName(), ROUTING_ASR_LEVEL,
212               (void *) netcard);
213   XBT_DEBUG("Having set name '%s' id '%d'", new_as->p_name, netcard->getId());
214
215   /* set the new current component of the tree */
216   current_routing = new_as;
217   current_routing->p_netcard = netcard;
218
219   simgrid::surf::netcardCreatedCallbacks(netcard);
220   simgrid::surf::asCreatedCallbacks(new_as);
221 }
222
223 /**
224  * \brief Specify that the current description of AS is finished
225  *
226  * Once you've declared all the content of your AS, you have to close
227  * it with this call. Your AS is not usable until you call this function.
228  *
229  * @fixme: this call is not as robust as wanted: bad things WILL happen
230  * if you call it twice for the same AS, or if you forget calling it, or
231  * even if you add stuff to a closed AS
232  *
233  */
234 void routing_AS_end()
235 {
236   xbt_assert(current_routing, "Cannot seal the current AS: none under construction");
237   current_routing->Seal();
238   current_routing = current_routing->p_routingFather;
239 }
240
241 /* Aux Business methods */
242
243 /**
244  * \brief Get the AS father and the first elements of the chain
245  *
246  * \param src the source host name
247  * \param dst the destination host name
248  *
249  * Get the common father of the to processing units, and the first different
250  * father in the chain
251  */
252 static void elements_father(sg_netcard_t src, sg_netcard_t dst,
253                             AS_t * res_father,
254                             AS_t * res_src,
255                             AS_t * res_dst)
256 {
257   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
258 #define ELEMENTS_FATHER_MAXDEPTH 16     /* increase if it is not enough */
259   simgrid::surf::As *src_as, *dst_as;
260   simgrid::surf::As *path_src[ELEMENTS_FATHER_MAXDEPTH];
261   simgrid::surf::As *path_dst[ELEMENTS_FATHER_MAXDEPTH];
262   int index_src = 0;
263   int index_dst = 0;
264   simgrid::surf::As *current;
265   simgrid::surf::As *current_src;
266   simgrid::surf::As *current_dst;
267   simgrid::surf::As *father;
268
269   /* (1) find the as where the src and dst are located */
270   sg_netcard_t src_data = src;
271   sg_netcard_t dst_data = dst;
272   src_as = src_data->getRcComponent();
273   dst_as = dst_data->getRcComponent();
274 #ifndef NDEBUG
275   char* src_name = src_data->getName();
276   char* dst_name = dst_data->getName();
277 #endif
278
279   xbt_assert(src_as && dst_as,
280              "Ask for route \"from\"(%s) or \"to\"(%s) no found", src_name, dst_name);
281
282   /* (2) find the path to the root routing component */
283   for (current = src_as; current != NULL; current = current->p_routingFather) {
284     if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
285       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
286     path_src[index_src++] = current;
287   }
288   for (current = dst_as; current != NULL; current = current->p_routingFather) {
289     if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
290       xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
291     path_dst[index_dst++] = current;
292   }
293
294   /* (3) find the common father */
295   do {
296     current_src = path_src[--index_src];
297     current_dst = path_dst[--index_dst];
298   } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
299
300   /* (4) they are not in the same routing component, make the path */
301   if (current_src == current_dst)
302     father = current_src;
303   else
304     father = path_src[index_src + 1];
305
306   /* (5) result generation */
307   *res_father = father;         /* first the common father of src and dst */
308   *res_src = current_src;       /* second the first different father of src */
309   *res_dst = current_dst;       /* three  the first different father of dst */
310
311 #undef ELEMENTS_FATHER_MAXDEPTH
312 }
313
314 /* Global Business methods */
315
316 /**
317  * \brief Recursive function for get_route_latency
318  *
319  * \param src the source host name
320  * \param dst the destination host name
321  * \param *route the route where the links are stored. It is either NULL or a ready to use dynar
322  * \param *latency the latency, if needed
323  *
324  * This function is called by "get_route" and "get_latency". It allows to walk
325  * recursively through the ASes tree.
326  */
327 static void _get_route_and_latency(
328   simgrid::surf::NetCard *src, simgrid::surf::NetCard *dst,
329   xbt_dynar_t * links, double *latency)
330 {
331   s_sg_platf_route_cbarg_t route = SG_PLATF_ROUTE_INITIALIZER;
332   memset(&route,0,sizeof(route));
333
334   xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
335   XBT_DEBUG("Solve route/latency  \"%s\" to \"%s\"", src->getName(), dst->getName());
336
337   /* Find how src and dst are interconnected */
338   simgrid::surf::As *common_father, *src_father, *dst_father;
339   elements_father(src, dst, &common_father, &src_father, &dst_father);
340   XBT_DEBUG("elements_father: common father '%s' src_father '%s' dst_father '%s'",
341       common_father->p_name, src_father->p_name, dst_father->p_name);
342
343   /* Check whether a direct bypass is defined */
344   sg_platf_route_cbarg_t e_route_bypass = NULL;
345   //FIXME:REMOVE:if (common_father->get_bypass_route)
346
347   e_route_bypass = common_father->getBypassRoute(src, dst, latency);
348
349   /* Common ancestor is kind enough to declare a bypass route from src to dst -- use it and bail out */
350   if (e_route_bypass) {
351     xbt_dynar_merge(links, &e_route_bypass->link_list);
352     routing_route_free(e_route_bypass);
353     return;
354   }
355
356   /* If src and dst are in the same AS, life is good */
357   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
358     route.link_list = *links;
359     common_father->getRouteAndLatency(src, dst, &route, latency);
360     // if vivaldi latency+=vivaldi(src,dst)
361     return;
362   }
363
364   /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
365
366   route.link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
367   // Find the net_card corresponding to father
368   simgrid::surf::NetCard *src_father_netcard = src_father->p_netcard;
369   simgrid::surf::NetCard *dst_father_netcard = dst_father->p_netcard;
370
371   common_father->getRouteAndLatency(src_father_netcard, dst_father_netcard,
372                                     &route, latency);
373
374   xbt_assert((route.gw_src != NULL) && (route.gw_dst != NULL),
375       "bad gateways for route from \"%s\" to \"%s\"", src->getName(), dst->getName());
376
377   sg_netcard_t src_gateway_net_elm = route.gw_src;
378   sg_netcard_t dst_gateway_net_elm = route.gw_dst;
379
380   /* If source gateway is not our source, we have to recursively find our way up to this point */
381   if (src != src_gateway_net_elm)
382     _get_route_and_latency(src, src_gateway_net_elm, links, latency);
383   xbt_dynar_merge(links, &route.link_list);
384
385   /* If dest gateway is not our destination, we have to recursively find our way from this point */
386   if (dst_gateway_net_elm != dst)
387     _get_route_and_latency(dst_gateway_net_elm, dst, links, latency);
388
389 }
390
391 AS_t surf_platf_get_root(routing_platf_t platf){
392   return platf->p_root;
393 }
394
395 e_surf_network_element_type_t surf_routing_edge_get_rc_type(sg_netcard_t netcard){
396   return netcard->getRcType();
397 }
398
399 namespace simgrid {
400 namespace surf {
401
402 /**
403  * \brief Find a route between hosts
404  *
405  * \param src the network_element_t for src host
406  * \param dst the network_element_t for dst host
407  * \param route where to store the list of links.
408  *              If *route=NULL, create a short lived dynar. Else, fill the provided dynar
409  * \param latency where to store the latency experienced on the path (or NULL if not interested)
410  *                It is the caller responsability to initialize latency to 0 (we add to provided route)
411  * \pre route!=NULL
412  *
413  * walk through the routing components tree and find a route between hosts
414  * by calling each "get_route" function in each routing component.
415  */
416 void RoutingPlatf::getRouteAndLatency(NetCard *src, NetCard *dst, xbt_dynar_t* route, double *latency)
417 {
418   XBT_DEBUG("getRouteAndLatency from %s to %s", src->getName(), dst->getName());
419   if (NULL == *route) {
420     xbt_dynar_reset(routing_platf->p_lastRoute);
421     *route = routing_platf->p_lastRoute;
422   }
423
424   _get_route_and_latency(src, dst, route, latency);
425
426   xbt_assert(!latency || *latency >= 0.0,
427              "negative latency on route between \"%s\" and \"%s\"", src->getName(), dst->getName());
428 }
429
430 xbt_dynar_t RoutingPlatf::getOneLinkRoutes(){
431   return recursiveGetOneLinkRoutes(p_root);
432 }
433
434 xbt_dynar_t RoutingPlatf::recursiveGetOneLinkRoutes(As *rc)
435 {
436   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
437
438   //adding my one link routes
439   xbt_dynar_t onelink_mine = rc->getOneLinkRoutes();
440   if (onelink_mine)
441     xbt_dynar_merge(&ret,&onelink_mine);
442
443   //recursing
444   char *key;
445   xbt_dict_cursor_t cursor = NULL;
446   AS_t rc_child;
447   xbt_dict_foreach(rc->p_routingSons, cursor, key, rc_child) {
448     xbt_dynar_t onelink_child = recursiveGetOneLinkRoutes(rc_child);
449     if (onelink_child)
450       xbt_dynar_merge(&ret,&onelink_child);
451   }
452   return ret;
453 }
454
455 }
456 }
457
458 e_surf_network_element_type_t routing_get_network_element_type(const char *name)
459 {
460   simgrid::surf::NetCard *rc = sg_netcard_by_name_or_null(name);
461   if (rc)
462     return rc->getRcType();
463
464   return SURF_NETWORK_ELEMENT_NULL;
465 }
466
467 /** @brief create the root AS */
468 void routing_model_create( void *loopback)
469 {
470   routing_platf = new simgrid::surf::RoutingPlatf(loopback);
471 }
472
473 /* ************************************************************************** */
474 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
475
476 void routing_cluster_add_backbone(void* bb) {
477   simgrid::surf::AsCluster *cluster = dynamic_cast<simgrid::surf::AsCluster*>(current_routing);
478
479   xbt_assert(cluster, "Only hosts from Cluster can get a backbone.");
480   xbt_assert(nullptr == cluster->p_backbone, "Cluster %s already has a backbone link!", cluster->p_name);
481
482   cluster->p_backbone = static_cast<simgrid::surf::Link*>(bb);
483   XBT_DEBUG("Add a backbone to AS '%s'", current_routing->p_name);
484 }
485
486 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
487 {
488   int start, end, i;
489   char *groups , *host_id , *link_id = NULL;
490   unsigned int iter;
491   xbt_dynar_t radical_elements;
492   xbt_dynar_t radical_ends;
493
494   //Make all hosts
495   radical_elements = xbt_str_split(cabinet->radical, ",");
496   xbt_dynar_foreach(radical_elements, iter, groups) {
497
498     radical_ends = xbt_str_split(groups, "-");
499     start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
500
501     switch (xbt_dynar_length(radical_ends)) {
502     case 1:
503       end = start;
504       break;
505     case 2:
506       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
507       break;
508     default:
509       surf_parse_error("Malformed radical");
510       break;
511     }
512     s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
513     memset(&host, 0, sizeof(host));
514     host.initiallyOn   = 1;
515     host.pstate        = 0;
516     host.speed_scale   = 1.0;
517     host.core_amount   = 1;
518
519     s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
520     memset(&link, 0, sizeof(link));
521     link.initiallyOn = 1;
522     link.policy    = SURF_LINK_FULLDUPLEX;
523     link.latency   = cabinet->lat;
524     link.bandwidth = cabinet->bw;
525
526     s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
527     memset(&host_link, 0, sizeof(host_link));
528
529     for (i = start; i <= end; i++) {
530       host_id                      = bprintf("%s%d%s",cabinet->prefix,i,cabinet->suffix);
531       link_id                      = bprintf("link_%s%d%s",cabinet->prefix,i,cabinet->suffix);
532       host.id                      = host_id;
533       link.id                      = link_id;
534       host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
535       xbt_dynar_push(host.speed_peak,&cabinet->speed);
536       sg_platf_new_host(&host);
537       xbt_dynar_free(&host.speed_peak);
538       sg_platf_new_link(&link);
539
540       char* link_up       = bprintf("%s_UP",link_id);
541       char* link_down     = bprintf("%s_DOWN",link_id);
542       host_link.id        = host_id;
543       host_link.link_up   = link_up;
544       host_link.link_down = link_down;
545       sg_platf_new_hostlink(&host_link);
546
547       free(host_id);
548       free(link_id);
549       free(link_up);
550       free(link_down);
551     }
552
553     xbt_dynar_free(&radical_ends);
554   }
555   xbt_dynar_free(&radical_elements);
556 }
557
558 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
559 {
560   using simgrid::surf::NetCard;
561   using simgrid::surf::AsCluster;
562
563   char *host_id = NULL;
564   char *link_id = NULL;
565   char *router_id = NULL;
566
567   XBT_DEBUG(" ");
568   host_id = HOST_PEER(peer->id);
569   link_id = LINK_PEER(peer->id);
570   router_id = ROUTER_PEER(peer->id);
571
572   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
573   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
574   AS.id                    = peer->id;
575   AS.routing               = A_surfxml_AS_routing_Cluster;
576   sg_platf_new_AS_begin(&AS);
577
578   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->speed);
579   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
580   memset(&host, 0, sizeof(host));
581   host.initiallyOn = 1;
582   host.id = host_id;
583
584   host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
585   xbt_dynar_push(host.speed_peak,&peer->speed);
586   host.pstate = 0;
587   //host.power_peak = peer->power;
588   host.speed_scale = 1.0;
589   host.speed_trace = peer->availability_trace;
590   host.state_trace = peer->state_trace;
591   host.core_amount = 1;
592   sg_platf_new_host(&host);
593   xbt_dynar_free(&host.speed_peak);
594
595   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
596   memset(&link, 0, sizeof(link));
597   link.initiallyOn = 1;
598   link.policy  = SURF_LINK_SHARED;
599   link.latency = peer->lat;
600
601   char* link_up = bprintf("%s_UP",link_id);
602   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_up,
603             peer->bw_out, peer->lat);
604   link.id = link_up;
605   link.bandwidth = peer->bw_out;
606   sg_platf_new_link(&link);
607
608   char* link_down = bprintf("%s_DOWN",link_id);
609   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_down,
610             peer->bw_in, peer->lat);
611   link.id = link_down;
612   link.bandwidth = peer->bw_in;
613   sg_platf_new_link(&link);
614
615   XBT_DEBUG("<host_link\tid=\"%s\"\tup=\"%s\"\tdown=\"%s\" />", host_id,link_up,link_down);
616   s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
617   memset(&host_link, 0, sizeof(host_link));
618   host_link.id        = host_id;
619   host_link.link_up   = link_up;
620   host_link.link_down = link_down;
621   sg_platf_new_hostlink(&host_link);
622
623   XBT_DEBUG("<router id=\"%s\"/>", router_id);
624   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
625   memset(&router, 0, sizeof(router));
626   router.id = router_id;
627   router.coord = peer->coord;
628   sg_platf_new_router(&router);
629   static_cast<AsCluster*>(current_routing)->p_router = static_cast<NetCard*>(xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL));
630
631   XBT_DEBUG("</AS>");
632   sg_platf_new_AS_end();
633   XBT_DEBUG(" ");
634
635   //xbt_dynar_free(&tab_elements_num);
636   free(router_id);
637   free(host_id);
638   free(link_id);
639   free(link_up);
640   free(link_down);
641 }
642
643 // static void routing_parse_Srandom(void)
644 // {
645 //   double mean, std, min, max, seed;
646 //   char *random_id = A_surfxml_random_id;
647 //   char *random_radical = A_surfxml_random_radical;
648 //   char *rd_name = NULL;
649 //   char *rd_value;
650 //   mean = surf_parse_get_double(A_surfxml_random_mean);
651 //   std = surf_parse_get_double(A_surfxml_random_std___deviation);
652 //   min = surf_parse_get_double(A_surfxml_random_min);
653 //   max = surf_parse_get_double(A_surfxml_random_max);
654 //   seed = surf_parse_get_double(A_surfxml_random_seed);
655
656 //   double res = 0;
657 //   int i = 0;
658 //   random_data_t random = xbt_new0(s_random_data_t, 1);
659 //   char *tmpbuf;
660
661 //   xbt_dynar_t radical_elements;
662 //   unsigned int iter;
663 //   char *groups;
664 //   int start, end;
665 //   xbt_dynar_t radical_ends;
666
667 //   switch (A_surfxml_random_generator) {
668 //   case AU_surfxml_random_generator:
669 //   case A_surfxml_random_generator_NONE:
670 //     random->generator = NONE;
671 //     break;
672 //   case A_surfxml_random_generator_DRAND48:
673 //     random->generator = DRAND48;
674 //     break;
675 //   case A_surfxml_random_generator_RAND:
676 //     random->generator = RAND;
677 //     break;
678 //   case A_surfxml_random_generator_RNGSTREAM:
679 //     random->generator = RNGSTREAM;
680 //     break;
681 //   default:
682 //     surf_parse_error("Invalid random generator");
683 //     break;
684 //   }
685 //   random->seed = seed;
686 //   random->min = min;
687 //   random->max = max;
688
689 //   /* Check user stupidities */
690 //   if (max < min)
691 //     THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
692 //   if (mean < min)
693 //     THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean, min);
694 //   if (mean > max)
695 //     THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean, max);
696
697 //   /* normalize the mean and standard deviation before storing */
698 //   random->mean = (mean - min) / (max - min);
699 //   random->std = std / (max - min);
700
701 //   if (random->mean * (1 - random->mean) < random->std * random->std)
702 //     THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
703 //            random->mean, random->std);
704
705 //   XBT_DEBUG
706 //       ("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
707 //        random_id, random->min, random->max, random->mean, random->std,
708 //        (int)random->generator, random->seed, random_radical);
709
710 //   if (!random_value)
711 //     random_value = xbt_dict_new_homogeneous(free);
712
713 //   if (!strcmp(random_radical, "")) {
714 //     res = random_generate(random);
715 //     rd_value = bprintf("%f", res);
716 //     xbt_dict_set(random_value, random_id, rd_value, NULL);
717 //   } else {
718 //     radical_elements = xbt_str_split(random_radical, ",");
719 //     xbt_dynar_foreach(radical_elements, iter, groups) {
720 //       radical_ends = xbt_str_split(groups, "-");
721 //       switch (xbt_dynar_length(radical_ends)) {
722 //       case 1:
723 //         xbt_assert(!xbt_dict_get_or_null(random_value, random_id),
724 //                    "Custom Random '%s' already exists !", random_id);
725 //         res = random_generate(random);
726 //         tmpbuf =
727 //             bprintf("%s%d", random_id,
728 //                     atoi(xbt_dynar_getfirst_as(radical_ends, char *)));
729 //         xbt_dict_set(random_value, tmpbuf, bprintf("%f", res), NULL);
730 //         xbt_free(tmpbuf);
731 //         break;
732
733 //       case 2:
734 //         start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
735 //         end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
736 //         for (i = start; i <= end; i++) {
737 //           xbt_assert(!xbt_dict_get_or_null(random_value, random_id),
738 //                      "Custom Random '%s' already exists !", bprintf("%s%d",
739 //                                                                     random_id,
740 //                                                                     i));
741 //           res = random_generate(random);
742 //           tmpbuf = bprintf("%s%d", random_id, i);
743 //           xbt_dict_set(random_value, tmpbuf, bprintf("%f", res), NULL);
744 //           xbt_free(tmpbuf);
745 //         }
746 //         break;
747 //       default:
748 //         XBT_CRITICAL("Malformed radical");
749 //         break;
750 //       }
751 //       res = random_generate(random);
752 //       rd_name = bprintf("%s_router", random_id);
753 //       rd_value = bprintf("%f", res);
754 //       xbt_dict_set(random_value, rd_name, rd_value, NULL);
755
756 //       xbt_dynar_free(&radical_ends);
757 //     }
758 //     free(rd_name);
759 //     xbt_dynar_free(&radical_elements);
760 //   }
761 // }
762
763 static void check_disk_attachment()
764 {
765   xbt_lib_cursor_t cursor;
766   char *key;
767   void **data;
768   simgrid::surf::NetCard *host_elm;
769   xbt_lib_foreach(storage_lib, cursor, key, data) {
770     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
771     simgrid::surf::Storage *storage = static_cast<simgrid::surf::Storage*>(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL));
772     host_elm = sg_netcard_by_name_or_null(storage->p_attach);
773     if(!host_elm)
774       surf_parse_error("Unable to attach storage %s: host %s doesn't exist.", storage->getName(), storage->p_attach);
775     }
776   }
777 }
778
779 void routing_register_callbacks()
780 {
781   simgrid::surf::on_postparse.connect(check_disk_attachment);
782
783   instr_routing_define_callbacks();
784 }
785
786 /**
787  * \brief Recursive function for finalize
788  *
789  * \param rc the source host name
790  *
791  * This fuction is call by "finalize". It allow to finalize the
792  * AS or routing components. It delete all the structures.
793  */
794 static void finalize_rec(simgrid::surf::As *as) {
795   xbt_dict_cursor_t cursor = NULL;
796   char *key;
797   AS_t elem;
798
799   xbt_dict_foreach(as->p_routingSons, cursor, key, elem) {
800     finalize_rec(elem);
801   }
802
803   delete as;;
804 }
805
806 /** \brief Frees all memory allocated by the routing module */
807 void routing_exit(void) {
808   delete routing_platf;
809 }
810
811 namespace simgrid {
812 namespace surf {
813
814   RoutingPlatf::RoutingPlatf(void *loopback)
815   : p_loopback(loopback)
816   {
817   }
818   RoutingPlatf::~RoutingPlatf()
819   {
820     xbt_dynar_free(&p_lastRoute);
821     finalize_rec(p_root);
822   }
823
824 }
825 }
826
827 AS_t surf_AS_get_routing_root() {
828   return routing_platf->p_root;
829 }
830
831 const char *surf_AS_get_name(simgrid::surf::As *as) {
832   return as->p_name;
833 }
834
835 static simgrid::surf::As *surf_AS_recursive_get_by_name(
836   simgrid::surf::As *current, const char * name)
837 {
838   xbt_dict_cursor_t cursor = NULL;
839   char *key;
840   AS_t elem;
841   simgrid::surf::As *tmp = NULL;
842
843   if(!strcmp(current->p_name, name))
844     return current;
845
846   xbt_dict_foreach(current->p_routingSons, cursor, key, elem) {
847     tmp = surf_AS_recursive_get_by_name(elem, name);
848     if(tmp != NULL ) {
849         break;
850     }
851   }
852   return tmp;
853 }
854
855 simgrid::surf::As *surf_AS_get_by_name(const char * name)
856 {
857   simgrid::surf::As *as = surf_AS_recursive_get_by_name(routing_platf->p_root, name);
858   if(as == NULL)
859     XBT_WARN("Impossible to find an AS with name %s, please check your input", name);
860   return as;
861 }
862
863 xbt_dict_t surf_AS_get_routing_sons(simgrid::surf::As *as)
864 {
865   return as->p_routingSons;
866 }
867
868 xbt_dynar_t surf_AS_get_hosts(simgrid::surf::As *as)
869 {
870   xbt_dynar_t elms = as->p_indexNetworkElm;
871   int count = xbt_dynar_length(elms);
872   xbt_dynar_t res =  xbt_dynar_new(sizeof(sg_host_t), NULL);
873   for (int index = 0; index < count; index++) {
874      sg_netcard_t relm =
875       xbt_dynar_get_as(elms, index, simgrid::surf::NetCard*);
876      sg_host_t delm = simgrid::s4u::Host::by_name_or_null(relm->getName());
877      if (delm!=NULL) {
878        xbt_dynar_push(res, &delm);
879      }
880   }
881   return res;
882 }
883
884 void surf_AS_get_graph(AS_t as, xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges) {
885   as->getGraph(graph, nodes, edges);
886 }