bbaaef
From f781f1bdb099c0bfc45e6bf732940f076f28c026 Mon Sep 17 00:00:00 2001
bbaaef
From: Dumitru Ceara <dceara@redhat.com>
bbaaef
Date: Thu, 12 Sep 2019 16:14:20 +0200
bbaaef
Subject: [PATCH 2/4] ovn-controller: Minimize SB DB port_binding lookups.
bbaaef
bbaaef
Instead of storing only peer_ports in struct local_datapath, store both
bbaaef
local-remote mappings for patch ports. Also, it's useful to directly
bbaaef
store sbrec_port_binding pointers for all datapath ports as we avoid
bbaaef
doing costly sbrec_port_binding index lookups by port name.
bbaaef
bbaaef
Change-Id: Id8df886cab6bb4a81cd105d2ac52dd7e9aa03326
bbaaef
Acked-by: Mark Michelson <mmichels@redhat.com>
bbaaef
Acked-by: Han Zhou <hzhou8@ebay.com>
bbaaef
Signed-off-by: Dumitru Ceara <dceara@redhat.com>
bbaaef
Signed-off-by: Numan Siddique <nusiddiq@redhat.com>
bbaaef
bbaaef
(cherry-picked from upstream commit 89f5048f960c67e9dac3d45c6a9b25fa260e1a46)
bbaaef
---
bbaaef
 ovn/controller/binding.c        | 19 +++++++++---
bbaaef
 ovn/controller/ovn-controller.h | 11 ++++++-
bbaaef
 ovn/controller/physical.c       | 17 ++++++-----
bbaaef
 ovn/controller/pinctrl.c        | 53 ++++++++-------------------------
bbaaef
 4 files changed, 48 insertions(+), 52 deletions(-)
bbaaef
bbaaef
diff --git a/ovn/controller/binding.c b/ovn/controller/binding.c
bbaaef
index c71731a1e..1380a3e6e 100644
bbaaef
--- a/ovn/controller/binding.c
bbaaef
+++ b/ovn/controller/binding.c
bbaaef
@@ -161,13 +161,24 @@ add_local_datapath__(struct ovsdb_idl_index *sbrec_datapath_binding_by_key,
bbaaef
                                          peer->datapath, false,
bbaaef
                                          depth + 1, local_datapaths);
bbaaef
                     ld->n_peer_ports++;
bbaaef
-                    ld->peer_ports = xrealloc(ld->peer_ports,
bbaaef
-                                              ld->n_peer_ports *
bbaaef
-                                              sizeof *ld->peer_ports);
bbaaef
-                    ld->peer_ports[ld->n_peer_ports - 1] = peer;
bbaaef
+                    if (ld->n_peer_ports > ld->n_allocated_peer_ports) {
bbaaef
+                        ld->peer_ports =
bbaaef
+                            x2nrealloc(ld->peer_ports,
bbaaef
+                                       &ld->n_allocated_peer_ports,
bbaaef
+                                       sizeof *ld->peer_ports);
bbaaef
+                    }
bbaaef
+                    ld->peer_ports[ld->n_peer_ports - 1].local = pb;
bbaaef
+                    ld->peer_ports[ld->n_peer_ports - 1].remote = peer;
bbaaef
                 }
bbaaef
             }
bbaaef
         }
bbaaef
+
bbaaef
+        ld->n_ports++;
bbaaef
+        if (ld->n_ports > ld->n_allocated_ports) {
bbaaef
+            ld->ports = x2nrealloc(ld->ports, &ld->n_allocated_ports,
bbaaef
+                                   sizeof *ld->ports);
bbaaef
+        }
bbaaef
+        ld->ports[ld->n_ports - 1] = pb;
bbaaef
     }
bbaaef
     sbrec_port_binding_index_destroy_row(target);
bbaaef
 }
bbaaef
diff --git a/ovn/controller/ovn-controller.h b/ovn/controller/ovn-controller.h
bbaaef
index 078c9eabe..09ef4b632 100644
bbaaef
--- a/ovn/controller/ovn-controller.h
bbaaef
+++ b/ovn/controller/ovn-controller.h
bbaaef
@@ -60,8 +60,17 @@ struct local_datapath {
bbaaef
      * hypervisor. */
bbaaef
     bool has_local_l3gateway;
bbaaef
 
bbaaef
-    const struct sbrec_port_binding **peer_ports;
bbaaef
+    const struct sbrec_port_binding **ports;
bbaaef
+    size_t n_ports;
bbaaef
+    size_t n_allocated_ports;
bbaaef
+
bbaaef
+    struct {
bbaaef
+        const struct sbrec_port_binding *local;
bbaaef
+        const struct sbrec_port_binding *remote;
bbaaef
+    } *peer_ports;
bbaaef
+
bbaaef
     size_t n_peer_ports;
bbaaef
+    size_t n_allocated_peer_ports;
bbaaef
 };
bbaaef
 
bbaaef
 struct local_datapath *get_local_datapath(const struct hmap *,
bbaaef
diff --git a/ovn/controller/physical.c b/ovn/controller/physical.c
bbaaef
index 95cbaba49..b439a4852 100644
bbaaef
--- a/ovn/controller/physical.c
bbaaef
+++ b/ovn/controller/physical.c
bbaaef
@@ -259,11 +259,12 @@ put_remote_port_redirect_bridged(const struct
bbaaef
 
bbaaef
         uint32_t ls_dp_key = 0;
bbaaef
         for (int i = 0; i < ld->n_peer_ports; i++) {
bbaaef
-            const struct sbrec_port_binding *sport_binding = ld->peer_ports[i];
bbaaef
-            const char *sport_peer_name = smap_get(&sport_binding->options,
bbaaef
-                                                   "peer");
bbaaef
-            const char *distributed_port = smap_get(&binding->options,
bbaaef
-                                                    "distributed-port");
bbaaef
+            const struct sbrec_port_binding *sport_binding =
bbaaef
+                ld->peer_ports[i].remote;
bbaaef
+            const char *sport_peer_name =
bbaaef
+                smap_get(&sport_binding->options, "peer");
bbaaef
+            const char *distributed_port =
bbaaef
+                smap_get(&binding->options, "distributed-port");
bbaaef
 
bbaaef
             if (!strcmp(sport_peer_name, distributed_port)) {
bbaaef
                 ls_dp_key = sport_binding->datapath->tunnel_key;
bbaaef
@@ -525,7 +526,8 @@ put_replace_chassis_mac_flows(const struct simap *ct_zones,
bbaaef
     struct zone_ids zone_ids = get_zone_ids(localnet_port, ct_zones);
bbaaef
 
bbaaef
     for (int i = 0; i < ld->n_peer_ports; i++) {
bbaaef
-        const struct sbrec_port_binding *rport_binding = ld->peer_ports[i];
bbaaef
+        const struct sbrec_port_binding *rport_binding =
bbaaef
+            ld->peer_ports[i].remote;
bbaaef
         struct eth_addr router_port_mac;
bbaaef
         char *err_str = NULL;
bbaaef
         struct match match;
bbaaef
@@ -632,7 +634,8 @@ put_replace_router_port_mac_flows(struct ovsdb_idl_index
bbaaef
     }
bbaaef
 
bbaaef
     for (int i = 0; i < ld->n_peer_ports; i++) {
bbaaef
-        const struct sbrec_port_binding *rport_binding = ld->peer_ports[i];
bbaaef
+        const struct sbrec_port_binding *rport_binding =
bbaaef
+            ld->peer_ports[i].remote;
bbaaef
         struct eth_addr router_port_mac;
bbaaef
         struct match match;
bbaaef
         struct ofpact_mac *replace_mac;
bbaaef
diff --git a/ovn/controller/pinctrl.c b/ovn/controller/pinctrl.c
bbaaef
index 8bc99df4e..5636fc1e0 100644
bbaaef
--- a/ovn/controller/pinctrl.c
bbaaef
+++ b/ovn/controller/pinctrl.c
bbaaef
@@ -175,8 +175,7 @@ static struct pinctrl pinctrl;
bbaaef
 static void init_buffered_packets_map(void);
bbaaef
 static void destroy_buffered_packets_map(void);
bbaaef
 static void
bbaaef
-run_buffered_binding(struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
bbaaef
-                     struct ovsdb_idl_index *sbrec_mac_binding_by_lport_ip,
bbaaef
+run_buffered_binding(struct ovsdb_idl_index *sbrec_mac_binding_by_lport_ip,
bbaaef
                      const struct hmap *local_datapaths)
bbaaef
     OVS_REQUIRES(pinctrl_mutex);
bbaaef
 
bbaaef
@@ -240,10 +239,7 @@ static void wait_controller_event(struct ovsdb_idl_txn *ovnsb_idl_txn);
bbaaef
 static void init_ipv6_ras(void);
bbaaef
 static void destroy_ipv6_ras(void);
bbaaef
 static void ipv6_ra_wait(long long int send_ipv6_ra_time);
bbaaef
-static void prepare_ipv6_ras(
bbaaef
-    struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
bbaaef
-    struct ovsdb_idl_index *sbrec_port_binding_by_name,
bbaaef
-    const struct hmap *local_datapaths)
bbaaef
+static void prepare_ipv6_ras(const struct hmap *local_datapaths)
bbaaef
     OVS_REQUIRES(pinctrl_mutex);
bbaaef
 static void send_ipv6_ras(struct rconn *swconn,
bbaaef
                           long long int *send_ipv6_ra_time)
bbaaef
@@ -2216,8 +2212,7 @@ pinctrl_run(struct ovsdb_idl_txn *ovnsb_idl_txn,
bbaaef
     send_garp_prepare(sbrec_port_binding_by_datapath,
bbaaef
                       sbrec_port_binding_by_name, br_int, chassis,
bbaaef
                       local_datapaths, active_tunnels);
bbaaef
-    prepare_ipv6_ras(sbrec_port_binding_by_datapath,
bbaaef
-                     sbrec_port_binding_by_name, local_datapaths);
bbaaef
+    prepare_ipv6_ras(local_datapaths);
bbaaef
     sync_dns_cache(dns_table);
bbaaef
     controller_event_run(ovnsb_idl_txn, ce_table, chassis);
bbaaef
     ip_mcast_sync(ovnsb_idl_txn, chassis, local_datapaths,
bbaaef
@@ -2225,8 +2220,7 @@ pinctrl_run(struct ovsdb_idl_txn *ovnsb_idl_txn,
bbaaef
                   sbrec_port_binding_by_key,
bbaaef
                   sbrec_igmp_groups,
bbaaef
                   sbrec_ip_multicast_opts);
bbaaef
-    run_buffered_binding(sbrec_port_binding_by_datapath,
bbaaef
-                         sbrec_mac_binding_by_lport_ip,
bbaaef
+    run_buffered_binding(sbrec_mac_binding_by_lport_ip,
bbaaef
                          local_datapaths);
bbaaef
     sync_svc_monitors(ovnsb_idl_txn, svc_mon_table, sbrec_port_binding_by_name,
bbaaef
                       chassis);
bbaaef
@@ -2669,9 +2663,7 @@ send_ipv6_ras(struct rconn *swconn, long long int *send_ipv6_ra_time)
bbaaef
 /* Called by pinctrl_run(). Runs with in the main ovn-controller
bbaaef
  * thread context. */
bbaaef
 static void
bbaaef
-prepare_ipv6_ras(struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
bbaaef
-                 struct ovsdb_idl_index *sbrec_port_binding_by_name,
bbaaef
-                 const struct hmap *local_datapaths)
bbaaef
+prepare_ipv6_ras(const struct hmap *local_datapaths)
bbaaef
     OVS_REQUIRES(pinctrl_mutex)
bbaaef
 {
bbaaef
     struct shash_node *iter, *iter_next;
bbaaef
@@ -2684,25 +2676,12 @@ prepare_ipv6_ras(struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
bbaaef
     bool changed = false;
bbaaef
     const struct local_datapath *ld;
bbaaef
     HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
bbaaef
-        struct sbrec_port_binding *target = sbrec_port_binding_index_init_row(
bbaaef
-            sbrec_port_binding_by_datapath);
bbaaef
-        sbrec_port_binding_index_set_datapath(target, ld->datapath);
bbaaef
 
bbaaef
-        struct sbrec_port_binding *pb;
bbaaef
-        SBREC_PORT_BINDING_FOR_EACH_EQUAL (pb, target,
bbaaef
-                                           sbrec_port_binding_by_datapath) {
bbaaef
-            if (!smap_get_bool(&pb->options, "ipv6_ra_send_periodic", false)) {
bbaaef
-                continue;
bbaaef
-            }
bbaaef
+        for (size_t i = 0; i < ld->n_peer_ports; i++) {
bbaaef
+            const struct sbrec_port_binding *peer = ld->peer_ports[i].remote;
bbaaef
+            const struct sbrec_port_binding *pb = ld->peer_ports[i].local;
bbaaef
 
bbaaef
-            const char *peer_s = smap_get(&pb->options, "peer");
bbaaef
-            if (!peer_s) {
bbaaef
-                continue;
bbaaef
-            }
bbaaef
-
bbaaef
-            const struct sbrec_port_binding *peer
bbaaef
-                = lport_lookup_by_name(sbrec_port_binding_by_name, peer_s);
bbaaef
-            if (!peer) {
bbaaef
+            if (!smap_get_bool(&pb->options, "ipv6_ra_send_periodic", false)) {
bbaaef
                 continue;
bbaaef
             }
bbaaef
 
bbaaef
@@ -2741,7 +2720,6 @@ prepare_ipv6_ras(struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
bbaaef
 
bbaaef
             /* pinctrl_handler thread will send the IPv6 RAs. */
bbaaef
         }
bbaaef
-        sbrec_port_binding_index_destroy_row(target);
bbaaef
     }
bbaaef
 
bbaaef
     /* Remove those that are no longer in the SB database */
bbaaef
@@ -2987,8 +2965,7 @@ run_put_mac_bindings(struct ovsdb_idl_txn *ovnsb_idl_txn,
bbaaef
 }
bbaaef
 
bbaaef
 static void
bbaaef
-run_buffered_binding(struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
bbaaef
-                     struct ovsdb_idl_index *sbrec_mac_binding_by_lport_ip,
bbaaef
+run_buffered_binding(struct ovsdb_idl_index *sbrec_mac_binding_by_lport_ip,
bbaaef
                      const struct hmap *local_datapaths)
bbaaef
     OVS_REQUIRES(pinctrl_mutex)
bbaaef
 {
bbaaef
@@ -2996,13 +2973,10 @@ run_buffered_binding(struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
bbaaef
     bool notify = false;
bbaaef
 
bbaaef
     HMAP_FOR_EACH (ld, hmap_node, local_datapaths) {
bbaaef
-        struct sbrec_port_binding *target = sbrec_port_binding_index_init_row(
bbaaef
-                sbrec_port_binding_by_datapath);
bbaaef
-        sbrec_port_binding_index_set_datapath(target, ld->datapath);
bbaaef
 
bbaaef
-        const struct sbrec_port_binding *pb;
bbaaef
-        SBREC_PORT_BINDING_FOR_EACH_EQUAL (pb, target,
bbaaef
-                                           sbrec_port_binding_by_datapath) {
bbaaef
+        for (size_t i = 0; i < ld->n_ports; i++) {
bbaaef
+
bbaaef
+            const struct sbrec_port_binding *pb = ld->ports[i];
bbaaef
             struct buffered_packets *cur_qp, *next_qp;
bbaaef
             HMAP_FOR_EACH_SAFE (cur_qp, next_qp, hmap_node,
bbaaef
                                 &buffered_packets_map) {
bbaaef
@@ -3020,7 +2994,6 @@ run_buffered_binding(struct ovsdb_idl_index *sbrec_port_binding_by_datapath,
bbaaef
                 ds_destroy(&ip_s);
bbaaef
             }
bbaaef
         }
bbaaef
-        sbrec_port_binding_index_destroy_row(target);
bbaaef
     }
bbaaef
     buffered_packets_map_gc();
bbaaef
 
bbaaef
-- 
bbaaef
2.26.2
bbaaef