blob: 28ed9bd4911797dd06cdac481eba8de9389ff9b8 [file] [log] [blame]
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01001#ifndef _TYPES_HLUA_H
2#define _TYPES_HLUA_H
3
Thierry FOURNIERa718b292015-03-04 16:48:34 +01004#ifdef USE_LUA
5
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01006#include <lua.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01007#include <lauxlib.h>
8
Dragan Dosen26743032019-04-30 15:54:36 +02009#include <common/regex.h>
Thierry FOURNIER2da788e2017-09-11 18:37:23 +020010#include <common/xref.h>
11
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020012#include <types/http_ana.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010013#include <types/proxy.h>
14#include <types/server.h>
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010015#include <types/stick_table.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010016
Thierry FOURNIER0efc94c2015-09-16 21:22:28 +020017#define CLASS_CORE "Core"
18#define CLASS_TXN "TXN"
19#define CLASS_FETCHES "Fetches"
20#define CLASS_CONVERTERS "Converters"
21#define CLASS_SOCKET "Socket"
22#define CLASS_CHANNEL "Channel"
23#define CLASS_HTTP "HTTP"
24#define CLASS_MAP "Map"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020025#define CLASS_APPLET_TCP "AppletTCP"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020026#define CLASS_APPLET_HTTP "AppletHTTP"
Thierry Fournierf61aa632016-02-19 20:56:00 +010027#define CLASS_PROXY "Proxy"
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010028#define CLASS_SERVER "Server"
Thierry Fournierff480422016-02-25 08:36:46 +010029#define CLASS_LISTENER "Listener"
Thierry FOURNIER31904272017-10-25 12:59:51 +020030#define CLASS_REGEX "Regex"
Adis Nezirovic8878f8e2018-07-13 12:18:33 +020031#define CLASS_STKTABLE "StickTable"
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010032
Willy Tarreau87b09662015-04-03 00:22:06 +020033struct stream;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010034
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010035#define HLUA_RUN 0x00000001
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +010036#define HLUA_CTRLYIELD 0x00000002
Thierry FOURNIERef6a2112015-03-05 17:45:34 +010037#define HLUA_WAKERESWR 0x00000004
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +010038#define HLUA_WAKEREQWR 0x00000008
Thierry FOURNIER0a99b892015-08-26 00:14:17 +020039#define HLUA_EXIT 0x00000010
Thierry FOURNIER380d0932015-01-23 14:27:52 +010040
Thierry FOURNIER7fa05492015-12-20 18:42:25 +010041#define HLUA_F_AS_STRING 0x01
Thierry FOURNIERca988662015-12-20 18:43:03 +010042#define HLUA_F_MAY_USE_HTTP 0x02
Thierry FOURNIER7fa05492015-12-20 18:42:25 +010043
Christopher Fauletbfab2dd2019-07-26 15:09:53 +020044#define HLUA_TXN_NOTERM 0x00000001
Thierry FOURNIERab00df62016-07-14 11:42:37 +020045
Thierry Fournier49d48422016-02-19 12:09:29 +010046#define HLUA_CONCAT_BLOCSZ 2048
47
Thierry FOURNIER380d0932015-01-23 14:27:52 +010048enum hlua_exec {
49 HLUA_E_OK = 0,
50 HLUA_E_AGAIN, /* LUA yield, must resume the stack execution later, when
51 the associatedtask is waked. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +020052 HLUA_E_ETMOUT, /* Execution timeout */
53 HLUA_E_NOMEM, /* Out of memory error */
54 HLUA_E_YIELD, /* LUA code try to yield, and this is not allowed */
Thierry FOURNIER380d0932015-01-23 14:27:52 +010055 HLUA_E_ERRMSG, /* LUA stack execution failed with a string error message
56 in the top of stack. */
57 HLUA_E_ERR, /* LUA stack execution failed without error message. */
58};
59
60struct hlua {
61 lua_State *T; /* The LUA stack. */
62 int Tref; /* The reference of the stack in coroutine case.
63 -1 for the main lua stack. */
64 int Mref; /* The reference of the memory context in coroutine case.
65 -1 if the memory context is not used. */
66 int nargs; /* The number of arguments in the stack at the start of execution. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010067 unsigned int flags; /* The current execution flags. */
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +010068 int wake_time; /* The lua wants to be waked at this time, or before. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +020069 unsigned int max_time; /* The max amount of execution time for an Lua process, in ms. */
70 unsigned int start_time; /* The ms time when the Lua starts the last execution. */
71 unsigned int run_time; /* Lua total execution time in ms. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +010072 struct task *task; /* The task associated with the lua stack execution.
73 We must wake this task to continue the task execution */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010074 struct list com; /* The list head of the signals attached to this task. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +010075 struct ebpt_node node;
Willy Tarreauf31af932020-01-14 09:59:38 +010076 int gc_count; /* number of items which need a GC */
Thierry FOURNIER380d0932015-01-23 14:27:52 +010077};
78
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010079/* This is a part of the list containing references to functions
80 * called at the initialisation time.
81 */
82struct hlua_init_function {
83 struct list l;
84 int function_ref;
85};
86
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +010087/* This struct contains the lua data used to bind
88 * Lua function on HAProxy hook like sample-fetches
89 * or actions.
90 */
91struct hlua_function {
92 char *name;
93 int function_ref;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +010094 int nargs;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +010095};
96
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +010097/* This struct is used with the structs:
98 * - http_req_rule
99 * - http_res_rule
100 * - tcp_rule
101 * It contains the lua execution configuration.
102 */
103struct hlua_rule {
104 struct hlua_function fcn;
105 char **args;
106};
107
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100108/* This struct contains the pointer provided on the most
109 * of internal HAProxy calls during the processing of
110 * rules, converters and sample-fetches. This struct is
111 * associated with the lua object called "TXN".
112 */
113struct hlua_txn {
Willy Tarreau87b09662015-04-03 00:22:06 +0200114 struct stream *s;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100115 struct proxy *p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +0100116 int dir; /* SMP_OPT_DIR_{REQ,RES} */
Thierry FOURNIERab00df62016-07-14 11:42:37 +0200117 int flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100118};
119
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200120/* This struct contains the applet context. */
121struct hlua_appctx {
122 struct appctx *appctx;
123 luaL_Buffer b; /* buffer used to prepare strings. */
124 struct hlua_txn htxn;
125};
126
Ilya Shipitsind4259502020-04-08 01:07:56 +0500127/* This struct is used with sample fetches and sample converters. */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100128struct hlua_smp {
Willy Tarreau87b09662015-04-03 00:22:06 +0200129 struct stream *s;
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100130 struct proxy *p;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +0100131 unsigned int flags; /* LUA_F_OPT_* */
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +0100132 int dir; /* SMP_OPT_DIR_{REQ,RES} */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100133};
134
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +0100135/* This struct contains data used with sleep functions. */
136struct hlua_sleep {
137 struct task *task; /* task associated with sleep. */
138 struct list com; /* list of signal to wake at the end of sleep. */
139 unsigned int wakeup_ms; /* hour to wakeup. */
140};
141
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100142/* This struct is used to create coprocess doing TCP or
Willy Tarreau87b09662015-04-03 00:22:06 +0200143 * SSL I/O. It uses a fake stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100144 */
145struct hlua_socket {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +0200146 struct xref xref; /* cross reference with the stream used for socket I/O. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100147 luaL_Buffer b; /* buffer used to prepare strings. */
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +0200148 unsigned long tid; /* Store the thread id which creates the socket. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100149};
150
Thierry Fournier49d48422016-02-19 12:09:29 +0100151struct hlua_concat {
152 int size;
153 int len;
154};
155
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100156struct hlua_addr {
157 union {
158 struct {
159 struct in_addr ip;
160 struct in_addr mask;
161 } v4;
162 struct {
163 struct in6_addr ip;
164 struct in6_addr mask;
165 } v6;
166 } addr;
167 int type;
168};
169
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100170#else /* USE_LUA */
171
172/* Empty struct for compilation compatibility */
173struct hlua { };
174struct hlua_socket { };
Thierry FOURNIER231ef1d2015-07-30 19:03:55 +0200175struct hlua_rule { };
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100176
177#endif /* USE_LUA */
178
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +0100179#endif /* _TYPES_HLUA_H */