ebb439
From 8eee079880255ecb70051be177c78bdfa115b3bf Mon Sep 17 00:00:00 2001
ebb439
From: Numan Siddique <numans@ovn.org>
ebb439
Date: Wed, 7 Oct 2020 17:49:17 +0530
ebb439
Subject: [PATCH 04/10] Add new table Load_Balancer in Southbound database.
ebb439
ebb439
This patch adds a new table 'Load_Balancer' in SB DB and syncs the Load_Balancer table rows
ebb439
from NB DB to SB DB. An upcoming patch will make use of this table for handling the
ebb439
load balancer hairpin traffic.
ebb439
ebb439
Co-authored-by: Dumitru Ceara <dceara@redhat.com>
ebb439
Signed-off-by: Dumitru Ceara <dceara@redhat.com>
ebb439
Signed-off-by: Numan Siddique <numans@ovn.org>
ebb439
Acked-by: Mark Michelson <mmichels@redhat.com>
ebb439
---
ebb439
 northd/ovn-northd.c   | 114 ++++++++++++++++++++++++++++++++++++++++--
ebb439
 ovn-sb.ovsschema      |  27 +++++++++-
ebb439
 ovn-sb.xml            |  45 +++++++++++++++++
ebb439
 tests/ovn-northd.at   |  86 +++++++++++++++++++++++++++++++
ebb439
 utilities/ovn-sbctl.c |   3 ++
ebb439
 5 files changed, 269 insertions(+), 6 deletions(-)
ebb439
ebb439
diff --git a/northd/ovn-northd.c b/northd/ovn-northd.c
ebb439
index 38074e7f4..c32e25c3d 100644
ebb439
--- a/northd/ovn-northd.c
ebb439
+++ b/northd/ovn-northd.c
ebb439
@@ -3333,9 +3333,14 @@ struct ovn_lb {
ebb439
     struct hmap_node hmap_node;
ebb439
 
ebb439
     const struct nbrec_load_balancer *nlb; /* May be NULL. */
ebb439
+    const struct sbrec_load_balancer *slb; /* May be NULL. */
ebb439
     char *selection_fields;
ebb439
     struct lb_vip *vips;
ebb439
     size_t n_vips;
ebb439
+
ebb439
+    size_t n_dps;
ebb439
+    size_t n_allocated_dps;
ebb439
+    const struct sbrec_datapath_binding **dps;
ebb439
 };
ebb439
 
ebb439
 struct lb_vip {
ebb439
@@ -3362,7 +3367,7 @@ struct lb_vip_backend {
ebb439
 
ebb439
 
ebb439
 static inline struct ovn_lb *
ebb439
-ovn_lb_find(struct hmap *lbs, struct uuid *uuid)
ebb439
+ovn_lb_find(struct hmap *lbs, const struct uuid *uuid)
ebb439
 {
ebb439
     struct ovn_lb *lb;
ebb439
     size_t hash = uuid_hash(uuid);
ebb439
@@ -3375,6 +3380,14 @@ ovn_lb_find(struct hmap *lbs, struct uuid *uuid)
ebb439
     return NULL;
ebb439
 }
ebb439
 
ebb439
+static void
ebb439
+ovn_lb_add_datapath(struct ovn_lb *lb, struct ovn_datapath *od)
ebb439
+{
ebb439
+    if (lb->n_allocated_dps == lb->n_dps) {
ebb439
+        lb->dps = x2nrealloc(lb->dps, &lb->n_allocated_dps, sizeof *lb->dps);
ebb439
+    }
ebb439
+    lb->dps[lb->n_dps++] = od->sb;
ebb439
+}
ebb439
 
ebb439
 struct service_monitor_info {
ebb439
     struct hmap_node hmap_node;
ebb439
@@ -3603,6 +3616,7 @@ ovn_lb_destroy(struct ovn_lb *lb)
ebb439
     }
ebb439
     free(lb->vips);
ebb439
     free(lb->selection_fields);
ebb439
+    free(lb->dps);
ebb439
 }
ebb439
 
ebb439
 static void build_lb_vip_ct_lb_actions(struct lb_vip *lb_vip,
ebb439
@@ -3648,8 +3662,8 @@ static void build_lb_vip_ct_lb_actions(struct lb_vip *lb_vip,
ebb439
 }
ebb439
 
ebb439
 static void
ebb439
-build_ovn_lbs(struct northd_context *ctx, struct hmap *ports,
ebb439
-              struct hmap *lbs)
ebb439
+build_ovn_lbs(struct northd_context *ctx, struct hmap *datapaths,
ebb439
+              struct hmap *ports, struct hmap *lbs)
ebb439
 {
ebb439
     hmap_init(lbs);
ebb439
     struct hmap monitor_map = HMAP_INITIALIZER(&monitor_map);
ebb439
@@ -3670,6 +3684,88 @@ build_ovn_lbs(struct northd_context *ctx, struct hmap *ports,
ebb439
         ovn_lb_create(ctx, lbs, nbrec_lb, ports, &monitor_map);
ebb439
     }
ebb439
 
ebb439
+    struct ovn_datapath *od;
ebb439
+    HMAP_FOR_EACH (od, key_node, datapaths) {
ebb439
+        if (!od->nbs) {
ebb439
+            continue;
ebb439
+        }
ebb439
+
ebb439
+        for (size_t i = 0; i < od->nbs->n_load_balancer; i++) {
ebb439
+            const struct uuid *lb_uuid =
ebb439
+                &od->nbs->load_balancer[i]->header_.uuid;
ebb439
+            struct ovn_lb *lb = ovn_lb_find(lbs, lb_uuid);
ebb439
+
ebb439
+            ovn_lb_add_datapath(lb, od);
ebb439
+        }
ebb439
+    }
ebb439
+
ebb439
+    struct ovn_lb *lb;
ebb439
+
ebb439
+    /* Delete any stale SB load balancer rows. */
ebb439
+    const struct sbrec_load_balancer *sbrec_lb, *next;
ebb439
+    SBREC_LOAD_BALANCER_FOR_EACH_SAFE (sbrec_lb, next, ctx->ovnsb_idl) {
ebb439
+        const char *nb_lb_uuid = smap_get(&sbrec_lb->external_ids, "lb_id");
ebb439
+        struct uuid lb_uuid;
ebb439
+        if (!nb_lb_uuid || !uuid_from_string(&lb_uuid, nb_lb_uuid)) {
ebb439
+            sbrec_load_balancer_delete(sbrec_lb);
ebb439
+            continue;
ebb439
+        }
ebb439
+
ebb439
+        lb = ovn_lb_find(lbs, &lb_uuid);
ebb439
+        if (lb && lb->n_dps) {
ebb439
+            lb->slb = sbrec_lb;
ebb439
+        } else {
ebb439
+            sbrec_load_balancer_delete(sbrec_lb);
ebb439
+        }
ebb439
+    }
ebb439
+
ebb439
+    /* Create SB Load balancer records if not present and sync
ebb439
+     * the SB load balancer columns. */
ebb439
+    HMAP_FOR_EACH (lb, hmap_node, lbs) {
ebb439
+        if (!lb->n_dps) {
ebb439
+            continue;
ebb439
+        }
ebb439
+
ebb439
+        if (!lb->slb) {
ebb439
+            sbrec_lb = sbrec_load_balancer_insert(ctx->ovnsb_txn);
ebb439
+            lb->slb = sbrec_lb;
ebb439
+            char *lb_id = xasprintf(
ebb439
+                UUID_FMT, UUID_ARGS(&lb->nlb->header_.uuid));
ebb439
+            const struct smap external_ids =
ebb439
+                SMAP_CONST1(&external_ids, "lb_id", lb_id);
ebb439
+            sbrec_load_balancer_set_external_ids(sbrec_lb, &external_ids);
ebb439
+            free(lb_id);
ebb439
+        }
ebb439
+        sbrec_load_balancer_set_name(lb->slb, lb->nlb->name);
ebb439
+        sbrec_load_balancer_set_vips(lb->slb, &lb->nlb->vips);
ebb439
+        sbrec_load_balancer_set_protocol(lb->slb, lb->nlb->protocol);
ebb439
+        sbrec_load_balancer_set_datapaths(
ebb439
+            lb->slb, (struct sbrec_datapath_binding **)lb->dps,
ebb439
+            lb->n_dps);
ebb439
+    }
ebb439
+
ebb439
+    /* Set the list of associated load balanacers to a logical switch
ebb439
+     * datapath binding in the SB DB. */
ebb439
+    HMAP_FOR_EACH (od, key_node, datapaths) {
ebb439
+        if (!od->nbs) {
ebb439
+            continue;
ebb439
+        }
ebb439
+
ebb439
+        const struct sbrec_load_balancer **sbrec_lbs =
ebb439
+            xmalloc(od->nbs->n_load_balancer * sizeof *sbrec_lbs);
ebb439
+        for (size_t i = 0; i < od->nbs->n_load_balancer; i++) {
ebb439
+            const struct uuid *lb_uuid =
ebb439
+                &od->nbs->load_balancer[i]->header_.uuid;
ebb439
+            lb = ovn_lb_find(lbs, lb_uuid);
ebb439
+            sbrec_lbs[i] = lb->slb;
ebb439
+        }
ebb439
+
ebb439
+        sbrec_datapath_binding_set_load_balancers(
ebb439
+            od->sb, (struct sbrec_load_balancer **)sbrec_lbs,
ebb439
+            od->nbs->n_load_balancer);
ebb439
+        free(sbrec_lbs);
ebb439
+    }
ebb439
+
ebb439
     struct service_monitor_info *mon_info;
ebb439
     HMAP_FOR_EACH_POP (mon_info, hmap_node, &monitor_map) {
ebb439
         if (!mon_info->required) {
ebb439
@@ -12176,7 +12272,7 @@ ovnnb_db_run(struct northd_context *ctx,
ebb439
 
ebb439
     build_datapaths(ctx, datapaths, lr_list);
ebb439
     build_ports(ctx, sbrec_chassis_by_name, datapaths, ports);
ebb439
-    build_ovn_lbs(ctx, ports, &lbs;;
ebb439
+    build_ovn_lbs(ctx, datapaths, ports, &lbs;;
ebb439
     build_ipam(datapaths, ports);
ebb439
     build_port_group_lswitches(ctx, &port_groups, ports);
ebb439
     build_lrouter_groups(ports, lr_list);
ebb439
@@ -12951,6 +13047,8 @@ main(int argc, char *argv[])
ebb439
     ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_datapath_binding);
ebb439
     add_column_noalert(ovnsb_idl_loop.idl,
ebb439
                        &sbrec_datapath_binding_col_tunnel_key);
ebb439
+    add_column_noalert(ovnsb_idl_loop.idl,
ebb439
+                       &sbrec_datapath_binding_col_load_balancers);
ebb439
     add_column_noalert(ovnsb_idl_loop.idl,
ebb439
                        &sbrec_datapath_binding_col_external_ids);
ebb439
 
ebb439
@@ -13118,6 +13216,14 @@ main(int argc, char *argv[])
ebb439
     add_column_noalert(ovnsb_idl_loop.idl,
ebb439
                        &sbrec_service_monitor_col_external_ids);
ebb439
 
ebb439
+    ovsdb_idl_add_table(ovnsb_idl_loop.idl, &sbrec_table_load_balancer);
ebb439
+    add_column_noalert(ovnsb_idl_loop.idl, &sbrec_load_balancer_col_datapaths);
ebb439
+    add_column_noalert(ovnsb_idl_loop.idl, &sbrec_load_balancer_col_name);
ebb439
+    add_column_noalert(ovnsb_idl_loop.idl, &sbrec_load_balancer_col_vips);
ebb439
+    add_column_noalert(ovnsb_idl_loop.idl, &sbrec_load_balancer_col_protocol);
ebb439
+    add_column_noalert(ovnsb_idl_loop.idl,
ebb439
+                       &sbrec_load_balancer_col_external_ids);
ebb439
+
ebb439
     struct ovsdb_idl_index *sbrec_chassis_by_name
ebb439
         = chassis_index_create(ovnsb_idl_loop.idl);
ebb439
 
ebb439
diff --git a/ovn-sb.ovsschema b/ovn-sb.ovsschema
ebb439
index d1c506a22..a1ee8d8d1 100644
ebb439
--- a/ovn-sb.ovsschema
ebb439
+++ b/ovn-sb.ovsschema
ebb439
@@ -1,7 +1,7 @@
ebb439
 {
ebb439
     "name": "OVN_Southbound",
ebb439
-    "version": "2.10.0",
ebb439
-    "cksum": "2548342632 22615",
ebb439
+    "version": "2.11.0",
ebb439
+    "cksum": "455413803 23814",
ebb439
     "tables": {
ebb439
         "SB_Global": {
ebb439
             "columns": {
ebb439
@@ -152,6 +152,11 @@
ebb439
                      "type": {"key": {"type": "integer",
ebb439
                                       "minInteger": 1,
ebb439
                                       "maxInteger": 16777215}}},
ebb439
+                "load_balancers": {"type": {"key": {"type": "uuid",
ebb439
+                                                   "refTable": "Load_Balancer",
ebb439
+                                                   "refType": "weak"},
ebb439
+                                            "min": 0,
ebb439
+                                            "max": "unlimited"}},
ebb439
                 "external_ids": {
ebb439
                     "type": {"key": "string", "value": "string",
ebb439
                              "min": 0, "max": "unlimited"}}},
ebb439
@@ -447,6 +452,24 @@
ebb439
                     "type": {"key": "string", "value": "string",
ebb439
                              "min": 0, "max": "unlimited"}}},
ebb439
             "indexes": [["logical_port", "ip", "port", "protocol"]],
ebb439
+            "isRoot": true},
ebb439
+        "Load_Balancer": {
ebb439
+            "columns": {
ebb439
+                "name": {"type": "string"},
ebb439
+                "vips": {
ebb439
+                    "type": {"key": "string", "value": "string",
ebb439
+                             "min": 0, "max": "unlimited"}},
ebb439
+                "protocol": {
ebb439
+                    "type": {"key": {"type": "string",
ebb439
+                             "enum": ["set", ["tcp", "udp", "sctp"]]},
ebb439
+                             "min": 0, "max": 1}},
ebb439
+                "datapaths": {
ebb439
+                    "type": {"key": {"type": "uuid",
ebb439
+                                     "refTable": "Datapath_Binding"},
ebb439
+                             "min": 0, "max": "unlimited"}},
ebb439
+                "external_ids": {
ebb439
+                    "type": {"key": "string", "value": "string",
ebb439
+                             "min": 0, "max": "unlimited"}}},
ebb439
             "isRoot": true}
ebb439
     }
ebb439
 }
ebb439
diff --git a/ovn-sb.xml b/ovn-sb.xml
ebb439
index 182ff0a8a..7fa0496fe 100644
ebb439
--- a/ovn-sb.xml
ebb439
+++ b/ovn-sb.xml
ebb439
@@ -2497,6 +2497,12 @@ tcp.flags = RST;
ebb439
       constructed for each supported encapsulation.
ebb439
     </column>
ebb439
 
ebb439
+    <column name="load_balancers">
ebb439
+      

ebb439
+        Load balancers associated with the datapath.
ebb439
+      

ebb439
+    </column>
ebb439
+
ebb439
     <group title="OVN_Northbound Relationship">
ebb439
       

ebb439
         Each row in <ref table="Datapath_Binding"/> is associated with some
ebb439
@@ -4104,4 +4110,43 @@ tcp.flags = RST;
ebb439
       </column>
ebb439
     </group>
ebb439
   
ebb439
+
ebb439
+  
ebb439
+    

ebb439
+      Each row represents a load balancer.
ebb439
+    

ebb439
+
ebb439
+    <column name="name">
ebb439
+      A name for the load balancer.  This name has no special meaning or
ebb439
+      purpose other than to provide convenience for human interaction with
ebb439
+      the ovn-nb database.
ebb439
+    </column>
ebb439
+
ebb439
+    <column name="vips">
ebb439
+      A map of virtual IP addresses (and an optional port number with
ebb439
+      : as a separator) associated with this load balancer and
ebb439
+      their corresponding endpoint IP addresses (and optional port numbers
ebb439
+      with : as separators) separated by commas.
ebb439
+    </column>
ebb439
+
ebb439
+    <column name="protocol">
ebb439
+      

ebb439
+        Valid protocols are tcp, udp, or
ebb439
+        sctp.  This column is useful when a port number is
ebb439
+        provided as part of the vips column.  If this column is
ebb439
+        empty and a port number is provided as part of vips
ebb439
+        column, OVN assumes the protocol to be tcp.
ebb439
+      

ebb439
+    </column>
ebb439
+
ebb439
+    <column name="datapaths">
ebb439
+      Datapaths to which this load balancer applies to.
ebb439
+    </column>
ebb439
+
ebb439
+    <group title="Common Columns">
ebb439
+      <column name="external_ids">
ebb439
+        See External IDs at the beginning of this document.
ebb439
+      </column>
ebb439
+    </group>
ebb439
+  
ebb439
 </database>
ebb439
diff --git a/tests/ovn-northd.at b/tests/ovn-northd.at
ebb439
index 49d74b08b..961fb3712 100644
ebb439
--- a/tests/ovn-northd.at
ebb439
+++ b/tests/ovn-northd.at
ebb439
@@ -1845,3 +1845,89 @@ action=(reg0 = 0; reject { /* eth.dst <-> eth.src; ip.dst <-> ip.src; is implici
ebb439
 ])
ebb439
 
ebb439
 AT_CLEANUP
ebb439
+
ebb439
+AT_SETUP([ovn -- NB to SB load balancer sync])
ebb439
+ovn_start
ebb439
+
ebb439
+check ovn-nbctl --wait=sb lb-add lb0 10.0.0.10:80 10.0.0.4:8080
ebb439
+check_row_count nb:load_balancer 1
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Check that there are no SB load balancer rows."
ebb439
+check_row_count sb:load_balancer 0
ebb439
+
ebb439
+check ovn-nbctl ls-add sw0
ebb439
+check ovn-nbctl --wait=sb ls-lb-add sw0 lb0
ebb439
+sw0_sb_uuid=$(fetch_column datapath_binding _uuid external_ids:name=sw0)
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Check that there is one SB load balancer row for lb0."
ebb439
+check_row_count sb:load_balancer 1
ebb439
+check_column "10.0.0.10:80=10.0.0.4:8080 tcp" sb:load_balancer vips,protocol name=lb0
ebb439
+
ebb439
+lb0_uuid=$(fetch_column sb:load_balancer _uuid name=lb0)
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Check that SB lb0 has sw0 in datapaths column."
ebb439
+
ebb439
+check_column "$sw0_sb_uuid" sb:load_balancer datapaths name=lb0
ebb439
+check_column "$lb0_uuid" sb:datapath_binding load_balancers external_ids:name=sw0
ebb439
+
ebb439
+check ovn-nbctl --wait=sb set load_balancer . vips:"10.0.0.20\:90"="20.0.0.4:8080,30.0.0.4:8080"
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Check that SB lb0 has vips and protocol columns are set properly."
ebb439
+
ebb439
+check_column "10.0.0.10:80=10.0.0.4:8080 10.0.0.20:90=20.0.0.4:8080,30.0.0.4:8080 tcp" \
ebb439
+sb:load_balancer vips,protocol name=lb0
ebb439
+
ebb439
+check ovn-nbctl lr-add lr0
ebb439
+check ovn-nbctl --wait=sb lr-lb-add lr0 lb0
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Check that SB lb0 has only sw0 in datapaths column."
ebb439
+check_column "$sw0_sb_uuid" sb:load_balancer datapaths name=lb0
ebb439
+
ebb439
+check ovn-nbctl ls-add sw1
ebb439
+check ovn-nbctl --wait=sb ls-lb-add sw1 lb0
ebb439
+sw1_sb_uuid=$(fetch_column datapath_binding _uuid external_ids:name=sw1)
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Check that SB lb0 has sw0 and sw1 in datapaths column."
ebb439
+check_column "$sw0_sb_uuid $sw1_sb_uuid" sb:load_balancer datapaths name=lb0
ebb439
+check_column "$lb0_uuid" sb:datapath_binding load_balancers external_ids:name=sw1
ebb439
+
ebb439
+check ovn-nbctl --wait=sb lb-add lb1 10.0.0.30:80 20.0.0.50:8080 udp
ebb439
+check_row_count sb:load_balancer 1
ebb439
+
ebb439
+check ovn-nbctl --wait=sb lr-lb-add lr0 lb1
ebb439
+check_row_count sb:load_balancer 1
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Associate lb1 to sw1 and check that lb1 is created in SB DB."
ebb439
+
ebb439
+check ovn-nbctl --wait=sb ls-lb-add sw1 lb1
ebb439
+check_row_count sb:load_balancer 2
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Check that SB lb1 has vips and protocol columns are set properly."
ebb439
+check_column "10.0.0.30:80=20.0.0.50:8080 udp" sb:load_balancer vips,protocol name=lb1
ebb439
+
ebb439
+lb1_uuid=$(fetch_column sb:load_balancer _uuid name=lb1)
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Check that SB lb1 has sw1 in datapaths column."
ebb439
+
ebb439
+check_column "$sw1_sb_uuid" sb:load_balancer datapaths name=lb1
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: check that datapath sw1 has lb0 and lb1 set in the load_balancers column."
ebb439
+check_column "$lb0_uuid $lb1_uuid" sb:datapath_binding load_balancers external_ids:name=sw1
ebb439
+
ebb439
+echo
ebb439
+echo "__file__:__line__: Delete load balancer lb1 an check that datapath sw1's load_balancers are updated accordingly."
ebb439
+
ebb439
+ovn-nbctl --wait=sb lb-del lb1
ebb439
+check_column "$lb0_uuid" sb:datapath_binding load_balancers external_ids:name=sw1
ebb439
+
ebb439
+AT_CLEANUP
ebb439
diff --git a/utilities/ovn-sbctl.c b/utilities/ovn-sbctl.c
ebb439
index f93384940..30236c9cc 100644
ebb439
--- a/utilities/ovn-sbctl.c
ebb439
+++ b/utilities/ovn-sbctl.c
ebb439
@@ -1442,6 +1442,9 @@ static const struct ctl_table_class tables[SBREC_N_TABLES] = {
ebb439
 
ebb439
     [SBREC_TABLE_GATEWAY_CHASSIS].row_ids[0]
ebb439
     = {&sbrec_gateway_chassis_col_name, NULL, NULL},
ebb439
+
ebb439
+    [SBREC_TABLE_LOAD_BALANCER].row_ids[0]
ebb439
+    = {&sbrec_load_balancer_col_name, NULL, NULL},
ebb439
 };
ebb439
 
ebb439
 
ebb439
-- 
ebb439
2.28.0
ebb439