Skip to content

Commit

Permalink
proxy: add 'valcrc' inspector step
Browse files Browse the repository at this point in the history
Returns the crc32c (iSCSI crc32?) of the value (minus \r\n) of a
request.

Also fixes some tests that I left commented somehow :(
  • Loading branch information
dormando committed Sep 5, 2024
1 parent b8b872a commit 7081f97
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions proto_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ void *proxy_init(bool use_uring, bool proxy_memprofile) {
proxy_ctx_t *ctx = calloc(1, sizeof(proxy_ctx_t));
ctx->use_uring = use_uring;
ctx->memprofile = proxy_memprofile;
crc32c_init();

pthread_mutex_init(&ctx->config_lock, NULL);
pthread_cond_init(&ctx->config_cond, NULL);
Expand Down
2 changes: 2 additions & 0 deletions proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define XXH_INLINE_ALL // modifier for xxh3's include below
#include "xxhash.h"

#include "crc32c.h"

#ifdef PROXY_DEBUG
#define P_DEBUG(...) \
do { \
Expand Down
27 changes: 27 additions & 0 deletions proxy_inspector.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum mcp_ins_steptype {
mcp_ins_step_flagtoken,
mcp_ins_step_flagint,
mcp_ins_step_flagis,
mcp_ins_step_valcrc,
mcp_ins_step_final, // not used.
};

Expand Down Expand Up @@ -473,6 +474,30 @@ static int mcp_inspector_flagis_r(lua_State *L, struct mcp_inspector *ins, struc
return 2;
}

static int mcp_inspector_valcrc_c(lua_State *L, int tidx) {
return 0;
}

static int mcp_inspector_valcrc_i(lua_State *L, int tidx, int sc, struct mcp_inspector *ins) {
return 0;
}

static int mcp_inspector_valcrc_r(lua_State *L, struct mcp_inspector *ins, struct mcp_ins_step *s, void *arg) {
if (ins->type == INS_REQ) {
mcp_request_t *rq = arg;
if (rq->pr.vlen > 2 && rq->pr.vbuf) {
// don't skip the "\r\n"
uint32_t crc = crc32c(0, rq->pr.vbuf, rq->pr.vlen-2);
lua_pushinteger(L, crc);
} else {
lua_pushnil(L);
}
} else {
lua_pushnil(L);
}
return 1;
}

// END STEPS

typedef int (*mcp_ins_c)(lua_State *L, int tidx);
Expand All @@ -497,6 +522,7 @@ static const struct mcp_ins_entry mcp_ins_entries[] = {
[mcp_ins_step_flagtoken] = {"flagtoken", mcp_inspector_flag_c_g, mcp_inspector_flag_i_g, mcp_inspector_flagtoken_r, INS_REQ|INS_RES, 2},
[mcp_ins_step_flagint] = {"flagint", mcp_inspector_flag_c_g, mcp_inspector_flag_i_g, mcp_inspector_flagint_r, INS_REQ|INS_RES, 2},
[mcp_ins_step_flagis] = {"flagis", mcp_inspector_flagstr_c, mcp_inspector_flagstr_i, mcp_inspector_flagis_r, INS_REQ|INS_RES, 2},
[mcp_ins_step_valcrc] = {"valcrc", mcp_inspector_valcrc_c, mcp_inspector_valcrc_i, mcp_inspector_valcrc_r, INS_REQ, 1},
};

// call with type string on top
Expand Down Expand Up @@ -639,6 +665,7 @@ int mcplib_inspector_gc(lua_State *L) {
case mcp_ins_step_flagtoken:
case mcp_ins_step_flagint:
case mcp_ins_step_flagis:
case mcp_ins_step_valcrc:
case mcp_ins_step_none:
case mcp_ins_step_final:
break;
Expand Down
30 changes: 30 additions & 0 deletions t/proxyins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,41 @@ function mcp_config_routes(p)
end
})

local msvalcrc_ins = mcp.req_inspector_new(
{ t = "valcrc" }
)

local msfg = mcp.funcgen_new()
msfg:ready({
n = "msfg", f = function(rctx)
return function(r)
-- only one test right now.
local crc = msvalcrc_ins(r)
return string.format("SERVER_ERROR crc[%q]\r\n", crc)
end
end
})

local msint = mcp.funcgen_new()
msint:ready({
n = "msint", f = function(rctx)
return function(r)
return mcp.internal(r)
end
end
})

local mgr = mcp.router_new({ map = {
sepkey = mgsepkey,
reshasf = mgreshasf,
reqkey = mgreqkey,
intres = mgintres,
}})

local msr = mcp.router_new({ map = {
all = msfg,
intres = msint,
}})
mcp.attach(mcp.CMD_MG, mgr)
mcp.attach(mcp.CMD_MS, msr)
end
13 changes: 11 additions & 2 deletions t/proxyins.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ print $w "watch proxyevents\r\n";
is(<$w>, "OK\r\n");

{
test_msreq();
test_mgintres();
#test_mgreq();
#test_mgres();
test_mgreq();
test_mgres();
}

sub test_msreq {
note 'testing ms req';
subtest 'valcrc' => sub {
$t->c_send("ms all/valcrc 5\r\nhello\r\n");
$t->c_recv("SERVER_ERROR crc[2591144780]\r\n");
}
}

sub test_mgintres {
Expand Down

0 comments on commit 7081f97

Please sign in to comment.