rmnet_module.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright (c) 2022 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_SWITCH,
  20. RMNET_MODULE_HOOK_PERF_TETHER_INGRESS,
  21. RMNET_MODULE_HOOK_PERF_TETHER_EGRESS,
  22. RMNET_MODULE_HOOK_PERF_TETHER_CMD,
  23. RMNET_MODULE_HOOK_PERF_INGRESS,
  24. RMNET_MODULE_HOOK_PERF_EGRESS,
  25. RMNET_MODULE_HOOK_APS_PRE_QUEUE,
  26. RMNET_MODULE_HOOK_APS_POST_QUEUE,
  27. RMNET_MODULE_HOOK_WLAN_FLOW_MATCH,
  28. __RMNET_MODULE_NUM_HOOKS,
  29. };
  30. struct rmnet_module_hook_register_info {
  31. int hooknum;
  32. void *func;
  33. };
  34. void
  35. rmnet_module_hook_register(const struct rmnet_module_hook_register_info *info,
  36. int hook_count);
  37. bool rmnet_module_hook_is_set(int hook);
  38. void
  39. rmnet_module_hook_unregister_no_sync(const struct rmnet_module_hook_register_info *info,
  40. int hook_count);
  41. static inline void
  42. rmnet_module_hook_unregister(const struct rmnet_module_hook_register_info *info,
  43. int hook_count)
  44. {
  45. rmnet_module_hook_unregister_no_sync(info, hook_count);
  46. synchronize_rcu();
  47. }
  48. /* Dummy macro. Can use kernel version later */
  49. #define __CAT(a, b) a ## b
  50. #define CAT(a, b) __CAT(a, b)
  51. #define RMNET_HOOK_PARAMS(args...) args
  52. #define RMNET_MODULE_HOOK_NUM(__hook) CAT(RMNET_MODULE_HOOK_, __hook)
  53. #define RMNET_MODULE_HOOK_PROTOCOL(proto...) proto
  54. #define RMNET_MODULE_HOOK_ARGS(args...) args
  55. #define RMNET_MODULE_HOOK_RETURN_TYPE(type) type
  56. /* A ...lovely... framework for checking if the argument passed in to a function
  57. * macro is a pair of parentheses.
  58. * If so, resolve to 1. Otherwise, 0.
  59. *
  60. * The idea here is that you pass the argument along with a "test" macro to
  61. * a "checker" macro. If the argument IS a pair of parentheses, this will cause
  62. * the tester macro to expand into multiple arguments.
  63. *
  64. * The key is that "checker" macro just returns the second argument it receives.
  65. * So have the "tester" macro expand to a set of arguments that makes 1 the
  66. * second argument, or 0 if it doesn't expand.
  67. */
  68. #define __RMNET_HOOK_SECOND_ARG(_, arg, ...) arg
  69. #define RMNET_HOOK_PARENTHESES_CHECKER(args...) \
  70. __RMNET_HOOK_SECOND_ARG(args, 0, )
  71. #define __RMNET_HOOK_PARENTHESES_TEST(arg) arg, 1,
  72. #define __RMNET_HOOK_IS_PARENTHESES_TEST(...) __RMNET_HOOK_PARENTHESES_TEST(XXX)
  73. #define RMNET_HOOK_IS_PARENTHESES(arg) \
  74. RMNET_HOOK_PARENTHESES_CHECKER(__RMNET_HOOK_IS_PARENTHESES_TEST arg)
  75. /* So what's the point of the above stuff, you ask?
  76. *
  77. * CPP can't actually do type checking ;). But what we CAN do is something
  78. * like this to determine if the type passed in is void. If so, this will
  79. * expand to (), causing the RMNET_HOOK_IS_PARENTHESES check to resolve to 1,
  80. * but if not, then the check resolves to 0.
  81. */
  82. #define __RMNET_HOOK_CHECK_TYPE_IS_void(arg) arg
  83. #define RMNET_HOOK_TYPE_IS_VOID(type) \
  84. RMNET_HOOK_IS_PARENTHESES( __RMNET_HOOK_CHECK_TYPE_IS_ ## type (()) )
  85. /* And now, we have some logic macros. The main macro will resolve
  86. * to one of the branches depending on the bool value passed in.
  87. */
  88. #define __IF_0(t_path, e_path...) e_path
  89. #define __IF_1(t_path, e_path...) t_path
  90. #define IF(arg) CAT(__IF_, arg)
  91. #define __NOT_1 0
  92. #define __NOT_0 1
  93. #define NOT(arg) CAT(__NOT_, arg)
  94. /* And now we combine this all, for a purely function macro way of splitting
  95. * return type handling...
  96. *
  97. * ...all to circumvent that you can't actually add #if conditionals in macro
  98. * expansions. It would have been much simpler that way. ;)
  99. */
  100. #define RMNET_HOOK_IF_NON_VOID_TYPE(type) \
  101. IF(NOT(RMNET_HOOK_TYPE_IS_VOID(type)))
  102. #define __RMNET_HOOK_PROTO(proto, ret_type)\
  103. RMNET_HOOK_IF_NON_VOID_TYPE(ret_type) \
  104. (RMNET_HOOK_PARAMS(ret_type *__ret_code, proto), \
  105. RMNET_HOOK_PARAMS(proto))
  106. #define __RMNET_HOOK_DECLARE(call, proto, ret_type) \
  107. int rmnet_module_hook_##call( \
  108. __RMNET_HOOK_PROTO(RMNET_HOOK_PARAMS(proto), ret_type));
  109. #undef RMNET_MODULE_HOOK
  110. #define RMNET_MODULE_HOOK(call, hook_num, proto, args, ret_type) \
  111. __RMNET_HOOK_DECLARE(call, RMNET_HOOK_PARAMS(proto), ret_type)
  112. #include "rmnet_hook.h"
  113. #endif