From ca0a05c8902a841011802479a0035847a43f97bf Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Tue, 11 Feb 2020 16:05:29 -0300 Subject: [PATCH 01/16] fixup! add XDPLua --- include/linux/filter.h | 4 +-- include/linux/netdevice.h | 4 +-- include/net/xdp.h | 4 +-- include/uapi/linux/if_link.h | 4 +-- net/core/dev.c | 39 +++++++++++++++--------------- net/core/filter.c | 12 ++++----- net/core/rtnetlink.c | 8 +++--- tools/include/uapi/linux/bpf.h | 1 + tools/include/uapi/linux/if_link.h | 4 +-- 9 files changed, 40 insertions(+), 40 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index d7166f205c50fc..1d3913f2eb57b1 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1229,7 +1229,7 @@ struct bpf_sockopt_kern { s32 retval; }; -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDPLUA */ extern struct list_head lua_state_cpu_list; struct lua_state_cpu { @@ -1237,6 +1237,6 @@ struct lua_state_cpu { int cpu; struct list_head list; }; -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ #endif /* __LINUX_FILTER_H__ */ diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 45d3ba9b3f879a..5b027a170cd2be 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_XDPLUA */ int generic_xdp_lua_install_prog(char *lua_prog); -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ 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..8025811447a538 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -70,10 +70,10 @@ struct xdp_buff { void *data_hard_start; unsigned long handle; struct xdp_rxq_info *rxq; -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDPLUA */ struct sk_buff *skb; struct lua_State *L; -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ }; struct xdp_frame { diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h index 52b7c0fef51bb0..e060ecaf51b3d6 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_XDPLUA */ IFLA_XDP_LUA_PROG, -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ __IFLA_XDP_MAX, }; diff --git a/net/core/dev.c b/net/core/dev.c index d8a1d352a16357..61bf2f216b828c 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -68,6 +68,12 @@ * - netif_rx() feedback */ +/* #ifdef CONFIG_XDPLUA */ +#include +#include +#include +/* #endif CONFIG_XDPLUA */ + #include #include #include @@ -143,12 +149,6 @@ #include #include -#ifdef CONFIG_XDPLUA -#include -#include -#include -#endif /* CONFIG_XDPLUA */ - #include "net-sysfs.h" #define MAX_GRO_SKBS 8 @@ -170,9 +170,9 @@ 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 +/* #ifdef CONFIG_XDPLUA */ struct list_head lua_state_cpu_list; -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ /* * The @dev_base_head list is protected by @dev_base_lock and the rtnl @@ -4381,9 +4381,9 @@ 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_XDPLUA */ xdp->skb = skb; -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ act = bpf_prog_run_xdp(xdp_prog, xdp); @@ -5196,7 +5196,7 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) return ret; } -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDPLUA */ int generic_xdp_lua_install_prog(char *lua_prog) { struct lua_state_cpu *sc; @@ -5210,7 +5210,7 @@ int generic_xdp_lua_install_prog(char *lua_prog) } return 0; } -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ static int netif_receive_skb_internal(struct sk_buff *skb) { @@ -9829,9 +9829,9 @@ static struct pernet_operations __net_initdata default_device_ops = { static int __init net_dev_init(void) { int i, rc = -ENOMEM; -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDPLUA */ struct lua_state_cpu *new_state_cpu; -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ BUG_ON(!dev_boot_phase); @@ -9846,9 +9846,9 @@ static int __init net_dev_init(void) INIT_LIST_HEAD(&ptype_base[i]); INIT_LIST_HEAD(&offload_base); -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDPLUA */ INIT_LIST_HEAD(&lua_state_cpu_list); -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ if (register_pernet_subsys(&netdev_net_ops)) goto out; @@ -9880,9 +9880,9 @@ static int __init net_dev_init(void) sd->backlog.poll = process_backlog; sd->backlog.weight = weight_p; -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDPLUA */ new_state_cpu = (struct lua_state_cpu *) - kmalloc(sizeof(lua_state_cpu), GFP_ATOMIC); + kmalloc(sizeof(struct lua_state_cpu), GFP_ATOMIC); if (!new_state_cpu) continue; @@ -9893,12 +9893,11 @@ static int __init net_dev_init(void) } 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; list_add(&new_state_cpu->list, &lua_state_cpu_list); -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ } dev_boot_phase = 0; diff --git a/net/core/filter.c b/net/core/filter.c index 56122004919790..a8c4f51d764431 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -74,9 +74,9 @@ #include #include -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDPLUA */ #include -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ /** * sk_filter_trim_cap - run a packet through a socket filter @@ -5856,7 +5856,7 @@ static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = { #endif /* CONFIG_INET */ -#ifdef CONFIG_XDPLUA +/* #ifdef CONFIG_XDPLUA */ BPF_CALL_2(bpf_lua_dataref, struct xdp_buff *, ctx, int, offset) { if (offset + ctx->data < ctx->data_end) { int data_ref; @@ -6064,7 +6064,7 @@ static const struct bpf_func_proto bpf_lua_tointeger_proto = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, }; -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ bool bpf_helper_changes_pkt_data(void *func) { @@ -6140,7 +6140,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_XDPLUA */ case BPF_FUNC_lua_dataref: return &bpf_lua_dataref_proto; case BPF_FUNC_lua_dataunref: @@ -6167,7 +6167,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_lua_toboolean_proto; case BPF_FUNC_lua_tointeger: return &bpf_lua_tointeger_proto; -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ default: return NULL; } diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 5232592d8f1f5f..96f9d692076f00 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_XDPLUA */ [IFLA_XDP_LUA_PROG] = { .type = NLA_STRING, .len = 8192 }, -#endif /* CONFIG_XDPLUA */ +/* #endif CONFIG_XDPLUA */ }; 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_XDPLUA */ 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_XDPLUA */ } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index b074d4a0be4ef6..095257ff526151 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2824,6 +2824,7 @@ 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), \ 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, }; From 00b981cc9604db60a32ce9ee620b498f28357de2 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Tue, 11 Feb 2020 16:40:15 -0300 Subject: [PATCH 02/16] fixup! add LuaData submodule --- net/core/Makefile | 6 ++++-- net/core/dev.c | 2 ++ net/core/filter.c | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/net/core/Makefile b/net/core/Makefile index 06c6bf007b4ff2..9f56a972205bb0 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/ 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 61bf2f216b828c..9bdc5aaeac6e9f 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -72,6 +72,7 @@ #include #include #include +#include /* #endif CONFIG_XDPLUA */ #include @@ -9893,6 +9894,7 @@ static int __init net_dev_init(void) } 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; diff --git a/net/core/filter.c b/net/core/filter.c index a8c4f51d764431..52b5ab6c37e2ff 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -76,6 +76,7 @@ /* #ifdef CONFIG_XDPLUA */ #include +#include /* #endif CONFIG_XDPLUA */ /** From 2fda6a1dacfd03f1c050bf12c01dd88172db97e7 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Tue, 11 Feb 2020 16:51:36 -0300 Subject: [PATCH 03/16] fixup! add XDPLua map sample --- samples/bpf/Makefile | 4 ++-- samples/bpf/map.lua | 4 ++-- samples/bpf/xdplua_map_kern.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index f1b7a3c55a589e..dfbf4d56030182 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -110,7 +110,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 +173,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 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..17a7e64f3f7747 100644 --- a/samples/bpf/xdplua_map_kern.c +++ b/samples/bpf/xdplua_map_kern.c @@ -21,8 +21,8 @@ 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"; From cd9fff9a97adfa8720ae9fc83c0f22026be43e75 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Wed, 12 Feb 2020 13:59:59 -0300 Subject: [PATCH 04/16] fixup! fixup! add XDPLua --- include/linux/filter.h | 4 ++-- include/linux/netdevice.h | 4 ++-- include/net/xdp.h | 4 ++-- include/uapi/linux/bpf.h | 4 ++-- include/uapi/linux/if_link.h | 4 ++-- net/core/dev.c | 28 ++++++++++++++-------------- net/core/filter.c | 12 ++++++------ net/core/rtnetlink.c | 8 ++++---- net/xdp/Kconfig | 7 +++++++ 9 files changed, 41 insertions(+), 34 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index 1d3913f2eb57b1..a613bb0754b6e9 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1229,7 +1229,7 @@ struct bpf_sockopt_kern { s32 retval; }; -/* #ifdef CONFIG_XDPLUA */ +#ifdef CONFIG_XDP_LUA extern struct list_head lua_state_cpu_list; struct lua_state_cpu { @@ -1237,6 +1237,6 @@ struct lua_state_cpu { int cpu; struct list_head list; }; -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ #endif /* __LINUX_FILTER_H__ */ diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 5b027a170cd2be..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 8025811447a538..de64fe2f1fc0b5 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -70,10 +70,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..97a29302d9180a 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2825,7 +2825,7 @@ 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), \ @@ -2839,7 +2839,7 @@ union bpf_attr { FN(lua_setstate), \ FN(lua_toboolean), \ FN(lua_tointeger), - /* #endif CONFIG_XDPLUA */ + /* #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 e060ecaf51b3d6..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/net/core/dev.c b/net/core/dev.c index 9bdc5aaeac6e9f..7c468f8ddeeaae 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -68,12 +68,12 @@ * - netif_rx() feedback */ -/* #ifdef CONFIG_XDPLUA */ +#ifdef CONFIG_XDP_LUA #include #include #include #include -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ #include #include @@ -171,9 +171,9 @@ 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 */ +#ifdef CONFIG_XDP_LUA struct list_head lua_state_cpu_list; -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ /* * The @dev_base_head list is protected by @dev_base_lock and the rtnl @@ -4382,9 +4382,9 @@ 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 xdp->skb = skb; -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ act = bpf_prog_run_xdp(xdp_prog, xdp); @@ -5197,7 +5197,7 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) return ret; } -/* #ifdef CONFIG_XDPLUA */ +#ifdef CONFIG_XDP_LUA int generic_xdp_lua_install_prog(char *lua_prog) { struct lua_state_cpu *sc; @@ -5211,7 +5211,7 @@ int generic_xdp_lua_install_prog(char *lua_prog) } return 0; } -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ static int netif_receive_skb_internal(struct sk_buff *skb) { @@ -9830,9 +9830,9 @@ static struct pernet_operations __net_initdata default_device_ops = { static int __init net_dev_init(void) { int i, rc = -ENOMEM; -/* #ifdef CONFIG_XDPLUA */ +#ifdef CONFIG_XDP_LUA struct lua_state_cpu *new_state_cpu; -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ BUG_ON(!dev_boot_phase); @@ -9847,9 +9847,9 @@ static int __init net_dev_init(void) INIT_LIST_HEAD(&ptype_base[i]); INIT_LIST_HEAD(&offload_base); -/* #ifdef CONFIG_XDPLUA */ +#ifdef CONFIG_XDP_LUA INIT_LIST_HEAD(&lua_state_cpu_list); -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ if (register_pernet_subsys(&netdev_net_ops)) goto out; @@ -9881,7 +9881,7 @@ static int __init net_dev_init(void) sd->backlog.poll = process_backlog; sd->backlog.weight = weight_p; -/* #ifdef CONFIG_XDPLUA */ +#ifdef CONFIG_XDP_LUA new_state_cpu = (struct lua_state_cpu *) kmalloc(sizeof(struct lua_state_cpu), GFP_ATOMIC); if (!new_state_cpu) @@ -9899,7 +9899,7 @@ static int __init net_dev_init(void) new_state_cpu->cpu = i; list_add(&new_state_cpu->list, &lua_state_cpu_list); -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ } dev_boot_phase = 0; diff --git a/net/core/filter.c b/net/core/filter.c index 52b5ab6c37e2ff..d4b27a53d1104f 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -74,10 +74,10 @@ #include #include -/* #ifdef CONFIG_XDPLUA */ +#ifdef CONFIG_XDP_LUA #include #include -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ /** * sk_filter_trim_cap - run a packet through a socket filter @@ -5857,7 +5857,7 @@ static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = { #endif /* CONFIG_INET */ -/* #ifdef CONFIG_XDPLUA */ +#ifdef CONFIG_XDP_LUA BPF_CALL_2(bpf_lua_dataref, struct xdp_buff *, ctx, int, offset) { if (offset + ctx->data < ctx->data_end) { int data_ref; @@ -6065,7 +6065,7 @@ static const struct bpf_func_proto bpf_lua_tointeger_proto = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, }; -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ bool bpf_helper_changes_pkt_data(void *func) { @@ -6141,7 +6141,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: @@ -6168,7 +6168,7 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_lua_toboolean_proto; case BPF_FUNC_lua_tointeger: return &bpf_lua_tointeger_proto; -/* #endif CONFIG_XDPLUA */ +#endif /* CONFIG_XDP_LUA */ default: return NULL; } diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 96f9d692076f00..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 From d29c749b0164066e7e8bf7e27bbcdd9ba23bc5df Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Thu, 13 Feb 2020 15:13:14 -0300 Subject: [PATCH 05/16] fixup! add Lunatik submodule --- init/Kconfig | 6 ++++++ 1 file changed, 6 insertions(+) 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 From aa6b6638418a1a99306079ac86692c7a11db4099 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Wed, 19 Feb 2020 22:13:09 -0300 Subject: [PATCH 06/16] Fix script loading synchronization issue --- include/linux/filter.h | 10 --- include/net/xdp.h | 11 ++- include/uapi/linux/bpf.h | 5 +- net/core/dev.c | 50 ++++++------- net/core/filter.c | 89 +++++++++++++---------- samples/bpf/xdplua_map_kern.c | 3 +- tools/include/uapi/linux/bpf.h | 5 +- tools/testing/selftests/bpf/bpf_helpers.h | 6 +- 8 files changed, 93 insertions(+), 86 deletions(-) diff --git a/include/linux/filter.h b/include/linux/filter.h index a613bb0754b6e9..92c6e31fb008ee 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1229,14 +1229,4 @@ 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__ */ diff --git a/include/net/xdp.h b/include/net/xdp.h index de64fe2f1fc0b5..3c0fce5bcb3b40 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -63,6 +63,15 @@ struct xdp_rxq_info { struct xdp_mem_info mem; } ____cacheline_aligned; /* perf critical, avoid false-sharing */ +#ifdef CONFIG_XDP_LUA +struct xdplua { + struct lua_State *L; + spinlock_t *lock; +}; + +DECLARE_PER_CPU(struct xdplua, xdplua_per_cpu); +#endif /* CONFIG_XDP_LUA */ + struct xdp_buff { void *data; void *data_end; @@ -72,7 +81,7 @@ struct xdp_buff { struct xdp_rxq_info *rxq; #ifdef CONFIG_XDP_LUA struct sk_buff *skb; - struct lua_State *L; + struct xdplua *xdplua; #endif /* CONFIG_XDP_LUA */ }; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 97a29302d9180a..1c1421dde0fd3b 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2836,9 +2836,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_putstate), \ + FN(lua_removestate), /* #endif CONFIG_XDP_LUA */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/net/core/dev.c b/net/core/dev.c index 7c468f8ddeeaae..1738079083d4b7 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -159,6 +159,9 @@ static DEFINE_SPINLOCK(ptype_lock); static DEFINE_SPINLOCK(offload_lock); +#ifdef CONFIG_XDP_LUA +static DEFINE_PER_CPU(spinlock_t, lua_state_lock); +#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; @@ -171,10 +174,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_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. @@ -5198,16 +5197,24 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) } #ifdef CONFIG_XDP_LUA +DEFINE_PER_CPU(struct xdplua, xdplua_per_cpu); + int generic_xdp_lua_install_prog(char *lua_prog) { - struct lua_state_cpu *sc; + struct xdplua *sc; + int i; - list_for_each_entry(sc, &lua_state_cpu_list, list) { + for_each_possible_cpu(i) { + sc = per_cpu_ptr(&xdplua_per_cpu, i); + spin_lock_bh(sc->lock); if (luaL_dostring(sc->L, lua_prog)) { pr_err(KERN_INFO "error: %s\nOn cpu: %d\n", - lua_tostring(sc->L, -1), sc->cpu); + lua_tostring(sc->L, -1), i); + spin_unlock_bh(sc->lock); return -EINVAL; } + + spin_unlock_bh(sc->lock); } return 0; } @@ -9830,9 +9837,6 @@ 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); @@ -9847,9 +9851,6 @@ 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; @@ -9861,6 +9862,7 @@ 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); + struct xdplua *xdplua = per_cpu_ptr(&xdplua_per_cpu, i); INIT_WORK(flush, flush_backlog); @@ -9880,25 +9882,17 @@ 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); + xdplua->L = luaL_newstate(); + if (!xdplua->L) { + kfree(xdplua); 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; - - list_add(&new_state_cpu->list, &lua_state_cpu_list); + luaL_openlibs(xdplua->L); + luaL_requiref(xdplua->L, "data", luaopen_data, 1); + lua_pop(xdplua->L, 1); + xdplua->lock = per_cpu_ptr(&lua_state_lock, i); #endif /* CONFIG_XDP_LUA */ } diff --git a/net/core/filter.c b/net/core/filter.c index d4b27a53d1104f..84d05187395eca 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5862,7 +5862,7 @@ BPF_CALL_2(bpf_lua_dataref, struct xdp_buff *, ctx, int, offset) { if (offset + ctx->data < ctx->data_end) { int data_ref; - data_ref = ldata_newref(ctx->L, ctx->data + offset, + data_ref = ldata_newref(ctx->xdplua->L, ctx->data + offset, ctx->data_end - ctx->data - offset); return data_ref; } @@ -5880,7 +5880,7 @@ static const struct bpf_func_proto bpf_lua_dataref_proto = { }; BPF_CALL_2(bpf_lua_dataunref, struct xdp_buff *, ctx, int, data_ref) { - ldata_unref(ctx->L, data_ref); + ldata_unref(ctx->xdplua->L, data_ref); return 0; } @@ -5895,16 +5895,16 @@ 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) { - if (lua_getglobal(ctx->L, funcname) != LUA_TFUNCTION) { + if (lua_getglobal(ctx->xdplua->L, funcname) != LUA_TFUNCTION) { pr_err("function %s not found\n", funcname); - lua_pop(ctx->L, num_args); + lua_pop(ctx->xdplua->L, num_args); return 0; } - lua_insert(ctx->L, 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); + lua_insert(ctx->xdplua->L, 1); + if (lua_pcall(ctx->xdplua->L, num_args, num_rets, 0)) { + pr_err("%s\n", lua_tostring(ctx->xdplua->L, -1)); + lua_pop(ctx->xdplua->L, 1); return 0; } return num_rets; @@ -5922,7 +5922,7 @@ static const struct bpf_func_proto bpf_lua_pcall_proto = { }; BPF_CALL_2(bpf_lua_pop, struct xdp_buff *, ctx, int, index) { - lua_pop(ctx->L, index); + lua_pop(ctx->xdplua->L, index); return 0; } @@ -5936,7 +5936,7 @@ static const struct bpf_func_proto bpf_lua_pop_proto = { }; BPF_CALL_2(bpf_lua_pushinteger, struct xdp_buff *, ctx, int, num) { - lua_pushinteger(ctx->L, num); + lua_pushinteger(ctx->xdplua->L, num); return 0; } @@ -5950,7 +5950,7 @@ static const struct bpf_func_proto bpf_lua_pushinteger_proto = { }; BPF_CALL_2(bpf_lua_pushlightuserdata, struct xdp_buff *, ctx, void *, ptr) { - lua_pushlightuserdata(ctx->L, ptr); + lua_pushlightuserdata(ctx->xdplua->L, ptr); return 0; } @@ -5964,12 +5964,12 @@ static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = { }; BPF_CALL_2(bpf_lua_pushmap, struct xdp_buff *, ctx, struct bpf_map *, map) { - lua_pushlightuserdata(ctx->L, map); + lua_pushlightuserdata(ctx->xdplua->L, map); return 0; } BPF_CALL_3(bpf_lua_pushlstring, struct xdp_buff *, ctx, const char *, str, size_t, len) { - lua_pushlstring(ctx->L, str, len); + lua_pushlstring(ctx->xdplua->L, str, len); return 0; } @@ -5993,7 +5993,7 @@ static const struct bpf_func_proto bpf_lua_pushmap_proto = { }; BPF_CALL_1(bpf_lua_pushskb, struct xdp_buff *, ctx) { - lua_pushlightuserdata(ctx->L, ctx->skb); + lua_pushlightuserdata(ctx->xdplua->L, ctx->skb); return 0; } @@ -6006,7 +6006,7 @@ static const struct bpf_func_proto bpf_lua_pushskb_proto = { }; BPF_CALL_2(bpf_lua_pushstring, struct xdp_buff *, ctx, const char *, str) { - lua_pushstring(ctx->L, str); + lua_pushstring(ctx->xdplua->L, str); return 0; } @@ -6019,29 +6019,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) { - return lua_toboolean(ctx->L, index); + return lua_toboolean(ctx->xdplua->L, index); } static const struct bpf_func_proto bpf_lua_toboolean_proto = { @@ -6054,7 +6033,7 @@ static const struct bpf_func_proto bpf_lua_toboolean_proto = { }; BPF_CALL_2(bpf_lua_tointeger, struct xdp_buff *, ctx, int, index) { - return lua_tointeger(ctx->L, index); + return lua_tointeger(ctx->xdplua->L, index); } static const struct bpf_func_proto bpf_lua_tointeger_proto = { @@ -6065,6 +6044,34 @@ static const struct bpf_func_proto bpf_lua_tointeger_proto = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, }; + +BPF_CALL_1(bpf_lua_putstate, struct xdp_buff *, ctx) { + ctx->xdplua = this_cpu_ptr(&xdplua_per_cpu); + spin_lock(ctx->xdplua->lock); + return 0; +} + +static const struct bpf_func_proto bpf_lua_putstate_proto = { + .func = bpf_lua_putstate, + .gpl_only = false, + .pkt_access = false, + .ret_type = RET_VOID, + .arg1_type = ARG_PTR_TO_CTX, +}; + +BPF_CALL_1(bpf_lua_removestate, struct xdp_buff *, ctx) { + spin_unlock(ctx->xdplua->lock); + ctx->xdplua = NULL; + return 0; +} + +static const struct bpf_func_proto bpf_lua_removestate_proto = { + .func = bpf_lua_removestate, + .gpl_only = false, + .pkt_access = false, + .ret_type = RET_VOID, + .arg1_type = ARG_PTR_TO_CTX, +}; #endif /* CONFIG_XDP_LUA */ bool bpf_helper_changes_pkt_data(void *func) @@ -6162,12 +6169,14 @@ 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_tointeger: return &bpf_lua_tointeger_proto; + case BPF_FUNC_lua_putstate: + return &bpf_lua_putstate_proto; + case BPF_FUNC_lua_removestate: + return &bpf_lua_removestate_proto; #endif /* CONFIG_XDP_LUA */ default: return NULL; diff --git a/samples/bpf/xdplua_map_kern.c b/samples/bpf/xdplua_map_kern.c index 17a7e64f3f7747..4f00af0f80a617 100644 --- a/samples/bpf/xdplua_map_kern.c +++ b/samples/bpf/xdplua_map_kern.c @@ -27,12 +27,13 @@ int xdp_lua_test_map_prog(struct xdp_md *ctx) char lookupname[] = "lookup"; char updatename[] = "update"; - bpf_lua_setstate(ctx); + bpf_lua_putstate(ctx); bpf_lua_pushmap(ctx, &test_map); bpf_lua_pcall(ctx, updatename, 1, 0); bpf_lua_pushmap(ctx, &test_map); bpf_lua_pcall(ctx, lookupname, 1, 0); + bpf_lua_removestate(ctx); return XDP_PASS; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 095257ff526151..2fbe6120a49f9e 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2836,9 +2836,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_putstate), \ + FN(lua_removestate), /* #endif CONFIG_XDPLUA */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index 6bc9919ae73c88..cdd098cf77c913 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -252,12 +252,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 void (*bpf_lua_putstate)(void *ctx) = + (void *)BPF_FUNC_lua_putstate; +static void (*bpf_lua_removestate)(void *ctx) = + (void *)BPF_FUNC_lua_removestate; /* #endif CONFIG_XDPLUA */ /* llvm builtin functions that eBPF C program may use to From 497fd52846a0bd5b4432db685756a43bd470ab6a Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Wed, 19 Feb 2020 21:14:48 -0300 Subject: [PATCH 07/16] add LuaUnpack submodule --- .gitmodules | 3 +++ include/uapi/linux/bpf.h | 3 ++- lib/Makefile | 3 ++- lib/luaunpack | 1 + net/core/Makefile | 2 +- net/core/filter.c | 20 ++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/bpf_helpers.h | 2 ++ 8 files changed, 33 insertions(+), 4 deletions(-) create mode 160000 lib/luaunpack 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/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 1c1421dde0fd3b..fc73c698b354aa 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2839,7 +2839,8 @@ union bpf_attr { FN(lua_toboolean), \ FN(lua_tointeger), \ FN(lua_putstate), \ - FN(lua_removestate), + FN(lua_removestate), \ + FN(lua_newpacket), /* #endif CONFIG_XDP_LUA */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper 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/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 9f56a972205bb0..eea1f004039926 100644 --- a/net/core/Makefile +++ b/net/core/Makefile @@ -11,7 +11,7 @@ obj-$(CONFIG_SYSCTL) += sysctl_net_core.o CFLAGS_dev.o = -Ilib/lunatik/lua/ -D_KERNEL \ -Ilib/luadata/ CFLAGS_filter.o = -Ilib/lunatik/lua/ -D_KERNEL \ - -Ilib/luadata/ + -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/filter.c b/net/core/filter.c index 84d05187395eca..f87fad13f85518 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -77,6 +77,7 @@ #ifdef CONFIG_XDP_LUA #include #include +#include #endif /* CONFIG_XDP_LUA */ /** @@ -6072,6 +6073,23 @@ static const struct bpf_func_proto bpf_lua_removestate_proto = { .ret_type = RET_VOID, .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_2(bpf_lua_newpacket, struct xdp_buff *, ctx, int, offset) { + if (offset + ctx->data < ctx->data_end) { + return lunpack_newpacket(ctx->xdplua->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, +}; #endif /* CONFIG_XDP_LUA */ bool bpf_helper_changes_pkt_data(void *func) @@ -6177,6 +6195,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_lua_putstate_proto; case BPF_FUNC_lua_removestate: return &bpf_lua_removestate_proto; + case BPF_FUNC_lua_newpacket: + return &bpf_lua_newpacket_proto; #endif /* CONFIG_XDP_LUA */ default: return NULL; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 2fbe6120a49f9e..58939ce2850b19 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2839,7 +2839,8 @@ union bpf_attr { FN(lua_toboolean), \ FN(lua_tointeger), \ FN(lua_putstate), \ - FN(lua_removestate), + FN(lua_removestate), \ + FN(lua_newpacket), /* #endif CONFIG_XDPLUA */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index cdd098cf77c913..a4732801337e90 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -260,6 +260,8 @@ static void (*bpf_lua_putstate)(void *ctx) = (void *)BPF_FUNC_lua_putstate; static void (*bpf_lua_removestate)(void *ctx) = (void *)BPF_FUNC_lua_removestate; +static int (*bpf_lua_newpacket)(void *ctx, int offset) = + (void *)BPF_FUNC_lua_newpacket; /* #endif CONFIG_XDPLUA */ /* llvm builtin functions that eBPF C program may use to From 42219e1c3c997bf363793e23f4a10f933e06db55 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Mon, 3 Feb 2020 18:57:06 -0300 Subject: [PATCH 08/16] Add bpf_lua_tostring function --- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 22 ++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 1 + tools/testing/selftests/bpf/bpf_helpers.h | 2 ++ 4 files changed, 26 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index fc73c698b354aa..1bd50706936f14 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2829,6 +2829,7 @@ union bpf_attr { FN(lua_dataref), \ FN(lua_dataunref), \ FN(lua_pcall), \ + FN(lua_tostring), \ FN(lua_pop), \ FN(lua_pushinteger), \ FN(lua_pushlightuserdata), \ diff --git a/net/core/filter.c b/net/core/filter.c index f87fad13f85518..6724b2a9e14bd2 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6060,6 +6060,26 @@ static const struct bpf_func_proto bpf_lua_putstate_proto = { .arg1_type = ARG_PTR_TO_CTX, }; +BPF_CALL_4(bpf_lua_tostring, struct xdp_buff *, ctx, char *, str, u32, size, int, index) { + if (lua_isstring(ctx->lstatecpu->L, index)) { + strncpy(str, lua_tostring(ctx->lstatecpu->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_1(bpf_lua_removestate, struct xdp_buff *, ctx) { spin_unlock(ctx->xdplua->lock); ctx->xdplua = NULL; @@ -6189,6 +6209,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_lua_pushstring_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; case BPF_FUNC_lua_putstate: diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 58939ce2850b19..cbb3f08e8257d7 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2829,6 +2829,7 @@ union bpf_attr { FN(lua_dataref), \ FN(lua_dataunref), \ FN(lua_pcall), \ + FN(lua_tostring), \ FN(lua_pop), \ FN(lua_pushinteger), \ FN(lua_pushlightuserdata), \ diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index a4732801337e90..d4baa2ec057557 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -237,6 +237,8 @@ static void (*bpf_lua_dataunref)(void *ctx, int data_ref) = static void (*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) = From 904bb0e401c12408fa99fc53a3569354b2d5b3f7 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Tue, 3 Mar 2020 12:22:29 -0300 Subject: [PATCH 09/16] Add bpf_lua_type function --- include/uapi/linux/bpf.h | 3 ++- net/core/filter.c | 16 ++++++++++++++++ tools/include/uapi/linux/bpf.h | 3 ++- tools/testing/selftests/bpf/bpf_helpers.h | 2 ++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 1bd50706936f14..a973103c5a111f 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2841,7 +2841,8 @@ union bpf_attr { FN(lua_tointeger), \ FN(lua_putstate), \ FN(lua_removestate), \ - FN(lua_newpacket), + FN(lua_newpacket), \ + FN(lua_type), /* #endif CONFIG_XDP_LUA */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/net/core/filter.c b/net/core/filter.c index 6724b2a9e14bd2..846142f595854f 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6093,6 +6093,7 @@ static const struct bpf_func_proto bpf_lua_removestate_proto = { .ret_type = RET_VOID, .arg1_type = ARG_PTR_TO_CTX, }; + BPF_CALL_2(bpf_lua_newpacket, struct xdp_buff *, ctx, int, offset) { if (offset + ctx->data < ctx->data_end) { return lunpack_newpacket(ctx->xdplua->L, ctx->data + offset, @@ -6110,6 +6111,19 @@ static const struct bpf_func_proto bpf_lua_newpacket_proto = { .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_ANYTHING, }; + +BPF_CALL_2(bpf_lua_type, struct xdp_buff *, ctx, int, index) { + return lua_type(ctx->xdplua->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) @@ -6219,6 +6233,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_lua_removestate_proto; 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/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index cbb3f08e8257d7..592ee33d5ee462 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2841,7 +2841,8 @@ union bpf_attr { FN(lua_tointeger), \ FN(lua_putstate), \ FN(lua_removestate), \ - FN(lua_newpacket), + 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/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index d4baa2ec057557..2408104b422b2b 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -264,6 +264,8 @@ static void (*bpf_lua_removestate)(void *ctx) = (void *)BPF_FUNC_lua_removestate; 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 From 17af788e690c166e466617ec1c489e2ef29b5b40 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Tue, 3 Mar 2020 16:24:55 -0300 Subject: [PATCH 10/16] fixup! Add bpf_lua_tostring function --- net/core/filter.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index 846142f595854f..52c87b93a67d42 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6061,8 +6061,8 @@ static const struct bpf_func_proto bpf_lua_putstate_proto = { }; BPF_CALL_4(bpf_lua_tostring, struct xdp_buff *, ctx, char *, str, u32, size, int, index) { - if (lua_isstring(ctx->lstatecpu->L, index)) { - strncpy(str, lua_tostring(ctx->lstatecpu->L, index), size); + if (lua_isstring(ctx->xdplua->L, index)) { + strncpy(str, lua_tostring(ctx->xdplua->L, index), size); return 1; } From 73e349a9bd0887d2bb4b19d688a1f4f12c4fcbed Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Wed, 18 Mar 2020 07:03:25 -0300 Subject: [PATCH 11/16] fixup! Fix script loading synchronization issue --- include/net/xdp.h | 12 ++- include/uapi/linux/bpf.h | 2 - net/core/dev.c | 71 +++++++++----- net/core/filter.c | 111 +++++++++++----------- samples/bpf/Makefile | 2 + tools/include/uapi/linux/bpf.h | 2 - tools/testing/selftests/bpf/bpf_helpers.h | 6 +- 7 files changed, 113 insertions(+), 93 deletions(-) diff --git a/include/net/xdp.h b/include/net/xdp.h index 3c0fce5bcb3b40..2a63dce62433fd 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -64,14 +64,18 @@ struct xdp_rxq_info { } ____cacheline_aligned; /* perf critical, avoid false-sharing */ #ifdef CONFIG_XDP_LUA -struct xdplua { +struct xdplua_create_work { + char *lua_script; struct lua_State *L; - spinlock_t *lock; + struct work_struct work; + spinlock_t lock; + bool init; }; -DECLARE_PER_CPU(struct xdplua, xdplua_per_cpu); +DECLARE_PER_CPU(struct xdplua_create_work, luaworks); #endif /* CONFIG_XDP_LUA */ + struct xdp_buff { void *data; void *data_end; @@ -81,7 +85,7 @@ struct xdp_buff { struct xdp_rxq_info *rxq; #ifdef CONFIG_XDP_LUA struct sk_buff *skb; - struct xdplua *xdplua; + struct lua_State *L; #endif /* CONFIG_XDP_LUA */ }; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index a973103c5a111f..88c5f377d90221 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -2839,8 +2839,6 @@ union bpf_attr { FN(lua_pushstring), \ FN(lua_toboolean), \ FN(lua_tointeger), \ - FN(lua_putstate), \ - FN(lua_removestate), \ FN(lua_newpacket), \ FN(lua_type), /* #endif CONFIG_XDP_LUA */ diff --git a/net/core/dev.c b/net/core/dev.c index 1738079083d4b7..9b19179e5c948e 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -71,6 +71,7 @@ #ifdef CONFIG_XDP_LUA #include #include +#include #include #include #endif /* CONFIG_XDP_LUA */ @@ -160,7 +161,7 @@ static DEFINE_SPINLOCK(ptype_lock); static DEFINE_SPINLOCK(offload_lock); #ifdef CONFIG_XDP_LUA -static DEFINE_PER_CPU(spinlock_t, lua_state_lock); +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 */ @@ -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. @@ -4382,11 +4386,21 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb, rxqueue = netif_get_rxqueue(skb); xdp->rxq = &rxqueue->xdp_rxq; #ifdef CONFIG_XDP_LUA + lw = this_cpu_ptr(&luaworks); + xdp->skb = skb; + 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) { @@ -5197,24 +5211,30 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) } #ifdef CONFIG_XDP_LUA -DEFINE_PER_CPU(struct xdplua, xdplua_per_cpu); -int generic_xdp_lua_install_prog(char *lua_prog) -{ - struct xdplua *sc; - int i; +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); - for_each_possible_cpu(i) { - sc = per_cpu_ptr(&xdplua_per_cpu, i); - spin_lock_bh(sc->lock); - if (luaL_dostring(sc->L, lua_prog)) { - pr_err(KERN_INFO "error: %s\nOn cpu: %d\n", - lua_tostring(sc->L, -1), i); - spin_unlock_bh(sc->lock); - return -EINVAL; - } + 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) +{ + int cpu; + struct xdplua_create_work *lw; - spin_unlock_bh(sc->lock); + for_each_possible_cpu(cpu) { + lw = per_cpu_ptr(&luaworks, cpu); + lw->lua_script = lua_script; + INIT_WORK(&lw->work, per_cpu_xdp_lua_install); + schedule_work_on(cpu, &lw->work); } return 0; } @@ -9862,7 +9882,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); - struct xdplua *xdplua = per_cpu_ptr(&xdplua_per_cpu, i); +#ifdef CONFIG_XDP_LUA + struct xdplua_create_work *lw = per_cpu_ptr(&luaworks, i); +#endif INIT_WORK(flush, flush_backlog); @@ -9883,16 +9905,15 @@ static int __init net_dev_init(void) sd->backlog.poll = process_backlog; sd->backlog.weight = weight_p; #ifdef CONFIG_XDP_LUA - xdplua->L = luaL_newstate(); - if (!xdplua->L) { - kfree(xdplua); + lw->L = luaL_newstate(); + WARN_ON(!lw->L); + + if (!lw->L) continue; - } - luaL_openlibs(xdplua->L); - luaL_requiref(xdplua->L, "data", luaopen_data, 1); - lua_pop(xdplua->L, 1); - xdplua->lock = per_cpu_ptr(&lua_state_lock, i); + luaL_openlibs(lw->L); + luaL_requiref(lw->L, "data", luaopen_data, 1); + lua_pop(lw->L, 1); #endif /* CONFIG_XDP_LUA */ } diff --git a/net/core/filter.c b/net/core/filter.c index 52c87b93a67d42..532e0310d8e00d 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5859,11 +5859,23 @@ static const struct bpf_func_proto bpf_tcp_check_syncookie_proto = { #endif /* CONFIG_INET */ #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; - data_ref = ldata_newref(ctx->xdplua->L, ctx->data + offset, + verify_and_lock(); + data_ref = ldata_newref(ctx->L, ctx->data + offset, ctx->data_end - ctx->data - offset); return data_ref; } @@ -5881,7 +5893,8 @@ static const struct bpf_func_proto bpf_lua_dataref_proto = { }; BPF_CALL_2(bpf_lua_dataunref, struct xdp_buff *, ctx, int, data_ref) { - ldata_unref(ctx->xdplua->L, data_ref); + verify_and_lock(); + ldata_unref(ctx->L, data_ref); return 0; } @@ -5896,18 +5909,26 @@ 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) { - if (lua_getglobal(ctx->xdplua->L, funcname) != LUA_TFUNCTION) { + 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->xdplua->L, num_args); - return 0; + num_rets = 0; + goto clean_state; } - lua_insert(ctx->xdplua->L, 1); - if (lua_pcall(ctx->xdplua->L, num_args, num_rets, 0)) { - pr_err("%s\n", lua_tostring(ctx->xdplua->L, -1)); - lua_pop(ctx->xdplua->L, 1); - return 0; + lua_insert(ctx->L, 1); + if (lua_pcall(ctx->L, num_args, num_rets, 0)) { + pr_err("%s\n", lua_tostring(ctx->L, -1)); + num_rets = 0; + goto clean_state; } + +clean_state: + lua_settop(ctx->L, base); return num_rets; } @@ -5923,7 +5944,8 @@ static const struct bpf_func_proto bpf_lua_pcall_proto = { }; BPF_CALL_2(bpf_lua_pop, struct xdp_buff *, ctx, int, index) { - lua_pop(ctx->xdplua->L, index); + verify_and_lock(); + lua_pop(ctx->L, index); return 0; } @@ -5937,7 +5959,8 @@ static const struct bpf_func_proto bpf_lua_pop_proto = { }; BPF_CALL_2(bpf_lua_pushinteger, struct xdp_buff *, ctx, int, num) { - lua_pushinteger(ctx->xdplua->L, num); + verify_and_lock(); + lua_pushinteger(ctx->L, num); return 0; } @@ -5951,7 +5974,8 @@ static const struct bpf_func_proto bpf_lua_pushinteger_proto = { }; BPF_CALL_2(bpf_lua_pushlightuserdata, struct xdp_buff *, ctx, void *, ptr) { - lua_pushlightuserdata(ctx->xdplua->L, ptr); + verify_and_lock(); + lua_pushlightuserdata(ctx->L, ptr); return 0; } @@ -5965,12 +5989,14 @@ static const struct bpf_func_proto bpf_lua_pushlightuserdata_proto = { }; BPF_CALL_2(bpf_lua_pushmap, struct xdp_buff *, ctx, struct bpf_map *, map) { - lua_pushlightuserdata(ctx->xdplua->L, 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) { - lua_pushlstring(ctx->xdplua->L, str, len); + verify_and_lock(); + lua_pushlstring(ctx->L, str, len); return 0; } @@ -5994,7 +6020,8 @@ static const struct bpf_func_proto bpf_lua_pushmap_proto = { }; BPF_CALL_1(bpf_lua_pushskb, struct xdp_buff *, ctx) { - lua_pushlightuserdata(ctx->xdplua->L, ctx->skb); + verify_and_lock(); + lua_pushlightuserdata(ctx->L, ctx->skb); return 0; } @@ -6007,7 +6034,8 @@ static const struct bpf_func_proto bpf_lua_pushskb_proto = { }; BPF_CALL_2(bpf_lua_pushstring, struct xdp_buff *, ctx, const char *, str) { - lua_pushstring(ctx->xdplua->L, str); + verify_and_lock(); + lua_pushstring(ctx->L, str); return 0; } @@ -6021,7 +6049,8 @@ static const struct bpf_func_proto bpf_lua_pushstring_proto = { }; BPF_CALL_2(bpf_lua_toboolean, struct xdp_buff *, ctx, int, index) { - return lua_toboolean(ctx->xdplua->L, index); + verify_and_lock(); + return lua_toboolean(ctx->L, index); } static const struct bpf_func_proto bpf_lua_toboolean_proto = { @@ -6034,7 +6063,8 @@ static const struct bpf_func_proto bpf_lua_toboolean_proto = { }; BPF_CALL_2(bpf_lua_tointeger, struct xdp_buff *, ctx, int, index) { - return lua_tointeger(ctx->xdplua->L, index); + verify_and_lock(); + return lua_tointeger(ctx->L, index); } static const struct bpf_func_proto bpf_lua_tointeger_proto = { @@ -6046,23 +6076,10 @@ static const struct bpf_func_proto bpf_lua_tointeger_proto = { .arg2_type = ARG_ANYTHING, }; -BPF_CALL_1(bpf_lua_putstate, struct xdp_buff *, ctx) { - ctx->xdplua = this_cpu_ptr(&xdplua_per_cpu); - spin_lock(ctx->xdplua->lock); - return 0; -} - -static const struct bpf_func_proto bpf_lua_putstate_proto = { - .func = bpf_lua_putstate, - .gpl_only = false, - .pkt_access = false, - .ret_type = RET_VOID, - .arg1_type = ARG_PTR_TO_CTX, -}; - BPF_CALL_4(bpf_lua_tostring, struct xdp_buff *, ctx, char *, str, u32, size, int, index) { - if (lua_isstring(ctx->xdplua->L, index)) { - strncpy(str, lua_tostring(ctx->xdplua->L, index), size); + verify_and_lock(); + if (lua_isstring(ctx->L, index)) { + strncpy(str, lua_tostring(ctx->L, index), size); return 1; } @@ -6080,23 +6097,10 @@ static const struct bpf_func_proto bpf_lua_tostring_proto = { .arg4_type = ARG_ANYTHING, }; -BPF_CALL_1(bpf_lua_removestate, struct xdp_buff *, ctx) { - spin_unlock(ctx->xdplua->lock); - ctx->xdplua = NULL; - return 0; -} - -static const struct bpf_func_proto bpf_lua_removestate_proto = { - .func = bpf_lua_removestate, - .gpl_only = false, - .pkt_access = false, - .ret_type = RET_VOID, - .arg1_type = ARG_PTR_TO_CTX, -}; - BPF_CALL_2(bpf_lua_newpacket, struct xdp_buff *, ctx, int, offset) { if (offset + ctx->data < ctx->data_end) { - return lunpack_newpacket(ctx->xdplua->L, ctx->data + offset, + verify_and_lock(); + return lunpack_newpacket(ctx->L, ctx->data + offset, ctx->data_end - ctx->data - offset); } @@ -6113,7 +6117,8 @@ static const struct bpf_func_proto bpf_lua_newpacket_proto = { }; BPF_CALL_2(bpf_lua_type, struct xdp_buff *, ctx, int, index) { - return lua_type(ctx->xdplua->L, index); + verify_and_lock(); + return lua_type(ctx->L, index); } static const struct bpf_func_proto bpf_lua_type_proto = { @@ -6227,10 +6232,6 @@ bpf_base_func_proto(enum bpf_func_id func_id) return &bpf_lua_tostring_proto; case BPF_FUNC_lua_tointeger: return &bpf_lua_tointeger_proto; - case BPF_FUNC_lua_putstate: - return &bpf_lua_putstate_proto; - case BPF_FUNC_lua_removestate: - return &bpf_lua_removestate_proto; case BPF_FUNC_lua_newpacket: return &bpf_lua_newpacket_proto; case BPF_FUNC_lua_type: diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index dfbf4d56030182..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 @@ -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/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 592ee33d5ee462..7e792e4eb42d9c 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -2839,8 +2839,6 @@ union bpf_attr { FN(lua_pushstring), \ FN(lua_toboolean), \ FN(lua_tointeger), \ - FN(lua_putstate), \ - FN(lua_removestate), \ FN(lua_newpacket), \ FN(lua_type), /* #endif CONFIG_XDPLUA */ diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index 2408104b422b2b..53da25bd0001f7 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -229,7 +229,7 @@ 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) = @@ -258,10 +258,6 @@ 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 void (*bpf_lua_putstate)(void *ctx) = - (void *)BPF_FUNC_lua_putstate; -static void (*bpf_lua_removestate)(void *ctx) = - (void *)BPF_FUNC_lua_removestate; static int (*bpf_lua_newpacket)(void *ctx, int offset) = (void *)BPF_FUNC_lua_newpacket; static int (*bpf_lua_type)(void *ctx, int index) = From 822408f5e114d4afbe298411b8c4ad076691dddf Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Wed, 18 Mar 2020 07:03:50 -0300 Subject: [PATCH 12/16] fixup! add XDPLua map sample --- samples/bpf/xdplua_map_kern.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/bpf/xdplua_map_kern.c b/samples/bpf/xdplua_map_kern.c index 4f00af0f80a617..d68d20732bb754 100644 --- a/samples/bpf/xdplua_map_kern.c +++ b/samples/bpf/xdplua_map_kern.c @@ -27,13 +27,11 @@ int xdp_lua_test_map_prog(struct xdp_md *ctx) char lookupname[] = "lookup"; char updatename[] = "update"; - bpf_lua_putstate(ctx); bpf_lua_pushmap(ctx, &test_map); bpf_lua_pcall(ctx, updatename, 1, 0); bpf_lua_pushmap(ctx, &test_map); bpf_lua_pcall(ctx, lookupname, 1, 0); - bpf_lua_removestate(ctx); return XDP_PASS; } From 47d54690fe3ac86e2a4424953d650e0831e47021 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Wed, 18 Mar 2020 21:07:16 -0300 Subject: [PATCH 13/16] fixup! add LuaRCU submodule --- lib/luarcu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 2bb260c04440abd6532050d0b3b6a9392e81eef6 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Sun, 22 Mar 2020 23:32:12 -0300 Subject: [PATCH 14/16] fixup! Fix script loading synchronization issue --- net/core/filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/core/filter.c b/net/core/filter.c index 532e0310d8e00d..4c2408182aa26f 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5913,7 +5913,7 @@ BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname, verify_and_lock(); - base = lua_gettop(ctx->L) - num_args; + base = lua_gettop(ctx->L) - num_args + num_rets; if (lua_getglobal(ctx->L, funcname) != LUA_TFUNCTION) { pr_err("function %s not found\n", funcname); num_rets = 0; From 5afe3af96c7c9105466d0900a5aa817057387636 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Fri, 3 Apr 2020 16:18:53 -0300 Subject: [PATCH 15/16] fixup! Fix script loading synchronization issue --- include/net/xdp.h | 2 +- net/core/dev.c | 5 +++-- net/core/filter.c | 3 ++- tools/testing/selftests/bpf/bpf_helpers.h | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/net/xdp.h b/include/net/xdp.h index 2a63dce62433fd..8c38a85ebff132 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -65,7 +65,7 @@ struct xdp_rxq_info { #ifdef CONFIG_XDP_LUA struct xdplua_create_work { - char *lua_script; + char lua_script[8192]; struct lua_State *L; struct work_struct work; spinlock_t lock; diff --git a/net/core/dev.c b/net/core/dev.c index 9b19179e5c948e..36fc3646d42d50 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -5232,8 +5232,7 @@ int generic_xdp_lua_install_prog(char *lua_script) for_each_possible_cpu(cpu) { lw = per_cpu_ptr(&luaworks, cpu); - lw->lua_script = lua_script; - INIT_WORK(&lw->work, per_cpu_xdp_lua_install); + strcpy(lw->lua_script, lua_script); schedule_work_on(cpu, &lw->work); } return 0; @@ -9914,6 +9913,8 @@ static int __init net_dev_init(void) luaL_openlibs(lw->L); luaL_requiref(lw->L, "data", luaopen_data, 1); lua_pop(lw->L, 1); + + INIT_WORK(&lw->work, per_cpu_xdp_lua_install); #endif /* CONFIG_XDP_LUA */ } diff --git a/net/core/filter.c b/net/core/filter.c index 4c2408182aa26f..64b33a24c887b6 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5913,7 +5913,7 @@ BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname, verify_and_lock(); - base = lua_gettop(ctx->L) - num_args + num_rets; + base = lua_gettop(ctx->L) - num_args; if (lua_getglobal(ctx->L, funcname) != LUA_TFUNCTION) { pr_err("function %s not found\n", funcname); num_rets = 0; @@ -5928,6 +5928,7 @@ BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname, } clean_state: + base += num_rets; lua_settop(ctx->L, base); return num_rets; } diff --git a/tools/testing/selftests/bpf/bpf_helpers.h b/tools/testing/selftests/bpf/bpf_helpers.h index 53da25bd0001f7..7851be402497ae 100644 --- a/tools/testing/selftests/bpf/bpf_helpers.h +++ b/tools/testing/selftests/bpf/bpf_helpers.h @@ -234,7 +234,7 @@ 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) = From 522f718fc0d455ab6cd20a575a26fd9fb0f46f30 Mon Sep 17 00:00:00 2001 From: Victor Nogueira Date: Fri, 28 Aug 2020 02:40:45 -0300 Subject: [PATCH 16/16] fixup! add XDPLua --- net/core/filter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index 64b33a24c887b6..2e03bfad0e5a85 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5920,15 +5920,16 @@ BPF_CALL_4(bpf_lua_pcall, struct xdp_buff *, ctx, char *, funcname, 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)); num_rets = 0; goto clean_state; } -clean_state: base += num_rets; + +clean_state: lua_settop(ctx->L, base); return num_rets; }