Prior to this commit, if any row in the NAT table had a malformed IP
address, then lr-nat-del could not delete that NAT record, even if the
command otherwise matched the record.

As an example, if a SNAT had logical_ip set to "10.0.0" then `ovn-nbctl
lr-nat-del snat` would match the record, since its type was "snat" but
the record would not be deleted.

With this change, NATs with malformed IP addresses can be deleted with
lr-nat-del as long as the other fields on the NAT record match the
arguments passed in.

Signed-off-by: Mark Michelson <[email protected]>
---
 NEWS                  |  4 +++
 tests/ovn-nbctl.at    | 72 +++++++++++++++++++++++++++++++++++++++++++
 utilities/ovn-nbctl.c | 27 ++++++++--------
 3 files changed, 91 insertions(+), 12 deletions(-)

diff --git a/NEWS b/NEWS
index 4648fd41a..12fb825b4 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,9 @@
 Post v26.09.0
 --------------
+   - ovn-nbctl lr-nat-del can now delete NAT records that have malformed IP
+     addresses set on them. Previously, NAT records with malformed IP
+     addresses were always ignored, even if other fields matched the
+     arguments passed to lr-nat-del.
    - 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
diff --git a/tests/ovn-nbctl.at b/tests/ovn-nbctl.at
index 023e5b424..4a02ba4ae 100644
--- a/tests/ovn-nbctl.at
+++ b/tests/ovn-nbctl.at
@@ -3839,3 +3839,75 @@ ovn-nbctl: no matching NAT with the type (snat), match 
(ip6)
 
 check_row_count nb:NAT 1
 ])
+
+OVN_NBCTL_TEST([nat_del_invalid_ip], [Delete NAT with invalid IPs], [
+check ovn-nbctl lr-add lr0
+
+add_nats() {
+    # UUIDS were generated using the `uuidgen` utility. There is no 
significance
+    # to their values.
+    snat_options=$1
+    dnat_options=$2
+    dnat_uuid=4503b135-e8f5-47d8-80b9-918aa275a975
+    snat_uuid=312e4205-2629-4c6c-a29b-a7041f80918b
+    check_uuid ovn-nbctl --id=$dnat_uuid create NAT type=dnat 
external_ip=invalid logical_ip="10.0.0.1" $dnat_options -- \
+         add Logical_Router lr0 nat $dnat_uuid
+    check_uuid ovn-nbctl --id=$snat_uuid create NAT type=snat 
external_ip="10.0.0.1" logical_ip=invalid $snat_options -- \
+         add Logical_Router lr0 nat $snat_uuid
+    wait_row_count nb:NAT 2
+}
+
+add_nats
+
+# Trying to delete all NATs should work properly, even though they have
+# invalid IP addresses set.
+check ovn-nbctl lr-nat-del lr0
+wait_row_ocunt nb:NAT 0
+
+add_nats
+
+# Trying to delete the NATs using only the type should work properly, even
+# though they have invalid IP addresses set.
+check ovn-nbctl lr-nat-del lr0 snat
+wait_row_count nb:NAT 1
+check ovn-nbctl lr-nat-del lr0 dnat
+wait_row_count nb:NAT 0
+
+add_nats "match=ip4" "match=ip4"
+
+# Trying to delete the NATS using only the match would work properly, even
+# though they have invalid IP addresses set.
+check ovn-nbctl --match="ip4" lr-nat-del lr0 snat
+wait_row_count nb:NAT 1
+check ovn-nbctl --match="ip4" lr-nat-del lr0 dnat
+wait_row_count nb:NAT 0
+
+add_nats
+
+# Now let's try to delete by using IP address. Since the IPs are invalid, the
+# deletions should fail.
+
+# First try using the literal string used for the SNAT logical_ip and DNAT
+# external_ip. These will fail because of the invalid input to lr-nat-del.
+# The error message assumes that "invalid" is a logical router port name,
+# hence why the error message says "port name not found".
+AT_CHECK([ovn-nbctl lr-nat-del lr0 snat invalid], [1], [], [dnl
+ovn-nbctl: invalid: port name not found
+])
+wait_row_count nb:NAT 2
+AT_CHECK([ovn-nbctl lr-nat-del lr0 dnat invalid], [1], [], [dnl
+ovn-nbctl: invalid: port name not found
+])
+wait_row_count nb:NAT 2
+
+# Now try with the wrong IP address. Use the logical_ip of the DNAT rule and
+# the external_ip of the SNAT rule.
+AT_CHECK([ovn-nbctl lr-nat-del lr0 snat 10.0.0.1], [1], [], [dnl
+ovn-nbctl: no matching NAT with the type (snat), logical_ip (10.0.0.1)
+])
+wait_row_count nb:NAT 2
+AT_CHECK([ovn-nbctl lr-nat-del lr0 dnat 10.0.0.1], [1], [], [dnl
+ovn-nbctl: no matching NAT with the type (dnat), external_ip (10.0.0.1)
+])
+wait_row_count nb:NAT 2
+])
diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index 7058d8fd8..247ae1516 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -6015,12 +6015,6 @@ lr_nat_del_matching(const struct nbrec_logical_router 
*lr, const char *type,
 
     for (size_t i = 0; i < lr->n_nat; i++) {
         struct nbrec_nat *nat = lr->nat[i];
-        char *old_ip = normalize_prefix_str(is_snat
-                                            ? nat->logical_ip
-                                            : nat->external_ip);
-        if (!old_ip) {
-            continue;
-        }
 
         bool delete = true;
 
@@ -6028,10 +6022,6 @@ lr_nat_del_matching(const struct nbrec_logical_router 
*lr, const char *type,
             delete = false;
         }
 
-        if (ip && strcmp(ip, old_ip)) {
-            delete = false;
-        }
-
         if (dgw_port && nat->gateway_port != dgw_port) {
             delete = false;
         }
@@ -6040,12 +6030,25 @@ lr_nat_del_matching(const struct nbrec_logical_router 
*lr, const char *type,
             delete = false;
         }
 
+        if (ip) {
+            char *old_ip = normalize_prefix_str(is_snat
+                                                ? nat->logical_ip
+                                                : nat->external_ip);
+            if (!old_ip) {
+                continue;
+            }
+
+            if (strcmp(ip, old_ip)) {
+                delete = false;
+            }
+
+            free(old_ip);
+        }
+
         if (delete) {
             nbrec_logical_router_update_nat_delvalue(lr, nat);
             n_deleted++;
         }
-
-        free(old_ip);
     }
 
     return n_deleted;
-- 
2.55.0

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

Reply via email to