test_control.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __TEST_CONTROL_H
  15. #define __TEST_CONTROL_H
  16. #include <string>
  17. #include <vector>
  18. #include <boost/noncopyable.hpp>
  19. #include <boost/date_time/posix_time/posix_time.hpp>
  20. #include <dhcp/pkt4.h>
  21. #include <dhcp/pkt6.h>
  22. namespace isc {
  23. namespace perfdhcp {
  24. /// \brief Test Control class.
  25. ///
  26. /// This class is responsible for running whole perfdhcp test.
  27. ///
  28. class TestControl : public boost::noncopyable {
  29. public:
  30. class TestControlSocket {
  31. public:
  32. TestControlSocket(const int socket);
  33. ~TestControlSocket();
  34. const std::string& getIface() const { return(iface_); }
  35. private:
  36. void initInterface();
  37. int socket_;
  38. std::string iface_;
  39. };
  40. static const uint8_t HW_ETHER_LEN = 6;
  41. /// TestControl is a singleton class. This method returns reference
  42. /// to its sole instance.
  43. ///
  44. /// \return the only existing instance of test control
  45. static TestControl& instance();
  46. /// Run performance test.
  47. ///
  48. /// Method runs whole performance test. Command line options must
  49. /// be parsed prior to running this function. Othewise function will
  50. /// throw exception.
  51. ///
  52. /// \throw isc::InvalidOperation if command line options are not parsed.
  53. /// \throw isc::Unexpected if internal Test Controler error occured.
  54. void run();
  55. private:
  56. /// \brief Private default constructor.
  57. ///
  58. /// Default constructor is private as the object can be created
  59. /// only via \ref instance method.
  60. TestControl();
  61. /// \brief Check if test exit condtitions fulfiled.
  62. ///
  63. /// Method checks if test exit conditions are fulfiled.
  64. /// Exit conditions are checked periodically from the
  65. /// main loop. Program should break the main loop when
  66. /// this method returns true. It is calling function
  67. /// responsibility to break main loop gracefully and
  68. /// cleanup after test execution.
  69. ///
  70. /// \return true if any of the exit conditions is fulfiled.
  71. bool checkExitConditions() const;
  72. boost::shared_ptr<dhcp::Pkt4>
  73. createDiscoverPkt4(const std::vector<uint8_t>& mac_addr) const;
  74. static dhcp::OptionPtr factoryGeneric4(dhcp::Option::Universe u,
  75. uint16_t type,
  76. const dhcp::OptionBuffer& buf);
  77. static dhcp::OptionPtr factoryRequestList4(dhcp::Option::Universe u,
  78. uint16_t type,
  79. const dhcp::OptionBuffer& buf);
  80. const std::vector<uint8_t>& generateMacAddress();
  81. /// \brief Returns number of exchanges to be started.
  82. ///
  83. /// Method returns number of new exchanges to be started as soon
  84. /// as possible to satisfy expected rate. Calculation used here
  85. /// is based on current time, due time calculated with
  86. /// \ref updateSendTime function and expected rate.
  87. ///
  88. /// \return number of exchanges to be started immediatelly.
  89. uint64_t getNextExchangesNum() const;
  90. int openSocket() const;
  91. void registerOptionFactories4() const;
  92. void registerOptionFactories6() const;
  93. void registerOptionFactories() const;
  94. void resetMacAddress();
  95. /// \brief Start new exchange of DHCP messages.
  96. ///
  97. void startExchange(const TestControlSocket& socket);
  98. /// \brief Update due time to initiate next chunk of exchanges.
  99. ///
  100. /// Method updates due time to initiate next chunk of exchanges.
  101. /// Function takes current time, last sent packet's time and
  102. /// expected rate in its calculations.
  103. void updateSendDue();
  104. boost::posix_time::ptime send_due_; ///< Due time to initiate next chunk
  105. ///< of exchanges.
  106. boost::posix_time::ptime last_sent_; ///< Indicates when the last exchange
  107. /// was initiated.
  108. std::vector<uint8_t> last_mac_address_;/// Least generated MAC address.
  109. uint64_t sent_packets_0_;
  110. uint64_t sent_packets_1_;
  111. };
  112. } // namespace perfdhcp
  113. } // namespace isc
  114. #endif // __COMMAND_OPTIONS_H