Logo AND Algorithmique Numérique Distribuée

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