]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: lb: use the LB ops tables
authorMaxime Henrion <mhenrion@haproxy.com>
Mon, 27 Apr 2026 19:28:42 +0000 (15:28 -0400)
committerOlivier Houchard <cognet@ci0.org>
Tue, 28 Apr 2026 13:31:51 +0000 (15:31 +0200)
Remove manual initialization of the callbacks and use the ops field in
the lbprm struct to invoke those callbacks.

src/backend.c
src/lb_chash.c
src/lb_fas.c
src/lb_fwlc.c
src/lb_fwrr.c
src/lb_map.c
src/lb_ss.c
src/proxy.c
src/queue.c
src/server.c
src/stream.c

index fb04f3b24aaf6adf73bb4afc67792bf2112cc067..fe1f680224109c6bddc146ee30289ae7e6d7cdd0 100644 (file)
@@ -2271,8 +2271,8 @@ int connect_server(struct stream *s)
                s->flags |= SF_CURR_SESS;
                count = _HA_ATOMIC_ADD_FETCH(&srv->cur_sess, 1);
                COUNTERS_UPDATE_MAX(&srv->counters.cur_sess_max, count);
-               if (s->be->lbprm.server_take_conn)
-                       s->be->lbprm.server_take_conn(srv);
+               if (s->be->lbprm.ops && s->be->lbprm.ops->server_take_conn)
+                       s->be->lbprm.ops->server_take_conn(srv);
        }
 
        /* Now handle synchronously connected sockets. We know the stream connector
index 8b4d5490816ad4e6536fcdc2f7aa79e4b8be2616..60f92da8d52d77f87d2284c6d342cf0fc46fa07c 100644 (file)
@@ -589,14 +589,6 @@ int chash_init_server_tree(struct proxy *p)
        struct server *srv;
        struct eb_root init_head = EB_ROOT;
 
-       p->lbprm.set_server_status_up   = chash_set_server_status_up;
-       p->lbprm.set_server_status_down = chash_set_server_status_down;
-       p->lbprm.update_server_eweight  = chash_update_server_weight;
-       p->lbprm.server_init            = chash_server_init;
-       p->lbprm.server_deinit          = chash_server_deinit;
-       p->lbprm.server_take_conn = NULL;
-       p->lbprm.server_drop_conn = NULL;
-
        p->lbprm.wdiv = BE_WEIGHT_SCALE;
        for (srv = p->srv; srv; srv = srv->next) {
                srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
index 43dc3be808597057ef3af4ca60c5876031d62244..add9eb7b8c0ecb001a4a2f2efb45c56f5737374b 100644 (file)
@@ -260,12 +260,6 @@ int fas_init_server_tree(struct proxy *p)
        struct server *srv;
        struct eb_root init_head = EB_ROOT;
 
-       p->lbprm.set_server_status_up   = fas_set_server_status_up;
-       p->lbprm.set_server_status_down = fas_set_server_status_down;
-       p->lbprm.update_server_eweight  = fas_update_server_weight;
-       p->lbprm.server_take_conn = fas_srv_reposition;
-       p->lbprm.server_drop_conn = fas_srv_reposition;
-
        p->lbprm.wdiv = BE_WEIGHT_SCALE;
        for (srv = p->srv; srv; srv = srv->next) {
                srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
index 933bc55911bcc16c84aab2821fb7592028ae47be..92bb6a34122869950f255c67aa818afa9a1680c5 100644 (file)
@@ -714,15 +714,6 @@ int fwlc_init_server_tree(struct proxy *p)
        struct server *srv;
        struct eb_root init_head = EB_ROOT;
 
-       p->lbprm.set_server_status_up   = fwlc_set_server_status_up;
-       p->lbprm.set_server_status_down = fwlc_set_server_status_down;
-       p->lbprm.update_server_eweight  = fwlc_update_server_weight;
-       p->lbprm.server_take_conn = fwlc_srv_reposition;
-       p->lbprm.server_drop_conn = fwlc_srv_reposition;
-       p->lbprm.server_requeue   = fwlc_srv_reposition;
-       p->lbprm.server_deinit    = fwlc_server_deinit;
-       p->lbprm.proxy_deinit     = fwlc_proxy_deinit;
-
        p->lbprm.wdiv = BE_WEIGHT_SCALE;
        for (srv = p->srv; srv; srv = srv->next) {
                srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
index d8f7742201715ef6c5eacb9b276350fb56835ed7..9ab6ceae82e355630be138639988a00804f29494 100644 (file)
@@ -299,10 +299,6 @@ int fwrr_init_server_groups(struct proxy *p)
        struct eb_root init_head = EB_ROOT;
        int i, j;
 
-       p->lbprm.set_server_status_up   = fwrr_set_server_status_up;
-       p->lbprm.set_server_status_down = fwrr_set_server_status_down;
-       p->lbprm.update_server_eweight  = fwrr_update_server_weight;
-
        p->lbprm.wdiv = BE_WEIGHT_SCALE;
        for (srv = p->srv; srv; srv = srv->next) {
                srv->next_eweight = (srv->uweight * p->lbprm.wdiv + p->lbprm.wmult - 1) / p->lbprm.wmult;
index c62d81822c72a76e55f1f1ae2ab4c40b9561bec2..1b74c217c407e4c5c156df899a2f369fb154128c 100644 (file)
@@ -143,10 +143,6 @@ int init_server_map(struct proxy *p)
        int pgcd;
        int act, bck;
 
-       p->lbprm.set_server_status_up   = map_set_server_status_up;
-       p->lbprm.set_server_status_down = map_set_server_status_down;
-       p->lbprm.update_server_eweight = NULL;
        if (!p->srv)
                return 0;
 
index e95b888dfc73e5bec5de41e2b1db05490546a0f9..40edfbfe3e44f2cb47185722cfebcc2b65dc82f3 100644 (file)
@@ -147,10 +147,6 @@ int init_server_ss(struct proxy *p)
 {
        struct server *srv;
 
-       p->lbprm.set_server_status_up   = ss_set_server_status_up;
-       p->lbprm.set_server_status_down = ss_set_server_status_down;
-       p->lbprm.update_server_eweight = NULL;
-
        if (!p->srv)
                return 0;
 
index 1acda4457589ddb139011a6e758ee4ebecef4050..5656a51d5fffa4d04f72d7a352118d7f2836c360 100644 (file)
@@ -392,8 +392,8 @@ void deinit_proxy(struct proxy *p)
                list_for_each_entry(srvdf, &server_deinit_list, list)
                        srvdf->fct(s);
 
-               if (p->lbprm.server_deinit)
-                       p->lbprm.server_deinit(s);
+               if (p->lbprm.ops && p->lbprm.ops->server_deinit)
+                       p->lbprm.ops->server_deinit(s);
 
                s = srv_drop(s);
        }/* end while(s) */
@@ -406,8 +406,8 @@ void deinit_proxy(struct proxy *p)
                srv_free(&p->defsrv);
        }
 
-       if (p->lbprm.proxy_deinit)
-               p->lbprm.proxy_deinit(p);
+       if (p->lbprm.ops && p->lbprm.ops->proxy_deinit)
+               p->lbprm.ops->proxy_deinit(p);
 
        list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
                guid_remove(&l->guid);
@@ -2615,46 +2615,45 @@ int proxy_finalize(struct proxy *px, int *err_code)
        case BE_LB_KIND_RR:
                if ((px->lbprm.algo & BE_LB_PARM) == BE_LB_RR_STATIC) {
                        px->lbprm.algo |= BE_LB_LKUP_MAP;
-                       init_server_map(px);
+                       px->lbprm.ops = &lb_map_ops;
                } else if ((px->lbprm.algo & BE_LB_PARM) == BE_LB_RR_RANDOM) {
                        px->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
-                       if (chash_init_server_tree(px) < 0) {
-                               cfgerr++;
-                       }
+                       px->lbprm.ops = &lb_chash_ops;
                } else {
                        px->lbprm.algo |= BE_LB_LKUP_RRTREE | BE_LB_PROP_DYN;
-                       fwrr_init_server_groups(px);
+                       px->lbprm.ops = &lb_fwrr_ops;
                }
                break;
 
        case BE_LB_KIND_CB:
                if ((px->lbprm.algo & BE_LB_PARM) == BE_LB_CB_LC) {
                        px->lbprm.algo |= BE_LB_LKUP_LCTREE | BE_LB_PROP_DYN;
-                       fwlc_init_server_tree(px);
+                       px->lbprm.ops = &lb_fwlc_ops;
                } else {
                        px->lbprm.algo |= BE_LB_LKUP_FSTREE | BE_LB_PROP_DYN;
-                       fas_init_server_tree(px);
+                       px->lbprm.ops = &lb_fas_ops;
                }
                break;
 
        case BE_LB_KIND_HI:
                if ((px->lbprm.algo & BE_LB_HASH_TYPE) == BE_LB_HASH_CONS) {
                        px->lbprm.algo |= BE_LB_LKUP_CHTREE | BE_LB_PROP_DYN;
-                       if (chash_init_server_tree(px) < 0) {
-                               cfgerr++;
-                       }
+                       px->lbprm.ops = &lb_chash_ops;
                } else {
                        px->lbprm.algo |= BE_LB_LKUP_MAP;
-                       init_server_map(px);
+                       px->lbprm.ops = &lb_map_ops;
                }
                break;
        case BE_LB_KIND_SA:
                if ((px->lbprm.algo & BE_LB_PARM) == BE_LB_SA_SS) {
                        px->lbprm.algo |= BE_LB_PROP_DYN;
-                       init_server_ss(px);
+                       px->lbprm.ops = &lb_ss_ops;
                }
                break;
        }
+       if (px->lbprm.ops && px->lbprm.ops->proxy_init &&
+           px->lbprm.ops->proxy_init(px) < 0)
+               cfgerr++;
        HA_RWLOCK_INIT(&px->lbprm.lock);
 
        if (px->options & PR_O_LOGASAP)
index 1fa40ed34c5b0b9e0f44b31ed7221a5641f60579..59d52771ef25c918f0371d1f37bf228d3b8b1e7f 100644 (file)
@@ -508,8 +508,8 @@ int process_srv_queue(struct server *s)
                _HA_ATOMIC_SUB(&p->totpend, done);
                _HA_ATOMIC_ADD(&p->served, done);
                __ha_barrier_atomic_store();
-               if (p->lbprm.server_take_conn)
-                       p->lbprm.server_take_conn(s);
+               if (p->lbprm.ops && p->lbprm.ops->server_take_conn)
+                       p->lbprm.ops->server_take_conn(s);
        }
        if (s->served == 0 && p->served == 0 && !HA_ATOMIC_LOAD(&p->ready_srv)) {
                int i;
index 88a05288ec9239dfee956f9ec570ff3584283b1f..8e49f79cf3ca2f0469855f71d712221ad763b4ed 100644 (file)
@@ -191,9 +191,9 @@ static void _srv_set_inetaddr_port(struct server *srv,
        else
                srv->flags &= ~SRV_F_MAPPORTS;
 
-       if (srv->proxy->lbprm.update_server_eweight) {
+       if (srv->proxy->lbprm.ops && srv->proxy->lbprm.ops->update_server_eweight) {
                /* some balancers (chash in particular) may use the addr in their routing decisions */
-               srv->proxy->lbprm.update_server_eweight(srv);
+               srv->proxy->lbprm.ops->update_server_eweight(srv);
        }
 
        if (srv->log_target && srv->log_target->type == LOG_TARGET_DGRAM) {
@@ -2530,8 +2530,8 @@ struct task *server_requeue(struct task *t, void *context, unsigned int state)
        /* let's call the LB's requeue function. If it fails, it will itself
         * wake us up.
         */
-       if (srv->proxy->lbprm.server_requeue)
-               srv->proxy->lbprm.server_requeue(srv);
+       if (srv->proxy->lbprm.ops && srv->proxy->lbprm.ops->server_requeue)
+               srv->proxy->lbprm.ops->server_requeue(srv);
        return t;
 }
 
@@ -5960,7 +5960,7 @@ static int srv_alloc_lb(struct server *sv, struct proxy *be)
        sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
        sv->lb_nodes_now = 0;
 
-       if (be->lbprm.server_init && be->lbprm.server_init(sv) < 0)
+       if (be->lbprm.ops && be->lbprm.ops->server_init && be->lbprm.ops->server_init(sv) < 0)
                return 0; // typically out of memory
 
        return 1;
@@ -6476,7 +6476,7 @@ int srv_check_for_deletion(const char *bename, const char *svname, struct proxy
 
        /* Only servers in maintenance can be deleted. This ensures that the
         * server is not present anymore in the lb structures (through
-        * lbprm.set_server_status_down).
+        * lbprm.ops->set_server_status_down).
         */
        if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
                msg = "Only servers in maintenance mode can be deleted.";
@@ -6562,8 +6562,8 @@ static int cli_parse_delete_server(char **args, char *payload, struct appctx *ap
        if (srv->agent.state & CHK_ST_CONFIGURED)
                check_purge(&srv->agent);
 
-       if (srv->proxy->lbprm.server_deinit)
-               srv->proxy->lbprm.server_deinit(srv);
+       if (srv->proxy->lbprm.ops && srv->proxy->lbprm.ops->server_deinit)
+               srv->proxy->lbprm.ops->server_deinit(srv);
 
        while (!MT_LIST_ISEMPTY(&srv->watcher_list)) {
                srv_watch = MT_LIST_NEXT(&srv->watcher_list, struct watcher *, el);
@@ -6731,15 +6731,18 @@ static void srv_lb_propagate(struct server *s)
 {
        struct proxy *px = s->proxy;
 
-       if (px->lbprm.update_server_eweight)
-               px->lbprm.update_server_eweight(s);
+       if (!px->lbprm.ops)
+               return;
+
+       if (px->lbprm.ops->update_server_eweight)
+               px->lbprm.ops->update_server_eweight(s);
        else if (srv_willbe_usable(s)) {
-               if (px->lbprm.set_server_status_up)
-                       px->lbprm.set_server_status_up(s);
+               if (px->lbprm.ops->set_server_status_up)
+                       px->lbprm.ops->set_server_status_up(s);
        }
        else {
-               if (px->lbprm.set_server_status_down)
-                       px->lbprm.set_server_status_down(s);
+               if (px->lbprm.ops->set_server_status_down)
+                       px->lbprm.ops->set_server_status_down(s);
        }
 }
 
index f6b01d6ab0fa424209e03c6744e3525057271c24..93a00019c4a2318393de633a9d428924749ef248 100644 (file)
@@ -2931,15 +2931,15 @@ void sess_change_server(struct stream *strm, struct server *newsrv)
                stream_del_srv_conn(strm);
                _HA_ATOMIC_DEC(&oldsrv->served);
                __ha_barrier_atomic_store();
-               if (oldsrv->proxy->lbprm.server_drop_conn)
-                       oldsrv->proxy->lbprm.server_drop_conn(oldsrv);
+               if (oldsrv->proxy->lbprm.ops && oldsrv->proxy->lbprm.ops->server_drop_conn)
+                       oldsrv->proxy->lbprm.ops->server_drop_conn(oldsrv);
        }
 
        if (newsrv) {
                _HA_ATOMIC_INC(&newsrv->proxy->served);
                __ha_barrier_atomic_store();
-               if (newsrv->proxy->lbprm.server_take_conn)
-                       newsrv->proxy->lbprm.server_take_conn(newsrv);
+               if (newsrv->proxy->lbprm.ops && newsrv->proxy->lbprm.ops->server_take_conn)
+                       newsrv->proxy->lbprm.ops->server_take_conn(newsrv);
                stream_add_srv_conn(strm, newsrv);
        }
 }