Logo AND Algorithmique Numérique Distribuée

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