Skip to content

Commit 459ecee

Browse files
add XDPLua
1 parent ea0abd3 commit 459ecee

File tree

16 files changed

+708
-2
lines changed

16 files changed

+708
-2
lines changed

include/linux/filter.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,4 +1099,13 @@ struct bpf_sock_ops_kern {
10991099
*/
11001100
};
11011101

1102+
#ifdef CONFIG_XDP_LUA
1103+
extern struct list_head lua_state_cpu_list;
1104+
1105+
struct lua_state_cpu {
1106+
struct lua_State *L;
1107+
int cpu;
1108+
struct list_head list;
1109+
};
1110+
#endif /* CONFIG_XDP_LUA */
11021111
#endif /* __LINUX_FILTER_H__ */

include/linux/netdevice.h

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

3626+
#ifdef CONFIG_XDP_LUA
3627+
int generic_xdp_lua_install_prog(char *lua_prog);
3628+
#endif /* CONFIG_XDP_LUA */
3629+
36263630
int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
36273631
int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
36283632
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_XDP_LUA
74+
struct sk_buff *skb;
75+
struct lua_State *L;
76+
#endif /* CONFIG_XDP_LUA */
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
@@ -2228,7 +2228,20 @@ union bpf_attr {
22282228
FN(get_current_cgroup_id), \
22292229
FN(get_local_storage), \
22302230
FN(sk_select_reuseport), \
2231-
FN(skb_ancestor_cgroup_id),
2231+
FN(skb_ancestor_cgroup_id), \
2232+
/* #ifdef CONFIG_XDP_LUA */ \
2233+
FN(lua_pcall), \
2234+
FN(lua_pop), \
2235+
FN(lua_pushinteger), \
2236+
FN(lua_pushlightuserdata), \
2237+
FN(lua_pushlstring), \
2238+
FN(lua_pushmap), \
2239+
FN(lua_pushskb), \
2240+
FN(lua_pushstring), \
2241+
FN(lua_setstate), \
2242+
FN(lua_toboolean), \
2243+
FN(lua_tointeger),
2244+
/* #endif CONFIG_XDP_LUA */
22322245

22332246
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
22342247
* 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
@@ -945,6 +945,9 @@ enum {
945945
IFLA_XDP_DRV_PROG_ID,
946946
IFLA_XDP_SKB_PROG_ID,
947947
IFLA_XDP_HW_PROG_ID,
948+
#ifdef CONFIG_XDP_LUA
949+
IFLA_XDP_LUA_PROG,
950+
#endif /* CONFIG_XDP_LUA */
948951
__IFLA_XDP_MAX,
949952
};
950953

net/core/dev.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
* - netif_rx() feedback
7373
*/
7474

75+
#ifdef CONFIG_XDP_LUA
76+
#include <lua.h>
77+
#include <lauxlib.h>
78+
#include <lualib.h>
79+
#endif /* CONFIG_XDP_LUA */
80+
7581
#include <linux/uaccess.h>
7682
#include <linux/bitops.h>
7783
#include <linux/capability.h>
@@ -166,6 +172,10 @@ static int call_netdevice_notifiers_info(unsigned long val,
166172
struct netdev_notifier_info *info);
167173
static struct napi_struct *napi_by_id(unsigned int napi_id);
168174

175+
#ifdef CONFIG_XDP_LUA
176+
struct list_head lua_state_cpu_list;
177+
#endif /* CONFIG_XDP_LUA */
178+
169179
/*
170180
* The @dev_base_head list is protected by @dev_base_lock and the rtnl
171181
* semaphore.
@@ -4343,6 +4353,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
43434353

43444354
rxqueue = netif_get_rxqueue(skb);
43454355
xdp->rxq = &rxqueue->xdp_rxq;
4356+
#ifdef CONFIG_XDP_LUA
4357+
xdp->skb = skb;
4358+
#endif /* CONFIG_XDP_LUA */
43464359

43474360
act = bpf_prog_run_xdp(xdp_prog, xdp);
43484361

@@ -5131,6 +5144,22 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
51315144
return ret;
51325145
}
51335146

5147+
#ifdef CONFIG_XDP_LUA
5148+
int generic_xdp_lua_install_prog(char *lua_prog)
5149+
{
5150+
struct lua_state_cpu *sc;
5151+
5152+
list_for_each_entry(sc, &lua_state_cpu_list, list) {
5153+
if (luaL_dostring(sc->L, lua_prog)) {
5154+
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
5155+
lua_tostring(sc->L, -1), sc->cpu);
5156+
return -EINVAL;
5157+
}
5158+
}
5159+
return 0;
5160+
}
5161+
#endif /* CONFIG_XDP_LUA */
5162+
51345163
static int netif_receive_skb_internal(struct sk_buff *skb)
51355164
{
51365165
int ret;
@@ -9801,6 +9830,9 @@ static struct pernet_operations __net_initdata default_device_ops = {
98019830
static int __init net_dev_init(void)
98029831
{
98039832
int i, rc = -ENOMEM;
9833+
#ifdef CONFIG_XDP_LUA
9834+
struct lua_state_cpu *new_state_cpu;
9835+
#endif /* CONFIG_XDP_LUA */
98049836

98059837
BUG_ON(!dev_boot_phase);
98069838

@@ -9815,6 +9847,9 @@ static int __init net_dev_init(void)
98159847
INIT_LIST_HEAD(&ptype_base[i]);
98169848

98179849
INIT_LIST_HEAD(&offload_base);
9850+
#ifdef CONFIG_XDP_LUA
9851+
INIT_LIST_HEAD(&lua_state_cpu_list);
9852+
#endif /* CONFIG_XDP_LUA */
98189853

98199854
if (register_pernet_subsys(&netdev_net_ops))
98209855
goto out;
@@ -9845,6 +9880,25 @@ static int __init net_dev_init(void)
98459880
init_gro_hash(&sd->backlog);
98469881
sd->backlog.poll = process_backlog;
98479882
sd->backlog.weight = weight_p;
9883+
9884+
#ifdef CONFIG_XDP_LUA
9885+
new_state_cpu = (struct lua_state_cpu *)
9886+
kmalloc(sizeof(struct lua_state_cpu), GFP_ATOMIC);
9887+
if (!new_state_cpu)
9888+
continue;
9889+
9890+
new_state_cpu->L = luaL_newstate();
9891+
if (!new_state_cpu->L) {
9892+
kfree(new_state_cpu);
9893+
continue;
9894+
}
9895+
9896+
luaL_openlibs(new_state_cpu->L);
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_XDP_LUA */
98489902
}
98499903

98509904
dev_boot_phase = 0;

net/core/filter.c

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
#include <net/seg6.h>
7070
#include <net/seg6_local.h>
7171

72+
#ifdef CONFIG_XDP_LUA
73+
#include <lua.h>
74+
#endif /* CONFIG_XDP_LUA */
75+
7276
/**
7377
* sk_filter_trim_cap - run a packet through a socket filter
7478
* @sk: sock associated with &sk_buff
@@ -4792,6 +4796,181 @@ static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = {
47924796
};
47934797
#endif /* CONFIG_IPV6_SEG6_BPF */
47944798

4799+
#ifdef CONFIG_XDP_LUA
4800+
BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname,
4801+
int, num_args, int, num_rets) {
4802+
if (lua_getglobal(ctx->L, funcname) != LUA_TFUNCTION) {
4803+
pr_err("function %s not found\n", funcname);
4804+
lua_pop(ctx->L, num_args);
4805+
return 0;
4806+
}
4807+
4808+
lua_insert(ctx->L, 1);
4809+
if (lua_pcall(ctx->L, num_args, num_rets, 0)) {
4810+
pr_err("%s\n", lua_tostring(ctx->L, -1));
4811+
lua_pop(ctx->L, 1);
4812+
return 0;
4813+
}
4814+
return num_rets;
4815+
}
4816+
4817+
static const struct bpf_func_proto bpf_lua_pcall_proto = {
4818+
.func = bpf_lua_pcall,
4819+
.gpl_only = false,
4820+
.pkt_access = false,
4821+
.ret_type = RET_INTEGER,
4822+
.arg1_type = ARG_PTR_TO_CTX,
4823+
.arg2_type = ARG_ANYTHING,
4824+
.arg3_type = RET_INTEGER,
4825+
.arg4_type = RET_INTEGER,
4826+
};
4827+
4828+
BPF_CALL_2(bpf_lua_pop, struct xdp_buff *, ctx, int, index) {
4829+
lua_pop(ctx->L, index);
4830+
return 0;
4831+
}
4832+
4833+
static const struct bpf_func_proto bpf_lua_pop_proto = {
4834+
.func = bpf_lua_pop,
4835+
.gpl_only = false,
4836+
.pkt_access = false,
4837+
.ret_type = RET_VOID,
4838+
.arg1_type = ARG_PTR_TO_CTX,
4839+
.arg2_type = ARG_ANYTHING,
4840+
};
4841+
4842+
BPF_CALL_2(bpf_lua_pushinteger, struct xdp_buff *, ctx, int, num) {
4843+
lua_pushinteger(ctx->L, num);
4844+
return 0;
4845+
}
4846+
4847+
static const struct bpf_func_proto bpf_lua_pushinteger_proto = {
4848+
.func = bpf_lua_pushinteger,
4849+
.gpl_only = false,
4850+
.pkt_access = false,
4851+
.ret_type = RET_VOID,
4852+
.arg1_type = ARG_PTR_TO_CTX,
4853+
.arg2_type = ARG_ANYTHING,
4854+
};
4855+
4856+
BPF_CALL_2(bpf_lua_pushlightuserdata, struct xdp_buff *, ctx, void *, ptr) {
4857+
lua_pushlightuserdata(ctx->L, ptr);
4858+
return 0;
4859+
}
4860+
4861+
static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = {
4862+
.func = bpf_lua_pushlightuserdata,
4863+
.gpl_only = false,
4864+
.pkt_access = false,
4865+
.ret_type = RET_VOID,
4866+
.arg1_type = ARG_PTR_TO_CTX,
4867+
.arg2_type = ARG_ANYTHING,
4868+
};
4869+
4870+
BPF_CALL_3(bpf_lua_pushlstring, struct xdp_buff *, ctx, const char *, str, size_t, len) {
4871+
lua_pushlstring(ctx->L, str, len);
4872+
return 0;
4873+
}
4874+
4875+
static const struct bpf_func_proto bpf_lua_pushlstring_proto = {
4876+
.func = bpf_lua_pushlstring,
4877+
.gpl_only = false,
4878+
.pkt_access = false,
4879+
.ret_type = RET_VOID,
4880+
.arg1_type = ARG_PTR_TO_CTX,
4881+
.arg2_type = ARG_ANYTHING,
4882+
.arg3_type = ARG_ANYTHING,
4883+
};
4884+
4885+
BPF_CALL_2(bpf_lua_pushmap, struct xdp_buff *, ctx, struct bpf_map *, map) {
4886+
lua_pushlightuserdata(ctx->L, map);
4887+
return 0;
4888+
}
4889+
4890+
static const struct bpf_func_proto bpf_lua_pushmap_proto = {
4891+
.func = bpf_lua_pushmap,
4892+
.gpl_only = false,
4893+
.pkt_access = false,
4894+
.ret_type = RET_VOID,
4895+
.arg1_type = ARG_PTR_TO_CTX,
4896+
.arg2_type = ARG_ANYTHING,
4897+
};
4898+
4899+
BPF_CALL_1(bpf_lua_pushskb, struct xdp_buff *, ctx) {
4900+
lua_pushlightuserdata(ctx->L, ctx->skb);
4901+
return 0;
4902+
}
4903+
4904+
static const struct bpf_func_proto bpf_lua_pushskb_proto = {
4905+
.func = bpf_lua_pushskb,
4906+
.gpl_only = false,
4907+
.pkt_access = false,
4908+
.ret_type = RET_VOID,
4909+
.arg1_type = ARG_PTR_TO_CTX,
4910+
};
4911+
4912+
BPF_CALL_2(bpf_lua_pushstring, struct xdp_buff *, ctx, const char *, str) {
4913+
lua_pushstring(ctx->L, str);
4914+
return 0;
4915+
}
4916+
4917+
static const struct bpf_func_proto bpf_lua_pushstring_proto = {
4918+
.func = bpf_lua_pushstring,
4919+
.gpl_only = false,
4920+
.pkt_access = false,
4921+
.ret_type = RET_VOID,
4922+
.arg1_type = ARG_PTR_TO_CTX,
4923+
.arg2_type = ARG_ANYTHING,
4924+
};
4925+
4926+
BPF_CALL_1(bpf_lua_setstate, struct xdp_buff *, ctx){
4927+
struct lua_state_cpu *sc;
4928+
int cpu = smp_processor_id();
4929+
4930+
list_for_each_entry(sc, &lua_state_cpu_list, list) {
4931+
if (sc->cpu == cpu) {
4932+
ctx->L = sc->L;
4933+
break;
4934+
}
4935+
}
4936+
return 0;
4937+
}
4938+
4939+
static const struct bpf_func_proto bpf_lua_setstate_proto = {
4940+
.func = bpf_lua_setstate,
4941+
.gpl_only = false,
4942+
.pkt_access = false,
4943+
.ret_type = RET_VOID,
4944+
.arg1_type = ARG_PTR_TO_CTX,
4945+
};
4946+
4947+
BPF_CALL_2(bpf_lua_toboolean, struct xdp_buff *, ctx, int, index) {
4948+
return lua_toboolean(ctx->L, index);
4949+
}
4950+
4951+
static const struct bpf_func_proto bpf_lua_toboolean_proto = {
4952+
.func = bpf_lua_toboolean,
4953+
.gpl_only = false,
4954+
.pkt_access = false,
4955+
.ret_type = RET_INTEGER,
4956+
.arg1_type = ARG_PTR_TO_CTX,
4957+
.arg2_type = ARG_ANYTHING,
4958+
};
4959+
4960+
BPF_CALL_2(bpf_lua_tointeger, struct xdp_buff *, ctx, int, index) {
4961+
return lua_tointeger(ctx->L, index);
4962+
}
4963+
4964+
static const struct bpf_func_proto bpf_lua_tointeger_proto = {
4965+
.func = bpf_lua_tointeger,
4966+
.gpl_only = false,
4967+
.pkt_access = false,
4968+
.ret_type = RET_INTEGER,
4969+
.arg1_type = ARG_PTR_TO_CTX,
4970+
.arg2_type = ARG_ANYTHING,
4971+
};
4972+
#endif /* CONFIG_XDP_LUA */
4973+
47954974
bool bpf_helper_changes_pkt_data(void *func)
47964975
{
47974976
if (func == bpf_skb_vlan_push ||
@@ -4847,6 +5026,30 @@ bpf_base_func_proto(enum bpf_func_id func_id)
48475026
if (capable(CAP_SYS_ADMIN))
48485027
return bpf_get_trace_printk_proto();
48495028
/* else: fall through */
5029+
#ifdef CONFIG_XDP_LUA
5030+
case BPF_FUNC_lua_pcall:
5031+
return &bpf_lua_pcall_proto;
5032+
case BPF_FUNC_lua_pop:
5033+
return &bpf_lua_pop_proto;
5034+
case BPF_FUNC_lua_pushinteger:
5035+
return &bpf_lua_pushinteger_proto;
5036+
case BPF_FUNC_lua_pushlightuserdata:
5037+
return &bpf_lua_pushlightuserdata_proto;
5038+
case BPF_FUNC_lua_pushlstring:
5039+
return &bpf_lua_pushlstring_proto;
5040+
case BPF_FUNC_lua_pushmap:
5041+
return &bpf_lua_pushmap_proto;
5042+
case BPF_FUNC_lua_pushskb:
5043+
return &bpf_lua_pushskb_proto;
5044+
case BPF_FUNC_lua_pushstring:
5045+
return &bpf_lua_pushstring_proto;
5046+
case BPF_FUNC_lua_setstate:
5047+
return &bpf_lua_setstate_proto;
5048+
case BPF_FUNC_lua_toboolean:
5049+
return &bpf_lua_toboolean_proto;
5050+
case BPF_FUNC_lua_tointeger:
5051+
return &bpf_lua_tointeger_proto;
5052+
#endif /* CONFIG_XDP_LUA */
48505053
default:
48515054
return NULL;
48525055
}

0 commit comments

Comments
 (0)