mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-09-23 06:41:32 +02:00
The global table of known variables names can only grow and was designed for static names that are registered at boot. Nowadays it's possible to set dynamic variable names from Lua or from the CLI, which causes a real problem that was partially addressed in 2.2 with commit 4e172c93f ("MEDIUM: lua: Add `ifexist` parameter to `set_var`"). Please see github issue #624 for more context. This patch simplifies all this by removing the need for a central registry of known names, and storing 64-bit hashes instead. This is highly sufficient given the low number of variables in each context. The hash is calculated using XXH64() which is bijective over the 64-bit space thus is guaranteed collision-free for 1..8 chars. Above that the risk remains around 1/2^64 per extra 8 chars so in practice this is highly sufficient for our usage. A random seed is used at boot to seed the hash so that it's not attackable from Lua for example. There's one particular nit though. The "ifexist" hack mentioned above is now limited to variables of scope "proc" only, and will only match variables that were already created or declared, but will now verify the scope as well. This may affect some bogus Lua scripts and SPOE agents which used to accidentally work because a similarly named variable used to exist in a different scope. These ones may need to be fixed to comply with the doc. Now we can sum up the situation as this one: - ephemeral variables (scopes sess, txn, req, res) will always be usable, regardless of any prior declaration. This effectively addresses the most problematic change from the commit above that in order to work well could have required some script auditing ; - process-wide variables (scope proc) that are mentioned in the configuration, referenced in a "register-var-names" SPOE directive, or created via "set-var" in the global section or the CLI, are permanent and will always accept to be set, with or without the "ifexist" restriction (SPOE uses this internally as well). - process-wide variables (scope proc) that are only created via a set-var() tcp/http action, via Lua's set_var() calls, or via an SPOE with the "force-set-var" directive), will not be permanent but will always accept to be replaced once they are created, even if "ifexist" is present - process-wide variables (scope proc) that do not exist will only support being created via the set-var() tcp/http action, Lua's set_var() calls without "ifexist", or an SPOE declared with "force-set-var". This means that non-proc variables do not care about "ifexist" nor prior declaration, and that using "ifexist" should most often be reliable in Lua and that SPOE should most often work without any prior declaration. It may be doable to turn "ifexist" to 1 by default in Lua to further ease the transition. Note: regtests were adjusted. Cc: Tim Düsterhus <tim@bastelstu.be>
64 lines
1.8 KiB
C
64 lines
1.8 KiB
C
/*
|
|
* include/haproxy/vars-t.h
|
|
* Macros and structures definitions for variables.
|
|
*
|
|
* Copyright (C) 2015 Thierry FOURNIER <tfournier@arpalert.org>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
* exclusively.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#ifndef _HAPROXY_VARS_T_H
|
|
#define _HAPROXY_VARS_T_H
|
|
|
|
#include <haproxy/sample_data-t.h>
|
|
#include <haproxy/thread-t.h>
|
|
|
|
/* flags used when setting/clearing variables */
|
|
#define VF_UPDATEONLY 0x00000001 // SCOPE_PROC variables are only updated
|
|
#define VF_CREATEONLY 0x00000002 // do nothing if the variable already exists
|
|
#define VF_PERMANENT 0x00000004 // variables known to the config parser
|
|
|
|
enum vars_scope {
|
|
SCOPE_SESS = 0,
|
|
SCOPE_TXN,
|
|
SCOPE_REQ,
|
|
SCOPE_RES,
|
|
SCOPE_PROC,
|
|
SCOPE_CHECK,
|
|
};
|
|
|
|
struct vars {
|
|
struct list head;
|
|
enum vars_scope scope;
|
|
unsigned int size;
|
|
__decl_thread(HA_RWLOCK_T rwlock);
|
|
};
|
|
|
|
/* This struct describes a variable as found in an arg_data */
|
|
struct var_desc {
|
|
uint64_t name_hash;
|
|
enum vars_scope scope;
|
|
};
|
|
|
|
struct var {
|
|
struct list l; /* Used for chaining vars. */
|
|
uint64_t name_hash; /* XXH3() of the variable's name */
|
|
uint flags; // VF_*
|
|
/* 32-bit hole here */
|
|
struct sample_data data; /* data storage. */
|
|
};
|
|
|
|
#endif
|