cfg_subnets6.h 9.7 KB

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