alloc_engine.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // Copyright (C) 2012-2013 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. #ifndef ALLOC_ENGINE_H
  15. #define ALLOC_ENGINE_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/duid.h>
  18. #include <dhcp/hwaddr.h>
  19. #include <dhcpsrv/subnet.h>
  20. #include <dhcpsrv/lease_mgr.h>
  21. #include <hooks/callout_handle.h>
  22. #include <boost/shared_ptr.hpp>
  23. #include <boost/noncopyable.hpp>
  24. namespace isc {
  25. namespace dhcp {
  26. /// An exception that is thrown when allocation module fails (e.g. due to
  27. /// lack of available addresses)
  28. class AllocFailed : public isc::Exception {
  29. public:
  30. /// @brief constructor
  31. ///
  32. /// @param file name of the file, where exception occurred
  33. /// @param line line of the file, where exception occurred
  34. /// @param what text description of the issue that caused exception
  35. AllocFailed(const char* file, size_t line, const char* what)
  36. : isc::Exception(file, line, what) {}
  37. };
  38. /// @brief DHCPv4 and DHCPv6 allocation engine
  39. ///
  40. /// This class represents DHCP allocation engine. It is responsible
  41. /// for picking subnets, choosing and allocating a lease, extending,
  42. /// renewing, releasing and possibly expiring leases.
  43. ///
  44. /// @todo: Does not handle out of leases well
  45. /// @todo: Does not handle out of allocation attempts well
  46. class AllocEngine : public boost::noncopyable {
  47. protected:
  48. /// @brief base class for all address/prefix allocation algorithms
  49. ///
  50. /// This is an abstract class that should not be used directly, but rather
  51. /// specialized implementations should be used instead.
  52. class Allocator {
  53. public:
  54. /// @brief picks one address out of available pools in a given subnet
  55. ///
  56. /// This method returns one address from the available pools in the
  57. /// specified subnet. It should not check if the address is used or
  58. /// reserved - AllocEngine will check that and will call pickAddress
  59. /// again if necessary. The number of times this method is called will
  60. /// increase as the number of available leases will decrease.
  61. ///
  62. /// @param subnet next address will be returned from pool of that subnet
  63. /// @param duid Client's DUID
  64. /// @param hint client's hint
  65. ///
  66. /// @return the next address
  67. virtual isc::asiolink::IOAddress
  68. pickAddress(const SubnetPtr& subnet, const DuidPtr& duid,
  69. const isc::asiolink::IOAddress& hint) = 0;
  70. /// @brief Default constructor.
  71. ///
  72. /// Specifies which type of leases this allocator will assign
  73. /// @param pool_type specifies pool type (addresses, temp. addr or prefixes)
  74. Allocator(Lease::Type pool_type)
  75. :pool_type_(pool_type) {
  76. }
  77. /// @brief virtual destructor
  78. virtual ~Allocator() {
  79. }
  80. protected:
  81. /// @brief defines pool type allocation
  82. Lease::Type pool_type_;
  83. };
  84. /// @brief Address/prefix allocator that iterates over all addresses
  85. ///
  86. /// This class implements iterative algorithm that returns all addresses in
  87. /// a pool iteratively, one after another. Once the last address is reached,
  88. /// it starts allocating from the beginning of the first pool (i.e. it loops
  89. /// over).
  90. class IterativeAllocator : public Allocator {
  91. public:
  92. /// @brief default constructor
  93. ///
  94. /// Does not do anything
  95. /// @param type - specifies allocation type
  96. IterativeAllocator(Lease::Type type);
  97. /// @brief returns the next address from pools in a subnet
  98. ///
  99. /// @param subnet next address will be returned from pool of that subnet
  100. /// @param duid Client's DUID (ignored)
  101. /// @param hint client's hint (ignored)
  102. /// @return the next address
  103. virtual isc::asiolink::IOAddress
  104. pickAddress(const SubnetPtr& subnet,
  105. const DuidPtr& duid,
  106. const isc::asiolink::IOAddress& hint);
  107. private:
  108. /// @brief returns an address by one
  109. /// @param addr address to be increased
  110. /// @return address increased by one
  111. isc::asiolink::IOAddress increaseAddress(const isc::asiolink::IOAddress& addr);
  112. };
  113. /// @brief Address/prefix allocator that gets an address based on a hash
  114. ///
  115. /// @todo: This is a skeleton class for now and is missing implementation.
  116. class HashedAllocator : public Allocator {
  117. public:
  118. /// @brief default constructor (does nothing)
  119. /// @param type - specifies allocation type
  120. HashedAllocator(Lease::Type type);
  121. /// @brief returns an address based on hash calculated from client's DUID.
  122. ///
  123. /// @todo: Implement this method
  124. ///
  125. /// @param subnet an address will be picked from pool of that subnet
  126. /// @param duid Client's DUID
  127. /// @param hint a hint (last address that was picked)
  128. /// @return selected address
  129. virtual isc::asiolink::IOAddress pickAddress(const SubnetPtr& subnet,
  130. const DuidPtr& duid,
  131. const isc::asiolink::IOAddress& hint);
  132. };
  133. /// @brief Random allocator that picks address randomly
  134. ///
  135. /// @todo: This is a skeleton class for now and is missing implementation.
  136. class RandomAllocator : public Allocator {
  137. public:
  138. /// @brief default constructor (does nothing)
  139. /// @param type - specifies allocation type
  140. RandomAllocator(Lease::Type type);
  141. /// @brief returns an random address from pool of specified subnet
  142. ///
  143. /// @todo: Implement this method
  144. ///
  145. /// @param subnet an address will be picked from pool of that subnet
  146. /// @param duid Client's DUID (ignored)
  147. /// @param hint the last address that was picked (ignored)
  148. /// @return a random address from the pool
  149. virtual isc::asiolink::IOAddress
  150. pickAddress(const SubnetPtr& subnet, const DuidPtr& duid,
  151. const isc::asiolink::IOAddress& hint);
  152. };
  153. public:
  154. /// @brief specifies allocation type
  155. typedef enum {
  156. ALLOC_ITERATIVE, // iterative - one address after another
  157. ALLOC_HASHED, // hashed - client's DUID/client-id is hashed
  158. ALLOC_RANDOM // random - an address is randomly selected
  159. } AllocType;
  160. /// @brief Default constructor.
  161. ///
  162. /// Instantiates necessary services, required to run DHCPv6 server.
  163. /// In particular, creates IfaceMgr that will be responsible for
  164. /// network interaction. Will instantiate lease manager, and load
  165. /// old or create new DUID.
  166. ///
  167. /// @param engine_type selects allocation algorithm
  168. /// @param attempts number of attempts for each lease allocation before
  169. /// we give up (0 means unlimited)
  170. /// @param ipv6 specifies if the engine should work for IPv4 or IPv6
  171. AllocEngine(AllocType engine_type, unsigned int attempts, bool ipv6 = true);
  172. /// @brief Returns IPv4 lease.
  173. ///
  174. /// This method finds the appropriate lease for the client using the
  175. /// following algorithm:
  176. /// - If lease exists for the combination of the HW address, client id and
  177. /// subnet, try to renew a lease and return it.
  178. /// - If lease exists for the combination of the client id and subnet, try
  179. /// to renew the lease and return it.
  180. /// - If client supplied an address hint and this address is available,
  181. /// allocate the new lease with this address.
  182. /// - If client supplied an address hint and the lease for this address
  183. /// exists in the database, return this lease if it is expired.
  184. /// - Pick new address from the pool and try to allocate it for the client,
  185. /// if expired lease exists for the picked address, try to reuse this lease.
  186. ///
  187. /// When a server should do DNS updates, it is required that allocation
  188. /// returns the information how the lease was obtained by the allocation
  189. /// engine. In particular, the DHCP server should be able to check whether
  190. /// existing lease was returned, or new lease was allocated. When existing
  191. /// lease was returned, server should check whether the FQDN has changed
  192. /// between the allocation of the old and new lease. If so, server should
  193. /// perform appropriate DNS update. If not, server may choose to not
  194. /// perform the update. The information about the old lease is returned via
  195. /// @c old_lease parameter. If NULL value is returned, it is an indication
  196. /// that new lease was allocated for the client. If non-NULL value is
  197. /// returned, it is an indication that allocation engine reused/renewed an
  198. /// existing lease.
  199. ///
  200. /// @param subnet subnet the allocation should come from
  201. /// @param clientid Client identifier
  202. /// @param hwaddr Client's hardware address info
  203. /// @param hint A hint that the client provided
  204. /// @param fwd_dns_update Indicates whether forward DNS update will be
  205. /// performed for the client (true) or not (false).
  206. /// @param rev_dns_update Indicates whether reverse DNS update will be
  207. /// performed for the client (true) or not (false).
  208. /// @param hostname A string carrying hostname to be used for DNS updates.
  209. /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
  210. /// an address for DISCOVER that is not really allocated (true)
  211. /// @param callout_handle A callout handle (used in hooks). A lease callouts
  212. /// will be executed if this parameter is passed.
  213. /// @param [out] old_lease Holds the pointer to a previous instance of a
  214. /// lease. The NULL pointer indicates that lease didn't exist prior
  215. /// to calling this function (e.g. new lease has been allocated).
  216. ///
  217. /// @return Allocated IPv4 lease (or NULL if allocation failed)
  218. Lease4Ptr
  219. allocateAddress4(const SubnetPtr& subnet,
  220. const ClientIdPtr& clientid,
  221. const HWAddrPtr& hwaddr,
  222. const isc::asiolink::IOAddress& hint,
  223. const bool fwd_dns_update,
  224. const bool rev_dns_update,
  225. const std::string& hostname,
  226. bool fake_allocation,
  227. const isc::hooks::CalloutHandlePtr& callout_handle,
  228. Lease4Ptr& old_lease);
  229. /// @brief Renews a IPv4 lease
  230. ///
  231. /// Since both request and renew are implemented in DHCPv4 as the sending of
  232. /// a REQUEST packet, it is difficult to easily distinguish between those
  233. /// cases. Therefore renew for DHCPv4 is done in the allocation engine.
  234. /// This method is also used when client crashed/rebooted and tries
  235. /// to get a new lease. It thinks that it gets a new lease, but in fact
  236. /// we are only renewing the still valid lease for that client.
  237. ///
  238. /// @param subnet A subnet the client is attached to
  239. /// @param clientid Client identifier
  240. /// @param hwaddr Client's hardware address
  241. /// @param fwd_dns_update Indicates whether forward DNS update will be
  242. /// performed for the client (true) or not (false).
  243. /// @param rev_dns_update Indicates whether reverse DNS update will be
  244. /// performed for the client (true) or not (false).
  245. /// @param hostname A string carrying hostname to be used for DNS updates.
  246. /// @param lease A lease to be renewed
  247. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  248. /// will be executed if this parameter is passed.
  249. /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
  250. /// an address for DISCOVER that is not really allocated (true)
  251. Lease4Ptr
  252. renewLease4(const SubnetPtr& subnet,
  253. const ClientIdPtr& clientid,
  254. const HWAddrPtr& hwaddr,
  255. const bool fwd_dns_update,
  256. const bool rev_dns_update,
  257. const std::string& hostname,
  258. const Lease4Ptr& lease,
  259. const isc::hooks::CalloutHandlePtr& callout_handle,
  260. bool fake_allocation /* = false */);
  261. /// @brief Allocates an IPv6 lease
  262. ///
  263. /// This method uses currently selected allocator to pick an address from
  264. /// specified subnet, creates a lease for that address and then inserts
  265. /// it into LeaseMgr (if this allocation is not fake).
  266. ///
  267. /// @param subnet subnet the allocation should come from
  268. /// @param duid Client's DUID
  269. /// @param iaid iaid field from the IA_NA container that client sent
  270. /// @param hint a hint that the client provided
  271. /// @param type lease type (IA, TA or PD)
  272. /// @param fwd_dns_update A boolean value which indicates that server takes
  273. /// responsibility for the forward DNS Update for this lease
  274. /// (if true).
  275. /// @param rev_dns_update A boolean value which indicates that server takes
  276. /// responsibility for the reverse DNS Update for this lease
  277. /// (if true).
  278. /// @param hostname A fully qualified domain-name of the client.
  279. /// @param fake_allocation is this real i.e. REQUEST (false) or just picking
  280. /// an address for SOLICIT that is not really allocated (true)
  281. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  282. /// will be executed if this parameter is passed.
  283. ///
  284. /// @return Allocated IPv6 leases (may be empty if allocation failed)
  285. Lease6Collection
  286. allocateAddress6(const Subnet6Ptr& subnet,
  287. const DuidPtr& duid,
  288. uint32_t iaid,
  289. const isc::asiolink::IOAddress& hint,
  290. Lease::Type type,
  291. const bool fwd_dns_update,
  292. const bool rev_dns_update,
  293. const std::string& hostname,
  294. bool fake_allocation,
  295. const isc::hooks::CalloutHandlePtr& callout_handle);
  296. /// @brief Destructor. Used during DHCPv6 service shutdown.
  297. virtual ~AllocEngine();
  298. private:
  299. /// @brief Creates a lease and inserts it in LeaseMgr if necessary
  300. ///
  301. /// Creates a lease based on specified parameters and tries to insert it
  302. /// into the database. That may fail in some cases, e.g. when there is another
  303. /// allocation process and we lost a race to a specific lease.
  304. ///
  305. /// @param subnet Subnet the lease is allocated from
  306. /// @param clientid Client identifier
  307. /// @param hwaddr Client's hardware address
  308. /// @param addr An address that was selected and is confirmed to be available
  309. /// @param fwd_dns_update Indicates whether forward DNS update will be
  310. /// performed for the client (true) or not (false).
  311. /// @param rev_dns_update Indicates whether reverse DNS update will be
  312. /// performed for the client (true) or not (false).
  313. /// @param hostname A string carrying hostname to be used for DNS updates.
  314. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  315. /// will be executed if this parameter is passed (and there are callouts
  316. /// registered)
  317. /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
  318. /// an address for DISCOVER that is not really allocated (true)
  319. /// @return allocated lease (or NULL in the unlikely case of the lease just
  320. /// becomed unavailable)
  321. Lease4Ptr createLease4(const SubnetPtr& subnet, const DuidPtr& clientid,
  322. const HWAddrPtr& hwaddr,
  323. const isc::asiolink::IOAddress& addr,
  324. const bool fwd_dns_update,
  325. const bool rev_dns_update,
  326. const std::string& hostname,
  327. const isc::hooks::CalloutHandlePtr& callout_handle,
  328. bool fake_allocation = false);
  329. /// @brief creates a lease and inserts it in LeaseMgr if necessary
  330. ///
  331. /// Creates a lease based on specified parameters and tries to insert it
  332. /// into the database. That may fail in some cases, i.e. when there is another
  333. /// allocation process and we lost a race to a specific lease.
  334. ///
  335. /// @param subnet subnet the lease is allocated from
  336. /// @param duid client's DUID
  337. /// @param iaid IAID from the IA_NA container the client sent to us
  338. /// @param addr an address that was selected and is confirmed to be
  339. /// available
  340. /// @param type lease type (IA, TA or PD)
  341. /// @param fwd_dns_update A boolean value which indicates that server takes
  342. /// responsibility for the forward DNS Update for this lease
  343. /// (if true).
  344. /// @param rev_dns_update A boolean value which indicates that server takes
  345. /// responsibility for the reverse DNS Update for this lease
  346. /// (if true).
  347. /// @param hostname A fully qualified domain-name of the client.
  348. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  349. /// will be executed if this parameter is passed (and there are callouts
  350. /// registered)
  351. /// @param fake_allocation is this real i.e. REQUEST (false) or just picking
  352. /// an address for SOLICIT that is not really allocated (true)
  353. /// @return allocated lease (or NULL in the unlikely case of the lease just
  354. /// became unavailable)
  355. Lease6Ptr createLease6(const Subnet6Ptr& subnet, const DuidPtr& duid,
  356. uint32_t iaid, const isc::asiolink::IOAddress& addr,
  357. Lease::Type type, const bool fwd_dns_update,
  358. const bool rev_dns_update,
  359. const std::string& hostname,
  360. const isc::hooks::CalloutHandlePtr& callout_handle,
  361. bool fake_allocation = false);
  362. /// @brief Reuses expired IPv4 lease
  363. ///
  364. /// Updates existing expired lease with new information. Lease database
  365. /// is updated if this is real (i.e. REQUEST, fake_allocation = false), not
  366. /// dummy allocation request (i.e. DISCOVER, fake_allocation = true).
  367. ///
  368. /// @param expired Old, expired lease
  369. /// @param subnet Subnet the lease is allocated from
  370. /// @param clientid Client identifier
  371. /// @param hwaddr Client's hardware address
  372. /// @param fwd_dns_update Indicates whether forward DNS update will be
  373. /// performed for the client (true) or not (false).
  374. /// @param rev_dns_update Indicates whether reverse DNS update will be
  375. /// performed for the client (true) or not (false).
  376. /// @param hostname A string carrying hostname to be used for DNS updates.
  377. /// @param callout_handle A callout handle (used in hooks). A lease callouts
  378. /// will be executed if this parameter is passed.
  379. /// @param fake_allocation Is this real i.e. REQUEST (false) or just picking
  380. /// an address for DISCOVER that is not really allocated (true)
  381. /// @return refreshed lease
  382. /// @throw BadValue if trying to recycle lease that is still valid
  383. Lease4Ptr reuseExpiredLease(Lease4Ptr& expired,
  384. const SubnetPtr& subnet,
  385. const ClientIdPtr& clientid,
  386. const HWAddrPtr& hwaddr,
  387. const bool fwd_dns_update,
  388. const bool rev_dns_update,
  389. const std::string& hostname,
  390. const isc::hooks::CalloutHandlePtr& callout_handle,
  391. bool fake_allocation = false);
  392. /// @brief Reuses expired IPv6 lease
  393. ///
  394. /// Updates existing expired lease with new information. Lease database
  395. /// is updated if this is real (i.e. REQUEST, fake_allocation = false), not
  396. /// dummy allocation request (i.e. SOLICIT, fake_allocation = true).
  397. ///
  398. /// @param expired old, expired lease
  399. /// @param subnet subnet the lease is allocated from
  400. /// @param duid client's DUID
  401. /// @param iaid IAID from the IA_NA container the client sent to us
  402. /// @param fwd_dns_update A boolean value which indicates that server takes
  403. /// responsibility for the forward DNS Update for this lease
  404. /// (if true).
  405. /// @param rev_dns_update A boolean value which indicates that server takes
  406. /// responsibility for the reverse DNS Update for this lease
  407. /// (if true).
  408. /// @param hostname A fully qualified domain-name of the client.
  409. /// @param callout_handle a callout handle (used in hooks). A lease callouts
  410. /// will be executed if this parameter is passed.
  411. /// @param fake_allocation is this real i.e. REQUEST (false) or just picking
  412. /// an address for SOLICIT that is not really allocated (true)
  413. /// @return refreshed lease
  414. /// @throw BadValue if trying to recycle lease that is still valid
  415. Lease6Ptr reuseExpiredLease(Lease6Ptr& expired, const Subnet6Ptr& subnet,
  416. const DuidPtr& duid, uint32_t iaid,
  417. const bool fwd_dns_update,
  418. const bool rev_dns_update,
  419. const std::string& hostname,
  420. const isc::hooks::CalloutHandlePtr& callout_handle,
  421. bool fake_allocation = false);
  422. /// @brief a pointer to currently used allocator
  423. boost::shared_ptr<Allocator> allocator_;
  424. /// @brief number of attempts before we give up lease allocation (0=unlimited)
  425. unsigned int attempts_;
  426. // hook name indexes (used in hooks callouts)
  427. int hook_index_lease4_select_; ///< index for lease4_select hook
  428. int hook_index_lease6_select_; ///< index for lease6_select hook
  429. };
  430. }; // namespace isc::dhcp
  431. }; // namespace isc
  432. #endif // ALLOC_ENGINE_H