Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move one method higher in As hierarchy
[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 *net_elm = h==NULL?NULL: h->pimpl_netcard;
98   if (!net_elm)
99     net_elm = (simgrid::surf::NetCard*) xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
100   return net_elm;
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(NULL == 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,
218               (void *) netcard);
219   XBT_DEBUG("Having set name '%s' id '%d'", new_as->name_, netcard->id());
220
221   /* set the new current component of the tree */
222   current_routing = new_as;
223   current_routing->netcard_ = netcard;
224
225   simgrid::surf::netcardCreatedCallbacks(netcard);
226   simgrid::surf::asCreatedCallbacks(new_as);
227 }
228
229 /**
230  * \brief Specify that the current description of AS is finished
231  *
232  * Once you've declared all the content of your AS, you have to close
233  * it with this call. Your AS is not usable until you call this function.
234  *
235  * @fixme: this call is not as robust as wanted: bad things WILL happen
236  * if you call it twice for the same AS, or if you forget calling it, or
237  * even if you add stuff to a closed AS
238  *
239  */
240 void routing_AS_end()
241 {
242   xbt_assert(current_routing, "Cannot seal the current AS: none under construction");
243   current_routing->Seal();
244   current_routing = current_routing->father_;
245 }
246
247 /* Aux Business methods */
248
249 /**
250  * \brief Get the AS father and the first elements of the chain
251  *
252  * \param src the source host name
253  * \param dst the destination host name
254  *
255  * Get the common father of the to processing units, and the first different
256  * father in the chain
257  */
258 static void elements_father(sg_netcard_t src, sg_netcard_t dst,
259                             AS_t * res_father,
260                             AS_t * res_src,
261                             AS_t * res_dst)
262 {
263   xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
264 #define ROUTING_HIERARCHY_MAXDEPTH 16     /* increase if it is not enough */
265   simgrid::surf::As *path_src[ROUTING_HIERARCHY_MAXDEPTH];
266   simgrid::surf::As *path_dst[ROUTING_HIERARCHY_MAXDEPTH];
267   int index_src = 0;
268   int index_dst = 0;
269   simgrid::surf::As *current_src;
270   simgrid::surf::As *current_dst;
271   simgrid::surf::As *father;
272
273   /* (1) find the path to root of src and dst*/
274   simgrid::surf::As *src_as = src->containingAS();
275   simgrid::surf::As *dst_as = dst->containingAS();
276
277   xbt_assert(src_as, "Host %s must be in an AS", src->name());
278   xbt_assert(dst_as, "Host %s must be in an AS", dst->name());
279
280   /* (2) find the path to the root routing component */
281   for (simgrid::surf::As *current = src_as; current != NULL; current = current->father_) {
282     if (index_src >= ROUTING_HIERARCHY_MAXDEPTH)
283       xbt_die("ROUTING_HIERARCHY_MAXDEPTH should be increased for element %s", src->name());
284     path_src[index_src++] = current;
285   }
286   for (simgrid::surf::As *current = dst_as; current != NULL; current = current->father_) {
287     if (index_dst >= ROUTING_HIERARCHY_MAXDEPTH)
288       xbt_die("ROUTING_HIERARCHY_MAXDEPTH should be increased for path_dst");
289     path_dst[index_dst++] = current;
290   }
291
292   /* (3) find the common father */
293   do {
294     current_src = path_src[--index_src];
295     current_dst = path_dst[--index_dst];
296   } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
297
298   /* (4) they are not in the same routing component, make the path */
299   if (current_src == current_dst)
300     father = current_src;
301   else
302     father = path_src[index_src + 1];
303
304   /* (5) result generation */
305   *res_father = father;         /* first the common father of src and dst */
306   *res_src = current_src;       /* second the first different father of src */
307   *res_dst = current_dst;       /* three  the first different father of dst */
308
309 #undef ROUTING_HIERARCHY_MAXDEPTH
310 }
311
312 /**
313  * \brief Recursive function for get_route_and_latency
314  *
315  * \param src the source host name
316  * \param dst the destination host name
317  * \param *route the route where the links are stored. It is either NULL or a ready to use dynar
318  * \param *latency the latency, if needed
319  *
320  * This function is called by "get_route" and "get_latency". It allows to walk recursively through the ASes tree.
321  */
322 static void _get_route_and_latency(simgrid::surf::NetCard *src, simgrid::surf::NetCard *dst,
323   xbt_dynar_t * links, double *latency)
324 {
325   s_sg_platf_route_cbarg_t route = SG_PLATF_ROUTE_INITIALIZER;
326   memset(&route,0,sizeof(route));
327
328   xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
329   XBT_DEBUG("Solve route/latency  \"%s\" to \"%s\"", src->name(), dst->name());
330
331   /* Find how src and dst are interconnected */
332   simgrid::surf::As *common_father, *src_father, *dst_father;
333   elements_father(src, dst, &common_father, &src_father, &dst_father);
334   XBT_DEBUG("elements_father: common father '%s' src_father '%s' dst_father '%s'",
335       common_father->name_, src_father->name_, dst_father->name_);
336
337   /* Check whether a direct bypass is defined. If so, use it and bail out */
338   sg_platf_route_cbarg_t bypassed_route = common_father->getBypassRoute(src, dst, latency);
339   if (bypassed_route) {
340     xbt_dynar_merge(links, &bypassed_route->link_list);
341     routing_route_free(bypassed_route);
342     return;
343   }
344
345   /* If src and dst are in the same AS, life is good */
346   if (src_father == dst_father) {       /* SURF_ROUTING_BASE */
347     route.link_list = *links;
348     common_father->getRouteAndLatency(src, dst, &route, latency);
349     return;
350   }
351
352   /* Not in the same AS, no bypass. We'll have to find our path between the ASes recursively*/
353
354   route.link_list = xbt_dynar_new(sizeof(Link*), NULL);
355
356   common_father->getRouteAndLatency(src_father->netcard_, dst_father->netcard_, &route, latency);
357   xbt_assert((route.gw_src != NULL) && (route.gw_dst != NULL),
358       "bad gateways for route from \"%s\" to \"%s\"", src->name(), dst->name());
359
360   /* If source gateway is not our source, we have to recursively find our way up to this point */
361   if (src != route.gw_src)
362     _get_route_and_latency(src, route.gw_src, links, latency);
363   xbt_dynar_merge(links, &route.link_list);
364
365   /* If dest gateway is not our destination, we have to recursively find our way from this point */
366   if (route.gw_dst != dst)
367     _get_route_and_latency(route.gw_dst, dst, links, latency);
368
369 }
370
371 AS_t surf_platf_get_root(routing_platf_t platf){
372   return platf->root_;
373 }
374
375 e_surf_network_element_type_t surf_routing_edge_get_rc_type(sg_netcard_t netcard){
376   return netcard->getRcType();
377 }
378
379 namespace simgrid {
380 namespace surf {
381
382 /**
383  * \brief Find a route between hosts
384  *
385  * \param src the network_element_t for src host
386  * \param dst the network_element_t for dst host
387  * \param route where to store the list of links.
388  *              If *route=NULL, create a short lived dynar. Else, fill the provided dynar
389  * \param latency where to store the latency experienced on the path (or NULL if not interested)
390  *                It is the caller responsability to initialize latency to 0 (we add to provided route)
391  * \pre route!=NULL
392  *
393  * walk through the routing components tree and find a route between hosts
394  * by calling each "get_route" function in each routing component.
395  */
396 void RoutingPlatf::getRouteAndLatency(NetCard *src, NetCard *dst, xbt_dynar_t* route, double *latency)
397 {
398   XBT_DEBUG("getRouteAndLatency from %s to %s", src->name(), dst->name());
399   if (NULL == *route) {
400     xbt_dynar_reset(routing_platf->lastRoute_);
401     *route = routing_platf->lastRoute_;
402   }
403
404   _get_route_and_latency(src, dst, route, latency);
405 }
406
407 static xbt_dynar_t _recursiveGetOneLinkRoutes(As *rc)
408 {
409   xbt_dynar_t ret = xbt_dynar_new(sizeof(Onelink*), xbt_free_f);
410
411   //adding my one link routes
412   xbt_dynar_t onelink_mine = rc->getOneLinkRoutes();
413   if (onelink_mine)
414     xbt_dynar_merge(&ret,&onelink_mine);
415
416   //recursing
417   char *key;
418   xbt_dict_cursor_t cursor = NULL;
419   AS_t rc_child;
420   xbt_dict_foreach(rc->sons_, cursor, key, rc_child) {
421     xbt_dynar_t onelink_child = _recursiveGetOneLinkRoutes(rc_child);
422     if (onelink_child)
423       xbt_dynar_merge(&ret,&onelink_child);
424   }
425   return ret;
426 }
427
428 xbt_dynar_t RoutingPlatf::getOneLinkRoutes(){
429   return _recursiveGetOneLinkRoutes(root_);
430 }
431
432 }
433 }
434
435 e_surf_network_element_type_t routing_get_network_element_type(const char *name)
436 {
437   simgrid::surf::NetCard *rc = sg_netcard_by_name_or_null(name);
438   if (rc)
439     return rc->getRcType();
440
441   return SURF_NETWORK_ELEMENT_NULL;
442 }
443
444 /** @brief create the root AS */
445 void routing_model_create( void *loopback)
446 {
447   routing_platf = new simgrid::surf::RoutingPlatf(loopback);
448 }
449
450 /* ************************************************************************** */
451 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
452
453 void routing_cluster_add_backbone(simgrid::surf::Link* bb) {
454   simgrid::surf::AsCluster *cluster = dynamic_cast<simgrid::surf::AsCluster*>(current_routing);
455
456   xbt_assert(cluster, "Only hosts from Cluster can get a backbone.");
457   xbt_assert(nullptr == cluster->backbone_, "Cluster %s already has a backbone link!", cluster->name_);
458
459   cluster->backbone_ = bb;
460   XBT_DEBUG("Add a backbone to AS '%s'", current_routing->name_);
461 }
462
463 void sg_platf_new_cabinet(sg_platf_cabinet_cbarg_t cabinet)
464 {
465   int start, end, i;
466   char *groups , *host_id , *link_id = NULL;
467   unsigned int iter;
468   xbt_dynar_t radical_elements;
469   xbt_dynar_t radical_ends;
470
471   //Make all hosts
472   radical_elements = xbt_str_split(cabinet->radical, ",");
473   xbt_dynar_foreach(radical_elements, iter, groups) {
474
475     radical_ends = xbt_str_split(groups, "-");
476     start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
477
478     switch (xbt_dynar_length(radical_ends)) {
479     case 1:
480       end = start;
481       break;
482     case 2:
483       end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
484       break;
485     default:
486       surf_parse_error("Malformed radical");
487       break;
488     }
489     s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
490     memset(&host, 0, sizeof(host));
491     host.initiallyOn   = 1;
492     host.pstate        = 0;
493     host.speed_scale   = 1.0;
494     host.core_amount   = 1;
495
496     s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
497     memset(&link, 0, sizeof(link));
498     link.initiallyOn = 1;
499     link.policy    = SURF_LINK_FULLDUPLEX;
500     link.latency   = cabinet->lat;
501     link.bandwidth = cabinet->bw;
502
503     s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
504     memset(&host_link, 0, sizeof(host_link));
505
506     for (i = start; i <= end; i++) {
507       host_id                      = bprintf("%s%d%s",cabinet->prefix,i,cabinet->suffix);
508       link_id                      = bprintf("link_%s%d%s",cabinet->prefix,i,cabinet->suffix);
509       host.id                      = host_id;
510       link.id                      = link_id;
511       host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
512       xbt_dynar_push(host.speed_peak,&cabinet->speed);
513       sg_platf_new_host(&host);
514       xbt_dynar_free(&host.speed_peak);
515       sg_platf_new_link(&link);
516
517       char* link_up       = bprintf("%s_UP",link_id);
518       char* link_down     = bprintf("%s_DOWN",link_id);
519       host_link.id        = host_id;
520       host_link.link_up   = link_up;
521       host_link.link_down = link_down;
522       sg_platf_new_hostlink(&host_link);
523
524       free(host_id);
525       free(link_id);
526       free(link_up);
527       free(link_down);
528     }
529
530     xbt_dynar_free(&radical_ends);
531   }
532   xbt_dynar_free(&radical_elements);
533 }
534
535 void sg_platf_new_peer(sg_platf_peer_cbarg_t peer)
536 {
537   using simgrid::surf::NetCard;
538   using simgrid::surf::AsCluster;
539
540   char *host_id = NULL;
541   char *link_id = NULL;
542   char *router_id = NULL;
543
544   XBT_DEBUG(" ");
545   host_id = HOST_PEER(peer->id);
546   link_id = LINK_PEER(peer->id);
547   router_id = ROUTER_PEER(peer->id);
548
549   XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", peer->id);
550   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
551   AS.id                    = peer->id;
552   AS.routing               = A_surfxml_AS_routing_Cluster;
553   sg_platf_new_AS_begin(&AS);
554
555   XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->speed);
556   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
557   memset(&host, 0, sizeof(host));
558   host.initiallyOn = 1;
559   host.id = host_id;
560
561   host.speed_peak = xbt_dynar_new(sizeof(double), NULL);
562   xbt_dynar_push(host.speed_peak,&peer->speed);
563   host.pstate = 0;
564   //host.power_peak = peer->power;
565   host.speed_scale = 1.0;
566   host.speed_trace = peer->availability_trace;
567   host.state_trace = peer->state_trace;
568   host.core_amount = 1;
569   sg_platf_new_host(&host);
570   xbt_dynar_free(&host.speed_peak);
571
572   s_sg_platf_link_cbarg_t link = SG_PLATF_LINK_INITIALIZER;
573   memset(&link, 0, sizeof(link));
574   link.initiallyOn = 1;
575   link.policy  = SURF_LINK_SHARED;
576   link.latency = peer->lat;
577
578   char* link_up = bprintf("%s_UP",link_id);
579   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_up,
580             peer->bw_out, peer->lat);
581   link.id = link_up;
582   link.bandwidth = peer->bw_out;
583   sg_platf_new_link(&link);
584
585   char* link_down = bprintf("%s_DOWN",link_id);
586   XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_down,
587             peer->bw_in, peer->lat);
588   link.id = link_down;
589   link.bandwidth = peer->bw_in;
590   sg_platf_new_link(&link);
591
592   XBT_DEBUG("<host_link\tid=\"%s\"\tup=\"%s\"\tdown=\"%s\" />", host_id,link_up,link_down);
593   s_sg_platf_host_link_cbarg_t host_link = SG_PLATF_HOST_LINK_INITIALIZER;
594   memset(&host_link, 0, sizeof(host_link));
595   host_link.id        = host_id;
596   host_link.link_up   = link_up;
597   host_link.link_down = link_down;
598   sg_platf_new_hostlink(&host_link);
599
600   XBT_DEBUG("<router id=\"%s\"/>", router_id);
601   s_sg_platf_router_cbarg_t router = SG_PLATF_ROUTER_INITIALIZER;
602   memset(&router, 0, sizeof(router));
603   router.id = router_id;
604   router.coord = peer->coord;
605   sg_platf_new_router(&router);
606   static_cast<AsCluster*>(current_routing)->router_ = static_cast<NetCard*>(xbt_lib_get_or_null(as_router_lib, router.id, ROUTING_ASR_LEVEL));
607
608   XBT_DEBUG("</AS>");
609   sg_platf_new_AS_end();
610   XBT_DEBUG(" ");
611
612   //xbt_dynar_free(&tab_elements_num);
613   free(router_id);
614   free(host_id);
615   free(link_id);
616   free(link_up);
617   free(link_down);
618 }
619
620 // static void routing_parse_Srandom(void)
621 // {
622 //   double mean, std, min, max, seed;
623 //   char *random_id = A_surfxml_random_id;
624 //   char *random_radical = A_surfxml_random_radical;
625 //   char *rd_name = NULL;
626 //   char *rd_value;
627 //   mean = surf_parse_get_double(A_surfxml_random_mean);
628 //   std = surf_parse_get_double(A_surfxml_random_std___deviation);
629 //   min = surf_parse_get_double(A_surfxml_random_min);
630 //   max = surf_parse_get_double(A_surfxml_random_max);
631 //   seed = surf_parse_get_double(A_surfxml_random_seed);
632
633 //   double res = 0;
634 //   int i = 0;
635 //   random_data_t random = xbt_new0(s_random_data_t, 1);
636 //   char *tmpbuf;
637
638 //   xbt_dynar_t radical_elements;
639 //   unsigned int iter;
640 //   char *groups;
641 //   int start, end;
642 //   xbt_dynar_t radical_ends;
643
644 //   switch (A_surfxml_random_generator) {
645 //   case AU_surfxml_random_generator:
646 //   case A_surfxml_random_generator_NONE:
647 //     random->generator = NONE;
648 //     break;
649 //   case A_surfxml_random_generator_DRAND48:
650 //     random->generator = DRAND48;
651 //     break;
652 //   case A_surfxml_random_generator_RAND:
653 //     random->generator = RAND;
654 //     break;
655 //   case A_surfxml_random_generator_RNGSTREAM:
656 //     random->generator = RNGSTREAM;
657 //     break;
658 //   default:
659 //     surf_parse_error("Invalid random generator");
660 //     break;
661 //   }
662 //   random->seed = seed;
663 //   random->min = min;
664 //   random->max = max;
665
666 //   /* Check user stupidities */
667 //   if (max < min)
668 //     THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
669 //   if (mean < min)
670 //     THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean, min);
671 //   if (mean > max)
672 //     THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean, max);
673
674 //   /* normalize the mean and standard deviation before storing */
675 //   random->mean = (mean - min) / (max - min);
676 //   random->std = std / (max - min);
677
678 //   if (random->mean * (1 - random->mean) < random->std * random->std)
679 //     THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
680 //            random->mean, random->std);
681
682 //   XBT_DEBUG
683 //       ("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
684 //        random_id, random->min, random->max, random->mean, random->std,
685 //        (int)random->generator, random->seed, random_radical);
686
687 //   if (!random_value)
688 //     random_value = xbt_dict_new_homogeneous(free);
689
690 //   if (!strcmp(random_radical, "")) {
691 //     res = random_generate(random);
692 //     rd_value = bprintf("%f", res);
693 //     xbt_dict_set(random_value, random_id, rd_value, NULL);
694 //   } else {
695 //     radical_elements = xbt_str_split(random_radical, ",");
696 //     xbt_dynar_foreach(radical_elements, iter, groups) {
697 //       radical_ends = xbt_str_split(groups, "-");
698 //       switch (xbt_dynar_length(radical_ends)) {
699 //       case 1:
700 //         xbt_assert(!xbt_dict_get_or_null(random_value, random_id),
701 //                    "Custom Random '%s' already exists !", random_id);
702 //         res = random_generate(random);
703 //         tmpbuf =
704 //             bprintf("%s%d", random_id,
705 //                     atoi(xbt_dynar_getfirst_as(radical_ends, char *)));
706 //         xbt_dict_set(random_value, tmpbuf, bprintf("%f", res), NULL);
707 //         xbt_free(tmpbuf);
708 //         break;
709
710 //       case 2:
711 //         start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
712 //         end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
713 //         for (i = start; i <= end; i++) {
714 //           xbt_assert(!xbt_dict_get_or_null(random_value, random_id),
715 //                      "Custom Random '%s' already exists !", bprintf("%s%d",
716 //                                                                     random_id,
717 //                                                                     i));
718 //           res = random_generate(random);
719 //           tmpbuf = bprintf("%s%d", random_id, i);
720 //           xbt_dict_set(random_value, tmpbuf, bprintf("%f", res), NULL);
721 //           xbt_free(tmpbuf);
722 //         }
723 //         break;
724 //       default:
725 //         XBT_CRITICAL("Malformed radical");
726 //         break;
727 //       }
728 //       res = random_generate(random);
729 //       rd_name = bprintf("%s_router", random_id);
730 //       rd_value = bprintf("%f", res);
731 //       xbt_dict_set(random_value, rd_name, rd_value, NULL);
732
733 //       xbt_dynar_free(&radical_ends);
734 //     }
735 //     free(rd_name);
736 //     xbt_dynar_free(&radical_elements);
737 //   }
738 // }
739
740 static void check_disk_attachment()
741 {
742   xbt_lib_cursor_t cursor;
743   char *key;
744   void **data;
745   simgrid::surf::NetCard *host_elm;
746   xbt_lib_foreach(storage_lib, cursor, key, data) {
747     if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), SURF_STORAGE_LEVEL) != NULL) {
748     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));
749     host_elm = sg_netcard_by_name_or_null(storage->p_attach);
750     if(!host_elm)
751       surf_parse_error("Unable to attach storage %s: host %s doesn't exist.", storage->getName(), storage->p_attach);
752     }
753   }
754 }
755
756 void routing_register_callbacks()
757 {
758   simgrid::surf::on_postparse.connect(check_disk_attachment);
759
760   instr_routing_define_callbacks();
761 }
762
763 /**
764  * \brief Recursive function for finalize
765  *
766  * \param rc the source host name
767  *
768  * This fuction is call by "finalize". It allow to finalize the
769  * AS or routing components. It delete all the structures.
770  */
771 static void finalize_rec(simgrid::surf::As *as) {
772   xbt_dict_cursor_t cursor = NULL;
773   char *key;
774   AS_t elem;
775
776   xbt_dict_foreach(as->sons_, cursor, key, elem) {
777     finalize_rec(elem);
778   }
779
780   delete as;;
781 }
782
783 /** \brief Frees all memory allocated by the routing module */
784 void routing_exit(void) {
785   delete routing_platf;
786 }
787
788 namespace simgrid {
789 namespace surf {
790
791   RoutingPlatf::RoutingPlatf(void *loopback)
792   : loopback_(loopback)
793   {
794   }
795   RoutingPlatf::~RoutingPlatf()
796   {
797     xbt_dynar_free(&lastRoute_);
798     finalize_rec(root_);
799   }
800
801 }
802 }
803
804 AS_t surf_AS_get_routing_root() {
805   return routing_platf->root_;
806 }
807
808 const char *surf_AS_get_name(simgrid::surf::As *as) {
809   return as->name_;
810 }
811
812 static simgrid::surf::As *surf_AS_recursive_get_by_name(
813   simgrid::surf::As *current, const char * name)
814 {
815   xbt_dict_cursor_t cursor = NULL;
816   char *key;
817   AS_t elem;
818   simgrid::surf::As *tmp = NULL;
819
820   if(!strcmp(current->name_, name))
821     return current;
822
823   xbt_dict_foreach(current->sons_, cursor, key, elem) {
824     tmp = surf_AS_recursive_get_by_name(elem, name);
825     if(tmp != NULL ) {
826         break;
827     }
828   }
829   return tmp;
830 }
831
832 simgrid::surf::As *surf_AS_get_by_name(const char * name)
833 {
834   simgrid::surf::As *as = surf_AS_recursive_get_by_name(routing_platf->root_, name);
835   if(as == NULL)
836     XBT_WARN("Impossible to find an AS with name %s, please check your input", name);
837   return as;
838 }
839
840 xbt_dict_t surf_AS_get_routing_sons(simgrid::surf::As *as)
841 {
842   return as->sons_;
843 }
844
845 xbt_dynar_t surf_AS_get_hosts(simgrid::surf::As *as)
846 {
847   xbt_dynar_t elms = as->vertices_;
848   int count = xbt_dynar_length(elms);
849   xbt_dynar_t res =  xbt_dynar_new(sizeof(sg_host_t), NULL);
850   for (int index = 0; index < count; index++) {
851      sg_netcard_t relm =
852       xbt_dynar_get_as(elms, index, simgrid::surf::NetCard*);
853      sg_host_t delm = simgrid::s4u::Host::by_name_or_null(relm->name());
854      if (delm!=NULL) {
855        xbt_dynar_push(res, &delm);
856      }
857   }
858   return res;
859 }
860
861 void surf_AS_get_graph(AS_t as, xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges) {
862   as->getGraph(graph, nodes, edges);
863 }