-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.h
290 lines (217 loc) · 6.55 KB
/
misc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Configuration tool for RNBD driver and RTRS library.
*
* Copyright (c) 2019 1&1 IONOS SE. All rights reserved.
* Authors: Danil Kipnis <danil.kipnis@cloud.ionos.com>
* Lutz Pogrell <lutz.pogrell@cloud.ionos.com>
*/
#ifndef __H_MISC
#define __H_MISC
#include <ctype.h> /* for isspace() */
#include <dirent.h> /* for opendir() */
#include <stddef.h>
#define ARRSIZE(x) (sizeof(x) / sizeof(*x))
#define MAX_PATHS_PER_SESSION 32
#define ERR(trm, fmt, ...) \
do { \
if (trm) \
fprintf(stderr, "%s%s", colors[CRED], colors[CBLD]); \
fprintf(stderr, "error: "); \
if (trm) \
fprintf(stderr, "%s", colors[CNRM]); \
fprintf(stderr, fmt, ##__VA_ARGS__); \
} while (0)
struct bit_str {
const char *str;
const char bits;
const char *descr;
};
extern const struct bit_str bits[];
enum rnbd_size_state {
size_not_set = 0,
size_number,
size_unit
};
struct path {
const char *provided;
const char *src;
const char *dst;
};
struct port_desc {
char hca[NAME_MAX];
char port[NAME_MAX];
char gid[NAME_MAX];
};
struct rnbd_ctx {
const char *pname;
const char *name;
bool sysfs_avail;
uint64_t size_sect;
enum rnbd_size_state size_state;
short sign;
enum fmt_type fmt;
bool fmt_set;
unsigned int lstmode;
bool lstmode_set;
unsigned int rnbdmode;
bool rnbdmode_set;
bool pname_with_mode;
const char *access_mode;
bool access_mode_set;
const char *help_arg;
bool help_arg_set;
struct port_desc port_desc_arg;
bool port_desc_set;
struct table_column *clms_devices_clt[CLM_MAX_CNT];
struct table_column *clms_devices_srv[CLM_MAX_CNT];
struct table_column *clms_sessions_clt[CLM_MAX_CNT];
struct table_column *clms_sessions_srv[CLM_MAX_CNT];
struct table_column *clms_paths_clt[CLM_MAX_CNT];
struct table_column *clms_paths_srv[CLM_MAX_CNT];
bool notree_set;
bool noterm_set;
bool help_set;
bool verbose_set;
bool debug_set;
bool simulate_set;
bool complete_set;
int unit_id;
bool unit_set;
char unit[5];
int prec;
bool prec_set;
bool noheaders_set;
bool nototals_set;
bool force_set;
bool all_set;
bool add_missing_set;
struct path paths[MAX_PATHS_PER_SESSION]; /* lazy */
int path_cnt;
struct port_desc port_descs[MAX_PATHS_PER_SESSION];
int port_cnt;
const char *from;
bool from_set;
};
int get_unit_index(const char *unit, int *index);
int get_unit_shift(const char *unit, int *shift);
int i_to_str_unit(uint64_t d, char *str, size_t len, int unit, int prec);
int i_to_str(uint64_t d, char *str, size_t len, int prec);
void trim(char *s);
/*
* Convert string [0-9]+[BKMGTPE] to size in ctx.
* If unit is provided, the size is converted to sectors,
* otherwise it is just a raw value which we have to apply a unit on.
* return 0 on success, negative if conversion failed
*/
int str_to_size(const char *str, struct rnbd_ctx *ctx);
int i_to_byte_unit(char *str, size_t len, const struct rnbd_ctx *ctx,
uint64_t v, bool humanize);
int byte_to_str(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sd_state_to_str(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sd_devname_to_str(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sd_devpath_to_str(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sd_rx_to_str(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sd_tx_to_str(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int dev_sessname_to_str(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int rnbd_path_state_to_str(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int path_to_sessname(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int path_to_hostname(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sd_sess_to_direction(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sd_sess_to_hostname(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int act_path_cnt_to_state(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sess_side_to_direction(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int path_sess_to_direction(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int path_to_shortdesc(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int sessname_to_srvname(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
bool is_path_addr(const char *arg);
int addr_to_norm(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
int path_to_norm(char *str, size_t len, const struct rnbd_ctx *ctx,
enum color *clr, void *v, bool humanize);
bool match_path_addr(const char *left, const char *right);
int read_port_descs(struct port_desc *port_descs, int max_ports);
int sessname_from_host(const char *from_name, char *out_buf, size_t buf_len);
int resolve_host(const char *from_name, struct path *path,
const struct rnbd_ctx *ctx);
int hostname_from_path(char *host, int host_len, const char *hca, int port,
const char *server_gid);
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
enum rnbd_token {
/* mode */
TOK_NONE = 0,
TOK_CLIENT = 1,
TOK_SERVER,
TOK_BOTH,
/* object */
TOK_DEVICES,
TOK_SESSIONS,
TOK_PATHS,
/* commands */
TOK_DUMP,
TOK_LIST,
TOK_SHOW,
TOK_MAP,
TOK_RESIZE,
TOK_UNMAP,
TOK_REMAP,
TOK_CLOSE,
TOK_DISCONNECT,
TOK_RECONNECT,
TOK_RECOVER,
TOK_ADD,
TOK_DELETE,
TOK_READD,
/* access permissions */
TOK_RO,
TOK_RW,
TOK_MIGRATION,
TOK_FROM,
/* output format */
TOK_XML,
TOK_CSV,
TOK_JSON,
TOK_TERM,
/* i/o mode */
TOK_BLOCKIO,
TOK_FILEIO,
/* output */
TOK_HELP,
TOK_VERBOSE,
TOK_VERSION,
/* units */
TOK_BYTE,
TOK_KIB,
TOK_MIB,
TOK_GIB,
TOK_TIB,
TOK_PIB,
TOK_EIB,
/*flags*/
TOK_NOTREE,
TOK_NOHEADERS,
TOK_NOTOTALS,
TOK_FORCE,
TOK_NOTERM,
TOK_ALL,
};
#endif /* __H_MISC */