diff mbox series

[ovs-dev,v3,2/4] northd: Add datapaths to load balancers in bulk.

Message ID 20220909213223.824013-3-i.maximets@ovn.org
State Accepted
Headers show
Series northd: Optimize preparation of load balancers. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test success github build: passed
ovsrobot/github-robot-_ovn-kubernetes fail github build: failed

Commit Message

Ilya Maximets Sept. 9, 2022, 9:32 p.m. UTC
Filling arrays of switches and routers for load balancers
one-by-one is not very efficient.  Copying them in bulk
allows to save a noticeable amount of time in setups with
large load balancer groups.

Acked-by: Dumitru Ceara <dceara@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
---
 lib/lb.c        | 27 +++++++++++++++++++--------
 lib/lb.h        | 30 +++++++++++++++++++++++++-----
 northd/northd.c | 29 +++++++++++++++++++++--------
 3 files changed, 65 insertions(+), 21 deletions(-)
diff mbox series

Patch

diff --git a/lib/lb.c b/lib/lb.c
index fca5f27da..0b229f828 100644
--- a/lib/lb.c
+++ b/lib/lb.c
@@ -239,23 +239,27 @@  ovn_northd_lb_find(const struct hmap *lbs, const struct uuid *uuid)
 }
 
 void
-ovn_northd_lb_add_lr(struct ovn_northd_lb *lb, struct ovn_datapath *od)
+ovn_northd_lb_add_lr(struct ovn_northd_lb *lb, size_t n,
+                     struct ovn_datapath **ods)
 {
-    if (lb->n_allocated_nb_lr == lb->n_nb_lr) {
+    while (lb->n_allocated_nb_lr <= lb->n_nb_lr + n) {
         lb->nb_lr = x2nrealloc(lb->nb_lr, &lb->n_allocated_nb_lr,
                                sizeof *lb->nb_lr);
     }
-    lb->nb_lr[lb->n_nb_lr++] = od;
+    memcpy(&lb->nb_lr[lb->n_nb_lr], ods, n * sizeof *ods);
+    lb->n_nb_lr += n;
 }
 
 void
-ovn_northd_lb_add_ls(struct ovn_northd_lb *lb, struct ovn_datapath *od)
+ovn_northd_lb_add_ls(struct ovn_northd_lb *lb, size_t n,
+                     struct ovn_datapath **ods)
 {
-    if (lb->n_allocated_nb_ls == lb->n_nb_ls) {
+    while (lb->n_allocated_nb_ls <= lb->n_nb_ls + n) {
         lb->nb_ls = x2nrealloc(lb->nb_ls, &lb->n_allocated_nb_ls,
                                sizeof *lb->nb_ls);
     }
-    lb->nb_ls[lb->n_nb_ls++] = od;
+    memcpy(&lb->nb_ls[lb->n_nb_ls], ods, n * sizeof *ods);
+    lb->n_nb_ls += n;
 }
 
 void
@@ -276,10 +280,13 @@  ovn_northd_lb_destroy(struct ovn_northd_lb *lb)
 }
 
 /* Constructs a new 'struct ovn_lb_group' object from the Nb LB Group record
- * and a hash map of all existing 'struct ovn_northd_lb' objects. */
+ * and a hash map of all existing 'struct ovn_northd_lb' objects.  Space will
+ * be allocated for 'max_datapaths' logical switches and the same amount of
+ * logical routers to which this LB Group is applied.  Can be filled later
+ * with ovn_lb_group_add_ls() and ovn_lb_group_add_lr() respectively. */
 struct ovn_lb_group *
 ovn_lb_group_create(const struct nbrec_load_balancer_group *nbrec_lb_group,
-                    const struct hmap *lbs)
+                    const struct hmap *lbs, size_t max_datapaths)
 {
     struct ovn_lb_group *lb_group;
 
@@ -287,6 +294,8 @@  ovn_lb_group_create(const struct nbrec_load_balancer_group *nbrec_lb_group,
     lb_group->uuid = nbrec_lb_group->header_.uuid;
     lb_group->n_lbs = nbrec_lb_group->n_load_balancer;
     lb_group->lbs = xmalloc(lb_group->n_lbs * sizeof *lb_group->lbs);
+    lb_group->ls = xmalloc(max_datapaths * sizeof *lb_group->ls);
+    lb_group->lr = xmalloc(max_datapaths * sizeof *lb_group->lr);
 
     for (size_t i = 0; i < nbrec_lb_group->n_load_balancer; i++) {
         const struct uuid *lb_uuid =
@@ -305,6 +314,8 @@  ovn_lb_group_destroy(struct ovn_lb_group *lb_group)
     }
 
     free(lb_group->lbs);
+    free(lb_group->ls);
+    free(lb_group->lr);
     free(lb_group);
 }
 
diff --git a/lib/lb.h b/lib/lb.h
index 1c70bfb6e..fbd4f19e8 100644
--- a/lib/lb.h
+++ b/lib/lb.h
@@ -28,6 +28,7 @@  struct nbrec_load_balancer;
 struct nbrec_load_balancer_group;
 struct sbrec_load_balancer;
 struct sbrec_datapath_binding;
+struct ovn_datapath;
 struct ovn_port;
 struct uuid;
 
@@ -91,25 +92,44 @@  struct ovn_northd_lb *ovn_northd_lb_create(const struct nbrec_load_balancer *);
 struct ovn_northd_lb *ovn_northd_lb_find(const struct hmap *,
                                          const struct uuid *);
 void ovn_northd_lb_destroy(struct ovn_northd_lb *);
-void
-ovn_northd_lb_add_lr(struct ovn_northd_lb *lb, struct ovn_datapath *od);
-void
-ovn_northd_lb_add_ls(struct ovn_northd_lb *lb, struct ovn_datapath *od);
+void ovn_northd_lb_add_lr(struct ovn_northd_lb *lb, size_t n,
+                          struct ovn_datapath **ods);
+void ovn_northd_lb_add_ls(struct ovn_northd_lb *lb, size_t n,
+                          struct ovn_datapath **ods);
 
 struct ovn_lb_group {
     struct hmap_node hmap_node;
     struct uuid uuid;
     size_t n_lbs;
     struct ovn_northd_lb **lbs;
+
+    /* Datapaths to which this LB group is applied. */
+    size_t n_ls;
+    struct ovn_datapath **ls;
+    size_t n_lr;
+    struct ovn_datapath **lr;
 };
 
 struct ovn_lb_group *ovn_lb_group_create(
     const struct nbrec_load_balancer_group *,
-    const struct hmap *lbs);
+    const struct hmap *lbs,
+    size_t max_datapaths);
 void ovn_lb_group_destroy(struct ovn_lb_group *lb_group);
 struct ovn_lb_group *ovn_lb_group_find(const struct hmap *lb_groups,
                                        const struct uuid *);
 
+static inline void
+ovn_lb_group_add_ls(struct ovn_lb_group *lb_group, struct ovn_datapath *ls)
+{
+    lb_group->ls[lb_group->n_ls++] = ls;
+}
+
+static inline void
+ovn_lb_group_add_lr(struct ovn_lb_group *lb_group, struct ovn_datapath *lr)
+{
+    lb_group->lr[lb_group->n_lr++] = lr;
+}
+
 struct ovn_controller_lb {
     const struct sbrec_load_balancer *slb; /* May be NULL. */
 
diff --git a/northd/northd.c b/northd/northd.c
index e00625e9c..b08ad932b 100644
--- a/northd/northd.c
+++ b/northd/northd.c
@@ -3862,7 +3862,8 @@  build_lbs(struct northd_input *input_data, struct hmap *datapaths,
 
     NBREC_LOAD_BALANCER_GROUP_TABLE_FOR_EACH (nbrec_lb_group,
                                input_data->nbrec_load_balancer_group_table) {
-        lb_group = ovn_lb_group_create(nbrec_lb_group, lbs);
+        lb_group = ovn_lb_group_create(nbrec_lb_group, lbs,
+                                       hmap_count(datapaths));
         hmap_insert(lb_groups, &lb_group->hmap_node,
                     uuid_hash(&lb_group->uuid));
     }
@@ -3877,16 +3878,21 @@  build_lbs(struct northd_input *input_data, struct hmap *datapaths,
             const struct uuid *lb_uuid =
                 &od->nbs->load_balancer[i]->header_.uuid;
             lb = ovn_northd_lb_find(lbs, lb_uuid);
-            ovn_northd_lb_add_ls(lb, od);
+            ovn_northd_lb_add_ls(lb, 1, &od);
         }
 
         for (size_t i = 0; i < od->nbs->n_load_balancer_group; i++) {
             nbrec_lb_group = od->nbs->load_balancer_group[i];
             lb_group = ovn_lb_group_find(lb_groups,
                                          &nbrec_lb_group->header_.uuid);
-            for (size_t j = 0; j < lb_group->n_lbs; j++) {
-                ovn_northd_lb_add_ls(lb_group->lbs[j], od);
-            }
+            ovn_lb_group_add_ls(lb_group, od);
+        }
+    }
+
+    HMAP_FOR_EACH (lb_group, hmap_node, lb_groups) {
+        for (size_t j = 0; j < lb_group->n_lbs; j++) {
+            ovn_northd_lb_add_ls(lb_group->lbs[j], lb_group->n_ls,
+                                 lb_group->ls);
         }
     }
 
@@ -3899,7 +3905,7 @@  build_lbs(struct northd_input *input_data, struct hmap *datapaths,
             const struct uuid *lb_uuid =
                 &od->nbr->load_balancer[i]->header_.uuid;
             lb = ovn_northd_lb_find(lbs, lb_uuid);
-            ovn_northd_lb_add_lr(lb, od);
+            ovn_northd_lb_add_lr(lb, 1, &od);
             build_lrouter_lb_ips(od, lb);
         }
 
@@ -3907,12 +3913,19 @@  build_lbs(struct northd_input *input_data, struct hmap *datapaths,
             nbrec_lb_group = od->nbr->load_balancer_group[i];
             lb_group = ovn_lb_group_find(lb_groups,
                                          &nbrec_lb_group->header_.uuid);
+            ovn_lb_group_add_lr(lb_group, od);
             for (size_t j = 0; j < lb_group->n_lbs; j++) {
-                ovn_northd_lb_add_lr(lb_group->lbs[j], od);
                 build_lrouter_lb_ips(od, lb_group->lbs[j]);
             }
         }
     }
+
+    HMAP_FOR_EACH (lb_group, hmap_node, lb_groups) {
+        for (size_t j = 0; j < lb_group->n_lbs; j++) {
+            ovn_northd_lb_add_lr(lb_group->lbs[j], lb_group->n_lr,
+                                 lb_group->lr);
+        }
+    }
 }
 
 static void
@@ -4097,7 +4110,7 @@  build_lswitch_lbs_from_lrouter(struct hmap *datapaths, struct hmap *lbs)
                    }
                 }
                 if (!installed) {
-                    ovn_northd_lb_add_ls(lb, od);
+                    ovn_northd_lb_add_ls(lb, 1, &od);
                 }
                 if (lb->nlb) {
                     od->has_lb_vip |= lb_has_vip(lb->nlb);