1 /* Copyright (c) 2006-2022. 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 #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>
15 #include "src/kernel/resource/LinkImpl.hpp"
16 #include "src/kernel/resource/StandardLinkImpl.hpp"
21 xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
22 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
23 std::vector<kernel::resource::StandardLinkImpl*> const& link_list)>
24 NetZone::on_route_creation;
25 xbt::signal<void(NetZone const&)> NetZone::on_creation;
26 xbt::signal<void(NetZone const&)> NetZone::on_seal;
28 const std::unordered_map<std::string, std::string>* NetZone::get_properties() const
30 return pimpl_->get_properties();
33 /** Retrieve the property value (or nullptr if not set) */
34 const char* NetZone::get_property(const std::string& key) const
36 return pimpl_->get_property(key);
39 void NetZone::set_property(const std::string& key, const std::string& value)
41 kernel::actor::simcall_answered([this, &key, &value] { pimpl_->set_property(key, value); });
44 /** @brief Returns the list of direct children (no grand-children) */
45 std::vector<NetZone*> NetZone::get_children() const
47 std::vector<NetZone*> res;
48 for (auto child : pimpl_->get_children())
49 res.push_back(child->get_iface());
53 NetZone* NetZone::add_child(NetZone* new_zone) // XBT_ATTRIB_DEPRECATED_v332
55 new_zone->set_parent(this);
59 const std::string& NetZone::get_name() const
61 return pimpl_->get_name();
64 const char* NetZone::get_cname() const
66 return pimpl_->get_cname();
69 NetZone* NetZone::get_parent() const
71 return pimpl_->get_parent()->get_iface();
74 NetZone* NetZone::set_parent(const NetZone* parent)
76 kernel::actor::simcall_answered([this, parent] { pimpl_->set_parent(parent->get_impl()); });
80 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
82 * Only the hosts that are directly contained in this NetZone are retrieved,
83 * not the ones contained in sub-netzones.
85 std::vector<Host*> NetZone::get_all_hosts() const
87 return pimpl_->get_all_hosts();
90 int NetZone::get_host_count() const
92 return pimpl_->get_host_count();
95 unsigned long NetZone::add_component(kernel::routing::NetPoint* elm)
97 return pimpl_->add_component(elm);
100 // XBT_ATTRIB_DEPRECATED_v332
101 std::vector<LinkInRoute>
102 NetZone::convert_to_linkInRoute(const std::vector<kernel::resource::StandardLinkImpl*>& link_list)
104 std::vector<LinkInRoute> links;
105 for (const auto* link : link_list) {
106 links.emplace_back(LinkInRoute(link->get_iface()));
111 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
112 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
113 const std::vector<LinkInRoute>& link_list, bool symmetrical)
115 pimpl_->add_route(src, dst, gw_src, gw_dst, link_list, symmetrical);
118 // XBT_ATTRIB_DEPRECATED_v332
119 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
120 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
121 const std::vector<kernel::resource::StandardLinkImpl*>& link_list, bool symmetrical)
123 pimpl_->add_route(src, dst, gw_src, gw_dst, convert_to_linkInRoute(link_list), symmetrical);
126 // XBT_ATTRIB_DEPRECATED_v332
127 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
128 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
129 const std::vector<kernel::resource::StandardLinkImpl*>& link_list, bool /*symmetrical*/)
131 pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, convert_to_linkInRoute(link_list));
134 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
135 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
136 const std::vector<LinkInRoute>& link_list)
138 pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, link_list);
141 void NetZone::extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
142 std::map<std::string, xbt_edge_t, std::less<>>* edges)
144 for (auto const& child : get_children())
145 child->extract_xbt_graph(graph, nodes, edges);
147 pimpl_->get_graph(graph, nodes, edges);
150 NetZone* NetZone::seal()
152 kernel::actor::simcall_answered([this] { pimpl_->seal(); });
156 s4u::Host* NetZone::create_host(const std::string& name, double speed)
158 return create_host(name, std::vector<double>{speed});
161 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<double>& speed_per_pstate)
163 return kernel::actor::simcall_answered(
164 [this, &name, &speed_per_pstate] { return pimpl_->create_host(name, speed_per_pstate); });
167 s4u::Host* NetZone::create_host(const std::string& name, const std::string& speed)
169 return create_host(name, std::vector<std::string>{speed});
172 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate)
174 return create_host(name, Host::convert_pstate_speed_vector(speed_per_pstate));
177 s4u::Link* NetZone::create_link(const std::string& name, double bandwidth)
179 return create_link(name, std::vector<double>{bandwidth});
182 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<double>& bandwidths)
184 return kernel::actor::simcall_answered([this, &name, &bandwidths] { return pimpl_->create_link(name, bandwidths); });
187 s4u::Link* NetZone::create_link(const std::string& name, const std::string& bandwidth)
189 return create_link(name, std::vector<std::string>{bandwidth});
192 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, const std::string& bandwidth)
196 speed = xbt_parse_get_bandwidth("", 0, bandwidth, "");
197 } catch (const simgrid::ParseError&) {
198 throw std::invalid_argument(std::string("Impossible to create split-duplex link: ") + name +
199 std::string(". Invalid bandwidth: ") + bandwidth);
201 return create_split_duplex_link(name, speed);
204 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, double bandwidth)
206 return kernel::actor::simcall_answered(
207 [this, &name, &bandwidth] { return pimpl_->create_split_duplex_link(name, std::vector<double>{bandwidth}); });
210 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<std::string>& bandwidths)
212 std::vector<double> bw;
213 bw.reserve(bandwidths.size());
214 for (const auto& speed_str : bandwidths) {
216 double speed = xbt_parse_get_bandwidth("", 0, speed_str, "");
218 } catch (const simgrid::ParseError&) {
219 throw std::invalid_argument(std::string("Impossible to create link: ") + name +
220 std::string(". Invalid bandwidth: ") + speed_str);
223 return create_link(name, bw);
226 kernel::routing::NetPoint* NetZone::create_router(const std::string& name)
228 return kernel::actor::simcall_answered([this, &name] { return pimpl_->create_router(name); });
231 kernel::routing::NetPoint* NetZone::get_netpoint()
233 return pimpl_->get_netpoint();
236 kernel::resource::NetworkModelIntf* NetZone::get_network_model() const
238 kernel::resource::NetworkModelIntf* model = pimpl_->get_network_model().get();
242 } // namespace simgrid
244 /* **************************** Public C interface *************************** */
246 sg_netzone_t sg_zone_get_root()
248 return simgrid::s4u::Engine::get_instance()->get_netzone_root();
251 const char* sg_zone_get_name(const_sg_netzone_t netzone)
253 return netzone->get_cname();
256 sg_netzone_t sg_zone_get_by_name(const char* name)
258 return simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(name);
261 void sg_zone_get_sons(const_sg_netzone_t netzone, xbt_dict_t whereto)
263 for (auto const& elem : netzone->get_children()) {
264 xbt_dict_set(whereto, elem->get_cname(), elem);
268 const char* sg_zone_get_property_value(const_sg_netzone_t netzone, const char* name)
270 return netzone->get_property(name);
273 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, const char* value)
275 netzone->set_property(name, value);
278 void sg_zone_get_hosts(const_sg_netzone_t netzone, xbt_dynar_t whereto)
280 /* converts vector to dynar */
281 std::vector<simgrid::s4u::Host*> hosts = netzone->get_all_hosts();
282 for (auto const& host : hosts)
283 xbt_dynar_push(whereto, &host);