test_control.cc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <unistd.h>
  18. #include <boost/date_time/posix_time/posix_time.hpp>
  19. #include <exceptions/exceptions.h>
  20. #include <asiolink/io_address.h>
  21. #include <dhcp/libdhcp++.h>
  22. #include <dhcp/iface_mgr.h>
  23. #include <dhcp/dhcp4.h>
  24. #include "test_control.h"
  25. #include "command_options.h"
  26. using namespace std;
  27. using namespace boost;
  28. using namespace boost::posix_time;
  29. using namespace isc;
  30. using namespace isc::dhcp;
  31. using namespace isc::asiolink;
  32. namespace isc {
  33. namespace perfdhcp {
  34. TestControl::TestControlSocket::TestControlSocket(int socket) :
  35. socket_(socket),
  36. addr_("127.0.0.1") {
  37. initSocketData();
  38. }
  39. TestControl::TestControlSocket::~TestControlSocket() {
  40. IfaceMgr::instance().closeSockets();
  41. }
  42. void
  43. TestControl::TestControlSocket::initSocketData() {
  44. const IfaceMgr::IfaceCollection& ifaces =
  45. IfaceMgr::instance().getIfaces();
  46. for (IfaceMgr::IfaceCollection::const_iterator it = ifaces.begin();
  47. it != ifaces.end();
  48. ++it) {
  49. const IfaceMgr::SocketCollection& socket_collection =
  50. it->getSockets();
  51. for (IfaceMgr::SocketCollection::const_iterator s =
  52. socket_collection.begin();
  53. s != socket_collection.end();
  54. ++s) {
  55. if (s->sockfd_ == socket_) {
  56. iface_ = it->getName();
  57. ifindex_ = it->getIndex();
  58. addr_ = s->addr_;
  59. return;
  60. }
  61. }
  62. }
  63. isc_throw(BadValue, "interface for for specified socket "
  64. "descriptor not found");
  65. }
  66. TestControl&
  67. TestControl::instance() {
  68. static TestControl test_control;
  69. return (test_control);
  70. }
  71. TestControl::TestControl() {
  72. reset();
  73. }
  74. bool
  75. TestControl::checkExitConditions() const {
  76. CommandOptions& options = CommandOptions::instance();
  77. // Check if test period passed..
  78. if (options.getPeriod() != 0) {
  79. if (options.getIpVersion() == 4) {
  80. time_period period(stats_mgr4_->getTestPeriod());
  81. if (period.length().total_seconds() >= options.getPeriod()) {
  82. return true;
  83. }
  84. } else if (options.getIpVersion() == 6) {
  85. time_period period = stats_mgr6_->getTestPeriod();
  86. if (period.length().total_seconds() >= options.getPeriod()) {
  87. return true;
  88. }
  89. }
  90. }
  91. // Check if we reached maximum number of DISCOVER/SOLICIT sent.
  92. if (options.getNumRequests().size() > 0) {
  93. if (options.getIpVersion() == 4) {
  94. if (getSentPacketsNum(StatsMgr4::XCHG_DO) >=
  95. options.getNumRequests()[0]) {
  96. return(true);
  97. }
  98. } else if (options.getIpVersion() == 6) {
  99. if (stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_SA) >=
  100. options.getNumRequests()[0]) {
  101. return(true);
  102. }
  103. }
  104. }
  105. // Check if we reached maximum number REQUEST packets.
  106. if (options.getNumRequests().size() == 2) {
  107. if (options.getIpVersion() == 4) {
  108. if (stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_RA) >=
  109. options.getNumRequests()[1]) {
  110. return(true);
  111. }
  112. } else if (options.getIpVersion() == 6) {
  113. if (stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_RR) >=
  114. options.getNumRequests()[1]) {
  115. return(true);
  116. }
  117. }
  118. }
  119. // Check if we reached maximum number of drops of OFFER/ADVERTISE packets.
  120. if (options.getMaxDrop().size() > 0) {
  121. if (options.getIpVersion() == 4) {
  122. if (stats_mgr4_->getDroppedPacketsNum(StatsMgr4::XCHG_DO) >=
  123. options.getMaxDrop()[0]) {
  124. return(true);
  125. }
  126. } else if (options.getIpVersion() == 6) {
  127. if (stats_mgr6_->getDroppedPacketsNum(StatsMgr6::XCHG_SA) >=
  128. options.getMaxDrop()[0]) {
  129. return(true);
  130. }
  131. }
  132. }
  133. // Check if we reached maximum number of drops of ACK/REPLY packets.
  134. if (options.getMaxDrop().size() == 2) {
  135. if (options.getIpVersion() == 4) {
  136. if (stats_mgr4_->getDroppedPacketsNum(StatsMgr4::XCHG_RA) >=
  137. options.getMaxDrop()[1]) {
  138. return(true);
  139. }
  140. } else if (options.getIpVersion() == 6) {
  141. if (stats_mgr6_->getDroppedPacketsNum(StatsMgr6::XCHG_RR) >=
  142. options.getMaxDrop()[1]) {
  143. return(true);
  144. }
  145. }
  146. }
  147. // Check if we reached maximum drops percentage of OFFER/ADVERTISE packets.
  148. if (options.getMaxDropPercentage().size() > 0) {
  149. if (options.getIpVersion() == 4) {
  150. if ((stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_DO) > 10) &&
  151. ((100. * stats_mgr4_->getDroppedPacketsNum(StatsMgr4::XCHG_DO) /
  152. stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_DO)) >=
  153. options.getMaxDropPercentage()[0])) {
  154. return(true);
  155. }
  156. } else if (options.getIpVersion() == 6) {
  157. if ((stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_SA) > 10) &&
  158. ((100. * stats_mgr6_->getDroppedPacketsNum(StatsMgr6::XCHG_SA) /
  159. stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_SA)) >=
  160. options.getMaxDropPercentage()[0])) {
  161. return(true);
  162. }
  163. }
  164. }
  165. // Check if we reached maximum drops percentage of ACK/REPLY packets.
  166. if (options.getMaxDropPercentage().size() == 2) {
  167. if (options.getIpVersion() == 4) {
  168. if ((stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_RA) > 10) &&
  169. ((100. * stats_mgr4_->getDroppedPacketsNum(StatsMgr4::XCHG_RA) /
  170. stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_RA)) >=
  171. options.getMaxDropPercentage()[1])) {
  172. return(true);
  173. }
  174. } else if (options.getIpVersion() == 6) {
  175. if ((stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_RR) > 10) &&
  176. ((100. * stats_mgr6_->getDroppedPacketsNum(StatsMgr6::XCHG_RR) /
  177. stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_RR)) >=
  178. options.getMaxDropPercentage()[1])) {
  179. return(true);
  180. }
  181. }
  182. }
  183. return(false);
  184. }
  185. OptionPtr
  186. TestControl::factoryElapsedTime6(Option::Universe, uint16_t,
  187. const OptionBuffer& buf) {
  188. if (buf.size() == 2) {
  189. return OptionPtr(new Option(Option::V6, D6O_ELAPSED_TIME, buf));
  190. } else if (buf.size() == 0) {
  191. return OptionPtr(new Option(Option::V6, D6O_ELAPSED_TIME,
  192. OptionBuffer(2, 0)));
  193. }
  194. isc_throw(isc::BadValue,
  195. "elapsed time option buffer size has to be 0 or 2");
  196. }
  197. OptionPtr
  198. TestControl::factoryGeneric(Option::Universe u, uint16_t type,
  199. const OptionBuffer& buf) {
  200. OptionPtr opt(new Option(u, type, buf));
  201. return opt;
  202. }
  203. OptionPtr
  204. TestControl::factoryIana6(Option::Universe, uint16_t,
  205. const OptionBuffer& buf) {
  206. const uint8_t buf_array[] = {
  207. 0, 0, 0, 1, // IAID = 1
  208. 0, 0, 3600 >> 8, 3600 && 0xff, // T1 = 3600
  209. 0, 0, 5400 >> 8, 5400 & 0xff, // T2 = 5400
  210. };
  211. OptionBuffer buf_ia_na(buf_array, buf_array + sizeof(buf_array));
  212. for (int i = 0; i < buf.size(); ++i) {
  213. buf_ia_na.push_back(buf[i]);
  214. }
  215. return OptionPtr(new Option(Option::V6, D6O_IA_NA, buf_ia_na));
  216. }
  217. OptionPtr
  218. TestControl::factoryRapidCommit6(Option::Universe, uint16_t,
  219. const OptionBuffer&) {
  220. return OptionPtr(new Option(Option::V6, D6O_RAPID_COMMIT, OptionBuffer()));
  221. }
  222. OptionPtr
  223. TestControl::factoryOptionRequestOption6(Option::Universe,
  224. uint16_t,
  225. const OptionBuffer&) {
  226. const uint8_t buf_array[] = {
  227. D6O_NAME_SERVERS,
  228. D6O_DOMAIN_SEARCH
  229. };
  230. OptionBuffer buf_with_options(buf_array, buf_array + sizeof(buf_array));
  231. return OptionPtr(new Option(Option::V6, D6O_ORO, buf_with_options));
  232. }
  233. OptionPtr
  234. TestControl::factoryRequestList4(Option::Universe u,
  235. uint16_t type,
  236. const OptionBuffer& buf) {
  237. const uint8_t buf_array[] = {
  238. DHO_SUBNET_MASK,
  239. DHO_BROADCAST_ADDRESS,
  240. DHO_TIME_OFFSET,
  241. DHO_ROUTERS,
  242. DHO_DOMAIN_NAME,
  243. DHO_DOMAIN_NAME_SERVERS,
  244. DHO_HOST_NAME
  245. };
  246. OptionBuffer buf_with_options(buf_array, buf_array + sizeof(buf_array));
  247. OptionPtr opt(new Option(u, type, buf));
  248. opt->setData(buf_with_options.begin(), buf_with_options.end());
  249. return opt;
  250. }
  251. std::vector<uint8_t>
  252. TestControl::generateMacAddress() const {
  253. CommandOptions& options = CommandOptions::instance();
  254. uint32_t clients_num = options.getClientsNum();
  255. if ((clients_num == 0) || (clients_num == 1)) {
  256. return options.getMacPrefix();
  257. }
  258. // Get the base MAC address. We are going to randomize part of it.
  259. std::vector<uint8_t> mac_addr(options.getMacPrefix());
  260. if (mac_addr.size() != HW_ETHER_LEN) {
  261. isc_throw(BadValue, "invalid MAC address prefix specified");
  262. }
  263. uint32_t r = random();
  264. // The random number must be in the range 0..clients_num. This
  265. // will guarantee that every client has exactly one random MAC
  266. // address assigned.
  267. r %= clients_num;
  268. // Randomize MAC address octets.
  269. for (std::vector<uint8_t>::iterator it = mac_addr.end() - 1;
  270. it >= mac_addr.begin();
  271. --it) {
  272. // Add the random value to the current octet.
  273. (*it) += r;
  274. if (r < 256) {
  275. // If we are here it means that there is no sense
  276. // to randomize the remaining octets of MAC address
  277. // because the following bytes of random value
  278. // are zero and it will have no effect.
  279. break;
  280. }
  281. // Randomize the next octet with the following
  282. // byte of random value.
  283. r >>= 8;
  284. }
  285. return mac_addr;
  286. }
  287. std::vector<uint8_t>
  288. TestControl::generateDuid() const {
  289. CommandOptions& options = CommandOptions::instance();
  290. uint32_t clients_num = options.getClientsNum();
  291. if ((clients_num == 0) || (clients_num == 1)) {
  292. return options.getDuidPrefix();
  293. }
  294. // Get the base DUID. We are going to randomize part of it.
  295. std::vector<uint8_t> duid(options.getDuidPrefix());
  296. std::vector<uint8_t> mac_addr(generateMacAddress());
  297. duid.resize(duid.size() - mac_addr.size());
  298. for (int i = 0; i < mac_addr.size(); ++i) {
  299. duid.push_back(mac_addr[i]);
  300. }
  301. return duid;
  302. }
  303. uint64_t
  304. TestControl::getNextExchangesNum() const {
  305. CommandOptions& options = CommandOptions::instance();
  306. // Reset number of exchanges.
  307. uint64_t due_exchanges = 0;
  308. // Get current time.
  309. ptime now(microsec_clock::universal_time());
  310. // The due time indicates when we should start sending next chunk
  311. // of packets. If it is already due time, we should calculate
  312. // how many packets to send.
  313. if (now >= send_due_) {
  314. // If rate is specified from the command line we have to
  315. // synchornize with it.
  316. if (options.getRate() != 0) {
  317. time_period period(send_due_, now);
  318. time_duration duration = period.length();
  319. // due_factor indicates the number of seconds that
  320. // sending next chunk of packets will take.
  321. double due_factor = duration.fractional_seconds() /
  322. time_duration::ticks_per_second();
  323. due_factor += duration.total_seconds();
  324. // Multiplying due_factor by expected rate gives the number
  325. // of exchanges to be initiated.
  326. due_exchanges = static_cast<uint64_t>(due_factor * options.getRate());
  327. // We want to make sure that at least one packet goes out.
  328. due_exchanges += 1;
  329. // We should not exceed aggressivity as it could have been
  330. // restricted from command line.
  331. if (due_exchanges > options.getAggressivity()) {
  332. due_exchanges = options.getAggressivity();
  333. }
  334. } else {
  335. // Rate is not specified so we rely on aggressivity
  336. // which is the number of packets to be sent in
  337. // one chunk.
  338. due_exchanges = options.getAggressivity();
  339. }
  340. return (due_exchanges);
  341. }
  342. return (0);
  343. }
  344. uint64_t
  345. TestControl::getRcvdPacketsNum(const ExchangeType xchg_type) const {
  346. uint8_t ip_version = CommandOptions::instance().getIpVersion();
  347. if (ip_version == 4) {
  348. return(stats_mgr4_->getRcvdPacketsNum(xchg_type));
  349. }
  350. return(stats_mgr6_->
  351. getRcvdPacketsNum(static_cast<StatsMgr6::ExchangeType>(xchg_type)));
  352. }
  353. uint64_t
  354. TestControl::getSentPacketsNum(const ExchangeType xchg_type) const {
  355. uint8_t ip_version = CommandOptions::instance().getIpVersion();
  356. if (ip_version == 4) {
  357. return(stats_mgr4_->getSentPacketsNum(xchg_type));
  358. }
  359. return(stats_mgr6_->
  360. getSentPacketsNum(static_cast<StatsMgr6::ExchangeType>(xchg_type)));
  361. }
  362. void
  363. TestControl::initPacketTemplates() {
  364. CommandOptions& options = CommandOptions::instance();
  365. std::vector<std::string> template_files = options.getTemplateFiles();
  366. for (std::vector<std::string>::const_iterator it = template_files.begin();
  367. it != template_files.end();
  368. ++it) {
  369. std::cout << "Open file " << *it << std::endl;
  370. }
  371. }
  372. void
  373. TestControl::initializeStatsMgr() {
  374. CommandOptions& options = CommandOptions::instance();
  375. if (options.getIpVersion() == 4) {
  376. stats_mgr4_.reset();
  377. stats_mgr4_ = StatsMgr4Ptr(new StatsMgr4());
  378. stats_mgr4_->addExchangeStats(StatsMgr4::XCHG_DO);
  379. if (options.getExchangeMode() == CommandOptions::DORA_SARR) {
  380. stats_mgr4_->addExchangeStats(StatsMgr4::XCHG_RA);
  381. }
  382. } else if (options.getIpVersion() == 6) {
  383. stats_mgr6_.reset();
  384. stats_mgr6_ = StatsMgr6Ptr(new StatsMgr6());
  385. stats_mgr6_->addExchangeStats(StatsMgr6::XCHG_SA);
  386. if (options.getExchangeMode() == CommandOptions::DORA_SARR) {
  387. stats_mgr6_->addExchangeStats(StatsMgr6::XCHG_RR);
  388. }
  389. }
  390. }
  391. int
  392. TestControl::openSocket() const {
  393. CommandOptions& options = CommandOptions::instance();
  394. std::string localname = options.getLocalName();
  395. std::string servername = options.getServerName();
  396. uint16_t port = options.getLocalPort();
  397. uint8_t family = AF_INET;
  398. int sock = 0;
  399. IOAddress remoteaddr(servername);
  400. if (port == 0) {
  401. if (options.getIpVersion() == 6) {
  402. port = DHCP6_CLIENT_PORT;
  403. } else if (options.getIpVersion() == 4) {
  404. port = 67; // TODO: find out why port 68 is wrong here.
  405. }
  406. }
  407. if (options.getIpVersion() == 6) {
  408. family = AF_INET6;
  409. }
  410. // Local name is specified along with '-l' option.
  411. // It may point to interface name or local address.
  412. if (!localname.empty()) {
  413. // CommandOptions should be already aware wether local name
  414. // is interface name or address because it uses IfaceMgr to
  415. // scan interfaces and get's their names.
  416. if (options.isInterface()) {
  417. sock = IfaceMgr::instance().openSocketFromIface(localname,
  418. port,
  419. family);
  420. } else {
  421. IOAddress localaddr(localname);
  422. sock = IfaceMgr::instance().openSocketFromAddress(localaddr,
  423. port);
  424. }
  425. } else if (!servername.empty()) {
  426. // If only server name is given we will need to try to resolve
  427. // the local address to bind socket to based on remote address.
  428. sock = IfaceMgr::instance().openSocketFromRemoteAddress(remoteaddr,
  429. port);
  430. }
  431. if (sock <= 0) {
  432. isc_throw(BadValue, "unable to open socket to communicate with "
  433. "DHCP server");
  434. }
  435. // IfaceMgr does not set broadcast option on the socket. We rely
  436. // on CommandOptions object to find out if socket has to have
  437. // broadcast enabled.
  438. if ((options.getIpVersion() == 4) && options.isBroadcast()) {
  439. int broadcast_enable = 1;
  440. int ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
  441. &broadcast_enable, sizeof(broadcast_enable));
  442. if (ret < 0) {
  443. isc_throw(InvalidOperation,
  444. "unable to set broadcast option on the socket");
  445. }
  446. } else if (options.getIpVersion() == 6) {
  447. // If remote address is multicast we need to enable it on
  448. // the socket that has been created.
  449. asio::ip::address_v6 remote_v6 = remoteaddr.getAddress().to_v6();
  450. if (remote_v6.is_multicast()) {
  451. int hops = 1;
  452. int ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  453. &hops, sizeof(hops));
  454. // If user specified interface name with '-l' the
  455. // IPV6_MULTICAST_IF has to be set.
  456. if ((ret >= 0) && options.isInterface()) {
  457. IfaceMgr::Iface* iface =
  458. IfaceMgr::instance().getIface(options.getLocalName());
  459. if (iface == NULL) {
  460. isc_throw(Unexpected, "unknown interface "
  461. << options.getLocalName());
  462. }
  463. int idx = iface->getIndex();
  464. ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  465. &idx, sizeof(idx));
  466. }
  467. if (ret < 0) {
  468. isc_throw(InvalidOperation,
  469. "unable to enable multicast on socket " << sock
  470. << ". errno = " << errno);
  471. }
  472. }
  473. }
  474. return(sock);
  475. }
  476. void
  477. TestControl::printRate() const {
  478. double rate = 0;
  479. CommandOptions& options = CommandOptions::instance();
  480. if (options.getIpVersion() == 4) {
  481. double duration =
  482. stats_mgr4_->getTestPeriod().length().total_nanoseconds() / 1e9;
  483. rate = stats_mgr4_->getRcvdPacketsNum(StatsMgr4::XCHG_DO) / duration;
  484. } else if (options.getIpVersion() == 6) {
  485. double duration =
  486. stats_mgr6_->getTestPeriod().length().total_nanoseconds() / 1e9;
  487. rate = stats_mgr6_->getRcvdPacketsNum(StatsMgr6::XCHG_SA) / duration;
  488. }
  489. std::cout << "***Rate statistics***" << std::endl;
  490. if (options.getRate() > 0) {
  491. std::cout << "Rate: " << rate << ", expected rate: "
  492. << options.getRate() << std::endl << std::endl;
  493. } else {
  494. std::cout << "Rate: " << rate << std::endl << std::endl;
  495. }
  496. }
  497. void
  498. TestControl::printStats() const {
  499. printRate();
  500. CommandOptions& options = CommandOptions::instance();
  501. if (options.getIpVersion() == 4) {
  502. if (!stats_mgr4_) {
  503. isc_throw(InvalidOperation, "Statistics Manager for DHCPv4 "
  504. "hasn't been initialized");
  505. }
  506. stats_mgr4_->printStats();
  507. } else if (options.getIpVersion() == 6) {
  508. if (!stats_mgr6_) {
  509. isc_throw(InvalidOperation, "Statistics Manager for DHCPv6 "
  510. "hasn't been initialized");
  511. }
  512. stats_mgr6_->printStats();
  513. }
  514. }
  515. void
  516. TestControl::receivePacket4(const TestControlSocket& socket,
  517. const Pkt4Ptr& pkt4) {
  518. if (pkt4->getType() == DHCPOFFER) {
  519. Pkt4Ptr discover_pkt4(stats_mgr4_->passRcvdPacket(StatsMgr4::XCHG_DO,
  520. pkt4));
  521. CommandOptions::ExchangeMode xchg_mode =
  522. CommandOptions::instance().getExchangeMode();
  523. if ((xchg_mode == CommandOptions::DORA_SARR) && discover_pkt4) {
  524. sendRequest4(socket, pkt4);
  525. }
  526. } else if (pkt4->getType() == DHCPACK) {
  527. stats_mgr4_->passRcvdPacket(StatsMgr4::XCHG_RA, pkt4);
  528. }
  529. }
  530. void
  531. TestControl::receivePacket6(const TestControlSocket& socket,
  532. const Pkt6Ptr& pkt6) {
  533. uint8_t packet_type = pkt6->getType();
  534. if (packet_type == DHCPV6_ADVERTISE) {
  535. Pkt6Ptr solicit_pkt6(stats_mgr6_->passRcvdPacket(StatsMgr6::XCHG_SA,
  536. pkt6));
  537. CommandOptions::ExchangeMode xchg_mode =
  538. CommandOptions::instance().getExchangeMode();
  539. if ((xchg_mode == CommandOptions::DORA_SARR) && solicit_pkt6) {
  540. sendRequest6(socket, solicit_pkt6, pkt6);
  541. }
  542. } else if (packet_type == DHCPV6_REPLY) {
  543. stats_mgr6_->passRcvdPacket(StatsMgr6::XCHG_RR, pkt6);
  544. }
  545. }
  546. void
  547. TestControl::receivePackets(const TestControlSocket& socket) {
  548. int timeout = 0;
  549. bool receiving = true;
  550. while (receiving) {
  551. if (CommandOptions::instance().getIpVersion() == 4) {
  552. Pkt4Ptr pkt4 = IfaceMgr::instance().receive4(timeout);
  553. if (!pkt4) {
  554. receiving = false;
  555. } else {
  556. pkt4->unpack();
  557. receivePacket4(socket, pkt4);
  558. }
  559. } else if (CommandOptions::instance().getIpVersion() == 6) {
  560. Pkt6Ptr pkt6 = IfaceMgr::instance().receive6(timeout);
  561. if (!pkt6) {
  562. receiving = false;
  563. } else {
  564. if (pkt6->unpack()) {
  565. receivePacket6(socket, pkt6);
  566. }
  567. }
  568. }
  569. }
  570. }
  571. void
  572. TestControl::registerOptionFactories4() const {
  573. static bool factories_registered = false;
  574. if (!factories_registered) {
  575. // DHCP_MESSAGE_TYPE option factory.
  576. LibDHCP::OptionFactoryRegister(Option::V4,
  577. DHO_DHCP_MESSAGE_TYPE,
  578. &TestControl::factoryGeneric);
  579. LibDHCP::OptionFactoryRegister(Option::V4,
  580. DHO_DHCP_SERVER_IDENTIFIER,
  581. &TestControl::factoryGeneric);
  582. // DHCP_PARAMETER_REQUEST_LIST option factory.
  583. LibDHCP::OptionFactoryRegister(Option::V4,
  584. DHO_DHCP_PARAMETER_REQUEST_LIST,
  585. &TestControl::factoryRequestList4);
  586. }
  587. factories_registered = true;
  588. }
  589. void
  590. TestControl::registerOptionFactories6() const {
  591. static bool factories_registered = false;
  592. if (!factories_registered) {
  593. LibDHCP::OptionFactoryRegister(Option::V6,
  594. D6O_ELAPSED_TIME,
  595. &TestControl::factoryElapsedTime6);
  596. LibDHCP::OptionFactoryRegister(Option::V6,
  597. D6O_RAPID_COMMIT,
  598. &TestControl::factoryRapidCommit6);
  599. LibDHCP::OptionFactoryRegister(Option::V6,
  600. D6O_ORO,
  601. &TestControl::factoryOptionRequestOption6);
  602. LibDHCP::OptionFactoryRegister(Option::V6,
  603. D6O_CLIENTID,
  604. &TestControl::factoryGeneric);
  605. LibDHCP::OptionFactoryRegister(Option::V6,
  606. D6O_SERVERID,
  607. &TestControl::factoryGeneric);
  608. LibDHCP::OptionFactoryRegister(Option::V6,
  609. D6O_IA_NA,
  610. &TestControl::factoryIana6);
  611. }
  612. factories_registered = true;
  613. }
  614. void
  615. TestControl::registerOptionFactories() const {
  616. CommandOptions& options = CommandOptions::instance();
  617. switch(options.getIpVersion()) {
  618. case 4:
  619. registerOptionFactories4();
  620. break;
  621. case 6:
  622. registerOptionFactories6();
  623. break;
  624. default:
  625. isc_throw(InvalidOperation, "command line options have to be parsed "
  626. "before DHCP option factories can be registered");
  627. }
  628. }
  629. void
  630. TestControl::reset() {
  631. send_due_ = microsec_clock::universal_time();
  632. last_sent_ = send_due_;
  633. transid_gen_.reset();
  634. transid_gen_ = TransidGeneratorPtr(new TransidGenerator());
  635. first_packet_serverid_.clear();
  636. }
  637. void
  638. TestControl::run() {
  639. sent_packets_0_ = 0;
  640. sent_packets_1_ = 0;
  641. // Reset singleton state before test starts.
  642. reset();
  643. CommandOptions& options = CommandOptions::instance();
  644. // Ip version is not set ONLY in case the command options
  645. // were not parsed. This surely means that parse() function
  646. // was not called prior to starting the test. This is fatal
  647. // error.
  648. if (options.getIpVersion() == 0) {
  649. isc_throw(InvalidOperation,
  650. "command options must be parsed before running a test");
  651. }
  652. registerOptionFactories();
  653. TestControlSocket socket(openSocket());
  654. // Initialize packet templates.
  655. initPacketTemplates();
  656. // Initialize randomization seed.
  657. if (options.isSeeded()) {
  658. srandom(options.getSeed());
  659. }
  660. // Preload server with number of packets.
  661. const bool do_preload = true;
  662. for (int i = 0; i < options.getPreload(); ++i) {
  663. if (options.getIpVersion() == 4) {
  664. sendDiscover4(socket, do_preload);
  665. } else if (options.getIpVersion() == 6) {
  666. sendSolicit6(socket, do_preload);
  667. }
  668. }
  669. initializeStatsMgr();
  670. uint64_t packets_sent = 0;
  671. for (;;) {
  672. updateSendDue();
  673. if (checkExitConditions()) {
  674. break;
  675. }
  676. uint64_t packets_due = getNextExchangesNum();
  677. receivePackets(socket);
  678. for (uint64_t i = packets_due; i > 0; --i) {
  679. if (options.getIpVersion() == 4) {
  680. sendDiscover4(socket);
  681. } else {
  682. sendSolicit6(socket);
  683. }
  684. ++packets_sent;
  685. }
  686. }
  687. printStats();
  688. }
  689. void
  690. TestControl::sendDiscover4(const TestControlSocket& socket,
  691. const bool preload /*= false*/) {
  692. ++sent_packets_0_;
  693. last_sent_ = microsec_clock::universal_time();
  694. // Generate the MAC address to be passed in the packet.
  695. std::vector<uint8_t> mac_address = generateMacAddress();
  696. // Generate trasnaction id to be set for the new exchange.
  697. const uint32_t transid = generateTransid();
  698. Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, transid));
  699. if (!pkt4) {
  700. isc_throw(Unexpected, "failed to create DISCOVER packet");
  701. }
  702. // Set options: DHCP_MESSAGE_TYPE and DHCP_PARAMETER_REQUEST_LIST
  703. OptionBuffer buf_msg_type;
  704. buf_msg_type.push_back(DHCPDISCOVER);
  705. pkt4->addOption(Option::factory(Option::V4, DHO_DHCP_MESSAGE_TYPE,
  706. buf_msg_type));
  707. pkt4->addOption(Option::factory(Option::V4,
  708. DHO_DHCP_PARAMETER_REQUEST_LIST));
  709. // Set client's and server's ports as well as server's address,
  710. // and local (relay) address.
  711. setDefaults4(socket, pkt4);
  712. pkt4->pack();
  713. IfaceMgr::instance().send(pkt4);
  714. if (!preload) {
  715. if (!stats_mgr4_) {
  716. isc_throw(InvalidOperation, "Statistics Manager for DHCPv4 "
  717. "hasn't been initialized");
  718. }
  719. stats_mgr4_->passSentPacket(StatsMgr4::XCHG_DO, pkt4);
  720. }
  721. }
  722. void
  723. TestControl::sendRequest4(const TestControlSocket& socket,
  724. const dhcp::Pkt4Ptr& offer_pkt4) {
  725. const uint32_t transid = generateTransid();
  726. Pkt4Ptr pkt4(new Pkt4(DHCPREQUEST, transid));
  727. OptionBuffer buf_msg_type;
  728. buf_msg_type.push_back(DHCPREQUEST);
  729. OptionPtr opt_msg_type = Option::factory(Option::V4, DHO_DHCP_MESSAGE_TYPE,
  730. buf_msg_type);
  731. pkt4->addOption(opt_msg_type);
  732. if (first_packet_serverid_.size() > 0) {
  733. pkt4->addOption(Option::factory(Option::V4, DHO_DHCP_SERVER_IDENTIFIER,
  734. first_packet_serverid_));
  735. } else {
  736. OptionPtr opt_serverid =
  737. offer_pkt4->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  738. if (!opt_serverid) {
  739. isc_throw(BadValue, "there is no SERVER_IDENTIFIER option "
  740. << "in OFFER message");
  741. }
  742. if (CommandOptions::instance().isUseFirst() &&
  743. stats_mgr4_->getRcvdPacketsNum(StatsMgr4::XCHG_DO) == 1) {
  744. first_packet_serverid_ = opt_serverid->getData();
  745. }
  746. pkt4->addOption(opt_serverid);
  747. }
  748. /// Set client address.
  749. asiolink::IOAddress yiaddr = offer_pkt4->getYiaddr();
  750. if (!yiaddr.getAddress().is_v4()) {
  751. isc_throw(BadValue, "the YIADDR returned in OFFER packet is not "
  752. " IPv4 address");
  753. }
  754. OptionPtr opt_requested_address =
  755. OptionPtr(new Option(Option::V4, DHO_DHCP_REQUESTED_ADDRESS,
  756. OptionBuffer()));
  757. opt_requested_address->setUint32(yiaddr);
  758. pkt4->addOption(opt_requested_address);
  759. OptionPtr opt_parameter_list =
  760. Option::factory(Option::V4, DHO_DHCP_PARAMETER_REQUEST_LIST);
  761. pkt4->addOption(opt_parameter_list);
  762. // Set client's and server's ports as well as server's address,
  763. // and local (relay) address.
  764. setDefaults4(socket, pkt4);
  765. pkt4->pack();
  766. IfaceMgr::instance().send(pkt4);
  767. if (!stats_mgr4_) {
  768. isc_throw(InvalidOperation, "Statistics Manager for DHCPv4 "
  769. "hasn't been initialized");
  770. }
  771. stats_mgr4_->passSentPacket(StatsMgr4::XCHG_RA, pkt4);
  772. }
  773. void
  774. TestControl::sendRequest6(const TestControlSocket& socket,
  775. const Pkt6Ptr& solicit_pkt6,
  776. const Pkt6Ptr& advertise_pkt6) {
  777. const uint32_t transid = generateTransid();
  778. Pkt6Ptr pkt6(new Pkt6(DHCPV6_REQUEST, transid));
  779. // Calculate elapsed time
  780. ptime solicit_time = solicit_pkt6->getTimestamp();
  781. ptime advertise_time = advertise_pkt6->getTimestamp();
  782. if (solicit_time.is_not_a_date_time()) {
  783. isc_throw(Unexpected, "timestamp was not set for SOLICIT packet");
  784. }
  785. if (advertise_time.is_not_a_date_time()) {
  786. isc_throw(Unexpected, "timestamp was not set for ADVERTISE packet");
  787. }
  788. time_period period(solicit_time, advertise_time);
  789. if (period.is_null()) {
  790. pkt6->addOption(Option::factory(Option::V6, D6O_ELAPSED_TIME));
  791. } else {
  792. OptionBuffer buf();
  793. const uint32_t elapsed_time = period.length().total_seconds();
  794. OptionPtr opt_elapsed_time =
  795. Option::factory(Option::V6, D6O_ELAPSED_TIME);
  796. opt_elapsed_time->setUint16(static_cast<uint16_t>(elapsed_time));
  797. pkt6->addOption(opt_elapsed_time);
  798. }
  799. OptionPtr opt_clientid = advertise_pkt6->getOption(D6O_CLIENTID);
  800. if (!opt_clientid) {
  801. isc_throw(Unexpected, "client id not found in received packet");
  802. }
  803. pkt6->addOption(opt_clientid);
  804. if (first_packet_serverid_.size() > 0) {
  805. pkt6->addOption(Option::factory(Option::V6, D6O_SERVERID,
  806. first_packet_serverid_));
  807. } else {
  808. OptionPtr opt_serverid = advertise_pkt6->getOption(D6O_SERVERID);
  809. if (!opt_serverid) {
  810. isc_throw(Unexpected, "server id not found in received packet");
  811. }
  812. if (CommandOptions::instance().isUseFirst() &&
  813. stats_mgr6_->getRcvdPacketsNum(StatsMgr6::XCHG_SA) == 1) {
  814. first_packet_serverid_ = opt_serverid->getData();
  815. }
  816. pkt6->addOption(opt_serverid);
  817. }
  818. OptionPtr opt_ia_na = advertise_pkt6->getOption(D6O_IA_NA);
  819. if (!opt_ia_na) {
  820. isc_throw(Unexpected, "DHCPv6 IA_NA option not found in received "
  821. "packet");
  822. }
  823. pkt6->addOption(opt_ia_na);
  824. setDefaults6(socket, pkt6);
  825. pkt6->pack();
  826. IfaceMgr::instance().send(pkt6);
  827. if (!stats_mgr6_) {
  828. isc_throw(InvalidOperation, "Statistics Manager for DHCPv6 "
  829. "hasn't been initialized");
  830. }
  831. stats_mgr6_->passSentPacket(StatsMgr6::XCHG_RR, pkt6);
  832. }
  833. void
  834. TestControl::sendSolicit6(const TestControlSocket& socket,
  835. const bool preload /*= false*/) {
  836. ++sent_packets_0_;
  837. last_sent_ = microsec_clock::universal_time();
  838. // Generate the MAC address to be passed in the packet.
  839. std::vector<uint8_t> mac_address = generateMacAddress();
  840. // Generate DUID to be passed to the packet
  841. std::vector<uint8_t> duid = generateDuid();
  842. // Generate trasnaction id to be set for the new exchange.
  843. const uint32_t transid = generateTransid();
  844. Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, transid));
  845. if (!pkt6) {
  846. isc_throw(Unexpected, "failed to create SOLICIT packet");
  847. }
  848. pkt6->addOption(Option::factory(Option::V6, D6O_ELAPSED_TIME));
  849. if (CommandOptions::instance().isRapidCommit()) {
  850. pkt6->addOption(Option::factory(Option::V6, D6O_RAPID_COMMIT));
  851. }
  852. pkt6->addOption(Option::factory(Option::V6, D6O_CLIENTID, duid));
  853. pkt6->addOption(Option::factory(Option::V6, D6O_ORO));
  854. pkt6->addOption(Option::factory(Option::V6, D6O_IA_NA));
  855. setDefaults6(socket, pkt6);
  856. pkt6->pack();
  857. IfaceMgr::instance().send(pkt6);
  858. if (!preload) {
  859. if (!stats_mgr6_) {
  860. isc_throw(InvalidOperation, "Statistics Manager for DHCPv6 "
  861. "hasn't been initialized");
  862. }
  863. stats_mgr6_->passSentPacket(StatsMgr6::XCHG_SA, pkt6);
  864. }
  865. }
  866. void
  867. TestControl::setDefaults4(const TestControlSocket& socket,
  868. const Pkt4Ptr& pkt) {
  869. CommandOptions& options = CommandOptions::instance();
  870. // Interface name.
  871. pkt->setIface(socket.getIface());
  872. // Interface index.
  873. pkt->setIndex(socket.getIfIndex());
  874. // Local client's port (68)
  875. pkt->setLocalPort(DHCP4_CLIENT_PORT);
  876. // Server's port (67)
  877. pkt->setRemotePort(DHCP4_SERVER_PORT);
  878. // The remote server's name or IP.
  879. pkt->setRemoteAddr(IOAddress(options.getServerName()));
  880. // Set local addresss.
  881. pkt->setLocalAddr(IOAddress(socket.getAddress()));
  882. // Set relay (GIADDR) address to local address.
  883. pkt->setGiaddr(IOAddress(socket.getAddress()));
  884. std::vector<uint8_t> mac = generateMacAddress();
  885. // Set hardware address
  886. pkt->setHWAddr(HTYPE_ETHER, mac.size(), mac);
  887. // Pretend that we have one relay (which is us).
  888. pkt->setHops(1);
  889. }
  890. void
  891. TestControl::setDefaults6(const TestControlSocket& socket,
  892. const Pkt6Ptr& pkt) {
  893. CommandOptions& options = CommandOptions::instance();
  894. // Interface name.
  895. pkt->setIface(socket.getIface());
  896. // Interface index.
  897. pkt->setIndex(socket.getIfIndex());
  898. // Local client's port (547)
  899. pkt->setLocalPort(DHCP6_CLIENT_PORT);
  900. // Server's port (548)
  901. pkt->setRemotePort(DHCP6_SERVER_PORT);
  902. // Set local address.
  903. pkt->setLocalAddr(socket.getAddress());
  904. // The remote server's name or IP.
  905. pkt->setRemoteAddr(IOAddress(options.getServerName()));
  906. }
  907. void
  908. TestControl::updateSendDue() {
  909. // If default constructor was called, this should not happen but
  910. // if somebody has changed default constructor it is better to
  911. // keep this check.
  912. if (last_sent_.is_not_a_date_time()) {
  913. isc_throw(Unexpected, "time of last sent packet not initialized");
  914. }
  915. // Get the expected exchange rate.
  916. CommandOptions& options = CommandOptions::instance();
  917. int rate = options.getRate();
  918. // If rate was not specified we will wait just one clock tick to
  919. // send next packet. This simulates best effort conditions.
  920. long duration = 1;
  921. if (rate != 0) {
  922. // We use number of ticks instead of nanoseconds because
  923. // nanosecond resolution may not be available on some
  924. // machines. Number of ticks guarantees the highest possible
  925. // timer resolution.
  926. duration = time_duration::ticks_per_second() / rate;
  927. }
  928. // Calculate due time to initate next chunk of exchanges.
  929. send_due_ = last_sent_ + time_duration(0, 0, 0, duration);
  930. }
  931. } // namespace perfdhcp
  932. } // namespace isc