test_control.h 32 KB

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