Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ClusterZone placeholder and kill some old code
[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 #if SIMGRID_HAVE_NS3
11 #include "src/surf/network_ns3.hpp"
12 #endif
13 #include "xbt/string.hpp"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_route_star, surf, "Routing part of surf");
16
17 namespace simgrid {
18 namespace kernel {
19 namespace routing {
20 StarZone::StarZone(const std::string& name) : ClusterZone(name) {}
21
22 void StarZone::add_links_to_route(const std::vector<resource::LinkImpl*>& links, Route* route, double* latency,
23                                   std::unordered_set<resource::LinkImpl*>& added_links) const
24 {
25   for (auto* link : links) {
26     /* do not add duplicated links in route->link_list_ */
27     if (not added_links.insert(link).second)
28       continue;
29     if (latency)
30       *latency += link->get_latency();
31     route->link_list_.push_back(link);
32   }
33 }
34
35 void StarZone::get_local_route(NetPoint* src, NetPoint* dst, Route* route, double* latency)
36 {
37   XBT_VERB("StarZone getLocalRoute from '%s'[%u] to '%s'[%u]", src->get_cname(), src->id(), dst->get_cname(),
38            dst->id());
39
40   const auto& src_route = routes_.at(src->id());
41   const auto& dst_route = routes_.at(dst->id());
42   std::unordered_set<resource::LinkImpl*> added_links;
43   /* loopback */
44   if (src == dst && src_route.has_loopback()) {
45     add_links_to_route(src_route.loopback, route, latency, added_links);
46     return;
47   }
48
49   xbt_assert(src_route.has_links_up(),
50              "StarZone routing (%s - %s): no link UP from source node. Did you use add_route() to set it?",
51              src->get_cname(), dst->get_cname());
52   xbt_assert(dst_route.has_links_down(),
53              "StarZone routing (%s - %s): no link DOWN to destination node. Did you use add_route() to set it?",
54              src->get_cname(), dst->get_cname());
55
56   /* going UP */
57   add_links_to_route(src_route.links_up, route, latency, added_links);
58
59   /* going DOWN */
60   add_links_to_route(dst_route.links_down, route, latency, added_links);
61   /* gateways */
62   route->gw_src_ = src_route.gateway;
63   route->gw_dst_ = dst_route.gateway;
64 }
65
66 void StarZone::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
67                          std::map<std::string, xbt_edge_t, std::less<>>* edges)
68 {
69   xbt_node_t star_node = new_xbt_graph_node(graph, get_cname(), nodes);
70
71   for (auto const& src : get_vertices()) {
72     /* going up */
73     xbt_node_t src_node = new_xbt_graph_node(graph, src->get_cname(), nodes);
74     xbt_node_t previous = src_node;
75     for (auto const* link : routes_[src->id()].links_up) {
76       xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
77       new_xbt_graph_edge(graph, previous, current, edges);
78       previous = current;
79     }
80     new_xbt_graph_edge(graph, previous, star_node, edges);
81     /* going down */
82     previous = star_node;
83     for (auto const* link : routes_[src->id()].links_down) {
84       xbt_node_t current = new_xbt_graph_node(graph, link->get_cname(), nodes);
85       new_xbt_graph_edge(graph, previous, current, edges);
86       previous = current;
87     }
88     new_xbt_graph_edge(graph, previous, src_node, edges);
89   }
90 }
91
92 void StarZone::check_add_route_param(const NetPoint* src, const NetPoint* dst, const NetPoint* gw_src,
93                                      const NetPoint* gw_dst, bool symmetrical) const
94 {
95   const char* src_name = src ? src->get_cname() : "nullptr";
96   const char* dst_name = dst ? dst->get_cname() : "nullptr";
97
98   if ((not src && not dst) || (dst && src && src != dst))
99     throw std::invalid_argument(xbt::string_printf(
100         "Cannot add route from %s to %s. In a StarZone, route must be:  i) from source netpoint to everyone, ii) from "
101         "everyone to a single netpoint or iii) loopback, same source and destination",
102         src_name, dst_name));
103
104   if (symmetrical && not src)
105     throw std::invalid_argument(xbt::string_printf("Cannot add route from %s to %s. In a StarZone, symmetrical routes "
106                                                    "must be set from source to everyone (not the contrary)",
107                                                    src_name, dst_name));
108
109   if (src && src->is_netzone()) {
110     if (not gw_src)
111       throw std::invalid_argument(xbt::string_printf(
112           "StarZone::add_route(): source %s is a netzone but gw_src isn't configured", src->get_cname()));
113     if (gw_src->is_netzone())
114       throw std::invalid_argument(
115           xbt::string_printf("StarZone::add_route(): src(%s) is a netzone, gw_src(%s) cannot be a netzone",
116                              src->get_cname(), gw_src->get_cname()));
117   }
118
119   if (dst && dst->is_netzone()) {
120     if (not gw_dst)
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()));
127   }
128 }
129
130 void StarZone::add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
131                          const std::vector<kernel::resource::LinkImpl*>& link_list_, bool symmetrical)
132 {
133   check_add_route_param(src, dst, gw_src, gw_dst, symmetrical);
134
135   s4u::NetZone::on_route_creation(symmetrical, src, dst, gw_src, gw_dst, link_list_);
136
137   /* loopback */
138   if (src == dst) {
139     routes_[src->id()].loopback = link_list_;
140   } else {
141     /* src to everyone */
142     if (src) {
143       auto& route        = routes_[src->id()];
144       route.links_up     = link_list_;
145       route.gateway      = gw_src;
146       route.links_up_set = true;
147       if (symmetrical) {
148         /* reverse it for down/symmetrical links */
149         route.links_down.assign(link_list_.rbegin(), link_list_.rend());
150         route.links_down_set = true;
151       }
152     }
153     /* dst to everyone */
154     if (dst) {
155       auto& route          = routes_[dst->id()];
156       route.links_down     = link_list_;
157       route.gateway        = gw_dst;
158       route.links_down_set = true;
159     }
160   }
161 }
162
163 void StarZone::do_seal()
164 {
165 #if SIMGRID_HAVE_NS3
166   xbt_assert(not dynamic_cast<resource::NetworkNS3Model*>(get_network_model().get()),
167              "StarZone are not supported by NS-3 right now");
168 #endif
169   /* add default empty links if nothing was configured by user */
170   for (auto const& node : get_vertices()) {
171     auto route = routes_.emplace(node->id(), StarRoute());
172     if (route.second) {
173       route.first->second.links_down_set = true;
174       route.first->second.links_up_set   = true;
175     }
176   }
177 }
178
179 } // namespace routing
180 } // namespace kernel
181
182 namespace s4u {
183 NetZone* create_star_zone(const std::string& name)
184 {
185   return (new kernel::routing::StarZone(name))->get_iface();
186 }
187 } // namespace s4u
188
189 } // namespace simgrid