lease_mgr.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Copyright (C) 2012-2013, 2015 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 LEASE_MGR_H
  15. #define LEASE_MGR_H
  16. #include <asiolink/io_address.h>
  17. #include <asiolink/io_service.h>
  18. #include <dhcp/duid.h>
  19. #include <dhcp/option.h>
  20. #include <dhcp/hwaddr.h>
  21. #include <dhcpsrv/lease.h>
  22. #include <dhcpsrv/subnet.h>
  23. #include <exceptions/exceptions.h>
  24. #include <boost/noncopyable.hpp>
  25. #include <boost/shared_ptr.hpp>
  26. #include <fstream>
  27. #include <iostream>
  28. #include <map>
  29. #include <string>
  30. #include <utility>
  31. #include <vector>
  32. /// @file lease_mgr.h
  33. /// @brief An abstract API for lease database
  34. ///
  35. /// This file contains declarations of Lease4, Lease6 and LeaseMgr classes.
  36. /// They are essential components of the interface to any database backend.
  37. /// Each concrete database backend (e.g. MySQL) will define a class derived
  38. /// from LeaseMgr class.
  39. ///
  40. /// Failover considerations:
  41. /// There are no intermediate plans to implement DHCPv4 failover
  42. /// (draft-ietf-dhc-failover-12.txt). Currently (Oct. 2012) the DHCPv6 failover
  43. /// is being defined in DHC WG in IETF (draft-ietf-dhcpv6-failover-requirements,
  44. /// draft-ietf-dhcpv6-failover-design), but the work is not advanced enough
  45. /// for implementation plans yet. v4 failover requires additional parameters
  46. /// to be kept with a lease. It is likely that v6 failover will require similar
  47. /// fields. Such implementation will require database schema extension.
  48. /// We have designed a way to expand/upgrade schemas during upgrades: a database
  49. /// schema is versioned and sanity checks about required version will be done
  50. /// upon start and/or upgrade. With this mechanism in place, we can add new
  51. /// fields to the database. In particular we can use that capability to
  52. /// introduce failover related fields.
  53. ///
  54. /// However, there is another approach that can be reliably used to provide
  55. /// failover, even without the actual failover protocol implemented. As the
  56. /// first backend will use MySQL, we will be able to use Multi-Master capability
  57. /// offered by MySQL and use two separatate Kea instances connecting to the
  58. /// same database.
  59. ///
  60. /// Nevertheless, we hope to have failover protocol eventually implemented in
  61. /// the Kea.
  62. namespace isc {
  63. namespace dhcp {
  64. /// @brief Exception thrown if name of database is not specified
  65. class NoDatabaseName : public Exception {
  66. public:
  67. NoDatabaseName(const char* file, size_t line, const char* what) :
  68. isc::Exception(file, line, what) {}
  69. };
  70. /// @brief Exception thrown on failure to open database
  71. class DbOpenError : public Exception {
  72. public:
  73. DbOpenError(const char* file, size_t line, const char* what) :
  74. isc::Exception(file, line, what) {}
  75. };
  76. /// @brief Exception thrown on failure to execute a database function
  77. class DbOperationError : public Exception {
  78. public:
  79. DbOperationError(const char* file, size_t line, const char* what) :
  80. isc::Exception(file, line, what) {}
  81. };
  82. /// @brief Multiple lease records found where one expected
  83. class MultipleRecords : public Exception {
  84. public:
  85. MultipleRecords(const char* file, size_t line, const char* what) :
  86. isc::Exception(file, line, what) {}
  87. };
  88. /// @brief Attempt to update lease that was not there
  89. class NoSuchLease : public Exception {
  90. public:
  91. NoSuchLease(const char* file, size_t line, const char* what) :
  92. isc::Exception(file, line, what) {}
  93. };
  94. /// @brief Data is truncated
  95. class DataTruncated : public Exception {
  96. public:
  97. DataTruncated(const char* file, size_t line, const char* what) :
  98. isc::Exception(file, line, what) {}
  99. };
  100. /// @brief Abstract Lease Manager
  101. ///
  102. /// This is an abstract API for lease database backends. It provides unified
  103. /// interface to all backends. As this is an abstract class, it should not
  104. /// be used directly, but rather specialized derived class should be used
  105. /// instead.
  106. ///
  107. /// This class creates an instance of the @c asiolink::IOService in the
  108. /// constructor. This object is required to execute the
  109. /// @c asiolink::IntervalTimer for the operations triggered periodically
  110. /// by the lease database backends which implement @c LeaseMgr interface.
  111. /// In order to execute the timers installed by the particular backend,
  112. /// the owner of the backend (e.g. DHCP server) should retrieve the pointer
  113. /// to the @c asiolink::IOService object by calling @c LeaseMgr::getIOService
  114. /// and call the appropriate functions, e.g. @c poll_one or @c run_one in a
  115. /// main loop.
  116. ///
  117. /// This class throws no exceptions. However, methods in concrete
  118. /// implementations of this class may throw exceptions: see the documentation
  119. /// of those classes for details.
  120. class LeaseMgr {
  121. public:
  122. /// @brief Defines maximum value for time that can be reliably stored.
  123. // If I'm still alive I'll be too old to care. You fix it.
  124. static const time_t MAX_DB_TIME;
  125. /// Database configuration parameter map
  126. typedef std::map<std::string, std::string> ParameterMap;
  127. /// @brief Constructor
  128. ///
  129. /// @param parameters A data structure relating keywords and values
  130. /// concerned with the database.
  131. LeaseMgr(const ParameterMap& parameters)
  132. : parameters_(parameters), io_service_(new asiolink::IOService())
  133. {}
  134. /// @brief Destructor
  135. virtual ~LeaseMgr()
  136. {}
  137. /// @brief Class method to return extended version info
  138. /// This class method must be redeclared and redefined in derived classes
  139. static std::string getDBVersion();
  140. /// @brief Adds an IPv4 lease.
  141. ///
  142. /// @param lease lease to be added
  143. ///
  144. /// @result true if the lease was added, false if not (because a lease
  145. /// with the same address was already there).
  146. virtual bool addLease(const isc::dhcp::Lease4Ptr& lease) = 0;
  147. /// @brief Adds an IPv6 lease.
  148. ///
  149. /// @param lease lease to be added
  150. ///
  151. /// @result true if the lease was added, false if not (because a lease
  152. /// with the same address was already there).
  153. virtual bool addLease(const Lease6Ptr& lease) = 0;
  154. /// @brief Returns an IPv4 lease for specified IPv4 address
  155. ///
  156. /// This method return a lease that is associated with a given address.
  157. /// For other query types (by hardware addr, by client-id) there can be
  158. /// several leases in different subnets (e.g. for mobile clients that
  159. /// got address in different subnets). However, for a single address
  160. /// there can be only one lease, so this method returns a pointer to
  161. /// a single lease, not a container of leases.
  162. ///
  163. /// @param addr address of the searched lease
  164. ///
  165. /// @return smart pointer to the lease (or NULL if a lease is not found)
  166. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const = 0;
  167. /// @brief Returns existing IPv4 leases for specified hardware address.
  168. ///
  169. /// Although in the usual case there will be only one lease, for mobile
  170. /// clients or clients with multiple static/fixed/reserved leases there
  171. /// can be more than one. Thus return type is a container, not a single
  172. /// pointer.
  173. ///
  174. /// @param hwaddr hardware address of the client
  175. ///
  176. /// @return lease collection
  177. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const = 0;
  178. /// @brief Returns existing IPv4 leases for specified hardware address
  179. /// and a subnet
  180. ///
  181. /// There can be at most one lease for a given HW address in a single
  182. /// pool, so this method will either return a single lease or NULL.
  183. ///
  184. /// @param hwaddr hardware address of the client
  185. /// @param subnet_id identifier of the subnet that lease must belong to
  186. ///
  187. /// @return a pointer to the lease (or NULL if a lease is not found)
  188. virtual Lease4Ptr getLease4(const isc::dhcp::HWAddr& hwaddr,
  189. SubnetID subnet_id) const = 0;
  190. /// @brief Returns existing IPv4 lease for specified client-id
  191. ///
  192. /// Although in the usual case there will be only one lease, for mobile
  193. /// clients or clients with multiple static/fixed/reserved leases there
  194. /// can be more than one. Thus return type is a container, not a single
  195. /// pointer.
  196. ///
  197. /// @param clientid client identifier
  198. ///
  199. /// @return lease collection
  200. virtual Lease4Collection getLease4(const ClientId& clientid) const = 0;
  201. /// @brief Returns existing IPv4 lease for a given client identifier,
  202. /// HW address and subnet identifier.
  203. ///
  204. /// @todo Consider whether this function is needed or not. In the basic
  205. /// DHCPv4 server implementation it is not used by the allocation engine.
  206. ///
  207. /// @param client_id A client identifier.
  208. /// @param hwaddr Hardware address.
  209. /// @param subnet_id A subnet identifier.
  210. ///
  211. /// @return A pointer to the lease or NULL if the lease is not found.
  212. virtual Lease4Ptr getLease4(const ClientId& client_id, const HWAddr& hwaddr,
  213. SubnetID subnet_id) const = 0;
  214. /// @brief Returns existing IPv4 lease for specified client-id
  215. ///
  216. /// There can be at most one lease for a given client-id in a single
  217. /// pool, so this method will either return a single lease or NULL.
  218. ///
  219. /// @param clientid client identifier
  220. /// @param subnet_id identifier of the subnet that lease must belong to
  221. ///
  222. /// @return a pointer to the lease (or NULL if a lease is not found)
  223. virtual Lease4Ptr getLease4(const ClientId& clientid,
  224. SubnetID subnet_id) const = 0;
  225. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  226. ///
  227. /// For a given address, we assume that there will be only one lease.
  228. /// The assumption here is that there will not be site or link-local
  229. /// addresses used, so there is no way of having address duplication.
  230. ///
  231. /// @param type specifies lease type: (NA, TA or PD)
  232. /// @param addr address of the searched lease
  233. ///
  234. /// @return smart pointer to the lease (or NULL if a lease is not found)
  235. virtual Lease6Ptr getLease6(Lease::Type type,
  236. const isc::asiolink::IOAddress& addr) const = 0;
  237. /// @brief Returns existing IPv6 leases for a given DUID+IA combination
  238. ///
  239. /// Although in the usual case there will be only one lease, for mobile
  240. /// clients or clients with multiple static/fixed/reserved leases there
  241. /// can be more than one. Thus return type is a container, not a single
  242. /// pointer.
  243. ///
  244. /// @param type specifies lease type: (NA, TA or PD)
  245. /// @param duid client DUID
  246. /// @param iaid IA identifier
  247. ///
  248. /// @return Lease collection (may be empty if no lease is found)
  249. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  250. uint32_t iaid) const = 0;
  251. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  252. ///
  253. /// There may be more than one address, temp. address or prefix
  254. /// for specified duid/iaid/subnet-id tuple.
  255. ///
  256. /// @param type specifies lease type: (NA, TA or PD)
  257. /// @param duid client DUID
  258. /// @param iaid IA identifier
  259. /// @param subnet_id subnet id of the subnet the lease belongs to
  260. ///
  261. /// @return Lease collection (may be empty if no lease is found)
  262. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  263. uint32_t iaid, SubnetID subnet_id) const = 0;
  264. /// @brief returns zero or one IPv6 lease for a given duid+iaid+subnet_id
  265. ///
  266. /// This function is mostly intended to be used in unit-tests during the
  267. /// transition from single to multi address per IA. It may also be used
  268. /// in other cases where at most one lease is expected in the database.
  269. ///
  270. /// It is a wrapper around getLeases6(), which returns a collection of
  271. /// leases. That collection can be converted into a single pointer if
  272. /// there are no leases (NULL pointer) or one lease (use that single lease).
  273. /// If there are more leases in the collection, the function will
  274. /// throw MultipleRecords exception.
  275. ///
  276. /// Note: This method is not virtual on purpose. It is common for all
  277. /// backends.
  278. ///
  279. /// @param type specifies lease type: (NA, TA or PD)
  280. /// @param duid client DUID
  281. /// @param iaid IA identifier
  282. /// @param subnet_id subnet id of the subnet the lease belongs to
  283. ///
  284. /// @throw MultipleRecords if there is more than one lease matching
  285. ///
  286. /// @return Lease pointer (or NULL if none is found)
  287. Lease6Ptr getLease6(Lease::Type type, const DUID& duid,
  288. uint32_t iaid, SubnetID subnet_id) const;
  289. /// @brief Returns a collection of expired DHCPv6 leases.
  290. ///
  291. /// This method returns at most @c max_leases expired leases. The leases
  292. /// returned haven't been reclaimed, i.e. the database query must exclude
  293. /// reclaimed leases from the results returned.
  294. ///
  295. /// @param [out] expired_leases A container to which expired leases returned
  296. /// by the database backend are added.
  297. /// @param max_leases A maximum number of leases to be returned. If this
  298. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  299. virtual void getExpiredLeases6(Lease6Collection& expired_leases,
  300. const size_t max_leases) const = 0;
  301. /// @brief Returns a collection of expired DHCPv4 leases.
  302. ///
  303. /// This method returns at most @c max_leases expired leases. The leases
  304. /// returned haven't been reclaimed, i.e. the database query must exclude
  305. /// reclaimed leases from the results returned.
  306. ///
  307. /// @param [out] expired_leases A container to which expired leases returned
  308. /// by the database backend are added.
  309. /// @param max_leases A maximum number of leases to be returned. If this
  310. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  311. virtual void getExpiredLeases4(Lease4Collection& expired_leases,
  312. const size_t max_leases) const = 0;
  313. /// @brief Updates IPv4 lease.
  314. ///
  315. /// @param lease4 The lease to be updated.
  316. ///
  317. /// If no such lease is present, an exception will be thrown.
  318. virtual void updateLease4(const Lease4Ptr& lease4) = 0;
  319. /// @brief Updates IPv6 lease.
  320. ///
  321. /// @param lease6 The lease to be updated.
  322. virtual void updateLease6(const Lease6Ptr& lease6) = 0;
  323. /// @brief Deletes a lease.
  324. ///
  325. /// @param addr Address of the lease to be deleted. (This can be IPv4 or
  326. /// IPv6.)
  327. ///
  328. /// @return true if deletion was successful, false if no such lease exists
  329. virtual bool deleteLease(const isc::asiolink::IOAddress& addr) = 0;
  330. /// @brief Deletes all expired and reclaimed DHCPv4 leases.
  331. ///
  332. /// @param secs Number of seconds since expiration of leases before
  333. /// they can be removed. Leases which have expired later than this
  334. /// time will not be deleted.
  335. ///
  336. /// @return Number of leases deleted.
  337. virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs) = 0;
  338. /// @brief Deletes all expired and reclaimed DHCPv6 leases.
  339. ///
  340. /// @param secs Number of seconds since expiration of leases before
  341. /// they can be removed. Leases which have expired later than this
  342. /// time will not be deleted.
  343. ///
  344. /// @return Number of leases deleted.
  345. virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs) = 0;
  346. /// @brief Return backend type
  347. ///
  348. /// Returns the type of the backend (e.g. "mysql", "memfile" etc.)
  349. ///
  350. /// @return Type of the backend.
  351. virtual std::string getType() const = 0;
  352. /// @brief Returns backend name.
  353. ///
  354. /// If the backend is a database, this is the name of the database or the
  355. /// file. Otherwise it is just the same as the type.
  356. ///
  357. /// @return Name of the backend.
  358. virtual std::string getName() const = 0;
  359. /// @brief Returns description of the backend.
  360. ///
  361. /// This description may be multiline text that describes the backend.
  362. ///
  363. /// @return Description of the backend.
  364. virtual std::string getDescription() const = 0;
  365. /// @brief Returns backend version.
  366. ///
  367. /// @return Version number as a pair of unsigned integers. "first" is the
  368. /// major version number, "second" the minor number.
  369. ///
  370. /// @todo: We will need to implement 3 version functions eventually:
  371. /// A. abstract API version
  372. /// B. backend version
  373. /// C. database version (stored in the database scheme)
  374. ///
  375. /// and then check that:
  376. /// B>=A and B=C (it is ok to have newer backend, as it should be backward
  377. /// compatible)
  378. /// Also if B>C, some database upgrade procedure may be triggered
  379. virtual std::pair<uint32_t, uint32_t> getVersion() const = 0;
  380. /// @brief Commit Transactions
  381. ///
  382. /// Commits all pending database operations. On databases that don't
  383. /// support transactions, this is a no-op.
  384. virtual void commit() = 0;
  385. /// @brief Rollback Transactions
  386. ///
  387. /// Rolls back all pending database operations. On databases that don't
  388. /// support transactions, this is a no-op.
  389. virtual void rollback() = 0;
  390. /// @todo: Add host management here
  391. /// As host reservation is outside of scope for 2012, support for hosts
  392. /// is currently postponed.
  393. /// @brief returns value of the parameter
  394. virtual std::string getParameter(const std::string& name) const;
  395. /// @brief Returns the interval at which the @c IOService events should
  396. /// be released.
  397. ///
  398. /// The implementations of this class may install the timers which
  399. /// periodically trigger event handlers defined for them. Depending
  400. /// on the intervals specified for these timers the @c IOService::poll,
  401. /// @c IOService::run etc. have to be executed to allow the timers
  402. /// for checking whether they have already expired and the handler
  403. /// must be executed. Running the @c IOService with a lower interval
  404. /// would cause the desynchronization of timers with the clock.
  405. ///
  406. /// @return A maximum interval in seconds at which the @c IOService
  407. /// should be executed. A value of 0 means that no timers are installed
  408. /// and that there is no requirement for the @c IOService to be
  409. /// executed at any specific interval.
  410. virtual uint32_t getIOServiceExecInterval() const {
  411. return (0);
  412. }
  413. /// @brief Returns a reference to the @c IOService object used
  414. /// by the Lease Manager.
  415. const asiolink::IOServicePtr& getIOService() const {
  416. return (io_service_);
  417. }
  418. private:
  419. /// @brief list of parameters passed in dbconfig
  420. ///
  421. /// That will be mostly used for storing database name, username,
  422. /// password and other parameters required for DB access. It is not
  423. /// intended to keep any DHCP-related parameters.
  424. ParameterMap parameters_;
  425. /// @brief Pointer to the IO service object used by the derived classes
  426. /// to trigger interval timers.
  427. asiolink::IOServicePtr io_service_;
  428. };
  429. }; // end of isc::dhcp namespace
  430. }; // end of isc namespace
  431. #endif // LEASE_MGR_H