Load balancer health checks can only be configured with generic database
commands today, e.g.:

  ovn-nbctl --id=@hc create Load_Balancer_Health_Check \
      vip="10.0.0.10\:80" options:interval=5 \
      -- add Load_Balancer lb0 health_check @hc

This is error prone: a "vip" that doesn't exactly match a key of the
load balancer's "vips" column is silently ignored by ovn-northd.

Add "lb-hc-add", "lb-hc-del" and "lb-hc-list", similar to the existing
"lsp-hc-*" commands:

  ovn-nbctl lb-hc-add lb0 10.0.0.10:80 interval=5 failure_count=3
  ovn-nbctl lb-hc-list lb0
  ovn-nbctl lb-hc-del lb0 10.0.0.10:80

"lb-hc-add" normalizes the VIP the same way as "lb-add" and checks that
it is part of the load balancer and has a port.  Options are limited to
interval, timeout, success_count and failure_count with values that
ovn-controller can handle.  Template and SCTP load balancers are
rejected because ovn-northd ignores their health checks.  "lb-hc-del"
also works when the VIP was already removed from the load balancer.

Assisted-by: Claude Opus 5.5, Claude Code
Signed-off-by: Henry Barreto <[email protected]>
---
 NEWS                      |   2 +
 tests/ovn-nbctl.at        | 145 +++++++++++++++++++
 utilities/ovn-nbctl.8.xml |  40 ++++++
 utilities/ovn-nbctl.c     | 296 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 483 insertions(+)

diff --git a/NEWS b/NEWS
index 6415fb973..135f660ec 100644
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,8 @@ Post v26.09.0
      instead of being ignored.  Such ports are useful to connect a transit
      router to a transit switch.  Accordingly, "ovn-ic-nbctl trp-add" no
      longer requires a trailing network or chassis argument.
+   - Added new ovn-nbctl commands "lb-hc-add", "lb-hc-del" and "lb-hc-list"
+     to manage load balancer health checks.
    - Dynamic Routing:
      * Remove the "other_config:dynamic-routing-arp-prefer-local"
        option from Logical Switches.  EVPN-learned MAC bindings are
diff --git a/tests/ovn-nbctl.at b/tests/ovn-nbctl.at
index 51c576f6e..4c490b26c 100644
--- a/tests/ovn-nbctl.at
+++ b/tests/ovn-nbctl.at
@@ -1442,6 +1442,151 @@ UUID                                    LB              
    PROTO      VIP
 
 dnl ---------------------------------------------------------------------
 
+OVN_NBCTL_TEST([ovn_nbctl_lb_hc], [LB health checks], [
+AT_CHECK([ovn-nbctl lb-add lb0 30.0.0.10:80 192.168.10.10:80,192.168.10.20:80])
+AT_CHECK([ovn-nbctl lb-add lb0 30.0.0.20:443 192.168.10.10:443])
+AT_CHECK([ovn-nbctl lb-add lb0 30.0.0.30 192.168.10.10])
+AT_CHECK([ovn-nbctl lb-add lb6 [[2001:db8::10]]:80 [[2001:db8::1]]:80])
+
+dnl Errors.
+AT_CHECK([ovn-nbctl lb-hc-add lbx 30.0.0.10:80], [1], [],
+[ovn-nbctl: lbx: load balancer name not found
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.99:80], [1], [],
+[ovn-nbctl: vip 30.0.0.99:80 is not part of the load balancer.
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.30], [1], [],
+[ovn-nbctl: 30.0.0.30: health checks need a VIP with a port.
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 foo=1], [1], [],
+[ovn-nbctl: foo=1: options must be interval, timeout, success_count or 
failure_count with an integer value from 1 to 2147483 (KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 interval=0], [1], [],
+[ovn-nbctl: interval=0: options must be interval, timeout, success_count or 
failure_count with an integer value from 1 to 2147483 (KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 interval], [1], [],
+[ovn-nbctl: interval: options must be interval, timeout, success_count or 
failure_count with an integer value from 1 to 2147483 (KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 interval=5x], [1], [],
+[ovn-nbctl: interval=5x: options must be interval, timeout, success_count or 
failure_count with an integer value from 1 to 2147483 (KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 interval=2147484], [1], [],
+[ovn-nbctl: interval=2147484: options must be interval, timeout, success_count 
or failure_count with an integer value from 1 to 2147483 (KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 timeout=3000000000], [1], [],
+[ovn-nbctl: timeout=3000000000: options must be interval, timeout, 
success_count or failure_count with an integer value from 1 to 2147483 
(KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 ==interval=5], [1], [],
+[ovn-nbctl: ==interval=5: options must be interval, timeout, success_count or 
failure_count with an integer value from 1 to 2147483 (KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 "interval= 5"], [1], [],
+[ovn-nbctl: interval= 5: options must be interval, timeout, success_count or 
failure_count with an integer value from 1 to 2147483 (KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 interval=+5], [1], [],
+[ovn-nbctl: interval=+5: options must be interval, timeout, success_count or 
failure_count with an integer value from 1 to 2147483 (KEY=VALUE).
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 interval=5 interval=9], [1], [],
+[ovn-nbctl: interval=9: option interval is given more than once.
+])
+AT_CHECK([ovn-nbctl lb-add lbs 30.0.0.40:80 192.168.10.10:80 sctp])
+AT_CHECK([ovn-nbctl lb-hc-add lbs 30.0.0.40:80], [1], [],
+[ovn-nbctl: lbs: SCTP load balancers do not support health checks.
+])
+
+dnl Add, duplicates and --may-exist.
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 interval=5 failure_count=3])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.20:443])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80], [1], [],
+[ovn-nbctl: lb0: a health check for vip 30.0.0.10:80 already exists.
+])
+AT_CHECK([ovn-nbctl lb-hc-list lb0 | uuidfilt], [0], [dnl
+UUID                                  VIP            OPTIONS
+<0>  30.0.0.10:80   failure_count=3 interval=5
+<1>  30.0.0.20:443  -
+])
+AT_CHECK([ovn-nbctl --may-exist lb-hc-add lb0 30.0.0.10:80 timeout=2])
+AT_CHECK([ovn-nbctl --bare --columns=vip,options find 
load_balancer_health_check vip='"30.0.0.10:80"'], [0], [dnl
+30.0.0.10:80
+timeout=2
+])
+AT_CHECK([ovn-nbctl --bare --columns=_uuid list load_balancer_health_check | 
grep -c .], [0], [2
+])
+
+dnl IPv6 VIP.
+AT_CHECK([ovn-nbctl lb-hc-add lb6 [[2001:db8::10]]:80 interval=1])
+AT_CHECK([ovn-nbctl lb-hc-list lb6 | uuidfilt], [0], [dnl
+UUID                                  VIP                OPTIONS
+<0>  [[2001:db8::10]]:80  interval=1
+])
+
+dnl Delete one, then all.
+AT_CHECK([ovn-nbctl lb-hc-del lb0 30.0.0.20:443])
+AT_CHECK([ovn-nbctl lb-hc-del lb0 30.0.0.20:443], [1], [],
+[ovn-nbctl: lb0: no health check for vip 30.0.0.20:443.
+])
+AT_CHECK([ovn-nbctl --if-exists lb-hc-del lb0 30.0.0.20:443])
+AT_CHECK([ovn-nbctl --if-exists lb-hc-del lbx])
+AT_CHECK([ovn-nbctl lb-hc-list lb0 | uuidfilt], [0], [dnl
+UUID                                  VIP           OPTIONS
+<0>  30.0.0.10:80  timeout=2
+])
+AT_CHECK([ovn-nbctl lb-hc-del lb0])
+AT_CHECK([ovn-nbctl lb-hc-del lb6])
+AT_CHECK([ovn-nbctl lb-hc-list lb0])
+AT_CHECK([ovn-nbctl list load_balancer_health_check])
+
+dnl Option values are stored normalized.
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80 interval=05])
+AT_CHECK([ovn-nbctl --bare --columns=options list load_balancer_health_check], 
[0], [dnl
+interval=5
+])
+
+dnl Several commands in one invocation see each other's changes.
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.20:443 -- lb-hc-add lb0 
30.0.0.20:443], [1], [],
+[ovn-nbctl: lb0: a health check for vip 30.0.0.20:443 already exists.
+])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.20:443 -- --may-exist lb-hc-add lb0 
30.0.0.20:443 interval=3])
+AT_CHECK([ovn-nbctl lb-hc-list lb0 | uuidfilt], [0], [dnl
+UUID                                  VIP            OPTIONS
+<0>  30.0.0.10:80   interval=5
+<1>  30.0.0.20:443  interval=3
+])
+AT_CHECK([ovn-nbctl lb-hc-del lb0 30.0.0.20:443 -- lb-hc-del lb0 
30.0.0.20:443], [1], [],
+[ovn-nbctl: lb0: no health check for vip 30.0.0.20:443.
+])
+AT_CHECK([ovn-nbctl lb-hc-del lb0 30.0.0.20:443 -- lb-hc-del lb0])
+AT_CHECK([ovn-nbctl list load_balancer_health_check])
+
+dnl A health check can be removed after its VIP is gone.
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.20:443])
+AT_CHECK([ovn-nbctl lb-del lb0 30.0.0.20:443])
+AT_CHECK([ovn-nbctl lb-hc-del lb0 30.0.0.20:443])
+AT_CHECK([ovn-nbctl list load_balancer_health_check])
+
+dnl A malformed VIP is an error even with --if-exists.
+AT_CHECK([ovn-nbctl -vsocket_util:off --if-exists lb-hc-del lb0 30.0.0.1a:80], 
[1], [],
+[ovn-nbctl: 30.0.0.1a:80: should be an IP address (or an IP address and a port 
number with : as a separator).
+])
+AT_CHECK([ovn-nbctl -vsocket_util:off --if-exists lb-hc-del lbx 30.0.0.1a:80], 
[1], [],
+[ovn-nbctl: 30.0.0.1a:80: should be an IP address (or an IP address and a port 
number with : as a separator).
+])
+
+dnl A health check shared by two load balancers can be removed from one.
+AT_CHECK([ovn-nbctl lb-add lb1 30.0.0.10:80 192.168.10.10:80])
+AT_CHECK([ovn-nbctl lb-hc-add lb0 30.0.0.10:80])
+hc=$(ovn-nbctl --bare --columns=_uuid list load_balancer_health_check)
+AT_CHECK([ovn-nbctl add load_balancer lb1 health_check $hc])
+AT_CHECK([ovn-nbctl lb-hc-del lb0 30.0.0.10:80])
+AT_CHECK([ovn-nbctl lb-hc-list lb1 | uuidfilt], [0], [dnl
+UUID                                  VIP           OPTIONS
+<0>  30.0.0.10:80  -
+])
+AT_CHECK([ovn-nbctl lb-hc-del lb1])
+AT_CHECK([ovn-nbctl list load_balancer_health_check])
+])
+
+dnl ---------------------------------------------------------------------
+
 OVN_NBCTL_TEST([ovn_nbctl_lbs_ipv6], [LBs IPv6], [
 dnl A bunch of commands that should fail
 AT_CHECK([ovn-nbctl -vsocket_util:off lb-add lb0 [[ae0f::10]]:80a 
[[fd0f::10]]:80,[[fd0f::20]]:80 tcp], [1], [],
diff --git a/utilities/ovn-nbctl.8.xml b/utilities/ovn-nbctl.8.xml
index ffc706dce..61f841b4e 100644
--- a/utilities/ovn-nbctl.8.xml
+++ b/utilities/ovn-nbctl.8.xml
@@ -1697,6 +1697,46 @@
         specified <var>lb</var> will be listed.
       </dd>
 
+      <dt>[<code>--may-exist</code>] <code>lb-hc-add</code> <var>lb</var>
+          <var>vip</var>:<var>port</var>
+          [<var>key</var>=<var>value</var>]...</dt>
+      <dd>
+        <p>
+          Adds a health check for <var>vip</var>:<var>port</var> of
+          <var>lb</var>, creating a row in the
+          <code>Load_Balancer_Health_Check</code> table.  The VIP must already
+          be part of <var>lb</var> and must have a port; IPv6 VIPs are written
+          as <code>[</code><var>ip</var><code>]:</code><var>port</var>.
+          Optional <var>key</var>=<var>value</var> pairs set the health check
+          options <code>interval</code>, <code>timeout</code>,
+          <code>success_count</code> and <code>failure_count</code>, each an
+          integer from 1 to 2147483.  Template and SCTP load balancers do not
+          support health checks.
+        </p>
+        <p>
+          It is an error if a health check for the VIP already exists, unless
+          <code>--may-exist</code> is specified, in which case its options are
+          replaced.  Each backend also needs an entry in the load balancer's
+          <code>ip_port_mappings</code> column to be monitored; backends
+          without one are not used once the VIP has a health check.
+        </p>
+      </dd>
+
+      <dt>[<code>--if-exists</code>] <code>lb-hc-del</code> <var>lb</var>
+          [<var>vip</var>:<var>port</var>]</dt>
+      <dd>
+        Removes the health check of <var>vip</var>:<var>port</var> from
+        <var>lb</var>, or all of its health checks if no VIP is given.  The
+        VIP does not need to be part of <var>lb</var> anymore.  It is an
+        error if <var>lb</var> or the health check does not exist, unless
+        <code>--if-exists</code> is specified.
+      </dd>
+
+      <dt><code>lb-hc-list</code> <var>lb</var></dt>
+      <dd>
+        Lists the health checks of <var>lb</var> with their options.
+      </dd>
+
       <dt>[<code>--may-exist</code>] <code>ls-lb-add</code> <var>switch</var> 
<var>lb</var></dt>
       <dd>
         Adds the specified <var>lb</var> to <var>switch</var>.
diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index 0c111a9fd..a23303897 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -14,6 +14,7 @@
 
 #include <config.h>
 
+#include <ctype.h>
 #include <getopt.h>
 #include <inttypes.h>
 #include <stdlib.h>
@@ -505,6 +506,16 @@ LB commands:\n\
   lb-del LB [VIP]           remove a load-balancer or just the VIP from\n\
                             the load balancer\n\
   lb-list [LB]              print load-balancers\n\
+"
+    "  [--may-exist]\n"
+    "  lb-hc-add LB VIP:PORT [KEY=VALUE]...\n"
+    "                            add a health check for VIP of LB (KEY is\n"
+    "                            interval, timeout, success_count or\n"
+    "                            failure_count)\n"
+    "  [--if-exists]\n"
+    "  lb-hc-del LB [VIP:PORT]   remove health checks from LB\n"
+    "  lb-hc-list LB             print health checks of LB\n"
+"\
   [--may-exist]\n\
   lr-lb-add ROUTER LB       add a load-balancer to ROUTER\n\
   [--if-exists]\n\
@@ -4037,6 +4048,284 @@ nbctl_lb_list(struct ctl_context *ctx)
     }
 }
 
+/* Load Balancer Health Check Functions. */
+
+/* ovn-controller multiplies "interval" and "timeout" (seconds) by 1000 into
+ * an int, so keep every health check option within that range. */
+#define LB_HC_OPTION_MAX (INT_MAX / 1000)
+
+/* Normalizes 'vip' ("IP:PORT" or "[IPv6]:PORT") the same way as "lb-add"
+ * and checks that it has a port.  On success stores the normalized VIP in
+ * 'normalized' and returns NULL. */
+static char * OVS_WARN_UNUSED_RESULT
+lb_health_check_normalize_vip(const char *vip, struct ds *normalized)
+{
+    struct ovn_lb_vip lb_vip;
+    char *error = ovn_lb_vip_init(&lb_vip, vip, "", false, AF_INET);
+    if (error) {
+        ovn_lb_vip_destroy(&lb_vip);
+        return error;
+    }
+    if (!lb_vip.port_str) {
+        ovn_lb_vip_destroy(&lb_vip);
+        return xasprintf("%s: health checks need a VIP with a port.", vip);
+    }
+    ovn_lb_vip_format(&lb_vip, normalized, false);
+    ovn_lb_vip_destroy(&lb_vip);
+    return NULL;
+}
+
+static const struct nbrec_load_balancer_health_check *
+lb_health_check_find(const struct nbrec_load_balancer *lb, const char *vip)
+{
+    for (size_t i = 0; i < lb->n_health_check; i++) {
+        if (!strcmp(lb->health_check[i]->vip, vip)) {
+            return lb->health_check[i];
+        }
+    }
+    return NULL;
+}
+
+/* Health checks are added and removed by rewriting the whole column rather
+ * than with update_*_addvalue/delvalue(), so that later commands in the same
+ * ovn-nbctl invocation see the change. */
+static void
+lb_health_check_set(const struct nbrec_load_balancer *lb,
+                    const struct nbrec_load_balancer_health_check *add,
+                    const struct nbrec_load_balancer_health_check *del)
+{
+    struct nbrec_load_balancer_health_check **hcs =
+        xmalloc((lb->n_health_check + 1) * sizeof *hcs);
+    size_t n = 0;
+
+    for (size_t i = 0; i < lb->n_health_check; i++) {
+        if (lb->health_check[i] != del) {
+            hcs[n++] = lb->health_check[i];
+        }
+    }
+    if (add) {
+        hcs[n++] = CONST_CAST(struct nbrec_load_balancer_health_check *, add);
+    }
+    nbrec_load_balancer_set_health_check(lb, hcs, n);
+    free(hcs);
+}
+
+static void
+nbctl_pre_lb_health_check(struct ctl_context *ctx)
+{
+    ovsdb_idl_add_column(ctx->idl, &nbrec_load_balancer_col_name);
+    ovsdb_idl_add_column(ctx->idl, &nbrec_load_balancer_col_vips);
+    ovsdb_idl_add_column(ctx->idl, &nbrec_load_balancer_col_options);
+    ovsdb_idl_add_column(ctx->idl, &nbrec_load_balancer_col_protocol);
+    ovsdb_idl_add_column(ctx->idl, &nbrec_load_balancer_col_health_check);
+    ovsdb_idl_add_column(ctx->idl, &nbrec_load_balancer_health_check_col_vip);
+    ovsdb_idl_add_column(ctx->idl,
+                         &nbrec_load_balancer_health_check_col_options);
+}
+
+static void
+nbctl_lb_health_check_add(struct ctl_context *ctx)
+{
+    static const char *valid_options[] = {
+        "interval", "timeout", "success_count", "failure_count",
+    };
+    bool may_exist = shash_find(&ctx->options, "--may-exist") != NULL;
+    const struct nbrec_load_balancer *lb = NULL;
+    struct ds vip = DS_EMPTY_INITIALIZER;
+    struct smap options = SMAP_INITIALIZER(&options);
+
+    char *error = lb_by_name_or_uuid(ctx, ctx->argv[1], true, &lb);
+    if (error) {
+        ctx->error = error;
+        goto out;
+    }
+
+    /* The checks below rely on these columns. */
+    nbrec_load_balancer_verify_options(lb);
+    nbrec_load_balancer_verify_protocol(lb);
+    nbrec_load_balancer_verify_vips(lb);
+
+    if (smap_get_bool(&lb->options, "template", false)) {
+        ctl_error(ctx, "%s: template load balancers do not support health "
+                  "checks.", ctx->argv[1]);
+        goto out;
+    }
+    if (nullable_string_is_equal(lb->protocol, "sctp")) {
+        ctl_error(ctx, "%s: SCTP load balancers do not support health "
+                  "checks.", ctx->argv[1]);
+        goto out;
+    }
+
+    error = lb_health_check_normalize_vip(ctx->argv[2], &vip);
+    if (error) {
+        ctx->error = error;
+        goto out;
+    }
+    if (!smap_get(&lb->vips, ds_cstr(&vip))) {
+        ctl_error(ctx, "vip %s is not part of the load balancer.",
+                  ds_cstr(&vip));
+        goto out;
+    }
+
+    for (int i = 3; i < ctx->argc; i++) {
+        char *key = xstrdup(ctx->argv[i]);
+        char *value = strchr(key, '=');
+        if (value) {
+            *value++ = '\0';
+        }
+        char *arg = key;
+
+        bool valid_key = false;
+        for (size_t j = 0; j < ARRAY_SIZE(valid_options); j++) {
+            valid_key |= !strcmp(key, valid_options[j]);
+        }
+        if (valid_key && smap_get(&options, key)) {
+            ctl_error(ctx, "%s: option %s is given more than once.",
+                      ctx->argv[i], key);
+            free(arg);
+            goto out;
+        }
+        /* str_to_int() accepts leading white space and a sign. */
+        int n;
+        if (!valid_key || !value || !isdigit((unsigned char) *value)
+            || !str_to_int(value, 10, &n)
+            || n <= 0 || n > LB_HC_OPTION_MAX) {
+            ctl_error(ctx, "%s: options must be interval, timeout, "
+                      "success_count or failure_count with an integer "
+                      "value from 1 to %d (KEY=VALUE).", ctx->argv[i],
+                      LB_HC_OPTION_MAX);
+            free(arg);
+            goto out;
+        }
+        smap_replace_nocopy(&options, key, xasprintf("%d", n));
+        free(arg);
+    }
+
+    nbrec_load_balancer_verify_health_check(lb);
+    const struct nbrec_load_balancer_health_check *hc =
+        lb_health_check_find(lb, ds_cstr(&vip));
+    if (hc) {
+        if (!may_exist) {
+            ctl_error(ctx, "%s: a health check for vip %s already exists.",
+                      ctx->argv[1], ds_cstr(&vip));
+            goto out;
+        }
+        nbrec_load_balancer_health_check_set_options(hc, &options);
+        goto out;
+    }
+
+    hc = nbrec_load_balancer_health_check_insert(ctx->txn);
+    nbrec_load_balancer_health_check_set_vip(hc, ds_cstr(&vip));
+    nbrec_load_balancer_health_check_set_options(hc, &options);
+    lb_health_check_set(lb, hc, NULL);
+
+out:
+    smap_destroy(&options);
+    ds_destroy(&vip);
+}
+
+static void
+nbctl_lb_health_check_del(struct ctl_context *ctx)
+{
+    bool must_exist = !shash_find(&ctx->options, "--if-exists");
+    const struct nbrec_load_balancer *lb = NULL;
+    struct ds vip = DS_EMPTY_INITIALIZER;
+    char *error;
+
+    /* The VIP may already be gone from 'vips' (e.g. after "lb-del LB VIP"),
+     * so only normalize it and look for the health check itself.  Do it
+     * first, so that a malformed VIP is an error even with --if-exists. */
+    if (ctx->argc == 3) {
+        error = lb_health_check_normalize_vip(ctx->argv[2], &vip);
+        if (error) {
+            ctx->error = error;
+            goto out;
+        }
+    }
+
+    error = lb_by_name_or_uuid(ctx, ctx->argv[1], must_exist, &lb);
+    if (error) {
+        ctx->error = error;
+        goto out;
+    }
+    if (!lb) {
+        goto out;
+    }
+
+    /* Health check rows are not root rows, so dropping the reference is
+     * enough: the database removes them once no load balancer refers to
+     * them, and keeps them while another one still does. */
+    nbrec_load_balancer_verify_health_check(lb);
+    if (ctx->argc == 2) {
+        nbrec_load_balancer_set_health_check(lb, NULL, 0);
+        goto out;
+    }
+
+    const struct nbrec_load_balancer_health_check *hc =
+        lb_health_check_find(lb, ds_cstr(&vip));
+    if (hc) {
+        lb_health_check_set(lb, NULL, hc);
+    } else if (must_exist) {
+        ctl_error(ctx, "%s: no health check for vip %s.", ctx->argv[1],
+                  ds_cstr(&vip));
+    }
+out:
+    ds_destroy(&vip);
+}
+
+static int
+lb_health_check_cmp(const void *a_, const void *b_)
+{
+    const struct nbrec_load_balancer_health_check *const *a = a_;
+    const struct nbrec_load_balancer_health_check *const *b = b_;
+
+    return strcmp((*a)->vip, (*b)->vip);
+}
+
+static void
+nbctl_lb_health_check_list(struct ctl_context *ctx)
+{
+    const struct nbrec_load_balancer *lb = NULL;
+
+    char *error = lb_by_name_or_uuid(ctx, ctx->argv[1], true, &lb);
+    if (error) {
+        ctx->error = error;
+        return;
+    }
+    if (!lb->n_health_check) {
+        return;
+    }
+
+    const struct nbrec_load_balancer_health_check **hcs =
+        xmemdup(lb->health_check, lb->n_health_check * sizeof *hcs);
+    qsort(hcs, lb->n_health_check, sizeof *hcs, lb_health_check_cmp);
+
+    int vip_width = strlen("VIP");
+    for (size_t i = 0; i < lb->n_health_check; i++) {
+        vip_width = MAX(vip_width, strlen(hcs[i]->vip));
+    }
+
+    ds_put_format(&ctx->output, "%-36s  %-*s  OPTIONS\n", "UUID",
+                  vip_width, "VIP");
+    for (size_t i = 0; i < lb->n_health_check; i++) {
+        ds_put_format(&ctx->output, UUID_FMT"  %-*s  ",
+                      UUID_ARGS(&hcs[i]->header_.uuid),
+                      vip_width, hcs[i]->vip);
+
+        const struct smap_node **nodes = smap_sort(&hcs[i]->options);
+        for (size_t j = 0; j < smap_count(&hcs[i]->options); j++) {
+            ds_put_format(&ctx->output, "%s%s=%s", j ? " " : "",
+                          nodes[j]->key, nodes[j]->value);
+        }
+        if (smap_is_empty(&hcs[i]->options)) {
+            ds_put_char(&ctx->output, '-');
+        }
+        free(nodes);
+        ds_put_char(&ctx->output, '\n');
+    }
+    free(hcs);
+}
+
 static void
 nbctl_pre_lr_lb_add(struct ctl_context *ctx)
 {
@@ -9517,6 +9806,13 @@ static const struct ctl_command_syntax nbctl_commands[] 
= {
     { "lb-del", 1, 2, "LB [VIP]", nbctl_pre_lb_del, nbctl_lb_del, NULL,
         "--if-exists", RW },
     { "lb-list", 0, 1, "[LB]", nbctl_pre_lb_list, nbctl_lb_list, NULL, "", RO 
},
+    { "lb-hc-add", 2, INT_MAX, "LB VIP:PORT [KEY=VALUE]...",
+      nbctl_pre_lb_health_check, nbctl_lb_health_check_add, NULL,
+      "--may-exist", RW },
+    { "lb-hc-del", 1, 2, "LB [VIP:PORT]", nbctl_pre_lb_health_check,
+      nbctl_lb_health_check_del, NULL, "--if-exists", RW },
+    { "lb-hc-list", 1, 1, "LB", nbctl_pre_lb_health_check,
+      nbctl_lb_health_check_list, NULL, "", RO },
     { "lr-lb-add", 2, 2, "ROUTER LB", nbctl_pre_lr_lb_add,
       nbctl_lr_lb_add, NULL, "--may-exist", RW },
     { "lr-lb-del", 1, 2, "ROUTER [LB]", nbctl_pre_lr_lb_del,
-- 
2.56.0

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

Reply via email to