Logo AND Algorithmique Numérique Distribuée

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