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 // FIXME: the next two lines cant be comented without fix the code in network.c file.
19 routing_t used_routing = NULL;
20 xbt_dict_t onelink_routes = NULL;
22 // /* Prototypes of each model */
23 // static void routing_model_full_create(size_t size_of_link,void *loopback);
24 // // static void routing_model_floyd_create(size_t size_of_link, void*loopback);
25 // // static void routing_model_dijkstra_create(size_t size_of_link, void*loopback);
26 // // static void routing_model_dijkstracache_create(size_t size_of_link, void*loopback);
27 // //static void routing_model_none_create(size_t size_of_link, void*loopback);
29 // /* Definition of each model */
30 // struct model_type {
33 // void (*fun)(size_t,void*);
36 // struct model_type models[] =
37 // { {"Full", "Full routing data (fast, large memory requirements, fully expressive)", routing_model_full_create },
38 // // {"Floyd", "Floyd routing data (slow initialization, fast lookup, lesser memory requirements, shortest path routing only)", routing_model_floyd_create },
39 // // {"Dijkstra", "Dijkstra routing data (fast initialization, slow lookup, small memory requirements, shortest path routing only)", routing_model_dijkstra_create },
40 // // {"DijkstraCache", "Dijkstra routing data (fast initialization, fast lookup, small memory requirements, shortest path routing only)", routing_model_dijkstracache_create },
41 // // {"none", "No routing (usable with Constant network only)", routing_model_none_create },
44 ////////////////////////////////////////////////////////////////////////////////
45 // HERE START THE NEW CODE
46 ////////////////////////////////////////////////////////////////////////////////
48 //...... DEBUG ONLY .... //
49 static void print_tree(routing_component_t rc);
50 static void print_global();
51 static void print_AS_start(void);
52 static void print_AS_end(void);
53 static void print_host(void);
54 static void print_link(void);
55 static void print_route(void);
56 static void print_ctn(void);
57 static void DEGUB_exit(void);
59 ////////////////////////////////////////////////////////////////////////////////
60 // HERE START THE NEW CODE
61 ////////////////////////////////////////////////////////////////////////////////
64 routing_global_t global_routing = NULL;
65 routing_component_t current_routing = NULL;
66 model_type_t current_routing_model = NULL;
68 /* Prototypes of each model */
69 static void* model_full_create(); /* create structures for full routing model */
70 static void model_full_load(); /* load parse functions for full routing model */
71 static void model_full_unload(); /* unload parse functions for full routing model */
72 static void model_full_end(); /* finalize the creation of full routing model */
74 /* Table of routing models */
75 struct s_model_type routing_models[] =
76 { {"Full", "Full routing data", model_full_create, model_full_load, model_full_unload, model_full_end },
77 {NULL,NULL,NULL,NULL,NULL,NULL}};
80 /* global parse functions */
83 * \brief Add a "host" to the network element list
85 * Allows find a "host" in any routing component
87 static void parse_S_host(void) { // FIXME: if not exists
88 xbt_dict_set(global_routing->where_network_elements,A_surfxml_host_id,(void*)current_routing,NULL);
92 * \brief Add a "router" to the network element list
94 * Allows find a "router" in any routing component
96 static void parse_S_router(void) { // FIXME: if not exists
97 xbt_dict_set(global_routing->where_network_elements,A_surfxml_router_id,(void*)current_routing,NULL);
101 * \brief Add a "gateway" to the network element list
103 * Allows find a "gateway" in any routing component
105 static void parse_S_gateway(void) { // FIXME: if not exists
106 xbt_dict_set(global_routing->where_network_elements,A_surfxml_gateway_id,(void*)current_routing,NULL);
110 * \brief Make a new routing component
112 * Detect the routing model type of the routing component, make the new structure and
113 * set the parsing functions to allows parsing the part of the routing tree
115 static void parse_S_AS(void) {
116 routing_component_t new_routing;
117 model_type_t model = NULL;
118 char* wanted = A_surfxml_AS_routing;
120 /* seach the routing model */
121 for (cpt=0;routing_models[cpt].name;cpt++)
122 if (!strcmp(wanted,routing_models[cpt].name))
123 model = &routing_models[cpt];
124 /* if its not exist, error */
125 if( model == NULL ) {
126 fprintf(stderr,"Routing model %s not found. Existing models:\n",wanted);
127 for (cpt=0;routing_models[cpt].name;cpt++)
128 if (!strcmp(wanted,routing_models[cpt].name))
129 fprintf(stderr," %s: %s\n",routing_models[cpt].name,routing_models[cpt].desc);
133 /* make a new routing component */
134 new_routing = (routing_component_t)(*(model->create))();
135 new_routing->routing = model;
136 new_routing->name = xbt_strdup(A_surfxml_AS_id);
137 new_routing->routing_sons = xbt_dict_new();
139 if( current_routing == NULL && global_routing->root == NULL )
140 { /* it is the first one */
141 new_routing->routing_father = NULL;
142 global_routing->root = new_routing;
144 else if( current_routing != NULL && global_routing->root != NULL )
145 { /* it is a part of the tree */
146 new_routing->routing_father = current_routing;
147 xbt_dict_set(current_routing->routing_sons,A_surfxml_AS_id,(void*)new_routing,NULL);
148 /* unload the prev parse rules */
149 (*(current_routing->routing->unload))();
153 THROW0(arg_error,0,"All defined components must be belong to a AS");
155 /* set the new parse rules */
156 (*(new_routing->routing->load))();
157 /* set the new current component of the tree */
158 current_routing = new_routing;
162 * \brief Finish the creation of a new routing component
164 * When you finish to read the routing component, other structures must be created.
165 * the "end" method allow to do that for any routing model type
167 static void parse_E_AS(void) {
169 if( current_routing == NULL ) {
170 THROW1(arg_error,0,"Close AS(%s), that never open",A_surfxml_AS_id);
172 (*(current_routing->routing->unload))();
173 (*(current_routing->routing->end))();
174 current_routing = current_routing->routing_father;
175 if( current_routing != NULL )
176 (*(current_routing->routing->load))();
181 * \brief Recursive function for add the differents AS to global dict
183 * This fuction is call by "parse_E_platform_add_parse_AS". It allow to add the
184 * AS or routing components, to the where_network_elements dictionary. In the same
185 * way as "parse_S_host", "parse_S_router" and "parse_S_gateway" do.
187 static void _add_parse_AS(routing_component_t rc) {
188 xbt_dict_set(global_routing->where_network_elements,rc->name,rc->routing_father,NULL);
189 xbt_dict_cursor_t cursor = NULL;
192 routing_component_t elem;
193 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
199 * \brief Add a "AS" to the network element list
201 * Allows find a "AS" in any routing component
203 static void parse_E_platform_add_parse_AS(void) {
204 _add_parse_AS(global_routing->root);
207 /* Global Business methods */
210 * \brief Generic method: find a route between hosts
212 * \param src the source host name
213 * \param dst the destination host name
215 * walk through the routing components tree and find a route between hosts
216 * by calling the differents "get_route" functions in each routing component
218 static xbt_dynar_t get_route(const char* src,const char* dst) {
220 routing_component_t src_as, dst_as;
221 int index_src, index_dst, index_father_src, index_father_dst;
222 int *src_id, *dst_id;
223 xbt_dynar_t path_src = NULL;
224 xbt_dynar_t path_dst = NULL;
225 char *current_router_src, *current_router_dst;
226 routing_component_t current = NULL;
227 routing_component_t* current_src_father;
228 routing_component_t* current_dst_father;
229 routing_component_t* current_src = NULL;
230 routing_component_t* current_dst = NULL;
231 routing_component_t* father;
232 route_extended_t tmp_route;
234 route_extended_t route_center_links;
235 xbt_dynar_t path_src_links, path_dst_links, path_first, path_last;
238 xbt_dynar_reset(global_routing->last_route);
240 /* (1) find the as-routetree where the src and dst are located */
241 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
242 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
243 xbt_assert0(src_as != NULL && dst_as != NULL, "Ask for route \"from\" or \"to\" no found");
245 /* (2) find the path to the root routing componen */
246 path_src = xbt_dynar_new(sizeof(routing_component_t), NULL);
248 while( current != NULL ) {
249 xbt_dynar_push(path_src,¤t);
250 current = current->routing_father;
252 path_dst = xbt_dynar_new(sizeof(routing_component_t), NULL);
254 while( current != NULL ) {
255 xbt_dynar_push(path_dst,¤t);
256 current = current->routing_father;
259 /* (3) find the common father */
260 index_src = (path_src->used)-1;
261 index_dst = (path_dst->used)-1;
262 current_src = xbt_dynar_get_ptr(path_src,index_src);
263 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
264 while( index_src >= 0 && index_dst >= 0 && *current_src == *current_dst ) {
265 current_src = xbt_dynar_get_ptr(path_src,index_src);
266 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
272 current_src = xbt_dynar_get_ptr(path_src,index_src);
273 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
275 /* (4) if they are in the same routing component? */
276 if( *current_src == *current_dst ) {
278 /* (5.1) belong to the same routing component */
279 tmp_route = (*(*current_src)->get_route)(*current_src,src,dst);
281 xbt_dynar_foreach(tmp_route->generic_route.link_list, cpt, link) {
282 xbt_dynar_push(global_routing->last_route,&link);
287 /* (5.2) they are not in the same routetree, make the path */
288 index_father_src = index_src+1;
289 index_father_dst = index_dst+1;
290 father = xbt_dynar_get_ptr(path_src,index_father_src);
291 route_center_links = (*((*father)->get_route))(*father,(*current_src)->name,(*current_dst)->name);
293 /* (5.2.1) make the source path */
294 path_src_links = xbt_dynar_new(global_routing->size_of_link, NULL);
295 current_router_dst = route_center_links->src_gateway; /* first router in the reverse path */
296 current_src_father = xbt_dynar_get_ptr(path_src,index_src);
298 for(index_src;index_src>=0;index_src--) {
299 current_src = xbt_dynar_get_ptr(path_src,index_src);
300 tmp_route = (*((*current_src_father)->get_route))(*current_src_father,(*current_src)->name,current_router_dst);
302 xbt_dynar_foreach(tmp_route->generic_route.link_list, cpt, link) {
303 //xbt_assert2(link!=NULL,"NULL link in the route from src %d to dst %d (5.2.1)", src, dst);
304 xbt_dynar_push(path_src_links,&link);
306 current_router_dst = tmp_route->src_gateway;
307 current_src_father = current_src;
310 /* (5.2.2) make the destination path */
311 path_dst_links = xbt_dynar_new(global_routing->size_of_link, NULL);
312 current_router_src = route_center_links->dst_gateway; /* last router in the reverse path */
313 current_dst_father = xbt_dynar_get_ptr(path_dst,index_dst);
315 for(index_dst;index_dst>=0;index_dst--) {
316 current_dst = xbt_dynar_get_ptr(path_dst,index_dst);
317 tmp_route = (*((*current_dst_father)->get_route))(*current_dst_father,current_router_src,(*current_dst)->name);
319 xbt_dynar_foreach(tmp_route->generic_route.link_list, cpt, link) {
320 //xbt_assert2(link!=NULL,"NULL link in the route from src %d to dst %d (5.2.1)", src, dst);
321 xbt_dynar_push(path_dst_links,&link);
323 current_router_src = tmp_route->dst_gateway;
324 current_dst_father = current_dst;
327 /* (5.2.3) the first part of the route */
328 path_first = ((*((*current_src)->get_route))(*current_src,src,current_router_dst))->generic_route.link_list;
330 /* (5.2.4) the last part of the route */
331 path_last = ((*((*current_dst)->get_route))(*current_dst,dst,current_router_src))->generic_route.link_list;
333 /* (5.2.5) make all paths together */
334 // FIXME: the paths non respect the right order
337 xbt_dynar_foreach(path_first, cpt, link) {
338 xbt_dynar_push(global_routing->last_route,&link);
341 xbt_dynar_foreach(path_src_links, cpt, link) {
342 xbt_dynar_push(global_routing->last_route,&link);
345 xbt_dynar_foreach(route_center_links->generic_route.link_list, cpt, link) {
346 xbt_dynar_push(global_routing->last_route,&link);
349 xbt_dynar_foreach(path_dst_links, cpt, link) {
350 xbt_dynar_push(global_routing->last_route,&link);
353 xbt_dynar_foreach(path_last, cpt, link) {
354 xbt_dynar_push(global_routing->last_route,&link);
357 xbt_dynar_free(&path_src_links);
358 xbt_dynar_free(&path_dst_links);
361 xbt_dynar_free(&path_src);
362 xbt_dynar_free(&path_dst);
364 return global_routing->last_route;
368 * \brief Recursive function for finalize
370 * \param rc the source host name
372 * This fuction is call by "finalize". It allow to finalize the
373 * AS or routing components. It delete all the structures.
375 static void _finalize(routing_component_t rc) {
377 xbt_dict_cursor_t cursor = NULL;
379 routing_component_t elem;
380 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
383 xbt_dict_free(&(rc->routing_sons));
385 (*(rc->finalize))(rc);
390 * \brief Generic method: delete all the routing structures
392 * walk through the routing components tree and delete the structures
393 * by calling the differents "finalize" functions in each routing component
395 static void finalize(void) {
396 /* delete recursibly all the tree */
397 _finalize(global_routing->root);
398 /* delete "where" dict */
399 xbt_dict_free(&(global_routing->where_network_elements));
400 /* delete last_route */
401 xbt_dynar_free(&(global_routing->last_route));
402 /* delete global routing structure */
403 xbt_free(global_routing);
407 * \brief Generic method: create the global routing schema
409 * Make a global routing structure and set all the parsing functions.
411 void routing_model_create(size_t size_of_links, void* loopback) {
413 /* config the uniq global routing */
414 global_routing = xbt_new0(s_routing_global_t,1);
415 global_routing->where_network_elements = xbt_dict_new();
416 global_routing->root = NULL;
417 global_routing->get_route = get_route;
418 global_routing->finalize = finalize;
419 global_routing->loopback = loopback;
420 global_routing->size_of_link = size_of_links;
421 global_routing->last_route = xbt_dynar_new(size_of_links, NULL);
423 /* no current routing at moment */
424 current_routing = NULL;
426 /* parse generic elements */
427 surfxml_add_callback(STag_surfxml_host_cb_list, &parse_S_host);
428 surfxml_add_callback(STag_surfxml_router_cb_list, &parse_S_router);
429 surfxml_add_callback(STag_surfxml_gateway_cb_list, &parse_S_gateway);
431 /* parse generic elements */
432 surfxml_add_callback(STag_surfxml_AS_cb_list, &parse_S_AS);
433 surfxml_add_callback(ETag_surfxml_AS_cb_list, &parse_E_AS);
435 /* set all the as in the global where table (recursive fuction) */
436 surfxml_add_callback(ETag_surfxml_platform_cb_list, &parse_E_platform_add_parse_AS);
439 surfxml_add_callback(ETag_surfxml_platform_cb_list, &DEGUB_exit);
443 /* ************************************************************************** */
444 /* *************************** FULL ROUTING ********************************* */
446 #define TO_ROUTE_FULL(i,j) ((routing_component_full_t)current_routing)->routing_table[(i)+(j)*xbt_dict_length(((routing_component_full_t)current_routing)->to_index)]
447 #define TO_ROUTE_FULL_RC(i,j,rc) ((routing_component_full_t)rc)->routing_table[(i)+(j)*xbt_dict_length(((routing_component_full_t)rc)->to_index)]
449 /* Routing model structure */
452 s_routing_component_t generic_routing;
453 xbt_dict_t to_index; /* char* -> index* */
454 route_extended_t *routing_table;
455 xbt_dict_t parse_routes;
456 } s_routing_componentl_full_t,*routing_component_full_t;
458 /* Parse routing model functions */
460 static char* src = NULL; /* temporary store the source name of a route */
461 static char* dst = NULL; /* temporary store the destination name of a route */
462 static char* gw_src = NULL; /* temporary store the gateway source name of a route */
463 static char* gw_dst = NULL; /* temporary store the gateway destination name of a route */
464 static xbt_dynar_t link_list = NULL; /* temporary store of current list link of a route */
466 static void full_parse_S_host(void) {
467 int *val = xbt_malloc(sizeof(int));
468 *val = xbt_dict_length(((routing_component_full_t)current_routing)->to_index);
469 xbt_dict_set(((routing_component_full_t)current_routing)->to_index,A_surfxml_host_id,val,xbt_free);
472 static void full_parse_S_gateway(void) {
473 int *val = xbt_malloc(sizeof(int));
474 *val = xbt_dict_length(((routing_component_full_t)current_routing)->to_index);
475 xbt_dict_set(((routing_component_full_t)current_routing)->to_index,A_surfxml_gateway_id,val,xbt_free);
478 static void full_parse_S_route_new_and_endpoints(void) {
479 if( src != NULL && dst != NULL && link_list != NULL )
480 THROW2(arg_error,0,"Route between %s to %s can not be defined",A_surfxml_route_src,A_surfxml_route_dst);
481 src = A_surfxml_route_src;
482 dst = A_surfxml_route_dst;
483 gw_src = A_surfxml_route_gw_src;
484 gw_dst = A_surfxml_route_gw_dst;
485 link_list = xbt_dynar_new(sizeof(char*), &xbt_free_ref);
488 static void parse_E_link_c_ctn_new_elem(void) {
490 val = xbt_strdup(A_surfxml_link_c_ctn_id);
491 xbt_dynar_push(link_list, &val);
494 static void full_parse_E_route_store_route(void) {
496 route_name = bprintf("%s#%s#%s#%s",src,dst,gw_src,gw_dst);
497 // FIXME: if not exists
498 xbt_dict_set(((routing_component_full_t)current_routing)->parse_routes, route_name, link_list, NULL);
505 /* Business methods */
507 static route_extended_t full_get_route(routing_component_t rc, const char* src,const char* dst) {
509 routing_component_full_t src_as, dst_as;
512 // FIXME: here we can use the current_routing var.
514 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src);
515 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst);
517 xbt_assert0(src_as != NULL && dst_as != NULL, "Ask for route \"from\" or \"to\" no found");
519 xbt_assert0(src_as == dst_as, "The src and dst are in differents AS");
521 xbt_assert0((routing_component_full_t)rc == dst_as, "The routing component of src and dst is not the same as the network elements belong");
523 src_id = (int*)xbt_dict_get(src_as->to_index,src);
524 dst_id = (int*)xbt_dict_get(dst_as->to_index,dst);
526 xbt_assert0(src_id != NULL && dst_id != NULL, "Ask for route \"from\" or \"to\" no found in the local table");
528 return TO_ROUTE_FULL_RC(*src_id,*dst_id,src_as);
531 static void full_finalize(routing_component_t rc) {
532 routing_component_full_t routing = (routing_component_full_t)rc;
535 int table_size = xbt_dict_length(routing->to_index);
536 /* Delete routing table */
537 for (i=0;i<table_size;i++) {
538 for (j=0;j<table_size;j++) {
539 route_extended_t e_route = TO_ROUTE_FULL_RC(i,j,rc);
540 xbt_dynar_free(&(e_route->generic_route.link_list));
541 if(e_route->src_gateway) xbt_free(e_route->src_gateway);
542 if(e_route->dst_gateway) xbt_free(e_route->dst_gateway);
546 xbt_free(routing->routing_table);
547 /* Delete index dict */
548 xbt_dict_free(&(routing->to_index));
549 /* Delete structure */
554 /* Creation routing model functions */
556 static void* model_full_create() {
557 routing_component_full_t new_component = xbt_new0(s_routing_componentl_full_t,1);
558 new_component->generic_routing.get_route = full_get_route;
559 new_component->generic_routing.finalize = full_finalize;
560 new_component->to_index = xbt_dict_new();
561 new_component->parse_routes = xbt_dict_new();
562 return new_component;
565 static void model_full_load() {
566 surfxml_add_callback(STag_surfxml_host_cb_list, &full_parse_S_host);
567 surfxml_add_callback(STag_surfxml_gateway_cb_list, &full_parse_S_gateway);
568 surfxml_add_callback(ETag_surfxml_link_c_ctn_cb_list, &parse_E_link_c_ctn_new_elem);
569 surfxml_add_callback(STag_surfxml_route_cb_list, &full_parse_S_route_new_and_endpoints);
570 surfxml_add_callback(ETag_surfxml_route_cb_list, &full_parse_E_route_store_route);
573 static void model_full_unload() {
574 surfxml_del_callback(&STag_surfxml_host_cb_list, &full_parse_S_host);
575 surfxml_del_callback(&STag_surfxml_gateway_cb_list, &full_parse_S_gateway);
576 surfxml_del_callback(&ETag_surfxml_link_c_ctn_cb_list, &parse_E_link_c_ctn_new_elem);
577 surfxml_del_callback(&STag_surfxml_route_cb_list, &full_parse_S_route_new_and_endpoints);
578 surfxml_del_callback(&ETag_surfxml_route_cb_list, &full_parse_E_route_store_route);
581 static void model_full_end() {
583 char *key, *end, *link_name, *src_name, *dst_name, *src_gw_name, *dst_gw_name;
584 const char* sep = "#";
585 int *src_id, *dst_id, *src_gw_id, *dst_gw_id;
587 routing_component_t component;
589 xbt_dict_cursor_t cursor = NULL;
590 xbt_dynar_t keys = NULL;
591 routing_component_full_t src_as;
592 routing_component_full_t dst_as;
594 routing_component_full_t routing = ((routing_component_full_t)current_routing);
596 /* Add the AS found to local components */
597 xbt_dict_foreach(current_routing->routing_sons, cursor, key, component) {
598 int *val = xbt_malloc(sizeof(int));
599 *val = xbt_dict_length(routing->to_index);
601 xbt_dict_set(routing->to_index,key,val,xbt_free);
604 int table_size = xbt_dict_length(routing->to_index);
606 /* Create the routing table */
607 routing->routing_table = xbt_new0(route_extended_t, table_size * table_size);
608 for (i=0;i<table_size;i++) {
609 for (j=0;j<table_size;j++) {
610 route_extended_t new_e_route = xbt_new0(s_route_extended_t,1);
611 new_e_route->generic_route.link_list = xbt_dynar_new(global_routing->size_of_link,NULL);
612 new_e_route->src_gateway = NULL;
613 new_e_route->dst_gateway = NULL;
614 TO_ROUTE_FULL(i,j) = new_e_route;
618 /* Put the routes in position */
619 xbt_dict_foreach(routing->parse_routes, cursor, key, links) {
621 keys = xbt_str_split_str(key, sep);
623 src_name = xbt_dynar_get_as(keys, 0, char*);
624 dst_name = xbt_dynar_get_as(keys, 1, char*);
625 src_gw_name = xbt_dynar_get_as(keys, 2, char*);
626 dst_gw_name = xbt_dynar_get_as(keys, 3, char*);
628 src_id = xbt_dict_get_or_null(routing->to_index, src_name);
629 dst_id = xbt_dict_get_or_null(routing->to_index, dst_name);
631 if (src_id == NULL || dst_id == NULL )
632 THROW2(mismatch_error,0,"Network elements %s or %s not found", src_name, dst_name);
634 if( strlen(src_gw_name) != 0 ) {
635 src_as = xbt_dict_get_or_null(global_routing->where_network_elements,src_gw_name);
637 THROW1(mismatch_error,0,"Network elements %s not found", src_gw_name);
638 src_gw_id = xbt_dict_get_or_null(src_as->to_index, src_gw_name);
639 if (src_gw_id == NULL)
640 THROW1(mismatch_error,0,"Network elements %s not found in the local tablfe", src_gw_name);
642 src_gw_name = NULL; // FIXME: maybe here put the same src_name.
645 if( strlen(dst_gw_name) != 0 ) {
646 dst_as = xbt_dict_get_or_null(global_routing->where_network_elements,dst_gw_name);
648 THROW1(mismatch_error,0,"Network elements %s not found", dst_gw_name);
649 dst_gw_id = xbt_dict_get_or_null(dst_as->to_index, dst_gw_name);
650 if (dst_gw_id == NULL)
651 THROW1(mismatch_error,0,"Network elements %s not found in the local table", dst_gw_name);
653 dst_gw_name = NULL; // FIXME: maybe here put the same dst_name.
656 TO_ROUTE_FULL(*src_id,*dst_id)->src_gateway = xbt_strdup(src_gw_name);
657 TO_ROUTE_FULL(*src_id,*dst_id)->dst_gateway = xbt_strdup(dst_gw_name);
659 xbt_dynar_foreach(links, cpt, link_name) {
660 void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
662 xbt_dynar_push(TO_ROUTE_FULL(*src_id,*dst_id)->generic_route.link_list,&link);
664 THROW1(mismatch_error,0,"Link %s not found", link_name);
667 // xbt_dynar_free(&links);
668 xbt_dynar_free(&keys);
671 /* delete the parse table */
672 xbt_dict_foreach(routing->parse_routes, cursor, key, links) {
673 xbt_dynar_free(&links);
676 /* delete parse dict */
677 xbt_dict_free(&(routing->parse_routes));
679 /* Add the loopback if needed */
680 for (i = 0; i < table_size; i++)
681 if (!xbt_dynar_length(TO_ROUTE_FULL(i, i)->generic_route.link_list))
682 xbt_dynar_push(TO_ROUTE_FULL(i,i)->generic_route.link_list,&global_routing->loopback);
684 /* Shrink the dynar routes (save unused slots) */
685 for (i=0;i<table_size;i++)
686 for (j=0;j<table_size;j++)
687 xbt_dynar_shrink(TO_ROUTE_FULL(i,j)->generic_route.link_list,0);
690 ////////////////////////////////////////////////////////////////////////////////
691 // HERE FINISH THE NEW CODE
692 ////////////////////////////////////////////////////////////////////////////////
694 //...... DEBUG ONLY .... //
695 static void print_tree(routing_component_t rc) {
696 printf("(%s %s)\n",rc->name,rc->routing->name);
698 xbt_dict_cursor_t cursor = NULL;
701 xbt_dict_foreach(((routing_component_full_t)rc)->to_index, cursor, key, val) {
702 printf("<%s-%d> ",key,*val);
705 routing_component_t elem;
706 xbt_dict_foreach(rc->routing_sons, cursor, key, elem) {
713 //...... DEBUG ONLY .... //
714 static void print_global() {
715 xbt_dict_cursor_t cursor = NULL;
717 routing_component_t elem;
718 xbt_dict_foreach(global_routing->where_network_elements, cursor, key, elem) {
719 printf("<%s>\n",key);
723 //...... DEBUG ONLY .... //
724 static void print_AS_start(void) { printf("AS!!! %s y se rutea \"%s\"\n",A_surfxml_AS_id,A_surfxml_AS_routing); }
725 static void print_AS_end(void) { printf("AS!!! %s\n",A_surfxml_AS_id); }
726 static void print_host(void) { printf("host!!! %s\n",A_surfxml_host_id); }
727 static void print_link(void) { printf("link!!! %s\n",A_surfxml_link_id); }
728 static void print_route(void) { printf("route!!! %s a %s\n",A_surfxml_route_src,A_surfxml_route_dst); }
729 static void print_ctn(void) { printf("ctn!!! %s\n",A_surfxml_link_c_ctn_id); }
731 //...... DEBUG ONLY .... //
732 static void DEGUB_exit(void) {
734 printf("-------- print tree elements -----\n");
735 print_tree(global_routing->root);
736 printf("----------------------------------\n\n");
738 printf("-------- network_elements --------\n");
740 printf("----------------------------------\n\n");
742 printf("-- print all the example routes --\n");
743 char* names[] = { "11.A","11.B","11.C","121.A","121.B","122.A","13.A","13.B"};
747 for(i=0;i<total;i++) {
748 for(j=0;j<total;j++) {
749 links = (*(global_routing->get_route))(names[i],names[j]);
750 printf("route from %s to %s >>> ",names[i],names[j]);
752 xbt_dynar_foreach(links, cpt, link) {
753 s_surf_resource_t* generic_resource = link;
754 printf(" %s",generic_resource->name);
759 printf("----------------------------------\n\n");
761 printf("---------- call finalize ---------\n");
762 (*(global_routing->finalize))();
763 printf("----------------------------------\n");
768 ////////////////////////////////////////////////////////////////////////////////
769 // HERE END THE NEW CODE
770 ////////////////////////////////////////////////////////////////////////////////
772 // /* ************************************************************************** */
773 // /* *************************** FULL ROUTING ********************************* */
775 // s_routing_t generic_routing;
776 // xbt_dynar_t *routing_table;
778 // size_t size_of_link;
779 // } s_routing_full_t,*routing_full_t;
781 // #define ROUTE_FULL(i,j) ((routing_full_t)used_routing)->routing_table[(i)+(j)*(used_routing)->host_count]
782 // #define HOST2ROUTER(id) ((id)+(2<<29))
783 // #define ROUTER2HOST(id) ((id)-(2>>29))
784 // #define ISROUTER(id) ((id)>=(2<<29))
787 // * Free the onelink routes
789 // static void onelink_route_elem_free(void *e) {
790 // s_onelink_t tmp = (s_onelink_t)e;
799 // static void routing_full_parse_Shost(void) {
800 // int *val = xbt_malloc(sizeof(int));
801 // DEBUG2("Seen host %s (#%d)",A_surfxml_host_id,used_routing->host_count);
802 // *val = used_routing->host_count++;
803 // xbt_dict_set(used_routing->host_id,A_surfxml_host_id,val,xbt_free);
804 // #ifdef HAVE_TRACING
805 // TRACE_surf_host_define_id (A_surfxml_host_id, *val);
809 // static void routing_full_parse_Srouter(void) {
810 // int *val = xbt_malloc(sizeof(int));
811 // DEBUG3("Seen router %s (%d -> #%d)",A_surfxml_router_id,used_routing->router_count,
812 // HOST2ROUTER(used_routing->router_count));
813 // *val = HOST2ROUTER(used_routing->router_count++);
814 // xbt_dict_set(used_routing->host_id,A_surfxml_router_id,val,xbt_free);
815 // #ifdef HAVE_TRACING
816 // TRACE_surf_host_define_id (A_surfxml_host_id, *val);
817 // TRACE_surf_host_declaration (A_surfxml_host_id, 0);
821 // static int src_id = -1;
822 // static int dst_id = -1;
823 // static void routing_full_parse_Sroute_set_endpoints(void)
825 // src_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_src);
826 // dst_id = *(int*)xbt_dict_get(used_routing->host_id,A_surfxml_route_dst);
827 // DEBUG4("Route %s %d -> %s %d",A_surfxml_route_src,src_id,A_surfxml_route_dst,dst_id);
828 // route_action = A_surfxml_route_action;
831 // static void routing_full_parse_Eroute(void)
834 // if (src_id != -1 && dst_id != -1) {
835 // name = bprintf("%x#%x", src_id, dst_id);
836 // manage_route(route_table, name, route_action, 0);
841 // /* Cluster tag functions */
843 // static void routing_full_parse_change_cpu_data(const char *hostName,
844 // const char *surfxml_host_power,
845 // const char *surfxml_host_availability,
846 // const char *surfxml_host_availability_file,
847 // const char *surfxml_host_state_file)
851 // SURFXML_BUFFER_SET(host_id, hostName);
852 // SURFXML_BUFFER_SET(host_power, surfxml_host_power /*hostPower */ );
853 // SURFXML_BUFFER_SET(host_availability, surfxml_host_availability);
854 // SURFXML_BUFFER_SET(host_availability_file, surfxml_host_availability_file);
855 // SURFXML_BUFFER_SET(host_state_file, surfxml_host_state_file);
858 // static void routing_full_parse_change_link_data(const char *linkName,
859 // const char *surfxml_link_bandwidth,
860 // const char *surfxml_link_bandwidth_file,
861 // const char *surfxml_link_latency,
862 // const char *surfxml_link_latency_file,
863 // const char *surfxml_link_state_file)
867 // SURFXML_BUFFER_SET(link_id, linkName);
868 // SURFXML_BUFFER_SET(link_bandwidth, surfxml_link_bandwidth);
869 // SURFXML_BUFFER_SET(link_bandwidth_file, surfxml_link_bandwidth_file);
870 // SURFXML_BUFFER_SET(link_latency, surfxml_link_latency);
871 // SURFXML_BUFFER_SET(link_latency_file, surfxml_link_latency_file);
872 // SURFXML_BUFFER_SET(link_state_file, surfxml_link_state_file);
875 // static void routing_full_parse_Scluster(void)
877 // static int AX_ptr = 0;
879 // char *cluster_id = A_surfxml_cluster_id;
880 // char *cluster_prefix = A_surfxml_cluster_prefix;
881 // char *cluster_suffix = A_surfxml_cluster_suffix;
882 // char *cluster_radical = A_surfxml_cluster_radical;
883 // char *cluster_power = A_surfxml_cluster_power;
884 // char *cluster_bw = A_surfxml_cluster_bw;
885 // char *cluster_lat = A_surfxml_cluster_lat;
886 // char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
887 // char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
888 // char *backbone_name;
889 // unsigned int it1,it2;
890 // char *name1,*name2;
891 // xbt_dynar_t names = NULL;
892 // surfxml_bufferstack_push(1);
894 // /* Make set a set to parse the prefix/suffix/radical into a neat list of names */
895 // DEBUG4("Make <set id='%s' prefix='%s' suffix='%s' radical='%s'>",
896 // cluster_id,cluster_prefix,cluster_suffix,cluster_radical);
897 // SURFXML_BUFFER_SET(set_id, cluster_id);
898 // SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
899 // SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
900 // SURFXML_BUFFER_SET(set_radical, cluster_radical);
902 // SURFXML_START_TAG(set);
903 // SURFXML_END_TAG(set);
905 // names = xbt_dict_get(set_list,cluster_id);
907 // xbt_dynar_foreach(names,it1,name1) {
908 // /* create the host */
909 // routing_full_parse_change_cpu_data(name1, cluster_power, "1.0", "", "");
910 // A_surfxml_host_state = A_surfxml_host_state_ON;
912 // SURFXML_START_TAG(host);
913 // SURFXML_END_TAG(host);
915 // /* Here comes the link */
916 // routing_full_parse_change_link_data(name1, cluster_bw, "", cluster_lat, "", "");
917 // A_surfxml_link_state = A_surfxml_link_state_ON;
918 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_SHARED;
920 // SURFXML_START_TAG(link);
921 // SURFXML_END_TAG(link);
924 // /* Make backbone link */
925 // backbone_name = bprintf("%s_bb", cluster_id);
926 // routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
928 // A_surfxml_link_state = A_surfxml_link_state_ON;
929 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
931 // SURFXML_START_TAG(link);
932 // SURFXML_END_TAG(link);
934 // /* And now the internal routes */
935 // xbt_dynar_foreach(names,it1,name1) {
936 // xbt_dynar_foreach(names,it2,name2) {
937 // if (strcmp(name1,name2)) {
938 // A_surfxml_route_action = A_surfxml_route_action_POSTPEND;
939 // SURFXML_BUFFER_SET(route_src,name1);
940 // SURFXML_BUFFER_SET(route_dst,name2);
941 // SURFXML_START_TAG(route); {
942 // /* FIXME: name1 link is added by error about 20 lines below, so don't add it here
943 // SURFXML_BUFFER_SET(link_c_ctn_id, name1);
944 // SURFXML_START_TAG(link_c_ctn);
945 // SURFXML_END_TAG(link_c_ctn);
947 // SURFXML_BUFFER_SET(link_c_ctn_id, backbone_name);
948 // SURFXML_START_TAG(link_c_ctn);
949 // SURFXML_END_TAG(link_c_ctn);
951 // SURFXML_BUFFER_SET(link_c_ctn_id, name2);
952 // SURFXML_START_TAG(link_c_ctn);
953 // SURFXML_END_TAG(link_c_ctn);
955 // } SURFXML_END_TAG(route);
960 // /* Make route multi with the outside world, i.e. cluster->$* */
963 // * This also adds an elements to the routes within the cluster,
964 // * and I guess it's wrong, but since this element is commented out in the above
965 // * code creating the internal routes, we're good.
966 // * To fix it, I'd say that we need a way to understand "$*-${cluster_id}" as "whole world, but the guys in that cluster"
967 // * But for that, we need to install a real expression parser for src/dst attributes
970 // * This also adds a dumb element (the private link) in place of the loopback. Then, since
971 // * the loopback is added only if no link to self already exist, this fails.
972 // * That's really dumb.
975 // * It seems to me that it does not add the backbone to the path to outside world...
977 // SURFXML_BUFFER_SET(route_c_multi_src, cluster_id);
978 // SURFXML_BUFFER_SET(route_c_multi_dst, "$*");
979 // A_surfxml_route_c_multi_symmetric = A_surfxml_route_c_multi_symmetric_NO;
980 // A_surfxml_route_c_multi_action = A_surfxml_route_c_multi_action_PREPEND;
982 // SURFXML_START_TAG(route_c_multi);
984 // SURFXML_BUFFER_SET(link_c_ctn_id, "$src");
986 // SURFXML_START_TAG(link_c_ctn);
987 // SURFXML_END_TAG(link_c_ctn);
989 // SURFXML_END_TAG(route_c_multi);
991 // free(backbone_name);
993 // /* Restore buff */
994 // surfxml_bufferstack_pop(1);
998 // static void routing_full_parse_end(void) {
999 // routing_full_t routing = (routing_full_t) used_routing;
1001 // unsigned int cpt = 0;
1002 // xbt_dict_cursor_t cursor = NULL;
1003 // char *key, *data, *end;
1004 // const char *sep = "#";
1005 // xbt_dynar_t links, keys;
1006 // char *link_name = NULL;
1009 // int host_count = routing->generic_routing.host_count;
1011 // /* Create the routing table */
1012 // routing->routing_table = xbt_new0(xbt_dynar_t, host_count * host_count);
1013 // for (i=0;i<host_count;i++)
1014 // for (j=0;j<host_count;j++)
1015 // ROUTE_FULL(i,j) = xbt_dynar_new(routing->size_of_link,NULL);
1017 // /* Put the routes in position */
1018 // xbt_dict_foreach(route_table, cursor, key, data) {
1020 // links = (xbt_dynar_t) data;
1021 // keys = xbt_str_split_str(key, sep);
1023 // src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
1024 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
1025 // xbt_dynar_free(&keys);
1027 // if(xbt_dynar_length(links) == 1){
1028 // s_onelink_t new_link = (s_onelink_t) xbt_malloc0(sizeof(s_onelink));
1029 // new_link->src_id = src_id;
1030 // new_link->dst_id = dst_id;
1031 // link_name = xbt_dynar_getfirst_as(links, char*);
1032 // new_link->link_ptr = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1033 // DEBUG3("Adding onelink route from (#%d) to (#%d), link_name %s",src_id, dst_id, link_name);
1034 // xbt_dict_set(onelink_routes, link_name, (void *)new_link, onelink_route_elem_free);
1035 // #ifdef HAVE_TRACING
1036 // TRACE_surf_link_save_endpoints (link_name, src_id, dst_id);
1040 // if(ISROUTER(src_id) || ISROUTER(dst_id)) {
1041 // DEBUG2("There is route with a router here: (%d ,%d)",src_id,dst_id);
1042 // /* Check there is only one link in the route and store the information */
1046 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
1047 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1048 // xbt_dynar_foreach(links, cpt, link_name) {
1049 // void* link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1051 // xbt_dynar_push(ROUTE_FULL(src_id,dst_id),&link);
1053 // THROW1(mismatch_error,0,"Link %s not found", link_name);
1057 // /* Add the loopback if needed */
1058 // for (i = 0; i < host_count; i++)
1059 // if (!xbt_dynar_length(ROUTE_FULL(i, i)))
1060 // xbt_dynar_push(ROUTE_FULL(i,i),&routing->loopback);
1062 // /* Shrink the dynar routes (save unused slots) */
1063 // for (i=0;i<host_count;i++)
1064 // for (j=0;j<host_count;j++)
1065 // xbt_dynar_shrink(ROUTE_FULL(i,j),0);
1069 // * Business methods
1071 // static xbt_dynar_t routing_full_get_route(int src,int dst) {
1072 // xbt_assert0(!(ISROUTER(src) || ISROUTER(dst)), "Ask for route \"from\" or \"to\" a router node");
1073 // return ROUTE_FULL(src,dst);
1076 // static xbt_dict_t routing_full_get_onelink_routes(void){
1077 // return onelink_routes;
1080 // static int routing_full_is_router(int id){
1081 // return ISROUTER(id);
1084 // static void routing_full_finalize(void) {
1085 // routing_full_t routing = (routing_full_t)used_routing;
1089 // for (i = 0; i < used_routing->host_count; i++)
1090 // for (j = 0; j < used_routing->host_count; j++)
1091 // xbt_dynar_free(&ROUTE_FULL(i, j));
1092 // free(routing->routing_table);
1093 // xbt_dict_free(&used_routing->host_id);
1094 // xbt_dict_free(&onelink_routes);
1100 // static void routing_model_full_create(size_t size_of_link,void *loopback) {
1101 // /* initialize our structure */
1102 // routing_full_t routing = xbt_new0(s_routing_full_t,1);
1103 // routing->generic_routing.name = "Full";
1104 // routing->generic_routing.host_count = 0;
1105 // routing->generic_routing.get_route = routing_full_get_route;
1106 // routing->generic_routing.get_onelink_routes = routing_full_get_onelink_routes;
1107 // routing->generic_routing.is_router = routing_full_is_router;
1108 // routing->generic_routing.finalize = routing_full_finalize;
1110 // routing->size_of_link = size_of_link;
1111 // routing->loopback = loopback;
1113 // /* Set it in position */
1114 // used_routing = (routing_t) routing;
1116 // /* Set the dict for onehop routes */
1117 // onelink_routes = xbt_dict_new();
1119 // routing->generic_routing.host_id = xbt_dict_new();
1121 // /* Setup the parsing callbacks we need */
1122 // // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1123 // // surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
1124 // // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_full_parse_end);
1125 // // surfxml_add_callback(STag_surfxml_route_cb_list, &routing_full_parse_Sroute_set_endpoints);
1126 // // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
1127 // // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_full_parse_Scluster);
1129 // // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1130 // // surfxml_add_callback(STag_surfxml_router_cb_list, &routing_full_parse_Srouter);
1134 /* ************************************************************************** */
1136 // static void routing_shortest_path_parse_Scluster(void)
1138 // static int AX_ptr = 0;
1140 // char *cluster_id = A_surfxml_cluster_id;
1141 // char *cluster_prefix = A_surfxml_cluster_prefix;
1142 // char *cluster_suffix = A_surfxml_cluster_suffix;
1143 // char *cluster_radical = A_surfxml_cluster_radical;
1144 // char *cluster_power = A_surfxml_cluster_power;
1145 // char *cluster_bb_bw = A_surfxml_cluster_bb_bw;
1146 // char *cluster_bb_lat = A_surfxml_cluster_bb_lat;
1147 // char *backbone_name;
1149 // surfxml_bufferstack_push(1);
1152 // SURFXML_BUFFER_SET(set_id, cluster_id);
1153 // SURFXML_BUFFER_SET(set_prefix, cluster_prefix);
1154 // SURFXML_BUFFER_SET(set_suffix, cluster_suffix);
1155 // SURFXML_BUFFER_SET(set_radical, cluster_radical);
1157 // SURFXML_START_TAG(set);
1158 // SURFXML_END_TAG(set);
1160 // /* Make foreach */
1161 // SURFXML_BUFFER_SET(foreach_set_id, cluster_id);
1163 // SURFXML_START_TAG(foreach);
1165 // /* Make host for the foreach */
1166 // routing_full_parse_change_cpu_data("$1", cluster_power, "1.0", "", "");
1167 // A_surfxml_host_state = A_surfxml_host_state_ON;
1169 // SURFXML_START_TAG(host);
1170 // SURFXML_END_TAG(host);
1172 // SURFXML_END_TAG(foreach);
1174 // /* Make backbone link */
1175 // backbone_name = bprintf("%s_bb", cluster_id);
1176 // routing_full_parse_change_link_data(backbone_name, cluster_bb_bw, "", cluster_bb_lat, "",
1178 // A_surfxml_link_state = A_surfxml_link_state_ON;
1179 // A_surfxml_link_sharing_policy = A_surfxml_link_sharing_policy_FATPIPE;
1181 // SURFXML_START_TAG(link);
1182 // SURFXML_END_TAG(link);
1184 // free(backbone_name);
1186 // /* Restore buff */
1187 // surfxml_bufferstack_pop(1);
1190 // /* ************************************************************************** */
1191 // /* *************************** FLOYD ROUTING ********************************* */
1193 // s_routing_t generic_routing;
1194 // int *predecessor_table;
1195 // void** link_table;
1196 // xbt_dynar_t last_route;
1198 // size_t size_of_link;
1199 // } s_routing_floyd_t,*routing_floyd_t;
1201 // #define FLOYD_COST(i,j) cost_table[(i)+(j)*(used_routing)->host_count]
1202 // #define FLOYD_PRED(i,j) ((routing_floyd_t)used_routing)->predecessor_table[(i)+(j)*(used_routing)->host_count]
1203 // #define FLOYD_LINK(i,j) ((routing_floyd_t)used_routing)->link_table[(i)+(j)*(used_routing)->host_count]
1205 // static void routing_floyd_parse_end(void) {
1207 // routing_floyd_t routing = (routing_floyd_t) used_routing;
1209 // void* link_list = NULL;
1210 // double * cost_table;
1211 // xbt_dict_cursor_t cursor = NULL;
1212 // char *key,*data, *end;
1213 // const char *sep = "#";
1214 // xbt_dynar_t links, keys;
1216 // unsigned int i,j;
1217 // unsigned int a,b,c;
1218 // int host_count = routing->generic_routing.host_count;
1219 // char * link_name = NULL;
1220 // void * link = NULL;
1222 // /* Create Cost, Predecessor and Link tables */
1223 // cost_table = xbt_new0(double, host_count * host_count); //link cost from host to host
1224 // routing->predecessor_table = xbt_new0(int, host_count*host_count); //predecessor host numbers
1225 // routing->link_table = xbt_new0(void*,host_count*host_count); //actual link between src and dst
1226 // routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
1228 // /* Initialize costs and predecessors*/
1229 // for(i = 0; i<host_count;i++)
1230 // for(j = 0; j<host_count;j++) {
1231 // FLOYD_COST(i,j) = DBL_MAX;
1232 // FLOYD_PRED(i,j) = -1;
1235 // /* Put the routes in position */
1236 // xbt_dict_foreach(route_table, cursor, key, data) {
1238 // links = (xbt_dynar_t)data;
1239 // keys = xbt_str_split_str(key, sep);
1242 // src_id = strtol(xbt_dynar_get_as(keys, 0, char*), &end, 16);
1243 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char*), &end, 16);
1244 // xbt_dynar_free(&keys);
1246 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
1247 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1248 // 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);
1250 // link_name = xbt_dynar_getfirst_as(links, char*);
1251 // link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1254 // link_list = link;
1256 // THROW1(mismatch_error,0,"Link %s not found", link_name);
1259 // FLOYD_LINK(src_id,dst_id) = link_list;
1260 // FLOYD_PRED(src_id, dst_id) = src_id;
1263 // FLOYD_COST(src_id, dst_id) = 1; // assume 1 for now
1267 // /* Add the loopback if needed */
1268 // for (i = 0; i < host_count; i++)
1269 // if (!FLOYD_PRED(i, i)) {
1270 // FLOYD_PRED(i, i) = i;
1271 // FLOYD_COST(i, i) = 1;
1272 // FLOYD_LINK(i, i) = routing->loopback;
1276 // //Calculate path costs
1278 // for(c=0;c<host_count;c++) {
1279 // for(a=0;a<host_count;a++) {
1280 // for(b=0;b<host_count;b++) {
1281 // if(FLOYD_COST(a,c) < DBL_MAX && FLOYD_COST(c,b) < DBL_MAX) {
1282 // if(FLOYD_COST(a,b) == DBL_MAX || (FLOYD_COST(a,c)+FLOYD_COST(c,b) < FLOYD_COST(a,b))) {
1283 // FLOYD_COST(a,b) = FLOYD_COST(a,c)+FLOYD_COST(c,b);
1284 // FLOYD_PRED(a,b) = FLOYD_PRED(c,b);
1292 // free(cost_table);
1296 // * Business methods
1298 // static xbt_dynar_t routing_floyd_get_route(int src_id,int dst_id) {
1300 // routing_floyd_t routing = (routing_floyd_t) used_routing;
1302 // int pred = dst_id;
1303 // int prev_pred = 0;
1305 // xbt_dynar_reset(routing->last_route);
1308 // prev_pred = pred;
1309 // pred = FLOYD_PRED(src_id, pred);
1311 // if(pred == -1) // if no pred in route -> no route to host
1314 // xbt_dynar_unshift(routing->last_route, &FLOYD_LINK(pred,prev_pred));
1316 // } while(pred != src_id);
1318 // xbt_assert2(pred != -1, "no route from host %d to %d", src_id, dst_id);
1320 // return routing->last_route;
1323 // static void routing_floyd_finalize(void) {
1324 // routing_floyd_t routing = (routing_floyd_t)used_routing;
1327 // free(routing->link_table);
1328 // free(routing->predecessor_table);
1329 // xbt_dynar_free(&routing->last_route);
1330 // xbt_dict_free(&used_routing->host_id);
1336 // static xbt_dict_t routing_floyd_get_onelink_routes(void){
1337 // xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model Floyd");
1340 // static int routing_floyd_is_router(int id){
1341 // xbt_assert0(0,"The get_is_router feature is not supported in routing model Floyd");
1344 // static void routing_model_floyd_create(size_t size_of_link,void *loopback) {
1345 // /* initialize our structure */
1346 // routing_floyd_t routing = xbt_new0(s_routing_floyd_t,1);
1347 // routing->generic_routing.name = "Floyd";
1348 // routing->generic_routing.host_count = 0;
1349 // routing->generic_routing.host_id = xbt_dict_new();
1350 // routing->generic_routing.get_route = routing_floyd_get_route;
1351 // routing->generic_routing.get_onelink_routes = routing_floyd_get_onelink_routes;
1352 // routing->generic_routing.is_router = routing_floyd_is_router;
1353 // routing->generic_routing.finalize = routing_floyd_finalize;
1354 // routing->size_of_link = size_of_link;
1355 // routing->loopback = loopback;
1357 // /* Set it in position */
1358 // used_routing = (routing_t) routing;
1360 // /* Setup the parsing callbacks we need */
1361 // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1362 // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_floyd_parse_end);
1363 // surfxml_add_callback(STag_surfxml_route_cb_list,
1364 // &routing_full_parse_Sroute_set_endpoints);
1365 // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
1366 // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
1370 // /* ************************************************************************** */
1371 // /* ********** Dijkstra & Dijkstra Cached ROUTING **************************** */
1373 // s_routing_t generic_routing;
1374 // xbt_graph_t route_graph;
1375 // xbt_dict_t graph_node_map;
1376 // xbt_dict_t route_cache;
1377 // xbt_dynar_t last_route;
1380 // size_t size_of_link;
1381 // } s_routing_dijkstra_t,*routing_dijkstra_t;
1384 // typedef struct graph_node_data {
1386 // int graph_id; //used for caching internal graph id's
1387 // } s_graph_node_data_t, * graph_node_data_t;
1389 // typedef struct graph_node_map_element {
1391 // } s_graph_node_map_element_t, * graph_node_map_element_t;
1393 // typedef struct route_cache_element {
1396 // } s_route_cache_element_t, * route_cache_element_t;
1401 // static void route_cache_elem_free(void *e) {
1402 // route_cache_element_t elm=(route_cache_element_t)e;
1405 // free(elm->pred_arr);
1410 // static void graph_node_map_elem_free(void *e) {
1411 // graph_node_map_element_t elm = (graph_node_map_element_t)e;
1419 // * Utility functions
1421 // static xbt_node_t route_graph_new_node(int id, int graph_id) {
1422 // xbt_node_t node = NULL;
1423 // graph_node_data_t data = NULL;
1424 // graph_node_map_element_t elm = NULL;
1425 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
1427 // data = xbt_new0(struct graph_node_data, sizeof(struct graph_node_data));
1429 // data->graph_id = graph_id;
1430 // node = xbt_graph_new_node(routing->route_graph, data);
1432 // elm = xbt_new0(struct graph_node_map_element, sizeof(struct graph_node_map_element));
1433 // elm->node = node;
1434 // xbt_dict_set_ext(routing->graph_node_map, (char*)(&id), sizeof(int), (xbt_set_elm_t)elm, &graph_node_map_elem_free);
1439 // static graph_node_map_element_t graph_node_map_search(int id) {
1440 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
1442 // 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));
1450 // static void route_new_dijkstra(int src_id, int dst_id, void* link) {
1451 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
1453 // xbt_node_t src = NULL;
1454 // xbt_node_t dst = NULL;
1455 // 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));
1456 // 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));
1459 // src = src_elm->node;
1462 // dst = dst_elm->node;
1464 // //add nodes if they don't exist in the graph
1465 // if(src_id == dst_id && src == NULL && dst == NULL) {
1466 // src = route_graph_new_node(src_id, -1);
1469 // if(src == NULL) {
1470 // src = route_graph_new_node(src_id, -1);
1473 // if(dst == NULL) {
1474 // dst = route_graph_new_node(dst_id, -1);
1478 // //add link as edge to graph
1479 // xbt_graph_new_edge(routing->route_graph, src, dst, link);
1483 // static void add_loopback_dijkstra(void) {
1484 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
1486 // xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1488 // xbt_node_t node = NULL;
1489 // unsigned int cursor2;
1490 // xbt_dynar_foreach(nodes, cursor2, node) {
1491 // xbt_dynar_t out_edges = xbt_graph_node_get_outedges(node);
1492 // xbt_edge_t edge = NULL;
1493 // unsigned int cursor;
1496 // xbt_dynar_foreach(out_edges, cursor, edge) {
1497 // xbt_node_t other_node = xbt_graph_edge_get_target(edge);
1498 // if(other_node == node) {
1505 // xbt_graph_new_edge(routing->route_graph, node, node, &routing->loopback);
1509 // static void routing_dijkstra_parse_end(void) {
1510 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
1512 // xbt_dict_cursor_t cursor = NULL;
1513 // char *key, *data, *end;
1514 // const char *sep = "#";
1515 // xbt_dynar_t links, keys;
1516 // char* link_name = NULL;
1517 // void* link = NULL;
1518 // xbt_node_t node = NULL;
1519 // unsigned int cursor2;
1520 // xbt_dynar_t nodes = NULL;
1521 // /* Create the topology graph */
1522 // routing->route_graph = xbt_graph_new_graph(1, NULL);
1523 // routing->graph_node_map = xbt_dict_new();
1524 // routing->last_route = xbt_dynar_new(routing->size_of_link, NULL);
1525 // if(routing->cached)
1526 // routing->route_cache = xbt_dict_new();
1529 // /* Put the routes in position */
1530 // xbt_dict_foreach(route_table, cursor, key, data) {
1532 // links = (xbt_dynar_t) data;
1533 // keys = xbt_str_split_str(key, sep);
1535 // src_id = strtol(xbt_dynar_get_as(keys, 0, char *), &end, 16);
1536 // dst_id = strtol(xbt_dynar_get_as(keys, 1, char *), &end, 16);
1537 // xbt_dynar_free(&keys);
1539 // DEBUG4("Handle %d %d (from %d hosts): %ld links",
1540 // src_id,dst_id,routing->generic_routing.host_count,xbt_dynar_length(links));
1542 // 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);
1544 // link_name = xbt_dynar_getfirst_as(links, char*);
1545 // link = xbt_dict_get_or_null(surf_network_model->resource_set, link_name);
1547 // route_new_dijkstra(src_id,dst_id,link);
1549 // THROW1(mismatch_error,0,"Link %s not found", link_name);
1553 // /* Add the loopback if needed */
1554 // add_loopback_dijkstra();
1556 // /* initialize graph indexes in nodes after graph has been built */
1557 // nodes = xbt_graph_get_nodes(routing->route_graph);
1559 // xbt_dynar_foreach(nodes, cursor2, node) {
1560 // graph_node_data_t data = xbt_graph_node_get_data(node);
1561 // data->graph_id = cursor2;
1567 // * Business methods
1569 // static xbt_dynar_t routing_dijkstra_get_route(int src_id,int dst_id) {
1571 // routing_dijkstra_t routing = (routing_dijkstra_t) used_routing;
1572 // int * pred_arr = NULL;
1573 // int src_node_id = 0;
1574 // int dst_node_id = 0;
1575 // int * nodeid = NULL;
1578 // void * link = NULL;
1579 // route_cache_element_t elm = NULL;
1580 // xbt_dynar_t nodes = xbt_graph_get_nodes(routing->route_graph);
1582 // /*Use the graph_node id mapping set to quickly find the nodes */
1583 // graph_node_map_element_t src_elm = graph_node_map_search(src_id);
1584 // graph_node_map_element_t dst_elm = graph_node_map_search(dst_id);
1585 // xbt_assert2(src_elm != NULL && dst_elm != NULL, "src %d or dst %d does not exist", src_id, dst_id);
1586 // src_node_id = ((graph_node_data_t)xbt_graph_node_get_data(src_elm->node))->graph_id;
1587 // dst_node_id = ((graph_node_data_t)xbt_graph_node_get_data(dst_elm->node))->graph_id;
1589 // if(routing->cached) {
1590 // /*check if there is a cached predecessor list avail */
1591 // elm = (route_cache_element_t)xbt_dict_get_or_null_ext(routing->route_cache, (char*)(&src_id), sizeof(int));
1594 // if(elm) { //cached mode and cache hit
1595 // pred_arr = elm->pred_arr;
1596 // } else { //not cached mode or cache miss
1597 // double * cost_arr = NULL;
1598 // xbt_heap_t pqueue = NULL;
1601 // int nr_nodes = xbt_dynar_length(nodes);
1602 // cost_arr = xbt_new0(double, nr_nodes); //link cost from src to other hosts
1603 // pred_arr = xbt_new0(int, nr_nodes); //predecessors in path from src
1604 // pqueue = xbt_heap_new(nr_nodes, free);
1607 // cost_arr[src_node_id] = 0.0;
1609 // for(i = 0; i < nr_nodes; i++) {
1610 // if(i != src_node_id) {
1611 // cost_arr[i] = DBL_MAX;
1616 // //initialize priority queue
1617 // nodeid = xbt_new0(int, 1);
1619 // xbt_heap_push(pqueue, nodeid, cost_arr[i]);
1623 // // apply dijkstra using the indexes from the graph's node array
1624 // while(xbt_heap_size(pqueue) > 0) {
1625 // int * v_id = xbt_heap_pop(pqueue);
1626 // xbt_node_t v_node = xbt_dynar_get_as(nodes, *v_id, xbt_node_t);
1627 // xbt_dynar_t out_edges = xbt_graph_node_get_outedges(v_node);
1628 // xbt_edge_t edge = NULL;
1629 // unsigned int cursor;
1631 // xbt_dynar_foreach(out_edges, cursor, edge) {
1632 // xbt_node_t u_node = xbt_graph_edge_get_target(edge);
1633 // graph_node_data_t data = xbt_graph_node_get_data(u_node);
1634 // int u_id = data->graph_id;
1635 // int cost_v_u = 1; //fixed link cost for now
1637 // if(cost_v_u + cost_arr[*v_id] < cost_arr[u_id]) {
1638 // pred_arr[u_id] = *v_id;
1639 // cost_arr[u_id] = cost_v_u + cost_arr[*v_id];
1640 // nodeid = xbt_new0(int, 1);
1642 // xbt_heap_push(pqueue, nodeid, cost_arr[u_id]);
1646 // //free item popped from pqueue
1651 // xbt_heap_free(pqueue);
1655 // //compose route path with links
1656 // xbt_dynar_reset(routing->last_route);
1658 // for(v = dst_node_id; v != src_node_id; v = pred_arr[v]) {
1659 // xbt_node_t node_pred_v = xbt_dynar_get_as(nodes, pred_arr[v], xbt_node_t);
1660 // xbt_node_t node_v = xbt_dynar_get_as(nodes, v, xbt_node_t);
1661 // xbt_edge_t edge = xbt_graph_get_edge(routing->route_graph, node_pred_v, node_v);
1663 // xbt_assert2(edge != NULL, "no route between host %d and %d", src_id, dst_id);
1665 // link = xbt_graph_edge_get_data(edge);
1666 // xbt_dynar_unshift(routing->last_route, &link);
1671 // if(routing->cached && elm == NULL) {
1672 // //add to predecessor list of the current src-host to cache
1673 // elm = xbt_new0(struct route_cache_element, sizeof(struct route_cache_element));
1674 // elm->pred_arr = pred_arr;
1675 // elm->size = size;
1676 // xbt_dict_set_ext(routing->route_cache, (char*)(&src_id), sizeof(int), (xbt_set_elm_t)elm, &route_cache_elem_free);
1679 // if(!routing->cached)
1682 // return routing->last_route;
1686 // static void routing_dijkstra_finalize(void) {
1687 // routing_dijkstra_t routing = (routing_dijkstra_t)used_routing;
1690 // xbt_graph_free_graph(routing->route_graph, &free, NULL, &free);
1691 // xbt_dict_free(&routing->graph_node_map);
1692 // if(routing->cached)
1693 // xbt_dict_free(&routing->route_cache);
1694 // xbt_dynar_free(&routing->last_route);
1695 // xbt_dict_free(&used_routing->host_id);
1701 // static xbt_dict_t routing_dijkstraboth_get_onelink_routes(void){
1702 // xbt_assert0(0,"The get_onelink_routes feature is not supported in routing model dijkstraboth");
1705 // static int routing_dijkstraboth_is_router(int id){
1706 // xbt_assert0(0,"The get_is_router feature is not supported in routing model dijkstraboth");
1712 // static void routing_model_dijkstraboth_create(size_t size_of_link,void *loopback, int cached) {
1713 // /* initialize our structure */
1714 // routing_dijkstra_t routing = xbt_new0(s_routing_dijkstra_t,1);
1715 // routing->generic_routing.name = "Dijkstra";
1716 // routing->generic_routing.host_count = 0;
1717 // routing->generic_routing.get_route = routing_dijkstra_get_route;
1718 // routing->generic_routing.get_onelink_routes = routing_dijkstraboth_get_onelink_routes;
1719 // routing->generic_routing.is_router = routing_dijkstraboth_is_router;
1720 // routing->generic_routing.finalize = routing_dijkstra_finalize;
1721 // routing->size_of_link = size_of_link;
1722 // routing->loopback = loopback;
1723 // routing->cached = cached;
1725 // /* Set it in position */
1726 // used_routing = (routing_t) routing;
1728 // /* Setup the parsing callbacks we need */
1729 // routing->generic_routing.host_id = xbt_dict_new();
1730 // surfxml_add_callback(STag_surfxml_host_cb_list, &routing_full_parse_Shost);
1731 // surfxml_add_callback(ETag_surfxml_platform_cb_list, &routing_dijkstra_parse_end);
1732 // surfxml_add_callback(STag_surfxml_route_cb_list,
1733 // &routing_full_parse_Sroute_set_endpoints);
1734 // surfxml_add_callback(ETag_surfxml_route_cb_list, &routing_full_parse_Eroute);
1735 // surfxml_add_callback(STag_surfxml_cluster_cb_list, &routing_shortest_path_parse_Scluster);
1738 // static void routing_model_dijkstra_create(size_t size_of_link,void *loopback) {
1739 // routing_model_dijkstraboth_create(size_of_link, loopback, 0);
1742 // static void routing_model_dijkstracache_create(size_t size_of_link,void *loopback) {
1743 // routing_model_dijkstraboth_create(size_of_link, loopback, 1);
1746 /* ************************************************** */
1747 /* ********** NO ROUTING **************************** */
1750 // static void routing_none_finalize(void) {
1751 // if (used_routing) {
1752 // xbt_dict_free(&used_routing->host_id);
1753 // free(used_routing);
1754 // used_routing=NULL;
1758 // static void routing_model_none_create(size_t size_of_link,void *loopback) {
1759 // routing_t routing = xbt_new0(s_routing_t,1);
1760 // INFO0("Null routing");
1761 // routing->name = "none";
1762 // routing->host_count = 0;
1763 // routing->host_id = xbt_dict_new();
1764 // routing->get_onelink_routes = NULL;
1765 // routing->is_router = NULL;
1766 // routing->get_route = NULL;
1768 // routing->finalize = routing_none_finalize;
1770 // /* Set it in position */
1771 // used_routing = (routing_t) routing;