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 "simgrid/platf_interface.h" // platform creation API internal interface
9 #include "surf_routing_private.h"
10 #include "surf/surf_routing.h"
11 #include "surf/surfxml_parse_values.h"
13 #include <pcre.h> /* regular expression library */
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;
32 new_component->bypassRoutes = xbt_dict_new_homogeneous((void (*)(void *)) generic_free_route);
36 void model_generic_finalize(AS_t as) {
37 xbt_dict_free(&as->bypassRoutes);
38 model_none_finalize(as);
41 int generic_parse_PU(AS_t as, sg_routing_edge_t elm)
43 XBT_DEBUG("Load process unit \"%s\"", elm->name);
44 xbt_dynar_push_as(as->index_network_elm,sg_routing_edge_t,elm);
45 return xbt_dynar_length(as->index_network_elm)-1;
48 int generic_parse_AS(AS_t as, sg_routing_edge_t elm)
50 XBT_DEBUG("Load Autonomous system \"%s\"", elm->name);
51 xbt_dynar_push_as(as->index_network_elm,sg_routing_edge_t,elm);
52 return xbt_dynar_length(as->index_network_elm)-1;
55 void generic_parse_bypassroute(AS_t rc, sg_platf_route_cbarg_t e_route)
57 char *src = (char*)(e_route->src);
58 char *dst = (char*)(e_route->dst);
61 XBT_DEBUG("Load bypassASroute from \"%s\" to \"%s\"", src, dst);
63 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
64 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
67 route_name = bprintf("%s#%s", src, dst);
68 xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
69 "Invalid count of links, must be greater than zero (%s,%s)",
71 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
72 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
73 src, e_route->gw_src->name, dst, e_route->gw_dst->name);
75 sg_platf_route_cbarg_t new_e_route = NULL;
77 new_e_route = generic_new_extended_route(SURF_ROUTING_RECURSIVE, e_route, 1);
79 new_e_route = generic_new_extended_route(SURF_ROUTING_BASE, e_route, 1);
81 xbt_dynar_free(&(e_route->link_list));
83 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
84 no_bypassroute_declared = 0;
88 /* ************************************************************************** */
89 /* *********************** GENERIC BUSINESS METHODS ************************* */
91 xbt_dynar_t generic_get_onelink_routes(AS_t rc) { // FIXME: kill that stub
92 xbt_die("\"generic_get_onelink_routes\" not implemented yet");
96 sg_platf_route_cbarg_t generic_get_bypassroute(AS_t rc, sg_routing_edge_t src, sg_routing_edge_t dst, double *lat)
98 // If never set a bypass route return NULL without any further computations
99 XBT_DEBUG("generic_get_bypassroute from %s to %s",src->name,dst->name);
100 if(no_bypassroute_declared)
103 sg_platf_route_cbarg_t e_route_bypass = NULL;
104 xbt_dict_t dict_bypassRoutes = rc->bypassRoutes;
106 if(dst->rc_component == rc && src->rc_component == rc ){
107 char *route_name = bprintf("%s#%s", src->name, dst->name);
108 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
110 XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
115 int index_src, index_dst;
116 xbt_dynar_t path_src = NULL;
117 xbt_dynar_t path_dst = NULL;
119 AS_t *current_src = NULL;
120 AS_t *current_dst = NULL;
122 if (src == NULL || dst == NULL)
123 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
124 src->name, dst->name, rc->name);
126 src_as = src->rc_component;
127 dst_as = dst->rc_component;
129 /* (2) find the path to the root routing component */
130 path_src = xbt_dynar_new(sizeof(AS_t), NULL);
132 while (current != NULL) {
133 xbt_dynar_push(path_src, ¤t);
134 current = current->routing_father;
136 path_dst = xbt_dynar_new(sizeof(AS_t), NULL);
138 while (current != NULL) {
139 xbt_dynar_push(path_dst, ¤t);
140 current = current->routing_father;
143 /* (3) find the common father */
144 index_src = path_src->used - 1;
145 index_dst = path_dst->used - 1;
146 current_src = xbt_dynar_get_ptr(path_src, index_src);
147 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
148 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
149 xbt_dynar_pop_ptr(path_src);
150 xbt_dynar_pop_ptr(path_dst);
153 current_src = xbt_dynar_get_ptr(path_src, index_src);
154 current_dst = xbt_dynar_get_ptr(path_dst, index_dst);
157 int max_index_src = path_src->used - 1;
158 int max_index_dst = path_dst->used - 1;
160 int max_index = max(max_index_src, max_index_dst);
163 for (max = 0; max <= max_index; max++) {
164 for (i = 0; i < max; i++) {
165 if (i <= max_index_src && max <= max_index_dst) {
166 char *route_name = bprintf("%s#%s",
168 (xbt_dynar_get_ptr(path_src, i)))->name,
170 (xbt_dynar_get_ptr(path_dst, max)))->name);
171 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
172 xbt_free(route_name);
176 if (max <= max_index_src && i <= max_index_dst) {
177 char *route_name = bprintf("%s#%s",
179 (xbt_dynar_get_ptr(path_src, max)))->name,
181 (xbt_dynar_get_ptr(path_dst, i)))->name);
182 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
183 xbt_free(route_name);
192 if (max <= max_index_src && max <= max_index_dst) {
193 char *route_name = bprintf("%s#%s",
195 (xbt_dynar_get_ptr(path_src, max)))->name,
197 (xbt_dynar_get_ptr(path_dst, max)))->name);
198 e_route_bypass = xbt_dict_get_or_null(dict_bypassRoutes, route_name);
199 xbt_free(route_name);
205 xbt_dynar_free(&path_src);
206 xbt_dynar_free(&path_dst);
209 sg_platf_route_cbarg_t new_e_route = NULL;
210 if (e_route_bypass) {
212 unsigned int cpt = 0;
213 new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
214 new_e_route->gw_src = e_route_bypass->gw_src;
215 new_e_route->gw_dst = e_route_bypass->gw_dst;
216 new_e_route->link_list =
217 xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
218 xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
219 xbt_dynar_push(new_e_route->link_list, &link);
221 *lat += surf_network_model->extension.network.get_link_latency(link);
228 /* ************************************************************************** */
229 /* ************************* GENERIC AUX FUNCTIONS ************************** */
230 /* change a route containing link names into a route containing link entities */
231 sg_platf_route_cbarg_t
232 generic_new_extended_route(e_surf_routing_hierarchy_t hierarchy,
233 sg_platf_route_cbarg_t routearg, int change_order) {
235 sg_platf_route_cbarg_t result;
239 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
240 result->link_list = xbt_dynar_new(sizeof(sg_routing_link_t), NULL);
242 xbt_assert(hierarchy == SURF_ROUTING_BASE
243 || hierarchy == SURF_ROUTING_RECURSIVE,
244 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
246 if (hierarchy == SURF_ROUTING_RECURSIVE) {
248 xbt_assert(routearg->gw_src && routearg->gw_dst,
249 "NULL is obviously a bad gateway");
251 /* remeber not erase the gateway names */
252 result->gw_src = routearg->gw_src;
253 result->gw_dst = routearg->gw_dst;
256 xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
258 void *link = xbt_lib_get_or_null(link_lib, link_name, SURF_LINK_LEVEL);
261 xbt_dynar_push(result->link_list, &link);
263 xbt_dynar_unshift(result->link_list, &link);
265 THROWF(mismatch_error, 0, "Link %s not found", link_name);
271 void generic_free_route(sg_platf_route_cbarg_t route)
274 xbt_dynar_free(&route->link_list);
279 static AS_t generic_as_exist(AS_t find_from,
282 //return to_find; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
283 xbt_dict_cursor_t cursor = NULL;
287 xbt_dict_foreach(find_from->routing_sons, cursor, key, elem) {
288 if (to_find == elem || generic_as_exist(elem, to_find)) {
299 generic_autonomous_system_exist(AS_t rc, char *element)
301 //return rc; // FIXME: BYPASSERROR OF FOREACH WITH BREAK
302 AS_t element_as, result, elem;
303 xbt_dict_cursor_t cursor = NULL;
305 element_as = ((sg_routing_edge_t)
306 xbt_lib_get_or_null(as_router_lib, element,
307 ROUTING_ASR_LEVEL))->rc_component;
308 result = ((AS_t) - 1);
309 if (element_as != rc)
310 result = generic_as_exist(rc, element_as);
314 xbt_dict_foreach(element_as->routing_sons, cursor, key, elem) {
315 found = !strcmp(elem->name, element);
326 generic_processing_units_exist(AS_t rc, char *element)
329 element_as = ((sg_routing_edge_t)
330 xbt_lib_get_or_null(host_lib,
331 element, ROUTING_HOST_LEVEL))->rc_component;
332 if (element_as == rc)
334 return generic_as_exist(rc, element_as);
337 void generic_src_dst_check(AS_t rc, sg_routing_edge_t src,
338 sg_routing_edge_t dst)
341 sg_routing_edge_t src_data = src;
342 sg_routing_edge_t dst_data = dst;
344 if (src_data == NULL || dst_data == NULL)
345 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
351 (src_data)->rc_component;
353 (dst_data)->rc_component;
355 if (src_as != dst_as)
356 xbt_die("The src(%s in %s) and dst(%s in %s) are in differents AS",
357 src->name, src_as->name,
358 dst->name, dst_as->name);
362 ("The routing component of src'%s' and dst'%s' is not the same as the network elements belong (%s?=%s?=%s)",