Logo AND Algorithmique Numérique Distribuée

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