1 /* Copyright (c) 2004-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. */
9 #include <simgrid/forward.h>
10 #include <simgrid/kernel/resource/Action.hpp>
11 #include <simgrid/link.h>
13 #include <unordered_map>
14 #include <xbt/Extendable.hpp>
16 #include <xbt/signal.hpp>
24 extern template class XBT_PUBLIC xbt::Extendable<s4u::Link>;
29 * A Link represents the network facilities between :cpp:class:`hosts <simgrid::s4u::Host>`.
32 class XBT_PUBLIC Link : public xbt::Extendable<Link> {
34 friend kernel::resource::LinkImpl;
38 // Links are created from the NetZone, and destroyed by their private implementation when the simulation ends
39 explicit Link(kernel::resource::LinkImplIntf* pimpl) : pimpl_(pimpl) {}
40 virtual ~Link() = default;
41 // The implementation that never changes
42 kernel::resource::LinkImplIntf* const pimpl_;
45 enum class SharingPolicy { WIFI = 3, SPLITDUPLEX = 2, SHARED = 1, FATPIPE = 0 };
47 kernel::resource::LinkImpl* get_impl() const;
49 /** @brief Retrieve a link from its name */
50 static Link* by_name(const std::string& name);
51 static Link* by_name_or_null(const std::string& name);
53 /** @brief Retrieves the name of that link as a C++ string */
54 const std::string& get_name() const;
55 /** @brief Retrieves the name of that link as a C string */
56 const char* get_cname() const;
58 /** Get/Set the bandwidth of the current Link (in bytes per second) */
59 double get_bandwidth() const;
60 Link* set_bandwidth(double value);
62 /** Get/Set the latency of the current Link (in seconds) */
63 double get_latency() const;
65 * @brief Set link's latency
67 * @param value New latency value (in s)
69 Link* set_latency(double value);
71 * @brief Set latency (string version)
73 * Accepts values with units, such as '1s' or '7ms'.
75 * Full list of accepted units: w (week), d (day), h, s, ms, us, ns, ps.
77 * @throw std::invalid_argument if latency format is incorrect.
79 Link* set_latency(const std::string& value);
81 /** @brief Describes how the link is shared between flows */
82 Link* set_sharing_policy(SharingPolicy policy);
83 SharingPolicy get_sharing_policy() const;
85 /** Setup the profile with states events (ON or OFF). The profile must contain boolean values. */
86 Link* set_state_profile(kernel::profile::Profile* profile);
87 /** Setup the profile with bandwidth events (peak speed changes due to external load).
88 * The profile must contain percentages (value between 0 and 1). */
89 Link* set_bandwidth_profile(kernel::profile::Profile* profile);
90 /** Setup the profile file with latency events (peak latency changes due to external load).
91 * The profile must contain absolute values */
92 Link* set_latency_profile(kernel::profile::Profile* profile);
94 const std::unordered_map<std::string, std::string>* get_properties() const;
95 const char* get_property(const std::string& key) const;
96 Link* set_properties(const std::unordered_map<std::string, std::string>& properties);
97 Link* set_property(const std::string& key, const std::string& value);
100 * @brief Set the number of communications that can shared this link at the same time
102 * Use this method to serialize communication flows going through this link.
103 * Use -1 to set no limit.
105 * @param limit Number of concurrent flows
107 Link* set_concurrency_limit(int limit);
109 /** @brief Set the level of communication speed of the given host on this wifi link.
111 * The bandwidth of a wifi link for a given host depends on its SNR (signal to noise ratio),
112 * which ultimately depends on the distance between the host and the station and the material between them.
114 * This is modeled in SimGrid by providing several bandwidths to wifi links, one per SNR level (just provide
115 * comma-separated values in the XML file). By default, the first level in the list is used, but you can use the
116 * current function to specify that a given host uses another level of bandwidth. This can be used to take the
117 * location of hosts into account, or even to model mobility in your SimGrid simulation.
119 * Note that this function asserts that the link is actually a wifi link */
120 void set_host_wifi_rate(const s4u::Host* host, int level) const;
122 /** @brief Returns the current load (in bytes per second) */
123 double get_usage() const;
125 /** @brief Check if the Link is used (at least one flow uses the link) */
126 bool is_used() const;
128 /** @brief Check if the Link is shared (not a FATPIPE) */
129 bool is_shared() const;
138 /** @brief Callback signal fired when a new Link is created */
139 static xbt::signal<void(Link&)> on_creation;
141 /** @brief Callback signal fired when a Link is destroyed */
142 static xbt::signal<void(Link const&)> on_destruction;
144 /** @brief Callback signal fired when the state of a Link changes (when it is turned on or off) */
145 static xbt::signal<void(Link const&)> on_state_change;
147 /** @brief Callback signal fired when the bandwidth of a Link changes */
148 static xbt::signal<void(Link const&)> on_bandwidth_change;
150 /** @brief Callback signal fired when a communication starts */
151 static xbt::signal<void(kernel::resource::NetworkAction&)> on_communicate;
153 /** @brief Callback signal fired when a communication changes it state (ready/done/cancel) */
154 static xbt::signal<void(kernel::resource::NetworkAction&, kernel::resource::Action::State)>
155 on_communication_state_change;
160 * A SplitDuplexLink encapsulates the :cpp:class:`links <simgrid::s4u::Link>` which
161 * compose a Split-Duplex link. Remember that a Split-Duplex link is nothing more than
162 * a pair of up/down links.
165 class XBT_PUBLIC SplitDuplexLink : public Link {
167 explicit SplitDuplexLink(kernel::resource::LinkImplIntf* pimpl) : Link(pimpl) {}
168 /** @brief Get the link direction up*/
169 Link* get_link_up() const;
170 /** @brief Get the link direction down */
171 Link* get_link_down() const;
173 /** @brief Retrieve a link from its name */
174 static SplitDuplexLink* by_name(const std::string& name);
179 * Another encapsulation for using links in the :cpp:func:`NetZone::add_route`
181 * When adding a route with split-duplex links, you need to specify the direction of the link
182 * so SimGrid can know exactly which physical link to insert in the route.
184 * For shared/fat-pipe links, use the :cpp:enum:`Direction::NONE` since they don't have
185 * the concept of UP/DOWN links.
188 class XBT_PUBLIC LinkInRoute {
190 enum class Direction { UP = 2, DOWN = 1, NONE = 0 };
192 explicit LinkInRoute(const Link* link) : link_(link) {}
193 LinkInRoute(const Link* link, Direction d) : link_(link), direction_(d) {}
195 /** @brief Get direction of this link in the route: UP or DOWN */
196 Direction get_direction() const { return direction_; }
197 /** @brief Get pointer to the link */
198 const Link* get_link() const { return link_; }
202 Direction direction_ = Direction::NONE;
206 } // namespace simgrid
208 #endif /* S4U_LINK_HPP */