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->V_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->V_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->V_host_id,ROUTING_HOST_LEVEL,(void *) info);
119 if (host->V_host_coord && strcmp(host->V_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->V_host_coord, " ");
122 xbt_dynar_shrink(ctn, 0);
123 xbt_lib_set(host_lib,host->V_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->V_router_id, ROUTING_ASR_LEVEL),
136 "Reading a router, processing unit \"%s\" already exists",
137 router->V_router_id);
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,
142 router->V_router_id);
143 info = xbt_new0(s_network_element_info_t, 1);
144 info->rc_component = current_routing;
145 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
147 xbt_lib_set(as_router_lib,router->V_router_id,ROUTING_ASR_LEVEL,(void *) info);
148 if (strcmp(A_surfxml_router_coordinates,"")) {
149 if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
150 xbt_dynar_t ctn = xbt_str_split_str(A_surfxml_router_coordinates, " ");
151 xbt_dynar_shrink(ctn, 0);
152 xbt_lib_set(as_router_lib,router->V_router_id,COORD_ASR_LEVEL,(void *) ctn);
157 * brief Add a "router" to the network element list from XML description
159 static void parse_S_router_lua(const char* router_id) {
160 s_sg_platf_router_cbarg_t router;
161 memset(&router,0,sizeof(router));
162 router.V_router_id = router_id;
163 router.V_router_coord = "";
164 return parse_S_router(&router);
168 * \brief Set the endponints for a route
170 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
172 if (src != NULL && dst != NULL && link_list != NULL)
173 THROWF(arg_error, 0, "Route between %s to %s can not be defined",
177 xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
178 "Some limits are null in the route between \"%s\" and \"%s\"",
180 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
184 * \brief Set the endpoints for a route from XML
186 static void parse_S_route_new_and_endpoints_XML(void)
188 parse_S_route_new_and_endpoints(A_surfxml_route_src,
189 A_surfxml_route_dst);
193 * \brief Set the endpoints for a route from lua
195 static void parse_S_route_new_and_endpoints_lua(const char *id_src, const char *id_dst)
197 parse_S_route_new_and_endpoints(id_src, id_dst);
201 * \brief Set the endponints and gateways for a ASroute
203 static void parse_S_ASroute_new_and_endpoints(void)
205 if (src != NULL && dst != NULL && link_list != NULL)
206 THROWF(arg_error, 0, "Route between %s to %s can not be defined",
207 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
208 src = A_surfxml_ASroute_src;
209 dst = A_surfxml_ASroute_dst;
210 gw_src = A_surfxml_ASroute_gw_src;
211 gw_dst = A_surfxml_ASroute_gw_dst;
212 xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
213 || strlen(gw_dst) > 0,
214 "Some limits are null in the route between \"%s\" and \"%s\"",
216 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
220 * \brief Set the endponints for a bypassRoute
222 static void parse_S_bypassRoute_new_and_endpoints(void)
224 if (src != NULL && dst != NULL && link_list != NULL)
226 "Bypass Route between %s to %s can not be defined",
227 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
228 src = A_surfxml_bypassRoute_src;
229 dst = A_surfxml_bypassRoute_dst;
230 gw_src = A_surfxml_bypassRoute_gw_src;
231 gw_dst = A_surfxml_bypassRoute_gw_dst;
232 xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
233 || strlen(gw_dst) > 0,
234 "Some limits are null in the route between \"%s\" and \"%s\"",
236 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
240 * \brief Set a new link on the actual list of link for a route or ASroute
242 static void parse_E_link_ctn_new_elem(const char *link_id)
245 val = xbt_strdup(link_id);
246 xbt_dynar_push(link_list, &val);
250 * \brief Set a new link on the actual list of link for a route or ASroute from XML
253 static void parse_E_link_ctn_new_elem_XML(void)
255 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
256 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
257 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP) {
258 char *link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
259 parse_E_link_ctn_new_elem(link_id);
262 if (A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN) {
263 char *link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
264 parse_E_link_ctn_new_elem(link_id);
270 * \brief Set a new link on the actual list of link for a route or ASroute from lua
272 static void parse_E_link_c_ctn_new_elem_lua(const char *link_id)
274 parse_E_link_ctn_new_elem(link_id);
278 * \brief Store the route by calling the set_route function of the current routing component
280 static void parse_E_route_store_route(void)
282 name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
283 route->generic_route.link_list = link_list;
284 xbt_assert(current_routing->set_route,
285 "no defined method \"set_route\" in \"%s\"",
286 current_routing->name);
287 (*(current_routing->set_route)) (current_routing, src, dst, route);
294 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
296 static void parse_E_ASroute_store_route(void)
298 name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
299 e_route->generic_route.link_list = link_list;
300 e_route->src_gateway = xbt_strdup(gw_src);
301 e_route->dst_gateway = xbt_strdup(gw_dst);
302 xbt_assert(current_routing->set_ASroute,
303 "no defined method \"set_ASroute\" in \"%s\"",
304 current_routing->name);
305 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
314 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
316 static void parse_E_bypassRoute_store_route(void)
318 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
319 e_route->generic_route.link_list = link_list;
320 e_route->src_gateway = xbt_strdup(gw_src);
321 e_route->dst_gateway = xbt_strdup(gw_dst);
322 xbt_assert(current_routing->set_bypassroute,
323 "no defined method \"set_bypassroute\" in \"%s\"",
324 current_routing->name);
325 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
335 * \brief Make a new routing component to the platform
337 * Add a new autonomous system to the platform. Any elements (such as host,
338 * router or sub-AS) added after this call and before the corresponding call
339 * to sg_platf_new_AS_close() will be added to this AS.
341 * Once this function was called, the configuration concerning the used
342 * models cannot be changed anymore.
344 * @param AS_id name of this autonomous system. Must be uniq in the platform
345 * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
347 void sg_platf_new_AS_open(const char *AS_id, const char *wanted_routing_type)
349 routing_component_t new_routing;
350 model_type_t model = NULL;
353 surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag and before the first <AS>-like */
355 /* search the routing model */
356 for (cpt = 0; routing_models[cpt].name; cpt++)
357 if (!strcmp(wanted_routing_type, routing_models[cpt].name))
358 model = &routing_models[cpt];
359 /* if its not exist, error */
361 fprintf(stderr, "Routing model %s not found. Existing models:\n",
362 wanted_routing_type);
363 for (cpt = 0; routing_models[cpt].name; cpt++)
364 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
365 routing_models[cpt].desc);
369 /* make a new routing component */
370 new_routing = (routing_component_t) (*(model->create)) ();
371 new_routing->routing = model;
372 new_routing->hierarchy = SURF_ROUTING_NULL;
373 new_routing->name = xbt_strdup(AS_id);
374 new_routing->routing_sons = xbt_dict_new();
376 if (current_routing == NULL && global_routing->root == NULL) {
378 /* it is the first one */
379 new_routing->routing_father = NULL;
380 global_routing->root = new_routing;
382 } else if (current_routing != NULL && global_routing->root != NULL) {
384 xbt_assert(!xbt_dict_get_or_null
385 (current_routing->routing_sons, AS_id),
386 "The AS \"%s\" already exists", AS_id);
387 /* it is a part of the tree */
388 new_routing->routing_father = current_routing;
389 /* set the father behavior */
390 if (current_routing->hierarchy == SURF_ROUTING_NULL)
391 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
392 /* add to the sons dictionary */
393 xbt_dict_set(current_routing->routing_sons, AS_id,
394 (void *) new_routing, NULL);
395 /* add to the father element list */
396 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
397 /* unload the prev parse rules */
398 (*(current_routing->routing->unload)) ();
401 THROWF(arg_error, 0, "All defined components must be belong to a AS");
403 /* set the new parse rules */
404 (*(new_routing->routing->load)) ();
405 /* set the new current component of the tree */
406 current_routing = new_routing;
410 * \brief Specify that the current description of AS is finished
412 * Once you've declared all the content of your AS, you have to close
413 * it with this call. Your AS is not usable until you call this function.
415 * @fixme: this call is not as robust as wanted: bad things WILL happen
416 * if you call it twice for the same AS, or if you forget calling it, or
417 * even if you add stuff to a closed AS
420 void sg_platf_new_AS_close() {
422 if (current_routing == NULL) {
423 THROWF(arg_error, 0, "Close an AS, but none was under construction");
425 network_element_info_t info = NULL;
426 xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
427 "The AS \"%s\" already exists",current_routing->name);
428 info = xbt_new0(s_network_element_info_t, 1);
429 info->rc_component = current_routing->routing_father;
430 info->rc_type = SURF_NETWORK_ELEMENT_AS;
431 xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
433 (*(current_routing->routing->unload)) ();
434 (*(current_routing->routing->end)) ();
435 current_routing = current_routing->routing_father;
436 if (current_routing != NULL)
437 (*(current_routing->routing->load)) ();
441 /* Aux Business methods */
444 * \brief Get the AS name of the element
446 * \param name the host name
449 static char* elements_As_name(const char *name)
451 routing_component_t as_comp;
453 /* (1) find the as where the host is located */
454 as_comp = ((network_element_info_t)
455 xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
456 return as_comp->name;
461 * \brief Get the AS father and the first elements of the chain
463 * \param src the source host name
464 * \param dst the destination host name
466 * Get the common father of the to processing units, and the first different
467 * father in the chain
469 static void elements_father(const char *src, const char *dst,
470 routing_component_t *res_father,
471 routing_component_t *res_src,
472 routing_component_t *res_dst)
474 xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
475 #define ELEMENTS_FATHER_MAXDEPTH 16 /* increase if it is not enough */
476 routing_component_t src_as, dst_as;
477 routing_component_t path_src[ELEMENTS_FATHER_MAXDEPTH];
478 routing_component_t path_dst[ELEMENTS_FATHER_MAXDEPTH];
481 routing_component_t current;
482 routing_component_t current_src;
483 routing_component_t current_dst;
484 routing_component_t father;
486 /* (1) find the as where the src and dst are located */
487 network_element_info_t src_data = xbt_lib_get_or_null(host_lib, src,
489 network_element_info_t dst_data = xbt_lib_get_or_null(host_lib, dst,
492 src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
494 dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
495 src_as = src_data->rc_component;
496 dst_as = dst_data->rc_component;
498 xbt_assert(src_as && dst_as,
499 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src, dst);
501 /* (2) find the path to the root routing component */
502 for (current = src_as ; current != NULL ; current = current->routing_father) {
503 if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
504 xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
505 path_src[index_src++] = current;
507 for (current = dst_as ; current != NULL ; current = current->routing_father) {
508 if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
509 xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
510 path_dst[index_dst++] = current;
513 /* (3) find the common father */
515 current_src = path_src[--index_src];
516 current_dst = path_dst[--index_dst];
517 } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
519 /* (4) they are not in the same routing component, make the path */
520 if (current_src == current_dst)
521 father = current_src;
523 father = path_src[index_src + 1];
525 /* (5) result generation */
526 *res_father = father; /* first the common father of src and dst */
527 *res_src = current_src; /* second the first different father of src */
528 *res_dst = current_dst; /* three the first different father of dst */
530 #undef ELEMENTS_FATHER_MAXDEPTH
533 /* Global Business methods */
536 * \brief Recursive function for get_route_latency
538 * \param src the source host name
539 * \param dst the destination host name
540 * \param *e_route the route where the links are stored
541 * \param *latency the latency, if needed
543 * This function is called by "get_route" and "get_latency". It allows to walk
544 * recursively through the routing components tree.
546 static void _get_route_latency(const char *src, const char *dst,
547 xbt_dynar_t *route, double *latency)
549 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src, dst);
550 xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
552 routing_component_t common_father;
553 routing_component_t src_father;
554 routing_component_t dst_father;
555 elements_father(src, dst, &common_father, &src_father, &dst_father);
557 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
559 route_extended_t e_route = NULL;
561 e_route = common_father->get_route(common_father, src, dst);
562 xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
563 *route = e_route->generic_route.link_list;
566 *latency = common_father->get_latency(common_father, src, dst, e_route);
567 xbt_assert(*latency >= 0.0,
568 "latency error on route between \"%s\" and \"%s\"", src, dst);
571 xbt_free(e_route->src_gateway);
572 xbt_free(e_route->dst_gateway);
576 } else { /* SURF_ROUTING_RECURSIVE */
578 route_extended_t e_route_bypass = NULL;
579 if (common_father->get_bypass_route)
580 e_route_bypass = common_father->get_bypass_route(common_father, src, dst);
582 xbt_assert(!latency || !e_route_bypass,
583 "Bypass cannot work yet with get_latency");
585 route_extended_t e_route_cnt = e_route_bypass
587 : common_father->get_route(common_father,
588 src_father->name, dst_father->name);
590 xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
591 src_father->name, dst_father->name);
593 xbt_assert((e_route_cnt->src_gateway == NULL) ==
594 (e_route_cnt->dst_gateway == NULL),
595 "bad gateway for route between \"%s\" and \"%s\"", src, dst);
598 *route = xbt_dynar_new(global_routing->size_of_link, NULL);
601 *latency = common_father->get_latency(common_father,
602 src_father->name, dst_father->name,
604 xbt_assert(*latency >= 0.0,
605 "latency error on route between \"%s\" and \"%s\"",
606 src_father->name, dst_father->name);
612 if (strcmp(src, e_route_cnt->src_gateway)) {
614 xbt_dynar_t route_src;
616 _get_route_latency(src, e_route_cnt->src_gateway,
617 (route ? &route_src : NULL),
618 (latency ? &latency_src : NULL));
620 xbt_assert(route_src, "no route between \"%s\" and \"%s\"",
621 src, e_route_cnt->src_gateway);
622 xbt_dynar_foreach(route_src, cpt, link) {
623 xbt_dynar_push(*route, &link);
625 xbt_dynar_free(&route_src);
628 xbt_assert(latency_src >= 0.0,
629 "latency error on route between \"%s\" and \"%s\"",
630 src, e_route_cnt->src_gateway);
631 *latency += latency_src;
636 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
637 xbt_dynar_push(*route, &link);
641 if (strcmp(e_route_cnt->dst_gateway, dst)) {
643 xbt_dynar_t route_dst;
645 _get_route_latency(e_route_cnt->dst_gateway, dst,
646 (route ? &route_dst : NULL),
647 (latency ? &latency_dst : NULL));
649 xbt_assert(route_dst, "no route between \"%s\" and \"%s\"",
650 e_route_cnt->dst_gateway, dst);
651 xbt_dynar_foreach(route_dst, cpt, link) {
652 xbt_dynar_push(*route, &link);
654 xbt_dynar_free(&route_dst);
657 xbt_assert(latency_dst >= 0.0,
658 "latency error on route between \"%s\" and \"%s\"",
659 e_route_cnt->dst_gateway, dst);
660 *latency += latency_dst;
664 generic_free_extended_route(e_route_cnt);
669 * \brief Generic function for get_route, get_route_no_cleanup, and get_latency
671 static void get_route_latency(const char *src, const char *dst,
672 xbt_dynar_t *route, double *latency, int cleanup)
674 _get_route_latency(src, dst, route, latency);
675 xbt_assert(!route || *route, "no route between \"%s\" and \"%s\"", src, dst);
676 xbt_assert(!latency || *latency >= 0.0,
677 "latency error on route between \"%s\" and \"%s\"", src, dst);
679 xbt_dynar_free(&global_routing->last_route);
680 global_routing->last_route = cleanup ? *route : NULL;
685 * \brief Generic method: find a route between hosts
687 * \param src the source host name
688 * \param dst the destination host name
690 * walk through the routing components tree and find a route between hosts
691 * by calling the differents "get_route" functions in each routing component.
692 * No need to free the returned dynar. It will be freed at the next call.
694 static xbt_dynar_t get_route(const char *src, const char *dst)
696 xbt_dynar_t route = NULL;
697 get_route_latency(src, dst, &route, NULL, 1);
702 * \brief Generic method: find a route between hosts
704 * \param src the source host name
705 * \param dst the destination host name
707 * same as get_route, but return NULL if any exception is raised.
709 static xbt_dynar_t get_route_or_null(const char *src, const char *dst)
711 xbt_dynar_t route = NULL;
714 get_route_latency(src, dst, &route, NULL, 1);
716 xbt_ex_free(exception);
723 * \brief Generic method: find a route between hosts
725 * \param src the source host name
726 * \param dst the destination host name
728 * walk through the routing components tree and find a route between hosts
729 * by calling the differents "get_route" functions in each routing component.
730 * Leaves the caller the responsability to clean the returned dynar.
732 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
734 xbt_dynar_t route = NULL;
735 get_route_latency(src, dst, &route, NULL, 0);
740 static double get_latency(const char *src, const char *dst)
742 double latency = -1.0;
743 get_route_latency(src, dst, NULL, &latency, 0);
747 static int surf_parse_models_setup_already_called=0;
748 /* Call the last initialization functions, that must be called after the
749 * <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
751 void surf_parse_models_setup()
753 if (surf_parse_models_setup_already_called)
755 surf_parse_models_setup_already_called=1;
756 surf_config_models_setup();
761 * \brief Recursive function for finalize
763 * \param rc the source host name
765 * This fuction is call by "finalize". It allow to finalize the
766 * AS or routing components. It delete all the structures.
768 static void _finalize(routing_component_t rc)
771 xbt_dict_cursor_t cursor = NULL;
773 routing_component_t elem;
774 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
777 xbt_dict_t tmp_sons = rc->routing_sons;
778 char *tmp_name = rc->name;
779 xbt_dict_free(&tmp_sons);
781 xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
782 current_routing->name);
783 (*(rc->finalize)) (rc);
788 * \brief Generic method: delete all the routing structures
790 * walk through the routing components tree and delete the structures
791 * by calling the differents "finalize" functions in each routing component
793 static void finalize(void)
795 /* delete recursibly all the tree */
796 _finalize(global_routing->root);
797 /* delete last_route */
798 xbt_dynar_free(&(global_routing->last_route));
799 /* delete global routing structure */
800 xbt_free(global_routing);
801 /* make sure that we will reinit the models while loading the platf once reinited -- HACK but there is no proper surf_routing_init() */
802 surf_parse_models_setup_already_called = 0;
805 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
807 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
809 //adding my one link routes
812 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
814 xbt_dynar_foreach(onelink_mine, cpt, link) {
815 xbt_dynar_push(ret, &link);
820 xbt_dict_cursor_t cursor = NULL;
821 routing_component_t rc_child;
822 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
823 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
825 xbt_dynar_foreach(onelink_child, cpt, link) {
826 xbt_dynar_push(ret, &link);
833 static xbt_dynar_t get_onelink_routes(void)
835 return recursive_get_onelink_routes(global_routing->root);
838 e_surf_network_element_type_t get_network_element_type(const char
841 network_element_info_t rc = NULL;
843 rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
844 if(rc) return rc->rc_type;
846 rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
847 if(rc) return rc->rc_type;
849 return SURF_NETWORK_ELEMENT_NULL;
853 * \brief Generic method: create the global routing schema
855 * Make a global routing structure and set all the parsing functions.
857 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
859 /* config the uniq global routing */
860 global_routing = xbt_new0(s_routing_global_t, 1);
861 global_routing->root = NULL;
862 global_routing->get_route = get_route;
863 global_routing->get_route_or_null = get_route_or_null;
864 global_routing->get_latency = get_latency;
865 global_routing->get_route_no_cleanup = get_route_no_cleanup;
866 global_routing->get_onelink_routes = get_onelink_routes;
867 global_routing->get_route_latency = get_route_latency;
868 global_routing->get_network_element_type = get_network_element_type;
869 global_routing->finalize = finalize;
870 global_routing->loopback = loopback;
871 global_routing->size_of_link = size_of_links;
872 global_routing->last_route = NULL;
873 get_link_latency = get_link_latency_fun;
874 /* no current routing at moment */
875 current_routing = NULL;
877 /* parse generic elements */
878 sg_platf_host_add_cb(parse_S_host);
879 sg_platf_router_add_cb(parse_S_router);
881 surfxml_add_callback(STag_surfxml_route_cb_list,
882 &parse_S_route_new_and_endpoints_XML);
883 surfxml_add_callback(STag_surfxml_ASroute_cb_list,
884 &parse_S_ASroute_new_and_endpoints);
885 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
886 &parse_S_bypassRoute_new_and_endpoints);
888 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
889 &parse_E_link_ctn_new_elem_XML);
891 surfxml_add_callback(ETag_surfxml_route_cb_list,
892 &parse_E_route_store_route);
893 surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
894 &parse_E_ASroute_store_route);
895 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
896 &parse_E_bypassRoute_store_route);
898 surfxml_add_callback(STag_surfxml_cluster_cb_list,
899 &routing_parse_Scluster);
901 surfxml_add_callback(STag_surfxml_peer_cb_list,
902 &routing_parse_Speer);
904 sg_platf_postparse_add_cb(clean_routing_after_parse);
907 instr_routing_define_callbacks();
911 void surf_parse_add_callback_config(void)
913 surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
917 /* ************************************************** */
918 /* ********** PATERN FOR NEW ROUTING **************** */
920 /* The minimal configuration of a new routing model need the next functions,
921 * also you need to set at the start of the file, the new model in the model
922 * list. Remember keep the null ending of the list.
924 /*** Routing model structure ***/
926 // s_routing_component_t generic_routing;
927 // /* things that your routing model need */
928 // } s_routing_component_NEW_t,*routing_component_NEW_t;
930 /*** Parse routing model functions ***/
931 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
932 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
933 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
934 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
935 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
937 /*** Business methods ***/
938 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
939 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
940 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
942 /*** Creation routing model functions ***/
943 // static void* model_NEW_create(void) {
944 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
945 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
946 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
947 // new_component->generic_routing.set_route = model_NEW_set_route;
948 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
949 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
950 // new_component->generic_routing.get_route = NEW_get_route;
951 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
952 // new_component->generic_routing.finalize = NEW_finalize;
953 // /* initialization of internal structures */
954 // return new_component;
956 // static void model_NEW_load(void) {} /* mandatory */
957 // static void model_NEW_unload(void) {} /* mandatory */
958 // static void model_NEW_end(void) {} /* mandatory */
960 /* ************************************************************************** */
961 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
963 void generic_set_processing_unit(routing_component_t rc,
966 XBT_DEBUG("Load process unit \"%s\"", name);
967 int *id = xbt_new0(int, 1);
968 xbt_dict_t _to_index;
969 _to_index = current_routing->to_index;
970 *id = xbt_dict_length(_to_index);
971 xbt_dict_set(_to_index, name, id, xbt_free);
974 void generic_set_autonomous_system(routing_component_t rc,
977 XBT_DEBUG("Load Autonomous system \"%s\"", name);
978 int *id = xbt_new0(int, 1);
979 xbt_dict_t _to_index;
980 _to_index = current_routing->to_index;
981 *id = xbt_dict_length(_to_index);
982 xbt_dict_set(_to_index, name, id, xbt_free);
985 int surf_pointer_resource_cmp(const void *a, const void *b)
990 int surf_link_resource_cmp(const void *a, const void *b)
992 return !!memcmp(a,b,global_routing->size_of_link);
995 void generic_set_bypassroute(routing_component_t rc,
996 const char *src, const char *dst,
997 route_extended_t e_route)
999 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
1000 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1003 route_name = bprintf("%s#%s", src, dst);
1004 xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
1005 "Invalid count of links, must be greater than zero (%s,%s)",
1007 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
1008 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
1009 src, e_route->src_gateway, dst, e_route->dst_gateway);
1011 route_extended_t new_e_route =
1012 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
1013 xbt_dynar_free(&(e_route->generic_route.link_list));
1016 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
1017 (void (*)(void *)) generic_free_extended_route);
1018 xbt_free(route_name);
1021 /* ************************************************************************** */
1022 /* *********************** GENERIC BUSINESS METHODS ************************* */
1024 double generic_get_link_latency(routing_component_t rc,
1025 const char *src, const char *dst,
1026 route_extended_t route)
1028 int need_to_clean = route?0:1;
1031 double latency = 0.0;
1033 route = route?route:rc->get_route(rc,src,dst);
1035 xbt_dynar_foreach(route->generic_route.link_list,i,link) {
1036 latency += get_link_latency(link);
1038 if(need_to_clean) generic_free_extended_route(route);
1042 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
1044 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
1047 route_extended_t generic_get_bypassroute(routing_component_t rc,
1051 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
1052 routing_component_t src_as, dst_as;
1053 int index_src, index_dst;
1054 xbt_dynar_t path_src = NULL;
1055 xbt_dynar_t path_dst = NULL;
1056 routing_component_t current = NULL;
1057 routing_component_t *current_src = NULL;
1058 routing_component_t *current_dst = NULL;
1060 /* (1) find the as where the src and dst are located */
1061 void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1062 void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1063 if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1064 if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1066 if(src_data == NULL || dst_data == NULL)
1067 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1068 src, dst, rc->name);
1070 src_as = ((network_element_info_t)src_data)->rc_component;
1071 dst_as = ((network_element_info_t)dst_data)->rc_component;
1073 /* (2) find the path to the root routing component */
1074 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1076 while (current != NULL) {
1077 xbt_dynar_push(path_src, ¤t);
1078 current = current->routing_father;
1080 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1082 while (current != NULL) {
1083 xbt_dynar_push(path_dst, ¤t);
1084 current = current->routing_father;
1087 /* (3) find the common father */
1088 index_src = path_src->used - 1;
1089 index_dst = path_dst->used - 1;
1090 current_src = xbt_dynar_get_ptr(path_src, index_src);
1091 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1092 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1093 xbt_dynar_pop_ptr(path_src);
1094 xbt_dynar_pop_ptr(path_dst);
1097 current_src = xbt_dynar_get_ptr(path_src, index_src);
1098 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1101 int max_index_src = path_src->used - 1;
1102 int max_index_dst = path_dst->used - 1;
1104 int max_index = max(max_index_src, max_index_dst);
1107 route_extended_t e_route_bypass = NULL;
1109 for (max = 0; max <= max_index; max++) {
1110 for (i = 0; i < max; i++) {
1111 if (i <= max_index_src && max <= max_index_dst) {
1112 char *route_name = bprintf("%s#%s",
1113 (*(routing_component_t *)
1115 (path_src, i)))->name,
1116 (*(routing_component_t *)
1118 (path_dst, max)))->name);
1120 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1121 xbt_free(route_name);
1125 if (max <= max_index_src && i <= max_index_dst) {
1126 char *route_name = bprintf("%s#%s",
1127 (*(routing_component_t *)
1129 (path_src, max)))->name,
1130 (*(routing_component_t *)
1132 (path_dst, i)))->name);
1134 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1135 xbt_free(route_name);
1144 if (max <= max_index_src && max <= max_index_dst) {
1145 char *route_name = bprintf("%s#%s",
1146 (*(routing_component_t *)
1148 (path_src, max)))->name,
1149 (*(routing_component_t *)
1151 (path_dst, max)))->name);
1152 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1153 xbt_free(route_name);
1159 xbt_dynar_free(&path_src);
1160 xbt_dynar_free(&path_dst);
1162 route_extended_t new_e_route = NULL;
1164 if (e_route_bypass) {
1166 unsigned int cpt = 0;
1167 new_e_route = xbt_new0(s_route_extended_t, 1);
1168 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1169 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1170 new_e_route->generic_route.link_list =
1171 xbt_dynar_new(global_routing->size_of_link, NULL);
1172 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1173 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1180 /* ************************************************************************** */
1181 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1184 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1185 void *data, int order)
1191 xbt_dynar_t links = NULL, links_id = NULL;
1193 new_route = xbt_new0(s_route_t, 1);
1194 new_route->link_list =
1195 xbt_dynar_new(global_routing->size_of_link, NULL);
1197 xbt_assert(hierarchy == SURF_ROUTING_BASE,
1198 "the hierarchy type is not SURF_ROUTING_BASE");
1200 links = ((route_t) data)->link_list;
1203 links_id = new_route->link_list;
1205 xbt_dynar_foreach(links, cpt, link_name) {
1208 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1211 xbt_dynar_push(links_id, &link);
1213 xbt_dynar_unshift(links_id, &link);
1215 THROWF(mismatch_error, 0, "Link %s not found", link_name);
1222 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1223 void *data, int order)
1227 route_extended_t e_route, new_e_route;
1230 xbt_dynar_t links = NULL, links_id = NULL;
1232 new_e_route = xbt_new0(s_route_extended_t, 1);
1233 new_e_route->generic_route.link_list =
1234 xbt_dynar_new(global_routing->size_of_link, NULL);
1235 new_e_route->src_gateway = NULL;
1236 new_e_route->dst_gateway = NULL;
1238 xbt_assert(hierarchy == SURF_ROUTING_BASE
1239 || hierarchy == SURF_ROUTING_RECURSIVE,
1240 "the hierarchy type is not defined");
1242 if (hierarchy == SURF_ROUTING_BASE) {
1244 route = (route_t) data;
1245 links = route->link_list;
1247 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1249 e_route = (route_extended_t) data;
1250 xbt_assert(e_route->src_gateway
1251 && e_route->dst_gateway, "bad gateway, is null");
1252 links = e_route->generic_route.link_list;
1254 /* remeber not erase the gateway names */
1255 new_e_route->src_gateway = strdup(e_route->src_gateway);
1256 new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1259 links_id = new_e_route->generic_route.link_list;
1261 xbt_dynar_foreach(links, cpt, link_name) {
1264 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1267 xbt_dynar_push(links_id, &link);
1269 xbt_dynar_unshift(links_id, &link);
1271 THROWF(mismatch_error, 0, "Link %s not found", link_name);
1277 void generic_free_route(route_t route)
1280 xbt_dynar_free(&(route->link_list));
1285 void generic_free_extended_route(route_extended_t e_route)
1288 xbt_dynar_free(&(e_route->generic_route.link_list));
1289 if (e_route->src_gateway)
1290 xbt_free(e_route->src_gateway);
1291 if (e_route->dst_gateway)
1292 xbt_free(e_route->dst_gateway);
1297 static routing_component_t generic_as_exist(routing_component_t find_from,
1298 routing_component_t to_find)
1300 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1301 xbt_dict_cursor_t cursor = NULL;
1304 routing_component_t elem;
1305 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1306 if (to_find == elem || generic_as_exist(elem, to_find)) {
1317 generic_autonomous_system_exist(routing_component_t rc, char *element)
1319 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1320 routing_component_t element_as, result, elem;
1321 xbt_dict_cursor_t cursor = NULL;
1323 element_as = ((network_element_info_t)
1324 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1325 result = ((routing_component_t) - 1);
1326 if (element_as != rc)
1327 result = generic_as_exist(rc, element_as);
1331 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1332 found = !strcmp(elem->name, element);
1343 generic_processing_units_exist(routing_component_t rc, char *element)
1345 routing_component_t element_as;
1346 element_as = ((network_element_info_t)
1347 xbt_lib_get_or_null(host_lib,
1348 element, ROUTING_HOST_LEVEL))->rc_component;
1349 if (element_as == rc)
1351 return generic_as_exist(rc, element_as);
1354 void generic_src_dst_check(routing_component_t rc, const char *src,
1358 void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1359 void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1360 if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1361 if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1363 if(src_data == NULL || dst_data == NULL)
1364 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1365 src, dst, rc->name);
1367 routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1368 routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1370 if(src_as != dst_as)
1371 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1372 src, src_as->name, dst, dst_as->name);
1374 xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1375 src,dst,src_as->name, dst_as->name,rc->name);
1378 void routing_parse_Scluster(void)
1380 if(!cluster_host_link)
1381 cluster_host_link = xbt_dict_new();
1383 static int AX_ptr = 0;
1384 char *host_id, *groups, *link_id = NULL;
1386 s_sg_platf_host_cbarg_t host;
1387 s_sg_platf_link_cbarg_t link;
1389 if( strcmp(struct_cluster->V_cluster_availability_file,"")
1390 || strcmp(struct_cluster->V_cluster_state_file,"") )
1392 if(xbt_dict_size(patterns)==0)
1393 patterns = xbt_dict_new();
1394 xbt_dict_set(patterns,"id",struct_cluster->V_cluster_id,NULL);
1395 xbt_dict_set(patterns,"prefix",struct_cluster->V_cluster_prefix,NULL);
1396 xbt_dict_set(patterns,"suffix",struct_cluster->V_cluster_suffix,NULL);
1401 xbt_dynar_t radical_elements;
1402 xbt_dynar_t radical_ends;
1404 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1405 static unsigned int surfxml_buffer_stack_stack[1024];
1407 surfxml_buffer_stack_stack[0] = 0;
1408 surfxml_bufferstack_push(1);
1410 SURFXML_BUFFER_SET(AS_id, struct_cluster->V_cluster_id);
1411 SURFXML_BUFFER_SET(AS_routing, "Cluster");
1412 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->V_cluster_id);
1413 SURFXML_START_TAG(AS);
1416 radical_elements = xbt_str_split(struct_cluster->V_cluster_radical, ",");
1417 xbt_dynar_foreach(radical_elements, iter, groups) {
1418 memset(&host,0,sizeof(host));
1420 radical_ends = xbt_str_split(groups, "-");
1421 switch (xbt_dynar_length(radical_ends)) {
1423 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1424 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, start, struct_cluster->V_cluster_suffix);
1425 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, start);
1427 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1428 host.V_host_id = host_id;
1429 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1430 xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1431 char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1432 xbt_str_varsubst(tmp_availability_file,patterns);
1433 XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1434 host.V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1435 xbt_free(tmp_availability_file);
1439 XBT_DEBUG("\tavailability_file=\"\"");
1441 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1442 char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1443 xbt_str_varsubst(tmp_state_file,patterns);
1444 XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1445 host.V_host_state_trace = tmgr_trace_new(tmp_state_file);
1446 xbt_free(tmp_state_file);
1450 XBT_DEBUG("\tstate_file=\"\"");
1453 host.V_host_power_peak = struct_cluster->S_cluster_power;
1454 host.V_host_power_scale = 1.0;
1455 host.V_host_core = struct_cluster->S_cluster_core;
1456 host.V_host_state_initial = SURF_RESOURCE_ON;
1457 host.V_host_coord = "";
1458 sg_platf_new_host(&host);
1459 XBT_DEBUG("</host>");
1462 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1463 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1464 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
1465 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1466 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
1468 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1470 memset(&link,0,sizeof(link));
1471 link.V_link_id = link_id;
1472 link.V_link_bandwidth = struct_cluster->S_cluster_bw;
1473 link.V_link_latency = struct_cluster->S_cluster_lat;
1474 link.V_link_bandwidth_file = NULL;
1475 link.V_link_latency_file = NULL;
1476 link.V_link_state_file = NULL;
1477 link.V_link_state = SURF_RESOURCE_ON;
1478 link.V_link_sharing_policy = A_surfxml_link_sharing_policy;
1480 switch (A_surfxml_link_sharing_policy) {
1481 case A_surfxml_link_sharing_policy_SHARED:
1482 link.V_policy_initial_link = SURF_LINK_SHARED;
1484 case A_surfxml_link_sharing_policy_FATPIPE:
1485 link.V_policy_initial_link = SURF_LINK_FATPIPE;
1487 case A_surfxml_link_sharing_policy_FULLDUPLEX:
1488 link.V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1490 case AU_surfxml_link_sharing_policy:
1491 surf_parse_error(bprintf("Invalid sharing policy in cluster %s (please report this bug, this shouldn't happen)",struct_cluster->V_cluster_id));
1494 sg_platf_new_link(&link);
1496 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1497 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
1498 char* tmp_link = bprintf("%s_UP",link_id);
1499 info->link_up = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1501 tmp_link = bprintf("%s_DOWN",link_id);
1502 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1506 info->link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1507 info->link_down = info->link_up;
1509 xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1517 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1518 end= surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1519 for (i = start; i <= end; i++) {
1520 host_id = bprintf("%s%d%s", struct_cluster->V_cluster_prefix, i, struct_cluster->V_cluster_suffix);
1521 link_id = bprintf("%s_link_%d", struct_cluster->V_cluster_id, i);
1523 A_surfxml_host_state = A_surfxml_host_state_ON;
1525 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->S_cluster_power);
1526 host.V_host_id = host_id;
1527 if(strcmp(struct_cluster->V_cluster_availability_file,"")){
1528 xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1529 char* tmp_availability_file = xbt_strdup(struct_cluster->V_cluster_availability_file);
1530 xbt_str_varsubst(tmp_availability_file,patterns);
1531 XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1532 host.V_host_power_trace = tmgr_trace_new(tmp_availability_file);
1533 xbt_free(tmp_availability_file);
1537 XBT_DEBUG("\tavailability_file=\"\"");
1539 if(strcmp(struct_cluster->V_cluster_state_file,"")){
1540 char *tmp_state_file = xbt_strdup(struct_cluster->V_cluster_state_file);
1541 xbt_str_varsubst(tmp_state_file,patterns);
1542 XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1543 host.V_host_state_trace = tmgr_trace_new(tmp_state_file);
1544 xbt_free(tmp_state_file);
1548 XBT_DEBUG("\tstate_file=\"\"");
1551 host.V_host_power_peak = struct_cluster->S_cluster_power;
1552 host.V_host_power_scale = 1.0;
1553 host.V_host_core = struct_cluster->S_cluster_core;
1554 host.V_host_state_initial = SURF_RESOURCE_ON;
1555 host.V_host_coord = "";
1556 sg_platf_new_host(&host);
1557 XBT_DEBUG("</host>");
1559 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1560 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
1561 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
1562 if(struct_cluster->V_cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
1563 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
1565 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->S_cluster_bw, struct_cluster->S_cluster_lat);
1567 memset(&link,0,sizeof(link));
1568 link.V_link_id = link_id;
1569 link.V_link_bandwidth = struct_cluster->S_cluster_bw;
1570 link.V_link_latency = struct_cluster->S_cluster_lat;
1571 link.V_link_bandwidth_file = NULL;
1572 link.V_link_latency_file = NULL;
1573 link.V_link_state_file = NULL;
1574 link.V_link_state = SURF_RESOURCE_ON;
1575 link.V_link_sharing_policy = A_surfxml_link_sharing_policy;
1577 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1578 link.V_policy_initial_link = SURF_LINK_SHARED;
1581 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FATPIPE)
1582 link.V_policy_initial_link = SURF_LINK_FATPIPE;
1583 else if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX)
1584 link.V_policy_initial_link = SURF_LINK_FULLDUPLEX;
1586 sg_platf_new_link(&link);
1588 ETag_surfxml_link();
1590 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1591 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_FULLDUPLEX){
1592 char* tmp_link = bprintf("%s_UP",link_id);
1593 info->link_up = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1595 tmp_link = bprintf("%s_DOWN",link_id);
1596 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1600 info->link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1601 info->link_down = info->link_up;
1603 xbt_dict_set(cluster_host_link,host_id,info,xbt_free);
1611 XBT_DEBUG("Malformed radical");
1615 xbt_dynar_free(&radical_ends);
1617 xbt_dynar_free(&radical_elements);
1621 XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->S_cluster_router_id);
1622 SURFXML_BUFFER_SET(router_id, struct_cluster->S_cluster_router_id);
1623 SURFXML_BUFFER_SET(router_coordinates, "");
1624 SURFXML_START_TAG(router);
1625 SURFXML_END_TAG(router);
1628 if( (struct_cluster->S_cluster_bb_bw!= 0) && (struct_cluster->S_cluster_bb_lat!=0) ){
1629 char *link_backbone = bprintf("%s_backbone", struct_cluster->V_cluster_id);
1630 XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->S_cluster_bb_bw, struct_cluster->S_cluster_bb_lat);
1632 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1633 if(AX_surfxml_cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
1634 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
1636 memset(&link,0,sizeof(link));
1637 link.V_link_id = link_backbone;
1638 link.V_link_bandwidth = struct_cluster->S_cluster_bb_bw;
1639 link.V_link_latency = struct_cluster->S_cluster_bb_lat;
1640 link.V_link_bandwidth_file = NULL;
1641 link.V_link_latency_file = NULL;
1642 link.V_link_state_file = NULL;
1643 link.V_link_state = SURF_RESOURCE_ON;
1644 link.V_link_sharing_policy = A_surfxml_link_sharing_policy;
1646 if (A_surfxml_link_sharing_policy == A_surfxml_link_sharing_policy_SHARED)
1647 link.V_policy_initial_link = SURF_LINK_SHARED;
1649 link.V_policy_initial_link = SURF_LINK_FATPIPE;
1651 sg_platf_new_link(&link);
1652 ETag_surfxml_link();
1654 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1655 info->link_up = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1656 info->link_down = info->link_up;
1657 xbt_dict_set(cluster_host_link,struct_cluster->V_cluster_id,info,xbt_free);
1658 free(link_backbone);
1663 char *new_suffix = xbt_strdup("");
1665 radical_elements = xbt_str_split(struct_cluster->V_cluster_suffix, ".");
1666 xbt_dynar_foreach(radical_elements, iter, groups) {
1667 if (strcmp(groups, "")) {
1668 char *old_suffix = new_suffix;
1669 new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1674 xbt_dynar_free(&radical_elements);
1675 xbt_free(new_suffix);
1677 if( strcmp(struct_cluster->V_cluster_availability_file,"")
1678 || strcmp(struct_cluster->V_cluster_state_file,"") )
1679 xbt_dict_free(&patterns);
1682 SURFXML_END_TAG(AS);
1685 surfxml_bufferstack_pop(1);
1688 * This function take a string and replace parameters from patterns dict.
1689 * It returns the new value.
1691 static char* replace_random_parameter(char * string)
1693 char *test_string = NULL;
1695 if(xbt_dict_size(random_value)==0)
1698 string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1699 test_string = bprintf("${%s}", string);
1700 test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1702 if (strcmp(test_string,"")) { //if not empty, keep this value.
1704 string = test_string;
1705 } //In other case take old value (without ${})
1711 static void clean_routing_after_parse(void)
1713 xbt_dict_free(&random_value);
1714 xbt_dict_free(&patterns);
1717 static void routing_parse_Speer(void)
1719 static int AX_ptr = 0;
1720 char *host_id = NULL;
1721 char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1723 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1724 static unsigned int surfxml_buffer_stack_stack[1024];
1726 surfxml_buffer_stack_stack[0] = 0;
1728 surfxml_bufferstack_push(1);
1730 SURFXML_BUFFER_SET(AS_id, struct_peer->V_peer_id);
1732 SURFXML_BUFFER_SET(AS_routing, "Full");
1733 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", struct_peer->V_peer_id);
1735 SURFXML_START_TAG(AS);
1738 host_id = HOST_PEER(struct_peer->V_peer_id);
1739 router_id = ROUTER_PEER(struct_peer->V_peer_id);
1740 link_id_up = LINK_UP_PEER(struct_peer->V_peer_id);
1741 link_id_down = LINK_DOWN_PEER(struct_peer->V_peer_id);
1743 link_router = bprintf("%s_link_router", struct_peer->V_peer_id);
1744 link_backbone = bprintf("%s_backbone", struct_peer->V_peer_id);
1746 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, struct_peer->V_peer_power);
1747 A_surfxml_host_state = A_surfxml_host_state_ON;
1748 SURFXML_BUFFER_SET(host_id, host_id);
1749 SURFXML_BUFFER_SET(host_power, struct_peer->V_peer_power);
1750 SURFXML_BUFFER_SET(host_availability, "1.0");
1751 SURFXML_BUFFER_SET(host_availability_file, struct_peer->V_peer_availability_trace);
1752 SURFXML_BUFFER_SET(host_state_file, struct_peer->V_peer_state_trace);
1753 SURFXML_BUFFER_SET(host_coordinates, "");
1754 SURFXML_BUFFER_SET(host_core, "1.0");
1755 SURFXML_START_TAG(host);
1756 SURFXML_END_TAG(host);
1758 XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, struct_peer->V_peer_coord);
1759 SURFXML_BUFFER_SET(router_id, router_id);
1760 SURFXML_BUFFER_SET(router_coordinates, struct_peer->V_peer_coord);
1761 SURFXML_START_TAG(router);
1762 SURFXML_END_TAG(router);
1764 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_up, struct_peer->V_peer_bw_in, struct_peer->V_peer_lat);
1765 A_surfxml_link_state = A_surfxml_link_state_ON;
1766 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1767 SURFXML_BUFFER_SET(link_id, link_id_up);
1768 SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_in);
1769 SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1770 SURFXML_BUFFER_SET(link_bandwidth_file, "");
1771 SURFXML_BUFFER_SET(link_latency_file, "");
1772 SURFXML_BUFFER_SET(link_state_file, "");
1773 SURFXML_START_TAG(link);
1774 SURFXML_END_TAG(link);
1776 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id_down, struct_peer->V_peer_bw_out, struct_peer->V_peer_lat);
1777 A_surfxml_link_state = A_surfxml_link_state_ON;
1778 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1779 SURFXML_BUFFER_SET(link_id, link_id_down);
1780 SURFXML_BUFFER_SET(link_bandwidth, struct_peer->V_peer_bw_out);
1781 SURFXML_BUFFER_SET(link_latency, struct_peer->V_peer_lat);
1782 SURFXML_BUFFER_SET(link_bandwidth_file, "");
1783 SURFXML_BUFFER_SET(link_latency_file, "");
1784 SURFXML_BUFFER_SET(link_state_file, "");
1785 SURFXML_START_TAG(link);
1786 SURFXML_END_TAG(link);
1791 XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1792 XBT_DEBUG("symmetrical=\"NO\">");
1793 SURFXML_BUFFER_SET(route_src, host_id);
1794 SURFXML_BUFFER_SET(route_dst, router_id);
1795 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1796 SURFXML_START_TAG(route);
1798 XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1799 SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1800 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1801 SURFXML_START_TAG(link_ctn);
1802 SURFXML_END_TAG(link_ctn);
1804 XBT_DEBUG("</route>");
1805 SURFXML_END_TAG(route);
1808 XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1809 XBT_DEBUG("symmetrical=\"NO\">");
1810 SURFXML_BUFFER_SET(route_src, router_id);
1811 SURFXML_BUFFER_SET(route_dst, host_id);
1812 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1813 SURFXML_START_TAG(route);
1815 XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1816 SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1817 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1818 SURFXML_START_TAG(link_ctn);
1819 SURFXML_END_TAG(link_ctn);
1821 XBT_DEBUG("</route>");
1822 SURFXML_END_TAG(route);
1825 SURFXML_END_TAG(AS);
1828 //xbt_dynar_free(&tab_elements_num);
1832 free(link_backbone);
1835 surfxml_bufferstack_pop(1);
1838 static void routing_parse_Srandom(void)
1840 double mean, std, min, max, seed;
1841 char *random_id = A_surfxml_random_id;
1842 char *random_radical = A_surfxml_random_radical;
1843 char *rd_name = NULL;
1845 mean = surf_parse_get_double(A_surfxml_random_mean);
1846 std = surf_parse_get_double(A_surfxml_random_std_deviation);
1847 min = surf_parse_get_double(A_surfxml_random_min);
1848 max = surf_parse_get_double(A_surfxml_random_max);
1849 seed = surf_parse_get_double(A_surfxml_random_seed);
1853 random_data_t random = xbt_new0(s_random_data_t, 1);
1856 xbt_dynar_t radical_elements;
1860 xbt_dynar_t radical_ends;
1862 random->generator = A_surfxml_random_generator;
1863 random->seed = seed;
1867 /* Check user stupidities */
1869 THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1871 THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1874 THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1877 /* normalize the mean and standard deviation before storing */
1878 random->mean = (mean - min) / (max - min);
1879 random->std = std / (max - min);
1881 if (random->mean * (1 - random->mean) < random->std * random->std)
1882 THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1883 random->mean, random->std);
1885 XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1895 if(xbt_dict_size(random_value)==0)
1896 random_value = xbt_dict_new();
1898 if(!strcmp(random_radical,""))
1900 res = random_generate(random);
1901 rd_value = bprintf("%f",res);
1902 xbt_dict_set(random_value, random_id, rd_value, free);
1906 radical_elements = xbt_str_split(random_radical, ",");
1907 xbt_dynar_foreach(radical_elements, iter, groups) {
1908 radical_ends = xbt_str_split(groups, "-");
1909 switch (xbt_dynar_length(radical_ends)) {
1911 xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
1912 res = random_generate(random);
1913 tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
1914 xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1919 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1920 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1921 for (i = start; i <= end; i++) {
1922 xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
1923 res = random_generate(random);
1924 tmpbuf = bprintf("%s%d",random_id,i);
1925 xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1930 XBT_INFO("Malformed radical");
1933 res = random_generate(random);
1934 rd_name = bprintf("%s_router",random_id);
1935 rd_value = bprintf("%f",res);
1936 xbt_dict_set(random_value, rd_name, rd_value, free);
1938 xbt_dynar_free(&radical_ends);
1941 xbt_dynar_free(&radical_elements);
1947 * Set a new link on the actual list of link for a route or ASroute
1949 void routing_add_link(const char *link_id)
1951 parse_E_link_c_ctn_new_elem_lua((char *) link_id);
1955 *Set the endpoints for a route
1957 void routing_set_route(const char *src_id, const char *dst_id)
1959 parse_S_route_new_and_endpoints_lua(src_id, dst_id);
1963 * Store the route by calling parse_E_route_store_route
1965 void routing_store_route(void)
1967 parse_E_route_store_route();