suricata
util-luajit.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  */
24 
25 #include "suricata-common.h"
26 
27 #ifdef HAVE_LUAJIT
28 #include "conf.h"
29 #include "util-pool.h"
30 #include "util-lua.h"
31 
32 /** \brief lua_State pool
33  *
34  * Lua requires states to be alloc'd in memory <2GB. For this reason we
35  * prealloc the states early during engine startup so we have a better chance
36  * of getting the states. We protect the pool with a lock as the detect
37  * threads access it during their init and cleanup.
38  *
39  * Pool size is automagically determined based on number of keyword occurences,
40  * cpus/cores and rule reloads being enabled or not.
41  *
42  * Alternatively, the "detect-engine.luajit-states" var can be set.
43  */
44 static Pool *luajit_states = NULL;
45 static pthread_mutex_t luajit_states_lock = SCMUTEX_INITIALIZER;
46 static int luajit_states_cnt = 0;
47 static int luajit_states_cnt_max = 0;
48 static int luajit_states_size = 0;
49 #define LUAJIT_DEFAULT_STATES 128
50 
51 static void *LuaStatePoolAlloc(void)
52 {
53  return luaL_newstate();
54 }
55 
56 static void LuaStatePoolFree(void *d)
57 {
58  lua_State *s = (lua_State *)d;
59  if (s != NULL)
60  lua_close(s);
61 }
62 
63 /** \brief Populate lua states pool
64  *
65  * \param num keyword instances
66  * \param reloads bool indicating we have rule reloads enabled
67  */
68 int LuajitSetupStatesPool(void)
69 {
70  int retval = 0;
71  pthread_mutex_lock(&luajit_states_lock);
72 
73  if (luajit_states == NULL) {
74  intmax_t cnt = 0;
75  if (ConfGetInt("luajit.states", &cnt) != 1) {
76  ConfNode *denode = NULL;
77  ConfNode *decnf = ConfGetNode("detect-engine");
78  if (decnf != NULL) {
79  TAILQ_FOREACH(denode, &decnf->head, next) {
80  if (denode->val && strcmp(denode->val, "luajit-states") == 0) {
81  ConfGetChildValueInt(denode, "luajit-states", &cnt);
82  }
83  }
84  }
85  }
86  if (cnt == 0) {
87  cnt = LUAJIT_DEFAULT_STATES;
88  }
89  luajit_states_size = cnt;
90 
91  luajit_states = PoolInit(0, cnt, 0, LuaStatePoolAlloc, NULL, NULL, NULL, LuaStatePoolFree);
92  if (luajit_states == NULL) {
93  SCLogError(SC_ERR_LUA_ERROR, "luastate pool init failed, lua/luajit keywords won't work");
94  retval = -1;
95  }
96 
97  if (retval == 0) {
98  SCLogConfig("luajit states preallocated: %d", luajit_states_size);
99  }
100  }
101 
102  pthread_mutex_unlock(&luajit_states_lock);
103  return retval;
104 }
105 
106 void LuajitFreeStatesPool(void)
107 {
108  pthread_mutex_lock(&luajit_states_lock);
109  if (luajit_states_cnt_max > luajit_states_size) {
110  SCLogNotice("luajit states used %d is bigger than pool size %d. Set "
111  "luajit.states to %d to avoid memory issues. "
112  "See #1577 and #1955.", luajit_states_cnt_max, luajit_states_size,
113  luajit_states_cnt_max);
114  }
115  PoolFree(luajit_states);
116  luajit_states = NULL;
117  luajit_states_size = 0;
118  luajit_states_cnt = 0;
119  pthread_mutex_unlock(&luajit_states_lock);
120 }
121 
122 lua_State *LuajitGetState(void)
123 {
124  lua_State *s = NULL;
125  pthread_mutex_lock(&luajit_states_lock);
126  if (luajit_states != NULL) {
127  s = (lua_State *)PoolGet(luajit_states);
128  if (s != NULL) {
129  if (luajit_states_cnt == luajit_states_size) {
130  SCLogWarning(SC_WARN_LUA_SCRIPT, "luajit states pool size %d "
131  "reached. Increase luajit.states config option. "
132  "See #1577 and #1955", luajit_states_size);
133  }
134 
135  luajit_states_cnt++;
136  if (luajit_states_cnt > luajit_states_cnt_max)
137  luajit_states_cnt_max = luajit_states_cnt;
138  }
139  }
140  pthread_mutex_unlock(&luajit_states_lock);
141  return s;
142 }
143 
144 void LuajitReturnState(lua_State *s)
145 {
146  if (s != NULL) {
147  pthread_mutex_lock(&luajit_states_lock);
148  PoolReturn(luajit_states, (void *)s);
149  BUG_ON(luajit_states_cnt <= 0);
150  luajit_states_cnt--;
151  pthread_mutex_unlock(&luajit_states_lock);
152  }
153 }
154 
155 #endif /* HAVE_LUAJIT */
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
#define BUG_ON(x)
char * val
Definition: conf.h:34
#define SCMUTEX_INITIALIZER
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void PoolReturn(Pool *p, void *data)
Definition: util-pool.c:333
void PoolFree(Pool *p)
Definition: util-pool.c:220
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
Definition: conf.h:32
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:269
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:437
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:176
void * PoolGet(Pool *p)
Definition: util-pool.c:275
int ConfGetChildValueInt(const ConfNode *base, const char *name, intmax_t *val)
Definition: conf.c:469
Pool * PoolInit(uint32_t size, uint32_t prealloc_size, uint32_t elt_size, void *(*Alloc)(void), int(*Init)(void *, void *), void *InitData, void(*Cleanup)(void *), void(*Free)(void *))
Init a Pool.
Definition: util-pool.c:84
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.