Skip to content

Commit f278687

Browse files
add LuaData submodule
1 parent ff7c9b9 commit f278687

8 files changed

Lines changed: 84 additions & 2 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "lib/lunatik"]
22
path = lib/lunatik
33
url = https://github.com/luainkernel/lunatik.git
4+
[submodule "lib/luadata"]
5+
path = lib/luadata
6+
url = https://github.com/luainkernel/luadata

include/uapi/linux/bpf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,20 @@ union bpf_attr {
28912891
* Return
28922892
* The 64 bit jiffies
28932893
*
2894+
* int bpf_lua_dataref(void *ctx, int offset)
2895+
* Description
2896+
* Create a new lua data buffer object pointing to the captured
2897+
* packet at the specified offset. The function leaves the new
2898+
* object on top of the Lua stack.
2899+
* Return
2900+
* Data object reference number on success, or -1 in case
2901+
* of failure
2902+
*
2903+
* void bpf_lua_dataunref(void *ctx, int data_ref)
2904+
* Description
2905+
* Releases the data-object reference, allowing it to be
2906+
* garbage-collected
2907+
*
28942908
* void bpf_lua_pcall(void *ctx, char *funcname, int num_args, int num_rets)
28952909
* Description
28962910
* Calls Lua function funcname with the given nargs arguments in protected mode
@@ -3079,6 +3093,8 @@ union bpf_attr {
30793093
FN(send_signal_thread), \
30803094
FN(jiffies64), \
30813095
/* #ifdef CONFIG_XDP_LUA */ \
3096+
FN(lua_dataref), \
3097+
FN(lua_dataunref), \
30823098
FN(lua_pcall), \
30833099
FN(lua_pop), \
30843100
FN(lua_pushinteger), \

lib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,7 @@ obj-$(CONFIG_OBJAGG) += objagg.o
301301
# KUnit tests
302302
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
303303
subdir-ccflags-y += -I$(srctree)/lib/lunatik/lua \
304+
-I$(srctree)/lib/luadata/ \
304305
-D_KERNEL
305306
obj-$(CONFIG_LUNATIK) += lunatik/
307+
obj-$(CONFIG_LUADATA) += luadata/

lib/luadata

Submodule luadata added at 21137a0

net/core/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ obj-y := sock.o request_sock.o skbuff.o datagram.o stream.o scm.o \
88

99
obj-$(CONFIG_SYSCTL) += sysctl_net_core.o
1010

11-
CFLAGS_dev.o = -Ilib/lunatik/lua/ -D_KERNEL
12-
CFLAGS_filter.o = -Ilib/lunatik/lua/ -D_KERNEL
11+
CFLAGS_dev.o = -Ilib/lunatik/lua/ -D_KERNEL \
12+
-Ilib/luadata/
13+
CFLAGS_filter.o = -Ilib/lunatik/lua/ -D_KERNEL \
14+
-Ilib/luadata/
1315
obj-y += dev.o dev_addr_lists.o dst.o netevent.o \
1416
neighbour.o rtnetlink.o utils.o link_watch.o filter.o \
1517
sock_diag.o dev_ioctl.o tso.o sock_reuseport.o \

net/core/dev.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include <lua.h>
7373
#include <lauxlib.h>
7474
#include <lualib.h>
75+
#include <luadata.h>
7576
#endif /* CONFIG_XDP_LUA */
7677

7778
#include <linux/uaccess.h>
@@ -10555,6 +10556,7 @@ static int __init net_dev_init(void)
1055510556
}
1055610557

1055710558
luaL_openlibs(new_state_cpu->L);
10559+
luaL_requiref(new_state_cpu->L, "data", luaopen_data, 1);
1055810560
lua_pop(new_state_cpu->L, 1);
1055910561
new_state_cpu->cpu = i;
1056010562

net/core/filter.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676

7777
#ifdef CONFIG_XDP_LUA
7878
#include <lua.h>
79+
#include <luadata.h>
7980
#endif /* CONFIG_XDP_LUA */
8081

8182
/**
@@ -5854,6 +5855,41 @@ static const struct bpf_func_proto bpf_tcp_gen_syncookie_proto = {
58545855
#endif /* CONFIG_INET */
58555856

58565857
#ifdef CONFIG_XDP_LUA
5858+
BPF_CALL_2(bpf_lua_dataref, struct xdp_buff *, ctx, int, offset) {
5859+
if (offset + ctx->data < ctx->data_end) {
5860+
int data_ref;
5861+
5862+
data_ref = ldata_newref(ctx->L, ctx->data + offset,
5863+
ctx->data_end - ctx->data - offset);
5864+
return data_ref;
5865+
}
5866+
5867+
return -1;
5868+
}
5869+
5870+
static const struct bpf_func_proto bpf_lua_dataref_proto = {
5871+
.func = bpf_lua_dataref,
5872+
.gpl_only = false,
5873+
.pkt_access = false,
5874+
.ret_type = RET_INTEGER,
5875+
.arg1_type = ARG_PTR_TO_CTX,
5876+
.arg1_type = ARG_ANYTHING,
5877+
};
5878+
5879+
BPF_CALL_2(bpf_lua_dataunref, struct xdp_buff *, ctx, int, data_ref) {
5880+
ldata_unref(ctx->L, data_ref);
5881+
return 0;
5882+
}
5883+
5884+
static const struct bpf_func_proto bpf_lua_dataunref_proto = {
5885+
.func = bpf_lua_dataunref,
5886+
.gpl_only = false,
5887+
.pkt_access = false,
5888+
.ret_type = RET_VOID,
5889+
.arg1_type = ARG_PTR_TO_CTX,
5890+
.arg2_type = ARG_ANYTHING,
5891+
};
5892+
58575893
BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname,
58585894
int, num_args, int, num_rets) {
58595895
if (lua_getglobal(ctx->L, funcname) != LUA_TFUNCTION) {
@@ -6105,6 +6141,10 @@ bpf_base_func_proto(enum bpf_func_id func_id)
61056141
case BPF_FUNC_jiffies64:
61066142
return &bpf_jiffies64_proto;
61076143
#ifdef CONFIG_XDP_LUA
6144+
case BPF_FUNC_lua_dataref:
6145+
return &bpf_lua_dataref_proto;
6146+
case BPF_FUNC_lua_dataunref:
6147+
return &bpf_lua_dataunref_proto;
61086148
case BPF_FUNC_lua_pcall:
61096149
return &bpf_lua_pcall_proto;
61106150
case BPF_FUNC_lua_pop:

tools/include/uapi/linux/bpf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2891,6 +2891,20 @@ union bpf_attr {
28912891
* Return
28922892
* The 64 bit jiffies
28932893
*
2894+
* int bpf_lua_dataref(void *ctx, int offset)
2895+
* Description
2896+
* Create a new lua data buffer object pointing to the captured
2897+
* packet at the specified offset. The function leaves the new
2898+
* object on top of the Lua stack.
2899+
* Return
2900+
* Data object reference number on success, or -1 in case
2901+
* of failure
2902+
*
2903+
* void bpf_lua_dataunref(void *ctx, int data_ref)
2904+
* Description
2905+
* Releases the data-object reference, allowing it to be
2906+
* garbage-collected
2907+
*
28942908
* void bpf_lua_pcall(void *ctx, char *funcname, int num_args, int num_rets)
28952909
* Description
28962910
* Calls Lua function funcname with the given nargs arguments in protected mode
@@ -3079,6 +3093,8 @@ union bpf_attr {
30793093
FN(send_signal_thread), \
30803094
FN(jiffies64), \
30813095
/* #ifdef CONFIG_XDP_LUA */ \
3096+
FN(lua_dataref), \
3097+
FN(lua_dataunref), \
30823098
FN(lua_pcall), \
30833099
FN(lua_pop), \
30843100
FN(lua_pushinteger), \

0 commit comments

Comments
 (0)