netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jon Maloy <jon.maloy@ericsson.com>
To: <davem@davemloft.net>, <netdev@vger.kernel.org>
Cc: tipc-discussion@lists.sourceforge.net,
	mohan.krishna.ghanta.krishnamurthy@ericsson.com
Subject: [iproute2-next 1/2] tipc: introduce command for handling a new 128-bit node identity
Date: Wed, 28 Mar 2018 18:52:13 +0200	[thread overview]
Message-ID: <1522255934-497-2-git-send-email-jon.maloy@ericsson.com> (raw)
In-Reply-To: <1522255934-497-1-git-send-email-jon.maloy@ericsson.com>

We add the possibility to set and get a 128 bit node identifier, as
an alternative to the legacy 32-bit node address we are using now.

We also add an option to set and get 'clusterid' in the node. This
is the same as what we have so far called 'netid' and performs the
same operations. For compatibility the old 'netid' commands are
retained, -we just remove them from the help texts.

Acked-by: GhantaKrishnamurthy MohanKrishna <mohan.krishna.ghanta.krishnamurthy@ericsson.com>
Signed-off-by: Jon Maloy <jon.maloy@ericsson.com>
---
 include/uapi/linux/tipc_netlink.h |  2 +
 tipc/misc.c                       | 78 ++++++++++++++++++++++++++++++-
 tipc/misc.h                       |  2 +
 tipc/node.c                       | 98 +++++++++++++++++++++++++++++++++++++--
 4 files changed, 174 insertions(+), 6 deletions(-)

diff --git a/include/uapi/linux/tipc_netlink.h b/include/uapi/linux/tipc_netlink.h
index 469aa67..6bf8ec6 100644
--- a/include/uapi/linux/tipc_netlink.h
+++ b/include/uapi/linux/tipc_netlink.h
@@ -162,6 +162,8 @@ enum {
 	TIPC_NLA_NET_UNSPEC,
 	TIPC_NLA_NET_ID,		/* u32 */
 	TIPC_NLA_NET_ADDR,		/* u32 */
+	TIPC_NLA_NET_NODEID,		/* u64 */
+	TIPC_NLA_NET_NODEID_W1,		/* u64 */
 
 	__TIPC_NLA_NET_MAX,
 	TIPC_NLA_NET_MAX = __TIPC_NLA_NET_MAX - 1
diff --git a/tipc/misc.c b/tipc/misc.c
index 8091222..16849f1 100644
--- a/tipc/misc.c
+++ b/tipc/misc.c
@@ -12,7 +12,7 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <linux/tipc.h>
-
+#include <string.h>
 #include "misc.h"
 
 #define IN_RANGE(val, low, high) ((val) <= (high) && (val) >= (low))
@@ -33,3 +33,79 @@ uint32_t str2addr(char *str)
 	fprintf(stderr, "invalid network address \"%s\"\n", str);
 	return 0;
 }
+
+static int is_hex(char *arr, int last)
+{
+	int i;
+
+	while (!arr[last])
+		last--;
+
+	for (i = 0; i <= last; i++) {
+		if (!IN_RANGE(arr[i], '0', '9') &&
+		    !IN_RANGE(arr[i], 'a', 'f') &&
+		    !IN_RANGE(arr[i], 'A', 'F'))
+			return 0;
+	}
+	return 1;
+}
+
+static int is_name(char *arr, int last)
+{
+	int i;
+	char c;
+
+	while (!arr[last])
+		last--;
+
+	if (last > 15)
+		return 0;
+
+	for (i = 0; i <= last; i++) {
+		c = arr[i];
+		if (!IN_RANGE(c, '0', '9') && !IN_RANGE(c, 'a', 'z') &&
+		    !IN_RANGE(c, 'A', 'Z') && c != '-' && c != '_' &&
+		    c != '.' && c != ':' && c != '@')
+			return 0;
+	}
+	return 1;
+}
+
+int str2nodeid(char *str, uint8_t *id)
+{
+	int len = strlen(str);
+	int i;
+
+	if (len > 32)
+		return -1;
+
+	if (is_name(str, len - 1)) {
+		memcpy(id, str, len);
+		return 0;
+	}
+	if (!is_hex(str, len - 1))
+		return -1;
+
+	str[len] = '0';
+	for (i = 0; i < 16; i++) {
+		if (sscanf(&str[2 * i], "%2hhx", &id[i]) != 1)
+			break;
+	}
+	return 0;
+}
+
+void nodeid2str(uint8_t *id, char *str)
+{
+	int i;
+
+	if (is_name((char *)id, 15)) {
+		memcpy(str, id, 16);
+		return;
+	}
+
+	for (i = 0; i < 16; i++)
+		sprintf(&str[2 * i], "%02x", id[i]);
+
+	for (i = 31; str[i] == '0'; i--)
+		str[i] = 0;
+}
diff --git a/tipc/misc.h b/tipc/misc.h
index 585df74..6e8afdd 100644
--- a/tipc/misc.h
+++ b/tipc/misc.h
@@ -15,5 +15,7 @@
 #include <stdint.h>
 
 uint32_t str2addr(char *str);
+int str2nodeid(char *str, uint8_t *id);
+void nodeid2str(uint8_t *id, char *str);
 
 #endif
diff --git a/tipc/node.c b/tipc/node.c
index fe085ae..3ebbe0b 100644
--- a/tipc/node.c
+++ b/tipc/node.c
@@ -131,6 +131,90 @@ static int cmd_node_get_addr(struct nlmsghdr *nlh, const struct cmd *cmd,
 	return 0;
 }
 
+static int cmd_node_set_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
+			       struct cmdl *cmdl, void *data)
+{
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+	uint8_t id[16] = {0,};
+	uint64_t *w0 = (uint64_t *) &id[0];
+	uint64_t *w1 = (uint64_t *) &id[8];
+	struct nlattr *nest;
+	char *str;
+
+	if (cmdl->argc != cmdl->optind + 1) {
+		fprintf(stderr, "Usage: %s node set nodeid NODE_ID\n",
+			cmdl->argv[0]);
+		return -EINVAL;
+	}
+
+	str = shift_cmdl(cmdl);
+	if (str2nodeid(str, id)) {
+		fprintf(stderr, "Invalid node identity\n");
+		return -EINVAL;
+	}
+
+	nlh = msg_init(buf, TIPC_NL_NET_SET);
+	if (!nlh) {
+		fprintf(stderr, "error, message initialisation failed\n");
+		return -1;
+	}
+	nest = mnl_attr_nest_start(nlh, TIPC_NLA_NET);
+	mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID, *w0);
+	mnl_attr_put_u64(nlh, TIPC_NLA_NET_NODEID_W1, *w1);
+	mnl_attr_nest_end(nlh, nest);
+	return msg_doit(nlh, NULL, NULL);
+}
+
+static int nodeid_get_cb(const struct nlmsghdr *nlh, void *data)
+{
+	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
+	struct nlattr *info[TIPC_NLA_MAX + 1] = {};
+	struct nlattr *attrs[TIPC_NLA_NET_MAX + 1] = {};
+	char str[33] = {0,};
+	uint8_t id[16] = {0,};
+	uint64_t *w0 = (uint64_t *) &id[0];
+	uint64_t *w1 = (uint64_t *) &id[8];
+	int i;
+
+	mnl_attr_parse(nlh, sizeof(*genl), parse_attrs, info);
+	if (!info[TIPC_NLA_NET])
+		return MNL_CB_ERROR;
+
+	mnl_attr_parse_nested(info[TIPC_NLA_NET], parse_attrs, attrs);
+	if (!attrs[TIPC_NLA_NET_ID])
+		return MNL_CB_ERROR;
+
+	*w0 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID]);
+	*w1 = mnl_attr_get_u64(attrs[TIPC_NLA_NET_NODEID_W1]);
+	nodeid2str(id, str);
+	printf("Node Identity                     Hash\n");
+	printf("%s", str);
+	for (i = strlen(str); i <= 33; i++)
+		printf(" ");
+	cmd_node_get_addr(NULL, NULL, NULL, NULL);
+	return MNL_CB_OK;
+}
+
+static int cmd_node_get_nodeid(struct nlmsghdr *nlh, const struct cmd *cmd,
+			       struct cmdl *cmdl, void *data)
+{
+	char buf[MNL_SOCKET_BUFFER_SIZE];
+
+	if (help_flag) {
+		(cmd->help)(cmdl);
+		return -EINVAL;
+	}
+
+	nlh = msg_init(buf, TIPC_NL_NET_GET);
+	if (!nlh) {
+		fprintf(stderr, "error, message initialisation failed\n");
+		return -1;
+	}
+
+	return msg_dumpit(nlh, nodeid_get_cb, NULL);
+}
+
+
 static int netid_get_cb(const struct nlmsghdr *nlh, void *data)
 {
 	struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
@@ -204,8 +288,8 @@ static void cmd_node_set_help(struct cmdl *cmdl)
 	fprintf(stderr,
 		"Usage: %s node set PROPERTY\n\n"
 		"PROPERTIES\n"
-		" address ADDRESS       - Set local address\n"
-		" netid NETID           - Set local netid\n",
+		" identity NODEID       - Set node identity\n"
+		" clusterid CLUSTERID   - Set local cluster id\n",
 		cmdl->argv[0]);
 }
 
@@ -213,8 +297,10 @@ static int cmd_node_set(struct nlmsghdr *nlh, const struct cmd *cmd,
 			struct cmdl *cmdl, void *data)
 {
 	const struct cmd cmds[] = {
-		{ "address",	cmd_node_set_addr,	NULL },
+		{ "address",    cmd_node_set_addr,      NULL },
+		{ "identity",	cmd_node_set_nodeid,	NULL },
 		{ "netid",	cmd_node_set_netid,	NULL },
+		{ "clusterid",	cmd_node_set_netid,	NULL },
 		{ NULL }
 	};
 
@@ -226,8 +312,8 @@ static void cmd_node_get_help(struct cmdl *cmdl)
 	fprintf(stderr,
 		"Usage: %s node get PROPERTY\n\n"
 		"PROPERTIES\n"
-		" address               - Get local address\n"
-		" netid                 - Get local netid\n",
+		" identity              - Get node identity\n"
+		" clusterid             - Get local clusterid\n",
 		cmdl->argv[0]);
 }
 
@@ -236,7 +322,9 @@ static int cmd_node_get(struct nlmsghdr *nlh, const struct cmd *cmd,
 {
 	const struct cmd cmds[] = {
 		{ "address",	cmd_node_get_addr,	NULL },
+		{ "identity",	cmd_node_get_nodeid,	NULL },
 		{ "netid",	cmd_node_get_netid,	NULL },
+		{ "clusterid",	cmd_node_get_netid,	NULL },
 		{ NULL }
 	};
 
-- 
2.1.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot

  reply	other threads:[~2018-03-28 16:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-28 16:52 [iproute2-next 0/2] tipc: changes to addressing structure Jon Maloy
2018-03-28 16:52 ` Jon Maloy [this message]
2018-03-28 16:52 ` [iproute2-next 2/2] tipc: change node address printout formats Jon Maloy
2018-03-29 17:52 ` [iproute2-next 0/2] tipc: changes to addressing structure David Ahern
2018-03-29 17:58   ` David Ahern
2018-03-29 22:49     ` Jon Maloy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1522255934-497-2-git-send-email-jon.maloy@ericsson.com \
    --to=jon.maloy@ericsson.com \
    --cc=davem@davemloft.net \
    --cc=mohan.krishna.ghanta.krishnamurthy@ericsson.com \
    --cc=netdev@vger.kernel.org \
    --cc=tipc-discussion@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).