1 /* Copyright (c) 2009, 2010, 2011. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #include <pcre.h> /* regular expression library */
9 #include "simgrid/platf_interface.h" // platform creation API internal interface
11 #include "surf_routing_private.h"
12 #include "surf/surf_routing.h"
13 #include "surf/surfxml_parse_values.h"
16 int ROUTING_HOST_LEVEL; //Routing level
17 int SURF_CPU_LEVEL; //Surf cpu level
18 int SURF_WKS_LEVEL; //Surf workstation level
19 int SIMIX_HOST_LEVEL; //Simix level
20 int MSG_HOST_LEVEL; //Msg level
21 int SD_HOST_LEVEL; //Simdag level
22 int COORD_HOST_LEVEL; //Coordinates level
23 int NS3_HOST_LEVEL; //host node for ns3
26 int SD_LINK_LEVEL; //Simdag level
27 int SURF_LINK_LEVEL; //Surf level
28 int NS3_LINK_LEVEL; //link for ns3
30 xbt_lib_t as_router_lib;
31 int ROUTING_ASR_LEVEL; //Routing level
32 int COORD_ASR_LEVEL; //Coordinates level
33 int NS3_ASR_LEVEL; //host node for ns3
35 static xbt_dict_t patterns = NULL;
36 static xbt_dict_t random_value = NULL;
39 routing_global_t global_routing = NULL;
40 routing_component_t current_routing = NULL;
41 model_type_t current_routing_model = NULL;
43 /* global parse functions */
44 xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
45 static const char *src = NULL; /* temporary store the source name of a route */
46 static const char *dst = NULL; /* temporary store the destination name of a route */
47 static char *gw_src = NULL; /* temporary store the gateway source name of a route */
48 static char *gw_dst = NULL; /* temporary store the gateway destination name of a route */
49 static double_f_cpvoid_t get_link_latency = NULL;
51 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
53 static void routing_parse_peer(sg_platf_peer_cbarg_t peer); /* peer bypass */
54 static void routing_parse_Srandom(void); /* random bypass */
56 static char* replace_random_parameter(char * chaine);
57 static void routing_parse_postparse(void);
59 /* this lines are only for replace use like index in the model table */
64 SURF_MODEL_DIJKSTRACACHE,
71 struct s_model_type routing_models[] = {
72 { "Full", "Full routing data (fast, large memory requirements, fully expressive)",
73 model_full_create, model_full_load, model_full_unload, model_full_end
75 { "Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
76 model_floyd_create, model_floyd_load, model_floyd_unload, model_floyd_end
78 { "Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
79 model_dijkstra_create, model_dijkstra_both_load,
80 model_dijkstra_both_unload, model_dijkstra_both_end
82 { "DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
83 model_dijkstracache_create, model_dijkstra_both_load, model_dijkstra_both_unload, model_dijkstra_both_end
85 { "none", "No routing (usable with Constant network only)",
86 model_none_create, model_none_load, model_none_unload, model_none_end
88 { "RuleBased", "Rule-Based routing data (...)",
89 model_rulebased_create, model_none_load, model_none_unload, model_none_end
91 { "Vivaldi", "Vivaldi routing",
92 model_vivaldi_create, model_none_load, model_none_unload, model_none_end
94 {"Cluster", "Cluster routing",
95 model_cluster_create, model_none_load, model_cluster_unload, model_none_end
97 {NULL, NULL, NULL, NULL, NULL, NULL}
101 * \brief Add a "host" to the network element list
103 static void parse_S_host(sg_platf_host_cbarg_t host) {
104 network_element_info_t info = NULL;
105 if (current_routing->hierarchy == SURF_ROUTING_NULL)
106 current_routing->hierarchy = SURF_ROUTING_BASE;
107 xbt_assert(!xbt_lib_get_or_null(host_lib, host->id,ROUTING_HOST_LEVEL),
108 "Reading a host, processing unit \"%s\" already exists",
110 xbt_assert(current_routing->set_processing_unit,
111 "no defined method \"set_processing_unit\" in \"%s\"",
112 current_routing->name);
113 (*(current_routing->set_processing_unit)) (current_routing, host->id);
114 info = xbt_new0(s_network_element_info_t, 1);
115 info->rc_component = current_routing;
116 info->rc_type = SURF_NETWORK_ELEMENT_HOST;
117 xbt_lib_set(host_lib,host->id,ROUTING_HOST_LEVEL,(void *) info);
118 if (host->coord && strcmp(host->coord,"")) {
119 if(!COORD_HOST_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
120 xbt_dynar_t ctn = xbt_str_split_str(host->coord, " ");
121 xbt_dynar_shrink(ctn, 0);
122 xbt_lib_set(host_lib,host->id,COORD_HOST_LEVEL,(void *) ctn);
127 * \brief Add a "router" to the network element list
129 static void parse_S_router(sg_platf_router_cbarg_t router)
131 network_element_info_t info = NULL;
132 if (current_routing->hierarchy == SURF_ROUTING_NULL)
133 current_routing->hierarchy = SURF_ROUTING_BASE;
134 xbt_assert(!xbt_lib_get_or_null(as_router_lib,router->id, ROUTING_ASR_LEVEL),
135 "Reading a router, processing unit \"%s\" already exists",
137 xbt_assert(current_routing->set_processing_unit,
138 "no defined method \"set_processing_unit\" in \"%s\"",
139 current_routing->name);
140 (*(current_routing->set_processing_unit)) (current_routing, router->id);
141 info = xbt_new0(s_network_element_info_t, 1);
142 info->rc_component = current_routing;
143 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
145 xbt_lib_set(as_router_lib,router->id,ROUTING_ASR_LEVEL,(void *) info);
146 if (strcmp(router->coord,"")) {
147 if(!COORD_ASR_LEVEL) xbt_die("To use coordinates, you must set configuration 'coordinates' to 'yes'");
148 xbt_dynar_t ctn = xbt_str_split_str(router->coord, " ");
149 xbt_dynar_shrink(ctn, 0);
150 xbt_lib_set(as_router_lib,router->id,COORD_ASR_LEVEL,(void *) ctn);
155 * \brief Set the endponints for a route
157 static void parse_S_route_new_and_endpoints(const char *src_id, const char *dst_id)
159 if (src != NULL && dst != NULL && link_list != NULL)
160 THROWF(arg_error, 0, "Route between %s to %s can not be defined",
164 xbt_assert(strlen(src) > 0 || strlen(dst) > 0,
165 "Some limits are null in the route between \"%s\" and \"%s\"",
167 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
171 * \brief Set the endpoints for a route from XML
173 static void routing_parse_S_route(void)
175 parse_S_route_new_and_endpoints(A_surfxml_route_src,
176 A_surfxml_route_dst);
180 * \brief Set the endponints and gateways for a ASroute
182 static void routing_parse_S_ASroute(void)
184 if (src != NULL && dst != NULL && link_list != NULL)
185 THROWF(arg_error, 0, "Route between %s to %s can not be defined",
186 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
187 src = A_surfxml_ASroute_src;
188 dst = A_surfxml_ASroute_dst;
189 gw_src = A_surfxml_ASroute_gw_src;
190 gw_dst = A_surfxml_ASroute_gw_dst;
191 xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
192 || strlen(gw_dst) > 0,
193 "Some limits are null in the route between \"%s\" and \"%s\"",
195 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
199 * \brief Set the endponints for a bypassRoute
201 static void routing_parse_S_bypassRoute(void)
203 if (src != NULL && dst != NULL && link_list != NULL)
205 "Bypass Route between %s to %s can not be defined",
206 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
207 src = A_surfxml_bypassRoute_src;
208 dst = A_surfxml_bypassRoute_dst;
209 gw_src = A_surfxml_bypassRoute_gw_src;
210 gw_dst = A_surfxml_bypassRoute_gw_dst;
211 xbt_assert(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
212 || strlen(gw_dst) > 0,
213 "Some limits are null in the route between \"%s\" and \"%s\"",
215 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
219 * \brief Set a new link on the actual list of link for a route or ASroute from XML
222 static void routing_parse_link_ctn(void)
225 switch (A_surfxml_link_ctn_direction) {
226 case AU_surfxml_link_ctn_direction:
227 case A_surfxml_link_ctn_direction_NONE:
228 link_id = xbt_strdup(A_surfxml_link_ctn_id);
230 case A_surfxml_link_ctn_direction_UP:
231 link_id = bprintf("%s_UP", A_surfxml_link_ctn_id);
233 case A_surfxml_link_ctn_direction_DOWN:
234 link_id = bprintf("%s_DOWN", A_surfxml_link_ctn_id);
236 xbt_dynar_push(link_list,&link_id);
240 * \brief Store the route by calling the set_route function of the current routing component
242 static void routing_parse_E_route(void)
244 name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
245 route->generic_route.link_list = link_list;
246 xbt_assert(current_routing->set_route,
247 "no defined method \"set_route\" in \"%s\"",
248 current_routing->name);
249 (*(current_routing->set_route)) (current_routing, src, dst, route);
256 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
258 static void routing_parse_E_ASroute(void)
260 name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
261 e_route->generic_route.link_list = link_list;
262 e_route->src_gateway = xbt_strdup(gw_src);
263 e_route->dst_gateway = xbt_strdup(gw_dst);
264 xbt_assert(current_routing->set_ASroute,
265 "no defined method \"set_ASroute\" in \"%s\"",
266 current_routing->name);
267 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
276 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
278 static void routing_parse_E_bypassRoute(void)
280 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
281 e_route->generic_route.link_list = link_list;
282 e_route->src_gateway = xbt_strdup(gw_src);
283 e_route->dst_gateway = xbt_strdup(gw_dst);
284 xbt_assert(current_routing->set_bypassroute,
285 "no defined method \"set_bypassroute\" in \"%s\"",
286 current_routing->name);
287 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
297 * \brief Make a new routing component to the platform
299 * Add a new autonomous system to the platform. Any elements (such as host,
300 * router or sub-AS) added after this call and before the corresponding call
301 * to sg_platf_new_AS_close() will be added to this AS.
303 * Once this function was called, the configuration concerning the used
304 * models cannot be changed anymore.
306 * @param AS_id name of this autonomous system. Must be unique in the platform
307 * @param wanted_routing_type one of Full, Floyd, Dijkstra or similar. Full list in the variable routing_models, in src/surf/surf_routing.c
309 void routing_AS_open(const char *AS_id, const char *wanted_routing_type)
311 routing_component_t new_routing;
312 model_type_t model = NULL;
315 surf_parse_models_setup(); /* ensure that the models are created after the last <config> tag and before the first <AS>-like */
317 /* search the routing model */
318 for (cpt = 0; routing_models[cpt].name; cpt++)
319 if (!strcmp(wanted_routing_type, routing_models[cpt].name))
320 model = &routing_models[cpt];
321 /* if its not exist, error */
323 fprintf(stderr, "Routing model %s not found. Existing models:\n",
324 wanted_routing_type);
325 for (cpt = 0; routing_models[cpt].name; cpt++)
326 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
327 routing_models[cpt].desc);
331 /* make a new routing component */
332 new_routing = (routing_component_t) (*(model->create)) ();
333 new_routing->routing = model;
334 new_routing->hierarchy = SURF_ROUTING_NULL;
335 new_routing->name = xbt_strdup(AS_id);
336 new_routing->routing_sons = xbt_dict_new();
338 if (current_routing == NULL && global_routing->root == NULL) {
340 /* it is the first one */
341 new_routing->routing_father = NULL;
342 global_routing->root = new_routing;
344 } else if (current_routing != NULL && global_routing->root != NULL) {
346 xbt_assert(!xbt_dict_get_or_null
347 (current_routing->routing_sons, AS_id),
348 "The AS \"%s\" already exists", AS_id);
349 /* it is a part of the tree */
350 new_routing->routing_father = current_routing;
351 /* set the father behavior */
352 if (current_routing->hierarchy == SURF_ROUTING_NULL)
353 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
354 /* add to the sons dictionary */
355 xbt_dict_set(current_routing->routing_sons, AS_id,
356 (void *) new_routing, NULL);
357 /* add to the father element list */
358 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
359 /* unload the prev parse rules */
360 (*(current_routing->routing->unload)) ();
363 THROWF(arg_error, 0, "All defined components must be belong to a AS");
365 /* set the new parse rules */
366 (*(new_routing->routing->load)) ();
367 /* set the new current component of the tree */
368 current_routing = new_routing;
372 * \brief Specify that the current description of AS is finished
374 * Once you've declared all the content of your AS, you have to close
375 * it with this call. Your AS is not usable until you call this function.
377 * @fixme: this call is not as robust as wanted: bad things WILL happen
378 * if you call it twice for the same AS, or if you forget calling it, or
379 * even if you add stuff to a closed AS
382 void routing_AS_close() {
384 if (current_routing == NULL) {
385 THROWF(arg_error, 0, "Close an AS, but none was under construction");
387 network_element_info_t info = NULL;
388 xbt_assert(!xbt_lib_get_or_null(as_router_lib,current_routing->name, ROUTING_ASR_LEVEL),
389 "The AS \"%s\" already exists",current_routing->name);
390 info = xbt_new0(s_network_element_info_t, 1);
391 info->rc_component = current_routing->routing_father;
392 info->rc_type = SURF_NETWORK_ELEMENT_AS;
393 xbt_lib_set(as_router_lib,current_routing->name,ROUTING_ASR_LEVEL,(void *) info);
395 (*(current_routing->routing->unload)) ();
396 (*(current_routing->routing->end)) ();
397 current_routing = current_routing->routing_father;
398 if (current_routing != NULL)
399 (*(current_routing->routing->load)) ();
403 /* Aux Business methods */
406 * \brief Get the AS name of the element
408 * \param name the host name
411 static char* elements_As_name(const char *name)
413 routing_component_t as_comp;
415 /* (1) find the as where the host is located */
416 as_comp = ((network_element_info_t)
417 xbt_lib_get_or_null(host_lib,name, ROUTING_HOST_LEVEL))->rc_component;
418 return as_comp->name;
423 * \brief Get the AS father and the first elements of the chain
425 * \param src the source host name
426 * \param dst the destination host name
428 * Get the common father of the to processing units, and the first different
429 * father in the chain
431 static void elements_father(const char *src, const char *dst,
432 routing_component_t *res_father,
433 routing_component_t *res_src,
434 routing_component_t *res_dst)
436 xbt_assert(src && dst, "bad parameters for \"elements_father\" method");
437 #define ELEMENTS_FATHER_MAXDEPTH 16 /* increase if it is not enough */
438 routing_component_t src_as, dst_as;
439 routing_component_t path_src[ELEMENTS_FATHER_MAXDEPTH];
440 routing_component_t path_dst[ELEMENTS_FATHER_MAXDEPTH];
443 routing_component_t current;
444 routing_component_t current_src;
445 routing_component_t current_dst;
446 routing_component_t father;
448 /* (1) find the as where the src and dst are located */
449 network_element_info_t src_data = xbt_lib_get_or_null(host_lib, src,
451 network_element_info_t dst_data = xbt_lib_get_or_null(host_lib, dst,
454 src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
456 dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
457 src_as = src_data->rc_component;
458 dst_as = dst_data->rc_component;
460 xbt_assert(src_as && dst_as,
461 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src, dst);
463 /* (2) find the path to the root routing component */
464 for (current = src_as ; current != NULL ; current = current->routing_father) {
465 if (index_src >= ELEMENTS_FATHER_MAXDEPTH)
466 xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_src");
467 path_src[index_src++] = current;
469 for (current = dst_as ; current != NULL ; current = current->routing_father) {
470 if (index_dst >= ELEMENTS_FATHER_MAXDEPTH)
471 xbt_die("ELEMENTS_FATHER_MAXDEPTH should be increased for path_dst");
472 path_dst[index_dst++] = current;
475 /* (3) find the common father */
477 current_src = path_src[--index_src];
478 current_dst = path_dst[--index_dst];
479 } while (index_src > 0 && index_dst > 0 && current_src == current_dst);
481 /* (4) they are not in the same routing component, make the path */
482 if (current_src == current_dst)
483 father = current_src;
485 father = path_src[index_src + 1];
487 /* (5) result generation */
488 *res_father = father; /* first the common father of src and dst */
489 *res_src = current_src; /* second the first different father of src */
490 *res_dst = current_dst; /* three the first different father of dst */
492 #undef ELEMENTS_FATHER_MAXDEPTH
495 /* Global Business methods */
498 * \brief Recursive function for get_route_latency
500 * \param src the source host name
501 * \param dst the destination host name
502 * \param *e_route the route where the links are stored
503 * \param *latency the latency, if needed
505 * This function is called by "get_route" and "get_latency". It allows to walk
506 * recursively through the routing components tree.
508 static void _get_route_latency(const char *src, const char *dst,
509 xbt_dynar_t *route, double *latency)
511 XBT_DEBUG("Solve route/latency \"%s\" to \"%s\"", src, dst);
512 xbt_assert(src && dst, "bad parameters for \"_get_route_latency\" method");
514 routing_component_t common_father;
515 routing_component_t src_father;
516 routing_component_t dst_father;
517 elements_father(src, dst, &common_father, &src_father, &dst_father);
519 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
521 route_extended_t e_route = NULL;
523 e_route = common_father->get_route(common_father, src, dst);
524 xbt_assert(e_route, "no route between \"%s\" and \"%s\"", src, dst);
525 *route = e_route->generic_route.link_list;
528 *latency = common_father->get_latency(common_father, src, dst, e_route);
529 xbt_assert(*latency >= 0.0,
530 "latency error on route between \"%s\" and \"%s\"", src, dst);
533 xbt_free(e_route->src_gateway);
534 xbt_free(e_route->dst_gateway);
538 } else { /* SURF_ROUTING_RECURSIVE */
540 route_extended_t e_route_bypass = NULL;
541 if (common_father->get_bypass_route)
542 e_route_bypass = common_father->get_bypass_route(common_father, src, dst);
544 xbt_assert(!latency || !e_route_bypass,
545 "Bypass cannot work yet with get_latency");
547 route_extended_t e_route_cnt = e_route_bypass
549 : common_father->get_route(common_father,
550 src_father->name, dst_father->name);
552 xbt_assert(e_route_cnt, "no route between \"%s\" and \"%s\"",
553 src_father->name, dst_father->name);
555 xbt_assert((e_route_cnt->src_gateway == NULL) ==
556 (e_route_cnt->dst_gateway == NULL),
557 "bad gateway for route between \"%s\" and \"%s\"", src, dst);
560 *route = xbt_dynar_new(global_routing->size_of_link, NULL);
563 *latency = common_father->get_latency(common_father,
564 src_father->name, dst_father->name,
566 xbt_assert(*latency >= 0.0,
567 "latency error on route between \"%s\" and \"%s\"",
568 src_father->name, dst_father->name);
574 if (strcmp(src, e_route_cnt->src_gateway)) {
576 xbt_dynar_t route_src;
578 _get_route_latency(src, e_route_cnt->src_gateway,
579 (route ? &route_src : NULL),
580 (latency ? &latency_src : NULL));
582 xbt_assert(route_src, "no route between \"%s\" and \"%s\"",
583 src, e_route_cnt->src_gateway);
584 xbt_dynar_foreach(route_src, cpt, link) {
585 xbt_dynar_push(*route, &link);
587 xbt_dynar_free(&route_src);
590 xbt_assert(latency_src >= 0.0,
591 "latency error on route between \"%s\" and \"%s\"",
592 src, e_route_cnt->src_gateway);
593 *latency += latency_src;
598 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
599 xbt_dynar_push(*route, &link);
603 if (strcmp(e_route_cnt->dst_gateway, dst)) {
605 xbt_dynar_t route_dst;
607 _get_route_latency(e_route_cnt->dst_gateway, dst,
608 (route ? &route_dst : NULL),
609 (latency ? &latency_dst : NULL));
611 xbt_assert(route_dst, "no route between \"%s\" and \"%s\"",
612 e_route_cnt->dst_gateway, dst);
613 xbt_dynar_foreach(route_dst, cpt, link) {
614 xbt_dynar_push(*route, &link);
616 xbt_dynar_free(&route_dst);
619 xbt_assert(latency_dst >= 0.0,
620 "latency error on route between \"%s\" and \"%s\"",
621 e_route_cnt->dst_gateway, dst);
622 *latency += latency_dst;
626 generic_free_extended_route(e_route_cnt);
631 * \brief Generic function for get_route, get_route_no_cleanup, and get_latency
633 static void get_route_latency(const char *src, const char *dst,
634 xbt_dynar_t *route, double *latency, int cleanup)
636 _get_route_latency(src, dst, route, latency);
637 xbt_assert(!route || *route, "no route between \"%s\" and \"%s\"", src, dst);
638 xbt_assert(!latency || *latency >= 0.0,
639 "latency error on route between \"%s\" and \"%s\"", src, dst);
641 xbt_dynar_free(&global_routing->last_route);
642 global_routing->last_route = cleanup ? *route : NULL;
647 * \brief Generic method: find a route between hosts
649 * \param src the source host name
650 * \param dst the destination host name
652 * walk through the routing components tree and find a route between hosts
653 * by calling the differents "get_route" functions in each routing component.
654 * No need to free the returned dynar. It will be freed at the next call.
656 static xbt_dynar_t get_route(const char *src, const char *dst)
658 xbt_dynar_t route = NULL;
659 get_route_latency(src, dst, &route, NULL, 1);
664 * \brief Generic method: find a route between hosts
666 * \param src the source host name
667 * \param dst the destination host name
669 * same as get_route, but return NULL if any exception is raised.
671 static xbt_dynar_t get_route_or_null(const char *src, const char *dst)
673 xbt_dynar_t route = NULL;
676 get_route_latency(src, dst, &route, NULL, 1);
678 xbt_ex_free(exception);
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 * Leaves the caller the responsability to clean the returned dynar.
694 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
696 xbt_dynar_t route = NULL;
697 get_route_latency(src, dst, &route, NULL, 0);
702 static double get_latency(const char *src, const char *dst)
704 double latency = -1.0;
705 get_route_latency(src, dst, NULL, &latency, 0);
709 static int surf_parse_models_setup_already_called=0;
710 /* Call the last initialization functions, that must be called after the
711 * <config> tag, if any, and before the first of cluster|peer|AS|trace|trace_connect
713 void surf_parse_models_setup()
715 if (surf_parse_models_setup_already_called)
717 surf_parse_models_setup_already_called=1;
718 surf_config_models_setup();
723 * \brief Recursive function for finalize
725 * \param rc the source host name
727 * This fuction is call by "finalize". It allow to finalize the
728 * AS or routing components. It delete all the structures.
730 static void _finalize(routing_component_t rc)
733 xbt_dict_cursor_t cursor = NULL;
735 routing_component_t elem;
736 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
739 xbt_dict_t tmp_sons = rc->routing_sons;
740 char *tmp_name = rc->name;
741 xbt_dict_free(&tmp_sons);
743 xbt_assert(rc->finalize, "no defined method \"finalize\" in \"%s\"",
744 current_routing->name);
745 (*(rc->finalize)) (rc);
750 * \brief Generic method: delete all the routing structures
752 * walk through the routing components tree and delete the structures
753 * by calling the differents "finalize" functions in each routing component
755 static void finalize(void)
757 /* delete recursively all the tree */
758 _finalize(global_routing->root);
759 /* delete last_route */
760 xbt_dynar_free(&(global_routing->last_route));
761 /* delete global routing structure */
762 xbt_free(global_routing);
763 /* make sure that we will reinit the models while loading the platf once reinited -- HACK but there is no proper surf_routing_init() */
764 surf_parse_models_setup_already_called = 0;
767 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
769 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
771 //adding my one link routes
774 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
776 xbt_dynar_foreach(onelink_mine, cpt, link) {
777 xbt_dynar_push(ret, &link);
782 xbt_dict_cursor_t cursor = NULL;
783 routing_component_t rc_child;
784 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
785 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
787 xbt_dynar_foreach(onelink_child, cpt, link) {
788 xbt_dynar_push(ret, &link);
795 static xbt_dynar_t get_onelink_routes(void)
797 return recursive_get_onelink_routes(global_routing->root);
800 e_surf_network_element_type_t get_network_element_type(const char
803 network_element_info_t rc = NULL;
805 rc = xbt_lib_get_or_null(host_lib, name, ROUTING_HOST_LEVEL);
806 if(rc) return rc->rc_type;
808 rc = xbt_lib_get_or_null(as_router_lib, name, ROUTING_ASR_LEVEL);
809 if(rc) return rc->rc_type;
811 return SURF_NETWORK_ELEMENT_NULL;
815 * \brief Generic method: create the global routing schema
817 * Make a global routing structure and set all the parsing functions.
819 void routing_model_create(size_t size_of_links, void *loopback, double_f_cpvoid_t get_link_latency_fun)
821 /* config the uniq global routing */
822 global_routing = xbt_new0(s_routing_global_t, 1);
823 global_routing->root = NULL;
824 global_routing->get_route = get_route;
825 global_routing->get_route_or_null = get_route_or_null;
826 global_routing->get_latency = get_latency;
827 global_routing->get_route_no_cleanup = get_route_no_cleanup;
828 global_routing->get_onelink_routes = get_onelink_routes;
829 global_routing->get_route_latency = get_route_latency;
830 global_routing->get_network_element_type = get_network_element_type;
831 global_routing->finalize = finalize;
832 global_routing->loopback = loopback;
833 global_routing->size_of_link = size_of_links;
834 global_routing->last_route = NULL;
835 get_link_latency = get_link_latency_fun;
836 /* no current routing at moment */
837 current_routing = NULL;
841 /* ************************************************** */
842 /* ********** PATERN FOR NEW ROUTING **************** */
844 /* The minimal configuration of a new routing model need the next functions,
845 * also you need to set at the start of the file, the new model in the model
846 * list. Remember keep the null ending of the list.
848 /*** Routing model structure ***/
850 // s_routing_component_t generic_routing;
851 // /* things that your routing model need */
852 // } s_routing_component_NEW_t,*routing_component_NEW_t;
854 /*** Parse routing model functions ***/
855 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
856 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
857 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
858 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
859 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
861 /*** Business methods ***/
862 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
863 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
864 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
866 /*** Creation routing model functions ***/
867 // static void* model_NEW_create(void) {
868 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
869 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
870 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
871 // new_component->generic_routing.set_route = model_NEW_set_route;
872 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
873 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
874 // new_component->generic_routing.get_route = NEW_get_route;
875 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
876 // new_component->generic_routing.finalize = NEW_finalize;
877 // /* initialization of internal structures */
878 // return new_component;
880 // static void model_NEW_load(void) {} /* mandatory */
881 // static void model_NEW_unload(void) {} /* mandatory */
882 // static void model_NEW_end(void) {} /* mandatory */
884 /* ************************************************************************** */
885 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
887 void generic_set_processing_unit(routing_component_t rc,
890 XBT_DEBUG("Load process unit \"%s\"", name);
891 int *id = xbt_new0(int, 1);
892 xbt_dict_t _to_index;
893 _to_index = current_routing->to_index;
894 *id = xbt_dict_length(_to_index);
895 xbt_dict_set(_to_index, name, id, xbt_free);
898 void generic_set_autonomous_system(routing_component_t rc,
901 XBT_DEBUG("Load Autonomous system \"%s\"", name);
902 int *id = xbt_new0(int, 1);
903 xbt_dict_t _to_index;
904 _to_index = current_routing->to_index;
905 *id = xbt_dict_length(_to_index);
906 xbt_dict_set(_to_index, name, id, xbt_free);
909 int surf_pointer_resource_cmp(const void *a, const void *b)
914 int surf_link_resource_cmp(const void *a, const void *b)
916 return !!memcmp(a,b,global_routing->size_of_link);
919 void generic_set_bypassroute(routing_component_t rc,
920 const char *src, const char *dst,
921 route_extended_t e_route)
923 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
924 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
927 route_name = bprintf("%s#%s", src, dst);
928 xbt_assert(xbt_dynar_length(e_route->generic_route.link_list) > 0,
929 "Invalid count of links, must be greater than zero (%s,%s)",
931 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
932 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
933 src, e_route->src_gateway, dst, e_route->dst_gateway);
935 route_extended_t new_e_route =
936 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
937 xbt_dynar_free(&(e_route->generic_route.link_list));
940 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
941 (void (*)(void *)) generic_free_extended_route);
942 xbt_free(route_name);
945 /* ************************************************************************** */
946 /* *********************** GENERIC BUSINESS METHODS ************************* */
948 double generic_get_link_latency(routing_component_t rc,
949 const char *src, const char *dst,
950 route_extended_t route)
952 int need_to_clean = route?0:1;
955 double latency = 0.0;
957 route = route?route:rc->get_route(rc,src,dst);
959 xbt_dynar_foreach(route->generic_route.link_list,i,link) {
960 latency += get_link_latency(link);
962 if(need_to_clean) generic_free_extended_route(route);
966 xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
968 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
971 route_extended_t generic_get_bypassroute(routing_component_t rc,
975 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
976 routing_component_t src_as, dst_as;
977 int index_src, index_dst;
978 xbt_dynar_t path_src = NULL;
979 xbt_dynar_t path_dst = NULL;
980 routing_component_t current = NULL;
981 routing_component_t *current_src = NULL;
982 routing_component_t *current_dst = NULL;
984 /* (1) find the as where the src and dst are located */
985 void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
986 void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
987 if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
988 if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
990 if(src_data == NULL || dst_data == NULL)
991 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
994 src_as = ((network_element_info_t)src_data)->rc_component;
995 dst_as = ((network_element_info_t)dst_data)->rc_component;
997 /* (2) find the path to the root routing component */
998 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
1000 while (current != NULL) {
1001 xbt_dynar_push(path_src, ¤t);
1002 current = current->routing_father;
1004 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
1006 while (current != NULL) {
1007 xbt_dynar_push(path_dst, ¤t);
1008 current = current->routing_father;
1011 /* (3) find the common father */
1012 index_src = path_src->used - 1;
1013 index_dst = path_dst->used - 1;
1014 current_src = xbt_dynar_get_ptr(path_src, index_src);
1015 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1016 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
1017 xbt_dynar_pop_ptr(path_src);
1018 xbt_dynar_pop_ptr(path_dst);
1021 current_src = xbt_dynar_get_ptr(path_src, index_src);
1022 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
1025 int max_index_src = path_src->used - 1;
1026 int max_index_dst = path_dst->used - 1;
1028 int max_index = max(max_index_src, max_index_dst);
1031 route_extended_t e_route_bypass = NULL;
1033 for (max = 0; max <= max_index; max++) {
1034 for (i = 0; i < max; i++) {
1035 if (i <= max_index_src && max <= max_index_dst) {
1036 char *route_name = bprintf("%s#%s",
1037 (*(routing_component_t *)
1039 (path_src, i)))->name,
1040 (*(routing_component_t *)
1042 (path_dst, max)))->name);
1044 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1045 xbt_free(route_name);
1049 if (max <= max_index_src && i <= max_index_dst) {
1050 char *route_name = bprintf("%s#%s",
1051 (*(routing_component_t *)
1053 (path_src, max)))->name,
1054 (*(routing_component_t *)
1056 (path_dst, i)))->name);
1058 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1059 xbt_free(route_name);
1068 if (max <= max_index_src && max <= max_index_dst) {
1069 char *route_name = bprintf("%s#%s",
1070 (*(routing_component_t *)
1072 (path_src, max)))->name,
1073 (*(routing_component_t *)
1075 (path_dst, max)))->name);
1076 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
1077 xbt_free(route_name);
1083 xbt_dynar_free(&path_src);
1084 xbt_dynar_free(&path_dst);
1086 route_extended_t new_e_route = NULL;
1088 if (e_route_bypass) {
1090 unsigned int cpt = 0;
1091 new_e_route = xbt_new0(s_route_extended_t, 1);
1092 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
1093 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
1094 new_e_route->generic_route.link_list =
1095 xbt_dynar_new(global_routing->size_of_link, NULL);
1096 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
1097 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1104 /* ************************************************************************** */
1105 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1108 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
1109 void *data, int order)
1115 xbt_dynar_t links = NULL, links_id = NULL;
1117 new_route = xbt_new0(s_route_t, 1);
1118 new_route->link_list =
1119 xbt_dynar_new(global_routing->size_of_link, NULL);
1121 xbt_assert(hierarchy == SURF_ROUTING_BASE,
1122 "the hierarchy type is not SURF_ROUTING_BASE");
1124 links = ((route_t) data)->link_list;
1127 links_id = new_route->link_list;
1129 xbt_dynar_foreach(links, cpt, link_name) {
1132 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1135 xbt_dynar_push(links_id, &link);
1137 xbt_dynar_unshift(links_id, &link);
1139 THROWF(mismatch_error, 0, "Link %s not found", link_name);
1146 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
1147 void *data, int order)
1151 route_extended_t e_route, new_e_route;
1154 xbt_dynar_t links = NULL, links_id = NULL;
1156 new_e_route = xbt_new0(s_route_extended_t, 1);
1157 new_e_route->generic_route.link_list =
1158 xbt_dynar_new(global_routing->size_of_link, NULL);
1159 new_e_route->src_gateway = NULL;
1160 new_e_route->dst_gateway = NULL;
1162 xbt_assert(hierarchy == SURF_ROUTING_BASE
1163 || hierarchy == SURF_ROUTING_RECURSIVE,
1164 "the hierarchy type is not defined");
1166 if (hierarchy == SURF_ROUTING_BASE) {
1168 route = (route_t) data;
1169 links = route->link_list;
1171 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
1173 e_route = (route_extended_t) data;
1174 xbt_assert(e_route->src_gateway
1175 && e_route->dst_gateway, "bad gateway, is null");
1176 links = e_route->generic_route.link_list;
1178 /* remeber not erase the gateway names */
1179 new_e_route->src_gateway = strdup(e_route->src_gateway);
1180 new_e_route->dst_gateway = strdup(e_route->dst_gateway);
1183 links_id = new_e_route->generic_route.link_list;
1185 xbt_dynar_foreach(links, cpt, link_name) {
1188 xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
1191 xbt_dynar_push(links_id, &link);
1193 xbt_dynar_unshift(links_id, &link);
1195 THROWF(mismatch_error, 0, "Link %s not found", link_name);
1201 void generic_free_route(route_t route)
1204 xbt_dynar_free(&(route->link_list));
1209 void generic_free_extended_route(route_extended_t e_route)
1212 xbt_dynar_free(&(e_route->generic_route.link_list));
1213 if (e_route->src_gateway)
1214 xbt_free(e_route->src_gateway);
1215 if (e_route->dst_gateway)
1216 xbt_free(e_route->dst_gateway);
1221 static routing_component_t generic_as_exist(routing_component_t find_from,
1222 routing_component_t to_find)
1224 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1225 xbt_dict_cursor_t cursor = NULL;
1228 routing_component_t elem;
1229 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
1230 if (to_find == elem || generic_as_exist(elem, to_find)) {
1241 generic_autonomous_system_exist(routing_component_t rc, char *element)
1243 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
1244 routing_component_t element_as, result, elem;
1245 xbt_dict_cursor_t cursor = NULL;
1247 element_as = ((network_element_info_t)
1248 xbt_lib_get_or_null(as_router_lib, element, ROUTING_ASR_LEVEL))->rc_component;
1249 result = ((routing_component_t) - 1);
1250 if (element_as != rc)
1251 result = generic_as_exist(rc, element_as);
1255 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
1256 found = !strcmp(elem->name, element);
1267 generic_processing_units_exist(routing_component_t rc, char *element)
1269 routing_component_t element_as;
1270 element_as = ((network_element_info_t)
1271 xbt_lib_get_or_null(host_lib,
1272 element, ROUTING_HOST_LEVEL))->rc_component;
1273 if (element_as == rc)
1275 return generic_as_exist(rc, element_as);
1278 void generic_src_dst_check(routing_component_t rc, const char *src,
1282 void * src_data = xbt_lib_get_or_null(host_lib,src, ROUTING_HOST_LEVEL);
1283 void * dst_data = xbt_lib_get_or_null(host_lib,dst, ROUTING_HOST_LEVEL);
1284 if(!src_data) src_data = xbt_lib_get_or_null(as_router_lib,src, ROUTING_ASR_LEVEL);
1285 if(!dst_data) dst_data = xbt_lib_get_or_null(as_router_lib,dst, ROUTING_ASR_LEVEL);
1287 if(src_data == NULL || dst_data == NULL)
1288 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
1289 src, dst, rc->name);
1291 routing_component_t src_as = ((network_element_info_t)src_data)->rc_component;
1292 routing_component_t dst_as = ((network_element_info_t)dst_data)->rc_component;
1294 if(src_as != dst_as)
1295 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
1296 src, src_as->name, dst, dst_as->name);
1298 xbt_die("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
1299 src,dst,src_as->name, dst_as->name,rc->name);
1302 static void routing_parse_cluster(void) {
1303 char *host_id, *groups, *link_id = NULL;
1305 s_sg_platf_host_cbarg_t host;
1306 s_sg_platf_link_cbarg_t link;
1308 if( strcmp(struct_cluster->availability_trace,"")
1309 || strcmp(struct_cluster->state_trace,"") )
1311 if(xbt_dict_size(patterns)==0)
1312 patterns = xbt_dict_new();
1313 xbt_dict_set(patterns,"id",xbt_strdup(struct_cluster->id),free);
1314 xbt_dict_set(patterns,"prefix",xbt_strdup(struct_cluster->prefix),free);
1315 xbt_dict_set(patterns,"suffix",xbt_strdup(struct_cluster->suffix),free);
1320 xbt_dynar_t radical_elements;
1321 xbt_dynar_t radical_ends;
1323 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Cluster\">", struct_cluster->id);
1324 sg_platf_new_AS_open(struct_cluster->id,"Cluster");
1327 radical_elements = xbt_str_split(struct_cluster->radical, ",");
1328 xbt_dynar_foreach(radical_elements, iter, groups) {
1329 memset(&host,0,sizeof(host));
1331 radical_ends = xbt_str_split(groups, "-");
1332 switch (xbt_dynar_length(radical_ends)) {
1334 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1335 host_id = bprintf("%s%d%s", struct_cluster->prefix, start, struct_cluster->suffix);
1336 link_id = bprintf("%s_link_%d", struct_cluster->id, start);
1338 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1340 if(strcmp(struct_cluster->availability_trace,"")){
1341 xbt_dict_set(patterns, "radical", bprintf("%d", start), xbt_free);
1342 char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1343 xbt_str_varsubst(tmp_availability_file,patterns);
1344 XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1345 host.power_trace = tmgr_trace_new(tmp_availability_file);
1346 xbt_free(tmp_availability_file);
1350 XBT_DEBUG("\tavailability_file=\"\"");
1352 if(strcmp(struct_cluster->state_trace,"")){
1353 char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1354 xbt_str_varsubst(tmp_state_file,patterns);
1355 XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1356 host.state_trace = tmgr_trace_new(tmp_state_file);
1357 xbt_free(tmp_state_file);
1361 XBT_DEBUG("\tstate_file=\"\"");
1364 host.power_peak = struct_cluster->power;
1365 host.power_scale = 1.0;
1366 host.core_amount = struct_cluster->core_amount;
1367 host.initial_state = SURF_RESOURCE_ON;
1369 sg_platf_new_host(&host);
1370 XBT_DEBUG("</host>");
1373 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1375 memset(&link,0,sizeof(link));
1377 link.bandwidth = struct_cluster->bw;
1378 link.latency = struct_cluster->lat;
1379 link.state = SURF_RESOURCE_ON;
1381 switch (struct_cluster->sharing_policy) {
1382 case A_surfxml_cluster_sharing_policy_FATPIPE:
1383 link.policy = SURF_LINK_FATPIPE;
1385 case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1386 link.policy = SURF_LINK_FULLDUPLEX;
1389 link.policy = SURF_LINK_SHARED;
1392 sg_platf_new_link(&link);
1394 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1395 if (struct_cluster->sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX){
1396 char* tmp_link = bprintf("%s_UP",link_id);
1397 info->link_up = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1399 tmp_link = bprintf("%s_DOWN",link_id);
1400 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1404 info->link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1405 info->link_down = info->link_up;
1407 surf_routing_cluster_add_link(host_id,info);
1415 start=surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1416 end= surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1417 for (i = start; i <= end; i++) {
1418 host_id = bprintf("%s%d%s", struct_cluster->prefix, i, struct_cluster->suffix);
1419 link_id = bprintf("%s_link_%d", struct_cluster->id, i);
1421 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\">", host_id, struct_cluster->power);
1423 if(strcmp(struct_cluster->availability_trace,"")){
1424 xbt_dict_set(patterns, "radical", bprintf("%d", i), xbt_free);
1425 char* tmp_availability_file = xbt_strdup(struct_cluster->availability_trace);
1426 xbt_str_varsubst(tmp_availability_file,patterns);
1427 XBT_DEBUG("\tavailability_file=\"%s\"",tmp_availability_file);
1428 host.power_trace = tmgr_trace_new(tmp_availability_file);
1429 xbt_free(tmp_availability_file);
1433 XBT_DEBUG("\tavailability_file=\"\"");
1435 if(strcmp(struct_cluster->state_trace,"")){
1436 char *tmp_state_file = xbt_strdup(struct_cluster->state_trace);
1437 xbt_str_varsubst(tmp_state_file,patterns);
1438 XBT_DEBUG("\tstate_file=\"%s\"",tmp_state_file);
1439 host.state_trace = tmgr_trace_new(tmp_state_file);
1440 xbt_free(tmp_state_file);
1444 XBT_DEBUG("\tstate_file=\"\"");
1447 host.power_peak = struct_cluster->power;
1448 host.power_scale = 1.0;
1449 host.core_amount = struct_cluster->core_amount;
1450 host.initial_state = SURF_RESOURCE_ON;
1452 sg_platf_new_host(&host);
1453 XBT_DEBUG("</host>");
1455 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id,struct_cluster->bw, struct_cluster->lat);
1457 memset(&link,0,sizeof(link));
1459 link.bandwidth = struct_cluster->bw;
1460 link.latency = struct_cluster->lat;
1461 link.state = SURF_RESOURCE_ON;
1463 switch (struct_cluster->sharing_policy) {
1464 case A_surfxml_cluster_sharing_policy_SHARED:
1465 link.policy = SURF_LINK_SHARED;
1467 case A_surfxml_cluster_sharing_policy_FULLDUPLEX:
1468 link.policy = SURF_LINK_FULLDUPLEX;
1470 case A_surfxml_cluster_sharing_policy_FATPIPE:
1471 link.policy = SURF_LINK_FATPIPE;
1474 surf_parse_error(bprintf("Invalid cluster sharing policy for cluster %s",struct_cluster->id));
1476 sg_platf_new_link(&link);
1478 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1479 if (link.policy == SURF_LINK_FULLDUPLEX) {
1480 char* tmp_link = bprintf("%s_UP",link_id);
1481 info->link_up = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1483 tmp_link = bprintf("%s_DOWN",link_id);
1484 info->link_down = xbt_lib_get_or_null(link_lib, tmp_link, SURF_LINK_LEVEL);
1488 info->link_up = xbt_lib_get_or_null(link_lib, link_id, SURF_LINK_LEVEL);
1489 info->link_down = info->link_up;
1491 surf_routing_cluster_add_link(host_id,info);
1500 XBT_DEBUG("Malformed radical");
1504 xbt_dynar_free(&radical_ends);
1506 xbt_dynar_free(&radical_elements);
1508 // Add a router. It is magically used thanks to the way in which surf_routing_cluster is written, and it's very useful to connect clusters together
1510 XBT_DEBUG("<router id=\"%s\"/>", struct_cluster->router_id);
1511 s_sg_platf_router_cbarg_t router;
1513 memset(&router,0,sizeof(router));
1514 router.id = struct_cluster->router_id;
1516 if (!router.id || !strcmp(router.id,""))
1517 router.id = newid = bprintf("%s%s_router%s", struct_cluster->prefix, struct_cluster->id, struct_cluster->suffix);
1518 sg_platf_new_router(&router);
1523 if( (struct_cluster->bb_bw!= 0) && (struct_cluster->bb_lat!=0) ){
1524 char *link_backbone = bprintf("%s_backbone", struct_cluster->id);
1525 XBT_DEBUG("<link\tid=\"%s\" bw=\"%f\" lat=\"%f\"/>", link_backbone,struct_cluster->bb_bw, struct_cluster->bb_lat);
1527 memset(&link,0,sizeof(link));
1528 link.id = link_backbone;
1529 link.bandwidth = struct_cluster->bb_bw;
1530 link.latency = struct_cluster->bb_lat;
1531 link.state = SURF_RESOURCE_ON;
1533 switch (struct_cluster->bb_sharing_policy) {
1534 case A_surfxml_cluster_bb_sharing_policy_FATPIPE:
1535 link.policy = SURF_LINK_FATPIPE;
1537 case A_surfxml_cluster_bb_sharing_policy_SHARED:
1538 link.policy = SURF_LINK_SHARED;
1541 surf_parse_error(bprintf("Invalid bb sharing policy in cluster %s",struct_cluster->id));
1544 sg_platf_new_link(&link);
1546 surf_parsing_link_up_down_t info = xbt_new0(s_surf_parsing_link_up_down_t, 1);
1547 info->link_up = xbt_lib_get_or_null(link_lib, link_backbone, SURF_LINK_LEVEL);
1548 info->link_down = info->link_up;
1549 surf_routing_cluster_add_link(struct_cluster->id, info);
1551 free(link_backbone);
1556 char *new_suffix = xbt_strdup("");
1558 radical_elements = xbt_str_split(struct_cluster->suffix, ".");
1559 xbt_dynar_foreach(radical_elements, iter, groups) {
1560 if (strcmp(groups, "")) {
1561 char *old_suffix = new_suffix;
1562 new_suffix = bprintf("%s\\.%s", old_suffix, groups);
1567 xbt_dynar_free(&radical_elements);
1568 xbt_free(new_suffix);
1570 if( strcmp(struct_cluster->availability_trace,"")
1571 || strcmp(struct_cluster->state_trace,"") )
1572 xbt_dict_free(&patterns);
1575 sg_platf_new_AS_close();
1579 * This function take a string and replace parameters from patterns dict.
1580 * It returns the new value.
1582 static char* replace_random_parameter(char * string)
1584 char *test_string = NULL;
1586 if(xbt_dict_size(random_value)==0)
1589 string = xbt_str_varsubst(string, patterns); // for patterns of cluster
1590 test_string = bprintf("${%s}", string);
1591 test_string = xbt_str_varsubst(test_string,random_value); //Add ${xxxxx} for random Generator
1593 if (strcmp(test_string,"")) { //if not empty, keep this value.
1595 string = test_string;
1596 } //In other case take old value (without ${})
1602 static void routing_parse_postparse(void)
1604 xbt_dict_free(&random_value);
1605 xbt_dict_free(&patterns);
1608 static void routing_parse_peer(sg_platf_peer_cbarg_t peer)
1610 static int AX_ptr = 0;
1611 char *host_id = NULL;
1612 char *router_id, *link_router, *link_backbone, *link_id_up, *link_id_down;
1614 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
1615 static unsigned int surfxml_buffer_stack_stack[1024];
1617 surfxml_buffer_stack_stack[0] = 0;
1619 surfxml_bufferstack_push(1);
1621 XBT_DEBUG("<AS id=\"%s\"\trouting=\"Full\">", peer->id);
1622 sg_platf_new_AS_open(peer->id, "Full");
1625 host_id = HOST_PEER(peer->id);
1626 router_id = ROUTER_PEER(peer->id);
1627 link_id_up = LINK_UP_PEER(peer->id);
1628 link_id_down = LINK_DOWN_PEER(peer->id);
1630 link_router = bprintf("%s_link_router", peer->id);
1631 link_backbone = bprintf("%s_backbone", peer->id);
1633 XBT_DEBUG("<host\tid=\"%s\"\tpower=\"%f\"/>", host_id, peer->power);
1634 s_sg_platf_host_cbarg_t host;
1635 memset(&host,0,sizeof(host));
1636 host.initial_state = SURF_RESOURCE_ON;
1638 host.power_peak = peer->power;
1639 host.power_scale = 1.0;
1640 host.power_trace = peer->availability_trace;
1641 host.state_trace = peer->state_trace;
1642 host.core_amount = 1;
1643 sg_platf_new_host(&host);
1646 XBT_DEBUG("<router id=\"%s\"\tcoordinates=\"%s\"/>", router_id, peer->coord);
1647 s_sg_platf_router_cbarg_t router;
1648 memset(&router,0,sizeof(router));
1649 router.id = router_id;
1650 router.coord = peer->coord;
1651 sg_platf_new_router(&router);
1653 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_up, peer->bw_in, peer->lat);
1654 s_sg_platf_link_cbarg_t link;
1655 memset(&link,0,sizeof(link));
1656 link.state = SURF_RESOURCE_ON;
1657 link.policy = SURF_LINK_SHARED;
1658 link.id = link_id_up;
1659 link.bandwidth = peer->bw_in;
1660 link.latency = peer->lat;
1661 sg_platf_new_link(&link);
1663 // FIXME: dealing with full duplex is not the role of this piece of code, I'd say [Mt]
1664 // Instead, it should be created fullduplex, and the models will do what's needed in this case
1665 XBT_DEBUG("<link\tid=\"%s\"\tbw=\"%f\"\tlat=\"%f\"/>", link_id_down, peer->bw_out, peer->lat);
1666 link.id = link_id_down;
1667 sg_platf_new_link(&link);
1672 XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", host_id, router_id);
1673 XBT_DEBUG("symmetrical=\"NO\">");
1674 SURFXML_BUFFER_SET(route_src, host_id);
1675 SURFXML_BUFFER_SET(route_dst, router_id);
1676 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1677 SURFXML_START_TAG(route);
1679 XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_up);
1680 SURFXML_BUFFER_SET(link_ctn_id, link_id_up);
1681 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1682 SURFXML_START_TAG(link_ctn);
1683 SURFXML_END_TAG(link_ctn);
1685 XBT_DEBUG("</route>");
1686 SURFXML_END_TAG(route);
1689 XBT_DEBUG("<route\tsrc=\"%s\"\tdst=\"%s\"", router_id, host_id);
1690 XBT_DEBUG("symmetrical=\"NO\">");
1691 SURFXML_BUFFER_SET(route_src, router_id);
1692 SURFXML_BUFFER_SET(route_dst, host_id);
1693 A_surfxml_route_symmetrical = A_surfxml_route_symmetrical_NO;
1694 SURFXML_START_TAG(route);
1696 XBT_DEBUG("<link_ctn\tid=\"%s\"/>", link_id_down);
1697 SURFXML_BUFFER_SET(link_ctn_id, link_id_down);
1698 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
1699 SURFXML_START_TAG(link_ctn);
1700 SURFXML_END_TAG(link_ctn);
1702 XBT_DEBUG("</route>");
1703 SURFXML_END_TAG(route);
1706 sg_platf_new_AS_close();
1709 //xbt_dynar_free(&tab_elements_num);
1713 free(link_backbone);
1716 surfxml_bufferstack_pop(1);
1719 static void routing_parse_Srandom(void)
1721 double mean, std, min, max, seed;
1722 char *random_id = A_surfxml_random_id;
1723 char *random_radical = A_surfxml_random_radical;
1724 char *rd_name = NULL;
1726 mean = surf_parse_get_double(A_surfxml_random_mean);
1727 std = surf_parse_get_double(A_surfxml_random_std_deviation);
1728 min = surf_parse_get_double(A_surfxml_random_min);
1729 max = surf_parse_get_double(A_surfxml_random_max);
1730 seed = surf_parse_get_double(A_surfxml_random_seed);
1734 random_data_t random = xbt_new0(s_random_data_t, 1);
1737 xbt_dynar_t radical_elements;
1741 xbt_dynar_t radical_ends;
1743 random->generator = A_surfxml_random_generator;
1744 random->seed = seed;
1748 /* Check user stupidities */
1750 THROWF(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
1752 THROWF(arg_error, 0, "random->mean < random->min (%f < %f)", mean,
1755 THROWF(arg_error, 0, "random->mean > random->max (%f > %f)", mean,
1758 /* normalize the mean and standard deviation before storing */
1759 random->mean = (mean - min) / (max - min);
1760 random->std = std / (max - min);
1762 if (random->mean * (1 - random->mean) < random->std * random->std)
1763 THROWF(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
1764 random->mean, random->std);
1766 XBT_DEBUG("id = '%s' min = '%f' max = '%f' mean = '%f' std_deviatinon = '%f' generator = '%d' seed = '%ld' radical = '%s'",
1776 if(xbt_dict_size(random_value)==0)
1777 random_value = xbt_dict_new();
1779 if(!strcmp(random_radical,""))
1781 res = random_generate(random);
1782 rd_value = bprintf("%f",res);
1783 xbt_dict_set(random_value, random_id, rd_value, free);
1787 radical_elements = xbt_str_split(random_radical, ",");
1788 xbt_dynar_foreach(radical_elements, iter, groups) {
1789 radical_ends = xbt_str_split(groups, "-");
1790 switch (xbt_dynar_length(radical_ends)) {
1792 xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",random_id);
1793 res = random_generate(random);
1794 tmpbuf = bprintf("%s%d",random_id,atoi(xbt_dynar_getfirst_as(radical_ends,char *)));
1795 xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1800 start = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 0, char *));
1801 end = surf_parse_get_int(xbt_dynar_get_as(radical_ends, 1, char *));
1802 for (i = start; i <= end; i++) {
1803 xbt_assert(!xbt_dict_get_or_null(random_value,random_id),"Custom Random '%s' already exists !",bprintf("%s%d",random_id,i));
1804 res = random_generate(random);
1805 tmpbuf = bprintf("%s%d",random_id,i);
1806 xbt_dict_set(random_value, tmpbuf, bprintf("%f",res), free);
1811 XBT_INFO("Malformed radical");
1814 res = random_generate(random);
1815 rd_name = bprintf("%s_router",random_id);
1816 rd_value = bprintf("%f",res);
1817 xbt_dict_set(random_value, rd_name, rd_value, free);
1819 xbt_dynar_free(&radical_ends);
1822 xbt_dynar_free(&radical_elements);
1826 void routing_register_callbacks() {
1827 sg_platf_host_add_cb(parse_S_host);
1828 sg_platf_router_add_cb(parse_S_router);
1830 surfxml_add_callback(STag_surfxml_random_cb_list, &routing_parse_Srandom);
1832 surfxml_add_callback(STag_surfxml_route_cb_list, &routing_parse_S_route);
1833 surfxml_add_callback(STag_surfxml_ASroute_cb_list, &routing_parse_S_ASroute);
1834 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,&routing_parse_S_bypassRoute);
1836 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list, &routing_parse_link_ctn);
1838 surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_parse_E_route);
1839 surfxml_add_callback(ETag_surfxml_ASroute_cb_list, &routing_parse_E_ASroute);
1840 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,&routing_parse_E_bypassRoute);
1842 surfxml_add_callback(STag_surfxml_cluster_cb_list,
1843 &routing_parse_cluster);
1845 sg_platf_peer_add_cb(routing_parse_peer);
1846 sg_platf_postparse_add_cb(routing_parse_postparse);
1849 instr_routing_define_callbacks();