Add add/del-library commands.

To add a library, with optional configuration 'x' and 'y':
ovs-vsctl add-library <LIB> x=1 y=2

To remove a library:
ovs-vsctl del-library <LIB>

To see the available libraries:
ovs-vsctl list library

Signed-off-by: Eli Britstein <[email protected]>
---
 utilities/ovs-vsctl.c | 89 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 89 insertions(+)

diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c
index d90db934b..8cc75c7be 100644
--- a/utilities/ovs-vsctl.c
+++ b/utilities/ovs-vsctl.c
@@ -783,6 +783,9 @@ pre_get_info(struct ctl_context *ctx)
 
     ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_ofport);
     ovsdb_idl_add_column(ctx->idl, &ovsrec_interface_col_error);
+
+    ovsdb_idl_add_column(ctx->idl, &ovsrec_open_vswitch_col_libraries);
+    ovsdb_idl_add_column(ctx->idl, &ovsrec_library_col_name);
 }
 
 static void
@@ -1885,6 +1888,86 @@ cmd_br_get_external_id(struct ctl_context *ctx)
     }
 }
 
+static void
+cmd_add_library(struct ctl_context *ctx)
+{
+    struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
+    struct smap config = SMAP_INITIALIZER(&config);
+    struct ovsrec_library **lib_list;
+    const char *name = ctx->argv[1];
+    struct ovsrec_library *lib;
+    size_t n_libs;
+
+    for (int i = 2; i < ctx->argc; i++) {
+        const char *arg = ctx->argv[i];
+        char *key, *val;
+
+        key = xstrdup(arg);
+        val = strchr(key, '=');
+
+        if (val) {
+            *val = '\0';
+            val++;
+            smap_add(&config, key, val);
+        }
+        free(key);
+    }
+
+    lib = ovsrec_library_insert(ctx->txn);
+
+    ovsrec_library_set_name(lib, name);
+    ovsrec_library_set_config(lib, &config);
+    smap_destroy(&config);
+
+    n_libs = vsctl_ctx->ovs->n_libraries;
+    lib_list = xmalloc(sizeof *lib_list * (n_libs + 1));
+    memcpy(lib_list, vsctl_ctx->ovs->libraries, sizeof *lib_list * n_libs);
+    lib_list[n_libs] = lib;
+    ovsrec_open_vswitch_set_libraries(vsctl_ctx->ovs, lib_list, n_libs + 1);
+    free(lib_list);
+}
+
+static void
+cmd_del_library(struct ctl_context *ctx)
+{
+    bool must_exist = !shash_find(&ctx->options, "--if-exists");
+    struct vsctl_context *vsctl_ctx = vsctl_context_cast(ctx);
+    struct ovsrec_library **orig_lib_list, **new_lib_list;
+    const char *name = ctx->argv[1];
+    struct ovsrec_library *lib;
+    size_t lib_list_idx;
+    size_t n_libs;
+
+    n_libs = vsctl_ctx->ovs->n_libraries;
+    orig_lib_list = vsctl_ctx->ovs->libraries;
+    new_lib_list = xmalloc(sizeof *new_lib_list * (n_libs - 1));
+    lib = NULL;
+    lib_list_idx = 0;
+
+    for (size_t i = 0; i < n_libs; i++) {
+        if (strcmp(orig_lib_list[i]->name, name)) {
+            new_lib_list[lib_list_idx++] = orig_lib_list[i];
+            continue;
+        }
+
+        lib = orig_lib_list[i];
+    }
+
+    if (!lib) {
+        free(new_lib_list);
+        if (must_exist) {
+            ctl_fatal("no library named %s", name);
+        }
+        return;
+    }
+
+    ovsrec_open_vswitch_set_libraries(vsctl_ctx->ovs, new_lib_list,
+                                      n_libs - 1);
+    free(new_lib_list);
+
+    ovsrec_library_delete(lib);
+}
+
 static void
 cmd_list_ports(struct ctl_context *ctx)
 {
@@ -3248,6 +3331,12 @@ static const struct ctl_command_syntax vsctl_commands[] 
= {
     {"br-get-external-id", 1, 2, "BRIDGE [KEY]", pre_cmd_br_get_external_id,
      cmd_br_get_external_id, NULL, "", RO},
 
+    /* Library commands. */
+    {"add-library", 1, INT_MAX, "LIBRARY [options:KEY=VALUE]...", pre_get_info,
+     cmd_add_library, NULL, "", RW},
+    {"del-library", 1, 1, "LIBRARY", pre_get_info, cmd_del_library,
+     NULL, "--if-exists", RW},
+
     /* Port commands. */
     {"list-ports", 1, 1, "BRIDGE", pre_get_info, cmd_list_ports, NULL, "",
      RO},
-- 
2.34.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to