]>
git.proxmox.com Git - mirror_iproute2.git/blob - tipc/node.c
2 * node.c TIPC node functionality.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Richard Alpe <richard.alpe@ericsson.com>
17 #include <linux/tipc_netlink.h>
18 #include <linux/tipc.h>
19 #include <linux/genetlink.h>
20 #include <libmnl/libmnl.h>
27 static int node_list_cb(const struct nlmsghdr
*nlh
, void *data
)
29 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
30 struct nlattr
*attrs
[TIPC_NLA_NODE_MAX
+ 1] = {};
34 mnl_attr_parse(nlh
, sizeof(struct genlmsghdr
), parse_attrs
, info
);
35 if (!info
[TIPC_NLA_NODE
])
38 mnl_attr_parse_nested(info
[TIPC_NLA_NODE
], parse_attrs
, attrs
);
39 if (!attrs
[TIPC_NLA_NODE_ADDR
])
42 addr
= mnl_attr_get_u32(attrs
[TIPC_NLA_NODE_ADDR
]);
43 hash2nodestr(addr
, str
);
44 printf("%-32s %08x ", str
, addr
);
45 if (attrs
[TIPC_NLA_NODE_UP
])
52 static int cmd_node_list(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
53 struct cmdl
*cmdl
, void *data
)
55 char buf
[MNL_SOCKET_BUFFER_SIZE
];
58 fprintf(stderr
, "Usage: %s node list\n", cmdl
->argv
[0]);
62 if (!(nlh
= msg_init(buf
, TIPC_NL_NODE_GET
))) {
63 fprintf(stderr
, "error, message initialisation failed\n");
66 printf("Node Identity Hash State\n");
67 return msg_dumpit(nlh
, node_list_cb
, NULL
);
70 static int cmd_node_set_addr(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
71 struct cmdl
*cmdl
, void *data
)
76 char buf
[MNL_SOCKET_BUFFER_SIZE
];
78 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
79 fprintf(stderr
, "Usage: %s node set address ADDRESS\n",
84 str
= shift_cmdl(cmdl
);
89 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_SET
))) {
90 fprintf(stderr
, "error, message initialisation failed\n");
94 nest
= mnl_attr_nest_start(nlh
, TIPC_NLA_NET
);
95 mnl_attr_put_u32(nlh
, TIPC_NLA_NET_ADDR
, addr
);
96 mnl_attr_nest_end(nlh
, nest
);
98 return msg_doit(nlh
, NULL
, NULL
);
101 static int cmd_node_get_addr(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
102 struct cmdl
*cmdl
, void *data
)
105 socklen_t sz
= sizeof(struct sockaddr_tipc
);
106 struct sockaddr_tipc addr
;
108 sk
= socket(AF_TIPC
, SOCK_RDM
, 0);
110 fprintf(stderr
, "opening TIPC socket: %s\n", strerror(errno
));
114 if (getsockname(sk
, (struct sockaddr
*)&addr
, &sz
) < 0) {
115 fprintf(stderr
, "getting TIPC socket address: %s\n",
122 printf("%08x\n", addr
.addr
.id
.node
);
126 static int cmd_node_set_nodeid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
127 struct cmdl
*cmdl
, void *data
)
129 char buf
[MNL_SOCKET_BUFFER_SIZE
];
130 uint8_t id
[16] = {0,};
131 uint64_t *w0
= (uint64_t *) &id
[0];
132 uint64_t *w1
= (uint64_t *) &id
[8];
136 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
137 fprintf(stderr
, "Usage: %s node set nodeid NODE_ID\n",
142 str
= shift_cmdl(cmdl
);
143 if (str2nodeid(str
, id
)) {
144 fprintf(stderr
, "Invalid node identity\n");
148 nlh
= msg_init(buf
, TIPC_NL_NET_SET
);
150 fprintf(stderr
, "error, message initialisation failed\n");
153 nest
= mnl_attr_nest_start(nlh
, TIPC_NLA_NET
);
154 mnl_attr_put_u64(nlh
, TIPC_NLA_NET_NODEID
, *w0
);
155 mnl_attr_put_u64(nlh
, TIPC_NLA_NET_NODEID_W1
, *w1
);
156 mnl_attr_nest_end(nlh
, nest
);
157 return msg_doit(nlh
, NULL
, NULL
);
160 static int nodeid_get_cb(const struct nlmsghdr
*nlh
, void *data
)
162 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
163 struct nlattr
*attrs
[TIPC_NLA_NET_MAX
+ 1] = {};
165 uint8_t id
[16] = {0,};
166 uint64_t *w0
= (uint64_t *) &id
[0];
167 uint64_t *w1
= (uint64_t *) &id
[8];
169 mnl_attr_parse(nlh
, sizeof(struct genlmsghdr
), parse_attrs
, info
);
170 if (!info
[TIPC_NLA_NET
])
173 mnl_attr_parse_nested(info
[TIPC_NLA_NET
], parse_attrs
, attrs
);
174 if (!attrs
[TIPC_NLA_NET_ID
])
177 *w0
= mnl_attr_get_u64(attrs
[TIPC_NLA_NET_NODEID
]);
178 *w1
= mnl_attr_get_u64(attrs
[TIPC_NLA_NET_NODEID_W1
]);
180 printf("Node Identity Hash\n");
181 printf("%-33s", str
);
182 cmd_node_get_addr(NULL
, NULL
, NULL
, NULL
);
186 static int cmd_node_get_nodeid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
187 struct cmdl
*cmdl
, void *data
)
189 char buf
[MNL_SOCKET_BUFFER_SIZE
];
196 nlh
= msg_init(buf
, TIPC_NL_NET_GET
);
198 fprintf(stderr
, "error, message initialisation failed\n");
202 return msg_dumpit(nlh
, nodeid_get_cb
, NULL
);
206 static int netid_get_cb(const struct nlmsghdr
*nlh
, void *data
)
208 struct nlattr
*info
[TIPC_NLA_MAX
+ 1] = {};
209 struct nlattr
*attrs
[TIPC_NLA_NET_MAX
+ 1] = {};
211 mnl_attr_parse(nlh
, sizeof(struct genlmsghdr
), parse_attrs
, info
);
212 if (!info
[TIPC_NLA_NET
])
215 mnl_attr_parse_nested(info
[TIPC_NLA_NET
], parse_attrs
, attrs
);
216 if (!attrs
[TIPC_NLA_NET_ID
])
219 printf("%u\n", mnl_attr_get_u32(attrs
[TIPC_NLA_NET_ID
]));
224 static int cmd_node_get_netid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
225 struct cmdl
*cmdl
, void *data
)
227 char buf
[MNL_SOCKET_BUFFER_SIZE
];
234 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_GET
))) {
235 fprintf(stderr
, "error, message initialisation failed\n");
239 return msg_dumpit(nlh
, netid_get_cb
, NULL
);
242 static int cmd_node_set_netid(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
243 struct cmdl
*cmdl
, void *data
)
246 char buf
[MNL_SOCKET_BUFFER_SIZE
];
254 if (!(nlh
= msg_init(buf
, TIPC_NL_NET_SET
))) {
255 fprintf(stderr
, "error, message initialisation failed\n");
259 if (cmdl
->argc
!= cmdl
->optind
+ 1) {
260 fprintf(stderr
, "Usage: %s node set netid NETID\n",
264 netid
= atoi(shift_cmdl(cmdl
));
266 nest
= mnl_attr_nest_start(nlh
, TIPC_NLA_NET
);
267 mnl_attr_put_u32(nlh
, TIPC_NLA_NET_ID
, netid
);
268 mnl_attr_nest_end(nlh
, nest
);
270 return msg_doit(nlh
, NULL
, NULL
);
273 static void cmd_node_set_help(struct cmdl
*cmdl
)
276 "Usage: %s node set PROPERTY\n\n"
278 " identity NODEID - Set node identity\n"
279 " clusterid CLUSTERID - Set local cluster id\n",
283 static int cmd_node_set(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
284 struct cmdl
*cmdl
, void *data
)
286 const struct cmd cmds
[] = {
287 { "address", cmd_node_set_addr
, NULL
},
288 { "identity", cmd_node_set_nodeid
, NULL
},
289 { "netid", cmd_node_set_netid
, NULL
},
290 { "clusterid", cmd_node_set_netid
, NULL
},
294 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
297 static void cmd_node_get_help(struct cmdl
*cmdl
)
300 "Usage: %s node get PROPERTY\n\n"
302 " identity - Get node identity\n"
303 " clusterid - Get local clusterid\n",
307 static int cmd_node_get(struct nlmsghdr
*nlh
, const struct cmd
*cmd
,
308 struct cmdl
*cmdl
, void *data
)
310 const struct cmd cmds
[] = {
311 { "address", cmd_node_get_addr
, NULL
},
312 { "identity", cmd_node_get_nodeid
, NULL
},
313 { "netid", cmd_node_get_netid
, NULL
},
314 { "clusterid", cmd_node_get_netid
, NULL
},
318 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);
321 void cmd_node_help(struct cmdl
*cmdl
)
324 "Usage: %s node COMMAND [ARGS] ...\n\n"
326 " list - List remote nodes\n"
327 " get - Get local node parameters\n"
328 " set - Set local node parameters\n",
332 int cmd_node(struct nlmsghdr
*nlh
, const struct cmd
*cmd
, struct cmdl
*cmdl
,
335 const struct cmd cmds
[] = {
336 { "list", cmd_node_list
, NULL
},
337 { "get", cmd_node_get
, cmd_node_get_help
},
338 { "set", cmd_node_set
, cmd_node_set_help
},
342 return run_cmd(nlh
, cmd
, cmds
, cmdl
, NULL
);