Skip to content

Commit ff7c9b9

Browse files
add XDPLua
1 parent 4d82fa2 commit ff7c9b9

16 files changed

Lines changed: 757 additions & 2 deletions

File tree

include/linux/filter.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,4 +1268,14 @@ struct bpf_sockopt_kern {
12681268
s32 retval;
12691269
};
12701270

1271+
#ifdef CONFIG_XDP_LUA
1272+
extern struct list_head lua_state_cpu_list;
1273+
1274+
struct lua_state_cpu {
1275+
struct lua_State *L;
1276+
int cpu;
1277+
struct list_head list;
1278+
};
1279+
#endif /* CONFIG_XDP_LUA */
1280+
12711281
#endif /* __LINUX_FILTER_H__ */

include/linux/netdevice.h

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

3775+
#ifdef CONFIG_XDP_LUA
3776+
int generic_xdp_lua_install_prog(char *lua_prog);
3777+
#endif /* CONFIG_XDP_LUA */
3778+
37753779
int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
37763780
int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
37773781
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: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2890,6 +2890,73 @@ union bpf_attr {
28902890
* Obtain the 64bit jiffies
28912891
* Return
28922892
* The 64 bit jiffies
2893+
*
2894+
* void bpf_lua_pcall(void *ctx, char *funcname, int num_args, int num_rets)
2895+
* Description
2896+
* Calls Lua function funcname with the given nargs arguments in protected mode
2897+
*
2898+
* void bpf_lua_pop(void *ctx, int n)
2899+
* Description
2900+
* Pops n elements from the Lua stack
2901+
*
2902+
* void bpf_lua_pushinteger(void *ctx, int num)
2903+
* Description
2904+
* Pushes an integer with value n onto the Lua stack.
2905+
*
2906+
* void bpf_lua_pushlightuserdata(void *ctx, void *ptr)
2907+
* Description
2908+
* Pushes a light userdata onto the Lua stack.
2909+
* Userdata represent C values in Lua.
2910+
* A light userdata represents a pointer, a void*.
2911+
* It is a value (like a number): you do not create it,
2912+
* it has no individual metatable, and it is not collected
2913+
* (as it was never created).
2914+
* A light userdata is equal to "any" light userdata with
2915+
* the same C address.
2916+
*
2917+
* void bpf_lua_pushlstring(void *ctx, const char *s, size_t len)
2918+
* Description
2919+
* Pushes the string pointed to by s with size len onto the stack.
2920+
* Lua makes (or reuses) an internal copy of the given string,
2921+
* so the memory at s can be freed or reused immediately after the
2922+
* function returns.
2923+
* The string can contain any binary data, including embedded zeros.
2924+
*
2925+
* void bpf_lua_pushmap(void *ctx, void *map)
2926+
* Description
2927+
* Pushes a BPF map onto the Lua stack
2928+
*
2929+
* void bpf_lua_pushskb(void *ctx)
2930+
* Description
2931+
* Pushes an SKB structure onto the Lua stack
2932+
*
2933+
* void bpf_lua_pushstring(void *ctx, const char *s)
2934+
* Description
2935+
* Pushes the zero-terminated string pointed to by s onto the stack.
2936+
* Lua makes (or reuses) an internal copy of the given string,
2937+
* so the memory at s can be freed or reused immediately after the
2938+
* function returns.
2939+
*
2940+
* void bpf_lua_setstate(void *ctx)
2941+
* Description
2942+
* Sets the Lua state pointer in the context struct
2943+
*
2944+
* int bpf_lua_toboolean(void *ctx, int index)
2945+
* Description
2946+
* Converts the Lua value at the given index to a C
2947+
* boolean value (0 or 1)
2948+
* Return
2949+
* 1 if the value in the given index of the Lua stack is
2950+
* different from from false or null, otherwise returns 0
2951+
*
2952+
* int bpf_lua_tointeger(void *ctx, int index)
2953+
* Description
2954+
* Converts the Lua value at the given index of the Lua stack
2955+
* to the signed integral type lua_Integer.
2956+
* Return
2957+
* The converted Lua value at the given index, if the value is
2958+
* convertible to an integer(see the Lua manual for more details
2959+
* on type conversion); otherwise returns 0
28932960
*/
28942961
#define __BPF_FUNC_MAPPER(FN) \
28952962
FN(unspec), \
@@ -3010,7 +3077,20 @@ union bpf_attr {
30103077
FN(probe_read_kernel_str), \
30113078
FN(tcp_send_ack), \
30123079
FN(send_signal_thread), \
3013-
FN(jiffies64),
3080+
FN(jiffies64), \
3081+
/* #ifdef CONFIG_XDP_LUA */ \
3082+
FN(lua_pcall), \
3083+
FN(lua_pop), \
3084+
FN(lua_pushinteger), \
3085+
FN(lua_pushlightuserdata), \
3086+
FN(lua_pushlstring), \
3087+
FN(lua_pushmap), \
3088+
FN(lua_pushskb), \
3089+
FN(lua_pushstring), \
3090+
FN(lua_setstate), \
3091+
FN(lua_toboolean), \
3092+
FN(lua_tointeger),
3093+
/* #endif CONFIG_XDP_LUA */
30143094

30153095
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
30163096
* 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
@@ -984,6 +984,9 @@ enum {
984984
IFLA_XDP_DRV_PROG_ID,
985985
IFLA_XDP_SKB_PROG_ID,
986986
IFLA_XDP_HW_PROG_ID,
987+
#ifdef CONFIG_XDP_LUA
988+
IFLA_XDP_LUA_PROG,
989+
#endif /* CONFIG_XDP_LUA */
987990
__IFLA_XDP_MAX,
988991
};
989992

net/core/dev.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
* - netif_rx() feedback
6969
*/
7070

71+
#ifdef CONFIG_XDP_LUA
72+
#include <lua.h>
73+
#include <lauxlib.h>
74+
#include <lualib.h>
75+
#endif /* CONFIG_XDP_LUA */
76+
7177
#include <linux/uaccess.h>
7278
#include <linux/bitops.h>
7379
#include <linux/capability.h>
@@ -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_XDP_LUA
174+
struct list_head lua_state_cpu_list;
175+
#endif /* CONFIG_XDP_LUA */
176+
167177
/*
168178
* The @dev_base_head list is protected by @dev_base_lock and the rtnl
169179
* semaphore.
@@ -4556,6 +4566,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
45564566

45574567
rxqueue = netif_get_rxqueue(skb);
45584568
xdp->rxq = &rxqueue->xdp_rxq;
4569+
#ifdef CONFIG_XDP_LUA
4570+
xdp->skb = skb;
4571+
#endif /* CONFIG_XDP_LUA */
45594572

45604573
act = bpf_prog_run_xdp(xdp_prog, xdp);
45614574

@@ -5366,6 +5379,22 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
53665379
return ret;
53675380
}
53685381

5382+
#ifdef CONFIG_XDP_LUA
5383+
int generic_xdp_lua_install_prog(char *lua_prog)
5384+
{
5385+
struct lua_state_cpu *sc;
5386+
5387+
list_for_each_entry(sc, &lua_state_cpu_list, list) {
5388+
if (luaL_dostring(sc->L, lua_prog)) {
5389+
pr_err(KERN_INFO "error: %s\nOn cpu: %d\n",
5390+
lua_tostring(sc->L, -1), sc->cpu);
5391+
return -EINVAL;
5392+
}
5393+
}
5394+
return 0;
5395+
}
5396+
#endif /* CONFIG_XDP_LUA */
5397+
53695398
static int netif_receive_skb_internal(struct sk_buff *skb)
53705399
{
53715400
int ret;
@@ -10462,6 +10491,9 @@ static struct pernet_operations __net_initdata default_device_ops = {
1046210491
static int __init net_dev_init(void)
1046310492
{
1046410493
int i, rc = -ENOMEM;
10494+
#ifdef CONFIG_XDP_LUA
10495+
struct lua_state_cpu *new_state_cpu;
10496+
#endif /* CONFIG_XDP_LUA */
1046510497

1046610498
BUG_ON(!dev_boot_phase);
1046710499

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

1047810510
INIT_LIST_HEAD(&offload_base);
10511+
#ifdef CONFIG_XDP_LUA
10512+
INIT_LIST_HEAD(&lua_state_cpu_list);
10513+
#endif /* CONFIG_XDP_LUA */
1047910514

1048010515
if (register_pernet_subsys(&netdev_net_ops))
1048110516
goto out;
@@ -10506,6 +10541,25 @@ static int __init net_dev_init(void)
1050610541
init_gro_hash(&sd->backlog);
1050710542
sd->backlog.poll = process_backlog;
1050810543
sd->backlog.weight = weight_p;
10544+
10545+
#ifdef CONFIG_XDP_LUA
10546+
new_state_cpu = (struct lua_state_cpu *)
10547+
kmalloc(sizeof(struct lua_state_cpu), GFP_ATOMIC);
10548+
if (!new_state_cpu)
10549+
continue;
10550+
10551+
new_state_cpu->L = luaL_newstate();
10552+
if (!new_state_cpu->L) {
10553+
kfree(new_state_cpu);
10554+
continue;
10555+
}
10556+
10557+
luaL_openlibs(new_state_cpu->L);
10558+
lua_pop(new_state_cpu->L, 1);
10559+
new_state_cpu->cpu = i;
10560+
10561+
list_add(&new_state_cpu->list, &lua_state_cpu_list);
10562+
#endif /* CONFIG_XDP_LUA */
1050910563
}
1051010564

1051110565
dev_boot_phase = 0;

0 commit comments

Comments
 (0)