Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'split_link_impl' into 'master'
[simgrid.git] / include / simgrid / kernel / routing / NetZoneImpl.hpp
1 /* Copyright (c) 2016-2021. 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::LinkImpl*> 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::LinkImpl*> 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
81   NetZoneImpl* parent_ = nullptr;
82   std::vector<NetZoneImpl*> children_; // sub-netzones
83   std::string name_;
84   bool sealed_ = false; // We cannot add more content when sealed
85
86   std::map<std::pair<const NetPoint*, const NetPoint*>, BypassRoute*> bypass_routes_; // src x dst -> route
87   routing::NetPoint* netpoint_ = nullptr;                                 // Our representative in the father NetZone
88
89 protected:
90   explicit NetZoneImpl(const std::string& name);
91   NetZoneImpl(const NetZoneImpl&) = delete;
92   NetZoneImpl& operator=(const NetZoneImpl&) = delete;
93   virtual ~NetZoneImpl();
94
95   /**
96    * @brief Probe the routing path between two points that are local to the called NetZone.
97    *
98    * @param src where from
99    * @param dst where to
100    * @param into Container into which the traversed links and gateway information should be pushed
101    * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
102    */
103   virtual void get_local_route(const NetPoint* src, const NetPoint* dst, Route* into, double* latency) = 0;
104   /** @brief retrieves the list of all routes of size 1 (of type src x dst x Link) */
105   /* returns whether we found a bypass path */
106   bool get_bypass_route(const routing::NetPoint* src, const routing::NetPoint* dst,
107                         /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency,
108                         std::unordered_set<NetZoneImpl*>& netzones);
109
110   /** @brief Get the NetZone that is represented by the netpoint */
111   const NetZoneImpl* get_netzone_recursive(const NetPoint* netpoint) const;
112
113   /** @brief Get the list of LinkImpl* to add in a route, considering split-duplex links and the direction */
114   std::vector<resource::LinkImpl*> get_link_list_impl(const std::vector<s4u::LinkInRoute>& link_list,
115                                                       bool backroute) const;
116
117 public:
118   enum class RoutingMode {
119     base,     /**< Base case: use simple link lists for routing     */
120     recursive /**< Recursive case: also return gateway information  */
121   };
122
123   /** @brief Retrieves the network model associated to this NetZone */
124   const std::shared_ptr<resource::NetworkModel>& get_network_model() const { return network_model_; }
125   /** @brief Retrieves the CPU model for virtual machines associated to this NetZone */
126   const std::shared_ptr<resource::CpuModel>& get_cpu_vm_model() const { return cpu_model_vm_; }
127   /** @brief Retrieves the CPU model for physical machines associated to this NetZone */
128   const std::shared_ptr<resource::CpuModel>& get_cpu_pm_model() const { return cpu_model_pm_; }
129   /** @brief Retrieves the disk model associated to this NetZone */
130   const std::shared_ptr<resource::DiskModel>& get_disk_model() const { return disk_model_; }
131   /** @brief Retrieves the host model associated to this NetZone */
132   const std::shared_ptr<surf::HostModel>& get_host_model() const { return host_model_; }
133
134   const s4u::NetZone* get_iface() const { return &piface_; }
135   s4u::NetZone* get_iface() { return &piface_; }
136   unsigned int get_table_size() const { return vertices_.size(); }
137   std::vector<kernel::routing::NetPoint*> get_vertices() const { return vertices_; }
138   XBT_ATTRIB_DEPRECATED_v331("Please use get_parent()") NetZoneImpl* get_father() const { return parent_; }
139   NetZoneImpl* get_parent() const { return parent_; }
140   /** @brief Returns the list of direct children (no grand-children). This returns the internal data, no copy.
141    * Don't mess with it.*/
142   const std::vector<NetZoneImpl*>& get_children() const { return children_; }
143   /** @brief Get current netzone hierarchy */
144   RoutingMode get_hierarchy() const { return hierarchy_; }
145
146   /** @brief Retrieves the name of that netzone as a C++ string */
147   const std::string& get_name() const { return name_; }
148   /** @brief Retrieves the name of that netzone as a C string */
149   const char* get_cname() const { return name_.c_str(); };
150
151   /** @brief Gets the netpoint associated to this netzone */
152   kernel::routing::NetPoint* get_netpoint() const { return netpoint_; }
153
154   std::vector<s4u::Host*> get_all_hosts() const;
155   int get_host_count() const;
156
157   /** @brief Make a host within that NetZone */
158   s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
159   /** @brief Create a disk with the disk model from this NetZone */
160   s4u::Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
161   /** @brief Make a link within that NetZone */
162   virtual s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
163   s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, const std::vector<double>& bandwidths);
164   /** @brief Make a router within that NetZone */
165   NetPoint* create_router(const std::string& name);
166   /** @brief Creates a new route in this NetZone */
167   virtual void add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
168                                 const std::vector<s4u::LinkInRoute>& link_list);
169
170   /** @brief Seal your netzone once you're done adding content, and before routing stuff through it */
171   void seal();
172   /** @brief Check if netpoint is a member of this NetZone or some of the childrens */
173   bool is_component_recursive(const NetPoint* netpoint) const;
174   virtual int add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
175   virtual void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
176                          kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
177                          const std::vector<s4u::LinkInRoute>& link_list, bool symmetrical);
178   /** @brief Set parent of this Netzone */
179   void set_parent(NetZoneImpl* parent);
180   /** @brief Set network model for this Netzone */
181   void set_network_model(std::shared_ptr<resource::NetworkModel> netmodel);
182   void set_cpu_vm_model(std::shared_ptr<resource::CpuModel> cpu_model);
183   void set_cpu_pm_model(std::shared_ptr<resource::CpuModel> cpu_model);
184   void set_disk_model(std::shared_ptr<resource::DiskModel> disk_model);
185   void set_host_model(std::shared_ptr<surf::HostModel> host_model);
186
187   /** @brief get the route between two nodes in the full platform
188    *
189    * @param src where from
190    * @param dst where to
191    * @param links Accumulator in which all traversed links should be pushed (caller must empty it)
192    * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
193    */
194   static void get_global_route(const routing::NetPoint* src, const routing::NetPoint* dst,
195                                /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency);
196
197   /** @brief Similar to get_global_route but get the NetZones traversed by route */
198   static void get_global_route_with_netzones(const routing::NetPoint* src, const routing::NetPoint* dst,
199                                              /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency,
200                                              std::unordered_set<NetZoneImpl*>& netzones);
201
202   virtual void get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
203                          std::map<std::string, xbt_edge_t, std::less<>>* edges) = 0;
204
205   /*** Called on each newly created regular route (not on bypass routes) */
206   static xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
207                           kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
208                           std::vector<kernel::resource::LinkImpl*> const& link_list)>
209       on_route_creation; // XBT_ATTRIB_DEPRECATED_v332 : should be an internal signal used by NS3.. if necessary,
210                          // callback shouldn't use LinkImpl*
211
212 private:
213   RoutingMode hierarchy_ = RoutingMode::base;
214   std::shared_ptr<resource::NetworkModel> network_model_;
215   std::shared_ptr<resource::CpuModel> cpu_model_vm_;
216   std::shared_ptr<resource::CpuModel> cpu_model_pm_;
217   std::shared_ptr<resource::DiskModel> disk_model_;
218   std::shared_ptr<simgrid::surf::HostModel> host_model_;
219   /** @brief Perform sealing procedure for derived classes, if necessary */
220   virtual void do_seal()
221   { /* obviously nothing to do by default */
222   }
223   void add_child(NetZoneImpl* new_zone);
224 };
225 } // namespace routing
226 } // namespace kernel
227 } // namespace simgrid
228
229 #endif /* SIMGRID_ROUTING_NETZONEIMPL_HPP */