From patchwork Fri Dec 16 22:25:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Pfaff X-Patchwork-Id: 706611 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 3tgQ3K6jhTz9t2N for ; Sat, 17 Dec 2016 09:29:33 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id ECBECBE4; Fri, 16 Dec 2016 22:26:00 +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 E0722BC6 for ; Fri, 16 Dec 2016 22:25:57 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 117FB23D for ; Fri, 16 Dec 2016 22:25:56 +0000 (UTC) Received: from mfilter34-d.gandi.net (mfilter34-d.gandi.net [217.70.178.165]) by relay3-d.mail.gandi.net (Postfix) with ESMTP id D8A8BA80CE; Fri, 16 Dec 2016 23:25:55 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter34-d.gandi.net Received: from relay3-d.mail.gandi.net ([IPv6:::ffff:217.70.183.195]) by mfilter34-d.gandi.net (mfilter34-d.gandi.net [::ffff:10.0.15.180]) (amavisd-new, port 10024) with ESMTP id uFoTGzUFBu-R; Fri, 16 Dec 2016 23:25:54 +0100 (CET) X-Originating-IP: 208.91.2.3 Received: from sigabrt.benpfaff.org (unknown [208.91.2.3]) (Authenticated sender: blp@ovn.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 60988A80D0; Fri, 16 Dec 2016 23:25:53 +0100 (CET) From: Ben Pfaff To: dev@openvswitch.org Date: Fri, 16 Dec 2016 14:25:33 -0800 Message-Id: <20161216222537.2221-8-blp@ovn.org> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20161216222537.2221-1-blp@ovn.org> References: <20161216222537.2221-1-blp@ovn.org> X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Ben Pfaff Subject: [ovs-dev] [PATCH v2 07/11] lport: Add index for logical datapaths. 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 will have its first real user in an upcoming commit. Signed-off-by: Ben Pfaff --- ovn/controller/lport.c | 62 +++++++++++++++++++++++++++++++++++++++++ ovn/controller/lport.h | 33 ++++++++++++++++++++-- ovn/controller/ovn-controller.c | 4 +++ 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/ovn/controller/lport.c b/ovn/controller/lport.c index 38aca22..906fda2 100644 --- a/ovn/controller/lport.c +++ b/ovn/controller/lport.c @@ -22,6 +22,68 @@ VLOG_DEFINE_THIS_MODULE(lport); +static struct ldatapath *ldatapath_lookup_by_key__( + const struct ldatapath_index *, uint32_t dp_key); + +void +ldatapath_index_init(struct ldatapath_index *ldatapaths, + struct ovsdb_idl *ovnsb_idl) +{ + hmap_init(&ldatapaths->by_key); + + const struct sbrec_port_binding *pb; + SBREC_PORT_BINDING_FOR_EACH (pb, ovnsb_idl) { + if (!pb->datapath) { + continue; + } + uint32_t dp_key = pb->datapath->tunnel_key; + struct ldatapath *ld = ldatapath_lookup_by_key__(ldatapaths, dp_key); + if (!ld) { + ld = xzalloc(sizeof *ld); + hmap_insert(&ldatapaths->by_key, &ld->by_key_node, dp_key); + ld->db = pb->datapath; + } + + if (ld->n_lports >= ld->allocated_lports) { + ld->lports = x2nrealloc(ld->lports, &ld->allocated_lports, + sizeof *ld->lports); + } + ld->lports[ld->n_lports++] = pb; + } +} + +void +ldatapath_index_destroy(struct ldatapath_index *ldatapaths) +{ + if (!ldatapaths) { + return; + } + + struct ldatapath *ld, *ld_next; + HMAP_FOR_EACH_SAFE (ld, ld_next, by_key_node, &ldatapaths->by_key) { + hmap_remove(&ldatapaths->by_key, &ld->by_key_node); + free(ld->lports); + free(ld); + } + hmap_destroy(&ldatapaths->by_key); +} + +static struct ldatapath *ldatapath_lookup_by_key__( + const struct ldatapath_index *ldatapaths, uint32_t dp_key) +{ + struct ldatapath *ld; + HMAP_FOR_EACH_WITH_HASH (ld, by_key_node, dp_key, &ldatapaths->by_key) { + return ld; + } + return NULL; +} + +const struct ldatapath *ldatapath_lookup_by_key( + const struct ldatapath_index *ldatapaths, uint32_t dp_key) +{ + return ldatapath_lookup_by_key__(ldatapaths, dp_key); +} + /* A logical port. */ struct lport { struct hmap_node name_node; /* Index by name. */ diff --git a/ovn/controller/lport.h b/ovn/controller/lport.h index 0cad74a..fe0e430 100644 --- a/ovn/controller/lport.h +++ b/ovn/controller/lport.h @@ -22,8 +22,37 @@ struct ovsdb_idl; struct sbrec_datapath_binding; -/* Logical port and multicast group indexes - * ======================================== +/* Database indexes. + * ================= + * + * If the database IDL were a little smarter, it would allow us to directly + * look up data based on values of its fields. It's not that smart (yet), so + * instead we define our own indexes. + */ + +/* Logical datapath index + * ====================== + */ + +struct ldatapath { + struct hmap_node by_key_node; /* Index by tunnel key. */ + const struct sbrec_datapath_binding *db; + const struct sbrec_port_binding **lports; + size_t n_lports, allocated_lports; +}; + +struct ldatapath_index { + struct hmap by_key; +}; + +void ldatapath_index_init(struct ldatapath_index *, struct ovsdb_idl *); +void ldatapath_index_destroy(struct ldatapath_index *); + +const struct ldatapath *ldatapath_lookup_by_key( + const struct ldatapath_index *, uint32_t dp_key); + +/* Logical port index + * ================== * * This data structure holds multiple indexes over logical ports, to allow for * efficient searching for logical ports by name or number. diff --git a/ovn/controller/ovn-controller.c b/ovn/controller/ovn-controller.c index b924589..5c66ba3 100644 --- a/ovn/controller/ovn-controller.c +++ b/ovn/controller/ovn-controller.c @@ -509,8 +509,10 @@ main(int argc, char *argv[]) const struct ovsrec_bridge *br_int = get_br_int(&ctx); const char *chassis_id = get_chassis_id(ctx.ovs_idl); + struct ldatapath_index ldatapaths; struct lport_index lports; struct mcgroup_index mcgroups; + ldatapath_index_init(&ldatapaths, ctx.ovnsb_idl); lport_index_init(&lports, ctx.ovnsb_idl); mcgroup_index_init(&mcgroups, ctx.ovnsb_idl); @@ -558,6 +560,8 @@ main(int argc, char *argv[]) mcgroup_index_destroy(&mcgroups); lport_index_destroy(&lports); + ldatapath_index_destroy(&ldatapaths); + sset_destroy(&all_lports); struct local_datapath *cur_node, *next_node;