ctrl_dhcp4_srv.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (C) 2012-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 CTRL_DHCPV4_SRV_H
  7. #define CTRL_DHCPV4_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 <dhcp4/dhcp4_srv.h>
  13. namespace isc {
  14. namespace dhcp {
  15. /// @brief Controlled version of the DHCPv4 server
  16. ///
  17. /// This is a class that is responsible for DHCPv4 server being controllable,
  18. /// by reading configuration file from disk.
  19. class ControlledDhcpv4Srv : public isc::dhcp::Dhcpv4Srv {
  20. public:
  21. /// @brief Constructor
  22. ///
  23. /// @param port UDP port to be opened for DHCP traffic
  24. ControlledDhcpv4Srv(uint16_t port = DHCP4_SERVER_PORT);
  25. /// @brief Destructor.
  26. ~ControlledDhcpv4Srv();
  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 Dhcpv4Srv object
  40. /// is destroyed. Currently it is a no-op.
  41. void cleanup();
  42. /// @brief Initiates shutdown procedure for the whole DHCPv4 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 Dhcpv4Srv
  80. ///
  81. /// @return server instance (may return NULL, if called before server is spawned)
  82. static ControlledDhcpv4Srv* 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 'leases-reclaim' command
  129. ///
  130. /// This handler processes leases-reclaim command, which triggers
  131. /// the leases reclamation immediately.
  132. /// No limit for processing time or number of processed leases applies.
  133. ///
  134. /// @param command (parameter ignored)
  135. /// @param args arguments map { "remove": <bool> }
  136. /// if true a lease is removed when it is reclaimed,
  137. /// if false its state is changed to "expired-reclaimed".
  138. ///
  139. /// @return status of the command (should be success unless args
  140. /// was not a Bool Element).
  141. isc::data::ConstElementPtr
  142. commandLeasesReclaimHandler(const std::string& command,
  143. isc::data::ConstElementPtr args);
  144. /// @brief Reclaims expired IPv4 leases and reschedules timer.
  145. ///
  146. /// This is a wrapper method for @c AllocEngine::reclaimExpiredLeases4.
  147. /// It reschedules the timer for leases reclamation upon completion of
  148. /// this method.
  149. ///
  150. /// @param max_leases Maximum number of leases to be reclaimed.
  151. /// @param timeout Maximum amount of time that the reclaimation routine
  152. /// may be processing expired leases, expressed in milliseconds.
  153. /// @param remove_lease A boolean value indicating if the lease should
  154. /// be removed when it is reclaimed (if true) or it should be left in the
  155. /// database in the "expired-reclaimed" state (if false).
  156. /// @param max_unwarned_cycles A number of consecutive processing cycles
  157. /// of expired leases, after which the system issues a warning if there
  158. /// are still expired leases in the database. If this value is 0, the
  159. /// warning is never issued.
  160. void reclaimExpiredLeases(const size_t max_leases, const uint16_t timeout,
  161. const bool remove_lease,
  162. const uint16_t max_unwarned_cycles);
  163. /// @brief Deletes reclaimed leases and reschedules the timer.
  164. ///
  165. /// This is a wrapper method for @c AllocEngine::deleteExpiredReclaimed4.
  166. /// It reschedules the timer for leases reclamation upon completion of
  167. /// this method.
  168. ///
  169. /// @param secs Minimum number of seconds after which a lease can be
  170. /// deleted.
  171. void deleteExpiredReclaimedLeases(const uint32_t secs);
  172. /// @brief Static pointer to the sole instance of the DHCP server.
  173. ///
  174. /// This is required for config and command handlers to gain access to
  175. /// the server
  176. static ControlledDhcpv4Srv* server_;
  177. /// @brief IOService object, used for all ASIO operations.
  178. isc::asiolink::IOService io_service_;
  179. /// @brief Instance of the @c TimerMgr.
  180. ///
  181. /// Shared pointer to the instance of timer @c TimerMgr is held here to
  182. /// make sure that the @c TimerMgr outlives instance of this class.
  183. TimerMgrPtr timer_mgr_;
  184. };
  185. }; // namespace isc::dhcp
  186. }; // namespace isc
  187. #endif