]> git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/src/common/ctl.h
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / common / ctl.h
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright 2016-2020, Intel Corporation */
3
4 /*
5 * ctl.h -- internal declaration of statistics and control related structures
6 */
7
8 #ifndef PMDK_CTL_H
9 #define PMDK_CTL_H 1
10
11 #include "queue.h"
12 #include "errno.h"
13 #include "out.h"
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 struct ctl;
20
21 struct ctl_index {
22 const char *name;
23 long value;
24 PMDK_SLIST_ENTRY(ctl_index) entry;
25 };
26
27 PMDK_SLIST_HEAD(ctl_indexes, ctl_index);
28
29 enum ctl_query_source {
30 CTL_UNKNOWN_QUERY_SOURCE,
31 /* query executed directly from the program */
32 CTL_QUERY_PROGRAMMATIC,
33 /* query executed from the config file */
34 CTL_QUERY_CONFIG_INPUT,
35
36 MAX_CTL_QUERY_SOURCE
37 };
38
39 enum ctl_query_type {
40 CTL_QUERY_READ,
41 CTL_QUERY_WRITE,
42 CTL_QUERY_RUNNABLE,
43
44 MAX_CTL_QUERY_TYPE
45 };
46
47 typedef int (*node_callback)(void *ctx, enum ctl_query_source type,
48 void *arg, struct ctl_indexes *indexes);
49
50 enum ctl_node_type {
51 CTL_NODE_UNKNOWN,
52 CTL_NODE_NAMED,
53 CTL_NODE_LEAF,
54 CTL_NODE_INDEXED,
55
56 MAX_CTL_NODE
57 };
58
59 typedef int (*ctl_arg_parser)(const void *arg, void *dest, size_t dest_size);
60
61 struct ctl_argument_parser {
62 size_t dest_offset; /* offset of the field inside of the argument */
63 size_t dest_size; /* size of the field inside of the argument */
64 ctl_arg_parser parser;
65 };
66
67 struct ctl_argument {
68 size_t dest_size; /* sizeof the entire argument */
69 struct ctl_argument_parser parsers[]; /* array of 'fields' in arg */
70 };
71
72 #define sizeof_member(t, m) sizeof(((t *)0)->m)
73
74 #define CTL_ARG_PARSER(t, p)\
75 {0, sizeof(t), p}
76
77 #define CTL_ARG_PARSER_STRUCT(t, m, p)\
78 {offsetof(t, m), sizeof_member(t, m), p}
79
80 #define CTL_ARG_PARSER_END {0, 0, NULL}
81
82 /*
83 * CTL Tree node structure, do not use directly. All the necessary functionality
84 * is provided by the included macros.
85 */
86 struct ctl_node {
87 const char *name;
88 enum ctl_node_type type;
89
90 node_callback cb[MAX_CTL_QUERY_TYPE];
91 const struct ctl_argument *arg;
92
93 const struct ctl_node *children;
94 };
95
96 struct ctl *ctl_new(void);
97 void ctl_delete(struct ctl *stats);
98
99 int ctl_load_config_from_string(struct ctl *ctl, void *ctx,
100 const char *cfg_string);
101 int ctl_load_config_from_file(struct ctl *ctl, void *ctx,
102 const char *cfg_file);
103
104 /* Use through CTL_REGISTER_MODULE, never directly */
105 void ctl_register_module_node(struct ctl *c,
106 const char *name, struct ctl_node *n);
107
108 int ctl_arg_boolean(const void *arg, void *dest, size_t dest_size);
109 #define CTL_ARG_BOOLEAN {sizeof(int),\
110 {{0, sizeof(int), ctl_arg_boolean},\
111 CTL_ARG_PARSER_END}};
112
113 int ctl_arg_integer(const void *arg, void *dest, size_t dest_size);
114 #define CTL_ARG_INT {sizeof(int),\
115 {{0, sizeof(int), ctl_arg_integer},\
116 CTL_ARG_PARSER_END}};
117
118 #define CTL_ARG_LONG_LONG {sizeof(long long),\
119 {{0, sizeof(long long), ctl_arg_integer},\
120 CTL_ARG_PARSER_END}};
121
122 int ctl_arg_string(const void *arg, void *dest, size_t dest_size);
123 #define CTL_ARG_STRING(len) {len,\
124 {{0, len, ctl_arg_string},\
125 CTL_ARG_PARSER_END}};
126
127 #define CTL_STR(name) #name
128
129 #define CTL_NODE_END {NULL, CTL_NODE_UNKNOWN, {NULL, NULL, NULL}, NULL, NULL}
130
131 #define CTL_NODE(name, ...)\
132 ctl_node_##__VA_ARGS__##_##name
133
134 int ctl_query(struct ctl *ctl, void *ctx, enum ctl_query_source source,
135 const char *name, enum ctl_query_type type, void *arg);
136
137 /* Declaration of a new child node */
138 #define CTL_CHILD(name, ...)\
139 {CTL_STR(name), CTL_NODE_NAMED, {NULL, NULL, NULL}, NULL,\
140 (struct ctl_node *)CTL_NODE(name, __VA_ARGS__)}
141
142 /* Declaration of a new indexed node */
143 #define CTL_INDEXED(name, ...)\
144 {CTL_STR(name), CTL_NODE_INDEXED, {NULL, NULL, NULL}, NULL,\
145 (struct ctl_node *)CTL_NODE(name, __VA_ARGS__)}
146
147 #define CTL_READ_HANDLER(name, ...)\
148 ctl_##__VA_ARGS__##_##name##_read
149
150 #define CTL_WRITE_HANDLER(name, ...)\
151 ctl_##__VA_ARGS__##_##name##_write
152
153 #define CTL_RUNNABLE_HANDLER(name, ...)\
154 ctl_##__VA_ARGS__##_##name##_runnable
155
156 #define CTL_ARG(name)\
157 ctl_arg_##name
158
159 /*
160 * Declaration of a new read-only leaf. If used the corresponding read function
161 * must be declared by CTL_READ_HANDLER macro.
162 */
163 #define CTL_LEAF_RO(name, ...)\
164 {CTL_STR(name), CTL_NODE_LEAF, \
165 {CTL_READ_HANDLER(name, __VA_ARGS__), NULL, NULL}, NULL, NULL}
166
167 /*
168 * Declaration of a new write-only leaf. If used the corresponding write
169 * function must be declared by CTL_WRITE_HANDLER macro.
170 */
171 #define CTL_LEAF_WO(name, ...)\
172 {CTL_STR(name), CTL_NODE_LEAF, \
173 {NULL, CTL_WRITE_HANDLER(name, __VA_ARGS__), NULL},\
174 &CTL_ARG(name), NULL}
175
176 /*
177 * Declaration of a new runnable leaf. If used the corresponding run
178 * function must be declared by CTL_RUNNABLE_HANDLER macro.
179 */
180 #define CTL_LEAF_RUNNABLE(name, ...)\
181 {CTL_STR(name), CTL_NODE_LEAF, \
182 {NULL, NULL, CTL_RUNNABLE_HANDLER(name, __VA_ARGS__)},\
183 NULL, NULL}
184
185 /*
186 * Declaration of a new read-write leaf. If used both read and write function
187 * must be declared by CTL_READ_HANDLER and CTL_WRITE_HANDLER macros.
188 */
189 #define CTL_LEAF_RW(name)\
190 {CTL_STR(name), CTL_NODE_LEAF,\
191 {CTL_READ_HANDLER(name), CTL_WRITE_HANDLER(name), NULL},\
192 &CTL_ARG(name), NULL}
193
194 #define CTL_REGISTER_MODULE(_ctl, name)\
195 ctl_register_module_node((_ctl), CTL_STR(name),\
196 (struct ctl_node *)CTL_NODE(name))
197
198 #ifdef __cplusplus
199 }
200 #endif
201
202 #endif