ctrl-channel.xml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
  4. <!ENTITY mdash "&#x2014;" >
  5. <!ENTITY % version SYSTEM "version.ent">
  6. %version;
  7. ]>
  8. <chapter id="ctrl-channel">
  9. <title>Management API</title>
  10. <para>A classic approach to daemon configuration assumes that
  11. the server's configuration is stored in configuration files
  12. and, when the configuration is changed, the daemon is restarted.
  13. This approach has the significant disadvantage of introducing periods
  14. of downtime, when client traffic is not handled. Another risk
  15. is that if the new configuration is invalid for whatever reason,
  16. the server may refuse to start, which will further extend the
  17. downtime period until the issue is resolved.</para>
  18. <para>To avoid such problems, both the DHCPv4 and DHCPv6 servers
  19. include support for a mechanism that allows
  20. on-line reconfiguration without requiring server shutdown.
  21. Both servers can be instructed to open control sockets, which
  22. is a communication channel. The server is able to receive
  23. commands on that channel, act on them and report back status.
  24. While the set of commands in Kea 1.2.0 is limited,
  25. the number is expected to grow over time.</para>
  26. <para>The DHCPv4 and DHCPv6 servers receive commands over the
  27. unix domain sockets. The details how to configure these sockets,
  28. see <xref linkend="dhcp4-ctrl-channel" /> and <xref
  29. linkend="dhcp6-ctrl-channel" />. While it is possible control
  30. the servers directly using unix domain sockets it requires that
  31. the controlling client be running on the same machine as
  32. the server. In order to connect remotely SSH is usually used to
  33. connect to the controlled machine.</para>
  34. <para>The network administrators usually prefer using some form
  35. of a RESTful API to control the servers, rather than using unix
  36. domain sockets directly. Therefore, as of Kea 1.2.0 release,
  37. Kea includes a new component called Control Agent (or CA) which
  38. exposes a RESTful API to the controlling clients and can forward
  39. commands to the respective Kea services over the unix domain
  40. sockets. The CA configuration has been described in
  41. <xref linkend="agent-configuration"/>.</para>
  42. <para>The HTTP requests received by the CA contain the control
  43. commands encapsulated within HTTP requests. Simply speaking,
  44. the CA is responsible for stripping the HTTP layer from the
  45. received commands and forwarding the commands in a JSON format
  46. over the unix domain sockets to respective services. Because the
  47. CA receives commands for all services it requires additional
  48. "forwarding" information to be included in the client's messages.
  49. This "forwarding" information is carried within the
  50. <command>service</command> parameter of the received command.
  51. If the <command>service</command> parameter is not included or if
  52. the parameter is a blank list the CA will assume that the control
  53. command is targetted at the CA itself and will try to handle
  54. it on its own.
  55. </para>
  56. <para>Control connections over both HTTP and unix domain sockets are
  57. guarded with timeouts. The default timeout value is set to 10s
  58. and is not configurable. The timeout configuration will be
  59. implemented in the future.
  60. </para>
  61. <note>
  62. <simpara>Kea 1.2.0 release and earlier had a limitation of 64kB
  63. on the maximum size of a command and a response sent over the unix
  64. domain socket. This limitation has been removed in Kea 1.3.0
  65. release.
  66. </simpara>
  67. </note>
  68. <section id="ctrl-channel-syntax">
  69. <title>Data Syntax</title>
  70. <para>Communication over the control channel is conducted using JSON
  71. structures. If configured, Kea will open a socket and listen
  72. for incoming connections. A process connecting to this socket
  73. is expected to send JSON commands structured as follows:
  74. <screen>
  75. {
  76. "command": "foo",
  77. "service": [ "dhcp4" ]
  78. "arguments": {
  79. "param1": "value1",
  80. "param2": "value2",
  81. ...
  82. }
  83. }
  84. </screen>
  85. The same command sent over the RESTful interface to the CA will have
  86. the following structure.
  87. <screen>
  88. POST / HTTP/1.1\r\n
  89. Content-Type: application/json\r\n
  90. Content-Length: 147\r\n\r\n
  91. {
  92. "command": "foo",
  93. "service": [ "dhcp4" ]
  94. "arguments": {
  95. "param1": "value1",
  96. "param2": "value2",
  97. ...
  98. }
  99. }
  100. </screen>
  101. <command>command</command> is the name of command to execute and
  102. is mandatory. <command>arguments</command> is a map of parameters
  103. required to carry out the given command. The exact content and
  104. format of the map is command specific.</para>
  105. <para>
  106. <command>service</command> is a list of the servers at which the control
  107. command is targetted. In the example above, the control command is
  108. targetted at the DHCPv4 server. In most cases, the CA will simply forward this
  109. command to the DHCPv4 server for processing via unix domain socket.
  110. Sometimes, the command including a service value may also be processed by the
  111. CA, if the CA is running a hooks library which handles such command for the
  112. given server. As an example, the hooks library attached to the CA
  113. may perform some operations on the database (like adding host reservations,
  114. modifying leases etc.). An advantage of performing DHCPv4 specific
  115. administrative operations in the CA rather than forwarding it to
  116. the DHCPv4 server is the ability to perform these operations without
  117. disrupting the DHCPv4 service (DHCPv4 server doesn't have to stop
  118. processing DHCP messages to apply changes to the database). Nevertheless,
  119. these situations are rather rare and, in most cases, when the
  120. <command>service</command> parameter contains a name of the service
  121. the commands are simply forwarded by the CA. The forwarded command
  122. includes the <command>service</command> parameter but this parameter
  123. is ignored by the receiving server. This parameter is only meaningful
  124. to the CA.
  125. </para>
  126. <para>
  127. If the command received by the CA does not include a <command>service</command>
  128. parameter or this list is empty, the CA will simply process this message
  129. on its own. For example, the <command>config-get</command> command which
  130. doesn't include service parameter will return Control Agent's own
  131. configuration. The <command>config-get</command> including a service
  132. value "dhcp4" will be forwarded to the DHCPv4 server and will return
  133. DHCPv4 server's configuration and so on.
  134. </para>
  135. <para>
  136. The following list contains a mapping of the values carried within the
  137. <command>service</command> parameter to the servers to which the commands
  138. are forwarded:
  139. <itemizedlist>
  140. <listitem>
  141. <simpara><command>dhcp4</command> - the command is forwarded to the
  142. <command>kea-dhcp4</command> server,</simpara>
  143. </listitem>
  144. <listitem>
  145. <simpara><command>dhcp6</command> - the command is forwarded to the
  146. <command>kea-dhcp6</command> server,</simpara>
  147. </listitem>
  148. <listitem>
  149. <simpara><command>d2</command> - the command is forwarded to the
  150. <command>kea-d2</command> server.</simpara>
  151. </listitem>
  152. </itemizedlist>
  153. </para>
  154. <para>The server processing the incoming command will send a response of
  155. the form:
  156. <screen>
  157. {
  158. "result": 0|1,
  159. "text": "textual description",
  160. "arguments": {
  161. "argument1": "value1",
  162. "argument2": "value2",
  163. ...
  164. }
  165. }
  166. </screen>
  167. <command>result</command> indicates the outcome of the command. A value of 0
  168. means success while any non-zero value designates an error. Currently 1 is
  169. used as a generic error, but additional error codes may be added in the
  170. future. The <command>text</command> field typically appears when result is
  171. non-zero and contains a description of the error encountered, but it may
  172. also appear for successful results (that is command specific).
  173. <command>arguments</command> is a map of additional data values returned by
  174. the server which is specific to the command issued. The map is always present, even
  175. if it contains no data values.</para>
  176. <note>
  177. <simpara>
  178. When sending commands via Control Agent, it is possible to specify
  179. multiple services at which the command is targetted. CA will forward this
  180. command to each service individually. Thus, the CA response to the
  181. controlling client will contain an array of individual responses.
  182. </simpara>
  183. </note>
  184. </section>
  185. <section id="ctrl-channel-client">
  186. <title>Using the Control Channel</title>
  187. <para>Kea development team is actively working on providing client applications
  188. which can be used to control the servers. These applications are, however, in the
  189. early stages of development and as of Kea 1.2.0 release have certain limitations.
  190. The easiest way to start playing with the control API is to use common Unix/Linux tools
  191. such as <command>socat</command> and <command>curl</command>.</para>
  192. <para>In order to control the given Kea service via unix domain socket, use
  193. <command>socat</command> as follows:
  194. <screen>
  195. $ socat UNIX:/path/to/the/kea/socket -
  196. </screen>
  197. where <command>/path/to/the/kea/socket</command> is the path specified in the
  198. <command>Dhcp4/control-socket/socket-name</command> parameter in the Kea
  199. configuration file. Text passed to <command>socat</command>
  200. will be sent to Kea and the responses received from Kea printed to standard output.</para>
  201. <para>It is also easy to open UNIX socket programmatically. An example of
  202. such a simplistic client written in C is available in the Kea Developer's
  203. Guide, chapter Control Channel Overview, section Using Control Channel.</para>
  204. <para>In order to use Kea's RESTful API with <command>curl</command> try the
  205. following:
  206. <screen>
  207. $ curl -X POST -H "Content-Type: application/json" -d '{ "command": "config-get", "service": [ "dhcp4" ] }' http://ca.example.org:8000/
  208. </screen>
  209. This assumes that the Control Agent is running on host
  210. <filename>ca.example.org</filename> and runs RESTful service on port 8000.
  211. </para>
  212. </section>
  213. <section id="commands-common">
  214. <title>Commands Supported by Both the DHCPv4 and DHCPv6 Servers</title>
  215. <section id="command-build-report">
  216. <title>build-report</title>
  217. <para>
  218. The <emphasis>build-report</emphasis> command returns
  219. on the control channel what the command line
  220. <command>-W</command> argument displays, i.e. the embedded
  221. content of the <filename>config.report</filename> file.
  222. This command does not take any parameters.
  223. </para>
  224. <screen>
  225. {
  226. "command": "build-report"
  227. }
  228. </screen>
  229. </section> <!-- end of command-build-report -->
  230. <section id="command-config-get">
  231. <title>config-get</title>
  232. <para>The <emphasis>config-get</emphasis> command retrieves the current
  233. configuration used by the server. This command does not take any
  234. parameters. The configuration returned is roughly equal to the
  235. configuration that was loaded using -c command line option during server
  236. start-up or later set using config-set command. However, there may be
  237. certain differences. Comments are not retained. If the original
  238. configuration used file inclusion, the returned configuration will
  239. include all parameters from all the included files.</para>
  240. <para> Note that returned configuration is not redacted, i.e. it will
  241. contain database passwords in plain text if those were specified in the
  242. original configuration. Care should be taken to not expose the command
  243. channel to unprivileged users.</para>
  244. <para>
  245. An example command invocation looks like this:
  246. <screen>
  247. {
  248. "command": "config-get"
  249. }
  250. </screen>
  251. </para>
  252. </section> <!-- end of command-config-get -->
  253. <section id="command-config-reload">
  254. <title>config-reload</title>
  255. <para>The <emphasis>config-reload</emphasis> command instructs
  256. Kea to load again the configuration file that was used
  257. previously. This operation is useful if the configuration file
  258. has been changed by some external sources. For example, a
  259. sysadmin can tweak the configuration file and use this command
  260. to force Kea pick up the changes.</para>
  261. <para>Caution should be taken when mixing this with config-set
  262. commands. Kea remembers the location of the configuration file
  263. it was started with. This configuration can be significantly
  264. changed using config-set command. When config-reload is issued
  265. after config-set, Kea will attempt to reload its original
  266. configuration from the file, possibly losing all changes
  267. introduced using config-set or other commands.</para>
  268. <para><emphasis>config-reload</emphasis> does not take any parameters.
  269. An example command invocation looks like this:
  270. <screen>
  271. {
  272. "command": "config-reload"
  273. }
  274. </screen>
  275. </para>
  276. </section> <!-- end of command-config-reload -->
  277. <section id="command-config-test">
  278. <title>config-test</title>
  279. <para>
  280. The <emphasis>config-test</emphasis> command instructs the server to check
  281. whether the new configuration supplied in the command's arguments can
  282. be loaded. The supplied configuration is expected to be the full
  283. configuration for the target server along with an optional Logger
  284. configuration. As for the <command>-t</command> command some sanity checks
  285. are not performed so it is possible a configuration which successfully
  286. passes this command will still fail in <command>config-set</command>
  287. command or at launch time.
  288. The structure of the command is as follows:
  289. </para>
  290. <screen>
  291. {
  292. "command": "config-test",
  293. "arguments": {
  294. "&#60;server&#62;": {
  295. },
  296. "Logging": {
  297. }
  298. }
  299. }
  300. </screen>
  301. <para>
  302. where &#60;server&#62; is the configuration element name for a given server
  303. such as "Dhcp4" or "Dhcp6". For example:
  304. </para>
  305. <screen>
  306. {
  307. "command": "config-test",
  308. "arguments": {
  309. "Dhcp6": {
  310. :
  311. },
  312. "Logging": {
  313. :
  314. }
  315. }
  316. }
  317. </screen>
  318. <para>
  319. The server's response will contain a numeric code, "result" (0 for success,
  320. non-zero on failure), and a string, "text", describing the outcome:
  321. <screen>
  322. {"result": 0, "text": "Configuration seems sane..." }
  323. or
  324. {"result": 1, "text": "unsupported parameter: BOGUS (&#60;string&#62;:16:26)" }
  325. </screen>
  326. </para>
  327. </section> <!-- end of command-config-test -->
  328. <section id="command-config-write">
  329. <title>config-write</title>
  330. <para>The <emphasis>config-write</emphasis> command instructs Kea server
  331. to write its current configuration to a file on disk. It takes one
  332. optional argument called <emphasis>filename</emphasis> that specifies
  333. the name of the file to write configuration to. If not specified, the
  334. name used when starting Kea (passed as -c argument) will be
  335. used. If relative path is specified, Kea will write its files
  336. only in the directory it is running.</para>
  337. <para>
  338. An example command invocation looks like this:
  339. <screen>
  340. {
  341. "command": "config-write",
  342. "arguments": {
  343. "filename": "config-modified-2017-03-15.json"
  344. }
  345. }
  346. </screen>
  347. </para>
  348. </section> <!-- end of command-config-write -->
  349. <section id="command-leases-reclaim">
  350. <title>leases-reclaim</title>
  351. <para>
  352. The <emphasis>leases-reclaim</emphasis> command instructs the server to
  353. reclaim all expired leases immediately. The command has the following
  354. JSON syntax:
  355. <screen>
  356. {
  357. "command": "leases-reclaim",
  358. "arguments": {
  359. "remove": true
  360. }
  361. }
  362. </screen>
  363. </para>
  364. <para>The <emphasis>remove</emphasis> boolean parameter is mandatory
  365. and it indicates whether the reclaimed leases should be removed from
  366. the lease database (if true), or they should be left in the
  367. <emphasis>expired-reclaimed</emphasis> state (if false). The latter
  368. facilitates lease affinity, i.e. ability to re-assign expired lease to
  369. the same client which used this lease before. See
  370. <xref linkend="lease-affinity"/> for the details. Also, see
  371. <xref linkend="lease-reclamation"/> for the general information
  372. about the processing of expired leases (leases reclamation).</para>
  373. </section>
  374. <section id="command-libreload">
  375. <title>libreload</title>
  376. <para>
  377. The <emphasis>libreload</emphasis> command will first unload and then
  378. load all currently loaded hook libraries. This is primarily intended
  379. to allow one or more hook libraries to be replaced with newer versions
  380. without requiring Kea servers to be reconfigured or restarted. Note
  381. the hook libraries will be passed the same parameter values (if any)
  382. they were passed when originally loaded.
  383. <screen>
  384. {
  385. "command": "libreload",
  386. "arguments": { }
  387. }
  388. </screen>
  389. </para>
  390. <para>
  391. The server will respond with a result of 0 indicating success, or 1
  392. indicating a failure.
  393. </para>
  394. </section> <!-- end of command-libreload -->
  395. <section id="command-list-commands">
  396. <title>list-commands</title>
  397. <para>
  398. The <emphasis>list-commands</emphasis> command retrieves a list of all
  399. commands supported by the server. It does not take any arguments.
  400. An example command may look like this:
  401. <screen>
  402. {
  403. "command": "list-commands",
  404. "arguments": { }
  405. }
  406. </screen>
  407. </para>
  408. <para>
  409. The server will respond with a list of all supported commands. The
  410. arguments element will be a list of strings. Each string will convey
  411. one supported command.
  412. </para>
  413. </section> <!-- end of command-list-commands -->
  414. <section id="command-config-set">
  415. <title>config-set</title>
  416. <para>
  417. The <emphasis>config-set</emphasis> command instructs the server to replace
  418. its current configuration with the new configuration supplied in the
  419. command's arguments. The supplied configuration is expected to be the full
  420. configuration for the target server along with an optional Logger
  421. configuration. While optional, the Logger configuration is highly
  422. recommended as without it the server will revert to its default logging
  423. configuration. The structure of the command is as follows:
  424. </para>
  425. <screen>
  426. {
  427. "command": "config-set",
  428. "arguments": {
  429. "&#60;server&#62;": {
  430. },
  431. "Logging": {
  432. }
  433. }
  434. }
  435. </screen>
  436. <para>
  437. where &#60;server&#62; is the configuration element name for a given server
  438. such as "Dhcp4" or "Dhcp6". For example:
  439. </para>
  440. <screen>
  441. {
  442. "command": "config-set",
  443. "arguments": {
  444. "Dhcp6": {
  445. :
  446. },
  447. "Logging": {
  448. :
  449. }
  450. }
  451. }
  452. </screen>
  453. <para>
  454. If the new configuration proves to be invalid the server will retain
  455. its current configuration. Please note that the new configuration is
  456. retained in memory only. If the server is restarted or a configuration
  457. reload is triggered via a signal, the server will use the configuration
  458. stored in its configuration file.
  459. The server's response will contain a numeric code, "result" (0 for success,
  460. non-zero on failure), and a string, "text", describing the outcome:
  461. <screen>
  462. {"result": 0, "text": "Configuration successful." }
  463. or
  464. {"result": 1, "text": "unsupported parameter: BOGUS (&#60;string&#62;:16:26)" }
  465. </screen>
  466. </para>
  467. </section> <!-- end of command-config-set -->
  468. <section id="command-shutdown">
  469. <title>shutdown</title>
  470. <para>
  471. The <emphasis>shutdown</emphasis> command instructs the server to initiate
  472. its shutdown procedure. It is the equivalent of sending a SIGTERM signal
  473. to the process. This command does not take any arguments. An example
  474. command may look like this:
  475. <screen>
  476. {
  477. "command": "shutdown"
  478. }
  479. </screen>
  480. </para>
  481. <para>
  482. The server will respond with a confirmation that the shutdown procedure
  483. has been initiated.
  484. </para>
  485. </section> <!-- end of command-shutdown -->
  486. <section id="command-version-get">
  487. <title>version-get</title>
  488. <para>
  489. The <emphasis>version-get</emphasis> command returns on the control
  490. channel what the command line <command>-v</command> argument
  491. displays with in arguments the extended version, i.e., what
  492. the command line <command>-V</command> argument displays. This command
  493. does not take any parameters.
  494. </para>
  495. <screen>
  496. {
  497. "command": "version-get"
  498. }
  499. </screen>
  500. </section> <!-- end of command-version-get -->
  501. </section> <!-- end of commands supported by both servers -->
  502. <section id="agent-commands">
  503. <title>Commands Supported by Control Agent</title>
  504. <para>The following commands listed in <xref linkend="commands-common"/>
  505. are also supported by the Control Agent, i.e. when the
  506. <command>service</command> parameter is blank the commands are handled
  507. by the CA and they relate to the CA process itself:
  508. <itemizedlist>
  509. <listitem>
  510. <simpara>build-report</simpara>
  511. </listitem>
  512. <listitem>
  513. <simpara>config-get</simpara>
  514. </listitem>
  515. <listitem>
  516. <simpara>config-test</simpara>
  517. </listitem>
  518. <listitem>
  519. <simpara>config-write</simpara>
  520. </listitem>
  521. <listitem>
  522. <simpara>list-commands</simpara>
  523. </listitem>
  524. <listitem>
  525. <simpara>shutdown</simpara>
  526. </listitem>
  527. <listitem>
  528. <simpara>version-get</simpara>
  529. </listitem>
  530. </itemizedlist>
  531. </para>
  532. </section>
  533. </chapter>