ctrl_dhcp6_srv.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright (C) 2012-2017 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 CTRL_DHCPV6_SRV_H
  7. #define CTRL_DHCPV6_SRV_H
  8. #include <asiolink/asiolink.h>
  9. #include <cc/data.h>
  10. #include <cc/command_interpreter.h>
  11. #include <dhcpsrv/timer_mgr.h>
  12. #include <dhcp6/dhcp6_srv.h>
  13. namespace isc {
  14. namespace dhcp {
  15. /// @brief Controlled version of the DHCPv6 server
  16. ///
  17. /// This is a class that is responsible for DHCPv6 server being controllable,
  18. /// by reading configuration file from disk.
  19. class ControlledDhcpv6Srv : public isc::dhcp::Dhcpv6Srv {
  20. public:
  21. /// @brief Constructor
  22. ///
  23. /// @param port UDP port to be opened for DHCP traffic
  24. ControlledDhcpv6Srv(uint16_t port = DHCP6_SERVER_PORT);
  25. /// @brief Destructor.
  26. virtual ~ControlledDhcpv6Srv();
  27. /// @brief Initializes the server.
  28. ///
  29. /// Depending on the configuration backend, it establishes msgq session,
  30. /// reads the JSON file from disk or may perform any other setup
  31. /// operation. For specific details, see actual implementation in
  32. /// *_backend.cc
  33. ///
  34. /// This method may throw if initialization fails. Exception types may be
  35. /// specific to used configuration backend.
  36. void init(const std::string& config_file);
  37. /// @brief Performs cleanup, immediately before termination
  38. ///
  39. /// This method performs final clean up, just before the Dhcpv6Srv object
  40. /// is destroyed. Currently it is a no-op.
  41. void cleanup();
  42. /// @brief Initiates shutdown procedure for the whole DHCPv6 server.
  43. void shutdown();
  44. /// @brief command processor
  45. ///
  46. /// This method is uniform for all config backends. It processes received
  47. /// command (as a string + JSON arguments). Internally, it's just a
  48. /// wrapper that calls process*Command() methods and catches exceptions
  49. /// in them.
  50. ///
  51. /// Currently supported commands are:
  52. /// - shutdown
  53. /// - libreload
  54. /// - config-reload
  55. /// - leases-reclaim
  56. ///
  57. /// @note It never throws.
  58. ///
  59. /// @param command Text representation of the command (e.g. "shutdown")
  60. /// @param args Optional parameters
  61. ///
  62. /// @return status of the command
  63. static isc::data::ConstElementPtr
  64. processCommand(const std::string& command, isc::data::ConstElementPtr args);
  65. /// @brief configuration processor
  66. ///
  67. /// This is a method for handling incoming configuration updates.
  68. /// This method should be called by all configuration backends when the
  69. /// server is starting up or when configuration has changed.
  70. ///
  71. /// As pointer to this method is used a callback in ASIO used in
  72. /// ModuleCCSession, it has to be static.
  73. ///
  74. /// @param new_config textual representation of the new configuration
  75. ///
  76. /// @return status of the config update
  77. static isc::data::ConstElementPtr
  78. processConfig(isc::data::ConstElementPtr new_config);
  79. /// @brief returns pointer to the sole instance of Dhcpv6Srv
  80. ///
  81. /// @return server instance (may return NULL, if called before server is spawned)
  82. static ControlledDhcpv6Srv* getInstance() {
  83. return (server_);
  84. }
  85. private:
  86. /// @brief Callback that will be called from iface_mgr when data
  87. /// is received over control socket.
  88. ///
  89. /// This static callback method is called from IfaceMgr::receive6() method,
  90. /// when there is a new command or configuration sent over control socket
  91. /// (that was sent from some yet unspecified sender).
  92. static void sessionReader(void);
  93. /// @brief handler for processing 'shutdown' command
  94. ///
  95. /// This handler processes shutdown command, which initializes shutdown
  96. /// procedure.
  97. /// @param command (parameter ignored)
  98. /// @param args (parameter ignored)
  99. ///
  100. /// @return status of the command
  101. isc::data::ConstElementPtr
  102. commandShutdownHandler(const std::string& command,
  103. isc::data::ConstElementPtr args);
  104. /// @brief handler for processing 'libreload' command
  105. ///
  106. /// This handler processes libreload command, which unloads all hook
  107. /// libraries and reloads them.
  108. ///
  109. /// @param command (parameter ignored)
  110. /// @param args (parameter ignored)
  111. ///
  112. /// @return status of the command
  113. isc::data::ConstElementPtr
  114. commandLibReloadHandler(const std::string& command,
  115. isc::data::ConstElementPtr args);
  116. /// @brief handler for processing 'config-reload' command
  117. ///
  118. /// This handler processes config-reload command, which processes
  119. /// configuration specified in args parameter.
  120. ///
  121. /// @param command (parameter ignored)
  122. /// @param args configuration to be processed
  123. ///
  124. /// @return status of the command
  125. isc::data::ConstElementPtr
  126. commandConfigReloadHandler(const std::string& command,
  127. isc::data::ConstElementPtr args);
  128. /// @brief handler for processing 'set-config' command
  129. ///
  130. /// This handler processes set-config command, which processes
  131. /// configuration specified in args parameter.
  132. /// @param command (parameter ignored)
  133. /// @param args configuration to be processed. Expected format:
  134. /// map containing Dhcp6 map that contains DHCPv6 server configuration.
  135. /// May also contain Logging map that specifies logging configuration.
  136. ///
  137. /// @return status of the command
  138. isc::data::ConstElementPtr
  139. commandSetConfigHandler(const std::string& command,
  140. isc::data::ConstElementPtr args);
  141. /// @brief Handler for processing 'leases-reclaim' command
  142. ///
  143. /// This handler processes leases-reclaim command, which triggers
  144. /// the leases reclamation immediately.
  145. /// No limit for processing time or number of processed leases applies.
  146. ///
  147. /// @param command (parameter ignored)
  148. /// @param args arguments map { "remove": <bool> }
  149. /// if true a lease is removed when it is reclaimed,
  150. /// if false its state is changed to "expired-reclaimed".
  151. ///
  152. /// @return status of the command (should be success unless args
  153. /// was not a Bool Element).
  154. isc::data::ConstElementPtr
  155. commandLeasesReclaimHandler(const std::string& command,
  156. isc::data::ConstElementPtr args);
  157. /// @brief Reclaims expired IPv6 leases and reschedules timer.
  158. ///
  159. /// This is a wrapper method for @c AllocEngine::reclaimExpiredLeases6.
  160. /// It reschedules the timer for leases reclamation upon completion of
  161. /// this method.
  162. ///
  163. /// @param max_leases Maximum number of leases to be reclaimed.
  164. /// @param timeout Maximum amount of time that the reclaimation routine
  165. /// may be processing expired leases, expressed in milliseconds.
  166. /// @param remove_lease A boolean value indicating if the lease should
  167. /// be removed when it is reclaimed (if true) or it should be left in the
  168. /// database in the "expired-reclaimed" state (if false).
  169. /// @param max_unwarned_cycles A number of consecutive processing cycles
  170. /// of expired leases, after which the system issues a warning if there
  171. /// are still expired leases in the database. If this value is 0, the
  172. /// warning is never issued.
  173. void reclaimExpiredLeases(const size_t max_leases, const uint16_t timeout,
  174. const bool remove_lease,
  175. const uint16_t max_unwarned_cycles);
  176. /// @brief Deletes reclaimed leases and reschedules the timer.
  177. ///
  178. /// This is a wrapper method for @c AllocEngine::deleteExpiredReclaimed6.
  179. /// It reschedules the timer for leases reclamation upon completion of
  180. /// this method.
  181. ///
  182. /// @param secs Minimum number of seconds after which a lease can be
  183. /// deleted.
  184. void deleteExpiredReclaimedLeases(const uint32_t secs);
  185. /// @brief Static pointer to the sole instance of the DHCP server.
  186. ///
  187. /// This is required for config and command handlers to gain access to
  188. /// the server. Some of them need to be static methods.
  189. static ControlledDhcpv6Srv* server_;
  190. /// @brief IOService object, used for all ASIO operations.
  191. isc::asiolink::IOService io_service_;
  192. /// @brief Instance of the @c TimerMgr.
  193. ///
  194. /// Shared pointer to the instance of timer @c TimerMgr is held here to
  195. /// make sure that the @c TimerMgr outlives instance of this class.
  196. TimerMgrPtr timer_mgr_;
  197. };
  198. }; // namespace isc::dhcp
  199. }; // namespace isc
  200. #endif