1 /* Copyright (c) 2016-2023. 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>
16 #include <unordered_set>
19 namespace simgrid::kernel::routing {
24 explicit Route(NetPoint* src, NetPoint* dst, NetPoint* gwSrc, NetPoint* gwDst)
25 : src_(src), dst_(dst), gw_src_(gwSrc), gw_dst_(gwDst)
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_;
37 explicit BypassRoute(NetPoint* gwSrc, NetPoint* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
40 std::vector<resource::StandardLinkImpl*> links;
43 /** @ingroup ROUTING_API
44 * @brief Private implementation of the Networking Zones
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().
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
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.
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.
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).
72 class XBT_PUBLIC NetZoneImpl : public xbt::PropertyHolder {
73 friend EngineImpl; // it destroys netRoot_
76 // our content, as known to our graph routing algorithm (maps vertex_id -> vertex)
77 std::vector<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 std::map<std::string, NetPoint*, std::less<>> gateways_;
85 NetZoneImpl* parent_ = nullptr;
86 std::vector<NetZoneImpl*> children_; // sub-netzones
88 bool sealed_ = false; // We cannot add more content when sealed
90 std::map<std::pair<const NetPoint*, const NetPoint*>, BypassRoute*> bypass_routes_; // src x dst -> route
91 NetPoint* netpoint_ = nullptr; // Our representative in the parent NetZone
94 explicit NetZoneImpl(const std::string& name);
95 NetZoneImpl(const NetZoneImpl&) = delete;
96 NetZoneImpl& operator=(const NetZoneImpl&) = delete;
97 virtual ~NetZoneImpl();
100 * @brief Probe the routing path between two points that are local to the called NetZone.
102 * @param src where from
103 * @param dst where to
104 * @param into Container into which the traversed links and gateway information should be pushed
105 * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
107 virtual void get_local_route(const NetPoint* src, const NetPoint* dst, Route* into, double* latency) = 0;
108 /** @brief retrieves the list of all routes of size 1 (of type src x dst x Link) */
109 /* returns whether we found a bypass path */
110 bool get_bypass_route(const routing::NetPoint* src, const routing::NetPoint* dst,
111 /* OUT */ std::vector<resource::StandardLinkImpl*>& links, double* latency,
112 std::unordered_set<NetZoneImpl*>& netzones);
114 /** @brief Get the NetZone that is represented by the netpoint */
115 const NetZoneImpl* get_netzone_recursive(const NetPoint* netpoint) const;
117 /** @brief Get the list of LinkImpl* to add in a route, considering split-duplex links and the direction */
118 std::vector<resource::StandardLinkImpl*> get_link_list_impl(const std::vector<s4u::LinkInRoute>& link_list,
119 bool backroute) const;
121 static xbt_node_t new_xbt_graph_node(const s_xbt_graph_t* graph, const char* name,
122 std::map<std::string, xbt_node_t, std::less<>>* nodes);
123 static xbt_edge_t new_xbt_graph_edge(const s_xbt_graph_t* graph, xbt_node_t src, xbt_node_t dst,
124 std::map<std::string, xbt_edge_t, std::less<>>* edges);
127 enum class RoutingMode {
128 base, /**< Base case: use simple link lists for routing */
129 recursive /**< Recursive case: also return gateway information */
132 /** @brief Retrieves the network model associated to this NetZone */
133 const std::shared_ptr<resource::NetworkModel>& get_network_model() const { return network_model_; }
134 /** @brief Retrieves the CPU model for virtual machines associated to this NetZone */
135 const std::shared_ptr<resource::CpuModel>& get_cpu_vm_model() const { return cpu_model_vm_; }
136 /** @brief Retrieves the CPU model for physical machines associated to this NetZone */
137 const std::shared_ptr<resource::CpuModel>& get_cpu_pm_model() const { return cpu_model_pm_; }
138 /** @brief Retrieves the disk model associated to this NetZone */
139 const std::shared_ptr<resource::DiskModel>& get_disk_model() const { return disk_model_; }
140 /** @brief Retrieves the host model associated to this NetZone */
141 const std::shared_ptr<resource::HostModel>& get_host_model() const { return host_model_; }
143 const s4u::NetZone* get_iface() const { return &piface_; }
144 s4u::NetZone* get_iface() { return &piface_; }
145 unsigned int get_table_size() const { return vertices_.size(); }
146 std::vector<NetPoint*> get_vertices() const { return vertices_; }
147 NetZoneImpl* get_parent() const { return parent_; }
148 /** @brief Returns the list of direct children (no grand-children). This returns the internal data, no copy.
149 * Don't mess with it.*/
150 const std::vector<NetZoneImpl*>& get_children() const { return children_; }
151 /** @brief Get current netzone hierarchy */
152 RoutingMode get_hierarchy() const { return hierarchy_; }
154 /** @brief Retrieves the name of that netzone as a C++ string */
155 const std::string& get_name() const { return name_; }
156 /** @brief Retrieves the name of that netzone as a C string */
157 const char* get_cname() const { return name_.c_str(); };
159 /** @brief Gets the netpoint associated to this netzone */
160 NetPoint* get_netpoint() const { return netpoint_; }
162 void set_gateway(const std::string& name, NetPoint* router);
163 /** @brief Gets the gateway associated to this netzone */
164 NetPoint* get_gateway() const;
165 NetPoint* get_gateway(const std::string& name) const { return gateways_.at(name); }
167 std::vector<s4u::Host*> get_all_hosts() const;
168 size_t get_host_count() const;
171 * @brief Recursively gets all links declared in this netzone
173 * Include children netzones.
174 * @return List of links
176 std::vector<s4u::Link*> get_all_links() const;
178 * @brief Recursively gets all links declared in this netzone.
180 * Using a filter function
181 * Include children netzones.
182 * @param filter Select links based on this filter
183 * @return List of links
185 std::vector<s4u::Link*> get_filtered_links(const std::function<bool(s4u::Link*)>& filter) const;
186 /** @brief Get total number of links declared in this netzone (and its children) */
187 size_t get_link_count() const;
190 * @brief Searches by the link by its name inside this netzone.
191 * Recursively searches in children netzones
193 * @param name Link name
194 * @return Link object or nullptr if not found
196 resource::StandardLinkImpl* get_link_by_name_or_null(const std::string& name) const;
199 * @brief Searches for split-duplex links by its name inside this netzone.
200 * Recursively searches in child netzones
202 * @param name Split-duplex Link name
203 * @return Link object or nullptr if not found
205 resource::SplitDuplexLinkImpl* get_split_duplex_link_by_name_or_null(const std::string& name) const;
208 * @brief Searches for a host by its name (recursively)
209 * Including children netzones and VMs on physival hosts
211 * @param name Host (or VM) name
212 * @return HostImpl pointer
214 resource::HostImpl* get_host_by_name_or_null(const std::string& name) const;
217 * @brief Gets list of hosts on this netzone recursively.
219 * Note: This includes hosts on children netzones and VMs on physical hosts.
221 * @param filter Filter function to select specific nodes
222 * @return List of hosts
224 std::vector<s4u::Host*> get_filtered_hosts(const std::function<bool(s4u::Host*)>& filter) const;
226 /** @brief Make a host within that NetZone */
227 s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate);
228 /** @brief Create a disk with the disk model from this NetZone */
229 s4u::Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
230 /** @brief Make a link within that NetZone */
231 s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths);
232 s4u::SplitDuplexLink* create_split_duplex_link(const std::string& name, const std::vector<double>& bandwidths);
233 /** @brief Make a router within that NetZone */
234 NetPoint* create_router(const std::string& name);
235 /** @brief Creates a new route in this NetZone */
236 virtual void add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
237 const std::vector<s4u::LinkInRoute>& link_list);
239 /** @brief Seal your netzone once you're done adding content, and before routing stuff through it */
241 /** @brief Check if netpoint is a member of this NetZone or some of the childrens */
242 bool is_component_recursive(const NetPoint* netpoint) const;
243 virtual unsigned long add_component(NetPoint* elm); /* A host, a router or a netzone, whatever */
244 virtual void add_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
245 const std::vector<s4u::LinkInRoute>& link_list, bool symmetrical);
246 /** @brief Set parent of this Netzone */
247 void set_parent(NetZoneImpl* parent);
248 /** @brief Set network model for this Netzone */
249 void set_network_model(std::shared_ptr<resource::NetworkModel> netmodel);
250 void set_cpu_vm_model(std::shared_ptr<resource::CpuModel> cpu_model);
251 void set_cpu_pm_model(std::shared_ptr<resource::CpuModel> cpu_model);
252 void set_disk_model(std::shared_ptr<resource::DiskModel> disk_model);
253 void set_host_model(std::shared_ptr<resource::HostModel> host_model);
255 /** @brief get the route between two nodes in the full platform
257 * @param src where from
258 * @param dst where to
259 * @param links Accumulator in which all traversed links should be pushed (caller must empty it)
260 * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
262 static void get_global_route(const NetPoint* src, const NetPoint* dst,
263 /* OUT */ std::vector<resource::StandardLinkImpl*>& links, double* latency);
265 /** @brief Similar to get_global_route but get the NetZones traversed by route */
266 static void get_global_route_with_netzones(const NetPoint* src, const NetPoint* dst,
267 /* OUT */ std::vector<resource::StandardLinkImpl*>& links, double* latency,
268 std::unordered_set<NetZoneImpl*>& netzones);
270 virtual void get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
271 std::map<std::string, xbt_edge_t, std::less<>>* edges);
273 /*** Called on each newly created regular route (not on bypass routes) */
274 static xbt::signal<void(bool symmetrical, NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
275 std::vector<resource::StandardLinkImpl*> const& link_list)>
276 on_route_creation; // FIXME: XBT_ATTRIB_DEPRECATED_v332: should be an internal signal used by NS3.. if necessary,
277 // callback shouldn't use LinkImpl*
280 RoutingMode hierarchy_ = RoutingMode::base;
281 std::shared_ptr<resource::NetworkModel> network_model_;
282 std::shared_ptr<resource::CpuModel> cpu_model_vm_;
283 std::shared_ptr<resource::CpuModel> cpu_model_pm_;
284 std::shared_ptr<resource::DiskModel> disk_model_;
285 std::shared_ptr<resource::HostModel> host_model_;
286 /** @brief Perform sealing procedure for derived classes, if necessary */
287 virtual void do_seal()
288 { /* obviously nothing to do by default */
290 /** @brief Allows subclasses (wi-fi) to have their own create link method, but keep links_ updated */
291 virtual resource::StandardLinkImpl* do_create_link(const std::string& name, const std::vector<double>& bandwidths);
292 void add_child(NetZoneImpl* new_zone);
294 } // namespace simgrid::kernel::routing
296 #endif /* SIMGRID_ROUTING_NETZONEIMPL_HPP */