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()
50 /* ************************************************************************** */
51 /* *********************** GENERIC BUSINESS METHODS ************************* */
53 static const char *instr_node_name(xbt_node_t node)
55 void *data = xbt_graph_node_get_data(node);
56 char *str = (char *) data;
60 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
63 xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
67 ret = xbt_graph_new_node(graph, xbt_strdup(name));
68 xbt_dict_set(nodes, name, ret, NULL);
72 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
77 const char *sn = instr_node_name(s);
78 const char *dn = instr_node_name(d);
79 int len = strlen(sn) + strlen(dn) + 1;
80 char *name = (char *) xbt_malloc(len * sizeof(char));
83 snprintf(name, len, "%s%s", sn, dn);
84 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
86 snprintf(name, len, "%s%s", dn, sn);
87 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
91 ret = xbt_graph_new_edge(graph, s, d, NULL);
92 xbt_dict_set(edges, name, ret, NULL);
101 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
104 int table_size = xbt_dynar_length(vertices_);
107 for (src = 0; src < table_size; src++) {
109 xbt_dynar_get_as(vertices_, src, NetCard*);
110 for (dst = 0; dst < table_size; dst++) {
114 xbt_dynar_get_as(vertices_, dst, NetCard*);
116 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
117 route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
119 getRouteAndLatency(my_src, my_dst, route, NULL);
121 XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
126 xbt_node_t current, previous;
127 const char *previous_name, *current_name;
130 previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
131 previous_name = route->gw_src->name();
133 previous = new_xbt_graph_node(graph, my_src->name(), nodes);
134 previous_name = my_src->name();
137 xbt_dynar_foreach(route->link_list, cpt, link) {
138 const char *link_name = static_cast<simgrid::surf::Resource*>(
140 current = new_xbt_graph_node(graph, link_name, nodes);
141 current_name = link_name;
142 new_xbt_graph_edge(graph, previous, current, edges);
143 XBT_DEBUG (" %s -> %s", previous_name, current_name);
145 previous_name = current_name;
149 current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
150 current_name = route->gw_dst->name();
152 current = new_xbt_graph_node(graph, my_dst->name(), nodes);
153 current_name = my_dst->name();
155 new_xbt_graph_edge(graph, previous, current, edges);
156 XBT_DEBUG (" %s -> %s", previous_name, current_name);
158 xbt_dynar_free (&(route->link_list));
165 /* ************************************************************************** */
166 /* ************************* GENERIC AUX FUNCTIONS ************************** */
167 /* change a route containing link names into a route containing link entities */
168 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
169 sg_platf_route_cbarg_t routearg, int change_order) {
171 sg_platf_route_cbarg_t result;
175 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
176 result->link_list = xbt_dynar_new(sizeof(Link*), NULL);
178 xbt_assert(hierarchy == SURF_ROUTING_BASE
179 || hierarchy == SURF_ROUTING_RECURSIVE,
180 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
182 if (hierarchy == SURF_ROUTING_RECURSIVE) {
184 xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
186 /* remember not erase the gateway names */
187 result->gw_src = routearg->gw_src;
188 result->gw_dst = routearg->gw_dst;
191 xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
193 Link *link = Link::byName(link_name);
196 xbt_dynar_push(result->link_list, &link);
198 xbt_dynar_unshift(result->link_list, &link);
200 THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
206 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
208 xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
209 xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
211 As *src_as = src->containingAS();
212 As *dst_as = dst->containingAS();
214 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.",
215 src->name(), src_as->name_, dst->name(), dst_as->name_);
217 xbt_assert(this == dst_as,
218 "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
219 src->name(), dst->name(), src_as->name_, dst_as->name_, name_);
221 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
222 const char *srcName = route->src;
223 const char *dstName = route->dst;
224 NetCard *src = sg_netcard_by_name_or_null(srcName);
225 NetCard *dst = sg_netcard_by_name_or_null(dstName);
227 if(!route->gw_dst && !route->gw_src) {
228 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
229 xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
230 xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
231 xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s and %s) forbidden.", srcName, dstName);
232 xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_HOST || src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
233 "When defining a route, src must be an host or a router but '%s' is not. Did you meant to have an ASroute?", srcName);
234 xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
235 "When defining a route, dst must be an host or a router but '%s' is not. Did you meant to have an ASroute?", dstName);
237 XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
238 xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_AS,
239 "When defining an ASroute, src must be an AS but '%s' is not", srcName);
240 xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_AS,
241 "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
243 xbt_assert(route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
244 "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
245 xbt_assert(route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
246 "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
248 xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
250 xbt_assert(src, "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(), srcName);
252 xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
253 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
254 xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s@%s and %s@%s) forbidden.",
255 srcName,route->gw_src->name(), dstName,route->gw_dst->name());