On Mon, Aug 20, 2018 at 02:11:13PM +0200, Adis Nezirovic wrote:
> Hi guys,
>
> I've attached a patch to add stick table support to Lua. Operations are
> mostly similar to "show table" functionality from admin socket, namely:
>
> - Show basic table info
> - Key lookup
> - Table dump, optionally using data/column filter
>
> One side note, the code provides support for multiple filters
> (4 by default) while CLI doesn't support that, nor it complains about
> multiple "<data.type> <op> <val>" clauses.
>
> Also, if this patch is accepted, maybe we can use provided helper
> functions in other places in the code.
>

It's always funny to reply to self, right sending email to public I've
spotted a bug. New patch attached.

SMT_T_SINT should be treated as ordinary signed integer, and shoud use
lua_pushinteger() on it?

On many places in the code it is noted as "64" bit integer, but in
stick_table.c it is defined as 32bit integer:

struct stktable_type stktable_types[SMP_TYPES] = {
        [SMP_T_SINT] = { "integer", 0,                     4 },


Best regards,
Adis
>From 4c699b0d57ba5d6d5463595a4bdaa2f2ae8c2c56 Mon Sep 17 00:00:00 2001
From: Adis Nezirovic <aneziro...@haproxy.com>
Date: Fri, 13 Jul 2018 12:18:33 +0200
Subject: [PATCH] MEDIUM: lua: Add stick table support for Lua (read-only ops).

---
 doc/lua-api/index.rst |  46 +++++
 include/types/hlua.h  |   1 +
 src/hlua_fcn.c        | 401 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 448 insertions(+)

diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst
index 0c79766e..63848661 100644
--- a/doc/lua-api/index.rst
+++ b/doc/lua-api/index.rst
@@ -852,6 +852,10 @@ Proxy class
   Contain a table with the attached servers. The table is indexed by server
   name, and each server entry is an object of type :ref:`server_class`.
 
+.. js:attribute:: Proxy.stktable
+
+  Contains a stick table object attached to the proxy.
+
 .. js:attribute:: Proxy.listeners
 
   Contain a table with the attached listeners. The table is indexed by listener
@@ -2489,6 +2493,48 @@ AppletTCP class
   :see: :js:func:`AppletTCP.unset_var`
   :see: :js:func:`AppletTCP.set_var`
 
+StickTable class
+================
+
+.. js:class:: StickTable
+
+  This class is used with applets that requires the 'tcp' mode. The tcp applet
+  can be registered with the *core.register_service()* function. They are used
+  for processing a tcp stream like a server in back of HAProxy.
+
+.. js:function:: StickTable.info()
+
+  Returns relevant stick table attributes: type, length, size, used, nopurge,
+  expire and exact interval values for frequency data columns.
+
+  :returns: Lua table
+
+.. js:function:: StickTable.lookup(key)
+
+   Returns stick table entry for given <key>
+
+   :param string key: Stick table key (IP addresses and strings are supported)
+   :returns: Lua table
+
+.. js:function:: StickTable.dump([filter])
+
+   Returns all entries in stick table. An optional filter can be used
+   to extract entries with specific data values. Filter is a table with valid
+   comparison operators as keys followed by data type name and value pairs.
+   e.g.
+
+   :param table filter: Stick table filter
+   :returns: Stick table entries (table)
+
+.. code-block:: lua
+
+    local filter = {
+      lt={{"gpc0", 1}, {"gpc1", 2}},
+      gt={{"conn_rate", 3}},
+      eq={{"conn_cur", 4}}
+    }
+
+
 External Lua libraries
 ======================
 
diff --git a/include/types/hlua.h b/include/types/hlua.h
index 5a8173f3..2e453351 100644
--- a/include/types/hlua.h
+++ b/include/types/hlua.h
@@ -25,6 +25,7 @@
 #define CLASS_SERVER       "Server"
 #define CLASS_LISTENER     "Listener"
 #define CLASS_REGEX        "Regex"
+#define CLASS_STKTABLE     "StickTable"
 
 struct stream;
 
diff --git a/src/hlua_fcn.c b/src/hlua_fcn.c
index cebce224..f38b9f37 100644
--- a/src/hlua_fcn.c
+++ b/src/hlua_fcn.c
@@ -30,6 +30,7 @@
 #include <proto/proxy.h>
 #include <proto/server.h>
 #include <proto/stats.h>
+#include <proto/stick_table.h>
 
 /* Contains the class reference of the concat object. */
 static int class_concat_ref;
@@ -37,7 +38,9 @@ static int class_proxy_ref;
 static int class_server_ref;
 static int class_listener_ref;
 static int class_regex_ref;
+static int class_stktable_ref;
 
+#define MAX_STK_FILTER_LEN 4
 #define STATS_LEN (MAX((int)ST_F_TOTAL_FIELDS, (int)INF_TOTAL_FIELDS))
 
 static THREAD_LOCAL struct field stats[STATS_LEN];
@@ -49,6 +52,38 @@ int hlua_checkboolean(lua_State *L, int index)
        return lua_toboolean(L, index);
 }
 
+/* Helper to push unsigned integers to Lua stack, respecting Lua limitations  
*/
+static int hlua_fcn_pushunsigned(lua_State *L, unsigned int val)
+{
+#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && 
(__WORDSIZE == 64)))
+       lua_pushinteger(L, val);
+#else
+       if (val > INT_MAX)
+               lua_pushnumber(L, (lua_Number)val);
+       else
+               lua_pushinteger(L, (int)val);
+#endif
+       return 1;
+}
+
+/* Helper to push unsigned long long to Lua stack, respecting Lua limitations  
*/
+static int hlua_fcn_pushunsigned_ll(lua_State *L, unsigned long long val) {
+#if (LUA_MAXINTEGER == LLONG_MAX || ((LUA_MAXINTEGER == LONG_MAX) && 
(__WORDSIZE == 64)))
+       /* 64 bits case, U64 is supported until LLONG_MAX */
+       if (val > LLONG_MAX)
+               lua_pushnumber(L, (lua_Number)val);
+       else
+               lua_pushinteger(L, val);
+#else
+       /* 32 bits case, U64 is supported until INT_MAX */
+       if (val > INT_MAX)
+               lua_pushnumber(L, (lua_Number)val);
+       else
+               lua_pushinteger(L, (int)val);
+#endif
+       return 1;
+}
+
 /* This function gets a struct field and convert it in Lua
  * variable. The variable is pushed at the top of the stak.
  */
@@ -446,6 +481,356 @@ static int hlua_concat_init(lua_State *L)
        return 1;
 }
 
+int hlua_fcn_new_stktable(lua_State *L, struct stktable *tbl)
+{
+       lua_newtable(L);
+
+       /* Pop a class stktbl metatable and affect it to the userdata. */
+       lua_rawgeti(L, LUA_REGISTRYINDEX, class_stktable_ref);
+       lua_setmetatable(L, -2);
+
+       lua_pushlightuserdata(L, tbl);
+       lua_rawseti(L, -2, 0);
+       return 1;
+}
+
+static struct stktable *hlua_check_stktable(lua_State *L, int ud)
+{
+       return hlua_checkudata(L, ud, class_stktable_ref);
+}
+
+/* Extract stick table attributes into Lua table */
+int hlua_stktable_info(lua_State *L)
+{
+       struct stktable *tbl;
+       int dt;
+
+       tbl = hlua_check_stktable(L, 1);
+
+       if (!tbl->id) {
+               lua_pushnil(L);
+               return 1;
+        }
+
+       lua_newtable(L);
+
+       lua_pushstring(L, "type");
+       lua_pushstring(L, stktable_types[tbl->type].kw);
+       lua_settable(L, -3);
+
+       lua_pushstring(L, "length");
+       lua_pushinteger(L, tbl->key_size);
+       lua_settable(L, -3);
+
+       lua_pushstring(L, "size");
+       hlua_fcn_pushunsigned(L, tbl->size);
+       lua_settable(L, -3);
+
+       lua_pushstring(L, "used");
+       hlua_fcn_pushunsigned(L, tbl->current);
+       lua_settable(L, -3);
+
+       lua_pushstring(L, "nopurge");
+       lua_pushboolean(L, tbl->nopurge > 0);
+       lua_settable(L, -3);
+
+       lua_pushstring(L, "expire");
+       lua_pushinteger(L, tbl->expire);
+       lua_settable(L, -3);
+
+       /* Save data types periods (if applicable) in 'data' table */
+       lua_pushstring(L, "data");
+       lua_newtable(L);
+
+       for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
+               if (tbl->data_ofs[dt] == 0)
+                       continue;
+
+               lua_pushstring(L, stktable_data_types[dt].name);
+
+               if (stktable_data_types[dt].arg_type == ARG_T_DELAY)
+                       lua_pushinteger(L, tbl->data_arg[dt].u);
+               else
+                       lua_pushinteger(L, -1);
+
+               lua_settable(L, -3);
+       }
+
+       lua_settable(L, -3);
+
+       return 1;
+}
+
+/* Helper to get extract stick table entry into Lua table */
+static void hlua_stktable_entry(lua_State *L, struct stktable *t, struct 
stksess *ts)
+{
+       int dt;
+       void *ptr;
+
+       for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) {
+
+               if (t->data_ofs[dt] == 0)
+                       continue;
+
+               lua_pushstring(L, stktable_data_types[dt].name);
+
+               ptr = stktable_data_ptr(t, ts, dt);
+               switch (stktable_data_types[dt].std_type) {
+               case STD_T_SINT:
+                       lua_pushinteger(L, stktable_data_cast(ptr, std_t_sint));
+                       break;
+               case STD_T_UINT:
+                       hlua_fcn_pushunsigned(L, stktable_data_cast(ptr, 
std_t_uint));
+                       break;
+               case STD_T_ULL:
+                       hlua_fcn_pushunsigned_ll(L, stktable_data_cast(ptr, 
std_t_ull));
+                       break;
+               case STD_T_FRQP:
+                       lua_pushinteger(L, 
read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
+                                       t->data_arg[dt].u));
+                       break;
+               }
+
+               lua_settable(L, -3);
+       }
+}
+
+/* Looks in table <t> for a sticky session matching key <key>
+ * Returns table with session data or nil
+ *
+ * The returned table always contains 'use' and 'expire' (integer) fields.
+ * For frequency/rate counters, each data entry is returned as table with
+ * 'value' and 'period' fields.
+ */
+int hlua_stktable_lookup(lua_State *L)
+{
+       struct stktable *t;
+       const char *key;
+       struct sample smp;
+       struct stktable_key *skey;
+       struct stksess *ts;
+
+       t = hlua_check_stktable(L, 1);
+       key = luaL_checkstring(L, 2);
+
+       smp.data.type = SMP_T_STR;
+       smp.flags |= SMP_F_CONST;
+       smp.data.u.str.area = (char *)key;
+
+       skey = smp_to_stkey(&smp, t);
+       if (!skey) {
+               lua_pushnil(L);
+               return 1;
+       }
+
+       ts = stktable_lookup_key(t, skey);
+       if (!ts) {
+               lua_pushnil(L);
+               return 1;
+       }
+
+       lua_newtable(L);
+       lua_pushstring(L, "use");
+       lua_pushinteger(L, ts->ref_cnt - 1);
+       lua_settable(L, -3);
+
+       lua_pushstring(L, "expire");
+       lua_pushinteger(L, tick_remain(now_ms, ts->expire));
+       lua_settable(L, -3);
+
+       hlua_stktable_entry(L, t, ts);
+       ts->ref_cnt--;
+
+       return 1;
+}
+
+struct stk_filter {
+       long long val;
+       int type;
+       int op;
+};
+
+
+static int hlua_error(lua_State *L, const char *msg)  {
+       lua_pushnil(L);
+       lua_pushstring(L, msg);
+       return 2;
+}
+
+/* Dump the contents of stick table <t>*/
+int hlua_stktable_dump(lua_State *L)
+{
+       struct stktable *t;
+       struct ebmb_node *eb;
+       struct ebmb_node *n;
+       struct stksess *ts;
+       int type;
+       int op;
+       int dt;
+       long long val;
+       struct stk_filter filter[MAX_STK_FILTER_LEN];
+       int filter_count = 0;
+       int i;
+       int skip_entry;
+       void *ptr;
+
+       t = hlua_check_stktable(L, 1);
+       type = lua_type(L, 2);
+
+       switch (type) {
+       case LUA_TNONE:
+       case LUA_TNIL:
+               break;
+       case LUA_TTABLE:
+               lua_pushnil(L);
+               while (lua_next(L, 2) != 0) {
+                       if (filter_count >= sizeof(filter)/sizeof(struct 
stk_filter)) {
+                               break;
+                       }
+                       if (lua_type(L, -2) != LUA_TSTRING) {
+                               return hlua_error(L, "String key (operator 
name) expected");
+                       }
+
+                       op = get_std_op(lua_tostring(L, -2));
+                       if (op < 0) {
+                               return hlua_error(L, "Unknown stick table/acl 
operator");
+                       }
+
+                       if (lua_type(L, -1) != LUA_TTABLE) {
+                               return hlua_error(L, "Datatype/value table 
expected");
+                       }
+
+                       lua_pushnil(L);
+                       while (lua_next(L, -2) != 0) {
+                               int entry_idx = 0;
+                               if (filter_count >= 
sizeof(filter)/sizeof(struct stk_filter)) {
+                                       break;
+                               }
+                               filter[filter_count].op = op;
+                               if (lua_type(L, -1) != LUA_TTABLE) {
+                                       return hlua_error(L, "Filter table must 
be multidimensional");
+                               }
+
+                               if (lua_rawlen(L, -1) != 2) {
+                                       return hlua_error(L, "Filter table 
entry length must be 2");
+                               }
+
+                               lua_pushnil(L);
+                               while (lua_next(L, -2) != 0) {
+                                       if (entry_idx == 0) {
+                                               if (lua_type(L, -1) != 
LUA_TSTRING) {
+                                                       return hlua_error(L, 
"Stick table data type must be string");
+                                               }
+                                               dt = 
stktable_get_data_type((char *)lua_tostring(L, -1));
+
+                                               if (t->data_ofs[dt] == 0) {
+                                                       return hlua_error(L, 
"Stick table filter column not present in table");
+
+                                               }
+
+                                               filter[filter_count].type = dt;
+                                       } else {
+                                               val = lua_tointeger(L, -1);
+                                               filter[filter_count].val = val;
+                                               filter_count++;
+                                       }
+                                       entry_idx++;
+                                       lua_pop(L, 1);
+                               }
+                               lua_pop(L, 1);
+                       }
+                       lua_pop(L, 1);
+               }
+
+               break;
+       default:
+               return hlua_error(L, "filter table expected");
+       }
+
+       lua_newtable(L);
+
+       HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
+       eb = ebmb_first(&t->keys);
+       for (n = eb; n; n = ebmb_next(n))
+       {
+               ts = ebmb_entry(n, struct stksess, key);
+               if (!ts) {
+                       HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
+                       return 1;
+               }
+               ts->ref_cnt++;
+               HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
+
+               /* multi condition/value filter */
+               skip_entry = 0;
+               for (i = 0; i < filter_count; i++) {
+                       if (t->data_ofs[filter[i].type] == 0)
+                               continue;
+
+                       ptr = stktable_data_ptr(t, ts, filter[i].type);
+
+                       switch (stktable_data_types[filter[i].type].std_type) {
+                       case STD_T_SINT:
+                               val = stktable_data_cast(ptr, std_t_sint);
+                               break;
+                       case STD_T_UINT:
+                               val = stktable_data_cast(ptr, std_t_uint);
+                               break;
+                       case STD_T_ULL:
+                               val = stktable_data_cast(ptr, std_t_ull);
+                               break;
+                       case STD_T_FRQP:
+                               val = 
read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
+                                                          
t->data_arg[filter[i].type].u);
+                               break;
+                       default:
+                               continue;
+                               break;
+                       }
+
+                       op = filter[i].op;
+
+                       if ((val < filter[i].val && (op == STD_OP_EQ || op == 
STD_OP_GT || op == STD_OP_GE)) ||
+                           (val == filter[i].val && (op == STD_OP_NE || op == 
STD_OP_GT || op == STD_OP_LT)) ||
+                           (val > filter[i].val && (op == STD_OP_EQ || op == 
STD_OP_LT || op == STD_OP_LE))) {
+                               skip_entry = 1;
+                               break;
+                       }
+               }
+
+               if (skip_entry) {
+                       ts->ref_cnt--;
+                       HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
+                       continue;
+               }
+
+               if (t->type == SMP_T_IPV4) {
+                       char addr[INET_ADDRSTRLEN];
+                       inet_ntop(AF_INET, (const void *)&ts->key.key, addr, 
sizeof(addr));
+                       lua_pushstring(L, addr);
+               } else if (t->type == SMP_T_IPV6) {
+                       char addr[INET6_ADDRSTRLEN];
+                       inet_ntop(AF_INET6, (const void *)&ts->key.key, addr, 
sizeof(addr));
+                       lua_pushstring(L, addr);
+               } else if (t->type == SMP_T_SINT) {
+                       lua_pushinteger(L, *ts->key.key);
+               } else if (t->type == SMP_T_STR) {
+                       lua_pushstring(L, (const char *)ts->key.key);
+               } else {
+                       return hlua_error(L, "Unsupported stick table key 
type");
+               }
+
+               ts->ref_cnt--;
+               lua_newtable(L);
+               hlua_stktable_entry(L, t, ts);
+               lua_settable(L, -3);
+               HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
+       }
+       HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
+
+       return 1;
+}
+
 int hlua_fcn_new_listener(lua_State *L, struct listener *lst)
 {
        lua_newtable(L);
@@ -887,6 +1272,12 @@ int hlua_fcn_new_proxy(lua_State *L, struct proxy *px)
        }
        lua_settable(L, -3);
 
+       if (px->table.id) {
+               lua_pushstring(L, "stktable");
+               hlua_fcn_new_stktable(L, &px->table);
+               lua_settable(L, -3);
+       }
+
        return 1;
 }
 
@@ -1289,6 +1680,16 @@ int hlua_fcn_reg_core_fcn(lua_State *L)
        lua_setmetatable(L, -2);
        lua_setglobal(L, CLASS_REGEX); /* Create global object called Regex */
 
+       /* Create stktable object. */
+       lua_newtable(L);
+       lua_pushstring(L, "__index");
+       lua_newtable(L);
+       hlua_class_function(L, "info", hlua_stktable_info);
+       hlua_class_function(L, "lookup", hlua_stktable_lookup);
+       hlua_class_function(L, "dump", hlua_stktable_dump);
+       lua_settable(L, -3); /* -> META["__index"] = TABLE */
+       class_stktable_ref = hlua_register_metatable(L, CLASS_STKTABLE);
+
        /* Create listener object. */
        lua_newtable(L);
        lua_pushstring(L, "__index");
-- 
2.18.0

Reply via email to