genl_magic_struct.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #ifndef GENL_MAGIC_STRUCT_H
  2. #define GENL_MAGIC_STRUCT_H
  3. #ifndef GENL_MAGIC_FAMILY
  4. # error "you need to define GENL_MAGIC_FAMILY before inclusion"
  5. #endif
  6. #ifndef GENL_MAGIC_VERSION
  7. # error "you need to define GENL_MAGIC_VERSION before inclusion"
  8. #endif
  9. #ifndef GENL_MAGIC_INCLUDE_FILE
  10. # error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
  11. #endif
  12. #include <linux/genetlink.h>
  13. #include <linux/types.h>
  14. #define CONCAT__(a,b) a ## b
  15. #define CONCAT_(a,b) CONCAT__(a,b)
  16. extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
  17. extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void);
  18. /*
  19. * Extension of genl attribute validation policies {{{2
  20. */
  21. /*
  22. * @DRBD_GENLA_F_MANDATORY: By default, netlink ignores attributes it does not
  23. * know about. This flag can be set in nlattr->nla_type to indicate that this
  24. * attribute must not be ignored.
  25. *
  26. * We check and remove this flag in drbd_nla_check_mandatory() before
  27. * validating the attribute types and lengths via nla_parse_nested().
  28. */
  29. #define DRBD_GENLA_F_MANDATORY (1 << 14)
  30. /*
  31. * Flags specific to drbd and not visible at the netlink layer, used in
  32. * <struct>_from_attrs and <struct>_to_skb:
  33. *
  34. * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is
  35. * invalid.
  36. *
  37. * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be
  38. * included in unpriviledged get requests or broadcasts.
  39. *
  40. * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but
  41. * cannot subsequently be changed.
  42. */
  43. #define DRBD_F_REQUIRED (1 << 0)
  44. #define DRBD_F_SENSITIVE (1 << 1)
  45. #define DRBD_F_INVARIANT (1 << 2)
  46. #define __nla_type(x) ((__u16)((x) & NLA_TYPE_MASK & ~DRBD_GENLA_F_MANDATORY))
  47. /* }}}1
  48. * MAGIC
  49. * multi-include macro expansion magic starts here
  50. */
  51. /* MAGIC helpers {{{2 */
  52. /* possible field types */
  53. #define __flg_field(attr_nr, attr_flag, name) \
  54. __field(attr_nr, attr_flag, name, NLA_U8, char, \
  55. nla_get_u8, nla_put_u8, false)
  56. #define __u8_field(attr_nr, attr_flag, name) \
  57. __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
  58. nla_get_u8, nla_put_u8, false)
  59. #define __u16_field(attr_nr, attr_flag, name) \
  60. __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
  61. nla_get_u16, nla_put_u16, false)
  62. #define __u32_field(attr_nr, attr_flag, name) \
  63. __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
  64. nla_get_u32, nla_put_u32, false)
  65. #define __s32_field(attr_nr, attr_flag, name) \
  66. __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
  67. nla_get_u32, nla_put_u32, true)
  68. #define __u64_field(attr_nr, attr_flag, name) \
  69. __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
  70. nla_get_u64, nla_put_u64, false)
  71. #define __str_field(attr_nr, attr_flag, name, maxlen) \
  72. __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
  73. nla_strlcpy, nla_put, false)
  74. #define __bin_field(attr_nr, attr_flag, name, maxlen) \
  75. __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
  76. nla_memcpy, nla_put, false)
  77. /* fields with default values */
  78. #define __flg_field_def(attr_nr, attr_flag, name, default) \
  79. __flg_field(attr_nr, attr_flag, name)
  80. #define __u32_field_def(attr_nr, attr_flag, name, default) \
  81. __u32_field(attr_nr, attr_flag, name)
  82. #define __s32_field_def(attr_nr, attr_flag, name, default) \
  83. __s32_field(attr_nr, attr_flag, name)
  84. #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
  85. __str_field(attr_nr, attr_flag, name, maxlen)
  86. #define GENL_op_init(args...) args
  87. #define GENL_doit(handler) \
  88. .doit = handler, \
  89. .flags = GENL_ADMIN_PERM,
  90. #define GENL_dumpit(handler) \
  91. .dumpit = handler, \
  92. .flags = GENL_ADMIN_PERM,
  93. /* }}}1
  94. * Magic: define the enum symbols for genl_ops
  95. * Magic: define the enum symbols for top level attributes
  96. * Magic: define the enum symbols for nested attributes
  97. * {{{2
  98. */
  99. #undef GENL_struct
  100. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  101. #undef GENL_mc_group
  102. #define GENL_mc_group(group)
  103. #undef GENL_notification
  104. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  105. op_name = op_num,
  106. #undef GENL_op
  107. #define GENL_op(op_name, op_num, handler, tla_list) \
  108. op_name = op_num,
  109. enum {
  110. #include GENL_MAGIC_INCLUDE_FILE
  111. };
  112. #undef GENL_notification
  113. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  114. #undef GENL_op
  115. #define GENL_op(op_name, op_num, handler, attr_list)
  116. #undef GENL_struct
  117. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  118. tag_name = tag_number,
  119. enum {
  120. #include GENL_MAGIC_INCLUDE_FILE
  121. };
  122. #undef GENL_struct
  123. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  124. enum { \
  125. s_fields \
  126. };
  127. #undef __field
  128. #define __field(attr_nr, attr_flag, name, nla_type, type, \
  129. __get, __put, __is_signed) \
  130. T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
  131. #undef __array
  132. #define __array(attr_nr, attr_flag, name, nla_type, type, \
  133. maxlen, __get, __put, __is_signed) \
  134. T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
  135. #include GENL_MAGIC_INCLUDE_FILE
  136. /* }}}1
  137. * Magic: compile time assert unique numbers for operations
  138. * Magic: -"- unique numbers for top level attributes
  139. * Magic: -"- unique numbers for nested attributes
  140. * {{{2
  141. */
  142. #undef GENL_struct
  143. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  144. #undef GENL_op
  145. #define GENL_op(op_name, op_num, handler, attr_list) \
  146. case op_name:
  147. #undef GENL_notification
  148. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  149. case op_name:
  150. static inline void ct_assert_unique_operations(void)
  151. {
  152. switch (0) {
  153. #include GENL_MAGIC_INCLUDE_FILE
  154. case 0:
  155. ;
  156. }
  157. }
  158. #undef GENL_op
  159. #define GENL_op(op_name, op_num, handler, attr_list)
  160. #undef GENL_notification
  161. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  162. #undef GENL_struct
  163. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  164. case tag_number:
  165. static inline void ct_assert_unique_top_level_attributes(void)
  166. {
  167. switch (0) {
  168. #include GENL_MAGIC_INCLUDE_FILE
  169. case 0:
  170. ;
  171. }
  172. }
  173. #undef GENL_struct
  174. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  175. static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
  176. { \
  177. switch (0) { \
  178. s_fields \
  179. case 0: \
  180. ; \
  181. } \
  182. }
  183. #undef __field
  184. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  185. __is_signed) \
  186. case attr_nr:
  187. #undef __array
  188. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  189. __get, __put, __is_signed) \
  190. case attr_nr:
  191. #include GENL_MAGIC_INCLUDE_FILE
  192. /* }}}1
  193. * Magic: declare structs
  194. * struct <name> {
  195. * fields
  196. * };
  197. * {{{2
  198. */
  199. #undef GENL_struct
  200. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  201. struct s_name { s_fields };
  202. #undef __field
  203. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  204. __is_signed) \
  205. type name;
  206. #undef __array
  207. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  208. __get, __put, __is_signed) \
  209. type name[maxlen]; \
  210. __u32 name ## _len;
  211. #include GENL_MAGIC_INCLUDE_FILE
  212. #undef GENL_struct
  213. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  214. enum { \
  215. s_fields \
  216. };
  217. #undef __field
  218. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  219. is_signed) \
  220. F_ ## name ## _IS_SIGNED = is_signed,
  221. #undef __array
  222. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  223. __get, __put, is_signed) \
  224. F_ ## name ## _IS_SIGNED = is_signed,
  225. #include GENL_MAGIC_INCLUDE_FILE
  226. /* }}}1 */
  227. #endif /* GENL_MAGIC_STRUCT_H */
  228. /* vim: set foldmethod=marker nofoldenable : */