Skip to content

Commit 096c904

Browse files
Add XDPLua
1 parent 3306991 commit 096c904

File tree

24 files changed

+1269
-2
lines changed

24 files changed

+1269
-2
lines changed

.gitmodules

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[submodule "lib/lunatik"]
2+
path = lib/lunatik
3+
url = https://github.com/luainkernel/lunatik.git
4+
[submodule "lib/luadata"]
5+
path = lib/luadata
6+
url = https://github.com/luainkernel/luadata
7+
[submodule "lib/luaxdp"]
8+
path = lib/luaxdp
9+
url = https://github.com/luainkernel/luaxdp.git
10+
[submodule "lib/luarcu"]
11+
path = lib/luarcu
12+
url = https://github.com/luainkernel/luarcu
13+
[submodule "lib/luaunpack"]
14+
path = lib/luaunpack
15+
url = https://github.com/VictorNogueiraRio/luaunpack.git

include/net/xdplua.h

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* XDPLua internal functions
2+
* Copyright (C) 2021 Victor Nogueira <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License as
6+
* published by the Free Software Foundation version 2.
7+
*
8+
* This program is distributed "as is" WITHOUT ANY WARRANTY of any
9+
* kind, whether express or implied; without even the implied warranty
10+
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*/
13+
#ifndef __LINUX_NET_XDP_LUA_H__
14+
#define __LINUX_NET_XDP_LUA_H__
15+
16+
#include <lua.h>
17+
18+
#include <linux/skbuff.h>
19+
#include <linux/workqueue.h>
20+
#include <uapi/linux/if_link.h>
21+
#include <linux/filter.h>
22+
23+
struct xdp_lua_work {
24+
char script[XDP_LUA_MAX_SCRIPT_LEN];
25+
size_t script_len;
26+
struct lua_State *L;
27+
struct sk_buff *skb;
28+
struct work_struct work;
29+
};
30+
31+
DECLARE_PER_CPU(struct xdp_lua_work, luaworks);
32+
33+
34+
#define XDP_LUA_BPF_FUNC(name) BPF_FUNC_lua_##name
35+
36+
#define xdp_lua_get_skb() (this_cpu_ptr(&luaworks)->skb)
37+
#define xdp_lua_set_skb(skb) (this_cpu_ptr(&luaworks)->skb = skb)
38+
39+
int generic_xdp_lua_install_prog(const char *script, size_t script_len);
40+
void xdp_lua_init(void);
41+
42+
#define __BPF_LUA_MAP_0(l, m, v, ...) l
43+
#define __BPF_LUA_MAP_1(l, m, v, t, a, ...) l, __BPF_MAP_1(m, v, t, a, __VA_ARGS__)
44+
#define __BPF_LUA_MAP_2(l, m, v, t, a, ...) l, __BPF_MAP_2(m, v, t, a, __VA_ARGS__)
45+
#define __BPF_LUA_MAP_3(l, m, v, t, a, ...) l, __BPF_MAP_3(m, v, t, a, __VA_ARGS__)
46+
#define __BPF_LUA_MAP_4(l, m, v, t, a, ...) l, __BPF_MAP_4(m, v, t, a, __VA_ARGS__)
47+
#define __BPF_LUA_MAP_5(l, m, v, t, a, ...) l, __BPF_MAP_5(m, v, t, a, __VA_ARGS__)
48+
49+
#define __BPF_LUA_MAP(n, l, ...) __BPF_LUA_MAP_##n(l, __VA_ARGS__)
50+
51+
#define LST_TYPE lua_State
52+
#define LST_NAME L
53+
#define LST_DECL __BPF_DECL_ARGS(LST_TYPE*, LST_NAME)
54+
55+
#define BPF_LUA_CALL_x(x, name, ...) \
56+
static __always_inline \
57+
u64 ____##name(__BPF_LUA_MAP(x, LST_DECL, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \
58+
u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \
59+
u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \
60+
{ \
61+
int ret = -ENOENT; \
62+
lua_State *L = this_cpu_ptr(&luaworks)->L; \
63+
WARN_ON(!L); \
64+
if (!L) \
65+
return ret; \
66+
ret = ____##name(__BPF_LUA_MAP(x, LST_NAME, __BPF_CAST, __BPF_N, __VA_ARGS__)); \
67+
return ret; \
68+
} \
69+
static __always_inline \
70+
u64 ____##name(__BPF_LUA_MAP(x, LST_DECL, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__))
71+
72+
#define BPF_LUA_CALL_0(name, ...) BPF_LUA_CALL_x(0, name, __VA_ARGS__)
73+
#define BPF_LUA_CALL_1(name, ...) BPF_LUA_CALL_x(1, name, __VA_ARGS__)
74+
#define BPF_LUA_CALL_2(name, ...) BPF_LUA_CALL_x(2, name, __VA_ARGS__)
75+
#define BPF_LUA_CALL_3(name, ...) BPF_LUA_CALL_x(3, name, __VA_ARGS__)
76+
#define BPF_LUA_CALL_4(name, ...) BPF_LUA_CALL_x(4, name, __VA_ARGS__)
77+
#define BPF_LUA_CALL_5(name, ...) BPF_LUA_CALL_x(5, name, __VA_ARGS__)
78+
79+
#endif /* __LINUX_NET_XDP_LUA_H__ */

include/uapi/linux/bpf.h

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,6 +3742,111 @@ union bpf_attr {
37423742
* Return
37433743
* The helper returns **TC_ACT_REDIRECT** on success or
37443744
* **TC_ACT_SHOT** on error.
3745+
*
3746+
* int bpf_lua_dataref(void *ctx, int offset)
3747+
* Description
3748+
* Create a new lua data buffer object pointing to the captured
3749+
* packet at the specified offset. The function leaves the new
3750+
* object on top of the Lua stack.
3751+
* Return
3752+
* Data object reference number on success, or -1 in case
3753+
* of failure
3754+
*
3755+
* void bpf_lua_dataunref(int data_ref)
3756+
* Description
3757+
* Releases the data-object reference, allowing it to be
3758+
* garbage-collected
3759+
*
3760+
* int bpf_lua_pcall(char *funcname, int num_args, int num_rets)
3761+
* Description
3762+
* Calls Lua function funcname with the given nargs arguments in protected mode
3763+
*
3764+
* void bpf_lua_pop(int n)
3765+
* Description
3766+
* Pops n elements from the Lua stack
3767+
*
3768+
* void bpf_lua_pushinteger(int num)
3769+
* Description
3770+
* Pushes an integer with value n onto the Lua stack.
3771+
*
3772+
* void bpf_lua_pushlightuserdata(void *ptr)
3773+
* Description
3774+
* Pushes a light userdata onto the Lua stack.
3775+
* Userdata represent C values in Lua.
3776+
* A light userdata represents a pointer, a void*.
3777+
* It is a value (like a number): you do not create it,
3778+
* it has no individual metatable, and it is not collected
3779+
* (as it was never created).
3780+
* A light userdata is equal to "any" light userdata with
3781+
* the same C address.
3782+
*
3783+
* void bpf_lua_pushlstring(const char *s, size_t len)
3784+
* Description
3785+
* Pushes the string pointed to by s with size len onto the stack.
3786+
* Lua makes (or reuses) an internal copy of the given string,
3787+
* so the memory at s can be freed or reused immediately after the
3788+
* function returns.
3789+
* The string can contain any binary data, including embedded zeros.
3790+
*
3791+
* void bpf_lua_pushmap(void *map)
3792+
* Description
3793+
* Pushes a BPF map onto the Lua stack
3794+
*
3795+
* void bpf_lua_pushskb(void)
3796+
* Description
3797+
* Pushes an SKB structure onto the Lua stack
3798+
*
3799+
* void bpf_lua_pushstring(const char *s)
3800+
* Description
3801+
* Pushes the zero-terminated string pointed to by s onto the stack.
3802+
* Lua makes (or reuses) an internal copy of the given string,
3803+
* so the memory at s can be freed or reused immediately after the
3804+
* function returns.
3805+
*
3806+
* int bpf_lua_toboolean(int index)
3807+
* Description
3808+
* Converts the Lua value at the given index to a C
3809+
* boolean value (0 or 1)
3810+
* Return
3811+
* 1 if the value in the given index of the Lua stack is
3812+
* different from from false or null, otherwise returns 0
3813+
*
3814+
* int bpf_lua_tointeger(int index)
3815+
* Description
3816+
* Converts the Lua value at the given index of the Lua stack
3817+
* to the signed integral type lua_Integer.
3818+
* Return
3819+
* The converted Lua value at the given index, if the value is
3820+
* convertible to an integer(see the Lua manual for more details
3821+
* on type conversion); otherwise returns 0
3822+
*
3823+
* void bpf_lua_tostring(const char *str, u32 size, int index)
3824+
* Description
3825+
* Converts the Lua value at the given index of the Lua
3826+
* stack to a C string and copies size bytes of it to
3827+
* value pointed by str
3828+
* Return
3829+
* 1 if the value at the given index of the Lua stack is a
3830+
* string; otherwise it returns 0
3831+
*
3832+
* int bpf_lua_newpacket(void *ctx, int offset)
3833+
* Description
3834+
* Create new luaunpack user data buffer pointing to
3835+
* the captured packet at the specified offset
3836+
*
3837+
* int bpf_lua_type(int index)
3838+
* Description
3839+
* Obtains the type of the Lua value at the given index
3840+
* of the Lua stack
3841+
*
3842+
* Return
3843+
* Type of the value in the given valid index,
3844+
* or LUA_TNONE for a non-valid (but acceptable) index.
3845+
* The types returned by lua_type are coded by the
3846+
* following constants defined in lua.h: LUA_TNIL (0),
3847+
* LUA_TNUMBER, LUA_TBOOLEAN, LUA_TSTRING, LUA_TTABLE,
3848+
* LUA_TFUNCTION, LUA_TUSERDATA, LUA_TTHREAD, and
3849+
* LUA_TLIGHTUSERDATA.
37453850
*/
37463851
#define __BPF_FUNC_MAPPER(FN) \
37473852
FN(unspec), \
@@ -3900,6 +4005,23 @@ union bpf_attr {
39004005
FN(per_cpu_ptr), \
39014006
FN(this_cpu_ptr), \
39024007
FN(redirect_peer), \
4008+
/* #ifdef CONFIG_XDP_LUA */ \
4009+
FN(lua_dataref), \
4010+
FN(lua_dataunref), \
4011+
FN(lua_pcall), \
4012+
FN(lua_pop), \
4013+
FN(lua_pushinteger), \
4014+
FN(lua_pushlightuserdata), \
4015+
FN(lua_pushlstring), \
4016+
FN(lua_pushmap), \
4017+
FN(lua_pushskb), \
4018+
FN(lua_pushstring), \
4019+
FN(lua_toboolean), \
4020+
FN(lua_tointeger), \
4021+
FN(lua_tostring), \
4022+
FN(lua_newpacket), \
4023+
FN(lua_type),
4024+
/* #endif CONFIG_XDP_LUA */
39034025
/* */
39044026

39054027
/* integer value in 'imm' field of BPF_CALL instruction selects which helper

include/uapi/linux/if_link.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ enum {
11721172
#define XDP_FLAGS_MASK (XDP_FLAGS_UPDATE_IF_NOEXIST | \
11731173
XDP_FLAGS_MODES | XDP_FLAGS_REPLACE)
11741174

1175+
#define XDP_LUA_MAX_SCRIPT_LEN 8192
11751176
/* These are stored into IFLA_XDP_ATTACHED on dump. */
11761177
enum {
11771178
XDP_ATTACHED_NONE = 0,
@@ -1191,6 +1192,7 @@ enum {
11911192
IFLA_XDP_SKB_PROG_ID,
11921193
IFLA_XDP_HW_PROG_ID,
11931194
IFLA_XDP_EXPECTED_FD,
1195+
IFLA_XDP_LUA_PROG,
11941196
__IFLA_XDP_MAX,
11951197
};
11961198

lib/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,12 @@ obj-$(CONFIG_BITFIELD_KUNIT) += bitfield_kunit.o
353353
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
354354
obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
355355
obj-$(CONFIG_BITS_TEST) += test_bits.o
356+
357+
subdir-ccflags-y += -I$(srctree)/lib/lunatik/lua \
358+
-I$(srctree)/lib/luadata/ -I$(srctree)/lib/luaunpack/ \
359+
-D_KERNEL
360+
obj-$(CONFIG_LUNATIK) += lunatik/
361+
obj-$(CONFIG_LUADATA) += luadata/
362+
obj-$(CONFIG_LUAXDP) += luaxdp/
363+
obj-$(CONFIG_LUARCU) += luarcu/
364+
obj-$(CONFIG_LUAUNPACK) += luaunpack/

lib/luadata

Submodule luadata added at 21137a0

lib/luarcu

Submodule luarcu added at bc563d5

lib/luaunpack

Submodule luaunpack added at de01679

lib/luaxdp

Submodule luaxdp added at be34f87

lib/lunatik

Submodule lunatik added at e417973

0 commit comments

Comments
 (0)