From patchwork Mon Jun 26 17:19:00 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: 780804 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 3wxG7x2LGlz9sNS for ; Tue, 27 Jun 2017 03:22:05 +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="jpaw6hFy"; dkim-atps=neutral Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 736C0BE7; Mon, 26 Jun 2017 17:19:10 +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 78FDCBCA for ; Mon, 26 Jun 2017 17:19:06 +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 2536A3E0 for ; Mon, 26 Jun 2017 17:19:04 +0000 (UTC) Received: by mail-pf0-f195.google.com with SMTP id s66so981561pfs.2 for ; Mon, 26 Jun 2017 10:19:04 -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=jpaw6hFyEGbjpZ1hoAFi264URb06mU7ptUDvW/8wOpnyWGxpWusx5toaDzIzQzCL1n YnyY7oY7cwXItITcwc/ouL7z5y8hBdvhzc1cpWTMd74jY8m1FKKFR9TUwovp4JhyJmi2 bVML/4OCeRKeLsT004lZ3S4cYd5hOUAUCN9LO3rE21LkHyMe8Covml5byH083crbZSK1 +VtrkAzDIUdj7+83kPogisMmQr+2tpLS9B1z03Mq7ZYTTDHcDh/kKqHwf51AQYoonvzp vXSkZk0/OA3DrVVJKVIhlXnK1a3wAk3xNpW8MYckp1sA/UEZ+ArmrqqMkcIeVq+nxHca LVKQ== 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=OfH2wI7hlZqS+rPNGqjhuTTVpq68+AYUmAmRdhzo6ZY1eWATqB7anna1HTKbYDgZ2D UJVcM2/SI+5mjoBOfJuYIi1wlXyIXvbkHFSEHhqBoG+dWwUybd6d/Yzv2ZvgyTHwr40o CopvkrXGqnzTXDu9bq3Rt4OkpTkyi7ctTy+vJ0Hz5g7TY2YSlYywGau6b30Teef1HzuB 5nAVgtKBRCN7BfUfQ3/LxZlYITwUIg7TgAbtL+5IuVp1ujFh/8iAAdactG/GEeZJebsB s7COrhlJPpiTUt6pwsoejl37lLUhj7PRCR7NwmxIqIBY+ohV8IlXA4lSiXfkzZf25L7z eebw== X-Gm-Message-State: AKS2vOwobp0wjlgRlYidWeX5wZk2refUEAGMklkJevgRglEHRULCdFVx vLHhS73p+8bHJM1QOdw= X-Received: by 10.99.107.5 with SMTP id g5mr1148709pgc.47.1498497543548; Mon, 26 Jun 2017 10:19:03 -0700 (PDT) Received: from sc9-mailhost2.vmware.com ([208.91.1.34]) by smtp.gmail.com with ESMTPSA id l3sm1120293pfk.34.2017.06.26.10.19.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Jun 2017 10:19:03 -0700 (PDT) From: Yi-Hung Wei To: dev@openvswitch.org Date: Mon, 26 Jun 2017 10:19:00 -0700 Message-Id: <1498497542-48323-2-git-send-email-yihung.wei@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1498497542-48323-1-git-send-email-yihung.wei@gmail.com> References: <1498497542-48323-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 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;