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);
118 /* ************************************************************************** */
119 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
121 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
122 static route_extended_t generic_get_bypassroute(routing_component_t rc,
126 /* ************************************************************************** */
127 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
129 static route_extended_t
130 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
131 void *data, int order);
132 static void generic_free_route(route_t route);
133 static void generic_free_extended_route(route_extended_t e_route);
134 static routing_component_t
135 generic_autonomous_system_exist(routing_component_t rc, char *element);
136 static routing_component_t
137 generic_processing_units_exist(routing_component_t rc, char *element);
138 static void generic_src_dst_check(routing_component_t rc, const char *src,
141 /* ************************************************************************** */
142 /* **************************** GLOBAL FUNCTIONS **************************** */
144 /* global parse functions */
145 static char *src = NULL; /* temporary store the source name of a route */
146 static char *dst = NULL; /* temporary store the destination name of a route */
147 static char *gw_src = NULL; /* temporary store the gateway source name of a route */
148 static char *gw_dst = NULL; /* temporary store the gateway destination name of a route */
149 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
151 * \brief Add a "host" to the network element list
153 static void parse_S_host(char *host_id)
155 network_element_info_t info = NULL;
156 if (current_routing->hierarchy == SURF_ROUTING_NULL)
157 current_routing->hierarchy = SURF_ROUTING_BASE;
158 xbt_assert1(!xbt_dict_get_or_null
159 (global_routing->where_network_elements, host_id),
160 "Reading a host, processing unit \"%s\" already exist",
162 xbt_assert1(current_routing->set_processing_unit,
163 "no defined method \"set_processing_unit\" in \"%s\"",
164 current_routing->name);
165 (*(current_routing->set_processing_unit)) (current_routing, host_id);
166 info = xbt_new0(s_network_element_info_t, 1);
167 info->rc_component = current_routing;
168 info->rc_type = SURF_NETWORK_ELEMENT_HOST;
169 xbt_dict_set(global_routing->where_network_elements, host_id,
170 (void *) info, NULL);
174 * \brief Add a host to the network element list from XML
176 static void parse_S_host_XML(void)
178 parse_S_host(A_surfxml_host_id);
182 * \brief Add a host to the network element list from lua script
184 static void parse_S_host_lua(char *host_id)
186 parse_S_host(host_id);
191 * \brief Add a "router" to the network element list
193 static void parse_S_router(void)
195 network_element_info_t info = NULL;
197 if (current_routing->hierarchy == SURF_ROUTING_NULL)
198 current_routing->hierarchy = SURF_ROUTING_BASE;
199 xbt_assert1(!xbt_dict_get_or_null
200 (global_routing->where_network_elements,
201 A_surfxml_router_id),
202 "Reading a router, processing unit \"%s\" already exist",
203 A_surfxml_router_id);
204 xbt_assert1(current_routing->set_processing_unit,
205 "no defined method \"set_processing_unit\" in \"%s\"",
206 current_routing->name);
207 (*(current_routing->set_processing_unit)) (current_routing,
208 A_surfxml_router_id);
209 info = xbt_new0(s_network_element_info_t, 1);
210 info->rc_component = current_routing;
211 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
212 xbt_dict_set(global_routing->where_network_elements, A_surfxml_router_id,
213 (void *) info, NULL);
215 TRACE_surf_host_declaration(A_surfxml_router_id, 0);
220 * \brief Set the endponints for a route
222 static void parse_S_route_new_and_endpoints(char *src_id, char *dst_id)
224 if (src != NULL && dst != NULL && link_list != NULL)
225 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
229 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0,
230 "Some limits are null in the route between \"%s\" and \"%s\"",
232 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
236 * \breif Set the endpoints for a route from XML
238 static void parse_S_route_new_and_endpoints_XML(void)
240 parse_S_route_new_and_endpoints(A_surfxml_route_src,
241 A_surfxml_route_dst);
245 * \breif Set the endpoints for a route from lua
247 static void parse_S_route_new_and_endpoints_lua(char *id_src, char *id_dst)
249 parse_S_route_new_and_endpoints(id_src, id_dst);
253 * \brief Set the endponints and gateways for a ASroute
255 static void parse_S_ASroute_new_and_endpoints(void)
257 if (src != NULL && dst != NULL && link_list != NULL)
258 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
259 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
260 src = A_surfxml_ASroute_src;
261 dst = A_surfxml_ASroute_dst;
262 gw_src = A_surfxml_ASroute_gw_src;
263 gw_dst = A_surfxml_ASroute_gw_dst;
264 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
265 || strlen(gw_dst) > 0,
266 "Some limits are null in the route between \"%s\" and \"%s\"",
268 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
272 * \brief Set the endponints for a bypassRoute
274 static void parse_S_bypassRoute_new_and_endpoints(void)
276 if (src != NULL && dst != NULL && link_list != NULL)
278 "Bypass Route between %s to %s can not be defined",
279 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
280 src = A_surfxml_bypassRoute_src;
281 dst = A_surfxml_bypassRoute_dst;
282 gw_src = A_surfxml_bypassRoute_gw_src;
283 gw_dst = A_surfxml_bypassRoute_gw_dst;
284 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
285 || strlen(gw_dst) > 0,
286 "Some limits are null in the route between \"%s\" and \"%s\"",
288 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
292 * \brief Set a new link on the actual list of link for a route or ASroute
294 static void parse_E_link_ctn_new_elem(char *link_id)
297 val = xbt_strdup(link_id);
298 xbt_dynar_push(link_list, &val);
302 * \brief Set a new link on the actual list of link for a route or ASroute from XML
305 static void parse_E_link_ctn_new_elem_XML(void)
307 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
308 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
309 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP)
310 parse_E_link_ctn_new_elem(bprintf("%s_UP",A_surfxml_link_ctn_id));
311 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN)
312 parse_E_link_ctn_new_elem(bprintf("%s_DOWN",A_surfxml_link_ctn_id));
316 * \brief Set a new link on the actual list of link for a route or ASroute from lua
318 static void parse_E_link_c_ctn_new_elem_lua(char *link_id)
320 parse_E_link_ctn_new_elem(link_id);
324 * \brief Store the route by calling the set_route function of the current routing component
326 static void parse_E_route_store_route(void)
328 route_t route = xbt_new0(s_route_t, 1);
329 route->link_list = link_list;
330 xbt_assert1(current_routing->set_route,
331 "no defined method \"set_route\" in \"%s\"",
332 current_routing->name);
333 (*(current_routing->set_route)) (current_routing, src, dst, route);
340 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
342 static void parse_E_ASroute_store_route(void)
344 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
345 e_route->generic_route.link_list = link_list;
346 e_route->src_gateway = xbt_strdup(gw_src);
347 e_route->dst_gateway = xbt_strdup(gw_dst);
348 xbt_assert1(current_routing->set_ASroute,
349 "no defined method \"set_ASroute\" in \"%s\"",
350 current_routing->name);
351 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
360 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
362 static void parse_E_bypassRoute_store_route(void)
364 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
365 e_route->generic_route.link_list = link_list;
366 e_route->src_gateway = xbt_strdup(gw_src);
367 e_route->dst_gateway = xbt_strdup(gw_dst);
368 xbt_assert1(current_routing->set_bypassroute,
369 "no defined method \"set_bypassroute\" in \"%s\"",
370 current_routing->name);
371 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
381 * \brief Make a new routing component
383 * make the new structure and
384 * set the parsing functions to allows parsing the part of the routing tree
386 static void parse_S_AS(char *AS_id, char *AS_routing)
388 routing_component_t new_routing;
389 model_type_t model = NULL;
390 char *wanted = AS_routing;
392 /* seach the routing model */
393 for (cpt = 0; routing_models[cpt].name; cpt++)
394 if (!strcmp(wanted, routing_models[cpt].name))
395 model = &routing_models[cpt];
396 /* if its not exist, error */
398 fprintf(stderr, "Routing model %s not found. Existing models:\n",
400 for (cpt = 0; routing_models[cpt].name; cpt++)
401 if (!strcmp(wanted, routing_models[cpt].name))
402 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
403 routing_models[cpt].desc);
407 /* make a new routing component */
408 new_routing = (routing_component_t) (*(model->create)) ();
409 new_routing->routing = model;
410 new_routing->hierarchy = SURF_ROUTING_NULL;
411 new_routing->name = xbt_strdup(AS_id);
412 new_routing->routing_sons = xbt_dict_new();
414 if (current_routing == NULL && global_routing->root == NULL) {
416 /* it is the first one */
417 new_routing->routing_father = NULL;
418 global_routing->root = new_routing;
420 } else if (current_routing != NULL && global_routing->root != NULL) {
422 xbt_assert1(!xbt_dict_get_or_null
423 (current_routing->routing_sons, AS_id),
424 "The AS \"%s\" already exist", AS_id);
425 /* it is a part of the tree */
426 new_routing->routing_father = current_routing;
427 /* set the father behavior */
428 if (current_routing->hierarchy == SURF_ROUTING_NULL)
429 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
430 /* add to the sons dictionary */
431 xbt_dict_set(current_routing->routing_sons, AS_id,
432 (void *) new_routing, NULL);
433 /* add to the father element list */
434 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
435 /* unload the prev parse rules */
436 (*(current_routing->routing->unload)) ();
439 THROW0(arg_error, 0, "All defined components must be belong to a AS");
441 /* set the new parse rules */
442 (*(new_routing->routing->load)) ();
443 /* set the new current component of the tree */
444 current_routing = new_routing;
448 * Detect the routing model type of the routing component from XML platforms
450 static void parse_S_AS_XML(void)
452 parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
456 * define the routing model type of routing component from lua script
458 static void parse_S_AS_lua(char *id, char *mode)
460 parse_S_AS(id, mode);
465 * \brief Finish the creation of a new routing component
467 * When you finish to read the routing component, other structures must be created.
468 * the "end" method allow to do that for any routing model type
470 static void parse_E_AS(char *AS_id)
473 if (current_routing == NULL) {
474 THROW1(arg_error, 0, "Close AS(%s), that never open", AS_id);
476 network_element_info_t info = NULL;
477 xbt_assert1(!xbt_dict_get_or_null
478 (global_routing->where_network_elements,
479 current_routing->name), "The AS \"%s\" already exist",
480 current_routing->name);
481 info = xbt_new0(s_network_element_info_t, 1);
482 info->rc_component = current_routing->routing_father;
483 info->rc_type = SURF_NETWORK_ELEMENT_AS;
484 xbt_dict_set(global_routing->where_network_elements,
485 current_routing->name, info, NULL);
486 (*(current_routing->routing->unload)) ();
487 (*(current_routing->routing->end)) ();
488 current_routing = current_routing->routing_father;
489 if (current_routing != NULL)
490 (*(current_routing->routing->load)) ();
495 * \brief Finish the creation of a new routing component from XML
497 static void parse_E_AS_XML(void)
499 parse_E_AS(A_surfxml_AS_id);
503 * \brief Finish the creation of a new routing component from lua
505 static void parse_E_AS_lua(char *id)
510 /* Aux Business methods */
513 * \brief Get the AS father and the first elements of the chain
515 * \param src the source host name
516 * \param dst the destination host name
518 * Get the common father of the to processing units, and the first different
519 * father in the chain
521 static xbt_dynar_t elements_father(const char *src, const char *dst)
524 xbt_assert0(src && dst, "bad parameters for \"elements_father\" method");
526 xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
528 routing_component_t src_as, dst_as;
529 int index_src, index_dst, index_father_src, index_father_dst;
530 xbt_dynar_t path_src = NULL;
531 xbt_dynar_t path_dst = NULL;
532 routing_component_t current = NULL;
533 routing_component_t *current_src = NULL;
534 routing_component_t *current_dst = NULL;
535 routing_component_t *father = NULL;
537 /* (1) find the as where the src and dst are located */
538 src_as = ((network_element_info_t)
539 xbt_dict_get_or_null(global_routing->where_network_elements,
541 dst_as = ((network_element_info_t)
542 xbt_dict_get_or_null(global_routing->where_network_elements,
546 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
549 /* (2) find the path to the root routing component */
550 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
552 while (current != NULL) {
553 xbt_dynar_push(path_src, ¤t);
554 current = current->routing_father;
556 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
558 while (current != NULL) {
559 xbt_dynar_push(path_dst, ¤t);
560 current = current->routing_father;
563 /* (3) find the common father */
564 index_src = (path_src->used) - 1;
565 index_dst = (path_dst->used) - 1;
566 current_src = xbt_dynar_get_ptr(path_src, index_src);
567 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
568 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
569 current_src = xbt_dynar_get_ptr(path_src, index_src);
570 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
576 current_src = xbt_dynar_get_ptr(path_src, index_src);
577 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
579 /* (4) they are not in the same routing component, make the path */
580 index_father_src = index_src + 1;
581 index_father_dst = index_dst + 1;
583 if (*current_src == *current_dst)
584 father = current_src;
586 father = xbt_dynar_get_ptr(path_src, index_father_src);
588 /* (5) result generation */
589 xbt_dynar_push(result, father); /* first same the father of src and dst */
590 xbt_dynar_push(result, current_src); /* second the first different father of src */
591 xbt_dynar_push(result, current_dst); /* three the first different father of dst */
593 xbt_dynar_free(&path_src);
594 xbt_dynar_free(&path_dst);
599 /* Global Business methods */
602 * \brief Recursive function for get_route
604 * \param src the source host name
605 * \param dst the destination host name
607 * This fuction is call by "get_route". It allow to walk through the
608 * routing components tree.
610 static route_extended_t _get_route(const char *src, const char *dst)
614 unsigned int cpt = 0;
616 DEBUG2("Solve route \"%s\" to \"%s\"", src, dst);
618 xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
620 route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
623 xbt_dynar_t elem_father_list = elements_father(src, dst);
625 routing_component_t common_father =
626 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
627 routing_component_t src_father =
628 xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
629 routing_component_t dst_father =
630 xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
632 e_route = xbt_new0(s_route_extended_t, 1);
633 e_route->src_gateway = NULL;
634 e_route->dst_gateway = NULL;
635 e_route->generic_route.link_list =
636 xbt_dynar_new(global_routing->size_of_link, NULL);
638 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
640 if (strcmp(src, dst)) {
642 (*(common_father->get_route)) (common_father, src, dst);
643 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
645 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
646 xbt_dynar_push(e_route->generic_route.link_list, &link);
648 generic_free_extended_route(e_route_cnt);
651 } else { /* SURF_ROUTING_RECURSIVE */
653 route_extended_t e_route_bypass = NULL;
655 if (common_father->get_bypass_route)
657 (*(common_father->get_bypass_route)) (common_father, src, dst);
660 e_route_cnt = e_route_bypass;
663 (*(common_father->get_route)) (common_father, src_father->name,
666 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
667 src_father->name, dst_father->name);
669 xbt_assert2((e_route_cnt->src_gateway == NULL) ==
670 (e_route_cnt->dst_gateway == NULL),
671 "bad gateway for route between \"%s\" and \"%s\"", src,
674 if (src != e_route_cnt->src_gateway) {
675 e_route_src = _get_route(src, e_route_cnt->src_gateway);
676 xbt_assert2(e_route_src, "no route between \"%s\" and \"%s\"", src,
677 e_route_cnt->src_gateway);
678 xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
679 xbt_dynar_push(e_route->generic_route.link_list, &link);
683 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
684 xbt_dynar_push(e_route->generic_route.link_list, &link);
687 if (e_route_cnt->dst_gateway != dst) {
688 e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
689 xbt_assert2(e_route_dst, "no route between \"%s\" and \"%s\"",
690 e_route_cnt->dst_gateway, dst);
691 xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
692 xbt_dynar_push(e_route->generic_route.link_list, &link);
696 e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
697 e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
699 generic_free_extended_route(e_route_src);
700 generic_free_extended_route(e_route_cnt);
701 generic_free_extended_route(e_route_dst);
704 xbt_dynar_free(&elem_father_list);
710 * \brief Generic method: find a route between hosts
712 * \param src the source host name
713 * \param dst the destination host name
715 * walk through the routing components tree and find a route between hosts
716 * by calling the differents "get_route" functions in each routing component.
717 * No need to free the returned dynar. It will be freed at the next call.
719 static xbt_dynar_t get_route(const char *src, const char *dst)
722 route_extended_t e_route;
723 xbt_dynar_t elem_father_list = elements_father(src, dst);
724 routing_component_t common_father =
725 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
727 if (strcmp(src, dst))
728 e_route = _get_route(src, dst);
730 e_route = (*(common_father->get_route)) (common_father, src, dst);
732 xbt_assert2(e_route, "no route between \"%s\" and \"%s\"", src, dst);
734 if (global_routing->last_route)
735 xbt_dynar_free(&(global_routing->last_route));
736 global_routing->last_route = e_route->generic_route.link_list;
738 if (e_route->src_gateway)
739 xbt_free(e_route->src_gateway);
740 if (e_route->dst_gateway)
741 xbt_free(e_route->dst_gateway);
744 xbt_dynar_free(&elem_father_list);
746 if (xbt_dynar_length(global_routing->last_route) == 0)
749 return global_routing->last_route;
753 * \brief Generic method: find a route between hosts
755 * \param src the source host name
756 * \param dst the destination host name
758 * walk through the routing components tree and find a route between hosts
759 * by calling the differents "get_route" functions in each routing component.
760 * Leaves the caller the responsability to clean the returned dynar.
762 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
764 xbt_dynar_t d = get_route(src,dst);
765 global_routing->last_route = NULL;
770 * \brief Recursive function for finalize
772 * \param rc the source host name
774 * This fuction is call by "finalize". It allow to finalize the
775 * AS or routing components. It delete all the structures.
777 static void _finalize(routing_component_t rc)
780 xbt_dict_cursor_t cursor = NULL;
782 routing_component_t elem;
783 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
786 xbt_dict_t tmp_sons = rc->routing_sons;
787 char *tmp_name = rc->name;
788 xbt_dict_free(&tmp_sons);
790 xbt_assert1(rc->finalize, "no defined method \"finalize\" in \"%s\"",
791 current_routing->name);
792 (*(rc->finalize)) (rc);
797 * \brief Generic method: delete all the routing structures
799 * walk through the routing components tree and delete the structures
800 * by calling the differents "finalize" functions in each routing component
802 static void finalize(void)
804 /* delete recursibly all the tree */
805 _finalize(global_routing->root);
806 /* delete "where" dict */
807 xbt_dict_free(&(global_routing->where_network_elements));
808 /* delete last_route */
809 xbt_dynar_free(&(global_routing->last_route));
810 /* delete global routing structure */
811 xbt_free(global_routing);
814 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
816 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
818 //adding my one link routes
821 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
823 xbt_dynar_foreach(onelink_mine, cpt, link) {
824 xbt_dynar_push(ret, &link);
829 xbt_dict_cursor_t cursor = NULL;
830 routing_component_t rc_child;
831 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
832 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
834 xbt_dynar_foreach(onelink_child, cpt, link) {
835 xbt_dynar_push(ret, &link);
842 static xbt_dynar_t get_onelink_routes(void)
844 return recursive_get_onelink_routes(global_routing->root);
847 static e_surf_network_element_type_t get_network_element_type(const char
850 network_element_info_t rc = NULL;
851 rc = xbt_dict_get(global_routing->where_network_elements, name);
856 * \brief Generic method: create the global routing schema
858 * Make a global routing structure and set all the parsing functions.
860 void routing_model_create(size_t size_of_links, void *loopback)
863 /* config the uniq global routing */
864 global_routing = xbt_new0(s_routing_global_t, 1);
865 global_routing->where_network_elements = xbt_dict_new();
866 global_routing->root = NULL;
867 global_routing->get_route = get_route;
868 global_routing->get_route_no_cleanup = get_route_no_cleanup;
869 global_routing->get_onelink_routes = get_onelink_routes;
870 global_routing->get_network_element_type = get_network_element_type;
871 global_routing->finalize = finalize;
872 global_routing->loopback = loopback;
873 global_routing->size_of_link = size_of_links;
874 global_routing->last_route = NULL;
876 /* no current routing at moment */
877 current_routing = NULL;
879 /* parse generic elements */
880 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
881 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
883 surfxml_add_callback(STag_surfxml_route_cb_list,
884 &parse_S_route_new_and_endpoints_XML);
885 surfxml_add_callback(STag_surfxml_ASroute_cb_list,
886 &parse_S_ASroute_new_and_endpoints);
887 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
888 &parse_S_bypassRoute_new_and_endpoints);
890 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
891 &parse_E_link_ctn_new_elem_XML);
893 surfxml_add_callback(ETag_surfxml_route_cb_list,
894 &parse_E_route_store_route);
895 surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
896 &parse_E_ASroute_store_route);
897 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
898 &parse_E_bypassRoute_store_route);
900 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
901 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
903 surfxml_add_callback(STag_surfxml_cluster_cb_list,
904 &routing_parse_Scluster);
906 surfxml_add_callback(STag_surfxml_config_cb_list,
907 &routing_parse_Sconfig);
908 surfxml_add_callback(ETag_surfxml_config_cb_list,
909 &routing_parse_Econfig);
912 /* ************************************************************************** */
913 /* *************************** FULL ROUTING ********************************* */
915 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
917 /* Routing model structure */
920 s_routing_component_t generic_routing;
921 route_extended_t *routing_table;
922 } s_routing_component_full_t, *routing_component_full_t;
924 /* Business methods */
925 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
927 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
929 routing_component_full_t routing = (routing_component_full_t) rc;
930 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
931 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
932 char *k1, *d1, *k2, *d2;
933 xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
934 xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
935 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
936 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
939 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
941 route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
943 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
945 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
947 onelink_t onelink = xbt_new0(s_onelink_t, 1);
948 onelink->link_ptr = link;
949 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
950 onelink->src = xbt_strdup(k1);
951 onelink->dst = xbt_strdup(k2);
952 } else if (routing->generic_routing.hierarchy ==
953 SURF_ROUTING_RECURSIVE) {
954 onelink->src = xbt_strdup(route->src_gateway);
955 onelink->dst = xbt_strdup(route->dst_gateway);
957 xbt_dynar_push(ret, &onelink);
965 static route_extended_t full_get_route(routing_component_t rc,
966 const char *src, const char *dst)
968 xbt_assert1(rc && src
970 "Invalid params for \"get_route\" function at AS \"%s\"",
974 routing_component_full_t routing = (routing_component_full_t) rc;
975 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
977 generic_src_dst_check(rc, src, dst);
978 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
979 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
982 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
985 route_extended_t e_route = NULL;
986 route_extended_t new_e_route = NULL;
988 unsigned int cpt = 0;
990 e_route = TO_ROUTE_FULL(*src_id, *dst_id);
993 new_e_route = xbt_new0(s_route_extended_t, 1);
994 new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
995 new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
996 new_e_route->generic_route.link_list =
997 xbt_dynar_new(global_routing->size_of_link, NULL);
998 xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
999 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1005 static void full_finalize(routing_component_t rc)
1007 routing_component_full_t routing = (routing_component_full_t) rc;
1008 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1011 /* Delete routing table */
1012 for (i = 0; i < table_size; i++)
1013 for (j = 0; j < table_size; j++)
1014 generic_free_extended_route(TO_ROUTE_FULL(i, j));
1015 xbt_free(routing->routing_table);
1016 /* Delete bypass dict */
1017 xbt_dict_free(&rc->bypassRoutes);
1018 /* Delete index dict */
1019 xbt_dict_free(&rc->to_index);
1020 /* Delete structure */
1025 /* Creation routing model functions */
1027 static void *model_full_create(void)
1029 routing_component_full_t new_component =
1030 xbt_new0(s_routing_component_full_t, 1);
1031 new_component->generic_routing.set_processing_unit =
1032 generic_set_processing_unit;
1033 new_component->generic_routing.set_autonomous_system =
1034 generic_set_autonomous_system;
1035 new_component->generic_routing.set_route = generic_set_route;
1036 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1037 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1038 new_component->generic_routing.get_route = full_get_route;
1039 new_component->generic_routing.get_onelink_routes =
1040 full_get_onelink_routes;
1041 new_component->generic_routing.get_bypass_route =
1042 generic_get_bypassroute;
1043 new_component->generic_routing.finalize = full_finalize;
1044 new_component->generic_routing.to_index = xbt_dict_new();
1045 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1046 return new_component;
1049 static void model_full_load(void)
1051 /* use "surfxml_add_callback" to add a parse function call */
1054 static void model_full_unload(void)
1056 /* use "surfxml_del_callback" to remove a parse function call */
1059 static void model_full_end(void)
1062 route_extended_t e_route;
1064 /* set utils vars */
1065 routing_component_full_t routing =
1066 ((routing_component_full_t) current_routing);
1067 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1069 /* Create table if necessary */
1070 if(!routing->routing_table)
1072 routing->routing_table =
1073 xbt_new0(route_extended_t, table_size * table_size);
1076 /* Add the loopback if needed */
1077 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1078 for (i = 0; i < table_size; i++) {
1079 e_route = TO_ROUTE_FULL(i, i);
1081 e_route = xbt_new0(s_route_extended_t, 1);
1082 e_route->src_gateway = NULL;
1083 e_route->dst_gateway = NULL;
1084 e_route->generic_route.link_list =
1085 xbt_dynar_new(global_routing->size_of_link, NULL);
1086 xbt_dynar_push(e_route->generic_route.link_list,
1087 &global_routing->loopback);
1088 TO_ROUTE_FULL(i, i) = e_route;
1093 /* Shrink the dynar routes (save unused slots) */
1094 for (i = 0; i < table_size; i++)
1095 for (j = 0; j < table_size; j++)
1096 if (TO_ROUTE_FULL(i, j))
1097 xbt_dynar_shrink(TO_ROUTE_FULL(i, j)->generic_route.link_list, 0);
1100 /* ************************************************************************** */
1101 /* *************************** FLOYD ROUTING ******************************** */
1103 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
1104 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1105 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1107 /* Routing model structure */
1110 s_routing_component_t generic_routing;
1111 /* vars for calculate the floyd algorith. */
1112 int *predecessor_table;
1113 route_extended_t *link_table; /* char* -> int* */
1114 } s_routing_component_floyd_t, *routing_component_floyd_t;
1116 static route_extended_t floyd_get_route(routing_component_t rc,
1117 const char *src, const char *dst);
1119 /* Business methods */
1120 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1122 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1124 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1125 //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1126 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1127 char *k1, *d1, *k2, *d2;
1128 xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1129 xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1130 route_extended_t route = floyd_get_route(rc, k1, k2);
1132 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1134 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1136 onelink_t onelink = xbt_new0(s_onelink_t, 1);
1137 onelink->link_ptr = link;
1138 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1139 onelink->src = xbt_strdup(k1);
1140 onelink->dst = xbt_strdup(k2);
1141 } else if (routing->generic_routing.hierarchy ==
1142 SURF_ROUTING_RECURSIVE) {
1143 onelink->src = xbt_strdup(route->src_gateway);
1144 onelink->dst = xbt_strdup(route->dst_gateway);
1146 xbt_dynar_push(ret, &onelink);
1154 static route_extended_t floyd_get_route(routing_component_t rc,
1155 const char *src, const char *dst)
1157 xbt_assert1(rc && src
1159 "Invalid params for \"get_route\" function at AS \"%s\"",
1162 /* set utils vars */
1163 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1164 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1166 generic_src_dst_check(rc, src, dst);
1167 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1168 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1171 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1174 /* create a result route */
1175 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1176 new_e_route->generic_route.link_list =
1177 xbt_dynar_new(global_routing->size_of_link, NULL);
1178 new_e_route->src_gateway = NULL;
1179 new_e_route->dst_gateway = NULL;
1184 char *gw_src = NULL, *gw_dst =
1185 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1192 pred = TO_FLOYD_PRED(*src_id, pred);
1193 if (pred == -1) /* if no pred in route -> no route to host */
1195 xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1196 "Invalid link for the route between \"%s\" or \"%s\"", src,
1199 prev_gw_src = gw_src;
1200 prev_gw_dst = gw_dst;
1202 route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1203 gw_src = e_route->src_gateway;
1204 gw_dst = e_route->dst_gateway;
1209 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1210 && strcmp(gw_dst, prev_gw_src)) {
1211 xbt_dynar_t e_route_as_to_as =
1212 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1213 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1214 gw_dst, prev_gw_src);
1215 links = e_route_as_to_as;
1217 xbt_dynar_foreach(links, cpt, link) {
1218 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1224 links = e_route->generic_route.link_list;
1225 xbt_dynar_foreach(links, cpt, link) {
1226 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1230 } while (pred != *src_id);
1231 xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1232 *src_id, *dst_id, src, dst);
1234 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1235 new_e_route->src_gateway = xbt_strdup(gw_src);
1236 new_e_route->dst_gateway = xbt_strdup(first_gw);
1242 static void floyd_finalize(routing_component_t rc)
1244 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1248 table_size = xbt_dict_length(routing->generic_routing.to_index);
1249 /* Delete link_table */
1250 for (i = 0; i < table_size; i++)
1251 for (j = 0; j < table_size; j++)
1252 generic_free_extended_route(TO_FLOYD_LINK(i, j));
1253 xbt_free(routing->link_table);
1254 /* Delete bypass dict */
1255 xbt_dict_free(&routing->generic_routing.bypassRoutes);
1256 /* Delete index dict */
1257 xbt_dict_free(&(routing->generic_routing.to_index));
1258 /* Delete dictionary index dict, predecessor and links table */
1259 xbt_free(routing->predecessor_table);
1260 /* Delete structure */
1265 static void *model_floyd_create(void)
1267 routing_component_floyd_t new_component =
1268 xbt_new0(s_routing_component_floyd_t, 1);
1269 new_component->generic_routing.set_processing_unit =
1270 generic_set_processing_unit;
1271 new_component->generic_routing.set_autonomous_system =
1272 generic_set_autonomous_system;
1273 new_component->generic_routing.set_route = generic_set_route;
1274 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1275 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1276 new_component->generic_routing.get_route = floyd_get_route;
1277 new_component->generic_routing.get_onelink_routes =
1278 floyd_get_onelink_routes;
1279 new_component->generic_routing.get_bypass_route =
1280 generic_get_bypassroute;
1281 new_component->generic_routing.finalize = floyd_finalize;
1282 new_component->generic_routing.to_index = xbt_dict_new();
1283 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1284 new_component->generic_routing.parse_routes = xbt_dict_new();
1285 return new_component;
1288 static void model_floyd_load(void)
1290 /* use "surfxml_add_callback" to add a parse function call */
1293 static void model_floyd_unload(void)
1295 /* use "surfxml_del_callback" to remove a parse function call */
1298 static void model_floyd_end(void)
1301 routing_component_floyd_t routing =
1302 ((routing_component_floyd_t) current_routing);
1303 xbt_dict_cursor_t cursor = NULL;
1305 char *key, *data, *end;
1306 const char *sep = "#";
1309 unsigned int i, j, a, b, c;
1311 /* set the size of inicial table */
1312 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1314 /* Create Cost, Predecessor and Link tables */
1315 cost_table = xbt_new0(double, table_size * table_size); /* link cost from host to host */
1316 routing->predecessor_table = xbt_new0(int, table_size * table_size); /* predecessor host numbers */
1317 routing->link_table = xbt_new0(route_extended_t, table_size * table_size); /* actual link between src and dst */
1319 /* Initialize costs and predecessors */
1320 for (i = 0; i < table_size; i++)
1321 for (j = 0; j < table_size; j++) {
1322 TO_FLOYD_COST(i, j) = DBL_MAX;
1323 TO_FLOYD_PRED(i, j) = -1;
1324 TO_FLOYD_LINK(i, j) = NULL; /* fixed, missing in the previous version */
1327 /* Put the routes in position */
1328 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1329 keys = xbt_str_split_str(key, sep);
1330 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1331 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1332 TO_FLOYD_LINK(src_id, dst_id) =
1333 generic_new_extended_route(current_routing->hierarchy, data, 0);
1334 TO_FLOYD_PRED(src_id, dst_id) = src_id;
1336 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 */
1337 xbt_dynar_free(&keys);
1340 /* Add the loopback if needed */
1341 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1342 for (i = 0; i < table_size; i++) {
1343 route_extended_t e_route = TO_FLOYD_LINK(i, i);
1345 e_route = xbt_new0(s_route_extended_t, 1);
1346 e_route->src_gateway = NULL;
1347 e_route->dst_gateway = NULL;
1348 e_route->generic_route.link_list =
1349 xbt_dynar_new(global_routing->size_of_link, NULL);
1350 xbt_dynar_push(e_route->generic_route.link_list,
1351 &global_routing->loopback);
1352 TO_FLOYD_LINK(i, i) = e_route;
1353 TO_FLOYD_PRED(i, i) = i;
1354 TO_FLOYD_COST(i, i) = 1;
1358 /* Calculate path costs */
1359 for (c = 0; c < table_size; c++) {
1360 for (a = 0; a < table_size; a++) {
1361 for (b = 0; b < table_size; b++) {
1362 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1363 if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1364 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1365 TO_FLOYD_COST(a, b))) {
1366 TO_FLOYD_COST(a, b) =
1367 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1368 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1375 /* delete the parse table */
1376 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1377 route_t route = (route_t) data;
1378 xbt_dynar_free(&(route->link_list));
1382 /* delete parse dict */
1383 xbt_dict_free(&(routing->generic_routing.parse_routes));
1386 xbt_free(cost_table);
1389 /* ************************************************************************** */
1390 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1393 s_routing_component_t generic_routing;
1394 xbt_graph_t route_graph; /* xbt_graph */
1395 xbt_dict_t graph_node_map; /* map */
1396 xbt_dict_t route_cache; /* use in cache mode */
1398 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1401 typedef struct graph_node_data {
1403 int graph_id; /* used for caching internal graph id's */
1404 } s_graph_node_data_t, *graph_node_data_t;
1406 typedef struct graph_node_map_element {
1408 } s_graph_node_map_element_t, *graph_node_map_element_t;
1410 typedef struct route_cache_element {
1413 } s_route_cache_element_t, *route_cache_element_t;
1415 /* Free functions */
1417 static void route_cache_elem_free(void *e)
1419 route_cache_element_t elm = (route_cache_element_t) e;
1421 xbt_free(elm->pred_arr);
1426 static void graph_node_map_elem_free(void *e)
1428 graph_node_map_element_t elm = (graph_node_map_element_t) e;
1434 static void graph_edge_data_free(void *e)
1436 route_extended_t e_route = (route_extended_t) e;
1438 xbt_dynar_free(&(e_route->generic_route.link_list));
1439 if (e_route->src_gateway)
1440 xbt_free(e_route->src_gateway);
1441 if (e_route->dst_gateway)
1442 xbt_free(e_route->dst_gateway);
1447 /* Utility functions */
1449 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1450 int id, int graph_id)
1452 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1453 xbt_node_t node = NULL;
1454 graph_node_data_t data = NULL;
1455 graph_node_map_element_t elm = NULL;
1457 data = xbt_new0(struct graph_node_data, 1);
1459 data->graph_id = graph_id;
1460 node = xbt_graph_new_node(routing->route_graph, data);
1462 elm = xbt_new0(struct graph_node_map_element, 1);
1464 xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1465 (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1470 static graph_node_map_element_t
1471 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1473 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1474 graph_node_map_element_t elm = (graph_node_map_element_t)
1475 xbt_dict_get_or_null_ext(routing->graph_node_map,
1483 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1484 int dst_id, route_extended_t e_route)
1486 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1488 xbt_node_t src = NULL;
1489 xbt_node_t dst = NULL;
1490 graph_node_map_element_t src_elm = (graph_node_map_element_t)
1491 xbt_dict_get_or_null_ext(routing->graph_node_map,
1494 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1495 xbt_dict_get_or_null_ext(routing->graph_node_map,
1500 src = src_elm->node;
1503 dst = dst_elm->node;
1505 /* add nodes if they don't exist in the graph */
1506 if (src_id == dst_id && src == NULL && dst == NULL) {
1507 src = route_graph_new_node(rc, src_id, -1);
1511 src = route_graph_new_node(rc, src_id, -1);
1514 dst = route_graph_new_node(rc, dst_id, -1);
1518 /* add link as edge to graph */
1519 xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1522 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1524 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1526 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1528 xbt_node_t node = NULL;
1529 unsigned int cursor2;
1530 xbt_dynar_foreach(nodes, cursor2, node) {
1531 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1532 xbt_edge_t edge = NULL;
1533 unsigned int cursor;
1536 xbt_dynar_foreach(out_edges, cursor, edge) {
1537 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1538 if (other_node == node) {
1545 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1546 e_route->src_gateway = NULL;
1547 e_route->dst_gateway = NULL;
1548 e_route->generic_route.link_list =
1549 xbt_dynar_new(global_routing->size_of_link, NULL);
1550 xbt_dynar_push(e_route->generic_route.link_list,
1551 &global_routing->loopback);
1552 xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1557 /* Business methods */
1558 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
1560 xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
1563 static route_extended_t dijkstra_get_route(routing_component_t rc,
1567 xbt_assert1(rc && src
1569 "Invalid params for \"get_route\" function at AS \"%s\"",
1572 /* set utils vars */
1573 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1575 generic_src_dst_check(rc, src, dst);
1576 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1577 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1580 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1583 /* create a result route */
1584 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1585 new_e_route->generic_route.link_list =
1586 xbt_dynar_new(global_routing->size_of_link, NULL);
1587 new_e_route->src_gateway = NULL;
1588 new_e_route->dst_gateway = NULL;
1590 int *pred_arr = NULL;
1591 int src_node_id = 0;
1592 int dst_node_id = 0;
1595 route_extended_t e_route;
1599 xbt_dynar_t links = NULL;
1600 route_cache_element_t elm = NULL;
1601 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1603 /* Use the graph_node id mapping set to quickly find the nodes */
1604 graph_node_map_element_t src_elm =
1605 graph_node_map_search(routing, *src_id);
1606 graph_node_map_element_t dst_elm =
1607 graph_node_map_search(routing, *dst_id);
1608 xbt_assert2(src_elm != NULL
1609 && dst_elm != NULL, "src %d or dst %d does not exist",
1611 src_node_id = ((graph_node_data_t)
1612 xbt_graph_node_get_data(src_elm->node))->graph_id;
1613 dst_node_id = ((graph_node_data_t)
1614 xbt_graph_node_get_data(dst_elm->node))->graph_id;
1616 /* if the src and dst are the same *//* fixed, missing in the previous version */
1617 if (src_node_id == dst_node_id) {
1619 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1620 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1622 xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1624 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1627 e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1629 links = e_route->generic_route.link_list;
1630 xbt_dynar_foreach(links, cpt, link) {
1631 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1637 if (routing->cached) {
1638 /*check if there is a cached predecessor list avail */
1639 elm = (route_cache_element_t)
1640 xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
1644 if (elm) { /* cached mode and cache hit */
1645 pred_arr = elm->pred_arr;
1646 } else { /* not cached mode or cache miss */
1647 double *cost_arr = NULL;
1648 xbt_heap_t pqueue = NULL;
1651 int nr_nodes = xbt_dynar_length(nodes);
1652 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
1653 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
1654 pqueue = xbt_heap_new(nr_nodes, xbt_free);
1657 cost_arr[src_node_id] = 0.0;
1659 for (i = 0; i < nr_nodes; i++) {
1660 if (i != src_node_id) {
1661 cost_arr[i] = DBL_MAX;
1666 /* initialize priority queue */
1667 nodeid = xbt_new0(int, 1);
1669 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1673 /* apply dijkstra using the indexes from the graph's node array */
1674 while (xbt_heap_size(pqueue) > 0) {
1675 int *v_id = xbt_heap_pop(pqueue);
1676 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1677 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1678 xbt_edge_t edge = NULL;
1679 unsigned int cursor;
1681 xbt_dynar_foreach(out_edges, cursor, edge) {
1682 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1683 graph_node_data_t data = xbt_graph_node_get_data(u_node);
1684 int u_id = data->graph_id;
1685 route_extended_t tmp_e_route =
1686 (route_extended_t) xbt_graph_edge_get_data(edge);
1687 int cost_v_u = (tmp_e_route->generic_route.link_list)->used; /* count of links, old model assume 1 */
1689 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1690 pred_arr[u_id] = *v_id;
1691 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1692 nodeid = xbt_new0(int, 1);
1694 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1698 /* free item popped from pqueue */
1703 xbt_heap_free(pqueue);
1706 /* compose route path with links */
1707 char *gw_src = NULL, *gw_dst =
1708 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1710 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1711 xbt_node_t node_pred_v =
1712 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1713 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1715 xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1717 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1720 prev_gw_src = gw_src;
1721 prev_gw_dst = gw_dst;
1723 e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1724 gw_src = e_route->src_gateway;
1725 gw_dst = e_route->dst_gateway;
1727 if (v == dst_node_id)
1730 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
1731 && strcmp(gw_dst, prev_gw_src)) {
1732 xbt_dynar_t e_route_as_to_as =
1733 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1734 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1735 gw_dst, prev_gw_src);
1736 links = e_route_as_to_as;
1738 xbt_dynar_foreach(links, cpt, link) {
1739 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1745 links = e_route->generic_route.link_list;
1746 xbt_dynar_foreach(links, cpt, link) {
1747 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1752 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1753 new_e_route->src_gateway = xbt_strdup(gw_src);
1754 new_e_route->dst_gateway = xbt_strdup(first_gw);
1757 if (routing->cached && elm == NULL) {
1758 /* add to predecessor list of the current src-host to cache */
1759 elm = xbt_new0(struct route_cache_element, 1);
1760 elm->pred_arr = pred_arr;
1762 xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
1763 (xbt_set_elm_t) elm, &route_cache_elem_free);
1766 if (!routing->cached)
1772 static void dijkstra_finalize(routing_component_t rc)
1774 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1777 xbt_graph_free_graph(routing->route_graph, &xbt_free,
1778 &graph_edge_data_free, &xbt_free);
1779 xbt_dict_free(&routing->graph_node_map);
1780 if (routing->cached)
1781 xbt_dict_free(&routing->route_cache);
1782 /* Delete bypass dict */
1783 xbt_dict_free(&routing->generic_routing.bypassRoutes);
1784 /* Delete index dict */
1785 xbt_dict_free(&(routing->generic_routing.to_index));
1786 /* Delete structure */
1791 /* Creation routing model functions */
1793 static void *model_dijkstra_both_create(int cached)
1795 routing_component_dijkstra_t new_component =
1796 xbt_new0(s_routing_component_dijkstra_t, 1);
1797 new_component->generic_routing.set_processing_unit =
1798 generic_set_processing_unit;
1799 new_component->generic_routing.set_autonomous_system =
1800 generic_set_autonomous_system;
1801 new_component->generic_routing.set_route = generic_set_route;
1802 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1803 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1804 new_component->generic_routing.get_route = dijkstra_get_route;
1805 new_component->generic_routing.get_onelink_routes =
1806 dijkstra_get_onelink_routes;
1807 new_component->generic_routing.get_bypass_route =
1808 generic_get_bypassroute;
1809 new_component->generic_routing.finalize = dijkstra_finalize;
1810 new_component->cached = cached;
1811 new_component->generic_routing.to_index = xbt_dict_new();
1812 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1813 new_component->generic_routing.parse_routes = xbt_dict_new();
1814 return new_component;
1817 static void *model_dijkstra_create(void)
1819 return model_dijkstra_both_create(0);
1822 static void *model_dijkstracache_create(void)
1824 return model_dijkstra_both_create(1);
1827 static void model_dijkstra_both_load(void)
1829 /* use "surfxml_add_callback" to add a parse function call */
1832 static void model_dijkstra_both_unload(void)
1834 /* use "surfxml_del_callback" to remove a parse function call */
1837 static void model_dijkstra_both_end(void)
1839 routing_component_dijkstra_t routing =
1840 (routing_component_dijkstra_t) current_routing;
1841 xbt_dict_cursor_t cursor = NULL;
1842 char *key, *data, *end;
1843 const char *sep = "#";
1845 xbt_node_t node = NULL;
1846 unsigned int cursor2;
1847 xbt_dynar_t nodes = NULL;
1851 /* Create the topology graph */
1852 routing->route_graph = xbt_graph_new_graph(1, NULL);
1853 routing->graph_node_map = xbt_dict_new();
1855 if (routing->cached)
1856 routing->route_cache = xbt_dict_new();
1858 /* Put the routes in position */
1859 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1860 keys = xbt_str_split_str(key, sep);
1861 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1862 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1863 route_extended_t e_route =
1864 generic_new_extended_route(current_routing->hierarchy, data, 0);
1865 route_new_dijkstra(routing, src_id, dst_id, e_route);
1866 xbt_dynar_free(&keys);
1869 /* delete the parse table */
1870 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1871 route = (route_t) data;
1872 xbt_dynar_free(&(route->link_list));
1876 /* delete parse dict */
1877 xbt_dict_free(&(routing->generic_routing.parse_routes));
1879 /* Add the loopback if needed */
1880 if (current_routing->hierarchy == SURF_ROUTING_BASE)
1881 add_loopback_dijkstra(routing);
1883 /* initialize graph indexes in nodes after graph has been built */
1884 nodes = xbt_graph_get_nodes(routing->route_graph);
1886 xbt_dynar_foreach(nodes, cursor2, node) {
1887 graph_node_data_t data = xbt_graph_node_get_data(node);
1888 data->graph_id = cursor2;
1893 #ifdef HAVE_PCRE_LIB
1894 /* ************************************************** */
1895 /* ************** RULE-BASED ROUTING **************** */
1897 /* Routing model structure */
1900 s_routing_component_t generic_routing;
1901 xbt_dict_t dict_processing_units;
1902 xbt_dict_t dict_autonomous_systems;
1903 xbt_dynar_t list_route;
1904 xbt_dynar_t list_ASroute;
1905 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
1907 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
1908 typedef struct s_rule_route_extended s_rule_route_extended_t,
1909 *rule_route_extended_t;
1911 struct s_rule_route {
1912 xbt_dynar_t re_str_link; // dynar of char*
1917 struct s_rule_route_extended {
1918 s_rule_route_t generic_rule_route;
1919 char *re_src_gateway;
1920 char *re_dst_gateway;
1923 static void rule_route_free(void *e)
1925 rule_route_t *elem = (rule_route_t *) (e);
1927 xbt_dynar_free(&(*elem)->re_str_link);
1928 pcre_free((*elem)->re_src);
1929 pcre_free((*elem)->re_dst);
1935 static void rule_route_extended_free(void *e)
1937 rule_route_extended_t *elem = (rule_route_extended_t *) e;
1939 xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
1940 pcre_free((*elem)->generic_rule_route.re_src);
1941 pcre_free((*elem)->generic_rule_route.re_dst);
1942 xbt_free((*elem)->re_src_gateway);
1943 xbt_free((*elem)->re_dst_gateway);
1948 /* Parse routing model functions */
1950 static void model_rulebased_set_processing_unit(routing_component_t rc,
1953 routing_component_rulebased_t routing =
1954 (routing_component_rulebased_t) rc;
1955 xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
1958 static void model_rulebased_set_autonomous_system(routing_component_t rc,
1961 routing_component_rulebased_t routing =
1962 (routing_component_rulebased_t) rc;
1963 xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
1967 static void model_rulebased_set_route(routing_component_t rc,
1968 const char *src, const char *dst,
1971 routing_component_rulebased_t routing =
1972 (routing_component_rulebased_t) rc;
1973 rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
1976 ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
1977 xbt_assert3(ruleroute->re_src,
1978 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
1979 erroffset, src, error);
1980 ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
1981 xbt_assert3(ruleroute->re_src,
1982 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
1983 erroffset, dst, error);
1984 ruleroute->re_str_link = route->link_list;
1985 xbt_dynar_push(routing->list_route, &ruleroute);
1989 static void model_rulebased_set_ASroute(routing_component_t rc,
1990 const char *src, const char *dst,
1991 route_extended_t route)
1993 routing_component_rulebased_t routing =
1994 (routing_component_rulebased_t) rc;
1995 rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
1998 ruleroute_e->generic_rule_route.re_src =
1999 pcre_compile(src, 0, &error, &erroffset, NULL);
2000 xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2001 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2002 erroffset, src, error);
2003 ruleroute_e->generic_rule_route.re_dst =
2004 pcre_compile(dst, 0, &error, &erroffset, NULL);
2005 xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2006 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2007 erroffset, dst, error);
2008 ruleroute_e->generic_rule_route.re_str_link =
2009 route->generic_route.link_list;
2010 ruleroute_e->re_src_gateway = route->src_gateway;
2011 ruleroute_e->re_dst_gateway = route->dst_gateway;
2012 xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2013 xbt_free(route->src_gateway);
2014 xbt_free(route->dst_gateway);
2018 static void model_rulebased_set_bypassroute(routing_component_t rc,
2021 route_extended_t e_route)
2023 xbt_die("bypass routing not supported for Route-Based model");
2026 #define BUFFER_SIZE 4096 /* result buffer size */
2027 #define OVECCOUNT 30 /* should be a multiple of 3 */
2029 static char *remplace(char *value, const char **src_list, int src_size,
2030 const char **dst_list, int dst_size)
2033 char result_result[BUFFER_SIZE];
2034 int i_result_buffer;
2035 int value_length = (int) strlen(value);
2039 i_result_buffer = 0;
2041 if (value[i] == '$') {
2045 int number_length = 0;
2046 while ('0' <= value[i + number_length]
2047 && value[i + number_length] <= '9') {
2050 xbt_assert2(number_length != 0,
2051 "bad string parameter, no number indication, at offset: %d (\"%s\")",
2055 number = atoi(value + i);
2056 i = i + number_length;
2057 xbt_assert2(i + 2 < value_length,
2058 "bad string parameter, too few chars, at offset: %d (\"%s\")",
2061 // solve the indication
2062 const char **param_list;
2064 if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2065 param_list = src_list;
2066 param_size = src_size;
2067 } else if (value[i] == 'd' && value[i + 1] == 's'
2068 && value[i + 2] == 't') {
2069 param_list = dst_list;
2070 param_size = dst_size;
2073 "bad string parameter, support only \"src\" and \"dst\", at offset: %d (\"%s\")",
2078 xbt_assert4(param_size >= number,
2079 "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2080 i, value, param_size, number);
2082 const char *param = param_list[number];
2083 int size = strlen(param);
2085 for (cp = 0; cp < size; cp++) {
2086 result_result[i_result_buffer] = param[cp];
2088 if (i_result_buffer >= BUFFER_SIZE)
2092 result_result[i_result_buffer] = value[i];
2097 } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2099 xbt_assert2(i_result_buffer < BUFFER_SIZE,
2100 "solving string \"%s\", small buffer size (%d)", value,
2102 result_result[i_result_buffer] = 0;
2103 return xbt_strdup(result_result);
2106 static route_extended_t rulebased_get_route(routing_component_t rc,
2109 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2111 xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2112 routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2114 xbt_dict_cursor_t c1 = NULL;
2118 char *router = NULL;
2119 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2120 if (strstr (k1, "router")){
2125 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2128 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2129 route_extended_t route = rulebased_get_route (rc, router, k1);
2131 int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2132 if (number_of_links != 3) {
2133 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2137 xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2138 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2139 onelink->src = xbt_strdup (k1);
2140 onelink->dst = xbt_strdup (router);
2141 onelink->link_ptr = link_ptr;
2142 xbt_dynar_push (ret, &onelink);
2147 /* Business methods */
2148 static route_extended_t rulebased_get_route(routing_component_t rc,
2152 xbt_assert1(rc && src
2154 "Invalid params for \"get_route\" function at AS \"%s\"",
2157 /* set utils vars */
2158 routing_component_rulebased_t routing =
2159 (routing_component_rulebased_t) rc;
2161 int are_processing_units;
2162 xbt_dynar_t rule_list;
2163 if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2164 && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2165 are_processing_units = 1;
2166 rule_list = routing->list_route;
2167 } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2168 && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2170 are_processing_units = 0;
2171 rule_list = routing->list_ASroute;
2174 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
2179 int src_length = (int) strlen(src);
2180 int dst_length = (int) strlen(dst);
2182 xbt_dynar_t links_list =
2183 xbt_dynar_new(global_routing->size_of_link, NULL);
2185 rule_route_t ruleroute;
2187 int ovector_src[OVECCOUNT];
2188 int ovector_dst[OVECCOUNT];
2189 const char **list_src = NULL;
2190 const char **list_dst = NULL;
2191 xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2193 pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2194 ovector_src, OVECCOUNT);
2197 pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2198 ovector_dst, OVECCOUNT);
2200 xbt_assert1(!pcre_get_substring_list
2201 (src, ovector_src, rc_src, &list_src),
2202 "error solving substring list for src \"%s\"", src);
2203 xbt_assert1(!pcre_get_substring_list
2204 (dst, ovector_dst, rc_dst, &list_dst),
2205 "error solving substring list for src \"%s\"", dst);
2207 xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2208 char *new_link_name =
2209 remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2211 xbt_dict_get_or_null(surf_network_model->resource_set,
2214 xbt_dynar_push(links_list, &link);
2216 THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2217 xbt_free(new_link_name);
2221 if (rc_src >= 0 && rc_dst >= 0)
2225 route_extended_t new_e_route = NULL;
2226 if (rc_src >= 0 && rc_dst >= 0) {
2227 new_e_route = xbt_new0(s_route_extended_t, 1);
2228 new_e_route->generic_route.link_list = links_list;
2229 } else if (!strcmp(src, dst) && are_processing_units) {
2230 new_e_route = xbt_new0(s_route_extended_t, 1);
2231 xbt_dynar_push(links_list, &(global_routing->loopback));
2232 new_e_route->generic_route.link_list = links_list;
2234 xbt_dynar_free(&link_list);
2237 if (!are_processing_units && new_e_route) {
2238 rule_route_extended_t ruleroute_extended =
2239 (rule_route_extended_t) ruleroute;
2240 new_e_route->src_gateway =
2241 remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2243 new_e_route->dst_gateway =
2244 remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2249 pcre_free_substring_list(list_src);
2251 pcre_free_substring_list(list_dst);
2256 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2263 static void rulebased_finalize(routing_component_t rc)
2265 routing_component_rulebased_t routing =
2266 (routing_component_rulebased_t) rc;
2268 xbt_dict_free(&routing->dict_processing_units);
2269 xbt_dict_free(&routing->dict_autonomous_systems);
2270 xbt_dynar_free(&routing->list_route);
2271 xbt_dynar_free(&routing->list_ASroute);
2272 /* Delete structure */
2277 /* Creation routing model functions */
2278 static void *model_rulebased_create(void)
2280 routing_component_rulebased_t new_component =
2281 xbt_new0(s_routing_component_rulebased_t, 1);
2282 new_component->generic_routing.set_processing_unit =
2283 model_rulebased_set_processing_unit;
2284 new_component->generic_routing.set_autonomous_system =
2285 model_rulebased_set_autonomous_system;
2286 new_component->generic_routing.set_route = model_rulebased_set_route;
2287 new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2288 new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
2289 new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
2290 new_component->generic_routing.get_route = rulebased_get_route;
2291 new_component->generic_routing.get_bypass_route = generic_get_bypassroute; //rulebased_get_bypass_route;
2292 new_component->generic_routing.finalize = rulebased_finalize;
2293 /* initialization of internal structures */
2294 new_component->dict_processing_units = xbt_dict_new();
2295 new_component->dict_autonomous_systems = xbt_dict_new();
2296 new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2297 new_component->list_ASroute =
2298 xbt_dynar_new(sizeof(rule_route_extended_t),
2299 &rule_route_extended_free);
2300 return new_component;
2303 static void model_rulebased_load(void)
2305 /* use "surfxml_add_callback" to add a parse function call */
2308 static void model_rulebased_unload(void)
2310 /* use "surfxml_del_callback" to remove a parse function call */
2313 static void model_rulebased_end(void)
2317 #endif /* HAVE_PCRE_LIB */
2319 /* ************************************************************************** */
2320 /* ******************************* NO ROUTING ******************************* */
2322 /* Routing model structure */
2324 s_routing_component_t generic_routing;
2325 } s_routing_component_none_t, *routing_component_none_t;
2327 /* Business methods */
2328 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2333 static route_extended_t none_get_route(routing_component_t rc,
2334 const char *src, const char *dst)
2339 static route_extended_t none_get_bypass_route(routing_component_t rc,
2346 static void none_finalize(routing_component_t rc)
2351 static void none_set_processing_unit(routing_component_t rc,
2356 static void none_set_autonomous_system(routing_component_t rc,
2361 /* Creation routing model functions */
2362 static void *model_none_create(void)
2364 routing_component_none_t new_component =
2365 xbt_new0(s_routing_component_none_t, 1);
2366 new_component->generic_routing.set_processing_unit =
2367 none_set_processing_unit;
2368 new_component->generic_routing.set_autonomous_system =
2369 none_set_autonomous_system;
2370 new_component->generic_routing.set_route = NULL;
2371 new_component->generic_routing.set_ASroute = NULL;
2372 new_component->generic_routing.set_bypassroute = NULL;
2373 new_component->generic_routing.get_route = none_get_route;
2374 new_component->generic_routing.get_onelink_routes =
2375 none_get_onelink_routes;
2376 new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2377 new_component->generic_routing.finalize = none_finalize;
2378 return new_component;
2381 static void model_none_load(void)
2385 static void model_none_unload(void)
2389 static void model_none_end(void)
2393 /* ************************************************** */
2394 /* ********** PATERN FOR NEW ROUTING **************** */
2396 /* The minimal configuration of a new routing model need the next functions,
2397 * also you need to set at the start of the file, the new model in the model
2398 * list. Remember keep the null ending of the list.
2400 /*** Routing model structure ***/
2402 // s_routing_component_t generic_routing;
2403 // /* things that your routing model need */
2404 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2406 /*** Parse routing model functions ***/
2407 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2408 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2409 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2410 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2411 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2413 /*** Business methods ***/
2414 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2415 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2416 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2418 /*** Creation routing model functions ***/
2419 // static void* model_NEW_create(void) {
2420 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
2421 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2422 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2423 // new_component->generic_routing.set_route = model_NEW_set_route;
2424 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2425 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2426 // new_component->generic_routing.get_route = NEW_get_route;
2427 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2428 // new_component->generic_routing.finalize = NEW_finalize;
2429 // /* initialization of internal structures */
2430 // return new_component;
2431 // } /* mandatory */
2432 // static void model_NEW_load(void) {} /* mandatory */
2433 // static void model_NEW_unload(void) {} /* mandatory */
2434 // static void model_NEW_end(void) {} /* mandatory */
2436 /* ************************************************************************** */
2437 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2439 static void generic_set_processing_unit(routing_component_t rc,
2442 DEBUG1("Load process unit \"%s\"", name);
2443 int *id = xbt_new0(int, 1);
2444 xbt_dict_t _to_index;
2445 _to_index = current_routing->to_index;
2446 *id = xbt_dict_length(_to_index);
2447 xbt_dict_set(_to_index, name, id, xbt_free);
2450 static void generic_set_autonomous_system(routing_component_t rc,
2453 DEBUG1("Load Autonomous system \"%s\"", name);
2454 int *id = xbt_new0(int, 1);
2455 xbt_dict_t _to_index;
2456 _to_index = current_routing->to_index;
2457 *id = xbt_dict_length(_to_index);
2458 xbt_dict_set(_to_index, name, id, xbt_free);
2461 static void generic_set_route(routing_component_t rc, const char *src,
2462 const char *dst, route_t route)
2464 DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
2465 xbt_dict_t _parse_routes;
2466 xbt_dict_t _to_index;
2468 int *src_id, *dst_id;
2469 unsigned long nb_links = xbt_dynar_length(route->link_list);
2471 _to_index = current_routing->to_index;
2472 _parse_routes = current_routing->parse_routes;
2473 src_id = xbt_dict_get_or_null(_to_index, src);
2474 dst_id = xbt_dict_get_or_null(_to_index, dst);
2475 routing_component_full_t routing = ((routing_component_full_t) current_routing);
2476 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
2477 route_t route_to_test;
2480 && dst_id, "Network elements %s or %s not found", src, dst);
2482 xbt_assert2(xbt_dynar_length(route->link_list) > 0,
2483 "Invalid count of links, must be greater than zero (%s,%s)",
2486 if(!strcmp(rc->routing->name,"Full"))
2488 /* Create the routing table */
2489 if(!routing->routing_table)
2490 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
2492 route_to_test = &(TO_ROUTE_FULL(*src_id, *dst_id))->generic_route;
2495 route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2498 xbt_assert2(!xbt_dynar_compare(
2499 (void*)route->link_list,
2500 (void*)route_to_test->link_list,
2501 (int_f_cpvoid_cpvoid_t) strcmp),
2502 "The route between \"%s\" and \"%s\" already exist", src,dst);
2504 if(!strcmp(rc->routing->name,"Full"))
2506 TO_ROUTE_FULL(*src_id, *dst_id) =
2507 generic_new_extended_route(current_routing->hierarchy, route, 1);
2511 route_name = bprintf("%d#%d", *src_id, *dst_id);
2512 xbt_dict_set(_parse_routes, route_name, route, NULL);
2513 xbt_free(route_name);
2516 if(A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES)
2519 route_t route_sym = xbt_new0(s_route_t, 1);
2520 route_sym->link_list = xbt_dynar_new(sizeof(char *),NULL);
2521 for(i=nb_links ; i>0 ; i--)
2523 char *link_name = xbt_new0(char,strlen(xbt_dynar_get_as(route->link_list, i-1, char *)));
2524 link_name = xbt_strdup(xbt_dynar_get_as(route->link_list, i-1, char *));
2525 xbt_dynar_push_as(route_sym->link_list ,char *, link_name);
2527 DEBUG2("Load Route from \"%s\" to \"%s\"", dst, src);
2529 if(!strcmp(rc->routing->name,"Full"))
2531 TO_ROUTE_FULL(*dst_id, *src_id) =
2532 generic_new_extended_route(current_routing->hierarchy, route_sym, 1);
2536 route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d",*dst_id, *src_id));
2538 xbt_assert2(!xbt_dynar_compare(
2539 (void*)route_sym->link_list,
2540 (void*)route_to_test->link_list,
2541 (int_f_cpvoid_cpvoid_t) strcmp),
2542 "The route between \"%s\" and \"%s\" already exist", dst,src);
2544 xbt_dict_set(_parse_routes, bprintf("%d#%d",*dst_id, *src_id), route_sym, NULL);
2549 static void generic_set_ASroute(routing_component_t rc, const char *src,
2550 const char *dst, route_extended_t e_route)
2552 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2553 e_route->src_gateway, dst, e_route->dst_gateway);
2555 xbt_dict_t _parse_routes;
2556 xbt_dict_t _to_index;
2558 int *src_id, *dst_id;
2559 _to_index = current_routing->to_index;
2560 _parse_routes = current_routing->parse_routes;
2562 src_id = xbt_dict_get_or_null(_to_index, src);
2563 dst_id = xbt_dict_get_or_null(_to_index, dst);
2564 routing_component_full_t routing = ((routing_component_full_t) current_routing);
2565 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
2566 route_t route_to_test;
2569 && dst_id, "Network elements %s or %s not found", src, dst);
2571 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2572 "Invalid count of links, must be greater than zero (%s,%s)",
2575 if(!strcmp(rc->routing->name,"Full"))
2577 /* Create the routing table */
2578 if(!routing->routing_table)
2579 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
2581 route_to_test = &((route_extended_t)(TO_ROUTE_FULL(*src_id, *dst_id)))->generic_route;
2584 route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2587 xbt_assert4(!xbt_dynar_compare(
2588 (void*) (&e_route->generic_route)->link_list,
2589 (void*) route_to_test->link_list,
2590 (int_f_cpvoid_cpvoid_t) strcmp),
2591 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2592 src, e_route->src_gateway, dst, e_route->dst_gateway);
2594 if(!strcmp(rc->routing->name,"Full"))
2596 TO_ROUTE_FULL(*src_id, *dst_id) =
2597 generic_new_extended_route(current_routing->hierarchy, &(e_route->generic_route), 1);
2601 route_name = bprintf("%d#%d", *src_id, *dst_id);
2602 xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2603 xbt_free(route_name);
2606 unsigned long nb_links = xbt_dynar_length(e_route->generic_route.link_list);
2608 if(A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES)
2611 route_extended_t route_sym = xbt_new0(s_route_extended_t, 1);
2612 route_sym->generic_route.link_list = xbt_dynar_new(sizeof(char *),NULL);
2613 for(i=nb_links ; i>0 ; i--)
2615 char *link_name = xbt_new0(char,strlen(xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *)));
2616 link_name = bprintf("%s",xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *));
2617 xbt_dynar_push_as(route_sym->generic_route.link_list ,char *, link_name);
2619 route_sym->src_gateway = xbt_new0( char,strlen(e_route->dst_gateway) );
2620 route_sym->src_gateway = bprintf("%s",e_route->dst_gateway);
2621 route_sym->dst_gateway = xbt_new0( char,strlen(e_route->src_gateway) );
2622 route_sym->dst_gateway = bprintf("%s",e_route->src_gateway);
2623 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",dst, route_sym->src_gateway,src,route_sym->dst_gateway);
2625 if(!strcmp(rc->routing->name,"Full"))
2627 TO_ROUTE_FULL(*dst_id, *src_id) =
2628 generic_new_extended_route(current_routing->hierarchy, &(route_sym->generic_route), 1);
2632 route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d", *dst_id, *src_id));
2634 xbt_assert4(!xbt_dynar_compare(
2635 (void*) (&route_sym->generic_route)->link_list,
2636 (void*) route_to_test->link_list,
2637 (int_f_cpvoid_cpvoid_t) strcmp),
2638 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2639 dst, route_sym->src_gateway, src, route_sym->dst_gateway);
2640 xbt_dict_set(_parse_routes, bprintf("%d#%d", *dst_id, *src_id), route_sym, NULL);
2645 static void generic_set_bypassroute(routing_component_t rc,
2646 const char *src, const char *dst,
2647 route_extended_t e_route)
2649 DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2650 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2653 route_name = bprintf("%s#%s", src, dst);
2654 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2655 "Invalid count of links, must be greater than zero (%s,%s)",
2657 xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2658 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2659 src, e_route->src_gateway, dst, e_route->dst_gateway);
2661 route_extended_t new_e_route =
2662 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2663 xbt_dynar_free(&(e_route->generic_route.link_list));
2666 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2667 (void (*)(void *)) generic_free_extended_route);
2668 xbt_free(route_name);
2671 /* ************************************************************************** */
2672 /* *********************** GENERIC BUSINESS METHODS ************************* */
2674 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2676 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2679 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2683 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2684 routing_component_t src_as, dst_as;
2685 int index_src, index_dst;
2686 xbt_dynar_t path_src = NULL;
2687 xbt_dynar_t path_dst = NULL;
2688 routing_component_t current = NULL;
2689 routing_component_t *current_src = NULL;
2690 routing_component_t *current_dst = NULL;
2692 /* (1) find the as where the src and dst are located */
2693 src_as = ((network_element_info_t)
2694 xbt_dict_get_or_null(global_routing->where_network_elements,
2695 src))->rc_component;
2696 dst_as = ((network_element_info_t)
2697 xbt_dict_get_or_null(global_routing->where_network_elements,
2698 dst))->rc_component;
2701 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2704 /* (2) find the path to the root routing component */
2705 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2707 while (current != NULL) {
2708 xbt_dynar_push(path_src, ¤t);
2709 current = current->routing_father;
2711 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2713 while (current != NULL) {
2714 xbt_dynar_push(path_dst, ¤t);
2715 current = current->routing_father;
2718 /* (3) find the common father */
2719 index_src = (path_src->used) - 1;
2720 index_dst = (path_dst->used) - 1;
2721 current_src = xbt_dynar_get_ptr(path_src, index_src);
2722 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2723 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2724 routing_component_t *tmp_src, *tmp_dst;
2725 tmp_src = xbt_dynar_pop_ptr(path_src);
2726 tmp_dst = xbt_dynar_pop_ptr(path_dst);
2729 current_src = xbt_dynar_get_ptr(path_src, index_src);
2730 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2733 int max_index_src = (path_src->used) - 1;
2734 int max_index_dst = (path_dst->used) - 1;
2736 int max_index = max(max_index_src, max_index_dst);
2739 route_extended_t e_route_bypass = NULL;
2741 for (max = 0; max <= max_index; max++) {
2742 for (i = 0; i < max; i++) {
2743 if (i <= max_index_src && max <= max_index_dst) {
2744 char *route_name = bprintf("%s#%s",
2745 (*(routing_component_t *)
2747 (path_src, i)))->name,
2748 (*(routing_component_t *)
2750 (path_dst, max)))->name);
2752 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2753 xbt_free(route_name);
2757 if (max <= max_index_src && i <= max_index_dst) {
2758 char *route_name = bprintf("%s#%s",
2759 (*(routing_component_t *)
2761 (path_src, max)))->name,
2762 (*(routing_component_t *)
2764 (path_dst, i)))->name);
2766 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2767 xbt_free(route_name);
2776 if (max <= max_index_src && max <= max_index_dst) {
2777 char *route_name = bprintf("%s#%s",
2778 (*(routing_component_t *)
2780 (path_src, max)))->name,
2781 (*(routing_component_t *)
2783 (path_dst, max)))->name);
2784 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2785 xbt_free(route_name);
2791 xbt_dynar_free(&path_src);
2792 xbt_dynar_free(&path_dst);
2794 route_extended_t new_e_route = NULL;
2796 if (e_route_bypass) {
2798 unsigned int cpt = 0;
2799 new_e_route = xbt_new0(s_route_extended_t, 1);
2800 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2801 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2802 new_e_route->generic_route.link_list =
2803 xbt_dynar_new(global_routing->size_of_link, NULL);
2804 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2805 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2812 /* ************************************************************************** */
2813 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2815 static route_extended_t
2816 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2817 void *data, int order)
2821 route_extended_t e_route, new_e_route;
2824 xbt_dynar_t links = NULL, links_id = NULL;
2826 new_e_route = xbt_new0(s_route_extended_t, 1);
2827 new_e_route->generic_route.link_list =
2828 xbt_dynar_new(global_routing->size_of_link, NULL);
2829 new_e_route->src_gateway = NULL;
2830 new_e_route->dst_gateway = NULL;
2832 xbt_assert0(hierarchy == SURF_ROUTING_BASE
2833 || hierarchy == SURF_ROUTING_RECURSIVE,
2834 "the hierarchy type is not defined");
2836 if (hierarchy == SURF_ROUTING_BASE) {
2838 route = (route_t) data;
2839 links = route->link_list;
2841 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2843 e_route = (route_extended_t) data;
2844 xbt_assert0(e_route->src_gateway
2845 && e_route->dst_gateway, "bad gateway, is null");
2846 links = e_route->generic_route.link_list;
2848 /* remeber not erase the gateway names */
2849 new_e_route->src_gateway = e_route->src_gateway;
2850 new_e_route->dst_gateway = e_route->dst_gateway;
2853 links_id = new_e_route->generic_route.link_list;
2855 xbt_dynar_foreach(links, cpt, link_name) {
2858 xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2861 xbt_dynar_push(links_id, &link);
2863 xbt_dynar_unshift(links_id, &link);
2865 THROW1(mismatch_error, 0, "Link %s not found", link_name);
2871 static void generic_free_route(route_t route)
2874 xbt_dynar_free(&(route->link_list));
2879 static void generic_free_extended_route(route_extended_t e_route)
2882 xbt_dynar_free(&(e_route->generic_route.link_list));
2883 if (e_route->src_gateway)
2884 xbt_free(e_route->src_gateway);
2885 if (e_route->dst_gateway)
2886 xbt_free(e_route->dst_gateway);
2891 static routing_component_t generic_as_exist(routing_component_t find_from,
2892 routing_component_t to_find)
2894 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2895 xbt_dict_cursor_t cursor = NULL;
2898 routing_component_t elem;
2899 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2900 if (to_find == elem || generic_as_exist(elem, to_find)) {
2910 static routing_component_t
2911 generic_autonomous_system_exist(routing_component_t rc, char *element)
2913 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2914 routing_component_t element_as, result, elem;
2915 xbt_dict_cursor_t cursor = NULL;
2917 element_as = ((network_element_info_t)
2918 xbt_dict_get_or_null
2919 (global_routing->where_network_elements,
2920 element))->rc_component;
2921 result = ((routing_component_t) - 1);
2922 if (element_as != rc)
2923 result = generic_as_exist(rc, element_as);
2927 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2928 found = !strcmp(elem->name, element);
2938 static routing_component_t
2939 generic_processing_units_exist(routing_component_t rc, char *element)
2941 routing_component_t element_as;
2942 element_as = ((network_element_info_t)
2943 xbt_dict_get_or_null
2944 (global_routing->where_network_elements,
2945 element))->rc_component;
2946 if (element_as == rc)
2948 return generic_as_exist(rc, element_as);
2951 static void generic_src_dst_check(routing_component_t rc, const char *src,
2955 routing_component_t src_as = ((network_element_info_t)
2956 xbt_dict_get_or_null
2957 (global_routing->where_network_elements,
2958 src))->rc_component;
2959 routing_component_t dst_as = ((network_element_info_t)
2960 xbt_dict_get_or_null
2961 (global_routing->where_network_elements,
2962 dst))->rc_component;
2964 xbt_assert3(src_as != NULL && dst_as != NULL,
2965 "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
2966 src, dst, rc->name);
2967 xbt_assert4(src_as == dst_as,
2968 "The src(%s in %s) and dst(%s in %s) are in differents AS",
2969 src, src_as->name, dst, dst_as->name);
2970 xbt_assert2(rc == dst_as,
2971 "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
2972 rc->name, dst_as->name);
2975 static void routing_parse_Sconfig(void)
2978 DEBUG0("WARNING tag config not yet implemented.");
2979 DEBUG1("Configuration name = %s",A_surfxml_config_id);
2982 static void routing_parse_Econfig(void)
2985 xbt_dict_cursor_t cursor = NULL;
2988 xbt_dict_foreach(current_property_set, cursor, key, elem) {
2989 DEBUG2("property : %s = %s",key,elem);
2993 static void routing_parse_Scluster(void)
2995 static int AX_ptr = 0;
2997 char *cluster_id = A_surfxml_cluster_id;
2998 char *cluster_prefix = A_surfxml_cluster_prefix;
2999 char *cluster_suffix = A_surfxml_cluster_suffix;
3000 char *cluster_radical = A_surfxml_cluster_radical;
3001 char *cluster_power = A_surfxml_cluster_power;
3002 char *cluster_bw = A_surfxml_cluster_bw;
3003 char *cluster_lat = A_surfxml_cluster_lat;
3004 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
3005 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
3006 char *host_id, *groups, *link_id = NULL;
3007 char *router_id, *link_router, *link_backbone, *route_src_dst;
3010 xbt_dynar_t radical_elements;
3011 xbt_dynar_t radical_ends;
3012 int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
3013 int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
3015 #ifndef HAVE_PCRE_LIB
3016 xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
3017 char *route_src, *route_dst;
3021 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
3022 static unsigned int surfxml_buffer_stack_stack[1024];
3024 surfxml_buffer_stack_stack[0] = 0;
3026 surfxml_bufferstack_push(1);
3028 SURFXML_BUFFER_SET(AS_id, cluster_id);
3029 #ifdef HAVE_PCRE_LIB
3030 SURFXML_BUFFER_SET(AS_routing, "RuleBased");
3031 DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
3033 SURFXML_BUFFER_SET(AS_routing, "Full");
3034 DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
3036 SURFXML_START_TAG(AS);
3038 radical_elements = xbt_str_split(cluster_radical, ",");
3039 xbt_dynar_foreach(radical_elements, iter, groups) {
3040 radical_ends = xbt_str_split(groups, "-");
3041 switch (xbt_dynar_length(radical_ends)) {
3043 surf_parse_get_int(&start,
3044 xbt_dynar_get_as(radical_ends, 0, char *));
3045 host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
3046 #ifndef HAVE_PCRE_LIB
3047 xbt_dynar_push_as(tab_elements_num, int, start);
3049 link_id = bprintf("%s_link_%d", cluster_id, start);
3051 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3052 A_surfxml_host_state = A_surfxml_host_state_ON;
3053 SURFXML_BUFFER_SET(host_id, host_id);
3054 SURFXML_BUFFER_SET(host_power, cluster_power);
3055 SURFXML_BUFFER_SET(host_availability, "1.0");
3056 SURFXML_BUFFER_SET(host_availability_file, "");
3057 SURFXML_BUFFER_SET(host_state_file, "");
3058 SURFXML_START_TAG(host);
3059 SURFXML_END_TAG(host);
3061 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3062 A_surfxml_link_state = A_surfxml_link_state_ON;
3063 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3064 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3065 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3066 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3067 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3068 SURFXML_BUFFER_SET(link_id, link_id);
3069 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3070 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3071 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3072 SURFXML_BUFFER_SET(link_latency_file, "");
3073 SURFXML_BUFFER_SET(link_state_file, "");
3074 SURFXML_START_TAG(link);
3075 SURFXML_END_TAG(link);
3081 surf_parse_get_int(&start,
3082 xbt_dynar_get_as(radical_ends, 0, char *));
3083 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3084 DEBUG2("Create hosts and links from %d to %d", start, end);
3085 for (i = start; i <= end; i++) {
3086 host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3087 #ifndef HAVE_PCRE_LIB
3088 xbt_dynar_push_as(tab_elements_num, int, i);
3090 link_id = bprintf("%s_link_%d", cluster_id, i);
3092 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3093 A_surfxml_host_state = A_surfxml_host_state_ON;
3094 SURFXML_BUFFER_SET(host_id, host_id);
3095 SURFXML_BUFFER_SET(host_power, cluster_power);
3096 SURFXML_BUFFER_SET(host_availability, "1.0");
3097 SURFXML_BUFFER_SET(host_availability_file, "");
3098 SURFXML_BUFFER_SET(host_state_file, "");
3099 SURFXML_START_TAG(host);
3100 SURFXML_END_TAG(host);
3102 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3103 A_surfxml_link_state = A_surfxml_link_state_ON;
3104 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3105 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3106 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3107 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3108 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3109 SURFXML_BUFFER_SET(link_id, link_id);
3110 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3111 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3112 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3113 SURFXML_BUFFER_SET(link_latency_file, "");
3114 SURFXML_BUFFER_SET(link_state_file, "");
3115 SURFXML_START_TAG(link);
3116 SURFXML_END_TAG(link);
3121 DEBUG0("Malformed radical");
3124 xbt_dynar_free(&radical_ends);
3129 bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3131 link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3132 link_backbone = bprintf("%s_backbone", cluster_id);
3134 DEBUG1("<router id=\"%s\"/>", router_id);
3135 SURFXML_BUFFER_SET(router_id, router_id);;
3136 SURFXML_START_TAG(router);
3137 SURFXML_END_TAG(router);
3139 DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,cluster_bw, cluster_lat);
3140 A_surfxml_link_state = A_surfxml_link_state_ON;
3141 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3142 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3143 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3144 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3145 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3146 SURFXML_BUFFER_SET(link_id, link_router);
3147 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3148 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3149 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3150 SURFXML_BUFFER_SET(link_latency_file, "");
3151 SURFXML_BUFFER_SET(link_state_file, "");
3152 SURFXML_START_TAG(link);
3153 SURFXML_END_TAG(link);
3155 DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bw, cluster_lat);
3156 A_surfxml_link_state = A_surfxml_link_state_ON;
3157 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3158 if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
3159 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3160 SURFXML_BUFFER_SET(link_id, link_backbone);
3161 SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3162 SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3163 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3164 SURFXML_BUFFER_SET(link_latency_file, "");
3165 SURFXML_BUFFER_SET(link_state_file, "");
3166 SURFXML_START_TAG(link);
3167 SURFXML_END_TAG(link);
3169 char *new_suffix = bprintf("%s", "");
3171 radical_elements = xbt_str_split(cluster_suffix, ".");
3172 xbt_dynar_foreach(radical_elements, iter, groups) {
3173 if (strcmp(groups, "")) {
3174 new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3177 route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3181 #ifdef HAVE_PCRE_LIB
3183 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
3184 DEBUG0("symetrical=\"NO\">");
3185 SURFXML_BUFFER_SET(route_src, route_src_dst);
3186 SURFXML_BUFFER_SET(route_dst, route_src_dst);
3187 A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3188 SURFXML_START_TAG(route);
3190 DEBUG1("<link_ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3191 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3192 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3193 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3194 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3195 SURFXML_START_TAG(link_ctn);
3196 SURFXML_END_TAG(link_ctn);
3198 DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3199 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3200 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3201 SURFXML_START_TAG(link_ctn);
3202 SURFXML_END_TAG(link_ctn);
3204 DEBUG1("<link_ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3205 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3206 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3207 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3208 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3209 SURFXML_START_TAG(link_ctn);
3210 SURFXML_END_TAG(link_ctn);
3213 SURFXML_END_TAG(route);
3215 for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3216 for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3217 if (i == xbt_dynar_length(tab_elements_num)) {
3218 route_src = router_id;
3221 bprintf("%s%d%s", cluster_prefix,
3222 xbt_dynar_get_as(tab_elements_num, i, int),
3226 if (j == xbt_dynar_length(tab_elements_num)) {
3227 route_dst = router_id;
3230 bprintf("%s%d%s", cluster_prefix,
3231 xbt_dynar_get_as(tab_elements_num, j, int),
3235 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
3236 DEBUG0("symetrical=\"NO\">");
3237 SURFXML_BUFFER_SET(route_src, route_src);
3238 SURFXML_BUFFER_SET(route_dst, route_dst);
3239 A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3240 SURFXML_START_TAG(route);
3242 if (i == xbt_dynar_length(tab_elements_num)) {
3243 route_src = link_router;
3246 bprintf("%s_link_%d", cluster_id,
3247 xbt_dynar_get_as(tab_elements_num, i, int));
3250 if (j == xbt_dynar_length(tab_elements_num)) {
3251 route_dst = link_router;
3254 bprintf("%s_link_%d", cluster_id,
3255 xbt_dynar_get_as(tab_elements_num, j, int));
3258 DEBUG1("<link_ctn\tid=\"%s\"/>", route_src);
3259 SURFXML_BUFFER_SET(link_ctn_id, route_src);
3260 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3261 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3262 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3263 SURFXML_START_TAG(link_ctn);
3264 SURFXML_END_TAG(link_ctn);
3266 DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3267 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3268 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3269 SURFXML_START_TAG(link_ctn);
3270 SURFXML_END_TAG(link_ctn);
3272 DEBUG1("<link_ctn\tid=\"%s\"/>", route_dst);
3273 SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3274 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3275 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3276 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3277 SURFXML_START_TAG(link_ctn);
3278 SURFXML_END_TAG(link_ctn);
3281 SURFXML_END_TAG(route);
3284 xbt_dynar_free(&tab_elements_num);
3288 SURFXML_END_TAG(AS);
3291 surfxml_bufferstack_pop(1);
3295 * New methods to init the routing model component from the lua script
3299 * calling parse_S_AS_lua with lua values
3301 void routing_AS_init(const char *AS_id, const char *AS_routing)
3303 parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3307 * calling parse_E_AS_lua to fisnish the creation of routing component
3309 void routing_AS_end(const char *AS_id)
3311 parse_E_AS_lua((char *) AS_id);
3315 * add a host to the network element list
3318 void routing_add_host(const char *host_id)
3320 parse_S_host_lua((char *) host_id);
3324 * Set a new link on the actual list of link for a route or ASroute
3326 void routing_add_link(const char *link_id)
3328 parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3332 *Set the endpoints for a route
3334 void routing_set_route(const char *src_id, const char *dst_id)
3336 parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3340 * Store the route by calling parse_E_route_store_route
3342 void routing_store_route(void)
3344 parse_E_route_store_route();