rmnet_module.h 4.5 KB

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