1 /* Copyright (c) 2006-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 #include "simgrid/Exception.hpp"
7 #include "simgrid/kernel/routing/NetPoint.hpp"
8 #include "simgrid/s4u/Engine.hpp"
9 #include "simgrid/s4u/Host.hpp"
10 #include "simgrid/s4u/NetZone.hpp"
11 #include "simgrid/simix.hpp"
12 #include "simgrid/zone.h"
13 #include "src/surf/network_interface.hpp"
14 #include "xbt/parse_units.hpp"
19 xbt::signal<void(bool symmetrical, kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
20 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
21 std::vector<kernel::resource::LinkImpl*> const& link_list)>
22 NetZone::on_route_creation;
23 xbt::signal<void(NetZone const&)> NetZone::on_creation;
24 xbt::signal<void(NetZone const&)> NetZone::on_seal;
26 const std::unordered_map<std::string, std::string>* NetZone::get_properties() const
28 return pimpl_->get_properties();
31 /** Retrieve the property value (or nullptr if not set) */
32 const char* NetZone::get_property(const std::string& key) const
34 return pimpl_->get_property(key);
37 void NetZone::set_property(const std::string& key, const std::string& value)
39 kernel::actor::simcall([this, &key, &value] { pimpl_->set_property(key, value); });
42 /** @brief Returns the list of direct children (no grand-children) */
43 std::vector<NetZone*> NetZone::get_children() const
45 std::vector<NetZone*> res;
46 for (auto child : pimpl_->get_children())
47 res.push_back(child->get_iface());
51 NetZone* NetZone::add_child(NetZone* new_zone) // XBT_ATTRIB_DEPRECATED_v332
53 new_zone->set_parent(this);
57 const std::string& NetZone::get_name() const
59 return pimpl_->get_name();
62 const char* NetZone::get_cname() const
64 return pimpl_->get_cname();
67 NetZone* NetZone::get_father() const // XBT_ATTRIB_DEPRECATED_v331
69 return pimpl_->get_parent()->get_iface();
72 NetZone* NetZone::get_parent() const
74 return pimpl_->get_parent()->get_iface();
77 NetZone* NetZone::set_parent(const NetZone* parent)
79 kernel::actor::simcall([this, parent] { pimpl_->set_parent(parent->get_impl()); });
83 /** @brief Returns the list of the hosts found in this NetZone (not recursively)
85 * Only the hosts that are directly contained in this NetZone are retrieved,
86 * not the ones contained in sub-netzones.
88 std::vector<Host*> NetZone::get_all_hosts() const
90 return pimpl_->get_all_hosts();
93 int NetZone::get_host_count() const
95 return pimpl_->get_host_count();
98 int NetZone::add_component(kernel::routing::NetPoint* elm)
100 return pimpl_->add_component(elm);
103 // XBT_ATTRIB_DEPRECATED_v332
104 std::vector<LinkInRoute> NetZone::convert_to_linkInRoute(const std::vector<kernel::resource::LinkImpl*>& link_list)
106 std::vector<LinkInRoute> links;
107 for (const auto* link : link_list) {
108 links.emplace_back(LinkInRoute(link->get_iface()));
113 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
114 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
115 const std::vector<LinkInRoute>& link_list, bool symmetrical)
117 pimpl_->add_route(src, dst, gw_src, gw_dst, link_list, symmetrical);
120 // XBT_ATTRIB_DEPRECATED_v332
121 void NetZone::add_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
122 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
123 const std::vector<kernel::resource::LinkImpl*>& link_list, bool symmetrical)
125 pimpl_->add_route(src, dst, gw_src, gw_dst, convert_to_linkInRoute(link_list), symmetrical);
128 // XBT_ATTRIB_DEPRECATED_v332
129 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
130 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
131 std::vector<kernel::resource::LinkImpl*>& link_list, bool /*symmetrical*/)
133 pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, convert_to_linkInRoute(link_list));
136 void NetZone::add_bypass_route(kernel::routing::NetPoint* src, kernel::routing::NetPoint* dst,
137 kernel::routing::NetPoint* gw_src, kernel::routing::NetPoint* gw_dst,
138 const std::vector<LinkInRoute>& link_list)
140 pimpl_->add_bypass_route(src, dst, gw_src, gw_dst, link_list);
143 void NetZone::extract_xbt_graph(const s_xbt_graph_t* graph, std::map<std::string, xbt_node_t, std::less<>>* nodes,
144 std::map<std::string, xbt_edge_t, std::less<>>* edges)
146 for (auto const& child : get_children())
147 child->extract_xbt_graph(graph, nodes, edges);
149 pimpl_->get_graph(graph, nodes, edges);
152 NetZone* NetZone::seal()
154 kernel::actor::simcall([this] { pimpl_->seal(); });
158 s4u::Host* NetZone::create_host(const std::string& name, double speed)
160 return create_host(name, std::vector<double>{speed});
163 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<double>& speed_per_pstate)
165 return kernel::actor::simcall(
166 [this, &name, &speed_per_pstate] { return pimpl_->create_host(name, speed_per_pstate); });
169 s4u::Host* NetZone::create_host(const std::string& name, const std::string& speed)
171 return create_host(name, std::vector<std::string>{speed});
174 s4u::Host* NetZone::create_host(const std::string& name, const std::vector<std::string>& speed_per_pstate)
176 return create_host(name, Host::convert_pstate_speed_vector(speed_per_pstate));
179 s4u::Link* NetZone::create_link(const std::string& name, double bandwidth)
181 return create_link(name, std::vector<double>{bandwidth});
184 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<double>& bandwidths)
186 return kernel::actor::simcall([this, &name, &bandwidths] { return pimpl_->create_link(name, bandwidths); });
189 s4u::Link* NetZone::create_link(const std::string& name, const std::string& bandwidth)
191 return create_link(name, std::vector<std::string>{bandwidth});
194 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, const std::string& bandwidth)
198 speed = xbt_parse_get_bandwidth("", 0, bandwidth, "");
199 } catch (const simgrid::ParseError&) {
200 throw std::invalid_argument(std::string("Impossible to create split-duplex link: ") + name +
201 std::string(". Invalid bandwidth: ") + bandwidth);
203 return create_split_duplex_link(name, speed);
206 s4u::SplitDuplexLink* NetZone::create_split_duplex_link(const std::string& name, double bandwidth)
208 return kernel::actor::simcall(
209 [this, &name, &bandwidth] { return pimpl_->create_split_duplex_link(name, std::vector<double>{bandwidth}); });
212 s4u::Link* NetZone::create_link(const std::string& name, const std::vector<std::string>& bandwidths)
214 std::vector<double> bw;
215 bw.reserve(bandwidths.size());
216 for (const auto& speed_str : bandwidths) {
218 double speed = xbt_parse_get_bandwidth("", 0, speed_str, "");
220 } catch (const simgrid::ParseError&) {
221 throw std::invalid_argument(std::string("Impossible to create link: ") + name +
222 std::string(". Invalid bandwidth: ") + speed_str);
225 return create_link(name, bw);
228 kernel::routing::NetPoint* NetZone::create_router(const std::string& name)
230 return kernel::actor::simcall([this, &name] { return pimpl_->create_router(name); });
233 kernel::routing::NetPoint* NetZone::get_netpoint()
235 return pimpl_->get_netpoint();
238 kernel::resource::NetworkModelIntf* NetZone::get_network_model() const
240 kernel::resource::NetworkModelIntf* model = pimpl_->get_network_model().get();
244 } // namespace simgrid
246 /* **************************** Public C interface *************************** */
248 sg_netzone_t sg_zone_get_root()
250 return simgrid::s4u::Engine::get_instance()->get_netzone_root();
253 const char* sg_zone_get_name(const_sg_netzone_t netzone)
255 return netzone->get_cname();
258 sg_netzone_t sg_zone_get_by_name(const char* name)
260 return simgrid::s4u::Engine::get_instance()->netzone_by_name_or_null(name);
263 void sg_zone_get_sons(const_sg_netzone_t netzone, xbt_dict_t whereto)
265 for (auto const& elem : netzone->get_children()) {
266 xbt_dict_set(whereto, elem->get_cname(), elem);
270 const char* sg_zone_get_property_value(const_sg_netzone_t netzone, const char* name)
272 return netzone->get_property(name);
275 void sg_zone_set_property_value(sg_netzone_t netzone, const char* name, const char* value)
277 netzone->set_property(name, value);
280 void sg_zone_get_hosts(const_sg_netzone_t netzone, xbt_dynar_t whereto)
282 /* converts vector to dynar */
283 std::vector<simgrid::s4u::Host*> hosts = netzone->get_all_hosts();
284 for (auto const& host : hosts)
285 xbt_dynar_push(whereto, &host);