Logo AND Algorithmique Numérique Distribuée

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