| 1 | /// @file |
| 2 | /// @author rfree (current maintainer in monero.cc project) |
| 3 | /// @brief implementaion for throttling of connection (count and rate-limit speed etc) |
| 4 | |
| 5 | // Copyright (c) 2014-2022, The Monero Project |
| 6 | // |
| 7 | // All rights reserved. |
| 8 | // |
| 9 | // Redistribution and use in source and binary forms, with or without modification, are |
| 10 | // permitted provided that the following conditions are met: |
| 11 | // |
| 12 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 13 | // conditions and the following disclaimer. |
| 14 | // |
| 15 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 16 | // of conditions and the following disclaimer in the documentation and/or other |
| 17 | // materials provided with the distribution. |
| 18 | // |
| 19 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 20 | // used to endorse or promote products derived from this software without specific |
| 21 | // prior written permission. |
| 22 | // |
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 24 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 26 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 28 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 30 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 31 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | |
| 33 | /* rfree: throttle details, implementing rate limiting */ |
| 34 | |
| 35 | |
| 36 | #ifndef INCLUDED_throttle_detail_hpp |
| 37 | #define INCLUDED_throttle_detail_hpp |
| 38 | |
| 39 | #include <boost/circular_buffer.hpp> |
| 40 | #include "network_throttle.hpp" |
| 41 | |
| 42 | namespace epee |
| 43 | { |
| 44 | namespace net_utils |
| 45 | { |
| 46 | |
| 47 | |
| 48 | class network_throttle : public i_network_throttle { |
| 49 | public: |
| 50 | struct packet_info { |
| 51 | size_t m_size; // octets sent. Summary for given small-window (e.g. for all packaged in 1 second) |
| 52 | packet_info(); |
| 53 | }; |
| 54 | |
| 55 | private: |
| 56 | network_speed_bps m_target_speed; |
| 57 | size_t m_network_add_cost; // estimated add cost of headers |
| 58 | size_t m_network_minimal_segment; // estimated minimal cost of sending 1 byte to round up to |
| 59 | size_t m_network_max_segment; // recommended max size of 1 TCP transmission |
| 60 | |
| 61 | const size_t m_window_size; // the number of samples to average over |
| 62 | network_time_seconds m_slot_size; // the size of one slot. TODO: now hardcoded for 1 second e.g. in time_to_slot() |
| 63 | // TODO for big window size, for performance better the substract on change of m_last_sample_time instead of recalculating average of eg >100 elements |
| 64 | |
| 65 | boost::circular_buffer< packet_info > m_history; // the history of bw usage |
| 66 | network_time_seconds m_last_sample_time; // time of last history[0] - so we know when to rotate the buffer |
| 67 | network_time_seconds m_start_time; // when we were created |
| 68 | bool m_any_packet_yet; // did we yet got any packet to count |
| 69 | uint64_t m_total_packets; |
| 70 | uint64_t m_total_bytes; |
| 71 | |
| 72 | std::string m_name; // my name for debug and logs |
| 73 | std::string m_nameshort; // my name for debug and logs (used in log file name) |
| 74 | |
| 75 | // each sample is now 1 second |
| 76 | public: |
| 77 | network_throttle(const std::string &nameshort, const std::string &name, int window_size=-1); |
| 78 | virtual ~network_throttle(); |
| 79 | virtual void set_name(const std::string &name); |
| 80 | virtual void set_target_speed( network_speed_kbps target ); |
| 81 | virtual network_speed_kbps get_target_speed(); |
| 82 | |
| 83 | // add information about events: |
| 84 | virtual void handle_trafic_exact(size_t packet_size); ///< count the new traffic/packet; the size is exact considering all network costs |
| 85 | virtual void handle_trafic_tcp(size_t packet_size); ///< count the new traffic/packet; the size is as TCP, we will consider MTU etc |
| 86 | |
| 87 | virtual void tick(); ///< poke and update timers/history (recalculates, moves the history if needed, checks the real clock etc) |
| 88 | |
| 89 | virtual double get_time_seconds() const ; ///< timer that we use, time in seconds, monotionic |
| 90 | |
| 91 | // time calculations: |
| 92 | virtual void calculate_times(size_t packet_size, calculate_times_struct &cts, bool dbg, double force_window) const; ///< MAIN LOGIC (see base class for info) |
| 93 | |
| 94 | virtual network_time_seconds get_sleep_time_after_tick(size_t packet_size); ///< increase the timer if needed, and get the package size |
| 95 | virtual network_time_seconds get_sleep_time(size_t packet_size) const; ///< gets the Delay (recommended Delay time) from calc. (not safe: only if time didnt change?) TODO |
| 96 | |
| 97 | virtual size_t get_recommended_size_of_planned_transport() const; ///< what should be the size (bytes) of next data block to be transported |
| 98 | virtual size_t get_recommended_size_of_planned_transport_window(double force_window) const; ///< ditto, but for given windows time frame |
| 99 | virtual double get_current_speed() const; |
| 100 | virtual void get_stats(uint64_t &total_packets, uint64_t &total_bytes) const; |
| 101 | |
| 102 | private: |
| 103 | virtual network_time_seconds time_to_slot(network_time_seconds t) const { return std::floor( t ); } // convert exact time eg 13.7 to rounded time for slot number in history 13 |
| 104 | virtual void _handle_trafic_exact(size_t packet_size, size_t orginal_size); |
| 105 | virtual void logger_handle_net(const std::string &filename, double time, size_t size); |
| 106 | }; |
| 107 | |
| 108 | /*** |
| 109 | * The complete set of traffic throttle for one typical connection |
| 110 | */ |
| 111 | struct network_throttle_bw { |
| 112 | public: |
| 113 | network_throttle m_in; ///< for incomming traffic (this we can not controll directly as it depends of what others send to us - usually) |
| 114 | network_throttle m_inreq; ///< for requesting incomming traffic (this is exact usually) |
| 115 | network_throttle m_out; ///< for outgoing traffic that we just sent (this is exact usually) |
| 116 | |
| 117 | public: |
| 118 | network_throttle_bw(const std::string &name1); |
| 119 | }; |
| 120 | |
| 121 | |
| 122 | |
| 123 | } // namespace net_utils |
| 124 | } // namespace epee |
| 125 | |
| 126 | |
| 127 | #endif |
| 128 | |
| 129 |