diff --git a/.gitmodules b/.gitmodules index d17f05c06fdfa0..3b57405d4b5084 100644 --- a/.gitmodules +++ b/.gitmodules @@ -10,3 +10,6 @@ [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 diff --git a/include/linux/filter.h b/include/linux/filter.h index d7166f205c50fc..92c6e31fb008ee 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1229,14 +1229,4 @@ struct bpf_sockopt_kern { s32 retval; }; -#ifdef CONFIG_XDPLUA -extern struct list_head lua_state_cpu_list; - -struct lua_state_cpu { - struct lua_State *L; - int cpu; - struct list_head list; -}; -#endif /* CONFIG_XDPLUA */ - #endif /* __LINUX_FILTER_H__ */ diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 45d3ba9b3f879a..b33126abfca6de 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -3684,9 +3684,9 @@ 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_XDPLUA +#ifdef CONFIG_XDP_LUA int generic_xdp_lua_install_prog(char *lua_prog); -#endif /* CONFIG_XDPLUA */ +#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); diff --git a/include/net/xdp.h b/include/net/xdp.h index 5c8c204adc827a..8c38a85ebff132 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -63,6 +63,19 @@ 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; @@ -70,10 +83,10 @@ struct xdp_buff { void *data_hard_start; unsigned long handle; struct xdp_rxq_info *rxq; -#ifdef CONFIG_XDPLUA +#ifdef CONFIG_XDP_LUA struct sk_buff *skb; struct lua_State *L; -#endif /* CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ }; struct xdp_frame { diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 095257ff526151..88c5f377d90221 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2825,10 +2825,11 @@ union bpf_attr { FN(sk_storage_get), \ FN(sk_storage_delete), \ FN(send_signal), \ - /* #ifdef CONFIG_XDPLUA */ \ + /* #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), \ @@ -2836,10 +2837,11 @@ union bpf_attr { FN(lua_pushmap), \ FN(lua_pushskb), \ FN(lua_pushstring), \ - FN(lua_setstate), \ FN(lua_toboolean), \ - FN(lua_tointeger), - /* #endif CONFIG_XDPLUA */ + 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 diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h index 52b7c0fef51bb0..09a614981d9b71 100644 --- a/include/uapi/linux/if_link.h +++ b/include/uapi/linux/if_link.h @@ -974,9 +974,9 @@ enum { IFLA_XDP_DRV_PROG_ID, IFLA_XDP_SKB_PROG_ID, IFLA_XDP_HW_PROG_ID, -#ifdef CONFIG_XDPLUA +#ifdef CONFIG_XDP_LUA IFLA_XDP_LUA_PROG, -#endif /* CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ __IFLA_XDP_MAX, }; diff --git a/init/Kconfig b/init/Kconfig index bd7d650d4a996c..bcfb14fb724df0 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1288,6 +1288,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 diff --git a/lib/Makefile b/lib/Makefile index 03d53106a3aca2..c0f8b32f40c941 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -296,9 +296,10 @@ obj-$(CONFIG_GENERIC_LIB_UCMPDI2) += ucmpdi2.o obj-$(CONFIG_OBJAGG) += objagg.o subdir-ccflags-y += -I$(srctree)/lib/lunatik/lua \ - -I$(srctree)/lib/luadata/ \ + -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/ diff --git a/lib/luarcu b/lib/luarcu index a205daf9379b68..bc563d5245afc8 160000 --- a/lib/luarcu +++ b/lib/luarcu @@ -1 +1 @@ -Subproject commit a205daf9379b68f496eb8c31b9fac772b89da9cc +Subproject commit bc563d5245afc8024dd3f47fd8da404a781e0215 diff --git a/lib/luaunpack b/lib/luaunpack new file mode 160000 index 00000000000000..0589fc254bedaa --- /dev/null +++ b/lib/luaunpack @@ -0,0 +1 @@ +Subproject commit 0589fc254bedaa648042710a539a1db8cfa3445d diff --git a/net/core/Makefile b/net/core/Makefile index 06c6bf007b4ff2..eea1f004039926 100644 --- a/net/core/Makefile +++ b/net/core/Makefile @@ -8,8 +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 -CFLAGS_filter.o = -Ilib/lunatik/lua/ -D_KERNEL +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 \ diff --git a/net/core/dev.c b/net/core/dev.c index d8a1d352a16357..36fc3646d42d50 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -68,6 +68,14 @@ * - netif_rx() feedback */ +#ifdef CONFIG_XDP_LUA +#include +#include +#include +#include +#include +#endif /* CONFIG_XDP_LUA */ + #include #include #include @@ -143,12 +151,6 @@ #include #include -#ifdef CONFIG_XDPLUA -#include -#include -#include -#endif /* CONFIG_XDPLUA */ - #include "net-sysfs.h" #define MAX_GRO_SKBS 8 @@ -158,6 +160,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; @@ -170,10 +175,6 @@ 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_XDPLUA -struct list_head lua_state_cpu_list; -#endif /* CONFIG_XDPLUA */ - /* * The @dev_base_head list is protected by @dev_base_lock and the rtnl * semaphore. @@ -4337,6 +4338,9 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb, bool orig_bcast; 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. @@ -4381,12 +4385,22 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb, rxqueue = netif_get_rxqueue(skb); xdp->rxq = &rxqueue->xdp_rxq; -#ifdef CONFIG_XDPLUA +#ifdef CONFIG_XDP_LUA + lw = this_cpu_ptr(&luaworks); + xdp->skb = skb; -#endif /* CONFIG_XDPLUA */ + 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 */ + /* check if bpf_xdp_adjust_head was used */ off = xdp->data - orig_data; if (off) { @@ -5196,21 +5210,34 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) return ret; } -#ifdef CONFIG_XDPLUA -int generic_xdp_lua_install_prog(char *lua_prog) +#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) { - struct lua_state_cpu *sc; + int cpu; + struct xdplua_create_work *lw; - 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; - } + 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_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ static int netif_receive_skb_internal(struct sk_buff *skb) { @@ -9829,9 +9856,6 @@ static struct pernet_operations __net_initdata default_device_ops = { static int __init net_dev_init(void) { int i, rc = -ENOMEM; -#ifdef CONFIG_XDPLUA - struct lua_state_cpu *new_state_cpu; -#endif /* CONFIG_XDPLUA */ BUG_ON(!dev_boot_phase); @@ -9846,9 +9870,6 @@ static int __init net_dev_init(void) INIT_LIST_HEAD(&ptype_base[i]); INIT_LIST_HEAD(&offload_base); -#ifdef CONFIG_XDPLUA - INIT_LIST_HEAD(&lua_state_cpu_list); -#endif /* CONFIG_XDPLUA */ if (register_pernet_subsys(&netdev_net_ops)) goto out; @@ -9860,6 +9881,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); @@ -9879,26 +9903,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); -#ifdef CONFIG_XDPLUA - new_state_cpu = (struct lua_state_cpu *) - kmalloc(sizeof(lua_state_cpu), GFP_ATOMIC); - if (!new_state_cpu) + if (!lw->L) continue; - new_state_cpu->L = luaL_newstate(); - if (!new_state_cpu->L) { - kfree(new_state_cpu); - continue; - } - - luaL_openlibs(new_state_cpu->L); - luaL_requiref(new_state_cpu->L, "data", luaopen_data, 1); - lua_pop(new_state_cpu->L, 1); - new_state_cpu->cpu = i; + luaL_openlibs(lw->L); + luaL_requiref(lw->L, "data", luaopen_data, 1); + lua_pop(lw->L, 1); - list_add(&new_state_cpu->list, &lua_state_cpu_list); -#endif /* CONFIG_XDPLUA */ + INIT_WORK(&lw->work, per_cpu_xdp_lua_install); +#endif /* CONFIG_XDP_LUA */ } dev_boot_phase = 0; diff --git a/net/core/filter.c b/net/core/filter.c index 56122004919790..2e03bfad0e5a85 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -74,9 +74,11 @@ #include #include -#ifdef CONFIG_XDPLUA +#ifdef CONFIG_XDP_LUA #include -#endif /* CONFIG_XDPLUA */ +#include +#include +#endif /* CONFIG_XDP_LUA */ /** * sk_filter_trim_cap - run a packet through a socket filter @@ -5856,11 +5858,23 @@ static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = { #endif /* CONFIG_INET */ -#ifdef CONFIG_XDPLUA +#ifdef CONFIG_XDP_LUA + +static inline void verify_and_lock(void) { + struct xdplua_create_work *lw; + + lw = this_cpu_ptr(&luaworks); + if (!lw->init) { + lw->init = true; + spin_lock(&lw->lock); + } +} + BPF_CALL_2(bpf_lua_dataref, struct xdp_buff *, ctx, int, offset) { if (offset + ctx->data < ctx->data_end) { int data_ref; + verify_and_lock(); data_ref = ldata_newref(ctx->L, ctx->data + offset, ctx->data_end - ctx->data - offset); return data_ref; @@ -5879,6 +5893,7 @@ static const struct bpf_func_proto bpf_lua_dataref_proto = { }; BPF_CALL_2(bpf_lua_dataunref, struct xdp_buff *, ctx, int, data_ref) { + verify_and_lock(); ldata_unref(ctx->L, data_ref); return 0; } @@ -5894,18 +5909,28 @@ static const struct bpf_func_proto bpf_lua_dataunref_proto = { BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname, int, num_args, int, num_rets) { + int base; + + verify_and_lock(); + + base = lua_gettop(ctx->L) - num_args; if (lua_getglobal(ctx->L, funcname) != LUA_TFUNCTION) { pr_err("function %s not found\n", funcname); - lua_pop(ctx->L, num_args); - return 0; + num_rets = 0; + goto clean_state; } - lua_insert(ctx->L, 1); + lua_insert(ctx->L, base + 1); if (lua_pcall(ctx->L, num_args, num_rets, 0)) { pr_err("%s\n", lua_tostring(ctx->L, -1)); - lua_pop(ctx->L, 1); - return 0; + num_rets = 0; + goto clean_state; } + + base += num_rets; + +clean_state: + lua_settop(ctx->L, base); return num_rets; } @@ -5921,6 +5946,7 @@ static const struct bpf_func_proto bpf_lua_pcall_proto = { }; BPF_CALL_2(bpf_lua_pop, struct xdp_buff *, ctx, int, index) { + verify_and_lock(); lua_pop(ctx->L, index); return 0; } @@ -5935,6 +5961,7 @@ static const struct bpf_func_proto bpf_lua_pop_proto = { }; BPF_CALL_2(bpf_lua_pushinteger, struct xdp_buff *, ctx, int, num) { + verify_and_lock(); lua_pushinteger(ctx->L, num); return 0; } @@ -5949,6 +5976,7 @@ static const struct bpf_func_proto bpf_lua_pushinteger_proto = { }; BPF_CALL_2(bpf_lua_pushlightuserdata, struct xdp_buff *, ctx, void *, ptr) { + verify_and_lock(); lua_pushlightuserdata(ctx->L, ptr); return 0; } @@ -5963,11 +5991,13 @@ static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = { }; BPF_CALL_2(bpf_lua_pushmap, struct xdp_buff *, ctx, struct bpf_map *, map) { + verify_and_lock(); lua_pushlightuserdata(ctx->L, map); return 0; } BPF_CALL_3(bpf_lua_pushlstring, struct xdp_buff *, ctx, const char *, str, size_t, len) { + verify_and_lock(); lua_pushlstring(ctx->L, str, len); return 0; } @@ -5992,6 +6022,7 @@ static const struct bpf_func_proto bpf_lua_pushmap_proto = { }; BPF_CALL_1(bpf_lua_pushskb, struct xdp_buff *, ctx) { + verify_and_lock(); lua_pushlightuserdata(ctx->L, ctx->skb); return 0; } @@ -6005,6 +6036,7 @@ static const struct bpf_func_proto bpf_lua_pushskb_proto = { }; BPF_CALL_2(bpf_lua_pushstring, struct xdp_buff *, ctx, const char *, str) { + verify_and_lock(); lua_pushstring(ctx->L, str); return 0; } @@ -6018,28 +6050,8 @@ static const struct bpf_func_proto bpf_lua_pushstring_proto = { .arg2_type = ARG_ANYTHING, }; -BPF_CALL_1(bpf_lua_setstate, struct xdp_buff *, ctx){ - struct lua_state_cpu *sc; - int cpu = smp_processor_id(); - - list_for_each_entry(sc, &lua_state_cpu_list, list) { - if (sc->cpu == cpu) { - ctx->L = sc->L; - break; - } - } - return 0; -} - -static const struct bpf_func_proto bpf_lua_setstate_proto = { - .func = bpf_lua_setstate, - .gpl_only = false, - .pkt_access = false, - .ret_type = RET_VOID, - .arg1_type = ARG_PTR_TO_CTX, -}; - BPF_CALL_2(bpf_lua_toboolean, struct xdp_buff *, ctx, int, index) { + verify_and_lock(); return lua_toboolean(ctx->L, index); } @@ -6053,6 +6065,7 @@ static const struct bpf_func_proto bpf_lua_toboolean_proto = { }; BPF_CALL_2(bpf_lua_tointeger, struct xdp_buff *, ctx, int, index) { + verify_and_lock(); return lua_tointeger(ctx->L, index); } @@ -6064,7 +6077,61 @@ static const struct bpf_func_proto bpf_lua_tointeger_proto = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, }; -#endif /* CONFIG_XDPLUA */ + +BPF_CALL_4(bpf_lua_tostring, struct xdp_buff *, ctx, char *, str, u32, size, int, index) { + verify_and_lock(); + if (lua_isstring(ctx->L, index)) { + strncpy(str, lua_tostring(ctx->L, index), size); + return 1; + } + + return 0; +} + +static const struct bpf_func_proto bpf_lua_tostring_proto = { + .func = bpf_lua_tostring, + .gpl_only = false, + .pkt_access = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_UNINIT_MEM, + .arg3_type = ARG_CONST_SIZE, + .arg4_type = ARG_ANYTHING, +}; + +BPF_CALL_2(bpf_lua_newpacket, struct xdp_buff *, ctx, int, offset) { + if (offset + ctx->data < ctx->data_end) { + verify_and_lock(); + return lunpack_newpacket(ctx->L, ctx->data + offset, + ctx->data_end - ctx->data - offset); + } + + return -EINVAL; +} + +static const struct bpf_func_proto bpf_lua_newpacket_proto = { + .func = bpf_lua_newpacket, + .gpl_only = false, + .pkt_access = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_ANYTHING, +}; + +BPF_CALL_2(bpf_lua_type, struct xdp_buff *, ctx, int, index) { + verify_and_lock(); + return lua_type(ctx->L, index); +} + +static const struct bpf_func_proto bpf_lua_type_proto = { + .func = bpf_lua_type, + .gpl_only = false, + .pkt_access = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_ANYTHING, +}; +#endif /* CONFIG_XDP_LUA */ bool bpf_helper_changes_pkt_data(void *func) { @@ -6140,7 +6207,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_spin_unlock_proto; case BPF_FUNC_trace_printk: return bpf_get_trace_printk_proto(); -#ifdef CONFIG_XDPLUA +#ifdef CONFIG_XDP_LUA case BPF_FUNC_lua_dataref: return &bpf_lua_dataref_proto; case BPF_FUNC_lua_dataunref: @@ -6161,13 +6228,17 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_lua_pushskb_proto; case BPF_FUNC_lua_pushstring: return &bpf_lua_pushstring_proto; - case BPF_FUNC_lua_setstate: - return &bpf_lua_setstate_proto; case BPF_FUNC_lua_toboolean: return &bpf_lua_toboolean_proto; + case BPF_FUNC_lua_tostring: + return &bpf_lua_tostring_proto; case BPF_FUNC_lua_tointeger: return &bpf_lua_tointeger_proto; -#endif /* CONFIG_XDPLUA */ + case BPF_FUNC_lua_newpacket: + return &bpf_lua_newpacket_proto; + case BPF_FUNC_lua_type: + return &bpf_lua_type_proto; +#endif /* CONFIG_XDP_LUA */ default: return NULL; } diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 5232592d8f1f5f..3556e33025d456 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -1799,9 +1799,9 @@ static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, -#ifdef CONFIG_XDPLUA +#ifdef CONFIG_XDP_LUA [IFLA_XDP_LUA_PROG] = { .type = NLA_STRING, .len = 8192 }, -#endif /* CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ }; static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla) @@ -2711,7 +2711,7 @@ static int do_setlink(const struct sk_buff *skb, status |= DO_SETLINK_NOTIFY; } -#ifdef CONFIG_XDPLUA +#ifdef CONFIG_XDP_LUA if (xdp[IFLA_XDP_LUA_PROG]) { char *lua_prog = nla_data(xdp[IFLA_XDP_LUA_PROG]); if (!lua_prog) { @@ -2723,7 +2723,7 @@ static int do_setlink(const struct sk_buff *skb, if (err) goto errout; } -#endif /* CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ } diff --git a/net/xdp/Kconfig b/net/xdp/Kconfig index 71af2febe72adf..0c77ed4f41fdd2 100644 --- a/net/xdp/Kconfig +++ b/net/xdp/Kconfig @@ -14,3 +14,10 @@ config XDP_SOCKETS_DIAG help Support for PF_XDP sockets monitoring interface used by the ss tool. If unsure, say Y. + +config XDP_LUA + bool "XDP LUA" + depends on BPF_SYSCALL && LUNATIK + default n + help + Support for Lua scripts inside XDP diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index f1b7a3c55a589e..110cacb2d568cc 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -53,6 +53,7 @@ hostprogs-y += task_fd_query hostprogs-y += xdp_sample_pkts hostprogs-y += ibumad hostprogs-y += hbm +# CONFIG_XDP_LUA hostprogs-y += xdplua # Libbpf dependencies @@ -110,7 +111,7 @@ task_fd_query-objs := bpf_load.o task_fd_query_user.o $(TRACE_HELPERS) xdp_sample_pkts-objs := xdp_sample_pkts_user.o $(TRACE_HELPERS) ibumad-objs := bpf_load.o ibumad_user.o $(TRACE_HELPERS) hbm-objs := bpf_load.o hbm.o $(CGROUP_HELPERS) -# CONFIG_XDPLUA +# CONFIG_XDP_LUA xdplua-objs := xdplua_user.o # Tell kbuild to always build the programs @@ -173,7 +174,7 @@ always += xdp_sample_pkts_kern.o always += ibumad_kern.o always += hbm_out_kern.o always += hbm_edt_kern.o -# CONFIG_XDPLUA +# CONFIG_XDP_LUA always += xdplua_map_kern.o KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include @@ -223,6 +224,7 @@ BTF_LLVM_PROBE := $(shell echo "int main() { return 0; }" | \ readelf -S ./llvm_btf_verify.o | grep BTF; \ /bin/rm -f ./llvm_btf_verify.o) +EXTRA_CFLAGS += -fno-stack-protector ifneq ($(BTF_LLVM_PROBE),) EXTRA_CFLAGS += -g else diff --git a/samples/bpf/map.lua b/samples/bpf/map.lua index a6ac31a47f5544..aa6e733424a8d2 100644 --- a/samples/bpf/map.lua +++ b/samples/bpf/map.lua @@ -13,10 +13,10 @@ xdp = require'xdp' function lookup(map) - local val = xdp.maplookup(map, 1) + local val = xdp.map_lookup(map, 1) print('val', val) end function update(map) - xdplua.mapupdate(map, 1, 3) + xdp.map_update(map, 1, 3) end diff --git a/samples/bpf/xdplua_map_kern.c b/samples/bpf/xdplua_map_kern.c index ffbf4b8ebb4f91..d68d20732bb754 100644 --- a/samples/bpf/xdplua_map_kern.c +++ b/samples/bpf/xdplua_map_kern.c @@ -21,13 +21,12 @@ struct bpf_map_def SEC("maps") test_map = { .max_entries = 20, }; -SEC("test_map") -int xdp_test_map(struct xdp_md *ctx) +SEC("xdp_lua_test_map") +int xdp_lua_test_map_prog(struct xdp_md *ctx) { char lookupname[] = "lookup"; char updatename[] = "update"; - bpf_lua_setstate(ctx); bpf_lua_pushmap(ctx, &test_map); bpf_lua_pcall(ctx, updatename, 1, 0); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index b074d4a0be4ef6..7e792e4eb42d9c 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2824,10 +2824,12 @@ union bpf_attr { FN(strtoul), \ FN(sk_storage_get), \ FN(sk_storage_delete), \ + FN(send_signal), \ /* #ifdef CONFIG_XDPLUA */ \ FN(lua_dataref), \ FN(lua_dataunref), \ FN(lua_pcall), \ + FN(lua_tostring), \ FN(lua_pop), \ FN(lua_pushinteger), \ FN(lua_pushlightuserdata), \ @@ -2835,9 +2837,10 @@ union bpf_attr { FN(lua_pushmap), \ FN(lua_pushskb), \ FN(lua_pushstring), \ - FN(lua_setstate), \ FN(lua_toboolean), \ - FN(lua_tointeger), + FN(lua_tointeger), \ + FN(lua_newpacket), \ + FN(lua_type), /* #endif CONFIG_XDPLUA */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/tools/include/uapi/linux/if_link.h b/tools/include/uapi/linux/if_link.h index 52b7c0fef51bb0..4b213ec3bfe19f 100644 --- a/tools/include/uapi/linux/if_link.h +++ b/tools/include/uapi/linux/if_link.h @@ -974,9 +974,9 @@ enum { IFLA_XDP_DRV_PROG_ID, IFLA_XDP_SKB_PROG_ID, IFLA_XDP_HW_PROG_ID, -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDP_LUA */ IFLA_XDP_LUA_PROG, -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDP_LUA */ __IFLA_XDP_MAX, }; diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index 6bc9919ae73c88..7851be402497ae 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -229,14 +229,16 @@ static int (*bpf_sk_storage_delete)(void *map, struct bpf_sock *sk) = (void *)BPF_FUNC_sk_storage_delete; static int (*bpf_send_signal)(unsigned sig) = (void *)BPF_FUNC_send_signal; -/* #fidef CONFIG_XDPLUA */ +/* #ifdef CONFIG_XDPLUA */ static int (*bpf_lua_dataref)(void *ctx, int offset) = (void *)BPF_FUNC_lua_dataref; static void (*bpf_lua_dataunref)(void *ctx, int data_ref) = (void *)BPF_FUNC_lua_dataunref; -static void (*bpf_lua_pcall)(void *ctx, char *funcname, int num_args, +static int (*bpf_lua_pcall)(void *ctx, char *funcname, int num_args, int num_rets) = (void *) BPF_FUNC_lua_pcall; +static int (*bpf_lua_tostring)(void *ctx, char *sslsni, u32 size, int index) = + (void *)BPF_FUNC_lua_tostring; static void (*bpf_lua_pop)(void *ctx, int index) = (void *)BPF_FUNC_lua_pop; static void (*bpf_lua_pushinteger)(void *ctx, int num) = @@ -252,12 +254,14 @@ static void (*bpf_lua_pushskb)(void *ctx) = (void *)BPF_FUNC_lua_pushskb; static void (*bpf_lua_pushstring)(void *ctx, const char *) = (void *)BPF_FUNC_lua_pushstring; -static void (*bpf_lua_setstate)(void *ctx) = - (void *)BPF_FUNC_lua_setstate; static int (*bpf_lua_toboolean)(void *ctx, int index) = (void *)BPF_FUNC_lua_toboolean; static int (*bpf_lua_tointeger)(void *ctx, int index) = (void *)BPF_FUNC_lua_tointeger; +static int (*bpf_lua_newpacket)(void *ctx, int offset) = + (void *)BPF_FUNC_lua_newpacket; +static int (*bpf_lua_type)(void *ctx, int index) = + (void *)BPF_FUNC_lua_type; /* #endif CONFIG_XDPLUA */ /* llvm builtin functions that eBPF C program may use to