alloc_engine.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <dhcpsrv/alloc_engine.h>
  15. #include <dhcpsrv/lease_mgr_factory.h>
  16. #include <cstring>
  17. #include <string.h>
  18. using namespace isc::asiolink;
  19. namespace isc {
  20. namespace dhcp {
  21. AllocEngine::IterativeAllocator::IterativeAllocator()
  22. :Allocator() {
  23. }
  24. isc::asiolink::IOAddress
  25. AllocEngine::IterativeAllocator::increaseAddress(const isc::asiolink::IOAddress& addr) {
  26. uint8_t packed[V6ADDRESS_LEN];
  27. int len;
  28. // First we copy the whole address as 16 bytes.
  29. if (addr.isV4()) {
  30. // IPv4
  31. std::memcpy(packed, &addr.toBytes()[0], 4);
  32. len = 4;
  33. } else {
  34. // IPv6
  35. std::memcpy(packed, &addr.toBytes()[0], 16);
  36. len = 16;
  37. }
  38. for (int i = len - 1; i >= 0; --i) {
  39. ++packed[i];
  40. if (packed[i] != 0) {
  41. break;
  42. }
  43. }
  44. return (IOAddress::fromBytes(addr.getFamily(), packed));
  45. }
  46. isc::asiolink::IOAddress
  47. AllocEngine::IterativeAllocator::pickAddress(const Subnet6Ptr& subnet,
  48. const DuidPtr&,
  49. const IOAddress&) {
  50. // Let's get the last allocated address. It is usually set correctly,
  51. // but there are times when it won't be (like after removing a pool or
  52. // perhaps restaring the server).
  53. IOAddress last = subnet->getLastAllocated();
  54. const Pool6Collection& pools = subnet->getPools();
  55. if (pools.empty()) {
  56. isc_throw(AllocFailed, "No pools defined in selected subnet");
  57. }
  58. // first we need to find a pool the last address belongs to.
  59. Pool6Collection::const_iterator it;
  60. for (it = pools.begin(); it != pools.end(); ++it) {
  61. if ((*it)->inRange(last)) {
  62. break;
  63. }
  64. }
  65. // last one was bogus for one of several reasons:
  66. // - we just booted up and that's the first address we're allocating
  67. // - a subnet was removed or other reconfiguration just completed
  68. // - perhaps allocation algorithm was changed
  69. if (it == pools.end()) {
  70. // ok to access first element directly. We checked that pools is non-empty
  71. IOAddress next = pools[0]->getFirstAddress();
  72. subnet->setLastAllocated(next);
  73. return (next);
  74. }
  75. // Ok, we have a pool that the last address belonged to, let's use it.
  76. IOAddress next = increaseAddress(last); // basically addr++
  77. if ((*it)->inRange(next)) {
  78. // the next one is in the pool as well, so we haven't hit pool boundary yet
  79. subnet->setLastAllocated(next);
  80. return (next);
  81. }
  82. // We hit pool boundary, let's try to jump to the next pool and try again
  83. ++it;
  84. if (it == pools.end()) {
  85. // Really out of luck today. That was the last pool. Let's rewind
  86. // to the beginning.
  87. next = pools[0]->getFirstAddress();
  88. subnet->setLastAllocated(next);
  89. return (next);
  90. }
  91. // there is a next pool, let's try first adddress from it
  92. next = (*it)->getFirstAddress();
  93. subnet->setLastAllocated(next);
  94. return (next);
  95. }
  96. AllocEngine::HashedAllocator::HashedAllocator()
  97. :Allocator() {
  98. isc_throw(NotImplemented, "Hashed allocator is not implemented");
  99. }
  100. isc::asiolink::IOAddress
  101. AllocEngine::HashedAllocator::pickAddress(const Subnet6Ptr&,
  102. const DuidPtr&,
  103. const IOAddress&) {
  104. isc_throw(NotImplemented, "Hashed allocator is not implemented");
  105. }
  106. AllocEngine::RandomAllocator::RandomAllocator()
  107. :Allocator() {
  108. isc_throw(NotImplemented, "Random allocator is not implemented");
  109. }
  110. isc::asiolink::IOAddress
  111. AllocEngine::RandomAllocator::pickAddress(const Subnet6Ptr&,
  112. const DuidPtr&,
  113. const IOAddress&) {
  114. isc_throw(NotImplemented, "Random allocator is not implemented");
  115. }
  116. AllocEngine::AllocEngine(AllocType engine_type, unsigned int attempts)
  117. :attempts_(attempts) {
  118. switch (engine_type) {
  119. case ALLOC_ITERATIVE:
  120. allocator_ = boost::shared_ptr<Allocator>(new IterativeAllocator());
  121. break;
  122. case ALLOC_HASHED:
  123. allocator_ = boost::shared_ptr<Allocator>(new HashedAllocator());
  124. break;
  125. case ALLOC_RANDOM:
  126. allocator_ = boost::shared_ptr<Allocator>(new RandomAllocator());
  127. break;
  128. default:
  129. isc_throw(BadValue, "Invalid/unsupported allocation algorithm");
  130. }
  131. }
  132. Lease6Ptr
  133. AllocEngine::allocateAddress6(const Subnet6Ptr& subnet,
  134. const DuidPtr& duid,
  135. uint32_t iaid,
  136. const IOAddress& hint,
  137. bool fake_allocation /* = false */ ) {
  138. // That check is not necessary. We create allocator in AllocEngine
  139. // constructor
  140. if (!allocator_) {
  141. isc_throw(InvalidOperation, "No allocator selected");
  142. }
  143. // check if there's existing lease for that subnet/duid/iaid combination.
  144. Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(*duid, iaid, subnet->getID());
  145. if (existing) {
  146. // we have a lease already. This is a returning client, probably after
  147. // his reboot.
  148. return (existing);
  149. }
  150. // check if the hint is in pool and is available
  151. if (subnet->inPool(hint)) {
  152. existing = LeaseMgrFactory::instance().getLease6(hint);
  153. if (!existing) {
  154. /// @todo: check if the hint is reserved once we have host support
  155. /// implemented
  156. // the hint is valid and not currently used, let's create a lease for it
  157. Lease6Ptr lease = createLease(subnet, duid, iaid, hint, fake_allocation);
  158. // It can happen that the lease allocation failed (we could have lost
  159. // the race condition. That means that the hint is lo longer usable and
  160. // we need to continue the regular allocation path.
  161. if (lease) {
  162. return (lease);
  163. }
  164. }
  165. }
  166. unsigned int i = attempts_;
  167. do {
  168. IOAddress candidate = allocator_->pickAddress(subnet, duid, hint);
  169. /// @todo: check if the address is reserved once we have host support
  170. /// implemented
  171. Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(candidate);
  172. // there's no existing lease for selected candidate, so it is
  173. // free. Let's allocate it.
  174. if (!existing) {
  175. Lease6Ptr lease = createLease(subnet, duid, iaid, candidate,
  176. fake_allocation);
  177. if (lease) {
  178. return (lease);
  179. }
  180. // Although the address was free just microseconds ago, it may have
  181. // been taken just now. If the lease insertion fails, we continue
  182. // allocation attempts.
  183. }
  184. // continue trying allocation until we run out of attempts
  185. // (or attempts are set to 0, which means infinite)
  186. --i;
  187. } while ( i || !attempts_);
  188. isc_throw(AllocFailed, "Failed to allocate address after " << attempts_
  189. << " tries");
  190. }
  191. Lease6Ptr AllocEngine::createLease(const Subnet6Ptr& subnet,
  192. const DuidPtr& duid,
  193. uint32_t iaid,
  194. const IOAddress& addr,
  195. bool fake_allocation /*= false */ ) {
  196. Lease6Ptr lease(new Lease6(Lease6::LEASE_IA_NA, addr, duid, iaid,
  197. subnet->getPreferred(), subnet->getValid(),
  198. subnet->getT1(), subnet->getT2(), subnet->getID()));
  199. if (!fake_allocation) {
  200. // That is a real (REQUEST) allocation
  201. bool status = LeaseMgrFactory::instance().addLease(lease);
  202. if (status) {
  203. return (lease);
  204. } else {
  205. // One of many failures with LeaseMgr (e.g. lost connection to the
  206. // database, database failed etc.). One notable case for that
  207. // is that we are working in multi-process mode and we lost a race
  208. // (some other process got that address first)
  209. return (Lease6Ptr());
  210. }
  211. } else {
  212. // That is only fake (SOLICIT without rapid-commit) allocation
  213. // It is for advertise only. We should not insert the lease into LeaseMgr,
  214. // but rather check that we could have inserted it.
  215. Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(addr);
  216. if (!existing) {
  217. return (lease);
  218. } else {
  219. return (Lease6Ptr());
  220. }
  221. }
  222. }
  223. AllocEngine::~AllocEngine() {
  224. // no need to delete allocator. smart_ptr will do the trick for us
  225. }
  226. }; // end of isc::dhcp namespace
  227. }; // end of isc namespace