[sheepdog] [PATCH 10/14] collie: use byte array for sdhost
MORITA Kazutaka
morita.kazutaka at gmail.com
Sat Aug 10 17:58:51 CEST 2013
From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
This simplifies code a bit, and also, fixes a problem that collie
doesn't show an error when an invalid address is specified to '-a'.
Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
collie/collie.c | 8 ++++++--
collie/collie.h | 7 ++++---
collie/common.c | 12 ++++++------
collie/node.c | 25 ++++++++-----------------
collie/vdi.c | 27 ++++++++++++---------------
tests/unit/collie/mock_collie.c | 2 +-
6 files changed, 37 insertions(+), 44 deletions(-)
diff --git a/collie/collie.c b/collie/collie.c
index 6982690..a1b0bfe 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -22,7 +22,8 @@
#define EPOLL_SIZE 4096
static const char program_name[] = "collie";
-const char *sdhost = "127.0.0.1";
+/* default sdhost is "127.0.0.1" */
+uint8_t sdhost[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1 };
int sdport = SD_LISTEN_PORT;
bool highlight = true;
bool raw_output;
@@ -365,7 +366,10 @@ int main(int argc, char **argv)
switch (ch) {
case 'a':
- sdhost = optarg;
+ if (!str_to_addr(optarg, sdhost)) {
+ sd_err("Invalid ip address %s", optarg);
+ return EXIT_FAILURE;
+ }
break;
case 'p':
sdport = strtol(optarg, &p, 10);
diff --git a/collie/collie.h b/collie/collie.h
index 2e4dc6d..f88f1e5 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -49,7 +49,7 @@ struct subcommand {
};
void subcommand_usage(char *cmd, char *subcmd, int status);
-extern const char *sdhost;
+extern uint8_t sdhost[16];
extern int sdport;
extern bool highlight;
extern bool raw_output;
@@ -72,8 +72,9 @@ int sd_read_object(uint64_t oid, void *data, unsigned int datalen,
int sd_write_object(uint64_t oid, uint64_t cow_oid, void *data,
unsigned int datalen, uint64_t offset, uint32_t flags,
int copies, bool create, bool direct);
-int collie_exec_req(const char *host, int port, struct sd_req *hdr, void *data);
-int send_light_req(struct sd_req *hdr, const char *host, int port);
+int collie_exec_req(const uint8_t *addr, int port, struct sd_req *hdr,
+ void *data);
+int send_light_req(struct sd_req *hdr, const uint8_t *addr, int port);
int do_generic_subcommand(struct subcommand *sub, int argc, char **argv);
int update_node_list(int max_nodes);
void confirm(const char *message);
diff --git a/collie/common.c b/collie/common.c
index 9d0147c..820afd6 100644
--- a/collie/common.c
+++ b/collie/common.c
@@ -167,14 +167,14 @@ out:
return ret;
}
-int collie_exec_req(const char *host, int port, struct sd_req *hdr, void *buf)
+int collie_exec_req(const uint8_t *addr, int port, struct sd_req *hdr,
+ void *buf)
{
- struct node_id nid;
+ struct node_id nid = {};
struct sockfd *sfd;
int ret;
- memset(&nid, 0, sizeof(nid));
- str_to_addr(host, nid.addr);
+ memcpy(nid.addr, addr, sizeof(nid.addr));
nid.port = port;
sfd = sockfd_cache_get(&nid);
@@ -194,9 +194,9 @@ int collie_exec_req(const char *host, int port, struct sd_req *hdr, void *buf)
}
/* Light request only contains header, without body content. */
-int send_light_req(struct sd_req *hdr, const char *host, int port)
+int send_light_req(struct sd_req *hdr, const uint8_t *addr, int port)
{
- int ret = collie_exec_req(host, port, hdr, NULL);
+ int ret = collie_exec_req(addr, port, hdr, NULL);
if (ret == -1)
return -1;
diff --git a/collie/node.c b/collie/node.c
index 79d4066..891f6e2 100644
--- a/collie/node.c
+++ b/collie/node.c
@@ -56,18 +56,16 @@ static int node_info(int argc, char **argv)
printf("Id\tSize\tUsed\tAvail\tUse%%\n");
for (i = 0; i < sd_nodes_nr; i++) {
- char host[128];
struct sd_req req;
struct sd_rsp *rsp = (struct sd_rsp *)&req;
char store_str[UINT64_DECIMAL_SIZE],
used_str[UINT64_DECIMAL_SIZE],
free_str[UINT64_DECIMAL_SIZE];
- addr_to_str(host, sizeof(host), sd_nodes[i].nid.addr, 0);
-
sd_init_req(&req, SD_OP_STAT_SHEEP);
- ret = send_light_req(&req, host, sd_nodes[i].nid.port);
+ ret = send_light_req(&req, sd_nodes[i].nid.addr,
+ sd_nodes[i].nid.port);
size_to_str(rsp->node.store_size, store_str, sizeof(store_str));
size_to_str(rsp->node.store_free, free_str, sizeof(free_str));
@@ -206,12 +204,12 @@ static int node_recovery(int argc, char **argv)
struct recovery_state state;
memset(&state, 0, sizeof(state));
- addr_to_str(host, sizeof(host), sd_nodes[i].nid.addr, 0);
sd_init_req(&req, SD_OP_STAT_RECOVERY);
req.data_length = sizeof(state);
- ret = collie_exec_req(host, sd_nodes[i].nid.port, &req, &state);
+ ret = collie_exec_req(sd_nodes[i].nid.addr,
+ sd_nodes[i].nid.port, &req, &state);
if (ret < 0)
return EXIT_SYSFAIL;
@@ -236,7 +234,6 @@ static int node_recovery(int argc, char **argv)
static int node_kill(int argc, char **argv)
{
- char host[128];
int node_id, ret;
struct sd_req req;
const char *p = argv[optind++];
@@ -253,11 +250,10 @@ static int node_kill(int argc, char **argv)
exit(EXIT_USAGE);
}
- addr_to_str(host, sizeof(host), sd_nodes[node_id].nid.addr, 0);
-
sd_init_req(&req, SD_OP_KILL_NODE);
- ret = send_light_req(&req, host, sd_nodes[node_id].nid.port);
+ ret = send_light_req(&req, sd_nodes[node_id].nid.addr,
+ sd_nodes[node_id].nid.port);
if (ret) {
sd_err("Failed to execute request");
exit(EXIT_FAILURE);
@@ -274,13 +270,11 @@ static int node_md_info(struct node_id *nid)
struct sd_req hdr;
struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
int ret, i;
- char host[HOST_NAME_MAX];
sd_init_req(&hdr, SD_OP_MD_INFO);
hdr.data_length = sizeof(info);
- addr_to_str(host, sizeof(host), nid->addr, 0);
- ret = collie_exec_req(host, nid->port, &hdr, &info);
+ ret = collie_exec_req(nid->addr, nid->port, &hdr, &info);
if (ret < 0)
return EXIT_SYSFAIL;
@@ -313,10 +307,7 @@ static int md_info(int argc, char **argv)
if (!node_cmd_data.all_nodes) {
struct node_id nid = {.port = sdport};
- if (!str_to_addr(sdhost, nid.addr)) {
- sd_err("Invalid address %s", sdhost);
- return EXIT_FAILURE;
- }
+ memcpy(nid.addr, sdhost, sizeof(nid.addr));
return node_md_info(&nid);
}
diff --git a/collie/vdi.c b/collie/vdi.c
index 852f7f6..9991beb 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -316,16 +316,16 @@ static void parse_objs(uint64_t oid, obj_parser_func_t func, void *data, unsigne
hdr.obj.oid = oid;
- addr_to_str(name, sizeof(name), sd_nodes[i].nid.addr, 0);
- ret = collie_exec_req(name, sd_nodes[i].nid.port, &hdr, buf);
+ ret = collie_exec_req(sd_nodes[i].nid.addr,
+ sd_nodes[i].nid.port, &hdr, buf);
if (ret < 0)
continue;
- snprintf(name + strlen(name), sizeof(name) - strlen(name),
- ":%d", sd_nodes[i].nid.port);
-
untrim_zero_blocks(buf, rsp->obj.offset, rsp->data_length,
size);
+
+ addr_to_str(name, sizeof(name), sd_nodes[i].nid.addr,
+ sd_nodes[i].nid.port);
cb_ret = func(name, oid, rsp, buf, data);
if (cb_ret)
break;
@@ -1326,7 +1326,6 @@ static void *read_object_from(const struct sd_vnode *vnode, uint64_t oid)
struct sd_req hdr;
struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
int ret;
- char name[128];
void *buf;
size_t size = get_objsize(oid);
@@ -1339,8 +1338,7 @@ static void *read_object_from(const struct sd_vnode *vnode, uint64_t oid)
hdr.obj.oid = oid;
- addr_to_str(name, sizeof(name), vnode->nid.addr, 0);
- ret = collie_exec_req(name, vnode->nid.port, &hdr, buf);
+ ret = collie_exec_req(vnode->nid.addr, vnode->nid.port, &hdr, buf);
if (ret < 0)
exit(EXIT_SYSFAIL);
@@ -1367,7 +1365,6 @@ static void write_object_to(const struct sd_vnode *vnode, uint64_t oid,
struct sd_req hdr;
struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
int ret;
- char name[128];
if (create)
sd_init_req(&hdr, SD_OP_CREATE_AND_WRITE_PEER);
@@ -1378,8 +1375,7 @@ static void write_object_to(const struct sd_vnode *vnode, uint64_t oid,
hdr.data_length = get_objsize(oid);
hdr.obj.oid = oid;
- addr_to_str(name, sizeof(name), vnode->nid.addr, 0);
- ret = collie_exec_req(name, vnode->nid.port, &hdr, buf);
+ ret = collie_exec_req(vnode->nid.addr, vnode->nid.port, &hdr, buf);
if (ret < 0)
exit(EXIT_SYSFAIL);
@@ -1461,8 +1457,8 @@ static void vdi_hash_check_work(struct work *work)
hdr.obj.oid = info->oid;
hdr.obj.tgt_epoch = sd_epoch;
- addr_to_str(host, sizeof(host), vcw->vnode->nid.addr, 0);
- ret = collie_exec_req(host, vcw->vnode->nid.port, &hdr, NULL);
+ ret = collie_exec_req(vcw->vnode->nid.addr, vcw->vnode->nid.port, &hdr,
+ NULL);
if (ret < 0)
exit(EXIT_SYSFAIL);
@@ -1476,8 +1472,9 @@ static void vdi_hash_check_work(struct work *work)
vcw->object_found = false;
break;
default:
- sd_err("failed to read %" PRIx64 " from %s:%d, %s", info->oid,
- host, vcw->vnode->nid.port, sd_strerror(ret));
+ sd_err("failed to read %" PRIx64 " from %s, %s", info->oid,
+ addr_to_str(host, sizeof(host), vcw->vnode->nid.addr,
+ vcw->vnode->nid.port), sd_strerror(ret));
exit(EXIT_FAILURE);
}
}
diff --git a/tests/unit/collie/mock_collie.c b/tests/unit/collie/mock_collie.c
index c66d730..ff52ed1 100644
--- a/tests/unit/collie/mock_collie.c
+++ b/tests/unit/collie/mock_collie.c
@@ -15,7 +15,7 @@
#include "mock.h"
/* collie mock */
-const char *sdhost = "127.0.0.1";
+uint8_t sdhost[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1 };
int sdport = 7000, sd_vnodes_nr = 100;
bool highlight = true;
bool raw_output;
--
1.7.9.5
More information about the sheepdog
mailing list