Skip to content

Commit

Permalink
[lists] drop internal implementation and use libqb one
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Mar 17, 2020
1 parent 8a8f9e1 commit a33337b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 194 deletions.
4 changes: 2 additions & 2 deletions configure.ac
Expand Up @@ -272,6 +272,8 @@ if test "x$enable_man" = "xyes"; then
ac_cv_path_PKG_CONFIG="$saved_ac_cv_path_PKG_CONFIG"
fi

PKG_CHECK_MODULES([libqb], [libqb])

# checks for libnozzle
if test "x$enable_libnozzle" = xyes; then
if `echo $host_os | grep -q linux`; then
Expand All @@ -294,8 +296,6 @@ if test "x$enable_kronosnetd" = xyes; then
[AC_SUBST([pam_misc_LIBS], [-lpam_misc])],
[AC_MSG_ERROR([Unable to find LinuxPAM MISC devel files])])])

PKG_CHECK_MODULES([libqb], [libqb])

AC_CHECK_LIB([qb], [qb_log_thread_priority_set],
[have_qb_log_thread_priority_set="yes"],
[have_qb_log_thread_priority_set="no"])
Expand Down
4 changes: 2 additions & 2 deletions kronosnet.spec.in
Expand Up @@ -49,10 +49,10 @@ URL: https://kronosnet.org
Source0: https://kronosnet.org/releases/%{name}-%{version}%{?numcomm:.%{numcomm}}%{?alphatag:-%{alphatag}}%{?dirty:-%{dirty}}.tar.gz

# Build dependencies
BuildRequires: gcc
BuildRequires: gcc libqb-devel
# required to build man pages
%if %{with buildman}
BuildRequires: libqb-devel libxml2-devel doxygen
BuildRequires: libxml2-devel doxygen
%endif
%if %{with sctp}
BuildRequires: lksctp-tools-devel
Expand Down
2 changes: 1 addition & 1 deletion libknet/Makefile.am
Expand Up @@ -87,7 +87,7 @@ lib_LTLIBRARIES = libknet.la

libknet_la_SOURCES = $(sources)

libknet_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS)
libknet_la_CFLAGS = $(AM_CFLAGS) $(libqb_CFLAGS) $(PTHREAD_CFLAGS)

EXTRA_libknet_la_DEPENDENCIES = $(SYMFILE)

Expand Down
170 changes: 1 addition & 169 deletions libknet/internals.h
Expand Up @@ -16,6 +16,7 @@

#include <pthread.h>
#include <stddef.h>
#include <qb/qblist.h>
#include "libknet.h"
#include "onwire.h"
#include "compat.h"
Expand Down Expand Up @@ -409,173 +410,4 @@ struct pretty_names {
uint8_t val;
};

/**
* This is a kernel style list implementation.
*
* @author Steven Dake <sdake@redhat.com>
*/

struct knet_list_head {
struct knet_list_head *next;
struct knet_list_head *prev;
};

/**
* @def KNET_LIST_DECLARE()
* Declare and initialize a list head.
*/
#define KNET_LIST_DECLARE(name) \
struct knet_list_head name = { &(name), &(name) }

#define KNET_INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)

/**
* Initialize the list entry.
*
* Points next and prev pointers to head.
* @param head pointer to the list head
*/
static inline void knet_list_init(struct knet_list_head *head)
{
head->next = head;
head->prev = head;
}

/**
* Add this element to the list.
*
* @param element the new element to insert.
* @param head pointer to the list head
*/
static inline void knet_list_add(struct knet_list_head *element,
struct knet_list_head *head)
{
head->next->prev = element;
element->next = head->next;
element->prev = head;
head->next = element;
}

/**
* Add to the list (but at the end of the list).
*
* @param element pointer to the element to add
* @param head pointer to the list head
* @see knet_list_add()
*/
static inline void knet_list_add_tail(struct knet_list_head *element,
struct knet_list_head *head)
{
head->prev->next = element;
element->next = head;
element->prev = head->prev;
head->prev = element;
}

/**
* Delete an entry from the list.
*
* @param _remove the list item to remove
*/
static inline void knet_list_del(struct knet_list_head *_remove)
{
_remove->next->prev = _remove->prev;
_remove->prev->next = _remove->next;
}

/**
* Replace old entry by new one
* @param old: the element to be replaced
* @param new: the new element to insert
*/
static inline void knet_list_replace(struct knet_list_head *old,
struct knet_list_head *new)
{
new->next = old->next;
new->next->prev = new;
new->prev = old->prev;
new->prev->next = new;
}

/**
* Tests whether list is the last entry in list head
* @param list: the entry to test
* @param head: the head of the list
* @return boolean true/false
*/
static inline int knet_list_is_last(const struct knet_list_head *list,
const struct knet_list_head *head)
{
return list->next == head;
}

/**
* A quick test to see if the list is empty (pointing to it's self).
* @param head pointer to the list head
* @return boolean true/false
*/
static inline int32_t knet_list_empty(const struct knet_list_head *head)
{
return head->next == head;
}


/**
* Get the struct for this entry
* @param ptr: the &struct list_head pointer.
* @param type: the type of the struct this is embedded in.
* @param member: the name of the list_struct within the struct.
*/
#define knet_list_entry(ptr,type,member) ({ \
((type *)((char*)ptr - offsetof(type, member))); })

/**
* Get the first element from a list
* @param ptr: the &struct list_head pointer.
* @param type: the type of the struct this is embedded in.
* @param member: the name of the list_struct within the struct.
*/
#define knet_list_first_entry(ptr, type, member) \
knet_list_entry((ptr)->next, type, member)

/**
* Iterate over a list
* @param pos: the &struct list_head to use as a loop counter.
* @param head: the head for your list.
*/
#define knet_list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)

/**
* Iterate over a list backwards
* @param pos: the &struct list_head to use as a loop counter.
* @param head: the head for your list.
*/
#define knet_list_for_each_reverse(pos, head) \
for (pos = (head)->prev; pos != (head); pos = pos->prev)

/**
* Iterate over a list safe against removal of list entry
* @param pos: the &struct list_head to use as a loop counter.
* @param n: another &struct list_head to use as temporary storage
* @param head: the head for your list.
*/
#define knet_list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)

/**
* Iterate over list of given type
* @param pos: the type * to use as a loop counter.
* @param head: the head for your list.
* @param member: the name of the list_struct within the struct.
*/
#define knet_list_for_each_entry(pos, head, member) \
for (pos = knet_list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = knet_list_entry(pos->member.next, typeof(*pos), member))


#endif
26 changes: 13 additions & 13 deletions libknet/transport_sctp.c
Expand Up @@ -33,8 +33,8 @@
#include "transport_sctp.h"

typedef struct sctp_handle_info {
struct knet_list_head listen_links_list;
struct knet_list_head connect_links_list;
struct qb_list_head listen_links_list;
struct qb_list_head connect_links_list;
int connect_epollfd;
int connectsockfd[2];
int listen_epollfd;
Expand All @@ -59,7 +59,7 @@ typedef struct sctp_handle_info {
#define MAX_ACCEPTED_SOCKS 256

typedef struct sctp_listen_link_info {
struct knet_list_head list;
struct qb_list_head list;
int listen_sock;
int accepted_socks[MAX_ACCEPTED_SOCKS];
struct sockaddr_storage src_address;
Expand All @@ -75,7 +75,7 @@ typedef struct sctp_accepted_link_info {
} sctp_accepted_link_info_t ;

typedef struct sctp_connect_link_info {
struct knet_list_head list;
struct qb_list_head list;
sctp_listen_link_info_t *listener;
struct knet_link *link;
struct sockaddr_storage dst_address;
Expand Down Expand Up @@ -977,7 +977,7 @@ static sctp_listen_link_info_t *sctp_link_listener_start(knet_handle_t knet_h, s
/*
* Only allocate a new listener if src address is different
*/
knet_list_for_each_entry(info, &handle_info->listen_links_list, list) {
qb_list_for_each_entry(info, &handle_info->listen_links_list, list) {
if (memcmp(&info->src_address, &kn_link->src_addr, sizeof(struct sockaddr_storage)) == 0) {
if ((check_add(knet_h, info->listen_sock, KNET_TRANSPORT_SCTP, -1,
&kn_link->dst_addr, &kn_link->dst_addr, CHECK_TYPE_ADDRESS, CHECK_ACCEPT) < 0) && (errno != EEXIST)) {
Expand Down Expand Up @@ -1059,7 +1059,7 @@ static sctp_listen_link_info_t *sctp_link_listener_start(knet_handle_t knet_h, s
info->on_listener_epoll = 1;

info->listen_sock = listen_sock;
knet_list_add(&info->list, &handle_info->listen_links_list);
qb_list_add(&info->list, &handle_info->listen_links_list);

log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Listening on fd %d for %s:%s", listen_sock, kn_link->status.src_ipaddr, kn_link->status.src_port);

Expand Down Expand Up @@ -1169,7 +1169,7 @@ static int sctp_link_listener_stop(knet_handle_t knet_h, struct knet_link *kn_li
}
}

knet_list_del(&info->list);
qb_list_del(&info->list);
free(info);
this_link_info->listener = NULL;

Expand Down Expand Up @@ -1216,7 +1216,7 @@ int sctp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_li
kn_link->outsock = info->connect_sock;
}

knet_list_add(&info->list, &handle_info->connect_links_list);
qb_list_add(&info->list, &handle_info->connect_links_list);

exit_error:
if (err) {
Expand Down Expand Up @@ -1271,7 +1271,7 @@ int sctp_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_
goto exit_error;
}

knet_list_del(&info->list);
qb_list_del(&info->list);

free(info);
kn_link->transport_link = NULL;
Expand Down Expand Up @@ -1305,10 +1305,10 @@ int sctp_transport_free(knet_handle_t knet_h)
/*
* keep it here while we debug list usage and such
*/
if (!knet_list_empty(&handle_info->listen_links_list)) {
if (!qb_list_empty(&handle_info->listen_links_list)) {
log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Internal error. listen links list is not empty");
}
if (!knet_list_empty(&handle_info->connect_links_list)) {
if (!qb_list_empty(&handle_info->connect_links_list)) {
log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Internal error. connect links list is not empty");
}

Expand Down Expand Up @@ -1441,8 +1441,8 @@ int sctp_transport_init(knet_handle_t knet_h)
goto exit_fail;
}

knet_list_init(&handle_info->listen_links_list);
knet_list_init(&handle_info->connect_links_list);
qb_list_init(&handle_info->listen_links_list);
qb_list_init(&handle_info->connect_links_list);

handle_info->listen_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS + 1);
if (handle_info->listen_epollfd < 0) {
Expand Down
14 changes: 7 additions & 7 deletions libknet/transport_udp.c
Expand Up @@ -33,11 +33,11 @@
#include "threads_common.h"

typedef struct udp_handle_info {
struct knet_list_head links_list;
struct qb_list_head links_list;
} udp_handle_info_t;

typedef struct udp_link_info {
struct knet_list_head list;
struct qb_list_head list;
struct sockaddr_storage local_address;
int socket_fd;
int on_epoll;
Expand All @@ -57,7 +57,7 @@ int udp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_lin
/*
* Only allocate a new link if the local address is different
*/
knet_list_for_each_entry(info, &handle_info->links_list, list) {
qb_list_for_each_entry(info, &handle_info->links_list, list) {
if (memcmp(&info->local_address, &kn_link->src_addr, sizeof(struct sockaddr_storage)) == 0) {
log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Re-using existing UDP socket for new link");
kn_link->outsock = info->socket_fd;
Expand Down Expand Up @@ -152,7 +152,7 @@ int udp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_lin

memmove(&info->local_address, &kn_link->src_addr, sizeof(struct sockaddr_storage));
info->socket_fd = sock;
knet_list_add(&info->list, &handle_info->links_list);
qb_list_add(&info->list, &handle_info->links_list);

kn_link->outsock = sock;
kn_link->transport_link = info;
Expand Down Expand Up @@ -226,7 +226,7 @@ int udp_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_l
}

close(info->socket_fd);
knet_list_del(&info->list);
qb_list_del(&info->list);
free(kn_link->transport_link);

exit_error:
Expand All @@ -248,7 +248,7 @@ int udp_transport_free(knet_handle_t knet_h)
/*
* keep it here while we debug list usage and such
*/
if (!knet_list_empty(&handle_info->links_list)) {
if (!qb_list_empty(&handle_info->links_list)) {
log_err(knet_h, KNET_SUB_TRANSP_UDP, "Internal error. handle list is not empty");
return -1;
}
Expand Down Expand Up @@ -278,7 +278,7 @@ int udp_transport_init(knet_handle_t knet_h)

knet_h->transports[KNET_TRANSPORT_UDP] = handle_info;

knet_list_init(&handle_info->links_list);
qb_list_init(&handle_info->links_list);

return 0;
}
Expand Down

0 comments on commit a33337b

Please sign in to comment.