From: Lucas Alvares Gomes <lucasago...@gmail.com>

This commit adds the following command:

lsp-get-ls: Get the logical switch which the port belongs to.

This command is handy for scripting since there's no logical switch id
in the Logical_Switch_Port table.

Signed-off-by: Lucas Alvares Gomes <lucasago...@gmail.com>
---
 ovn/utilities/ovn-nbctl.8.xml |  5 +++++
 ovn/utilities/ovn-nbctl.c     | 26 ++++++++++++++++++++++++++
 tests/ovn-nbctl.at            | 14 ++++++++++++++
 3 files changed, 45 insertions(+)

diff --git a/ovn/utilities/ovn-nbctl.8.xml b/ovn/utilities/ovn-nbctl.8.xml
index 14cc02f53..a7a9c2701 100644
--- a/ovn/utilities/ovn-nbctl.8.xml
+++ b/ovn/utilities/ovn-nbctl.8.xml
@@ -476,6 +476,11 @@
         Get the configured DHCPv6 options for the logical port.
       </dd>
 
+      <dt><code>lsp-get-ls</code> <var>port</var></dt>
+      <dd>
+        Get the logical switch which the <var>port</var> belongs to.
+      </dd>
+
     </dl>
 
     <h1>Logical Router Commands</h1>
diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
index cca7dbaa1..8a72e9574 100644
--- a/ovn/utilities/ovn-nbctl.c
+++ b/ovn/utilities/ovn-nbctl.c
@@ -613,6 +613,7 @@ Logical switch port commands:\n\
   lsp-set-dhcpv6-options PORT [DHCP_OPTIONS_UUID]\n\
                             set dhcpv6 options for PORT\n\
   lsp-get-dhcpv6-options PORT  get the dhcpv6 options for PORT\n\
+  lsp-get-ls PORT           get the logical switch which the port belongs to\n\
 \n\
 Logical router commands:\n\
   lr-add [ROUTER]           create a logical router named ROUTER\n\
@@ -1876,6 +1877,30 @@ nbctl_lsp_get_dhcpv6_options(struct ctl_context *ctx)
     }
 }
 
+static void
+nbctl_lsp_get_ls(struct ctl_context *ctx)
+{
+    const char *id = ctx->argv[1];
+    const struct nbrec_logical_switch_port *lsp = NULL;
+
+    char *error = lsp_by_name_or_uuid(ctx, id, true, &lsp);
+    if (error) {
+        ctx->error = error;
+        return;
+    }
+
+    const struct nbrec_logical_switch *ls;
+    NBREC_LOGICAL_SWITCH_FOR_EACH(ls, ctx->idl) {
+        for (size_t i = 0; i < ls->n_ports; i++) {
+            if (ls->ports[i] == lsp) {
+                ds_put_format(&ctx->output, UUID_FMT " (%s)\n",
+                      UUID_ARGS(&ls->header_.uuid), ls->name);
+                break;
+            }
+        }
+    }
+}
+
 enum {
     DIR_FROM_LPORT,
     DIR_TO_LPORT
@@ -5115,6 +5140,7 @@ static const struct ctl_command_syntax nbctl_commands[] = 
{
       nbctl_lsp_set_dhcpv6_options, NULL, "", RW },
     { "lsp-get-dhcpv6-options", 1, 1, "PORT", NULL,
       nbctl_lsp_get_dhcpv6_options, NULL, "", RO },
+    { "lsp-get-ls", 1, 1, "PORT", NULL, nbctl_lsp_get_ls, NULL, "", RO },
 
     /* logical router commands. */
     { "lr-add", 0, 1, "[ROUTER]", NULL, nbctl_lr_add, NULL,
diff --git a/tests/ovn-nbctl.at b/tests/ovn-nbctl.at
index a4c8ed3c3..f884fc7d2 100644
--- a/tests/ovn-nbctl.at
+++ b/tests/ovn-nbctl.at
@@ -153,6 +153,20 @@ AT_CHECK([ovn-nbctl --may-exist lsp-add ls0 lp2 lp3 5], 
[1], [],
 
 dnl ---------------------------------------------------------------------
 
+OVN_NBCTL_TEST([ovn_nbctl_lsp_get_ls], [lsp get ls], [
+AT_CHECK([ovn-nbctl ls-add ls0])
+AT_CHECK([ovn-nbctl lsp-add ls0 lp0])
+
+AT_CHECK([ovn-nbctl lsp-get-ls lp0 | uuidfilt], [0], [dnl
+<0> (ls0)
+])
+
+AT_CHECK([ovn-nbctl lsp-get-ls lp1], [1], [],
+  [ovn-nbctl: lp1: port name not found
+])])
+
+dnl ---------------------------------------------------------------------
+
 OVN_NBCTL_TEST([ovn_nbctl_lport_addresses], [lport addresses], [
 AT_CHECK([ovn-nbctl ls-add ls0])
 AT_CHECK([ovn-nbctl lsp-add ls0 lp0])
-- 
2.20.1

_______________________________________________
dev mailing list
d...@openvswitch.org
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to