test_control.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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/shared_ptr.hpp>
  20. #include <boost/function.hpp>
  21. #include <boost/date_time/posix_time/posix_time.hpp>
  22. #include <dhcp/dhcp6.h>
  23. #include <dhcp/pkt4.h>
  24. #include <dhcp/pkt6.h>
  25. #include "stats_mgr.h"
  26. namespace isc {
  27. namespace perfdhcp {
  28. /// \brief Test Control class.
  29. ///
  30. /// This class is responsible for executing DHCP performance
  31. /// test end to end.
  32. ///
  33. /// Option factory functions are registered using
  34. /// \ref LibDHCP::OptionFactoryRegister. Registered factory functions
  35. /// provide a way to create options of the same type in the same way.
  36. /// When new option instance is needed the corresponding factory
  37. /// function is called to create it. This is done by calling
  38. /// \ref Option::factory with DHCP message type specified as one of
  39. /// parameters. Some of the parameters passed to factory function
  40. /// may be ignored (e.g. option buffer).
  41. class TestControl : public boost::noncopyable {
  42. public:
  43. /// Default transaction id offset.
  44. static const size_t DHCPV4_TRANSID_OFFSET = 4;
  45. /// Default offset of MAC's last octet.
  46. static const size_t DHCPV4_RANDOMIZATION_OFFSET = 35;
  47. /// Default elapsed time offset.
  48. static const size_t DHCPV4_ELAPSED_TIME_OFFSET = 8;
  49. /// Default server id offset.
  50. static const size_t DHCPV4_SERVERID_OFFSET = 54;
  51. /// Default requested ip offset.
  52. static const size_t DHCPV4_REQUESTED_IP_OFFSET = 240;
  53. /// Default DHCPV6 transaction id offset.
  54. static const size_t DHCPV6_TRANSID_OFFSET = 1;
  55. /// Default DHCPV6 randomization offset (last octet of DUID)
  56. static const size_t DHCPV6_RANDOMIZATION_OFFSET = 21;
  57. /// Default DHCPV6 elapsed time offset.
  58. static const size_t DHCPV6_ELAPSED_TIME_OFFSET = 84;
  59. /// Default DHCPV6 server id offset.
  60. static const size_t DHCPV6_SERVERID_OFFSET = 22;
  61. /// Default DHCPV6 IA_NA offset.
  62. static const size_t DHCPV6_IA_NA_OFFSET = 40;
  63. /// Statistics Manager for DHCPv4.
  64. typedef StatsMgr<dhcp::Pkt4> StatsMgr4;
  65. /// Pointer to Statistics Manager for DHCPv4;
  66. typedef boost::shared_ptr<StatsMgr4> StatsMgr4Ptr;
  67. /// Statictics Manager for DHCPv6.
  68. typedef StatsMgr<dhcp::Pkt6> StatsMgr6;
  69. /// Pointer to Statistics Manager for DHCPv6.
  70. typedef boost::shared_ptr<StatsMgr6> StatsMgr6Ptr;
  71. /// Packet exchange type.
  72. typedef StatsMgr<>::ExchangeType ExchangeType;
  73. /// Packet template buffer.
  74. typedef std::vector<uint8_t> TemplateBuffer;
  75. /// Packet template buffers list.
  76. typedef std::vector<TemplateBuffer> TemplateBufferCollection;
  77. /// \brief Socket wrapper class.
  78. ///
  79. /// This is wrapper class that holds descriptor of the socket
  80. /// used to run DHCP test. All sockets created with \ref IfaceMgr
  81. /// are closed in the destructor. This ensures that socket is
  82. /// closed when the function that created the socket ends
  83. /// (normally or when exception occurs).
  84. class TestControlSocket {
  85. public:
  86. /// \brief Constructor of socket wrapper class.
  87. ///
  88. /// This constructor uses provided socket descriptor to
  89. /// find the name of the interface where socket has been
  90. /// bound to.
  91. ///
  92. /// \param socket socket descriptor.
  93. /// \throw isc::BadValue if interface for specified
  94. /// socket descriptor does not exist.
  95. TestControlSocket(const int socket);
  96. /// \brief Destriuctor of the socket wrapper class.
  97. ///
  98. /// Destructor closes all open sockets on all interfaces.
  99. /// TODO: close only the socket being wrapped by this class.
  100. ~TestControlSocket();
  101. /// \brief Return name of the interface where socket is bound to.
  102. ///
  103. /// \return name of the interface where socket is bound to.
  104. const std::string& getIface() const { return(iface_); }
  105. /// \brief Return interface index where socket is bound to.
  106. ///
  107. /// \return index fo the interface where sockert is bound to.
  108. int getIfIndex() const { return(ifindex_); }
  109. /// \brief Return address where socket is bound to.
  110. ///
  111. /// \return address where socket is bound to.
  112. const asiolink::IOAddress& getAddress() const { return(addr_); }
  113. private:
  114. /// \brief Private default constructor.
  115. ///
  116. /// Default constructor is private to make sure that valid
  117. /// socket descriptor is passed to create object.
  118. TestControlSocket();
  119. /// \brief Initialize socket data.
  120. ///
  121. /// This method initializes members of the class that Interface
  122. /// Manager holds: interface name, local address.
  123. ///
  124. /// \throw isc::BadValue if interface for specified socket
  125. /// descriptor does not exist.
  126. void initSocketData();
  127. int socket_; ///< Socket descirptor.
  128. std::string iface_; ///< Name of the interface.
  129. int ifindex_; ///< Index of the interface.
  130. asiolink::IOAddress addr_; ///< Address bound.
  131. };
  132. /// \brief Default transaction id generator class.
  133. ///
  134. /// This is default transaction id generator class. The member
  135. /// function is used to generate unique transaction id value.
  136. /// Other generator classes should derive from this one to
  137. /// override the standard generation algorithm (e.g. unit tests
  138. /// override this class wih algorithm that produces more predictable
  139. /// transaction id values).
  140. class TransidGenerator {
  141. public:
  142. /// \brief generate transaction id.
  143. ///
  144. /// \return generated transazction id value.
  145. virtual uint32_t generate() {
  146. return static_cast<uint32_t>(random() % 0x00FFFFFF);
  147. }
  148. };
  149. typedef boost::shared_ptr<TransidGenerator> TransidGeneratorPtr;
  150. /// \brief Length of the Ethernet HW address (MAC) in bytes.
  151. static const uint8_t HW_ETHER_LEN = 6;
  152. /// TestControl is a singleton class. This method returns reference
  153. /// to its sole instance.
  154. ///
  155. /// \return the only existing instance of test control
  156. static TestControl& instance();
  157. /// brief\ Run performance test.
  158. ///
  159. /// Method runs whole performance test. Command line options must
  160. /// be parsed prior to running this function. Othewise function will
  161. /// throw exception.
  162. ///
  163. /// \throw isc::InvalidOperation if command line options are not parsed.
  164. /// \throw isc::Unexpected if internal Test Controler error occured.
  165. void run();
  166. /// \brief Set new transaction id generator.
  167. ///
  168. /// \param generator generator object to be used.
  169. void setTransidGenerator(TransidGeneratorPtr& generator) {
  170. transid_gen_.reset();
  171. transid_gen_ = generator;
  172. }
  173. protected:
  174. // We would really like these methods and members to be private but
  175. // they have to be accessible for unit-testing. Another, possibly better,
  176. // solution is to make this class friend of test class but this is not
  177. // what's followed in other classes.
  178. /// \brief Default constructor.
  179. ///
  180. /// Default constructor is protected as the object can be created
  181. /// only via \ref instance method.
  182. TestControl();
  183. /// \brief Check if test exit condtitions fulfiled.
  184. ///
  185. /// Method checks if test exit conditions are fulfiled.
  186. /// Exit conditions are checked periodically from the
  187. /// main loop. Program should break the main loop when
  188. /// this method returns true. It is calling function
  189. /// responsibility to break main loop gracefully and
  190. /// cleanup after test execution.
  191. ///
  192. /// \return true if any of the exit conditions is fulfiled.
  193. bool checkExitConditions() const;
  194. /// \brief Factory function to create DHCPv6 ELAPSED_TIME option.
  195. ///
  196. /// This factory function creates DHCPv6 ELAPSED_TIME option instance.
  197. /// If empty buffer is passed the option buffer will be initialized
  198. /// to length 2 and values will be initialized to zeros. Otherwise
  199. /// function will initialize option buffer with values in passed buffer.
  200. ///
  201. /// \param u universe (V6 or V4).
  202. /// \param type option-type.
  203. /// \param buf option-buffer.
  204. /// \throw if elapsed time buffer size is neither 2 nor 0.
  205. /// \return instance o the option.
  206. static dhcp::OptionPtr
  207. factoryElapsedTime6(dhcp::Option::Universe u,
  208. uint16_t type,
  209. const dhcp::OptionBuffer& buf);
  210. /// \brief Factory function to create generic option.
  211. ///
  212. /// This factory function creates option with specified universe,
  213. /// type and buf. It does not have any additional logic validating
  214. /// the buffer contents, size etc.
  215. ///
  216. /// \param u universe (V6 or V4).
  217. /// \param type option-type.
  218. /// \param buf option-buffer.
  219. /// \return instance o the option.
  220. static dhcp::OptionPtr factoryGeneric(dhcp::Option::Universe u,
  221. uint16_t type,
  222. const dhcp::OptionBuffer& buf);
  223. /// \brief Factory function to create IA_NA option.
  224. ///
  225. /// This factory function creates DHCPv6 IA_NA option instance.
  226. /// \TODO: add support for IA Address options.
  227. /// \param u universe (V6 or V4).
  228. /// \param type option-type.
  229. /// \param buf option-buffer.
  230. /// \return instance of IA_NA option.
  231. static dhcp::OptionPtr factoryIana6(dhcp::Option::Universe u,
  232. uint16_t type,
  233. const dhcp::OptionBuffer& buf);
  234. /// \brief Factory function to create DHCPv6 ORO option.
  235. ///
  236. /// This factory function creates DHCPv6 Option Request Option instance.
  237. /// The created option will contain the following set of requested options:
  238. /// - D6O_NAME_SERVERS
  239. /// - D6O_DOMAIN_SEARCH
  240. ///
  241. /// \param u universe (V6 or V4).
  242. /// \param type option-type.
  243. /// \param buf option-buffer (ignored and should be empty).
  244. /// \return instance of ORO option.
  245. static dhcp::OptionPtr
  246. factoryOptionRequestOption6(dhcp::Option::Universe u,
  247. uint16_t type,
  248. const dhcp::OptionBuffer& buf);
  249. /// \brief Factory function to create DHCPv6 RAPID_COMMIT option instance.
  250. ///
  251. /// This factory function creates DHCPv6 RAPID_COMMIT option instance.
  252. /// The buffer passed to this option must be empty because option does
  253. /// not have any payload.
  254. ///
  255. /// \param u universe (V6 or V4).
  256. /// \param type option-type.
  257. /// \param buf option-buffer (ignored and should be empty).
  258. /// \return instance of RAPID_COMMIT option..
  259. static dhcp::OptionPtr factoryRapidCommit6(dhcp::Option::Universe u,
  260. uint16_t type,
  261. const dhcp::OptionBuffer& buf);
  262. /// \brief Factory function to create DHCPv4 Request List option.
  263. ///
  264. /// This factory function creayes DHCPv4 PARAMETER_REQUEST_LIST option
  265. /// instance with the following set of requested options:
  266. /// - DHO_SUBNET_MASK,
  267. /// - DHO_BROADCAST_ADDRESS,
  268. /// - DHO_TIME_OFFSET,
  269. /// - DHO_ROUTERS,
  270. /// - DHO_DOMAIN_NAME,
  271. /// - DHO_DOMAIN_NAME_SERVERS,
  272. /// - DHO_HOST_NAME.
  273. ///
  274. /// \param u universe (V6 or V4).
  275. /// \param type option-type.
  276. /// \param buf option-buffer (ignored and should be empty).
  277. /// \return instance o the generic option.
  278. static dhcp::OptionPtr factoryRequestList4(dhcp::Option::Universe u,
  279. uint16_t type,
  280. const dhcp::OptionBuffer& buf);
  281. /// \brief Generate DUID.
  282. ///
  283. /// Method generates unique DUID. The number of DUIDs it can generate
  284. /// depends on the number of simulated clinets, which is specified
  285. /// from the command line. It uses \ref CommandOptions object to retrieve
  286. /// number of clinets. Since the last six octets of DUID are constructed
  287. /// from the MAC address, this function uses \ref generateMacAddress
  288. /// internally to randomize the DUID.
  289. ///
  290. /// \param randomized number of bytes randomized.
  291. /// \throw isc::BadValue if \ref generateMacAddress throws.
  292. /// \return vector representing DUID.
  293. std::vector<uint8_t> generateDuid(uint8_t& randomized) const;
  294. /// \brief Generate MAC address.
  295. ///
  296. /// This method generates MAC address. The number of unique
  297. /// MAC addresses it can generate is determined by the number
  298. /// simulated DHCP clients specified from command line. It uses
  299. /// \ref CommandOptions object to retrieve number of clients.
  300. /// Based on this the random value is generated and added to
  301. /// the MAC address prefix (default MAC address).
  302. ///
  303. /// \param randomized number of bytes randomized.
  304. /// \throw isc::BadValue if MAC address prefix (default or specified
  305. /// from the command line) has invalid size (expected 6 octets).
  306. /// \return generated MAC address.
  307. std::vector<uint8_t> generateMacAddress(uint8_t& randomized) const;
  308. /// \brief generate transaction id.
  309. ///
  310. /// \return generated transaction id.
  311. uint32_t generateTransid() {
  312. return(transid_gen_->generate());
  313. }
  314. /// \brief Returns number of exchanges to be started.
  315. ///
  316. /// Method returns number of new exchanges to be started as soon
  317. /// as possible to satisfy expected rate. Calculation used here
  318. /// is based on current time, due time calculated with
  319. /// \ref updateSendTime function and expected rate.
  320. ///
  321. /// \return number of exchanges to be started immediatelly.
  322. uint64_t getNextExchangesNum() const;
  323. /// \brief Return template buffer.
  324. ///
  325. /// Method returns template buffer at specified index.
  326. ///
  327. /// \param idx index of template buffer.
  328. /// \return reference to template buffer or empty buffer if index
  329. /// is out of bounds.
  330. TemplateBuffer getTemplateBuffer(const size_t idx) const;
  331. /// \brief Reads packet templates from files.
  332. ///
  333. /// Method iterates through all specified template files, reads
  334. /// their content and stores it in class internal buffers
  335. ///
  336. /// \throw isc::BadValue if any of the template files does not exist
  337. void initPacketTemplates();
  338. /// \brief Initializes Statistics Manager.
  339. ///
  340. /// This function initializes Statistics Manager. If there is
  341. /// the one initialized already it is released.
  342. void initializeStatsMgr();
  343. /// \brief Open socket to communicate with DHCP server.
  344. ///
  345. /// Method opens socket and binds it to local address. Function will
  346. /// can use either interface name, local address or server address
  347. /// to create a socket, depending on what is available (specified
  348. /// from the command line). If socket can't be created for any
  349. /// reason, exception is thrown.
  350. /// If destination address is broadcast (for DHCPv4) or multicast
  351. /// (for DHCPv6) than broadcast or multicast option is set on
  352. /// the socket.
  353. ///
  354. /// \throw isc::BadValue if socket can't be created for given
  355. /// interface, local address or remote address.
  356. /// \throw isc::InvalidOperation if broadcast option can't be
  357. /// set for the v4 socket or if multicast option cat't be set
  358. /// for the v6 socket.
  359. /// \throw isc::Unexpected if interal unexpected error occured.
  360. /// \return socket descriptor.
  361. int openSocket() const;
  362. /// \brief Print intermediate statistics.
  363. ///
  364. /// Print brief statistics regarding number of sent packets,
  365. /// received packets and dropped packets so far.
  366. void printIntermediateStats();
  367. /// \brief Print rate statistics.
  368. ///
  369. /// Method print packet exchange rate statistics.
  370. void printRate() const;
  371. /// \brief Print performance statistics.
  372. ///
  373. /// Method prints performance statistics.
  374. /// \throws isc::InvalidOperation if Statistics Manager was
  375. /// not initialized.
  376. void printStats() const;
  377. /// \brief Receive DHCPv4 packet.
  378. ///
  379. /// Method performs reception of the DHCPv4 packet, updates
  380. /// statistics and responsds to the server if required, e.g.
  381. /// when OFFER packet arrives, this function will initiate
  382. /// REQUEST message to the server.
  383. ///
  384. /// \param socket socket to be used.
  385. /// \param pkt4 object representing DHCPv4 packet received.
  386. /// \throw isc::BadValue if unknown message type received.
  387. /// \throw isc::Unexpected if unexpected error occured.
  388. void receivePacket4(const TestControlSocket& socket,
  389. const dhcp::Pkt4Ptr& pkt4);
  390. /// \brief Receive DHCPv6 packet.
  391. ///
  392. /// Method performs reception of the DHCPv6 packet, updates
  393. /// statistics and responsds to the server if required, e.g.
  394. /// when ADVERTISE packet arrives, this function will initiate
  395. /// REQUEST message to the server.
  396. ///
  397. /// \param socket socket to be used.
  398. /// \param pkt6 object representing DHCPv6 packet received.
  399. /// \throw isc::BadValue if unknown message type received.
  400. /// \throw isc::Unexpected if unexpected error occured.
  401. void receivePacket6(const TestControlSocket& socket,
  402. const dhcp::Pkt6Ptr& pkt6);
  403. /// \brief Receive DHCPv4 or DHCPv6 packets from the server.
  404. ///
  405. /// Method receives DHCPv4 or DHCPv6 packets from the server.
  406. /// This function will call \ref receivePacket4 or
  407. /// \ref receivePacket6 depending if DHCPv4 or DHCPv6 packet
  408. /// has arrived.
  409. ///
  410. /// \param socket socket to be used.
  411. /// \throw::BadValue if unknown message type received.
  412. /// \throw::Unexpected if unexpected error occured.
  413. void receivePackets(const TestControlSocket& socket);
  414. /// \brief Register option factory functions for DHCPv4
  415. ///
  416. /// Method registers option factory functions for DHCPv4.
  417. /// These functions are called to create instances of DHCPv4
  418. /// options. Call \ref Option::factory to invoke factory
  419. /// function for particular option. Don't use this function directly.
  420. /// Use \ref registerOptionFactories instead.
  421. void registerOptionFactories4() const;
  422. /// \brief Register option factory functions for DHCPv6
  423. ///
  424. /// Method registers option factory functions for DHCPv6.
  425. /// These functions are called to create instances of DHCPv6
  426. /// options. Call \ref Option::factory to invoke factory
  427. /// function for particular option. Don't use this function directly.
  428. /// Use \ref registerOptionFactories instead.
  429. void registerOptionFactories6() const;
  430. /// \brief Register option factory functions for DHCPv4 or DHCPv6.
  431. ///
  432. /// Method registers option factory functions for DHCPv4 or DHCPv6,
  433. /// depending in whch mode test is currently running.
  434. void registerOptionFactories() const;
  435. /// \brief Resets internal state of the object.
  436. ///
  437. /// Method resets internal state of the object. It has to be
  438. /// called before new test is started.
  439. void reset();
  440. /// \brief Send DHCPv4 DISCOVER message.
  441. ///
  442. /// Method creates and sends DHCPv4 DISCOVER message to the server
  443. /// with the following options:
  444. /// - MESSAGE_TYPE set to DHCPDISCOVER
  445. /// - PARAMETER_REQUEST_LIST with the same list of requested options
  446. /// as described in \ref factoryRequestList.
  447. /// The transaction id and MAC address are randomly generated for
  448. /// the message. Range of unique MAC addresses generated depends
  449. /// on the number of clients specified from the command line.
  450. ///
  451. /// \param socket socket to be used to send the message.
  452. /// \param preload preload mode, packets not included in statistics.
  453. /// \throw isc::Unexpected if failed to create new packet instance.
  454. /// \throw isc::BadValue if MAC address has invalid length.
  455. void sendDiscover4(const TestControlSocket& socket,
  456. const bool preload = false);
  457. /// \brief Send DHCPv4 DISCOVER message from template.
  458. ///
  459. /// Method sends DHCPv4 DISCOVER message from template. The
  460. /// template data is exepcted to be in binary format. Provided
  461. /// buffer is copied and parts of it are replaced with actual
  462. /// data (e.g. MAC address, transaction id etc.).
  463. ///
  464. /// \param socket socket to be used to send the message.
  465. /// \param template_buf buffer holding template packet.
  466. /// \param preload preload mode, packets not included in statistics.
  467. /// \throw isc::OutOfRange if randomization offset is out of bounds.
  468. void sendDiscover4(const TestControlSocket& socket,
  469. const std::vector<uint8_t>& template_buf,
  470. const bool preload = false);
  471. /// \brief Send DHCPv4 REQUEST message.
  472. ///
  473. /// Method creates and sends DHCPv4 REQUEST message to the server.
  474. ///
  475. /// \param socket socket to be used to send message.
  476. /// \param discover_pkt4 DISCOVER packet sent.
  477. /// \param offer_pkt4 OFFER packet object.
  478. /// \throw isc::Unexpected if unexpected error occured.
  479. /// \throw isc::InvalidOperation if Statistics Manager has not been
  480. /// initialized.
  481. void sendRequest4(const TestControlSocket& socket,
  482. const dhcp::Pkt4Ptr& discover_pkt4,
  483. const dhcp::Pkt4Ptr& offer_pkt4);
  484. /// \brief Send DHCPv4 REQUEST message from template.
  485. ///
  486. /// Method sends DHCPv4 REQUEST message from template.
  487. ///
  488. /// \param socket socket to be used to send message.
  489. /// \param template_buf buffer holding template packet.
  490. /// \param discover_pkt4 DISCOVER packet sent.
  491. /// \param offer_pkt4 OFFER packet received.
  492. void sendRequest4(const TestControlSocket& socket,
  493. const std::vector<uint8_t>& template_buf,
  494. const dhcp::Pkt4Ptr& discover_pkt4,
  495. const dhcp::Pkt4Ptr& offer_pkt4);
  496. /// \brief Send DHCPv6 REQUEST message.
  497. ///
  498. /// Method creates and sends DHCPv6 REQUEST message to the server
  499. /// with the following options:
  500. /// - D6O_ELAPSED_TIME
  501. /// - D6O_CLIENTID
  502. /// - D6O_SERVERID
  503. /// The elapsed time is calculated based on the duration between
  504. /// sending a SOLICIT and receiving the ADVERTISE packet prior.
  505. /// For this reason both solicit and advertise packet objects have
  506. /// to be passed when calling this function.
  507. ///
  508. /// \param socket socket to be used to send message.
  509. /// \param solicit_pkt6 SOLICIT packet object.
  510. /// \param advertise_pkt6 ADVERTISE packet object.
  511. /// \throw isc::Unexpected if unexpected error occured.
  512. /// \throw isc::InvalidOperation if Statistics Manager has not been
  513. /// initialized.
  514. void sendRequest6(const TestControlSocket& socket,
  515. const dhcp::Pkt6Ptr& solicit_pkt6,
  516. const dhcp::Pkt6Ptr& advertise_pkt6);
  517. /// \brief Send DHCPv6 REQUEST message from template.
  518. ///
  519. /// Method sends DHCPv6 REQUEST message from template.
  520. ///
  521. /// \param socket socket to be used to send message.
  522. /// \param template_buf packet template buffer.
  523. /// \param solicit_pkt6 SOLICIT packet object.
  524. /// \param advertise_pkt6 ADVERTISE packet object.
  525. void sendRequest6(const TestControlSocket& socket,
  526. const std::vector<uint8_t>& template_buf,
  527. const dhcp::Pkt6Ptr& solicit_pkt6,
  528. const dhcp::Pkt6Ptr& advertise_pkt6);
  529. /// \brief Send DHCPv6 SOLICIT message.
  530. ///
  531. /// Method creates and sends DHCPv6 SOLICIT message to the server
  532. /// with the following options:
  533. /// - D6O_ELAPSED_TIME,
  534. /// - D6O_RAPID_COMMIT if rapid commit is requested in command line,
  535. /// - D6O_CLIENTID,
  536. /// - D6O_ORO (Option Request Option),
  537. /// - D6O_IA_NA.
  538. ///
  539. /// \param socket socket to be used to send the message.
  540. /// \param preload mode, packets not included in statistics.
  541. /// \throw isc::Unexpected if failed to create new packet instance.
  542. void sendSolicit6(const TestControlSocket& socket,
  543. const bool preload = false);
  544. /// \brief Send DHCPv6 SOLICIT message from template.
  545. ///
  546. /// Method sends DHCPv6 SOLICIT message from template.
  547. ///
  548. /// \param socket socket to be used to send the message.
  549. /// \param template_buf packet template buffer.
  550. /// \param preload mode, packets not included in statistics.
  551. void sendSolicit6(const TestControlSocket& socket,
  552. const std::vector<uint8_t>& template_buf,
  553. const bool preload = false);
  554. /// \brief Set default DHCPv4 packet parameters.
  555. ///
  556. /// This method sets default parameters on the DHCPv4 packet:
  557. /// - interface name,
  558. /// - local port = 68 (DHCP client port),
  559. /// - remote port = 67 (DHCP server port),
  560. /// - server's address,
  561. /// - GIADDR = local address where socket is bound to,
  562. /// - hops = 1 (pretending that we are a relay)
  563. ///
  564. /// \param socket socket used to send the packet.
  565. /// \param pkt reference to packet to be configured.
  566. void setDefaults4(const TestControlSocket& socket,
  567. const dhcp::Pkt4Ptr& pkt);
  568. /// \brief Set default DHCPv6 packet parameters.
  569. ///
  570. /// This method sets default parameters on the DHCPv6 packet:
  571. /// - interface name,
  572. /// - interface index,
  573. /// - local port,
  574. /// - remote port,
  575. /// - local address,
  576. /// - remote address (server).
  577. ///
  578. /// \param socket socket used to send the packet.
  579. /// \param pkt reference to packet to be configured.
  580. void setDefaults6(const TestControlSocket& socket,
  581. const dhcp::Pkt6Ptr& pkt);
  582. /// \brief Find of diagnostic flag has been set.
  583. ///
  584. /// \param diag diagnostic flag (a,e,i,s,r,t,T).
  585. /// \return true if diagnostics flag has been set.
  586. bool testDiags(const char diag) const;
  587. /// \brief Update due time to initiate next chunk of exchanges.
  588. ///
  589. /// Method updates due time to initiate next chunk of exchanges.
  590. /// Function takes current time, last sent packet's time and
  591. /// expected rate in its calculations.
  592. void updateSendDue();
  593. private:
  594. /// \brief Convert binary value to hex string.
  595. ///
  596. /// \param b byte to convert.
  597. /// \return hex string.
  598. std::string byte2Hex(const uint8_t b) const;
  599. /// \brief Calculate elapsed time between two packets.
  600. ///
  601. /// \param T Pkt4Ptr or Pkt6Ptr class.
  602. /// \param pkt1 first packet.
  603. /// \param pkt2 second packet.
  604. /// \return elapsed time in milliseconds between pkt1 and pkt2.
  605. template<class T>
  606. uint32_t getElapsedTime(const T& pkt1, const T& pkt2) {
  607. using namespace boost::posix_time;
  608. ptime pkt1_time = pkt1->getTimestamp();
  609. ptime pkt2_time = pkt2->getTimestamp();
  610. if (pkt1_time.is_not_a_date_time() ||
  611. pkt2_time.is_not_a_date_time()) {
  612. return (0);
  613. }
  614. time_period elapsed_period(pkt1_time, pkt2_time);
  615. if (elapsed_period.is_null()) {
  616. return (0);
  617. }
  618. return(elapsed_period.length().total_milliseconds());
  619. }
  620. /// \brief Get number of received packets.
  621. ///
  622. /// Get the number of received packets from the Statistics Manager.
  623. /// Function may throw if Statistics Manager object is not
  624. /// initialized.
  625. /// \param xchg_type packet exchange type.
  626. /// \return number of received packets.
  627. uint64_t getRcvdPacketsNum(const ExchangeType xchg_type) const;
  628. /// \brief Get number of sent packets.
  629. ///
  630. /// Get the number of sent packets from the Statistics Manager.
  631. /// Function may throw if Statistics Manager object is not
  632. /// initialized.
  633. /// \param xchg_type packet exchange type.
  634. /// \return number of sent packets.
  635. uint64_t getSentPacketsNum(const ExchangeType xchg_type) const;
  636. /// \brief Handle interrupt signal.
  637. ///
  638. /// Function sets flag indicating that program has been
  639. /// interupted.
  640. ///
  641. /// \param sig signal (ignored)
  642. static void handleInterrupt(int sig);
  643. /// \brief Print main diagnostics data.
  644. ///
  645. /// Method prints main diagnostics data.
  646. void printDiagnostics() const;
  647. /// \brief Read DHCP message template from file.
  648. ///
  649. /// Method reads DHCP message template from file and
  650. /// converts it to binary format. Read data is appended
  651. /// to template_buffers_ vector.
  652. void readPacketTemplate(const std::string& file_name);
  653. /// \brief Convert vector in hexadecimal string.
  654. ///
  655. /// \param vec vector to be converted.
  656. /// \param separator separator.
  657. std::string vector2Hex(const std::vector<uint8_t>& vec,
  658. const std::string& separator = "") const;
  659. boost::posix_time::ptime send_due_; ///< Due time to initiate next chunk
  660. ///< of exchanges.
  661. boost::posix_time::ptime last_sent_; ///< Indicates when the last exchange
  662. /// was initiated.
  663. boost::posix_time::ptime last_report_; ///< Last intermediate report time.
  664. StatsMgr4Ptr stats_mgr4_; ///< Statistics Manager 4.
  665. StatsMgr6Ptr stats_mgr6_; ///< Statistics Manager 6.
  666. TransidGeneratorPtr transid_gen_; ///< Transaction id generator.
  667. /// Buffer holiding server id received in first packet
  668. dhcp::OptionBuffer first_packet_serverid_;
  669. /// Packet template buffers.
  670. TemplateBufferCollection template_buffers_;
  671. static bool interrupted_; ///< Is program interrupted.
  672. };
  673. } // namespace perfdhcp
  674. } // namespace isc
  675. #endif // __COMMAND_OPTIONS_H