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>
24 explicit BypassRoute(NetPoint* gwSrc, NetPoint* gwDst) : gw_src(gwSrc), gw_dst(gwDst) {}
27 std::vector<resource::LinkImpl*> links;
30 /** @ingroup ROUTING_API
31 * @brief Private implementation of the Networking Zones
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().
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
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.
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.
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).
59 class XBT_PUBLIC NetZoneImpl : public xbt::PropertyHolder {
60 friend EngineImpl; // it destroys netRoot_
63 // our content, as known to our graph routing algorithm (maps vertex_id -> vertex)
64 std::vector<kernel::routing::NetPoint*> vertices_;
66 NetZoneImpl* father_ = nullptr;
67 std::vector<NetZoneImpl*> children_; // sub-netzones
69 bool sealed_ = false; // We cannot add more content when sealed
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 resource::NetworkModel* network_model_;
74 resource::CpuModel* cpu_model_vm_;
75 resource::CpuModel* cpu_model_pm_;
76 resource::DiskModel* disk_model_;
77 simgrid::surf::HostModel* host_model_;
78 /** @brief Perform sealing procedure for derived classes, if necessary */
79 virtual void do_seal(){};
82 explicit NetZoneImpl(const std::string& name);
83 NetZoneImpl(const NetZoneImpl&) = delete;
84 NetZoneImpl& operator=(const NetZoneImpl&) = delete;
85 virtual ~NetZoneImpl();
88 * @brief Probe the routing path between two points that are local to the called NetZone.
90 * @param src where from
92 * @param into Container into which the traversed links and gateway information should be pushed
93 * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
95 virtual void get_local_route(NetPoint* src, NetPoint* dst, RouteCreationArgs* into, double* latency) = 0;
96 /** @brief retrieves the list of all routes of size 1 (of type src x dst x Link) */
97 /* returns whether we found a bypass path */
98 bool get_bypass_route(routing::NetPoint* src, routing::NetPoint* dst,
99 /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency);
102 enum class RoutingMode {
103 unset = 0, /**< Undefined type */
104 base, /**< Base case: use simple link lists for routing */
105 recursive /**< Recursive case: also return gateway information */
108 /* FIXME: protect the following fields once the construction madness is sorted out */
109 RoutingMode hierarchy_ = RoutingMode::unset;
111 /** @brief Retrieves the network model associated to this NetZone */
112 resource::NetworkModel* get_network_model() const { return network_model_; }
113 /** @brief Retrieves the CPU model for virtual machines associated to this NetZone */
114 resource::CpuModel* get_cpu_vm_model() const { return cpu_model_vm_; }
115 /** @brief Retrieves the CPU model for physical machines associated to this NetZone */
116 resource::CpuModel* get_cpu_pm_model() const { return cpu_model_pm_; }
117 /** @brief Retrieves the disk model associated to this NetZone */
118 resource::DiskModel* get_disk_model() const { return disk_model_; }
119 /** @brief Retrieves the host model associated to this NetZone */
120 simgrid::surf::HostModel* get_host_model() const { return host_model_; }
122 const s4u::NetZone* get_iface() const { return &piface_; }
123 s4u::NetZone* get_iface() { return &piface_; }
124 unsigned int get_table_size() const { return vertices_.size(); }
125 std::vector<kernel::routing::NetPoint*> get_vertices() const { return vertices_; }
126 NetZoneImpl* get_father() const { return father_; }
127 /** @brief Returns the list of direct children (no grand-children). This returns the internal data, no copy.
128 * Don't mess with it.*/
129 std::vector<NetZoneImpl*>* get_children() { return &children_; }
130 /** @brief Retrieves the name of that netzone as a C++ string */
131 const std::string& get_name() const { return name_; }
132 /** @brief Retrieves the name of that netzone as a C string */
133 const char* get_cname() const { return name_.c_str(); };
135 std::vector<s4u::Host*> get_all_hosts() const;
136 int get_host_count() const;
138 /** @brief Make a host within that NetZone */
139 s4u::Host* create_host(const std::string& name, const std::vector<double>& speed_per_pstate, int core_amount);
140 /** @brief Create a disk with the disk model from this NetZone */
141 s4u::Disk* create_disk(const std::string& name, double read_bandwidth, double write_bandwidth);
142 /** @brief Make a link within that NetZone */
143 virtual s4u::Link* create_link(const std::string& name, const std::vector<double>& bandwidths,
144 s4u::Link::SharingPolicy policy);
145 /** @brief Creates a new route in this NetZone */
146 virtual void add_bypass_route(NetPoint* src, NetPoint* dst, NetPoint* gw_src, NetPoint* gw_dst,
147 std::vector<resource::LinkImpl*>& link_list, bool symmetrical);
149 /** @brief Seal your netzone once you're done adding content, and before routing stuff through it */
151 virtual int add_component(kernel::routing::NetPoint* elm); /* A host, a router or a netzone, whatever */
152 virtual void add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
153 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
154 std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical);
155 /** @brief Set parent of this Netzone */
156 void set_parent(NetZoneImpl* parent);
157 /** @brief Set network model for this Netzone */
158 void set_network_model(simgrid::kernel::resource::NetworkModel* netmodel);
160 /* @brief get the route between two nodes in the full platform
162 * @param src where from
163 * @param dst where to
164 * @param links Accumulator in which all traversed links should be pushed (caller must empty it)
165 * @param latency Accumulator in which the latencies should be added (caller must set it to 0)
167 static void get_global_route(routing::NetPoint* src, routing::NetPoint* dst,
168 /* OUT */ std::vector<resource::LinkImpl*>& links, double* latency);
170 virtual void get_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
171 std::map<std::string, xbt_edge_t, std::less<>>* edges) = 0;
173 } // namespace routing
174 } // namespace kernel
175 } // namespace simgrid
177 #endif /* SIMGRID_ROUTING_NETZONEIMPL_HPP */