Logo AND Algorithmique Numérique Distribuée

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