Skip to content

Commit 5bf1c3f

Browse files
VictorNogueiraRiolneto
authored andcommitted
add XDPLua
1 parent 296751e commit 5bf1c3f

16 files changed

Lines changed: 629 additions & 2 deletions

File tree

include/linux/filter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,4 +1229,14 @@ struct bpf_sockopt_kern {
12291229
s32 retval;
12301230
};
12311231

1232+
#ifdef CONFIG_XDPLUA
1233+
extern struct list_head lua_state_cpu_list;
1234+
1235+
struct lua_state_cpu {
1236+
struct lua_State *L;
1237+
int cpu;
1238+
struct list_head list;
1239+
};
1240+
#endif /* CONFIG_XDPLUA */
1241+
12321242
#endif /* __LINUX_FILTER_H__ */

include/linux/netdevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,6 +3684,10 @@ u32 __dev_xdp_query(struct net_device *dev, bpf_op_t xdp_op,
36843684
enum bpf_netdev_command cmd);
36853685
int xdp_umem_query(struct net_device *dev, u16 queue_id);
36863686

3687+
#ifdef CONFIG_XDPLUA
3688+
int generic_xdp_lua_install_prog(char *lua_prog);
3689+
#endif /* CONFIG_XDPLUA */
3690+
36873691
int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
36883692
int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
36893693
bool is_skb_forwardable(const struct net_device *dev,

include/net/xdp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ struct xdp_buff {
7070
void *data_hard_start;
7171
unsigned long handle;
7272
struct xdp_rxq_info *rxq;
73+
#ifdef CONFIG_XDPLUA
74+
struct sk_buff *skb;
75+
struct lua_State *L;
76+
#endif /* CONFIG_XDPLUA */
7377
};
7478

7579
struct xdp_frame {

include/uapi/linux/bpf.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2824,7 +2824,20 @@ union bpf_attr {
28242824
FN(strtoul), \
28252825
FN(sk_storage_get), \
28262826
FN(sk_storage_delete), \
2827-
FN(send_signal),
2827+
FN(send_signal), \
2828+
/* #ifdef CONFIG_XDPLUA */ \
2829+
FN(lua_pcall), \
2830+
FN(lua_pop), \
2831+
FN(lua_pushinteger), \
2832+
FN(lua_pushlightuserdata), \
2833+
FN(lua_pushlstring), \
2834+
FN(lua_pushmap), \
2835+
FN(lua_pushskb), \
2836+
FN(lua_pushstring), \
2837+
FN(lua_setstate), \
2838+
FN(lua_toboolean), \
2839+
FN(lua_tointeger),
2840+
/* #endif CONFIG_XDPLUA */
28282841

28292842
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
28302843
* function eBPF program intends to call

include/uapi/linux/if_link.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,9 @@ enum {
974974
IFLA_XDP_DRV_PROG_ID,
975975
IFLA_XDP_SKB_PROG_ID,
976976
IFLA_XDP_HW_PROG_ID,
977+
#ifdef CONFIG_XDPLUA
978+
IFLA_XDP_LUA_PROG,
979+
#endif /* CONFIG_XDPLUA */
977980
__IFLA_XDP_MAX,
978981
};
979982

net/core/dev.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@
143143
#include <linux/indirect_call_wrapper.h>
144144
#include <net/devlink.h>
145145

146+
#ifdef CONFIG_XDPLUA
147+
#include <lua.h>
148+
#include <lauxlib.h>
149+
#include <lualib.h>
150+
#endif /* CONFIG_XDPLUA */
151+
146152
#include "net-sysfs.h"
147153

148154
#define MAX_GRO_SKBS 8
@@ -164,6 +170,10 @@ static int call_netdevice_notifiers_extack(unsigned long val,
164170
struct netlink_ext_ack *extack);
165171
static struct napi_struct *napi_by_id(unsigned int napi_id);
166172

173+
#ifdef CONFIG_XDPLUA
174+
struct list_head lua_state_cpu_list;
175+
#endif /* CONFIG_XDPLUA */
176+
167177
/*
168178
* The @dev_base_head list is protected by @dev_base_lock and the rtnl
169179
* semaphore.
@@ -4371,6 +4381,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
43714381

43724382
rxqueue = netif_get_rxqueue(skb);
43734383
xdp->rxq = &rxqueue->xdp_rxq;
4384+
#ifdef CONFIG_XDPLUA
4385+
xdp->skb = skb;
4386+
#endif /* CONFIG_XDPLUA */
43744387

43754388
act = bpf_prog_run_xdp(xdp_prog, xdp);
43764389

@@ -5183,6 +5196,22 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
51835196
return ret;
51845197
}
51855198

5199+
#ifdef CONFIG_XDPLUA
5200+
int generic_xdp_lua_install_prog(char *lua_prog)
5201+
{
5202+
struct lua_state_cpu *sc;
5203+
5204+
list_for_each_entry(sc, &lua_state_cpu_list, list) {
5205+
if (luaL_dostring(sc->L, lua_prog)) {
5206+
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
5207+
lua_tostring(sc->L, -1), sc->cpu);
5208+
return -EINVAL;
5209+
}
5210+
}
5211+
return 0;
5212+
}
5213+
#endif /* CONFIG_XDPLUA */
5214+
51865215
static int netif_receive_skb_internal(struct sk_buff *skb)
51875216
{
51885217
int ret;
@@ -9800,6 +9829,9 @@ static struct pernet_operations __net_initdata default_device_ops = {
98009829
static int __init net_dev_init(void)
98019830
{
98029831
int i, rc = -ENOMEM;
9832+
#ifdef CONFIG_XDPLUA
9833+
struct lua_state_cpu *new_state_cpu;
9834+
#endif /* CONFIG_XDPLUA */
98039835

98049836
BUG_ON(!dev_boot_phase);
98059837

@@ -9814,6 +9846,9 @@ static int __init net_dev_init(void)
98149846
INIT_LIST_HEAD(&ptype_base[i]);
98159847

98169848
INIT_LIST_HEAD(&offload_base);
9849+
#ifdef CONFIG_XDPLUA
9850+
INIT_LIST_HEAD(&lua_state_cpu_list);
9851+
#endif /* CONFIG_XDPLUA */
98179852

98189853
if (register_pernet_subsys(&netdev_net_ops))
98199854
goto out;
@@ -9844,6 +9879,26 @@ static int __init net_dev_init(void)
98449879
init_gro_hash(&sd->backlog);
98459880
sd->backlog.poll = process_backlog;
98469881
sd->backlog.weight = weight_p;
9882+
9883+
#ifdef CONFIG_XDPLUA
9884+
new_state_cpu = (struct lua_state_cpu *)
9885+
kmalloc(sizeof(lua_state_cpu), GFP_ATOMIC);
9886+
if (!new_state_cpu)
9887+
continue;
9888+
9889+
new_state_cpu->L = luaL_newstate();
9890+
if (!new_state_cpu->L) {
9891+
kfree(new_state_cpu);
9892+
continue;
9893+
}
9894+
9895+
luaL_openlibs(new_state_cpu->L);
9896+
luaL_requiref(new_state_cpu->L, "data", luaopen_data, 1);
9897+
lua_pop(new_state_cpu->L, 1);
9898+
new_state_cpu->cpu = i;
9899+
9900+
list_add(&new_state_cpu->list, &lua_state_cpu_list);
9901+
#endif /* CONFIG_XDPLUA */
98479902
}
98489903

98499904
dev_boot_phase = 0;

net/core/filter.c

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
#include <net/ipv6_stubs.h>
7575
#include <net/bpf_sk_storage.h>
7676

77+
#ifdef CONFIG_XDPLUA
78+
#include <lua.h>
79+
#endif /* CONFIG_XDPLUA */
80+
7781
/**
7882
* sk_filter_trim_cap - run a packet through a socket filter
7983
* @sk: sock associated with &sk_buff
@@ -5852,6 +5856,181 @@ static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = {
58525856

58535857
#endif /* CONFIG_INET */
58545858

5859+
#ifdef CONFIG_XDPLUA
5860+
BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname,
5861+
int, num_args, int, num_rets) {
5862+
if (lua_getglobal(ctx->L, funcname) != LUA_TFUNCTION) {
5863+
pr_err("function %s not found\n", funcname);
5864+
lua_pop(ctx->L, num_args);
5865+
return 0;
5866+
}
5867+
5868+
lua_insert(ctx->L, 1);
5869+
if (lua_pcall(ctx->L, num_args, num_rets, 0)) {
5870+
pr_err("%s\n", lua_tostring(ctx->L, -1));
5871+
lua_pop(ctx->L, 1);
5872+
return 0;
5873+
}
5874+
return num_rets;
5875+
}
5876+
5877+
static const struct bpf_func_proto bpf_lua_pcall_proto = {
5878+
.func = bpf_lua_pcall,
5879+
.gpl_only = false,
5880+
.pkt_access = false,
5881+
.ret_type = RET_INTEGER,
5882+
.arg1_type = ARG_PTR_TO_CTX,
5883+
.arg2_type = ARG_ANYTHING,
5884+
.arg3_type = RET_INTEGER,
5885+
.arg4_type = RET_INTEGER,
5886+
};
5887+
5888+
BPF_CALL_2(bpf_lua_pop, struct xdp_buff *, ctx, int, index) {
5889+
lua_pop(ctx->L, index);
5890+
return 0;
5891+
}
5892+
5893+
static const struct bpf_func_proto bpf_lua_pop_proto = {
5894+
.func = bpf_lua_pop,
5895+
.gpl_only = false,
5896+
.pkt_access = false,
5897+
.ret_type = RET_VOID,
5898+
.arg1_type = ARG_PTR_TO_CTX,
5899+
.arg2_type = ARG_ANYTHING,
5900+
};
5901+
5902+
BPF_CALL_2(bpf_lua_pushinteger, struct xdp_buff *, ctx, int, num) {
5903+
lua_pushinteger(ctx->L, num);
5904+
return 0;
5905+
}
5906+
5907+
static const struct bpf_func_proto bpf_lua_pushinteger_proto = {
5908+
.func = bpf_lua_pushinteger,
5909+
.gpl_only = false,
5910+
.pkt_access = false,
5911+
.ret_type = RET_VOID,
5912+
.arg1_type = ARG_PTR_TO_CTX,
5913+
.arg2_type = ARG_ANYTHING,
5914+
};
5915+
5916+
BPF_CALL_2(bpf_lua_pushlightuserdata, struct xdp_buff *, ctx, void *, ptr) {
5917+
lua_pushlightuserdata(ctx->L, ptr);
5918+
return 0;
5919+
}
5920+
5921+
static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = {
5922+
.func = bpf_lua_pushlightuserdata,
5923+
.gpl_only = false,
5924+
.pkt_access = false,
5925+
.ret_type = RET_VOID,
5926+
.arg1_type = ARG_PTR_TO_CTX,
5927+
.arg2_type = ARG_ANYTHING,
5928+
};
5929+
5930+
BPF_CALL_2(bpf_lua_pushmap, struct xdp_buff *, ctx, struct bpf_map *, map) {
5931+
lua_pushlightuserdata(ctx->L, map);
5932+
return 0;
5933+
}
5934+
5935+
BPF_CALL_3(bpf_lua_pushlstring, struct xdp_buff *, ctx, const char *, str, size_t, len) {
5936+
lua_pushlstring(ctx->L, str, len);
5937+
return 0;
5938+
}
5939+
5940+
static const struct bpf_func_proto bpf_lua_pushlstring_proto = {
5941+
.func = bpf_lua_pushlstring,
5942+
.gpl_only = false,
5943+
.pkt_access = false,
5944+
.ret_type = RET_VOID,
5945+
.arg1_type = ARG_PTR_TO_CTX,
5946+
.arg2_type = ARG_ANYTHING,
5947+
.arg3_type = ARG_ANYTHING,
5948+
};
5949+
5950+
static const struct bpf_func_proto bpf_lua_pushmap_proto = {
5951+
.func = bpf_lua_pushmap,
5952+
.gpl_only = false,
5953+
.pkt_access = false,
5954+
.ret_type = RET_VOID,
5955+
.arg1_type = ARG_PTR_TO_CTX,
5956+
.arg2_type = ARG_ANYTHING,
5957+
};
5958+
5959+
BPF_CALL_1(bpf_lua_pushskb, struct xdp_buff *, ctx) {
5960+
lua_pushlightuserdata(ctx->L, ctx->skb);
5961+
return 0;
5962+
}
5963+
5964+
static const struct bpf_func_proto bpf_lua_pushskb_proto = {
5965+
.func = bpf_lua_pushskb,
5966+
.gpl_only = false,
5967+
.pkt_access = false,
5968+
.ret_type = RET_VOID,
5969+
.arg1_type = ARG_PTR_TO_CTX,
5970+
};
5971+
5972+
BPF_CALL_2(bpf_lua_pushstring, struct xdp_buff *, ctx, const char *, str) {
5973+
lua_pushstring(ctx->L, str);
5974+
return 0;
5975+
}
5976+
5977+
static const struct bpf_func_proto bpf_lua_pushstring_proto = {
5978+
.func = bpf_lua_pushstring,
5979+
.gpl_only = false,
5980+
.pkt_access = false,
5981+
.ret_type = RET_VOID,
5982+
.arg1_type = ARG_PTR_TO_CTX,
5983+
.arg2_type = ARG_ANYTHING,
5984+
};
5985+
5986+
BPF_CALL_1(bpf_lua_setstate, struct xdp_buff *, ctx){
5987+
struct lua_state_cpu *sc;
5988+
int cpu = smp_processor_id();
5989+
5990+
list_for_each_entry(sc, &lua_state_cpu_list, list) {
5991+
if (sc->cpu == cpu) {
5992+
ctx->L = sc->L;
5993+
break;
5994+
}
5995+
}
5996+
return 0;
5997+
}
5998+
5999+
static const struct bpf_func_proto bpf_lua_setstate_proto = {
6000+
.func = bpf_lua_setstate,
6001+
.gpl_only = false,
6002+
.pkt_access = false,
6003+
.ret_type = RET_VOID,
6004+
.arg1_type = ARG_PTR_TO_CTX,
6005+
};
6006+
6007+
BPF_CALL_2(bpf_lua_toboolean, struct xdp_buff *, ctx, int, index) {
6008+
return lua_toboolean(ctx->L, index);
6009+
}
6010+
6011+
static const struct bpf_func_proto bpf_lua_toboolean_proto = {
6012+
.func = bpf_lua_toboolean,
6013+
.gpl_only = false,
6014+
.pkt_access = false,
6015+
.ret_type = RET_INTEGER,
6016+
.arg1_type = ARG_PTR_TO_CTX,
6017+
.arg2_type = ARG_ANYTHING,
6018+
};
6019+
6020+
BPF_CALL_2(bpf_lua_tointeger, struct xdp_buff *, ctx, int, index) {
6021+
return lua_tointeger(ctx->L, index);
6022+
}
6023+
6024+
static const struct bpf_func_proto bpf_lua_tointeger_proto = {
6025+
.func = bpf_lua_tointeger,
6026+
.gpl_only = false,
6027+
.pkt_access = false,
6028+
.ret_type = RET_INTEGER,
6029+
.arg1_type = ARG_PTR_TO_CTX,
6030+
.arg2_type = ARG_ANYTHING,
6031+
};
6032+
#endif /* CONFIG_XDPLUA */
6033+
58556034
bool bpf_helper_changes_pkt_data(void *func)
58566035
{
58576036
if (func == bpf_skb_vlan_push ||
@@ -5926,6 +6105,30 @@ bpf_base_func_proto(enum bpf_func_id func_id)
59266105
return &bpf_spin_unlock_proto;
59276106
case BPF_FUNC_trace_printk:
59286107
return bpf_get_trace_printk_proto();
6108+
#ifdef CONFIG_XDPLUA
6109+
case BPF_FUNC_lua_pcall:
6110+
return &bpf_lua_pcall_proto;
6111+
case BPF_FUNC_lua_pop:
6112+
return &bpf_lua_pop_proto;
6113+
case BPF_FUNC_lua_pushinteger:
6114+
return &bpf_lua_pushinteger_proto;
6115+
case BPF_FUNC_lua_pushlightuserdata:
6116+
return &bpf_lua_pushlightuserdata_proto;
6117+
case BPF_FUNC_lua_pushlstring:
6118+
return &bpf_lua_pushlstring_proto;
6119+
case BPF_FUNC_lua_pushmap:
6120+
return &bpf_lua_pushmap_proto;
6121+
case BPF_FUNC_lua_pushskb:
6122+
return &bpf_lua_pushskb_proto;
6123+
case BPF_FUNC_lua_pushstring:
6124+
return &bpf_lua_pushstring_proto;
6125+
case BPF_FUNC_lua_setstate:
6126+
return &bpf_lua_setstate_proto;
6127+
case BPF_FUNC_lua_toboolean:
6128+
return &bpf_lua_toboolean_proto;
6129+
case BPF_FUNC_lua_tointeger:
6130+
return &bpf_lua_tointeger_proto;
6131+
#endif /* CONFIG_XDPLUA */
59296132
default:
59306133
return NULL;
59316134
}

0 commit comments

Comments
 (0)