rmnet_module.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #ifndef __RMNET_MODULE_H__
  13. #define __RMNET_MODULE_H__
  14. #include <linux/rcupdate.h>
  15. enum {
  16. RMNET_MODULE_HOOK_OFFLOAD_INGRESS,
  17. RMNET_MODULE_HOOK_OFFLOAD_CHAIN_END,
  18. RMNET_MODULE_HOOK_SHS_SKB_ENTRY,
  19. RMNET_MODULE_HOOK_SHS_SKB_LL_ENTRY,
  20. RMNET_MODULE_HOOK_SHS_SWITCH,
  21. RMNET_MODULE_HOOK_PERF_TETHER_INGRESS,
  22. RMNET_MODULE_HOOK_PERF_TETHER_EGRESS,
  23. RMNET_MODULE_HOOK_PERF_TETHER_CMD,
  24. RMNET_MODULE_HOOK_PERF_INGRESS,
  25. RMNET_MODULE_HOOK_PERF_EGRESS,
  26. RMNET_MODULE_HOOK_APS_PRE_QUEUE,
  27. RMNET_MODULE_HOOK_APS_POST_QUEUE,
  28. RMNET_MODULE_HOOK_WLAN_FLOW_MATCH,
  29. RMNET_MODULE_HOOK_APS_DATA_INACTIVE,
  30. RMNET_MODULE_HOOK_APS_DATA_ACTIVE,
  31. __RMNET_MODULE_NUM_HOOKS,
  32. };
  33. struct rmnet_module_hook_register_info {
  34. int hooknum;
  35. void *func;
  36. };
  37. void
  38. rmnet_module_hook_register(const struct rmnet_module_hook_register_info *info,
  39. int hook_count);
  40. bool rmnet_module_hook_is_set(int hook);
  41. void
  42. rmnet_module_hook_unregister_no_sync(const struct rmnet_module_hook_register_info *info,
  43. int hook_count);
  44. static inline void
  45. rmnet_module_hook_unregister(const struct rmnet_module_hook_register_info *info,
  46. int hook_count)
  47. {
  48. rmnet_module_hook_unregister_no_sync(info, hook_count);
  49. synchronize_rcu();
  50. }
  51. /* Dummy macro. Can use kernel version later */
  52. #define __CAT(a, b) a ## b
  53. #define CAT(a, b) __CAT(a, b)
  54. #define RMNET_HOOK_PARAMS(args...) args
  55. #define RMNET_MODULE_HOOK_NUM(__hook) CAT(RMNET_MODULE_HOOK_, __hook)
  56. #define RMNET_MODULE_HOOK_PROTOCOL(proto...) proto
  57. #define RMNET_MODULE_HOOK_ARGS(args...) args
  58. #define RMNET_MODULE_HOOK_RETURN_TYPE(type) type
  59. /* A ...lovely... framework for checking if the argument passed in to a function
  60. * macro is a pair of parentheses.
  61. * If so, resolve to 1. Otherwise, 0.
  62. *
  63. * The idea here is that you pass the argument along with a "test" macro to
  64. * a "checker" macro. If the argument IS a pair of parentheses, this will cause
  65. * the tester macro to expand into multiple arguments.
  66. *
  67. * The key is that "checker" macro just returns the second argument it receives.
  68. * So have the "tester" macro expand to a set of arguments that makes 1 the
  69. * second argument, or 0 if it doesn't expand.
  70. */
  71. #define __RMNET_HOOK_SECOND_ARG(_, arg, ...) arg
  72. #define RMNET_HOOK_PARENTHESES_CHECKER(args...) \
  73. __RMNET_HOOK_SECOND_ARG(args, 0, )
  74. #define __RMNET_HOOK_PARENTHESES_TEST(arg) arg, 1,
  75. #define __RMNET_HOOK_IS_PARENTHESES_TEST(...) __RMNET_HOOK_PARENTHESES_TEST(XXX)
  76. #define RMNET_HOOK_IS_PARENTHESES(arg) \
  77. RMNET_HOOK_PARENTHESES_CHECKER(__RMNET_HOOK_IS_PARENTHESES_TEST arg)
  78. /* So what's the point of the above stuff, you ask?
  79. *
  80. * CPP can't actually do type checking ;). But what we CAN do is something
  81. * like this to determine if the type passed in is void. If so, this will
  82. * expand to (), causing the RMNET_HOOK_IS_PARENTHESES check to resolve to 1,
  83. * but if not, then the check resolves to 0.
  84. */
  85. #define __RMNET_HOOK_CHECK_TYPE_IS_void(arg) arg
  86. #define RMNET_HOOK_TYPE_IS_VOID(type) \
  87. RMNET_HOOK_IS_PARENTHESES( __RMNET_HOOK_CHECK_TYPE_IS_ ## type (()) )
  88. /* And now, we have some logic macros. The main macro will resolve
  89. * to one of the branches depending on the bool value passed in.
  90. */
  91. #define __IF_0(t_path, e_path...) e_path
  92. #define __IF_1(t_path, e_path...) t_path
  93. #define IF(arg) CAT(__IF_, arg)
  94. #define __NOT_1 0
  95. #define __NOT_0 1
  96. #define NOT(arg) CAT(__NOT_, arg)
  97. /* And now we combine this all, for a purely function macro way of splitting
  98. * return type handling...
  99. *
  100. * ...all to circumvent that you can't actually add #if conditionals in macro
  101. * expansions. It would have been much simpler that way. ;)
  102. */
  103. #define RMNET_HOOK_IF_NON_VOID_TYPE(type) \
  104. IF(NOT(RMNET_HOOK_TYPE_IS_VOID(type)))
  105. #define __RMNET_HOOK_PROTO(proto, ret_type)\
  106. RMNET_HOOK_IF_NON_VOID_TYPE(ret_type) \
  107. (RMNET_HOOK_PARAMS(ret_type *__ret_code, proto), \
  108. RMNET_HOOK_PARAMS(proto))
  109. #define __RMNET_HOOK_DECLARE(call, proto, ret_type) \
  110. int rmnet_module_hook_##call( \
  111. __RMNET_HOOK_PROTO(RMNET_HOOK_PARAMS(proto), ret_type));
  112. #undef RMNET_MODULE_HOOK
  113. #define RMNET_MODULE_HOOK(call, hook_num, proto, args, ret_type) \
  114. __RMNET_HOOK_DECLARE(call, RMNET_HOOK_PARAMS(proto), ret_type)
  115. #include "rmnet_hook.h"
  116. #endif