memfile_lease_mgr.h 28 KB

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