2020-08-18 21:27:58 -07: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-27 17:06:43 -07: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-18 21:27:58 -07:00
|
|
|
|
2022-02-09 16:20:01 -07:00
|
|
|
static struct bpf_link *maps_link, *progs_link;
|
|
|
|
static struct iterators_bpf *skel;
|
2020-08-18 21:27:58 -07:00
|
|
|
|
2022-02-09 16:20:01 -07: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)
|
|
|
|
{
|
2023-05-30 08:56:59 -07:00
|
|
|
strscpy(obj[0].link_name, "maps.debug", sizeof(obj[0].link_name));
|
2022-02-09 16:20:01 -07:00
|
|
|
obj[0].link = maps_link;
|
2023-05-30 08:56:59 -07:00
|
|
|
strscpy(obj[1].link_name, "progs.debug", sizeof(obj[1].link_name));
|
2022-02-09 16:20:01 -07:00
|
|
|
obj[1].link = progs_link;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-08-18 21:27:58 -07:00
|
|
|
|
2022-02-09 16:20:01 -07:00
|
|
|
static struct bpf_preload_ops ops = {
|
2020-08-18 21:27:58 -07:00
|
|
|
.preload = preload,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2022-02-09 16:20:01 -07:00
|
|
|
static int load_skel(void)
|
2020-08-18 21:27:58 -07:00
|
|
|
{
|
2022-02-09 16:20:01 -07:00
|
|
|
int err;
|
2020-08-18 21:27:58 -07:00
|
|
|
|
2022-02-09 16:20:01 -07:00
|
|
|
skel = iterators_bpf__open();
|
|
|
|
if (!skel)
|
|
|
|
return -ENOMEM;
|
|
|
|
err = iterators_bpf__load(skel);
|
2020-08-18 21:27:58 -07:00
|
|
|
if (err)
|
2022-02-09 16:20:01 -07: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-18 21:27:58 -07:00
|
|
|
}
|
2022-02-09 16:20:01 -07: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-16 20:09:15 -07:00
|
|
|
}
|
2022-02-25 11:59:24 -07: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-18 21:27:58 -07:00
|
|
|
return 0;
|
2022-02-09 16:20:01 -07:00
|
|
|
out:
|
|
|
|
free_links_and_skel();
|
|
|
|
return err;
|
2020-08-18 21:27:58 -07:00
|
|
|
}
|
|
|
|
|
2022-02-09 16:20:01 -07:00
|
|
|
static int __init load(void)
|
2020-08-18 21:27:58 -07:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2022-02-09 16:20:01 -07:00
|
|
|
err = load_skel();
|
2020-08-18 21:27:58 -07:00
|
|
|
if (err)
|
|
|
|
return err;
|
2022-02-09 16:20:01 -07:00
|
|
|
bpf_preload_ops = &ops;
|
2020-08-18 21:27:58 -07:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-02-09 16:20:01 -07:00
|
|
|
static void __exit fini(void)
|
2020-08-18 21:27:58 -07:00
|
|
|
{
|
|
|
|
bpf_preload_ops = NULL;
|
2022-02-09 16:20:01 -07:00
|
|
|
free_links_and_skel();
|
2020-08-18 21:27:58 -07:00
|
|
|
}
|
2022-02-09 16:20:01 -07:00
|
|
|
late_initcall(load);
|
|
|
|
module_exit(fini);
|
2020-08-18 21:27:58 -07:00
|
|
|
MODULE_LICENSE("GPL");
|