memfile_lease_mgr.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. // Copyright (C) 2012-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 MEMFILE_LEASE_MGR_H
  15. #define MEMFILE_LEASE_MGR_H
  16. #include <asiolink/interval_timer.h>
  17. #include <dhcp/hwaddr.h>
  18. #include <dhcpsrv/csv_lease_file4.h>
  19. #include <dhcpsrv/csv_lease_file6.h>
  20. #include <dhcpsrv/memfile_lease_storage.h>
  21. #include <dhcpsrv/lease_mgr.h>
  22. #include <util/process_spawn.h>
  23. #include <boost/scoped_ptr.hpp>
  24. #include <boost/shared_ptr.hpp>
  25. namespace isc {
  26. namespace dhcp {
  27. class LFCSetup;
  28. /// @brief Concrete implementation of a lease database backend using flat file.
  29. ///
  30. /// This class implements a lease database backend using CSV files to store
  31. /// DHCPv4 and DHCPv6 leases on disk. The format of the files is determined
  32. /// by the @c CSVLeaseFile4 and @c CSVLeaseFile6 classes.
  33. ///
  34. /// In order to obtain good performance, the backend stores leases
  35. /// incrementally, i.e. updates to leases are appended at the end of the lease
  36. /// file. To record the deletion of a lease, the lease record is appended to
  37. /// the lease file with the valid lifetime set to 0. However, this may result
  38. /// in a significant growth of the lease file size over time, because the lease
  39. /// file will contain many entries for each lease. In order to mitigate this
  40. /// problem, the backend implements the Lease File Cleanup mechanism which is
  41. /// described on the Kea wiki: http://kea.isc.org/wiki/LFCDesign.
  42. ///
  43. /// The backend installs an @c asiolink::IntervalTimer to periodically execute
  44. /// the @c Memfile_LeaseMgr::lfcCallback. This callback function controls
  45. /// the startup of the background process which removes redundant information
  46. /// from the lease file(s).
  47. ///
  48. /// When the backend is starting up, it reads leases from the lease file (one
  49. /// by one) and adds them to the in-memory container as follows:
  50. /// - if the lease record being parsed identifies a lease which is not present
  51. /// in the container, and the lease has valid lifetime greater than 0,
  52. /// the lease is added to the container,
  53. /// - if the lease record being parsed identifies a lease which is present in
  54. /// the container, and the valid lifetime of the lease record being parsed is
  55. /// greater than 0, the lease in the container is updated
  56. /// - if the lease record being parsed has valid lifetime equal to 0, and the
  57. /// corresponding lease exists in the container, the lease is removed from
  58. /// the container.
  59. ///
  60. /// After the container holding leases is initialized, each subsequent update,
  61. /// removal or addition of the lease is appended to the lease file
  62. /// synchronously.
  63. ///
  64. /// Originally, the Memfile backend didn't write leases to disk. This was
  65. /// particularly useful for testing server performance in non-disk bound
  66. /// conditions. In order to preserve this capability, the new parameter
  67. /// "persist=true|false" has been introduced in the database access string.
  68. /// For example, database access string: "type=memfile persist=true"
  69. /// enables writes of leases to a disk.
  70. ///
  71. /// The lease file locations can be specified with the "name=[path]"
  72. /// parameter in the database access string. The [path] is the
  73. /// absolute path to the file (including file name). If this parameter
  74. /// is not specified, the default location in the installation
  75. /// directory is used: var/kea/kea-leases4.csv and
  76. /// var/kea/kea-leases6.csv.
  77. class Memfile_LeaseMgr : public LeaseMgr {
  78. public:
  79. /// @defgroup versions Specified memfile backend version.
  80. ///
  81. /// @brief Defines major version of the memfile backend.
  82. ///
  83. /// Version history:
  84. /// 1.0 - initial version (released in Kea 0.9)
  85. /// 2.0 - hwaddr column added (to be released in Kea 0.9.1)
  86. ///
  87. /// @{
  88. static const int MAJOR_VERSION = 2;
  89. /// Defines minor version of the memfile backend.
  90. static const int MINOR_VERSION = 0;
  91. /// @}
  92. /// @brief Specifies universe (V4, V6)
  93. ///
  94. /// This enumeration is used by various functions in Memfile %Lease Manager,
  95. /// to identify the lease type referred to. In particular, it is used by
  96. /// functions operating on the lease files to distinguish between lease
  97. /// files for DHCPv4 and DHCPv6.
  98. enum Universe {
  99. V4,
  100. V6
  101. };
  102. /// @name Methods implementing the API of the lease database backend.
  103. /// The following methods are implementing the API of the
  104. /// @c LeaseMgr to manage leases.
  105. //@{
  106. /// @brief The sole lease manager constructor
  107. ///
  108. /// dbconfig is a generic way of passing parameters. Parameters
  109. /// are passed in the "name=value" format, separated by spaces.
  110. /// Values may be enclosed in double quotes, if needed.
  111. ///
  112. /// @param parameters A data structure relating keywords and values
  113. /// concerned with the database.
  114. Memfile_LeaseMgr(const ParameterMap& parameters);
  115. /// @brief Destructor (closes file)
  116. virtual ~Memfile_LeaseMgr();
  117. /// @brief Local version of getDBVersion() class method
  118. static std::string getDBVersion();
  119. /// @brief Adds an IPv4 lease.
  120. ///
  121. /// @param lease lease to be added
  122. virtual bool addLease(const Lease4Ptr& lease);
  123. /// @brief Adds an IPv6 lease.
  124. ///
  125. /// @param lease lease to be added
  126. virtual bool addLease(const Lease6Ptr& lease);
  127. /// @brief Returns existing IPv4 lease for specified IPv4 address.
  128. ///
  129. /// This function returns a copy of the lease. The modification in the
  130. /// return lease does not affect the instance held in the lease storage.
  131. ///
  132. /// @param addr An address of the searched lease.
  133. ///
  134. /// @return a collection of leases
  135. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  136. /// @brief Returns existing IPv4 leases for specified hardware address.
  137. ///
  138. /// Although in the usual case there will be only one lease, for mobile
  139. /// clients or clients with multiple static/fixed/reserved leases there
  140. /// can be more than one. Thus return type is a container, not a single
  141. /// pointer.
  142. ///
  143. /// @param hwaddr hardware address of the client
  144. ///
  145. /// @return lease collection
  146. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
  147. /// @brief Returns existing IPv4 lease for specified hardware address
  148. /// and a subnet
  149. ///
  150. /// This function returns a copy of the lease. The modification in the
  151. /// return lease does not affect the instance held in the lease storage.
  152. ///
  153. /// There can be at most one lease for a given HW address in a single
  154. /// pool, so this method with either return a single lease or NULL.
  155. ///
  156. /// @param hwaddr hardware address of the client
  157. /// @param subnet_id identifier of the subnet that lease must belong to
  158. ///
  159. /// @return a pointer to the lease (or NULL if a lease is not found)
  160. virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
  161. SubnetID subnet_id) const;
  162. /// @brief Returns existing IPv4 lease for specified client-id
  163. ///
  164. /// @param client_id client identifier
  165. virtual Lease4Collection getLease4(const ClientId& client_id) const;
  166. /// @brief Returns IPv4 lease for specified client-id/hwaddr/subnet-id tuple
  167. ///
  168. /// There can be at most one lease for a given client-id/hwaddr tuple
  169. /// in a single pool, so this method with either return a single lease
  170. /// or NULL.
  171. ///
  172. /// @param clientid client identifier
  173. /// @param hwaddr hardware address of the client
  174. /// @param subnet_id identifier of the subnet that lease must belong to
  175. ///
  176. /// @return a pointer to the lease (or NULL if a lease is not found)
  177. virtual Lease4Ptr getLease4(const ClientId& clientid,
  178. const HWAddr& hwaddr,
  179. SubnetID subnet_id) const;
  180. /// @brief Returns existing IPv4 lease for specified client-id
  181. ///
  182. /// This function returns a copy of the lease. The modification in the
  183. /// return lease does not affect the instance held in the lease storage.
  184. ///
  185. /// There can be at most one lease for a given HW address in a single
  186. /// pool, so this method with either return a single lease or NULL.
  187. ///
  188. /// @param clientid client identifier
  189. /// @param subnet_id identifier of the subnet that lease must belong to
  190. ///
  191. /// @return a pointer to the lease (or NULL if a lease is not found)
  192. virtual Lease4Ptr getLease4(const ClientId& clientid,
  193. SubnetID subnet_id) const;
  194. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  195. ///
  196. /// This function returns a copy of the lease. The modification in the
  197. /// return lease does not affect the instance held in the lease storage.
  198. ///
  199. /// @param type specifies lease type: (NA, TA or PD)
  200. /// @param addr An address of the searched lease.
  201. ///
  202. /// @return smart pointer to the lease (or NULL if a lease is not found)
  203. virtual Lease6Ptr getLease6(Lease::Type type,
  204. const isc::asiolink::IOAddress& addr) const;
  205. /// @brief Returns existing IPv6 lease for a given DUID + IA + lease type
  206. /// combination
  207. ///
  208. /// @param type specifies lease type: (NA, TA or PD)
  209. /// @param duid client DUID
  210. /// @param iaid IA identifier
  211. ///
  212. /// @return collection of IPv6 leases
  213. virtual Lease6Collection getLeases6(Lease::Type type,
  214. const DUID& duid, uint32_t iaid) const;
  215. /// @brief Returns existing IPv6 lease for a given DUID + IA + subnet-id +
  216. /// lease type combination.
  217. ///
  218. /// This function returns a copy of the lease. The modification in the
  219. /// return lease does not affect the instance held in the lease storage.
  220. ///
  221. /// @param type specifies lease type: (NA, TA or PD)
  222. /// @param duid client DUID
  223. /// @param iaid IA identifier
  224. /// @param subnet_id identifier of the subnet the lease must belong to
  225. ///
  226. /// @return lease collection (may be empty if no lease is found)
  227. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  228. uint32_t iaid,
  229. SubnetID subnet_id) const;
  230. /// @brief Returns a collection of expired DHCPv6 leases.
  231. ///
  232. /// This method returns at most @c max_leases expired leases. The leases
  233. /// returned haven't been reclaimed, i.e. the database query must exclude
  234. /// reclaimed leases from the results returned.
  235. ///
  236. /// @param [out] expired_leases A container to which expired leases returned
  237. /// by the database backend are added.
  238. /// @param max_leases A maximum number of leases to be returned. If this
  239. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  240. virtual void getExpiredLeases6(Lease6Collection& expired_leases,
  241. const size_t max_leases) const;
  242. /// @brief Returns a collection of expired DHCPv4 leases.
  243. ///
  244. /// This method returns at most @c max_leases expired leases. The leases
  245. /// returned haven't been reclaimed, i.e. the database query must exclude
  246. /// reclaimed leases from the results returned.
  247. ///
  248. /// @param [out] expired_leases A container to which expired leases returned
  249. /// by the database backend are added.
  250. /// @param max_leases A maximum number of leases to be returned. If this
  251. /// value is set to 0, all expired (but not reclaimed) leases are returned.
  252. virtual void getExpiredLeases4(Lease4Collection& expired_leases,
  253. const size_t max_leases) const;
  254. /// @brief Updates IPv4 lease.
  255. ///
  256. /// @warning This function does not validate the pointer to the lease.
  257. /// It is caller's responsibility to pass the valid pointer.
  258. ///
  259. /// @param lease4 The lease to be updated.
  260. ///
  261. /// If no such lease is present, an exception will be thrown.
  262. virtual void updateLease4(const Lease4Ptr& lease4);
  263. /// @brief Updates IPv6 lease.
  264. ///
  265. /// @warning This function does not validate the pointer to the lease.
  266. /// It is caller's responsibility to pass the valid pointer.
  267. ///
  268. /// @param lease6 The lease to be updated.
  269. ///
  270. /// If no such lease is present, an exception will be thrown.
  271. virtual void updateLease6(const Lease6Ptr& lease6);
  272. /// @brief Deletes a lease.
  273. ///
  274. /// @param addr Address of the lease to be deleted. (This can be IPv4 or
  275. /// IPv6.)
  276. ///
  277. /// @return true if deletion was successful, false if no such lease exists
  278. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  279. /// @brief Deletes all expired-reclaimed DHCPv4 leases.
  280. ///
  281. /// @param secs Number of seconds since expiration of leases before
  282. /// they can be removed. Leases which have expired later than this
  283. /// time will not be deleted.
  284. ///
  285. /// @return Number of leases deleted.
  286. virtual uint64_t deleteExpiredReclaimedLeases4(const uint32_t secs);
  287. /// @brief Deletes all expired-reclaimed DHCPv6 leases.
  288. ///
  289. /// @param secs Number of seconds since expiration of leases before
  290. /// they can be removed. Leases which have expired later than this
  291. /// time will not be deleted.
  292. ///
  293. /// @return Number of leases deleted.
  294. virtual uint64_t deleteExpiredReclaimedLeases6(const uint32_t secs);
  295. private:
  296. /// @brief Deletes all expired-reclaimed leases.
  297. ///
  298. /// This private method is called by both of the public methods:
  299. /// @c deleteExpiredReclaimedLeases4 and
  300. /// @c deleteExpiredReclaimedLeases6 to remove all expired
  301. /// reclaimed DHCPv4 or DHCPv6 leases respectively.
  302. ///
  303. /// @param secs Number of seconds since expiration of leases before
  304. /// they can be removed. Leases which have expired later than this
  305. /// time will not be deleted.
  306. /// @param universe V4 or V6.
  307. /// @param storage Reference to the container where leases are held.
  308. /// Some expired-reclaimed leases will be removed from this container.
  309. /// @param lease_file Reference to a DHCPv4 or DHCPv6 lease file
  310. /// instance where leases should be marked as deleted.
  311. ///
  312. /// @return Number of leases deleted.
  313. ///
  314. /// @tparam IndexType Index type to be used to search for the
  315. /// expired-reclaimed leases, i.e.
  316. /// @c Lease4StorageExpirationIndex or @c Lease6StorageExpirationIndex.
  317. /// @tparam LeaseType Lease type, i.e. @c Lease4 or @c Lease6.
  318. /// @tparam StorageType Type of storage where leases are held, i.e.
  319. /// @c Lease4Storage or @c Lease6Storage.
  320. /// @tparam LeaseFileType Type of the lease file, i.e. DHCPv4 or
  321. /// DHCPv6 lease file type.
  322. template<typename IndexType, typename LeaseType, typename StorageType,
  323. typename LeaseFileType>
  324. uint64_t deleteExpiredReclaimedLeases(const uint32_t secs,
  325. const Universe& universe,
  326. StorageType& storage,
  327. LeaseFileType& lease_file) const;
  328. public:
  329. /// @brief Return backend type
  330. ///
  331. /// Returns the type of the backend.
  332. ///
  333. /// @return Type of the backend.
  334. virtual std::string getType() const {
  335. return (std::string("memfile"));
  336. }
  337. /// @brief Returns backend name.
  338. ///
  339. /// For now, memfile can only store data in memory.
  340. ///
  341. /// @return Name of the backend.
  342. virtual std::string getName() const {
  343. return ("memory");
  344. }
  345. /// @brief Returns description of the backend.
  346. ///
  347. /// This description may be multiline text that describes the backend.
  348. ///
  349. /// @return Description of the backend.
  350. virtual std::string getDescription() const;
  351. /// @brief Returns backend version.
  352. ///
  353. /// @return Version number as a pair of unsigned integers. "first" is the
  354. /// major version number, "second" the minor number.
  355. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  356. return (std::make_pair(MAJOR_VERSION, MINOR_VERSION));
  357. }
  358. /// @brief Commit Transactions
  359. ///
  360. /// Commits all pending database operations. On databases that don't
  361. /// support transactions, this is a no-op.
  362. virtual void commit();
  363. /// @brief Rollback Transactions
  364. ///
  365. /// Rolls back all pending database operations. On databases that don't
  366. /// support transactions, this is a no-op.
  367. virtual void rollback();
  368. //@}
  369. /// @name Public type and method used to determine file names for LFC.
  370. //@{
  371. /// @brief Types of the lease files used by the %Lease File Cleanup.
  372. ///
  373. /// This enumeration is used by a method which appends the appropriate
  374. /// suffix to the lease file name.
  375. enum LFCFileType {
  376. FILE_CURRENT, ///< %Lease File
  377. FILE_INPUT, ///< %Lease File Copy
  378. FILE_PREVIOUS, ///< Previous %Lease File
  379. FILE_OUTPUT, ///< LFC Output File
  380. FILE_FINISH, ///< LFC Finish File
  381. FILE_PID ///< PID File
  382. };
  383. /// @brief Appends appropriate suffix to the file name.
  384. ///
  385. /// The suffix is selected using the LFC file type specified as a
  386. /// parameter. Each file type uses a unique suffix or no suffix:
  387. /// - Current File: no suffix
  388. /// - %Lease File Copy or Input File: ".1"
  389. /// - Previous File: ".2"
  390. /// - LFC Output File: ".output"
  391. /// - LFC Finish File: ".completed"
  392. /// - LFC PID File: ".pid"
  393. ///
  394. /// See http://kea.isc.org/wiki/LFCDesign for details.
  395. ///
  396. /// @param file_name A base file name to which suffix is appended.
  397. /// @param file_type An LFC file type.
  398. /// @return A lease file name with a suffix appended.
  399. static std::string appendSuffix(const std::string& file_name,
  400. const LFCFileType& file_type);
  401. //@}
  402. /// @name Miscellaneous public convenience methods.
  403. /// The following methods allow for retrieving useful information
  404. /// about the state of the backend.
  405. //@{
  406. /// @brief Returns default path to the lease file.
  407. ///
  408. /// @param u Universe (V4 or V6).
  409. std::string getDefaultLeaseFilePath(Universe u) const;
  410. /// @brief Returns an absolute path to the lease file.
  411. ///
  412. /// @param u Universe (V4 or V6).
  413. ///
  414. /// @return Absolute path to the lease file or empty string if no lease
  415. /// file is used.
  416. std::string getLeaseFilePath(Universe u) const;
  417. /// @brief Specifies whether or not leases are written to disk.
  418. ///
  419. /// It is possible that leases for DHCPv4 are written to disk whereas leases
  420. /// for DHCPv6 are not; or vice versa. The argument of the method specifies
  421. /// the type of lease in that respect.
  422. ///
  423. /// @param u Universe (V4 or V6).
  424. ///
  425. /// @return true if leases are written to lease file; if false is
  426. /// returned, leases will be held in memory and will be lost upon
  427. /// server shut down.
  428. bool persistLeases(Universe u) const;
  429. //@}
  430. private:
  431. /// @brief Initialize the location of the lease file.
  432. ///
  433. /// This method uses the parameters passed as a map to the constructor to
  434. /// initialize the location of the lease file. If the lease file is not
  435. /// specified, the method will use the default location for the universe
  436. /// (v4 or v6) selected. If the location is specified in the map as empty
  437. /// or the "persist" parameter is set to "no" it will set the empty
  438. /// location, which implies that leases belonging to the specified universe
  439. /// will not be written to disk.
  440. ///
  441. /// @param u Universe (v4 or v6)
  442. ///
  443. /// @return The location of the lease file that should be assigned to the
  444. /// lease_file4_ or lease_file6_, depending on the universe specified as an
  445. /// argument to this function.
  446. std::string initLeaseFilePath(Universe u);
  447. /// @brief Load leases from the persistent storage.
  448. ///
  449. /// This method loads DHCPv4 or DHCPv6 leases from lease files in the
  450. /// following order:
  451. /// - If the <filename>.completed doesn't exist:
  452. /// - leases from the <filename>.2
  453. /// - leases from the <filename>.1
  454. /// - leases from the <filename>
  455. /// - else
  456. /// - leases from the <filename>.completed
  457. /// - leases from the <filename>
  458. ///
  459. /// If any of the files doesn't exist the method proceeds to reading
  460. /// leases from the subsequent file. If the <filename> doesn't exist
  461. /// it is created.
  462. ///
  463. /// When the method successfully reads leases from the files, it leaves
  464. /// the file <filename> open and its internal pointer is set to the
  465. /// end of file. The server will append lease entries to this file as
  466. /// a result of processing new messages from the clients.
  467. ///
  468. /// The <filename>.2, <filename>.1 and <filename>.completed are the
  469. /// products of the lease file cleanups (LFC).
  470. /// See: http://kea.isc.org/wiki/LFCDesign for details.
  471. ///
  472. /// @note: When the server starts up or is reconfigured it will try to
  473. /// read leases from the lease files using this method. It is possible
  474. /// that the %Lease File Cleanup is performed upon the lease files to
  475. /// be read by this method. This may result in conflicts between the
  476. /// server process and the LFC. To prevent it, the method checks if the
  477. /// instance of the @c kea-lfc is running (using the PID file) before it
  478. /// tries to load leases from the lease files. If it finds that there
  479. /// is an LFC in progress, it throws an exception which will result
  480. /// in the server refuse to start or reconfigure. When the administrator
  481. /// retries starting up or reconfiguring the server it will most likely
  482. /// be successful as the LFC should be complete by that time.
  483. ///
  484. /// @todo Consider implementing delaying the lease files loading when
  485. /// the LFC is in progress by the specified amount of time.
  486. ///
  487. /// @param filename Name of the lease file.
  488. /// @param lease_file An object representing a lease file to which
  489. /// the server will store lease updates.
  490. /// @param storage A storage for leases read from the lease file.
  491. /// @tparam LeaseObjectType @c Lease4 or @c Lease6.
  492. /// @tparam LeaseFileType @c CSVLeaseFile4 or @c CSVLeaseFile6.
  493. /// @tparam StorageType @c Lease4Storage or @c Lease6Storage.
  494. ///
  495. /// @throw CSVFileError when parsing any of the lease files fails.
  496. /// @throw DbOpenError when it is found that the LFC is in progress.
  497. template<typename LeaseObjectType, typename LeaseFileType,
  498. typename StorageType>
  499. void loadLeasesFromFiles(const std::string& filename,
  500. boost::shared_ptr<LeaseFileType>& lease_file,
  501. StorageType& storage);
  502. /// @brief stores IPv4 leases
  503. Lease4Storage storage4_;
  504. /// @brief stores IPv6 leases
  505. Lease6Storage storage6_;
  506. /// @brief Holds the pointer to the DHCPv4 lease file IO.
  507. boost::shared_ptr<CSVLeaseFile4> lease_file4_;
  508. /// @brief Holds the pointer to the DHCPv6 lease file IO.
  509. boost::shared_ptr<CSVLeaseFile6> lease_file6_;
  510. public:
  511. /// @name Public methods to retrieve information about the LFC process state.
  512. /// These methods are meant to be used by unit tests to retrieve the
  513. /// state of the spawned LFC process before validating the result of
  514. /// the lease file cleanup.
  515. //@{
  516. /// @brief Checks if the process performing lease file cleanup is running.
  517. ///
  518. /// @return true if the process performing lease file cleanup is running.
  519. bool isLFCRunning() const;
  520. /// @brief Returns the status code returned by the last executed
  521. /// LFC process.
  522. int getLFCExitStatus() const;
  523. //@}
  524. /// @name Protected methods used for %Lease File Cleanup.
  525. /// The following methods are protected so as they can be accessed and
  526. /// tested by unit tests.
  527. //@{
  528. protected:
  529. /// @brief A callback function triggering %Lease File Cleanup (LFC).
  530. ///
  531. /// This method is executed periodically to start the lease file cleanup.
  532. /// It checks whether the file is a DHCPv4 lease file or DHCPv6 lease file
  533. /// and executes the @c Memfile_LeaseMgr::lfcExecute private method
  534. /// with the appropriate parameters.
  535. ///
  536. /// This method is virtual so as it can be overridden and customized in
  537. /// the unit tests. In particular, the unit test which checks that the
  538. /// callback function has been executed would override this function
  539. /// to increase the execution counter each time it is executed.
  540. virtual void lfcCallback();
  541. //@}
  542. /// @name Private methods and members used for %Lease File Cleanup.
  543. //@{
  544. private:
  545. /// @brief Setup the periodic %Lease File Cleanup.
  546. ///
  547. /// This method checks if the @c lfc-interval configuration parameter
  548. /// is set to a non-zero value and sets up the interval timer to
  549. /// perform the %Lease File Cleanup periodically. It also prepares the
  550. /// path and arguments for the @c kea-lfc application which will be
  551. /// executed to perform the cleanup. By default the backend will use
  552. /// the path to the kea-lfc in the Kea installation directory. If
  553. /// the unit tests need to override this path (with the path in the
  554. /// Kea build directory, the @c KEA_LFC_EXECUTABLE environmental
  555. /// variable should be set to hold an absolute path to the kea-lfc
  556. /// excutable.
  557. void lfcSetup();
  558. /// @brief Performs a lease file cleanup for DHCPv4 or DHCPv6.
  559. ///
  560. /// This method performs all the actions necessary to prepare for the
  561. /// execution of the LFC and if these actions are successful, it executes
  562. /// the @c kea-lfc application as a background process to process (cleanup)
  563. /// the lease files.
  564. ///
  565. /// For the design and the terminology used in this description refer to
  566. /// the http://kea.isc.org/wiki/LFCDesign.
  567. ///
  568. /// If the method finds that the %Lease File Copy exists it simply runs
  569. /// the @c kea-lfc application.
  570. ///
  571. /// If the %Lease File Copy doesn't exist it moves the Current %Lease File
  572. /// to Lease File Copy, and then recreates the Current Lease File without
  573. /// any lease entries. If the file has been successfully moved, it runs
  574. /// the @c kea-lfc application.
  575. ///
  576. /// @param lease_file A pointer to the object representing the Current
  577. /// %Lease File (DHCPv4 or DHCPv6 lease file).
  578. ///
  579. /// @tparam LeaseFileType One of @c CSVLeaseFile4 or @c CSVLeaseFile6.
  580. template<typename LeaseFileType>
  581. void lfcExecute(boost::shared_ptr<LeaseFileType>& lease_file);
  582. /// @brief A pointer to the Lease File Cleanup configuration.
  583. boost::scoped_ptr<LFCSetup> lfc_setup_;
  584. //@}
  585. };
  586. }; // end of isc::dhcp namespace
  587. }; // end of isc namespace
  588. #endif // MEMFILE_LEASE_MGR_H