test_control.cc 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  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 <fstream>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdint.h>
  18. #include <unistd.h>
  19. #include <signal.h>
  20. #include <boost/date_time/posix_time/posix_time.hpp>
  21. #include <exceptions/exceptions.h>
  22. #include <asiolink/io_address.h>
  23. #include <dhcp/libdhcp++.h>
  24. #include <dhcp/iface_mgr.h>
  25. #include <dhcp/dhcp4.h>
  26. #include <dhcp/option6_ia.h>
  27. #include "test_control.h"
  28. #include "command_options.h"
  29. #include "perf_pkt4.h"
  30. #include "perf_pkt6.h"
  31. using namespace std;
  32. using namespace boost;
  33. using namespace boost::posix_time;
  34. using namespace isc;
  35. using namespace isc::dhcp;
  36. using namespace isc::asiolink;
  37. namespace isc {
  38. namespace perfdhcp {
  39. bool TestControl::interrupted_ = false;
  40. TestControl::TestControlSocket::TestControlSocket(int socket) :
  41. socket_(socket),
  42. addr_("127.0.0.1") {
  43. initSocketData();
  44. }
  45. TestControl::TestControlSocket::~TestControlSocket() {
  46. IfaceMgr::instance().closeSockets();
  47. }
  48. void
  49. TestControl::TestControlSocket::initSocketData() {
  50. const IfaceMgr::IfaceCollection& ifaces =
  51. IfaceMgr::instance().getIfaces();
  52. for (IfaceMgr::IfaceCollection::const_iterator it = ifaces.begin();
  53. it != ifaces.end();
  54. ++it) {
  55. const IfaceMgr::SocketCollection& socket_collection =
  56. it->getSockets();
  57. for (IfaceMgr::SocketCollection::const_iterator s =
  58. socket_collection.begin();
  59. s != socket_collection.end();
  60. ++s) {
  61. if (s->sockfd_ == socket_) {
  62. iface_ = it->getName();
  63. ifindex_ = it->getIndex();
  64. addr_ = s->addr_;
  65. return;
  66. }
  67. }
  68. }
  69. isc_throw(BadValue, "interface for for specified socket "
  70. "descriptor not found");
  71. }
  72. TestControl&
  73. TestControl::instance() {
  74. static TestControl test_control;
  75. return (test_control);
  76. }
  77. TestControl::TestControl() {
  78. reset();
  79. }
  80. std::string
  81. TestControl::byte2Hex(const uint8_t b) const {
  82. const int b1 = b / 16;
  83. const int b0 = b % 16;
  84. ostringstream stream;
  85. stream << std::hex << b1 << b0 << std::dec;
  86. return (stream.str());
  87. }
  88. bool
  89. TestControl::checkExitConditions() const {
  90. if (interrupted_) {
  91. return (true);
  92. }
  93. CommandOptions& options = CommandOptions::instance();
  94. bool test_period_reached = false;
  95. // Check if test period passed.
  96. if (options.getPeriod() != 0) {
  97. if (options.getIpVersion() == 4) {
  98. time_period period(stats_mgr4_->getTestPeriod());
  99. if (period.length().total_seconds() >= options.getPeriod()) {
  100. test_period_reached = true;
  101. }
  102. } else if (options.getIpVersion() == 6) {
  103. time_period period = stats_mgr6_->getTestPeriod();
  104. if (period.length().total_seconds() >= options.getPeriod()) {
  105. test_period_reached = true;
  106. }
  107. }
  108. }
  109. if (test_period_reached) {
  110. if (testDiags('e')) {
  111. std::cout << "reached test-period." << std::endl;
  112. }
  113. return (true);
  114. }
  115. bool max_requests = false;
  116. // Check if we reached maximum number of DISCOVER/SOLICIT sent.
  117. if (options.getNumRequests().size() > 0) {
  118. if (options.getIpVersion() == 4) {
  119. if (getSentPacketsNum(StatsMgr4::XCHG_DO) >=
  120. options.getNumRequests()[0]) {
  121. max_requests = true;
  122. }
  123. } else if (options.getIpVersion() == 6) {
  124. if (stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_SA) >=
  125. options.getNumRequests()[0]) {
  126. max_requests = true;
  127. }
  128. }
  129. }
  130. // Check if we reached maximum number REQUEST packets.
  131. if (options.getNumRequests().size() == 2) {
  132. if (options.getIpVersion() == 4) {
  133. if (stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_RA) >=
  134. options.getNumRequests()[1]) {
  135. max_requests = true;
  136. }
  137. } else if (options.getIpVersion() == 6) {
  138. if (stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_RR) >=
  139. options.getNumRequests()[1]) {
  140. max_requests = true;
  141. }
  142. }
  143. }
  144. if (max_requests) {
  145. if (testDiags('e')) {
  146. std::cout << "Reached max requests limit." << std::endl;
  147. }
  148. return (true);
  149. }
  150. // Check if we reached maximum number of drops of OFFER/ADVERTISE packets.
  151. bool max_drops = false;
  152. if (options.getMaxDrop().size() > 0) {
  153. if (options.getIpVersion() == 4) {
  154. if (stats_mgr4_->getDroppedPacketsNum(StatsMgr4::XCHG_DO) >=
  155. options.getMaxDrop()[0]) {
  156. max_drops = true;
  157. }
  158. } else if (options.getIpVersion() == 6) {
  159. if (stats_mgr6_->getDroppedPacketsNum(StatsMgr6::XCHG_SA) >=
  160. options.getMaxDrop()[0]) {
  161. max_drops = true;
  162. }
  163. }
  164. }
  165. // Check if we reached maximum number of drops of ACK/REPLY packets.
  166. if (options.getMaxDrop().size() == 2) {
  167. if (options.getIpVersion() == 4) {
  168. if (stats_mgr4_->getDroppedPacketsNum(StatsMgr4::XCHG_RA) >=
  169. options.getMaxDrop()[1]) {
  170. max_drops = true;
  171. }
  172. } else if (options.getIpVersion() == 6) {
  173. if (stats_mgr6_->getDroppedPacketsNum(StatsMgr6::XCHG_RR) >=
  174. options.getMaxDrop()[1]) {
  175. max_drops = true;
  176. }
  177. }
  178. }
  179. if (max_drops) {
  180. if (testDiags('e')) {
  181. std::cout << "Reached maximum drops number." << std::endl;
  182. }
  183. return (true);
  184. }
  185. // Check if we reached maximum drops percentage of OFFER/ADVERTISE packets.
  186. bool max_pdrops = false;
  187. if (options.getMaxDropPercentage().size() > 0) {
  188. if (options.getIpVersion() == 4) {
  189. if ((stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_DO) > 10) &&
  190. ((100. * stats_mgr4_->getDroppedPacketsNum(StatsMgr4::XCHG_DO) /
  191. stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_DO)) >=
  192. options.getMaxDropPercentage()[0])) {
  193. max_pdrops = true;
  194. }
  195. } else if (options.getIpVersion() == 6) {
  196. if ((stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_SA) > 10) &&
  197. ((100. * stats_mgr6_->getDroppedPacketsNum(StatsMgr6::XCHG_SA) /
  198. stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_SA)) >=
  199. options.getMaxDropPercentage()[0])) {
  200. max_pdrops = true;
  201. }
  202. }
  203. }
  204. // Check if we reached maximum drops percentage of ACK/REPLY packets.
  205. if (options.getMaxDropPercentage().size() == 2) {
  206. if (options.getIpVersion() == 4) {
  207. if ((stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_RA) > 10) &&
  208. ((100. * stats_mgr4_->getDroppedPacketsNum(StatsMgr4::XCHG_RA) /
  209. stats_mgr4_->getSentPacketsNum(StatsMgr4::XCHG_RA)) >=
  210. options.getMaxDropPercentage()[1])) {
  211. max_pdrops = true;
  212. }
  213. } else if (options.getIpVersion() == 6) {
  214. if ((stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_RR) > 10) &&
  215. ((100. * stats_mgr6_->getDroppedPacketsNum(StatsMgr6::XCHG_RR) /
  216. stats_mgr6_->getSentPacketsNum(StatsMgr6::XCHG_RR)) >=
  217. options.getMaxDropPercentage()[1])) {
  218. max_pdrops = true;
  219. }
  220. }
  221. }
  222. if (max_pdrops) {
  223. if (testDiags('e')) {
  224. std::cout << "Reached maximum percentage of drops." << std::endl;
  225. }
  226. return (true);
  227. }
  228. return (false);
  229. }
  230. OptionPtr
  231. TestControl::factoryElapsedTime6(Option::Universe, uint16_t,
  232. const OptionBuffer& buf) {
  233. if (buf.size() == 2) {
  234. return (OptionPtr(new Option(Option::V6, D6O_ELAPSED_TIME, buf)));
  235. } else if (buf.size() == 0) {
  236. return (OptionPtr(new Option(Option::V6, D6O_ELAPSED_TIME,
  237. OptionBuffer(2, 0))));
  238. }
  239. isc_throw(isc::BadValue,
  240. "elapsed time option buffer size has to be 0 or 2");
  241. }
  242. OptionPtr
  243. TestControl::factoryGeneric(Option::Universe u, uint16_t type,
  244. const OptionBuffer& buf) {
  245. OptionPtr opt(new Option(u, type, buf));
  246. return (opt);
  247. }
  248. OptionPtr
  249. TestControl::factoryIana6(Option::Universe, uint16_t,
  250. const OptionBuffer& buf) {
  251. const uint8_t buf_array[] = {
  252. 0, 0, 0, 1, // IAID = 1
  253. 0, 0, 3600 >> 8, 3600 && 0xff, // T1 = 3600
  254. 0, 0, 5400 >> 8, 5400 & 0xff, // T2 = 5400
  255. };
  256. OptionBuffer buf_ia_na(buf_array, buf_array + sizeof(buf_array));
  257. for (int i = 0; i < buf.size(); ++i) {
  258. buf_ia_na.push_back(buf[i]);
  259. }
  260. return (OptionPtr(new Option(Option::V6, D6O_IA_NA, buf_ia_na)));
  261. }
  262. OptionPtr
  263. TestControl::factoryRapidCommit6(Option::Universe, uint16_t,
  264. const OptionBuffer&) {
  265. return (OptionPtr(new Option(Option::V6, D6O_RAPID_COMMIT, OptionBuffer())));
  266. }
  267. OptionPtr
  268. TestControl::factoryOptionRequestOption6(Option::Universe,
  269. uint16_t,
  270. const OptionBuffer&) {
  271. const uint8_t buf_array[] = {
  272. 0, D6O_NAME_SERVERS,
  273. 0, D6O_DOMAIN_SEARCH
  274. };
  275. OptionBuffer buf_with_options(buf_array, buf_array + sizeof(buf_array));
  276. return (OptionPtr(new Option(Option::V6, D6O_ORO, buf_with_options)));
  277. }
  278. OptionPtr
  279. TestControl::factoryRequestList4(Option::Universe u,
  280. uint16_t type,
  281. const OptionBuffer& buf) {
  282. const uint8_t buf_array[] = {
  283. DHO_SUBNET_MASK,
  284. DHO_BROADCAST_ADDRESS,
  285. DHO_TIME_OFFSET,
  286. DHO_ROUTERS,
  287. DHO_DOMAIN_NAME,
  288. DHO_DOMAIN_NAME_SERVERS,
  289. DHO_HOST_NAME
  290. };
  291. OptionBuffer buf_with_options(buf_array, buf_array + sizeof(buf_array));
  292. OptionPtr opt(new Option(u, type, buf));
  293. opt->setData(buf_with_options.begin(), buf_with_options.end());
  294. return (opt);
  295. }
  296. std::vector<uint8_t>
  297. TestControl::generateMacAddress(uint8_t& randomized) const {
  298. CommandOptions& options = CommandOptions::instance();
  299. uint32_t clients_num = options.getClientsNum();
  300. if ((clients_num == 0) || (clients_num == 1)) {
  301. return (options.getMacTemplate());
  302. }
  303. // Get the base MAC address. We are going to randomize part of it.
  304. std::vector<uint8_t> mac_addr(options.getMacTemplate());
  305. if (mac_addr.size() != HW_ETHER_LEN) {
  306. isc_throw(BadValue, "invalid MAC address template specified");
  307. }
  308. uint32_t r = random();
  309. // The random number must be in the range 0..clients_num. This
  310. // will guarantee that every client has exactly one random MAC
  311. // address assigned.
  312. r %= clients_num;
  313. randomized = 0;
  314. // Randomize MAC address octets.
  315. for (std::vector<uint8_t>::iterator it = mac_addr.end() - 1;
  316. it >= mac_addr.begin();
  317. --it) {
  318. // Add the random value to the current octet.
  319. (*it) += r;
  320. ++randomized;
  321. if (r < 256) {
  322. // If we are here it means that there is no sense
  323. // to randomize the remaining octets of MAC address
  324. // because the following bytes of random value
  325. // are zero and it will have no effect.
  326. break;
  327. }
  328. // Randomize the next octet with the following
  329. // byte of random value.
  330. r >>= 8;
  331. }
  332. return (mac_addr);
  333. }
  334. std::vector<uint8_t>
  335. TestControl::generateDuid(uint8_t& randomized) const {
  336. CommandOptions& options = CommandOptions::instance();
  337. uint32_t clients_num = options.getClientsNum();
  338. if ((clients_num == 0) || (clients_num == 1)) {
  339. return (options.getDuidTemplate());
  340. }
  341. // Get the base DUID. We are going to randomize part of it.
  342. std::vector<uint8_t> duid(options.getDuidTemplate());
  343. // @todo: add support for DUIDs of different sizes.
  344. std::vector<uint8_t> mac_addr(generateMacAddress(randomized));
  345. duid.resize(duid.size() - mac_addr.size());
  346. for (int i = 0; i < mac_addr.size(); ++i) {
  347. duid.push_back(mac_addr[i]);
  348. }
  349. return (duid);
  350. }
  351. uint64_t
  352. TestControl::getNextExchangesNum() const {
  353. CommandOptions& options = CommandOptions::instance();
  354. // Reset number of exchanges.
  355. uint64_t due_exchanges = 0;
  356. // Get current time.
  357. ptime now(microsec_clock::universal_time());
  358. if (now >= send_due_) {
  359. // If rate is specified from the command line we have to
  360. // synchornize with it.
  361. if (options.getRate() != 0) {
  362. time_period period(send_due_, now);
  363. time_duration duration = period.length();
  364. // due_factor indicates the number of seconds that
  365. // sending next chunk of packets will take.
  366. double due_factor = duration.fractional_seconds() /
  367. time_duration::ticks_per_second();
  368. due_factor += duration.total_seconds();
  369. // Multiplying due_factor by expected rate gives the number
  370. // of exchanges to be initiated.
  371. due_exchanges = static_cast<uint64_t>(due_factor * options.getRate());
  372. // We want to make sure that at least one packet goes out.
  373. due_exchanges += 1;
  374. // We should not exceed aggressivity as it could have been
  375. // restricted from command line.
  376. if (due_exchanges > options.getAggressivity()) {
  377. due_exchanges = options.getAggressivity();
  378. }
  379. } else {
  380. // Rate is not specified so we rely on aggressivity
  381. // which is the number of packets to be sent in
  382. // one chunk.
  383. due_exchanges = options.getAggressivity();
  384. }
  385. return (due_exchanges);
  386. }
  387. return (0);
  388. }
  389. uint64_t
  390. TestControl::getRcvdPacketsNum(const ExchangeType xchg_type) const {
  391. uint8_t ip_version = CommandOptions::instance().getIpVersion();
  392. if (ip_version == 4) {
  393. return (stats_mgr4_->getRcvdPacketsNum(xchg_type));
  394. }
  395. return (stats_mgr6_->
  396. getRcvdPacketsNum(static_cast<StatsMgr6::ExchangeType>(xchg_type)));
  397. }
  398. uint64_t
  399. TestControl::getSentPacketsNum(const ExchangeType xchg_type) const {
  400. uint8_t ip_version = CommandOptions::instance().getIpVersion();
  401. if (ip_version == 4) {
  402. return (stats_mgr4_->getSentPacketsNum(xchg_type));
  403. }
  404. return (stats_mgr6_->
  405. getSentPacketsNum(static_cast<StatsMgr6::ExchangeType>(xchg_type)));
  406. }
  407. TestControl::TemplateBuffer
  408. TestControl::getTemplateBuffer(const size_t idx) const {
  409. if (template_buffers_.size() > idx) {
  410. return (template_buffers_[idx]);
  411. }
  412. return (TemplateBuffer());
  413. }
  414. void
  415. TestControl::handleInterrupt(int) {
  416. interrupted_ = true;
  417. }
  418. void
  419. TestControl::initPacketTemplates() {
  420. template_buffers_.clear();
  421. CommandOptions& options = CommandOptions::instance();
  422. std::vector<std::string> template_files = options.getTemplateFiles();
  423. for (std::vector<std::string>::const_iterator it = template_files.begin();
  424. it != template_files.end();
  425. ++it) {
  426. readPacketTemplate(*it);
  427. }
  428. }
  429. void
  430. TestControl::initializeStatsMgr() {
  431. CommandOptions& options = CommandOptions::instance();
  432. if (options.getIpVersion() == 4) {
  433. stats_mgr4_.reset();
  434. stats_mgr4_ = StatsMgr4Ptr(new StatsMgr4());
  435. stats_mgr4_->addExchangeStats(StatsMgr4::XCHG_DO);
  436. if (options.getExchangeMode() == CommandOptions::DORA_SARR) {
  437. stats_mgr4_->addExchangeStats(StatsMgr4::XCHG_RA);
  438. }
  439. } else if (options.getIpVersion() == 6) {
  440. stats_mgr6_.reset();
  441. stats_mgr6_ = StatsMgr6Ptr(new StatsMgr6());
  442. stats_mgr6_->addExchangeStats(StatsMgr6::XCHG_SA);
  443. if (options.getExchangeMode() == CommandOptions::DORA_SARR) {
  444. stats_mgr6_->addExchangeStats(StatsMgr6::XCHG_RR);
  445. }
  446. }
  447. if (testDiags('i')) {
  448. if (options.getIpVersion() == 4) {
  449. stats_mgr4_->addCustomCounter("latesend", "Late sent packets");
  450. stats_mgr4_->addCustomCounter("shortwait", "Short waits for packets");
  451. stats_mgr4_->addCustomCounter("multircvd", "Multiple packets receives");
  452. // stats_mgr4_->addCustomCounter("latercvd", "Late received packets");
  453. } else if (options.getIpVersion() == 6) {
  454. stats_mgr6_->addCustomCounter("latesend", "Late sent packets");
  455. stats_mgr6_->addCustomCounter("shortwait", "Short waits for packets");
  456. stats_mgr6_->addCustomCounter("multircvd", "Multiple packets receives");
  457. // stats_mgr6_->addCustomCounter("latercvd", "Late received packets");
  458. }
  459. }
  460. }
  461. int
  462. TestControl::openSocket() const {
  463. CommandOptions& options = CommandOptions::instance();
  464. std::string localname = options.getLocalName();
  465. std::string servername = options.getServerName();
  466. uint16_t port = options.getLocalPort();
  467. uint8_t family = AF_INET;
  468. int sock = 0;
  469. IOAddress remoteaddr(servername);
  470. if (port == 0) {
  471. if (options.getIpVersion() == 6) {
  472. port = DHCP6_CLIENT_PORT;
  473. } else if (options.getIpVersion() == 4) {
  474. port = 67; // TODO: find out why port 68 is wrong here.
  475. }
  476. }
  477. if (options.getIpVersion() == 6) {
  478. family = AF_INET6;
  479. }
  480. // Local name is specified along with '-l' option.
  481. // It may point to interface name or local address.
  482. if (!localname.empty()) {
  483. // CommandOptions should be already aware wether local name
  484. // is interface name or address because it uses IfaceMgr to
  485. // scan interfaces and get's their names.
  486. if (options.isInterface()) {
  487. sock = IfaceMgr::instance().openSocketFromIface(localname,
  488. port,
  489. family);
  490. } else {
  491. IOAddress localaddr(localname);
  492. sock = IfaceMgr::instance().openSocketFromAddress(localaddr,
  493. port);
  494. }
  495. } else if (!servername.empty()) {
  496. // If only server name is given we will need to try to resolve
  497. // the local address to bind socket to based on remote address.
  498. sock = IfaceMgr::instance().openSocketFromRemoteAddress(remoteaddr,
  499. port);
  500. }
  501. if (sock <= 0) {
  502. isc_throw(BadValue, "unable to open socket to communicate with "
  503. "DHCP server");
  504. }
  505. // IfaceMgr does not set broadcast option on the socket. We rely
  506. // on CommandOptions object to find out if socket has to have
  507. // broadcast enabled.
  508. if ((options.getIpVersion() == 4) && options.isBroadcast()) {
  509. int broadcast_enable = 1;
  510. int ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST,
  511. &broadcast_enable, sizeof(broadcast_enable));
  512. if (ret < 0) {
  513. isc_throw(InvalidOperation,
  514. "unable to set broadcast option on the socket");
  515. }
  516. } else if (options.getIpVersion() == 6) {
  517. // If remote address is multicast we need to enable it on
  518. // the socket that has been created.
  519. asio::ip::address_v6 remote_v6 = remoteaddr.getAddress().to_v6();
  520. if (remote_v6.is_multicast()) {
  521. int hops = 1;
  522. int ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
  523. &hops, sizeof(hops));
  524. // If user specified interface name with '-l' the
  525. // IPV6_MULTICAST_IF has to be set.
  526. if ((ret >= 0) && options.isInterface()) {
  527. IfaceMgr::Iface* iface =
  528. IfaceMgr::instance().getIface(options.getLocalName());
  529. if (iface == NULL) {
  530. isc_throw(Unexpected, "unknown interface "
  531. << options.getLocalName());
  532. }
  533. int idx = iface->getIndex();
  534. ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
  535. &idx, sizeof(idx));
  536. }
  537. if (ret < 0) {
  538. isc_throw(InvalidOperation,
  539. "unable to enable multicast on socket " << sock
  540. << ". errno = " << errno);
  541. }
  542. }
  543. }
  544. return (sock);
  545. }
  546. void
  547. TestControl::printDiagnostics() const {
  548. CommandOptions& options = CommandOptions::instance();
  549. if (testDiags('a')) {
  550. // Print all command line parameters.
  551. options.printCommandLine();
  552. // Print MAC and DUID.
  553. std::cout << "Set MAC to " << vector2Hex(options.getMacTemplate(), "::")
  554. << std::endl;
  555. if (options.getDuidTemplate().size() > 0) {
  556. std::cout << "Set DUID to " << vector2Hex(options.getDuidTemplate()) << std::endl;
  557. }
  558. }
  559. }
  560. void
  561. TestControl::printRate() const {
  562. double rate = 0;
  563. CommandOptions& options = CommandOptions::instance();
  564. if (options.getIpVersion() == 4) {
  565. double duration =
  566. stats_mgr4_->getTestPeriod().length().total_nanoseconds() / 1e9;
  567. rate = stats_mgr4_->getRcvdPacketsNum(StatsMgr4::XCHG_DO) / duration;
  568. } else if (options.getIpVersion() == 6) {
  569. double duration =
  570. stats_mgr6_->getTestPeriod().length().total_nanoseconds() / 1e9;
  571. rate = stats_mgr6_->getRcvdPacketsNum(StatsMgr6::XCHG_SA) / duration;
  572. }
  573. std::cout << "***Rate statistics***" << std::endl;
  574. if (options.getRate() > 0) {
  575. std::cout << "Rate: " << rate << ", expected rate: "
  576. << options.getRate() << std::endl << std::endl;
  577. } else {
  578. std::cout << "Rate: " << rate << std::endl << std::endl;
  579. }
  580. }
  581. void
  582. TestControl::printIntermediateStats() {
  583. CommandOptions& options = CommandOptions::instance();
  584. int delay = options.getReportDelay();
  585. ptime now = microsec_clock::universal_time();
  586. time_period time_since_report(last_report_, now);
  587. if (time_since_report.length().total_seconds() >= delay) {
  588. if (options.getIpVersion() == 4) {
  589. stats_mgr4_->printIntermediateStats();
  590. } else if (options.getIpVersion() == 6) {
  591. stats_mgr6_->printIntermediateStats();
  592. }
  593. last_report_ = now;
  594. }
  595. }
  596. void
  597. TestControl::printStats() const {
  598. printRate();
  599. CommandOptions& options = CommandOptions::instance();
  600. if (options.getIpVersion() == 4) {
  601. if (!stats_mgr4_) {
  602. isc_throw(InvalidOperation, "Statistics Manager for DHCPv4 "
  603. "hasn't been initialized");
  604. }
  605. stats_mgr4_->printStats();
  606. if (testDiags('i')) {
  607. stats_mgr4_->printCustomCounters();
  608. }
  609. } else if (options.getIpVersion() == 6) {
  610. if (!stats_mgr6_) {
  611. isc_throw(InvalidOperation, "Statistics Manager for DHCPv6 "
  612. "hasn't been initialized");
  613. }
  614. stats_mgr6_->printStats();
  615. if (testDiags('i')) {
  616. stats_mgr6_->printCustomCounters();
  617. }
  618. }
  619. }
  620. std::string
  621. TestControl::vector2Hex(const std::vector<uint8_t>& vec,
  622. const std::string& separator /* ="" */) const {
  623. std::ostringstream stream;
  624. for (std::vector<uint8_t>::const_iterator it = vec.begin();
  625. it != vec.end();
  626. ++it) {
  627. if (it == vec.begin()) {
  628. stream << byte2Hex(*it);
  629. } else {
  630. stream << separator << byte2Hex(*it);
  631. }
  632. }
  633. return (stream.str());
  634. }
  635. void
  636. TestControl::readPacketTemplate(const std::string& file_name) {
  637. std::ifstream temp_file;
  638. temp_file.open(file_name.c_str(), ios::in | ios::binary | ios::ate);
  639. if (!temp_file.is_open()) {
  640. isc_throw(BadValue, "unable to open template file " << file_name);
  641. }
  642. std::ifstream::pos_type temp_size = temp_file.tellg();
  643. if (temp_size % 2 != 0) {
  644. temp_file.close();
  645. isc_throw(BadValue, "odd number of digits in template file");
  646. }
  647. temp_file.seekg(0, ios::beg);
  648. std::vector<char> hex_digits(temp_size);
  649. std::vector<uint8_t> binary_stream;
  650. temp_file.read(&hex_digits[0], temp_size);
  651. temp_file.close();
  652. for (int i = 0; i < hex_digits.size(); i += 2) {
  653. if (!isxdigit(hex_digits[i]) || !isxdigit(hex_digits[i+1])) {
  654. isc_throw(BadValue, "the '" << hex_digits[i] << hex_digits[i+1]
  655. << "' is not hexadecimal digit");
  656. }
  657. stringstream s;
  658. s << "0x" << hex_digits[i] << hex_digits[i+1];
  659. int b;
  660. s >> std::hex >> b;
  661. binary_stream.push_back(static_cast<uint8_t>(b));
  662. }
  663. template_buffers_.push_back(binary_stream);
  664. }
  665. void
  666. TestControl::receivePacket4(const TestControlSocket& socket,
  667. const Pkt4Ptr& pkt4) {
  668. if (pkt4->getType() == DHCPOFFER) {
  669. Pkt4Ptr discover_pkt4(stats_mgr4_->passRcvdPacket(StatsMgr4::XCHG_DO,
  670. pkt4));
  671. CommandOptions::ExchangeMode xchg_mode =
  672. CommandOptions::instance().getExchangeMode();
  673. if ((xchg_mode == CommandOptions::DORA_SARR) && discover_pkt4) {
  674. if (template_buffers_.size() < 2) {
  675. sendRequest4(socket, discover_pkt4, pkt4);
  676. } else {
  677. sendRequest4(socket, template_buffers_[1], discover_pkt4, pkt4);
  678. }
  679. }
  680. } else if (pkt4->getType() == DHCPACK) {
  681. stats_mgr4_->passRcvdPacket(StatsMgr4::XCHG_RA, pkt4);
  682. }
  683. }
  684. void
  685. TestControl::receivePacket6(const TestControlSocket& socket,
  686. const Pkt6Ptr& pkt6) {
  687. uint8_t packet_type = pkt6->getType();
  688. if (packet_type == DHCPV6_ADVERTISE) {
  689. Pkt6Ptr solicit_pkt6(stats_mgr6_->passRcvdPacket(StatsMgr6::XCHG_SA,
  690. pkt6));
  691. CommandOptions::ExchangeMode xchg_mode =
  692. CommandOptions::instance().getExchangeMode();
  693. if ((xchg_mode == CommandOptions::DORA_SARR) && solicit_pkt6) {
  694. if (template_buffers_.size() < 2) {
  695. sendRequest6(socket, solicit_pkt6, pkt6);
  696. } else {
  697. sendRequest6(socket, template_buffers_[1], solicit_pkt6, pkt6);
  698. }
  699. }
  700. } else if (packet_type == DHCPV6_REPLY) {
  701. stats_mgr6_->passRcvdPacket(StatsMgr6::XCHG_RR, pkt6);
  702. }
  703. }
  704. void
  705. TestControl::receivePackets(const TestControlSocket& socket) {
  706. int timeout = 0;
  707. bool receiving = true;
  708. uint64_t received = 0;
  709. while (receiving) {
  710. if (CommandOptions::instance().getIpVersion() == 4) {
  711. Pkt4Ptr pkt4 = IfaceMgr::instance().receive4(timeout);
  712. if (!pkt4) {
  713. receiving = false;
  714. } else {
  715. ++received;
  716. if ((received > 1) && testDiags('i')) {
  717. stats_mgr4_->incrementCounter("multircvd");
  718. }
  719. pkt4->unpack();
  720. receivePacket4(socket, pkt4);
  721. }
  722. } else if (CommandOptions::instance().getIpVersion() == 6) {
  723. Pkt6Ptr pkt6 = IfaceMgr::instance().receive6(timeout);
  724. if (!pkt6) {
  725. receiving = false;
  726. } else {
  727. ++received;
  728. if ((received > 1) && testDiags('i')) {
  729. stats_mgr6_->incrementCounter("multircvd");
  730. }
  731. if (pkt6->unpack()) {
  732. receivePacket6(socket, pkt6);
  733. }
  734. }
  735. }
  736. }
  737. }
  738. void
  739. TestControl::registerOptionFactories4() const {
  740. static bool factories_registered = false;
  741. if (!factories_registered) {
  742. // DHCP_MESSAGE_TYPE option factory.
  743. LibDHCP::OptionFactoryRegister(Option::V4,
  744. DHO_DHCP_MESSAGE_TYPE,
  745. &TestControl::factoryGeneric);
  746. // DHCP_SERVER_IDENTIFIER option factory.
  747. LibDHCP::OptionFactoryRegister(Option::V4,
  748. DHO_DHCP_SERVER_IDENTIFIER,
  749. &TestControl::factoryGeneric);
  750. // DHCP_PARAMETER_REQUEST_LIST option factory.
  751. LibDHCP::OptionFactoryRegister(Option::V4,
  752. DHO_DHCP_PARAMETER_REQUEST_LIST,
  753. &TestControl::factoryRequestList4);
  754. }
  755. factories_registered = true;
  756. }
  757. void
  758. TestControl::registerOptionFactories6() const {
  759. static bool factories_registered = false;
  760. if (!factories_registered) {
  761. // D60_ELAPSED_TIME
  762. LibDHCP::OptionFactoryRegister(Option::V6,
  763. D6O_ELAPSED_TIME,
  764. &TestControl::factoryElapsedTime6);
  765. // D6O_RAPID_COMMIT
  766. LibDHCP::OptionFactoryRegister(Option::V6,
  767. D6O_RAPID_COMMIT,
  768. &TestControl::factoryRapidCommit6);
  769. // D6O_ORO (option request option) factory.
  770. LibDHCP::OptionFactoryRegister(Option::V6,
  771. D6O_ORO,
  772. &TestControl::factoryOptionRequestOption6);
  773. // D6O_CLIENTID option factory.
  774. LibDHCP::OptionFactoryRegister(Option::V6,
  775. D6O_CLIENTID,
  776. &TestControl::factoryGeneric);
  777. // D6O_SERVERID option factory.
  778. LibDHCP::OptionFactoryRegister(Option::V6,
  779. D6O_SERVERID,
  780. &TestControl::factoryGeneric);
  781. // D6O_IA_NA option factory.
  782. LibDHCP::OptionFactoryRegister(Option::V6,
  783. D6O_IA_NA,
  784. &TestControl::factoryIana6);
  785. }
  786. factories_registered = true;
  787. }
  788. void
  789. TestControl::registerOptionFactories() const {
  790. CommandOptions& options = CommandOptions::instance();
  791. switch(options.getIpVersion()) {
  792. case 4:
  793. registerOptionFactories4();
  794. break;
  795. case 6:
  796. registerOptionFactories6();
  797. break;
  798. default:
  799. isc_throw(InvalidOperation, "command line options have to be parsed "
  800. "before DHCP option factories can be registered");
  801. }
  802. }
  803. void
  804. TestControl::reset() {
  805. send_due_ = microsec_clock::universal_time();
  806. last_sent_ = send_due_;
  807. last_report_ = send_due_;
  808. transid_gen_.reset();
  809. transid_gen_ = TransidGeneratorPtr(new TransidGenerator());
  810. first_packet_serverid_.clear();
  811. interrupted_ = false;
  812. }
  813. void
  814. TestControl::run() {
  815. // Reset singleton state before test starts.
  816. reset();
  817. CommandOptions& options = CommandOptions::instance();
  818. // Ip version is not set ONLY in case the command options
  819. // were not parsed. This surely means that parse() function
  820. // was not called prior to starting the test. This is fatal
  821. // error.
  822. if (options.getIpVersion() == 0) {
  823. isc_throw(InvalidOperation,
  824. "command options must be parsed before running a test");
  825. }
  826. // Diagnostics is command line options mainly.
  827. printDiagnostics();
  828. // Option factories have to be registered.
  829. registerOptionFactories();
  830. TestControlSocket socket(openSocket());
  831. // Initialize packet templates.
  832. initPacketTemplates();
  833. // Initialize randomization seed.
  834. if (options.isSeeded()) {
  835. srandom(options.getSeed());
  836. }
  837. // If user interrupts the program we will exit gracefully.
  838. signal(SIGINT, TestControl::handleInterrupt);
  839. // Preload server with number of packets.
  840. const bool do_preload = true;
  841. for (int i = 0; i < options.getPreload(); ++i) {
  842. if (options.getIpVersion() == 4) {
  843. // No template buffer means no -T option specified.
  844. // We will build packet ourselfs.
  845. if (template_buffers_.size() == 0) {
  846. sendDiscover4(socket, do_preload);
  847. } else {
  848. const uint8_t template_idx = 0;
  849. sendDiscover4(socket, template_buffers_[template_idx],
  850. do_preload);
  851. }
  852. } else if (options.getIpVersion() == 6) {
  853. // No template buffer means no -T option specified.
  854. // We will build packet ourselfs.
  855. if (template_buffers_.size() == 0) {
  856. sendSolicit6(socket, do_preload);
  857. } else {
  858. const uint8_t template_idx = 0;
  859. sendSolicit6(socket, template_buffers_[template_idx],
  860. do_preload);
  861. }
  862. }
  863. }
  864. // Initialize Statistics Manager. Release previous if any.
  865. initializeStatsMgr();
  866. for (;;) {
  867. // Calculate send due based on when last exchange was initiated.
  868. updateSendDue();
  869. // If test period finished, maximum number of packet drops
  870. // has been reached or test has been interrupted we have to
  871. // finish the test.
  872. if (checkExitConditions()) {
  873. break;
  874. }
  875. // Calculate number of packets to be sent to stay
  876. // catch up with rate.
  877. uint64_t packets_due = getNextExchangesNum();
  878. if ((packets_due == 0) && testDiags('i')) {
  879. if (options.getIpVersion() == 4) {
  880. stats_mgr4_->incrementCounter("shortwait");
  881. } else if (options.getIpVersion() == 6) {
  882. stats_mgr6_->incrementCounter("shortwait");
  883. }
  884. }
  885. // @todo: set non-zero timeout for packets once we implement
  886. // microseconds timeout in IfaceMgr.
  887. receivePackets(socket);
  888. // Send packets.
  889. for (uint64_t i = packets_due; i > 0; --i) {
  890. if (options.getIpVersion() == 4) {
  891. // No template packets means that no -T option was specified.
  892. // We have to build packets ourselfs.
  893. if (template_buffers_.size() == 0) {
  894. sendDiscover4(socket);
  895. } else {
  896. const uint8_t template_idx = 0;
  897. sendDiscover4(socket, template_buffers_[template_idx]);
  898. }
  899. } else {
  900. // No template packets means that no -T option was specified.
  901. // We have to build packets ourselfs.
  902. if (template_buffers_.size() == 0) {
  903. sendSolicit6(socket);
  904. } else {
  905. const uint8_t template_idx = 0;
  906. sendSolicit6(socket, template_buffers_[template_idx]);
  907. }
  908. }
  909. }
  910. // Report delay means that user requested printing number
  911. // of sent/received/dropped packets repeatedly.
  912. if (options.getReportDelay() > 0) {
  913. printIntermediateStats();
  914. }
  915. }
  916. printStats();
  917. // Print server id.
  918. if (testDiags('s') && (first_packet_serverid_.size() > 0)) {
  919. std::cout << "Server id: " << vector2Hex(first_packet_serverid_) << std::endl;
  920. }
  921. // Diagnostics flag 'e' means show exit reason.
  922. if (testDiags('e')) {
  923. std::cout << "Interrupted" << std::endl;
  924. }
  925. }
  926. void
  927. TestControl::sendDiscover4(const TestControlSocket& socket,
  928. const bool preload /*= false*/) {
  929. last_sent_ = microsec_clock::universal_time();
  930. // Generate the MAC address to be passed in the packet.
  931. uint8_t randomized = 0;
  932. std::vector<uint8_t> mac_address = generateMacAddress(randomized);
  933. // Generate trasnaction id to be set for the new exchange.
  934. const uint32_t transid = generateTransid();
  935. Pkt4Ptr pkt4(new Pkt4(DHCPDISCOVER, transid));
  936. if (!pkt4) {
  937. isc_throw(Unexpected, "failed to create DISCOVER packet");
  938. }
  939. // Set options: DHCP_MESSAGE_TYPE and DHCP_PARAMETER_REQUEST_LIST
  940. OptionBuffer buf_msg_type;
  941. buf_msg_type.push_back(DHCPDISCOVER);
  942. pkt4->addOption(Option::factory(Option::V4, DHO_DHCP_MESSAGE_TYPE,
  943. buf_msg_type));
  944. pkt4->addOption(Option::factory(Option::V4,
  945. DHO_DHCP_PARAMETER_REQUEST_LIST));
  946. // Set client's and server's ports as well as server's address,
  947. // and local (relay) address.
  948. setDefaults4(socket, pkt4);
  949. // Set hardware address
  950. pkt4->setHWAddr(HTYPE_ETHER, mac_address.size(), mac_address);
  951. pkt4->pack();
  952. IfaceMgr::instance().send(pkt4);
  953. if (!preload) {
  954. if (!stats_mgr4_) {
  955. isc_throw(InvalidOperation, "Statistics Manager for DHCPv4 "
  956. "hasn't been initialized");
  957. }
  958. stats_mgr4_->passSentPacket(StatsMgr4::XCHG_DO, pkt4);
  959. }
  960. }
  961. void
  962. TestControl::sendDiscover4(const TestControlSocket& socket,
  963. const std::vector<uint8_t>& template_buf,
  964. const bool preload /* = false */) {
  965. // last_sent_ has to be updated for each function that initiates
  966. // new transaction. The packet exchange synchronization relies on this.
  967. last_sent_ = microsec_clock::universal_time();
  968. CommandOptions& options = CommandOptions::instance();
  969. // Get the first argument if mulitple the same arguments specified
  970. // in the command line. First one refers to DISCOVER packets.
  971. const uint8_t arg_idx = 0;
  972. // Generate the MAC address to be passed in the packet.
  973. uint8_t randomized = 0;
  974. std::vector<uint8_t> mac_address = generateMacAddress(randomized);
  975. // Generate trasnaction id to be set for the new exchange.
  976. const uint32_t transid = generateTransid();
  977. // Get transaction id offset.
  978. size_t transid_offset = DHCPV4_TRANSID_OFFSET;
  979. if (options.getTransactionIdOffset().size() > arg_idx) {
  980. transid_offset = options.getTransactionIdOffset()[arg_idx];
  981. }
  982. // Calculate randomization offset.
  983. size_t rand_offset = DHCPV4_RANDOMIZATION_OFFSET;
  984. if (options.getRandomOffset().size() > arg_idx) {
  985. rand_offset = options.getRandomOffset()[arg_idx];
  986. }
  987. // We need to go back by HW_ETHER_LEN (MAC address length)
  988. // because this offset points to last octet of MAC address.
  989. rand_offset -= HW_ETHER_LEN + 1;
  990. // Create temporary buffer with template contents. We will
  991. // modify this temporary buffer but we don't want to modify
  992. // the original template.
  993. std::vector<uint8_t> in_buf(template_buf.begin(),
  994. template_buf.end());
  995. // Check if we are not going out of bounds.
  996. if (rand_offset + HW_ETHER_LEN > in_buf.size()) {
  997. isc_throw(OutOfRange, "randomization offset is out of bounds");
  998. }
  999. PerfPkt4Ptr pkt4(new PerfPkt4(&in_buf[0], in_buf.size(),
  1000. transid_offset,
  1001. transid));
  1002. // Replace MAC address in the template with actual MAC address.
  1003. pkt4->writeAt(rand_offset, mac_address.begin(), mac_address.end());
  1004. // Create a packet from the temporary buffer.
  1005. setDefaults4(socket, static_pointer_cast<Pkt4>(pkt4));
  1006. // Pack the input packet buffer to output buffer so as it can
  1007. // be sent to server.
  1008. pkt4->rawPack();
  1009. IfaceMgr::instance().send(static_pointer_cast<Pkt4>(pkt4));
  1010. if (!preload) {
  1011. if (!stats_mgr4_) {
  1012. isc_throw(InvalidOperation, "Statistics Manager for DHCPv4 "
  1013. "hasn't been initialized");
  1014. }
  1015. // Update packet stats.
  1016. stats_mgr4_->passSentPacket(StatsMgr4::XCHG_DO,
  1017. static_pointer_cast<Pkt4>(pkt4));
  1018. }
  1019. }
  1020. void
  1021. TestControl::sendRequest4(const TestControlSocket& socket,
  1022. const dhcp::Pkt4Ptr& discover_pkt4,
  1023. const dhcp::Pkt4Ptr& offer_pkt4) {
  1024. const uint32_t transid = generateTransid();
  1025. Pkt4Ptr pkt4(new Pkt4(DHCPREQUEST, transid));
  1026. OptionBuffer buf_msg_type;
  1027. buf_msg_type.push_back(DHCPREQUEST);
  1028. OptionPtr opt_msg_type = Option::factory(Option::V4, DHO_DHCP_MESSAGE_TYPE,
  1029. buf_msg_type);
  1030. pkt4->addOption(opt_msg_type);
  1031. if (CommandOptions::instance().isUseFirst() &&
  1032. (first_packet_serverid_.size() > 0)) {
  1033. pkt4->addOption(Option::factory(Option::V4, DHO_DHCP_SERVER_IDENTIFIER,
  1034. first_packet_serverid_));
  1035. } else {
  1036. OptionPtr opt_serverid =
  1037. offer_pkt4->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  1038. if (!opt_serverid) {
  1039. isc_throw(BadValue, "there is no SERVER_IDENTIFIER option "
  1040. << "in OFFER message");
  1041. }
  1042. if (stats_mgr4_->getRcvdPacketsNum(StatsMgr4::XCHG_DO) == 1) {
  1043. first_packet_serverid_ = opt_serverid->getData();
  1044. }
  1045. pkt4->addOption(opt_serverid);
  1046. }
  1047. /// Set client address.
  1048. asiolink::IOAddress yiaddr = offer_pkt4->getYiaddr();
  1049. if (!yiaddr.getAddress().is_v4()) {
  1050. isc_throw(BadValue, "the YIADDR returned in OFFER packet is not "
  1051. " IPv4 address");
  1052. }
  1053. OptionPtr opt_requested_address =
  1054. OptionPtr(new Option(Option::V4, DHO_DHCP_REQUESTED_ADDRESS,
  1055. OptionBuffer()));
  1056. opt_requested_address->setUint32(static_cast<uint32_t>(yiaddr));
  1057. pkt4->addOption(opt_requested_address);
  1058. OptionPtr opt_parameter_list =
  1059. Option::factory(Option::V4, DHO_DHCP_PARAMETER_REQUEST_LIST);
  1060. pkt4->addOption(opt_parameter_list);
  1061. // Set client's and server's ports as well as server's address,
  1062. // and local (relay) address.
  1063. setDefaults4(socket, pkt4);
  1064. // Set hardware address
  1065. const uint8_t* chaddr = offer_pkt4->getChaddr();
  1066. std::vector<uint8_t> mac_address(chaddr, chaddr + HW_ETHER_LEN);
  1067. pkt4->setHWAddr(HTYPE_ETHER, mac_address.size(), mac_address);
  1068. // Set elapsed time.
  1069. uint32_t elapsed_time = getElapsedTime<Pkt4Ptr>(discover_pkt4, offer_pkt4);
  1070. pkt4->setSecs(static_cast<uint16_t>(elapsed_time / 1000));
  1071. // Prepare on wire data to send.
  1072. pkt4->pack();
  1073. IfaceMgr::instance().send(pkt4);
  1074. if (!stats_mgr4_) {
  1075. isc_throw(InvalidOperation, "Statistics Manager for DHCPv4 "
  1076. "hasn't been initialized");
  1077. }
  1078. stats_mgr4_->passSentPacket(StatsMgr4::XCHG_RA, pkt4);
  1079. }
  1080. void
  1081. TestControl::sendRequest4(const TestControlSocket& socket,
  1082. const std::vector<uint8_t>& template_buf,
  1083. const dhcp::Pkt4Ptr& discover_pkt4,
  1084. const dhcp::Pkt4Ptr& offer_pkt4) {
  1085. CommandOptions& options = CommandOptions::instance();
  1086. // Get the second argument if multiple the same arguments specified
  1087. // in the command line. Second one refers to REQUEST packets.
  1088. const uint8_t arg_idx = 1;
  1089. // Generate new transaction id.
  1090. const uint32_t transid = generateTransid();
  1091. // Get transaction id offset.
  1092. size_t transid_offset = DHCPV4_TRANSID_OFFSET;
  1093. if (options.getTransactionIdOffset().size() > arg_idx) {
  1094. transid_offset = options.getTransactionIdOffset()[arg_idx];
  1095. }
  1096. // Get the offset of MAC's last octet.
  1097. size_t rand_offset = DHCPV4_RANDOMIZATION_OFFSET;
  1098. if (options.getRandomOffset().size() > arg_idx) {
  1099. rand_offset = options.getRandomOffset()[arg_idx];
  1100. }
  1101. // We need to go back by HW_ETHER_LEN (MAC address length)
  1102. // because this offset points to last octet of MAC address.
  1103. rand_offset -= HW_ETHER_LEN + 1;
  1104. // Create temporaru buffer from the template.
  1105. std::vector<uint8_t> in_buf(template_buf.begin(),
  1106. template_buf.end());
  1107. // Check if given randomization offset is not out of bounds.
  1108. if (rand_offset + HW_ETHER_LEN > in_buf.size()) {
  1109. isc_throw(OutOfRange, "randomization offset is out of bounds");
  1110. }
  1111. // Create packet from the temporary buffer.
  1112. PerfPkt4Ptr pkt4(new PerfPkt4(&in_buf[0], in_buf.size(),
  1113. transid_offset,
  1114. transid));
  1115. // Set hardware address from OFFER packet received.
  1116. const uint8_t* chaddr = offer_pkt4->getChaddr();
  1117. std::vector<uint8_t> mac_address(chaddr, chaddr + HW_ETHER_LEN);
  1118. pkt4->writeAt(rand_offset, mac_address.begin(), mac_address.end());
  1119. // Set elapsed time.
  1120. size_t elp_offset = 0;
  1121. if (options.getElapsedTimeOffset() > 0) {
  1122. elp_offset = options.getElapsedTimeOffset();
  1123. }
  1124. uint32_t elapsed_time = getElapsedTime<Pkt4Ptr>(discover_pkt4, offer_pkt4);
  1125. pkt4->writeValueAt<uint16_t>(elp_offset,
  1126. static_cast<uint16_t>(elapsed_time / 1000));
  1127. // Get the actual server id offset.
  1128. size_t sid_offset = DHCPV4_SERVERID_OFFSET;
  1129. if (options.getServerIdOffset() > 0) {
  1130. sid_offset = options.getServerIdOffset();
  1131. }
  1132. if (CommandOptions::instance().isUseFirst() &&
  1133. (first_packet_serverid_.size() > 0)) {
  1134. boost::shared_ptr<LocalizedOption>
  1135. opt_serverid(new LocalizedOption(Option::V4,
  1136. DHO_DHCP_SERVER_IDENTIFIER,
  1137. first_packet_serverid_,
  1138. sid_offset));
  1139. pkt4->addOption(opt_serverid);
  1140. } else {
  1141. // Copy the contents of server identifier received in
  1142. // OFFER packet to put this into REQUEST.
  1143. OptionPtr opt_serverid_offer =
  1144. offer_pkt4->getOption(DHO_DHCP_SERVER_IDENTIFIER);
  1145. if (!opt_serverid_offer) {
  1146. isc_throw(BadValue, "there is no SERVER_IDENTIFIER option "
  1147. << "in OFFER message");
  1148. }
  1149. boost::shared_ptr<LocalizedOption>
  1150. opt_serverid(new LocalizedOption(Option::V4,
  1151. DHO_DHCP_SERVER_IDENTIFIER,
  1152. opt_serverid_offer->getData(),
  1153. sid_offset));
  1154. pkt4->addOption(opt_serverid);
  1155. if (stats_mgr4_->getRcvdPacketsNum(StatsMgr4::XCHG_DO) == 1) {
  1156. first_packet_serverid_ = opt_serverid_offer->getData();
  1157. }
  1158. }
  1159. /// Set client address.
  1160. asiolink::IOAddress yiaddr = offer_pkt4->getYiaddr();
  1161. if (!yiaddr.getAddress().is_v4()) {
  1162. isc_throw(BadValue, "the YIADDR returned in OFFER packet is not "
  1163. " IPv4 address");
  1164. }
  1165. // Get the actual offset of requested ip.
  1166. size_t rip_offset = DHCPV4_REQUESTED_IP_OFFSET;
  1167. if (options.getRequestedIpOffset() > 0) {
  1168. rip_offset = options.getRequestedIpOffset();
  1169. }
  1170. // Place requested IP option at specified position (rip_offset).
  1171. boost::shared_ptr<LocalizedOption>
  1172. opt_requested_ip(new LocalizedOption(Option::V4,
  1173. DHO_DHCP_REQUESTED_ADDRESS,
  1174. OptionBuffer(),
  1175. rip_offset));
  1176. // The IOAddress is castable to uint32_t and returns exactly what we need.
  1177. opt_requested_ip->setUint32(static_cast<uint32_t>(yiaddr));
  1178. pkt4->addOption(opt_requested_ip);
  1179. setDefaults4(socket, static_pointer_cast<Pkt4>(pkt4));
  1180. // Prepare on-wire data.
  1181. pkt4->rawPack();
  1182. IfaceMgr::instance().send(static_pointer_cast<Pkt4>(pkt4));
  1183. if (!stats_mgr4_) {
  1184. isc_throw(InvalidOperation, "Statistics Manager for DHCPv4 "
  1185. "hasn't been initialized");
  1186. }
  1187. // Update packet stats.
  1188. stats_mgr4_->passSentPacket(StatsMgr4::XCHG_RA,
  1189. static_pointer_cast<Pkt4>(pkt4));
  1190. }
  1191. void
  1192. TestControl::sendRequest6(const TestControlSocket& socket,
  1193. const Pkt6Ptr& solicit_pkt6,
  1194. const Pkt6Ptr& advertise_pkt6) {
  1195. const uint32_t transid = generateTransid();
  1196. Pkt6Ptr pkt6(new Pkt6(DHCPV6_REQUEST, transid));
  1197. // Set elapsed time.
  1198. const uint32_t elapsed_time =
  1199. getElapsedTime<Pkt6Ptr>(solicit_pkt6, advertise_pkt6);
  1200. OptionPtr opt_elapsed_time =
  1201. Option::factory(Option::V6, D6O_ELAPSED_TIME);
  1202. opt_elapsed_time->setUint16(static_cast<uint16_t>(elapsed_time / 10));
  1203. pkt6->addOption(opt_elapsed_time);
  1204. // Set client id.
  1205. OptionPtr opt_clientid = advertise_pkt6->getOption(D6O_CLIENTID);
  1206. if (!opt_clientid) {
  1207. isc_throw(Unexpected, "client id not found in received packet");
  1208. }
  1209. pkt6->addOption(opt_clientid);
  1210. // Use first flags indicates that we want to use the server
  1211. // id captured in fisrt packet.
  1212. if (CommandOptions::instance().isUseFirst() &&
  1213. (first_packet_serverid_.size() > 0)) {
  1214. pkt6->addOption(Option::factory(Option::V6, D6O_SERVERID,
  1215. first_packet_serverid_));
  1216. } else {
  1217. OptionPtr opt_serverid = advertise_pkt6->getOption(D6O_SERVERID);
  1218. if (!opt_serverid) {
  1219. isc_throw(Unexpected, "server id not found in received packet");
  1220. }
  1221. if (stats_mgr6_->getRcvdPacketsNum(StatsMgr6::XCHG_SA) == 1) {
  1222. first_packet_serverid_ = opt_serverid->getData();
  1223. }
  1224. pkt6->addOption(opt_serverid);
  1225. }
  1226. // Set IA_NA option.
  1227. OptionPtr opt_ia_na = advertise_pkt6->getOption(D6O_IA_NA);
  1228. if (!opt_ia_na) {
  1229. isc_throw(Unexpected, "DHCPv6 IA_NA option not found in received "
  1230. "packet");
  1231. }
  1232. pkt6->addOption(opt_ia_na);
  1233. // Set default packet data.
  1234. setDefaults6(socket, pkt6);
  1235. // Prepare on-wire data.
  1236. pkt6->pack();
  1237. IfaceMgr::instance().send(pkt6);
  1238. if (!stats_mgr6_) {
  1239. isc_throw(InvalidOperation, "Statistics Manager for DHCPv6 "
  1240. "hasn't been initialized");
  1241. }
  1242. stats_mgr6_->passSentPacket(StatsMgr6::XCHG_RR, pkt6);
  1243. }
  1244. void
  1245. TestControl::sendRequest6(const TestControlSocket& socket,
  1246. const std::vector<uint8_t>& template_buf,
  1247. const Pkt6Ptr& solicit_pkt6,
  1248. const Pkt6Ptr& advertise_pkt6) {
  1249. CommandOptions& options = CommandOptions::instance();
  1250. // Get the second argument if multiple the same arguments specified
  1251. // in the command line. Second one refers to REQUEST packets.
  1252. const uint8_t arg_idx = 1;
  1253. // Generate transaction id.
  1254. const uint32_t transid = generateTransid();
  1255. // Get transaction id offset.
  1256. size_t transid_offset = DHCPV6_TRANSID_OFFSET;
  1257. if (options.getTransactionIdOffset().size() > arg_idx) {
  1258. transid_offset = options.getTransactionIdOffset()[arg_idx];
  1259. }
  1260. PerfPkt6Ptr pkt6(new PerfPkt6(&template_buf[0], template_buf.size(),
  1261. transid_offset, transid));
  1262. // Set elapsed time.
  1263. size_t elp_offset = DHCPV6_ELAPSED_TIME_OFFSET;
  1264. if (options.getElapsedTimeOffset() > 0) {
  1265. elp_offset = options.getElapsedTimeOffset();
  1266. }
  1267. uint32_t elapsed_time =
  1268. getElapsedTime<Pkt6Ptr>(solicit_pkt6, advertise_pkt6);
  1269. boost::shared_ptr<LocalizedOption>
  1270. opt_elapsed_time(new LocalizedOption(Option::V6, D6O_ELAPSED_TIME,
  1271. OptionBuffer(), elp_offset));
  1272. opt_elapsed_time->setUint16(static_cast<uint16_t>(elapsed_time / 10));
  1273. pkt6->addOption(opt_elapsed_time);
  1274. // Get the actual server id offset.
  1275. size_t sid_offset = DHCPV6_SERVERID_OFFSET;
  1276. if (options.getServerIdOffset() > 0) {
  1277. sid_offset = options.getServerIdOffset();
  1278. }
  1279. if (CommandOptions::instance().isUseFirst() &&
  1280. (first_packet_serverid_.size() > 0)) {
  1281. boost::shared_ptr<LocalizedOption>
  1282. opt_serverid(new LocalizedOption(Option::V6,
  1283. D6O_SERVERID,
  1284. first_packet_serverid_,
  1285. sid_offset));
  1286. pkt6->addOption(opt_serverid);
  1287. } else {
  1288. // Copy the contents of server identifier received in
  1289. // ADVERTISE packet to put this into REQUEST.
  1290. OptionPtr opt_serverid_advertise =
  1291. advertise_pkt6->getOption(D6O_SERVERID);
  1292. if (!opt_serverid_advertise) {
  1293. isc_throw(BadValue, "there is no SERVERID option "
  1294. << "in ADVERTISE message");
  1295. }
  1296. boost::shared_ptr<LocalizedOption>
  1297. opt_serverid(new LocalizedOption(Option::V6,
  1298. D6O_SERVERID,
  1299. opt_serverid_advertise->getData(),
  1300. sid_offset));
  1301. pkt6->addOption(opt_serverid);
  1302. if (stats_mgr6_->getRcvdPacketsNum(StatsMgr6::XCHG_SA) == 1) {
  1303. first_packet_serverid_ = opt_serverid_advertise->getData();
  1304. }
  1305. }
  1306. // Set IA_NA
  1307. boost::shared_ptr<Option6IA> opt_ia_na_advertise =
  1308. static_pointer_cast<Option6IA>(advertise_pkt6->getOption(D6O_IA_NA));
  1309. if (!opt_ia_na_advertise) {
  1310. isc_throw(Unexpected, "DHCPv6 IA_NA option not found in received "
  1311. "packet");
  1312. }
  1313. size_t addr_offset = DHCPV6_IA_NA_OFFSET;
  1314. if (options.getRequestedIpOffset() > 0) {
  1315. addr_offset = options.getRequestedIpOffset();
  1316. }
  1317. boost::shared_ptr<LocalizedOption>
  1318. opt_ia_na(new LocalizedOption(opt_ia_na_advertise, addr_offset));
  1319. if (!opt_ia_na->valid()) {
  1320. isc_throw(BadValue, "Option IA_NA in advertise packet is invalid");
  1321. }
  1322. pkt6->addOption(opt_ia_na);
  1323. // Set server id.
  1324. OptionPtr opt_serverid_advertise = advertise_pkt6->getOption(D6O_SERVERID);
  1325. if (!opt_serverid_advertise) {
  1326. isc_throw(Unexpected, "DHCPV6 SERVERID option not found in received "
  1327. "packet");
  1328. }
  1329. size_t srvid_offset = DHCPV6_SERVERID_OFFSET;
  1330. if (options.getServerIdOffset() > 0) {
  1331. srvid_offset = options.getServerIdOffset();
  1332. }
  1333. boost::shared_ptr<LocalizedOption>
  1334. opt_serverid(new LocalizedOption(Option::V6, D6O_SERVERID,
  1335. opt_serverid_advertise->getData(),
  1336. srvid_offset));
  1337. pkt6->addOption(opt_serverid);
  1338. // Get randomization offset.
  1339. size_t rand_offset = DHCPV6_RANDOMIZATION_OFFSET;
  1340. if (options.getRandomOffset().size() > arg_idx) {
  1341. rand_offset = options.getRandomOffset()[arg_idx];
  1342. }
  1343. OptionPtr opt_clientid_advertise = advertise_pkt6->getOption(D6O_CLIENTID);
  1344. if (!opt_clientid_advertise) {
  1345. isc_throw(Unexpected, "DHCPV6 CLIENTID option not found in received packet");
  1346. }
  1347. rand_offset -= (opt_clientid_advertise->len() - 1);
  1348. // Set client id.
  1349. boost::shared_ptr<LocalizedOption>
  1350. opt_clientid(new LocalizedOption(Option::V6, D6O_CLIENTID,
  1351. opt_clientid_advertise->getData(),
  1352. rand_offset));
  1353. pkt6->addOption(opt_clientid);
  1354. // Set default packet data.
  1355. setDefaults6(socket, pkt6);
  1356. // Prepare on wire data.
  1357. pkt6->rawPack();
  1358. // Send packet.
  1359. IfaceMgr::instance().send(pkt6);
  1360. if (!stats_mgr6_) {
  1361. isc_throw(InvalidOperation, "Statistics Manager for DHCPv6 "
  1362. "hasn't been initialized");
  1363. }
  1364. // Update packet stats.
  1365. stats_mgr6_->passSentPacket(StatsMgr6::XCHG_RR, pkt6);
  1366. }
  1367. void
  1368. TestControl::sendSolicit6(const TestControlSocket& socket,
  1369. const bool preload /*= false*/) {
  1370. last_sent_ = microsec_clock::universal_time();
  1371. // Generate DUID to be passed to the packet
  1372. uint8_t randomized = 0;
  1373. std::vector<uint8_t> duid = generateDuid(randomized);
  1374. // Generate trasnaction id to be set for the new exchange.
  1375. const uint32_t transid = generateTransid();
  1376. Pkt6Ptr pkt6(new Pkt6(DHCPV6_SOLICIT, transid));
  1377. if (!pkt6) {
  1378. isc_throw(Unexpected, "failed to create SOLICIT packet");
  1379. }
  1380. pkt6->addOption(Option::factory(Option::V6, D6O_ELAPSED_TIME));
  1381. if (CommandOptions::instance().isRapidCommit()) {
  1382. pkt6->addOption(Option::factory(Option::V6, D6O_RAPID_COMMIT));
  1383. }
  1384. pkt6->addOption(Option::factory(Option::V6, D6O_CLIENTID, duid));
  1385. pkt6->addOption(Option::factory(Option::V6, D6O_ORO));
  1386. pkt6->addOption(Option::factory(Option::V6, D6O_IA_NA));
  1387. setDefaults6(socket, pkt6);
  1388. pkt6->pack();
  1389. IfaceMgr::instance().send(pkt6);
  1390. if (!preload) {
  1391. if (!stats_mgr6_) {
  1392. isc_throw(InvalidOperation, "Statistics Manager for DHCPv6 "
  1393. "hasn't been initialized");
  1394. }
  1395. stats_mgr6_->passSentPacket(StatsMgr6::XCHG_SA, pkt6);
  1396. }
  1397. }
  1398. void
  1399. TestControl::sendSolicit6(const TestControlSocket& socket,
  1400. const std::vector<uint8_t>& template_buf,
  1401. const bool preload /*= false*/) {
  1402. last_sent_ = microsec_clock::universal_time();
  1403. CommandOptions& options = CommandOptions::instance();
  1404. const int arg_idx = 0;
  1405. // Get transaction id offset.
  1406. size_t transid_offset = DHCPV6_TRANSID_OFFSET;
  1407. if (options.getTransactionIdOffset().size() > arg_idx) {
  1408. transid_offset = options.getTransactionIdOffset()[arg_idx];
  1409. }
  1410. // Generate trasnaction id to be set for the new exchange.
  1411. const uint32_t transid = generateTransid();
  1412. // Create packet.
  1413. PerfPkt6Ptr pkt6(new PerfPkt6(&template_buf[0], template_buf.size(),
  1414. transid_offset, transid));
  1415. if (!pkt6) {
  1416. isc_throw(Unexpected, "failed to create SOLICIT packet");
  1417. }
  1418. size_t rand_offset = DHCPV6_RANDOMIZATION_OFFSET;
  1419. if (options.getRandomOffset().size() > arg_idx) {
  1420. rand_offset = options.getRandomOffset()[arg_idx];
  1421. }
  1422. // randomized will pick number of bytes randomized so we can
  1423. // just use part of the generated duid and substitude a few bytes
  1424. /// in template.
  1425. uint8_t randomized = 0;
  1426. std::vector<uint8_t> duid = generateDuid(randomized);
  1427. if (rand_offset > template_buf.size()) {
  1428. isc_throw(OutOfRange, "randomization offset is out of bounds");
  1429. }
  1430. // Store random part of the DUID into the packet.
  1431. pkt6->writeAt(rand_offset - randomized + 1,
  1432. duid.end() - randomized, duid.end());
  1433. // Prepare on-wire data.
  1434. pkt6->rawPack();
  1435. setDefaults6(socket, pkt6);
  1436. // Send solicit packet.
  1437. IfaceMgr::instance().send(pkt6);
  1438. if (!preload) {
  1439. if (!stats_mgr6_) {
  1440. isc_throw(InvalidOperation, "Statistics Manager for DHCPv6 "
  1441. "hasn't been initialized");
  1442. }
  1443. // Update packet stats.
  1444. stats_mgr6_->passSentPacket(StatsMgr6::XCHG_SA, pkt6);
  1445. }
  1446. }
  1447. void
  1448. TestControl::setDefaults4(const TestControlSocket& socket,
  1449. const Pkt4Ptr& pkt) {
  1450. CommandOptions& options = CommandOptions::instance();
  1451. // Interface name.
  1452. pkt->setIface(socket.getIface());
  1453. // Interface index.
  1454. pkt->setIndex(socket.getIfIndex());
  1455. // Local client's port (68)
  1456. pkt->setLocalPort(DHCP4_CLIENT_PORT);
  1457. // Server's port (67)
  1458. pkt->setRemotePort(DHCP4_SERVER_PORT);
  1459. // The remote server's name or IP.
  1460. pkt->setRemoteAddr(IOAddress(options.getServerName()));
  1461. // Set local addresss.
  1462. pkt->setLocalAddr(IOAddress(socket.getAddress()));
  1463. // Set relay (GIADDR) address to local address.
  1464. pkt->setGiaddr(IOAddress(socket.getAddress()));
  1465. // Pretend that we have one relay (which is us).
  1466. pkt->setHops(1);
  1467. }
  1468. void
  1469. TestControl::setDefaults6(const TestControlSocket& socket,
  1470. const Pkt6Ptr& pkt) {
  1471. CommandOptions& options = CommandOptions::instance();
  1472. // Interface name.
  1473. pkt->setIface(socket.getIface());
  1474. // Interface index.
  1475. pkt->setIndex(socket.getIfIndex());
  1476. // Local client's port (547)
  1477. pkt->setLocalPort(DHCP6_CLIENT_PORT);
  1478. // Server's port (548)
  1479. pkt->setRemotePort(DHCP6_SERVER_PORT);
  1480. // Set local address.
  1481. pkt->setLocalAddr(socket.getAddress());
  1482. // The remote server's name or IP.
  1483. pkt->setRemoteAddr(IOAddress(options.getServerName()));
  1484. }
  1485. bool
  1486. TestControl::testDiags(const char diag) const {
  1487. std::string diags(CommandOptions::instance().getDiags());
  1488. if (diags.find(diag) != std::string::npos) {
  1489. return (true);
  1490. }
  1491. return (false);
  1492. }
  1493. void
  1494. TestControl::updateSendDue() {
  1495. // If default constructor was called, this should not happen but
  1496. // if somebody has changed default constructor it is better to
  1497. // keep this check.
  1498. if (last_sent_.is_not_a_date_time()) {
  1499. isc_throw(Unexpected, "time of last sent packet not initialized");
  1500. }
  1501. // Get the expected exchange rate.
  1502. CommandOptions& options = CommandOptions::instance();
  1503. int rate = options.getRate();
  1504. // If rate was not specified we will wait just one clock tick to
  1505. // send next packet. This simulates best effort conditions.
  1506. long duration = 1;
  1507. if (rate != 0) {
  1508. // We use number of ticks instead of nanoseconds because
  1509. // nanosecond resolution may not be available on some
  1510. // machines. Number of ticks guarantees the highest possible
  1511. // timer resolution.
  1512. duration = time_duration::ticks_per_second() / rate;
  1513. }
  1514. // Calculate due time to initate next chunk of exchanges.
  1515. send_due_ = last_sent_ + time_duration(0, 0, 0, duration);
  1516. // Check if it is already due.
  1517. ptime now(microsec_clock::universal_time());
  1518. if (now > send_due_) {
  1519. if (testDiags('i')) {
  1520. if (options.getIpVersion() == 4) {
  1521. stats_mgr4_->incrementCounter("latesend");
  1522. } else if (options.getIpVersion() == 6) {
  1523. stats_mgr6_->incrementCounter("latesend");
  1524. }
  1525. }
  1526. }
  1527. }
  1528. } // namespace perfdhcp
  1529. } // namespace isc