Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 1 | #ifndef _TYPES_HLUA_H |
| 2 | #define _TYPES_HLUA_H |
| 3 | |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 4 | #ifdef USE_LUA |
| 5 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 6 | #include <lua.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7 | #include <lauxlib.h> |
| 8 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 9 | #include <common/regex.h> |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 10 | #include <common/xref.h> |
| 11 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 12 | #include <types/http_ana.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 13 | #include <types/proxy.h> |
| 14 | #include <types/server.h> |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 15 | #include <types/stick_table.h> |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 16 | |
Thierry FOURNIER | 0efc94c | 2015-09-16 21:22:28 +0200 | [diff] [blame] | 17 | #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 FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 25 | #define CLASS_APPLET_TCP "AppletTCP" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 26 | #define CLASS_APPLET_HTTP "AppletHTTP" |
Thierry Fournier | f61aa63 | 2016-02-19 20:56:00 +0100 | [diff] [blame] | 27 | #define CLASS_PROXY "Proxy" |
Thierry Fournier | f2fdc9d | 2016-02-22 08:21:39 +0100 | [diff] [blame] | 28 | #define CLASS_SERVER "Server" |
Thierry Fournier | ff48042 | 2016-02-25 08:36:46 +0100 | [diff] [blame] | 29 | #define CLASS_LISTENER "Listener" |
Thierry FOURNIER | 3190427 | 2017-10-25 12:59:51 +0200 | [diff] [blame] | 30 | #define CLASS_REGEX "Regex" |
Adis Nezirovic | 8878f8e | 2018-07-13 12:18:33 +0200 | [diff] [blame] | 31 | #define CLASS_STKTABLE "StickTable" |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 32 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 33 | struct stream; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 34 | |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 35 | #define HLUA_RUN 0x00000001 |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 36 | #define HLUA_CTRLYIELD 0x00000002 |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 37 | #define HLUA_WAKERESWR 0x00000004 |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 38 | #define HLUA_WAKEREQWR 0x00000008 |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 39 | #define HLUA_EXIT 0x00000010 |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 40 | /* unused: 0x00000020 */ |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 41 | #define HLUA_STOP 0x00000040 |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 42 | |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 43 | #define HLUA_F_AS_STRING 0x01 |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 44 | #define HLUA_F_MAY_USE_HTTP 0x02 |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 45 | |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 46 | #define HLUA_TXN_NOTERM 0x00000001 |
| 47 | #define HLUA_TXN_HTTP_RDY 0x00000002 /* Set if the txn is HTTP ready for the defined direction */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 48 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 49 | #define HLUA_CONCAT_BLOCSZ 2048 |
| 50 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 51 | enum hlua_exec { |
| 52 | HLUA_E_OK = 0, |
| 53 | HLUA_E_AGAIN, /* LUA yield, must resume the stack execution later, when |
| 54 | the associatedtask is waked. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 55 | HLUA_E_ETMOUT, /* Execution timeout */ |
| 56 | HLUA_E_NOMEM, /* Out of memory error */ |
| 57 | HLUA_E_YIELD, /* LUA code try to yield, and this is not allowed */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 58 | HLUA_E_ERRMSG, /* LUA stack execution failed with a string error message |
| 59 | in the top of stack. */ |
| 60 | HLUA_E_ERR, /* LUA stack execution failed without error message. */ |
| 61 | }; |
| 62 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 63 | /* This struct is use for storing HAProxy parsers state |
| 64 | * before executing some Lua code. The goal is we can |
| 65 | * check and compare the parser state a the end of Lua |
| 66 | * execution. If the state is changed by Lua towards |
| 67 | * an unexpected state, we can abort the transaction. |
| 68 | */ |
| 69 | struct hlua_consistency { |
| 70 | enum pr_mode mode; |
| 71 | union { |
| 72 | struct { |
| 73 | int dir; |
Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame] | 74 | enum h1_state state; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 75 | } http; |
| 76 | } data; |
| 77 | }; |
| 78 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 79 | struct hlua { |
| 80 | lua_State *T; /* The LUA stack. */ |
| 81 | int Tref; /* The reference of the stack in coroutine case. |
| 82 | -1 for the main lua stack. */ |
| 83 | int Mref; /* The reference of the memory context in coroutine case. |
| 84 | -1 if the memory context is not used. */ |
| 85 | int nargs; /* The number of arguments in the stack at the start of execution. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 86 | unsigned int flags; /* The current execution flags. */ |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 87 | int wake_time; /* The lua wants to be waked at this time, or before. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 88 | unsigned int max_time; /* The max amount of execution time for an Lua process, in ms. */ |
| 89 | unsigned int start_time; /* The ms time when the Lua starts the last execution. */ |
| 90 | unsigned int run_time; /* Lua total execution time in ms. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 91 | struct task *task; /* The task associated with the lua stack execution. |
| 92 | We must wake this task to continue the task execution */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 93 | struct list com; /* The list head of the signals attached to this task. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 94 | struct ebpt_node node; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 95 | struct hlua_consistency cons; /* Store data consistency check. */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 96 | int gc_count; /* number of items which need a GC */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 99 | /* This is a part of the list containing references to functions |
| 100 | * called at the initialisation time. |
| 101 | */ |
| 102 | struct hlua_init_function { |
| 103 | struct list l; |
| 104 | int function_ref; |
| 105 | }; |
| 106 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 107 | /* This struct contains the lua data used to bind |
| 108 | * Lua function on HAProxy hook like sample-fetches |
| 109 | * or actions. |
| 110 | */ |
| 111 | struct hlua_function { |
| 112 | char *name; |
| 113 | int function_ref; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 114 | int nargs; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 117 | /* This struct is used with the structs: |
| 118 | * - http_req_rule |
| 119 | * - http_res_rule |
| 120 | * - tcp_rule |
| 121 | * It contains the lua execution configuration. |
| 122 | */ |
| 123 | struct hlua_rule { |
| 124 | struct hlua_function fcn; |
| 125 | char **args; |
| 126 | }; |
| 127 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 128 | /* This struct contains the pointer provided on the most |
| 129 | * of internal HAProxy calls during the processing of |
| 130 | * rules, converters and sample-fetches. This struct is |
| 131 | * associated with the lua object called "TXN". |
| 132 | */ |
| 133 | struct hlua_txn { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 134 | struct stream *s; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 135 | struct proxy *p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 136 | int dir; /* SMP_OPT_DIR_{REQ,RES} */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 137 | int flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 140 | /* This struct contains the applet context. */ |
| 141 | struct hlua_appctx { |
| 142 | struct appctx *appctx; |
| 143 | luaL_Buffer b; /* buffer used to prepare strings. */ |
| 144 | struct hlua_txn htxn; |
| 145 | }; |
| 146 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 147 | /* This struc is used with sample fetches and sample converters. */ |
| 148 | struct hlua_smp { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 149 | struct stream *s; |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 150 | struct proxy *p; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 151 | unsigned int flags; /* LUA_F_OPT_* */ |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 152 | int dir; /* SMP_OPT_DIR_{REQ,RES} */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 153 | }; |
| 154 | |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 155 | /* This struct contains data used with sleep functions. */ |
| 156 | struct hlua_sleep { |
| 157 | struct task *task; /* task associated with sleep. */ |
| 158 | struct list com; /* list of signal to wake at the end of sleep. */ |
| 159 | unsigned int wakeup_ms; /* hour to wakeup. */ |
| 160 | }; |
| 161 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 162 | /* This struct is used to create coprocess doing TCP or |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 163 | * SSL I/O. It uses a fake stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 164 | */ |
| 165 | struct hlua_socket { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 166 | struct xref xref; /* cross reference with the stream used for socket I/O. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 167 | luaL_Buffer b; /* buffer used to prepare strings. */ |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 168 | unsigned long tid; /* Store the thread id which creates the socket. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 169 | }; |
| 170 | |
Thierry Fournier | 49d4842 | 2016-02-19 12:09:29 +0100 | [diff] [blame] | 171 | struct hlua_concat { |
| 172 | int size; |
| 173 | int len; |
| 174 | }; |
| 175 | |
Thierry FOURNIER / OZON.IO | 62fec75 | 2016-11-10 20:38:11 +0100 | [diff] [blame] | 176 | struct hlua_addr { |
| 177 | union { |
| 178 | struct { |
| 179 | struct in_addr ip; |
| 180 | struct in_addr mask; |
| 181 | } v4; |
| 182 | struct { |
| 183 | struct in6_addr ip; |
| 184 | struct in6_addr mask; |
| 185 | } v6; |
| 186 | } addr; |
| 187 | int type; |
| 188 | }; |
| 189 | |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 190 | #else /* USE_LUA */ |
| 191 | |
| 192 | /* Empty struct for compilation compatibility */ |
| 193 | struct hlua { }; |
| 194 | struct hlua_socket { }; |
Thierry FOURNIER | 231ef1d | 2015-07-30 19:03:55 +0200 | [diff] [blame] | 195 | struct hlua_rule { }; |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 196 | |
| 197 | #endif /* USE_LUA */ |
| 198 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 199 | #endif /* _TYPES_HLUA_H */ |