From patchwork Fri Oct 2 21:16:08 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Stringer X-Patchwork-Id: 525801 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (unknown [IPv6:2600:3c00::f03c:91ff:fe6e:bdf7]) by ozlabs.org (Postfix) with ESMTP id 353881402C4 for ; Sat, 3 Oct 2015 07:16:34 +1000 (AEST) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id 74FDD22C48B; Fri, 2 Oct 2015 14:16:33 -0700 (PDT) X-Original-To: dev@openvswitch.org Delivered-To: dev@openvswitch.org Received: from mx1e3.cudamail.com (mx1.cudamail.com [69.90.118.67]) by archives.nicira.com (Postfix) with ESMTPS id 7491922C48B for ; Fri, 2 Oct 2015 14:16:32 -0700 (PDT) Received: from bar2.cudamail.com (localhost [127.0.0.1]) by mx1e3.cudamail.com (Postfix) with ESMTPS id 67B0542013A for ; Fri, 2 Oct 2015 15:16:31 -0600 (MDT) X-ASG-Debug-ID: 1443820590-03dc537fe1ab1f60001-byXFYA Received: from mx1-pf1.cudamail.com ([192.168.24.1]) by bar2.cudamail.com with ESMTP id Uq51eHlTZf1428db (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Fri, 02 Oct 2015 15:16:31 -0600 (MDT) X-Barracuda-Envelope-From: joestringer@nicira.com X-Barracuda-RBL-Trusted-Forwarder: 192.168.24.1 Received: from unknown (HELO mail-pa0-f52.google.com) (209.85.220.52) by mx1-pf1.cudamail.com with ESMTPS (RC4-SHA encrypted); 2 Oct 2015 21:16:30 -0000 Received-SPF: unknown (mx1-pf1.cudamail.com: Multiple SPF records returned) X-Barracuda-RBL-Trusted-Forwarder: 209.85.220.52 Received: by pacex6 with SMTP id ex6so116492909pac.0 for ; Fri, 02 Oct 2015 14:16:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PrTp1c/q9AGuGhoct8LHuKR7yTsAs1/V+fBdYCqg+PY=; b=fa4Lvpe7v5wnUzT418SbZ59E5n76NGqdU/YtftqvGSMY+s+3gzJ49r9PPQckDlAGfo aHNSV4K501NwECJ4oBpgpq1rSty9y/6p+O9fbGyUksg2OOxLkQArVCDhCTvxA3enIKkg 3+OdatjUXzWIxrKKKT5mJC/x8j9AsrW4uj5Au4gb//i6RZuC9r7m5gk/nxKFm5ihfHYp QgVkI4suvgZUF/WPm3SWzaTWNFyEPqSmK4LK5QGDKNNIpdg0qtinIUwQTKoKbmdsm9NV 824R90pOAxkdayFNzDQneDD7TqzncxqbedaSDgurX9gnHIcB3MqTg1gyvoEAal8LoLlM XXhA== X-Gm-Message-State: ALoCoQmnDg8CWBSbbkccgRB9N2QrtWe4T/GTc766G8iqH6+3mEfrgxX30DHjjf8/DTVubalu9Cgy X-Received: by 10.68.163.5 with SMTP id ye5mr22222164pbb.120.1443820590141; Fri, 02 Oct 2015 14:16:30 -0700 (PDT) Received: from localhost.localdomain ([208.91.2.4]) by smtp.gmail.com with ESMTPSA id z6sm13633900pbt.51.2015.10.02.14.16.29 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 02 Oct 2015 14:16:29 -0700 (PDT) X-CudaMail-Envelope-Sender: joestringer@nicira.com X-Barracuda-Apparent-Source-IP: 208.91.2.4 From: Joe Stringer To: dev@openvswitch.org X-CudaMail-Whitelist-To: dev@openvswitch.org X-CudaMail-MID: CM-E1-1001083965 X-CudaMail-DTE: 100215 X-CudaMail-Originating-IP: 209.85.220.52 Date: Fri, 2 Oct 2015 14:16:08 -0700 X-ASG-Orig-Subj: [##CM-E1-1001083965##][PATCHv4 01/11] ofp-actions: Refactor set_field tokenization. Message-Id: <1443820578-9287-2-git-send-email-joestringer@nicira.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1443820578-9287-1-git-send-email-joestringer@nicira.com> References: <1443820578-9287-1-git-send-email-joestringer@nicira.com> X-Barracuda-Connect: UNKNOWN[192.168.24.1] X-Barracuda-Start-Time: 1443820591 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://web.cudamail.com:443/cgi-mod/mark.cgi X-ASG-Whitelist: Header =?UTF-8?B?eFwtY3VkYW1haWxcLXdoaXRlbGlzdFwtdG8=?= X-Virus-Scanned: by bsmtpd at cudamail.com X-Barracuda-BRTS-Status: 1 Subject: [ovs-dev] [PATCHv4 01/11] ofp-actions: Refactor set_field tokenization. X-BeenThere: dev@openvswitch.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dev-bounces@openvswitch.org Sender: "dev" Combine the codepaths for splitting "set_field" and "reg_load" string arguments into the value, key, and delimiter component. The only user-visible change is that reg_load will now provide a more meaningful error message when parsing input such as "reg_load:1". Signed-off-by: Joe Stringer Acked-by: Ben Pfaff --- v4: Reduce refactor to identifying key,delim,value. v3: Initial post. --- lib/ofp-actions.c | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/lib/ofp-actions.c b/lib/ofp-actions.c index 88f0f85..6c9e1cb 100644 --- a/lib/ofp-actions.c +++ b/lib/ofp-actions.c @@ -2476,6 +2476,39 @@ encode_SET_FIELD(const struct ofpact_set_field *sf, } } +/* Parses the input argument 'arg' into the key, value, and delimiter + * components that are common across the reg_load and set_field action format. + * + * With an argument like "1->metadata", sets the following pointers to + * point within 'arg': + * key: "metadata" + * value: "1" + * delim: "->" + * + * Returns NULL if successful, otherwise a malloc()'d string describing the + * error. The caller is responsible for freeing the returned string. */ +static char * OVS_WARN_UNUSED_RESULT +set_field_split_str(char *arg, char **key, char **value, char **delim) +{ + char *value_end; + + *value = arg; + value_end = strstr(arg, "->"); + *key = value_end + strlen("->"); + if (delim) { + *delim = value_end; + } + + if (!value_end) { + return xasprintf("%s: missing `->'", arg); + } + if (strlen(value_end) <= strlen("->")) { + return xasprintf("%s: missing field name following `->'", arg); + } + + return NULL; +} + /* Parses a "set_field" action with argument 'arg', appending the parsed * action to 'ofpacts'. * @@ -2492,16 +2525,11 @@ set_field_parse__(char *arg, struct ofpbuf *ofpacts, const struct mf_field *mf; char *error; - value = arg; - delim = strstr(arg, "->"); - if (!delim) { - return xasprintf("%s: missing `->'", arg); - } - if (strlen(delim) <= strlen("->")) { - return xasprintf("%s: missing field name following `->'", arg); + error = set_field_split_str(arg, &key, &value, &delim); + if (error) { + return error; } - key = delim + strlen("->"); mf = mf_from_name(key); if (!mf) { return xasprintf("%s is not a valid OXM field name", key); @@ -2546,13 +2574,15 @@ parse_reg_load(char *arg, struct ofpbuf *ofpacts) const char *full_arg = arg; uint64_t value = strtoull(arg, (char **) &arg, 0); struct mf_subfield dst; + char *key, *value_str; char *error; - if (strncmp(arg, "->", 2)) { - return xasprintf("%s: missing `->' following value", full_arg); + error = set_field_split_str(arg, &key, &value_str, NULL); + if (error) { + return error; } - arg += 2; - error = mf_parse_subfield(&dst, arg); + + error = mf_parse_subfield(&dst, key); if (error) { return error; }