Skip to content

Commit

Permalink
make knet_host_add comply with documented API
Browse files Browse the repository at this point in the history
also continue renaming node_id to host_id as we go

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Dec 20, 2012
1 parent d6260a3 commit 1488d1e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
70 changes: 47 additions & 23 deletions libknet/host.c
Expand Up @@ -20,38 +20,63 @@
#include "internals.h"
#include "logging.h"

int knet_host_add(knet_handle_t knet_h, uint16_t node_id)
int knet_host_add(knet_handle_t knet_h, uint16_t host_id)
{
int link_idx, ret = 0; /* success */
uint8_t link_idx, savederrno = 0, err = 0;
struct knet_host *host;

if ((ret = pthread_rwlock_wrlock(&knet_h->list_rwlock)) != 0) {
log_debug(knet_h, KNET_SUB_HOST, "host_add: Unable to get write lock");
goto exit_clean;
if (pthread_rwlock_wrlock(&knet_h->list_rwlock)) {
savederrno = errno;
log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s",
strerror(savederrno));
errno = savederrno;
return -1;
}

if (knet_h->host_index[node_id] != NULL) {
log_debug(knet_h, KNET_SUB_HOST, "host_add: host already exists");
errno = ret = EEXIST;
if (knet_h->host_index[host_id]) {
err = -1;
savederrno = EEXIST;
log_debug(knet_h, KNET_SUB_HOST, "Unable to add host %u: %s",
host_id, strerror(savederrno));
goto exit_unlock;
}

if ((host = malloc(sizeof(struct knet_host))) == NULL) {
log_debug(knet_h, KNET_SUB_HOST, "host_add: unable to allocate memory for host");
host = malloc(sizeof(struct knet_host));
if (!host) {
err = -1;
savederrno = errno;
log_err(knet_h, KNET_SUB_HOST, "Unable to allocate memory for host %u: %s",
host_id, strerror(savederrno));
goto exit_unlock;
}

memset(host, 0, sizeof(struct knet_host));

host->node_id = node_id;
snprintf(host->name, KNET_MAX_HOST_LEN - 1, "%u", node_id);
/*
* set host_id
*/
host->host_id = host_id;

for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++)
/*
* set default host->name to host_id for logging
*/
snprintf(host->name, KNET_MAX_HOST_LEN - 1, "%u", host_id);

/*
* initialize links internal data
*/
for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) {
host->link[link_idx].link_id = link_idx;
}

/* adding new host to the index */
knet_h->host_index[node_id] = host;
/*
* add new host to the index
*/
knet_h->host_index[host_id] = host;

/*
* add new host to host list
*/
if (!knet_h->host_head) {
knet_h->host_head = host;
knet_h->host_tail = host;
Expand All @@ -60,11 +85,10 @@ int knet_host_add(knet_handle_t knet_h, uint16_t node_id)
knet_h->host_tail = host;
}

exit_unlock:
exit_unlock:
pthread_rwlock_unlock(&knet_h->list_rwlock);

exit_clean:
return ret;
errno = savederrno;
return err;
}

int knet_host_remove(knet_handle_t knet_h, uint16_t node_id)
Expand All @@ -86,12 +110,12 @@ int knet_host_remove(knet_handle_t knet_h, uint16_t node_id)
removed = NULL;

/* removing host from list */
if (knet_h->host_head->node_id == node_id) {
if (knet_h->host_head->host_id == node_id) {
removed = knet_h->host_head;
knet_h->host_head = removed->next;
} else {
for (host = knet_h->host_head; host->next != NULL; host = host->next) {
if (host->next->node_id == node_id) {
if (host->next->host_id == node_id) {
removed = host->next;
host->next = removed->next;
break;
Expand Down Expand Up @@ -181,7 +205,7 @@ int knet_host_get_id(knet_handle_t knet_h, const char *name, uint16_t *node_id)

for (host = knet_h->host_head; host != NULL; host = host->next) {
if (!strcmp(name, host->name)) {
*node_id = host->node_id;
*node_id = host->host_id;
ret = 1;
break;
}
Expand All @@ -208,7 +232,7 @@ int knet_host_list(knet_handle_t knet_h, uint16_t *host_ids, size_t *ids_entries
entries = 0;

for (host = knet_h->host_head; host != NULL; host = host->next) {
host_ids[entries] = host->node_id;
host_ids[entries] = host->host_id;
entries++;
}

Expand Down
2 changes: 1 addition & 1 deletion libknet/internals.h
Expand Up @@ -57,7 +57,7 @@ typedef uint16_t seq_num_t;

struct knet_host {
/* required */
uint16_t node_id;
uint16_t host_id;
/* configurable */
uint8_t link_handler_policy;
char name[KNET_MAX_HOST_LEN];
Expand Down
21 changes: 19 additions & 2 deletions libknet/libknet.h
Expand Up @@ -236,9 +236,26 @@ struct knet_handle_crypto_cfg {
int knet_handle_crypto(knet_handle_t knet_h,
struct knet_handle_crypto_cfg *knet_handle_crypto_cfg);

/* host */
/*
* host structs/API calls
*/

/*
* knet_host_add
*
* knet_h - pointer to knet_handle_t
*
* host_id - each host in a knet is identified with a unique ID
* (see also knet_handle_new documentation above)
*
* knet_host_add returns:
*
* 0 on success
* -1 on error and errno is set.
*/

int knet_host_add(knet_handle_t knet_h, uint16_t host_id);

int knet_host_add(knet_handle_t knet_h, uint16_t node_id);
int knet_host_remove(knet_handle_t knet_h, uint16_t node_id);

/* name must be <= KNET_MAX_HOST_LEN */
Expand Down
6 changes: 3 additions & 3 deletions libknet/threads.c
Expand Up @@ -356,7 +356,7 @@ static void _handle_recv_from_links(knet_handle_t knet_h, int sockfd)
if (!src_link->status.connected) {
log_info(knet_h, KNET_SUB_LINK, "host: %s link: %s is up",
src_host->name, src_link->status.dst_ipaddr);
_link_updown(knet_h, src_host->node_id, src_link, src_link->status.configured, 1);
_link_updown(knet_h, src_host->host_id, src_link, src_link->status.configured, 1);
}
}

Expand Down Expand Up @@ -396,7 +396,7 @@ static void _handle_recv_from_links(knet_handle_t knet_h, int sockfd)
src_host->name,
src_link->status.dst_ipaddr,
src_link->remoteconnected);
if (_dst_cache_update(knet_h, src_host->node_id)) {
if (_dst_cache_update(knet_h, src_host->host_id)) {
log_debug(knet_h, KNET_SUB_LINK,
"Unable to update switch cache (host: %s link: %s remote connected: %u)",
src_host->name,
Expand Down Expand Up @@ -589,7 +589,7 @@ static void _handle_check_each(knet_handle_t knet_h, struct knet_host *dst_host,
if (diff_ping >= (dst_link->pong_timeout * 1000llu)) {
log_info(knet_h, KNET_SUB_LINK, "host: %s link: %s is down",
dst_host->name, dst_link->status.dst_ipaddr);
_link_updown(knet_h, dst_host->node_id, dst_link, dst_link->status.configured, 0);
_link_updown(knet_h, dst_host->host_id, dst_link, dst_link->status.configured, 0);
}
}
}
Expand Down

0 comments on commit 1488d1e

Please sign in to comment.