From patchwork Wed Oct 18 06:28:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ales Musil X-Patchwork-Id: 1850575 X-Patchwork-Delegate: i.maximets@samsung.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KExT0Jnk; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4S9LXs1cTyz20Zj for ; Wed, 18 Oct 2023 17:29:17 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 6CC7582226; Wed, 18 Oct 2023 06:29:15 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 6CC7582226 Authentication-Results: smtp1.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KExT0Jnk X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Wz3vB7GbzE7W; Wed, 18 Oct 2023 06:29:14 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id 559B982230; Wed, 18 Oct 2023 06:29:13 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 559B982230 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 28399C008C; Wed, 18 Oct 2023 06:29:13 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp2.osuosl.org (smtp2.osuosl.org [IPv6:2605:bc80:3010::133]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1A680C0088 for ; Wed, 18 Oct 2023 06:29:12 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id 110084094C for ; Wed, 18 Oct 2023 06:29:10 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org 110084094C Authentication-Results: smtp2.osuosl.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=KExT0Jnk X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id dL8hdTe7MZBu for ; Wed, 18 Oct 2023 06:29:09 +0000 (UTC) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp2.osuosl.org (Postfix) with ESMTPS id BBCDB403C7 for ; Wed, 18 Oct 2023 06:29:08 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org BBCDB403C7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1697610547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3k3P6vcTNe5eRTcIRiLuw+xg+9uVfsRzfnOskD3mcSA=; b=KExT0JnkdFbQMhPFkWhedphbzjVu5lg6FAqO5++UDJQvkA3DkogtW8jx4FgNPCDuUbF5bO 46vKl9hCkkKPYmHs6VgJaThnxBebDjFqGwqTUewsK2js477YTnh5f20gb8n/DHY7YNDrwa fUvuUeCCiweQ2EMihGVidQF1NpE985g= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-146-Z42fa9oJPK2LUUb0dYn30Q-1; Wed, 18 Oct 2023 02:28:52 -0400 X-MC-Unique: Z42fa9oJPK2LUUb0dYn30Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5C980185A7B3; Wed, 18 Oct 2023 06:28:52 +0000 (UTC) Received: from amusil.. (unknown [10.34.130.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id AA054C15BB8; Wed, 18 Oct 2023 06:28:51 +0000 (UTC) From: Ales Musil To: dev@openvswitch.org Date: Wed, 18 Oct 2023 08:28:49 +0200 Message-ID: <20231018062850.67677-2-amusil@redhat.com> In-Reply-To: <20231018062850.67677-1-amusil@redhat.com> References: <20231018062850.67677-1-amusil@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Cc: i.maximets@ovn.org, dceara@redhat.com Subject: [ovs-dev] [PATCH v3 2/3] dpctl, ovs-ofctl: Unify parsing of ct-flush arguments. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" In order to make the command extensible unify the arguments parsing into single function. This will be later on used for the mark and labels arguments. Signed-off-by: Ales Musil --- v3: Rebase on top of current master. Fix the system-tests failure. --- include/openvswitch/ofp-ct.h | 5 ++-- lib/dpctl.c | 41 ++++--------------------------- lib/ofp-ct.c | 47 +++++++++++++++++++++++++++++++++++- tests/system-traffic.at | 2 +- utilities/ovs-ofctl.c | 37 ++++++---------------------- 5 files changed, 62 insertions(+), 70 deletions(-) diff --git a/include/openvswitch/ofp-ct.h b/include/openvswitch/ofp-ct.h index c8023c309..cd6192e6f 100644 --- a/include/openvswitch/ofp-ct.h +++ b/include/openvswitch/ofp-ct.h @@ -58,8 +58,9 @@ bool ofp_ct_tuple_is_zero(const struct ofp_ct_tuple *, uint8_t ip_proto); bool ofp_ct_tuple_is_five_tuple(const struct ofp_ct_tuple *, uint8_t ip_proto); void ofp_ct_match_format(struct ds *, const struct ofp_ct_match *); -bool ofp_ct_tuple_parse(struct ofp_ct_tuple *, const char *, - struct ds *, uint8_t *ip_proto, uint16_t *l3_type); +bool ofp_ct_match_parse(const char **, int argc, struct ds *, + struct ofp_ct_match *, bool *with_zone, + uint16_t *zone_id); enum ofperr ofp_ct_match_decode(struct ofp_ct_match *, bool *with_zone, uint16_t *zone_id, const struct ofp_header *); diff --git a/lib/dpctl.c b/lib/dpctl.c index cd12625a1..bbab5881e 100644 --- a/lib/dpctl.c +++ b/lib/dpctl.c @@ -1773,48 +1773,17 @@ dpctl_flush_conntrack(int argc, const char *argv[], struct dpif *dpif = NULL; struct ofp_ct_match match = {0}; struct ds ds = DS_EMPTY_INITIALIZER; - uint16_t zone, *pzone = NULL; + uint16_t zone; int error; int args = argc - 1; - int zone_pos = 1; + bool with_zone = false; if (dp_arg_exists(argc, argv)) { args--; - zone_pos = 2; - } - - /* Parse zone. */ - if (args && !strncmp(argv[zone_pos], "zone=", 5)) { - if (!ovs_scan(argv[zone_pos], "zone=%"SCNu16, &zone)) { - ds_put_cstr(&ds, "failed to parse zone"); - error = EINVAL; - goto error; - } - pzone = &zone; - args--; - } - - /* Parse ct tuples. */ - for (int i = 0; i < 2; i++) { - if (!args) { - break; - } - - struct ofp_ct_tuple *tuple = - i ? &match.tuple_reply : &match.tuple_orig; - const char *arg = argv[argc - args]; - - if (arg[0] && !ofp_ct_tuple_parse(tuple, arg, &ds, &match.ip_proto, - &match.l3_type)) { - error = EINVAL; - goto error; - } - args--; } - /* Report error if there is more than one unparsed argument. */ - if (args > 0) { - ds_put_cstr(&ds, "invalid arguments"); + if (args && !ofp_ct_match_parse(&argv[argc - args], args, &ds, &match, + &with_zone, &zone)) { error = EINVAL; goto error; } @@ -1825,7 +1794,7 @@ dpctl_flush_conntrack(int argc, const char *argv[], return error; } - error = ct_dpif_flush(dpif, pzone, &match); + error = ct_dpif_flush(dpif, with_zone ? &zone : NULL, &match); if (!error) { dpif_close(dpif); return 0; diff --git a/lib/ofp-ct.c b/lib/ofp-ct.c index 85a9d8bec..32aeb5455 100644 --- a/lib/ofp-ct.c +++ b/lib/ofp-ct.c @@ -98,7 +98,7 @@ ofp_ct_match_format(struct ds *ds, const struct ofp_ct_match *match) /* Parses a specification of a conntrack 5-tuple from 's' into 'tuple'. * Returns true on success. Otherwise, returns false and puts the error * message in 'ds'. */ -bool +static bool ofp_ct_tuple_parse(struct ofp_ct_tuple *tuple, const char *s, struct ds *ds, uint8_t *ip_proto, uint16_t *l3_type) { @@ -216,6 +216,51 @@ error: return false; } +/* Parses a specification of a conntrack match from 'argv' into 'match'. + * Returns true on success. Otherwise, returns false and puts the error + * message in 'ds'. */ +bool +ofp_ct_match_parse(const char **argv, int argc, struct ds *ds, + struct ofp_ct_match *match, bool *with_zone, + uint16_t *zone_id) +{ + int args = argc; + + /* Parse zone. */ + if (args && !strncmp(argv[argc - args], "zone=", 5)) { + if (!ovs_scan(argv[argc - args], "zone=%"SCNu16, zone_id)) { + ds_put_cstr(ds, "failed to parse zone"); + return false; + } + *with_zone = true; + args--; + } + + /* Parse ct tuples. */ + for (int i = 0; i < 2; i++) { + if (!args) { + break; + } + + struct ofp_ct_tuple *tuple = + i ? &match->tuple_reply : &match->tuple_orig; + const char *arg = argv[argc - args]; + + if (arg[0] && !ofp_ct_tuple_parse(tuple, arg, ds, &match->ip_proto, + &match->l3_type)) { + return false; + } + args--; + } + + if (args > 0) { + ds_put_cstr(ds, "invalid arguments"); + return false; + } + + return true; +} + static enum ofperr ofpprop_pull_ipv6(struct ofpbuf *property, struct in6_addr *addr, uint16_t *l3_type) diff --git a/tests/system-traffic.at b/tests/system-traffic.at index 418cd32fe..ec65ca5de 100644 --- a/tests/system-traffic.at +++ b/tests/system-traffic.at @@ -2737,7 +2737,7 @@ AT_CHECK([ovs-ofctl ct-flush br0 zone=1 'ct_nw_src=10.1.1.1' 'ct_nw_dst=10.1.1.1 AT_CHECK([grep -q "command takes at most 4 arguments" stderr]) AT_CHECK([ovs-ofctl ct-flush br0 'ct_nw_src=10.1.1.1' 'ct_nw_dst=10.1.1.1' invalid], [1], [ignore], [stderr]) -AT_CHECK([grep -q "Invalid arguments" stderr]) +AT_CHECK([grep -q "invalid arguments" stderr]) OVS_TRAFFIC_VSWITCHD_STOP AT_CLEANUP diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c index 24d0941cf..79d42dd0b 100644 --- a/utilities/ovs-ofctl.c +++ b/utilities/ovs-ofctl.c @@ -3060,42 +3060,19 @@ ofctl_ct_flush(struct ovs_cmdl_context *ctx) struct vconn *vconn; struct ofp_ct_match match = {0}; struct ds ds = DS_EMPTY_INITIALIZER; - uint16_t zone, *pzone = NULL; + uint16_t zone; int args = ctx->argc - 2; + bool with_zone = false; - /* Parse zone. */ - if (args && !strncmp(ctx->argv[2], "zone=", 5)) { - if (!ovs_scan(ctx->argv[2], "zone=%"SCNu16, &zone)) { - ovs_fatal(0, "Failed to parse zone"); - } - pzone = &zone; - args--; - } - - /* Parse ct tuples. */ - for (int i = 0; i < 2; i++) { - if (!args) { - break; - } - - struct ofp_ct_tuple *tuple = - i ? &match.tuple_reply : &match.tuple_orig; - const char *arg = ctx->argv[ctx->argc - args]; - - if (arg[0] && !ofp_ct_tuple_parse(tuple, arg, &ds, &match.ip_proto, - &match.l3_type)) { - ovs_fatal(0, "Failed to parse ct-tuple: %s", ds_cstr(&ds)); - } - args--; - } - - if (args > 0) { - ovs_fatal(0, "Invalid arguments"); + if (args && !ofp_ct_match_parse((const char **) &ctx->argv[2], + args, &ds, &match, &with_zone, &zone)) { + ovs_fatal(0, "Failed to parse CT match: %s", ds_cstr(&ds)); } open_vconn(ctx->argv[1], &vconn); enum ofp_version version = vconn_get_version(vconn); - struct ofpbuf *msg = ofp_ct_match_encode(&match, pzone, version); + struct ofpbuf *msg = + ofp_ct_match_encode(&match, with_zone ? &zone : NULL, version); ds_destroy(&ds); transact_noreply(vconn, msg);