2020-08-19 12:27:58 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include "bpf_preload.h"
|
2023-01-28 08:06:43 +08:00
|
|
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
#include "iterators/iterators.lskel-little-endian.h"
|
|
|
|
#else
|
|
|
|
#include "iterators/iterators.lskel-big-endian.h"
|
|
|
|
#endif
|
2020-08-19 12:27:58 +08:00
|
|
|
|
2022-02-10 07:20:01 +08:00
|
|
|
static struct bpf_link *maps_link, *progs_link;
|
|
|
|
static struct iterators_bpf *skel;
|
2020-08-19 12:27:58 +08:00
|
|
|
|
2022-02-10 07:20:01 +08:00
|
|
|
static void free_links_and_skel(void)
|
|
|
|
{
|
|
|
|
if (!IS_ERR_OR_NULL(maps_link))
|
|
|
|
bpf_link_put(maps_link);
|
|
|
|
if (!IS_ERR_OR_NULL(progs_link))
|
|
|
|
bpf_link_put(progs_link);
|
|
|
|
iterators_bpf__destroy(skel);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int preload(struct bpf_preload_info *obj)
|
|
|
|
{
|
|
|
|
strlcpy(obj[0].link_name, "maps.debug", sizeof(obj[0].link_name));
|
|
|
|
obj[0].link = maps_link;
|
|
|
|
strlcpy(obj[1].link_name, "progs.debug", sizeof(obj[1].link_name));
|
|
|
|
obj[1].link = progs_link;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-08-19 12:27:58 +08:00
|
|
|
|
2022-02-10 07:20:01 +08:00
|
|
|
static struct bpf_preload_ops ops = {
|
2020-08-19 12:27:58 +08:00
|
|
|
.preload = preload,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2022-02-10 07:20:01 +08:00
|
|
|
static int load_skel(void)
|
2020-08-19 12:27:58 +08:00
|
|
|
{
|
2022-02-10 07:20:01 +08:00
|
|
|
int err;
|
2020-08-19 12:27:58 +08:00
|
|
|
|
2022-02-10 07:20:01 +08:00
|
|
|
skel = iterators_bpf__open();
|
|
|
|
if (!skel)
|
|
|
|
return -ENOMEM;
|
|
|
|
err = iterators_bpf__load(skel);
|
2020-08-19 12:27:58 +08:00
|
|
|
if (err)
|
2022-02-10 07:20:01 +08:00
|
|
|
goto out;
|
|
|
|
err = iterators_bpf__attach(skel);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
maps_link = bpf_link_get_from_fd(skel->links.dump_bpf_map_fd);
|
|
|
|
if (IS_ERR(maps_link)) {
|
|
|
|
err = PTR_ERR(maps_link);
|
|
|
|
goto out;
|
2020-08-19 12:27:58 +08:00
|
|
|
}
|
2022-02-10 07:20:01 +08:00
|
|
|
progs_link = bpf_link_get_from_fd(skel->links.dump_bpf_prog_fd);
|
|
|
|
if (IS_ERR(progs_link)) {
|
|
|
|
err = PTR_ERR(progs_link);
|
|
|
|
goto out;
|
2021-03-17 11:09:15 +08:00
|
|
|
}
|
2022-02-26 02:59:24 +08:00
|
|
|
/* Avoid taking over stdin/stdout/stderr of init process. Zeroing out
|
|
|
|
* makes skel_closenz() a no-op later in iterators_bpf__destroy().
|
|
|
|
*/
|
|
|
|
close_fd(skel->links.dump_bpf_map_fd);
|
|
|
|
skel->links.dump_bpf_map_fd = 0;
|
|
|
|
close_fd(skel->links.dump_bpf_prog_fd);
|
|
|
|
skel->links.dump_bpf_prog_fd = 0;
|
2020-08-19 12:27:58 +08:00
|
|
|
return 0;
|
2022-02-10 07:20:01 +08:00
|
|
|
out:
|
|
|
|
free_links_and_skel();
|
|
|
|
return err;
|
2020-08-19 12:27:58 +08:00
|
|
|
}
|
|
|
|
|
2022-02-10 07:20:01 +08:00
|
|
|
static int __init load(void)
|
2020-08-19 12:27:58 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-02-10 07:20:01 +08:00
|
|
|
err = load_skel();
|
2020-08-19 12:27:58 +08:00
|
|
|
if (err)
|
|
|
|
return err;
|
2022-02-10 07:20:01 +08:00
|
|
|
bpf_preload_ops = &ops;
|
2020-08-19 12:27:58 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-02-10 07:20:01 +08:00
|
|
|
static void __exit fini(void)
|
2020-08-19 12:27:58 +08:00
|
|
|
{
|
|
|
|
bpf_preload_ops = NULL;
|
2022-02-10 07:20:01 +08:00
|
|
|
free_links_and_skel();
|
2020-08-19 12:27:58 +08:00
|
|
|
}
|
2022-02-10 07:20:01 +08:00
|
|
|
late_initcall(load);
|
|
|
|
module_exit(fini);
|
2020-08-19 12:27:58 +08:00
|
|
|
MODULE_LICENSE("GPL");
|