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 std::vector<kernel::routing::NetPoint*> vertices = getVertices();
77 for (auto my_src : vertices) {
78 for (auto my_dst : vertices) {
82 sg_platf_route_cbarg_t route = xbt_new0(s_sg_platf_route_cbarg_t, 1);
83 route->link_list = new std::vector<surf::LinkImpl*>();
85 getLocalRoute(my_src, my_dst, route, nullptr);
87 XBT_DEBUG("get_route_and_latency %s -> %s", my_src->cname(), my_dst->cname());
91 const char *previous_name;
92 const char *current_name;
95 previous = new_xbt_graph_node(graph, route->gw_src->cname(), nodes);
96 previous_name = route->gw_src->cname();
98 previous = new_xbt_graph_node(graph, my_src->cname(), nodes);
99 previous_name = my_src->cname();
102 for (auto link : *route->link_list) {
103 const char* link_name = link->cname();
104 current = new_xbt_graph_node(graph, link_name, nodes);
105 current_name = link_name;
106 new_xbt_graph_edge(graph, previous, current, edges);
107 XBT_DEBUG(" %s -> %s", previous_name, current_name);
109 previous_name = current_name;
113 current = new_xbt_graph_node(graph, route->gw_dst->cname(), nodes);
114 current_name = route->gw_dst->cname();
116 current = new_xbt_graph_node(graph, my_dst->cname(), nodes);
117 current_name = my_dst->cname();
119 new_xbt_graph_edge(graph, previous, current, edges);
120 XBT_DEBUG(" %s -> %s", previous_name, current_name);
122 delete route->link_list;
128 /* ************************************************************************** */
129 /* ************************* GENERIC AUX FUNCTIONS ************************** */
130 /* change a route containing link names into a route containing link entities */
131 sg_platf_route_cbarg_t RoutedZone::newExtendedRoute(RoutingMode hierarchy, sg_platf_route_cbarg_t routearg,
134 sg_platf_route_cbarg_t result;
136 result = xbt_new0(s_sg_platf_route_cbarg_t, 1);
137 result->link_list = new std::vector<surf::LinkImpl*>();
139 xbt_assert(hierarchy == RoutingMode::base || hierarchy == RoutingMode::recursive,
140 "The hierarchy of this netzone is neither BASIC nor RECURSIVE, I'm lost here.");
142 if (hierarchy == RoutingMode::recursive) {
143 xbt_assert(routearg->gw_src && routearg->gw_dst, "nullptr is obviously a deficient gateway");
145 result->gw_src = routearg->gw_src;
146 result->gw_dst = routearg->gw_dst;
149 for (auto link : *routearg->link_list) {
151 result->link_list->push_back(link);
153 result->link_list->insert(result->link_list->begin(), link);
155 result->link_list->shrink_to_fit();
160 void RoutedZone::getRouteCheckParams(NetPoint* src, NetPoint* dst)
162 xbt_assert(src, "Cannot find a route from nullptr to %s", dst->cname());
163 xbt_assert(dst, "Cannot find a route from %s to nullptr", src->cname());
165 NetZone* src_as = src->netzone();
166 NetZone* dst_as = dst->netzone();
168 xbt_assert(src_as == dst_as,
169 "Internal error: %s@%s and %s@%s are not in the same netzone as expected. Please report that bug.",
170 src->cname(), src_as->getCname(), dst->cname(), dst_as->getCname());
172 xbt_assert(this == dst_as, "Internal error: route destination %s@%s is not in netzone %s as expected (route source: "
173 "%s@%s). Please report that bug.",
174 src->cname(), dst->cname(), src_as->getCname(), dst_as->getCname(), getCname());
176 void RoutedZone::addRouteCheckParams(sg_platf_route_cbarg_t route)
178 NetPoint* src = route->src;
179 NetPoint* dst = route->dst;
180 const char* srcName = src->cname();
181 const char* dstName = dst->cname();
183 if (not route->gw_dst && not route->gw_src) {
184 XBT_DEBUG("Load Route from \"%s\" to \"%s\"", srcName, dstName);
185 xbt_assert(src, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, srcName);
186 xbt_assert(dst, "Cannot add a route from %s to %s: %s does not exist.", srcName, dstName, dstName);
187 xbt_assert(not route->link_list->empty(), "Empty route (between %s and %s) forbidden.", srcName, dstName);
188 xbt_assert(not src->isNetZone(),
189 "When defining a route, src cannot be a netzone such as '%s'. Did you meant to have an NetzoneRoute?",
191 xbt_assert(not dst->isNetZone(),
192 "When defining a route, dst cannot be a netzone such as '%s'. Did you meant to have an NetzoneRoute?",
195 XBT_DEBUG("Load NetzoneRoute from %s@%s to %s@%s", srcName, route->gw_src->cname(), dstName, route->gw_dst->cname());
196 xbt_assert(src->isNetZone(), "When defining a NetzoneRoute, src must be a netzone but '%s' is not", srcName);
197 xbt_assert(dst->isNetZone(), "When defining a NetzoneRoute, dst must be a netzone but '%s' is not", dstName);
199 xbt_assert(route->gw_src->isHost() || route->gw_src->isRouter(),
200 "When defining a NetzoneRoute, gw_src must be an host or a router but '%s' is not.", srcName);
201 xbt_assert(route->gw_dst->isHost() || route->gw_dst->isRouter(),
202 "When defining a NetzoneRoute, gw_dst must be an host or a router but '%s' is not.", dstName);
204 xbt_assert(route->gw_src != route->gw_dst, "Cannot define an NetzoneRoute from '%s' to itself", route->gw_src->cname());
206 xbt_assert(src, "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(), srcName);
208 xbt_assert(dst, "Cannot add a route from %s@%s to %s@%s: %s does not exist.", srcName, route->gw_src->cname(),
209 dstName, route->gw_dst->cname(), dstName);
210 xbt_assert(not route->link_list->empty(), "Empty route (between %s@%s and %s@%s) forbidden.", srcName,
211 route->gw_src->cname(), dstName, route->gw_dst->cname());
214 onRouteCreation(route->symmetrical, route->src, route->dst, route->gw_src, route->gw_dst, route->link_list);