Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
One method to set netzone's relationship
[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 <vector>
17
18 namespace simgrid {
19 namespace kernel {
20 namespace routing {
21
22 class BypassRoute {
23 public:
24   explicit BypassRoute(NetPoint* gwSrc, NetPoint* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
25   NetPoint* gw_src;
26   NetPoint* gw_dst;
27   std::vector<resource::LinkImpl*> links;
28 };
29
30 /** @ingroup ROUTING_API
31  *  @brief Private implementation of the Networking Zones
32  *
33  * A netzone is a network container, in charge of routing information between elements (hosts and sub-netzones)
34  * and to the nearby netzones. In SimGrid, there is a hierarchy of netzones, ie a tree with a unique root
35  * NetZone, that you can retrieve with simgrid::s4u::Engine::netRoot().
36  *
37  * The purpose of the kernel::routing module is to retrieve the routing path between two points in a time- and
38  * space-efficient manner. This is done by NetZoneImpl::getGlobalRoute(), called when creating a communication to
39  * retrieve both the list of links that the create communication will use, and the summed latency that these
40  * links represent.
41  *
42  * The network model could recompute the latency by itself from the list, but it would require an additional
43  * traversal of the link set. This operation being on the critical path of SimGrid, the routing computes the
44  * latency on the behalf of the network while constructing the link set.
45  *
46  * Finding the path between two nodes is rather complex because we navigate a hierarchy of netzones, each of them
47  * being a full network. In addition, the routing can declare shortcuts (called bypasses), either within a NetZone
48  * at the route level or directly between NetZones. Also, each NetZone can use a differing routing algorithm, depending
49  * on its class. @ref FullZone have a full matrix giving explicitly the path between any pair of their
50  * contained nodes, while @ref DijkstraZone or @ref FloydZone rely on a shortest path algorithm. @ref VivaldiZone
51  * does not even have any link but only use only coordinate information to compute the latency.
52  *
53  * So NetZoneImpl::getGlobalRoute builds the path recursively asking its specific information to each traversed NetZone
54  * with NetZoneImpl::getLocalRoute, that is redefined in each sub-class.
55  * The algorithm for that is explained in http://hal.inria.fr/hal-00650233/ (but for historical reasons, NetZones are
56  * called Autonomous Systems in this article).
57  *
58  */
59 class XBT_PUBLIC NetZoneImpl : public xbt::PropertyHolder {
60   friend EngineImpl; // it destroys netRoot_
61   s4u::NetZone piface_;
62
63   // our content, as known to our graph routing algorithm (maps vertex_id -> vertex)
64   std::vector<kernel::routing::NetPoint*> vertices_;
65
66   NetZoneImpl* parent_ = nullptr;
67   std::vector<NetZoneImpl*> children_; // sub-netzones
68   std::string name_;
69   bool sealed_ = false; // We cannot add more content when sealed
70
71   std::map<std::pair<NetPoint*, NetPoint*>, BypassRoute*> bypass_routes_; // src x dst -> route
72   routing::NetPoint* netpoint_ = nullptr;                                 // Our representative in the father NetZone
73   std::shared_ptr<resource::NetworkModel> network_model_;
74   std::shared_ptr<resource::CpuModel> cpu_model_vm_;
75   std::shared_ptr<resource::CpuModel> cpu_model_pm_;
76   std::shared_ptr<resource::DiskModel> disk_model_;
77   std::shared_ptr<simgrid::surf::HostModel> host_model_;
78   /** @brief Perform sealing procedure for derived classes, if necessary */
79   virtual void do_seal(){};
80   void add_child(NetZoneImpl* new_zone);
81
82 protected:
83   explicit NetZoneImpl(const std::string& name);
84   NetZoneImpl(const NetZoneImpl&) = delete;
85   NetZoneImpl& operator=(const NetZoneImpl&) = delete;
86   virtual ~NetZoneImpl();
87
88   /**
89    * @brief Probe the routing path between two points that are local to the called NetZone.
90    *
91    * @param src where from
92    * @param dst where to
93    * @param into Container into which the traversed links and gateway information should be pushed
94    * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
95    */
96   virtual void get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* into, double* latency) = 0;
97   /** @brief retrieves the list of all routes of size 1 (of type src x dst x Link) */
98   /* returns whether we found a bypass path */
99   bool get_bypass_route(routing::NetPoint* src, routing::NetPoint* dst,
100                         /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency);
101
102 public:
103   enum class RoutingMode {
104     unset = 0, /**< Undefined type                                   */
105     base,      /**< Base case: use simple link lists for routing     */
106     recursive  /**< Recursive case: also return gateway information  */
107   };
108
109   /* FIXME: protect the following fields once the construction madness is sorted out */
110   RoutingMode hierarchy_ = RoutingMode::unset;
111
112   /** @brief Retrieves the network model associated to this NetZone */
113   const std::shared_ptr<resource::NetworkModel>& get_network_model() const { return network_model_; }
114   /** @brief Retrieves the CPU model for virtual machines associated to this NetZone */
115   const std::shared_ptr<resource::CpuModel>& get_cpu_vm_model() const { return cpu_model_vm_; }
116   /** @brief Retrieves the CPU model for physical machines associated to this NetZone */
117   const std::shared_ptr<resource::CpuModel>& get_cpu_pm_model() const { return cpu_model_pm_; }
118   /** @brief Retrieves the disk model associated to this NetZone */
119   const std::shared_ptr<resource::DiskModel>& get_disk_model() const { return disk_model_; }
120   /** @brief Retrieves the host model associated to this NetZone */
121   const std::shared_ptr<surf::HostModel>& get_host_model() const { return host_model_; }
122
123   const s4u::NetZone* get_iface() const { return &piface_; }
124   s4u::NetZone* get_iface() { return &piface_; }
125   unsigned int get_table_size() const { return vertices_.size(); }
126   std::vector<kernel::routing::NetPoint*> get_vertices() const { return vertices_; }
127   XBT_ATTRIB_DEPRECATED_v331("Please use get_parent()") NetZoneImpl* get_father() const { return parent_; }
128   NetZoneImpl* get_parent() const { return parent_; }
129   /** @brief Returns the list of direct children (no grand-children). This returns the internal data, no copy.
130    * Don't mess with it.*/
131   std::vector<NetZoneImpl*>* get_children() { return &children_; }
132
133   /** @brief Retrieves the name of that netzone as a C++ string */
134   const std::string& get_name() const { return name_; }
135   /** @brief Retrieves the name of that netzone as a C string */
136   const char* get_cname() const { return name_.c_str(); };
137
138   std::vector<s4u::Host*> get_all_hosts() const;
139   int get_host_count() const;
140
141   /** @brief Make a host within that NetZone */
142   s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
143   /** @brief Create a disk with the disk model from this NetZone */
144   s4u::Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
145   /** @brief Make a link within that NetZone */
146   virtual s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
147   virtual s4u::Link* create_wifi_link(const std::string& name, const std::vector<double>& bandwidths);
148   /** @brief Creates a new route in this NetZone */
149   virtual void add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
150                                 std::vector<resource::LinkImpl*>& link_list, bool symmetrical);
151
152   /** @brief Seal your netzone once you're done adding content, and before routing stuff through it */
153   void seal();
154   virtual int add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
155   virtual void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
156                          kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
157                          const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical);
158   /** @brief Set parent of this Netzone */
159   void set_parent(NetZoneImpl* parent);
160   /** @brief Set network model for this Netzone */
161   void set_network_model(std::shared_ptr<resource::NetworkModel> netmodel);
162   void set_cpu_vm_model(std::shared_ptr<resource::CpuModel> cpu_model);
163   void set_cpu_pm_model(std::shared_ptr<resource::CpuModel> cpu_model);
164   void set_disk_model(std::shared_ptr<resource::DiskModel> disk_model);
165   void set_host_model(std::shared_ptr<surf::HostModel> host_model);
166
167   /* @brief get the route between two nodes in the full platform
168    *
169    * @param src where from
170    * @param dst where to
171    * @param links Accumulator in which all traversed links should be pushed (caller must empty it)
172    * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
173    */
174   static void get_global_route(routing::NetPoint* src, routing::NetPoint* dst,
175                                /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency);
176
177   virtual void get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
178                          std::map<std::string, xbt_edge_t, std::less<>>* edges) = 0;
179 };
180 } // namespace routing
181 } // namespace kernel
182 } // namespace simgrid
183
184 #endif /* SIMGRID_ROUTING_NETZONEIMPL_HPP */