Logo AND Algorithmique Numérique Distribuée

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