[ovs-dev] [PATCH] [RFC] ovn: add start of ovn-nbctl

Russell Bryant rbryant at redhat.com
Mon Mar 16 16:08:08 UTC 2015


ovn-nbctl is intended to be a utility for manually interacting with
the OVN northbound db.  A real consumer of OVN, such as OpenStack,
should be using ovsdb directly.  However, a utility like this is
useful during development and testing of both ovn itself as well as
the OpenStack Neutron plugin.

Signed-off-by: Russell Bryant <rbryant at redhat.com>
---
 ovn/.gitignore      |   2 +
 ovn/automake.mk     |   8 ++-
 ovn/ovn-nbctl.8.xml |  18 +++++++
 ovn/ovn-nbctl.c     | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 168 insertions(+), 2 deletions(-)
 create mode 100644 ovn/ovn-nbctl.8.xml
 create mode 100644 ovn/ovn-nbctl.c

diff --git a/ovn/.gitignore b/ovn/.gitignore
index 011bb1c..f320a6b 100644
--- a/ovn/.gitignore
+++ b/ovn/.gitignore
@@ -12,3 +12,5 @@
 /ovn-nb-idl.c
 /ovn-nb-idl.h
 /ovn-nb-idl.ovsidl
+/ovn-nbctl
+/ovn-nbctl.8
diff --git a/ovn/automake.mk b/ovn/automake.mk
index cc1f8ae..37b0ca6 100644
--- a/ovn/automake.mk
+++ b/ovn/automake.mk
@@ -66,8 +66,8 @@ ovn/ovn-nb.5: \
 		$(srcdir)/ovn/ovn-nb.xml > $@.tmp && \
 	mv $@.tmp $@
 
-man_MANS += ovn/ovn-controller.8 ovn/ovn-architecture.7
-EXTRA_DIST += ovn/ovn-controller.8.in ovn/ovn-architecture.7.xml
+man_MANS += ovn/ovn-controller.8 ovn/ovn-architecture.7 ovn/ovn-nbctl.8
+EXTRA_DIST += ovn/ovn-controller.8.in ovn/ovn-architecture.7.xml ovn/ovn-nbctl.8.xml
 
 SUFFIXES += .xml
 %: %.xml
@@ -115,3 +115,7 @@ ovn_libovn_la_SOURCES = \
 	ovn/ovn-idl.h \
 	ovn/ovn-nb-idl.c \
 	ovn/ovn-nb-idl.h
+
+bin_PROGRAMS += ovn/ovn-nbctl
+ovn_ovn_nbctl_SOURCES = ovn/ovn-nbctl.c
+ovn_ovn_nbctl_LDADD = ovn/libovn.la ovsdb/libovsdb.la lib/libopenvswitch.la
diff --git a/ovn/ovn-nbctl.8.xml b/ovn/ovn-nbctl.8.xml
new file mode 100644
index 0000000..1745283
--- /dev/null
+++ b/ovn/ovn-nbctl.8.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manpage program="ovn-nbctl" section="8" title="ovn-nbctl">
+    <h1>Name</h1>
+    <p>ovn-nbctl -- Open Virtual Network northbound db management utility</p>
+
+    <h1>Synopsys</h1>
+    <p>ovn-nbctl [OPTIONS] COMMAND [ARG...]</p>
+
+    <h1>Description</h1>
+    <p>This utility can be used to manage the OVN northbound database.</p>
+
+    <h1>Commands</h1>
+    <p>add-lswitch</p>
+
+    <h1>Options</h1>
+    <p>-h | --help</p>
+    <p>-V | --version</p>
+</manpage>
diff --git a/ovn/ovn-nbctl.c b/ovn/ovn-nbctl.c
new file mode 100644
index 0000000..0084ae5
--- /dev/null
+++ b/ovn/ovn-nbctl.c
@@ -0,0 +1,142 @@
+/*
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+
+#include <getopt.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "command-line.h"
+#include "dirs.h"
+#include "fatal-signal.h"
+#include "ovn/ovn-nb-idl.h"
+#include "poll-loop.h"
+#include "util.h"
+#include "openvswitch/vlog.h"
+
+struct nbctl_context {
+    struct ovsdb_idl *idl;
+    struct ovsdb_idl_txn *txn;
+    const char *db;
+};
+
+static const struct ovs_cmdl_command *get_all_commands(void);
+
+static void
+usage(void)
+{
+    printf("\
+%s: OVN northbound DB management utility\n\
+usage: %s [OPTIONS] COMMAND [ARG...]\n\
+\n\
+Commands:\n\
+  add-lswitch               Create a logical switch\n\
+  list-lswitch              List configured logical switches\n\
+Options:\n\
+  -h, --help                display this help message\n\
+  -V, --version             display version information\n\
+", program_name, program_name);
+}
+
+static const char *
+default_db(void)
+{
+    static char *db;
+    if (!db) {
+        db = xasprintf("unix:%s/db.sock", ovs_rundir());
+    }
+    return db;
+}
+
+static void
+do_add_lswitch(struct ovs_cmdl_context *ctx)
+{
+    struct nbctl_context *nb_ctx = ctx->pvt;
+
+    nbrec_logical_switch_insert(nb_ctx->txn);
+}
+
+static void
+parse_options(int argc, char *argv[])
+{
+    static const struct option long_options[] = {
+        {"help", no_argument, NULL, 'h'},
+        {"version", no_argument, NULL, 'V'},
+        {NULL, 0, NULL, 0},
+    };
+    char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
+
+    for (;;) {
+        int c;
+
+        c = getopt_long(argc, argv, short_options, long_options, NULL);
+        if (c == -1) {
+            break;
+        }
+
+        switch (c) {
+        case 'h':
+            usage();
+
+        case 'V':
+            ovs_print_version(0, 0);
+            exit(EXIT_SUCCESS);
+
+        default:
+            break;
+        }
+    }
+
+    free(short_options);
+}
+
+static const struct ovs_cmdl_command all_commands[] = {
+    { "add-lswitch", "", 0, 0, do_add_lswitch },
+    { NULL, NULL, 0, 0, NULL },
+};
+
+static const struct ovs_cmdl_command *get_all_commands(void)
+{
+    return all_commands;
+}
+
+int
+main(int argc, char *argv[])
+{
+    struct ovs_cmdl_context ctx;
+    struct nbctl_context nb_ctx = { NULL, };
+    enum ovsdb_idl_txn_status txn_status;
+
+    fatal_ignore_sigpipe();
+    set_program_name(argv[0]);
+    parse_options(argc, argv);
+    nbrec_init();
+
+    nb_ctx.db = default_db();
+    nb_ctx.idl = ovsdb_idl_create(nb_ctx.db, &nbrec_idl_class, true, false);
+    ctx.pvt = &nb_ctx;
+    ctx.argc = argc - optind;
+    ctx.argv = argv + optind;
+
+    do {
+        /* XXX this loop needs to be a lot smarter */
+        ovsdb_idl_run(nb_ctx.idl);
+        nb_ctx.txn = ovsdb_idl_txn_create(nb_ctx.idl);
+        ovs_cmdl_run_command(&ctx, get_all_commands());
+        txn_status = ovsdb_idl_txn_commit_block(nb_ctx.txn);
+    } while (txn_status == TXN_TRY_AGAIN);
+
+    exit(0);
+}
-- 
2.1.0




More information about the dev mailing list