Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / include / simgrid / kernel / routing / NetZoneImpl.hpp
1 /* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved.               */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_ROUTING_NETZONEIMPL_HPP
7 #define SIMGRID_ROUTING_NETZONEIMPL_HPP
8
9 #include <simgrid/forward.h>
10 #include <simgrid/s4u/Link.hpp>
11 #include <simgrid/s4u/NetZone.hpp>
12 #include <xbt/PropertyHolder.hpp>
13 #include <xbt/graph.h>
14
15 #include <map>
16 #include <unordered_set>
17 #include <vector>
18
19 namespace simgrid::kernel::routing {
20
21 class Route {
22 public:
23   Route() = default;
24   explicit Route(NetPoint* src, NetPoint* dst, NetPoint* gwSrc, NetPoint* gwDst)
25       : src_(src), dst_(dst), gw_src_(gwSrc), gw_dst_(gwDst)
26   {
27   }
28   NetPoint* src_    = nullptr;
29   NetPoint* dst_    = nullptr;
30   NetPoint* gw_src_ = nullptr;
31   NetPoint* gw_dst_ = nullptr;
32   std::vector<resource::StandardLinkImpl*> link_list_;
33 };
34
35 class BypassRoute {
36 public:
37   explicit BypassRoute(NetPoint* gwSrc, NetPoint* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
38   NetPoint* gw_src;
39   NetPoint* gw_dst;
40   std::vector<resource::StandardLinkImpl*> links;
41 };
42
43 /** @ingroup ROUTING_API
44  *  @brief Private implementation of the Networking Zones
45  *
46  * A netzone is a network container, in charge of routing information between elements (hosts and sub-netzones)
47  * and to the nearby netzones. In SimGrid, there is a hierarchy of netzones, ie a tree with a unique root
48  * NetZone, that you can retrieve with simgrid::s4u::Engine::netRoot().
49  *
50  * The purpose of the kernel::routing module is to retrieve the routing path between two points in a time- and
51  * space-efficient manner. This is done by NetZoneImpl::getGlobalRoute(), called when creating a communication to
52  * retrieve both the list of links that the create communication will use, and the summed latency that these
53  * links represent.
54  *
55  * The network model could recompute the latency by itself from the list, but it would require an additional
56  * traversal of the link set. This operation being on the critical path of SimGrid, the routing computes the
57  * latency on the behalf of the network while constructing the link set.
58  *
59  * Finding the path between two nodes is rather complex because we navigate a hierarchy of netzones, each of them
60  * being a full network. In addition, the routing can declare shortcuts (called bypasses), either within a NetZone
61  * at the route level or directly between NetZones. Also, each NetZone can use a differing routing algorithm, depending
62  * on its class. @ref FullZone have a full matrix giving explicitly the path between any pair of their
63  * contained nodes, while @ref DijkstraZone or @ref FloydZone rely on a shortest path algorithm. @ref VivaldiZone
64  * does not even have any link but only use only coordinate information to compute the latency.
65  *
66  * So NetZoneImpl::getGlobalRoute builds the path recursively asking its specific information to each traversed NetZone
67  * with NetZoneImpl::getLocalRoute, that is redefined in each sub-class.
68  * The algorithm for that is explained in http://hal.inria.fr/hal-00650233/ (but for historical reasons, NetZones are
69  * called Autonomous Systems in this article).
70  *
71  */
72 class XBT_PUBLIC NetZoneImpl : public xbt::PropertyHolder {
73   friend EngineImpl; // it destroys netRoot_
74   s4u::NetZone piface_;
75
76   // our content, as known to our graph routing algorithm (maps vertex_id -> vertex)
77   std::vector<kernel::routing::NetPoint*> vertices_;
78   std::map<std::string, resource::StandardLinkImpl*, std::less<>> links_;
79   /* save split-duplex links separately, keep links_ with only LinkImpl* seen by the user
80    * members of a split-duplex are saved in the links_ */
81   std::map<std::string, std::unique_ptr<resource::SplitDuplexLinkImpl>, std::less<>> split_duplex_links_;
82   std::map<std::string, resource::HostImpl*, std::less<>> hosts_;
83
84   NetZoneImpl* parent_ = nullptr;
85   std::vector<NetZoneImpl*> children_; // sub-netzones
86   std::string name_;
87   bool sealed_ = false; // We cannot add more content when sealed
88
89   std::map<std::pair<const NetPoint*, const NetPoint*>, BypassRoute*> bypass_routes_; // src x dst -> route
90   routing::NetPoint* netpoint_ = nullptr; // Our representative in the parent NetZone
91
92 protected:
93   explicit NetZoneImpl(const std::string& name);
94   NetZoneImpl(const NetZoneImpl&) = delete;
95   NetZoneImpl& operator=(const NetZoneImpl&) = delete;
96   virtual ~NetZoneImpl();
97
98   /**
99    * @brief Probe the routing path between two points that are local to the called NetZone.
100    *
101    * @param src where from
102    * @param dst where to
103    * @param into Container into which the traversed links and gateway information should be pushed
104    * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
105    */
106   virtual void get_local_route(const NetPoint* src, const NetPoint* dst, Route* into, double* latency) = 0;
107   /** @brief retrieves the list of all routes of size 1 (of type src x dst x Link) */
108   /* returns whether we found a bypass path */
109   bool get_bypass_route(const routing::NetPoint* src, const routing::NetPoint* dst,
110                         /* OUT */ std::vector<resource::StandardLinkImpl*>& links, double* latency,
111                         std::unordered_set<NetZoneImpl*>& netzones);
112
113   /** @brief Get the NetZone that is represented by the netpoint */
114   const NetZoneImpl* get_netzone_recursive(const NetPoint* netpoint) const;
115
116   /** @brief Get the list of LinkImpl* to add in a route, considering split-duplex links and the direction */
117   std::vector<resource::StandardLinkImpl*> get_link_list_impl(const std::vector<s4u::LinkInRoute>& link_list,
118                                                               bool backroute) const;
119
120   static xbt_node_t new_xbt_graph_node(const s_xbt_graph_t* graph, const char* name,
121                                        std::map<std::string, xbt_node_t, std::less<>>* nodes);
122   static xbt_edge_t new_xbt_graph_edge(const s_xbt_graph_t* graph, xbt_node_t src, xbt_node_t dst,
123                                        std::map<std::string, xbt_edge_t, std::less<>>* edges);
124
125 public:
126   enum class RoutingMode {
127     base,     /**< Base case: use simple link lists for routing     */
128     recursive /**< Recursive case: also return gateway information  */
129   };
130
131   /** @brief Retrieves the network model associated to this NetZone */
132   const std::shared_ptr<resource::NetworkModel>& get_network_model() const { return network_model_; }
133   /** @brief Retrieves the CPU model for virtual machines associated to this NetZone */
134   const std::shared_ptr<resource::CpuModel>& get_cpu_vm_model() const { return cpu_model_vm_; }
135   /** @brief Retrieves the CPU model for physical machines associated to this NetZone */
136   const std::shared_ptr<resource::CpuModel>& get_cpu_pm_model() const { return cpu_model_pm_; }
137   /** @brief Retrieves the disk model associated to this NetZone */
138   const std::shared_ptr<resource::DiskModel>& get_disk_model() const { return disk_model_; }
139   /** @brief Retrieves the host model associated to this NetZone */
140   const std::shared_ptr<resource::HostModel>& get_host_model() const { return host_model_; }
141
142   const s4u::NetZone* get_iface() const { return &piface_; }
143   s4u::NetZone* get_iface() { return &piface_; }
144   unsigned int get_table_size() const { return vertices_.size(); }
145   std::vector<kernel::routing::NetPoint*> get_vertices() const { return vertices_; }
146   NetZoneImpl* get_parent() const { return parent_; }
147   /** @brief Returns the list of direct children (no grand-children). This returns the internal data, no copy.
148    * Don't mess with it.*/
149   const std::vector<NetZoneImpl*>& get_children() const { return children_; }
150   /** @brief Get current netzone hierarchy */
151   RoutingMode get_hierarchy() const { return hierarchy_; }
152
153   /** @brief Retrieves the name of that netzone as a C++ string */
154   const std::string& get_name() const { return name_; }
155   /** @brief Retrieves the name of that netzone as a C string */
156   const char* get_cname() const { return name_.c_str(); };
157
158   /** @brief Gets the netpoint associated to this netzone */
159   kernel::routing::NetPoint* get_netpoint() const { return netpoint_; }
160
161   std::vector<s4u::Host*> get_all_hosts() const;
162   size_t get_host_count() const;
163
164   /**
165    * @brief Recursively gets all links declared in this netzone
166    *
167    * Include children netzones.
168    * @return List of links
169    */
170   std::vector<s4u::Link*> get_all_links() const;
171   /**
172    * @brief Recursively gets all links declared in this netzone.
173    *
174    * Using a filter function
175    * Include children netzones.
176    * @param filter Select links based on this filter
177    * @return List of links
178    */
179   std::vector<s4u::Link*> get_filtered_links(const std::function<bool(s4u::Link*)>& filter) const;
180   /** @brief Get total number of links declared in this netzone (and its children) */
181   size_t get_link_count() const;
182
183   /**
184    * @brief Searches by the link by its name inside this netzone.
185    * Recursively searches in children netzones
186    *
187    * @param name Link name
188    * @return Link object or nullptr if not found
189    */
190   resource::StandardLinkImpl* get_link_by_name_or_null(const std::string& name) const;
191
192   /**
193    * @brief Searches for split-duplex links by its name inside this netzone.
194    * Recursively searches in child netzones
195    *
196    * @param name Split-duplex Link name
197    * @return Link object or nullptr if not found
198    */
199   resource::SplitDuplexLinkImpl* get_split_duplex_link_by_name_or_null(const std::string& name) const;
200
201   /**
202    * @brief Searches for a host by its name (recursively)
203    * Including children netzones and VMs on physival hosts
204    *
205    * @param name Host (or VM) name
206    * @return HostImpl pointer
207    */
208   resource::HostImpl* get_host_by_name_or_null(const std::string& name) const;
209
210   /**
211    * @brief Gets list of hosts on this netzone recursively.
212    *
213    * Note: This includes hosts on children netzones and VMs on physical hosts.
214    *
215    * @param filter Filter function to select specific nodes
216    * @return List of hosts
217    */
218   std::vector<s4u::Host*> get_filtered_hosts(const std::function<bool(s4u::Host*)>& filter) const;
219
220   /** @brief Make a host within that NetZone */
221   s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
222   /** @brief Create a disk with the disk model from this NetZone */
223   s4u::Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
224   /** @brief Make a link within that NetZone */
225   s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
226   s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, const std::vector<double>& bandwidths);
227   /** @brief Make a router within that NetZone */
228   NetPoint* create_router(const std::string& name);
229   /** @brief Creates a new route in this NetZone */
230   virtual void add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
231                                 const std::vector<s4u::LinkInRoute>& link_list);
232
233   /** @brief Seal your netzone once you're done adding content, and before routing stuff through it */
234   void seal();
235   /** @brief Check if netpoint is a member of this NetZone or some of the childrens */
236   bool is_component_recursive(const NetPoint* netpoint) const;
237   virtual unsigned long add_component(NetPoint* elm); /* A host, a router or a netzone, whatever */
238   virtual void add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
239                          const std::vector<s4u::LinkInRoute>& link_list, bool symmetrical);
240   /** @brief Set parent of this Netzone */
241   void set_parent(NetZoneImpl* parent);
242   /** @brief Set network model for this Netzone */
243   void set_network_model(std::shared_ptr<resource::NetworkModel> netmodel);
244   void set_cpu_vm_model(std::shared_ptr<resource::CpuModel> cpu_model);
245   void set_cpu_pm_model(std::shared_ptr<resource::CpuModel> cpu_model);
246   void set_disk_model(std::shared_ptr<resource::DiskModel> disk_model);
247   void set_host_model(std::shared_ptr<resource::HostModel> host_model);
248
249   /** @brief get the route between two nodes in the full platform
250    *
251    * @param src where from
252    * @param dst where to
253    * @param links Accumulator in which all traversed links should be pushed (caller must empty it)
254    * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
255    */
256   static void get_global_route(const NetPoint* src, const NetPoint* dst,
257                                /* OUT */ std::vector<resource::StandardLinkImpl*>& links, double* latency);
258
259   /** @brief Similar to get_global_route but get the NetZones traversed by route */
260   static void get_global_route_with_netzones(const NetPoint* src, const NetPoint* dst,
261                                              /* OUT */ std::vector<resource::StandardLinkImpl*>& links, double* latency,
262                                              std::unordered_set<NetZoneImpl*>& netzones);
263
264   virtual void get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
265                          std::map<std::string, xbt_edge_t, std::less<>>* edges);
266
267   /*** Called on each newly created regular route (not on bypass routes) */
268   static xbt::signal<void(bool symmetrical, NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
269                           std::vector<resource::StandardLinkImpl*> const& link_list)>
270       on_route_creation; // FIXME: XBT_ATTRIB_DEPRECATED_v332: should be an internal signal used by NS3.. if necessary,
271                          // callback shouldn't use LinkImpl*
272
273 private:
274   RoutingMode hierarchy_ = RoutingMode::base;
275   std::shared_ptr<resource::NetworkModel> network_model_;
276   std::shared_ptr<resource::CpuModel> cpu_model_vm_;
277   std::shared_ptr<resource::CpuModel> cpu_model_pm_;
278   std::shared_ptr<resource::DiskModel> disk_model_;
279   std::shared_ptr<resource::HostModel> host_model_;
280   /** @brief Perform sealing procedure for derived classes, if necessary */
281   virtual void do_seal()
282   { /* obviously nothing to do by default */
283   }
284   /** @brief Allows subclasses (wi-fi) to have their own create link method, but keep links_ updated */
285   virtual resource::StandardLinkImpl* do_create_link(const std::string& name, const std::vector<double>& bandwidths);
286   void add_child(NetZoneImpl* new_zone);
287 };
288 } // namespace simgrid::kernel::routing
289
290 #endif /* SIMGRID_ROUTING_NETZONEIMPL_HPP */