1 /* Copyright (c) 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "gras_config.h"
13 #include <pcre.h> /* regular expresion library */
15 #include "surf_private.h"
16 #include "xbt/dynar.h"
18 #include "xbt/config.h"
19 #include "xbt/graph.h"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");
25 routing_global_t global_routing = NULL;
26 routing_component_t current_routing = NULL;
27 model_type_t current_routing_model = NULL;
29 /* Prototypes of each model */
30 static void* model_full_create(void); /* create structures for full routing model */
31 static void model_full_load(void); /* load parse functions for full routing model */
32 static void model_full_unload(void); /* unload parse functions for full routing model */
33 static void model_full_end(void); /* finalize the creation of full routing model */
35 static void* model_floyd_create(void); /* create structures for floyd routing model */
36 static void model_floyd_load(void); /* load parse functions for floyd routing model */
37 static void model_floyd_unload(void); /* unload parse functions for floyd routing model */
38 static void model_floyd_end(void); /* finalize the creation of floyd routing model */
40 static void* model_dijkstra_both_create(int cached); /* create by calling dijkstra or dijkstracache */
41 static void* model_dijkstra_create(void); /* create structures for dijkstra routing model */
42 static void* model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
43 static void model_dijkstra_both_load(void); /* load parse functions for dijkstra routing model */
44 static void model_dijkstra_both_unload(void); /* unload parse functions for dijkstra routing model */
45 static void model_dijkstra_both_end(void); /* finalize the creation of dijkstra routing model */
47 static void* model_rulebased_create(void); /* create structures for rulebased routing model */
48 static void model_rulebased_load(void); /* load parse functions for rulebased routing model */
49 static void model_rulebased_unload(void); /* unload parse functions for rulebased routing model */
50 static void model_rulebased_end(void); /* finalize the creation of rulebased routing model */
52 static void* model_none_create(void); /* none routing model */
53 static void model_none_load(void); /* none routing model */
54 static void model_none_unload(void); /* none routing model */
55 static void model_none_end(void); /* none routing model */
57 static void routing_full_parse_Scluster(void);/*cluster bypass*/
59 static void parse_Sconfig(void);/*config Tag*/
61 /* this lines are only for replace use like index in the model table */
66 SURF_MODEL_DIJKSTRACACHE,
74 /* must be finish with null and carefull if change de order */
75 struct s_model_type routing_models[] =
76 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)",
77 model_full_create, model_full_load, model_full_unload, model_full_end },
78 {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
79 model_floyd_create, model_floyd_load, model_floyd_unload, model_floyd_end },
80 {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
81 model_dijkstra_create ,model_dijkstra_both_load, model_dijkstra_both_unload, model_dijkstra_both_end },
82 {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
83 model_dijkstracache_create, model_dijkstra_both_load, model_dijkstra_both_unload, model_dijkstra_both_end },
84 {"none", "No routing (usable with Constant network only)",
85 model_none_create, model_none_load, model_none_unload, model_none_end },
87 {"RuleBased", "Rule-Based routing data (...)", model_rulebased_create, model_rulebased_load, model_rulebased_unload, model_rulebased_end },
89 {NULL,NULL,NULL,NULL,NULL,NULL}};
91 /* ************************************************************************** */
92 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
94 static void generic_set_processing_unit(routing_component_t rc, const char* name);
95 static void generic_set_autonomous_system(routing_component_t rc, const char* name);
96 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route);
97 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route);
98 static void generic_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route);
100 /* ************************************************************************** */
101 /* *************** GENERIC BUSINESS METHODS (declarations) ****************** */
103 static xbt_dynar_t generic_get_onelink_routes (routing_component_t rc);
104 static route_extended_t generic_get_bypassroute(routing_component_t rc, const char* src, const char* dst);
106 /* ************************************************************************** */
107 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
109 static route_extended_t generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy, void* data, int order);
110 static void generic_free_route(route_t route);
111 static void generic_free_extended_route(route_extended_t e_route);
112 static routing_component_t generic_autonomous_system_exist(routing_component_t rc, char* element);
113 static routing_component_t generic_processing_units_exist(routing_component_t rc, char* element);
114 static void generic_src_dst_check(routing_component_t rc, const char* src, const char* dst);
116 /* ************************************************************************** */
117 /* **************************** GLOBAL FUNCTIONS **************************** */
119 /* global parse functions */
120 static char* src = NULL; /* temporary store the source name of a route */
121 static char* dst = NULL; /* temporary store the destination name of a route */
122 static char* gw_src = NULL; /* temporary store the gateway source name of a route */
123 static char* gw_dst = NULL; /* temporary store the gateway destination name of a route */
124 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
127 * \brief Add a "host" to the network element list
129 static void parse_S_host(char* host_id) {
130 network_element_info_t info = NULL;
131 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
132 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,host_id),
133 "Reading a host, processing unit \"%s\" already exist",host_id);
134 xbt_assert1(current_routing->set_processing_unit,
135 "no defined method \"set_processing_unit\" in \"%s\"",current_routing->name);
136 (*(current_routing->set_processing_unit))(current_routing,host_id);
137 info = xbt_new0(s_network_element_info_t,1);
138 info->rc_component=current_routing;
139 info->rc_type=SURF_NETWORK_ELEMENT_HOST;
140 xbt_dict_set(global_routing->where_network_elements,host_id,(void*)info,NULL);
144 * \brief Add a host to the network element list from XML
146 static void parse_S_host_XML(void)
148 parse_S_host(A_surfxml_host_id);
152 * \brief Add a host to the network element list from lua script
154 static void parse_S_host_lua(char *host_id)
156 parse_S_host(host_id);
163 * \brief Add a "router" to the network element list
165 static void parse_S_router(void) {
166 network_element_info_t info = NULL;
168 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
169 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_router_id),
170 "Reading a router, processing unit \"%s\" already exist",A_surfxml_router_id);
171 xbt_assert1(current_routing->set_processing_unit,
172 "no defined method \"set_processing_unit\" in \"%s\"",current_routing->name);
173 (*(current_routing->set_processing_unit))(current_routing,A_surfxml_router_id);
174 info = xbt_new0(s_network_element_info_t,1);
175 info->rc_component=current_routing;
176 info->rc_type=SURF_NETWORK_ELEMENT_ROUTER;
177 xbt_dict_set(global_routing->where_network_elements,A_surfxml_router_id,(void*)info,NULL);
179 TRACE_surf_host_declaration (A_surfxml_router_id, 0);
184 * \brief Set the endponints for a route
186 static void parse_S_route_new_and_endpoints(char *src_id,char *dst_id) {
187 if( src != NULL && dst != NULL && link_list != NULL )
188 THROW2(arg_error,0,"Route between %s to %s can not be defined",src_id,dst_id);
191 xbt_assert2(strlen(src)>0||strlen(dst)>0,
192 "Some limits are null in the route between \"%s\" and \"%s\"",src,dst);
193 link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
197 * \breif Set the endpoints for a route from XML
199 static void parse_S_route_new_and_endpoints_XML(void)
201 parse_S_route_new_and_endpoints(A_surfxml_route_src,A_surfxml_route_dst);
205 * \breif Set the endpoints for a route from lua
207 static void parse_S_route_new_and_endpoints_lua(char *id_src,char *id_dst)
209 parse_S_route_new_and_endpoints(id_src,id_dst);
213 * \brief Set the endponints and gateways for a ASroute
215 static void parse_S_ASroute_new_and_endpoints(void) {
216 if( src != NULL && dst != NULL && link_list != NULL )
217 THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_ASroute_src,A_surfxml_ASroute_dst);
218 src = A_surfxml_ASroute_src;
219 dst = A_surfxml_ASroute_dst;
220 gw_src = A_surfxml_ASroute_gw_src;
221 gw_dst = A_surfxml_ASroute_gw_dst;
222 xbt_assert2(strlen(src)>0||strlen(dst)>0||strlen(gw_src)>0||strlen(gw_dst)>0,
223 "Some limits are null in the route between \"%s\" and \"%s\"",src,dst);
224 link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
228 * \brief Set the endponints for a bypassRoute
230 static void parse_S_bypassRoute_new_and_endpoints(void) {
231 if( src != NULL && dst != NULL && link_list != NULL )
232 THROW2(arg_error,0,"Bypass Route between %s to %s can not be defined",A_surfxml_bypassRoute_src,A_surfxml_bypassRoute_dst);
233 src = A_surfxml_bypassRoute_src;
234 dst = A_surfxml_bypassRoute_dst;
235 gw_src = A_surfxml_bypassRoute_gw_src;
236 gw_dst = A_surfxml_bypassRoute_gw_dst;
237 xbt_assert2(strlen(src)>0||strlen(dst)>0||strlen(gw_src)>0||strlen(gw_dst)>0,
238 "Some limits are null in the route between \"%s\" and \"%s\"",src,dst);
239 link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
243 * \brief Set a new link on the actual list of link for a route or ASroute
245 static void parse_E_link_ctn_new_elem(char *link_id) {
247 val = xbt_strdup(link_id);
248 xbt_dynar_push(link_list, &val);
252 * \brief Set a new link on the actual list of link for a route or ASroute from XML
255 static void parse_E_link_ctn_new_elem_XML(void)
257 parse_E_link_ctn_new_elem(A_surfxml_link_ctn_id);
261 * \brief Set a new link on the actual list of link for a route or ASroute from lua
263 static void parse_E_link_c_ctn_new_elem_lua(char *link_id) {
265 parse_E_link_ctn_new_elem(link_id);
269 * \brief Store the route by calling the set_route function of the current routing component
271 static void parse_E_route_store_route(void) {
272 route_t route = xbt_new0(s_route_t,1);
273 route->link_list = link_list;
274 // xbt_assert1(generic_processing_units_exist(current_routing,src),"the \"%s\" processing units gateway does not exist",src);
275 // xbt_assert1(generic_processing_units_exist(current_routing,dst),"the \"%s\" processing units gateway does not exist",dst);
276 xbt_assert1(current_routing->set_route,"no defined method \"set_route\" in \"%s\"",current_routing->name);
277 (*(current_routing->set_route))(current_routing,src,dst,route);
284 * \brief Store the ASroute by calling the set_ASroute function of the current routing component
286 static void parse_E_ASroute_store_route(void) {
287 route_extended_t e_route = xbt_new0(s_route_extended_t,1);
288 e_route->generic_route.link_list = link_list;
289 e_route->src_gateway = xbt_strdup(gw_src);
290 e_route->dst_gateway = xbt_strdup(gw_dst);
291 // xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
292 // xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
293 // xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
294 // xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
295 xbt_assert1(current_routing->set_ASroute,"no defined method \"set_ASroute\" in \"%s\"",current_routing->name);
296 (*(current_routing->set_ASroute))(current_routing,src,dst,e_route);
305 * \brief Store the bypass route by calling the set_bypassroute function of the current routing component
307 static void parse_E_bypassRoute_store_route(void) {
308 route_extended_t e_route = xbt_new0(s_route_extended_t,1);
309 e_route->generic_route.link_list = link_list;
310 e_route->src_gateway = xbt_strdup(gw_src);
311 e_route->dst_gateway = xbt_strdup(gw_dst);
312 // xbt_assert1(generic_autonomous_system_exist(current_routing,src),"the \"%s\" autonomous system does not exist",src);
313 // xbt_assert1(generic_autonomous_system_exist(current_routing,dst),"the \"%s\" autonomous system does not exist",dst);
314 // xbt_assert1(generic_processing_units_exist(current_routing,gw_src),"the \"%s\" processing units gateway does not exist",gw_src);
315 // xbt_assert1(generic_processing_units_exist(current_routing,gw_dst),"the \"%s\" processing units gateway does not exist",gw_dst);
316 xbt_assert1(current_routing->set_bypassroute,"no defined method \"set_bypassroute\" in \"%s\"",current_routing->name);
317 (*(current_routing->set_bypassroute))(current_routing,src,dst,e_route);
326 * \brief Make a new routing component
328 * make the new structure and
329 * set the parsing functions to allows parsing the part of the routing tree
331 static void parse_S_AS(char* AS_id,char* AS_routing) {
332 routing_component_t new_routing;
333 model_type_t model = NULL;
334 char* wanted = AS_routing;
336 /* seach the routing model */
337 for (cpt=0;routing_models[cpt].name;cpt++)
338 if (!strcmp(wanted,routing_models[cpt].name))
339 model = &routing_models[cpt];
340 /* if its not exist, error */
341 if( model == NULL ) {
342 fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
343 for (cpt=0;routing_models[cpt].name;cpt++)
344 if (!strcmp(wanted,routing_models[cpt].name))
345 fprintf(stderr," %s: %s\n",routing_models[cpt].name,routing_models[cpt].desc);
349 /* make a new routing component */
350 new_routing = (routing_component_t)(*(model->create))();
351 new_routing->routing = model;
352 new_routing->hierarchy = SURF_ROUTING_NULL;
353 new_routing->name = xbt_strdup(AS_id);
354 new_routing->routing_sons = xbt_dict_new();
355 //INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
357 if( current_routing == NULL && global_routing->root == NULL ){
359 /* it is the first one */
360 new_routing->routing_father = NULL;
361 global_routing->root = new_routing;
363 } else if( current_routing != NULL && global_routing->root != NULL ) {
365 xbt_assert1(!xbt_dict_get_or_null(current_routing->routing_sons,AS_id),
366 "The AS \"%s\" already exist",AS_id);
367 /* it is a part of the tree */
368 new_routing->routing_father = current_routing;
369 /* set the father behavior */
370 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
371 /* add to the sons dictionary */
372 xbt_dict_set(current_routing->routing_sons,AS_id,(void*)new_routing,NULL);
373 /* add to the father element list */
374 (*(current_routing->set_autonomous_system))(current_routing,AS_id);
375 /* unload the prev parse rules */
376 (*(current_routing->routing->unload))();
379 THROW0(arg_error,0,"All defined components must be belong to a AS");
381 /* set the new parse rules */
382 (*(new_routing->routing->load))();
383 /* set the new current component of the tree */
384 current_routing = new_routing;
388 * Detect the routing model type of the routing component from XML platforms
390 static void parse_S_AS_XML(void)
392 parse_S_AS(A_surfxml_AS_id,A_surfxml_AS_routing);
396 * define the routing model type of routing component from lua script
398 static void parse_S_AS_lua(char* id,char* mode)
405 * \brief Finish the creation of a new routing component
407 * When you finish to read the routing component, other structures must be created.
408 * the "end" method allow to do that for any routing model type
410 static void parse_E_AS(char *AS_id) {
412 if( current_routing == NULL ) {
413 THROW1(arg_error,0,"Close AS(%s), that never open",AS_id);
415 network_element_info_t info = NULL;
416 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,current_routing->name),
417 "The AS \"%s\" already exist",current_routing->name);
418 info = xbt_new0(s_network_element_info_t,1);
419 info->rc_component=current_routing->routing_father;
420 info->rc_type=SURF_NETWORK_ELEMENT_AS;
421 xbt_dict_set(global_routing->where_network_elements,current_routing->name,info,NULL);
422 (*(current_routing->routing->unload))();
423 (*(current_routing->routing->end))();
424 current_routing = current_routing->routing_father;
425 if( current_routing != NULL )
426 (*(current_routing->routing->load))();
431 * \brief Finish the creation of a new routing component from XML
433 static void parse_E_AS_XML(void)
435 parse_E_AS(A_surfxml_AS_id);
439 * \brief Finish the creation of a new routing component from lua
441 static void parse_E_AS_lua(char *id)
446 /* Aux Business methods */
449 * \brief Get the AS father and the first elements of the chain
451 * \param src the source host name
452 * \param dst the destination host name
454 * Get the common father of the to processing units, and the first different
455 * father in the chain
457 static xbt_dynar_t elements_father(const char* src,const char* dst) {
459 xbt_assert0(src&&dst,"bad parameters for \"elements_father\" method");
461 xbt_dynar_t result = xbt_dynar_new(sizeof(char*), NULL);
463 routing_component_t src_as, dst_as;
464 int index_src, index_dst, index_father_src, index_father_dst;
465 xbt_dynar_t path_src = NULL;
466 xbt_dynar_t path_dst = NULL;
467 routing_component_t current = NULL;
468 routing_component_t* current_src = NULL;
469 routing_component_t* current_dst = NULL;
470 routing_component_t* father = NULL;
472 /* (1) find the as where the src and dst are located */
473 src_as = ((network_element_info_t)xbt_dict_get_or_null(global_routing->where_network_elements,src))->rc_component;
474 dst_as = ((network_element_info_t)xbt_dict_get_or_null(global_routing->where_network_elements,dst))->rc_component;
475 xbt_assert2(src_as&&dst_as, "Ask for route \"from\"(%s) or \"to\"(%s) no found",src,dst);
477 /* (2) find the path to the root routing component */
478 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
480 while( current != NULL ) {
481 xbt_dynar_push(path_src,¤t);
482 current = current->routing_father;
484 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
486 while( current != NULL ) {
487 xbt_dynar_push(path_dst,¤t);
488 current = current->routing_father;
491 /* (3) find the common father */
492 index_src = (path_src->used)-1;
493 index_dst = (path_dst->used)-1;
494 current_src = xbt_dynar_get_ptr(path_src,index_src);
495 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
496 while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
497 current_src = xbt_dynar_get_ptr(path_src,index_src);
498 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
504 current_src = xbt_dynar_get_ptr(path_src,index_src);
505 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
507 /* (4) they are not in the same routing component, make the path */
508 index_father_src = index_src+1;
509 index_father_dst = index_dst+1;
511 if(*current_src==*current_dst)
512 father = current_src;
514 father = xbt_dynar_get_ptr(path_src,index_father_src);
516 /* (5) result generation */
517 xbt_dynar_push(result,father); /* first same the father of src and dst */
518 xbt_dynar_push(result,current_src); /* second the first different father of src */
519 xbt_dynar_push(result,current_dst); /* three the first different father of dst */
521 xbt_dynar_free(&path_src);
522 xbt_dynar_free(&path_dst);
527 /* Global Business methods */
530 * \brief Recursive function for get_route
532 * \param src the source host name
533 * \param dst the destination host name
535 * This fuction is call by "get_route". It allow to walk through the
536 * routing components tree.
538 static route_extended_t _get_route(const char* src,const char* dst) {
543 DEBUG2("Solve route \"%s\" to \"%s\"",src,dst);
545 xbt_assert0(src&&dst,"bad parameters for \"_get_route\" method");
547 route_extended_t e_route, e_route_cnt, e_route_src=NULL, e_route_dst=NULL;
549 xbt_dynar_t elem_father_list = elements_father(src,dst);
551 routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
552 routing_component_t src_father = xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
553 routing_component_t dst_father = xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
555 e_route = xbt_new0(s_route_extended_t,1);
556 e_route->src_gateway = NULL;
557 e_route->dst_gateway = NULL;
558 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
560 if(src_father==dst_father) { /* SURF_ROUTING_BASE */
562 if( strcmp(src,dst) ){
563 e_route_cnt = (*(common_father->get_route))(common_father,src,dst);
564 xbt_assert2(e_route_cnt,"no route between \"%s\" and \"%s\"",src,dst);
565 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
566 xbt_dynar_push(e_route->generic_route.link_list,&link);
568 generic_free_extended_route(e_route_cnt);
571 } else { /* SURF_ROUTING_RECURSIVE */
573 route_extended_t e_route_bypass = NULL;
575 if(common_father->get_bypass_route)
576 e_route_bypass = (*(common_father->get_bypass_route))(common_father,src,dst);
579 e_route_cnt = e_route_bypass;
581 e_route_cnt = (*(common_father->get_route))(common_father,src_father->name,dst_father->name);
583 xbt_assert2(e_route_cnt,"no route between \"%s\" and \"%s\"",src_father->name,dst_father->name);
585 xbt_assert2( (e_route_cnt->src_gateway==NULL) == (e_route_cnt->dst_gateway==NULL) ,
586 "bad gateway for route between \"%s\" and \"%s\"",src,dst);
588 if( src != e_route_cnt->src_gateway ) {
589 e_route_src = _get_route(src,e_route_cnt->src_gateway);
590 xbt_assert2(e_route_src,"no route between \"%s\" and \"%s\"",src,e_route_cnt->src_gateway);
591 xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
592 xbt_dynar_push(e_route->generic_route.link_list,&link);
596 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
597 xbt_dynar_push(e_route->generic_route.link_list,&link);
600 if( e_route_cnt->dst_gateway != dst ) {
601 e_route_dst = _get_route(e_route_cnt->dst_gateway,dst);
602 xbt_assert2(e_route_dst,"no route between \"%s\" and \"%s\"",e_route_cnt->dst_gateway,dst);
603 xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
604 xbt_dynar_push(e_route->generic_route.link_list,&link);
608 e_route->src_gateway = xbt_strdup(e_route_cnt->src_gateway);
609 e_route->dst_gateway = xbt_strdup(e_route_cnt->dst_gateway);
611 generic_free_extended_route(e_route_src);
612 generic_free_extended_route(e_route_cnt);
613 generic_free_extended_route(e_route_dst);
616 xbt_dynar_free(&elem_father_list);
622 * \brief Generic method: find a route between hosts
624 * \param src the source host name
625 * \param dst the destination host name
627 * walk through the routing components tree and find a route between hosts
628 * by calling the differents "get_route" functions in each routing component
630 static xbt_dynar_t get_route(const char* src,const char* dst) {
632 route_extended_t e_route;
633 xbt_dynar_t elem_father_list = elements_father(src,dst);
634 routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
637 e_route = _get_route(src,dst);
639 e_route = (*(common_father->get_route))(common_father,src,dst);
641 xbt_assert2(e_route,"no route between \"%s\" and \"%s\"",src,dst);
643 if(global_routing->last_route) xbt_dynar_free( &(global_routing->last_route) );
644 global_routing->last_route = e_route->generic_route.link_list;
646 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
647 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
650 xbt_dynar_free(&elem_father_list);
652 if( xbt_dynar_length(global_routing->last_route)==0 )
655 return global_routing->last_route;
659 * \brief Recursive function for finalize
661 * \param rc the source host name
663 * This fuction is call by "finalize". It allow to finalize the
664 * AS or routing components. It delete all the structures.
666 static void _finalize(routing_component_t rc) {
668 xbt_dict_cursor_t cursor = NULL;
670 routing_component_t elem;
671 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
674 xbt_dict_t tmp_sons = rc->routing_sons;
675 char* tmp_name = rc->name;
676 xbt_dict_free(&tmp_sons);
678 xbt_assert1(rc->finalize,"no defined method \"finalize\" in \"%s\"",current_routing->name);
679 (*(rc->finalize))(rc);
684 * \brief Generic method: delete all the routing structures
686 * walk through the routing components tree and delete the structures
687 * by calling the differents "finalize" functions in each routing component
689 static void finalize(void) {
690 /* delete recursibly all the tree */
691 _finalize(global_routing->root);
692 /* delete "where" dict */
693 xbt_dict_free(&(global_routing->where_network_elements));
694 /* delete last_route */
695 xbt_dynar_free(&(global_routing->last_route));
696 /* delete global routing structure */
697 xbt_free(global_routing);
700 static xbt_dynar_t recursive_get_onelink_routes (routing_component_t rc)
702 xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
704 //adding my one link routes
707 xbt_dynar_t onelink_mine = rc->get_onelink_routes (rc);
709 xbt_dynar_foreach(onelink_mine, cpt, link) {
710 xbt_dynar_push(ret,&link);
716 xbt_dict_cursor_t cursor=NULL;
717 routing_component_t rc_child;
718 xbt_dict_foreach(rc->routing_sons, cursor, key, rc_child) {
719 xbt_dynar_t onelink_child = recursive_get_onelink_routes (rc_child);
721 xbt_dynar_foreach(onelink_child, cpt, link) {
722 xbt_dynar_push(ret,&link);
729 static xbt_dynar_t get_onelink_routes(void)
731 return recursive_get_onelink_routes (global_routing->root);
734 static e_surf_network_element_type_t get_network_element_type(const char *name)
736 network_element_info_t rc=NULL;
737 rc = xbt_dict_get(global_routing->where_network_elements,name);
742 * \brief Generic method: create the global routing schema
744 * Make a global routing structure and set all the parsing functions.
746 void routing_model_create(size_t size_of_links, void* loopback) {
748 /* config the uniq global routing */
749 global_routing = xbt_new0(s_routing_global_t,1);
750 global_routing->where_network_elements = xbt_dict_new();
751 global_routing->root = NULL;
752 global_routing->get_route = get_route;
753 global_routing->get_onelink_routes = get_onelink_routes;
754 global_routing->get_network_element_type = get_network_element_type;
755 global_routing->finalize = finalize;
756 global_routing->loopback = loopback;
757 global_routing->size_of_link = size_of_links;
758 global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
760 /* no current routing at moment */
761 current_routing = NULL;
763 /* parse generic elements */
764 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host_XML);
765 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
767 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_S_route_new_and_endpoints_XML);
768 surfxml_add_callback(STag_surfxml_ASroute_cb_list, &parse_S_ASroute_new_and_endpoints);
769 surfxml_add_callback(STag_surfxml_bypassRoute_cb_list, &parse_S_bypassRoute_new_and_endpoints);
771 surfxml_add_callback(ETag_surfxml_link_ctn_cb_list, &parse_E_link_ctn_new_elem_XML);
773 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_E_route_store_route);
774 surfxml_add_callback(ETag_surfxml_ASroute_cb_list, &parse_E_ASroute_store_route);
775 surfxml_add_callback(ETag_surfxml_bypassRoute_cb_list, &parse_E_bypassRoute_store_route);
777 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS_XML);
778 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS_XML);
780 surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
782 surfxml_add_callback(STag_surfxml_config_cb_list, &parse_Sconfig);
785 /* ************************************************************************** */
786 /* *************************** FULL ROUTING ********************************* */
788 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
790 /* Routing model structure */
793 s_routing_component_t generic_routing;
794 xbt_dict_t parse_routes; /* store data during the parse process */
795 xbt_dict_t to_index; /* char* -> network_element_t */
796 xbt_dict_t bypassRoutes;
797 route_extended_t *routing_table;
798 } s_routing_component_full_t,*routing_component_full_t;
800 /* Business methods */
801 static xbt_dynar_t full_get_onelink_routes(routing_component_t rc)
803 xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
805 routing_component_full_t routing = (routing_component_full_t)rc;
806 int table_size = xbt_dict_length(routing->to_index);
807 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
808 char *k1, *d1, *k2, *d2;
809 xbt_dict_foreach(routing->to_index, c1, k1, d1) {
810 xbt_dict_foreach (routing->to_index, c2, k2, d2) {
811 int *src_id = xbt_dict_get_or_null(routing->to_index, k1);
812 int *dst_id = xbt_dict_get_or_null(routing->to_index, k2);
813 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
814 route_extended_t route = TO_ROUTE_FULL(*src_id,*dst_id);
816 if (xbt_dynar_length(route->generic_route.link_list) == 1){
817 void *link = *(void**)xbt_dynar_get_ptr(route->generic_route.link_list,0);
818 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
819 onelink->link_ptr = link;
820 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE){
821 onelink->src = xbt_strdup (k1);
822 onelink->dst = xbt_strdup (k2);
823 }else if (routing->generic_routing.hierarchy == SURF_ROUTING_RECURSIVE){
824 onelink->src = xbt_strdup (route->src_gateway);
825 onelink->dst = xbt_strdup (route->dst_gateway);
827 xbt_dynar_push (ret, &onelink);
835 static route_extended_t full_get_route(routing_component_t rc, const char* src,const char* dst) {
836 xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
839 routing_component_full_t routing = (routing_component_full_t)rc;
840 int table_size = xbt_dict_length(routing->to_index);
842 generic_src_dst_check(rc,src,dst);
843 int *src_id = xbt_dict_get_or_null(routing->to_index,src);
844 int *dst_id = xbt_dict_get_or_null(routing->to_index,dst);
845 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
847 route_extended_t e_route = NULL;
848 route_extended_t new_e_route = NULL;
852 e_route = TO_ROUTE_FULL(*src_id,*dst_id);
855 new_e_route = xbt_new0(s_route_extended_t,1);
856 new_e_route->src_gateway = xbt_strdup(e_route->src_gateway);
857 new_e_route->dst_gateway = xbt_strdup(e_route->dst_gateway);
858 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
859 xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
860 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
866 static void full_finalize(routing_component_t rc) {
867 routing_component_full_t routing = (routing_component_full_t)rc;
868 int table_size = xbt_dict_length(routing->to_index);
871 /* Delete routing table */
872 for (i=0;i<table_size;i++)
873 for (j=0;j<table_size;j++)
874 generic_free_extended_route(TO_ROUTE_FULL(i,j));
875 xbt_free(routing->routing_table);
876 /* Delete bypass dict */
877 xbt_dict_free(&routing->bypassRoutes);
878 /* Delete index dict */
879 xbt_dict_free(&(routing->to_index));
880 /* Delete structure */
885 /* Creation routing model functions */
887 static void* model_full_create(void) {
888 routing_component_full_t new_component = xbt_new0(s_routing_component_full_t,1);
889 new_component->generic_routing.set_processing_unit = generic_set_processing_unit;
890 new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
891 new_component->generic_routing.set_route = generic_set_route;
892 new_component->generic_routing.set_ASroute = generic_set_ASroute;
893 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
894 new_component->generic_routing.get_route = full_get_route;
895 new_component->generic_routing.get_onelink_routes = full_get_onelink_routes;
896 new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
897 new_component->generic_routing.finalize = full_finalize;
898 new_component->to_index = xbt_dict_new();
899 new_component->bypassRoutes = xbt_dict_new();
900 new_component->parse_routes = xbt_dict_new();
901 return new_component;
904 static void model_full_load(void) {
905 /* use "surfxml_add_callback" to add a parse function call */
908 static void model_full_unload(void) {
909 /* use "surfxml_del_callback" to remove a parse function call */
912 static void model_full_end(void) {
915 const char* sep = "#";
919 route_extended_t e_route;
922 xbt_dict_cursor_t cursor = NULL;
923 xbt_dynar_t keys = NULL;
926 routing_component_full_t routing = ((routing_component_full_t)current_routing);
927 int table_size = xbt_dict_length(routing->to_index);
929 /* Create the routing table */
930 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
932 /* Put the routes in position */
933 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
934 keys = xbt_str_split_str(key, sep);
935 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
936 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
937 TO_ROUTE_FULL(src_id,dst_id) = generic_new_extended_route(current_routing->hierarchy,data,1);
938 xbt_dynar_free(&keys);
941 /* delete the parse table */
942 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
943 route = (route_t)data;
944 xbt_dynar_free(&(route->link_list));
948 /* delete parse dict */
949 xbt_dict_free(&(routing->parse_routes));
951 /* Add the loopback if needed */
952 if(current_routing->hierarchy == SURF_ROUTING_BASE) {
953 for (i = 0; i < table_size; i++) {
954 e_route = TO_ROUTE_FULL(i, i);
956 e_route = xbt_new0(s_route_extended_t,1);
957 e_route->src_gateway = NULL;
958 e_route->dst_gateway = NULL;
959 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
960 xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
961 TO_ROUTE_FULL(i, i) = e_route;
966 /* Shrink the dynar routes (save unused slots) */
967 for (i=0;i<table_size;i++)
968 for (j=0;j<table_size;j++)
969 if(TO_ROUTE_FULL(i,j))
970 xbt_dynar_shrink(TO_ROUTE_FULL(i,j)->generic_route.link_list,0);
973 /* ************************************************************************** */
974 /* *************************** FLOYD ROUTING ******************************** */
976 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
977 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
978 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
980 /* Routing model structure */
983 s_routing_component_t generic_routing;
984 /* vars for calculate the floyd algorith. */
985 int *predecessor_table;
986 route_extended_t *link_table; /* char* -> int* */
988 xbt_dict_t bypassRoutes;
989 /* store data during the parse process */
990 xbt_dict_t parse_routes;
991 } s_routing_component_floyd_t,*routing_component_floyd_t;
993 static route_extended_t floyd_get_route(routing_component_t rc, const char* src,const char* dst);
995 /* Business methods */
996 static xbt_dynar_t floyd_get_onelink_routes(routing_component_t rc)
998 xbt_dynar_t ret = xbt_dynar_new (sizeof(onelink_t), xbt_free);
1000 routing_component_floyd_t routing = (routing_component_floyd_t)rc;
1001 //int table_size = xbt_dict_length(routing->to_index);
1002 xbt_dict_cursor_t c1 = NULL, c2 = NULL;
1003 char *k1, *d1, *k2, *d2;
1004 xbt_dict_foreach(routing->to_index, c1, k1, d1) {
1005 xbt_dict_foreach (routing->to_index, c2, k2, d2) {
1006 route_extended_t route = floyd_get_route (rc, k1, k2);
1008 if (xbt_dynar_length(route->generic_route.link_list) == 1){
1009 void *link = *(void**)xbt_dynar_get_ptr(route->generic_route.link_list,0);
1010 onelink_t onelink = xbt_new0 (s_onelink_t, 1);
1011 onelink->link_ptr = link;
1012 if (routing->generic_routing.hierarchy == SURF_ROUTING_BASE){
1013 onelink->src = xbt_strdup (k1);
1014 onelink->dst = xbt_strdup (k2);
1015 }else if (routing->generic_routing.hierarchy == SURF_ROUTING_RECURSIVE){
1016 onelink->src = xbt_strdup (route->src_gateway);
1017 onelink->dst = xbt_strdup (route->dst_gateway);
1019 xbt_dynar_push (ret, &onelink);
1027 static route_extended_t floyd_get_route(routing_component_t rc, const char* src,const char* dst) {
1028 xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
1030 /* set utils vars */
1031 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
1032 int table_size = xbt_dict_length(routing->to_index);
1034 generic_src_dst_check(rc,src,dst);
1035 int *src_id = xbt_dict_get_or_null(routing->to_index,src);
1036 int *dst_id = xbt_dict_get_or_null(routing->to_index,dst);
1037 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
1039 /* create a result route */
1040 route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
1041 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1042 new_e_route->src_gateway = NULL;
1043 new_e_route->dst_gateway = NULL;
1048 char *gw_src=NULL,*gw_dst=NULL, *prev_gw_src,*prev_gw_dst, *first_gw=NULL;
1055 pred = TO_FLOYD_PRED(*src_id, pred);
1056 if(pred == -1) /* if no pred in route -> no route to host */
1058 xbt_assert2(TO_FLOYD_LINK(pred,prev_pred),"Invalid link for the route between \"%s\" or \"%s\"", src, dst);
1060 prev_gw_src = gw_src;
1061 prev_gw_dst = gw_dst;
1063 route_extended_t e_route = TO_FLOYD_LINK(pred,prev_pred);
1064 gw_src = e_route->src_gateway;
1065 gw_dst = e_route->dst_gateway;
1067 if(first) first_gw = gw_dst;
1069 if(rc->hierarchy == SURF_ROUTING_RECURSIVE && !first && strcmp(gw_dst,prev_gw_src)) {
1070 xbt_dynar_t e_route_as_to_as = (*(global_routing->get_route))(gw_dst,prev_gw_src);
1071 xbt_assert2(e_route_as_to_as,"no route between \"%s\" and \"%s\"",gw_dst,prev_gw_src);
1072 links = e_route_as_to_as;
1074 xbt_dynar_foreach(links, cpt, link) {
1075 xbt_dynar_insert_at(new_e_route->generic_route.link_list,pos,&link);
1080 links = e_route->generic_route.link_list;
1081 xbt_dynar_foreach(links, cpt, link) {
1082 xbt_dynar_unshift(new_e_route->generic_route.link_list,&link);
1086 } while(pred != *src_id);
1087 xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")", *src_id, *dst_id,src,dst);
1089 if(rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1090 new_e_route->src_gateway = xbt_strdup(gw_src);
1091 new_e_route->dst_gateway = xbt_strdup(first_gw);
1097 static void floyd_finalize(routing_component_t rc) {
1098 routing_component_floyd_t routing = (routing_component_floyd_t)rc;
1101 table_size = xbt_dict_length(routing->to_index);
1102 /* Delete link_table */
1103 for (i=0;i<table_size;i++)
1104 for (j=0;j<table_size;j++)
1105 generic_free_extended_route(TO_FLOYD_LINK(i,j));
1106 xbt_free(routing->link_table);
1107 /* Delete bypass dict */
1108 xbt_dict_free(&routing->bypassRoutes);
1109 /* Delete index dict */
1110 xbt_dict_free(&(routing->to_index));
1111 /* Delete dictionary index dict, predecessor and links table */
1112 xbt_free(routing->predecessor_table);
1113 /* Delete structure */
1118 static void* model_floyd_create(void) {
1119 routing_component_floyd_t new_component = xbt_new0(s_routing_component_floyd_t,1);
1120 new_component->generic_routing.set_processing_unit = generic_set_processing_unit;
1121 new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
1122 new_component->generic_routing.set_route = generic_set_route;
1123 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1124 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1125 new_component->generic_routing.get_route = floyd_get_route;
1126 new_component->generic_routing.get_onelink_routes = floyd_get_onelink_routes;
1127 new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
1128 new_component->generic_routing.finalize = floyd_finalize;
1129 new_component->to_index = xbt_dict_new();
1130 new_component->bypassRoutes = xbt_dict_new();
1131 new_component->parse_routes = xbt_dict_new();
1132 return new_component;
1135 static void model_floyd_load(void) {
1136 /* use "surfxml_add_callback" to add a parse function call */
1139 static void model_floyd_unload(void) {
1140 /* use "surfxml_del_callback" to remove a parse function call */
1143 static void model_floyd_end(void) {
1145 routing_component_floyd_t routing = ((routing_component_floyd_t)current_routing);
1146 xbt_dict_cursor_t cursor = NULL;
1147 double * cost_table;
1148 char *key,*data, *end;
1149 const char *sep = "#";
1152 unsigned int i,j,a,b,c;
1154 /* set the size of inicial table */
1155 int table_size = xbt_dict_length(routing->to_index);
1157 /* Create Cost, Predecessor and Link tables */
1158 cost_table = xbt_new0(double, table_size*table_size); /* link cost from host to host */
1159 routing->predecessor_table = xbt_new0(int, table_size*table_size); /* predecessor host numbers */
1160 routing->link_table = xbt_new0(route_extended_t, table_size*table_size); /* actual link between src and dst */
1162 /* Initialize costs and predecessors*/
1163 for(i = 0; i<table_size;i++)
1164 for(j = 0; j<table_size;j++) {
1165 TO_FLOYD_COST(i,j) = DBL_MAX;
1166 TO_FLOYD_PRED(i,j) = -1;
1167 TO_FLOYD_LINK(i,j) = NULL; /* fixed, missing in the previous version */
1170 /* Put the routes in position */
1171 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1172 keys = xbt_str_split_str(key, sep);
1173 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1174 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1175 TO_FLOYD_LINK(src_id,dst_id) = generic_new_extended_route(current_routing->hierarchy,data,0);
1176 TO_FLOYD_PRED(src_id,dst_id) = src_id;
1178 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 */
1179 xbt_dynar_free(&keys);
1182 /* Add the loopback if needed */
1183 if(current_routing->hierarchy == SURF_ROUTING_BASE) {
1184 for (i = 0; i < table_size; i++) {
1185 route_extended_t e_route = TO_FLOYD_LINK(i, i);
1187 e_route = xbt_new0(s_route_extended_t,1);
1188 e_route->src_gateway = NULL;
1189 e_route->dst_gateway = NULL;
1190 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1191 xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
1192 TO_FLOYD_LINK(i,i) = e_route;
1193 TO_FLOYD_PRED(i,i) = i;
1194 TO_FLOYD_COST(i,i) = 1;
1198 /* Calculate path costs */
1199 for(c=0;c<table_size;c++) {
1200 for(a=0;a<table_size;a++) {
1201 for(b=0;b<table_size;b++) {
1202 if(TO_FLOYD_COST(a,c) < DBL_MAX && TO_FLOYD_COST(c,b) < DBL_MAX) {
1203 if(TO_FLOYD_COST(a,b) == DBL_MAX ||
1204 (TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b) < TO_FLOYD_COST(a,b))) {
1205 TO_FLOYD_COST(a,b) = TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b);
1206 TO_FLOYD_PRED(a,b) = TO_FLOYD_PRED(c,b);
1213 /* delete the parse table */
1214 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1215 route_t route = (route_t)data;
1216 xbt_dynar_free(&(route->link_list));
1220 /* delete parse dict */
1221 xbt_dict_free(&(routing->parse_routes));
1224 xbt_free(cost_table);
1227 /* ************************************************************************** */
1228 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1231 s_routing_component_t generic_routing;
1232 xbt_dict_t to_index;
1233 xbt_dict_t bypassRoutes;
1234 xbt_graph_t route_graph; /* xbt_graph */
1235 xbt_dict_t graph_node_map; /* map */
1236 xbt_dict_t route_cache; /* use in cache mode */
1238 xbt_dict_t parse_routes;
1239 } s_routing_component_dijkstra_t,*routing_component_dijkstra_t;
1242 typedef struct graph_node_data {
1244 int graph_id; /* used for caching internal graph id's */
1245 } s_graph_node_data_t, * graph_node_data_t;
1247 typedef struct graph_node_map_element {
1249 } s_graph_node_map_element_t, * graph_node_map_element_t;
1251 typedef struct route_cache_element {
1254 } s_route_cache_element_t, * route_cache_element_t;
1256 /* Free functions */
1258 static void route_cache_elem_free(void *e) {
1259 route_cache_element_t elm=(route_cache_element_t)e;
1261 xbt_free(elm->pred_arr);
1266 static void graph_node_map_elem_free(void *e) {
1267 graph_node_map_element_t elm = (graph_node_map_element_t)e;
1273 static void graph_edge_data_free(void *e) {
1274 route_extended_t e_route = (route_extended_t)e;
1276 xbt_dynar_free(&(e_route->generic_route.link_list));
1277 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
1278 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
1283 /* Utility functions */
1285 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc, int id, int graph_id) {
1286 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1287 xbt_node_t node = NULL;
1288 graph_node_data_t data = NULL;
1289 graph_node_map_element_t elm = NULL;
1291 data = xbt_new0(struct graph_node_data, 1);
1293 data->graph_id = graph_id;
1294 node = xbt_graph_new_node(routing->route_graph, data);
1296 elm = xbt_new0(struct graph_node_map_element, 1);
1298 xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
1303 static graph_node_map_element_t graph_node_map_search(routing_component_dijkstra_t rc, int id) {
1304 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1305 graph_node_map_element_t elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&id), sizeof(int));
1311 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id, int dst_id, route_extended_t e_route ) {
1312 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1314 xbt_node_t src = NULL;
1315 xbt_node_t dst = NULL;
1316 graph_node_map_element_t src_elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&src_id), sizeof(int));
1317 graph_node_map_element_t dst_elm = (graph_node_map_element_t)xbt_dict_get_or_null_ext(routing->graph_node_map, (char*)(&dst_id), sizeof(int));
1320 src = src_elm->node;
1323 dst = dst_elm->node;
1325 /* add nodes if they don't exist in the graph */
1326 if(src_id == dst_id && src == NULL && dst == NULL) {
1327 src = route_graph_new_node(rc,src_id, -1);
1331 src = route_graph_new_node(rc,src_id, -1);
1334 dst = route_graph_new_node(rc,dst_id, -1);
1338 /* add link as edge to graph */
1339 xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1342 static void add_loopback_dijkstra(routing_component_dijkstra_t rc) {
1343 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1345 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1347 xbt_node_t node = NULL;
1348 unsigned int cursor2;
1349 xbt_dynar_foreach(nodes, cursor2, node) {
1350 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1351 xbt_edge_t edge = NULL;
1352 unsigned int cursor;
1355 xbt_dynar_foreach(out_edges, cursor, edge) {
1356 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1357 if(other_node == node) {
1364 route_extended_t e_route = xbt_new0(s_route_extended_t,1);
1365 e_route->src_gateway = NULL;
1366 e_route->dst_gateway = NULL;
1367 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1368 xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
1369 xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1374 /* Business methods */
1375 static xbt_dynar_t dijkstra_get_onelink_routes(routing_component_t rc)
1377 xbt_die("\"dijkstra_get_onelink_routes\" function not implemented yet");
1380 static route_extended_t dijkstra_get_route(routing_component_t rc, const char* src,const char* dst) {
1381 xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
1383 /* set utils vars */
1384 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1386 generic_src_dst_check(rc,src,dst);
1387 int *src_id = xbt_dict_get_or_null(routing->to_index,src);
1388 int *dst_id = xbt_dict_get_or_null(routing->to_index,dst);
1389 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
1391 /* create a result route */
1392 route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
1393 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1394 new_e_route->src_gateway = NULL;
1395 new_e_route->dst_gateway = NULL;
1397 int *pred_arr = NULL;
1398 int src_node_id = 0;
1399 int dst_node_id = 0;
1400 int * nodeid = NULL;
1402 route_extended_t e_route;
1406 xbt_dynar_t links = NULL;
1407 route_cache_element_t elm = NULL;
1408 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1410 /* Use the graph_node id mapping set to quickly find the nodes */
1411 graph_node_map_element_t src_elm = graph_node_map_search(routing,*src_id);
1412 graph_node_map_element_t dst_elm = graph_node_map_search(routing,*dst_id);
1413 xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", *src_id, *dst_id);
1414 src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
1415 dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
1417 /* if the src and dst are the same */ /* fixed, missing in the previous version */
1418 if( src_node_id == dst_node_id ) {
1420 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1421 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1422 xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1424 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id, *dst_id);
1426 e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
1428 links = e_route->generic_route.link_list;
1429 xbt_dynar_foreach(links, cpt, link) {
1430 xbt_dynar_unshift(new_e_route->generic_route.link_list,&link);
1436 if(routing->cached) {
1437 /*check if there is a cached predecessor list avail */
1438 elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
1441 if(elm) { /* cached mode and cache hit */
1442 pred_arr = elm->pred_arr;
1443 } else { /* not cached mode or cache miss */
1444 double * cost_arr = NULL;
1445 xbt_heap_t pqueue = NULL;
1448 int nr_nodes = xbt_dynar_length(nodes);
1449 cost_arr = xbt_new0(double, nr_nodes); /* link cost from src to other hosts */
1450 pred_arr = xbt_new0(int, nr_nodes); /* predecessors in path from src */
1451 pqueue = xbt_heap_new(nr_nodes, xbt_free);
1454 cost_arr[src_node_id] = 0.0;
1456 for(i = 0; i < nr_nodes; i++) {
1457 if(i != src_node_id) {
1458 cost_arr[i] = DBL_MAX;
1463 /* initialize priority queue */
1464 nodeid = xbt_new0(int, 1);
1466 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1470 /* apply dijkstra using the indexes from the graph's node array */
1471 while(xbt_heap_size(pqueue) > 0) {
1472 int * v_id = xbt_heap_pop(pqueue);
1473 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1474 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1475 xbt_edge_t edge = NULL;
1476 unsigned int cursor;
1478 xbt_dynar_foreach(out_edges, cursor, edge) {
1479 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1480 graph_node_data_t data = xbt_graph_node_get_data(u_node);
1481 int u_id = data->graph_id;
1482 route_extended_t tmp_e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
1483 int cost_v_u = (tmp_e_route->generic_route.link_list)->used; /* count of links, old model assume 1 */
1485 if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1486 pred_arr[u_id] = *v_id;
1487 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1488 nodeid = xbt_new0(int, 1);
1490 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1494 /* free item popped from pqueue */
1499 xbt_heap_free(pqueue);
1502 /* compose route path with links */
1503 char *gw_src=NULL,*gw_dst=NULL, *prev_gw_src,*prev_gw_dst, *first_gw=NULL;
1505 for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1506 xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1507 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1508 xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1510 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id, *dst_id);
1512 prev_gw_src = gw_src;
1513 prev_gw_dst = gw_dst;
1515 e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
1516 gw_src = e_route->src_gateway;
1517 gw_dst = e_route->dst_gateway;
1519 if(v==dst_node_id) first_gw = gw_dst;
1521 if(rc->hierarchy == SURF_ROUTING_RECURSIVE && v!=dst_node_id && strcmp(gw_dst,prev_gw_src)) {
1522 xbt_dynar_t e_route_as_to_as = (*(global_routing->get_route))(gw_dst,prev_gw_src);
1523 xbt_assert2(e_route_as_to_as,"no route between \"%s\" and \"%s\"",gw_dst,prev_gw_src);
1524 links = e_route_as_to_as;
1526 xbt_dynar_foreach(links, cpt, link) {
1527 xbt_dynar_insert_at(new_e_route->generic_route.link_list,pos,&link);
1532 links = e_route->generic_route.link_list;
1533 xbt_dynar_foreach(links, cpt, link) {
1534 xbt_dynar_unshift(new_e_route->generic_route.link_list,&link);
1539 if(rc->hierarchy == SURF_ROUTING_RECURSIVE) {
1540 new_e_route->src_gateway = xbt_strdup(gw_src);
1541 new_e_route->dst_gateway = xbt_strdup(first_gw);
1544 if(routing->cached && elm == NULL) {
1545 /* add to predecessor list of the current src-host to cache */
1546 elm = xbt_new0(struct route_cache_element, 1);
1547 elm->pred_arr = pred_arr;
1549 xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
1552 if(!routing->cached)
1558 static void dijkstra_finalize(routing_component_t rc) {
1559 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1562 xbt_graph_free_graph(routing->route_graph, &xbt_free, &graph_edge_data_free, &xbt_free);
1563 xbt_dict_free(&routing->graph_node_map);
1565 xbt_dict_free(&routing->route_cache);
1566 /* Delete bypass dict */
1567 xbt_dict_free(&routing->bypassRoutes);
1568 /* Delete index dict */
1569 xbt_dict_free(&(routing->to_index));
1570 /* Delete structure */
1575 /* Creation routing model functions */
1577 static void* model_dijkstra_both_create(int cached) {
1578 routing_component_dijkstra_t new_component = xbt_new0(s_routing_component_dijkstra_t,1);
1579 new_component->generic_routing.set_processing_unit = generic_set_processing_unit;
1580 new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
1581 new_component->generic_routing.set_route = generic_set_route;
1582 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1583 new_component->generic_routing.set_bypassroute = generic_set_bypassroute;
1584 new_component->generic_routing.get_route = dijkstra_get_route;
1585 new_component->generic_routing.get_onelink_routes = dijkstra_get_onelink_routes;
1586 new_component->generic_routing.get_bypass_route = generic_get_bypassroute;
1587 new_component->generic_routing.finalize = dijkstra_finalize;
1588 new_component->cached = cached;
1589 new_component->to_index = xbt_dict_new();
1590 new_component->bypassRoutes = xbt_dict_new();
1591 new_component->parse_routes = xbt_dict_new();
1592 return new_component;
1595 static void* model_dijkstra_create(void) {
1596 return model_dijkstra_both_create(0);
1599 static void* model_dijkstracache_create(void) {
1600 return model_dijkstra_both_create(1);
1603 static void model_dijkstra_both_load(void) {
1604 /* use "surfxml_add_callback" to add a parse function call */
1607 static void model_dijkstra_both_unload(void) {
1608 /* use "surfxml_del_callback" to remove a parse function call */
1611 static void model_dijkstra_both_end(void) {
1612 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) current_routing;
1613 xbt_dict_cursor_t cursor = NULL;
1614 char *key, *data, *end;
1615 const char *sep = "#";
1617 xbt_node_t node = NULL;
1618 unsigned int cursor2;
1619 xbt_dynar_t nodes = NULL;
1623 /* Create the topology graph */
1624 routing->route_graph = xbt_graph_new_graph(1, NULL);
1625 routing->graph_node_map = xbt_dict_new();
1628 routing->route_cache = xbt_dict_new();
1630 /* Put the routes in position */
1631 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1632 keys = xbt_str_split_str(key, sep);
1633 src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 10);
1634 dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 10);
1635 route_extended_t e_route = generic_new_extended_route(current_routing->hierarchy,data,0);
1636 route_new_dijkstra(routing,src_id,dst_id,e_route);
1637 xbt_dynar_free(&keys);
1640 /* delete the parse table */
1641 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1642 route = (route_t)data;
1643 xbt_dynar_free(&(route->link_list));
1647 /* delete parse dict */
1648 xbt_dict_free(&(routing->parse_routes));
1650 /* Add the loopback if needed */
1651 if(current_routing->hierarchy == SURF_ROUTING_BASE)
1652 add_loopback_dijkstra(routing);
1654 /* initialize graph indexes in nodes after graph has been built */
1655 nodes = xbt_graph_get_nodes(routing->route_graph);
1657 xbt_dynar_foreach(nodes, cursor2, node) {
1658 graph_node_data_t data = xbt_graph_node_get_data(node);
1659 data->graph_id = cursor2;
1664 #ifdef HAVE_PCRE_LIB
1665 /* ************************************************** */
1666 /* ************** RULE-BASED ROUTING **************** */
1668 /* Routing model structure */
1671 s_routing_component_t generic_routing;
1672 xbt_dict_t dict_processing_units;
1673 xbt_dict_t dict_autonomous_systems;
1674 xbt_dynar_t list_route;
1675 xbt_dynar_t list_ASroute;
1676 } s_routing_component_rulebased_t,*routing_component_rulebased_t;
1678 typedef struct s_rule_route s_rule_route_t, *rule_route_t;
1679 typedef struct s_rule_route_extended s_rule_route_extended_t, *rule_route_extended_t;
1681 struct s_rule_route {
1682 xbt_dynar_t re_str_link; // dynar of char*
1687 struct s_rule_route_extended {
1688 s_rule_route_t generic_rule_route;
1689 char* re_src_gateway;
1690 char* re_dst_gateway;
1693 static void rule_route_free(void *e) {
1694 rule_route_t* elem = (rule_route_t*)(e);
1696 xbt_dynar_free(&(*elem)->re_str_link);
1697 pcre_free((*elem)->re_src);
1698 pcre_free((*elem)->re_dst);
1704 static void rule_route_extended_free(void *e) {
1705 rule_route_extended_t* elem = (rule_route_extended_t*)e;
1707 xbt_dynar_free(&(*elem)->generic_rule_route.re_str_link);
1708 pcre_free((*elem)->generic_rule_route.re_src);
1709 pcre_free((*elem)->generic_rule_route.re_dst);
1710 xbt_free((*elem)->re_src_gateway);
1711 xbt_free((*elem)->re_dst_gateway);
1716 /* Parse routing model functions */
1718 static void model_rulebased_set_processing_unit(routing_component_t rc, const char* name) {
1719 routing_component_rulebased_t routing = (routing_component_rulebased_t) rc;
1720 xbt_dict_set(routing->dict_processing_units, name, (void*)(-1), NULL);
1723 static void model_rulebased_set_autonomous_system(routing_component_t rc, const char* name) {
1724 routing_component_rulebased_t routing = (routing_component_rulebased_t) rc;
1725 xbt_dict_set(routing->dict_autonomous_systems, name, (void*)(-1), NULL);
1728 static void model_rulebased_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {
1729 routing_component_rulebased_t routing = (routing_component_rulebased_t) rc;
1730 rule_route_t ruleroute = xbt_new0(s_rule_route_t,1);
1733 ruleroute->re_src = pcre_compile(src,0,&error,&erroffset,NULL);
1734 xbt_assert3(ruleroute->re_src,"PCRE compilation failed at offset %d (\"%s\"): %s\n", erroffset, src, error);
1735 ruleroute->re_dst = pcre_compile(dst,0,&error,&erroffset,NULL);
1736 xbt_assert3(ruleroute->re_src,"PCRE compilation failed at offset %d (\"%s\"): %s\n", erroffset, dst, error);
1737 ruleroute->re_str_link = route->link_list;
1738 xbt_dynar_push(routing->list_route,&ruleroute);
1742 static void model_rulebased_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {
1743 routing_component_rulebased_t routing = (routing_component_rulebased_t) rc;
1744 rule_route_extended_t ruleroute_e = xbt_new0(s_rule_route_extended_t,1);
1747 ruleroute_e->generic_rule_route.re_src = pcre_compile(src,0,&error,&erroffset,NULL);
1748 xbt_assert3(ruleroute_e->generic_rule_route.re_src,"PCRE compilation failed at offset %d (\"%s\"): %s\n", erroffset, src, error);
1749 ruleroute_e->generic_rule_route.re_dst = pcre_compile(dst,0,&error,&erroffset,NULL);
1750 xbt_assert3(ruleroute_e->generic_rule_route.re_src,"PCRE compilation failed at offset %d (\"%s\"): %s\n", erroffset, dst, error);
1751 ruleroute_e->generic_rule_route.re_str_link = route->generic_route.link_list;
1752 ruleroute_e->re_src_gateway = route->src_gateway;
1753 ruleroute_e->re_dst_gateway = route->dst_gateway;
1754 xbt_dynar_push(routing->list_ASroute,&ruleroute_e);
1755 xbt_free(route->src_gateway);
1756 xbt_free(route->dst_gateway);
1760 static void model_rulebased_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
1761 xbt_die("bypass routing not supported for Route-Based model");
1764 #define BUFFER_SIZE 4096 /* result buffer size */
1765 #define OVECCOUNT 30 /* should be a multiple of 3 */
1767 static char* remplace(char* value, const char** src_list, int src_size, const char** dst_list, int dst_size ) {
1769 char result_result[BUFFER_SIZE];
1770 int i_result_buffer;
1771 int value_length = (int)strlen(value);
1775 i_result_buffer = 0;
1777 if( value[i] == '$' ) {
1781 int number_length = 0;
1782 while( '0' <= value[i+number_length] && value[i+number_length] <= '9' ) {
1785 xbt_assert2( number_length!=0, "bad string parameter, no number indication, at offset: %d (\"%s\")",i,value);
1788 number = atoi(value+i);
1790 xbt_assert2(i+2<value_length,"bad string parameter, too few chars, at offset: %d (\"%s\")",i,value);
1792 // solve the indication
1793 const char** param_list;
1795 if( value[i] == 's' && value[i+1] == 'r' && value[i+2] == 'c' ) {
1796 param_list = src_list;
1797 param_size = src_size;
1798 } else if( value[i] == 'd' && value[i+1] == 's' && value[i+2] == 't' ) {
1799 param_list = dst_list;
1800 param_size = dst_size;
1802 xbt_assert2(0,"bad string parameter, support only \"src\" and \"dst\", at offset: %d (\"%s\")",i,value);
1806 xbt_assert4( param_size >= number, "bad string parameter, not enough length param_size, at offset: %d (\"%s\") %d %d",i,value,param_size,number);
1808 const char* param = param_list[number];
1809 int size = strlen(param);
1811 for(cp = 0; cp < size; cp++ ) {
1812 result_result[i_result_buffer] = param[cp];
1814 if( i_result_buffer >= BUFFER_SIZE ) break;
1817 result_result[i_result_buffer] = value[i];
1822 } while(i<value_length && i_result_buffer < BUFFER_SIZE);
1824 xbt_assert2( i_result_buffer < BUFFER_SIZE, "solving string \"%s\", small buffer size (%d)",value,BUFFER_SIZE);
1825 result_result[i_result_buffer] = 0;
1826 return xbt_strdup(result_result);
1829 static xbt_dynar_t rulebased_get_onelink_routes(routing_component_t rc)
1831 xbt_die("\"rulebased_get_onelink_routes\" function not implemented yet");
1834 /* Business methods */
1835 static route_extended_t rulebased_get_route(routing_component_t rc, const char* src,const char* dst) {
1836 xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
1838 /* set utils vars */
1839 routing_component_rulebased_t routing = (routing_component_rulebased_t) rc;
1841 int are_processing_units;
1842 xbt_dynar_t rule_list;
1843 if( xbt_dict_get_or_null(routing->dict_processing_units,src) && xbt_dict_get_or_null(routing->dict_processing_units,dst) ) {
1844 are_processing_units = 1;
1845 rule_list = routing->list_route;
1846 } else if( xbt_dict_get_or_null(routing->dict_autonomous_systems,src) && xbt_dict_get_or_null(routing->dict_autonomous_systems,dst) ) {
1847 are_processing_units = 0;
1848 rule_list = routing->list_ASroute;
1850 xbt_assert2(NULL, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
1854 int src_length = (int)strlen(src);
1855 int dst_length = (int)strlen(dst);
1857 xbt_dynar_t links_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1859 rule_route_t ruleroute;
1861 int ovector_src[OVECCOUNT];
1862 int ovector_dst[OVECCOUNT];
1863 const char** list_src = NULL;
1864 const char** list_dst = NULL;
1865 xbt_dynar_foreach(rule_list, cpt, ruleroute) {
1866 rc_src = pcre_exec(ruleroute->re_src,NULL,src,src_length,0,0,ovector_src,OVECCOUNT);
1868 rc_dst = pcre_exec(ruleroute->re_dst,NULL,dst,dst_length,0,0,ovector_dst,OVECCOUNT);
1870 xbt_assert1(!pcre_get_substring_list(src,ovector_src,rc_src,&list_src),"error solving substring list for src \"%s\"",src);
1871 xbt_assert1(!pcre_get_substring_list(dst,ovector_dst,rc_dst,&list_dst),"error solving substring list for src \"%s\"",dst);
1873 xbt_dynar_foreach(ruleroute->re_str_link, cpt, link_name) {
1874 char* new_link_name = remplace(link_name,list_src,rc_src,list_dst,rc_dst);
1875 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, new_link_name);
1877 xbt_dynar_push(links_list,&link);
1879 THROW1(mismatch_error,0,"Link %s not found", new_link_name);
1880 xbt_free(new_link_name);
1884 if( rc_src >= 0 && rc_dst >= 0 ) break;
1887 route_extended_t new_e_route = NULL;
1888 if(rc_src >= 0 && rc_dst >= 0) {
1889 new_e_route = xbt_new0(s_route_extended_t,1);
1890 new_e_route->generic_route.link_list = links_list;
1891 } else if( !strcmp(src,dst) && are_processing_units ) {
1892 new_e_route = xbt_new0(s_route_extended_t,1);
1893 xbt_dynar_push(links_list,&(global_routing->loopback));
1894 new_e_route->generic_route.link_list = links_list;
1896 xbt_dynar_free(&link_list);
1899 if(!are_processing_units && new_e_route)
1901 rule_route_extended_t ruleroute_extended = (rule_route_extended_t)ruleroute;
1902 new_e_route->src_gateway = remplace(ruleroute_extended->re_src_gateway,list_src,rc_src,list_dst,rc_dst);
1903 new_e_route->dst_gateway = remplace(ruleroute_extended->re_dst_gateway,list_src,rc_src,list_dst,rc_dst);
1906 if(list_src) pcre_free_substring_list(list_src);
1907 if(list_dst) pcre_free_substring_list(list_dst);
1912 static route_extended_t rulebased_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {
1916 static void rulebased_finalize(routing_component_t rc) {
1917 routing_component_rulebased_t routing = (routing_component_rulebased_t) rc;
1919 xbt_dict_free(&routing->dict_processing_units);
1920 xbt_dict_free(&routing->dict_autonomous_systems);
1921 xbt_dynar_free(&routing->list_route);
1922 xbt_dynar_free(&routing->list_ASroute);
1923 /* Delete structure */
1928 /* Creation routing model functions */
1929 static void* model_rulebased_create(void) {
1930 routing_component_rulebased_t new_component = xbt_new0(s_routing_component_rulebased_t,1);
1931 new_component->generic_routing.set_processing_unit = model_rulebased_set_processing_unit;
1932 new_component->generic_routing.set_autonomous_system = model_rulebased_set_autonomous_system;
1933 new_component->generic_routing.set_route = model_rulebased_set_route;
1934 new_component->generic_routing.set_ASroute = model_rulebased_set_ASroute;
1935 new_component->generic_routing.set_bypassroute = model_rulebased_set_bypassroute;
1936 new_component->generic_routing.get_onelink_routes = rulebased_get_onelink_routes;
1937 new_component->generic_routing.get_route = rulebased_get_route;
1938 new_component->generic_routing.get_bypass_route = NULL; //rulebased_get_bypass_route;
1939 new_component->generic_routing.finalize = rulebased_finalize;
1940 /* initialization of internal structures */
1941 new_component->dict_processing_units = xbt_dict_new();
1942 new_component->dict_autonomous_systems = xbt_dict_new();
1943 new_component->list_route = xbt_dynar_new(sizeof(rule_route_t), &rule_route_free);
1944 new_component->list_ASroute = xbt_dynar_new(sizeof(rule_route_extended_t), &rule_route_extended_free);
1945 return new_component;
1948 static void model_rulebased_load(void) {
1949 /* use "surfxml_add_callback" to add a parse function call */
1952 static void model_rulebased_unload(void) {
1953 /* use "surfxml_del_callback" to remove a parse function call */
1956 static void model_rulebased_end(void) {
1959 #endif /* HAVE_PCRE_LIB */
1961 /* ************************************************************************** */
1962 /* ******************************* NO ROUTING ******************************* */
1964 /* Routing model structure */
1966 s_routing_component_t generic_routing;
1967 } s_routing_component_none_t,*routing_component_none_t;
1969 /* Business methods */
1970 static xbt_dynar_t none_get_onelink_routes(routing_component_t rc){
1973 static route_extended_t none_get_route(routing_component_t rc, const char* src,const char* dst){
1976 static route_extended_t none_get_bypass_route(routing_component_t rc, const char* src,const char* dst){
1979 static void none_finalize(routing_component_t rc) {
1983 static void none_set_processing_unit(routing_component_t rc, const char* name) {}
1984 static void none_set_autonomous_system(routing_component_t rc, const char* name) {}
1986 /* Creation routing model functions */
1987 static void* model_none_create(void) {
1988 routing_component_none_t new_component = xbt_new0(s_routing_component_none_t,1);
1989 new_component->generic_routing.set_processing_unit = none_set_processing_unit;
1990 new_component->generic_routing.set_autonomous_system = none_set_autonomous_system;
1991 new_component->generic_routing.set_route = NULL;
1992 new_component->generic_routing.set_ASroute = NULL;
1993 new_component->generic_routing.set_bypassroute = NULL;
1994 new_component->generic_routing.get_route = none_get_route;
1995 new_component->generic_routing.get_onelink_routes = none_get_onelink_routes;
1996 new_component->generic_routing.get_bypass_route = none_get_bypass_route;
1997 new_component->generic_routing.finalize = none_finalize;
1998 return new_component;
2001 static void model_none_load(void) {}
2002 static void model_none_unload(void) {}
2003 static void model_none_end(void) {}
2005 /* ************************************************** */
2006 /* ********** PATERN FOR NEW ROUTING **************** */
2008 /* The minimal configuration of a new routing model need the next functions,
2009 * also you need to set at the start of the file, the new model in the model
2010 * list. Remember keep the null ending of the list.
2012 /*** Routing model structure ***/
2014 // s_routing_component_t generic_routing;
2015 // /* things that your routing model need */
2016 // } s_routing_component_NEW_t,*routing_component_NEW_t;
2018 /*** Parse routing model functions ***/
2019 // static void model_NEW_set_processing_unit(routing_component_t rc, const char* name) {}
2020 // static void model_NEW_set_autonomous_system(routing_component_t rc, const char* name) {}
2021 // static void model_NEW_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {}
2022 // static void model_NEW_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t route) {}
2023 // static void model_NEW_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {}
2025 /*** Business methods ***/
2026 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2027 // static route_extended_t NEW_get_bypass_route(routing_component_t rc, const char* src,const char* dst) {return NULL;}
2028 // static void NEW_finalize(routing_component_t rc) { xbt_free(rc);}
2030 /*** Creation routing model functions ***/
2031 // static void* model_NEW_create(void) {
2032 // routing_component_NEW_t new_component = xbt_new0(s_routing_component_NEW_t,1);
2033 // new_component->generic_routing.set_processing_unit = model_NEW_set_processing_unit;
2034 // new_component->generic_routing.set_autonomous_system = model_NEW_set_autonomous_system;
2035 // new_component->generic_routing.set_route = model_NEW_set_route;
2036 // new_component->generic_routing.set_ASroute = model_NEW_set_ASroute;
2037 // new_component->generic_routing.set_bypassroute = model_NEW_set_bypassroute;
2038 // new_component->generic_routing.get_route = NEW_get_route;
2039 // new_component->generic_routing.get_bypass_route = NEW_get_bypass_route;
2040 // new_component->generic_routing.finalize = NEW_finalize;
2041 // /* initialization of internal structures */
2042 // return new_component;
2043 // } /* mandatory */
2044 // static void model_NEW_load(void) {} /* mandatory */
2045 // static void model_NEW_unload(void) {} /* mandatory */
2046 // static void model_NEW_end(void) {} /* mandatory */
2048 /* ************************************************************************** */
2049 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
2051 static void generic_set_processing_unit(routing_component_t rc, const char* name) {
2052 DEBUG1("Load process unit \"%s\"",name);
2053 model_type_t modeltype = rc->routing;
2054 int *id = xbt_new0(int,1);
2055 xbt_dict_t _to_index;
2056 if(modeltype==&routing_models[SURF_MODEL_FULL])
2057 _to_index = ((routing_component_full_t)rc)->to_index;
2059 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
2060 _to_index = ((routing_component_floyd_t)rc)->to_index;
2062 else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
2063 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE])
2064 _to_index = ((routing_component_dijkstra_t)rc)->to_index;
2066 else xbt_die("\"generic_set_processing_unit\" not supported");
2067 *id = xbt_dict_length(_to_index);
2068 xbt_dict_set(_to_index,name,id,xbt_free);
2071 static void generic_set_autonomous_system(routing_component_t rc, const char* name) {
2072 DEBUG1("Load Autonomous system \"%s\"",name);
2073 model_type_t modeltype = rc->routing;
2074 int *id = xbt_new0(int,1);
2075 xbt_dict_t _to_index;
2076 if(modeltype==&routing_models[SURF_MODEL_FULL])
2077 _to_index = ((routing_component_full_t)rc)->to_index;
2079 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
2080 _to_index = ((routing_component_floyd_t)rc)->to_index;
2082 else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
2083 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE])
2084 _to_index = ((routing_component_dijkstra_t)rc)->to_index;
2086 else xbt_die("\"generic_set_autonomous_system\" not supported");
2087 *id = xbt_dict_length(_to_index);
2088 xbt_dict_set(_to_index,name,id,xbt_free);
2091 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {
2092 DEBUG2("Load Route from \"%s\" to \"%s\"",src,dst);
2093 model_type_t modeltype = rc->routing;
2094 xbt_dict_t _parse_routes;
2095 xbt_dict_t _to_index;
2097 int *src_id, *dst_id;
2099 if(modeltype==&routing_models[SURF_MODEL_FULL]) {
2100 _parse_routes = ((routing_component_full_t)rc)->parse_routes;
2101 _to_index = ((routing_component_full_t)rc)->to_index;
2103 } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
2104 _parse_routes = ((routing_component_floyd_t)rc)->parse_routes;
2105 _to_index = ((routing_component_floyd_t)rc)->to_index;
2107 } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
2108 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2109 _parse_routes = ((routing_component_dijkstra_t)rc)->parse_routes;
2110 _to_index = ((routing_component_dijkstra_t)rc)->to_index;
2112 } else xbt_die("\"generic_set_route\" not supported");
2114 src_id = xbt_dict_get_or_null(_to_index, src);
2115 dst_id = xbt_dict_get_or_null(_to_index, dst);
2117 xbt_assert2(src_id&&dst_id,"Network elements %s or %s not found", src, dst);
2118 route_name = bprintf("%d#%d",*src_id,*dst_id);
2120 xbt_assert2(xbt_dynar_length(route->link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
2121 xbt_assert2(!xbt_dict_get_or_null(_parse_routes,route_name),
2122 "The route between \"%s\" and \"%s\" already exist",src,dst);
2124 xbt_dict_set(_parse_routes, route_name, route, NULL);
2125 xbt_free(route_name);
2128 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
2129 DEBUG4("Load ASroute from \"%s(%s)\" to \"%s(%s)\"",src,e_route->src_gateway,dst,e_route->dst_gateway);
2130 model_type_t modeltype = rc->routing;
2131 xbt_dict_t _parse_routes;
2132 xbt_dict_t _to_index;
2134 int *src_id, *dst_id;
2136 if(modeltype==&routing_models[SURF_MODEL_FULL]) {
2137 _parse_routes = ((routing_component_full_t)rc)->parse_routes;
2138 _to_index = ((routing_component_full_t)rc)->to_index;
2140 } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
2141 _parse_routes = ((routing_component_floyd_t)rc)->parse_routes;
2142 _to_index = ((routing_component_floyd_t)rc)->to_index;
2144 } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
2145 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2146 _parse_routes = ((routing_component_dijkstra_t)rc)->parse_routes;
2147 _to_index = ((routing_component_dijkstra_t)rc)->to_index;
2149 } else xbt_die("\"generic_set_route\" not supported");
2151 src_id = xbt_dict_get_or_null(_to_index, src);
2152 dst_id = xbt_dict_get_or_null(_to_index, dst);
2154 xbt_assert2(src_id&&dst_id,"Network elements %s or %s not found", src, dst);
2155 route_name = bprintf("%d#%d",*src_id,*dst_id);
2157 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
2158 xbt_assert4(!xbt_dict_get_or_null(_parse_routes,route_name),
2159 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
2161 xbt_dict_set(_parse_routes, route_name, e_route, NULL);
2162 xbt_free(route_name);
2165 static void generic_set_bypassroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
2166 DEBUG2("Load bypassRoute from \"%s\" to \"%s\"",src,dst);
2167 model_type_t modeltype = rc->routing;
2168 xbt_dict_t dict_bypassRoutes;
2170 if(modeltype==&routing_models[SURF_MODEL_FULL]) {
2171 dict_bypassRoutes = ((routing_component_full_t)rc)->bypassRoutes;
2172 } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
2173 dict_bypassRoutes = ((routing_component_floyd_t)rc)->bypassRoutes;
2174 } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
2175 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2176 dict_bypassRoutes = ((routing_component_dijkstra_t)rc)->bypassRoutes;
2177 } else xbt_die("\"generic_set_bypassroute\" not supported");
2178 route_name = bprintf("%s#%s",src,dst);
2179 xbt_assert2(xbt_dynar_length(e_route->generic_route.link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
2180 xbt_assert4(!xbt_dict_get_or_null(dict_bypassRoutes,route_name),
2181 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
2183 route_extended_t new_e_route = generic_new_extended_route(SURF_ROUTING_RECURSIVE,e_route,0);
2184 xbt_dynar_free( &(e_route->generic_route.link_list) );
2187 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, (void(*)(void*))generic_free_extended_route );
2188 xbt_free(route_name);
2191 /* ************************************************************************** */
2192 /* *********************** GENERIC BUSINESS METHODS ************************* */
2194 static xbt_dynar_t generic_get_onelink_routes (routing_component_t rc)
2196 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
2199 static route_extended_t generic_get_bypassroute(routing_component_t rc, const char* src, const char* dst) {
2200 model_type_t modeltype = rc->routing;
2201 xbt_dict_t dict_bypassRoutes;
2203 if(modeltype==&routing_models[SURF_MODEL_FULL]) {
2204 dict_bypassRoutes = ((routing_component_full_t)rc)->bypassRoutes;
2205 } else if(modeltype==&routing_models[SURF_MODEL_FLOYD]) {
2206 dict_bypassRoutes = ((routing_component_floyd_t)rc)->bypassRoutes;
2207 } else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
2208 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE]) {
2209 dict_bypassRoutes = ((routing_component_dijkstra_t)rc)->bypassRoutes;
2210 } else xbt_die("\"generic_get_bypassroute\" not supported");
2213 routing_component_t src_as, dst_as;
2214 int index_src, index_dst;
2215 xbt_dynar_t path_src = NULL;
2216 xbt_dynar_t path_dst = NULL;
2217 routing_component_t current = NULL;
2218 routing_component_t* current_src = NULL;
2219 routing_component_t* current_dst = NULL;
2221 /* (1) find the as where the src and dst are located */
2222 src_as = ((network_element_info_t)xbt_dict_get_or_null(global_routing->where_network_elements,src))->rc_component;
2223 dst_as = ((network_element_info_t)xbt_dict_get_or_null(global_routing->where_network_elements,dst))->rc_component;
2224 xbt_assert2(src_as&&dst_as, "Ask for route \"from\"(%s) or \"to\"(%s) no found",src,dst);
2226 /* (2) find the path to the root routing component */
2227 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
2229 while( current != NULL ) {
2230 xbt_dynar_push(path_src,¤t);
2231 current = current->routing_father;
2233 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
2235 while( current != NULL ) {
2236 xbt_dynar_push(path_dst,¤t);
2237 current = current->routing_father;
2240 /* (3) find the common father */
2241 index_src = (path_src->used)-1;
2242 index_dst = (path_dst->used)-1;
2243 current_src = xbt_dynar_get_ptr(path_src,index_src);
2244 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
2245 while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
2246 routing_component_t *tmp_src,*tmp_dst;
2247 tmp_src = xbt_dynar_pop_ptr(path_src);
2248 tmp_dst = xbt_dynar_pop_ptr(path_dst);
2251 current_src = xbt_dynar_get_ptr(path_src,index_src);
2252 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
2255 int max_index_src = (path_src->used)-1;
2256 int max_index_dst = (path_dst->used)-1;
2258 int max_index = max(max_index_src,max_index_dst);
2261 route_extended_t e_route_bypass = NULL;
2263 for( max=0;max<=max_index;max++)
2267 if( i <= max_index_src && max <= max_index_dst ) {
2268 char* route_name = bprintf("%s#%s",
2269 (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,i)))->name,
2270 (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,max)))->name);
2271 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
2272 xbt_free(route_name);
2274 if( e_route_bypass ) break;
2275 if( max <= max_index_src && i <= max_index_dst ) {
2276 char* route_name = bprintf("%s#%s",
2277 (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,max)))->name,
2278 (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,i)))->name);
2279 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
2280 xbt_free(route_name);
2282 if( e_route_bypass ) break;
2285 if( e_route_bypass ) break;
2287 if( max <= max_index_src && max <= max_index_dst ) {
2288 char* route_name = bprintf("%s#%s",
2289 (*(routing_component_t*)(xbt_dynar_get_ptr(path_src,max)))->name,
2290 (*(routing_component_t*)(xbt_dynar_get_ptr(path_dst,max)))->name);
2291 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes,route_name);
2292 xbt_free(route_name);
2294 if( e_route_bypass ) break;
2297 xbt_dynar_free(&path_src);
2298 xbt_dynar_free(&path_dst);
2300 route_extended_t new_e_route = NULL;
2302 if(e_route_bypass) {
2305 new_e_route = xbt_new0(s_route_extended_t,1);
2306 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
2307 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
2308 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
2309 xbt_dynar_foreach(e_route_bypass->generic_route.link_list, cpt, link) {
2310 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
2317 /* ************************************************************************** */
2318 /* ************************* GENERIC AUX FUNCTIONS ************************** */
2320 static route_extended_t generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy, void* data, int order) {
2323 route_extended_t e_route, new_e_route;
2326 xbt_dynar_t links, links_id;
2328 new_e_route = xbt_new0(s_route_extended_t,1);
2329 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
2330 new_e_route->src_gateway = NULL;
2331 new_e_route->dst_gateway = NULL;
2333 xbt_assert0(hierarchy == SURF_ROUTING_BASE || hierarchy == SURF_ROUTING_RECURSIVE,
2334 "the hierarchy type is not defined");
2336 if(hierarchy == SURF_ROUTING_BASE ) {
2338 route = (route_t)data;
2339 links = route->link_list;
2341 } else if(hierarchy == SURF_ROUTING_RECURSIVE ) {
2343 e_route = (route_extended_t)data;
2344 xbt_assert0(e_route->src_gateway&&e_route->dst_gateway,"bad gateway, is null");
2345 links = e_route->generic_route.link_list;
2347 /* remeber not erase the gateway names */
2348 new_e_route->src_gateway = e_route->src_gateway;
2349 new_e_route->dst_gateway = e_route->dst_gateway;
2352 links_id = new_e_route->generic_route.link_list;
2354 xbt_dynar_foreach(links, cpt, link_name) {
2356 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2360 xbt_dynar_push(links_id,&link);
2362 xbt_dynar_unshift(links_id,&link);
2365 THROW1(mismatch_error,0,"Link %s not found", link_name);
2371 static void generic_free_route(route_t route) {
2373 xbt_dynar_free(&(route->link_list));
2378 static void generic_free_extended_route(route_extended_t e_route) {
2380 xbt_dynar_free(&(e_route->generic_route.link_list));
2381 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
2382 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
2387 static routing_component_t generic_as_exist(routing_component_t find_from, routing_component_t to_find) {
2388 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2389 xbt_dict_cursor_t cursor = NULL;
2392 routing_component_t elem;
2393 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
2394 if( to_find == elem || generic_as_exist(elem,to_find) ){
2399 if(found) return to_find;
2403 static routing_component_t generic_autonomous_system_exist(routing_component_t rc, char* element) {
2404 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
2405 routing_component_t element_as, result, elem;
2406 xbt_dict_cursor_t cursor = NULL;
2408 element_as = ((network_element_info_t)xbt_dict_get_or_null(global_routing->where_network_elements,element))->rc_component;
2409 result = ((routing_component_t)-1);
2411 result = generic_as_exist(rc,element_as);
2416 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
2417 found = !strcmp(elem->name,element);
2420 if( found ) return element_as;
2425 static routing_component_t generic_processing_units_exist(routing_component_t rc, char* element) {
2426 routing_component_t element_as;
2427 element_as = ((network_element_info_t)xbt_dict_get_or_null(global_routing->where_network_elements,element))->rc_component;
2428 if(element_as==rc) return element_as;
2429 return generic_as_exist(rc,element_as);
2432 static void generic_src_dst_check(routing_component_t rc, const char* src, const char* dst) {
2434 routing_component_t src_as = ((network_element_info_t)xbt_dict_get_or_null(global_routing->where_network_elements,src))->rc_component;
2435 routing_component_t dst_as = ((network_element_info_t)xbt_dict_get_or_null(global_routing->where_network_elements,dst))->rc_component;
2437 xbt_assert3(src_as != NULL && dst_as != NULL,
2438 "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",src,dst,rc->name);
2439 xbt_assert4(src_as == dst_as,
2440 "The src(%s in %s) and dst(%s in %s) are in differents AS",src,src_as->name,dst,dst_as->name);
2441 xbt_assert2(rc == dst_as,
2442 "The routing component of src and dst is not the same as the network elements belong (%s==%s)",rc->name,dst_as->name);
2445 static void parse_Sconfig(void)
2448 DEBUG0("WARNING tag config not yet implemented.");
2451 static void routing_full_parse_Scluster(void)
2453 static int AX_ptr = 0;
2455 char *cluster_id = A_surfxml_cluster_id;
2456 char *cluster_prefix = A_surfxml_cluster_prefix;
2457 char *cluster_suffix = A_surfxml_cluster_suffix;
2458 char *cluster_radical = A_surfxml_cluster_radical;
2459 char *cluster_power = A_surfxml_cluster_power;
2460 char *cluster_bw = A_surfxml_cluster_bw;
2461 char *cluster_lat = A_surfxml_cluster_lat;
2462 char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2463 char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2464 char *host_id, *groups, *link_id;
2465 char *router_id, *link_router, *link_backbone, *route_src_dst;
2468 xbt_dynar_t radical_elements;
2469 xbt_dynar_t radical_ends;
2470 #ifndef HAVE_PCRE_LIB
2471 xbt_dynar_t tab_elements_num = xbt_dynar_new(sizeof(int), NULL);
2472 char *route_src,*route_dst;
2476 static unsigned int surfxml_buffer_stack_stack_ptr = 1;
2477 static unsigned int surfxml_buffer_stack_stack[1024];
2479 surfxml_buffer_stack_stack[0]= 0;
2481 surfxml_bufferstack_push(1);
2483 SURFXML_BUFFER_SET(AS_id, cluster_id);
2484 #ifdef HAVE_PCRE_LIB
2485 SURFXML_BUFFER_SET(AS_routing, "RuleBased");
2486 DEBUG1("<AS id=\"%s\"\trouting=\"RuleBased\">",cluster_id);
2488 SURFXML_BUFFER_SET(AS_routing, "Full");
2489 DEBUG1("<AS id=\"%s\"\trouting=\"Full\">",cluster_id);
2491 SURFXML_START_TAG(AS);
2493 radical_elements = xbt_str_split(cluster_radical, ",");
2494 xbt_dynar_foreach(radical_elements, iter, groups)
2496 radical_ends = xbt_str_split(groups, "-");
2497 switch (xbt_dynar_length(radical_ends))
2500 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
2501 host_id = bprintf("%s_%d%s", cluster_prefix, start, cluster_suffix);
2502 #ifndef HAVE_PCRE_LIB
2503 xbt_dynar_push_as(tab_elements_num, int, start);
2505 link_id = bprintf("%s_link_%d", cluster_id, start);
2507 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>",host_id,cluster_power);
2508 SURFXML_BUFFER_SET(host_id, host_id);
2509 SURFXML_BUFFER_SET(host_power, cluster_power);
2510 SURFXML_BUFFER_SET(host_availability, "1.0");
2511 SURFXML_BUFFER_SET(host_availability_file, "");
2512 A_surfxml_host_state = A_surfxml_host_state_ON;
2513 SURFXML_BUFFER_SET(host_state_file, "");
2514 SURFXML_START_TAG(host);
2515 SURFXML_END_TAG(host);
2517 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>",link_id,cluster_bw,cluster_lat);
2518 SURFXML_BUFFER_SET(link_id, link_id);
2519 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
2520 SURFXML_BUFFER_SET(link_latency, cluster_lat);
2521 SURFXML_BUFFER_SET(link_bandwidth_file, "");
2522 SURFXML_BUFFER_SET(link_latency_file, "");
2523 A_surfxml_link_state = A_surfxml_link_state_ON;
2524 SURFXML_BUFFER_SET(link_state_file, "");
2525 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2526 SURFXML_START_TAG(link);
2527 SURFXML_END_TAG(link);
2533 surf_parse_get_int(&start, xbt_dynar_get_as(radical_ends, 0, char *));
2534 surf_parse_get_int(&end, xbt_dynar_get_as(radical_ends, 1, char *));
2535 DEBUG2("Create hosts and links from %d to %d",start,end);
2536 for (i = start; i <= end; i++)
2538 host_id = bprintf("%s_%d%s", cluster_prefix, i, cluster_suffix);
2539 #ifndef HAVE_PCRE_LIB
2540 xbt_dynar_push_as(tab_elements_num, int, i);
2542 link_id = bprintf("%s_link_%d", cluster_id, i);
2544 DEBUG2("<host\tid=\"%s\"\tpower=\"%s\"/>",host_id,cluster_power);
2545 SURFXML_BUFFER_SET(host_id, host_id);
2546 SURFXML_BUFFER_SET(host_power, cluster_power);
2547 SURFXML_BUFFER_SET(host_availability, "1.0");
2548 SURFXML_BUFFER_SET(host_availability_file, "");
2549 A_surfxml_host_state = A_surfxml_host_state_ON;
2550 SURFXML_BUFFER_SET(host_state_file, "");
2551 SURFXML_START_TAG(host);
2552 SURFXML_END_TAG(host);
2554 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>",link_id,cluster_bw,cluster_lat);
2555 SURFXML_BUFFER_SET(link_id, link_id);
2556 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
2557 SURFXML_BUFFER_SET(link_latency, cluster_lat);
2558 SURFXML_BUFFER_SET(link_bandwidth_file, "");
2559 SURFXML_BUFFER_SET(link_latency_file, "");
2560 A_surfxml_link_state = A_surfxml_link_state_ON;
2561 SURFXML_BUFFER_SET(link_state_file, "");
2562 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2563 SURFXML_START_TAG(link);
2564 SURFXML_END_TAG(link);
2569 DEBUG0("Malformed radical");
2572 xbt_dynar_free(&radical_ends);
2576 router_id = bprintf("%s_%s_router%s",cluster_prefix,cluster_id,cluster_suffix);
2577 link_router = bprintf("%s_link_%s_router",cluster_id,cluster_id);
2578 link_backbone = bprintf("%s_backbone",cluster_id);
2580 DEBUG1("<router id=\"%s\"/>",router_id);
2581 SURFXML_BUFFER_SET(router_id, router_id);;
2582 SURFXML_START_TAG(router);
2583 SURFXML_END_TAG(router);
2585 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>",link_router,cluster_bw,cluster_lat);
2586 SURFXML_BUFFER_SET(link_id, link_router);
2587 SURFXML_BUFFER_SET(link_bandwidth, cluster_bw);
2588 SURFXML_BUFFER_SET(link_latency, cluster_lat);
2589 SURFXML_BUFFER_SET(link_bandwidth_file, "");
2590 SURFXML_BUFFER_SET(link_latency_file, "");
2591 A_surfxml_link_state = A_surfxml_link_state_ON;
2592 SURFXML_BUFFER_SET(link_state_file, "");
2593 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2594 SURFXML_START_TAG(link);
2595 SURFXML_END_TAG(link);
2597 DEBUG3("<link\tid=\"%s\"\tbw=\"%s\"\tlat=\"%s\"/>",link_backbone,cluster_bb_bw,cluster_bb_lat);
2598 SURFXML_BUFFER_SET(link_id, link_backbone);
2599 SURFXML_BUFFER_SET(link_bandwidth, cluster_bb_bw);
2600 SURFXML_BUFFER_SET(link_latency, cluster_bb_lat);
2601 SURFXML_BUFFER_SET(link_bandwidth_file, "");
2602 SURFXML_BUFFER_SET(link_latency_file, "");
2603 A_surfxml_link_state = A_surfxml_link_state_ON;
2604 SURFXML_BUFFER_SET(link_state_file, "");
2605 A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
2606 SURFXML_START_TAG(link);
2607 SURFXML_END_TAG(link);
2609 char *new_suffix = bprintf("%s","");
2611 radical_elements = xbt_str_split(cluster_suffix, ".");
2612 xbt_dynar_foreach(radical_elements, iter, groups)
2614 if(strcmp(groups,""))
2616 new_suffix = bprintf("%s\\.%s",new_suffix,groups);
2619 route_src_dst = bprintf("%s_(.*)%s",cluster_prefix,new_suffix);
2623 #ifdef HAVE_PCRE_LIB
2625 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">",route_src_dst,route_src_dst);
2626 SURFXML_BUFFER_SET(route_src, route_src_dst);
2627 SURFXML_BUFFER_SET(route_dst, route_src_dst);
2628 SURFXML_START_TAG(route);
2630 DEBUG1("<link:ctn\tid=\"%s_link_$1src\"/>",cluster_id);
2631 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1src",cluster_id));
2632 SURFXML_START_TAG(link_ctn);
2633 SURFXML_END_TAG(link_ctn);
2635 DEBUG1("<link:ctn\tid=\"%s_backbone\"/>",cluster_id);
2636 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone",cluster_id));
2637 SURFXML_START_TAG(link_ctn);
2638 SURFXML_END_TAG(link_ctn);
2640 DEBUG1("<link:ctn\tid=\"%s_link_$1dst\"/>",cluster_id);
2641 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_link_$1dst",cluster_id));
2642 SURFXML_START_TAG(link_ctn);
2643 SURFXML_END_TAG(link_ctn);
2646 SURFXML_END_TAG(route);
2648 for(i=0 ; i<=xbt_dynar_length(tab_elements_num) ; i++)
2650 for(j=0 ; j<=xbt_dynar_length(tab_elements_num) ; j++)
2652 if(i == xbt_dynar_length(tab_elements_num))
2654 route_src = router_id;
2658 route_src = bprintf("%s_%d%s",cluster_prefix,xbt_dynar_get_as(tab_elements_num,i,int),cluster_suffix);
2661 if(j == xbt_dynar_length(tab_elements_num))
2663 route_dst = router_id;
2667 route_dst = bprintf("%s_%d%s",cluster_prefix,xbt_dynar_get_as(tab_elements_num,j,int),cluster_suffix);
2670 DEBUG2("<route\tsrc=\"%s\"\tdst=\"%s\">",route_src,route_dst);
2671 SURFXML_BUFFER_SET(route_src, route_src);
2672 SURFXML_BUFFER_SET(route_dst, route_dst);
2673 SURFXML_START_TAG(route);
2675 if(i == xbt_dynar_length(tab_elements_num))
2677 route_src = link_router;
2681 route_src = bprintf("%s_link_%d",cluster_id,xbt_dynar_get_as(tab_elements_num,i,int));
2684 if(j == xbt_dynar_length(tab_elements_num))
2686 route_dst = link_router;
2690 route_dst = bprintf("%s_link_%d",cluster_id,xbt_dynar_get_as(tab_elements_num,j,int));
2693 DEBUG1("<link:ctn\tid=\"%s\"/>",route_src);
2694 SURFXML_BUFFER_SET(link_ctn_id, route_src);
2695 SURFXML_START_TAG(link_ctn);
2696 SURFXML_END_TAG(link_ctn);
2698 DEBUG1("<link:ctn\tid=\"%s_backbone\"/>",cluster_id);
2699 SURFXML_BUFFER_SET(link_ctn_id, bprintf("%s_backbone",cluster_id));
2700 SURFXML_START_TAG(link_ctn);
2701 SURFXML_END_TAG(link_ctn);
2703 DEBUG1("<link:ctn\tid=\"%s\"/>",route_dst);
2704 SURFXML_BUFFER_SET(link_ctn_id, route_dst);
2705 SURFXML_START_TAG(link_ctn);
2706 SURFXML_END_TAG(link_ctn);
2709 SURFXML_END_TAG(route);
2712 xbt_dynar_free(&tab_elements_num);
2716 SURFXML_END_TAG(AS);
2719 surfxml_bufferstack_pop(1);
2723 * New methods to init the routing model component from the lua script
2727 * calling parse_S_AS_lua with lua values
2729 void routing_AS_init(const char* AS_id,const char* AS_routing)
2731 parse_S_AS_lua((char*)AS_id,(char*)AS_routing);
2735 * calling parse_E_AS_lua to fisnish the creation of routing component
2737 void routing_AS_end(const char *AS_id)
2739 parse_E_AS_lua((char*)AS_id);
2743 * add a host to the network element list
2746 void routing_add_host(const char* host_id)
2748 parse_S_host_lua((char*)host_id);
2752 * Set a new link on the actual list of link for a route or ASroute
2754 void routing_add_link(const char* link_id)
2756 parse_E_link_c_ctn_new_elem_lua((char*)link_id);
2760 *Set the endpoints for a route
2762 void routing_set_route(const char* src_id,const char *dst_id)
2764 parse_S_route_new_and_endpoints_lua((char*)src_id,(char*)dst_id);
2768 * Store the route by calling parse_E_route_store_route
2770 void routing_store_route(void)
2772 parse_E_route_store_route();