In a subsequent patch we need these functions in the route-exchange module.
Signed-off-by: Frode Nordahl <fnord...@ubuntu.com> --- controller/lb.c | 119 +++++++++++++++++++++++++++++++++ controller/lb.h | 19 ++++++ controller/ovn-controller.c | 128 ------------------------------------ 3 files changed, 138 insertions(+), 128 deletions(-) diff --git a/controller/lb.c b/controller/lb.c index 8f9f20ed5..8dd748d64 100644 --- a/controller/lb.c +++ b/controller/lb.c @@ -23,6 +23,7 @@ /* OVN includes */ #include "lb.h" #include "lib/ovn-sb-idl.h" +#include "local_data.h" #include "ovn/lex.h" VLOG_DEFINE_THIS_MODULE(controller_lb); @@ -144,3 +145,121 @@ ovn_controller_lb_find(const struct hmap *ovn_controller_lbs, return NULL; } +static struct load_balancers_by_dp * +load_balancers_by_dp_create(struct hmap *lbs, + const struct sbrec_datapath_binding *dp) +{ + struct load_balancers_by_dp *lbs_by_dp = xzalloc(sizeof *lbs_by_dp); + + lbs_by_dp->dp = dp; + hmap_insert(lbs, &lbs_by_dp->node, hash_uint64(dp->tunnel_key)); + return lbs_by_dp; +} + +static void +load_balancers_by_dp_destroy(struct load_balancers_by_dp *lbs_by_dp) +{ + if (!lbs_by_dp) { + return; + } + + free(lbs_by_dp->dp_lbs); + free(lbs_by_dp); +} + +struct load_balancers_by_dp * +load_balancers_by_dp_find(struct hmap *lbs, + const struct sbrec_datapath_binding *dp) +{ + uint32_t hash = hash_uint64(dp->tunnel_key); + struct load_balancers_by_dp *lbs_by_dp; + + HMAP_FOR_EACH_WITH_HASH (lbs_by_dp, node, hash, lbs) { + if (lbs_by_dp->dp == dp) { + return lbs_by_dp; + } + } + return NULL; +} + +static void +load_balancers_by_dp_add_one(const struct hmap *local_datapaths, + const struct sbrec_datapath_binding *datapath, + const struct sbrec_load_balancer *lb, + struct hmap *lbs) +{ + struct local_datapath *ldp = + get_local_datapath(local_datapaths, datapath->tunnel_key); + + if (!ldp) { + return; + } + + struct load_balancers_by_dp *lbs_by_dp = + load_balancers_by_dp_find(lbs, ldp->datapath); + if (!lbs_by_dp) { + lbs_by_dp = load_balancers_by_dp_create(lbs, ldp->datapath); + } + + if (lbs_by_dp->n_dp_lbs == lbs_by_dp->n_allocated_dp_lbs) { + lbs_by_dp->dp_lbs = x2nrealloc(lbs_by_dp->dp_lbs, + &lbs_by_dp->n_allocated_dp_lbs, + sizeof *lbs_by_dp->dp_lbs); + } + lbs_by_dp->dp_lbs[lbs_by_dp->n_dp_lbs++] = lb; +} + +/* Builds and returns a hmap of 'load_balancers_by_dp', one record for each + * local datapath. + */ +struct hmap * +load_balancers_by_dp_init(const struct hmap *local_datapaths, + const struct sbrec_load_balancer_table *lb_table) +{ + struct hmap *lbs = xmalloc(sizeof *lbs); + hmap_init(lbs); + + const struct sbrec_load_balancer *lb; + SBREC_LOAD_BALANCER_TABLE_FOR_EACH (lb, lb_table) { + for (size_t i = 0; i < lb->n_datapaths; i++) { + load_balancers_by_dp_add_one(local_datapaths, + lb->datapaths[i], lb, lbs); + } + /* datapath_group column is deprecated. */ + for (size_t i = 0; lb->datapath_group + && i < lb->datapath_group->n_datapaths; i++) { + load_balancers_by_dp_add_one(local_datapaths, + lb->datapath_group->datapaths[i], + lb, lbs); + } + for (size_t i = 0; lb->ls_datapath_group + && i < lb->ls_datapath_group->n_datapaths; i++) { + load_balancers_by_dp_add_one(local_datapaths, + lb->ls_datapath_group->datapaths[i], + lb, lbs); + } + for (size_t i = 0; lb->lr_datapath_group + && i < lb->lr_datapath_group->n_datapaths; i++) { + load_balancers_by_dp_add_one(local_datapaths, + lb->lr_datapath_group->datapaths[i], + lb, lbs); + } + } + return lbs; +} + +void +load_balancers_by_dp_cleanup(struct hmap *lbs) +{ + if (!lbs) { + return; + } + + struct load_balancers_by_dp *lbs_by_dp; + + HMAP_FOR_EACH_POP (lbs_by_dp, node, lbs) { + load_balancers_by_dp_destroy(lbs_by_dp); + } + hmap_destroy(lbs); + free(lbs); +} diff --git a/controller/lb.h b/controller/lb.h index 84d51c332..86e6e611b 100644 --- a/controller/lb.h +++ b/controller/lb.h @@ -20,6 +20,7 @@ #include "lib/lb.h" struct sbrec_load_balancer; +struct sbrec_load_balancer_table; struct ovn_controller_lb { struct hmap_node hmap_node; @@ -41,6 +42,15 @@ struct ovn_controller_lb { */ }; +/* Stores the load balancers that are applied to the datapath 'dp'. */ +struct load_balancers_by_dp { + struct hmap_node node; + const struct sbrec_datapath_binding *dp; + const struct sbrec_load_balancer **dp_lbs; + size_t n_allocated_dp_lbs; + size_t n_dp_lbs; +}; + struct ovn_controller_lb *ovn_controller_lb_create( const struct sbrec_load_balancer *, const struct smap *template_vars, @@ -51,5 +61,14 @@ struct ovn_controller_lb *ovn_controller_lb_find( const struct hmap *ovn_controller_lbs, const struct uuid *uuid); +struct hmap *load_balancers_by_dp_init( + const struct hmap *local_datapaths, + const struct sbrec_load_balancer_table *lb_table); +struct load_balancers_by_dp *load_balancers_by_dp_find( + struct hmap *lbs, + const struct sbrec_datapath_binding *dp); +void load_balancers_by_dp_cleanup(struct hmap *lbs); + + #endif /* OVN_CONTROLLER_LB_H */ diff --git a/controller/ovn-controller.c b/controller/ovn-controller.c index 4e30302ea..805d29c81 100644 --- a/controller/ovn-controller.c +++ b/controller/ovn-controller.c @@ -2357,134 +2357,6 @@ en_mff_ovn_geneve_run(struct engine_node *node, void *data) engine_set_node_state(node, EN_UNCHANGED); } -/* Stores the load balancers that are applied to the datapath 'dp'. */ -struct load_balancers_by_dp { - struct hmap_node node; - const struct sbrec_datapath_binding *dp; - const struct sbrec_load_balancer **dp_lbs; - size_t n_allocated_dp_lbs; - size_t n_dp_lbs; -}; - -static struct load_balancers_by_dp * -load_balancers_by_dp_create(struct hmap *lbs, - const struct sbrec_datapath_binding *dp) -{ - struct load_balancers_by_dp *lbs_by_dp = xzalloc(sizeof *lbs_by_dp); - - lbs_by_dp->dp = dp; - hmap_insert(lbs, &lbs_by_dp->node, hash_uint64(dp->tunnel_key)); - return lbs_by_dp; -} - -static void -load_balancers_by_dp_destroy(struct load_balancers_by_dp *lbs_by_dp) -{ - if (!lbs_by_dp) { - return; - } - - free(lbs_by_dp->dp_lbs); - free(lbs_by_dp); -} - -static struct load_balancers_by_dp * -load_balancers_by_dp_find(struct hmap *lbs, - const struct sbrec_datapath_binding *dp) -{ - uint32_t hash = hash_uint64(dp->tunnel_key); - struct load_balancers_by_dp *lbs_by_dp; - - HMAP_FOR_EACH_WITH_HASH (lbs_by_dp, node, hash, lbs) { - if (lbs_by_dp->dp == dp) { - return lbs_by_dp; - } - } - return NULL; -} - -static void -load_balancers_by_dp_add_one(const struct hmap *local_datapaths, - const struct sbrec_datapath_binding *datapath, - const struct sbrec_load_balancer *lb, - struct hmap *lbs) -{ - struct local_datapath *ldp = - get_local_datapath(local_datapaths, datapath->tunnel_key); - - if (!ldp) { - return; - } - - struct load_balancers_by_dp *lbs_by_dp = - load_balancers_by_dp_find(lbs, ldp->datapath); - if (!lbs_by_dp) { - lbs_by_dp = load_balancers_by_dp_create(lbs, ldp->datapath); - } - - if (lbs_by_dp->n_dp_lbs == lbs_by_dp->n_allocated_dp_lbs) { - lbs_by_dp->dp_lbs = x2nrealloc(lbs_by_dp->dp_lbs, - &lbs_by_dp->n_allocated_dp_lbs, - sizeof *lbs_by_dp->dp_lbs); - } - lbs_by_dp->dp_lbs[lbs_by_dp->n_dp_lbs++] = lb; -} - -/* Builds and returns a hmap of 'load_balancers_by_dp', one record for each - * local datapath. - */ -static struct hmap * -load_balancers_by_dp_init(const struct hmap *local_datapaths, - const struct sbrec_load_balancer_table *lb_table) -{ - struct hmap *lbs = xmalloc(sizeof *lbs); - hmap_init(lbs); - - const struct sbrec_load_balancer *lb; - SBREC_LOAD_BALANCER_TABLE_FOR_EACH (lb, lb_table) { - for (size_t i = 0; i < lb->n_datapaths; i++) { - load_balancers_by_dp_add_one(local_datapaths, - lb->datapaths[i], lb, lbs); - } - /* datapath_group column is deprecated. */ - for (size_t i = 0; lb->datapath_group - && i < lb->datapath_group->n_datapaths; i++) { - load_balancers_by_dp_add_one(local_datapaths, - lb->datapath_group->datapaths[i], - lb, lbs); - } - for (size_t i = 0; lb->ls_datapath_group - && i < lb->ls_datapath_group->n_datapaths; i++) { - load_balancers_by_dp_add_one(local_datapaths, - lb->ls_datapath_group->datapaths[i], - lb, lbs); - } - for (size_t i = 0; lb->lr_datapath_group - && i < lb->lr_datapath_group->n_datapaths; i++) { - load_balancers_by_dp_add_one(local_datapaths, - lb->lr_datapath_group->datapaths[i], - lb, lbs); - } - } - return lbs; -} - -static void -load_balancers_by_dp_cleanup(struct hmap *lbs) -{ - if (!lbs) { - return; - } - - struct load_balancers_by_dp *lbs_by_dp; - - HMAP_FOR_EACH_POP (lbs_by_dp, node, lbs) { - load_balancers_by_dp_destroy(lbs_by_dp); - } - hmap_destroy(lbs); - free(lbs); -} - /* Engine node which is used to handle runtime related data to * load balancers. */ struct ed_type_lb_data { -- 2.45.2 _______________________________________________ dev mailing list d...@openvswitch.org https://mail.openvswitch.org/mailman/listinfo/ovs-dev