Logo AND Algorithmique Numérique Distribuée

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