| 1 |
|
| 2 |
#ifndef __BPF_CORE_READ_H__ |
| 3 |
#define __BPF_CORE_READ_H__ |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
enum bpf_field_info_kind { |
| 14 |
BPF_FIELD_BYTE_OFFSET = 0, |
| 15 |
BPF_FIELD_BYTE_SIZE = 1, |
| 16 |
BPF_FIELD_EXISTS = 2, |
| 17 |
BPF_FIELD_SIGNED = 3, |
| 18 |
BPF_FIELD_LSHIFT_U64 = 4, |
| 19 |
BPF_FIELD_RSHIFT_U64 = 5, |
| 20 |
}; |
| 21 |
|
| 22 |
|
| 23 |
enum bpf_type_id_kind { |
| 24 |
BPF_TYPE_ID_LOCAL = 0, |
| 25 |
BPF_TYPE_ID_TARGET = 1, |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
enum bpf_type_info_kind { |
| 30 |
BPF_TYPE_EXISTS = 0, |
| 31 |
BPF_TYPE_SIZE = 1, |
| 32 |
BPF_TYPE_MATCHES = 2, |
| 33 |
}; |
| 34 |
|
| 35 |
|
| 36 |
enum bpf_enum_value_kind { |
| 37 |
BPF_ENUMVAL_EXISTS = 0, |
| 38 |
BPF_ENUMVAL_VALUE = 1, |
| 39 |
}; |
| 40 |
|
| 41 |
#define __CORE_RELO(src, field, info) \ |
| 42 |
__builtin_preserve_field_info((src)->field, BPF_FIELD_##info) |
| 43 |
|
| 44 |
#if __BYTE_ORDER == __LITTLE_ENDIAN |
| 45 |
#define __CORE_BITFIELD_PROBE_READ(dst, src, fld) \ |
| 46 |
bpf_probe_read_kernel( \ |
| 47 |
(void *)dst, \ |
| 48 |
__CORE_RELO(src, fld, BYTE_SIZE), \ |
| 49 |
(const void *)src + __CORE_RELO(src, fld, BYTE_OFFSET)) |
| 50 |
#else |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#define __CORE_BITFIELD_PROBE_READ(dst, src, fld) \ |
| 56 |
bpf_probe_read_kernel( \ |
| 57 |
(void *)dst + (8 - __CORE_RELO(src, fld, BYTE_SIZE)), \ |
| 58 |
__CORE_RELO(src, fld, BYTE_SIZE), \ |
| 59 |
(const void *)src + __CORE_RELO(src, fld, BYTE_OFFSET)) |
| 60 |
#endif |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#define BPF_CORE_READ_BITFIELD_PROBED(s, field) ({ \ |
| 71 |
unsigned long long val = 0; \ |
| 72 |
\ |
| 73 |
__CORE_BITFIELD_PROBE_READ(&val, s, field); \ |
| 74 |
val <<= __CORE_RELO(s, field, LSHIFT_U64); \ |
| 75 |
if (__CORE_RELO(s, field, SIGNED)) \ |
| 76 |
val = ((long long)val) >> __CORE_RELO(s, field, RSHIFT_U64); \ |
| 77 |
else \ |
| 78 |
val = val >> __CORE_RELO(s, field, RSHIFT_U64); \ |
| 79 |
val; \ |
| 80 |
}) |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
#define BPF_CORE_READ_BITFIELD(s, field) ({ \ |
| 89 |
const void *p = (const void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \ |
| 90 |
unsigned long long val; \ |
| 91 |
\ |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
\ |
| 98 |
asm volatile("" : "=r"(p) : "0"(p)); \ |
| 99 |
\ |
| 100 |
switch (__CORE_RELO(s, field, BYTE_SIZE)) { \ |
| 101 |
case 1: val = *(const unsigned char *)p; break; \ |
| 102 |
case 2: val = *(const unsigned short *)p; break; \ |
| 103 |
case 4: val = *(const unsigned int *)p; break; \ |
| 104 |
case 8: val = *(const unsigned long long *)p; break; \ |
| 105 |
} \ |
| 106 |
val <<= __CORE_RELO(s, field, LSHIFT_U64); \ |
| 107 |
if (__CORE_RELO(s, field, SIGNED)) \ |
| 108 |
val = ((long long)val) >> __CORE_RELO(s, field, RSHIFT_U64); \ |
| 109 |
else \ |
| 110 |
val = val >> __CORE_RELO(s, field, RSHIFT_U64); \ |
| 111 |
val; \ |
| 112 |
}) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#define bpf_core_field_exists(field) \ |
| 121 |
__builtin_preserve_field_info(field, BPF_FIELD_EXISTS) |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#define bpf_core_field_size(field) \ |
| 128 |
__builtin_preserve_field_info(field, BPF_FIELD_BYTE_SIZE) |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#define bpf_core_type_id_local(type) \ |
| 136 |
__builtin_btf_type_id(*(typeof(type) *)0, BPF_TYPE_ID_LOCAL) |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#define bpf_core_type_id_kernel(type) \ |
| 146 |
__builtin_btf_type_id(*(typeof(type) *)0, BPF_TYPE_ID_TARGET) |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
#define bpf_core_type_exists(type) \ |
| 156 |
__builtin_preserve_type_info(*(typeof(type) *)0, BPF_TYPE_EXISTS) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
#define bpf_core_type_matches(type) \ |
| 166 |
__builtin_preserve_type_info(*(typeof(type) *)0, BPF_TYPE_MATCHES) |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
#define bpf_core_type_size(type) \ |
| 177 |
__builtin_preserve_type_info(*(typeof(type) *)0, BPF_TYPE_SIZE) |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
#define bpf_core_enum_value_exists(enum_type, enum_value) \ |
| 188 |
__builtin_preserve_enum_value(*(typeof(enum_type) *)enum_value, BPF_ENUMVAL_EXISTS) |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
#define bpf_core_enum_value(enum_type, enum_value) \ |
| 199 |
__builtin_preserve_enum_value(*(typeof(enum_type) *)enum_value, BPF_ENUMVAL_VALUE) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
#define bpf_core_read(dst, sz, src) \ |
| 218 |
bpf_probe_read_kernel(dst, sz, (const void *)__builtin_preserve_access_index(src)) |
| 219 |
|
| 220 |
|
| 221 |
#define bpf_core_read_user(dst, sz, src) \ |
| 222 |
bpf_probe_read_user(dst, sz, (const void *)__builtin_preserve_access_index(src)) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
#define bpf_core_read_str(dst, sz, src) \ |
| 229 |
bpf_probe_read_kernel_str(dst, sz, (const void *)__builtin_preserve_access_index(src)) |
| 230 |
|
| 231 |
|
| 232 |
#define bpf_core_read_user_str(dst, sz, src) \ |
| 233 |
bpf_probe_read_user_str(dst, sz, (const void *)__builtin_preserve_access_index(src)) |
| 234 |
|
| 235 |
#define ___concat(a, b) a ## b |
| 236 |
#define ___apply(fn, n) ___concat(fn, n) |
| 237 |
#define ___nth(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, __11, N, ...) N |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
#define ___narg(...) ___nth(_, ##__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
#define ___empty(...) ___nth(_, ##__VA_ARGS__, N, N, N, N, N, N, N, N, N, N, 0) |
| 250 |
|
| 251 |
#define ___last1(x) x |
| 252 |
#define ___last2(a, x) x |
| 253 |
#define ___last3(a, b, x) x |
| 254 |
#define ___last4(a, b, c, x) x |
| 255 |
#define ___last5(a, b, c, d, x) x |
| 256 |
#define ___last6(a, b, c, d, e, x) x |
| 257 |
#define ___last7(a, b, c, d, e, f, x) x |
| 258 |
#define ___last8(a, b, c, d, e, f, g, x) x |
| 259 |
#define ___last9(a, b, c, d, e, f, g, h, x) x |
| 260 |
#define ___last10(a, b, c, d, e, f, g, h, i, x) x |
| 261 |
#define ___last(...) ___apply(___last, ___narg(__VA_ARGS__))(__VA_ARGS__) |
| 262 |
|
| 263 |
#define ___nolast2(a, _) a |
| 264 |
#define ___nolast3(a, b, _) a, b |
| 265 |
#define ___nolast4(a, b, c, _) a, b, c |
| 266 |
#define ___nolast5(a, b, c, d, _) a, b, c, d |
| 267 |
#define ___nolast6(a, b, c, d, e, _) a, b, c, d, e |
| 268 |
#define ___nolast7(a, b, c, d, e, f, _) a, b, c, d, e, f |
| 269 |
#define ___nolast8(a, b, c, d, e, f, g, _) a, b, c, d, e, f, g |
| 270 |
#define ___nolast9(a, b, c, d, e, f, g, h, _) a, b, c, d, e, f, g, h |
| 271 |
#define ___nolast10(a, b, c, d, e, f, g, h, i, _) a, b, c, d, e, f, g, h, i |
| 272 |
#define ___nolast(...) ___apply(___nolast, ___narg(__VA_ARGS__))(__VA_ARGS__) |
| 273 |
|
| 274 |
#define ___arrow1(a) a |
| 275 |
#define ___arrow2(a, b) a->b |
| 276 |
#define ___arrow3(a, b, c) a->b->c |
| 277 |
#define ___arrow4(a, b, c, d) a->b->c->d |
| 278 |
#define ___arrow5(a, b, c, d, e) a->b->c->d->e |
| 279 |
#define ___arrow6(a, b, c, d, e, f) a->b->c->d->e->f |
| 280 |
#define ___arrow7(a, b, c, d, e, f, g) a->b->c->d->e->f->g |
| 281 |
#define ___arrow8(a, b, c, d, e, f, g, h) a->b->c->d->e->f->g->h |
| 282 |
#define ___arrow9(a, b, c, d, e, f, g, h, i) a->b->c->d->e->f->g->h->i |
| 283 |
#define ___arrow10(a, b, c, d, e, f, g, h, i, j) a->b->c->d->e->f->g->h->i->j |
| 284 |
#define ___arrow(...) ___apply(___arrow, ___narg(__VA_ARGS__))(__VA_ARGS__) |
| 285 |
|
| 286 |
#define ___type(...) typeof(___arrow(__VA_ARGS__)) |
| 287 |
|
| 288 |
#define ___read(read_fn, dst, src_type, src, accessor) \ |
| 289 |
read_fn((void *)(dst), sizeof(*(dst)), &((src_type)(src))->accessor) |
| 290 |
|
| 291 |
|
| 292 |
#define ___rd_first(fn, src, a) ___read(fn, &__t, ___type(src), src, a); |
| 293 |
#define ___rd_last(fn, ...) \ |
| 294 |
___read(fn, &__t, ___type(___nolast(__VA_ARGS__)), __t, ___last(__VA_ARGS__)); |
| 295 |
#define ___rd_p1(fn, ...) const void *__t; ___rd_first(fn, __VA_ARGS__) |
| 296 |
#define ___rd_p2(fn, ...) ___rd_p1(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__) |
| 297 |
#define ___rd_p3(fn, ...) ___rd_p2(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__) |
| 298 |
#define ___rd_p4(fn, ...) ___rd_p3(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__) |
| 299 |
#define ___rd_p5(fn, ...) ___rd_p4(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__) |
| 300 |
#define ___rd_p6(fn, ...) ___rd_p5(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__) |
| 301 |
#define ___rd_p7(fn, ...) ___rd_p6(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__) |
| 302 |
#define ___rd_p8(fn, ...) ___rd_p7(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__) |
| 303 |
#define ___rd_p9(fn, ...) ___rd_p8(fn, ___nolast(__VA_ARGS__)) ___rd_last(fn, __VA_ARGS__) |
| 304 |
#define ___read_ptrs(fn, src, ...) \ |
| 305 |
___apply(___rd_p, ___narg(__VA_ARGS__))(fn, src, __VA_ARGS__) |
| 306 |
|
| 307 |
#define ___core_read0(fn, fn_ptr, dst, src, a) \ |
| 308 |
___read(fn, dst, ___type(src), src, a); |
| 309 |
#define ___core_readN(fn, fn_ptr, dst, src, ...) \ |
| 310 |
___read_ptrs(fn_ptr, src, ___nolast(__VA_ARGS__)) \ |
| 311 |
___read(fn, dst, ___type(src, ___nolast(__VA_ARGS__)), __t, \ |
| 312 |
___last(__VA_ARGS__)); |
| 313 |
#define ___core_read(fn, fn_ptr, dst, src, a, ...) \ |
| 314 |
___apply(___core_read, ___empty(__VA_ARGS__))(fn, fn_ptr, dst, \ |
| 315 |
src, a, ##__VA_ARGS__) |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
#define BPF_CORE_READ_INTO(dst, src, a, ...) ({ \ |
| 323 |
___core_read(bpf_core_read, bpf_core_read, \ |
| 324 |
dst, (src), a, ##__VA_ARGS__) \ |
| 325 |
}) |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
#define BPF_CORE_READ_USER_INTO(dst, src, a, ...) ({ \ |
| 333 |
___core_read(bpf_core_read_user, bpf_core_read_user, \ |
| 334 |
dst, (src), a, ##__VA_ARGS__) \ |
| 335 |
}) |
| 336 |
|
| 337 |
|
| 338 |
#define BPF_PROBE_READ_INTO(dst, src, a, ...) ({ \ |
| 339 |
___core_read(bpf_probe_read, bpf_probe_read, \ |
| 340 |
dst, (src), a, ##__VA_ARGS__) \ |
| 341 |
}) |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
#define BPF_PROBE_READ_USER_INTO(dst, src, a, ...) ({ \ |
| 349 |
___core_read(bpf_probe_read_user, bpf_probe_read_user, \ |
| 350 |
dst, (src), a, ##__VA_ARGS__) \ |
| 351 |
}) |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
#define BPF_CORE_READ_STR_INTO(dst, src, a, ...) ({ \ |
| 359 |
___core_read(bpf_core_read_str, bpf_core_read, \ |
| 360 |
dst, (src), a, ##__VA_ARGS__) \ |
| 361 |
}) |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
#define BPF_CORE_READ_USER_STR_INTO(dst, src, a, ...) ({ \ |
| 369 |
___core_read(bpf_core_read_user_str, bpf_core_read_user, \ |
| 370 |
dst, (src), a, ##__VA_ARGS__) \ |
| 371 |
}) |
| 372 |
|
| 373 |
|
| 374 |
#define BPF_PROBE_READ_STR_INTO(dst, src, a, ...) ({ \ |
| 375 |
___core_read(bpf_probe_read_str, bpf_probe_read, \ |
| 376 |
dst, (src), a, ##__VA_ARGS__) \ |
| 377 |
}) |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
#define BPF_PROBE_READ_USER_STR_INTO(dst, src, a, ...) ({ \ |
| 386 |
___core_read(bpf_probe_read_user_str, bpf_probe_read_user, \ |
| 387 |
dst, (src), a, ##__VA_ARGS__) \ |
| 388 |
}) |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
#define BPF_CORE_READ(src, a, ...) ({ \ |
| 415 |
___type((src), a, ##__VA_ARGS__) __r; \ |
| 416 |
BPF_CORE_READ_INTO(&__r, (src), a, ##__VA_ARGS__); \ |
| 417 |
__r; \ |
| 418 |
}) |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
#define BPF_CORE_READ_USER(src, a, ...) ({ \ |
| 431 |
___type((src), a, ##__VA_ARGS__) __r; \ |
| 432 |
BPF_CORE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__); \ |
| 433 |
__r; \ |
| 434 |
}) |
| 435 |
|
| 436 |
|
| 437 |
#define BPF_PROBE_READ(src, a, ...) ({ \ |
| 438 |
___type((src), a, ##__VA_ARGS__) __r; \ |
| 439 |
BPF_PROBE_READ_INTO(&__r, (src), a, ##__VA_ARGS__); \ |
| 440 |
__r; \ |
| 441 |
}) |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
#define BPF_PROBE_READ_USER(src, a, ...) ({ \ |
| 450 |
___type((src), a, ##__VA_ARGS__) __r; \ |
| 451 |
BPF_PROBE_READ_USER_INTO(&__r, (src), a, ##__VA_ARGS__); \ |
| 452 |
__r; \ |
| 453 |
}) |
| 454 |
|
| 455 |
#endif |
| 456 |
|