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. */
8 #include "surf_private.h"
11 #include "xbt/config.h"
12 #include "xbt/graph.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route,surf,"Routing part of surf");
17 ////////////////////////////////////////////////////////////////////////////////
18 // HERE START THE NEW CODE
19 ////////////////////////////////////////////////////////////////////////////////
21 //...... DEBUG ONLY .... //
22 static void print_tree(routing_component_t rc);
23 static void print_global(void);
24 static void print_AS_start(void);
25 static void print_AS_end(void);
26 static void print_host(void);
27 static void print_link(void);
28 static void print_route(void);
29 static void print_ctn(void);
30 static void DEBUG_exit(void);
32 ////////////////////////////////////////////////////////////////////////////////
33 // HERE START THE NEW CODE
34 ////////////////////////////////////////////////////////////////////////////////
37 routing_global_t global_routing = NULL;
38 routing_component_t current_routing = NULL;
39 model_type_t current_routing_model = NULL;
41 /* Prototypes of each model */
42 static void* model_full_create(void); /* create structures for full routing model */
43 static void model_full_load(void); /* load parse functions for full routing model */
44 static void model_full_unload(void); /* unload parse functions for full routing model */
45 static void model_full_end(void); /* finalize the creation of full routing model */
47 static void* model_floyd_create(void); /* create structures for floyd routing model */
48 static void model_floyd_load(void); /* load parse functions for floyd routing model */
49 static void model_floyd_unload(void); /* unload parse functions for floyd routing model */
50 static void model_floyd_end(void); /* finalize the creation of floyd routing model */
52 static void* model_dijkstra_both_create(int cached); /* create by calling dijkstra or dijkstracache */
53 static void* model_dijkstra_create(void); /* create structures for dijkstra routing model */
54 static void* model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
55 static void model_dijkstra_both_load(void); /* load parse functions for dijkstra routing model */
56 static void model_dijkstra_both_unload(void); /* unload parse functions for dijkstra routing model */
57 static void model_dijkstra_both_end(void); /* finalize the creation of dijkstra routing model */
59 static void* model_none_create(void); /* none routing model */
60 static void model_none_load(void); /* none routing model */
61 static void model_none_unload(void); /* none routing model */
62 static void model_none_end(void); /* none routing model */
64 /* Table of routing models */
66 // SURF_MODEL_FULL = 0,
68 // SURF_MODEL_DIJKSTRA,
69 // SURF_MODEL_DIJKSTRACACHE,
71 // } e_surf_model_type_t;
73 /* this lines are olny for replace use like index in the model table */
74 #define SURF_MODEL_FULL 0
75 #define SURF_MODEL_FLOYD 1
76 #define SURF_MODEL_DIJKSTRA 2
77 #define SURF_MODEL_DIJKSTRACACHE 3
78 #define SURF_MODEL_NONE 4
80 /* must be finish with null and carefull if change de order */
81 struct s_model_type routing_models[] =
82 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)",
83 model_full_create, model_full_load, model_full_unload, model_full_end },
84 {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
85 model_floyd_create, model_floyd_load, model_floyd_unload, model_floyd_end },
86 {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
87 model_dijkstra_create ,model_dijkstra_both_load, model_dijkstra_both_unload, model_dijkstra_both_end },
88 {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
89 model_dijkstracache_create, model_dijkstra_both_load, model_dijkstra_both_unload, model_dijkstra_both_end },
90 {"none", "No routing (usable with Constant network only)",
91 model_none_create, model_none_load, model_none_unload, model_none_end },
92 {NULL,NULL,NULL,NULL,NULL,NULL}};
94 /* global parse functions */
96 static char* src = NULL; /* temporary store the source name of a route */
97 static char* dst = NULL; /* temporary store the destination name of a route */
98 static char* gw_src = NULL; /* temporary store the gateway source name of a route */
99 static char* gw_dst = NULL; /* temporary store the gateway destination name of a route */
100 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
103 * \brief Add a "host" to the network element list
105 static void parse_S_host(void) {
106 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
107 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_host_id),
108 "Reading a host, processing unit \"%s\" already exist",A_surfxml_host_id);
109 // FIXME: checked by parser
110 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
111 // "Bad declaration of processing unit \"%s\"",A_surfxml_host_id);
112 (*(current_routing->set_processing_units))(current_routing,A_surfxml_host_id);
113 xbt_dict_set(global_routing->where_network_elements,A_surfxml_host_id,(void*)current_routing,NULL);
117 * \brief Add a "router" to the network element list
119 static void parse_S_router(void) {
120 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
121 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_router_id),
122 "Reading a router, processing unit \"%s\" already exist",A_surfxml_router_id);
123 // FIXME: checked by parser
124 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
125 // "Bad declaration of processing unit \"%s\"",A_surfxml_host_id);
126 (*(current_routing->set_processing_units))(current_routing,A_surfxml_router_id);
127 xbt_dict_set(global_routing->where_network_elements,A_surfxml_router_id,(void*)current_routing,NULL);
131 * \brief Set the endponints for a route
133 static void parse_S_route_new_and_endpoints(void) {
134 // FIXME: checked by parser
135 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
136 // "Bad declaration of route in \"%s\"",current_routing->name);
137 if( src != NULL && dst != NULL && link_list != NULL )
138 THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_route_src,A_surfxml_route_dst);
139 src = A_surfxml_route_src;
140 dst = A_surfxml_route_dst;
141 link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
145 * \brief Set the endponints and gateways for a ASroute
147 static void parse_S_ASroute_new_and_endpoints(void) {
148 // FIXME: checked by parser
149 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
150 // "Bad declaration of ASroute in \"%s\"",current_routing->name);
151 if( src != NULL && dst != NULL && link_list != NULL )
152 THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_ASroute_src,A_surfxml_ASroute_dst);
153 src = A_surfxml_ASroute_src;
154 dst = A_surfxml_ASroute_dst;
155 gw_src = A_surfxml_ASroute_gw_src;
156 gw_dst = A_surfxml_ASroute_gw_dst;
157 link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
161 * \brief Set a new link on the actual list of link for a route or ASroute
163 static void parse_E_link_c_ctn_new_elem(void) {
165 val = xbt_strdup(A_surfxml_link_c_ctn_id);
166 xbt_dynar_push(link_list, &val);
170 * \brief Store de route by calling the set_route function of the current routing component
172 static void parse_E_route_store_route(void) {
173 // FIXME: checked by parser
174 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
175 // "Bad declaration of route in \"%s\"",current_routing->name);
176 route_t route = xbt_new0(s_route_t,1);
177 route->link_list = link_list;
178 // TODO check if are correct
179 (*(current_routing->set_route))(current_routing,src,dst,route);
186 * \brief Store de ASroute by calling the set_ASroute function of the current routing component
188 static void parse_E_ASroute_store_route(void) {
189 // FIXME: checked by parser
190 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
191 // "Bad declaration of ASroute in \"%s\"",current_routing->name);
192 route_extended_t e_route = xbt_new0(s_route_extended_t,1);
193 e_route->generic_route.link_list = link_list;
194 e_route->src_gateway = xbt_strdup(gw_src);
195 e_route->dst_gateway = xbt_strdup(gw_dst);
196 // TODO check if are correct
197 (*(current_routing->set_ASroute))(current_routing,src,dst,e_route);
206 * \brief Make a new routing component
208 * Detect the routing model type of the routing component, make the new structure and
209 * set the parsing functions to allows parsing the part of the routing tree
211 static void parse_S_AS(void) {
212 routing_component_t new_routing;
213 model_type_t model = NULL;
214 char* wanted = A_surfxml_AS_routing;
216 /* seach the routing model */
217 for (cpt=0;routing_models[cpt].name;cpt++)
218 if (!strcmp(wanted,routing_models[cpt].name))
219 model = &routing_models[cpt];
220 /* if its not exist, error */
221 if( model == NULL ) {
222 fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
223 for (cpt=0;routing_models[cpt].name;cpt++)
224 if (!strcmp(wanted,routing_models[cpt].name))
225 fprintf(stderr," %s: %s\n",routing_models[cpt].name,routing_models[cpt].desc);
229 /* make a new routing component */
230 new_routing = (routing_component_t)(*(model->create))();
232 /* FIXME: for now, if I forget to declare */
233 xbt_assert1( new_routing->set_processing_units,
234 "Bad routing type, \"set_processing_units\" is not declared for \"%s\"",A_surfxml_AS_id);
235 xbt_assert1( new_routing->set_autonomous_system,
236 "Bad routing type, \"set_autonomous_system\" is not declared for \"%s\"",A_surfxml_AS_id);
237 xbt_assert1( new_routing->set_route,
238 "Bad routing type, \"set_route\" is not declared for \"%s\"",A_surfxml_AS_id);
239 xbt_assert1( new_routing->set_ASroute,
240 "Bad routing type, \"set_ASroute\" is not declared for \"%s\"",A_surfxml_AS_id);
241 xbt_assert1( new_routing->finalize,
242 "Bad routing type, \"finalize\" is not declared for \"%s\"",A_surfxml_AS_id);
244 new_routing->routing = model;
245 new_routing->hierarchy = SURF_ROUTING_NULL;
246 new_routing->name = xbt_strdup(A_surfxml_AS_id);
247 new_routing->routing_sons = xbt_dict_new();
248 INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
250 if( current_routing == NULL && global_routing->root == NULL ){
252 /* it is the first one */
253 new_routing->routing_father = NULL;
254 global_routing->root = new_routing;
256 } else if( current_routing != NULL && global_routing->root != NULL ) {
258 xbt_assert1(!xbt_dict_get_or_null(current_routing->routing_sons,A_surfxml_AS_id),
259 "The AS \"%s\" already exist",A_surfxml_AS_id);
260 /* it is a part of the tree */
261 new_routing->routing_father = current_routing;
262 /* set the father behavior */
263 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
264 // FIXME: checked by parser
265 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
266 // "Bad declaration of AS \"%s\"",A_surfxml_AS_id);
267 /* add to the sons dictionary */
268 xbt_dict_set(current_routing->routing_sons,A_surfxml_AS_id,(void*)new_routing,NULL);
269 /* add to the father element list */
270 (*(current_routing->set_autonomous_system))(current_routing,A_surfxml_AS_id);
271 /* unload the prev parse rules */
272 (*(current_routing->routing->unload))();
275 THROW0(arg_error,0,"All defined components must be belong to a AS");
277 /* set the new parse rules */
278 (*(new_routing->routing->load))();
279 /* set the new current component of the tree */
280 current_routing = new_routing;
284 * \brief Finish the creation of a new routing component
286 * When you finish to read the routing component, other structures must be created.
287 * the "end" method allow to do that for any routing model type
289 static void parse_E_AS(void) {
291 if( current_routing == NULL ) {
292 THROW1(arg_error,0,"Close AS(%s), that never open",A_surfxml_AS_id);
294 (*(current_routing->routing->unload))();
295 (*(current_routing->routing->end))();
296 current_routing = current_routing->routing_father;
297 if( current_routing != NULL )
298 (*(current_routing->routing->load))();
303 * \brief Recursive function for add the differents AS to global dict of network element
305 * This fuction is call by "parse_E_platform_add_parse_AS". It allow to add the
306 * AS or routing components, to the where_network_elements dictionary. In the same
307 * way as "parse_S_host", "parse_S_router" and "parse_S_gateway" do.
309 static void _add_parse_AS(routing_component_t rc) {
310 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,rc->name),
311 "The AS \"%s\" already exist",rc->name);
312 xbt_dict_set(global_routing->where_network_elements,rc->name,rc->routing_father,NULL);
313 xbt_dict_cursor_t cursor = NULL;
315 routing_component_t elem;
316 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
322 * \brief Add all "AS" to the global dict of network element
324 * Allows find a "AS" in any routing component
326 static void parse_E_platform_add_parse_AS(void) {
327 _add_parse_AS(global_routing->root);
330 /* Aux Business methods */
333 * \brief Get the AS father and the first elements of the chain
335 * \param src the source host name
336 * \param dst the destination host name
338 * Get the common father of the to processing units, and the first different
339 * father in the chain
341 static xbt_dynar_t elements_father(const char* src,const char* dst) {
343 xbt_assert0(src&&dst,"bad parameters for \"elements_father\" method");
345 xbt_dynar_t result = xbt_dynar_new(sizeof(char*), NULL); // &xbt_free_ref
347 routing_component_t src_as, dst_as;
348 int index_src, index_dst, index_father_src, index_father_dst;
349 xbt_dynar_t path_src = NULL;
350 xbt_dynar_t path_dst = NULL;
351 routing_component_t current = NULL;
352 routing_component_t* current_src = NULL;
353 routing_component_t* current_dst = NULL;
354 routing_component_t* father = NULL;
356 /* (1) find the as where the src and dst are located */
357 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
358 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
359 xbt_assert2(src_as&&dst_as, "Ask for route \"from\"(%s) or \"to\"(%s) no found",src,dst);
361 /* (2) find the path to the root routing component */
362 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
364 while( current != NULL ) {
365 xbt_dynar_push(path_src,¤t);
366 current = current->routing_father;
368 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
370 while( current != NULL ) {
371 xbt_dynar_push(path_dst,¤t);
372 current = current->routing_father;
375 /* (3) find the common father */
376 index_src = (path_src->used)-1;
377 index_dst = (path_dst->used)-1;
378 current_src = xbt_dynar_get_ptr(path_src,index_src);
379 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
380 while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
381 current_src = xbt_dynar_get_ptr(path_src,index_src);
382 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
388 current_src = xbt_dynar_get_ptr(path_src,index_src);
389 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
391 /* (4) they are not in the same routing component, make the path */
392 index_father_src = index_src+1;
393 index_father_dst = index_dst+1;
395 if(*current_src==*current_dst)
396 father = current_src;
398 father = xbt_dynar_get_ptr(path_src,index_father_src);
400 /* (5) result generation */
401 xbt_dynar_push(result,father); // first same the father of src and dst
402 xbt_dynar_push(result,current_src); // second the first different father of src
403 xbt_dynar_push(result,current_dst); // three the first different father of dst
405 xbt_dynar_free(&path_src);
406 xbt_dynar_free(&path_dst);
411 /* Global Business methods */
414 * \brief Recursive function for get_route
416 * \param src the source host name
417 * \param dst the destination host name
419 * This fuction is call by "get_route". It allow to walk through the
420 * routing components tree.
422 static route_extended_t _get_route(const char* src,const char* dst) {
427 DEBUG2("Solve route \"%s\" to \"%s\"",src,dst);
429 xbt_assert0(src&&dst,"bad parameters for \"_get_route\" method");
431 route_extended_t e_route, e_route_cnt, e_route_src, e_route_dst;
433 xbt_dynar_t elem_father_list = elements_father(src,dst);
435 routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
436 routing_component_t src_father = xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
437 routing_component_t dst_father = xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
439 e_route = xbt_new0(s_route_extended_t,1);
440 e_route->src_gateway = NULL;
441 e_route->dst_gateway = NULL;
442 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
444 if(src_father==dst_father) { /* SURF_ROUTING_BASE */
446 if( strcmp(src,dst) ){
447 e_route_cnt = (*(common_father->get_route))(common_father,src,dst);
448 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
449 xbt_dynar_push(e_route->generic_route.link_list,&link);
451 xbt_dynar_free( &(e_route_cnt->generic_route.link_list) );
452 xbt_free(e_route_cnt);
455 } else { /* SURF_ROUTING_RECURSIVE */
457 e_route_cnt = (*(common_father->get_route))(common_father,src_father->name,dst_father->name);
459 xbt_assert2( (e_route_cnt->src_gateway==NULL) == (e_route_cnt->dst_gateway==NULL) ,
460 "bad gateway for route between \"%s\" and \"%s\"",src,dst);
462 if( src != e_route_cnt->src_gateway ) {
463 e_route_src = _get_route(src,e_route_cnt->src_gateway);
464 xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
465 xbt_dynar_push(e_route->generic_route.link_list,&link);
469 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
470 xbt_dynar_push(e_route->generic_route.link_list,&link);
473 if( e_route_cnt->dst_gateway != dst ) {
474 e_route_dst = _get_route(e_route_cnt->dst_gateway,dst);
475 xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
476 xbt_dynar_push(e_route->generic_route.link_list,&link);
480 e_route->src_gateway = e_route_cnt->src_gateway;
481 e_route->dst_gateway = e_route_cnt->dst_gateway;
483 xbt_dynar_free( &(e_route_src->generic_route.link_list) );
484 xbt_free(e_route_src);
485 xbt_dynar_free( &(e_route_cnt->generic_route.link_list) );
486 xbt_free(e_route_cnt);
487 xbt_dynar_free( &(e_route_dst->generic_route.link_list) );
488 xbt_free(e_route_dst);
491 xbt_dynar_free(&elem_father_list);
497 * \brief Generic method: find a route between hosts
499 * \param src the source host name
500 * \param dst the destination host name
502 * walk through the routing components tree and find a route between hosts
503 * by calling the differents "get_route" functions in each routing component
505 static xbt_dynar_t get_route(const char* src,const char* dst) {
507 if(global_routing->last_route) xbt_dynar_free( &(global_routing->last_route) );
509 route_extended_t e_route;
510 xbt_dynar_t elem_father_list = elements_father(src,dst);
511 routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
514 e_route = _get_route(src,dst);
516 e_route = (*(common_father->get_route))(common_father,src,dst);
518 global_routing->last_route = e_route->generic_route.link_list;
521 xbt_dynar_free(&elem_father_list);
523 if( xbt_dynar_length(global_routing->last_route)==0 )
526 return global_routing->last_route;
530 * \brief Recursive function for finalize
532 * \param rc the source host name
534 * This fuction is call by "finalize". It allow to finalize the
535 * AS or routing components. It delete all the structures.
537 static void _finalize(routing_component_t rc) {
539 xbt_dict_cursor_t cursor = NULL;
541 routing_component_t elem;
542 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
545 xbt_dict_t tmp_sons = rc->routing_sons;
546 char* tmp_name = rc->name;
547 xbt_dict_free(&tmp_sons);
549 (*(rc->finalize))(rc);
554 * \brief Generic method: delete all the routing structures
556 * walk through the routing components tree and delete the structures
557 * by calling the differents "finalize" functions in each routing component
559 static void finalize(void) {
560 /* delete recursibly all the tree */
561 _finalize(global_routing->root);
562 /* delete "where" dict */
563 xbt_dict_free(&(global_routing->where_network_elements));
564 /* delete last_route */
565 xbt_dynar_free(&(global_routing->last_route));
566 /* delete global routing structure */
567 xbt_free(global_routing);
571 * \brief Generic method: create the global routing schema
573 * Make a global routing structure and set all the parsing functions.
575 void routing_model_create(size_t size_of_links, void* loopback) {
577 /* config the uniq global routing */
578 global_routing = xbt_new0(s_routing_global_t,1);
579 global_routing->where_network_elements = xbt_dict_new();
580 global_routing->root = NULL;
581 global_routing->get_route = get_route;
582 global_routing->finalize = finalize;
583 global_routing->loopback = loopback;
584 global_routing->size_of_link = size_of_links;
585 global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
587 /* no current routing at moment */
588 current_routing = NULL;
590 /* parse generic elements */
591 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host);
592 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
594 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_S_route_new_and_endpoints);
595 surfxml_add_callback(STag_surfxml_ASroute_cb_list, &parse_S_ASroute_new_and_endpoints);
597 surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_E_link_c_ctn_new_elem);
599 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_E_route_store_route);
600 surfxml_add_callback(ETag_surfxml_ASroute_cb_list, &parse_E_ASroute_store_route);
602 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS);
603 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS);
605 /* set all the as in the global where table (recursive fuction) */
606 surfxml_add_callback(ETag_surfxml_platform_cb_list, &parse_E_platform_add_parse_AS);
609 //surfxml_add_callback(ETag_surfxml_platform_cb_list, &DEBUG_exit);
612 /* ************************************************************************** */
613 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
615 static void generic_set_processing_units(routing_component_t rc, const char* name);
616 static void generic_set_autonomous_system(routing_component_t rc, const char* name);
617 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route);
618 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route);
620 /* ************************************************************************** */
621 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
623 static route_extended_t generic_new_extended_route(routing_component_t rc, void* data);
625 /* ************************************************************************** */
626 /* *************************** FULL ROUTING ********************************* */
628 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
630 /* Routing model structure */
633 s_routing_component_t generic_routing;
634 xbt_dict_t parse_routes; /* store data during the parse process */
635 xbt_dict_t to_index; /* char* -> network_element_t */
636 route_extended_t *routing_table;
637 } s_routing_component_full_t,*routing_component_full_t;
639 /* Business methods */
641 static route_extended_t full_get_route(routing_component_t rc, const char* src,const char* dst) {
643 routing_component_full_t routing = (routing_component_full_t)rc;
644 int table_size = xbt_dict_length(routing->to_index);
646 xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
648 routing_component_t src_as, dst_as;
651 // TODO: MAKE A FUNCTION FOR GENERIC CHECK
652 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
653 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
655 xbt_assert3(src_as != NULL && dst_as != NULL, "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",src,dst,rc->name);
656 xbt_assert4(src_as == dst_as, "The src(%s in %s) and dst(%s in %s) are in differents AS",src,src_as->name,dst,dst_as->name);
657 xbt_assert2(rc == dst_as, "The routing component of src and dst is not the same as the network elements belong (%s==%s)",rc->name,dst_as->name);
659 src_id = xbt_dict_get(routing->to_index,src);
660 dst_id = xbt_dict_get(routing->to_index,dst);
661 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
665 route_extended_t new_e_route;
666 route_extended_t e_route = TO_ROUTE_FULL(*src_id,*dst_id);
668 new_e_route = xbt_new0(s_route_extended_t,1);
669 new_e_route->src_gateway = e_route->src_gateway;
670 new_e_route->dst_gateway = e_route->dst_gateway;
671 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
672 xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
673 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
681 static void full_finalize(routing_component_t rc) {
682 routing_component_full_t routing = (routing_component_full_t)rc;
683 int table_size = xbt_dict_length(routing->to_index);
686 /* Delete routing table */
687 for (i=0;i<table_size;i++) {
688 for (j=0;j<table_size;j++) {
689 route_extended_t e_route = TO_ROUTE_FULL(i,j);
691 xbt_dynar_free(&(e_route->generic_route.link_list));
692 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
693 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
698 xbt_free(routing->routing_table);
699 /* Delete index dict */
700 xbt_dict_free(&(routing->to_index));
701 /* Delete structure */
706 /* Creation routing model functions */
708 static void* model_full_create(void) {
709 routing_component_full_t new_component = xbt_new0(s_routing_component_full_t,1);
710 new_component->generic_routing.set_processing_units = generic_set_processing_units;
711 new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
712 new_component->generic_routing.set_route = generic_set_route;
713 new_component->generic_routing.set_ASroute = generic_set_ASroute;
714 new_component->generic_routing.get_route = full_get_route;
715 new_component->generic_routing.finalize = full_finalize;
716 new_component->to_index = xbt_dict_new();
717 new_component->parse_routes = xbt_dict_new();
718 return new_component;
721 static void model_full_load(void) {
722 /* use "surfxml_add_callback" to add a parse function call */
725 static void model_full_unload(void) {
726 /* use "surfxml_del_callback" to remove a parse function call */
729 static void model_full_end(void) {
731 char *key, *src_name, *dst_name;
732 const char* sep = "#";
733 int *src_id, *dst_id;
736 route_extended_t e_route;
739 xbt_dict_cursor_t cursor = NULL;
740 xbt_dynar_t keys = NULL;
743 routing_component_full_t routing = ((routing_component_full_t)current_routing);
744 int table_size = xbt_dict_length(routing->to_index);
746 /* Create the routing table */
747 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
749 /* Put the routes in position */
750 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
752 // FIXME: there are a misstake, is nesesary to use a different way to save the src_name and dst_name.
753 // or use a restricted char for separator (maybe space)
754 keys = xbt_str_split_str(key, sep);
756 src_name = xbt_dynar_get_as(keys, 0, char*);
757 dst_name = xbt_dynar_get_as(keys, 1, char*);
759 src_id = xbt_dict_get_or_null(routing->to_index, src_name);
760 dst_id = xbt_dict_get_or_null(routing->to_index, dst_name);
762 if (src_id == NULL || dst_id == NULL )
763 THROW2(mismatch_error,0,"Network elements %s or %s not found", src_name, dst_name);
765 TO_ROUTE_FULL(*src_id,*dst_id) = generic_new_extended_route(current_routing,data);
767 xbt_dynar_free(&keys);
770 /* delete the parse table */
771 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
772 route = (route_t)data;
773 xbt_dynar_free(&(route->link_list));
777 /* delete parse dict */
778 xbt_dict_free(&(routing->parse_routes));
780 /* Add the loopback if needed */
781 if(current_routing->hierarchy == SURF_ROUTING_BASE) {
782 for (i = 0; i < table_size; i++) {
783 e_route = TO_ROUTE_FULL(i, i);
784 if(!e_route) { // && !xbt_dynar_length(e_route->generic_route.link_list)
785 e_route = xbt_new0(s_route_extended_t,1);
786 e_route->src_gateway = NULL;
787 e_route->dst_gateway = NULL;
788 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
789 xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
790 TO_ROUTE_FULL(i, i) = e_route;
795 /* Shrink the dynar routes (save unused slots) */
796 for (i=0;i<table_size;i++)
797 for (j=0;j<table_size;j++)
798 if(TO_ROUTE_FULL(i,j))
799 xbt_dynar_shrink(TO_ROUTE_FULL(i,j)->generic_route.link_list,0);
802 /* ************************************************************************** */
803 /* *************************** FLOYD ROUTING ******************************** */
805 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
806 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
807 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
809 /* Routing model structure */
812 s_routing_component_t generic_routing;
813 /* vars for calculate the floyd algorith. */
814 int *predecessor_table;
815 route_extended_t *link_table; /* char* -> int* */
817 /* store data during the parse process */
818 xbt_dict_t parse_routes;
819 } s_routing_component_floyd_t,*routing_component_floyd_t;
821 /* Business methods */
823 static route_extended_t floyd_get_route(routing_component_t rc, const char* src,const char* dst) {
825 xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
828 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
829 int table_size = xbt_dict_length(routing->to_index);
831 route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
832 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
833 new_e_route->src_gateway = NULL;
834 new_e_route->dst_gateway = NULL;
836 routing_component_t src_as, dst_as;
838 // TODO: MAKE A FUNCTION FOR GENERIC CHECK
839 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
840 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
842 xbt_assert3(src_as != NULL && dst_as != NULL, "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",src,dst,rc->name);
843 xbt_assert4(src_as == dst_as, "The src(%s in %s) and dst(%s in %s) are in differents AS",src,src_as->name,dst,dst_as->name);
844 xbt_assert2(rc == dst_as, "The routing component of src and dst is not the same as the network elements belong (%s==%s)",rc->name,dst_as->name);
846 int *src_id = xbt_dict_get(routing->to_index,src);
847 int *dst_id = xbt_dict_get(routing->to_index,dst);
848 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
853 char *gw_src,*gw_dst, *prev_gw_src,*prev_gw_dst, *first_gw;
857 route_extended_t e_route_as_to_as;
860 pred = TO_FLOYD_PRED(*src_id, pred);
861 if(pred == -1) // if no pred in route -> no route to host
863 xbt_assert2(TO_FLOYD_LINK(pred,prev_pred),"Invalid link for the route between \"%s\" or \"%s\"", src, dst);
865 prev_gw_src = gw_src;
866 prev_gw_dst = gw_dst;
868 route_extended_t e_route = TO_FLOYD_LINK(pred,prev_pred);
869 gw_src = e_route->src_gateway;
870 gw_dst = e_route->dst_gateway;
872 if(first) first_gw = gw_src;
874 if(rc->hierarchy == SURF_ROUTING_RECURSIVE && !first && strcmp(prev_gw_dst,gw_src)) {
876 routing_component_t src_as = xbt_dict_get_or_null(global_routing->where_network_elements,prev_gw_dst);
877 routing_component_t dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,gw_src);
878 xbt_assert2(src_as==dst_as,"bad routing, differents AS gateways in route \"%s\" to \"%s\"",src,dst);
879 e_route_as_to_as = (*(src_as->get_route))(src_as,prev_gw_dst,gw_src);
880 links = e_route_as_to_as->generic_route.link_list;
881 xbt_dynar_foreach(links, cpt, link) {
882 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
886 links = e_route->generic_route.link_list;
887 xbt_dynar_foreach(links, cpt, link) {
888 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
892 } while(pred != *src_id);
893 xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")", *src_id, *dst_id,src,dst);
895 if(rc->hierarchy == SURF_ROUTING_RECURSIVE) {
896 new_e_route->src_gateway = first_gw;
897 new_e_route->dst_gateway = gw_dst;
903 static void floyd_finalize(routing_component_t rc) {
904 routing_component_floyd_t routing = (routing_component_floyd_t)rc;
907 table_size = xbt_dict_length(routing->to_index);
908 /* Delete link_table */
909 for (i=0;i<table_size;i++) {
910 for (j=0;j<table_size;j++) {
911 route_extended_t e_route = TO_FLOYD_LINK(i,j);
913 xbt_dynar_free(&(e_route->generic_route.link_list));
914 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
915 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
920 xbt_free(routing->link_table);
921 /* Delete index dict */
922 xbt_dict_free(&(routing->to_index));
923 /* Delete dictionary index dict, predecessor and links table */
924 xbt_free(routing->predecessor_table);
925 /* Delete structure */
930 static void* model_floyd_create(void) {
931 routing_component_floyd_t new_component = xbt_new0(s_routing_component_floyd_t,1);
932 new_component->generic_routing.set_processing_units = generic_set_processing_units;
933 new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
934 new_component->generic_routing.set_route = generic_set_route;
935 new_component->generic_routing.set_ASroute = generic_set_ASroute;
936 new_component->generic_routing.get_route = floyd_get_route;
937 new_component->generic_routing.finalize = floyd_finalize;
938 new_component->to_index = xbt_dict_new();
939 new_component->parse_routes = xbt_dict_new();
940 return new_component;
943 static void model_floyd_load(void) {
944 /* use "surfxml_add_callback" to add a parse function call */
947 static void model_floyd_unload(void) {
948 /* use "surfxml_del_callback" to remove a parse function call */
951 static void model_floyd_end(void) {
953 routing_component_floyd_t routing = ((routing_component_floyd_t)current_routing);
954 xbt_dict_cursor_t cursor = NULL;
956 char *key,*data, *src_name, *dst_name;
957 const char *sep = "#";
959 int *src_id, *dst_id;
960 unsigned int i,j,a,b,c;
962 /* set the size of inicial table */
963 int table_size = xbt_dict_length(routing->to_index);
965 /* Create Cost, Predecessor and Link tables */
966 cost_table = xbt_new0(double, table_size*table_size); //link cost from host to host
967 routing->predecessor_table = xbt_new0(int, table_size*table_size); //predecessor host numbers
968 routing->link_table = xbt_new0(route_extended_t, table_size*table_size); //actual link between src and dst
970 /* Initialize costs and predecessors*/
971 for(i = 0; i<table_size;i++)
972 for(j = 0; j<table_size;j++) {
973 TO_FLOYD_COST(i,j) = DBL_MAX;
974 TO_FLOYD_PRED(i,j) = -1;
975 TO_FLOYD_LINK(i,j) = NULL; // FIXED DAVID
978 /* Put the routes in position */
979 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
981 // FIXME: there are a misstake, is nesesary to use a different way to save the src_name and dst_name.
982 // or use a restricted char for separator (maybe space)
983 keys = xbt_str_split_str(key, sep);
985 src_name = xbt_dynar_get_as(keys, 0, char*);
986 dst_name = xbt_dynar_get_as(keys, 1, char*);
988 src_id = xbt_dict_get_or_null(routing->to_index, src_name);
989 dst_id = xbt_dict_get_or_null(routing->to_index, dst_name);
991 if (src_id == NULL || dst_id == NULL )
992 THROW2(mismatch_error,0,"Network elements %s or %s not found", src_name, dst_name);
994 TO_FLOYD_LINK(*src_id,*dst_id) = generic_new_extended_route(current_routing,data);
995 TO_FLOYD_PRED(*src_id,*dst_id) = *src_id;
998 TO_FLOYD_COST(*src_id,*dst_id) = 1; // assume 1 for now // TODO DAVID REDO
1000 xbt_dynar_free(&keys);
1003 /* Add the loopback if needed */
1004 if(current_routing->hierarchy == SURF_ROUTING_BASE) {
1005 for (i = 0; i < table_size; i++) {
1006 route_extended_t e_route = TO_FLOYD_LINK(i, i);
1007 if(!e_route) { // && !xbt_dynar_length(e_route->generic_route.link_list)
1008 e_route = xbt_new0(s_route_extended_t,1);
1009 e_route->src_gateway = NULL;
1010 e_route->dst_gateway = NULL;
1011 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1012 xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
1013 TO_FLOYD_LINK(i,i) = e_route;
1014 TO_FLOYD_PRED(i,i) = i;
1015 TO_FLOYD_COST(i,i) = 1;
1019 //Calculate path costs
1020 for(c=0;c<table_size;c++) {
1021 for(a=0;a<table_size;a++) {
1022 for(b=0;b<table_size;b++) {
1023 if(TO_FLOYD_COST(a,c) < DBL_MAX && TO_FLOYD_COST(c,b) < DBL_MAX) {
1024 if(TO_FLOYD_COST(a,b) == DBL_MAX ||
1025 (TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b) < TO_FLOYD_COST(a,b))) {
1026 TO_FLOYD_COST(a,b) = TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b);
1027 TO_FLOYD_PRED(a,b) = TO_FLOYD_PRED(c,b);
1034 /* delete the parse table */
1035 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1036 route_t route = (route_t)data;
1037 xbt_dynar_free(&(route->link_list));
1041 /* delete parse dict */
1042 xbt_dict_free(&(routing->parse_routes));
1045 xbt_free(cost_table);
1048 /* ************************************************************************** */
1049 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1052 s_routing_component_t generic_routing;
1053 xbt_dict_t parse_routes;
1054 xbt_dict_t to_index;
1055 xbt_graph_t route_graph; /* xbt_graph */
1056 xbt_dict_t graph_node_map; /* map */
1057 xbt_dict_t route_cache; /* use in cache mode */
1059 } s_routing_component_dijkstra_t,*routing_component_dijkstra_t;
1062 typedef struct graph_node_data {
1064 int graph_id; //used for caching internal graph id's
1065 } s_graph_node_data_t, * graph_node_data_t;
1067 typedef struct graph_node_map_element {
1069 } s_graph_node_map_element_t, * graph_node_map_element_t;
1071 typedef struct route_cache_element {
1074 } s_route_cache_element_t, * route_cache_element_t;
1076 /* Free functions */
1078 static void route_cache_elem_free(void *e) {
1079 route_cache_element_t elm=(route_cache_element_t)e;
1081 xbt_free(elm->pred_arr);
1086 static void graph_node_map_elem_free(void *e) {
1087 graph_node_map_element_t elm = (graph_node_map_element_t)e;
1093 static void graph_edge_data_free(void *e) {
1094 route_extended_t e_route = (route_extended_t)e;
1096 xbt_dynar_free(&(e_route->generic_route.link_list));
1097 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
1098 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
1103 /* Utility functions */
1105 static xbt_node_t route_graph_new_node(routing_component_dijkstra_t rc, int id, int graph_id) {
1106 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1107 xbt_node_t node = NULL;
1108 graph_node_data_t data = NULL;
1109 graph_node_map_element_t elm = NULL;
1111 data = xbt_new0(struct graph_node_data, 1); // FIXED by david
1113 data->graph_id = graph_id;
1114 node = xbt_graph_new_node(routing->route_graph, data);
1116 elm = xbt_new0(struct graph_node_map_element, 1); // FIXED by david
1118 xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
1123 static graph_node_map_element_t graph_node_map_search(routing_component_dijkstra_t rc, int id) {
1124 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1125 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));
1131 static void route_new_dijkstra(routing_component_dijkstra_t rc, int src_id, int dst_id, route_extended_t e_route ) {
1132 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1134 xbt_node_t src = NULL;
1135 xbt_node_t dst = NULL;
1136 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));
1137 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));
1140 src = src_elm->node;
1143 dst = dst_elm->node;
1145 //add nodes if they don't exist in the graph
1146 if(src_id == dst_id && src == NULL && dst == NULL) {
1147 src = route_graph_new_node(rc,src_id, -1);
1151 src = route_graph_new_node(rc,src_id, -1);
1154 dst = route_graph_new_node(rc,dst_id, -1);
1158 //add link as edge to graph
1159 xbt_graph_new_edge(routing->route_graph, src, dst, e_route);
1162 static void add_loopback_dijkstra(routing_component_dijkstra_t rc) {
1163 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1165 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1167 xbt_node_t node = NULL;
1168 unsigned int cursor2;
1169 xbt_dynar_foreach(nodes, cursor2, node) {
1170 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1171 xbt_edge_t edge = NULL;
1172 unsigned int cursor;
1175 xbt_dynar_foreach(out_edges, cursor, edge) {
1176 xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1177 if(other_node == node) {
1184 route_extended_t e_route = xbt_new0(s_route_extended_t,1);
1185 e_route->src_gateway = NULL;
1186 e_route->dst_gateway = NULL;
1187 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1188 xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
1189 xbt_graph_new_edge(routing->route_graph, node, node, e_route);
1194 /* Business methods */
1196 static route_extended_t dijkstra_get_route(routing_component_t rc, const char* src,const char* dst) {
1198 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1200 xbt_assert1(rc&&src&&dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
1202 int * pred_arr = NULL;
1203 int src_node_id = 0;
1204 int dst_node_id = 0;
1205 int * nodeid = NULL;
1207 routing_component_t src_as, dst_as;
1208 int *src_id,*dst_id;
1209 route_extended_t e_route;
1213 xbt_dynar_t links = NULL;
1214 route_cache_element_t elm = NULL;
1215 xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1217 route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
1218 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1219 new_e_route->src_gateway = NULL;
1220 new_e_route->dst_gateway = NULL;
1222 // TODO: MAKE A FUNCTION FOR GENERIC CHECK
1223 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
1224 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
1226 xbt_assert3(src_as != NULL && dst_as != NULL, "Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",src,dst,rc->name);
1227 xbt_assert4(src_as == dst_as, "The src(%s in %s) and dst(%s in %s) are in differents AS",src,src_as->name,dst,dst_as->name);
1228 xbt_assert2(rc == dst_as, "The routing component of src and dst is not the same as the network elements belong (%s==%s)",rc->name,dst_as->name);
1230 src_id = xbt_dict_get_or_null(routing->to_index,src);
1231 dst_id = xbt_dict_get_or_null(routing->to_index,dst);
1232 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
1234 /*Use the graph_node id mapping set to quickly find the nodes */
1235 graph_node_map_element_t src_elm = graph_node_map_search(routing,*src_id);
1236 graph_node_map_element_t dst_elm = graph_node_map_search(routing,*dst_id);
1237 xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", *src_id, *dst_id);
1238 src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
1239 dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
1241 /* if the src and dst are the same */ // FIXED by david
1242 if( src_node_id == dst_node_id ) {
1244 xbt_node_t node_s_v = xbt_dynar_get_as(nodes, src_node_id, xbt_node_t);
1245 xbt_node_t node_e_v = xbt_dynar_get_as(nodes, dst_node_id, xbt_node_t);
1246 xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_s_v, node_e_v);
1248 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id, *dst_id);
1250 e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
1252 links = e_route->generic_route.link_list;
1253 xbt_dynar_foreach(links, cpt, link) {
1254 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
1260 if(routing->cached) {
1261 /*check if there is a cached predecessor list avail */
1262 elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
1265 if(elm) { //cached mode and cache hit
1266 pred_arr = elm->pred_arr;
1267 } else { //not cached mode or cache miss
1268 double * cost_arr = NULL;
1269 xbt_heap_t pqueue = NULL;
1272 int nr_nodes = xbt_dynar_length(nodes);
1273 cost_arr = xbt_new0(double, nr_nodes); // link cost from src to other hosts
1274 pred_arr = xbt_new0(int, nr_nodes); // predecessors in path from src
1275 pqueue = xbt_heap_new(nr_nodes, xbt_free);
1278 cost_arr[src_node_id] = 0.0;
1280 for(i = 0; i < nr_nodes; i++) {
1281 if(i != src_node_id) {
1282 cost_arr[i] = DBL_MAX;
1287 //initialize priority queue
1288 nodeid = xbt_new0(int, 1);
1290 xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1294 // apply dijkstra using the indexes from the graph's node array
1295 while(xbt_heap_size(pqueue) > 0) {
1296 int * v_id = xbt_heap_pop(pqueue);
1297 xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1298 xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1299 xbt_edge_t edge = NULL;
1300 unsigned int cursor;
1302 xbt_dynar_foreach(out_edges, cursor, edge) {
1303 xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1304 graph_node_data_t data = xbt_graph_node_get_data(u_node);
1305 int u_id = data->graph_id;
1306 int cost_v_u = 1; //fixed link cost for now // TODO DAVID REDO
1308 if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1309 pred_arr[u_id] = *v_id;
1310 cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1311 nodeid = xbt_new0(int, 1);
1313 xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1317 //free item popped from pqueue
1322 xbt_heap_free(pqueue);
1326 //compose route path with links
1327 for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1328 xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1329 xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1330 xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1332 xbt_assert2(edge != NULL, "no route between host %d and %d", *src_id, *dst_id);
1334 e_route = (route_extended_t)xbt_graph_edge_get_data(edge);
1337 new_e_route->src_gateway=e_route->src_gateway;
1338 new_e_route->dst_gateway=e_route->dst_gateway;
1340 links = e_route->generic_route.link_list;
1341 xbt_dynar_foreach(links, cpt, link) {
1342 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
1347 if(routing->cached && elm == NULL) {
1348 //add to predecessor list of the current src-host to cache
1349 elm = xbt_new0(struct route_cache_element, 1); // FIXED by david
1350 elm->pred_arr = pred_arr;
1352 xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
1355 if(!routing->cached)
1361 static void dijkstra_finalize(routing_component_t rc) {
1362 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) rc;
1365 xbt_graph_free_graph(routing->route_graph, &xbt_free, &graph_edge_data_free, &xbt_free);
1366 xbt_dict_free(&routing->graph_node_map);
1368 xbt_dict_free(&routing->route_cache);
1369 xbt_dict_free(&routing->to_index);
1374 /* Creation routing model functions */
1376 static void* model_dijkstra_both_create(int cached) {
1377 routing_component_dijkstra_t new_component = xbt_new0(s_routing_component_dijkstra_t,1);
1378 new_component->generic_routing.set_processing_units = generic_set_processing_units;
1379 new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
1380 new_component->generic_routing.set_route = generic_set_route;
1381 new_component->generic_routing.set_ASroute = generic_set_ASroute;
1382 new_component->generic_routing.get_route = dijkstra_get_route;
1383 new_component->generic_routing.finalize = dijkstra_finalize;
1384 new_component->cached = cached;
1385 new_component->to_index = xbt_dict_new();
1386 new_component->parse_routes = xbt_dict_new();
1387 return new_component;
1390 static void* model_dijkstra_create(void) {
1391 return model_dijkstra_both_create(0);
1394 static void* model_dijkstracache_create(void) {
1395 return model_dijkstra_both_create(1);
1398 static void model_dijkstra_both_load(void) {
1399 /* use "surfxml_add_callback" to add a parse function call */
1402 static void model_dijkstra_both_unload(void) {
1403 /* use "surfxml_del_callback" to remove a parse function call */
1406 static void model_dijkstra_both_end(void) {
1407 routing_component_dijkstra_t routing = (routing_component_dijkstra_t) current_routing;
1408 xbt_dict_cursor_t cursor = NULL;
1410 const char *sep = "#";
1412 xbt_node_t node = NULL;
1413 unsigned int cursor2;
1414 xbt_dynar_t nodes = NULL;
1415 char *src_name, *dst_name;
1416 int *src_id, *dst_id;
1419 /* Create the topology graph */
1420 routing->route_graph = xbt_graph_new_graph(1, NULL);
1421 routing->graph_node_map = xbt_dict_new();
1424 routing->route_cache = xbt_dict_new();
1426 /* Put the routes in position */
1427 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1429 // FIXME: there are a misstake, is nesesary to use a different way to save the src_name and dst_name.
1430 // or use a restricted char for separator (maybe space)
1431 keys = xbt_str_split_str(key, sep);
1433 src_name = xbt_dynar_get_as(keys, 0, char*);
1434 dst_name = xbt_dynar_get_as(keys, 1, char*);
1436 src_id = xbt_dict_get_or_null(routing->to_index, src_name);
1437 dst_id = xbt_dict_get_or_null(routing->to_index, dst_name);
1439 if (src_id == NULL || dst_id == NULL )
1440 THROW2(mismatch_error,0,"Network elements %s or %s not found", src_name, dst_name);
1442 route_extended_t e_route = generic_new_extended_route(current_routing,data);
1444 route_new_dijkstra(routing,*src_id,*dst_id,e_route);
1446 xbt_dynar_free(&keys);
1449 /* delete the parse table */
1450 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1451 route = (route_t)data;
1452 xbt_dynar_free(&(route->link_list));
1456 /* delete parse dict */
1457 xbt_dict_free(&(routing->parse_routes));
1459 /* Add the loopback if needed */
1460 if(current_routing->hierarchy == SURF_ROUTING_BASE)
1461 add_loopback_dijkstra(routing);
1463 /* initialize graph indexes in nodes after graph has been built */
1464 nodes = xbt_graph_get_nodes(routing->route_graph);
1466 xbt_dynar_foreach(nodes, cursor2, node) {
1467 graph_node_data_t data = xbt_graph_node_get_data(node);
1468 data->graph_id = cursor2;
1474 /* ************************************************************************** */
1475 /* ******************************* NO ROUTING ******************************* */
1477 /* Routing model structure */
1479 s_routing_component_t generic_routing;
1480 } s_routing_component_none_t,*routing_component_none_t;
1482 /* Business methods */
1483 static route_extended_t none_get_route(routing_component_t rc, const char* src,const char* dst){
1486 static void none_finalize(routing_component_t rc) {
1490 /* Creation routing model functions */
1491 static void* model_none_create(void) {
1492 routing_component_none_t new_component = xbt_new0(s_routing_component_none_t,1);
1493 new_component->generic_routing.set_processing_units = NULL;
1494 new_component->generic_routing.set_autonomous_system = NULL;
1495 new_component->generic_routing.set_route = NULL;
1496 new_component->generic_routing.set_ASroute = NULL;
1497 new_component->generic_routing.get_route = none_get_route;
1498 new_component->generic_routing.finalize = none_finalize;
1499 return new_component;
1502 static void model_none_load(void) {}
1503 static void model_none_unload(void) {}
1504 static void model_none_end(void) {}
1506 // /* ************************************************** */
1507 // /* ********** PATERN FOR NEW ROUTING **************** */
1509 // /* The minimal configuration of a new routing model need the next functions,
1510 // * also you need to set at the start of the file, the new model in the model
1511 // * list. Remember keep the null ending of the list.
1513 // /* Routing model structure */
1515 // s_routing_component_t generic_routing;
1516 // /* things that your routing model need */
1517 // } s_routing_component_NEW_t,*routing_component_NEW_t;
1519 // /* Parse routing model functions */
1520 // static void NEW_parse_S_host(void) {} /* example*/
1522 // /* Business methods */
1523 // static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;} /* mandatory */
1524 // static void NEW_finalize(routing_component_t rc) {} /* mandatory */
1526 // /* Creation routing model functions */
1527 // static void* model_NEW_create(void) {return NULL;} /* mandatory */
1528 // static void model_NEW_load(void) {} /* mandatory */
1529 // static void model_NEW_unload(void) {} /* mandatory */
1530 // static void model_NEW_end(void) {} /* mandatory */
1532 /* ************************************************************************** */
1533 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
1535 static void generic_set_processing_units(routing_component_t rc, const char* name) {
1536 DEBUG1("Full - Load process unit \"%s\"",name);
1537 model_type_t modeltype = rc->routing;
1538 int *id = xbt_new0(int,1); // xbt_malloc(sizeof(int)); ?
1540 if(modeltype==&routing_models[SURF_MODEL_FULL])
1541 index = ((routing_component_full_t)current_routing)->to_index;
1542 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1543 index = ((routing_component_floyd_t)current_routing)->to_index;
1544 else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1545 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE])
1546 index = ((routing_component_dijkstra_t)current_routing)->to_index;
1547 else xbt_die("\"generic_set_processing_units\" not support");
1548 *id = xbt_dict_length(index);
1549 xbt_dict_set(index,name,id,xbt_free);
1552 static void generic_set_autonomous_system(routing_component_t rc, const char* name) {
1553 DEBUG1("Full - Load Autonomous system \"%s\"",name);
1554 model_type_t modeltype = rc->routing;
1555 int *id = xbt_new0(int,1); // xbt_malloc(sizeof(int)); ?
1557 if(modeltype==&routing_models[SURF_MODEL_FULL])
1558 index = ((routing_component_full_t)current_routing)->to_index;
1559 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1560 index = ((routing_component_floyd_t)current_routing)->to_index;
1561 else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1562 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE])
1563 index = ((routing_component_dijkstra_t)current_routing)->to_index;
1564 else xbt_die("\"generic_set_autonomous_system\" not support");
1565 *id = xbt_dict_length(index);
1566 xbt_dict_set(index,name,id,xbt_free);
1569 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {
1570 DEBUG2("Full - Load Route from \"%s\" to \"%s\"",src,dst);
1571 model_type_t modeltype = rc->routing;
1572 xbt_dict_t parseroutes;
1573 if(modeltype==&routing_models[SURF_MODEL_FULL])
1574 parseroutes = ((routing_component_full_t)current_routing)->parse_routes;
1575 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1576 parseroutes = ((routing_component_floyd_t)current_routing)->parse_routes;
1577 else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1578 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE])
1579 parseroutes = ((routing_component_dijkstra_t)current_routing)->parse_routes;
1580 else xbt_die("\"generic_set_autonomous_system\" not support");
1582 route_name = bprintf("%s#%s",src,dst);
1583 xbt_assert2(xbt_dynar_length(link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
1584 xbt_assert2(!xbt_dict_get_or_null(parseroutes,route_name),
1585 "The route between \"%s\" and \"%s\" already exist",src,dst);
1586 xbt_dict_set(parseroutes, route_name, route, NULL);
1587 xbt_free(route_name);
1590 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
1591 DEBUG4("Full - Load ASroute from \"%s(%s)\" to \"%s(%s)\"",src,e_route->src_gateway,dst,e_route->dst_gateway);
1592 model_type_t modeltype = rc->routing;
1593 xbt_dict_t parseroutes;
1594 if(modeltype==&routing_models[SURF_MODEL_FULL])
1595 parseroutes = ((routing_component_full_t)current_routing)->parse_routes;
1596 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1597 parseroutes = ((routing_component_floyd_t)current_routing)->parse_routes;
1598 else if(modeltype==&routing_models[SURF_MODEL_DIJKSTRA]||
1599 modeltype==&routing_models[SURF_MODEL_DIJKSTRACACHE])
1600 parseroutes = ((routing_component_dijkstra_t)current_routing)->parse_routes;
1601 else xbt_die("\"generic_set_autonomous_system\" not support");
1603 route_name = bprintf("%s#%s",src,dst);
1604 xbt_assert2(xbt_dynar_length(link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
1605 xbt_assert4(!xbt_dict_get_or_null(parseroutes,route_name),
1606 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
1607 xbt_dict_set(parseroutes, route_name, e_route, NULL);
1608 xbt_free(route_name);
1611 /* ************************************************************************** */
1612 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1614 static route_extended_t generic_new_extended_route(routing_component_t rc, void* data) {
1617 route_extended_t e_route, new_e_route;
1620 xbt_dynar_t links, links_id;
1622 new_e_route = xbt_new0(s_route_extended_t,1);
1623 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1624 new_e_route->src_gateway = NULL;
1625 new_e_route->dst_gateway = NULL;
1627 xbt_assert0(rc->hierarchy == SURF_ROUTING_BASE || rc->hierarchy == SURF_ROUTING_RECURSIVE,
1628 "the hierarchy type is not defined");
1630 if(rc->hierarchy == SURF_ROUTING_BASE ) {
1632 route = (route_t)data;
1633 links = route->link_list;
1635 } else if(rc->hierarchy == SURF_ROUTING_RECURSIVE ) {
1637 e_route = (route_extended_t)data;
1639 // FIXME: HERE CHECK THE GATEWAY!!!!!!!!!
1640 xbt_assert0(e_route->src_gateway&&e_route->dst_gateway,"bad gateway, is null");
1642 links = e_route->generic_route.link_list;
1644 /* remeber not erase the gateway names */
1645 new_e_route->src_gateway = e_route->src_gateway;
1646 new_e_route->dst_gateway = e_route->dst_gateway;
1649 links_id = new_e_route->generic_route.link_list;
1651 xbt_dynar_foreach(links, cpt, link_name) {
1653 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1655 xbt_dynar_push(links_id,&link);
1657 THROW1(mismatch_error,0,"Link %s not found", link_name);
1663 ////////////////////////////////////////////////////////////////////////////////
1664 // HERE FINISH THE NEW CODE
1665 ////////////////////////////////////////////////////////////////////////////////
1667 //...... DEBUG ONLY .... //
1668 static void print_tree(routing_component_t rc) {
1669 printf("(%s %s)\n",rc->name,rc->routing->name);
1671 xbt_dict_cursor_t cursor = NULL;
1674 if( rc->routing == &(routing_models[SURF_MODEL_FULL]) )
1675 { xbt_dict_foreach(((routing_component_full_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } }
1676 else if( rc->routing == &(routing_models[SURF_MODEL_FLOYD]) )
1677 { xbt_dict_foreach(((routing_component_floyd_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); }
1680 xbt_assert0(0, "Invalid model for call \"print_tree\"");
1682 routing_component_t elem;
1683 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
1690 //...... DEBUG ONLY .... //
1691 static void print_global() {
1692 xbt_dict_cursor_t cursor = NULL;
1694 routing_component_t elem;
1695 xbt_dict_foreach(global_routing->where_network_elements, cursor, key, elem) { printf("<%s>\n",key); }
1698 //...... DEBUG ONLY .... //
1699 static void print_AS_start(void) { printf("AS!!! %s y se rutea \"%s\"\n",A_surfxml_AS_id,A_surfxml_AS_routing); }
1700 static void print_AS_end(void) { printf("AS!!! %s\n",A_surfxml_AS_id); }
1701 static void print_host(void) { printf("host!!! %s\n",A_surfxml_host_id); }
1702 static void print_link(void) { printf("link!!! %s\n",A_surfxml_link_id); }
1703 static void print_route(void) { printf("route!!! %s a %s\n",A_surfxml_route_src,A_surfxml_route_dst); }
1704 static void print_ctn(void) { printf("ctn!!! %s\n",A_surfxml_link_c_ctn_id); }
1706 //...... DEBUG ONLY .... //
1707 static void DEBUG_exit(void) {
1709 // printf("-------- print tree elements -----\n");
1710 // print_tree(global_routing->root);
1711 // printf("----------------------------------\n\n");
1713 // printf("-------- network_elements --------\n");
1715 // printf("----------------------------------\n\n");
1717 // const char* names[] = { "11.A","11.B","11.C","121.A","121.B","122.A","13.A","13.B"};
1718 // int i,j,total = 8;
1720 // printf("-- print all the example routes --\n");
1721 // const char* names[] = { "11.A","11.B","11.C"};
1722 // int i,j,total = 3;
1724 // const char* names[] = { "141.A","141.B","143.A","143.B"};
1725 // int i,j,total = 4;
1727 // const char* names[] = { "15.A","15.B","15.C" };
1728 // int i,j,total = 3;
1730 // const char* names[] = { "142.A","142.B","142.C", "11.A","11.B","11.C","141.A","141.B","143.A","143.B","11.A","11.B","11.C","121.A","121.B","122.A","13.A","13.B"};
1731 // int i,j,total = 3+4+8+3;
1733 // const char* names[] = { "142.A","142.B","142.C" };
1734 // int i,j,total = 3;
1736 const char* names[] = { "11.A","11.B","11.C",
1741 "142.A","142.B","142.C",
1743 "15.A","15.B","15.C"};
1744 unsigned int i,j,total = 3+2+1+2+2+3+2+3;
1746 printf("-- print all the example routes --\n");
1749 for(i=0;i<total;i++) {
1750 for(j=0;j<total;j++) {
1751 printf("route from %s to %s >>> ",names[i],names[j]);
1752 links = (*(global_routing->get_route))(names[i],names[j]);
1754 xbt_dynar_foreach(links, cpt, link) {
1755 s_surf_resource_t* generic_resource = link;
1756 printf(" %s",generic_resource->name);
1762 printf("----------------------------------\n\n");
1764 printf("---------- call finalize ---------\n");
1765 (*(global_routing->finalize))();
1766 printf("----------------------------------\n");
1771 ////////////////////////////////////////////////////////////////////////////////
1772 // HERE END THE NEW CODE
1773 ////////////////////////////////////////////////////////////////////////////////
1775 // /* ************************************************************************** */
1776 // /* *************************** FULL ROUTING ********************************* */
1778 // s_routing_t generic_routing;
1779 // xbt_dynar_t *routing_table;
1781 // size_t size_of_link;
1782 // } s_routing_full_t,*routing_full_t;
1784 // #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
1785 // #define HOST2ROUTER(id) ((id)+(2<<29))
1786 // #define ROUTER2HOST(id) ((id)-(2>>29))
1787 // #define ISROUTER(id) ((id)>=(2<<29))
1790 // * Free the onelink routes
1792 // static void onelink_route_elem_free(void *e) {
1793 // s_onelink_t tmp = (s_onelink_t)e;
1802 // static void routing_full_parse_Shost(void) {
1803 // int *val = xbt_malloc(sizeof(int));
1804 // DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
1805 // *val = used_routing->host_count++;
1806 // xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
1807 // #ifdef HAVE_TRACING
1808 // TRACE_surf_host_define_id (A_surfxml_host_id, *val);
1812 // static void routing_full_parse_Srouter(void) {
1813 // int *val = xbt_malloc(sizeof(int));
1814 // DEBUG3("Seen router %s (%d -> #%d)",A_surfxml_router_id,used_routing->router_count,
1815 // HOST2ROUTER(used_routing->router_count));
1816 // *val = HOST2ROUTER(used_routing->router_count++);
1817 // xbt_dict_set(used_routing->host_id,A_surfxml_router_id,val,xbt_free);
1818 // #ifdef HAVE_TRACING
1819 // TRACE_surf_host_define_id (A_surfxml_host_id, *val);
1820 // TRACE_surf_host_declaration (A_surfxml_host_id, 0);
1824 // static int src_id = -1;
1825 // static int dst_id = -1;
1826 // static void routing_full_parse_Sroute_set_endpoints(void)
1828 // src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
1829 // dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
1830 // DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
1831 // route_action = A_surfxml_route_action;
1834 // static void routing_full_parse_Eroute(void)
1837 // if (src_id != -1 && dst_id != -1) {
1838 // name = bprintf("%x#%x", src_id, dst_id);
1839 // manage_route(route_table, name, route_action, 0);
1844 // /* Cluster tag functions */
1846 // static void routing_full_parse_change_cpu_data(const char *hostName,
1847 // const char *surfxml_host_power,
1848 // const char *surfxml_host_availability,
1849 // const char *surfxml_host_availability_file,
1850 // const char *surfxml_host_state_file)
1854 // SURFXML_BUFFER_SET(host_id, hostName);
1855 // SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
1856 // SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
1857 // SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
1858 // SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
1861 // static void routing_full_parse_change_link_data(const char *linkName,
1862 // const char *surfxml_link_bandwidth,
1863 // const char *surfxml_link_bandwidth_file,
1864 // const char *surfxml_link_latency,
1865 // const char *surfxml_link_latency_file,
1866 // const char *surfxml_link_state_file)
1870 // SURFXML_BUFFER_SET(link_id, linkName);
1871 // SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
1872 // SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
1873 // SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
1874 // SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
1875 // SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
1878 // static void routing_full_parse_Scluster(void)
1880 // static int AX_ptr = 0;
1882 // char *cluster_id = A_surfxml_cluster_id;
1883 // char *cluster_prefix = A_surfxml_cluster_prefix;
1884 // char *cluster_suffix = A_surfxml_cluster_suffix;
1885 // char *cluster_radical = A_surfxml_cluster_radical;
1886 // char *cluster_power = A_surfxml_cluster_power;
1887 // char *cluster_bw = A_surfxml_cluster_bw;
1888 // char *cluster_lat = A_surfxml_cluster_lat;
1889 // char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1890 // char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1891 // char *backbone_name;
1892 // unsigned int it1,it2;
1893 // char *name1,*name2;
1894 // xbt_dynar_t names = NULL;
1895 // surfxml_bufferstack_push(1);
1897 // /* Make set a set to parse the prefix/suffix/radical into a neat list of names */
1898 // DEBUG4("Make <set id='%s' prefix='%s' suffix='%s' radical='%s'>",
1899 // cluster_id,cluster_prefix,cluster_suffix,cluster_radical);
1900 // SURFXML_BUFFER_SET(set_id, cluster_id);
1901 // SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1902 // SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1903 // SURFXML_BUFFER_SET(set_radical, cluster_radical);
1905 // SURFXML_START_TAG(set);
1906 // SURFXML_END_TAG(set);
1908 // names = xbt_dict_get(set_list,cluster_id);
1910 // xbt_dynar_foreach(names,it1,name1) {
1911 // /* create the host */
1912 // routing_full_parse_change_cpu_data(name1, cluster_power, "1.0", "", "");
1913 // A_surfxml_host_state = A_surfxml_host_state_ON;
1915 // SURFXML_START_TAG(host);
1916 // SURFXML_END_TAG(host);
1918 // /* Here comes the link */
1919 // routing_full_parse_change_link_data(name1, cluster_bw, "", cluster_lat, "", "");
1920 // A_surfxml_link_state = A_surfxml_link_state_ON;
1921 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1923 // SURFXML_START_TAG(link);
1924 // SURFXML_END_TAG(link);
1927 // /* Make backbone link */
1928 // backbone_name = bprintf("%s_bb", cluster_id);
1929 // routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
1931 // A_surfxml_link_state = A_surfxml_link_state_ON;
1932 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1934 // SURFXML_START_TAG(link);
1935 // SURFXML_END_TAG(link);
1937 // /* And now the internal routes */
1938 // xbt_dynar_foreach(names,it1,name1) {
1939 // xbt_dynar_foreach(names,it2,name2) {
1940 // if (strcmp(name1,name2)) {
1941 // A_surfxml_route_action = A_surfxml_route_action_POSTPEND;
1942 // SURFXML_BUFFER_SET(route_src,name1);
1943 // SURFXML_BUFFER_SET(route_dst,name2);
1944 // SURFXML_START_TAG(route); {
1945 // /* FIXME: name1 link is added by error about 20 lines below, so don't add it here
1946 // SURFXML_BUFFER_SET(link_c_ctn_id, name1);
1947 // SURFXML_START_TAG(link_c_ctn);
1948 // SURFXML_END_TAG(link_c_ctn);
1950 // SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1951 // SURFXML_START_TAG(link_c_ctn);
1952 // SURFXML_END_TAG(link_c_ctn);
1954 // SURFXML_BUFFER_SET(link_c_ctn_id, name2);
1955 // SURFXML_START_TAG(link_c_ctn);
1956 // SURFXML_END_TAG(link_c_ctn);
1958 // } SURFXML_END_TAG(route);
1963 // /* Make route multi with the outside world, i.e. cluster->$* */
1966 // * This also adds an elements to the routes within the cluster,
1967 // * and I guess it's wrong, but since this element is commented out in the above
1968 // * code creating the internal routes, we're good.
1969 // * To fix it, I'd say that we need a way to understand "$*-${cluster_id}" as "whole world, but the guys in that cluster"
1970 // * But for that, we need to install a real expression parser for src/dst attributes
1973 // * This also adds a dumb element (the private link) in place of the loopback. Then, since
1974 // * the loopback is added only if no link to self already exist, this fails.
1975 // * That's really dumb.
1978 // * It seems to me that it does not add the backbone to the path to outside world...
1980 // SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1981 // SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1982 // A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1983 // A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_PREPEND;
1985 // SURFXML_START_TAG(route_c_multi);
1987 // SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1989 // SURFXML_START_TAG(link_c_ctn);
1990 // SURFXML_END_TAG(link_c_ctn);
1992 // SURFXML_END_TAG(route_c_multi);
1994 // free(backbone_name);
1996 // /* Restore buff */
1997 // surfxml_bufferstack_pop(1);
2001 // static void routing_full_parse_end(void) {
2002 // routing_full_t routing = (routing_full_t) used_routing;
2004 // unsigned int cpt = 0;
2005 // xbt_dict_cursor_t cursor = NULL;
2006 // char *key, *data, *end;
2007 // const char *sep = "#";
2008 // xbt_dynar_t links, keys;
2009 // char *link_name = NULL;
2012 // int host_count = routing->generic_routing.host_count;
2014 // /* Create the routing table */
2015 // routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
2016 // for (i=0;i<host_count;i++)
2017 // for (j=0;j<host_count;j++)
2018 // ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
2020 // /* Put the routes in position */
2021 // xbt_dict_foreach(route_table, cursor, key, data) {
2023 // links = (xbt_dynar_t) data;
2024 // keys = xbt_str_split_str(key, sep);
2026 // src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
2027 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
2028 // xbt_dynar_free(&keys);
2030 // if(xbt_dynar_length(links) == 1){
2031 // s_onelink_t new_link = (s_onelink_t) xbt_malloc0(sizeof(s_onelink));
2032 // new_link->src_id = src_id;
2033 // new_link->dst_id = dst_id;
2034 // link_name = xbt_dynar_getfirst_as(links, char*);
2035 // new_link->link_ptr = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2036 // DEBUG3("Adding onelink route from (#%d) to (#%d), link_name %s",src_id, dst_id, link_name);
2037 // xbt_dict_set(onelink_routes, link_name, (void *)new_link, onelink_route_elem_free);
2038 // #ifdef HAVE_TRACING
2039 // TRACE_surf_link_save_endpoints (link_name, src_id, dst_id);
2043 // if(ISROUTER(src_id) || ISROUTER(dst_id)) {
2044 // DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
2045 // /* Check there is only one link in the route and store the information */
2049 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
2050 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2051 // xbt_dynar_foreach(links, cpt, link_name) {
2052 // void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2054 // xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
2056 // THROW1(mismatch_error,0,"Link %s not found", link_name);
2060 // /* Add the loopback if needed */
2061 // for (i = 0; i < host_count; i++)
2062 // if (!xbt_dynar_length(ROUTE_FULL(i, i)))
2063 // xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
2065 // /* Shrink the dynar routes (save unused slots) */
2066 // for (i=0;i<host_count;i++)
2067 // for (j=0;j<host_count;j++)
2068 // xbt_dynar_shrink(ROUTE_FULL(i,j),0);
2072 // * Business methods
2074 // static xbt_dynar_t routing_full_get_route(int src,int dst) {
2075 // xbt_assert0(!(ISROUTER(src) || ISROUTER(dst)), "Ask for route \"from\" or \"to\" a router node");
2076 // return ROUTE_FULL(src,dst);
2079 // static xbt_dict_t routing_full_get_onelink_routes(void){
2080 // return onelink_routes;
2083 // static int routing_full_is_router(int id){
2084 // return ISROUTER(id);
2087 // static void routing_full_finalize(void) {
2088 // routing_full_t routing = (routing_full_t)used_routing;
2092 // for (i = 0; i < used_routing->host_count; i++)
2093 // for (j = 0; j < used_routing->host_count; j++)
2094 // xbt_dynar_free(&ROUTE_FULL(i, j));
2095 // free(routing->routing_table);
2096 // xbt_dict_free(&used_routing->host_id);
2097 // xbt_dict_free(&onelink_routes);
2103 // static void routing_model_full_create(size_t size_of_link,void *loopback) {
2104 // /* initialize our structure */
2105 // routing_full_t routing = xbt_new0(s_routing_full_t,1);
2106 // routing->generic_routing.name = "Full";
2107 // routing->generic_routing.host_count = 0;
2108 // routing->generic_routing.get_route = routing_full_get_route;
2109 // routing->generic_routing.get_onelink_routes = routing_full_get_onelink_routes;
2110 // routing->generic_routing.is_router = routing_full_is_router;
2111 // routing->generic_routing.finalize = routing_full_finalize;
2113 // routing->size_of_link = size_of_link;
2114 // routing->loopback = loopback;
2116 // /* Set it in position */
2117 // used_routing = (routing_t) routing;
2119 // /* Set the dict for onehop routes */
2120 // onelink_routes = xbt_dict_new();
2122 // routing->generic_routing.host_id = xbt_dict_new();
2124 // /* Setup the parsing callbacks we need */
2125 // // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2126 // // surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
2127 // // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
2128 // // surfxml_add_callback(STag_surfxml_route_cb_list, &routing_full_parse_Sroute_set_endpoints);
2129 // // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2130 // // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
2132 // // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2133 // // surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
2137 /* ************************************************************************** */
2139 // static void routing_shortest_path_parse_Scluster(void)
2141 // static int AX_ptr = 0;
2143 // char *cluster_id = A_surfxml_cluster_id;
2144 // char *cluster_prefix = A_surfxml_cluster_prefix;
2145 // char *cluster_suffix = A_surfxml_cluster_suffix;
2146 // char *cluster_radical = A_surfxml_cluster_radical;
2147 // char *cluster_power = A_surfxml_cluster_power;
2148 // char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
2149 // char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
2150 // char *backbone_name;
2152 // surfxml_bufferstack_push(1);
2155 // SURFXML_BUFFER_SET(set_id, cluster_id);
2156 // SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
2157 // SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
2158 // SURFXML_BUFFER_SET(set_radical, cluster_radical);
2160 // SURFXML_START_TAG(set);
2161 // SURFXML_END_TAG(set);
2163 // /* Make foreach */
2164 // SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
2166 // SURFXML_START_TAG(foreach);
2168 // /* Make host for the foreach */
2169 // routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
2170 // A_surfxml_host_state = A_surfxml_host_state_ON;
2172 // SURFXML_START_TAG(host);
2173 // SURFXML_END_TAG(host);
2175 // SURFXML_END_TAG(foreach);
2177 // /* Make backbone link */
2178 // backbone_name = bprintf("%s_bb", cluster_id);
2179 // routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
2181 // A_surfxml_link_state = A_surfxml_link_state_ON;
2182 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
2184 // SURFXML_START_TAG(link);
2185 // SURFXML_END_TAG(link);
2187 // free(backbone_name);
2189 // /* Restore buff */
2190 // surfxml_bufferstack_pop(1);
2193 // /* ************************************************************************** */
2194 // /* *************************** FLOYD ROUTING ********************************* */
2196 // s_routing_t generic_routing;
2197 // int *predecessor_table;
2198 // void** link_table;
2199 // xbt_dynar_t last_route;
2201 // size_t size_of_link;
2202 // } s_routing_floyd_t,*routing_floyd_t;
2204 // #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
2205 // #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
2206 // #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
2208 // static void routing_floyd_parse_end(void) {
2210 // routing_floyd_t routing = (routing_floyd_t) used_routing;
2212 // void* link_list = NULL;
2213 // double * cost_table;
2214 // xbt_dict_cursor_t cursor = NULL;
2215 // char *key,*data, *end;
2216 // const char *sep = "#";
2217 // xbt_dynar_t links, keys;
2219 // unsigned int i,j;
2220 // unsigned int a,b,c;
2221 // int host_count = routing->generic_routing.host_count;
2222 // char * link_name = NULL;
2223 // void * link = NULL;
2225 // /* Create Cost, Predecessor and Link tables */
2226 // cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
2227 // routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
2228 // routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
2229 // routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
2231 // /* Initialize costs and predecessors*/
2232 // for(i = 0; i<host_count;i++)
2233 // for(j = 0; j<host_count;j++) {
2234 // FLOYD_COST(i,j) = DBL_MAX;
2235 // FLOYD_PRED(i,j) = -1;
2238 // /* Put the routes in position */
2239 // xbt_dict_foreach(route_table, cursor, key, data) {
2241 // links = (xbt_dynar_t)data;
2242 // keys = xbt_str_split_str(key, sep);
2245 // src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
2246 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
2247 // xbt_dynar_free(&keys);
2249 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
2250 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2251 // xbt_assert3(xbt_dynar_length(links) == 1, "%ld links in route between host %d and %d, should be 1", xbt_dynar_length(links), src_id, dst_id);
2253 // link_name = xbt_dynar_getfirst_as(links, char*);
2254 // link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2257 // link_list = link;
2259 // THROW1(mismatch_error,0,"Link %s not found", link_name);
2262 // FLOYD_LINK(src_id,dst_id) = link_list;
2263 // FLOYD_PRED(src_id, dst_id) = src_id;
2266 // FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
2270 // /* Add the loopback if needed */
2271 // for (i = 0; i < host_count; i++)
2272 // if (!FLOYD_PRED(i, i)) {
2273 // FLOYD_PRED(i, i) = i;
2274 // FLOYD_COST(i, i) = 1;
2275 // FLOYD_LINK(i, i) = routing->loopback;
2279 // //Calculate path costs
2281 // for(c=0;c<host_count;c++) {
2282 // for(a=0;a<host_count;a++) {
2283 // for(b=0;b<host_count;b++) {
2284 // if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
2285 // if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
2286 // FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
2287 // FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
2295 // free(cost_table);
2299 // * Business methods
2301 // static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
2303 // routing_floyd_t routing = (routing_floyd_t) used_routing;
2305 // int pred = dst_id;
2306 // int prev_pred = 0;
2308 // xbt_dynar_reset(routing->last_route);
2311 // prev_pred = pred;
2312 // pred = FLOYD_PRED(src_id, pred);
2314 // if(pred == -1) // if no pred in route -> no route to host
2317 // xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
2319 // } while(pred != src_id);
2321 // xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
2323 // return routing->last_route;
2326 // static void routing_floyd_finalize(void) {
2327 // routing_floyd_t routing = (routing_floyd_t)used_routing;
2330 // free(routing->link_table);
2331 // free(routing->predecessor_table);
2332 // xbt_dynar_free(&routing->last_route);
2333 // xbt_dict_free(&used_routing->host_id);
2339 // static xbt_dict_t routing_floyd_get_onelink_routes(void){
2340 // xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model Floyd");
2343 // static int routing_floyd_is_router(int id){
2344 // xbt_assert0(0,"The get_is_router feature is not supported in routing model Floyd");
2347 // static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
2348 // /* initialize our structure */
2349 // routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
2350 // routing->generic_routing.name = "Floyd";
2351 // routing->generic_routing.host_count = 0;
2352 // routing->generic_routing.host_id = xbt_dict_new();
2353 // routing->generic_routing.get_route = routing_floyd_get_route;
2354 // routing->generic_routing.get_onelink_routes = routing_floyd_get_onelink_routes;
2355 // routing->generic_routing.is_router = routing_floyd_is_router;
2356 // routing->generic_routing.finalize = routing_floyd_finalize;
2357 // routing->size_of_link = size_of_link;
2358 // routing->loopback = loopback;
2360 // /* Set it in position */
2361 // used_routing = (routing_t) routing;
2363 // /* Setup the parsing callbacks we need */
2364 // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2365 // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
2366 // surfxml_add_callback(STag_surfxml_route_cb_list,
2367 // &routing_full_parse_Sroute_set_endpoints);
2368 // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2369 // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
2373 // /* ************************************************************************** */
2374 // /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
2376 // s_routing_t generic_routing;
2377 // xbt_graph_t route_graph;
2378 // xbt_dict_t graph_node_map;
2379 // xbt_dict_t route_cache;
2380 // xbt_dynar_t last_route;
2383 // size_t size_of_link;
2384 // } s_routing_dijkstra_t,*routing_dijkstra_t;
2387 // typedef struct graph_node_data {
2389 // int graph_id; //used for caching internal graph id's
2390 // } s_graph_node_data_t, * graph_node_data_t;
2392 // typedef struct graph_node_map_element {
2394 // } s_graph_node_map_element_t, * graph_node_map_element_t;
2396 // typedef struct route_cache_element {
2399 // } s_route_cache_element_t, * route_cache_element_t;
2404 // static void route_cache_elem_free(void *e) {
2405 // route_cache_element_t elm=(route_cache_element_t)e;
2408 // free(elm->pred_arr);
2413 // static void graph_node_map_elem_free(void *e) {
2414 // graph_node_map_element_t elm = (graph_node_map_element_t)e;
2422 // * Utility functions
2424 // static xbt_node_t route_graph_new_node(int id, int graph_id) {
2425 // xbt_node_t node = NULL;
2426 // graph_node_data_t data = NULL;
2427 // graph_node_map_element_t elm = NULL;
2428 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2430 // data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
2432 // data->graph_id = graph_id;
2433 // node = xbt_graph_new_node(routing->route_graph, data);
2435 // elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
2436 // elm->node = node;
2437 // xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
2442 // static graph_node_map_element_t graph_node_map_search(int id) {
2443 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2445 // 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));
2453 // static void route_new_dijkstra(int src_id, int dst_id, void* link) {
2454 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2456 // xbt_node_t src = NULL;
2457 // xbt_node_t dst = NULL;
2458 // 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));
2459 // 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));
2462 // src = src_elm->node;
2465 // dst = dst_elm->node;
2467 // //add nodes if they don't exist in the graph
2468 // if(src_id == dst_id && src == NULL && dst == NULL) {
2469 // src = route_graph_new_node(src_id, -1);
2472 // if(src == NULL) {
2473 // src = route_graph_new_node(src_id, -1);
2476 // if(dst == NULL) {
2477 // dst = route_graph_new_node(dst_id, -1);
2481 // //add link as edge to graph
2482 // xbt_graph_new_edge(routing->route_graph, src, dst, link);
2486 // static void add_loopback_dijkstra(void) {
2487 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2489 // xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2491 // xbt_node_t node = NULL;
2492 // unsigned int cursor2;
2493 // xbt_dynar_foreach(nodes, cursor2, node) {
2494 // xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
2495 // xbt_edge_t edge = NULL;
2496 // unsigned int cursor;
2499 // xbt_dynar_foreach(out_edges, cursor, edge) {
2500 // xbt_node_t other_node = xbt_graph_edge_get_target(edge);
2501 // if(other_node == node) {
2508 // xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
2512 // static void routing_dijkstra_parse_end(void) {
2513 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2515 // xbt_dict_cursor_t cursor = NULL;
2516 // char *key, *data, *end;
2517 // const char *sep = "#";
2518 // xbt_dynar_t links, keys;
2519 // char* link_name = NULL;
2520 // void* link = NULL;
2521 // xbt_node_t node = NULL;
2522 // unsigned int cursor2;
2523 // xbt_dynar_t nodes = NULL;
2524 // /* Create the topology graph */
2525 // routing->route_graph = xbt_graph_new_graph(1, NULL);
2526 // routing->graph_node_map = xbt_dict_new();
2527 // routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
2528 // if(routing->cached)
2529 // routing->route_cache = xbt_dict_new();
2532 // /* Put the routes in position */
2533 // xbt_dict_foreach(route_table, cursor, key, data) {
2535 // links = (xbt_dynar_t) data;
2536 // keys = xbt_str_split_str(key, sep);
2538 // src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
2539 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
2540 // xbt_dynar_free(&keys);
2542 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
2543 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2545 // xbt_assert3(xbt_dynar_length(links) == 1, "%ld links in route between host %d and %d, should be 1", xbt_dynar_length(links), src_id, dst_id);
2547 // link_name = xbt_dynar_getfirst_as(links, char*);
2548 // link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2550 // route_new_dijkstra(src_id,dst_id,link);
2552 // THROW1(mismatch_error,0,"Link %s not found", link_name);
2556 // /* Add the loopback if needed */
2557 // add_loopback_dijkstra();
2559 // /* initialize graph indexes in nodes after graph has been built */
2560 // nodes = xbt_graph_get_nodes(routing->route_graph);
2562 // xbt_dynar_foreach(nodes, cursor2, node) {
2563 // graph_node_data_t data = xbt_graph_node_get_data(node);
2564 // data->graph_id = cursor2;
2570 // * Business methods
2572 // static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
2574 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2575 // int * pred_arr = NULL;
2576 // int src_node_id = 0;
2577 // int dst_node_id = 0;
2578 // int * nodeid = NULL;
2581 // void * link = NULL;
2582 // route_cache_element_t elm = NULL;
2583 // xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2585 // /*Use the graph_node id mapping set to quickly find the nodes */
2586 // graph_node_map_element_t src_elm = graph_node_map_search(src_id);
2587 // graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
2588 // xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
2589 // src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
2590 // dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
2592 // if(routing->cached) {
2593 // /*check if there is a cached predecessor list avail */
2594 // elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
2597 // if(elm) { //cached mode and cache hit
2598 // pred_arr = elm->pred_arr;
2599 // } else { //not cached mode or cache miss
2600 // double * cost_arr = NULL;
2601 // xbt_heap_t pqueue = NULL;
2604 // int nr_nodes = xbt_dynar_length(nodes);
2605 // cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
2606 // pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
2607 // pqueue = xbt_heap_new(nr_nodes, free);
2610 // cost_arr[src_node_id] = 0.0;
2612 // for(i = 0; i < nr_nodes; i++) {
2613 // if(i != src_node_id) {
2614 // cost_arr[i] = DBL_MAX;
2619 // //initialize priority queue
2620 // nodeid = xbt_new0(int, 1);
2622 // xbt_heap_push(pqueue, nodeid, cost_arr[i]);
2626 // // apply dijkstra using the indexes from the graph's node array
2627 // while(xbt_heap_size(pqueue) > 0) {
2628 // int * v_id = xbt_heap_pop(pqueue);
2629 // xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
2630 // xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
2631 // xbt_edge_t edge = NULL;
2632 // unsigned int cursor;
2634 // xbt_dynar_foreach(out_edges, cursor, edge) {
2635 // xbt_node_t u_node = xbt_graph_edge_get_target(edge);
2636 // graph_node_data_t data = xbt_graph_node_get_data(u_node);
2637 // int u_id = data->graph_id;
2638 // int cost_v_u = 1; //fixed link cost for now
2640 // if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
2641 // pred_arr[u_id] = *v_id;
2642 // cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
2643 // nodeid = xbt_new0(int, 1);
2645 // xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
2649 // //free item popped from pqueue
2654 // xbt_heap_free(pqueue);
2658 // //compose route path with links
2659 // xbt_dynar_reset(routing->last_route);
2661 // for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
2662 // xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
2663 // xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
2664 // xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
2666 // xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
2668 // link = xbt_graph_edge_get_data(edge);
2669 // xbt_dynar_unshift(routing->last_route, &link);
2674 // if(routing->cached && elm == NULL) {
2675 // //add to predecessor list of the current src-host to cache
2676 // elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
2677 // elm->pred_arr = pred_arr;
2678 // elm->size = size;
2679 // xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
2682 // if(!routing->cached)
2685 // return routing->last_route;
2689 // static void routing_dijkstra_finalize(void) {
2690 // routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
2693 // xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
2694 // xbt_dict_free(&routing->graph_node_map);
2695 // if(routing->cached)
2696 // xbt_dict_free(&routing->route_cache);
2697 // xbt_dynar_free(&routing->last_route);
2698 // xbt_dict_free(&used_routing->host_id);
2704 // static xbt_dict_t routing_dijkstraboth_get_onelink_routes(void){
2705 // xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model dijkstraboth");
2708 // static int routing_dijkstraboth_is_router(int id){
2709 // xbt_assert0(0,"The get_is_router feature is not supported in routing model dijkstraboth");
2715 // static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
2716 // /* initialize our structure */
2717 // routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
2718 // routing->generic_routing.name = "Dijkstra";
2719 // routing->generic_routing.host_count = 0;
2720 // routing->generic_routing.get_route = routing_dijkstra_get_route;
2721 // routing->generic_routing.get_onelink_routes = routing_dijkstraboth_get_onelink_routes;
2722 // routing->generic_routing.is_router = routing_dijkstraboth_is_router;
2723 // routing->generic_routing.finalize = routing_dijkstra_finalize;
2724 // routing->size_of_link = size_of_link;
2725 // routing->loopback = loopback;
2726 // routing->cached = cached;
2728 // /* Set it in position */
2729 // used_routing = (routing_t) routing;
2731 // /* Setup the parsing callbacks we need */
2732 // routing->generic_routing.host_id = xbt_dict_new();
2733 // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2734 // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
2735 // surfxml_add_callback(STag_surfxml_route_cb_list,
2736 // &routing_full_parse_Sroute_set_endpoints);
2737 // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2738 // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
2741 // static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
2742 // routing_model_dijkstraboth_create(size_of_link, loopback, 0);
2745 // static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
2746 // routing_model_dijkstraboth_create(size_of_link, loopback, 1);
2749 /* ************************************************** */
2750 /* ********** NO ROUTING **************************** */
2753 // static void routing_none_finalize(void) {
2754 // if (used_routing) {
2755 // xbt_dict_free(&used_routing->host_id);
2756 // free(used_routing);
2757 // used_routing=NULL;
2761 // static void routing_model_none_create(size_t size_of_link,void *loopback) {
2762 // routing_t routing = xbt_new0(s_routing_t,1);
2763 // INFO0("Null routing");
2764 // routing->name = "none";
2765 // routing->host_count = 0;
2766 // routing->host_id = xbt_dict_new();
2767 // routing->get_onelink_routes = NULL;
2768 // routing->is_router = NULL;
2769 // routing->get_route = NULL;
2771 // routing->finalize = routing_none_finalize;
2773 // /* Set it in position */
2774 // used_routing = (routing_t) routing;