1 /* Copyright (c) 2009, 2010, 2011. 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. */
7 #include <pcre.h> /* regular expression library */
9 #include "simgrid/platf_interface.h" // platform creation API internal interface
11 #include "surf_routing_private.h"
12 #include "surf/surf_routing.h"
13 #include "surf/surfxml_parse_values.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
17 static int no_bypassroute_declared = 1;
19 AS_t model_generic_create_sized(size_t childsize) {
20 AS_t new_component = model_none_create_sized(childsize);
22 new_component->parse_PU = generic_parse_PU;
23 new_component->parse_AS = generic_parse_AS;
24 new_component->parse_route = NULL;
25 new_component->parse_ASroute = NULL;
26 new_component->parse_bypassroute = generic_parse_bypassroute;
27 new_component->get_route_and_latency = NULL;
28 new_component->get_onelink_routes = NULL;
29 new_component->get_bypass_route =
30 generic_get_bypassroute;
31 new_component->finalize = model_generic_finalize;
33 new_component->to_index = xbt_dict_new_homogeneous(xbt_free);
34 new_component->bypassRoutes = xbt_dict_new_homogeneous((void (*)(void *)) generic_free_route);
38 void model_generic_finalize(AS_t as) {
39 xbt_dict_free(&as->to_index);
40 xbt_dict_free(&as->bypassRoutes);
41 model_none_finalize(as);
44 void generic_parse_PU(AS_t as, const char *name)
46 XBT_DEBUG("Load process unit \"%s\"", name);
47 int *id = xbt_new0(int, 1);
49 _to_index = as->to_index;
50 *id = xbt_dict_length(_to_index);
51 xbt_dict_set(_to_index, name, id, NULL);
54 void generic_parse_AS(AS_t as, const char *name)
56 XBT_DEBUG("Load Autonomous system \"%s\"", name);
57 int *id = xbt_new0(int, 1);
59 _to_index = as->to_index;
60 *id = xbt_dict_length(_to_index);
61 xbt_dict_set(_to_index, name, id, NULL);
64 void generic_parse_bypassroute(AS_t rc,
65 const char *src, const char *dst,
68 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
69 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
72 route_name = bprintf("%s#%s", src, dst);
73 xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
74 "Invalid count of links, must be greater than zero (%s,%s)",
76 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
77 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
78 src, e_route->src_gateway, dst, e_route->dst_gateway);
81 generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 0);
82 xbt_dynar_free(&(e_route->link_list));
85 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
86 no_bypassroute_declared = 0;
90 /* ************************************************************************** */
91 /* *********************** GENERIC BUSINESS METHODS ************************* */
93 xbt_dynar_t generic_get_onelink_routes(AS_t rc) { // FIXME: kill that stub
94 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
98 route_t generic_get_bypassroute(AS_t rc, const char *src, const char *dst)
100 // If never set a bypass route return NULL without any further computations
101 if(no_bypassroute_declared)
104 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
106 int index_src, index_dst;
107 xbt_dynar_t path_src = NULL;
108 xbt_dynar_t path_dst = NULL;
110 AS_t *current_src = NULL;
111 AS_t *current_dst = NULL;
113 /* (1) find the as where the src and dst are located */
114 void *src_data = xbt_lib_get_or_null(host_lib, src, ROUTING_HOST_LEVEL);
115 void *dst_data = xbt_lib_get_or_null(host_lib, dst, ROUTING_HOST_LEVEL);
117 src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
119 dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
121 if (src_data == NULL || dst_data == NULL)
122 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
125 src_as = ((network_element_info_t) src_data)->rc_component;
126 dst_as = ((network_element_info_t) dst_data)->rc_component;
128 /* (2) find the path to the root routing component */
129 path_src = xbt_dynar_new(sizeof(AS_t), NULL);
131 while (current != NULL) {
132 xbt_dynar_push(path_src, ¤t);
133 current = current->routing_father;
135 path_dst = xbt_dynar_new(sizeof(AS_t), NULL);
137 while (current != NULL) {
138 xbt_dynar_push(path_dst, ¤t);
139 current = current->routing_father;
142 /* (3) find the common father */
143 index_src = path_src->used - 1;
144 index_dst = path_dst->used - 1;
145 current_src = xbt_dynar_get_ptr(path_src, index_src);
146 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
147 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
148 xbt_dynar_pop_ptr(path_src);
149 xbt_dynar_pop_ptr(path_dst);
152 current_src = xbt_dynar_get_ptr(path_src, index_src);
153 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
156 int max_index_src = path_src->used - 1;
157 int max_index_dst = path_dst->used - 1;
159 int max_index = max(max_index_src, max_index_dst);
162 route_t e_route_bypass = NULL;
164 for (max = 0; max <= max_index; max++) {
165 for (i = 0; i < max; i++) {
166 if (i <= max_index_src && max <= max_index_dst) {
167 char *route_name = bprintf("%s#%s",
169 (xbt_dynar_get_ptr(path_src, i)))->name,
171 (xbt_dynar_get_ptr(path_dst, max)))->name);
172 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
173 xbt_free(route_name);
177 if (max <= max_index_src && i <= max_index_dst) {
178 char *route_name = bprintf("%s#%s",
180 (xbt_dynar_get_ptr(path_src, max)))->name,
182 (xbt_dynar_get_ptr(path_dst, i)))->name);
183 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
184 xbt_free(route_name);
193 if (max <= max_index_src && max <= max_index_dst) {
194 char *route_name = bprintf("%s#%s",
196 (xbt_dynar_get_ptr(path_src, max)))->name,
198 (xbt_dynar_get_ptr(path_dst, max)))->name);
199 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
200 xbt_free(route_name);
206 xbt_dynar_free(&path_src);
207 xbt_dynar_free(&path_dst);
209 route_t new_e_route = NULL;
211 if (e_route_bypass) {
213 unsigned int cpt = 0;
214 new_e_route = xbt_new0(s_route_t, 1);
215 new_e_route->src_gateway = xbt_strdup(e_route_bypass->src_gateway);
216 new_e_route->dst_gateway = xbt_strdup(e_route_bypass->dst_gateway);
217 new_e_route->link_list =
218 xbt_dynar_new(global_routing->size_of_link, NULL);
219 xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
220 xbt_dynar_push(new_e_route->link_list, &link);
227 /* ************************************************************************** */
228 /* ************************* GENERIC AUX FUNCTIONS ************************** */
231 generic_new_route(e_surf_routing_hierarchy_t hierarchy, void *data, int order)
237 xbt_dynar_t links = NULL, links_id = NULL;
239 new_route = xbt_new0(s_route_t, 1);
240 new_route->link_list = xbt_dynar_new(global_routing->size_of_link, NULL);
242 xbt_assert(hierarchy == SURF_ROUTING_BASE,
243 "the hierarchy type is not SURF_ROUTING_BASE");
245 links = ((route_t) data)->link_list;
248 links_id = new_route->link_list;
250 xbt_dynar_foreach(links, cpt, link_name) {
252 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
255 xbt_dynar_push(links_id, &link);
257 xbt_dynar_unshift(links_id, &link);
259 THROWF(mismatch_error, 0, "Link %s not found", link_name);
266 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
267 void *data, int order)
271 route_t e_route, new_e_route;
274 xbt_dynar_t links = NULL, links_id = NULL;
276 new_e_route = xbt_new0(s_route_t, 1);
277 new_e_route->link_list = xbt_dynar_new(global_routing->size_of_link, NULL);
279 xbt_assert(hierarchy == SURF_ROUTING_BASE
280 || hierarchy == SURF_ROUTING_RECURSIVE,
281 "the hierarchy type is not defined");
283 if (hierarchy == SURF_ROUTING_BASE) {
285 route = (route_t) data;
286 links = route->link_list;
288 } else if (hierarchy == SURF_ROUTING_RECURSIVE) {
290 e_route = (route_t) data;
291 xbt_assert(e_route->src_gateway
292 && e_route->dst_gateway, "bad gateway, is null");
293 links = e_route->link_list;
295 /* remeber not erase the gateway names */
296 new_e_route->src_gateway = strdup(e_route->src_gateway);
297 new_e_route->dst_gateway = strdup(e_route->dst_gateway);
300 links_id = new_e_route->link_list;
302 xbt_dynar_foreach(links, cpt, link_name) {
304 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
307 xbt_dynar_push(links_id, &link);
309 xbt_dynar_unshift(links_id, &link);
311 THROWF(mismatch_error, 0, "Link %s not found", link_name);
317 void generic_free_route(route_t route)
320 xbt_dynar_free(&route->link_list);
321 xbt_free(route->src_gateway);
322 xbt_free(route->dst_gateway);
327 static AS_t generic_as_exist(AS_t find_from,
330 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
331 xbt_dict_cursor_t cursor = NULL;
335 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
336 if (to_find == elem || generic_as_exist(elem, to_find)) {
347 generic_autonomous_system_exist(AS_t rc, char *element)
349 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
350 AS_t element_as, result, elem;
351 xbt_dict_cursor_t cursor = NULL;
353 element_as = ((network_element_info_t)
354 xbt_lib_get_or_null(as_router_lib, element,
355 ROUTING_ASR_LEVEL))->rc_component;
356 result = ((AS_t) - 1);
357 if (element_as != rc)
358 result = generic_as_exist(rc, element_as);
362 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
363 found = !strcmp(elem->name, element);
374 generic_processing_units_exist(AS_t rc, char *element)
377 element_as = ((network_element_info_t)
378 xbt_lib_get_or_null(host_lib,
379 element, ROUTING_HOST_LEVEL))->rc_component;
380 if (element_as == rc)
382 return generic_as_exist(rc, element_as);
385 void generic_src_dst_check(AS_t rc, const char *src,
389 void *src_data = xbt_lib_get_or_null(host_lib, src, ROUTING_HOST_LEVEL);
390 void *dst_data = xbt_lib_get_or_null(host_lib, dst, ROUTING_HOST_LEVEL);
392 src_data = xbt_lib_get_or_null(as_router_lib, src, ROUTING_ASR_LEVEL);
394 dst_data = xbt_lib_get_or_null(as_router_lib, dst, ROUTING_ASR_LEVEL);
396 if (src_data == NULL || dst_data == NULL)
397 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
401 ((network_element_info_t) src_data)->rc_component;
403 ((network_element_info_t) dst_data)->rc_component;
405 if (src_as != dst_as)
406 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
407 src, src_as->name, dst, dst_as->name);
410 ("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",
411 src, dst, src_as->name, dst_as->name, rc->name);