memfile_lease_mgr.h 17 KB

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