106f6ba60Sopenharmony_ci/* 206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. 306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License. 506f6ba60Sopenharmony_ci * You may obtain a copy of the License at 606f6ba60Sopenharmony_ci * 706f6ba60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 806f6ba60Sopenharmony_ci * 906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and 1306f6ba60Sopenharmony_ci * limitations under the License. 1406f6ba60Sopenharmony_ci */ 1506f6ba60Sopenharmony_ci 1606f6ba60Sopenharmony_ci#ifndef HIEBPF_MACROS_H 1706f6ba60Sopenharmony_ci#define HIEBPF_MACROS_H 1806f6ba60Sopenharmony_ci 1906f6ba60Sopenharmony_ci#ifndef __user 2006f6ba60Sopenharmony_ci#define __user // user space address 2106f6ba60Sopenharmony_ci#endif 2206f6ba60Sopenharmony_ci 2306f6ba60Sopenharmony_ci/* 2406f6ba60Sopenharmony_ci * The following constants are used in both user and kernel space program, 2506f6ba60Sopenharmony_ci * since global variables are unavailable at present, these constants must 2606f6ba60Sopenharmony_ci * be defined as macros 2706f6ba60Sopenharmony_ci*/ 2806f6ba60Sopenharmony_ci#define MAX_STACK_LIMIT 64 2906f6ba60Sopenharmony_ci#define MAX_STACK_DEPTH 64 3006f6ba60Sopenharmony_ci 3106f6ba60Sopenharmony_ci#ifndef MAX_COMM_LEN 3206f6ba60Sopenharmony_ci#define MAX_COMM_LEN 16 3306f6ba60Sopenharmony_ci#endif 3406f6ba60Sopenharmony_ci 3506f6ba60Sopenharmony_ci#define MAX_START_EVENTS_NUM 256 3606f6ba60Sopenharmony_ci#define MAX_TARGET_PIDS 32 3706f6ba60Sopenharmony_ci#define MAX_TARGET_EVENTS 256 3806f6ba60Sopenharmony_ci#define MAX_FILENAME_LEN 512 3906f6ba60Sopenharmony_ci#define MAX_STACK_TRACE_ENTRIES 10000 4006f6ba60Sopenharmony_ci 4106f6ba60Sopenharmony_ci#define KERN_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP) 4206f6ba60Sopenharmony_ci#define USER_STACKID_FLAGS (0 | BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK | BPF_F_REUSE_STACKID) 4306f6ba60Sopenharmony_ci 4406f6ba60Sopenharmony_ci// BPF_RINGBUF_SIZE = 16M 4506f6ba60Sopenharmony_ci#define BPF_RINGBUF_SIZE (1 << 24) 4606f6ba60Sopenharmony_ci#define MAX_DENTRY_NAME_LEN 16 4706f6ba60Sopenharmony_ci 4806f6ba60Sopenharmony_ci#define TRACER_PID_INDEX 0 4906f6ba60Sopenharmony_ci#define BPF_LOG_LEVEL_INDEX 1 5006f6ba60Sopenharmony_ci#define MAX_STACK_LIMIT_INDEX 2 5106f6ba60Sopenharmony_ci#define MAX_STACK_DEPTH_INDEX 3 5206f6ba60Sopenharmony_ci#define UNWIND_FLAG_INDEX 4 5306f6ba60Sopenharmony_ci#define NR_CONFIG_VARIABLES 5 5406f6ba60Sopenharmony_ci 5506f6ba60Sopenharmony_ci#define NUM_STACK_TRACE_MAPS 1 5606f6ba60Sopenharmony_ci#define FSTRACE_STACK_TRACE_INDEX 0 5706f6ba60Sopenharmony_ci#define PFTRACE_STACK_TRACE_INDEX 0 5806f6ba60Sopenharmony_ci#define BIOTRACE_STACK_TRACE_INDEX 0 5906f6ba60Sopenharmony_ci 6006f6ba60Sopenharmony_ci#ifndef PAGE_SHIFT 6106f6ba60Sopenharmony_ci#define PAGE_SHIFT 12 6206f6ba60Sopenharmony_ci#endif 6306f6ba60Sopenharmony_ci 6406f6ba60Sopenharmony_ci#define FILE_TYPE_BITS 20 6506f6ba60Sopenharmony_ci#define DCACHE_REGULAR_TYPE (0x00400000 >> FILE_TYPE_BITS) /* Regular file type (or fallthru to such) */ 6606f6ba60Sopenharmony_ci#define DCACHE_DIRECTORY_TYPE (0x00200000 >> FILE_TYPE_BITS) /* Normal directory */ 6706f6ba60Sopenharmony_ci 6806f6ba60Sopenharmony_ci// the following definitions have to be stay consistent with their 6906f6ba60Sopenharmony_ci// counterparts in linux 5.10.0 7006f6ba60Sopenharmony_ci 7106f6ba60Sopenharmony_ci/* 7206f6ba60Sopenharmony_ci * Operations and flags common to the bio and request structures. 7306f6ba60Sopenharmony_ci * We use 8 bits for encoding the operation, and the remaining 24 for flags. 7406f6ba60Sopenharmony_ci * 7506f6ba60Sopenharmony_ci * The least significant bit of the operation number indicates the data 7606f6ba60Sopenharmony_ci * transfer direction: 7706f6ba60Sopenharmony_ci * 7806f6ba60Sopenharmony_ci * - if the least significant bit is set transfers are TO the device 7906f6ba60Sopenharmony_ci * - if the least significant bit is not set transfers are FROM the device 8006f6ba60Sopenharmony_ci * 8106f6ba60Sopenharmony_ci * If a operation does not transfer data the least significant bit has no 8206f6ba60Sopenharmony_ci * meaning. 8306f6ba60Sopenharmony_ci */ 8406f6ba60Sopenharmony_ci#define REQ_OP_BITS 8 8506f6ba60Sopenharmony_ci#define REQ_OP_MASK ((1 << REQ_OP_BITS) - 1) 8606f6ba60Sopenharmony_ci#define REQ_FLAG_BITS 24 8706f6ba60Sopenharmony_ci 8806f6ba60Sopenharmony_ci#define REQ_FAILFAST_DEV (1ULL << __REQ_FAILFAST_DEV) 8906f6ba60Sopenharmony_ci#define REQ_FAILFAST_TRANSPORT (1ULL << __REQ_FAILFAST_TRANSPORT) 9006f6ba60Sopenharmony_ci#define REQ_FAILFAST_DRIVER (1ULL << __REQ_FAILFAST_DRIVER) 9106f6ba60Sopenharmony_ci#define REQ_SYNC (1ULL << __REQ_SYNC) 9206f6ba60Sopenharmony_ci#define REQ_META (1ULL << __REQ_META) 9306f6ba60Sopenharmony_ci#define REQ_PRIO (1ULL << __REQ_PRIO) 9406f6ba60Sopenharmony_ci#define REQ_NOMERGE (1ULL << __REQ_NOMERGE) 9506f6ba60Sopenharmony_ci#define REQ_IDLE (1ULL << __REQ_IDLE) 9606f6ba60Sopenharmony_ci#define REQ_INTEGRITY (1ULL << __REQ_INTEGRITY) 9706f6ba60Sopenharmony_ci#define REQ_FUA (1ULL << __REQ_FUA) 9806f6ba60Sopenharmony_ci#define REQ_PREFLUSH (1ULL << __REQ_PREFLUSH) 9906f6ba60Sopenharmony_ci#define REQ_RAHEAD (1ULL << __REQ_RAHEAD) 10006f6ba60Sopenharmony_ci#define REQ_BACKGROUND (1ULL << __REQ_BACKGROUND) 10106f6ba60Sopenharmony_ci#define REQ_NOWAIT (1ULL << __REQ_NOWAIT) 10206f6ba60Sopenharmony_ci#define REQ_CGROUP_PUNT (1ULL << __REQ_CGROUP_PUNT) 10306f6ba60Sopenharmony_ci 10406f6ba60Sopenharmony_ci#define REQ_NOUNMAP (1ULL << __REQ_NOUNMAP) 10506f6ba60Sopenharmony_ci#define REQ_HIPRI (1ULL << __REQ_HIPRI) 10606f6ba60Sopenharmony_ci 10706f6ba60Sopenharmony_ci#define REQ_DRV (1ULL << __REQ_DRV) 10806f6ba60Sopenharmony_ci#define REQ_SWAP (1ULL << __REQ_SWAP) 10906f6ba60Sopenharmony_ci 11006f6ba60Sopenharmony_ci#define REQ_FAILFAST_MASK \ 11106f6ba60Sopenharmony_ci (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER) 11206f6ba60Sopenharmony_ci 11306f6ba60Sopenharmony_ci#define REQ_NOMERGE_FLAGS \ 11406f6ba60Sopenharmony_ci (REQ_NOMERGE | REQ_PREFLUSH | REQ_FUA) 11506f6ba60Sopenharmony_ci 11606f6ba60Sopenharmony_ci/* 11706f6ba60Sopenharmony_ci * Helper macro to manipulate data structures 11806f6ba60Sopenharmony_ci */ 11906f6ba60Sopenharmony_ci#ifndef offsetof 12006f6ba60Sopenharmony_ci#define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER) 12106f6ba60Sopenharmony_ci#endif 12206f6ba60Sopenharmony_ci#ifndef container_of 12306f6ba60Sopenharmony_ci#define container_of(ptr, type, member) \ 12406f6ba60Sopenharmony_ci ({ \ 12506f6ba60Sopenharmony_ci void *__mptr = (void *)(ptr); \ 12606f6ba60Sopenharmony_ci ((type *)(__mptr - offsetof(type, member))); \ 12706f6ba60Sopenharmony_ci }) 12806f6ba60Sopenharmony_ci#endif 12906f6ba60Sopenharmony_ci 13006f6ba60Sopenharmony_ci/** 13106f6ba60Sopenharmony_ci * list_for_each - iterate over a list 13206f6ba60Sopenharmony_ci * @pos: the &struct list_head to use as a loop cursor. 13306f6ba60Sopenharmony_ci * @head: the head for your list. 13406f6ba60Sopenharmony_ci */ 13506f6ba60Sopenharmony_ci#ifndef list_for_each 13606f6ba60Sopenharmony_ci#define list_for_each(pos, head) \ 13706f6ba60Sopenharmony_ci for (pos = (head)->next; pos != (head); pos = pos->next) 13806f6ba60Sopenharmony_ci#endif 13906f6ba60Sopenharmony_ci 14006f6ba60Sopenharmony_ci/** 14106f6ba60Sopenharmony_ci * list_entry - get the struct for this entry 14206f6ba60Sopenharmony_ci * @ptr: the &struct list_head pointer. 14306f6ba60Sopenharmony_ci * @type: the type of the struct this is embedded in. 14406f6ba60Sopenharmony_ci * @member: the name of the list_head within the struct. 14506f6ba60Sopenharmony_ci */ 14606f6ba60Sopenharmony_ci#ifndef list_entry 14706f6ba60Sopenharmony_ci#define list_entry(ptr, type, member) \ 14806f6ba60Sopenharmony_ci container_of(ptr, type, member) 14906f6ba60Sopenharmony_ci#endif 15006f6ba60Sopenharmony_ci 15106f6ba60Sopenharmony_ci 15206f6ba60Sopenharmony_ci#define hlist_entry(ptr, type, member) container_of(ptr, type, member) 15306f6ba60Sopenharmony_ci 15406f6ba60Sopenharmony_ci#define hlist_for_each(pos, head) \ 15506f6ba60Sopenharmony_ci for (pos = (head)->first; pos ; pos = pos->next) 15606f6ba60Sopenharmony_ci 15706f6ba60Sopenharmony_ci#endif