memfile_lease_mgr.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/lease_mgr.h>
  18. #include <boost/multi_index/indexed_by.hpp>
  19. #include <boost/multi_index/member.hpp>
  20. #include <boost/multi_index/ordered_index.hpp>
  21. #include <boost/multi_index_container.hpp>
  22. #include <boost/multi_index/composite_key.hpp>
  23. namespace isc {
  24. namespace dhcp {
  25. // This is a concrete implementation of a Lease database.
  26. //
  27. // It is for testing purposes only. It is NOT a production code.
  28. //
  29. // It does not do anything useful now, and is used for abstract LeaseMgr
  30. // class testing. It may later evolve into more useful backend if the
  31. // need arises. We can reuse code from memfile benchmark. See code in
  32. // tests/tools/dhcp-ubench/memfile_bench.{cc|h}
  33. class Memfile_LeaseMgr : public LeaseMgr {
  34. public:
  35. /// @brief Specifies universe (V4, V6)
  36. ///
  37. /// This enumeration is used by various functions in Memfile Lease Manager,
  38. /// to identify the lease type referred to. In particular, it is used by
  39. /// functions operating on the lease files to distinguish between lease
  40. /// files for DHCPv4 and DHCPv6.
  41. enum Universe {
  42. V4,
  43. V6
  44. };
  45. /// @brief The sole lease manager constructor
  46. ///
  47. /// dbconfig is a generic way of passing parameters. Parameters
  48. /// are passed in the "name=value" format, separated by spaces.
  49. /// Values may be enclosed in double quotes, if needed.
  50. ///
  51. /// @param parameters A data structure relating keywords and values
  52. /// concerned with the database.
  53. Memfile_LeaseMgr(const ParameterMap& parameters);
  54. /// @brief Destructor (closes file)
  55. virtual ~Memfile_LeaseMgr();
  56. /// @brief Adds an IPv4 lease.
  57. ///
  58. /// @param lease lease to be added
  59. virtual bool addLease(const Lease4Ptr& lease);
  60. /// @brief Adds an IPv6 lease.
  61. ///
  62. /// @param lease lease to be added
  63. virtual bool addLease(const Lease6Ptr& lease);
  64. /// @brief Returns existing IPv4 lease for specified IPv4 address.
  65. ///
  66. /// This function returns a copy of the lease. The modification in the
  67. /// return lease does not affect the instance held in the lease storage.
  68. ///
  69. /// @param addr An address of the searched lease.
  70. ///
  71. /// @return a collection of leases
  72. virtual Lease4Ptr getLease4(const isc::asiolink::IOAddress& addr) const;
  73. /// @brief Returns existing IPv4 leases for specified hardware address.
  74. ///
  75. /// Although in the usual case there will be only one lease, for mobile
  76. /// clients or clients with multiple static/fixed/reserved leases there
  77. /// can be more than one. Thus return type is a container, not a single
  78. /// pointer.
  79. ///
  80. /// @param hwaddr hardware address of the client
  81. ///
  82. /// @return lease collection
  83. virtual Lease4Collection getLease4(const isc::dhcp::HWAddr& hwaddr) const;
  84. /// @brief Returns existing IPv4 lease for specified hardware address
  85. /// and a subnet
  86. ///
  87. /// This function returns a copy of the lease. The modification in the
  88. /// return lease does not affect the instance held in the lease storage.
  89. ///
  90. /// There can be at most one lease for a given HW address in a single
  91. /// pool, so this method with either return a single lease or NULL.
  92. ///
  93. /// @param hwaddr hardware address of the client
  94. /// @param subnet_id identifier of the subnet that lease must belong to
  95. ///
  96. /// @return a pointer to the lease (or NULL if a lease is not found)
  97. virtual Lease4Ptr getLease4(const HWAddr& hwaddr,
  98. SubnetID subnet_id) const;
  99. /// @brief Returns existing IPv4 lease for specified client-id
  100. ///
  101. /// @param client_id client identifier
  102. virtual Lease4Collection getLease4(const ClientId& client_id) const;
  103. /// @brief Returns IPv4 lease for specified client-id/hwaddr/subnet-id tuple
  104. ///
  105. /// There can be at most one lease for a given client-id/hwaddr tuple
  106. /// in a single pool, so this method with either return a single lease
  107. /// or NULL.
  108. ///
  109. /// @param clientid client identifier
  110. /// @param hwaddr hardware address of the client
  111. /// @param subnet_id identifier of the subnet that lease must belong to
  112. ///
  113. /// @return a pointer to the lease (or NULL if a lease is not found)
  114. virtual Lease4Ptr getLease4(const ClientId& clientid,
  115. const HWAddr& hwaddr,
  116. SubnetID subnet_id) const;
  117. /// @brief Returns existing IPv4 lease for specified client-id
  118. ///
  119. /// This function returns a copy of the lease. The modification in the
  120. /// return lease does not affect the instance held in the lease storage.
  121. ///
  122. /// There can be at most one lease for a given HW address in a single
  123. /// pool, so this method with either return a single lease or NULL.
  124. ///
  125. /// @param clientid client identifier
  126. /// @param subnet_id identifier of the subnet that lease must belong to
  127. ///
  128. /// @return a pointer to the lease (or NULL if a lease is not found)
  129. virtual Lease4Ptr getLease4(const ClientId& clientid,
  130. SubnetID subnet_id) const;
  131. /// @brief Returns existing IPv6 lease for a given IPv6 address.
  132. ///
  133. /// This function returns a copy of the lease. The modification in the
  134. /// return lease does not affect the instance held in the lease storage.
  135. ///
  136. /// @param type specifies lease type: (NA, TA or PD)
  137. /// @param addr An address of the searched lease.
  138. ///
  139. /// @return smart pointer to the lease (or NULL if a lease is not found)
  140. virtual Lease6Ptr getLease6(Lease::Type type,
  141. const isc::asiolink::IOAddress& addr) const;
  142. /// @brief Returns existing IPv6 lease for a given DUID+IA combination
  143. ///
  144. /// @todo Not implemented yet
  145. ///
  146. /// @param type specifies lease type: (NA, TA or PD)
  147. /// @param duid client DUID
  148. /// @param iaid IA identifier
  149. ///
  150. /// @return collection of IPv6 leases
  151. virtual Lease6Collection getLeases6(Lease::Type type,
  152. const DUID& duid, uint32_t iaid) const;
  153. /// @brief Returns existing IPv6 lease for a given DUID/IA/subnet-id tuple
  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. /// @param type specifies lease type: (NA, TA or PD)
  159. /// @param duid client DUID
  160. /// @param iaid IA identifier
  161. /// @param subnet_id identifier of the subnet the lease must belong to
  162. ///
  163. /// @return lease collection (may be empty if no lease is found)
  164. virtual Lease6Collection getLeases6(Lease::Type type, const DUID& duid,
  165. uint32_t iaid, SubnetID subnet_id) const;
  166. /// @brief Updates IPv4 lease.
  167. ///
  168. /// @warning This function does not validate the pointer to the lease.
  169. /// It is caller's responsibility to pass the valid pointer.
  170. ///
  171. /// @param lease4 The lease to be updated.
  172. ///
  173. /// If no such lease is present, an exception will be thrown.
  174. virtual void updateLease4(const Lease4Ptr& lease4);
  175. /// @brief Updates IPv6 lease.
  176. ///
  177. /// @warning This function does not validate the pointer to the lease.
  178. /// It is caller's responsibility to pass the valid pointer.
  179. ///
  180. /// @param lease6 The lease to be updated.
  181. ///
  182. /// If no such lease is present, an exception will be thrown.
  183. virtual void updateLease6(const Lease6Ptr& lease6);
  184. /// @brief Deletes a lease.
  185. ///
  186. /// @param addr Address of the lease to be deleted. (This can be IPv4 or
  187. /// IPv6.)
  188. ///
  189. /// @return true if deletion was successful, false if no such lease exists
  190. virtual bool deleteLease(const isc::asiolink::IOAddress& addr);
  191. /// @brief Return backend type
  192. ///
  193. /// Returns the type of the backend.
  194. ///
  195. /// @return Type of the backend.
  196. virtual std::string getType() const {
  197. return (std::string("memfile"));
  198. }
  199. /// @brief Returns backend name.
  200. ///
  201. /// For now, memfile can only store data in memory.
  202. ///
  203. /// @return Name of the backend.
  204. virtual std::string getName() const {
  205. return ("memory");
  206. }
  207. /// @brief Returns description of the backend.
  208. ///
  209. /// This description may be multiline text that describes the backend.
  210. ///
  211. /// @return Description of the backend.
  212. virtual std::string getDescription() const;
  213. /// @brief Returns backend version.
  214. ///
  215. /// @return Version number as a pair of unsigned integers. "first" is the
  216. /// major version number, "second" the minor number.
  217. virtual std::pair<uint32_t, uint32_t> getVersion() const {
  218. return (std::make_pair(1, 0));
  219. }
  220. /// @brief Commit Transactions
  221. ///
  222. /// Commits all pending database operations. On databases that don't
  223. /// support transactions, this is a no-op.
  224. virtual void commit();
  225. /// @brief Rollback Transactions
  226. ///
  227. /// Rolls back all pending database operations. On databases that don't
  228. /// support transactions, this is a no-op.
  229. virtual void rollback();
  230. /// @brief Returns default path to the lease file.
  231. ///
  232. /// @param u Universe (V4 or V6).
  233. std::string getDefaultLeaseFilePath(Universe u) const;
  234. /// @brief Returns an absolute path to the lease file.
  235. ///
  236. /// @param u Universe (V4 or V6).
  237. std::string getLeaseFilePath(Universe u) const {
  238. return (u == V4 ? lease_file4_ : lease_file6_);
  239. }
  240. /// @brief Specifies whether or not leases are written to disk.
  241. ///
  242. /// It is possible that leases for DHCPv4 are written to disk whereas leases
  243. /// for DHCPv6 are not; or vice versa. The argument of the method specifies
  244. /// the type of lease in that respect.
  245. ///
  246. /// @param u Universe (V4 or V6).
  247. ///
  248. /// @return true if leases are written to lease file; if false is
  249. /// returned, leases will be held in memory and will be lost upon
  250. /// server shut down.
  251. bool persistLeases(Universe u) const;
  252. protected:
  253. /// @brief Initialize the location of the lease file.
  254. ///
  255. /// This method uses the parameters passed as a map to the constructor to
  256. /// initialize the location of the lease file. If the lease file is not
  257. /// specified, the method will use the default location for the universe
  258. /// (v4 or v6) selected. If the location is specified in the map as empty
  259. /// it will set the empty location, which implies that leases belonging to
  260. /// the specified universe will not be written to disk.
  261. ///
  262. /// @param u Universe (v4 or v6)
  263. /// @param parameters Map holding parameters of the Lease Manager, passed to
  264. /// the constructor.
  265. ///
  266. /// @return The location of the lease file that should be assigned to the
  267. /// lease_file4_ or lease_file6_, depending on the universe specified as an
  268. /// argument to this function.
  269. std::string initLeaseFilePath(Universe u);
  270. // This is a multi-index container, which holds elements that can
  271. // be accessed using different search indexes.
  272. typedef boost::multi_index_container<
  273. // It holds pointers to Lease6 objects.
  274. Lease6Ptr,
  275. boost::multi_index::indexed_by<
  276. // Specification of the first index starts here.
  277. // This index sorts leases by IPv6 addresses represented as
  278. // IOAddress objects.
  279. boost::multi_index::ordered_unique<
  280. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  281. >,
  282. // Specification of the second index starts here.
  283. boost::multi_index::ordered_unique<
  284. // This is a composite index that will be used to search for
  285. // the lease using three attributes: DUID, IAID, Subnet Id.
  286. boost::multi_index::composite_key<
  287. Lease6,
  288. // The DUID can be retrieved from the Lease6 object using
  289. // a getDuidVector const function.
  290. boost::multi_index::const_mem_fun<Lease6, const std::vector<uint8_t>&,
  291. &Lease6::getDuidVector>,
  292. // The two other ingredients of this index are IAID and
  293. // subnet id.
  294. boost::multi_index::member<Lease6, uint32_t, &Lease6::iaid_>,
  295. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  296. >
  297. >
  298. >
  299. > Lease6Storage; // Specify the type name of this container.
  300. // This is a multi-index container, which holds elements that can
  301. // be accessed using different search indexes.
  302. typedef boost::multi_index_container<
  303. // It holds pointers to Lease4 objects.
  304. Lease4Ptr,
  305. // Specification of search indexes starts here.
  306. boost::multi_index::indexed_by<
  307. // Specification of the first index starts here.
  308. // This index sorts leases by IPv4 addresses represented as
  309. // IOAddress objects.
  310. boost::multi_index::ordered_unique<
  311. // The IPv4 address are held in addr_ members that belong to
  312. // Lease class.
  313. boost::multi_index::member<Lease, isc::asiolink::IOAddress, &Lease::addr_>
  314. >,
  315. // Specification of the second index starts here.
  316. boost::multi_index::ordered_unique<
  317. // This is a composite index that combines two attributes of the
  318. // Lease4 object: hardware address and subnet id.
  319. boost::multi_index::composite_key<
  320. Lease4,
  321. // The hardware address is held in the hwaddr_ member of the
  322. // Lease4 object.
  323. boost::multi_index::member<Lease4, std::vector<uint8_t>,
  324. &Lease4::hwaddr_>,
  325. // The subnet id is held in the subnet_id_ member of Lease4
  326. // class. Note that the subnet_id_ is defined in the base
  327. // class (Lease) so we have to point to this class rather
  328. // than derived class: Lease4.
  329. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  330. >
  331. >,
  332. // Specification of the third index starts here.
  333. boost::multi_index::ordered_unique<
  334. // This is a composite index that uses two values to search for a
  335. // lease: client id and subnet id.
  336. boost::multi_index::composite_key<
  337. Lease4,
  338. // The client id can be retrieved from the Lease4 object by
  339. // calling getClientIdVector const function.
  340. boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
  341. &Lease4::getClientIdVector>,
  342. // The subnet id is accessed through the subnet_id_ member.
  343. boost::multi_index::member<Lease, uint32_t, &Lease::subnet_id_>
  344. >
  345. >,
  346. // Specification of the fourth index starts here.
  347. boost::multi_index::ordered_unique<
  348. // This is a composite index that uses two values to search for a
  349. // lease: client id and subnet id.
  350. boost::multi_index::composite_key<
  351. Lease4,
  352. // The client id can be retrieved from the Lease4 object by
  353. // calling getClientIdVector const function.
  354. boost::multi_index::const_mem_fun<Lease4, const std::vector<uint8_t>&,
  355. &Lease4::getClientIdVector>,
  356. // The hardware address is held in the hwaddr_ member of the
  357. // Lease4 object.
  358. boost::multi_index::member<Lease4, std::vector<uint8_t>,
  359. &Lease4::hwaddr_>,
  360. // The subnet id is accessed through the subnet_id_ member.
  361. boost::multi_index::member<Lease, SubnetID, &Lease::subnet_id_>
  362. >
  363. >
  364. >
  365. > Lease4Storage; // Specify the type name for this container.
  366. /// @brief stores IPv4 leases
  367. Lease4Storage storage4_;
  368. /// @brief stores IPv6 leases
  369. Lease6Storage storage6_;
  370. /// @brief Holds the absolute path to the lease file for DHCPv4.
  371. std::string lease_file4_;
  372. /// @brief Holds the absolute path to the lease file for DHCPv6.
  373. std::string lease_file6_;
  374. };
  375. }; // end of isc::dhcp namespace
  376. }; // end of isc namespace
  377. #endif // MEMFILE_LEASE_MGR