Logo AND Algorithmique Numérique Distribuée

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