Permalink
Browse files

libknet: fix knet_link_get_status and document it

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information...
1 parent 848b161 commit 48f7e563a424937747fce2b415dc6609533c0b84 @fabbione committed Jan 29, 2013
Showing with 102 additions and 16 deletions.
  1. +45 −3 libknet/libknet.h
  2. +57 −13 libknet/link.c
View
@@ -622,6 +622,33 @@ int knet_link_set_priority(knet_handle_t knet_h, uint16_t host_id, uint8_t link_
int knet_link_get_priority(knet_handle_t knet_h, uint16_t host_id, uint8_t link_id,
uint8_t *priority);
+/*
+ * define link status structure for quick lookup
+ * struct is in flux as more stats will be added soon
+ *
+ * src/dst_{ipaddr,port} strings are filled by
+ * getnameinfo(3) when configuring the link.
+ * if the link is dynamic (see knet_link_set_config)
+ * dst_ipaddr/port will contain ipaddr/port of the currently
+ * connected peer or "Unknown" if it was not possible
+ * to determine the ipaddr/port at runtime.
+ *
+ * enabled see also knet_link_set/get_enable.
+ *
+ * connected the link is connected to a peer and ping/pong traffic
+ * is flowing.
+ *
+ * dynconnected the link has dynamic ip on the other end, and
+ * we can see the other host is sending ping to us.
+ *
+ * latency average latency of this link
+ * see also kent_link_set/get_timeout.
+ *
+ * pong_last in case the link is down, this value tells us how long
+ * ago this link was active. A value of 0 means that the link
+ * has never been active.
+ */
+
struct knet_link_status {
char src_ipaddr[KNET_MAX_HOST_LEN];
char src_port[KNET_MAX_PORT_LEN];
@@ -634,9 +661,24 @@ struct knet_link_status {
struct timespec pong_last;
};
-int knet_link_get_status(knet_handle_t knet_h,
- uint16_t node_id,
- uint8_t link_id,
+/*
+ * knet_link_get_status
+ *
+ * knet_h - pointer to knet_handle_t
+ *
+ * host_id - see above
+ *
+ * link_id - see above
+ *
+ * status - pointer to knet_link_status struct (see above)
+ *
+ * knet_link_get_status returns:
+ *
+ * 0 on success
+ * -1 on error and errno is set.
+ */
+
+int knet_link_get_status(knet_handle_t knet_h, uint16_t host_id, uint8_t link_id,
struct knet_link_status *status);
/* logging */
View
@@ -58,19 +58,6 @@ int _link_updown(knet_handle_t knet_h, uint16_t node_id,
return 0;
}
-int knet_link_get_status(knet_handle_t knet_h,
- uint16_t node_id,
- uint8_t link_id,
- struct knet_link_status *status)
-{
- if (!knet_h->host_index[node_id])
- return -1;
-
- memcpy(status, &knet_h->host_index[node_id]->link[link_id].status, sizeof(struct knet_link_status));
-
- return 0;
-}
-
int knet_link_set_config(knet_handle_t knet_h, uint16_t host_id, uint8_t link_id,
struct sockaddr_storage *src_addr,
struct sockaddr_storage *dst_addr)
@@ -702,3 +689,60 @@ int knet_link_get_priority(knet_handle_t knet_h, uint16_t host_id, uint8_t link_
errno = savederrno;
return err;
}
+
+int knet_link_get_status(knet_handle_t knet_h, uint16_t host_id, uint8_t link_id,
+ struct knet_link_status *status)
+{
+ int savederrno = 0, err = 0;
+ struct knet_host *host;
+ struct knet_link *link;
+
+ if (!knet_h) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (link_id >= KNET_MAX_LINK) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (!status) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ savederrno = pthread_rwlock_rdlock(&knet_h->list_rwlock);
+ if (savederrno) {
+ log_err(knet_h, KNET_SUB_LINK, "Unable to get read lock: %s",
+ strerror(savederrno));
+ errno = savederrno;
+ return -1;
+ }
+
+ host = knet_h->host_index[host_id];
+ if (!host) {
+ err = -1;
+ savederrno = EINVAL;
+ log_err(knet_h, KNET_SUB_LINK, "Unable to find host %u: %s",
+ host_id, strerror(savederrno));
+ goto exit_unlock;
+ }
+
+ link = &host->link[link_id];
+
+ if (!link->configured) {
+ err = -1;
+ savederrno = EINVAL;
+ log_err(knet_h, KNET_SUB_LINK, "host %u link %u is not configured: %s",
+ host_id, link_id, strerror(savederrno));
+ goto exit_unlock;
+ }
+
+ memcpy(status, &link->status, sizeof(struct knet_link_status));
+
+exit_unlock:
+ pthread_rwlock_unlock(&knet_h->list_rwlock);
+ errno = savederrno;
+ return err;
+}

0 comments on commit 48f7e56

Please sign in to comment.