Logo AND Algorithmique Numérique Distribuée

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