• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef HIEBPF_MACROS_H
17 #define HIEBPF_MACROS_H
18 
19 #ifndef __user
20 #define __user // user space address
21 #endif
22 
23 /*
24  * The following constants are used in both user and kernel space program,
25  * since global variables are unavailable at present, these constants must
26  * be defined as macros
27 */
28 #define MAX_STACK_LIMIT       64
29 #define MAX_STACK_DEPTH       64
30 
31 #ifndef MAX_COMM_LEN
32 #define MAX_COMM_LEN                16
33 #endif
34 
35 #define MAX_START_EVENTS_NUM        256
36 #define MAX_TARGET_PIDS             32
37 #define MAX_TARGET_EVENTS           256
38 #define MAX_FILENAME_LEN            512
39 #define MAX_STACK_TRACE_ENTRIES     10000
40 
41 #define KERN_STACKID_FLAGS          (0 | BPF_F_FAST_STACK_CMP)
42 #define USER_STACKID_FLAGS          (0 | BPF_F_FAST_STACK_CMP | BPF_F_USER_STACK | BPF_F_REUSE_STACKID)
43 
44 // BPF_RINGBUF_SIZE = 16M
45 #define BPF_RINGBUF_SIZE            (1 << 24)
46 #define MAX_DENTRY_NAME_LEN         16
47 
48 #define TRACER_PID_INDEX            0
49 #define BPF_LOG_LEVEL_INDEX         1
50 #define MAX_STACK_LIMIT_INDEX       2
51 #define MAX_STACK_DEPTH_INDEX       3
52 #define UNWIND_FLAG_INDEX           4
53 #define NR_CONFIG_VARIABLES         5
54 
55 #define NUM_STACK_TRACE_MAPS        1
56 #define FSTRACE_STACK_TRACE_INDEX   0
57 #define PFTRACE_STACK_TRACE_INDEX   0
58 #define BIOTRACE_STACK_TRACE_INDEX  0
59 
60 #ifndef PAGE_SHIFT
61 #define PAGE_SHIFT                    12
62 #endif
63 
64 #define FILE_TYPE_BITS              20
65 #define DCACHE_REGULAR_TYPE         (0x00400000 >> FILE_TYPE_BITS) /* Regular file type (or fallthru to such) */
66 #define DCACHE_DIRECTORY_TYPE       (0x00200000 >> FILE_TYPE_BITS) /* Normal directory */
67 
68 // the following definitions have to be stay consistent with their
69 // counterparts in linux 5.10.0
70 
71 /*
72  * Operations and flags common to the bio and request structures.
73  * We use 8 bits for encoding the operation, and the remaining 24 for flags.
74  *
75  * The least significant bit of the operation number indicates the data
76  * transfer direction:
77  *
78  *   - if the least significant bit is set transfers are TO the device
79  *   - if the least significant bit is not set transfers are FROM the device
80  *
81  * If a operation does not transfer data the least significant bit has no
82  * meaning.
83  */
84 #define REQ_OP_BITS    8
85 #define REQ_OP_MASK    ((1 << REQ_OP_BITS) - 1)
86 #define REQ_FLAG_BITS    24
87 
88 #define REQ_FAILFAST_DEV    (1ULL << __REQ_FAILFAST_DEV)
89 #define REQ_FAILFAST_TRANSPORT    (1ULL << __REQ_FAILFAST_TRANSPORT)
90 #define REQ_FAILFAST_DRIVER    (1ULL << __REQ_FAILFAST_DRIVER)
91 #define REQ_SYNC        (1ULL << __REQ_SYNC)
92 #define REQ_META        (1ULL << __REQ_META)
93 #define REQ_PRIO        (1ULL << __REQ_PRIO)
94 #define REQ_NOMERGE        (1ULL << __REQ_NOMERGE)
95 #define REQ_IDLE        (1ULL << __REQ_IDLE)
96 #define REQ_INTEGRITY        (1ULL << __REQ_INTEGRITY)
97 #define REQ_FUA            (1ULL << __REQ_FUA)
98 #define REQ_PREFLUSH        (1ULL << __REQ_PREFLUSH)
99 #define REQ_RAHEAD        (1ULL << __REQ_RAHEAD)
100 #define REQ_BACKGROUND        (1ULL << __REQ_BACKGROUND)
101 #define REQ_NOWAIT        (1ULL << __REQ_NOWAIT)
102 #define REQ_CGROUP_PUNT        (1ULL << __REQ_CGROUP_PUNT)
103 
104 #define REQ_NOUNMAP        (1ULL << __REQ_NOUNMAP)
105 #define REQ_HIPRI        (1ULL << __REQ_HIPRI)
106 
107 #define REQ_DRV            (1ULL << __REQ_DRV)
108 #define REQ_SWAP        (1ULL << __REQ_SWAP)
109 
110 #define REQ_FAILFAST_MASK \
111     (REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | REQ_FAILFAST_DRIVER)
112 
113 #define REQ_NOMERGE_FLAGS \
114     (REQ_NOMERGE | REQ_PREFLUSH | REQ_FUA)
115 
116 /*
117  * Helper macro to manipulate data structures
118  */
119 #ifndef offsetof
120 #define offsetof(TYPE, MEMBER)    ((unsigned long)&((TYPE *)0)->MEMBER)
121 #endif
122 #ifndef container_of
123 #define container_of(ptr, type, member)                    \
124     ({                                                    \
125         void *__mptr = (void *)(ptr);                    \
126         ((type *)(__mptr - offsetof(type, member)));    \
127     })
128 #endif
129 
130 /**
131  * list_for_each    -    iterate over a list
132  * @pos:    the &struct list_head to use as a loop cursor.
133  * @head:    the head for your list.
134  */
135 #ifndef list_for_each
136 #define list_for_each(pos, head) \
137     for (pos = (head)->next; pos != (head); pos = pos->next)
138 #endif
139 
140 /**
141  * list_entry - get the struct for this entry
142  * @ptr:    the &struct list_head pointer.
143  * @type:    the type of the struct this is embedded in.
144  * @member:    the name of the list_head within the struct.
145  */
146 #ifndef list_entry
147 #define list_entry(ptr, type, member) \
148     container_of(ptr, type, member)
149 #endif
150 
151 
152 #define hlist_entry(ptr, type, member) container_of(ptr, type, member)
153 
154 #define hlist_for_each(pos, head) \
155     for (pos = (head)->first; pos ; pos = pos->next)
156 
157 #endif