|
| 1 | +/* XDPLua internal functions |
| 2 | + * Copyright (C) 2021 Victor Nogueira <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU General Public License as |
| 6 | + * published by the Free Software Foundation version 2. |
| 7 | + * |
| 8 | + * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 9 | + * kind, whether express or implied; without even the implied warranty |
| 10 | + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + */ |
| 13 | +#ifndef __LINUX_NET_XDP_LUA_H__ |
| 14 | +#define __LINUX_NET_XDP_LUA_H__ |
| 15 | + |
| 16 | +#include <lua.h> |
| 17 | + |
| 18 | +#include <linux/skbuff.h> |
| 19 | +#include <linux/workqueue.h> |
| 20 | +#include <uapi/linux/if_link.h> |
| 21 | +#include <linux/filter.h> |
| 22 | + |
| 23 | +struct xdp_lua_work { |
| 24 | + char script[XDP_LUA_MAX_SCRIPT_LEN]; |
| 25 | + size_t script_len; |
| 26 | + struct lua_State *L; |
| 27 | + struct sk_buff *skb; |
| 28 | + struct work_struct work; |
| 29 | +}; |
| 30 | + |
| 31 | +DECLARE_PER_CPU(struct xdp_lua_work, luaworks); |
| 32 | + |
| 33 | + |
| 34 | +#define XDP_LUA_BPF_FUNC(name) BPF_FUNC_lua_##name |
| 35 | + |
| 36 | +#define xdp_lua_get_skb() (this_cpu_ptr(&luaworks)->skb) |
| 37 | +#define xdp_lua_set_skb(skb) (this_cpu_ptr(&luaworks)->skb = skb) |
| 38 | + |
| 39 | +int generic_xdp_lua_install_prog(const char *script, size_t script_len); |
| 40 | +void xdp_lua_init(void); |
| 41 | + |
| 42 | +#define __BPF_LUA_MAP_0(l, m, v, ...) l |
| 43 | +#define __BPF_LUA_MAP_1(l, m, v, t, a, ...) l, __BPF_MAP_1(m, v, t, a, __VA_ARGS__) |
| 44 | +#define __BPF_LUA_MAP_2(l, m, v, t, a, ...) l, __BPF_MAP_2(m, v, t, a, __VA_ARGS__) |
| 45 | +#define __BPF_LUA_MAP_3(l, m, v, t, a, ...) l, __BPF_MAP_3(m, v, t, a, __VA_ARGS__) |
| 46 | +#define __BPF_LUA_MAP_4(l, m, v, t, a, ...) l, __BPF_MAP_4(m, v, t, a, __VA_ARGS__) |
| 47 | +#define __BPF_LUA_MAP_5(l, m, v, t, a, ...) l, __BPF_MAP_5(m, v, t, a, __VA_ARGS__) |
| 48 | + |
| 49 | +#define __BPF_LUA_MAP(n, l, ...) __BPF_LUA_MAP_##n(l, __VA_ARGS__) |
| 50 | + |
| 51 | +#define LST_TYPE lua_State |
| 52 | +#define LST_NAME L |
| 53 | +#define LST_DECL __BPF_DECL_ARGS(LST_TYPE*, LST_NAME) |
| 54 | + |
| 55 | +#define BPF_LUA_CALL_x(x, name, ...) \ |
| 56 | + static __always_inline \ |
| 57 | + u64 ____##name(__BPF_LUA_MAP(x, LST_DECL, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)); \ |
| 58 | + u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)); \ |
| 59 | + u64 name(__BPF_REG(x, __BPF_DECL_REGS, __BPF_N, __VA_ARGS__)) \ |
| 60 | + { \ |
| 61 | + int ret = -ENOENT; \ |
| 62 | + lua_State *L = this_cpu_ptr(&luaworks)->L; \ |
| 63 | + WARN_ON(!L); \ |
| 64 | + if (!L) \ |
| 65 | + return ret; \ |
| 66 | + ret = ____##name(__BPF_LUA_MAP(x, LST_NAME, __BPF_CAST, __BPF_N, __VA_ARGS__)); \ |
| 67 | + return ret; \ |
| 68 | + } \ |
| 69 | + static __always_inline \ |
| 70 | + u64 ____##name(__BPF_LUA_MAP(x, LST_DECL, __BPF_DECL_ARGS, __BPF_V, __VA_ARGS__)) |
| 71 | + |
| 72 | +#define BPF_LUA_CALL_0(name, ...) BPF_LUA_CALL_x(0, name, __VA_ARGS__) |
| 73 | +#define BPF_LUA_CALL_1(name, ...) BPF_LUA_CALL_x(1, name, __VA_ARGS__) |
| 74 | +#define BPF_LUA_CALL_2(name, ...) BPF_LUA_CALL_x(2, name, __VA_ARGS__) |
| 75 | +#define BPF_LUA_CALL_3(name, ...) BPF_LUA_CALL_x(3, name, __VA_ARGS__) |
| 76 | +#define BPF_LUA_CALL_4(name, ...) BPF_LUA_CALL_x(4, name, __VA_ARGS__) |
| 77 | +#define BPF_LUA_CALL_5(name, ...) BPF_LUA_CALL_x(5, name, __VA_ARGS__) |
| 78 | + |
| 79 | +#endif /* __LINUX_NET_XDP_LUA_H__ */ |
0 commit comments