ctrl-channel.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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|2|3,
  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 or at least a
  169. failure to complete the requested action. Currently 1 is used as a generic
  170. error, 2 means that a command is not supported and 3 means that the
  171. requested operation was completed, but the requested object was not
  172. found. Additional error codes may be added in the future. For example a well
  173. formed command that requests a subnet that exists in server's configuration
  174. would return result 0. If the server encounters an error condition, it would
  175. return 1. If the command was asking for IPv6 subnet, but was sent to DHCPv4
  176. server, it would return 2. If the query was asking for a subnet-id and there
  177. is no subnet with such id, the result would be set to 3.</para>
  178. <para>
  179. The <command>text</command> field typically appears when result is non-zero
  180. and contains a description of the error encountered, but it often also appears
  181. for successful outcomes. The exact text is command specific, but in general
  182. uses plain English to describe the outcome of the command.
  183. <command>arguments</command> is a map of additional data values
  184. returned by the server which is specific to the command issued. The map is
  185. may be present, but that depends on specific command.
  186. </para>
  187. <note>
  188. <simpara>
  189. When sending commands via Control Agent, it is possible to specify
  190. multiple services at which the command is targetted. CA will forward this
  191. command to each service individually. Thus, the CA response to the
  192. controlling client will contain an array of individual responses.
  193. </simpara>
  194. </note>
  195. </section>
  196. <section id="ctrl-channel-client">
  197. <title>Using the Control Channel</title>
  198. <para>Kea development team is actively working on providing client applications
  199. which can be used to control the servers. These applications are, however, in the
  200. early stages of development and as of Kea 1.2.0 release have certain limitations.
  201. The easiest way to start interacting with the control API is to use common Unix/Linux tools
  202. such as <command>socat</command> and <command>curl</command>.</para>
  203. <para>In order to control the given Kea service via unix domain socket, use
  204. <command>socat</command> as follows:
  205. <screen>
  206. $ socat UNIX:/path/to/the/kea/socket -
  207. </screen>
  208. where <command>/path/to/the/kea/socket</command> is the path specified in the
  209. <command>Dhcp4/control-socket/socket-name</command> parameter in the Kea
  210. configuration file. Text passed to <command>socat</command>
  211. will be sent to Kea and the responses received from Kea printed to standard
  212. output. This approach communicates with the specific server directly and
  213. bypasses Control Agent.</para>
  214. <para>It is also easy to open UNIX socket programmatically. An example of
  215. such a simplistic client written in C is available in the Kea Developer's
  216. Guide, chapter Control Channel Overview, section Using Control Channel.</para>
  217. <para>In order to use Kea's RESTful API with <command>curl</command> you may
  218. use the following:
  219. <screen>
  220. $ curl -X POST -H "Content-Type: application/json" -d '{ "command": "config-get", "service": [ "dhcp4" ] }' http://ca.example.org:8000/
  221. </screen>
  222. This assumes that the Control Agent is running on host
  223. <filename>ca.example.org</filename> and runs RESTful service on port 8000.
  224. </para>
  225. </section>
  226. <section id="commands-common">
  227. <title>Commands Supported by Both the DHCPv4 and DHCPv6 Servers</title>
  228. <section id="command-build-report">
  229. <title>build-report</title>
  230. <para>
  231. The <emphasis>build-report</emphasis> command returns
  232. on the control channel what the command line
  233. <command>-W</command> argument displays, i.e. the embedded
  234. content of the <filename>config.report</filename> file.
  235. This command does not take any parameters.
  236. </para>
  237. <screen>
  238. {
  239. "command": "build-report"
  240. }
  241. </screen>
  242. </section> <!-- end of command-build-report -->
  243. <section id="command-config-get">
  244. <title>config-get</title>
  245. <para>The <emphasis>config-get</emphasis> command retrieves the current
  246. configuration used by the server. This command does not take any
  247. parameters. The configuration returned is roughly equal to the
  248. configuration that was loaded using -c command line option during server
  249. start-up or later set using config-set command. However, there may be
  250. certain differences. Comments are not retained. If the original
  251. configuration used file inclusion, the returned configuration will
  252. include all parameters from all the included files.</para>
  253. <para> Note that returned configuration is not redacted, i.e. it will
  254. contain database passwords in plain text if those were specified in the
  255. original configuration. Care should be taken to not expose the command
  256. channel to unprivileged users.</para>
  257. <para>
  258. An example command invocation looks like this:
  259. <screen>
  260. {
  261. "command": "config-get"
  262. }
  263. </screen>
  264. </para>
  265. </section> <!-- end of command-config-get -->
  266. <section id="command-config-reload">
  267. <title>config-reload</title>
  268. <para>The <emphasis>config-reload</emphasis> command instructs
  269. Kea to load again the configuration file that was used
  270. previously. This operation is useful if the configuration file
  271. has been changed by some external sources. For example, a
  272. sysadmin can tweak the configuration file and use this command
  273. to force Kea pick up the changes.</para>
  274. <para>Caution should be taken when mixing this with config-set
  275. commands. Kea remembers the location of the configuration file
  276. it was started with. This configuration can be significantly
  277. changed using config-set command. When config-reload is issued
  278. after config-set, Kea will attempt to reload its original
  279. configuration from the file, possibly losing all changes
  280. introduced using config-set or other commands.</para>
  281. <para><emphasis>config-reload</emphasis> does not take any parameters.
  282. An example command invocation looks like this:
  283. <screen>
  284. {
  285. "command": "config-reload"
  286. }
  287. </screen>
  288. </para>
  289. </section> <!-- end of command-config-reload -->
  290. <section id="command-config-test">
  291. <title>config-test</title>
  292. <para>
  293. The <emphasis>config-test</emphasis> command instructs the server to check
  294. whether the new configuration supplied in the command's arguments can
  295. be loaded. The supplied configuration is expected to be the full
  296. configuration for the target server along with an optional Logger
  297. configuration. As for the <command>-t</command> command some sanity checks
  298. are not performed so it is possible a configuration which successfully
  299. passes this command will still fail in <command>config-set</command>
  300. command or at launch time.
  301. The structure of the command is as follows:
  302. </para>
  303. <screen>
  304. {
  305. "command": "config-test",
  306. "arguments": {
  307. "&#60;server&#62;": {
  308. },
  309. "Logging": {
  310. }
  311. }
  312. }
  313. </screen>
  314. <para>
  315. where &#60;server&#62; is the configuration element name for a given server
  316. such as "Dhcp4" or "Dhcp6". For example:
  317. </para>
  318. <screen>
  319. {
  320. "command": "config-test",
  321. "arguments": {
  322. "Dhcp6": {
  323. :
  324. },
  325. "Logging": {
  326. :
  327. }
  328. }
  329. }
  330. </screen>
  331. <para>
  332. The server's response will contain a numeric code, "result" (0 for success,
  333. non-zero on failure), and a string, "text", describing the outcome:
  334. <screen>
  335. {"result": 0, "text": "Configuration seems sane..." }
  336. or
  337. {"result": 1, "text": "unsupported parameter: BOGUS (&#60;string&#62;:16:26)" }
  338. </screen>
  339. </para>
  340. </section> <!-- end of command-config-test -->
  341. <section id="command-config-write">
  342. <title>config-write</title>
  343. <para>The <emphasis>config-write</emphasis> command instructs Kea server
  344. to write its current configuration to a file on disk. It takes one
  345. optional argument called <emphasis>filename</emphasis> that specifies
  346. the name of the file to write configuration to. If not specified, the
  347. name used when starting Kea (passed as -c argument) will be
  348. used. If relative path is specified, Kea will write its files
  349. only in the directory it is running.</para>
  350. <para>
  351. An example command invocation looks like this:
  352. <screen>
  353. {
  354. "command": "config-write",
  355. "arguments": {
  356. "filename": "config-modified-2017-03-15.json"
  357. }
  358. }
  359. </screen>
  360. </para>
  361. </section> <!-- end of command-config-write -->
  362. <section id="command-leases-reclaim">
  363. <title>leases-reclaim</title>
  364. <para>
  365. The <emphasis>leases-reclaim</emphasis> command instructs the server to
  366. reclaim all expired leases immediately. The command has the following
  367. JSON syntax:
  368. <screen>
  369. {
  370. "command": "leases-reclaim",
  371. "arguments": {
  372. "remove": true
  373. }
  374. }
  375. </screen>
  376. </para>
  377. <para>The <emphasis>remove</emphasis> boolean parameter is mandatory
  378. and it indicates whether the reclaimed leases should be removed from
  379. the lease database (if true), or they should be left in the
  380. <emphasis>expired-reclaimed</emphasis> state (if false). The latter
  381. facilitates lease affinity, i.e. ability to re-assign expired lease to
  382. the same client which used this lease before. See
  383. <xref linkend="lease-affinity"/> for the details. Also, see
  384. <xref linkend="lease-reclamation"/> for the general information
  385. about the processing of expired leases (leases reclamation).</para>
  386. </section>
  387. <section id="command-libreload">
  388. <title>libreload</title>
  389. <para>
  390. The <emphasis>libreload</emphasis> command will first unload and then
  391. load all currently loaded hook libraries. This is primarily intended
  392. to allow one or more hook libraries to be replaced with newer versions
  393. without requiring Kea servers to be reconfigured or restarted. Note
  394. the hook libraries will be passed the same parameter values (if any)
  395. they were passed when originally loaded.
  396. <screen>
  397. {
  398. "command": "libreload",
  399. "arguments": { }
  400. }
  401. </screen>
  402. </para>
  403. <para>
  404. The server will respond with a result of 0 indicating success, or 1
  405. indicating a failure.
  406. </para>
  407. </section> <!-- end of command-libreload -->
  408. <section id="command-list-commands">
  409. <title>list-commands</title>
  410. <para>
  411. The <emphasis>list-commands</emphasis> command retrieves a list of all
  412. commands supported by the server. It does not take any arguments.
  413. An example command may look like this:
  414. <screen>
  415. {
  416. "command": "list-commands",
  417. "arguments": { }
  418. }
  419. </screen>
  420. </para>
  421. <para>
  422. The server will respond with a list of all supported commands. The
  423. arguments element will be a list of strings. Each string will convey
  424. one supported command.
  425. </para>
  426. </section> <!-- end of command-list-commands -->
  427. <section id="command-config-set">
  428. <title>config-set</title>
  429. <para>
  430. The <emphasis>config-set</emphasis> command instructs the server to replace
  431. its current configuration with the new configuration supplied in the
  432. command's arguments. The supplied configuration is expected to be the full
  433. configuration for the target server along with an optional Logger
  434. configuration. While optional, the Logger configuration is highly
  435. recommended as without it the server will revert to its default logging
  436. configuration. The structure of the command is as follows:
  437. </para>
  438. <screen>
  439. {
  440. "command": "config-set",
  441. "arguments": {
  442. "&#60;server&#62;": {
  443. },
  444. "Logging": {
  445. }
  446. }
  447. }
  448. </screen>
  449. <para>
  450. where &#60;server&#62; is the configuration element name for a given server
  451. such as "Dhcp4" or "Dhcp6". For example:
  452. </para>
  453. <screen>
  454. {
  455. "command": "config-set",
  456. "arguments": {
  457. "Dhcp6": {
  458. :
  459. },
  460. "Logging": {
  461. :
  462. }
  463. }
  464. }
  465. </screen>
  466. <para>
  467. If the new configuration proves to be invalid the server will retain
  468. its current configuration. Please note that the new configuration is
  469. retained in memory only. If the server is restarted or a configuration
  470. reload is triggered via a signal, the server will use the configuration
  471. stored in its configuration file.
  472. The server's response will contain a numeric code, "result" (0 for success,
  473. non-zero on failure), and a string, "text", describing the outcome:
  474. <screen>
  475. {"result": 0, "text": "Configuration successful." }
  476. or
  477. {"result": 1, "text": "unsupported parameter: BOGUS (&#60;string&#62;:16:26)" }
  478. </screen>
  479. </para>
  480. </section> <!-- end of command-config-set -->
  481. <section id="command-shutdown">
  482. <title>shutdown</title>
  483. <para>
  484. The <emphasis>shutdown</emphasis> command instructs the server to initiate
  485. its shutdown procedure. It is the equivalent of sending a SIGTERM signal
  486. to the process. This command does not take any arguments. An example
  487. command may look like this:
  488. <screen>
  489. {
  490. "command": "shutdown"
  491. }
  492. </screen>
  493. </para>
  494. <para>
  495. The server will respond with a confirmation that the shutdown procedure
  496. has been initiated.
  497. </para>
  498. </section> <!-- end of command-shutdown -->
  499. <section id="command-version-get">
  500. <title>version-get</title>
  501. <para>
  502. The <emphasis>version-get</emphasis> command returns on the control
  503. channel what the command line <command>-v</command> argument
  504. displays with in arguments the extended version, i.e., what
  505. the command line <command>-V</command> argument displays. This command
  506. does not take any parameters.
  507. </para>
  508. <screen>
  509. {
  510. "command": "version-get"
  511. }
  512. </screen>
  513. </section> <!-- end of command-version-get -->
  514. </section> <!-- end of commands supported by both servers -->
  515. <section id="agent-commands">
  516. <title>Commands Supported by Control Agent</title>
  517. <para>The following commands listed in <xref linkend="commands-common"/>
  518. are also supported by the Control Agent, i.e. when the
  519. <command>service</command> parameter is blank the commands are handled
  520. by the CA and they relate to the CA process itself:
  521. <itemizedlist>
  522. <listitem>
  523. <simpara>build-report</simpara>
  524. </listitem>
  525. <listitem>
  526. <simpara>config-get</simpara>
  527. </listitem>
  528. <listitem>
  529. <simpara>config-test</simpara>
  530. </listitem>
  531. <listitem>
  532. <simpara>config-write</simpara>
  533. </listitem>
  534. <listitem>
  535. <simpara>list-commands</simpara>
  536. </listitem>
  537. <listitem>
  538. <simpara>shutdown</simpara>
  539. </listitem>
  540. <listitem>
  541. <simpara>version-get</simpara>
  542. </listitem>
  543. </itemizedlist>
  544. </para>
  545. </section>
  546. </chapter>