memfile_lease_mgr.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Copyright (C) 2012-2014 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 <dhcp/hwaddr.h>
  17. #include <dhcpsrv/csv_lease_file4.h>
  18. #include <dhcpsrv/csv_lease_file6.h>
  19. #include <dhcpsrv/lease_mgr.h>
  20. #include <boost/multi_index/indexed_by.hpp>
  21. #include <boost/multi_index/member.hpp>
  22. #include <boost/multi_index/ordered_index.hpp>
  23. #include <boost/multi_index_container.hpp>
  24. #include <boost/multi_index/composite_key.hpp>
  25. namespace isc {
  26. namespace dhcp {
  27. /// @brief Concrete implementation of a lease database backend using flat file.
  28. ///
  29. /// This class implements a lease database backend using CSV files to store
  30. /// DHCPv4 and DHCPv6 leases on disk. The format of the files is determined
  31. /// by the @c CSVLeaseFile4 and @c CSVLeaseFile6 classes.
  32. ///
  33. /// The backend stores leases incrementally, i.e. updates to leases are appended
  34. /// at the end of the lease file. When leases is to be deleted, the lease
  35. /// record is appended to the lease file, with valid lifetime set to 0.
  36. ///
  37. /// When backend is starting up, it reads leases from the lease file (one by
  38. /// one) and adds them to the in-memory container as follows:
  39. /// - if lease record being parsed identifies a lease which is not present
  40. /// in the container, and the lease has valid lifetime greater than 0,
  41. /// the lease is added to the container,
  42. /// - if lease record being parsed identifies a lease which is present in the
  43. /// container, and the valid lifetime of the lease record being parsed is
  44. /// greater than 0, the lease in the container is updated
  45. /// - if lease record being parsed has valid lifetime equal to 0, and the
  46. /// corresponding lease exists in the container, the lease is removed
  47. /// from the container.
  48. ///
  49. /// After the container holding leases is initialized, each subsequent update,
  50. /// removal or addition of the lease is appended to the lease file
  51. /// synchronously.
  52. ///
  53. /// Originally, the Memfile backend didn't write leases to disk. This was
  54. /// particularly useful for testing server performance in non-disk bound
  55. /// conditions. In order to preserve this capability, the new parameters
  56. /// "persist4=yes|no" and "persist6=yes|no" has been introduced in the
  57. /// database access string. For example, database access string:
  58. /// "type=memfile persist4=no persist6=yes" disables disk writes to disk
  59. /// of DHCPv4 leases enables writes to disk of DHCPv6 leases.
  60. ///
  61. /// The lease file locations can be specified for DHCPv4 and DHCPv6 leases
  62. /// with the following two parameters in the database access string:
  63. /// - leasefile4
  64. /// - leasefile6
  65. ///
  66. /// They specify the absolute path to the files (including file names).
  67. /// If they are not specified, the default location in the installation
  68. /// directory is used: var/bind10/kea-leases4.csv and
  69. /// var/bind10/kea-leases6.csv.
  70. class Memfile_LeaseMgr : public LeaseMgr {
  71. public:
  72. /// @brief Specifies universe (V4, V6)
  73. ///
  74. /// This enumeration is used by various functions in Memfile Lease Manager,
  75. /// to identify the lease type referred to. In particular, it is used by
  76. /// functions operating on the lease files to distinguish between lease
  77. /// files for DHCPv4 and DHCPv6.
  78. enum Universe {
  79. V4,
  80. V6
  81. };
  82. /// @brief The sole lease manager constructor
  83. ///
  84. /// dbconfig is a generic way of passing parameters. Parameters
  85. /// are passed in the "name=value" format, separated by spaces.
  86. /// Values may be enclosed in double quotes, if needed.
  87. ///
  88. /// @param parameters A data structure relating keywords and values
  89. /// concerned with the database.
  90. Memfile_LeaseMgr(const ParameterMap& parameters);
  91. /// @brief Destructor (closes file)
  92. virtual ~Memfile_LeaseMgr();
  93. /// @brief Adds an IPv4 lease.
  94. ///
  95. /// @param lease lease to be added
  96. virtual bool addLease(const Lease4Ptr& lease);
  97. /// @brief Adds an IPv6 lease.
  98. ///
  99. /// @param lease lease to be added
  100. virtual bool addLease(const Lease6Ptr& lease);
  101. /// @brief Returns existing IPv4 lease for specified IPv4 address.
  102. ///
  103. /// This function returns a copy of the lease. The modification in the
  104. /// return lease does not affect the instance held in the lease storage.
  105. ///
  106. /// @param addr An address of the searched lease.
  107. ///
  108. /// @return a collection of leases
  109. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  110. /// @brief Returns existing IPv4 leases for specified hardware address.
  111. ///
  112. /// Although in the usual case there will be only one lease, for mobile
  113. /// clients or clients with multiple static/fixed/reserved leases there
  114. /// can be more than one. Thus return type is a container, not a single
  115. /// pointer.
  116. ///
  117. /// @param hwaddr hardware address of the client
  118. ///
  119. /// @return lease collection
  120. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
  121. /// @brief Returns existing IPv4 lease for specified hardware address
  122. /// and a subnet
  123. ///
  124. /// This function returns a copy of the lease. The modification in the
  125. /// return lease does not affect the instance held in the lease storage.
  126. ///
  127. /// There can be at most one lease for a given HW address in a single
  128. /// pool, so this method with either return a single lease or NULL.
  129. ///
  130. /// @param hwaddr hardware address of the client
  131. /// @param subnet_id identifier of the subnet that lease must belong to
  132. ///
  133. /// @return a pointer to the lease (or NULL if a lease is not found)
  134. virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
  135. SubnetID subnet_id) const;
  136. /// @brief Returns existing IPv4 lease for specified client-id
  137. ///
  138. /// @param client_id client identifier
  139. virtual Lease4Collection getLease4(const ClientId& client_id) const;
  140. /// @brief Returns IPv4 lease for specified client-id/hwaddr/subnet-id tuple
  141. ///
  142. /// There can be at most one lease for a given client-id/hwaddr tuple
  143. /// in a single pool, so this method with either return a single lease
  144. /// or NULL.
  145. ///
  146. /// @param clientid client identifier
  147. /// @param hwaddr hardware address of the client
  148. /// @param subnet_id identifier of the subnet that lease must belong to
  149. ///
  150. /// @return a pointer to the lease (or NULL if a lease is not found)
  151. virtual Lease4Ptr getLease4(const ClientId& clientid,
  152. const HWAddr& hwaddr,
  153. SubnetID subnet_id) const;
  154. /// @brief Returns existing IPv4 lease for specified client-id
  155. ///
  156. /// This function returns a copy of the lease. The modification in the
  157. /// return lease does not affect the instance held in the lease storage.
  158. ///
  159. /// There can be at most one lease for a given HW address in a single
  160. /// pool, so this method with either return a single lease or NULL.
  161. ///
  162. /// @param clientid client identifier
  163. /// @param subnet_id identifier of the subnet that lease must belong to
  164. ///
  165. /// @return a pointer to the lease (or NULL if a lease is not found)
  166. virtual Lease4Ptr getLease4(const ClientId& clientid,
  167. SubnetID subnet_id) const;
  168. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  169. ///
  170. /// This function returns a copy of the lease. The modification in the
  171. /// return lease does not affect the instance held in the lease storage.
  172. ///
  173. /// @param type specifies lease type: (NA, TA or PD)
  174. /// @param addr An address of the searched lease.
  175. ///
  176. /// @return smart pointer to the lease (or NULL if a lease is not found)
  177. virtual Lease6Ptr getLease6(Lease::Type type,
  178. const isc::asiolink::IOAddress& addr) const;
  179. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  180. ///
  181. /// @todo Not implemented yet
  182. ///
  183. /// @param type specifies lease type: (NA, TA or PD)
  184. /// @param duid client DUID
  185. /// @param iaid IA identifier
  186. ///
  187. /// @return collection of IPv6 leases
  188. virtual Lease6Collection getLeases6(Lease::Type type,
  189. const DUID& duid, uint32_t iaid) const;
  190. /// @brief Returns existing IPv6 lease for a given DUID/IA/subnet-id tuple
  191. ///
  192. /// This function returns a copy of the lease. The modification in the
  193. /// return lease does not affect the instance held in the lease storage.
  194. ///
  195. /// @param type specifies lease type: (NA, TA or PD)
  196. /// @param duid client DUID
  197. /// @param iaid IA identifier
  198. /// @param subnet_id identifier of the subnet the lease must belong to
  199. ///
  200. /// @return lease collection (may be empty if no lease is found)
  201. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  202. uint32_t iaid, SubnetID subnet_id) const;
  203. /// @brief Updates IPv4 lease.
  204. ///
  205. /// @warning This function does not validate the pointer to the lease.
  206. /// It is caller's responsibility to pass the valid pointer.
  207. ///
  208. /// @param lease4 The lease to be updated.
  209. ///
  210. /// If no such lease is present, an exception will be thrown.
  211. virtual void updateLease4(const Lease4Ptr& lease4);
  212. /// @brief Updates IPv6 lease.
  213. ///
  214. /// @warning This function does not validate the pointer to the lease.
  215. /// It is caller's responsibility to pass the valid pointer.
  216. ///
  217. /// @param lease6 The lease to be updated.
  218. ///
  219. /// If no such lease is present, an exception will be thrown.
  220. virtual void updateLease6(const Lease6Ptr& lease6);
  221. /// @brief Deletes a lease.
  222. ///
  223. /// @param addr Address of the lease to be deleted. (This can be IPv4 or
  224. /// IPv6.)
  225. ///
  226. /// @return true if deletion was successful, false if no such lease exists
  227. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  228. /// @brief Return backend type
  229. ///
  230. /// Returns the type of the backend.
  231. ///
  232. /// @return Type of the backend.
  233. virtual std::string getType() const {
  234. return (std::string("memfile"));
  235. }
  236. /// @brief Returns backend name.
  237. ///
  238. /// For now, memfile can only store data in memory.
  239. ///
  240. /// @return Name of the backend.
  241. virtual std::string getName() const {
  242. return ("memory");
  243. }
  244. /// @brief Returns description of the backend.
  245. ///
  246. /// This description may be multiline text that describes the backend.
  247. ///
  248. /// @return Description of the backend.
  249. virtual std::string getDescription() const;
  250. /// @brief Returns backend version.
  251. ///
  252. /// @return Version number as a pair of unsigned integers. "first" is the
  253. /// major version number, "second" the minor number.
  254. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  255. return (std::make_pair(1, 0));
  256. }
  257. /// @brief Commit Transactions
  258. ///
  259. /// Commits all pending database operations. On databases that don't
  260. /// support transactions, this is a no-op.
  261. virtual void commit();
  262. /// @brief Rollback Transactions
  263. ///
  264. /// Rolls back all pending database operations. On databases that don't
  265. /// support transactions, this is a no-op.
  266. virtual void rollback();
  267. /// @brief Returns default path to the lease file.
  268. ///
  269. /// @param u Universe (V4 or V6).
  270. std::string getDefaultLeaseFilePath(Universe u) const;
  271. /// @brief Returns an absolute path to the lease file.
  272. ///
  273. /// @param u Universe (V4 or V6).
  274. ///
  275. /// @return Absolute path to the lease file or empty string if no lease
  276. /// file is used.
  277. std::string getLeaseFilePath(Universe u) const;
  278. /// @brief Specifies whether or not leases are written to disk.
  279. ///
  280. /// It is possible that leases for DHCPv4 are written to disk whereas leases
  281. /// for DHCPv6 are not; or vice versa. The argument of the method specifies
  282. /// the type of lease in that respect.
  283. ///
  284. /// @param u Universe (V4 or V6).
  285. ///
  286. /// @return true if leases are written to lease file; if false is
  287. /// returned, leases will be held in memory and will be lost upon
  288. /// server shut down.
  289. bool persistLeases(Universe u) const;
  290. protected:
  291. /// @brief Load all DHCPv4 leases from the file.
  292. ///
  293. /// This method loads all DHCPv4 leases from a file to memory. It removes
  294. /// existing leases before reading a file.
  295. ///
  296. /// @throw isc::DbOperationError If failed to read a lease from the lease
  297. /// file.
  298. void load4();
  299. /// @brief Loads a single DHCPv4 lease from the file.
  300. ///
  301. /// This method reads a single lease record from the lease file. If the
  302. /// corresponding record doesn't exist in the in-memory container, the
  303. /// lease is added to the container (except for a lease which valid lifetime
  304. /// is 0). If the corresponding lease exists, the lease being read updates
  305. /// the existing lease. If the lease being read from the lease file has
  306. /// valid lifetime of 0 and the corresponding lease exists in the in-memory
  307. /// database, the existing lease is removed.
  308. ///
  309. /// @param lease Pointer to the lease read from the lease file.
  310. void loadLease4(Lease4Ptr& lease);
  311. /// @brief Load all DHCPv6 leases from the file.
  312. ///
  313. /// This method loads all DHCPv6 leases from a file to memory. It removes
  314. /// existing leases before reading a file.
  315. ///
  316. /// @throw isc::DbOperationError If failed to read a lease from the lease
  317. /// file.
  318. void load6();
  319. /// @brief Loads a single DHCPv6 lease from the file.
  320. ///
  321. /// This method reads a single lease record from the lease file. If the
  322. /// corresponding record doesn't exist in the in-memory container, the
  323. /// lease is added to the container (except for a lease which valid lifetime
  324. /// is 0). If the corresponding lease exists, the lease being read updates
  325. /// the existing lease. If the lease being read from the lease file has
  326. /// valid lifetime of 0 and the corresponding lease exists in the in-memory
  327. /// database, the existing lease is removed.
  328. ///
  329. /// @param lease Pointer to the lease read from the lease file.
  330. void loadLease6(Lease6Ptr& lease);
  331. /// @brief Initialize the location of the lease file.
  332. ///
  333. /// This method uses the parameters passed as a map to the constructor to
  334. /// initialize the location of the lease file. If the lease file is not
  335. /// specified, the method will use the default location for the universe
  336. /// (v4 or v6) selected. If the location is specified in the map as empty
  337. /// or the "persist" parameter is set to "no" it will set the empty
  338. /// location, which implies that leases belonging to the specified universe
  339. /// will not be written to disk.
  340. ///
  341. /// @param u Universe (v4 or v6)
  342. /// @param parameters Map holding parameters of the Lease Manager, passed to
  343. /// the constructor.
  344. ///
  345. /// @return The location of the lease file that should be assigned to the
  346. /// lease_file4_ or lease_file6_, depending on the universe specified as an
  347. /// argument to this function.
  348. std::string initLeaseFilePath(Universe u);
  349. // This is a multi-index container, which holds elements that can
  350. // be accessed using different search indexes.
  351. typedef boost::multi_index_container<
  352. // It holds pointers to Lease6 objects.
  353. Lease6Ptr,
  354. boost::multi_index::indexed_by<
  355. // Specification of the first index starts here.
  356. // This index sorts leases by IPv6 addresses represented as
  357. // IOAddress objects.
  358. boost::multi_index::ordered_unique<
  359. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  360. >,
  361. // Specification of the second index starts here.
  362. boost::multi_index::ordered_unique<
  363. // This is a composite index that will be used to search for
  364. // the lease using three attributes: DUID, IAID, Subnet Id.
  365. boost::multi_index::composite_key<
  366. Lease6,
  367. // The DUID can be retrieved from the Lease6 object using
  368. // a getDuidVector const function.
  369. boost::multi_index::const_mem_fun<Lease6, const std::vector<uint8_t>&,
  370. &Lease6::getDuidVector>,
  371. // The two other ingredients of this index are IAID and
  372. // subnet id.
  373. boost::multi_index::member<Lease6, uint32_t, &Lease6::iaid_>,
  374. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  375. >
  376. >
  377. >
  378. > Lease6Storage; // Specify the type name of this container.
  379. // This is a multi-index container, which holds elements that can
  380. // be accessed using different search indexes.
  381. typedef boost::multi_index_container<
  382. // It holds pointers to Lease4 objects.
  383. Lease4Ptr,
  384. // Specification of search indexes starts here.
  385. boost::multi_index::indexed_by<
  386. // Specification of the first index starts here.
  387. // This index sorts leases by IPv4 addresses represented as
  388. // IOAddress objects.
  389. boost::multi_index::ordered_unique<
  390. // The IPv4 address are held in addr_ members that belong to
  391. // Lease class.
  392. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  393. >,
  394. // Specification of the second index starts here.
  395. boost::multi_index::ordered_unique<
  396. // This is a composite index that combines two attributes of the
  397. // Lease4 object: hardware address and subnet id.
  398. boost::multi_index::composite_key<
  399. Lease4,
  400. // The hardware address is held in the hwaddr_ member of the
  401. // Lease4 object.
  402. boost::multi_index::member<Lease4, std::vector<uint8_t>,
  403. &Lease4::hwaddr_>,
  404. // The subnet id is held in the subnet_id_ member of Lease4
  405. // class. Note that the subnet_id_ is defined in the base
  406. // class (Lease) so we have to point to this class rather
  407. // than derived class: Lease4.
  408. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  409. >
  410. >,
  411. // Specification of the third index starts here.
  412. boost::multi_index::ordered_non_unique<
  413. // This is a composite index that uses two values to search for a
  414. // lease: client id and subnet id.
  415. boost::multi_index::composite_key<
  416. Lease4,
  417. // The client id can be retrieved from the Lease4 object by
  418. // calling getClientIdVector const function.
  419. boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
  420. &Lease4::getClientIdVector>,
  421. // The subnet id is accessed through the subnet_id_ member.
  422. boost::multi_index::member<Lease, uint32_t, &Lease::subnet_id_>
  423. >
  424. >,
  425. // Specification of the fourth index starts here.
  426. boost::multi_index::ordered_non_unique<
  427. // This is a composite index that uses two values to search for a
  428. // lease: client id and subnet id.
  429. boost::multi_index::composite_key<
  430. Lease4,
  431. // The client id can be retrieved from the Lease4 object by
  432. // calling getClientIdVector const function.
  433. boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
  434. &Lease4::getClientIdVector>,
  435. // The hardware address is held in the hwaddr_ member of the
  436. // Lease4 object.
  437. boost::multi_index::member<Lease4, std::vector<uint8_t>,
  438. &Lease4::hwaddr_>,
  439. // The subnet id is accessed through the subnet_id_ member.
  440. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  441. >
  442. >
  443. >
  444. > Lease4Storage; // Specify the type name for this container.
  445. /// @brief stores IPv4 leases
  446. Lease4Storage storage4_;
  447. /// @brief stores IPv6 leases
  448. Lease6Storage storage6_;
  449. /// @brief Holds the pointer to the DHCPv4 lease file IO.
  450. boost::shared_ptr<CSVLeaseFile4> lease_file4_;
  451. /// @brief Holds the pointer to the DHCPv6 lease file IO.
  452. boost::shared_ptr<CSVLeaseFile6> lease_file6_;
  453. };
  454. }; // end of isc::dhcp namespace
  455. }; // end of isc namespace
  456. #endif // MEMFILE_LEASE_MGR