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");
18 ////////////////////////////////////////////////////////////////////////////////
19 // HERE START THE NEW CODE
20 ////////////////////////////////////////////////////////////////////////////////
22 //...... DEBUG ONLY .... //
23 static void print_tree(routing_component_t rc);
24 static void print_global();
25 static void print_AS_start(void);
26 static void print_AS_end(void);
27 static void print_host(void);
28 static void print_link(void);
29 static void print_route(void);
30 static void print_ctn(void);
31 static void DEBUG_exit(void);
33 ////////////////////////////////////////////////////////////////////////////////
34 // HERE START THE NEW CODE
35 ////////////////////////////////////////////////////////////////////////////////
38 routing_global_t global_routing = NULL;
39 routing_component_t current_routing = NULL;
40 model_type_t current_routing_model = NULL;
42 /* Prototypes of each model */
43 static void* model_full_create(void); /* create structures for full routing model */
44 static void model_full_load(void); /* load parse functions for full routing model */
45 static void model_full_unload(void); /* unload parse functions for full routing model */
46 static void model_full_end(void); /* finalize the creation of full routing model */
48 static void* model_floyd_create(void); /* create structures for floyd routing model */
49 static void model_floyd_load(void); /* load parse functions for floyd routing model */
50 static void model_floyd_unload(void); /* unload parse functions for floyd routing model */
51 static void model_floyd_end(void); /* finalize the creation of floyd routing model */
53 static void* model_dijkstra_create(void); /* create structures for dijkstra routing model */
54 static void model_dijkstra_load(void); /* load parse functions for dijkstra routing model */
55 static void model_dijkstra_unload(void); /* unload parse functions for dijkstra routing model */
56 static void model_dijkstra_end(void); /* finalize the creation of dijkstra routing model */
58 static void* model_dijkstracache_create(void); /* create structures for dijkstracache routing model */
59 static void model_dijkstracache_load(void); /* load parse functions for dijkstracache routing model */
60 static void model_dijkstracache_unload(void); /* unload parse functions for dijkstracache routing model */
61 static void model_dijkstracache_end(void); /* finalize the creation of dijkstracache routing model */
63 static void* model_none_create(void); /* none routing model */
64 static void model_none_load(void); /* none routing model */
65 static void model_none_unload(void); /* none routing model */
66 static void model_none_end(void); /* none routing model */
68 /* Table of routing models */
70 // SURF_MODEL_FULL = 0,
72 // SURF_MODEL_DIJKSTRA,
73 // SURF_MODEL_DIJKSTRACACHE,
75 // } e_surf_model_type_t;
77 #define SURF_MODEL_FULL 0
78 #define SURF_MODEL_FLOYD 1
79 #define SURF_MODEL_DIJKSTRA 2
80 #define SURF_MODEL_DIJKSTRACACHE 3
81 #define SURF_MODEL_NONE 4
83 /* must be finish with null and carefull if change de order */
84 struct s_model_type routing_models[] =
85 { {"Full", "Full routing data (fast, large memory requirements, fully expressive)",
86 model_full_create, model_full_load, model_full_unload, model_full_end },
87 {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)",
88 model_floyd_create, model_floyd_load, model_floyd_unload, model_floyd_end },
89 {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)",
90 model_dijkstra_create ,model_dijkstra_load, model_dijkstra_unload, model_dijkstra_end },
91 {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)",
92 model_dijkstracache_create, model_dijkstracache_load, model_dijkstracache_unload, model_dijkstracache_end },
93 {"none", "No routing (usable with Constant network only)",
94 model_none_create, model_none_load, model_none_unload, model_none_end },
95 {NULL,NULL,NULL,NULL,NULL,NULL}};
97 /* global parse functions */
99 static char* src = NULL; /* temporary store the source name of a route */
100 static char* dst = NULL; /* temporary store the destination name of a route */
101 static char* gw_src = NULL; /* temporary store the gateway source name of a route */
102 static char* gw_dst = NULL; /* temporary store the gateway destination name of a route */
103 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
106 * \brief Add a "host" to the network element list
108 static void parse_S_host(void) {
109 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
110 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_host_id),
111 "Reading a host, processing unit \"%s\" already exist",A_surfxml_host_id);
112 // FIXME: checked by parser
113 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
114 // "Bad declaration of processing unit \"%s\"",A_surfxml_host_id);
115 (*(current_routing->set_processing_units))(current_routing,A_surfxml_host_id);
116 xbt_dict_set(global_routing->where_network_elements,A_surfxml_host_id,(void*)current_routing,NULL);
120 * \brief Add a "router" to the network element list
122 static void parse_S_router(void) {
123 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_BASE;
124 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,A_surfxml_router_id),
125 "Reading a router, processing unit \"%s\" already exist",A_surfxml_router_id);
126 // FIXME: checked by parser
127 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
128 // "Bad declaration of processing unit \"%s\"",A_surfxml_host_id);
129 (*(current_routing->set_processing_units))(current_routing,A_surfxml_router_id);
130 xbt_dict_set(global_routing->where_network_elements,A_surfxml_router_id,(void*)current_routing,NULL);
134 * \brief Set the endponints for a route
136 static void parse_S_route_new_and_endpoints(void) {
137 // FIXME: checked by parser
138 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
139 // "Bad declaration of route in \"%s\"",current_routing->name);
140 if( src != NULL && dst != NULL && link_list != NULL )
141 THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_route_src,A_surfxml_route_dst);
142 src = A_surfxml_route_src;
143 dst = A_surfxml_route_dst;
144 link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
148 * \brief Set the endponints and gateways for a ASroute
150 static void parse_S_ASroute_new_and_endpoints(void) {
151 // FIXME: checked by parser
152 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
153 // "Bad declaration of ASroute in \"%s\"",current_routing->name);
154 if( src != NULL && dst != NULL && link_list != NULL )
155 THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_ASroute_src,A_surfxml_ASroute_dst);
156 src = A_surfxml_ASroute_src;
157 dst = A_surfxml_ASroute_dst;
158 gw_src = A_surfxml_ASroute_gw_src;
159 gw_dst = A_surfxml_ASroute_gw_dst;
160 link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
164 * \brief Set a new link on the actual list of link for a route or ASroute
166 static void parse_E_link_c_ctn_new_elem(void) {
168 val = xbt_strdup(A_surfxml_link_c_ctn_id);
169 xbt_dynar_push(link_list, &val);
173 * \brief Store de route by calling the set_route function of the current routing component
175 static void parse_E_route_store_route(void) {
176 // FIXME: checked by parser
177 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_BASE,
178 // "Bad declaration of route in \"%s\"",current_routing->name);
179 route_t route = xbt_new(s_route_t,1);
180 route->link_list = link_list;
181 // TODO check if are correct
182 (*(current_routing->set_route))(current_routing,src,dst,route);
189 * \brief Store de ASroute by calling the set_ASroute function of the current routing component
191 static void parse_E_ASroute_store_route(void) {
192 // FIXME: checked by parser
193 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
194 // "Bad declaration of ASroute in \"%s\"",current_routing->name);
195 route_extended_t e_route = xbt_new(s_route_extended_t,1);
196 e_route->generic_route.link_list = link_list;
197 e_route->src_gateway = xbt_strdup(gw_src);
198 e_route->dst_gateway = xbt_strdup(gw_dst);
199 // TODO check if are correct
200 (*(current_routing->set_ASroute))(current_routing,src,dst,e_route);
209 * \brief Make a new routing component
211 * Detect the routing model type of the routing component, make the new structure and
212 * set the parsing functions to allows parsing the part of the routing tree
214 static void parse_S_AS(void) {
215 routing_component_t new_routing;
216 model_type_t model = NULL;
217 char* wanted = A_surfxml_AS_routing;
219 /* seach the routing model */
220 for (cpt=0;routing_models[cpt].name;cpt++)
221 if (!strcmp(wanted,routing_models[cpt].name))
222 model = &routing_models[cpt];
223 /* if its not exist, error */
224 if( model == NULL ) {
225 fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
226 for (cpt=0;routing_models[cpt].name;cpt++)
227 if (!strcmp(wanted,routing_models[cpt].name))
228 fprintf(stderr," %s: %s\n",routing_models[cpt].name,routing_models[cpt].desc);
232 /* make a new routing component */
233 new_routing = (routing_component_t)(*(model->create))();
235 /* FIXME: for now, if I forget to declare */
236 xbt_assert1( new_routing->set_processing_units,
237 "Bad routing type, \"set_processing_units\" is not declared for \"%s\"",A_surfxml_AS_id);
238 xbt_assert1( new_routing->set_autonomous_system,
239 "Bad routing type, \"set_autonomous_system\" is not declared for \"%s\"",A_surfxml_AS_id);
240 xbt_assert1( new_routing->set_route,
241 "Bad routing type, \"set_route\" is not declared for \"%s\"",A_surfxml_AS_id);
242 xbt_assert1( new_routing->set_ASroute,
243 "Bad routing type, \"set_ASroute\" is not declared for \"%s\"",A_surfxml_AS_id);
244 xbt_assert1( new_routing->finalize,
245 "Bad routing type, \"finalize\" is not declared for \"%s\"",A_surfxml_AS_id);
247 new_routing->routing = model;
248 new_routing->hierarchy = SURF_ROUTING_NULL;
249 new_routing->name = xbt_strdup(A_surfxml_AS_id);
250 new_routing->routing_sons = xbt_dict_new();
251 INFO2("Routing %s for AS %s",A_surfxml_AS_routing,A_surfxml_AS_id);
253 if( current_routing == NULL && global_routing->root == NULL ){
255 /* it is the first one */
256 new_routing->routing_father = NULL;
257 global_routing->root = new_routing;
259 } else if( current_routing != NULL && global_routing->root != NULL ) {
261 xbt_assert1(!xbt_dict_get_or_null(current_routing->routing_sons,A_surfxml_AS_id),
262 "The AS \"%s\" already exist",A_surfxml_AS_id);
263 /* it is a part of the tree */
264 new_routing->routing_father = current_routing;
265 /* set the father behavior */
266 if( current_routing->hierarchy == SURF_ROUTING_NULL ) current_routing->hierarchy = SURF_ROUTING_RECURSIVE;
267 // FIXME: checked by parser
268 // xbt_assert1(current_routing->hierarchy==SURF_ROUTING_RECURSIVE,
269 // "Bad declaration of AS \"%s\"",A_surfxml_AS_id);
270 /* add to the sons dictionary */
271 xbt_dict_set(current_routing->routing_sons,A_surfxml_AS_id,(void*)new_routing,NULL);
272 /* add to the father element list */
273 (*(current_routing->set_autonomous_system))(current_routing,A_surfxml_AS_id);
274 /* unload the prev parse rules */
275 (*(current_routing->routing->unload))();
278 THROW0(arg_error,0,"All defined components must be belong to a AS");
280 /* set the new parse rules */
281 (*(new_routing->routing->load))();
282 /* set the new current component of the tree */
283 current_routing = new_routing;
287 * \brief Finish the creation of a new routing component
289 * When you finish to read the routing component, other structures must be created.
290 * the "end" method allow to do that for any routing model type
292 static void parse_E_AS(void) {
294 if( current_routing == NULL ) {
295 THROW1(arg_error,0,"Close AS(%s), that never open",A_surfxml_AS_id);
297 (*(current_routing->routing->unload))();
298 (*(current_routing->routing->end))();
299 current_routing = current_routing->routing_father;
300 if( current_routing != NULL )
301 (*(current_routing->routing->load))();
306 * \brief Recursive function for add the differents AS to global dict of network element
308 * This fuction is call by "parse_E_platform_add_parse_AS". It allow to add the
309 * AS or routing components, to the where_network_elements dictionary. In the same
310 * way as "parse_S_host", "parse_S_router" and "parse_S_gateway" do.
312 static void _add_parse_AS(routing_component_t rc) {
313 xbt_assert1(!xbt_dict_get_or_null(global_routing->where_network_elements,rc->name),
314 "The AS \"%s\" already exist",rc->name);
315 xbt_dict_set(global_routing->where_network_elements,rc->name,rc->routing_father,NULL);
316 xbt_dict_cursor_t cursor = NULL;
318 routing_component_t elem;
319 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
325 * \brief Add all "AS" to the global dict of network element
327 * Allows find a "AS" in any routing component
329 static void parse_E_platform_add_parse_AS(void) {
330 _add_parse_AS(global_routing->root);
333 /* Aux Business methods */
336 * \brief Get the AS father and the first elements of the chain
338 * \param src the source host name
339 * \param dst the destination host name
341 * Get the common father of the to processing units, and the first different
342 * father in the chain
344 static xbt_dynar_t elements_father(const char* src,const char* dst) {
346 xbt_assert0(src&&dst,"bad parameters for \"elements_father\" method");
348 xbt_dynar_t result = xbt_dynar_new(sizeof(char*), NULL); // &xbt_free_ref
350 routing_component_t src_as, dst_as;
351 int index_src, index_dst, index_father_src, index_father_dst;
352 xbt_dynar_t path_src = NULL;
353 xbt_dynar_t path_dst = NULL;
354 routing_component_t current = NULL;
355 routing_component_t* current_src = NULL;
356 routing_component_t* current_dst = NULL;
357 routing_component_t* father = NULL;
359 /* (1) find the as where the src and dst are located */
360 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
361 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
362 xbt_assert2(src_as&&dst_as, "Ask for route \"from\"(%s) or \"to\"(%s) no found",src,dst);
364 /* (2) find the path to the root routing component */
365 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
367 while( current != NULL ) {
368 xbt_dynar_push(path_src,¤t);
369 current = current->routing_father;
371 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
373 while( current != NULL ) {
374 xbt_dynar_push(path_dst,¤t);
375 current = current->routing_father;
378 /* (3) find the common father */
379 index_src = (path_src->used)-1;
380 index_dst = (path_dst->used)-1;
381 current_src = xbt_dynar_get_ptr(path_src,index_src);
382 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
383 while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
384 current_src = xbt_dynar_get_ptr(path_src,index_src);
385 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
391 current_src = xbt_dynar_get_ptr(path_src,index_src);
392 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
394 /* (4) they are not in the same routing component, make the path */
395 index_father_src = index_src+1;
396 index_father_dst = index_dst+1;
398 if(*current_src==*current_dst)
399 father = current_src;
401 father = xbt_dynar_get_ptr(path_src,index_father_src);
403 /* (5) result generation */
404 xbt_dynar_push(result,father); // first same the father of src and dst
405 xbt_dynar_push(result,current_src); // second the first different father of src
406 xbt_dynar_push(result,current_dst); // three the first different father of dst
408 xbt_dynar_free(&path_src);
409 xbt_dynar_free(&path_dst);
414 /* Global Business methods */
417 * \brief Recursive function for get_route
419 * \param src the source host name
420 * \param dst the destination host name
422 * This fuction is call by "get_route". It allow to walk through the
423 * routing components tree.
425 static route_extended_t _get_route(const char* src,const char* dst) {
430 DEBUG2("Solve route \"%s\" to \"%s\"",src,dst);
432 xbt_assert0(src&&dst,"bad parameters for \"_get_route\" method");
434 route_extended_t e_route, e_route_cnt, e_route_src, e_route_dst;
436 xbt_dynar_t elem_father_list = elements_father(src,dst);
438 routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
439 routing_component_t src_father = xbt_dynar_get_as(elem_father_list, 1, routing_component_t);
440 routing_component_t dst_father = xbt_dynar_get_as(elem_father_list, 2, routing_component_t);
442 e_route = xbt_new0(s_route_extended_t,1);
443 e_route->src_gateway = NULL;
444 e_route->dst_gateway = NULL;
445 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
447 if(src_father==dst_father) { /* SURF_ROUTING_BASE */
449 if( strcmp(src,dst) ){
450 e_route_cnt = (*(common_father->get_route))(common_father,src,dst);
451 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
452 xbt_dynar_push(e_route->generic_route.link_list,&link);
454 xbt_dynar_free( &(e_route_cnt->generic_route.link_list) );
455 xbt_free(e_route_cnt);
458 } else { /* SURF_ROUTING_RECURSIVE */
460 e_route_cnt = (*(common_father->get_route))(common_father,src_father->name,dst_father->name);
462 xbt_assert2( (e_route_cnt->src_gateway==NULL) == (e_route_cnt->dst_gateway==NULL) ,
463 "bad gateway for route between \"%s\" and \"%s\"",src,dst);
465 if( src != e_route_cnt->src_gateway ) {
466 e_route_src = _get_route(src,e_route_cnt->src_gateway);
467 xbt_dynar_foreach(e_route_src->generic_route.link_list, cpt, link) {
468 xbt_dynar_push(e_route->generic_route.link_list,&link);
472 xbt_dynar_foreach(e_route_cnt->generic_route.link_list, cpt, link) {
473 xbt_dynar_push(e_route->generic_route.link_list,&link);
476 if( e_route_cnt->dst_gateway != dst ) {
477 e_route_dst = _get_route(e_route_cnt->dst_gateway,dst);
478 xbt_dynar_foreach(e_route_dst->generic_route.link_list, cpt, link) {
479 xbt_dynar_push(e_route->generic_route.link_list,&link);
483 e_route->src_gateway = e_route_cnt->src_gateway;
484 e_route->dst_gateway = e_route_cnt->dst_gateway;
486 xbt_dynar_free( &(e_route_src->generic_route.link_list) );
487 xbt_free(e_route_src);
488 xbt_dynar_free( &(e_route_cnt->generic_route.link_list) );
489 xbt_free(e_route_cnt);
490 xbt_dynar_free( &(e_route_dst->generic_route.link_list) );
491 xbt_free(e_route_dst);
494 xbt_dynar_free(&elem_father_list);
500 * \brief Generic method: find a route between hosts
502 * \param src the source host name
503 * \param dst the destination host name
505 * walk through the routing components tree and find a route between hosts
506 * by calling the differents "get_route" functions in each routing component
508 static xbt_dynar_t get_route(const char* src,const char* dst) {
510 if(global_routing->last_route) xbt_dynar_free( &(global_routing->last_route) );
512 route_extended_t e_route;
513 xbt_dynar_t elem_father_list = elements_father(src,dst);
514 routing_component_t common_father = xbt_dynar_get_as(elem_father_list, 0, routing_component_t);
517 e_route = _get_route(src,dst);
519 e_route = (*(common_father->get_route))(common_father,src,dst);
521 global_routing->last_route = e_route->generic_route.link_list;
524 xbt_dynar_free(&elem_father_list);
526 if( xbt_dynar_length(global_routing->last_route)==0 )
529 return global_routing->last_route;
533 * \brief Recursive function for finalize
535 * \param rc the source host name
537 * This fuction is call by "finalize". It allow to finalize the
538 * AS or routing components. It delete all the structures.
540 static void _finalize(routing_component_t rc) {
542 xbt_dict_cursor_t cursor = NULL;
544 routing_component_t elem;
545 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
548 xbt_dict_t tmp_sons = rc->routing_sons;
549 char* tmp_name = rc->name;
550 xbt_dict_free(&tmp_sons);
552 (*(rc->finalize))(rc);
557 * \brief Generic method: delete all the routing structures
559 * walk through the routing components tree and delete the structures
560 * by calling the differents "finalize" functions in each routing component
562 static void finalize(void) {
563 /* delete recursibly all the tree */
564 _finalize(global_routing->root);
565 /* delete "where" dict */
566 xbt_dict_free(&(global_routing->where_network_elements));
567 /* delete last_route */
568 xbt_dynar_free(&(global_routing->last_route));
569 /* delete global routing structure */
570 xbt_free(global_routing);
574 * \brief Generic method: create the global routing schema
576 * Make a global routing structure and set all the parsing functions.
578 void routing_model_create(size_t size_of_links, void* loopback) {
580 /* config the uniq global routing */
581 global_routing = xbt_new0(s_routing_global_t,1);
582 global_routing->where_network_elements = xbt_dict_new();
583 global_routing->root = NULL;
584 global_routing->get_route = get_route;
585 global_routing->finalize = finalize;
586 global_routing->loopback = loopback;
587 global_routing->size_of_link = size_of_links;
588 global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
590 /* no current routing at moment */
591 current_routing = NULL;
593 /* parse generic elements */
594 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host);
595 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
597 surfxml_add_callback(STag_surfxml_route_cb_list, &parse_S_route_new_and_endpoints);
598 surfxml_add_callback(STag_surfxml_ASroute_cb_list, &parse_S_ASroute_new_and_endpoints);
600 surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_E_link_c_ctn_new_elem);
602 surfxml_add_callback(ETag_surfxml_route_cb_list, &parse_E_route_store_route);
603 surfxml_add_callback(ETag_surfxml_ASroute_cb_list, &parse_E_ASroute_store_route);
605 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS);
606 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS);
608 /* set all the as in the global where table (recursive fuction) */
609 surfxml_add_callback(ETag_surfxml_platform_cb_list, &parse_E_platform_add_parse_AS);
612 //surfxml_add_callback(ETag_surfxml_platform_cb_list, &DEBUG_exit);
615 /* ************************************************************************** */
616 /* ***************** GENERIC PARSE FUNCTIONS (declarations) ***************** */
618 static void generic_set_processing_units(routing_component_t rc, const char* name);
619 static void generic_set_autonomous_system(routing_component_t rc, const char* name);
620 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route);
621 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route);
623 /* ************************************************************************** */
624 /* ****************** GENERIC AUX FUNCTIONS (declarations) ****************** */
626 static route_extended_t generic_new_extended_route(routing_component_t rc, void* data);
628 /* ************************************************************************** */
629 /* *************************** FULL ROUTING ********************************* */
631 #define TO_ROUTE_FULL(i,j) routing->routing_table[(i)+(j)*table_size]
633 /* Routing model structure */
636 s_routing_component_t generic_routing;
637 xbt_dict_t parse_routes; /* store data during the parse process */
638 xbt_dict_t to_index; /* char* -> network_element_t */
639 route_extended_t *routing_table;
640 } s_routing_component_full_t,*routing_component_full_t;
642 /* Business methods */
644 static route_extended_t full_get_route(routing_component_t rc, const char* src,const char* dst) {
646 routing_component_full_t routing = (routing_component_full_t)rc;
647 int table_size = xbt_dict_length(routing->to_index);
649 xbt_assert1(src && dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
651 routing_component_t src_as, dst_as;
654 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
655 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
657 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);
658 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);
659 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);
661 src_id = xbt_dict_get(routing->to_index,src);
662 dst_id = xbt_dict_get(routing->to_index,dst);
663 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
667 route_extended_t new_e_route;
668 route_extended_t e_route = TO_ROUTE_FULL(*src_id,*dst_id);
670 new_e_route = xbt_new0(s_route_extended_t,1);
671 new_e_route->src_gateway = e_route->src_gateway;
672 new_e_route->dst_gateway = e_route->dst_gateway;
673 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
674 xbt_dynar_foreach(e_route->generic_route.link_list, cpt, link) {
675 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
683 static void full_finalize(routing_component_t rc) {
684 routing_component_full_t routing = (routing_component_full_t)rc;
685 int table_size = xbt_dict_length(routing->to_index);
688 /* Delete routing table */
689 for (i=0;i<table_size;i++) {
690 for (j=0;j<table_size;j++) {
691 route_extended_t e_route = TO_ROUTE_FULL(i,j);
693 xbt_dynar_free(&(e_route->generic_route.link_list));
694 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
695 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
700 xbt_free(routing->routing_table);
701 /* Delete index dict */
702 xbt_dict_free(&(routing->to_index));
703 /* Delete structure */
708 /* Creation routing model functions */
710 static void* model_full_create() {
711 routing_component_full_t new_component = xbt_new0(s_routing_component_full_t,1);
712 new_component->generic_routing.set_processing_units = generic_set_processing_units;
713 new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
714 new_component->generic_routing.set_route = generic_set_route;
715 new_component->generic_routing.set_ASroute = generic_set_ASroute;
716 new_component->generic_routing.get_route = full_get_route;
717 new_component->generic_routing.finalize = full_finalize;
718 new_component->to_index = xbt_dict_new();
719 new_component->parse_routes = xbt_dict_new();
720 return new_component;
723 static void model_full_load() {
724 /* use "surfxml_add_callback" to add a parse function call */
727 static void model_full_unload() {
728 /* use "surfxml_del_callback" to remove a parse function call */
731 static void model_full_end() {
733 char *key, *src_name, *dst_name;
734 const char* sep = "#";
735 int *src_id, *dst_id;
738 route_extended_t e_route;
741 xbt_dict_cursor_t cursor = NULL;
742 xbt_dynar_t keys = NULL;
745 routing_component_full_t routing = ((routing_component_full_t)current_routing);
746 int table_size = xbt_dict_length(routing->to_index);
748 /* Create the routing table */
749 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
751 /* Put the routes in position */
752 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
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 for (i = 0; i < table_size; i++) {
782 e_route = TO_ROUTE_FULL(i, i);
783 if(!e_route) { // && !xbt_dynar_length(e_route->generic_route.link_list)
784 e_route = xbt_new0(s_route_extended_t,1);
785 e_route->src_gateway = NULL;
786 e_route->dst_gateway = NULL;
787 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
788 xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
789 TO_ROUTE_FULL(i, i) = e_route;
793 /* Shrink the dynar routes (save unused slots) */
794 for (i=0;i<table_size;i++)
795 for (j=0;j<table_size;j++)
796 if(TO_ROUTE_FULL(i,j))
797 xbt_dynar_shrink(TO_ROUTE_FULL(i,j)->generic_route.link_list,0);
800 /* ************************************************************************** */
801 /* *************************** FLOYD ROUTING ******************************** */
803 #define TO_FLOYD_COST(i,j) cost_table[(i)+(j)*table_size]
804 #define TO_FLOYD_PRED(i,j) (routing->predecessor_table)[(i)+(j)*table_size]
805 #define TO_FLOYD_LINK(i,j) (routing->link_table)[(i)+(j)*table_size]
807 /* Routing model structure */
810 s_routing_component_t generic_routing;
811 /* vars for calculate the floyd algorith. */
812 int *predecessor_table;
813 route_extended_t *link_table; /* char* -> int* */
815 /* store data during the parse process */
816 xbt_dict_t parse_routes;
817 } s_routing_component_floyd_t,*routing_component_floyd_t;
819 /* Business methods */
821 static route_extended_t floyd_get_route(routing_component_t rc, const char* src,const char* dst) {
823 xbt_assert1(src && dst, "Invalid params for \"get_route\" function at AS \"%s\"",rc->name);
826 routing_component_floyd_t routing = (routing_component_floyd_t) rc;
827 int table_size = xbt_dict_length(routing->to_index);
829 route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
830 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
831 new_e_route->src_gateway = NULL;
832 new_e_route->dst_gateway = NULL;
834 int *src_id = xbt_dict_get(routing->to_index,src);
835 int *dst_id = xbt_dict_get(routing->to_index,dst);
836 xbt_assert2(src_id && dst_id, "Ask for route \"from\"(%s) or \"to\"(%s) no found in the local table",src,dst);
841 char *gw_src,*gw_dst, *prev_gw_src,*prev_gw_dst, *first_gw;
845 route_extended_t e_route_as_to_as;
848 pred = TO_FLOYD_PRED(*src_id, pred);
849 if(pred == -1) // if no pred in route -> no route to host
851 xbt_assert2(TO_FLOYD_LINK(pred,prev_pred),"Invalid link for the route between \"%s\" or \"%s\"", src, dst);
853 prev_gw_src = gw_src;
854 prev_gw_dst = gw_dst;
856 route_extended_t e_route = TO_FLOYD_LINK(pred,prev_pred);
857 gw_src = e_route->src_gateway;
858 gw_dst = e_route->dst_gateway;
860 if(first) first_gw = gw_src;
862 if(rc->hierarchy == SURF_ROUTING_RECURSIVE && !first && strcmp(prev_gw_dst,gw_src)) {
864 routing_component_t src_as = xbt_dict_get_or_null(global_routing->where_network_elements,prev_gw_dst);
865 routing_component_t dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,gw_src);
866 xbt_assert2(src_as==dst_as,"bad routing, differents AS gateways in route \"%s\" to \"%s\"",src,dst);
867 e_route_as_to_as = (*(src_as->get_route))(src_as,prev_gw_dst,gw_src);
868 links = e_route_as_to_as->generic_route.link_list;
869 xbt_dynar_foreach(links, cpt, link) {
870 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
874 links = e_route->generic_route.link_list;
875 xbt_dynar_foreach(links, cpt, link) {
876 xbt_dynar_push(new_e_route->generic_route.link_list,&link);
880 } while(pred != *src_id);
881 xbt_assert4(pred != -1, "no route from host %d to %d (\"%s\" to \"%s\")", *src_id, *dst_id,src,dst);
883 if(rc->hierarchy == SURF_ROUTING_RECURSIVE) {
884 new_e_route->src_gateway = first_gw;
885 new_e_route->dst_gateway = gw_dst;
891 static void floyd_finalize(routing_component_t rc) {
892 routing_component_floyd_t routing = (routing_component_floyd_t)rc;
895 table_size = xbt_dict_length(routing->to_index);
896 /* Delete link_table */
897 for (i=0;i<table_size;i++) {
898 for (j=0;j<table_size;j++) {
899 route_extended_t e_route = TO_FLOYD_LINK(i,j);
901 xbt_dynar_free(&(e_route->generic_route.link_list));
902 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
903 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
908 xbt_free(routing->link_table);
909 /* Delete index dict */
910 xbt_dict_free(&(routing->to_index));
911 /* Delete dictionary index dict, predecessor and links table */
912 xbt_free(routing->predecessor_table);
913 /* Delete structure */
918 static void* model_floyd_create(void) {
919 routing_component_floyd_t new_component = xbt_new0(s_routing_component_floyd_t,1);
920 new_component->generic_routing.set_processing_units = generic_set_processing_units;
921 new_component->generic_routing.set_autonomous_system = generic_set_autonomous_system;
922 new_component->generic_routing.set_route = generic_set_route;
923 new_component->generic_routing.set_ASroute = generic_set_ASroute;
924 new_component->generic_routing.get_route = floyd_get_route;
925 new_component->generic_routing.finalize = floyd_finalize;
926 new_component->to_index = xbt_dict_new();
927 new_component->parse_routes = xbt_dict_new();
928 return new_component;
931 static void model_floyd_load() {
932 /* use "surfxml_add_callback" to add a parse function call */
935 static void model_floyd_unload() {
936 /* use "surfxml_del_callback" to remove a parse function call */
939 static void model_floyd_end() {
941 routing_component_floyd_t routing = ((routing_component_floyd_t)current_routing);
942 xbt_dict_cursor_t cursor = NULL;
944 char *key,*data, *src_name, *dst_name;
945 const char *sep = "#";
947 int *src_id, *dst_id;
948 unsigned int i,j,a,b,c;
950 /* set the size of inicial table */
951 int table_size = xbt_dict_length(routing->to_index);
953 /* Create Cost, Predecessor and Link tables */
954 cost_table = xbt_new0(double, table_size*table_size); //link cost from host to host
955 routing->predecessor_table = xbt_new0(int, table_size*table_size); //predecessor host numbers
956 routing->link_table = xbt_new0(route_extended_t, table_size*table_size); //actual link between src and dst
958 /* Initialize costs and predecessors*/
959 for(i = 0; i<table_size;i++)
960 for(j = 0; j<table_size;j++) {
961 TO_FLOYD_COST(i,j) = DBL_MAX;
962 TO_FLOYD_PRED(i,j) = -1;
963 TO_FLOYD_LINK(i,j) = NULL; // FIXED DAVID
966 /* Put the routes in position */
967 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
969 keys = xbt_str_split_str(key, sep);
971 src_name = xbt_dynar_get_as(keys, 0, char*);
972 dst_name = xbt_dynar_get_as(keys, 1, char*);
974 src_id = xbt_dict_get_or_null(routing->to_index, src_name);
975 dst_id = xbt_dict_get_or_null(routing->to_index, dst_name);
977 if (src_id == NULL || dst_id == NULL )
978 THROW2(mismatch_error,0,"Network elements %s or %s not found", src_name, dst_name);
980 TO_FLOYD_LINK(*src_id,*dst_id) = generic_new_extended_route(current_routing,data);
981 TO_FLOYD_PRED(*src_id,*dst_id) = *src_id;
984 TO_FLOYD_COST(*src_id,*dst_id) = 1; // assume 1 for now
986 xbt_dynar_free(&keys);
989 /* Add the loopback if needed */
990 for (i = 0; i < table_size; i++) {
991 route_extended_t e_route = TO_FLOYD_LINK(i, i);
992 if(!e_route) { // && !xbt_dynar_length(e_route->generic_route.link_list)
993 e_route = xbt_new0(s_route_extended_t,1);
994 e_route->src_gateway = NULL;
995 e_route->dst_gateway = NULL;
996 e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
997 xbt_dynar_push(e_route->generic_route.link_list,&global_routing->loopback);
998 TO_FLOYD_LINK(i,i) = e_route;
999 TO_FLOYD_PRED(i,i) = i;
1000 TO_FLOYD_COST(i,i) = 1;
1004 //Calculate path costs
1005 for(c=0;c<table_size;c++) {
1006 for(a=0;a<table_size;a++) {
1007 for(b=0;b<table_size;b++) {
1008 if(TO_FLOYD_COST(a,c) < DBL_MAX && TO_FLOYD_COST(c,b) < DBL_MAX) {
1009 if(TO_FLOYD_COST(a,b) == DBL_MAX ||
1010 (TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b) < TO_FLOYD_COST(a,b))) {
1011 TO_FLOYD_COST(a,b) = TO_FLOYD_COST(a,c)+TO_FLOYD_COST(c,b);
1012 TO_FLOYD_PRED(a,b) = TO_FLOYD_PRED(c,b);
1019 /* delete the parse table */
1020 xbt_dict_foreach(routing->parse_routes, cursor, key, data) {
1021 route_t route = (route_t)data;
1022 xbt_dynar_free(&(route->link_list));
1026 /* delete parse dict */
1027 xbt_dict_free(&(routing->parse_routes));
1033 /* ************************************************************************** */
1034 /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1037 s_routing_component_t generic_routing;
1038 xbt_graph_t route_graph;
1039 xbt_dict_t graph_node_map;
1040 xbt_dict_t route_cache;
1041 xbt_dynar_t last_route;
1043 } s_routing_component_dijkstra_t,*routing_component_dijkstra_t;
1045 /* Parse routing model functions */
1047 /* Business methods */
1049 /* Creation routing model functions */
1051 static void* model_dijkstra_create() {
1055 static void model_dijkstra_load() {
1058 static void model_dijkstra_unload() {
1061 static void model_dijkstra_end() {
1064 static void* model_dijkstracache_create() {
1068 static void model_dijkstracache_load() {
1071 static void model_dijkstracache_unload() {
1074 static void model_dijkstracache_end() {
1078 /* ************************************************************************** */
1079 /* ******************************* NO ROUTING ******************************* */
1081 /* Routing model structure */
1083 s_routing_component_t generic_routing;
1084 } s_routing_component_none_t,*routing_component_none_t;
1086 /* Business methods */
1087 static route_extended_t none_get_route(routing_component_t rc, const char* src,const char* dst){
1090 static void none_finalize(routing_component_t rc) {
1094 /* Creation routing model functions */
1095 static void* model_none_create() {
1096 routing_component_none_t new_component = xbt_new0(s_routing_component_none_t,1);
1097 new_component->generic_routing.set_processing_units = NULL;
1098 new_component->generic_routing.set_autonomous_system = NULL;
1099 new_component->generic_routing.set_route = NULL;
1100 new_component->generic_routing.set_ASroute = NULL;
1101 new_component->generic_routing.get_route = none_get_route;
1102 new_component->generic_routing.finalize = none_finalize;
1103 return new_component;
1106 static void model_none_load() {}
1107 static void model_none_unload() {}
1108 static void model_none_end() {}
1110 /* ************************************************** */
1111 /* ********** PATERN FOR NEW ROUTING **************** */
1113 /* The minimal configuration of a new routing model need the next functions,
1114 * also you need to set at the start of the file, the new model in the model
1115 * list. Remember keep the null ending of the list.
1117 /* Routing model structure */
1119 s_routing_component_t generic_routing;
1120 /* things that your routing model need */
1121 } s_routing_component_NEW_t,*routing_component_NEW_t;
1123 /* Parse routing model functions */
1124 static void NEW_parse_S_host(void) {} /* example*/
1126 /* Business methods */
1127 static route_extended_t NEW_get_route(routing_component_t rc, const char* src,const char* dst) {return NULL;} /* mandatory */
1128 static void NEW_finalize(routing_component_t rc) {} /* mandatory */
1130 /* Creation routing model functions */
1131 static void* model_NEW_create() {return NULL;} /* mandatory */
1132 static void model_NEW_load() {} /* mandatory */
1133 static void model_NEW_unload() {} /* mandatory */
1134 static void model_NEW_end() {} /* mandatory */
1136 /* ************************************************************************** */
1137 /* ************************* GENERIC PARSE FUNCTIONS ************************ */
1139 static void generic_set_processing_units(routing_component_t rc, const char* name) {
1140 DEBUG1("Full - Load process unit \"%s\"",name);
1141 model_type_t modeltype = rc->routing;
1142 int *id = xbt_new0(int,1); // xbt_malloc(sizeof(int)); ?
1144 if(modeltype==&routing_models[SURF_MODEL_FULL])
1145 index = ((routing_component_full_t)current_routing)->to_index;
1146 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1147 index = ((routing_component_floyd_t)current_routing)->to_index;
1148 else xbt_die("\"generic_set_processing_units\" not support");
1149 *id = xbt_dict_length(index);
1150 xbt_dict_set(index,name,id,xbt_free);
1153 static void generic_set_autonomous_system(routing_component_t rc, const char* name) {
1154 DEBUG1("Full - Load Autonomous system \"%s\"",name);
1155 model_type_t modeltype = rc->routing;
1156 int *id = xbt_new0(int,1); // xbt_malloc(sizeof(int)); ?
1158 if(modeltype==&routing_models[SURF_MODEL_FULL])
1159 index = ((routing_component_full_t)current_routing)->to_index;
1160 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1161 index = ((routing_component_floyd_t)current_routing)->to_index;
1162 else xbt_die("\"generic_set_autonomous_system\" not support");
1163 *id = xbt_dict_length(index);
1164 xbt_dict_set(index,name,id,xbt_free);
1167 static void generic_set_route(routing_component_t rc, const char* src, const char* dst, route_t route) {
1168 DEBUG2("Full - Load Route from \"%s\" to \"%s\"",src,dst);
1169 model_type_t modeltype = rc->routing;
1170 xbt_dict_t parseroutes;
1171 if(modeltype==&routing_models[SURF_MODEL_FULL])
1172 parseroutes = ((routing_component_full_t)current_routing)->parse_routes;
1173 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1174 parseroutes = ((routing_component_floyd_t)current_routing)->parse_routes;
1175 else xbt_die("\"generic_set_autonomous_system\" not support");
1177 route_name = bprintf("%s#%s",src,dst);
1178 xbt_assert2(xbt_dynar_length(link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
1179 xbt_assert2(!xbt_dict_get_or_null(parseroutes,route_name),
1180 "The route between \"%s\" and \"%s\" already exist",src,dst);
1181 xbt_dict_set(parseroutes, route_name, route, NULL);
1185 static void generic_set_ASroute(routing_component_t rc, const char* src, const char* dst, route_extended_t e_route) {
1186 DEBUG4("Full - Load ASroute from \"%s(%s)\" to \"%s(%s)\"",src,e_route->src_gateway,dst,e_route->dst_gateway);
1187 model_type_t modeltype = rc->routing;
1188 xbt_dict_t parseroutes;
1189 if(modeltype==&routing_models[SURF_MODEL_FULL])
1190 parseroutes = ((routing_component_full_t)current_routing)->parse_routes;
1191 else if(modeltype==&routing_models[SURF_MODEL_FLOYD])
1192 parseroutes = ((routing_component_floyd_t)current_routing)->parse_routes;
1193 else xbt_die("\"generic_set_autonomous_system\" not support");
1195 route_name = bprintf("%s#%s",src,dst);
1196 xbt_assert2(xbt_dynar_length(link_list)>0, "Invalid count of links, must be greater than zero (%s,%s)",src,dst);
1197 xbt_assert4(!xbt_dict_get_or_null(parseroutes,route_name),
1198 "The route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exist",src,e_route->src_gateway,dst,e_route->dst_gateway);
1199 xbt_dict_set(parseroutes, route_name, e_route, NULL);
1203 /* ************************************************************************** */
1204 /* ************************* GENERIC AUX FUNCTIONS ************************** */
1206 static route_extended_t generic_new_extended_route(routing_component_t rc, void* data) {
1209 route_extended_t e_route, new_e_route;
1212 xbt_dynar_t links, links_id;
1214 new_e_route = xbt_new0(s_route_extended_t,1);
1215 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
1216 new_e_route->src_gateway = NULL;
1217 new_e_route->dst_gateway = NULL;
1219 xbt_assert0(rc->hierarchy == SURF_ROUTING_BASE || rc->hierarchy == SURF_ROUTING_RECURSIVE,
1220 "the hierarchy type is not defined");
1222 if(rc->hierarchy == SURF_ROUTING_BASE ) {
1224 route = (route_t)data;
1225 links = route->link_list;
1227 } else if(rc->hierarchy == SURF_ROUTING_RECURSIVE ) {
1229 e_route = (route_extended_t)data;
1231 // FIXME: HERE CHECK THE GATEWAY!!!!!!!!!
1232 xbt_assert0(e_route->src_gateway&&e_route->dst_gateway,"bad gateway, is null");
1234 links = e_route->generic_route.link_list;
1236 /* remeber not erase the gateway names */
1237 new_e_route->src_gateway = e_route->src_gateway;
1238 new_e_route->dst_gateway = e_route->dst_gateway;
1241 links_id = new_e_route->generic_route.link_list;
1243 xbt_dynar_foreach(links, cpt, link_name) {
1245 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1247 xbt_dynar_push(links_id,&link);
1249 THROW1(mismatch_error,0,"Link %s not found", link_name);
1255 ////////////////////////////////////////////////////////////////////////////////
1256 // HERE FINISH THE NEW CODE
1257 ////////////////////////////////////////////////////////////////////////////////
1259 //...... DEBUG ONLY .... //
1260 static void print_tree(routing_component_t rc) {
1261 printf("(%s %s)\n",rc->name,rc->routing->name);
1263 xbt_dict_cursor_t cursor = NULL;
1266 if( rc->routing == &(routing_models[SURF_MODEL_FULL]) )
1267 { xbt_dict_foreach(((routing_component_full_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); } }
1268 else if( rc->routing == &(routing_models[SURF_MODEL_FLOYD]) )
1269 { xbt_dict_foreach(((routing_component_floyd_t)rc)->to_index, cursor, key, val) { printf("<%s-%d> ",key,*val); }
1272 xbt_assert0(0, "Invalid model for call \"print_tree\"");
1274 routing_component_t elem;
1275 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
1282 //...... DEBUG ONLY .... //
1283 static void print_global() {
1284 xbt_dict_cursor_t cursor = NULL;
1286 routing_component_t elem;
1287 xbt_dict_foreach(global_routing->where_network_elements, cursor, key, elem) { printf("<%s>\n",key); }
1290 //...... DEBUG ONLY .... //
1291 static void print_AS_start(void) { printf("AS!!! %s y se rutea \"%s\"\n",A_surfxml_AS_id,A_surfxml_AS_routing); }
1292 static void print_AS_end(void) { printf("AS!!! %s\n",A_surfxml_AS_id); }
1293 static void print_host(void) { printf("host!!! %s\n",A_surfxml_host_id); }
1294 static void print_link(void) { printf("link!!! %s\n",A_surfxml_link_id); }
1295 static void print_route(void) { printf("route!!! %s a %s\n",A_surfxml_route_src,A_surfxml_route_dst); }
1296 static void print_ctn(void) { printf("ctn!!! %s\n",A_surfxml_link_c_ctn_id); }
1298 //...... DEBUG ONLY .... //
1299 static void DEBUG_exit(void) {
1301 printf("-------- print tree elements -----\n");
1302 print_tree(global_routing->root);
1303 printf("----------------------------------\n\n");
1305 printf("-------- network_elements --------\n");
1307 printf("----------------------------------\n\n");
1309 // char* names[] = { "11.A","11.B","11.C","121.A","121.B","122.A","13.A","13.B"};
1310 // int i,j,total = 8;
1312 // printf("-- print all the example routes --\n");
1313 // char* names[] = { "11.A","11.B","11.C"};
1314 // int i,j,total = 3;
1316 // char* names[] = { "141.A","141.B","143.A","143.B"};
1317 // int i,j,total = 4;
1319 // char* names[] = { "142.A","142.B","142.C" };
1320 // int i,j,total = 3;
1322 // 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"};
1323 // int i,j,total = 3+4+8+3;
1325 const char* names[] = { "11.A","11.B","11.C",
1330 "142.A","142.B","142.C",
1332 "15.A","15.B","15.C"};
1333 unsigned int i,j,total = 3+2+1+2+2+3+2+3;
1335 printf("-- print all the example routes --\n");
1338 for(i=0;i<total;i++) {
1339 for(j=0;j<total;j++) {
1340 printf("route from %s to %s >>> ",names[i],names[j]);
1341 links = (*(global_routing->get_route))(names[i],names[j]);
1343 xbt_dynar_foreach(links, cpt, link) {
1344 s_surf_resource_t* generic_resource = link;
1345 printf(" %s",generic_resource->name);
1351 printf("----------------------------------\n\n");
1353 printf("---------- call finalize ---------\n");
1354 (*(global_routing->finalize))();
1355 printf("----------------------------------\n");
1360 ////////////////////////////////////////////////////////////////////////////////
1361 // HERE END THE NEW CODE
1362 ////////////////////////////////////////////////////////////////////////////////
1364 // /* ************************************************************************** */
1365 // /* *************************** FULL ROUTING ********************************* */
1367 // s_routing_t generic_routing;
1368 // xbt_dynar_t *routing_table;
1370 // size_t size_of_link;
1371 // } s_routing_full_t,*routing_full_t;
1373 // #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
1374 // #define HOST2ROUTER(id) ((id)+(2<<29))
1375 // #define ROUTER2HOST(id) ((id)-(2>>29))
1376 // #define ISROUTER(id) ((id)>=(2<<29))
1379 // * Free the onelink routes
1381 // static void onelink_route_elem_free(void *e) {
1382 // s_onelink_t tmp = (s_onelink_t)e;
1391 // static void routing_full_parse_Shost(void) {
1392 // int *val = xbt_malloc(sizeof(int));
1393 // DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
1394 // *val = used_routing->host_count++;
1395 // xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
1396 // #ifdef HAVE_TRACING
1397 // TRACE_surf_host_define_id (A_surfxml_host_id, *val);
1401 // static void routing_full_parse_Srouter(void) {
1402 // int *val = xbt_malloc(sizeof(int));
1403 // DEBUG3("Seen router %s (%d -> #%d)",A_surfxml_router_id,used_routing->router_count,
1404 // HOST2ROUTER(used_routing->router_count));
1405 // *val = HOST2ROUTER(used_routing->router_count++);
1406 // xbt_dict_set(used_routing->host_id,A_surfxml_router_id,val,xbt_free);
1407 // #ifdef HAVE_TRACING
1408 // TRACE_surf_host_define_id (A_surfxml_host_id, *val);
1409 // TRACE_surf_host_declaration (A_surfxml_host_id, 0);
1413 // static int src_id = -1;
1414 // static int dst_id = -1;
1415 // static void routing_full_parse_Sroute_set_endpoints(void)
1417 // src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
1418 // dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
1419 // DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
1420 // route_action = A_surfxml_route_action;
1423 // static void routing_full_parse_Eroute(void)
1426 // if (src_id != -1 && dst_id != -1) {
1427 // name = bprintf("%x#%x", src_id, dst_id);
1428 // manage_route(route_table, name, route_action, 0);
1433 // /* Cluster tag functions */
1435 // static void routing_full_parse_change_cpu_data(const char *hostName,
1436 // const char *surfxml_host_power,
1437 // const char *surfxml_host_availability,
1438 // const char *surfxml_host_availability_file,
1439 // const char *surfxml_host_state_file)
1443 // SURFXML_BUFFER_SET(host_id, hostName);
1444 // SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
1445 // SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
1446 // SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
1447 // SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
1450 // static void routing_full_parse_change_link_data(const char *linkName,
1451 // const char *surfxml_link_bandwidth,
1452 // const char *surfxml_link_bandwidth_file,
1453 // const char *surfxml_link_latency,
1454 // const char *surfxml_link_latency_file,
1455 // const char *surfxml_link_state_file)
1459 // SURFXML_BUFFER_SET(link_id, linkName);
1460 // SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
1461 // SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
1462 // SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
1463 // SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
1464 // SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
1467 // static void routing_full_parse_Scluster(void)
1469 // static int AX_ptr = 0;
1471 // char *cluster_id = A_surfxml_cluster_id;
1472 // char *cluster_prefix = A_surfxml_cluster_prefix;
1473 // char *cluster_suffix = A_surfxml_cluster_suffix;
1474 // char *cluster_radical = A_surfxml_cluster_radical;
1475 // char *cluster_power = A_surfxml_cluster_power;
1476 // char *cluster_bw = A_surfxml_cluster_bw;
1477 // char *cluster_lat = A_surfxml_cluster_lat;
1478 // char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1479 // char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1480 // char *backbone_name;
1481 // unsigned int it1,it2;
1482 // char *name1,*name2;
1483 // xbt_dynar_t names = NULL;
1484 // surfxml_bufferstack_push(1);
1486 // /* Make set a set to parse the prefix/suffix/radical into a neat list of names */
1487 // DEBUG4("Make <set id='%s' prefix='%s' suffix='%s' radical='%s'>",
1488 // cluster_id,cluster_prefix,cluster_suffix,cluster_radical);
1489 // SURFXML_BUFFER_SET(set_id, cluster_id);
1490 // SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1491 // SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1492 // SURFXML_BUFFER_SET(set_radical, cluster_radical);
1494 // SURFXML_START_TAG(set);
1495 // SURFXML_END_TAG(set);
1497 // names = xbt_dict_get(set_list,cluster_id);
1499 // xbt_dynar_foreach(names,it1,name1) {
1500 // /* create the host */
1501 // routing_full_parse_change_cpu_data(name1, cluster_power, "1.0", "", "");
1502 // A_surfxml_host_state = A_surfxml_host_state_ON;
1504 // SURFXML_START_TAG(host);
1505 // SURFXML_END_TAG(host);
1507 // /* Here comes the link */
1508 // routing_full_parse_change_link_data(name1, cluster_bw, "", cluster_lat, "", "");
1509 // A_surfxml_link_state = A_surfxml_link_state_ON;
1510 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
1512 // SURFXML_START_TAG(link);
1513 // SURFXML_END_TAG(link);
1516 // /* Make backbone link */
1517 // backbone_name = bprintf("%s_bb", cluster_id);
1518 // routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
1520 // A_surfxml_link_state = A_surfxml_link_state_ON;
1521 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1523 // SURFXML_START_TAG(link);
1524 // SURFXML_END_TAG(link);
1526 // /* And now the internal routes */
1527 // xbt_dynar_foreach(names,it1,name1) {
1528 // xbt_dynar_foreach(names,it2,name2) {
1529 // if (strcmp(name1,name2)) {
1530 // A_surfxml_route_action = A_surfxml_route_action_POSTPEND;
1531 // SURFXML_BUFFER_SET(route_src,name1);
1532 // SURFXML_BUFFER_SET(route_dst,name2);
1533 // SURFXML_START_TAG(route); {
1534 // /* FIXME: name1 link is added by error about 20 lines below, so don't add it here
1535 // SURFXML_BUFFER_SET(link_c_ctn_id, name1);
1536 // SURFXML_START_TAG(link_c_ctn);
1537 // SURFXML_END_TAG(link_c_ctn);
1539 // SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
1540 // SURFXML_START_TAG(link_c_ctn);
1541 // SURFXML_END_TAG(link_c_ctn);
1543 // SURFXML_BUFFER_SET(link_c_ctn_id, name2);
1544 // SURFXML_START_TAG(link_c_ctn);
1545 // SURFXML_END_TAG(link_c_ctn);
1547 // } SURFXML_END_TAG(route);
1552 // /* Make route multi with the outside world, i.e. cluster->$* */
1555 // * This also adds an elements to the routes within the cluster,
1556 // * and I guess it's wrong, but since this element is commented out in the above
1557 // * code creating the internal routes, we're good.
1558 // * To fix it, I'd say that we need a way to understand "$*-${cluster_id}" as "whole world, but the guys in that cluster"
1559 // * But for that, we need to install a real expression parser for src/dst attributes
1562 // * This also adds a dumb element (the private link) in place of the loopback. Then, since
1563 // * the loopback is added only if no link to self already exist, this fails.
1564 // * That's really dumb.
1567 // * It seems to me that it does not add the backbone to the path to outside world...
1569 // SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
1570 // SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
1571 // A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
1572 // A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_PREPEND;
1574 // SURFXML_START_TAG(route_c_multi);
1576 // SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
1578 // SURFXML_START_TAG(link_c_ctn);
1579 // SURFXML_END_TAG(link_c_ctn);
1581 // SURFXML_END_TAG(route_c_multi);
1583 // free(backbone_name);
1585 // /* Restore buff */
1586 // surfxml_bufferstack_pop(1);
1590 // static void routing_full_parse_end(void) {
1591 // routing_full_t routing = (routing_full_t) used_routing;
1593 // unsigned int cpt = 0;
1594 // xbt_dict_cursor_t cursor = NULL;
1595 // char *key, *data, *end;
1596 // const char *sep = "#";
1597 // xbt_dynar_t links, keys;
1598 // char *link_name = NULL;
1601 // int host_count = routing->generic_routing.host_count;
1603 // /* Create the routing table */
1604 // routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
1605 // for (i=0;i<host_count;i++)
1606 // for (j=0;j<host_count;j++)
1607 // ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
1609 // /* Put the routes in position */
1610 // xbt_dict_foreach(route_table, cursor, key, data) {
1612 // links = (xbt_dynar_t) data;
1613 // keys = xbt_str_split_str(key, sep);
1615 // src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
1616 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
1617 // xbt_dynar_free(&keys);
1619 // if(xbt_dynar_length(links) == 1){
1620 // s_onelink_t new_link = (s_onelink_t) xbt_malloc0(sizeof(s_onelink));
1621 // new_link->src_id = src_id;
1622 // new_link->dst_id = dst_id;
1623 // link_name = xbt_dynar_getfirst_as(links, char*);
1624 // new_link->link_ptr = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1625 // DEBUG3("Adding onelink route from (#%d) to (#%d), link_name %s",src_id, dst_id, link_name);
1626 // xbt_dict_set(onelink_routes, link_name, (void *)new_link, onelink_route_elem_free);
1627 // #ifdef HAVE_TRACING
1628 // TRACE_surf_link_save_endpoints (link_name, src_id, dst_id);
1632 // if(ISROUTER(src_id) || ISROUTER(dst_id)) {
1633 // DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
1634 // /* Check there is only one link in the route and store the information */
1638 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
1639 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1640 // xbt_dynar_foreach(links, cpt, link_name) {
1641 // void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1643 // xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
1645 // THROW1(mismatch_error,0,"Link %s not found", link_name);
1649 // /* Add the loopback if needed */
1650 // for (i = 0; i < host_count; i++)
1651 // if (!xbt_dynar_length(ROUTE_FULL(i, i)))
1652 // xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
1654 // /* Shrink the dynar routes (save unused slots) */
1655 // for (i=0;i<host_count;i++)
1656 // for (j=0;j<host_count;j++)
1657 // xbt_dynar_shrink(ROUTE_FULL(i,j),0);
1661 // * Business methods
1663 // static xbt_dynar_t routing_full_get_route(int src,int dst) {
1664 // xbt_assert0(!(ISROUTER(src) || ISROUTER(dst)), "Ask for route \"from\" or \"to\" a router node");
1665 // return ROUTE_FULL(src,dst);
1668 // static xbt_dict_t routing_full_get_onelink_routes(void){
1669 // return onelink_routes;
1672 // static int routing_full_is_router(int id){
1673 // return ISROUTER(id);
1676 // static void routing_full_finalize(void) {
1677 // routing_full_t routing = (routing_full_t)used_routing;
1681 // for (i = 0; i < used_routing->host_count; i++)
1682 // for (j = 0; j < used_routing->host_count; j++)
1683 // xbt_dynar_free(&ROUTE_FULL(i, j));
1684 // free(routing->routing_table);
1685 // xbt_dict_free(&used_routing->host_id);
1686 // xbt_dict_free(&onelink_routes);
1692 // static void routing_model_full_create(size_t size_of_link,void *loopback) {
1693 // /* initialize our structure */
1694 // routing_full_t routing = xbt_new0(s_routing_full_t,1);
1695 // routing->generic_routing.name = "Full";
1696 // routing->generic_routing.host_count = 0;
1697 // routing->generic_routing.get_route = routing_full_get_route;
1698 // routing->generic_routing.get_onelink_routes = routing_full_get_onelink_routes;
1699 // routing->generic_routing.is_router = routing_full_is_router;
1700 // routing->generic_routing.finalize = routing_full_finalize;
1702 // routing->size_of_link = size_of_link;
1703 // routing->loopback = loopback;
1705 // /* Set it in position */
1706 // used_routing = (routing_t) routing;
1708 // /* Set the dict for onehop routes */
1709 // onelink_routes = xbt_dict_new();
1711 // routing->generic_routing.host_id = xbt_dict_new();
1713 // /* Setup the parsing callbacks we need */
1714 // // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1715 // // surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
1716 // // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
1717 // // surfxml_add_callback(STag_surfxml_route_cb_list, &routing_full_parse_Sroute_set_endpoints);
1718 // // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
1719 // // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
1721 // // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1722 // // surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
1726 /* ************************************************************************** */
1728 // static void routing_shortest_path_parse_Scluster(void)
1730 // static int AX_ptr = 0;
1732 // char *cluster_id = A_surfxml_cluster_id;
1733 // char *cluster_prefix = A_surfxml_cluster_prefix;
1734 // char *cluster_suffix = A_surfxml_cluster_suffix;
1735 // char *cluster_radical = A_surfxml_cluster_radical;
1736 // char *cluster_power = A_surfxml_cluster_power;
1737 // char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1738 // char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1739 // char *backbone_name;
1741 // surfxml_bufferstack_push(1);
1744 // SURFXML_BUFFER_SET(set_id, cluster_id);
1745 // SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1746 // SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1747 // SURFXML_BUFFER_SET(set_radical, cluster_radical);
1749 // SURFXML_START_TAG(set);
1750 // SURFXML_END_TAG(set);
1752 // /* Make foreach */
1753 // SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
1755 // SURFXML_START_TAG(foreach);
1757 // /* Make host for the foreach */
1758 // routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1759 // A_surfxml_host_state = A_surfxml_host_state_ON;
1761 // SURFXML_START_TAG(host);
1762 // SURFXML_END_TAG(host);
1764 // SURFXML_END_TAG(foreach);
1766 // /* Make backbone link */
1767 // backbone_name = bprintf("%s_bb", cluster_id);
1768 // routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
1770 // A_surfxml_link_state = A_surfxml_link_state_ON;
1771 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1773 // SURFXML_START_TAG(link);
1774 // SURFXML_END_TAG(link);
1776 // free(backbone_name);
1778 // /* Restore buff */
1779 // surfxml_bufferstack_pop(1);
1782 // /* ************************************************************************** */
1783 // /* *************************** FLOYD ROUTING ********************************* */
1785 // s_routing_t generic_routing;
1786 // int *predecessor_table;
1787 // void** link_table;
1788 // xbt_dynar_t last_route;
1790 // size_t size_of_link;
1791 // } s_routing_floyd_t,*routing_floyd_t;
1793 // #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
1794 // #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
1795 // #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
1797 // static void routing_floyd_parse_end(void) {
1799 // routing_floyd_t routing = (routing_floyd_t) used_routing;
1801 // void* link_list = NULL;
1802 // double * cost_table;
1803 // xbt_dict_cursor_t cursor = NULL;
1804 // char *key,*data, *end;
1805 // const char *sep = "#";
1806 // xbt_dynar_t links, keys;
1808 // unsigned int i,j;
1809 // unsigned int a,b,c;
1810 // int host_count = routing->generic_routing.host_count;
1811 // char * link_name = NULL;
1812 // void * link = NULL;
1814 // /* Create Cost, Predecessor and Link tables */
1815 // cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
1816 // routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
1817 // routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
1818 // routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
1820 // /* Initialize costs and predecessors*/
1821 // for(i = 0; i<host_count;i++)
1822 // for(j = 0; j<host_count;j++) {
1823 // FLOYD_COST(i,j) = DBL_MAX;
1824 // FLOYD_PRED(i,j) = -1;
1827 // /* Put the routes in position */
1828 // xbt_dict_foreach(route_table, cursor, key, data) {
1830 // links = (xbt_dynar_t)data;
1831 // keys = xbt_str_split_str(key, sep);
1834 // src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
1835 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
1836 // xbt_dynar_free(&keys);
1838 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
1839 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1840 // 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);
1842 // link_name = xbt_dynar_getfirst_as(links, char*);
1843 // link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1846 // link_list = link;
1848 // THROW1(mismatch_error,0,"Link %s not found", link_name);
1851 // FLOYD_LINK(src_id,dst_id) = link_list;
1852 // FLOYD_PRED(src_id, dst_id) = src_id;
1855 // FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
1859 // /* Add the loopback if needed */
1860 // for (i = 0; i < host_count; i++)
1861 // if (!FLOYD_PRED(i, i)) {
1862 // FLOYD_PRED(i, i) = i;
1863 // FLOYD_COST(i, i) = 1;
1864 // FLOYD_LINK(i, i) = routing->loopback;
1868 // //Calculate path costs
1870 // for(c=0;c<host_count;c++) {
1871 // for(a=0;a<host_count;a++) {
1872 // for(b=0;b<host_count;b++) {
1873 // if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
1874 // if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
1875 // FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
1876 // FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
1884 // free(cost_table);
1888 // * Business methods
1890 // static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
1892 // routing_floyd_t routing = (routing_floyd_t) used_routing;
1894 // int pred = dst_id;
1895 // int prev_pred = 0;
1897 // xbt_dynar_reset(routing->last_route);
1900 // prev_pred = pred;
1901 // pred = FLOYD_PRED(src_id, pred);
1903 // if(pred == -1) // if no pred in route -> no route to host
1906 // xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
1908 // } while(pred != src_id);
1910 // xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
1912 // return routing->last_route;
1915 // static void routing_floyd_finalize(void) {
1916 // routing_floyd_t routing = (routing_floyd_t)used_routing;
1919 // free(routing->link_table);
1920 // free(routing->predecessor_table);
1921 // xbt_dynar_free(&routing->last_route);
1922 // xbt_dict_free(&used_routing->host_id);
1928 // static xbt_dict_t routing_floyd_get_onelink_routes(void){
1929 // xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model Floyd");
1932 // static int routing_floyd_is_router(int id){
1933 // xbt_assert0(0,"The get_is_router feature is not supported in routing model Floyd");
1936 // static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
1937 // /* initialize our structure */
1938 // routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
1939 // routing->generic_routing.name = "Floyd";
1940 // routing->generic_routing.host_count = 0;
1941 // routing->generic_routing.host_id = xbt_dict_new();
1942 // routing->generic_routing.get_route = routing_floyd_get_route;
1943 // routing->generic_routing.get_onelink_routes = routing_floyd_get_onelink_routes;
1944 // routing->generic_routing.is_router = routing_floyd_is_router;
1945 // routing->generic_routing.finalize = routing_floyd_finalize;
1946 // routing->size_of_link = size_of_link;
1947 // routing->loopback = loopback;
1949 // /* Set it in position */
1950 // used_routing = (routing_t) routing;
1952 // /* Setup the parsing callbacks we need */
1953 // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1954 // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
1955 // surfxml_add_callback(STag_surfxml_route_cb_list,
1956 // &routing_full_parse_Sroute_set_endpoints);
1957 // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
1958 // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
1962 // /* ************************************************************************** */
1963 // /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1965 // s_routing_t generic_routing;
1966 // xbt_graph_t route_graph;
1967 // xbt_dict_t graph_node_map;
1968 // xbt_dict_t route_cache;
1969 // xbt_dynar_t last_route;
1972 // size_t size_of_link;
1973 // } s_routing_dijkstra_t,*routing_dijkstra_t;
1976 // typedef struct graph_node_data {
1978 // int graph_id; //used for caching internal graph id's
1979 // } s_graph_node_data_t, * graph_node_data_t;
1981 // typedef struct graph_node_map_element {
1983 // } s_graph_node_map_element_t, * graph_node_map_element_t;
1985 // typedef struct route_cache_element {
1988 // } s_route_cache_element_t, * route_cache_element_t;
1993 // static void route_cache_elem_free(void *e) {
1994 // route_cache_element_t elm=(route_cache_element_t)e;
1997 // free(elm->pred_arr);
2002 // static void graph_node_map_elem_free(void *e) {
2003 // graph_node_map_element_t elm = (graph_node_map_element_t)e;
2011 // * Utility functions
2013 // static xbt_node_t route_graph_new_node(int id, int graph_id) {
2014 // xbt_node_t node = NULL;
2015 // graph_node_data_t data = NULL;
2016 // graph_node_map_element_t elm = NULL;
2017 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2019 // data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
2021 // data->graph_id = graph_id;
2022 // node = xbt_graph_new_node(routing->route_graph, data);
2024 // elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
2025 // elm->node = node;
2026 // xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
2031 // static graph_node_map_element_t graph_node_map_search(int id) {
2032 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2034 // 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));
2042 // static void route_new_dijkstra(int src_id, int dst_id, void* link) {
2043 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2045 // xbt_node_t src = NULL;
2046 // xbt_node_t dst = NULL;
2047 // 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));
2048 // 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));
2051 // src = src_elm->node;
2054 // dst = dst_elm->node;
2056 // //add nodes if they don't exist in the graph
2057 // if(src_id == dst_id && src == NULL && dst == NULL) {
2058 // src = route_graph_new_node(src_id, -1);
2061 // if(src == NULL) {
2062 // src = route_graph_new_node(src_id, -1);
2065 // if(dst == NULL) {
2066 // dst = route_graph_new_node(dst_id, -1);
2070 // //add link as edge to graph
2071 // xbt_graph_new_edge(routing->route_graph, src, dst, link);
2075 // static void add_loopback_dijkstra(void) {
2076 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2078 // xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2080 // xbt_node_t node = NULL;
2081 // unsigned int cursor2;
2082 // xbt_dynar_foreach(nodes, cursor2, node) {
2083 // xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
2084 // xbt_edge_t edge = NULL;
2085 // unsigned int cursor;
2088 // xbt_dynar_foreach(out_edges, cursor, edge) {
2089 // xbt_node_t other_node = xbt_graph_edge_get_target(edge);
2090 // if(other_node == node) {
2097 // xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
2101 // static void routing_dijkstra_parse_end(void) {
2102 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2104 // xbt_dict_cursor_t cursor = NULL;
2105 // char *key, *data, *end;
2106 // const char *sep = "#";
2107 // xbt_dynar_t links, keys;
2108 // char* link_name = NULL;
2109 // void* link = NULL;
2110 // xbt_node_t node = NULL;
2111 // unsigned int cursor2;
2112 // xbt_dynar_t nodes = NULL;
2113 // /* Create the topology graph */
2114 // routing->route_graph = xbt_graph_new_graph(1, NULL);
2115 // routing->graph_node_map = xbt_dict_new();
2116 // routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
2117 // if(routing->cached)
2118 // routing->route_cache = xbt_dict_new();
2121 // /* Put the routes in position */
2122 // xbt_dict_foreach(route_table, cursor, key, data) {
2124 // links = (xbt_dynar_t) data;
2125 // keys = xbt_str_split_str(key, sep);
2127 // src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
2128 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
2129 // xbt_dynar_free(&keys);
2131 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
2132 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
2134 // 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);
2136 // link_name = xbt_dynar_getfirst_as(links, char*);
2137 // link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
2139 // route_new_dijkstra(src_id,dst_id,link);
2141 // THROW1(mismatch_error,0,"Link %s not found", link_name);
2145 // /* Add the loopback if needed */
2146 // add_loopback_dijkstra();
2148 // /* initialize graph indexes in nodes after graph has been built */
2149 // nodes = xbt_graph_get_nodes(routing->route_graph);
2151 // xbt_dynar_foreach(nodes, cursor2, node) {
2152 // graph_node_data_t data = xbt_graph_node_get_data(node);
2153 // data->graph_id = cursor2;
2159 // * Business methods
2161 // static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
2163 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
2164 // int * pred_arr = NULL;
2165 // int src_node_id = 0;
2166 // int dst_node_id = 0;
2167 // int * nodeid = NULL;
2170 // void * link = NULL;
2171 // route_cache_element_t elm = NULL;
2172 // xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
2174 // /*Use the graph_node id mapping set to quickly find the nodes */
2175 // graph_node_map_element_t src_elm = graph_node_map_search(src_id);
2176 // graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
2177 // xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
2178 // src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
2179 // dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
2181 // if(routing->cached) {
2182 // /*check if there is a cached predecessor list avail */
2183 // elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
2186 // if(elm) { //cached mode and cache hit
2187 // pred_arr = elm->pred_arr;
2188 // } else { //not cached mode or cache miss
2189 // double * cost_arr = NULL;
2190 // xbt_heap_t pqueue = NULL;
2193 // int nr_nodes = xbt_dynar_length(nodes);
2194 // cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
2195 // pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
2196 // pqueue = xbt_heap_new(nr_nodes, free);
2199 // cost_arr[src_node_id] = 0.0;
2201 // for(i = 0; i < nr_nodes; i++) {
2202 // if(i != src_node_id) {
2203 // cost_arr[i] = DBL_MAX;
2208 // //initialize priority queue
2209 // nodeid = xbt_new0(int, 1);
2211 // xbt_heap_push(pqueue, nodeid, cost_arr[i]);
2215 // // apply dijkstra using the indexes from the graph's node array
2216 // while(xbt_heap_size(pqueue) > 0) {
2217 // int * v_id = xbt_heap_pop(pqueue);
2218 // xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
2219 // xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
2220 // xbt_edge_t edge = NULL;
2221 // unsigned int cursor;
2223 // xbt_dynar_foreach(out_edges, cursor, edge) {
2224 // xbt_node_t u_node = xbt_graph_edge_get_target(edge);
2225 // graph_node_data_t data = xbt_graph_node_get_data(u_node);
2226 // int u_id = data->graph_id;
2227 // int cost_v_u = 1; //fixed link cost for now
2229 // if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
2230 // pred_arr[u_id] = *v_id;
2231 // cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
2232 // nodeid = xbt_new0(int, 1);
2234 // xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
2238 // //free item popped from pqueue
2243 // xbt_heap_free(pqueue);
2247 // //compose route path with links
2248 // xbt_dynar_reset(routing->last_route);
2250 // for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
2251 // xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
2252 // xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
2253 // xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
2255 // xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
2257 // link = xbt_graph_edge_get_data(edge);
2258 // xbt_dynar_unshift(routing->last_route, &link);
2263 // if(routing->cached && elm == NULL) {
2264 // //add to predecessor list of the current src-host to cache
2265 // elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
2266 // elm->pred_arr = pred_arr;
2267 // elm->size = size;
2268 // xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
2271 // if(!routing->cached)
2274 // return routing->last_route;
2278 // static void routing_dijkstra_finalize(void) {
2279 // routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
2282 // xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
2283 // xbt_dict_free(&routing->graph_node_map);
2284 // if(routing->cached)
2285 // xbt_dict_free(&routing->route_cache);
2286 // xbt_dynar_free(&routing->last_route);
2287 // xbt_dict_free(&used_routing->host_id);
2293 // static xbt_dict_t routing_dijkstraboth_get_onelink_routes(void){
2294 // xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model dijkstraboth");
2297 // static int routing_dijkstraboth_is_router(int id){
2298 // xbt_assert0(0,"The get_is_router feature is not supported in routing model dijkstraboth");
2304 // static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
2305 // /* initialize our structure */
2306 // routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
2307 // routing->generic_routing.name = "Dijkstra";
2308 // routing->generic_routing.host_count = 0;
2309 // routing->generic_routing.get_route = routing_dijkstra_get_route;
2310 // routing->generic_routing.get_onelink_routes = routing_dijkstraboth_get_onelink_routes;
2311 // routing->generic_routing.is_router = routing_dijkstraboth_is_router;
2312 // routing->generic_routing.finalize = routing_dijkstra_finalize;
2313 // routing->size_of_link = size_of_link;
2314 // routing->loopback = loopback;
2315 // routing->cached = cached;
2317 // /* Set it in position */
2318 // used_routing = (routing_t) routing;
2320 // /* Setup the parsing callbacks we need */
2321 // routing->generic_routing.host_id = xbt_dict_new();
2322 // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
2323 // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
2324 // surfxml_add_callback(STag_surfxml_route_cb_list,
2325 // &routing_full_parse_Sroute_set_endpoints);
2326 // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
2327 // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
2330 // static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
2331 // routing_model_dijkstraboth_create(size_of_link, loopback, 0);
2334 // static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
2335 // routing_model_dijkstraboth_create(size_of_link, loopback, 1);
2338 /* ************************************************** */
2339 /* ********** NO ROUTING **************************** */
2342 // static void routing_none_finalize(void) {
2343 // if (used_routing) {
2344 // xbt_dict_free(&used_routing->host_id);
2345 // free(used_routing);
2346 // used_routing=NULL;
2350 // static void routing_model_none_create(size_t size_of_link,void *loopback) {
2351 // routing_t routing = xbt_new0(s_routing_t,1);
2352 // INFO0("Null routing");
2353 // routing->name = "none";
2354 // routing->host_count = 0;
2355 // routing->host_id = xbt_dict_new();
2356 // routing->get_onelink_routes = NULL;
2357 // routing->is_router = NULL;
2358 // routing->get_route = NULL;
2360 // routing->finalize = routing_none_finalize;
2362 // /* Set it in position */
2363 // used_routing = (routing_t) routing;