Logo AND Algorithmique Numérique Distribuée

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