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