Skip to content
Open
15 changes: 15 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[submodule "lib/lunatik"]
path = lib/lunatik
url = https://github.com/luainkernel/lunatik.git
[submodule "lib/luadata"]
path = lib/luadata
url = https://github.com/luainkernel/luadata
[submodule "lib/luaxdp"]
path = lib/luaxdp
url = https://github.com/luainkernel/luaxdp.git
[submodule "lib/luarcu"]
path = lib/luarcu
url = https://github.com/luainkernel/luarcu
[submodule "lib/luaunpack"]
path = lib/luaunpack
url = https://github.com/VictorNogueiraRio/luaunpack.git
4 changes: 4 additions & 0 deletions include/linux/netdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3587,6 +3587,10 @@ u32 __dev_xdp_query(struct net_device *dev, bpf_op_t xdp_op,
enum bpf_netdev_command cmd);
int xdp_umem_query(struct net_device *dev, u16 queue_id);

#ifdef CONFIG_XDP_LUA
int generic_xdp_lua_install_prog(char *lua_prog);
#endif /* CONFIG_XDP_LUA */

int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
bool is_skb_forwardable(const struct net_device *dev,
Expand Down
17 changes: 17 additions & 0 deletions include/net/xdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,30 @@ struct xdp_rxq_info {
struct xdp_mem_info mem;
} ____cacheline_aligned; /* perf critical, avoid false-sharing */

#ifdef CONFIG_XDP_LUA
struct xdplua_create_work {
char lua_script[8192];
struct lua_State *L;
struct work_struct work;
spinlock_t lock;
bool init;
};

DECLARE_PER_CPU(struct xdplua_create_work, luaworks);
#endif /* CONFIG_XDP_LUA */


struct xdp_buff {
void *data;
void *data_end;
void *data_meta;
void *data_hard_start;
unsigned long handle;
struct xdp_rxq_info *rxq;
#ifdef CONFIG_XDP_LUA
struct sk_buff *skb;
struct lua_State *L;
#endif /* CONFIG_XDP_LUA */
};

struct xdp_frame {
Expand Down
19 changes: 18 additions & 1 deletion include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,24 @@ union bpf_attr {
FN(get_current_cgroup_id), \
FN(get_local_storage), \
FN(sk_select_reuseport), \
FN(skb_ancestor_cgroup_id),
FN(skb_ancestor_cgroup_id), \
/* #ifdef CONFIG_XDP_LUA */ \
FN(lua_dataref), \
FN(lua_dataunref), \
FN(lua_pcall), \
FN(lua_tostring), \
FN(lua_pop), \
FN(lua_pushinteger), \
FN(lua_pushlightuserdata), \
FN(lua_pushlstring), \
FN(lua_pushmap), \
FN(lua_pushskb), \
FN(lua_pushstring), \
FN(lua_toboolean), \
FN(lua_tointeger), \
FN(lua_newpacket), \
FN(lua_type),
/* #endif CONFIG_XDP_LUA */

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call
Expand Down
3 changes: 3 additions & 0 deletions include/uapi/linux/if_link.h
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,9 @@ enum {
IFLA_XDP_DRV_PROG_ID,
IFLA_XDP_SKB_PROG_ID,
IFLA_XDP_HW_PROG_ID,
#ifdef CONFIG_XDP_LUA
IFLA_XDP_LUA_PROG,
#endif /* CONFIG_XDP_LUA */
__IFLA_XDP_MAX,
};

Expand Down
6 changes: 6 additions & 0 deletions init/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,12 @@ config HAVE_PCSPKR_PLATFORM
config BPF
bool

config LUNATIK
bool "Lunatik"
default n
help
Support for the Lua interpreter

menuconfig EXPERT
bool "Configure standard kernel features (expert users)"
# Unhide debug options, to make the on-by-default options visible
Expand Down
9 changes: 9 additions & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,12 @@ obj-$(CONFIG_GENERIC_LIB_LSHRDI3) += lshrdi3.o
obj-$(CONFIG_GENERIC_LIB_MULDI3) += muldi3.o
obj-$(CONFIG_GENERIC_LIB_CMPDI2) += cmpdi2.o
obj-$(CONFIG_GENERIC_LIB_UCMPDI2) += ucmpdi2.o

subdir-ccflags-y += -I$(srctree)/lib/lunatik/lua \
-I$(srctree)/lib/luadata/ -I$(srctree)/lib/luaunpack/ \
-D_KERNEL
obj-$(CONFIG_LUNATIK) += lunatik/
obj-$(CONFIG_LUADATA) += luadata/
obj-$(CONFIG_LUAXDP) += luaxdp/
obj-$(CONFIG_LUARCU) += luarcu/
obj-$(CONFIG_LUAUNPACK) += luaunpack/
1 change: 1 addition & 0 deletions lib/luadata
Submodule luadata added at 21137a
1 change: 1 addition & 0 deletions lib/luarcu
Submodule luarcu added at bc563d
1 change: 1 addition & 0 deletions lib/luaunpack
Submodule luaunpack added at 0589fc
1 change: 1 addition & 0 deletions lib/luaxdp
Submodule luaxdp added at be34f8
1 change: 1 addition & 0 deletions lib/lunatik
Submodule lunatik added at fb7ef4
4 changes: 4 additions & 0 deletions net/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ obj-y := sock.o request_sock.o skbuff.o datagram.o stream.o scm.o \

obj-$(CONFIG_SYSCTL) += sysctl_net_core.o

CFLAGS_dev.o = -Ilib/lunatik/lua/ -D_KERNEL \
-Ilib/luadata/
CFLAGS_filter.o = -Ilib/lunatik/lua/ -D_KERNEL \
-Ilib/luadata/ -Ilib/luaunpack/
obj-y += dev.o ethtool.o dev_addr_lists.o dst.o netevent.o \
neighbour.o rtnetlink.o utils.o link_watch.o filter.o \
sock_diag.o dev_ioctl.o tso.o sock_reuseport.o \
Expand Down
72 changes: 72 additions & 0 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@
* - netif_rx() feedback
*/

#ifdef CONFIG_XDP_LUA
#include <lua.h>
#include <lauxlib.h>
#include <lstate.h>
#include <lualib.h>
#include <luadata.h>
#endif /* CONFIG_XDP_LUA */

#include <linux/uaccess.h>
#include <linux/bitops.h>
#include <linux/capability.h>
Expand Down Expand Up @@ -155,6 +163,9 @@

static DEFINE_SPINLOCK(ptype_lock);
static DEFINE_SPINLOCK(offload_lock);
#ifdef CONFIG_XDP_LUA
DEFINE_PER_CPU(struct xdplua_create_work, luaworks);
#endif
struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
struct list_head ptype_all __read_mostly; /* Taps */
static struct list_head offload_base __read_mostly;
Expand Down Expand Up @@ -4279,6 +4290,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
u32 metalen, act = XDP_DROP;
int hlen, off;
u32 mac_len;
#ifdef CONFIG_XDP_LUA
struct xdplua_create_work *lw;
#endif /* CONFIG_XDP_LUA */

/* Reinjected packets coming from act_mirred or similar should
* not get XDP generic processing.
Expand Down Expand Up @@ -4320,9 +4334,22 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,

rxqueue = netif_get_rxqueue(skb);
xdp->rxq = &rxqueue->xdp_rxq;
#ifdef CONFIG_XDP_LUA
lw = this_cpu_ptr(&luaworks);

xdp->skb = skb;
xdp->L = lw->L;
#endif /* CONFIG_XDP_LUA */

act = bpf_prog_run_xdp(xdp_prog, xdp);

#ifdef CONFIG_XDP_LUA
if (lw->init) {
lw->init = false;
spin_unlock(&lw->lock);
}
#endif /* CONFIG_XDP_LUA */

off = xdp->data - orig_data;
if (off > 0)
__skb_pull(skb, off);
Expand Down Expand Up @@ -5088,6 +5115,35 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
return ret;
}

#ifdef CONFIG_XDP_LUA

static void per_cpu_xdp_lua_install(struct work_struct *w) {
int this_cpu = smp_processor_id();
struct xdplua_create_work *lw =
container_of(w, struct xdplua_create_work, work);

spin_lock_bh(&lw->lock);
if (luaL_dostring(lw->L, lw->lua_script)) {
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
lua_tostring(lw->L, -1), this_cpu);
}
spin_unlock_bh(&lw->lock);
}

int generic_xdp_lua_install_prog(char *lua_script)
{
int cpu;
struct xdplua_create_work *lw;

for_each_possible_cpu(cpu) {
lw = per_cpu_ptr(&luaworks, cpu);
strcpy(lw->lua_script, lua_script);
schedule_work_on(cpu, &lw->work);
}
return 0;
}
#endif /* CONFIG_XDP_LUA */

static int netif_receive_skb_internal(struct sk_buff *skb)
{
int ret;
Expand Down Expand Up @@ -9595,6 +9651,9 @@ static int __init net_dev_init(void)
for_each_possible_cpu(i) {
struct work_struct *flush = per_cpu_ptr(&flush_works, i);
struct softnet_data *sd = &per_cpu(softnet_data, i);
#ifdef CONFIG_XDP_LUA
struct xdplua_create_work *lw = per_cpu_ptr(&luaworks, i);
#endif

INIT_WORK(flush, flush_backlog);

Expand All @@ -9614,6 +9673,19 @@ static int __init net_dev_init(void)
init_gro_hash(&sd->backlog);
sd->backlog.poll = process_backlog;
sd->backlog.weight = weight_p;
#ifdef CONFIG_XDP_LUA
lw->L = luaL_newstate();
WARN_ON(!lw->L);

if (!lw->L)
continue;

luaL_openlibs(lw->L);
luaL_requiref(lw->L, "data", luaopen_data, 1);
lua_pop(lw->L, 1);

INIT_WORK(&lw->work, per_cpu_xdp_lua_install);
#endif /* CONFIG_XDP_LUA */
}

dev_boot_phase = 0;
Expand Down
Loading