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 */
35 static void *model_floyd_create(void); /* create structures for floyd routing model */
36 static void model_floyd_load(void); /* load parse functions for floyd routing model */
37 static void model_floyd_unload(void); /* unload parse functions for floyd routing model */
38 static void model_floyd_end(void); /* finalize the creation of floyd routing model */
40 static void *model_dijkstra_both_create(int cached); /* create by calling dijkstra or dijkstracache */
41 static void *model_dijkstra_create(void); /* create structures for dijkstra routing model */
42 static void *model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
43 static void model_dijkstra_both_load(void); /* load parse functions for dijkstra routing model */
44 static void model_dijkstra_both_unload(void); /* unload parse functions for dijkstra routing model */
45 static void model_dijkstra_both_end(void); /* finalize the creation of dijkstra routing model */
47 static void *model_rulebased_create(void); /* create structures for rulebased routing model */
48 static void model_rulebased_load(void); /* load parse functions for rulebased routing model */
49 static void model_rulebased_unload(void); /* unload parse functions for rulebased routing model */
50 static void model_rulebased_end(void); /* finalize the creation of rulebased routing model */
52 static void *model_none_create(void); /* none routing model */
53 static void model_none_load(void); /* none routing model */
54 static void model_none_unload(void); /* none routing model */
55 static void model_none_end(void); /* none routing model */
57 static void routing_full_parse_Scluster(void); /*cluster bypass */
59 static void parse_Sconfig(void); /*config Tag */
61 /* this lines are only for replace use like index in the model table */
66 SURF_MODEL_DIJKSTRACACHE,
74 /* must be finish with null and carefull if change de order */
75 struct s_model_type routing_models[] = { {"Full",
76 "Full routing data (fast, large memory requirements, fully expressive)",
82 "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
83 model_floyd_create, model_floyd_load, model_floyd_unload,
86 "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
87 model_dijkstra_create, model_dijkstra_both_load,
88 model_dijkstra_both_unload, model_dijkstra_both_end},
90 "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
91 model_dijkstracache_create, model_dijkstra_both_load,
92 model_dijkstra_both_unload, model_dijkstra_both_end},
93 {"none", "No routing (usable with Constant network only)",
94 model_none_create, model_none_load, model_none_unload, model_none_end},
96 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
97 model_rulebased_load, model_rulebased_unload, model_rulebased_end},
99 {NULL, NULL, NULL, NULL, NULL, NULL}
102 /* ************************************************************************** */
103 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
105 static void generic_set_processing_unit(routing_component_t rc,
107 static void generic_set_autonomous_system(routing_component_t rc,
109 static void generic_set_route(routing_component_t rc, const char *src,
110 const char *dst, route_t route);
111 static void generic_set_ASroute(routing_component_t rc, const char *src,
112 const char *dst, route_extended_t e_route);
113 static void generic_set_bypassroute(routing_component_t rc,
114 const char *src, const char *dst,
115 route_extended_t e_route);
117 /* ************************************************************************** */
118 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
120 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
121 static route_extended_t generic_get_bypassroute(routing_component_t rc,
125 /* ************************************************************************** */
126 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
128 static route_extended_t
129 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
130 void *data, int order);
131 static void generic_free_route(route_t route);
132 static void generic_free_extended_route(route_extended_t e_route);
133 static routing_component_t
134 generic_autonomous_system_exist(routing_component_t rc, char *element);
135 static routing_component_t
136 generic_processing_units_exist(routing_component_t rc, char *element);
137 static void generic_src_dst_check(routing_component_t rc, const char *src,
140 /* ************************************************************************** */
141 /* **************************** GLOBAL FUNCTIONS **************************** */
143 /* global parse functions */
144 static char *src = NULL; /* temporary store the source name of a route */
145 static char *dst = NULL; /* temporary store the destination name of a route */
146 static char *gw_src = NULL; /* temporary store the gateway source name of a route */
147 static char *gw_dst = NULL; /* temporary store the gateway destination name of a route */
148 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
151 * \brief Add a "host" to the network element list
153 static void parse_S_host(char *host_id)
155 network_element_info_t info = NULL;
156 if (current_routing->hierarchy == SURF_ROUTING_NULL)
157 current_routing->hierarchy = SURF_ROUTING_BASE;
158 xbt_assert1(!xbt_dict_get_or_null
159 (global_routing->where_network_elements, host_id),
160 "Reading a host, processing unit \"%s\" already exist",
162 xbt_assert1(current_routing->set_processing_unit,
163 "no defined method \"set_processing_unit\" in \"%s\"",
164 current_routing->name);
165 (*(current_routing->set_processing_unit)) (current_routing, host_id);
166 info = xbt_new0(s_network_element_info_t, 1);
167 info->rc_component = current_routing;
168 info->rc_type = SURF_NETWORK_ELEMENT_HOST;
169 xbt_dict_set(global_routing->where_network_elements, host_id,
170 (void *) info, NULL);
174 * \brief Add a host to the network element list from XML
176 static void parse_S_host_XML(void)
178 parse_S_host(A_surfxml_host_id);
182 * \brief Add a host to the network element list from lua script
184 static void parse_S_host_lua(char *host_id)
186 parse_S_host(host_id);
194 * \brief Add a "router" to the network element list
196 static void parse_S_router(void)
198 network_element_info_t info = NULL;
200 if (current_routing->hierarchy == SURF_ROUTING_NULL)
201 current_routing->hierarchy = SURF_ROUTING_BASE;
202 xbt_assert1(!xbt_dict_get_or_null
203 (global_routing->where_network_elements,
204 A_surfxml_router_id),
205 "Reading a router, processing unit \"%s\" already exist",
206 A_surfxml_router_id);
207 xbt_assert1(current_routing->set_processing_unit,
208 "no defined method \"set_processing_unit\" in \"%s\"",
209 current_routing->name);
210 (*(current_routing->set_processing_unit)) (current_routing,
211 A_surfxml_router_id);
212 info = xbt_new0(s_network_element_info_t, 1);
213 info->rc_component = current_routing;
214 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
215 xbt_dict_set(global_routing->where_network_elements, A_surfxml_router_id,
216 (void *) info, NULL);
218 TRACE_surf_host_declaration(A_surfxml_router_id, 0);
223 * \brief Set the endponints for a route
225 static void parse_S_route_new_and_endpoints(char *src_id, char *dst_id)
227 if (src != NULL && dst != NULL && link_list != NULL)
228 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
232 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0,
233 "Some limits are null in the route between \"%s\" and \"%s\"",
235 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
239 * \breif Set the endpoints for a route from XML
241 static void parse_S_route_new_and_endpoints_XML(void)
243 parse_S_route_new_and_endpoints(A_surfxml_route_src,
244 A_surfxml_route_dst);
248 * \breif Set the endpoints for a route from lua
250 static void parse_S_route_new_and_endpoints_lua(char *id_src, char *id_dst)
252 parse_S_route_new_and_endpoints(id_src, id_dst);
256 * \brief Set the endponints and gateways for a ASroute
258 static void parse_S_ASroute_new_and_endpoints(void)
260 if (src != NULL && dst != NULL && link_list != NULL)
261 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
262 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
263 src = A_surfxml_ASroute_src;
264 dst = A_surfxml_ASroute_dst;
265 gw_src = A_surfxml_ASroute_gw_src;
266 gw_dst = A_surfxml_ASroute_gw_dst;
267 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
268 || strlen(gw_dst) > 0,
269 "Some limits are null in the route between \"%s\" and \"%s\"",
271 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
275 * \brief Set the endponints for a bypassRoute
277 static void parse_S_bypassRoute_new_and_endpoints(void)
279 if (src != NULL && dst != NULL && link_list != NULL)
281 "Bypass Route between %s to %s can not be defined",
282 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
283 src = A_surfxml_bypassRoute_src;
284 dst = A_surfxml_bypassRoute_dst;
285 gw_src = A_surfxml_bypassRoute_gw_src;
286 gw_dst = A_surfxml_bypassRoute_gw_dst;
287 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
288 || strlen(gw_dst) > 0,
289 "Some limits are null in the route between \"%s\" and \"%s\"",
291 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
295 * \brief Set a new link on the actual list of link for a route or ASroute
297 static void parse_E_link_ctn_new_elem(char *link_id)
300 val = xbt_strdup(link_id);
301 xbt_dynar_push(link_list, &val);
305 * \brief Set a new link on the actual list of link for a route or ASroute from XML
308 static void parse_E_link_ctn_new_elem_XML(void)
310 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
314 * \brief Set a new link on the actual list of link for a route or ASroute from lua
316 static void parse_E_link_c_ctn_new_elem_lua(char *link_id)
319 parse_E_link_ctn_new_elem(link_id);
323 * \brief Store the route by calling the set_route function of the current routing component
325 static void parse_E_route_store_route(void)
327 route_t route = xbt_new0(s_route_t, 1);
328 route->link_list = link_list;
329 // xbt_assert1(generic_processing_units_exist(current_routing,src),"the \"%s\" processing units gateway does not exist",src);
330 // xbt_assert1(generic_processing_units_exist(current_routing,dst),"the \"%s\" processing units gateway does not exist",dst);
331 xbt_assert1(current_routing->set_route,
332 "no defined method \"set_route\" in \"%s\"",
333 current_routing->name);
334 (*(current_routing->set_route)) (current_routing, src, dst, route);
341 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
343 static void parse_E_ASroute_store_route(void)
345 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
346 e_route->generic_route.link_list = link_list;
347 e_route->src_gateway = xbt_strdup(gw_src);
348 e_route->dst_gateway = xbt_strdup(gw_dst);
349 // xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
350 // xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
351 // xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
352 // xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
353 xbt_assert1(current_routing->set_ASroute,
354 "no defined method \"set_ASroute\" in \"%s\"",
355 current_routing->name);
356 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
365 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
367 static void parse_E_bypassRoute_store_route(void)
369 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
370 e_route->generic_route.link_list = link_list;
371 e_route->src_gateway = xbt_strdup(gw_src);
372 e_route->dst_gateway = xbt_strdup(gw_dst);
373 // xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
374 // xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
375 // xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
376 // xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
377 xbt_assert1(current_routing->set_bypassroute,
378 "no defined method \"set_bypassroute\" in \"%s\"",
379 current_routing->name);
380 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
390 * \brief Make a new routing component
392 * make the new structure and
393 * set the parsing functions to allows parsing the part of the routing tree
395 static void parse_S_AS(char *AS_id, char *AS_routing)
397 routing_component_t new_routing;
398 model_type_t model = NULL;
399 char *wanted = AS_routing;
401 /* seach the routing model */
402 for (cpt = 0; routing_models[cpt].name; cpt++)
403 if (!strcmp(wanted, routing_models[cpt].name))
404 model = &routing_models[cpt];
405 /* if its not exist, error */
407 fprintf(stderr, "Routing model %s not found. Existing models:\n",
409 for (cpt = 0; routing_models[cpt].name; cpt++)
410 if (!strcmp(wanted, routing_models[cpt].name))
411 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
412 routing_models[cpt].desc);
416 /* make a new routing component */
417 new_routing = (routing_component_t) (*(model->create)) ();
418 new_routing->routing = model;
419 new_routing->hierarchy = SURF_ROUTING_NULL;
420 new_routing->name = xbt_strdup(AS_id);
421 new_routing->routing_sons = xbt_dict_new();
422 //INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
424 if (current_routing == NULL && global_routing->root == NULL) {
426 /* it is the first one */
427 new_routing->routing_father = NULL;
428 global_routing->root = new_routing;
430 } else if (current_routing != NULL && global_routing->root != NULL) {
432 xbt_assert1(!xbt_dict_get_or_null
433 (current_routing->routing_sons, AS_id),
434 "The AS \"%s\" already exist", AS_id);
435 /* it is a part of the tree */
436 new_routing->routing_father = current_routing;
437 /* set the father behavior */
438 if (current_routing->hierarchy == SURF_ROUTING_NULL)
439 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
440 /* add to the sons dictionary */
441 xbt_dict_set(current_routing->routing_sons, AS_id,
442 (void *) new_routing, NULL);
443 /* add to the father element list */
444 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
445 /* unload the prev parse rules */
446 (*(current_routing->routing->unload)) ();
449 THROW0(arg_error, 0, "All defined components must be belong to a AS");
451 /* set the new parse rules */
452 (*(new_routing->routing->load)) ();
453 /* set the new current component of the tree */
454 current_routing = new_routing;
458 * Detect the routing model type of the routing component from XML platforms
460 static void parse_S_AS_XML(void)
462 parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
466 * define the routing model type of routing component from lua script
468 static void parse_S_AS_lua(char *id, char *mode)
470 parse_S_AS(id, mode);
475 * \brief Finish the creation of a new routing component
477 * When you finish to read the routing component, other structures must be created.
478 * the "end" method allow to do that for any routing model type
480 static void parse_E_AS(char *AS_id)
483 if (current_routing == NULL) {
484 THROW1(arg_error, 0, "Close AS(%s), that never open", AS_id);
486 network_element_info_t info = NULL;
487 xbt_assert1(!xbt_dict_get_or_null
488 (global_routing->where_network_elements,
489 current_routing->name), "The AS \"%s\" already exist",
490 current_routing->name);
491 info = xbt_new0(s_network_element_info_t, 1);
492 info->rc_component = current_routing->routing_father;
493 info->rc_type = SURF_NETWORK_ELEMENT_AS;
494 xbt_dict_set(global_routing->where_network_elements,
495 current_routing->name, info, NULL);
496 (*(current_routing->routing->unload)) ();
497 (*(current_routing->routing->end)) ();
498 current_routing = current_routing->routing_father;
499 if (current_routing != NULL)
500 (*(current_routing->routing->load)) ();
505 * \brief Finish the creation of a new routing component from XML
507 static void parse_E_AS_XML(void)
509 parse_E_AS(A_surfxml_AS_id);
513 * \brief Finish the creation of a new routing component from lua
515 static void parse_E_AS_lua(char *id)
520 /* Aux Business methods */
523 * \brief Get the AS father and the first elements of the chain
525 * \param src the source host name
526 * \param dst the destination host name
528 * Get the common father of the to processing units, and the first different
529 * father in the chain
531 static xbt_dynar_t elements_father(const char *src, const char *dst)
534 xbt_assert0(src && dst, "bad parameters for \"elements_father\" method");
536 xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
538 routing_component_t src_as, dst_as;
539 int index_src, index_dst, index_father_src, index_father_dst;
540 xbt_dynar_t path_src = NULL;
541 xbt_dynar_t path_dst = NULL;
542 routing_component_t current = NULL;
543 routing_component_t *current_src = NULL;
544 routing_component_t *current_dst = NULL;
545 routing_component_t *father = NULL;
547 /* (1) find the as where the src and dst are located */
548 src_as = ((network_element_info_t)
549 xbt_dict_get_or_null(global_routing->where_network_elements,
551 dst_as = ((network_element_info_t)
552 xbt_dict_get_or_null(global_routing->where_network_elements,
556 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
559 /* (2) find the path to the root routing component */
560 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
562 while (current != NULL) {
563 xbt_dynar_push(path_src, ¤t);
564 current = current->routing_father;
566 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
568 while (current != NULL) {
569 xbt_dynar_push(path_dst, ¤t);
570 current = current->routing_father;
573 /* (3) find the common father */
574 index_src = (path_src->used) - 1;
575 index_dst = (path_dst->used) - 1;
576 current_src = xbt_dynar_get_ptr(path_src, index_src);
577 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
578 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
579 current_src = xbt_dynar_get_ptr(path_src, index_src);
580 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
586 current_src = xbt_dynar_get_ptr(path_src, index_src);
587 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
589 /* (4) they are not in the same routing component, make the path */
590 index_father_src = index_src + 1;
591 index_father_dst = index_dst + 1;
593 if (*current_src == *current_dst)
594 father = current_src;
596 father = xbt_dynar_get_ptr(path_src, index_father_src);
598 /* (5) result generation */
599 xbt_dynar_push(result, father); /* first same the father of src and dst */
600 xbt_dynar_push(result, current_src); /* second the first different father of src */
601 xbt_dynar_push(result, current_dst); /* three the first different father of dst */
603 xbt_dynar_free(&path_src);
604 xbt_dynar_free(&path_dst);
609 /* Global Business methods */
612 * \brief Recursive function for get_route
614 * \param src the source host name
615 * \param dst the destination host name
617 * This fuction is call by "get_route". It allow to walk through the
618 * routing components tree.
620 static route_extended_t _get_route(const char *src, const char *dst)
624 unsigned int cpt = 0;
626 DEBUG2("Solve route \"%s\" to \"%s\"", src, dst);
628 xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
630 route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
633 xbt_dynar_t elem_father_list = elements_father(src, dst);
635 routing_component_t common_father =
636 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
637 routing_component_t src_father =
638 xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
639 routing_component_t dst_father =
640 xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
642 e_route = xbt_new0(s_route_extended_t, 1);
643 e_route->src_gateway = NULL;
644 e_route->dst_gateway = NULL;
645 e_route->generic_route.link_list =
646 xbt_dynar_new(global_routing->size_of_link, NULL);
648 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
650 if (strcmp(src, dst)) {
652 (*(common_father->get_route)) (common_father, src, dst);
653 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
655 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
656 xbt_dynar_push(e_route->generic_route.link_list, &link);
658 generic_free_extended_route(e_route_cnt);
661 } else { /* SURF_ROUTING_RECURSIVE */
663 route_extended_t e_route_bypass = NULL;
665 if (common_father->get_bypass_route)
667 (*(common_father->get_bypass_route)) (common_father, src, dst);
670 e_route_cnt = e_route_bypass;
673 (*(common_father->get_route)) (common_father, src_father->name,
676 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
677 src_father->name, dst_father->name);
679 xbt_assert2((e_route_cnt->src_gateway == NULL) ==
680 (e_route_cnt->dst_gateway == NULL),
681 "bad gateway for route between \"%s\" and \"%s\"", src,
684 if (src != e_route_cnt->src_gateway) {
685 e_route_src = _get_route(src, e_route_cnt->src_gateway);
686 xbt_assert2(e_route_src, "no route between \"%s\" and \"%s\"", src,
687 e_route_cnt->src_gateway);
688 xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
689 xbt_dynar_push(e_route->generic_route.link_list, &link);
693 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
694 xbt_dynar_push(e_route->generic_route.link_list, &link);
697 if (e_route_cnt->dst_gateway != dst) {
698 e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
699 xbt_assert2(e_route_dst, "no route between \"%s\" and \"%s\"",
700 e_route_cnt->dst_gateway, dst);
701 xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
702 xbt_dynar_push(e_route->generic_route.link_list, &link);
706 e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
707 e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
709 generic_free_extended_route(e_route_src);
710 generic_free_extended_route(e_route_cnt);
711 generic_free_extended_route(e_route_dst);
714 xbt_dynar_free(&elem_father_list);
720 * \brief Generic method: find a route between hosts
722 * \param src the source host name
723 * \param dst the destination host name
725 * walk through the routing components tree and find a route between hosts
726 * by calling the differents "get_route" functions in each routing component
728 static xbt_dynar_t get_route(const char *src, const char *dst)
731 route_extended_t e_route;
732 xbt_dynar_t elem_father_list = elements_father(src, dst);
733 routing_component_t common_father =
734 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
736 if (strcmp(src, dst))
737 e_route = _get_route(src, dst);
739 e_route = (*(common_father->get_route)) (common_father, src, dst);
741 xbt_assert2(e_route, "no route between \"%s\" and \"%s\"", src, dst);
743 if (global_routing->last_route)
744 xbt_dynar_free(&(global_routing->last_route));
745 global_routing->last_route = e_route->generic_route.link_list;
747 if (e_route->src_gateway)
748 xbt_free(e_route->src_gateway);
749 if (e_route->dst_gateway)
750 xbt_free(e_route->dst_gateway);
753 xbt_dynar_free(&elem_father_list);
755 if (xbt_dynar_length(global_routing->last_route) == 0)
758 return global_routing->last_route;
762 * \brief Recursive function for finalize
764 * \param rc the source host name
766 * This fuction is call by "finalize". It allow to finalize the
767 * AS or routing components. It delete all the structures.
769 static void _finalize(routing_component_t rc)
772 xbt_dict_cursor_t cursor = NULL;
774 routing_component_t elem;
775 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
778 xbt_dict_t tmp_sons = rc->routing_sons;
779 char *tmp_name = rc->name;
780 xbt_dict_free(&tmp_sons);
782 xbt_assert1(rc->finalize, "no defined method \"finalize\" in \"%s\"",
783 current_routing->name);
784 (*(rc->finalize)) (rc);
789 * \brief Generic method: delete all the routing structures
791 * walk through the routing components tree and delete the structures
792 * by calling the differents "finalize" functions in each routing component
794 static void finalize(void)
796 /* delete recursibly all the tree */
797 _finalize(global_routing->root);
798 /* delete "where" dict */
799 xbt_dict_free(&(global_routing->where_network_elements));
800 /* delete last_route */
801 xbt_dynar_free(&(global_routing->last_route));
802 /* delete global routing structure */
803 xbt_free(global_routing);
806 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
808 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
810 //adding my one link routes
813 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
815 xbt_dynar_foreach(onelink_mine, cpt, link) {
816 xbt_dynar_push(ret, &link);
821 xbt_dict_cursor_t cursor = NULL;
822 routing_component_t rc_child;
823 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
824 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
826 xbt_dynar_foreach(onelink_child, cpt, link) {
827 xbt_dynar_push(ret, &link);
834 static xbt_dynar_t get_onelink_routes(void)
836 return recursive_get_onelink_routes(global_routing->root);
839 static e_surf_network_element_type_t get_network_element_type(const char
842 network_element_info_t rc = NULL;
843 rc = xbt_dict_get(global_routing->where_network_elements, name);
848 * \brief Generic method: create the global routing schema
850 * Make a global routing structure and set all the parsing functions.
852 void routing_model_create(size_t size_of_links, void *loopback)
855 /* config the uniq global routing */
856 global_routing = xbt_new0(s_routing_global_t, 1);
857 global_routing->where_network_elements = xbt_dict_new();
858 global_routing->root = NULL;
859 global_routing->get_route = get_route;
860 global_routing->get_onelink_routes = get_onelink_routes;
861 global_routing->get_network_element_type = get_network_element_type;
862 global_routing->finalize = finalize;
863 global_routing->loopback = loopback;
864 global_routing->size_of_link = size_of_links;
865 global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
867 /* no current routing at moment */
868 current_routing = NULL;
870 /* parse generic elements */
871 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
872 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
874 surfxml_add_callback(STag_surfxml_route_cb_list,
875 &parse_S_route_new_and_endpoints_XML);
876 surfxml_add_callback(STag_surfxml_ASroute_cb_list,
877 &parse_S_ASroute_new_and_endpoints);
878 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
879 &parse_S_bypassRoute_new_and_endpoints);
881 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
882 &parse_E_link_ctn_new_elem_XML);
884 surfxml_add_callback(ETag_surfxml_route_cb_list,
885 &parse_E_route_store_route);
886 surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
887 &parse_E_ASroute_store_route);
888 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
889 &parse_E_bypassRoute_store_route);
891 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
892 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
894 surfxml_add_callback(STag_surfxml_cluster_cb_list,
895 &routing_full_parse_Scluster);
897 surfxml_add_callback(STag_surfxml_config_cb_list, &parse_Sconfig);
900 /* ************************************************************************** */
901 /* *************************** FULL ROUTING ********************************* */
903 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
905 /* Routing model structure */
908 s_routing_component_t generic_routing;
909 xbt_dict_t parse_routes; /* store data during the parse process */
910 xbt_dict_t to_index; /* char* -> network_element_t */
911 xbt_dict_t bypassRoutes;
912 route_extended_t *routing_table;
913 } s_routing_component_full_t, *routing_component_full_t;
915 /* Business methods */
916 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
918 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
920 routing_component_full_t routing = (routing_component_full_t) rc;
921 int table_size = xbt_dict_length(routing->to_index);
922 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
923 char *k1, *d1, *k2, *d2;
924 xbt_dict_foreach(routing->to_index, c1, k1, d1) {
925 xbt_dict_foreach(routing->to_index, c2, k2, d2) {
926 int *src_id = xbt_dict_get_or_null(routing->to_index, k1);
927 int *dst_id = xbt_dict_get_or_null(routing->to_index, k2);
930 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
932 route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
934 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
936 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
938 onelink_t onelink = xbt_new0(s_onelink_t, 1);
939 onelink->link_ptr = link;
940 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
941 onelink->src = xbt_strdup(k1);
942 onelink->dst = xbt_strdup(k2);
943 } else if (routing->generic_routing.hierarchy ==
944 SURF_ROUTING_RECURSIVE) {
945 onelink->src = xbt_strdup(route->src_gateway);
946 onelink->dst = xbt_strdup(route->dst_gateway);
948 xbt_dynar_push(ret, &onelink);
956 static route_extended_t full_get_route(routing_component_t rc,
957 const char *src, const char *dst)
959 xbt_assert1(rc && src
961 "Invalid params for \"get_route\" function at AS \"%s\"",
965 routing_component_full_t routing = (routing_component_full_t) rc;
966 int table_size = xbt_dict_length(routing->to_index);
968 generic_src_dst_check(rc, src, dst);
969 int *src_id = xbt_dict_get_or_null(routing->to_index, src);
970 int *dst_id = xbt_dict_get_or_null(routing->to_index, dst);
973 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
976 route_extended_t e_route = NULL;
977 route_extended_t new_e_route = NULL;
979 unsigned int cpt = 0;
981 e_route = TO_ROUTE_FULL(*src_id, *dst_id);
984 new_e_route = xbt_new0(s_route_extended_t, 1);
985 new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
986 new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
987 new_e_route->generic_route.link_list =
988 xbt_dynar_new(global_routing->size_of_link, NULL);
989 xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
990 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
996 static void full_finalize(routing_component_t rc)
998 routing_component_full_t routing = (routing_component_full_t) rc;
999 int table_size = xbt_dict_length(routing->to_index);
1002 /* Delete routing table */
1003 for (i = 0; i < table_size; i++)
1004 for (j = 0; j < table_size; j++)
1005 generic_free_extended_route(TO_ROUTE_FULL(i, j));
1006 xbt_free(routing->routing_table);
1007 /* Delete bypass dict */
1008 xbt_dict_free(&routing->bypassRoutes);
1009 /* Delete index dict */
1010 xbt_dict_free(&(routing->to_index));
1011 /* Delete structure */
1016 /* Creation routing model functions */
1018 static void *model_full_create(void)
1020 routing_component_full_t new_component =
1021 xbt_new0(s_routing_component_full_t, 1);
1022 new_component->generic_routing.set_processing_unit =
1023 generic_set_processing_unit;
1024 new_component->generic_routing.set_autonomous_system =
1025 generic_set_autonomous_system;
1026 new_component->generic_routing.set_route = generic_set_route;
1027 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1028 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1029 new_component->generic_routing.get_route = full_get_route;
1030 new_component->generic_routing.get_onelink_routes =
1031 full_get_onelink_routes;
1032 new_component->generic_routing.get_bypass_route =
1033 generic_get_bypassroute;
1034 new_component->generic_routing.finalize = full_finalize;
1035 new_component->to_index = xbt_dict_new();
1036 new_component->bypassRoutes = xbt_dict_new();
1037 new_component->parse_routes = xbt_dict_new();
1038 return new_component;
1041 static void model_full_load(void)
1043 /* use "surfxml_add_callback" to add a parse function call */
1046 static void model_full_unload(void)
1048 /* use "surfxml_del_callback" to remove a parse function call */
1051 static void model_full_end(void)
1055 const char *sep = "#";
1059 route_extended_t e_route;
1062 xbt_dict_cursor_t cursor = NULL;
1063 xbt_dynar_t keys = NULL;
1065 /* set utils vars */
1066 routing_component_full_t routing =
1067 ((routing_component_full_t) current_routing);
1068 int table_size = xbt_dict_length(routing->to_index);
1070 /* Create the routing table */
1071 routing->routing_table =
1072 xbt_new0(route_extended_t, table_size * table_size);
1074 /* Put the routes in position */
1075 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1076 keys = xbt_str_split_str(key, sep);
1077 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1078 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1079 TO_ROUTE_FULL(src_id, dst_id) =
1080 generic_new_extended_route(current_routing->hierarchy, data, 1);
1081 xbt_dynar_free(&keys);
1084 /* delete the parse table */
1085 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1086 route = (route_t) data;
1087 xbt_dynar_free(&(route->link_list));
1091 /* delete parse dict */
1092 xbt_dict_free(&(routing->parse_routes));
1094 /* Add the loopback if needed */
1095 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1096 for (i = 0; i < table_size; i++) {
1097 e_route = TO_ROUTE_FULL(i, i);
1099 e_route = xbt_new0(s_route_extended_t, 1);
1100 e_route->src_gateway = NULL;
1101 e_route->dst_gateway = NULL;
1102 e_route->generic_route.link_list =
1103 xbt_dynar_new(global_routing->size_of_link, NULL);
1104 xbt_dynar_push(e_route->generic_route.link_list,
1105 &global_routing->loopback);
1106 TO_ROUTE_FULL(i, i) = e_route;
1111 /* Shrink the dynar routes (save unused slots) */
1112 for (i = 0; i < table_size; i++)
1113 for (j = 0; j < table_size; j++)
1114 if (TO_ROUTE_FULL(i, j))
1115 xbt_dynar_shrink(TO_ROUTE_FULL(i, j)->generic_route.link_list, 0);
1118 /* ************************************************************************** */
1119 /* *************************** FLOYD ROUTING ******************************** */
1121 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
1122 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1123 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1125 /* Routing model structure */
1128 s_routing_component_t generic_routing;
1129 /* vars for calculate the floyd algorith. */
1130 int *predecessor_table;
1131 route_extended_t *link_table; /* char* -> int* */
1132 xbt_dict_t to_index;
1133 xbt_dict_t bypassRoutes;
1134 /* store data during the parse process */
1135 xbt_dict_t parse_routes;
1136 } s_routing_component_floyd_t, *routing_component_floyd_t;
1138 static route_extended_t floyd_get_route(routing_component_t rc,
1139 const char *src, const char *dst);
1141 /* Business methods */
1142 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1144 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1146 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1147 //int table_size = xbt_dict_length(routing->to_index);
1148 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1149 char *k1, *d1, *k2, *d2;
1150 xbt_dict_foreach(routing->to_index, c1, k1, d1) {
1151 xbt_dict_foreach(routing->to_index, c2, k2, d2) {
1152 route_extended_t route = floyd_get_route(rc, k1, k2);
1154 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1156 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1158 onelink_t onelink = xbt_new0(s_onelink_t, 1);
1159 onelink->link_ptr = link;
1160 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1161 onelink->src = xbt_strdup(k1);
1162 onelink->dst = xbt_strdup(k2);
1163 } else if (routing->generic_routing.hierarchy ==
1164 SURF_ROUTING_RECURSIVE) {
1165 onelink->src = xbt_strdup(route->src_gateway);
1166 onelink->dst = xbt_strdup(route->dst_gateway);
1168 xbt_dynar_push(ret, &onelink);
1176 static route_extended_t floyd_get_route(routing_component_t rc,
1177 const char *src, const char *dst)
1179 xbt_assert1(rc && src
1181 "Invalid params for \"get_route\" function at AS \"%s\"",
1184 /* set utils vars */
1185 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1186 int table_size = xbt_dict_length(routing->to_index);
1188 generic_src_dst_check(rc, src, dst);
1189 int *src_id = xbt_dict_get_or_null(routing->to_index, src);
1190 int *dst_id = xbt_dict_get_or_null(routing->to_index, dst);
1193 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1196 /* create a result route */
1197 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1198 new_e_route->generic_route.link_list =
1199 xbt_dynar_new(global_routing->size_of_link, NULL);
1200 new_e_route->src_gateway = NULL;
1201 new_e_route->dst_gateway = NULL;
1206 char *gw_src = NULL, *gw_dst =
1207 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1214 pred = TO_FLOYD_PRED(*src_id, pred);
1215 if (pred == -1) /* if no pred in route -> no route to host */
1217 xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1218 "Invalid link for the route between \"%s\" or \"%s\"", src,
1221 prev_gw_src = gw_src;
1222 prev_gw_dst = gw_dst;
1224 route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1225 gw_src = e_route->src_gateway;
1226 gw_dst = e_route->dst_gateway;
1231 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1232 && strcmp(gw_dst, prev_gw_src)) {
1233 xbt_dynar_t e_route_as_to_as =
1234 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1235 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1236 gw_dst, prev_gw_src);
1237 links = e_route_as_to_as;
1239 xbt_dynar_foreach(links, cpt, link) {
1240 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1246 links = e_route->generic_route.link_list;
1247 xbt_dynar_foreach(links, cpt, link) {
1248 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1252 } while (pred != *src_id);
1253 xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1254 *src_id, *dst_id, src, dst);
1256 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1257 new_e_route->src_gateway = xbt_strdup(gw_src);
1258 new_e_route->dst_gateway = xbt_strdup(first_gw);
1264 static void floyd_finalize(routing_component_t rc)
1266 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1267 int i, j, table_size;
1269 table_size = xbt_dict_length(routing->to_index);
1270 /* Delete link_table */
1271 for (i = 0; i < table_size; i++)
1272 for (j = 0; j < table_size; j++)
1273 generic_free_extended_route(TO_FLOYD_LINK(i, j));
1274 xbt_free(routing->link_table);
1275 /* Delete bypass dict */
1276 xbt_dict_free(&routing->bypassRoutes);
1277 /* Delete index dict */
1278 xbt_dict_free(&(routing->to_index));
1279 /* Delete dictionary index dict, predecessor and links table */
1280 xbt_free(routing->predecessor_table);
1281 /* Delete structure */
1286 static void *model_floyd_create(void)
1288 routing_component_floyd_t new_component =
1289 xbt_new0(s_routing_component_floyd_t, 1);
1290 new_component->generic_routing.set_processing_unit =
1291 generic_set_processing_unit;
1292 new_component->generic_routing.set_autonomous_system =
1293 generic_set_autonomous_system;
1294 new_component->generic_routing.set_route = generic_set_route;
1295 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1296 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1297 new_component->generic_routing.get_route = floyd_get_route;
1298 new_component->generic_routing.get_onelink_routes =
1299 floyd_get_onelink_routes;
1300 new_component->generic_routing.get_bypass_route =
1301 generic_get_bypassroute;
1302 new_component->generic_routing.finalize = floyd_finalize;
1303 new_component->to_index = xbt_dict_new();
1304 new_component->bypassRoutes = xbt_dict_new();
1305 new_component->parse_routes = xbt_dict_new();
1306 return new_component;
1309 static void model_floyd_load(void)
1311 /* use "surfxml_add_callback" to add a parse function call */
1314 static void model_floyd_unload(void)
1316 /* use "surfxml_del_callback" to remove a parse function call */
1319 static void model_floyd_end(void)
1322 routing_component_floyd_t routing =
1323 ((routing_component_floyd_t) current_routing);
1324 xbt_dict_cursor_t cursor = NULL;
1326 char *key, *data, *end;
1327 const char *sep = "#";
1330 unsigned int i, j, a, b, c;
1332 /* set the size of inicial table */
1333 int table_size = xbt_dict_length(routing->to_index);
1335 /* Create Cost, Predecessor and Link tables */
1336 cost_table = xbt_new0(double, table_size * table_size); /* link cost from host to host */
1337 routing->predecessor_table = xbt_new0(int, table_size * table_size); /* predecessor host numbers */
1338 routing->link_table = xbt_new0(route_extended_t, table_size * table_size); /* actual link between src and dst */
1340 /* Initialize costs and predecessors */
1341 for (i = 0; i < table_size; i++)
1342 for (j = 0; j < table_size; j++) {
1343 TO_FLOYD_COST(i, j) = DBL_MAX;
1344 TO_FLOYD_PRED(i, j) = -1;
1345 TO_FLOYD_LINK(i, j) = NULL; /* fixed, missing in the previous version */
1348 /* Put the routes in position */
1349 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1350 keys = xbt_str_split_str(key, sep);
1351 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1352 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1353 TO_FLOYD_LINK(src_id, dst_id) =
1354 generic_new_extended_route(current_routing->hierarchy, data, 0);
1355 TO_FLOYD_PRED(src_id, dst_id) = src_id;
1357 TO_FLOYD_COST(src_id, dst_id) = ((TO_FLOYD_LINK(src_id, dst_id))->generic_route.link_list)->used; /* count of links, old model assume 1 */
1358 xbt_dynar_free(&keys);
1361 /* Add the loopback if needed */
1362 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1363 for (i = 0; i < table_size; i++) {
1364 route_extended_t e_route = TO_FLOYD_LINK(i, i);
1366 e_route = xbt_new0(s_route_extended_t, 1);
1367 e_route->src_gateway = NULL;
1368 e_route->dst_gateway = NULL;
1369 e_route->generic_route.link_list =
1370 xbt_dynar_new(global_routing->size_of_link, NULL);
1371 xbt_dynar_push(e_route->generic_route.link_list,
1372 &global_routing->loopback);
1373 TO_FLOYD_LINK(i, i) = e_route;
1374 TO_FLOYD_PRED(i, i) = i;
1375 TO_FLOYD_COST(i, i) = 1;
1379 /* Calculate path costs */
1380 for (c = 0; c < table_size; c++) {
1381 for (a = 0; a < table_size; a++) {
1382 for (b = 0; b < table_size; b++) {
1383 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1384 if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1385 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1386 TO_FLOYD_COST(a, b))) {
1387 TO_FLOYD_COST(a, b) =
1388 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1389 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1396 /* delete the parse table */
1397 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1398 route_t route = (route_t) data;
1399 xbt_dynar_free(&(route->link_list));
1403 /* delete parse dict */
1404 xbt_dict_free(&(routing->parse_routes));
1407 xbt_free(cost_table);
1410 /* ************************************************************************** */
1411 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1414 s_routing_component_t generic_routing;
1415 xbt_dict_t to_index;
1416 xbt_dict_t bypassRoutes;
1417 xbt_graph_t route_graph; /* xbt_graph */
1418 xbt_dict_t graph_node_map; /* map */
1419 xbt_dict_t route_cache; /* use in cache mode */
1421 xbt_dict_t parse_routes;
1422 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1425 typedef struct graph_node_data {
1427 int graph_id; /* used for caching internal graph id's */
1428 } s_graph_node_data_t, *graph_node_data_t;
1430 typedef struct graph_node_map_element {
1432 } s_graph_node_map_element_t, *graph_node_map_element_t;
1434 typedef struct route_cache_element {
1437 } s_route_cache_element_t, *route_cache_element_t;
1439 /* Free functions */
1441 static void route_cache_elem_free(void *e)
1443 route_cache_element_t elm = (route_cache_element_t) e;
1445 xbt_free(elm->pred_arr);
1450 static void graph_node_map_elem_free(void *e)
1452 graph_node_map_element_t elm = (graph_node_map_element_t) e;
1458 static void graph_edge_data_free(void *e)
1460 route_extended_t e_route = (route_extended_t) e;
1462 xbt_dynar_free(&(e_route->generic_route.link_list));
1463 if (e_route->src_gateway)
1464 xbt_free(e_route->src_gateway);
1465 if (e_route->dst_gateway)
1466 xbt_free(e_route->dst_gateway);
1471 /* Utility functions */
1473 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1474 int id, int graph_id)
1476 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1477 xbt_node_t node = NULL;
1478 graph_node_data_t data = NULL;
1479 graph_node_map_element_t elm = NULL;
1481 data = xbt_new0(struct graph_node_data, 1);
1483 data->graph_id = graph_id;
1484 node = xbt_graph_new_node(routing->route_graph, data);
1486 elm = xbt_new0(struct graph_node_map_element, 1);
1488 xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1489 (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1494 static graph_node_map_element_t
1495 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1497 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1498 graph_node_map_element_t elm = (graph_node_map_element_t)
1499 xbt_dict_get_or_null_ext(routing->graph_node_map,
1507 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1508 int dst_id, route_extended_t e_route)
1510 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1512 xbt_node_t src = NULL;
1513 xbt_node_t dst = NULL;
1514 graph_node_map_element_t src_elm = (graph_node_map_element_t)
1515 xbt_dict_get_or_null_ext(routing->graph_node_map,
1518 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1519 xbt_dict_get_or_null_ext(routing->graph_node_map,
1524 src = src_elm->node;
1527 dst = dst_elm->node;
1529 /* add nodes if they don't exist in the graph */
1530 if (src_id == dst_id && src == NULL && dst == NULL) {
1531 src = route_graph_new_node(rc, src_id, -1);
1535 src = route_graph_new_node(rc, src_id, -1);
1538 dst = route_graph_new_node(rc, dst_id, -1);
1542 /* add link as edge to graph */
1543 xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1546 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1548 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1550 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1552 xbt_node_t node = NULL;
1553 unsigned int cursor2;
1554 xbt_dynar_foreach(nodes, cursor2, node) {
1555 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1556 xbt_edge_t edge = NULL;
1557 unsigned int cursor;
1560 xbt_dynar_foreach(out_edges, cursor, edge) {
1561 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1562 if (other_node == node) {
1569 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1570 e_route->src_gateway = NULL;
1571 e_route->dst_gateway = NULL;
1572 e_route->generic_route.link_list =
1573 xbt_dynar_new(global_routing->size_of_link, NULL);
1574 xbt_dynar_push(e_route->generic_route.link_list,
1575 &global_routing->loopback);
1576 xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1581 /* Business methods */
1582 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
1584 xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
1587 static route_extended_t dijkstra_get_route(routing_component_t rc,
1591 xbt_assert1(rc && src
1593 "Invalid params for \"get_route\" function at AS \"%s\"",
1596 /* set utils vars */
1597 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1599 generic_src_dst_check(rc, src, dst);
1600 int *src_id = xbt_dict_get_or_null(routing->to_index, src);
1601 int *dst_id = xbt_dict_get_or_null(routing->to_index, dst);
1604 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1607 /* create a result route */
1608 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1609 new_e_route->generic_route.link_list =
1610 xbt_dynar_new(global_routing->size_of_link, NULL);
1611 new_e_route->src_gateway = NULL;
1612 new_e_route->dst_gateway = NULL;
1614 int *pred_arr = NULL;
1615 int src_node_id = 0;
1616 int dst_node_id = 0;
1619 route_extended_t e_route;
1623 xbt_dynar_t links = NULL;
1624 route_cache_element_t elm = NULL;
1625 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1627 /* Use the graph_node id mapping set to quickly find the nodes */
1628 graph_node_map_element_t src_elm =
1629 graph_node_map_search(routing, *src_id);
1630 graph_node_map_element_t dst_elm =
1631 graph_node_map_search(routing, *dst_id);
1632 xbt_assert2(src_elm != NULL
1633 && dst_elm != NULL, "src %d or dst %d does not exist",
1635 src_node_id = ((graph_node_data_t)
1636 xbt_graph_node_get_data(src_elm->node))->graph_id;
1637 dst_node_id = ((graph_node_data_t)
1638 xbt_graph_node_get_data(dst_elm->node))->graph_id;
1640 /* if the src and dst are the same *//* fixed, missing in the previous version */
1641 if (src_node_id == dst_node_id) {
1643 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1644 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1646 xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1648 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1651 e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1653 links = e_route->generic_route.link_list;
1654 xbt_dynar_foreach(links, cpt, link) {
1655 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1661 if (routing->cached) {
1662 /*check if there is a cached predecessor list avail */
1663 elm = (route_cache_element_t)
1664 xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
1668 if (elm) { /* cached mode and cache hit */
1669 pred_arr = elm->pred_arr;
1670 } else { /* not cached mode or cache miss */
1671 double *cost_arr = NULL;
1672 xbt_heap_t pqueue = NULL;
1675 int nr_nodes = xbt_dynar_length(nodes);
1676 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
1677 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
1678 pqueue = xbt_heap_new(nr_nodes, xbt_free);
1681 cost_arr[src_node_id] = 0.0;
1683 for (i = 0; i < nr_nodes; i++) {
1684 if (i != src_node_id) {
1685 cost_arr[i] = DBL_MAX;
1690 /* initialize priority queue */
1691 nodeid = xbt_new0(int, 1);
1693 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1697 /* apply dijkstra using the indexes from the graph's node array */
1698 while (xbt_heap_size(pqueue) > 0) {
1699 int *v_id = xbt_heap_pop(pqueue);
1700 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1701 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1702 xbt_edge_t edge = NULL;
1703 unsigned int cursor;
1705 xbt_dynar_foreach(out_edges, cursor, edge) {
1706 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1707 graph_node_data_t data = xbt_graph_node_get_data(u_node);
1708 int u_id = data->graph_id;
1709 route_extended_t tmp_e_route =
1710 (route_extended_t) xbt_graph_edge_get_data(edge);
1711 int cost_v_u = (tmp_e_route->generic_route.link_list)->used; /* count of links, old model assume 1 */
1713 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1714 pred_arr[u_id] = *v_id;
1715 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1716 nodeid = xbt_new0(int, 1);
1718 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1722 /* free item popped from pqueue */
1727 xbt_heap_free(pqueue);
1730 /* compose route path with links */
1731 char *gw_src = NULL, *gw_dst =
1732 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1734 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1735 xbt_node_t node_pred_v =
1736 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1737 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1739 xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1741 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1744 prev_gw_src = gw_src;
1745 prev_gw_dst = gw_dst;
1747 e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1748 gw_src = e_route->src_gateway;
1749 gw_dst = e_route->dst_gateway;
1751 if (v == dst_node_id)
1754 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
1755 && strcmp(gw_dst, prev_gw_src)) {
1756 xbt_dynar_t e_route_as_to_as =
1757 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1758 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1759 gw_dst, prev_gw_src);
1760 links = e_route_as_to_as;
1762 xbt_dynar_foreach(links, cpt, link) {
1763 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1769 links = e_route->generic_route.link_list;
1770 xbt_dynar_foreach(links, cpt, link) {
1771 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1776 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1777 new_e_route->src_gateway = xbt_strdup(gw_src);
1778 new_e_route->dst_gateway = xbt_strdup(first_gw);
1781 if (routing->cached && elm == NULL) {
1782 /* add to predecessor list of the current src-host to cache */
1783 elm = xbt_new0(struct route_cache_element, 1);
1784 elm->pred_arr = pred_arr;
1786 xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
1787 (xbt_set_elm_t) elm, &route_cache_elem_free);
1790 if (!routing->cached)
1796 static void dijkstra_finalize(routing_component_t rc)
1798 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1801 xbt_graph_free_graph(routing->route_graph, &xbt_free,
1802 &graph_edge_data_free, &xbt_free);
1803 xbt_dict_free(&routing->graph_node_map);
1804 if (routing->cached)
1805 xbt_dict_free(&routing->route_cache);
1806 /* Delete bypass dict */
1807 xbt_dict_free(&routing->bypassRoutes);
1808 /* Delete index dict */
1809 xbt_dict_free(&(routing->to_index));
1810 /* Delete structure */
1815 /* Creation routing model functions */
1817 static void *model_dijkstra_both_create(int cached)
1819 routing_component_dijkstra_t new_component =
1820 xbt_new0(s_routing_component_dijkstra_t, 1);
1821 new_component->generic_routing.set_processing_unit =
1822 generic_set_processing_unit;
1823 new_component->generic_routing.set_autonomous_system =
1824 generic_set_autonomous_system;
1825 new_component->generic_routing.set_route = generic_set_route;
1826 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1827 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1828 new_component->generic_routing.get_route = dijkstra_get_route;
1829 new_component->generic_routing.get_onelink_routes =
1830 dijkstra_get_onelink_routes;
1831 new_component->generic_routing.get_bypass_route =
1832 generic_get_bypassroute;
1833 new_component->generic_routing.finalize = dijkstra_finalize;
1834 new_component->cached = cached;
1835 new_component->to_index = xbt_dict_new();
1836 new_component->bypassRoutes = xbt_dict_new();
1837 new_component->parse_routes = xbt_dict_new();
1838 return new_component;
1841 static void *model_dijkstra_create(void)
1843 return model_dijkstra_both_create(0);
1846 static void *model_dijkstracache_create(void)
1848 return model_dijkstra_both_create(1);
1851 static void model_dijkstra_both_load(void)
1853 /* use "surfxml_add_callback" to add a parse function call */
1856 static void model_dijkstra_both_unload(void)
1858 /* use "surfxml_del_callback" to remove a parse function call */
1861 static void model_dijkstra_both_end(void)
1863 routing_component_dijkstra_t routing =
1864 (routing_component_dijkstra_t) current_routing;
1865 xbt_dict_cursor_t cursor = NULL;
1866 char *key, *data, *end;
1867 const char *sep = "#";
1869 xbt_node_t node = NULL;
1870 unsigned int cursor2;
1871 xbt_dynar_t nodes = NULL;
1875 /* Create the topology graph */
1876 routing->route_graph = xbt_graph_new_graph(1, NULL);
1877 routing->graph_node_map = xbt_dict_new();
1879 if (routing->cached)
1880 routing->route_cache = xbt_dict_new();
1882 /* Put the routes in position */
1883 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1884 keys = xbt_str_split_str(key, sep);
1885 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1886 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1887 route_extended_t e_route =
1888 generic_new_extended_route(current_routing->hierarchy, data, 0);
1889 route_new_dijkstra(routing, src_id, dst_id, e_route);
1890 xbt_dynar_free(&keys);
1893 /* delete the parse table */
1894 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1895 route = (route_t) data;
1896 xbt_dynar_free(&(route->link_list));
1900 /* delete parse dict */
1901 xbt_dict_free(&(routing->parse_routes));
1903 /* Add the loopback if needed */
1904 if (current_routing->hierarchy == SURF_ROUTING_BASE)
1905 add_loopback_dijkstra(routing);
1907 /* initialize graph indexes in nodes after graph has been built */
1908 nodes = xbt_graph_get_nodes(routing->route_graph);
1910 xbt_dynar_foreach(nodes, cursor2, node) {
1911 graph_node_data_t data = xbt_graph_node_get_data(node);
1912 data->graph_id = cursor2;
1917 #ifdef HAVE_PCRE_LIB
1918 /* ************************************************** */
1919 /* ************** RULE-BASED ROUTING **************** */
1921 /* Routing model structure */
1924 s_routing_component_t generic_routing;
1925 xbt_dict_t dict_processing_units;
1926 xbt_dict_t dict_autonomous_systems;
1927 xbt_dynar_t list_route;
1928 xbt_dynar_t list_ASroute;
1929 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
1931 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
1932 typedef struct s_rule_route_extended s_rule_route_extended_t,
1933 *rule_route_extended_t;
1935 struct s_rule_route {
1936 xbt_dynar_t re_str_link; // dynar of char*
1941 struct s_rule_route_extended {
1942 s_rule_route_t generic_rule_route;
1943 char *re_src_gateway;
1944 char *re_dst_gateway;
1947 static void rule_route_free(void *e)
1949 rule_route_t *elem = (rule_route_t *) (e);
1951 xbt_dynar_free(&(*elem)->re_str_link);
1952 pcre_free((*elem)->re_src);
1953 pcre_free((*elem)->re_dst);
1959 static void rule_route_extended_free(void *e)
1961 rule_route_extended_t *elem = (rule_route_extended_t *) e;
1963 xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
1964 pcre_free((*elem)->generic_rule_route.re_src);
1965 pcre_free((*elem)->generic_rule_route.re_dst);
1966 xbt_free((*elem)->re_src_gateway);
1967 xbt_free((*elem)->re_dst_gateway);
1972 /* Parse routing model functions */
1974 static void model_rulebased_set_processing_unit(routing_component_t rc,
1977 routing_component_rulebased_t routing =
1978 (routing_component_rulebased_t) rc;
1979 xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
1982 static void model_rulebased_set_autonomous_system(routing_component_t rc,
1985 routing_component_rulebased_t routing =
1986 (routing_component_rulebased_t) rc;
1987 xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
1991 static void model_rulebased_set_route(routing_component_t rc,
1992 const char *src, const char *dst,
1995 routing_component_rulebased_t routing =
1996 (routing_component_rulebased_t) rc;
1997 rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
2000 ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
2001 xbt_assert3(ruleroute->re_src,
2002 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2003 erroffset, src, error);
2004 ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
2005 xbt_assert3(ruleroute->re_src,
2006 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2007 erroffset, dst, error);
2008 ruleroute->re_str_link = route->link_list;
2009 xbt_dynar_push(routing->list_route, &ruleroute);
2013 static void model_rulebased_set_ASroute(routing_component_t rc,
2014 const char *src, const char *dst,
2015 route_extended_t route)
2017 routing_component_rulebased_t routing =
2018 (routing_component_rulebased_t) rc;
2019 rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
2022 ruleroute_e->generic_rule_route.re_src =
2023 pcre_compile(src, 0, &error, &erroffset, NULL);
2024 xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2025 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2026 erroffset, src, error);
2027 ruleroute_e->generic_rule_route.re_dst =
2028 pcre_compile(dst, 0, &error, &erroffset, NULL);
2029 xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2030 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2031 erroffset, dst, error);
2032 ruleroute_e->generic_rule_route.re_str_link =
2033 route->generic_route.link_list;
2034 ruleroute_e->re_src_gateway = route->src_gateway;
2035 ruleroute_e->re_dst_gateway = route->dst_gateway;
2036 xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2037 xbt_free(route->src_gateway);
2038 xbt_free(route->dst_gateway);
2042 static void model_rulebased_set_bypassroute(routing_component_t rc,
2045 route_extended_t e_route)
2047 xbt_die("bypass routing not supported for Route-Based model");
2050 #define BUFFER_SIZE 4096 /* result buffer size */
2051 #define OVECCOUNT 30 /* should be a multiple of 3 */
2053 static char *remplace(char *value, const char **src_list, int src_size,
2054 const char **dst_list, int dst_size)
2057 char result_result[BUFFER_SIZE];
2058 int i_result_buffer;
2059 int value_length = (int) strlen(value);
2063 i_result_buffer = 0;
2065 if (value[i] == '$') {
2069 int number_length = 0;
2070 while ('0' <= value[i + number_length]
2071 && value[i + number_length] <= '9') {
2074 xbt_assert2(number_length != 0,
2075 "bad string parameter, no number indication, at offset: %d (\"%s\")",
2079 number = atoi(value + i);
2080 i = i + number_length;
2081 xbt_assert2(i + 2 < value_length,
2082 "bad string parameter, too few chars, at offset: %d (\"%s\")",
2085 // solve the indication
2086 const char **param_list;
2088 if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2089 param_list = src_list;
2090 param_size = src_size;
2091 } else if (value[i] == 'd' && value[i + 1] == 's'
2092 && value[i + 2] == 't') {
2093 param_list = dst_list;
2094 param_size = dst_size;
2097 "bad string parameter, support only \"src\" and \"dst\", at offset: %d (\"%s\")",
2102 xbt_assert4(param_size >= number,
2103 "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2104 i, value, param_size, number);
2106 const char *param = param_list[number];
2107 int size = strlen(param);
2109 for (cp = 0; cp < size; cp++) {
2110 result_result[i_result_buffer] = param[cp];
2112 if (i_result_buffer >= BUFFER_SIZE)
2116 result_result[i_result_buffer] = value[i];
2121 } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2123 xbt_assert2(i_result_buffer < BUFFER_SIZE,
2124 "solving string \"%s\", small buffer size (%d)", value,
2126 result_result[i_result_buffer] = 0;
2127 return xbt_strdup(result_result);
2130 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2132 xbt_die("\"rulebased_get_onelink_routes\" function not implemented yet");
2135 /* Business methods */
2136 static route_extended_t rulebased_get_route(routing_component_t rc,
2140 xbt_assert1(rc && src
2142 "Invalid params for \"get_route\" function at AS \"%s\"",
2145 /* set utils vars */
2146 routing_component_rulebased_t routing =
2147 (routing_component_rulebased_t) rc;
2149 int are_processing_units;
2150 xbt_dynar_t rule_list;
2151 if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2152 && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2153 are_processing_units = 1;
2154 rule_list = routing->list_route;
2155 } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2156 && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2158 are_processing_units = 0;
2159 rule_list = routing->list_ASroute;
2162 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
2167 int src_length = (int) strlen(src);
2168 int dst_length = (int) strlen(dst);
2170 xbt_dynar_t links_list =
2171 xbt_dynar_new(global_routing->size_of_link, NULL);
2173 rule_route_t ruleroute;
2175 int ovector_src[OVECCOUNT];
2176 int ovector_dst[OVECCOUNT];
2177 const char **list_src = NULL;
2178 const char **list_dst = NULL;
2179 xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2181 pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2182 ovector_src, OVECCOUNT);
2185 pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2186 ovector_dst, OVECCOUNT);
2188 xbt_assert1(!pcre_get_substring_list
2189 (src, ovector_src, rc_src, &list_src),
2190 "error solving substring list for src \"%s\"", src);
2191 xbt_assert1(!pcre_get_substring_list
2192 (dst, ovector_dst, rc_dst, &list_dst),
2193 "error solving substring list for src \"%s\"", dst);
2195 xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2196 char *new_link_name =
2197 remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2199 xbt_dict_get_or_null(surf_network_model->resource_set,
2202 xbt_dynar_push(links_list, &link);
2204 THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2205 xbt_free(new_link_name);
2209 if (rc_src >= 0 && rc_dst >= 0)
2213 route_extended_t new_e_route = NULL;
2214 if (rc_src >= 0 && rc_dst >= 0) {
2215 new_e_route = xbt_new0(s_route_extended_t, 1);
2216 new_e_route->generic_route.link_list = links_list;
2217 } else if (!strcmp(src, dst) && are_processing_units) {
2218 new_e_route = xbt_new0(s_route_extended_t, 1);
2219 xbt_dynar_push(links_list, &(global_routing->loopback));
2220 new_e_route->generic_route.link_list = links_list;
2222 xbt_dynar_free(&link_list);
2225 if (!are_processing_units && new_e_route) {
2226 rule_route_extended_t ruleroute_extended =
2227 (rule_route_extended_t) ruleroute;
2228 new_e_route->src_gateway =
2229 remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2231 new_e_route->dst_gateway =
2232 remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2237 pcre_free_substring_list(list_src);
2239 pcre_free_substring_list(list_dst);
2244 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2251 static void rulebased_finalize(routing_component_t rc)
2253 routing_component_rulebased_t routing =
2254 (routing_component_rulebased_t) rc;
2256 xbt_dict_free(&routing->dict_processing_units);
2257 xbt_dict_free(&routing->dict_autonomous_systems);
2258 xbt_dynar_free(&routing->list_route);
2259 xbt_dynar_free(&routing->list_ASroute);
2260 /* Delete structure */
2265 /* Creation routing model functions */
2266 static void *model_rulebased_create(void)
2268 routing_component_rulebased_t new_component =
2269 xbt_new0(s_routing_component_rulebased_t, 1);
2270 new_component->generic_routing.set_processing_unit =
2271 model_rulebased_set_processing_unit;
2272 new_component->generic_routing.set_autonomous_system =
2273 model_rulebased_set_autonomous_system;
2274 new_component->generic_routing.set_route = model_rulebased_set_route;
2275 new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2276 new_component->generic_routing.set_bypassroute =
2277 model_rulebased_set_bypassroute;
2278 new_component->generic_routing.get_onelink_routes =
2279 rulebased_get_onelink_routes;
2280 new_component->generic_routing.get_route = rulebased_get_route;
2281 new_component->generic_routing.get_bypass_route = NULL; //rulebased_get_bypass_route;
2282 new_component->generic_routing.finalize = rulebased_finalize;
2283 /* initialization of internal structures */
2284 new_component->dict_processing_units = xbt_dict_new();
2285 new_component->dict_autonomous_systems = xbt_dict_new();
2286 new_component->list_route =
2287 xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2288 new_component->list_ASroute =
2289 xbt_dynar_new(sizeof(rule_route_extended_t),
2290 &rule_route_extended_free);
2291 return new_component;
2294 static void model_rulebased_load(void)
2296 /* use "surfxml_add_callback" to add a parse function call */
2299 static void model_rulebased_unload(void)
2301 /* use "surfxml_del_callback" to remove a parse function call */
2304 static void model_rulebased_end(void)
2308 #endif /* HAVE_PCRE_LIB */
2310 /* ************************************************************************** */
2311 /* ******************************* NO ROUTING ******************************* */
2313 /* Routing model structure */
2315 s_routing_component_t generic_routing;
2316 } s_routing_component_none_t, *routing_component_none_t;
2318 /* Business methods */
2319 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2324 static route_extended_t none_get_route(routing_component_t rc,
2325 const char *src, const char *dst)
2330 static route_extended_t none_get_bypass_route(routing_component_t rc,
2337 static void none_finalize(routing_component_t rc)
2342 static void none_set_processing_unit(routing_component_t rc,
2347 static void none_set_autonomous_system(routing_component_t rc,
2352 /* Creation routing model functions */
2353 static void *model_none_create(void)
2355 routing_component_none_t new_component =
2356 xbt_new0(s_routing_component_none_t, 1);
2357 new_component->generic_routing.set_processing_unit =
2358 none_set_processing_unit;
2359 new_component->generic_routing.set_autonomous_system =
2360 none_set_autonomous_system;
2361 new_component->generic_routing.set_route = NULL;
2362 new_component->generic_routing.set_ASroute = NULL;
2363 new_component->generic_routing.set_bypassroute = NULL;
2364 new_component->generic_routing.get_route = none_get_route;
2365 new_component->generic_routing.get_onelink_routes =
2366 none_get_onelink_routes;
2367 new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2368 new_component->generic_routing.finalize = none_finalize;
2369 return new_component;
2372 static void model_none_load(void)
2376 static void model_none_unload(void)
2380 static void model_none_end(void)
2384 /* ************************************************** */
2385 /* ********** PATERN FOR NEW ROUTING **************** */
2387 /* The minimal configuration of a new routing model need the next functions,
2388 * also you need to set at the start of the file, the new model in the model
2389 * list. Remember keep the null ending of the list.
2391 /*** Routing model structure ***/
2393 // s_routing_component_t generic_routing;
2394 // /* things that your routing model need */
2395 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2397 /*** Parse routing model functions ***/
2398 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2399 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2400 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2401 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2402 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2404 /*** Business methods ***/
2405 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2406 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2407 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2409 /*** Creation routing model functions ***/
2410 // static void* model_NEW_create(void) {
2411 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
2412 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2413 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2414 // new_component->generic_routing.set_route = model_NEW_set_route;
2415 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2416 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2417 // new_component->generic_routing.get_route = NEW_get_route;
2418 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2419 // new_component->generic_routing.finalize = NEW_finalize;
2420 // /* initialization of internal structures */
2421 // return new_component;
2422 // } /* mandatory */
2423 // static void model_NEW_load(void) {} /* mandatory */
2424 // static void model_NEW_unload(void) {} /* mandatory */
2425 // static void model_NEW_end(void) {} /* mandatory */
2427 /* ************************************************************************** */
2428 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2430 static void generic_set_processing_unit(routing_component_t rc,
2433 DEBUG1("Load process unit \"%s\"", name);
2434 model_type_t modeltype = rc->routing;
2435 int *id = xbt_new0(int, 1);
2436 xbt_dict_t _to_index;
2437 if (modeltype == &routing_models[SURF_MODEL_FULL])
2438 _to_index = ((routing_component_full_t) rc)->to_index;
2440 else if (modeltype == &routing_models[SURF_MODEL_FLOYD])
2441 _to_index = ((routing_component_floyd_t) rc)->to_index;
2443 else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2444 modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE])
2445 _to_index = ((routing_component_dijkstra_t) rc)->to_index;
2448 xbt_die("\"generic_set_processing_unit\" not supported");
2449 *id = xbt_dict_length(_to_index);
2450 xbt_dict_set(_to_index, name, id, xbt_free);
2453 static void generic_set_autonomous_system(routing_component_t rc,
2456 DEBUG1("Load Autonomous system \"%s\"", name);
2457 model_type_t modeltype = rc->routing;
2458 int *id = xbt_new0(int, 1);
2459 xbt_dict_t _to_index;
2460 if (modeltype == &routing_models[SURF_MODEL_FULL])
2461 _to_index = ((routing_component_full_t) rc)->to_index;
2463 else if (modeltype == &routing_models[SURF_MODEL_FLOYD])
2464 _to_index = ((routing_component_floyd_t) rc)->to_index;
2466 else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2467 modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE])
2468 _to_index = ((routing_component_dijkstra_t) rc)->to_index;
2471 xbt_die("\"generic_set_autonomous_system\" not supported");
2472 *id = xbt_dict_length(_to_index);
2473 xbt_dict_set(_to_index, name, id, xbt_free);
2476 static void generic_set_route(routing_component_t rc, const char *src,
2477 const char *dst, route_t route)
2479 DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
2480 model_type_t modeltype = rc->routing;
2481 xbt_dict_t _parse_routes;
2482 xbt_dict_t _to_index;
2484 int *src_id, *dst_id;
2486 if (modeltype == &routing_models[SURF_MODEL_FULL]) {
2487 _parse_routes = ((routing_component_full_t) rc)->parse_routes;
2488 _to_index = ((routing_component_full_t) rc)->to_index;
2490 } else if (modeltype == &routing_models[SURF_MODEL_FLOYD]) {
2491 _parse_routes = ((routing_component_floyd_t) rc)->parse_routes;
2492 _to_index = ((routing_component_floyd_t) rc)->to_index;
2494 } else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2495 modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2496 _parse_routes = ((routing_component_dijkstra_t) rc)->parse_routes;
2497 _to_index = ((routing_component_dijkstra_t) rc)->to_index;
2500 xbt_die("\"generic_set_route\" not supported");
2502 src_id = xbt_dict_get_or_null(_to_index, src);
2503 dst_id = xbt_dict_get_or_null(_to_index, dst);
2506 && dst_id, "Network elements %s or %s not found", src, dst);
2507 route_name = bprintf("%d#%d", *src_id, *dst_id);
2509 xbt_assert2(xbt_dynar_length(route->link_list) > 0,
2510 "Invalid count of links, must be greater than zero (%s,%s)",
2512 xbt_assert2(!xbt_dict_get_or_null(_parse_routes, route_name),
2513 "The route between \"%s\" and \"%s\" already exist", src,
2516 xbt_dict_set(_parse_routes, route_name, route, NULL);
2517 xbt_free(route_name);
2520 static void generic_set_ASroute(routing_component_t rc, const char *src,
2521 const char *dst, route_extended_t e_route)
2523 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2524 e_route->src_gateway, dst, e_route->dst_gateway);
2525 model_type_t modeltype = rc->routing;
2526 xbt_dict_t _parse_routes;
2527 xbt_dict_t _to_index;
2529 int *src_id, *dst_id;
2531 if (modeltype == &routing_models[SURF_MODEL_FULL]) {
2532 _parse_routes = ((routing_component_full_t) rc)->parse_routes;
2533 _to_index = ((routing_component_full_t) rc)->to_index;
2535 } else if (modeltype == &routing_models[SURF_MODEL_FLOYD]) {
2536 _parse_routes = ((routing_component_floyd_t) rc)->parse_routes;
2537 _to_index = ((routing_component_floyd_t) rc)->to_index;
2539 } else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2540 modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2541 _parse_routes = ((routing_component_dijkstra_t) rc)->parse_routes;
2542 _to_index = ((routing_component_dijkstra_t) rc)->to_index;
2545 xbt_die("\"generic_set_route\" not supported");
2547 src_id = xbt_dict_get_or_null(_to_index, src);
2548 dst_id = xbt_dict_get_or_null(_to_index, dst);
2551 && dst_id, "Network elements %s or %s not found", src, dst);
2552 route_name = bprintf("%d#%d", *src_id, *dst_id);
2554 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2555 "Invalid count of links, must be greater than zero (%s,%s)",
2557 xbt_assert4(!xbt_dict_get_or_null(_parse_routes, route_name),
2558 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2559 src, e_route->src_gateway, dst, e_route->dst_gateway);
2561 xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2562 xbt_free(route_name);
2565 static void generic_set_bypassroute(routing_component_t rc,
2566 const char *src, const char *dst,
2567 route_extended_t e_route)
2569 DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2570 model_type_t modeltype = rc->routing;
2571 xbt_dict_t dict_bypassRoutes;
2573 if (modeltype == &routing_models[SURF_MODEL_FULL]) {
2574 dict_bypassRoutes = ((routing_component_full_t) rc)->bypassRoutes;
2575 } else if (modeltype == &routing_models[SURF_MODEL_FLOYD]) {
2576 dict_bypassRoutes = ((routing_component_floyd_t) rc)->bypassRoutes;
2577 } else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2578 modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2579 dict_bypassRoutes = ((routing_component_dijkstra_t) rc)->bypassRoutes;
2581 xbt_die("\"generic_set_bypassroute\" not supported");
2582 route_name = bprintf("%s#%s", src, dst);
2583 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2584 "Invalid count of links, must be greater than zero (%s,%s)",
2586 xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2587 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2588 src, e_route->src_gateway, dst, e_route->dst_gateway);
2590 route_extended_t new_e_route =
2591 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2592 xbt_dynar_free(&(e_route->generic_route.link_list));
2595 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2596 (void (*)(void *)) generic_free_extended_route);
2597 xbt_free(route_name);
2600 /* ************************************************************************** */
2601 /* *********************** GENERIC BUSINESS METHODS ************************* */
2603 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2605 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2608 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2612 model_type_t modeltype = rc->routing;
2613 xbt_dict_t dict_bypassRoutes;
2615 if (modeltype == &routing_models[SURF_MODEL_FULL]) {
2616 dict_bypassRoutes = ((routing_component_full_t) rc)->bypassRoutes;
2617 } else if (modeltype == &routing_models[SURF_MODEL_FLOYD]) {
2618 dict_bypassRoutes = ((routing_component_floyd_t) rc)->bypassRoutes;
2619 } else if (modeltype == &routing_models[SURF_MODEL_DIJKSTRA] ||
2620 modeltype == &routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2621 dict_bypassRoutes = ((routing_component_dijkstra_t) rc)->bypassRoutes;
2623 xbt_die("\"generic_get_bypassroute\" not supported");
2626 routing_component_t src_as, dst_as;
2627 int index_src, index_dst;
2628 xbt_dynar_t path_src = NULL;
2629 xbt_dynar_t path_dst = NULL;
2630 routing_component_t current = NULL;
2631 routing_component_t *current_src = NULL;
2632 routing_component_t *current_dst = NULL;
2634 /* (1) find the as where the src and dst are located */
2635 src_as = ((network_element_info_t)
2636 xbt_dict_get_or_null(global_routing->where_network_elements,
2637 src))->rc_component;
2638 dst_as = ((network_element_info_t)
2639 xbt_dict_get_or_null(global_routing->where_network_elements,
2640 dst))->rc_component;
2643 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2646 /* (2) find the path to the root routing component */
2647 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2649 while (current != NULL) {
2650 xbt_dynar_push(path_src, ¤t);
2651 current = current->routing_father;
2653 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2655 while (current != NULL) {
2656 xbt_dynar_push(path_dst, ¤t);
2657 current = current->routing_father;
2660 /* (3) find the common father */
2661 index_src = (path_src->used) - 1;
2662 index_dst = (path_dst->used) - 1;
2663 current_src = xbt_dynar_get_ptr(path_src, index_src);
2664 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2665 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2666 routing_component_t *tmp_src, *tmp_dst;
2667 tmp_src = xbt_dynar_pop_ptr(path_src);
2668 tmp_dst = xbt_dynar_pop_ptr(path_dst);
2671 current_src = xbt_dynar_get_ptr(path_src, index_src);
2672 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2675 int max_index_src = (path_src->used) - 1;
2676 int max_index_dst = (path_dst->used) - 1;
2678 int max_index = max(max_index_src, max_index_dst);
2681 route_extended_t e_route_bypass = NULL;
2683 for (max = 0; max <= max_index; max++) {
2684 for (i = 0; i < max; i++) {
2685 if (i <= max_index_src && max <= max_index_dst) {
2686 char *route_name = bprintf("%s#%s",
2687 (*(routing_component_t *)
2689 (path_src, i)))->name,
2690 (*(routing_component_t *)
2692 (path_dst, max)))->name);
2694 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2695 xbt_free(route_name);
2699 if (max <= max_index_src && i <= max_index_dst) {
2700 char *route_name = bprintf("%s#%s",
2701 (*(routing_component_t *)
2703 (path_src, max)))->name,
2704 (*(routing_component_t *)
2706 (path_dst, i)))->name);
2708 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2709 xbt_free(route_name);
2718 if (max <= max_index_src && max <= max_index_dst) {
2719 char *route_name = bprintf("%s#%s",
2720 (*(routing_component_t *)
2722 (path_src, max)))->name,
2723 (*(routing_component_t *)
2725 (path_dst, max)))->name);
2726 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2727 xbt_free(route_name);
2733 xbt_dynar_free(&path_src);
2734 xbt_dynar_free(&path_dst);
2736 route_extended_t new_e_route = NULL;
2738 if (e_route_bypass) {
2740 unsigned int cpt = 0;
2741 new_e_route = xbt_new0(s_route_extended_t, 1);
2742 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2743 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2744 new_e_route->generic_route.link_list =
2745 xbt_dynar_new(global_routing->size_of_link, NULL);
2746 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2747 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2754 /* ************************************************************************** */
2755 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2757 static route_extended_t
2758 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2759 void *data, int order)
2763 route_extended_t e_route, new_e_route;
2766 xbt_dynar_t links, links_id;
2768 new_e_route = xbt_new0(s_route_extended_t, 1);
2769 new_e_route->generic_route.link_list =
2770 xbt_dynar_new(global_routing->size_of_link, NULL);
2771 new_e_route->src_gateway = NULL;
2772 new_e_route->dst_gateway = NULL;
2774 xbt_assert0(hierarchy == SURF_ROUTING_BASE
2775 || hierarchy == SURF_ROUTING_RECURSIVE,
2776 "the hierarchy type is not defined");
2778 if (hierarchy == SURF_ROUTING_BASE) {
2780 route = (route_t) data;
2781 links = route->link_list;
2783 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2785 e_route = (route_extended_t) data;
2786 xbt_assert0(e_route->src_gateway
2787 && e_route->dst_gateway, "bad gateway, is null");
2788 links = e_route->generic_route.link_list;
2790 /* remeber not erase the gateway names */
2791 new_e_route->src_gateway = e_route->src_gateway;
2792 new_e_route->dst_gateway = e_route->dst_gateway;
2795 links_id = new_e_route->generic_route.link_list;
2797 xbt_dynar_foreach(links, cpt, link_name) {
2800 xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2803 xbt_dynar_push(links_id, &link);
2805 xbt_dynar_unshift(links_id, &link);
2807 THROW1(mismatch_error, 0, "Link %s not found", link_name);
2813 static void generic_free_route(route_t route)
2816 xbt_dynar_free(&(route->link_list));
2821 static void generic_free_extended_route(route_extended_t e_route)
2824 xbt_dynar_free(&(e_route->generic_route.link_list));
2825 if (e_route->src_gateway)
2826 xbt_free(e_route->src_gateway);
2827 if (e_route->dst_gateway)
2828 xbt_free(e_route->dst_gateway);
2833 static routing_component_t generic_as_exist(routing_component_t find_from,
2834 routing_component_t to_find)
2836 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2837 xbt_dict_cursor_t cursor = NULL;
2840 routing_component_t elem;
2841 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2842 if (to_find == elem || generic_as_exist(elem, to_find)) {
2852 static routing_component_t
2853 generic_autonomous_system_exist(routing_component_t rc, char *element)
2855 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2856 routing_component_t element_as, result, elem;
2857 xbt_dict_cursor_t cursor = NULL;
2859 element_as = ((network_element_info_t)
2860 xbt_dict_get_or_null
2861 (global_routing->where_network_elements,
2862 element))->rc_component;
2863 result = ((routing_component_t) - 1);
2864 if (element_as != rc)
2865 result = generic_as_exist(rc, element_as);
2869 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2870 found = !strcmp(elem->name, element);
2880 static routing_component_t
2881 generic_processing_units_exist(routing_component_t rc, char *element)
2883 routing_component_t element_as;
2884 element_as = ((network_element_info_t)
2885 xbt_dict_get_or_null
2886 (global_routing->where_network_elements,
2887 element))->rc_component;
2888 if (element_as == rc)
2890 return generic_as_exist(rc, element_as);
2893 static void generic_src_dst_check(routing_component_t rc, const char *src,
2897 routing_component_t src_as = ((network_element_info_t)
2898 xbt_dict_get_or_null
2899 (global_routing->where_network_elements,
2900 src))->rc_component;
2901 routing_component_t dst_as = ((network_element_info_t)
2902 xbt_dict_get_or_null
2903 (global_routing->where_network_elements,
2904 dst))->rc_component;
2906 xbt_assert3(src_as != NULL && dst_as != NULL,
2907 "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
2908 src, dst, rc->name);
2909 xbt_assert4(src_as == dst_as,
2910 "The src(%s in %s) and dst(%s in %s) are in differents AS",
2911 src, src_as->name, dst, dst_as->name);
2912 xbt_assert2(rc == dst_as,
2913 "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
2914 rc->name, dst_as->name);
2917 static void parse_Sconfig(void)
2920 DEBUG0("WARNING tag config not yet implemented.");
2923 static void routing_full_parse_Scluster(void)
2925 static int AX_ptr = 0;
2927 char *cluster_id = A_surfxml_cluster_id;
2928 char *cluster_prefix = A_surfxml_cluster_prefix;
2929 char *cluster_suffix = A_surfxml_cluster_suffix;
2930 char *cluster_radical = A_surfxml_cluster_radical;
2931 char *cluster_power = A_surfxml_cluster_power;
2932 char *cluster_bw = A_surfxml_cluster_bw;
2933 char *cluster_lat = A_surfxml_cluster_lat;
2934 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2935 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2936 char *host_id, *groups, *link_id;
2937 char *router_id, *link_router, *link_backbone, *route_src_dst;
2940 xbt_dynar_t radical_elements;
2941 xbt_dynar_t radical_ends;
2942 #ifndef HAVE_PCRE_LIB
2943 xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
2944 char *route_src, *route_dst;
2948 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
2949 static unsigned int surfxml_buffer_stack_stack[1024];
2951 surfxml_buffer_stack_stack[0] = 0;
2953 surfxml_bufferstack_push(1);
2955 SURFXML_BUFFER_SET(AS_id, cluster_id);
2956 #ifdef HAVE_PCRE_LIB
2957 SURFXML_BUFFER_SET(AS_routing, "RuleBased");
2958 DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
2960 SURFXML_BUFFER_SET(AS_routing, "Full");
2961 DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
2963 SURFXML_START_TAG(AS);
2965 radical_elements = xbt_str_split(cluster_radical, ",");
2966 xbt_dynar_foreach(radical_elements, iter, groups) {
2967 radical_ends = xbt_str_split(groups, "-");
2968 switch (xbt_dynar_length(radical_ends)) {
2970 surf_parse_get_int(&start,
2971 xbt_dynar_get_as(radical_ends, 0, char *));
2972 host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
2973 #ifndef HAVE_PCRE_LIB
2974 xbt_dynar_push_as(tab_elements_num, int, start);
2976 link_id = bprintf("%s_link_%d", cluster_id, start);
2978 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
2979 SURFXML_BUFFER_SET(host_id, host_id);
2980 SURFXML_BUFFER_SET(host_power, cluster_power);
2981 SURFXML_BUFFER_SET(host_availability, "1.0");
2982 SURFXML_BUFFER_SET(host_availability_file, "");
2983 A_surfxml_host_state = A_surfxml_host_state_ON;
2984 SURFXML_BUFFER_SET(host_state_file, "");
2985 SURFXML_START_TAG(host);
2986 SURFXML_END_TAG(host);
2988 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,
2989 cluster_bw, cluster_lat);
2990 SURFXML_BUFFER_SET(link_id, link_id);
2991 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
2992 SURFXML_BUFFER_SET(link_latency, cluster_lat);
2993 SURFXML_BUFFER_SET(link_bandwidth_file, "");
2994 SURFXML_BUFFER_SET(link_latency_file, "");
2995 A_surfxml_link_state = A_surfxml_link_state_ON;
2996 SURFXML_BUFFER_SET(link_state_file, "");
2997 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2998 SURFXML_START_TAG(link);
2999 SURFXML_END_TAG(link);
3005 surf_parse_get_int(&start,
3006 xbt_dynar_get_as(radical_ends, 0, char *));
3007 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3008 DEBUG2("Create hosts and links from %d to %d", start, end);
3009 for (i = start; i <= end; i++) {
3010 host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3011 #ifndef HAVE_PCRE_LIB
3012 xbt_dynar_push_as(tab_elements_num, int, i);
3014 link_id = bprintf("%s_link_%d", cluster_id, i);
3016 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3017 SURFXML_BUFFER_SET(host_id, host_id);
3018 SURFXML_BUFFER_SET(host_power, cluster_power);
3019 SURFXML_BUFFER_SET(host_availability, "1.0");
3020 SURFXML_BUFFER_SET(host_availability_file, "");
3021 A_surfxml_host_state = A_surfxml_host_state_ON;
3022 SURFXML_BUFFER_SET(host_state_file, "");
3023 SURFXML_START_TAG(host);
3024 SURFXML_END_TAG(host);
3026 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,
3027 cluster_bw, cluster_lat);
3028 SURFXML_BUFFER_SET(link_id, link_id);
3029 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3030 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3031 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3032 SURFXML_BUFFER_SET(link_latency_file, "");
3033 A_surfxml_link_state = A_surfxml_link_state_ON;
3034 SURFXML_BUFFER_SET(link_state_file, "");
3035 A_surfxml_link_sharing_policy =
3036 A_surfxml_link_sharing_policy_SHARED;
3037 SURFXML_START_TAG(link);
3038 SURFXML_END_TAG(link);
3043 DEBUG0("Malformed radical");
3046 xbt_dynar_free(&radical_ends);
3051 bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3053 link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3054 link_backbone = bprintf("%s_backbone", cluster_id);
3056 DEBUG1("<router id=\"%s\"/>", router_id);
3057 SURFXML_BUFFER_SET(router_id, router_id);;
3058 SURFXML_START_TAG(router);
3059 SURFXML_END_TAG(router);
3061 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_router,
3062 cluster_bw, cluster_lat);
3063 SURFXML_BUFFER_SET(link_id, link_router);
3064 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3065 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3066 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3067 SURFXML_BUFFER_SET(link_latency_file, "");
3068 A_surfxml_link_state = A_surfxml_link_state_ON;
3069 SURFXML_BUFFER_SET(link_state_file, "");
3070 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3071 SURFXML_START_TAG(link);
3072 SURFXML_END_TAG(link);
3074 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_backbone,
3075 cluster_bb_bw, cluster_bb_lat);
3076 SURFXML_BUFFER_SET(link_id, link_backbone);
3077 SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3078 SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3079 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3080 SURFXML_BUFFER_SET(link_latency_file, "");
3081 A_surfxml_link_state = A_surfxml_link_state_ON;
3082 SURFXML_BUFFER_SET(link_state_file, "");
3083 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3084 SURFXML_START_TAG(link);
3085 SURFXML_END_TAG(link);
3087 char *new_suffix = bprintf("%s", "");
3089 radical_elements = xbt_str_split(cluster_suffix, ".");
3090 xbt_dynar_foreach(radical_elements, iter, groups) {
3091 if (strcmp(groups, "")) {
3092 new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3095 route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3099 #ifdef HAVE_PCRE_LIB
3101 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">", route_src_dst, route_src_dst);
3102 SURFXML_BUFFER_SET(route_src, route_src_dst);
3103 SURFXML_BUFFER_SET(route_dst, route_src_dst);
3104 SURFXML_START_TAG(route);
3106 DEBUG1("<link:ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3107 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3108 SURFXML_START_TAG(link_ctn);
3109 SURFXML_END_TAG(link_ctn);
3111 DEBUG1("<link:ctn\tid=\"%s_backbone\"/>", cluster_id);
3112 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3113 SURFXML_START_TAG(link_ctn);
3114 SURFXML_END_TAG(link_ctn);
3116 DEBUG1("<link:ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3117 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3118 SURFXML_START_TAG(link_ctn);
3119 SURFXML_END_TAG(link_ctn);
3122 SURFXML_END_TAG(route);
3124 for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3125 for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3126 if (i == xbt_dynar_length(tab_elements_num)) {
3127 route_src = router_id;
3130 bprintf("%s%d%s", cluster_prefix,
3131 xbt_dynar_get_as(tab_elements_num, i, int),
3135 if (j == xbt_dynar_length(tab_elements_num)) {
3136 route_dst = router_id;
3139 bprintf("%s%d%s", cluster_prefix,
3140 xbt_dynar_get_as(tab_elements_num, j, int),
3144 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">", route_src, route_dst);
3145 SURFXML_BUFFER_SET(route_src, route_src);
3146 SURFXML_BUFFER_SET(route_dst, route_dst);
3147 SURFXML_START_TAG(route);
3149 if (i == xbt_dynar_length(tab_elements_num)) {
3150 route_src = link_router;
3153 bprintf("%s_link_%d", cluster_id,
3154 xbt_dynar_get_as(tab_elements_num, i, int));
3157 if (j == xbt_dynar_length(tab_elements_num)) {
3158 route_dst = link_router;
3161 bprintf("%s_link_%d", cluster_id,
3162 xbt_dynar_get_as(tab_elements_num, j, int));
3165 DEBUG1("<link:ctn\tid=\"%s\"/>", route_src);
3166 SURFXML_BUFFER_SET(link_ctn_id, route_src);
3167 SURFXML_START_TAG(link_ctn);
3168 SURFXML_END_TAG(link_ctn);
3170 DEBUG1("<link:ctn\tid=\"%s_backbone\"/>", cluster_id);
3171 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3172 SURFXML_START_TAG(link_ctn);
3173 SURFXML_END_TAG(link_ctn);
3175 DEBUG1("<link:ctn\tid=\"%s\"/>", route_dst);
3176 SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3177 SURFXML_START_TAG(link_ctn);
3178 SURFXML_END_TAG(link_ctn);
3181 SURFXML_END_TAG(route);
3184 xbt_dynar_free(&tab_elements_num);
3188 SURFXML_END_TAG(AS);
3191 surfxml_bufferstack_pop(1);
3195 * New methods to init the routing model component from the lua script
3199 * calling parse_S_AS_lua with lua values
3201 void routing_AS_init(const char *AS_id, const char *AS_routing)
3203 parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3207 * calling parse_E_AS_lua to fisnish the creation of routing component
3209 void routing_AS_end(const char *AS_id)
3211 parse_E_AS_lua((char *) AS_id);
3215 * add a host to the network element list
3218 void routing_add_host(const char *host_id)
3220 parse_S_host_lua((char *) host_id);
3224 * Set a new link on the actual list of link for a route or ASroute
3226 void routing_add_link(const char *link_id)
3228 parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3232 *Set the endpoints for a route
3234 void routing_set_route(const char *src_id, const char *dst_id)
3236 parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3240 * Store the route by calling parse_E_route_store_route
3242 void routing_store_route(void)
3244 parse_E_route_store_route();