From patchwork Wed Aug 8 21:09:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Darrell Ball X-Patchwork-Id: 955248 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="rncoGbGm"; dkim-atps=neutral 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 41m3t971xSz9s5H for ; Thu, 9 Aug 2018 07:09:37 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 5B1BBBD8; Wed, 8 Aug 2018 21:09:34 +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 E55F09D7 for ; Wed, 8 Aug 2018 21:09:32 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-pg1-f175.google.com (mail-pg1-f175.google.com [209.85.215.175]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id B79F11FB for ; Wed, 8 Aug 2018 21:09:31 +0000 (UTC) Received: by mail-pg1-f175.google.com with SMTP id f14-v6so1640178pgv.13 for ; Wed, 08 Aug 2018 14:09:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=w9zrjAjnqlO7mzVuX+QDNcV2kQ9BgVZKK1sxYiKOU9s=; b=rncoGbGmbHNsW8nOnzRMkK9/M2+YIYpPYbJ+wkvkoJl5uGYCyZEDXSnv1tqDq+fGjP XxQ6BzyaO3Oj/OvAtwyY2cKNdmXQWMxU1oDwOp82iTKYz5FLVLhOWnRZAp9looMHb/vy xIDn+LXlfgPod9e0YFXHaV6ITg9h5TQL4wjJvmSb75vMU9KY93u85HnVnzRfj+C+oXmH 9vEa7yKZI+5uReqpq1wIUUq8x/ezm3cNFKWRZRhlG4x1TUMcY2ebRNfbVF4rJS8whOtL i1pjqVfcvWfJa31D5KXs80JexdNaQh3h3pyRfDqbEU54uFCYIuO3Y/LBzIIL1d/55TOz /qrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=w9zrjAjnqlO7mzVuX+QDNcV2kQ9BgVZKK1sxYiKOU9s=; b=LrVhYzu9aNGIUbJV+FiJsc/1p4tzW41dvjvlcLUlwIplj7pSCd5l2MFuPDheR/1Pbh rpsxQYvMnXUTrthbmQ7XKbbDFeNRiZxTLXpaoutbMUj1iXzPHmD63cAh4FXeGA8+PpZx 8aSjJnq509J+wiM/OneuFseDHfc5YUNFG8KFXkf5xEUVWyOrjH02fsTLzqU17jJk7oiH iZYWtj6uEjPr4xRM08GjK4k1rO7z8C31IQITkHbefqAFRRg5wG4TE4isS2vWV9bJvF2B 8qY2Am7uY/h8P7rxFww2CWaHMcsabp8iyyyiTgzIJQrEl+/ywS1EAqfSz7GAhIdbgFw/ oKxg== X-Gm-Message-State: AOUpUlHFmhPpZ2Qqw4yBQxeqxmhiTEnAKBHRLC3XZ1bMBEZB5NXMlaOS FauhYBWsPoBcdkThuYcYGI0= X-Google-Smtp-Source: AA+uWPyPZUipwdom8BU1pfaxHRfSJec/tYBeM9177qPo/wR2/JEocUPmwqFvhTXPyJ0ojKlfxZcCcQ== X-Received: by 2002:a62:87ce:: with SMTP id i197-v6mr4631374pfe.62.1533762571332; Wed, 08 Aug 2018 14:09:31 -0700 (PDT) Received: from ubuntu.localdomain (c-73-162-236-45.hsd1.ca.comcast.net. [73.162.236.45]) by smtp.gmail.com with ESMTPSA id s27-v6sm10704459pfk.133.2018.08.08.14.09.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 08 Aug 2018 14:09:30 -0700 (PDT) From: Darrell Ball To: dlu998@gmail.com, dev@openvswitch.org Date: Wed, 8 Aug 2018 14:09:14 -0700 Message-Id: <1533762554-11008-1-git-send-email-dlu998@gmail.com> X-Mailer: git-send-email 1.9.1 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE 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 v7] dpctl: Make opt_dpif_open() more general. 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 By making opt_dpif_open() more general, it can be used effectively by all potential callers and avoids trying to open potentially bogus datapaths provided by the user. Also, the error handling is improved by reducing bogus errors and having more specific real errors. Signed-off-by: Darrell Ball --- lib/dpctl.c | 56 ++++++++++++++++++++++++++++++++++++++++++++----- tests/system-traffic.at | 10 ++++----- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/lib/dpctl.c b/lib/dpctl.c index c600eeb..ef057aa 100644 --- a/lib/dpctl.c +++ b/lib/dpctl.c @@ -187,18 +187,64 @@ parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp) return result; } +static bool +dp_exists(const char *queried_dp) +{ + struct sset dpif_names = SSET_INITIALIZER(&dpif_names), + dpif_types = SSET_INITIALIZER(&dpif_types); + bool found = false; + char *queried_name, *queried_type; + + dp_parse_name(queried_dp, &queried_name, &queried_type); + dp_enumerate_types(&dpif_types); + + if (!sset_contains(&dpif_types, queried_type)) { + goto out; + } + + int error = dp_enumerate_names(queried_type, &dpif_names); + if (!error && sset_contains(&dpif_names, queried_name)) { + found = true; + goto out; + } + +out: + sset_destroy(&dpif_names); + sset_destroy(&dpif_types); + return found; +} + +static bool +dp_arg_exists(int argc, const char *argv[]) +{ + if (argc > 1 && dp_exists(argv[1])) { + return true; + } + return false; +} + /* Open a dpif with an optional name argument. * - * The datapath name is not a mandatory parameter for this command. If - * it is not specified -- so 'argc' < 'max_args' -- we retrieve it from - * the current setup, assuming only one exists. On success stores the - * opened dpif in '*dpifp'. */ + * The datapath name is not a mandatory parameter for this command. If it is + * not specified, we retrieve it from the current setup, assuming only one + * exists. On success stores the opened dpif in '*dpifp'. */ static int opt_dpif_open(int argc, const char *argv[], struct dpctl_params *dpctl_p, uint8_t max_args, struct dpif **dpifp) { + char *dpname; + if (dp_arg_exists(argc, argv)) { + dpname = xstrdup(argv[1]); + } else if (argc != max_args) { + dpname = get_one_dp(dpctl_p); + } else { + /* If the arguments are the maximum possible number and there is no + * valid datapath argument, then we fall into the case of dpname is + * NULL, since this is an error. */ + dpname = NULL; + } + int error = 0; - char *dpname = argc >= max_args ? xstrdup(argv[1]) : get_one_dp(dpctl_p); if (!dpname) { error = EINVAL; dpctl_error(dpctl_p, error, "datapath not found"); diff --git a/tests/system-traffic.at b/tests/system-traffic.at index cbd9542..a7c8a24 100644 --- a/tests/system-traffic.at +++ b/tests/system-traffic.at @@ -1040,7 +1040,7 @@ AT_CHECK([ovs-appctl dpctl/flush-conntrack zone=5 $ICMP_TUPLE]) AT_CHECK([ovs-appctl dpctl/dump-conntrack | grep "orig=.src=10\.1\.1\.2,"], [1], [dnl ]) -OVS_TRAFFIC_VSWITCHD_STOP(["/could not create datapath/d"]) +OVS_TRAFFIC_VSWITCHD_STOP AT_CLEANUP AT_SETUP([conntrack - IPv4 ping]) @@ -1124,17 +1124,17 @@ ovs-appctl: ovs-vswitchd: server returned an error ]) AT_CHECK([ovs-appctl dpctl/ct-set-maxconns one-bad-dp 10], [2], [], [dnl -ovs-vswitchd: opening datapath (Address family not supported by protocol) +ovs-vswitchd: datapath not found (Invalid argument) ovs-appctl: ovs-vswitchd: server returned an error ]) AT_CHECK([ovs-appctl dpctl/ct-get-maxconns one-bad-dp], [2], [], [dnl -ovs-vswitchd: opening datapath (Address family not supported by protocol) +ovs-vswitchd: datapath not found (Invalid argument) ovs-appctl: ovs-vswitchd: server returned an error ]) AT_CHECK([ovs-appctl dpctl/ct-get-nconns one-bad-dp], [2], [], [dnl -ovs-vswitchd: opening datapath (Address family not supported by protocol) +ovs-vswitchd: datapath not found (Invalid argument) ovs-appctl: ovs-vswitchd: server returned an error ]) @@ -1164,7 +1164,7 @@ AT_CHECK([ovs-appctl dpctl/ct-get-maxconns], [], [dnl 10 ]) -OVS_TRAFFIC_VSWITCHD_STOP(["/could not create datapath one-bad-dp of unknown type system/d"]) +OVS_TRAFFIC_VSWITCHD_STOP AT_CLEANUP AT_SETUP([conntrack - IPv6 ping])