From patchwork Tue Jun 27 18:11:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi-Hung Wei X-Patchwork-Id: 781324 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wxvCR1T3Pz9s2s for ; Wed, 28 Jun 2017 04:12:19 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="DVPgqkd8"; dkim-atps=neutral Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 2109CCBA; Tue, 27 Jun 2017 18:11:45 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 829EFCB8 for ; Tue, 27 Jun 2017 18:11:43 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pf0-f195.google.com (mail-pf0-f195.google.com [209.85.192.195]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 223471A0 for ; Tue, 27 Jun 2017 18:11:43 +0000 (UTC) Received: by mail-pf0-f195.google.com with SMTP id z6so5708968pfk.3 for ; Tue, 27 Jun 2017 11:11:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JAuJ79vB1eymzu/amUk8ZXnSUhY3CDEXYPHVAkci8Pk=; b=DVPgqkd83ZMPOAKVpWzZodGzhWV4xdIrygZzsPx7b+/blBQsPRIjtPg0ybGrfF5sgd rvzSfUEP12BAygmfiaXNJSh+71x9cloz9lMgtPV3bY1mxP7QSlXX5iXD2zY4OI66j0Ta kAsFHo995dCmqtFEUltMWU+zWn8zx9hVhAwrDyPrDouopZkY+LWxR0dqbrHFUV5wWhV3 tfRgAU/a6iG29tNcaqWvy/cyJ1gKRWVCXu0JdewMFfLvqllRM3dL8dJSlX1cVmDSQLYO FGjeCZomfdByIAzKzKpB2OwRnbFJV/D1Czj0w2ONzM0U4hoQ9UYqpn5eTWtbsoLElr5f 5fbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JAuJ79vB1eymzu/amUk8ZXnSUhY3CDEXYPHVAkci8Pk=; b=LidpePce17m6fO1KiwvY4BkPZcswWeXzy3nfuLNiA8PiNq05CQ/nw7C8Ab4/cdBgu+ T8MP7SBlkhY4fziKoZHQcxo0+f6prOmmNV2cuzHy4ZZsR1lyqkyS7I4jSwUxtvhRZ7/z X88DE+UlApORe6KBtdIrS3Zy8zi9u3T59bEF3DQhWOpIRJqE2Fo8n2FLHhQGfaYLdbpE MBdudcSMxMK7AUMzqJxJiBAnRER0tyCkAWt6eYNTbMgbKyxIX8l7eTNGo8VOtRruTZIZ XS1M1ILBQrbnpMZfm5CLlsf/x2KwtW6qK1eYDGstf4OWTnPl2PSeIbjeMHFLnC5g3+ej 4XrA== X-Gm-Message-State: AKS2vOyimoBaeWYWfxENOPEOnQHz06eZObJ9hG1mWv3nmkR91mSEjqKu NgNmp/td9/1LxFYv7LE= X-Received: by 10.99.165.28 with SMTP id n28mr6465686pgf.163.1498587102528; Tue, 27 Jun 2017 11:11:42 -0700 (PDT) Received: from sc9-mailhost3.vmware.com ([208.91.1.34]) by smtp.gmail.com with ESMTPSA id j14sm7350852pgt.7.2017.06.27.11.11.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 27 Jun 2017 11:11:42 -0700 (PDT) From: Yi-Hung Wei To: dev@openvswitch.org Date: Tue, 27 Jun 2017 11:11:32 -0700 Message-Id: <1498587094-107805-2-git-send-email-yihung.wei@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1498587094-107805-1-git-send-email-yihung.wei@gmail.com> References: <1498587094-107805-1-git-send-email-yihung.wei@gmail.com> X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM autolearn=no version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH v3 1/3] conntrack: Move ct_state parsing to lib/flow.c X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org This patch moves conntrack state parsing function from ovn-trace.c to lib/flow.c, because it will be used by ofproto/trace unixctl command later on. It also updates the ct_state checking logic, since we no longer assume CS_TRACKED is enable by default. Signed-off-by: Yi-Hung Wei --- lib/flow.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++ lib/flow.h | 3 +++ ovn/utilities/ovn-trace.c | 32 +++++----------------- 3 files changed, 78 insertions(+), 25 deletions(-) diff --git a/lib/flow.c b/lib/flow.c index d73e796a2f45..119ce1a7f20b 100644 --- a/lib/flow.c +++ b/lib/flow.c @@ -1047,6 +1047,74 @@ ct_state_from_string(const char *s) return 0; } +/* Parses conntrack state from 'state_str'. If it is parsed successfully, + * stores the parsed ct_state in 'ct_state', and returns true. Otherwise, + * returns false, and reports error message in 'ds'. */ +bool +parse_ct_state(const char *state_str, uint32_t default_state, + uint32_t *ct_state, struct ds *ds) +{ + uint32_t state = default_state; + char *state_s = xstrdup(state_str); + char *save_ptr = NULL; + + for (char *cs = strtok_r(state_s, ", ", &save_ptr); cs; + cs = strtok_r(NULL, ", ", &save_ptr)) { + uint32_t bit = ct_state_from_string(cs); + if (!bit) { + ds_put_format(ds, "%s: unknown connection tracking state flag", + cs); + return false; + } + state |= bit; + } + + *ct_state = state; + free(state_s); + + return true; +} + +/* Checks the given conntrack state 'state' according to the constraints + * listed in ovs-fields (7). Returns true if it is valid. Otherwise, returns + * false, and reports error in 'ds'. */ +bool +validate_ct_state(uint32_t state, struct ds *ds) +{ + bool valid_ct_state = true; + struct ds d_str = DS_EMPTY_INITIALIZER; + + format_flags(&d_str, ct_state_to_string, state, '|'); + + if (state && !(state & CS_TRACKED)) { + ds_put_format(ds, "%s: invalid connection state: " + "If \"trk\" is unset, no other flags are set\n", + ds_cstr(&d_str)); + valid_ct_state = false; + } + if (state & CS_INVALID && state & ~(CS_TRACKED | CS_INVALID)) { + ds_put_format(ds, "%s: invalid connection state: " + "when \"inv\" is set, only \"trk\" may also be set\n", + ds_cstr(&d_str)); + valid_ct_state = false; + } + if (state & CS_NEW && state & CS_ESTABLISHED) { + ds_put_format(ds, "%s: invalid connection state: " + "\"new\" and \"est\" are mutually exclusive\n", + ds_cstr(&d_str)); + valid_ct_state = false; + } + if (state & CS_NEW && state & CS_REPLY_DIR) { + ds_put_format(ds, "%s: invalid connection state: " + "\"new\" and \"rpy\" are mutually exclusive\n", + ds_cstr(&d_str)); + valid_ct_state = false; + } + + ds_destroy(&d_str); + return valid_ct_state; +} + /* Clears the fields in 'flow' associated with connection tracking. */ void flow_clear_conntrack(struct flow *flow) diff --git a/lib/flow.h b/lib/flow.h index 68bd4f3c5149..5889c4decaaa 100644 --- a/lib/flow.h +++ b/lib/flow.h @@ -75,6 +75,9 @@ void flow_get_metadata(const struct flow *, struct match *flow_metadata); const char *ct_state_to_string(uint32_t state); uint32_t ct_state_from_string(const char *); +bool parse_ct_state(const char *state_str, uint32_t default_state, + uint32_t *ct_state, struct ds *); +bool validate_ct_state(uint32_t state, struct ds *); void flow_clear_conntrack(struct flow *); char *flow_to_string(const struct flow *, const struct ofputil_port_map *); diff --git a/ovn/utilities/ovn-trace.c b/ovn/utilities/ovn-trace.c index 8bdb7d8a304d..ab56221d7829 100644 --- a/ovn/utilities/ovn-trace.c +++ b/ovn/utilities/ovn-trace.c @@ -169,34 +169,16 @@ default_ovs(void) static void parse_ct_option(const char *state_s_) { - uint32_t state = CS_TRACKED; - - char *state_s = xstrdup(state_s_); - char *save_ptr = NULL; - for (char *cs = strtok_r(state_s, ", ", &save_ptr); cs; - cs = strtok_r(NULL, ", ", &save_ptr)) { - uint32_t bit = ct_state_from_string(cs); - if (!bit) { - ovs_fatal(0, "%s: unknown connection tracking state flag", cs); - } - state |= bit; - } - free(state_s); + uint32_t state; + struct ds ds = DS_EMPTY_INITIALIZER; - /* Check constraints listed in ovs-fields(7). */ - if (state & CS_INVALID && state & ~(CS_TRACKED | CS_INVALID)) { - VLOG_WARN("%s: invalid connection state: " - "when \"inv\" is set, only \"trk\" may also be set", - state_s_); - } - if (state & CS_NEW && state & CS_ESTABLISHED) { - VLOG_WARN("%s: invalid connection state: " - "\"new\" and \"est\" are mutually exclusive", state_s_); + if (!parse_ct_state(state_s_, CS_TRACKED, &state, &ds)) { + ovs_fatal(0, "%s", ds_cstr(&ds)); } - if (state & CS_NEW && state & CS_REPLY_DIR) { - VLOG_WARN("%s: invalid connection state: " - "\"new\" and \"rpy\" are mutually exclusive", state_s_); + if (!validate_ct_state(state, &ds)) { + VLOG_WARN("%s", ds_cstr(&ds)); } + ds_destroy(&ds); ct_states = xrealloc(ct_states, (n_ct_states + 1) * sizeof *ct_states); ct_states[n_ct_states++] = state;