test_control.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 executing DHCP performance
  27. /// test end to end.
  28. ///
  29. class TestControl : public boost::noncopyable {
  30. public:
  31. /// \brief Socket wrapper class.
  32. ///
  33. /// This is wrapper class that holds descriptor of the socket
  34. /// used to run DHCP test. All sockets created with \ref IfaceMgr
  35. /// are closed in the destructor. This ensures that socket is
  36. /// closed when the function that created the socket ends
  37. /// (normally or when exception occurs).
  38. class TestControlSocket {
  39. public:
  40. /// \brief Constructor of socket wrapper class.
  41. ///
  42. /// This constructor uses provided socket descriptor to
  43. /// find the name of the interface where socket has been
  44. /// bound to.
  45. ///
  46. /// \param socket socket descriptor.
  47. /// \throw isc::BadValue if interface for specified
  48. /// socket descriptor does not exist.
  49. TestControlSocket(const int socket);
  50. /// \brief Destriuctor of the socket wrapper class.
  51. ///
  52. /// Destructor closes all open sockets on all interfaces.
  53. /// TODO: close only the socket being wrapped by this class.
  54. ~TestControlSocket();
  55. /// \brief Return name of the interface where socket is bound to.
  56. ///
  57. /// \return name of the interface where socket is bound to.
  58. const std::string& getIface() const { return(iface_); }
  59. private:
  60. /// \brief Private default constructor.
  61. ///
  62. /// Default constructor is private to make sure that valid
  63. /// socket descriptor is passed to create object.
  64. TestControlSocket();
  65. /// \brief Initialize name of the interface.
  66. ///
  67. /// This method intializes the name of the interface where
  68. /// socket is bound to. This name can be later retreived by
  69. /// client class to set interface name in DHCP packet objects.
  70. ///
  71. /// \throw isc::BadValue if interface for specified socket
  72. /// descriptor does not exist.
  73. void initInterface();
  74. int socket_; ///< Socket descirptor.
  75. std::string iface_; ///< Name of the interface.
  76. };
  77. /// \brief Length of the Ethernet HW address (MAC) in bytes.
  78. static const uint8_t HW_ETHER_LEN = 6;
  79. /// TestControl is a singleton class. This method returns reference
  80. /// to its sole instance.
  81. ///
  82. /// \return the only existing instance of test control
  83. static TestControl& instance();
  84. /// Run performance test.
  85. ///
  86. /// Method runs whole performance test. Command line options must
  87. /// be parsed prior to running this function. Othewise function will
  88. /// throw exception.
  89. ///
  90. /// \throw isc::InvalidOperation if command line options are not parsed.
  91. /// \throw isc::Unexpected if internal Test Controler error occured.
  92. void run();
  93. private:
  94. /// \brief Private default constructor.
  95. ///
  96. /// Default constructor is private as the object can be created
  97. /// only via \ref instance method.
  98. TestControl();
  99. /// \brief Check if test exit condtitions fulfiled.
  100. ///
  101. /// Method checks if test exit conditions are fulfiled.
  102. /// Exit conditions are checked periodically from the
  103. /// main loop. Program should break the main loop when
  104. /// this method returns true. It is calling function
  105. /// responsibility to break main loop gracefully and
  106. /// cleanup after test execution.
  107. ///
  108. /// \return true if any of the exit conditions is fulfiled.
  109. bool checkExitConditions() const;
  110. /// \brief Create DHCPv4 DISCOVER packet.
  111. ///
  112. /// Create instance of DHCPv4 DISCOVER packet with ethernet
  113. /// HW type and MAC address specified as parameter. The following
  114. /// DHCP options are added to the packet:
  115. /// - DHO_DHCP_MESSAGE_TYPE with DHCPDISCOVER message type value
  116. /// - DHO_DHCP_PARAMETER_REQUEST_LIST with the following options
  117. /// being requested from the server:
  118. /// - DHO_SUBNET_MASK,
  119. /// - DHO_BROADCAST_ADDRESS,
  120. /// - DHO_TIME_OFFSET,
  121. /// - DHO_ROUTERS,
  122. /// - DHO_DOMAIN_NAME,
  123. /// - DHO_DOMAIN_NAME_SERVERS,
  124. /// - DHO_HOST_NAME.
  125. ///
  126. /// \param mac_addr MAC address to be set for the packet. MAC address
  127. /// has to be exactly 6 octets long.
  128. /// \throw isc::Unexpected if failed to create new packet instance.
  129. /// \throw isc::BadValue if MAC address has invalid length.
  130. /// \return insance of the DHCPv4 DISCOVER packet.
  131. boost::shared_ptr<dhcp::Pkt4>
  132. createDiscoverPkt4(const std::vector<uint8_t>& mac_addr) const;
  133. /// \brief Factory function to create generic option.
  134. ///
  135. /// Factory function is registered using \ref LibDHCP::OptionFactoryRegister.
  136. /// Registered factory functions provide a way to create options of the
  137. /// same type in the same way. When new option instance is needed the
  138. /// corresponding factory function is called to create it. This is done
  139. /// by calling \ref Option::factory with DHCP message type specified as
  140. /// one of parameters. Some of the parameters passed to factory function
  141. /// may be ignored (e.g. option buffer). For generic option however, factory
  142. /// function creates option using contents of the buffer.
  143. ///
  144. /// \param u universe (V6 or V4).
  145. /// \param type option-type.
  146. /// \param buf option-buffer.
  147. /// \return instance o the generic option.
  148. static dhcp::OptionPtr factoryGeneric4(dhcp::Option::Universe u,
  149. uint16_t type,
  150. const dhcp::OptionBuffer& buf);
  151. /// \brief Factory function to create DHCPv4 Request List option.
  152. ///
  153. /// Factory function is registered using \ref LibDHCP::OptionFactoryRegister.
  154. /// Registered factory functions provide a way to create options of the
  155. /// same type in the same way. When new option instance is needed the
  156. /// corresponding factory function is called to create it. This is done
  157. /// by calling \ref Option::factory with DHCP message type specified as
  158. /// one of parameters. This factory function ignores contents of the
  159. /// buffer provided and creates option buffer internally with the following
  160. /// list of requested options:
  161. /// - DHO_SUBNET_MASK,
  162. /// - DHO_BROADCAST_ADDRESS,
  163. /// - DHO_TIME_OFFSET,
  164. /// - DHO_ROUTERS,
  165. /// - DHO_DOMAIN_NAME,
  166. /// - DHO_DOMAIN_NAME_SERVERS,
  167. /// - DHO_HOST_NAME.
  168. ///
  169. /// \param u universe (V6 or V4).
  170. /// \param type option-type.
  171. /// \param buf option-buffer.
  172. /// \return instance o the generic option.
  173. static dhcp::OptionPtr factoryRequestList4(dhcp::Option::Universe u,
  174. uint16_t type,
  175. const dhcp::OptionBuffer& buf);
  176. /// \brief Generate MAC address.
  177. ///
  178. /// This method generates MAC address. The number of unique
  179. /// MAC addresses it can generate is determined by the number
  180. /// simulated DHCP clients specified from command line. It uses
  181. /// \ref CommandOptions object to retrieve number of clients.
  182. /// Based on this the random value is generated and added to
  183. /// the MAC address prefix (default MAC address).
  184. ///
  185. /// \return generated MAC address.
  186. std::vector<uint8_t> generateMacAddress() const;
  187. /// \brief Returns number of exchanges to be started.
  188. ///
  189. /// Method returns number of new exchanges to be started as soon
  190. /// as possible to satisfy expected rate. Calculation used here
  191. /// is based on current time, due time calculated with
  192. /// \ref updateSendTime function and expected rate.
  193. ///
  194. /// \return number of exchanges to be started immediatelly.
  195. uint64_t getNextExchangesNum() const;
  196. /// \brief Open socket to communicate with DHCP server.
  197. ///
  198. /// Method opens socket and binds it to local address. Function will
  199. /// can use either interface name, local address or server address
  200. /// to create a socket, depending on what is available (specified
  201. /// from the command line). If socket can't be created for any
  202. /// reason, exception is thrown.
  203. ///
  204. /// \throw isc::BadValue if socket can't be created.
  205. /// \return socket descriptor.
  206. int openSocket() const;
  207. /// \brief Register option factory functions for DHCPv4
  208. ///
  209. /// Method registers option factory functions for DHCPv4.
  210. /// These functions are called to create instances of DHCPv4
  211. /// options. Call \ref Option::factory to invoke factory
  212. /// function for particular option. Don't use this function directly.
  213. /// Use \ref registerOptionFactories instead.
  214. void registerOptionFactories4() const;
  215. /// \brief Register option factory functions for DHCPv6
  216. ///
  217. /// Method registers option factory functions for DHCPv6.
  218. /// These functions are called to create instances of DHCPv6
  219. /// options. Call \ref Option::factory to invoke factory
  220. /// function for particular option. Don't use this function directly.
  221. /// Use \ref registerOptionFactories instead.
  222. void registerOptionFactories6() const;
  223. /// \brief Register option factory functions for DHCPv4 or DHCPv6.
  224. ///
  225. /// Method registers option factory functions for DHCPv4 or DHCPv6,
  226. /// depending in whch mode test is currently running.
  227. void registerOptionFactories() const;
  228. /// \brief Start new exchange of DHCP messages.
  229. ///
  230. /// Method starts new DHCP exchange by sending new DHCPv4 DISCOVER
  231. /// or DHCPv6 SOLICIT packet to the server.
  232. ///
  233. /// \param socket socket used to send DHCP message.
  234. /// \throw isc::Unexpected if failed to create or send packet
  235. void startExchange(const TestControlSocket& socket);
  236. /// \brief Update due time to initiate next chunk of exchanges.
  237. ///
  238. /// Method updates due time to initiate next chunk of exchanges.
  239. /// Function takes current time, last sent packet's time and
  240. /// expected rate in its calculations.
  241. void updateSendDue();
  242. boost::posix_time::ptime send_due_; ///< Due time to initiate next chunk
  243. ///< of exchanges.
  244. boost::posix_time::ptime last_sent_; ///< Indicates when the last exchange
  245. /// was initiated.
  246. uint64_t sent_packets_0_;
  247. uint64_t sent_packets_1_;
  248. };
  249. } // namespace perfdhcp
  250. } // namespace isc
  251. #endif // __COMMAND_OPTIONS_H