cfg_subnets6.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright (C) 2014-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 CFG_SUBNETS6_H
  15. #define CFG_SUBNETS6_H
  16. #include <asiolink/io_address.h>
  17. #include <dhcp/option.h>
  18. #include <dhcpsrv/subnet.h>
  19. #include <dhcpsrv/subnet_selector.h>
  20. #include <util/optional_value.h>
  21. #include <boost/shared_ptr.hpp>
  22. namespace isc {
  23. namespace dhcp {
  24. /// @brief Holds subnets configured for the DHCPv6 server.
  25. ///
  26. /// This class holds a collection of subnets configured for the DHCPv6 server.
  27. /// It allows for retrieving a subnet for the particular client using various
  28. /// parameters extracted from the DHCPv6 message. These parameters must be
  29. /// assigned to the appropriate members of the @c SubnetSelector structure.
  30. ///
  31. /// See @c CfgSubnets6::selectSubnet documentation for more details on how the subnet
  32. /// is selected for the client.
  33. class CfgSubnets6 {
  34. public:
  35. /// @brief Adds new subnet to the configuration.
  36. ///
  37. /// @param subnet Pointer to the subnet being added.
  38. ///
  39. /// @throw isc::DuplicateSubnetID If the subnet id for the new subnet
  40. /// duplicates id of an existing subnet.
  41. void add(const Subnet6Ptr& subnet);
  42. /// @brief Returns pointer to the collection of all IPv6 subnets.
  43. ///
  44. /// This is used in a hook (subnet6_select), where the hook is able
  45. /// to choose a different subnet. Server code has to offer a list
  46. /// of possible choices (i.e. all subnets).
  47. ///
  48. /// @return A pointer to const Subnet6 collection
  49. const Subnet6Collection* getAll() const {
  50. return (&subnets_);
  51. }
  52. /// @brief Selects a subnet using parameters specified in the selector.
  53. ///
  54. /// This method tries to retrieve the subnet for the client using various
  55. /// parameters extracted from the client's message using the following
  56. /// logic.
  57. ///
  58. /// If the relay agent link address is set to zero it is assumed that
  59. /// the subnet is selected for the directly connected client.
  60. /// In this case it is checked if there is any subnet associated with the
  61. /// interface over which the message has been received. If there is no
  62. /// subnet explicitly associated with this interface the client's address
  63. /// will be used to check if the address is in range with any of the
  64. /// subnets.
  65. ///
  66. /// If the message was relayed it is possible that the relay agent has
  67. /// appended an Interface ID option. If this option is present, the method
  68. /// will check if it matches with any explicitly specified interface id
  69. /// for any subnet. If it does, the subnet is returned. Otherwise, the
  70. /// relay agents link address is used to select the subnet. In this case,
  71. /// the method will first check if this link address is explicitly
  72. /// associated with any subnet. If not, it is checked if the link address
  73. /// is in range with any of the subnets.
  74. ///
  75. /// @todo This method requires performance improvement! It currently
  76. /// iterates over all existing subnets (possibly a couple of times)
  77. /// to find the one which fulfils the search criteria. The subnet storage
  78. /// is implemented as a simple STL vector which precludes fast searches
  79. /// using specific keys. Hence, full scan is required. To improve the
  80. /// search performance a different container type is required, e.g.
  81. /// multi-index container, or something of a similar functionality.
  82. ///
  83. /// @param selector Const reference to the selector structure which holds
  84. /// various information extracted from the client's packet which are used
  85. /// to find appropriate subnet.
  86. ///
  87. /// @return Pointer to the selected subnet or NULL if no subnet found.
  88. Subnet6Ptr selectSubnet(const SubnetSelector& selector) const;
  89. /// @brief Selects the subnet using a specified address.
  90. ///
  91. /// This method searches for the subnet using the specified address. If
  92. /// the specified address is a link address on the relay agent (which is
  93. /// indicated by the 3rd argument) the method will first try to match the
  94. /// specified address with the relay addresses explicitly specified for
  95. /// existing subnets. If no match is found, the method will check if the
  96. /// address is in range with any of the subnets.
  97. ///
  98. /// If the address is not a relay agent link address (@c is_relay_address
  99. /// is set to false), the method will simply check if the address is in
  100. /// range with any of the subnets.
  101. ///
  102. /// @note This method is mainly to be used in unit tests, which often
  103. /// require sanity-checking if the subnet exists for the particular
  104. /// address. For other purposes the @c selectSubnet(SubnetSelector) should
  105. /// rather be used instead.
  106. ///
  107. /// @todo This method requires performance improvement! It currently
  108. /// iterates over all existing subnets (possibly a couple of times)
  109. /// to find the one which fulfils the search criteria. The subnet storage
  110. /// is implemented as a simple STL vector which precludes fast searches
  111. /// using specific keys. Hence, full scan is required. To improve the
  112. /// search performance a different container type is required, e.g.
  113. /// multi-index container, or something of a similar functionality.
  114. ///
  115. /// @param address Address for which the subnet is searched.
  116. /// @param client_classes Optional parameter specifying the classes that
  117. /// the client belongs to.
  118. /// @param is_relay_address Specifies if the provided address is an
  119. /// address of the relay agent (true) or not (false).
  120. ///
  121. /// @return Pointer to the selected subnet or NULL if no subnet found.
  122. Subnet6Ptr
  123. selectSubnet(const asiolink::IOAddress& address,
  124. const ClientClasses& client_classes = ClientClasses(),
  125. const bool is_relay_address = false) const;
  126. /// @brief Updates statistics.
  127. ///
  128. /// This method updates statistics that are affected by the newly committed
  129. /// configuration. In particular, it updates the number of available addresses
  130. /// and prefixes in each subnet. Other statistics may be added in the future. In
  131. /// general, these are statistics that are dependant only on configuration, so
  132. /// they are not expected to change until the next reconfiguration event.
  133. void updateStatistics();
  134. /// @brief Removes statistics.
  135. ///
  136. /// During commitment of a new configuration, we need to get rid of the old
  137. /// statistics for the old configuration. In particular, we need to remove
  138. /// anything related to subnets, as there may be fewer subnets in the new
  139. /// configuration and also subnet-ids may change.
  140. void removeStatistics();
  141. private:
  142. /// @brief Selects a subnet using the interface name.
  143. ///
  144. /// This method searches for the subnet using the name of the interface.
  145. /// If any of the subnets is explicitly associated with the interface
  146. /// name, the subnet is returned.
  147. ///
  148. /// @todo This method requires performance improvement! It currently
  149. /// iterates over all existing subnets to find the one which fulfils
  150. /// the search criteria. The subnet storage is implemented as a
  151. /// simple STL vector which precludes fast searches using specific
  152. /// keys. Hence, full scan is required. To improve the search
  153. /// performance a different container type is required, e.g.
  154. /// multi-index container, or something of a similar functionality.
  155. ///
  156. /// @param iface_name Interface name.
  157. /// @param client_classes Optional parameter specifying the classes that
  158. /// the client belongs to.
  159. ///
  160. /// @return Pointer to the selected subnet or NULL if no subnet found.
  161. Subnet6Ptr
  162. selectSubnet(const std::string& iface_name,
  163. const ClientClasses& client_classes) const;
  164. /// @brief Selects a subnet using Interface ID option.
  165. ///
  166. /// This method searches for the subnet using the Interface ID option
  167. /// inserted by the relay agent to the message from a client. If any
  168. /// of the subnets is explicitly associated with that interface id, the
  169. /// subnet is returned.
  170. ///
  171. /// @todo This method requires performance improvement! It currently
  172. /// iterates over all existing subnets to find the one which fulfils
  173. /// the search criteria. The subnet storage is implemented as a
  174. /// simple STL vector which precludes fast searches using specific
  175. /// keys. Hence, full scan is required. To improve the search
  176. /// performance a different container type is required, e.g.
  177. /// multi-index container, or something of a similar functionality.
  178. ///
  179. /// @param interface_id An instance of the Interface ID option received
  180. /// from the client.
  181. /// @param client_classes Optional parameter specifying the classes that
  182. /// the client belongs to.
  183. ///
  184. /// @return Pointer to the selected subnet or NULL if no subnet found.
  185. Subnet6Ptr
  186. selectSubnet(const OptionPtr& interface_id,
  187. const ClientClasses& client_classes) const;
  188. /// @brief Checks that the IPv6 subnet with the given id already exists.
  189. ///
  190. /// @param subnet Subnet for which this function will check if the other
  191. /// subnet with equal id already exists.
  192. ///
  193. /// @return true if the duplicate subnet exists.
  194. bool isDuplicate(const Subnet6& subnet) const;
  195. /// @brief A container for IPv6 subnets.
  196. Subnet6Collection subnets_;
  197. };
  198. /// @name Pointer to the @c CfgSubnets6 objects.
  199. //@{
  200. /// @brief Non-const pointer.
  201. typedef boost::shared_ptr<CfgSubnets6> CfgSubnets6Ptr;
  202. /// @brief Const pointer.
  203. typedef boost::shared_ptr<const CfgSubnets6> ConstCfgSubnets6Ptr;
  204. //@}
  205. }
  206. }
  207. #endif // CFG_SUBNETS6_H