Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/lunatik"]
path = lib/lunatik
url = https://github.com/luainkernel/lunatik.git
10 changes: 10 additions & 0 deletions include/linux/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -1268,4 +1268,14 @@ struct bpf_sockopt_kern {
s32 retval;
};

#ifdef CONFIG_XDP_LUA
extern struct list_head lua_state_cpu_list;

struct lua_state_cpu {
struct lua_State *L;
int cpu;
struct list_head list;
};
#endif /* CONFIG_XDP_LUA */

#endif /* __LINUX_FILTER_H__ */
4 changes: 4 additions & 0 deletions include/linux/netdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3772,6 +3772,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
4 changes: 4 additions & 0 deletions include/net/xdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ struct xdp_buff {
void *data_hard_start;
unsigned long handle;
struct xdp_rxq_info *rxq;
#ifdef CONFIG_XDP_LUA
Copy link
Member Author

Choose a reason for hiding this comment

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

With the creation of struct xdplua_create_work there probably is no reason to modify struct xdp_buff

Copy link

Choose a reason for hiding this comment

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

agreed..

struct sk_buff *skb;
struct lua_State *L;
#endif /* CONFIG_XDP_LUA */
};

struct xdp_frame {
Expand Down
82 changes: 81 additions & 1 deletion include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2890,6 +2890,73 @@ union bpf_attr {
* Obtain the 64bit jiffies
* Return
* The 64 bit jiffies
*
* void bpf_lua_pcall(void *ctx, char *funcname, int num_args, int num_rets)
* Description
* Calls Lua function funcname with the given nargs arguments in protected mode
*
* void bpf_lua_pop(void *ctx, int n)
* Description
* Pops n elements from the Lua stack
*
* void bpf_lua_pushinteger(void *ctx, int num)
* Description
* Pushes an integer with value n onto the Lua stack.
*
* void bpf_lua_pushlightuserdata(void *ctx, void *ptr)
* Description
* Pushes a light userdata onto the Lua stack.
* Userdata represent C values in Lua.
* A light userdata represents a pointer, a void*.
* It is a value (like a number): you do not create it,
* it has no individual metatable, and it is not collected
* (as it was never created).
* A light userdata is equal to "any" light userdata with
* the same C address.
*
* void bpf_lua_pushlstring(void *ctx, const char *s, size_t len)
* Description
* Pushes the string pointed to by s with size len onto the stack.
* Lua makes (or reuses) an internal copy of the given string,
* so the memory at s can be freed or reused immediately after the
* function returns.
* The string can contain any binary data, including embedded zeros.
*
* void bpf_lua_pushmap(void *ctx, void *map)
* Description
* Pushes a BPF map onto the Lua stack
*
* void bpf_lua_pushskb(void *ctx)
* Description
* Pushes an SKB structure onto the Lua stack
*
* void bpf_lua_pushstring(void *ctx, const char *s)
* Description
* Pushes the zero-terminated string pointed to by s onto the stack.
* Lua makes (or reuses) an internal copy of the given string,
* so the memory at s can be freed or reused immediately after the
* function returns.
*
* void bpf_lua_setstate(void *ctx)
* Description
* Sets the Lua state pointer in the context struct
*
* int bpf_lua_toboolean(void *ctx, int index)
* Description
* Converts the Lua value at the given index to a C
* boolean value (0 or 1)
* Return
* 1 if the value in the given index of the Lua stack is
* different from from false or null, otherwise returns 0
*
* int bpf_lua_tointeger(void *ctx, int index)
* Description
* Converts the Lua value at the given index of the Lua stack
* to the signed integral type lua_Integer.
* Return
* The converted Lua value at the given index, if the value is
* convertible to an integer(see the Lua manual for more details
* on type conversion); otherwise returns 0
*/
#define __BPF_FUNC_MAPPER(FN) \
FN(unspec), \
Expand Down Expand Up @@ -3010,7 +3077,20 @@ union bpf_attr {
FN(probe_read_kernel_str), \
FN(tcp_send_ack), \
FN(send_signal_thread), \
FN(jiffies64),
FN(jiffies64), \
/* #ifdef CONFIG_XDP_LUA */ \
FN(lua_pcall), \
FN(lua_pop), \
FN(lua_pushinteger), \
FN(lua_pushlightuserdata), \
FN(lua_pushlstring), \
FN(lua_pushmap), \
FN(lua_pushskb), \
FN(lua_pushstring), \
FN(lua_setstate), \
FN(lua_toboolean), \
FN(lua_tointeger),
/* #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 @@ -984,6 +984,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,
Copy link

Choose a reason for hiding this comment

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

I believe we should uniformize our "namespace".. it looks like we should use XDP_LUA everywhere instead of XDPLUA, right?

#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 @@ -1307,6 +1307,12 @@ config HAVE_PCSPKR_PLATFORM
config BPF
bool

config LUNATIK
Copy link

Choose a reason for hiding this comment

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

shouldn't we put this config on Lunatik module itself?

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
3 changes: 3 additions & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,6 @@ obj-$(CONFIG_OBJAGG) += objagg.o

# KUnit tests
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
subdir-ccflags-y += -I$(srctree)/lib/lunatik/lua \
-D_KERNEL
obj-$(CONFIG_LUNATIK) += lunatik/
1 change: 1 addition & 0 deletions lib/lunatik
Submodule lunatik added at fb7ef4
2 changes: 2 additions & 0 deletions net/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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
CFLAGS_filter.o = -Ilib/lunatik/lua/ -D_KERNEL
obj-y += dev.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
54 changes: 54 additions & 0 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
* - netif_rx() feedback
*/

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

#include <linux/uaccess.h>
#include <linux/bitops.h>
#include <linux/capability.h>
Expand Down Expand Up @@ -164,6 +170,10 @@ static int call_netdevice_notifiers_extack(unsigned long val,
struct netlink_ext_ack *extack);
static struct napi_struct *napi_by_id(unsigned int napi_id);

#ifdef CONFIG_XDP_LUA
struct list_head lua_state_cpu_list;
#endif /* CONFIG_XDP_LUA */

/*
* The @dev_base_head list is protected by @dev_base_lock and the rtnl
* semaphore.
Expand Down Expand Up @@ -4556,6 +4566,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,

rxqueue = netif_get_rxqueue(skb);
xdp->rxq = &rxqueue->xdp_rxq;
#ifdef CONFIG_XDP_LUA
xdp->skb = skb;
Copy link

Choose a reason for hiding this comment

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

we will get rid of this, right? so, I would remove the \n above as well..

#endif /* CONFIG_XDP_LUA */

act = bpf_prog_run_xdp(xdp_prog, xdp);

Expand Down Expand Up @@ -5366,6 +5379,22 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
return ret;
}

#ifdef CONFIG_XDP_LUA
int generic_xdp_lua_install_prog(char *lua_prog)
{
struct lua_state_cpu *sc;

list_for_each_entry(sc, &lua_state_cpu_list, list) {
if (luaL_dostring(sc->L, lua_prog)) {
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
lua_tostring(sc->L, -1), sc->cpu);
return -EINVAL;
}
}
return 0;
Copy link

Choose a reason for hiding this comment

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

if you can't fail, why not using void? is it some contract?

}
#endif /* CONFIG_XDP_LUA */

static int netif_receive_skb_internal(struct sk_buff *skb)
{
int ret;
Expand Down Expand Up @@ -10462,6 +10491,9 @@ static struct pernet_operations __net_initdata default_device_ops = {
static int __init net_dev_init(void)
{
int i, rc = -ENOMEM;
#ifdef CONFIG_XDP_LUA
struct lua_state_cpu *new_state_cpu;
#endif /* CONFIG_XDP_LUA */

BUG_ON(!dev_boot_phase);

Expand All @@ -10476,6 +10508,9 @@ static int __init net_dev_init(void)
INIT_LIST_HEAD(&ptype_base[i]);

INIT_LIST_HEAD(&offload_base);
#ifdef CONFIG_XDP_LUA
INIT_LIST_HEAD(&lua_state_cpu_list);
#endif /* CONFIG_XDP_LUA */

if (register_pernet_subsys(&netdev_net_ops))
goto out;
Expand Down Expand Up @@ -10506,6 +10541,25 @@ 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
new_state_cpu = (struct lua_state_cpu *)
kmalloc(sizeof(struct lua_state_cpu), GFP_ATOMIC);
if (!new_state_cpu)
continue;

new_state_cpu->L = luaL_newstate();
if (!new_state_cpu->L) {
kfree(new_state_cpu);
continue;
}

luaL_openlibs(new_state_cpu->L);
lua_pop(new_state_cpu->L, 1);
new_state_cpu->cpu = i;

list_add(&new_state_cpu->list, &lua_state_cpu_list);
#endif /* CONFIG_XDP_LUA */
}

dev_boot_phase = 0;
Expand Down
Loading