1 /* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "xbt/sysdep.h"
12 #include "src/kernel/routing/NetPoint.hpp"
13 #include "src/kernel/routing/RoutedZone.hpp"
14 #include "src/surf/network_interface.hpp"
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_routing_generic, surf_route, "Generic implementation of the surf routing");
18 void routing_route_free(sg_platf_route_cbarg_t route)
21 delete route->link_list;
26 /* ***************************************************************** */
27 /* *********************** GENERIC METHODS ************************* */
29 static const char* instr_node_name(xbt_node_t node)
31 void* data = xbt_graph_node_get_data(node);
32 return static_cast<const char*>(data);
35 xbt_node_t new_xbt_graph_node(xbt_graph_t graph, const char* name, xbt_dict_t nodes)
37 xbt_node_t ret = static_cast<xbt_node_t>(xbt_dict_get_or_null(nodes, name));
41 ret = xbt_graph_new_node(graph, xbt_strdup(name));
42 xbt_dict_set(nodes, name, ret, nullptr);
46 xbt_edge_t new_xbt_graph_edge(xbt_graph_t graph, xbt_node_t s, xbt_node_t d, xbt_dict_t edges)
48 const char* sn = instr_node_name(s);
49 const char* dn = instr_node_name(d);
50 std::string name = std::string(sn) + dn;
52 xbt_edge_t ret = static_cast<xbt_edge_t>(xbt_dict_get_or_null(edges, name.c_str()));
54 name = std::string(dn) + sn;
55 ret = static_cast<xbt_edge_t>(xbt_dict_get_or_null(edges, name.c_str()));
59 ret = xbt_graph_new_edge(graph, s, d, nullptr);
60 xbt_dict_set(edges, name.c_str(), ret, nullptr);
69 RoutedZone::RoutedZone(NetZone* father, const char* name) : NetZoneImpl(father, name)
73 void RoutedZone::getGraph(xbt_graph_t graph, xbt_dict_t nodes, xbt_dict_t edges)
75 for (auto my_src : vertices_) {
76 for (auto my_dst : vertices_) {
80 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
81 route->link_list = new std::vector<surf::LinkImpl*>();
83 getLocalRoute(my_src, my_dst, route, nullptr);
85 XBT_DEBUG("get_route_and_latency %s -> %s", my_src->cname(), my_dst->cname());
89 const char *previous_name;
90 const char *current_name;
93 previous = new_xbt_graph_node(graph, route->gw_src->cname(), nodes);
94 previous_name = route->gw_src->cname();
96 previous = new_xbt_graph_node(graph, my_src->cname(), nodes);
97 previous_name = my_src->cname();
100 for (auto link : *route->link_list) {
101 const char* link_name = link->cname();
102 current = new_xbt_graph_node(graph, link_name, nodes);
103 current_name = link_name;
104 new_xbt_graph_edge(graph, previous, current, edges);
105 XBT_DEBUG(" %s -> %s", previous_name, current_name);
107 previous_name = current_name;
111 current = new_xbt_graph_node(graph, route->gw_dst->cname(), nodes);
112 current_name = route->gw_dst->cname();
114 current = new_xbt_graph_node(graph, my_dst->cname(), nodes);
115 current_name = my_dst->cname();
117 new_xbt_graph_edge(graph, previous, current, edges);
118 XBT_DEBUG(" %s -> %s", previous_name, current_name);
120 delete route->link_list;
126 /* ************************************************************************** */
127 /* ************************* GENERIC AUX FUNCTIONS ************************** */
128 /* change a route containing link names into a route containing link entities */
129 sg_platf_route_cbarg_t RoutedZone::newExtendedRoute(RoutingMode hierarchy, sg_platf_route_cbarg_t routearg,
132 sg_platf_route_cbarg_t result;
134 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
135 result->link_list = new std::vector<surf::LinkImpl*>();
137 xbt_assert(hierarchy == RoutingMode::base || hierarchy == RoutingMode::recursive,
138 "The hierarchy of this netzone is neither BASIC nor RECURSIVE, I'm lost here.");
140 if (hierarchy == RoutingMode::recursive) {
141 xbt_assert(routearg->gw_src && routearg->gw_dst, "nullptr is obviously a deficient gateway");
143 result->gw_src = routearg->gw_src;
144 result->gw_dst = routearg->gw_dst;
147 for (auto link : *routearg->link_list) {
149 result->link_list->push_back(link);
151 result->link_list->insert(result->link_list->begin(), link);
153 result->link_list->shrink_to_fit();
158 void RoutedZone::getRouteCheckParams(NetPoint* src, NetPoint* dst)
160 xbt_assert(src, "Cannot find a route from nullptr to %s", dst->cname());
161 xbt_assert(dst, "Cannot find a route from %s to nullptr", src->cname());
163 NetZone* src_as = src->netzone();
164 NetZone* dst_as = dst->netzone();
166 xbt_assert(src_as == dst_as,
167 "Internal error: %s@%s and %s@%s are not in the same netzone as expected. Please report that bug.",
168 src->cname(), src_as->getCname(), dst->cname(), dst_as->getCname());
170 xbt_assert(this == dst_as, "Internal error: route destination %s@%s is not in netzone %s as expected (route source: "
171 "%s@%s). Please report that bug.",
172 src->cname(), dst->cname(), src_as->getCname(), dst_as->getCname(), getCname());
174 void RoutedZone::addRouteCheckParams(sg_platf_route_cbarg_t route)
176 NetPoint* src = route->src;
177 NetPoint* dst = route->dst;
178 const char* srcName = src->cname();
179 const char* dstName = dst->cname();
181 if (not route->gw_dst && not route->gw_src) {
182 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
183 xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
184 xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
185 xbt_assert(not route->link_list->empty(), "Empty route (between %s and %s) forbidden.", srcName, dstName);
186 xbt_assert(not src->isNetZone(),
187 "When defining a route, src cannot be a netzone such as '%s'. Did you meant to have an NetzoneRoute?",
189 xbt_assert(not dst->isNetZone(),
190 "When defining a route, dst cannot be a netzone such as '%s'. Did you meant to have an NetzoneRoute?",
193 XBT_DEBUG("Load NetzoneRoute from %s@%s to %s@%s", srcName, route->gw_src->cname(), dstName, route->gw_dst->cname());
194 xbt_assert(src->isNetZone(), "When defining a NetzoneRoute, src must be a netzone but '%s' is not", srcName);
195 xbt_assert(dst->isNetZone(), "When defining a NetzoneRoute, dst must be a netzone but '%s' is not", dstName);
197 xbt_assert(route->gw_src->isHost() || route->gw_src->isRouter(),
198 "When defining a NetzoneRoute, gw_src must be an host or a router but '%s' is not.", srcName);
199 xbt_assert(route->gw_dst->isHost() || route->gw_dst->isRouter(),
200 "When defining a NetzoneRoute, gw_dst must be an host or a router but '%s' is not.", dstName);
202 xbt_assert(route->gw_src != route->gw_dst, "Cannot define an NetzoneRoute from '%s' to itself", route->gw_src->cname());
204 xbt_assert(src, "Cannot add a route from %s@%s to %s@%s: %s does not exist.", srcName, route->gw_src->cname(),
205 dstName, route->gw_dst->cname(), srcName);
206 xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.", srcName, route->gw_src->cname(),
207 dstName, route->gw_dst->cname(), dstName);
208 xbt_assert(not route->link_list->empty(), "Empty route (between %s@%s and %s@%s) forbidden.", srcName,
209 route->gw_src->cname(), dstName, route->gw_dst->cname());
212 onRouteCreation(route->symmetrical, route->src, route->dst, route->gw_src, route->gw_dst, route->link_list);