1 #ifndef GENL_MAGIC_FUNC_H
2 #define GENL_MAGIC_FUNC_H
4 #include <linux/genl_magic_struct.h>
7 * Magic: declare tla policy {{{1
8 * Magic: declare nested policies
12 #define GENL_mc_group(group)
14 #undef GENL_notification
15 #define GENL_notification(op_name, op_num, mcast_group, tla_list)
18 #define GENL_op(op_name, op_num, handler, tla_list)
21 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
22 [tag_name] = { .type = NLA_NESTED },
24 static struct nla_policy
CONCAT_(GENL_MAGIC_FAMILY
, _tla_nl_policy
)[] = {
25 #include GENL_MAGIC_INCLUDE_FILE
29 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
30 static struct nla_policy s_name ## _nl_policy[] __read_mostly = \
34 #define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \
36 [attr_nr] = { .type = nla_type },
39 #define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen, \
40 __get, __put, __is_signed) \
41 [attr_nr] = { .type = nla_type, \
42 .len = maxlen - (nla_type == NLA_NUL_STRING) },
44 #include GENL_MAGIC_INCLUDE_FILE
48 #define pr_info(args...) fprintf(stderr, args);
52 #ifdef GENL_MAGIC_DEBUG
53 static void dprint_field(const char *dir
, int nla_type
,
54 const char *name
, void *valp
)
56 __u64 val
= valp
? *(__u32
*)valp
: 1;
58 case NLA_U8
: val
= (__u8
)val
;
59 case NLA_U16
: val
= (__u16
)val
;
60 case NLA_U32
: val
= (__u32
)val
;
61 pr_info("%s attr %s: %d 0x%08x\n", dir
,
62 name
, (int)val
, (unsigned)val
);
66 pr_info("%s attr %s: %lld 0x%08llx\n", dir
,
67 name
, (long long)val
, (unsigned long long)val
);
71 pr_info("%s attr %s: set\n", dir
, name
);
76 static void dprint_array(const char *dir
, int nla_type
,
77 const char *name
, const char *val
, unsigned len
)
81 if (len
&& val
[len
-1] == '\0')
83 pr_info("%s attr %s: [len:%u] '%s'\n", dir
, name
, len
, val
);
86 /* we can always show 4 byte,
87 * thats what nlattr are aligned to. */
88 pr_info("%s attr %s: [len:%u] %02x%02x%02x%02x ...\n",
89 dir
, name
, len
, val
[0], val
[1], val
[2], val
[3]);
93 #define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b);
95 /* Name is a member field name of the struct s.
96 * If s is NULL (only parsing, no copy requested in *_from_attrs()),
97 * nla is supposed to point to the attribute containing the information
98 * corresponding to that struct member. */
99 #define DPRINT_FIELD(dir, nla_type, name, s, nla) \
102 dprint_field(dir, nla_type, #name, &s->name); \
104 dprint_field(dir, nla_type, #name, \
105 (nla_type == NLA_FLAG) ? NULL \
109 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) \
112 dprint_array(dir, nla_type, #name, \
113 s->name, s->name ## _len); \
115 dprint_array(dir, nla_type, #name, \
116 nla_data(nla), nla_len(nla)); \
119 #define DPRINT_TLA(a, op, b) do {} while (0)
120 #define DPRINT_FIELD(dir, nla_type, name, s, nla) do {} while (0)
121 #define DPRINT_ARRAY(dir, nla_type, name, s, nla) do {} while (0)
125 * Magic: provide conversion functions {{{1
126 * populate struct from attribute table:
130 /* processing of generic netlink messages is serialized.
131 * use one static buffer for parsing of nested attributes */
132 static struct nlattr
*nested_attr_tb
[128];
135 /* Force a compilation error if condition is true */
136 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
137 /* Force a compilation error if condition is true, but also produce a
138 result (of value 0 and type size_t), so the expression can be used
139 e.g. in a structure initializer (or where-ever else comma expressions
140 aren't permitted). */
141 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
142 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
146 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
147 /* *_from_attrs functions are static, but potentially unused */ \
148 static int __ ## s_name ## _from_attrs(struct s_name *s, \
149 struct genl_info *info, bool exclude_invariants) \
151 const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \
152 struct nlattr *tla = info->attrs[tag_number]; \
153 struct nlattr **ntb = nested_attr_tb; \
154 struct nlattr *nla; \
156 BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \
159 DPRINT_TLA(#s_name, "<=-", #tag_name); \
160 err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \
166 } __attribute__((unused)) \
167 static int s_name ## _from_attrs(struct s_name *s, \
168 struct genl_info *info) \
170 return __ ## s_name ## _from_attrs(s, info, false); \
171 } __attribute__((unused)) \
172 static int s_name ## _from_attrs_for_change(struct s_name *s, \
173 struct genl_info *info) \
175 return __ ## s_name ## _from_attrs(s, info, true); \
176 } __attribute__((unused)) \
178 #define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \
179 nla = ntb[attr_nr]; \
181 if (exclude_invariants && ((attr_flag) & DRBD_F_INVARIANT)) { \
182 pr_info("<< must not change invariant attr: %s\n", #name); \
186 } else if (exclude_invariants && ((attr_flag) & DRBD_F_INVARIANT)) { \
187 /* attribute missing from payload, */ \
188 /* which was expected */ \
189 } else if ((attr_flag) & DRBD_F_REQUIRED) { \
190 pr_info("<< missing attr: %s\n", #name); \
195 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
197 __assign(attr_nr, attr_flag, name, nla_type, type, \
199 s->name = __get(nla); \
200 DPRINT_FIELD("<<", nla_type, name, s, nla))
202 /* validate_nla() already checked nla_len <= maxlen appropriately. */
204 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
205 __get, __put, __is_signed) \
206 __assign(attr_nr, attr_flag, name, nla_type, type, \
209 __get(s->name, nla, maxlen); \
210 DPRINT_ARRAY("<<", nla_type, name, s, nla))
212 #include GENL_MAGIC_INCLUDE_FILE
215 #define GENL_struct(tag_name, tag_number, s_name, s_fields)
218 * Magic: define op number to op name mapping {{{1
221 const char *CONCAT_(GENL_MAGIC_FAMILY
, _genl_cmd_to_str
)(__u8 cmd
)
225 #define GENL_op(op_name, op_num, handler, tla_list) \
226 case op_num: return #op_name;
227 #include GENL_MAGIC_INCLUDE_FILE
234 #include <linux/stringify.h>
236 * Magic: define genl_ops {{{1
241 #define GENL_op(op_name, op_num, handler, tla_list) \
245 .policy = CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy), \
248 #define ZZZ_genl_ops CONCAT_(GENL_MAGIC_FAMILY, _genl_ops)
249 static struct genl_ops ZZZ_genl_ops
[] __read_mostly
= {
250 #include GENL_MAGIC_INCLUDE_FILE
254 #define GENL_op(op_name, op_num, handler, tla_list)
257 * Define the genl_family, multicast groups, {{{1
258 * and provide register/unregister functions.
261 #define ZZZ_genl_family CONCAT_(GENL_MAGIC_FAMILY, _genl_family)
262 static struct genl_family ZZZ_genl_family __read_mostly
= {
263 .id
= GENL_ID_GENERATE
,
264 .name
= __stringify(GENL_MAGIC_FAMILY
),
265 .version
= GENL_MAGIC_VERSION
,
266 #ifdef GENL_MAGIC_FAMILY_HDRSZ
267 .hdrsize
= NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ
),
269 .maxattr
= ARRAY_SIZE(drbd_tla_nl_policy
)-1,
273 * Magic: define multicast groups
274 * Magic: define multicast group registration helper
277 #define GENL_mc_group(group) \
278 static struct genl_multicast_group \
279 CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group) __read_mostly = { \
282 static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \
283 struct sk_buff *skb, gfp_t flags) \
285 unsigned int group_id = \
286 CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group).id; \
289 return genlmsg_multicast(skb, 0, group_id, flags); \
292 #include GENL_MAGIC_INCLUDE_FILE
294 int CONCAT_(GENL_MAGIC_FAMILY
, _genl_register
)(void)
296 int err
= genl_register_family_with_ops(&ZZZ_genl_family
,
297 ZZZ_genl_ops
, ARRAY_SIZE(ZZZ_genl_ops
));
301 #define GENL_mc_group(group) \
302 err = genl_register_mc_group(&ZZZ_genl_family, \
303 &CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group)); \
307 pr_info("%s: mcg %s: %u\n", #group, \
308 __stringify(GENL_MAGIC_FAMILY), \
309 CONCAT_(GENL_MAGIC_FAMILY, _mcg_ ## group).id);
311 #include GENL_MAGIC_INCLUDE_FILE
314 #define GENL_mc_group(group)
317 genl_unregister_family(&ZZZ_genl_family
);
321 void CONCAT_(GENL_MAGIC_FAMILY
, _genl_unregister
)(void)
323 genl_unregister_family(&ZZZ_genl_family
);
327 * Magic: provide conversion functions {{{1
328 * populate skb from struct.
333 #define GENL_op(op_name, op_num, handler, tla_list)
336 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
337 static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \
338 const bool exclude_sensitive) \
340 struct nlattr *tla = nla_nest_start(skb, tag_number); \
342 goto nla_put_failure; \
343 DPRINT_TLA(#s_name, "-=>", #tag_name); \
345 nla_nest_end(skb, tla); \
350 nla_nest_cancel(skb, tla); \
353 static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \
356 return s_name ## _to_skb(skb, s, 0); \
358 static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \
361 return s_name ## _to_skb(skb, s, 1); \
366 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
368 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
369 DPRINT_FIELD(">>", nla_type, name, s, NULL); \
370 if (__put(skb, attr_nr, s->name)) \
371 goto nla_put_failure; \
375 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
376 __get, __put, __is_signed) \
377 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
378 DPRINT_ARRAY(">>",nla_type, name, s, NULL); \
379 if (__put(skb, attr_nr, min_t(int, maxlen, \
380 s->name ## _len + (nla_type == NLA_NUL_STRING)),\
382 goto nla_put_failure; \
385 #include GENL_MAGIC_INCLUDE_FILE
388 /* Functions for initializing structs to default values. */
391 #define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
394 #define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
395 __get, __put, __is_signed)
396 #undef __u32_field_def
397 #define __u32_field_def(attr_nr, attr_flag, name, default) \
399 #undef __s32_field_def
400 #define __s32_field_def(attr_nr, attr_flag, name, default) \
402 #undef __flg_field_def
403 #define __flg_field_def(attr_nr, attr_flag, name, default) \
405 #undef __str_field_def
406 #define __str_field_def(attr_nr, attr_flag, name, maxlen) \
407 memset(x->name, 0, sizeof(x->name)); \
410 #define GENL_struct(tag_name, tag_number, s_name, s_fields) \
411 static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \
412 static void set_ ## s_name ## _defaults(struct s_name *x) { \
416 #include GENL_MAGIC_INCLUDE_FILE
418 #endif /* __KERNEL__ */
421 #endif /* GENL_MAGIC_FUNC_H */
422 /* vim: set foldmethod=marker foldlevel=1 nofoldenable : */