Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simpler way to define routes between zones (and between host and anything)
[simgrid.git] / src / s4u / s4u_Netzone.cpp
1 /* Copyright (c) 2006-2023. 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 #include "simgrid/Exception.hpp"
7 #include "simgrid/s4u/Host.hpp"
8 #include <simgrid/kernel/routing/NetZoneImpl.hpp>
9 #include <simgrid/s4u/Engine.hpp>
10 #include <simgrid/s4u/NetZone.hpp>
11 #include <simgrid/simix.hpp>
12 #include <simgrid/zone.h>
13 #include <xbt/parse_units.hpp>
14
15 #include "src/kernel/resource/NetworkModel.hpp"
16
17 namespace simgrid::s4u {
18
19 xbt::signal<void(NetZone const&)> NetZone::on_creation;
20 xbt::signal<void(NetZone const&)> NetZone::on_seal;
21
22 const std::unordered_map<std::string, std::string>* NetZone::get_properties() const
23 {
24   return pimpl_->get_properties();
25 }
26
27 /** Retrieve the property value (or nullptr if not set) */
28 const char* NetZone::get_property(const std::string& key) const
29 {
30   return pimpl_->get_property(key);
31 }
32
33 void NetZone::set_property(const std::string& key, const std::string& value)
34 {
35   kernel::actor::simcall_answered([this, &key, &value] { pimpl_->set_property(key, value); });
36 }
37
38 /** @brief Returns the list of direct children (no grand-children) */
39 std::vector<NetZone*> NetZone::get_children() const
40 {
41   std::vector<NetZone*> res;
42   for (auto* child : pimpl_->get_children())
43     res.push_back(child->get_iface());
44   return res;
45 }
46
47 const std::string& NetZone::get_name() const
48 {
49   return pimpl_->get_name();
50 }
51
52 const char* NetZone::get_cname() const
53 {
54   return pimpl_->get_cname();
55 }
56
57 NetZone* NetZone::get_parent() const
58 {
59   return pimpl_->get_parent()->get_iface();
60 }
61
62 NetZone* NetZone::set_parent(const NetZone* parent)
63 {
64   kernel::actor::simcall_answered([this, parent] { pimpl_->set_parent(parent->get_impl()); });
65   return this;
66 }
67
68 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
69  *
70  * Only the hosts that are directly contained in this NetZone are retrieved,
71  * not the ones contained in sub-netzones.
72  */
73 std::vector<Host*> NetZone::get_all_hosts() const
74 {
75   return pimpl_->get_all_hosts();
76 }
77
78 size_t NetZone::get_host_count() const
79 {
80   return pimpl_->get_host_count();
81 }
82
83 unsigned long NetZone::add_component(kernel::routing::NetPoint* elm)
84 {
85   return pimpl_->add_component(elm);
86 }
87
88 void NetZone::add_route(NetZone* src, NetZone* dst, const std::vector<const Link*>& links)
89 {
90   std::vector<LinkInRoute> links_direct;
91   std::vector<LinkInRoute> links_reverse;
92   for (auto* l : links) {
93     links_direct.emplace_back(LinkInRoute(l, LinkInRoute::Direction::UP));
94     links_reverse.emplace_back(LinkInRoute(l, LinkInRoute::Direction::DOWN));
95   }
96   pimpl_->add_route(src->get_netpoint(), dst->get_netpoint(), src->get_gateway(), dst->get_gateway(), links_direct,
97                     false);
98   pimpl_->add_route(dst->get_netpoint(), src->get_netpoint(), dst->get_gateway(), src->get_gateway(), links_reverse,
99                     false);
100 }
101
102 void NetZone::add_route(NetZone* src, NetZone* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical)
103 {
104   pimpl_->add_route(src->get_netpoint(), dst->get_netpoint(), src->get_gateway(), dst->get_gateway(), link_list,
105                     symmetrical);
106 }
107
108 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
109                         kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
110                         const std::vector<LinkInRoute>& link_list, bool symmetrical)
111 {
112   pimpl_->add_route(src, dst, gw_src, gw_dst, link_list, symmetrical);
113 }
114
115 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
116                         kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
117                         const std::vector<const Link*>& links)
118 {
119   std::vector<LinkInRoute> links_direct;
120   std::vector<LinkInRoute> links_reverse;
121   for (auto* l : links) {
122     links_direct.emplace_back(LinkInRoute(l, LinkInRoute::Direction::UP));
123     links_reverse.emplace_back(LinkInRoute(l, LinkInRoute::Direction::DOWN));
124   }
125   pimpl_->add_route(src, dst, gw_src, gw_dst, links_direct, false);
126   pimpl_->add_route(dst, src, gw_dst, gw_src, links_reverse, false);
127 }
128
129 void NetZone::add_route(const Host* src, const Host* dst, const std::vector<LinkInRoute>& link_list, bool symmetrical)
130 {
131   pimpl_->add_route(src ? src->get_netpoint(): nullptr, dst ? dst->get_netpoint(): nullptr, nullptr, nullptr,
132                     link_list, symmetrical);
133
134 }
135
136 void NetZone::add_route(const Host* src, const Host* dst, const std::vector<const Link*>& links)
137 {
138   std::vector<LinkInRoute> links_direct;
139   std::vector<LinkInRoute> links_reverse;
140   for (auto* l : links) {
141     links_direct.emplace_back(LinkInRoute(l, LinkInRoute::Direction::UP));
142     links_reverse.emplace_back(LinkInRoute(l, LinkInRoute::Direction::DOWN));
143   }
144   pimpl_->add_route(src ? src->get_netpoint(): nullptr, dst ? dst->get_netpoint(): nullptr, nullptr, nullptr,
145                     links_direct, false);
146   pimpl_->add_route(dst ? dst->get_netpoint(): nullptr, src ? src->get_netpoint(): nullptr, nullptr, nullptr,
147                     links_reverse, false);
148 }
149
150 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
151                                kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
152                                const std::vector<LinkInRoute>& link_list)
153 {
154   pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, link_list);
155 }
156
157 void NetZone::extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
158                                 std::map<std::string, xbt_edge_t, std::less<>>* edges)
159 {
160   for (auto const& child : get_children())
161     child->extract_xbt_graph(graph, nodes, edges);
162
163   pimpl_->get_graph(graph, nodes, edges);
164 }
165
166 NetZone* NetZone::seal()
167 {
168   kernel::actor::simcall_answered([this] { pimpl_->seal(); });
169   return this;
170 }
171 void NetZone::set_latency_factor_cb(
172     std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
173                          const std::vector<s4u::Link*>& /*links*/,
174                          const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const
175 {
176   kernel::actor::simcall_answered([this, &cb]() { pimpl_->get_network_model()->set_lat_factor_cb(cb); });
177 }
178 void NetZone::set_bandwidth_factor_cb(
179     std::function<double(double size, const s4u::Host* src, const s4u::Host* dst,
180                          const std::vector<s4u::Link*>& /*links*/,
181                          const std::unordered_set<s4u::NetZone*>& /*netzones*/)> const& cb) const
182 {
183   kernel::actor::simcall_answered([this, &cb]() { pimpl_->get_network_model()->set_bw_factor_cb(cb); });
184 }
185
186 s4u::Host* NetZone::create_host(const std::string& name, double speed)
187 {
188   return create_host(name, std::vector<double>{speed});
189 }
190
191 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<double>& speed_per_pstate)
192 {
193   return kernel::actor::simcall_answered(
194       [this, &name, &speed_per_pstate] { return pimpl_->create_host(name, speed_per_pstate); });
195 }
196
197 s4u::Host* NetZone::create_host(const std::string& name, const std::string& speed)
198 {
199   return create_host(name, std::vector<std::string>{speed});
200 }
201
202 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate)
203 {
204   return create_host(name, Host::convert_pstate_speed_vector(speed_per_pstate));
205 }
206
207 s4u::Link* NetZone::create_link(const std::string& name, double bandwidth)
208 {
209   return create_link(name, std::vector<double>{bandwidth});
210 }
211
212 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<double>& bandwidths)
213 {
214   return kernel::actor::simcall_answered([this, &name, &bandwidths] { return pimpl_->create_link(name, bandwidths); });
215 }
216
217 s4u::Link* NetZone::create_link(const std::string& name, const std::string& bandwidth)
218 {
219   return create_link(name, std::vector<std::string>{bandwidth});
220 }
221
222 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, const std::string& bandwidth)
223 {
224   double speed;
225   try {
226     speed = xbt_parse_get_bandwidth("", 0, bandwidth, "");
227   } catch (const simgrid::ParseError&) {
228     throw std::invalid_argument("Impossible to create split-duplex link: " + name +
229                                 ". Invalid bandwidth: " + bandwidth);
230   }
231   return create_split_duplex_link(name, speed);
232 }
233
234 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, double bandwidth)
235 {
236   return kernel::actor::simcall_answered(
237       [this, &name, &bandwidth] { return pimpl_->create_split_duplex_link(name, std::vector<double>{bandwidth}); });
238 }
239
240 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<std::string>& bandwidths)
241 {
242   std::vector<double> bw;
243   bw.reserve(bandwidths.size());
244   for (const auto& speed_str : bandwidths) {
245     try {
246       double speed = xbt_parse_get_bandwidth("", 0, speed_str, "");
247       bw.push_back(speed);
248     } catch (const simgrid::ParseError&) {
249       throw std::invalid_argument("Impossible to create link: " + name + ". Invalid bandwidth: " + speed_str);
250     }
251   }
252   return create_link(name, bw);
253 }
254
255 kernel::routing::NetPoint* NetZone::create_router(const std::string& name)
256 {
257   return kernel::actor::simcall_answered([this, &name] { return pimpl_->create_router(name); });
258 }
259
260 kernel::routing::NetPoint* NetZone::get_netpoint() const
261 {
262   return pimpl_->get_netpoint();
263 }
264
265 kernel::routing::NetPoint* NetZone::get_gateway() const
266 {
267   return pimpl_->get_gateway();
268 }
269
270 kernel::routing::NetPoint* NetZone::get_gateway(const std::string& name) const
271 {
272   return pimpl_->get_gateway(name);
273 }
274
275 void NetZone::set_gateway(kernel::routing::NetPoint* router)
276 {
277   set_gateway("default", router);
278 }
279
280 void NetZone::set_gateway(const std::string& name, kernel::routing::NetPoint* router)
281 {
282   kernel::actor::simcall_answered([this, name, router] { pimpl_->set_gateway(name, router); });
283 }
284
285 kernel::resource::NetworkModel* NetZone::get_network_model() const
286 {
287   return pimpl_->get_network_model().get();
288 }
289 } // namespace simgrid::s4u
290
291 /* **************************** Public C interface *************************** */
292
293 sg_netzone_t sg_zone_get_root()
294 {
295   return simgrid::s4u::Engine::get_instance()->get_netzone_root();
296 }
297
298 const char* sg_zone_get_name(const_sg_netzone_t netzone)
299 {
300   return netzone->get_cname();
301 }
302
303 sg_netzone_t sg_zone_get_by_name(const char* name)
304 {
305   return simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(name);
306 }
307
308 void sg_zone_get_sons(const_sg_netzone_t netzone, xbt_dict_t whereto)
309 {
310   for (auto const& elem : netzone->get_children()) {
311     xbt_dict_set(whereto, elem->get_cname(), elem);
312   }
313 }
314
315 const char* sg_zone_get_property_value(const_sg_netzone_t netzone, const char* name)
316 {
317   return netzone->get_property(name);
318 }
319
320 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, const char* value)
321 {
322   netzone->set_property(name, value);
323 }
324
325 void sg_zone_get_hosts(const_sg_netzone_t netzone, xbt_dynar_t whereto)
326 {
327   /* converts vector to dynar */
328   std::vector<simgrid::s4u::Host*> hosts = netzone->get_all_hosts();
329   for (auto const& host : hosts)
330     xbt_dynar_push(whereto, &host);
331 }