Debian Codegraph @

tipc/link.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * link.c TIPC link functionality.
4 *
5 * Authors: Richard Alpe <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12
13 #include <linux/tipc_netlink.h>
14 #include <linux/tipc.h>
15 #include <linux/genetlink.h>
16 #include <libmnl/libmnl.h>
17
18 #include "mnl_utils.h"
19 #include "cmdl.h"
20 #include "msg.h"
21 #include "link.h"
22 #include "bearer.h"
23 #include "utils.h"
24
25 #define PRIORITY_STR "priority"
26 #define TOLERANCE_STR "tolerance"
27 #define WINDOW_STR "window"
28 #define BROADCAST_STR "broadcast"
29
30 static const char tipc_bclink_name[] = "broadcast-link";
31
32 static int link_list_cb(const struct nlmsghdr *nlh, void *data)
33 {
34 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
35 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
36 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1] = {};
37
38 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
39 if (!info[TIPC_NLA_LINK])
40 return MNL_CB_ERROR;
41
42 mnl_attr_parse_nested(info[TIPC_NLA_LINK], parse_attrs, attrs);
43 if (!attrs[TIPC_NLA_LINK_NAME])
44 return MNL_CB_ERROR;
45
46 print_string(PRINT_FP, NULL, "%s: ",
47 mnl_attr_get_str(attrs[TIPC_NLA_LINK_NAME]));
48 if (attrs[TIPC_NLA_LINK_UP])
49 print_string(PRINT_ANY,
50 mnl_attr_get_str(attrs[TIPC_NLA_LINK_NAME]),"%s\n", "up");
51 else
52 print_string(PRINT_ANY,
53 mnl_attr_get_str(attrs[TIPC_NLA_LINK_NAME]), "%s\n", "down");
54 return MNL_CB_OK;
55 }
56
57 static int cmd_link_list(struct nlmsghdr *nlh, const struct cmd *cmd,
58 struct cmdl *cmdl, void *data)
59 {
60 int err = 0;
61
62 if (help_flag) {
63 fprintf(stderr, "Usage: %s link list\n", cmdl->argv[0]);
64 return -EINVAL;
65 }
66
67 nlh = msg_init(TIPC_NL_LINK_GET);
68 if (!nlh) {
69 fprintf(stderr, "error, message initialisation failed\n");
70 return -1;
71 }
72
73 new_json_obj(json);
74 open_json_object(NULL);
75 err = msg_dumpit(nlh, link_list_cb, NULL);
76 close_json_object();
77 delete_json_obj();
78 return err;
79 }
80
81 static int link_get_cb(const struct nlmsghdr *nlh, void *data)
82 {
83 int *prop = data;
84 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
85 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
86 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1] = {};
87 struct nlattr *props[TIPC_NLA_PROP_MAX + 1] = {};
88
89 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
90 if (!info[TIPC_NLA_LINK])
91 return MNL_CB_ERROR;
92
93 mnl_attr_parse_nested(info[TIPC_NLA_LINK], parse_attrs, attrs);
94 if (!attrs[TIPC_NLA_LINK_PROP])
95 return MNL_CB_ERROR;
96
97 mnl_attr_parse_nested(attrs[TIPC_NLA_LINK_PROP], parse_attrs, props);
98 if (!props[*prop])
99 return MNL_CB_ERROR;
100
101 new_json_obj(json);
102 open_json_object(NULL);
103 switch (*prop) {
104 case TIPC_NLA_PROP_PRIO:
105 print_uint(PRINT_ANY, PRIORITY_STR, "%u\n", mnl_attr_get_u32(props[*prop]));
106 break;
107 case TIPC_NLA_PROP_TOL:
108 print_uint(PRINT_ANY, TOLERANCE_STR, "%u\n", mnl_attr_get_u32(props[*prop]));
109 break;
110 case TIPC_NLA_PROP_WIN:
111 print_uint(PRINT_ANY, WINDOW_STR, "%u\n", mnl_attr_get_u32(props[*prop]));
112 break;
113 default:
114 break;
115 }
116 close_json_object();
117 delete_json_obj();
118 return MNL_CB_OK;
119 }
120
121 static int cmd_link_get_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
122 struct cmdl *cmdl, void *data)
123 {
124 int prop;
125 struct nlattr *attrs;
126 struct opt *opt;
127 struct opt opts[] = {
128 { "link", OPT_KEYVAL, NULL },
129 { NULL }
130 };
131
132 if (strcmp(cmd->cmd, PRIORITY_STR) == 0)
133 prop = TIPC_NLA_PROP_PRIO;
134 else if ((strcmp(cmd->cmd, TOLERANCE_STR) == 0))
135 prop = TIPC_NLA_PROP_TOL;
136 else if ((strcmp(cmd->cmd, WINDOW_STR) == 0))
137 prop = TIPC_NLA_PROP_WIN;
138 else
139 return -EINVAL;
140
141 if (help_flag) {
142 (cmd->help)(cmdl);
143 return -EINVAL;
144 }
145
146 if (parse_opts(opts, cmdl) < 0)
147 return -EINVAL;
148
149 nlh = msg_init(TIPC_NL_LINK_GET);
150 if (!nlh) {
151 fprintf(stderr, "error, message initialisation failed\n");
152 return -1;
153 }
154
155 opt = get_opt(opts, "link");
156 if (!opt) {
157 fprintf(stderr, "error, missing link\n");
158 return -EINVAL;
159 }
160 attrs = mnl_attr_nest_start(nlh, TIPC_NLA_LINK);
161 mnl_attr_put_strz(nlh, TIPC_NLA_LINK_NAME, opt->val);
162 mnl_attr_nest_end(nlh, attrs);
163
164 return msg_doit(nlh, link_get_cb, &prop);
165 }
166
167 static void cmd_link_get_help(struct cmdl *cmdl)
168 {
169 fprintf(stderr, "Usage: %s link get PPROPERTY link LINK\n\n"
170 "PROPERTIES\n"
171 " tolerance - Get link tolerance\n"
172 " priority - Get link priority\n"
173 " window - Get link window\n"
174 " broadcast - Get link broadcast\n",
175 cmdl->argv[0]);
176 }
177
178 static int cmd_link_get_bcast_cb(const struct nlmsghdr *nlh, void *data)
179 {
180 int *prop = data;
181 int prop_ratio = TIPC_NLA_PROP_BROADCAST_RATIO;
182 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
183 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
184 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1] = {};
185 struct nlattr *props[TIPC_NLA_PROP_MAX + 1] = {};
186 int bc_mode;
187
188 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
189 if (!info[TIPC_NLA_LINK])
190 return MNL_CB_ERROR;
191
192 mnl_attr_parse_nested(info[TIPC_NLA_LINK], parse_attrs, attrs);
193 if (!attrs[TIPC_NLA_LINK_PROP])
194 return MNL_CB_ERROR;
195
196 mnl_attr_parse_nested(attrs[TIPC_NLA_LINK_PROP], parse_attrs, props);
197 if (!props[*prop])
198 return MNL_CB_ERROR;
199
200 bc_mode = mnl_attr_get_u32(props[*prop]);
201
202 new_json_obj(json);
203 open_json_object(NULL);
204 switch (bc_mode) {
205 case 0x1:
206 print_string(PRINT_ANY, "method", "%s\n", "BROADCAST");
207 break;
208 case 0x2:
209 print_string(PRINT_ANY, "method", "%s\n", "REPLICAST");
210 break;
211 case 0x4:
212 print_string(PRINT_ANY, "method", "%s", "AUTOSELECT");
213 close_json_object();
214 open_json_object(NULL);
215 print_uint(PRINT_ANY, "ratio", " ratio:%u\n",
216 mnl_attr_get_u32(props[prop_ratio]));
217 break;
218 default:
219 print_string(PRINT_ANY, NULL, "UNKNOWN\n", NULL);
220 break;
221 }
222 close_json_object();
223 delete_json_obj();
224 return MNL_CB_OK;
225 }
226
227 static void cmd_link_get_bcast_help(struct cmdl *cmdl)
228 {
229 fprintf(stderr, "Usage: %s link get PPROPERTY\n\n"
230 "PROPERTIES\n"
231 " broadcast - Get link broadcast\n",
232 cmdl->argv[0]);
233 }
234
235 static int cmd_link_get_bcast(struct nlmsghdr *nlh, const struct cmd *cmd,
236 struct cmdl *cmdl, void *data)
237 {
238 int prop = TIPC_NLA_PROP_BROADCAST;
239 struct nlattr *attrs;
240
241 if (help_flag) {
242 (cmd->help)(cmdl);
243 return -EINVAL;
244 }
245
246 nlh = msg_init(TIPC_NL_LINK_GET);
247 if (!nlh) {
248 fprintf(stderr, "error, message initialisation failed\n");
249 return -1;
250 }
251 attrs = mnl_attr_nest_start(nlh, TIPC_NLA_LINK);
252 /* Direct to broadcast-link setting */
253 mnl_attr_put_strz(nlh, TIPC_NLA_LINK_NAME, tipc_bclink_name);
254 mnl_attr_nest_end(nlh, attrs);
255 return msg_doit(nlh, cmd_link_get_bcast_cb, &prop);
256 }
257
258 static int cmd_link_get(struct nlmsghdr *nlh, const struct cmd *cmd,
259 struct cmdl *cmdl, void *data)
260 {
261 const struct cmd cmds[] = {
262 { PRIORITY_STR, cmd_link_get_prop, cmd_link_get_help },
263 { TOLERANCE_STR, cmd_link_get_prop, cmd_link_get_help },
264 { WINDOW_STR, cmd_link_get_prop, cmd_link_get_help },
265 { BROADCAST_STR, cmd_link_get_bcast, cmd_link_get_bcast_help },
266 { NULL }
267 };
268
269 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
270 }
271
272 static void cmd_link_stat_reset_help(struct cmdl *cmdl)
273 {
274 fprintf(stderr, "Usage: %s link stat reset link LINK\n\n", cmdl->argv[0]);
275 }
276
277 static int cmd_link_stat_reset(struct nlmsghdr *nlh, const struct cmd *cmd,
278 struct cmdl *cmdl, void *data)
279 {
280 char *link;
281 struct opt *opt;
282 struct nlattr *nest;
283 struct opt opts[] = {
284 { "link", OPT_KEYVAL, NULL },
285 { NULL }
286 };
287
288 if (help_flag) {
289 (cmd->help)(cmdl);
290 return -EINVAL;
291 }
292
293 if (parse_opts(opts, cmdl) != 1) {
294 (cmd->help)(cmdl);
295 return -EINVAL;
296 }
297
298 nlh = msg_init(TIPC_NL_LINK_RESET_STATS);
299 if (!nlh) {
300 fprintf(stderr, "error, message initialisation failed\n");
301 return -1;
302 }
303
304 opt = get_opt(opts, "link");
305 if (!opt) {
306 fprintf(stderr, "error, missing link\n");
307 return -EINVAL;
308 }
309 link = opt->val;
310
311 nest = mnl_attr_nest_start(nlh, TIPC_NLA_LINK);
312 mnl_attr_put_strz(nlh, TIPC_NLA_LINK_NAME, link);
313 mnl_attr_nest_end(nlh, nest);
314
315 return msg_doit(nlh, NULL, NULL);
316 }
317
318 static uint32_t perc(uint32_t count, uint32_t total)
319 {
320 return (count * 100 + (total / 2)) / total;
321 }
322
323 static int _show_link_stat(const char *name, struct nlattr *attrs[],
324 struct nlattr *prop[], struct nlattr *stats[])
325 {
326 uint32_t proft;
327
328 open_json_object(NULL);
329
330 print_string(PRINT_ANY, "link", "Link <%s>\n", name);
331 print_string(PRINT_JSON, "state", NULL, NULL);
332 open_json_array(PRINT_JSON, NULL);
333 if (attrs[TIPC_NLA_LINK_ACTIVE])
334 print_string(PRINT_ANY, NULL, " %s", "ACTIVE");
335 else if (attrs[TIPC_NLA_LINK_UP])
336 print_string(PRINT_ANY, NULL, " %s", "STANDBY");
337 else
338 print_string(PRINT_ANY, NULL, " %s", "DEFUNCT");
339 close_json_array(PRINT_JSON, NULL);
340
341 print_uint(PRINT_ANY, "mtu", " MTU:%u",
342 mnl_attr_get_u32(attrs[TIPC_NLA_LINK_MTU]));
343 print_uint(PRINT_ANY, PRIORITY_STR, " Priority:%u",
344 mnl_attr_get_u32(prop[TIPC_NLA_PROP_PRIO]));
345 print_uint(PRINT_ANY, TOLERANCE_STR, " Tolerance:%u ms",
346 mnl_attr_get_u32(prop[TIPC_NLA_PROP_TOL]));
347 print_uint(PRINT_ANY, WINDOW_STR, " Window:%u packets\n",
348 mnl_attr_get_u32(prop[TIPC_NLA_PROP_WIN]));
349
350 open_json_object("rx packets");
351 print_uint(PRINT_ANY, "rx packets", " RX packets:%u",
352 mnl_attr_get_u32(attrs[TIPC_NLA_LINK_RX]) -
353 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_INFO]));
354 print_uint(PRINT_ANY, "fragments", " fragments:%u",
355 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]));
356 print_uint(PRINT_ANY, "fragmented", "/%u",
357 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]));
358 print_uint(PRINT_ANY, "bundles", " bundles:%u",
359 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]));
360 print_uint(PRINT_ANY, "bundled", "/%u\n",
361 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
362 close_json_object();
363
364 open_json_object("tx packets");
365 print_uint(PRINT_ANY, "tx packets", " TX packets:%u",
366 mnl_attr_get_u32(attrs[TIPC_NLA_LINK_TX]) -
367 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_INFO]));
368 print_uint(PRINT_ANY, "fragments", " fragments:%u",
369 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]));
370 print_uint(PRINT_ANY, "fragmented", "/%u",
371 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]));
372 print_uint(PRINT_ANY, "bundles", " bundles:%u",
373 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]));
374 print_uint(PRINT_ANY, "bundled", "/%u\n",
375 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
376 close_json_object();
377
378 proft = mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_PROF_TOT]);
379 print_uint(PRINT_ANY, "tx profile sample", " TX profile sample:%u",
380 mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_CNT]));
381 print_uint(PRINT_ANY, "packets average", " packets average:%u octets\n",
382 mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_TOT]) / proft);
383
384 print_uint(PRINT_ANY, "0-64", " 0-64:%u%%",
385 perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P0]), proft));
386 print_uint(PRINT_ANY, "-256", " -256:%u%%",
387 perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P1]), proft));
388 print_uint(PRINT_ANY, "-1024", " -1024:%u%%",
389 perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P2]), proft));
390 print_uint(PRINT_ANY, "-4096", " -4096:%u%%",
391 perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P3]), proft));
392 print_uint(PRINT_ANY, "-16384", " -16384:%u%%",
393 perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P4]), proft));
394 print_uint(PRINT_ANY, "-32768", " -32768:%u%%",
395 perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P5]), proft));
396 print_uint(PRINT_ANY, "-66000", " -66000:%u%%\n",
397 perc(mnl_attr_get_u32(stats[TIPC_NLA_STATS_MSG_LEN_P6]), proft));
398
399 open_json_object("rx states");
400 print_uint(PRINT_ANY, "rx states", " RX states:%u",
401 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_STATES]));
402 print_uint(PRINT_ANY, "probes", " probes:%u",
403 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_PROBES]));
404 print_uint(PRINT_ANY, "naks", " naks:%u",
405 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]));
406 print_uint(PRINT_ANY, "defs", " defs:%u",
407 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]));
408 print_uint(PRINT_ANY, "dups", " dups:%u\n",
409 mnl_attr_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
410 close_json_object();
411
412 open_json_object("tx states");
413 print_uint(PRINT_ANY, "tx states", " TX states:%u",
414 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_STATES]));
415 print_uint(PRINT_ANY, "probes", " probes:%u",
416 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_PROBES]));
417 print_uint(PRINT_ANY, "naks", " naks:%u",
418 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]));
419 print_uint(PRINT_ANY, "acks", " acks:%u",
420 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]));
421 print_uint(PRINT_ANY, "retrans", " retrans:%u\n",
422 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
423 close_json_object();
424
425 print_uint(PRINT_ANY, "congestion link", " Congestion link:%u",
426 mnl_attr_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]));
427 print_uint(PRINT_ANY, "send queue max", " Send queue max:%u",
428 mnl_attr_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]));
429 print_uint(PRINT_ANY, "avg", " avg:%u\n\n",
430 mnl_attr_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
431
432 close_json_object();
433 return MNL_CB_OK;
434 }
435
436 static int _show_bc_link_stat(const char *name, struct nlattr *prop[],
437 struct nlattr *stats[])
438 {
439 open_json_object(NULL);
440 print_string(PRINT_ANY, "link", "Link <%s>\n", name);
441 print_uint(PRINT_ANY, WINDOW_STR, " Window:%u packets\n",
442 mnl_attr_get_u32(prop[TIPC_NLA_PROP_WIN]));
443
444 open_json_object("rx packets");
445 print_uint(PRINT_ANY, "rx packets", " RX packets:%u",
446 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_INFO]));
447 print_uint(PRINT_ANY, "fragments", " fragments:%u",
448 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTS]));
449 print_uint(PRINT_ANY, "fragmented", "/%u",
450 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_FRAGMENTED]));
451 print_uint(PRINT_ANY, "bundles", " bundles:%u",
452 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLES]));
453 print_uint(PRINT_ANY, "bundled", "/%u\n",
454 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_BUNDLED]));
455 close_json_object();
456
457 open_json_object("tx packets");
458 print_uint(PRINT_ANY, "tx packets", " TX packets:%u",
459 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_INFO]));
460 print_uint(PRINT_ANY, "fragments", " fragments:%u",
461 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTS]));
462 print_uint(PRINT_ANY, "fragmented", "/%u",
463 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_FRAGMENTED]));
464 print_uint(PRINT_ANY, "bundles", " bundles:%u",
465 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLES]));
466 print_uint(PRINT_ANY, "bundled", "/%u\n",
467 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_BUNDLED]));
468 close_json_object();
469
470 open_json_object("rx naks");
471 print_uint(PRINT_ANY, "rx naks", " RX naks:%u",
472 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_NACKS]));
473 print_uint(PRINT_ANY, "defs", " defs:%u",
474 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RX_DEFERRED]));
475 print_uint(PRINT_ANY, "dups", " dups:%u\n",
476 mnl_attr_get_u32(stats[TIPC_NLA_STATS_DUPLICATES]));
477 close_json_object();
478
479 open_json_object("tx naks");
480 print_uint(PRINT_ANY, "tx naks", " TX naks:%u",
481 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_NACKS]));
482 print_uint(PRINT_ANY, "acks", " acks:%u",
483 mnl_attr_get_u32(stats[TIPC_NLA_STATS_TX_ACKS]));
484 print_uint(PRINT_ANY, "retrans", " retrans:%u\n",
485 mnl_attr_get_u32(stats[TIPC_NLA_STATS_RETRANSMITTED]));
486 close_json_object();
487
488 print_uint(PRINT_ANY, "congestion link", " Congestion link:%u",
489 mnl_attr_get_u32(stats[TIPC_NLA_STATS_LINK_CONGS]));
490 print_uint(PRINT_ANY, "send queue max", " Send queue max:%u",
491 mnl_attr_get_u32(stats[TIPC_NLA_STATS_MAX_QUEUE]));
492 print_uint(PRINT_ANY, "avg", " avg:%u\n\n",
493 mnl_attr_get_u32(stats[TIPC_NLA_STATS_AVG_QUEUE]));
494 close_json_object();
495
496 return MNL_CB_OK;
497 }
498
499 static int link_stat_show_cb(const struct nlmsghdr *nlh, void *data)
500 {
501 const char *name;
502 const char *link = data;
503 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
504 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
505 struct nlattr *attrs[TIPC_NLA_LINK_MAX + 1] = {};
506 struct nlattr *prop[TIPC_NLA_PROP_MAX + 1] = {};
507 struct nlattr *stats[TIPC_NLA_STATS_MAX + 1] = {};
508
509 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
510 if (!info[TIPC_NLA_LINK])
511 return MNL_CB_ERROR;
512
513 mnl_attr_parse_nested(info[TIPC_NLA_LINK], parse_attrs, attrs);
514 if (!attrs[TIPC_NLA_LINK_NAME] || !attrs[TIPC_NLA_LINK_PROP] ||
515 !attrs[TIPC_NLA_LINK_STATS])
516 return MNL_CB_ERROR;
517
518 mnl_attr_parse_nested(attrs[TIPC_NLA_LINK_PROP], parse_attrs, prop);
519 mnl_attr_parse_nested(attrs[TIPC_NLA_LINK_STATS], parse_attrs, stats);
520
521 name = mnl_attr_get_str(attrs[TIPC_NLA_LINK_NAME]);
522
523 /* If a link is passed, skip all but that link.
524 * Support a substring matching as well.
525 */
526 if (link && !strstr(name, link))
527 return MNL_CB_OK;
528
529 if (attrs[TIPC_NLA_LINK_BROADCAST]) {
530 return _show_bc_link_stat(name, prop, stats);
531 }
532
533 return _show_link_stat(name, attrs, prop, stats);
534 }
535
536 static void cmd_link_stat_show_help(struct cmdl *cmdl)
537 {
538 fprintf(stderr, "Usage: %s link stat show [ link { LINK | SUBSTRING | all } ]\n",
539 cmdl->argv[0]);
540 }
541
542 static int cmd_link_stat_show(struct nlmsghdr *nlh, const struct cmd *cmd,
543 struct cmdl *cmdl, void *data)
544 {
545 char *link = NULL;
546 struct opt *opt;
547 struct opt opts[] = {
548 { "link", OPT_KEYVAL, NULL },
549 { NULL }
550 };
551 struct nlattr *attrs;
552 int err = 0;
553
554 if (help_flag) {
555 (cmd->help)(cmdl);
556 return -EINVAL;
557 }
558
559 nlh = msg_init(TIPC_NL_LINK_GET);
560 if (!nlh) {
561 fprintf(stderr, "error, message initialisation failed\n");
562 return -1;
563 }
564
565 if (parse_opts(opts, cmdl) < 0)
566 return -EINVAL;
567
568 opt = get_opt(opts, "link");
569 if (opt) {
570 if (strcmp(opt->val, "all"))
571 link = opt->val;
572 /* Set the flag to dump all bc links */
573 attrs = mnl_attr_nest_start(nlh, TIPC_NLA_LINK);
574 mnl_attr_put(nlh, TIPC_NLA_LINK_BROADCAST, 0, NULL);
575 mnl_attr_nest_end(nlh, attrs);
576 }
577
578 new_json_obj(json);
579 err = msg_dumpit(nlh, link_stat_show_cb, link);
580 delete_json_obj();
581 return err;
582 }
583
584 static void cmd_link_stat_help(struct cmdl *cmdl)
585 {
586 fprintf(stderr, "Usage: %s link stat COMMAND [ARGS]\n\n"
587 "COMMANDS:\n"
588 " reset - Reset link statistics for link\n"
589 " show - Get link priority\n",
590 cmdl->argv[0]);
591 }
592
593 static int cmd_link_stat(struct nlmsghdr *nlh, const struct cmd *cmd,
594 struct cmdl *cmdl, void *data)
595 {
596 const struct cmd cmds[] = {
597 { "reset", cmd_link_stat_reset, cmd_link_stat_reset_help },
598 { "show", cmd_link_stat_show, cmd_link_stat_show_help },
599 { NULL }
600 };
601
602 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
603 }
604
605 static void cmd_link_set_help(struct cmdl *cmdl)
606 {
607 fprintf(stderr, "Usage: %s link set PPROPERTY link LINK\n\n"
608 "PROPERTIES\n"
609 " tolerance TOLERANCE - Set link tolerance\n"
610 " priority PRIORITY - Set link priority\n"
611 " window WINDOW - Set link window\n"
612 " broadcast BROADCAST - Set link broadcast\n",
613 cmdl->argv[0]);
614 }
615
616 static int cmd_link_set_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
617 struct cmdl *cmdl, void *data)
618 {
619 int val;
620 int prop;
621 struct nlattr *props;
622 struct nlattr *attrs;
623 struct opt *opt;
624 struct opt opts[] = {
625 { "link", OPT_KEYVAL, NULL },
626 { NULL }
627 };
628
629 if (strcmp(cmd->cmd, PRIORITY_STR) == 0)
630 prop = TIPC_NLA_PROP_PRIO;
631 else if ((strcmp(cmd->cmd, TOLERANCE_STR) == 0))
632 prop = TIPC_NLA_PROP_TOL;
633 else if ((strcmp(cmd->cmd, WINDOW_STR) == 0))
634 prop = TIPC_NLA_PROP_WIN;
635 else
636 return -EINVAL;
637
638 if (help_flag) {
639 (cmd->help)(cmdl);
640 return -EINVAL;
641 }
642
643 if (cmdl->optind >= cmdl->argc) {
644 fprintf(stderr, "error, missing value\n");
645 return -EINVAL;
646 }
647 val = atoi(shift_cmdl(cmdl));
648
649 if (parse_opts(opts, cmdl) < 0)
650 return -EINVAL;
651
652 nlh = msg_init(TIPC_NL_LINK_SET);
653 if (!nlh) {
654 fprintf(stderr, "error, message initialisation failed\n");
655 return -1;
656 }
657 attrs = mnl_attr_nest_start(nlh, TIPC_NLA_LINK);
658
659 opt = get_opt(opts, "link");
660 if (!opt) {
661 fprintf(stderr, "error, missing link\n");
662 return -EINVAL;
663 }
664 mnl_attr_put_strz(nlh, TIPC_NLA_LINK_NAME, opt->val);
665
666 props = mnl_attr_nest_start(nlh, TIPC_NLA_LINK_PROP);
667 mnl_attr_put_u32(nlh, prop, val);
668 mnl_attr_nest_end(nlh, props);
669
670 mnl_attr_nest_end(nlh, attrs);
671
672 return msg_doit(nlh, link_get_cb, &prop);
673 }
674
675 static void cmd_link_set_bcast_help(struct cmdl *cmdl)
676 {
677 fprintf(stderr, "Usage: %s link set broadcast PROPERTY\n\n"
678 "PROPERTIES\n"
679 " BROADCAST - Forces all multicast traffic to be\n"
680 " transmitted via broadcast only,\n"
681 " irrespective of cluster size and number\n"
682 " of destinations\n\n"
683 " REPLICAST - Forces all multicast traffic to be\n"
684 " transmitted via replicast only,\n"
685 " irrespective of cluster size and number\n"
686 " of destinations\n\n"
687 " AUTOSELECT - Auto switching to broadcast or replicast\n"
688 " depending on cluster size and destination\n"
689 " node number\n\n"
690 " ratio SIZE - Set the AUTOSELECT criteria, percentage of\n"
691 " destination nodes vs cluster size\n\n",
692 cmdl->argv[0]);
693 }
694
695 static int cmd_link_set_bcast(struct nlmsghdr *nlh, const struct cmd *cmd,
696 struct cmdl *cmdl, void *data)
697 {
698 struct nlattr *props;
699 struct nlattr *attrs;
700 struct opt *opt;
701 struct opt opts[] = {
702 { "BROADCAST", OPT_KEY, NULL },
703 { "REPLICAST", OPT_KEY, NULL },
704 { "AUTOSELECT", OPT_KEY, NULL },
705 { "ratio", OPT_KEYVAL, NULL },
706 { NULL }
707 };
708 int method = 0;
709
710 if (help_flag) {
711 (cmd->help)(cmdl);
712 return -EINVAL;
713 }
714
715 if (parse_opts(opts, cmdl) < 0)
716 return -EINVAL;
717
718 for (opt = opts; opt->key; opt++)
719 if (opt->val)
720 break;
721
722 if (!opt || !opt->key) {
723 (cmd->help)(cmdl);
724 return -EINVAL;
725 }
726
727 nlh = msg_init(TIPC_NL_LINK_SET);
728 if (!nlh) {
729 fprintf(stderr, "error, message initialisation failed\n");
730 return -1;
731 }
732
733 attrs = mnl_attr_nest_start(nlh, TIPC_NLA_LINK);
734 /* Direct to broadcast-link setting */
735 mnl_attr_put_strz(nlh, TIPC_NLA_LINK_NAME, tipc_bclink_name);
736 props = mnl_attr_nest_start(nlh, TIPC_NLA_LINK_PROP);
737
738 if (get_opt(opts, "BROADCAST"))
739 method = 0x1;
740 else if (get_opt(opts, "REPLICAST"))
741 method = 0x2;
742 else if (get_opt(opts, "AUTOSELECT"))
743 method = 0x4;
744
745 opt = get_opt(opts, "ratio");
746 if (!method && !opt) {
747 (cmd->help)(cmdl);
748 return -EINVAL;
749 }
750
751 if (method)
752 mnl_attr_put_u32(nlh, TIPC_NLA_PROP_BROADCAST, method);
753
754 if (opt)
755 mnl_attr_put_u32(nlh, TIPC_NLA_PROP_BROADCAST_RATIO,
756 atoi(opt->val));
757
758 mnl_attr_nest_end(nlh, props);
759 mnl_attr_nest_end(nlh, attrs);
760 return msg_doit(nlh, NULL, NULL);
761 }
762
763 static int cmd_link_set(struct nlmsghdr *nlh, const struct cmd *cmd,
764 struct cmdl *cmdl, void *data)
765 {
766 const struct cmd cmds[] = {
767 { PRIORITY_STR, cmd_link_set_prop, cmd_link_set_help },
768 { TOLERANCE_STR, cmd_link_set_prop, cmd_link_set_help },
769 { WINDOW_STR, cmd_link_set_prop, cmd_link_set_help },
770 { BROADCAST_STR, cmd_link_set_bcast, cmd_link_set_bcast_help },
771 { NULL }
772 };
773
774 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
775 }
776
777 static int cmd_link_mon_set_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
778 struct cmdl *cmdl, void *data)
779 {
780 int size;
781 struct nlattr *attrs;
782
783 if (cmdl->argc != cmdl->optind + 1) {
784 fprintf(stderr, "error, missing value\n");
785 return -EINVAL;
786 }
787 size = atoi(shift_cmdl(cmdl));
788
789 nlh = msg_init(TIPC_NL_MON_SET);
790 if (!nlh) {
791 fprintf(stderr, "error, message initialisation failed\n");
792 return -1;
793 }
794 attrs = mnl_attr_nest_start(nlh, TIPC_NLA_MON);
795
796 mnl_attr_put_u32(nlh, TIPC_NLA_MON_ACTIVATION_THRESHOLD, size);
797
798 mnl_attr_nest_end(nlh, attrs);
799
800 return msg_doit(nlh, NULL, NULL);
801 }
802
803 static int link_mon_summary_cb(const struct nlmsghdr *nlh, void *data)
804 {
805 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
806 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
807 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1] = {};
808
809 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
810 if (!info[TIPC_NLA_MON])
811 return MNL_CB_ERROR;
812
813 mnl_attr_parse_nested(info[TIPC_NLA_MON], parse_attrs, attrs);
814
815 open_json_object(NULL);
816 print_string(PRINT_ANY, "bearer", "\nbearer %s\n",
817 mnl_attr_get_str(attrs[TIPC_NLA_MON_BEARER_NAME]));
818
819 print_uint(PRINT_ANY, "table_generation", " table_generation %u\n",
820 mnl_attr_get_u32(attrs[TIPC_NLA_MON_LISTGEN]));
821 print_uint(PRINT_ANY, "cluster_size", " cluster_size %u\n",
822 mnl_attr_get_u32(attrs[TIPC_NLA_MON_PEERCNT]));
823 print_string(PRINT_ANY, "algorithm", " algorithm %s\n",
824 attrs[TIPC_NLA_MON_ACTIVE] ? "overlapping-ring" : "full-mesh");
825 close_json_object();
826
827 return MNL_CB_OK;
828 }
829
830 static int cmd_link_mon_summary(struct nlmsghdr *nlh, const struct cmd *cmd,
831 struct cmdl *cmdl, void *data)
832 {
833 int err = 0;
834
835 if (help_flag) {
836 fprintf(stderr, "Usage: %s monitor summary\n", cmdl->argv[0]);
837 return -EINVAL;
838 }
839
840 nlh = msg_init(TIPC_NL_MON_GET);
841 if (!nlh) {
842 fprintf(stderr, "error, message initialisation failed\n");
843 return -1;
844 }
845
846 new_json_obj(json);
847 err = msg_dumpit(nlh, link_mon_summary_cb, NULL);
848 delete_json_obj();
849
850 return err;
851 }
852
853 #define STATUS_WIDTH 7
854 #define MAX_NODE_WIDTH 14 /* 255.4095.4095 */
855 #define MAX_DOM_GEN_WIDTH 11 /* 65535 */
856 #define DIRECTLY_MON_WIDTH 10
857
858 #define APPL_NODE_STATUS_WIDTH 5
859
860 static int map_get(uint64_t up_map, int i)
861 {
862 return (up_map & (1 << i)) >> i;
863 }
864
865 /* print the applied members, since we know the the members
866 * are listed in ascending order, we print only the state
867 */
868 static void link_mon_print_applied(uint16_t applied, uint64_t up_map)
869 {
870 int i;
871
872 open_json_array(PRINT_JSON, "applied_node_status");
873 for (i = 0; i < applied; i++) {
874 char state_str[2] = {0};
875
876 /* print the delimiter for every -n- entry */
877 if (i && !(i % APPL_NODE_STATUS_WIDTH))
878 print_string(PRINT_FP, NULL, "%s", ",");
879
880 sprintf(state_str, "%c", map_get(up_map, i) ? 'U' : 'D');
881 print_string(PRINT_ANY, NULL, "%s", state_str);
882 }
883 close_json_array(PRINT_JSON, "applied_node_status");
884 }
885
886 /* print the non applied members, since we don't know
887 * the members, we print them along with the state
888 */
889 static void link_mon_print_non_applied(uint16_t applied, uint16_t member_cnt,
890 uint64_t up_map, uint32_t *members)
891 {
892 int i;
893 char state;
894
895 open_json_array(PRINT_JSON, "[non_applied_node:status]");
896 print_string(PRINT_FP, NULL, " %s", "[");
897 for (i = applied; i < member_cnt; i++) {
898 char addr_str[16];
899 char full_state[17] = {0};
900
901 /* print the delimiter for every entry */
902 if (i != applied)
903 print_string(PRINT_FP, NULL, "%s", ",");
904
905 sprintf(addr_str, "%x:", members[i]);
906 state = map_get(up_map, i) ? 'U' : 'D';
907 sprintf(full_state, "%s%c", addr_str, state);
908 print_string(PRINT_ANY, NULL, "%s", full_state);
909 }
910 print_string(PRINT_FP, NULL, "%s", "]");
911 close_json_array(PRINT_JSON, "[non_applied_node:status]");
912 }
913
914 static void link_mon_print_peer_state(const uint32_t addr, const char *status,
915 const char *monitored,
916 const uint32_t dom_gen)
917 {
918 char addr_str[16];
919
920 sprintf(addr_str, "%u.%u.%u", tipc_zone(addr), tipc_cluster(addr),
921 tipc_node(addr));
922 if (is_json_context()) {
923 print_string(PRINT_JSON, "node", NULL, addr_str);
924 print_string(PRINT_JSON, "status", NULL, status);
925 print_string(PRINT_JSON, "monitored", NULL, monitored);
926 print_uint(PRINT_JSON, "generation", NULL, dom_gen);
927 } else {
928 printf("%-*s", MAX_NODE_WIDTH, addr_str);
929 printf("%-*s", STATUS_WIDTH, status);
930 printf("%-*s", DIRECTLY_MON_WIDTH, monitored);
931 printf("%-*u", MAX_DOM_GEN_WIDTH, dom_gen);
932 }
933 }
934
935 static int link_mon_peer_list_cb(const struct nlmsghdr *nlh, void *data)
936 {
937 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
938 struct nlattr *attrs[TIPC_NLA_MON_PEER_MAX + 1] = {};
939 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
940 uint16_t member_cnt;
941 uint32_t applied;
942 uint32_t dom_gen;
943 uint64_t up_map;
944 char status[16];
945 char monitored[16];
946
947 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
948 if (!info[TIPC_NLA_MON_PEER])
949 return MNL_CB_ERROR;
950
951 open_json_object(NULL);
952 mnl_attr_parse_nested(info[TIPC_NLA_MON_PEER], parse_attrs, attrs);
953
954 (attrs[TIPC_NLA_MON_PEER_LOCAL] || attrs[TIPC_NLA_MON_PEER_HEAD]) ?
955 strcpy(monitored, "direct") :
956 strcpy(monitored, "indirect");
957
958 attrs[TIPC_NLA_MON_PEER_UP] ?
959 strcpy(status, "up") :
960 strcpy(status, "down");
961
962 dom_gen = attrs[TIPC_NLA_MON_PEER_DOMGEN] ?
963 mnl_attr_get_u32(attrs[TIPC_NLA_MON_PEER_DOMGEN]) : 0;
964
965 link_mon_print_peer_state(mnl_attr_get_u32(attrs[TIPC_NLA_MON_PEER_ADDR]),
966 status, monitored, dom_gen);
967
968 applied = mnl_attr_get_u32(attrs[TIPC_NLA_MON_PEER_APPLIED]);
969
970 if (!applied)
971 goto exit;
972
973 up_map = mnl_attr_get_u64(attrs[TIPC_NLA_MON_PEER_UPMAP]);
974
975 member_cnt = mnl_attr_get_payload_len(attrs[TIPC_NLA_MON_PEER_MEMBERS]);
976
977 /* each tipc address occupies 4 bytes of payload, hence compensate it */
978 member_cnt /= sizeof(uint32_t);
979
980 link_mon_print_applied(applied, up_map);
981
982 link_mon_print_non_applied(applied, member_cnt, up_map,
983 mnl_attr_get_payload(attrs[TIPC_NLA_MON_PEER_MEMBERS]));
984
985 exit:
986 print_string(PRINT_FP, NULL, "\n", "");
987
988 close_json_object();
989 return MNL_CB_OK;
990 }
991
992 static int link_mon_peer_list(uint32_t mon_ref)
993 {
994 struct mnlu_gen_socket link_nlg;
995 struct nlmsghdr *nlh;
996 struct nlattr *nest;
997 int err = 0;
998
999 err = mnlu_gen_socket_open(&link_nlg, TIPC_GENL_V2_NAME,
1000 TIPC_GENL_V2_VERSION);
1001 if (err)
1002 return -1;
1003 nlh = mnlu_gen_socket_cmd_prepare(&link_nlg, TIPC_NL_MON_PEER_GET,
1004 NLM_F_REQUEST | NLM_F_DUMP);
1005 if (!nlh) {
1006 fprintf(stderr, "error, message initialisation failed\n");
1007 mnlu_gen_socket_close(&link_nlg);
1008 return -1;
1009 }
1010
1011 nest = mnl_attr_nest_start(nlh, TIPC_NLA_MON);
1012 mnl_attr_put_u32(nlh, TIPC_NLA_MON_REF, mon_ref);
1013 mnl_attr_nest_end(nlh, nest);
1014
1015 err = mnlu_gen_socket_sndrcv(&link_nlg, nlh, link_mon_peer_list_cb,
1016 NULL);
1017 mnlu_gen_socket_close(&link_nlg);
1018 return err;
1019 }
1020
1021 static int link_mon_list_cb(const struct nlmsghdr *nlh, void *data)
1022 {
1023 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
1024 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
1025 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1] = {};
1026 char *req_bearer = data;
1027 const char *bname;
1028 const char title[] =
1029 "node status monitored generation applied_node_status [non_applied_node:status]";
1030
1031 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
1032 if (!info[TIPC_NLA_MON])
1033 return MNL_CB_ERROR;
1034
1035 mnl_attr_parse_nested(info[TIPC_NLA_MON], parse_attrs, attrs);
1036
1037 bname = mnl_attr_get_str(attrs[TIPC_NLA_MON_BEARER_NAME]);
1038
1039 if (*req_bearer && (strcmp(req_bearer, bname) != 0))
1040 return MNL_CB_OK;
1041
1042 open_json_object(NULL);
1043 print_string(PRINT_ANY, "bearer", "\nbearer %s\n", bname);
1044 print_string(PRINT_FP, NULL, "%s\n", title);
1045
1046 open_json_array(PRINT_JSON, bname);
1047 if (mnl_attr_get_u32(attrs[TIPC_NLA_MON_PEERCNT]))
1048 link_mon_peer_list(mnl_attr_get_u32(attrs[TIPC_NLA_MON_REF]));
1049 close_json_array(PRINT_JSON, bname);
1050
1051 close_json_object();
1052 return MNL_CB_OK;
1053 }
1054
1055 static void cmd_link_mon_list_help(struct cmdl *cmdl)
1056 {
1057 fprintf(stderr, "Usage: %s monitor list [ media MEDIA ARGS...]\n\n",
1058 cmdl->argv[0]);
1059 print_bearer_media();
1060 }
1061
1062 static void cmd_link_mon_list_l2_help(struct cmdl *cmdl, char *media)
1063 {
1064 fprintf(stderr,
1065 "Usage: %s monitor list media %s device DEVICE [OPTIONS]\n",
1066 cmdl->argv[0], media);
1067 }
1068
1069 static void cmd_link_mon_list_udp_help(struct cmdl *cmdl, char *media)
1070 {
1071 fprintf(stderr,
1072 "Usage: %s monitor list media udp name NAME\n\n",
1073 cmdl->argv[0]);
1074 }
1075
1076 static int cmd_link_mon_list(struct nlmsghdr *nlh, const struct cmd *cmd,
1077 struct cmdl *cmdl, void *data)
1078 {
1079 char bname[TIPC_MAX_BEARER_NAME] = {0};
1080 struct opt opts[] = {
1081 { "media", OPT_KEYVAL, NULL },
1082 { "device", OPT_KEYVAL, NULL },
1083 { "name", OPT_KEYVAL, NULL },
1084 { NULL }
1085 };
1086 struct tipc_sup_media sup_media[] = {
1087 { "udp", "name", cmd_link_mon_list_udp_help},
1088 { "eth", "device", cmd_link_mon_list_l2_help },
1089 { "ib", "device", cmd_link_mon_list_l2_help },
1090 { NULL, },
1091 };
1092
1093 int err;
1094
1095 if (parse_opts(opts, cmdl) < 0)
1096 return -EINVAL;
1097
1098 if (get_opt(opts, "media")) {
1099 err = cmd_get_unique_bearer_name(cmd, cmdl, opts, bname,
1100 sup_media);
1101 if (err)
1102 return err;
1103 }
1104
1105 if (help_flag) {
1106 cmd->help(cmdl);
1107 return -EINVAL;
1108 }
1109
1110 nlh = msg_init(TIPC_NL_MON_GET);
1111 if (!nlh) {
1112 fprintf(stderr, "error, message initialisation failed\n");
1113 return -1;
1114 }
1115
1116 new_json_obj(json);
1117 err = msg_dumpit(nlh, link_mon_list_cb, bname);
1118 delete_json_obj();
1119 return err;
1120 }
1121
1122 static void cmd_link_mon_set_help(struct cmdl *cmdl)
1123 {
1124 fprintf(stderr, "Usage: %s monitor set PPROPERTY\n\n"
1125 "PROPERTIES\n"
1126 " threshold SIZE - Set monitor activation threshold\n",
1127 cmdl->argv[0]);
1128 }
1129
1130 static int cmd_link_mon_set(struct nlmsghdr *nlh, const struct cmd *cmd,
1131 struct cmdl *cmdl, void *data)
1132 {
1133 const struct cmd cmds[] = {
1134 { "threshold", cmd_link_mon_set_prop, NULL },
1135 { NULL }
1136 };
1137
1138 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
1139 }
1140
1141 static void cmd_link_mon_get_help(struct cmdl *cmdl)
1142 {
1143 fprintf(stderr, "Usage: %s monitor get PPROPERTY\n\n"
1144 "PROPERTIES\n"
1145 " threshold - Get monitor activation threshold\n",
1146 cmdl->argv[0]);
1147 }
1148
1149 static int link_mon_get_cb(const struct nlmsghdr *nlh, void *data)
1150 {
1151 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
1152 struct nlattr *info[TIPC_NLA_MAX + 1] = {};
1153 struct nlattr *attrs[TIPC_NLA_MON_MAX + 1] = {};
1154
1155 mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
1156 if (!info[TIPC_NLA_MON])
1157 return MNL_CB_ERROR;
1158
1159 mnl_attr_parse_nested(info[TIPC_NLA_MON], parse_attrs, attrs);
1160 if (!attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD])
1161 return MNL_CB_ERROR;
1162
1163 new_json_obj(json);
1164 print_uint(PRINT_ANY, "threshold", "%u\n",
1165 mnl_attr_get_u32(attrs[TIPC_NLA_MON_ACTIVATION_THRESHOLD]));
1166 delete_json_obj();
1167
1168 return MNL_CB_OK;
1169 }
1170
1171 static int cmd_link_mon_get_prop(struct nlmsghdr *nlh, const struct cmd *cmd,
1172 struct cmdl *cmdl, void *data)
1173 {
1174
1175 nlh = msg_init(TIPC_NL_MON_GET);
1176 if (!nlh) {
1177 fprintf(stderr, "error, message initialisation failed\n");
1178 return -1;
1179 }
1180
1181 return msg_doit(nlh, link_mon_get_cb, NULL);
1182 }
1183
1184 static int cmd_link_mon_get(struct nlmsghdr *nlh, const struct cmd *cmd,
1185 struct cmdl *cmdl, void *data)
1186 {
1187 const struct cmd cmds[] = {
1188 { "threshold", cmd_link_mon_get_prop, NULL},
1189 { NULL }
1190 };
1191
1192 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
1193 }
1194
1195 static void cmd_link_mon_help(struct cmdl *cmdl)
1196 {
1197 fprintf(stderr,
1198 "Usage: %s montior COMMAND [ARGS] ...\n\n"
1199 "COMMANDS\n"
1200 " set - Set monitor properties\n"
1201 " get - Get monitor properties\n"
1202 " list - List all cluster members\n"
1203 " summary - Show local node monitor summary\n",
1204 cmdl->argv[0]);
1205 }
1206
1207 static int cmd_link_mon(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
1208 void *data)
1209 {
1210 const struct cmd cmds[] = {
1211 { "set", cmd_link_mon_set, cmd_link_mon_set_help },
1212 { "get", cmd_link_mon_get, cmd_link_mon_get_help },
1213 { "list", cmd_link_mon_list, cmd_link_mon_list_help },
1214 { "summary", cmd_link_mon_summary, NULL },
1215 { NULL }
1216 };
1217
1218 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
1219 }
1220
1221 void cmd_link_help(struct cmdl *cmdl)
1222 {
1223 fprintf(stderr,
1224 "Usage: %s link COMMAND [ARGS] ...\n"
1225 "\n"
1226 "COMMANDS\n"
1227 " list - List links\n"
1228 " get - Get various link properties\n"
1229 " set - Set various link properties\n"
1230 " statistics - Show or reset statistics\n"
1231 " monitor - Show or set link supervision\n",
1232 cmdl->argv[0]);
1233 }
1234
1235 int cmd_link(struct nlmsghdr *nlh, const struct cmd *cmd, struct cmdl *cmdl,
1236 void *data)
1237 {
1238 const struct cmd cmds[] = {
1239 { "get", cmd_link_get, cmd_link_get_help },
1240 { "list", cmd_link_list, NULL },
1241 { "set", cmd_link_set, cmd_link_set_help },
1242 { "statistics", cmd_link_stat, cmd_link_stat_help },
1243 { "monitor", cmd_link_mon, cmd_link_mon_help },
1244 { NULL }
1245 };
1246
1247 return run_cmd(nlh, cmd, cmds, cmdl, NULL);
1248 }