genl_magic_struct.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. static inline int nla_put_u64_0pad(struct sk_buff *skb, int attrtype, u64 value)
  53. {
  54. return nla_put_64bit(skb, attrtype, sizeof(u64), &value, 0);
  55. }
  56. /* possible field types */
  57. #define __flg_field(attr_nr, attr_flag, name) \
  58. __field(attr_nr, attr_flag, name, NLA_U8, char, \
  59. nla_get_u8, nla_put_u8, false)
  60. #define __u8_field(attr_nr, attr_flag, name) \
  61. __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
  62. nla_get_u8, nla_put_u8, false)
  63. #define __u16_field(attr_nr, attr_flag, name) \
  64. __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
  65. nla_get_u16, nla_put_u16, false)
  66. #define __u32_field(attr_nr, attr_flag, name) \
  67. __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
  68. nla_get_u32, nla_put_u32, false)
  69. #define __s32_field(attr_nr, attr_flag, name) \
  70. __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
  71. nla_get_u32, nla_put_u32, true)
  72. #define __u64_field(attr_nr, attr_flag, name) \
  73. __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
  74. nla_get_u64, nla_put_u64_0pad, false)
  75. #define __str_field(attr_nr, attr_flag, name, maxlen) \
  76. __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
  77. nla_strlcpy, nla_put, false)
  78. #define __bin_field(attr_nr, attr_flag, name, maxlen) \
  79. __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
  80. nla_memcpy, nla_put, false)
  81. /* fields with default values */
  82. #define __flg_field_def(attr_nr, attr_flag, name, default) \
  83. __flg_field(attr_nr, attr_flag, name)
  84. #define __u32_field_def(attr_nr, attr_flag, name, default) \
  85. __u32_field(attr_nr, attr_flag, name)
  86. #define __s32_field_def(attr_nr, attr_flag, name, default) \
  87. __s32_field(attr_nr, attr_flag, name)
  88. #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
  89. __str_field(attr_nr, attr_flag, name, maxlen)
  90. #define GENL_op_init(args...) args
  91. #define GENL_doit(handler) \
  92. .doit = handler, \
  93. .flags = GENL_ADMIN_PERM,
  94. #define GENL_dumpit(handler) \
  95. .dumpit = handler, \
  96. .flags = GENL_ADMIN_PERM,
  97. /* }}}1
  98. * Magic: define the enum symbols for genl_ops
  99. * Magic: define the enum symbols for top level attributes
  100. * Magic: define the enum symbols for nested attributes
  101. * {{{2
  102. */
  103. #undef GENL_struct
  104. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  105. #undef GENL_mc_group
  106. #define GENL_mc_group(group)
  107. #undef GENL_notification
  108. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  109. op_name = op_num,
  110. #undef GENL_op
  111. #define GENL_op(op_name, op_num, handler, tla_list) \
  112. op_name = op_num,
  113. enum {
  114. #include GENL_MAGIC_INCLUDE_FILE
  115. };
  116. #undef GENL_notification
  117. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  118. #undef GENL_op
  119. #define GENL_op(op_name, op_num, handler, attr_list)
  120. #undef GENL_struct
  121. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  122. tag_name = tag_number,
  123. enum {
  124. #include GENL_MAGIC_INCLUDE_FILE
  125. };
  126. #undef GENL_struct
  127. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  128. enum { \
  129. s_fields \
  130. };
  131. #undef __field
  132. #define __field(attr_nr, attr_flag, name, nla_type, type, \
  133. __get, __put, __is_signed) \
  134. T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
  135. #undef __array
  136. #define __array(attr_nr, attr_flag, name, nla_type, type, \
  137. maxlen, __get, __put, __is_signed) \
  138. T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
  139. #include GENL_MAGIC_INCLUDE_FILE
  140. /* }}}1
  141. * Magic: compile time assert unique numbers for operations
  142. * Magic: -"- unique numbers for top level attributes
  143. * Magic: -"- unique numbers for nested attributes
  144. * {{{2
  145. */
  146. #undef GENL_struct
  147. #define GENL_struct(tag_name, tag_number, s_name, s_fields)
  148. #undef GENL_op
  149. #define GENL_op(op_name, op_num, handler, attr_list) \
  150. case op_name:
  151. #undef GENL_notification
  152. #define GENL_notification(op_name, op_num, mcast_group, tla_list) \
  153. case op_name:
  154. static inline void ct_assert_unique_operations(void)
  155. {
  156. switch (0) {
  157. #include GENL_MAGIC_INCLUDE_FILE
  158. ;
  159. }
  160. }
  161. #undef GENL_op
  162. #define GENL_op(op_name, op_num, handler, attr_list)
  163. #undef GENL_notification
  164. #define GENL_notification(op_name, op_num, mcast_group, tla_list)
  165. #undef GENL_struct
  166. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  167. case tag_number:
  168. static inline void ct_assert_unique_top_level_attributes(void)
  169. {
  170. switch (0) {
  171. #include GENL_MAGIC_INCLUDE_FILE
  172. ;
  173. }
  174. }
  175. #undef GENL_struct
  176. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  177. static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
  178. { \
  179. switch (0) { \
  180. s_fields \
  181. ; \
  182. } \
  183. }
  184. #undef __field
  185. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  186. __is_signed) \
  187. case attr_nr:
  188. #undef __array
  189. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  190. __get, __put, __is_signed) \
  191. case attr_nr:
  192. #include GENL_MAGIC_INCLUDE_FILE
  193. /* }}}1
  194. * Magic: declare structs
  195. * struct <name> {
  196. * fields
  197. * };
  198. * {{{2
  199. */
  200. #undef GENL_struct
  201. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  202. struct s_name { s_fields };
  203. #undef __field
  204. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  205. __is_signed) \
  206. type name;
  207. #undef __array
  208. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  209. __get, __put, __is_signed) \
  210. type name[maxlen]; \
  211. __u32 name ## _len;
  212. #include GENL_MAGIC_INCLUDE_FILE
  213. #undef GENL_struct
  214. #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
  215. enum { \
  216. s_fields \
  217. };
  218. #undef __field
  219. #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
  220. is_signed) \
  221. F_ ## name ## _IS_SIGNED = is_signed,
  222. #undef __array
  223. #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
  224. __get, __put, is_signed) \
  225. F_ ## name ## _IS_SIGNED = is_signed,
  226. #include GENL_MAGIC_INCLUDE_FILE
  227. /* }}}1 */
  228. #endif /* GENL_MAGIC_STRUCT_H */
  229. /* vim: set foldmethod=marker nofoldenable : */