Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Pointer-to-const for Sonar.
[simgrid.git] / src / kernel / routing / StarZone.cpp
1 /* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
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 "xbt/string.hpp"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_star, surf, "Routing part of surf");
13
14 namespace simgrid {
15 namespace kernel {
16 namespace routing {
17 StarZone::StarZone(const std::string& name) : ClusterZone(name) {}
18
19 void StarZone::add_links_to_route(const std::vector<resource::LinkImpl*>& links, Route* route, double* latency,
20                                   std::unordered_set<resource::LinkImpl*>& added_links) const
21 {
22   for (auto* link : links) {
23     /* do not add duplicated links in route->link_list_ */
24     if (not added_links.insert(link).second)
25       continue;
26     if (latency)
27       *latency += link->get_latency();
28     route->link_list_.push_back(link);
29   }
30 }
31
32 void StarZone::get_local_route(const NetPoint* src, const NetPoint* dst, Route* route, double* latency)
33 {
34   XBT_VERB("StarZone getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
35            dst->id());
36
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;
40   /* loopback */
41   if (src == dst && src_route.has_loopback()) {
42     add_links_to_route(src_route.loopback, route, latency, added_links);
43     return;
44   }
45
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());
52
53   /* going UP */
54   add_links_to_route(src_route.links_up, route, latency, added_links);
55
56   /* going DOWN */
57   add_links_to_route(dst_route.links_down, route, latency, added_links);
58   /* gateways */
59   route->gw_src_ = src_route.gateway;
60   route->gw_dst_ = dst_route.gateway;
61 }
62
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)
65 {
66   xbt_node_t star_node = new_xbt_graph_node(graph, get_cname(), nodes);
67
68   for (auto const& src : get_vertices()) {
69     /* going up */
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);
75       previous = current;
76     }
77     new_xbt_graph_edge(graph, previous, star_node, edges);
78     /* going down */
79     previous = star_node;
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, current, previous, edges);
83       previous = current;
84     }
85     new_xbt_graph_edge(graph, src_node, previous, edges);
86   }
87 }
88
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
91 {
92   const char* src_name = src ? src->get_cname() : "nullptr";
93   const char* dst_name = dst ? dst->get_cname() : "nullptr";
94
95   if ((not src && not dst) || (dst && src && src != dst))
96     throw std::invalid_argument(xbt::string_printf(
97         "Cannot add route from %s to %s. In a StarZone, route must be:  i) from source netpoint to everyone, ii) from "
98         "everyone to a single netpoint or iii) loopback, same source and destination",
99         src_name, dst_name));
100
101   if (symmetrical && not src)
102     throw std::invalid_argument(xbt::string_printf("Cannot add route from %s to %s. In a StarZone, symmetrical routes "
103                                                    "must be set from source to everyone (not the contrary)",
104                                                    src_name, dst_name));
105
106   if (src && src->is_netzone()) {
107     if (not gw_src)
108       throw std::invalid_argument(xbt::string_printf(
109           "StarZone::add_route(): source %s is a netzone but gw_src isn't configured", src->get_cname()));
110     if (gw_src->is_netzone())
111       throw std::invalid_argument(
112           xbt::string_printf("StarZone::add_route(): src(%s) is a netzone, gw_src(%s) cannot be a netzone",
113                              src->get_cname(), gw_src->get_cname()));
114
115     const auto* netzone_src = get_netzone_recursive(src);
116     if (not netzone_src->is_component_recursive(gw_src))
117       throw std::invalid_argument(xbt::string_printf(
118           "Invalid NetzoneRoute from %s@%s to %s: gw_src %s belongs to %s, not to %s.", src_name, gw_src->get_cname(),
119           dst_name, gw_src->get_cname(), gw_src->get_englobing_zone()->get_cname(), src_name));
120   }
121
122   if (dst && dst->is_netzone()) {
123     if (not gw_dst)
124       throw std::invalid_argument(xbt::string_printf(
125           "StarZone::add_route(): destination %s is a netzone but gw_dst isn't configured", dst->get_cname()));
126     if (gw_dst->is_netzone())
127       throw std::invalid_argument(
128           xbt::string_printf("StarZone::add_route(): dst(%s) is a netzone, gw_dst(%s) cannot be a netzone",
129                              dst->get_cname(), gw_dst->get_cname()));
130
131     const auto* netzone_dst = get_netzone_recursive(dst);
132     if (not netzone_dst->is_component_recursive(gw_dst))
133       throw std::invalid_argument(xbt::string_printf(
134           "Invalid NetzoneRoute from %s@%s to %s: gw_dst %s belongs to %s, not to %s.", dst_name, gw_dst->get_cname(),
135           src_name, gw_dst->get_cname(), gw_dst->get_englobing_zone()->get_cname(), dst_name));
136   }
137 }
138
139 void StarZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
140                          const std::vector<kernel::resource::LinkImpl*>& link_list_, bool symmetrical)
141 {
142   check_add_route_param(src, dst, gw_src, gw_dst, symmetrical);
143
144   s4u::NetZone::on_route_creation(symmetrical, src, dst, gw_src, gw_dst, link_list_);
145
146   /* loopback */
147   if (src == dst) {
148     routes_[src->id()].loopback = link_list_;
149   } else {
150     /* src to everyone */
151     if (src) {
152       auto& route        = routes_[src->id()];
153       route.links_up     = link_list_;
154       route.gateway      = gw_src;
155       route.links_up_set = true;
156       if (symmetrical) {
157         /* reverse it for down/symmetrical links */
158         route.links_down.assign(link_list_.rbegin(), link_list_.rend());
159         route.links_down_set = true;
160       }
161     }
162     /* dst to everyone */
163     if (dst) {
164       auto& route          = routes_[dst->id()];
165       route.links_down     = link_list_;
166       route.gateway        = gw_dst;
167       route.links_down_set = true;
168     }
169   }
170 }
171
172 void StarZone::do_seal()
173 {
174   /* add default empty links if nothing was configured by user */
175   for (auto const& node : get_vertices()) {
176     auto route = routes_.emplace(node->id(), StarRoute());
177     if (route.second) {
178       route.first->second.links_down_set = true;
179       route.first->second.links_up_set   = true;
180     }
181   }
182 }
183
184 } // namespace routing
185 } // namespace kernel
186
187 namespace s4u {
188 NetZone* create_star_zone(const std::string& name)
189 {
190   return (new kernel::routing::StarZone(name))->get_iface();
191 }
192 } // namespace s4u
193
194 } // namespace simgrid