Logo AND Algorithmique Numérique Distribuée

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