[ovs-dev] [PATCH 4/4] [RFC] ovn-nbctl: Make daemon mode more transparent.

Ben Pfaff blp at ovn.org
Tue Jul 24 19:53:31 UTC 2018


This has some flaws in the details; for example, command-line options
parsing is very inflexible.

Signed-off-by: Ben Pfaff <blp at ovn.org>
---
 NEWS                          |  6 ++---
 ovn/utilities/ovn-nbctl.8.xml | 61 ++++++++++++++++++++++++++++++++++++-------
 ovn/utilities/ovn-nbctl.c     | 37 ++++++++++++++++++++++++++
 3 files changed, 91 insertions(+), 13 deletions(-)

diff --git a/NEWS b/NEWS
index 2414b9199ae0..2b4fec48c128 100644
--- a/NEWS
+++ b/NEWS
@@ -31,10 +31,8 @@ Post-v2.9.0
      * ACL match conditions can now match on Port_Groups as well as address
        sets that are automatically generated by Port_Groups.  ACLs can be
        applied directly to Port_Groups as well.
-     * ovn-nbctl can now run as a daemon (long-lived, background process)
-       running commands in response to JSON-RPC requests received over a UNIX
-       socket. Requests to run commands can be sent using ovs-appctl tool, same
-       as for any other OVS/OVN daemon. See ovn-nbctl(8) for details.
+     * ovn-nbctl can now run as a daemon (long-lived, background process).
+       See ovn-nbctl(8) for details.
    - DPDK:
      * New 'check-dpdk' Makefile target to run a new system testsuite.
        See Testing topic for the details.
diff --git a/ovn/utilities/ovn-nbctl.8.xml b/ovn/utilities/ovn-nbctl.8.xml
index 2cd2fab304cd..3d30d8cd9a33 100644
--- a/ovn/utilities/ovn-nbctl.8.xml
+++ b/ovn/utilities/ovn-nbctl.8.xml
@@ -916,15 +916,62 @@
     <h1>Daemon Mode</h1>
 
     <p>
-      If <code>ovn-nbctl</code> is invoked with the <code>--detach</code>
-      option (see <code>Daemon Options</code>, below), it runs in the
-      background as a daemon and accepts commands from <code>ovs-appctl</code>
-      (or another JSON-RPC client) indefinitely.  The currently supported
-      commands are described below.
+      When it is invoked in the most ordinary way, <code>ovn-nbctl</code>
+      connects to an OVSDB server that hosts the northbound database, retrieves
+      a partial copy of the database that is complete enough to do its work,
+      sends a transaction request to the server, and receives and processes the
+      server's reply.  In common interactive use, this is fine, but if the
+      database is large, the step in which <code>ovn-nbctl</code> retrieves a
+      partial copy of the database can take a long time, which yields poor
+      performance overall.
     </p>
 
     <p>
+      To improve performance in such a case, <code>ovn-nbctl</code> offers a
+      "daemon mode," in which the user first starts <code>ovn-nbctl</code>
+      running in the background and afterward uses the daemon to execute
+      operations.  Over several <code>ovn-nbctl</code> command invocations,
+      this performs better overall because it retrieves a copy of the database
+      only once at the beginning, not once per program run.
+    </p>
+
+    <p>
+      Use the <code>--detach</code> option to start an <code>ovn-nbctl</code>
+      daemon.  With this option, <code>ovn-nbctl</code> prints the name of a
+      control socket to stdout.  The client should save this name in
+      environment variable <env>OVN_NB_DAEMON</env>.  Under the Bourne shell
+      this might be done like this:
+    </p>
+
+    <pre fixed="yes">
+      export OVN_NB_DAEMON=$(ovn-nbctl --pidfile --detach)
+    </pre>
+
+    <p>
+      When <env>OVN_NB_DAEMON</env> is set, <code>ovn-nbctl</code>
+      automatically and transparently uses the daemon to execute its commands.
+    </p>
+
+    <p>
+      When the daemon is no longer needed, kill it and unset the environment
+      variable, e.g.:
+    </p>
+
+    <pre fixed="yes">
+      kill $(cat /var/run/ovn-nbctl.pid)
+      unset OVN_NB_DAEMON
+    </pre>
+
+    <p>
+      Daemon mode is experimental.
+    </p>
 
+    <h2>Daemon Commands</h2>
+
+    <p>
+      Daemon mode is internally implemented using the same mechanism used by
+      <code>ovs-appctl</code>.  One may also use <code>ovs-appctl</code>
+      directly with the following commands:
     </p>
 
     <dl>
@@ -946,10 +993,6 @@
       <dd>Causes <code>ovn-nbctl</code> to gracefully terminate.</dd>
     </dl>
 
-    <p>
-      Daemon mode is considered experimental.
-    </p>
-
     <h1>Options</h1>
 
     <dl>
diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
index 3c3e582cb906..8b888294e495 100644
--- a/ovn/utilities/ovn-nbctl.c
+++ b/ovn/utilities/ovn-nbctl.c
@@ -24,6 +24,7 @@
 #include "db-ctl-base.h"
 #include "dirs.h"
 #include "fatal-signal.h"
+#include "jsonrpc.h"
 #include "openvswitch/json.h"
 #include "ovn/lib/acl-log.h"
 #include "ovn/lib/ovn-nb-idl.h"
@@ -123,6 +124,40 @@ main(int argc, char *argv[])
 
     nbctl_cmd_init();
 
+    char *socket_name = getenv("OVN_NB_DAEMON");
+    if (socket_name && socket_name[0]) {
+        struct jsonrpc *client;
+        int error = unixctl_client_create(socket_name, &client);
+        if (error) {
+            ctl_fatal("%s: could not connect to ovn-nb daemon (%s); "
+                      "unset OVN_NB_DAEMON to avoid using daemon",
+                      socket_name, ovs_strerror(error));
+        }
+
+        char *cmd_result;
+        char *cmd_error;
+        error = unixctl_client_transact(client, "run",
+                                        argc - optind, argv + optind,
+                                        &cmd_result, &cmd_error);
+        if (error) {
+            ctl_fatal("%s: transaction error (%s)",
+                      socket_name, ovs_strerror(error));
+        }
+
+        int exit_status;
+        if (cmd_error) {
+            exit_status = EXIT_FAILURE;
+            fputs(cmd_error, stderr);
+        } else {
+            exit_status = EXIT_SUCCESS;
+            fputs(cmd_result, stdout);
+        }
+        free(cmd_result);
+        free(cmd_error);
+        jsonrpc_close(client);
+        exit(exit_status);
+    }
+
     /* Parse command line. */
     char *args = process_escape_args(argv);
     shash_init(&local_options);
@@ -5019,6 +5054,8 @@ server_loop(struct ovsdb_idl *idl)
         ctl_fatal("failed to create unixctl server (%s)",
                   ovs_retval_to_string(error));
     }
+    puts(unixctl_server_get_path(server));
+    fflush(stdout);
     server_cmd_init(idl, &exiting);
 
     for (;;) {
-- 
2.16.1



More information about the dev mailing list