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 "network_interface.hpp"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
23 void routing_route_free(sg_platf_route_cbarg_t route)
26 xbt_dynar_free(&route->link_list);
34 AsRoutedGraph::AsRoutedGraph(const char*name)
39 AsRoutedGraph::~AsRoutedGraph()
47 /* ************************************************************************** */
48 /* *********************** GENERIC BUSINESS METHODS ************************* */
50 static const char *instr_node_name(xbt_node_t node)
52 void *data = xbt_graph_node_get_data(node);
53 char *str = (char *) data;
57 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char *name,
60 xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
64 ret = xbt_graph_new_node(graph, xbt_strdup(name));
65 xbt_dict_set(nodes, name, ret, NULL);
69 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d,
74 const char *sn = instr_node_name(s);
75 const char *dn = instr_node_name(d);
76 int len = strlen(sn) + strlen(dn) + 1;
77 char *name = (char *) xbt_malloc(len * sizeof(char));
80 snprintf(name, len, "%s%s", sn, dn);
81 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
83 snprintf(name, len, "%s%s", dn, sn);
84 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
88 ret = xbt_graph_new_edge(graph, s, d, NULL);
89 xbt_dict_set(edges, name, ret, NULL);
98 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
101 int table_size = xbt_dynar_length(vertices_);
104 for (src = 0; src < table_size; src++) {
106 xbt_dynar_get_as(vertices_, src, NetCard*);
107 for (dst = 0; dst < table_size; dst++) {
111 xbt_dynar_get_as(vertices_, dst, NetCard*);
113 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
114 route->link_list = xbt_dynar_new(sizeof(Link*), NULL);
116 getRouteAndLatency(my_src, my_dst, route, NULL);
118 XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
123 xbt_node_t current, previous;
124 const char *previous_name, *current_name;
127 previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
128 previous_name = route->gw_src->name();
130 previous = new_xbt_graph_node(graph, my_src->name(), nodes);
131 previous_name = my_src->name();
134 xbt_dynar_foreach(route->link_list, cpt, link) {
135 const char *link_name = static_cast<simgrid::surf::Resource*>(
137 current = new_xbt_graph_node(graph, link_name, nodes);
138 current_name = link_name;
139 new_xbt_graph_edge(graph, previous, current, edges);
140 XBT_DEBUG (" %s -> %s", previous_name, current_name);
142 previous_name = current_name;
146 current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
147 current_name = route->gw_dst->name();
149 current = new_xbt_graph_node(graph, my_dst->name(), nodes);
150 current_name = my_dst->name();
152 new_xbt_graph_edge(graph, previous, current, edges);
153 XBT_DEBUG (" %s -> %s", previous_name, current_name);
155 xbt_dynar_free (&(route->link_list));
162 /* ************************************************************************** */
163 /* ************************* GENERIC AUX FUNCTIONS ************************** */
164 /* change a route containing link names into a route containing link entities */
165 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
166 sg_platf_route_cbarg_t routearg, int change_order) {
168 sg_platf_route_cbarg_t result;
172 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
173 result->link_list = xbt_dynar_new(sizeof(Link*), NULL);
175 xbt_assert(hierarchy == SURF_ROUTING_BASE
176 || hierarchy == SURF_ROUTING_RECURSIVE,
177 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
179 if (hierarchy == SURF_ROUTING_RECURSIVE) {
181 xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
183 /* remember not erase the gateway names */
184 result->gw_src = routearg->gw_src;
185 result->gw_dst = routearg->gw_dst;
188 xbt_dynar_foreach(routearg->link_list, cpt, link_name) {
190 Link *link = Link::byName(link_name);
193 xbt_dynar_push(result->link_list, &link);
195 xbt_dynar_unshift(result->link_list, &link);
197 THROWF(mismatch_error, 0, "Link '%s' not found", link_name);
203 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
205 xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
206 xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
208 As *src_as = src->containingAS();
209 As *dst_as = dst->containingAS();
211 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.",
212 src->name(), src_as->name_, dst->name(), dst_as->name_);
214 xbt_assert(this == dst_as,
215 "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
216 src->name(), dst->name(), src_as->name_, dst_as->name_, name_);
218 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
219 const char *srcName = route->src;
220 const char *dstName = route->dst;
221 NetCard *src = sg_netcard_by_name_or_null(srcName);
222 NetCard *dst = sg_netcard_by_name_or_null(dstName);
224 if(!route->gw_dst && !route->gw_src) {
225 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
226 xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
227 xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
228 xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s and %s) forbidden.", srcName, dstName);
229 xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_HOST || src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
230 "When defining a route, src must be an host or a router but '%s' is not. Did you meant to have an ASroute?", srcName);
231 xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
232 "When defining a route, dst must be an host or a router but '%s' is not. Did you meant to have an ASroute?", dstName);
234 XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
235 xbt_assert(src->getRcType()==SURF_NETWORK_ELEMENT_AS,
236 "When defining an ASroute, src must be an AS but '%s' is not", srcName);
237 xbt_assert(dst->getRcType()==SURF_NETWORK_ELEMENT_AS,
238 "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
240 xbt_assert(route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_src->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
241 "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
242 xbt_assert(route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_HOST || route->gw_dst->getRcType()==SURF_NETWORK_ELEMENT_ROUTER,
243 "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
245 xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
247 xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
248 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
249 xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
250 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
251 xbt_assert(!xbt_dynar_is_empty(route->link_list), "Empty route (between %s@%s and %s@%s) forbidden.",
252 srcName,route->gw_src->name(), dstName,route->gw_dst->name());