This is a nice reflector! one comment below
On Fri, Nov 20, 2026 at 9:28 AM Mark Michelson via dev <
[email protected]> wrote:

> This change accomplishes a few things:
>
> * It reorganizes the code so that it's more clear where arguments are
>   being parsed.
> * It reduces redunancy by gathering all arguments and then calling
>   lr_nat_del_matching() one time. This also eliminates the need for the
>   "cleanup" goto label.
> * It causes lr-nat-del to return non-zero and emit an error message in
>   more cases than it previously did. Before, the only circumstance under
>   which this would happen is if a type, IP address, and gateway port
>   were all provided. If only a type, or type and IP address, or type and
>   gateway port were provided, then lr-nat-del always returned zero no
>   matter if any NATs were actually deleted. The existing "--if-exists"
>   option can be used to suppress the error message and have lr-nat-del
>   return zero in all cases where valid inputs were given but no NATs
>   were deleted.
>
> The only negative from this patch is that the error message emitted at
> the end no longer contains the word "and", meaning the message may read
> a bit more stilted than it previously did.
>
> Existing NAT tests have been updated. The test updates in
> tests/ovn-northd.at expose incorrect commands that were used to attempt
> to delete NATs. The NATs test in tests/ovn-nbctl.at has been updated to
> account for the new return values.
>
> Signed-off-by: Mark Michelson <[email protected]>
> ---
>  NEWS                      |  7 ++++
>  tests/ovn-nbctl.at        | 21 ++++++++----
>  tests/ovn-northd.at       |  8 ++---
>  utilities/ovn-nbctl.8.xml |  6 ++--
>  utilities/ovn-nbctl.c     | 67 +++++++++++++++++++--------------------
>  5 files changed, 60 insertions(+), 49 deletions(-)
>
> diff --git a/NEWS b/NEWS
> index b2416f98b..4648fd41a 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -1,5 +1,12 @@
>  Post v26.09.0
>  --------------
> +   - ovn-nbctl lr-nat-del will now return non-zero and emit an error
> +     whenever valid inputs are presented but no NATs are deleted. If
> +     "--if-exists" is specified, then no error is returned and no error
> +     message is emitted. The "--if-exists" option formerly applied only
> when
> +     a type, IP, and gateway port were provided. Now "--if-exists" needs
> to be
> +     specified when fewer arguments are provided in order to prevent the
> error
> +     from being emitted.
>     - ovn-nbctl lr-policy-del, when not passed a --chain value, can now
> delete
>       matching policies with any chain value.
>     - ovn-nbctl lr-policy-del will now return non-zero and emit an error
> diff --git a/tests/ovn-nbctl.at b/tests/ovn-nbctl.at
> index d3a7619b8..b0490cd57 100644
> --- a/tests/ovn-nbctl.at
> +++ b/tests/ovn-nbctl.at
> @@ -868,7 +868,9 @@ AT_CHECK([ovn-nbctl --stateless lr-nat-add lr0
> dnat_and_snat 40.0.0.3 192.168.1.
>  AT_CHECK([ovn-nbctl --stateless --may-exist lr-nat-add lr0 dnat_and_snat
> 40.0.0.3 192.168.1.7])
>
>  dnl Deletes the NATs
> -AT_CHECK([ovn-nbctl lr-nat-del lr0 dnat_and_snat 30.0.0.3])
> +AT_CHECK([ovn-nbctl lr-nat-del lr0 dnat_and_snat 30.0.0.3], [1], [], [dnl
> +ovn-nbctl: no matching NAT with the type (dnat_and_snat), external_ip
> (30.0.0.3)
> +])
>
>  AT_CHECK([ovn-nbctl lr-nat-del lr0 dnat_and_snat 30.0.0.1])
>  AT_CHECK([ovn-nbctl lr-nat-del lr0 dnat_and_snat fd01::1])
> @@ -973,10 +975,13 @@ snat
>          40.0.0.3
>
>  AT_CHECK([ovn-nbctl lr-nat-del lr0])
>  AT_CHECK([ovn-nbctl lr-nat-list lr0], [0], [])
> -AT_CHECK([ovn-nbctl lr-nat-del lr0])
> -AT_CHECK([ovn-nbctl lr-nat-del lr0 dnat])
> +AT_CHECK([ovn-nbctl lr-nat-del lr0], [1], [], [dnl
> +ovn-nbctl: Logical router lr0 has no NATs
> +])
>

I don't see testing for for the "--if-exists" to supress the Logical router
%s has no NATs"


>
> -AT_CHECK([ovn-nbctl lr-nat-del lr0])
> +AT_CHECK([ovn-nbctl lr-nat-del lr0 dnat], [1], [], [dnl
> +ovn-nbctl: no matching NAT with the type (dnat)
> +])
>
>  check_uuid ovn-nbctl create Address_Set name=allowed_range
> addresses=\"1.1.1.1\"
>  check_uuid ovn-nbctl create Address_Set name=disallowed_range
> addresses=\"2.2.2.2\"
> @@ -1077,7 +1082,9 @@ AT_CHECK([ovn-nbctl lr-nat-list lr0], [0], [dnl
>  TYPE             GATEWAY_PORT          MATCH                 EXTERNAL_IP
>       EXTERNAL_PORT    LOGICAL_IP          EXTERNAL_MAC         LOGICAL_PORT
>  snat                                                         172.64.1.10
>                        20.0.0.10
>  ])
> -AT_CHECK([ovn-nbctl lr-nat-del lr0 snat lrp01])
> +AT_CHECK([ovn-nbctl lr-nat-del lr0 snat lrp01], [1], [], [dnl
> +ovn-nbctl: no matching NAT with the type (snat), gateway_port (lrp01)
> +])
>  AT_CHECK([ovn-nbctl lr-nat-list lr0], [0], [dnl
>  TYPE             GATEWAY_PORT          MATCH                 EXTERNAL_IP
>       EXTERNAL_PORT    LOGICAL_IP          EXTERNAL_MAC         LOGICAL_PORT
>  snat                                                         172.64.1.10
>                        20.0.0.10
> @@ -1087,12 +1094,12 @@ AT_CHECK([ovn-nbctl lr-nat-del lr0 snat 20.0.0
> lrp01], [1], [],
>  [ovn-nbctl: 20.0.0: Invalid IP address or CIDR
>  ])
>  AT_CHECK([ovn-nbctl lr-nat-del lr0 snat 20.0.0.10 lrp01], [1], [],
> -[ovn-nbctl: no matching NAT with the type (snat), logical_ip (20.0.0.10)
> and gateway_port (lrp01)
> +[ovn-nbctl: no matching NAT with the type (snat), logical_ip (20.0.0.10),
> gateway_port (lrp01)
>  ])
>  AT_CHECK([ovn-nbctl --if-exists lr-nat-del lr0 snat 20.0.0.10 lrp01])
>  AT_CHECK([ovn-nbctl lr-nat-del lr0 snat])
>
> -AT_CHECK([ovn-nbctl lr-nat-del lr0])
> +AT_CHECK([ovn-nbctl --if-exists lr-nat-del lr0])
>  AT_CHECK([ovn-nbctl lrp-del-gateway-chassis lrp00 chassis1])
>  AT_CHECK([ovn-nbctl lrp-del-gateway-chassis lrp01 chassis2])
>
> diff --git a/tests/ovn-northd.at b/tests/ovn-northd.at
> index f1e88b4bd..fb03a1b28 100644
> --- a/tests/ovn-northd.at
> +++ b/tests/ovn-northd.at
> @@ -1263,14 +1263,14 @@ AT_CHECK([grep -c 'ct_snat.*3000' sbflows2 && grep
> -c 'ct_dnat.*3000' sbflows2],
>  0
>  ])
>
> -check ovn-nbctl lr-nat-del R1 snat  172.16.1.1
> +check ovn-nbctl lr-nat-del R1 snat 50.0.0.11
>  check ovn-nbctl --wait=sb --portrange --stateless lr-nat-add R1
> dnat_and_snat  172.16.1.2 50.0.0.12 1-3000
>
>  AT_CAPTURE_FILE([sbflows3])
> -OVS_WAIT_UNTIL([ovn-sbctl dump-flows R1 > sbflows3 && test 3 = `grep -c
> lr_in_unsnat sbflows3`])
> +OVS_WAIT_UNTIL([ovn-sbctl dump-flows R1 > sbflows3 && test 2 = `grep -c
> lr_in_unsnat sbflows3`])
>  AT_CHECK([grep 'ct_[s]dnat.*172\.16\.1\.2.*3000' sbflows3], [1])
>
> -check ovn-nbctl lr-nat-del R1 dnat_and_snat  172.16.1.1
> +check ovn-nbctl lr-nat-del R1 dnat_and_snat 172.16.1.2
>
>  OVN_CLEANUP_NORTHD
>  AT_CLEANUP
> @@ -24126,7 +24126,7 @@ AS_BOX([Non-distributed NATs are not advertised])
>  check ovn-nbctl --wait=sb lr-nat-add lr snat 172.16.0.50 10.0.0.10
>  check ovn-nbctl --wait=sb lr-nat-add lr dnat 172.16.0.51 10.0.0.10
>  check_row_count Advertised_MAC_Binding 1
> -check ovn-nbctl --wait=sb lr-nat-del lr snat 172.16.0.50
> +check ovn-nbctl --wait=sb lr-nat-del lr snat 10.0.0.10
>  check ovn-nbctl --wait=sb lr-nat-del lr dnat 172.16.0.51
>
>  AS_BOX([dnat_and_snat without external_mac is not advertised])
> diff --git a/utilities/ovn-nbctl.8.xml b/utilities/ovn-nbctl.8.xml
> index ffc706dce..1c6dc25aa 100644
> --- a/utilities/ovn-nbctl.8.xml
> +++ b/utilities/ovn-nbctl.8.xml
> @@ -1562,9 +1562,9 @@
>          </p>
>
>          <p>
> -          It is an error if both <var>ip</var> and
> <var>gateway_port</var> are
> -          specified and there is no matching NAT entry, unless
> -          <code>--if-exists</code> is specified.
> +          Assuming all inputs are valid, then <code>lr-nat-del</code> will
> +          return an error if no NATs were deleted. If
> <code>--if-exists</code>
> +          is specified, then no error is returned.
>          </p>
>        </dd>


> diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
> index 6f918f55d..c24fd639d 100644
> --- a/utilities/ovn-nbctl.c
> +++ b/utilities/ovn-nbctl.c
> @@ -6066,13 +6066,17 @@ nbctl_lr_nat_del(struct ctl_context *ctx)
>      if (ctx->argc == 2 && !match) {
>          /* If type, external_ip, logical_ip and match are not specified,
>           * delete all NATs. */
> +        if (!lr->n_nat && must_exist) {
> +            ctl_error(ctx, "Logical router %s has no NATs", lr->name);
> +            return;
> +        }
>          nbrec_logical_router_verify_nat(lr);
>          nbrec_logical_router_set_nat(lr, NULL, 0);
>          return;
>      }
>
>      const char *nat_type = ctx->argv[2];
> -    int is_snat = !strcmp("snat", nat_type);
> +    bool is_snat = !strcmp("snat", nat_type);
>      if (strcmp(nat_type, "dnat") && strcmp(nat_type, "snat")
>              && strcmp(nat_type, "dnat_and_snat")) {
>          ctl_error(ctx, "%s: type must be one of \"dnat\", \"snat\" and "
> @@ -6080,59 +6084,52 @@ nbctl_lr_nat_del(struct ctl_context *ctx)
>          return;
>      }
>
> -    if (ctx->argc == 3) {
> -        /*Deletes all NATs with the specified type. */
> -        lr_nat_del_matching(lr, nat_type, NULL, NULL, match, is_snat);
> -        return;
> -    }
> -
> -    char *nat_ip = normalize_prefix_str(ctx->argv[3]);
> +    char *nat_ip = NULL;
>      const struct nbrec_logical_router_port *dgw_port = NULL;
> -
> -    if (ctx->argc == 4) {
> -        if (!nat_ip) {
> -            /* GATEWAY_PORT is assumed to be passed. */
> +    if (ctx->argc > 3) {
> +        nat_ip = normalize_prefix_str(ctx->argv[3]);
> +        if (!nat_ip && ctx->argc == 4) {
>              error = lrp_by_name_or_uuid(ctx, ctx->argv[3], true,
> &dgw_port);
>              if (error) {
>                  ctx->error = error;
>                  return;
>              }
>          }
> -
> -        lr_nat_del_matching(lr, nat_type, nat_ip, dgw_port, match,
> is_snat);
> -        goto cleanup;
> -    }
> -
> -    if (!nat_ip) {
> -        ctl_error(ctx, "%s: Invalid IP address or CIDR", ctx->argv[3]);
> -        return;
>      }
>
> -    error = lrp_by_name_or_uuid(ctx, ctx->argv[4], true, &dgw_port);
> -    if (error) {
> -        ctx->error = error;
> -        goto cleanup;
> +    if (ctx->argc > 4) {
> +        if (!nat_ip) {
> +            ctl_error(ctx, "%s: Invalid IP address or CIDR",
> ctx->argv[3]);
> +            return;
> +        }
> +        error = lrp_by_name_or_uuid(ctx, ctx->argv[4], true, &dgw_port);
> +        if (error) {
> +            ctx->error = error;
> +            free(nat_ip);
> +            return;
> +        }
>      }
>
> -    size_t n_deleted = lr_nat_del_matching(lr, nat_type, nat_ip, dgw_port,
> -                                           match, is_snat);
> -    if (must_exist && !n_deleted) {
> +    if (must_exist && !lr_nat_del_matching(lr, nat_type, nat_ip, dgw_port,
> +                                           match, is_snat)) {
>          struct ds ds = DS_EMPTY_INITIALIZER;
> -        ds_put_format(&ds, "no matching NAT with the type (%s), %s (%s)",
> -                      nat_type, is_snat ? "logical_ip" : "external_ip",
> -                      nat_ip);
> +        ds_put_format(&ds, "no matching NAT with the type (%s)",
> +                      nat_type);
> +        if (nat_ip) {
> +            ds_put_format(&ds, ", %s (%s)",
> +                          is_snat ? "logical_ip" : "external_ip", nat_ip);
> +        }
> +        if (dgw_port) {
> +            ds_put_format(&ds, ", gateway_port (%s)", dgw_port->name);
> +        }
>          if (match) {
> -            ds_put_format(&ds, ", gateway_port (%s) and match (%s)",
> -                          ctx->argv[4], match);
> -        } else {
> -            ds_put_format(&ds, " and gateway_port (%s)", ctx->argv[4]);
> +            ds_put_format(&ds, ", match (%s)", match);
>          }
>
>          ctx->error = xstrdup(ds_cstr_ro(&ds));
>          ds_destroy(&ds);
>      }
>
> -cleanup:
>      free(nat_ip);
>  }
>
> --
> 2.55.0
>

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

The removal of the "goto cleanup" is great.

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

Reply via email to