1 /* Copyright (c) 2009, 2010. 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. */
10 #include "gras_config.h"
13 #include <pcre.h> /* regular expresion library */
15 #include "surf_private.h"
16 #include "xbt/dynar.h"
18 #include "xbt/config.h"
19 #include "xbt/graph.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
25 routing_global_t global_routing = NULL;
26 routing_component_t current_routing = NULL;
27 model_type_t current_routing_model = NULL;
29 /* Prototypes of each model */
30 static void *model_full_create(void); /* create structures for full routing model */
31 static void model_full_load(void); /* load parse functions for full routing model */
32 static void model_full_unload(void); /* unload parse functions for full routing model */
33 static void model_full_end(void); /* finalize the creation of full routing model */
34 static void model_full_set_route( /* Set the route and ASroute between src and dst */
35 routing_component_t rc, const char *src, const char *dst, name_route_extended_t route);
37 static void *model_floyd_create(void); /* create structures for floyd routing model */
38 static void model_floyd_load(void); /* load parse functions for floyd routing model */
39 static void model_floyd_unload(void); /* unload parse functions for floyd routing model */
40 static void model_floyd_end(void); /* finalize the creation of floyd routing model */
41 static void model_floyd_set_route(routing_component_t rc, const char *src,
42 const char *dst, name_route_extended_t route);
44 static void *model_dijkstra_both_create(int cached); /* create by calling dijkstra or dijkstracache */
45 static void *model_dijkstra_create(void); /* create structures for dijkstra routing model */
46 static void *model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
47 static void model_dijkstra_both_load(void); /* load parse functions for dijkstra routing model */
48 static void model_dijkstra_both_unload(void); /* unload parse functions for dijkstra routing model */
49 static void model_dijkstra_both_end(void); /* finalize the creation of dijkstra routing model */
50 static void model_dijkstra_both_set_route (routing_component_t rc, const char *src,
51 const char *dst, name_route_extended_t route);
53 static void *model_rulebased_create(void); /* create structures for rulebased routing model */
54 static void model_rulebased_load(void); /* load parse functions for rulebased routing model */
55 static void model_rulebased_unload(void); /* unload parse functions for rulebased routing model */
56 static void model_rulebased_end(void); /* finalize the creation of rulebased routing model */
58 static void *model_none_create(void); /* none routing model */
59 static void model_none_load(void); /* none routing model */
60 static void model_none_unload(void); /* none routing model */
61 static void model_none_end(void); /* none routing model */
63 static void routing_parse_Scluster(void); /*cluster bypass */
65 static void routing_parse_Sconfig(void); /*config Tag */
66 static void routing_parse_Econfig(void); /*config Tag */
68 /* this lines are only for replace use like index in the model table */
73 SURF_MODEL_DIJKSTRACACHE,
81 /* must be finish with null and carefull if change de order */
82 struct s_model_type routing_models[] = { {"Full",
83 "Full routing data (fast, large memory requirements, fully expressive)",
89 "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
90 model_floyd_create, model_floyd_load, model_floyd_unload,
93 "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
94 model_dijkstra_create, model_dijkstra_both_load,
95 model_dijkstra_both_unload, model_dijkstra_both_end},
97 "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
98 model_dijkstracache_create, model_dijkstra_both_load,
99 model_dijkstra_both_unload, model_dijkstra_both_end},
100 {"none", "No routing (usable with Constant network only)",
101 model_none_create, model_none_load, model_none_unload, model_none_end},
103 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
104 model_rulebased_load, model_rulebased_unload, model_rulebased_end},
106 {NULL, NULL, NULL, NULL, NULL, NULL}
109 /* ************************************************************************** */
110 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
112 static void generic_set_processing_unit(routing_component_t rc,
114 static void generic_set_autonomous_system(routing_component_t rc,
116 static void generic_set_bypassroute(routing_component_t rc,
117 const char *src, const char *dst,
118 route_extended_t e_route);
120 static int surf_link_resource_cmp(const void *a, const void *b);
121 static int surf_pointer_resource_cmp(const void *a, const void *b);
123 /* ************************************************************************** */
124 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
126 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
127 static route_extended_t generic_get_bypassroute(routing_component_t rc,
131 /* ************************************************************************** */
132 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
134 static route_extended_t
135 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
136 void *data, int order);
138 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
139 void *data, int order);
140 static void generic_free_route(route_t route);
141 static void generic_free_extended_route(route_extended_t e_route);
142 static routing_component_t
143 generic_autonomous_system_exist(routing_component_t rc, char *element);
144 static routing_component_t
145 generic_processing_units_exist(routing_component_t rc, char *element);
146 static void generic_src_dst_check(routing_component_t rc, const char *src,
149 /* ************************************************************************** */
150 /* **************************** GLOBAL FUNCTIONS **************************** */
152 /* global parse functions */
153 static char *src = NULL; /* temporary store the source name of a route */
154 static char *dst = NULL; /* temporary store the destination name of a route */
155 static char *gw_src = NULL; /* temporary store the gateway source name of a route */
156 static char *gw_dst = NULL; /* temporary store the gateway destination name of a route */
157 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
159 * \brief Add a "host" to the network element list
161 static void parse_S_host(char *host_id)
163 network_element_info_t info = NULL;
164 if (current_routing->hierarchy == SURF_ROUTING_NULL)
165 current_routing->hierarchy = SURF_ROUTING_BASE;
166 xbt_assert1(!xbt_dict_get_or_null
167 (global_routing->where_network_elements, host_id),
168 "Reading a host, processing unit \"%s\" already exists",
170 xbt_assert1(current_routing->set_processing_unit,
171 "no defined method \"set_processing_unit\" in \"%s\"",
172 current_routing->name);
173 (*(current_routing->set_processing_unit)) (current_routing, host_id);
174 info = xbt_new0(s_network_element_info_t, 1);
175 info->rc_component = current_routing;
176 info->rc_type = SURF_NETWORK_ELEMENT_HOST;
177 xbt_dict_set(global_routing->where_network_elements, host_id,
178 (void *) info, NULL);
182 * \brief Add a host to the network element list from XML
184 static void parse_S_host_XML(void)
186 parse_S_host(A_surfxml_host_id);
190 * \brief Add a host to the network element list from lua script
192 static void parse_S_host_lua(char *host_id)
194 parse_S_host(host_id);
199 * \brief Add a "router" to the network element list
201 static void parse_S_router(void)
203 network_element_info_t info = NULL;
205 if (current_routing->hierarchy == SURF_ROUTING_NULL)
206 current_routing->hierarchy = SURF_ROUTING_BASE;
207 xbt_assert1(!xbt_dict_get_or_null
208 (global_routing->where_network_elements,
209 A_surfxml_router_id),
210 "Reading a router, processing unit \"%s\" already exists",
211 A_surfxml_router_id);
212 xbt_assert1(current_routing->set_processing_unit,
213 "no defined method \"set_processing_unit\" in \"%s\"",
214 current_routing->name);
215 (*(current_routing->set_processing_unit)) (current_routing,
216 A_surfxml_router_id);
217 info = xbt_new0(s_network_element_info_t, 1);
218 info->rc_component = current_routing;
219 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
220 xbt_dict_set(global_routing->where_network_elements, A_surfxml_router_id,
221 (void *) info, NULL);
223 TRACE_surf_host_declaration(A_surfxml_router_id, 0);
228 * \brief Set the endponints for a route
230 static void parse_S_route_new_and_endpoints(char *src_id, char *dst_id)
232 if (src != NULL && dst != NULL && link_list != NULL)
233 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
237 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0,
238 "Some limits are null in the route between \"%s\" and \"%s\"",
240 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
244 * \breif Set the endpoints for a route from XML
246 static void parse_S_route_new_and_endpoints_XML(void)
248 parse_S_route_new_and_endpoints(A_surfxml_route_src,
249 A_surfxml_route_dst);
253 * \breif Set the endpoints for a route from lua
255 static void parse_S_route_new_and_endpoints_lua(char *id_src, char *id_dst)
257 parse_S_route_new_and_endpoints(id_src, id_dst);
261 * \brief Set the endponints and gateways for a ASroute
263 static void parse_S_ASroute_new_and_endpoints(void)
265 if (src != NULL && dst != NULL && link_list != NULL)
266 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
267 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
268 src = A_surfxml_ASroute_src;
269 dst = A_surfxml_ASroute_dst;
270 gw_src = A_surfxml_ASroute_gw_src;
271 gw_dst = A_surfxml_ASroute_gw_dst;
272 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
273 || strlen(gw_dst) > 0,
274 "Some limits are null in the route between \"%s\" and \"%s\"",
276 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
280 * \brief Set the endponints for a bypassRoute
282 static void parse_S_bypassRoute_new_and_endpoints(void)
284 if (src != NULL && dst != NULL && link_list != NULL)
286 "Bypass Route between %s to %s can not be defined",
287 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
288 src = A_surfxml_bypassRoute_src;
289 dst = A_surfxml_bypassRoute_dst;
290 gw_src = A_surfxml_bypassRoute_gw_src;
291 gw_dst = A_surfxml_bypassRoute_gw_dst;
292 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
293 || strlen(gw_dst) > 0,
294 "Some limits are null in the route between \"%s\" and \"%s\"",
296 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
300 * \brief Set a new link on the actual list of link for a route or ASroute
302 static void parse_E_link_ctn_new_elem(char *link_id)
305 val = xbt_strdup(link_id);
306 xbt_dynar_push(link_list, &val);
310 * \brief Set a new link on the actual list of link for a route or ASroute from XML
313 static void parse_E_link_ctn_new_elem_XML(void)
315 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
316 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
317 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP)
318 parse_E_link_ctn_new_elem(bprintf("%s_UP",A_surfxml_link_ctn_id));
319 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN)
320 parse_E_link_ctn_new_elem(bprintf("%s_DOWN",A_surfxml_link_ctn_id));
324 * \brief Set a new link on the actual list of link for a route or ASroute from lua
326 static void parse_E_link_c_ctn_new_elem_lua(char *link_id)
328 parse_E_link_ctn_new_elem(link_id);
332 * \brief Store the route by calling the set_route function of the current routing component
334 static void parse_E_route_store_route(void)
336 name_route_extended_t route = xbt_new0(s_name_route_extended_t, 1);
337 route->generic_route.link_list = link_list;
338 xbt_assert1(current_routing->set_route,
339 "no defined method \"set_route\" in \"%s\"",
340 current_routing->name);
341 (*(current_routing->set_route)) (current_routing, src, dst, route);
348 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
350 static void parse_E_ASroute_store_route(void)
352 name_route_extended_t e_route = xbt_new0(s_name_route_extended_t, 1);
353 e_route->generic_route.link_list = link_list;
354 e_route->src_gateway = xbt_strdup(gw_src);
355 e_route->dst_gateway = xbt_strdup(gw_dst);
356 xbt_assert1(current_routing->set_ASroute,
357 "no defined method \"set_ASroute\" in \"%s\"",
358 current_routing->name);
359 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
368 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
370 static void parse_E_bypassRoute_store_route(void)
372 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
373 e_route->generic_route.link_list = link_list;
374 e_route->src_gateway = xbt_strdup(gw_src);
375 e_route->dst_gateway = xbt_strdup(gw_dst);
376 xbt_assert1(current_routing->set_bypassroute,
377 "no defined method \"set_bypassroute\" in \"%s\"",
378 current_routing->name);
379 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
389 * \brief Make a new routing component
391 * make the new structure and
392 * set the parsing functions to allows parsing the part of the routing tree
394 static void parse_S_AS(char *AS_id, char *AS_routing)
396 routing_component_t new_routing;
397 model_type_t model = NULL;
398 char *wanted = AS_routing;
400 /* seach the routing model */
401 for (cpt = 0; routing_models[cpt].name; cpt++)
402 if (!strcmp(wanted, routing_models[cpt].name))
403 model = &routing_models[cpt];
404 /* if its not exist, error */
406 fprintf(stderr, "Routing model %s not found. Existing models:\n",
408 for (cpt = 0; routing_models[cpt].name; cpt++)
409 if (!strcmp(wanted, routing_models[cpt].name))
410 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
411 routing_models[cpt].desc);
415 /* make a new routing component */
416 new_routing = (routing_component_t) (*(model->create)) ();
417 new_routing->routing = model;
418 new_routing->hierarchy = SURF_ROUTING_NULL;
419 new_routing->name = xbt_strdup(AS_id);
420 new_routing->routing_sons = xbt_dict_new();
422 if (current_routing == NULL && global_routing->root == NULL) {
424 /* it is the first one */
425 new_routing->routing_father = NULL;
426 global_routing->root = new_routing;
428 } else if (current_routing != NULL && global_routing->root != NULL) {
430 xbt_assert1(!xbt_dict_get_or_null
431 (current_routing->routing_sons, AS_id),
432 "The AS \"%s\" already exists", AS_id);
433 /* it is a part of the tree */
434 new_routing->routing_father = current_routing;
435 /* set the father behavior */
436 if (current_routing->hierarchy == SURF_ROUTING_NULL)
437 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
438 /* add to the sons dictionary */
439 xbt_dict_set(current_routing->routing_sons, AS_id,
440 (void *) new_routing, NULL);
441 /* add to the father element list */
442 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
443 /* unload the prev parse rules */
444 (*(current_routing->routing->unload)) ();
447 THROW0(arg_error, 0, "All defined components must be belong to a AS");
449 /* set the new parse rules */
450 (*(new_routing->routing->load)) ();
451 /* set the new current component of the tree */
452 current_routing = new_routing;
456 * Detect the routing model type of the routing component from XML platforms
458 static void parse_S_AS_XML(void)
460 parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
464 * define the routing model type of routing component from lua script
466 static void parse_S_AS_lua(char *id, char *mode)
468 parse_S_AS(id, mode);
473 * \brief Finish the creation of a new routing component
475 * When you finish to read the routing component, other structures must be created.
476 * the "end" method allow to do that for any routing model type
478 static void parse_E_AS(char *AS_id)
481 if (current_routing == NULL) {
482 THROW1(arg_error, 0, "Close AS(%s), that never open", AS_id);
484 network_element_info_t info = NULL;
485 xbt_assert1(!xbt_dict_get_or_null
486 (global_routing->where_network_elements,
487 current_routing->name), "The AS \"%s\" already exists",
488 current_routing->name);
489 info = xbt_new0(s_network_element_info_t, 1);
490 info->rc_component = current_routing->routing_father;
491 info->rc_type = SURF_NETWORK_ELEMENT_AS;
492 xbt_dict_set(global_routing->where_network_elements,
493 current_routing->name, info, NULL);
494 (*(current_routing->routing->unload)) ();
495 (*(current_routing->routing->end)) ();
496 current_routing = current_routing->routing_father;
497 if (current_routing != NULL)
498 (*(current_routing->routing->load)) ();
503 * \brief Finish the creation of a new routing component from XML
505 static void parse_E_AS_XML(void)
507 parse_E_AS(A_surfxml_AS_id);
511 * \brief Finish the creation of a new routing component from lua
513 static void parse_E_AS_lua(char *id)
518 /* Aux Business methods */
521 * \brief Get the AS father and the first elements of the chain
523 * \param src the source host name
524 * \param dst the destination host name
526 * Get the common father of the to processing units, and the first different
527 * father in the chain
529 static xbt_dynar_t elements_father(const char *src, const char *dst)
532 xbt_assert0(src && dst, "bad parameters for \"elements_father\" method");
534 xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
536 routing_component_t src_as, dst_as;
537 int index_src, index_dst, index_father_src, index_father_dst;
538 xbt_dynar_t path_src = NULL;
539 xbt_dynar_t path_dst = NULL;
540 routing_component_t current = NULL;
541 routing_component_t *current_src = NULL;
542 routing_component_t *current_dst = NULL;
543 routing_component_t *father = NULL;
545 /* (1) find the as where the src and dst are located */
546 src_as = ((network_element_info_t)
547 xbt_dict_get_or_null(global_routing->where_network_elements,
549 dst_as = ((network_element_info_t)
550 xbt_dict_get_or_null(global_routing->where_network_elements,
554 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
557 /* (2) find the path to the root routing component */
558 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
560 while (current != NULL) {
561 xbt_dynar_push(path_src, ¤t);
562 current = current->routing_father;
564 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
566 while (current != NULL) {
567 xbt_dynar_push(path_dst, ¤t);
568 current = current->routing_father;
571 /* (3) find the common father */
572 index_src = (path_src->used) - 1;
573 index_dst = (path_dst->used) - 1;
574 current_src = xbt_dynar_get_ptr(path_src, index_src);
575 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
576 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
577 current_src = xbt_dynar_get_ptr(path_src, index_src);
578 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
584 current_src = xbt_dynar_get_ptr(path_src, index_src);
585 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
587 /* (4) they are not in the same routing component, make the path */
588 index_father_src = index_src + 1;
589 index_father_dst = index_dst + 1;
591 if (*current_src == *current_dst)
592 father = current_src;
594 father = xbt_dynar_get_ptr(path_src, index_father_src);
596 /* (5) result generation */
597 xbt_dynar_push(result, father); /* first same the father of src and dst */
598 xbt_dynar_push(result, current_src); /* second the first different father of src */
599 xbt_dynar_push(result, current_dst); /* three the first different father of dst */
601 xbt_dynar_free(&path_src);
602 xbt_dynar_free(&path_dst);
607 /* Global Business methods */
610 * \brief Recursive function for get_route
612 * \param src the source host name
613 * \param dst the destination host name
615 * This fuction is call by "get_route". It allow to walk through the
616 * routing components tree.
618 static route_extended_t _get_route(const char *src, const char *dst)
622 unsigned int cpt = 0;
624 DEBUG2("Solve route \"%s\" to \"%s\"", src, dst);
626 xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
628 route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
631 xbt_dynar_t elem_father_list = elements_father(src, dst);
633 routing_component_t common_father =
634 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
635 routing_component_t src_father =
636 xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
637 routing_component_t dst_father =
638 xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
640 e_route = xbt_new0(s_route_extended_t, 1);
641 e_route->src_gateway = NULL;
642 e_route->dst_gateway = NULL;
643 e_route->generic_route.link_list =
644 xbt_dynar_new(global_routing->size_of_link, NULL);
646 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
648 if (strcmp(src, dst)) {
650 (*(common_father->get_route)) (common_father, src, dst);
651 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
653 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
654 xbt_dynar_push(e_route->generic_route.link_list, &link);
656 generic_free_extended_route(e_route_cnt);
659 } else { /* SURF_ROUTING_RECURSIVE */
661 route_extended_t e_route_bypass = NULL;
663 if (common_father->get_bypass_route)
665 (*(common_father->get_bypass_route)) (common_father, src, dst);
668 e_route_cnt = e_route_bypass;
671 (*(common_father->get_route)) (common_father, src_father->name,
674 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
675 src_father->name, dst_father->name);
677 xbt_assert2((e_route_cnt->src_gateway == NULL) ==
678 (e_route_cnt->dst_gateway == NULL),
679 "bad gateway for route between \"%s\" and \"%s\"", src,
682 if (src != e_route_cnt->src_gateway) {
683 e_route_src = _get_route(src, e_route_cnt->src_gateway);
684 xbt_assert2(e_route_src, "no route between \"%s\" and \"%s\"", src,
685 e_route_cnt->src_gateway);
686 xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
687 xbt_dynar_push(e_route->generic_route.link_list, &link);
691 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
692 xbt_dynar_push(e_route->generic_route.link_list, &link);
695 if (e_route_cnt->dst_gateway != dst) {
696 e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
697 xbt_assert2(e_route_dst, "no route between \"%s\" and \"%s\"",
698 e_route_cnt->dst_gateway, dst);
699 xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
700 xbt_dynar_push(e_route->generic_route.link_list, &link);
704 e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
705 e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
707 generic_free_extended_route(e_route_src);
708 generic_free_extended_route(e_route_cnt);
709 generic_free_extended_route(e_route_dst);
712 xbt_dynar_free(&elem_father_list);
718 * \brief Generic method: find a route between hosts
720 * \param src the source host name
721 * \param dst the destination host name
723 * walk through the routing components tree and find a route between hosts
724 * by calling the differents "get_route" functions in each routing component.
725 * No need to free the returned dynar. It will be freed at the next call.
727 static xbt_dynar_t get_route(const char *src, const char *dst)
730 route_extended_t e_route;
731 xbt_dynar_t elem_father_list = elements_father(src, dst);
732 routing_component_t common_father =
733 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
735 if (strcmp(src, dst))
736 e_route = _get_route(src, dst);
738 e_route = (*(common_father->get_route)) (common_father, src, dst);
740 xbt_assert2(e_route, "no route between \"%s\" and \"%s\"", src, dst);
742 if (global_routing->last_route)
743 xbt_dynar_free(&(global_routing->last_route));
744 global_routing->last_route = e_route->generic_route.link_list;
746 if (e_route->src_gateway)
747 xbt_free(e_route->src_gateway);
748 if (e_route->dst_gateway)
749 xbt_free(e_route->dst_gateway);
752 xbt_dynar_free(&elem_father_list);
754 if (xbt_dynar_length(global_routing->last_route) == 0)
757 return global_routing->last_route;
761 * \brief Generic method: find a route between hosts
763 * \param src the source host name
764 * \param dst the destination host name
766 * walk through the routing components tree and find a route between hosts
767 * by calling the differents "get_route" functions in each routing component.
768 * Leaves the caller the responsability to clean the returned dynar.
770 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
772 xbt_dynar_t d = get_route(src,dst);
773 global_routing->last_route = NULL;
778 * \brief Recursive function for finalize
780 * \param rc the source host name
782 * This fuction is call by "finalize". It allow to finalize the
783 * AS or routing components. It delete all the structures.
785 static void _finalize(routing_component_t rc)
788 xbt_dict_cursor_t cursor = NULL;
790 routing_component_t elem;
791 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
794 xbt_dict_t tmp_sons = rc->routing_sons;
795 char *tmp_name = rc->name;
796 xbt_dict_free(&tmp_sons);
798 xbt_assert1(rc->finalize, "no defined method \"finalize\" in \"%s\"",
799 current_routing->name);
800 (*(rc->finalize)) (rc);
805 * \brief Generic method: delete all the routing structures
807 * walk through the routing components tree and delete the structures
808 * by calling the differents "finalize" functions in each routing component
810 static void finalize(void)
812 /* delete recursibly all the tree */
813 _finalize(global_routing->root);
814 /* delete "where" dict */
815 xbt_dict_free(&(global_routing->where_network_elements));
816 /* delete last_route */
817 xbt_dynar_free(&(global_routing->last_route));
818 /* delete global routing structure */
819 xbt_free(global_routing);
822 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
824 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
826 //adding my one link routes
829 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
831 xbt_dynar_foreach(onelink_mine, cpt, link) {
832 xbt_dynar_push(ret, &link);
837 xbt_dict_cursor_t cursor = NULL;
838 routing_component_t rc_child;
839 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
840 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
842 xbt_dynar_foreach(onelink_child, cpt, link) {
843 xbt_dynar_push(ret, &link);
850 static xbt_dynar_t get_onelink_routes(void)
852 return recursive_get_onelink_routes(global_routing->root);
855 static e_surf_network_element_type_t get_network_element_type(const char
858 network_element_info_t rc = NULL;
859 rc = xbt_dict_get(global_routing->where_network_elements, name);
864 * \brief Generic method: create the global routing schema
866 * Make a global routing structure and set all the parsing functions.
868 void routing_model_create(size_t size_of_links, void *loopback)
871 /* config the uniq global routing */
872 global_routing = xbt_new0(s_routing_global_t, 1);
873 global_routing->where_network_elements = xbt_dict_new();
874 global_routing->root = NULL;
875 global_routing->get_route = get_route;
876 global_routing->get_route_no_cleanup = get_route_no_cleanup;
877 global_routing->get_onelink_routes = get_onelink_routes;
878 global_routing->get_network_element_type = get_network_element_type;
879 global_routing->finalize = finalize;
880 global_routing->loopback = loopback;
881 global_routing->size_of_link = size_of_links;
882 global_routing->last_route = NULL;
884 /* no current routing at moment */
885 current_routing = NULL;
887 /* parse generic elements */
888 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
889 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
891 surfxml_add_callback(STag_surfxml_route_cb_list,
892 &parse_S_route_new_and_endpoints_XML);
893 surfxml_add_callback(STag_surfxml_ASroute_cb_list,
894 &parse_S_ASroute_new_and_endpoints);
895 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
896 &parse_S_bypassRoute_new_and_endpoints);
898 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
899 &parse_E_link_ctn_new_elem_XML);
901 surfxml_add_callback(ETag_surfxml_route_cb_list,
902 &parse_E_route_store_route);
903 surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
904 &parse_E_ASroute_store_route);
905 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
906 &parse_E_bypassRoute_store_route);
908 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
909 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
911 surfxml_add_callback(STag_surfxml_cluster_cb_list,
912 &routing_parse_Scluster);
914 surfxml_add_callback(STag_surfxml_config_cb_list,
915 &routing_parse_Sconfig);
916 surfxml_add_callback(ETag_surfxml_config_cb_list,
917 &routing_parse_Econfig);
920 /* ************************************************************************** */
921 /* *************************** FULL ROUTING ********************************* */
923 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
925 /* Routing model structure */
928 s_routing_component_t generic_routing;
929 route_extended_t *routing_table;
930 } s_routing_component_full_t, *routing_component_full_t;
932 /* Business methods */
933 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
935 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
937 routing_component_full_t routing = (routing_component_full_t) rc;
938 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
939 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
940 char *k1, *d1, *k2, *d2;
941 xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
942 xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
943 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
944 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
947 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
949 route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
951 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
953 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
955 onelink_t onelink = xbt_new0(s_onelink_t, 1);
956 onelink->link_ptr = link;
957 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
958 onelink->src = xbt_strdup(k1);
959 onelink->dst = xbt_strdup(k2);
960 } else if (routing->generic_routing.hierarchy ==
961 SURF_ROUTING_RECURSIVE) {
962 onelink->src = xbt_strdup(route->src_gateway);
963 onelink->dst = xbt_strdup(route->dst_gateway);
965 xbt_dynar_push(ret, &onelink);
973 static route_extended_t full_get_route(routing_component_t rc,
974 const char *src, const char *dst)
976 xbt_assert1(rc && src
978 "Invalid params for \"get_route\" function at AS \"%s\"",
982 routing_component_full_t routing = (routing_component_full_t) rc;
983 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
985 generic_src_dst_check(rc, src, dst);
986 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
987 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
990 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
993 route_extended_t e_route = NULL;
994 route_extended_t new_e_route = NULL;
996 unsigned int cpt = 0;
998 e_route = TO_ROUTE_FULL(*src_id, *dst_id);
1001 new_e_route = xbt_new0(s_route_extended_t, 1);
1002 new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
1003 new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
1004 new_e_route->generic_route.link_list =
1005 xbt_dynar_new(global_routing->size_of_link, NULL);
1006 xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
1007 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1013 static void full_finalize(routing_component_t rc)
1015 routing_component_full_t routing = (routing_component_full_t) rc;
1016 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1019 /* Delete routing table */
1020 for (i = 0; i < table_size; i++)
1021 for (j = 0; j < table_size; j++)
1022 generic_free_extended_route(TO_ROUTE_FULL(i, j));
1023 xbt_free(routing->routing_table);
1024 /* Delete bypass dict */
1025 xbt_dict_free(&rc->bypassRoutes);
1026 /* Delete index dict */
1027 xbt_dict_free(&rc->to_index);
1028 /* Delete structure */
1033 /* Creation routing model functions */
1035 static void *model_full_create(void)
1037 routing_component_full_t new_component =
1038 xbt_new0(s_routing_component_full_t, 1);
1039 new_component->generic_routing.set_processing_unit =
1040 generic_set_processing_unit;
1041 new_component->generic_routing.set_autonomous_system =
1042 generic_set_autonomous_system;
1043 new_component->generic_routing.set_route = model_full_set_route;
1044 new_component->generic_routing.set_ASroute = model_full_set_route;
1045 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1046 new_component->generic_routing.get_route = full_get_route;
1047 new_component->generic_routing.get_onelink_routes =
1048 full_get_onelink_routes;
1049 new_component->generic_routing.get_bypass_route =
1050 generic_get_bypassroute;
1051 new_component->generic_routing.finalize = full_finalize;
1052 new_component->generic_routing.to_index = xbt_dict_new();
1053 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1054 return new_component;
1057 static void model_full_load(void)
1059 /* use "surfxml_add_callback" to add a parse function call */
1062 static void model_full_unload(void)
1064 /* use "surfxml_del_callback" to remove a parse function call */
1067 static void model_full_end(void)
1070 route_extended_t e_route;
1072 /* set utils vars */
1073 routing_component_full_t routing =
1074 ((routing_component_full_t) current_routing);
1075 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1077 /* Create table if necessary */
1078 if(!routing->routing_table)
1079 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
1081 /* Add the loopback if needed */
1082 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1083 for (i = 0; i < table_size; i++) {
1084 e_route = TO_ROUTE_FULL(i, i);
1086 e_route = xbt_new0(s_route_extended_t, 1);
1087 e_route->src_gateway = NULL;
1088 e_route->dst_gateway = NULL;
1089 e_route->generic_route.link_list =
1090 xbt_dynar_new(global_routing->size_of_link, NULL);
1091 xbt_dynar_push(e_route->generic_route.link_list,
1092 &global_routing->loopback);
1093 TO_ROUTE_FULL(i, i) = e_route;
1099 static void model_full_set_route(routing_component_t rc, const char *src,
1100 const char *dst, name_route_extended_t route)
1102 int *src_id, *dst_id;
1103 src_id = xbt_dict_get_or_null(rc->to_index, src);
1104 dst_id = xbt_dict_get_or_null(rc->to_index, dst);
1105 routing_component_full_t routing = ((routing_component_full_t) rc);
1106 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1109 && dst_id, "Network elements %s or %s not found", src, dst);
1111 xbt_assert2(xbt_dynar_length(route->generic_route.link_list) > 0,
1112 "Invalid count of links, must be greater than zero (%s,%s)",
1115 if(!routing->routing_table)
1116 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
1118 if(TO_ROUTE_FULL(*src_id, *dst_id))
1122 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1123 xbt_dynar_foreach(route->generic_route.link_list,i,link_name)
1125 void *link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1126 xbt_assert1(link,"Link : '%s' doesn't exists.",link_name);
1127 xbt_dynar_push(link_route_to_test,&link);
1129 xbt_assert2(!xbt_dynar_compare(
1130 (void*)TO_ROUTE_FULL(*src_id, *dst_id)->generic_route.link_list,
1131 (void*)link_route_to_test,
1132 (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1133 "The route between \"%s\" and \"%s\" already exists", src,dst);
1134 xbt_free(link_route_to_test);
1138 if(!route->dst_gateway && !route->src_gateway)
1139 DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
1141 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1142 route->src_gateway, dst, route->dst_gateway);
1143 TO_ROUTE_FULL(*src_id, *dst_id) = generic_new_extended_route(rc->hierarchy,route,1);
1144 xbt_dynar_shrink(TO_ROUTE_FULL(*src_id, *dst_id)->generic_route.link_list, 0);
1147 if( A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES
1148 || A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES )
1150 if(route->dst_gateway && route->src_gateway)
1152 char * gw_src = bprintf("%s",route->src_gateway);
1153 char * gw_dst = bprintf("%s",route->dst_gateway);
1154 route->src_gateway = bprintf("%s",gw_dst);
1155 route->dst_gateway = bprintf("%s",gw_src);
1157 if(TO_ROUTE_FULL(*dst_id, *src_id))
1161 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1162 for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
1164 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
1165 void *link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1166 xbt_assert1(link,"Link : '%s' doesn't exists.",link_name);
1167 xbt_dynar_push(link_route_to_test,&link);
1169 xbt_assert2(!xbt_dynar_compare(
1170 (void*)TO_ROUTE_FULL(*dst_id, *src_id)->generic_route.link_list,
1171 (void*)link_route_to_test,
1172 (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1173 "The route between \"%s\" and \"%s\" already exists", src,dst);
1174 xbt_free(link_route_to_test);
1178 if(!route->dst_gateway && !route->src_gateway)
1179 DEBUG2("Load Route from \"%s\" to \"%s\"", dst, src);
1181 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1182 route->src_gateway, src, route->dst_gateway);
1183 TO_ROUTE_FULL(*dst_id, *src_id) = generic_new_extended_route(rc->hierarchy,route,0);
1184 xbt_dynar_shrink(TO_ROUTE_FULL(*dst_id, *src_id)->generic_route.link_list, 0);
1190 /* ************************************************************************** */
1191 /* *************************** FLOYD ROUTING ******************************** */
1193 #define TO_FLOYD_COST(i,j) (routing->cost_table)[(i)+(j)*table_size]
1194 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1195 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1197 /* Routing model structure */
1200 s_routing_component_t generic_routing;
1201 /* vars for calculate the floyd algorith. */
1202 int *predecessor_table;
1204 route_extended_t *link_table; /* char* -> int* */
1205 } s_routing_component_floyd_t, *routing_component_floyd_t;
1207 static route_extended_t floyd_get_route(routing_component_t rc,
1208 const char *src, const char *dst);
1210 /* Business methods */
1211 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1213 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1215 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1216 //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1217 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1218 char *k1, *d1, *k2, *d2;
1219 xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1220 xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1221 route_extended_t route = floyd_get_route(rc, k1, k2);
1223 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1225 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1227 onelink_t onelink = xbt_new0(s_onelink_t, 1);
1228 onelink->link_ptr = link;
1229 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1230 onelink->src = xbt_strdup(k1);
1231 onelink->dst = xbt_strdup(k2);
1232 } else if (routing->generic_routing.hierarchy ==
1233 SURF_ROUTING_RECURSIVE) {
1234 onelink->src = xbt_strdup(route->src_gateway);
1235 onelink->dst = xbt_strdup(route->dst_gateway);
1237 xbt_dynar_push(ret, &onelink);
1245 static route_extended_t floyd_get_route(routing_component_t rc,
1246 const char *src, const char *dst)
1248 xbt_assert1(rc && src
1250 "Invalid params for \"get_route\" function at AS \"%s\"",
1253 /* set utils vars */
1254 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1255 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1257 generic_src_dst_check(rc, src, dst);
1258 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1259 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1262 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1265 /* create a result route */
1266 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1267 new_e_route->generic_route.link_list =
1268 xbt_dynar_new(global_routing->size_of_link, NULL);
1269 new_e_route->src_gateway = NULL;
1270 new_e_route->dst_gateway = NULL;
1275 char *gw_src = NULL, *gw_dst =
1276 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1283 pred = TO_FLOYD_PRED(*src_id, pred);
1284 if (pred == -1) /* if no pred in route -> no route to host */
1286 xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1287 "Invalid link for the route between \"%s\" or \"%s\"", src,
1290 prev_gw_src = gw_src;
1291 prev_gw_dst = gw_dst;
1293 route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1294 gw_src = e_route->src_gateway;
1295 gw_dst = e_route->dst_gateway;
1300 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1301 && strcmp(gw_dst, prev_gw_src)) {
1302 xbt_dynar_t e_route_as_to_as =
1303 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1304 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1305 gw_dst, prev_gw_src);
1306 links = e_route_as_to_as;
1308 xbt_dynar_foreach(links, cpt, link) {
1309 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1315 links = e_route->generic_route.link_list;
1316 xbt_dynar_foreach(links, cpt, link) {
1317 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1321 } while (pred != *src_id);
1322 xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1323 *src_id, *dst_id, src, dst);
1325 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1326 new_e_route->src_gateway = xbt_strdup(gw_src);
1327 new_e_route->dst_gateway = xbt_strdup(first_gw);
1333 static void floyd_finalize(routing_component_t rc)
1335 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1339 table_size = xbt_dict_length(routing->generic_routing.to_index);
1340 /* Delete link_table */
1341 for (i = 0; i < table_size; i++)
1342 for (j = 0; j < table_size; j++)
1343 generic_free_extended_route(TO_FLOYD_LINK(i, j));
1344 xbt_free(routing->link_table);
1345 /* Delete bypass dict */
1346 xbt_dict_free(&routing->generic_routing.bypassRoutes);
1347 /* Delete index dict */
1348 xbt_dict_free(&(routing->generic_routing.to_index));
1349 /* Delete dictionary index dict, predecessor and links table */
1350 xbt_free(routing->predecessor_table);
1351 /* Delete structure */
1356 static void *model_floyd_create(void)
1358 routing_component_floyd_t new_component =
1359 xbt_new0(s_routing_component_floyd_t, 1);
1360 new_component->generic_routing.set_processing_unit =
1361 generic_set_processing_unit;
1362 new_component->generic_routing.set_autonomous_system =
1363 generic_set_autonomous_system;
1364 new_component->generic_routing.set_route = model_floyd_set_route;
1365 new_component->generic_routing.set_ASroute = model_floyd_set_route;
1366 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1367 new_component->generic_routing.get_route = floyd_get_route;
1368 new_component->generic_routing.get_onelink_routes =
1369 floyd_get_onelink_routes;
1370 new_component->generic_routing.get_bypass_route =
1371 generic_get_bypassroute;
1372 new_component->generic_routing.finalize = floyd_finalize;
1373 new_component->generic_routing.to_index = xbt_dict_new();
1374 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1375 return new_component;
1378 static void model_floyd_load(void)
1380 /* use "surfxml_add_callback" to add a parse function call */
1383 static void model_floyd_unload(void)
1385 /* use "surfxml_del_callback" to remove a parse function call */
1388 static void model_floyd_end(void)
1391 routing_component_floyd_t routing =
1392 ((routing_component_floyd_t) current_routing);
1394 unsigned int i, j, a, b, c;
1396 /* set the size of table routing */
1397 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1399 if(!routing->link_table)
1401 /* Create Cost, Predecessor and Link tables */
1402 routing->cost_table = xbt_new0(double, table_size * table_size); /* link cost from host to host */
1403 routing->predecessor_table = xbt_new0(int, table_size * table_size); /* predecessor host numbers */
1404 routing->link_table = xbt_new0(route_extended_t, table_size * table_size); /* actual link between src and dst */
1406 /* Initialize costs and predecessors */
1407 for (i = 0; i < table_size; i++)
1408 for (j = 0; j < table_size; j++) {
1409 TO_FLOYD_COST(i, j) = DBL_MAX;
1410 TO_FLOYD_PRED(i, j) = -1;
1411 TO_FLOYD_LINK(i, j) = NULL; /* fixed, missing in the previous version */
1415 /* Add the loopback if needed */
1416 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1417 for (i = 0; i < table_size; i++) {
1418 route_extended_t e_route = TO_FLOYD_LINK(i, i);
1420 e_route = xbt_new0(s_route_extended_t, 1);
1421 e_route->src_gateway = NULL;
1422 e_route->dst_gateway = NULL;
1423 e_route->generic_route.link_list =
1424 xbt_dynar_new(global_routing->size_of_link, NULL);
1425 xbt_dynar_push(e_route->generic_route.link_list,
1426 &global_routing->loopback);
1427 TO_FLOYD_LINK(i, i) = e_route;
1428 TO_FLOYD_PRED(i, i) = i;
1429 TO_FLOYD_COST(i, i) = 1;
1433 /* Calculate path costs */
1434 for (c = 0; c < table_size; c++) {
1435 for (a = 0; a < table_size; a++) {
1436 for (b = 0; b < table_size; b++) {
1437 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1438 if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1439 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1440 TO_FLOYD_COST(a, b))) {
1441 TO_FLOYD_COST(a, b) =
1442 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1443 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1451 static void model_floyd_set_route(routing_component_t rc, const char *src,
1452 const char *dst, name_route_extended_t route)
1454 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1456 /* set the size of table routing */
1457 size_t table_size = xbt_dict_length(rc->to_index);
1458 int *src_id, *dst_id;
1461 src_id = xbt_dict_get_or_null(rc->to_index, src);
1462 dst_id = xbt_dict_get_or_null(rc->to_index, dst);
1464 if(!routing->link_table)
1466 /* Create Cost, Predecessor and Link tables */
1467 routing->cost_table = xbt_new0(double, table_size * table_size); /* link cost from host to host */
1468 routing->predecessor_table = xbt_new0(int, table_size * table_size); /* predecessor host numbers */
1469 routing->link_table = xbt_new0(route_extended_t, table_size * table_size); /* actual link between src and dst */
1471 /* Initialize costs and predecessors */
1472 for (i = 0; i < table_size; i++)
1473 for (j = 0; j < table_size; j++) {
1474 TO_FLOYD_COST(i, j) = DBL_MAX;
1475 TO_FLOYD_PRED(i, j) = -1;
1476 TO_FLOYD_LINK(i, j) = NULL; /* fixed, missing in the previous version */
1480 if(TO_FLOYD_LINK(*src_id, *dst_id))
1482 if(!route->dst_gateway && !route->src_gateway)
1483 DEBUG2("See Route from \"%s\" to \"%s\"", src, dst);
1485 DEBUG4("See ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1486 route->src_gateway, dst, route->dst_gateway);
1489 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1490 xbt_dynar_foreach(route->generic_route.link_list,cpt,link_name)
1492 void *link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1493 xbt_assert1(link,"Link : '%s' doesn't exists.",link_name);
1494 xbt_dynar_push(link_route_to_test,&link);
1496 xbt_assert2(!xbt_dynar_compare(
1497 (void*)TO_FLOYD_LINK(*src_id, *dst_id)->generic_route.link_list,
1498 (void*)link_route_to_test,
1499 (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1500 "The route between \"%s\" and \"%s\" already exists", src,dst);
1501 xbt_free(link_route_to_test);
1505 if(!route->dst_gateway && !route->src_gateway)
1506 DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
1508 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
1509 route->src_gateway, dst, route->dst_gateway);
1511 TO_FLOYD_LINK(*src_id, *dst_id) =
1512 generic_new_extended_route(rc->hierarchy, route, 1);
1513 TO_FLOYD_PRED(*src_id, *dst_id) = *src_id;
1514 TO_FLOYD_COST(*src_id, *dst_id) =
1515 ((TO_FLOYD_LINK(*src_id, *dst_id))->generic_route.link_list)->used; /* count of links, old model assume 1 */
1518 if( A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES
1519 || A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES )
1521 if(TO_FLOYD_LINK(*dst_id, *src_id))
1523 if(!route->dst_gateway && !route->src_gateway)
1524 DEBUG2("See Route from \"%s\" to \"%s\"", dst, src);
1526 DEBUG4("See ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1527 route->src_gateway, src, route->dst_gateway);
1530 xbt_dynar_t link_route_to_test = xbt_dynar_new(global_routing->size_of_link, NULL);
1531 for(i=xbt_dynar_length(route->generic_route.link_list) ;i>0 ;i--)
1533 link_name = xbt_dynar_get_as(route->generic_route.link_list,i-1,void *);
1534 void *link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1535 xbt_assert1(link,"Link : '%s' doesn't exists.",link_name);
1536 xbt_dynar_push(link_route_to_test,&link);
1538 xbt_assert2(!xbt_dynar_compare(
1539 (void*)TO_FLOYD_LINK(*dst_id, *src_id)->generic_route.link_list,
1540 (void*)link_route_to_test,
1541 (int_f_cpvoid_cpvoid_t) surf_pointer_resource_cmp),
1542 "The route between \"%s\" and \"%s\" already exists", src,dst);
1543 xbt_free(link_route_to_test);
1547 if(route->dst_gateway && route->src_gateway)
1549 char * gw_src = bprintf("%s",route->src_gateway);
1550 char * gw_dst = bprintf("%s",route->dst_gateway);
1551 route->src_gateway = bprintf("%s",gw_dst);
1552 route->dst_gateway = bprintf("%s",gw_src);
1555 if(!route->dst_gateway && !route->src_gateway)
1556 DEBUG2("Load Route from \"%s\" to \"%s\"", dst, src);
1558 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", dst,
1559 route->src_gateway, src, route->dst_gateway);
1561 TO_FLOYD_LINK(*dst_id, *src_id) =
1562 generic_new_extended_route(rc->hierarchy, route, 0);
1563 TO_FLOYD_PRED(*dst_id, *src_id) = *dst_id;
1564 TO_FLOYD_COST(*dst_id, *src_id) =
1565 ((TO_FLOYD_LINK(*dst_id, *src_id))->generic_route.link_list)->used; /* count of links, old model assume 1 */
1570 /* ************************************************************************** */
1571 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1574 s_routing_component_t generic_routing;
1575 xbt_graph_t route_graph; /* xbt_graph */
1576 xbt_dict_t graph_node_map; /* map */
1577 xbt_dict_t route_cache; /* use in cache mode */
1579 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1582 typedef struct graph_node_data {
1584 int graph_id; /* used for caching internal graph id's */
1585 } s_graph_node_data_t, *graph_node_data_t;
1587 typedef struct graph_node_map_element {
1589 } s_graph_node_map_element_t, *graph_node_map_element_t;
1591 typedef struct route_cache_element {
1594 } s_route_cache_element_t, *route_cache_element_t;
1596 /* Free functions */
1598 static void route_cache_elem_free(void *e)
1600 route_cache_element_t elm = (route_cache_element_t) e;
1602 xbt_free(elm->pred_arr);
1607 static void graph_node_map_elem_free(void *e)
1609 graph_node_map_element_t elm = (graph_node_map_element_t) e;
1615 static void graph_edge_data_free(void *e)
1617 route_extended_t e_route = (route_extended_t) e;
1619 xbt_dynar_free(&(e_route->generic_route.link_list));
1620 if (e_route->src_gateway)
1621 xbt_free(e_route->src_gateway);
1622 if (e_route->dst_gateway)
1623 xbt_free(e_route->dst_gateway);
1628 /* Utility functions */
1630 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1631 int id, int graph_id)
1633 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1634 xbt_node_t node = NULL;
1635 graph_node_data_t data = NULL;
1636 graph_node_map_element_t elm = NULL;
1638 data = xbt_new0(struct graph_node_data, 1);
1640 data->graph_id = graph_id;
1641 node = xbt_graph_new_node(routing->route_graph, data);
1643 elm = xbt_new0(struct graph_node_map_element, 1);
1645 xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1646 (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1651 static graph_node_map_element_t
1652 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1654 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1655 graph_node_map_element_t elm = (graph_node_map_element_t)
1656 xbt_dict_get_or_null_ext(routing->graph_node_map,
1664 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1665 int dst_id, route_extended_t e_route)
1667 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1669 xbt_node_t src = NULL;
1670 xbt_node_t dst = NULL;
1671 graph_node_map_element_t src_elm = (graph_node_map_element_t)
1672 xbt_dict_get_or_null_ext(routing->graph_node_map,
1675 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1676 xbt_dict_get_or_null_ext(routing->graph_node_map,
1681 src = src_elm->node;
1684 dst = dst_elm->node;
1686 /* add nodes if they don't exist in the graph */
1687 if (src_id == dst_id && src == NULL && dst == NULL) {
1688 src = route_graph_new_node(rc, src_id, -1);
1692 src = route_graph_new_node(rc, src_id, -1);
1695 dst = route_graph_new_node(rc, dst_id, -1);
1699 /* add link as edge to graph */
1700 xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1703 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1705 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1707 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1709 xbt_node_t node = NULL;
1710 unsigned int cursor2;
1711 xbt_dynar_foreach(nodes, cursor2, node) {
1712 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1713 xbt_edge_t edge = NULL;
1714 unsigned int cursor;
1717 xbt_dynar_foreach(out_edges, cursor, edge) {
1718 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1719 if (other_node == node) {
1726 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1727 e_route->src_gateway = NULL;
1728 e_route->dst_gateway = NULL;
1729 e_route->generic_route.link_list =
1730 xbt_dynar_new(global_routing->size_of_link, NULL);
1731 xbt_dynar_push(e_route->generic_route.link_list,
1732 &global_routing->loopback);
1733 xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1738 /* Business methods */
1739 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
1741 xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
1744 static route_extended_t dijkstra_get_route(routing_component_t rc,
1748 xbt_assert1(rc && src
1750 "Invalid params for \"get_route\" function at AS \"%s\"",
1753 /* set utils vars */
1754 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1756 generic_src_dst_check(rc, src, dst);
1757 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1758 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1761 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1764 /* create a result route */
1765 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1766 new_e_route->generic_route.link_list =
1767 xbt_dynar_new(global_routing->size_of_link, NULL);
1768 new_e_route->src_gateway = NULL;
1769 new_e_route->dst_gateway = NULL;
1771 int *pred_arr = NULL;
1772 int src_node_id = 0;
1773 int dst_node_id = 0;
1776 route_extended_t e_route;
1780 xbt_dynar_t links = NULL;
1781 route_cache_element_t elm = NULL;
1782 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1784 /* Use the graph_node id mapping set to quickly find the nodes */
1785 graph_node_map_element_t src_elm =
1786 graph_node_map_search(routing, *src_id);
1787 graph_node_map_element_t dst_elm =
1788 graph_node_map_search(routing, *dst_id);
1789 xbt_assert2(src_elm != NULL
1790 && dst_elm != NULL, "src %d or dst %d does not exist",
1792 src_node_id = ((graph_node_data_t)
1793 xbt_graph_node_get_data(src_elm->node))->graph_id;
1794 dst_node_id = ((graph_node_data_t)
1795 xbt_graph_node_get_data(dst_elm->node))->graph_id;
1797 /* if the src and dst are the same *//* fixed, missing in the previous version */
1798 if (src_node_id == dst_node_id) {
1800 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1801 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1803 xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1805 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1808 e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1810 links = e_route->generic_route.link_list;
1811 xbt_dynar_foreach(links, cpt, link) {
1812 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1818 if (routing->cached) {
1819 /*check if there is a cached predecessor list avail */
1820 elm = (route_cache_element_t)
1821 xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
1825 if (elm) { /* cached mode and cache hit */
1826 pred_arr = elm->pred_arr;
1827 } else { /* not cached mode or cache miss */
1828 double *cost_arr = NULL;
1829 xbt_heap_t pqueue = NULL;
1832 int nr_nodes = xbt_dynar_length(nodes);
1833 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
1834 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
1835 pqueue = xbt_heap_new(nr_nodes, xbt_free);
1838 cost_arr[src_node_id] = 0.0;
1840 for (i = 0; i < nr_nodes; i++) {
1841 if (i != src_node_id) {
1842 cost_arr[i] = DBL_MAX;
1847 /* initialize priority queue */
1848 nodeid = xbt_new0(int, 1);
1850 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1854 /* apply dijkstra using the indexes from the graph's node array */
1855 while (xbt_heap_size(pqueue) > 0) {
1856 int *v_id = xbt_heap_pop(pqueue);
1857 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1858 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1859 xbt_edge_t edge = NULL;
1860 unsigned int cursor;
1862 xbt_dynar_foreach(out_edges, cursor, edge) {
1863 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1864 graph_node_data_t data = xbt_graph_node_get_data(u_node);
1865 int u_id = data->graph_id;
1866 route_extended_t tmp_e_route =
1867 (route_extended_t) xbt_graph_edge_get_data(edge);
1868 int cost_v_u = (tmp_e_route->generic_route.link_list)->used; /* count of links, old model assume 1 */
1870 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1871 pred_arr[u_id] = *v_id;
1872 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1873 nodeid = xbt_new0(int, 1);
1875 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1879 /* free item popped from pqueue */
1884 xbt_heap_free(pqueue);
1887 /* compose route path with links */
1888 char *gw_src = NULL, *gw_dst =
1889 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1891 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1892 xbt_node_t node_pred_v =
1893 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1894 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1896 xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1898 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1901 prev_gw_src = gw_src;
1902 prev_gw_dst = gw_dst;
1904 e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1905 gw_src = e_route->src_gateway;
1906 gw_dst = e_route->dst_gateway;
1908 if (v == dst_node_id)
1911 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
1912 && strcmp(gw_dst, prev_gw_src)) {
1913 xbt_dynar_t e_route_as_to_as =
1914 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1915 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1916 gw_dst, prev_gw_src);
1917 links = e_route_as_to_as;
1919 xbt_dynar_foreach(links, cpt, link) {
1920 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1926 links = e_route->generic_route.link_list;
1927 xbt_dynar_foreach(links, cpt, link) {
1928 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1933 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1934 new_e_route->src_gateway = xbt_strdup(gw_src);
1935 new_e_route->dst_gateway = xbt_strdup(first_gw);
1938 if (routing->cached && elm == NULL) {
1939 /* add to predecessor list of the current src-host to cache */
1940 elm = xbt_new0(struct route_cache_element, 1);
1941 elm->pred_arr = pred_arr;
1943 xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
1944 (xbt_set_elm_t) elm, &route_cache_elem_free);
1947 if (!routing->cached)
1953 static void dijkstra_finalize(routing_component_t rc)
1955 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1958 xbt_graph_free_graph(routing->route_graph, &xbt_free,
1959 &graph_edge_data_free, &xbt_free);
1960 xbt_dict_free(&routing->graph_node_map);
1961 if (routing->cached)
1962 xbt_dict_free(&routing->route_cache);
1963 /* Delete bypass dict */
1964 xbt_dict_free(&routing->generic_routing.bypassRoutes);
1965 /* Delete index dict */
1966 xbt_dict_free(&(routing->generic_routing.to_index));
1967 /* Delete structure */
1972 /* Creation routing model functions */
1974 static void *model_dijkstra_both_create(int cached)
1976 routing_component_dijkstra_t new_component =
1977 xbt_new0(s_routing_component_dijkstra_t, 1);
1978 new_component->generic_routing.set_processing_unit =
1979 generic_set_processing_unit;
1980 new_component->generic_routing.set_autonomous_system =
1981 generic_set_autonomous_system;
1982 new_component->generic_routing.set_route = model_dijkstra_both_set_route;
1983 new_component->generic_routing.set_ASroute = model_dijkstra_both_set_route; //TODO
1984 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1985 new_component->generic_routing.get_route = dijkstra_get_route;
1986 new_component->generic_routing.get_onelink_routes =
1987 dijkstra_get_onelink_routes;
1988 new_component->generic_routing.get_bypass_route =
1989 generic_get_bypassroute;
1990 new_component->generic_routing.finalize = dijkstra_finalize;
1991 new_component->cached = cached;
1992 new_component->generic_routing.to_index = xbt_dict_new();
1993 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1994 return new_component;
1997 static void *model_dijkstra_create(void)
1999 return model_dijkstra_both_create(0);
2002 static void *model_dijkstracache_create(void)
2004 return model_dijkstra_both_create(1);
2007 static void model_dijkstra_both_load(void)
2009 /* use "surfxml_add_callback" to add a parse function call */
2012 static void model_dijkstra_both_unload(void)
2014 /* use "surfxml_del_callback" to remove a parse function call */
2017 static void model_dijkstra_both_end(void)
2019 routing_component_dijkstra_t routing =
2020 (routing_component_dijkstra_t) current_routing;
2022 xbt_node_t node = NULL;
2023 unsigned int cursor2;
2024 xbt_dynar_t nodes = NULL;
2026 /* Create the topology graph */
2027 routing->route_graph = xbt_graph_new_graph(1, NULL);
2028 routing->graph_node_map = xbt_dict_new();
2030 if (routing->cached && !routing->route_cache)
2031 routing->route_cache = xbt_dict_new();
2033 /* Add the loopback if needed */
2034 if (current_routing->hierarchy == SURF_ROUTING_BASE)
2035 add_loopback_dijkstra(routing);
2037 /* initialize graph indexes in nodes after graph has been built */
2038 nodes = xbt_graph_get_nodes(routing->route_graph);
2040 xbt_dynar_foreach(nodes, cursor2, node) {
2041 graph_node_data_t data = xbt_graph_node_get_data(node);
2042 data->graph_id = cursor2;
2046 static void model_dijkstra_both_set_route (routing_component_t rc, const char *src,
2047 const char *dst, name_route_extended_t route)
2049 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
2050 int *src_id, *dst_id;
2051 src_id = xbt_dict_get_or_null(rc->to_index, src);
2052 dst_id = xbt_dict_get_or_null(rc->to_index, dst);
2054 if (routing->cached && !routing->route_cache)
2055 routing->route_cache = xbt_dict_new();
2057 if( A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES
2058 || A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES )
2059 xbt_die("Route symmetrical not supported on model dijkstra");
2061 if(!route->dst_gateway && !route->src_gateway)
2062 DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
2064 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2065 route->src_gateway, dst, route->dst_gateway);
2067 route_extended_t e_route =
2068 generic_new_extended_route(current_routing->hierarchy, route, 1);
2069 route_new_dijkstra(routing, *src_id, *dst_id, e_route);
2072 #ifdef HAVE_PCRE_LIB
2073 /* ************************************************** */
2074 /* ************** RULE-BASED ROUTING **************** */
2076 /* Routing model structure */
2079 s_routing_component_t generic_routing;
2080 xbt_dict_t dict_processing_units;
2081 xbt_dict_t dict_autonomous_systems;
2082 xbt_dynar_t list_route;
2083 xbt_dynar_t list_ASroute;
2084 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
2086 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
2087 typedef struct s_rule_route_extended s_rule_route_extended_t,
2088 *rule_route_extended_t;
2090 struct s_rule_route {
2091 xbt_dynar_t re_str_link; // dynar of char*
2096 struct s_rule_route_extended {
2097 s_rule_route_t generic_rule_route;
2098 char *re_src_gateway;
2099 char *re_dst_gateway;
2102 static void rule_route_free(void *e)
2104 rule_route_t *elem = (rule_route_t *) (e);
2106 xbt_dynar_free(&(*elem)->re_str_link);
2107 pcre_free((*elem)->re_src);
2108 pcre_free((*elem)->re_dst);
2114 static void rule_route_extended_free(void *e)
2116 rule_route_extended_t *elem = (rule_route_extended_t *) e;
2118 xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
2119 pcre_free((*elem)->generic_rule_route.re_src);
2120 pcre_free((*elem)->generic_rule_route.re_dst);
2121 xbt_free((*elem)->re_src_gateway);
2122 xbt_free((*elem)->re_dst_gateway);
2127 /* Parse routing model functions */
2129 static void model_rulebased_set_processing_unit(routing_component_t rc,
2132 routing_component_rulebased_t routing =
2133 (routing_component_rulebased_t) rc;
2134 xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
2137 static void model_rulebased_set_autonomous_system(routing_component_t rc,
2140 routing_component_rulebased_t routing =
2141 (routing_component_rulebased_t) rc;
2142 xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
2146 static void model_rulebased_set_route(routing_component_t rc,
2147 const char *src, const char *dst,
2148 name_route_extended_t route)
2150 routing_component_rulebased_t routing =
2151 (routing_component_rulebased_t) rc;
2152 rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
2155 ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
2156 xbt_assert3(ruleroute->re_src,
2157 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2158 erroffset, src, error);
2159 ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
2160 xbt_assert3(ruleroute->re_src,
2161 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2162 erroffset, dst, error);
2163 ruleroute->re_str_link = route->generic_route.link_list;
2164 xbt_dynar_push(routing->list_route, &ruleroute);
2168 static void model_rulebased_set_ASroute(routing_component_t rc,
2169 const char *src, const char *dst,
2170 name_route_extended_t route)
2172 routing_component_rulebased_t routing =
2173 (routing_component_rulebased_t) rc;
2174 rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
2177 ruleroute_e->generic_rule_route.re_src =
2178 pcre_compile(src, 0, &error, &erroffset, NULL);
2179 xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2180 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2181 erroffset, src, error);
2182 ruleroute_e->generic_rule_route.re_dst =
2183 pcre_compile(dst, 0, &error, &erroffset, NULL);
2184 xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2185 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2186 erroffset, dst, error);
2187 ruleroute_e->generic_rule_route.re_str_link =
2188 route->generic_route.link_list;
2189 ruleroute_e->re_src_gateway = route->src_gateway;
2190 ruleroute_e->re_dst_gateway = route->dst_gateway;
2191 xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2192 xbt_free(route->src_gateway);
2193 xbt_free(route->dst_gateway);
2197 static void model_rulebased_set_bypassroute(routing_component_t rc,
2200 route_extended_t e_route)
2202 xbt_die("bypass routing not supported for Route-Based model");
2205 #define BUFFER_SIZE 4096 /* result buffer size */
2206 #define OVECCOUNT 30 /* should be a multiple of 3 */
2208 static char *remplace(char *value, const char **src_list, int src_size,
2209 const char **dst_list, int dst_size)
2212 char result_result[BUFFER_SIZE];
2213 int i_result_buffer;
2214 int value_length = (int) strlen(value);
2218 i_result_buffer = 0;
2220 if (value[i] == '$') {
2224 int number_length = 0;
2225 while ('0' <= value[i + number_length]
2226 && value[i + number_length] <= '9') {
2229 xbt_assert2(number_length != 0,
2230 "bad string parameter, no number indication, at offset: %d (\"%s\")",
2234 number = atoi(value + i);
2235 i = i + number_length;
2236 xbt_assert2(i + 2 < value_length,
2237 "bad string parameter, too few chars, at offset: %d (\"%s\")",
2240 // solve the indication
2241 const char **param_list;
2243 if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2244 param_list = src_list;
2245 param_size = src_size;
2246 } else if (value[i] == 'd' && value[i + 1] == 's'
2247 && value[i + 2] == 't') {
2248 param_list = dst_list;
2249 param_size = dst_size;
2252 "bad string parameter, support only \"src\" and \"dst\", at offset: %d (\"%s\")",
2257 xbt_assert4(param_size >= number,
2258 "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2259 i, value, param_size, number);
2261 const char *param = param_list[number];
2262 int size = strlen(param);
2264 for (cp = 0; cp < size; cp++) {
2265 result_result[i_result_buffer] = param[cp];
2267 if (i_result_buffer >= BUFFER_SIZE)
2271 result_result[i_result_buffer] = value[i];
2276 } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2278 xbt_assert2(i_result_buffer < BUFFER_SIZE,
2279 "solving string \"%s\", small buffer size (%d)", value,
2281 result_result[i_result_buffer] = 0;
2282 return xbt_strdup(result_result);
2285 static route_extended_t rulebased_get_route(routing_component_t rc,
2288 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2290 xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2291 routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2293 xbt_dict_cursor_t c1 = NULL;
2297 char *router = NULL;
2298 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2299 if (strstr (k1, "router")){
2304 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2307 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2308 route_extended_t route = rulebased_get_route (rc, router, k1);
2310 int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2311 if (number_of_links != 3) {
2312 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2316 xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2317 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2318 onelink->src = xbt_strdup (k1);
2319 onelink->dst = xbt_strdup (router);
2320 onelink->link_ptr = link_ptr;
2321 xbt_dynar_push (ret, &onelink);
2326 /* Business methods */
2327 static route_extended_t rulebased_get_route(routing_component_t rc,
2331 xbt_assert1(rc && src
2333 "Invalid params for \"get_route\" function at AS \"%s\"",
2336 /* set utils vars */
2337 routing_component_rulebased_t routing =
2338 (routing_component_rulebased_t) rc;
2340 int are_processing_units;
2341 xbt_dynar_t rule_list;
2342 if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2343 && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2344 are_processing_units = 1;
2345 rule_list = routing->list_route;
2346 } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2347 && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2349 are_processing_units = 0;
2350 rule_list = routing->list_ASroute;
2353 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
2358 int src_length = (int) strlen(src);
2359 int dst_length = (int) strlen(dst);
2361 xbt_dynar_t links_list =
2362 xbt_dynar_new(global_routing->size_of_link, NULL);
2364 rule_route_t ruleroute;
2366 int ovector_src[OVECCOUNT];
2367 int ovector_dst[OVECCOUNT];
2368 const char **list_src = NULL;
2369 const char **list_dst = NULL;
2370 xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2372 pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2373 ovector_src, OVECCOUNT);
2376 pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2377 ovector_dst, OVECCOUNT);
2379 xbt_assert1(!pcre_get_substring_list
2380 (src, ovector_src, rc_src, &list_src),
2381 "error solving substring list for src \"%s\"", src);
2382 xbt_assert1(!pcre_get_substring_list
2383 (dst, ovector_dst, rc_dst, &list_dst),
2384 "error solving substring list for src \"%s\"", dst);
2386 xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2387 char *new_link_name =
2388 remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2390 xbt_dict_get_or_null(surf_network_model->resource_set,
2393 xbt_dynar_push(links_list, &link);
2395 THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2396 xbt_free(new_link_name);
2400 if (rc_src >= 0 && rc_dst >= 0)
2404 route_extended_t new_e_route = NULL;
2405 if (rc_src >= 0 && rc_dst >= 0) {
2406 new_e_route = xbt_new0(s_route_extended_t, 1);
2407 new_e_route->generic_route.link_list = links_list;
2408 } else if (!strcmp(src, dst) && are_processing_units) {
2409 new_e_route = xbt_new0(s_route_extended_t, 1);
2410 xbt_dynar_push(links_list, &(global_routing->loopback));
2411 new_e_route->generic_route.link_list = links_list;
2413 xbt_dynar_free(&link_list);
2416 if (!are_processing_units && new_e_route) {
2417 rule_route_extended_t ruleroute_extended =
2418 (rule_route_extended_t) ruleroute;
2419 new_e_route->src_gateway =
2420 remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2422 new_e_route->dst_gateway =
2423 remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2428 pcre_free_substring_list(list_src);
2430 pcre_free_substring_list(list_dst);
2435 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2442 static void rulebased_finalize(routing_component_t rc)
2444 routing_component_rulebased_t routing =
2445 (routing_component_rulebased_t) rc;
2447 xbt_dict_free(&routing->dict_processing_units);
2448 xbt_dict_free(&routing->dict_autonomous_systems);
2449 xbt_dynar_free(&routing->list_route);
2450 xbt_dynar_free(&routing->list_ASroute);
2451 /* Delete structure */
2456 /* Creation routing model functions */
2457 static void *model_rulebased_create(void)
2459 routing_component_rulebased_t new_component =
2460 xbt_new0(s_routing_component_rulebased_t, 1);
2461 new_component->generic_routing.set_processing_unit =
2462 model_rulebased_set_processing_unit;
2463 new_component->generic_routing.set_autonomous_system =
2464 model_rulebased_set_autonomous_system;
2465 new_component->generic_routing.set_route = model_rulebased_set_route;
2466 new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2467 new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
2468 new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
2469 new_component->generic_routing.get_route = rulebased_get_route;
2470 new_component->generic_routing.get_bypass_route = generic_get_bypassroute; //rulebased_get_bypass_route;
2471 new_component->generic_routing.finalize = rulebased_finalize;
2472 /* initialization of internal structures */
2473 new_component->dict_processing_units = xbt_dict_new();
2474 new_component->dict_autonomous_systems = xbt_dict_new();
2475 new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2476 new_component->list_ASroute =
2477 xbt_dynar_new(sizeof(rule_route_extended_t),
2478 &rule_route_extended_free);
2479 return new_component;
2482 static void model_rulebased_load(void)
2484 /* use "surfxml_add_callback" to add a parse function call */
2487 static void model_rulebased_unload(void)
2489 /* use "surfxml_del_callback" to remove a parse function call */
2492 static void model_rulebased_end(void)
2496 #endif /* HAVE_PCRE_LIB */
2498 /* ************************************************************************** */
2499 /* ******************************* NO ROUTING ******************************* */
2501 /* Routing model structure */
2503 s_routing_component_t generic_routing;
2504 } s_routing_component_none_t, *routing_component_none_t;
2506 /* Business methods */
2507 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2512 static route_extended_t none_get_route(routing_component_t rc,
2513 const char *src, const char *dst)
2518 static route_extended_t none_get_bypass_route(routing_component_t rc,
2525 static void none_finalize(routing_component_t rc)
2530 static void none_set_processing_unit(routing_component_t rc,
2535 static void none_set_autonomous_system(routing_component_t rc,
2540 /* Creation routing model functions */
2541 static void *model_none_create(void)
2543 routing_component_none_t new_component =
2544 xbt_new0(s_routing_component_none_t, 1);
2545 new_component->generic_routing.set_processing_unit =
2546 none_set_processing_unit;
2547 new_component->generic_routing.set_autonomous_system =
2548 none_set_autonomous_system;
2549 new_component->generic_routing.set_route = NULL;
2550 new_component->generic_routing.set_ASroute = NULL;
2551 new_component->generic_routing.set_bypassroute = NULL;
2552 new_component->generic_routing.get_route = none_get_route;
2553 new_component->generic_routing.get_onelink_routes =
2554 none_get_onelink_routes;
2555 new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2556 new_component->generic_routing.finalize = none_finalize;
2557 return new_component;
2560 static void model_none_load(void)
2564 static void model_none_unload(void)
2568 static void model_none_end(void)
2572 /* ************************************************** */
2573 /* ********** PATERN FOR NEW ROUTING **************** */
2575 /* The minimal configuration of a new routing model need the next functions,
2576 * also you need to set at the start of the file, the new model in the model
2577 * list. Remember keep the null ending of the list.
2579 /*** Routing model structure ***/
2581 // s_routing_component_t generic_routing;
2582 // /* things that your routing model need */
2583 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2585 /*** Parse routing model functions ***/
2586 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2587 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2588 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2589 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2590 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2592 /*** Business methods ***/
2593 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2594 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2595 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2597 /*** Creation routing model functions ***/
2598 // static void* model_NEW_create(void) {
2599 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
2600 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2601 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2602 // new_component->generic_routing.set_route = model_NEW_set_route;
2603 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2604 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2605 // new_component->generic_routing.get_route = NEW_get_route;
2606 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2607 // new_component->generic_routing.finalize = NEW_finalize;
2608 // /* initialization of internal structures */
2609 // return new_component;
2610 // } /* mandatory */
2611 // static void model_NEW_load(void) {} /* mandatory */
2612 // static void model_NEW_unload(void) {} /* mandatory */
2613 // static void model_NEW_end(void) {} /* mandatory */
2615 /* ************************************************************************** */
2616 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2618 static void generic_set_processing_unit(routing_component_t rc,
2621 DEBUG1("Load process unit \"%s\"", name);
2622 int *id = xbt_new0(int, 1);
2623 xbt_dict_t _to_index;
2624 _to_index = current_routing->to_index;
2625 *id = xbt_dict_length(_to_index);
2626 xbt_dict_set(_to_index, name, id, xbt_free);
2629 static void generic_set_autonomous_system(routing_component_t rc,
2632 DEBUG1("Load Autonomous system \"%s\"", name);
2633 int *id = xbt_new0(int, 1);
2634 xbt_dict_t _to_index;
2635 _to_index = current_routing->to_index;
2636 *id = xbt_dict_length(_to_index);
2637 xbt_dict_set(_to_index, name, id, xbt_free);
2640 static int surf_pointer_resource_cmp(const void *a, const void *b) {
2646 static int surf_link_resource_cmp(const void *a, const void *b) {
2647 if( memcmp(a,b,global_routing->size_of_link) == 0 );
2652 static void generic_set_bypassroute(routing_component_t rc,
2653 const char *src, const char *dst,
2654 route_extended_t e_route)
2656 DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2657 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2660 route_name = bprintf("%s#%s", src, dst);
2661 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2662 "Invalid count of links, must be greater than zero (%s,%s)",
2664 xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2665 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
2666 src, e_route->src_gateway, dst, e_route->dst_gateway);
2668 route_extended_t new_e_route =
2669 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2670 xbt_dynar_free(&(e_route->generic_route.link_list));
2673 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2674 (void (*)(void *)) generic_free_extended_route);
2675 xbt_free(route_name);
2678 /* ************************************************************************** */
2679 /* *********************** GENERIC BUSINESS METHODS ************************* */
2681 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2683 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2686 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2690 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2691 routing_component_t src_as, dst_as;
2692 int index_src, index_dst;
2693 xbt_dynar_t path_src = NULL;
2694 xbt_dynar_t path_dst = NULL;
2695 routing_component_t current = NULL;
2696 routing_component_t *current_src = NULL;
2697 routing_component_t *current_dst = NULL;
2699 /* (1) find the as where the src and dst are located */
2700 src_as = ((network_element_info_t)
2701 xbt_dict_get_or_null(global_routing->where_network_elements,
2702 src))->rc_component;
2703 dst_as = ((network_element_info_t)
2704 xbt_dict_get_or_null(global_routing->where_network_elements,
2705 dst))->rc_component;
2708 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2711 /* (2) find the path to the root routing component */
2712 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2714 while (current != NULL) {
2715 xbt_dynar_push(path_src, ¤t);
2716 current = current->routing_father;
2718 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2720 while (current != NULL) {
2721 xbt_dynar_push(path_dst, ¤t);
2722 current = current->routing_father;
2725 /* (3) find the common father */
2726 index_src = (path_src->used) - 1;
2727 index_dst = (path_dst->used) - 1;
2728 current_src = xbt_dynar_get_ptr(path_src, index_src);
2729 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2730 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2731 routing_component_t *tmp_src, *tmp_dst;
2732 tmp_src = xbt_dynar_pop_ptr(path_src);
2733 tmp_dst = xbt_dynar_pop_ptr(path_dst);
2736 current_src = xbt_dynar_get_ptr(path_src, index_src);
2737 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2740 int max_index_src = (path_src->used) - 1;
2741 int max_index_dst = (path_dst->used) - 1;
2743 int max_index = max(max_index_src, max_index_dst);
2746 route_extended_t e_route_bypass = NULL;
2748 for (max = 0; max <= max_index; max++) {
2749 for (i = 0; i < max; i++) {
2750 if (i <= max_index_src && max <= max_index_dst) {
2751 char *route_name = bprintf("%s#%s",
2752 (*(routing_component_t *)
2754 (path_src, i)))->name,
2755 (*(routing_component_t *)
2757 (path_dst, max)))->name);
2759 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2760 xbt_free(route_name);
2764 if (max <= max_index_src && i <= max_index_dst) {
2765 char *route_name = bprintf("%s#%s",
2766 (*(routing_component_t *)
2768 (path_src, max)))->name,
2769 (*(routing_component_t *)
2771 (path_dst, i)))->name);
2773 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2774 xbt_free(route_name);
2783 if (max <= max_index_src && max <= max_index_dst) {
2784 char *route_name = bprintf("%s#%s",
2785 (*(routing_component_t *)
2787 (path_src, max)))->name,
2788 (*(routing_component_t *)
2790 (path_dst, max)))->name);
2791 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2792 xbt_free(route_name);
2798 xbt_dynar_free(&path_src);
2799 xbt_dynar_free(&path_dst);
2801 route_extended_t new_e_route = NULL;
2803 if (e_route_bypass) {
2805 unsigned int cpt = 0;
2806 new_e_route = xbt_new0(s_route_extended_t, 1);
2807 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2808 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2809 new_e_route->generic_route.link_list =
2810 xbt_dynar_new(global_routing->size_of_link, NULL);
2811 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2812 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2819 /* ************************************************************************** */
2820 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2823 generic_new_route(e_surf_routing_hierarchy_t hierarchy,
2824 void *data, int order)
2830 xbt_dynar_t links = NULL, links_id = NULL;
2832 new_route = xbt_new0(s_route_t, 1);
2833 new_route->link_list =
2834 xbt_dynar_new(global_routing->size_of_link, NULL);
2836 xbt_assert0(hierarchy == SURF_ROUTING_BASE,
2837 "the hierarchy type is not SURF_ROUTING_BASE");
2839 links = ((route_t) data)->link_list;
2842 links_id = new_route->link_list;
2844 xbt_dynar_foreach(links, cpt, link_name) {
2847 xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2850 xbt_dynar_push(links_id, &link);
2852 xbt_dynar_unshift(links_id, &link);
2854 THROW1(mismatch_error, 0, "Link %s not found", link_name);
2860 static route_extended_t
2861 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2862 void *data, int order)
2866 route_extended_t e_route, new_e_route;
2869 xbt_dynar_t links = NULL, links_id = NULL;
2871 new_e_route = xbt_new0(s_route_extended_t, 1);
2872 new_e_route->generic_route.link_list =
2873 xbt_dynar_new(global_routing->size_of_link, NULL);
2874 new_e_route->src_gateway = NULL;
2875 new_e_route->dst_gateway = NULL;
2877 xbt_assert0(hierarchy == SURF_ROUTING_BASE
2878 || hierarchy == SURF_ROUTING_RECURSIVE,
2879 "the hierarchy type is not defined");
2881 if (hierarchy == SURF_ROUTING_BASE) {
2883 route = (route_t) data;
2884 links = route->link_list;
2886 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2888 e_route = (route_extended_t) data;
2889 xbt_assert0(e_route->src_gateway
2890 && e_route->dst_gateway, "bad gateway, is null");
2891 links = e_route->generic_route.link_list;
2893 /* remeber not erase the gateway names */
2894 new_e_route->src_gateway = e_route->src_gateway;
2895 new_e_route->dst_gateway = e_route->dst_gateway;
2898 links_id = new_e_route->generic_route.link_list;
2900 xbt_dynar_foreach(links, cpt, link_name) {
2903 xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2906 xbt_dynar_push(links_id, &link);
2908 xbt_dynar_unshift(links_id, &link);
2910 THROW1(mismatch_error, 0, "Link %s not found", link_name);
2916 static void generic_free_route(route_t route)
2919 xbt_dynar_free(&(route->link_list));
2924 static void generic_free_extended_route(route_extended_t e_route)
2927 xbt_dynar_free(&(e_route->generic_route.link_list));
2928 if (e_route->src_gateway)
2929 xbt_free(e_route->src_gateway);
2930 if (e_route->dst_gateway)
2931 xbt_free(e_route->dst_gateway);
2936 static routing_component_t generic_as_exist(routing_component_t find_from,
2937 routing_component_t to_find)
2939 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2940 xbt_dict_cursor_t cursor = NULL;
2943 routing_component_t elem;
2944 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2945 if (to_find == elem || generic_as_exist(elem, to_find)) {
2955 static routing_component_t
2956 generic_autonomous_system_exist(routing_component_t rc, char *element)
2958 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2959 routing_component_t element_as, result, elem;
2960 xbt_dict_cursor_t cursor = NULL;
2962 element_as = ((network_element_info_t)
2963 xbt_dict_get_or_null
2964 (global_routing->where_network_elements,
2965 element))->rc_component;
2966 result = ((routing_component_t) - 1);
2967 if (element_as != rc)
2968 result = generic_as_exist(rc, element_as);
2972 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2973 found = !strcmp(elem->name, element);
2983 static routing_component_t
2984 generic_processing_units_exist(routing_component_t rc, char *element)
2986 routing_component_t element_as;
2987 element_as = ((network_element_info_t)
2988 xbt_dict_get_or_null
2989 (global_routing->where_network_elements,
2990 element))->rc_component;
2991 if (element_as == rc)
2993 return generic_as_exist(rc, element_as);
2996 static void generic_src_dst_check(routing_component_t rc, const char *src,
3000 routing_component_t src_as = ((network_element_info_t)
3001 xbt_dict_get_or_null
3002 (global_routing->where_network_elements,
3003 src))->rc_component;
3004 routing_component_t dst_as = ((network_element_info_t)
3005 xbt_dict_get_or_null
3006 (global_routing->where_network_elements,
3007 dst))->rc_component;
3009 xbt_assert3(src_as != NULL && dst_as != NULL,
3010 "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
3011 src, dst, rc->name);
3012 xbt_assert4(src_as == dst_as,
3013 "The src(%s in %s) and dst(%s in %s) are in differents AS",
3014 src, src_as->name, dst, dst_as->name);
3015 xbt_assert2(rc == dst_as,
3016 "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
3017 rc->name, dst_as->name);
3020 static void routing_parse_Sconfig(void)
3023 DEBUG0("WARNING tag config not yet implemented.");
3024 DEBUG1("Configuration name = %s",A_surfxml_config_id);
3027 static void routing_parse_Econfig(void)
3030 xbt_dict_cursor_t cursor = NULL;
3033 xbt_dict_foreach(current_property_set, cursor, key, elem) {
3034 DEBUG2("property : %s = %s",key,elem);
3038 static void routing_parse_Scluster(void)
3040 static int AX_ptr = 0;
3042 char *cluster_id = A_surfxml_cluster_id;
3043 char *cluster_prefix = A_surfxml_cluster_prefix;
3044 char *cluster_suffix = A_surfxml_cluster_suffix;
3045 char *cluster_radical = A_surfxml_cluster_radical;
3046 char *cluster_power = A_surfxml_cluster_power;
3047 char *cluster_bw = A_surfxml_cluster_bw;
3048 char *cluster_lat = A_surfxml_cluster_lat;
3049 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
3050 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
3051 char *host_id, *groups, *link_id = NULL;
3052 char *router_id, *link_router, *link_backbone, *route_src_dst;
3055 xbt_dynar_t radical_elements;
3056 xbt_dynar_t radical_ends;
3057 int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
3058 int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
3060 #ifndef HAVE_PCRE_LIB
3061 xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
3062 char *route_src, *route_dst;
3066 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
3067 static unsigned int surfxml_buffer_stack_stack[1024];
3069 surfxml_buffer_stack_stack[0] = 0;
3071 surfxml_bufferstack_push(1);
3073 SURFXML_BUFFER_SET(AS_id, cluster_id);
3074 #ifdef HAVE_PCRE_LIB
3075 SURFXML_BUFFER_SET(AS_routing, "RuleBased");
3076 DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
3078 SURFXML_BUFFER_SET(AS_routing, "Full");
3079 DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
3081 SURFXML_START_TAG(AS);
3083 radical_elements = xbt_str_split(cluster_radical, ",");
3084 xbt_dynar_foreach(radical_elements, iter, groups) {
3085 radical_ends = xbt_str_split(groups, "-");
3086 switch (xbt_dynar_length(radical_ends)) {
3088 surf_parse_get_int(&start,
3089 xbt_dynar_get_as(radical_ends, 0, char *));
3090 host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
3091 #ifndef HAVE_PCRE_LIB
3092 xbt_dynar_push_as(tab_elements_num, int, start);
3094 link_id = bprintf("%s_link_%d", cluster_id, start);
3096 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3097 A_surfxml_host_state = A_surfxml_host_state_ON;
3098 SURFXML_BUFFER_SET(host_id, host_id);
3099 SURFXML_BUFFER_SET(host_power, cluster_power);
3100 SURFXML_BUFFER_SET(host_availability, "1.0");
3101 SURFXML_BUFFER_SET(host_availability_file, "");
3102 SURFXML_BUFFER_SET(host_state_file, "");
3103 SURFXML_START_TAG(host);
3104 SURFXML_END_TAG(host);
3106 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3107 A_surfxml_link_state = A_surfxml_link_state_ON;
3108 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3109 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3110 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3111 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3112 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3113 SURFXML_BUFFER_SET(link_id, link_id);
3114 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3115 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3116 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3117 SURFXML_BUFFER_SET(link_latency_file, "");
3118 SURFXML_BUFFER_SET(link_state_file, "");
3119 SURFXML_START_TAG(link);
3120 SURFXML_END_TAG(link);
3126 surf_parse_get_int(&start,
3127 xbt_dynar_get_as(radical_ends, 0, char *));
3128 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3129 DEBUG2("Create hosts and links from %d to %d", start, end);
3130 for (i = start; i <= end; i++) {
3131 host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3132 #ifndef HAVE_PCRE_LIB
3133 xbt_dynar_push_as(tab_elements_num, int, i);
3135 link_id = bprintf("%s_link_%d", cluster_id, i);
3137 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3138 A_surfxml_host_state = A_surfxml_host_state_ON;
3139 SURFXML_BUFFER_SET(host_id, host_id);
3140 SURFXML_BUFFER_SET(host_power, cluster_power);
3141 SURFXML_BUFFER_SET(host_availability, "1.0");
3142 SURFXML_BUFFER_SET(host_availability_file, "");
3143 SURFXML_BUFFER_SET(host_state_file, "");
3144 SURFXML_START_TAG(host);
3145 SURFXML_END_TAG(host);
3147 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3148 A_surfxml_link_state = A_surfxml_link_state_ON;
3149 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3150 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3151 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3152 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3153 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3154 SURFXML_BUFFER_SET(link_id, link_id);
3155 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3156 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3157 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3158 SURFXML_BUFFER_SET(link_latency_file, "");
3159 SURFXML_BUFFER_SET(link_state_file, "");
3160 SURFXML_START_TAG(link);
3161 SURFXML_END_TAG(link);
3166 DEBUG0("Malformed radical");
3169 xbt_dynar_free(&radical_ends);
3174 bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3176 link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3177 link_backbone = bprintf("%s_backbone", cluster_id);
3179 DEBUG1("<router id=\"%s\"/>", router_id);
3180 SURFXML_BUFFER_SET(router_id, router_id);;
3181 SURFXML_START_TAG(router);
3182 SURFXML_END_TAG(router);
3184 DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,cluster_bw, cluster_lat);
3185 A_surfxml_link_state = A_surfxml_link_state_ON;
3186 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3187 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3188 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3189 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3190 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3191 SURFXML_BUFFER_SET(link_id, link_router);
3192 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3193 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3194 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3195 SURFXML_BUFFER_SET(link_latency_file, "");
3196 SURFXML_BUFFER_SET(link_state_file, "");
3197 SURFXML_START_TAG(link);
3198 SURFXML_END_TAG(link);
3200 DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bw, cluster_lat);
3201 A_surfxml_link_state = A_surfxml_link_state_ON;
3202 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3203 if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
3204 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3205 SURFXML_BUFFER_SET(link_id, link_backbone);
3206 SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3207 SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3208 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3209 SURFXML_BUFFER_SET(link_latency_file, "");
3210 SURFXML_BUFFER_SET(link_state_file, "");
3211 SURFXML_START_TAG(link);
3212 SURFXML_END_TAG(link);
3214 char *new_suffix = bprintf("%s", "");
3216 radical_elements = xbt_str_split(cluster_suffix, ".");
3217 xbt_dynar_foreach(radical_elements, iter, groups) {
3218 if (strcmp(groups, "")) {
3219 new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3222 route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3226 #ifdef HAVE_PCRE_LIB
3228 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
3229 DEBUG0("symetrical=\"NO\">");
3230 SURFXML_BUFFER_SET(route_src, route_src_dst);
3231 SURFXML_BUFFER_SET(route_dst, route_src_dst);
3232 A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3233 SURFXML_START_TAG(route);
3235 DEBUG1("<link_ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3236 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3237 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3238 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3239 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3240 SURFXML_START_TAG(link_ctn);
3241 SURFXML_END_TAG(link_ctn);
3243 DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3244 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3245 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3246 SURFXML_START_TAG(link_ctn);
3247 SURFXML_END_TAG(link_ctn);
3249 DEBUG1("<link_ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3250 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3251 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3252 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3253 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3254 SURFXML_START_TAG(link_ctn);
3255 SURFXML_END_TAG(link_ctn);
3258 SURFXML_END_TAG(route);
3260 for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3261 for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3262 if (i == xbt_dynar_length(tab_elements_num)) {
3263 route_src = router_id;
3266 bprintf("%s%d%s", cluster_prefix,
3267 xbt_dynar_get_as(tab_elements_num, i, int),
3271 if (j == xbt_dynar_length(tab_elements_num)) {
3272 route_dst = router_id;
3275 bprintf("%s%d%s", cluster_prefix,
3276 xbt_dynar_get_as(tab_elements_num, j, int),
3280 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
3281 DEBUG0("symetrical=\"NO\">");
3282 SURFXML_BUFFER_SET(route_src, route_src);
3283 SURFXML_BUFFER_SET(route_dst, route_dst);
3284 A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3285 SURFXML_START_TAG(route);
3287 if (i == xbt_dynar_length(tab_elements_num)) {
3288 route_src = link_router;
3291 bprintf("%s_link_%d", cluster_id,
3292 xbt_dynar_get_as(tab_elements_num, i, int));
3295 if (j == xbt_dynar_length(tab_elements_num)) {
3296 route_dst = link_router;
3299 bprintf("%s_link_%d", cluster_id,
3300 xbt_dynar_get_as(tab_elements_num, j, int));
3303 DEBUG1("<link_ctn\tid=\"%s\"/>", route_src);
3304 SURFXML_BUFFER_SET(link_ctn_id, route_src);
3305 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3306 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3307 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3308 SURFXML_START_TAG(link_ctn);
3309 SURFXML_END_TAG(link_ctn);
3311 DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3312 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3313 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3314 SURFXML_START_TAG(link_ctn);
3315 SURFXML_END_TAG(link_ctn);
3317 DEBUG1("<link_ctn\tid=\"%s\"/>", route_dst);
3318 SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3319 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3320 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3321 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3322 SURFXML_START_TAG(link_ctn);
3323 SURFXML_END_TAG(link_ctn);
3326 SURFXML_END_TAG(route);
3329 xbt_dynar_free(&tab_elements_num);
3333 SURFXML_END_TAG(AS);
3336 surfxml_bufferstack_pop(1);
3340 * New methods to init the routing model component from the lua script
3344 * calling parse_S_AS_lua with lua values
3346 void routing_AS_init(const char *AS_id, const char *AS_routing)
3348 parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3352 * calling parse_E_AS_lua to fisnish the creation of routing component
3354 void routing_AS_end(const char *AS_id)
3356 parse_E_AS_lua((char *) AS_id);
3360 * add a host to the network element list
3363 void routing_add_host(const char *host_id)
3365 parse_S_host_lua((char *) host_id);
3369 * Set a new link on the actual list of link for a route or ASroute
3371 void routing_add_link(const char *link_id)
3373 parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3377 *Set the endpoints for a route
3379 void routing_set_route(const char *src_id, const char *dst_id)
3381 parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3385 * Store the route by calling parse_E_route_store_route
3387 void routing_store_route(void)
3389 parse_E_route_store_route();