1 /* Copyright (c) 2009-2021. 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/surf/network_interface.hpp"
10 #include "src/surf/xml/platf_private.hpp" // RouteCreationArgs and friends
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_star, surf, "Routing part of surf");
17 StarZone::StarZone(const std::string& name) : NetZoneImpl(name) {}
19 void StarZone::add_links_to_route(const std::vector<resource::LinkImpl*>& links, RouteCreationArgs* route,
20 double* latency, std::unordered_set<resource::LinkImpl*>& added_links) const
22 for (auto* link : links) {
23 /* do not add duplicated links in route->link_list */
24 if (not added_links.insert(link).second)
27 *latency += link->get_latency();
28 route->link_list.push_back(link);
32 void StarZone::get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* route, double* latency)
34 XBT_VERB("StarZone getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
37 const auto& src_route = routes_.at(src->id());
38 const auto& dst_route = routes_.at(dst->id());
39 std::unordered_set<resource::LinkImpl*> added_links;
41 if (src == dst && src_route.has_loopback()) {
42 add_links_to_route(src_route.loopback, route, latency, added_links);
46 xbt_assert(src_route.has_links_up(),
47 "StarZone routing (%s - %s): no link UP from source node. Did you use add_route() to set it?",
48 src->get_cname(), dst->get_cname());
49 xbt_assert(dst_route.has_links_down(),
50 "StarZone routing (%s - %s): no link DOWN to destination node. Did you use add_route() to set it?",
51 src->get_cname(), dst->get_cname());
54 add_links_to_route(src_route.links_up, route, latency, added_links);
57 add_links_to_route(dst_route.links_down, route, latency, added_links);
59 route->gw_src = src_route.gateway;
60 route->gw_dst = dst_route.gateway;
63 void StarZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
64 std::map<std::string, xbt_edge_t, std::less<>>* edges)
66 xbt_node_t star_node = new_xbt_graph_node(graph, get_cname(), nodes);
68 for (auto const& src : get_vertices()) {
70 xbt_node_t src_node = new_xbt_graph_node(graph, src->get_cname(), nodes);
71 xbt_node_t previous = src_node;
72 for (auto const* link : routes_[src->id()].links_up) {
73 xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
74 new_xbt_graph_edge(graph, previous, current, edges);
77 new_xbt_graph_edge(graph, previous, star_node, edges);
80 for (auto const* link : routes_[src->id()].links_down) {
81 xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
82 new_xbt_graph_edge(graph, previous, current, edges);
85 new_xbt_graph_edge(graph, previous, src_node, edges);
89 void StarZone::check_add_route_param(const NetPoint* src, const NetPoint* dst, const NetPoint* gw_src,
90 const NetPoint* gw_dst, bool symmetrical) const
92 const char* src_name = src ? src->get_cname() : "nullptr";
93 const char* dst_name = dst ? dst->get_cname() : "nullptr";
95 xbt_assert((src || dst) && (not dst || not src || src == dst),
96 "Cannot add route from %s to %s. In a StarZone, route must be: i) from source host to everyone, ii) from "
97 "everyone to a single host or iii) loopback, same source and destination",
99 xbt_assert(not symmetrical || src,
100 "Cannot add route from %s to %s. In a StarZone, symmetrical routes must be set from source to everyone "
101 "(not the contrary).",
104 if (src && src->is_netzone()) {
105 xbt_assert(gw_src, "add_route(): source %s is a netzone but gw_src isn't configured", src->get_cname());
106 xbt_assert(not gw_src->is_netzone(), "add_route(): src(%s) is a netzone, gw_src(%s) cannot be a netzone",
107 src->get_cname(), gw_src->get_cname());
110 if (dst && dst->is_netzone()) {
111 xbt_assert(gw_dst, "add_route(): destination %s is a netzone but gw_dst isn't configured", dst->get_cname());
112 xbt_assert(not gw_dst->is_netzone(), "add_route(): dst(%s) is a netzone, gw_dst(%s) cannot be a netzone",
113 dst->get_cname(), gw_dst->get_cname());
117 void StarZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
118 const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical)
120 check_add_route_param(src, dst, gw_src, gw_dst, symmetrical);
122 s4u::NetZone::on_route_creation(symmetrical, gw_src, gw_dst, gw_src, gw_dst, link_list);
126 routes_[src->id()].loopback = link_list;
128 /* src to everyone */
130 auto& route = routes_[src->id()];
131 route.links_up = link_list;
132 route.gateway = gw_src;
133 route.links_up_set = true;
135 /* reverse it for down/symmetrical links */
136 route.links_down.assign(link_list.rbegin(), link_list.rend());
137 route.links_down_set = true;
140 /* dst to everyone */
142 auto& route = routes_[dst->id()];
143 route.links_down = link_list;
144 route.gateway = gw_dst;
145 route.links_down_set = true;
150 void StarZone::do_seal()
152 /* add default empty links if nothing was configured by user */
153 for (auto const& node : get_vertices()) {
154 auto route = routes_.emplace(node->id(), StarRoute());
156 route.first->second.links_down_set = true;
157 route.first->second.links_up_set = true;
162 } // namespace routing
163 } // namespace kernel
166 NetZone* create_star_zone(const std::string& name)
168 return (new kernel::routing::StarZone(name))->get_iface();
172 } // namespace simgrid