test_control.cc 30 KB

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