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_parse_Scluster(void); /*cluster bypass */
59 static void routing_parse_Sconfig(void); /*config Tag */
60 static void routing_parse_Econfig(void); /*config Tag */
62 /* this lines are only for replace use like index in the model table */
67 SURF_MODEL_DIJKSTRACACHE,
75 /* must be finish with null and carefull if change de order */
76 struct s_model_type routing_models[] = { {"Full",
77 "Full routing data (fast, large memory requirements, fully expressive)",
83 "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
84 model_floyd_create, model_floyd_load, model_floyd_unload,
87 "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
88 model_dijkstra_create, model_dijkstra_both_load,
89 model_dijkstra_both_unload, model_dijkstra_both_end},
91 "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
92 model_dijkstracache_create, model_dijkstra_both_load,
93 model_dijkstra_both_unload, model_dijkstra_both_end},
94 {"none", "No routing (usable with Constant network only)",
95 model_none_create, model_none_load, model_none_unload, model_none_end},
97 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
98 model_rulebased_load, model_rulebased_unload, model_rulebased_end},
100 {NULL, NULL, NULL, NULL, NULL, NULL}
103 /* ************************************************************************** */
104 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
106 static void generic_set_processing_unit(routing_component_t rc,
108 static void generic_set_autonomous_system(routing_component_t rc,
110 static void generic_set_route(routing_component_t rc, const char *src,
111 const char *dst, route_t route);
112 static void generic_set_ASroute(routing_component_t rc, const char *src,
113 const char *dst, route_extended_t e_route);
114 static void generic_set_bypassroute(routing_component_t rc,
115 const char *src, const char *dst,
116 route_extended_t e_route);
117 int compare_routes(route_t route1, route_t route2);
119 /* ************************************************************************** */
120 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
122 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
123 static route_extended_t generic_get_bypassroute(routing_component_t rc,
127 /* ************************************************************************** */
128 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
130 static route_extended_t
131 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
132 void *data, int order);
133 static void generic_free_route(route_t route);
134 static void generic_free_extended_route(route_extended_t e_route);
135 static routing_component_t
136 generic_autonomous_system_exist(routing_component_t rc, char *element);
137 static routing_component_t
138 generic_processing_units_exist(routing_component_t rc, char *element);
139 static void generic_src_dst_check(routing_component_t rc, const char *src,
142 /* ************************************************************************** */
143 /* **************************** GLOBAL FUNCTIONS **************************** */
145 /* global parse functions */
146 static char *src = NULL; /* temporary store the source name of a route */
147 static char *dst = NULL; /* temporary store the destination name of a route */
148 static char *gw_src = NULL; /* temporary store the gateway source name of a route */
149 static char *gw_dst = NULL; /* temporary store the gateway destination name of a route */
150 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
152 * \brief Add a "host" to the network element list
154 static void parse_S_host(char *host_id)
156 network_element_info_t info = NULL;
157 if (current_routing->hierarchy == SURF_ROUTING_NULL)
158 current_routing->hierarchy = SURF_ROUTING_BASE;
159 xbt_assert1(!xbt_dict_get_or_null
160 (global_routing->where_network_elements, host_id),
161 "Reading a host, processing unit \"%s\" already exist",
163 xbt_assert1(current_routing->set_processing_unit,
164 "no defined method \"set_processing_unit\" in \"%s\"",
165 current_routing->name);
166 (*(current_routing->set_processing_unit)) (current_routing, host_id);
167 info = xbt_new0(s_network_element_info_t, 1);
168 info->rc_component = current_routing;
169 info->rc_type = SURF_NETWORK_ELEMENT_HOST;
170 xbt_dict_set(global_routing->where_network_elements, host_id,
171 (void *) info, NULL);
175 * \brief Add a host to the network element list from XML
177 static void parse_S_host_XML(void)
179 parse_S_host(A_surfxml_host_id);
183 * \brief Add a host to the network element list from lua script
185 static void parse_S_host_lua(char *host_id)
187 parse_S_host(host_id);
195 * \brief Add a "router" to the network element list
197 static void parse_S_router(void)
199 network_element_info_t info = NULL;
201 if (current_routing->hierarchy == SURF_ROUTING_NULL)
202 current_routing->hierarchy = SURF_ROUTING_BASE;
203 xbt_assert1(!xbt_dict_get_or_null
204 (global_routing->where_network_elements,
205 A_surfxml_router_id),
206 "Reading a router, processing unit \"%s\" already exist",
207 A_surfxml_router_id);
208 xbt_assert1(current_routing->set_processing_unit,
209 "no defined method \"set_processing_unit\" in \"%s\"",
210 current_routing->name);
211 (*(current_routing->set_processing_unit)) (current_routing,
212 A_surfxml_router_id);
213 info = xbt_new0(s_network_element_info_t, 1);
214 info->rc_component = current_routing;
215 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
216 xbt_dict_set(global_routing->where_network_elements, A_surfxml_router_id,
217 (void *) info, NULL);
219 TRACE_surf_host_declaration(A_surfxml_router_id, 0);
224 * \brief Set the endponints for a route
226 static void parse_S_route_new_and_endpoints(char *src_id, char *dst_id)
228 if (src != NULL && dst != NULL && link_list != NULL)
229 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
233 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0,
234 "Some limits are null in the route between \"%s\" and \"%s\"",
236 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
240 * \breif Set the endpoints for a route from XML
242 static void parse_S_route_new_and_endpoints_XML(void)
244 parse_S_route_new_and_endpoints(A_surfxml_route_src,
245 A_surfxml_route_dst);
249 * \breif Set the endpoints for a route from lua
251 static void parse_S_route_new_and_endpoints_lua(char *id_src, char *id_dst)
253 parse_S_route_new_and_endpoints(id_src, id_dst);
257 * \brief Set the endponints and gateways for a ASroute
259 static void parse_S_ASroute_new_and_endpoints(void)
261 if (src != NULL && dst != NULL && link_list != NULL)
262 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
263 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
264 src = A_surfxml_ASroute_src;
265 dst = A_surfxml_ASroute_dst;
266 gw_src = A_surfxml_ASroute_gw_src;
267 gw_dst = A_surfxml_ASroute_gw_dst;
268 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
269 || strlen(gw_dst) > 0,
270 "Some limits are null in the route between \"%s\" and \"%s\"",
272 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
276 * \brief Set the endponints for a bypassRoute
278 static void parse_S_bypassRoute_new_and_endpoints(void)
280 if (src != NULL && dst != NULL && link_list != NULL)
282 "Bypass Route between %s to %s can not be defined",
283 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
284 src = A_surfxml_bypassRoute_src;
285 dst = A_surfxml_bypassRoute_dst;
286 gw_src = A_surfxml_bypassRoute_gw_src;
287 gw_dst = A_surfxml_bypassRoute_gw_dst;
288 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
289 || strlen(gw_dst) > 0,
290 "Some limits are null in the route between \"%s\" and \"%s\"",
292 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
296 * \brief Set a new link on the actual list of link for a route or ASroute
298 static void parse_E_link_ctn_new_elem(char *link_id)
301 val = xbt_strdup(link_id);
302 xbt_dynar_push(link_list, &val);
306 * \brief Set a new link on the actual list of link for a route or ASroute from XML
309 static void parse_E_link_ctn_new_elem_XML(void)
311 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
312 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
313 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP)
314 parse_E_link_ctn_new_elem(bprintf("%s_UP",A_surfxml_link_ctn_id));
315 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN)
316 parse_E_link_ctn_new_elem(bprintf("%s_DOWN",A_surfxml_link_ctn_id));
320 * \brief Set a new link on the actual list of link for a route or ASroute from lua
322 static void parse_E_link_c_ctn_new_elem_lua(char *link_id)
324 parse_E_link_ctn_new_elem(link_id);
328 * \brief Store the route by calling the set_route function of the current routing component
330 static void parse_E_route_store_route(void)
332 route_t route = xbt_new0(s_route_t, 1);
333 route->link_list = link_list;
334 //xbt_assert1(generic_processing_units_exist(current_routing,src),"the \"%s\" processing units gateway does not exist",src);
335 //xbt_assert1(generic_processing_units_exist(current_routing,dst),"the \"%s\" processing units gateway does not exist",dst);
336 xbt_assert1(current_routing->set_route,
337 "no defined method \"set_route\" in \"%s\"",
338 current_routing->name);
339 (*(current_routing->set_route)) (current_routing, src, dst, route);
346 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
348 static void parse_E_ASroute_store_route(void)
350 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
351 e_route->generic_route.link_list = link_list;
352 e_route->src_gateway = xbt_strdup(gw_src);
353 e_route->dst_gateway = xbt_strdup(gw_dst);
354 // xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
355 // xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
356 // xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
357 // xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
358 xbt_assert1(current_routing->set_ASroute,
359 "no defined method \"set_ASroute\" in \"%s\"",
360 current_routing->name);
361 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
370 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
372 static void parse_E_bypassRoute_store_route(void)
374 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
375 e_route->generic_route.link_list = link_list;
376 e_route->src_gateway = xbt_strdup(gw_src);
377 e_route->dst_gateway = xbt_strdup(gw_dst);
378 // xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
379 // xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
380 // xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
381 // xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
382 xbt_assert1(current_routing->set_bypassroute,
383 "no defined method \"set_bypassroute\" in \"%s\"",
384 current_routing->name);
385 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
395 * \brief Make a new routing component
397 * make the new structure and
398 * set the parsing functions to allows parsing the part of the routing tree
400 static void parse_S_AS(char *AS_id, char *AS_routing)
402 routing_component_t new_routing;
403 model_type_t model = NULL;
404 char *wanted = AS_routing;
406 /* seach the routing model */
407 for (cpt = 0; routing_models[cpt].name; cpt++)
408 if (!strcmp(wanted, routing_models[cpt].name))
409 model = &routing_models[cpt];
410 /* if its not exist, error */
412 fprintf(stderr, "Routing model %s not found. Existing models:\n",
414 for (cpt = 0; routing_models[cpt].name; cpt++)
415 if (!strcmp(wanted, routing_models[cpt].name))
416 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
417 routing_models[cpt].desc);
421 /* make a new routing component */
422 new_routing = (routing_component_t) (*(model->create)) ();
423 new_routing->routing = model;
424 new_routing->hierarchy = SURF_ROUTING_NULL;
425 new_routing->name = xbt_strdup(AS_id);
426 new_routing->routing_sons = xbt_dict_new();
427 //INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
429 if (current_routing == NULL && global_routing->root == NULL) {
431 /* it is the first one */
432 new_routing->routing_father = NULL;
433 global_routing->root = new_routing;
435 } else if (current_routing != NULL && global_routing->root != NULL) {
437 xbt_assert1(!xbt_dict_get_or_null
438 (current_routing->routing_sons, AS_id),
439 "The AS \"%s\" already exist", AS_id);
440 /* it is a part of the tree */
441 new_routing->routing_father = current_routing;
442 /* set the father behavior */
443 if (current_routing->hierarchy == SURF_ROUTING_NULL)
444 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
445 /* add to the sons dictionary */
446 xbt_dict_set(current_routing->routing_sons, AS_id,
447 (void *) new_routing, NULL);
448 /* add to the father element list */
449 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
450 /* unload the prev parse rules */
451 (*(current_routing->routing->unload)) ();
454 THROW0(arg_error, 0, "All defined components must be belong to a AS");
456 /* set the new parse rules */
457 (*(new_routing->routing->load)) ();
458 /* set the new current component of the tree */
459 current_routing = new_routing;
463 * Detect the routing model type of the routing component from XML platforms
465 static void parse_S_AS_XML(void)
467 parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
471 * define the routing model type of routing component from lua script
473 static void parse_S_AS_lua(char *id, char *mode)
475 parse_S_AS(id, mode);
480 * \brief Finish the creation of a new routing component
482 * When you finish to read the routing component, other structures must be created.
483 * the "end" method allow to do that for any routing model type
485 static void parse_E_AS(char *AS_id)
488 if (current_routing == NULL) {
489 THROW1(arg_error, 0, "Close AS(%s), that never open", AS_id);
491 network_element_info_t info = NULL;
492 xbt_assert1(!xbt_dict_get_or_null
493 (global_routing->where_network_elements,
494 current_routing->name), "The AS \"%s\" already exist",
495 current_routing->name);
496 info = xbt_new0(s_network_element_info_t, 1);
497 info->rc_component = current_routing->routing_father;
498 info->rc_type = SURF_NETWORK_ELEMENT_AS;
499 xbt_dict_set(global_routing->where_network_elements,
500 current_routing->name, info, NULL);
501 (*(current_routing->routing->unload)) ();
502 (*(current_routing->routing->end)) ();
503 current_routing = current_routing->routing_father;
504 if (current_routing != NULL)
505 (*(current_routing->routing->load)) ();
510 * \brief Finish the creation of a new routing component from XML
512 static void parse_E_AS_XML(void)
514 parse_E_AS(A_surfxml_AS_id);
518 * \brief Finish the creation of a new routing component from lua
520 static void parse_E_AS_lua(char *id)
525 /* Aux Business methods */
528 * \brief Get the AS father and the first elements of the chain
530 * \param src the source host name
531 * \param dst the destination host name
533 * Get the common father of the to processing units, and the first different
534 * father in the chain
536 static xbt_dynar_t elements_father(const char *src, const char *dst)
539 xbt_assert0(src && dst, "bad parameters for \"elements_father\" method");
541 xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
543 routing_component_t src_as, dst_as;
544 int index_src, index_dst, index_father_src, index_father_dst;
545 xbt_dynar_t path_src = NULL;
546 xbt_dynar_t path_dst = NULL;
547 routing_component_t current = NULL;
548 routing_component_t *current_src = NULL;
549 routing_component_t *current_dst = NULL;
550 routing_component_t *father = NULL;
552 /* (1) find the as where the src and dst are located */
553 src_as = ((network_element_info_t)
554 xbt_dict_get_or_null(global_routing->where_network_elements,
556 dst_as = ((network_element_info_t)
557 xbt_dict_get_or_null(global_routing->where_network_elements,
561 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
564 /* (2) find the path to the root routing component */
565 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
567 while (current != NULL) {
568 xbt_dynar_push(path_src, ¤t);
569 current = current->routing_father;
571 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
573 while (current != NULL) {
574 xbt_dynar_push(path_dst, ¤t);
575 current = current->routing_father;
578 /* (3) find the common father */
579 index_src = (path_src->used) - 1;
580 index_dst = (path_dst->used) - 1;
581 current_src = xbt_dynar_get_ptr(path_src, index_src);
582 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
583 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
584 current_src = xbt_dynar_get_ptr(path_src, index_src);
585 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
591 current_src = xbt_dynar_get_ptr(path_src, index_src);
592 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
594 /* (4) they are not in the same routing component, make the path */
595 index_father_src = index_src + 1;
596 index_father_dst = index_dst + 1;
598 if (*current_src == *current_dst)
599 father = current_src;
601 father = xbt_dynar_get_ptr(path_src, index_father_src);
603 /* (5) result generation */
604 xbt_dynar_push(result, father); /* first same the father of src and dst */
605 xbt_dynar_push(result, current_src); /* second the first different father of src */
606 xbt_dynar_push(result, current_dst); /* three the first different father of dst */
608 xbt_dynar_free(&path_src);
609 xbt_dynar_free(&path_dst);
614 /* Global Business methods */
617 * \brief Recursive function for get_route
619 * \param src the source host name
620 * \param dst the destination host name
622 * This fuction is call by "get_route". It allow to walk through the
623 * routing components tree.
625 static route_extended_t _get_route(const char *src, const char *dst)
629 unsigned int cpt = 0;
631 DEBUG2("Solve route \"%s\" to \"%s\"", src, dst);
633 xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
635 route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
638 xbt_dynar_t elem_father_list = elements_father(src, dst);
640 routing_component_t common_father =
641 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
642 routing_component_t src_father =
643 xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
644 routing_component_t dst_father =
645 xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
647 e_route = xbt_new0(s_route_extended_t, 1);
648 e_route->src_gateway = NULL;
649 e_route->dst_gateway = NULL;
650 e_route->generic_route.link_list =
651 xbt_dynar_new(global_routing->size_of_link, NULL);
653 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
655 if (strcmp(src, dst)) {
657 (*(common_father->get_route)) (common_father, src, dst);
658 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
660 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
661 xbt_dynar_push(e_route->generic_route.link_list, &link);
663 generic_free_extended_route(e_route_cnt);
666 } else { /* SURF_ROUTING_RECURSIVE */
668 route_extended_t e_route_bypass = NULL;
670 if (common_father->get_bypass_route)
672 (*(common_father->get_bypass_route)) (common_father, src, dst);
675 e_route_cnt = e_route_bypass;
678 (*(common_father->get_route)) (common_father, src_father->name,
681 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
682 src_father->name, dst_father->name);
684 xbt_assert2((e_route_cnt->src_gateway == NULL) ==
685 (e_route_cnt->dst_gateway == NULL),
686 "bad gateway for route between \"%s\" and \"%s\"", src,
689 if (src != e_route_cnt->src_gateway) {
690 e_route_src = _get_route(src, e_route_cnt->src_gateway);
691 xbt_assert2(e_route_src, "no route between \"%s\" and \"%s\"", src,
692 e_route_cnt->src_gateway);
693 xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
694 xbt_dynar_push(e_route->generic_route.link_list, &link);
698 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
699 xbt_dynar_push(e_route->generic_route.link_list, &link);
702 if (e_route_cnt->dst_gateway != dst) {
703 e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
704 xbt_assert2(e_route_dst, "no route between \"%s\" and \"%s\"",
705 e_route_cnt->dst_gateway, dst);
706 xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
707 xbt_dynar_push(e_route->generic_route.link_list, &link);
711 e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
712 e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
714 generic_free_extended_route(e_route_src);
715 generic_free_extended_route(e_route_cnt);
716 generic_free_extended_route(e_route_dst);
719 xbt_dynar_free(&elem_father_list);
725 * \brief Generic method: find a route between hosts
727 * \param src the source host name
728 * \param dst the destination host name
730 * walk through the routing components tree and find a route between hosts
731 * by calling the differents "get_route" functions in each routing component
733 static xbt_dynar_t get_route(const char *src, const char *dst)
736 route_extended_t e_route;
737 xbt_dynar_t elem_father_list = elements_father(src, dst);
738 routing_component_t common_father =
739 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
741 if (strcmp(src, dst))
742 e_route = _get_route(src, dst);
744 e_route = (*(common_father->get_route)) (common_father, src, dst);
746 xbt_assert2(e_route, "no route between \"%s\" and \"%s\"", src, dst);
748 if (global_routing->last_route)
749 xbt_dynar_free(&(global_routing->last_route));
750 global_routing->last_route = e_route->generic_route.link_list;
752 if (e_route->src_gateway)
753 xbt_free(e_route->src_gateway);
754 if (e_route->dst_gateway)
755 xbt_free(e_route->dst_gateway);
758 xbt_dynar_free(&elem_father_list);
760 if (xbt_dynar_length(global_routing->last_route) == 0)
763 return global_routing->last_route;
767 * \brief Recursive function for finalize
769 * \param rc the source host name
771 * This fuction is call by "finalize". It allow to finalize the
772 * AS or routing components. It delete all the structures.
774 static void _finalize(routing_component_t rc)
777 xbt_dict_cursor_t cursor = NULL;
779 routing_component_t elem;
780 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
783 xbt_dict_t tmp_sons = rc->routing_sons;
784 char *tmp_name = rc->name;
785 xbt_dict_free(&tmp_sons);
787 xbt_assert1(rc->finalize, "no defined method \"finalize\" in \"%s\"",
788 current_routing->name);
789 (*(rc->finalize)) (rc);
794 * \brief Generic method: delete all the routing structures
796 * walk through the routing components tree and delete the structures
797 * by calling the differents "finalize" functions in each routing component
799 static void finalize(void)
801 /* delete recursibly all the tree */
802 _finalize(global_routing->root);
803 /* delete "where" dict */
804 xbt_dict_free(&(global_routing->where_network_elements));
805 /* delete last_route */
806 xbt_dynar_free(&(global_routing->last_route));
807 /* delete global routing structure */
808 xbt_free(global_routing);
811 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
813 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
815 //adding my one link routes
818 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
820 xbt_dynar_foreach(onelink_mine, cpt, link) {
821 xbt_dynar_push(ret, &link);
826 xbt_dict_cursor_t cursor = NULL;
827 routing_component_t rc_child;
828 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
829 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
831 xbt_dynar_foreach(onelink_child, cpt, link) {
832 xbt_dynar_push(ret, &link);
839 static xbt_dynar_t get_onelink_routes(void)
841 return recursive_get_onelink_routes(global_routing->root);
844 static e_surf_network_element_type_t get_network_element_type(const char
847 network_element_info_t rc = NULL;
848 rc = xbt_dict_get(global_routing->where_network_elements, name);
853 * \brief Generic method: create the global routing schema
855 * Make a global routing structure and set all the parsing functions.
857 void routing_model_create(size_t size_of_links, void *loopback)
860 /* config the uniq global routing */
861 global_routing = xbt_new0(s_routing_global_t, 1);
862 global_routing->where_network_elements = xbt_dict_new();
863 global_routing->root = NULL;
864 global_routing->get_route = get_route;
865 global_routing->get_onelink_routes = get_onelink_routes;
866 global_routing->get_network_element_type = get_network_element_type;
867 global_routing->finalize = finalize;
868 global_routing->loopback = loopback;
869 global_routing->size_of_link = size_of_links;
870 global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
872 /* no current routing at moment */
873 current_routing = NULL;
875 /* parse generic elements */
876 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
877 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
879 surfxml_add_callback(STag_surfxml_route_cb_list,
880 &parse_S_route_new_and_endpoints_XML);
881 surfxml_add_callback(STag_surfxml_ASroute_cb_list,
882 &parse_S_ASroute_new_and_endpoints);
883 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
884 &parse_S_bypassRoute_new_and_endpoints);
886 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
887 &parse_E_link_ctn_new_elem_XML);
889 surfxml_add_callback(ETag_surfxml_route_cb_list,
890 &parse_E_route_store_route);
891 surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
892 &parse_E_ASroute_store_route);
893 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
894 &parse_E_bypassRoute_store_route);
896 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
897 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
899 surfxml_add_callback(STag_surfxml_cluster_cb_list,
900 &routing_parse_Scluster);
902 surfxml_add_callback(STag_surfxml_config_cb_list,
903 &routing_parse_Sconfig);
904 surfxml_add_callback(ETag_surfxml_config_cb_list,
905 &routing_parse_Econfig);
908 /* ************************************************************************** */
909 /* *************************** FULL ROUTING ********************************* */
911 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
913 /* Routing model structure */
916 s_routing_component_t generic_routing;
917 xbt_dict_t parse_routes; /* store data during the parse process */
918 route_extended_t *routing_table;
919 } s_routing_component_full_t, *routing_component_full_t;
921 /* Business methods */
922 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
924 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
926 routing_component_full_t routing = (routing_component_full_t) rc;
927 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
928 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
929 char *k1, *d1, *k2, *d2;
930 xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
931 xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
932 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
933 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
936 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
938 route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
940 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
942 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
944 onelink_t onelink = xbt_new0(s_onelink_t, 1);
945 onelink->link_ptr = link;
946 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
947 onelink->src = xbt_strdup(k1);
948 onelink->dst = xbt_strdup(k2);
949 } else if (routing->generic_routing.hierarchy ==
950 SURF_ROUTING_RECURSIVE) {
951 onelink->src = xbt_strdup(route->src_gateway);
952 onelink->dst = xbt_strdup(route->dst_gateway);
954 xbt_dynar_push(ret, &onelink);
962 static route_extended_t full_get_route(routing_component_t rc,
963 const char *src, const char *dst)
965 xbt_assert1(rc && src
967 "Invalid params for \"get_route\" function at AS \"%s\"",
971 routing_component_full_t routing = (routing_component_full_t) rc;
972 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
974 generic_src_dst_check(rc, src, dst);
975 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
976 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
979 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
982 route_extended_t e_route = NULL;
983 route_extended_t new_e_route = NULL;
985 unsigned int cpt = 0;
987 e_route = TO_ROUTE_FULL(*src_id, *dst_id);
990 new_e_route = xbt_new0(s_route_extended_t, 1);
991 new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
992 new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
993 new_e_route->generic_route.link_list =
994 xbt_dynar_new(global_routing->size_of_link, NULL);
995 xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
996 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1002 static void full_finalize(routing_component_t rc)
1004 routing_component_full_t routing = (routing_component_full_t) rc;
1005 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1008 /* Delete routing table */
1009 for (i = 0; i < table_size; i++)
1010 for (j = 0; j < table_size; j++)
1011 generic_free_extended_route(TO_ROUTE_FULL(i, j));
1012 xbt_free(routing->routing_table);
1013 /* Delete bypass dict */
1014 xbt_dict_free(&rc->bypassRoutes);
1015 /* Delete index dict */
1016 xbt_dict_free(&rc->to_index);
1017 /* Delete structure */
1022 /* Creation routing model functions */
1024 static void *model_full_create(void)
1026 routing_component_full_t new_component =
1027 xbt_new0(s_routing_component_full_t, 1);
1028 new_component->generic_routing.set_processing_unit =
1029 generic_set_processing_unit;
1030 new_component->generic_routing.set_autonomous_system =
1031 generic_set_autonomous_system;
1032 new_component->generic_routing.set_route = generic_set_route;
1033 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1034 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1035 new_component->generic_routing.get_route = full_get_route;
1036 new_component->generic_routing.get_onelink_routes =
1037 full_get_onelink_routes;
1038 new_component->generic_routing.get_bypass_route =
1039 generic_get_bypassroute;
1040 new_component->generic_routing.finalize = full_finalize;
1041 new_component->generic_routing.to_index = xbt_dict_new();
1042 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1043 new_component->generic_routing.parse_routes = xbt_dict_new();
1044 return new_component;
1047 static void model_full_load(void)
1049 /* use "surfxml_add_callback" to add a parse function call */
1052 static void model_full_unload(void)
1054 /* use "surfxml_del_callback" to remove a parse function call */
1057 static void model_full_end(void)
1061 const char *sep = "#";
1065 route_extended_t e_route;
1068 xbt_dict_cursor_t cursor = NULL;
1069 xbt_dynar_t keys = NULL;
1071 /* set utils vars */
1072 routing_component_full_t routing =
1073 ((routing_component_full_t) current_routing);
1074 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1076 /* Create the routing table */
1077 routing->routing_table =
1078 xbt_new0(route_extended_t, table_size * table_size);
1080 /* Put the routes in position */
1081 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1082 keys = xbt_str_split_str(key, sep);
1083 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1084 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1085 TO_ROUTE_FULL(src_id, dst_id) =
1086 generic_new_extended_route(current_routing->hierarchy, data, 1);
1087 xbt_dynar_free(&keys);
1090 /* delete the parse table */
1091 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1092 route = (route_t) data;
1093 xbt_dynar_free(&(route->link_list));
1097 /* delete parse dict */
1098 xbt_dict_free(&(routing->generic_routing.parse_routes));
1100 /* Add the loopback if needed */
1101 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1102 for (i = 0; i < table_size; i++) {
1103 e_route = TO_ROUTE_FULL(i, i);
1105 e_route = xbt_new0(s_route_extended_t, 1);
1106 e_route->src_gateway = NULL;
1107 e_route->dst_gateway = NULL;
1108 e_route->generic_route.link_list =
1109 xbt_dynar_new(global_routing->size_of_link, NULL);
1110 xbt_dynar_push(e_route->generic_route.link_list,
1111 &global_routing->loopback);
1112 TO_ROUTE_FULL(i, i) = e_route;
1117 /* Shrink the dynar routes (save unused slots) */
1118 for (i = 0; i < table_size; i++)
1119 for (j = 0; j < table_size; j++)
1120 if (TO_ROUTE_FULL(i, j))
1121 xbt_dynar_shrink(TO_ROUTE_FULL(i, j)->generic_route.link_list, 0);
1124 /* ************************************************************************** */
1125 /* *************************** FLOYD ROUTING ******************************** */
1127 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
1128 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1129 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1131 /* Routing model structure */
1134 s_routing_component_t generic_routing;
1135 /* vars for calculate the floyd algorith. */
1136 int *predecessor_table;
1137 route_extended_t *link_table; /* char* -> int* */
1138 } s_routing_component_floyd_t, *routing_component_floyd_t;
1140 static route_extended_t floyd_get_route(routing_component_t rc,
1141 const char *src, const char *dst);
1143 /* Business methods */
1144 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1146 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1148 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1149 //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1150 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1151 char *k1, *d1, *k2, *d2;
1152 xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1153 xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1154 route_extended_t route = floyd_get_route(rc, k1, k2);
1156 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1158 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1160 onelink_t onelink = xbt_new0(s_onelink_t, 1);
1161 onelink->link_ptr = link;
1162 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1163 onelink->src = xbt_strdup(k1);
1164 onelink->dst = xbt_strdup(k2);
1165 } else if (routing->generic_routing.hierarchy ==
1166 SURF_ROUTING_RECURSIVE) {
1167 onelink->src = xbt_strdup(route->src_gateway);
1168 onelink->dst = xbt_strdup(route->dst_gateway);
1170 xbt_dynar_push(ret, &onelink);
1178 static route_extended_t floyd_get_route(routing_component_t rc,
1179 const char *src, const char *dst)
1181 xbt_assert1(rc && src
1183 "Invalid params for \"get_route\" function at AS \"%s\"",
1186 /* set utils vars */
1187 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1188 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1190 generic_src_dst_check(rc, src, dst);
1191 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1192 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1195 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1198 /* create a result route */
1199 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1200 new_e_route->generic_route.link_list =
1201 xbt_dynar_new(global_routing->size_of_link, NULL);
1202 new_e_route->src_gateway = NULL;
1203 new_e_route->dst_gateway = NULL;
1208 char *gw_src = NULL, *gw_dst =
1209 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1216 pred = TO_FLOYD_PRED(*src_id, pred);
1217 if (pred == -1) /* if no pred in route -> no route to host */
1219 xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1220 "Invalid link for the route between \"%s\" or \"%s\"", src,
1223 prev_gw_src = gw_src;
1224 prev_gw_dst = gw_dst;
1226 route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1227 gw_src = e_route->src_gateway;
1228 gw_dst = e_route->dst_gateway;
1233 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1234 && strcmp(gw_dst, prev_gw_src)) {
1235 xbt_dynar_t e_route_as_to_as =
1236 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1237 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1238 gw_dst, prev_gw_src);
1239 links = e_route_as_to_as;
1241 xbt_dynar_foreach(links, cpt, link) {
1242 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1248 links = e_route->generic_route.link_list;
1249 xbt_dynar_foreach(links, cpt, link) {
1250 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1254 } while (pred != *src_id);
1255 xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1256 *src_id, *dst_id, src, dst);
1258 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1259 new_e_route->src_gateway = xbt_strdup(gw_src);
1260 new_e_route->dst_gateway = xbt_strdup(first_gw);
1266 static void floyd_finalize(routing_component_t rc)
1268 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1272 table_size = xbt_dict_length(routing->generic_routing.to_index);
1273 /* Delete link_table */
1274 for (i = 0; i < table_size; i++)
1275 for (j = 0; j < table_size; j++)
1276 generic_free_extended_route(TO_FLOYD_LINK(i, j));
1277 xbt_free(routing->link_table);
1278 /* Delete bypass dict */
1279 xbt_dict_free(&routing->generic_routing.bypassRoutes);
1280 /* Delete index dict */
1281 xbt_dict_free(&(routing->generic_routing.to_index));
1282 /* Delete dictionary index dict, predecessor and links table */
1283 xbt_free(routing->predecessor_table);
1284 /* Delete structure */
1289 static void *model_floyd_create(void)
1291 routing_component_floyd_t new_component =
1292 xbt_new0(s_routing_component_floyd_t, 1);
1293 new_component->generic_routing.set_processing_unit =
1294 generic_set_processing_unit;
1295 new_component->generic_routing.set_autonomous_system =
1296 generic_set_autonomous_system;
1297 new_component->generic_routing.set_route = generic_set_route;
1298 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1299 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1300 new_component->generic_routing.get_route = floyd_get_route;
1301 new_component->generic_routing.get_onelink_routes =
1302 floyd_get_onelink_routes;
1303 new_component->generic_routing.get_bypass_route =
1304 generic_get_bypassroute;
1305 new_component->generic_routing.finalize = floyd_finalize;
1306 new_component->generic_routing.to_index = xbt_dict_new();
1307 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1308 new_component->generic_routing.parse_routes = xbt_dict_new();
1309 return new_component;
1312 static void model_floyd_load(void)
1314 /* use "surfxml_add_callback" to add a parse function call */
1317 static void model_floyd_unload(void)
1319 /* use "surfxml_del_callback" to remove a parse function call */
1322 static void model_floyd_end(void)
1325 routing_component_floyd_t routing =
1326 ((routing_component_floyd_t) current_routing);
1327 xbt_dict_cursor_t cursor = NULL;
1329 char *key, *data, *end;
1330 const char *sep = "#";
1333 unsigned int i, j, a, b, c;
1335 /* set the size of inicial table */
1336 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1338 /* Create Cost, Predecessor and Link tables */
1339 cost_table = xbt_new0(double, table_size * table_size); /* link cost from host to host */
1340 routing->predecessor_table = xbt_new0(int, table_size * table_size); /* predecessor host numbers */
1341 routing->link_table = xbt_new0(route_extended_t, table_size * table_size); /* actual link between src and dst */
1343 /* Initialize costs and predecessors */
1344 for (i = 0; i < table_size; i++)
1345 for (j = 0; j < table_size; j++) {
1346 TO_FLOYD_COST(i, j) = DBL_MAX;
1347 TO_FLOYD_PRED(i, j) = -1;
1348 TO_FLOYD_LINK(i, j) = NULL; /* fixed, missing in the previous version */
1351 /* Put the routes in position */
1352 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1353 keys = xbt_str_split_str(key, sep);
1354 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1355 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1356 TO_FLOYD_LINK(src_id, dst_id) =
1357 generic_new_extended_route(current_routing->hierarchy, data, 0);
1358 TO_FLOYD_PRED(src_id, dst_id) = src_id;
1360 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 */
1361 xbt_dynar_free(&keys);
1364 /* Add the loopback if needed */
1365 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1366 for (i = 0; i < table_size; i++) {
1367 route_extended_t e_route = TO_FLOYD_LINK(i, i);
1369 e_route = xbt_new0(s_route_extended_t, 1);
1370 e_route->src_gateway = NULL;
1371 e_route->dst_gateway = NULL;
1372 e_route->generic_route.link_list =
1373 xbt_dynar_new(global_routing->size_of_link, NULL);
1374 xbt_dynar_push(e_route->generic_route.link_list,
1375 &global_routing->loopback);
1376 TO_FLOYD_LINK(i, i) = e_route;
1377 TO_FLOYD_PRED(i, i) = i;
1378 TO_FLOYD_COST(i, i) = 1;
1382 /* Calculate path costs */
1383 for (c = 0; c < table_size; c++) {
1384 for (a = 0; a < table_size; a++) {
1385 for (b = 0; b < table_size; b++) {
1386 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1387 if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1388 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1389 TO_FLOYD_COST(a, b))) {
1390 TO_FLOYD_COST(a, b) =
1391 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1392 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1399 /* delete the parse table */
1400 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1401 route_t route = (route_t) data;
1402 xbt_dynar_free(&(route->link_list));
1406 /* delete parse dict */
1407 xbt_dict_free(&(routing->generic_routing.parse_routes));
1410 xbt_free(cost_table);
1413 /* ************************************************************************** */
1414 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1417 s_routing_component_t generic_routing;
1418 xbt_graph_t route_graph; /* xbt_graph */
1419 xbt_dict_t graph_node_map; /* map */
1420 xbt_dict_t route_cache; /* use in cache mode */
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->generic_routing.to_index, src);
1601 int *dst_id = xbt_dict_get_or_null(routing->generic_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->generic_routing.bypassRoutes);
1808 /* Delete index dict */
1809 xbt_dict_free(&(routing->generic_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->generic_routing.to_index = xbt_dict_new();
1836 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1837 new_component->generic_routing.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->generic_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->generic_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->generic_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 route_extended_t rulebased_get_route(routing_component_t rc,
2133 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2135 xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2136 routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2138 xbt_dict_cursor_t c1 = NULL;
2142 char *router = NULL;
2143 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2144 if (strstr (k1, "router")){
2149 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2152 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2153 route_extended_t route = rulebased_get_route (rc, router, k1);
2155 int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2156 if (number_of_links != 3) {
2157 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2161 xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2162 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2163 onelink->src = xbt_strdup (k1);
2164 onelink->dst = xbt_strdup (router);
2165 onelink->link_ptr = link_ptr;
2166 xbt_dynar_push (ret, &onelink);
2171 /* Business methods */
2172 static route_extended_t rulebased_get_route(routing_component_t rc,
2176 xbt_assert1(rc && src
2178 "Invalid params for \"get_route\" function at AS \"%s\"",
2181 /* set utils vars */
2182 routing_component_rulebased_t routing =
2183 (routing_component_rulebased_t) rc;
2185 int are_processing_units;
2186 xbt_dynar_t rule_list;
2187 if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2188 && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2189 are_processing_units = 1;
2190 rule_list = routing->list_route;
2191 } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2192 && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2194 are_processing_units = 0;
2195 rule_list = routing->list_ASroute;
2198 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
2203 int src_length = (int) strlen(src);
2204 int dst_length = (int) strlen(dst);
2206 xbt_dynar_t links_list =
2207 xbt_dynar_new(global_routing->size_of_link, NULL);
2209 rule_route_t ruleroute;
2211 int ovector_src[OVECCOUNT];
2212 int ovector_dst[OVECCOUNT];
2213 const char **list_src = NULL;
2214 const char **list_dst = NULL;
2215 xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2217 pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2218 ovector_src, OVECCOUNT);
2221 pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2222 ovector_dst, OVECCOUNT);
2224 xbt_assert1(!pcre_get_substring_list
2225 (src, ovector_src, rc_src, &list_src),
2226 "error solving substring list for src \"%s\"", src);
2227 xbt_assert1(!pcre_get_substring_list
2228 (dst, ovector_dst, rc_dst, &list_dst),
2229 "error solving substring list for src \"%s\"", dst);
2231 xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2232 char *new_link_name =
2233 remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2235 xbt_dict_get_or_null(surf_network_model->resource_set,
2238 xbt_dynar_push(links_list, &link);
2240 THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2241 xbt_free(new_link_name);
2245 if (rc_src >= 0 && rc_dst >= 0)
2249 route_extended_t new_e_route = NULL;
2250 if (rc_src >= 0 && rc_dst >= 0) {
2251 new_e_route = xbt_new0(s_route_extended_t, 1);
2252 new_e_route->generic_route.link_list = links_list;
2253 } else if (!strcmp(src, dst) && are_processing_units) {
2254 new_e_route = xbt_new0(s_route_extended_t, 1);
2255 xbt_dynar_push(links_list, &(global_routing->loopback));
2256 new_e_route->generic_route.link_list = links_list;
2258 xbt_dynar_free(&link_list);
2261 if (!are_processing_units && new_e_route) {
2262 rule_route_extended_t ruleroute_extended =
2263 (rule_route_extended_t) ruleroute;
2264 new_e_route->src_gateway =
2265 remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2267 new_e_route->dst_gateway =
2268 remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2273 pcre_free_substring_list(list_src);
2275 pcre_free_substring_list(list_dst);
2280 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2287 static void rulebased_finalize(routing_component_t rc)
2289 routing_component_rulebased_t routing =
2290 (routing_component_rulebased_t) rc;
2292 xbt_dict_free(&routing->dict_processing_units);
2293 xbt_dict_free(&routing->dict_autonomous_systems);
2294 xbt_dynar_free(&routing->list_route);
2295 xbt_dynar_free(&routing->list_ASroute);
2296 /* Delete structure */
2301 /* Creation routing model functions */
2302 static void *model_rulebased_create(void)
2304 routing_component_rulebased_t new_component =
2305 xbt_new0(s_routing_component_rulebased_t, 1);
2306 new_component->generic_routing.set_processing_unit =
2307 model_rulebased_set_processing_unit;
2308 new_component->generic_routing.set_autonomous_system =
2309 model_rulebased_set_autonomous_system;
2310 new_component->generic_routing.set_route = model_rulebased_set_route;
2311 new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2312 new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
2313 new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
2314 new_component->generic_routing.get_route = rulebased_get_route;
2315 new_component->generic_routing.get_bypass_route = generic_get_bypassroute; //rulebased_get_bypass_route;
2316 new_component->generic_routing.finalize = rulebased_finalize;
2317 /* initialization of internal structures */
2318 new_component->dict_processing_units = xbt_dict_new();
2319 new_component->dict_autonomous_systems = xbt_dict_new();
2320 new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2321 new_component->list_ASroute =
2322 xbt_dynar_new(sizeof(rule_route_extended_t),
2323 &rule_route_extended_free);
2324 return new_component;
2327 static void model_rulebased_load(void)
2329 /* use "surfxml_add_callback" to add a parse function call */
2332 static void model_rulebased_unload(void)
2334 /* use "surfxml_del_callback" to remove a parse function call */
2337 static void model_rulebased_end(void)
2341 #endif /* HAVE_PCRE_LIB */
2343 /* ************************************************************************** */
2344 /* ******************************* NO ROUTING ******************************* */
2346 /* Routing model structure */
2348 s_routing_component_t generic_routing;
2349 } s_routing_component_none_t, *routing_component_none_t;
2351 /* Business methods */
2352 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2357 static route_extended_t none_get_route(routing_component_t rc,
2358 const char *src, const char *dst)
2363 static route_extended_t none_get_bypass_route(routing_component_t rc,
2370 static void none_finalize(routing_component_t rc)
2375 static void none_set_processing_unit(routing_component_t rc,
2380 static void none_set_autonomous_system(routing_component_t rc,
2385 /* Creation routing model functions */
2386 static void *model_none_create(void)
2388 routing_component_none_t new_component =
2389 xbt_new0(s_routing_component_none_t, 1);
2390 new_component->generic_routing.set_processing_unit =
2391 none_set_processing_unit;
2392 new_component->generic_routing.set_autonomous_system =
2393 none_set_autonomous_system;
2394 new_component->generic_routing.set_route = NULL;
2395 new_component->generic_routing.set_ASroute = NULL;
2396 new_component->generic_routing.set_bypassroute = NULL;
2397 new_component->generic_routing.get_route = none_get_route;
2398 new_component->generic_routing.get_onelink_routes =
2399 none_get_onelink_routes;
2400 new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2401 new_component->generic_routing.finalize = none_finalize;
2402 return new_component;
2405 static void model_none_load(void)
2409 static void model_none_unload(void)
2413 static void model_none_end(void)
2417 /* ************************************************** */
2418 /* ********** PATERN FOR NEW ROUTING **************** */
2420 /* The minimal configuration of a new routing model need the next functions,
2421 * also you need to set at the start of the file, the new model in the model
2422 * list. Remember keep the null ending of the list.
2424 /*** Routing model structure ***/
2426 // s_routing_component_t generic_routing;
2427 // /* things that your routing model need */
2428 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2430 /*** Parse routing model functions ***/
2431 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2432 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2433 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2434 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2435 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2437 /*** Business methods ***/
2438 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2439 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2440 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2442 /*** Creation routing model functions ***/
2443 // static void* model_NEW_create(void) {
2444 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
2445 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2446 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2447 // new_component->generic_routing.set_route = model_NEW_set_route;
2448 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2449 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2450 // new_component->generic_routing.get_route = NEW_get_route;
2451 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2452 // new_component->generic_routing.finalize = NEW_finalize;
2453 // /* initialization of internal structures */
2454 // return new_component;
2455 // } /* mandatory */
2456 // static void model_NEW_load(void) {} /* mandatory */
2457 // static void model_NEW_unload(void) {} /* mandatory */
2458 // static void model_NEW_end(void) {} /* mandatory */
2460 /* ************************************************************************** */
2461 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2463 static void generic_set_processing_unit(routing_component_t rc,
2466 DEBUG1("Load process unit \"%s\"", name);
2467 int *id = xbt_new0(int, 1);
2468 xbt_dict_t _to_index;
2469 _to_index = current_routing->to_index;
2470 *id = xbt_dict_length(_to_index);
2471 xbt_dict_set(_to_index, name, id, xbt_free);
2474 static void generic_set_autonomous_system(routing_component_t rc,
2477 DEBUG1("Load Autonomous system \"%s\"", name);
2478 int *id = xbt_new0(int, 1);
2479 xbt_dict_t _to_index;
2480 _to_index = current_routing->to_index;
2481 *id = xbt_dict_length(_to_index);
2482 xbt_dict_set(_to_index, name, id, xbt_free);
2485 static void generic_set_route(routing_component_t rc, const char *src,
2486 const char *dst, route_t route)
2488 DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
2489 xbt_dict_t _parse_routes;
2490 xbt_dict_t _to_index;
2492 int *src_id, *dst_id;
2493 unsigned long nb_links = xbt_dynar_length(route->link_list);
2495 _to_index = current_routing->to_index;
2497 _parse_routes = current_routing->parse_routes;
2499 src_id = xbt_dict_get_or_null(_to_index, src);
2500 dst_id = xbt_dict_get_or_null(_to_index, dst);
2503 && dst_id, "Network elements %s or %s not found", src, dst);
2504 route_name = bprintf("%d#%d", *src_id, *dst_id);
2506 xbt_assert2(xbt_dynar_length(route->link_list) > 0,
2507 "Invalid count of links, must be greater than zero (%s,%s)",
2510 route_t route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2512 xbt_assert2(!xbt_dynar_compare(
2513 (void*)route->link_list,
2514 (void*)route_to_test->link_list,
2515 (int_f_cpvoid_cpvoid_t) strcmp),
2516 "The route between \"%s\" and \"%s\" already exists", src,dst);
2518 xbt_dict_set(_parse_routes, route_name, route, NULL);
2520 xbt_free(route_name);
2522 if (A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES) {
2524 route_t route_sym = xbt_new0(s_route_t, 1);
2525 route_sym->link_list = xbt_dynar_new(sizeof(char *),NULL);
2526 for(i=nb_links ; i>0 ; i--) {
2527 char *link_name = xbt_strdup(xbt_dynar_get_as(route->link_list, i-1, char *));
2528 xbt_dynar_push_as(route_sym->link_list ,char *, link_name);
2530 DEBUG2("Load Route from \"%s\" to \"%s\"", dst, src);
2531 route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d",*dst_id, *src_id));
2533 xbt_assert2(!xbt_dynar_compare(
2534 (void*)route_sym->link_list,
2535 (void*)route_to_test->link_list,
2536 (int_f_cpvoid_cpvoid_t) strcmp),
2537 "The route between \"%s\" and \"%s\" already exists", dst,src);
2539 xbt_dict_set(_parse_routes, bprintf("%d#%d",*dst_id, *src_id), route_sym, NULL);
2543 static void generic_set_ASroute(routing_component_t rc, const char *src,
2544 const char *dst, route_extended_t e_route)
2546 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2547 e_route->src_gateway, dst, e_route->dst_gateway);
2549 xbt_dict_t _parse_routes;
2550 xbt_dict_t _to_index;
2552 int *src_id, *dst_id;
2553 _to_index = current_routing->to_index;
2554 _parse_routes = current_routing->parse_routes;
2557 src_id = xbt_dict_get_or_null(_to_index, src);
2558 dst_id = xbt_dict_get_or_null(_to_index, dst);
2561 && dst_id, "Network elements %s or %s not found", src, dst);
2562 route_name = bprintf("%d#%d", *src_id, *dst_id);
2564 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2565 "Invalid count of links, must be greater than zero (%s,%s)",
2568 route_t route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2570 xbt_assert4(!xbt_dynar_compare(
2571 (void*) (&e_route->generic_route)->link_list,
2572 (void*) route_to_test->link_list,
2573 (int_f_cpvoid_cpvoid_t) strcmp),
2574 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
2575 src, e_route->src_gateway, dst, e_route->dst_gateway);
2577 xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2579 xbt_free(route_name);
2581 unsigned long nb_links = xbt_dynar_length(e_route->generic_route.link_list);
2582 if (A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES) {
2584 route_extended_t route_sym = xbt_new0(s_route_extended_t, 1);
2585 route_sym->generic_route.link_list = xbt_dynar_new(sizeof(char *),NULL);
2586 for(i=nb_links ; i>0 ; i--) {
2587 char *link_name = bprintf("%s",xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *));
2588 xbt_dynar_push_as(route_sym->generic_route.link_list ,char *, link_name);
2590 route_sym->src_gateway = xbt_new0( char,strlen(e_route->dst_gateway) );
2591 route_sym->src_gateway = bprintf("%s",e_route->dst_gateway);
2592 route_sym->dst_gateway = xbt_new0( char,strlen(e_route->src_gateway) );
2593 route_sym->dst_gateway = bprintf("%s",e_route->src_gateway);
2594 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",dst, route_sym->src_gateway,src,route_sym->dst_gateway);
2596 route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d", *dst_id, *src_id));
2598 xbt_assert4(!xbt_dynar_compare(
2599 (void*) (&route_sym->generic_route)->link_list,
2600 (void*) route_to_test->link_list,
2601 (int_f_cpvoid_cpvoid_t) strcmp),
2602 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
2603 dst, route_sym->src_gateway, src, route_sym->dst_gateway);
2605 xbt_dict_set(_parse_routes, bprintf("%d#%d", *dst_id, *src_id), route_sym, NULL);
2609 static void generic_set_bypassroute(routing_component_t rc,
2610 const char *src, const char *dst,
2611 route_extended_t e_route)
2613 DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2614 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2617 route_name = bprintf("%s#%s", src, dst);
2618 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2619 "Invalid count of links, must be greater than zero (%s,%s)",
2621 xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2622 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2623 src, e_route->src_gateway, dst, e_route->dst_gateway);
2625 route_extended_t new_e_route =
2626 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2627 xbt_dynar_free(&(e_route->generic_route.link_list));
2630 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2631 (void (*)(void *)) generic_free_extended_route);
2632 xbt_free(route_name);
2635 /* ************************************************************************** */
2636 /* *********************** GENERIC BUSINESS METHODS ************************* */
2638 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2640 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2643 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2647 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2648 routing_component_t src_as, dst_as;
2649 int index_src, index_dst;
2650 xbt_dynar_t path_src = NULL;
2651 xbt_dynar_t path_dst = NULL;
2652 routing_component_t current = NULL;
2653 routing_component_t *current_src = NULL;
2654 routing_component_t *current_dst = NULL;
2656 /* (1) find the as where the src and dst are located */
2657 src_as = ((network_element_info_t)
2658 xbt_dict_get_or_null(global_routing->where_network_elements,
2659 src))->rc_component;
2660 dst_as = ((network_element_info_t)
2661 xbt_dict_get_or_null(global_routing->where_network_elements,
2662 dst))->rc_component;
2665 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2668 /* (2) find the path to the root routing component */
2669 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2671 while (current != NULL) {
2672 xbt_dynar_push(path_src, ¤t);
2673 current = current->routing_father;
2675 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2677 while (current != NULL) {
2678 xbt_dynar_push(path_dst, ¤t);
2679 current = current->routing_father;
2682 /* (3) find the common father */
2683 index_src = (path_src->used) - 1;
2684 index_dst = (path_dst->used) - 1;
2685 current_src = xbt_dynar_get_ptr(path_src, index_src);
2686 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2687 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2688 routing_component_t *tmp_src, *tmp_dst;
2689 tmp_src = xbt_dynar_pop_ptr(path_src);
2690 tmp_dst = xbt_dynar_pop_ptr(path_dst);
2693 current_src = xbt_dynar_get_ptr(path_src, index_src);
2694 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2697 int max_index_src = (path_src->used) - 1;
2698 int max_index_dst = (path_dst->used) - 1;
2700 int max_index = max(max_index_src, max_index_dst);
2703 route_extended_t e_route_bypass = NULL;
2705 for (max = 0; max <= max_index; max++) {
2706 for (i = 0; i < max; i++) {
2707 if (i <= max_index_src && max <= max_index_dst) {
2708 char *route_name = bprintf("%s#%s",
2709 (*(routing_component_t *)
2711 (path_src, i)))->name,
2712 (*(routing_component_t *)
2714 (path_dst, max)))->name);
2716 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2717 xbt_free(route_name);
2721 if (max <= max_index_src && i <= max_index_dst) {
2722 char *route_name = bprintf("%s#%s",
2723 (*(routing_component_t *)
2725 (path_src, max)))->name,
2726 (*(routing_component_t *)
2728 (path_dst, i)))->name);
2730 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2731 xbt_free(route_name);
2740 if (max <= max_index_src && max <= max_index_dst) {
2741 char *route_name = bprintf("%s#%s",
2742 (*(routing_component_t *)
2744 (path_src, max)))->name,
2745 (*(routing_component_t *)
2747 (path_dst, max)))->name);
2748 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2749 xbt_free(route_name);
2755 xbt_dynar_free(&path_src);
2756 xbt_dynar_free(&path_dst);
2758 route_extended_t new_e_route = NULL;
2760 if (e_route_bypass) {
2762 unsigned int cpt = 0;
2763 new_e_route = xbt_new0(s_route_extended_t, 1);
2764 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2765 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2766 new_e_route->generic_route.link_list =
2767 xbt_dynar_new(global_routing->size_of_link, NULL);
2768 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2769 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2776 /* ************************************************************************** */
2777 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2779 static route_extended_t
2780 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2781 void *data, int order)
2785 route_extended_t e_route, new_e_route;
2788 xbt_dynar_t links = NULL, links_id = NULL;
2790 new_e_route = xbt_new0(s_route_extended_t, 1);
2791 new_e_route->generic_route.link_list =
2792 xbt_dynar_new(global_routing->size_of_link, NULL);
2793 new_e_route->src_gateway = NULL;
2794 new_e_route->dst_gateway = NULL;
2796 xbt_assert0(hierarchy == SURF_ROUTING_BASE
2797 || hierarchy == SURF_ROUTING_RECURSIVE,
2798 "the hierarchy type is not defined");
2800 if (hierarchy == SURF_ROUTING_BASE) {
2802 route = (route_t) data;
2803 links = route->link_list;
2805 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2807 e_route = (route_extended_t) data;
2808 xbt_assert0(e_route->src_gateway
2809 && e_route->dst_gateway, "bad gateway, is null");
2810 links = e_route->generic_route.link_list;
2812 /* remeber not erase the gateway names */
2813 new_e_route->src_gateway = e_route->src_gateway;
2814 new_e_route->dst_gateway = e_route->dst_gateway;
2817 links_id = new_e_route->generic_route.link_list;
2819 xbt_dynar_foreach(links, cpt, link_name) {
2822 xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2825 xbt_dynar_push(links_id, &link);
2827 xbt_dynar_unshift(links_id, &link);
2829 THROW1(mismatch_error, 0, "Link %s not found", link_name);
2835 static void generic_free_route(route_t route)
2838 xbt_dynar_free(&(route->link_list));
2843 static void generic_free_extended_route(route_extended_t e_route)
2846 xbt_dynar_free(&(e_route->generic_route.link_list));
2847 if (e_route->src_gateway)
2848 xbt_free(e_route->src_gateway);
2849 if (e_route->dst_gateway)
2850 xbt_free(e_route->dst_gateway);
2855 static routing_component_t generic_as_exist(routing_component_t find_from,
2856 routing_component_t to_find)
2858 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2859 xbt_dict_cursor_t cursor = NULL;
2862 routing_component_t elem;
2863 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2864 if (to_find == elem || generic_as_exist(elem, to_find)) {
2874 static routing_component_t
2875 generic_autonomous_system_exist(routing_component_t rc, char *element)
2877 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2878 routing_component_t element_as, result, elem;
2879 xbt_dict_cursor_t cursor = NULL;
2881 element_as = ((network_element_info_t)
2882 xbt_dict_get_or_null
2883 (global_routing->where_network_elements,
2884 element))->rc_component;
2885 result = ((routing_component_t) - 1);
2886 if (element_as != rc)
2887 result = generic_as_exist(rc, element_as);
2891 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2892 found = !strcmp(elem->name, element);
2902 static routing_component_t
2903 generic_processing_units_exist(routing_component_t rc, char *element)
2905 routing_component_t element_as;
2906 element_as = ((network_element_info_t)
2907 xbt_dict_get_or_null
2908 (global_routing->where_network_elements,
2909 element))->rc_component;
2910 if (element_as == rc)
2912 return generic_as_exist(rc, element_as);
2915 static void generic_src_dst_check(routing_component_t rc, const char *src,
2919 routing_component_t src_as = ((network_element_info_t)
2920 xbt_dict_get_or_null
2921 (global_routing->where_network_elements,
2922 src))->rc_component;
2923 routing_component_t dst_as = ((network_element_info_t)
2924 xbt_dict_get_or_null
2925 (global_routing->where_network_elements,
2926 dst))->rc_component;
2928 xbt_assert3(src_as != NULL && dst_as != NULL,
2929 "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
2930 src, dst, rc->name);
2931 xbt_assert4(src_as == dst_as,
2932 "The src(%s in %s) and dst(%s in %s) are in differents AS",
2933 src, src_as->name, dst, dst_as->name);
2934 xbt_assert2(rc == dst_as,
2935 "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
2936 rc->name, dst_as->name);
2939 static void routing_parse_Sconfig(void)
2942 DEBUG0("WARNING tag config not yet implemented.");
2943 DEBUG1("Configuration name = %s",A_surfxml_config_id);
2946 static void routing_parse_Econfig(void)
2949 xbt_dict_cursor_t cursor = NULL;
2952 xbt_dict_foreach(current_property_set, cursor, key, elem) {
2953 DEBUG2("property : %s = %s",key,elem);
2957 static void routing_parse_Scluster(void)
2959 static int AX_ptr = 0;
2961 char *cluster_id = A_surfxml_cluster_id;
2962 char *cluster_prefix = A_surfxml_cluster_prefix;
2963 char *cluster_suffix = A_surfxml_cluster_suffix;
2964 char *cluster_radical = A_surfxml_cluster_radical;
2965 char *cluster_power = A_surfxml_cluster_power;
2966 char *cluster_bw = A_surfxml_cluster_bw;
2967 char *cluster_lat = A_surfxml_cluster_lat;
2968 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2969 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2970 char *host_id, *groups, *link_id = NULL;
2971 char *router_id, *link_router, *link_backbone, *route_src_dst;
2974 xbt_dynar_t radical_elements;
2975 xbt_dynar_t radical_ends;
2976 int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
2977 int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
2979 #ifndef HAVE_PCRE_LIB
2980 xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
2981 char *route_src, *route_dst;
2985 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
2986 static unsigned int surfxml_buffer_stack_stack[1024];
2988 surfxml_buffer_stack_stack[0] = 0;
2990 surfxml_bufferstack_push(1);
2992 SURFXML_BUFFER_SET(AS_id, cluster_id);
2993 #ifdef HAVE_PCRE_LIB
2994 SURFXML_BUFFER_SET(AS_routing, "RuleBased");
2995 DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
2997 SURFXML_BUFFER_SET(AS_routing, "Full");
2998 DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
3000 SURFXML_START_TAG(AS);
3002 radical_elements = xbt_str_split(cluster_radical, ",");
3003 xbt_dynar_foreach(radical_elements, iter, groups) {
3004 radical_ends = xbt_str_split(groups, "-");
3005 switch (xbt_dynar_length(radical_ends)) {
3007 surf_parse_get_int(&start,
3008 xbt_dynar_get_as(radical_ends, 0, char *));
3009 host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
3010 #ifndef HAVE_PCRE_LIB
3011 xbt_dynar_push_as(tab_elements_num, int, start);
3013 link_id = bprintf("%s_link_%d", cluster_id, start);
3015 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3016 A_surfxml_host_state = A_surfxml_host_state_ON;
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 SURFXML_BUFFER_SET(host_state_file, "");
3022 SURFXML_START_TAG(host);
3023 SURFXML_END_TAG(host);
3025 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3026 A_surfxml_link_state = A_surfxml_link_state_ON;
3027 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3028 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3029 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3030 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3031 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3032 SURFXML_BUFFER_SET(link_id, link_id);
3033 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3034 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3035 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3036 SURFXML_BUFFER_SET(link_latency_file, "");
3037 SURFXML_BUFFER_SET(link_state_file, "");
3038 SURFXML_START_TAG(link);
3039 SURFXML_END_TAG(link);
3045 surf_parse_get_int(&start,
3046 xbt_dynar_get_as(radical_ends, 0, char *));
3047 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3048 DEBUG2("Create hosts and links from %d to %d", start, end);
3049 for (i = start; i <= end; i++) {
3050 host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3051 #ifndef HAVE_PCRE_LIB
3052 xbt_dynar_push_as(tab_elements_num, int, i);
3054 link_id = bprintf("%s_link_%d", cluster_id, i);
3056 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3057 A_surfxml_host_state = A_surfxml_host_state_ON;
3058 SURFXML_BUFFER_SET(host_id, host_id);
3059 SURFXML_BUFFER_SET(host_power, cluster_power);
3060 SURFXML_BUFFER_SET(host_availability, "1.0");
3061 SURFXML_BUFFER_SET(host_availability_file, "");
3062 SURFXML_BUFFER_SET(host_state_file, "");
3063 SURFXML_START_TAG(host);
3064 SURFXML_END_TAG(host);
3066 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3067 A_surfxml_link_state = A_surfxml_link_state_ON;
3068 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3069 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3070 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3071 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3072 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3073 SURFXML_BUFFER_SET(link_id, link_id);
3074 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3075 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3076 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3077 SURFXML_BUFFER_SET(link_latency_file, "");
3078 SURFXML_BUFFER_SET(link_state_file, "");
3079 SURFXML_START_TAG(link);
3080 SURFXML_END_TAG(link);
3085 DEBUG0("Malformed radical");
3088 xbt_dynar_free(&radical_ends);
3093 bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3095 link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3096 link_backbone = bprintf("%s_backbone", cluster_id);
3098 DEBUG1("<router id=\"%s\"/>", router_id);
3099 SURFXML_BUFFER_SET(router_id, router_id);;
3100 SURFXML_START_TAG(router);
3101 SURFXML_END_TAG(router);
3103 DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,cluster_bw, cluster_lat);
3104 A_surfxml_link_state = A_surfxml_link_state_ON;
3105 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3106 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3107 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3108 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3109 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3110 SURFXML_BUFFER_SET(link_id, link_router);
3111 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3112 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3113 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3114 SURFXML_BUFFER_SET(link_latency_file, "");
3115 SURFXML_BUFFER_SET(link_state_file, "");
3116 SURFXML_START_TAG(link);
3117 SURFXML_END_TAG(link);
3119 DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bw, cluster_lat);
3120 A_surfxml_link_state = A_surfxml_link_state_ON;
3121 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3122 if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
3123 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3124 SURFXML_BUFFER_SET(link_id, link_backbone);
3125 SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3126 SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3127 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3128 SURFXML_BUFFER_SET(link_latency_file, "");
3129 SURFXML_BUFFER_SET(link_state_file, "");
3130 SURFXML_START_TAG(link);
3131 SURFXML_END_TAG(link);
3133 char *new_suffix = bprintf("%s", "");
3135 radical_elements = xbt_str_split(cluster_suffix, ".");
3136 xbt_dynar_foreach(radical_elements, iter, groups) {
3137 if (strcmp(groups, "")) {
3138 new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3141 route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3145 #ifdef HAVE_PCRE_LIB
3147 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
3148 DEBUG0("symetrical=\"NO\">");
3149 SURFXML_BUFFER_SET(route_src, route_src_dst);
3150 SURFXML_BUFFER_SET(route_dst, route_src_dst);
3151 A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3152 SURFXML_START_TAG(route);
3154 DEBUG1("<link_ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3155 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3156 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3157 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3158 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3159 SURFXML_START_TAG(link_ctn);
3160 SURFXML_END_TAG(link_ctn);
3162 DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3163 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3164 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3165 SURFXML_START_TAG(link_ctn);
3166 SURFXML_END_TAG(link_ctn);
3168 DEBUG1("<link_ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3169 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3170 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3171 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3172 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3173 SURFXML_START_TAG(link_ctn);
3174 SURFXML_END_TAG(link_ctn);
3177 SURFXML_END_TAG(route);
3179 for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3180 for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3181 if (i == xbt_dynar_length(tab_elements_num)) {
3182 route_src = router_id;
3185 bprintf("%s%d%s", cluster_prefix,
3186 xbt_dynar_get_as(tab_elements_num, i, int),
3190 if (j == xbt_dynar_length(tab_elements_num)) {
3191 route_dst = router_id;
3194 bprintf("%s%d%s", cluster_prefix,
3195 xbt_dynar_get_as(tab_elements_num, j, int),
3199 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
3200 DEBUG0("symetrical=\"NO\">");
3201 SURFXML_BUFFER_SET(route_src, route_src);
3202 SURFXML_BUFFER_SET(route_dst, route_dst);
3203 A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3204 SURFXML_START_TAG(route);
3206 if (i == xbt_dynar_length(tab_elements_num)) {
3207 route_src = link_router;
3210 bprintf("%s_link_%d", cluster_id,
3211 xbt_dynar_get_as(tab_elements_num, i, int));
3214 if (j == xbt_dynar_length(tab_elements_num)) {
3215 route_dst = link_router;
3218 bprintf("%s_link_%d", cluster_id,
3219 xbt_dynar_get_as(tab_elements_num, j, int));
3222 DEBUG1("<link_ctn\tid=\"%s\"/>", route_src);
3223 SURFXML_BUFFER_SET(link_ctn_id, route_src);
3224 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3225 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3226 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3227 SURFXML_START_TAG(link_ctn);
3228 SURFXML_END_TAG(link_ctn);
3230 DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3231 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3232 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3233 SURFXML_START_TAG(link_ctn);
3234 SURFXML_END_TAG(link_ctn);
3236 DEBUG1("<link_ctn\tid=\"%s\"/>", route_dst);
3237 SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3238 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3239 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3240 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3241 SURFXML_START_TAG(link_ctn);
3242 SURFXML_END_TAG(link_ctn);
3245 SURFXML_END_TAG(route);
3248 xbt_dynar_free(&tab_elements_num);
3252 SURFXML_END_TAG(AS);
3255 surfxml_bufferstack_pop(1);
3259 * New methods to init the routing model component from the lua script
3263 * calling parse_S_AS_lua with lua values
3265 void routing_AS_init(const char *AS_id, const char *AS_routing)
3267 parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3271 * calling parse_E_AS_lua to fisnish the creation of routing component
3273 void routing_AS_end(const char *AS_id)
3275 parse_E_AS_lua((char *) AS_id);
3279 * add a host to the network element list
3282 void routing_add_host(const char *host_id)
3284 parse_S_host_lua((char *) host_id);
3288 * Set a new link on the actual list of link for a route or ASroute
3290 void routing_add_link(const char *link_id)
3292 parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3296 *Set the endpoints for a route
3298 void routing_set_route(const char *src_id, const char *dst_id)
3300 parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3304 * Store the route by calling parse_E_route_store_route
3306 void routing_store_route(void)
3308 parse_E_route_store_route();