Skip to content

Commit

Permalink
Minor fix-ups for zrepl code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Kryl committed Jun 26, 2018
1 parent 371d78f commit 0daccaf
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 68 deletions.
1 change: 1 addition & 0 deletions cmd/zrepl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/zrepl
71 changes: 37 additions & 34 deletions cmd/zrepl/zrepl.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,23 +436,26 @@ uzfs_zvol_mgmt_do_handshake(zvol_io_hdr_t *hdr, int sfd, char *name)
char *p = NULL;

printf("Volume: %s sent for enq\n", name);
hdr->len = sizeof (mgmt_ack_t);
zinfo = uzfs_zinfo_lookup(name);
if (zinfo != NULL) {
hdr->status = ZVOL_OP_STATUS_OK;
} else {
/*
* XXX if anything in this function fails we should not send any
* payload at all - just a header with failed status.
*/
if (zinfo == NULL) {
hdr->status = ZVOL_OP_STATUS_FAILED;
} else {
hdr->status = ZVOL_OP_STATUS_OK;
}
hdr->len = sizeof (mgmt_ack_t);

bzero(&mgmt_ack, sizeof (mgmt_ack));
strncpy(mgmt_ack.volname, name, strlen(name));
mgmt_ack.port = atoi(accpt_port);
rc = uzfs_zvol_get_ip(mgmt_ack.ip);
printf("IP address: %s\n", mgmt_ack.ip);
if (rc == -1) {
hdr->status = ZVOL_OP_STATUS_FAILED;
ZREPL_ERRLOG("Unable to get IP"
" with err:%d\n", errno);
goto exit;
}

packet = kmem_alloc((sizeof (mgmt_ack_t) + sizeof (*hdr)) *
Expand All @@ -466,11 +469,10 @@ uzfs_zvol_mgmt_do_handshake(zvol_io_hdr_t *hdr, int sfd, char *name)
" with err:%d\n", errno);
rc = -1;
}
exit:
if (packet != NULL) {
if (packet != NULL)
free(packet);
}
uzfs_zinfo_drop_refcnt(zinfo, false);
if (zinfo != NULL)
uzfs_zinfo_drop_refcnt(zinfo, false);
return (rc);
}

Expand All @@ -479,27 +481,24 @@ uzfs_zvol_mgmt_do_handshake(zvol_io_hdr_t *hdr, int sfd, char *name)
* Side Car has to find a more smart way to pass
* ISCSI Controller IP address.
*/
static char *
get_controller_ip_address(void)
static int
get_controller_ip_address(char *buf, int len)
{
size_t nbytes;
char *buf = NULL;

FILE *fp = fopen("/var/openebs/controllers.conf", "r");
if (fp == NULL) {
printf("Error opening file\n");
return (buf);
return (-1);
}

buf = kmem_alloc(1024, KM_SLEEP);
nbytes = fread(buf, sizeof (char), 1024, fp);
nbytes = fread(buf, sizeof (char), len, fp);

if (nbytes <= 0) {
printf("Read error\n");
return (buf);
return (-1);
}
printf("buffer value is %s", buf);
return (buf);
return (0);
}

/*
Expand All @@ -510,31 +509,32 @@ get_controller_ip_address(void)
static void
uzfs_zvol_mgmt_thread(void *arg)
{

const char *target_addr = arg;
char buf[256];
int sfd, rc, count;
struct sockaddr_in istgt_addr;
zvol_io_hdr_t hdr = {0, };
char *name = NULL;
char *buf = NULL;


sfd = create_and_bind(mgmt_port, false);
if (sfd == -1) {
goto exit;
}

buf = get_controller_ip_address();
if (buf == NULL) {
printf("parsing IP address did not work\n");
goto exit;
if (target_addr == NULL) {
if (get_controller_ip_address(buf, sizeof (buf)) != 0) {
printf("parsing IP address did not work\n");
goto exit;
}
target_addr = buf;
}

printf("Controller IP address is: %s", buf);
printf("Controller IP address is: %s\n", target_addr);
bzero((char *)&istgt_addr, sizeof (istgt_addr));
istgt_addr.sin_family = AF_INET;
istgt_addr.sin_addr.s_addr = inet_addr(buf);
istgt_addr.sin_port = htons(6060);
free(buf);
istgt_addr.sin_addr.s_addr = inet_addr(target_addr);
istgt_addr.sin_port = htons(TARGET_PORT);
retry:
rc = connect(sfd, (struct sockaddr *)&istgt_addr, sizeof (istgt_addr));
if ((rc == -1) && ((errno == EINTR) || (errno == ECONNREFUSED) ||
Expand Down Expand Up @@ -1056,12 +1056,12 @@ uzfs_zrepl_walk_pool_directory(void)
* Main function for replica.
*/
int
main(void)
main(int argc, char **argv)
{
int rc;
kthread_t *conn_accpt_thrd;
kthread_t *uzfs_mgmt_thread;

kthread_t *conn_accpt_thrd;
kthread_t *uzfs_mgmt_thread;
char *target_addr = NULL;

pthread_t slf = pthread_self();
snprintf(tinfo, sizeof (tinfo), "m#%d.%d",
Expand Down Expand Up @@ -1092,6 +1092,9 @@ main(void)
goto initialize_error;
}

if (argc > 1)
target_addr = argv[1];

conn_accpt_thrd = zk_thread_create(NULL, 0,
(thread_func_t)uzfs_zvol_io_conn_acceptor,
NULL, 0, NULL, TS_RUN, 0,
Expand All @@ -1100,7 +1103,7 @@ main(void)

uzfs_mgmt_thread = zk_thread_create(NULL, 0,
(thread_func_t)uzfs_zvol_mgmt_thread,
NULL, 0, NULL, TS_RUN, 0,
target_addr, 0, NULL, TS_RUN, 0,
PTHREAD_CREATE_DETACHED);
VERIFY3P(uzfs_mgmt_thread, !=, NULL);

Expand Down
1 change: 1 addition & 0 deletions include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ USER_H = \
$(top_srcdir)/include/libzfs_impl.h \
$(top_srcdir)/include/uzfs_io.h \
$(top_srcdir)/include/uzfs_mgmt.h \
$(top_srcdir)/include/zrepl_prot.h \
$(top_srcdir)/include/zrepl_mgmt.h \
$(top_srcdir)/include/uzfs_test.h \
$(top_srcdir)/include/rte_ring.h \
Expand Down
64 changes: 33 additions & 31 deletions include/zrepl_mgmt.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2018 Cloudbyte. All rights reserved.
*/

#ifndef ZREPL_MGMT_H
#define ZREPL_MGMT_H

#include <pthread.h>
#include <sys/queue.h>
#include "zrepl_prot.h"

#ifdef __cplusplus
extern "C" {
#endif

#define uZFS_ZVOL_WORKERS_MAX 128
#define uZFS_ZVOL_WORKERS_DEFAULT 6
#define MAX_IP_LEN 56

extern pthread_mutex_t zvol_list_mutex;
struct zvol_io_cmd_s;
Expand Down Expand Up @@ -61,30 +89,6 @@ typedef struct zvol_info_s {
int write_req_ack_cnt;
} zvol_info_t;

typedef enum zvol_op_code_e {
ZVOL_OPCODE_HANDSHAKE = 1,
ZVOL_OPCODE_READ,
ZVOL_OPCODE_WRITE,
ZVOL_OPCODE_UNMAP,
ZVOL_OPCODE_SYNC,
ZVOL_OPCODE_SNAP_CREATE,
ZVOL_OPCODE_SNAP_ROLLBACK,
} zvol_op_code_t;

typedef enum zvol_op_status_e {
ZVOL_OP_STATUS_OK = 1,
ZVOL_OP_STATUS_FAILED,
} zvol_op_status_t;

typedef struct zvol_io_hdr_s {
zvol_op_code_t opcode;
uint64_t io_seq;
uint64_t offset;
uint64_t len;
void *q_ptr;
zvol_op_status_t status;
} zvol_io_hdr_t;

typedef struct zvol_io_cmd_s {
STAILQ_ENTRY(zvol_io_cmd_s) cmd_link;
zvol_io_hdr_t hdr;
Expand All @@ -93,12 +97,6 @@ typedef struct zvol_io_cmd_s {
int conn;
} zvol_io_cmd_t;

typedef struct mgmt_ack_s {
char volname[MAXNAMELEN];
char ip[MAX_IP_LEN];
int port;
} mgmt_ack_t;

extern int uzfs_zinfo_init(void *zv, const char *ds_name);
extern zvol_info_t *uzfs_zinfo_lookup(const char *name);
extern void uzfs_zinfo_drop_refcnt(zvol_info_t *zinfo, int locked);
Expand Down Expand Up @@ -128,4 +126,8 @@ extern int uzfs_zinfo_destroy(const char *ds_name);
fmt, __func__, __LINE__, tinfo, ##__VA_ARGS__); \
} while (0)

#ifdef __cplusplus
}
#endif

#endif /* ZREPL_MGMT_H */
79 changes: 79 additions & 0 deletions include/zrepl_prot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2018 Cloudbyte. All rights reserved.
*/

#ifndef ZREPL_PROT_H
#define ZREPL_PROT_H

#ifdef __cplusplus
extern "C" {
#endif

/*
* Over the wire spec for replica protocol.
*
* TODO: All structures should be defined with "packed" attribute to avoid
* ambiguous padding added by compiler.
*/

#define MAX_NAME_LEN 256
#define MAX_IP_LEN 56
#define TARGET_PORT 6060

typedef enum zvol_op_code_e {
ZVOL_OPCODE_HANDSHAKE = 1,
ZVOL_OPCODE_READ,
ZVOL_OPCODE_WRITE,
ZVOL_OPCODE_UNMAP,
ZVOL_OPCODE_SYNC,
ZVOL_OPCODE_SNAP_CREATE,
ZVOL_OPCODE_SNAP_ROLLBACK,
} zvol_op_code_t;

typedef enum zvol_op_status_e {
ZVOL_OP_STATUS_OK = 1,
ZVOL_OP_STATUS_FAILED,
} zvol_op_status_t;

typedef struct zvol_io_hdr_s {
zvol_op_code_t opcode;
uint64_t io_seq;
uint64_t offset;
uint64_t len;
// XXX (void *) must be removed from over-the-wire data
void *q_ptr;
zvol_op_status_t status;
} zvol_io_hdr_t;

typedef struct mgmt_ack_s {
char volname[MAX_NAME_LEN];
char ip[MAX_IP_LEN];
// XXX this should be uint16_t type
int port;
} mgmt_ack_t;

#ifdef __cplusplus
}
#endif

#endif
8 changes: 5 additions & 3 deletions lib/libzpool/zrepl_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ uzfs_zinfo_lookup(const char *name)
break;
}
}

/* Take refcount */
uzfs_zinfo_take_refcnt(zv, true);
if (zv != NULL) {
/* Take refcount */
uzfs_zinfo_take_refcnt(zv, true);
}
(void) pthread_mutex_unlock(&zvol_list_mutex);

return (zv);
}

Expand Down

0 comments on commit 0daccaf

Please sign in to comment.