Logo AND Algorithmique Numérique Distribuée

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