1 /* Copyright (c) 2009-2015. 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 "surf_routing_RoutedGraph.hpp"
15 #include <xbt/sysdep.h>
16 #include <xbt/dynar.h>
17 #include <xbt/graph.h>
19 #include "simgrid/platf_interface.h" // platform creation API internal interface
21 #include "surf_routing_private.hpp"
22 #include "network_interface.hpp"
24 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
26 void routing_route_free(sg_platf_route_cbarg_t route)
29 xbt_dynar_free(&route->link_list);
37 AsRoutedGraph::AsRoutedGraph(const char*name)
42 AsRoutedGraph::~AsRoutedGraph()
44 xbt_dict_free(&bypassRoutes_);
47 void AsRoutedGraph::addBypassRoute(sg_platf_route_cbarg_t e_route)
49 char *src = (char*)(e_route->src);
50 char *dst = (char*)(e_route->dst);
52 if(bypassRoutes_ == nullptr)
53 bypassRoutes_ = xbt_dict_new_homogeneous((void (*)(void *)) routing_route_free);
56 XBT_DEBUG("Load bypassASroute from \"%s\" to \"%s\"", src, dst);
58 XBT_DEBUG("Load bypassRoute from \"%s\" to \"%s\"", src, dst);
59 xbt_dict_t dict_bypassRoutes = bypassRoutes_;
62 route_name = bprintf("%s#%s", src, dst);
63 xbt_assert(!xbt_dynar_is_empty(e_route->link_list),
64 "Invalid count of links, must be greater than zero (%s,%s)",
66 xbt_assert(!xbt_dict_get_or_null(dict_bypassRoutes, route_name),
67 "The bypass route between \"%s\"(\"%s\") and \"%s\"(\"%s\") already exists",
68 src, e_route->gw_src->name(), dst, e_route->gw_dst->name());
70 sg_platf_route_cbarg_t new_e_route = NULL;
72 new_e_route = newExtendedRoute(SURF_ROUTING_RECURSIVE, e_route, 1);
74 new_e_route = newExtendedRoute(SURF_ROUTING_BASE, e_route, 1);
76 xbt_dynar_free(&(e_route->link_list));
78 xbt_dict_set(dict_bypassRoutes, route_name, new_e_route, NULL);
85 /* ************************************************************************** */
86 /* *********************** GENERIC BUSINESS METHODS ************************* */
88 static const char *instr_node_name(xbt_node_t node)
90 void *data = xbt_graph_node_get_data(node);
91 char *str = (char *) data;
95 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
98 xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
102 ret = xbt_graph_new_node(graph, xbt_strdup(name));
103 xbt_dict_set(nodes, name, ret, NULL);
107 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
112 const char *sn = instr_node_name(s);
113 const char *dn = instr_node_name(d);
114 int len = strlen(sn) + strlen(dn) + 1;
115 char *name = (char *) xbt_malloc(len * sizeof(char));
118 snprintf(name, len, "%s%s", sn, dn);
119 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
121 snprintf(name, len, "%s%s", dn, sn);
122 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
126 ret = xbt_graph_new_edge(graph, s, d, NULL);
127 xbt_dict_set(edges, name, ret, NULL);
136 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
139 int table_size = xbt_dynar_length(vertices_);
142 for (src = 0; src < table_size; src++) {
144 xbt_dynar_get_as(vertices_, src, NetCard*);
145 for (dst = 0; dst < table_size; dst++) {
149 xbt_dynar_get_as(vertices_, dst, NetCard*);
151 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
152 route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
154 getRouteAndLatency(my_src, my_dst, route, NULL);
156 XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
161 xbt_node_t current, previous;
162 const char *previous_name, *current_name;
165 previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
166 previous_name = route->gw_src->name();
168 previous = new_xbt_graph_node(graph, my_src->name(), nodes);
169 previous_name = my_src->name();
172 xbt_dynar_foreach(route->link_list, cpt, link) {
173 const char *link_name = static_cast<simgrid::surf::Resource*>(
175 current = new_xbt_graph_node(graph, link_name, nodes);
176 current_name = link_name;
177 new_xbt_graph_edge(graph, previous, current, edges);
178 XBT_DEBUG (" %s -> %s", previous_name, current_name);
180 previous_name = current_name;
184 current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
185 current_name = route->gw_dst->name();
187 current = new_xbt_graph_node(graph, my_dst->name(), nodes);
188 current_name = my_dst->name();
190 new_xbt_graph_edge(graph, previous, current, edges);
191 XBT_DEBUG (" %s -> %s", previous_name, current_name);
193 xbt_dynar_free (&(route->link_list));
199 sg_platf_route_cbarg_t AsRoutedGraph::getBypassRoute(NetCard *src, NetCard *dst, double *lat)
201 // If never set a bypass route return NULL without any further computations
202 XBT_DEBUG("generic_get_bypassroute from %s to %s", src->name(), dst->name());
203 if (bypassRoutes_ == nullptr)
206 sg_platf_route_cbarg_t e_route_bypass = NULL;
208 if(dst->containingAS() == this && src->containingAS() == this ){
209 char *route_name = bprintf("%s#%s", src->name(), dst->name());
210 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(bypassRoutes_, route_name);
212 XBT_DEBUG("Find bypass route with %ld links",xbt_dynar_length(e_route_bypass->link_list));
217 int index_src, index_dst;
218 xbt_dynar_t path_src = NULL;
219 xbt_dynar_t path_dst = NULL;
221 As **current_src = NULL;
222 As **current_dst = NULL;
224 if (src == NULL || dst == NULL)
225 xbt_die("Ask for route \"from\"(%s) or \"to\"(%s) no found at AS \"%s\"",
226 src ? src->name() : "(null)",
227 dst ? dst->name() : "(null)", name_);
229 src_as = src->containingAS();
230 dst_as = dst->containingAS();
232 /* (2) find the path to the root routing component */
233 path_src = xbt_dynar_new(sizeof(As*), NULL);
235 while (current != NULL) {
236 xbt_dynar_push(path_src, ¤t);
237 current = current->father_;
239 path_dst = xbt_dynar_new(sizeof(As*), NULL);
241 while (current != NULL) {
242 xbt_dynar_push(path_dst, ¤t);
243 current = current->father_;
246 /* (3) find the common father */
247 index_src = path_src->used - 1;
248 index_dst = path_dst->used - 1;
249 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
250 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
251 while (index_src >= 0 && index_dst >= 0 && *current_src == *current_dst) {
252 xbt_dynar_pop_ptr(path_src);
253 xbt_dynar_pop_ptr(path_dst);
256 current_src = (As **) xbt_dynar_get_ptr(path_src, index_src);
257 current_dst = (As **) xbt_dynar_get_ptr(path_dst, index_dst);
260 int max_index_src = path_src->used - 1;
261 int max_index_dst = path_dst->used - 1;
263 int max_index = std::max(max_index_src, max_index_dst);
265 for (int max = 0; max <= max_index; max++) {
266 for (int i = 0; i < max; i++) {
267 if (i <= max_index_src && max <= max_index_dst) {
268 char *route_name = bprintf("%s#%s",
269 (*(As **) (xbt_dynar_get_ptr(path_src, i)))->name_,
270 (*(As **) (xbt_dynar_get_ptr(path_dst, max)))->name_);
271 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(bypassRoutes_, route_name);
272 xbt_free(route_name);
276 if (max <= max_index_src && i <= max_index_dst) {
277 char *route_name = bprintf("%s#%s",
278 (*(As **) (xbt_dynar_get_ptr(path_src, max)))->name_,
279 (*(As **) (xbt_dynar_get_ptr(path_dst, i)))->name_);
280 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(bypassRoutes_, route_name);
281 xbt_free(route_name);
290 if (max <= max_index_src && max <= max_index_dst) {
291 char *route_name = bprintf("%s#%s",
292 (*(As **) (xbt_dynar_get_ptr(path_src, max)))->name_,
293 (*(As **) (xbt_dynar_get_ptr(path_dst, max)))->name_);
294 e_route_bypass = (sg_platf_route_cbarg_t) xbt_dict_get_or_null(bypassRoutes_, route_name);
295 xbt_free(route_name);
301 xbt_dynar_free(&path_src);
302 xbt_dynar_free(&path_dst);
305 sg_platf_route_cbarg_t new_e_route = NULL;
306 if (e_route_bypass) {
308 unsigned int cpt = 0;
309 new_e_route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
310 new_e_route->gw_src = e_route_bypass->gw_src;
311 new_e_route->gw_dst = e_route_bypass->gw_dst;
312 new_e_route->link_list =
313 xbt_dynar_new(sizeof(Link*), NULL);
314 xbt_dynar_foreach(e_route_bypass->link_list, cpt, link) {
315 xbt_dynar_push(new_e_route->link_list, &link);
317 *lat += link->getLatency();
324 /* ************************************************************************** */
325 /* ************************* GENERIC AUX FUNCTIONS ************************** */
326 /* change a route containing link names into a route containing link entities */
327 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
328 sg_platf_route_cbarg_t routearg, int change_order) {
330 sg_platf_route_cbarg_t result;
334 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
335 result->link_list = xbt_dynar_new(sizeof(Link*), NULL);
337 xbt_assert(hierarchy == SURF_ROUTING_BASE
338 || hierarchy == SURF_ROUTING_RECURSIVE,
339 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
341 if (hierarchy == SURF_ROUTING_RECURSIVE) {
343 xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
345 /* remember not erase the gateway names */
346 result->gw_src = routearg->gw_src;
347 result->gw_dst = routearg->gw_dst;
350 xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
352 Link *link = Link::byName(link_name);
355 xbt_dynar_push(result->link_list, &link);
357 xbt_dynar_unshift(result->link_list, &link);
359 THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
365 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
367 xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
368 xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
370 As *src_as = src->containingAS();
371 As *dst_as = dst->containingAS();
373 xbt_assert(src_as == dst_as, "Internal error: %s@%s and %s@%s are not in the same AS as expected. Please report that bug.",
374 src->name(), src_as->name_, dst->name(), dst_as->name_);
376 xbt_assert(this == dst_as,
377 "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
378 src->name(), dst->name(), src_as->name_, dst_as->name_, name_);
380 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
381 const char *srcName = route->src;
382 const char *dstName = route->dst;
383 NetCard *src = sg_netcard_by_name_or_null(srcName);
384 NetCard *dst = sg_netcard_by_name_or_null(dstName);
386 if(!route->gw_dst && !route->gw_src) {
387 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
388 xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
389 xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
390 xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s and %s) forbidden.", srcName, dstName);
391 xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_HOST || src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
392 "When defining a route, src must be an host or a router but '%s' is not. Did you meant to have an ASroute?", srcName);
393 xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
394 "When defining a route, dst must be an host or a router but '%s' is not. Did you meant to have an ASroute?", dstName);
396 XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
397 xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_AS,
398 "When defining an ASroute, src must be an AS but '%s' is not", srcName);
399 xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_AS,
400 "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
402 xbt_assert(route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
403 "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
404 xbt_assert(route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
405 "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
407 xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
409 xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
410 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
411 xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
412 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
413 xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s@%s and %s@%s) forbidden.",
414 srcName,route->gw_src->name(), dstName,route->gw_dst->name());