blob: eecd19b3700112b22268c7570b42e381aa3685f6 [file] [log] [blame]
Lars Ellenbergec2c35a2011-03-07 10:20:08 +01001#ifndef GENL_MAGIC_STRUCT_H
2#define GENL_MAGIC_STRUCT_H
3
4#ifndef GENL_MAGIC_FAMILY
5# error "you need to define GENL_MAGIC_FAMILY before inclusion"
6#endif
7
8#ifndef GENL_MAGIC_VERSION
9# error "you need to define GENL_MAGIC_VERSION before inclusion"
10#endif
11
12#ifndef GENL_MAGIC_INCLUDE_FILE
13# error "you need to define GENL_MAGIC_INCLUDE_FILE before inclusion"
14#endif
15
16#include <linux/genetlink.h>
17#include <linux/types.h>
18
19#define CONCAT__(a,b) a ## b
20#define CONCAT_(a,b) CONCAT__(a,b)
21
22extern int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void);
23extern void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void);
24
25/*
26 * Extension of genl attribute validation policies {{{2
27 */
28
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020029/*
30 * @DRBD_GENLA_F_MANDATORY: By default, netlink ignores attributes it does not
31 * know about. This flag can be set in nlattr->nla_type to indicate that this
32 * attribute must not be ignored.
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010033 *
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020034 * We check and remove this flag in drbd_nla_check_mandatory() before
35 * validating the attribute types and lengths via nla_parse_nested().
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010036 */
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020037#define DRBD_GENLA_F_MANDATORY (1 << 14)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010038
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020039/*
40 * Flags specific to drbd and not visible at the netlink layer, used in
41 * <struct>_from_attrs and <struct>_to_skb:
42 *
43 * @DRBD_F_REQUIRED: Attribute is required; a request without this attribute is
44 * invalid.
45 *
46 * @DRBD_F_SENSITIVE: Attribute includes sensitive information and must not be
47 * included in unpriviledged get requests or broadcasts.
48 *
49 * @DRBD_F_INVARIANT: Attribute is set when an object is initially created, but
50 * cannot subsequently be changed.
51 */
52#define DRBD_F_REQUIRED (1 << 0)
53#define DRBD_F_SENSITIVE (1 << 1)
54#define DRBD_F_INVARIANT (1 << 2)
Lars Ellenbergf3990022011-03-23 14:31:09 +010055
Andreas Gruenbacher5f935922011-05-19 17:39:28 +020056#define __nla_type(x) ((__u16)((x) & NLA_TYPE_MASK & ~DRBD_GENLA_F_MANDATORY))
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010057
58/* }}}1
59 * MAGIC
60 * multi-include macro expansion magic starts here
61 */
62
63/* MAGIC helpers {{{2 */
64
65/* possible field types */
66#define __flg_field(attr_nr, attr_flag, name) \
Andreas Gruenbachera5d8e1f2011-05-04 16:06:51 +020067 __field(attr_nr, attr_flag, name, NLA_U8, char, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020068 nla_get_u8, nla_put_u8, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010069#define __u8_field(attr_nr, attr_flag, name) \
70 __field(attr_nr, attr_flag, name, NLA_U8, unsigned char, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020071 nla_get_u8, nla_put_u8, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010072#define __u16_field(attr_nr, attr_flag, name) \
73 __field(attr_nr, attr_flag, name, NLA_U16, __u16, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020074 nla_get_u16, nla_put_u16, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010075#define __u32_field(attr_nr, attr_flag, name) \
76 __field(attr_nr, attr_flag, name, NLA_U32, __u32, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020077 nla_get_u32, nla_put_u32, false)
Lars Ellenberg563e4cf2011-05-04 10:33:52 +020078#define __s32_field(attr_nr, attr_flag, name) \
79 __field(attr_nr, attr_flag, name, NLA_U32, __s32, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020080 nla_get_u32, nla_put_u32, true)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010081#define __u64_field(attr_nr, attr_flag, name) \
82 __field(attr_nr, attr_flag, name, NLA_U64, __u64, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020083 nla_get_u64, nla_put_u64, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010084#define __str_field(attr_nr, attr_flag, name, maxlen) \
85 __array(attr_nr, attr_flag, name, NLA_NUL_STRING, char, maxlen, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020086 nla_strlcpy, nla_put, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010087#define __bin_field(attr_nr, attr_flag, name, maxlen) \
88 __array(attr_nr, attr_flag, name, NLA_BINARY, char, maxlen, \
Andreas Gruenbacher26ec9282012-07-11 20:36:03 +020089 nla_memcpy, nla_put, false)
Lars Ellenbergec2c35a2011-03-07 10:20:08 +010090
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +020091/* fields with default values */
92#define __flg_field_def(attr_nr, attr_flag, name, default) \
93 __flg_field(attr_nr, attr_flag, name)
94#define __u32_field_def(attr_nr, attr_flag, name, default) \
95 __u32_field(attr_nr, attr_flag, name)
Andreas Gruenbacher3a45abd2011-05-12 12:02:54 +020096#define __s32_field_def(attr_nr, attr_flag, name, default) \
97 __s32_field(attr_nr, attr_flag, name)
Andreas Gruenbacherb966b5d2011-05-03 14:56:09 +020098#define __str_field_def(attr_nr, attr_flag, name, maxlen) \
99 __str_field(attr_nr, attr_flag, name, maxlen)
100
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100101#define GENL_op_init(args...) args
102#define GENL_doit(handler) \
103 .doit = handler, \
104 .flags = GENL_ADMIN_PERM,
105#define GENL_dumpit(handler) \
106 .dumpit = handler, \
107 .flags = GENL_ADMIN_PERM,
108
109/* }}}1
110 * Magic: define the enum symbols for genl_ops
111 * Magic: define the enum symbols for top level attributes
112 * Magic: define the enum symbols for nested attributes
113 * {{{2
114 */
115
116#undef GENL_struct
117#define GENL_struct(tag_name, tag_number, s_name, s_fields)
118
119#undef GENL_mc_group
120#define GENL_mc_group(group)
121
122#undef GENL_notification
123#define GENL_notification(op_name, op_num, mcast_group, tla_list) \
124 op_name = op_num,
125
126#undef GENL_op
127#define GENL_op(op_name, op_num, handler, tla_list) \
128 op_name = op_num,
129
130enum {
131#include GENL_MAGIC_INCLUDE_FILE
132};
133
134#undef GENL_notification
135#define GENL_notification(op_name, op_num, mcast_group, tla_list)
136
137#undef GENL_op
138#define GENL_op(op_name, op_num, handler, attr_list)
139
140#undef GENL_struct
141#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
142 tag_name = tag_number,
143
144enum {
145#include GENL_MAGIC_INCLUDE_FILE
146};
147
148#undef GENL_struct
149#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
150enum { \
151 s_fields \
152};
153
154#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200155#define __field(attr_nr, attr_flag, name, nla_type, type, \
156 __get, __put, __is_signed) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200157 T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100158
159#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200160#define __array(attr_nr, attr_flag, name, nla_type, type, \
161 maxlen, __get, __put, __is_signed) \
Andreas Gruenbacher5f935922011-05-19 17:39:28 +0200162 T_ ## name = (__u16)(attr_nr | ((attr_flag) & DRBD_GENLA_F_MANDATORY)),
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100163
164#include GENL_MAGIC_INCLUDE_FILE
165
166/* }}}1
167 * Magic: compile time assert unique numbers for operations
168 * Magic: -"- unique numbers for top level attributes
169 * Magic: -"- unique numbers for nested attributes
170 * {{{2
171 */
172
173#undef GENL_struct
174#define GENL_struct(tag_name, tag_number, s_name, s_fields)
175
176#undef GENL_op
177#define GENL_op(op_name, op_num, handler, attr_list) \
178 case op_name:
179
180#undef GENL_notification
181#define GENL_notification(op_name, op_num, mcast_group, tla_list) \
182 case op_name:
183
184static inline void ct_assert_unique_operations(void)
185{
186 switch (0) {
187#include GENL_MAGIC_INCLUDE_FILE
188 ;
189 }
190}
191
192#undef GENL_op
193#define GENL_op(op_name, op_num, handler, attr_list)
194
195#undef GENL_notification
196#define GENL_notification(op_name, op_num, mcast_group, tla_list)
197
198#undef GENL_struct
199#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
200 case tag_number:
201
202static inline void ct_assert_unique_top_level_attributes(void)
203{
204 switch (0) {
205#include GENL_MAGIC_INCLUDE_FILE
206 ;
207 }
208}
209
210#undef GENL_struct
211#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
212static inline void ct_assert_unique_ ## s_name ## _attributes(void) \
213{ \
214 switch (0) { \
215 s_fields \
216 ; \
217 } \
218}
219
220#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200221#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
222 __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100223 case attr_nr:
224
225#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200226#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
227 __get, __put, __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100228 case attr_nr:
229
230#include GENL_MAGIC_INCLUDE_FILE
231
232/* }}}1
233 * Magic: declare structs
234 * struct <name> {
235 * fields
236 * };
237 * {{{2
238 */
239
240#undef GENL_struct
241#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
242struct s_name { s_fields };
243
244#undef __field
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200245#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
246 __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100247 type name;
248
249#undef __array
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200250#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
251 __get, __put, __is_signed) \
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100252 type name[maxlen]; \
253 __u32 name ## _len;
254
255#include GENL_MAGIC_INCLUDE_FILE
256
Andreas Gruenbacher509100e2011-05-17 13:29:46 +0200257#undef GENL_struct
258#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
259enum { \
260 s_fields \
261};
262
263#undef __field
264#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
265 is_signed) \
266 F_ ## name ## _IS_SIGNED = is_signed,
267
268#undef __array
269#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
270 __get, __put, is_signed) \
271 F_ ## name ## _IS_SIGNED = is_signed,
272
273#include GENL_MAGIC_INCLUDE_FILE
274
Lars Ellenbergec2c35a2011-03-07 10:20:08 +0100275/* }}}1 */
276#endif /* GENL_MAGIC_STRUCT_H */
277/* vim: set foldmethod=marker nofoldenable : */