Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
51bfe24e493c5a45b2e9a34f3083fcc49cf4e8ae
[simgrid.git] / src / kernel / routing / NetZoneImpl.cpp
1 /* Copyright (c) 2006-2023. 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/NetPoint.hpp>
7 #include <simgrid/kernel/routing/NetZoneImpl.hpp>
8 #include <simgrid/s4u/Engine.hpp>
9 #include <simgrid/s4u/Host.hpp>
10 #include <simgrid/s4u/VirtualMachine.hpp>
11
12 #include "src/include/simgrid/sg_config.hpp"
13 #include "src/kernel/EngineImpl.hpp"
14 #include "src/kernel/resource/CpuImpl.hpp"
15 #include "src/kernel/resource/DiskImpl.hpp"
16 #include "src/kernel/resource/HostImpl.hpp"
17 #include "src/kernel/resource/NetworkModel.hpp"
18 #include "src/kernel/resource/SplitDuplexLinkImpl.hpp"
19 #include "src/kernel/resource/StandardLinkImpl.hpp"
20 #include "src/kernel/resource/VirtualMachineImpl.hpp"
21 #include "xbt/asserts.hpp"
22
23 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_platform, kernel, "Kernel platform-related information");
24
25 namespace simgrid::kernel::routing {
26
27 xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
28                  kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
29                  std::vector<kernel::resource::StandardLinkImpl*> const& link_list)>
30     NetZoneImpl::on_route_creation;
31
32 NetZoneImpl::NetZoneImpl(const std::string& name) : piface_(this), name_(name)
33 {
34   auto* engine = s4u::Engine::get_instance();
35   /* workaroud: first netzoneImpl will be the root netzone.
36    * Without globals and with current model description init functions (see module.hpp), we need
37    * the root netzone to exist when creating the models.
38    * This is usually done at sg_platf.cpp, during XML parsing */
39   if (not engine->get_netzone_root()) {
40     engine->set_netzone_root(&piface_);
41     /* root netzone set, initialize models */
42     simgrid::s4u::Engine::on_platform_creation();
43
44     /* Initialize the models. That must be done after we got all config, and before we need the models.
45      * That is, after the last <config> tag, if any, and before the first of cluster|peer|zone|trace|trace_cb
46      *
47      * I'm not sure for <trace> and <trace_cb>, there may be a bug here
48      * (FIXME: check it out by creating a file beginning with one of these tags)
49      * but cluster and peer come down to zone creations, so putting this verification here is correct.
50      */
51     simgrid_host_models().init_from_flag_value();
52     simgrid_vm_model_init_HL13();
53   }
54
55   xbt_enforce(nullptr == engine->netpoint_by_name_or_null(get_name()),
56              "Refusing to create a second NetZone called '%s'.", get_cname());
57   netpoint_ = new NetPoint(name_, NetPoint::Type::NetZone);
58   XBT_DEBUG("NetZone '%s' created with the id '%lu'", get_cname(), netpoint_->id());
59   _sg_cfg_init_status = 2; /* HACK: direct access to the global controlling the level of configuration to prevent
60                             * any further config now that we created some real content */
61   simgrid::s4u::NetZone::on_creation(piface_); // notify the signal
62 }
63
64 NetZoneImpl::~NetZoneImpl()
65 {
66   for (auto const& nz : children_)
67     delete nz;
68
69   /* Since hosts_ and links_ are a std::map, the hosts are destroyed in the lexicographic order, which ensures that the
70    * output is reproducible.
71    */
72   for (auto const& [_, host] : hosts_) {
73     host->destroy();
74   }
75   hosts_.clear();
76   for (auto const& [_, link] : links_) {
77     link->destroy();
78   }
79   links_.clear();
80
81   for (auto const& [_, route] : bypass_routes_)
82     delete route;
83
84   s4u::Engine::get_instance()->netpoint_unregister(netpoint_);
85 }
86
87 xbt_node_t NetZoneImpl::new_xbt_graph_node(const s_xbt_graph_t* graph, const char* name,
88                                            std::map<std::string, xbt_node_t, std::less<>>* nodes)
89 {
90   auto [elm, inserted] = nodes->try_emplace(name);
91   if (inserted)
92     elm->second = xbt_graph_new_node(graph, xbt_strdup(name));
93   return elm->second;
94 }
95
96 xbt_edge_t NetZoneImpl::new_xbt_graph_edge(const s_xbt_graph_t* graph, xbt_node_t src, xbt_node_t dst,
97                                            std::map<std::string, xbt_edge_t, std::less<>>* edges)
98 {
99   const auto* src_name = static_cast<const char*>(xbt_graph_node_get_data(src));
100   const auto* dst_name = static_cast<const char*>(xbt_graph_node_get_data(dst));
101
102   auto elm = edges->find(std::string(src_name) + dst_name);
103   if (elm == edges->end()) {
104     bool inserted;
105     std::tie(elm, inserted) = edges->try_emplace(std::string(dst_name) + src_name);
106     if (inserted)
107       elm->second = xbt_graph_new_edge(graph, src, dst, nullptr);
108   }
109
110   return elm->second;
111 }
112
113 void NetZoneImpl::add_child(NetZoneImpl* new_zone)
114 {
115   xbt_enforce(not sealed_, "Cannot add a new child to the sealed zone %s", get_cname());
116   /* set the parent behavior */
117   hierarchy_ = RoutingMode::recursive;
118   children_.push_back(new_zone);
119 }
120
121 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
122  *
123  * Only the hosts that are directly contained in this NetZone are retrieved,
124  * not the ones contained in sub-netzones.
125  */
126 std::vector<s4u::Host*> NetZoneImpl::get_all_hosts() const
127 {
128   return s4u::Engine::get_instance()->get_filtered_hosts(
129       [this](const s4u::Host* host) { return host->get_impl()->get_englobing_zone() == this; });
130 }
131 size_t NetZoneImpl::get_host_count() const
132 {
133   return get_all_hosts().size();
134 }
135
136 std::vector<s4u::Link*> NetZoneImpl::get_filtered_links(const std::function<bool(s4u::Link*)>& filter) const
137 {
138   std::vector<s4u::Link*> filtered_list;
139   for (auto const& [_, link] : links_) {
140     s4u::Link* l = link->get_iface();
141     if (filter(l))
142       filtered_list.push_back(l);
143   }
144
145   for (const auto* child : children_) {
146     auto child_links = child->get_filtered_links(filter);
147     filtered_list.insert(filtered_list.end(), std::make_move_iterator(child_links.begin()),
148                          std::make_move_iterator(child_links.end()));
149   }
150   return filtered_list;
151 }
152
153 std::vector<s4u::Link*> NetZoneImpl::get_all_links() const
154 {
155   return get_filtered_links([](const s4u::Link*) { return true; });
156 }
157
158 size_t NetZoneImpl::get_link_count() const
159 {
160   size_t total = links_.size();
161   for (const auto* child : children_) {
162     total += child->get_link_count();
163   }
164   return total;
165 }
166
167 s4u::Host* NetZoneImpl::create_host(const std::string& name, const std::vector<double>& speed_per_pstate)
168 {
169   xbt_enforce(cpu_model_pm_,
170              "Impossible to create host: %s. Invalid CPU model: nullptr. Have you set the parent of this NetZone: %s?",
171              name.c_str(), get_cname());
172   xbt_enforce(not sealed_, "Impossible to create host: %s. NetZone %s already sealed", name.c_str(), get_cname());
173   auto* host   = (new resource::HostImpl(name))->set_englobing_zone(this);
174   hosts_[name] = host;
175   host->get_iface()->set_netpoint((new NetPoint(name, NetPoint::Type::Host))->set_englobing_zone(this));
176
177   cpu_model_pm_->create_cpu(host->get_iface(), speed_per_pstate);
178
179   return host->get_iface();
180 }
181
182 resource::StandardLinkImpl* NetZoneImpl::do_create_link(const std::string& name, const std::vector<double>& bandwidths)
183 {
184   return network_model_->create_link(name, bandwidths);
185 }
186
187 s4u::Link* NetZoneImpl::create_link(const std::string& name, const std::vector<double>& bandwidths)
188 {
189   xbt_enforce(
190       network_model_,
191       "Impossible to create link: %s. Invalid network model: nullptr. Have you set the parent of this NetZone: %s?",
192       name.c_str(), get_cname());
193   xbt_enforce(not sealed_, "Impossible to create link: %s. NetZone %s already sealed", name.c_str(), get_cname());
194   links_[name] = do_create_link(name, bandwidths)->set_englobing_zone(this);
195   return links_[name]->get_iface();
196 }
197
198 s4u::SplitDuplexLink* NetZoneImpl::create_split_duplex_link(const std::string& name,
199                                                             const std::vector<double>& bandwidths)
200 {
201   xbt_enforce(
202       network_model_,
203       "Impossible to create link: %s. Invalid network model: nullptr. Have you set the parent of this NetZone: %s?",
204       name.c_str(), get_cname());
205   xbt_enforce(not sealed_, "Impossible to create link: %s. NetZone %s already sealed", name.c_str(), get_cname());
206
207   auto* link_up             = create_link(name + "_UP", bandwidths)->get_impl()->set_englobing_zone(this);
208   auto* link_down           = create_link(name + "_DOWN", bandwidths)->get_impl()->set_englobing_zone(this);
209   split_duplex_links_[name] = std::make_unique<resource::SplitDuplexLinkImpl>(name, link_up, link_down);
210   return split_duplex_links_[name]->get_iface();
211 }
212
213 s4u::Disk* NetZoneImpl::create_disk(const std::string& name, double read_bandwidth, double write_bandwidth)
214 {
215   xbt_enforce(disk_model_,
216              "Impossible to create disk: %s. Invalid disk model: nullptr. Have you set the parent of this NetZone: %s?",
217              name.c_str(), get_cname());
218   xbt_enforce(not sealed_, "Impossible to create disk: %s. NetZone %s already sealed", name.c_str(), get_cname());
219   auto* l = disk_model_->create_disk(name, read_bandwidth, write_bandwidth);
220
221   return l->get_iface();
222 }
223
224 NetPoint* NetZoneImpl::create_router(const std::string& name)
225 {
226   xbt_enforce(nullptr == s4u::Engine::get_instance()->netpoint_by_name_or_null(name),
227              "Refusing to create a router named '%s': this name already describes a node.", name.c_str());
228   xbt_enforce(not sealed_, "Impossible to create router: %s. NetZone %s already sealed", name.c_str(), get_cname());
229
230   return (new NetPoint(name, NetPoint::Type::Router))->set_englobing_zone(this);
231 }
232
233 unsigned long NetZoneImpl::add_component(NetPoint* elm)
234 {
235   vertices_.push_back(elm);
236   return vertices_.size() - 1; // The rank of the newly created object
237 }
238
239 std::vector<resource::StandardLinkImpl*> NetZoneImpl::get_link_list_impl(const std::vector<s4u::LinkInRoute>& link_list,
240                                                                          bool backroute) const
241 {
242   std::vector<resource::StandardLinkImpl*> links;
243
244   for (const auto& link : link_list) {
245     if (link.get_link()->get_sharing_policy() != s4u::Link::SharingPolicy::SPLITDUPLEX) {
246       links.push_back(link.get_link()->get_impl());
247       continue;
248     }
249     // split-duplex links
250     const auto* sd_link = dynamic_cast<const s4u::SplitDuplexLink*>(link.get_link());
251     xbt_enforce(sd_link,
252                "Add_route: cast to SpliDuplexLink impossible. This should not happen, please contact SimGrid team");
253     resource::StandardLinkImpl* link_impl;
254     switch (link.get_direction()) {
255       case s4u::LinkInRoute::Direction::UP:
256         if (backroute)
257           link_impl = sd_link->get_link_down()->get_impl();
258         else
259           link_impl = sd_link->get_link_up()->get_impl();
260         break;
261       case s4u::LinkInRoute::Direction::DOWN:
262         if (backroute)
263           link_impl = sd_link->get_link_up()->get_impl();
264         else
265           link_impl = sd_link->get_link_down()->get_impl();
266         break;
267       default:
268         throw std::invalid_argument("Invalid add_route. Split-Duplex link without a direction: " +
269                                     link.get_link()->get_name());
270     }
271     links.push_back(link_impl);
272   }
273   return links;
274 }
275
276 resource::StandardLinkImpl* NetZoneImpl::get_link_by_name_or_null(const std::string& name) const
277 {
278   if (auto link_it = links_.find(name); link_it != links_.end())
279     return link_it->second;
280
281   for (const auto* child : children_) {
282     if (auto* link = child->get_link_by_name_or_null(name))
283       return link;
284   }
285
286   return nullptr;
287 }
288
289 resource::SplitDuplexLinkImpl* NetZoneImpl::get_split_duplex_link_by_name_or_null(const std::string& name) const
290 {
291   if (auto link_it = split_duplex_links_.find(name); link_it != split_duplex_links_.end())
292     return link_it->second.get();
293
294   for (const auto* child : children_) {
295     if (auto* link = child->get_split_duplex_link_by_name_or_null(name))
296       return link;
297   }
298
299   return nullptr;
300 }
301
302 resource::HostImpl* NetZoneImpl::get_host_by_name_or_null(const std::string& name) const
303 {
304   if (auto host_it = hosts_.find(name); host_it != hosts_.end())
305     return host_it->second;
306
307   for (const auto* child : children_) {
308     if (auto* host = child->get_host_by_name_or_null(name))
309       return host;
310   }
311
312   return nullptr;
313 }
314
315 std::vector<s4u::Host*> NetZoneImpl::get_filtered_hosts(const std::function<bool(s4u::Host*)>& filter) const
316 {
317   std::vector<s4u::Host*> filtered_list;
318   for (auto const& [_, host] : hosts_) {
319     s4u::Host* h = host->get_iface();
320     if (filter(h))
321       filtered_list.push_back(h);
322     /* Engine::get_hosts returns the VMs too */
323     for (auto* vm : h->get_impl()->get_vms()) {
324       if (filter(vm))
325         filtered_list.push_back(vm);
326     }
327   }
328
329   for (const auto* child : children_) {
330     auto child_links = child->get_filtered_hosts(filter);
331     filtered_list.insert(filtered_list.end(), std::make_move_iterator(child_links.begin()),
332                          std::make_move_iterator(child_links.end()));
333   }
334   return filtered_list;
335 }
336
337 void NetZoneImpl::add_route(NetPoint* /*src*/, NetPoint* /*dst*/, NetPoint* /*gw_src*/, NetPoint* /*gw_dst*/,
338                             const std::vector<s4u::LinkInRoute>& /*link_list_*/, bool /*symmetrical*/)
339 {
340   xbt_die("NetZone '%s' does not accept new routes (wrong class).", get_cname());
341 }
342
343 void NetZoneImpl::add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
344                                    const std::vector<s4u::LinkInRoute>& link_list)
345 {
346   /* Argument validity checks */
347   if (gw_dst) {
348     XBT_DEBUG("Load bypassNetzoneRoute from %s@%s to %s@%s", src->get_cname(), gw_src->get_cname(), dst->get_cname(),
349               gw_dst->get_cname());
350     xbt_enforce(not link_list.empty(), "Bypass route between %s@%s and %s@%s cannot be empty.", src->get_cname(),
351                gw_src->get_cname(), dst->get_cname(), gw_dst->get_cname());
352     xbt_enforce(bypass_routes_.find({src, dst}) == bypass_routes_.end(),
353                "The bypass route between %s@%s and %s@%s already exists.", src->get_cname(), gw_src->get_cname(),
354                dst->get_cname(), gw_dst->get_cname());
355   } else {
356     XBT_DEBUG("Load bypassRoute from %s to %s", src->get_cname(), dst->get_cname());
357     xbt_enforce(not link_list.empty(), "Bypass route between %s and %s cannot be empty.", src->get_cname(),
358                dst->get_cname());
359     xbt_enforce(bypass_routes_.find({src, dst}) == bypass_routes_.end(),
360                "The bypass route between %s and %s already exists.", src->get_cname(), dst->get_cname());
361   }
362
363   /* Build a copy that will be stored in the dict */
364   auto* newRoute = new BypassRoute(gw_src, gw_dst);
365   auto converted_list = get_link_list_impl(link_list, false);
366   newRoute->links.insert(newRoute->links.end(), begin(converted_list), end(converted_list));
367
368   /* Store it */
369   bypass_routes_.try_emplace({src, dst}, newRoute);
370 }
371
372 /** @brief Get the common ancestor and its first children in each line leading to src and dst
373  *
374  * In the recursive case, this sets common_ancestor, src_ancestor and dst_ancestor are set as follows.
375  * @verbatim
376  *         platform root
377  *               |
378  *              ...                <- possibly long path
379  *               |
380  *         common_ancestor
381  *           /        \
382  *          /          \
383  *         /            \          <- direct links
384  *        /              \
385  *       /                \
386  *  src_ancestor     dst_ancestor  <- must be different in the recursive case
387  *      |                   |
388  *     ...                 ...     <-- possibly long paths (one hop or more)
389  *      |                   |
390  *     src                 dst
391  *  @endverbatim
392  *
393  *  In the base case (when src and dst are in the same netzone), things are as follows:
394  *  @verbatim
395  *                  platform root
396  *                        |
397  *                       ...                      <-- possibly long path
398  *                        |
399  * common_ancestor==src_ancestor==dst_ancestor    <-- all the same value
400  *                   /        \
401  *                  /          \                  <-- direct links (exactly one hop)
402  *                 /            \
403  *              src              dst
404  *  @endverbatim
405  *
406  * A specific recursive case occurs when src is the ancestor of dst. In this case,
407  * the base case routing should be used so the common_ancestor is specifically set
408  * to src_ancestor==dst_ancestor.
409  * Naturally, things are completely symmetrical if dst is the ancestor of src.
410  * @verbatim
411  *            platform root
412  *                  |
413  *                 ...                <-- possibly long path
414  *                  |
415  *  src == src_ancestor==dst_ancestor==common_ancestor <-- same value
416  *                  |
417  *                 ...                <-- possibly long path (one hop or more)
418  *                  |
419  *                 dst
420  *  @endverbatim
421  */
422 static void find_common_ancestors(const NetPoint* src, const NetPoint* dst,
423                                   /* OUT */ NetZoneImpl** common_ancestor, NetZoneImpl** src_ancestor,
424                                   NetZoneImpl** dst_ancestor)
425 {
426   /* Deal with the easy base case */
427   if (src->get_englobing_zone() == dst->get_englobing_zone()) {
428     *common_ancestor = src->get_englobing_zone();
429     *src_ancestor    = *common_ancestor;
430     *dst_ancestor    = *common_ancestor;
431     return;
432   }
433
434   /* engage the full recursive search */
435
436   /* (1) find the path to root of src and dst*/
437   const NetZoneImpl* src_as = src->get_englobing_zone();
438   const NetZoneImpl* dst_as = dst->get_englobing_zone();
439
440   xbt_enforce(src_as, "Host %s must be in a netzone", src->get_cname());
441   xbt_enforce(dst_as, "Host %s must be in a netzone", dst->get_cname());
442
443   /* (2) find the path to the root routing component */
444   std::vector<NetZoneImpl*> path_src;
445   NetZoneImpl* current = src->get_englobing_zone();
446   while (current != nullptr) {
447     path_src.push_back(current);
448     current = current->get_parent();
449   }
450   std::vector<NetZoneImpl*> path_dst;
451   current = dst->get_englobing_zone();
452   while (current != nullptr) {
453     path_dst.push_back(current);
454     current = current->get_parent();
455   }
456
457   /* (3) find the common parent.
458    * Before that, index_src and index_dst may be different, they both point to nullptr in path_src/path_dst
459    * So we move them down simultaneously as long as they point to the same content.
460    *
461    * This works because all SimGrid platform have a unique root element (that is the last element of both paths).
462    */
463   NetZoneImpl* parent = nullptr; // the netzone we dropped on the previous loop iteration
464   while (path_src.size() > 1 && path_dst.size() > 1 && path_src.back() == path_dst.back()) {
465     parent = path_src.back();
466     path_src.pop_back();
467     path_dst.pop_back();
468   }
469
470   /* (4) we found the difference at least. Finalize the returned values */
471   *src_ancestor = path_src.back();                  /* the first different parent of src */
472   *dst_ancestor = path_dst.back();                  /* the first different parent of dst */
473   if (*src_ancestor == *dst_ancestor) {             // src is the ancestor of dst, or the contrary
474     *common_ancestor = *src_ancestor;
475   } else {
476     xbt_enforce(parent != nullptr);
477     *common_ancestor = parent;
478   }
479 }
480
481 /* PRECONDITION: this is the common ancestor of src and dst */
482 bool NetZoneImpl::get_bypass_route(const NetPoint* src, const NetPoint* dst,
483                                    /* OUT */ std::vector<resource::StandardLinkImpl*>& links, double* latency,
484                                    std::unordered_set<NetZoneImpl*>& netzones)
485 {
486   // If never set a bypass route return nullptr without any further computations
487   if (bypass_routes_.empty())
488     return false;
489
490   /* Base case, no recursion is needed */
491   if (dst->get_englobing_zone() == this && src->get_englobing_zone() == this) {
492     if (bypass_routes_.find({src, dst}) != bypass_routes_.end()) {
493       const BypassRoute* bypassedRoute = bypass_routes_.at({src, dst});
494       add_link_latency(links, bypassedRoute->links, latency);
495       XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links", src->get_cname(), dst->get_cname(),
496                 bypassedRoute->links.size());
497       return true;
498     }
499     return false;
500   }
501
502   /* Engage recursive search */
503
504   /* (1) find the path to the root routing component */
505   std::vector<NetZoneImpl*> path_src;
506   NetZoneImpl* current = src->get_englobing_zone();
507   while (current != nullptr) {
508     path_src.push_back(current);
509     current = current->parent_;
510   }
511
512   std::vector<NetZoneImpl*> path_dst;
513   current = dst->get_englobing_zone();
514   while (current != nullptr) {
515     path_dst.push_back(current);
516     current = current->parent_;
517   }
518
519   /* (2) find the common parent */
520   while (path_src.size() > 1 && path_dst.size() > 1 && path_src.back() == path_dst.back()) {
521     path_src.pop_back();
522     path_dst.pop_back();
523   }
524
525   /* (3) Search for a bypass making the path up to the ancestor useless */
526   const BypassRoute* bypassedRoute = nullptr;
527   std::pair<kernel::routing::NetPoint*, kernel::routing::NetPoint*> key;
528   // Search for a bypass with the given indices. Returns true if found. Initialize variables `bypassedRoute' and `key'.
529   auto lookup = [&bypassedRoute, &key, &path_src, &path_dst, this](unsigned src_index, unsigned dst_index) {
530     if (src_index < path_src.size() && dst_index < path_dst.size()) {
531       key      = {path_src[src_index]->netpoint_, path_dst[dst_index]->netpoint_};
532       auto bpr = bypass_routes_.find(key);
533       if (bpr != bypass_routes_.end()) {
534         bypassedRoute = bpr->second;
535         return true;
536       }
537     }
538     return false;
539   };
540
541   for (unsigned max = 0, max_index = std::max(path_src.size(), path_dst.size()); max < max_index; max++) {
542     for (unsigned i = 0; i < max; i++) {
543       if (lookup(i, max) || lookup(max, i))
544         break;
545     }
546     if (bypassedRoute || lookup(max, max))
547       break;
548   }
549
550   /* (4) If we have the bypass, use it. If not, caller will do the Right Thing. */
551   if (bypassedRoute) {
552     XBT_DEBUG("Found a bypass route from '%s' to '%s' with %zu links. We may have to complete it with recursive "
553               "calls to getRoute",
554               src->get_cname(), dst->get_cname(), bypassedRoute->links.size());
555     if (src != key.first)
556       get_global_route_with_netzones(src, bypassedRoute->gw_src, links, latency, netzones);
557     add_link_latency(links, bypassedRoute->links, latency);
558     if (dst != key.second)
559       get_global_route_with_netzones(bypassedRoute->gw_dst, dst, links, latency, netzones);
560     return true;
561   }
562   XBT_DEBUG("No bypass route from '%s' to '%s'.", src->get_cname(), dst->get_cname());
563   return false;
564 }
565
566 void NetZoneImpl::get_global_route(const NetPoint* src, const NetPoint* dst,
567                                    /* OUT */ std::vector<resource::StandardLinkImpl*>& links, double* latency)
568 {
569   std::unordered_set<NetZoneImpl*> netzones;
570   get_global_route_with_netzones(src, dst, links, latency, netzones);
571 }
572
573 void NetZoneImpl::get_global_route_with_netzones(const NetPoint* src, const NetPoint* dst,
574                                                  /* OUT */ std::vector<resource::StandardLinkImpl*>& links,
575                                                  double* latency, std::unordered_set<NetZoneImpl*>& netzones)
576 {
577   Route route;
578
579   XBT_DEBUG("Resolve route from '%s' to '%s'", src->get_cname(), dst->get_cname());
580
581   /* Find how src and dst are interconnected */
582   NetZoneImpl* common_ancestor;
583   NetZoneImpl* src_ancestor;
584   NetZoneImpl* dst_ancestor;
585   find_common_ancestors(src, dst, &common_ancestor, &src_ancestor, &dst_ancestor);
586   XBT_DEBUG("find_common_ancestors: common ancestor '%s' src ancestor '%s' dst ancestor '%s'",
587             common_ancestor->get_cname(), src_ancestor->get_cname(), dst_ancestor->get_cname());
588
589   netzones.insert(src->get_englobing_zone());
590   netzones.insert(dst->get_englobing_zone());
591   netzones.insert(common_ancestor);
592   /* Check whether a direct bypass is defined. If so, use it and bail out */
593   if (common_ancestor->get_bypass_route(src, dst, links, latency, netzones))
594     return;
595
596   /* If src and dst are in the same netzone, life is good */
597   if (src_ancestor == dst_ancestor) { /* SURF_ROUTING_BASE */
598     route.link_list_ = std::move(links);
599     common_ancestor->get_local_route(src, dst, &route, latency);
600     links = std::move(route.link_list_);
601     return;
602   }
603
604   /* Not in the same netzone, no bypass. We'll have to find our path between the netzones recursively */
605   common_ancestor->get_local_route(src_ancestor->netpoint_, dst_ancestor->netpoint_, &route, latency);
606   xbt_enforce((route.gw_src_ != nullptr) && (route.gw_dst_ != nullptr), "Bad gateways for route from '%s' to '%s'.",
607              src->get_cname(), dst->get_cname());
608
609   /* If source gateway is not our source, we have to recursively find our way up to this point */
610   if (src != route.gw_src_)
611     get_global_route_with_netzones(src, route.gw_src_, links, latency, netzones);
612   links.insert(links.end(), begin(route.link_list_), end(route.link_list_));
613
614   /* If dest gateway is not our destination, we have to recursively find our way from this point */
615   if (route.gw_dst_ != dst)
616     get_global_route_with_netzones(route.gw_dst_, dst, links, latency, netzones);
617 }
618
619 void NetZoneImpl::get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
620                             std::map<std::string, xbt_edge_t, std::less<>>* edges)
621 {
622   std::vector<NetPoint*> vertices = get_vertices();
623
624   for (auto const& my_src : vertices) {
625     for (auto const& my_dst : vertices) {
626       if (my_src == my_dst)
627         continue;
628
629       Route route;
630
631       get_local_route(my_src, my_dst, &route, nullptr);
632
633       XBT_DEBUG("get_route_and_latency %s -> %s", my_src->get_cname(), my_dst->get_cname());
634
635       xbt_node_t current;
636       xbt_node_t previous;
637       const char* previous_name;
638       const char* current_name;
639
640       if (route.gw_src_) {
641         previous      = new_xbt_graph_node(graph, route.gw_src_->get_cname(), nodes);
642         previous_name = route.gw_src_->get_cname();
643       } else {
644         previous      = new_xbt_graph_node(graph, my_src->get_cname(), nodes);
645         previous_name = my_src->get_cname();
646       }
647
648       for (auto const& link : route.link_list_) {
649         const char* link_name = link->get_cname();
650         current               = new_xbt_graph_node(graph, link_name, nodes);
651         current_name          = link_name;
652         new_xbt_graph_edge(graph, previous, current, edges);
653         XBT_DEBUG("  %s -> %s", previous_name, current_name);
654         previous      = current;
655         previous_name = current_name;
656       }
657
658       if (route.gw_dst_) {
659         current      = new_xbt_graph_node(graph, route.gw_dst_->get_cname(), nodes);
660         current_name = route.gw_dst_->get_cname();
661       } else {
662         current      = new_xbt_graph_node(graph, my_dst->get_cname(), nodes);
663         current_name = my_dst->get_cname();
664       }
665       new_xbt_graph_edge(graph, previous, current, edges);
666       XBT_DEBUG("  %s -> %s", previous_name, current_name);
667     }
668   }
669 }
670
671 void NetZoneImpl::seal()
672 {
673   /* already sealed netzone */
674   if (sealed_)
675     return;
676   do_seal(); // derived class' specific sealing procedure
677
678   /* seals sub-netzones and hosts */
679   for (auto* host : get_all_hosts()) {
680     host->seal();
681   }
682
683   /* sealing links */
684   for (auto const& [_, link] : links_)
685     link->get_iface()->seal();
686
687   for (auto* sub_net : get_children()) {
688     sub_net->seal();
689   }
690   sealed_ = true;
691   s4u::NetZone::on_seal(piface_);
692 }
693
694 void NetZoneImpl::set_parent(NetZoneImpl* parent)
695 {
696   xbt_enforce(not sealed_, "Impossible to set parent to an already sealed NetZone(%s)", this->get_cname());
697   parent_ = parent;
698   netpoint_->set_englobing_zone(parent_);
699   if (parent) {
700     /* adding this class as child */
701     parent->add_child(this);
702     /* copying models from parent host, to be reviewed when we allow multi-models */
703     set_network_model(parent->get_network_model());
704     set_cpu_pm_model(parent->get_cpu_pm_model());
705     set_cpu_vm_model(parent->get_cpu_vm_model());
706     set_disk_model(parent->get_disk_model());
707     set_host_model(parent->get_host_model());
708   }
709 }
710
711 void NetZoneImpl::set_network_model(std::shared_ptr<resource::NetworkModel> netmodel)
712 {
713   xbt_enforce(not sealed_, "Impossible to set network model to an already sealed NetZone(%s)", this->get_cname());
714   network_model_ = std::move(netmodel);
715 }
716
717 void NetZoneImpl::set_cpu_vm_model(std::shared_ptr<resource::CpuModel> cpu_model)
718 {
719   xbt_enforce(not sealed_, "Impossible to set CPU model to an already sealed NetZone(%s)", this->get_cname());
720   cpu_model_vm_ = std::move(cpu_model);
721 }
722
723 void NetZoneImpl::set_cpu_pm_model(std::shared_ptr<resource::CpuModel> cpu_model)
724 {
725   xbt_enforce(not sealed_, "Impossible to set CPU model to an already sealed NetZone(%s)", this->get_cname());
726   cpu_model_pm_ = std::move(cpu_model);
727 }
728
729 void NetZoneImpl::set_disk_model(std::shared_ptr<resource::DiskModel> disk_model)
730 {
731   xbt_enforce(not sealed_, "Impossible to set disk model to an already sealed NetZone(%s)", this->get_cname());
732   disk_model_ = std::move(disk_model);
733 }
734
735 void NetZoneImpl::set_host_model(std::shared_ptr<resource::HostModel> host_model)
736 {
737   xbt_enforce(not sealed_, "Impossible to set host model to an already sealed NetZone(%s)", this->get_cname());
738   host_model_ = std::move(host_model);
739 }
740
741 const NetZoneImpl* NetZoneImpl::get_netzone_recursive(const NetPoint* netpoint) const
742 {
743   xbt_enforce(netpoint && netpoint->is_netzone(), "Netpoint %s must be of the type NetZone",
744              netpoint ? netpoint->get_cname() : "nullptr");
745
746   if (netpoint == netpoint_)
747     return this;
748
749   for (const auto* children : children_) {
750     const NetZoneImpl* netzone = children->get_netzone_recursive(netpoint);
751     if (netzone)
752       return netzone;
753   }
754   return nullptr;
755 }
756
757 bool NetZoneImpl::is_component_recursive(const NetPoint* netpoint) const
758 {
759   /* check direct components */
760   if (std::any_of(begin(vertices_), end(vertices_), [netpoint](const auto* elem) { return elem == netpoint; }))
761     return true;
762
763   /* check childrens */
764   return std::any_of(begin(children_), end(children_),
765                      [netpoint](const auto* child) { return child->is_component_recursive(netpoint); });
766 }
767 } // namespace simgrid::kernel::routing