From patchwork Tue Sep 1 20:35:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 513054 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from archives.nicira.com (li376-54.members.linode.com [96.126.127.54]) by ozlabs.org (Postfix) with ESMTP id EAEC3140134 for ; Wed, 2 Sep 2015 06:35:58 +1000 (AEST) Received: from archives.nicira.com (localhost [127.0.0.1]) by archives.nicira.com (Postfix) with ESMTP id 53CB210661; Tue, 1 Sep 2015 13:35:57 -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 5A5EE10604 for ; Tue, 1 Sep 2015 13:35:56 -0700 (PDT) Received: from bar5.cudamail.com (localhost [127.0.0.1]) by mx1e3.cudamail.com (Postfix) with ESMTPS id 9EC59420510 for ; Tue, 1 Sep 2015 14:35:55 -0600 (MDT) X-ASG-Debug-ID: 1441139754-09eadd1348896f0001-byXFYA Received: from mx1-pf2.cudamail.com ([192.168.24.2]) by bar5.cudamail.com with ESMTP id oy3HJV8Yk9AB1MqN (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 01 Sep 2015 14:35:54 -0600 (MDT) X-Barracuda-Envelope-From: blp@nicira.com X-Barracuda-RBL-Trusted-Forwarder: 192.168.24.2 Received: from unknown (HELO mail-pa0-f51.google.com) (209.85.220.51) by mx1-pf2.cudamail.com with ESMTPS (RC4-SHA encrypted); 1 Sep 2015 20:35:54 -0000 Received-SPF: unknown (mx1-pf2.cudamail.com: Multiple SPF records returned) X-Barracuda-RBL-Trusted-Forwarder: 209.85.220.51 Received: by pacfv12 with SMTP id fv12so6781667pac.2 for ; Tue, 01 Sep 2015 13:35:54 -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; bh=84e7BnXJ8cQe56CyuECchUEtOZkCA12Qg7gDnbfXhkY=; b=Q8qIrTyKQ5wv0YPUOT8Nk/XpVK6v4ybvT7XqEVPCYuQ8HOYiY9KOB7qUOaWW0+h+9u Lk+qaEkHHMqmu5Jk7Cxy9/2MOnkDogg6PH38XvvP5ivv2Xqaz86OntOutvOIV5fNhdpr ULvt2eFOZPnl2eTnxKXf1rlL/2j10dRmj/+DQml9Odnnf0rK8afzAQfAT16SlDuZ1SHL QPWpLkrsHEDNl49ksOtiZvvtcroOmd4/iLi/azoTkjFI35YWZz0fWP190TWcaiHe0FkE EGNwefdDeaavoMGx44RN6I5Gy5qxF+Mo58D4DRHQAYcpBqFyyUYwozJmPurglxcQjD8Y 90Tg== X-Gm-Message-State: ALoCoQkt0sPLLUmCFWIstvgFoooyHg0phuknHOQV8roqzbPgLC1EVYfi5iPliy7n7R3NfZ8QFRUE X-Received: by 10.69.14.227 with SMTP id fj3mr49320031pbd.144.1441139754198; Tue, 01 Sep 2015 13:35:54 -0700 (PDT) Received: from sigabrt.benpfaff.org ([208.91.2.4]) by smtp.gmail.com with ESMTPSA id vv2sm19263170pab.21.2015.09.01.13.35.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 01 Sep 2015 13:35:52 -0700 (PDT) X-CudaMail-Envelope-Sender: blp@nicira.com X-Barracuda-Apparent-Source-IP: 208.91.2.4 From: Ben Pfaff To: dev@openvswitch.org X-CudaMail-Whitelist-To: dev@openvswitch.org X-CudaMail-MID: CM-E2-831080917 X-CudaMail-DTE: 090115 X-CudaMail-Originating-IP: 209.85.220.51 Date: Tue, 1 Sep 2015 13:35:51 -0700 X-ASG-Orig-Subj: [##CM-E2-831080917##][PATCH] smap: New macro SMAP_INIT1 for initializing immutable 1-member smaps. Message-Id: <1441139751-8836-1-git-send-email-blp@nicira.com> X-Mailer: git-send-email 2.1.3 X-Barracuda-Connect: UNKNOWN[192.168.24.2] X-Barracuda-Start-Time: 1441139754 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 Cc: Ben Pfaff Subject: [ovs-dev] [PATCH] smap: New macro SMAP_INIT1 for initializing immutable 1-member smaps. 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" Reviewing the ovn-controller code I started to notice a common pattern: struct smap ext_ids = SMAP_INITIALIZER(&ext_ids); smap_add(&ext_ids, "ovn-patch-port", network); ovsrec_port_set_external_ids(port, &ext_ids); smap_destroy(&ext_ids); This seemed like a bit too much code for something as simple as initializing an smap with a single key-value pair. This commit allows the code to be reduced to just: const struct smap ids = SMAP_INIT1(&ids, "ovn-patch-port", network); ovsrec_port_set_external_ids(port, &ids); This new form also eliminates multiple memory allocation and free operations, but I doubt that has any real effect on performance; the primary goal here is code readability. Signed-off-by: Ben Pfaff Acked-by: Russell Bryant --- lib/hmap.h | 6 ++++++ lib/smap.h | 19 +++++++++++++++++++ ovn/controller/encaps.c | 6 ++---- ovn/controller/ovn-controller.c | 10 +++------- ovn/northd/ovn-northd.c | 19 ++++++++----------- utilities/ovs-vsctl.c | 4 +--- 6 files changed, 39 insertions(+), 25 deletions(-) diff --git a/lib/hmap.h b/lib/hmap.h index cab567e..7c87c33 100644 --- a/lib/hmap.h +++ b/lib/hmap.h @@ -68,6 +68,12 @@ struct hmap { #define HMAP_INITIALIZER(HMAP) \ { (struct hmap_node **const) &(HMAP)->one, NULL, 0, 0 } +/* Initializer for an immutable struct hmap 'HMAP' that contains a single + * 'NODE'. */ +#define HMAP_INIT1(HMAP, NODE) { \ + CONST_CAST(struct hmap_node **, &(HMAP)->one), NODE, 0, 1 } +#define HMAP_NODE_INIT(HASH) { HASH, NULL } + /* Initialization. */ void hmap_init(struct hmap *); void hmap_destroy(struct hmap *); diff --git a/lib/smap.h b/lib/smap.h index 846e384..cf9ed3e 100644 --- a/lib/smap.h +++ b/lib/smap.h @@ -39,6 +39,25 @@ struct smap_node { #define SMAP_FOR_EACH_SAFE(SMAP_NODE, NEXT, SMAP) \ HMAP_FOR_EACH_SAFE (SMAP_NODE, NEXT, node, &(SMAP)->map) +/* Initializer for an immutable struct smap 'SMAP' that contains a single + * 'KEY'-'VALUE' pair, e.g. + * + * const struct smap smap = SMAP1_INIT1(&smap, "key", "value"); + * + * An smap initialized this way must not be modified or destroyed. + * + * The 'KEY' argument is evaluated multiple times. + */ +#define SMAP_INIT1(SMAP, KEY, VALUE) { \ + HMAP_INIT1(&(SMAP)->map, \ + (&(struct smap_node) SMAP_NODE_INIT(KEY, VALUE).node)) \ + } +#define SMAP_NODE_INIT(KEY, VALUE) { \ + HMAP_NODE_INIT(hash_string(KEY, 0)), \ + CONST_CAST(char *, KEY), \ + CONST_CAST(char *, VALUE) } + + void smap_init(struct smap *); void smap_destroy(struct smap *); diff --git a/ovn/controller/encaps.c b/ovn/controller/encaps.c index 070b741..d050e12 100644 --- a/ovn/controller/encaps.c +++ b/ovn/controller/encaps.c @@ -149,7 +149,6 @@ tunnel_add(struct tunnel_ctx *tc, const char *new_chassis_id, } /* No such port, so add one. */ - struct smap external_ids = SMAP_INITIALIZER(&external_ids); struct smap options = SMAP_INITIALIZER(&options); struct ovsrec_port *port, **ports; struct ovsrec_interface *iface; @@ -174,9 +173,8 @@ tunnel_add(struct tunnel_ctx *tc, const char *new_chassis_id, port = ovsrec_port_insert(tc->ovs_txn); ovsrec_port_set_name(port, port_name); ovsrec_port_set_interfaces(port, &iface, 1); - smap_add(&external_ids, "ovn-chassis-id", new_chassis_id); - ovsrec_port_set_external_ids(port, &external_ids); - smap_destroy(&external_ids); + const struct smap ids = SMAP_INIT1(&ids, "ovn-chassis-id", new_chassis_id); + ovsrec_port_set_external_ids(port, &ids); ports = xmalloc(sizeof *tc->br_int->ports * (tc->br_int->n_ports + 1)); for (i = 0; i < tc->br_int->n_ports; i++) { diff --git a/ovn/controller/ovn-controller.c b/ovn/controller/ovn-controller.c index 1608cc4..85ebd9e 100644 --- a/ovn/controller/ovn-controller.c +++ b/ovn/controller/ovn-controller.c @@ -160,19 +160,15 @@ create_patch_port(struct controller_ctx *ctx, iface = ovsrec_interface_insert(ctx->ovs_idl_txn); ovsrec_interface_set_name(iface, port_name); ovsrec_interface_set_type(iface, "patch"); - struct smap options = SMAP_INITIALIZER(&options); - smap_add(&options, "peer", peer_port_name); + const struct smap options = SMAP_INIT1(&options, "peer", peer_port_name); ovsrec_interface_set_options(iface, &options); - smap_destroy(&options); struct ovsrec_port *port; port = ovsrec_port_insert(ctx->ovs_idl_txn); ovsrec_port_set_name(port, port_name); ovsrec_port_set_interfaces(port, &iface, 1); - struct smap ext_ids = SMAP_INITIALIZER(&ext_ids); - smap_add(&ext_ids, "ovn-patch-port", network); - ovsrec_port_set_external_ids(port, &ext_ids); - smap_destroy(&ext_ids); + const struct smap ids = SMAP_INIT1(&ids, "ovn-patch-port", network); + ovsrec_port_set_external_ids(port, &ids); struct ovsrec_port **ports; ports = xmalloc(sizeof *ports * (b1->n_ports + 1)); diff --git a/ovn/northd/ovn-northd.c b/ovn/northd/ovn-northd.c index befe122..0869e69 100644 --- a/ovn/northd/ovn-northd.c +++ b/ovn/northd/ovn-northd.c @@ -309,12 +309,10 @@ build_datapaths(struct northd_context *ctx, struct hmap *datapaths) od->sb = sbrec_datapath_binding_insert(ctx->ovnsb_txn); - struct smap external_ids = SMAP_INITIALIZER(&external_ids); char uuid_s[UUID_LEN + 1]; sprintf(uuid_s, UUID_FMT, UUID_ARGS(&od->nb->header_.uuid)); - smap_add(&external_ids, "logical-switch", uuid_s); - sbrec_datapath_binding_set_external_ids(od->sb, &external_ids); - smap_destroy(&external_ids); + const struct smap ids = SMAP_INIT1(&ids, "logical-switch", uuid_s); + sbrec_datapath_binding_set_external_ids(od->sb, &ids); sbrec_datapath_binding_set_tunnel_key(od->sb, tunnel_key); } @@ -888,13 +886,12 @@ build_lflows(struct northd_context *ctx, struct hmap *datapaths, sbrec_logical_flow_set_match(sbflow, lflow->match); sbrec_logical_flow_set_actions(sbflow, lflow->actions); - struct smap external_ids = SMAP_INITIALIZER(&external_ids); - smap_add(&external_ids, "stage-name", - lflow->pipeline == P_IN ? - ingress_stage_to_str(lflow->table_id) : - egress_stage_to_str(lflow->table_id)); - sbrec_logical_flow_set_external_ids(sbflow, &external_ids); - smap_destroy(&external_ids); + const struct smap ids = SMAP_INIT1( + &ids, "stage-name", + (lflow->pipeline == P_IN + ? ingress_stage_to_str(lflow->table_id) + : egress_stage_to_str(lflow->table_id))); + sbrec_logical_flow_set_external_ids(sbflow, &ids); ovn_lflow_destroy(&lflows, lflow); } diff --git a/utilities/ovs-vsctl.c b/utilities/ovs-vsctl.c index e177060..d651da7 100644 --- a/utilities/ovs-vsctl.c +++ b/utilities/ovs-vsctl.c @@ -1091,10 +1091,8 @@ cmd_emer_reset(struct ctl_context *ctx) /* We only want to save the "hwaddr" key from other_config. */ hwaddr = smap_get(&br->other_config, "hwaddr"); if (hwaddr) { - struct smap smap = SMAP_INITIALIZER(&smap); - smap_add(&smap, "hwaddr", hwaddr); + const struct smap smap = SMAP_INIT1(&smap, "hwaddr", hwaddr); ovsrec_bridge_set_other_config(br, &smap); - smap_destroy(&smap); } else { ovsrec_bridge_set_other_config(br, NULL); }