1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include <pcre.h> /* regular expression library */
9 #include "simgrid/platf_interface.h" // platform creation API internal interface
11 #include "surf_routing_private.h"
12 #include "surf/surf_routing.h"
13 #include "surf/surfxml_parse_values.h"
16 int ROUTING_HOST_LEVEL; //Routing level
17 int SURF_CPU_LEVEL; //Surf cpu level
18 int SURF_WKS_LEVEL; //Surf workstation level
19 int SIMIX_HOST_LEVEL; //Simix level
20 int MSG_HOST_LEVEL; //Msg level
21 int SD_HOST_LEVEL; //Simdag level
22 int COORD_HOST_LEVEL; //Coordinates level
23 int NS3_HOST_LEVEL; //host node for ns3
26 int SD_LINK_LEVEL; //Simdag level
27 int SURF_LINK_LEVEL; //Surf level
28 int NS3_LINK_LEVEL; //link for ns3
30 xbt_lib_t as_router_lib;
31 int ROUTING_ASR_LEVEL; //Routing level
32 int COORD_ASR_LEVEL; //Coordinates level
33 int NS3_ASR_LEVEL; //host node for ns3
35 static xbt_dict_t patterns = NULL;
36 static xbt_dict_t random_value = NULL;
39 routing_global_t global_routing = NULL;
40 routing_component_t current_routing = NULL;
41 model_type_t current_routing_model = NULL;
43 /* global parse functions */
44 xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
45 static const char *src = NULL; /* temporary store the source name of a route */
46 static const char *dst = NULL; /* temporary store the destination name of a route */
47 static char *gw_src = NULL; /* temporary store the gateway source name of a route */
48 static char *gw_dst = NULL; /* temporary store the gateway destination name of a route */
49 static double_f_cpvoid_t get_link_latency = NULL;
51 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
53 static void routing_parse_Speer(sg_platf_peer_cbarg_t peer); /* peer bypass */
54 static void routing_parse_Srandom(void); /* random bypass */
56 static char* replace_random_parameter(char * chaine);
57 static void clean_routing_after_parse(void);
59 /* this lines are only for replace use like index in the model table */
64 SURF_MODEL_DIJKSTRACACHE,
71 struct s_model_type routing_models[] = { {"Full",
72 "Full routing data (fast, large memory requirements, fully expressive)",
78 "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
79 model_floyd_create, model_floyd_load, model_floyd_unload,
82 "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
83 model_dijkstra_create, model_dijkstra_both_load,
84 model_dijkstra_both_unload, model_dijkstra_both_end},
86 "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
87 model_dijkstracache_create, model_dijkstra_both_load,
88 model_dijkstra_both_unload, model_dijkstra_both_end},
89 {"none", "No routing (usable with Constant network only)",
90 model_none_create, model_none_load, model_none_unload, model_none_end},
91 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
92 model_none_load, model_none_unload, model_none_end},
93 {"Vivaldi", "Vivaldi routing", model_vivaldi_create,
94 model_none_load, model_none_unload, model_none_end},
95 {"Cluster", "Cluster routing", model_cluster_create,
96 model_none_load, model_cluster_unload, model_none_end},
97 {NULL, NULL, NULL, NULL, NULL, NULL}
101 * \brief Add a "host" to the network element list
103 static void parse_S_host(sg_platf_host_cbarg_t host) {
104 network_element_info_t info = NULL;
105 if (current_routing->hierarchy == SURF_ROUTING_NULL)
106 current_routing->hierarchy = SURF_ROUTING_BASE;
107 xbt_assert(!xbt_lib_get_or_null(host_lib, host->id,ROUTING_HOST_LEVEL),
108 "Reading a host, processing unit \"%s\" already exists",
110 xbt_assert(current_routing->set_processing_unit,
111 "no defined method \"set_processing_unit\" in \"%s\"",
112 current_routing->name);
113 (*(current_routing->set_processing_unit)) (current_routing, host->id);
114 info = xbt_new0(s_network_element_info_t, 1);
115 info->rc_component = current_routing;
116 info->rc_type = SURF_NETWORK_ELEMENT_HOST;
117 xbt_lib_set(host_lib,host->id,ROUTING_HOST_LEVEL,(void *) info);
118 if (host->coord && strcmp(host->coord,"")) {
119 if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
120 xbt_dynar_t ctn = xbt_str_split_str(host->coord, " ");
121 xbt_dynar_shrink(ctn, 0);
122 xbt_lib_set(host_lib,host->id,COORD_HOST_LEVEL,(void *) ctn);
127 * \brief Add a "router" to the network element list
129 static void parse_S_router(sg_platf_router_cbarg_t router)
131 network_element_info_t info = NULL;
132 if (current_routing->hierarchy == SURF_ROUTING_NULL)
133 current_routing->hierarchy = SURF_ROUTING_BASE;
134 xbt_assert(!xbt_lib_get_or_null(as_router_lib,router->id, ROUTING_ASR_LEVEL),
135 "Reading a router, processing unit \"%s\" already exists",
137 xbt_assert(current_routing->set_processing_unit,
138 "no defined method \"set_processing_unit\" in \"%s\"",
139 current_routing->name);
140 (*(current_routing->set_processing_unit)) (current_routing, router->id);
141 info = xbt_new0(s_network_element_info_t, 1);
142 info->rc_component = current_routing;
143 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
145 xbt_lib_set(as_router_lib,router->id,ROUTING_ASR_LEVEL,(void *) info);
146 if (strcmp(router->coord,"")) {
147 if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
148 xbt_dynar_t ctn = xbt_str_split_str(router->coord, " ");
149 xbt_dynar_shrink(ctn, 0);
150 xbt_lib_set(as_router_lib,router->id,COORD_ASR_LEVEL,(void *) ctn);
155 * \brief Set the endponints for a route
157 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
159 if (src != NULL && dst != NULL && link_list != NULL)
160 THROWF(arg_error, 0, "Route between %s to %s can not be defined",
164 xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
165 "Some limits are null in the route between \"%s\" and \"%s\"",
167 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
171 * \brief Set the endpoints for a route from XML
173 static void parse_S_route_new_and_endpoints_XML(void)
175 parse_S_route_new_and_endpoints(A_surfxml_route_src,
176 A_surfxml_route_dst);
180 * \brief Set the endponints and gateways for a ASroute
182 static void parse_S_ASroute_new_and_endpoints(void)
184 if (src != NULL && dst != NULL && link_list != NULL)
185 THROWF(arg_error, 0, "Route between %s to %s can not be defined",
186 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
187 src = A_surfxml_ASroute_src;
188 dst = A_surfxml_ASroute_dst;
189 gw_src = A_surfxml_ASroute_gw_src;
190 gw_dst = A_surfxml_ASroute_gw_dst;
191 xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
192 || strlen(gw_dst) > 0,
193 "Some limits are null in the route between \"%s\" and \"%s\"",
195 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
199 * \brief Set the endponints for a bypassRoute
201 static void parse_S_bypassRoute_new_and_endpoints(void)
203 if (src != NULL && dst != NULL && link_list != NULL)
205 "Bypass Route between %s to %s can not be defined",
206 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
207 src = A_surfxml_bypassRoute_src;
208 dst = A_surfxml_bypassRoute_dst;
209 gw_src = A_surfxml_bypassRoute_gw_src;
210 gw_dst = A_surfxml_bypassRoute_gw_dst;
211 xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
212 || strlen(gw_dst) > 0,
213 "Some limits are null in the route between \"%s\" and \"%s\"",
215 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
219 * \brief Set a new link on the actual list of link for a route or ASroute
221 static void parse_E_link_ctn_new_elem(const char *link_id)
224 val = xbt_strdup(link_id);
225 xbt_dynar_push(link_list, &val);
229 * \brief Set a new link on the actual list of link for a route or ASroute from XML
232 static void parse_E_link_ctn_new_elem_XML(void)
234 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
235 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
236 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
237 char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
238 parse_E_link_ctn_new_elem(link_id);
241 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
242 char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
243 parse_E_link_ctn_new_elem(link_id);
249 * \brief Store the route by calling the set_route function of the current routing component
251 static void parse_E_route_store_route(void)
253 name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
254 route->generic_route.link_list = link_list;
255 xbt_assert(current_routing->set_route,
256 "no defined method \"set_route\" in \"%s\"",
257 current_routing->name);
258 (*(current_routing->set_route)) (current_routing, src, dst, route);
265 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
267 static void parse_E_ASroute_store_route(void)
269 name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
270 e_route->generic_route.link_list = link_list;
271 e_route->src_gateway = xbt_strdup(gw_src);
272 e_route->dst_gateway = xbt_strdup(gw_dst);
273 xbt_assert(current_routing->set_ASroute,
274 "no defined method \"set_ASroute\" in \"%s\"",
275 current_routing->name);
276 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
285 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
287 static void parse_E_bypassRoute_store_route(void)
289 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
290 e_route->generic_route.link_list = link_list;
291 e_route->src_gateway = xbt_strdup(gw_src);
292 e_route->dst_gateway = xbt_strdup(gw_dst);
293 xbt_assert(current_routing->set_bypassroute,
294 "no defined method \"set_bypassroute\" in \"%s\"",
295 current_routing->name);
296 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
306 * \brief Make a new routing component to the platform
308 * Add a new autonomous system to the platform. Any elements (such as host,
309 * router or sub-AS) added after this call and before the corresponding call
310 * to sg_platf_new_AS_close() will be added to this AS.
312 * Once this function was called, the configuration concerning the used
313 * models cannot be changed anymore.
315 * @param AS_id name of this autonomous system. Must be unique in the platform
316 * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
318 void routing_AS_open(const char *AS_id, const char *wanted_routing_type)
320 routing_component_t new_routing;
321 model_type_t model = NULL;
324 surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag and before the first <AS>-like */
326 /* search the routing model */
327 for (cpt = 0; routing_models[cpt].name; cpt++)
328 if (!strcmp(wanted_routing_type, routing_models[cpt].name))
329 model = &routing_models[cpt];
330 /* if its not exist, error */
332 fprintf(stderr, "Routing model %s not found. Existing models:\n",
333 wanted_routing_type);
334 for (cpt = 0; routing_models[cpt].name; cpt++)
335 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
336 routing_models[cpt].desc);
340 /* make a new routing component */
341 new_routing = (routing_component_t) (*(model->create)) ();
342 new_routing->routing = model;
343 new_routing->hierarchy = SURF_ROUTING_NULL;
344 new_routing->name = xbt_strdup(AS_id);
345 new_routing->routing_sons = xbt_dict_new();
347 if (current_routing == NULL && global_routing->root == NULL) {
349 /* it is the first one */
350 new_routing->routing_father = NULL;
351 global_routing->root = new_routing;
353 } else if (current_routing != NULL && global_routing->root != NULL) {
355 xbt_assert(!xbt_dict_get_or_null
356 (current_routing->routing_sons, AS_id),
357 "The AS \"%s\" already exists", AS_id);
358 /* it is a part of the tree */
359 new_routing->routing_father = current_routing;
360 /* set the father behavior */
361 if (current_routing->hierarchy == SURF_ROUTING_NULL)
362 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
363 /* add to the sons dictionary */
364 xbt_dict_set(current_routing->routing_sons, AS_id,
365 (void *) new_routing, NULL);
366 /* add to the father element list */
367 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
368 /* unload the prev parse rules */
369 (*(current_routing->routing->unload)) ();
372 THROWF(arg_error, 0, "All defined components must be belong to a AS");
374 /* set the new parse rules */
375 (*(new_routing->routing->load)) ();
376 /* set the new current component of the tree */
377 current_routing = new_routing;
381 * \brief Specify that the current description of AS is finished
383 * Once you've declared all the content of your AS, you have to close
384 * it with this call. Your AS is not usable until you call this function.
386 * @fixme: this call is not as robust as wanted: bad things WILL happen
387 * if you call it twice for the same AS, or if you forget calling it, or
388 * even if you add stuff to a closed AS
391 void routing_AS_close() {
393 if (current_routing == NULL) {
394 THROWF(arg_error, 0, "Close an AS, but none was under construction");
396 network_element_info_t info = NULL;
397 xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
398 "The AS \"%s\" already exists",current_routing->name);
399 info = xbt_new0(s_network_element_info_t, 1);
400 info->rc_component = current_routing->routing_father;
401 info->rc_type = SURF_NETWORK_ELEMENT_AS;
402 xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
404 (*(current_routing->routing->unload)) ();
405 (*(current_routing->routing->end)) ();
406 current_routing = current_routing->routing_father;
407 if (current_routing != NULL)
408 (*(current_routing->routing->load)) ();
412 /* Aux Business methods */
415 * \brief Get the AS name of the element
417 * \param name the host name
420 static char* elements_As_name(const char *name)
422 routing_component_t as_comp;
424 /* (1) find the as where the host is located */
425 as_comp = ((network_element_info_t)
426 xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
427 return as_comp->name;
432 * \brief Get the AS father and the first elements of the chain
434 * \param src the source host name
435 * \param dst the destination host name
437 * Get the common father of the to processing units, and the first different
438 * father in the chain
440 static void elements_father(const char *src, const char *dst,
441 routing_component_t *res_father,
442 routing_component_t *res_src,
443 routing_component_t *res_dst)
445 xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
446 #define ELEMENTS_FATHER_MAXDEPTH 16 /* increase if it is not enough */
447 routing_component_t src_as, dst_as;
448 routing_component_t path_src[ELEMENTS_FATHER_MAXDEPTH];
449 routing_component_t path_dst[ELEMENTS_FATHER_MAXDEPTH];
452 routing_component_t current;
453 routing_component_t current_src;
454 routing_component_t current_dst;
455 routing_component_t father;
457 /* (1) find the as where the src and dst are located */
458 network_element_info_t src_data = xbt_lib_get_or_null(host_lib, src,
460 network_element_info_t dst_data = xbt_lib_get_or_null(host_lib, dst,
463 src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
465 dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
466 src_as = src_data->rc_component;
467 dst_as = dst_data->rc_component;
469 xbt_assert(src_as && dst_as,
470 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src, dst);
472 /* (2) find the path to the root routing component */
473 for (current = src_as ; current != NULL ; current = current->routing_father) {
474 if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
475 xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
476 path_src[index_src++] = current;
478 for (current = dst_as ; current != NULL ; current = current->routing_father) {
479 if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
480 xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
481 path_dst[index_dst++] = current;
484 /* (3) find the common father */
486 current_src = path_src[--index_src];
487 current_dst = path_dst[--index_dst];
488 } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
490 /* (4) they are not in the same routing component, make the path */
491 if (current_src == current_dst)
492 father = current_src;
494 father = path_src[index_src + 1];
496 /* (5) result generation */
497 *res_father = father; /* first the common father of src and dst */
498 *res_src = current_src; /* second the first different father of src */
499 *res_dst = current_dst; /* three the first different father of dst */
501 #undef ELEMENTS_FATHER_MAXDEPTH
504 /* Global Business methods */
507 * \brief Recursive function for get_route_latency
509 * \param src the source host name
510 * \param dst the destination host name
511 * \param *e_route the route where the links are stored
512 * \param *latency the latency, if needed
514 * This function is called by "get_route" and "get_latency". It allows to walk
515 * recursively through the routing components tree.
517 static void _get_route_latency(const char *src, const char *dst,
518 xbt_dynar_t *route, double *latency)
520 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src, dst);
521 xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
523 routing_component_t common_father;
524 routing_component_t src_father;
525 routing_component_t dst_father;
526 elements_father(src, dst, &common_father, &src_father, &dst_father);
528 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
530 route_extended_t e_route = NULL;
532 e_route = common_father->get_route(common_father, src, dst);
533 xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
534 *route = e_route->generic_route.link_list;
537 *latency = common_father->get_latency(common_father, src, dst, e_route);
538 xbt_assert(*latency >= 0.0,
539 "latency error on route between \"%s\" and \"%s\"", src, dst);
542 xbt_free(e_route->src_gateway);
543 xbt_free(e_route->dst_gateway);
547 } else { /* SURF_ROUTING_RECURSIVE */
549 route_extended_t e_route_bypass = NULL;
550 if (common_father->get_bypass_route)
551 e_route_bypass = common_father->get_bypass_route(common_father, src, dst);
553 xbt_assert(!latency || !e_route_bypass,
554 "Bypass cannot work yet with get_latency");
556 route_extended_t e_route_cnt = e_route_bypass
558 : common_father->get_route(common_father,
559 src_father->name, dst_father->name);
561 xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
562 src_father->name, dst_father->name);
564 xbt_assert((e_route_cnt->src_gateway == NULL) ==
565 (e_route_cnt->dst_gateway == NULL),
566 "bad gateway for route between \"%s\" and \"%s\"", src, dst);
569 *route = xbt_dynar_new(global_routing->size_of_link, NULL);
572 *latency = common_father->get_latency(common_father,
573 src_father->name, dst_father->name,
575 xbt_assert(*latency >= 0.0,
576 "latency error on route between \"%s\" and \"%s\"",
577 src_father->name, dst_father->name);
583 if (strcmp(src, e_route_cnt->src_gateway)) {
585 xbt_dynar_t route_src;
587 _get_route_latency(src, e_route_cnt->src_gateway,
588 (route ? &route_src : NULL),
589 (latency ? &latency_src : NULL));
591 xbt_assert(route_src, "no route between \"%s\" and \"%s\"",
592 src, e_route_cnt->src_gateway);
593 xbt_dynar_foreach(route_src, cpt, link) {
594 xbt_dynar_push(*route, &link);
596 xbt_dynar_free(&route_src);
599 xbt_assert(latency_src >= 0.0,
600 "latency error on route between \"%s\" and \"%s\"",
601 src, e_route_cnt->src_gateway);
602 *latency += latency_src;
607 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
608 xbt_dynar_push(*route, &link);
612 if (strcmp(e_route_cnt->dst_gateway, dst)) {
614 xbt_dynar_t route_dst;
616 _get_route_latency(e_route_cnt->dst_gateway, dst,
617 (route ? &route_dst : NULL),
618 (latency ? &latency_dst : NULL));
620 xbt_assert(route_dst, "no route between \"%s\" and \"%s\"",
621 e_route_cnt->dst_gateway, dst);
622 xbt_dynar_foreach(route_dst, cpt, link) {
623 xbt_dynar_push(*route, &link);
625 xbt_dynar_free(&route_dst);
628 xbt_assert(latency_dst >= 0.0,
629 "latency error on route between \"%s\" and \"%s\"",
630 e_route_cnt->dst_gateway, dst);
631 *latency += latency_dst;
635 generic_free_extended_route(e_route_cnt);
640 * \brief Generic function for get_route, get_route_no_cleanup, and get_latency
642 static void get_route_latency(const char *src, const char *dst,
643 xbt_dynar_t *route, double *latency, int cleanup)
645 _get_route_latency(src, dst, route, latency);
646 xbt_assert(!route || *route, "no route between \"%s\" and \"%s\"", src, dst);
647 xbt_assert(!latency || *latency >= 0.0,
648 "latency error on route between \"%s\" and \"%s\"", src, dst);
650 xbt_dynar_free(&global_routing->last_route);
651 global_routing->last_route = cleanup ? *route : NULL;
656 * \brief Generic method: find a route between hosts
658 * \param src the source host name
659 * \param dst the destination host name
661 * walk through the routing components tree and find a route between hosts
662 * by calling the differents "get_route" functions in each routing component.
663 * No need to free the returned dynar. It will be freed at the next call.
665 static xbt_dynar_t get_route(const char *src, const char *dst)
667 xbt_dynar_t route = NULL;
668 get_route_latency(src, dst, &route, NULL, 1);
673 * \brief Generic method: find a route between hosts
675 * \param src the source host name
676 * \param dst the destination host name
678 * same as get_route, but return NULL if any exception is raised.
680 static xbt_dynar_t get_route_or_null(const char *src, const char *dst)
682 xbt_dynar_t route = NULL;
685 get_route_latency(src, dst, &route, NULL, 1);
687 xbt_ex_free(exception);
694 * \brief Generic method: find a route between hosts
696 * \param src the source host name
697 * \param dst the destination host name
699 * walk through the routing components tree and find a route between hosts
700 * by calling the differents "get_route" functions in each routing component.
701 * Leaves the caller the responsability to clean the returned dynar.
703 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
705 xbt_dynar_t route = NULL;
706 get_route_latency(src, dst, &route, NULL, 0);
711 static double get_latency(const char *src, const char *dst)
713 double latency = -1.0;
714 get_route_latency(src, dst, NULL, &latency, 0);
718 static int surf_parse_models_setup_already_called=0;
719 /* Call the last initialization functions, that must be called after the
720 * <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
722 void surf_parse_models_setup()
724 if (surf_parse_models_setup_already_called)
726 surf_parse_models_setup_already_called=1;
727 surf_config_models_setup();
732 * \brief Recursive function for finalize
734 * \param rc the source host name
736 * This fuction is call by "finalize". It allow to finalize the
737 * AS or routing components. It delete all the structures.
739 static void _finalize(routing_component_t rc)
742 xbt_dict_cursor_t cursor = NULL;
744 routing_component_t elem;
745 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
748 xbt_dict_t tmp_sons = rc->routing_sons;
749 char *tmp_name = rc->name;
750 xbt_dict_free(&tmp_sons);
752 xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
753 current_routing->name);
754 (*(rc->finalize)) (rc);
759 * \brief Generic method: delete all the routing structures
761 * walk through the routing components tree and delete the structures
762 * by calling the differents "finalize" functions in each routing component
764 static void finalize(void)
766 /* delete recursively all the tree */
767 _finalize(global_routing->root);
768 /* delete last_route */
769 xbt_dynar_free(&(global_routing->last_route));
770 /* delete global routing structure */
771 xbt_free(global_routing);
772 /* make sure that we will reinit the models while loading the platf once reinited -- HACK but there is no proper surf_routing_init() */
773 surf_parse_models_setup_already_called = 0;
776 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
778 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
780 //adding my one link routes
783 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
785 xbt_dynar_foreach(onelink_mine, cpt, link) {
786 xbt_dynar_push(ret, &link);
791 xbt_dict_cursor_t cursor = NULL;
792 routing_component_t rc_child;
793 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
794 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
796 xbt_dynar_foreach(onelink_child, cpt, link) {
797 xbt_dynar_push(ret, &link);
804 static xbt_dynar_t get_onelink_routes(void)
806 return recursive_get_onelink_routes(global_routing->root);
809 e_surf_network_element_type_t get_network_element_type(const char
812 network_element_info_t rc = NULL;
814 rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
815 if(rc) return rc->rc_type;
817 rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
818 if(rc) return rc->rc_type;
820 return SURF_NETWORK_ELEMENT_NULL;
824 * \brief Generic method: create the global routing schema
826 * Make a global routing structure and set all the parsing functions.
828 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
830 /* config the uniq global routing */
831 global_routing = xbt_new0(s_routing_global_t, 1);
832 global_routing->root = NULL;
833 global_routing->get_route = get_route;
834 global_routing->get_route_or_null = get_route_or_null;
835 global_routing->get_latency = get_latency;
836 global_routing->get_route_no_cleanup = get_route_no_cleanup;
837 global_routing->get_onelink_routes = get_onelink_routes;
838 global_routing->get_route_latency = get_route_latency;
839 global_routing->get_network_element_type = get_network_element_type;
840 global_routing->finalize = finalize;
841 global_routing->loopback = loopback;
842 global_routing->size_of_link = size_of_links;
843 global_routing->last_route = NULL;
844 get_link_latency = get_link_latency_fun;
845 /* no current routing at moment */
846 current_routing = NULL;
848 /* parse generic elements */
849 sg_platf_host_add_cb(parse_S_host);
850 sg_platf_router_add_cb(parse_S_router);
852 surfxml_add_callback(STag_surfxml_route_cb_list,
853 &parse_S_route_new_and_endpoints_XML);
854 surfxml_add_callback(STag_surfxml_ASroute_cb_list,
855 &parse_S_ASroute_new_and_endpoints);
856 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
857 &parse_S_bypassRoute_new_and_endpoints);
859 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
860 &parse_E_link_ctn_new_elem_XML);
862 surfxml_add_callback(ETag_surfxml_route_cb_list,
863 &parse_E_route_store_route);
864 surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
865 &parse_E_ASroute_store_route);
866 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
867 &parse_E_bypassRoute_store_route);
869 surfxml_add_callback(STag_surfxml_cluster_cb_list,
870 &routing_parse_Scluster);
872 sg_platf_peer_add_cb(routing_parse_Speer); // FIXME: inline in the sg_platf_new_peer instead
873 sg_platf_postparse_add_cb(clean_routing_after_parse);
876 instr_routing_define_callbacks();
880 void surf_parse_add_callback_config(void)
882 surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
886 /* ************************************************** */
887 /* ********** PATERN FOR NEW ROUTING **************** */
889 /* The minimal configuration of a new routing model need the next functions,
890 * also you need to set at the start of the file, the new model in the model
891 * list. Remember keep the null ending of the list.
893 /*** Routing model structure ***/
895 // s_routing_component_t generic_routing;
896 // /* things that your routing model need */
897 // } s_routing_component_NEW_t,*routing_component_NEW_t;
899 /*** Parse routing model functions ***/
900 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
901 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
902 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
903 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
904 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
906 /*** Business methods ***/
907 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
908 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
909 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
911 /*** Creation routing model functions ***/
912 // static void* model_NEW_create(void) {
913 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
914 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
915 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
916 // new_component->generic_routing.set_route = model_NEW_set_route;
917 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
918 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
919 // new_component->generic_routing.get_route = NEW_get_route;
920 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
921 // new_component->generic_routing.finalize = NEW_finalize;
922 // /* initialization of internal structures */
923 // return new_component;
925 // static void model_NEW_load(void) {} /* mandatory */
926 // static void model_NEW_unload(void) {} /* mandatory */
927 // static void model_NEW_end(void) {} /* mandatory */
929 /* ************************************************************************** */
930 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
932 void generic_set_processing_unit(routing_component_t rc,
935 XBT_DEBUG("Load process unit \"%s\"", name);
936 int *id = xbt_new0(int, 1);
937 xbt_dict_t _to_index;
938 _to_index = current_routing->to_index;
939 *id = xbt_dict_length(_to_index);
940 xbt_dict_set(_to_index, name, id, xbt_free);
943 void generic_set_autonomous_system(routing_component_t rc,
946 XBT_DEBUG("Load Autonomous system \"%s\"", name);
947 int *id = xbt_new0(int, 1);
948 xbt_dict_t _to_index;
949 _to_index = current_routing->to_index;
950 *id = xbt_dict_length(_to_index);
951 xbt_dict_set(_to_index, name, id, xbt_free);
954 int surf_pointer_resource_cmp(const void *a, const void *b)
959 int surf_link_resource_cmp(const void *a, const void *b)
961 return !!memcmp(a,b,global_routing->size_of_link);
964 void generic_set_bypassroute(routing_component_t rc,
965 const char *src, const char *dst,
966 route_extended_t e_route)
968 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
969 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
972 route_name = bprintf("%s#%s", src, dst);
973 xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
974 "Invalid count of links, must be greater than zero (%s,%s)",
976 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
977 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
978 src, e_route->src_gateway, dst, e_route->dst_gateway);
980 route_extended_t new_e_route =
981 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
982 xbt_dynar_free(&(e_route->generic_route.link_list));
985 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
986 (void (*)(void *)) generic_free_extended_route);
987 xbt_free(route_name);
990 /* ************************************************************************** */
991 /* *********************** GENERIC BUSINESS METHODS ************************* */
993 double generic_get_link_latency(routing_component_t rc,
994 const char *src, const char *dst,
995 route_extended_t route)
997 int need_to_clean = route?0:1;
1000 double latency = 0.0;
1002 route = route?route:rc->get_route(rc,src,dst);
1004 xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1005 latency += get_link_latency(link);
1007 if(need_to_clean) generic_free_extended_route(route);
1011 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1013 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1016 route_extended_t generic_get_bypassroute(routing_component_t rc,
1020 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1021 routing_component_t src_as, dst_as;
1022 int index_src, index_dst;
1023 xbt_dynar_t path_src = NULL;
1024 xbt_dynar_t path_dst = NULL;
1025 routing_component_t current = NULL;
1026 routing_component_t *current_src = NULL;
1027 routing_component_t *current_dst = NULL;
1029 /* (1) find the as where the src and dst are located */
1030 void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1031 void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1032 if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1033 if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1035 if(src_data == NULL || dst_data == NULL)
1036 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1037 src, dst, rc->name);
1039 src_as = ((network_element_info_t)src_data)->rc_component;
1040 dst_as = ((network_element_info_t)dst_data)->rc_component;
1042 /* (2) find the path to the root routing component */
1043 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1045 while (current != NULL) {
1046 xbt_dynar_push(path_src, ¤t);
1047 current = current->routing_father;
1049 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1051 while (current != NULL) {
1052 xbt_dynar_push(path_dst, ¤t);
1053 current = current->routing_father;
1056 /* (3) find the common father */
1057 index_src = path_src->used - 1;
1058 index_dst = path_dst->used - 1;
1059 current_src = xbt_dynar_get_ptr(path_src, index_src);
1060 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1061 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1062 xbt_dynar_pop_ptr(path_src);
1063 xbt_dynar_pop_ptr(path_dst);
1066 current_src = xbt_dynar_get_ptr(path_src, index_src);
1067 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1070 int max_index_src = path_src->used - 1;
1071 int max_index_dst = path_dst->used - 1;
1073 int max_index = max(max_index_src, max_index_dst);
1076 route_extended_t e_route_bypass = NULL;
1078 for (max = 0; max <= max_index; max++) {
1079 for (i = 0; i < max; i++) {
1080 if (i <= max_index_src && max <= max_index_dst) {
1081 char *route_name = bprintf("%s#%s",
1082 (*(routing_component_t *)
1084 (path_src, i)))->name,
1085 (*(routing_component_t *)
1087 (path_dst, max)))->name);
1089 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1090 xbt_free(route_name);
1094 if (max <= max_index_src && i <= max_index_dst) {
1095 char *route_name = bprintf("%s#%s",
1096 (*(routing_component_t *)
1098 (path_src, max)))->name,
1099 (*(routing_component_t *)
1101 (path_dst, i)))->name);
1103 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1104 xbt_free(route_name);
1113 if (max <= max_index_src && max <= max_index_dst) {
1114 char *route_name = bprintf("%s#%s",
1115 (*(routing_component_t *)
1117 (path_src, max)))->name,
1118 (*(routing_component_t *)
1120 (path_dst, max)))->name);
1121 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1122 xbt_free(route_name);
1128 xbt_dynar_free(&path_src);
1129 xbt_dynar_free(&path_dst);
1131 route_extended_t new_e_route = NULL;
1133 if (e_route_bypass) {
1135 unsigned int cpt = 0;
1136 new_e_route = xbt_new0(s_route_extended_t, 1);
1137 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1138 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1139 new_e_route->generic_route.link_list =
1140 xbt_dynar_new(global_routing->size_of_link, NULL);
1141 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1142 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1149 /* ************************************************************************** */
1150 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1153 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1154 void *data, int order)
1160 xbt_dynar_t links = NULL, links_id = NULL;
1162 new_route = xbt_new0(s_route_t, 1);
1163 new_route->link_list =
1164 xbt_dynar_new(global_routing->size_of_link, NULL);
1166 xbt_assert(hierarchy == SURF_ROUTING_BASE,
1167 "the hierarchy type is not SURF_ROUTING_BASE");
1169 links = ((route_t) data)->link_list;
1172 links_id = new_route->link_list;
1174 xbt_dynar_foreach(links, cpt, link_name) {
1177 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1180 xbt_dynar_push(links_id, &link);
1182 xbt_dynar_unshift(links_id, &link);
1184 THROWF(mismatch_error, 0, "Link %s not found", link_name);
1191 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1192 void *data, int order)
1196 route_extended_t e_route, new_e_route;
1199 xbt_dynar_t links = NULL, links_id = NULL;
1201 new_e_route = xbt_new0(s_route_extended_t, 1);
1202 new_e_route->generic_route.link_list =
1203 xbt_dynar_new(global_routing->size_of_link, NULL);
1204 new_e_route->src_gateway = NULL;
1205 new_e_route->dst_gateway = NULL;
1207 xbt_assert(hierarchy == SURF_ROUTING_BASE
1208 || hierarchy == SURF_ROUTING_RECURSIVE,
1209 "the hierarchy type is not defined");
1211 if (hierarchy == SURF_ROUTING_BASE) {
1213 route = (route_t) data;
1214 links = route->link_list;
1216 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1218 e_route = (route_extended_t) data;
1219 xbt_assert(e_route->src_gateway
1220 && e_route->dst_gateway, "bad gateway, is null");
1221 links = e_route->generic_route.link_list;
1223 /* remeber not erase the gateway names */
1224 new_e_route->src_gateway = strdup(e_route->src_gateway);
1225 new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1228 links_id = new_e_route->generic_route.link_list;
1230 xbt_dynar_foreach(links, cpt, link_name) {
1233 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1236 xbt_dynar_push(links_id, &link);
1238 xbt_dynar_unshift(links_id, &link);
1240 THROWF(mismatch_error, 0, "Link %s not found", link_name);
1246 void generic_free_route(route_t route)
1249 xbt_dynar_free(&(route->link_list));
1254 void generic_free_extended_route(route_extended_t e_route)
1257 xbt_dynar_free(&(e_route->generic_route.link_list));
1258 if (e_route->src_gateway)
1259 xbt_free(e_route->src_gateway);
1260 if (e_route->dst_gateway)
1261 xbt_free(e_route->dst_gateway);
1266 static routing_component_t generic_as_exist(routing_component_t find_from,
1267 routing_component_t to_find)
1269 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1270 xbt_dict_cursor_t cursor = NULL;
1273 routing_component_t elem;
1274 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1275 if (to_find == elem || generic_as_exist(elem, to_find)) {
1286 generic_autonomous_system_exist(routing_component_t rc, char *element)
1288 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1289 routing_component_t element_as, result, elem;
1290 xbt_dict_cursor_t cursor = NULL;
1292 element_as = ((network_element_info_t)
1293 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1294 result = ((routing_component_t) - 1);
1295 if (element_as != rc)
1296 result = generic_as_exist(rc, element_as);
1300 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1301 found = !strcmp(elem->name, element);
1312 generic_processing_units_exist(routing_component_t rc, char *element)
1314 routing_component_t element_as;
1315 element_as = ((network_element_info_t)
1316 xbt_lib_get_or_null(host_lib,
1317 element, ROUTING_HOST_LEVEL))->rc_component;
1318 if (element_as == rc)
1320 return generic_as_exist(rc, element_as);
1323 void generic_src_dst_check(routing_component_t rc, const char *src,
1327 void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1328 void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1329 if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1330 if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1332 if(src_data == NULL || dst_data == NULL)
1333 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1334 src, dst, rc->name);
1336 routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1337 routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1339 if(src_as != dst_as)
1340 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1341 src, src_as->name, dst, dst_as->name);
1343 xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1344 src,dst,src_as->name, dst_as->name,rc->name);
1347 void routing_parse_Scluster(void)
1349 static int AX_ptr = 0;
1350 char *host_id, *groups, *link_id = NULL;
1352 s_sg_platf_host_cbarg_t host;
1353 s_sg_platf_link_cbarg_t link;
1355 if( strcmp(struct_cluster->availability_trace,"")
1356 || strcmp(struct_cluster->state_trace,"") )
1358 if(xbt_dict_size(patterns)==0)
1359 patterns = xbt_dict_new();
1360 xbt_dict_set(patterns,"id",xbt_strdup(struct_cluster->id),free);
1361 xbt_dict_set(patterns,"prefix",xbt_strdup(struct_cluster->prefix),free);
1362 xbt_dict_set(patterns,"suffix",xbt_strdup(struct_cluster->suffix),free);
1367 xbt_dynar_t radical_elements;
1368 xbt_dynar_t radical_ends;
1370 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1371 static unsigned int surfxml_buffer_stack_stack[1024];
1373 surfxml_buffer_stack_stack[0] = 0;
1374 surfxml_bufferstack_push(1);
1376 SURFXML_BUFFER_SET(AS_id, struct_cluster->id);
1377 SURFXML_BUFFER_SET(AS_routing, "Cluster");
1378 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->id);
1379 SURFXML_START_TAG(AS);
1382 radical_elements = xbt_str_split(struct_cluster->radical, ",");
1383 xbt_dynar_foreach(radical_elements, iter, groups) {
1384 memset(&host,0,sizeof(host));
1386 radical_ends = xbt_str_split(groups, "-");
1387 switch (xbt_dynar_length(radical_ends)) {
1389 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1390 host_id = bprintf("%s%d%s", struct_cluster->prefix, start, struct_cluster->suffix);
1391 link_id = bprintf("%s_link_%d", struct_cluster->id, start);
1393 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1395 if(strcmp(struct_cluster->availability_trace,"")){
1396 xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1397 char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1398 xbt_str_varsubst(tmp_availability_file,patterns);
1399 XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1400 host.power_trace = tmgr_trace_new(tmp_availability_file);
1401 xbt_free(tmp_availability_file);
1405 XBT_DEBUG("\tavailability_file=\"\"");
1407 if(strcmp(struct_cluster->state_trace,"")){
1408 char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1409 xbt_str_varsubst(tmp_state_file,patterns);
1410 XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1411 host.state_trace = tmgr_trace_new(tmp_state_file);
1412 xbt_free(tmp_state_file);
1416 XBT_DEBUG("\tstate_file=\"\"");
1419 host.power_peak = struct_cluster->power;
1420 host.power_scale = 1.0;
1421 host.core_amount = struct_cluster->core_amount;
1422 host.initial_state = SURF_RESOURCE_ON;
1424 sg_platf_new_host(&host);
1425 XBT_DEBUG("</host>");
1428 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1430 memset(&link,0,sizeof(link));
1432 link.bandwidth = struct_cluster->bw;
1433 link.latency = struct_cluster->lat;
1434 link.state = SURF_RESOURCE_ON;
1436 switch (struct_cluster->sharing_policy) {
1437 case A_surfxml_cluster_sharing_policy_FATPIPE:
1438 link.policy = SURF_LINK_FATPIPE;
1440 case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1441 link.policy = SURF_LINK_FULLDUPLEX;
1444 link.policy = SURF_LINK_SHARED;
1447 sg_platf_new_link(&link);
1449 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1450 if (struct_cluster->sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX){
1451 char* tmp_link = bprintf("%s_UP",link_id);
1452 info->link_up = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1454 tmp_link = bprintf("%s_DOWN",link_id);
1455 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1459 info->link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1460 info->link_down = info->link_up;
1462 surf_routing_cluster_add_link(host_id,info);
1470 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1471 end= surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1472 for (i = start; i <= end; i++) {
1473 host_id = bprintf("%s%d%s", struct_cluster->prefix, i, struct_cluster->suffix);
1474 link_id = bprintf("%s_link_%d", struct_cluster->id, i);
1476 A_surfxml_host_state = A_surfxml_host_state_ON;
1478 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1480 if(strcmp(struct_cluster->availability_trace,"")){
1481 xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1482 char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1483 xbt_str_varsubst(tmp_availability_file,patterns);
1484 XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1485 host.power_trace = tmgr_trace_new(tmp_availability_file);
1486 xbt_free(tmp_availability_file);
1490 XBT_DEBUG("\tavailability_file=\"\"");
1492 if(strcmp(struct_cluster->state_trace,"")){
1493 char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1494 xbt_str_varsubst(tmp_state_file,patterns);
1495 XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1496 host.state_trace = tmgr_trace_new(tmp_state_file);
1497 xbt_free(tmp_state_file);
1501 XBT_DEBUG("\tstate_file=\"\"");
1504 host.power_peak = struct_cluster->power;
1505 host.power_scale = 1.0;
1506 host.core_amount = struct_cluster->core_amount;
1507 host.initial_state = SURF_RESOURCE_ON;
1509 sg_platf_new_host(&host);
1510 XBT_DEBUG("</host>");
1512 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1514 memset(&link,0,sizeof(link));
1516 link.bandwidth = struct_cluster->bw;
1517 link.latency = struct_cluster->lat;
1518 link.state = SURF_RESOURCE_ON;
1520 switch (struct_cluster->sharing_policy) {
1521 case A_surfxml_cluster_sharing_policy_SHARED:
1522 link.policy = SURF_LINK_SHARED;
1524 case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1525 link.policy = SURF_LINK_FULLDUPLEX;
1527 case A_surfxml_cluster_sharing_policy_FATPIPE:
1528 link.policy = SURF_LINK_FATPIPE;
1531 surf_parse_error(bprintf("Invalid cluster sharing policy for cluster %s",struct_cluster->id));
1533 sg_platf_new_link(&link);
1535 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1536 if (link.policy == SURF_LINK_FULLDUPLEX) {
1537 char* tmp_link = bprintf("%s_UP",link_id);
1538 info->link_up = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1540 tmp_link = bprintf("%s_DOWN",link_id);
1541 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1545 info->link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1546 info->link_down = info->link_up;
1548 surf_routing_cluster_add_link(host_id,info);
1557 XBT_DEBUG("Malformed radical");
1561 xbt_dynar_free(&radical_ends);
1563 xbt_dynar_free(&radical_elements);
1565 // Add a router. It is magically used thanks to the way in which surf_routing_cluster is written, and it's very useful to connect clusters together
1567 XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->router_id);
1568 s_sg_platf_router_cbarg_t router;
1570 memset(&router,0,sizeof(router));
1571 router.id = struct_cluster->router_id;
1573 if (!router.id || !strcmp(router.id,""))
1574 router.id = newid = bprintf("%s%s_router%s", struct_cluster->prefix, struct_cluster->id, struct_cluster->suffix);
1575 sg_platf_new_router(&router);
1580 if( (struct_cluster->bb_bw!= 0) && (struct_cluster->bb_lat!=0) ){
1581 char *link_backbone = bprintf("%s_backbone", struct_cluster->id);
1582 XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->bb_bw, struct_cluster->bb_lat);
1584 memset(&link,0,sizeof(link));
1585 link.id = link_backbone;
1586 link.bandwidth = struct_cluster->bb_bw;
1587 link.latency = struct_cluster->bb_lat;
1588 link.state = SURF_RESOURCE_ON;
1590 switch (AX_surfxml_cluster_bb_sharing_policy) {
1591 case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
1592 link.policy = SURF_LINK_FATPIPE;
1594 case A_surfxml_cluster_bb_sharing_policy_SHARED:
1595 link.policy = SURF_LINK_SHARED;
1598 surf_parse_error(bprintf("Invalid bb sharing policy in cluster %s",struct_cluster->id));
1601 sg_platf_new_link(&link);
1603 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1604 info->link_up = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1605 info->link_down = info->link_up;
1606 surf_routing_cluster_add_link(struct_cluster->id, info);
1608 free(link_backbone);
1613 char *new_suffix = xbt_strdup("");
1615 radical_elements = xbt_str_split(struct_cluster->suffix, ".");
1616 xbt_dynar_foreach(radical_elements, iter, groups) {
1617 if (strcmp(groups, "")) {
1618 char *old_suffix = new_suffix;
1619 new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1624 xbt_dynar_free(&radical_elements);
1625 xbt_free(new_suffix);
1627 if( strcmp(struct_cluster->availability_trace,"")
1628 || strcmp(struct_cluster->state_trace,"") )
1629 xbt_dict_free(&patterns);
1632 SURFXML_END_TAG(AS);
1635 surfxml_bufferstack_pop(1);
1638 * This function take a string and replace parameters from patterns dict.
1639 * It returns the new value.
1641 static char* replace_random_parameter(char * string)
1643 char *test_string = NULL;
1645 if(xbt_dict_size(random_value)==0)
1648 string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1649 test_string = bprintf("${%s}", string);
1650 test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1652 if (strcmp(test_string,"")) { //if not empty, keep this value.
1654 string = test_string;
1655 } //In other case take old value (without ${})
1661 static void clean_routing_after_parse(void)
1663 xbt_dict_free(&random_value);
1664 xbt_dict_free(&patterns);
1667 static void routing_parse_Speer(sg_platf_peer_cbarg_t peer)
1669 static int AX_ptr = 0;
1670 char *host_id = NULL;
1671 char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1673 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1674 static unsigned int surfxml_buffer_stack_stack[1024];
1676 surfxml_buffer_stack_stack[0] = 0;
1678 surfxml_bufferstack_push(1);
1680 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer->id);
1681 sg_platf_new_AS_open(peer->id, "Full");
1684 host_id = HOST_PEER(peer->id);
1685 router_id = ROUTER_PEER(peer->id);
1686 link_id_up = LINK_UP_PEER(peer->id);
1687 link_id_down = LINK_DOWN_PEER(peer->id);
1689 link_router = bprintf("%s_link_router", peer->id);
1690 link_backbone = bprintf("%s_backbone", peer->id);
1692 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->power);
1693 s_sg_platf_host_cbarg_t host;
1694 memset(&host,0,sizeof(host));
1695 host.initial_state = SURF_RESOURCE_ON;
1697 host.power_peak = peer->power;
1698 host.power_scale = 1.0;
1699 host.power_trace = peer->availability_trace;
1700 host.state_trace = peer->state_trace;
1701 host.core_amount = 1;
1702 sg_platf_new_host(&host);
1705 XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer->coord);
1706 s_sg_platf_router_cbarg_t router;
1707 memset(&router,0,sizeof(router));
1708 router.id = router_id;
1709 router.coord = peer->coord;
1710 sg_platf_new_router(&router);
1712 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_up, peer->bw_in, peer->lat);
1713 s_sg_platf_link_cbarg_t link;
1714 memset(&link,0,sizeof(link));
1715 link.state = SURF_RESOURCE_ON;
1716 link.policy = SURF_LINK_SHARED;
1717 link.id = link_id_up;
1718 link.bandwidth = peer->bw_in;
1719 link.latency = peer->lat;
1720 sg_platf_new_link(&link);
1722 // FIXME: dealing with full duplex is not the role of this piece of code, I'd say [Mt]
1723 // Instead, it should be created fullduplex, and the models will do what's needed in this case
1724 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_down, peer->bw_out, peer->lat);
1725 link.id = link_id_down;
1726 sg_platf_new_link(&link);
1731 XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1732 XBT_DEBUG("symmetrical=\"NO\">");
1733 SURFXML_BUFFER_SET(route_src, host_id);
1734 SURFXML_BUFFER_SET(route_dst, router_id);
1735 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1736 SURFXML_START_TAG(route);
1738 XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1739 SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1740 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1741 SURFXML_START_TAG(link_ctn);
1742 SURFXML_END_TAG(link_ctn);
1744 XBT_DEBUG("</route>");
1745 SURFXML_END_TAG(route);
1748 XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1749 XBT_DEBUG("symmetrical=\"NO\">");
1750 SURFXML_BUFFER_SET(route_src, router_id);
1751 SURFXML_BUFFER_SET(route_dst, host_id);
1752 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1753 SURFXML_START_TAG(route);
1755 XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1756 SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1757 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1758 SURFXML_START_TAG(link_ctn);
1759 SURFXML_END_TAG(link_ctn);
1761 XBT_DEBUG("</route>");
1762 SURFXML_END_TAG(route);
1765 sg_platf_new_AS_close();
1768 //xbt_dynar_free(&tab_elements_num);
1772 free(link_backbone);
1775 surfxml_bufferstack_pop(1);
1778 static void routing_parse_Srandom(void)
1780 double mean, std, min, max, seed;
1781 char *random_id = A_surfxml_random_id;
1782 char *random_radical = A_surfxml_random_radical;
1783 char *rd_name = NULL;
1785 mean = surf_parse_get_double(A_surfxml_random_mean);
1786 std = surf_parse_get_double(A_surfxml_random_std_deviation);
1787 min = surf_parse_get_double(A_surfxml_random_min);
1788 max = surf_parse_get_double(A_surfxml_random_max);
1789 seed = surf_parse_get_double(A_surfxml_random_seed);
1793 random_data_t random = xbt_new0(s_random_data_t, 1);
1796 xbt_dynar_t radical_elements;
1800 xbt_dynar_t radical_ends;
1802 random->generator = A_surfxml_random_generator;
1803 random->seed = seed;
1807 /* Check user stupidities */
1809 THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1811 THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1814 THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1817 /* normalize the mean and standard deviation before storing */
1818 random->mean = (mean - min) / (max - min);
1819 random->std = std / (max - min);
1821 if (random->mean * (1 - random->mean) < random->std * random->std)
1822 THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1823 random->mean, random->std);
1825 XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1835 if(xbt_dict_size(random_value)==0)
1836 random_value = xbt_dict_new();
1838 if(!strcmp(random_radical,""))
1840 res = random_generate(random);
1841 rd_value = bprintf("%f",res);
1842 xbt_dict_set(random_value, random_id, rd_value, free);
1846 radical_elements = xbt_str_split(random_radical, ",");
1847 xbt_dynar_foreach(radical_elements, iter, groups) {
1848 radical_ends = xbt_str_split(groups, "-");
1849 switch (xbt_dynar_length(radical_ends)) {
1851 xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
1852 res = random_generate(random);
1853 tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
1854 xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1859 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1860 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1861 for (i = start; i <= end; i++) {
1862 xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
1863 res = random_generate(random);
1864 tmpbuf = bprintf("%s%d",random_id,i);
1865 xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1870 XBT_INFO("Malformed radical");
1873 res = random_generate(random);
1874 rd_name = bprintf("%s_router",random_id);
1875 rd_value = bprintf("%f",res);
1876 xbt_dict_set(random_value, rd_name, rd_value, free);
1878 xbt_dynar_free(&radical_ends);
1881 xbt_dynar_free(&radical_elements);