]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/genl_magic_func.h
cpufreq: Rename cpufreq_can_do_remote_dvfs()
[mirror_ubuntu-bionic-kernel.git] / include / linux / genl_magic_func.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
ec2c35ac
LE
2#ifndef GENL_MAGIC_FUNC_H
3#define GENL_MAGIC_FUNC_H
4
5#include <linux/genl_magic_struct.h>
6
ec2c35ac
LE
7/*
8 * Magic: declare tla policy {{{1
9 * Magic: declare nested policies
10 * {{{2
11 */
12#undef GENL_mc_group
13#define GENL_mc_group(group)
14
15#undef GENL_notification
16#define GENL_notification(op_name, op_num, mcast_group, tla_list)
17
18#undef GENL_op
19#define GENL_op(op_name, op_num, handler, tla_list)
20
21#undef GENL_struct
22#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
23 [tag_name] = { .type = NLA_NESTED },
24
25static struct nla_policy CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy)[] = {
26#include GENL_MAGIC_INCLUDE_FILE
27};
28
29#undef GENL_struct
30#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
31static struct nla_policy s_name ## _nl_policy[] __read_mostly = \
32{ s_fields };
33
34#undef __field
509100e6
AG
35#define __field(attr_nr, attr_flag, name, nla_type, _type, __get, \
36 __put, __is_signed) \
5f935920 37 [attr_nr] = { .type = nla_type },
ec2c35ac
LE
38
39#undef __array
40#define __array(attr_nr, attr_flag, name, nla_type, _type, maxlen, \
509100e6 41 __get, __put, __is_signed) \
5f935920
AG
42 [attr_nr] = { .type = nla_type, \
43 .len = maxlen - (nla_type == NLA_NUL_STRING) },
ec2c35ac
LE
44
45#include GENL_MAGIC_INCLUDE_FILE
46
47#ifndef __KERNEL__
48#ifndef pr_info
49#define pr_info(args...) fprintf(stderr, args);
50#endif
51#endif
52
3b98c0c2 53#ifdef GENL_MAGIC_DEBUG
ec2c35ac
LE
54static void dprint_field(const char *dir, int nla_type,
55 const char *name, void *valp)
56{
57 __u64 val = valp ? *(__u32 *)valp : 1;
58 switch (nla_type) {
59 case NLA_U8: val = (__u8)val;
60 case NLA_U16: val = (__u16)val;
61 case NLA_U32: val = (__u32)val;
62 pr_info("%s attr %s: %d 0x%08x\n", dir,
63 name, (int)val, (unsigned)val);
64 break;
65 case NLA_U64:
66 val = *(__u64*)valp;
67 pr_info("%s attr %s: %lld 0x%08llx\n", dir,
68 name, (long long)val, (unsigned long long)val);
69 break;
70 case NLA_FLAG:
71 if (val)
72 pr_info("%s attr %s: set\n", dir, name);
73 break;
74 }
75}
76
77static void dprint_array(const char *dir, int nla_type,
78 const char *name, const char *val, unsigned len)
79{
80 switch (nla_type) {
81 case NLA_NUL_STRING:
82 if (len && val[len-1] == '\0')
83 len--;
84 pr_info("%s attr %s: [len:%u] '%s'\n", dir, name, len, val);
85 break;
86 default:
87 /* we can always show 4 byte,
88 * thats what nlattr are aligned to. */
89 pr_info("%s attr %s: [len:%u] %02x%02x%02x%02x ...\n",
90 dir, name, len, val[0], val[1], val[2], val[3]);
91 }
92}
93
94#define DPRINT_TLA(a, op, b) pr_info("%s %s %s\n", a, op, b);
95
96/* Name is a member field name of the struct s.
97 * If s is NULL (only parsing, no copy requested in *_from_attrs()),
98 * nla is supposed to point to the attribute containing the information
99 * corresponding to that struct member. */
100#define DPRINT_FIELD(dir, nla_type, name, s, nla) \
101 do { \
102 if (s) \
103 dprint_field(dir, nla_type, #name, &s->name); \
104 else if (nla) \
105 dprint_field(dir, nla_type, #name, \
106 (nla_type == NLA_FLAG) ? NULL \
107 : nla_data(nla)); \
108 } while (0)
109
110#define DPRINT_ARRAY(dir, nla_type, name, s, nla) \
111 do { \
112 if (s) \
113 dprint_array(dir, nla_type, #name, \
114 s->name, s->name ## _len); \
115 else if (nla) \
116 dprint_array(dir, nla_type, #name, \
117 nla_data(nla), nla_len(nla)); \
118 } while (0)
119#else
120#define DPRINT_TLA(a, op, b) do {} while (0)
121#define DPRINT_FIELD(dir, nla_type, name, s, nla) do {} while (0)
122#define DPRINT_ARRAY(dir, nla_type, name, s, nla) do {} while (0)
123#endif
124
125/*
126 * Magic: provide conversion functions {{{1
127 * populate struct from attribute table:
128 * {{{2
129 */
130
131/* processing of generic netlink messages is serialized.
132 * use one static buffer for parsing of nested attributes */
133static struct nlattr *nested_attr_tb[128];
134
135#ifndef BUILD_BUG_ON
136/* Force a compilation error if condition is true */
137#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
138/* Force a compilation error if condition is true, but also produce a
139 result (of value 0 and type size_t), so the expression can be used
140 e.g. in a structure initializer (or where-ever else comma expressions
141 aren't permitted). */
142#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
143#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
144#endif
145
146#undef GENL_struct
147#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
f399002e
LE
148/* *_from_attrs functions are static, but potentially unused */ \
149static int __ ## s_name ## _from_attrs(struct s_name *s, \
150 struct genl_info *info, bool exclude_invariants) \
ec2c35ac
LE
151{ \
152 const int maxtype = ARRAY_SIZE(s_name ## _nl_policy)-1; \
f399002e 153 struct nlattr *tla = info->attrs[tag_number]; \
ec2c35ac
LE
154 struct nlattr **ntb = nested_attr_tb; \
155 struct nlattr *nla; \
156 int err; \
157 BUILD_BUG_ON(ARRAY_SIZE(s_name ## _nl_policy) > ARRAY_SIZE(nested_attr_tb)); \
158 if (!tla) \
159 return -ENOMSG; \
160 DPRINT_TLA(#s_name, "<=-", #tag_name); \
5f935920 161 err = drbd_nla_parse_nested(ntb, maxtype, tla, s_name ## _nl_policy); \
ec2c35ac
LE
162 if (err) \
163 return err; \
ec2c35ac
LE
164 \
165 s_fields \
166 return 0; \
f399002e
LE
167} __attribute__((unused)) \
168static int s_name ## _from_attrs(struct s_name *s, \
169 struct genl_info *info) \
170{ \
171 return __ ## s_name ## _from_attrs(s, info, false); \
172} __attribute__((unused)) \
173static int s_name ## _from_attrs_for_change(struct s_name *s, \
174 struct genl_info *info) \
175{ \
176 return __ ## s_name ## _from_attrs(s, info, true); \
177} __attribute__((unused)) \
ec2c35ac 178
f399002e 179#define __assign(attr_nr, attr_flag, name, nla_type, type, assignment...) \
5f935920 180 nla = ntb[attr_nr]; \
ec2c35ac 181 if (nla) { \
db404b13 182 if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
f399002e
LE
183 pr_info("<< must not change invariant attr: %s\n", #name); \
184 return -EEXIST; \
185 } \
186 assignment; \
db404b13 187 } else if (exclude_invariants && !!((attr_flag) & DRBD_F_INVARIANT)) { \
f399002e
LE
188 /* attribute missing from payload, */ \
189 /* which was expected */ \
5f935920 190 } else if ((attr_flag) & DRBD_F_REQUIRED) { \
ec2c35ac
LE
191 pr_info("<< missing attr: %s\n", #name); \
192 return -ENOMSG; \
193 }
194
f399002e 195#undef __field
509100e6
AG
196#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
197 __is_signed) \
f399002e
LE
198 __assign(attr_nr, attr_flag, name, nla_type, type, \
199 if (s) \
200 s->name = __get(nla); \
201 DPRINT_FIELD("<<", nla_type, name, s, nla))
202
ec2c35ac
LE
203/* validate_nla() already checked nla_len <= maxlen appropriately. */
204#undef __array
509100e6
AG
205#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
206 __get, __put, __is_signed) \
f399002e 207 __assign(attr_nr, attr_flag, name, nla_type, type, \
ec2c35ac
LE
208 if (s) \
209 s->name ## _len = \
210 __get(s->name, nla, maxlen); \
f399002e 211 DPRINT_ARRAY("<<", nla_type, name, s, nla))
ec2c35ac
LE
212
213#include GENL_MAGIC_INCLUDE_FILE
214
215#undef GENL_struct
216#define GENL_struct(tag_name, tag_number, s_name, s_fields)
217
218/*
219 * Magic: define op number to op name mapping {{{1
220 * {{{2
221 */
222const char *CONCAT_(GENL_MAGIC_FAMILY, _genl_cmd_to_str)(__u8 cmd)
223{
224 switch (cmd) {
225#undef GENL_op
226#define GENL_op(op_name, op_num, handler, tla_list) \
227 case op_num: return #op_name;
228#include GENL_MAGIC_INCLUDE_FILE
229 default:
230 return "unknown";
231 }
232}
233
234#ifdef __KERNEL__
235#include <linux/stringify.h>
236/*
237 * Magic: define genl_ops {{{1
238 * {{{2
239 */
240
241#undef GENL_op
242#define GENL_op(op_name, op_num, handler, tla_list) \
243{ \
244 handler \
245 .cmd = op_name, \
246 .policy = CONCAT_(GENL_MAGIC_FAMILY, _tla_nl_policy), \
247},
248
249#define ZZZ_genl_ops CONCAT_(GENL_MAGIC_FAMILY, _genl_ops)
250static struct genl_ops ZZZ_genl_ops[] __read_mostly = {
251#include GENL_MAGIC_INCLUDE_FILE
252};
253
254#undef GENL_op
255#define GENL_op(op_name, op_num, handler, tla_list)
256
257/*
258 * Define the genl_family, multicast groups, {{{1
259 * and provide register/unregister functions.
260 * {{{2
261 */
262#define ZZZ_genl_family CONCAT_(GENL_MAGIC_FAMILY, _genl_family)
489111e5 263static struct genl_family ZZZ_genl_family;
ec2c35ac
LE
264/*
265 * Magic: define multicast groups
266 * Magic: define multicast group registration helper
267 */
2a94fe48
JB
268#define ZZZ_genl_mcgrps CONCAT_(GENL_MAGIC_FAMILY, _genl_mcgrps)
269static const struct genl_multicast_group ZZZ_genl_mcgrps[] = {
270#undef GENL_mc_group
271#define GENL_mc_group(group) { .name = #group, },
272#include GENL_MAGIC_INCLUDE_FILE
273};
274
275enum CONCAT_(GENL_MAGIC_FAMILY, group_ids) {
276#undef GENL_mc_group
277#define GENL_mc_group(group) CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group),
278#include GENL_MAGIC_INCLUDE_FILE
279};
280
ec2c35ac
LE
281#undef GENL_mc_group
282#define GENL_mc_group(group) \
ec2c35ac
LE
283static int CONCAT_(GENL_MAGIC_FAMILY, _genl_multicast_ ## group)( \
284 struct sk_buff *skb, gfp_t flags) \
285{ \
286 unsigned int group_id = \
2a94fe48 287 CONCAT_(GENL_MAGIC_FAMILY, _group_ ## group); \
68eb5503
JB
288 return genlmsg_multicast(&ZZZ_genl_family, skb, 0, \
289 group_id, flags); \
ec2c35ac
LE
290}
291
292#include GENL_MAGIC_INCLUDE_FILE
293
ec2c35ac
LE
294#undef GENL_mc_group
295#define GENL_mc_group(group)
2a94fe48 296
56989f6d 297static struct genl_family ZZZ_genl_family __ro_after_init = {
489111e5
JB
298 .name = __stringify(GENL_MAGIC_FAMILY),
299 .version = GENL_MAGIC_VERSION,
300#ifdef GENL_MAGIC_FAMILY_HDRSZ
301 .hdrsize = NLA_ALIGN(GENL_MAGIC_FAMILY_HDRSZ),
302#endif
303 .maxattr = ARRAY_SIZE(drbd_tla_nl_policy)-1,
304 .ops = ZZZ_genl_ops,
305 .n_ops = ARRAY_SIZE(ZZZ_genl_ops),
306 .mcgrps = ZZZ_genl_mcgrps,
307 .n_mcgrps = ARRAY_SIZE(ZZZ_genl_mcgrps),
308 .module = THIS_MODULE,
309};
310
2a94fe48
JB
311int CONCAT_(GENL_MAGIC_FAMILY, _genl_register)(void)
312{
489111e5 313 return genl_register_family(&ZZZ_genl_family);
ec2c35ac
LE
314}
315
316void CONCAT_(GENL_MAGIC_FAMILY, _genl_unregister)(void)
317{
318 genl_unregister_family(&ZZZ_genl_family);
319}
320
321/*
322 * Magic: provide conversion functions {{{1
323 * populate skb from struct.
324 * {{{2
325 */
326
327#undef GENL_op
328#define GENL_op(op_name, op_num, handler, tla_list)
329
330#undef GENL_struct
331#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
332static int s_name ## _to_skb(struct sk_buff *skb, struct s_name *s, \
333 const bool exclude_sensitive) \
334{ \
335 struct nlattr *tla = nla_nest_start(skb, tag_number); \
336 if (!tla) \
337 goto nla_put_failure; \
338 DPRINT_TLA(#s_name, "-=>", #tag_name); \
339 s_fields \
340 nla_nest_end(skb, tla); \
341 return 0; \
342 \
343nla_put_failure: \
344 if (tla) \
345 nla_nest_cancel(skb, tla); \
346 return -EMSGSIZE; \
347} \
348static inline int s_name ## _to_priv_skb(struct sk_buff *skb, \
349 struct s_name *s) \
350{ \
351 return s_name ## _to_skb(skb, s, 0); \
352} \
353static inline int s_name ## _to_unpriv_skb(struct sk_buff *skb, \
354 struct s_name *s) \
355{ \
356 return s_name ## _to_skb(skb, s, 1); \
357}
358
359
360#undef __field
509100e6
AG
361#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
362 __is_signed) \
5f935920 363 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
ec2c35ac 364 DPRINT_FIELD(">>", nla_type, name, s, NULL); \
26ec9287
AG
365 if (__put(skb, attr_nr, s->name)) \
366 goto nla_put_failure; \
ec2c35ac
LE
367 }
368
369#undef __array
509100e6
AG
370#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
371 __get, __put, __is_signed) \
5f935920 372 if (!exclude_sensitive || !((attr_flag) & DRBD_F_SENSITIVE)) { \
ec2c35ac 373 DPRINT_ARRAY(">>",nla_type, name, s, NULL); \
26ec9287 374 if (__put(skb, attr_nr, min_t(int, maxlen, \
ec2c35ac 375 s->name ## _len + (nla_type == NLA_NUL_STRING)),\
26ec9287
AG
376 s->name)) \
377 goto nla_put_failure; \
ec2c35ac
LE
378 }
379
380#include GENL_MAGIC_INCLUDE_FILE
381
b966b5dd
AG
382
383/* Functions for initializing structs to default values. */
384
385#undef __field
509100e6
AG
386#define __field(attr_nr, attr_flag, name, nla_type, type, __get, __put, \
387 __is_signed)
b966b5dd 388#undef __array
509100e6
AG
389#define __array(attr_nr, attr_flag, name, nla_type, type, maxlen, \
390 __get, __put, __is_signed)
b966b5dd
AG
391#undef __u32_field_def
392#define __u32_field_def(attr_nr, attr_flag, name, default) \
393 x->name = default;
3a45abd5
AG
394#undef __s32_field_def
395#define __s32_field_def(attr_nr, attr_flag, name, default) \
396 x->name = default;
b966b5dd
AG
397#undef __flg_field_def
398#define __flg_field_def(attr_nr, attr_flag, name, default) \
399 x->name = default;
400#undef __str_field_def
401#define __str_field_def(attr_nr, attr_flag, name, maxlen) \
402 memset(x->name, 0, sizeof(x->name)); \
403 x->name ## _len = 0;
404#undef GENL_struct
405#define GENL_struct(tag_name, tag_number, s_name, s_fields) \
406static void set_ ## s_name ## _defaults(struct s_name *x) __attribute__((unused)); \
407static void set_ ## s_name ## _defaults(struct s_name *x) { \
408s_fields \
409}
410
411#include GENL_MAGIC_INCLUDE_FILE
412
ec2c35ac
LE
413#endif /* __KERNEL__ */
414
415/* }}}1 */
416#endif /* GENL_MAGIC_FUNC_H */
417/* vim: set foldmethod=marker foldlevel=1 nofoldenable : */