common.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
  4. * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
  5. * Copyright (c) 2023 David Vernet <dvernet@meta.com>
  6. */
  7. #ifndef __SCHED_EXT_COMMON_H
  8. #define __SCHED_EXT_COMMON_H
  9. #ifdef __KERNEL__
  10. #error "Should not be included by BPF programs"
  11. #endif
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <errno.h>
  17. typedef uint8_t u8;
  18. typedef uint16_t u16;
  19. typedef uint32_t u32;
  20. typedef uint64_t u64;
  21. typedef int8_t s8;
  22. typedef int16_t s16;
  23. typedef int32_t s32;
  24. typedef int64_t s64;
  25. #define SCX_BUG(__fmt, ...) \
  26. do { \
  27. fprintf(stderr, "[SCX_BUG] %s:%d", __FILE__, __LINE__); \
  28. if (errno) \
  29. fprintf(stderr, " (%s)\n", strerror(errno)); \
  30. else \
  31. fprintf(stderr, "\n"); \
  32. fprintf(stderr, __fmt __VA_OPT__(,) __VA_ARGS__); \
  33. fprintf(stderr, "\n"); \
  34. \
  35. exit(EXIT_FAILURE); \
  36. } while (0)
  37. #define SCX_BUG_ON(__cond, __fmt, ...) \
  38. do { \
  39. if (__cond) \
  40. SCX_BUG((__fmt) __VA_OPT__(,) __VA_ARGS__); \
  41. } while (0)
  42. /**
  43. * RESIZE_ARRAY - Convenience macro for resizing a BPF array
  44. * @__skel: the skeleton containing the array
  45. * @elfsec: the data section of the BPF program in which the array exists
  46. * @arr: the name of the array
  47. * @n: the desired array element count
  48. *
  49. * For BPF arrays declared with RESIZABLE_ARRAY(), this macro performs two
  50. * operations. It resizes the map which corresponds to the custom data
  51. * section that contains the target array. As a side effect, the BTF info for
  52. * the array is adjusted so that the array length is sized to cover the new
  53. * data section size. The second operation is reassigning the skeleton pointer
  54. * for that custom data section so that it points to the newly memory mapped
  55. * region.
  56. */
  57. #define RESIZE_ARRAY(__skel, elfsec, arr, n) \
  58. do { \
  59. size_t __sz; \
  60. bpf_map__set_value_size((__skel)->maps.elfsec##_##arr, \
  61. sizeof((__skel)->elfsec##_##arr->arr[0]) * (n)); \
  62. (__skel)->elfsec##_##arr = \
  63. bpf_map__initial_value((__skel)->maps.elfsec##_##arr, &__sz); \
  64. } while (0)
  65. #include "user_exit_info.h"
  66. #include "compat.h"
  67. #endif /* __SCHED_EXT_COMMON_H */