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 "surf_routing_private.hpp"
20 #include "network_interface.hpp"
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
24 void routing_route_free(sg_platf_route_cbarg_t route)
27 xbt_dynar_free(&route->link_list);
35 AsRoutedGraph::AsRoutedGraph(const char*name)
40 AsRoutedGraph::~AsRoutedGraph()
48 /* ************************************************************************** */
49 /* *********************** GENERIC BUSINESS METHODS ************************* */
51 static const char *instr_node_name(xbt_node_t node)
53 void *data = xbt_graph_node_get_data(node);
54 char *str = (char *) data;
58 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
61 xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
65 ret = xbt_graph_new_node(graph, xbt_strdup(name));
66 xbt_dict_set(nodes, name, ret, NULL);
70 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
75 const char *sn = instr_node_name(s);
76 const char *dn = instr_node_name(d);
77 int len = strlen(sn) + strlen(dn) + 1;
78 char *name = (char *) xbt_malloc(len * sizeof(char));
81 snprintf(name, len, "%s%s", sn, dn);
82 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
84 snprintf(name, len, "%s%s", dn, sn);
85 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
89 ret = xbt_graph_new_edge(graph, s, d, NULL);
90 xbt_dict_set(edges, name, ret, NULL);
99 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
102 int table_size = xbt_dynar_length(vertices_);
105 for (src = 0; src < table_size; src++) {
107 xbt_dynar_get_as(vertices_, src, NetCard*);
108 for (dst = 0; dst < table_size; dst++) {
112 xbt_dynar_get_as(vertices_, dst, NetCard*);
114 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
115 route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
117 getRouteAndLatency(my_src, my_dst, route, NULL);
119 XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
124 xbt_node_t current, previous;
125 const char *previous_name, *current_name;
128 previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
129 previous_name = route->gw_src->name();
131 previous = new_xbt_graph_node(graph, my_src->name(), nodes);
132 previous_name = my_src->name();
135 xbt_dynar_foreach(route->link_list, cpt, link) {
136 const char *link_name = static_cast<simgrid::surf::Resource*>(
138 current = new_xbt_graph_node(graph, link_name, nodes);
139 current_name = link_name;
140 new_xbt_graph_edge(graph, previous, current, edges);
141 XBT_DEBUG (" %s -> %s", previous_name, current_name);
143 previous_name = current_name;
147 current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
148 current_name = route->gw_dst->name();
150 current = new_xbt_graph_node(graph, my_dst->name(), nodes);
151 current_name = my_dst->name();
153 new_xbt_graph_edge(graph, previous, current, edges);
154 XBT_DEBUG (" %s -> %s", previous_name, current_name);
156 xbt_dynar_free (&(route->link_list));
163 /* ************************************************************************** */
164 /* ************************* GENERIC AUX FUNCTIONS ************************** */
165 /* change a route containing link names into a route containing link entities */
166 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
167 sg_platf_route_cbarg_t routearg, int change_order) {
169 sg_platf_route_cbarg_t result;
173 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
174 result->link_list = xbt_dynar_new(sizeof(Link*), NULL);
176 xbt_assert(hierarchy == SURF_ROUTING_BASE
177 || hierarchy == SURF_ROUTING_RECURSIVE,
178 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
180 if (hierarchy == SURF_ROUTING_RECURSIVE) {
182 xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
184 /* remember not erase the gateway names */
185 result->gw_src = routearg->gw_src;
186 result->gw_dst = routearg->gw_dst;
189 xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
191 Link *link = Link::byName(link_name);
194 xbt_dynar_push(result->link_list, &link);
196 xbt_dynar_unshift(result->link_list, &link);
198 THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
204 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
206 xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
207 xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
209 As *src_as = src->containingAS();
210 As *dst_as = dst->containingAS();
212 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.",
213 src->name(), src_as->name_, dst->name(), dst_as->name_);
215 xbt_assert(this == dst_as,
216 "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
217 src->name(), dst->name(), src_as->name_, dst_as->name_, name_);
219 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
220 const char *srcName = route->src;
221 const char *dstName = route->dst;
222 NetCard *src = sg_netcard_by_name_or_null(srcName);
223 NetCard *dst = sg_netcard_by_name_or_null(dstName);
225 if(!route->gw_dst && !route->gw_src) {
226 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
227 xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
228 xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
229 xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s and %s) forbidden.", srcName, dstName);
230 xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_HOST || src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
231 "When defining a route, src must be an host or a router but '%s' is not. Did you meant to have an ASroute?", srcName);
232 xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
233 "When defining a route, dst must be an host or a router but '%s' is not. Did you meant to have an ASroute?", dstName);
235 XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
236 xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_AS,
237 "When defining an ASroute, src must be an AS but '%s' is not", srcName);
238 xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_AS,
239 "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
241 xbt_assert(route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
242 "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
243 xbt_assert(route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
244 "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
246 xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
248 xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
249 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
250 xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
251 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
252 xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s@%s and %s@%s) forbidden.",
253 srcName,route->gw_src->name(), dstName,route->gw_dst->name());