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;
50 xbt_dict_t cluster_host_link = NULL; /* for tag cluster */
52 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
54 static void routing_parse_Speer(void); /* peer bypass */
55 static void routing_parse_Srandom(void); /* random bypass */
57 static char* replace_random_parameter(char * chaine);
58 static void clean_routing_after_parse(void);
60 /* this lines are only for replace use like index in the model table */
65 SURF_MODEL_DIJKSTRACACHE,
72 struct s_model_type routing_models[] = { {"Full",
73 "Full routing data (fast, large memory requirements, fully expressive)",
79 "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
80 model_floyd_create, model_floyd_load, model_floyd_unload,
83 "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
84 model_dijkstra_create, model_dijkstra_both_load,
85 model_dijkstra_both_unload, model_dijkstra_both_end},
87 "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
88 model_dijkstracache_create, model_dijkstra_both_load,
89 model_dijkstra_both_unload, model_dijkstra_both_end},
90 {"none", "No routing (usable with Constant network only)",
91 model_none_create, model_none_load, model_none_unload, model_none_end},
92 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
93 model_none_load, model_none_unload, model_none_end},
94 {"Vivaldi", "Vivaldi routing", model_vivaldi_create,
95 model_none_load, model_none_unload, model_none_end},
96 {"Cluster", "Cluster routing", model_cluster_create,
97 model_none_load, model_none_unload, model_none_end},
98 {NULL, NULL, NULL, NULL, NULL, NULL}
102 * \brief Add a "host" to the network element list
104 static void parse_S_host(sg_platf_host_cbarg_t host) {
105 network_element_info_t info = NULL;
106 if (current_routing->hierarchy == SURF_ROUTING_NULL)
107 current_routing->hierarchy = SURF_ROUTING_BASE;
108 xbt_assert(!xbt_lib_get_or_null(host_lib, host->id,ROUTING_HOST_LEVEL),
109 "Reading a host, processing unit \"%s\" already exists",
111 xbt_assert(current_routing->set_processing_unit,
112 "no defined method \"set_processing_unit\" in \"%s\"",
113 current_routing->name);
114 (*(current_routing->set_processing_unit)) (current_routing, host->id);
115 info = xbt_new0(s_network_element_info_t, 1);
116 info->rc_component = current_routing;
117 info->rc_type = SURF_NETWORK_ELEMENT_HOST;
118 xbt_lib_set(host_lib,host->id,ROUTING_HOST_LEVEL,(void *) info);
119 if (host->coord && strcmp(host->coord,"")) {
120 if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
121 xbt_dynar_t ctn = xbt_str_split_str(host->coord, " ");
122 xbt_dynar_shrink(ctn, 0);
123 xbt_lib_set(host_lib,host->id,COORD_HOST_LEVEL,(void *) ctn);
128 * \brief Add a "router" to the network element list
130 static void parse_S_router(sg_platf_router_cbarg_t router)
132 network_element_info_t info = NULL;
133 if (current_routing->hierarchy == SURF_ROUTING_NULL)
134 current_routing->hierarchy = SURF_ROUTING_BASE;
135 xbt_assert(!xbt_lib_get_or_null(as_router_lib,router->id, ROUTING_ASR_LEVEL),
136 "Reading a router, processing unit \"%s\" already exists",
138 xbt_assert(current_routing->set_processing_unit,
139 "no defined method \"set_processing_unit\" in \"%s\"",
140 current_routing->name);
141 (*(current_routing->set_processing_unit)) (current_routing, router->id);
142 info = xbt_new0(s_network_element_info_t, 1);
143 info->rc_component = current_routing;
144 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
146 xbt_lib_set(as_router_lib,router->id,ROUTING_ASR_LEVEL,(void *) info);
147 if (strcmp(A_surfxml_router_coordinates,"")) {
148 if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
149 xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_router_coordinates, " ");
150 xbt_dynar_shrink(ctn, 0);
151 xbt_lib_set(as_router_lib,router->id,COORD_ASR_LEVEL,(void *) ctn);
156 * brief Add a "router" to the network element list from XML description
158 static void parse_S_router_lua(const char* router_id) {
159 s_sg_platf_router_cbarg_t router;
160 memset(&router,0,sizeof(router));
161 router.id = router_id;
163 return parse_S_router(&router);
167 * \brief Set the endponints for a route
169 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
171 if (src != NULL && dst != NULL && link_list != NULL)
172 THROWF(arg_error, 0, "Route between %s to %s can not be defined",
176 xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
177 "Some limits are null in the route between \"%s\" and \"%s\"",
179 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
183 * \brief Set the endpoints for a route from XML
185 static void parse_S_route_new_and_endpoints_XML(void)
187 parse_S_route_new_and_endpoints(A_surfxml_route_src,
188 A_surfxml_route_dst);
192 * \brief Set the endpoints for a route from lua
194 static void parse_S_route_new_and_endpoints_lua(const char *id_src, const char *id_dst)
196 parse_S_route_new_and_endpoints(id_src, id_dst);
200 * \brief Set the endponints and gateways for a ASroute
202 static void parse_S_ASroute_new_and_endpoints(void)
204 if (src != NULL && dst != NULL && link_list != NULL)
205 THROWF(arg_error, 0, "Route between %s to %s can not be defined",
206 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
207 src = A_surfxml_ASroute_src;
208 dst = A_surfxml_ASroute_dst;
209 gw_src = A_surfxml_ASroute_gw_src;
210 gw_dst = A_surfxml_ASroute_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 the endponints for a bypassRoute
221 static void parse_S_bypassRoute_new_and_endpoints(void)
223 if (src != NULL && dst != NULL && link_list != NULL)
225 "Bypass Route between %s to %s can not be defined",
226 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
227 src = A_surfxml_bypassRoute_src;
228 dst = A_surfxml_bypassRoute_dst;
229 gw_src = A_surfxml_bypassRoute_gw_src;
230 gw_dst = A_surfxml_bypassRoute_gw_dst;
231 xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
232 || strlen(gw_dst) > 0,
233 "Some limits are null in the route between \"%s\" and \"%s\"",
235 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
239 * \brief Set a new link on the actual list of link for a route or ASroute
241 static void parse_E_link_ctn_new_elem(const char *link_id)
244 val = xbt_strdup(link_id);
245 xbt_dynar_push(link_list, &val);
249 * \brief Set a new link on the actual list of link for a route or ASroute from XML
252 static void parse_E_link_ctn_new_elem_XML(void)
254 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
255 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
256 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
257 char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
258 parse_E_link_ctn_new_elem(link_id);
261 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
262 char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
263 parse_E_link_ctn_new_elem(link_id);
269 * \brief Set a new link on the actual list of link for a route or ASroute from lua
271 static void parse_E_link_c_ctn_new_elem_lua(const char *link_id)
273 parse_E_link_ctn_new_elem(link_id);
277 * \brief Store the route by calling the set_route function of the current routing component
279 static void parse_E_route_store_route(void)
281 name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
282 route->generic_route.link_list = link_list;
283 xbt_assert(current_routing->set_route,
284 "no defined method \"set_route\" in \"%s\"",
285 current_routing->name);
286 (*(current_routing->set_route)) (current_routing, src, dst, route);
293 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
295 static void parse_E_ASroute_store_route(void)
297 name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
298 e_route->generic_route.link_list = link_list;
299 e_route->src_gateway = xbt_strdup(gw_src);
300 e_route->dst_gateway = xbt_strdup(gw_dst);
301 xbt_assert(current_routing->set_ASroute,
302 "no defined method \"set_ASroute\" in \"%s\"",
303 current_routing->name);
304 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
313 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
315 static void parse_E_bypassRoute_store_route(void)
317 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
318 e_route->generic_route.link_list = link_list;
319 e_route->src_gateway = xbt_strdup(gw_src);
320 e_route->dst_gateway = xbt_strdup(gw_dst);
321 xbt_assert(current_routing->set_bypassroute,
322 "no defined method \"set_bypassroute\" in \"%s\"",
323 current_routing->name);
324 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
334 * \brief Make a new routing component to the platform
336 * Add a new autonomous system to the platform. Any elements (such as host,
337 * router or sub-AS) added after this call and before the corresponding call
338 * to sg_platf_new_AS_close() will be added to this AS.
340 * Once this function was called, the configuration concerning the used
341 * models cannot be changed anymore.
343 * @param AS_id name of this autonomous system. Must be uniq in the platform
344 * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
346 void sg_platf_new_AS_open(const char *AS_id, const char *wanted_routing_type)
348 routing_component_t new_routing;
349 model_type_t model = NULL;
352 surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag and before the first <AS>-like */
354 /* search the routing model */
355 for (cpt = 0; routing_models[cpt].name; cpt++)
356 if (!strcmp(wanted_routing_type, routing_models[cpt].name))
357 model = &routing_models[cpt];
358 /* if its not exist, error */
360 fprintf(stderr, "Routing model %s not found. Existing models:\n",
361 wanted_routing_type);
362 for (cpt = 0; routing_models[cpt].name; cpt++)
363 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
364 routing_models[cpt].desc);
368 /* make a new routing component */
369 new_routing = (routing_component_t) (*(model->create)) ();
370 new_routing->routing = model;
371 new_routing->hierarchy = SURF_ROUTING_NULL;
372 new_routing->name = xbt_strdup(AS_id);
373 new_routing->routing_sons = xbt_dict_new();
375 if (current_routing == NULL && global_routing->root == NULL) {
377 /* it is the first one */
378 new_routing->routing_father = NULL;
379 global_routing->root = new_routing;
381 } else if (current_routing != NULL && global_routing->root != NULL) {
383 xbt_assert(!xbt_dict_get_or_null
384 (current_routing->routing_sons, AS_id),
385 "The AS \"%s\" already exists", AS_id);
386 /* it is a part of the tree */
387 new_routing->routing_father = current_routing;
388 /* set the father behavior */
389 if (current_routing->hierarchy == SURF_ROUTING_NULL)
390 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
391 /* add to the sons dictionary */
392 xbt_dict_set(current_routing->routing_sons, AS_id,
393 (void *) new_routing, NULL);
394 /* add to the father element list */
395 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
396 /* unload the prev parse rules */
397 (*(current_routing->routing->unload)) ();
400 THROWF(arg_error, 0, "All defined components must be belong to a AS");
402 /* set the new parse rules */
403 (*(new_routing->routing->load)) ();
404 /* set the new current component of the tree */
405 current_routing = new_routing;
409 * \brief Specify that the current description of AS is finished
411 * Once you've declared all the content of your AS, you have to close
412 * it with this call. Your AS is not usable until you call this function.
414 * @fixme: this call is not as robust as wanted: bad things WILL happen
415 * if you call it twice for the same AS, or if you forget calling it, or
416 * even if you add stuff to a closed AS
419 void sg_platf_new_AS_close() {
421 if (current_routing == NULL) {
422 THROWF(arg_error, 0, "Close an AS, but none was under construction");
424 network_element_info_t info = NULL;
425 xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
426 "The AS \"%s\" already exists",current_routing->name);
427 info = xbt_new0(s_network_element_info_t, 1);
428 info->rc_component = current_routing->routing_father;
429 info->rc_type = SURF_NETWORK_ELEMENT_AS;
430 xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
432 (*(current_routing->routing->unload)) ();
433 (*(current_routing->routing->end)) ();
434 current_routing = current_routing->routing_father;
435 if (current_routing != NULL)
436 (*(current_routing->routing->load)) ();
440 /* Aux Business methods */
443 * \brief Get the AS name of the element
445 * \param name the host name
448 static char* elements_As_name(const char *name)
450 routing_component_t as_comp;
452 /* (1) find the as where the host is located */
453 as_comp = ((network_element_info_t)
454 xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
455 return as_comp->name;
460 * \brief Get the AS father and the first elements of the chain
462 * \param src the source host name
463 * \param dst the destination host name
465 * Get the common father of the to processing units, and the first different
466 * father in the chain
468 static void elements_father(const char *src, const char *dst,
469 routing_component_t *res_father,
470 routing_component_t *res_src,
471 routing_component_t *res_dst)
473 xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
474 #define ELEMENTS_FATHER_MAXDEPTH 16 /* increase if it is not enough */
475 routing_component_t src_as, dst_as;
476 routing_component_t path_src[ELEMENTS_FATHER_MAXDEPTH];
477 routing_component_t path_dst[ELEMENTS_FATHER_MAXDEPTH];
480 routing_component_t current;
481 routing_component_t current_src;
482 routing_component_t current_dst;
483 routing_component_t father;
485 /* (1) find the as where the src and dst are located */
486 network_element_info_t src_data = xbt_lib_get_or_null(host_lib, src,
488 network_element_info_t dst_data = xbt_lib_get_or_null(host_lib, dst,
491 src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
493 dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
494 src_as = src_data->rc_component;
495 dst_as = dst_data->rc_component;
497 xbt_assert(src_as && dst_as,
498 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src, dst);
500 /* (2) find the path to the root routing component */
501 for (current = src_as ; current != NULL ; current = current->routing_father) {
502 if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
503 xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
504 path_src[index_src++] = current;
506 for (current = dst_as ; current != NULL ; current = current->routing_father) {
507 if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
508 xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
509 path_dst[index_dst++] = current;
512 /* (3) find the common father */
514 current_src = path_src[--index_src];
515 current_dst = path_dst[--index_dst];
516 } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
518 /* (4) they are not in the same routing component, make the path */
519 if (current_src == current_dst)
520 father = current_src;
522 father = path_src[index_src + 1];
524 /* (5) result generation */
525 *res_father = father; /* first the common father of src and dst */
526 *res_src = current_src; /* second the first different father of src */
527 *res_dst = current_dst; /* three the first different father of dst */
529 #undef ELEMENTS_FATHER_MAXDEPTH
532 /* Global Business methods */
535 * \brief Recursive function for get_route_latency
537 * \param src the source host name
538 * \param dst the destination host name
539 * \param *e_route the route where the links are stored
540 * \param *latency the latency, if needed
542 * This function is called by "get_route" and "get_latency". It allows to walk
543 * recursively through the routing components tree.
545 static void _get_route_latency(const char *src, const char *dst,
546 xbt_dynar_t *route, double *latency)
548 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src, dst);
549 xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
551 routing_component_t common_father;
552 routing_component_t src_father;
553 routing_component_t dst_father;
554 elements_father(src, dst, &common_father, &src_father, &dst_father);
556 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
558 route_extended_t e_route = NULL;
560 e_route = common_father->get_route(common_father, src, dst);
561 xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
562 *route = e_route->generic_route.link_list;
565 *latency = common_father->get_latency(common_father, src, dst, e_route);
566 xbt_assert(*latency >= 0.0,
567 "latency error on route between \"%s\" and \"%s\"", src, dst);
570 xbt_free(e_route->src_gateway);
571 xbt_free(e_route->dst_gateway);
575 } else { /* SURF_ROUTING_RECURSIVE */
577 route_extended_t e_route_bypass = NULL;
578 if (common_father->get_bypass_route)
579 e_route_bypass = common_father->get_bypass_route(common_father, src, dst);
581 xbt_assert(!latency || !e_route_bypass,
582 "Bypass cannot work yet with get_latency");
584 route_extended_t e_route_cnt = e_route_bypass
586 : common_father->get_route(common_father,
587 src_father->name, dst_father->name);
589 xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
590 src_father->name, dst_father->name);
592 xbt_assert((e_route_cnt->src_gateway == NULL) ==
593 (e_route_cnt->dst_gateway == NULL),
594 "bad gateway for route between \"%s\" and \"%s\"", src, dst);
597 *route = xbt_dynar_new(global_routing->size_of_link, NULL);
600 *latency = common_father->get_latency(common_father,
601 src_father->name, dst_father->name,
603 xbt_assert(*latency >= 0.0,
604 "latency error on route between \"%s\" and \"%s\"",
605 src_father->name, dst_father->name);
611 if (strcmp(src, e_route_cnt->src_gateway)) {
613 xbt_dynar_t route_src;
615 _get_route_latency(src, e_route_cnt->src_gateway,
616 (route ? &route_src : NULL),
617 (latency ? &latency_src : NULL));
619 xbt_assert(route_src, "no route between \"%s\" and \"%s\"",
620 src, e_route_cnt->src_gateway);
621 xbt_dynar_foreach(route_src, cpt, link) {
622 xbt_dynar_push(*route, &link);
624 xbt_dynar_free(&route_src);
627 xbt_assert(latency_src >= 0.0,
628 "latency error on route between \"%s\" and \"%s\"",
629 src, e_route_cnt->src_gateway);
630 *latency += latency_src;
635 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
636 xbt_dynar_push(*route, &link);
640 if (strcmp(e_route_cnt->dst_gateway, dst)) {
642 xbt_dynar_t route_dst;
644 _get_route_latency(e_route_cnt->dst_gateway, dst,
645 (route ? &route_dst : NULL),
646 (latency ? &latency_dst : NULL));
648 xbt_assert(route_dst, "no route between \"%s\" and \"%s\"",
649 e_route_cnt->dst_gateway, dst);
650 xbt_dynar_foreach(route_dst, cpt, link) {
651 xbt_dynar_push(*route, &link);
653 xbt_dynar_free(&route_dst);
656 xbt_assert(latency_dst >= 0.0,
657 "latency error on route between \"%s\" and \"%s\"",
658 e_route_cnt->dst_gateway, dst);
659 *latency += latency_dst;
663 generic_free_extended_route(e_route_cnt);
668 * \brief Generic function for get_route, get_route_no_cleanup, and get_latency
670 static void get_route_latency(const char *src, const char *dst,
671 xbt_dynar_t *route, double *latency, int cleanup)
673 _get_route_latency(src, dst, route, latency);
674 xbt_assert(!route || *route, "no route between \"%s\" and \"%s\"", src, dst);
675 xbt_assert(!latency || *latency >= 0.0,
676 "latency error on route between \"%s\" and \"%s\"", src, dst);
678 xbt_dynar_free(&global_routing->last_route);
679 global_routing->last_route = cleanup ? *route : NULL;
684 * \brief Generic method: find a route between hosts
686 * \param src the source host name
687 * \param dst the destination host name
689 * walk through the routing components tree and find a route between hosts
690 * by calling the differents "get_route" functions in each routing component.
691 * No need to free the returned dynar. It will be freed at the next call.
693 static xbt_dynar_t get_route(const char *src, const char *dst)
695 xbt_dynar_t route = NULL;
696 get_route_latency(src, dst, &route, NULL, 1);
701 * \brief Generic method: find a route between hosts
703 * \param src the source host name
704 * \param dst the destination host name
706 * same as get_route, but return NULL if any exception is raised.
708 static xbt_dynar_t get_route_or_null(const char *src, const char *dst)
710 xbt_dynar_t route = NULL;
713 get_route_latency(src, dst, &route, NULL, 1);
715 xbt_ex_free(exception);
722 * \brief Generic method: find a route between hosts
724 * \param src the source host name
725 * \param dst the destination host name
727 * walk through the routing components tree and find a route between hosts
728 * by calling the differents "get_route" functions in each routing component.
729 * Leaves the caller the responsability to clean the returned dynar.
731 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
733 xbt_dynar_t route = NULL;
734 get_route_latency(src, dst, &route, NULL, 0);
739 static double get_latency(const char *src, const char *dst)
741 double latency = -1.0;
742 get_route_latency(src, dst, NULL, &latency, 0);
746 static int surf_parse_models_setup_already_called=0;
747 /* Call the last initialization functions, that must be called after the
748 * <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
750 void surf_parse_models_setup()
752 if (surf_parse_models_setup_already_called)
754 surf_parse_models_setup_already_called=1;
755 surf_config_models_setup();
760 * \brief Recursive function for finalize
762 * \param rc the source host name
764 * This fuction is call by "finalize". It allow to finalize the
765 * AS or routing components. It delete all the structures.
767 static void _finalize(routing_component_t rc)
770 xbt_dict_cursor_t cursor = NULL;
772 routing_component_t elem;
773 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
776 xbt_dict_t tmp_sons = rc->routing_sons;
777 char *tmp_name = rc->name;
778 xbt_dict_free(&tmp_sons);
780 xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
781 current_routing->name);
782 (*(rc->finalize)) (rc);
787 * \brief Generic method: delete all the routing structures
789 * walk through the routing components tree and delete the structures
790 * by calling the differents "finalize" functions in each routing component
792 static void finalize(void)
794 /* delete recursibly all the tree */
795 _finalize(global_routing->root);
796 /* delete last_route */
797 xbt_dynar_free(&(global_routing->last_route));
798 /* delete global routing structure */
799 xbt_free(global_routing);
800 /* make sure that we will reinit the models while loading the platf once reinited -- HACK but there is no proper surf_routing_init() */
801 surf_parse_models_setup_already_called = 0;
804 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
806 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
808 //adding my one link routes
811 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
813 xbt_dynar_foreach(onelink_mine, cpt, link) {
814 xbt_dynar_push(ret, &link);
819 xbt_dict_cursor_t cursor = NULL;
820 routing_component_t rc_child;
821 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
822 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
824 xbt_dynar_foreach(onelink_child, cpt, link) {
825 xbt_dynar_push(ret, &link);
832 static xbt_dynar_t get_onelink_routes(void)
834 return recursive_get_onelink_routes(global_routing->root);
837 e_surf_network_element_type_t get_network_element_type(const char
840 network_element_info_t rc = NULL;
842 rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
843 if(rc) return rc->rc_type;
845 rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
846 if(rc) return rc->rc_type;
848 return SURF_NETWORK_ELEMENT_NULL;
852 * \brief Generic method: create the global routing schema
854 * Make a global routing structure and set all the parsing functions.
856 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
858 /* config the uniq global routing */
859 global_routing = xbt_new0(s_routing_global_t, 1);
860 global_routing->root = NULL;
861 global_routing->get_route = get_route;
862 global_routing->get_route_or_null = get_route_or_null;
863 global_routing->get_latency = get_latency;
864 global_routing->get_route_no_cleanup = get_route_no_cleanup;
865 global_routing->get_onelink_routes = get_onelink_routes;
866 global_routing->get_route_latency = get_route_latency;
867 global_routing->get_network_element_type = get_network_element_type;
868 global_routing->finalize = finalize;
869 global_routing->loopback = loopback;
870 global_routing->size_of_link = size_of_links;
871 global_routing->last_route = NULL;
872 get_link_latency = get_link_latency_fun;
873 /* no current routing at moment */
874 current_routing = NULL;
876 /* parse generic elements */
877 sg_platf_host_add_cb(parse_S_host);
878 sg_platf_router_add_cb(parse_S_router);
880 surfxml_add_callback(STag_surfxml_route_cb_list,
881 &parse_S_route_new_and_endpoints_XML);
882 surfxml_add_callback(STag_surfxml_ASroute_cb_list,
883 &parse_S_ASroute_new_and_endpoints);
884 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
885 &parse_S_bypassRoute_new_and_endpoints);
887 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
888 &parse_E_link_ctn_new_elem_XML);
890 surfxml_add_callback(ETag_surfxml_route_cb_list,
891 &parse_E_route_store_route);
892 surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
893 &parse_E_ASroute_store_route);
894 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
895 &parse_E_bypassRoute_store_route);
897 surfxml_add_callback(STag_surfxml_cluster_cb_list,
898 &routing_parse_Scluster);
900 surfxml_add_callback(STag_surfxml_peer_cb_list,
901 &routing_parse_Speer);
903 sg_platf_postparse_add_cb(clean_routing_after_parse);
906 instr_routing_define_callbacks();
910 void surf_parse_add_callback_config(void)
912 surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
916 /* ************************************************** */
917 /* ********** PATERN FOR NEW ROUTING **************** */
919 /* The minimal configuration of a new routing model need the next functions,
920 * also you need to set at the start of the file, the new model in the model
921 * list. Remember keep the null ending of the list.
923 /*** Routing model structure ***/
925 // s_routing_component_t generic_routing;
926 // /* things that your routing model need */
927 // } s_routing_component_NEW_t,*routing_component_NEW_t;
929 /*** Parse routing model functions ***/
930 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
931 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
932 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
933 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
934 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
936 /*** Business methods ***/
937 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
938 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
939 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
941 /*** Creation routing model functions ***/
942 // static void* model_NEW_create(void) {
943 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
944 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
945 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
946 // new_component->generic_routing.set_route = model_NEW_set_route;
947 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
948 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
949 // new_component->generic_routing.get_route = NEW_get_route;
950 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
951 // new_component->generic_routing.finalize = NEW_finalize;
952 // /* initialization of internal structures */
953 // return new_component;
955 // static void model_NEW_load(void) {} /* mandatory */
956 // static void model_NEW_unload(void) {} /* mandatory */
957 // static void model_NEW_end(void) {} /* mandatory */
959 /* ************************************************************************** */
960 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
962 void generic_set_processing_unit(routing_component_t rc,
965 XBT_DEBUG("Load process unit \"%s\"", name);
966 int *id = xbt_new0(int, 1);
967 xbt_dict_t _to_index;
968 _to_index = current_routing->to_index;
969 *id = xbt_dict_length(_to_index);
970 xbt_dict_set(_to_index, name, id, xbt_free);
973 void generic_set_autonomous_system(routing_component_t rc,
976 XBT_DEBUG("Load Autonomous system \"%s\"", name);
977 int *id = xbt_new0(int, 1);
978 xbt_dict_t _to_index;
979 _to_index = current_routing->to_index;
980 *id = xbt_dict_length(_to_index);
981 xbt_dict_set(_to_index, name, id, xbt_free);
984 int surf_pointer_resource_cmp(const void *a, const void *b)
989 int surf_link_resource_cmp(const void *a, const void *b)
991 return !!memcmp(a,b,global_routing->size_of_link);
994 void generic_set_bypassroute(routing_component_t rc,
995 const char *src, const char *dst,
996 route_extended_t e_route)
998 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
999 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1002 route_name = bprintf("%s#%s", src, dst);
1003 xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
1004 "Invalid count of links, must be greater than zero (%s,%s)",
1006 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
1007 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
1008 src, e_route->src_gateway, dst, e_route->dst_gateway);
1010 route_extended_t new_e_route =
1011 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
1012 xbt_dynar_free(&(e_route->generic_route.link_list));
1015 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
1016 (void (*)(void *)) generic_free_extended_route);
1017 xbt_free(route_name);
1020 /* ************************************************************************** */
1021 /* *********************** GENERIC BUSINESS METHODS ************************* */
1023 double generic_get_link_latency(routing_component_t rc,
1024 const char *src, const char *dst,
1025 route_extended_t route)
1027 int need_to_clean = route?0:1;
1030 double latency = 0.0;
1032 route = route?route:rc->get_route(rc,src,dst);
1034 xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1035 latency += get_link_latency(link);
1037 if(need_to_clean) generic_free_extended_route(route);
1041 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1043 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1046 route_extended_t generic_get_bypassroute(routing_component_t rc,
1050 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1051 routing_component_t src_as, dst_as;
1052 int index_src, index_dst;
1053 xbt_dynar_t path_src = NULL;
1054 xbt_dynar_t path_dst = NULL;
1055 routing_component_t current = NULL;
1056 routing_component_t *current_src = NULL;
1057 routing_component_t *current_dst = NULL;
1059 /* (1) find the as where the src and dst are located */
1060 void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1061 void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1062 if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1063 if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1065 if(src_data == NULL || dst_data == NULL)
1066 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1067 src, dst, rc->name);
1069 src_as = ((network_element_info_t)src_data)->rc_component;
1070 dst_as = ((network_element_info_t)dst_data)->rc_component;
1072 /* (2) find the path to the root routing component */
1073 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1075 while (current != NULL) {
1076 xbt_dynar_push(path_src, ¤t);
1077 current = current->routing_father;
1079 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1081 while (current != NULL) {
1082 xbt_dynar_push(path_dst, ¤t);
1083 current = current->routing_father;
1086 /* (3) find the common father */
1087 index_src = path_src->used - 1;
1088 index_dst = path_dst->used - 1;
1089 current_src = xbt_dynar_get_ptr(path_src, index_src);
1090 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1091 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1092 xbt_dynar_pop_ptr(path_src);
1093 xbt_dynar_pop_ptr(path_dst);
1096 current_src = xbt_dynar_get_ptr(path_src, index_src);
1097 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1100 int max_index_src = path_src->used - 1;
1101 int max_index_dst = path_dst->used - 1;
1103 int max_index = max(max_index_src, max_index_dst);
1106 route_extended_t e_route_bypass = NULL;
1108 for (max = 0; max <= max_index; max++) {
1109 for (i = 0; i < max; i++) {
1110 if (i <= max_index_src && max <= max_index_dst) {
1111 char *route_name = bprintf("%s#%s",
1112 (*(routing_component_t *)
1114 (path_src, i)))->name,
1115 (*(routing_component_t *)
1117 (path_dst, max)))->name);
1119 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1120 xbt_free(route_name);
1124 if (max <= max_index_src && i <= max_index_dst) {
1125 char *route_name = bprintf("%s#%s",
1126 (*(routing_component_t *)
1128 (path_src, max)))->name,
1129 (*(routing_component_t *)
1131 (path_dst, i)))->name);
1133 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1134 xbt_free(route_name);
1143 if (max <= max_index_src && max <= max_index_dst) {
1144 char *route_name = bprintf("%s#%s",
1145 (*(routing_component_t *)
1147 (path_src, max)))->name,
1148 (*(routing_component_t *)
1150 (path_dst, max)))->name);
1151 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1152 xbt_free(route_name);
1158 xbt_dynar_free(&path_src);
1159 xbt_dynar_free(&path_dst);
1161 route_extended_t new_e_route = NULL;
1163 if (e_route_bypass) {
1165 unsigned int cpt = 0;
1166 new_e_route = xbt_new0(s_route_extended_t, 1);
1167 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1168 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1169 new_e_route->generic_route.link_list =
1170 xbt_dynar_new(global_routing->size_of_link, NULL);
1171 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1172 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1179 /* ************************************************************************** */
1180 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1183 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1184 void *data, int order)
1190 xbt_dynar_t links = NULL, links_id = NULL;
1192 new_route = xbt_new0(s_route_t, 1);
1193 new_route->link_list =
1194 xbt_dynar_new(global_routing->size_of_link, NULL);
1196 xbt_assert(hierarchy == SURF_ROUTING_BASE,
1197 "the hierarchy type is not SURF_ROUTING_BASE");
1199 links = ((route_t) data)->link_list;
1202 links_id = new_route->link_list;
1204 xbt_dynar_foreach(links, cpt, link_name) {
1207 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1210 xbt_dynar_push(links_id, &link);
1212 xbt_dynar_unshift(links_id, &link);
1214 THROWF(mismatch_error, 0, "Link %s not found", link_name);
1221 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1222 void *data, int order)
1226 route_extended_t e_route, new_e_route;
1229 xbt_dynar_t links = NULL, links_id = NULL;
1231 new_e_route = xbt_new0(s_route_extended_t, 1);
1232 new_e_route->generic_route.link_list =
1233 xbt_dynar_new(global_routing->size_of_link, NULL);
1234 new_e_route->src_gateway = NULL;
1235 new_e_route->dst_gateway = NULL;
1237 xbt_assert(hierarchy == SURF_ROUTING_BASE
1238 || hierarchy == SURF_ROUTING_RECURSIVE,
1239 "the hierarchy type is not defined");
1241 if (hierarchy == SURF_ROUTING_BASE) {
1243 route = (route_t) data;
1244 links = route->link_list;
1246 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1248 e_route = (route_extended_t) data;
1249 xbt_assert(e_route->src_gateway
1250 && e_route->dst_gateway, "bad gateway, is null");
1251 links = e_route->generic_route.link_list;
1253 /* remeber not erase the gateway names */
1254 new_e_route->src_gateway = strdup(e_route->src_gateway);
1255 new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1258 links_id = new_e_route->generic_route.link_list;
1260 xbt_dynar_foreach(links, cpt, link_name) {
1263 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1266 xbt_dynar_push(links_id, &link);
1268 xbt_dynar_unshift(links_id, &link);
1270 THROWF(mismatch_error, 0, "Link %s not found", link_name);
1276 void generic_free_route(route_t route)
1279 xbt_dynar_free(&(route->link_list));
1284 void generic_free_extended_route(route_extended_t e_route)
1287 xbt_dynar_free(&(e_route->generic_route.link_list));
1288 if (e_route->src_gateway)
1289 xbt_free(e_route->src_gateway);
1290 if (e_route->dst_gateway)
1291 xbt_free(e_route->dst_gateway);
1296 static routing_component_t generic_as_exist(routing_component_t find_from,
1297 routing_component_t to_find)
1299 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1300 xbt_dict_cursor_t cursor = NULL;
1303 routing_component_t elem;
1304 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1305 if (to_find == elem || generic_as_exist(elem, to_find)) {
1316 generic_autonomous_system_exist(routing_component_t rc, char *element)
1318 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1319 routing_component_t element_as, result, elem;
1320 xbt_dict_cursor_t cursor = NULL;
1322 element_as = ((network_element_info_t)
1323 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1324 result = ((routing_component_t) - 1);
1325 if (element_as != rc)
1326 result = generic_as_exist(rc, element_as);
1330 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1331 found = !strcmp(elem->name, element);
1342 generic_processing_units_exist(routing_component_t rc, char *element)
1344 routing_component_t element_as;
1345 element_as = ((network_element_info_t)
1346 xbt_lib_get_or_null(host_lib,
1347 element, ROUTING_HOST_LEVEL))->rc_component;
1348 if (element_as == rc)
1350 return generic_as_exist(rc, element_as);
1353 void generic_src_dst_check(routing_component_t rc, const char *src,
1357 void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1358 void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1359 if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1360 if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1362 if(src_data == NULL || dst_data == NULL)
1363 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1364 src, dst, rc->name);
1366 routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1367 routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1369 if(src_as != dst_as)
1370 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1371 src, src_as->name, dst, dst_as->name);
1373 xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1374 src,dst,src_as->name, dst_as->name,rc->name);
1377 void routing_parse_Scluster(void)
1379 if(!cluster_host_link)
1380 cluster_host_link = xbt_dict_new();
1382 static int AX_ptr = 0;
1383 char *host_id, *groups, *link_id = NULL;
1385 s_sg_platf_host_cbarg_t host;
1386 s_sg_platf_link_cbarg_t link;
1388 if( strcmp(struct_cluster->V_cluster_availability_file,"")
1389 || strcmp(struct_cluster->V_cluster_state_file,"") )
1391 if(xbt_dict_size(patterns)==0)
1392 patterns = xbt_dict_new();
1393 xbt_dict_set(patterns,"id",struct_cluster->V_cluster_id,NULL);
1394 xbt_dict_set(patterns,"prefix",struct_cluster->V_cluster_prefix,NULL);
1395 xbt_dict_set(patterns,"suffix",struct_cluster->V_cluster_suffix,NULL);
1400 xbt_dynar_t radical_elements;
1401 xbt_dynar_t radical_ends;
1403 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1404 static unsigned int surfxml_buffer_stack_stack[1024];
1406 surfxml_buffer_stack_stack[0] = 0;
1407 surfxml_bufferstack_push(1);
1409 SURFXML_BUFFER_SET(AS_id, struct_cluster->V_cluster_id);
1410 SURFXML_BUFFER_SET(AS_routing, "Cluster");
1411 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->V_cluster_id);
1412 SURFXML_START_TAG(AS);
1415 radical_elements = xbt_str_split(struct_cluster->V_cluster_radical, ",");
1416 xbt_dynar_foreach(radical_elements, iter, groups) {
1417 memset(&host,0,sizeof(host));
1419 radical_ends = xbt_str_split(groups, "-");
1420 switch (xbt_dynar_length(radical_ends)) {
1422 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1423 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, start, struct_cluster->V_cluster_suffix);
1424 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, start);
1426 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1428 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1429 xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1430 char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1431 xbt_str_varsubst(tmp_availability_file,patterns);
1432 XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1433 host.power_trace = tmgr_trace_new(tmp_availability_file);
1434 xbt_free(tmp_availability_file);
1438 XBT_DEBUG("\tavailability_file=\"\"");
1440 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1441 char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1442 xbt_str_varsubst(tmp_state_file,patterns);
1443 XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1444 host.state_trace = tmgr_trace_new(tmp_state_file);
1445 xbt_free(tmp_state_file);
1449 XBT_DEBUG("\tstate_file=\"\"");
1452 host.power_peak = struct_cluster->S_cluster_power;
1453 host.power_scale = 1.0;
1454 host.core_amount = struct_cluster->S_cluster_core;
1455 host.initial_state = SURF_RESOURCE_ON;
1457 sg_platf_new_host(&host);
1458 XBT_DEBUG("</host>");
1461 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1462 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1463 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
1464 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1465 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
1467 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1469 memset(&link,0,sizeof(link));
1471 link.bandwidth = struct_cluster->S_cluster_bw;
1472 link.latency = struct_cluster->S_cluster_lat;
1473 link.state = SURF_RESOURCE_ON;
1475 switch (A_surfxml_link_sharing_policy) {
1476 case A_surfxml_link_sharing_policy_SHARED:
1477 link.policy = SURF_LINK_SHARED;
1479 case A_surfxml_link_sharing_policy_FATPIPE:
1480 link.policy = SURF_LINK_FATPIPE;
1482 case A_surfxml_link_sharing_policy_FULLDUPLEX:
1483 link.policy = SURF_LINK_FULLDUPLEX;
1485 case AU_surfxml_link_sharing_policy:
1486 surf_parse_error(bprintf("Invalid sharing policy in cluster %s (please report this bug, this shouldn't happen)",struct_cluster->V_cluster_id));
1489 sg_platf_new_link(&link);
1491 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1492 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
1493 char* tmp_link = bprintf("%s_UP",link_id);
1494 info->link_up = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1496 tmp_link = bprintf("%s_DOWN",link_id);
1497 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1501 info->link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1502 info->link_down = info->link_up;
1504 xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1512 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1513 end= surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1514 for (i = start; i <= end; i++) {
1515 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, i, struct_cluster->V_cluster_suffix);
1516 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, i);
1518 A_surfxml_host_state = A_surfxml_host_state_ON;
1520 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1522 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1523 xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1524 char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1525 xbt_str_varsubst(tmp_availability_file,patterns);
1526 XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1527 host.power_trace = tmgr_trace_new(tmp_availability_file);
1528 xbt_free(tmp_availability_file);
1532 XBT_DEBUG("\tavailability_file=\"\"");
1534 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1535 char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1536 xbt_str_varsubst(tmp_state_file,patterns);
1537 XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1538 host.state_trace = tmgr_trace_new(tmp_state_file);
1539 xbt_free(tmp_state_file);
1543 XBT_DEBUG("\tstate_file=\"\"");
1546 host.power_peak = struct_cluster->S_cluster_power;
1547 host.power_scale = 1.0;
1548 host.core_amount = struct_cluster->S_cluster_core;
1549 host.initial_state = SURF_RESOURCE_ON;
1551 sg_platf_new_host(&host);
1552 XBT_DEBUG("</host>");
1554 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1555 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1556 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
1557 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1558 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
1560 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1562 memset(&link,0,sizeof(link));
1564 link.bandwidth = struct_cluster->S_cluster_bw;
1565 link.latency = struct_cluster->S_cluster_lat;
1566 link.state = SURF_RESOURCE_ON;
1568 switch (struct_cluster->V_cluster_sharing_policy) {
1569 case A_surfxml_cluster_sharing_policy_SHARED:
1570 link.policy = SURF_LINK_SHARED;
1572 case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1573 link.policy = SURF_LINK_FULLDUPLEX;
1575 case A_surfxml_cluster_sharing_policy_FATPIPE:
1576 link.policy = SURF_LINK_FATPIPE;
1579 surf_parse_error(bprintf("Invalid cluster sharing policy for cluster %s",struct_cluster->V_cluster_id));
1581 sg_platf_new_link(&link);
1583 ETag_surfxml_link();
1585 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1586 if (link.policy == SURF_LINK_FULLDUPLEX) {
1587 char* tmp_link = bprintf("%s_UP",link_id);
1588 info->link_up = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1590 tmp_link = bprintf("%s_DOWN",link_id);
1591 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1595 info->link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1596 info->link_down = info->link_up;
1598 xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1606 XBT_DEBUG("Malformed radical");
1610 xbt_dynar_free(&radical_ends);
1612 xbt_dynar_free(&radical_elements);
1616 XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->S_cluster_router_id);
1617 SURFXML_BUFFER_SET(router_id, struct_cluster->S_cluster_router_id);
1618 SURFXML_BUFFER_SET(router_coordinates, "");
1619 SURFXML_START_TAG(router);
1620 SURFXML_END_TAG(router);
1623 if( (struct_cluster->S_cluster_bb_bw!= 0) && (struct_cluster->S_cluster_bb_lat!=0) ){
1624 char *link_backbone = bprintf("%s_backbone", struct_cluster->V_cluster_id);
1625 XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->S_cluster_bb_bw, struct_cluster->S_cluster_bb_lat);
1627 memset(&link,0,sizeof(link));
1628 link.id = link_backbone;
1629 link.bandwidth = struct_cluster->S_cluster_bb_bw;
1630 link.latency = struct_cluster->S_cluster_bb_lat;
1631 link.state = SURF_RESOURCE_ON;
1633 switch (AX_surfxml_cluster_bb_sharing_policy) {
1634 case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
1635 link.policy = SURF_LINK_FATPIPE;
1637 case A_surfxml_cluster_bb_sharing_policy_SHARED:
1638 link.policy = SURF_LINK_SHARED;
1641 surf_parse_error(bprintf("Invalid bb sharing policy in cluster %s",struct_cluster->V_cluster_id));
1644 sg_platf_new_link(&link);
1645 ETag_surfxml_link();
1647 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1648 info->link_up = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1649 info->link_down = info->link_up;
1650 xbt_dict_set(cluster_host_link,struct_cluster->V_cluster_id,info,xbt_free);
1651 free(link_backbone);
1656 char *new_suffix = xbt_strdup("");
1658 radical_elements = xbt_str_split(struct_cluster->V_cluster_suffix, ".");
1659 xbt_dynar_foreach(radical_elements, iter, groups) {
1660 if (strcmp(groups, "")) {
1661 char *old_suffix = new_suffix;
1662 new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1667 xbt_dynar_free(&radical_elements);
1668 xbt_free(new_suffix);
1670 if( strcmp(struct_cluster->V_cluster_availability_file,"")
1671 || strcmp(struct_cluster->V_cluster_state_file,"") )
1672 xbt_dict_free(&patterns);
1675 SURFXML_END_TAG(AS);
1678 surfxml_bufferstack_pop(1);
1681 * This function take a string and replace parameters from patterns dict.
1682 * It returns the new value.
1684 static char* replace_random_parameter(char * string)
1686 char *test_string = NULL;
1688 if(xbt_dict_size(random_value)==0)
1691 string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1692 test_string = bprintf("${%s}", string);
1693 test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1695 if (strcmp(test_string,"")) { //if not empty, keep this value.
1697 string = test_string;
1698 } //In other case take old value (without ${})
1704 static void clean_routing_after_parse(void)
1706 xbt_dict_free(&random_value);
1707 xbt_dict_free(&patterns);
1710 static void routing_parse_Speer(void)
1712 static int AX_ptr = 0;
1713 char *host_id = NULL;
1714 char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1716 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1717 static unsigned int surfxml_buffer_stack_stack[1024];
1719 surfxml_buffer_stack_stack[0] = 0;
1721 surfxml_bufferstack_push(1);
1723 SURFXML_BUFFER_SET(AS_id, struct_peer->V_peer_id);
1725 SURFXML_BUFFER_SET(AS_routing, "Full");
1726 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", struct_peer->V_peer_id);
1728 SURFXML_START_TAG(AS);
1731 host_id = HOST_PEER(struct_peer->V_peer_id);
1732 router_id = ROUTER_PEER(struct_peer->V_peer_id);
1733 link_id_up = LINK_UP_PEER(struct_peer->V_peer_id);
1734 link_id_down = LINK_DOWN_PEER(struct_peer->V_peer_id);
1736 link_router = bprintf("%s_link_router", struct_peer->V_peer_id);
1737 link_backbone = bprintf("%s_backbone", struct_peer->V_peer_id);
1739 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, struct_peer->V_peer_power);
1740 A_surfxml_host_state = A_surfxml_host_state_ON;
1741 SURFXML_BUFFER_SET(host_id, host_id);
1742 SURFXML_BUFFER_SET(host_power, struct_peer->V_peer_power);
1743 SURFXML_BUFFER_SET(host_availability, "1.0");
1744 SURFXML_BUFFER_SET(host_availability_file, struct_peer->V_peer_availability_trace);
1745 SURFXML_BUFFER_SET(host_state_file, struct_peer->V_peer_state_trace);
1746 SURFXML_BUFFER_SET(host_coordinates, "");
1747 SURFXML_BUFFER_SET(host_core, "1.0");
1748 SURFXML_START_TAG(host);
1749 SURFXML_END_TAG(host);
1751 XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, struct_peer->V_peer_coord);
1752 SURFXML_BUFFER_SET(router_id, router_id);
1753 SURFXML_BUFFER_SET(router_coordinates, struct_peer->V_peer_coord);
1754 SURFXML_START_TAG(router);
1755 SURFXML_END_TAG(router);
1757 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, struct_peer->V_peer_bw_in, struct_peer->V_peer_lat);
1758 A_surfxml_link_state = A_surfxml_link_state_ON;
1759 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1760 SURFXML_BUFFER_SET(link_id, link_id_up);
1761 SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_in);
1762 SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1763 SURFXML_BUFFER_SET(link_bandwidth_file, "");
1764 SURFXML_BUFFER_SET(link_latency_file, "");
1765 SURFXML_BUFFER_SET(link_state_file, "");
1766 SURFXML_START_TAG(link);
1767 SURFXML_END_TAG(link);
1769 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, struct_peer->V_peer_bw_out, struct_peer->V_peer_lat);
1770 A_surfxml_link_state = A_surfxml_link_state_ON;
1771 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1772 SURFXML_BUFFER_SET(link_id, link_id_down);
1773 SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_out);
1774 SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1775 SURFXML_BUFFER_SET(link_bandwidth_file, "");
1776 SURFXML_BUFFER_SET(link_latency_file, "");
1777 SURFXML_BUFFER_SET(link_state_file, "");
1778 SURFXML_START_TAG(link);
1779 SURFXML_END_TAG(link);
1784 XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1785 XBT_DEBUG("symmetrical=\"NO\">");
1786 SURFXML_BUFFER_SET(route_src, host_id);
1787 SURFXML_BUFFER_SET(route_dst, router_id);
1788 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1789 SURFXML_START_TAG(route);
1791 XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1792 SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1793 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1794 SURFXML_START_TAG(link_ctn);
1795 SURFXML_END_TAG(link_ctn);
1797 XBT_DEBUG("</route>");
1798 SURFXML_END_TAG(route);
1801 XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1802 XBT_DEBUG("symmetrical=\"NO\">");
1803 SURFXML_BUFFER_SET(route_src, router_id);
1804 SURFXML_BUFFER_SET(route_dst, host_id);
1805 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1806 SURFXML_START_TAG(route);
1808 XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1809 SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1810 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1811 SURFXML_START_TAG(link_ctn);
1812 SURFXML_END_TAG(link_ctn);
1814 XBT_DEBUG("</route>");
1815 SURFXML_END_TAG(route);
1818 SURFXML_END_TAG(AS);
1821 //xbt_dynar_free(&tab_elements_num);
1825 free(link_backbone);
1828 surfxml_bufferstack_pop(1);
1831 static void routing_parse_Srandom(void)
1833 double mean, std, min, max, seed;
1834 char *random_id = A_surfxml_random_id;
1835 char *random_radical = A_surfxml_random_radical;
1836 char *rd_name = NULL;
1838 mean = surf_parse_get_double(A_surfxml_random_mean);
1839 std = surf_parse_get_double(A_surfxml_random_std_deviation);
1840 min = surf_parse_get_double(A_surfxml_random_min);
1841 max = surf_parse_get_double(A_surfxml_random_max);
1842 seed = surf_parse_get_double(A_surfxml_random_seed);
1846 random_data_t random = xbt_new0(s_random_data_t, 1);
1849 xbt_dynar_t radical_elements;
1853 xbt_dynar_t radical_ends;
1855 random->generator = A_surfxml_random_generator;
1856 random->seed = seed;
1860 /* Check user stupidities */
1862 THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1864 THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1867 THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1870 /* normalize the mean and standard deviation before storing */
1871 random->mean = (mean - min) / (max - min);
1872 random->std = std / (max - min);
1874 if (random->mean * (1 - random->mean) < random->std * random->std)
1875 THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1876 random->mean, random->std);
1878 XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1888 if(xbt_dict_size(random_value)==0)
1889 random_value = xbt_dict_new();
1891 if(!strcmp(random_radical,""))
1893 res = random_generate(random);
1894 rd_value = bprintf("%f",res);
1895 xbt_dict_set(random_value, random_id, rd_value, free);
1899 radical_elements = xbt_str_split(random_radical, ",");
1900 xbt_dynar_foreach(radical_elements, iter, groups) {
1901 radical_ends = xbt_str_split(groups, "-");
1902 switch (xbt_dynar_length(radical_ends)) {
1904 xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
1905 res = random_generate(random);
1906 tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
1907 xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1912 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1913 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1914 for (i = start; i <= end; i++) {
1915 xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
1916 res = random_generate(random);
1917 tmpbuf = bprintf("%s%d",random_id,i);
1918 xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1923 XBT_INFO("Malformed radical");
1926 res = random_generate(random);
1927 rd_name = bprintf("%s_router",random_id);
1928 rd_value = bprintf("%f",res);
1929 xbt_dict_set(random_value, rd_name, rd_value, free);
1931 xbt_dynar_free(&radical_ends);
1934 xbt_dynar_free(&radical_elements);
1940 * Set a new link on the actual list of link for a route or ASroute
1942 void routing_add_link(const char *link_id)
1944 parse_E_link_c_ctn_new_elem_lua((char *) link_id);
1948 *Set the endpoints for a route
1950 void routing_set_route(const char *src_id, const char *dst_id)
1952 parse_S_route_new_and_endpoints_lua(src_id, dst_id);
1956 * Store the route by calling parse_E_route_store_route
1958 void routing_store_route(void)
1960 parse_E_route_store_route();