1 /* Copyright (c) 2009-2022. 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. */
6 #include "simgrid/kernel/routing/StarZone.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/kernel/routing/RoutedZone.hpp"
9 #include "src/kernel/resource/NetworkModel.hpp"
10 #include "xbt/string.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_routing_star, ker_routing, "Kernel Star Routing");
15 namespace kernel::routing {
16 StarZone::StarZone(const std::string& name) : ClusterZone(name) {}
18 void StarZone::add_links_to_route(const std::vector<resource::StandardLinkImpl*>& links, Route* route, double* latency,
19 std::unordered_set<resource::StandardLinkImpl*>& added_links) const
21 for (auto* link : links) {
22 /* do not add duplicated links in route->link_list_ */
23 if (not added_links.insert(link).second)
25 add_link_latency(route->link_list_, link, latency);
29 void StarZone::get_local_route(const NetPoint* src, const NetPoint* dst, Route* route, double* latency)
31 XBT_VERB("StarZone getLocalRoute from '%s'[%lu] to '%s'[%lu]", src->get_cname(), src->id(), dst->get_cname(),
34 const auto& src_route = routes_.at(src->id());
35 const auto& dst_route = routes_.at(dst->id());
36 std::unordered_set<resource::StandardLinkImpl*> added_links;
38 if (src == dst && src_route.has_loopback()) {
39 add_links_to_route(src_route.loopback, route, latency, added_links);
43 xbt_assert(src_route.has_links_up(),
44 "StarZone routing (%s - %s): no link UP from source node. Did you use add_route() to set it?",
45 src->get_cname(), dst->get_cname());
46 xbt_assert(dst_route.has_links_down(),
47 "StarZone routing (%s - %s): no link DOWN to destination node. Did you use add_route() to set it?",
48 src->get_cname(), dst->get_cname());
51 add_links_to_route(src_route.links_up, route, latency, added_links);
54 add_links_to_route(dst_route.links_down, route, latency, added_links);
56 route->gw_src_ = src_route.gateway;
57 route->gw_dst_ = dst_route.gateway;
60 void StarZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
61 std::map<std::string, xbt_edge_t, std::less<>>* edges)
63 xbt_node_t star_node = new_xbt_graph_node(graph, get_cname(), nodes);
65 for (auto const& src : get_vertices()) {
67 xbt_node_t src_node = new_xbt_graph_node(graph, src->get_cname(), nodes);
68 xbt_node_t previous = src_node;
69 for (auto const* link : routes_[src->id()].links_up) {
70 xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
71 new_xbt_graph_edge(graph, previous, current, edges);
74 new_xbt_graph_edge(graph, previous, star_node, edges);
77 for (auto const* link : routes_[src->id()].links_down) {
78 xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
79 new_xbt_graph_edge(graph, current, previous, edges);
82 new_xbt_graph_edge(graph, src_node, previous, edges);
86 void StarZone::check_add_route_param(const NetPoint* src, const NetPoint* dst, const NetPoint* gw_src,
87 const NetPoint* gw_dst, bool symmetrical) const
89 const char* src_name = src ? src->get_cname() : "nullptr";
90 const char* dst_name = dst ? dst->get_cname() : "nullptr";
92 if ((not src && not dst) || (dst && src && src != dst))
93 throw std::invalid_argument(xbt::string_printf(
94 "Cannot add route from %s to %s. In a StarZone, route must be: i) from source netpoint to everyone, ii) from "
95 "everyone to a single netpoint or iii) loopback, same source and destination",
98 if (symmetrical && not src)
99 throw std::invalid_argument(xbt::string_printf("Cannot add route from %s to %s. In a StarZone, symmetrical routes "
100 "must be set from source to everyone (not the contrary)",
101 src_name, dst_name));
103 if (src && src->is_netzone()) {
105 throw std::invalid_argument(xbt::string_printf(
106 "StarZone::add_route(): source %s is a netzone but gw_src isn't configured", src->get_cname()));
107 if (gw_src->is_netzone())
108 throw std::invalid_argument(
109 xbt::string_printf("StarZone::add_route(): src(%s) is a netzone, gw_src(%s) cannot be a netzone",
110 src->get_cname(), gw_src->get_cname()));
112 const auto* netzone_src = get_netzone_recursive(src);
113 if (not netzone_src->is_component_recursive(gw_src))
114 throw std::invalid_argument(xbt::string_printf(
115 "Invalid NetzoneRoute from %s@%s to %s: gw_src %s belongs to %s, not to %s.", src_name, gw_src->get_cname(),
116 dst_name, gw_src->get_cname(), gw_src->get_englobing_zone()->get_cname(), src_name));
119 if (dst && dst->is_netzone()) {
121 throw std::invalid_argument(xbt::string_printf(
122 "StarZone::add_route(): destination %s is a netzone but gw_dst isn't configured", dst->get_cname()));
123 if (gw_dst->is_netzone())
124 throw std::invalid_argument(
125 xbt::string_printf("StarZone::add_route(): dst(%s) is a netzone, gw_dst(%s) cannot be a netzone",
126 dst->get_cname(), gw_dst->get_cname()));
128 const auto* netzone_dst = get_netzone_recursive(dst);
129 if (not netzone_dst->is_component_recursive(gw_dst))
130 throw std::invalid_argument(xbt::string_printf(
131 "Invalid NetzoneRoute from %s@%s to %s: gw_dst %s belongs to %s, not to %s.", dst_name, gw_dst->get_cname(),
132 src_name, gw_dst->get_cname(), gw_dst->get_englobing_zone()->get_cname(), dst_name));
136 void StarZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
137 const std::vector<s4u::LinkInRoute>& link_list, bool symmetrical)
139 check_add_route_param(src, dst, gw_src, gw_dst, symmetrical);
143 routes_[src->id()].loopback = get_link_list_impl(link_list, false);
145 /* src to everyone */
147 auto& route = routes_[src->id()];
148 route.links_up = get_link_list_impl(link_list, false);
149 route.gateway = gw_src;
150 route.links_up_set = true;
152 auto links_down = get_link_list_impl(link_list, true);
153 /* reverse it for down/symmetrical links */
154 route.links_down.assign(links_down.rbegin(), links_down.rend());
155 route.links_down_set = true;
158 /* dst to everyone */
160 auto& route = routes_[dst->id()];
161 route.links_down = get_link_list_impl(link_list, false);
162 route.gateway = gw_dst;
163 route.links_down_set = true;
168 void StarZone::do_seal()
170 /* add default empty links if nothing was configured by user */
171 for (auto const& node : get_vertices()) {
172 auto [route, inserted] = routes_.try_emplace(node->id());
174 route->second.links_down_set = true;
175 route->second.links_up_set = true;
180 } // namespace kernel::routing
183 NetZone* create_star_zone(const std::string& name)
185 return (new kernel::routing::StarZone(name))->get_iface();
189 } // namespace simgrid