Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XDPLua helper version #11

Open
wants to merge 3 commits into
base: xdp_lua_helper_version_base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/linux/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct sock_reuseport;
struct ctl_table;
struct ctl_table_header;

u32 lua_prog_run_xdp(struct xdp_buff *ctx, const char *func);

/* ArgX, context and stack frame pointer register positions. Note,
* Arg1, Arg2, Arg3, etc are used as argument mappings of function
* calls in BPF_CALL instruction.
Expand Down
1 change: 1 addition & 0 deletions include/linux/netdevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3684,6 +3684,7 @@ 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);

int generic_xdp_lua_install(char *lua_prog);
int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
bool is_skb_forwardable(const struct net_device *dev,
Expand Down
3 changes: 2 additions & 1 deletion include/uapi/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2824,7 +2824,8 @@ union bpf_attr {
FN(strtoul), \
FN(sk_storage_get), \
FN(sk_storage_delete), \
FN(send_signal),
FN(send_signal), \
FN(lua_run),

/* integer value in 'imm' field of BPF_CALL instruction selects which helper
* function eBPF program intends to call
Expand Down
1 change: 1 addition & 0 deletions include/uapi/linux/if_link.h
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ enum {
IFLA_XDP_DRV_PROG_ID,
IFLA_XDP_SKB_PROG_ID,
IFLA_XDP_HW_PROG_ID,
IFLA_XDP_LUA,
__IFLA_XDP_MAX,
};

Expand Down
1 change: 1 addition & 0 deletions net/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 = -Iinclude/linux/lunatik/ -Iinclude/linux/luadata/ -D_KERNEL
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 \
Expand Down
93 changes: 93 additions & 0 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@
* - netif_rx() feedback
*/

#ifndef _LUNATIK_H
#define _LUNATIK_H
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#endif

#ifndef _LUADATA_H
#define _LUADATA_H
#include <luadata.h>
#endif

#include <linux/uaccess.h>
#include <linux/bitops.h>
#include <linux/capability.h>
Expand Down Expand Up @@ -150,11 +162,20 @@
/* This should be increased if a protocol with a bigger head is added. */
#define GRO_MAX_HEAD (MAX_HEADER + 128)

struct lua_state_cpu {
lua_State *L;
int cpu;
struct list_head list;
};

typedef struct lua_state_cpu lua_state_cpu;

static DEFINE_SPINLOCK(ptype_lock);
static DEFINE_SPINLOCK(offload_lock);
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;
static struct list_head lua_state_cpu_list;

static int netif_rx_internal(struct sk_buff *skb);
static int call_netdevice_notifiers_info(unsigned long val,
Expand Down Expand Up @@ -868,6 +889,46 @@ struct net_device *dev_get_by_index(struct net *net, int ifindex)
}
EXPORT_SYMBOL(dev_get_by_index);

u32 lua_prog_run_xdp(struct xdp_buff *ctx, const char *func)
{
lua_State *L = NULL;
lua_state_cpu *sc;
int cpu;
int base;
int data_ref;
u32 ret = 0;

cpu = smp_processor_id();
list_for_each_entry(sc, &lua_state_cpu_list, list) {
if (sc->cpu == cpu) {
L = sc->L;
break;
}
}

if (!L)
goto out;

base = lua_gettop(L);
if (lua_getglobal(L, func) != LUA_TFUNCTION) {
printk(KERN_WARNING "function %s not found\n", func);
goto out;
}

data_ref = ldata_newref(L, ctx->data, ctx->data_end - ctx->data);
if (lua_pcall(L, 1, 1, 0)) {
printk(KERN_WARNING "%s\n", lua_tostring(L, -1));
goto cleanup;
}
ret = lua_tointeger(L, -1);

cleanup:
lua_settop(L, base);
ldata_unref(L, data_ref);
out:
return ret;
}

/**
* dev_get_by_napi_id - find a device by napi_id
* @napi_id: ID of the NAPI struct
Expand Down Expand Up @@ -5183,6 +5244,20 @@ static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
return ret;
}

int generic_xdp_lua_install(char *lua_prog) {
lua_state_cpu *sc;

list_for_each_entry(sc, &lua_state_cpu_list, list) {
if (luaL_dostring(sc->L, lua_prog)) {
printk(KERN_WARNING "error: %s\nOn cpu: %d\n",
lua_tostring(sc->L, -1), sc->cpu);
return -1;
}
}

return 0;
}

static int netif_receive_skb_internal(struct sk_buff *skb)
{
int ret;
Expand Down Expand Up @@ -9800,6 +9875,7 @@ static struct pernet_operations __net_initdata default_device_ops = {
static int __init net_dev_init(void)
{
int i, rc = -ENOMEM;
lua_state_cpu *new_state_cpu;

BUG_ON(!dev_boot_phase);

Expand All @@ -9814,6 +9890,7 @@ static int __init net_dev_init(void)
INIT_LIST_HEAD(&ptype_base[i]);

INIT_LIST_HEAD(&offload_base);
INIT_LIST_HEAD(&lua_state_cpu_list);

if (register_pernet_subsys(&netdev_net_ops))
goto out;
Expand Down Expand Up @@ -9844,6 +9921,22 @@ static int __init net_dev_init(void)
init_gro_hash(&sd->backlog);
sd->backlog.poll = process_backlog;
sd->backlog.weight = weight_p;

new_state_cpu = (lua_state_cpu *) kmalloc(sizeof(lua_state_cpu), GFP_KERNEL);
if (!new_state_cpu)
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);
new_state_cpu->cpu = i;

list_add(&new_state_cpu->list, &lua_state_cpu_list);
}

dev_boot_phase = 0;
Expand Down
15 changes: 15 additions & 0 deletions net/core/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,19 @@ static const struct bpf_func_proto bpf_csum_diff_proto = {
.arg5_type = ARG_ANYTHING,
};

BPF_CALL_2(bpf_lua_run, struct xdp_buff *, ctx, const char *, func)
{
return lua_prog_run_xdp(ctx, func);
}

static const struct bpf_func_proto bpf_lua_run_proto = {
.func = bpf_lua_run,
.gpl_only = false,
.ret_type = RET_INTEGER,
.arg1_type = ARG_ANYTHING,
.arg2_type = ARG_ANYTHING,
};

BPF_CALL_2(bpf_csum_update, struct sk_buff *, skb, __wsum, csum)
{
/* The interface is to be used in combination with bpf_csum_diff()
Expand Down Expand Up @@ -6175,6 +6188,8 @@ xdp_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_tcp_check_syncookie:
return &bpf_tcp_check_syncookie_proto;
#endif
case BPF_FUNC_lua_run:
return &bpf_lua_run_proto;
default:
return bpf_base_func_proto(func_id);
}
Expand Down
5 changes: 5 additions & 0 deletions net/core/rtnetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,7 @@ 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 },
[IFLA_XDP_LUA] = { .type = NLA_STRING, .len = 4096 },
};

static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
Expand Down Expand Up @@ -2707,6 +2708,10 @@ static int do_setlink(const struct sk_buff *skb,
goto errout;
status |= DO_SETLINK_NOTIFY;
}
if (xdp[IFLA_XDP_LUA]) {
char *lua_prog = nla_data(xdp[IFLA_XDP_LUA]);
generic_xdp_lua_install(lua_prog);
}
}

errout:
Expand Down
3 changes: 3 additions & 0 deletions samples/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ hostprogs-y += test_cgrp2_attach
hostprogs-y += test_cgrp2_sock
hostprogs-y += test_cgrp2_sock2
hostprogs-y += xdp1
hostprogs-y += xdplua
hostprogs-y += xdp2
hostprogs-y += xdp_router_ipv4
hostprogs-y += test_current_task_under_cgroup
Expand Down Expand Up @@ -109,6 +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)
xdplua-objs := xdplua_user.o

# Tell kbuild to always build the programs
always := $(hostprogs-y)
Expand Down Expand Up @@ -170,6 +172,7 @@ always += xdp_sample_pkts_kern.o
always += ibumad_kern.o
always += hbm_out_kern.o
always += hbm_edt_kern.o
always += xdplua_kern.o

KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include
KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib/bpf/
Expand Down
1 change: 1 addition & 0 deletions samples/bpf/bpf_load.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map);

void read_trace_pipe(void);
int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
int bpf_set_link_xdp_lua(int ifindex, char *lua_prog);
#endif
13 changes: 13 additions & 0 deletions samples/bpf/xdplua_kern.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#define KBUILD_MODNAME "foo"
#include <uapi/linux/bpf.h>
#include "bpf_helpers.h"

SEC("xdp1")
int xdp_prog1(struct xdp_md *ctx)
{
char funcname[] = "callback";

return bpf_lua_run(ctx, funcname);
}

char _license[] SEC("license") = "GPL";
Loading