test_control.h 32 KB

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