Logo AND Algorithmique Numérique Distribuée

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