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. * Any case where no NATs are deleted results in ovn-nbctl returning non-zero and emitting an error message. As long as the issue is not due to invalid inputs (e.g. malformed IP address, non-existent logical router, non-existen gateway port), then this behavior can be reversed with the --if-exists option.
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. * Some tests in tests/ovn-northd.at were using incorrect IP addresses when trying to delete SNATs. These have been corrected. * Tests in tests/ovn-nbctl and tests/system-ovn.at have 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 ++--- tests/system-ovn.at | 13 +++++--- utilities/ovn-nbctl.8.xml | 6 ++-- utilities/ovn-nbctl.c | 67 +++++++++++++++++++-------------------- 6 files changed, 68 insertions(+), 54 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 +]) -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/tests/system-ovn.at b/tests/system-ovn.at index 28914a93f..a251bc5d3 100644 --- a/tests/system-ovn.at +++ b/tests/system-ovn.at @@ -13172,7 +13172,8 @@ NETNS_DAEMONIZE([vm2], [ncat -l -k 192.168.200.20 4242], [server2.pid]) NETNS_DAEMONIZE([vm3], [ncat -l -k 192.168.200.30 4242], [server3.pid]) check_snat() { - check ovn-nbctl lr-nat-del lr + # Use --if-exists since lr won't always have NATs present when we call this. + check ovn-nbctl --if-exists lr-nat-del lr check ovn-nbctl lr-nat-add lr snat 192.168.100.1 192.168.200.0/24 check ovn-nbctl --match="tcp && tcp.src == 2001" lr-nat-add lr snat 192.168.100.11 192.168.200.0/24 check ovn-nbctl --match="tcp && tcp.src == 2002" lr-nat-add lr snat 192.168.100.12 192.168.200.0/24 @@ -13193,7 +13194,8 @@ tcp,orig=(src=192.168.200.10,dst=192.168.100.10,sport=<cleared>,dport=<cleared>) } check_dnat() { - check ovn-nbctl lr-nat-del lr + # Use --if-exists since lr won't always have NATs present when we call this. + check ovn-nbctl --if-exists lr-nat-del lr check ovn-nbctl lr-nat-add lr dnat 192.168.100.100 192.168.200.10 check ovn-nbctl --match="tcp && tcp.src == 2001" lr-nat-add lr dnat 192.168.100.100 192.168.200.20 check ovn-nbctl --match="tcp && tcp.src == 2002" lr-nat-add lr dnat 192.168.100.100 192.168.200.30 @@ -13298,7 +13300,8 @@ NETNS_DAEMONIZE([vm2], [ncat -lk 2000::20 4242 > /dev/null], [server2.pid]) NETNS_DAEMONIZE([vm3], [ncat -lk 2000::30 4242 > /dev/null], [server3.pid]) check_snat() { - check ovn-nbctl lr-nat-del lr + # Use --if-exists since lr won't always have NATs present when we call this. + check ovn-nbctl --if-exists lr-nat-del lr check ovn-nbctl lr-nat-add lr snat 1000::1 2000::/64 check ovn-nbctl --match="tcp && tcp.src == 2001" lr-nat-add lr snat 1000::11 2000::/64 check ovn-nbctl --match="tcp && tcp.src == 2002" lr-nat-add lr snat 1000::12 2000::/64 @@ -13319,7 +13322,8 @@ tcp,orig=(src=2000::10,dst=1000::10,sport=<cleared>,dport=<cleared>),reply=(src= } check_dnat() { - check ovn-nbctl lr-nat-del lr + # Use --if-exists since lr won't always have NATs present when we call this. + check ovn-nbctl --if-exists lr-nat-del lr check ovn-nbctl lr-nat-add lr dnat 1000::100 2000::10 check ovn-nbctl --match="tcp && tcp.src == 2001" lr-nat-add lr dnat 1000::100 2000::20 check ovn-nbctl --match="tcp && tcp.src == 2002" lr-nat-add lr dnat 1000::100 2000::30 @@ -22234,7 +22238,6 @@ ADD_VETH(ext-foo, ext-foo, br-ext, "172.16.1.100/24", "00:10:10:01:02:13", \ # Create distributed nat and set gw chassis to non-exisrting # one - to check distributed NAT on non gw-chassis. check ovn-nbctl lrp-set-gateway-chassis rp-public hv2 -check ovn-nbctl lr-nat-del lr1 check ovn-nbctl --wait=hv lr-nat-add lr1 \ dnat_and_snat 172.16.0.1 192.168.1.2 sw01-x 00:00:04:00:00:01 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
