blob: a0a1e26561f2093c2f629b69c3642949cf8db514 [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
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02009#include <common/xref.h>
10
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010011#include <types/proxy.h>
12#include <types/server.h>
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010013
Thierry FOURNIER0efc94c2015-09-16 21:22:28 +020014#define CLASS_CORE "Core"
15#define CLASS_TXN "TXN"
16#define CLASS_FETCHES "Fetches"
17#define CLASS_CONVERTERS "Converters"
18#define CLASS_SOCKET "Socket"
19#define CLASS_CHANNEL "Channel"
20#define CLASS_HTTP "HTTP"
21#define CLASS_MAP "Map"
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +020022#define CLASS_APPLET_TCP "AppletTCP"
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +020023#define CLASS_APPLET_HTTP "AppletHTTP"
Thierry Fournierf61aa632016-02-19 20:56:00 +010024#define CLASS_PROXY "Proxy"
Thierry Fournierf2fdc9d2016-02-22 08:21:39 +010025#define CLASS_SERVER "Server"
Thierry Fournierff480422016-02-25 08:36:46 +010026#define CLASS_LISTENER "Listener"
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010027
Willy Tarreau87b09662015-04-03 00:22:06 +020028struct stream;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +010029
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010030#define HLUA_RUN 0x00000001
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +010031#define HLUA_CTRLYIELD 0x00000002
Thierry FOURNIERef6a2112015-03-05 17:45:34 +010032#define HLUA_WAKERESWR 0x00000004
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +010033#define HLUA_WAKEREQWR 0x00000008
Thierry FOURNIER0a99b892015-08-26 00:14:17 +020034#define HLUA_EXIT 0x00000010
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +020035#define HLUA_MUST_GC 0x00000020
Thierry FOURNIER9bd52d42016-07-14 11:45:33 +020036#define HLUA_STOP 0x00000040
Thierry FOURNIER380d0932015-01-23 14:27:52 +010037
Thierry FOURNIER7fa05492015-12-20 18:42:25 +010038#define HLUA_F_AS_STRING 0x01
Thierry FOURNIERca988662015-12-20 18:43:03 +010039#define HLUA_F_MAY_USE_HTTP 0x02
Thierry FOURNIER7fa05492015-12-20 18:42:25 +010040
Thierry FOURNIERab00df62016-07-14 11:42:37 +020041#define HLUA_TXN_NOTERM 0x00000001
42
Thierry Fournier49d48422016-02-19 12:09:29 +010043#define HLUA_CONCAT_BLOCSZ 2048
44
Thierry FOURNIER380d0932015-01-23 14:27:52 +010045enum hlua_exec {
46 HLUA_E_OK = 0,
47 HLUA_E_AGAIN, /* LUA yield, must resume the stack execution later, when
48 the associatedtask is waked. */
49 HLUA_E_ERRMSG, /* LUA stack execution failed with a string error message
50 in the top of stack. */
51 HLUA_E_ERR, /* LUA stack execution failed without error message. */
52};
53
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +010054/* This struct is use for storing HAProxy parsers state
55 * before executing some Lua code. The goal is we can
56 * check and compare the parser state a the end of Lua
57 * execution. If the state is changed by Lua towards
58 * an unexpected state, we can abort the transaction.
59 */
60struct hlua_consistency {
61 enum pr_mode mode;
62 union {
63 struct {
64 int dir;
65 enum ht_state state;
66 } http;
67 } data;
68};
69
Thierry FOURNIER380d0932015-01-23 14:27:52 +010070struct hlua {
71 lua_State *T; /* The LUA stack. */
72 int Tref; /* The reference of the stack in coroutine case.
73 -1 for the main lua stack. */
74 int Mref; /* The reference of the memory context in coroutine case.
75 -1 if the memory context is not used. */
76 int nargs; /* The number of arguments in the stack at the start of execution. */
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +010077 unsigned int flags; /* The current execution flags. */
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +010078 int wake_time; /* The lua wants to be waked at this time, or before. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +020079 unsigned int max_time; /* The max amount of execution time for an Lua process, in ms. */
80 unsigned int start_time; /* The ms time when the Lua starts the last execution. */
81 unsigned int run_time; /* Lua total execution time in ms. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +010082 struct task *task; /* The task associated with the lua stack execution.
83 We must wake this task to continue the task execution */
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010084 struct list com; /* The list head of the signals attached to this task. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +010085 struct ebpt_node node;
Thierry FOURNIER11cfb3d2016-12-13 13:06:23 +010086 struct hlua_consistency cons; /* Store data consistency check. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +010087};
88
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +010089struct hlua_com {
90 struct list purge_me; /* Part of the list of signals to be purged in the
91 case of the LUA execution stack crash. */
92 struct list wake_me; /* Part of list of signals to be targeted if an
93 event occurs. */
94 struct task *task; /* The task to be wake if an event occurs. */
95};
96
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +010097/* This is a part of the list containing references to functions
98 * called at the initialisation time.
99 */
100struct hlua_init_function {
101 struct list l;
102 int function_ref;
103};
104
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +0100105/* This struct contains the lua data used to bind
106 * Lua function on HAProxy hook like sample-fetches
107 * or actions.
108 */
109struct hlua_function {
110 char *name;
111 int function_ref;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +0100112 int nargs;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +0100113};
114
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +0100115/* This struct is used with the structs:
116 * - http_req_rule
117 * - http_res_rule
118 * - tcp_rule
119 * It contains the lua execution configuration.
120 */
121struct hlua_rule {
122 struct hlua_function fcn;
123 char **args;
124};
125
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100126/* This struct contains the pointer provided on the most
127 * of internal HAProxy calls during the processing of
128 * rules, converters and sample-fetches. This struct is
129 * associated with the lua object called "TXN".
130 */
131struct hlua_txn {
Willy Tarreau87b09662015-04-03 00:22:06 +0200132 struct stream *s;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100133 struct proxy *p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +0100134 int dir; /* SMP_OPT_DIR_{REQ,RES} */
Thierry FOURNIERab00df62016-07-14 11:42:37 +0200135 int flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100136};
137
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200138/* This struct contains the applet context. */
139struct hlua_appctx {
140 struct appctx *appctx;
141 luaL_Buffer b; /* buffer used to prepare strings. */
142 struct hlua_txn htxn;
143};
144
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100145/* This struc is used with sample fetches and sample converters. */
146struct hlua_smp {
Willy Tarreau87b09662015-04-03 00:22:06 +0200147 struct stream *s;
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100148 struct proxy *p;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +0100149 unsigned int flags; /* LUA_F_OPT_* */
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +0100150 int dir; /* SMP_OPT_DIR_{REQ,RES} */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100151};
152
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +0100153/* This struct contains data used with sleep functions. */
154struct hlua_sleep {
155 struct task *task; /* task associated with sleep. */
156 struct list com; /* list of signal to wake at the end of sleep. */
157 unsigned int wakeup_ms; /* hour to wakeup. */
158};
159
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100160/* This struct is used to create coprocess doing TCP or
Willy Tarreau87b09662015-04-03 00:22:06 +0200161 * SSL I/O. It uses a fake stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100162 */
163struct hlua_socket {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +0200164 struct xref xref; /* cross reference with the stream used for socket I/O. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100165 luaL_Buffer b; /* buffer used to prepare strings. */
166};
167
Thierry Fournier49d48422016-02-19 12:09:29 +0100168struct hlua_concat {
169 int size;
170 int len;
171};
172
Thierry FOURNIER / OZON.IO62fec752016-11-10 20:38:11 +0100173struct hlua_addr {
174 union {
175 struct {
176 struct in_addr ip;
177 struct in_addr mask;
178 } v4;
179 struct {
180 struct in6_addr ip;
181 struct in6_addr mask;
182 } v6;
183 } addr;
184 int type;
185};
186
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100187#else /* USE_LUA */
188
189/* Empty struct for compilation compatibility */
190struct hlua { };
191struct hlua_socket { };
Thierry FOURNIER231ef1d2015-07-30 19:03:55 +0200192struct hlua_rule { };
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100193
194#endif /* USE_LUA */
195
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +0100196#endif /* _TYPES_HLUA_H */