MINOR: cfgdiag: adjust diag on servers

Adjust code dealing with diagnostics performed on server. The objective
is to extract the check on duplicate cookies in a dedicated function
outside of the proxies/servers loop.

This does not have any noticeable impact. This patch is merely a code
improvment to implement easily new future diagnostics on servers.
This commit is contained in:
Amaury Denoyelle 2025-11-14 09:40:07 +01:00
parent d12971dfea
commit d92f8f84fb

View File

@ -8,6 +8,10 @@
#include <haproxy/proxy.h>
#include <haproxy/server.h>
struct cookie_entry {
struct ebpt_node node;
};
/* Use this function to emit diagnostic.
* This can be used as a shortcut to set value pointed by <ret> to 1 at the
* same time.
@ -41,43 +45,42 @@ static inline void *diag_alloc(size_t size)
* value. Backup servers are not taken into account as it can be quite common to
* share cookie values in this case.
*/
static void check_server_cookies(int *ret)
static void srv_diag_cookies(int *ret, struct server *srv, struct eb_root *cookies_tree)
{
struct cookie_entry {
struct ebpt_node node;
};
struct proxy *px;
struct server *srv;
struct eb_root cookies_tree = EB_ROOT_UNIQUE;
struct ebpt_node *cookie_node;
struct cookie_entry *cookie_entry;
struct ebpt_node *node;
for (px = proxies_list; px; px = px->next) {
for (srv = px->srv; srv; srv = srv->next) {
/* do not take into account backup servers */
if (!srv->cookie || (srv->flags & SRV_F_BACKUP))
continue;
return;
cookie_node = ebis_lookup(&cookies_tree, srv->cookie);
cookie_node = ebis_lookup(cookies_tree, srv->cookie);
if (cookie_node) {
diag_warning(ret, "parsing [%s:%d] : 'server %s' : same cookie value is set for a previous non-backup server in the same backend, it may break connection persistence\n",
srv->conf.file, srv->conf.line, srv->id);
continue;
}
cookie_entry = diag_alloc(sizeof(*cookie_entry));
cookie_entry->node.key = srv->cookie;
ebis_insert(&cookies_tree, &cookie_entry->node);
else {
cookie_node = diag_alloc(sizeof(*cookie_node));
cookie_node->key = srv->cookie;
ebis_insert(cookies_tree, cookie_node);
}
}
/* clear the tree and free its entries */
while ((node = ebpt_first(&cookies_tree))) {
cookie_entry = ebpt_entry(node, struct cookie_entry, node);
eb_delete(&node->node);
free(cookie_entry);
/* Perform a series of diagnostics on every servers from the configuration. */
static void run_servers_diag(int *ret)
{
struct eb_root cookies_tree = EB_ROOT_UNIQUE;
struct ebpt_node *cookie_node;
struct proxy *px;
struct server *srv;
for (px = proxies_list; px; px = px->next) {
for (srv = px->srv; srv; srv = srv->next)
srv_diag_cookies(ret, srv, &cookies_tree);
/* clear the cookies tree before passing to the next proxy */
while ((cookie_node = ebpt_first(&cookies_tree))) {
ebpt_delete(cookie_node);
free(cookie_node);
}
}
}
@ -91,7 +94,7 @@ int cfg_run_diagnostics()
{
int ret = 0;
check_server_cookies(&ret);
run_servers_diag(&ret);
return ret;
}