Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add linux.percpu api #213

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/lunatik
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function lunatik.dostring(chunk)
end

function lunatik.usage()
print("usage: lunatik [load|unload|reload|status|list] [run|spawn|stop <script>]")
print("usage: lunatik [load|unload|reload|status|list] [run|percpu|spawn|stop <script>]")
os.exit(false)
end

Expand Down Expand Up @@ -111,7 +111,7 @@ local function set(t)
return s
end

local tokens = set{"run", "spawn", "stop", "list"}
local tokens = set{"run", "spawn", "percpu", "stop", "list"}

if #arg >= 1 then
local token = arg[1]
Expand Down
14 changes: 14 additions & 0 deletions lib/lualinux.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ static int lualinux_ifindex(lua_State *L)
return 1;
}

static int lualinux_percpu(lua_State *L)
{
int cpu;

luaL_checktype(L, 1, LUA_TFUNCTION);
for_each_present_cpu(cpu) {
lua_pushvalue(L, 1);
lua_pushinteger(L, cpu);
lua_call(L, 1, 1);
}
return 0;
}

#define LUALINUX_NEW_BYTESWAPPER(func, T) \
static int lualinux_##func(lua_State *L) \
{ \
Expand Down Expand Up @@ -226,6 +239,7 @@ static const luaL_Reg lualinux_lib[] = {
{"difftime", lualinux_difftime},
{"lookup", lualinux_lookup},
{"ifindex", lualinux_ifindex},
{"percpu", lualinux_percpu},
{"ntoh16", lualinux_be16_to_cpu},
{"ntoh32", lualinux_be32_to_cpu},
{"hton16", lualinux_cpu_to_be16},
Expand Down
18 changes: 18 additions & 0 deletions lib/luaxdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,32 @@ static int luaxdp_handler(lua_State *L, struct xdp_buff *ctx, void *arg, size_t
return action;
}

#define LUAXDP_SCRIPT_LEN 128
#define LUAXDP_SUFFIX_LEN 4
#define LUAXDP_KEY_LEN (LUAXDP_SCRIPT_LEN + LUAXDP_SUFFIX_LEN)

__bpf_kfunc int bpf_luaxdp_run(char *key, size_t key__sz, struct xdp_md *xdp_ctx, void *arg, size_t arg__sz)
{
char buf[LUAXDP_KEY_LEN];
lunatik_object_t *runtime;
struct xdp_buff *ctx = (struct xdp_buff *)xdp_ctx;
unsigned int cpuid = smp_processor_id();
int action = -1;
size_t keylen = key__sz - 1;

key[keylen] = '\0';

if (key__sz + LUAXDP_SUFFIX_LEN > LUAXDP_KEY_LEN) {
pr_err("runtime name is too long");
goto out;
}

keylen = snprintf(buf, LUAXDP_KEY_LEN, "%s:%d", key, cpuid);
key = buf;

if (keylen > LUAXDP_KEY_LEN)
keylen = LUAXDP_KEY_LEN;

if ((runtime = luarcu_gettable(luaxdp_runtimes, key, keylen)) == NULL) {
pr_err("couldn't find runtime '%s'\n", key);
goto out;
Expand Down
41 changes: 34 additions & 7 deletions lib/lunatik/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
--

local lunatik = require("lunatik")
local linux = require("linux")
local thread = require("thread")
local rcu = require("rcu")

Expand All @@ -15,16 +16,36 @@ local function trim(script) -- drop ".lua" file extension
return script:gsub("(%w+).lua", "%1")
end

function runner.run(script, ...)
local script = trim(script)
if env.runtimes[script] then
local function runtime_name(script, cpu)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it a bit misnomer.. not sure if it would be cleaner to use script .. ":" .. cpu directly in place..

return script .. ":" .. cpu
end

local function run(name, script, ...)
if env.runtimes[name] then
error(string.format("%s is already running", script))
end
local runtime = lunatik.runtime(script, ...)
env.runtimes[script] = runtime
env.runtimes[name] = runtime
return runtime
end

function runner.run(script, ...)
local script = trim(script)
local name = runtime_name(script, 0)
return run(name, script, ...)
end

function runner.percpu(script, ...)
local script = trim(script)
local runtimes = {}
local args = {...}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this, right? couldn't we just pass ... along?

linux.percpu(function (cpu)
local name = runtime_name(script, cpu)
runtimes[name] = run(name, script, table.unpack(args))
end)
return runtimes
end

function runner.spawn(script, ...)
local runtime = runner.run(script, ...)
local name = string.match(script, "(%w*/*%w*)$")
Expand All @@ -43,7 +64,10 @@ end
function runner.stop(script)
local script = trim(script)
stop(env.threads, script)
stop(env.runtimes, script)
linux.percpu(function (cpu)
local name = runtime_name(script, cpu)
stop(env.runtimes, name)
end)
end

function runner.list()
Expand All @@ -55,8 +79,11 @@ function runner.list()
end

function runner.shutdown()
rcu.map(env.runtimes, function (script)
runner.stop(script)
rcu.map(env.threads, function (script)
stop(env.threads, script)
end)
rcu.map(env.runtimes, function (name)
stop(env.runtimes, name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't we need to stop percpu here as well?

end)
end

Expand Down