18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2020 Facebook */
38c2ecf20Sopenharmony_ci#pragma once
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#define TASK_COMM_LEN 16
68c2ecf20Sopenharmony_ci#define MAX_ANCESTORS 4
78c2ecf20Sopenharmony_ci#define MAX_PATH 256
88c2ecf20Sopenharmony_ci#define KILL_TARGET_LEN 64
98c2ecf20Sopenharmony_ci#define CTL_MAXNAME 10
108c2ecf20Sopenharmony_ci#define MAX_ARGS_LEN 4096
118c2ecf20Sopenharmony_ci#define MAX_FILENAME_LEN 512
128c2ecf20Sopenharmony_ci#define MAX_ENVIRON_LEN 8192
138c2ecf20Sopenharmony_ci#define MAX_PATH_DEPTH 32
148c2ecf20Sopenharmony_ci#define MAX_FILEPATH_LENGTH (MAX_PATH_DEPTH * MAX_PATH)
158c2ecf20Sopenharmony_ci#define MAX_CGROUPS_PATH_DEPTH 8
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define MAX_METADATA_PAYLOAD_LEN TASK_COMM_LEN
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define MAX_CGROUP_PAYLOAD_LEN \
208c2ecf20Sopenharmony_ci	(MAX_PATH * 2 + (MAX_PATH * MAX_CGROUPS_PATH_DEPTH))
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define MAX_CAP_PAYLOAD_LEN (MAX_METADATA_PAYLOAD_LEN + MAX_CGROUP_PAYLOAD_LEN)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define MAX_SYSCTL_PAYLOAD_LEN \
258c2ecf20Sopenharmony_ci	(MAX_METADATA_PAYLOAD_LEN + MAX_CGROUP_PAYLOAD_LEN + CTL_MAXNAME + MAX_PATH)
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define MAX_KILL_PAYLOAD_LEN \
288c2ecf20Sopenharmony_ci	(MAX_METADATA_PAYLOAD_LEN + MAX_CGROUP_PAYLOAD_LEN + TASK_COMM_LEN + \
298c2ecf20Sopenharmony_ci	 KILL_TARGET_LEN)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define MAX_EXEC_PAYLOAD_LEN \
328c2ecf20Sopenharmony_ci	(MAX_METADATA_PAYLOAD_LEN + MAX_CGROUP_PAYLOAD_LEN + MAX_FILENAME_LEN + \
338c2ecf20Sopenharmony_ci	 MAX_ARGS_LEN + MAX_ENVIRON_LEN)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define MAX_FILEMOD_PAYLOAD_LEN \
368c2ecf20Sopenharmony_ci	(MAX_METADATA_PAYLOAD_LEN + MAX_CGROUP_PAYLOAD_LEN + MAX_FILEPATH_LENGTH + \
378c2ecf20Sopenharmony_ci	 MAX_FILEPATH_LENGTH)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cienum data_type {
408c2ecf20Sopenharmony_ci	INVALID_EVENT,
418c2ecf20Sopenharmony_ci	EXEC_EVENT,
428c2ecf20Sopenharmony_ci	FORK_EVENT,
438c2ecf20Sopenharmony_ci	KILL_EVENT,
448c2ecf20Sopenharmony_ci	SYSCTL_EVENT,
458c2ecf20Sopenharmony_ci	FILEMOD_EVENT,
468c2ecf20Sopenharmony_ci	MAX_DATA_TYPE_EVENT
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cienum filemod_type {
508c2ecf20Sopenharmony_ci	FMOD_OPEN,
518c2ecf20Sopenharmony_ci	FMOD_LINK,
528c2ecf20Sopenharmony_ci	FMOD_SYMLINK,
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct ancestors_data_t {
568c2ecf20Sopenharmony_ci	pid_t ancestor_pids[MAX_ANCESTORS];
578c2ecf20Sopenharmony_ci	uint32_t ancestor_exec_ids[MAX_ANCESTORS];
588c2ecf20Sopenharmony_ci	uint64_t ancestor_start_times[MAX_ANCESTORS];
598c2ecf20Sopenharmony_ci	uint32_t num_ancestors;
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct var_metadata_t {
638c2ecf20Sopenharmony_ci	enum data_type type;
648c2ecf20Sopenharmony_ci	pid_t pid;
658c2ecf20Sopenharmony_ci	uint32_t exec_id;
668c2ecf20Sopenharmony_ci	uid_t uid;
678c2ecf20Sopenharmony_ci	gid_t gid;
688c2ecf20Sopenharmony_ci	uint64_t start_time;
698c2ecf20Sopenharmony_ci	uint32_t cpu_id;
708c2ecf20Sopenharmony_ci	uint64_t bpf_stats_num_perf_events;
718c2ecf20Sopenharmony_ci	uint64_t bpf_stats_start_ktime_ns;
728c2ecf20Sopenharmony_ci	uint8_t comm_length;
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistruct cgroup_data_t {
768c2ecf20Sopenharmony_ci	ino_t cgroup_root_inode;
778c2ecf20Sopenharmony_ci	ino_t cgroup_proc_inode;
788c2ecf20Sopenharmony_ci	uint64_t cgroup_root_mtime;
798c2ecf20Sopenharmony_ci	uint64_t cgroup_proc_mtime;
808c2ecf20Sopenharmony_ci	uint16_t cgroup_root_length;
818c2ecf20Sopenharmony_ci	uint16_t cgroup_proc_length;
828c2ecf20Sopenharmony_ci	uint16_t cgroup_full_length;
838c2ecf20Sopenharmony_ci	int cgroup_full_path_root_pos;
848c2ecf20Sopenharmony_ci};
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistruct var_sysctl_data_t {
878c2ecf20Sopenharmony_ci	struct var_metadata_t meta;
888c2ecf20Sopenharmony_ci	struct cgroup_data_t cgroup_data;
898c2ecf20Sopenharmony_ci	struct ancestors_data_t ancestors_info;
908c2ecf20Sopenharmony_ci	uint8_t sysctl_val_length;
918c2ecf20Sopenharmony_ci	uint16_t sysctl_path_length;
928c2ecf20Sopenharmony_ci	char payload[MAX_SYSCTL_PAYLOAD_LEN];
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistruct var_kill_data_t {
968c2ecf20Sopenharmony_ci	struct var_metadata_t meta;
978c2ecf20Sopenharmony_ci	struct cgroup_data_t cgroup_data;
988c2ecf20Sopenharmony_ci	struct ancestors_data_t ancestors_info;
998c2ecf20Sopenharmony_ci	pid_t kill_target_pid;
1008c2ecf20Sopenharmony_ci	int kill_sig;
1018c2ecf20Sopenharmony_ci	uint32_t kill_count;
1028c2ecf20Sopenharmony_ci	uint64_t last_kill_time;
1038c2ecf20Sopenharmony_ci	uint8_t kill_target_name_length;
1048c2ecf20Sopenharmony_ci	uint8_t kill_target_cgroup_proc_length;
1058c2ecf20Sopenharmony_ci	char payload[MAX_KILL_PAYLOAD_LEN];
1068c2ecf20Sopenharmony_ci	size_t payload_length;
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistruct var_exec_data_t {
1108c2ecf20Sopenharmony_ci	struct var_metadata_t meta;
1118c2ecf20Sopenharmony_ci	struct cgroup_data_t cgroup_data;
1128c2ecf20Sopenharmony_ci	pid_t parent_pid;
1138c2ecf20Sopenharmony_ci	uint32_t parent_exec_id;
1148c2ecf20Sopenharmony_ci	uid_t parent_uid;
1158c2ecf20Sopenharmony_ci	uint64_t parent_start_time;
1168c2ecf20Sopenharmony_ci	uint16_t bin_path_length;
1178c2ecf20Sopenharmony_ci	uint16_t cmdline_length;
1188c2ecf20Sopenharmony_ci	uint16_t environment_length;
1198c2ecf20Sopenharmony_ci	char payload[MAX_EXEC_PAYLOAD_LEN];
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistruct var_fork_data_t {
1238c2ecf20Sopenharmony_ci	struct var_metadata_t meta;
1248c2ecf20Sopenharmony_ci	pid_t parent_pid;
1258c2ecf20Sopenharmony_ci	uint32_t parent_exec_id;
1268c2ecf20Sopenharmony_ci	uint64_t parent_start_time;
1278c2ecf20Sopenharmony_ci	char payload[MAX_METADATA_PAYLOAD_LEN];
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistruct var_filemod_data_t {
1318c2ecf20Sopenharmony_ci	struct var_metadata_t meta;
1328c2ecf20Sopenharmony_ci	struct cgroup_data_t cgroup_data;
1338c2ecf20Sopenharmony_ci	enum filemod_type fmod_type;
1348c2ecf20Sopenharmony_ci	unsigned int dst_flags;
1358c2ecf20Sopenharmony_ci	uint32_t src_device_id;
1368c2ecf20Sopenharmony_ci	uint32_t dst_device_id;
1378c2ecf20Sopenharmony_ci	ino_t src_inode;
1388c2ecf20Sopenharmony_ci	ino_t dst_inode;
1398c2ecf20Sopenharmony_ci	uint16_t src_filepath_length;
1408c2ecf20Sopenharmony_ci	uint16_t dst_filepath_length;
1418c2ecf20Sopenharmony_ci	char payload[MAX_FILEMOD_PAYLOAD_LEN];
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistruct profiler_config_struct {
1458c2ecf20Sopenharmony_ci	bool fetch_cgroups_from_bpf;
1468c2ecf20Sopenharmony_ci	ino_t cgroup_fs_inode;
1478c2ecf20Sopenharmony_ci	ino_t cgroup_login_session_inode;
1488c2ecf20Sopenharmony_ci	uint64_t kill_signals_mask;
1498c2ecf20Sopenharmony_ci	ino_t inode_filter;
1508c2ecf20Sopenharmony_ci	uint32_t stale_info_secs;
1518c2ecf20Sopenharmony_ci	bool use_variable_buffers;
1528c2ecf20Sopenharmony_ci	bool read_environ_from_exec;
1538c2ecf20Sopenharmony_ci	bool enable_cgroup_v1_resolver;
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistruct bpf_func_stats_data {
1578c2ecf20Sopenharmony_ci	uint64_t time_elapsed_ns;
1588c2ecf20Sopenharmony_ci	uint64_t num_executions;
1598c2ecf20Sopenharmony_ci	uint64_t num_perf_events;
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistruct bpf_func_stats_ctx {
1638c2ecf20Sopenharmony_ci	uint64_t start_time_ns;
1648c2ecf20Sopenharmony_ci	struct bpf_func_stats_data* bpf_func_stats_data_val;
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cienum bpf_function_id {
1688c2ecf20Sopenharmony_ci	profiler_bpf_proc_sys_write,
1698c2ecf20Sopenharmony_ci	profiler_bpf_sched_process_exec,
1708c2ecf20Sopenharmony_ci	profiler_bpf_sched_process_exit,
1718c2ecf20Sopenharmony_ci	profiler_bpf_sys_enter_kill,
1728c2ecf20Sopenharmony_ci	profiler_bpf_do_filp_open_ret,
1738c2ecf20Sopenharmony_ci	profiler_bpf_sched_process_fork,
1748c2ecf20Sopenharmony_ci	profiler_bpf_vfs_link,
1758c2ecf20Sopenharmony_ci	profiler_bpf_vfs_symlink,
1768c2ecf20Sopenharmony_ci	profiler_bpf_max_function_id
1778c2ecf20Sopenharmony_ci};
178