Logo AND Algorithmique Numérique Distribuée

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