Skip to content

Commit 37ebb23

Browse files
committed
SAVEPOINT: 2022-06-07 @ 15:33:40
1 parent f3da7ce commit 37ebb23

5 files changed

Lines changed: 65 additions & 25 deletions

File tree

api/api.$progname.h.in

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "${PROGNAME}.skel.h"
22

3-
int ${OPERATION}${PROGNAME}(struct config *conf, after_attach_fn_t cb, bpf_obj_fn_t obj_cb)
3+
#define ${PROGNAME}_has_rodata ${PROGNAME_WITH_RODATA}
4+
5+
int ${OPERATION}${PROGNAME}(struct config *conf, after_attach_fn_t cb, const void* rodata)
46
{
57
int err;
68
char buf[100];
@@ -15,14 +17,28 @@ int ${OPERATION}${PROGNAME}(struct config *conf, after_attach_fn_t cb, bpf_obj_f
1517
return 1;
1618
}
1719

18-
if (obj_cb) {
19-
err = obj_cb(obj);
20-
if (err)
21-
{
22-
fprintf(stderr, "traffico: fail calling obj callback\n");
23-
goto destroy_${PROGNAME};
24-
}
20+
// Set read-only data
21+
#if ${PROGNAME}_has_rodata
22+
struct bpf_map *m = bpf_object__find_map_by_name(obj->obj, ".rodata");
23+
if (!m || !bpf_map__is_internal(m))
24+
{
25+
log_err(conf, "fail: finding the .rodata map\n");
26+
return 1;
27+
}
28+
const char *m_name = bpf_map__name(m);
29+
size_t m_size = bpf_map__value_size(m);
30+
log_out(conf, "done: finding the %s map (size %d)\n", m_name, m_size);
31+
err = bpf_map__set_initial_value(m, rodata, m_size);
32+
if (err)
33+
{
34+
free(m);
35+
log_err(conf, "fail: setting the %s map\n", m_name);
36+
return 1;
2537
}
38+
log_out(conf, "done: setting the %s map\n", m_name);
39+
#else
40+
log_out(conf, "done: moving on: no .rodata map\n");
41+
#endif
2642

2743
err = ${PROGNAME}_bpf__load(obj);
2844
if (err)

api/api.h.in

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,9 @@ struct config
3131
FILE *out_stream;
3232
};
3333

34-
typedef int (*bpf_obj_fn_t)(void *obj);
35-
3634
typedef int (*after_attach_fn_t)(struct bpf_tc_hook hook, struct bpf_tc_opts opts);
3735

38-
typedef int (*attach_fn_t)(struct config *conf, after_attach_fn_t cb, bpf_obj_fn_t obj_cb);
36+
typedef int (*attach_fn_t)(struct config *conf, after_attach_fn_t cb, const void* rodata);
3937

4038
/// logging
4139
int print_log(FILE *f, bool verbosity, bool prefix, const char *fmt, va_list argptr)
@@ -78,7 +76,7 @@ int exit_after_attach(struct bpf_tc_hook hook, struct bpf_tc_opts opts)
7876
}
7977

8078
/// non-existing programs
81-
int ${OPERATION}0(struct config *conf, after_attach_fn_t cb, bpf_obj_fn_t obj_cb)
79+
int ${OPERATION}0(struct config *conf, after_attach_fn_t cb, const void *rodata)
8280
{
8381
return 1;
8482
}
@@ -88,15 +86,13 @@ ${API}
8886
/// dispatch
8987
attach_fn_t attach_fn[NUM_PROGRAMS] = { ${PROGRAMS_OPS_AS_SYMBOLS} };
9088

91-
int attach(struct config *conf, after_attach_fn_t cb, bpf_obj_fn_t obj_cb)
89+
int attach(struct config *conf, after_attach_fn_t cb, const void* rodata)
9290
{
9391
attach_fn_t fn = attach_fn[conf->program];
9492
if (fn) {
95-
return fn(conf, cb, obj_cb);
93+
return fn(conf, cb, rodata);
9694
}
97-
return ${OPERATION}0(conf, cb, obj_cb);
95+
return ${OPERATION}0(conf, cb, NULL);
9896
}
9997

100-
101-
10298
#endif // TRAFFICO_API_H

api/xmake.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ includes("xmake/repos.lua")
88
add_rules("mode.release", "mode.debug")
99

1010
-- target to generate API components for every BPF program
11-
target("every.api")
11+
target("every-api")
1212
set_kind("headeronly")
1313
includes("../bpf")
1414
add_deps("bpf")
@@ -23,10 +23,10 @@ target("every.api")
2323
-- target to generate the API
2424
target("api")
2525
set_kind("headeronly")
26-
add_deps("every.api")
26+
add_deps("every-api")
2727
on_config(function(target)
2828
import("xmake.modules.api", { rootdir = os.projectdir() })
29-
api(target, "every.api", true)
29+
api(target, "every-api", true)
3030

3131
import("actions.config.configfiles", { alias = "gen_configfiles", rootdir = os.programdir() })
3232
gen_configfiles()

traffico.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,8 @@ int main(int argc, char **argv)
244244

245245
// Execute
246246
log_info("prog: %s\n", g_programs_name[g_config.program]);
247-
return attach(&g_config, &await, NULL);
247+
__u32 input = 16843010;
248+
__u8 *val = malloc(4);
249+
memcpy(val, &input, 4); // memset(val, 1, 4);
250+
return attach(&g_config, &await, val);
248251
}

xmake/modules/api.lua

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import("core.project.project")
2+
import("lib.detect.check_cxsnippets")
23

34
-- get sourcefiles
45
function _get_programs(target_name)
@@ -17,6 +18,10 @@ function gen(target, source_target)
1718
if not target then
1819
raise("could not configure target")
1920
end
21+
local source_gendir = path.absolute(project.target(source_target):autogendir())
22+
if not source_gendir then
23+
raise("could not obtain the autogendir of the source target")
24+
end
2025

2126
target:set("configdir", target:autogendir())
2227

@@ -30,7 +35,22 @@ function gen(target, source_target)
3035
local tempconf = path.join(os.tmpdir(), confname)
3136
os.tryrm(tempconf)
3237
os.cp(configfile_template_path, tempconf)
33-
target:add("configfiles", tempconf, { variables = { PROGNAME = progname, OPERATION = "attach__" } })
38+
39+
local skelpath = path.join(source_gendir, progname .. ".skel.h")
40+
local has_rodata = check_cxsnippets("void test() {&((struct " .. progname .. "_bpf*)0)->rodata;}", {includes = skelpath}) and 1 or 0
41+
if has_rodata == 1 then
42+
utils.dump(
43+
check_cxsnippets('void test() {printf("%ld", sizeof(&((struct ' .. progname .. '_bpf*)0)->rodata));}', {includes = skelpath})
44+
)
45+
end
46+
47+
target:add("configfiles", tempconf, {
48+
variables = {
49+
PROGNAME = progname,
50+
OPERATION = "attach__",
51+
PROGNAME_WITH_RODATA = has_rodata
52+
}
53+
})
3454
end
3555
end
3656

@@ -51,18 +71,23 @@ function main(target, components_target, banner)
5171

5272
local op = vars[1].variables.OPERATION
5373
v["OPERATION"] = op
54-
74+
75+
local descr = '" - '
5576
local programs = {}
5677
table.insert(programs, "0")
57-
for _, v in ipairs(vars) do
78+
for i, v in ipairs(vars) do
5879
table.insert(programs, v.variables.PROGNAME)
80+
descr = descr .. v.variables.PROGNAME .. (v.variables.PROGNAME_WITH_RODATA == 1 and ' [input]' or '')
81+
if i ~= #vars then
82+
descr = descr .. '\\n - '
83+
end
5984
end
6085
table.sort(programs)
6186
v["PROGRAMS_AS_SYMBOLS"] = 'program_' .. table.concat(programs, ", program_")
6287
v["PROGRAMS_AS_STRINGS"] = '"' .. table.concat(programs, '", "') .. '"'
6388
v["PROGRAMS_OPS_AS_SYMBOLS"] = op .. table.concat(programs, ', ' .. op)
6489
table.remove(programs, 1)
65-
v["PROGRAMS_DESCRIPTION"] = '" - ' .. table.concat(programs, '\\n - ') .. '"'
90+
v["PROGRAMS_DESCRIPTION"] = descr .. '"'
6691

6792
local content = ""
6893
for i, c in ipairs(components) do

0 commit comments

Comments
 (0)