alloc_engine.cc 9.3 KB

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