alloc_engine.cc 8.4 KB

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