Logo AND Algorithmique Numérique Distribuée

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