1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "gras_config.h"
13 #include <pcre.h> /* regular expresion library */
15 #include "surf_private.h"
16 #include "xbt/dynar.h"
18 #include "xbt/config.h"
19 #include "xbt/graph.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route, surf, "Routing part of surf");
25 routing_global_t global_routing = NULL;
26 routing_component_t current_routing = NULL;
27 model_type_t current_routing_model = NULL;
29 /* Prototypes of each model */
30 static void *model_full_create(void); /* create structures for full routing model */
31 static void model_full_load(void); /* load parse functions for full routing model */
32 static void model_full_unload(void); /* unload parse functions for full routing model */
33 static void model_full_end(void); /* finalize the creation of full routing model */
35 static void *model_floyd_create(void); /* create structures for floyd routing model */
36 static void model_floyd_load(void); /* load parse functions for floyd routing model */
37 static void model_floyd_unload(void); /* unload parse functions for floyd routing model */
38 static void model_floyd_end(void); /* finalize the creation of floyd routing model */
40 static void *model_dijkstra_both_create(int cached); /* create by calling dijkstra or dijkstracache */
41 static void *model_dijkstra_create(void); /* create structures for dijkstra routing model */
42 static void *model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
43 static void model_dijkstra_both_load(void); /* load parse functions for dijkstra routing model */
44 static void model_dijkstra_both_unload(void); /* unload parse functions for dijkstra routing model */
45 static void model_dijkstra_both_end(void); /* finalize the creation of dijkstra routing model */
47 static void *model_rulebased_create(void); /* create structures for rulebased routing model */
48 static void model_rulebased_load(void); /* load parse functions for rulebased routing model */
49 static void model_rulebased_unload(void); /* unload parse functions for rulebased routing model */
50 static void model_rulebased_end(void); /* finalize the creation of rulebased routing model */
52 static void *model_none_create(void); /* none routing model */
53 static void model_none_load(void); /* none routing model */
54 static void model_none_unload(void); /* none routing model */
55 static void model_none_end(void); /* none routing model */
57 static void routing_parse_Scluster(void); /*cluster bypass */
59 static void routing_parse_Sconfig(void); /*config Tag */
60 static void routing_parse_Econfig(void); /*config Tag */
62 /* this lines are only for replace use like index in the model table */
67 SURF_MODEL_DIJKSTRACACHE,
75 /* must be finish with null and carefull if change de order */
76 struct s_model_type routing_models[] = { {"Full",
77 "Full routing data (fast, large memory requirements, fully expressive)",
83 "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
84 model_floyd_create, model_floyd_load, model_floyd_unload,
87 "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
88 model_dijkstra_create, model_dijkstra_both_load,
89 model_dijkstra_both_unload, model_dijkstra_both_end},
91 "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
92 model_dijkstracache_create, model_dijkstra_both_load,
93 model_dijkstra_both_unload, model_dijkstra_both_end},
94 {"none", "No routing (usable with Constant network only)",
95 model_none_create, model_none_load, model_none_unload, model_none_end},
97 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create,
98 model_rulebased_load, model_rulebased_unload, model_rulebased_end},
100 {NULL, NULL, NULL, NULL, NULL, NULL}
103 /* ************************************************************************** */
104 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
106 static void generic_set_processing_unit(routing_component_t rc,
108 static void generic_set_autonomous_system(routing_component_t rc,
110 static void generic_set_route(routing_component_t rc, const char *src,
111 const char *dst, route_t route);
112 static void generic_set_ASroute(routing_component_t rc, const char *src,
113 const char *dst, route_extended_t e_route);
114 static void generic_set_bypassroute(routing_component_t rc,
115 const char *src, const char *dst,
116 route_extended_t e_route);
117 int compare_routes(route_t route1, route_t route2);
119 /* ************************************************************************** */
120 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
122 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc);
123 static route_extended_t generic_get_bypassroute(routing_component_t rc,
127 /* ************************************************************************** */
128 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
130 static route_extended_t
131 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
132 void *data, int order);
133 static void generic_free_route(route_t route);
134 static void generic_free_extended_route(route_extended_t e_route);
135 static routing_component_t
136 generic_autonomous_system_exist(routing_component_t rc, char *element);
137 static routing_component_t
138 generic_processing_units_exist(routing_component_t rc, char *element);
139 static void generic_src_dst_check(routing_component_t rc, const char *src,
142 /* ************************************************************************** */
143 /* **************************** GLOBAL FUNCTIONS **************************** */
145 /* global parse functions */
146 static char *src = NULL; /* temporary store the source name of a route */
147 static char *dst = NULL; /* temporary store the destination name of a route */
148 static char *gw_src = NULL; /* temporary store the gateway source name of a route */
149 static char *gw_dst = NULL; /* temporary store the gateway destination name of a route */
150 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
152 * \brief Add a "host" to the network element list
154 static void parse_S_host(char *host_id)
156 network_element_info_t info = NULL;
157 if (current_routing->hierarchy == SURF_ROUTING_NULL)
158 current_routing->hierarchy = SURF_ROUTING_BASE;
159 xbt_assert1(!xbt_dict_get_or_null
160 (global_routing->where_network_elements, host_id),
161 "Reading a host, processing unit \"%s\" already exist",
163 xbt_assert1(current_routing->set_processing_unit,
164 "no defined method \"set_processing_unit\" in \"%s\"",
165 current_routing->name);
166 (*(current_routing->set_processing_unit)) (current_routing, host_id);
167 info = xbt_new0(s_network_element_info_t, 1);
168 info->rc_component = current_routing;
169 info->rc_type = SURF_NETWORK_ELEMENT_HOST;
170 xbt_dict_set(global_routing->where_network_elements, host_id,
171 (void *) info, NULL);
175 * \brief Add a host to the network element list from XML
177 static void parse_S_host_XML(void)
179 parse_S_host(A_surfxml_host_id);
183 * \brief Add a host to the network element list from lua script
185 static void parse_S_host_lua(char *host_id)
187 parse_S_host(host_id);
195 * \brief Add a "router" to the network element list
197 static void parse_S_router(void)
199 network_element_info_t info = NULL;
201 if (current_routing->hierarchy == SURF_ROUTING_NULL)
202 current_routing->hierarchy = SURF_ROUTING_BASE;
203 xbt_assert1(!xbt_dict_get_or_null
204 (global_routing->where_network_elements,
205 A_surfxml_router_id),
206 "Reading a router, processing unit \"%s\" already exist",
207 A_surfxml_router_id);
208 xbt_assert1(current_routing->set_processing_unit,
209 "no defined method \"set_processing_unit\" in \"%s\"",
210 current_routing->name);
211 (*(current_routing->set_processing_unit)) (current_routing,
212 A_surfxml_router_id);
213 info = xbt_new0(s_network_element_info_t, 1);
214 info->rc_component = current_routing;
215 info->rc_type = SURF_NETWORK_ELEMENT_ROUTER;
216 xbt_dict_set(global_routing->where_network_elements, A_surfxml_router_id,
217 (void *) info, NULL);
219 TRACE_surf_host_declaration(A_surfxml_router_id, 0);
224 * \brief Set the endponints for a route
226 static void parse_S_route_new_and_endpoints(char *src_id, char *dst_id)
228 if (src != NULL && dst != NULL && link_list != NULL)
229 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
233 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0,
234 "Some limits are null in the route between \"%s\" and \"%s\"",
236 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
240 * \breif Set the endpoints for a route from XML
242 static void parse_S_route_new_and_endpoints_XML(void)
244 parse_S_route_new_and_endpoints(A_surfxml_route_src,
245 A_surfxml_route_dst);
249 * \breif Set the endpoints for a route from lua
251 static void parse_S_route_new_and_endpoints_lua(char *id_src, char *id_dst)
253 parse_S_route_new_and_endpoints(id_src, id_dst);
257 * \brief Set the endponints and gateways for a ASroute
259 static void parse_S_ASroute_new_and_endpoints(void)
261 if (src != NULL && dst != NULL && link_list != NULL)
262 THROW2(arg_error, 0, "Route between %s to %s can not be defined",
263 A_surfxml_ASroute_src, A_surfxml_ASroute_dst);
264 src = A_surfxml_ASroute_src;
265 dst = A_surfxml_ASroute_dst;
266 gw_src = A_surfxml_ASroute_gw_src;
267 gw_dst = A_surfxml_ASroute_gw_dst;
268 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
269 || strlen(gw_dst) > 0,
270 "Some limits are null in the route between \"%s\" and \"%s\"",
272 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
276 * \brief Set the endponints for a bypassRoute
278 static void parse_S_bypassRoute_new_and_endpoints(void)
280 if (src != NULL && dst != NULL && link_list != NULL)
282 "Bypass Route between %s to %s can not be defined",
283 A_surfxml_bypassRoute_src, A_surfxml_bypassRoute_dst);
284 src = A_surfxml_bypassRoute_src;
285 dst = A_surfxml_bypassRoute_dst;
286 gw_src = A_surfxml_bypassRoute_gw_src;
287 gw_dst = A_surfxml_bypassRoute_gw_dst;
288 xbt_assert2(strlen(src) > 0 || strlen(dst) > 0 || strlen(gw_src) > 0
289 || strlen(gw_dst) > 0,
290 "Some limits are null in the route between \"%s\" and \"%s\"",
292 link_list = xbt_dynar_new(sizeof(char *), &xbt_free_ref);
296 * \brief Set a new link on the actual list of link for a route or ASroute
298 static void parse_E_link_ctn_new_elem(char *link_id)
301 val = xbt_strdup(link_id);
302 xbt_dynar_push(link_list, &val);
306 * \brief Set a new link on the actual list of link for a route or ASroute from XML
309 static void parse_E_link_ctn_new_elem_XML(void)
311 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_NONE)
312 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
313 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_UP)
314 parse_E_link_ctn_new_elem(bprintf("%s_UP",A_surfxml_link_ctn_id));
315 if( A_surfxml_link_ctn_direction == A_surfxml_link_ctn_direction_DOWN)
316 parse_E_link_ctn_new_elem(bprintf("%s_DOWN",A_surfxml_link_ctn_id));
320 * \brief Set a new link on the actual list of link for a route or ASroute from lua
322 static void parse_E_link_c_ctn_new_elem_lua(char *link_id)
324 parse_E_link_ctn_new_elem(link_id);
328 * \brief Store the route by calling the set_route function of the current routing component
330 static void parse_E_route_store_route(void)
332 route_t route = xbt_new0(s_route_t, 1);
333 route->link_list = link_list;
334 //xbt_assert1(generic_processing_units_exist(current_routing,src),"the \"%s\" processing units gateway does not exist",src);
335 //xbt_assert1(generic_processing_units_exist(current_routing,dst),"the \"%s\" processing units gateway does not exist",dst);
336 xbt_assert1(current_routing->set_route,
337 "no defined method \"set_route\" in \"%s\"",
338 current_routing->name);
339 (*(current_routing->set_route)) (current_routing, src, dst, route);
346 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
348 static void parse_E_ASroute_store_route(void)
350 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
351 e_route->generic_route.link_list = link_list;
352 e_route->src_gateway = xbt_strdup(gw_src);
353 e_route->dst_gateway = xbt_strdup(gw_dst);
354 // xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
355 // xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
356 // xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
357 // xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
358 xbt_assert1(current_routing->set_ASroute,
359 "no defined method \"set_ASroute\" in \"%s\"",
360 current_routing->name);
361 (*(current_routing->set_ASroute)) (current_routing, src, dst, e_route);
370 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
372 static void parse_E_bypassRoute_store_route(void)
374 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
375 e_route->generic_route.link_list = link_list;
376 e_route->src_gateway = xbt_strdup(gw_src);
377 e_route->dst_gateway = xbt_strdup(gw_dst);
378 // xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
379 // xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
380 // xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
381 // xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
382 xbt_assert1(current_routing->set_bypassroute,
383 "no defined method \"set_bypassroute\" in \"%s\"",
384 current_routing->name);
385 (*(current_routing->set_bypassroute)) (current_routing, src, dst,
395 * \brief Make a new routing component
397 * make the new structure and
398 * set the parsing functions to allows parsing the part of the routing tree
400 static void parse_S_AS(char *AS_id, char *AS_routing)
402 routing_component_t new_routing;
403 model_type_t model = NULL;
404 char *wanted = AS_routing;
406 /* seach the routing model */
407 for (cpt = 0; routing_models[cpt].name; cpt++)
408 if (!strcmp(wanted, routing_models[cpt].name))
409 model = &routing_models[cpt];
410 /* if its not exist, error */
412 fprintf(stderr, "Routing model %s not found. Existing models:\n",
414 for (cpt = 0; routing_models[cpt].name; cpt++)
415 if (!strcmp(wanted, routing_models[cpt].name))
416 fprintf(stderr, " %s: %s\n", routing_models[cpt].name,
417 routing_models[cpt].desc);
421 /* make a new routing component */
422 new_routing = (routing_component_t) (*(model->create)) ();
423 new_routing->routing = model;
424 new_routing->hierarchy = SURF_ROUTING_NULL;
425 new_routing->name = xbt_strdup(AS_id);
426 new_routing->routing_sons = xbt_dict_new();
427 //INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
429 if (current_routing == NULL && global_routing->root == NULL) {
431 /* it is the first one */
432 new_routing->routing_father = NULL;
433 global_routing->root = new_routing;
435 } else if (current_routing != NULL && global_routing->root != NULL) {
437 xbt_assert1(!xbt_dict_get_or_null
438 (current_routing->routing_sons, AS_id),
439 "The AS \"%s\" already exist", AS_id);
440 /* it is a part of the tree */
441 new_routing->routing_father = current_routing;
442 /* set the father behavior */
443 if (current_routing->hierarchy == SURF_ROUTING_NULL)
444 current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
445 /* add to the sons dictionary */
446 xbt_dict_set(current_routing->routing_sons, AS_id,
447 (void *) new_routing, NULL);
448 /* add to the father element list */
449 (*(current_routing->set_autonomous_system)) (current_routing, AS_id);
450 /* unload the prev parse rules */
451 (*(current_routing->routing->unload)) ();
454 THROW0(arg_error, 0, "All defined components must be belong to a AS");
456 /* set the new parse rules */
457 (*(new_routing->routing->load)) ();
458 /* set the new current component of the tree */
459 current_routing = new_routing;
463 * Detect the routing model type of the routing component from XML platforms
465 static void parse_S_AS_XML(void)
467 parse_S_AS(A_surfxml_AS_id, A_surfxml_AS_routing);
471 * define the routing model type of routing component from lua script
473 static void parse_S_AS_lua(char *id, char *mode)
475 parse_S_AS(id, mode);
480 * \brief Finish the creation of a new routing component
482 * When you finish to read the routing component, other structures must be created.
483 * the "end" method allow to do that for any routing model type
485 static void parse_E_AS(char *AS_id)
488 if (current_routing == NULL) {
489 THROW1(arg_error, 0, "Close AS(%s), that never open", AS_id);
491 network_element_info_t info = NULL;
492 xbt_assert1(!xbt_dict_get_or_null
493 (global_routing->where_network_elements,
494 current_routing->name), "The AS \"%s\" already exist",
495 current_routing->name);
496 info = xbt_new0(s_network_element_info_t, 1);
497 info->rc_component = current_routing->routing_father;
498 info->rc_type = SURF_NETWORK_ELEMENT_AS;
499 xbt_dict_set(global_routing->where_network_elements,
500 current_routing->name, info, NULL);
501 (*(current_routing->routing->unload)) ();
502 (*(current_routing->routing->end)) ();
503 current_routing = current_routing->routing_father;
504 if (current_routing != NULL)
505 (*(current_routing->routing->load)) ();
510 * \brief Finish the creation of a new routing component from XML
512 static void parse_E_AS_XML(void)
514 parse_E_AS(A_surfxml_AS_id);
518 * \brief Finish the creation of a new routing component from lua
520 static void parse_E_AS_lua(char *id)
525 /* Aux Business methods */
528 * \brief Get the AS father and the first elements of the chain
530 * \param src the source host name
531 * \param dst the destination host name
533 * Get the common father of the to processing units, and the first different
534 * father in the chain
536 static xbt_dynar_t elements_father(const char *src, const char *dst)
539 xbt_assert0(src && dst, "bad parameters for \"elements_father\" method");
541 xbt_dynar_t result = xbt_dynar_new(sizeof(char *), NULL);
543 routing_component_t src_as, dst_as;
544 int index_src, index_dst, index_father_src, index_father_dst;
545 xbt_dynar_t path_src = NULL;
546 xbt_dynar_t path_dst = NULL;
547 routing_component_t current = NULL;
548 routing_component_t *current_src = NULL;
549 routing_component_t *current_dst = NULL;
550 routing_component_t *father = NULL;
552 /* (1) find the as where the src and dst are located */
553 src_as = ((network_element_info_t)
554 xbt_dict_get_or_null(global_routing->where_network_elements,
556 dst_as = ((network_element_info_t)
557 xbt_dict_get_or_null(global_routing->where_network_elements,
561 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
564 /* (2) find the path to the root routing component */
565 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
567 while (current != NULL) {
568 xbt_dynar_push(path_src, ¤t);
569 current = current->routing_father;
571 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
573 while (current != NULL) {
574 xbt_dynar_push(path_dst, ¤t);
575 current = current->routing_father;
578 /* (3) find the common father */
579 index_src = (path_src->used) - 1;
580 index_dst = (path_dst->used) - 1;
581 current_src = xbt_dynar_get_ptr(path_src, index_src);
582 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
583 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
584 current_src = xbt_dynar_get_ptr(path_src, index_src);
585 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
591 current_src = xbt_dynar_get_ptr(path_src, index_src);
592 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
594 /* (4) they are not in the same routing component, make the path */
595 index_father_src = index_src + 1;
596 index_father_dst = index_dst + 1;
598 if (*current_src == *current_dst)
599 father = current_src;
601 father = xbt_dynar_get_ptr(path_src, index_father_src);
603 /* (5) result generation */
604 xbt_dynar_push(result, father); /* first same the father of src and dst */
605 xbt_dynar_push(result, current_src); /* second the first different father of src */
606 xbt_dynar_push(result, current_dst); /* three the first different father of dst */
608 xbt_dynar_free(&path_src);
609 xbt_dynar_free(&path_dst);
614 /* Global Business methods */
617 * \brief Recursive function for get_route
619 * \param src the source host name
620 * \param dst the destination host name
622 * This fuction is call by "get_route". It allow to walk through the
623 * routing components tree.
625 static route_extended_t _get_route(const char *src, const char *dst)
629 unsigned int cpt = 0;
631 DEBUG2("Solve route \"%s\" to \"%s\"", src, dst);
633 xbt_assert0(src && dst, "bad parameters for \"_get_route\" method");
635 route_extended_t e_route, e_route_cnt, e_route_src = NULL, e_route_dst =
638 xbt_dynar_t elem_father_list = elements_father(src, dst);
640 routing_component_t common_father =
641 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
642 routing_component_t src_father =
643 xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
644 routing_component_t dst_father =
645 xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
647 e_route = xbt_new0(s_route_extended_t, 1);
648 e_route->src_gateway = NULL;
649 e_route->dst_gateway = NULL;
650 e_route->generic_route.link_list =
651 xbt_dynar_new(global_routing->size_of_link, NULL);
653 if (src_father == dst_father) { /* SURF_ROUTING_BASE */
655 if (strcmp(src, dst)) {
657 (*(common_father->get_route)) (common_father, src, dst);
658 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"", src,
660 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
661 xbt_dynar_push(e_route->generic_route.link_list, &link);
663 generic_free_extended_route(e_route_cnt);
666 } else { /* SURF_ROUTING_RECURSIVE */
668 route_extended_t e_route_bypass = NULL;
670 if (common_father->get_bypass_route)
672 (*(common_father->get_bypass_route)) (common_father, src, dst);
675 e_route_cnt = e_route_bypass;
678 (*(common_father->get_route)) (common_father, src_father->name,
681 xbt_assert2(e_route_cnt, "no route between \"%s\" and \"%s\"",
682 src_father->name, dst_father->name);
684 xbt_assert2((e_route_cnt->src_gateway == NULL) ==
685 (e_route_cnt->dst_gateway == NULL),
686 "bad gateway for route between \"%s\" and \"%s\"", src,
689 if (src != e_route_cnt->src_gateway) {
690 e_route_src = _get_route(src, e_route_cnt->src_gateway);
691 xbt_assert2(e_route_src, "no route between \"%s\" and \"%s\"", src,
692 e_route_cnt->src_gateway);
693 xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
694 xbt_dynar_push(e_route->generic_route.link_list, &link);
698 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
699 xbt_dynar_push(e_route->generic_route.link_list, &link);
702 if (e_route_cnt->dst_gateway != dst) {
703 e_route_dst = _get_route(e_route_cnt->dst_gateway, dst);
704 xbt_assert2(e_route_dst, "no route between \"%s\" and \"%s\"",
705 e_route_cnt->dst_gateway, dst);
706 xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
707 xbt_dynar_push(e_route->generic_route.link_list, &link);
711 e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
712 e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
714 generic_free_extended_route(e_route_src);
715 generic_free_extended_route(e_route_cnt);
716 generic_free_extended_route(e_route_dst);
719 xbt_dynar_free(&elem_father_list);
725 * \brief Generic method: find a route between hosts
727 * \param src the source host name
728 * \param dst the destination host name
730 * walk through the routing components tree and find a route between hosts
731 * by calling the differents "get_route" functions in each routing component.
732 * No need to free the returned dynar. It will be freed at the next call.
734 static xbt_dynar_t get_route(const char *src, const char *dst)
737 route_extended_t e_route;
738 xbt_dynar_t elem_father_list = elements_father(src, dst);
739 routing_component_t common_father =
740 xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
742 if (strcmp(src, dst))
743 e_route = _get_route(src, dst);
745 e_route = (*(common_father->get_route)) (common_father, src, dst);
747 xbt_assert2(e_route, "no route between \"%s\" and \"%s\"", src, dst);
749 if (global_routing->last_route)
750 xbt_dynar_free(&(global_routing->last_route));
751 global_routing->last_route = e_route->generic_route.link_list;
753 if (e_route->src_gateway)
754 xbt_free(e_route->src_gateway);
755 if (e_route->dst_gateway)
756 xbt_free(e_route->dst_gateway);
759 xbt_dynar_free(&elem_father_list);
761 if (xbt_dynar_length(global_routing->last_route) == 0)
764 return global_routing->last_route;
768 * \brief Generic method: find a route between hosts
770 * \param src the source host name
771 * \param dst the destination host name
773 * walk through the routing components tree and find a route between hosts
774 * by calling the differents "get_route" functions in each routing component.
775 * Leaves the caller the responsability to clean the returned dynar.
777 static xbt_dynar_t get_route_no_cleanup(const char *src, const char *dst)
779 xbt_dynar_t d = get_route(src,dst);
780 global_routing->last_route = NULL;
785 * \brief Recursive function for finalize
787 * \param rc the source host name
789 * This fuction is call by "finalize". It allow to finalize the
790 * AS or routing components. It delete all the structures.
792 static void _finalize(routing_component_t rc)
795 xbt_dict_cursor_t cursor = NULL;
797 routing_component_t elem;
798 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
801 xbt_dict_t tmp_sons = rc->routing_sons;
802 char *tmp_name = rc->name;
803 xbt_dict_free(&tmp_sons);
805 xbt_assert1(rc->finalize, "no defined method \"finalize\" in \"%s\"",
806 current_routing->name);
807 (*(rc->finalize)) (rc);
812 * \brief Generic method: delete all the routing structures
814 * walk through the routing components tree and delete the structures
815 * by calling the differents "finalize" functions in each routing component
817 static void finalize(void)
819 /* delete recursibly all the tree */
820 _finalize(global_routing->root);
821 /* delete "where" dict */
822 xbt_dict_free(&(global_routing->where_network_elements));
823 /* delete last_route */
824 xbt_dynar_free(&(global_routing->last_route));
825 /* delete global routing structure */
826 xbt_free(global_routing);
829 static xbt_dynar_t recursive_get_onelink_routes(routing_component_t rc)
831 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
833 //adding my one link routes
836 xbt_dynar_t onelink_mine = rc->get_onelink_routes(rc);
838 xbt_dynar_foreach(onelink_mine, cpt, link) {
839 xbt_dynar_push(ret, &link);
844 xbt_dict_cursor_t cursor = NULL;
845 routing_component_t rc_child;
846 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
847 xbt_dynar_t onelink_child = recursive_get_onelink_routes(rc_child);
849 xbt_dynar_foreach(onelink_child, cpt, link) {
850 xbt_dynar_push(ret, &link);
857 static xbt_dynar_t get_onelink_routes(void)
859 return recursive_get_onelink_routes(global_routing->root);
862 static e_surf_network_element_type_t get_network_element_type(const char
865 network_element_info_t rc = NULL;
866 rc = xbt_dict_get(global_routing->where_network_elements, name);
871 * \brief Generic method: create the global routing schema
873 * Make a global routing structure and set all the parsing functions.
875 void routing_model_create(size_t size_of_links, void *loopback)
878 /* config the uniq global routing */
879 global_routing = xbt_new0(s_routing_global_t, 1);
880 global_routing->where_network_elements = xbt_dict_new();
881 global_routing->root = NULL;
882 global_routing->get_route = get_route;
883 global_routing->get_route_no_cleanup = get_route_no_cleanup;
884 global_routing->get_onelink_routes = get_onelink_routes;
885 global_routing->get_network_element_type = get_network_element_type;
886 global_routing->finalize = finalize;
887 global_routing->loopback = loopback;
888 global_routing->size_of_link = size_of_links;
889 global_routing->last_route = NULL;
891 /* no current routing at moment */
892 current_routing = NULL;
894 /* parse generic elements */
895 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
896 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
898 surfxml_add_callback(STag_surfxml_route_cb_list,
899 &parse_S_route_new_and_endpoints_XML);
900 surfxml_add_callback(STag_surfxml_ASroute_cb_list,
901 &parse_S_ASroute_new_and_endpoints);
902 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list,
903 &parse_S_bypassRoute_new_and_endpoints);
905 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list,
906 &parse_E_link_ctn_new_elem_XML);
908 surfxml_add_callback(ETag_surfxml_route_cb_list,
909 &parse_E_route_store_route);
910 surfxml_add_callback(ETag_surfxml_ASroute_cb_list,
911 &parse_E_ASroute_store_route);
912 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list,
913 &parse_E_bypassRoute_store_route);
915 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
916 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
918 surfxml_add_callback(STag_surfxml_cluster_cb_list,
919 &routing_parse_Scluster);
921 surfxml_add_callback(STag_surfxml_config_cb_list,
922 &routing_parse_Sconfig);
923 surfxml_add_callback(ETag_surfxml_config_cb_list,
924 &routing_parse_Econfig);
927 /* ************************************************************************** */
928 /* *************************** FULL ROUTING ********************************* */
930 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
932 /* Routing model structure */
935 s_routing_component_t generic_routing;
936 xbt_dict_t parse_routes; /* store data during the parse process */
937 route_extended_t *routing_table;
938 } s_routing_component_full_t, *routing_component_full_t;
940 /* Business methods */
941 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
943 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
945 routing_component_full_t routing = (routing_component_full_t) rc;
946 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
947 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
948 char *k1, *d1, *k2, *d2;
949 xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
950 xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
951 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k1);
952 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, k2);
955 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
957 route_extended_t route = TO_ROUTE_FULL(*src_id, *dst_id);
959 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
961 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
963 onelink_t onelink = xbt_new0(s_onelink_t, 1);
964 onelink->link_ptr = link;
965 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
966 onelink->src = xbt_strdup(k1);
967 onelink->dst = xbt_strdup(k2);
968 } else if (routing->generic_routing.hierarchy ==
969 SURF_ROUTING_RECURSIVE) {
970 onelink->src = xbt_strdup(route->src_gateway);
971 onelink->dst = xbt_strdup(route->dst_gateway);
973 xbt_dynar_push(ret, &onelink);
981 static route_extended_t full_get_route(routing_component_t rc,
982 const char *src, const char *dst)
984 xbt_assert1(rc && src
986 "Invalid params for \"get_route\" function at AS \"%s\"",
990 routing_component_full_t routing = (routing_component_full_t) rc;
991 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
993 generic_src_dst_check(rc, src, dst);
994 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
995 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
998 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1001 route_extended_t e_route = NULL;
1002 route_extended_t new_e_route = NULL;
1004 unsigned int cpt = 0;
1006 e_route = TO_ROUTE_FULL(*src_id, *dst_id);
1009 new_e_route = xbt_new0(s_route_extended_t, 1);
1010 new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
1011 new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
1012 new_e_route->generic_route.link_list =
1013 xbt_dynar_new(global_routing->size_of_link, NULL);
1014 xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
1015 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
1021 static void full_finalize(routing_component_t rc)
1023 routing_component_full_t routing = (routing_component_full_t) rc;
1024 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1027 /* Delete routing table */
1028 for (i = 0; i < table_size; i++)
1029 for (j = 0; j < table_size; j++)
1030 generic_free_extended_route(TO_ROUTE_FULL(i, j));
1031 xbt_free(routing->routing_table);
1032 /* Delete bypass dict */
1033 xbt_dict_free(&rc->bypassRoutes);
1034 /* Delete index dict */
1035 xbt_dict_free(&rc->to_index);
1036 /* Delete structure */
1041 /* Creation routing model functions */
1043 static void *model_full_create(void)
1045 routing_component_full_t new_component =
1046 xbt_new0(s_routing_component_full_t, 1);
1047 new_component->generic_routing.set_processing_unit =
1048 generic_set_processing_unit;
1049 new_component->generic_routing.set_autonomous_system =
1050 generic_set_autonomous_system;
1051 new_component->generic_routing.set_route = generic_set_route;
1052 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1053 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1054 new_component->generic_routing.get_route = full_get_route;
1055 new_component->generic_routing.get_onelink_routes =
1056 full_get_onelink_routes;
1057 new_component->generic_routing.get_bypass_route =
1058 generic_get_bypassroute;
1059 new_component->generic_routing.finalize = full_finalize;
1060 new_component->generic_routing.to_index = xbt_dict_new();
1061 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1062 new_component->generic_routing.parse_routes = xbt_dict_new();
1063 return new_component;
1066 static void model_full_load(void)
1068 /* use "surfxml_add_callback" to add a parse function call */
1071 static void model_full_unload(void)
1073 /* use "surfxml_del_callback" to remove a parse function call */
1076 static void model_full_end(void)
1080 const char *sep = "#";
1084 route_extended_t e_route;
1087 xbt_dict_cursor_t cursor = NULL;
1088 xbt_dynar_t keys = NULL;
1090 /* set utils vars */
1091 routing_component_full_t routing =
1092 ((routing_component_full_t) current_routing);
1093 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1095 /* Create the routing table */
1096 routing->routing_table =
1097 xbt_new0(route_extended_t, table_size * table_size);
1099 /* Put the routes in position */
1100 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1101 keys = xbt_str_split_str(key, sep);
1102 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1103 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1104 TO_ROUTE_FULL(src_id, dst_id) =
1105 generic_new_extended_route(current_routing->hierarchy, data, 1);
1106 xbt_dynar_free(&keys);
1109 /* delete the parse table */
1110 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1111 route = (route_t) data;
1112 xbt_dynar_free(&(route->link_list));
1116 /* delete parse dict */
1117 xbt_dict_free(&(routing->generic_routing.parse_routes));
1119 /* Add the loopback if needed */
1120 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1121 for (i = 0; i < table_size; i++) {
1122 e_route = TO_ROUTE_FULL(i, i);
1124 e_route = xbt_new0(s_route_extended_t, 1);
1125 e_route->src_gateway = NULL;
1126 e_route->dst_gateway = NULL;
1127 e_route->generic_route.link_list =
1128 xbt_dynar_new(global_routing->size_of_link, NULL);
1129 xbt_dynar_push(e_route->generic_route.link_list,
1130 &global_routing->loopback);
1131 TO_ROUTE_FULL(i, i) = e_route;
1136 /* Shrink the dynar routes (save unused slots) */
1137 for (i = 0; i < table_size; i++)
1138 for (j = 0; j < table_size; j++)
1139 if (TO_ROUTE_FULL(i, j))
1140 xbt_dynar_shrink(TO_ROUTE_FULL(i, j)->generic_route.link_list, 0);
1143 /* ************************************************************************** */
1144 /* *************************** FLOYD ROUTING ******************************** */
1146 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
1147 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
1148 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
1150 /* Routing model structure */
1153 s_routing_component_t generic_routing;
1154 /* vars for calculate the floyd algorith. */
1155 int *predecessor_table;
1156 route_extended_t *link_table; /* char* -> int* */
1157 } s_routing_component_floyd_t, *routing_component_floyd_t;
1159 static route_extended_t floyd_get_route(routing_component_t rc,
1160 const char *src, const char *dst);
1162 /* Business methods */
1163 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
1165 xbt_dynar_t ret = xbt_dynar_new(sizeof(onelink_t), xbt_free);
1167 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1168 //size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1169 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1170 char *k1, *d1, *k2, *d2;
1171 xbt_dict_foreach(routing->generic_routing.to_index, c1, k1, d1) {
1172 xbt_dict_foreach(routing->generic_routing.to_index, c2, k2, d2) {
1173 route_extended_t route = floyd_get_route(rc, k1, k2);
1175 if (xbt_dynar_length(route->generic_route.link_list) == 1) {
1177 *(void **) xbt_dynar_get_ptr(route->generic_route.link_list,
1179 onelink_t onelink = xbt_new0(s_onelink_t, 1);
1180 onelink->link_ptr = link;
1181 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE) {
1182 onelink->src = xbt_strdup(k1);
1183 onelink->dst = xbt_strdup(k2);
1184 } else if (routing->generic_routing.hierarchy ==
1185 SURF_ROUTING_RECURSIVE) {
1186 onelink->src = xbt_strdup(route->src_gateway);
1187 onelink->dst = xbt_strdup(route->dst_gateway);
1189 xbt_dynar_push(ret, &onelink);
1197 static route_extended_t floyd_get_route(routing_component_t rc,
1198 const char *src, const char *dst)
1200 xbt_assert1(rc && src
1202 "Invalid params for \"get_route\" function at AS \"%s\"",
1205 /* set utils vars */
1206 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1207 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1209 generic_src_dst_check(rc, src, dst);
1210 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1211 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1214 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1217 /* create a result route */
1218 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1219 new_e_route->generic_route.link_list =
1220 xbt_dynar_new(global_routing->size_of_link, NULL);
1221 new_e_route->src_gateway = NULL;
1222 new_e_route->dst_gateway = NULL;
1227 char *gw_src = NULL, *gw_dst =
1228 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1235 pred = TO_FLOYD_PRED(*src_id, pred);
1236 if (pred == -1) /* if no pred in route -> no route to host */
1238 xbt_assert2(TO_FLOYD_LINK(pred, prev_pred),
1239 "Invalid link for the route between \"%s\" or \"%s\"", src,
1242 prev_gw_src = gw_src;
1243 prev_gw_dst = gw_dst;
1245 route_extended_t e_route = TO_FLOYD_LINK(pred, prev_pred);
1246 gw_src = e_route->src_gateway;
1247 gw_dst = e_route->dst_gateway;
1252 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && !first
1253 && strcmp(gw_dst, prev_gw_src)) {
1254 xbt_dynar_t e_route_as_to_as =
1255 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1256 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1257 gw_dst, prev_gw_src);
1258 links = e_route_as_to_as;
1260 xbt_dynar_foreach(links, cpt, link) {
1261 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1267 links = e_route->generic_route.link_list;
1268 xbt_dynar_foreach(links, cpt, link) {
1269 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1273 } while (pred != *src_id);
1274 xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")",
1275 *src_id, *dst_id, src, dst);
1277 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1278 new_e_route->src_gateway = xbt_strdup(gw_src);
1279 new_e_route->dst_gateway = xbt_strdup(first_gw);
1285 static void floyd_finalize(routing_component_t rc)
1287 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1291 table_size = xbt_dict_length(routing->generic_routing.to_index);
1292 /* Delete link_table */
1293 for (i = 0; i < table_size; i++)
1294 for (j = 0; j < table_size; j++)
1295 generic_free_extended_route(TO_FLOYD_LINK(i, j));
1296 xbt_free(routing->link_table);
1297 /* Delete bypass dict */
1298 xbt_dict_free(&routing->generic_routing.bypassRoutes);
1299 /* Delete index dict */
1300 xbt_dict_free(&(routing->generic_routing.to_index));
1301 /* Delete dictionary index dict, predecessor and links table */
1302 xbt_free(routing->predecessor_table);
1303 /* Delete structure */
1308 static void *model_floyd_create(void)
1310 routing_component_floyd_t new_component =
1311 xbt_new0(s_routing_component_floyd_t, 1);
1312 new_component->generic_routing.set_processing_unit =
1313 generic_set_processing_unit;
1314 new_component->generic_routing.set_autonomous_system =
1315 generic_set_autonomous_system;
1316 new_component->generic_routing.set_route = generic_set_route;
1317 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1318 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1319 new_component->generic_routing.get_route = floyd_get_route;
1320 new_component->generic_routing.get_onelink_routes =
1321 floyd_get_onelink_routes;
1322 new_component->generic_routing.get_bypass_route =
1323 generic_get_bypassroute;
1324 new_component->generic_routing.finalize = floyd_finalize;
1325 new_component->generic_routing.to_index = xbt_dict_new();
1326 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1327 new_component->generic_routing.parse_routes = xbt_dict_new();
1328 return new_component;
1331 static void model_floyd_load(void)
1333 /* use "surfxml_add_callback" to add a parse function call */
1336 static void model_floyd_unload(void)
1338 /* use "surfxml_del_callback" to remove a parse function call */
1341 static void model_floyd_end(void)
1344 routing_component_floyd_t routing =
1345 ((routing_component_floyd_t) current_routing);
1346 xbt_dict_cursor_t cursor = NULL;
1348 char *key, *data, *end;
1349 const char *sep = "#";
1352 unsigned int i, j, a, b, c;
1354 /* set the size of inicial table */
1355 size_t table_size = xbt_dict_length(routing->generic_routing.to_index);
1357 /* Create Cost, Predecessor and Link tables */
1358 cost_table = xbt_new0(double, table_size * table_size); /* link cost from host to host */
1359 routing->predecessor_table = xbt_new0(int, table_size * table_size); /* predecessor host numbers */
1360 routing->link_table = xbt_new0(route_extended_t, table_size * table_size); /* actual link between src and dst */
1362 /* Initialize costs and predecessors */
1363 for (i = 0; i < table_size; i++)
1364 for (j = 0; j < table_size; j++) {
1365 TO_FLOYD_COST(i, j) = DBL_MAX;
1366 TO_FLOYD_PRED(i, j) = -1;
1367 TO_FLOYD_LINK(i, j) = NULL; /* fixed, missing in the previous version */
1370 /* Put the routes in position */
1371 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1372 keys = xbt_str_split_str(key, sep);
1373 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1374 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1375 TO_FLOYD_LINK(src_id, dst_id) =
1376 generic_new_extended_route(current_routing->hierarchy, data, 0);
1377 TO_FLOYD_PRED(src_id, dst_id) = src_id;
1379 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 */
1380 xbt_dynar_free(&keys);
1383 /* Add the loopback if needed */
1384 if (current_routing->hierarchy == SURF_ROUTING_BASE) {
1385 for (i = 0; i < table_size; i++) {
1386 route_extended_t e_route = TO_FLOYD_LINK(i, i);
1388 e_route = xbt_new0(s_route_extended_t, 1);
1389 e_route->src_gateway = NULL;
1390 e_route->dst_gateway = NULL;
1391 e_route->generic_route.link_list =
1392 xbt_dynar_new(global_routing->size_of_link, NULL);
1393 xbt_dynar_push(e_route->generic_route.link_list,
1394 &global_routing->loopback);
1395 TO_FLOYD_LINK(i, i) = e_route;
1396 TO_FLOYD_PRED(i, i) = i;
1397 TO_FLOYD_COST(i, i) = 1;
1401 /* Calculate path costs */
1402 for (c = 0; c < table_size; c++) {
1403 for (a = 0; a < table_size; a++) {
1404 for (b = 0; b < table_size; b++) {
1405 if (TO_FLOYD_COST(a, c) < DBL_MAX && TO_FLOYD_COST(c, b) < DBL_MAX) {
1406 if (TO_FLOYD_COST(a, b) == DBL_MAX ||
1407 (TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b) <
1408 TO_FLOYD_COST(a, b))) {
1409 TO_FLOYD_COST(a, b) =
1410 TO_FLOYD_COST(a, c) + TO_FLOYD_COST(c, b);
1411 TO_FLOYD_PRED(a, b) = TO_FLOYD_PRED(c, b);
1418 /* delete the parse table */
1419 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1420 route_t route = (route_t) data;
1421 xbt_dynar_free(&(route->link_list));
1425 /* delete parse dict */
1426 xbt_dict_free(&(routing->generic_routing.parse_routes));
1429 xbt_free(cost_table);
1432 /* ************************************************************************** */
1433 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1436 s_routing_component_t generic_routing;
1437 xbt_graph_t route_graph; /* xbt_graph */
1438 xbt_dict_t graph_node_map; /* map */
1439 xbt_dict_t route_cache; /* use in cache mode */
1441 } s_routing_component_dijkstra_t, *routing_component_dijkstra_t;
1444 typedef struct graph_node_data {
1446 int graph_id; /* used for caching internal graph id's */
1447 } s_graph_node_data_t, *graph_node_data_t;
1449 typedef struct graph_node_map_element {
1451 } s_graph_node_map_element_t, *graph_node_map_element_t;
1453 typedef struct route_cache_element {
1456 } s_route_cache_element_t, *route_cache_element_t;
1458 /* Free functions */
1460 static void route_cache_elem_free(void *e)
1462 route_cache_element_t elm = (route_cache_element_t) e;
1464 xbt_free(elm->pred_arr);
1469 static void graph_node_map_elem_free(void *e)
1471 graph_node_map_element_t elm = (graph_node_map_element_t) e;
1477 static void graph_edge_data_free(void *e)
1479 route_extended_t e_route = (route_extended_t) e;
1481 xbt_dynar_free(&(e_route->generic_route.link_list));
1482 if (e_route->src_gateway)
1483 xbt_free(e_route->src_gateway);
1484 if (e_route->dst_gateway)
1485 xbt_free(e_route->dst_gateway);
1490 /* Utility functions */
1492 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc,
1493 int id, int graph_id)
1495 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1496 xbt_node_t node = NULL;
1497 graph_node_data_t data = NULL;
1498 graph_node_map_element_t elm = NULL;
1500 data = xbt_new0(struct graph_node_data, 1);
1502 data->graph_id = graph_id;
1503 node = xbt_graph_new_node(routing->route_graph, data);
1505 elm = xbt_new0(struct graph_node_map_element, 1);
1507 xbt_dict_set_ext(routing->graph_node_map, (char *) (&id), sizeof(int),
1508 (xbt_set_elm_t) elm, &graph_node_map_elem_free);
1513 static graph_node_map_element_t
1514 graph_node_map_search(routing_component_dijkstra_t rc, int id)
1516 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1517 graph_node_map_element_t elm = (graph_node_map_element_t)
1518 xbt_dict_get_or_null_ext(routing->graph_node_map,
1526 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id,
1527 int dst_id, route_extended_t e_route)
1529 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1531 xbt_node_t src = NULL;
1532 xbt_node_t dst = NULL;
1533 graph_node_map_element_t src_elm = (graph_node_map_element_t)
1534 xbt_dict_get_or_null_ext(routing->graph_node_map,
1537 graph_node_map_element_t dst_elm = (graph_node_map_element_t)
1538 xbt_dict_get_or_null_ext(routing->graph_node_map,
1543 src = src_elm->node;
1546 dst = dst_elm->node;
1548 /* add nodes if they don't exist in the graph */
1549 if (src_id == dst_id && src == NULL && dst == NULL) {
1550 src = route_graph_new_node(rc, src_id, -1);
1554 src = route_graph_new_node(rc, src_id, -1);
1557 dst = route_graph_new_node(rc, dst_id, -1);
1561 /* add link as edge to graph */
1562 xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1565 static void add_loopback_dijkstra(routing_component_dijkstra_t rc)
1567 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1569 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1571 xbt_node_t node = NULL;
1572 unsigned int cursor2;
1573 xbt_dynar_foreach(nodes, cursor2, node) {
1574 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1575 xbt_edge_t edge = NULL;
1576 unsigned int cursor;
1579 xbt_dynar_foreach(out_edges, cursor, edge) {
1580 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1581 if (other_node == node) {
1588 route_extended_t e_route = xbt_new0(s_route_extended_t, 1);
1589 e_route->src_gateway = NULL;
1590 e_route->dst_gateway = NULL;
1591 e_route->generic_route.link_list =
1592 xbt_dynar_new(global_routing->size_of_link, NULL);
1593 xbt_dynar_push(e_route->generic_route.link_list,
1594 &global_routing->loopback);
1595 xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1600 /* Business methods */
1601 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
1603 xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
1606 static route_extended_t dijkstra_get_route(routing_component_t rc,
1610 xbt_assert1(rc && src
1612 "Invalid params for \"get_route\" function at AS \"%s\"",
1615 /* set utils vars */
1616 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1618 generic_src_dst_check(rc, src, dst);
1619 int *src_id = xbt_dict_get_or_null(routing->generic_routing.to_index, src);
1620 int *dst_id = xbt_dict_get_or_null(routing->generic_routing.to_index, dst);
1623 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
1626 /* create a result route */
1627 route_extended_t new_e_route = xbt_new0(s_route_extended_t, 1);
1628 new_e_route->generic_route.link_list =
1629 xbt_dynar_new(global_routing->size_of_link, NULL);
1630 new_e_route->src_gateway = NULL;
1631 new_e_route->dst_gateway = NULL;
1633 int *pred_arr = NULL;
1634 int src_node_id = 0;
1635 int dst_node_id = 0;
1638 route_extended_t e_route;
1642 xbt_dynar_t links = NULL;
1643 route_cache_element_t elm = NULL;
1644 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1646 /* Use the graph_node id mapping set to quickly find the nodes */
1647 graph_node_map_element_t src_elm =
1648 graph_node_map_search(routing, *src_id);
1649 graph_node_map_element_t dst_elm =
1650 graph_node_map_search(routing, *dst_id);
1651 xbt_assert2(src_elm != NULL
1652 && dst_elm != NULL, "src %d or dst %d does not exist",
1654 src_node_id = ((graph_node_data_t)
1655 xbt_graph_node_get_data(src_elm->node))->graph_id;
1656 dst_node_id = ((graph_node_data_t)
1657 xbt_graph_node_get_data(dst_elm->node))->graph_id;
1659 /* if the src and dst are the same *//* fixed, missing in the previous version */
1660 if (src_node_id == dst_node_id) {
1662 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1663 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1665 xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1667 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1670 e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1672 links = e_route->generic_route.link_list;
1673 xbt_dynar_foreach(links, cpt, link) {
1674 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1680 if (routing->cached) {
1681 /*check if there is a cached predecessor list avail */
1682 elm = (route_cache_element_t)
1683 xbt_dict_get_or_null_ext(routing->route_cache, (char *) (&src_id),
1687 if (elm) { /* cached mode and cache hit */
1688 pred_arr = elm->pred_arr;
1689 } else { /* not cached mode or cache miss */
1690 double *cost_arr = NULL;
1691 xbt_heap_t pqueue = NULL;
1694 int nr_nodes = xbt_dynar_length(nodes);
1695 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
1696 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
1697 pqueue = xbt_heap_new(nr_nodes, xbt_free);
1700 cost_arr[src_node_id] = 0.0;
1702 for (i = 0; i < nr_nodes; i++) {
1703 if (i != src_node_id) {
1704 cost_arr[i] = DBL_MAX;
1709 /* initialize priority queue */
1710 nodeid = xbt_new0(int, 1);
1712 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1716 /* apply dijkstra using the indexes from the graph's node array */
1717 while (xbt_heap_size(pqueue) > 0) {
1718 int *v_id = xbt_heap_pop(pqueue);
1719 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1720 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1721 xbt_edge_t edge = NULL;
1722 unsigned int cursor;
1724 xbt_dynar_foreach(out_edges, cursor, edge) {
1725 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1726 graph_node_data_t data = xbt_graph_node_get_data(u_node);
1727 int u_id = data->graph_id;
1728 route_extended_t tmp_e_route =
1729 (route_extended_t) xbt_graph_edge_get_data(edge);
1730 int cost_v_u = (tmp_e_route->generic_route.link_list)->used; /* count of links, old model assume 1 */
1732 if (cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1733 pred_arr[u_id] = *v_id;
1734 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1735 nodeid = xbt_new0(int, 1);
1737 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1741 /* free item popped from pqueue */
1746 xbt_heap_free(pqueue);
1749 /* compose route path with links */
1750 char *gw_src = NULL, *gw_dst =
1751 NULL, *prev_gw_src, *prev_gw_dst, *first_gw = NULL;
1753 for (v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1754 xbt_node_t node_pred_v =
1755 xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1756 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1758 xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1760 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id,
1763 prev_gw_src = gw_src;
1764 prev_gw_dst = gw_dst;
1766 e_route = (route_extended_t) xbt_graph_edge_get_data(edge);
1767 gw_src = e_route->src_gateway;
1768 gw_dst = e_route->dst_gateway;
1770 if (v == dst_node_id)
1773 if (rc->hierarchy == SURF_ROUTING_RECURSIVE && v != dst_node_id
1774 && strcmp(gw_dst, prev_gw_src)) {
1775 xbt_dynar_t e_route_as_to_as =
1776 (*(global_routing->get_route)) (gw_dst, prev_gw_src);
1777 xbt_assert2(e_route_as_to_as, "no route between \"%s\" and \"%s\"",
1778 gw_dst, prev_gw_src);
1779 links = e_route_as_to_as;
1781 xbt_dynar_foreach(links, cpt, link) {
1782 xbt_dynar_insert_at(new_e_route->generic_route.link_list, pos,
1788 links = e_route->generic_route.link_list;
1789 xbt_dynar_foreach(links, cpt, link) {
1790 xbt_dynar_unshift(new_e_route->generic_route.link_list, &link);
1795 if (rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1796 new_e_route->src_gateway = xbt_strdup(gw_src);
1797 new_e_route->dst_gateway = xbt_strdup(first_gw);
1800 if (routing->cached && elm == NULL) {
1801 /* add to predecessor list of the current src-host to cache */
1802 elm = xbt_new0(struct route_cache_element, 1);
1803 elm->pred_arr = pred_arr;
1805 xbt_dict_set_ext(routing->route_cache, (char *) (&src_id), sizeof(int),
1806 (xbt_set_elm_t) elm, &route_cache_elem_free);
1809 if (!routing->cached)
1815 static void dijkstra_finalize(routing_component_t rc)
1817 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1820 xbt_graph_free_graph(routing->route_graph, &xbt_free,
1821 &graph_edge_data_free, &xbt_free);
1822 xbt_dict_free(&routing->graph_node_map);
1823 if (routing->cached)
1824 xbt_dict_free(&routing->route_cache);
1825 /* Delete bypass dict */
1826 xbt_dict_free(&routing->generic_routing.bypassRoutes);
1827 /* Delete index dict */
1828 xbt_dict_free(&(routing->generic_routing.to_index));
1829 /* Delete structure */
1834 /* Creation routing model functions */
1836 static void *model_dijkstra_both_create(int cached)
1838 routing_component_dijkstra_t new_component =
1839 xbt_new0(s_routing_component_dijkstra_t, 1);
1840 new_component->generic_routing.set_processing_unit =
1841 generic_set_processing_unit;
1842 new_component->generic_routing.set_autonomous_system =
1843 generic_set_autonomous_system;
1844 new_component->generic_routing.set_route = generic_set_route;
1845 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1846 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1847 new_component->generic_routing.get_route = dijkstra_get_route;
1848 new_component->generic_routing.get_onelink_routes =
1849 dijkstra_get_onelink_routes;
1850 new_component->generic_routing.get_bypass_route =
1851 generic_get_bypassroute;
1852 new_component->generic_routing.finalize = dijkstra_finalize;
1853 new_component->cached = cached;
1854 new_component->generic_routing.to_index = xbt_dict_new();
1855 new_component->generic_routing.bypassRoutes = xbt_dict_new();
1856 new_component->generic_routing.parse_routes = xbt_dict_new();
1857 return new_component;
1860 static void *model_dijkstra_create(void)
1862 return model_dijkstra_both_create(0);
1865 static void *model_dijkstracache_create(void)
1867 return model_dijkstra_both_create(1);
1870 static void model_dijkstra_both_load(void)
1872 /* use "surfxml_add_callback" to add a parse function call */
1875 static void model_dijkstra_both_unload(void)
1877 /* use "surfxml_del_callback" to remove a parse function call */
1880 static void model_dijkstra_both_end(void)
1882 routing_component_dijkstra_t routing =
1883 (routing_component_dijkstra_t) current_routing;
1884 xbt_dict_cursor_t cursor = NULL;
1885 char *key, *data, *end;
1886 const char *sep = "#";
1888 xbt_node_t node = NULL;
1889 unsigned int cursor2;
1890 xbt_dynar_t nodes = NULL;
1894 /* Create the topology graph */
1895 routing->route_graph = xbt_graph_new_graph(1, NULL);
1896 routing->graph_node_map = xbt_dict_new();
1898 if (routing->cached)
1899 routing->route_cache = xbt_dict_new();
1901 /* Put the routes in position */
1902 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1903 keys = xbt_str_split_str(key, sep);
1904 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1905 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1906 route_extended_t e_route =
1907 generic_new_extended_route(current_routing->hierarchy, data, 0);
1908 route_new_dijkstra(routing, src_id, dst_id, e_route);
1909 xbt_dynar_free(&keys);
1912 /* delete the parse table */
1913 xbt_dict_foreach(routing->generic_routing.parse_routes, cursor, key, data) {
1914 route = (route_t) data;
1915 xbt_dynar_free(&(route->link_list));
1919 /* delete parse dict */
1920 xbt_dict_free(&(routing->generic_routing.parse_routes));
1922 /* Add the loopback if needed */
1923 if (current_routing->hierarchy == SURF_ROUTING_BASE)
1924 add_loopback_dijkstra(routing);
1926 /* initialize graph indexes in nodes after graph has been built */
1927 nodes = xbt_graph_get_nodes(routing->route_graph);
1929 xbt_dynar_foreach(nodes, cursor2, node) {
1930 graph_node_data_t data = xbt_graph_node_get_data(node);
1931 data->graph_id = cursor2;
1936 #ifdef HAVE_PCRE_LIB
1937 /* ************************************************** */
1938 /* ************** RULE-BASED ROUTING **************** */
1940 /* Routing model structure */
1943 s_routing_component_t generic_routing;
1944 xbt_dict_t dict_processing_units;
1945 xbt_dict_t dict_autonomous_systems;
1946 xbt_dynar_t list_route;
1947 xbt_dynar_t list_ASroute;
1948 } s_routing_component_rulebased_t, *routing_component_rulebased_t;
1950 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
1951 typedef struct s_rule_route_extended s_rule_route_extended_t,
1952 *rule_route_extended_t;
1954 struct s_rule_route {
1955 xbt_dynar_t re_str_link; // dynar of char*
1960 struct s_rule_route_extended {
1961 s_rule_route_t generic_rule_route;
1962 char *re_src_gateway;
1963 char *re_dst_gateway;
1966 static void rule_route_free(void *e)
1968 rule_route_t *elem = (rule_route_t *) (e);
1970 xbt_dynar_free(&(*elem)->re_str_link);
1971 pcre_free((*elem)->re_src);
1972 pcre_free((*elem)->re_dst);
1978 static void rule_route_extended_free(void *e)
1980 rule_route_extended_t *elem = (rule_route_extended_t *) e;
1982 xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
1983 pcre_free((*elem)->generic_rule_route.re_src);
1984 pcre_free((*elem)->generic_rule_route.re_dst);
1985 xbt_free((*elem)->re_src_gateway);
1986 xbt_free((*elem)->re_dst_gateway);
1991 /* Parse routing model functions */
1993 static void model_rulebased_set_processing_unit(routing_component_t rc,
1996 routing_component_rulebased_t routing =
1997 (routing_component_rulebased_t) rc;
1998 xbt_dict_set(routing->dict_processing_units, name, (void *) (-1), NULL);
2001 static void model_rulebased_set_autonomous_system(routing_component_t rc,
2004 routing_component_rulebased_t routing =
2005 (routing_component_rulebased_t) rc;
2006 xbt_dict_set(routing->dict_autonomous_systems, name, (void *) (-1),
2010 static void model_rulebased_set_route(routing_component_t rc,
2011 const char *src, const char *dst,
2014 routing_component_rulebased_t routing =
2015 (routing_component_rulebased_t) rc;
2016 rule_route_t ruleroute = xbt_new0(s_rule_route_t, 1);
2019 ruleroute->re_src = pcre_compile(src, 0, &error, &erroffset, NULL);
2020 xbt_assert3(ruleroute->re_src,
2021 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2022 erroffset, src, error);
2023 ruleroute->re_dst = pcre_compile(dst, 0, &error, &erroffset, NULL);
2024 xbt_assert3(ruleroute->re_src,
2025 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2026 erroffset, dst, error);
2027 ruleroute->re_str_link = route->link_list;
2028 xbt_dynar_push(routing->list_route, &ruleroute);
2032 static void model_rulebased_set_ASroute(routing_component_t rc,
2033 const char *src, const char *dst,
2034 route_extended_t route)
2036 routing_component_rulebased_t routing =
2037 (routing_component_rulebased_t) rc;
2038 rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t, 1);
2041 ruleroute_e->generic_rule_route.re_src =
2042 pcre_compile(src, 0, &error, &erroffset, NULL);
2043 xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2044 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2045 erroffset, src, error);
2046 ruleroute_e->generic_rule_route.re_dst =
2047 pcre_compile(dst, 0, &error, &erroffset, NULL);
2048 xbt_assert3(ruleroute_e->generic_rule_route.re_src,
2049 "PCRE compilation failed at offset %d (\"%s\"): %s\n",
2050 erroffset, dst, error);
2051 ruleroute_e->generic_rule_route.re_str_link =
2052 route->generic_route.link_list;
2053 ruleroute_e->re_src_gateway = route->src_gateway;
2054 ruleroute_e->re_dst_gateway = route->dst_gateway;
2055 xbt_dynar_push(routing->list_ASroute, &ruleroute_e);
2056 xbt_free(route->src_gateway);
2057 xbt_free(route->dst_gateway);
2061 static void model_rulebased_set_bypassroute(routing_component_t rc,
2064 route_extended_t e_route)
2066 xbt_die("bypass routing not supported for Route-Based model");
2069 #define BUFFER_SIZE 4096 /* result buffer size */
2070 #define OVECCOUNT 30 /* should be a multiple of 3 */
2072 static char *remplace(char *value, const char **src_list, int src_size,
2073 const char **dst_list, int dst_size)
2076 char result_result[BUFFER_SIZE];
2077 int i_result_buffer;
2078 int value_length = (int) strlen(value);
2082 i_result_buffer = 0;
2084 if (value[i] == '$') {
2088 int number_length = 0;
2089 while ('0' <= value[i + number_length]
2090 && value[i + number_length] <= '9') {
2093 xbt_assert2(number_length != 0,
2094 "bad string parameter, no number indication, at offset: %d (\"%s\")",
2098 number = atoi(value + i);
2099 i = i + number_length;
2100 xbt_assert2(i + 2 < value_length,
2101 "bad string parameter, too few chars, at offset: %d (\"%s\")",
2104 // solve the indication
2105 const char **param_list;
2107 if (value[i] == 's' && value[i + 1] == 'r' && value[i + 2] == 'c') {
2108 param_list = src_list;
2109 param_size = src_size;
2110 } else if (value[i] == 'd' && value[i + 1] == 's'
2111 && value[i + 2] == 't') {
2112 param_list = dst_list;
2113 param_size = dst_size;
2116 "bad string parameter, support only \"src\" and \"dst\", at offset: %d (\"%s\")",
2121 xbt_assert4(param_size >= number,
2122 "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",
2123 i, value, param_size, number);
2125 const char *param = param_list[number];
2126 int size = strlen(param);
2128 for (cp = 0; cp < size; cp++) {
2129 result_result[i_result_buffer] = param[cp];
2131 if (i_result_buffer >= BUFFER_SIZE)
2135 result_result[i_result_buffer] = value[i];
2140 } while (i < value_length && i_result_buffer < BUFFER_SIZE);
2142 xbt_assert2(i_result_buffer < BUFFER_SIZE,
2143 "solving string \"%s\", small buffer size (%d)", value,
2145 result_result[i_result_buffer] = 0;
2146 return xbt_strdup(result_result);
2149 static route_extended_t rulebased_get_route(routing_component_t rc,
2152 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
2154 xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
2155 routing_component_rulebased_t routing = (routing_component_rulebased_t)rc;
2157 xbt_dict_cursor_t c1 = NULL;
2161 char *router = NULL;
2162 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2163 if (strstr (k1, "router")){
2168 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2171 xbt_dict_foreach(routing->dict_processing_units, c1, k1, d1) {
2172 route_extended_t route = rulebased_get_route (rc, router, k1);
2174 int number_of_links = xbt_dynar_length(route->generic_route.link_list);
2175 if (number_of_links != 3) {
2176 xbt_die ("rulebased_get_onelink_routes works only if the AS is a cluster, sorry.");
2180 xbt_dynar_get_cpy (route->generic_route.link_list, 2, &link_ptr);
2181 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
2182 onelink->src = xbt_strdup (k1);
2183 onelink->dst = xbt_strdup (router);
2184 onelink->link_ptr = link_ptr;
2185 xbt_dynar_push (ret, &onelink);
2190 /* Business methods */
2191 static route_extended_t rulebased_get_route(routing_component_t rc,
2195 xbt_assert1(rc && src
2197 "Invalid params for \"get_route\" function at AS \"%s\"",
2200 /* set utils vars */
2201 routing_component_rulebased_t routing =
2202 (routing_component_rulebased_t) rc;
2204 int are_processing_units;
2205 xbt_dynar_t rule_list;
2206 if (xbt_dict_get_or_null(routing->dict_processing_units, src)
2207 && xbt_dict_get_or_null(routing->dict_processing_units, dst)) {
2208 are_processing_units = 1;
2209 rule_list = routing->list_route;
2210 } else if (xbt_dict_get_or_null(routing->dict_autonomous_systems, src)
2211 && xbt_dict_get_or_null(routing->dict_autonomous_systems,
2213 are_processing_units = 0;
2214 rule_list = routing->list_ASroute;
2217 "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",
2222 int src_length = (int) strlen(src);
2223 int dst_length = (int) strlen(dst);
2225 xbt_dynar_t links_list =
2226 xbt_dynar_new(global_routing->size_of_link, NULL);
2228 rule_route_t ruleroute;
2230 int ovector_src[OVECCOUNT];
2231 int ovector_dst[OVECCOUNT];
2232 const char **list_src = NULL;
2233 const char **list_dst = NULL;
2234 xbt_dynar_foreach(rule_list, cpt, ruleroute) {
2236 pcre_exec(ruleroute->re_src, NULL, src, src_length, 0, 0,
2237 ovector_src, OVECCOUNT);
2240 pcre_exec(ruleroute->re_dst, NULL, dst, dst_length, 0, 0,
2241 ovector_dst, OVECCOUNT);
2243 xbt_assert1(!pcre_get_substring_list
2244 (src, ovector_src, rc_src, &list_src),
2245 "error solving substring list for src \"%s\"", src);
2246 xbt_assert1(!pcre_get_substring_list
2247 (dst, ovector_dst, rc_dst, &list_dst),
2248 "error solving substring list for src \"%s\"", dst);
2250 xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
2251 char *new_link_name =
2252 remplace(link_name, list_src, rc_src, list_dst, rc_dst);
2254 xbt_dict_get_or_null(surf_network_model->resource_set,
2257 xbt_dynar_push(links_list, &link);
2259 THROW1(mismatch_error, 0, "Link %s not found", new_link_name);
2260 xbt_free(new_link_name);
2264 if (rc_src >= 0 && rc_dst >= 0)
2268 route_extended_t new_e_route = NULL;
2269 if (rc_src >= 0 && rc_dst >= 0) {
2270 new_e_route = xbt_new0(s_route_extended_t, 1);
2271 new_e_route->generic_route.link_list = links_list;
2272 } else if (!strcmp(src, dst) && are_processing_units) {
2273 new_e_route = xbt_new0(s_route_extended_t, 1);
2274 xbt_dynar_push(links_list, &(global_routing->loopback));
2275 new_e_route->generic_route.link_list = links_list;
2277 xbt_dynar_free(&link_list);
2280 if (!are_processing_units && new_e_route) {
2281 rule_route_extended_t ruleroute_extended =
2282 (rule_route_extended_t) ruleroute;
2283 new_e_route->src_gateway =
2284 remplace(ruleroute_extended->re_src_gateway, list_src, rc_src,
2286 new_e_route->dst_gateway =
2287 remplace(ruleroute_extended->re_dst_gateway, list_src, rc_src,
2292 pcre_free_substring_list(list_src);
2294 pcre_free_substring_list(list_dst);
2299 static route_extended_t rulebased_get_bypass_route(routing_component_t rc,
2306 static void rulebased_finalize(routing_component_t rc)
2308 routing_component_rulebased_t routing =
2309 (routing_component_rulebased_t) rc;
2311 xbt_dict_free(&routing->dict_processing_units);
2312 xbt_dict_free(&routing->dict_autonomous_systems);
2313 xbt_dynar_free(&routing->list_route);
2314 xbt_dynar_free(&routing->list_ASroute);
2315 /* Delete structure */
2320 /* Creation routing model functions */
2321 static void *model_rulebased_create(void)
2323 routing_component_rulebased_t new_component =
2324 xbt_new0(s_routing_component_rulebased_t, 1);
2325 new_component->generic_routing.set_processing_unit =
2326 model_rulebased_set_processing_unit;
2327 new_component->generic_routing.set_autonomous_system =
2328 model_rulebased_set_autonomous_system;
2329 new_component->generic_routing.set_route = model_rulebased_set_route;
2330 new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
2331 new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
2332 new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
2333 new_component->generic_routing.get_route = rulebased_get_route;
2334 new_component->generic_routing.get_bypass_route = generic_get_bypassroute; //rulebased_get_bypass_route;
2335 new_component->generic_routing.finalize = rulebased_finalize;
2336 /* initialization of internal structures */
2337 new_component->dict_processing_units = xbt_dict_new();
2338 new_component->dict_autonomous_systems = xbt_dict_new();
2339 new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
2340 new_component->list_ASroute =
2341 xbt_dynar_new(sizeof(rule_route_extended_t),
2342 &rule_route_extended_free);
2343 return new_component;
2346 static void model_rulebased_load(void)
2348 /* use "surfxml_add_callback" to add a parse function call */
2351 static void model_rulebased_unload(void)
2353 /* use "surfxml_del_callback" to remove a parse function call */
2356 static void model_rulebased_end(void)
2360 #endif /* HAVE_PCRE_LIB */
2362 /* ************************************************************************** */
2363 /* ******************************* NO ROUTING ******************************* */
2365 /* Routing model structure */
2367 s_routing_component_t generic_routing;
2368 } s_routing_component_none_t, *routing_component_none_t;
2370 /* Business methods */
2371 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc)
2376 static route_extended_t none_get_route(routing_component_t rc,
2377 const char *src, const char *dst)
2382 static route_extended_t none_get_bypass_route(routing_component_t rc,
2389 static void none_finalize(routing_component_t rc)
2394 static void none_set_processing_unit(routing_component_t rc,
2399 static void none_set_autonomous_system(routing_component_t rc,
2404 /* Creation routing model functions */
2405 static void *model_none_create(void)
2407 routing_component_none_t new_component =
2408 xbt_new0(s_routing_component_none_t, 1);
2409 new_component->generic_routing.set_processing_unit =
2410 none_set_processing_unit;
2411 new_component->generic_routing.set_autonomous_system =
2412 none_set_autonomous_system;
2413 new_component->generic_routing.set_route = NULL;
2414 new_component->generic_routing.set_ASroute = NULL;
2415 new_component->generic_routing.set_bypassroute = NULL;
2416 new_component->generic_routing.get_route = none_get_route;
2417 new_component->generic_routing.get_onelink_routes =
2418 none_get_onelink_routes;
2419 new_component->generic_routing.get_bypass_route = none_get_bypass_route;
2420 new_component->generic_routing.finalize = none_finalize;
2421 return new_component;
2424 static void model_none_load(void)
2428 static void model_none_unload(void)
2432 static void model_none_end(void)
2436 /* ************************************************** */
2437 /* ********** PATERN FOR NEW ROUTING **************** */
2439 /* The minimal configuration of a new routing model need the next functions,
2440 * also you need to set at the start of the file, the new model in the model
2441 * list. Remember keep the null ending of the list.
2443 /*** Routing model structure ***/
2445 // s_routing_component_t generic_routing;
2446 // /* things that your routing model need */
2447 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2449 /*** Parse routing model functions ***/
2450 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2451 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2452 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2453 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2454 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2456 /*** Business methods ***/
2457 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2458 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2459 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2461 /*** Creation routing model functions ***/
2462 // static void* model_NEW_create(void) {
2463 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
2464 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2465 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2466 // new_component->generic_routing.set_route = model_NEW_set_route;
2467 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2468 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2469 // new_component->generic_routing.get_route = NEW_get_route;
2470 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2471 // new_component->generic_routing.finalize = NEW_finalize;
2472 // /* initialization of internal structures */
2473 // return new_component;
2474 // } /* mandatory */
2475 // static void model_NEW_load(void) {} /* mandatory */
2476 // static void model_NEW_unload(void) {} /* mandatory */
2477 // static void model_NEW_end(void) {} /* mandatory */
2479 /* ************************************************************************** */
2480 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2482 static void generic_set_processing_unit(routing_component_t rc,
2485 DEBUG1("Load process unit \"%s\"", name);
2486 int *id = xbt_new0(int, 1);
2487 xbt_dict_t _to_index;
2488 _to_index = current_routing->to_index;
2489 *id = xbt_dict_length(_to_index);
2490 xbt_dict_set(_to_index, name, id, xbt_free);
2493 static void generic_set_autonomous_system(routing_component_t rc,
2496 DEBUG1("Load Autonomous system \"%s\"", name);
2497 int *id = xbt_new0(int, 1);
2498 xbt_dict_t _to_index;
2499 _to_index = current_routing->to_index;
2500 *id = xbt_dict_length(_to_index);
2501 xbt_dict_set(_to_index, name, id, xbt_free);
2504 static void generic_set_route(routing_component_t rc, const char *src,
2505 const char *dst, route_t route)
2507 DEBUG2("Load Route from \"%s\" to \"%s\"", src, dst);
2508 xbt_dict_t _parse_routes;
2509 xbt_dict_t _to_index;
2511 int *src_id, *dst_id;
2512 unsigned long nb_links = xbt_dynar_length(route->link_list);
2514 _to_index = current_routing->to_index;
2516 _parse_routes = current_routing->parse_routes;
2518 src_id = xbt_dict_get_or_null(_to_index, src);
2519 dst_id = xbt_dict_get_or_null(_to_index, dst);
2522 && dst_id, "Network elements %s or %s not found", src, dst);
2523 route_name = bprintf("%d#%d", *src_id, *dst_id);
2525 xbt_assert2(xbt_dynar_length(route->link_list) > 0,
2526 "Invalid count of links, must be greater than zero (%s,%s)",
2529 route_t route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2531 xbt_assert2(!xbt_dynar_compare(
2532 (void*)route->link_list,
2533 (void*)route_to_test->link_list,
2534 (int_f_cpvoid_cpvoid_t) strcmp),
2535 "The route between \"%s\" and \"%s\" already exists", src,dst);
2537 xbt_dict_set(_parse_routes, route_name, route, NULL);
2539 xbt_free(route_name);
2541 if (A_surfxml_route_symetrical == A_surfxml_route_symetrical_YES) {
2543 route_t route_sym = xbt_new0(s_route_t, 1);
2544 route_sym->link_list = xbt_dynar_new(sizeof(char *),NULL);
2545 for(i=nb_links ; i>0 ; i--) {
2546 char *link_name = xbt_strdup(xbt_dynar_get_as(route->link_list, i-1, char *));
2547 xbt_dynar_push_as(route_sym->link_list ,char *, link_name);
2549 DEBUG2("Load Route from \"%s\" to \"%s\"", dst, src);
2550 route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d",*dst_id, *src_id));
2552 xbt_assert2(!xbt_dynar_compare(
2553 (void*)route_sym->link_list,
2554 (void*)route_to_test->link_list,
2555 (int_f_cpvoid_cpvoid_t) strcmp),
2556 "The route between \"%s\" and \"%s\" already exists", dst,src);
2558 xbt_dict_set(_parse_routes, bprintf("%d#%d",*dst_id, *src_id), route_sym, NULL);
2562 static void generic_set_ASroute(routing_component_t rc, const char *src,
2563 const char *dst, route_extended_t e_route)
2565 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"", src,
2566 e_route->src_gateway, dst, e_route->dst_gateway);
2568 xbt_dict_t _parse_routes;
2569 xbt_dict_t _to_index;
2571 int *src_id, *dst_id;
2572 _to_index = current_routing->to_index;
2573 _parse_routes = current_routing->parse_routes;
2576 src_id = xbt_dict_get_or_null(_to_index, src);
2577 dst_id = xbt_dict_get_or_null(_to_index, dst);
2580 && dst_id, "Network elements %s or %s not found", src, dst);
2581 route_name = bprintf("%d#%d", *src_id, *dst_id);
2583 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2584 "Invalid count of links, must be greater than zero (%s,%s)",
2587 route_t route_to_test = xbt_dict_get_or_null(_parse_routes, route_name);
2589 xbt_assert4(!xbt_dynar_compare(
2590 (void*) (&e_route->generic_route)->link_list,
2591 (void*) route_to_test->link_list,
2592 (int_f_cpvoid_cpvoid_t) strcmp),
2593 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
2594 src, e_route->src_gateway, dst, e_route->dst_gateway);
2596 xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2598 xbt_free(route_name);
2600 unsigned long nb_links = xbt_dynar_length(e_route->generic_route.link_list);
2601 if (A_surfxml_ASroute_symetrical == A_surfxml_ASroute_symetrical_YES) {
2603 route_extended_t route_sym = xbt_new0(s_route_extended_t, 1);
2604 route_sym->generic_route.link_list = xbt_dynar_new(sizeof(char *),NULL);
2605 for(i=nb_links ; i>0 ; i--) {
2606 char *link_name = bprintf("%s",xbt_dynar_get_as(e_route->generic_route.link_list, i-1, char *));
2607 xbt_dynar_push_as(route_sym->generic_route.link_list ,char *, link_name);
2609 route_sym->src_gateway = xbt_new0( char,strlen(e_route->dst_gateway) );
2610 route_sym->src_gateway = bprintf("%s",e_route->dst_gateway);
2611 route_sym->dst_gateway = xbt_new0( char,strlen(e_route->src_gateway) );
2612 route_sym->dst_gateway = bprintf("%s",e_route->src_gateway);
2613 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",dst, route_sym->src_gateway,src,route_sym->dst_gateway);
2615 route_to_test = xbt_dict_get_or_null(_parse_routes, bprintf("%d#%d", *dst_id, *src_id));
2617 xbt_assert4(!xbt_dynar_compare(
2618 (void*) (&route_sym->generic_route)->link_list,
2619 (void*) route_to_test->link_list,
2620 (int_f_cpvoid_cpvoid_t) strcmp),
2621 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
2622 dst, route_sym->src_gateway, src, route_sym->dst_gateway);
2624 xbt_dict_set(_parse_routes, bprintf("%d#%d", *dst_id, *src_id), route_sym, NULL);
2628 static void generic_set_bypassroute(routing_component_t rc,
2629 const char *src, const char *dst,
2630 route_extended_t e_route)
2632 DEBUG2("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
2633 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2636 route_name = bprintf("%s#%s", src, dst);
2637 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list) > 0,
2638 "Invalid count of links, must be greater than zero (%s,%s)",
2640 xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
2641 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",
2642 src, e_route->src_gateway, dst, e_route->dst_gateway);
2644 route_extended_t new_e_route =
2645 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
2646 xbt_dynar_free(&(e_route->generic_route.link_list));
2649 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route,
2650 (void (*)(void *)) generic_free_extended_route);
2651 xbt_free(route_name);
2654 /* ************************************************************************** */
2655 /* *********************** GENERIC BUSINESS METHODS ************************* */
2657 static xbt_dynar_t generic_get_onelink_routes(routing_component_t rc)
2659 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2662 static route_extended_t generic_get_bypassroute(routing_component_t rc,
2666 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
2667 routing_component_t src_as, dst_as;
2668 int index_src, index_dst;
2669 xbt_dynar_t path_src = NULL;
2670 xbt_dynar_t path_dst = NULL;
2671 routing_component_t current = NULL;
2672 routing_component_t *current_src = NULL;
2673 routing_component_t *current_dst = NULL;
2675 /* (1) find the as where the src and dst are located */
2676 src_as = ((network_element_info_t)
2677 xbt_dict_get_or_null(global_routing->where_network_elements,
2678 src))->rc_component;
2679 dst_as = ((network_element_info_t)
2680 xbt_dict_get_or_null(global_routing->where_network_elements,
2681 dst))->rc_component;
2684 "Ask for route \"from\"(%s) or \"to\"(%s) no found", src,
2687 /* (2) find the path to the root routing component */
2688 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2690 while (current != NULL) {
2691 xbt_dynar_push(path_src, ¤t);
2692 current = current->routing_father;
2694 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2696 while (current != NULL) {
2697 xbt_dynar_push(path_dst, ¤t);
2698 current = current->routing_father;
2701 /* (3) find the common father */
2702 index_src = (path_src->used) - 1;
2703 index_dst = (path_dst->used) - 1;
2704 current_src = xbt_dynar_get_ptr(path_src, index_src);
2705 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2706 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
2707 routing_component_t *tmp_src, *tmp_dst;
2708 tmp_src = xbt_dynar_pop_ptr(path_src);
2709 tmp_dst = xbt_dynar_pop_ptr(path_dst);
2712 current_src = xbt_dynar_get_ptr(path_src, index_src);
2713 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
2716 int max_index_src = (path_src->used) - 1;
2717 int max_index_dst = (path_dst->used) - 1;
2719 int max_index = max(max_index_src, max_index_dst);
2722 route_extended_t e_route_bypass = NULL;
2724 for (max = 0; max <= max_index; max++) {
2725 for (i = 0; i < max; i++) {
2726 if (i <= max_index_src && max <= max_index_dst) {
2727 char *route_name = bprintf("%s#%s",
2728 (*(routing_component_t *)
2730 (path_src, i)))->name,
2731 (*(routing_component_t *)
2733 (path_dst, max)))->name);
2735 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2736 xbt_free(route_name);
2740 if (max <= max_index_src && i <= max_index_dst) {
2741 char *route_name = bprintf("%s#%s",
2742 (*(routing_component_t *)
2744 (path_src, max)))->name,
2745 (*(routing_component_t *)
2747 (path_dst, i)))->name);
2749 xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2750 xbt_free(route_name);
2759 if (max <= max_index_src && max <= max_index_dst) {
2760 char *route_name = bprintf("%s#%s",
2761 (*(routing_component_t *)
2763 (path_src, max)))->name,
2764 (*(routing_component_t *)
2766 (path_dst, max)))->name);
2767 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
2768 xbt_free(route_name);
2774 xbt_dynar_free(&path_src);
2775 xbt_dynar_free(&path_dst);
2777 route_extended_t new_e_route = NULL;
2779 if (e_route_bypass) {
2781 unsigned int cpt = 0;
2782 new_e_route = xbt_new0(s_route_extended_t, 1);
2783 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2784 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2785 new_e_route->generic_route.link_list =
2786 xbt_dynar_new(global_routing->size_of_link, NULL);
2787 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2788 xbt_dynar_push(new_e_route->generic_route.link_list, &link);
2795 /* ************************************************************************** */
2796 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2798 static route_extended_t
2799 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
2800 void *data, int order)
2804 route_extended_t e_route, new_e_route;
2807 xbt_dynar_t links = NULL, links_id = NULL;
2809 new_e_route = xbt_new0(s_route_extended_t, 1);
2810 new_e_route->generic_route.link_list =
2811 xbt_dynar_new(global_routing->size_of_link, NULL);
2812 new_e_route->src_gateway = NULL;
2813 new_e_route->dst_gateway = NULL;
2815 xbt_assert0(hierarchy == SURF_ROUTING_BASE
2816 || hierarchy == SURF_ROUTING_RECURSIVE,
2817 "the hierarchy type is not defined");
2819 if (hierarchy == SURF_ROUTING_BASE) {
2821 route = (route_t) data;
2822 links = route->link_list;
2824 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
2826 e_route = (route_extended_t) data;
2827 xbt_assert0(e_route->src_gateway
2828 && e_route->dst_gateway, "bad gateway, is null");
2829 links = e_route->generic_route.link_list;
2831 /* remeber not erase the gateway names */
2832 new_e_route->src_gateway = e_route->src_gateway;
2833 new_e_route->dst_gateway = e_route->dst_gateway;
2836 links_id = new_e_route->generic_route.link_list;
2838 xbt_dynar_foreach(links, cpt, link_name) {
2841 xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2844 xbt_dynar_push(links_id, &link);
2846 xbt_dynar_unshift(links_id, &link);
2848 THROW1(mismatch_error, 0, "Link %s not found", link_name);
2854 static void generic_free_route(route_t route)
2857 xbt_dynar_free(&(route->link_list));
2862 static void generic_free_extended_route(route_extended_t e_route)
2865 xbt_dynar_free(&(e_route->generic_route.link_list));
2866 if (e_route->src_gateway)
2867 xbt_free(e_route->src_gateway);
2868 if (e_route->dst_gateway)
2869 xbt_free(e_route->dst_gateway);
2874 static routing_component_t generic_as_exist(routing_component_t find_from,
2875 routing_component_t to_find)
2877 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2878 xbt_dict_cursor_t cursor = NULL;
2881 routing_component_t elem;
2882 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2883 if (to_find == elem || generic_as_exist(elem, to_find)) {
2893 static routing_component_t
2894 generic_autonomous_system_exist(routing_component_t rc, char *element)
2896 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2897 routing_component_t element_as, result, elem;
2898 xbt_dict_cursor_t cursor = NULL;
2900 element_as = ((network_element_info_t)
2901 xbt_dict_get_or_null
2902 (global_routing->where_network_elements,
2903 element))->rc_component;
2904 result = ((routing_component_t) - 1);
2905 if (element_as != rc)
2906 result = generic_as_exist(rc, element_as);
2910 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2911 found = !strcmp(elem->name, element);
2921 static routing_component_t
2922 generic_processing_units_exist(routing_component_t rc, char *element)
2924 routing_component_t element_as;
2925 element_as = ((network_element_info_t)
2926 xbt_dict_get_or_null
2927 (global_routing->where_network_elements,
2928 element))->rc_component;
2929 if (element_as == rc)
2931 return generic_as_exist(rc, element_as);
2934 static void generic_src_dst_check(routing_component_t rc, const char *src,
2938 routing_component_t src_as = ((network_element_info_t)
2939 xbt_dict_get_or_null
2940 (global_routing->where_network_elements,
2941 src))->rc_component;
2942 routing_component_t dst_as = ((network_element_info_t)
2943 xbt_dict_get_or_null
2944 (global_routing->where_network_elements,
2945 dst))->rc_component;
2947 xbt_assert3(src_as != NULL && dst_as != NULL,
2948 "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
2949 src, dst, rc->name);
2950 xbt_assert4(src_as == dst_as,
2951 "The src(%s in %s) and dst(%s in %s) are in differents AS",
2952 src, src_as->name, dst, dst_as->name);
2953 xbt_assert2(rc == dst_as,
2954 "The routing component of src and dst is not the same as the network elements belong (%s==%s)",
2955 rc->name, dst_as->name);
2958 static void routing_parse_Sconfig(void)
2961 DEBUG0("WARNING tag config not yet implemented.");
2962 DEBUG1("Configuration name = %s",A_surfxml_config_id);
2965 static void routing_parse_Econfig(void)
2968 xbt_dict_cursor_t cursor = NULL;
2971 xbt_dict_foreach(current_property_set, cursor, key, elem) {
2972 DEBUG2("property : %s = %s",key,elem);
2976 static void routing_parse_Scluster(void)
2978 static int AX_ptr = 0;
2980 char *cluster_id = A_surfxml_cluster_id;
2981 char *cluster_prefix = A_surfxml_cluster_prefix;
2982 char *cluster_suffix = A_surfxml_cluster_suffix;
2983 char *cluster_radical = A_surfxml_cluster_radical;
2984 char *cluster_power = A_surfxml_cluster_power;
2985 char *cluster_bw = A_surfxml_cluster_bw;
2986 char *cluster_lat = A_surfxml_cluster_lat;
2987 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2988 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2989 char *host_id, *groups, *link_id = NULL;
2990 char *router_id, *link_router, *link_backbone, *route_src_dst;
2993 xbt_dynar_t radical_elements;
2994 xbt_dynar_t radical_ends;
2995 int cluster_sharing_policy = AX_surfxml_cluster_sharing_policy;
2996 int cluster_bb_sharing_policy = AX_surfxml_cluster_bb_sharing_policy;
2998 #ifndef HAVE_PCRE_LIB
2999 xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
3000 char *route_src, *route_dst;
3004 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
3005 static unsigned int surfxml_buffer_stack_stack[1024];
3007 surfxml_buffer_stack_stack[0] = 0;
3009 surfxml_bufferstack_push(1);
3011 SURFXML_BUFFER_SET(AS_id, cluster_id);
3012 #ifdef HAVE_PCRE_LIB
3013 SURFXML_BUFFER_SET(AS_routing, "RuleBased");
3014 DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">", cluster_id);
3016 SURFXML_BUFFER_SET(AS_routing, "Full");
3017 DEBUG1("<AS id=\"%s\"\trouting=\"Full\">", cluster_id);
3019 SURFXML_START_TAG(AS);
3021 radical_elements = xbt_str_split(cluster_radical, ",");
3022 xbt_dynar_foreach(radical_elements, iter, groups) {
3023 radical_ends = xbt_str_split(groups, "-");
3024 switch (xbt_dynar_length(radical_ends)) {
3026 surf_parse_get_int(&start,
3027 xbt_dynar_get_as(radical_ends, 0, char *));
3028 host_id = bprintf("%s%d%s", cluster_prefix, start, cluster_suffix);
3029 #ifndef HAVE_PCRE_LIB
3030 xbt_dynar_push_as(tab_elements_num, int, start);
3032 link_id = bprintf("%s_link_%d", cluster_id, start);
3034 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3035 A_surfxml_host_state = A_surfxml_host_state_ON;
3036 SURFXML_BUFFER_SET(host_id, host_id);
3037 SURFXML_BUFFER_SET(host_power, cluster_power);
3038 SURFXML_BUFFER_SET(host_availability, "1.0");
3039 SURFXML_BUFFER_SET(host_availability_file, "");
3040 SURFXML_BUFFER_SET(host_state_file, "");
3041 SURFXML_START_TAG(host);
3042 SURFXML_END_TAG(host);
3044 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3045 A_surfxml_link_state = A_surfxml_link_state_ON;
3046 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3047 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3048 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3049 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3050 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3051 SURFXML_BUFFER_SET(link_id, link_id);
3052 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3053 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3054 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3055 SURFXML_BUFFER_SET(link_latency_file, "");
3056 SURFXML_BUFFER_SET(link_state_file, "");
3057 SURFXML_START_TAG(link);
3058 SURFXML_END_TAG(link);
3064 surf_parse_get_int(&start,
3065 xbt_dynar_get_as(radical_ends, 0, char *));
3066 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
3067 DEBUG2("Create hosts and links from %d to %d", start, end);
3068 for (i = start; i <= end; i++) {
3069 host_id = bprintf("%s%d%s", cluster_prefix, i, cluster_suffix);
3070 #ifndef HAVE_PCRE_LIB
3071 xbt_dynar_push_as(tab_elements_num, int, i);
3073 link_id = bprintf("%s_link_%d", cluster_id, i);
3075 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>", host_id, cluster_power);
3076 A_surfxml_host_state = A_surfxml_host_state_ON;
3077 SURFXML_BUFFER_SET(host_id, host_id);
3078 SURFXML_BUFFER_SET(host_power, cluster_power);
3079 SURFXML_BUFFER_SET(host_availability, "1.0");
3080 SURFXML_BUFFER_SET(host_availability_file, "");
3081 SURFXML_BUFFER_SET(host_state_file, "");
3082 SURFXML_START_TAG(host);
3083 SURFXML_END_TAG(host);
3085 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>", link_id,cluster_bw, cluster_lat);
3086 A_surfxml_link_state = A_surfxml_link_state_ON;
3087 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3088 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3089 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3090 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3091 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3092 SURFXML_BUFFER_SET(link_id, link_id);
3093 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3094 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3095 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3096 SURFXML_BUFFER_SET(link_latency_file, "");
3097 SURFXML_BUFFER_SET(link_state_file, "");
3098 SURFXML_START_TAG(link);
3099 SURFXML_END_TAG(link);
3104 DEBUG0("Malformed radical");
3107 xbt_dynar_free(&radical_ends);
3112 bprintf("%s%s_router%s", cluster_prefix, cluster_id,
3114 link_router = bprintf("%s_link_%s_router", cluster_id, cluster_id);
3115 link_backbone = bprintf("%s_backbone", cluster_id);
3117 DEBUG1("<router id=\"%s\"/>", router_id);
3118 SURFXML_BUFFER_SET(router_id, router_id);;
3119 SURFXML_START_TAG(router);
3120 SURFXML_END_TAG(router);
3122 DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_router,cluster_bw, cluster_lat);
3123 A_surfxml_link_state = A_surfxml_link_state_ON;
3124 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3125 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3126 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FULLDUPLEX;}
3127 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FATPIPE)
3128 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3129 SURFXML_BUFFER_SET(link_id, link_router);
3130 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
3131 SURFXML_BUFFER_SET(link_latency, cluster_lat);
3132 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3133 SURFXML_BUFFER_SET(link_latency_file, "");
3134 SURFXML_BUFFER_SET(link_state_file, "");
3135 SURFXML_START_TAG(link);
3136 SURFXML_END_TAG(link);
3138 DEBUG3("<link\tid=\"%s\" bw=\"%s\" lat=\"%s\"/>", link_backbone,cluster_bw, cluster_lat);
3139 A_surfxml_link_state = A_surfxml_link_state_ON;
3140 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
3141 if(cluster_bb_sharing_policy == A_surfxml_cluster_bb_sharing_policy_FATPIPE)
3142 {A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;}
3143 SURFXML_BUFFER_SET(link_id, link_backbone);
3144 SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
3145 SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
3146 SURFXML_BUFFER_SET(link_bandwidth_file, "");
3147 SURFXML_BUFFER_SET(link_latency_file, "");
3148 SURFXML_BUFFER_SET(link_state_file, "");
3149 SURFXML_START_TAG(link);
3150 SURFXML_END_TAG(link);
3152 char *new_suffix = bprintf("%s", "");
3154 radical_elements = xbt_str_split(cluster_suffix, ".");
3155 xbt_dynar_foreach(radical_elements, iter, groups) {
3156 if (strcmp(groups, "")) {
3157 new_suffix = bprintf("%s\\.%s", new_suffix, groups);
3160 route_src_dst = bprintf("%s(.*)%s", cluster_prefix, new_suffix);
3164 #ifdef HAVE_PCRE_LIB
3166 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src_dst, route_src_dst);
3167 DEBUG0("symetrical=\"NO\">");
3168 SURFXML_BUFFER_SET(route_src, route_src_dst);
3169 SURFXML_BUFFER_SET(route_dst, route_src_dst);
3170 A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3171 SURFXML_START_TAG(route);
3173 DEBUG1("<link_ctn\tid=\"%s_link_$1src\"/>", cluster_id);
3174 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src", cluster_id));
3175 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3176 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3177 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3178 SURFXML_START_TAG(link_ctn);
3179 SURFXML_END_TAG(link_ctn);
3181 DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3182 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3183 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3184 SURFXML_START_TAG(link_ctn);
3185 SURFXML_END_TAG(link_ctn);
3187 DEBUG1("<link_ctn\tid=\"%s_link_$1dst\"/>", cluster_id);
3188 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst", cluster_id));
3189 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3190 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3191 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3192 SURFXML_START_TAG(link_ctn);
3193 SURFXML_END_TAG(link_ctn);
3196 SURFXML_END_TAG(route);
3198 for (i = 0; i <= xbt_dynar_length(tab_elements_num); i++) {
3199 for (j = 0; j <= xbt_dynar_length(tab_elements_num); j++) {
3200 if (i == xbt_dynar_length(tab_elements_num)) {
3201 route_src = router_id;
3204 bprintf("%s%d%s", cluster_prefix,
3205 xbt_dynar_get_as(tab_elements_num, i, int),
3209 if (j == xbt_dynar_length(tab_elements_num)) {
3210 route_dst = router_id;
3213 bprintf("%s%d%s", cluster_prefix,
3214 xbt_dynar_get_as(tab_elements_num, j, int),
3218 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\"", route_src, route_dst);
3219 DEBUG0("symetrical=\"NO\">");
3220 SURFXML_BUFFER_SET(route_src, route_src);
3221 SURFXML_BUFFER_SET(route_dst, route_dst);
3222 A_surfxml_route_symetrical = A_surfxml_route_symetrical_NO;
3223 SURFXML_START_TAG(route);
3225 if (i == xbt_dynar_length(tab_elements_num)) {
3226 route_src = link_router;
3229 bprintf("%s_link_%d", cluster_id,
3230 xbt_dynar_get_as(tab_elements_num, i, int));
3233 if (j == xbt_dynar_length(tab_elements_num)) {
3234 route_dst = link_router;
3237 bprintf("%s_link_%d", cluster_id,
3238 xbt_dynar_get_as(tab_elements_num, j, int));
3241 DEBUG1("<link_ctn\tid=\"%s\"/>", route_src);
3242 SURFXML_BUFFER_SET(link_ctn_id, route_src);
3243 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3244 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3245 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_UP;}
3246 SURFXML_START_TAG(link_ctn);
3247 SURFXML_END_TAG(link_ctn);
3249 DEBUG1("<link_ctn\tid=\"%s_backbone\"/>", cluster_id);
3250 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone", cluster_id));
3251 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3252 SURFXML_START_TAG(link_ctn);
3253 SURFXML_END_TAG(link_ctn);
3255 DEBUG1("<link_ctn\tid=\"%s\"/>", route_dst);
3256 SURFXML_BUFFER_SET(link_ctn_id, route_dst);
3257 A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_NONE;
3258 if(cluster_sharing_policy == A_surfxml_cluster_sharing_policy_FULLDUPLEX)
3259 {A_surfxml_link_ctn_direction = A_surfxml_link_ctn_direction_DOWN;}
3260 SURFXML_START_TAG(link_ctn);
3261 SURFXML_END_TAG(link_ctn);
3264 SURFXML_END_TAG(route);
3267 xbt_dynar_free(&tab_elements_num);
3271 SURFXML_END_TAG(AS);
3274 surfxml_bufferstack_pop(1);
3278 * New methods to init the routing model component from the lua script
3282 * calling parse_S_AS_lua with lua values
3284 void routing_AS_init(const char *AS_id, const char *AS_routing)
3286 parse_S_AS_lua((char *) AS_id, (char *) AS_routing);
3290 * calling parse_E_AS_lua to fisnish the creation of routing component
3292 void routing_AS_end(const char *AS_id)
3294 parse_E_AS_lua((char *) AS_id);
3298 * add a host to the network element list
3301 void routing_add_host(const char *host_id)
3303 parse_S_host_lua((char *) host_id);
3307 * Set a new link on the actual list of link for a route or ASroute
3309 void routing_add_link(const char *link_id)
3311 parse_E_link_c_ctn_new_elem_lua((char *) link_id);
3315 *Set the endpoints for a route
3317 void routing_set_route(const char *src_id, const char *dst_id)
3319 parse_S_route_new_and_endpoints_lua((char *) src_id, (char *) dst_id);
3323 * Store the route by calling parse_E_route_store_route
3325 void routing_store_route(void)
3327 parse_E_route_store_route();