ctrl-channel.xml 22 KB

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