Skip to content

Commit 9949d1e

Browse files
VictorNogueiraRiolneto
authored andcommitted
add XDPLua map sample
1 parent 6cabbbc commit 9949d1e

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

samples/bpf/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ always += xdp_sample_pkts_kern.o
173173
always += ibumad_kern.o
174174
always += hbm_out_kern.o
175175
always += hbm_edt_kern.o
176+
# CONFIG_XDPLUA
177+
always += xdplua_map_kern.o
176178

177179
KBUILD_HOSTCFLAGS += -I$(objtree)/usr/include
178180
KBUILD_HOSTCFLAGS += -I$(srctree)/tools/lib/bpf/

samples/bpf/map.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--
2+
-- Copyright (C) 2019-2020 Victor Nogueira <victor.nogueira@ring-0.io>
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+
xdp = require'xdp'
14+
15+
function lookup(map)
16+
local val = xdp.maplookup(map, 1)
17+
print('val', val)
18+
end
19+
20+
function update(map)
21+
xdplua.mapupdate(map, 1, 3)
22+
end

samples/bpf/xdplua_map_kern.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2019-2020 Victor Nogueira <victor.nogueira@ring-0.io>
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+
#define KBUILD_MODNAME "foo"
14+
#include <uapi/linux/bpf.h>
15+
#include "bpf_helpers.h"
16+
17+
struct bpf_map_def SEC("maps") test_map = {
18+
.type = BPF_MAP_TYPE_ARRAY,
19+
.key_size = sizeof(int),
20+
.value_size = sizeof(int),
21+
.max_entries = 20,
22+
};
23+
24+
SEC("test_map")
25+
int xdp_test_map(struct xdp_md *ctx)
26+
{
27+
char lookupname[] = "lookup";
28+
char updatename[] = "update";
29+
30+
bpf_lua_setstate(ctx);
31+
bpf_lua_pushmap(ctx, &test_map);
32+
bpf_lua_pcall(ctx, updatename, 1, 0);
33+
34+
bpf_lua_pushmap(ctx, &test_map);
35+
bpf_lua_pcall(ctx, lookupname, 1, 0);
36+
37+
return XDP_PASS;
38+
}
39+
40+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)