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 delete 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, xbt_dict_t nodes)
59 xbt_node_t ret = (xbt_node_t) xbt_dict_get_or_null(nodes, name);
63 ret = xbt_graph_new_node(graph, xbt_strdup(name));
64 xbt_dict_set(nodes, name, ret, NULL);
68 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d, xbt_dict_t edges)
70 const char *sn = instr_node_name(s);
71 const char *dn = instr_node_name(d);
72 int len = strlen(sn) + strlen(dn) + 1;
73 char *name = (char *) xbt_malloc(len * sizeof(char));
76 snprintf(name, len, "%s%s", sn, dn);
77 xbt_edge_t ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
79 snprintf(name, len, "%s%s", dn, sn);
80 ret = (xbt_edge_t) xbt_dict_get_or_null(edges, name);
84 ret = xbt_graph_new_edge(graph, s, d, NULL);
85 xbt_dict_set(edges, name, ret, NULL);
94 void AsRoutedGraph::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
97 int table_size = xbt_dynar_length(vertices_);
100 for (src = 0; src < table_size; src++) {
102 xbt_dynar_get_as(vertices_, src, NetCard*);
103 for (dst = 0; dst < table_size; dst++) {
107 xbt_dynar_get_as(vertices_, dst, NetCard*);
109 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
110 route->link_list = new std::vector<Link*>();
112 getRouteAndLatency(my_src, my_dst, route, NULL);
114 XBT_DEBUG ("get_route_and_latency %s -> %s", my_src->name(), my_dst->name());
116 xbt_node_t current, previous;
117 const char *previous_name, *current_name;
120 previous = new_xbt_graph_node(graph, route->gw_src->name(), nodes);
121 previous_name = route->gw_src->name();
123 previous = new_xbt_graph_node(graph, my_src->name(), nodes);
124 previous_name = my_src->name();
127 for (auto link: *route->link_list) {
128 const char *link_name = link->getName();
129 current = new_xbt_graph_node(graph, link_name, nodes);
130 current_name = link_name;
131 new_xbt_graph_edge(graph, previous, current, edges);
132 XBT_DEBUG (" %s -> %s", previous_name, current_name);
134 previous_name = current_name;
138 current = new_xbt_graph_node(graph, route->gw_dst->name(), nodes);
139 current_name = route->gw_dst->name();
141 current = new_xbt_graph_node(graph, my_dst->name(), nodes);
142 current_name = my_dst->name();
144 new_xbt_graph_edge(graph, previous, current, edges);
145 XBT_DEBUG (" %s -> %s", previous_name, current_name);
147 delete route->link_list;
154 /* ************************************************************************** */
155 /* ************************* GENERIC AUX FUNCTIONS ************************** */
156 /* change a route containing link names into a route containing link entities */
157 sg_platf_route_cbarg_t AsRoutedGraph::newExtendedRoute(e_surf_routing_hierarchy_t hierarchy,
158 sg_platf_route_cbarg_t routearg, int change_order)
161 sg_platf_route_cbarg_t result;
163 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
164 result->link_list = new std::vector<Link*>();
166 xbt_assert(hierarchy == SURF_ROUTING_BASE
167 || hierarchy == SURF_ROUTING_RECURSIVE,
168 "The hierarchy of this AS is neither BASIC nor RECURSIVE, I'm lost here.");
170 if (hierarchy == SURF_ROUTING_RECURSIVE) {
172 xbt_assert(routearg->gw_src && routearg->gw_dst, "NULL is obviously a deficient gateway");
174 /* remember not erase the gateway names */
175 result->gw_src = routearg->gw_src;
176 result->gw_dst = routearg->gw_dst;
179 for (auto link : *routearg->link_list) {
181 result->link_list->push_back(link);
183 result->link_list->insert(result->link_list->begin(), link);
189 void AsRoutedGraph::getRouteCheckParams(NetCard *src, NetCard *dst)
191 xbt_assert(src,"Cannot find a route from NULL to %s", dst->name());
192 xbt_assert(dst,"Cannot find a route from %s to NULL", src->name());
194 As *src_as = src->containingAS();
195 As *dst_as = dst->containingAS();
197 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.",
198 src->name(), src_as->name_, dst->name(), dst_as->name_);
200 xbt_assert(this == dst_as,
201 "Internal error: route destination %s@%s is not in AS %s as expected (route source: %s@%s). Please report that bug.",
202 src->name(), dst->name(), src_as->name_, dst_as->name_, name_);
204 void AsRoutedGraph::addRouteCheckParams(sg_platf_route_cbarg_t route) {
205 const char *srcName = route->src;
206 const char *dstName = route->dst;
207 NetCard *src = sg_netcard_by_name_or_null(srcName);
208 NetCard *dst = sg_netcard_by_name_or_null(dstName);
210 if(!route->gw_dst && !route->gw_src) {
211 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
212 xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
213 xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
214 xbt_assert(! route->link_list->empty(), "Empty route (between %s and %s) forbidden.", srcName, dstName);
215 xbt_assert(! src->isAS(), "When defining a route, src cannot be an AS such as '%s'. Did you meant to have an ASroute?", srcName);
216 xbt_assert(! dst->isAS(), "When defining a route, dst cannot be an AS such as '%s'. Did you meant to have an ASroute?", dstName);
218 XBT_DEBUG("Load ASroute from %s@%s to %s@%s", srcName, route->gw_src->name(), dstName, route->gw_dst->name());
219 xbt_assert(src->isAS(), "When defining an ASroute, src must be an AS but '%s' is not", srcName);
220 xbt_assert(dst->isAS(), "When defining an ASroute, dst must be an AS but '%s' is not", dstName);
222 xbt_assert(route->gw_src->isHost() || route->gw_src->isRouter(),
223 "When defining an ASroute, gw_src must be an host or a router but '%s' is not.", srcName);
224 xbt_assert(route->gw_dst->isHost() || route->gw_dst->isRouter(),
225 "When defining an ASroute, gw_dst must be an host or a router but '%s' is not.", dstName);
227 xbt_assert(route->gw_src != route->gw_dst, "Cannot define an ASroute from '%s' to itself", route->gw_src->name());
229 xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
230 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), srcName);
231 xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.",
232 srcName,route->gw_src->name(), dstName,route->gw_dst->name(), dstName);
233 xbt_assert(! route->link_list->empty(), "Empty route (between %s@%s and %s@%s) forbidden.",
234 srcName,route->gw_src->name(), dstName,route->gw_dst->name());