test_control.h 30 KB

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