diff --git a/COPYRIGHT b/COPYRIGHT index 8adbb0b1..2bc38e49 100644 --- a/COPYRIGHT +++ b/COPYRIGHT @@ -1,5 +1,5 @@ -Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. Authors: Fabio M. Di Nitto Christine Caulfield Federico Simoncelli diff --git a/Makefile.am b/Makefile.am index 86d11d9b..42eef557 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,210 +1,210 @@ # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in aclocal.m4 configure depcomp \ config.guess config.sub missing install-sh \ ltmain.sh compile config.h.in config.h.in~ \ autoscan.log configure.scan test-driver \ m4/libtool.m4 m4/lt~obsolete.m4 m4/ltoptions.m4 \ m4/ltsugar.m4 m4/ltversion.m4 include $(top_srcdir)/build-aux/check.mk AUTOMAKE_OPTIONS = foreign ACLOCAL_AMFLAGS = -I m4 SPEC = $(PACKAGE_NAME).spec TARGZFILE = $(PACKAGE_NAME)-$(VERSION).tar.gz EXTRA_DIST = autogen.sh .version \ NOTES_TO_PACKAGE_MAINTAINERS \ $(SPEC).in build-aux SUBDIRS = libnozzle libknet if BUILD_MAN SUBDIRS += man endif dist_doc_DATA = \ COPYING.applications \ COPYING.libraries \ COPYRIGHT \ README.licence \ README all-local: $(SPEC) clean-local: rm -rf $(SPEC) cov* distclean-local: rm -f $(PACKAGE_NAME)-*.tar.* $(PACKAGE_NAME)-*.sha256* tag-* ## make rpm/srpm section. $(SPEC): $(SPEC).in .version config.status rm -f $@-t $@ date="`LC_ALL=C $(UTC_DATE_AT)$(SOURCE_EPOCH) "+%a %b %d %Y"`" && \ gvgver="`cd $(abs_srcdir); build-aux/git-version-gen --fallback $(VERSION) .tarball-version .gitarchivever`" && \ if [ "$$gvgver" = "`echo $$gvgver | sed 's/-/./'`" ];then \ rpmver="$$gvgver" && \ alphatag="" && \ dirty="" && \ numcomm="0"; \ else \ gitver="`echo $$gvgver | sed 's/\(.*\)\./\1-/'`" && \ rpmver=`echo $$gitver | sed 's/-.*//g'` && \ alphatag=`echo $$gvgver | sed 's/[^-]*-\([^-]*\).*/\1/'` && \ numcomm=`echo $$gitver | sed 's/[^-]*-\([^-]*\).*/\1/'` && \ dirty="" && \ if [ "`echo $$gitver | sed 's/^.*-dirty$$//g'`" = "" ];then \ dirty="dirty"; \ fi \ fi && \ if [ -n "$$dirty" ]; then dirty="dirty"; else dirty=""; fi && \ if [ "$$numcomm" = "0" ]; then \ sed \ -e "s#@version@#$$rpmver#g" \ -e "s#%glo.*alpha.*##g" \ -e "s#%glo.*numcomm.*##g" \ -e "s#@dirty@#$$dirty#g" \ -e "s#@date@#$$date#g" \ $(abs_srcdir)/$@.in > $@-t; \ else \ sed \ -e "s#@version@#$$rpmver#g" \ -e "s#@alphatag@#$$alphatag#g" \ -e "s#@numcomm@#$$numcomm#g" \ -e "s#@dirty@#$$dirty#g" \ -e "s#@date@#$$date#g" \ $(abs_srcdir)/$@.in > $@-t; \ fi; \ if [ -z "$$dirty" ]; then sed -i -e "s#%glo.*dirty.*##g" $@-t; fi if BUILD_SCTP sed -i -e "s#@sctp@#bcond_without#g" $@-t else sed -i -e "s#@sctp@#bcond_with#g" $@-t endif if BUILD_CRYPTO_NSS sed -i -e "s#@nss@#bcond_without#g" $@-t else sed -i -e "s#@nss@#bcond_with#g" $@-t endif if BUILD_CRYPTO_OPENSSL sed -i -e "s#@openssl@#bcond_without#g" $@-t else sed -i -e "s#@openssl@#bcond_with#g" $@-t endif if BUILD_CRYPTO_GCRYPT sed -i -e "s#@gcrypt@#bcond_without#g" $@-t else sed -i -e "s#@gcrypt@#bcond_with#g" $@-t endif if BUILD_COMPRESS_ZLIB sed -i -e "s#@zlib@#bcond_without#g" $@-t else sed -i -e "s#@zlib@#bcond_with#g" $@-t endif if BUILD_COMPRESS_LZ4 sed -i -e "s#@lz4@#bcond_without#g" $@-t else sed -i -e "s#@lz4@#bcond_with#g" $@-t endif if BUILD_COMPRESS_LZO2 sed -i -e "s#@lzo2@#bcond_without#g" $@-t else sed -i -e "s#@lzo2@#bcond_with#g" $@-t endif if BUILD_COMPRESS_LZMA sed -i -e "s#@lzma@#bcond_without#g" $@-t else sed -i -e "s#@lzma@#bcond_with#g" $@-t endif if BUILD_COMPRESS_BZIP2 sed -i -e "s#@bzip2@#bcond_without#g" $@-t else sed -i -e "s#@bzip2@#bcond_with#g" $@-t endif if BUILD_COMPRESS_ZSTD sed -i -e "s#@zstd@#bcond_without#g" $@-t else sed -i -e "s#@zstd@#bcond_with#g" $@-t endif if BUILD_LIBNOZZLE sed -i -e "s#@libnozzle@#bcond_without#g" $@-t else sed -i -e "s#@libnozzle@#bcond_with#g" $@-t endif if BUILD_RUNAUTOGEN sed -i -e "s#@runautogen@#bcond_without#g" $@-t else sed -i -e "s#@runautogen@#bcond_with#g" $@-t endif if OVERRIDE_RPM_DEBUGINFO sed -i -e "s#@overriderpmdebuginfo@#bcond_without#g" $@-t else sed -i -e "s#@overriderpmdebuginfo@#bcond_with#g" $@-t endif if BUILD_RPM_DEBUGINFO sed -i -e "s#@rpmdebuginfo@#bcond_without#g" $@-t else sed -i -e "s#@rpmdebuginfo@#bcond_with#g" $@-t endif if BUILD_MAN sed -i -e "s#@buildman@#bcond_without#g" $@-t else sed -i -e "s#@buildman@#bcond_with#g" $@-t endif if INSTALL_TESTS sed -i -e "s#@installtests@#bcond_without#g" $@-t else sed -i -e "s#@installtests@#bcond_with#g" $@-t endif sed -i -e "s#@defaultadmgroup@#$(DEFAULTADMGROUP)#g" $@-t chmod a-w $@-t mv $@-t $@ rm -f $@-t* $(TARGZFILE): $(MAKE) dist RPMBUILDOPTS = --define "_sourcedir $(abs_builddir)" \ --define "_specdir $(abs_builddir)" \ --define "_builddir $(abs_builddir)" \ --define "_srcrpmdir $(abs_builddir)" \ --define "_rpmdir $(abs_builddir)" srpm: clean $(MAKE) $(SPEC) $(TARGZFILE) rpmbuild $(RPMBUILDOPTS) --nodeps -bs $(SPEC) rpm: clean $(MAKE) $(SPEC) $(TARGZFILE) rpmbuild $(RPMBUILDOPTS) -ba $(SPEC) # release/versioning BUILT_SOURCES = .version .version: echo $(VERSION) > $@-t && mv $@-t $@ dist-hook: gen-ChangeLog echo $(VERSION) > $(distdir)/.tarball-version echo $(SOURCE_EPOCH) > $(distdir)/source_epoch gen_start_date = 2000-01-01 .PHONY: gen-ChangeLog gen-ChangeLog: if test -d $(abs_srcdir)/.git; then \ LC_ALL=C $(top_srcdir)/build-aux/gitlog-to-changelog \ --since=$(gen_start_date) > $(distdir)/cl-t; \ rm -f $(distdir)/ChangeLog; \ mv $(distdir)/cl-t $(distdir)/ChangeLog; \ fi diff --git a/NOTES_TO_PACKAGE_MAINTAINERS b/NOTES_TO_PACKAGE_MAINTAINERS index 946d88b7..2f822a44 100644 --- a/NOTES_TO_PACKAGE_MAINTAINERS +++ b/NOTES_TO_PACKAGE_MAINTAINERS @@ -1,35 +1,35 @@ -# Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ To: distribution package maintainers Those are a few things about this project that you should know. I surely welcome patches to support both in a better way. libnozzle is a simple commodity library currently used by corosync, to manage tun/tap devices. libknet is the core of this project. It is considered stable and supported in the stable* branches and still under heavy development in master branch. Upstream does guarantee onwire and update compatibility between releases in the same major versions (aka 1.x will always be able to talk to 1.x+n). There is NO guarantee of onwire compatibility between major versions of knet (aka: 1.x might not be able to talk to 2.x). libknet has a lot of build dependencies due to its modular implementation. It does not, however, link with all those libraries but uses a dlopen model to save runtime resources and provide flexibility to users to install only the libraries they are planning to use. Make sure that you do build with all feature enabled (compatible with your distribution licencing/patent policy of course) and tune your packaging to Recommend/Suggest the external libraries. Thanks Your upstream maintainers diff --git a/README b/README index 25ae6fb9..8fc73436 100644 --- a/README +++ b/README @@ -1,65 +1,65 @@ # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # Upstream resources ------------------ https://github.com/kronosnet/kronosnet/ https://ci.kronosnet.org/ https://projects.clusterlabs.org/project/board/86/ (TODO list and activities tracking) https://drive.google.com/drive/folders/0B_zxAPgZTkM_TklfYzN6a2FYUFE?resourcekey=0-Cfr5D94rZ8LVbeMPGjxbdg&usp=sharing (google shared drive) https://lists.kronosnet.org/mailman3/postorius/lists/users.lists.kronosnet.org/ https://lists.kronosnet.org/mailman3/postorius/lists/devel.lists.kronosnet.org/ https://lists.kronosnet.org/mailman3/postorius/lists/commits.lists.kronosnet.org/ https://kronosnet.org/ (web 0.1 style) IRC: #kronosnet on Libera.Chat Architecture ------------ Please refer to the google shared drive Presentations directory for diagrams and fancy schemas Running knet on FreeBSD ----------------------- knet requires big socket buffers and you need to set: kern.ipc.maxsockbuf=18388608 in /etc/sysctl.conf or knet will fail to run. For version 12 (or lower), knet requires also: net.inet.sctp.blackhole=1 in /etc/sysctl.conf or knet will fail to work with SCTP. This sysctl is obsoleted in version 13. libnozzle requires if_tap.ko loaded in the kernel. Please avoid using ifconfig_DEFAULT in /etc/rc.conf to use DHCP for all interfaces or the dhclient will interfere with libnozzle interface management, causing errors on some operations such as "ifconfig tap down". Rust Bindings ------------- Rust bindings for libknet and libnozzle are part of this source tree, but are included here mainly to keep all of the kronosnet APIs in one place and to ensure that everything is kept up-to-date and properly tested in our CI system. The correct place to get the Rust crates for libknet and libnozzle is still crates.io as it would be for other crates. These will be updated when we issue a new release of knet. https://crates.io/crates/knet-bindings https://crates.io/crates/nozzle-bindings Of course, if you want to try any new features in the APIs that may have not yet been released then you can try these sources, but please keep in touch with us via email or IRC if you do so. diff --git a/autogen.sh b/autogen.sh index 0efd9f1b..e03a1a64 100755 --- a/autogen.sh +++ b/autogen.sh @@ -1,11 +1,11 @@ #!/bin/sh # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # # Run this to generate all the initial makefiles, etc. autoreconf -i -v && echo Now run ./configure and make diff --git a/build-aux/check.mk b/build-aux/check.mk index 9addf1ad..7445ed7d 100644 --- a/build-aux/check.mk +++ b/build-aux/check.mk @@ -1,84 +1,84 @@ # -# Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # VALGRIND = $(VALGRIND_EXEC) -q --error-exitcode=127 --gen-suppressions=all MEMCHECK = $(VALGRIND) --track-fds=yes --leak-check=full --alignment=16 --suppressions=$(abs_top_srcdir)/build-aux/knet_valgrind_memcheck.supp HELGRIND = $(VALGRIND) --tool=helgrind --suppressions=$(abs_top_srcdir)/build-aux/knet_valgrind_helgrind.supp check-memcheck: $(check_PROGRAMS) if HAS_VALGRIND export KNETMEMCHECK=yes && \ $(MAKE) check LOG_COMPILE="libtool --mode=execute $(MEMCHECK)" else @echo valgrind not available on this platform endif check-helgrind: $(check_PROGRAMS) if HAS_VALGRIND export KNETHELGRIND=yes && \ $(MAKE) check LOG_COMPILE="libtool --mode=execute $(HELGRIND)" else @echo valgrind not available on this platform endif check-covscan: if HAS_COVBUILD rm -rf $(abs_top_builddir)/cov* $(MAKE) -C $(abs_top_builddir) clean $(COVBUILD_EXEC) --dir=$(abs_top_builddir)/cov $(MAKE) -C $(abs_top_builddir) if HAS_COVANALYZE if [ -z "$(covoptions)" ]; then \ COVOPTS="--all --disable STACK_USE --disable-parse-warnings";\ else \ COVOPTS="$(covoptions)";\ fi; \ $(COVANALYZE_EXEC) --dir=$(abs_top_builddir)/cov --wait-for-license $$COVOPTS if HAS_COVFORMATERRORS $(COVFORMATERRORS_EXEC) --dir=$(abs_top_builddir)/cov --emacs-style > $(abs_top_builddir)/cov.output.txt $(COVFORMATERRORS_EXEC) --dir=$(abs_top_builddir)/cov --html-output $(abs_top_builddir)/cov.html endif else @echo directory $(abs_top_builddir)/cov ready to be uploaded to https://scan.coverity.com endif else @echo cov-build not available on this platform endif check-annocheck-libs: if HAS_ANNOCHECK @echo Running annocheck libs test TESTLIBS="$(shell find .libs/ -type f -name "*.so.*")"; \ if ! $(ANNOCHECK_EXEC) --skip-lto --quiet $$TESTLIBS; then \ $(ANNOCHECK_EXEC) --skip-lto --verbose $$TESTLIBS; \ echo annocheck libs test: FAILED; \ exit 1; \ else \ echo annocheck libs test: PASS; \ fi else @echo Annocheck build or binary not available endif # we cannot check run-path because CI builds with specific prefix/user_prefix # and the only binaries affected are the test suite. check-annocheck-bins: if HAS_ANNOCHECK @echo Running annocheck binaries test TESTBINS="$(shell find .libs/ -type f)"; \ if ! $(ANNOCHECK_EXEC) --skip-run-path --skip-lto --quiet $$TESTBINS; then \ $(ANNOCHECK_EXEC) --skip-run-path --skip-lto --verbose $$TESTBINS; \ echo annocheck binaries test: FAILED; \ exit 1; \ else \ echo annocheck binaries test: PASS; \ fi else @echo Annocheck build or binary not available endif diff --git a/build-aux/git-version-gen b/build-aux/git-version-gen index 94e78bc4..d19f5d4e 100755 --- a/build-aux/git-version-gen +++ b/build-aux/git-version-gen @@ -1,265 +1,265 @@ #!/bin/sh # Print a version string. scriptversion=2018-08-31.20; # UTC -# Copyright (C) 2012-2022 Red Hat, Inc. +# Copyright (C) 2012-2023 Red Hat, Inc. # Copyright (C) 2007-2016 Free Software Foundation, Inc. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # This script is derived from GIT-VERSION-GEN from GIT: http://git.or.cz/. # It may be run two ways: # - from a git repository in which the "git describe" command below # produces useful output (thus requiring at least one signed tag) # - from a non-git-repo directory containing a .tarball-version file, which # presumes this script is invoked like "./git-version-gen .tarball-version". # In order to use intra-version strings in your project, you will need two # separate generated version string files: # # .tarball-version - present only in a distribution tarball, and not in # a checked-out repository. Created with contents that were learned at # the last time autoconf was run, and used by git-version-gen. Must not # be present in either $(srcdir) or $(builddir) for git-version-gen to # give accurate answers during normal development with a checked out tree, # but must be present in a tarball when there is no version control system. # Therefore, it cannot be used in any dependencies. GNUmakefile has # hooks to force a reconfigure at distribution time to get the value # correct, without penalizing normal development with extra reconfigures. # # .version - present in a checked-out repository and in a distribution # tarball. Usable in dependencies, particularly for files that don't # want to depend on config.h but do want to track version changes. # Delete this file prior to any autoconf run where you want to rebuild # files to pick up a version string change; and leave it stale to # minimize rebuild time after unrelated changes to configure sources. # # As with any generated file in a VC'd directory, you should add # /.version to .gitignore, so that you don't accidentally commit it. # .tarball-version is never generated in a VC'd directory, so needn't # be listed there. # # In order to use git archive versions another two files has to be presented: # # .gitarchive-version - present in checked-out repository and git # archive tarball, but not in the distribution tarball. Used as a last # option for version. File must contain special string $Format:%d$, # which is substitued by git on archive operation. # # .gitattributes - present in checked-out repository and git archive # tarball, but not in the distribution tarball. Must set export-subst # attribute for .gitarchive-version file. # # Use the following line in your configure.ac, so that $(VERSION) will # automatically be up-to-date each time configure is run (and note that # since configure.ac no longer includes a version string, Makefile rules # should not depend on configure.ac for version updates). # # AC_INIT([GNU project], # m4_esyscmd([build-aux/git-version-gen .tarball-version]), # [bug-project@example]) # # Then use the following lines in your Makefile.am, so that .version # will be present for dependencies, and so that .version and # .tarball-version will exist in distribution tarballs. # # EXTRA_DIST = $(top_srcdir)/.version # BUILT_SOURCES = $(top_srcdir)/.version # $(top_srcdir)/.version: # echo $(VERSION) > $@-t && mv $@-t $@ # dist-hook: # echo $(VERSION) > $(distdir)/.tarball-version me=$0 version="git-version-gen $scriptversion Copyright 2011 Free Software Foundation, Inc. There is NO warranty. You may redistribute this software under the terms of the GNU General Public License. For more information about these matters, see the files named COPYING." usage="\ Usage: $me [OPTION]... \$srcdir/.tarball-version [\$srcdir/.gitarchive-version] [TAG-NORMALIZATION-SED-SCRIPT] Print a version string. Options: --prefix PREFIX prefix of git tags (default 'v') --fallback VERSION fallback version to use if \"git --version\" fails --help display this help and exit --version output version information and exit Running without arguments will suffice in most cases." prefix=v fallback= while test $# -gt 0; do case $1 in --help) echo "$usage"; exit 0;; --version) echo "$version"; exit 0;; --prefix) shift; prefix="$1";; --fallback) shift; fallback="$1";; -*) echo "$0: Unknown option '$1'." >&2 echo "$0: Try '--help' for more information." >&2 exit 1;; *) if test "x$tarball_version_file" = x; then tarball_version_file="$1" elif test "x$gitarchive_version_file" = x; then gitarchive_version_file="$1" elif test "x$tag_sed_script" = x; then tag_sed_script="$1" else echo "$0: extra non-option argument '$1'." >&2 exit 1 fi;; esac shift done if test "x$tarball_version_file" = x; then echo "$usage" exit 1 fi tag_sed_script="${tag_sed_script:-s/x/x/}" nl=' ' # Avoid meddling by environment variable of the same name. v= v_from_git= # First see if there is a tarball-only version file. # then try "git describe", then default. if test -f $tarball_version_file then v=`cat $tarball_version_file` || v= case $v in *$nl*) v= ;; # reject multi-line output [0-9]*) ;; *) v= ;; esac test "x$v" = x \ && echo "$0: WARNING: $tarball_version_file is missing or damaged" 1>&2 fi if test "x$v" != x then : # use $v # Otherwise, if there is at least one git commit involving the working # directory, and "git describe" output looks sensible, use that to # derive a version string. elif test "`git log -1 --pretty=format:x . 2>&1`" = x \ && v=`git describe --abbrev=4 --match="$prefix*" HEAD 2>/dev/null \ || git describe --abbrev=4 HEAD 2>/dev/null` \ && v=`printf '%s\n' "$v" | sed "$tag_sed_script"` \ && case $v in $prefix[0-9]*) ;; *) (exit 1) ;; esac then # Is this a new git that lists number of commits since the last # tag or the previous older version that did not? # Newer: v6.10-77-g0f8faeb # Older: v6.10-g0f8faeb case $v in *-*-*) : git describe is okay three part flavor ;; *-*) : git describe is older two part flavor # Recreate the number of commits and rewrite such that the # result is the same as if we were using the newer version # of git describe. vtag=`echo "$v" | sed 's/-.*//'` commit_list=`git rev-list "$vtag"..HEAD 2>/dev/null` \ || { commit_list=failed; echo "$0: WARNING: git rev-list failed" 1>&2; } numcommits=`echo "$commit_list" | wc -l` v=`echo "$v" | sed "s/\(.*\)-\(.*\)/\1-$numcommits-\2/"`; test "$commit_list" = failed && v=UNKNOWN ;; esac # Change the first '-' to a '.', so version-comparing tools work properly. # Remove the "g" in git describe's output string, to save a byte. v=`echo "$v" | sed 's/-/./;s/\(.*\)-g/\1-/'`; v_from_git=1 elif test "x$fallback" = x || git --version >/dev/null 2>&1; then if test -f $gitarchive_version_file then v=`sed "s/^.*tag: \($prefix[0-9)][^,)]*\).*\$/\1/" $gitarchive_version_file \ | sed "$tag_sed_script"` || exit 1 case $v in *$nl*) v= ;; # reject multi-line output $prefix[0-9]*) ;; *) v= ;; esac test -z "$v" \ && echo "$0: WARNING: $gitarchive_version_file doesn't contain valid version tag" 1>&2 \ && v=UNKNOWN elif test "x$fallback" = x; then v=UNKNOWN else v=$fallback fi else v=$fallback fi if test "x$fallback" = x -a "$v" = "UNKNOWN" then echo "$0: ERROR: Can't find valid version. Please use valid git repository," \ "released tarball or version tagged archive" 1>&2 exit 1 fi v=`echo "$v" |sed "s/^$prefix//"` # Test whether to append the "-dirty" suffix only if the version # string we're using came from git. I.e., skip the test if it's "UNKNOWN" # or if it came from .tarball-version. if test "x$v_from_git" != x; then # Don't declare a version "dirty" merely because a time stamp has changed. git update-index --refresh > /dev/null 2>&1 dirty=`exec 2>/dev/null;git diff-index --name-only HEAD` || dirty= case "$dirty" in '') ;; *) # Append the suffix only if there isn't one already. case $v in *-dirty) ;; *) v="$v-dirty" ;; esac ;; esac fi # Omit the trailing newline, so that m4_esyscmd can use the result directly. printf %s "$v" # Local variables: # eval: (add-hook 'write-file-hooks 'time-stamp) # time-stamp-start: "scriptversion=" # time-stamp-format: "%:y-%02m-%02d.%02H" # time-stamp-time-zone: "UTC0" # time-stamp-end: "; # UTC" # End: diff --git a/build-aux/knet_valgrind_helgrind.supp b/build-aux/knet_valgrind_helgrind.supp index 2336a316..318dca8f 100644 --- a/build-aux/knet_valgrind_helgrind.supp +++ b/build-aux/knet_valgrind_helgrind.supp @@ -1,49 +1,49 @@ -# Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ { link enable/disable known race (safe to ignore) Helgrind:Race fun:_link_updown fun:knet_link_set_enable fun:test fun:main } { link enable/disable known race (safe to ignore) Helgrind:Race fun:_handle_heartbt_thread obj:/usr/lib64/valgrind/vgpreload_helgrind-amd64-linux.so fun:start_thread fun:clone } { helgrind glitch in parsing the heartbeat code Helgrind:Race fun:_handle_check_each fun:_handle_heartbt_thread obj:/usr/lib64/valgrind/vgpreload_helgrind-amd64-linux.so fun:start_thread fun:clone } { helgrind glitch in parsing the recv from links code Helgrind:Race fun:_parse_recv_from_links fun:_handle_recv_from_links fun:_handle_recv_from_links_thread obj:/usr/lib64/valgrind/vgpreload_helgrind-amd64-linux.so fun:start_thread fun:clone } { helgrind glitch in parsing the PMTUd code Helgrind:Race fun:_handle_pmtud_link_thread obj:/usr/lib64/valgrind/vgpreload_helgrind-amd64-linux.so fun:start_thread fun:clone } diff --git a/build-aux/knet_valgrind_memcheck.supp b/build-aux/knet_valgrind_memcheck.supp index 825ba0ca..45c30829 100644 --- a/build-aux/knet_valgrind_memcheck.supp +++ b/build-aux/knet_valgrind_memcheck.supp @@ -1,839 +1,839 @@ -# Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ { lzma internals (spotted on Debian 9 and Ubuntu 18.04 LTS x86-64) Memcheck:Cond obj:/lib/x86_64-linux-gnu/liblzma.so.5.2.2 obj:/lib/x86_64-linux-gnu/liblzma.so.5.2.2 obj:/lib/x86_64-linux-gnu/liblzma.so.5.2.2 obj:/lib/x86_64-linux-gnu/liblzma.so.5.2.2 fun:lzma_block_buffer_encode fun:lzma_stream_buffer_encode fun:lzma_easy_buffer_encode fun:lzma_compress fun:compress_lib_test fun:compress_cfg fun:knet_handle_compress fun:test } { lzma internals (spotted on Ubuntu 18.04 LTS i386) Memcheck:Cond obj:/lib/i386-linux-gnu/liblzma.so.5.2.2 obj:/lib/i386-linux-gnu/liblzma.so.5.2.2 obj:/lib/i386-linux-gnu/liblzma.so.5.2.2 obj:/lib/i386-linux-gnu/liblzma.so.5.2.2 obj:/lib/i386-linux-gnu/liblzma.so.5.2.2 obj:/lib/i386-linux-gnu/liblzma.so.5.2.2 fun:lzma_stream_buffer_encode fun:lzma_easy_buffer_encode fun:lzma_compress fun:compress_lib_test fun:compress_cfg fun:knet_handle_compress } { openssl internals (spotted on OpenSUSE 15) Memcheck:Cond fun:__memcmp_sse4_1 obj:/usr/lib64/libcrypto.so.1.1 fun:FIPS_selftest obj:/usr/lib64/libcrypto.so.1.1 fun:FIPS_mode_set obj:/usr/lib64/libcrypto.so.1.1 fun:call_init.part.0 fun:_dl_init fun:dl_open_worker fun:_dl_catch_error fun:_dl_open fun:dlopen_doit } { openssl internals (spotted on OpenSUSE Tumbleweed) Memcheck:Cond obj:/usr/lib64/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib64/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib64/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib64/libcrypto.so.1.1 fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread } { openssl internals (spotted on Ubuntu Devel x86-64 - 2019-10-30) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread } { openssl internals (spotted on OpenSUSE Tumbleweed) Memcheck:Cond obj:/usr/lib64/libcrypto.so.1.1 obj:/usr/lib64/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib64/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib64/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib64/libcrypto.so.1.1 fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread } { openssl internals (spotted on Ubuntu Devel x86-64 - 2019-10-30) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread } { openssl internals (spotted on OpenSUSE Tumbleweed) Memcheck:Cond obj:/usr/lib64/libcrypto.so.1.1 obj:/usr/lib64/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { openssl internals (spotted on Ubuntu Devel x86-64 - 2019-10-30) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { openssl internals (spotted on OpenSUSE Tumbleweed) Memcheck:Cond obj:/usr/lib64/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { openssl internals (spotted on Ubuntu Devel x86-64 - 2019-10-30) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { openssl internals (spotted on OpenSUSE 15) Memcheck:Cond obj:/usr/lib64/libcrypto.so.1.1 fun:FIPS_mode_set obj:/usr/lib64/libcrypto.so.1.1 fun:call_init.part.0 fun:_dl_init fun:dl_open_worker fun:_dl_catch_error fun:_dl_open fun:dlopen_doit fun:_dl_catch_error fun:_dlerror_run fun:dlopen@@GLIBC_2.2.5 } { openssl uninitialised byte(s) (spotted on OpenSUSE Tumbleweed and Ubuntu Devel x86-64 - 2019-10-30) Memcheck:Param socketcall.sendto(msg) fun:sendto fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { openssl uninitialised byte(s) (spotted on OpenSUSE Tumbleweed and Ubuntu Devel x86-64 - 2019-10-30) Memcheck:Param socketcall.sendto(msg) fun:sendto fun:_parse_recv_from_links fun:_handle_recv_from_links fun:_handle_recv_from_links_thread fun:start_thread fun:clone } { openssl uninitialised byte(s) (spotted on OpenSUSE Tumbleweed and Ubuntu Devel x86-64 - 2019-10-30) Memcheck:Param socketcall.sendto(msg) fun:sendto fun:_handle_check_link_pmtud fun:_handle_check_pmtud fun:_handle_pmtud_link_thread fun:start_thread fun:clone } { openssl uninitialised byte(s) (spotted on OpenSUSE Tumbleweed) Memcheck:Param sendmsg(msg.msg_iov[0]) fun:sendmsg fun:_sendmmsg fun:_dispatch_to_links fun:_parse_recv_from_sock fun:_handle_send_to_links fun:_handle_send_to_links_thread fun:start_thread fun:clone } { openssl internals (spotted on Ubuntu Devel x86-64 - 2019-10-30) Memcheck:Param sendmsg(msg.msg_iov[0]) fun:__libc_sendmsg fun:sendmsg fun:_sendmmsg fun:_dispatch_to_links fun:_parse_recv_from_sock fun:_handle_send_to_links fun:_handle_send_to_links_thread fun:start_thread fun:clone } { openssl internals (spotted on Ubuntu Devel x86-64 - 2020-07-10) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { openssl internals (spotted on Ubuntu Devel x86-64 - 2020-07-10) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:encrypt_openssl fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread } { openssl internals (spotted on Ubuntu Devel x86-64 - 2020-07-10) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:encrypt_openssl fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread } { openssl internals (spotted on Ubuntu Devel x86-64 - 2020-07-10) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:_handle_check_each fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { nss internal leak (3.41) non recurring (spotted on f29) Memcheck:Leak match-leak-kinds: definite fun:malloc obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:/usr/lib64/libnss3.so } { nss internal leak (3.41) non recurring Memcheck:Leak match-leak-kinds: definite fun:calloc obj:* obj:* obj:* obj:* obj:* fun:init_nss fun:nsscrypto_init fun:crypto_init fun:knet_handle_crypto_set_config fun:test fun:main } { nss internal leak (3.41) non recurring Memcheck:Leak match-leak-kinds: definite fun:malloc obj:* obj:* obj:* obj:* obj:* fun:init_nss fun:nsscrypto_init fun:crypto_init fun:knet_handle_crypto_set_config fun:test fun:main } { nss internal leak (3.55) non recurring (spotted on f34) Memcheck:Leak match-leak-kinds: definite fun:malloc fun:realpath@@GLIBC_2.3 obj:* obj:* obj:* obj:/usr/lib64/libnss3.so fun:SECMOD_LoadModule fun:SECMOD_LoadModule obj:/usr/lib64/libnss3.so fun:NSS_NoDB_Init fun:init_nss fun:nsscrypto_init fun:crypto_init fun:_knet_handle_crypto_set_config } { openssl uncoditional jump (spotted on Ubuntu devel 10092020) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread } { openssl uncoditional jump (spotted on Ubuntu devel 10092020) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread } { openssl uncoditional jump (spotted on Ubuntu devel 10092020) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { openssl uncoditional jump (spotted on Ubuntu devel 10092020) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl.isra.0 fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { ubuntu-devel new toolchain is not stable yet (spotted on Ubuntu devel 10092020) Memcheck:Param socketcall.sendto(msg) fun:sendto fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { ubuntu-devel new toolchain is not stable yet (spotted on Ubuntu devel 10092020) Memcheck:Param socketcall.sendto(msg) fun:sendto fun:send_pong fun:process_ping fun:_parse_recv_from_links fun:_handle_recv_from_links fun:_handle_recv_from_links_thread fun:start_thread fun:clone } { ubuntu-devel new toolchain is not stable yet (spotted on Ubuntu devel 10092020) Memcheck:Param socketcall.sendto(msg) fun:sendto fun:send_pmtud_reply fun:process_pmtud fun:_parse_recv_from_links fun:_handle_recv_from_links fun:_handle_recv_from_links_thread fun:start_thread fun:clone } { ubuntu-devel new toolchain is not stable yet (spotted on Ubuntu devel 10092020) Memcheck:Param sendmsg(msg.msg_iov[0]) fun:__libc_sendmsg fun:sendmsg fun:_sendmmsg fun:_dispatch_to_links fun:_prep_and_send_msgs fun:_parse_recv_from_sock fun:_handle_send_to_links fun:_handle_send_to_links_thread fun:start_thread fun:clone } { openssl uncoditional jump (clang) (spotted on Ubuntu devel 10092020) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:encrypt_openssl fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread } { openssl uncoditional jump (clang) (spotted on Ubuntu devel 10092020) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_instantiate obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_get0_public obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:encrypt_openssl fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread } { openssl uncoditional jump (clang) (spotted on Ubuntu devel 10092020) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { openssl uncoditional jump (clang) (spotted on Ubuntu devel 10092020) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1 fun:RAND_DRBG_generate fun:RAND_DRBG_bytes fun:encrypt_openssl fun:opensslcrypto_encrypt_and_signv fun:opensslcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { nss internal leak (3.59) non recurring (spotted on f34) Memcheck:Leak match-leak-kinds: definite fun:malloc fun:realpath@@GLIBC_2.3 obj:* obj:* obj:* obj:* obj:/usr/lib64/libnss3.so fun:SECMOD_LoadModule fun:SECMOD_LoadModule obj:/usr/lib64/libnss3.so fun:NSS_NoDB_Init fun:init_nss fun:nsscrypto_init } { nss internal leak (3.59) non recurring (spotted on f34) Memcheck:Leak match-leak-kinds: definite fun:malloc fun:realpath@@GLIBC_2.3 obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* fun:init_nss fun:nsscrypto_init } { nss internal leak non recurring (spotted on rhel8.4 and clang) Memcheck:Leak match-leak-kinds: possible fun:malloc fun:realpath@@GLIBC_2.3 ... obj:/usr/lib64/libnss3.so fun:SECMOD_LoadModule fun:SECMOD_LoadModule obj:/usr/lib64/libnss3.so } { libgcrypt internal conditinal (debian 10) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 fun:encrypt_gcrypt.isra.0 fun:gcryptcrypto_encrypt_and_signv fun:gcryptcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { libgcrypt internal conditinal (debian 10 - clang) Memcheck:Cond obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 obj:/usr/lib/x86_64-linux-gnu/libgcrypt.so.20.2.4 fun:encrypt_gcrypt fun:gcryptcrypto_encrypt_and_signv fun:gcryptcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { libgcrypt internal conditinal (opensuse 15) Memcheck:Cond obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 fun:encrypt_gcrypt.isra.0 fun:gcryptcrypto_encrypt_and_signv fun:gcryptcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { libgcrypt internal conditinal (opensuse 15 - clang) Memcheck:Cond obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 obj:/usr/lib64/libgcrypt.so.20.2.2 fun:encrypt_gcrypt fun:gcryptcrypto_encrypt_and_signv fun:gcryptcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { libgcrypt internal leak (fedora and others) Memcheck:Leak match-leak-kinds: definite fun:malloc ... fun:gcryptcrypto_init fun:crypto_init fun:knet_handle_crypto_set_config fun:test fun:main } { libgcrypt internal leak (fedora and others) Memcheck:Leak match-leak-kinds: definite fun:malloc ... fun:encrypt_gcrypt fun:gcryptcrypto_encrypt_and_signv fun:gcryptcrypto_encrypt_and_sign fun:send_ping fun:_send_pings fun:_handle_heartbt_thread fun:start_thread fun:clone } { glibc2.34 dlopen internal memory leak Memcheck:Leak match-leak-kinds: definite fun:malloc ... fun:_dl_catch_exception fun:_dl_catch_error fun:_dlerror_run fun:dlopen_implementation fun:dlopen@@GLIBC_2.34 } { glibc2.34 dlopen internal memory leak Memcheck:Leak match-leak-kinds: definite fun:malloc fun:_dl_find_object_update ... fun:_dl_catch_error fun:_dlerror_run fun:dlopen@@GLIBC_2.34 } { glibc2.34 dlopen internal memory leak Memcheck:Leak match-leak-kinds: definite fun:malloc ... fun:_rtld_catch_error fun:_dlerror_run } { glibc2.34 Memcheck:Cond fun:strcmp ... fun:_rtld_catch_error fun:_dlerror_run } { For knet-test Rust API checker Memcheck:Leak match-leak-kinds: possible fun:malloc ... fun:_ZN* } { For knet-test Rust API checker Memcheck:Leak match-leak-kinds: possible fun:calloc fun:UnknownInlinedFun fun:allocate_dtv fun:_dl_allocate_tls fun:pthread_create ... fun:_ZN* } { For knet-test Rust API checker Memcheck:Leak match-leak-kinds: possible fun:calloc fun:UnknownInlinedFun fun:allocate_dtv fun:_dl_allocate_tls fun:pthread_create ... fun:_ZN* fun:main } { For knet-test Rust API checker Memcheck:Leak match-leak-kinds: possible fun:malloc ... fun:_ZN* fun:main } { For knet-test Rust API checker Memcheck:Cond ... fun:_ZN* fun:main } { powerpc specific leak (in crypto code) Memcheck:Leak match-leak-kinds: definite fun:malloc obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* obj:* } diff --git a/build-aux/release.mk b/build-aux/release.mk index 3bb911f3..97d91e19 100644 --- a/build-aux/release.mk +++ b/build-aux/release.mk @@ -1,112 +1,112 @@ # -# Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # # to build official release tarballs, handle tagging and publish. # example: # make -f build-aux/release.mk all version=0.9 release=yes publish gpgsignkey = 1F22889A project = kronosnet deliverables = $(project)-$(version).sha256 \ $(project)-$(version).tar.bz2 \ $(project)-$(version).tar.gz \ $(project)-$(version).tar.xz .PHONY: all all: tag tarballs sign # first/last skipped per release/gpgsignkey respectively .PHONY: checks checks: ifeq (,$(version)) @echo ERROR: need to define version= @exit 1 endif @if [ ! -d .git ]; then \ echo This script needs to be executed from top level cluster git tree; \ exit 1; \ fi .PHONY: setup setup: checks ./autogen.sh ./configure --enable-rust-bindings make maintainer-clean .PHONY: tag tag: setup ./tag-$(version) tag-$(version): ifeq (,$(release)) @echo Building test release $(version), no tagging echo '$(version)' > .tarball-version else # following will be captured by git-version-gen automatically git tag -a -m "v$(version) release" v$(version) HEAD @touch $@ endif .PHONY: tarballs tarballs: tag ./autogen.sh ./configure --enable-rust-bindings make distcheck DISTCHECK_CONFIGURE_FLAGS="--enable-rust-bindings" make all -j .PHONY: sha256 sha256: $(project)-$(version).sha256 # NOTE: dependency backtrack may fail trying to sign missing tarballs otherwise # (actually, only when signing tarballs directly, but doesn't hurt anyway) $(deliverables): tarballs $(project)-$(version).sha256: # checksum anything from deliverables except for in-prep checksums file sha256sum $(deliverables:$@=) | sort -k2 > $@ .PHONY: sign ifeq (,$(gpgsignkey)) sign: $(deliverables) @echo No GPG signing key defined else sign: $(deliverables:=.asc) endif # NOTE: cannot sign multiple files at once $(project)-$(version).%.asc: $(project)-$(version).% gpg --default-key "$(gpgsignkey)" \ --detach-sign \ --armor \ $< .PHONY: publish publish: ifeq (,$(release)) @echo Building test release $(version), no publishing! else @echo : pushing tags @git push --follow-tags origin @echo : publishing files @scp $(deliverables) $(deliverables:=.asc) www.kronosnet.org:kronosnet/releases/. $(MAKE) -C libnozzle/bindings/rust crates-publish $(MAKE) -C libknet/bindings/rust crates-publish endif .PHONY: clean clean: rm -rf $(project)-* tag-* .tarball-version diff --git a/build-aux/rust-regen.sh b/build-aux/rust-regen.sh index 01b1efa1..fc97fec4 100755 --- a/build-aux/rust-regen.sh +++ b/build-aux/rust-regen.sh @@ -1,29 +1,29 @@ #!/bin/sh # -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Authors: Christine Caulfield # Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # # # Regerate the FFI bindings in src/sys from the current headers # srcheader="$1" dstrs="$2" filter="$3" bindgen \ --size_t-is-usize \ --no-recursive-allowlist \ --no-prepend-enum-name \ --no-layout-tests \ --no-doc-comments \ --generate functions,types,vars \ --fit-macro-constant-types \ --allowlist-var=$3.* \ --allowlist-type=.* \ --allowlist-function=.* \ $srcheader -o $dstrs diff --git a/build-aux/rust.mk b/build-aux/rust.mk index e9f26223..cf8e863d 100644 --- a/build-aux/rust.mk +++ b/build-aux/rust.mk @@ -1,106 +1,106 @@ # -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # RUST_COMMON = \ build.rs.in RUST_SRCS = $(RUST_SHIP_SRCS) $(RUST_BUILT_SRCS) %.rlib: $(RUST_SRCS) Cargo.toml build.rs PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(CARGO) build $(RUST_FLAGS) %-test: $(RUST_SRCS) Cargo.toml build.rs PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(CARGO) build $(RUST_FLAGS) build.rs: build.rs.in rm -f $@ $@-t cat $^ | sed \ -e 's#@ABSTOPLEVELSRC@#$(abs_top_srcdir)#g' \ -e 's#@ABSTOPLEVELBUILD@#$(abs_top_builddir)#g' \ > $@-t chmod a-w $@-t mv $@-t $@ rm -f $@-t cargo-tree-prep: if [ "${abs_builddir}" != "${abs_srcdir}" ]; then \ echo "Generating builddir out-of-tree rust symlinks"; \ src_realpath=$(shell realpath ${abs_srcdir}); \ for i in `find "$$src_realpath/" -type d | \ grep -v "${abs_builddir}" | \ sed -e 's#^'$$src_realpath'/##g'`; do \ $(MKDIR_P) ${abs_builddir}/$${i}; \ done; \ find "$$src_realpath/" -type f | { while read src; do \ process=no; \ copy=no; \ case $$src in \ ${abs_builddir}*) \ ;; \ *Makefile.*|*.in) \ ;; \ *) \ process=yes; \ ;; \ esac ; \ dst=`echo $$src | sed -e 's#^'$$src_realpath'/##g'`; \ if [ $${process} == yes ]; then \ rm -f ${abs_builddir}/$$dst; \ $(LN_S) $$src ${abs_builddir}/$$dst; \ fi; \ if [ $${copy} == yes ]; then \ rm -f ${abs_builddir}/$$dst; \ cp $$src ${abs_builddir}/$$dst; \ chmod u+w ${abs_builddir}/$$dst; \ fi; \ done; }; \ fi cargo-clean: -$(CARGO) clean rm -rf Cargo.lock $(RUST_BUILT_SRCS) build.rs target/ if [ "${abs_builddir}" != "${abs_srcdir}" ]; then \ echo "Cleaning out-of-tree rust symlinks" ; \ find "${abs_builddir}/" -type l -delete; \ find "${abs_builddir}/" -type d -empty -delete; \ fi clippy-check: $(CARGO) clippy --verbose --all-features -- -D warnings doc-check: $(CARGO) doc --verbose --all-features publish-check: if [ -f "${abs_srcdir}/README" ]; then \ $(CARGO) publish --dry-run; \ fi crates-publish: if [ -f "${abs_srcdir}/README" ]; then \ bindingname=`cat Cargo.toml | grep ^name | sed -e 's#.*= ##g' -e 's#"##g'` && \ cratesver=`cargo search $$bindingname | grep "^$$bindingname " | sed -e 's#.*= ##g' -e 's#"##g' -e 's/\+.*//g'` && \ testver=`echo $(localver) | sed -e 's/\+.*//g'` && \ if [ "$$cratesver" != "$$testver" ]; then \ $(CARGO) publish; \ fi; \ fi crates-check: if [ -f "${abs_srcdir}/README" ]; then \ bindingname=`cat Cargo.toml | grep ^name | sed -e 's#.*= ##g' -e 's#"##g'` && \ cratesver=`cargo search $$bindingname | grep "^$$bindingname " | sed -e 's#.*= ##g' -e 's#"##g' -e 's/\+.*//g'` && \ testver=`echo $(localver) | sed -e 's/\+.*//g'` && \ if [ "$$cratesver" != "$$testver" ]; then \ echo "!!!!! WARNING !!!!!"; \ echo "!!!!! WARNING: $$bindingname local version ($$testver) is higher than the current published one on crates.io ($$cratesver)"; \ echo "!!!!! WARNING !!!!!"; \ fi; \ fi check-local: clippy-check doc-check crates-check publish-check diff --git a/configure.ac b/configure.ac index 252a8bf6..f8a1a0a3 100644 --- a/configure.ac +++ b/configure.ac @@ -1,581 +1,581 @@ # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # Federico Simoncelli # # This software licensed under GPL-2.0+ # # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. # AC_PREREQ([2.63]) AC_INIT([kronosnet], m4_esyscmd([build-aux/git-version-gen .tarball-version .gitarchivever]), [devel@lists.kronosnet.org]) # Don't let AC_PROC_CC (invoked by AC_USE_SYSTEM_EXTENSIONS) replace # undefined CFLAGS with -g -O2, overriding our special OPT_CFLAGS. : ${CFLAGS=""} AC_USE_SYSTEM_EXTENSIONS AM_INIT_AUTOMAKE([1.13 dist-bzip2 dist-xz color-tests -Wno-portability subdir-objects]) LT_PREREQ([2.2.6]) # --enable-new-dtags: Use RUNPATH instead of RPATH. # It is necessary to have this done before libtool does linker detection. # See also: https://github.com/kronosnet/kronosnet/issues/107 # --as-needed: Modern systems have builtin ceil() making -lm superfluous but # AC_SEARCH_LIBS can't detect this because it tests with a false prototype AX_CHECK_LINK_FLAG([-Wl,--enable-new-dtags], [AM_LDFLAGS=-Wl,--enable-new-dtags], [AC_MSG_ERROR(["Linker support for --enable-new-dtags is required"])]) AX_CHECK_LINK_FLAG([-Wl,--as-needed], [AM_LDFLAGS="$AM_LDFLAGS -Wl,--as-needed"]) saved_LDFLAGS="$LDFLAGS" LDFLAGS="$AM_LDFLAGS $LDFLAGS" LT_INIT LDFLAGS="$saved_LDFLAGS" AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_SRCDIR([libknet/handle.c]) AC_CONFIG_HEADERS([config.h]) AC_CANONICAL_HOST AC_LANG([C]) if test "$prefix" = "NONE"; then prefix="/usr" if test "$localstatedir" = "\${prefix}/var"; then localstatedir="/var" fi if test "$libdir" = "\${exec_prefix}/lib"; then if test -e /usr/lib64; then libdir="/usr/lib64" else libdir="/usr/lib" fi fi fi AC_PROG_AWK AC_PROG_GREP AC_PROG_SED AC_PROG_CPP AC_PROG_CC m4_version_prereq([2.70], [:], [AC_PROG_CC_C99]) if test "x$ac_cv_prog_cc_c99" = "xno"; then AC_MSG_ERROR(["C99 support is required"]) fi AC_PROG_LN_S AC_PROG_INSTALL AC_PROG_MAKE_SET PKG_PROG_PKG_CONFIG AC_CHECK_PROGS([VALGRIND_EXEC], [valgrind]) AM_CONDITIONAL([HAS_VALGRIND], [test x$VALGRIND_EXEC != "x"]) AC_CHECK_PROGS([COVBUILD_EXEC], [cov-build]) AM_CONDITIONAL([HAS_COVBUILD], [test x$COVBUILD_EXEC != "x"]) AC_CHECK_PROGS([COVANALYZE_EXEC], [cov-analyze]) AM_CONDITIONAL([HAS_COVANALYZE], [test x$COVANALYZE_EXEC != "x"]) AC_CHECK_PROGS([COVFORMATERRORS_EXEC], [cov-format-errors]) AM_CONDITIONAL([HAS_COVFORMATERRORS], [test x$COVFORMATERRORS_EXEC != "x"]) # KNET_OPTION_DEFINES(stem,type,detection code) # stem: enters name of option, Automake conditional and preprocessor define # type: compress or crypto, determines where the default comes from AC_DEFUN([KNET_OPTION_DEFINES],[ AC_ARG_ENABLE([$2-$1],[AS_HELP_STRING([--disable-$2-$1],[disable libknet $1 support])],, [enable_$2_$1="$enable_$2_all"]) AM_CONDITIONAL([BUILD_]m4_toupper([$2_$1]),[test "x$enable_$2_$1" = xyes]) if test "x$enable_$2_$1" = xyes; then $3 fi AC_DEFINE_UNQUOTED([WITH_]m4_toupper([$2_$1]), [`test "x$enable_$2_$1" != xyes; echo $?`], $1 $2 [built in]) ]) AC_ARG_ENABLE([man], [AS_HELP_STRING([--disable-man],[disable man page creation])],, [ enable_man="yes" ]) AM_CONDITIONAL([BUILD_MAN], [test x$enable_man = xyes]) AC_ARG_ENABLE([libknet-sctp], [AS_HELP_STRING([--disable-libknet-sctp],[disable libknet SCTP support])],, [ enable_libknet_sctp="yes" ]) AM_CONDITIONAL([BUILD_SCTP], [test x$enable_libknet_sctp = xyes]) AC_ARG_ENABLE([functional-tests], [AS_HELP_STRING([--disable-functional-tests],[disable execution of functional tests, useful for old and slow arches])],, [ enable_functional_tests="yes" ]) AM_CONDITIONAL([RUN_FUN_TESTS], [test x$enable_functional_tests = xyes]) AC_ARG_ENABLE([crypto-all], [AS_HELP_STRING([--disable-crypto-all],[disable libknet all crypto modules support])],, [ enable_crypto_all="yes" ]) KNET_OPTION_DEFINES([nss],[crypto],[PKG_CHECK_MODULES([nss], [nss])]) KNET_OPTION_DEFINES([openssl],[crypto],[PKG_CHECK_MODULES([openssl], [libcrypto])]) # use gcry_mac_open to detect if libgcrypt is new enough KNET_OPTION_DEFINES([gcrypt],[crypto],[ PKG_CHECK_MODULES([gcrypt], [libgcrypt >= 1.8.0],, [AC_CHECK_HEADERS([gcrypt.h], [AC_CHECK_LIB([gcrypt], [gcry_mac_open], [AC_SUBST([gcrypt_LIBS], ["-lgcrypt -ldl -lgpg-error"])])], [AC_MSG_ERROR(["missing required gcrypt.h"])])]) ]) AC_ARG_ENABLE([compress-all], [AS_HELP_STRING([--disable-compress-all],[disable libknet all compress modules support])],, [ enable_compress_all="yes" ]) KNET_OPTION_DEFINES([zstd],[compress],[PKG_CHECK_MODULES([libzstd], [libzstd])]) KNET_OPTION_DEFINES([zlib],[compress],[PKG_CHECK_MODULES([zlib], [zlib])]) KNET_OPTION_DEFINES([lz4],[compress],[PKG_CHECK_MODULES([liblz4], [liblz4])]) KNET_OPTION_DEFINES([lzo2],[compress],[ PKG_CHECK_MODULES([lzo2], [lzo2], [# work around broken pkg-config file in v2.10 AC_SUBST([lzo2_CFLAGS],[`echo $lzo2_CFLAGS | sed 's,/lzo *, ,'`])], [AC_CHECK_HEADERS([lzo/lzo1x.h], [AC_CHECK_LIB([lzo2], [lzo1x_decompress_safe], [AC_SUBST([lzo2_LIBS], [-llzo2])])], [AC_MSG_ERROR(["missing required lzo/lzo1x.h header"])])]) ]) KNET_OPTION_DEFINES([lzma],[compress],[PKG_CHECK_MODULES([liblzma], [liblzma])]) KNET_OPTION_DEFINES([bzip2],[compress],[ PKG_CHECK_MODULES([bzip2], [bzip2],, [AC_CHECK_HEADERS([bzlib.h], [AC_CHECK_LIB([bz2], [BZ2_bzBuffToBuffCompress], [AC_SUBST([bzip2_LIBS], [-lbz2])])], [AC_MSG_ERROR(["missing required bzlib.h"])])]) ]) AC_ARG_ENABLE([install-tests], [AS_HELP_STRING([--enable-install-tests],[install tests])],, [ enable_install_tests="no" ]) AM_CONDITIONAL([INSTALL_TESTS], [test x$enable_install_tests = xyes]) AC_ARG_ENABLE([runautogen], [AS_HELP_STRING([--enable-runautogen],[run autogen.sh])],, [ enable_runautogen="no" ]) AM_CONDITIONAL([BUILD_RUNAUTOGEN], [test x$enable_runautogen = xyes]) override_rpm_debuginfo_option="yes" AC_ARG_ENABLE([rpm-debuginfo], [AS_HELP_STRING([--enable-rpm-debuginfo],[build debuginfo packages])],, [ enable_rpm_debuginfo="no", override_rpm_debuginfo_option="no" ]) AM_CONDITIONAL([BUILD_RPM_DEBUGINFO], [test x$enable_rpm_debuginfo = xyes]) AM_CONDITIONAL([OVERRIDE_RPM_DEBUGINFO], [test x$override_rpm_debuginfo_option = xyes]) AC_ARG_ENABLE([libnozzle], [AS_HELP_STRING([--enable-libnozzle],[libnozzle support])],, [ enable_libnozzle="yes" ]) AM_CONDITIONAL([BUILD_LIBNOZZLE], [test x$enable_libnozzle = xyes]) AC_ARG_ENABLE([rust-bindings], [AS_HELP_STRING([--enable-rust-bindings],[rust bindings support])],, [ enable_rust_bindings="no" ]) AM_CONDITIONAL([BUILD_RUST_BINDINGS], [test x$enable_rust_bindings = xyes]) ## local helper functions # this function checks if CC support options passed as # args. Global CPPFLAGS are ignored during this test. cc_supports_flag() { saveCPPFLAGS="$CPPFLAGS" CPPFLAGS="-Werror $@" AC_MSG_CHECKING([whether $CC supports "$@"]) AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])], [RC=0; AC_MSG_RESULT([yes])], [RC=1; AC_MSG_RESULT([no])]) CPPFLAGS="$saveCPPFLAGS" return $RC } # Checks for libraries. AX_PTHREAD(,[AC_MSG_ERROR([POSIX threads support is required])]) saved_LIBS="$LIBS" LIBS= AC_SEARCH_LIBS([ceil], [m], , [AC_MSG_ERROR([ceil not found])]) AC_SUBST([m_LIBS], [$LIBS]) LIBS= AC_SEARCH_LIBS([clock_gettime], [rt], , [AC_MSG_ERROR([clock_gettime not found])]) AC_SUBST([rt_LIBS], [$LIBS]) LIBS= AC_SEARCH_LIBS([dlopen], [dl dld], , [AC_MSG_ERROR([dlopen not found])]) AC_SUBST([dl_LIBS], [$LIBS]) LIBS="$saved_LIBS" # Check RTLD_DI_ORIGIN (not decalred by musl. glibc has it as an enum so cannot use ifdef) AC_CHECK_DECL([RTLD_DI_ORIGIN], [AC_DEFINE([HAVE_RTLD_DI_ORIGIN], 1, [define when RTLD_DI_ORIGIN is declared])], ,[[#include ]]) # OS detection AC_MSG_CHECKING([for os in ${host_os}]) case "$host_os" in *linux*) AC_DEFINE_UNQUOTED([KNET_LINUX], [1], [Compiling for Linux platform]) AC_MSG_RESULT([Linux]) ;; *bsd*) AC_DEFINE_UNQUOTED([KNET_BSD], [1], [Compiling for BSD platform]) AC_MSG_RESULT([BSD]) ;; *) AC_MSG_ERROR([Unsupported OS? hmmmm]) ;; esac # Checks for header files. AC_CHECK_HEADERS([sys/epoll.h]) AC_CHECK_FUNCS([kevent]) # if neither sys/epoll.h nor kevent are present, we should fail. if test "x$ac_cv_header_sys_epoll_h" = xno && test "x$ac_cv_func_kevent" = xno; then AC_MSG_ERROR([Both epoll and kevent unavailable on this OS]) fi if test "x$ac_cv_header_sys_epoll_h" = xyes && test "x$ac_cv_func_kevent" = xyes; then AC_MSG_ERROR([Both epoll and kevent available on this OS, please contact the maintainers to fix the code]) fi if test "x$enable_libknet_sctp" = xyes; then AC_CHECK_HEADERS([netinet/sctp.h],, [AC_MSG_ERROR(["missing required SCTP headers"])]) fi # Checks for typedefs, structures, and compiler characteristics. AC_C_INLINE AC_TYPE_PID_T AC_TYPE_SIZE_T AC_TYPE_SSIZE_T AC_TYPE_UINT8_T AC_TYPE_UINT16_T AC_TYPE_UINT32_T AC_TYPE_UINT64_T AC_TYPE_INT8_T AC_TYPE_INT16_T AC_TYPE_INT32_T AC_TYPE_INT64_T PKG_CHECK_MODULES([libqb], [libqb]) if test "x$enable_man" = "xyes"; then AC_ARG_VAR([DOXYGEN], [override doxygen executable]) AC_CHECK_PROGS([DOXYGEN], [doxygen], [no]) if test "x$DOXYGEN" = xno; then AC_MSG_ERROR(["Doxygen command not found"]) fi AC_ARG_VAR([DOXYGEN2MAN], [override doxygen2man executable]) # required to detect doxygen2man when libqb is installed # in non standard paths saved_PKG_CONFIG="$PKG_CONFIG" saved_ac_cv_path_PKG_CONFIG="$ac_cv_path_PKG_CONFIG" unset PKG_CONFIG ac_cv_path_PKG_CONFIG AC_PATH_PROG([PKG_CONFIG], [pkg-config]) PKG_CHECK_MODULES([libqb_BUILD], [libqb]) PKG_CHECK_VAR([libqb_BUILD_PREFIX], [libqb], [prefix]) AC_PATH_PROG([DOXYGEN2MAN], [doxygen2man], [no], [$libqb_BUILD_PREFIX/bin$PATH_SEPARATOR$PATH]) PKG_CONFIG="$saved_PKG_CONFIG" ac_cv_path_PKG_CONFIG="$saved_ac_cv_path_PKG_CONFIG" if test "x$DOXYGEN2MAN" = "xno"; then AC_MSG_ERROR(["doxygen2man command not found"]) fi AC_SUBST([DOXYGEN2MAN]) fi # check for rust tools to build bindings if test "x$enable_rust_bindings" = "xyes"; then AC_PATH_PROG([CARGO], [cargo], [no]) if test "x$CARGO" = xno; then AC_MSG_ERROR(["cargo command not found"]) fi AC_PATH_PROG([RUSTC], [rustc], [no]) if test "x$RUSTC" = xno; then AC_MSG_ERROR(["rustc command not found"]) fi AC_PATH_PROG([RUSTDOC], [rustdoc], [no]) if test "x$RUSTDOC" = xno; then AC_MSG_ERROR(["rustdoc command not found"]) fi AC_PATH_PROG([BINDGEN], [bindgen], [no]) if test "x$BINDGEN" = xno; then AC_MSG_ERROR(["bindgen command not found"]) fi AC_PATH_PROG([CLIPPY], [clippy-driver], [no]) if test "x$CLIPPY" = xno; then AC_MSG_ERROR(["clippy-driver command not found"]) fi AC_PATH_PROG([RUSTFMT], [rustfmt], [no]) if test "x$RUSTFMT" = xno; then AC_MSG_WARN(["rustfmt command not found (optional)"]) fi fi # checks for libnozzle if test "x$enable_libnozzle" = xyes; then if `echo $host_os | grep -q linux`; then PKG_CHECK_MODULES([libnl], [libnl-3.0]) PKG_CHECK_MODULES([libnlroute], [libnl-route-3.0 >= 3.3], [], [PKG_CHECK_MODULES([libnlroute], [libnl-route-3.0 < 3.3], [AC_DEFINE_UNQUOTED([LIBNL3_WORKAROUND], [1], [Enable libnl < 3.3 build workaround])], [])]) fi fi # https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html knetcurrent="2" knetrevision="0" knetage="0" # c:r:a libknetversion="$knetcurrent:$knetrevision:$knetage" # soname derived from c:r:a # use $VERSION as build info https://semver.org/. build info are incremental automatically knetalpha="-alpha1" libknetrustver="$(($knetcurrent - $knetage)).$knetage.$knetrevision$knetalpha+$VERSION" nozzlecurrent="1" nozzlerevision="0" nozzleage="0" libnozzleversion="$nozzlecurrent:$nozzlerevision:$nozzleage" # nozzle is stable for now nozzlealpha="" libnozzlerustver="$(($nozzlecurrent - $nozzleage)).$nozzleage.$nozzlerevision$nozzlealpha+$VERSION" AC_SUBST([libknetversion]) AC_SUBST([libknetrustver]) AC_SUBST([libnozzleversion]) AC_SUBST([libnozzlerustver]) # local options AC_ARG_ENABLE([debug], [AS_HELP_STRING([--enable-debug],[enable debug build])]) AC_ARG_ENABLE([onwire-v1-extra-debug], [AS_HELP_STRING([--enable-onwire-v1-extra-debug],[enable onwire protocol v1 extra debug. WARNING: IT BREAKS ONWIRE COMPATIBILITY! DO NOT USE IN PRODUCTION!])]) if test "x${enable_onwire_v1_extra_debug}" = xyes; then AC_DEFINE_UNQUOTED([ONWIRE_V1_EXTRA_DEBUG], [1], [Enable crc32 checksum for data and packets]) fi # for standard crc32 function (used in test suite) PKG_CHECK_MODULES([zlib], [zlib]) AC_ARG_ENABLE([hardening], [AS_HELP_STRING([--disable-hardening],[disable hardening build flags])],, [ enable_hardening="yes" ]) AC_ARG_WITH([sanitizers], [AS_HELP_STRING([--with-sanitizers=...,...], [enable SANitizer build, do *NOT* use for production. Only ASAN/UBSAN/TSAN are currently supported])], [ SANITIZERS="$withval" ], [ SANITIZERS="" ]) AC_ARG_WITH([testdir], [AS_HELP_STRING([--with-testdir=DIR],[path to /usr/lib../kronosnet/tests/ dir where to install the test suite])], [ TESTDIR="$withval" ], [ TESTDIR="$libdir/kronosnet/tests" ]) ## do subst AC_SUBST([TESTDIR]) # debug build stuff if test "x${enable_debug}" = xyes; then AC_DEFINE_UNQUOTED([DEBUG], [1], [Compiling Debugging code]) OPT_CFLAGS="-O0" FORTIFY_CFLAGS="" RUST_FLAGS="" RUST_TARGET_DIR="debug" else OPT_CFLAGS="-O3" FORTIFY_CFLAGS="-D_FORTIFY_SOURCE=2" RUST_FLAGS="--release" RUST_TARGET_DIR="release" fi # Check for availablility of hardening options annocheck=no if test "x${enable_hardening}" = xyes; then # support only gcc for now if echo $CC | grep -q gcc; then ANNOPLUGIN="-fplugin=annobin" annocheck=yes fi HARDENING_CFLAGS_ANNOCHECK="$ANNOPLUGIN -fPIC -DPIC -pie $FORTIFY_CFLAGS -fstack-protector-strong -fexceptions -D_GLIBCXX_ASSERTIONS -Wl,-z,now" HARDENING_CFLAGS="-fstack-clash-protection -fcf-protection=full -mcet -mstackrealign" EXTRA_HARDENING_CFLAGS="" # check for annobin required cflags/ldflags for j in $HARDENING_CFLAGS_ANNOCHECK; do if cc_supports_flag $j; then EXTRA_HARDENING_CFLAGS="$EXTRA_HARDENING_CFLAGS $j" else annocheck=no fi done # check for other hardening cflags/ldflags for j in $HARDENING_CFLAGS; do if cc_supports_flag $j; then EXTRA_HARDENING_CFLAGS="$EXTRA_HARDENING_CFLAGS $j" fi done # check if annocheck binary is available if test "x${annocheck}" = xyes; then AC_CHECK_PROGS([ANNOCHECK_EXEC], [annocheck]) if test "x${ANNOCHECK_EXEC}" = x; then annocheck=no fi fi AM_LDFLAGS="$AM_LDFLAGS $EXTRA_HARDENING_CFLAGS" fi if test "x${enable_debug}" = xyes; then annocheck=no fi AM_CONDITIONAL([HAS_ANNOCHECK], [test "x$annocheck" = "xyes"]) # gdb flags if test "x${GCC}" = xyes; then GDB_CFLAGS="-ggdb3" else GDB_CFLAGS="-g" fi # --- ASAN/UBSAN/TSAN (see man gcc) --- # when using SANitizers, we need to pass the -fsanitize.. # to both CFLAGS and LDFLAGS. The CFLAGS/LDFLAGS must be # specified as first in the list or there will be runtime # issues (for example user has to LD_PRELOAD asan for it to work # properly). if test -n "${SANITIZERS}"; then SANITIZERS=$(echo $SANITIZERS | sed -e 's/,/ /g') for SANITIZER in $SANITIZERS; do case $SANITIZER in asan|ASAN) SANITIZERS_CFLAGS="$SANITIZERS_CFLAGS -fsanitize=address" SANITIZERS_LDFLAGS="$SANITIZERS_LDFLAGS -fsanitize=address -lasan" AC_CHECK_LIB([asan],[main],,AC_MSG_ERROR([Unable to find libasan])) ;; ubsan|UBSAN) SANITIZERS_CFLAGS="$SANITIZERS_CFLAGS -fsanitize=undefined" SANITIZERS_LDFLAGS="$SANITIZERS_LDFLAGS -fsanitize=undefined -lubsan" AC_CHECK_LIB([ubsan],[main],,AC_MSG_ERROR([Unable to find libubsan])) ;; tsan|TSAN) SANITIZERS_CFLAGS="$SANITIZERS_CFLAGS -fsanitize=thread" SANITIZERS_LDFLAGS="$SANITIZERS_LDFLAGS -fsanitize=thread -ltsan" AC_CHECK_LIB([tsan],[main],,AC_MSG_ERROR([Unable to find libtsan])) ;; esac done fi DEFAULT_CFLAGS="-Werror -Wall -Wextra" # manual overrides # generates too much noise for stub APIs UNWANTED_CFLAGS="-Wno-unused-parameter" AC_SUBST([AM_CFLAGS],["$SANITIZERS_CFLAGS $OPT_CFLAGS $GDB_CFLAGS $DEFAULT_CFLAGS $EXTRA_HARDENING_CFLAGS $UNWANTED_CFLAGS"]) LDFLAGS="$SANITIZERS_LDFLAGS $LDFLAGS" AC_SUBST([AM_LDFLAGS]) AC_SUBST([RUST_FLAGS]) AC_SUBST([RUST_TARGET_DIR]) AX_PROG_DATE AS_IF([test "$ax_cv_prog_date_gnu_date:$ax_cv_prog_date_gnu_utc" = yes:yes], [UTC_DATE_AT="date -u -d@"], [AS_IF([test "x$ax_cv_prog_date_bsd_date" = xyes], [UTC_DATE_AT="date -u -r"], [AC_MSG_ERROR([date utility unable to convert epoch to UTC])])]) AC_SUBST([UTC_DATE_AT]) AC_ARG_VAR([SOURCE_EPOCH],[last modification date of the source]) AC_MSG_NOTICE([trying to determine source epoch]) AC_MSG_CHECKING([for source epoch in \$SOURCE_EPOCH]) AS_IF([test -n "$SOURCE_EPOCH"], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no]) AC_MSG_CHECKING([for source epoch in source_epoch file]) AS_IF([test -e "$srcdir/source_epoch"], [read SOURCE_EPOCH <"$srcdir/source_epoch" AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no]) AC_MSG_CHECKING([for source epoch baked in by gitattributes export-subst]) SOURCE_EPOCH='$Format:%at$' # template for rewriting by git-archive AS_CASE([$SOURCE_EPOCH], [?Format:*], # was not rewritten [AC_MSG_RESULT([no]) AC_MSG_CHECKING([for source epoch in \$SOURCE_DATE_EPOCH]) AS_IF([test "x$SOURCE_DATE_EPOCH" != x], [SOURCE_EPOCH="$SOURCE_DATE_EPOCH" AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no]) AC_MSG_CHECKING([whether git log can provide a source epoch]) SOURCE_EPOCH=f${SOURCE_EPOCH#\$F} # convert into git log --pretty format SOURCE_EPOCH=$(cd "$srcdir" && git log -1 --pretty=${SOURCE_EPOCH%$} 2>/dev/null) AS_IF([test -n "$SOURCE_EPOCH"], [AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no, using current time and breaking reproducibility]) SOURCE_EPOCH=$(date +%s)])])], [AC_MSG_RESULT([yes])] )]) ]) AC_MSG_NOTICE([using source epoch $($UTC_DATE_AT$SOURCE_EPOCH +'%F %T %Z')]) AC_CONFIG_FILES([ Makefile libnozzle/Makefile libnozzle/libnozzle.pc libnozzle/tests/Makefile libnozzle/bindings/Makefile libnozzle/bindings/rust/Makefile libnozzle/bindings/rust/Cargo.toml libnozzle/bindings/rust/tests/Makefile libnozzle/bindings/rust/tests/Cargo.toml libknet/Makefile libknet/libknet.pc libknet/tests/Makefile libknet/bindings/Makefile libknet/bindings/rust/Makefile libknet/bindings/rust/Cargo.toml libknet/bindings/rust/tests/Makefile libknet/bindings/rust/tests/Cargo.toml man/Makefile man/Doxyfile-knet man/Doxyfile-nozzle ]) if test "x$VERSION" = "xUNKNOWN"; then AC_MSG_ERROR([m4_text_wrap([ configure was unable to determine the source tree's current version. This generally happens when using git archive (or the github download button) generated tarball/zip file. In order to workaround this issue, either use git clone https://github.com/kronosnet/kronosnet.git or use an official release tarball, available at https://kronosnet.org/releases/. Alternatively you can add a compatible version in a .tarball-version file at the top of the source tree, wipe your autom4te.cache dir and generated configure, and rerun autogen.sh. ], [ ], [ ], [76])]) fi AC_OUTPUT diff --git a/kronosnet.spec.in b/kronosnet.spec.in index f60d84d5..42afe303 100644 --- a/kronosnet.spec.in +++ b/kronosnet.spec.in @@ -1,483 +1,483 @@ ############################################################################### ############################################################################### ## -## Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. +## Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. ## ## This copyrighted material is made available to anyone wishing to use, ## modify, copy, or redistribute it subject to the terms and conditions ## of the GNU General Public License v.2 or higher ## ############################################################################### ############################################################################### # keep around ready for later user %global alphatag @alphatag@ %global numcomm @numcomm@ %global dirty @dirty@ # set defaults from ./configure invokation %@sctp@ sctp %@nss@ nss %@openssl@ openssl %@gcrypt@ gcrypt %@zlib@ zlib %@lz4@ lz4 %@lzo2@ lzo2 %@lzma@ lzma %@bzip2@ bzip2 %@zstd@ zstd %@libnozzle@ libnozzle %@runautogen@ runautogen %@rpmdebuginfo@ rpmdebuginfo %@overriderpmdebuginfo@ overriderpmdebuginfo %@buildman@ buildman %@installtests@ installtests %if %{with overriderpmdebuginfo} %undefine _enable_debug_packages %endif # main (empty) package # http://www.rpm.org/max-rpm/s1-rpm-subpack-spec-file-changes.html Name: kronosnet Summary: Multipoint-to-Multipoint VPN daemon Version: @version@ Release: 1%{?numcomm:.%{numcomm}}%{?alphatag:.%{alphatag}}%{?dirty:.%{dirty}}%{?dist} License: GPLv2+ and LGPLv2+ URL: https://kronosnet.org Source0: https://kronosnet.org/releases/%{name}-%{version}%{?numcomm:.%{numcomm}}%{?alphatag:-%{alphatag}}%{?dirty:-%{dirty}}.tar.gz # Build dependencies BuildRequires: gcc libqb-devel make # required to build man pages %if %{with buildman} BuildRequires: doxygen doxygen2man %endif %if %{with sctp} BuildRequires: lksctp-tools-devel %endif %if %{with nss} %if 0%{?suse_version} BuildRequires: mozilla-nss-devel %else BuildRequires: nss-devel %endif %endif %if %{with openssl} %if 0%{?suse_version} BuildRequires: libopenssl-devel %else BuildRequires: openssl-devel %endif %endif %if %{with gcrypt} BuildRequires: libgcrypt-devel >= 1.8.0 %endif %if %{with zlib} BuildRequires: zlib-devel %endif %if %{with lz4} %if 0%{?suse_version} BuildRequires: liblz4-devel %else BuildRequires: lz4-devel %endif %endif %if %{with lzo2} BuildRequires: lzo-devel %endif %if %{with lzma} BuildRequires: xz-devel %endif %if %{with bzip2} %if 0%{?suse_version} BuildRequires: libbz2-devel %else BuildRequires: bzip2-devel %endif %endif %if %{with zstd} BuildRequires: libzstd-devel %endif %if %{with libnozzle} BuildRequires: libnl3-devel %endif %if %{with runautogen} BuildRequires: autoconf automake libtool %endif %prep %setup -q -n %{name}-%{version}%{?numcomm:.%{numcomm}}%{?alphatag:-%{alphatag}}%{?dirty:-%{dirty}} %build %if %{with runautogen} ./autogen.sh %endif %{configure} \ %if %{with installtests} --enable-install-tests \ %else --disable-install-tests \ %endif %if %{with buildman} --enable-man \ %else --disable-man \ %endif %if %{with sctp} --enable-libknet-sctp \ %else --disable-libknet-sctp \ %endif %if %{with nss} --enable-crypto-nss \ %else --disable-crypto-nss \ %endif %if %{with openssl} --enable-crypto-openssl \ %else --disable-crypto-openssl \ %endif %if %{with gcrypt} --enable-crypto-gcrypt \ %else --disable-crypto-gcrypt \ %endif %if %{with zlib} --enable-compress-zlib \ %else --disable-compress-zlib \ %endif %if %{with lz4} --enable-compress-lz4 \ %else --disable-compress-lz4 \ %endif %if %{with lzo2} --enable-compress-lzo2 \ %else --disable-compress-lzo2 \ %endif %if %{with lzma} --enable-compress-lzma \ %else --disable-compress-lzma \ %endif %if %{with bzip2} --enable-compress-bzip2 \ %else --disable-compress-bzip2 \ %endif %if %{with zstd} --enable-compress-zstd \ %else --disable-compress-zstd \ %endif %if %{with libnozzle} --enable-libnozzle %else --disable-libnozzle %endif make %{_smp_mflags} %install rm -rf %{buildroot} make install DESTDIR=%{buildroot} # tree cleanup # remove static libraries find %{buildroot} -name "*.a" -exec rm {} \; # remove libtools leftovers find %{buildroot} -name "*.la" -exec rm {} \; # remove docs rm -rf %{buildroot}/usr/share/doc/kronosnet # main empty package %description The kronosnet source %if %{with libnozzle} %package -n libnozzle1 Summary: Simple userland wrapper around kernel tap devices License: LGPLv2+ %description -n libnozzle1 This is an over-engineered commodity library to manage a pool of tap devices and provides the basic pre-up.d/up.d/down.d/post-down.d infrastructure. %files -n libnozzle1 %license COPYING.* COPYRIGHT %{_libdir}/libnozzle.so.* %if 0%{?ldconfig_scriptlets} %ldconfig_scriptlets -n libnozzle1 %else %post -n libnozzle1 -p /sbin/ldconfig %postun -n libnozzle1 -p /sbin/ldconfig %endif %package -n libnozzle1-devel Summary: Simple userland wrapper around kernel tap devices (developer files) License: LGPLv2+ Requires: libnozzle1%{_isa} = %{version}-%{release} Requires: pkgconfig %description -n libnozzle1-devel This is an over-engineered commodity library to manage a pool of tap devices and provides the basic pre-up.d/up.d/down.d/post-down.d infrastructure. %files -n libnozzle1-devel %license COPYING.* COPYRIGHT %{_libdir}/libnozzle.so %{_includedir}/libnozzle.h %{_libdir}/pkgconfig/libnozzle.pc %if %{with buildman} %{_mandir}/man3/nozzle*.3.gz %endif %endif %package -n libknet1 Summary: Kronosnet core switching implementation License: LGPLv2+ %description -n libknet1 The whole kronosnet core is implemented in this library. Please refer to the not-yet-existing documentation for further information. %files -n libknet1 %license COPYING.* COPYRIGHT %{_libdir}/libknet.so.* %dir %{_libdir}/kronosnet %if 0%{?ldconfig_scriptlets} %ldconfig_scriptlets -n libknet1 %else %post -n libknet1 -p /sbin/ldconfig %postun -n libknet1 -p /sbin/ldconfig %endif %package -n libknet1-devel Summary: Kronosnet core switching implementation (developer files) License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} Requires: pkgconfig %description -n libknet1-devel The whole kronosnet core is implemented in this library. Please refer to the not-yet-existing documentation for further information. %files -n libknet1-devel %license COPYING.* COPYRIGHT %{_libdir}/libknet.so %{_includedir}/libknet.h %{_libdir}/pkgconfig/libknet.pc %if %{with buildman} %{_mandir}/man3/knet*.3.gz %endif %if %{with nss} %package -n libknet1-crypto-nss-plugin Summary: Provides libknet1 nss support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-crypto-nss-plugin Provides NSS crypto support for libknet1. %files -n libknet1-crypto-nss-plugin %{_libdir}/kronosnet/crypto_nss.so %endif %if %{with openssl} %package -n libknet1-crypto-openssl-plugin Summary: Provides libknet1 openssl support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-crypto-openssl-plugin Provides OpenSSL crypto support for libknet1. %files -n libknet1-crypto-openssl-plugin %{_libdir}/kronosnet/crypto_openssl.so %endif %if %{with gcrypt} %package -n libknet1-crypto-gcrypt-plugin Summary: Provides libknet1 gcrypt support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-crypto-gcrypt-plugin Provides libgcrypt crypto support for libknet1. %files -n libknet1-crypto-gcrypt-plugin %{_libdir}/kronosnet/crypto_gcrypt.so %endif %if %{with zlib} %package -n libknet1-compress-zlib-plugin Summary: Provides libknet1 zlib support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-compress-zlib-plugin Provides zlib compression support for libknet1. %files -n libknet1-compress-zlib-plugin %{_libdir}/kronosnet/compress_zlib.so %endif %if %{with lz4} %package -n libknet1-compress-lz4-plugin Summary: Provides libknet1 lz4 and lz4hc support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-compress-lz4-plugin Provides lz4 and lz4hc compression support for libknet1. %files -n libknet1-compress-lz4-plugin %{_libdir}/kronosnet/compress_lz4.so %{_libdir}/kronosnet/compress_lz4hc.so %endif %if %{with lzo2} %package -n libknet1-compress-lzo2-plugin Summary: Provides libknet1 lzo2 support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-compress-lzo2-plugin Provides lzo2 compression support for libknet1. %files -n libknet1-compress-lzo2-plugin %{_libdir}/kronosnet/compress_lzo2.so %endif %if %{with lzma} %package -n libknet1-compress-lzma-plugin Summary: Provides libknet1 lzma support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-compress-lzma-plugin Provides lzma compression support for libknet1. %files -n libknet1-compress-lzma-plugin %{_libdir}/kronosnet/compress_lzma.so %endif %if %{with bzip2} %package -n libknet1-compress-bzip2-plugin Summary: Provides libknet1 bzip2 support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-compress-bzip2-plugin Provides bzip2 compression support for libknet1. %files -n libknet1-compress-bzip2-plugin %{_libdir}/kronosnet/compress_bzip2.so %endif %if %{with zstd} %package -n libknet1-compress-zstd-plugin Summary: Provides libknet1 zstd support License: LGPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %description -n libknet1-compress-zstd-plugin Provides zstd compression support for libknet1. %files -n libknet1-compress-zstd-plugin %{_libdir}/kronosnet/compress_zstd.so %endif %package -n libknet1-crypto-plugins-all Summary: Provides libknet1 crypto plugins meta package License: LGPLv2+ %if %{with nss} Requires: libknet1-crypto-nss-plugin%{_isa} = %{version}-%{release} %endif %if %{with openssl} Requires: libknet1-crypto-openssl-plugin%{_isa} = %{version}-%{release} %endif %if %{with gcrypt} Requires: libknet1-crypto-gcrypt-plugin%{_isa} = %{version}-%{release} %endif %description -n libknet1-crypto-plugins-all Provides meta package to install all of libknet1 crypto plugins %files -n libknet1-crypto-plugins-all %package -n libknet1-compress-plugins-all Summary: Provides libknet1 compress plugins meta package License: LGPLv2+ %if %{with zlib} Requires: libknet1-compress-zlib-plugin%{_isa} = %{version}-%{release} %endif %if %{with lz4} Requires: libknet1-compress-lz4-plugin%{_isa} = %{version}-%{release} %endif %if %{with lzo2} Requires: libknet1-compress-lzo2-plugin%{_isa} = %{version}-%{release} %endif %if %{with lzma} Requires: libknet1-compress-lzma-plugin%{_isa} = %{version}-%{release} %endif %if %{with bzip2} Requires: libknet1-compress-bzip2-plugin%{_isa} = %{version}-%{release} %endif %if %{with zstd} Requires: libknet1-compress-zstd-plugin%{_isa} = %{version}-%{release} %endif %description -n libknet1-compress-plugins-all Meta package to install all of libknet1 compress plugins %files -n libknet1-compress-plugins-all %package -n libknet1-plugins-all Summary: Provides libknet1 plugins meta package License: LGPLv2+ Requires: libknet1-compress-plugins-all%{_isa} = %{version}-%{release} Requires: libknet1-crypto-plugins-all%{_isa} = %{version}-%{release} %description -n libknet1-plugins-all Meta package to install all of libknet1 plugins %files -n libknet1-plugins-all %if %{with installtests} %package -n kronosnet-tests Summary: Provides kronosnet test suite License: GPLv2+ Requires: libknet1%{_isa} = %{version}-%{release} %if %{with libnozzle} Requires: libnozzle1%{_isa} = %{version}-%{release} %endif %description -n kronosnet-tests This package contains all the libknet and libnozzle test suite. %files -n kronosnet-tests %{_libdir}/kronosnet/tests/* %endif %if %{with rpmdebuginfo} %debug_package %endif %changelog * @date@ Autotools generated version - @version@-1-@numcomm@.@alphatag@.@dirty@ - These aren't the droids you're looking for. diff --git a/libknet/Makefile.am b/libknet/Makefile.am index 58e661ab..842511c8 100644 --- a/libknet/Makefile.am +++ b/libknet/Makefile.am @@ -1,176 +1,176 @@ # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # Federico Simoncelli # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk SYMFILE = libknet_exported_syms EXTRA_DIST = $(SYMFILE) SUBDIRS = . tests bindings # override global LIBS that pulls in lots of craft we don't need here LIBS = sources = \ common.c \ compat.c \ compress.c \ crypto.c \ handle.c \ handle_api.c \ host.c \ lib_config.c \ links.c \ links_acl.c \ links_acl_ip.c \ links_acl_loopback.c \ logging.c \ netutils.c \ onwire.c \ onwire_v1.c \ threads_common.c \ threads_dsthandler.c \ threads_heartbeat.c \ threads_pmtud.c \ threads_rx.c \ threads_tx.c \ transports.c \ transport_common.c \ transport_loopback.c \ transport_udp.c \ transport_sctp.c include_HEADERS = libknet.h pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libknet.pc noinst_HEADERS = \ common.h \ compat.h \ compress.h \ compress_model.h \ crypto.h \ crypto_model.h \ host.h \ internals.h \ links.h \ links_acl.h \ links_acl_ip.h \ links_acl_loopback.h \ logging.h \ netutils.h \ onwire.h \ onwire_v1.h \ threads_common.h \ threads_dsthandler.h \ threads_heartbeat.h \ threads_pmtud.h \ threads_rx.h \ threads_tx.h \ transports.h \ transport_common.h \ transport_loopback.h \ transport_udp.h \ transport_sctp.h lib_LTLIBRARIES = libknet.la libknet_la_SOURCES = $(sources) AM_CFLAGS += $(libqb_CFLAGS) libknet_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) $(zlib_CFLAGS) \ -DPLUGINPATH="\"$(pkglibdir)\"" EXTRA_libknet_la_DEPENDENCIES = $(SYMFILE) libknet_la_LDFLAGS = $(AM_LDFLAGS) \ -Wl,--version-script=$(srcdir)/$(SYMFILE) \ -version-info $(libknetversion) libknet_la_LIBADD = $(PTHREAD_LIBS) $(dl_LIBS) $(rt_LIBS) $(m_LIBS) $(zlib_LIBS) check-local: check-annocheck-libs # Prepare empty value for appending pkglib_LTLIBRARIES = # MODULE_LDFLAGS would mean a target-specific variable for Automake MODULELDFLAGS = $(AM_LDFLAGS) -module -avoid-version -export-dynamic if BUILD_COMPRESS_ZSTD pkglib_LTLIBRARIES += compress_zstd.la compress_zstd_la_LDFLAGS = $(MODULELDFLAGS) compress_zstd_la_CFLAGS = $(AM_CFLAGS) $(libzstd_CFLAGS) compress_zstd_la_LIBADD = $(libzstd_LIBS) endif if BUILD_COMPRESS_ZLIB pkglib_LTLIBRARIES += compress_zlib.la compress_zlib_la_LDFLAGS = $(MODULELDFLAGS) compress_zlib_la_CFLAGS = $(AM_CFLAGS) $(zlib_CFLAGS) compress_zlib_la_LIBADD = $(zlib_LIBS) endif if BUILD_COMPRESS_LZ4 pkglib_LTLIBRARIES += compress_lz4.la compress_lz4hc.la compress_lz4_la_LDFLAGS = $(MODULELDFLAGS) compress_lz4_la_CFLAGS = $(AM_CFLAGS) $(liblz4_CFLAGS) compress_lz4_la_LIBADD = $(liblz4_LIBS) compress_lz4hc_la_LDFLAGS = $(MODULELDFLAGS) compress_lz4hc_la_CFLAGS = $(AM_CFLAGS) $(liblz4_CFLAGS) compress_lz4hc_la_LIBADD = $(liblz4_LIBS) endif if BUILD_COMPRESS_LZO2 pkglib_LTLIBRARIES += compress_lzo2.la compress_lzo2_la_LDFLAGS = $(MODULELDFLAGS) compress_lzo2_la_CFLAGS = $(AM_CFLAGS) $(lzo2_CFLAGS) compress_lzo2_la_LIBADD = $(lzo2_LIBS) endif if BUILD_COMPRESS_LZMA pkglib_LTLIBRARIES += compress_lzma.la compress_lzma_la_LDFLAGS = $(MODULELDFLAGS) compress_lzma_la_CFLAGS = $(AM_CFLAGS) $(liblzma_CFLAGS) compress_lzma_la_LIBADD = $(liblzma_LIBS) endif if BUILD_COMPRESS_BZIP2 pkglib_LTLIBRARIES += compress_bzip2.la compress_bzip2_la_LDFLAGS = $(MODULELDFLAGS) compress_bzip2_la_CFLAGS = $(AM_CFLAGS) $(bzip2_CFLAGS) compress_bzip2_la_LIBADD = $(bzip2_LIBS) endif if BUILD_CRYPTO_NSS pkglib_LTLIBRARIES += crypto_nss.la crypto_nss_la_LDFLAGS = $(MODULELDFLAGS) crypto_nss_la_CFLAGS = $(AM_CFLAGS) $(nss_CFLAGS) crypto_nss_la_LIBADD = $(nss_LIBS) endif if BUILD_CRYPTO_OPENSSL pkglib_LTLIBRARIES += crypto_openssl.la crypto_openssl_la_LDFLAGS = $(MODULELDFLAGS) crypto_openssl_la_CFLAGS = $(AM_CFLAGS) $(openssl_CFLAGS) crypto_openssl_la_LIBADD = $(openssl_LIBS) endif if BUILD_CRYPTO_GCRYPT pkglib_LTLIBRARIES += crypto_gcrypt.la crypto_gcrypt_la_LDFLAGS = $(MODULELDFLAGS) crypto_gcrypt_la_CFLAGS = $(AM_CFLAGS) $(gcrypt_CFLAGS) crypto_gcrypt_la_LIBADD = $(gcrypt_LIBS) endif diff --git a/libknet/bindings/Makefile.am b/libknet/bindings/Makefile.am index 46a860a5..2a3d7536 100644 --- a/libknet/bindings/Makefile.am +++ b/libknet/bindings/Makefile.am @@ -1,17 +1,17 @@ # -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk SUBDIRS = . if BUILD_RUST_BINDINGS SUBDIRS += rust endif diff --git a/libknet/bindings/rust/Cargo.toml.in b/libknet/bindings/rust/Cargo.toml.in index 1f7f1979..f5805f10 100644 --- a/libknet/bindings/rust/Cargo.toml.in +++ b/libknet/bindings/rust/Cargo.toml.in @@ -1,28 +1,28 @@ -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under LGPL-2.1+ [package] name = "knet-bindings" version = "@libknetrustver@" authors = ["Christine Caulfield "] edition = "2018" readme = "README" license = "LGPL-2.1+" repository = "https://github.com/kronosnet/kronosnet" description = "Rust bindings for Kronosnet libraries" categories = ["api-bindings"] keywords = ["cluster", "high-availability"] exclude = [ "*.in", "Makefile*", ] [dependencies] bitflags = "1.2.1" lazy_static = "1.4.0" os_socketaddr = "0.2.0" libc = "0.2.93" diff --git a/libknet/bindings/rust/Makefile.am b/libknet/bindings/rust/Makefile.am index 7a92afa5..9254b2f7 100644 --- a/libknet/bindings/rust/Makefile.am +++ b/libknet/bindings/rust/Makefile.am @@ -1,37 +1,37 @@ # -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk include $(top_srcdir)/build-aux/rust.mk # required for make check localver = $(libknetrustver) SUBDIRS = . tests EXTRA_DIST = \ $(RUST_COMMON) \ $(RUST_SHIP_SRCS) \ README RUST_SHIP_SRCS = \ src/knet_bindings.rs \ src/lib.rs \ src/sys/mod.rs RUST_BUILT_SRCS = \ src/sys/libknet.rs src/sys/libknet.rs: ../../libknet.h $(top_srcdir)/build-aux/rust-regen.sh $^ $@ KNET all-local: cargo-tree-prep target/$(RUST_TARGET_DIR)/knet_bindings.rlib clean-local: cargo-clean diff --git a/libknet/bindings/rust/README b/libknet/bindings/rust/README index 33ae6a07..3095ddd5 100644 --- a/libknet/bindings/rust/README +++ b/libknet/bindings/rust/README @@ -1,13 +1,13 @@ -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under GPL-2.0+ This crate contains Rust bindings for the kronosnet (knet) library libknet: https://kronosnet.org/ Kronosnet, often referred to as knet, is a network abstraction layer designed for High Availability use cases, where redundancy, security, fault tolerance and fast fail-over are the core requirements of your application. diff --git a/libknet/bindings/rust/build.rs.in b/libknet/bindings/rust/build.rs.in index 00f39b90..9a18e280 100644 --- a/libknet/bindings/rust/build.rs.in +++ b/libknet/bindings/rust/build.rs.in @@ -1,11 +1,11 @@ -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // fn main() { println!("cargo:rustc-link-search=native=../../"); println!("cargo:rustc-link-lib=knet"); } diff --git a/libknet/bindings/rust/src/knet_bindings.rs b/libknet/bindings/rust/src/knet_bindings.rs index 6c0c6d2c..b3e9326b 100644 --- a/libknet/bindings/rust/src/knet_bindings.rs +++ b/libknet/bindings/rust/src/knet_bindings.rs @@ -1,2547 +1,2547 @@ // libknet interface for Rust -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // #![allow(clippy::too_many_arguments)] #![allow(clippy::collapsible_else_if)] // For the code generated by bindgen use crate::sys::libknet as ffi; use std::ffi::{CString, CStr}; use std::sync::mpsc::*; use std::ptr::{copy_nonoverlapping, null, null_mut}; use std::sync::Mutex; use std::collections::HashMap; use std::io::{Result, Error, ErrorKind}; use std::os::raw::{c_void, c_char, c_uchar, c_uint}; use std::mem::size_of; use std::net::SocketAddr; use std::fmt; use std::thread::spawn; use std::time::{Duration, SystemTime}; use os_socketaddr::OsSocketAddr; #[derive(Copy, Clone, PartialEq, Eq)] /// The ID of a host known to knet. pub struct HostId { host_id: u16, } impl HostId { pub fn new(id: u16) -> HostId { HostId{host_id: id} } pub fn to_u16(self: HostId) -> u16 { self.host_id } } impl fmt::Display for HostId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f,"{}", self.host_id)?; Ok(()) } } pub enum TxRx { Tx = 0, Rx = 1 } impl TxRx { pub fn new (tx_rx: u8) -> TxRx { match tx_rx { 1 => TxRx::Rx, _ => TxRx::Tx } } } impl fmt::Display for TxRx { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { TxRx::Tx => write!(f, "Tx"), TxRx::Rx => write!(f, "Rx"), } } } bitflags! { /// Flags passed into [handle_new] pub struct HandleFlags: u64 { const PRIVILEGED = 1; const NONE = 0; } } bitflags! { /// Flags passed into [link_set_config] pub struct LinkFlags: u64 { const TRAFFICHIPRIO = 1; const NONE = 0; } } /// for passing to [handle_crypto_set_config] pub struct CryptoConfig<'a> { pub crypto_model: String, pub crypto_cipher_type: String, pub crypto_hash_type: String, pub private_key: &'a [u8], } /// for passing to [handle_compress] pub struct CompressConfig { pub compress_model: String, pub compress_threshold: u32, pub compress_level: i32, } /// Return value from packet filter pub enum FilterDecision { Discard, Unicast, Multicast } impl FilterDecision { pub fn to_i32(self: &FilterDecision) -> i32 { match self { FilterDecision::Discard => -1, FilterDecision::Unicast => 0, FilterDecision::Multicast => 1, } } } impl fmt::Display for FilterDecision { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { FilterDecision::Discard => write!(f, "Discard"), FilterDecision::Unicast => write!(f, "Unicast"), FilterDecision::Multicast => write!(f, "Multicast"), } } } // Used to convert a knet_handle_t into one of ours lazy_static! { static ref HANDLE_HASH: Mutex> = Mutex::new(HashMap::new()); } fn get_errno() -> i32 { match Error::last_os_error().raw_os_error() { Some(e) => e, None => libc::EINVAL, } } /// Callback from [handle_enable_sock_notify] pub type SockNotifyFn = fn(private_data: u64, datafd: i32, channel: i8, txrx: TxRx, Result<()>); /// Callback called when packets arrive/are sent [handle_enable_filter] pub type FilterFn = fn(private_data: u64, outdata: &[u8], txrx: TxRx, this_host_id: HostId, src_host_id: HostId, channel: &mut i8, dst_host_ids: &mut Vec) -> FilterDecision; /// Callback called when PMTU changes, see [handle_enable_pmtud_notify] pub type PmtudNotifyFn = fn(private_data: u64, data_mtu: u32); /// Called when the onwire version number for a node changes, see [handle_enable_onwire_ver_notify] pub type OnwireNotifyFn = fn(private_data: u64, onwire_min_ver: u8, onwire_max_ver: u8, onwire_ver: u8); /// Called when a host status changes, see [host_enable_status_change_notify] pub type HostStatusChangeNotifyFn = fn(private_data: u64, host_id: HostId, reachable: bool, remote: bool, external: bool); /// Called when a link status changes, see [link_enable_status_change_notify] pub type LinkStatusChangeNotifyFn = fn(private_data: u64, host_id: HostId, link_id: u8, connected: bool, remote: bool, external: bool); // Called from knet, we work out where to route it to and convert params extern "C" fn rust_sock_notify_fn( private_data: *mut c_void, datafd: i32, channel: i8, tx_rx: u8, error: i32, errorno: i32) { if let Some(h) = HANDLE_HASH.lock().unwrap().get(&(private_data as u64)) { let res = if error == 0 { Ok(()) } else { Err(Error::from_raw_os_error(errorno)) }; // Call user fn if let Some(f) = h.sock_notify_fn { f(h.sock_notify_private_data, datafd, channel, TxRx::new(tx_rx), res); } } } // Called from knet, we work out where to route it to and convert params extern "C" fn rust_filter_fn( private_data: *mut c_void, outdata: *const c_uchar, outdata_len: isize, tx_rx: u8, this_host_id: u16, src_host_id: u16, channel: *mut i8, dst_host_ids: *mut u16, dst_host_ids_entries: *mut usize) -> i32 { let mut ret : i32 = -1; if let Some(h) = HANDLE_HASH.lock().unwrap().get(&(private_data as u64)) { // Is there is filter fn? if let Some(f) = h.filter_fn { let data : &[u8] = unsafe { std::slice::from_raw_parts(outdata as *const u8, outdata_len as usize) }; let mut r_channel = unsafe {*channel}; let mut hosts_vec = Vec::::new(); // Call Rust callback ret = f(h.filter_private_data, data, TxRx::new(tx_rx), HostId{host_id: this_host_id}, HostId{host_id: src_host_id}, &mut r_channel, &mut hosts_vec).to_i32(); // Pass back mutable params dst_hosts unsafe { *channel = r_channel; *dst_host_ids_entries = hosts_vec.len(); let mut retvec = dst_host_ids; for i in &hosts_vec { *retvec = i.host_id; retvec = retvec.offset(1); // next entry } } } } ret } // Called from knet, we work out where to route it to and convert params extern "C" fn rust_pmtud_notify_fn( private_data: *mut c_void, data_mtu: u32) { if let Some(h) = HANDLE_HASH.lock().unwrap().get(&(private_data as u64)) { // Call user fn if let Some(f) = h.pmtud_notify_fn { f(h.pmtud_notify_private_data, data_mtu); } } } // Called from knet, we work out where to route it to and convert params extern "C" fn rust_onwire_notify_fn( private_data: *mut c_void, onwire_min_ver: u8, onwire_max_ver: u8, onwire_ver: u8) { if let Some(h) = HANDLE_HASH.lock().unwrap().get(&(private_data as u64)) { // Call user fn if let Some(f) = h.onwire_notify_fn { f(h.onwire_notify_private_data, onwire_min_ver, onwire_max_ver, onwire_ver); } } } // Called from knet, we work out where to route it to and convert params extern "C" fn rust_host_status_change_notify_fn( private_data: *mut c_void, host_id: u16, reachable: u8, remote: u8, external: u8) { if let Some(h) = HANDLE_HASH.lock().unwrap().get(&(private_data as u64)) { // Call user fn if let Some(f) = h.host_status_change_notify_fn { f(h.host_status_change_notify_private_data, HostId{host_id}, crate::u8_to_bool(reachable), crate::u8_to_bool(remote), crate::u8_to_bool(external)); } } } // Called from knet, we work out where to route it to and convert params extern "C" fn rust_link_status_change_notify_fn( private_data: *mut c_void, host_id: u16, link_id: u8, connected: u8, remote: u8, external: u8) { if let Some(h) = HANDLE_HASH.lock().unwrap().get(&(private_data as u64)) { // Call user fn if let Some(f) = h.link_status_change_notify_fn { f(h.link_status_change_notify_private_data, HostId{host_id}, link_id, crate::u8_to_bool(connected), crate::u8_to_bool(remote), crate::u8_to_bool(external)); } } } // Logging thread fn logging_thread(knet_pipe: i32, sender: Sender) { let mut logbuf = ffi::knet_log_msg {msg: [0; 254], subsystem: 0, msglevel: 0, knet_h: 0 as ffi::knet_handle_t}; // Make it blocking unsafe { libc::fcntl(knet_pipe, libc::F_SETFL, libc::fcntl(knet_pipe, libc::F_GETFL, 0) & !libc::O_NONBLOCK)}; loop { let msglen = unsafe {libc::read(knet_pipe, &mut logbuf as *mut _ as *mut c_void, size_of::())}; if msglen < 1 { unsafe { libc::close(knet_pipe); } // EOF on pipe, handle is closed. return; } if msglen == size_of::() as isize { let rmsg = LogMsg { msg: crate::string_from_bytes_safe(logbuf.msg.as_ptr(), 254), subsystem: SubSystem::new(logbuf.subsystem), level: LogLevel::new(logbuf.msglevel), handle: Handle{knet_handle: logbuf.knet_h as u64}}; if let Err(e) = sender.send(rmsg) { println!("Error sending log message: {}", e); } } } } #[derive(Copy, Clone, PartialEq, Eq)] #[repr(transparent)] /// a handle into the knet library, returned from [handle_new] pub struct Handle { knet_handle: u64, } // Private version of knet handle, contains all the callback data so // we only need to access it in the calback functions, making the rest // a bit quicker & neater struct PrivHandle { log_fd: i32, sock_notify_fn: Option, sock_notify_private_data: u64, filter_fn: Option, filter_private_data: u64, pmtud_notify_fn: Option, pmtud_notify_private_data: u64, onwire_notify_fn: Option, onwire_notify_private_data: u64, host_status_change_notify_fn: Option, host_status_change_notify_private_data: u64, link_status_change_notify_fn: Option, link_status_change_notify_private_data: u64, } /// A knet logging message returned down the log_sender channel set in [handle_new] pub struct LogMsg { pub msg: String, pub subsystem: SubSystem, pub level: LogLevel, pub handle: Handle, } /// Initialise the knet library, returns a handle for use with the other API calls pub fn handle_new(host_id: &HostId, log_sender: Option>, default_log_level: LogLevel, flags: HandleFlags) -> Result { // If a log sender was passed, make an FD & thread for knet let log_fd = match log_sender { Some(s) => { let mut pipes = [0i32; 2]; if unsafe {libc::pipe(pipes.as_mut_ptr())} != 0 { return Err(Error::last_os_error()); } spawn(move || logging_thread(pipes[0], s)); pipes[1] }, None => 0 }; let res = unsafe { ffi::knet_handle_new(host_id.host_id, log_fd, default_log_level.to_u8(), flags.bits) }; if res.is_null() { Err(Error::last_os_error()) } else { let rhandle = PrivHandle{log_fd, sock_notify_fn: None, sock_notify_private_data: 0u64, filter_fn: None, filter_private_data: 0u64, pmtud_notify_fn: None, pmtud_notify_private_data: 0u64, onwire_notify_fn: None, onwire_notify_private_data: 0u64, host_status_change_notify_fn: None, host_status_change_notify_private_data: 0u64, link_status_change_notify_fn: None, link_status_change_notify_private_data: 0u64, }; HANDLE_HASH.lock().unwrap().insert(res as u64, rhandle); Ok(Handle{knet_handle: res as u64}) } } /// Finish with knet, frees the handle returned by [handle_new] pub fn handle_free(handle: Handle) -> Result<()> { let res = unsafe { ffi::knet_handle_free(handle.knet_handle as ffi::knet_handle_t) }; if res == 0 { // Close the log fd as knet doesn't "do ownership" and this will shut down // our logging thread. if let Some(h) = HANDLE_HASH.lock().unwrap().get_mut(&(handle.knet_handle)) { unsafe { libc::close(h.log_fd); }; } HANDLE_HASH.lock().unwrap().remove(&handle.knet_handle); Ok(()) } else { Err(Error::last_os_error()) } } /// Enable notifications of socket state changes, set callback to 'None' to disable pub fn handle_enable_sock_notify(handle: Handle, private_data: u64, sock_notify_fn: Option) -> Result<()> { if let Some(h) = HANDLE_HASH.lock().unwrap().get_mut(&(handle.knet_handle)) { h.sock_notify_private_data = private_data; h.sock_notify_fn = sock_notify_fn; let res = match sock_notify_fn { Some(_f) => unsafe { ffi::knet_handle_enable_sock_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, Some(rust_sock_notify_fn)) }, None => unsafe { ffi::knet_handle_enable_sock_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, None) }, }; if res == 0 { return Ok(()); } else { return Err(Error::last_os_error()); } } Err(Error::new(ErrorKind::Other, "Rust handle not found")) } /// Add a data FD to knet. if datafd is 0 then knet will allocate one for you. pub fn handle_add_datafd(handle: Handle, datafd: i32, channel: i8) -> Result<(i32, i8)> { let mut c_datafd = datafd; let mut c_channel = channel; let res = unsafe { ffi::knet_handle_add_datafd(handle.knet_handle as ffi::knet_handle_t, &mut c_datafd, &mut c_channel) }; if res == 0 { Ok((c_datafd, c_channel)) } else { Err(Error::last_os_error()) } } /// Remove a datafd from knet pub fn handle_remove_datafd(handle: Handle, datafd: i32) -> Result<()> { let res = unsafe { ffi::knet_handle_remove_datafd(handle.knet_handle as ffi::knet_handle_t, datafd) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Returns the channel associated with data fd pub fn handle_get_channel(handle: Handle, datafd: i32) -> Result { let mut c_channel = 0i8; let res = unsafe { ffi::knet_handle_get_channel(handle.knet_handle as ffi::knet_handle_t, datafd, &mut c_channel) }; if res == 0 { Ok(c_channel) } else { Err(Error::last_os_error()) } } /// Returns the data FD associated with a channel pub fn handle_get_datafd(handle: Handle, channel: i8) -> Result { let mut c_datafd = 0i32; let res = unsafe { ffi::knet_handle_get_datafd(handle.knet_handle as ffi::knet_handle_t, channel, &mut c_datafd) }; if res == 0 { Ok(c_datafd) } else { Err(Error::last_os_error()) } } #[derive(Copy, Clone, PartialEq, Eq)] pub enum DefragReclaimPolicy { Average = 0, Absolute = 1 } impl DefragReclaimPolicy { pub fn new (policy: u32) -> DefragReclaimPolicy { { match policy { 1 => DefragReclaimPolicy::Absolute, _ => DefragReclaimPolicy::Average, } } } pub fn to_u32 (&self) -> u32 { { match self { DefragReclaimPolicy::Absolute => 1, DefragReclaimPolicy::Average => 0, } } } } impl fmt::Display for DefragReclaimPolicy { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { DefragReclaimPolicy::Absolute => write!(f, "Absolute"), DefragReclaimPolicy::Average => write!(f, "Average"), } } } /// Configure the defrag buffer parameters - applies to all hosts pub fn handle_set_host_defrag_bufs(handle: Handle, min_defrag_bufs: u16, max_defrag_bufs: u16, shrink_threshold: u8, reclaim_policy: DefragReclaimPolicy) -> Result<()> { let res = unsafe { ffi::knet_handle_set_host_defrag_bufs(handle.knet_handle as ffi::knet_handle_t, min_defrag_bufs, max_defrag_bufs, shrink_threshold, reclaim_policy.to_u32()) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get the defrag buffer parameters. /// Returns (min_defrag_bufs, max_defrag_bufs, shrink_threshold, usage_samples, usage_samples_timespan, reclaim_policy) pub fn handle_get_host_defrag_bufs(handle: Handle) -> Result<(u16, u16, u8, DefragReclaimPolicy)> { let mut min_defrag_bufs: u16 = 0; let mut max_defrag_bufs: u16 = 0; let mut shrink_threshold: u8 = 0; let mut reclaim_policy: u32 = 0; let res = unsafe { ffi::knet_handle_get_host_defrag_bufs(handle.knet_handle as ffi::knet_handle_t, &mut min_defrag_bufs, &mut max_defrag_bufs, &mut shrink_threshold, &mut reclaim_policy) }; if res == 0 { Ok((min_defrag_bufs, max_defrag_bufs, shrink_threshold, DefragReclaimPolicy::new(reclaim_policy))) } else { Err(Error::last_os_error()) } } /// Receive messages from knet pub fn recv(handle: Handle, buf: &[u8], channel: i8) -> Result { let res = unsafe { ffi::knet_recv(handle.knet_handle as ffi::knet_handle_t, buf.as_ptr() as *mut c_char, buf.len(), channel) }; if res >= 0 { Ok(res) } else { if get_errno() == libc::EAGAIN { Err(Error::new(ErrorKind::WouldBlock, "Try again")) } else { Err(Error::last_os_error()) } } } /// Send messages knet pub fn send(handle: Handle, buf: &[u8], channel: i8) -> Result { let res = unsafe { ffi::knet_send(handle.knet_handle as ffi::knet_handle_t, buf.as_ptr() as *const c_char, buf.len(), channel) }; if res >= 0 { Ok(res) } else { if get_errno() == libc::EAGAIN { Err(Error::new(ErrorKind::WouldBlock, "Try again")) } else { Err(Error::last_os_error()) } } } /// Send messages to knet and wait till they have gone pub fn send_sync(handle: Handle, buf: &[u8], channel: i8) -> Result<()> { let res = unsafe { ffi::knet_send_sync(handle.knet_handle as ffi::knet_handle_t, buf.as_ptr() as *const c_char, buf.len(), channel) }; if res == 0 { Ok(()) } else { if get_errno() == libc::EAGAIN { Err(Error::new(ErrorKind::WouldBlock, "Try again")) } else { Err(Error::last_os_error()) } } } /// Enable the packet filter. pass 'None' as the callback to disable. pub fn handle_enable_filter(handle: Handle, private_data: u64, filter_fn: Option) -> Result<()> { if let Some(h) = HANDLE_HASH.lock().unwrap().get_mut(&(handle.knet_handle)) { h.filter_private_data = private_data; h.filter_fn = filter_fn; let res = match filter_fn { Some(_f) => unsafe { ffi::knet_handle_enable_filter(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, Some(rust_filter_fn)) }, None => unsafe { ffi::knet_handle_enable_filter(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, None) }, }; if res == 0 { return Ok(()); } else { return Err(Error::last_os_error()); } }; Err(Error::new(ErrorKind::Other, "Rust handle not found")) } /// Set timer resolution pub fn handle_set_threads_timer_res(handle: Handle, timeres: u32) -> Result<()> { let res = unsafe { ffi::knet_handle_set_threads_timer_res(handle.knet_handle as ffi::knet_handle_t, timeres) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get timer resolution pub fn handle_get_threads_timer_res(handle: Handle) -> Result { let mut c_timeres: u32 = 0; let res = unsafe { ffi::knet_handle_get_threads_timer_res(handle.knet_handle as ffi::knet_handle_t, &mut c_timeres) }; if res == 0 { Ok(c_timeres) } else { Err(Error::last_os_error()) } } /// Starts traffic moving. You must call this before knet will do anything. pub fn handle_setfwd(handle: Handle, enabled: bool) -> Result<()> { let res = unsafe { ffi::knet_handle_setfwd(handle.knet_handle as ffi::knet_handle_t, enabled as c_uint) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Enable access control lists pub fn handle_enable_access_lists(handle: Handle, enabled: bool) -> Result<()> { let res = unsafe { ffi::knet_handle_enable_access_lists(handle.knet_handle as ffi::knet_handle_t, enabled as c_uint) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Set frequency that PMTUd will check for MTU changes. value in milliseconds pub fn handle_pmtud_setfreq(handle: Handle, interval: u32) -> Result<()> { let res = unsafe { ffi::knet_handle_pmtud_setfreq(handle.knet_handle as ffi::knet_handle_t, interval) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get frequency that PMTUd will check for MTU changes. value in milliseconds pub fn handle_pmtud_getfreq(handle: Handle) -> Result { let mut c_interval = 0u32; let res = unsafe { ffi::knet_handle_pmtud_getfreq(handle.knet_handle as ffi::knet_handle_t, &mut c_interval) }; if res == 0 { Ok(c_interval) } else { Err(Error::last_os_error()) } } /// Get the current MTU pub fn handle_pmtud_get(handle: Handle) -> Result { let mut c_mtu = 0u32; let res = unsafe { ffi::knet_handle_pmtud_get(handle.knet_handle as ffi::knet_handle_t, &mut c_mtu) }; if res == 0 { Ok(c_mtu) } else { Err(Error::last_os_error()) } } /// Set the interface MTU (this should not be necessary) pub fn handle_pmtud_set(handle: Handle, iface_mtu: u32) -> Result<()> { let res = unsafe { ffi::knet_handle_pmtud_set(handle.knet_handle as ffi::knet_handle_t, iface_mtu) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Enable notification of MTU changes pub fn handle_enable_pmtud_notify(handle: Handle, private_data: u64, pmtud_notify_fn: Option) -> Result<()> { if let Some(h) = HANDLE_HASH.lock().unwrap().get_mut(&(handle.knet_handle)) { h.pmtud_notify_private_data = private_data; h.pmtud_notify_fn = pmtud_notify_fn; let res = match pmtud_notify_fn { Some(_f) => unsafe { ffi::knet_handle_enable_pmtud_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, Some(rust_pmtud_notify_fn)) }, None => unsafe { ffi::knet_handle_enable_pmtud_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, None) }, }; if res == 0 { return Ok(()); } else { return Err(Error::last_os_error()); } } Err(Error::new(ErrorKind::Other, "Rust handle not found")) } /// Configure cryptographic seetings for packets being transmitted pub fn handle_crypto_set_config(handle: Handle, config: &CryptoConfig, config_num: u8) -> Result<()> { let mut crypto_cfg = ffi::knet_handle_crypto_cfg { crypto_model: [0; 16], crypto_cipher_type: [0; 16], crypto_hash_type: [0; 16], private_key: [0; 4096], private_key_len: 0, }; if config.private_key.len() > 4096 { return Err(Error::new(ErrorKind::Other, "key too long")); } crate::string_to_bytes(&config.crypto_model, &mut crypto_cfg.crypto_model)?; crate::string_to_bytes(&config.crypto_cipher_type, &mut crypto_cfg.crypto_cipher_type)?; crate::string_to_bytes(&config.crypto_hash_type, &mut crypto_cfg.crypto_hash_type)?; unsafe { // NOTE param order is 'wrong-way round' from C copy_nonoverlapping(config.private_key.as_ptr(), crypto_cfg.private_key.as_mut_ptr(), config.private_key.len()); } crypto_cfg.private_key_len = config.private_key.len() as u32; let res = unsafe { ffi::knet_handle_crypto_set_config(handle.knet_handle as ffi::knet_handle_t, &mut crypto_cfg, config_num) }; if res == 0 { Ok(()) } else { if res == -2 { Err(Error::new(ErrorKind::Other, "Other cryto error")) } else { Err(Error::last_os_error()) } } } /// Whether to allow or disallow clear-text traffic when crypto is enabled with [handle_crypto_rx_clear_traffic] pub enum RxClearTraffic { Allow = 0, Disallow = 1, } /// Enable or disable clear-text traffic when crypto is enabled pub fn handle_crypto_rx_clear_traffic(handle: Handle, value: RxClearTraffic) -> Result<()> { let c_value : u8 = match value { RxClearTraffic::Allow => 0, RxClearTraffic::Disallow => 1 }; let res = unsafe { ffi::knet_handle_crypto_rx_clear_traffic(handle.knet_handle as ffi::knet_handle_t, c_value) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Tell knet which crypto settings to use pub fn handle_crypto_use_config(handle: Handle, config_num: u8) -> Result<()> { let res = unsafe { ffi::knet_handle_crypto_use_config(handle.knet_handle as ffi::knet_handle_t, config_num) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Set up packet compression pub fn handle_compress(handle: Handle, config: &CompressConfig) -> Result<()> { let mut compress_cfg = ffi::knet_handle_compress_cfg { compress_model: [0; 16], compress_threshold : config.compress_threshold, compress_level : config.compress_level }; if config.compress_model.len() > 16 { return Err(Error::new(ErrorKind::Other, "key too long")); } crate::string_to_bytes(&config.compress_model, &mut compress_cfg.compress_model)?; let res = unsafe { ffi::knet_handle_compress(handle.knet_handle as ffi::knet_handle_t, &mut compress_cfg) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Stats for the knet handle pub type HandleStats = ffi::knet_handle_stats; impl HandleStats { pub fn new() -> HandleStats { HandleStats { size: 0, tx_uncompressed_packets: 0, tx_compressed_packets: 0, tx_compressed_original_bytes: 0, tx_compressed_size_bytes: 0, tx_compress_time_ave: 0, tx_compress_time_min: 0, tx_compress_time_max: 0, tx_failed_to_compress: 0, tx_unable_to_compress: 0, rx_compressed_packets: 0, rx_compressed_original_bytes: 0, rx_compressed_size_bytes: 0, rx_compress_time_ave: 0, rx_compress_time_min: 0, rx_compress_time_max: 0, rx_failed_to_decompress: 0, tx_crypt_packets: 0, tx_crypt_byte_overhead: 0, tx_crypt_time_ave: 0, tx_crypt_time_min: 0, tx_crypt_time_max: 0, rx_crypt_packets: 0, rx_crypt_time_ave: 0, rx_crypt_time_min: 0, rx_crypt_time_max: 0, } } } impl Default for ffi::knet_handle_stats { fn default() -> Self { ffi::knet_handle_stats::new() } } impl fmt::Display for HandleStats { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}, ", self.tx_uncompressed_packets)?; write!(f, "{}, ", self.tx_compressed_packets)?; write!(f, "{}, ", self.tx_compressed_original_bytes)?; write!(f, "{}, ", self.tx_compressed_size_bytes)?; write!(f, "{}, ", self.tx_compress_time_ave)?; write!(f, "{}, ", self.tx_compress_time_min)?; write!(f, "{}, ", self.tx_compress_time_max)?; write!(f, "{}, ", self.tx_failed_to_compress)?; write!(f, "{}, ", self.tx_unable_to_compress)?; write!(f, "{}, ", self.rx_compressed_packets)?; write!(f, "{}, ", self.rx_compressed_original_bytes)?; write!(f, "{}, ", self.rx_compressed_size_bytes)?; write!(f, "{}, ", self.rx_compress_time_ave)?; write!(f, "{}, ", self.rx_compress_time_min)?; write!(f, "{}, ", self.rx_compress_time_max)?; write!(f, "{}, ", self.rx_failed_to_decompress)?; write!(f, "{}, ", self.tx_crypt_packets)?; write!(f, "{}, ", self.tx_crypt_byte_overhead)?; write!(f, "{}, ", self.tx_crypt_time_ave)?; write!(f, "{}, ", self.tx_crypt_time_min)?; write!(f, "{}, ", self.tx_crypt_time_max)?; write!(f, "{}, ", self.rx_crypt_packets)?; write!(f, "{}, ", self.rx_crypt_time_ave)?; write!(f, "{}, ", self.rx_crypt_time_min)?; write!(f, "{}, ", self.rx_crypt_time_max)?; Ok(()) } } /// Return statistics for this knet handle pub fn handle_get_stats(handle: Handle) -> Result { let (res, stats) = unsafe { let mut c_stats = HandleStats::new(); let res = ffi::knet_handle_get_stats(handle.knet_handle as ffi::knet_handle_t, &mut c_stats, size_of::()); (res, c_stats) }; if res == 0 { Ok(stats) } else { Err(Error::last_os_error()) } } /// Tell [handle_clear_stats] whether to cleat all stats or just handle stats pub enum ClearStats { Handle = 1, HandleAndLink = 2, } /// Clear statistics pub fn handle_clear_stats(handle: Handle, clear_options: ClearStats) -> Result<()> { let c_value : i32 = match clear_options { ClearStats::Handle => 1, ClearStats::HandleAndLink => 2 }; let res = unsafe { ffi::knet_handle_clear_stats(handle.knet_handle as ffi::knet_handle_t, c_value) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Crypto info returned from [get_crypto_list] pub struct CryptoInfo { pub name: String, pub properties: u8, // Unused } impl CryptoInfo { pub fn new(c_info: ffi::knet_crypto_info) -> CryptoInfo { let cstr = unsafe {CStr::from_ptr(c_info.name) }; let name = match cstr.to_str() { Ok(s) => s.to_string(), Err(e) => e.to_string(), }; CryptoInfo {properties: 0, name} } } /// Get a list of valid crypto options pub fn get_crypto_list() -> Result> { let mut list_entries: usize = 256; let mut c_list : [ffi::knet_crypto_info; 256] = [ ffi::knet_crypto_info{name: null(), properties: 0u8, pad:[0; 256]}; 256]; let res = unsafe { ffi::knet_get_crypto_list(&mut c_list[0], &mut list_entries) }; if res == 0 { let mut retvec = Vec::::new(); for i in c_list.iter().take(list_entries) { retvec.push(CryptoInfo::new(*i)); } Ok(retvec) } else { Err(Error::last_os_error()) } } /// Compressions types returned from [get_compress_list] pub struct CompressInfo { pub name: String, pub properties: u8, // Unused } impl CompressInfo { pub fn new(c_info: ffi::knet_compress_info) -> CompressInfo { let cstr = unsafe {CStr::from_ptr(c_info.name) }; let name = match cstr.to_str() { Ok(s) => s.to_string(), Err(e) => e.to_string(), }; CompressInfo {properties: 0, name} } } /// Return a list of compression options pub fn get_compress_list() -> Result> { let mut list_entries: usize = 256; let mut c_list : [ffi::knet_compress_info; 256] = [ ffi::knet_compress_info{name: null(), properties: 0u8, pad:[0; 256]}; 256]; let res = unsafe { ffi::knet_get_compress_list(&mut c_list[0], &mut list_entries) }; if res == 0 { let mut retvec = Vec::::new(); for i in c_list.iter().take(list_entries) { retvec.push(CompressInfo::new(*i)); } Ok(retvec) } else { Err(Error::last_os_error()) } } /// Enable callback when the onwire version for a node changes pub fn handle_enable_onwire_ver_notify(handle: Handle, private_data: u64, onwire_notify_fn: Option) -> Result<()> { // This looks a bit different to the other _enable*_notify calls because knet // calls the callback function in the API. Which results in a deadlock with our // own mutex if let Some(h) = HANDLE_HASH.lock().unwrap().get_mut(&(handle.knet_handle)) { h.onwire_notify_private_data = private_data; h.onwire_notify_fn = onwire_notify_fn; } else { return Err(Error::new(ErrorKind::Other, "Rust handle not found")); }; let res = match onwire_notify_fn { Some(_f) => unsafe { ffi::knet_handle_enable_onwire_ver_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, Some(rust_onwire_notify_fn)) }, None => unsafe { ffi::knet_handle_enable_onwire_ver_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, None) }, }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get the onsure version for a node pub fn handle_get_onwire_ver(handle: Handle, host_id: &HostId) -> Result<(u8,u8,u8)> { let mut onwire_min_ver = 0u8; let mut onwire_max_ver = 0u8; let mut onwire_ver = 0u8; let res = unsafe { ffi::knet_handle_get_onwire_ver(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, &mut onwire_min_ver, &mut onwire_max_ver, &mut onwire_ver) }; if res == 0 { Ok((onwire_min_ver, onwire_max_ver, onwire_ver)) } else { Err(Error::last_os_error()) } } /// Set the onsire version for this node pub fn handle_set_onwire_ver(handle: Handle, onwire_ver: u8) -> Result<()> { let res = unsafe { ffi::knet_handle_set_onwire_ver(handle.knet_handle as ffi::knet_handle_t, onwire_ver) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Set the reconnect interval. pub fn handle_set_transport_reconnect_interval(handle: Handle, msecs: u32) -> Result<()> { let res = unsafe { ffi::knet_handle_set_transport_reconnect_interval(handle.knet_handle as ffi::knet_handle_t, msecs) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get the reconnect interval. pub fn handle_get_transport_reconnect_interval(handle: Handle) -> Result { let mut msecs = 0u32; let res = unsafe { ffi::knet_handle_get_transport_reconnect_interval(handle.knet_handle as ffi::knet_handle_t, &mut msecs) }; if res == 0 { Ok(msecs) } else { Err(Error::last_os_error()) } } /// Add a new host ID pub fn host_add(handle: Handle, host_id: &HostId) -> Result<()> { let res = unsafe { ffi::knet_host_add(handle.knet_handle as ffi::knet_handle_t, host_id.host_id) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Remove a Host ID pub fn host_remove(handle: Handle, host_id: &HostId) -> Result<()> { let res = unsafe { ffi::knet_host_remove(handle.knet_handle as ffi::knet_handle_t, host_id.host_id) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Set the name of a host pub fn host_set_name(handle: Handle, host_id: &HostId, name: &str) -> Result<()> { let c_name = CString::new(name)?; let res = unsafe { ffi::knet_host_set_name(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, c_name.as_ptr()) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } const KNET_MAX_HOST_LEN:usize = 256; const KNET_MAX_PORT_LEN:usize = 6; /// Retrieve the name of a host given its ID pub fn host_get_name_by_host_id(handle: Handle, host_id: &HostId) -> Result { let mut c_name: [c_char; KNET_MAX_HOST_LEN] = [0; KNET_MAX_HOST_LEN]; let res = unsafe { ffi::knet_host_get_name_by_host_id(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, c_name.as_mut_ptr()) }; if res == 0 { crate::string_from_bytes(c_name.as_ptr(), KNET_MAX_HOST_LEN) } else { Err(Error::last_os_error()) } } /// Return the ID of a host given its name pub fn host_get_id_by_host_name(handle: Handle, name: &str) -> Result { let c_name = CString::new(name)?; let mut c_host_id = 0u16; let res = unsafe { ffi::knet_host_get_id_by_host_name(handle.knet_handle as ffi::knet_handle_t, c_name.as_ptr(), &mut c_host_id) }; if res == 0 { Ok(HostId{host_id: c_host_id}) } else { Err(Error::last_os_error()) } } const KNET_MAX_HOST: usize = 65536; /// Return a list of host IDs known to this handle pub fn host_get_host_list(handle: Handle) -> Result> { let mut c_host_ids: [u16; KNET_MAX_HOST] = [0; KNET_MAX_HOST]; let mut c_host_ids_entries: usize = 0; let res = unsafe { ffi::knet_host_get_host_list(handle.knet_handle as ffi::knet_handle_t, &mut c_host_ids[0], &mut c_host_ids_entries) }; if res == 0 { let mut host_vec = Vec::::new(); for i in c_host_ids.iter().take(c_host_ids_entries) { host_vec.push(HostId {host_id: *i}); } Ok(host_vec) } else { Err(Error::last_os_error()) } } /// Link Policies for [host_set_policy] #[derive(Copy, Clone, PartialEq, Eq)] pub enum LinkPolicy { Passive, Active, Rr, } impl LinkPolicy{ pub fn new(value: u8) -> LinkPolicy { match value { 2 => LinkPolicy::Rr, 1 => LinkPolicy::Active, _ => LinkPolicy::Passive, } } pub fn to_u8(self: LinkPolicy) -> u8 { match self { LinkPolicy::Passive => 0, LinkPolicy::Active => 1, LinkPolicy::Rr => 2, } } } impl fmt::Display for LinkPolicy { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { LinkPolicy::Passive => write!(f, "Passive"), LinkPolicy::Active => write!(f, "Active"), LinkPolicy::Rr => write!(f, "RR"), } } } /// Set the policy for this host, this only makes sense if multiple links between hosts are configured pub fn host_set_policy(handle: Handle, host_id: &HostId, policy: LinkPolicy) -> Result<()> { let c_value: u8 = policy.to_u8(); let res = unsafe { ffi::knet_host_set_policy(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, c_value) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Return the current link policy for a node pub fn host_get_policy(handle: Handle, host_id: &HostId) -> Result { let mut c_value: u8 = 0; let res = unsafe { ffi::knet_host_get_policy(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, &mut c_value) }; if res == 0 { Ok(LinkPolicy::new(c_value)) } else { Err(Error::last_os_error()) } } /// Current status of a host. remote & reachable are current not used pub struct HostStatus { pub reachable: bool, pub remote: bool, pub external: bool, } impl fmt::Display for HostStatus { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "reachable: {}, ", self.reachable)?; write!(f, "remote: {}, ", self.remote)?; write!(f, "external: {}", self.external)?; Ok(()) } } /// Return the current status of a host pub fn host_get_status(handle: Handle, host_id: &HostId) -> Result { let mut c_value = ffi::knet_host_status { reachable:0, remote:0, external:0}; let res = unsafe { ffi::knet_host_get_status(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, &mut c_value) }; if res == 0 { Ok(HostStatus { reachable: crate::u8_to_bool(c_value.reachable), remote: crate::u8_to_bool(c_value.remote), external: crate::u8_to_bool(c_value.external) }) } else { Err(Error::last_os_error()) } } /// Enable callbacks when the status of a host changes pub fn host_enable_status_change_notify(handle: Handle, private_data: u64, host_status_change_notify_fn: Option) -> Result<()> { if let Some(h) = HANDLE_HASH.lock().unwrap().get_mut(&(handle.knet_handle)) { h.host_status_change_notify_private_data = private_data; h.host_status_change_notify_fn = host_status_change_notify_fn; let res = match host_status_change_notify_fn { Some(_f) => unsafe { ffi::knet_host_enable_status_change_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, Some(rust_host_status_change_notify_fn)) }, None => unsafe { ffi::knet_host_enable_status_change_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, None) }, }; if res == 0 { return Ok(()); } else { return Err(Error::last_os_error()); } } Err(Error::new(ErrorKind::Other, "Rust handle not found")) } /// Transport types supported in knet pub enum TransportId { Loopback, Udp, Sctp, } impl TransportId { pub fn new(id: u8) -> TransportId { match id { 2 => TransportId::Sctp, 1 => TransportId::Udp, _ => TransportId::Loopback, } } pub fn to_u8(self: &TransportId) -> u8 { match self { TransportId::Loopback => 0, TransportId::Udp => 1, TransportId::Sctp => 2, } } pub fn to_string(self: &TransportId) -> String { match self { TransportId::Udp => "UDP".to_string(), TransportId::Sctp => "SCTP".to_string(), TransportId::Loopback => "Loopback".to_string() } } pub fn from_string(name: String) -> TransportId { match name.as_str() { "UDP" => TransportId::Udp, "SCTP" => TransportId::Sctp, "Loopback" => TransportId::Loopback, _ => TransportId::Loopback, } } } /// Transport info returned from [get_transport_list] pub struct TransportInfo { pub name: String, pub id: TransportId, pub properties: u8, // currently unused } // Controversially implementing name_by_id and id_by_name here impl TransportInfo { pub fn new(c_info: ffi::knet_transport_info) -> TransportInfo { let cstr = unsafe {CStr::from_ptr(c_info.name) }; let name = match cstr.to_str() { Ok(s) => s.to_string(), Err(e) => e.to_string(), }; TransportInfo {properties: 0, id: TransportId::new(c_info.id), name} } } pub fn get_transport_list() -> Result> { let mut list_entries: usize = 256; let mut c_list : [ffi::knet_transport_info; 256] = [ ffi::knet_transport_info{name: null(), id: 0u8, properties: 0u8, pad:[0; 256]}; 256]; let res = unsafe { ffi::knet_get_transport_list(&mut c_list[0], &mut list_entries) }; if res == 0 { let mut retvec = Vec::::new(); for i in c_list.iter().take(list_entries) { retvec.push(TransportInfo::new(*i)); } Ok(retvec) } else { Err(Error::last_os_error()) } } /// Configure a link to a host ID. dst_addr may be None for a dynamic link. pub fn link_set_config(handle: Handle, host_id: &HostId, link_id: u8, transport: TransportId, src_addr: &SocketAddr, dst_addr: Option<&SocketAddr>, flags: LinkFlags) -> Result<()> { // Not really mut, but C is dumb let mut c_srcaddr = make_new_sockaddr_storage(src_addr); // dst_addr can be NULL/None if this is a dynamic link let res = if let Some(dst) = dst_addr { let mut c_dstaddr = make_new_sockaddr_storage(dst); unsafe { ffi::knet_link_set_config(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, transport.to_u8(), &mut c_srcaddr, &mut c_dstaddr, flags.bits) } } else { unsafe { ffi::knet_link_set_config(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, transport.to_u8(), &mut c_srcaddr, null_mut(), flags.bits) } }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Return a link's configuration pub fn link_get_config(handle: Handle, host_id: &HostId, link_id: u8) -> Result<(TransportId, Option, Option, LinkFlags)> { let mut c_srcaddr = OsSocketAddr::new(); let mut c_dstaddr = OsSocketAddr::new(); let mut c_transport = 0u8; let mut c_flags = 0u64; let mut c_dynamic = 0u8; let res = unsafe { ffi::knet_link_get_config(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, &mut c_transport, c_srcaddr.as_mut_ptr() as *mut ffi::sockaddr_storage, c_dstaddr.as_mut_ptr() as *mut ffi::sockaddr_storage, &mut c_dynamic, &mut c_flags) }; if res == 0 { let r_transport = TransportId::new(c_transport); Ok((r_transport, c_srcaddr.into(), c_dstaddr.into(), LinkFlags{bits:c_flags})) } else { Err(Error::last_os_error()) } } /// Clear a link configuration. pub fn link_clear_config(handle: Handle, host_id: &HostId, link_id: u8) -> Result<()> { let res = unsafe { ffi::knet_link_clear_config(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Type of ACL pub enum AclAcceptReject { Accept, Reject, } impl AclAcceptReject { pub fn new(ar: u32) -> AclAcceptReject { match ar { ffi::CHECK_ACCEPT => AclAcceptReject::Accept, ffi::CHECK_REJECT => AclAcceptReject::Reject, _ => AclAcceptReject::Reject, } } pub fn to_u32(self: &AclAcceptReject) -> u32 { match self { AclAcceptReject::Accept => ffi::CHECK_ACCEPT, AclAcceptReject::Reject => ffi::CHECK_REJECT, } } } /// What the ACL should check pub enum AclCheckType { Address, Mask, Range, } impl AclCheckType { pub fn new(ct: u32) -> AclCheckType { match ct { ffi::CHECK_TYPE_ADDRESS => AclCheckType::Address, ffi::CHECK_TYPE_MASK => AclCheckType::Mask, ffi::CHECK_TYPE_RANGE => AclCheckType::Range, _ => AclCheckType::Address, } } pub fn to_u32(self: &AclCheckType) -> u32 { match self { AclCheckType::Address => ffi::CHECK_TYPE_ADDRESS, AclCheckType::Mask => ffi::CHECK_TYPE_MASK, AclCheckType::Range => ffi::CHECK_TYPE_RANGE, } } } // We need to have a zeroed-out stackaddr storage to pass to the ACL APIs // as knet compares the whole sockaddr_storage when using knet_rm_acl() fn make_new_sockaddr_storage(ss: &SocketAddr) -> ffi::sockaddr_storage { // A blank one let mut new_ss = ffi::sockaddr_storage { ss_family: 0, __ss_padding: [0; 118], __ss_align: 0, }; let p_new_ss : *mut ffi::sockaddr_storage = &mut new_ss; // Rust only fills in what it thinks is necessary let c_ss : OsSocketAddr = (*ss).into(); // Copy it unsafe { // Only copy as much as is in the OsSocketAddr copy_nonoverlapping(c_ss.as_ptr(), p_new_ss as *mut libc::sockaddr, 1); } new_ss } /// Add an ACL to a link, adds the ACL to the end of the list. pub fn link_add_acl(handle: Handle, host_id: &HostId, link_id: u8, ss1: &SocketAddr, ss2: &SocketAddr, check_type: AclCheckType, acceptreject: AclAcceptReject) -> Result<()> { // Not really mut, but C is dumb let mut c_ss1 = make_new_sockaddr_storage(ss1); let mut c_ss2 = make_new_sockaddr_storage(ss2); let res = unsafe { ffi::knet_link_add_acl(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, &mut c_ss1, &mut c_ss2, check_type.to_u32(), acceptreject.to_u32()) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Insert an ACL anywhere in the ACL list for this host/link pub fn link_insert_acl(handle: Handle, host_id: &HostId, link_id: u8, index: i32, ss1: &SocketAddr, ss2: &SocketAddr, check_type: AclCheckType, acceptreject: AclAcceptReject) -> Result<()> { // Not really mut, but C is dumb let mut c_ss1 = make_new_sockaddr_storage(ss1); let mut c_ss2 = make_new_sockaddr_storage(ss2); let res = unsafe { ffi::knet_link_insert_acl(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, index, &mut c_ss1, &mut c_ss2, check_type.to_u32(), acceptreject.to_u32()) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Remove an ACL for this host/link pub fn link_rm_acl(handle: Handle, host_id: &HostId, link_id: u8, ss1: &SocketAddr, ss2: &SocketAddr, check_type: AclCheckType, acceptreject: AclAcceptReject) -> Result<()> { // Not really mut, but C is dumb let mut c_ss1 = make_new_sockaddr_storage(ss1); let mut c_ss2 = make_new_sockaddr_storage(ss2); let res = unsafe { ffi::knet_link_rm_acl(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, &mut c_ss1, &mut c_ss2, check_type.to_u32(), acceptreject.to_u32()) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Clear out all ACLs from this host/link pub fn link_clear_acl(handle: Handle, host_id: &HostId, link_id: u8) -> Result<()> { let res = unsafe { ffi::knet_link_clear_acl(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Enable/disable a link (you still need to call [handle_setfwd] for traffic to flow pub fn link_set_enable(handle: Handle, host_id: &HostId, link_id: u8, enable: bool) -> Result<()> { let res = unsafe { ffi::knet_link_set_enable(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, enable as u32) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get the 'enabled' status for a link pub fn link_get_enable(handle: Handle, host_id: &HostId, link_id: u8) -> Result { let mut c_enable = 0u32; let res = unsafe { ffi::knet_link_get_enable(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, &mut c_enable) }; if res == 0 { Ok(crate::u32_to_bool(c_enable)) } else { Err(Error::last_os_error()) } } /// Set the ping timers for a link pub fn link_set_ping_timers(handle: Handle, host_id: &HostId, link_id: u8, interval: i64, timeout: i64, precision: u32) -> Result<()> { let res = unsafe { ffi::knet_link_set_ping_timers(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, interval, timeout, precision) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get the ping timers for a link pub fn link_get_ping_timers(handle: Handle, host_id: &HostId, link_id: u8) -> Result<(i64, i64, u32)> { let mut c_interval : ffi::time_t = 0; let mut c_timeout : ffi::time_t = 0; let mut c_precision = 0u32; let res = unsafe { ffi::knet_link_get_ping_timers(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, &mut c_interval, &mut c_timeout, &mut c_precision) }; if res == 0 { Ok((c_interval as i64, c_timeout as i64, c_precision)) } else { Err(Error::last_os_error()) } } /// Set the pong count for a link pub fn link_set_pong_count(handle: Handle, host_id: &HostId, link_id: u8, pong_count: u8) -> Result<()> { let res = unsafe { ffi::knet_link_set_pong_count(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, pong_count) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get the pong count for a link pub fn link_get_pong_count(handle: Handle, host_id: &HostId, link_id: u8) -> Result { let mut c_pong_count = 0u8; let res = unsafe { ffi::knet_link_get_pong_count(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, &mut c_pong_count) }; if res == 0 { Ok(c_pong_count) } else { Err(Error::last_os_error()) } } /// Set the link priority (only useful with multiple links to a node) pub fn link_set_priority(handle: Handle, host_id: &HostId, link_id: u8, priority: u8) -> Result<()> { let res = unsafe { ffi::knet_link_set_priority(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, priority) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get the link priority pub fn link_get_priority(handle: Handle, host_id: &HostId, link_id: u8) -> Result { let mut c_priority = 0u8; let res = unsafe { ffi::knet_link_get_priority(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, &mut c_priority) }; if res == 0 { Ok(c_priority) } else { Err(Error::last_os_error()) } } const KNET_MAX_LINK: usize = 8; /// Get a list of links for this host pub fn link_get_link_list(handle: Handle, host_id: &HostId) -> Result> { let mut c_link_ids: [u8; KNET_MAX_LINK] = [0; KNET_MAX_LINK]; let mut c_link_ids_entries: usize = 0; let res = unsafe { ffi::knet_link_get_link_list(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, &mut c_link_ids[0], &mut c_link_ids_entries) }; if res == 0 { let mut link_vec = Vec::::new(); for i in c_link_ids.iter().take(c_link_ids_entries) { link_vec.push(*i); } Ok(link_vec) } else { Err(Error::last_os_error()) } } /// Enable callbacks when a link status changes pub fn link_enable_status_change_notify(handle: Handle, private_data: u64, link_status_change_notify_fn: Option) -> Result<()> { if let Some(h) = HANDLE_HASH.lock().unwrap().get_mut(&(handle.knet_handle)) { h.link_status_change_notify_private_data = private_data; h.link_status_change_notify_fn = link_status_change_notify_fn; let res = match link_status_change_notify_fn { Some(_f) => unsafe { ffi::knet_link_enable_status_change_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, Some(rust_link_status_change_notify_fn)) }, None => unsafe { ffi::knet_link_enable_status_change_notify(handle.knet_handle as ffi::knet_handle_t, handle.knet_handle as *mut c_void, None) }, }; if res == 0 { return Ok(()); } else { return Err(Error::last_os_error()); } } Err(Error::new(ErrorKind::Other, "Rust handle not found")) } /// Link stats pub struct LinkStats { pub tx_data_packets: u64, pub rx_data_packets: u64, pub tx_data_bytes: u64, pub rx_data_bytes: u64, pub rx_ping_packets: u64, pub tx_ping_packets: u64, pub rx_ping_bytes: u64, pub tx_ping_bytes: u64, pub rx_pong_packets: u64, pub tx_pong_packets: u64, pub rx_pong_bytes: u64, pub tx_pong_bytes: u64, pub rx_pmtu_packets: u64, pub tx_pmtu_packets: u64, pub rx_pmtu_bytes: u64, pub tx_pmtu_bytes: u64, pub tx_total_packets: u64, pub rx_total_packets: u64, pub tx_total_bytes: u64, pub rx_total_bytes: u64, pub tx_total_errors: u64, pub tx_total_retries: u64, pub tx_pmtu_errors: u32, pub tx_pmtu_retries: u32, pub tx_ping_errors: u32, pub tx_ping_retries: u32, pub tx_pong_errors: u32, pub tx_pong_retries: u32, pub tx_data_errors: u32, pub tx_data_retries: u32, pub latency_min: u32, pub latency_max: u32, pub latency_ave: u32, pub latency_samples: u32, pub down_count: u32, pub up_count: u32, pub last_up_times: Vec, pub last_down_times: Vec, } // Quick & Dirty printing impl fmt::Display for LinkStats { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}, ", self.tx_data_packets)?; write!(f, "{}, ", self.rx_data_packets)?; write!(f, "{}, ", self.tx_data_bytes)?; write!(f, "{}, ", self.rx_data_bytes)?; write!(f, "{}, ", self.rx_ping_packets)?; write!(f, "{}, ", self.tx_ping_packets)?; write!(f, "{}, ", self.rx_ping_bytes)?; write!(f, "{}, ", self.tx_ping_bytes)?; write!(f, "{}, ", self.rx_pong_packets)?; write!(f, "{}, ", self.tx_pong_packets)?; write!(f, "{}, ", self.rx_pong_bytes)?; write!(f, "{}, ", self.tx_pong_bytes)?; write!(f, "{}, ", self.rx_pmtu_packets)?; write!(f, "{}, ", self.tx_pmtu_packets)?; write!(f, "{}, ", self.rx_pmtu_bytes)?; write!(f, "{}, ", self.tx_pmtu_bytes)?; write!(f, "{}, ", self.tx_total_packets)?; write!(f, "{}, ", self.rx_total_packets)?; write!(f, "{}, ", self.tx_total_bytes)?; write!(f, "{}, ", self.rx_total_bytes)?; write!(f, "{}, ", self.tx_total_errors)?; write!(f, "{}, ", self.tx_total_retries)?; write!(f, "{}, ", self.tx_pmtu_errors)?; write!(f, "{}, ", self.tx_pmtu_retries)?; write!(f, "{}, ", self.tx_ping_errors)?; write!(f, "{}, ", self.tx_ping_retries)?; write!(f, "{}, ", self.tx_pong_errors)?; write!(f, "{}, ", self.tx_pong_retries)?; write!(f, "{}, ", self.tx_data_errors)?; write!(f, "{}, ", self.tx_data_retries)?; write!(f, "{}, ", self.latency_min)?; write!(f, "{}, ", self.latency_max)?; write!(f, "{}, ", self.latency_ave)?; write!(f, "{}, ", self.latency_samples)?; write!(f, "{}, ", self.down_count)?; write!(f, "{}, ", self.up_count)?; write!(f, "Last up times: ")?; // There's no sensible print for SystemTime in the std library // and I don't want to add dependancies here for printing as it // mostly going to be the client's responsibility, so use the Debug option for i in &self.last_up_times { write!(f, "{:?}", i)?; } write!(f, " Last down times: ")?; for i in &self.last_down_times { write!(f, "{:?}", i)?; } Ok(()) } } // I wish this all wasn't necessary! impl ffi::knet_link_stats { pub fn new() -> ffi::knet_link_stats { ffi::knet_link_stats { tx_data_packets: 0, rx_data_packets: 0, tx_data_bytes: 0, rx_data_bytes: 0, rx_ping_packets: 0, tx_ping_packets: 0, rx_ping_bytes: 0, tx_ping_bytes: 0, rx_pong_packets: 0, tx_pong_packets: 0, rx_pong_bytes: 0, tx_pong_bytes: 0, rx_pmtu_packets: 0, tx_pmtu_packets: 0, rx_pmtu_bytes: 0, tx_pmtu_bytes: 0, tx_total_packets: 0, rx_total_packets: 0, tx_total_bytes: 0, rx_total_bytes: 0, tx_total_errors: 0, tx_total_retries: 0, tx_pmtu_errors: 0, tx_pmtu_retries: 0, tx_ping_errors: 0, tx_ping_retries: 0, tx_pong_errors: 0, tx_pong_retries: 0, tx_data_errors: 0, tx_data_retries: 0, latency_min: 0, latency_max: 0, latency_ave: 0, latency_samples: 0, down_count: 0, up_count: 0, last_up_times: [0; 16], last_down_times: [0; 16], last_up_time_index: 0, last_down_time_index: 0, } } } impl Default for ffi::knet_link_stats { fn default() -> Self { ffi::knet_link_stats::new() } } impl ffi::knet_link_status { pub fn new()-> ffi::knet_link_status { ffi::knet_link_status { size: 0, src_ipaddr : [0; KNET_MAX_HOST_LEN], dst_ipaddr : [0; KNET_MAX_HOST_LEN], src_port : [0; KNET_MAX_PORT_LEN], dst_port : [0; KNET_MAX_PORT_LEN], enabled: 0, connected: 0, dynconnected: 0, pong_last: ffi::timespec{ tv_sec: 0, tv_nsec: 0}, mtu: 0, proto_overhead: 0, stats: ffi::knet_link_stats::new(), } } } impl Default for ffi::knet_link_status { fn default() -> Self { ffi::knet_link_status::new() } } /// Link status (includes a [LinkStats]) pub struct LinkStatus { pub src_ipaddr: String, pub dst_ipaddr: String, pub src_port: String, pub dst_port: String, pub enabled: bool, pub connected: bool, pub dynconnected: bool, pub pong_last: SystemTime, pub mtu: u32, pub proto_overhead: u32, pub stats: LinkStats, } impl LinkStatus { pub fn new(c_stats: ffi::knet_link_status) -> LinkStatus { LinkStatus { src_ipaddr : crate::string_from_bytes_safe(c_stats.src_ipaddr.as_ptr(), KNET_MAX_HOST_LEN), src_port : crate::string_from_bytes_safe(c_stats.src_port.as_ptr(), KNET_MAX_HOST_LEN), dst_ipaddr : crate::string_from_bytes_safe(c_stats.dst_ipaddr.as_ptr(), KNET_MAX_HOST_LEN), dst_port : crate::string_from_bytes_safe(c_stats.dst_port.as_ptr(), KNET_MAX_HOST_LEN), enabled : crate::u8_to_bool(c_stats.enabled), connected : crate::u8_to_bool(c_stats.connected), dynconnected : crate::u8_to_bool(c_stats.dynconnected), pong_last : systemtime_from_timespec(c_stats.pong_last), mtu : c_stats.mtu, proto_overhead : c_stats.proto_overhead, stats : LinkStats::new(c_stats.stats), } } } impl fmt::Display for LinkStatus { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "src_ip_addr: {}:{}, ", self.src_ipaddr, self.src_port)?; write!(f, "dst_ip_addr: {}:{}, ", self.dst_ipaddr, self.src_port)?; write!(f, "enabled: {}, connected: {}, mtu: {}, overhead: {}, ", self.enabled, self.connected, self.mtu, self.proto_overhead)?; write!(f, "stats: {}", self.stats)?; Ok(()) } } fn systemtime_from_time_t(t: u64) -> SystemTime { SystemTime::UNIX_EPOCH+Duration::from_secs(t) } fn systemtime_from_timespec(t: ffi::timespec) -> SystemTime { SystemTime::UNIX_EPOCH+Duration::from_secs(t.tv_sec as u64) +Duration::from_nanos(t.tv_nsec as u64) // TODO may panic?? } fn copy_circular_buffer_of_link_events(num: usize, times: &[ffi::time_t]) -> Vec { let mut times_vec = Vec::::new(); for index in (0 .. num).rev() { if times[index] == 0 { break } times_vec.push(systemtime_from_time_t(times[index] as u64)); // TODO may panic?? } for index in (num+1 .. MAX_LINK_EVENTS).rev() { if times[index] == 0 { break; } times_vec.push(systemtime_from_time_t(times[index] as u64)); // TODO may panic?? } times_vec } const MAX_LINK_EVENTS: usize = 16; impl LinkStats { pub fn new(cstats: ffi::knet_link_stats) -> LinkStats { let up_times = copy_circular_buffer_of_link_events(cstats.last_up_time_index as usize, &cstats.last_up_times); let down_times = copy_circular_buffer_of_link_events(cstats.last_down_time_index as usize, &cstats.last_down_times); LinkStats { tx_data_packets: cstats.tx_data_packets, rx_data_packets: cstats.rx_data_packets, tx_data_bytes: cstats.tx_data_bytes, rx_data_bytes: cstats.rx_data_bytes, rx_ping_packets: cstats.rx_ping_packets, tx_ping_packets: cstats.tx_ping_packets, rx_ping_bytes: cstats.rx_ping_bytes, tx_ping_bytes: cstats.tx_ping_bytes, rx_pong_packets: cstats.rx_pong_packets, tx_pong_packets: cstats.tx_pong_packets, rx_pong_bytes: cstats.rx_pong_bytes, tx_pong_bytes: cstats.tx_pong_bytes, rx_pmtu_packets: cstats.rx_pmtu_packets, tx_pmtu_packets: cstats.tx_pmtu_packets, rx_pmtu_bytes: cstats.rx_pmtu_bytes, tx_pmtu_bytes: cstats.tx_pmtu_bytes, tx_total_packets: cstats.tx_total_packets, rx_total_packets: cstats.rx_total_packets, tx_total_bytes: cstats.tx_total_bytes, rx_total_bytes: cstats.rx_total_bytes, tx_total_errors: cstats.tx_total_errors, tx_total_retries: cstats.tx_total_retries, tx_pmtu_errors: cstats.tx_pmtu_errors, tx_pmtu_retries: cstats.tx_pmtu_retries, tx_ping_errors: cstats.tx_ping_errors, tx_ping_retries: cstats.tx_ping_retries, tx_pong_errors: cstats.tx_pong_errors, tx_pong_retries: cstats.tx_pong_retries, tx_data_errors: cstats.tx_data_errors, tx_data_retries: cstats.tx_data_retries, latency_min: cstats.latency_min, latency_max: cstats.latency_max, latency_ave: cstats.latency_ave, latency_samples: cstats.latency_samples, down_count: cstats.down_count, up_count: cstats.up_count, last_up_times: up_times, last_down_times: down_times, } } } /// Get the status (and stats) of a link pub fn link_get_status(handle: Handle, host_id: &HostId, link_id: u8) -> Result { let (res, stats) = unsafe { let mut c_stats : ffi::knet_link_status = ffi::knet_link_status::new(); let res = ffi::knet_link_get_status(handle.knet_handle as ffi::knet_handle_t, host_id.host_id, link_id, &mut c_stats, size_of::()); (res, c_stats) }; if res == 0 { let r_status = LinkStatus::new(stats); Ok(r_status) } else { Err(Error::last_os_error()) } } /// Get the logging subsystem ID given its name pub fn log_get_subsystem_id(name: &str) -> Result { let c_name = CString::new(name)?; let res = unsafe { ffi::knet_log_get_subsystem_id(c_name.as_ptr()) }; Ok(res) } /// Get the logging subsystem name given its ID pub fn log_get_subsystem_name(id: u8) -> Result { let res = unsafe { ffi::knet_log_get_subsystem_name(id) }; crate::string_from_bytes(res, 256) } /// Get the name of a logging level pub fn log_get_loglevel_id(name: &str) -> Result { let c_name = CString::new(name)?; let res = unsafe { ffi::knet_log_get_loglevel_id(c_name.as_ptr()) }; Ok(res) } /// Get the ID of a logging level, given its name pub fn log_get_loglevel_name(id: u8) -> Result { let res = unsafe { ffi::knet_log_get_loglevel_name(id) }; crate::string_from_bytes(res, 256) } /// Logging levels pub enum LogLevel { Err, Warn, Info, Debug, } impl LogLevel { pub fn new(level: u8) -> LogLevel { match level { 0 => LogLevel::Err, 1 => LogLevel::Warn, 2 => LogLevel::Info, _ => LogLevel::Debug, // 3=Debug, but default anything to it too } } pub fn to_u8(self: &LogLevel) -> u8 { match self { LogLevel::Err => 0, LogLevel::Warn => 1, LogLevel::Info => 2, LogLevel::Debug => 3, } } } impl fmt::Display for LogLevel { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { LogLevel::Err => write!(f, "Err"), LogLevel::Warn => write!(f, "Warn"), LogLevel::Info => write!(f, "Info"), LogLevel::Debug => write!(f, "Debug"), } } } /// Subsystems known to the knet logger pub enum SubSystem { Common, Handle, Host, Listener, Link, Transport, Crypto, Compress, Filter, Dstcache, Heartbeat, Pmtud, Tx, Rx, TranspBase, TranspLoopback, TranspUdp, TranspSctp, NssCrypto, OpensslCrypto, Zlibcomp, Lz4comp, Lz4hccomp, Lzo2comp, Lzmacomp, Bzip2comp, Zstdcomp, Unknown, } impl SubSystem { pub fn to_u8(self: &SubSystem) -> u8 { match self { SubSystem::Common => ffi::KNET_SUB_COMMON, SubSystem::Handle => ffi::KNET_SUB_HANDLE, SubSystem::Host => ffi::KNET_SUB_HOST, SubSystem::Listener => ffi::KNET_SUB_LISTENER, SubSystem::Link => ffi::KNET_SUB_LINK, SubSystem::Transport => ffi::KNET_SUB_TRANSPORT, SubSystem::Crypto => ffi::KNET_SUB_CRYPTO, SubSystem::Compress => ffi::KNET_SUB_COMPRESS, SubSystem::Filter => ffi::KNET_SUB_FILTER, SubSystem::Dstcache => ffi::KNET_SUB_DSTCACHE, SubSystem::Heartbeat => ffi::KNET_SUB_HEARTBEAT, SubSystem::Pmtud => ffi::KNET_SUB_PMTUD, SubSystem::Tx => ffi::KNET_SUB_TX, SubSystem::Rx => ffi::KNET_SUB_RX, SubSystem::TranspBase => ffi::KNET_SUB_TRANSP_BASE, SubSystem::TranspLoopback => ffi::KNET_SUB_TRANSP_LOOPBACK, SubSystem::TranspUdp => ffi::KNET_SUB_TRANSP_UDP, SubSystem::TranspSctp => ffi::KNET_SUB_TRANSP_SCTP, SubSystem::NssCrypto => ffi::KNET_SUB_NSSCRYPTO, SubSystem::OpensslCrypto => ffi::KNET_SUB_OPENSSLCRYPTO, SubSystem::Zlibcomp => ffi::KNET_SUB_ZLIBCOMP, SubSystem::Lz4comp => ffi::KNET_SUB_LZ4COMP, SubSystem::Lz4hccomp => ffi::KNET_SUB_LZ4HCCOMP, SubSystem::Lzo2comp => ffi::KNET_SUB_LZO2COMP, SubSystem::Lzmacomp => ffi::KNET_SUB_LZMACOMP, SubSystem::Bzip2comp => ffi::KNET_SUB_BZIP2COMP, SubSystem::Zstdcomp => ffi::KNET_SUB_ZSTDCOMP, SubSystem::Unknown => ffi::KNET_SUB_UNKNOWN, } } pub fn new(subsys: u8) -> SubSystem { match subsys { 1 => SubSystem::Unknown, 2 => SubSystem::Unknown, _ => SubSystem::Unknown, } } } /// Set the current logging level pub fn log_set_loglevel(handle: Handle, subsystem: SubSystem, level: LogLevel) -> Result<()> { let c_level = level.to_u8(); let c_subsys = subsystem.to_u8(); let res = unsafe { ffi::knet_log_set_loglevel(handle.knet_handle as ffi::knet_handle_t, c_subsys, c_level) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Get the current logging level pub fn log_get_loglevel(handle: Handle, subsystem: SubSystem) -> Result { let mut c_level:u8 = 0; let c_subsys = subsystem.to_u8(); let res = unsafe { ffi::knet_log_get_loglevel(handle.knet_handle as ffi::knet_handle_t, c_subsys, &mut c_level) }; if res == 0 { Ok(LogLevel::new(c_level)) } else { Err(Error::last_os_error()) } } diff --git a/libknet/bindings/rust/src/lib.rs b/libknet/bindings/rust/src/lib.rs index afe27b8c..77d4717f 100644 --- a/libknet/bindings/rust/src/lib.rs +++ b/libknet/bindings/rust/src/lib.rs @@ -1,165 +1,165 @@ -// Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +// Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. // // Authors: Christine Caulfield // // This software licensed under LGPL-2.0+ // //! This crate provides access to the kronosnet library 'libknet' //! from Rust. They are a fairly thin layer around the actual API calls but with Rust data types //! and iterators. //! //! No more information about knet itself will be provided here, it is expected that if //! you feel you need access to the knet API calls, you know what they do :) //! //! # Example //! ``` //! use knet_bindings::knet_bindings as knet; //! use std::net::{SocketAddr, IpAddr, Ipv4Addr}; //! use std::thread::spawn; //! use std::sync::mpsc::Receiver; //! use std::sync::mpsc::channel; //! use std::io::{Result, ErrorKind, Error}; //! use std::{thread, time}; //! //! const CHANNEL: i8 = 1; //! //! pub fn main() -> Result<()> //! { //! let host_id = knet::HostId::new(1); //! let other_host_id = knet::HostId::new(2); //! //! let (log_sender, log_receiver) = channel::(); //! spawn(move || logging_thread(log_receiver)); //! //! let knet_handle = match knet::handle_new(&our_hostid, Some(log_sender), //! knet::LogLevel::Debug, knet::HandleFlags::NONE) { //! Ok(h) => h, //! Err(e) => { //! return Err(e); //! } //! }; //! //! if let Err(e) = knet::host_add(knet_handle, &other_hostid) { //! return Err(e); //! } //! if let Err(e) = knet::link_set_config(knet_handle, &other_hostid, 0, //! knet::TransportId::Udp, //! &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000+(our_hostid.to_u16())), //! &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000+(other_hostid.to_u16())), //! knet::LinkFlags::NONE) { //! return Err(e); //! } //! if let Err(e) = knet::handle_add_datafd(knet_handle, 0, CHANNEL) { //! return Err(e); //! } //! //! if let Err(e) = knet::handle_crypto_rx_clear_traffic(knet_handle, knet::RxClearTraffic::Allow) { //! return Err(e); //! } //! //! if let Err(e) = knet::link_set_enable(knet_handle, &other_hostid, 0, true) { //! return Err(e); //! } //! //! if let Err(e) = knet::handle_setfwd(knet_handle, true) { //! return Err(e); //! } //! //! Ok() //! } //! mod sys; pub mod knet_bindings; #[macro_use] extern crate lazy_static; #[macro_use] extern crate bitflags; use std::os::raw::c_char; use std::ptr::copy_nonoverlapping; use std::ffi::CString; use std::io::{Error, Result, ErrorKind}; // Quick & dirty u8 to boolean fn u8_to_bool(val: u8) -> bool { val != 0 } fn u32_to_bool(val: u32) -> bool { val != 0 } // General internal routine to copy bytes from a C array into a Rust String fn string_from_bytes(bytes: *const ::std::os::raw::c_char, max_length: usize) -> Result { let mut newbytes = Vec::::new(); newbytes.resize(max_length, 0u8); // Get length of the string in old-fashioned style let mut length: usize = 0; let mut count = 0; let mut tmpbytes = bytes; while count < max_length || length == 0 { if unsafe {*tmpbytes} == 0 && length == 0 { length = count; break; } count += 1; tmpbytes = unsafe { tmpbytes.offset(1) } } // Cope with an empty string if length == 0 { return Ok(String::new()); } unsafe { // We need to fully copy it, not shallow copy it. // Messy casting on both parts of the copy here to get it to work on both signed // and unsigned char machines copy_nonoverlapping(bytes as *mut i8, newbytes.as_mut_ptr() as *mut i8, length); } let cs = CString::new(&newbytes[0..length as usize])?; // This is just to convert the error type match cs.into_string() { Ok(s) => Ok(s), Err(_) => Err(Error::new(ErrorKind::Other, "Cannot convert to String")), } } // As below but always returns a string even if there was an error doing the conversion fn string_from_bytes_safe(bytes: *const ::std::os::raw::c_char, max_length: usize) -> String { match string_from_bytes(bytes, max_length) { Ok(s) => s, Err(_)=> "".to_string() } } fn string_to_bytes(s: &str, bytes: &mut [c_char]) -> Result<()> { let c_name = match CString::new(s) { Ok(n) => n, Err(_) => return Err(Error::new(ErrorKind::Other, "Rust conversion error")), }; if c_name.as_bytes().len() > bytes.len() { return Err(Error::new(ErrorKind::Other, "String too long")); } unsafe { // NOTE param order is 'wrong-way round' from C copy_nonoverlapping(c_name.as_ptr(), bytes.as_mut_ptr(), c_name.as_bytes().len()); } Ok(()) } diff --git a/libknet/bindings/rust/src/sys/mod.rs b/libknet/bindings/rust/src/sys/mod.rs index ed06a36a..4a976539 100644 --- a/libknet/bindings/rust/src/sys/mod.rs +++ b/libknet/bindings/rust/src/sys/mod.rs @@ -1,10 +1,10 @@ -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // #![allow(non_camel_case_types, non_snake_case, dead_code, improper_ctypes)] pub mod libknet; diff --git a/libknet/bindings/rust/tests/Cargo.toml.in b/libknet/bindings/rust/tests/Cargo.toml.in index 75c4431f..2db86620 100644 --- a/libknet/bindings/rust/tests/Cargo.toml.in +++ b/libknet/bindings/rust/tests/Cargo.toml.in @@ -1,26 +1,26 @@ -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under GPL-2.0+ [package] name = "knet-bindings-tests" version = "@libknetrustver@" authors = ["Christine Caulfield "] edition = "2018" [build-dependencies] cc = "1.0" pkg-config = "0.3.19" [dependencies] knet-bindings = { path = ".." } libc = "0.2.97" [[bin]] name = "knet-test" test = true bench = false diff --git a/libknet/bindings/rust/tests/Makefile.am b/libknet/bindings/rust/tests/Makefile.am index 0f95693e..12385031 100644 --- a/libknet/bindings/rust/tests/Makefile.am +++ b/libknet/bindings/rust/tests/Makefile.am @@ -1,34 +1,34 @@ # -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk include $(top_srcdir)/build-aux/rust.mk EXTRA_DIST = \ $(RUST_COMMON) \ $(RUST_SHIP_SRCS) RUST_SHIP_SRCS = src/bin/set_plugin_path.c \ src/bin/knet-test.rs check_SCRIPTS = target/$(RUST_TARGET_DIR)/knet-test noinst_SCRIPTS = $(check_SCRIPTS) if INSTALL_TESTS testsuitedir = $(TESTDIR) testsuite_SCRIPTS = $(check_SCRIPTS) endif AM_TESTS_ENVIRONMENT=LD_LIBRARY_PATH="$(abs_top_builddir)/libknet/.libs" TESTS = $(check_SCRIPTS) clean-local: cargo-clean diff --git a/libknet/bindings/rust/tests/build.rs.in b/libknet/bindings/rust/tests/build.rs.in index 165ea41d..7b0718f7 100644 --- a/libknet/bindings/rust/tests/build.rs.in +++ b/libknet/bindings/rust/tests/build.rs.in @@ -1,30 +1,30 @@ -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // extern crate pkg_config; fn main() { // Tell the compiler to use the build-tree libs & headers for compiling println!("cargo:rustc-link-search=native=../../../.libs/"); println!("cargo:rustc-link-lib=knet"); let lib = pkg_config::probe_library("libqb").unwrap(); cc::Build::new() .file("src/bin/set_plugin_path.c") .file("@ABSTOPLEVELSRC@/libknet/tests/test-common.c") // for find_plugins_path() .flag("-Wno-unused-parameter") // Needed for test-common.c to compile cleanly .include("@ABSTOPLEVELSRC@") // for config.h .include("@ABSTOPLEVELSRC@/libknet") // for internals.h .include("@ABSTOPLEVELSRC@/libknet/tests") // for test-common.h .include("@ABSTOPLEVELBUILD@") // for config.h .include("@ABSTOPLEVELBUILD@/libknet") // for internals.h .include("@ABSTOPLEVELBUILD@/libknet/tests") // for test-common.h .includes(lib.include_paths) .compile("set_plugin_path"); } diff --git a/libknet/bindings/rust/tests/src/bin/knet-test.rs b/libknet/bindings/rust/tests/src/bin/knet-test.rs index 8ff88729..c5d1a6c1 100644 --- a/libknet/bindings/rust/tests/src/bin/knet-test.rs +++ b/libknet/bindings/rust/tests/src/bin/knet-test.rs @@ -1,974 +1,974 @@ // Testing the Knet Rust APIs // -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // use knet_bindings::knet_bindings as knet; use std::net::{SocketAddr, IpAddr, Ipv4Addr}; use std::thread::spawn; use std::sync::mpsc::Receiver; use std::sync::mpsc::channel; use std::io::{Result, ErrorKind, Error}; use std::{thread, time}; use std::env; const CHANNEL: i8 = 1; // Dirty C function to set the plugin path for testing (only) extern { fn set_plugin_path(knet_h: knet::Handle); } fn is_memcheck() -> bool { match env::var("KNETMEMCHECK") { Ok(s) => { s == "yes" } Err(_) => false } } // Probably this will never happen, but just-in-case fn is_helgrind() -> bool { match env::var("KNETHELGRIND") { Ok(s) => { s == "yes" } Err(_) => false } } fn get_scaled_tmo(millis: u64) -> time::Duration { if is_memcheck() || is_helgrind() { println!("Running under valgrind, increasing timer from {} to {}", millis, millis*16); time::Duration::from_millis(millis * 16) } else { time::Duration::from_millis(millis) } } // Callbacks fn sock_notify_fn(private_data: u64, datafd: i32, channel: i8, txrx: knet::TxRx, _res: Result<()>) { println!("sock notify called for host {}, datafd: {}, channel: {}, {}", private_data, datafd, channel, txrx); } fn link_notify_fn(private_data: u64, host_id: knet::HostId, link_id: u8, connected: bool, _remote: bool, _external: bool) { println!("link status notify called ({}) for host {}, linkid: {}, connected: {}", private_data, host_id.to_u16(), link_id, connected); } fn host_notify_fn(private_data: u64, host_id: knet::HostId, connected: bool, _remote: bool, _external: bool) { println!("host status notify called ({}) for host {}, connected: {}", private_data, host_id.to_u16(), connected); } fn pmtud_fn(private_data: u64, data_mtu: u32) { println!("PMTUD notify: host {}, MTU:{} ", private_data, data_mtu); } fn onwire_fn(private_data: u64, onwire_min_ver: u8, onwire_max_ver: u8, onwire_ver: u8) { println!("Onwire ver notify for {} : {}/{}/{}", private_data, onwire_min_ver, onwire_max_ver, onwire_ver); } fn filter_fn(private_data: u64, _outdata: &[u8], txrx: knet::TxRx, this_host_id: knet::HostId, src_host_id: knet::HostId, channel: &mut i8, dst_host_ids: &mut Vec) -> knet::FilterDecision { println!("Filter ({}) called {} to {} from {}, channel: {}", private_data, txrx, this_host_id, src_host_id, channel); let dst: u16 = (private_data & 0xFFFF) as u16; match txrx { knet::TxRx::Tx => { dst_host_ids.push(knet::HostId::new(3-dst)); knet::FilterDecision::Unicast } knet::TxRx::Rx => { dst_host_ids.push(this_host_id); knet::FilterDecision::Unicast } } } fn logging_thread(recvr: Receiver) { for i in &recvr { eprintln!("KNET: {}", i.msg); } eprintln!("Logging thread finished"); } fn setup_node(our_hostid: &knet::HostId, other_hostid: &knet::HostId, name: &str) -> Result { let (log_sender, log_receiver) = channel::(); spawn(move || logging_thread(log_receiver)); let knet_handle = match knet::handle_new(our_hostid, Some(log_sender), knet::LogLevel::Debug, knet::HandleFlags::NONE) { Ok(h) => h, Err(e) => { println!("Error from handle_new: {}", e); return Err(e); } }; // Make sure we use the build-tree plugins if LD_LIBRRAY_PATH points to them unsafe { set_plugin_path(knet_handle); } if let Err(e) = knet::host_add(knet_handle, other_hostid) { println!("Error from host_add: {}", e); return Err(e); } if let Err(e) = knet::host_set_name(knet_handle, other_hostid, name) { println!("Error from host_set_name: {}", e); return Err(e); } Ok(knet_handle) } // Called by the ACL tests to get a free port for a dynamic link fn setup_dynamic_link(handle: knet::Handle, hostid: &knet::HostId, link: u8, lowest_port: u16) -> Result<()> { let mut src_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0); for p in lowest_port..=65535 { src_addr.set_port(p); if let Err(e) = knet::link_set_config(handle, hostid, link, knet::TransportId::Udp, &src_addr, None, knet::LinkFlags::NONE) { if let Some(os_err) = e.raw_os_error() { if os_err != libc::EADDRINUSE { println!("Error from link_set_config(dyn): {}", e); return Err(e); } // In use - try the next port number } } else { println!("Dynamic link - Using port {}", p); return Ok(()) } } Err(Error::new(ErrorKind::Other, "No ports available")) } // This is the bit that configures two links on two handles that talk to each other // while also making sure they don't clash with anything else on the system fn setup_links(handle1: knet::Handle, hostid1: &knet::HostId, handle2: knet::Handle, hostid2: &knet::HostId) -> Result { let mut src_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0); let mut dst_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0); for p in 1025..=65534 { src_addr.set_port(p); dst_addr.set_port(p+1); if let Err(e) = knet::link_set_config(handle1, hostid2, 0, knet::TransportId::Udp, &src_addr, Some(&dst_addr), knet::LinkFlags::NONE) { if let Some(os_err) = e.raw_os_error() { if os_err != libc::EADDRINUSE { println!("Error from link_set_config(1): {}", e); return Err(e); } // In use - try the next port number } else { return Err(Error::new(ErrorKind::Other, "Error returned from link_set_config(1) was not an os_error")); } } else { // Now try the other handle if let Err(e) = knet::link_set_config(handle2, hostid1, 0, knet::TransportId::Udp, &dst_addr, Some(&src_addr), knet::LinkFlags::NONE) { if let Some(os_err) = e.raw_os_error() { if os_err != libc::EADDRINUSE { println!("Error from link_set_config(2): {}", e); return Err(e); } else { // In use - clear handle1 and try next pair of ports knet::link_clear_config(handle1, hostid2, 0)?; } } else { return Err(Error::new(ErrorKind::Other, "Error returned from link_set_config(1) was not an os_error")); } } println!("Bound to ports {} & {}",p, p+1); return Ok(p+2) } } Err(Error::new(ErrorKind::Other, "No ports available")) } // Finish configuring links fn configure_link(knet_handle: knet::Handle, our_hostid: &knet::HostId, other_hostid: &knet::HostId) -> Result<()> { if let Err(e) = knet::handle_enable_sock_notify(knet_handle, our_hostid.to_u16() as u64, Some(sock_notify_fn)) { println!("Error from handle_enable_sock_notify: {}", e); return Err(e); } if let Err(e) = knet::link_enable_status_change_notify(knet_handle, our_hostid.to_u16() as u64, Some(link_notify_fn)) { println!("Error from handle_enable_link_notify: {}", e); return Err(e); } if let Err(e) = knet::host_enable_status_change_notify(knet_handle, our_hostid.to_u16() as u64, Some(host_notify_fn)) { println!("Error from handle_enable_host_notify: {}", e); return Err(e); } if let Err(e) = knet::handle_enable_filter(knet_handle, our_hostid.to_u16() as u64, Some(filter_fn)) { println!("Error from handle_enable_filter: {}", e); return Err(e); } if let Err(e) = knet::handle_enable_pmtud_notify(knet_handle, our_hostid.to_u16() as u64, Some(pmtud_fn)) { println!("Error from handle_enable_pmtud_notify: {}", e); return Err(e); } if let Err(e) = knet::handle_enable_onwire_ver_notify(knet_handle, our_hostid.to_u16() as u64, Some(onwire_fn)) { println!("Error from handle_enable_onwire_ver_notify: {}", e); return Err(e); } match knet::handle_add_datafd(knet_handle, 0, CHANNEL) { Ok((fd,chan)) => { println!("Added datafd, fd={}, channel={}", fd, chan); }, Err(e) => { println!("Error from add_datafd: {}", e); return Err(e); } } if let Err(e) = knet::handle_crypto_rx_clear_traffic(knet_handle, knet::RxClearTraffic::Allow) { println!("Error from handle_crypto_rx_clear_traffic: {}", e); return Err(e); } if let Err(e) = knet::link_set_enable(knet_handle, other_hostid, 0, true) { println!("Error from set_link_enable(true): {}", e); return Err(e); } if let Err(e) = knet::link_set_ping_timers(knet_handle, other_hostid, 0, 500, 1000, 1024) { println!("Error from set_link_ping_timers: {}", e); return Err(e); } match knet::link_get_ping_timers(knet_handle, other_hostid, 0) { Ok((a,b,c)) => { if a != 500 || b != 1000 || c != 1024 { println!("get_link_ping_timers returned wrong values {}, {},{} (s/b 500,1000,1024)", a,b,c); return Err(Error::new(ErrorKind::Other, "Error in ping timers")); } }, Err(e) => { println!("Error from set_link_ping_timers: {}", e); return Err(e); } } if let Err(e) = knet::handle_setfwd(knet_handle, true) { println!("Error from setfwd(true): {}", e); return Err(e); } // Check status let data_fd = match knet::handle_get_datafd(knet_handle, CHANNEL) { Ok(f) => { println!("got datafd {} for channel", f); f } Err(e) => { println!("Error from handle_get_datafd: {}", e); return Err(e); } }; match knet::handle_get_channel(knet_handle, data_fd) { Ok(c) => if c != CHANNEL { println!("handle_get_channel returned wrong channel ID: {}, {}",c, CHANNEL); return Err(Error::new(ErrorKind::Other, "Error in handle_get_channel")); } Err(e) => { println!("Error from handle_get_channel: {}", e); return Err(e); } } match knet::link_get_enable(knet_handle, other_hostid, 0) { Ok(b) => if !b { println!("link not enabled (according to link_get_enable()"); }, Err(e) => { println!("Error from link_get_enable: {}", e); return Err(e); } } Ok(()) } fn recv_stuff(handle: knet::Handle, host: knet::HostId) -> Result<()> { let buf = [0u8; 1024]; loop { match knet::recv(handle, &buf, CHANNEL) { Ok(len) => { let recv_len = len as usize; if recv_len == 0 { break; // EOF?? } else { let s = String::from_utf8(buf[0..recv_len].to_vec()).unwrap(); println!("recvd on {}: {} {:?} {} ", host, recv_len, &buf[0..recv_len], s); if s == *"QUIT" { println!("got QUIT on {}, exitting", host); break; } } } Err(e) => { if e.kind() == ErrorKind::WouldBlock { thread::sleep(get_scaled_tmo(100)); } else { println!("recv failed: {}", e); return Err(e); } } } } Ok(()) } fn close_handle(handle: knet::Handle, remnode: u16) -> Result<()> { let other_hostid = knet::HostId::new(remnode); if let Err(e) = knet::handle_setfwd(handle, false) { println!("Error from setfwd 1 (false): {}", e); return Err(e); } let data_fd = match knet::handle_get_datafd(handle, CHANNEL) { Ok(f) => { println!("got datafd {} for channel", f); f } Err(e) => { println!("Error from handle_get_datafd: {}", e); return Err(e); } }; if let Err(e) = knet::handle_remove_datafd(handle, data_fd) { println!("Error from handle_remove_datafd: {}", e); return Err(e); } if let Err(e) = knet::link_set_enable(handle, &other_hostid, 0, false) { println!("Error from set_link_enable(false): {}", e); return Err(e); } if let Err(e) = knet::link_clear_config(handle, &other_hostid, 0) { println!("clear config failed: {}", e); return Err(e); } if let Err(e) = knet::host_remove(handle, &other_hostid) { println!("host remove failed: {}", e); return Err(e); } if let Err(e) = knet::handle_free(handle) { println!("handle_free failed: {}", e); return Err(e); } Ok(()) } fn set_compress(handle: knet::Handle) -> Result<()> { let compress_config = knet::CompressConfig { compress_model: "zlib".to_string(), compress_threshold : 10, compress_level: 1, }; if let Err(e) = knet::handle_compress(handle, &compress_config) { println!("Error from handle_compress: {}", e); Err(e) } else { Ok(()) } } fn set_crypto(handle: knet::Handle) -> Result<()> { let private_key = [55u8; 2048]; // Add some crypto let crypto_config = knet::CryptoConfig { crypto_model: "openssl".to_string(), crypto_cipher_type: "aes256".to_string(), crypto_hash_type: "sha256".to_string(), private_key: &private_key, }; if let Err(e) = knet::handle_crypto_set_config(handle, &crypto_config, 1) { println!("Error from handle_crypto_set_config: {}", e); return Err(e); } if let Err(e) = knet::handle_crypto_use_config(handle, 1) { println!("Error from handle_crypto_use_config: {}", e); return Err(e); } if let Err(e) = knet::handle_crypto_rx_clear_traffic(handle, knet::RxClearTraffic::Disallow) { println!("Error from handle_crypto_rx_clear_traffic: {}", e); return Err(e); } Ok(()) } fn send_messages(handle: knet::Handle, send_quit: bool) -> Result<()> { let mut buf : [u8; 20] = [b'0'; 20]; for i in 0..10 { buf[i as usize + 1] = i + b'0'; match knet::send(handle, &buf, CHANNEL) { Ok(len) => { if len as usize != buf.len() { println!("sent {} bytes instead of {}", len, buf.len()); } }, Err(e) => { println!("send failed: {}", e); return Err(e); } } } let s = String::from("SYNC TEST").into_bytes(); if let Err(e) = knet::send_sync(handle, &s, CHANNEL) { println!("send_sync failed: {}", e); return Err(e); } if send_quit { // Sleep to allow messages to calm down before we tell the RX thread to quit thread::sleep(get_scaled_tmo(3000)); let b = String::from("QUIT").into_bytes(); match knet::send(handle, &b, CHANNEL) { Ok(len) => { if len as usize != b.len() { println!("sent {} bytes instead of {}", len, b.len()); } }, Err(e) => { println!("send failed: {}", e); return Err(e); } } } Ok(()) } fn test_link_host_list(handle: knet::Handle) -> Result<()> { match knet::host_get_host_list(handle) { Ok(hosts) => { for i in &hosts { print!("host {}: links: ", i); match knet::link_get_link_list(handle, i) { Ok(ll) => { for j in ll { print!(" {}",j); } }, Err(e) => { println!("link_get_link_list failed: {}", e); return Err(e); } } println!(); } } Err(e) => { println!("link_get_host_list failed: {}", e); return Err(e); } } Ok(()) } // Try some metadata calls fn test_metadata_calls(handle: knet::Handle, host: &knet::HostId) -> Result<()> { if let Err(e) = knet::handle_set_threads_timer_res(handle, 190000) { println!("knet_handle_set_threads_timer_res failed: {:?}", e); return Err(e); } match knet::handle_get_threads_timer_res(handle) { Ok(v) => { if v != 190000 { println!("knet_handle_get_threads_timer_res returned wrong value {}", v); } }, Err(e) => { println!("knet_handle_set_threads_timer_res failed: {:?}", e); return Err(e); } } if let Err(e) = knet::handle_pmtud_set(handle, 1000) { println!("knet_handle_pmtud_set failed: {:?}", e); return Err(e); } match knet::handle_pmtud_get(handle) { Ok(v) => { if v != 1000 { println!("knet_handle_pmtud_get returned wrong value {} (ALLOWED)", v); // Don't fail on this, it might not have been set yet } }, Err(e) => { println!("knet_handle_pmtud_get failed: {:?}", e); return Err(e); } } if let Err(e) = knet::handle_pmtud_setfreq(handle, 1000) { println!("knet_handle_pmtud_setfreq failed: {:?}", e); return Err(e); } match knet::handle_pmtud_getfreq(handle) { Ok(v) => { if v != 1000 { println!("knet_handle_pmtud_getfreq returned wrong value {}", v); } }, Err(e) => { println!("knet_handle_pmtud_getfreq failed: {:?}", e); return Err(e); } } if let Err(e) = knet::handle_set_transport_reconnect_interval(handle, 100) { println!("knet_handle_set_transport_reconnect_interval failed: {:?}", e); return Err(e); } match knet::handle_get_transport_reconnect_interval(handle) { Ok(v) => { if v != 100 { println!("knet_handle_get_transport_reconnect_interval {}", v); } }, Err(e) => { println!("knet_handle_get_transport_reconnect_interval failed: {:?}", e); return Err(e); } } if let Err(e) = knet::link_set_pong_count(handle, host, 0, 4) { println!("knet_link_set_pong_count failed: {:?}", e); return Err(e); } match knet::link_get_pong_count(handle, host, 0) { Ok(v) => { if v != 4 { println!("knet_link_get_pong_count returned wrong value {}", v); } }, Err(e) => { println!("knet_link_get_pong_count failed: {:?}", e); return Err(e); } } if let Err(e) = knet::host_set_policy(handle, host, knet::LinkPolicy::Active) { println!("knet_host_set_policy failed: {:?}", e); return Err(e); } match knet::host_get_policy(handle, host) { Ok(v) => { if v != knet::LinkPolicy::Active { println!("knet_host_get_policy returned wrong value {}", v); } }, Err(e) => { println!("knet_host_get_policy failed: {:?}", e); return Err(e); } } if let Err(e) = knet::link_set_priority(handle, host, 0, 5) { println!("knet_link_set_priority failed: {:?}", e); return Err(e); } match knet::link_get_priority(handle, host, 0) { Ok(v) => { if v != 5 { println!("knet_link_get_priority returned wrong value {}", v); } }, Err(e) => { println!("knet_link_get_priority failed: {:?}", e); return Err(e); } } let name = match knet::host_get_name_by_host_id(handle, host) { Ok(n) => { println!("Returned host name is {}", n); n }, Err(e) => { println!("knet_host_get_name_by_host_id failed: {:?}", e); return Err(e); } }; match knet::host_get_id_by_host_name(handle, &name) { Ok(n) => { println!("Returned host id is {}", n); if n != *host { println!("Returned host id is not 2"); return Err(Error::new(ErrorKind::Other, "Error in get_id_by_host_name")); } }, Err(e) => { println!("knet_host_get_id_by_host_name failed: {:?}", e); return Err(e); } } match knet::link_get_config(handle, host, 0) { Ok((t, s, d, _f)) => { println!("Got link config: {}, {:?}, {:?}", t.to_string(),s,d); }, Err(e) => { println!("knet_link_get_config failed: {:?}", e); return Err(e); } } if let Err(e) = knet::handle_set_host_defrag_bufs(handle, 4, 32, 25, knet::DefragReclaimPolicy::Absolute) { println!("handle_config_set_host_defrag_bufs failed: {:?}", e); return Err(e); } match knet::handle_get_host_defrag_bufs(handle) { Ok((min, max, shrink, policy)) => { if min != 4 || max != 32 || shrink != 25 || policy != knet::DefragReclaimPolicy::Absolute { println!("handle_config_get_host_defrag_bufs returned bad values"); println!("Got {},{},{},{}. expected 4,32,2,Absolute", min, max, shrink, policy); } else { println!("Defrag params correct: {},{},{},{}", min, max, shrink, policy); } } Err(e) => { println!("handle_config_get_host_defrag_bufs failed: {:?}", e); return Err(e); } } // Can't set this to anything different if let Err(e) = knet::handle_set_onwire_ver(handle, 1) { println!("knet_link_set_onwire_ver failed: {:?}", e); return Err(e); } match knet::handle_get_onwire_ver(handle, host) { Ok((min, max, ver)) => { println!("get_onwire_ver: Got onwire ver: {}/{}/{}", min, max, ver); }, Err(e) => { println!("knet_link_get_onwire_ver failed: {:?}", e); return Err(e); } } // Logging match knet::log_get_subsystem_name(3) { Ok(n) => println!("subsystem name for 3 is {}", n), Err(e) => { println!("knet_log_get_subsystem_name failed: {:?}", e); return Err(e); } } match knet::log_get_subsystem_id("TX") { Ok(n) => println!("subsystem ID for TX is {}", n), Err(e) => { println!("knet_log_get_subsystem_id failed: {:?}", e); return Err(e); } } match knet::log_get_loglevel_id("DEBUG") { Ok(n) => println!("loglevel ID for DEBUG is {}", n), Err(e) => { println!("knet_log_get_loglevel_id failed: {:?}", e); return Err(e); } } match knet::log_get_loglevel_name(1) { Ok(n) => println!("loglevel name for 1 is {}", n), Err(e) => { println!("knet_log_get_loglevel_name failed: {:?}", e); return Err(e); } } if let Err(e) = knet::log_set_loglevel(handle, knet::SubSystem::Handle , knet::LogLevel::Debug) { println!("knet_log_set_loglevel failed: {:?}", e); return Err(e); } match knet::log_get_loglevel(handle, knet::SubSystem::Handle) { Ok(n) => println!("loglevel for Handle is {}", n), Err(e) => { println!("knet_log_get_loglevel failed: {:?}", e); return Err(e); } } Ok(()) } fn test_acl(handle: knet::Handle, host: &knet::HostId, low_port: u16) -> Result<()> { if let Err(e) = knet::handle_enable_access_lists(handle, true) { println!("Error from handle_enable_access_lists: {:?}", e); return Err(e); } // Dynamic link for testing ACL APIs (it never gets used) if let Err(e) = setup_dynamic_link(handle, host, 1, low_port) { println!("Error from link_set_config (dynamic): {}", e); return Err(e); } // These ACLs are nonsense on stilts if let Err(e) = knet::link_add_acl(handle, host, 1, &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8003_u16), &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8003_u16), knet::AclCheckType::Address, knet::AclAcceptReject::Reject) { println!("Error from link_add_acl: {:?}", e); return Err(e); } if let Err(e) = knet::link_insert_acl(handle, host, 1, 0, &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), 8004_u16), &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), 8004_u16), knet::AclCheckType::Address, knet::AclAcceptReject::Reject) { println!("Error from link_add_acl: {:?}", e); return Err(e); } if let Err(e) = knet::link_rm_acl(handle, host, 1, &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8003_u16), &SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8003_u16), knet::AclCheckType::Address, knet::AclAcceptReject::Reject) { println!("Error from link_rm_acl: {:?}", e); return Err(e); } if let Err(e) = knet::link_clear_acl(handle, host, 1) { println!("Error from link_clear_acl: {:?}", e); return Err(e); } // Get rid of this link before it messes things up if let Err(e) = knet::link_clear_config(handle, host, 1) { println!("clear config (dynamic) failed: {}", e); return Err(e); } if let Err(e) = knet::handle_enable_access_lists(handle, false) { println!("Error from handle_enable_access_lists: {:?}", e); return Err(e); } Ok(()) } fn main() -> Result<()> { // Start with some non-handle information match knet::get_crypto_list() { Ok(l) => { print!("Crypto models:"); for i in &l { print!(" {}", i.name); } println!(); } Err(e) => { println!("link_get_crypto_list failed: {:?}", e); return Err(e); } } match knet::get_compress_list() { Ok(l) => { print!("Compress models:"); for i in &l { print!(" {}", i.name); } println!(); } Err(e) => { println!("link_get_compress_list failed: {:?}", e); return Err(e); } } match knet::get_transport_list() { Ok(l) => { print!("Transports:"); for i in &l { print!(" {}", i.name); } println!(); } Err(e) => { println!("link_get_transport_list failed: {:?}", e); return Err(e); } } let host1 = knet::HostId::new(1); let host2 = knet::HostId::new(2); // Now test traffic let handle1 = setup_node(&host1, &host2, "host2")?; let handle2 = setup_node(&host2, &host1, "host1")?; let low_port = setup_links(handle1, &host1, handle2, &host2)?; configure_link(handle1, &host1, &host2)?; configure_link(handle2, &host2, &host1)?; // Copy stuff for the threads let handle1_clone = handle1; let handle2_clone = handle2; let host1_clone = host1; let host2_clone = host2; // Wait for links to start thread::sleep(get_scaled_tmo(10000)); test_link_host_list(handle1)?; test_link_host_list(handle2)?; // Start recv threads for each handle let thread_handles = vec![ spawn(move || recv_stuff(handle1_clone, host1_clone)), spawn(move || recv_stuff(handle2_clone, host2_clone)) ]; send_messages(handle1, false)?; send_messages(handle2, false)?; thread::sleep(get_scaled_tmo(3000)); set_crypto(handle1)?; set_crypto(handle2)?; set_compress(handle1)?; set_compress(handle2)?; thread::sleep(get_scaled_tmo(3000)); send_messages(handle1, true)?; send_messages(handle2, true)?; test_acl(handle1, &host2, low_port)?; // Wait for recv threads to finish for handle in thread_handles { if let Err(error) = handle.join() { println!("thread join error: {:?}", error); } } // Try some statses match knet::handle_get_stats(handle1) { Ok(s) => println!("handle stats: {}", s), Err(e) => { println!("handle_get_stats failed: {:?}", e); return Err(e); } } match knet::host_get_status(handle1, &host2) { Ok(s) => println!("host status: {}", s), Err(e) => { println!("host_get_status failed: {:?}", e); return Err(e); } } match knet::link_get_status(handle1, &host2, 0) { Ok(s) => println!("link status: {}", s), Err(e) => { println!("link_get_status failed: {:?}", e); return Err(e); } } if let Err(e) = knet::handle_clear_stats(handle1, knet::ClearStats::Handle) { println!("handle_clear_stats failed: {:?}", e); return Err(e); } test_metadata_calls(handle1, &knet::HostId::new(2))?; close_handle(handle1, 2)?; close_handle(handle2, 1)?; // Sleep to see if log thread dies thread::sleep(get_scaled_tmo(3000)); Ok(()) } diff --git a/libknet/bindings/rust/tests/src/bin/set_plugin_path.c b/libknet/bindings/rust/tests/src/bin/set_plugin_path.c index c311e0d1..d4398300 100644 --- a/libknet/bindings/rust/tests/src/bin/set_plugin_path.c +++ b/libknet/bindings/rust/tests/src/bin/set_plugin_path.c @@ -1,23 +1,23 @@ /* - * Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under GPL-2.0+ */ #include #include "test-common.h" #include "internals.h" #include "libknet.h" // Set the path for compress/crypto plugins when running the test program void set_plugin_path(knet_handle_t knet_h) { struct knet_handle *handle = (struct knet_handle *)knet_h; char *plugins_path = find_plugins_path(); if (plugins_path) { handle->plugin_path = plugins_path; } } diff --git a/libknet/common.c b/libknet/common.c index 62a99dd8..68d2b224 100644 --- a/libknet/common.c +++ b/libknet/common.c @@ -1,189 +1,189 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "logging.h" #include "common.h" int _fdset_cloexec(int fd) { int fdflags; fdflags = fcntl(fd, F_GETFD, 0); if (fdflags < 0) return -1; fdflags |= FD_CLOEXEC; if (fcntl(fd, F_SETFD, fdflags) < 0) return -1; return 0; } int _fdset_nonblock(int fd) { int fdflags; fdflags = fcntl(fd, F_GETFL, 0); if (fdflags < 0) return -1; fdflags |= O_NONBLOCK; if (fcntl(fd, F_SETFL, fdflags) < 0) return -1; return 0; } static int get_lib_dir(void *lib_handle, char dir[MAXPATHLEN]) { int res; #ifndef HAVE_RTLD_DI_ORIGIN struct link_map *lm; char l_name[MAXPATHLEN]; #endif #ifdef HAVE_RTLD_DI_ORIGIN res = dlinfo(lib_handle, RTLD_DI_ORIGIN, dir); #else /* * musl libc doesn't support RTLD_DI_ORIGIN */ res = dlinfo(lib_handle, RTLD_DI_LINKMAP, &lm); if (res == 0) { snprintf(l_name, sizeof(l_name), "%s", lm->l_name); snprintf(dir, MAXPATHLEN, "%s", dirname(l_name)); } #endif return res; } static void *open_lib(knet_handle_t knet_h, const char *libname, int extra_flags) { void *ret = NULL; char *error = NULL; char dir[MAXPATHLEN], path[MAXPATHLEN * 2], link[MAXPATHLEN]; struct stat sb; /* * clear any pending error */ dlerror(); strncpy(path, knet_h->plugin_path, sizeof(path)-1); strncat(path, "/", sizeof(path)-1); strncat(path, libname, sizeof(path)-strlen(knet_h->plugin_path)-2); ret = dlopen(path, RTLD_NOW | RTLD_GLOBAL | extra_flags); if (!ret) { error = dlerror(); if (error) { log_err(knet_h, KNET_SUB_COMMON, "unable to dlopen %s: %s", libname, error); } else { log_err(knet_h, KNET_SUB_COMMON, "unable to dlopen %s: unknown error", libname); } errno = EAGAIN; return NULL; } memset(dir, 0, sizeof(dir)); memset(link, 0, sizeof(link)); memset(path, 0, sizeof(path)); if (get_lib_dir(ret, dir) < 0) { /* * should we dlclose and return error? */ error = dlerror(); log_warn(knet_h, KNET_SUB_COMMON, "unable to dlinfo %s: %s", libname, error); } else { snprintf(path, sizeof(path), "%s/%s", dir, libname); log_info(knet_h, KNET_SUB_COMMON, "%s has been loaded from %s", libname, path); /* * try to resolve the library and check if it is a symlink and to where. * we can't prevent symlink attacks but at least we can log where the library * has been loaded from */ if (lstat(path, &sb) < 0) { log_debug(knet_h, KNET_SUB_COMMON, "Unable to stat %s: %s", path, strerror(errno)); goto out; } if (S_ISLNK(sb.st_mode)) { if (readlink(path, link, sizeof(link)-1) < 0) { log_debug(knet_h, KNET_SUB_COMMON, "Unable to readlink %s: %s", path, strerror(errno)); goto out; } link[sizeof(link) - 1] = 0; /* * symlink is relative to the directory */ if (link[0] != '/') { snprintf(path, sizeof(path), "%s/%s", dir, link); log_info(knet_h, KNET_SUB_COMMON, "%s/%s is a symlink to %s", dir, libname, path); } else { log_info(knet_h, KNET_SUB_COMMON, "%s/%s is a symlink to %s", dir, libname, link); } } } out: return ret; } void *load_module(knet_handle_t knet_h, const char *type, const char *name) { void *module, *ops; log_msg_t **log_msg_sym; char soname[MAXPATHLEN], opsname[MAXPATHLEN]; snprintf (soname, sizeof soname, "%s_%s.so", type, name); module = open_lib(knet_h, soname, 0); if (!module) { return NULL; } log_msg_sym = dlsym (module, "log_msg"); if (!log_msg_sym) { log_err (knet_h, KNET_SUB_COMMON, "unable to map symbol 'log_msg' in module %s: %s", soname, dlerror ()); errno = EINVAL; return NULL; } *log_msg_sym = log_msg; snprintf (opsname, sizeof opsname, "%s_model", type); ops = dlsym (module, opsname); if (!ops) { log_err (knet_h, KNET_SUB_COMMON, "unable to map symbol 'model' in module %s: %s", soname, dlerror ()); errno = EINVAL; return NULL; } return ops; } diff --git a/libknet/common.h b/libknet/common.h index d6078c49..a8d99a66 100644 --- a/libknet/common.h +++ b/libknet/common.h @@ -1,19 +1,19 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "internals.h" #ifndef __KNET_COMMON_H__ #define __KNET_COMMON_H__ int _fdset_cloexec(int fd); int _fdset_nonblock(int fd); void *load_module(knet_handle_t knet_h, const char *type, const char *name); #endif diff --git a/libknet/compat.c b/libknet/compat.c index 74e72133..907cb158 100644 --- a/libknet/compat.c +++ b/libknet/compat.c @@ -1,114 +1,114 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Jan Friesse * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include "compat.h" #ifndef HAVE_SYS_EPOLL_H #ifdef HAVE_KEVENT /* for FreeBSD which has kevent instead of epoll */ #include #include #include #include static int32_t _poll_to_filter_(int32_t event) { int32_t out = 0; if (event & POLLIN) out |= EVFILT_READ; if (event & POLLOUT) out |= EVFILT_WRITE; return out; } int epoll_create(int size) { return kqueue(); } int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) { int ret = 0; struct kevent ke; short filters = _poll_to_filter_(event->events); switch (op) { /* The kevent man page says that EV_ADD also does MOD */ case EPOLL_CTL_ADD: case EPOLL_CTL_MOD: EV_SET(&ke, fd, filters, EV_ADD | EV_ENABLE, 0, 0, event->data.ptr); break; case EPOLL_CTL_DEL: EV_SET(&ke, fd, filters, EV_DELETE, 0, 0, event->data.ptr); break; default: errno = EINVAL; return -1; } ret = kevent(epfd, &ke, 1, NULL, 0, NULL); return ret; } int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout_ms) { struct kevent kevents[maxevents]; struct timespec timeout = { 0, 0 }; struct timespec *timeout_ptr = &timeout; uint32_t revents; int event_count; int i; int returned_events; if (timeout_ms != -1) { timeout.tv_sec = timeout_ms/1000; timeout.tv_nsec += (timeout_ms % 1000) * 1000000ULL; } else { timeout_ptr = NULL; } event_count = kevent(epfd, NULL, 0, kevents, maxevents, timeout_ptr); if (event_count == -1) { return -1; } returned_events = 0; for (i = 0; i < event_count; i++) { revents = 0; if (kevents[i].flags & EV_ERROR) { revents |= POLLERR; } if (kevents[i].flags & EV_EOF) { revents |= POLLHUP; } if (kevents[i].filter == EVFILT_READ) { revents |= POLLIN; } if (kevents[i].filter == EVFILT_WRITE) { revents |= POLLOUT; } events[returned_events].events = revents; events[returned_events].data.ptr = kevents[i].udata; returned_events++; } return returned_events; } #endif /* HAVE_KEVENT */ #endif /* HAVE_SYS_EPOLL_H */ diff --git a/libknet/compat.h b/libknet/compat.h index b5fdb2f0..49284418 100644 --- a/libknet/compat.h +++ b/libknet/compat.h @@ -1,50 +1,50 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Jan Friesse * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_COMPAT_H__ #define __KNET_COMPAT_H__ #include "config.h" #include #include #ifndef ETIME #define ETIME ETIMEDOUT #endif #ifdef HAVE_SYS_EPOLL_H #include #else #ifdef HAVE_KEVENT #include #define EPOLL_CTL_ADD 1 #define EPOLL_CTL_MOD 2 #define EPOLL_CTL_DEL 3 #define EPOLLIN POLLIN #define EPOLLOUT POLLOUT typedef union epoll_data { void *ptr; int fd; uint32_t u32; uint64_t u64; } epoll_data_t; struct epoll_event { uint32_t events; /* Epoll events */ epoll_data_t data; /* User data variable */ }; int epoll_create(int size); int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout_ms); #endif /* HAVE_KEVENT */ #endif /* HAVE_SYS_EPOLL_H */ #endif /* __KNET_COMPAT_H__ */ diff --git a/libknet/compress.c b/libknet/compress.c index 122fe9fe..d0bcb424 100644 --- a/libknet/compress.c +++ b/libknet/compress.c @@ -1,544 +1,544 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "internals.h" #include "compress.h" #include "compress_model.h" #include "logging.h" #include "threads_common.h" #include "common.h" /* * internal module switch data */ /* * DO NOT CHANGE MODEL_ID HERE OR ONWIRE COMPATIBILITY * WILL BREAK! * * Always add new items before the last NULL. */ static compress_model_t compress_modules_cmds[KNET_MAX_COMPRESS_METHODS + 1] = { { "none" , 0, 0, 0, NULL }, { "zlib" , 1, WITH_COMPRESS_ZLIB , 0, NULL }, { "lz4" , 2, WITH_COMPRESS_LZ4 , 0, NULL }, { "lz4hc", 3, WITH_COMPRESS_LZ4 , 0, NULL }, { "lzo2" , 4, WITH_COMPRESS_LZO2 , 0, NULL }, { "lzma" , 5, WITH_COMPRESS_LZMA , 0, NULL }, { "bzip2", 6, WITH_COMPRESS_BZIP2, 0, NULL }, { "zstd" , 7, WITH_COMPRESS_ZSTD, 0, NULL }, { NULL, KNET_MAX_COMPRESS_METHODS, 0, 0, NULL } }; static int max_model = 0; static struct timespec last_load_failure; static int compress_get_model(const char *model) { int idx = 0; while (compress_modules_cmds[idx].model_name != NULL) { if (!strcmp(compress_modules_cmds[idx].model_name, model)) { return compress_modules_cmds[idx].model_id; } idx++; } return -1; } static int compress_get_max_model(void) { int idx = 0; while (compress_modules_cmds[idx].model_name != NULL) { idx++; } return idx - 1; } static int compress_is_valid_model(int compress_model) { int idx = 0; while (compress_modules_cmds[idx].model_name != NULL) { if ((compress_model == compress_modules_cmds[idx].model_id) && (compress_modules_cmds[idx].built_in == 1)) { return 0; } idx++; } return -1; } static int val_level( knet_handle_t knet_h, int compress_model, int compress_level) { if (compress_modules_cmds[compress_model].ops->val_level != NULL) { return compress_modules_cmds[compress_model].ops->val_level(knet_h, compress_level); } return 0; } /* * compress_check_lib_is_init needs to be invoked in a locked context! */ static int compress_check_lib_is_init(knet_handle_t knet_h, int cmp_model) { /* * lack of a .is_init function means that the module does not require * init per handle so we use a fake reference in the compress_int_data * to identify that we already increased the libref for this handle */ if (compress_modules_cmds[cmp_model].loaded == 1) { if (compress_modules_cmds[cmp_model].ops->is_init == NULL) { if (knet_h->compress_int_data[cmp_model] != NULL) { return 1; } } else { if (compress_modules_cmds[cmp_model].ops->is_init(knet_h, cmp_model) == 1) { return 1; } } } return 0; } /* * compress_load_lib should _always_ be invoked in write lock context */ static int compress_load_lib(knet_handle_t knet_h, int cmp_model, int rate_limit) { struct timespec clock_now; unsigned long long timediff; /* * checking again for paranoia and because * compress_check_lib_is_init is usually invoked in read context * and we need to switch from read to write locking in between. * another thread might have init the library in the meantime */ if (compress_check_lib_is_init(knet_h, cmp_model)) { return 0; } /* * due to the fact that decompress can load libraries * on demand, depending on the compress model selected * on other nodes, it is possible for an attacker * to send crafted packets to attempt to load libraries * at random in a DoS fashion. * If there is an error loading a library, then we want * to rate_limit a retry to reload the library every X * seconds to avoid a lock DoS that could greatly slow * down libknet. */ if (rate_limit) { if ((last_load_failure.tv_sec != 0) || (last_load_failure.tv_nsec != 0)) { clock_gettime(CLOCK_MONOTONIC, &clock_now); timespec_diff(last_load_failure, clock_now, &timediff); if (timediff < 10000000000) { errno = EAGAIN; return -1; } } } if (compress_modules_cmds[cmp_model].loaded == 0) { compress_modules_cmds[cmp_model].ops = load_module (knet_h, "compress", compress_modules_cmds[cmp_model].model_name); if (!compress_modules_cmds[cmp_model].ops) { clock_gettime(CLOCK_MONOTONIC, &last_load_failure); return -1; } if (compress_modules_cmds[cmp_model].ops->abi_ver != KNET_COMPRESS_MODEL_ABI) { log_err(knet_h, KNET_SUB_COMPRESS, "ABI mismatch loading module %s. knet ver: %d, module ver: %d", compress_modules_cmds[cmp_model].model_name, KNET_COMPRESS_MODEL_ABI, compress_modules_cmds[cmp_model].ops->abi_ver); errno = EINVAL; return -1; } compress_modules_cmds[cmp_model].loaded = 1; } if (compress_modules_cmds[cmp_model].ops->init != NULL) { if (compress_modules_cmds[cmp_model].ops->init(knet_h, cmp_model) < 0) { return -1; } } else { knet_h->compress_int_data[cmp_model] = (void *)&"1"; } return 0; } static int compress_lib_test(knet_handle_t knet_h) { int savederrno = 0; unsigned char src[KNET_DATABUFSIZE]; unsigned char dst[KNET_DATABUFSIZE_COMPRESS]; ssize_t dst_comp_len = KNET_DATABUFSIZE_COMPRESS, dst_decomp_len = KNET_DATABUFSIZE; unsigned int i; int request_level; memset(src, 0, KNET_DATABUFSIZE); memset(dst, 0, KNET_DATABUFSIZE_COMPRESS); /* * NOTE: we cannot use compress and decompress API calls due to locking * so we need to call directly into the modules */ if (compress_modules_cmds[knet_h->compress_model].ops->compress(knet_h, src, KNET_DATABUFSIZE, dst, &dst_comp_len) < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_COMPRESS, "Unable to compress test buffer. Please check your compression settings: %s", strerror(savederrno)); errno = savederrno; return -1; } else if ((long unsigned int)dst_comp_len >= KNET_DATABUFSIZE) { /* * compress not effective, try again using default compression level when available */ request_level = knet_h->compress_level; log_warn(knet_h, KNET_SUB_COMPRESS, "Requested compression level (%d) did not generate any compressed data (source: %zu destination: %zu)", request_level, sizeof(src), dst_comp_len); if ((!compress_modules_cmds[knet_h->compress_model].ops->get_default_level()) || ((knet_h->compress_level = compress_modules_cmds[knet_h->compress_model].ops->get_default_level()) == KNET_COMPRESS_UNKNOWN_DEFAULT)) { log_err(knet_h, KNET_SUB_COMPRESS, "compression %s does not provide a default value", compress_modules_cmds[knet_h->compress_model].model_name); errno = EINVAL; return -1; } else { memset(src, 0, KNET_DATABUFSIZE); memset(dst, 0, KNET_DATABUFSIZE_COMPRESS); dst_comp_len = KNET_DATABUFSIZE_COMPRESS; if (compress_modules_cmds[knet_h->compress_model].ops->compress(knet_h, src, KNET_DATABUFSIZE, dst, &dst_comp_len) < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_COMPRESS, "Unable to compress with default compression level: %s", strerror(savederrno)); errno = savederrno; return -1; } log_warn(knet_h, KNET_SUB_COMPRESS, "Requested compression level (%d) did not work, switching to default (%d)", request_level, knet_h->compress_level); } } if (compress_modules_cmds[knet_h->compress_model].ops->decompress(knet_h, dst, dst_comp_len, src, &dst_decomp_len) < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_COMPRESS, "Unable to decompress test buffer. Please check your compression settings: %s", strerror(savederrno)); errno = savederrno; return -1; } for (i = 0; i < KNET_DATABUFSIZE; i++) { if (src[i] != 0) { log_err(knet_h, KNET_SUB_COMPRESS, "Decompressed buffer contains incorrect data"); errno = EINVAL; return -1; } } return 0; } int compress_init( knet_handle_t knet_h) { max_model = compress_get_max_model(); if (max_model > KNET_MAX_COMPRESS_METHODS) { log_err(knet_h, KNET_SUB_COMPRESS, "Too many compress methods defined in compress.c."); errno = EINVAL; return -1; } memset(&last_load_failure, 0, sizeof(struct timespec)); return 0; } static int compress_cfg( knet_handle_t knet_h, struct knet_handle_compress_cfg *knet_handle_compress_cfg) { int savederrno = 0, err = 0; int cmp_model; cmp_model = compress_get_model(knet_handle_compress_cfg->compress_model); if (cmp_model < 0) { log_err(knet_h, KNET_SUB_COMPRESS, "compress model %s not supported", knet_handle_compress_cfg->compress_model); errno = EINVAL; return -1; } log_debug(knet_h, KNET_SUB_COMPRESS, "Initializing compress module [%s/%d/%u]", knet_handle_compress_cfg->compress_model, knet_handle_compress_cfg->compress_level, knet_handle_compress_cfg->compress_threshold); if (cmp_model > 0) { if (compress_modules_cmds[cmp_model].built_in == 0) { log_err(knet_h, KNET_SUB_COMPRESS, "compress model %s support has not been built in. Please contact your vendor or fix the build", knet_handle_compress_cfg->compress_model); errno = EINVAL; return -1; } if (knet_handle_compress_cfg->compress_threshold > KNET_MAX_PACKET_SIZE) { log_err(knet_h, KNET_SUB_COMPRESS, "compress threshold cannot be higher than KNET_MAX_PACKET_SIZE (%d).", KNET_MAX_PACKET_SIZE); errno = EINVAL; return -1; } if (knet_handle_compress_cfg->compress_threshold == 0) { knet_h->compress_threshold = KNET_COMPRESS_THRESHOLD; log_debug(knet_h, KNET_SUB_COMPRESS, "resetting compression threshold to default (%d)", KNET_COMPRESS_THRESHOLD); } else { knet_h->compress_threshold = knet_handle_compress_cfg->compress_threshold; } savederrno = pthread_rwlock_rdlock(&shlib_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_COMPRESS, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!compress_check_lib_is_init(knet_h, cmp_model)) { /* * need to switch to write lock, load the lib, and return with a write lock * this is not racy because compress_load_lib is written idempotent. */ pthread_rwlock_unlock(&shlib_rwlock); savederrno = pthread_rwlock_wrlock(&shlib_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_COMPRESS, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (compress_load_lib(knet_h, cmp_model, 0) < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_COMPRESS, "Unable to load library: %s", strerror(savederrno)); err = -1; goto out_unlock; } } if (val_level(knet_h, cmp_model, knet_handle_compress_cfg->compress_level) < 0) { log_err(knet_h, KNET_SUB_COMPRESS, "compress level %d not supported for model %s", knet_handle_compress_cfg->compress_level, knet_handle_compress_cfg->compress_model); savederrno = EINVAL; err = -1; goto out_unlock; } knet_h->compress_model = cmp_model; knet_h->compress_level = knet_handle_compress_cfg->compress_level; if (compress_lib_test(knet_h) < 0) { savederrno = errno; err = -1; goto out_unlock; } out_unlock: pthread_rwlock_unlock(&shlib_rwlock); } if (err) { knet_h->compress_model = 0; knet_h->compress_level = 0; } errno = savederrno; return err; } void compress_fini( knet_handle_t knet_h, int all) { int savederrno = 0; int idx = 0; savederrno = pthread_rwlock_wrlock(&shlib_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_COMPRESS, "Unable to get write lock: %s", strerror(savederrno)); return; } while (idx < KNET_MAX_COMPRESS_METHODS) { if ((compress_modules_cmds[idx].model_name != NULL) && (compress_modules_cmds[idx].built_in == 1) && (compress_modules_cmds[idx].loaded == 1) && (compress_modules_cmds[idx].model_id > 0) && (knet_h->compress_int_data[idx] != NULL)) { if ((all) || (compress_modules_cmds[idx].model_id == knet_h->compress_model)) { if (compress_modules_cmds[idx].ops->fini != NULL) { compress_modules_cmds[idx].ops->fini(knet_h, idx); } else { knet_h->compress_int_data[idx] = NULL; } } } idx++; } pthread_rwlock_unlock(&shlib_rwlock); return; } /* * compress does not require compress_check_lib_is_init * because it's protected by compress_cfg */ int compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { return compress_modules_cmds[knet_h->compress_model].ops->compress(knet_h, buf_in, buf_in_len, buf_out, buf_out_len); } int decompress( knet_handle_t knet_h, int compress_model, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int savederrno = 0, err = 0; if (compress_model > max_model) { log_err(knet_h, KNET_SUB_COMPRESS, "Received packet with unknown compress model %d", compress_model); errno = EINVAL; return -1; } if (compress_is_valid_model(compress_model) < 0) { log_err(knet_h, KNET_SUB_COMPRESS, "Received packet compressed with %s but support is not built in this version of libknet. Please contact your distribution vendor or fix the build.", compress_modules_cmds[compress_model].model_name); errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&shlib_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_COMPRESS, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!compress_check_lib_is_init(knet_h, compress_model)) { /* * need to switch to write lock, load the lib, and return with a write lock * this is not racy because compress_load_lib is written idempotent. */ pthread_rwlock_unlock(&shlib_rwlock); savederrno = pthread_rwlock_wrlock(&shlib_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_COMPRESS, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (compress_load_lib(knet_h, compress_model, 1) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_COMPRESS, "Unable to load library: %s", strerror(savederrno)); goto out_unlock; } } err = compress_modules_cmds[compress_model].ops->decompress(knet_h, buf_in, buf_in_len, buf_out, buf_out_len); savederrno = errno; out_unlock: pthread_rwlock_unlock(&shlib_rwlock); errno = savederrno; return err; } int knet_handle_compress(knet_handle_t knet_h, struct knet_handle_compress_cfg *knet_handle_compress_cfg) { int savederrno = 0; int err = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!knet_handle_compress_cfg) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } compress_fini(knet_h, 0); err = compress_cfg(knet_h, knet_handle_compress_cfg); savederrno = errno; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_get_compress_list(struct knet_compress_info *compress_list, size_t *compress_list_entries) { int err = 0; int idx = 0; int outidx = 0; if (!compress_list_entries) { errno = EINVAL; return -1; } while (compress_modules_cmds[idx].model_name != NULL) { if (compress_modules_cmds[idx].built_in) { if (compress_list) { compress_list[outidx].name = compress_modules_cmds[idx].model_name; } outidx++; } idx++; } *compress_list_entries = outidx; if (!err) errno = 0; return err; } diff --git a/libknet/compress.h b/libknet/compress.h index 21ec4ebe..aa3eec54 100644 --- a/libknet/compress.h +++ b/libknet/compress.h @@ -1,36 +1,36 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_COMPRESS_H__ #define __KNET_COMPRESS_H__ #include "internals.h" int compress_init( knet_handle_t knet_h); void compress_fini( knet_handle_t knet_h, int all); int compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len); int decompress( knet_handle_t knet_h, int compress_model, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len); #endif diff --git a/libknet/compress_bzip2.c b/libknet/compress_bzip2.c index 4132ad1a..e919f606 100644 --- a/libknet/compress_bzip2.c +++ b/libknet/compress_bzip2.c @@ -1,126 +1,126 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include "logging.h" #include "compress_model.h" #ifdef BZIP2_COMPRESS_LEVEL #define KNET_COMPRESS_DEFAULT BZIP2_COMPRESS_LEVEL #else #define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT #endif static int bzip2_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int err = 0; int savederrno = 0; unsigned int destLen = KNET_DATABUFSIZE_COMPRESS; err = BZ2_bzBuffToBuffCompress((char *)buf_out, &destLen, (char *)buf_in, buf_in_len, knet_h->compress_level, 0, 0); switch(err) { case BZ_OK: *buf_out_len = destLen; break; case BZ_MEM_ERROR: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 compress has not enough memory"); savederrno = ENOMEM; err = -1; break; case BZ_OUTBUFF_FULL: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 unable to compress source in destination buffer"); savederrno = E2BIG; err = -1; break; default: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 compress unknown error %d", err); savederrno = EINVAL; err = -1; break; } errno = savederrno; return err; } static int bzip2_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int err = 0; int savederrno = 0; unsigned int destLen = KNET_DATABUFSIZE_COMPRESS; err = BZ2_bzBuffToBuffDecompress((char *)buf_out, &destLen, (char *)buf_in, buf_in_len, 0, 0); switch(err) { case BZ_OK: *buf_out_len = destLen; break; case BZ_MEM_ERROR: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 decompress has not enough memory"); savederrno = ENOMEM; err = -1; break; case BZ_OUTBUFF_FULL: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 unable to decompress source in destination buffer"); savederrno = E2BIG; err = -1; break; case BZ_DATA_ERROR: case BZ_DATA_ERROR_MAGIC: case BZ_UNEXPECTED_EOF: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 decompress detected input data corruption"); savederrno = EINVAL; err = -1; break; default: log_err(knet_h, KNET_SUB_BZIP2COMP, "bzip2 decompress unknown error %d", err); savederrno = EINVAL; err = -1; break; } errno = savederrno; return err; } static int bzip2_get_default_level() { return KNET_COMPRESS_DEFAULT; } compress_ops_t compress_model = { KNET_COMPRESS_MODEL_ABI, NULL, NULL, NULL, NULL, bzip2_compress, bzip2_decompress, bzip2_get_default_level }; diff --git a/libknet/compress_lz4.c b/libknet/compress_lz4.c index cf2bebcd..d6338073 100644 --- a/libknet/compress_lz4.c +++ b/libknet/compress_lz4.c @@ -1,104 +1,104 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #define ACCELERATION_DEFAULT 1 /* lz4 default compression level from lz4.c */ #include "config.h" #include #include #include "logging.h" #include "compress_model.h" #ifdef LZ4_COMPRESS_DEFAULT #define KNET_COMPRESS_DEFAULT LZ4_COMPRESS_DEFAULT #else #define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT #endif static int lz4_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzerr = LZ4_compress_fast((const char *)buf_in, (char *)buf_out, buf_in_len, KNET_DATABUFSIZE_COMPRESS, knet_h->compress_level); /* * data compressed */ if (lzerr > 0) { *buf_out_len = lzerr; } /* * unable to compress */ if (lzerr == 0) { *buf_out_len = buf_in_len; } /* * lz4 internal error */ if (lzerr < 0) { log_err(knet_h, KNET_SUB_LZ4COMP, "lz4 compression error: %d", lzerr); savederrno = EINVAL; err = -1; } errno = savederrno; return err; } static int lz4_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzerr = LZ4_decompress_safe((const char *)buf_in, (char *)buf_out, buf_in_len, KNET_DATABUFSIZE); if (lzerr < 0) { log_err(knet_h, KNET_SUB_LZ4COMP, "lz4 decompression error: %d", lzerr); savederrno = EINVAL; err = -1; } if (lzerr > 0) { *buf_out_len = lzerr; } errno = savederrno; return err; } static int lz4_get_default_level() { return KNET_COMPRESS_DEFAULT; } compress_ops_t compress_model = { KNET_COMPRESS_MODEL_ABI, NULL, NULL, NULL, NULL, lz4_compress, lz4_decompress, lz4_get_default_level }; diff --git a/libknet/compress_lz4hc.c b/libknet/compress_lz4hc.c index aecad904..aedabc69 100644 --- a/libknet/compress_lz4hc.c +++ b/libknet/compress_lz4hc.c @@ -1,113 +1,113 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include #include "logging.h" #include "compress_model.h" #ifdef LZ4HC_CLEVEL_DEFAULT #define KNET_COMPRESS_DEFAULT LZ4HC_CLEVEL_DEFAULT /* lz4hc default compression level from lz4hc.h */ #else #define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT #endif #ifdef LZ4HC_CLEVEL_MAX #define KNET_LZ4HC_MAX LZ4HC_CLEVEL_MAX #endif #ifdef LZ4HC_MAX_CLEVEL #define KNET_LZ4HC_MAX LZ4HC_MAX_CLEVEL #endif #ifndef KNET_LZ4HC_MAX /* * older releases of lz4 do not define LZ4HC_CLEVEL range. * According to lz4hc.h, any value between 0 and 16 is valid. * We defalt to 16 based on the comments in the include file * from older versions. */ #define KNET_LZ4HC_MAX 16 #endif static int lz4hc_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzerr = LZ4_compress_HC((const char *)buf_in, (char *)buf_out, buf_in_len, KNET_DATABUFSIZE_COMPRESS, knet_h->compress_level); /* * data compressed */ if (lzerr > 0) { *buf_out_len = lzerr; } /* * unable to compress */ if (lzerr <= 0) { log_err(knet_h, KNET_SUB_LZ4HCCOMP, "lz4hc compression error: %d", lzerr); savederrno = EINVAL; err = -1; } errno = savederrno; return err; } /* This is a straight copy from compress_lz4.c */ static int lz4_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzerr = LZ4_decompress_safe((const char *)buf_in, (char *)buf_out, buf_in_len, KNET_DATABUFSIZE); if (lzerr < 0) { log_err(knet_h, KNET_SUB_LZ4COMP, "lz4 decompression error: %d", lzerr); savederrno = EINVAL; err = -1; } if (lzerr > 0) { *buf_out_len = lzerr; } errno = savederrno; return err; } static int lz4hc_get_default_level() { return KNET_COMPRESS_DEFAULT; } compress_ops_t compress_model = { KNET_COMPRESS_MODEL_ABI, NULL, NULL, NULL, NULL, lz4hc_compress, lz4_decompress, lz4hc_get_default_level }; diff --git a/libknet/compress_lzma.c b/libknet/compress_lzma.c index baf40337..156f43ce 100644 --- a/libknet/compress_lzma.c +++ b/libknet/compress_lzma.c @@ -1,137 +1,137 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include "logging.h" #include "compress_model.h" #ifdef LZMA_PRESET_DEFAULT #define KNET_COMPRESS_DEFAULT LZMA_PRESET_DEFAULT /* lzma default compression level from lzma.h */ #else #define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT #endif static int lzma_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int err = 0; int savederrno = 0; size_t out_pos = 0; lzma_ret ret = 0; ret = lzma_easy_buffer_encode(knet_h->compress_level, LZMA_CHECK_NONE, NULL, (const uint8_t *)buf_in, buf_in_len, (uint8_t *)buf_out, &out_pos, KNET_DATABUFSIZE_COMPRESS); switch(ret) { case LZMA_OK: *buf_out_len = out_pos; break; case LZMA_MEM_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma compress memory allocation failed"); savederrno = ENOMEM; err = -1; break; case LZMA_MEMLIMIT_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma compress requires higher memory boundaries (see lzma_memlimit_set)"); savederrno = ENOMEM; err = -1; break; case LZMA_PROG_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma compress has been called with incorrect options"); savederrno = EINVAL; err = -1; break; default: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma compress unknown error %u", ret); savederrno = EINVAL; err = -1; break; } errno = savederrno; return err; } static int lzma_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int err = 0; int savederrno = 0; uint64_t memlimit = UINT64_MAX; /* disable lzma internal memlimit check */ size_t out_pos = 0, in_pos = 0; lzma_ret ret = 0; ret = lzma_stream_buffer_decode(&memlimit, 0, NULL, (const uint8_t *)buf_in, &in_pos, buf_in_len, (uint8_t *)buf_out, &out_pos, KNET_DATABUFSIZE_COMPRESS); switch(ret) { case LZMA_OK: *buf_out_len = out_pos; break; case LZMA_MEM_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress memory allocation failed"); savederrno = ENOMEM; err = -1; break; case LZMA_MEMLIMIT_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress requires higher memory boundaries (see lzma_memlimit_set)"); savederrno = ENOMEM; err = -1; break; case LZMA_DATA_ERROR: case LZMA_FORMAT_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress invalid data received"); savederrno = EINVAL; err = -1; break; case LZMA_PROG_ERROR: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress has been called with incorrect options"); savederrno = EINVAL; err = -1; break; default: log_err(knet_h, KNET_SUB_LZMACOMP, "lzma decompress unknown error %u", ret); savederrno = EINVAL; err = -1; break; } errno = savederrno; return err; } static int lzma_get_default_level() { return KNET_COMPRESS_DEFAULT; } compress_ops_t compress_model = { KNET_COMPRESS_MODEL_ABI, NULL, NULL, NULL, NULL, lzma_compress, lzma_decompress, lzma_get_default_level }; diff --git a/libknet/compress_lzo2.c b/libknet/compress_lzo2.c index e451e8a4..a1f98c24 100644 --- a/libknet/compress_lzo2.c +++ b/libknet/compress_lzo2.c @@ -1,178 +1,178 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #define LZO2_COMPRESS_DEFAULT 1 #include "config.h" #include #include #include #include #include "logging.h" #include "compress_model.h" #ifdef LZO2_COMPRESS_DEFAULT #define KNET_COMPRESS_DEFAULT LZO2_COMPRESS_DEFAULT #else #define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT #endif static int lzo2_is_init( knet_handle_t knet_h, int method_idx) { if (knet_h->compress_int_data[method_idx]) { return 1; } return 0; } static int lzo2_init( knet_handle_t knet_h, int method_idx) { /* * LZO1X_999_MEM_COMPRESS is the highest amount of memory lzo2 can use */ if (!knet_h->compress_int_data[method_idx]) { knet_h->compress_int_data[method_idx] = malloc(LZO1X_999_MEM_COMPRESS); if (!knet_h->compress_int_data[method_idx]) { log_err(knet_h, KNET_SUB_LZO2COMP, "lzo2 unable to allocate work memory"); errno = ENOMEM; return -1; } memset(knet_h->compress_int_data[method_idx], 0, LZO1X_999_MEM_COMPRESS); } return 0; } static void lzo2_fini( knet_handle_t knet_h, int method_idx) { if (knet_h->compress_int_data[method_idx]) { free(knet_h->compress_int_data[method_idx]); knet_h->compress_int_data[method_idx] = NULL; } return; } static int lzo2_val_level( knet_handle_t knet_h, int compress_level) { switch(compress_level) { case 1: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_1_compress internal compress method"); break; case 11: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_1_11_compress internal compress method"); break; case 12: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_1_12_compress internal compress method"); break; case 15: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_1_15_compress internal compress method"); break; case 999: log_debug(knet_h, KNET_SUB_LZO2COMP, "lzo2 will use lzo1x_999_compress internal compress method"); break; default: log_warn(knet_h, KNET_SUB_LZO2COMP, "Unknown lzo2 internal compress method. lzo1x_1_compress will be used as default fallback"); break; } return 0; } static int lzo2_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int savederrno = 0, lzerr = 0, err = 0; lzo_uint cmp_len; switch(knet_h->compress_level) { case 1: lzerr = lzo1x_1_compress(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; case 11: lzerr = lzo1x_1_11_compress(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; case 12: lzerr = lzo1x_1_12_compress(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; case 15: lzerr = lzo1x_1_15_compress(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; case 999: lzerr = lzo1x_999_compress(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; default: lzerr = lzo1x_1_compress(buf_in, buf_in_len, buf_out, &cmp_len, knet_h->compress_int_data[knet_h->compress_model]); break; } if (lzerr != LZO_E_OK) { log_err(knet_h, KNET_SUB_LZO2COMP, "lzo2 internal compression error"); savederrno = EAGAIN; err = -1; } else { *buf_out_len = cmp_len; } errno = savederrno; return err; } static int lzo2_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int lzerr = 0, err = 0; int savederrno = 0; lzo_uint decmp_len; lzerr = lzo1x_decompress(buf_in, buf_in_len, buf_out, &decmp_len, NULL); if (lzerr != LZO_E_OK) { log_err(knet_h, KNET_SUB_LZO2COMP, "lzo2 internal decompression error"); savederrno = EAGAIN; err = -1; } else { *buf_out_len = decmp_len; } errno = savederrno; return err; } static int lzo2_get_default_level() { return KNET_COMPRESS_DEFAULT; } compress_ops_t compress_model = { KNET_COMPRESS_MODEL_ABI, lzo2_is_init, lzo2_init, lzo2_fini, lzo2_val_level, lzo2_compress, lzo2_decompress, lzo2_get_default_level }; diff --git a/libknet/compress_model.h b/libknet/compress_model.h index 1da8d792..674e6553 100644 --- a/libknet/compress_model.h +++ b/libknet/compress_model.h @@ -1,97 +1,97 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_COMPRESS_MODEL_H__ #define __KNET_COMPRESS_MODEL_H__ #include "internals.h" #define KNET_COMPRESS_MODEL_ABI 2 #define KNET_COMPRESS_UNKNOWN_DEFAULT (-2) typedef struct { uint8_t abi_ver; /* * some libs need special init and handling of buffers etc. * is_init is called in shlib_rwlock read only context to see if * the module has been initialized within this knet_handle. * Providing is_init is optional. A module that does not export * an is_init and if the associated shared library is already loaded * is treated as "does not require init". */ int (*is_init) (knet_handle_t knet_h, int method_idx); /* * init is called when the library requires special init handling, * such as memory allocation and such. * init is invoked in shlib_rwlock write only context when * the module exports this function. * It is optional to provide an init function if the module * does not require any init. */ int (*init) (knet_handle_t knet_h, int method_idx); /* * fini is invoked only on knet_handle_free in a write only context. * It is optional to provide this function if the module * does not require any finalization */ void (*fini) (knet_handle_t knet_h, int method_idx); /* * runtime config validation and compress/decompress */ /* * val_level is called upon compress configuration changes * to make sure that the requested compress_level is valid * within the context of a given module. */ int (*val_level)(knet_handle_t knet_h, int compress_level); /* * required functions * * hopefully those 2 don't require any explanation.... */ int (*compress) (knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len); int (*decompress)(knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len); /* * Get default compression level */ int (*get_default_level) (void); } compress_ops_t; typedef struct { const char *model_name; uint8_t model_id; /* sequential unique identifier */ uint8_t built_in; /* set at configure/build time to 1 if available */ /* * library is loaded */ uint8_t loaded; /* * runtime bits */ compress_ops_t *ops; } compress_model_t; #endif diff --git a/libknet/compress_zlib.c b/libknet/compress_zlib.c index a9872e8b..edeb9440 100644 --- a/libknet/compress_zlib.c +++ b/libknet/compress_zlib.c @@ -1,129 +1,129 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include "logging.h" #include "compress_model.h" #ifdef Z_DEFAULT_COMPRESSION #define KNET_COMPRESS_DEFAULT Z_DEFAULT_COMPRESSION /* zlib default compression level from zlib.h */ #else #define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT #endif static int zlib_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int zerr = 0, err = 0; int savederrno = 0; uLongf destLen = *buf_out_len; zerr = compress2(buf_out, &destLen, buf_in, buf_in_len, knet_h->compress_level); *buf_out_len = destLen; switch(zerr) { case Z_OK: err = 0; savederrno = 0; break; case Z_MEM_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress mem error"); err = -1; savederrno = ENOMEM; break; case Z_BUF_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress buf error"); err = -1; savederrno = ENOBUFS; break; case Z_STREAM_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress stream error"); err = -1; savederrno = EINVAL; break; default: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib unknown compress error: %d", zerr); break; } errno = savederrno; return err; } static int zlib_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int zerr = 0, err = 0; int savederrno = 0; uLongf destLen = *buf_out_len; zerr = uncompress(buf_out, &destLen, buf_in, buf_in_len); *buf_out_len = destLen; switch(zerr) { case Z_OK: err = 0; savederrno = 0; break; case Z_MEM_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib decompress mem error"); err = -1; savederrno = ENOMEM; break; case Z_BUF_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib decompress buf error"); err = -1; savederrno = ENOBUFS; break; case Z_DATA_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib decompress data error"); err = -1; savederrno = EINVAL; break; default: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib unknown decompress error: %d", zerr); break; } errno = savederrno; return err; } static int zlib_get_default_level() { return KNET_COMPRESS_DEFAULT; } compress_ops_t compress_model = { KNET_COMPRESS_MODEL_ABI, NULL, NULL, NULL, NULL, zlib_compress, zlib_decompress, zlib_get_default_level }; diff --git a/libknet/compress_zstd.c b/libknet/compress_zstd.c index 6b77fd24..9a21e106 100644 --- a/libknet/compress_zstd.c +++ b/libknet/compress_zstd.c @@ -1,172 +1,172 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include #include #include "logging.h" #include "compress_model.h" #ifdef ZSTD_CLEVEL_DEFAULT #define KNET_COMPRESS_DEFAULT ZSTD_CLEVEL_DEFAULT /* zstd default compression level from zstd.h */ #else #define KNET_COMPRESS_DEFAULT KNET_COMPRESS_UNKNOWN_DEFAULT #endif struct zstd_ctx { ZSTD_CCtx* cctx; ZSTD_DCtx* dctx; }; static int zstd_is_init( knet_handle_t knet_h, int method_idx) { if (knet_h->compress_int_data[method_idx]) { return 1; } return 0; } static void zstd_fini( knet_handle_t knet_h, int method_idx) { struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model]; if (zstd_ctx) { if (zstd_ctx->cctx) { ZSTD_freeCCtx(zstd_ctx->cctx); } if (zstd_ctx->dctx) { ZSTD_freeDCtx(zstd_ctx->dctx); } free(knet_h->compress_int_data[method_idx]); knet_h->compress_int_data[method_idx] = NULL; } return; } static int zstd_init( knet_handle_t knet_h, int method_idx) { struct zstd_ctx *zstd_ctx; int err = 0; if (!knet_h->compress_int_data[method_idx]) { zstd_ctx = malloc(sizeof(struct zstd_ctx)); if (!zstd_ctx) { errno = ENOMEM; return -1; } memset(zstd_ctx, 0, sizeof(struct zstd_ctx)); knet_h->compress_int_data[method_idx] = zstd_ctx; zstd_ctx->cctx = ZSTD_createCCtx(); if (!zstd_ctx->cctx) { log_err(knet_h, KNET_SUB_ZSTDCOMP, "Unable to create compression context"); err = -1; goto out_err; } zstd_ctx->dctx = ZSTD_createDCtx(); if (!zstd_ctx->dctx) { log_err(knet_h, KNET_SUB_ZSTDCOMP, "Unable to create decompression context"); err = -1; goto out_err; } } out_err: if (err) { zstd_fini(knet_h, method_idx); } return err; } static int zstd_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model]; size_t compress_size; compress_size = ZSTD_compressCCtx(zstd_ctx->cctx, buf_out, *buf_out_len, buf_in, buf_in_len, knet_h->compress_level); if (ZSTD_isError(compress_size)) { log_err(knet_h, KNET_SUB_ZSTDCOMP, "error compressing packet: %s", ZSTD_getErrorName(compress_size)); /* * ZSTD has lots of internal errors that are not easy to map * to standard errnos. Use a generic one for now */ errno = EINVAL; return -1; } *buf_out_len = compress_size; return 0; } static int zstd_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { struct zstd_ctx *zstd_ctx = knet_h->compress_int_data[knet_h->compress_model]; size_t decompress_size; decompress_size = ZSTD_decompressDCtx(zstd_ctx->dctx, buf_out, *buf_out_len, buf_in, buf_in_len); if (ZSTD_isError(decompress_size)) { log_err(knet_h, KNET_SUB_ZSTDCOMP, "error decompressing packet: %s", ZSTD_getErrorName(decompress_size)); /* * ZSTD has lots of internal errors that are not easy to map * to standard errnos. Use a generic one for now */ errno = EINVAL; return -1; } *buf_out_len = decompress_size; return 0; } static int zstd_get_default_level() { return KNET_COMPRESS_DEFAULT; } compress_ops_t compress_model = { KNET_COMPRESS_MODEL_ABI, zstd_is_init, zstd_init, zstd_fini, NULL, zstd_compress, zstd_decompress, zstd_get_default_level }; diff --git a/libknet/crypto.c b/libknet/crypto.c index ca1f3234..dbe2fee9 100644 --- a/libknet/crypto.c +++ b/libknet/crypto.c @@ -1,457 +1,457 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "crypto.h" #include "crypto_model.h" #include "internals.h" #include "logging.h" #include "common.h" /* * internal module switch data */ static crypto_model_t crypto_modules_cmds[] = { { "nss", WITH_CRYPTO_NSS, 0, NULL }, { "openssl", WITH_CRYPTO_OPENSSL, 0, NULL }, { "gcrypt", WITH_CRYPTO_GCRYPT, 0, NULL }, { NULL, 0, 0, NULL } }; static int crypto_get_model(const char *model) { int idx = 0; while (crypto_modules_cmds[idx].model_name != NULL) { if (!strcmp(crypto_modules_cmds[idx].model_name, model)) return idx; idx++; } return -1; } /* * exported API */ int crypto_encrypt_and_sign ( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { return crypto_modules_cmds[knet_h->crypto_instance[knet_h->crypto_in_use_config]->model].ops->crypt(knet_h, knet_h->crypto_instance[knet_h->crypto_in_use_config], buf_in, buf_in_len, buf_out, buf_out_len); } int crypto_encrypt_and_signv ( knet_handle_t knet_h, const struct iovec *iov_in, int iovcnt_in, unsigned char *buf_out, ssize_t *buf_out_len) { return crypto_modules_cmds[knet_h->crypto_instance[knet_h->crypto_in_use_config]->model].ops->cryptv(knet_h, knet_h->crypto_instance[knet_h->crypto_in_use_config], iov_in, iovcnt_in, buf_out, buf_out_len); } int crypto_authenticate_and_decrypt ( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int i, err = 0; int multiple_configs = 0; uint8_t log_level = KNET_LOG_ERR; for (i = 1; i <= KNET_MAX_CRYPTO_INSTANCES; i++) { if (knet_h->crypto_instance[i]) { multiple_configs++; } } /* * attempt to decrypt first with the in-use config * to avoid excessive performance hit. */ if (multiple_configs > 1) { log_level = KNET_LOG_DEBUG; } if (knet_h->crypto_in_use_config) { err = crypto_modules_cmds[knet_h->crypto_instance[knet_h->crypto_in_use_config]->model].ops->decrypt(knet_h, knet_h->crypto_instance[knet_h->crypto_in_use_config], buf_in, buf_in_len, buf_out, buf_out_len, log_level); } else { err = -1; } /* * if we fail, try to use the other configurations */ if (err) { for (i = 1; i <= KNET_MAX_CRYPTO_INSTANCES; i++) { /* * in-use config was already attempted */ if (i == knet_h->crypto_in_use_config) { continue; } if (knet_h->crypto_instance[i]) { log_debug(knet_h, KNET_SUB_CRYPTO, "Alternative crypto configuration found, attempting to decrypt with config %u", i); err = crypto_modules_cmds[knet_h->crypto_instance[i]->model].ops->decrypt(knet_h, knet_h->crypto_instance[i], buf_in, buf_in_len, buf_out, buf_out_len, KNET_LOG_ERR); if (!err) { errno = 0; /* clear errno from previous failures */ return err; } log_debug(knet_h, KNET_SUB_CRYPTO, "Packet failed to decrypt with crypto config %u", i); } } } return err; } static int crypto_use_config( knet_handle_t knet_h, uint8_t config_num) { if ((config_num) && (!knet_h->crypto_instance[config_num])) { errno = EINVAL; return -1; } knet_h->crypto_in_use_config = config_num; if (config_num) { knet_h->sec_block_size = knet_h->crypto_instance[config_num]->sec_block_size; knet_h->sec_hash_size = knet_h->crypto_instance[config_num]->sec_hash_size; knet_h->sec_salt_size = knet_h->crypto_instance[config_num]->sec_salt_size; } else { knet_h->sec_block_size = 0; knet_h->sec_hash_size = 0; knet_h->sec_salt_size = 0; } force_pmtud_run(knet_h, KNET_SUB_CRYPTO, 1, 0); return 0; } static int crypto_init( knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg, uint8_t config_num) { int err = 0, savederrno = 0; int model = 0; struct crypto_instance *current = NULL, *new = NULL; current = knet_h->crypto_instance[config_num]; model = crypto_get_model(knet_handle_crypto_cfg->crypto_model); if (model < 0) { log_err(knet_h, KNET_SUB_CRYPTO, "model %s not supported", knet_handle_crypto_cfg->crypto_model); return -1; } if (crypto_modules_cmds[model].built_in == 0) { log_err(knet_h, KNET_SUB_CRYPTO, "this version of libknet was built without %s support. Please contact your vendor or fix the build.", knet_handle_crypto_cfg->crypto_model); return -1; } savederrno = pthread_rwlock_wrlock(&shlib_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_CRYPTO, "Unable to get write lock: %s", strerror(savederrno)); return -1; } if (!crypto_modules_cmds[model].loaded) { crypto_modules_cmds[model].ops = load_module (knet_h, "crypto", crypto_modules_cmds[model].model_name); if (!crypto_modules_cmds[model].ops) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_CRYPTO, "Unable to load %s lib", crypto_modules_cmds[model].model_name); goto out; } if (crypto_modules_cmds[model].ops->abi_ver != KNET_CRYPTO_MODEL_ABI) { savederrno = EINVAL; err = -1; log_err(knet_h, KNET_SUB_CRYPTO, "ABI mismatch loading module %s. knet ver: %d, module ver: %d", crypto_modules_cmds[model].model_name, KNET_CRYPTO_MODEL_ABI, crypto_modules_cmds[model].ops->abi_ver); goto out; } crypto_modules_cmds[model].loaded = 1; } log_debug(knet_h, KNET_SUB_CRYPTO, "Initializing crypto module [%s/%s/%s]", knet_handle_crypto_cfg->crypto_model, knet_handle_crypto_cfg->crypto_cipher_type, knet_handle_crypto_cfg->crypto_hash_type); new = malloc(sizeof(struct crypto_instance)); if (!new) { savederrno = ENOMEM; err = -1; log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto instance"); goto out; } /* * if crypto_modules_cmds.ops->init fails, it is expected that * it will clean everything by itself. * crypto_modules_cmds.ops->fini is not invoked on error. */ new->model = model; if (crypto_modules_cmds[model].ops->init(knet_h, new, knet_handle_crypto_cfg)) { savederrno = errno; err = -1; goto out; } out: if (!err) { knet_h->crypto_instance[config_num] = new; if (current) { /* * if we are replacing the current config, we need to enable it right away */ if (knet_h->crypto_in_use_config == config_num) { crypto_use_config(knet_h, config_num); } if (crypto_modules_cmds[current->model].ops->fini != NULL) { crypto_modules_cmds[current->model].ops->fini(knet_h, current); } free(current); } } else { if (new) { free(new); } } pthread_rwlock_unlock(&shlib_rwlock); errno = err ? savederrno : 0; return err; } static void crypto_fini_config( knet_handle_t knet_h, uint8_t config_num) { if (knet_h->crypto_instance[config_num]) { if (crypto_modules_cmds[knet_h->crypto_instance[config_num]->model].ops->fini != NULL) { crypto_modules_cmds[knet_h->crypto_instance[config_num]->model].ops->fini(knet_h, knet_h->crypto_instance[config_num]); } free(knet_h->crypto_instance[config_num]); knet_h->crypto_instance[config_num] = NULL; } } void crypto_fini( knet_handle_t knet_h, uint8_t config_num) { int savederrno = 0, i; savederrno = pthread_rwlock_wrlock(&shlib_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_CRYPTO, "Unable to get write lock: %s", strerror(savederrno)); return; } if (config_num > KNET_MAX_CRYPTO_INSTANCES) { for (i = 1; i <= KNET_MAX_CRYPTO_INSTANCES; i++) { crypto_fini_config(knet_h, i); } } else { crypto_fini_config(knet_h, config_num); } pthread_rwlock_unlock(&shlib_rwlock); return; } int knet_handle_crypto_set_config(knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg, uint8_t config_num) { int savederrno = 0; int err = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!knet_handle_crypto_cfg) { errno = EINVAL; return -1; } if ((config_num < 1) || (config_num > KNET_MAX_CRYPTO_INSTANCES)) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (knet_h->crypto_in_use_config == config_num) { savederrno = EBUSY; err = -1; goto exit_unlock; } if ((!strncmp("none", knet_handle_crypto_cfg->crypto_model, 4)) || ((!strncmp("none", knet_handle_crypto_cfg->crypto_cipher_type, 4)) && (!strncmp("none", knet_handle_crypto_cfg->crypto_hash_type, 4)))) { crypto_fini(knet_h, config_num); log_debug(knet_h, KNET_SUB_CRYPTO, "crypto config %u is not enabled", config_num); err = 0; goto exit_unlock; } if (knet_handle_crypto_cfg->private_key_len < KNET_MIN_KEY_LEN) { log_debug(knet_h, KNET_SUB_CRYPTO, "private key len too short for config %u (min %d): %u", config_num, KNET_MIN_KEY_LEN, knet_handle_crypto_cfg->private_key_len); savederrno = EINVAL; err = -1; goto exit_unlock; } if (knet_handle_crypto_cfg->private_key_len > KNET_MAX_KEY_LEN) { log_debug(knet_h, KNET_SUB_CRYPTO, "private key len too long for config %u (max %d): %u", config_num, KNET_MAX_KEY_LEN, knet_handle_crypto_cfg->private_key_len); savederrno = EINVAL; err = -1; goto exit_unlock; } err = crypto_init(knet_h, knet_handle_crypto_cfg, config_num); if (err) { err = -2; savederrno = errno; } exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_handle_crypto_rx_clear_traffic(knet_handle_t knet_h, uint8_t value) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (value > KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->crypto_only = value; if (knet_h->crypto_only) { log_debug(knet_h, KNET_SUB_CRYPTO, "Only crypto traffic allowed for RX"); } else { log_debug(knet_h, KNET_SUB_CRYPTO, "Both crypto and clear traffic allowed for RX"); } pthread_rwlock_unlock(&knet_h->global_rwlock); return 0; } int knet_handle_crypto_use_config(knet_handle_t knet_h, uint8_t config_num) { int savederrno = 0; int err = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (config_num > KNET_MAX_CRYPTO_INSTANCES) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } err = crypto_use_config(knet_h, config_num); savederrno = errno; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_get_crypto_list(struct knet_crypto_info *crypto_list, size_t *crypto_list_entries) { int err = 0; int idx = 0; int outidx = 0; if (!crypto_list_entries) { errno = EINVAL; return -1; } while (crypto_modules_cmds[idx].model_name != NULL) { if (crypto_modules_cmds[idx].built_in) { if (crypto_list) { crypto_list[outidx].name = crypto_modules_cmds[idx].model_name; } outidx++; } idx++; } *crypto_list_entries = outidx; if (!err) errno = 0; return err; } diff --git a/libknet/crypto.h b/libknet/crypto.h index fd49e20e..c8d60d73 100644 --- a/libknet/crypto.h +++ b/libknet/crypto.h @@ -1,39 +1,39 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_CRYPTO_H__ #define __KNET_CRYPTO_H__ #include "internals.h" int crypto_authenticate_and_decrypt ( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len); int crypto_encrypt_and_sign ( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len); int crypto_encrypt_and_signv ( knet_handle_t knet_h, const struct iovec *iov_in, int iovcnt_in, unsigned char *buf_out, ssize_t *buf_out_len); void crypto_fini( knet_handle_t knet_h, uint8_t config_num); #endif diff --git a/libknet/crypto_gcrypt.c b/libknet/crypto_gcrypt.c index 2e0a143b..04ab0d6b 100644 --- a/libknet/crypto_gcrypt.c +++ b/libknet/crypto_gcrypt.c @@ -1,598 +1,598 @@ /* - * Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include /* * make sure NOT to use deprecated API */ #define GCRYPTO_NO_DEPRECATED 1 #define NEED_LIBGCRYPT_VERSION "1.8.0" #include #include "logging.h" #include "crypto_model.h" /* * crypto definitions and conversion tables */ #define SALT_SIZE 16 /* * gcrypt rejects private key len > crypto max keylen. openssl/nss automatically trim the key. * we need to store a crypto key len and a hash keylen separately * and crypto key len is trimmed automatically at gcrypt_init time. */ struct gcryptcrypto_instance { void *private_key; size_t crypt_private_key_len; size_t hash_private_key_len; int crypto_cipher_type; int crypto_hash_type; }; static int gcrypt_is_init = 0; /* * crypt/decrypt functions */ static int encrypt_gcrypt( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const struct iovec *iov, int iovcnt, unsigned char *buf_out, ssize_t *buf_out_len) { struct gcryptcrypto_instance *instance = crypto_instance->model_instance; gcry_error_t gerr; gcry_cipher_hd_t handle = NULL; int err = 0; int i; unsigned char *salt = buf_out; size_t output_len = 0, pad_len = 0; unsigned char inbuf[KNET_DATABUFSIZE_CRYPT]; unsigned char *data = buf_out + SALT_SIZE; gerr = gcry_cipher_open(&handle, instance->crypto_cipher_type, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_SECURE); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to allocate gcrypt cipher context: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } gerr = gcry_cipher_setkey(handle, instance->private_key, instance->crypt_private_key_len); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to load private key: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } gcry_randomize(salt, SALT_SIZE, GCRY_VERY_STRONG_RANDOM); gerr = gcry_cipher_setiv(handle, salt, SALT_SIZE); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to load init vector: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } /* * libgcrypt requires an input buffer that is * already aligned to block size. * The easiest way is to build the data in * a dedicated buffer */ output_len = 0; for (i=0; isec_block_size - (output_len % crypto_instance->sec_block_size)); memset(inbuf + output_len, pad_len, pad_len); output_len = output_len + pad_len; /* * some ciphers methods require _final to be called * before the last call to _encrypt, for example when * encrypting big chunks of data split in multiple buffers. * knet only has one buffer, so we can safely call _final here. * adding a comment as the code looks backwards compared to other * cipher implementations that do final _after_ encrypting the last block */ gcry_cipher_final(handle); gerr = gcry_cipher_encrypt(handle, data, output_len, inbuf, output_len); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to encrypt data: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } *buf_out_len = output_len + SALT_SIZE; out_err: if (handle) { gcry_cipher_close(handle); } return err; } static int decrypt_gcrypt( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len, uint8_t log_level) { struct gcryptcrypto_instance *instance = crypto_instance->model_instance; gcry_error_t gerr; gcry_cipher_hd_t handle = NULL; unsigned char *salt = (unsigned char *)buf_in; unsigned char *data = salt + SALT_SIZE; int datalen = buf_in_len - SALT_SIZE; int err = 0; if (datalen <= 0) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Packet is too short"); err = -1; goto out_err; } gerr = gcry_cipher_open(&handle, instance->crypto_cipher_type, GCRY_CIPHER_MODE_CBC, GCRY_CIPHER_SECURE); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to allocate gcrypt cipher context: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } gerr = gcry_cipher_setkey(handle, instance->private_key, instance->crypt_private_key_len); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to load private key: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } gerr = gcry_cipher_setiv(handle, salt, SALT_SIZE); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to load init vector: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } gerr = gcry_cipher_decrypt(handle, buf_out, KNET_DATABUFSIZE_CRYPT, data, datalen); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to decrypt data: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } /* * drop the padding size based on PCKS standards * (see also crypt above) */ *buf_out_len = datalen - buf_out[datalen - 1]; out_err: if (handle) { gcry_cipher_close(handle); } return err; } /* * hash/hmac/digest functions */ static int calculate_gcrypt_hash( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf, const size_t buf_len, unsigned char *hash, uint8_t log_level) { struct gcryptcrypto_instance *instance = crypto_instance->model_instance; gcry_error_t gerr; gcry_mac_hd_t handle = NULL; int err = 0; size_t outlen = crypto_instance->sec_hash_size; gerr = gcry_mac_open(&handle, instance->crypto_hash_type, GCRY_MAC_FLAG_SECURE, 0); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to allocate gcrypt hmac context: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } gerr = gcry_mac_setkey(handle, instance->private_key, instance->hash_private_key_len); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to set gcrypt hmac key: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } gerr = gcry_mac_write(handle, buf, buf_len); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to calculate gcrypt hmac: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } gerr = gcry_mac_read(handle, hash, &outlen); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to retrive gcrypt hmac: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); err = -1; goto out_err; } out_err: if (handle) { gcry_mac_close(handle); } return err; } /* * exported API */ static int gcryptcrypto_encrypt_and_signv ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const struct iovec *iov_in, int iovcnt_in, unsigned char *buf_out, ssize_t *buf_out_len) { struct gcryptcrypto_instance *instance = crypto_instance->model_instance; int i; if (instance->crypto_cipher_type) { if (encrypt_gcrypt(knet_h, crypto_instance, iov_in, iovcnt_in, buf_out, buf_out_len) < 0) { return -1; } } else { *buf_out_len = 0; for (i=0; icrypto_hash_type) { if (calculate_gcrypt_hash(knet_h, crypto_instance, buf_out, *buf_out_len, buf_out + *buf_out_len, KNET_LOG_ERR) < 0) { return -1; } *buf_out_len = *buf_out_len + crypto_instance->sec_hash_size; } return 0; } static int gcryptcrypto_encrypt_and_sign ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { struct iovec iov_in; memset(&iov_in, 0, sizeof(iov_in)); iov_in.iov_base = (unsigned char *)buf_in; iov_in.iov_len = buf_in_len; return gcryptcrypto_encrypt_and_signv(knet_h, crypto_instance, &iov_in, 1, buf_out, buf_out_len); } static int gcryptcrypto_authenticate_and_decrypt ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len, uint8_t log_level) { struct gcryptcrypto_instance *instance = crypto_instance->model_instance; ssize_t temp_len = buf_in_len; if (instance->crypto_hash_type) { unsigned char tmp_hash[crypto_instance->sec_hash_size]; ssize_t temp_buf_len = buf_in_len - crypto_instance->sec_hash_size; if ((temp_buf_len <= 0) || (temp_buf_len > KNET_MAX_PACKET_SIZE)) { log_debug(knet_h, KNET_SUB_GCRYPTCRYPTO, "Received incorrect packet size: %zu for hash size: %zu", buf_in_len, crypto_instance->sec_hash_size); return -1; } if (calculate_gcrypt_hash(knet_h, crypto_instance, buf_in, temp_buf_len, tmp_hash, log_level) < 0) { return -1; } if (memcmp(tmp_hash, buf_in + temp_buf_len, crypto_instance->sec_hash_size) != 0) { if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_GCRYPTCRYPTO, "Digest does not match. Check crypto key and configuration."); } else { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Digest does not match. Check crypto key and configuration."); } return -1; } temp_len = temp_len - crypto_instance->sec_hash_size; *buf_out_len = temp_len; } if (instance->crypto_cipher_type) { if (decrypt_gcrypt(knet_h, crypto_instance, buf_in, temp_len, buf_out, buf_out_len, log_level) < 0) { return -1; } } else { memmove(buf_out, buf_in, temp_len); *buf_out_len = temp_len; } return 0; } static void gcryptcrypto_fini( knet_handle_t knet_h, struct crypto_instance *crypto_instance) { struct gcryptcrypto_instance *gcryptcrypto_instance = crypto_instance->model_instance; if (gcryptcrypto_instance) { if (gcryptcrypto_instance->private_key) { free(gcryptcrypto_instance->private_key); gcryptcrypto_instance->private_key = NULL; } free(gcryptcrypto_instance); crypto_instance->model_instance = NULL; } return; } static int gcryptcrypto_init( knet_handle_t knet_h, struct crypto_instance *crypto_instance, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) { struct gcryptcrypto_instance *gcryptcrypto_instance = NULL; gcry_error_t gerr; int savederrno; /* * gcrypt name to ID mapping requires HMAC_ in the name. * so for example to use SHA1, the name should be HMAC_SHA1 * that makes it not compatible with nss/openssl naming. * make sure to add HMAC_ transparently so that changing crypto config * can be done transparently. */ char remap_hash_type[sizeof(knet_handle_crypto_cfg->crypto_hash_type) + strlen("HMAC_") + 1]; log_debug(knet_h, KNET_SUB_GCRYPTCRYPTO, "Initializing gcrypt crypto module [%s/%s]", knet_handle_crypto_cfg->crypto_cipher_type, knet_handle_crypto_cfg->crypto_hash_type); if (!gcrypt_is_init) { if (!gcry_check_version(NEED_LIBGCRYPT_VERSION)) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "libgcrypt is too old (need %s, have %s)", NEED_LIBGCRYPT_VERSION, gcry_check_version (NULL)); errno = EINVAL; return -1; } gerr = gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to suppress sec mem warnings: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); errno = EINVAL; return -1; } gerr = gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to init sec mem: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); errno = ENOMEM; return -1; } gerr = gcry_control(GCRYCTL_RESUME_SECMEM_WARN); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to restore sec mem warnings: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); errno = EINVAL; return -1; } gerr = gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0); if (gerr) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to notify gcrypt that init is completed: %s/%s", gcry_strsource(gerr), gcry_strerror(gerr)); errno = EINVAL; return -1; } if (!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P)) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "gcrypt could not initialize properly"); errno = EINVAL; return -1; } gcrypt_is_init = 1; } crypto_instance->model_instance = malloc(sizeof(struct gcryptcrypto_instance)); if (!crypto_instance->model_instance) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to allocate memory for gcrypt model instance"); errno = ENOMEM; return -1; } gcryptcrypto_instance = crypto_instance->model_instance; memset(gcryptcrypto_instance, 0, sizeof(struct gcryptcrypto_instance)); gcryptcrypto_instance->private_key = malloc(knet_handle_crypto_cfg->private_key_len); if (!gcryptcrypto_instance->private_key) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "Unable to allocate memory for gcrypt private key"); savederrno = ENOMEM; goto out_err; } memmove(gcryptcrypto_instance->private_key, knet_handle_crypto_cfg->private_key, knet_handle_crypto_cfg->private_key_len); if (strcmp(knet_handle_crypto_cfg->crypto_cipher_type, "none") == 0) { gcryptcrypto_instance->crypto_cipher_type = 0; } else { gcryptcrypto_instance->crypto_cipher_type = gcry_cipher_map_name(knet_handle_crypto_cfg->crypto_cipher_type); if (!gcryptcrypto_instance->crypto_cipher_type) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "unknown crypto cipher type requested"); savederrno = EINVAL; goto out_err; } if (gcry_cipher_test_algo(gcryptcrypto_instance->crypto_cipher_type)) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "requested crypto cipher type not available for use"); savederrno = EINVAL; goto out_err; } if (gcry_cipher_get_algo_keylen(gcryptcrypto_instance->crypto_cipher_type) < knet_handle_crypto_cfg->private_key_len) { log_warn(knet_h, KNET_SUB_GCRYPTCRYPTO, "requested crypto cipher key len (%u) too big (max: %zu)", knet_handle_crypto_cfg->private_key_len, gcry_cipher_get_algo_keylen(gcryptcrypto_instance->crypto_cipher_type)); gcryptcrypto_instance->crypt_private_key_len = gcry_cipher_get_algo_keylen(gcryptcrypto_instance->crypto_cipher_type); } else { gcryptcrypto_instance->crypt_private_key_len = knet_handle_crypto_cfg->private_key_len; } } if (strcmp(knet_handle_crypto_cfg->crypto_hash_type, "none") == 0) { gcryptcrypto_instance->crypto_hash_type = 0; } else { if (!strncasecmp(knet_handle_crypto_cfg->crypto_hash_type, "HMAC_", strlen("HMAC_"))) { strncpy(remap_hash_type, knet_handle_crypto_cfg->crypto_hash_type, sizeof(remap_hash_type) - 1); } else { snprintf(remap_hash_type, sizeof(remap_hash_type) - 1, "%s%s", "HMAC_", knet_handle_crypto_cfg->crypto_hash_type); } gcryptcrypto_instance->crypto_hash_type = gcry_mac_map_name(remap_hash_type); if (!gcryptcrypto_instance->crypto_hash_type) { savederrno = EINVAL; goto out_err; } if (gcry_mac_test_algo(gcryptcrypto_instance->crypto_hash_type)) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "requested crypto hash type not available for use"); savederrno = EINVAL; goto out_err; } gcryptcrypto_instance->hash_private_key_len = knet_handle_crypto_cfg->private_key_len; } if ((gcryptcrypto_instance->crypto_cipher_type) && (!gcryptcrypto_instance->crypto_hash_type)) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "crypto communication requires hash specified"); savederrno = EINVAL; goto out_err; } if (gcryptcrypto_instance->crypto_hash_type) { crypto_instance->sec_hash_size = gcry_mac_get_algo_maclen(gcryptcrypto_instance->crypto_hash_type); if (!crypto_instance->sec_hash_size) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "unable to gather hash digest size"); savederrno = EINVAL; goto out_err; } } if (gcryptcrypto_instance->crypto_cipher_type) { size_t block_size; block_size = gcry_cipher_get_algo_blklen(gcryptcrypto_instance->crypto_cipher_type); if (!block_size) { log_err(knet_h, KNET_SUB_GCRYPTCRYPTO, "unable to gather cipher blocksize"); savederrno = EINVAL; goto out_err; } crypto_instance->sec_salt_size = SALT_SIZE; crypto_instance->sec_block_size = block_size; } return 0; out_err: gcryptcrypto_fini(knet_h, crypto_instance); errno = savederrno; return -1; } crypto_ops_t crypto_model = { KNET_CRYPTO_MODEL_ABI, gcryptcrypto_init, gcryptcrypto_fini, gcryptcrypto_encrypt_and_sign, gcryptcrypto_encrypt_and_signv, gcryptcrypto_authenticate_and_decrypt }; diff --git a/libknet/crypto_model.h b/libknet/crypto_model.h index eca7f095..f9b5d914 100644 --- a/libknet/crypto_model.h +++ b/libknet/crypto_model.h @@ -1,62 +1,62 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_CRYPTO_MODEL_H__ #define __KNET_CRYPTO_MODEL_H__ #include "internals.h" struct crypto_instance { int model; void *model_instance; size_t sec_block_size; size_t sec_hash_size; size_t sec_salt_size; }; #define KNET_CRYPTO_MODEL_ABI 4 /* * see compress_model.h for explanation of the various lib related functions */ typedef struct { uint8_t abi_ver; int (*init) (knet_handle_t knet_h, struct crypto_instance *crypto_instance, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg); void (*fini) (knet_handle_t knet_h, struct crypto_instance *crypto_instance); int (*crypt) (knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len); int (*cryptv) (knet_handle_t knet_h, struct crypto_instance *crypto_instance, const struct iovec *iov_in, int iovcnt_in, unsigned char *buf_out, ssize_t *buf_out_len); int (*decrypt) (knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len, uint8_t log_level); } crypto_ops_t; typedef struct { const char *model_name; uint8_t built_in; uint8_t loaded; crypto_ops_t *ops; } crypto_model_t; #endif diff --git a/libknet/crypto_nss.c b/libknet/crypto_nss.c index e61a7be3..87338293 100644 --- a/libknet/crypto_nss.c +++ b/libknet/crypto_nss.c @@ -1,875 +1,875 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "crypto_model.h" #include "logging.h" static int nss_db_is_init = 0; static void nss_atexit_handler(void) { if (nss_db_is_init) { NSS_Shutdown(); if (PR_Initialized()) { PL_ArenaFinish(); PR_Cleanup(); } } return; } /* * crypto definitions and conversion tables */ #define SALT_SIZE 16 /* * This are defined in new NSS. For older one, we will define our own */ #ifndef AES_256_KEY_LENGTH #define AES_256_KEY_LENGTH 32 #endif #ifndef AES_192_KEY_LENGTH #define AES_192_KEY_LENGTH 24 #endif #ifndef AES_128_KEY_LENGTH #define AES_128_KEY_LENGTH 16 #endif enum nsscrypto_crypt_t { CRYPTO_CIPHER_TYPE_NONE = 0, CRYPTO_CIPHER_TYPE_AES256 = 1, CRYPTO_CIPHER_TYPE_AES192 = 2, CRYPTO_CIPHER_TYPE_AES128 = 3 }; CK_MECHANISM_TYPE cipher_to_nss[] = { 0, /* CRYPTO_CIPHER_TYPE_NONE */ CKM_AES_CBC_PAD, /* CRYPTO_CIPHER_TYPE_AES256 */ CKM_AES_CBC_PAD, /* CRYPTO_CIPHER_TYPE_AES192 */ CKM_AES_CBC_PAD /* CRYPTO_CIPHER_TYPE_AES128 */ }; size_t nsscipher_key_len[] = { 0, /* CRYPTO_CIPHER_TYPE_NONE */ AES_256_KEY_LENGTH, /* CRYPTO_CIPHER_TYPE_AES256 */ AES_192_KEY_LENGTH, /* CRYPTO_CIPHER_TYPE_AES192 */ AES_128_KEY_LENGTH /* CRYPTO_CIPHER_TYPE_AES128 */ }; size_t nsscypher_block_len[] = { 0, /* CRYPTO_CIPHER_TYPE_NONE */ AES_BLOCK_SIZE, /* CRYPTO_CIPHER_TYPE_AES256 */ AES_BLOCK_SIZE, /* CRYPTO_CIPHER_TYPE_AES192 */ AES_BLOCK_SIZE /* CRYPTO_CIPHER_TYPE_AES128 */ }; /* * hash definitions and conversion tables */ enum nsscrypto_hash_t { CRYPTO_HASH_TYPE_NONE = 0, CRYPTO_HASH_TYPE_MD5 = 1, CRYPTO_HASH_TYPE_SHA1 = 2, CRYPTO_HASH_TYPE_SHA256 = 3, CRYPTO_HASH_TYPE_SHA384 = 4, CRYPTO_HASH_TYPE_SHA512 = 5 }; CK_MECHANISM_TYPE hash_to_nss[] = { 0, /* CRYPTO_HASH_TYPE_NONE */ CKM_MD5_HMAC, /* CRYPTO_HASH_TYPE_MD5 */ CKM_SHA_1_HMAC, /* CRYPTO_HASH_TYPE_SHA1 */ CKM_SHA256_HMAC, /* CRYPTO_HASH_TYPE_SHA256 */ CKM_SHA384_HMAC, /* CRYPTO_HASH_TYPE_SHA384 */ CKM_SHA512_HMAC /* CRYPTO_HASH_TYPE_SHA512 */ }; size_t nsshash_len[] = { 0, /* CRYPTO_HASH_TYPE_NONE */ MD5_LENGTH, /* CRYPTO_HASH_TYPE_MD5 */ SHA1_LENGTH, /* CRYPTO_HASH_TYPE_SHA1 */ SHA256_LENGTH, /* CRYPTO_HASH_TYPE_SHA256 */ SHA384_LENGTH, /* CRYPTO_HASH_TYPE_SHA384 */ SHA512_LENGTH /* CRYPTO_HASH_TYPE_SHA512 */ }; enum sym_key_type { SYM_KEY_TYPE_CRYPT, SYM_KEY_TYPE_HASH }; struct nsscrypto_instance { PK11SymKey *nss_sym_key; PK11SymKey *nss_sym_key_sign; unsigned char *private_key; unsigned int private_key_len; int crypto_cipher_type; int crypto_hash_type; }; /* * crypt/decrypt functions */ static int nssstring_to_crypto_cipher_type(const char* crypto_cipher_type) { if (strcmp(crypto_cipher_type, "none") == 0) { return CRYPTO_CIPHER_TYPE_NONE; } else if (strcmp(crypto_cipher_type, "aes256") == 0) { return CRYPTO_CIPHER_TYPE_AES256; } else if (strcmp(crypto_cipher_type, "aes192") == 0) { return CRYPTO_CIPHER_TYPE_AES192; } else if (strcmp(crypto_cipher_type, "aes128") == 0) { return CRYPTO_CIPHER_TYPE_AES128; } return -1; } static PK11SymKey *nssimport_symmetric_key(knet_handle_t knet_h, struct crypto_instance *crypto_instance, enum sym_key_type key_type) { struct nsscrypto_instance *instance = crypto_instance->model_instance; SECItem key_item; PK11SlotInfo *slot; PK11SymKey *res_key; CK_MECHANISM_TYPE cipher; CK_ATTRIBUTE_TYPE operation; CK_MECHANISM_TYPE wrap_mechanism; int wrap_key_len; PK11SymKey *wrap_key; PK11Context *wrap_key_crypt_context; SECItem tmp_sec_item; SECItem wrapped_key; int wrapped_key_len; int wrap_key_block_size; unsigned char wrapped_key_data[KNET_MAX_KEY_LEN]; unsigned char pad_key_data[KNET_MAX_KEY_LEN]; memset(&key_item, 0, sizeof(key_item)); slot = NULL; wrap_key = NULL; res_key = NULL; wrap_key_crypt_context = NULL; if (instance->private_key_len > sizeof(pad_key_data)) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Import symmetric key failed. Private key is too long"); goto exit_res_key; } memset(pad_key_data, 0, sizeof(pad_key_data)); memcpy(pad_key_data, instance->private_key, instance->private_key_len); key_item.type = siBuffer; key_item.data = pad_key_data; switch (key_type) { case SYM_KEY_TYPE_CRYPT: key_item.len = nsscipher_key_len[instance->crypto_cipher_type]; cipher = cipher_to_nss[instance->crypto_cipher_type]; operation = CKA_ENCRYPT|CKA_DECRYPT; break; case SYM_KEY_TYPE_HASH: key_item.len = instance->private_key_len; cipher = hash_to_nss[instance->crypto_hash_type]; operation = CKA_SIGN; break; default: log_err(knet_h, KNET_SUB_NSSCRYPTO, "Import symmetric key failed. Unknown keyimport request"); goto exit_res_key; break; } slot = PK11_GetBestSlot(cipher, NULL); if (slot == NULL) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to find security slot (%d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto exit_res_key; } /* * Without FIPS it would be possible to just use * res_key = PK11_ImportSymKey(slot, cipher, PK11_OriginUnwrap, operation, &key_item, NULL); * with FIPS NSS Level 2 certification has to be "workarounded" (so it becomes Level 1) by using * following method: * 1. Generate wrap key * 2. Encrypt authkey with wrap key * 3. Unwrap encrypted authkey using wrap key */ /* * Generate wrapping key */ wrap_mechanism = PK11_GetBestWrapMechanism(slot); wrap_key_len = PK11_GetBestKeyLength(slot, wrap_mechanism); wrap_key = PK11_KeyGen(slot, wrap_mechanism, NULL, wrap_key_len, NULL); if (wrap_key == NULL) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to generate wrapping key (%d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto exit_res_key; } /* * Encrypt authkey with wrapping key */ /* * Key must be padded to a block size */ wrap_key_block_size = PK11_GetBlockSize(wrap_mechanism, 0); if (wrap_key_block_size < 0) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to get wrap key block size (%d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto exit_res_key; } if (sizeof(pad_key_data) % wrap_key_block_size != 0) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Padded key buffer size (%zu) is not dividable by " "wrap key block size (%u).", sizeof(pad_key_data), (unsigned int)wrap_key_block_size); goto exit_res_key; } /* * Initialization of IV is not needed because PK11_GetBestWrapMechanism should return ECB mode */ memset(&tmp_sec_item, 0, sizeof(tmp_sec_item)); wrap_key_crypt_context = PK11_CreateContextBySymKey(wrap_mechanism, CKA_ENCRYPT, wrap_key, &tmp_sec_item); if (wrap_key_crypt_context == NULL) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to create encrypt context (%d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto exit_res_key; } wrapped_key_len = (int)sizeof(wrapped_key_data); if (PK11_CipherOp(wrap_key_crypt_context, wrapped_key_data, &wrapped_key_len, sizeof(wrapped_key_data), key_item.data, sizeof(pad_key_data)) != SECSuccess) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to encrypt authkey (%d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto exit_res_key; } if (PK11_Finalize(wrap_key_crypt_context) != SECSuccess) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to finalize encryption of authkey (%d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto exit_res_key; } /* * Finally unwrap sym key */ memset(&tmp_sec_item, 0, sizeof(tmp_sec_item)); wrapped_key.data = wrapped_key_data; wrapped_key.len = wrapped_key_len; res_key = PK11_UnwrapSymKey(wrap_key, wrap_mechanism, &tmp_sec_item, &wrapped_key, cipher, operation, key_item.len); if (res_key == NULL) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Failure to import key into NSS (%d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); if (PR_GetError() == SEC_ERROR_BAD_DATA) { /* * Maximum key length for FIPS enabled softtoken is limited to * MAX_KEY_LEN (pkcs11i.h - 256) and checked in NSC_UnwrapKey. Returned * error is CKR_TEMPLATE_INCONSISTENT which is mapped to SEC_ERROR_BAD_DATA. */ log_err(knet_h, KNET_SUB_NSSCRYPTO, "Secret key is probably too long. " "Try reduce it to 256 bytes"); } goto exit_res_key; } exit_res_key: if (wrap_key_crypt_context != NULL) { PK11_DestroyContext(wrap_key_crypt_context, PR_TRUE); } if (wrap_key != NULL) { PK11_FreeSymKey(wrap_key); } if (slot != NULL) { PK11_FreeSlot(slot); } return (res_key); } static int init_nss_crypto(knet_handle_t knet_h, struct crypto_instance *crypto_instance) { struct nsscrypto_instance *instance = crypto_instance->model_instance; if (!cipher_to_nss[instance->crypto_cipher_type]) { return 0; } instance->nss_sym_key = nssimport_symmetric_key(knet_h, crypto_instance, SYM_KEY_TYPE_CRYPT); if (instance->nss_sym_key == NULL) { errno = ENXIO; /* NSS reported error */ return -1; } return 0; } static int encrypt_nss( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const struct iovec *iov, int iovcnt, unsigned char *buf_out, ssize_t *buf_out_len) { struct nsscrypto_instance *instance = crypto_instance->model_instance; PK11Context* crypt_context = NULL; SECItem crypt_param; SECItem *nss_sec_param = NULL; int tmp_outlen = 0, tmp1_outlen = 0; unsigned int tmp2_outlen = 0; unsigned char *salt = buf_out; unsigned char *data = buf_out + SALT_SIZE; int err = -1; int i; if (PK11_GenerateRandom(salt, SALT_SIZE) != SECSuccess) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Failure to generate a random number (err %d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto out; } crypt_param.type = siBuffer; crypt_param.data = salt; crypt_param.len = SALT_SIZE; nss_sec_param = PK11_ParamFromIV(cipher_to_nss[instance->crypto_cipher_type], &crypt_param); if (nss_sec_param == NULL) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Failure to set up PKCS11 param (err %d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto out; } /* * Create cipher context for encryption */ crypt_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type], CKA_ENCRYPT, instance->nss_sym_key, nss_sec_param); if (!crypt_context) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CreateContext failed (encrypt) crypt_type=%d (err %d): %s", (int)cipher_to_nss[instance->crypto_cipher_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto out; } for (i=0; icrypto_cipher_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto out; } tmp1_outlen = tmp1_outlen + tmp_outlen; } if (PK11_DigestFinal(crypt_context, data + tmp1_outlen, &tmp2_outlen, KNET_DATABUFSIZE_CRYPT - tmp1_outlen) != SECSuccess) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestFinal failed (encrypt) crypt_type=%d (err %d): %s", (int)cipher_to_nss[instance->crypto_cipher_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto out; } *buf_out_len = tmp1_outlen + tmp2_outlen + SALT_SIZE; err = 0; out: if (crypt_context) { PK11_DestroyContext(crypt_context, PR_TRUE); } if (nss_sec_param) { SECITEM_FreeItem(nss_sec_param, PR_TRUE); } return err; } static int decrypt_nss ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len, uint8_t log_level) { struct nsscrypto_instance *instance = crypto_instance->model_instance; PK11Context* decrypt_context = NULL; SECItem decrypt_param; int tmp1_outlen = 0; unsigned int tmp2_outlen = 0; unsigned char *salt = (unsigned char *)buf_in; unsigned char *data = salt + SALT_SIZE; int datalen = buf_in_len - SALT_SIZE; int err = -1; if (datalen <= 0) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Packet is too short"); goto out; } /* Create cipher context for decryption */ decrypt_param.type = siBuffer; decrypt_param.data = salt; decrypt_param.len = SALT_SIZE; decrypt_context = PK11_CreateContextBySymKey(cipher_to_nss[instance->crypto_cipher_type], CKA_DECRYPT, instance->nss_sym_key, &decrypt_param); if (!decrypt_context) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CreateContext (decrypt) failed (err %d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto out; } if (PK11_CipherOp(decrypt_context, buf_out, &tmp1_outlen, KNET_DATABUFSIZE_CRYPT, data, datalen) != SECSuccess) { if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CipherOp (decrypt) failed (err %d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); } else { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CipherOp (decrypt) failed (err %d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); } goto out; } if (PK11_DigestFinal(decrypt_context, buf_out + tmp1_outlen, &tmp2_outlen, KNET_DATABUFSIZE_CRYPT - tmp1_outlen) != SECSuccess) { if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestFinal (decrypt) failed (err %d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); } else { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestFinal (decrypt) failed (err %d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); } goto out; } *buf_out_len = tmp1_outlen + tmp2_outlen; err = 0; out: if (decrypt_context) { PK11_DestroyContext(decrypt_context, PR_TRUE); } return err; } /* * hash/hmac/digest functions */ static int nssstring_to_crypto_hash_type(const char* crypto_hash_type) { if (strcmp(crypto_hash_type, "none") == 0) { return CRYPTO_HASH_TYPE_NONE; } else if (strcmp(crypto_hash_type, "md5") == 0) { return CRYPTO_HASH_TYPE_MD5; } else if (strcmp(crypto_hash_type, "sha1") == 0) { return CRYPTO_HASH_TYPE_SHA1; } else if (strcmp(crypto_hash_type, "sha256") == 0) { return CRYPTO_HASH_TYPE_SHA256; } else if (strcmp(crypto_hash_type, "sha384") == 0) { return CRYPTO_HASH_TYPE_SHA384; } else if (strcmp(crypto_hash_type, "sha512") == 0) { return CRYPTO_HASH_TYPE_SHA512; } return -1; } static int init_nss_hash(knet_handle_t knet_h, struct crypto_instance *crypto_instance) { struct nsscrypto_instance *instance = crypto_instance->model_instance; if (!hash_to_nss[instance->crypto_hash_type]) { return 0; } instance->nss_sym_key_sign = nssimport_symmetric_key(knet_h, crypto_instance, SYM_KEY_TYPE_HASH); if (instance->nss_sym_key_sign == NULL) { errno = ENXIO; /* NSS reported error */ return -1; } return 0; } static int calculate_nss_hash( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf, const size_t buf_len, unsigned char *hash, uint8_t log_level) { struct nsscrypto_instance *instance = crypto_instance->model_instance; PK11Context* hash_context = NULL; SECItem hash_param; unsigned int hash_tmp_outlen = 0; int err = -1; /* Now do the digest */ hash_param.type = siBuffer; hash_param.data = 0; hash_param.len = 0; hash_context = PK11_CreateContextBySymKey(hash_to_nss[instance->crypto_hash_type], CKA_SIGN, instance->nss_sym_key_sign, &hash_param); if (!hash_context) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_CreateContext failed (hash) hash_type=%d (err %d): %s", (int)hash_to_nss[instance->crypto_hash_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto out; } if (PK11_DigestBegin(hash_context) != SECSuccess) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestBegin failed (hash) hash_type=%d (err %d): %s", (int)hash_to_nss[instance->crypto_hash_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); goto out; } if (PK11_DigestOp(hash_context, buf, buf_len) != SECSuccess) { if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestOp failed (hash) hash_type=%d (err %d): %s", (int)hash_to_nss[instance->crypto_hash_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); } else { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestOp failed (hash) hash_type=%d (err %d): %s", (int)hash_to_nss[instance->crypto_hash_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); } goto out; } if (PK11_DigestFinal(hash_context, hash, &hash_tmp_outlen, nsshash_len[instance->crypto_hash_type]) != SECSuccess) { if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestFinale failed (hash) hash_type=%d (err %d): %s", (int)hash_to_nss[instance->crypto_hash_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); } else { log_err(knet_h, KNET_SUB_NSSCRYPTO, "PK11_DigestFinale failed (hash) hash_type=%d (err %d): %s", (int)hash_to_nss[instance->crypto_hash_type], PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); } goto out; } err = 0; out: if (hash_context) { PK11_DestroyContext(hash_context, PR_TRUE); } return err; } /* * global/glue nss functions */ static int init_nss(knet_handle_t knet_h, struct crypto_instance *crypto_instance) { static int at_exit_registered = 0; if (!at_exit_registered) { if (atexit(nss_atexit_handler)) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to register NSS atexit handler"); errno = EAGAIN; return -1; } at_exit_registered = 1; } if (!nss_db_is_init) { if (NSS_NoDB_Init(NULL) != SECSuccess) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "NSS DB initialization failed (err %d): %s", PR_GetError(), PR_ErrorToString(PR_GetError(), PR_LANGUAGE_I_DEFAULT)); errno = EAGAIN; return -1; } nss_db_is_init = 1; } if (init_nss_crypto(knet_h, crypto_instance) < 0) { return -1; } if (init_nss_hash(knet_h, crypto_instance) < 0) { return -1; } return 0; } /* * exported API */ static int nsscrypto_encrypt_and_signv ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const struct iovec *iov_in, int iovcnt_in, unsigned char *buf_out, ssize_t *buf_out_len) { struct nsscrypto_instance *instance = crypto_instance->model_instance; int i; if (cipher_to_nss[instance->crypto_cipher_type]) { if (encrypt_nss(knet_h, crypto_instance, iov_in, iovcnt_in, buf_out, buf_out_len) < 0) { return -1; } } else { *buf_out_len = 0; for (i=0; icrypto_hash_type]) { if (calculate_nss_hash(knet_h, crypto_instance, buf_out, *buf_out_len, buf_out + *buf_out_len, KNET_LOG_ERR) < 0) { return -1; } *buf_out_len = *buf_out_len + nsshash_len[instance->crypto_hash_type]; } return 0; } static int nsscrypto_encrypt_and_sign ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { struct iovec iov_in; memset(&iov_in, 0, sizeof(iov_in)); iov_in.iov_base = (unsigned char *)buf_in; iov_in.iov_len = buf_in_len; return nsscrypto_encrypt_and_signv(knet_h, crypto_instance, &iov_in, 1, buf_out, buf_out_len); } static int nsscrypto_authenticate_and_decrypt ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len, uint8_t log_level) { struct nsscrypto_instance *instance = crypto_instance->model_instance; ssize_t temp_len = buf_in_len; if (hash_to_nss[instance->crypto_hash_type]) { unsigned char tmp_hash[nsshash_len[instance->crypto_hash_type]]; ssize_t temp_buf_len = buf_in_len - nsshash_len[instance->crypto_hash_type]; if ((temp_buf_len <= 0) || (temp_buf_len > KNET_MAX_PACKET_SIZE)) { log_debug(knet_h, KNET_SUB_NSSCRYPTO, "Received incorrect packet size: %zu for hash size: %zu", buf_in_len, nsshash_len[instance->crypto_hash_type]); return -1; } if (calculate_nss_hash(knet_h, crypto_instance, buf_in, temp_buf_len, tmp_hash, log_level) < 0) { return -1; } if (memcmp(tmp_hash, buf_in + temp_buf_len, nsshash_len[instance->crypto_hash_type]) != 0) { if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_NSSCRYPTO, "Digest does not match. Check crypto key and configuration."); } else { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Digest does not match. Check crypto key and configuration."); } return -1; } temp_len = temp_len - nsshash_len[instance->crypto_hash_type]; *buf_out_len = temp_len; } if (cipher_to_nss[instance->crypto_cipher_type]) { if (decrypt_nss(knet_h, crypto_instance, buf_in, temp_len, buf_out, buf_out_len, log_level) < 0) { return -1; } } else { memmove(buf_out, buf_in, temp_len); *buf_out_len = temp_len; } return 0; } static void nsscrypto_fini( knet_handle_t knet_h, struct crypto_instance *crypto_instance) { struct nsscrypto_instance *nsscrypto_instance = crypto_instance->model_instance; if (nsscrypto_instance) { if (nsscrypto_instance->nss_sym_key) { PK11_FreeSymKey(nsscrypto_instance->nss_sym_key); nsscrypto_instance->nss_sym_key = NULL; } if (nsscrypto_instance->nss_sym_key_sign) { PK11_FreeSymKey(nsscrypto_instance->nss_sym_key_sign); nsscrypto_instance->nss_sym_key_sign = NULL; } free(nsscrypto_instance); crypto_instance->model_instance = NULL; } return; } static int nsscrypto_init( knet_handle_t knet_h, struct crypto_instance *crypto_instance, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) { struct nsscrypto_instance *nsscrypto_instance = NULL; int savederrno; log_debug(knet_h, KNET_SUB_NSSCRYPTO, "Initializing nss crypto module [%s/%s]", knet_handle_crypto_cfg->crypto_cipher_type, knet_handle_crypto_cfg->crypto_hash_type); crypto_instance->model_instance = malloc(sizeof(struct nsscrypto_instance)); if (!crypto_instance->model_instance) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "Unable to allocate memory for nss model instance"); errno = ENOMEM; return -1; } nsscrypto_instance = crypto_instance->model_instance; memset(nsscrypto_instance, 0, sizeof(struct nsscrypto_instance)); nsscrypto_instance->crypto_cipher_type = nssstring_to_crypto_cipher_type(knet_handle_crypto_cfg->crypto_cipher_type); if (nsscrypto_instance->crypto_cipher_type < 0) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "unknown crypto cipher type requested"); savederrno = ENXIO; goto out_err; } nsscrypto_instance->crypto_hash_type = nssstring_to_crypto_hash_type(knet_handle_crypto_cfg->crypto_hash_type); if (nsscrypto_instance->crypto_hash_type < 0) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "unknown crypto hash type requested"); savederrno = ENXIO; goto out_err; } if ((nsscrypto_instance->crypto_cipher_type > 0) && (nsscrypto_instance->crypto_hash_type == 0)) { log_err(knet_h, KNET_SUB_NSSCRYPTO, "crypto communication requires hash specified"); savederrno = EINVAL; goto out_err; } nsscrypto_instance->private_key = knet_handle_crypto_cfg->private_key; nsscrypto_instance->private_key_len = knet_handle_crypto_cfg->private_key_len; if (init_nss(knet_h, crypto_instance) < 0) { savederrno = errno; goto out_err; } if (nsscrypto_instance->crypto_hash_type > 0) { crypto_instance->sec_hash_size = nsshash_len[nsscrypto_instance->crypto_hash_type]; } if (nsscrypto_instance->crypto_cipher_type > 0) { int block_size; if (nsscypher_block_len[nsscrypto_instance->crypto_cipher_type]) { block_size = nsscypher_block_len[nsscrypto_instance->crypto_cipher_type]; } else { block_size = PK11_GetBlockSize(nsscrypto_instance->crypto_cipher_type, NULL); if (block_size < 0) { savederrno = ENXIO; goto out_err; } } crypto_instance->sec_salt_size = SALT_SIZE; crypto_instance->sec_block_size = block_size; } return 0; out_err: nsscrypto_fini(knet_h, crypto_instance); errno = savederrno; return -1; } crypto_ops_t crypto_model = { KNET_CRYPTO_MODEL_ABI, nsscrypto_init, nsscrypto_fini, nsscrypto_encrypt_and_sign, nsscrypto_encrypt_and_signv, nsscrypto_authenticate_and_decrypt }; diff --git a/libknet/crypto_openssl.c b/libknet/crypto_openssl.c index 76609f3c..5d42b99c 100644 --- a/libknet/crypto_openssl.c +++ b/libknet/crypto_openssl.c @@ -1,729 +1,729 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include #include #include #include #if (OPENSSL_VERSION_NUMBER < 0x30000000L) #include #endif #include #include #include "logging.h" #include "crypto_model.h" /* * 1.0.2 requires at least 120 bytes * 1.1.0 requires at least 256 bytes */ #define SSLERR_BUF_SIZE 512 /* * crypto definitions and conversion tables */ #define SALT_SIZE 16 /* * required by OSSL_PARAM_construct_* * making them global and cost, saves 2 strncpy and some memory on each config */ #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) static const char *hash = "digest"; #endif struct opensslcrypto_instance { void *private_key; int private_key_len; const EVP_CIPHER *crypto_cipher_type; const EVP_MD *crypto_hash_type; #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) EVP_MAC *crypto_hash_mac; OSSL_PARAM params[3]; char hash_type[16]; /* Need to store a copy from knet_handle_crypto_cfg for OSSL_PARAM_construct_* */ #endif }; static int openssl_is_init = 0; /* * crypt/decrypt functions openssl1.0 */ #if (OPENSSL_VERSION_NUMBER < 0x10100000L) static int encrypt_openssl( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const struct iovec *iov, int iovcnt, unsigned char *buf_out, ssize_t *buf_out_len) { struct opensslcrypto_instance *instance = crypto_instance->model_instance; EVP_CIPHER_CTX ctx; int tmplen = 0, offset = 0; unsigned char *salt = buf_out; unsigned char *data = buf_out + SALT_SIZE; int err = 0; int i; char sslerr[SSLERR_BUF_SIZE]; EVP_CIPHER_CTX_init(&ctx); if (!RAND_bytes(salt, SALT_SIZE)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to get random salt data: %s", sslerr); err = -1; goto out; } /* * add warning re keylength */ EVP_EncryptInit_ex(&ctx, instance->crypto_cipher_type, NULL, instance->private_key, salt); for (i=0; imodel_instance; EVP_CIPHER_CTX ctx; int tmplen1 = 0, tmplen2 = 0; unsigned char *salt = (unsigned char *)buf_in; unsigned char *data = salt + SALT_SIZE; int datalen = buf_in_len - SALT_SIZE; int err = 0; char sslerr[SSLERR_BUF_SIZE]; EVP_CIPHER_CTX_init(&ctx); /* * add warning re keylength */ EVP_DecryptInit_ex(&ctx, instance->crypto_cipher_type, NULL, instance->private_key, salt); if (!EVP_DecryptUpdate(&ctx, buf_out, &tmplen1, data, datalen)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to decrypt: %s", sslerr); } else { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to decrypt: %s", sslerr); } err = -1; goto out; } if (!EVP_DecryptFinal_ex(&ctx, buf_out + tmplen1, &tmplen2)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to finalize decrypt: %s", sslerr); } else { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to finalize decrypt: %s", sslerr); } err = -1; goto out; } *buf_out_len = tmplen1 + tmplen2; out: EVP_CIPHER_CTX_cleanup(&ctx); return err; } #else /* (OPENSSL_VERSION_NUMBER < 0x10100000L) */ static int encrypt_openssl( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const struct iovec *iov, int iovcnt, unsigned char *buf_out, ssize_t *buf_out_len) { struct opensslcrypto_instance *instance = crypto_instance->model_instance; EVP_CIPHER_CTX *ctx; int tmplen = 0, offset = 0; unsigned char *salt = buf_out; unsigned char *data = buf_out + SALT_SIZE; int err = 0; int i; char sslerr[SSLERR_BUF_SIZE]; ctx = EVP_CIPHER_CTX_new(); if (!RAND_bytes(salt, SALT_SIZE)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to get random salt data: %s", sslerr); err = -1; goto out; } /* * add warning re keylength */ EVP_EncryptInit_ex(ctx, instance->crypto_cipher_type, NULL, instance->private_key, salt); for (i=0; imodel_instance; EVP_CIPHER_CTX *ctx = NULL; int tmplen1 = 0, tmplen2 = 0; unsigned char *salt = (unsigned char *)buf_in; unsigned char *data = salt + SALT_SIZE; int datalen = buf_in_len - SALT_SIZE; int err = 0; char sslerr[SSLERR_BUF_SIZE]; if (datalen <= 0) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Packet is too short"); err = -1; goto out; } ctx = EVP_CIPHER_CTX_new(); /* * add warning re keylength */ EVP_DecryptInit_ex(ctx, instance->crypto_cipher_type, NULL, instance->private_key, salt); if (!EVP_DecryptUpdate(ctx, buf_out, &tmplen1, data, datalen)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to decrypt: %s", sslerr); } else { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to decrypt: %s", sslerr); } err = -1; goto out; } if (!EVP_DecryptFinal_ex(ctx, buf_out + tmplen1, &tmplen2)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to finalize decrypt: %s", sslerr); } else { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to finalize decrypt: %s", sslerr); } err = -1; goto out; } *buf_out_len = tmplen1 + tmplen2; out: if (ctx) { EVP_CIPHER_CTX_free(ctx); } return err; } #endif /* * hash/hmac/digest functions */ #if (OPENSSL_VERSION_NUMBER < 0x30000000L) static int calculate_openssl_hash( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf, const size_t buf_len, unsigned char *hash, uint8_t log_level) { struct opensslcrypto_instance *instance = crypto_instance->model_instance; unsigned int hash_len = 0; unsigned char *hash_out = NULL; char sslerr[SSLERR_BUF_SIZE]; hash_out = HMAC(instance->crypto_hash_type, instance->private_key, instance->private_key_len, buf, buf_len, hash, &hash_len); if ((!hash_out) || (hash_len != crypto_instance->sec_hash_size)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to calculate hash: %s", sslerr); } else { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to calculate hash: %s", sslerr); } return -1; } return 0; } #else static int calculate_openssl_hash( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf, const size_t buf_len, unsigned char *hash, uint8_t log_level) { struct opensslcrypto_instance *instance = crypto_instance->model_instance; EVP_MAC_CTX *ctx = NULL; char sslerr[SSLERR_BUF_SIZE]; int err = 0; size_t outlen = 0; ctx = EVP_MAC_CTX_new(instance->crypto_hash_mac); if (!ctx) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to allocate openssl context: %s", sslerr); err = -1; goto out_err; } if (!EVP_MAC_init(ctx, instance->private_key, instance->private_key_len, instance->params)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to set openssl context parameters: %s", sslerr); err = -1; goto out_err; } if (!EVP_MAC_update(ctx, buf, buf_len)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to update hash: %s", sslerr); err = -1; goto out_err; } if (!EVP_MAC_final(ctx, hash, &outlen, crypto_instance->sec_hash_size)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to finalize hash: %s", sslerr); err = -1; goto out_err; } out_err: if (ctx) { EVP_MAC_CTX_free(ctx); } return err; } #endif /* * exported API */ static int opensslcrypto_encrypt_and_signv ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const struct iovec *iov_in, int iovcnt_in, unsigned char *buf_out, ssize_t *buf_out_len) { struct opensslcrypto_instance *instance = crypto_instance->model_instance; int i; if (instance->crypto_cipher_type) { if (encrypt_openssl(knet_h, crypto_instance, iov_in, iovcnt_in, buf_out, buf_out_len) < 0) { return -1; } } else { *buf_out_len = 0; for (i=0; icrypto_hash_type) { if (calculate_openssl_hash(knet_h, crypto_instance, buf_out, *buf_out_len, buf_out + *buf_out_len, KNET_LOG_ERR) < 0) { return -1; } *buf_out_len = *buf_out_len + crypto_instance->sec_hash_size; } return 0; } static int opensslcrypto_encrypt_and_sign ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { struct iovec iov_in; memset(&iov_in, 0, sizeof(iov_in)); iov_in.iov_base = (unsigned char *)buf_in; iov_in.iov_len = buf_in_len; return opensslcrypto_encrypt_and_signv(knet_h, crypto_instance, &iov_in, 1, buf_out, buf_out_len); } static int opensslcrypto_authenticate_and_decrypt ( knet_handle_t knet_h, struct crypto_instance *crypto_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len, uint8_t log_level) { struct opensslcrypto_instance *instance = crypto_instance->model_instance; ssize_t temp_len = buf_in_len; if (instance->crypto_hash_type) { unsigned char tmp_hash[crypto_instance->sec_hash_size]; ssize_t temp_buf_len = buf_in_len - crypto_instance->sec_hash_size; memset(tmp_hash, 0, sizeof(tmp_hash)); if ((temp_buf_len <= 0) || (temp_buf_len > KNET_MAX_PACKET_SIZE)) { log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Received incorrect packet size: %zu for hash size: %zu", buf_in_len, crypto_instance->sec_hash_size); return -1; } if (calculate_openssl_hash(knet_h, crypto_instance, buf_in, temp_buf_len, tmp_hash, log_level) < 0) { return -1; } if (memcmp(tmp_hash, buf_in + temp_buf_len, crypto_instance->sec_hash_size) != 0) { if (log_level == KNET_LOG_DEBUG) { log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Digest does not match. Check crypto key and configuration."); } else { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Digest does not match. Check crypto key and configuration."); } return -1; } temp_len = temp_len - crypto_instance->sec_hash_size; *buf_out_len = temp_len; } if (instance->crypto_cipher_type) { if (decrypt_openssl(knet_h, crypto_instance, buf_in, temp_len, buf_out, buf_out_len, log_level) < 0) { return -1; } } else { memmove(buf_out, buf_in, temp_len); *buf_out_len = temp_len; } return 0; } #if (OPENSSL_VERSION_NUMBER < 0x10100000L) static pthread_mutex_t *openssl_internal_lock; static void openssl_internal_locking_callback(int mode, int type, char *file, int line) { if (mode & CRYPTO_LOCK) { (void)pthread_mutex_lock(&(openssl_internal_lock[type])); } else { pthread_mutex_unlock(&(openssl_internal_lock[type])); } } static pthread_t openssl_internal_thread_id(void) { return pthread_self(); } static void openssl_internal_lock_cleanup(void) { int i; CRYPTO_set_locking_callback(NULL); CRYPTO_set_id_callback(NULL); for (i = 0; i < CRYPTO_num_locks(); i++) { pthread_mutex_destroy(&(openssl_internal_lock[i])); } if (openssl_internal_lock) { free(openssl_internal_lock); } return; } static void openssl_atexit_handler(void) { openssl_internal_lock_cleanup(); } static int openssl_internal_lock_setup(void) { int savederrno = 0, err = 0; int i; openssl_internal_lock = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t)); if (!openssl_internal_lock) { savederrno = errno; err = -1; goto out; } for (i = 0; i < CRYPTO_num_locks(); i++) { savederrno = pthread_mutex_init(&(openssl_internal_lock[i]), NULL); if (savederrno) { err = -1; goto out; } } CRYPTO_set_id_callback((void *)openssl_internal_thread_id); CRYPTO_set_locking_callback((void *)&openssl_internal_locking_callback); if (atexit(openssl_atexit_handler)) { err = -1; } out: if (err) { openssl_internal_lock_cleanup(); } errno = savederrno; return err; } #endif static void opensslcrypto_fini( knet_handle_t knet_h, struct crypto_instance *crypto_instance) { struct opensslcrypto_instance *opensslcrypto_instance = crypto_instance->model_instance; if (opensslcrypto_instance) { if (opensslcrypto_instance->private_key) { free(opensslcrypto_instance->private_key); opensslcrypto_instance->private_key = NULL; } #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) if (opensslcrypto_instance->crypto_hash_mac) { EVP_MAC_free(opensslcrypto_instance->crypto_hash_mac); } #endif free(opensslcrypto_instance); crypto_instance->model_instance = NULL; } #if (OPENSSL_VERSION_NUMBER < 0x10100000L) ERR_free_strings(); #endif return; } static int opensslcrypto_init( knet_handle_t knet_h, struct crypto_instance *crypto_instance, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) { struct opensslcrypto_instance *opensslcrypto_instance = NULL; int savederrno; #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) char sslerr[SSLERR_BUF_SIZE]; size_t params_n = 0; #endif log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Initializing openssl crypto module [%s/%s]", knet_handle_crypto_cfg->crypto_cipher_type, knet_handle_crypto_cfg->crypto_hash_type); if (!openssl_is_init) { #if (OPENSSL_VERSION_NUMBER < 0x10100000L) ERR_load_crypto_strings(); OPENSSL_add_all_algorithms_noconf(); if (openssl_internal_lock_setup() < 0) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to init openssl"); errno = EAGAIN; return -1; } #else if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS \ | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL)) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to init openssl"); errno = EAGAIN; return -1; } #endif openssl_is_init = 1; } crypto_instance->model_instance = malloc(sizeof(struct opensslcrypto_instance)); if (!crypto_instance->model_instance) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to allocate memory for openssl model instance"); errno = ENOMEM; return -1; } opensslcrypto_instance = crypto_instance->model_instance; memset(opensslcrypto_instance, 0, sizeof(struct opensslcrypto_instance)); opensslcrypto_instance->private_key = malloc(knet_handle_crypto_cfg->private_key_len); if (!opensslcrypto_instance->private_key) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to allocate memory for openssl private key"); savederrno = ENOMEM; goto out_err; } memmove(opensslcrypto_instance->private_key, knet_handle_crypto_cfg->private_key, knet_handle_crypto_cfg->private_key_len); opensslcrypto_instance->private_key_len = knet_handle_crypto_cfg->private_key_len; if (strcmp(knet_handle_crypto_cfg->crypto_cipher_type, "none") == 0) { opensslcrypto_instance->crypto_cipher_type = NULL; } else { opensslcrypto_instance->crypto_cipher_type = EVP_get_cipherbyname(knet_handle_crypto_cfg->crypto_cipher_type); if (!opensslcrypto_instance->crypto_cipher_type) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "unknown crypto cipher type requested"); savederrno = ENXIO; goto out_err; } } if (strcmp(knet_handle_crypto_cfg->crypto_hash_type, "none") == 0) { opensslcrypto_instance->crypto_hash_type = NULL; } else { opensslcrypto_instance->crypto_hash_type = EVP_get_digestbyname(knet_handle_crypto_cfg->crypto_hash_type); if (!opensslcrypto_instance->crypto_hash_type) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "unknown crypto hash type requested"); savederrno = ENXIO; goto out_err; } #if (OPENSSL_VERSION_NUMBER >= 0x30000000L) opensslcrypto_instance->crypto_hash_mac = EVP_MAC_fetch(NULL, "HMAC", NULL); if (!opensslcrypto_instance->crypto_hash_mac) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "unable to fetch HMAC: %s", sslerr); savederrno = ENXIO; goto out_err; } /* * OSSL_PARAM_construct_* store pointers to the data, it´s important that the referenced data are per-instance */ memmove(opensslcrypto_instance->hash_type, knet_handle_crypto_cfg->crypto_hash_type, sizeof(opensslcrypto_instance->hash_type)); opensslcrypto_instance->params[params_n++] = OSSL_PARAM_construct_utf8_string(hash, opensslcrypto_instance->hash_type, 0); opensslcrypto_instance->params[params_n] = OSSL_PARAM_construct_end(); #endif } if ((opensslcrypto_instance->crypto_cipher_type) && (!opensslcrypto_instance->crypto_hash_type)) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "crypto communication requires hash specified"); savederrno = EINVAL; goto out_err; } if (opensslcrypto_instance->crypto_hash_type) { crypto_instance->sec_hash_size = EVP_MD_size(opensslcrypto_instance->crypto_hash_type); } if (opensslcrypto_instance->crypto_cipher_type) { size_t block_size; block_size = EVP_CIPHER_block_size(opensslcrypto_instance->crypto_cipher_type); crypto_instance->sec_salt_size = SALT_SIZE; crypto_instance->sec_block_size = block_size; } return 0; out_err: opensslcrypto_fini(knet_h, crypto_instance); errno = savederrno; return -1; } crypto_ops_t crypto_model = { KNET_CRYPTO_MODEL_ABI, opensslcrypto_init, opensslcrypto_fini, opensslcrypto_encrypt_and_sign, opensslcrypto_encrypt_and_signv, opensslcrypto_authenticate_and_decrypt }; diff --git a/libknet/handle.c b/libknet/handle.c index 9d23a24d..905338e0 100644 --- a/libknet/handle.c +++ b/libknet/handle.c @@ -1,795 +1,795 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "internals.h" #include "crypto.h" #include "links.h" #include "compress.h" #include "compat.h" #include "common.h" #include "threads_common.h" #include "threads_heartbeat.h" #include "threads_pmtud.h" #include "threads_dsthandler.h" #include "threads_rx.h" #include "threads_tx.h" #include "transports.h" #include "transport_common.h" #include "logging.h" static int _init_locks(knet_handle_t knet_h) { int savederrno = 0; savederrno = pthread_rwlock_init(&knet_h->global_rwlock, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize list rwlock: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->handle_stats_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize handle stats mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->threads_status_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize threads status mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->pmtud_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize pmtud mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->kmtu_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize kernel_mtu mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_cond_init(&knet_h->pmtud_cond, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize pmtud conditional mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->hb_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize hb_thread mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->tx_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize tx_thread mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->backoff_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize pong timeout backoff mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->tx_seq_num_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize tx_seq_num_mutex mutex: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_mutex_init(&knet_h->onwire_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize onwire_mutex mutex: %s", strerror(savederrno)); goto exit_fail; } return 0; exit_fail: errno = savederrno; return -1; } static void _destroy_locks(knet_handle_t knet_h) { pthread_rwlock_destroy(&knet_h->global_rwlock); pthread_mutex_destroy(&knet_h->pmtud_mutex); pthread_mutex_destroy(&knet_h->kmtu_mutex); pthread_cond_destroy(&knet_h->pmtud_cond); pthread_mutex_destroy(&knet_h->hb_mutex); pthread_mutex_destroy(&knet_h->tx_mutex); pthread_mutex_destroy(&knet_h->backoff_mutex); pthread_mutex_destroy(&knet_h->tx_seq_num_mutex); pthread_mutex_destroy(&knet_h->threads_status_mutex); pthread_mutex_destroy(&knet_h->handle_stats_mutex); pthread_mutex_destroy(&knet_h->onwire_mutex); } static int _init_socks(knet_handle_t knet_h) { int savederrno = 0; if (_init_socketpair(knet_h, knet_h->dstsockfd)) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize internal dstsockpair: %s", strerror(savederrno)); goto exit_fail; } return 0; exit_fail: errno = savederrno; return -1; } static void _close_socks(knet_handle_t knet_h) { _close_socketpair(knet_h, knet_h->dstsockfd); } static int _init_buffers(knet_handle_t knet_h) { int savederrno = 0; int i; size_t bufsize; for (i = 0; i < PCKT_FRAG_MAX; i++) { bufsize = ceil((float)KNET_MAX_PACKET_SIZE / (i + 1)) + KNET_HEADER_ALL_SIZE; knet_h->send_to_links_buf[i] = malloc(bufsize); if (!knet_h->send_to_links_buf[i]) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory datafd to link buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->send_to_links_buf[i], 0, bufsize); } for (i = 0; i < PCKT_RX_BUFS; i++) { knet_h->recv_from_links_buf[i] = malloc(KNET_DATABUFSIZE); if (!knet_h->recv_from_links_buf[i]) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for link to datafd buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->recv_from_links_buf[i], 0, KNET_DATABUFSIZE); } knet_h->recv_from_sock_buf = malloc(KNET_DATABUFSIZE); if (!knet_h->recv_from_sock_buf) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for app to datafd buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->recv_from_sock_buf, 0, KNET_DATABUFSIZE); knet_h->pingbuf = malloc(KNET_HEADER_ALL_SIZE); if (!knet_h->pingbuf) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for hearbeat buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->pingbuf, 0, KNET_HEADER_ALL_SIZE); knet_h->pmtudbuf = malloc(KNET_PMTUD_SIZE_V6 + KNET_HEADER_ALL_SIZE); if (!knet_h->pmtudbuf) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for pmtud buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->pmtudbuf, 0, KNET_PMTUD_SIZE_V6 + KNET_HEADER_ALL_SIZE); for (i = 0; i < PCKT_FRAG_MAX; i++) { bufsize = ceil((float)KNET_MAX_PACKET_SIZE / (i + 1)) + KNET_HEADER_ALL_SIZE + KNET_DATABUFSIZE_CRYPT_PAD; knet_h->send_to_links_buf_crypt[i] = malloc(bufsize); if (!knet_h->send_to_links_buf_crypt[i]) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for crypto datafd to link buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->send_to_links_buf_crypt[i], 0, bufsize); } knet_h->recv_from_links_buf_decrypt = malloc(KNET_DATABUFSIZE_CRYPT); if (!knet_h->recv_from_links_buf_decrypt) { savederrno = errno; log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto link to datafd buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->recv_from_links_buf_decrypt, 0, KNET_DATABUFSIZE_CRYPT); knet_h->recv_from_links_buf_crypt = malloc(KNET_DATABUFSIZE_CRYPT); if (!knet_h->recv_from_links_buf_crypt) { savederrno = errno; log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto link to datafd buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->recv_from_links_buf_crypt, 0, KNET_DATABUFSIZE_CRYPT); knet_h->pingbuf_crypt = malloc(KNET_DATABUFSIZE_CRYPT); if (!knet_h->pingbuf_crypt) { savederrno = errno; log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto hearbeat buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->pingbuf_crypt, 0, KNET_DATABUFSIZE_CRYPT); knet_h->pmtudbuf_crypt = malloc(KNET_DATABUFSIZE_CRYPT); if (!knet_h->pmtudbuf_crypt) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for crypto pmtud buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->pmtudbuf_crypt, 0, KNET_DATABUFSIZE_CRYPT); knet_h->recv_from_links_buf_decompress = malloc(KNET_DATABUFSIZE_COMPRESS); if (!knet_h->recv_from_links_buf_decompress) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for decompress buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->recv_from_links_buf_decompress, 0, KNET_DATABUFSIZE_COMPRESS); knet_h->send_to_links_buf_compress = malloc(KNET_DATABUFSIZE_COMPRESS); if (!knet_h->send_to_links_buf_compress) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to allocate memory for compress buffer: %s", strerror(savederrno)); goto exit_fail; } memset(knet_h->send_to_links_buf_compress, 0, KNET_DATABUFSIZE_COMPRESS); memset(knet_h->knet_transport_fd_tracker, 0, sizeof(knet_h->knet_transport_fd_tracker)); for (i = 0; i < KNET_MAX_FDS; i++) { knet_h->knet_transport_fd_tracker[i].transport = KNET_MAX_TRANSPORTS; } return 0; exit_fail: errno = savederrno; return -1; } static void _destroy_buffers(knet_handle_t knet_h) { int i; for (i = 0; i < PCKT_FRAG_MAX; i++) { free(knet_h->send_to_links_buf[i]); free(knet_h->send_to_links_buf_crypt[i]); } for (i = 0; i < PCKT_RX_BUFS; i++) { free(knet_h->recv_from_links_buf[i]); } free(knet_h->recv_from_links_buf_decompress); free(knet_h->send_to_links_buf_compress); free(knet_h->recv_from_sock_buf); free(knet_h->recv_from_links_buf_decrypt); free(knet_h->recv_from_links_buf_crypt); free(knet_h->pingbuf); free(knet_h->pingbuf_crypt); free(knet_h->pmtudbuf); free(knet_h->pmtudbuf_crypt); } static int _init_epolls(knet_handle_t knet_h) { struct epoll_event ev; int savederrno = 0; /* * even if the kernel does dynamic allocation with epoll_ctl * we need to reserve one extra for host to host communication */ knet_h->send_to_links_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS + 1); if (knet_h->send_to_links_epollfd < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to create epoll datafd to link fd: %s", strerror(savederrno)); goto exit_fail; } knet_h->recv_from_links_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS); if (knet_h->recv_from_links_epollfd < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to create epoll link to datafd fd: %s", strerror(savederrno)); goto exit_fail; } knet_h->dst_link_handler_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS); if (knet_h->dst_link_handler_epollfd < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to create epoll dst cache fd: %s", strerror(savederrno)); goto exit_fail; } if (_fdset_cloexec(knet_h->send_to_links_epollfd)) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to set CLOEXEC on datafd to link epoll fd: %s", strerror(savederrno)); goto exit_fail; } if (_fdset_cloexec(knet_h->recv_from_links_epollfd)) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to set CLOEXEC on link to datafd epoll fd: %s", strerror(savederrno)); goto exit_fail; } if (_fdset_cloexec(knet_h->dst_link_handler_epollfd)) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to set CLOEXEC on dst cache epoll fd: %s", strerror(savederrno)); goto exit_fail; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = knet_h->dstsockfd[0]; if (epoll_ctl(knet_h->dst_link_handler_epollfd, EPOLL_CTL_ADD, knet_h->dstsockfd[0], &ev)) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to add dstsockfd[0] to epoll pool: %s", strerror(savederrno)); goto exit_fail; } return 0; exit_fail: errno = savederrno; return -1; } static void _close_epolls(knet_handle_t knet_h) { struct epoll_event ev; int i; memset(&ev, 0, sizeof(struct epoll_event)); for (i = 0; i < KNET_DATAFD_MAX; i++) { if (knet_h->sockfd[i].in_use) { epoll_ctl(knet_h->send_to_links_epollfd, EPOLL_CTL_DEL, knet_h->sockfd[i].sockfd[knet_h->sockfd[i].is_created], &ev); if (knet_h->sockfd[i].sockfd[knet_h->sockfd[i].is_created]) { _close_socketpair(knet_h, knet_h->sockfd[i].sockfd); } } } epoll_ctl(knet_h->dst_link_handler_epollfd, EPOLL_CTL_DEL, knet_h->dstsockfd[0], &ev); close(knet_h->send_to_links_epollfd); close(knet_h->recv_from_links_epollfd); close(knet_h->dst_link_handler_epollfd); } static int _start_threads(knet_handle_t knet_h) { int savederrno = 0; pthread_attr_t attr; set_thread_status(knet_h, KNET_THREAD_PMTUD, KNET_THREAD_REGISTERED); savederrno = pthread_attr_init(&attr); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to init pthread attributes: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_attr_setstacksize(&attr, KNET_THREAD_STACK_SIZE); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to set stack size attribute: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_create(&knet_h->pmtud_link_handler_thread, &attr, _handle_pmtud_link_thread, (void *) knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to start pmtud link thread: %s", strerror(savederrno)); goto exit_fail; } set_thread_status(knet_h, KNET_THREAD_DST_LINK, KNET_THREAD_REGISTERED); savederrno = pthread_create(&knet_h->dst_link_handler_thread, &attr, _handle_dst_link_handler_thread, (void *) knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to start dst cache thread: %s", strerror(savederrno)); goto exit_fail; } set_thread_status(knet_h, KNET_THREAD_TX, KNET_THREAD_REGISTERED); savederrno = pthread_create(&knet_h->send_to_links_thread, &attr, _handle_send_to_links_thread, (void *) knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to start datafd to link thread: %s", strerror(savederrno)); goto exit_fail; } set_thread_status(knet_h, KNET_THREAD_RX, KNET_THREAD_REGISTERED); savederrno = pthread_create(&knet_h->recv_from_links_thread, &attr, _handle_recv_from_links_thread, (void *) knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to start link to datafd thread: %s", strerror(savederrno)); goto exit_fail; } set_thread_status(knet_h, KNET_THREAD_HB, KNET_THREAD_REGISTERED); savederrno = pthread_create(&knet_h->heartbt_thread, &attr, _handle_heartbt_thread, (void *) knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to start heartbeat thread: %s", strerror(savederrno)); goto exit_fail; } savederrno = pthread_attr_destroy(&attr); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to destroy pthread attributes: %s", strerror(savederrno)); /* * Do not return error code. Error is not critical. */ } return 0; exit_fail: errno = savederrno; return -1; } static void _stop_threads(knet_handle_t knet_h) { void *retval; wait_all_threads_status(knet_h, KNET_THREAD_STOPPED); if (knet_h->heartbt_thread) { pthread_cancel(knet_h->heartbt_thread); pthread_join(knet_h->heartbt_thread, &retval); } if (knet_h->send_to_links_thread) { pthread_cancel(knet_h->send_to_links_thread); pthread_join(knet_h->send_to_links_thread, &retval); } if (knet_h->recv_from_links_thread) { pthread_cancel(knet_h->recv_from_links_thread); pthread_join(knet_h->recv_from_links_thread, &retval); } if (knet_h->dst_link_handler_thread) { pthread_cancel(knet_h->dst_link_handler_thread); pthread_join(knet_h->dst_link_handler_thread, &retval); } if (knet_h->pmtud_link_handler_thread) { pthread_cancel(knet_h->pmtud_link_handler_thread); pthread_join(knet_h->pmtud_link_handler_thread, &retval); } } knet_handle_t knet_handle_new(knet_node_id_t host_id, int log_fd, uint8_t default_log_level, uint64_t flags) { knet_handle_t knet_h; int savederrno = 0; struct rlimit cur; if (getrlimit(RLIMIT_NOFILE, &cur) < 0) { return NULL; } if ((log_fd < 0) || ((unsigned int)log_fd >= cur.rlim_max)) { errno = EINVAL; return NULL; } /* * validate incoming request */ if ((log_fd) && (default_log_level > KNET_LOG_DEBUG)) { errno = EINVAL; return NULL; } if (flags > KNET_HANDLE_FLAG_PRIVILEGED * 2 - 1) { errno = EINVAL; return NULL; } /* * allocate handle */ knet_h = malloc(sizeof(struct knet_handle)); if (!knet_h) { errno = ENOMEM; return NULL; } memset(knet_h, 0, sizeof(struct knet_handle)); /* * setting up some handle data so that we can use logging * also when initializing the library global locks * and trackers */ knet_h->flags = flags; /* * copy config in place */ knet_h->host_id = host_id; knet_h->logfd = log_fd; if (knet_h->logfd > 0) { memset(&knet_h->log_levels, default_log_level, KNET_MAX_SUBSYSTEMS); } /* * set internal threads time resolutions */ knet_h->threads_timer_res = KNET_THREADS_TIMER_RES; /* * set pmtud default timers */ knet_h->pmtud_interval = KNET_PMTUD_DEFAULT_INTERVAL; /* * set transports reconnect default timers */ knet_h->reconnect_int = KNET_TRANSPORT_DEFAULT_RECONNECT_INTERVAL; /* * Set the default path for plugins */ knet_h->plugin_path = PLUGINPATH; /* * Set 'min' stats to the maximum value so the * first value we get is always less */ knet_h->stats.tx_compress_time_min = UINT64_MAX; knet_h->stats.rx_compress_time_min = UINT64_MAX; knet_h->stats.tx_crypt_time_min = UINT64_MAX; knet_h->stats.rx_crypt_time_min = UINT64_MAX; /* * set onwire version. See also comments in internals.h * on why we don´t use constants directly across the code. */ knet_h->onwire_ver = KNET_HEADER_ONWIRE_MIN_VER; knet_h->onwire_min_ver = KNET_HEADER_ONWIRE_MIN_VER; knet_h->onwire_max_ver = KNET_HEADER_ONWIRE_MAX_VER; knet_h->onwire_ver_remap = 0; log_info(knet_h, KNET_SUB_HANDLE, "Default onwire version: %u", knet_h->onwire_ver); /* * set default buffers */ knet_h->defrag_bufs_min = KNET_MIN_DEFRAG_BUFS_DEFAULT; knet_h->defrag_bufs_max = KNET_MAX_DEFRAG_BUFS_DEFAULT; knet_h->defrag_bufs_shrink_threshold = KNET_SHRINK_THRESHOLD_DEFAULT; knet_h->defrag_bufs_usage_samples = KNET_USAGE_SAMPLES_DEFAULT; knet_h->defrag_bufs_usage_samples_timespan = KNET_USAGE_SAMPLES_TIMESPAN_DEFAULT; knet_h->defrag_bufs_reclaim_policy = RECLAIM_POLICY_ABSOLUTE; /* * init global shared bits */ savederrno = pthread_mutex_lock(&handle_config_mutex); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get handle mutex lock: %s", strerror(savederrno)); free(knet_h); knet_h = NULL; errno = savederrno; return NULL; } if (!handle_list_init) { qb_list_init(&handle_list.head); handle_list_init = 1; } qb_list_add(&knet_h->list, &handle_list.head); /* * init global shlib tracker */ if (_init_shlib_tracker(knet_h) < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to init handle tracker: %s", strerror(savederrno)); errno = savederrno; pthread_mutex_unlock(&handle_config_mutex); goto exit_fail; } pthread_mutex_unlock(&handle_config_mutex); /* * init main locking structures */ if (_init_locks(knet_h)) { savederrno = errno; goto exit_fail; } /* * init sockets */ if (_init_socks(knet_h)) { savederrno = errno; goto exit_fail; } /* * allocate packet buffers */ if (_init_buffers(knet_h)) { savederrno = errno; goto exit_fail; } if (compress_init(knet_h)) { savederrno = errno; goto exit_fail; } /* * create epoll fds */ if (_init_epolls(knet_h)) { savederrno = errno; goto exit_fail; } /* * start transports */ if (start_all_transports(knet_h)) { savederrno = errno; goto exit_fail; } /* * start internal threads */ if (_start_threads(knet_h)) { savederrno = errno; goto exit_fail; } wait_all_threads_status(knet_h, KNET_THREAD_STARTED); errno = 0; return knet_h; exit_fail: knet_handle_free(knet_h); errno = savederrno; return NULL; } int knet_handle_free(knet_handle_t knet_h) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (knet_h->host_head != NULL) { savederrno = EBUSY; log_err(knet_h, KNET_SUB_HANDLE, "Unable to free handle: host(s) or listener(s) are still active: %s", strerror(savederrno)); pthread_rwlock_unlock(&knet_h->global_rwlock); errno = savederrno; return -1; } knet_h->fini_in_progress = 1; pthread_rwlock_unlock(&knet_h->global_rwlock); _stop_threads(knet_h); stop_all_transports(knet_h); _close_epolls(knet_h); _destroy_buffers(knet_h); _close_socks(knet_h); crypto_fini(knet_h, KNET_MAX_CRYPTO_INSTANCES + 1); /* values above MAX_CRYPTO will release all crypto resources */ compress_fini(knet_h, 1); _destroy_locks(knet_h); (void)pthread_mutex_lock(&handle_config_mutex); qb_list_del(&knet_h->list); _fini_shlib_tracker(); pthread_mutex_unlock(&handle_config_mutex); free(knet_h); knet_h = NULL; errno = 0; return 0; } diff --git a/libknet/handle_api.c b/libknet/handle_api.c index f664e3f2..9ac5329d 100644 --- a/libknet/handle_api.c +++ b/libknet/handle_api.c @@ -1,718 +1,718 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "internals.h" #include "crypto.h" #include "host.h" #include "links.h" #include "common.h" #include "transport_common.h" #include "logging.h" int knet_handle_enable_sock_notify(knet_handle_t knet_h, void *sock_notify_fn_private_data, void (*sock_notify_fn) ( void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno)) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!sock_notify_fn) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->sock_notify_fn_private_data = sock_notify_fn_private_data; knet_h->sock_notify_fn = sock_notify_fn; log_debug(knet_h, KNET_SUB_HANDLE, "sock_notify_fn enabled"); pthread_rwlock_unlock(&knet_h->global_rwlock); return 0; } int knet_handle_add_datafd(knet_handle_t knet_h, int *datafd, int8_t *channel) { int err = 0, savederrno = 0; int i; struct epoll_event ev; if (!_is_valid_handle(knet_h)) { return -1; } if (datafd == NULL) { errno = EINVAL; return -1; } if (channel == NULL) { errno = EINVAL; return -1; } if (*channel >= KNET_DATAFD_MAX) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->sock_notify_fn) { log_err(knet_h, KNET_SUB_HANDLE, "Adding datafd requires sock notify callback enabled!"); savederrno = EINVAL; err = -1; goto out_unlock; } if (*datafd > 0) { for (i = 0; i < KNET_DATAFD_MAX; i++) { if ((knet_h->sockfd[i].in_use) && (knet_h->sockfd[i].sockfd[0] == *datafd)) { log_err(knet_h, KNET_SUB_HANDLE, "requested datafd: %d already exist in index: %d", *datafd, i); savederrno = EEXIST; err = -1; goto out_unlock; } } } /* * auto allocate a channel */ if (*channel < 0) { for (i = 0; i < KNET_DATAFD_MAX; i++) { if (!knet_h->sockfd[i].in_use) { *channel = i; break; } } if (*channel < 0) { savederrno = EBUSY; err = -1; goto out_unlock; } } else { if (knet_h->sockfd[*channel].in_use) { savederrno = EBUSY; err = -1; goto out_unlock; } } knet_h->sockfd[*channel].is_created = 0; knet_h->sockfd[*channel].is_socket = 0; knet_h->sockfd[*channel].has_error = 0; if (*datafd > 0) { int sockopt; socklen_t sockoptlen = sizeof(sockopt); if (_fdset_cloexec(*datafd)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_HANDLE, "Unable to set CLOEXEC on datafd: %s", strerror(savederrno)); goto out_unlock; } if (_fdset_nonblock(*datafd)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_HANDLE, "Unable to set NONBLOCK on datafd: %s", strerror(savederrno)); goto out_unlock; } knet_h->sockfd[*channel].sockfd[0] = *datafd; knet_h->sockfd[*channel].sockfd[1] = 0; if (!getsockopt(knet_h->sockfd[*channel].sockfd[0], SOL_SOCKET, SO_TYPE, &sockopt, &sockoptlen)) { knet_h->sockfd[*channel].is_socket = 1; } } else { if (_init_socketpair(knet_h, knet_h->sockfd[*channel].sockfd)) { savederrno = errno; err = -1; goto out_unlock; } knet_h->sockfd[*channel].is_created = 1; knet_h->sockfd[*channel].is_socket = 1; *datafd = knet_h->sockfd[*channel].sockfd[0]; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = knet_h->sockfd[*channel].sockfd[knet_h->sockfd[*channel].is_created]; if (epoll_ctl(knet_h->send_to_links_epollfd, EPOLL_CTL_ADD, knet_h->sockfd[*channel].sockfd[knet_h->sockfd[*channel].is_created], &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_HANDLE, "Unable to add datafd %d to linkfd epoll pool: %s", knet_h->sockfd[*channel].sockfd[knet_h->sockfd[*channel].is_created], strerror(savederrno)); if (knet_h->sockfd[*channel].is_created) { _close_socketpair(knet_h, knet_h->sockfd[*channel].sockfd); } goto out_unlock; } knet_h->sockfd[*channel].in_use = 1; out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_handle_remove_datafd(knet_handle_t knet_h, int datafd) { int err = 0, savederrno = 0; int8_t channel = -1; int i; struct epoll_event ev; if (!_is_valid_handle(knet_h)) { return -1; } if (datafd <= 0) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } for (i = 0; i < KNET_DATAFD_MAX; i++) { if ((knet_h->sockfd[i].in_use) && (knet_h->sockfd[i].sockfd[0] == datafd)) { channel = i; break; } } if (channel < 0) { savederrno = EINVAL; err = -1; goto out_unlock; } if (!knet_h->sockfd[channel].has_error) { memset(&ev, 0, sizeof(struct epoll_event)); if (epoll_ctl(knet_h->send_to_links_epollfd, EPOLL_CTL_DEL, knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created], &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_HANDLE, "Unable to del datafd %d from linkfd epoll pool: %s", knet_h->sockfd[channel].sockfd[0], strerror(savederrno)); goto out_unlock; } } if (knet_h->sockfd[channel].is_created) { _close_socketpair(knet_h, knet_h->sockfd[channel].sockfd); } memset(&knet_h->sockfd[channel], 0, sizeof(struct knet_sock)); out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_handle_get_datafd(knet_handle_t knet_h, const int8_t channel, int *datafd) { int err = 0, savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if ((channel < 0) || (channel >= KNET_DATAFD_MAX)) { errno = EINVAL; return -1; } if (datafd == NULL) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->sockfd[channel].in_use) { savederrno = EINVAL; err = -1; goto out_unlock; } *datafd = knet_h->sockfd[channel].sockfd[0]; out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_handle_get_channel(knet_handle_t knet_h, const int datafd, int8_t *channel) { int err = 0, savederrno = 0; int i; if (!_is_valid_handle(knet_h)) { return -1; } if (datafd <= 0) { errno = EINVAL; return -1; } if (channel == NULL) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } *channel = -1; for (i = 0; i < KNET_DATAFD_MAX; i++) { if ((knet_h->sockfd[i].in_use) && (knet_h->sockfd[i].sockfd[0] == datafd)) { *channel = i; break; } } if (*channel < 0) { savederrno = EINVAL; err = -1; goto out_unlock; } out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_handle_enable_filter(knet_handle_t knet_h, void *dst_host_filter_fn_private_data, int (*dst_host_filter_fn) ( void *private_data, const unsigned char *outdata, ssize_t outdata_len, uint8_t tx_rx, knet_node_id_t this_host_id, knet_node_id_t src_node_id, int8_t *channel, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries)) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->dst_host_filter_fn_private_data = dst_host_filter_fn_private_data; knet_h->dst_host_filter_fn = dst_host_filter_fn; if (knet_h->dst_host_filter_fn) { log_debug(knet_h, KNET_SUB_HANDLE, "dst_host_filter_fn enabled"); } else { log_debug(knet_h, KNET_SUB_HANDLE, "dst_host_filter_fn disabled"); } pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_handle_setfwd(knet_handle_t knet_h, unsigned int enabled) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (enabled > 1) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (enabled) { knet_h->enabled = enabled; log_debug(knet_h, KNET_SUB_HANDLE, "Data forwarding is enabled"); } else { /* * notify TX and RX threads to flush the queues */ if (set_thread_flush_queue(knet_h, KNET_THREAD_TX, KNET_THREAD_QUEUE_FLUSH) < 0) { log_debug(knet_h, KNET_SUB_HANDLE, "Unable to request queue flushing for TX thread"); } if (set_thread_flush_queue(knet_h, KNET_THREAD_RX, KNET_THREAD_QUEUE_FLUSH) < 0) { log_debug(knet_h, KNET_SUB_HANDLE, "Unable to request queue flushing for RX thread"); } } pthread_rwlock_unlock(&knet_h->global_rwlock); /* * when disabling data forward, we need to give time to TX and RX * to flush the queues. * * the TX thread is the main leader here. When there is no more * data in the TX queue, we will also close traffic for RX. */ if (!enabled) { /* * this usleep might be unnecessary, but wait_all_threads_flush_queue * adds extra locking delay. * * allow all threads to run free without extra locking interference * and then we switch to a more active wait in case the scheduler * has decided to delay one thread or another */ usleep(knet_h->threads_timer_res * 2); wait_all_threads_flush_queue(knet_h); /* * all threads have done flushing the queue, we can stop data forwarding */ savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->enabled = enabled; log_debug(knet_h, KNET_SUB_HANDLE, "Data forwarding is disabled"); pthread_rwlock_unlock(&knet_h->global_rwlock); } errno = 0; return 0; } int knet_handle_get_stats(knet_handle_t knet_h, struct knet_handle_stats *stats, size_t struct_size) { int err = 0, savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!stats) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } savederrno = pthread_mutex_lock(&knet_h->handle_stats_mutex); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get mutex lock: %s", strerror(savederrno)); err = -1; goto out_unlock; } if (struct_size > sizeof(struct knet_handle_stats)) { struct_size = sizeof(struct knet_handle_stats); } memmove(stats, &knet_h->stats, struct_size); /* * TX crypt stats only count the data packets sent, so add in the ping/pong/pmtud figures * RX is OK as it counts them before they are sorted. */ stats->tx_crypt_packets += knet_h->stats_extra.tx_crypt_ping_packets + knet_h->stats_extra.tx_crypt_pong_packets + knet_h->stats_extra.tx_crypt_pmtu_packets + knet_h->stats_extra.tx_crypt_pmtu_reply_packets; /* Tell the caller our full size in case they have an old version */ stats->size = sizeof(struct knet_handle_stats); out_unlock: pthread_mutex_unlock(&knet_h->handle_stats_mutex); pthread_rwlock_unlock(&knet_h->global_rwlock); return err; } int knet_handle_clear_stats(knet_handle_t knet_h, int clear_option) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (clear_option != KNET_CLEARSTATS_HANDLE_ONLY && clear_option != KNET_CLEARSTATS_HANDLE_AND_LINK) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } memset(&knet_h->stats, 0, sizeof(struct knet_handle_stats)); memset(&knet_h->stats_extra, 0, sizeof(struct knet_handle_stats_extra)); if (clear_option == KNET_CLEARSTATS_HANDLE_AND_LINK) { _link_clear_stats(knet_h); } pthread_rwlock_unlock(&knet_h->global_rwlock); return 0; } int knet_handle_enable_access_lists(knet_handle_t knet_h, unsigned int enabled) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (enabled > 1) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->use_access_lists = enabled; if (enabled) { log_debug(knet_h, KNET_SUB_HANDLE, "Links access lists are enabled"); } else { log_debug(knet_h, KNET_SUB_HANDLE, "Links access lists are disabled"); } pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_handle_get_host_defrag_bufs(knet_handle_t knet_h, uint16_t *min_defrag_bufs, uint16_t *max_defrag_bufs, uint8_t *shrink_threshold, defrag_bufs_reclaim_policy_t *reclaim_policy) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!min_defrag_bufs) { errno = EINVAL; return -1; } if (!max_defrag_bufs) { errno = EINVAL; return -1; } if (!shrink_threshold) { errno = EINVAL; return -1; } if (!reclaim_policy) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } *min_defrag_bufs = knet_h->defrag_bufs_min; *max_defrag_bufs = knet_h->defrag_bufs_max; *shrink_threshold = knet_h->defrag_bufs_shrink_threshold; *reclaim_policy = knet_h->defrag_bufs_reclaim_policy; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } static int _is_power_of_two(uint16_t num) { if ((num != 0) && ((num &(num - 1)) == 0)) { return 1; } return 0; } int knet_handle_set_host_defrag_bufs(knet_handle_t knet_h, uint16_t min_defrag_bufs, uint16_t max_defrag_bufs, uint8_t shrink_threshold, defrag_bufs_reclaim_policy_t reclaim_policy) { int err = 0, savederrno = 0; struct knet_host *host; if (!_is_valid_handle(knet_h)) { return -1; } if ((!min_defrag_bufs) || (!_is_power_of_two(min_defrag_bufs)) || (min_defrag_bufs > max_defrag_bufs)) { errno = EINVAL; return -1; } if ((!max_defrag_bufs) || (!_is_power_of_two(max_defrag_bufs)) || (max_defrag_bufs < min_defrag_bufs)) { errno = EINVAL; return -1; } if ((!shrink_threshold) || (shrink_threshold > 50)) { errno = EINVAL; return -1; } if (reclaim_policy > 1) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } /* * all those parameters are managed by thread_rx in read only context. * it is safe to change them here because we are in write lock. */ knet_h->defrag_bufs_min = min_defrag_bufs; knet_h->defrag_bufs_max = max_defrag_bufs; knet_h->defrag_bufs_shrink_threshold = shrink_threshold; knet_h->defrag_bufs_reclaim_policy = reclaim_policy; /* * reset all stats based on new values */ for (host = knet_h->host_head; host != NULL; host = host->next) { _clear_defrag_bufs_stats(host); } pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return err; } diff --git a/libknet/host.c b/libknet/host.c index a506d8e7..f7830d0e 100644 --- a/libknet/host.c +++ b/libknet/host.c @@ -1,844 +1,844 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "host.h" #include "internals.h" #include "logging.h" #include "threads_common.h" static void _host_list_update(knet_handle_t knet_h) { struct knet_host *host; knet_h->host_ids_entries = 0; for (host = knet_h->host_head; host != NULL; host = host->next) { knet_h->host_ids[knet_h->host_ids_entries] = host->host_id; knet_h->host_ids_entries++; } } int knet_host_add(knet_handle_t knet_h, knet_node_id_t host_id) { int savederrno = 0, err = 0; struct knet_host *host = NULL; uint8_t link_idx; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (knet_h->host_index[host_id]) { err = -1; savederrno = EEXIST; log_err(knet_h, KNET_SUB_HOST, "Unable to add host %u: %s", host_id, strerror(savederrno)); goto exit_unlock; } 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->defrag_bufs = malloc(knet_h->defrag_bufs_min * sizeof(struct knet_host_defrag_buf)); if (!host->defrag_bufs) { err = -1; savederrno = errno; log_err(knet_h, KNET_SUB_HOST, "Unable to allocate memory for host %u defrag buffers: %s", host_id, strerror(savederrno)); goto exit_unlock; } host->allocated_defrag_bufs = knet_h->defrag_bufs_min; memset(host->defrag_bufs, 0, host->allocated_defrag_bufs * sizeof(struct knet_host_defrag_buf)); log_debug(knet_h, KNET_SUB_HOST, "Allocated %u defrag buffers for host %u", host->allocated_defrag_bufs, host_id); /* * set host_id */ host->host_id = host_id; /* * fill up our own data */ if (knet_h->host_id == host->host_id) { host->onwire_ver = knet_h->onwire_ver; host->onwire_max_ver = knet_h->onwire_max_ver; } /* * set default host->name to host_id for logging */ snprintf(host->name, KNET_MAX_HOST_LEN, "%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; host->link[link_idx].status.stats.latency_min = UINT32_MAX; } /* * add new host to the index */ knet_h->host_index[host_id] = host; /* * add new host to host list */ if (knet_h->host_head) { host->next = knet_h->host_head; } knet_h->host_head = host; _host_list_update(knet_h); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); if (err < 0) { if (host) { free(host->defrag_bufs); } free(host); } errno = err ? savederrno : 0; return err; } int knet_host_remove(knet_handle_t knet_h, knet_node_id_t host_id) { int savederrno = 0, err = 0; struct knet_host *host, *removed; uint8_t link_idx; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get write 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_HOST, "Unable to remove host %u: %s", host_id, strerror(savederrno)); goto exit_unlock; } /* * if links are configured we cannot release the host */ for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { if (host->link[link_idx].configured) { err = -1; savederrno = EBUSY; log_err(knet_h, KNET_SUB_HOST, "Unable to remove host %u, links are still configured: %s", host_id, strerror(savederrno)); goto exit_unlock; } } removed = NULL; /* * removing host from list */ if (knet_h->host_head->host_id == host_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->host_id == host_id) { removed = host->next; host->next = removed->next; break; } } } knet_h->host_index[host_id] = NULL; if (removed) { free(removed->defrag_bufs); } free(removed); _host_list_update(knet_h); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_host_set_name(knet_handle_t knet_h, knet_node_id_t host_id, const char *name) { int savederrno = 0, err = 0; struct knet_host *host; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->host_index[host_id]) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_HOST, "Unable to find host %u to set name: %s", host_id, strerror(savederrno)); goto exit_unlock; } if (!name) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_HOST, "Unable to set name for host %u: %s", host_id, strerror(savederrno)); goto exit_unlock; } if (strlen(name) >= KNET_MAX_HOST_LEN) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_HOST, "Requested name for host %u is too long: %s", host_id, strerror(savederrno)); goto exit_unlock; } for (host = knet_h->host_head; host != NULL; host = host->next) { if (!strncmp(host->name, name, KNET_MAX_HOST_LEN)) { err = -1; savederrno = EEXIST; log_err(knet_h, KNET_SUB_HOST, "Duplicated name found on host_id %u", host->host_id); goto exit_unlock; } } snprintf(knet_h->host_index[host_id]->name, KNET_MAX_HOST_LEN, "%s", name); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_host_get_name_by_host_id(knet_handle_t knet_h, knet_node_id_t host_id, char *name) { int savederrno = 0, err = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!name) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->host_index[host_id]) { savederrno = EINVAL; err = -1; log_debug(knet_h, KNET_SUB_HOST, "Host %u not found", host_id); goto exit_unlock; } snprintf(name, KNET_MAX_HOST_LEN, "%s", knet_h->host_index[host_id]->name); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_host_get_id_by_host_name(knet_handle_t knet_h, const char *name, knet_node_id_t *host_id) { int savederrno = 0, err = 0, found = 0; struct knet_host *host; if (!_is_valid_handle(knet_h)) { return -1; } if (!name) { errno = EINVAL; return -1; } if (!host_id) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } for (host = knet_h->host_head; host != NULL; host = host->next) { if (!strncmp(name, host->name, KNET_MAX_HOST_LEN)) { found = 1; *host_id = host->host_id; break; } } if (!found) { savederrno = ENOENT; err = -1; } pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_host_get_host_list(knet_handle_t knet_h, knet_node_id_t *host_ids, size_t *host_ids_entries) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if ((!host_ids) || (!host_ids_entries)) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } memmove(host_ids, knet_h->host_ids, sizeof(knet_h->host_ids)); *host_ids_entries = knet_h->host_ids_entries; pthread_rwlock_unlock(&knet_h->global_rwlock); return 0; } int knet_host_set_policy(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t policy) { int savederrno = 0, err = 0; uint8_t old_policy; if (!_is_valid_handle(knet_h)) { return -1; } if (policy > KNET_LINK_POLICY_RR) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->host_index[host_id]) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_HOST, "Unable to set name for host %u: %s", host_id, strerror(savederrno)); goto exit_unlock; } old_policy = knet_h->host_index[host_id]->link_handler_policy; knet_h->host_index[host_id]->link_handler_policy = policy; if (_host_dstcache_update_async(knet_h, knet_h->host_index[host_id])) { savederrno = errno; err = -1; knet_h->host_index[host_id]->link_handler_policy = old_policy; log_debug(knet_h, KNET_SUB_HOST, "Unable to update switch cache for host %u: %s", host_id, strerror(savederrno)); } log_debug(knet_h, KNET_SUB_HOST, "Host %u has new switching policy: %u", host_id, policy); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_host_get_policy(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t *policy) { int savederrno = 0, err = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!policy) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->host_index[host_id]) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_HOST, "Unable to get name for host %u: %s", host_id, strerror(savederrno)); goto exit_unlock; } *policy = knet_h->host_index[host_id]->link_handler_policy; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_host_get_status(knet_handle_t knet_h, knet_node_id_t host_id, struct knet_host_status *status) { int savederrno = 0, err = 0; struct knet_host *host; if (!_is_valid_handle(knet_h)) { return -1; } if (!status) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "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_HOST, "Unable to find host %u: %s", host_id, strerror(savederrno)); goto exit_unlock; } memmove(status, &host->status, sizeof(struct knet_host_status)); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_host_enable_status_change_notify(knet_handle_t knet_h, void *host_status_change_notify_fn_private_data, void (*host_status_change_notify_fn) ( void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external)) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HOST, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->host_status_change_notify_fn_private_data = host_status_change_notify_fn_private_data; knet_h->host_status_change_notify_fn = host_status_change_notify_fn; if (knet_h->host_status_change_notify_fn) { log_debug(knet_h, KNET_SUB_HOST, "host_status_change_notify_fn enabled"); } else { log_debug(knet_h, KNET_SUB_HOST, "host_status_change_notify_fn disabled"); } pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } void _clear_defrag_bufs_stats(struct knet_host *host) { memset(&host->in_use_defrag_buffers, 0, sizeof(host->in_use_defrag_buffers)); host->in_use_defrag_buffers_samples = 0; host->in_use_defrag_buffers_index = 0; } static void _clear_cbuffers(struct knet_host *host, seq_num_t rx_seq_num) { int i; memset(host->circular_buffer, 0, KNET_CBUFFER_SIZE); host->rx_seq_num = rx_seq_num; memset(host->circular_buffer_defrag, 0, KNET_CBUFFER_SIZE); for (i = 0; i < host->allocated_defrag_bufs; i++) { memset(&host->defrag_bufs[i], 0, sizeof(struct knet_host_defrag_buf)); } _clear_defrag_bufs_stats(host); } static void _reclaim_old_defrag_bufs(knet_handle_t knet_h, struct knet_host *host, seq_num_t seq_num) { seq_num_t head, tail; /* seq_num boundaries */ int i; head = seq_num + 1; if (knet_h->defrag_bufs_max > host->allocated_defrag_bufs) { tail = seq_num - (knet_h->defrag_bufs_max + 1); } else { tail = seq_num - (host->allocated_defrag_bufs + 1); } /* * expire old defrag buffers */ for (i = 0; i < host->allocated_defrag_bufs; i++) { if (host->defrag_bufs[i].in_use) { /* * head has done a rollover to 0+ */ if (tail > head) { if ((host->defrag_bufs[i].pckt_seq >= head) && (host->defrag_bufs[i].pckt_seq <= tail)) { host->defrag_bufs[i].in_use = 0; } } else { if ((host->defrag_bufs[i].pckt_seq >= head) || (host->defrag_bufs[i].pckt_seq <= tail)){ host->defrag_bufs[i].in_use = 0; } } } } } /* * check if a given packet seq num is in the circular buffers * defrag_buf = 0 -> use normal cbuf 1 -> use the defrag buffer lookup */ int _seq_num_lookup(knet_handle_t knet_h, struct knet_host *host, seq_num_t seq_num, int defrag_buf, int clear_buf) { size_t head, tail; /* circular buffer indexes */ seq_num_t seq_dist; char *dst_cbuf = host->circular_buffer; char *dst_cbuf_defrag = host->circular_buffer_defrag; seq_num_t *dst_seq_num = &host->rx_seq_num; /* * There is a potential race condition where the sender * is overloaded, sending data packets before pings * can kick in and set the correct dst_seq_num. * * if this node is starting up (dst_seq_num = 0), * it can start rejecing valid packets and get stuck. * * Set the dst_seq_num to the first seen packet and * use that as reference instead. */ if (!*dst_seq_num) { *dst_seq_num = seq_num; } if (clear_buf) { _clear_cbuffers(host, seq_num); } _reclaim_old_defrag_bufs(knet_h, host, *dst_seq_num); if (seq_num < *dst_seq_num) { seq_dist = (SEQ_MAX - seq_num) + *dst_seq_num; } else { seq_dist = *dst_seq_num - seq_num; } head = seq_num % KNET_CBUFFER_SIZE; if (seq_dist < KNET_CBUFFER_SIZE) { /* seq num is in ring buffer */ if (!defrag_buf) { return (dst_cbuf[head] == 0) ? 1 : 0; } else { return (dst_cbuf_defrag[head] == 0) ? 1 : 0; } } else if (seq_dist <= SEQ_MAX - KNET_CBUFFER_SIZE) { memset(dst_cbuf, 0, KNET_CBUFFER_SIZE); memset(dst_cbuf_defrag, 0, KNET_CBUFFER_SIZE); *dst_seq_num = seq_num; } /* cleaning up circular buffer */ tail = (*dst_seq_num + 1) % KNET_CBUFFER_SIZE; if (tail > head) { memset(dst_cbuf + tail, 0, KNET_CBUFFER_SIZE - tail); memset(dst_cbuf, 0, head + 1); memset(dst_cbuf_defrag + tail, 0, KNET_CBUFFER_SIZE - tail); memset(dst_cbuf_defrag, 0, head + 1); } else { memset(dst_cbuf + tail, 0, head - tail + 1); memset(dst_cbuf_defrag + tail, 0, head - tail + 1); } *dst_seq_num = seq_num; return 1; } void _seq_num_set(struct knet_host *host, seq_num_t seq_num, int defrag_buf) { if (!defrag_buf) { host->circular_buffer[seq_num % KNET_CBUFFER_SIZE] = 1; } else { host->circular_buffer_defrag[seq_num % KNET_CBUFFER_SIZE] = 1; } return; } int _host_dstcache_update_async(knet_handle_t knet_h, struct knet_host *host) { int savederrno = 0; knet_node_id_t host_id = host->host_id; if (sendto(knet_h->dstsockfd[1], &host_id, sizeof(host_id), MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0) != sizeof(host_id)) { savederrno = errno; log_debug(knet_h, KNET_SUB_HOST, "Unable to write to dstpipefd[1]: %s", strerror(savederrno)); errno = savederrno; return -1; } return 0; } int _host_dstcache_update_sync(knet_handle_t knet_h, struct knet_host *host) { int link_idx; int best_priority = -1; int reachable = 0; if (knet_h->host_id == host->host_id && knet_h->has_loop_link) { host->active_link_entries = 1; return 0; } host->active_link_entries = 0; for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { if (host->link[link_idx].status.enabled != 1) /* link is not enabled */ continue; if (host->link[link_idx].status.connected != 1) /* link is not enabled */ continue; if (host->link[link_idx].has_valid_mtu != 1) /* link does not have valid MTU */ continue; if (host->link_handler_policy == KNET_LINK_POLICY_PASSIVE) { /* for passive we look for the only active link with higher priority */ if (host->link[link_idx].priority > best_priority) { host->active_links[0] = link_idx; best_priority = host->link[link_idx].priority; } host->active_link_entries = 1; } else { /* for RR and ACTIVE we need to copy all available links */ host->active_links[host->active_link_entries] = link_idx; host->active_link_entries++; } } if (host->link_handler_policy == KNET_LINK_POLICY_PASSIVE) { log_info(knet_h, KNET_SUB_HOST, "host: %u (passive) best link: %u (pri: %u)", host->host_id, host->link[host->active_links[0]].link_id, host->link[host->active_links[0]].priority); } else { log_info(knet_h, KNET_SUB_HOST, "host: %u has %u active links", host->host_id, host->active_link_entries); } /* no active links, we can clean the circular buffers and indexes */ if (!host->active_link_entries) { log_warn(knet_h, KNET_SUB_HOST, "host: %u has no active links", host->host_id); _clear_cbuffers(host, 0); } else { reachable = 1; } if (host->status.reachable != reachable) { host->status.reachable = reachable; if (knet_h->host_status_change_notify_fn) { knet_h->host_status_change_notify_fn( knet_h->host_status_change_notify_fn_private_data, host->host_id, host->status.reachable, host->status.remote, host->status.external); } } return 0; } void _handle_onwire_version(knet_handle_t knet_h, struct knet_host *host, struct knet_header *inbuf) { struct knet_host *tmp_host = NULL; uint8_t onwire_ver = knet_h->onwire_max_ver; int docallback = 0; /* * data we process here are onwire independent * we are in a global read only lock context, so it´s safe to parse host lists * and we can change onwire_ver using the dedicated mutex */ /* * update current host onwire info */ host->onwire_ver = inbuf->kh_version; host->onwire_max_ver = inbuf->kh_max_ver; for (tmp_host = knet_h->host_head; tmp_host != NULL; tmp_host = tmp_host->next) { /* * do not attempt to change protocol till * we see all nodes at least once. */ if (!tmp_host->onwire_max_ver) { return; } /* * ignore nodes were max ver is lower than our min ver * logged as error by thread_rx, we need to make sure to skip it * during onwire_ver calculation. */ if (tmp_host->onwire_max_ver < knet_h->onwire_min_ver) { continue; } /* * use the highest max_ver common to all known nodes */ if (tmp_host->onwire_max_ver < onwire_ver) { onwire_ver = tmp_host->onwire_max_ver; } } if (pthread_mutex_lock(&knet_h->onwire_mutex)) { log_debug(knet_h, KNET_SUB_HOST, "Unable to get onwire mutex lock"); return; } if (knet_h->onwire_force_ver) { onwire_ver = knet_h->onwire_force_ver; } if (knet_h->onwire_ver != onwire_ver) { log_debug(knet_h, KNET_SUB_HOST, "node %u updating onwire version to %u", knet_h->host_id, onwire_ver); knet_h->onwire_ver = onwire_ver; docallback = 1; } pthread_mutex_unlock(&knet_h->onwire_mutex); /* * do the callback outside of locked context and use cached value * to avoid blocking on locking */ if ((docallback) && (knet_h->onwire_ver_notify_fn)) { knet_h->onwire_ver_notify_fn(knet_h->onwire_ver_notify_fn_private_data, knet_h->onwire_min_ver, knet_h->onwire_max_ver, onwire_ver); } } diff --git a/libknet/host.h b/libknet/host.h index e4387dac..88048150 100644 --- a/libknet/host.h +++ b/libknet/host.h @@ -1,25 +1,25 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_HOST_H__ #define __KNET_HOST_H__ #include "internals.h" void _clear_defrag_bufs_stats(struct knet_host *host); int _seq_num_lookup(knet_handle_t knet_h, struct knet_host *host, seq_num_t seq_num, int defrag_buf, int clear_buf); void _seq_num_set(struct knet_host *host, seq_num_t seq_num, int defrag_buf); int _host_dstcache_update_async(knet_handle_t knet_h, struct knet_host *host); int _host_dstcache_update_sync(knet_handle_t knet_h, struct knet_host *host); void _handle_onwire_version(knet_handle_t knet_h, struct knet_host *host, struct knet_header *inbuf); #endif diff --git a/libknet/internals.h b/libknet/internals.h index f6aac2fe..24397141 100644 --- a/libknet/internals.h +++ b/libknet/internals.h @@ -1,495 +1,495 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_INTERNALS_H__ #define __KNET_INTERNALS_H__ /* * NOTE: you shouldn't need to include this header normally */ #include #include #include #include "libknet.h" #include "onwire.h" #include "compat.h" #include "threads_common.h" #define KNET_DATABUFSIZE KNET_MAX_PACKET_SIZE + KNET_HEADER_ALL_SIZE #define KNET_DATABUFSIZE_CRYPT_PAD 1024 #define KNET_DATABUFSIZE_CRYPT KNET_DATABUFSIZE + KNET_DATABUFSIZE_CRYPT_PAD #define KNET_DATABUFSIZE_COMPRESS_PAD 1024 #define KNET_DATABUFSIZE_COMPRESS KNET_DATABUFSIZE + KNET_DATABUFSIZE_COMPRESS_PAD #define KNET_RING_RCVBUFF 8388608 #define PCKT_FRAG_MAX UINT8_MAX #define PCKT_RX_BUFS 512 #define KNET_EPOLL_MAX_EVENTS KNET_DATAFD_MAX + 1 /* * Size of threads stack. Value is choosen by experimenting, how much is needed * to sucesfully finish test suite, and at the time of writing patch it was * ~300KiB. To have some room for future enhancement it is increased * by factor of 3 and rounded. */ #define KNET_THREAD_STACK_SIZE (1024 * 1024) typedef void *knet_transport_link_t; /* per link transport handle */ typedef void *knet_transport_t; /* per knet_h transport handle */ struct knet_transport_ops; /* Forward because of circular dependancy */ struct knet_mmsghdr { struct msghdr msg_hdr; /* Message header */ unsigned int msg_len; /* Number of bytes transmitted */ }; struct knet_link { /* required */ struct sockaddr_storage src_addr; struct sockaddr_storage dst_addr; /* configurable */ unsigned int dynamic; /* see KNET_LINK_DYN_ define above */ uint8_t priority; /* higher priority == preferred for A/P */ unsigned long long ping_interval; /* interval */ unsigned long long pong_timeout; /* timeout */ unsigned long long pong_timeout_adj; /* timeout adjusted for latency */ uint8_t pong_timeout_backoff; /* see link.h for definition */ unsigned int latency_max_samples; /* precision */ uint8_t pong_count; /* how many ping/pong to send/receive before link is up */ uint64_t flags; void *access_list_match_entry_head; /* pointer to access list match_entry list head */ /* status */ struct knet_link_status status; /* internals */ pthread_mutex_t link_stats_mutex; /* used to update link stats */ uint8_t link_id; uint8_t transport; /* #defined constant from API */ knet_transport_link_t transport_link; /* link_info_t from transport */ int outsock; unsigned int configured:1; /* set to 1 if src/dst have been configured transport initialized on this link*/ unsigned int transport_connected:1; /* set to 1 if lower level transport is connected */ uint8_t received_pong; struct timespec ping_last; /* used by PMTUD thread as temp per-link variables and should always contain the onwire_len value! */ uint32_t proto_overhead; /* IP + UDP/SCTP overhead. NOT to be confused with stats.proto_overhead that includes also knet headers and crypto headers */ struct timespec pmtud_last; uint32_t last_ping_size; uint32_t last_good_mtu; uint32_t last_bad_mtu; uint32_t last_sent_mtu; uint32_t last_recv_mtu; uint32_t pmtud_crypto_timeout_multiplier;/* used by PMTUd to adjust timeouts on high loads */ uint8_t has_valid_mtu; }; #define KNET_CBUFFER_SIZE 4096 struct knet_host_defrag_buf { char buf[KNET_DATABUFSIZE]; uint8_t in_use; /* 0 buffer is free, 1 is in use */ seq_num_t pckt_seq; /* identify the pckt we are receiving */ uint8_t frag_recv; /* how many frags did we receive */ uint8_t frag_map[PCKT_FRAG_MAX];/* bitmap of what we received? */ uint8_t last_first; /* special case if we receive the last fragment first */ ssize_t frag_size; /* normal frag size (not the last one) */ ssize_t last_frag_size; /* the last fragment might not be aligned with MTU size */ struct timespec last_update; /* keep time of the last pckt */ }; struct knet_host { /* required */ knet_node_id_t host_id; /* configurable */ uint8_t link_handler_policy; char name[KNET_MAX_HOST_LEN]; /* status */ struct knet_host_status status; /* * onwire info */ uint8_t onwire_ver; /* node current onwire version */ uint8_t onwire_max_ver; /* node supports up to this version */ /* internals */ char circular_buffer[KNET_CBUFFER_SIZE]; seq_num_t rx_seq_num; seq_num_t untimed_rx_seq_num; seq_num_t timed_rx_seq_num; uint8_t got_data; /* defrag/reassembly buffers */ struct knet_host_defrag_buf *defrag_bufs; uint16_t allocated_defrag_bufs; /* track use % of allocated defrag buffers */ uint8_t in_use_defrag_buffers[UINT8_MAX]; uint8_t in_use_defrag_buffers_samples; uint8_t in_use_defrag_buffers_index; char circular_buffer_defrag[KNET_CBUFFER_SIZE]; /* link stuff */ struct knet_link link[KNET_MAX_LINK]; uint8_t active_link_entries; uint8_t active_links[KNET_MAX_LINK]; struct knet_host *next; }; struct knet_sock { int sockfd[2]; /* sockfd[0] will always be application facing * and sockfd[1] internal if sockpair has been created by knet */ int is_socket; /* check if it's a socket for recvmmsg usage */ int is_created; /* knet created this socket and has to clean up on exit/del */ int in_use; /* set to 1 if it's use, 0 if free */ int has_error; /* set to 1 if there were errors reading from the sock * and socket has been removed from epoll */ }; struct knet_fd_trackers { uint8_t transport; /* transport type (UDP/SCTP...) */ uint8_t data_type; /* internal use for transport to define what data are associated * with this fd */ socklen_t sockaddr_len; /* Size of sockaddr_in[6] structure for this socket */ void *data; /* pointer to the data */ }; #define KNET_MAX_FDS KNET_MAX_HOST * KNET_MAX_LINK * 4 #define KNET_MAX_COMPRESS_METHODS UINT8_MAX #define KNET_MAX_CRYPTO_INSTANCES 2 struct knet_handle_stats_extra { uint64_t tx_crypt_pmtu_packets; uint64_t tx_crypt_pmtu_reply_packets; uint64_t tx_crypt_ping_packets; uint64_t tx_crypt_pong_packets; }; #define KNET_RX_ODD_PACKETS_THRESHOLD 20 #define KNET_USAGE_SAMPLES_DEFAULT UINT8_MAX #define KNET_USAGE_SAMPLES_TIMESPAN_DEFAULT 10 /* seconds */ struct knet_handle { knet_node_id_t host_id; unsigned int enabled:1; struct knet_sock sockfd[KNET_DATAFD_MAX + 1]; int logfd; uint8_t log_levels[KNET_MAX_SUBSYSTEMS]; int dstsockfd[2]; int send_to_links_epollfd; int recv_from_links_epollfd; int dst_link_handler_epollfd; uint8_t use_access_lists; /* set to 0 for disable, 1 for enable */ unsigned int pmtud_interval; unsigned int manual_mtu; unsigned int data_mtu; /* contains the max data size that we can send onwire * without frags */ struct knet_host *host_head; struct knet_host *host_index[KNET_MAX_HOST]; knet_transport_t transports[KNET_MAX_TRANSPORTS+1]; struct knet_fd_trackers knet_transport_fd_tracker[KNET_MAX_FDS]; /* track status for each fd handled by transports */ struct knet_handle_stats stats; struct knet_handle_stats_extra stats_extra; pthread_mutex_t handle_stats_mutex; /* used to protect handle stats */ uint32_t reconnect_int; knet_node_id_t host_ids[KNET_MAX_HOST]; size_t host_ids_entries; struct knet_header *recv_from_sock_buf; struct knet_header *send_to_links_buf[PCKT_FRAG_MAX]; struct knet_header *recv_from_links_buf[PCKT_RX_BUFS]; struct knet_header *pingbuf; struct knet_header *pmtudbuf; uint8_t threads_status[KNET_THREAD_MAX]; uint8_t threads_flush_queue[KNET_THREAD_MAX]; useconds_t threads_timer_res; pthread_mutex_t threads_status_mutex; pthread_t send_to_links_thread; pthread_t recv_from_links_thread; pthread_t heartbt_thread; pthread_t dst_link_handler_thread; pthread_t pmtud_link_handler_thread; pthread_rwlock_t global_rwlock; /* global config lock */ pthread_mutex_t pmtud_mutex; /* pmtud mutex to handle conditional send/recv + timeout */ pthread_cond_t pmtud_cond; /* conditional for above */ pthread_mutex_t tx_mutex; /* used to protect knet_send_sync and TX thread */ pthread_mutex_t hb_mutex; /* used to protect heartbeat thread and seq_num broadcasting */ pthread_mutex_t backoff_mutex; /* used to protect dst_link->pong_timeout_adj */ pthread_mutex_t kmtu_mutex; /* used to protect kernel_mtu */ pthread_mutex_t onwire_mutex; /* used to protect onwire version */ uint8_t onwire_ver; /* currently agreed onwire version across known nodes */ uint8_t onwire_min_ver; /* min and max are constant and don´t need any mutex protection. */ uint8_t onwire_max_ver; /* we define them as part of internal handle so that we can mingle with them for testing purposes */ uint8_t onwire_force_ver; /* manually configure onwire_ver */ uint8_t onwire_ver_remap; /* when this is on, all mapping will use version 1 for now */ uint8_t rx_odd_packets; /* used to warn if too many weird packets are being received */ uint32_t kernel_mtu; /* contains the MTU detected by the kernel on a given link */ int pmtud_waiting; int pmtud_running; int pmtud_forcerun; int pmtud_abort; struct crypto_instance *crypto_instance[KNET_MAX_CRYPTO_INSTANCES + 1]; /* store an extra pointer to allow 0|1|2 values without too much magic in the code */ uint8_t crypto_in_use_config; /* crypto config to use for TX */ uint8_t crypto_only; /* allow only crypto (1) or also clear (0) traffic */ size_t sec_block_size; size_t sec_hash_size; size_t sec_salt_size; unsigned char *send_to_links_buf_crypt[PCKT_FRAG_MAX]; unsigned char *recv_from_links_buf_crypt; unsigned char *recv_from_links_buf_decrypt; unsigned char *pingbuf_crypt; unsigned char *pmtudbuf_crypt; int compress_model; int compress_level; size_t compress_threshold; void *compress_int_data[KNET_MAX_COMPRESS_METHODS]; /* for compress method private data */ unsigned char *recv_from_links_buf_decompress; unsigned char *send_to_links_buf_compress; seq_num_t tx_seq_num; pthread_mutex_t tx_seq_num_mutex; uint16_t defrag_bufs_min; uint16_t defrag_bufs_max; uint8_t defrag_bufs_shrink_threshold; uint8_t defrag_bufs_usage_samples; uint8_t defrag_bufs_usage_samples_timespan; defrag_bufs_reclaim_policy_t defrag_bufs_reclaim_policy; struct timespec defrag_bufs_last_run; uint8_t has_loop_link; uint8_t loop_link; void *dst_host_filter_fn_private_data; int (*dst_host_filter_fn) ( void *private_data, const unsigned char *outdata, ssize_t outdata_len, uint8_t tx_rx, knet_node_id_t this_host_id, knet_node_id_t src_node_id, int8_t *channel, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries); void *pmtud_notify_fn_private_data; void (*pmtud_notify_fn) ( void *private_data, unsigned int data_mtu); void *host_status_change_notify_fn_private_data; void (*host_status_change_notify_fn) ( void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external); void *link_status_change_notify_fn_private_data; void (*link_status_change_notify_fn) ( void *private_data, knet_node_id_t host_id, uint8_t link_id, uint8_t connected, uint8_t remote, uint8_t external); void *sock_notify_fn_private_data; void (*sock_notify_fn) ( void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno); void *onwire_ver_notify_fn_private_data; void (*onwire_ver_notify_fn) ( void *private_data, uint8_t onwire_min_ver, uint8_t onwire_max_ver, uint8_t onwire_ver); int fini_in_progress; uint64_t flags; struct qb_list_head list; const char *plugin_path; }; struct handle_tracker { struct qb_list_head head; }; /* * lib_config stuff shared across everything */ extern pthread_rwlock_t shlib_rwlock; /* global shared lib load lock */ extern pthread_mutex_t handle_config_mutex; extern struct handle_tracker handle_list; extern uint8_t handle_list_init; int _is_valid_handle(knet_handle_t knet_h); int _init_shlib_tracker(knet_handle_t knet_h); void _fini_shlib_tracker(void); /* * NOTE: every single operation must be implementend * for every protocol. */ /* * for now knet supports only IP protocols (udp/sctp) * in future there might be others like ARP * or TIPC. * keep this around as transport information * to use for access lists and other operations */ #define TRANSPORT_PROTO_LOOPBACK 0 #define TRANSPORT_PROTO_IP_PROTO 1 /* * some transports like SCTP can filter incoming * connections before knet has to process * any packets. * GENERIC_ACL -> packet has to be read and filterted * PROTO_ACL -> transport provides filtering at lower levels * and packet does not need to be processed */ typedef enum { USE_NO_ACL, USE_GENERIC_ACL, USE_PROTO_ACL } transport_acl; /* * make it easier to map values in transports.c */ #define TRANSPORT_PROTO_NOT_CONNECTION_ORIENTED 0 #define TRANSPORT_PROTO_IS_CONNECTION_ORIENTED 1 /* * Returns from transport_tx_sock_error */ typedef enum { KNET_TRANSPORT_RX_ERROR = -1, KNET_TRANSPORT_SOCK_ERROR_INTERNAL = -1, KNET_TRANSPORT_SOCK_ERROR_IGNORE = 0, KNET_TRANSPORT_SOCK_ERROR_RETRY = 1, } transport_sock_error_t; /* * Returns from transport_rx_is_data */ typedef enum { KNET_TRANSPORT_RX_ISDATA_ERROR = -1, KNET_TRANSPORT_RX_NOT_DATA_CONTINUE = 0, KNET_TRANSPORT_RX_NOT_DATA_STOP = 1, KNET_TRANSPORT_RX_IS_DATA = 2, /* These two are only really used by SCTP */ KNET_TRANSPORT_RX_OOB_DATA_CONTINUE = 3, KNET_TRANSPORT_RX_OOB_DATA_STOP = 4 } transport_rx_isdata_t; /* * All functions that return int return 0 for success, and -1 for failure. */ typedef struct knet_transport_ops { /* * transport generic information */ const char *transport_name; const uint8_t transport_id; const uint8_t built_in; uint8_t transport_protocol; transport_acl transport_acl_type; /* * connection oriented protocols like SCTP * don´t need dst_addr in sendto calls and * on some OSes are considered EINVAL. */ uint8_t transport_is_connection_oriented; uint32_t transport_mtu_overhead; /* * transport init must allocate the new transport * and perform all internal initializations * (threads, lists, etc). */ int (*transport_init)(knet_handle_t knet_h); /* * transport free must releases _all_ resources * allocated by tranport_init */ int (*transport_free)(knet_handle_t knet_h); /* * link operations should take care of all the * sockets and epoll management for a given link/transport set * transport_link_disable should return -1 and errno = EBUSY * if listener is still in use, and any other errno in case * the link cannot be disabled. * * set_config/clear_config are invoked in global write lock context */ int (*transport_link_set_config)(knet_handle_t knet_h, struct knet_link *link); int (*transport_link_clear_config)(knet_handle_t knet_h, struct knet_link *link); /* * transport callback for incoming dynamic connections * this is called in global read lock context */ int (*transport_link_dyn_connect)(knet_handle_t knet_h, int sockfd, struct knet_link *link); /* * per transport error handling of recvmmsg * (see _handle_recv_from_links comments for details) */ /* * transport_rx_sock_error is invoked when recvmmsg returns <= 0 * * transport_rx_sock_error is invoked with both global_rdlock */ int (*transport_rx_sock_error)(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno); /* * transport_tx_sock_error is invoked with global_rwlock and * it's invoked when sendto or sendmmsg returns =< 0 * * it should return a transport_sock_error_t * any sleep or wait action should happen inside the transport code */ transport_sock_error_t (*transport_tx_sock_error)(knet_handle_t knet_h, int sockfd, int subsys, int recv_err, int recv_errno); /* * this function is called on _every_ received packet * to verify if the packet is data or internal protocol error handling * * it should return a transport_tx_isdata_t * * transport_rx_is_data is invoked with both global_rwlock * and fd_tracker read lock (from RX thread) */ transport_rx_isdata_t (*transport_rx_is_data)(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg); /* * this function is called by links.c when a link down event is recorded * to notify the transport that packets are not going through, and give * transport the opportunity to take actions. */ int (*transport_link_is_down)(knet_handle_t knet_h, struct knet_link *link); } knet_transport_ops_t; struct pretty_names { const char *name; uint8_t val; }; #endif diff --git a/libknet/lib_config.c b/libknet/lib_config.c index 8659d2eb..ea956879 100644 --- a/libknet/lib_config.c +++ b/libknet/lib_config.c @@ -1,86 +1,86 @@ /* - * Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include "internals.h" #include "logging.h" pthread_mutex_t handle_config_mutex = PTHREAD_MUTEX_INITIALIZER; struct handle_tracker handle_list; uint8_t handle_list_init = 0; int _is_valid_handle(knet_handle_t knet_h) { int found = 0; int savederrno = 0; knet_handle_t temp = NULL; /* * we are validating the handle, hence we cannot use * the handle for logging purposes */ savederrno = pthread_mutex_lock(&handle_config_mutex); if (savederrno) { errno = savederrno; return 0; } errno = EINVAL; /* * this is to protect against knet_handle_free being called * before knet_handle_new that initialize the list struct */ if (handle_list_init) { qb_list_for_each_entry(temp, &handle_list.head, list) { if (temp == knet_h) { found = 1; errno = 0; } } } pthread_mutex_unlock(&handle_config_mutex); return found; } pthread_rwlock_t shlib_rwlock; static uint8_t shlib_wrlock_init = 0; int _init_shlib_tracker(knet_handle_t knet_h) { int savederrno = 0; if (!shlib_wrlock_init) { savederrno = pthread_rwlock_init(&shlib_rwlock, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize shared lib rwlock: %s", strerror(savederrno)); errno = savederrno; return -1; } shlib_wrlock_init = 1; } return 0; } void _fini_shlib_tracker(void) { if (qb_list_empty(&handle_list.head)) { pthread_rwlock_destroy(&shlib_rwlock); shlib_wrlock_init = 0; } return; } diff --git a/libknet/libknet.h b/libknet/libknet.h index 5face2c1..a34d394e 100644 --- a/libknet/libknet.h +++ b/libknet/libknet.h @@ -1,2699 +1,2699 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __LIBKNET_H__ #define __LIBKNET_H__ #include #include #include #include #include /** * @file libknet.h * @brief kronosnet API include file - * @copyright Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * @copyright Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Kronosnet is an advanced VPN system for High Availability applications. */ #define KNET_API_VER 2 /* * libknet limits */ /** typedef for a knet node */ typedef uint16_t knet_node_id_t; /* * Maximum number of hosts */ #define KNET_MAX_HOST 65536 /* * Maximum number of links between 2 hosts */ #define KNET_MAX_LINK 8 /* * Maximum packet size that should be written to datafd * see knet_handle_new for details */ #define KNET_MAX_PACKET_SIZE 65536 /* * Buffers used for pretty logging * host is used to store both ip addresses and hostnames */ #define KNET_MAX_HOST_LEN 256 #define KNET_MAX_PORT_LEN 6 /* * Some notifications can be generated either on TX or RX */ #define KNET_NOTIFY_TX 0 #define KNET_NOTIFY_RX 1 /* * Link flags */ /* * Where possible, set traffic priority to high. * On Linux this sets the TOS to INTERACTIVE (6), * see tc-prio(8) for more infomation */ #define KNET_LINK_FLAG_TRAFFICHIPRIO (1ULL << 0) /* * Handle flags */ /* * Use privileged operations during socket setup. */ #define KNET_HANDLE_FLAG_PRIVILEGED (1ULL << 0) /* * threads timer resolution (see knet_handle_set_threads_timer_res below) */ #define KNET_THREADS_TIMER_RES 200000 /** * Opaque handle for this knet connection, created with knet_handle_new() and * freed with knet_handle_free() */ typedef struct knet_handle *knet_handle_t; /* * Handle structs/API calls */ /** * knet_handle_new * * @brief create a new instance of a knet handle * * host_id - Each host in a knet is identified with a unique * ID. when creating a new handle local host_id * must be specified (0 to UINT16_MAX are all valid). * It is the user's responsibility to check that the value * is unique, or bad things might happen. * * log_fd - Write file descriptor. If set to a value > 0, it will be used * to write log packets from libknet to the application. * Setting to 0 will disable logging from libknet. * It is possible to enable logging at any given time (see logging API). * Make sure to either read from this filedescriptor properly and/or * mark it O_NONBLOCK, otherwise if the fd becomes full, libknet could * block. * It is strongly encouraged to use pipes (ex: pipe(2) or pipe2(2)) for * logging fds due to the atomic nature of writes between fds. * See also libknet test suite for reference and guidance. * The caller is responsible for management of the FD. eg. knet will not * close it when knet_handle_free(3) is called * * default_log_level - * If logfd is specified, it will initialize all subsystems to log * at default_log_level value. (see logging API) * * flags - bitwise OR of some of the following flags: * KNET_HANDLE_FLAG_PRIVILEGED: use privileged operations setting up the * communication sockets. If disabled, failure to acquire large * enough socket buffers is ignored but logged. Inadequate buffers * lead to poor performance. * * @return * on success, a new knet_handle_t is returned. * on failure, NULL is returned and errno is set. * knet-specific errno values: * ENAMETOOLONG - socket buffers couldn't be set big enough and KNET_HANDLE_FLAG_PRIVILEGED was specified * ERANGE - buffer size readback returned unexpected type */ knet_handle_t knet_handle_new(knet_node_id_t host_id, int log_fd, uint8_t default_log_level, uint64_t flags); /** * knet_handle_free * * @brief Destroy a knet handle, free all resources * * knet_h - pointer to knet_handle_t * * @return * knet_handle_free returns * 0 on success * -1 on error and errno is set. */ int knet_handle_free(knet_handle_t knet_h); /** * knet_handle_set_threads_timer_res * * @brief Change internal thread timer resolution * * knet_h - pointer to knet_handle_t * * timeres - some threads inside knet will use usleep(timeres) * to check if any activity has to be performed, or wait * for the next cycle. 'timeres' (expressed in nano seconds) * defines this interval, with a default of KNET_THREADS_TIMER_RES * (200000). * The lower this value is, the more often knet will perform * those checks and allows a more (time) precise execution of * some operations (for example ping/pong), at the cost of higher * CPU usage. * Accepted values: * 0 - reset timer res to default * 1 - 999 invalid (as it would cause 100% CPU spinning on some * epoll operations) * 1000 or higher - valid * * Unless you know exactly what you are doing, stay away from * changing the default or seek written and notarized approval * from the knet developer team. * * @return * knet_handle_set_threads_timer_res returns * 0 on success * -1 on error and errno is set. */ int knet_handle_set_threads_timer_res(knet_handle_t knet_h, useconds_t timeres); /** * knet_handle_get_threads_timer_res * * @brief Get internal thread timer resolutions * * knet_h - pointer to knet_handle_t * * timeres - current timer res value * * @return * knet_handle_set_threads_timer_res returns * 0 on success and timerres will contain the current value * -1 on error and errno is set. */ int knet_handle_get_threads_timer_res(knet_handle_t knet_h, useconds_t *timeres); /** * knet_handle_enable_sock_notify * * @brief Register a callback to receive socket events * * knet_h - pointer to knet_handle_t * * sock_notify_fn_private_data * void pointer to data that can be used to identify * the callback. * * sock_notify_fn * A callback function that is invoked every time * a socket in the datafd pool will report an error (-1) * or an end of read (0) (see socket.7). * This function MUST NEVER block or add substantial delays. * The callback is invoked in an internal unlocked area * to allow calls to knet_handle_add_datafd/knet_handle_remove_datafd * to swap/replace the bad fd. * if both err and errno are 0, it means that the socket * has received a 0 byte packet (EOF?). * The callback function must either remove the fd from knet * (by calling knet_handle_remove_fd()) or dup a new fd in its place. * Failure to do this can cause problems. * * @return * knet_handle_enable_sock_notify returns * 0 on success * -1 on error and errno is set. */ int knet_handle_enable_sock_notify(knet_handle_t knet_h, void *sock_notify_fn_private_data, void (*sock_notify_fn) ( void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno)); /* sorry! can't call it errno ;) */ #define KNET_DATAFD_MAX 32 /** * knet_handle_add_datafd * * @brief Install a file descriptor for communication * * IMPORTANT: In order to add datafd to knet, knet_handle_enable_sock_notify * _MUST_ be set and be able to handle both errors (-1) and * 0 bytes read / write from the provided datafd. * On read error (< 0) from datafd, the socket is automatically * removed from polling to avoid spinning on dead sockets. * It is safe to call knet_handle_remove_datafd even on sockets * that have been removed. * * knet_h - pointer to knet_handle_t * * *datafd - read/write file descriptor. * knet will read data here to send to the other hosts * and will write data received from the network. * Each data packet can be of max size KNET_MAX_PACKET_SIZE! * Applications using knet_send/knet_recv will receive a * proper error if the packet size is not within boundaries. * Applications using their own functions to write to the * datafd should NOT write more than KNET_MAX_PACKET_SIZE. * * Please refer to handle.c on how to set up a socketpair. * * datafd can be 0, and knet_handle_add_datafd will create a properly * populated socket pair the same way as ping_test, or a value * higher than 0. A negative number will return an error. * On exit knet_handle_free will take care to cleanup the * socketpair only if they have been created by knet_handle_add_datafd. * * It is possible to pass either sockets or normal fds. * User provided datafd will be marked as non-blocking and close-on-exec. * * *channel - This value is analogous to the tag in VLAN tagging. * A negative value will auto-allocate a channel. * Setting a value between 0 and 31 will try to allocate that * specific channel (unless already in use). * * It is possible to add up to 32 datafds but be aware that each * one of them must have a receiving end on the other host. * * Example: * hostA channel 0 will be delivered to datafd on hostB channel 0 * hostA channel 1 to hostB channel 1. * * Each channel must have a unique file descriptor. * * If your application could have 2 channels on one host and one * channel on another host, then you can use dst_host_filter * to manipulate channel values on TX and RX. * * @return * knet_handle_add_datafd returns * @retval 0 on success, * *datafd will be populated with a socket if the original value was 0 * or if a specific fd was set, the value is untouched. * *channel will be populated with a channel number if the original value * was negative or the value is untouched if a specific channel * was requested. * * @retval -1 on error and errno is set. * *datafd and *channel are untouched or empty. */ int knet_handle_add_datafd(knet_handle_t knet_h, int *datafd, int8_t *channel); /** * knet_handle_remove_datafd * * @brief Remove a file descriptor from knet * * knet_h - pointer to knet_handle_t * * datafd - file descriptor to remove. * NOTE that if the socket/fd was created by knet_handle_add_datafd, * the socket will be closed by libknet. * * @return * knet_handle_remove_datafd returns * 0 on success * -1 on error and errno is set. */ int knet_handle_remove_datafd(knet_handle_t knet_h, int datafd); /** * knet_handle_get_channel * * @brief Get the channel associated with a file descriptor * * knet_h - pointer to knet_handle_t * * datafd - get the channel associated to this datafd * * *channel - will contain the result * * @return * knet_handle_get_channel returns * @retval 0 on success * and *channel will contain the result * @retval -1 on error and errno is set. * and *channel content is meaningless */ int knet_handle_get_channel(knet_handle_t knet_h, const int datafd, int8_t *channel); /** * knet_handle_get_datafd * * @brief Get the file descriptor associated with a channel * * knet_h - pointer to knet_handle_t * * channel - get the datafd associated to this channel * * *datafd - will contain the result * * @return * knet_handle_get_datafd returns * @retval 0 on success * and *datafd will contain the results * @retval -1 on error and errno is set. * and *datafd content is meaningless */ int knet_handle_get_datafd(knet_handle_t knet_h, const int8_t channel, int *datafd); /** * knet_recv * * @brief Receive data from knet nodes * * knet_h - pointer to knet_handle_t * * buff - pointer to buffer to store the received data * * buff_len - buffer length * * channel - channel number * * @return * knet_recv is a commodity function to wrap iovec operations * around a socket. It returns a call to readv(2). */ ssize_t knet_recv(knet_handle_t knet_h, char *buff, const size_t buff_len, const int8_t channel); /** * knet_send * * @brief Send data to knet nodes * * knet_h - pointer to knet_handle_t * * buff - pointer to the buffer of data to send * * buff_len - length of data to send * * channel - channel number * * @return * knet_send is a commodity function to wrap iovec operations * around a socket. It returns a call to writev(2). */ ssize_t knet_send(knet_handle_t knet_h, const char *buff, const size_t buff_len, const int8_t channel); /** * knet_send_sync * * @brief Synchronously send data to knet nodes * * knet_h - pointer to knet_handle_t * * buff - pointer to the buffer of data to send * * buff_len - length of data to send * * channel - data channel to use (see knet_handle_add_datafd(3)) * * All knet RX/TX operations are async for performance reasons. * There are applications that might need a sync version of data * transmission and receive errors in case of failure to deliver * to another host. * knet_send_sync bypasses the whole TX async layer and delivers * data directly to the link layer, and returns errors accordingly. * knet_send_sync sends only one packet to one host at a time. * It does NOT support multiple destinations or multicast packets. * Decision is still based on dst_host_filter_fn. * * @return * knet_send_sync returns 0 on success and -1 on error. * In addition to normal sendmmsg errors, knet_send_sync can fail * due to: * * @retval ECANCELED - data forward is disabled * @retval EFAULT - dst_host_filter fatal error * @retval EINVAL - dst_host_filter did not provide dst_host_ids_entries on unicast pckts * @retval E2BIG - dst_host_filter did return more than one dst_host_ids_entries on unicast pckts * @retval ENOMSG - received unknown message type * @retval EHOSTDOWN - unicast pckt cannot be delivered because dest host is not connected yet * @retval ECHILD - crypto failed * @retval EAGAIN - sendmmsg was unable to send all messages and there was no progress during retry * @retval ENETDOWN - a packet filter was not installed (necessary for knet_send_sync, but not knet_send) */ int knet_send_sync(knet_handle_t knet_h, const char *buff, const size_t buff_len, const int8_t channel); /** * knet_handle_enable_filter * * @brief install a filter to route packets * * knet_h - pointer to knet_handle_t * * dst_host_filter_fn_private_data * void pointer to data that can be used to identify * the callback. * * dst_host_filter_fn - * is a callback function that is invoked every time * a packet hits datafd (see knet_handle_new(3)). * the function allows users to tell libknet where the * packet has to be delivered. * * const unsigned char *outdata - is a pointer to the * current packet * ssize_t outdata_len - length of the above data * uint8_t tx_rx - filter is called on tx or rx * (KNET_NOTIFY_TX, KNET_NOTIFY_RX) * knet_node_id_t this_host_id - host_id processing the packet * knet_node_id_t src_host_id - host_id that generated the * packet * knet_node_id_t *dst_host_ids - array of KNET_MAX_HOST knet_node_id_t * where to store the destinations * size_t *dst_host_ids_entries - number of hosts to send the message * * dst_host_filter_fn should return * -1 on error, packet is discarded. * 0 packet is unicast and should be sent to dst_host_ids and there are * dst_host_ids_entries in the buffer. * 1 packet is broadcast/multicast and is sent all hosts. * contents of dst_host_ids and dst_host_ids_entries are ignored. * * @return * knet_handle_enable_filter returns * 0 on success * -1 on error and errno is set. */ int knet_handle_enable_filter(knet_handle_t knet_h, void *dst_host_filter_fn_private_data, int (*dst_host_filter_fn) ( void *private_data, const unsigned char *outdata, ssize_t outdata_len, uint8_t tx_rx, knet_node_id_t this_host_id, knet_node_id_t src_host_id, int8_t *channel, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries)); /** * knet_handle_setfwd * * @brief Start packet forwarding * * knet_h - pointer to knet_handle_t * * enable - set to 1 to allow data forwarding, 0 to disable data forwarding. * * @return * knet_handle_setfwd returns * 0 on success * -1 on error and errno is set. * * By default data forwarding is off and no traffic will pass through knet until * it is set on. */ int knet_handle_setfwd(knet_handle_t knet_h, unsigned int enabled); /** * knet_handle_enable_access_lists * * @brief Enable or disable usage of access lists (default: off) * * knet_h - pointer to knet_handle_t * * enable - set to 1 to use access lists, 0 to disable access_lists. * * @return * knet_handle_enable_access_lists returns * 0 on success * -1 on error and errno is set. * * access lists are bound to links. There are 2 types of links: * 1) point to point, where both source and destinations are well known * at configuration time. * 2) open links, where only the source is known at configuration time. * * knet will automatically generate access lists for point to point links. * * For open links, knet provides 4 API calls to manipulate access lists: * knet_link_add_acl(3), knet_link_rm_acl(3), knet_link_insert_acl(3) * and knet_link_clear_acl(3). * Those API calls will work exclusively on open links as they * are of no use on point to point links. * * knet will not enforce any access list unless specifically enabled by * knet_handle_enable_access_lists(3). * * From a security / programming perspective we recommend: * - create the knet handle * - enable access lists * - configure hosts and links * - configure access lists for open links */ int knet_handle_enable_access_lists(knet_handle_t knet_h, unsigned int enabled); #define KNET_PMTUD_DEFAULT_INTERVAL 60 /** * knet_handle_pmtud_setfreq * * @brief Set the interval between PMTUd scans * * knet_h - pointer to knet_handle_t * * interval - define the interval in seconds between PMTUd scans * range from 1 to 86400 (24h) * * @return * knet_handle_pmtud_setfreq returns * 0 on success * -1 on error and errno is set. * * default interval is 60. */ int knet_handle_pmtud_setfreq(knet_handle_t knet_h, unsigned int interval); /** * knet_handle_pmtud_getfreq * * @brief Get the interval between PMTUd scans * * knet_h - pointer to knet_handle_t * * interval - pointer where to store the current interval value * * @return * knet_handle_pmtud_setfreq returns * 0 on success * -1 on error and errno is set. */ int knet_handle_pmtud_getfreq(knet_handle_t knet_h, unsigned int *interval); /** * knet_handle_enable_pmtud_notify * * @brief install a callback to receive PMTUd changes * * knet_h - pointer to knet_handle_t * * pmtud_notify_fn_private_data * void pointer to data that can be used to identify * the callback. * * pmtud_notify_fn * is a callback function that is invoked every time * a path MTU size change is detected. * The function allows libknet to notify the user * of data MTU, that's the max value that can be send * onwire without fragmentation. The data MTU will always * be lower than real link MTU because it accounts for * protocol overhead, knet packet header and (if configured) * crypto overhead, * This function MUST NEVER block or add substantial delays. * * @return * knet_handle_enable_pmtud_notify returns * 0 on success * -1 on error and errno is set. */ int knet_handle_enable_pmtud_notify(knet_handle_t knet_h, void *pmtud_notify_fn_private_data, void (*pmtud_notify_fn) ( void *private_data, unsigned int data_mtu)); /** * knet_handle_pmtud_set * * @brief Set the current interface MTU * * knet_h - pointer to knet_handle_t * * iface_mtu - current interface MTU, value 0 to 65535. 0 will * re-enable automatic MTU discovery. * In a setup with multiple interfaces, please specify * the lowest MTU between the selected intefaces. * knet will automatically adjust this value for * all headers overhead and set the correct data_mtu. * data_mtu can be retrivied with knet_handle_pmtud_get(3) * or applications will receive a pmtud_notify event * if enabled via knet_handle_enable_pmtud_notify(3). * * @return * knet_handle_pmtud_set returns * 0 on success * -1 on error and errno is set. */ int knet_handle_pmtud_set(knet_handle_t knet_h, unsigned int iface_mtu); /** * knet_handle_pmtud_get * * @brief Get the current data MTU * * knet_h - pointer to knet_handle_t * * data_mtu - pointer where to store data_mtu * * @return * knet_handle_pmtud_get returns * 0 on success * -1 on error and errno is set. */ int knet_handle_pmtud_get(knet_handle_t knet_h, unsigned int *data_mtu); #define KNET_MIN_KEY_LEN 128 #define KNET_MAX_KEY_LEN 4096 /** * Structure passed into knet_handle_set_crypto_config() to determine * the crypto options to use for the current communications handle */ struct knet_handle_crypto_cfg { /** Model to use. nss, openssl, etc */ char crypto_model[16]; /** Cipher type name for encryption. aes 256 etc */ char crypto_cipher_type[16]; /** Hash type for digest. sha512 etc */ char crypto_hash_type[16]; /** Private key */ unsigned char private_key[KNET_MAX_KEY_LEN]; /** Length of private key */ unsigned int private_key_len; }; /** * knet_handle_crypto_set_config * * @brief set up packet cryptographic signing & encryption * * knet_h - pointer to knet_handle_t * * knet_handle_crypto_cfg - * pointer to a knet_handle_crypto_cfg structure * * crypto_model should contain the model name. * Currently "openssl", "nss" and "gcrypt" are supported. * Setting to "none" will disable crypto. * * crypto_cipher_type * should contain the cipher algo name. * It can be set to "none" to disable * encryption. * Currently supported by "nss" model: * "aes128", "aes192" and "aes256". * "openssl" model supports more modes and it strictly * depends on the openssl build. See: EVP_get_cipherbyname * openssl API call for details. * * crypto_hash_type * should contain the hashing algo name. * It can be set to "none" to disable * hashing. * Currently supported by "nss" model: * "md5", "sha1", "sha256", "sha384" and "sha512". * "openssl" model supports more modes and it strictly * depends on the openssl build. See: EVP_get_digestbyname * openssl API call for details. * * private_key will contain the private shared key. * It has to be at least KNET_MIN_KEY_LEN long. * * private_key_len * length of the provided private_key. * * config_num - knet supports 2 concurrent sets of crypto configurations, * to allow runtime change of crypto config and keys. * On RX both configurations will be used sequentially * in an attempt to decrypt/validate a packet (when 2 are available). * Note that this might slow down performance during a reconfiguration. * See also knet_handle_crypto_rx_clear_traffic(3) to enable / disable * processing of clear (unencrypted) traffic. * For TX, the user needs to specify which configuration to use via * knet_handle_crypto_use_config(3). * config_num accepts 0, 1 or 2 as the value. 0 should be used when * all crypto is being disabled. * Calling knet_handle_crypto_set_config(3) twice with * the same config_num will REPLACE the configuration and * NOT activate the second key. If the configuration is currently in use * EBUSY will be returned. See also knet_handle_crypto_use_config(3). * The correct sequence to perform a runtime rekey / reconfiguration * is: * - knet_handle_crypto_set_config(..., 1). -> first time config, will use config1 * - knet_handle_crypto_use_config(..., 1). -> switch TX to config 1 * - knet_handle_crypto_set_config(..., 2). -> install config2 and use it only for RX * - knet_handle_crypto_use_config(..., 2). -> switch TX to config 2 * - knet_handle_crypto_set_config(..., 1). -> with a "none"/"none"/"none" configuration to * release the resources previously allocated * The application is responsible for synchronizing calls on the nodes * to make sure the new config is in place before switching the TX configuration. * Failure to do so will result in knet being unable to talk to some of the nodes. * * Implementation notes/current limitations: * - enabling crypto, will increase latency as packets have * to processed. * - enabling crypto might reduce the overall throughtput * due to crypto data overhead. * - private/public key encryption/hashing is not currently * planned. * - crypto key must be the same for all hosts in the same * knet instance / configX. * - it is safe to call knet_handle_crypto_set_config multiple times at runtime. * The last config will be used. * IMPORTANT: a call to knet_handle_crypto_set_config can fail due to: * 1) failure to obtain locking * 2) errors to initializing the crypto level. * This can happen even in subsequent calls to knet_handle_crypto_set_config(3). * A failure in crypto init will restore the previous crypto configuration if any. * * @return * knet_handle_crypto_set_config returns: * @retval 0 on success * @retval -1 on error and errno is set. * @retval -2 on crypto subsystem initialization error. No errno is provided at the moment (yet). */ int knet_handle_crypto_set_config(knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg, uint8_t config_num); #define KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC 0 #define KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC 1 /** * knet_handle_crypto_rx_clear_traffic * * @brief enable or disable RX processing of clear (unencrypted) traffic * * knet_h - pointer to knet_handle_t * * value - KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC or KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC * * @return * knet_handle_crypto_use_config returns: * @retval 0 on success * @retval -1 on error and errno is set. */ int knet_handle_crypto_rx_clear_traffic(knet_handle_t knet_h, uint8_t value); /** * knet_handle_crypto_use_config * * @brief specify crypto configuration to use for TX * * knet_h - pointer to knet_handle_t * * config_num - 1|2 use configuration 1 or 2, 0 for clear (unencrypted) traffic. * * @return * knet_handle_crypto_use_config returns: * @retval 0 on success * @retval -1 on error and errno is set. */ int knet_handle_crypto_use_config(knet_handle_t knet_h, uint8_t config_num); #define KNET_COMPRESS_THRESHOLD 100 /** * Structure passed into knet_handle_compress() * to tell knet what type of compression to use * for this communiction */ struct knet_handle_compress_cfg { /** Compression library to use, bzip2 etc... */ char compress_model[16]; /** Threshold. Packets smaller than this will not be compressed */ uint32_t compress_threshold; /** Passed into the compression library as an indication of the level of compression to apply */ int compress_level; }; /** * knet_handle_compress * * @brief Set up packet compression * * knet_h - pointer to knet_handle_t * * knet_handle_compress_cfg - * pointer to a knet_handle_compress_cfg structure * * compress_model contains the model name. * See "compress_level" for the list of accepted values. * Setting the value to "none" disables compression. * * compress_threshold * tells the transmission thread to NOT compress * any packets that are smaller than the value * indicated. Default 100 bytes. * Set to 0 to reset to the default. * Set to 1 to compress everything. * Max accepted value is KNET_MAX_PACKET_SIZE. * * compress_level is the "level" parameter for most models: * zlib: 0 (no compression), 1 (minimal) .. 9 (max compression). * lz4: 1 (max compression)... 9 (fastest compression). * lz4hc: 1 (min compression) ... LZ4HC_MAX_CLEVEL (16) or LZ4HC_CLEVEL_MAX (12) * depending on the version of lz4hc libknet was built with. * lzma: 0 (minimal) .. 9 (max compression) * bzip2: 1 (minimal) .. 9 (max compression) * For lzo2 it selects the algorithm to use: * 1 : lzo1x_1_compress (default) * 11 : lzo1x_1_11_compress * 12 : lzo1x_1_12_compress * 15 : lzo1x_1_15_compress * 999: lzo1x_999_compress * Other values select the default algorithm. * Please refer to the documentation of the respective * compression library for guidance about setting this * value. * * Implementation notes: * - it is possible to enable/disable compression at any time. * - nodes can be using a different compression algorithm at any time. * - knet does NOT implement the compression algorithm directly. it relies * on external libraries for this functionality. Please read * the libraries man pages to figure out which algorithm/compression * level is best for the data you are planning to transmit. * * @return * knet_handle_compress returns * 0 on success * -1 on error and errno is set. EINVAL means that either the model or the * level are not supported. */ int knet_handle_compress(knet_handle_t knet_h, struct knet_handle_compress_cfg *knet_handle_compress_cfg); /** * Detailed stats for this knet handle as returned by knet_handle_get_stats() */ struct knet_handle_stats { /** Size of the structure. set this to sizeof(struct knet_handle_stats) before calling */ size_t size; /** Number of uncompressed packets sent */ uint64_t tx_uncompressed_packets; /** Number of compressed packets sent */ uint64_t tx_compressed_packets; /** Number of bytes sent (as if uncompressed, ie actual data bytes) */ uint64_t tx_compressed_original_bytes; /** Number of bytes sent on the wire after compression */ uint64_t tx_compressed_size_bytes; /** Average(mean) time take to compress transmitted packets */ uint64_t tx_compress_time_ave; /** Minimum time taken to compress transmitted packets */ uint64_t tx_compress_time_min; /** Maximum time taken to compress transmitted packets */ uint64_t tx_compress_time_max; /** Number of times the compression attempt failed for some reason */ uint64_t tx_failed_to_compress; /** Number of packets where the compressed size was no smaller than the original */ uint64_t tx_unable_to_compress; /** Number of compressed packets received */ uint64_t rx_compressed_packets; /** Number of bytes received - after decompression */ uint64_t rx_compressed_original_bytes; /** Number of compressed bytes received before decompression */ uint64_t rx_compressed_size_bytes; /** Average(mean) time take to decompress received packets */ uint64_t rx_compress_time_ave; /** Minimum time take to decompress received packets */ uint64_t rx_compress_time_min; /** Maximum time take to decompress received packets */ uint64_t rx_compress_time_max; /** Number of times decompression failed */ uint64_t rx_failed_to_decompress; /** Number of encrypted packets sent */ uint64_t tx_crypt_packets; /** Cumulative byte overhead of encrypted traffic */ uint64_t tx_crypt_byte_overhead; /** Average(mean) time take to encrypt packets in usecs */ uint64_t tx_crypt_time_ave; /** Minimum time take to encrypto packets in usecs */ uint64_t tx_crypt_time_min; /** Maximum time take to encrypto packets in usecs */ uint64_t tx_crypt_time_max; /** Number of encrypted packets received */ uint64_t rx_crypt_packets; /** Average(mean) time take to decrypt received packets */ uint64_t rx_crypt_time_ave; /** Minimum time take to decrypt received packets in usecs */ uint64_t rx_crypt_time_min; /** Maximum time take to decrypt received packets in usecs */ uint64_t rx_crypt_time_max; }; /** * knet_handle_get_stats * * @brief Get statistics for compression & crypto * * knet_h - pointer to knet_handle_t * * knet_handle_stats * pointer to a knet_handle_stats structure * * struct_size * size of knet_handle_stats structure to allow * for backwards compatibility. libknet will only * copy this much data into the stats structure * so that older callers will not get overflowed if * new fields are added. * * @return * 0 on success * -1 on error and errno is set. * */ int knet_handle_get_stats(knet_handle_t knet_h, struct knet_handle_stats *stats, size_t struct_size); /* * Tell knet_handle_clear_stats whether to clear just the handle stats * or all of them. */ #define KNET_CLEARSTATS_HANDLE_ONLY 1 #define KNET_CLEARSTATS_HANDLE_AND_LINK 2 /** * knet_handle_clear_stats * * @brief Clear knet stats, link and/or handle * * knet_h - pointer to knet_handle_t * * clear_option - Which stats to clear, must be one of * * KNET_CLEARSTATS_HANDLE_ONLY or * KNET_CLEARSTATS_HANDLE_AND_LINK * * @return * 0 on success * -1 on error and errno is set. * */ int knet_handle_clear_stats(knet_handle_t knet_h, int clear_option); /** * Structure returned from get_crypto_list() containing * information about the installed cryptographic systems */ struct knet_crypto_info { /** Name of the crypto library/ openssl, nss,etc .. */ const char *name; /** Properties - currently unused */ uint8_t properties; /** Currently unused padding */ char pad[256]; }; /** * knet_get_crypto_list * * @brief Get a list of supported crypto libraries * * crypto_list - array of struct knet_crypto_info * * If NULL then only the number of structs is returned in crypto_list_entries * to allow the caller to allocate sufficient space. * libknet does not allow more than 256 crypto methods at the moment. * it is safe to allocate 256 structs to avoid calling * knet_get_crypto_list twice. * * crypto_list_entries - returns the number of structs in crypto_list * * @return * knet_get_crypto_list returns * 0 on success * -1 on error and errno is set. */ int knet_get_crypto_list(struct knet_crypto_info *crypto_list, size_t *crypto_list_entries); /** * Structure returned from get_compress_list() containing * information about the installed compression systems */ struct knet_compress_info { /** Name of the compression type bzip2, lz4, etc.. */ const char *name; /** Properties - currently unused */ uint8_t properties; /** Currently unused padding */ char pad[256]; }; /** * knet_get_compress_list * * @brief Get a list of support compression types * * compress_list - array of struct knet_compress_info * * If NULL then only the number of structs is returned in compress_list_entries * to allow the caller to allocate sufficient space. * libknet does not allow more than 256 compress methods at the moment. * it is safe to allocate 256 structs to avoid calling * knet_get_compress_list twice. * * compress_list_entries - returns the number of structs in compress_list * * @return * knet_get_compress_list returns * 0 on success * -1 on error and errno is set. */ int knet_get_compress_list(struct knet_compress_info *compress_list, size_t *compress_list_entries); /** * knet_handle_enable_onwire_ver_notify * * @brief install a callback to receive onwire changes * * knet_h - pointer to knet_handle_t * * onwire_ver_notify_fn_private_data * void pointer to data that can be used to identify * the callback. * * onwire_ver_notify_fn * is a callback function that is invoked every time * an onwire version change is detected. * The function allows libknet to notify the user * of onwire version changes. * onwire_min_ver - minimum onwire version supported * onwire_max_ver - maximum onwire version supported * onwire_ver - currently onwire version in use * This function MUST NEVER block or add substantial delays. * * NOTE: the callback function will be invoked upon install to * immediately notify the user of the current configuration. * During startup, it is safer to use onwire_min_ver and * onwire_ver on subsequent calls. * * @return * knet_handle_enable_onwire_ver_notify returns * 0 on success * -1 on error and errno is set. */ int knet_handle_enable_onwire_ver_notify(knet_handle_t knet_h, void *onwire_ver_notify_fn_private_data, void (*onwire_ver_notify_fn) ( void *private_data, uint8_t onwire_min_ver, uint8_t onwire_max_ver, uint8_t onwire_ver)); /** * knet_handle_get_onwire_ver * * @brief get onwire protocol version information * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * onwire_min_ver - minimum onwire version supported by local node. * this value is set to 0 for remote nodes. * * onwire_max_ver - maximum onwire version supported by local or * remote node. * * onwire_ver - currently onwire version in use by local or * remote node. * * @return * knet_handle_get_onwire_ver returns * 0 on success * -1 on error and errno is set. */ int knet_handle_get_onwire_ver(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t *onwire_min_ver, uint8_t *onwire_max_ver, uint8_t *onwire_ver); /** * knet_handle_set_onwire_ver * * @brief force onwire protocol version * * knet_h - pointer to knet_handle_t * * onwire_ver - onwire version to use. * reset to 0 to allow knet to detect * automatically the highest version. * * @return * knet_handle_get_onwire_ver returns * 0 on success * -1 on error and errno is set. */ int knet_handle_set_onwire_ver(knet_handle_t knet_h, uint8_t onwire_ver); /* * defrag buffer configuration defaults */ #define KNET_MIN_DEFRAG_BUFS_DEFAULT 32 #define KNET_MAX_DEFRAG_BUFS_DEFAULT 1024 #define KNET_SHRINK_THRESHOLD_DEFAULT 25 /** * reclaim_policy for defrag buffers */ typedef enum { RECLAIM_POLICY_AVERAGE = 0, RECLAIM_POLICY_ABSOLUTE = 1 /* default */ } defrag_bufs_reclaim_policy_t; /** * knet_handle_get_host_defrag_bufs * * @brief Return the defrag buffers parameters for hosts * * knet_h - pointer to knet_handle_t * * min_defrag_bufs - minimum defrag buffers for each host * * max_defrag_bufs - maximum defrag buffers for each host * * shrink_threshold - define buffer usage threshold in % * below which buffers will be shrunk. * This is measured over the last * usage_samples_timespan, as an * average of usage_samples. * * reclaim_policy - define how % threshold is calculated. * * @return * knet_handle_set_host_defrag_bufs returns * 0 on success * -1 on error and errno is set. */ int knet_handle_get_host_defrag_bufs(knet_handle_t knet_h, uint16_t *min_defrag_bufs, uint16_t *max_defrag_bufs, uint8_t *shrink_threshold, defrag_bufs_reclaim_policy_t *reclaim_policy); /** * knet_handle_set_host_defrag_bufs * * @brief configure defrag buffers parameters per host * * knet_h - pointer to knet_handle_t * * min_defrag_bufs - minimum defrag buffers for each host, * This should be a power of 2. * * max_defrag_bufs - maximum defrag buffers for each host, * This should be a power of 2. * * shrink_threshold - define buffer usage threshold in % * below which buffers will be shrunk. * This is measured over the last * usage_samples_timespan, as an * average of usage_samples. * Only values less than or equal to 50% are accepted. * * reclaim_policy - define how % threshold is calculated. * * @note The defrag buffer parameters are global to a handle * but the buffers themselves are allocated and shrunk per-host. * * @return * knet_handle_set_host_defrag_bufs returns * 0 on success * -1 on error and errno is set. */ int knet_handle_set_host_defrag_bufs(knet_handle_t knet_h, uint16_t min_defrag_bufs, uint16_t max_defrag_bufs, uint8_t shrink_threshold, defrag_bufs_reclaim_policy_t reclaim_policy); /* * host structs/API calls */ /** * knet_host_add * * @brief Add a new host ID to knet * * 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(3)) * * @return * knet_host_add returns: * 0 on success * -1 on error and errno is set. */ int knet_host_add(knet_handle_t knet_h, knet_node_id_t host_id); /** * knet_host_remove * * @brief Remove a host ID from knet * * 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(3)) * * @return * knet_host_remove returns: * 0 on success * -1 on error and errno is set. */ int knet_host_remove(knet_handle_t knet_h, knet_node_id_t host_id); /** * knet_host_set_name * * @brief Set the name of a knet host * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * name - this name will be used for pretty logging and eventually * search for hosts (see also knet_handle_host_get_name(2) and knet_handle_host_get_id(3)). * Only up to KNET_MAX_HOST_LEN - 1 bytes will be accepted and * name has to be unique for each host. * * @return * knet_host_set_name returns: * 0 on success * -1 on error and errno is set. */ int knet_host_set_name(knet_handle_t knet_h, knet_node_id_t host_id, const char *name); /** * knet_host_get_name_by_host_id * * @brief Get the name of a host given its ID * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * name - pointer to a preallocated buffer of at least size KNET_MAX_HOST_LEN * where the current host name will be stored * (as set by knet_host_set_name or default by knet_host_add) * * @return * knet_host_get_name_by_host_id returns: * 0 on success * -1 on error and errno is set (name is left untouched) */ int knet_host_get_name_by_host_id(knet_handle_t knet_h, knet_node_id_t host_id, char *name); /** * knet_host_get_id_by_host_name * * @brief Get the ID of a host given its name * * knet_h - pointer to knet_handle_t * * name - name to lookup, max len KNET_MAX_HOST_LEN * * host_id - where to store the result * * @return * knet_host_get_id_by_host_name returns: * 0 on success * -1 on error and errno is set. */ int knet_host_get_id_by_host_name(knet_handle_t knet_h, const char *name, knet_node_id_t *host_id); /** * knet_host_get_host_list * * @brief Get a list of hosts known to knet * * knet_h - pointer to knet_handle_t * * host_ids - array of at lest KNET_MAX_HOST size * * host_ids_entries - * number of entries writted in host_ids * * @return * knet_host_get_host_list returns * 0 on success * -1 on error and errno is set. */ int knet_host_get_host_list(knet_handle_t knet_h, knet_node_id_t *host_ids, size_t *host_ids_entries); /* * define switching policies */ #define KNET_LINK_POLICY_PASSIVE 0 #define KNET_LINK_POLICY_ACTIVE 1 #define KNET_LINK_POLICY_RR 2 /** * knet_host_set_policy * * @brief Set the switching policy for a host's links * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * policy - there are currently 3 kind of simple switching policies * based on link configuration. * KNET_LINK_POLICY_PASSIVE - the active link with the highest * priority (highest number) will be used. * if one or more active links share * the same priority, the one with * lowest link_id will be used. * * KNET_LINK_POLICY_ACTIVE - all active links will be used * simultaneously to send traffic. * link priority is ignored. * * KNET_LINK_POLICY_RR - round-robin policy, every packet * will be send on a different active * link. * * @return * knet_host_set_policy returns * 0 on success * -1 on error and errno is set. */ int knet_host_set_policy(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t policy); /** * knet_host_get_policy * * @brief Get the switching policy for a host's links * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * policy - will contain the current configured switching policy. * Default is passive when creating a new host. * * @return * knet_host_get_policy returns * 0 on success * -1 on error and errno is set. */ int knet_host_get_policy(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t *policy); /** * knet_host_enable_status_change_notify * * @brief Install a callback to get host status change events * * knet_h - pointer to knet_handle_t * * host_status_change_notify_fn_private_data - * void pointer to data that can be used to identify * the callback * * host_status_change_notify_fn - * is a callback function that is invoked every time * there is a change in the host status. * host status is identified by: * - reachable, this host can send/receive data to/from host_id * - remote, 0 if the host_id is connected locally or 1 if * the there is one or more knet host(s) in between. * NOTE: re-switching is NOT currently implemented, * but this is ready for future and can avoid * an API/ABI breakage later on. * - external, 0 if the host_id is configured locally or 1 if * it has been added from remote nodes config. * NOTE: dynamic topology is NOT currently implemented, * but this is ready for future and can avoid * an API/ABI breakage later on. * This function MUST NEVER block or add substantial delays. * * @return * knet_host_status_change_notify returns * 0 on success * -1 on error and errno is set. */ int knet_host_enable_status_change_notify(knet_handle_t knet_h, void *host_status_change_notify_fn_private_data, void (*host_status_change_notify_fn) ( void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external)); /* * define host status structure for quick lookup * struct is in flux as more stats will be added soon * * reachable host_id can be seen either directly connected * or via another host_id * * remote 0 = node is connected locally, 1 is visible via * via another host_id * * external 0 = node is configured/known locally, * 1 host_id has been received via another host_id */ /** * status of a knet host, returned from knet_host_get_status() */ struct knet_host_status { /** Whether the host is currently reachable */ uint8_t reachable; /** Whether the host is a remote node (not currently implemented) */ uint8_t remote; /** Whether the host is external (not currently implemented) */ uint8_t external; /* add host statistics */ }; /** * knet_host_get_status * * @brief Get the status of a host * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * status - pointer to knet_host_status struct * * @return * knet_handle_pmtud_get returns * 0 on success * -1 on error and errno is set. */ int knet_host_get_status(knet_handle_t knet_h, knet_node_id_t host_id, struct knet_host_status *status); /* * link structs/API calls * * every host allocated/managed by knet_host_* has * KNET_MAX_LINK structures to define the network * paths that connect 2 hosts. * * Each link is identified by a link_id that has a * values between 0 and KNET_MAX_LINK - 1. * * KNOWN LIMITATIONS: * * - let's assume the scenario where two hosts are connected * with any number of links. link_id must match on both sides. * If host_id 0 link_id 0 is configured to connect IP1 to IP2 and * host_id 0 link_id 1 is configured to connect IP3 to IP4, * host_id 1 link_id 0 _must_ connect IP2 to IP1 and likewise * host_id 1 link_id 1 _must_ connect IP4 to IP3. * We might be able to lift this restriction in future, by using * other data to determine src/dst link_id, but for now, deal with it. */ /* * commodity functions to convert strings to sockaddr and viceversa */ /** * knet_strtoaddr * * @brief Convert a hostname string to an address * * host - IPaddr/hostname to convert * be aware only the first IP address will be returned * in case a hostname resolves to multiple IP * * port - port to connect to * * ss - sockaddr_storage where to store the converted data * * sslen - len of the sockaddr_storage * * @return * knet_strtoaddr returns same error codes as getaddrinfo * */ int knet_strtoaddr(const char *host, const char *port, struct sockaddr_storage *ss, socklen_t sslen); /** * knet_addrtostr * * @brief Convert an address to a host name * * ss - sockaddr_storage to convert * * sslen - len of the sockaddr_storage * * host - IPaddr/hostname where to store data * (recommended size: KNET_MAX_HOST_LEN) * * port - port buffer where to store data * (recommended size: KNET_MAX_PORT_LEN) * * @return * knet_strtoaddr returns same error codes as getnameinfo */ int knet_addrtostr(const struct sockaddr_storage *ss, socklen_t sslen, char *addr_buf, size_t addr_buf_size, char *port_buf, size_t port_buf_size); #define KNET_TRANSPORT_LOOPBACK 0 #define KNET_TRANSPORT_UDP 1 #define KNET_TRANSPORT_SCTP 2 #define KNET_MAX_TRANSPORTS UINT8_MAX /* * The Loopback transport is only valid for connections to localhost, the host * with the same node_id specified in knet_handle_new(). Only one link of this * type is allowed. Data sent down a LOOPBACK link will be copied directly from * the knet send datafd to the knet receive datafd so the application must be set * up to take data from that socket at least as often as it is sent or deadlocks * could occur. If used, a LOOPBACK link must be the only link configured to the * local host. */ /** * Transport information returned from knet_get_transport_list() */ struct knet_transport_info { /** Transport name. UDP, SCTP, etc... */ const char *name; /** value that can be used for knet_link_set_config() */ uint8_t id; /** currently unused */ uint8_t properties; /** currently unused */ char pad[256]; }; /** * knet_get_transport_list * * @brief Get a list of the transports support by this build of knet * * transport_list - an array of struct transport_info that must be * at least of size struct transport_info * KNET_MAX_TRANSPORTS * * transport_list_entries - pointer to a size_t where to store how many transports * are available in this build of libknet. * * @return * knet_get_transport_list returns * 0 on success * -1 on error and errno is set. */ int knet_get_transport_list(struct knet_transport_info *transport_list, size_t *transport_list_entries); /** * knet_get_transport_name_by_id * * @brief Get a transport name from its ID number * * transport - one of the KNET_TRANSPORT_xxx constants * * @return * knet_get_transport_name_by_id returns: * * @retval pointer to the name on success or * @retval NULL on error and errno is set. */ const char *knet_get_transport_name_by_id(uint8_t transport); /** * knet_get_transport_id_by_name * * @brief Get a transport ID from its name * * name - transport name (UDP/SCTP/etc) * * @return * knet_get_transport_name_by_id returns: * * @retval KNET_MAX_TRANSPORTS on error and errno is set accordingly * @retval KNET_TRANSPORT_xxx on success. */ uint8_t knet_get_transport_id_by_name(const char *name); #define KNET_TRANSPORT_DEFAULT_RECONNECT_INTERVAL 1000 /** * knet_handle_set_transport_reconnect_interval * * @brief Set the interval between transport attempts to reconnect a failed link * * knet_h - pointer to knet_handle_t * * msecs - milliseconds * * @return * knet_handle_set_transport_reconnect_interval returns * 0 on success * -1 on error and errno is set. */ int knet_handle_set_transport_reconnect_interval(knet_handle_t knet_h, uint32_t msecs); /** * knet_handle_get_transport_reconnect_interval * * @brief Get the interval between transport attempts to reconnect a failed link * * knet_h - pointer to knet_handle_t * * msecs - milliseconds * * @return * knet_handle_get_transport_reconnect_interval returns * 0 on success * -1 on error and errno is set. */ int knet_handle_get_transport_reconnect_interval(knet_handle_t knet_h, uint32_t *msecs); /** * knet_link_set_config * * @brief Configure the link to a host * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * transport - one of the KNET_TRANSPORT_xxx constants * * src_addr - sockaddr_storage that can be either IPv4 or IPv6 * * dst_addr - sockaddr_storage that can be either IPv4 or IPv6 * this can be null if we don't know the incoming * IP address/port and the link will remain quiet * till the node on the other end will initiate a * connection * * flags - KNET_LINK_FLAG_* * * @return * knet_link_set_config returns * 0 on success * -1 on error and errno is set. */ int knet_link_set_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t transport, struct sockaddr_storage *src_addr, struct sockaddr_storage *dst_addr, uint64_t flags); /** * knet_link_get_config * * @brief Get the link configutation information * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * transport - see knet_link_set_config(3) * * src_addr - sockaddr_storage that can be either IPv4 or IPv6 * * dst_addr - sockaddr_storage that can be either IPv4 or IPv6 * * dynamic - 0 if dst_addr is static or 1 if dst_addr is dynamic. * In case of 1, dst_addr can be NULL and it will be left * untouched. * * flags - KNET_LINK_FLAG_* * * @return * knet_link_get_config returns * 0 on success. * -1 on error and errno is set. */ int knet_link_get_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t *transport, struct sockaddr_storage *src_addr, struct sockaddr_storage *dst_addr, uint8_t *dynamic, uint64_t *flags); /** * knet_link_clear_config * * @brief Clear link information and disconnect the link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * @return * knet_link_clear_config returns * 0 on success. * -1 on error and errno is set. */ int knet_link_clear_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id); /* * Access lists management for open links * see also knet_handle_enable_access_lists(3) */ /** * check_type_t * @brief address type enum for knet access lists * * CHECK_TYPE_ADDRESS is the equivalent of a single entry / IP address. * for example: 10.1.9.3 * and the entry is stored in ss1. ss2 can be NULL. * * CHECK_TYPE_MASK is used to configure network/netmask. * for example: 192.168.0.0/24 * the network is stored in ss1 and the netmask in ss2. * * CHECK_TYPE_RANGE defines a value / range of ip addresses. * for example: 172.16.0.1-172.16.0.10 * the start is stored in ss1 and the end in ss2. * * Please be aware that the above examples refer only to IP based protocols. * Other protocols might use ss1 and ss2 in slightly different ways. * At the moment knet only supports IP based protocol, though that might change * in the future. */ typedef enum { CHECK_TYPE_ADDRESS, CHECK_TYPE_MASK, CHECK_TYPE_RANGE } check_type_t; /** * check_acceptreject_t * * @brief enum for accept/reject in knet access lists * * accept or reject incoming packets defined in the access list entry */ typedef enum { CHECK_ACCEPT, CHECK_REJECT } check_acceptreject_t; /** * knet_link_add_acl * * @brief Add access list entry to an open link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * ss1 / ss2 / type / acceptreject - see typedef definitions for details * * IMPORTANT: the order in which access lists are added is critical and it * is left to the user to add them in the right order. knet * will not attempt to logically sort them. * * For example: * 1 - accept from 10.0.0.0/8 * 2 - reject from 10.0.0.1/32 * * is not the same as: * * 1 - reject from 10.0.0.1/32 * 2 - accept from 10.0.0.0/8 * * In the first example, rule number 2 will never match because * packets from 10.0.0.1 will be accepted by rule number 1. * * @return * knet_link_add_acl returns * 0 on success. * -1 on error and errno is set. */ int knet_link_add_acl(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); /** * knet_link_insert_acl * * @brief Insert access list entry to an open link at given index * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * index - insert at position "index" where 0 is the first entry and -1 * appends to the current list. * * ss1 / ss2 / type / acceptreject - see typedef definitions for details * * @return * knet_link_insert_acl returns * 0 on success. * -1 on error and errno is set. */ int knet_link_insert_acl(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); /** * knet_link_rm_acl * * @brief Remove access list entry from an open link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * ss1 / ss2 / type / acceptreject - see typedef definitions for details * * IMPORTANT: the data passed to this API call must match exactly that passed * to knet_link_add_acl(3). * * @return * knet_link_rm_acl returns * 0 on success. * -1 on error and errno is set. */ int knet_link_rm_acl(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); /** * knet_link_clear_acl * * @brief Remove all access list entries from an open link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * @return * knet_link_clear_acl returns * 0 on success. * -1 on error and errno is set. */ int knet_link_clear_acl(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id); /** * knet_link_set_enable * * @brief Enable traffic on a link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * enabled - 0 disable the link, 1 enable the link * * @return * knet_link_set_enable returns * 0 on success * -1 on error and errno is set. */ int knet_link_set_enable(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, unsigned int enabled); /** * knet_link_get_enable * * @brief Find out whether a link is enabled or not * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * enabled - 0 disable the link, 1 enable the link * * @return * knet_link_get_enable returns * 0 on success * -1 on error and errno is set. */ int knet_link_get_enable(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, unsigned int *enabled); #define KNET_LINK_DEFAULT_PING_INTERVAL 1000 /* 1 second */ #define KNET_LINK_DEFAULT_PING_TIMEOUT 2000 /* 2 seconds */ #define KNET_LINK_DEFAULT_PING_PRECISION 2048 /* samples */ /** * knet_link_set_ping_timers * * @brief Set the ping timers for a link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * interval - specify the ping interval in milliseconds. * * timeout - if no pong is received within this time, * the link is declared dead, in milliseconds. * NOTE: in future it will be possible to set timeout to 0 * for an autocalculated timeout based on interval, pong_count * and latency. The API already accept 0 as value and it will * return ENOSYS / -1. Once the automatic calculation feature * will be implemented, this call will only return EINVAL * for incorrect values. * * precision - how many values of latency are used to calculate * the average link latency (see also knet_link_get_status(3)) * * @return * knet_link_set_ping_timers returns * 0 on success * -1 on error and errno is set. */ int knet_link_set_ping_timers(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, time_t interval, time_t timeout, unsigned int precision); /** * knet_link_get_ping_timers * * @brief Get the ping timers for a link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * interval - ping interval * * timeout - if no pong is received within this time, * the link is declared dead * * precision - how many values of latency are used to calculate * the average link latency (see also knet_link_get_status(3)) * * @return * knet_link_get_ping_timers returns * 0 on success * -1 on error and errno is set. */ int knet_link_get_ping_timers(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, time_t *interval, time_t *timeout, unsigned int *precision); #define KNET_LINK_DEFAULT_PONG_COUNT 5 /** * knet_link_set_pong_count * * @brief Set the pong count for a link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * pong_count - how many valid ping/pongs before a link is marked UP. * default: 5, value should be > 0 * * @return * knet_link_set_pong_count returns * 0 on success * -1 on error and errno is set. */ int knet_link_set_pong_count(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t pong_count); /** * knet_link_get_pong_count * * @brief Get the pong count for a link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * pong_count - how many valid ping/pongs before a link is marked UP. * default: 5, value should be > 0 * * @return * knet_link_get_pong_count returns * 0 on success * -1 on error and errno is set. */ int knet_link_get_pong_count(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t *pong_count); /** * knet_link_set_priority * * @brief Set the priority for a link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * priority - specify the switching priority for this link * see also knet_host_set_policy * * @return * knet_link_set_priority returns * 0 on success * -1 on error and errno is set. */ int knet_link_set_priority(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t priority); /** * knet_link_get_priority * * @brief Get the priority for a link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * priority - gather the switching priority for this link * see also knet_host_set_policy * * @return * knet_link_get_priority returns * 0 on success * -1 on error and errno is set. */ int knet_link_get_priority(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t *priority); /** * knet_link_get_link_list * * @brief Get a list of links connecting a host * * knet_h - pointer to knet_handle_t * * link_ids - array of at lest KNET_MAX_LINK size * with the list of configured links for a certain host. * * link_ids_entries - * number of entries contained in link_ids * * @return * knet_link_get_link_list returns * 0 on success * -1 on error and errno is set. */ int knet_link_get_link_list(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t *link_ids, size_t *link_ids_entries); /* * define link status structure for quick lookup * * src/dst_{ipaddr,port} strings are filled by * getnameinfo(3) when configuring the link. * if the link is dynamic (see knet_link_set_config(3)) * 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 pings to us. * * pong_last if 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. * * knet_link_stats structure that contains details statistics for the link */ #define MAX_LINK_EVENTS 16 /** * Stats for a knet link * returned from knet_link_get_status() as part of a knet_link_status structure * link stats are 'onwire', ie they indicate the number of actual bytes/packets * sent including overheads, not just data packets. */ struct knet_link_stats { /** Number of data packets sent */ uint64_t tx_data_packets; /** Number of data packets received */ uint64_t rx_data_packets; /** Number of data bytes sent */ uint64_t tx_data_bytes; /** Number of data bytes received */ uint64_t rx_data_bytes; /** Number of ping packets sent */ uint64_t rx_ping_packets; /** Number of ping packets received */ uint64_t tx_ping_packets; /** Number of ping bytes sent */ uint64_t rx_ping_bytes; /** Number of ping bytes received */ uint64_t tx_ping_bytes; /** Number of pong packets sent */ uint64_t rx_pong_packets; /** Number of pong packets received */ uint64_t tx_pong_packets; /** Number of pong bytes sent */ uint64_t rx_pong_bytes; /** Number of pong bytes received */ uint64_t tx_pong_bytes; /** Number of pMTU packets sent */ uint64_t rx_pmtu_packets; /** Number of pMTU packets received */ uint64_t tx_pmtu_packets; /** Number of pMTU bytes sent */ uint64_t rx_pmtu_bytes; /** Number of pMTU bytes received */ uint64_t tx_pmtu_bytes; /* These are only filled in when requested ie. they are not collected in realtime */ /** Total of all packets sent */ uint64_t tx_total_packets; /** Total of all packets received */ uint64_t rx_total_packets; /** Total number of bytes sent */ uint64_t tx_total_bytes; /** Total number of bytes received */ uint64_t rx_total_bytes; /** Total number of errors that occurred while sending */ uint64_t tx_total_errors; /** Total number of retries that occurred while sending */ uint64_t tx_total_retries; /** Total number of errors that occurred while sending pMTU packets */ uint32_t tx_pmtu_errors; /** Total number of retries that occurred while sending pMTU packets */ uint32_t tx_pmtu_retries; /** Total number of errors that occurred while sending ping packets */ uint32_t tx_ping_errors; /** Total number of retries that occurred while sending ping packets */ uint32_t tx_ping_retries; /** Total number of errors that occurred while sending pong packets */ uint32_t tx_pong_errors; /** Total number of retries that occurred while sending pong packets */ uint32_t tx_pong_retries; /** Total number of errors that occurred while sending data packets */ uint32_t tx_data_errors; /** Total number of retries that occurred while sending data packets */ uint32_t tx_data_retries; /** Minimum latency measured in usecs */ uint32_t latency_min; /** Maximum latency measured in usecs */ uint32_t latency_max; /** Average(mean) latency measured in usecs */ uint32_t latency_ave; /** Number of samples used to calculate latency */ uint32_t latency_samples; /** How many times the link has gone down */ uint32_t down_count; /** How many times the link has come up */ uint32_t up_count; /** * A circular buffer of time_t structs collecting the history * of up events on this link. * The index indicates current/last event. * it is safe to walk back the history by decreasing the index */ time_t last_up_times[MAX_LINK_EVENTS]; /** * A circular buffer of time_t structs collecting the history * of down events on this link. * The index indicates current/last event. * it is safe to walk back the history by decreasing the index */ time_t last_down_times[MAX_LINK_EVENTS]; /** Index of last element in the last_up_times[] array */ int8_t last_up_time_index; /** Index of last element in the last_down_times[] array */ int8_t last_down_time_index; /* Always add new stats at the end */ }; /** * Status of a knet link as returned from knet_link_get_status() */ struct knet_link_status { /** Size of the structure for ABI checking, set this to sizeof(knet_link_status) before calling knet_link_get_status() */ size_t size; /** Local IP address as a string*/ char src_ipaddr[KNET_MAX_HOST_LEN]; /** Local IP port as a string */ char src_port[KNET_MAX_PORT_LEN]; /** Remote IP address as a string */ char dst_ipaddr[KNET_MAX_HOST_LEN]; /** Remote IP port as a string*/ char dst_port[KNET_MAX_PORT_LEN]; /** Link is configured and admin enabled for traffic */ uint8_t enabled; /** Link is connected for data (local view) */ uint8_t connected; /** Link has been activated by remote dynip */ uint8_t dynconnected; /** Timestamp of the past pong received */ struct timespec pong_last; /** Currently detected MTU on this link */ unsigned int mtu; /** * Contains the size of the IP protocol, knet headers and * crypto headers (if configured). This value is filled in * ONLY after the first PMTUd run on that given link, * and can change if link configuration or crypto configuration * changes at runtime. * WARNING: in general mtu + proto_overhead might or might * not match the output of ifconfig mtu due to crypto * requirements to pad packets to some specific boundaries. */ unsigned int proto_overhead; /** Link statistics */ struct knet_link_stats stats; }; /** * knet_link_get_status * * @brief Get the status (and statistics) for a link * * knet_h - pointer to knet_handle_t * * host_id - see knet_host_add(3) * * link_id - see knet_link_set_config(3) * * status - pointer to knet_link_status struct * * struct_size - max size of knet_link_status - allows library to * add fields without ABI change. Returned structure * will be truncated to this length and .size member * indicates the full size. * * @return * 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, knet_node_id_t host_id, uint8_t link_id, struct knet_link_status *status, size_t struct_size); /** * knet_link_enable_status_change_notify * * @brief Install a callback to get a link status change events * * knet_h - pointer to knet_handle_t * * host_status_change_notify_fn_private_data - * void pointer to data that can be used to identify * the callback * * host_status_change_notify_fn - * is a callback function that is invoked every time * there is a change in a link status. * host status is identified by: * - connected, 0 if the link has been disconnected, 1 if the link * is connected. * - remote, 0 if the host_id is connected locally or 1 if * the there is one or more knet host(s) in between. * NOTE: re-switching is NOT currently implemented, * but this is ready for future and can avoid * an API/ABI breakage later on. * - external, 0 if the host_id is configured locally or 1 if * it has been added from remote nodes config. * NOTE: dynamic topology is NOT currently implemented, * but this is ready for future and can avoid * an API/ABI breakage later on. * This function MUST NEVER block or add substantial delays. * * @return * knet_host_status_change_notify returns * 0 on success * -1 on error and errno is set. */ int knet_link_enable_status_change_notify(knet_handle_t knet_h, void *link_status_change_notify_fn_private_data, void (*link_status_change_notify_fn) ( void *private_data, knet_node_id_t host_id, uint8_t link_id, uint8_t connected, uint8_t remote, uint8_t external)); /* * logging structs/API calls */ /* * libknet is composed of several subsystems. In order * to easily distinguish log messages coming from different * places, each subsystem has its own ID. * * 0-19 config/management * 20-39 internal threads * 40-59 transports * 60-69 crypto implementations */ #define KNET_SUB_COMMON 0 /* common.c */ #define KNET_SUB_HANDLE 1 /* handle.c alloc/dealloc config changes */ #define KNET_SUB_HOST 2 /* host add/del/modify */ #define KNET_SUB_LISTENER 3 /* listeners add/del/modify... */ #define KNET_SUB_LINK 4 /* link add/del/modify */ #define KNET_SUB_TRANSPORT 5 /* Transport common */ #define KNET_SUB_CRYPTO 6 /* crypto.c config generic layer */ #define KNET_SUB_COMPRESS 7 /* compress.c config generic layer */ #define KNET_SUB_FILTER 19 /* allocated for users to log from dst_filter */ #define KNET_SUB_DSTCACHE 20 /* switching thread (destination cache handling) */ #define KNET_SUB_HEARTBEAT 21 /* heartbeat thread */ #define KNET_SUB_PMTUD 22 /* Path MTU Discovery thread */ #define KNET_SUB_TX 23 /* send to link thread */ #define KNET_SUB_RX 24 /* recv from link thread */ #define KNET_SUB_TRANSP_BASE 40 /* Base log level for transports */ #define KNET_SUB_TRANSP_LOOPBACK (KNET_SUB_TRANSP_BASE + KNET_TRANSPORT_LOOPBACK) #define KNET_SUB_TRANSP_UDP (KNET_SUB_TRANSP_BASE + KNET_TRANSPORT_UDP) #define KNET_SUB_TRANSP_SCTP (KNET_SUB_TRANSP_BASE + KNET_TRANSPORT_SCTP) #define KNET_SUB_NSSCRYPTO 60 /* crypto_nss.c */ #define KNET_SUB_OPENSSLCRYPTO 61 /* crypto_openssl.c */ #define KNET_SUB_GCRYPTCRYPTO 62 /* crypto_gcrypt.c */ #define KNET_SUB_ZLIBCOMP 70 /* compress_zlib.c */ #define KNET_SUB_LZ4COMP 71 /* compress_lz4.c */ #define KNET_SUB_LZ4HCCOMP 72 /* compress_lz4.c */ #define KNET_SUB_LZO2COMP 73 /* compress_lzo.c */ #define KNET_SUB_LZMACOMP 74 /* compress_lzma.c */ #define KNET_SUB_BZIP2COMP 75 /* compress_bzip2.c */ #define KNET_SUB_ZSTDCOMP 76 /* compress_zstd.c */ #define KNET_SUB_UNKNOWN UINT8_MAX - 1 #define KNET_MAX_SUBSYSTEMS UINT8_MAX /* * Convert between subsystem IDs and names */ /** * knet_log_get_subsystem_name * * @brief Get a logging system name from its numeric ID * * @return * returns internal name of the subsystem or "common" */ const char *knet_log_get_subsystem_name(uint8_t subsystem); /** * knet_log_get_subsystem_id * * @brief Get a logging system ID from its name * * @return * returns internal ID of the subsystem or KNET_SUB_COMMON */ uint8_t knet_log_get_subsystem_id(const char *name); /* * 4 log levels are enough for everybody */ #define KNET_LOG_ERR 0 /* unrecoverable errors/conditions */ #define KNET_LOG_WARN 1 /* recoverable errors/conditions */ #define KNET_LOG_INFO 2 /* info, link up/down, config changes.. */ #define KNET_LOG_DEBUG 3 /* * Convert between log level values and names */ /** * knet_log_get_loglevel_name * * @brief Get a logging level name from its numeric ID * * @return * returns internal name of the log level or "ERROR" for unknown values */ const char *knet_log_get_loglevel_name(uint8_t level); /** * knet_log_get_loglevel_id * * @brief Get a logging level ID from its name * * @return * returns internal log level ID or KNET_LOG_ERR for invalid names */ uint8_t knet_log_get_loglevel_id(const char *name); /* * every log message is composed by a text message * and message level/subsystem IDs. * In order to make debugging easier it is possible to send those packets * straight to stdout/stderr (see knet_bench.c stdout option). */ #define KNET_MAX_LOG_MSG_SIZE 254 #if KNET_MAX_LOG_MSG_SIZE > PIPE_BUF #error KNET_MAX_LOG_MSG_SIZE cannot be bigger than PIPE_BUF for guaranteed system atomic writes #endif /** * Structure of a log message sent to the logging fd */ struct knet_log_msg { /** Text of the log message */ char msg[KNET_MAX_LOG_MSG_SIZE]; /** Subsystem that sent this message. KNET_SUB_* */ uint8_t subsystem; /** Logging level of this message. KNET_LOG_* */ uint8_t msglevel; /** Pointer to the handle generating the log message */ knet_handle_t knet_h; }; /** * knet_log_set_loglevel * * @brief Set the logging level for a subsystem * * knet_h - same as above * * subsystem - same as above * * level - same as above * * knet_log_set_loglevel allows fine control of log levels by subsystem. * See also knet_handle_new for defaults. * * @return * knet_log_set_loglevel returns * 0 on success * -1 on error and errno is set. */ int knet_log_set_loglevel(knet_handle_t knet_h, uint8_t subsystem, uint8_t level); /** * knet_log_get_loglevel * * @brief Get the logging level for a subsystem * * knet_h - same as above * * subsystem - same as above * * level - same as above * * @return * knet_log_get_loglevel returns * 0 on success * -1 on error and errno is set. */ int knet_log_get_loglevel(knet_handle_t knet_h, uint8_t subsystem, uint8_t *level); #endif diff --git a/libknet/libknet.pc.in b/libknet/libknet.pc.in index 4114a609..66e02f42 100644 --- a/libknet/libknet.pc.in +++ b/libknet/libknet.pc.in @@ -1,19 +1,19 @@ # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Author: Federico Simoncelli # # This software licensed under LGPL-2.0+ # prefix=@prefix@ exec_prefix=${prefix} libdir=@libdir@ includedir=${prefix}/include Name: libknet Version: @VERSION@ Description: kronosnet library Requires: Libs: -L${libdir} -lknet Cflags: -I${includedir} diff --git a/libknet/libknet_exported_syms b/libknet/libknet_exported_syms index de770ce9..a55cf2ef 100644 --- a/libknet/libknet_exported_syms +++ b/libknet/libknet_exported_syms @@ -1,15 +1,15 @@ # Version and symbol export for libknet.so # -# Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under LGPL-2.0+ # LIBKNET { global: knet_*; local: *; }; diff --git a/libknet/links.c b/libknet/links.c index bad2e057..ef789f50 100644 --- a/libknet/links.c +++ b/libknet/links.c @@ -1,1572 +1,1572 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include "netutils.h" #include "internals.h" #include "logging.h" #include "links.h" #include "transports.h" #include "host.h" #include "threads_common.h" #include "links_acl.h" int _link_updown(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, unsigned int enabled, unsigned int connected, unsigned int lock_stats) { struct knet_host *host = knet_h->host_index[host_id]; struct knet_link *link = &host->link[link_id]; int notify_status = link->status.connected; int savederrno = 0; if ((link->status.enabled == enabled) && (link->status.connected == connected)) return 0; if ((link->status.enabled) && (knet_h->link_status_change_notify_fn)) { if (link->status.connected != connected) { notify_status = connected; /* connection state */ } if (!enabled) { notify_status = 0; /* disable == disconnected */ } knet_h->link_status_change_notify_fn( knet_h->link_status_change_notify_fn_private_data, host_id, link_id, notify_status, host->status.remote, host->status.external); } link->status.enabled = enabled; link->status.connected = connected; _host_dstcache_update_async(knet_h, host); if ((link->status.dynconnected) && (!link->status.connected)) { link->status.dynconnected = 0; } if (!connected) { transport_link_is_down(knet_h, link); } else { /* Reset MTU in case new link can't use full line MTU */ log_info(knet_h, KNET_SUB_LINK, "Resetting MTU for link %u because host %u joined", link_id, host_id); force_pmtud_run(knet_h, KNET_SUB_LINK, 1, 1); } if (lock_stats) { savederrno = pthread_mutex_lock(&link->link_stats_mutex); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get stats mutex lock for host %u link %u: %s", host_id, link_id, strerror(savederrno)); errno = savederrno; return -1; } } if (connected) { time(&link->status.stats.last_up_times[link->status.stats.last_up_time_index]); link->status.stats.up_count++; if (++link->status.stats.last_up_time_index >= MAX_LINK_EVENTS) { link->status.stats.last_up_time_index = 0; } } else { time(&link->status.stats.last_down_times[link->status.stats.last_down_time_index]); link->status.stats.down_count++; if (++link->status.stats.last_down_time_index >= MAX_LINK_EVENTS) { link->status.stats.last_down_time_index = 0; } } if (lock_stats) { pthread_mutex_unlock(&link->link_stats_mutex); } return 0; } void _link_clear_stats(knet_handle_t knet_h) { struct knet_host *host; struct knet_link *link; uint32_t host_id; uint8_t link_id; for (host_id = 0; host_id < KNET_MAX_HOST; host_id++) { host = knet_h->host_index[host_id]; if (!host) { continue; } for (link_id = 0; link_id < KNET_MAX_LINK; link_id++) { link = &host->link[link_id]; memset(&link->status.stats, 0, sizeof(struct knet_link_stats)); } } } int knet_link_set_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t transport, struct sockaddr_storage *src_addr, struct sockaddr_storage *dst_addr, uint64_t flags) { int savederrno = 0, err = 0, i, wipelink = 0, link_idx; struct knet_host *host, *tmp_host; struct knet_link *link = NULL; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!src_addr) { errno = EINVAL; return -1; } if (dst_addr && (src_addr->ss_family != dst_addr->ss_family)) { log_err(knet_h, KNET_SUB_LINK, "Source address family does not match destination address family"); errno = EINVAL; return -1; } if (transport >= KNET_MAX_TRANSPORTS) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (transport == KNET_TRANSPORT_LOOPBACK && knet_h->host_id != host_id) { log_err(knet_h, KNET_SUB_LINK, "Cannot create loopback link to remote node"); err = -1; savederrno = EINVAL; goto exit_unlock; } if (knet_h->host_id == host_id && knet_h->has_loop_link) { log_err(knet_h, KNET_SUB_LINK, "Cannot create more than 1 link when loopback is active"); err = -1; savederrno = EINVAL; goto exit_unlock; } 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; } if (transport == KNET_TRANSPORT_LOOPBACK && knet_h->host_id == host_id) { for (i=0; ilink[i].configured) { log_err(knet_h, KNET_SUB_LINK, "Cannot add loopback link when other links are already configured."); err = -1; savederrno = EINVAL; goto exit_unlock; } } } link = &host->link[link_id]; if (link->configured != 0) { err =-1; savederrno = EBUSY; log_err(knet_h, KNET_SUB_LINK, "Host %u link %u is currently configured: %s", host_id, link_id, strerror(savederrno)); goto exit_unlock; } if (link->status.enabled != 0) { err =-1; savederrno = EBUSY; log_err(knet_h, KNET_SUB_LINK, "Host %u link %u is currently in use: %s", host_id, link_id, strerror(savederrno)); goto exit_unlock; } /* * errors happening after this point should trigger * a memset of the link */ wipelink = 1; copy_sockaddr(&link->src_addr, src_addr); err = knet_addrtostr(src_addr, sizeof(struct sockaddr_storage), link->status.src_ipaddr, KNET_MAX_HOST_LEN, link->status.src_port, KNET_MAX_PORT_LEN); if (err) { if (err == EAI_SYSTEM) { savederrno = errno; log_warn(knet_h, KNET_SUB_LINK, "Unable to resolve host: %u link: %u source addr/port: %s", host_id, link_id, strerror(savederrno)); } else { savederrno = EINVAL; log_warn(knet_h, KNET_SUB_LINK, "Unable to resolve host: %u link: %u source addr/port: %s", host_id, link_id, gai_strerror(err)); } err = -1; goto exit_unlock; } if (!dst_addr) { link->dynamic = KNET_LINK_DYNIP; } else { link->dynamic = KNET_LINK_STATIC; copy_sockaddr(&link->dst_addr, dst_addr); err = knet_addrtostr(dst_addr, sizeof(struct sockaddr_storage), link->status.dst_ipaddr, KNET_MAX_HOST_LEN, link->status.dst_port, KNET_MAX_PORT_LEN); if (err) { if (err == EAI_SYSTEM) { savederrno = errno; log_warn(knet_h, KNET_SUB_LINK, "Unable to resolve host: %u link: %u destination addr/port: %s", host_id, link_id, strerror(savederrno)); } else { savederrno = EINVAL; log_warn(knet_h, KNET_SUB_LINK, "Unable to resolve host: %u link: %u destination addr/port: %s", host_id, link_id, gai_strerror(err)); } err = -1; goto exit_unlock; } } link->pmtud_crypto_timeout_multiplier = KNET_LINK_PMTUD_CRYPTO_TIMEOUT_MULTIPLIER_MIN; link->pong_count = KNET_LINK_DEFAULT_PONG_COUNT; link->has_valid_mtu = 0; link->ping_interval = KNET_LINK_DEFAULT_PING_INTERVAL * 1000; /* microseconds */ link->pong_timeout = KNET_LINK_DEFAULT_PING_TIMEOUT * 1000; /* microseconds */ link->pong_timeout_backoff = KNET_LINK_PONG_TIMEOUT_BACKOFF; link->pong_timeout_adj = link->pong_timeout * link->pong_timeout_backoff; /* microseconds */ link->latency_max_samples = KNET_LINK_DEFAULT_PING_PRECISION; link->status.stats.latency_samples = 0; link->flags = flags; /* * check for DYNIP vs STATIC collisions. * example: link0 is static, user attempts to configure link1 as dynamic with the same source * address/port. * This configuration is invalid and would cause ACL collisions. */ for (tmp_host = knet_h->host_head; tmp_host != NULL; tmp_host = tmp_host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { if (&tmp_host->link[link_idx] == link) continue; if ((!memcmp(&tmp_host->link[link_idx].src_addr, &link->src_addr, sizeof(struct sockaddr_storage))) && (tmp_host->link[link_idx].dynamic != link->dynamic)) { savederrno = EINVAL; err = -1; log_err(knet_h, KNET_SUB_LINK, "Failed to configure host %u link %u dyn %u. Conflicts with host %u link %u dyn %u: %s", host_id, link_id, link->dynamic, tmp_host->host_id, link_idx, tmp_host->link[link_idx].dynamic, strerror(savederrno)); goto exit_unlock; } } } savederrno = pthread_mutex_init(&link->link_stats_mutex, NULL); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to initialize link stats mutex: %s", strerror(savederrno)); err = -1; goto exit_unlock; } if (transport_link_set_config(knet_h, link, transport) < 0) { savederrno = errno; err = -1; goto exit_transport_err; } /* * we can only configure default access lists if we know both endpoints * and the protocol uses GENERIC_ACL, otherwise the protocol has * to setup their own access lists above in transport_link_set_config. */ if ((transport_get_acl_type(knet_h, transport) == USE_GENERIC_ACL) && (link->dynamic == KNET_LINK_STATIC)) { log_debug(knet_h, KNET_SUB_LINK, "Configuring default access lists for host: %u link: %u socket: %d", host_id, link_id, link->outsock); if ((check_add(knet_h, link, -1, &link->dst_addr, &link->dst_addr, CHECK_TYPE_ADDRESS, CHECK_ACCEPT) < 0) && (errno != EEXIST)) { log_warn(knet_h, KNET_SUB_LINK, "Failed to configure default access lists for host: %u link: %u", host_id, link_id); savederrno = errno; err = -1; goto exit_acl_error; } } /* * no errors should happen after link is configured */ link->configured = 1; log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u is configured", host_id, link_id); if (transport == KNET_TRANSPORT_LOOPBACK) { knet_h->has_loop_link = 1; knet_h->loop_link = link_id; host->status.reachable = 1; link->status.mtu = KNET_PMTUD_SIZE_V6; } else { /* * calculate the minimum MTU that is safe to use, * based on RFCs and that each network device should * be able to support without any troubles */ if (link->dynamic == KNET_LINK_STATIC) { /* * with static link we can be more precise than using * the generic calc_min_mtu() */ switch (link->dst_addr.ss_family) { case AF_INET6: link->status.mtu = calc_max_data_outlen(knet_h, KNET_PMTUD_MIN_MTU_V6 - (KNET_PMTUD_OVERHEAD_V6 + link->proto_overhead)); break; case AF_INET: link->status.mtu = calc_max_data_outlen(knet_h, KNET_PMTUD_MIN_MTU_V4 - (KNET_PMTUD_OVERHEAD_V4 + link->proto_overhead)); break; } } else { /* * for dynamic links we start with the minimum MTU * possible and PMTUd will kick in immediately * after connection status is 1 */ link->status.mtu = calc_min_mtu(knet_h); } link->has_valid_mtu = 1; } exit_acl_error: /* * if creating access lists has error, we only need to clean * the transport and the stuff below. */ if (err < 0) { if ((transport_link_clear_config(knet_h, link) < 0) && (errno != EBUSY)) { log_warn(knet_h, KNET_SUB_LINK, "Failed to deconfigure transport for host %u link %u: %s", host_id, link_id, strerror(errno)); } } exit_transport_err: /* * if transport has errors, transport will clean after itself * and we only need to clean the mutex */ if (err < 0) { pthread_mutex_destroy(&link->link_stats_mutex); } exit_unlock: /* * re-init the link on error */ if ((err < 0) && (wipelink)) { memset(link, 0, sizeof(struct knet_link)); link->link_id = link_id; } pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_get_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t *transport, struct sockaddr_storage *src_addr, struct sockaddr_storage *dst_addr, uint8_t *dynamic, uint64_t *flags) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!src_addr) { errno = EINVAL; return -1; } if (!dynamic) { errno = EINVAL; return -1; } if (!transport) { errno = EINVAL; return -1; } if (!flags) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_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; } if ((link->dynamic == KNET_LINK_STATIC) && (!dst_addr)) { savederrno = EINVAL; err = -1; goto exit_unlock; } memmove(src_addr, &link->src_addr, sockaddr_len(&link->src_addr)); *transport = link->transport; *flags = link->flags; if (link->dynamic == KNET_LINK_STATIC) { *dynamic = 0; memmove(dst_addr, &link->dst_addr, sockaddr_len(&link->dst_addr)); } else { *dynamic = 1; } exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_clear_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; int sock; uint8_t transport; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write 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 != 1) { 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; } if (link->status.enabled != 0) { err = -1; savederrno = EBUSY; log_err(knet_h, KNET_SUB_LINK, "Host %u link %u is currently in use: %s", host_id, link_id, strerror(savederrno)); goto exit_unlock; } /* * remove well known access lists here. * After the transport has done clearing the config, * then we can remove any leftover access lists if the link * is no longer in use. */ if ((transport_get_acl_type(knet_h, link->transport) == USE_GENERIC_ACL) && (link->dynamic == KNET_LINK_STATIC)) { if ((check_rm(knet_h, link, &link->dst_addr, &link->dst_addr, CHECK_TYPE_ADDRESS, CHECK_ACCEPT) < 0) && (errno != ENOENT)) { err = -1; savederrno = errno; log_err(knet_h, KNET_SUB_LINK, "Host %u link %u: unable to remove default access list", host_id, link_id); goto exit_unlock; } } /* * cache it for later as we don't know if the transport * will clear link info during clear_config. */ sock = link->outsock; transport = link->transport; if ((transport_link_clear_config(knet_h, link) < 0) && (errno != EBUSY)) { savederrno = errno; err = -1; goto exit_unlock; } /* * remove any other access lists when the socket is no * longer in use by the transport. */ if ((transport_get_acl_type(knet_h, transport) == USE_GENERIC_ACL) && (knet_h->knet_transport_fd_tracker[sock].transport == KNET_MAX_TRANSPORTS)) { check_rmall(knet_h, link); } pthread_mutex_destroy(&link->link_stats_mutex); memset(link, 0, sizeof(struct knet_link)); link->link_id = link_id; if (knet_h->has_loop_link && host_id == knet_h->host_id && link_id == knet_h->loop_link) { knet_h->has_loop_link = 0; if (host->active_link_entries == 0) { host->status.reachable = 0; } } log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u config has been wiped", host_id, link_id); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_set_enable(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, unsigned int enabled) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (enabled > 1) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); 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; } if (link->status.enabled == enabled) { err = 0; goto exit_unlock; } err = _link_updown(knet_h, host_id, link_id, enabled, link->status.connected, 0); savederrno = errno; if (enabled) { goto exit_unlock; } log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u is disabled", host_id, link_id); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_get_enable(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, unsigned int *enabled) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!enabled) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_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; } *enabled = link->status.enabled; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_set_pong_count(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t pong_count) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (pong_count < 1) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write 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; } link->pong_count = pong_count; log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u pong count update: %u", host_id, link_id, link->pong_count); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_get_pong_count(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t *pong_count) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!pong_count) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_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; } *pong_count = link->pong_count; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_set_ping_timers(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, time_t interval, time_t timeout, unsigned int precision) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!interval) { errno = EINVAL; return -1; } if (!timeout) { errno = ENOSYS; return -1; } if (!precision) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write 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; } link->ping_interval = interval * 1000; /* microseconds */ link->pong_timeout = timeout * 1000; /* microseconds */ link->latency_max_samples = precision; log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u timeout update - interval: %llu timeout: %llu precision: %u", host_id, link_id, link->ping_interval, link->pong_timeout, precision); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_get_ping_timers(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, time_t *interval, time_t *timeout, unsigned int *precision) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!interval) { errno = EINVAL; return -1; } if (!timeout) { errno = EINVAL; return -1; } if (!precision) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_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; } *interval = link->ping_interval / 1000; /* microseconds */ *timeout = link->pong_timeout / 1000; *precision = link->latency_max_samples; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_set_priority(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t priority) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; uint8_t old_priority; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write 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; } old_priority = link->priority; if (link->priority == priority) { err = 0; goto exit_unlock; } link->priority = priority; if (_host_dstcache_update_sync(knet_h, host)) { savederrno = errno; log_debug(knet_h, KNET_SUB_LINK, "Unable to update link priority (host: %u link: %u priority: %u): %s", host_id, link_id, link->priority, strerror(savederrno)); link->priority = old_priority; err = -1; goto exit_unlock; } log_debug(knet_h, KNET_SUB_LINK, "host: %u link: %u priority set to: %u", host_id, link_id, link->priority); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_get_priority(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t *priority) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!priority) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_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; } *priority = link->priority; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_get_link_list(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t *link_ids, size_t *link_ids_entries) { int savederrno = 0, err = 0, i, count = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (!link_ids) { errno = EINVAL; return -1; } if (!link_ids_entries) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_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; } for (i = 0; i < KNET_MAX_LINK; i++) { link = &host->link[i]; if (!link->configured) { continue; } link_ids[count] = i; count++; } *link_ids_entries = count; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_get_status(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, struct knet_link_status *status, size_t struct_size) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!status) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_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; } savederrno = pthread_mutex_lock(&link->link_stats_mutex); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get stats mutex lock for host %u link %u: %s", host_id, link_id, strerror(savederrno)); err = -1; goto exit_unlock; } memmove(status, &link->status, struct_size); pthread_mutex_unlock(&link->link_stats_mutex); /* Calculate totals - no point in doing this on-the-fly */ status->stats.rx_total_packets = status->stats.rx_data_packets + status->stats.rx_ping_packets + status->stats.rx_pong_packets + status->stats.rx_pmtu_packets; status->stats.tx_total_packets = status->stats.tx_data_packets + status->stats.tx_ping_packets + status->stats.tx_pong_packets + status->stats.tx_pmtu_packets; status->stats.rx_total_bytes = status->stats.rx_data_bytes + status->stats.rx_ping_bytes + status->stats.rx_pong_bytes + status->stats.rx_pmtu_bytes; status->stats.tx_total_bytes = status->stats.tx_data_bytes + status->stats.tx_ping_bytes + status->stats.tx_pong_bytes + status->stats.tx_pmtu_bytes; status->stats.tx_total_errors = status->stats.tx_data_errors + status->stats.tx_ping_errors + status->stats.tx_pong_errors + status->stats.tx_pmtu_errors; status->stats.tx_total_retries = status->stats.tx_data_retries + status->stats.tx_ping_retries + status->stats.tx_pong_retries + status->stats.tx_pmtu_retries; /* Tell the caller our full size in case they have an old version */ status->size = sizeof(struct knet_link_status); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } int knet_link_enable_status_change_notify(knet_handle_t knet_h, void *link_status_change_notify_fn_private_data, void (*link_status_change_notify_fn) ( void *private_data, knet_node_id_t host_id, uint8_t link_id, uint8_t connected, uint8_t remote, uint8_t external)) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->link_status_change_notify_fn_private_data = link_status_change_notify_fn_private_data; knet_h->link_status_change_notify_fn = link_status_change_notify_fn; if (knet_h->link_status_change_notify_fn) { log_debug(knet_h, KNET_SUB_LINK, "link_status_change_notify_fn enabled"); } else { log_debug(knet_h, KNET_SUB_LINK, "link_status_change_notify_fn disabled"); } pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_link_insert_acl(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (!ss1) { errno = EINVAL; return -1; } if ((type != CHECK_TYPE_ADDRESS) && (type != CHECK_TYPE_MASK) && (type != CHECK_TYPE_RANGE)) { errno = EINVAL; return -1; } if ((acceptreject != CHECK_ACCEPT) && (acceptreject != CHECK_REJECT)) { errno = EINVAL; return -1; } if ((type != CHECK_TYPE_ADDRESS) && (!ss2)) { errno = EINVAL; return -1; } if ((type == CHECK_TYPE_RANGE) && (ss1->ss_family != ss2->ss_family)) { errno = EINVAL; return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write 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; } if (link->dynamic != KNET_LINK_DYNIP) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_LINK, "host %u link %u is a point to point connection: %s", host_id, link_id, strerror(savederrno)); goto exit_unlock; } err = check_add(knet_h, link, index, ss1, ss2, type, acceptreject); savederrno = errno; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = savederrno; return err; } int knet_link_add_acl(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { return knet_link_insert_acl(knet_h, host_id, link_id, -1, ss1, ss2, type, acceptreject); } int knet_link_rm_acl(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (!ss1) { errno = EINVAL; return -1; } if ((type != CHECK_TYPE_ADDRESS) && (type != CHECK_TYPE_MASK) && (type != CHECK_TYPE_RANGE)) { errno = EINVAL; return -1; } if ((acceptreject != CHECK_ACCEPT) && (acceptreject != CHECK_REJECT)) { errno = EINVAL; return -1; } if ((type != CHECK_TYPE_ADDRESS) && (!ss2)) { errno = EINVAL; return -1; } if ((type == CHECK_TYPE_RANGE) && (ss1->ss_family != ss2->ss_family)) { errno = EINVAL; return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write 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; } if (link->dynamic != KNET_LINK_DYNIP) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_LINK, "host %u link %u is a point to point connection: %s", host_id, link_id, strerror(savederrno)); goto exit_unlock; } err = check_rm(knet_h, link, ss1, ss2, type, acceptreject); savederrno = errno; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = savederrno; return err; } int knet_link_clear_acl(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id) { int savederrno = 0, err = 0; struct knet_host *host; struct knet_link *link; if (!_is_valid_handle(knet_h)) { return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_LINK, "Unable to get write 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; } if (link->dynamic != KNET_LINK_DYNIP) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_LINK, "host %u link %u is a point to point connection: %s", host_id, link_id, strerror(savederrno)); goto exit_unlock; } check_rmall(knet_h, link); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = savederrno; return err; } diff --git a/libknet/links.h b/libknet/links.h index a6c3c3e4..5826eaa1 100644 --- a/libknet/links.h +++ b/libknet/links.h @@ -1,48 +1,48 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_LINK_H__ #define __KNET_LINK_H__ #include "internals.h" #define KNET_LINK_STATIC 0 /* link has static ip on both ends */ #define KNET_LINK_DYNIP 1 /* link has dynamic destination ip */ /* * number of iterations to reduce pong_timeout_adj * from configured(pong_timeout * KNET_LINK_PONG_TIMEOUT_BACKOFF * to pong_timeout */ #define KNET_LINK_PONG_TIMEOUT_BACKOFF 10 /* * when adjusting link pong_timeout for latency, * multiply the max recorded latency by this number. * Yes it's a bit of magic, fairy dust and unicorn farts * mixed together. */ #define KNET_LINK_PONG_TIMEOUT_LAT_MUL 2 /* * under heavy load with crypto enabled, it takes much * longer time to receive a response from the other node. * * 128 is somewhat arbitrary number but we want to set a limit * and report failures after that. */ #define KNET_LINK_PMTUD_CRYPTO_TIMEOUT_MULTIPLIER_MIN 2 #define KNET_LINK_PMTUD_CRYPTO_TIMEOUT_MULTIPLIER_MAX 128 int _link_updown(knet_handle_t knet_h, knet_node_id_t node_id, uint8_t link_id, unsigned int enabled, unsigned int connected, unsigned int lock_stats); void _link_clear_stats(knet_handle_t knet_h); #endif diff --git a/libknet/links_acl.c b/libknet/links_acl.c index 73381b03..d6247113 100644 --- a/libknet/links_acl.c +++ b/libknet/links_acl.c @@ -1,67 +1,67 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include "internals.h" #include "logging.h" #include "transports.h" #include "transport_common.h" #include "links_acl.h" #include "links_acl_ip.h" #include "links_acl_loopback.h" static check_ops_t proto_check_modules_cmds[] = { { TRANSPORT_PROTO_LOOPBACK, loopbackcheck_validate, loopbackcheck_add, loopbackcheck_rm, loopbackcheck_rmall }, { TRANSPORT_PROTO_IP_PROTO, ipcheck_validate, ipcheck_addip, ipcheck_rmip, ipcheck_rmall } }; /* * all those functions will return errno from the * protocol specific functions */ int check_add(knet_handle_t knet_h, struct knet_link *kn_link, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { return proto_check_modules_cmds[transport_get_proto(knet_h, kn_link->transport)].protocheck_add( &kn_link->access_list_match_entry_head, index, ss1, ss2, type, acceptreject); } int check_rm(knet_handle_t knet_h, struct knet_link *kn_link, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { return proto_check_modules_cmds[transport_get_proto(knet_h, kn_link->transport)].protocheck_rm( &kn_link->access_list_match_entry_head, ss1, ss2, type, acceptreject); } void check_rmall(knet_handle_t knet_h, struct knet_link *kn_link) { proto_check_modules_cmds[transport_get_proto(knet_h, kn_link->transport)].protocheck_rmall( &kn_link->access_list_match_entry_head); } /* * return 0 to reject and 1 to accept a packet */ int check_validate(knet_handle_t knet_h, struct knet_link *kn_link, struct sockaddr_storage *checkip) { return proto_check_modules_cmds[transport_get_proto(knet_h, kn_link->transport)].protocheck_validate( &kn_link->access_list_match_entry_head, checkip); } diff --git a/libknet/links_acl.h b/libknet/links_acl.h index 63f4c0ed..84deef41 100644 --- a/libknet/links_acl.h +++ b/libknet/links_acl.h @@ -1,44 +1,44 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_LINKS_ACL_H__ #define __KNET_LINKS_ACL_H__ #include "internals.h" typedef struct { uint8_t transport_proto; int (*protocheck_validate) (void *fd_tracker_match_entry_head, struct sockaddr_storage *checkip); int (*protocheck_add) (void *fd_tracker_match_entry_head, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); int (*protocheck_rm) (void *fd_tracker_match_entry_head, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); void (*protocheck_rmall) (void *fd_tracker_match_entry_head); } check_ops_t; int check_add(knet_handle_t knet_h, struct knet_link *kn_link, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); int check_rm(knet_handle_t knet_h, struct knet_link *kn_link, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); void check_rmall(knet_handle_t knet_h, struct knet_link *kn_link); int check_validate(knet_handle_t knet_h, struct knet_link *kn_link, struct sockaddr_storage *checkip); #endif diff --git a/libknet/links_acl_ip.c b/libknet/links_acl_ip.c index 83e4bbe1..3edda492 100644 --- a/libknet/links_acl_ip.c +++ b/libknet/links_acl_ip.c @@ -1,305 +1,305 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "internals.h" #include "netutils.h" #include "logging.h" #include "transports.h" #include "links_acl.h" #include "links_acl_ip.h" struct ip_acl_match_entry { check_type_t type; check_acceptreject_t acceptreject; struct sockaddr_storage addr1; /* Actual IP address, mask top or low IP */ struct sockaddr_storage addr2; /* high IP address or address bitmask */ struct ip_acl_match_entry *next; }; /* * IPv4 See if the address we have matches the current match entry */ static int ip_matches_v4(struct sockaddr_storage *checkip, struct ip_acl_match_entry *match_entry) { struct sockaddr_in *ip_to_check; struct sockaddr_in *match1; struct sockaddr_in *match2; ip_to_check = (struct sockaddr_in *)checkip; match1 = (struct sockaddr_in *)&match_entry->addr1; match2 = (struct sockaddr_in *)&match_entry->addr2; switch(match_entry->type) { case CHECK_TYPE_ADDRESS: if (ip_to_check->sin_addr.s_addr == match1->sin_addr.s_addr) return 1; break; case CHECK_TYPE_MASK: if ((ip_to_check->sin_addr.s_addr & match2->sin_addr.s_addr) == match1->sin_addr.s_addr) return 1; break; case CHECK_TYPE_RANGE: if ((ntohl(ip_to_check->sin_addr.s_addr) >= ntohl(match1->sin_addr.s_addr)) && (ntohl(ip_to_check->sin_addr.s_addr) <= ntohl(match2->sin_addr.s_addr))) return 1; break; } return 0; } /* * Compare two IPv6 addresses */ static int ip6addr_cmp(struct in6_addr *a, struct in6_addr *b) { uint64_t a_high, a_low; uint64_t b_high, b_low; a_high = ((uint64_t)htonl(a->s6_addr32[0]) << 32) | (uint64_t)htonl(a->s6_addr32[1]); a_low = ((uint64_t)htonl(a->s6_addr32[2]) << 32) | (uint64_t)htonl(a->s6_addr32[3]); b_high = ((uint64_t)htonl(b->s6_addr32[0]) << 32) | (uint64_t)htonl(b->s6_addr32[1]); b_low = ((uint64_t)htonl(b->s6_addr32[2]) << 32) | (uint64_t)htonl(b->s6_addr32[3]); if (a_high > b_high) return 1; if (a_high < b_high) return -1; if (a_low > b_low) return 1; if (a_low < b_low) return -1; return 0; } /* * IPv6 See if the address we have matches the current match entry */ static int ip_matches_v6(struct sockaddr_storage *checkip, struct ip_acl_match_entry *match_entry) { struct sockaddr_in6 *ip_to_check; struct sockaddr_in6 *match1; struct sockaddr_in6 *match2; int i; ip_to_check = (struct sockaddr_in6 *)checkip; match1 = (struct sockaddr_in6 *)&match_entry->addr1; match2 = (struct sockaddr_in6 *)&match_entry->addr2; switch(match_entry->type) { case CHECK_TYPE_ADDRESS: if (!memcmp(ip_to_check->sin6_addr.s6_addr32, match1->sin6_addr.s6_addr32, sizeof(struct in6_addr))) return 1; break; case CHECK_TYPE_MASK: /* * Note that this little loop will quit early if there is a non-match so the * comparison might look backwards compared to the IPv4 one */ for (i=sizeof(struct in6_addr)/4-1; i>=0; i--) { if ((ip_to_check->sin6_addr.s6_addr32[i] & match2->sin6_addr.s6_addr32[i]) != match1->sin6_addr.s6_addr32[i]) return 0; } return 1; case CHECK_TYPE_RANGE: if ((ip6addr_cmp(&ip_to_check->sin6_addr, &match1->sin6_addr) >= 0) && (ip6addr_cmp(&ip_to_check->sin6_addr, &match2->sin6_addr) <= 0)) return 1; break; } return 0; } int ipcheck_validate(void *fd_tracker_match_entry_head, struct sockaddr_storage *checkip) { struct ip_acl_match_entry **match_entry_head = (struct ip_acl_match_entry **)fd_tracker_match_entry_head; struct ip_acl_match_entry *match_entry = *match_entry_head; int (*match_fn)(struct sockaddr_storage *checkip, struct ip_acl_match_entry *match_entry); if (checkip->ss_family == AF_INET) { match_fn = ip_matches_v4; } else { match_fn = ip_matches_v6; } while (match_entry) { if (match_fn(checkip, match_entry)) { if (match_entry->acceptreject == CHECK_ACCEPT) return 1; else return 0; } match_entry = match_entry->next; } return 0; /* Default reject */ } /* * Routines to manuipulate access lists */ void ipcheck_rmall(void *fd_tracker_match_entry_head) { struct ip_acl_match_entry **match_entry_head = (struct ip_acl_match_entry **)fd_tracker_match_entry_head; struct ip_acl_match_entry *next_match_entry; struct ip_acl_match_entry *match_entry = *match_entry_head; while (match_entry) { next_match_entry = match_entry->next; free(match_entry); match_entry = next_match_entry; } *match_entry_head = NULL; } static struct ip_acl_match_entry *ipcheck_findmatch(struct ip_acl_match_entry **match_entry_head, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { struct ip_acl_match_entry *match_entry = *match_entry_head; while (match_entry) { if ((!memcmp(&match_entry->addr1, ss1, sockaddr_len(ss1))) && ((match_entry->type == CHECK_TYPE_ADDRESS) || ((match_entry->type != CHECK_TYPE_ADDRESS) && ss2 && !memcmp(&match_entry->addr2, ss2, sockaddr_len(ss1)))) && (match_entry->type == type) && (match_entry->acceptreject == acceptreject)) { return match_entry; } match_entry = match_entry->next; } return NULL; } int ipcheck_rmip(void *fd_tracker_match_entry_head, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { struct ip_acl_match_entry **match_entry_head = (struct ip_acl_match_entry **)fd_tracker_match_entry_head; struct ip_acl_match_entry *next_match_entry = NULL; struct ip_acl_match_entry *rm_match_entry; struct ip_acl_match_entry *match_entry = *match_entry_head; rm_match_entry = ipcheck_findmatch(match_entry_head, ss1, ss2, type, acceptreject); if (!rm_match_entry) { errno = ENOENT; return -1; } while (match_entry) { next_match_entry = match_entry->next; /* * we are removing the list head, be careful */ if (rm_match_entry == match_entry) { *match_entry_head = next_match_entry; free(match_entry); break; } /* * the next one is the one we need to remove */ if (rm_match_entry == next_match_entry) { match_entry->next = next_match_entry->next; free(next_match_entry); break; } match_entry = next_match_entry; } return 0; } int ipcheck_addip(void *fd_tracker_match_entry_head, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { struct ip_acl_match_entry **match_entry_head = (struct ip_acl_match_entry **)fd_tracker_match_entry_head; struct ip_acl_match_entry *new_match_entry; struct ip_acl_match_entry *match_entry = *match_entry_head; int i = 0; if (ipcheck_findmatch(match_entry_head, ss1, ss2, type, acceptreject) != NULL) { errno = EEXIST; return -1; } new_match_entry = malloc(sizeof(struct ip_acl_match_entry)); if (!new_match_entry) { return -1; } copy_sockaddr(&new_match_entry->addr1, ss1); if (ss2) { copy_sockaddr(&new_match_entry->addr2, ss2); } new_match_entry->type = type; new_match_entry->acceptreject = acceptreject; new_match_entry->next = NULL; if (match_entry) { /* * special case for index 0, since we need to update * the head of the list */ if (index == 0) { *match_entry_head = new_match_entry; new_match_entry->next = match_entry; } else { /* * find the end of the list or stop at "index" */ while (match_entry->next) { match_entry = match_entry->next; if (i == index) { break; } i++; } /* * insert if there are more entries in the list */ if (match_entry->next) { new_match_entry->next = match_entry->next; } /* * add if we are at the end */ match_entry->next = new_match_entry; } } else { /* * first entry in the list */ *match_entry_head = new_match_entry; } return 0; } diff --git a/libknet/links_acl_ip.h b/libknet/links_acl_ip.h index 6416a330..2f60e834 100644 --- a/libknet/links_acl_ip.h +++ b/libknet/links_acl_ip.h @@ -1,27 +1,27 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_LINKS_ACL_IP_H__ #define __KNET_LINKS_ACL_IP_H__ #include "internals.h" #include "links_acl.h" int ipcheck_validate(void *fd_tracker_match_entry_head, struct sockaddr_storage *checkip); int ipcheck_addip(void *fd_tracker_match_entry_head, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); int ipcheck_rmip(void *fd_tracker_match_entry_head, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); void ipcheck_rmall(void *fd_tracker_match_entry_head); #endif diff --git a/libknet/links_acl_loopback.c b/libknet/links_acl_loopback.c index 1d26e5d4..f0147db3 100644 --- a/libknet/links_acl_loopback.c +++ b/libknet/links_acl_loopback.c @@ -1,41 +1,41 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include "internals.h" #include "logging.h" #include "transports.h" #include "links_acl.h" #include "links_acl_loopback.h" int loopbackcheck_validate(void *fd_tracker_match_entry_head, struct sockaddr_storage *checkip) { return 1; } void loopbackcheck_rmall(void *fd_tracker_match_entry_head) { return; } int loopbackcheck_rm(void *fd_tracker_match_entry_head, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { return 0; } int loopbackcheck_add(void *fd_tracker_match_entry_head, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { return 0; } diff --git a/libknet/links_acl_loopback.h b/libknet/links_acl_loopback.h index d22caded..96d15064 100644 --- a/libknet/links_acl_loopback.h +++ b/libknet/links_acl_loopback.h @@ -1,27 +1,27 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_LINKS_ACL_LOOPBACK_H__ #define __KNET_LINKS_ACL_LOOPBACK_H__ #include "internals.h" #include "links_acl.h" int loopbackcheck_validate(void *fd_tracker_match_entry_head, struct sockaddr_storage *checkip); int loopbackcheck_add(void *fd_tracker_match_entry_head, int index, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); int loopbackcheck_rm(void *fd_tracker_match_entry_head, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); void loopbackcheck_rmall(void *fd_tracker_match_entry_head); #endif diff --git a/libknet/logging.c b/libknet/logging.c index b95cff89..04dba429 100644 --- a/libknet/logging.c +++ b/libknet/logging.c @@ -1,275 +1,275 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "internals.h" #include "logging.h" #include "threads_common.h" static struct pretty_names subsystem_names[KNET_MAX_SUBSYSTEMS] = { { "common", KNET_SUB_COMMON }, { "handle", KNET_SUB_HANDLE }, { "host", KNET_SUB_HOST }, { "listener", KNET_SUB_LISTENER }, { "link", KNET_SUB_LINK }, { "transport", KNET_SUB_TRANSPORT }, { "crypto", KNET_SUB_CRYPTO }, { "compress", KNET_SUB_COMPRESS }, { "filter", KNET_SUB_FILTER }, { "dstcache", KNET_SUB_DSTCACHE }, { "heartbeat", KNET_SUB_HEARTBEAT }, { "pmtud", KNET_SUB_PMTUD }, { "tx", KNET_SUB_TX }, { "rx", KNET_SUB_RX }, { "loopback", KNET_SUB_TRANSP_LOOPBACK }, { "udp", KNET_SUB_TRANSP_UDP }, { "sctp", KNET_SUB_TRANSP_SCTP }, { "nsscrypto", KNET_SUB_NSSCRYPTO }, { "opensslcrypto", KNET_SUB_OPENSSLCRYPTO }, { "gcryptcrypto", KNET_SUB_GCRYPTCRYPTO }, { "zlibcomp", KNET_SUB_ZLIBCOMP }, { "lz4comp", KNET_SUB_LZ4COMP }, { "lz4hccomp", KNET_SUB_LZ4HCCOMP }, { "lzo2comp", KNET_SUB_LZO2COMP }, { "lzmacomp", KNET_SUB_LZMACOMP }, { "bzip2comp", KNET_SUB_BZIP2COMP }, { "zstdcomp", KNET_SUB_ZSTDCOMP }, { "unknown", KNET_SUB_UNKNOWN } /* unknown MUST always be last in this array */ }; const char *knet_log_get_subsystem_name(uint8_t subsystem) { unsigned int i; for (i = 0; i < KNET_MAX_SUBSYSTEMS; i++) { if (subsystem_names[i].val == KNET_SUB_UNKNOWN) { break; } if (subsystem_names[i].val == subsystem) { errno = 0; return subsystem_names[i].name; } } return "unknown"; } uint8_t knet_log_get_subsystem_id(const char *name) { unsigned int i; for (i = 0; i < KNET_MAX_SUBSYSTEMS; i++) { if (subsystem_names[i].val == KNET_SUB_UNKNOWN) { break; } if (strcasecmp(name, subsystem_names[i].name) == 0) { errno = 0; return subsystem_names[i].val; } } return KNET_SUB_UNKNOWN; } static int is_valid_subsystem(uint8_t subsystem) { unsigned int i; for (i = 0; i < KNET_MAX_SUBSYSTEMS; i++) { if ((subsystem != KNET_SUB_UNKNOWN) && (subsystem_names[i].val == KNET_SUB_UNKNOWN)) { break; } if (subsystem_names[i].val == subsystem) { return 0; } } return -1; } static struct pretty_names loglevel_names[KNET_LOG_DEBUG + 1] = { { "ERROR", KNET_LOG_ERR }, { "WARNING", KNET_LOG_WARN }, { "info", KNET_LOG_INFO }, { "debug", KNET_LOG_DEBUG } }; const char *knet_log_get_loglevel_name(uint8_t level) { unsigned int i; for (i = 0; i <= KNET_LOG_DEBUG; i++) { if (loglevel_names[i].val == level) { errno = 0; return loglevel_names[i].name; } } return "ERROR"; } uint8_t knet_log_get_loglevel_id(const char *name) { unsigned int i; for (i = 0; i <= KNET_LOG_DEBUG; i++) { if (strcasecmp(name, loglevel_names[i].name) == 0) { errno = 0; return loglevel_names[i].val; } } return KNET_LOG_ERR; } int knet_log_set_loglevel(knet_handle_t knet_h, uint8_t subsystem, uint8_t level) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (is_valid_subsystem(subsystem) < 0) { errno = EINVAL; return -1; } if (level > KNET_LOG_DEBUG) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, subsystem, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->log_levels[subsystem] = level; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_log_get_loglevel(knet_handle_t knet_h, uint8_t subsystem, uint8_t *level) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (is_valid_subsystem(subsystem) < 0) { errno = EINVAL; return -1; } if (!level) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, subsystem, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } *level = knet_h->log_levels[subsystem]; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } void log_msg(knet_handle_t knet_h, uint8_t subsystem, uint8_t msglevel, const char *fmt, ...) { va_list ap; struct knet_log_msg msg; size_t byte_cnt = 0; int len; int retry_loop = 0; if ((!knet_h) || (subsystem == KNET_MAX_SUBSYSTEMS) || (msglevel > knet_h->log_levels[subsystem])) return; if (knet_h->logfd <= 0) goto out; memset(&msg, 0, sizeof(struct knet_log_msg)); msg.subsystem = subsystem; msg.msglevel = msglevel; msg.knet_h = knet_h; va_start(ap, fmt); #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-nonliteral" #endif vsnprintf(msg.msg, sizeof(msg.msg), fmt, ap); #ifdef __clang__ #pragma clang diagnostic pop #endif va_end(ap); retry: while (byte_cnt < sizeof(struct knet_log_msg)) { len = write(knet_h->logfd, &msg, sizeof(struct knet_log_msg) - byte_cnt); if (len <= 0) { if (errno == EAGAIN) { struct timeval tv; /* * those 3 lines are the equivalent of usleep(1) * but usleep makes some static code analizers very * unhappy. * * this version is somewhat stolen from gnulib * nanosleep implementation */ tv.tv_sec = 0; tv.tv_usec = 1; select(0, NULL, NULL, NULL, &tv); retry_loop++; /* * arbitrary amount of retries. * tested with fun_log_bench, 10 retries was never hit */ if (retry_loop >= 100) { goto out; } goto retry; } goto out; } byte_cnt += len; } out: return; } diff --git a/libknet/logging.h b/libknet/logging.h index c492eada..1e2f38f3 100644 --- a/libknet/logging.h +++ b/libknet/logging.h @@ -1,38 +1,38 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_LOGGING_H__ #define __KNET_LOGGING_H__ #include "internals.h" typedef void log_msg_t(knet_handle_t knet_h, uint8_t subsystem, uint8_t msglevel, const char *fmt, ...) __attribute__((format(printf, 4, 5))); #ifdef KNET_MODULE #define LOG_MSG (*log_msg) #else #define LOG_MSG log_msg #endif log_msg_t LOG_MSG; #define log_err(knet_h, subsys, fmt, args...) \ LOG_MSG(knet_h, subsys, KNET_LOG_ERR, fmt, ##args) #define log_warn(knet_h, subsys, fmt, args...) \ LOG_MSG(knet_h, subsys, KNET_LOG_WARN, fmt, ##args) #define log_info(knet_h, subsys, fmt, args...) \ LOG_MSG(knet_h, subsys, KNET_LOG_INFO, fmt, ##args) #define log_debug(knet_h, subsys, fmt, args...) \ LOG_MSG(knet_h, subsys, KNET_LOG_DEBUG, fmt, ##args) #endif diff --git a/libknet/netutils.c b/libknet/netutils.c index ed582da1..08def6a7 100644 --- a/libknet/netutils.c +++ b/libknet/netutils.c @@ -1,140 +1,140 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "internals.h" #include "netutils.h" int cmpaddr(const struct sockaddr_storage *ss1, const struct sockaddr_storage *ss2) { struct sockaddr_in6 *ss1_addr6 = (struct sockaddr_in6 *)ss1; struct sockaddr_in6 *ss2_addr6 = (struct sockaddr_in6 *)ss2; struct sockaddr_in *ss1_addr = (struct sockaddr_in *)ss1; struct sockaddr_in *ss2_addr = (struct sockaddr_in *)ss2; if (ss1->ss_family != ss2->ss_family) { return -1; } if (ss1->ss_family == AF_INET6) { return memcmp(&ss1_addr6->sin6_addr.s6_addr32, &ss2_addr6->sin6_addr.s6_addr32, sizeof(struct in6_addr)); } return memcmp(&ss1_addr->sin_addr.s_addr, &ss2_addr->sin_addr.s_addr, sizeof(struct in_addr)); } socklen_t sockaddr_len(const struct sockaddr_storage *ss) { if (ss->ss_family == AF_INET) { return sizeof(struct sockaddr_in); } else { return sizeof(struct sockaddr_in6); } } /* Only copy the valid parts of a sockaddr* */ void copy_sockaddr(struct sockaddr_storage *sout, const struct sockaddr_storage *sin) { memset(sout, 0, sizeof(struct sockaddr_storage)); memmove(sout, sin, sockaddr_len(sin)); } /* * exported APIs */ int knet_strtoaddr(const char *host, const char *port, struct sockaddr_storage *ss, socklen_t sslen) { int err; struct addrinfo hints; struct addrinfo *result = NULL; if (!host) { errno = EINVAL; return -1; } if (!port) { errno = EINVAL; return -1; } if (!ss) { errno = EINVAL; return -1; } if (!sslen) { errno = EINVAL; return -1; } memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV; err = getaddrinfo(host, port, &hints, &result); if (!err) { memmove(ss, result->ai_addr, (sslen < result->ai_addrlen) ? sslen : result->ai_addrlen); freeaddrinfo(result); } if (!err) errno = 0; return err; } int knet_addrtostr(const struct sockaddr_storage *ss, socklen_t sslen, char *addr_buf, size_t addr_buf_size, char *port_buf, size_t port_buf_size) { int err; if (!ss) { errno = EINVAL; return -1; } if (!sslen) { errno = EINVAL; return -1; } if (!addr_buf) { errno = EINVAL; return -1; } if (!port_buf) { errno = EINVAL; return -1; } err = getnameinfo((struct sockaddr *)ss, sockaddr_len(ss), addr_buf, addr_buf_size, port_buf, port_buf_size, NI_NUMERICHOST | NI_NUMERICSERV); if (!err) errno = 0; return err; } diff --git a/libknet/netutils.h b/libknet/netutils.h index 988b1304..e7606bef 100644 --- a/libknet/netutils.h +++ b/libknet/netutils.h @@ -1,31 +1,31 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_NETUTILS_H__ #define __KNET_NETUTILS_H__ #include #include /* * s6_addr32 is not defined in BSD userland, only kernel. * definition is the same as linux and it works fine for * what we need. */ #ifndef s6_addr32 #define s6_addr32 __u6_addr.__u6_addr32 #endif int cmpaddr(const struct sockaddr_storage *ss1, const struct sockaddr_storage *ss2); void copy_sockaddr(struct sockaddr_storage *sout, const struct sockaddr_storage *sin); socklen_t sockaddr_len(const struct sockaddr_storage *ss); #endif diff --git a/libknet/onwire.c b/libknet/onwire.c index 581488c4..ad7dddbb 100644 --- a/libknet/onwire.c +++ b/libknet/onwire.c @@ -1,265 +1,265 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include "crypto.h" #include "internals.h" #include "logging.h" #include "common.h" #include "transport_udp.h" #include "transport_sctp.h" /* * unencrypted packet looks like: * * | ip | protocol | knet_header | unencrypted data | * | onwire_len | * | proto_overhead | * | data_len | * | app MTU | * * encrypted packet looks like (not to scale): * * | ip | protocol | salt | crypto(knet_header | data) | crypto_data_pad | hash | * | onwire_len | * | proto_overhead | * | data_len | * | app MTU | * * knet_h->sec_block_size is >= 0 if encryption will pad the data * knet_h->sec_salt_size is >= 0 if encryption is enabled * knet_h->sec_hash_size is >= 0 if signing is enabled */ /* * this function takes in the data that we would like to send * and tells us the outgoing onwire data size with crypto and * all the headers adjustment. * calling thread needs to account for protocol overhead. */ size_t calc_data_outlen(knet_handle_t knet_h, size_t inlen) { size_t outlen = inlen, pad_len = 0; if (knet_h->sec_block_size) { /* * if the crypto mechanism requires padding, calculate the padding * and add it back to outlen because that's what the crypto layer * would do. */ pad_len = knet_h->sec_block_size - (outlen % knet_h->sec_block_size); outlen = outlen + pad_len; } return outlen + knet_h->sec_salt_size + knet_h->sec_hash_size; } /* * this function takes in the data that we would like to send * and tells us what is the real maximum data we can send * accounting for headers and crypto * calling thread needs to account for protocol overhead. */ size_t calc_max_data_outlen(knet_handle_t knet_h, size_t inlen) { size_t outlen = inlen, pad_len = 0; if (knet_h->sec_block_size) { /* * drop both salt and hash, that leaves only the crypto data and padding * we need to calculate the padding based on the real encrypted data * that includes the knet_header. */ outlen = outlen - (knet_h->sec_salt_size + knet_h->sec_hash_size); /* * if the crypto mechanism requires padding, calculate the padding * and remove it, to align the data. * NOTE: we need to remove pad_len + 1 because, based on testing, * if we send data that are already aligned to block_size, the * crypto implementations will add another block_size! * so we want to make sure that our data won't add an unnecessary * block_size that we need to remove later. */ pad_len = outlen % knet_h->sec_block_size; outlen = outlen - (pad_len + 1); /* * add both hash and salt size back, similar to padding above, * the crypto layer will add them to the outlen */ outlen = outlen + (knet_h->sec_salt_size + knet_h->sec_hash_size); } /* * drop KNET_HEADER_ALL_SIZE to provide a clean application MTU * and various crypto headers */ outlen = outlen - (KNET_HEADER_ALL_SIZE + knet_h->sec_salt_size + knet_h->sec_hash_size); return outlen; } /* * set the lowest possible value as failsafe for all links. * KNET_PMTUD_MIN_MTU_V4 < KNET_PMTUD_MIN_MTU_V6 * KNET_PMTUD_OVERHEAD_V6 > KNET_PMTUD_OVERHEAD_V4 * KNET_PMTUD_SCTP_OVERHEAD > KNET_PMTUD_UDP_OVERHEAD */ size_t calc_min_mtu(knet_handle_t knet_h) { return calc_max_data_outlen(knet_h, KNET_PMTUD_MIN_MTU_V4 - (KNET_PMTUD_OVERHEAD_V6 + KNET_PMTUD_SCTP_OVERHEAD)); } int knet_handle_enable_onwire_ver_notify(knet_handle_t knet_h, void *onwire_ver_notify_fn_private_data, void (*onwire_ver_notify_fn) ( void *private_data, uint8_t onwire_min_ver, uint8_t onwire_max_ver, uint8_t onwire_ver)) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->onwire_ver_notify_fn_private_data = onwire_ver_notify_fn_private_data; knet_h->onwire_ver_notify_fn = onwire_ver_notify_fn; if (knet_h->onwire_ver_notify_fn) { log_debug(knet_h, KNET_SUB_HANDLE, "onwire_ver_notify_fn enabled"); /* * generate an artificial call to notify the app of what´s curently * happening */ knet_h->onwire_ver_notify_fn(knet_h->onwire_ver_notify_fn_private_data, knet_h->onwire_min_ver, knet_h->onwire_max_ver, knet_h->onwire_ver); } else { log_debug(knet_h, KNET_SUB_HANDLE, "onwire_ver_notify_fn disabled"); } pthread_rwlock_unlock(&knet_h->global_rwlock); return 0; } int knet_handle_get_onwire_ver(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t *onwire_min_ver, uint8_t *onwire_max_ver, uint8_t *onwire_ver) { int err = 0, savederrno = 0; struct knet_host *host; if (!_is_valid_handle(knet_h)) { return -1; } if (!onwire_min_ver) { errno = EINVAL; return -1; } if (!onwire_max_ver) { errno = EINVAL; return -1; } if (!onwire_ver) { errno = EINVAL; return -1; } /* * we need a write lock here so that gathering host onwire info * is not racy (updated by thread_rx) and we can save a mutex_lock * to gather local node info. */ savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (host_id == knet_h->host_id) { *onwire_min_ver = knet_h->onwire_min_ver; *onwire_max_ver = knet_h->onwire_max_ver; *onwire_ver = knet_h->onwire_ver; } else { host = knet_h->host_index[host_id]; if (!host) { err = -1; savederrno = EINVAL; log_err(knet_h, KNET_SUB_HANDLE, "Unable to find host %u: %s", host_id, strerror(savederrno)); goto out_unlock; } *onwire_min_ver = 0; *onwire_max_ver = host->onwire_max_ver; *onwire_ver = host->onwire_ver; } out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = savederrno; return err; } int knet_handle_set_onwire_ver(knet_handle_t knet_h, uint8_t onwire_ver) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if ((onwire_ver) && ((onwire_ver < knet_h->onwire_min_ver) || (onwire_ver > knet_h->onwire_max_ver))) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->onwire_force_ver = onwire_ver; pthread_rwlock_unlock(&knet_h->global_rwlock); return 0; } diff --git a/libknet/onwire.h b/libknet/onwire.h index ba5904e3..dd83013a 100644 --- a/libknet/onwire.h +++ b/libknet/onwire.h @@ -1,175 +1,175 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_ONWIRE_H__ #define __KNET_ONWIRE_H__ #include #include "libknet.h" /* * data structures to define network packets. * Start from knet_header at the bottom */ /* * Plan is to support MAX_VER with MIN_VER = MAX_VER - 1 * but for the sake of not rewriting the world later on, * let´s make sure we can support a random range of protocol * versions */ #define KNET_HEADER_ONWIRE_MAX_VER 0x01 /* max onwire protocol supported by this build */ #define KNET_HEADER_ONWIRE_MIN_VER 0x01 /* min onwire protocol supported by this build */ /* * Packet types * * adding new DATA types requires the packet to contain * data_seq_num and frag_num/frag_seq in the current data types. * * Changing those data types requires major surgery to thread_tx/thread_rx * and defrag buffer allocation in knet_host_add. * * Also please be aware that frags buffer allocation size is not constant * so you cannot assume each frag is 64K+. * (see handle.c) */ #define KNET_HEADER_TYPE_DATA 0x00 /* pure data packet */ #define KNET_HEADER_TYPE_PING 0x81 /* heartbeat */ #define KNET_HEADER_TYPE_PONG 0x82 /* reply to heartbeat */ #define KNET_HEADER_TYPE_PMTUD 0x83 /* Used to determine Path MTU */ #define KNET_HEADER_TYPE_PMTUD_REPLY 0x84 /* reply from remote host */ /* * KNET_HEADER_TYPE_DATA */ typedef uint16_t seq_num_t; /* data sequence number required to deduplicate pckts */ #define SEQ_MAX UINT16_MAX struct knet_header_payload_data_v1 { seq_num_t khp_data_seq_num; /* pckt seq number used to deduplicate pckts */ uint8_t khp_data_compress; /* identify if user data are compressed */ uint8_t khp_data_pad1; /* make sure to have space in the header to grow features */ uint8_t khp_data_bcast; /* data destination bcast/ucast */ uint8_t khp_data_frag_num; /* number of fragments of this pckt. 1 is not fragmented */ uint8_t khp_data_frag_seq; /* as above, indicates the frag sequence number */ int8_t khp_data_channel; /* transport channel data for localsock <-> knet <-> localsock mapping */ #ifdef ONWIRE_V1_EXTRA_DEBUG uint32_t khp_data_checksum; /* debug checksum of all user data */ #endif uint8_t khp_data_userdata[0]; /* pointer to the real user data */ } __attribute__((packed)); #define khp_data_v1_seq_num kh_payload.khp_data_v1.khp_data_seq_num #define khp_data_v1_frag_num kh_payload.khp_data_v1.khp_data_frag_num #define khp_data_v1_frag_seq kh_payload.khp_data_v1.khp_data_frag_seq #define khp_data_v1_userdata kh_payload.khp_data_v1.khp_data_userdata #define khp_data_v1_bcast kh_payload.khp_data_v1.khp_data_bcast #define khp_data_v1_channel kh_payload.khp_data_v1.khp_data_channel #define khp_data_v1_compress kh_payload.khp_data_v1.khp_data_compress #ifdef ONWIRE_V1_EXTRA_DEBUG #define khp_data_v1_checksum kh_payload.khp_data_v1.khp_data_checksum #endif /* * KNET_HEADER_TYPE_PING / KNET_HEADER_TYPE_PONG */ struct knet_header_payload_ping_v1 { uint8_t khp_ping_link; /* changing khp_ping_link requires changes to thread_rx.c KNET_LINK_DYNIP code handling */ uint32_t khp_ping_time[4]; /* ping timestamp */ seq_num_t khp_ping_seq_num; /* transport host seq_num */ uint8_t khp_ping_timed; /* timed pinged (1) or forced by seq_num (0) */ } __attribute__((packed)); #define khp_ping_v1_link kh_payload.khp_ping_v1.khp_ping_link #define khp_ping_v1_time kh_payload.khp_ping_v1.khp_ping_time #define khp_ping_v1_seq_num kh_payload.khp_ping_v1.khp_ping_seq_num #define khp_ping_v1_timed kh_payload.khp_ping_v1.khp_ping_timed /* * KNET_HEADER_TYPE_PMTUD / KNET_HEADER_TYPE_PMTUD_REPLY */ /* * taken from tracepath6 */ #define KNET_PMTUD_SIZE_V4 65535 #define KNET_PMTUD_SIZE_V6 KNET_PMTUD_SIZE_V4 /* * IPv4/IPv6 header size */ #define KNET_PMTUD_OVERHEAD_V4 20 #define KNET_PMTUD_OVERHEAD_V6 40 #define KNET_PMTUD_MIN_MTU_V4 576 #define KNET_PMTUD_MIN_MTU_V6 1280 struct knet_header_payload_pmtud_v1 { uint8_t khp_pmtud_link; /* link_id */ uint16_t khp_pmtud_size; /* size of the current packet */ uint8_t khp_pmtud_data[0]; /* pointer to empty/random data/fill buffer */ } __attribute__((packed)); #define khp_pmtud_v1_link kh_payload.khp_pmtud_v1.khp_pmtud_link #define khp_pmtud_v1_size kh_payload.khp_pmtud_v1.khp_pmtud_size #define khp_pmtud_v1_data kh_payload.khp_pmtud_v1.khp_pmtud_data /* * PMTUd related functions */ size_t calc_data_outlen(knet_handle_t knet_h, size_t inlen); size_t calc_max_data_outlen(knet_handle_t knet_h, size_t inlen); size_t calc_min_mtu(knet_handle_t knet_h); /* * union to reference possible individual payloads */ union knet_header_payload { struct knet_header_payload_data_v1 khp_data_v1; /* pure data packet struct */ struct knet_header_payload_ping_v1 khp_ping_v1; /* heartbeat packet struct */ struct knet_header_payload_pmtud_v1 khp_pmtud_v1; /* Path MTU discovery packet struct */ } __attribute__((packed)); /* * this header CANNOT change or onwire compat will break! */ struct knet_header { uint8_t kh_version; /* this pckt format/version */ uint8_t kh_type; /* from above defines. Tells what kind of pckt it is */ knet_node_id_t kh_node; /* host id of the source host for this pckt */ uint8_t kh_max_ver; /* max version of the protocol supported by this node */ uint8_t kh_pad1; /* make sure to have space in the header to grow features */ #ifdef ONWIRE_V1_EXTRA_DEBUG uint32_t kh_checksum;/* debug checksum per packet */ #endif union knet_header_payload kh_payload; /* union of potential data struct based on kh_type */ } __attribute__((packed)); /* * extra defines to avoid mingling with sizeof() too much */ #define KNET_HEADER_ALL_SIZE sizeof(struct knet_header) #define KNET_HEADER_SIZE (KNET_HEADER_ALL_SIZE - sizeof(union knet_header_payload)) #define KNET_HEADER_PING_V1_SIZE (KNET_HEADER_SIZE + sizeof(struct knet_header_payload_ping_v1)) #define KNET_HEADER_PMTUD_V1_SIZE (KNET_HEADER_SIZE + sizeof(struct knet_header_payload_pmtud_v1)) #define KNET_HEADER_DATA_V1_SIZE (KNET_HEADER_SIZE + sizeof(struct knet_header_payload_data_v1)) #endif diff --git a/libknet/onwire_v1.c b/libknet/onwire_v1.c index ed7db75f..7da2dde0 100644 --- a/libknet/onwire_v1.c +++ b/libknet/onwire_v1.c @@ -1,248 +1,248 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "logging.h" #include "host.h" #include "links.h" #include "onwire_v1.h" int prep_ping_v1(knet_handle_t knet_h, struct knet_link *dst_link, uint8_t onwire_ver, struct timespec clock_now, int timed, ssize_t *outlen) { *outlen = KNET_HEADER_PING_V1_SIZE; /* preparing ping buffer */ knet_h->pingbuf->kh_version = onwire_ver; knet_h->pingbuf->kh_max_ver = knet_h->onwire_max_ver; knet_h->pingbuf->kh_type = KNET_HEADER_TYPE_PING; knet_h->pingbuf->kh_node = htons(knet_h->host_id); knet_h->pingbuf->khp_ping_v1_link = dst_link->link_id; knet_h->pingbuf->khp_ping_v1_timed = timed; memmove(&knet_h->pingbuf->khp_ping_v1_time[0], &clock_now, sizeof(struct timespec)); if (pthread_mutex_lock(&knet_h->tx_seq_num_mutex)) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get seq mutex lock"); return -1; } knet_h->pingbuf->khp_ping_v1_seq_num = htons(knet_h->tx_seq_num); pthread_mutex_unlock(&knet_h->tx_seq_num_mutex); #ifdef ONWIRE_V1_EXTRA_DEBUG knet_h->pingbuf->kh_checksum = 0; knet_h->pingbuf->kh_checksum = compute_chksum((const unsigned char*)knet_h->pingbuf, KNET_HEADER_PING_V1_SIZE); #endif return 0; } void prep_pong_v1(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *outlen) { *outlen = KNET_HEADER_PING_V1_SIZE; inbuf->kh_type = KNET_HEADER_TYPE_PONG; inbuf->kh_node = htons(knet_h->host_id); #ifdef ONWIRE_V1_EXTRA_DEBUG inbuf->kh_checksum = 0; inbuf->kh_checksum = compute_chksum((const unsigned char*)inbuf, KNET_HEADER_PING_V1_SIZE); #endif } void process_ping_v1(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, ssize_t len) { int wipe_bufs = 0; seq_num_t recv_seq_num = ntohs(inbuf->khp_ping_v1_seq_num); if (!inbuf->khp_ping_v1_timed) { /* * we might be receiving this message from all links, but we want * to process it only the first time */ if (recv_seq_num != src_host->untimed_rx_seq_num) { /* * cache the untimed seq num */ src_host->untimed_rx_seq_num = recv_seq_num; /* * if the host has received data in between * untimed ping, then we don't need to wipe the bufs */ if (src_host->got_data) { src_host->got_data = 0; wipe_bufs = 0; } else { wipe_bufs = 1; } } _seq_num_lookup(knet_h, src_host, recv_seq_num, 0, wipe_bufs); } else { /* * pings always arrives in bursts over all the link * catch the first of them to cache the seq num and * avoid duplicate processing */ if (recv_seq_num != src_host->timed_rx_seq_num) { src_host->timed_rx_seq_num = recv_seq_num; if (recv_seq_num == 0) { _seq_num_lookup(knet_h, src_host, recv_seq_num, 0, 1); } } } } void process_pong_v1(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, struct timespec *recvtime) { memmove(recvtime, &inbuf->khp_ping_v1_time[0], sizeof(struct timespec)); } struct knet_link *get_link_from_pong_v1(knet_handle_t knet_h, struct knet_host *src_host, struct knet_header *inbuf) { return &src_host->link[inbuf->khp_ping_v1_link]; } void prep_pmtud_v1(knet_handle_t knet_h, struct knet_link *dst_link, uint8_t onwire_ver, size_t onwire_len, size_t data_len) { knet_h->pmtudbuf->kh_version = onwire_ver; knet_h->pmtudbuf->kh_max_ver = knet_h->onwire_max_ver; knet_h->pmtudbuf->kh_type = KNET_HEADER_TYPE_PMTUD; knet_h->pmtudbuf->kh_node = htons(knet_h->host_id); knet_h->pmtudbuf->khp_pmtud_v1_link = dst_link->link_id; knet_h->pmtudbuf->khp_pmtud_v1_size = onwire_len; #ifdef ONWIRE_V1_EXTRA_DEBUG knet_h->pmtudbuf->kh_checksum = 0; knet_h->pmtudbuf->kh_checksum = compute_chksum((const unsigned char*)knet_h->pmtudbuf, data_len); #endif } void prep_pmtud_reply_v1(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *outlen) { *outlen = KNET_HEADER_PMTUD_V1_SIZE; inbuf->kh_type = KNET_HEADER_TYPE_PMTUD_REPLY; inbuf->kh_node = htons(knet_h->host_id); #ifdef ONWIRE_V1_EXTRA_DEBUG inbuf->kh_checksum = 0; inbuf->kh_checksum = compute_chksum((const unsigned char*)inbuf, KNET_HEADER_PMTUD_V1_SIZE); #endif } void process_pmtud_reply_v1(knet_handle_t knet_h, struct knet_link *src_link, struct knet_header *inbuf) { src_link->last_recv_mtu = inbuf->khp_pmtud_v1_size; } void prep_tx_bufs_v1(knet_handle_t knet_h, struct knet_header *inbuf, unsigned char *data, size_t inlen, uint32_t data_checksum, unsigned int temp_data_mtu, seq_num_t tx_seq_num, int8_t channel, int bcast, int data_compressed, int *msgs_to_send, struct iovec iov_out[PCKT_FRAG_MAX][2], int *iovcnt_out) { uint8_t frag_idx = 0; size_t frag_len = inlen; /* * prepare the main header */ inbuf->kh_type = KNET_HEADER_TYPE_DATA; inbuf->kh_version = 1; inbuf->kh_max_ver = knet_h->onwire_max_ver; inbuf->kh_node = htons(knet_h->host_id); /* * prepare the data header */ inbuf->khp_data_v1_frag_seq = 0; inbuf->khp_data_v1_bcast = bcast; inbuf->khp_data_v1_frag_num = ceil((float)inlen / temp_data_mtu); inbuf->khp_data_v1_channel = channel; inbuf->khp_data_v1_seq_num = htons(tx_seq_num); if (data_compressed) { inbuf->khp_data_v1_compress = knet_h->compress_model; } else { inbuf->khp_data_v1_compress = 0; } #ifdef ONWIRE_V1_EXTRA_DEBUG inbuf->khp_data_v1_checksum = data_checksum; #endif /* * handle fragmentation */ if (inbuf->khp_data_v1_frag_num > 1) { while (frag_idx < inbuf->khp_data_v1_frag_num) { /* * set the iov_base */ iov_out[frag_idx][0].iov_base = (void *)knet_h->send_to_links_buf[frag_idx]; iov_out[frag_idx][0].iov_len = KNET_HEADER_DATA_V1_SIZE; iov_out[frag_idx][1].iov_base = data + (temp_data_mtu * frag_idx); /* * set the len */ if (frag_len > temp_data_mtu) { iov_out[frag_idx][1].iov_len = temp_data_mtu; } else { iov_out[frag_idx][1].iov_len = frag_len; } /* * copy the frag info on all buffers */ memmove(knet_h->send_to_links_buf[frag_idx], inbuf, KNET_HEADER_DATA_V1_SIZE); /* * bump the frag */ knet_h->send_to_links_buf[frag_idx]->khp_data_v1_frag_seq = frag_idx + 1; #ifdef ONWIRE_V1_EXTRA_DEBUG knet_h->send_to_links_buf[frag_idx]->kh_checksum = 0; knet_h->send_to_links_buf[frag_idx]->kh_checksum = compute_chksumv(iov_out[frag_idx], 2); #endif frag_len = frag_len - temp_data_mtu; frag_idx++; } *iovcnt_out = 2; } else { iov_out[frag_idx][0].iov_base = (void *)inbuf; iov_out[frag_idx][0].iov_len = frag_len + KNET_HEADER_DATA_V1_SIZE; *iovcnt_out = 1; #ifdef ONWIRE_V1_EXTRA_DEBUG inbuf->kh_checksum = 0; inbuf->kh_checksum = compute_chksumv(iov_out[frag_idx], 1); #endif } *msgs_to_send = inbuf->khp_data_v1_frag_num; } unsigned char *get_data_v1(knet_handle_t knet_h, struct knet_header *inbuf) { return inbuf->khp_data_v1_userdata; } void get_data_header_info_v1(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *header_size, int8_t *channel, seq_num_t *seq_num, uint8_t *decompress_type, uint8_t *frags, uint8_t *frag_seq) { *header_size = KNET_HEADER_DATA_V1_SIZE; *channel = inbuf->khp_data_v1_channel; *seq_num = ntohs(inbuf->khp_data_v1_seq_num); *decompress_type = inbuf->khp_data_v1_compress; *frags = inbuf->khp_data_v1_frag_num; *frag_seq = inbuf->khp_data_v1_frag_seq; } diff --git a/libknet/onwire_v1.h b/libknet/onwire_v1.h index 6f128ae9..1c35ca1e 100644 --- a/libknet/onwire_v1.h +++ b/libknet/onwire_v1.h @@ -1,37 +1,37 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_ONWIRE_V1_H__ #define __KNET_ONWIRE_V1_H__ #include #include "internals.h" int prep_ping_v1(knet_handle_t knet_h, struct knet_link *dst_link, uint8_t onwire_ver, struct timespec clock_now, int timed, ssize_t *outlen); void prep_pong_v1(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *outlen); void process_ping_v1(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, ssize_t len); void process_pong_v1(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, struct timespec *recvtime); struct knet_link *get_link_from_pong_v1(knet_handle_t knet_h, struct knet_host *src_host, struct knet_header *inbuf); void prep_pmtud_v1(knet_handle_t knet_h, struct knet_link *dst_link, uint8_t onwire_ver, size_t onwire_len, size_t data_len); void prep_pmtud_reply_v1(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *outlen); void process_pmtud_reply_v1(knet_handle_t knet_h, struct knet_link *src_link, struct knet_header *inbuf); void prep_tx_bufs_v1(knet_handle_t knet_h, struct knet_header *inbuf, unsigned char *data, size_t inlen, uint32_t data_checksum, unsigned int temp_data_mtu, seq_num_t tx_seq_num, int8_t channel, int bcast, int data_compressed, int *msgs_to_send, struct iovec iov_out[PCKT_FRAG_MAX][2], int *iovcnt_out); unsigned char *get_data_v1(knet_handle_t knet_h, struct knet_header *inbuf); void get_data_header_info_v1(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *header_size, int8_t *channel, seq_num_t *seq_num, uint8_t *decompress_type, uint8_t *frags, uint8_t *frag_seq); #endif diff --git a/libknet/tests/Makefile.am b/libknet/tests/Makefile.am index 5eb9bf00..23ea82b7 100644 --- a/libknet/tests/Makefile.am +++ b/libknet/tests/Makefile.am @@ -1,126 +1,126 @@ # -# Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk include $(top_srcdir)/libknet/tests/api-check.mk EXTRA_DIST = \ api-test-coverage \ api-check.mk AM_CPPFLAGS = -I$(top_srcdir)/libknet AM_CFLAGS += $(PTHREAD_CFLAGS) $(libqb_CFLAGS) $(zlib_CFLAGS) LIBS = $(top_builddir)/libknet/libknet.la \ $(PTHREAD_LIBS) $(dl_LIBS) $(zlib_LIBS) noinst_HEADERS = \ test-common.h # the order of those tests is NOT random. # some functions can only be tested properly after some dependents # API have been validated upfront. check_PROGRAMS = \ $(api_checks) \ $(int_checks) if RUN_FUN_TESTS check_PROGRAMS += $(fun_checks) endif int_checks = \ int_links_acl_ip_test \ int_timediff_test fun_checks = \ fun_config_crypto_test \ fun_onwire_upgrade_test \ fun_acl_check_test # checks below need to be executed manually # or with a specifi environment long_run_checks = \ fun_pmtud_crypto_test benchmarks = \ knet_bench_test noinst_PROGRAMS = \ api_knet_handle_new_limit_test \ pckt_test \ $(benchmarks) \ $(long_run_checks) \ $(api_checks) \ $(int_checks) \ $(fun_checks) noinst_SCRIPTS = \ api-test-coverage TESTS = $(check_PROGRAMS) if INSTALL_TESTS testsuitedir = $(TESTDIR) testsuite_PROGRAMS = $(noinst_PROGRAMS) endif check-local: check-api-test-coverage check-annocheck-bins check-api-test-coverage: chmod u+x $(top_srcdir)/libknet/tests/api-test-coverage $(top_srcdir)/libknet/tests/api-test-coverage $(top_srcdir) $(top_builddir) pckt_test_SOURCES = pckt_test.c int_links_acl_ip_test_SOURCES = int_links_acl_ip.c \ ../common.c \ ../compat.c \ ../logging.c \ ../netutils.c \ ../threads_common.c \ ../onwire.c \ ../transports.c \ ../transport_common.c \ ../transport_loopback.c \ ../transport_sctp.c \ ../transport_udp.c \ ../links_acl.c \ ../links_acl_ip.c \ ../links_acl_loopback.c \ ../lib_config.c int_timediff_test_SOURCES = int_timediff.c knet_bench_test_SOURCES = knet_bench.c \ test-common.c \ ../common.c \ ../logging.c \ ../compat.c \ ../transport_common.c \ ../threads_common.c \ ../onwire.c \ ../lib_config.c fun_pmtud_crypto_test_SOURCES = fun_pmtud_crypto.c \ test-common.c \ ../onwire.c \ ../logging.c \ ../threads_common.c \ ../lib_config.c fun_config_crypto_test_SOURCES = fun_config_crypto.c \ test-common.c fun_onwire_upgrade_test_SOURCES = fun_onwire_upgrade.c \ test-common.c fun_acl_check_test_SOURCES = fun_acl_check.c \ test-common.c diff --git a/libknet/tests/api-check.mk b/libknet/tests/api-check.mk index 8055edcd..634e49e4 100644 --- a/libknet/tests/api-check.mk +++ b/libknet/tests/api-check.mk @@ -1,318 +1,318 @@ # -# Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # api_checks = \ api_knet_handle_new_test \ api_knet_handle_free_test \ api_knet_handle_compress_test \ api_knet_handle_setfwd_test \ api_knet_handle_enable_access_lists_test \ api_knet_handle_enable_filter_test \ api_knet_handle_enable_sock_notify_test \ api_knet_handle_add_datafd_test \ api_knet_handle_remove_datafd_test \ api_knet_handle_get_channel_test \ api_knet_handle_get_datafd_test \ api_knet_handle_get_stats_test \ api_knet_get_crypto_list_test \ api_knet_get_compress_list_test \ api_knet_handle_clear_stats_test \ api_knet_get_transport_list_test \ api_knet_get_transport_name_by_id_test \ api_knet_get_transport_id_by_name_test \ api_knet_handle_set_transport_reconnect_interval_test \ api_knet_handle_get_transport_reconnect_interval_test \ api_knet_recv_test \ api_knet_send_test \ api_knet_send_crypto_test \ api_knet_send_compress_test \ api_knet_send_sync_test \ api_knet_send_loopback_test \ api_knet_handle_pmtud_setfreq_test \ api_knet_handle_pmtud_getfreq_test \ api_knet_handle_enable_pmtud_notify_test \ api_knet_handle_pmtud_get_test \ api_knet_handle_pmtud_set_test \ api_knet_host_add_test \ api_knet_host_remove_test \ api_knet_host_set_name_test \ api_knet_host_get_name_by_host_id_test \ api_knet_host_get_id_by_host_name_test \ api_knet_host_get_host_list_test \ api_knet_host_set_policy_test \ api_knet_host_get_policy_test \ api_knet_host_get_status_test \ api_knet_host_enable_status_change_notify_test \ api_knet_log_get_subsystem_name_test \ api_knet_log_get_subsystem_id_test \ api_knet_log_get_loglevel_name_test \ api_knet_log_get_loglevel_id_test \ api_knet_log_set_loglevel_test \ api_knet_log_get_loglevel_test \ api_knet_strtoaddr_test \ api_knet_addrtostr_test \ api_knet_link_set_config_test \ api_knet_link_clear_config_test \ api_knet_link_get_config_test \ api_knet_link_set_ping_timers_test \ api_knet_link_get_ping_timers_test \ api_knet_link_set_pong_count_test \ api_knet_link_get_pong_count_test \ api_knet_link_set_priority_test \ api_knet_link_get_priority_test \ api_knet_link_set_enable_test \ api_knet_link_get_enable_test \ api_knet_link_get_link_list_test \ api_knet_link_get_status_test \ api_knet_link_enable_status_change_notify_test \ api_knet_handle_set_threads_timer_res_test \ api_knet_handle_get_threads_timer_res_test \ api_knet_link_add_acl_test \ api_knet_link_insert_acl_test \ api_knet_link_rm_acl_test \ api_knet_link_clear_acl_test \ api_knet_handle_crypto_set_config_test \ api_knet_handle_crypto_use_config_test \ api_knet_handle_crypto_rx_clear_traffic_test \ api_knet_handle_enable_onwire_ver_notify_test \ api_knet_handle_get_onwire_ver_test \ api_knet_handle_set_onwire_ver_test \ api_knet_handle_get_host_defrag_bufs_test \ api_knet_handle_set_host_defrag_bufs_test api_knet_handle_new_test_SOURCES = api_knet_handle_new.c \ test-common.c api_knet_handle_free_test_SOURCES = api_knet_handle_free.c \ test-common.c api_knet_handle_new_limit_test_SOURCES = api_knet_handle_new_limit.c \ test-common.c api_knet_handle_compress_test_SOURCES = api_knet_handle_compress.c \ test-common.c api_knet_handle_setfwd_test_SOURCES = api_knet_handle_setfwd.c \ test-common.c api_knet_handle_enable_access_lists_test_SOURCES = api_knet_handle_enable_access_lists.c \ test-common.c api_knet_handle_enable_filter_test_SOURCES = api_knet_handle_enable_filter.c \ test-common.c api_knet_handle_enable_sock_notify_test_SOURCES = api_knet_handle_enable_sock_notify.c \ test-common.c api_knet_handle_add_datafd_test_SOURCES = api_knet_handle_add_datafd.c \ test-common.c api_knet_handle_remove_datafd_test_SOURCES = api_knet_handle_remove_datafd.c \ test-common.c api_knet_handle_get_channel_test_SOURCES = api_knet_handle_get_channel.c \ test-common.c api_knet_handle_get_datafd_test_SOURCES = api_knet_handle_get_datafd.c \ test-common.c api_knet_handle_get_stats_test_SOURCES = api_knet_handle_get_stats.c \ test-common.c api_knet_get_crypto_list_test_SOURCES = api_knet_get_crypto_list.c \ test-common.c api_knet_get_compress_list_test_SOURCES = api_knet_get_compress_list.c \ test-common.c api_knet_handle_clear_stats_test_SOURCES = api_knet_handle_clear_stats.c \ test-common.c api_knet_get_transport_list_test_SOURCES = api_knet_get_transport_list.c \ test-common.c api_knet_get_transport_name_by_id_test_SOURCES = api_knet_get_transport_name_by_id.c \ test-common.c api_knet_get_transport_id_by_name_test_SOURCES = api_knet_get_transport_id_by_name.c \ test-common.c api_knet_handle_set_transport_reconnect_interval_test_SOURCES = api_knet_handle_set_transport_reconnect_interval.c \ test-common.c api_knet_handle_get_transport_reconnect_interval_test_SOURCES = api_knet_handle_get_transport_reconnect_interval.c \ test-common.c api_knet_recv_test_SOURCES = api_knet_recv.c \ test-common.c api_knet_send_test_SOURCES = api_knet_send.c \ test-common.c api_knet_send_compress_test_SOURCES = api_knet_send_compress.c \ test-common.c api_knet_send_crypto_test_SOURCES = api_knet_send_crypto.c \ test-common.c api_knet_send_loopback_test_SOURCES = api_knet_send_loopback.c \ test-common.c api_knet_send_sync_test_SOURCES = api_knet_send_sync.c \ test-common.c api_knet_handle_pmtud_setfreq_test_SOURCES = api_knet_handle_pmtud_setfreq.c \ test-common.c api_knet_handle_pmtud_getfreq_test_SOURCES = api_knet_handle_pmtud_getfreq.c \ test-common.c api_knet_handle_enable_pmtud_notify_test_SOURCES = api_knet_handle_enable_pmtud_notify.c \ test-common.c api_knet_handle_pmtud_get_test_SOURCES = api_knet_handle_pmtud_get.c \ test-common.c api_knet_handle_pmtud_set_test_SOURCES = api_knet_handle_pmtud_set.c \ test-common.c api_knet_host_add_test_SOURCES = api_knet_host_add.c \ test-common.c api_knet_host_remove_test_SOURCES = api_knet_host_remove.c \ test-common.c api_knet_host_set_name_test_SOURCES = api_knet_host_set_name.c \ test-common.c api_knet_host_get_name_by_host_id_test_SOURCES = api_knet_host_get_name_by_host_id.c \ test-common.c api_knet_host_get_id_by_host_name_test_SOURCES = api_knet_host_get_id_by_host_name.c \ test-common.c api_knet_host_get_host_list_test_SOURCES = api_knet_host_get_host_list.c \ test-common.c api_knet_host_set_policy_test_SOURCES = api_knet_host_set_policy.c \ test-common.c api_knet_host_get_policy_test_SOURCES = api_knet_host_get_policy.c \ test-common.c api_knet_host_get_status_test_SOURCES = api_knet_host_get_status.c \ test-common.c api_knet_host_enable_status_change_notify_test_SOURCES = api_knet_host_enable_status_change_notify.c \ test-common.c api_knet_log_get_subsystem_name_test_SOURCES = api_knet_log_get_subsystem_name.c \ test-common.c api_knet_log_get_subsystem_id_test_SOURCES = api_knet_log_get_subsystem_id.c \ test-common.c api_knet_log_get_loglevel_name_test_SOURCES = api_knet_log_get_loglevel_name.c \ test-common.c api_knet_log_get_loglevel_id_test_SOURCES = api_knet_log_get_loglevel_id.c \ test-common.c api_knet_log_set_loglevel_test_SOURCES = api_knet_log_set_loglevel.c \ test-common.c api_knet_log_get_loglevel_test_SOURCES = api_knet_log_get_loglevel.c \ test-common.c api_knet_strtoaddr_test_SOURCES = api_knet_strtoaddr.c api_knet_addrtostr_test_SOURCES = api_knet_addrtostr.c api_knet_link_set_config_test_SOURCES = api_knet_link_set_config.c \ test-common.c api_knet_link_clear_config_test_SOURCES = api_knet_link_clear_config.c \ test-common.c api_knet_link_get_config_test_SOURCES = api_knet_link_get_config.c \ test-common.c api_knet_link_set_ping_timers_test_SOURCES = api_knet_link_set_ping_timers.c \ test-common.c api_knet_link_get_ping_timers_test_SOURCES = api_knet_link_get_ping_timers.c \ test-common.c api_knet_link_set_pong_count_test_SOURCES = api_knet_link_set_pong_count.c \ test-common.c api_knet_link_get_pong_count_test_SOURCES = api_knet_link_get_pong_count.c \ test-common.c api_knet_link_set_priority_test_SOURCES = api_knet_link_set_priority.c \ test-common.c api_knet_link_get_priority_test_SOURCES = api_knet_link_get_priority.c \ test-common.c api_knet_link_set_enable_test_SOURCES = api_knet_link_set_enable.c \ test-common.c api_knet_link_get_enable_test_SOURCES = api_knet_link_get_enable.c \ test-common.c api_knet_link_get_link_list_test_SOURCES = api_knet_link_get_link_list.c \ test-common.c api_knet_link_get_status_test_SOURCES = api_knet_link_get_status.c \ test-common.c api_knet_link_enable_status_change_notify_test_SOURCES = api_knet_link_enable_status_change_notify.c \ test-common.c api_knet_handle_set_threads_timer_res_test_SOURCES = api_knet_handle_set_threads_timer_res.c \ test-common.c api_knet_handle_get_threads_timer_res_test_SOURCES = api_knet_handle_get_threads_timer_res.c \ test-common.c api_knet_link_add_acl_test_SOURCES = api_knet_link_add_acl.c \ test-common.c api_knet_link_insert_acl_test_SOURCES = api_knet_link_insert_acl.c \ test-common.c api_knet_link_rm_acl_test_SOURCES = api_knet_link_rm_acl.c \ test-common.c api_knet_link_clear_acl_test_SOURCES = api_knet_link_clear_acl.c \ test-common.c api_knet_handle_crypto_set_config_test_SOURCES = api_knet_handle_crypto_set_config.c \ test-common.c api_knet_handle_crypto_use_config_test_SOURCES = api_knet_handle_crypto_use_config.c \ test-common.c api_knet_handle_crypto_rx_clear_traffic_test_SOURCES = api_knet_handle_crypto_rx_clear_traffic.c \ test-common.c api_knet_handle_enable_onwire_ver_notify_test_SOURCES = api_knet_handle_enable_onwire_ver_notify.c \ test-common.c api_knet_handle_get_onwire_ver_test_SOURCES = api_knet_handle_get_onwire_ver.c \ test-common.c api_knet_handle_set_onwire_ver_test_SOURCES = api_knet_handle_set_onwire_ver.c \ test-common.c api_knet_handle_get_host_defrag_bufs_test_SOURCES = api_knet_handle_get_host_defrag_bufs.c \ test-common.c api_knet_handle_set_host_defrag_bufs_test_SOURCES = api_knet_handle_set_host_defrag_bufs.c \ test-common.c diff --git a/libknet/tests/api-test-coverage b/libknet/tests/api-test-coverage index fd2fce93..f99936ed 100755 --- a/libknet/tests/api-test-coverage +++ b/libknet/tests/api-test-coverage @@ -1,164 +1,164 @@ #!/bin/sh # -# Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # srcdir="$1"/libknet/tests builddir="$2"/libknet/tests headerapicalls="$(grep knet_ "$srcdir"/../libknet.h | grep -v "^ \*" | grep -v ^struct | grep -v "^[[:space:]]" | grep -v typedef | sed -e 's/(.*//g' -e 's/^const //g' -e 's/\*//g' | awk '{print $2}')" # The PowerPC64 ELFv1 ABI defines the address of a function as that of a # function descriptor defined in .opd, a data (D) section. Other ABIs # use the entry address of the function itself in the text (T) section. exportedapicalls="$(nm -B -D "$builddir"/../.libs/libknet.so | grep ' [DT] ' | awk '{print $3}' | sed -e 's#@@LIBKNET##g')" echo "Checking for exported symbols NOT available in header file" for i in $exportedapicalls; do found=0 for x in $headerapicalls; do if [ "$x" = "$i" ]; then found=1 break; fi done if [ "$found" = 0 ]; then echo "Symbol $i not found in header file" exit 1 fi done echo "Checking for symbols in header file NOT exported by binary lib" for i in $headerapicalls; do found=0 for x in $exportedapicalls; do if [ "$x" = "$i" ]; then found=1 break; fi done if [ "$found" = 0 ]; then echo "Symbol $i not found in binary lib" exit 1 fi done echo "Checking for tests with memcheck exceptions" for i in $(grep -l is_memcheck "$srcdir"/*.c | grep -v test-common); do echo "WARNING: $(basename $i) - has memcheck exception enabled" done echo "Checking for tests with helgrind exceptions" for i in $(grep -l is_helgrind "$srcdir"/*.c | grep -v test-common); do echo "WARNING: $(basename $i) has helgrind exception enabled" done echo "Checking for api test coverage" numapicalls=0 found=0 missing=0 for i in $headerapicalls; do numapicalls=$((numapicalls + 1)) if [ -f $srcdir/api_${i}.c ]; then found=$((found + 1)) else missing=$((missing + 1)) echo "MISSING: $i" fi done # Check Rust bindings coverage rust_found=0 rust_missing=0 # These are implemented directly in the Rust code deliberately_missing="knet_strtoaddr knet_addrtostr knet_get_transport_name_by_id knet_get_transport_id_by_name" rustapicalls=$numapicalls for i in $headerapicalls; do rustcall=`echo $i|awk '{print substr($0, 6)}'` grep "^pub fn ${rustcall}(" $1/libknet/bindings/rust/src/knet_bindings.rs > /dev/null 2>/dev/null if [ $? = 0 ] then rust_found=$((rust_found+1)) else echo $deliberately_missing | grep $i 2>/dev/null >/dev/null if [ $? != 0 ] then echo "$i Missing from Rust API" rust_missing=$((rust_missing+1)) else rustapicalls=$((rustapicalls-1)) fi fi done # Check Rust test coverage rust_test_found=0 rust_test_missing=0 deliberately_missing="knet_strtoaddr knet_addrtostr knet_get_transport_name_by_id knet_get_transport_id_by_name" rust_testapicalls=$numapicalls for i in $headerapicalls; do rustcall=`echo $i|awk '{print substr($0, 6)}'` grep "knet::${rustcall}(" $1/libknet/bindings/rust/tests/src/bin/knet-test.rs > /dev/null 2>/dev/null if [ $? = 0 ] then rust_test_found=$((rust_test_found+1)) else echo $deliberately_missing | grep $i 2>/dev/null >/dev/null if [ $? != 0 ] then echo "$i Missing from Rust test" rust_test_missing=$((rust_test_missing+1)) else rust_testapicalls=$((rust_testapicalls-1)) fi fi done echo "" echo "Summary" echo "-------" echo "Found : $found" echo "Missing : $missing" echo "Total : $numapicalls" which bc > /dev/null 2>&1 && { coverage=$(echo "scale=3; $found / $numapicalls * 100" | bc -l) echo "Coverage: $coverage%" } echo echo "Rust API Summary" echo "----------------" echo "Found : $rust_found" echo "Missing : $rust_missing" echo "Total : $rustapicalls" which bc > /dev/null 2>&1 && { coverage=$(echo "scale=3; $rust_found / $rustapicalls * 100" | bc -l) echo "Coverage: $coverage%" } echo echo "Rust test Summary" echo "-----------------" echo "Found : $rust_test_found" echo "Missing : $rust_test_missing" echo "Total : $rustapicalls" which bc > /dev/null 2>&1 && { coverage=$(echo "scale=3; $rust_test_found / $rust_testapicalls * 100" | bc -l) echo "Coverage: $coverage%" } exit 0 diff --git a/libknet/tests/api_knet_addrtostr.c b/libknet/tests/api_knet_addrtostr.c index 3a0f2bc8..26279ce8 100644 --- a/libknet/tests/api_knet_addrtostr.c +++ b/libknet/tests/api_knet_addrtostr.c @@ -1,127 +1,127 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "libknet.h" #include "test-common.h" static void test(void) { struct sockaddr_storage addr; struct sockaddr_in *addrv4; struct sockaddr_in6 *addrv6; char addr_str[KNET_MAX_HOST_LEN]; char port_str[KNET_MAX_PORT_LEN]; memset(&addr, 0, sizeof(struct sockaddr_storage)); printf("Checking knet_addrtostr with invalid ss\n"); if (!knet_addrtostr(NULL, sizeof(struct sockaddr_storage), addr_str, KNET_MAX_HOST_LEN, port_str, KNET_MAX_PORT_LEN) || (errno != EINVAL)) { printf("knet_addrtostr accepted invalid ss or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Checking knet_addrtostr with invalid sslen\n"); if (!knet_addrtostr(&addr, 0, addr_str, KNET_MAX_HOST_LEN, port_str, KNET_MAX_PORT_LEN) || (errno != EINVAL)) { printf("knet_addrtostr accepted invalid sslen or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Checking knet_addrtostr with invalid addr_str\n"); if (!knet_addrtostr(&addr, sizeof(struct sockaddr_storage), NULL, KNET_MAX_HOST_LEN, port_str, KNET_MAX_PORT_LEN) || (errno != EINVAL)) { printf("knet_addrtostr accepted invalid addr_str or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Checking knet_addrtostr with invalid port_str\n"); if (!knet_addrtostr(&addr, sizeof(struct sockaddr_storage), addr_str, KNET_MAX_HOST_LEN, NULL, KNET_MAX_PORT_LEN) || (errno != EINVAL)) { printf("knet_addrtostr accepted invalid addr_str or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } addrv4 = (struct sockaddr_in *)&addr; addrv4->sin_family = AF_INET; addrv4->sin_addr.s_addr = htonl(0xc0a80001); /* 192.168.0.1 */ addrv4->sin_port = htons(50000); printf("Checking knet_addrtostr with valid data (192.168.0.1:50000)\n"); if (knet_addrtostr(&addr, sizeof(struct sockaddr_storage), addr_str, KNET_MAX_HOST_LEN, port_str, KNET_MAX_PORT_LEN) < 0) { printf("Unable to convert 192.168.0.1:50000\n"); exit(FAIL); } if (strcmp(addr_str, "192.168.0.1") != 0) { printf("Wrong address conversion: %s\n", addr_str); exit(EXIT_FAILURE); } if (strcmp(port_str, "50000") != 0) { printf("Wrong port conversion: %s\n", port_str); exit(EXIT_FAILURE); } printf("Checking knet_addrtostr with valid data ([fd00::1]:50000)\n"); memset(&addr, 0, sizeof(struct sockaddr_storage)); addrv6 = (struct sockaddr_in6 *)&addr; addrv6->sin6_family = AF_INET6; addrv6->sin6_addr.s6_addr16[0] = htons(0xfd00); /* fd00::1 */ addrv6->sin6_addr.s6_addr16[7] = htons(0x0001); addrv6->sin6_port = htons(50000); if (knet_addrtostr(&addr, sizeof(struct sockaddr_storage), addr_str, KNET_MAX_HOST_LEN, port_str, KNET_MAX_PORT_LEN) < 0) { printf("Unable to convert [fd00::1]:50000\n"); exit(FAIL); } if (strcmp(addr_str, "fd00::1") != 0) { printf("Wrong address conversion: %s\n", addr_str); exit(FAIL); } if (strcmp(port_str, "50000") != 0) { printf("Wrong port conversion: %s\n", port_str); exit(EXIT_FAILURE); } } int main(int argc, char *argv[]) { test(); exit(PASS); } diff --git a/libknet/tests/api_knet_get_compress_list.c b/libknet/tests/api_knet_get_compress_list.c index 4e02218d..e4ca0a94 100644 --- a/libknet/tests/api_knet_get_compress_list.c +++ b/libknet/tests/api_knet_get_compress_list.c @@ -1,68 +1,68 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { struct knet_compress_info compress_list[16]; size_t compress_list_entries; size_t compress_list_entries1; size_t i; memset(compress_list, 0, sizeof(compress_list)); printf("Test knet_get_compress_list with no entries_list\n"); if ((!knet_get_compress_list(compress_list, NULL)) || (errno != EINVAL)) { printf("knet_get_compress_list accepted invalid list_entries or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_compress_list with no compress_list (get number of entries)\n"); if (knet_get_compress_list(NULL, &compress_list_entries) < 0) { printf("knet_handle_get_compress_list returned error instead of number of entries: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_compress_list with valid data\n"); if (knet_get_compress_list(compress_list, &compress_list_entries1) < 0) { printf("knet_get_compress_list failed: %s\n", strerror(errno)); exit(FAIL); } if (compress_list_entries != compress_list_entries1) { printf("knet_get_compress_list returned a different number of entries: %d, %d\n", (int)compress_list_entries, (int)compress_list_entries1); exit(FAIL); } for (i=0; i * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { struct knet_crypto_info crypto_list[16]; size_t crypto_list_entries; size_t crypto_list_entries1; size_t i; memset(crypto_list, 0, sizeof(crypto_list)); printf("Test knet_handle_get_crypto_list with no entries_list\n"); if ((!knet_get_crypto_list(crypto_list, NULL)) || (errno != EINVAL)) { printf("knet_get_crypto_list accepted invalid list_entries or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_crypto_list with no crypto_list (get number of entries)\n"); if (knet_get_crypto_list(NULL, &crypto_list_entries) < 0) { printf("knet_handle_get_crypto_list returned error instead of number of entries: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_crypto_list with valid data\n"); if (knet_get_crypto_list(crypto_list, &crypto_list_entries1) < 0) { printf("knet_get_crypto_list failed: %s\n", strerror(errno)); exit(FAIL); } if (crypto_list_entries != crypto_list_entries1) { printf("knet_get_crypto_list returned a different number of entries: %d, %d\n", (int)crypto_list_entries, (int)crypto_list_entries1); exit(FAIL); } for (i=0; i * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { uint8_t id; printf("Test knet_get_transport_id_by_name with no name\n"); if ((knet_get_transport_id_by_name(NULL) != KNET_MAX_TRANSPORTS) || (errno != EINVAL)) { printf("knet_get_transport_id_by_name accepted invalid transport or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_transport_id_by_name with incorrect name\n"); if ((knet_get_transport_id_by_name("ARP") != KNET_MAX_TRANSPORTS) || (errno != EINVAL)) { printf("knet_get_transport_id_by_name accepted invalid transport or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_transport_id_by_name with correct values\n"); id = knet_get_transport_id_by_name("UDP"); if (id != KNET_TRANSPORT_UDP) { printf("knet_get_transport_id_by_name failed: %s\n", strerror(errno)); exit(FAIL); } } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_get_transport_list.c b/libknet/tests/api_knet_get_transport_list.c index c7c3f571..0e7c8e7b 100644 --- a/libknet/tests/api_knet_get_transport_list.c +++ b/libknet/tests/api_knet_get_transport_list.c @@ -1,69 +1,69 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { struct knet_transport_info transport_list[KNET_MAX_TRANSPORTS]; size_t transport_list_entries; size_t transport_list_entries1; size_t i; memset(transport_list, 0, sizeof(transport_list)); printf("Test knet_get_transport_list with no entries_list\n"); if ((!knet_get_transport_list(transport_list, NULL)) || (errno != EINVAL)) { printf("knet_get_transport_list accepted invalid list_entries or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_transport_list with no transport_list (get number of entries)\n"); if (knet_get_transport_list(NULL, &transport_list_entries) < 0) { printf("knet_get_transport_list returned error instead of number of entries: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_transport_list with valid data\n"); if (knet_get_transport_list(transport_list, &transport_list_entries1) < 0) { printf("knet_get_transport_list failed: %s\n", strerror(errno)); exit(FAIL); } if (transport_list_entries != transport_list_entries1) { printf("knet_get_transport_list returned a different number of entries: %d, %d\n", (int)transport_list_entries, (int)transport_list_entries1); exit(FAIL); } for (i=0; i * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { const char *name = NULL; printf("Test knet_get_transport_name_by_id with incorrect transport\n"); if ((knet_get_transport_name_by_id(KNET_MAX_TRANSPORTS) != NULL) || (errno != EINVAL)) { printf("knet_get_transport_name_by_id accepted invalid transport or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_get_transport_name_by_id with correct values\n"); name = knet_get_transport_name_by_id(KNET_TRANSPORT_UDP); if (!name) { printf("knet_get_transport_name_by_id failed: %s\n", strerror(errno)); exit(FAIL); } if (strcmp(name, "UDP")) { printf("knet_get_transport_name_by_id failed to get UDP transport name\n"); exit(FAIL); } } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_add_datafd.c b/libknet/tests/api_knet_handle_add_datafd.c index eef793fd..7becf098 100644 --- a/libknet/tests/api_knet_handle_add_datafd.c +++ b/libknet/tests/api_knet_handle_add_datafd.c @@ -1,117 +1,117 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(void) { knet_handle_t knet_h[2]; knet_handle_t knet_h1; int res; int logfds[2]; int datafd = 0, i; int8_t channel = 0; int datafdmax[KNET_DATAFD_MAX]; int8_t channels[KNET_DATAFD_MAX]; printf("Test knet_handle_add_datafd incorrect knet_h\n"); if ((!knet_handle_add_datafd(NULL, &datafd, &channel)) || (errno != EINVAL)) { printf("knet_handle_add_datafd accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_add_datafd with no datafd\n"); FAIL_ON_SUCCESS(knet_handle_add_datafd(knet_h1, NULL, &channel), EINVAL); printf("Test knet_handle_add_datafd with no channel\n"); FAIL_ON_SUCCESS(knet_handle_add_datafd(knet_h1, &datafd, NULL), EINVAL); printf("Test knet_handle_add_datafd with invalid channel\n"); channel = KNET_DATAFD_MAX; FAIL_ON_SUCCESS(knet_handle_add_datafd(knet_h1, &datafd, &channel), EINVAL); printf("Test knet_handle_add_datafd with no socknotify\n"); datafd = 0; channel = -1; FAIL_ON_SUCCESS(knet_handle_add_datafd(knet_h1, &datafd, &channel), EINVAL); printf("Test knet_handle_add_datafd with automatic config values\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); printf("got datafd: %d channel: %d\n", datafd, channel); printf("Test knet_handle_add_datafd with duplicated datafd\n"); FAIL_ON_SUCCESS(knet_handle_add_datafd(knet_h1, &datafd, &channel), EEXIST); printf("Test knet_handle_add_datafd with busy channel\n"); datafd = datafd + 1; FAIL_ON_SUCCESS(knet_handle_add_datafd(knet_h1, &datafd, &channel), EBUSY); datafd = datafd - 1; FAIL_ON_ERR(knet_handle_remove_datafd(knet_h1, datafd)); printf("Test knet_handle_add_datafd with no available channels\n"); for (i = 0; i < KNET_DATAFD_MAX; i++) { datafdmax[i] = 0; channels[i] = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafdmax[i], &channels[i])); } datafd = 0; channel = -1; FAIL_ON_SUCCESS(knet_handle_add_datafd(knet_h1, &datafd, &channel), EBUSY); for (i = 0; i < KNET_DATAFD_MAX; i++) { FAIL_ON_ERR(knet_handle_remove_datafd(knet_h1, datafdmax[i])); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_clear_stats.c b/libknet/tests/api_knet_handle_clear_stats.c index 37df5d48..d71cd22f 100644 --- a/libknet/tests/api_knet_handle_clear_stats.c +++ b/libknet/tests/api_knet_handle_clear_stats.c @@ -1,155 +1,155 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int datafd = 0; int8_t channel = 0; struct knet_link_status link_status; char send_buff[KNET_MAX_PACKET_SIZE]; char recv_buff[KNET_MAX_PACKET_SIZE]; ssize_t send_len = 0; int recv_len = 0; int savederrno; int res; struct sockaddr_storage lo; memset(send_buff, 0, sizeof(send_buff)); printf("Test knet_handle_clear_stats incorrect knet_h\n"); if (!knet_handle_clear_stats(NULL, 0) || (errno != EINVAL)) { printf("knet_handle_clear_stats accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_send with valid data\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); FAIL_ON_ERR(wait_for_host(knet_h1, 1, 10, logfds[0], stdout)); send_len = knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel); if (send_len <= 0) { printf("knet_send failed: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } if (send_len != sizeof(send_buff)) { CLEAN_EXIT(FAIL); } FAIL_ON_ERR(wait_for_packet(knet_h1, 10, datafd, logfds[0], stdout)); recv_len = knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel); savederrno = errno; if (recv_len != send_len) { printf("knet_recv received only %d bytes: %s (errno: %d)\n", recv_len, strerror(errno), errno); if ((is_helgrind()) && (recv_len == -1) && (savederrno == EAGAIN)) { printf("helgrind exception. this is normal due to possible timeouts\n"); CLEAN_EXIT(PASS); } CLEAN_EXIT(FAIL); } if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { printf("recv and send buffers are different!\n"); CLEAN_EXIT(FAIL); } /* A sanity check on the stats */ FAIL_ON_ERR(knet_link_get_status(knet_h1, 1, 0, &link_status, sizeof(link_status))); if (link_status.stats.tx_data_packets != 2 || link_status.stats.rx_data_packets != 2 || link_status.stats.tx_data_bytes < KNET_MAX_PACKET_SIZE || link_status.stats.rx_data_bytes < KNET_MAX_PACKET_SIZE || link_status.stats.tx_data_bytes > KNET_MAX_PACKET_SIZE*2 || link_status.stats.rx_data_bytes > KNET_MAX_PACKET_SIZE*2) { printf("stats look wrong: tx_packets: %" PRIu64 " (%" PRIu64 " bytes), rx_packets: %" PRIu64 " (%" PRIu64 " bytes)\n", link_status.stats.tx_data_packets, link_status.stats.tx_data_bytes, link_status.stats.rx_data_packets, link_status.stats.rx_data_bytes); } printf("Test knet_clear_stats (link)\n"); FAIL_ON_ERR(knet_handle_clear_stats(knet_h1, KNET_CLEARSTATS_HANDLE_AND_LINK)); /* Check they've been cleared */ FAIL_ON_ERR(knet_link_get_status(knet_h1, 1, 0, &link_status, sizeof(link_status))); if (link_status.stats.tx_data_packets != 0 || link_status.stats.rx_data_packets != 0 || link_status.stats.tx_data_bytes != 0 || link_status.stats.rx_data_bytes != 0 || link_status.stats.tx_data_bytes != 0 || link_status.stats.rx_data_bytes != 0) { printf("stats not cleared: tx_packets: %" PRIu64 " (%" PRIu64 " bytes), rx_packets: %" PRIu64 " (%" PRIu64 " bytes)\n", link_status.stats.tx_data_packets, link_status.stats.tx_data_bytes, link_status.stats.rx_data_packets, link_status.stats.rx_data_bytes); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_compress.c b/libknet/tests/api_knet_handle_compress.c index 51c5bdbe..4caa3e75 100644 --- a/libknet/tests/api_knet_handle_compress.c +++ b/libknet/tests/api_knet_handle_compress.c @@ -1,124 +1,124 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; struct knet_handle_compress_cfg knet_handle_compress_cfg; memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); setup_logpipes(logfds); printf("Test knet_handle_compress incorrect knet_h\n"); if ((!knet_handle_compress(NULL, &knet_handle_compress_cfg)) || (errno != EINVAL)) { printf("knet_handle_compress accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_compress with invalid cfg\n"); FAIL_ON_SUCCESS(knet_handle_compress(knet_h1, NULL), EINVAL); printf("Test knet_handle_compress with un-initialized cfg\n"); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); FAIL_ON_SUCCESS(knet_handle_compress(knet_h1, &knet_handle_compress_cfg), EINVAL); printf("Test knet_handle_compress with none compress model (disable compress)\n"); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); strncpy(knet_handle_compress_cfg.compress_model, "none", sizeof(knet_handle_compress_cfg.compress_model) - 1); FAIL_ON_ERR(knet_handle_compress(knet_h1, &knet_handle_compress_cfg)); #if WITH_COMPRESS_BZIP2 > 0 printf("Test knet_handle_compress with bzip2 (no default) with negative level (-3)\n"); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); strncpy(knet_handle_compress_cfg.compress_model, "bzip2", sizeof(knet_handle_compress_cfg.compress_model) - 1); knet_handle_compress_cfg.compress_level = -3; FAIL_ON_SUCCESS(knet_handle_compress(knet_h1, &knet_handle_compress_cfg), EINVAL); #endif printf("Test knet_handle_compress with zlib compress and not effective compression level (0)\n"); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); strncpy(knet_handle_compress_cfg.compress_model, "zlib", sizeof(knet_handle_compress_cfg.compress_model) - 1); knet_handle_compress_cfg.compress_level = 0; FAIL_ON_ERR(knet_handle_compress(knet_h1, &knet_handle_compress_cfg)); printf("Test knet_handle_compress with zlib compress and negative level (-2)\n"); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); strncpy(knet_handle_compress_cfg.compress_model, "zlib", sizeof(knet_handle_compress_cfg.compress_model) - 1); knet_handle_compress_cfg.compress_level = -2; FAIL_ON_SUCCESS(knet_handle_compress(knet_h1, &knet_handle_compress_cfg), EINVAL); printf("Test knet_handle_compress with zlib compress and excessive compress level\n"); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); strncpy(knet_handle_compress_cfg.compress_model, "zlib", sizeof(knet_handle_compress_cfg.compress_model) - 1); knet_handle_compress_cfg.compress_level = 10; FAIL_ON_SUCCESS(knet_handle_compress(knet_h1, &knet_handle_compress_cfg), EINVAL); printf("Test knet_handle_compress with zlib compress and excessive compress threshold\n"); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); strncpy(knet_handle_compress_cfg.compress_model, "zlib", sizeof(knet_handle_compress_cfg.compress_model) - 1); knet_handle_compress_cfg.compress_level = 1; knet_handle_compress_cfg.compress_threshold = KNET_MAX_PACKET_SIZE +1; FAIL_ON_SUCCESS(knet_handle_compress(knet_h1, &knet_handle_compress_cfg), EINVAL); printf("Test knet_handle_compress with zlib compress model normal compress level and threshold\n"); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); strncpy(knet_handle_compress_cfg.compress_model, "zlib", sizeof(knet_handle_compress_cfg.compress_model) - 1); knet_handle_compress_cfg.compress_level = 1; knet_handle_compress_cfg.compress_threshold = 64; FAIL_ON_ERR(knet_handle_compress(knet_h1, &knet_handle_compress_cfg)); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { struct knet_compress_info compress_list[16]; size_t compress_list_entries; size_t i; memset(compress_list, 0, sizeof(compress_list)); if (knet_get_compress_list(compress_list, &compress_list_entries) < 0) { printf("knet_get_compress_list failed: %s\n", strerror(errno)); return FAIL; } if (compress_list_entries == 0) { printf("no compression modules detected. Skipping\n"); return SKIP; } for (i=0; i < compress_list_entries; i++) { if (!strcmp(compress_list[i].name, "zlib")) { test(); return PASS; } } printf("WARNING: zlib support not builtin the library. Unable to test/verify internal compress API calls\n"); return SKIP; } diff --git a/libknet/tests/api_knet_handle_crypto_rx_clear_traffic.c b/libknet/tests/api_knet_handle_crypto_rx_clear_traffic.c index 5a7da4ea..c834a514 100644 --- a/libknet/tests/api_knet_handle_crypto_rx_clear_traffic.c +++ b/libknet/tests/api_knet_handle_crypto_rx_clear_traffic.c @@ -1,63 +1,63 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "crypto_model.h" #include "test-common.h" static void test() { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_handle_crypto_rx_clear_traffic incorrect knet_h\n"); if ((!knet_handle_crypto_rx_clear_traffic(NULL, 1)) || (errno != EINVAL)) { printf("knet_handle_crypto_rx_clear_traffic accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_crypto_rx_clear_traffic with invalid value\n"); FAIL_ON_SUCCESS(knet_handle_crypto_rx_clear_traffic(knet_h1, 2), EINVAL); printf("Test knet_handle_crypto_rx_clear_traffic with valid value KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC\n"); FAIL_ON_ERR(knet_handle_crypto_rx_clear_traffic(knet_h1, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC)); if (knet_h1->crypto_only != KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC) { printf("knet_handle_crypto_rx_clear_traffic failed to set correct value\n"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_crypto_rx_clear_traffic with valid value KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC\n"); FAIL_ON_ERR(knet_handle_crypto_rx_clear_traffic(knet_h1, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC)); if (knet_h1->crypto_only != KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC) { printf("knet_handle_crypto_rx_clear_traffic failed to set correct value\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_crypto_set_config.c b/libknet/tests/api_knet_handle_crypto_set_config.c index cff7c83d..2848eef5 100644 --- a/libknet/tests/api_knet_handle_crypto_set_config.c +++ b/libknet/tests/api_knet_handle_crypto_set_config.c @@ -1,232 +1,232 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "crypto_model.h" #include "test-common.h" static void test(const char *model, const char *model2) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct knet_handle_crypto_cfg knet_handle_crypto_cfg; struct crypto_instance *current = NULL; memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); printf("Test knet_handle_crypto_set_config incorrect knet_h\n"); if ((!knet_handle_crypto_set_config(NULL, &knet_handle_crypto_cfg, 1)) || (errno != EINVAL)) { printf("knet_handle_crypto_set_config accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_crypto_set_config with invalid cfg\n"); FAIL_ON_SUCCESS(knet_handle_crypto_set_config(knet_h1, NULL, 1), EINVAL); printf("Test knet_handle_crypto_set_config with invalid config num\n"); FAIL_ON_SUCCESS(knet_handle_crypto_set_config(knet_h1, NULL, KNET_MAX_CRYPTO_INSTANCES + 1), EINVAL); printf("Test knet_handle_crypto_set_config with un-initialized cfg\n"); FAIL_ON_SUCCESS(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1), EINVAL); printf("Test knet_handle_crypto_set_config with none crypto model (disable crypto)\n"); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); printf("Test knet_handle_crypto_set_config with none crypto cipher and hash (disable crypto)\n"); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); printf("Test knet_handle_crypto_set_config with %s/aes128/sha1 and too short key\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 10; FAIL_ON_SUCCESS(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1), EINVAL); printf("Test knet_handle_crypto_set_config with %s/aes128/sha1 and too long key\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 10000; FAIL_ON_SUCCESS(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1), EINVAL); printf("Test knet_handle_crypto_set_config with %s/aes128/sha1 and normal key\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); printf("Test knet_handle_crypto_set_config reconfig with %s/aes128/sha1 and normal key\n", model2); current = knet_h1->crypto_instance[1]; memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); if (current == knet_h1->crypto_instance[1]) { printf("knet_handle_crypto_set_config failed to install new correct config: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_handle_crypto_set_config reconfig with %s/aes128/sha1 and normal key\n", model); current = knet_h1->crypto_instance[1]; memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); if (current == knet_h1->crypto_instance[1]) { printf("knet_handle_crypto_set_config failed to install new correct config: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_handle_crypto_set_config reconfig with %s/aes129/sha1 and normal key\n", model); current = knet_h1->crypto_instance[1]; memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes129", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR_ONLY(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); if (current != knet_h1->crypto_instance[1]) { printf("knet_handle_crypto_set_config failed to restore correct config: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_handle_crypto_set_config with %s/aes128/none and normal key\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_SUCCESS(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1), EINVAL); printf("Test knet_handle_crypto_set_config with %s/aes128/sha1 and key where (key_len %% wrap_key_block_size != 0)\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); /* * Prime number so chance that (private_key_len % wrap_key_block_size == 0) is minimalized */ knet_handle_crypto_cfg.private_key_len = 2003; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); printf("Test knet_handle_crypto_set_config second with %s/aes128/sha1 and normal key\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 2)); if (!knet_h1->crypto_instance[2]) { printf("knet_handle_crypto_set_config failed to install second config but reported success\n"); CLEAN_EXIT(FAIL); } if (knet_h1->crypto_instance[1] == knet_h1->crypto_instance[2]) { printf("knet_handle_crypto_set_config failed to install second config and assigned to first\n"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_crypto_set_config second config BUSY test\n"); FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 2)); FAIL_ON_SUCCESS(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 2), EBUSY); FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 0)); printf("Shutdown crypto\n"); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); if (knet_h1->crypto_instance[1]) { printf("knet_handle_crypto_set_config failed to wipe first config but reported success\n"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 2)); if (knet_h1->crypto_instance[2]) { printf("knet_handle_crypto_set_config failed to wipe first config but reported success\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { struct knet_crypto_info crypto_list[16]; size_t crypto_list_entries; size_t i; memset(crypto_list, 0, sizeof(crypto_list)); if (knet_get_crypto_list(crypto_list, &crypto_list_entries) < 0) { printf("knet_get_crypto_list failed: %s\n", strerror(errno)); return FAIL; } if (crypto_list_entries == 0) { printf("no crypto modules detected. Skipping\n"); return SKIP; } for (i=0; i < crypto_list_entries; i++) { test(crypto_list[i].name, crypto_list[0].name); } return PASS; } diff --git a/libknet/tests/api_knet_handle_crypto_use_config.c b/libknet/tests/api_knet_handle_crypto_use_config.c index 3fcf4003..df1f8931 100644 --- a/libknet/tests/api_knet_handle_crypto_use_config.c +++ b/libknet/tests/api_knet_handle_crypto_use_config.c @@ -1,129 +1,129 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "crypto_model.h" #include "test-common.h" static void test(const char *model, const char *model2) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct knet_handle_crypto_cfg knet_handle_crypto_cfg; memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); printf("Test knet_handle_crypto_use_config incorrect knet_h\n"); if ((!knet_handle_crypto_use_config(NULL, 1)) || (errno != EINVAL)) { printf("knet_handle_crypto_use_config accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_crypto_use_config with invalid config num\n"); FAIL_ON_SUCCESS(knet_handle_crypto_use_config(knet_h1, KNET_MAX_CRYPTO_INSTANCES + 1), EINVAL); printf("Test knet_handle_crypto_use_config with un-initialized cfg\n"); FAIL_ON_SUCCESS(knet_handle_crypto_use_config(knet_h1, 1), EINVAL); FAIL_ON_SUCCESS(knet_handle_crypto_use_config(knet_h1, 2), EINVAL); printf("Test knet_handle_crypto_set_config with %s/aes128/sha1 and normal key\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); printf("Test knet_handle_crypto_use_config with un-initialized cfg (part 2)\n"); FAIL_ON_SUCCESS(knet_handle_crypto_use_config(knet_h1, 2), EINVAL); FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 1)); printf("Test knet_handle_crypto_set_config for second config with %s/aes128/sha1 and normal key\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha1", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 2)); printf("Test knet_handle_crypto_use_config with valid data\n"); FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 2)); if (knet_h1->crypto_in_use_config != 2) { printf("knet_handle_crypto_set_config failed to set crypto in-use config to 2\n"); CLEAN_EXIT(FAIL); } printf("Shutdown crypto\n"); printf("Test knet_handle_crypto_use_config with valid data\n"); FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 0)); if (knet_h1->crypto_in_use_config != 0) { printf("knet_handle_crypto_set_config failed to set crypto in-use config to 2\n"); CLEAN_EXIT(FAIL); } memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); if (knet_h1->crypto_instance[1]) { printf("knet_handle_crypto_set_config failed to wipe first config but reported success\n"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 2)); if (knet_h1->crypto_instance[2]) { printf("knet_handle_crypto_set_config failed to wipe first config but reported success\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { struct knet_crypto_info crypto_list[16]; size_t crypto_list_entries; size_t i; memset(crypto_list, 0, sizeof(crypto_list)); if (knet_get_crypto_list(crypto_list, &crypto_list_entries) < 0) { printf("knet_get_crypto_list failed: %s\n", strerror(errno)); return FAIL; } if (crypto_list_entries == 0) { printf("no crypto modules detected. Skipping\n"); return SKIP; } for (i=0; i < crypto_list_entries; i++) { test(crypto_list[i].name, crypto_list[0].name); } return PASS; } diff --git a/libknet/tests/api_knet_handle_enable_access_lists.c b/libknet/tests/api_knet_handle_enable_access_lists.c index f226b90d..ec75ff1a 100644 --- a/libknet/tests/api_knet_handle_enable_access_lists.c +++ b/libknet/tests/api_knet_handle_enable_access_lists.c @@ -1,65 +1,65 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; printf("Test knet_handle_enable_access_lists with invalid knet_h\n"); if ((!knet_handle_enable_access_lists(NULL, 0)) || (errno != EINVAL)) { printf("knet_handle_enable_access_lists accepted invalid knet_h parameter\n"); exit(FAIL); } setup_logpipes(logfds); printf("Test knet_handle_enable_access_lists with invalid param (2) \n"); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); FAIL_ON_SUCCESS(knet_handle_enable_access_lists(knet_h1, 2), EINVAL); printf("Test knet_handle_enable_access_lists with valid param (1) \n"); FAIL_ON_ERR(knet_handle_enable_access_lists(knet_h1, 1)); if (knet_h1->use_access_lists != 1) { printf("knet_handle_enable_access_lists failed to set correct value"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_access_lists with valid param (0) \n"); FAIL_ON_ERR(knet_handle_enable_access_lists(knet_h1, 0)); if (knet_h1->use_access_lists != 0) { printf("knet_handle_enable_access_lists failed to set correct value"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_enable_filter.c b/libknet/tests/api_knet_handle_enable_filter.c index c481c662..9071ff98 100644 --- a/libknet/tests/api_knet_handle_enable_filter.c +++ b/libknet/tests/api_knet_handle_enable_filter.c @@ -1,94 +1,94 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static int dhost_filter(void *pvt_data, const unsigned char *outdata, ssize_t outdata_len, uint8_t tx_rx, knet_node_id_t this_host_id, knet_node_id_t src_host_id, int8_t *dst_channel, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries) { return 0; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_handle_enable_filter incorrect knet_h\n"); if ((!knet_handle_enable_filter(NULL, NULL, dhost_filter)) || (errno != EINVAL)) { printf("knet_handle_enable_filter accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_enable_filter with no private_data\n"); FAIL_ON_ERR(knet_handle_enable_filter(knet_h1, NULL, dhost_filter)); if (knet_h1->dst_host_filter_fn_private_data != NULL) { printf("knet_handle_enable_filter failed to unset private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_filter with private_data\n"); FAIL_ON_ERR(knet_handle_enable_filter(knet_h1, &private_data, NULL)); if (knet_h1->dst_host_filter_fn_private_data != &private_data) { printf("knet_handle_enable_filter failed to set private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_filter with no dhost_filter fn\n"); FAIL_ON_ERR(knet_handle_enable_filter(knet_h1, NULL, NULL)); if (knet_h1->dst_host_filter_fn != NULL) { printf("knet_handle_enable_filter failed to unset dhost_filter fn"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_filter with dhost_filter fn\n"); FAIL_ON_ERR(knet_handle_enable_filter(knet_h1, NULL, dhost_filter)); if (knet_h1->dst_host_filter_fn != &dhost_filter) { printf("knet_handle_enable_filter failed to set dhost_filter fn"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_enable_onwire_ver_notify.c b/libknet/tests/api_knet_handle_enable_onwire_ver_notify.c index 7344bb59..b6a36bf3 100644 --- a/libknet/tests/api_knet_handle_enable_onwire_ver_notify.c +++ b/libknet/tests/api_knet_handle_enable_onwire_ver_notify.c @@ -1,84 +1,84 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void onwire_ver_notify(void *priv_data, uint8_t onwire_min_ver, uint8_t onwire_max_ver, uint8_t onwire_ver) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_handle_enable_onwire_ver_notify incorrect knet_h\n"); if ((!knet_handle_enable_onwire_ver_notify(NULL, NULL, onwire_ver_notify)) || (errno != EINVAL)) { printf("knet_handle_enable_onwire_ver_notify accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_enable_onwire_ver_notify with no private_data\n"); FAIL_ON_ERR(knet_handle_enable_onwire_ver_notify(knet_h1, NULL, onwire_ver_notify)); if (knet_h1->onwire_ver_notify_fn_private_data != NULL) { printf("knet_handle_enable_onwire_ver_notify failed to unset private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_onwire_ver_notify with private_data\n"); FAIL_ON_ERR(knet_handle_enable_onwire_ver_notify(knet_h1, &private_data, NULL)); if (knet_h1->onwire_ver_notify_fn_private_data != &private_data) { printf("knet_handle_enable_onwire_ver_notify failed to set private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_onwire_ver_notify with no onwire_ver_notify fn\n"); FAIL_ON_ERR(knet_handle_enable_onwire_ver_notify(knet_h1, NULL, NULL)); if (knet_h1->onwire_ver_notify_fn != NULL) { printf("knet_handle_enable_onwire_ver_notify failed to unset onwire_ver_notify fn"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_onwire_ver_notify with onwire_ver_notify fn\n"); FAIL_ON_ERR(knet_handle_enable_onwire_ver_notify(knet_h1, NULL, onwire_ver_notify)); if (knet_h1->onwire_ver_notify_fn != &onwire_ver_notify) { printf("knet_handle_enable_onwire_ver_notify failed to set onwire_ver_notify fn"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_enable_pmtud_notify.c b/libknet/tests/api_knet_handle_enable_pmtud_notify.c index df8383c4..2d2d5798 100644 --- a/libknet/tests/api_knet_handle_enable_pmtud_notify.c +++ b/libknet/tests/api_knet_handle_enable_pmtud_notify.c @@ -1,83 +1,83 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void pmtud_notify(void *priv_data, unsigned int data_mtu) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; printf("Test knet_handle_enable_pmtud_notify incorrect knet_h\n"); if ((!knet_handle_enable_pmtud_notify(NULL, NULL, pmtud_notify)) || (errno != EINVAL)) { printf("knet_handle_enable_pmtud_notify accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_enable_pmtud_notify with no private_data\n"); FAIL_ON_ERR(knet_handle_enable_pmtud_notify(knet_h1, NULL, pmtud_notify)); if (knet_h1->pmtud_notify_fn_private_data != NULL) { printf("knet_handle_enable_pmtud_notify failed to unset private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_pmtud_notify with private_data\n"); FAIL_ON_ERR(knet_handle_enable_pmtud_notify(knet_h1, &private_data, NULL)); if (knet_h1->pmtud_notify_fn_private_data != &private_data) { printf("knet_handle_enable_pmtud_notify failed to set private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_pmtud_notify with no pmtud_notify fn\n"); FAIL_ON_ERR(knet_handle_enable_pmtud_notify(knet_h1, NULL, NULL)); if (knet_h1->pmtud_notify_fn != NULL) { printf("knet_handle_enable_pmtud_notify failed to unset pmtud_notify fn"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_pmtud_notify with pmtud_notify fn\n"); FAIL_ON_ERR(knet_handle_enable_pmtud_notify(knet_h1, NULL, pmtud_notify)); if (knet_h1->pmtud_notify_fn != &pmtud_notify) { printf("knet_handle_enable_pmtud_notify failed to set pmtud_notify fn"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_enable_sock_notify.c b/libknet/tests/api_knet_handle_enable_sock_notify.c index c06f1d2d..7d787bd9 100644 --- a/libknet/tests/api_knet_handle_enable_sock_notify.c +++ b/libknet/tests/api_knet_handle_enable_sock_notify.c @@ -1,85 +1,85 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_handle_enable_sock_notify incorrect knet_h\n"); if ((!knet_handle_enable_sock_notify(NULL, NULL, sock_notify)) || (errno != EINVAL)) { printf("knet_handle_enable_sock_notify accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_enable_sock_notify with no private_data\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, NULL, sock_notify)); if (knet_h1->sock_notify_fn_private_data != NULL) { printf("knet_handle_enable_sock_notify failed to unset private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_sock_notify with private_data\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); if (knet_h1->sock_notify_fn_private_data != &private_data) { printf("knet_handle_enable_sock_notify failed to set private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_enable_sock_notify with no sock_notify fn\n"); FAIL_ON_SUCCESS(knet_handle_enable_sock_notify(knet_h1, NULL, NULL), EINVAL); printf("Test knet_handle_enable_sock_notify with sock_notify fn\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, NULL, sock_notify)); if (knet_h1->sock_notify_fn != &sock_notify) { CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_free.c b/libknet/tests/api_knet_handle_free.c index 359181f8..6e86327e 100644 --- a/libknet/tests/api_knet_handle_free.c +++ b/libknet/tests/api_knet_handle_free.c @@ -1,61 +1,61 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" #define TESTNODES 1 static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; setup_logpipes(logfds); printf("Test knet_handle_free with invalid knet_h (part 1)\n"); if ((!knet_handle_free(NULL)) || (errno != EINVAL)) { printf("knet_handle_free failed to detect invalid parameter\n"); exit(FAIL); } knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_free with one host configured\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); if ((!knet_handle_free(knet_h1)) || (errno != EBUSY)) { CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_host_remove(knet_h1, 1)); printf("Test knet_handle_free with invalid knet_h (part 2)\n"); FAIL_ON_SUCCESS(knet_handle_free(knet_h1 + 1), EINVAL); FAIL_ON_ERR(knet_handle_free(knet_h1)); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_get_channel.c b/libknet/tests/api_knet_handle_get_channel.c index d2b48f42..9423c783 100644 --- a/libknet/tests/api_knet_handle_get_channel.c +++ b/libknet/tests/api_knet_handle_get_channel.c @@ -1,84 +1,84 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; int datafd = 0; int8_t channel = 0, old_channel = 0; printf("Test knet_handle_get_channel incorrect knet_h\n"); if ((!knet_handle_get_channel(NULL, datafd, &channel)) || (errno != EINVAL)) { printf("knet_handle_get_channel accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_get_channel with invalid datafd\n"); datafd = 0; FAIL_ON_SUCCESS(knet_handle_get_channel(knet_h1, datafd, &channel), EINVAL); printf("Test knet_handle_get_channel with invalid channel\n"); datafd = 10; FAIL_ON_SUCCESS(knet_handle_get_channel(knet_h1, datafd, NULL), EINVAL); printf("Test knet_handle_get_channel with unconfigured datafd/channel\n"); datafd = 10; FAIL_ON_SUCCESS(knet_handle_get_channel(knet_h1, datafd, &channel), EINVAL); printf("Test knet_handle_get_channel with valid datafd\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; old_channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &old_channel)); FAIL_ON_ERR(knet_handle_get_channel(knet_h1, datafd, &channel)); if (old_channel != channel) { CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_get_datafd.c b/libknet/tests/api_knet_handle_get_datafd.c index 0fa6f811..304db05c 100644 --- a/libknet/tests/api_knet_handle_get_datafd.c +++ b/libknet/tests/api_knet_handle_get_datafd.c @@ -1,86 +1,86 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int datafd = 0, old_datafd; int8_t channel = 0; int res; printf("Test knet_handle_get_datafd incorrect knet_h\n"); if ((!knet_handle_get_datafd(NULL, channel, &datafd)) || (errno != EINVAL)) { printf("knet_handle_get_datafd accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_get_datafd with invalid channel (< 0)\n"); channel = 0; FAIL_ON_SUCCESS(knet_handle_get_datafd(knet_h1, channel, &datafd), EINVAL); printf("Test knet_handle_get_datafd with invalid channel (KNET_DATAFD_MAX)\n"); channel = KNET_DATAFD_MAX; FAIL_ON_SUCCESS(knet_handle_get_datafd(knet_h1, channel, &datafd), EINVAL); printf("Test knet_handle_get_datafd with unconfigured datafd/channel\n"); channel = 10; FAIL_ON_SUCCESS(knet_handle_get_datafd(knet_h1, channel, &datafd), EINVAL); printf("Test knet_handle_get_datafd with valid datafd\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); old_datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &old_datafd, &channel)); FAIL_ON_ERR(knet_handle_get_datafd(knet_h1, channel, &datafd)); if (old_datafd != datafd) { CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_get_host_defrag_bufs.c b/libknet/tests/api_knet_handle_get_host_defrag_bufs.c index 1b4ed348..4d5d6a0e 100644 --- a/libknet/tests/api_knet_handle_get_host_defrag_bufs.c +++ b/libknet/tests/api_knet_handle_get_host_defrag_bufs.c @@ -1,85 +1,85 @@ /* - * Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; uint16_t min_defrag_bufs, max_defrag_bufs; uint8_t shrink_threshold; defrag_bufs_reclaim_policy_t reclaim_policy; printf("Test knet_handle_get_host_defrag_bufs incorrect knet_h\n"); if ((!knet_handle_get_host_defrag_bufs(NULL, &min_defrag_bufs, &max_defrag_bufs, &shrink_threshold, &reclaim_policy)) || (errno != EINVAL)) { printf("knet_handle_get_host_defrag_bufs accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_get_host_defrag_bufs with invalid min_defrag_bufs\n"); FAIL_ON_SUCCESS(knet_handle_get_host_defrag_bufs(knet_h1, NULL, &max_defrag_bufs, &shrink_threshold, &reclaim_policy), EINVAL); printf("Test knet_handle_get_host_defrag_bufs with invalid max_defrag_bufs\n"); FAIL_ON_SUCCESS(knet_handle_get_host_defrag_bufs(knet_h1, &min_defrag_bufs, NULL, &shrink_threshold, &reclaim_policy), EINVAL); printf("Test knet_handle_get_host_defrag_bufs with invalid shrink_threshold\n"); FAIL_ON_SUCCESS(knet_handle_get_host_defrag_bufs(knet_h1, &min_defrag_bufs, &max_defrag_bufs, NULL, &reclaim_policy), EINVAL); printf("Test knet_handle_get_host_defrag_bufs with invalid reclaim_policy\n"); FAIL_ON_SUCCESS(knet_handle_get_host_defrag_bufs(knet_h1, &min_defrag_bufs, &max_defrag_bufs, &shrink_threshold, NULL), EINVAL); printf("Test knet_handle_get_host_defrag_bufs with valid data\n"); FAIL_ON_ERR(knet_handle_get_host_defrag_bufs(knet_h1, &min_defrag_bufs, &max_defrag_bufs, &shrink_threshold, &reclaim_policy)); printf("Test knet_handle_get_host_defrag_bufs default data\n"); if ((min_defrag_bufs != KNET_MIN_DEFRAG_BUFS_DEFAULT) || (max_defrag_bufs != KNET_MAX_DEFRAG_BUFS_DEFAULT) || (shrink_threshold != KNET_SHRINK_THRESHOLD_DEFAULT) || (reclaim_policy != RECLAIM_POLICY_ABSOLUTE)) { printf("knet_handle_get_host_defrag_bufs returned incorrect default data\n"); CLEAN_EXIT(FAIL); } knet_h1->defrag_bufs_reclaim_policy = RECLAIM_POLICY_AVERAGE; printf("Test knet_handle_get_host_defrag_bufs with reclaim_policy override\n"); FAIL_ON_ERR(knet_handle_get_host_defrag_bufs(knet_h1, &min_defrag_bufs, &max_defrag_bufs, &shrink_threshold, &reclaim_policy)); if ((min_defrag_bufs != KNET_MIN_DEFRAG_BUFS_DEFAULT) || (max_defrag_bufs != KNET_MAX_DEFRAG_BUFS_DEFAULT) || (shrink_threshold != KNET_SHRINK_THRESHOLD_DEFAULT) || (reclaim_policy != RECLAIM_POLICY_AVERAGE)) { printf("knet_handle_get_host_defrag_bufs returned incorrect default data\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_get_onwire_ver.c b/libknet/tests/api_knet_handle_get_onwire_ver.c index e3c0085f..deb14e82 100644 --- a/libknet/tests/api_knet_handle_get_onwire_ver.c +++ b/libknet/tests/api_knet_handle_get_onwire_ver.c @@ -1,76 +1,76 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; uint8_t onwire_min_ver, onwire_max_ver, onwire_ver; printf("Test knet_handle_get_onwire_ver incorrect knet_h\n"); if ((!knet_handle_get_onwire_ver(NULL, 1, &onwire_min_ver, &onwire_max_ver, &onwire_ver)) || (errno != EINVAL)) { printf("knet_handle_get_onwire_ver accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_get_onwire_ver with invalid host_id\n"); FAIL_ON_SUCCESS(knet_handle_get_onwire_ver(knet_h1, 199, &onwire_min_ver, &onwire_max_ver, &onwire_ver), EINVAL); printf("Test knet_handle_get_onwire_ver with invalid onwire_min_ver\n"); FAIL_ON_SUCCESS(knet_handle_get_onwire_ver(knet_h1, knet_h1->host_id, NULL, &onwire_max_ver, &onwire_ver), EINVAL); printf("Test knet_handle_get_onwire_ver with invalid onwire_max_ver\n"); FAIL_ON_SUCCESS(knet_handle_get_onwire_ver(knet_h1, knet_h1->host_id, &onwire_min_ver, NULL, &onwire_ver), EINVAL); printf("Test knet_handle_get_onwire_ver with invalid onwire_ver\n"); FAIL_ON_SUCCESS(knet_handle_get_onwire_ver(knet_h1, knet_h1->host_id, &onwire_min_ver, &onwire_max_ver, NULL), EINVAL); printf("Test knet_handle_get_onwire_ver with valid data\n"); FAIL_ON_ERR(knet_handle_get_onwire_ver(knet_h1, knet_h1->host_id, &onwire_min_ver, &onwire_max_ver, &onwire_ver)); if (onwire_min_ver != knet_h1->onwire_min_ver) { printf("knet_handle_get_onwire_ver returned invalid onwire_min_ver\n"); CLEAN_EXIT(FAIL); } if (onwire_max_ver != knet_h1->onwire_max_ver) { printf("knet_handle_get_onwire_ver returned invalid onwire_max_ver\n"); CLEAN_EXIT(FAIL); } if (onwire_ver != knet_h1->onwire_ver) { printf("knet_handle_get_onwire_ver returned invalid onwire_ver\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_get_stats.c b/libknet/tests/api_knet_handle_get_stats.c index 6d3dd36f..af560c27 100644 --- a/libknet/tests/api_knet_handle_get_stats.c +++ b/libknet/tests/api_knet_handle_get_stats.c @@ -1,70 +1,70 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; struct knet_handle_stats test_byte_array[2]; struct knet_handle_stats ref_byte_array[2]; struct knet_handle_stats stats; int res; printf("Test knet_handle_get_stats incorrect knet_h\n"); memset(&stats, 0, sizeof(struct knet_handle_stats)); if ((!knet_handle_get_stats(NULL, &stats, sizeof(struct knet_handle_stats))) || (errno != EINVAL)) { printf("knet_handle_get_stats accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_get_stats with NULL structure pointer\n"); FAIL_ON_SUCCESS(knet_handle_get_stats(knet_h1, NULL, 0), EINVAL); printf("Test knet_handle_get_stats with small structure size\n"); memset(test_byte_array, 0x55, sizeof(struct knet_handle_stats) * 2); memset(ref_byte_array, 0x55, sizeof(struct knet_handle_stats) * 2); FAIL_ON_ERR(knet_handle_get_stats(knet_h1, (struct knet_handle_stats *)test_byte_array, sizeof(size_t))); if (memcmp(&test_byte_array[1], ref_byte_array, sizeof(struct knet_handle_stats))) { printf("knet_handle_get_stats corrupted memory after stats structure\n"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_get_stats with valid input\n"); FAIL_ON_ERR(knet_handle_get_stats(knet_h1, &stats, sizeof(struct knet_handle_stats))); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_get_threads_timer_res.c b/libknet/tests/api_knet_handle_get_threads_timer_res.c index 845da541..fd909013 100644 --- a/libknet/tests/api_knet_handle_get_threads_timer_res.c +++ b/libknet/tests/api_knet_handle_get_threads_timer_res.c @@ -1,65 +1,65 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; useconds_t timeres; printf("Test knet_handle_get_threads_timer_res incorrect knet_h\n"); if ((!knet_handle_get_threads_timer_res(NULL, &timeres)) || (errno != EINVAL)) { printf("knet_handle_get_threads_timer_res accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_get_threads_timer_res with invalid timeres\n"); FAIL_ON_SUCCESS(knet_handle_get_threads_timer_res(knet_h1, NULL), EINVAL); printf("Test knet_handle_get_threads_timer_res with valid timeres\n"); FAIL_ON_ERR(knet_handle_get_threads_timer_res(knet_h1, &timeres)); if (timeres != knet_h1->threads_timer_res) { printf("knet_handle_get_threads_timer_res did not get timeres correct value: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_handle_get_threads_timer_res with valid timeres\n"); FAIL_ON_ERR(knet_handle_set_threads_timer_res(knet_h1, 1000)); FAIL_ON_ERR(knet_handle_get_threads_timer_res(knet_h1, &timeres)); if (timeres != knet_h1->threads_timer_res) { printf("knet_handle_get_threads_timer_res did not get timeres correct value: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_get_transport_reconnect_interval.c b/libknet/tests/api_knet_handle_get_transport_reconnect_interval.c index d4154345..2dbc1890 100644 --- a/libknet/tests/api_knet_handle_get_transport_reconnect_interval.c +++ b/libknet/tests/api_knet_handle_get_transport_reconnect_interval.c @@ -1,58 +1,58 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; uint32_t msecs = 0; printf("Test knet_handle_get_transport_reconnect_interval with incorrect knet_h\n"); if ((!knet_handle_get_transport_reconnect_interval(NULL, &msecs)) || (errno != EINVAL)) { printf("knet_handle_get_transport_reconnect_interval accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_get_transport_reconnect_interval with incorrect msecs\n"); FAIL_ON_SUCCESS(knet_handle_get_transport_reconnect_interval(knet_h1, NULL), EINVAL); printf("Test knet_handle_get_transport_reconnect_interval with correct values\n"); FAIL_ON_ERR(knet_handle_get_transport_reconnect_interval(knet_h1, &msecs)); if (msecs != KNET_TRANSPORT_DEFAULT_RECONNECT_INTERVAL) { printf("knet_handle_get_transport_reconnect_interval failed to set correct value\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_new.c b/libknet/tests/api_knet_handle_new.c index 60e5c4f7..02392630 100644 --- a/libknet/tests/api_knet_handle_new.c +++ b/libknet/tests/api_knet_handle_new.c @@ -1,124 +1,124 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; struct rlimit cur; int logfds[2]; printf("Test knet_handle_new hostid 1, no logging\n"); knet_h1 = knet_handle_new(1, 0, 0, 0); if (!knet_h1) { printf("Unable to init knet_handle! err: %s\n", strerror(errno)); exit(FAIL); } if (knet_handle_free(knet_h1) != 0) { printf("Unable to free knet_handle\n"); exit(FAIL); } printf("Test knet_handle_new hostid -1, no logging\n"); knet_h1 = knet_handle_new(-1, 0, 0, 0); if (!knet_h1) { printf("Unable to init knet_handle! err: %s\n", strerror(errno)); exit(FAIL); } /* * -1 == knet_node_id_t 65535 */ if (knet_h1->host_id != 65535) { printf("host_id size might have changed!\n"); knet_handle_free(knet_h1); exit(FAIL); } if (knet_handle_free(knet_h1) != 0) { printf("Unable to free knet_handle\n"); exit(FAIL); } if (getrlimit(RLIMIT_NOFILE, &cur) < 0) { printf("Unable to get current fd limit: %s\n", strerror(errno)); exit(SKIP); } /* * passing a bad fd and it should fail */ printf("Test knet_handle_new hostid 1, incorrect log_fd (-1)\n"); knet_h1 = knet_handle_new(1, -1, 0, 0); if ((!knet_h1) && (errno != EINVAL)) { printf("knet_handle_new returned incorrect errno on incorrect log_fd\n"); exit(FAIL); } if (knet_h1) { printf("knet_handle_new accepted an incorrect (-1) log_fd\n"); knet_handle_free(knet_h1); exit(FAIL); } /* * passing a bad fd and it should fail */ printf("Test knet_handle_new hostid 1, incorrect log_fd (max_fd + 1)\n"); knet_h1 = knet_handle_new(1, (int) cur.rlim_max, 0, 0); if ((knet_h1) || (errno != EINVAL)) { printf("knet_handle_new accepted an incorrect (max_fd + 1) log_fd or returned incorrect errno on incorrect log_fd: %s\n", strerror(errno)); knet_handle_free(knet_h1); exit(FAIL); } setup_logpipes(logfds); printf("Test knet_handle_new hostid 1, proper log_fd, invalid log level (DEBUG + 1)\n"); knet_h1 = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG + 1 ,0); if ((knet_h1) || (errno != EINVAL)) { printf("knet_handle_new accepted an incorrect log level or returned incorrect errno on incorrect log level: %s\n", strerror(errno)); knet_h[1] = knet_h1; CLEAN_EXIT(FAIL); } printf("Test knet_handle_new hostid 1, proper log_fd, proper log level (DEBUG)\n"); (void)knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_new_limit.c b/libknet/tests/api_knet_handle_new_limit.c index 2c121101..93b16ee1 100644 --- a/libknet/tests/api_knet_handle_new_limit.c +++ b/libknet/tests/api_knet_handle_new_limit.c @@ -1,70 +1,70 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h[UINT8_MAX + 1]; int logfds[2]; int idx; setup_logpipes(logfds); for (idx = 0; idx < UINT8_MAX; idx++) { printf("Allocating %d\n", idx); knet_h[idx] = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG, 0); if (!knet_h[idx]) { printf("knet_handle_new[%d] failed: %s\n", idx, strerror(errno)); flush_logs(logfds[0], stdout); close_logpipes(logfds); exit(FAIL); } flush_logs(logfds[0], stdout); } printf("forcing UINT8_T MAX\n"); knet_h[UINT8_MAX] = knet_handle_new(1, logfds[1], KNET_LOG_DEBUG, 0); if (knet_h[UINT8_MAX]) { printf("off by one somewhere\n"); knet_handle_free(knet_h[UINT8_MAX]); } flush_logs(logfds[0], stdout); for (idx = 0; idx < UINT8_MAX; idx++) { printf("Freeing %d\n", idx); knet_handle_free(knet_h[idx]); flush_logs(logfds[0], stdout); } flush_logs(logfds[0], stdout); close_logpipes(logfds); } int main(int argc, char *argv[]) { if ((is_memcheck()) || (is_helgrind())) { return SKIP; } test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_pmtud_get.c b/libknet/tests/api_knet_handle_pmtud_get.c index 57adb5ac..f4b0e2a4 100644 --- a/libknet/tests/api_knet_handle_pmtud_get.c +++ b/libknet/tests/api_knet_handle_pmtud_get.c @@ -1,59 +1,59 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; unsigned int data_mtu; printf("Test knet_handle_pmtud_get incorrect knet_h\n"); if ((!knet_handle_pmtud_get(NULL, &data_mtu)) || (errno != EINVAL)) { printf("knet_handle_pmtud_get accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_handle_pmtud_get with no data_mtu\n"); FAIL_ON_SUCCESS(knet_handle_pmtud_get(knet_h1, NULL), EINVAL); FAIL_ON_ERR(knet_handle_pmtud_get(knet_h1, &data_mtu)); if (knet_h1->data_mtu != data_mtu) { printf("knet_handle_pmtud_get failed to set the value\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_pmtud_getfreq.c b/libknet/tests/api_knet_handle_pmtud_getfreq.c index 400d984a..695895b6 100644 --- a/libknet/tests/api_knet_handle_pmtud_getfreq.c +++ b/libknet/tests/api_knet_handle_pmtud_getfreq.c @@ -1,60 +1,60 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; unsigned int interval; printf("Test knet_handle_pmtud_getfreq incorrect knet_h\n"); if ((!knet_handle_pmtud_getfreq(NULL, &interval)) || (errno != EINVAL)) { printf("knet_handle_pmtud_getfreq accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_handle_pmtud_getfreq with no interval\n"); FAIL_ON_SUCCESS(knet_handle_pmtud_getfreq(knet_h1, NULL), EINVAL); FAIL_ON_ERR(knet_handle_pmtud_getfreq(knet_h1, &interval)); if (knet_h1->pmtud_interval != interval) { printf("knet_handle_pmtud_getfreq failed to set the value\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_pmtud_set.c b/libknet/tests/api_knet_handle_pmtud_set.c index 4898986e..0df25aaf 100644 --- a/libknet/tests/api_knet_handle_pmtud_set.c +++ b/libknet/tests/api_knet_handle_pmtud_set.c @@ -1,120 +1,120 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; unsigned int iface_mtu = 0, data_mtu; int datafd = 0; int8_t channel = 0; struct sockaddr_storage lo; printf("Test knet_handle_pmtud_set incorrect knet_h\n"); if ((!knet_handle_pmtud_set(NULL, iface_mtu)) || (errno != EINVAL)) { printf("knet_handle_pmtud_set accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); iface_mtu = KNET_PMTUD_SIZE_V4 + 1; printf("Test knet_handle_pmtud_set with wrong iface_mtu\n"); FAIL_ON_SUCCESS(knet_handle_pmtud_set(knet_h1, iface_mtu), EINVAL); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_pong_count(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(wait_for_host(knet_h1, 1, 4, logfds[0], stdout)); FAIL_ON_ERR(knet_handle_pmtud_get(knet_h1, &data_mtu)); /* * 28 = IP (20) + UDP (8) */ iface_mtu = data_mtu + 28 + KNET_HEADER_ALL_SIZE - 64; printf("Test knet_handle_pmtud_set with iface_mtu %u\n", iface_mtu); FAIL_ON_ERR(knet_handle_pmtud_set(knet_h1, iface_mtu)); /* * wait for PMTUd to pick up the change */ test_sleep(knet_h1, 1); flush_logs(logfds[0], stdout); if (knet_h1->data_mtu != data_mtu - 64) { printf("knet_handle_pmtud_set failed to set the value\n"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_pmtud_set with iface_mtu 0\n"); FAIL_ON_ERR(knet_handle_pmtud_set(knet_h1, 0)); /* * wait for PMTUd to pick up the change */ test_sleep(knet_h1, 1); flush_logs(logfds[0], stdout); if (knet_h1->data_mtu != data_mtu) { printf("knet_handle_pmtud_set failed to redetect MTU: detected mtu: %u data_mtu: %u \n", knet_h1->data_mtu, data_mtu); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_pmtud_setfreq.c b/libknet/tests/api_knet_handle_pmtud_setfreq.c index 9b9f997c..562347e9 100644 --- a/libknet/tests/api_knet_handle_pmtud_setfreq.c +++ b/libknet/tests/api_knet_handle_pmtud_setfreq.c @@ -1,62 +1,62 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_handle_pmtud_setfreq incorrect knet_h\n"); if ((!knet_handle_pmtud_setfreq(NULL, 1)) || (errno != EINVAL)) { printf("knet_handle_pmtud_setfreq accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_handle_pmtud_setfreq with 0 (incorrect)\n"); FAIL_ON_SUCCESS(knet_handle_pmtud_setfreq(NULL, 0), EINVAL); printf("Test knet_handle_pmtud_setfreq with 86401 (incorrect)\n"); FAIL_ON_SUCCESS(knet_handle_pmtud_setfreq(NULL, 86401), EINVAL); printf("Test knet_handle_pmtud_setfreq with 1 (correct)\n"); FAIL_ON_ERR(knet_handle_pmtud_setfreq(knet_h1, 1)); if (knet_h1->pmtud_interval != 1) { printf("knet_handle_pmtud_setfreq failed to set the value\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_remove_datafd.c b/libknet/tests/api_knet_handle_remove_datafd.c index 091bdba2..1b2d92c7 100644 --- a/libknet/tests/api_knet_handle_remove_datafd.c +++ b/libknet/tests/api_knet_handle_remove_datafd.c @@ -1,77 +1,77 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; int datafd = 0; int8_t channel = 0; printf("Test knet_handle_remove_datafd incorrect knet_h\n"); if ((!knet_handle_remove_datafd(NULL, datafd)) || (errno != EINVAL)) { printf("knet_handle_remove_datafd accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_remove_datafd with no datafd\n"); datafd = 0; FAIL_ON_SUCCESS(knet_handle_remove_datafd(knet_h1, datafd), EINVAL); printf("Test knet_handle_remove_datafd with invalid datafd\n"); datafd = 10; FAIL_ON_SUCCESS(knet_handle_remove_datafd(knet_h1, datafd), EINVAL); printf("Test knet_handle_remove_datafd with valid datafd\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); FAIL_ON_ERR(knet_handle_remove_datafd(knet_h1, datafd)); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_set_host_defrag_bufs.c b/libknet/tests/api_knet_handle_set_host_defrag_bufs.c index fc1ba048..f4ac812a 100644 --- a/libknet/tests/api_knet_handle_set_host_defrag_bufs.c +++ b/libknet/tests/api_knet_handle_set_host_defrag_bufs.c @@ -1,98 +1,98 @@ /* - * Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; uint16_t min_defrag_bufs = KNET_MIN_DEFRAG_BUFS_DEFAULT, max_defrag_bufs = KNET_MAX_DEFRAG_BUFS_DEFAULT; uint8_t shrink_threshold = KNET_SHRINK_THRESHOLD_DEFAULT; defrag_bufs_reclaim_policy_t reclaim_policy = RECLAIM_POLICY_ABSOLUTE; printf("Test knet_handle_set_host_defrag_bufs incorrect knet_h\n"); if ((!knet_handle_set_host_defrag_bufs(NULL, min_defrag_bufs, max_defrag_bufs, shrink_threshold, reclaim_policy)) || (errno != EINVAL)) { printf("knet_handle_set_host_defrag_bufs accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_set_host_defrag_bufs with invalid min_defrag_bufs (0)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, 0, max_defrag_bufs, shrink_threshold, reclaim_policy), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with invalid min_defrag_bufs (3 - not power of 2)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, 3, max_defrag_bufs, shrink_threshold, reclaim_policy), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with invalid min_defrag_bufs (> max_defrag_bufs)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, max_defrag_bufs * 2, max_defrag_bufs, shrink_threshold, reclaim_policy), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with invalid max_defrag_bufs (0)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, min_defrag_bufs, 0, shrink_threshold, reclaim_policy), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with invalid max_defrag_bufs (min_defrag_bufs + 1)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, min_defrag_bufs, min_defrag_bufs + 1, shrink_threshold, reclaim_policy), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with invalid max_defrag_bufs (< min_defrag_bufs)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, min_defrag_bufs, min_defrag_bufs / 2, shrink_threshold, reclaim_policy), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with invalid shrink_threshold (0)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, min_defrag_bufs, max_defrag_bufs, 0, reclaim_policy), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with invalid shrink_threshold (51)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, min_defrag_bufs, max_defrag_bufs, 51, reclaim_policy), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with invalid reclaim_policy (20)\n"); FAIL_ON_SUCCESS(knet_handle_set_host_defrag_bufs(knet_h1, min_defrag_bufs, max_defrag_bufs, shrink_threshold, 20), EINVAL); printf("Test knet_handle_set_host_defrag_bufs with valid data (defaults)\n"); FAIL_ON_ERR(knet_handle_set_host_defrag_bufs(knet_h1, min_defrag_bufs, max_defrag_bufs, shrink_threshold, reclaim_policy)); printf("Test knet_handle_set_host_defrag_bufs default data\n"); if ((knet_h1->defrag_bufs_min != KNET_MIN_DEFRAG_BUFS_DEFAULT) || (knet_h1->defrag_bufs_max != KNET_MAX_DEFRAG_BUFS_DEFAULT) || (knet_h1->defrag_bufs_shrink_threshold != KNET_SHRINK_THRESHOLD_DEFAULT) || (knet_h1->defrag_bufs_reclaim_policy != RECLAIM_POLICY_ABSOLUTE)) { printf("knet_handle_set_host_defrag_bufs set incorrect default data\n"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_set_host_defrag_bufs with reclaim_policy override\n"); FAIL_ON_ERR(knet_handle_set_host_defrag_bufs(knet_h1, min_defrag_bufs, max_defrag_bufs, shrink_threshold, RECLAIM_POLICY_AVERAGE)); if ((knet_h1->defrag_bufs_min != KNET_MIN_DEFRAG_BUFS_DEFAULT) || (knet_h1->defrag_bufs_max != KNET_MAX_DEFRAG_BUFS_DEFAULT) || (knet_h1->defrag_bufs_shrink_threshold != KNET_SHRINK_THRESHOLD_DEFAULT) || (knet_h1->defrag_bufs_reclaim_policy != RECLAIM_POLICY_AVERAGE)) { printf("knet_handle_set_host_defrag_bufs set incorrect reclaim_policy override\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_set_onwire_ver.c b/libknet/tests/api_knet_handle_set_onwire_ver.c index a268c081..27e796d8 100644 --- a/libknet/tests/api_knet_handle_set_onwire_ver.c +++ b/libknet/tests/api_knet_handle_set_onwire_ver.c @@ -1,75 +1,75 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_handle_set_onwire_ver incorrect knet_h\n"); if ((!knet_handle_set_onwire_ver(NULL, 1)) || (errno != EINVAL)) { printf("knet_handle_set_onwire_ver accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); knet_h1->onwire_min_ver = 2; knet_h1->onwire_max_ver = 3; printf("Test knet_handle_set_onwire_ver with invalid onwire_ver (1)\n"); FAIL_ON_SUCCESS(knet_handle_set_onwire_ver(knet_h1, 1), EINVAL); printf("Test knet_handle_set_onwire_ver with invalid onwire_ver (4)\n"); FAIL_ON_SUCCESS(knet_handle_set_onwire_ver(knet_h1, 4), EINVAL); printf("Test knet_handle_set_onwire_ver with valid onwire_ver (2)\n"); if (knet_handle_set_onwire_ver(knet_h1, 2) < 0) { printf("knet_handle_set_onwire_ver did not accepted valid onwire_ver\n"); CLEAN_EXIT(FAIL); } if (knet_h1->onwire_force_ver != 2) { printf("knet_handle_set_onwire_ver did not set correct onwire_ver\n"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_set_onwire_ver reset (0)\n"); FAIL_ON_ERR(knet_handle_set_onwire_ver(knet_h1, 0)); if (knet_h1->onwire_force_ver != 0) { printf("knet_handle_set_onwire_ver did not set correct onwire_ver\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_set_threads_timer_res.c b/libknet/tests/api_knet_handle_set_threads_timer_res.c index faf9854b..09116e33 100644 --- a/libknet/tests/api_knet_handle_set_threads_timer_res.c +++ b/libknet/tests/api_knet_handle_set_threads_timer_res.c @@ -1,55 +1,55 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_handle_set_threads_timer_res incorrect knet_h\n"); if ((!knet_handle_set_threads_timer_res(NULL, 0)) || (errno != EINVAL)) { printf("knet_handle_set_threads_timer_res accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_set_threads_timer_res with invalid timeres\n"); FAIL_ON_SUCCESS(knet_handle_set_threads_timer_res(knet_h1, 999), EINVAL); printf("Test knet_handle_set_threads_timer_res with valid timeres\n"); FAIL_ON_ERR(knet_handle_set_threads_timer_res(knet_h1, 2000)); if (knet_h1->threads_timer_res != 2000) { printf("knet_handle_set_threads_timer_res did not set timeres to correct value: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_set_transport_reconnect_interval.c b/libknet/tests/api_knet_handle_set_transport_reconnect_interval.c index dd03974a..1a21dca9 100644 --- a/libknet/tests/api_knet_handle_set_transport_reconnect_interval.c +++ b/libknet/tests/api_knet_handle_set_transport_reconnect_interval.c @@ -1,58 +1,58 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_handle_set_transport_reconnect_interval with incorrect knet_h\n"); if ((!knet_handle_set_transport_reconnect_interval(NULL, 1000)) || (errno != EINVAL)) { printf("knet_handle_set_transport_reconnect_interval accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_handle_set_transport_reconnect_interval with incorrect msecs\n"); FAIL_ON_SUCCESS(knet_handle_set_transport_reconnect_interval(knet_h1, 0), EINVAL); printf("Test knet_handle_set_transport_reconnect_interval with correct values\n"); FAIL_ON_ERR(knet_handle_set_transport_reconnect_interval(knet_h1, 2000)); if (knet_h1->reconnect_int != 2000) { printf("knet_handle_set_transport_reconnect_interval failed to set correct value\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_handle_setfwd.c b/libknet/tests/api_knet_handle_setfwd.c index d29039d4..813f3167 100644 --- a/libknet/tests/api_knet_handle_setfwd.c +++ b/libknet/tests/api_knet_handle_setfwd.c @@ -1,65 +1,65 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; printf("Test knet_handle_setfwd with invalid knet_h\n"); if ((!knet_handle_setfwd(NULL, 0)) || (errno != EINVAL)) { printf("knet_handle_setfwd accepted invalid knet_h parameter\n"); exit(FAIL); } setup_logpipes(logfds); printf("Test knet_handle_setfwd with invalid param (2) \n"); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); FAIL_ON_SUCCESS(knet_handle_setfwd(knet_h1, 2), EINVAL); printf("Test knet_handle_setfwd with valid param (1) \n"); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); if (knet_h1->enabled != 1) { printf("knet_handle_setfwd failed to set correct value"); CLEAN_EXIT(FAIL); } printf("Test knet_handle_setfwd with valid param (0) \n"); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 0)); if (knet_h1->enabled != 0) { printf("knet_handle_setfwd failed to set correct value"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_add.c b/libknet/tests/api_knet_host_add.c index c1af2e23..9c0ed69e 100644 --- a/libknet/tests/api_knet_host_add.c +++ b/libknet/tests/api_knet_host_add.c @@ -1,67 +1,67 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; knet_node_id_t host_ids[KNET_MAX_HOST]; size_t host_ids_entries; printf("Test knet_host_add incorrect knet_h\n"); if ((!knet_host_add(NULL, 1)) || (errno != EINVAL)) { printf("knet_host_add accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_host_add with hostid 1\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); printf("Test verify host_id 1 is in the host list\n"); FAIL_ON_ERR(knet_host_get_host_list(knet_h1, host_ids, &host_ids_entries)); if (host_ids_entries != 1) { printf("Too many hosts?\n"); CLEAN_EXIT(FAIL); } if (host_ids[0] != 1) { printf("Unable to find host id 1 in host list\n"); CLEAN_EXIT(FAIL); } printf("Test knet_host_add adding host 1 again\n"); FAIL_ON_SUCCESS(knet_host_add(knet_h1, 1), EEXIST); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_enable_status_change_notify.c b/libknet/tests/api_knet_host_enable_status_change_notify.c index c3dbb3cd..5fc79cc6 100644 --- a/libknet/tests/api_knet_host_enable_status_change_notify.c +++ b/libknet/tests/api_knet_host_enable_status_change_notify.c @@ -1,85 +1,85 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void host_notify(void *priv_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_host_enable_status_change_notify incorrect knet_h\n"); if ((!knet_host_enable_status_change_notify(NULL, NULL, host_notify)) || (errno != EINVAL)) { printf("knet_host_enable_status_change_notify accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_host_enable_status_change_notify with no private_data\n"); FAIL_ON_ERR(knet_host_enable_status_change_notify(knet_h1, NULL, host_notify)); if (knet_h1->host_status_change_notify_fn_private_data != NULL) { printf("knet_host_enable_status_change_notify failed to unset private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_host_enable_status_change_notify with private_data\n"); FAIL_ON_ERR(knet_host_enable_status_change_notify(knet_h1, &private_data, NULL)); if (knet_h1->host_status_change_notify_fn_private_data != &private_data) { printf("knet_host_enable_status_change_notify failed to set private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_host_enable_status_change_notify with no host_notify fn\n"); FAIL_ON_ERR(knet_host_enable_status_change_notify(knet_h1, NULL, NULL)); if (knet_h1->host_status_change_notify_fn != NULL) { printf("knet_host_enable_status_change_notify failed to unset host_notify fn"); CLEAN_EXIT(FAIL); } printf("Test knet_host_enable_status_change_notify with host_notify fn\n"); FAIL_ON_ERR(knet_host_enable_status_change_notify(knet_h1, NULL, host_notify)); if (knet_h1->host_status_change_notify_fn != &host_notify) { printf("knet_host_enable_status_change_notify failed to set host_notify fn"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_get_host_list.c b/libknet/tests/api_knet_host_get_host_list.c index f80587fb..16b0ebc1 100644 --- a/libknet/tests/api_knet_host_get_host_list.c +++ b/libknet/tests/api_knet_host_get_host_list.c @@ -1,76 +1,76 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; knet_node_id_t host_ids[KNET_MAX_HOST]; size_t host_ids_entries; printf("Test knet_host_get_host_list incorrect knet_h\n"); if ((!knet_host_get_host_list(NULL, host_ids, &host_ids_entries)) || (errno != EINVAL)) { printf("knet_host_get_host_list accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_host_get_host_list incorrect host_ids\n"); FAIL_ON_SUCCESS(knet_host_get_host_list(knet_h1, NULL, &host_ids_entries), EINVAL); printf("Test knet_host_get_host_list incorrect host_ids_entries\n"); FAIL_ON_SUCCESS(knet_host_get_host_list(knet_h1, host_ids, NULL), EINVAL); printf("Test knet_host_get_host_list with one host\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(knet_host_get_host_list(knet_h1, host_ids, &host_ids_entries)); if (host_ids_entries != 1) { printf("Too many hosts?\n"); CLEAN_EXIT(FAIL); } if (host_ids[0] != 1) { printf("Unable to find host id 1 in host list\n"); CLEAN_EXIT(FAIL); } printf("Test knet_host_get_host_list with zero hosts\n"); FAIL_ON_ERR(knet_host_remove(knet_h1, 1)); FAIL_ON_ERR(knet_host_get_host_list(knet_h1, host_ids, &host_ids_entries)); if (host_ids_entries != 0) { printf("Too many hosts?\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_get_id_by_host_name.c b/libknet/tests/api_knet_host_get_id_by_host_name.c index 9896a610..88d00903 100644 --- a/libknet/tests/api_knet_host_get_id_by_host_name.c +++ b/libknet/tests/api_knet_host_get_id_by_host_name.c @@ -1,63 +1,63 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; knet_node_id_t host_id; printf("Test knet_host_get_id_by_host_name incorrect knet_h\n"); if ((!knet_host_get_id_by_host_name(NULL, "1", &host_id)) || (errno != EINVAL)) { printf("knet_host_get_id_by_host_name accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_host_get_id_by_host_name with incorrect name 1\n"); FAIL_ON_SUCCESS(knet_host_get_id_by_host_name(knet_h1, NULL, &host_id), EINVAL); printf("Test knet_host_get_id_by_host_name with incorrect host_id\n"); FAIL_ON_SUCCESS(knet_host_get_id_by_host_name(knet_h1, "1", NULL), EINVAL); printf("Test knet_host_get_id_by_host_name with incorrect values for name\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_host_get_id_by_host_name(knet_h1, "test", &host_id), ENOENT); printf("Test knet_host_get_id_by_host_name with correct values\n"); FAIL_ON_ERR(knet_host_get_id_by_host_name(knet_h1, "1", &host_id)); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_get_name_by_host_id.c b/libknet/tests/api_knet_host_get_name_by_host_id.c index 155a67b8..af5881ad 100644 --- a/libknet/tests/api_knet_host_get_name_by_host_id.c +++ b/libknet/tests/api_knet_host_get_name_by_host_id.c @@ -1,62 +1,62 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; char name[KNET_MAX_HOST_LEN]; printf("Test knet_host_get_name_by_host_id incorrect knet_h\n"); if ((!knet_host_get_name_by_host_id(NULL, 1, name)) || (errno != EINVAL)) { printf("knet_host_get_name_by_host_id accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_host_get_name_by_host_id with incorrect hostid 1\n"); FAIL_ON_SUCCESS(knet_host_get_name_by_host_id(knet_h1, 1, name), EINVAL); printf("Test knet_host_get_name_by_host_id with incorrect name\n"); FAIL_ON_SUCCESS(knet_host_get_name_by_host_id(knet_h1, 1, NULL), EINVAL); printf("Test knet_host_get_name_by_host_id with correct values for hostid 1: "); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(knet_host_get_name_by_host_id(knet_h1, 1, name)); printf("%s\n", name); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_get_policy.c b/libknet/tests/api_knet_host_get_policy.c index 68637409..d131916e 100644 --- a/libknet/tests/api_knet_host_get_policy.c +++ b/libknet/tests/api_knet_host_get_policy.c @@ -1,64 +1,64 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; uint8_t policy; printf("Test knet_host_get_policy incorrect knet_h\n"); if ((!knet_host_get_policy(NULL, 1, &policy)) || (errno != EINVAL)) { printf("knet_host_get_policy accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_host_get_policy incorrect host_id\n"); FAIL_ON_SUCCESS(knet_host_get_policy(knet_h1, 1, &policy), EINVAL); printf("Test knet_host_get_policy incorrect policy\n"); FAIL_ON_SUCCESS(knet_host_get_policy(knet_h1, 1, NULL), EINVAL); printf("Test knet_host_get_policy correct policy\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(knet_host_set_policy(knet_h1, 1, KNET_LINK_POLICY_RR)); FAIL_ON_ERR(knet_host_get_policy(knet_h1, 1, &policy)); if (policy != KNET_LINK_POLICY_RR) { printf("knet_host_get_policy policy for host 1 does not appear to be correct\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_get_status.c b/libknet/tests/api_knet_host_get_status.c index 9fd05005..79ff0f3a 100644 --- a/libknet/tests/api_knet_host_get_status.c +++ b/libknet/tests/api_knet_host_get_status.c @@ -1,61 +1,61 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "host.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct knet_host_status status; printf("Test knet_host_get_status incorrect knet_h\n"); memset(&status, 0, sizeof(struct knet_host_status)); if ((!knet_host_get_status(NULL, 1, &status)) || (errno != EINVAL)) { printf("knet_host_get_status accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_host_get_status with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_host_get_status(knet_h1, 1, &status), EINVAL); printf("Test knet_host_get_status with incorrect status\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_host_get_status(knet_h1, 1, NULL), EINVAL); printf("Test knet_host_get_status with correct values\n"); FAIL_ON_ERR(knet_host_get_status(knet_h1, 1, &status)); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_remove.c b/libknet/tests/api_knet_host_remove.c index 0262adc3..35f4c880 100644 --- a/libknet/tests/api_knet_host_remove.c +++ b/libknet/tests/api_knet_host_remove.c @@ -1,78 +1,78 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; knet_node_id_t host_ids[KNET_MAX_HOST]; size_t host_ids_entries; struct sockaddr_storage lo; printf("Test knet_host_add incorrect knet_h\n"); if ((!knet_host_remove(NULL, 1)) || (errno != EINVAL)) { printf("knet_host_remove accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_host_remove with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_host_remove(knet_h1, 1), EINVAL); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); printf("Test knet_host_remove with configured host_id and links\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 1, &lo)); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); if ((!knet_host_remove(knet_h1, 1)) || (errno != EBUSY)) { printf("knet_host_remove accepted invalid request to remove host with link enabled or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 0)); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); printf("Test knet_host_remove with configured host_id (no links)\n"); FAIL_ON_ERR(knet_host_remove(knet_h1, 1)); FAIL_ON_ERR(knet_host_get_host_list(knet_h1, host_ids, &host_ids_entries)); if (host_ids_entries) { printf("Too many hosts?\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_set_name.c b/libknet/tests/api_knet_host_set_name.c index 72fc2f51..3487435d 100644 --- a/libknet/tests/api_knet_host_set_name.c +++ b/libknet/tests/api_knet_host_set_name.c @@ -1,92 +1,92 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; char longhostname[KNET_MAX_HOST_LEN+2]; printf("Test knet_host_set_name incorrect knet_h\n"); if ((!knet_host_set_name(NULL, 1, "test")) || (errno != EINVAL)) { printf("knet_host_set_name accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_host_set_name with incorrect hostid 1\n"); FAIL_ON_SUCCESS(knet_host_set_name(knet_h1, 2, "test"), EINVAL); printf("Test knet_host_set_name with correct values\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(knet_host_set_name(knet_h1, 1, "test")); if (strcmp("test", knet_h1->host_index[1]->name)) { printf("knet_host_set_name failed to copy name\n"); CLEAN_EXIT(FAIL); } printf("Test knet_host_set_name with correct values (name change)\n"); FAIL_ON_ERR(knet_host_set_name(knet_h1, 1, "tes")); if (strcmp("tes", knet_h1->host_index[1]->name)) { printf("knet_host_set_name failed to change name\n"); CLEAN_EXIT(FAIL); } printf("Test knet_host_set_name with NULL name\n"); FAIL_ON_SUCCESS(knet_host_set_name(knet_h1, 1, NULL), EINVAL); printf("Test knet_host_set_name with duplicate name\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 2)); if ((!knet_host_set_name(knet_h1, 2, "tes")) || (errno != EEXIST)) { printf("knet_host_set_name accepted duplicated name or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } knet_host_remove(knet_h1, 2); flush_logs(logfds[0], stdout); printf("Test knet_host_set_name with (too) long name\n"); memset(longhostname, 'a', sizeof(longhostname)); longhostname[KNET_MAX_HOST_LEN] = '\0'; if ((!knet_host_set_name(knet_h1, 1, longhostname)) || (errno != EINVAL)) { printf("knet_host_set_name accepted invalid (too long) name or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_host_set_policy.c b/libknet/tests/api_knet_host_set_policy.c index c04287b3..c3f087d5 100644 --- a/libknet/tests/api_knet_host_set_policy.c +++ b/libknet/tests/api_knet_host_set_policy.c @@ -1,62 +1,62 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_host_set_policy incorrect knet_h\n"); if ((!knet_host_set_policy(NULL, 1, KNET_LINK_POLICY_PASSIVE)) || (errno != EINVAL)) { printf("knet_host_set_policy accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_host_set_policy incorrect host_id\n"); FAIL_ON_SUCCESS(knet_host_set_policy(knet_h1, 1, KNET_LINK_POLICY_PASSIVE), EINVAL); printf("Test knet_host_set_policy incorrect policy\n"); FAIL_ON_SUCCESS(knet_host_set_policy(knet_h1, 1, KNET_LINK_POLICY_RR + 1), EINVAL); printf("Test knet_host_set_policy correct policy\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(knet_host_set_policy(knet_h1, 1, KNET_LINK_POLICY_RR)); if (knet_h1->host_index[1]->link_handler_policy != KNET_LINK_POLICY_RR) { printf("knet_host_set_policy failed to set RR policy for host 1: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_add_acl.c b/libknet/tests/api_knet_link_add_acl.c index 28326974..f0e3d926 100644 --- a/libknet/tests/api_knet_link_add_acl.c +++ b/libknet/tests/api_knet_link_add_acl.c @@ -1,104 +1,104 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct knet_host *host; struct knet_link *link; struct sockaddr_storage lo, lo6; if (make_local_sockaddr(&lo, 0) < 0) { printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); exit(FAIL); } if (make_local_sockaddr6(&lo6, 0) < 0) { printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_link_add_acl incorrect knet_h\n"); if ((!knet_link_add_acl(NULL, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)) || (errno != EINVAL)) { printf("knet_link_add_acl accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_add_acl with unconfigured host\n"); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_add_acl with unconfigured link\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_add_acl with invalid link\n"); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, KNET_MAX_LINK, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_add_acl with invalid ss1\n"); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, 0, NULL, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_add_acl with invalid ss2\n"); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, 0, &lo, NULL, CHECK_TYPE_RANGE, CHECK_ACCEPT), EINVAL); printf("Test knet_link_add_acl with non matching families\n"); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo6, CHECK_TYPE_RANGE, CHECK_ACCEPT), EINVAL); printf("Test knet_link_add_acl with wrong check_type\n"); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_RANGE + CHECK_TYPE_MASK + CHECK_TYPE_ADDRESS + 1, CHECK_ACCEPT), EINVAL); printf("Test knet_link_add_acl with wrong acceptreject\n"); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT + CHECK_REJECT + 1), EINVAL); printf("Test knet_link_add_acl with point to point link\n"); FAIL_ON_ERR (_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_SUCCESS(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); printf("Test knet_link_add_acl with dynamic link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 1, &lo)); host = knet_h1->host_index[1]; link = &host->link[0]; if (link->access_list_match_entry_head) { printf("match list not empty!"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)); if (!link->access_list_match_entry_head) { printf("match list empty!"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_clear_acl.c b/libknet/tests/api_knet_link_clear_acl.c index 22b822d7..b1643142 100644 --- a/libknet/tests/api_knet_link_clear_acl.c +++ b/libknet/tests/api_knet_link_clear_acl.c @@ -1,88 +1,88 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct knet_host *host; struct knet_link *link; struct sockaddr_storage lo; printf("Test knet_link_clear_acl incorrect knet_h\n"); if ((!knet_link_clear_acl(NULL, 1, 0)) || (errno != EINVAL)) { printf("knet_link_clear_acl accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_clear_acl with unconfigured host\n"); FAIL_ON_SUCCESS(knet_link_clear_acl(knet_h1, 1, 0), EINVAL); printf("Test knet_link_clear_acl with unconfigured link\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_clear_acl(knet_h1, 1, 0), EINVAL); printf("Test knet_link_clear_acl with invalid link\n"); FAIL_ON_SUCCESS(knet_link_clear_acl(knet_h1, 1, KNET_MAX_LINK), EINVAL); printf("Test knet_link_clear_acl with point to point link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_SUCCESS(knet_link_clear_acl(knet_h1, 1, 0), EINVAL); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); printf("Test knet_link_clear_acl with dynamic link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 1, &lo)); host = knet_h1->host_index[1]; link = &host->link[0]; if (link->access_list_match_entry_head) { printf("match list NOT empty!"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)); if (!link->access_list_match_entry_head) { printf("match list empty!"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_link_clear_acl(knet_h1, 1, 0)); if (link->access_list_match_entry_head) { printf("match list NOT empty!"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_clear_config.c b/libknet/tests/api_knet_link_clear_config.c index 20f83ff5..ad1d7164 100644 --- a/libknet/tests/api_knet_link_clear_config.c +++ b/libknet/tests/api_knet_link_clear_config.c @@ -1,68 +1,68 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int res; struct sockaddr_storage lo; printf("Test knet_link_clear_config incorrect knet_h\n"); if ((!knet_link_clear_config(NULL, 1, 0)) || (errno != EINVAL)) { printf("knet_link_clear_config accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_clear_config with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_clear_config(knet_h1, 1, 0), EINVAL); printf("Test knet_link_clear_config with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_clear_config(knet_h1, 1, KNET_MAX_LINK), EINVAL); printf("Test knet_link_clear_config with unconfigured linkid\n"); FAIL_ON_SUCCESS(knet_link_clear_config(knet_h1, 1, 0), EINVAL); printf("Test knet_link_clear_config with enabled linkid\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_SUCCESS(knet_link_clear_config(knet_h1, 1, 0), EBUSY); printf("Test knet_link_clear_config with correct data\n"); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 0)); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_enable_status_change_notify.c b/libknet/tests/api_knet_link_enable_status_change_notify.c index b2c28455..64f1a84c 100644 --- a/libknet/tests/api_knet_link_enable_status_change_notify.c +++ b/libknet/tests/api_knet_link_enable_status_change_notify.c @@ -1,85 +1,85 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void link_notify(void *priv_data, knet_node_id_t host_id, uint8_t link_id, uint8_t connected, uint8_t remote, uint8_t external) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_link_enable_status_change_notify incorrect knet_h\n"); if ((!knet_link_enable_status_change_notify(NULL, NULL, link_notify)) || (errno != EINVAL)) { printf("knet_link_enable_status_change_notify accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_enable_status_change_notify with no private_data\n"); FAIL_ON_ERR(knet_link_enable_status_change_notify(knet_h1, NULL, link_notify)); if (knet_h1->link_status_change_notify_fn_private_data != NULL) { printf("knet_link_enable_status_change_notify failed to unset private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_link_enable_status_change_notify with private_data\n"); FAIL_ON_ERR(knet_link_enable_status_change_notify(knet_h1, &private_data, NULL)); if (knet_h1->link_status_change_notify_fn_private_data != &private_data) { printf("knet_link_enable_status_change_notify failed to set private_data"); CLEAN_EXIT(FAIL); } printf("Test knet_link_enable_status_change_notify with no link_notify fn\n"); FAIL_ON_ERR(knet_link_enable_status_change_notify(knet_h1, NULL, NULL)); if (knet_h1->link_status_change_notify_fn != NULL) { printf("knet_link_enable_status_change_notify failed to unset link_notify fn"); CLEAN_EXIT(FAIL); } printf("Test knet_link_enable_status_change_notify with link_notify fn\n"); FAIL_ON_ERR(knet_link_enable_status_change_notify(knet_h1, NULL, link_notify)); if (knet_h1->link_status_change_notify_fn != &link_notify) { printf("knet_link_enable_status_change_notify failed to set link_notify fn"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_get_config.c b/libknet/tests/api_knet_link_get_config.c index d3326c7e..0bee561c 100644 --- a/libknet/tests/api_knet_link_get_config.c +++ b/libknet/tests/api_knet_link_get_config.c @@ -1,134 +1,134 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct sockaddr_storage lo, get_src, get_dst; uint8_t dynamic = 0, transport = 0; uint64_t flags; printf("Test knet_link_get_config incorrect knet_h\n"); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); if ((!knet_link_get_config(NULL, 1, 0, &transport, &get_src, &get_dst, &dynamic, &flags)) || (errno != EINVAL)) { printf("knet_link_get_config accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_get_config with unconfigured host_id\n"); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); FAIL_ON_SUCCESS(knet_link_get_config(knet_h1, 1, 0, &transport, &get_src, &get_dst, &dynamic, &flags), EINVAL); printf("Test knet_link_get_config with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); FAIL_ON_SUCCESS(knet_link_get_config(knet_h1, 1, KNET_MAX_LINK, &transport, &get_src, &get_dst, &dynamic, &flags), EINVAL); printf("Test knet_link_get_config with incorrect src_addr\n"); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); FAIL_ON_SUCCESS(knet_link_get_config(knet_h1, 1, 0, &transport, NULL, &get_dst, &dynamic, &flags), EINVAL); printf("Test knet_link_get_config with incorrect dynamic\n"); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); FAIL_ON_SUCCESS(knet_link_get_config(knet_h1, 1, 0, &transport, &get_src, &get_dst, NULL, &flags), EINVAL); printf("Test knet_link_get_config with unconfigured link\n"); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); FAIL_ON_SUCCESS(knet_link_get_config(knet_h1, 1, 0, &transport, &get_src, &get_dst, &dynamic, &flags), EINVAL); printf("Test knet_link_get_config with incorrect dst_addr\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); FAIL_ON_SUCCESS(knet_link_get_config(knet_h1, 1, 0, &transport, &get_src, NULL, &dynamic, &flags), EINVAL); if (dynamic) { printf("knet_link_get_config returned invalid dynamic status\n"); CLEAN_EXIT(FAIL); } printf("Test knet_link_get_config with correct parameters for static link\n"); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); FAIL_ON_ERR(knet_link_get_config(knet_h1, 1, 0, &transport, &get_src, &get_dst, &dynamic, &flags)); if (transport != KNET_TRANSPORT_UDP) { printf("knet_link_get_config returned incorrect transport: %d\n", transport); CLEAN_EXIT(FAIL); } if ((dynamic) || (memcmp(&lo, &get_src, sizeof(struct sockaddr_storage))) || (memcmp(&lo, &get_dst, sizeof(struct sockaddr_storage)))) { printf("knet_link_get_config returned invalid data\n"); CLEAN_EXIT(FAIL); } printf("Test knet_link_get_config with correct parameters for dynamic link\n"); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 1, &lo)); memset(&get_src, 0, sizeof(struct sockaddr_storage)); memset(&get_dst, 0, sizeof(struct sockaddr_storage)); FAIL_ON_ERR(knet_link_get_config(knet_h1, 1, 0, &transport, &get_src, &get_dst, &dynamic, &flags)); if ((!dynamic) || (memcmp(&lo, &get_src, sizeof(struct sockaddr_storage)))) { printf("knet_link_get_config returned invalid data\n"); CLEAN_EXIT(FAIL); } printf("Test knet_link_get_config NULL transport ptr\n"); FAIL_ON_SUCCESS(knet_link_get_config(knet_h1, 1, 0, NULL, &get_src, &get_dst, &dynamic, &flags), EINVAL); printf("Test knet_link_get_config with flags\n"); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, KNET_LINK_FLAG_TRAFFICHIPRIO, AF_INET, 1, &lo)); FAIL_ON_ERR(knet_link_get_config(knet_h1, 1, 0, &transport, &get_src, &get_dst, &dynamic, &flags)); if (flags != KNET_LINK_FLAG_TRAFFICHIPRIO) { printf("knet_link_get_config returned no flags\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_get_enable.c b/libknet/tests/api_knet_link_get_enable.c index 49067d44..2a4ef0f6 100644 --- a/libknet/tests/api_knet_link_get_enable.c +++ b/libknet/tests/api_knet_link_get_enable.c @@ -1,78 +1,78 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; unsigned int enabled; struct sockaddr_storage lo; printf("Test knet_link_get_enable incorrect knet_h\n"); if ((!knet_link_get_enable(NULL, 1, 0, &enabled)) || (errno != EINVAL)) { printf("knet_link_get_enable accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_get_enable with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_get_enable(knet_h1, 1, 0, &enabled), EINVAL); printf("Test knet_link_get_enable with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_get_enable(knet_h1, 1, KNET_MAX_LINK, &enabled), EINVAL); printf("Test knet_link_get_enable with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_get_enable(knet_h1, 1, 0, &enabled), EINVAL); printf("Test knet_link_get_enable without enabled\n"); FAIL_ON_SUCCESS(knet_link_get_enable(knet_h1, 1, 0, NULL), EINVAL); printf("Test knet_link_get_enable with disabled link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_get_enable(knet_h1, 1, 0, &enabled)); if (enabled) { printf("knet_link_get_enable returned incorrect value"); CLEAN_EXIT(FAIL); } printf("Test knet_link_get_enable with enabled link\n"); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_link_get_enable(knet_h1, 1, 0, &enabled)); if (!enabled) { printf("knet_link_get_enable returned incorrect value"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_get_link_list.c b/libknet/tests/api_knet_link_get_link_list.c index 248e2e76..23fcd943 100644 --- a/libknet/tests/api_knet_link_get_link_list.c +++ b/libknet/tests/api_knet_link_get_link_list.c @@ -1,77 +1,77 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; uint8_t link_ids[KNET_MAX_LINK]; size_t link_ids_entries = 0; struct sockaddr_storage lo; memset(&link_ids, 1, sizeof(link_ids)); printf("Test knet_link_get_link_list incorrect knet_h\n"); if ((!knet_link_get_link_list(NULL, 1, link_ids, &link_ids_entries)) || (errno != EINVAL)) { printf("knet_link_get_link_list accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_get_link_list with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_get_link_list(knet_h1, 1, link_ids, &link_ids_entries), EINVAL); printf("Test knet_link_get_link_list with incorrect link_id\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_get_link_list(knet_h1, 1, NULL, &link_ids_entries), EINVAL); printf("Test knet_link_get_link_list with incorrect link_ids_entries\n"); FAIL_ON_SUCCESS(knet_link_get_link_list(knet_h1, 1, link_ids, NULL), EINVAL); printf("Test knet_link_get_link_list with no links\n"); FAIL_ON_ERR(knet_link_get_link_list(knet_h1, 1, link_ids, &link_ids_entries)); if (link_ids_entries != 0) { printf("knet_link_get_link_list returned incorrect number of links"); CLEAN_EXIT(FAIL); } printf("Test knet_link_get_link_list with 1 link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_get_link_list(knet_h1, 1, link_ids, &link_ids_entries)); if ((link_ids_entries != 1) || (link_ids[0] != 0)) { printf("knet_link_get_link_list returned incorrect values"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_get_ping_timers.c b/libknet/tests/api_knet_link_get_ping_timers.c index 7a5af7ca..7e87b0a3 100644 --- a/libknet/tests/api_knet_link_get_ping_timers.c +++ b/libknet/tests/api_knet_link_get_ping_timers.c @@ -1,82 +1,82 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; time_t interval = 0, timeout = 0; unsigned int precision = 0; struct sockaddr_storage lo; printf("Test knet_link_get_ping_timers incorrect knet_h\n"); if ((!knet_link_get_ping_timers(NULL, 1, 0, &interval, &timeout, &precision)) || (errno != EINVAL)) { printf("knet_link_get_ping_timers accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_get_ping_timers with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_get_ping_timers(knet_h1, 1, 0, &interval, &timeout, &precision), EINVAL); printf("Test knet_link_get_ping_timers with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_get_ping_timers(knet_h1, 1, KNET_MAX_LINK, &interval, &timeout, &precision), EINVAL); printf("Test knet_link_get_ping_timers with incorrect interval\n"); FAIL_ON_SUCCESS(knet_link_get_ping_timers(knet_h1, 1, 0, NULL, &timeout, &precision), EINVAL); printf("Test knet_link_get_ping_timers with incorrect timeout\n"); FAIL_ON_SUCCESS(knet_link_get_ping_timers(knet_h1, 1, 0, &interval, NULL, &precision), EINVAL); printf("Test knet_link_get_ping_timers with incorrect interval\n"); FAIL_ON_SUCCESS(knet_link_get_ping_timers(knet_h1, 1, 0, &interval, &timeout, NULL), EINVAL); printf("Test knet_link_get_ping_timers with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_get_ping_timers(knet_h1, 1, 0, &interval, &timeout, &precision), EINVAL); printf("Test knet_link_get_ping_timers with correct values\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_get_ping_timers(knet_h1, 1, 0, &interval, &timeout, &precision)); printf("DEFAULT: int: %ld timeout: %ld prec: %u\n", (long int)interval, (long int)timeout, precision); if ((interval != KNET_LINK_DEFAULT_PING_INTERVAL) || (timeout != KNET_LINK_DEFAULT_PING_TIMEOUT) || (precision != KNET_LINK_DEFAULT_PING_PRECISION)) { printf("knet_link_get_ping_timers failed to set values\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_get_pong_count.c b/libknet/tests/api_knet_link_get_pong_count.c index 313797b3..e142c515 100644 --- a/libknet/tests/api_knet_link_get_pong_count.c +++ b/libknet/tests/api_knet_link_get_pong_count.c @@ -1,72 +1,72 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; uint8_t pong_count = 0; struct sockaddr_storage lo; printf("Test knet_link_get_pong_count incorrect knet_h\n"); if ((!knet_link_get_pong_count(NULL, 1, 0, &pong_count)) || (errno != EINVAL)) { printf("knet_link_get_pong_count accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_get_pong_count with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_get_pong_count(knet_h1, 1, 0, &pong_count), EINVAL); printf("Test knet_link_get_pong_count with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_get_pong_count(knet_h1, 1, KNET_MAX_LINK, &pong_count), EINVAL); printf("Test knet_link_get_pong_count with incorrect pong count\n"); FAIL_ON_SUCCESS(knet_link_get_pong_count(knet_h1, 1, 0, NULL), EINVAL); printf("Test knet_link_get_pong_count with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_get_pong_count(knet_h1, 1, 0, &pong_count), EINVAL); printf("Test knet_link_get_pong_count with correct values\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_pong_count(knet_h1, 1, 0, 3)); FAIL_ON_ERR(knet_link_get_pong_count(knet_h1, 1, 0, &pong_count)); if (pong_count != 3) { printf("knet_link_get_pong_count failed to get correct values\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_get_priority.c b/libknet/tests/api_knet_link_get_priority.c index 95efb42f..0370f979 100644 --- a/libknet/tests/api_knet_link_get_priority.c +++ b/libknet/tests/api_knet_link_get_priority.c @@ -1,72 +1,72 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; uint8_t priority = 0; struct sockaddr_storage lo; printf("Test knet_link_get_priority incorrect knet_h\n"); if ((!knet_link_get_priority(NULL, 1, 0, &priority)) || (errno != EINVAL)) { printf("knet_link_get_priority accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_get_priority with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_get_priority(knet_h1, 1, 0, &priority), EINVAL); printf("Test knet_link_get_priority with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_get_priority(knet_h1, 1, KNET_MAX_LINK, &priority), EINVAL); printf("Test knet_link_get_priority with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_get_priority(knet_h1, 1, 0, &priority), EINVAL); printf("Test knet_link_get_priority with incorrect priority\n"); FAIL_ON_SUCCESS(knet_link_get_priority(knet_h1, 1, 0, NULL), EINVAL); printf("Test knet_link_get_priority with correct values\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_priority(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_link_get_priority(knet_h1, 1, 0, &priority)); if (priority != 1) { printf("knet_link_get_priority failed to get correct values\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_get_status.c b/libknet/tests/api_knet_link_get_status.c index ce0a9c65..d35ebab9 100644 --- a/libknet/tests/api_knet_link_get_status.c +++ b/libknet/tests/api_knet_link_get_status.c @@ -1,69 +1,69 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct knet_link_status status; struct sockaddr_storage lo; printf("Test knet_link_get_status incorrect knet_h\n"); memset(&status, 0, sizeof(struct knet_link_status)); if ((!knet_link_get_status(NULL, 1, 0, &status, sizeof(struct knet_link_status))) || (errno != EINVAL)) { printf("knet_link_get_status accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_get_status with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_get_status(knet_h1, 1, 0, &status, sizeof(struct knet_link_status)), EINVAL); printf("Test knet_link_get_status with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_get_status(knet_h1, 1, KNET_MAX_LINK, &status, sizeof(struct knet_link_status)), EINVAL); printf("Test knet_link_get_status with incorrect status\n"); FAIL_ON_SUCCESS(knet_link_get_status(knet_h1, 1, 0, NULL, 0), EINVAL); printf("Test knet_link_get_status with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_get_status(knet_h1, 1, 0, &status, sizeof(struct knet_link_status)), EINVAL); printf("Test knet_link_get_status with correct values\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_get_status(knet_h1, 1, 0, &status, sizeof(struct knet_link_status))); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_insert_acl.c b/libknet/tests/api_knet_link_insert_acl.c index 498e23e5..e5df0524 100644 --- a/libknet/tests/api_knet_link_insert_acl.c +++ b/libknet/tests/api_knet_link_insert_acl.c @@ -1,108 +1,108 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct knet_host *host; struct knet_link *link; struct sockaddr_storage lo, lo6; if (make_local_sockaddr(&lo, 0) < 0) { printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); exit(FAIL); } if (make_local_sockaddr6(&lo6, 0) < 0) { printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_link_insert_acl incorrect knet_h\n"); if ((!knet_link_insert_acl(NULL, 1, 0, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)) || (errno != EINVAL)) { printf("knet_link_insert_acl accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_insert_acl with unconfigured host\n"); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, 0, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_insert_acl with unconfigured link\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, 0, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_insert_acl with invalid link\n"); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, KNET_MAX_LINK, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_insert_acl with invalid ss1\n"); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, 0, 0, NULL, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_insert_acl with invalid ss2\n"); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, 0, 0, &lo, NULL, CHECK_TYPE_RANGE, CHECK_ACCEPT), EINVAL); printf("Test knet_link_insert_acl with non matching families\n"); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, 0, 0, &lo, &lo6, CHECK_TYPE_RANGE, CHECK_ACCEPT), EINVAL); printf("Test knet_link_insert_acl with wrong check_type\n"); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, 0, 0, &lo, &lo, CHECK_TYPE_RANGE + CHECK_TYPE_MASK + CHECK_TYPE_ADDRESS + 1, CHECK_ACCEPT), EINVAL); printf("Test knet_link_insert_acl with wrong acceptreject\n"); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, 0, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT + CHECK_REJECT + 1), EINVAL); printf("Test knet_link_insert_acl with point to point link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_SUCCESS(knet_link_insert_acl(knet_h1, 1, 0, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); printf("Test knet_link_insert_acl with dynamic link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 1, &lo)); host = knet_h1->host_index[1]; link = &host->link[0]; if (link->access_list_match_entry_head) { printf("match list not empty!"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_link_insert_acl(knet_h1, 1, 0, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)); if (!link->access_list_match_entry_head) { printf("match list empty!"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_rm_acl.c b/libknet/tests/api_knet_link_rm_acl.c index 13272bbf..b4a34090 100644 --- a/libknet/tests/api_knet_link_rm_acl.c +++ b/libknet/tests/api_knet_link_rm_acl.c @@ -1,107 +1,107 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct knet_host *host; struct knet_link *link; struct sockaddr_storage lo, lo6; if (make_local_sockaddr(&lo, 0) < 0) { printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); exit(FAIL); } if (make_local_sockaddr6(&lo6, 0) < 0) { printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_link_rm_acl incorrect knet_h\n"); if ((!knet_link_rm_acl(NULL, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)) || (errno != EINVAL)) { printf("knet_link_rm_acl accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_rm_acl with unconfigured host\n"); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_rm_acl with unconfigured link\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_rm_acl with invalid link\n"); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, KNET_MAX_LINK, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_rm_acl with invalid ss1\n"); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, 0, NULL, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); printf("Test knet_link_rm_acl with invalid ss2\n"); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, 0, &lo, NULL, CHECK_TYPE_RANGE, CHECK_ACCEPT), EINVAL); printf("Test knet_link_rm_acl with non matching families\n"); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, 0, &lo, &lo6, CHECK_TYPE_RANGE, CHECK_ACCEPT), EINVAL); printf("Test knet_link_rm_acl with wrong check_type\n"); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_RANGE + CHECK_TYPE_MASK + CHECK_TYPE_ADDRESS + 1, CHECK_ACCEPT), EINVAL); printf("Test knet_link_rm_acl with wrong acceptreject\n"); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT + CHECK_REJECT + 1), EINVAL); printf("Test knet_link_rm_acl with point to point link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_SUCCESS(knet_link_rm_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT), EINVAL); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); printf("Test knet_link_rm_acl with dynamic link\n"); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 1, &lo)); host = knet_h1->host_index[1]; link = &host->link[0]; if (link->access_list_match_entry_head) { printf("match list not empty!"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_link_add_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)); FAIL_ON_ERR(knet_link_rm_acl(knet_h1, 1, 0, &lo, &lo, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)); if (link->access_list_match_entry_head) { printf("match list NOT empty!"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_set_config.c b/libknet/tests/api_knet_link_set_config.c index 01d450b1..89892150 100644 --- a/libknet/tests/api_knet_link_set_config.c +++ b/libknet/tests/api_knet_link_set_config.c @@ -1,132 +1,132 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "links.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; struct knet_host *host; struct knet_link *link; int logfds[2]; char lo_portstr[32]; struct sockaddr_storage lo, lo6; struct sockaddr_in *lo_in = (struct sockaddr_in *)&lo; struct knet_link_status link_status; if (make_local_sockaddr(&lo, -1) < 0) { printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); exit(FAIL); } sprintf(lo_portstr, "%d", ntohs(lo_in->sin_port)); printf("Test knet_link_set_config incorrect knet_h\n"); if ((!knet_link_set_config(NULL, 1, 0, KNET_TRANSPORT_UDP, &lo, &lo, 0)) || (errno != EINVAL)) { printf("knet_link_set_config accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_set_config with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &lo, &lo, 0), EINVAL); printf("Test knet_link_set_config with bad transport type\n"); FAIL_ON_SUCCESS(knet_link_set_config(knet_h1, 1, 0, KNET_MAX_TRANSPORTS, &lo, &lo, 0), EINVAL); printf("Test knet_link_set_config with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_set_config(knet_h1, 1, KNET_MAX_LINK, KNET_TRANSPORT_UDP, &lo, &lo, 0), EINVAL); printf("Test knet_link_set_config with incorrect src_addr\n"); FAIL_ON_SUCCESS(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, NULL, &lo, 0), EINVAL); printf("Test knet_link_set_config with conflicting address families\n"); FAIL_ON_ERR(make_local_sockaddr6(&lo6, -1)); FAIL_ON_SUCCESS(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &lo, &lo6, 0), EINVAL); printf("Test knet_link_set_config with dynamic dst_addr\n"); FAIL_ON_ERR(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &lo, NULL, 0)); host = knet_h1->host_index[1]; link = &host->link[0]; if (link->access_list_match_entry_head) { printf("found access lists for dynamic dst_addr!\n"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_link_get_status(knet_h1, 1, 0, &link_status, sizeof(struct knet_link_status))); if ((link_status.enabled != 0) || (strcmp(link_status.src_ipaddr, "127.0.0.1")) || (strcmp(link_status.src_port, lo_portstr)) || (knet_h1->host_index[1]->link[0].dynamic != KNET_LINK_DYNIP)) { printf("knet_link_set_config failed to set configuration. enabled: %d src_addr %s src_port %s dynamic %u\n", link_status.enabled, link_status.src_ipaddr, link_status.src_port, knet_h1->host_index[1]->link[0].dynamic); CLEAN_EXIT(FAIL); } printf("Test knet_link_set_config with dynamic link (0) and static link (1)\n"); FAIL_ON_SUCCESS(knet_link_set_config(knet_h1, 1, 1, KNET_TRANSPORT_UDP, &lo, &lo, 0), EINVAL); printf("Test knet_link_set_config with already configured link\n"); FAIL_ON_SUCCESS(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &lo, NULL, 0), EBUSY); printf("Test knet_link_set_config with link enabled\n"); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_link_get_status(knet_h1, 1, 0, &link_status, sizeof(struct knet_link_status))); FAIL_ON_SUCCESS(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &lo, NULL, 0), EBUSY); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 0)); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); printf("Test knet_link_set_config with static dst_addr\n"); FAIL_ON_ERR(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &lo, &lo, 0)); host = knet_h1->host_index[1]; link = &host->link[0]; if (!link->access_list_match_entry_head) { printf("Unable to find default access lists for static dst_addr!\n"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_link_get_status(knet_h1, 1, 0, &link_status, sizeof(struct knet_link_status))); if ((link_status.enabled != 0) || (strcmp(link_status.src_ipaddr, "127.0.0.1")) || (strcmp(link_status.src_port, lo_portstr)) || (strcmp(link_status.dst_ipaddr, "127.0.0.1")) || (strcmp(link_status.dst_port, lo_portstr)) || (knet_h1->host_index[1]->link[0].dynamic != KNET_LINK_STATIC)) { printf("knet_link_set_config failed to set configuration. enabled: %d src_addr %s src_port %s dst_addr %s dst_port %s dynamic %u\n", link_status.enabled, link_status.src_ipaddr, link_status.src_port, link_status.dst_ipaddr, link_status.dst_port, knet_h1->host_index[1]->link[0].dynamic); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_set_enable.c b/libknet/tests/api_knet_link_set_enable.c index e18acb5e..e65244cc 100644 --- a/libknet/tests/api_knet_link_set_enable.c +++ b/libknet/tests/api_knet_link_set_enable.c @@ -1,161 +1,161 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test_udp(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct sockaddr_storage src, dst; if (make_local_sockaddr(&src, 0) < 0) { printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); exit(FAIL); } if (make_local_sockaddr(&dst, 1) < 0) { printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_link_set_enable incorrect knet_h\n"); if ((!knet_link_set_enable(NULL, 1, 0, 1)) || (errno != EINVAL)) { printf("knet_link_set_enable accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_set_enable with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_set_enable(knet_h1, 1, 0, 1), EINVAL); printf("Test knet_link_set_enable with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_set_enable(knet_h1, 1, KNET_MAX_LINK, 1), EINVAL); printf("Test knet_link_set_enable with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_set_enable(knet_h1, 1, 0, 1), EINVAL); printf("Test knet_link_set_enable with incorrect values\n"); FAIL_ON_ERR(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &src, &dst, 0)); FAIL_ON_SUCCESS(knet_link_set_enable(knet_h1, 1, 0, 2), EINVAL); printf("Test knet_link_set_enable with correct values (1)\n"); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); if (knet_h1->host_index[1]->link[0].status.enabled != 1) { printf("knet_link_set_enable failed to set correct values\n"); CLEAN_EXIT(FAIL); } printf("Test knet_link_set_enable with correct values (0)\n"); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 0)); if (knet_h1->host_index[1]->link[0].status.enabled != 0) { printf("knet_link_set_enable failed to set correct values\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } #ifdef HAVE_NETINET_SCTP_H static void test_sctp(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct sockaddr_storage src, dst; if (make_local_sockaddr(&src, 0) < 0) { printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); exit(FAIL); } if (make_local_sockaddr(&dst, 1) < 0) { printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_link_set_enable incorrect knet_h\n"); if ((!knet_link_set_enable(NULL, 1, 0, 1)) || (errno != EINVAL)) { printf("knet_link_set_enable accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_set_enable with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_set_enable(knet_h1, 1, 0, 1), EINVAL); printf("Test knet_link_set_enable with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_set_enable(knet_h1, 1, KNET_MAX_LINK, 1), EINVAL); printf("Test knet_link_set_enable with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_set_enable(knet_h1, 1, 0, 1), EINVAL); printf("Test knet_link_set_enable with incorrect values\n"); if (knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_SCTP, &src, &dst, 0) < 0) { printf("Unable to configure link: %s\n", strerror(errno)); CLEAN_EXIT(SKIP); } FAIL_ON_SUCCESS(knet_link_set_enable(knet_h1, 1, 0, 2), EINVAL); printf("Test knet_link_set_enable with correct values (1)\n"); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); if (knet_h1->host_index[1]->link[0].status.enabled != 1) { printf("knet_link_set_enable failed to set correct values\n"); CLEAN_EXIT(FAIL); } printf("Wait 2 seconds for sockets to connect\n"); test_sleep(knet_h1, 2); printf("Test knet_link_set_enable with correct values (0)\n"); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 0)); if (knet_h1->host_index[1]->link[0].status.enabled != 0) { printf("knet_link_set_enable failed to set correct values\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } #endif int main(int argc, char *argv[]) { printf("Testing with UDP\n"); test_udp(); #ifdef HAVE_NETINET_SCTP_H printf("Testing with SCTP\n"); test_sctp(); #else printf("Skipping SCTP test. Protocol not supported in this build\n"); #endif return PASS; } diff --git a/libknet/tests/api_knet_link_set_ping_timers.c b/libknet/tests/api_knet_link_set_ping_timers.c index fd94e3c0..89852cb1 100644 --- a/libknet/tests/api_knet_link_set_ping_timers.c +++ b/libknet/tests/api_knet_link_set_ping_timers.c @@ -1,88 +1,88 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct sockaddr_storage src, dst; if (make_local_sockaddr(&src, 0) < 0) { printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); exit(FAIL); } if (make_local_sockaddr(&dst, 1) < 0) { printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_link_set_ping_timers incorrect knet_h\n"); if ((!knet_link_set_ping_timers(NULL, 1, 0, 1000, 2000, 2048)) || (errno != EINVAL)) { printf("knet_link_set_ping_timers accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_set_ping_timers with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_set_ping_timers(knet_h1, 1, 0, 1000, 2000, 2048), EINVAL); printf("Test knet_link_set_ping_timers with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_set_ping_timers(knet_h1, 1, KNET_MAX_LINK, 1000, 2000, 2048), EINVAL); printf("Test knet_link_set_ping_timers with incorrect interval\n"); FAIL_ON_SUCCESS(knet_link_set_ping_timers(knet_h1, 1, 0, 0, 2000, 2048), EINVAL); printf("Test knet_link_set_ping_timers with 0 timeout\n"); FAIL_ON_SUCCESS(knet_link_set_ping_timers(knet_h1, 1, 0, 1000, 0, 2048), ENOSYS); printf("Test knet_link_set_ping_timers with incorrect interval\n"); FAIL_ON_SUCCESS(knet_link_set_ping_timers(knet_h1, 1, 0, 1000, 2000, 0), EINVAL); printf("Test knet_link_set_ping_timers with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_set_ping_timers(knet_h1, 1, 0, 1000, 2000, 2048), EINVAL); printf("Test knet_link_set_ping_timers with correct values\n"); FAIL_ON_ERR(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &src, &dst, 0)); FAIL_ON_ERR(knet_link_set_ping_timers(knet_h1, 1, 0, 1000, 2000, 2048)); if ((knet_h1->host_index[1]->link[0].ping_interval != 1000000) || (knet_h1->host_index[1]->link[0].pong_timeout != 2000000) || (knet_h1->host_index[1]->link[0].latency_max_samples != 2048)) { printf("knet_link_set_ping_timers failed to set values\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_set_pong_count.c b/libknet/tests/api_knet_link_set_pong_count.c index 9463c1e1..63cfbc86 100644 --- a/libknet/tests/api_knet_link_set_pong_count.c +++ b/libknet/tests/api_knet_link_set_pong_count.c @@ -1,79 +1,79 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct sockaddr_storage src, dst; if (make_local_sockaddr(&src, 0) < 0) { printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); exit(FAIL); } if (make_local_sockaddr(&dst, 1) < 0) { printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_link_set_pong_count incorrect knet_h\n"); if ((!knet_link_set_pong_count(NULL, 1, 0, 2)) || (errno != EINVAL)) { printf("knet_link_set_pong_count accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_set_pong_count with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_set_pong_count(knet_h1, 1, 0, 2), EINVAL); printf("Test knet_link_set_pong_count with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_set_pong_count(knet_h1, 1, KNET_MAX_LINK, 2), EINVAL); printf("Test knet_link_set_pong_count with incorrect pong count\n"); FAIL_ON_SUCCESS(knet_link_set_pong_count(knet_h1, 1, 0, 0), EINVAL); printf("Test knet_link_set_pong_count with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_set_pong_count(knet_h1, 1, 0, 2), EINVAL); printf("Test knet_link_set_pong_count with correct values\n"); FAIL_ON_ERR(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &src, &dst, 0)); FAIL_ON_ERR(knet_link_set_pong_count(knet_h1, 1, 0, 3)); if (knet_h1->host_index[1]->link[0].pong_count != 3) { printf("knet_link_set_pong_count failed to set correct values\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_link_set_priority.c b/libknet/tests/api_knet_link_set_priority.c index 875b7174..9b4cf27e 100644 --- a/libknet/tests/api_knet_link_set_priority.c +++ b/libknet/tests/api_knet_link_set_priority.c @@ -1,77 +1,77 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "link.h" #include "netutils.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; struct sockaddr_storage src, dst; if (make_local_sockaddr(&src, 0) < 0) { printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); exit(FAIL); } if (make_local_sockaddr(&dst, 1) < 0) { printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); exit(FAIL); } printf("Test knet_link_set_priority incorrect knet_h\n"); if ((!knet_link_set_priority(NULL, 1, 0, 1)) || (errno != EINVAL)) { printf("knet_link_set_priority accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_link_set_priority with unconfigured host_id\n"); FAIL_ON_SUCCESS(knet_link_set_priority(knet_h1, 1, 0, 1), EINVAL); printf("Test knet_link_set_priority with incorrect linkid\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_SUCCESS(knet_link_set_priority(knet_h1, 1, KNET_MAX_LINK, 2), EINVAL); printf("Test knet_link_set_priority with unconfigured link\n"); FAIL_ON_SUCCESS(knet_link_set_priority(knet_h1, 1, 0, 1), EINVAL); printf("Test knet_link_set_priority with correct values\n"); FAIL_ON_ERR(knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, &src, &dst, 0)); FAIL_ON_ERR(knet_link_set_priority(knet_h1, 1, 0, 3)); if (knet_h1->host_index[1]->link[0].priority != 3) { printf("knet_link_set_priority failed to set correct values\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_log_get_loglevel.c b/libknet/tests/api_knet_log_get_loglevel.c index 40d1d545..1445c4fe 100644 --- a/libknet/tests/api_knet_log_get_loglevel.c +++ b/libknet/tests/api_knet_log_get_loglevel.c @@ -1,62 +1,62 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; uint8_t level; int logfds[2]; printf("Test knet_log_get_loglevel incorrect knet_h\n"); if ((!knet_log_get_loglevel(NULL, KNET_SUB_UNKNOWN, &level)) || (errno != EINVAL)) { printf("knet_log_get_loglevel accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_INFO, knet_h); printf("Test knet_log_get_loglevel incorrect subsystem\n"); FAIL_ON_SUCCESS(knet_log_get_loglevel(knet_h1, KNET_SUB_UNKNOWN - 1, &level), EINVAL); printf("Test knet_log_get_loglevel incorrect log level\n"); FAIL_ON_SUCCESS(knet_log_get_loglevel(knet_h1, KNET_SUB_UNKNOWN, NULL), EINVAL); printf("Test knet_log_get_loglevel with valid parameters\n"); FAIL_ON_ERR(knet_log_get_loglevel(knet_h1, KNET_SUB_UNKNOWN, &level)); if (knet_h1->log_levels[KNET_SUB_UNKNOWN] != level) { printf("knet_log_get_loglevel failed to get the right value\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_log_get_loglevel_id.c b/libknet/tests/api_knet_log_get_loglevel_id.c index 8d474a4d..eb16dad5 100644 --- a/libknet/tests/api_knet_log_get_loglevel_id.c +++ b/libknet/tests/api_knet_log_get_loglevel_id.c @@ -1,47 +1,47 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "test-common.h" static void test(void) { uint8_t res; printf("Testing knet_log_get_loglevel_id normal lookup\n"); res = knet_log_get_loglevel_id("debug"); if (res != KNET_LOG_DEBUG) { printf("knet_log_get_loglevel_id failed to get correct log level id. got: %u expected: %d\n", res, KNET_LOG_DEBUG); exit(FAIL); } printf("Testing knet_log_get_loglevel_id bad lookup\n"); res = knet_log_get_loglevel_id("whatever"); if (res != KNET_LOG_ERR) { printf("knet_log_get_loglevel_id failed to get correct log level id. got: %u expected: %d\n", res, KNET_LOG_ERR); exit(FAIL); } } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_log_get_loglevel_name.c b/libknet/tests/api_knet_log_get_loglevel_name.c index dce9ad77..0d51d720 100644 --- a/libknet/tests/api_knet_log_get_loglevel_name.c +++ b/libknet/tests/api_knet_log_get_loglevel_name.c @@ -1,47 +1,47 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "test-common.h" static void test(void) { const char *res; printf("Testing knet_log_get_loglevel_name normal lookup\n"); res = knet_log_get_loglevel_name(KNET_LOG_DEBUG); if (strcmp(res, "debug")) { printf("knet_log_get_loglevel_name failed to get correct log level name. got: %s expected: debug\n", res); exit(FAIL); } printf("Testing knet_log_get_loglevel_name bad lookup\n"); res = knet_log_get_loglevel_name(KNET_LOG_DEBUG+1); if (strcmp(res, "ERROR")) { printf("knet_log_get_loglevel_name failed to get correct log level name. got: %s expected: ERROR\n", res); exit(FAIL); } } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_log_get_subsystem_id.c b/libknet/tests/api_knet_log_get_subsystem_id.c index 8f9c89ed..ede85e0a 100644 --- a/libknet/tests/api_knet_log_get_subsystem_id.c +++ b/libknet/tests/api_knet_log_get_subsystem_id.c @@ -1,47 +1,47 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "test-common.h" static void test(void) { uint8_t res; printf("Testing knet_log_get_subsystem_id normal lookup\n"); res = knet_log_get_subsystem_id("nsscrypto"); if (res != KNET_SUB_NSSCRYPTO) { printf("knet_log_get_subsystem_id failed to get correct log subsystem id. got: %u expected: %d\n", res, KNET_SUB_NSSCRYPTO); exit(FAIL); } printf("Testing knet_log_get_subsystem_id bad lookup\n"); res = knet_log_get_subsystem_id("whatever"); if (res != KNET_SUB_UNKNOWN) { printf("knet_log_get_subsystem_id failed to get correct log subsystem id. got: %u expected: %d\n", res, KNET_SUB_UNKNOWN); exit(FAIL); } } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_log_get_subsystem_name.c b/libknet/tests/api_knet_log_get_subsystem_name.c index b82d6d4b..ff6f104d 100644 --- a/libknet/tests/api_knet_log_get_subsystem_name.c +++ b/libknet/tests/api_knet_log_get_subsystem_name.c @@ -1,55 +1,55 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "test-common.h" static void test(void) { const char *res; printf("Testing knet_log_get_subsystem_name normal lookup\n"); res = knet_log_get_subsystem_name(KNET_SUB_NSSCRYPTO); if (strcmp(res, "nsscrypto")) { printf("knet_log_get_subsystem_name failed to get correct log subsystem name. got: %s expected: nsscrypto\n", res); exit(FAIL); } printf("Testing knet_log_get_subsystem_name bad lookup (within boundaries)\n"); res = knet_log_get_subsystem_name(KNET_SUB_UNKNOWN - 1); if (strcmp(res, "unknown")) { printf("knet_log_get_subsystem_name failed to get correct log subsystem name. got: %s expected: common\n", res); exit(FAIL); } printf("Testing knet_log_get_subsystem_name bad lookup (outside boundaries)\n"); res = knet_log_get_subsystem_name(KNET_MAX_SUBSYSTEMS); if (strcmp(res, "unknown")) { printf("knet_log_get_subsystem_name failed to get correct log subsystem name. got: %s expected: common\n", res); exit(FAIL); } } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_log_set_loglevel.c b/libknet/tests/api_knet_log_set_loglevel.c index a0753564..e8cc1582 100644 --- a/libknet/tests/api_knet_log_set_loglevel.c +++ b/libknet/tests/api_knet_log_set_loglevel.c @@ -1,64 +1,64 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static void test(void) { knet_handle_t knet_h1, knet_h[2]; int res; int logfds[2]; printf("Test knet_log_set_loglevel incorrect knet_h\n"); if ((!knet_log_set_loglevel(NULL, KNET_SUB_COMMON, KNET_LOG_DEBUG)) || (errno != EINVAL)) { printf("knet_log_set_loglevel accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_INFO, knet_h); printf("Test knet_log_set_loglevel incorrect subsystem\n"); FAIL_ON_SUCCESS(knet_log_set_loglevel(knet_h1, KNET_SUB_UNKNOWN - 1, KNET_LOG_DEBUG), EINVAL); printf("Test knet_log_set_loglevel incorrect log level\n"); FAIL_ON_SUCCESS(knet_log_set_loglevel(knet_h1, KNET_SUB_UNKNOWN, KNET_LOG_DEBUG + 1), EINVAL); printf("Test knet_log_set_loglevel with valid parameters\n"); if (knet_h1->log_levels[KNET_SUB_UNKNOWN] != KNET_LOG_INFO) { printf("knet_handle_new did not init log_levels correctly?\n"); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_log_set_loglevel(knet_h1, KNET_SUB_UNKNOWN, KNET_LOG_DEBUG)); if (knet_h1->log_levels[KNET_SUB_UNKNOWN] != KNET_LOG_DEBUG) { printf("knet_log_set_loglevel did not set log level correctly\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_recv.c b/libknet/tests/api_knet_recv.c index 1bf3e843..e2ca2d25 100644 --- a/libknet/tests/api_knet_recv.c +++ b/libknet/tests/api_knet_recv.c @@ -1,121 +1,121 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int datafd = 0; int8_t channel = 0; char recv_buff[KNET_MAX_PACKET_SIZE]; char send_buff[KNET_MAX_PACKET_SIZE]; ssize_t recv_len = 0; int res; struct iovec iov_out[1]; printf("Test knet_recv incorrect knet_h\n"); if ((!knet_recv(NULL, recv_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { printf("knet_recv accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_recv with no recv_buff\n"); FAIL_ON_SUCCESS(knet_recv(knet_h1, NULL, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_recv with invalid recv_buff len (0)\n"); FAIL_ON_SUCCESS(knet_recv(knet_h1, recv_buff, 0, channel), EINVAL); printf("Test knet_recv with invalid recv_buff len (> KNET_MAX_PACKET_SIZE)\n"); FAIL_ON_SUCCESS(knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE + 1, channel), EINVAL); printf("Test knet_recv with invalid channel (-1)\n"); channel = -1; FAIL_ON_SUCCESS(knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_recv with invalid channel (KNET_DATAFD_MAX)\n"); channel = KNET_DATAFD_MAX; FAIL_ON_SUCCESS(knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_recv with unconfigured channel\n"); channel = 0; FAIL_ON_SUCCESS(knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_recv with valid data\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); memset(recv_buff, 0, KNET_MAX_PACKET_SIZE); memset(send_buff, 1, sizeof(send_buff)); iov_out[0].iov_base = (void *)send_buff; iov_out[0].iov_len = sizeof(send_buff); if (writev(knet_h1->sockfd[channel].sockfd[1], iov_out, 1) != sizeof(send_buff)) { printf("Unable to write data: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } recv_len = knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel); if (recv_len <= 0) { printf("knet_recv failed: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } if (recv_len != sizeof(send_buff)) { printf("knet_recv received only %zd bytes: %s\n", recv_len, strerror(errno)); CLEAN_EXIT(FAIL); } if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { printf("knet_recv received bad data\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_send.c b/libknet/tests/api_knet_send.c index f15b59ae..6c71f874 100644 --- a/libknet/tests/api_knet_send.c +++ b/libknet/tests/api_knet_send.c @@ -1,177 +1,177 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(uint8_t transport) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int datafd = 0; int8_t channel = 0; struct knet_link_status link_status; char send_buff[KNET_MAX_PACKET_SIZE + 1]; char recv_buff[KNET_MAX_PACKET_SIZE]; ssize_t send_len = 0; int recv_len = 0; int savederrno; int res; struct sockaddr_storage lo; memset(send_buff, 0, sizeof(send_buff)); printf("Test knet_send incorrect knet_h\n"); if ((!knet_send(NULL, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { printf("knet_send accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); FAIL_ON_ERR(knet_handle_enable_access_lists(knet_h1, 1)); printf("Test knet_send with no send_buff\n"); FAIL_ON_SUCCESS(knet_send(knet_h1, NULL, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_send with invalid send_buff len (0)\n"); FAIL_ON_SUCCESS(knet_send(knet_h1, send_buff, 0, channel), EINVAL); printf("Test knet_send with invalid send_buff len (> KNET_MAX_PACKET_SIZE)\n"); FAIL_ON_SUCCESS(knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE + 1, channel), EINVAL); printf("Test knet_send with invalid channel (-1)\n"); channel = -1; FAIL_ON_SUCCESS(knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_send with invalid channel (KNET_DATAFD_MAX)\n"); channel = KNET_DATAFD_MAX; FAIL_ON_SUCCESS(knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_send with unconfigured channel\n"); channel = 0; FAIL_ON_SUCCESS(knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_send with valid data\n"); FAIL_ON_ERR(knet_handle_enable_access_lists(knet_h1, 1)); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); if (_knet_link_set_config(knet_h1, 1, 0, transport, 0, AF_INET, 0, &lo) < 0 ) { int exit_status = transport == KNET_TRANSPORT_SCTP && errno == EPROTONOSUPPORT ? SKIP : FAIL; printf("Unable to configure link: %s\n", strerror(errno)); CLEAN_EXIT(exit_status); } FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); FAIL_ON_ERR(wait_for_host(knet_h1, 1, 10, logfds[0], stdout)); send_len = knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel); if (send_len <= 0) { printf("knet_send failed: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } if (send_len != sizeof(send_buff) - 1) { printf("knet_send sent only %zd bytes: %s\n", send_len, strerror(errno)); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 0)); FAIL_ON_ERR(wait_for_packet(knet_h1, 10, datafd, logfds[0], stdout)); recv_len = knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel); savederrno = errno; if (recv_len != send_len) { printf("knet_recv received only %d bytes: %s (errno: %d)\n", recv_len, strerror(errno), errno); if ((is_helgrind()) && (recv_len == -1) && (savederrno == EAGAIN)) { printf("helgrind exception. this is normal due to possible timeouts\n"); CLEAN_EXIT(PASS); } CLEAN_EXIT(FAIL); } if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { printf("recv and send buffers are different!\n"); CLEAN_EXIT(FAIL); } /* A sanity check on the stats */ FAIL_ON_ERR(knet_link_get_status(knet_h1, 1, 0, &link_status, sizeof(link_status))); if (link_status.stats.tx_data_packets != 2 || link_status.stats.rx_data_packets != 2 || link_status.stats.tx_data_bytes < KNET_MAX_PACKET_SIZE || link_status.stats.rx_data_bytes < KNET_MAX_PACKET_SIZE || link_status.stats.tx_data_bytes > KNET_MAX_PACKET_SIZE*2 || link_status.stats.rx_data_bytes > KNET_MAX_PACKET_SIZE*2) { printf("stats look wrong: tx_packets: %" PRIu64 " (%" PRIu64 " bytes), rx_packets: %" PRIu64 " (%" PRIu64 " bytes)\n", link_status.stats.tx_data_packets, link_status.stats.tx_data_bytes, link_status.stats.rx_data_packets, link_status.stats.rx_data_bytes); } FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); printf("try to send big packet to local datafd (bypass knet_send)\n"); if (write(datafd, &send_buff, sizeof(send_buff)) != KNET_MAX_PACKET_SIZE + 1) { printf("Error writing to datafd: %s\n", strerror(errno)); } if (!wait_for_packet(knet_h1, 2, datafd, logfds[0], stdout)) { printf("Received unexpected packet!\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { printf("Testing with UDP\n"); test(KNET_TRANSPORT_UDP); #ifdef HAVE_NETINET_SCTP_H printf("Testing with SCTP\n"); test(KNET_TRANSPORT_SCTP); #endif return PASS; } diff --git a/libknet/tests/api_knet_send_compress.c b/libknet/tests/api_knet_send_compress.c index 4758170b..fa84685e 100644 --- a/libknet/tests/api_knet_send_compress.c +++ b/libknet/tests/api_knet_send_compress.c @@ -1,176 +1,176 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "compress.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(const char *model) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int datafd = 0; int8_t channel = 0; struct knet_handle_stats stats; char send_buff[KNET_MAX_PACKET_SIZE]; char recv_buff[KNET_MAX_PACKET_SIZE]; ssize_t send_len = 0; int recv_len = 0; int savederrno; int res; struct sockaddr_storage lo; struct knet_handle_compress_cfg knet_handle_compress_cfg; memset(send_buff, 0, sizeof(send_buff)); setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_send with %s and valid data\n", model); memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); strncpy(knet_handle_compress_cfg.compress_model, model, sizeof(knet_handle_compress_cfg.compress_model) - 1); knet_handle_compress_cfg.compress_level = 4; knet_handle_compress_cfg.compress_threshold = 0; FAIL_ON_ERR(knet_handle_compress(knet_h1, &knet_handle_compress_cfg)); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); FAIL_ON_ERR(wait_for_host(knet_h1, 1, 10, logfds[0], stdout)); send_len = knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel); if (send_len <= 0) { printf("knet_send failed: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } if (send_len != sizeof(send_buff)) { printf("knet_send sent only %zd bytes: %s\n", send_len, strerror(errno)); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 0)); FAIL_ON_ERR(wait_for_packet(knet_h1, 10, datafd, logfds[0], stdout)); recv_len = knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel); savederrno = errno; if (recv_len != send_len) { printf("knet_recv received only %d bytes: %s (errno: %d)\n", recv_len, strerror(errno), errno); if ((is_helgrind()) && (recv_len == -1) && (savederrno == EAGAIN)) { printf("helgrind exception. this is normal due to possible timeouts\n"); CLEAN_EXIT(PASS); } CLEAN_EXIT(FAIL); } if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { printf("recv and send buffers are different!\n"); CLEAN_EXIT(FAIL); } /* A sanity check on the stats */ FAIL_ON_ERR(knet_handle_get_stats(knet_h1, &stats, sizeof(stats))); if (strcmp(model, "none") == 0) { if (stats.tx_compressed_packets != 0 || stats.rx_compressed_packets != 0) { printf("stats look wrong: s/b all 0 for model 'none' tx_packets: %" PRIu64 " (%" PRIu64 "/%" PRIu64 " comp/uncomp), rx_packets: %" PRIu64 " (%" PRIu64 "/%" PRIu64 " comp/uncomp)\n", stats.tx_compressed_packets, stats.tx_compressed_size_bytes, stats.tx_compressed_original_bytes, stats.rx_compressed_packets, stats.rx_compressed_size_bytes, stats.rx_compressed_original_bytes); } } else { if (stats.tx_compressed_packets != 1 || stats.rx_compressed_packets != 1 || stats.tx_compressed_original_bytes < stats.tx_compressed_size_bytes || stats.tx_compressed_original_bytes < stats.tx_compressed_size_bytes) { printf("stats look wrong: tx_packets: %" PRIu64 " (%" PRIu64 "/%" PRIu64 " comp/uncomp), rx_packets: %" PRIu64 " (%" PRIu64 "/%" PRIu64 " comp/uncomp)\n", stats.tx_compressed_packets, stats.tx_compressed_size_bytes, stats.tx_compressed_original_bytes, stats.rx_compressed_packets, stats.rx_compressed_size_bytes, stats.rx_compressed_original_bytes); } } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { struct knet_compress_info compress_list[16]; size_t compress_list_entries; size_t i; memset(compress_list, 0, sizeof(compress_list)); if (knet_get_compress_list(compress_list, &compress_list_entries) < 0) { printf("knet_get_compress_list failed: %s\n", strerror(errno)); return FAIL; } if (compress_list_entries == 0) { printf("no compression modules detected. Skipping\n"); return SKIP; } test("none"); for (i=0; i < compress_list_entries; i++) { test(compress_list[i].name); } return PASS; } diff --git a/libknet/tests/api_knet_send_crypto.c b/libknet/tests/api_knet_send_crypto.c index 3c9f778b..8680c571 100644 --- a/libknet/tests/api_knet_send_crypto.c +++ b/libknet/tests/api_knet_send_crypto.c @@ -1,168 +1,168 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "compress.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static void test(const char *model) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int datafd = 0; int8_t channel = 0; struct knet_handle_stats stats; char send_buff[KNET_MAX_PACKET_SIZE]; char recv_buff[KNET_MAX_PACKET_SIZE]; ssize_t send_len = 0; int recv_len = 0; int savederrno; int res; struct sockaddr_storage lo; struct knet_handle_crypto_cfg knet_handle_crypto_cfg; memset(send_buff, 0, sizeof(send_buff)); setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_send with %s and valid data\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha256", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h1, &knet_handle_crypto_cfg, 1)); FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h1, 1)); FAIL_ON_ERR(knet_handle_crypto_rx_clear_traffic(knet_h1, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC)); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); FAIL_ON_ERR(wait_for_host(knet_h1, 1, 10, logfds[0], stdout)); send_len = knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel); if (send_len <= 0) { printf("knet_send failed: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } if (send_len != sizeof(send_buff)) { printf("knet_send sent only %zd bytes: %s\n", send_len, strerror(errno)); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 0)); FAIL_ON_ERR(wait_for_packet(knet_h1, 10, datafd, logfds[0], stdout)); recv_len = knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel); savederrno = errno; if (recv_len != send_len) { printf("knet_recv received only %d bytes: %s (errno: %d)\n", recv_len, strerror(errno), errno); if ((is_helgrind()) && (recv_len == -1) && (savederrno == EAGAIN)) { printf("helgrind exception. this is normal due to possible timeouts\n"); CLEAN_EXIT(PASS); } CLEAN_EXIT(FAIL); } if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { printf("recv and send buffers are different!\n"); CLEAN_EXIT(FAIL); } /* A sanity check on the stats */ if (knet_handle_get_stats(knet_h1, &stats, sizeof(stats)) < 0) { printf("knet_handle_get_stats failed: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } if (stats.tx_crypt_packets >= 1 || stats.rx_crypt_packets < 1) { printf("stats look wrong: tx_packets: %" PRIu64 ", rx_packets: %" PRIu64 "\n", stats.tx_crypt_packets, stats.rx_crypt_packets); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { struct knet_crypto_info crypto_list[16]; size_t crypto_list_entries; size_t i; #ifdef KNET_BSD if (is_memcheck() || is_helgrind()) { printf("valgrind-freebsd cannot run this test properly. Skipping\n"); return SKIP; } #endif memset(crypto_list, 0, sizeof(crypto_list)); if (knet_get_crypto_list(crypto_list, &crypto_list_entries) < 0) { printf("knet_get_crypto_list failed: %s\n", strerror(errno)); return FAIL; } if (crypto_list_entries == 0) { printf("no crypto modules detected. Skipping\n"); return SKIP; } for (i=0; i < crypto_list_entries; i++) { test(crypto_list[i].name); } return PASS; } diff --git a/libknet/tests/api_knet_send_loopback.c b/libknet/tests/api_knet_send_loopback.c index cf2d33c6..a1b90520 100644 --- a/libknet/tests/api_knet_send_loopback.c +++ b/libknet/tests/api_knet_send_loopback.c @@ -1,200 +1,200 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static int dhost_filter(void *pvt_data, const unsigned char *outdata, ssize_t outdata_len, uint8_t tx_rx, knet_node_id_t this_host_id, knet_node_id_t src_host_id, int8_t *dst_channel, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries) { dst_host_ids[0] = 1; *dst_host_ids_entries = 1; return 0; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int datafd = 0; int8_t channel = 0; struct knet_link_status link_status; char send_buff[KNET_MAX_PACKET_SIZE]; char recv_buff[KNET_MAX_PACKET_SIZE]; ssize_t send_len = 0; int recv_len = 0; int savederrno; int res; struct sockaddr_storage lo; memset(send_buff, 0, sizeof(send_buff)); setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test configuring multiple links with loopback\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_LOOPBACK, 0, AF_INET, 0, &lo)); if (_knet_link_set_config(knet_h1, 1, 1, KNET_TRANSPORT_LOOPBACK, 0, AF_INET, 0, &lo) == 0) { printf("Managed to configure two LOOPBACK links - this is wrong\n"); CLEAN_EXIT(FAIL); } printf("Test configuring UDP link after loopback\n"); if (_knet_link_set_config(knet_h1, 1, 1, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo) == 0) { printf("Managed to configure UDP and LOOPBACK links together: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test configuring UDP link before loopback\n"); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); if (_knet_link_set_config(knet_h1, 1, 1, KNET_TRANSPORT_LOOPBACK, 0, AF_INET, 0, &lo) == 0) { printf("Managed to configure LOOPBACK link after UDP: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_send with valid data\n"); FAIL_ON_ERR(knet_handle_enable_access_lists(knet_h1, 1)); FAIL_ON_ERR(knet_link_clear_config(knet_h1, 1, 0)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_LOOPBACK, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); FAIL_ON_ERR(wait_for_host(knet_h1, 1, 10, logfds[0], stdout)); send_len = knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel); if (send_len <= 0) { printf("knet_send failed: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } if (send_len != sizeof(send_buff)) { printf("knet_send sent only %zd bytes: %s\n", send_len, strerror(errno)); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 0)); FAIL_ON_ERR(wait_for_packet(knet_h1, 10, datafd, logfds[0], stdout)) recv_len = knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel); savederrno = errno; if (recv_len != send_len) { printf("knet_recv received only %d bytes: %s (errno: %d)\n", recv_len, strerror(errno), errno); if ((is_helgrind()) && (recv_len == -1) && (savederrno == EAGAIN)) { printf("helgrind exception. this is normal due to possible timeouts\n"); CLEAN_EXIT(PASS); } CLEAN_EXIT(FAIL); } if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { printf("recv and send buffers are different!\n"); CLEAN_EXIT(FAIL); } /* A sanity check on the stats */ FAIL_ON_ERR(knet_link_get_status(knet_h1, 1, 0, &link_status, sizeof(link_status))); if (link_status.stats.tx_data_packets != 1 || link_status.stats.rx_data_packets != 0 || link_status.stats.tx_data_bytes != KNET_MAX_PACKET_SIZE) { printf("stats look wrong: tx_packets: %" PRIu64 " (%" PRIu64 " bytes), rx_packets: %" PRIu64 " (%" PRIu64 " bytes)\n", link_status.stats.tx_data_packets, link_status.stats.tx_data_bytes, link_status.stats.rx_data_packets, link_status.stats.rx_data_bytes); } printf("Test knet_send with only localhost\n"); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); FAIL_ON_ERR(knet_handle_enable_filter(knet_h1, NULL, dhost_filter)); send_len = knet_send(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel); if (send_len <= 0) { printf("knet_send failed: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } if (send_len != sizeof(send_buff)) { printf("knet_send sent only %zd bytes: %s\n", send_len, strerror(errno)); CLEAN_EXIT(FAIL); } FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 0)); FAIL_ON_ERR(wait_for_packet(knet_h1, 10, datafd, logfds[0], stdout)); recv_len = knet_recv(knet_h1, recv_buff, KNET_MAX_PACKET_SIZE, channel); savederrno = errno; if (recv_len != send_len) { printf("knet_recv received only %d bytes: %s (errno: %d)\n", recv_len, strerror(errno), errno); if ((is_helgrind()) && (recv_len == -1) && (savederrno == EAGAIN)) { printf("helgrind exception. this is normal due to possible timeouts\n"); CLEAN_EXIT(PASS); } CLEAN_EXIT(FAIL); } if (memcmp(recv_buff, send_buff, KNET_MAX_PACKET_SIZE)) { printf("recv and send buffers are different!\n"); CLEAN_EXIT(FAIL); } CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_send_sync.c b/libknet/tests/api_knet_send_sync.c index 5f916499..ac059f17 100644 --- a/libknet/tests/api_knet_send_sync.c +++ b/libknet/tests/api_knet_send_sync.c @@ -1,213 +1,213 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static int dhost_filter_ret = 0; static int dhost_filter(void *pvt_data, const unsigned char *outdata, ssize_t outdata_len, uint8_t tx_rx, knet_node_id_t this_host_id, knet_node_id_t src_host_id, int8_t *dst_channel, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries) { dst_host_ids[0] = 0; /* * fatal fault */ if (dhost_filter_ret < 0) { return -1; } /* * trigger EINVAL * no ids found */ if (dhost_filter_ret == 0) { *dst_host_ids_entries = 0; return 0; } /* * send correct info back */ if (dhost_filter_ret == 1) { dst_host_ids[0] = 1; *dst_host_ids_entries = 1; return 0; } /* * trigger E2BIG * mcast destinations */ if (dhost_filter_ret == 2) { dst_host_ids[0] = 1; *dst_host_ids_entries = 2; return 0; } /* * return mcast */ if (dhost_filter_ret == 3) { return 1; } return dhost_filter_ret; } static void test(void) { knet_handle_t knet_h1, knet_h[2]; int logfds[2]; int datafd = 0; int8_t channel = 0; char send_buff[KNET_MAX_PACKET_SIZE]; struct sockaddr_storage lo; int res; memset(send_buff, 0, sizeof(send_buff)); printf("Test knet_send_sync incorrect knet_h\n"); if ((!knet_send_sync(NULL, send_buff, KNET_MAX_PACKET_SIZE, channel)) || (errno != EINVAL)) { printf("knet_send_sync accepted invalid knet_h or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } setup_logpipes(logfds); knet_h1 = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); printf("Test knet_send_sync with no send_buff\n"); FAIL_ON_SUCCESS(knet_send_sync(knet_h1, NULL, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_send_sync with invalid send_buff len (0)\n"); FAIL_ON_SUCCESS(knet_send_sync(knet_h1, send_buff, 0, channel), EINVAL); printf("Test knet_send_sync with invalid send_buff len (> KNET_MAX_PACKET_SIZE)\n"); FAIL_ON_SUCCESS(knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE + 1, channel), EINVAL); printf("Test knet_send_sync with invalid channel (-1)\n"); channel = -1; FAIL_ON_SUCCESS(knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_send_sync with invalid channel (KNET_DATAFD_MAX)\n"); channel = KNET_DATAFD_MAX; FAIL_ON_SUCCESS(knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_send_sync with no filter configured\n"); channel = 1; FAIL_ON_SUCCESS(knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel), ENETDOWN); FAIL_ON_ERR(knet_handle_enable_filter(knet_h1, NULL, dhost_filter)); printf("Test knet_send_sync with unconfigured channel\n"); channel = 0; FAIL_ON_SUCCESS(knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel), EINVAL); printf("Test knet_send_sync with data forwarding disabled\n"); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h1, &private_data, sock_notify)); datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h1, &datafd, &channel)); if ((knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != ECANCELED)) { printf("knet_send_sync didn't detect datafwd disabled or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_send_sync with broken dst_host_filter\n"); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 1)); dhost_filter_ret = -1; if ((knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != EFAULT)) { printf("knet_send_sync didn't detect fatal error from dst_host_filter or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_send_sync with dst_host_filter returning no host_ids_entries\n"); dhost_filter_ret = 0; if ((knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != EINVAL)) { printf("knet_send_sync didn't detect 0 host_ids from dst_host_filter or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_send_sync with host down\n"); dhost_filter_ret = 1; if ((knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != EHOSTDOWN)) { printf("knet_send_sync didn't detect hostdown or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_send_sync with dst_host_filter returning too many host_ids_entries\n"); FAIL_ON_ERR(knet_host_add(knet_h1, 1)); FAIL_ON_ERR(_knet_link_set_config(knet_h1, 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_enable(knet_h1, 1, 0, 1)); FAIL_ON_ERR(wait_for_host(knet_h1, 1, 10, logfds[0], stdout)); dhost_filter_ret = 2; if ((knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != E2BIG)) { printf("knet_send_sync didn't detect 2+ host_ids from dst_host_filter or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_send_sync with dst_host_filter returning mcast packets\n"); dhost_filter_ret = 3; if ((knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel) == sizeof(send_buff)) || (errno != E2BIG)) { printf("knet_send_sync didn't detect mcast packet from dst_host_filter or returned incorrect error: %s\n", strerror(errno)); CLEAN_EXIT(FAIL); } printf("Test knet_send_sync with valid data\n"); dhost_filter_ret = 1; FAIL_ON_ERR(knet_send_sync(knet_h1, send_buff, KNET_MAX_PACKET_SIZE, channel)); FAIL_ON_ERR(knet_handle_setfwd(knet_h1, 0)); CLEAN_EXIT(CONTINUE); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/api_knet_strtoaddr.c b/libknet/tests/api_knet_strtoaddr.c index b058266e..3ccfb93c 100644 --- a/libknet/tests/api_knet_strtoaddr.c +++ b/libknet/tests/api_knet_strtoaddr.c @@ -1,115 +1,115 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "libknet.h" #include "test-common.h" static void test(void) { struct sockaddr_storage out_addr; struct sockaddr_in *out_addrv4 = (struct sockaddr_in *)&out_addr; struct sockaddr_in6 *out_addrv6 = (struct sockaddr_in6 *)&out_addr; struct sockaddr_in addrv4; struct sockaddr_in6 addrv6; memset(&out_addr, 0, sizeof(struct sockaddr_storage)); memset(&addrv4, 0, sizeof(struct sockaddr_in)); memset(&addrv6, 0, sizeof(struct sockaddr_in6)); printf("Checking knet_strtoaddr with invalid host\n"); if (!knet_strtoaddr(NULL, "50000", &out_addr, sizeof(struct sockaddr_storage)) || (errno != EINVAL)) { printf("knet_strtoaddr accepted invalid host or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Checking knet_strtoaddr with invalid port\n"); if (!knet_strtoaddr("127.0.0.1", NULL, &out_addr, sizeof(struct sockaddr_storage)) || (errno != EINVAL)) { printf("knet_strtoaddr accepted invalid port or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Checking knet_strtoaddr with invalid addr\n"); if (!knet_strtoaddr("127.0.0.1", "50000", NULL, sizeof(struct sockaddr_storage)) || (errno != EINVAL)) { printf("knet_strtoaddr accepted invalid addr or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } printf("Checking knet_strtoaddr with invalid size\n"); if (!knet_strtoaddr("127.0.0.1", "50000", &out_addr, 0) || (errno != EINVAL)) { printf("knet_strtoaddr accepted invalid size or returned incorrect error: %s\n", strerror(errno)); exit(FAIL); } addrv4.sin_family = AF_INET; addrv4.sin_addr.s_addr = htonl(0xc0a80001); /* 192.168.0.1 */ addrv4.sin_port = htons(50000); printf("Checking knet_strtoaddr with valid data (192.168.0.1:50000)\n"); if (knet_strtoaddr("192.168.0.1", "50000", &out_addr, sizeof(struct sockaddr_storage))) { printf("Unable to convert 192.168.0.1:50000\n"); exit(FAIL); } if (out_addrv4->sin_family != addrv4.sin_family || out_addrv4->sin_port != addrv4.sin_port || out_addrv4->sin_addr.s_addr != addrv4.sin_addr.s_addr) { printf("Check on 192.168.0.1:50000 failed\n"); exit(FAIL); } printf("Checking knet_strtoaddr with valid data ([fd00::1]:50000)\n"); memset(&out_addr, 0, sizeof(struct sockaddr_storage)); addrv6.sin6_family = AF_INET6; addrv6.sin6_addr.s6_addr16[0] = htons(0xfd00); /* fd00::1 */ addrv6.sin6_addr.s6_addr16[7] = htons(0x0001); addrv6.sin6_port = htons(50000); if (knet_strtoaddr("fd00::1", "50000", &out_addr, sizeof(struct sockaddr_storage))) { printf("Unable to convert fd00::1:50000\n"); exit(FAIL); } if (out_addrv6->sin6_family != addrv6.sin6_family || out_addrv6->sin6_port != addrv6.sin6_port || memcmp(&out_addrv6->sin6_addr, &addrv6.sin6_addr, sizeof(struct in6_addr))) { printf("Check on fd00::1:50000 failed\n"); exit(FAIL); } } int main(int argc, char *argv[]) { test(); exit(PASS); } diff --git a/libknet/tests/fun_acl_check.c b/libknet/tests/fun_acl_check.c index 8de50f77..fd6de69c 100644 --- a/libknet/tests/fun_acl_check.c +++ b/libknet/tests/fun_acl_check.c @@ -1,404 +1,404 @@ /* - * Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. * * Authors: Christine Caulfield * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "libknet.h" #include "internals.h" #include "netutils.h" #include "test-common.h" /* * Keep track of how many messages got through: * clean + 3xACLs + QUIT */ #define CORRECT_NUM_MSGS 5 static int msgs_recvd = 0; #undef TESTNODES #define TESTNODES 2 static pthread_mutex_t recv_mutex = PTHREAD_MUTEX_INITIALIZER; static int quit_recv_thread = 0; static int reply_pipe[2]; /* Our local version of FOE that also tidies up the threads */ #define FAIL_ON_ERR_THR(fn) \ printf("FOE: %s\n", #fn); \ if ((res = fn) != 0) { \ int savederrno = errno; \ pthread_mutex_lock(&recv_mutex); \ quit_recv_thread = 1; \ pthread_mutex_unlock(&recv_mutex); \ if (recv_thread) { \ pthread_join(recv_thread, (void**)&thread_err); \ } \ knet_handle_stop_everything(knet_h, TESTNODES); \ stop_logthread(); \ flush_logs(logfds[0], stdout); \ close_logpipes(logfds); \ close(reply_pipe[0]); \ close(reply_pipe[1]); \ if (res == -2) { \ exit(SKIP); \ } else { \ printf("*** FAIL on line %d %s failed: %s\n", __LINE__ , #fn, strerror(savederrno)); \ exit(FAIL); \ } \ } static int knet_send_str(knet_handle_t knet_h, char *str) { return knet_send_sync(knet_h, str, strlen(str)+1, 0); } /* * lo0 is filled in with the local address on return. * lo1 is expected to be provided - it's the actual remote address to connect to. */ int dyn_knet_link_set_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t transport, uint64_t flags, int family, int dynamic, struct sockaddr_storage *lo0, struct sockaddr_storage *lo1) { int err = 0, savederrno = 0; uint32_t port; char portstr[32]; for (port = 1025; port < 65536; port++) { sprintf(portstr, "%u", port); memset(lo0, 0, sizeof(struct sockaddr_storage)); if (family == AF_INET6) { err = knet_strtoaddr("::1", portstr, lo0, sizeof(struct sockaddr_storage)); } else { err = knet_strtoaddr("127.0.0.1", portstr, lo0, sizeof(struct sockaddr_storage)); } if (err < 0) { printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); goto out; } errno = 0; if (dynamic) { err = knet_link_set_config(knet_h, host_id, link_id, transport, lo0, NULL, flags); } else { err = knet_link_set_config(knet_h, host_id, link_id, transport, lo0, lo1, flags); } savederrno = errno; if ((err < 0) && (savederrno != EADDRINUSE)) { if (savederrno == EPROTONOSUPPORT && transport == KNET_TRANSPORT_SCTP) { return -2; } else { printf("Unable to configure link: %s\n", strerror(savederrno)); goto out; } } if (!err) { printf("Using port %u\n", port); goto out; } } if (err) { printf("No more ports available\n"); } out: errno = savederrno; return err; } static void *recv_messages(void *handle) { knet_handle_t knet_h = (knet_handle_t)handle; char buf[4096]; ssize_t len; static int err = 0; int savederrno = 0, quit = 0; while ((len = knet_recv(knet_h, buf, sizeof(buf), 0)) && (!quit)) { savederrno = errno; pthread_mutex_lock(&recv_mutex); quit = quit_recv_thread; pthread_mutex_unlock(&recv_mutex); if (quit) { printf(" *** recv thread was requested to exit via FOE\n"); err = 1; return &err; } if (len > 0) { int res; printf("recv: (%ld) %s\n", (long)len, buf); msgs_recvd++; if (strcmp("QUIT", buf) == 0) { break; } if (buf[0] == '0') { /* We should not have received this! */ printf(" *** FAIL received packet that should have been blocked\n"); err = 1; return &err; } /* Tell the main thread we have received something */ res = write(reply_pipe[1], ".", 1); if (res != 1) { printf(" *** FAIL to send response back to main thread\n"); err = 1; return &err; } } usleep(1000); if (len < 0 && savederrno != EAGAIN) { break; } } printf("-- recv thread finished: %zd %d %s\n", len, errno, strerror(savederrno)); return &err; } static void notify_fn(void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { printf("NOTIFY fn called\n"); } /* A VERY basic filter because all data traffic is going to one place */ static int dhost_filter(void *pvt_data, const unsigned char *outdata, ssize_t outdata_len, uint8_t tx_rx, knet_node_id_t this_host_id, knet_node_id_t src_host_id, int8_t *dst_channel, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries) { dst_host_ids[0] = 1; *dst_host_ids_entries = 1; return 0; } /* This used to be a pthread condition variable, but there was a race where it could be triggered before the main thread was waiting for it. Go old-fashioned. */ static int wait_for_reply(int seconds) { int res; struct pollfd pfds; char tmpbuf[32]; pfds.fd = reply_pipe[0]; pfds.events = POLLIN | POLLERR | POLLHUP; pfds.revents = 0; res = poll(&pfds, 1, seconds*1000); if (res == 1) { if (pfds.revents & POLLIN) { res = read(reply_pipe[0], tmpbuf, sizeof(tmpbuf)); if (res > 0) { return 0; } } else { printf("Error on pipe poll revent = 0x%x\n", pfds.revents); errno = EIO; } } if (res == 0) { errno = ETIMEDOUT; return -1; } return -1; } static void test(int transport) { knet_handle_t knet_h[TESTNODES+1]; int logfds[2]; struct sockaddr_storage lo0, lo1; struct sockaddr_storage ss1, ss2; int res; pthread_t recv_thread = 0; int *thread_err; int datafd; int8_t channel; int seconds = 90; // dynamic tests take longer than normal tests if (is_memcheck() || is_helgrind()) { printf("Test suite is running under valgrind, adjusting wait_for_host timeout\n"); seconds = seconds * 16; } memset(knet_h, 0, sizeof(knet_h)); memset(reply_pipe, 0, sizeof(reply_pipe)); memset(logfds, 0, sizeof(logfds)); FAIL_ON_ERR_THR(pipe(reply_pipe)); // Initial setup gubbins msgs_recvd = 0; setup_logpipes(logfds); start_logthread(logfds[1], stdout); knet_handle_start_nodes(knet_h, TESTNODES, logfds, KNET_LOG_DEBUG); FAIL_ON_ERR_THR(knet_host_add(knet_h[2], 1)); FAIL_ON_ERR_THR(knet_host_add(knet_h[1], 2)); FAIL_ON_ERR_THR(knet_handle_enable_filter(knet_h[2], NULL, dhost_filter)); // Create the dynamic (receiving) link FAIL_ON_ERR_THR(dyn_knet_link_set_config(knet_h[1], 2, 0, transport, 0, AF_INET, 1, &lo0, NULL)); // Connect to the dynamic link FAIL_ON_ERR_THR(dyn_knet_link_set_config(knet_h[2], 1, 0, transport, 0, AF_INET, 0, &lo1, &lo0)); // All the rest of the setup gubbins FAIL_ON_ERR_THR(knet_handle_enable_sock_notify(knet_h[1], 0, ¬ify_fn)); FAIL_ON_ERR_THR(knet_handle_enable_sock_notify(knet_h[2], 0, ¬ify_fn)); channel = datafd = 0; FAIL_ON_ERR_THR(knet_handle_add_datafd(knet_h[1], &datafd, &channel)); channel = datafd = 0; FAIL_ON_ERR_THR(knet_handle_add_datafd(knet_h[2], &datafd, &channel)); FAIL_ON_ERR_THR(knet_link_set_enable(knet_h[1], 2, 0, 1)); FAIL_ON_ERR_THR(knet_link_set_enable(knet_h[2], 1, 0, 1)); FAIL_ON_ERR_THR(knet_handle_setfwd(knet_h[1], 1)); FAIL_ON_ERR_THR(knet_handle_setfwd(knet_h[2], 1)); // Start receive thread FAIL_ON_ERR_THR(pthread_create(&recv_thread, NULL, recv_messages, (void *)knet_h[1])); // Let everything settle down FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[1], TESTNODES, 1, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[2], TESTNODES, 1, seconds, logfds[0], stdout)); /* * TESTING STARTS HERE * strings starting '1' should reach the receiving thread * strings starting '0' should not */ // No ACL printf("Testing No ACL - this should get through\n"); FAIL_ON_ERR_THR(knet_send_str(knet_h[2], "1No ACL - this should get through")); FAIL_ON_ERR_THR(wait_for_reply(seconds)) // Block traffic from this address. memset(&ss1, 0, sizeof(ss1)); memset(&ss2, 0, sizeof(ss1)); knet_strtoaddr("127.0.0.1","0", &ss1, sizeof(ss1)); FAIL_ON_ERR_THR(knet_link_add_acl(knet_h[1], 2, 0, &ss1, NULL, CHECK_TYPE_ADDRESS, CHECK_REJECT)); // Accept ACL for when we remove them FAIL_ON_ERR_THR(knet_link_add_acl(knet_h[1], 2, 0, &ss1, NULL, CHECK_TYPE_ADDRESS, CHECK_ACCEPT)); // This needs to go after the first ACLs are added FAIL_ON_ERR_THR(knet_handle_enable_access_lists(knet_h[1], 1)); printf("Testing Address blocked - this should NOT get through\n"); FAIL_ON_ERR_THR(knet_send_str(knet_h[2], "0Address blocked - this should NOT get through")); // Unblock and check again FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[1], TESTNODES, 0, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[2], TESTNODES, 0, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(knet_link_rm_acl(knet_h[1], 2, 0, &ss1, NULL, CHECK_TYPE_ADDRESS, CHECK_REJECT)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[1], TESTNODES, 1, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[2], TESTNODES, 1, seconds, logfds[0], stdout)); printf("Testing Address unblocked - this should get through\n"); FAIL_ON_ERR_THR(knet_send_str(knet_h[2], "1Address unblocked - this should get through")); FAIL_ON_ERR_THR(wait_for_reply(seconds)); // Block traffic using a netmask knet_strtoaddr("127.0.0.1","0", &ss1, sizeof(ss1)); knet_strtoaddr("255.0.0.1","0", &ss2, sizeof(ss2)); FAIL_ON_ERR_THR(knet_link_insert_acl(knet_h[1], 2, 0, 0, &ss1, &ss2, CHECK_TYPE_MASK, CHECK_REJECT)); printf("Testing Netmask blocked - this should NOT get through\n"); FAIL_ON_ERR_THR(knet_send_str(knet_h[2], "0Netmask blocked - this should NOT get through")); // Unblock and check again FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[1], TESTNODES, 0, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[2], TESTNODES, 0, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(knet_link_rm_acl(knet_h[1], 2, 0, &ss1, &ss2, CHECK_TYPE_MASK, CHECK_REJECT)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[1], TESTNODES, 1, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[2], TESTNODES, 1, seconds, logfds[0], stdout)); printf("Testing Netmask unblocked - this should get through\n"); FAIL_ON_ERR_THR(knet_send_str(knet_h[2], "1Netmask unblocked - this should get through")); FAIL_ON_ERR_THR(wait_for_reply(seconds)); // Block traffic from a range knet_strtoaddr("127.0.0.0", "0", &ss1, sizeof(ss1)); knet_strtoaddr("127.0.0.9", "0", &ss2, sizeof(ss2)); FAIL_ON_ERR_THR(knet_link_insert_acl(knet_h[1], 2, 0, 0, &ss1, &ss2, CHECK_TYPE_RANGE, CHECK_REJECT)); printf("Testing Range blocked - this should NOT get through\n"); FAIL_ON_ERR_THR(knet_send_str(knet_h[2], "0Range blocked - this should NOT get through")); // Unblock and check again FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[1], TESTNODES, 0, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[2], TESTNODES, 0, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(knet_link_rm_acl(knet_h[1], 2, 0, &ss1, &ss2, CHECK_TYPE_RANGE, CHECK_REJECT)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[1], TESTNODES, 1, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[2], TESTNODES, 1, seconds, logfds[0], stdout)); printf("Testing Range unblocked - this should get through\n"); FAIL_ON_ERR_THR(knet_send_str(knet_h[2], "1Range unblocked - this should get through")); FAIL_ON_ERR_THR(wait_for_reply(seconds)); // Finish up - disable ACLS to make sure the QUIT message gets through FAIL_ON_ERR_THR(knet_handle_enable_access_lists(knet_h[1], 0)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[1], TESTNODES, 1, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(wait_for_nodes_state(knet_h[2], TESTNODES, 1, seconds, logfds[0], stdout)); FAIL_ON_ERR_THR(knet_send_str(knet_h[2], "QUIT")); // Check return from the receiving thread pthread_join(recv_thread, (void**)&thread_err); if (*thread_err) { printf("Thread returned %d\n", *thread_err); clean_exit(knet_h, TESTNODES, logfds, FAIL); } if (msgs_recvd != CORRECT_NUM_MSGS) { printf("*** FAIL Recv thread got %d messages, expected %d\n", msgs_recvd, CORRECT_NUM_MSGS); clean_exit(knet_h, TESTNODES, logfds, FAIL); } clean_exit(knet_h, TESTNODES, logfds, PASS); } int main(int argc, char *argv[]) { printf("Testing with UDP\n"); test(KNET_TRANSPORT_UDP); #ifdef HAVE_NETINET_SCTP_H printf("Testing with SCTP currently disabled\n"); //test(KNET_TRANSPORT_SCTP); #endif return PASS; } diff --git a/libknet/tests/fun_config_crypto.c b/libknet/tests/fun_config_crypto.c index cfacb78d..3bd53260 100644 --- a/libknet/tests/fun_config_crypto.c +++ b/libknet/tests/fun_config_crypto.c @@ -1,182 +1,182 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "compress.h" #include "internals.h" #include "netutils.h" #include "test-common.h" #undef TESTNODES #define TESTNODES 2 static void test(const char *model) { knet_handle_t knet_h[TESTNODES + 1]; int logfds[2]; struct knet_handle_crypto_cfg knet_handle_crypto_cfg; int i,x; int seconds = 10; int res; if (is_memcheck() || is_helgrind()) { printf("Test suite is running under valgrind, adjusting wait_for_host timeout\n"); seconds = seconds * 16; } setup_logpipes(logfds); knet_handle_start_nodes(knet_h, TESTNODES, logfds, KNET_LOG_DEBUG); flush_logs(logfds[0], stdout); /* * config1: aes128/sha256 key1 is all 0s (2000 bytes) */ memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes128", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha256", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); memset(knet_handle_crypto_cfg.private_key, 0, KNET_MAX_KEY_LEN); knet_handle_crypto_cfg.private_key_len = 2000; for (i = 1; i <= TESTNODES; i++) { FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h[i], &knet_handle_crypto_cfg, 1)); FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h[i], 1)); FAIL_ON_ERR(knet_handle_crypto_rx_clear_traffic(knet_h[i], KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC)); } flush_logs(logfds[0], stdout); knet_handle_join_nodes(knet_h, TESTNODES, 1, AF_INET, KNET_TRANSPORT_UDP); flush_logs(logfds[0], stdout); /* * config2: aes256/sha512 key1 is all 1s (KNET_MAX_KEY_LEN bytes) */ memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "aes256", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "sha512", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); memset(knet_handle_crypto_cfg.private_key, 1, KNET_MAX_KEY_LEN); knet_handle_crypto_cfg.private_key_len = KNET_MAX_KEY_LEN; for (i = 1; i <= TESTNODES; i++) { if (knet_handle_crypto_set_config(knet_h[i], &knet_handle_crypto_cfg, 2) < 0) { printf("knet_handle_crypto_set_config (2) failed with correct config: %s\n", strerror(errno)); clean_exit(knet_h, TESTNODES, logfds, FAIL); } } flush_logs(logfds[0], stdout); printf("Testing crypto config switch from 1 to 2\n"); for (i = 1; i <= TESTNODES; i++) { if (knet_handle_crypto_use_config(knet_h[i], 2) < 0) { printf("knet_handle_crypto_use_config (2) failed with correct config: %s\n", strerror(errno)); clean_exit(knet_h, TESTNODES, logfds, FAIL); } for (x = 1; x <= TESTNODES; x++) { wait_for_nodes_state(knet_h[x], TESTNODES, 1, 600, knet_h[1]->logfd, stdout); } } flush_logs(logfds[0], stdout); printf("Testing crypto config switch from 2 to 1\n"); for (i = 1; i <= TESTNODES; i++) { FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h[i], 1)); wait_for_nodes_state(knet_h[i], TESTNODES, 1, 600, knet_h[1]->logfd, stdout); } printf("Testing disable crypto config and allow clear traffic\n"); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); memset(knet_handle_crypto_cfg.private_key, 0, KNET_MAX_KEY_LEN); knet_handle_crypto_cfg.private_key_len = KNET_MAX_KEY_LEN; for (i = 1; i <= TESTNODES; i++) { /* * config2 is no longer in use */ FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h[i], &knet_handle_crypto_cfg, 2)); /* * allow clear traffic on RX on all nodes, before we change config to clear traffic */ FAIL_ON_ERR(knet_handle_crypto_rx_clear_traffic(knet_h[i], KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC)); } for (i = 1; i <= TESTNODES; i++) { /* * switch to clear traffic on RX on all nodes */ FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h[i], 0)); } for (i = 1; i <= TESTNODES; i++) { /* * config1 is no longer in use */ FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h[i], &knet_handle_crypto_cfg, 1)); } for (i = 1; i <= TESTNODES; i++) { for (x = 0; x < seconds; x++){ flush_logs(logfds[0], stdout); sleep(1); } for (x = 1; x <= TESTNODES; x++) { wait_for_nodes_state(knet_h[x], TESTNODES, 1, 600, knet_h[1]->logfd, stdout); } } flush_logs(logfds[0], stdout); close_logpipes(logfds); knet_handle_stop_everything(knet_h, TESTNODES); } int main(int argc, char *argv[]) { struct knet_crypto_info crypto_list[16]; size_t crypto_list_entries; size_t i; memset(crypto_list, 0, sizeof(crypto_list)); if (knet_get_crypto_list(crypto_list, &crypto_list_entries) < 0) { printf("knet_get_crypto_list failed: %s\n", strerror(errno)); return FAIL; } if (crypto_list_entries == 0) { printf("no crypto modules detected. Skipping\n"); return SKIP; } for (i=0; i < crypto_list_entries; i++) { test(crypto_list[i].name); } return PASS; } diff --git a/libknet/tests/fun_onwire_upgrade.c b/libknet/tests/fun_onwire_upgrade.c index 6b9369a6..2bd8d1ae 100644 --- a/libknet/tests/fun_onwire_upgrade.c +++ b/libknet/tests/fun_onwire_upgrade.c @@ -1,297 +1,297 @@ /* - * Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "libknet.h" #include "compress.h" #include "internals.h" #include "netutils.h" #include "test-common.h" #undef TESTNODES #define TESTNODES 3 static int upgrade_onwire_max_ver(knet_handle_t knet_h, int nodes, uint8_t min, uint8_t max, int seconds, int logfd, FILE *std) { if (knet_handle_disconnect_links(knet_h) < 0) { return -1; } if (wait_for_nodes_state(knet_h, TESTNODES, 0, seconds, logfd, std) < 0) { printf("Failed waiting for nodes 0\n"); return -1; } knet_h->onwire_min_ver = min; knet_h->onwire_max_ver = max; if (knet_handle_reconnect_links(knet_h) < 0) { return -1; } if (nodes) { if (wait_for_nodes_state(knet_h, nodes, 1, seconds, logfd, std) < 0) { printf("Failed waiting for nodes 1\n"); return -1; } } return 0; } static void onwire_ver_callback_fn(void *private_data, uint8_t onwire_min_ver, uint8_t onwire_max_ver, uint8_t onwire_ver) { knet_handle_t knet_h = (knet_handle_t)private_data; printf("Received callback from %p: min: %u max: %u current: %u\n", knet_h, onwire_min_ver, onwire_max_ver, onwire_ver); } static void test(void) { knet_handle_t knet_h[TESTNODES + 1]; int logfds[2]; int i,j; int res; int seconds = 10; if (is_memcheck() || is_helgrind()) { printf("Test suite is running under valgrind, adjusting wait_for_host timeout\n"); seconds = seconds * 16; } setup_logpipes(logfds); knet_handle_start_nodes(knet_h, TESTNODES, logfds, KNET_LOG_DEBUG); flush_logs(logfds[0], stdout); for (i = 1; i <= TESTNODES; i++) { knet_h[i]->onwire_ver_remap = 1; FAIL_ON_ERR(knet_handle_enable_onwire_ver_notify(knet_h[i], (void *)&knet_h[i], onwire_ver_callback_fn)); } flush_logs(logfds[0], stdout); knet_handle_join_nodes(knet_h, TESTNODES, 1, AF_INET, KNET_TRANSPORT_UDP); flush_logs(logfds[0], stdout); printf("Test normal onwire upgrade from %u to %u\n", knet_h[1]->onwire_ver, knet_h[1]->onwire_ver + 1); for (i = 1; i <= TESTNODES; i++) { FAIL_ON_ERR(upgrade_onwire_max_ver(knet_h[i], TESTNODES, knet_h[1]->onwire_ver, knet_h[1]->onwire_ver + 1, seconds, logfds[0], stdout)); flush_logs(logfds[0], stdout); } flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); for (i = 1; i <= TESTNODES; i++) { printf("node %u, onwire: %u min: %u max: %u\n", i, knet_h[i]->onwire_ver, knet_h[i]->onwire_min_ver, knet_h[i]->onwire_max_ver); } flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); printf("Test onwire upgrade from %u to %u (all but one node)\n", knet_h[1]->onwire_ver, knet_h[1]->onwire_ver + 1); for (i = 1; i < TESTNODES; i++) { FAIL_ON_ERR(upgrade_onwire_max_ver(knet_h[i], TESTNODES, knet_h[i]->onwire_ver, knet_h[i]->onwire_ver + 1, seconds, logfds[0], stdout)); flush_logs(logfds[0], stdout); } flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); for (i = 1; i <= TESTNODES; i++) { printf("node %u, onwire: %u min: %u max: %u\n", i, knet_h[i]->onwire_ver, knet_h[i]->onwire_min_ver, knet_h[i]->onwire_max_ver); } flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); printf("Test onwire upgrade from %u to %u (all but one node - phase 2, node should be kicked out and remaining nodes should upgrade)\n", knet_h[1]->onwire_max_ver, knet_h[1]->onwire_max_ver + 1); for (i = 1; i < TESTNODES; i++) { FAIL_ON_ERR(upgrade_onwire_max_ver(knet_h[i], TESTNODES - 1, knet_h[i]->onwire_max_ver, knet_h[i]->onwire_max_ver + 1, seconds, logfds[0], stdout)); flush_logs(logfds[0], stdout); } flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); for (i = 1; i <= TESTNODES; i++) { printf("node %u, onwire: %u min: %u max: %u\n", i, knet_h[i]->onwire_ver, knet_h[i]->onwire_min_ver, knet_h[i]->onwire_max_ver); for (j = 1; j <= TESTNODES; j++) { if (j == i) { continue; } if (i == TESTNODES) { /* * highset node has been kicked out and should not * be able to reach any other node */ if (knet_h[i]->host_index[j]->status.reachable != 0) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } else { /* * all other nodes should detect the highest node unreachable * and all the remaining nodes reachable */ if (j == TESTNODES) { if (knet_h[i]->host_index[j]->status.reachable != 0) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } else { if (knet_h[i]->host_index[j]->status.reachable != 1) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } } } } flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); /* * CHANGE THIS TEST if we decide to support downgrades */ printf("Testing node rejoining one version lower (cluster should reject the node)\n"); FAIL_ON_ERR(upgrade_onwire_max_ver(knet_h[TESTNODES], 0, knet_h[1]->onwire_min_ver - 1, knet_h[1]->onwire_max_ver - 1, seconds, logfds[0], stdout)); /* * need more time here for membership to settle */ flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); for (i = 1; i <= TESTNODES; i++) { printf("node %u, onwire: %u min: %u max: %u\n", i, knet_h[i]->onwire_ver, knet_h[i]->onwire_min_ver, knet_h[i]->onwire_max_ver); for (j = 1; j <= TESTNODES; j++) { if (j == i) { continue; } if (i == TESTNODES) { /* * highset node has been kicked out and should not * be able to reach any other node */ if (knet_h[i]->host_index[j]->status.reachable != 0) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } else { /* * all other nodes should detect the highest node unreachable * and all the remaining nodes reachable */ if (j == TESTNODES) { if (knet_h[i]->host_index[j]->status.reachable != 0) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } else { if (knet_h[i]->host_index[j]->status.reachable != 1) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } } } } printf("Testing node rejoining with proper version (cluster should reform)\n"); FAIL_ON_ERR(upgrade_onwire_max_ver(knet_h[TESTNODES], TESTNODES, knet_h[1]->onwire_min_ver, knet_h[1]->onwire_max_ver, seconds, logfds[0], stdout)); /* * need more time here for membership to settle */ flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); for (i = 1; i <= TESTNODES; i++) { printf("node %u, onwire: %u min: %u max: %u\n", i, knet_h[i]->onwire_ver, knet_h[i]->onwire_min_ver, knet_h[i]->onwire_max_ver); for (j = 1; j <= TESTNODES; j++) { if (j == i) { continue; } if ((knet_h[i]->host_index[j]->status.reachable != 1) || (knet_h[i]->onwire_ver != knet_h[1]->onwire_max_ver)) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } } printf("Testing node force onwire version\n"); for (i = 1; i <= TESTNODES; i++) { if (knet_handle_set_onwire_ver(knet_h[i], knet_h[i]->onwire_min_ver) < 0) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } /* * need more time here for membership to settle */ flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); sleep(seconds); flush_logs(logfds[0], stdout); for (i = 1; i <= TESTNODES; i++) { printf("node %u, onwire: %u min: %u max: %u\n", i, knet_h[i]->onwire_ver, knet_h[i]->onwire_min_ver, knet_h[i]->onwire_max_ver); for (j = 1; j <= TESTNODES; j++) { if (j == i) { continue; } if ((knet_h[i]->host_index[j]->status.reachable != 1) || (knet_h[i]->onwire_ver != knet_h[1]->onwire_min_ver)) { clean_exit(knet_h, TESTNODES, logfds, FAIL); } } } flush_logs(logfds[0], stdout); close_logpipes(logfds); knet_handle_stop_everything(knet_h, TESTNODES); } int main(int argc, char *argv[]) { test(); return PASS; } diff --git a/libknet/tests/fun_pmtud_crypto.c b/libknet/tests/fun_pmtud_crypto.c index 7dc94340..88d4ee8e 100644 --- a/libknet/tests/fun_pmtud_crypto.c +++ b/libknet/tests/fun_pmtud_crypto.c @@ -1,247 +1,247 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include "libknet.h" #include "compress.h" #include "internals.h" #include "netutils.h" #include "onwire.h" #include "test-common.h" static int private_data; static void sock_notify(void *pvt_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno) { return; } static int iface_fd = 0; static int default_mtu = 0; #ifdef KNET_LINUX const char *loopback = "lo"; #endif #ifdef KNET_BSD const char *loopback = "lo0"; #endif static int fd_init(void) { #ifdef KNET_LINUX return socket(AF_INET, SOCK_STREAM, 0); #endif #ifdef KNET_BSD return socket(AF_LOCAL, SOCK_DGRAM, 0); #endif return -1; } static int set_iface_mtu(uint32_t mtu) { int err = 0; struct ifreq ifr; memset(&ifr, 0, sizeof(struct ifreq)); strncpy(ifr.ifr_name, loopback, IFNAMSIZ - 1); ifr.ifr_mtu = mtu; err = ioctl(iface_fd, SIOCSIFMTU, &ifr); return err; } static int get_iface_mtu(void) { int err = 0, savederrno = 0; struct ifreq ifr; memset(&ifr, 0, sizeof(struct ifreq)); strncpy(ifr.ifr_name, loopback, IFNAMSIZ - 1); err = ioctl(iface_fd, SIOCGIFMTU, &ifr); if (err) { savederrno = errno; goto out_clean; } err = ifr.ifr_mtu; out_clean: errno = savederrno; return err; } static void exit_local(int exit_code) { set_iface_mtu(default_mtu); close(iface_fd); iface_fd = 0; exit(exit_code); } #define TESTNODES 1 static void test_mtu(const char *model, const char *crypto, const char *hash) { knet_handle_t knet_h[TESTNODES+1]; int logfds[2]; int datafd = 0; int8_t channel = 0; struct sockaddr_storage lo; struct knet_handle_crypto_cfg knet_handle_crypto_cfg; unsigned int data_mtu, expected_mtu; size_t calculated_iface_mtu = 0, detected_iface_mtu = 0; int res; setup_logpipes(logfds); knet_h[1] = knet_handle_start(logfds, KNET_LOG_DEBUG, knet_h); flush_logs(logfds[0], stdout); printf("Test knet_send with %s and valid data\n", model); memset(&knet_handle_crypto_cfg, 0, sizeof(struct knet_handle_crypto_cfg)); strncpy(knet_handle_crypto_cfg.crypto_model, model, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_handle_crypto_cfg.crypto_cipher_type, crypto, sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_handle_crypto_cfg.crypto_hash_type, hash, sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); knet_handle_crypto_cfg.private_key_len = 2000; FAIL_ON_ERR(knet_handle_crypto_set_config(knet_h[1], &knet_handle_crypto_cfg, 1)); FAIL_ON_ERR(knet_handle_crypto_use_config(knet_h[1], 1)); FAIL_ON_ERR(knet_handle_crypto_rx_clear_traffic(knet_h[1], KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC)); FAIL_ON_ERR(knet_handle_enable_sock_notify(knet_h[1], &private_data, sock_notify)); // CHECK cond was <0 not !=0 datafd = 0; channel = -1; FAIL_ON_ERR(knet_handle_add_datafd(knet_h[1], &datafd, &channel)); FAIL_ON_ERR(knet_host_add(knet_h[1], 1)); FAIL_ON_ERR(_knet_link_set_config(knet_h[1], 1, 0, KNET_TRANSPORT_UDP, 0, AF_INET, 0, &lo)); FAIL_ON_ERR(knet_link_set_pong_count(knet_h[1], 1, 0, 1)); FAIL_ON_ERR(knet_link_set_enable(knet_h[1], 1, 0, 1)); FAIL_ON_ERR(wait_for_host(knet_h[1], 1, 4, logfds[0], stdout)); flush_logs(logfds[0], stdout); FAIL_ON_ERR(knet_handle_pmtud_get(knet_h[1], &data_mtu)); calculated_iface_mtu = calc_data_outlen(knet_h[1], data_mtu + KNET_HEADER_ALL_SIZE) + 28; detected_iface_mtu = get_iface_mtu(); /* * 28 = 20 IP header + 8 UDP header */ expected_mtu = calc_max_data_outlen(knet_h[1], detected_iface_mtu - 28); if (expected_mtu != data_mtu) { printf("Wrong MTU detected! interface mtu: %zu knet mtu: %u expected mtu: %u\n", detected_iface_mtu, data_mtu, expected_mtu); clean_exit(knet_h, TESTNODES, logfds, FAIL); } if ((detected_iface_mtu - calculated_iface_mtu) >= knet_h[1]->sec_block_size) { printf("Wrong MTU detected! real iface mtu: %zu calculated: %zu\n", detected_iface_mtu, calculated_iface_mtu); clean_exit(knet_h, TESTNODES, logfds, FAIL); } knet_handle_stop_everything(knet_h, TESTNODES); close_logpipes(logfds); } static void test(const char *model, const char *crypto, const char *hash) { int i = 576; int max = 65535; while (i <= max) { printf("Setting interface MTU to: %i\n", i); set_iface_mtu(i); test_mtu(model, crypto, hash); if (i == max) { break; } i = i + 15; if (i > max) { i = max; } } } int main(int argc, char *argv[]) { struct knet_crypto_info crypto_list[16]; size_t crypto_list_entries; #ifdef KNET_BSD if (is_memcheck() || is_helgrind()) { printf("valgrind-freebsd cannot run this test properly. Skipping\n"); return SKIP; } #endif if (geteuid() != 0) { printf("This test requires root privileges\n"); return SKIP; } iface_fd = fd_init(); if (iface_fd < 0) { printf("fd_init failed: %s\n", strerror(errno)); return FAIL; } default_mtu = get_iface_mtu(); if (default_mtu < 0) { printf("get_iface_mtu failed: %s\n", strerror(errno)); return FAIL; } memset(crypto_list, 0, sizeof(crypto_list)); if (knet_get_crypto_list(crypto_list, &crypto_list_entries) < 0) { printf("knet_get_crypto_list failed: %s\n", strerror(errno)); return FAIL; } if (crypto_list_entries == 0) { printf("no crypto modules detected. Skipping\n"); return SKIP; } test(crypto_list[0].name, "aes128", "sha1"); test(crypto_list[0].name, "aes128", "sha256"); test(crypto_list[0].name, "aes256", "sha1"); test(crypto_list[0].name, "aes256", "sha256"); exit_local(PASS); } diff --git a/libknet/tests/int_links_acl_ip.c b/libknet/tests/int_links_acl_ip.c index 83d206eb..fa4cbc93 100644 --- a/libknet/tests/int_links_acl_ip.c +++ b/libknet/tests/int_links_acl_ip.c @@ -1,399 +1,399 @@ /* - * Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "internals.h" #include "links_acl.h" #include "links_acl_ip.h" #include "test-common.h" static struct acl_match_entry *match_entry_v4; static struct acl_match_entry *match_entry_v6; /* This is a test program .. remember! */ #define BUFLEN 1024 static int get_ipaddress(const char *buf, struct sockaddr_storage *addr) { struct addrinfo *info; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; if (getaddrinfo(buf, NULL, &hints, &info)) { return -1; } memmove(addr, info->ai_addr, info->ai_addrlen); freeaddrinfo(info); return 0; } static int read_2ip(const char *buf, const char *delim, struct sockaddr_storage *addr, struct sockaddr_storage *addr2) { char tmpbuf[BUFLEN]; char *deli; deli = strstr(buf, delim); if (!deli) { return -1; } strncpy(tmpbuf, buf, deli-buf); tmpbuf[deli-buf] = '\0'; if (get_ipaddress(tmpbuf, addr)) { return -1; } if (get_ipaddress(deli+1, addr2)) { return -1; } return 0; } /* * be aware that ordering is important * so we can test all the rules with few * ipcheck_validate calls */ static const char *rules[100] = { /* * ipv4 */ "RA192.168.0.3", /* reject address */ "AA192.168.0.1", /* accept address */ "RR192.168.0.10-192.168.0.20", /* reject range */ "AR192.168.0.0-192.168.0.255", /* accept range */ "RM192.168.2.0/255.255.255.0", /* reject mask */ "AM192.168.2.0/255.255.254.0", /* accept mask */ /* * ipv6 */ "RA3ffe::3", "AA3ffe::1", "RR3ffe::10-3ffe::20", "AR3ffe::0-3ffe::ff", "RM3ffe:1::0/ffff:ffff:ffff:ffff:ffff:ffff:ffff:0", "AM3ffe:1::0/ffff:ffff:ffff:ffff::0" }; static int _ipcheck_addip(void *fd_tracker_match_entry_head, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject) { return ipcheck_addip(fd_tracker_match_entry_head, -1, ss1, ss2, type, acceptreject); } static int default_rules(int load) { int ret; check_type_t type; check_acceptreject_t acceptreject; struct sockaddr_storage addr1; struct sockaddr_storage addr2; int i = 0; int (*loadfn)(void *fd_tracker_match_entry_head, struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, check_type_t type, check_acceptreject_t acceptreject); if (load) { loadfn = _ipcheck_addip; } else { loadfn = ipcheck_rmip; } while (rules[i] != NULL) { printf("Parsing rule: %s\n", rules[i]); memset(&addr1, 0, sizeof(struct sockaddr_storage)); memset(&addr2, 0, sizeof(struct sockaddr_storage)); /* * First char is A (accept) or R (Reject) */ switch(rules[i][0] & 0x5F) { case 'A': acceptreject = CHECK_ACCEPT; break; case 'R': acceptreject = CHECK_REJECT; break; default: fprintf(stderr, "Unknown record type on line %d: %s\n", i, rules[i]); goto next_record; } /* * Second char is the filter type: * A Address * M Mask * R Range */ switch(rules[i][1] & 0x5F) { case 'A': type = CHECK_TYPE_ADDRESS; ret = get_ipaddress(rules[i]+2, &addr1); break; case 'M': type = CHECK_TYPE_MASK; ret = read_2ip(rules[i]+2, "/", &addr1, &addr2); break; case 'R': type = CHECK_TYPE_RANGE; ret = read_2ip(rules[i]+2, "-", &addr1, &addr2); break; default: fprintf(stderr, "Unknown filter type on line %d: %s\n", i, rules[i]); goto next_record; break; } if (ret) { fprintf(stderr, "Failed to parse address on line %d: %s\n", i, rules[i]); return -1; } else { if (addr1.ss_family == AF_INET) { if (loadfn(&match_entry_v4, &addr1, &addr2, type, acceptreject) < 0) { fprintf(stderr, "Failed to add/rm address on line %d: %s (errno: %s)\n", i, rules[i], strerror(errno)); return -1; } } else { if (loadfn(&match_entry_v6, &addr1, &addr2, type, acceptreject) < 0) { fprintf(stderr, "Failed to add/rm address on line %d: %s (errno: %s)\n", i, rules[i], strerror(errno)); return -1; } } } next_record: i++; } return 0; } static const char *tests[100] = { /* * ipv4 */ "R192.168.0.3", /* reject address */ "A192.168.0.1", /* accept address */ "R192.168.0.11", /* reject range */ "A192.168.0.8", /* accept range */ "R192.168.2.1", /* reject mask */ "A192.168.3.1", /* accept mask */ /* * ipv6 */ "R3ffe::3", "A3ffe::1", "R3ffe::11", "A3ffe::8", "R3ffe:1::1", "A3ffe:1::1:1" }; static const char *after_insert_tests[100] = { /* * ipv4 */ "R192.168.0.3", /* reject address */ "A192.168.0.1", /* accept address */ "R192.168.0.11", /* reject range */ "A192.168.0.8", /* accept range */ "A192.168.2.1", /* reject mask */ "A192.168.3.1", /* accept mask */ /* * ipv6 */ "R3ffe::3", "A3ffe::1", "R3ffe::11", "A3ffe::8", "A3ffe:1::1", "A3ffe:1::1:1" }; int test(void) { int i = 0; int expected; struct sockaddr_storage saddr; struct acl_match_entry *match_entry; /* * default tests */ while (tests[i] != NULL) { /* * First char is A (accept) or R (Reject) */ switch(tests[i][0] & 0x5F) { case 'A': expected = 1; break; case 'R': expected = 0; break; default: fprintf(stderr, "Unknown record type on line %d: %s\n", i, tests[i]); return FAIL; break; } if (get_ipaddress(tests[i]+1, &saddr)) { fprintf(stderr, "Cannot parse address %s\n", tests[i]+1); return FAIL; } if (saddr.ss_family == AF_INET) { match_entry = match_entry_v4; } else { match_entry = match_entry_v6; } if (ipcheck_validate(&match_entry, &saddr) != expected) { fprintf(stderr, "Failed to check access list for ip: %s\n", tests[i]); return FAIL; } i++; } /* * insert tests */ if (get_ipaddress("192.168.2.1", &saddr)) { fprintf(stderr, "Cannot parse address 192.168.2.1\n"); return FAIL; } if (ipcheck_addip(&match_entry_v4, 3, &saddr, &saddr, CHECK_TYPE_ADDRESS, CHECK_ACCEPT) < 0) { fprintf(stderr, "Unable to insert address in position 3 192.168.2.1\n"); return FAIL; } if (get_ipaddress("3ffe:1::1", &saddr)) { fprintf(stderr, "Cannot parse address 3ffe:1::1\n"); return FAIL; } if (ipcheck_addip(&match_entry_v6, 3, &saddr, &saddr, CHECK_TYPE_ADDRESS, CHECK_ACCEPT) < 0) { fprintf(stderr, "Unable to insert address in position 3 3ffe:1::1\n"); return FAIL; } while (after_insert_tests[i] != NULL) { /* * First char is A (accept) or R (Reject) */ switch(after_insert_tests[i][0] & 0x5F) { case 'A': expected = 1; break; case 'R': expected = 0; break; default: fprintf(stderr, "Unknown record type on line %d: %s\n", i, after_insert_tests[i]); return FAIL; break; } if (get_ipaddress(after_insert_tests[i]+1, &saddr)) { fprintf(stderr, "Cannot parse address %s\n", after_insert_tests[i]+1); return FAIL; } if (saddr.ss_family == AF_INET) { match_entry = match_entry_v4; } else { match_entry = match_entry_v6; } if (ipcheck_validate(&match_entry, &saddr) != expected) { fprintf(stderr, "Failed to check access list for ip: %s\n", after_insert_tests[i]); return FAIL; } i++; } return PASS; } int main(int argc, char *argv[]) { struct sockaddr_storage saddr; struct acl_match_entry *match_entry; int ret = PASS; int i; if (default_rules(1) < 0) { return -1; } if (argc > 1) { /* * run manual check against default access lists */ for (i=1; i * Federico Simoncelli * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include "test-common.h" #define timespec_set(x, sec, nsec) \ do { \ x.tv_sec = sec; \ x.tv_nsec = nsec; \ } while (0); static void check_timespec_diff(void) { unsigned long long diff; struct timespec start, end; timespec_set(start, 1, 30000); timespec_set(end, start.tv_sec, start.tv_nsec + 10000); timespec_diff(start, end, &diff); printf("Checking 10000 == %llu\n", diff); if (diff != 10000) { printf("Failure!\n"); exit(FAIL); } timespec_set(end, start.tv_sec + 5, start.tv_nsec - 5000); timespec_diff(start, end, &diff); printf("Checking 4999995000 == %llu\n", diff); if (diff != 4999995000llu) { printf("Failure!\n"); exit(FAIL); } } int main(int argc, char *argv[]) { check_timespec_diff(); return PASS; } diff --git a/libknet/tests/knet_bench.c b/libknet/tests/knet_bench.c index 82b5d4b3..d9b9deea 100644 --- a/libknet/tests/knet_bench.c +++ b/libknet/tests/knet_bench.c @@ -1,1379 +1,1379 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include "libknet.h" #include "compat.h" #include "internals.h" #include "netutils.h" #include "transport_common.h" #include "test-common.h" #define MAX_NODES 128 static int senderid = -1; static int thisnodeid = -1; static knet_handle_t knet_h; static int datafd = 0; static int8_t channel = 0; static int globallistener = 0; static int continous = 0; static int show_stats = 0; static struct sockaddr_storage allv4; static struct sockaddr_storage allv6; static int broadcast_test = 1; static pthread_t rx_thread = (pthread_t)NULL; static char *rx_buf[PCKT_FRAG_MAX]; static int wait_for_perf_rx = 0; static char *compresscfg = NULL; static char *cryptocfg = NULL; static int machine_output = 0; static int use_access_lists = 0; static int use_pckt_verification = 0; static int bench_shutdown_in_progress = 0; static pthread_mutex_t shutdown_mutex = PTHREAD_MUTEX_INITIALIZER; #define TEST_PING 0 #define TEST_PING_AND_DATA 1 #define TEST_PERF_BY_SIZE 2 #define TEST_PERF_BY_TIME 3 static int test_type = TEST_PING; #define TEST_START 2 #define TEST_STOP 4 #define TEST_COMPLETE 6 #define ONE_GIGABYTE 1073741824 static uint64_t perf_by_size_size = 1 * ONE_GIGABYTE; static uint64_t perf_by_time_secs = 10; static uint32_t force_packet_size = 0; struct node { int nodeid; int links; uint8_t transport[KNET_MAX_LINK]; struct sockaddr_storage address[KNET_MAX_LINK]; }; struct pckt_ver { uint32_t len; uint32_t chksum; }; static void print_help(void) { printf("knet_bench usage:\n"); printf(" -h print this help (no really)\n"); printf(" -d enable debug logs (default INFO)\n"); printf(" -f enable use of access lists (default: off)\n"); printf(" -c [implementation]:[crypto]:[hashing] crypto configuration. (default disabled)\n"); printf(" Example: -c nss:aes128:sha1\n"); printf(" -z [implementation]:[level]:[threshold] compress configuration. (default disabled)\n"); printf(" Example: -z zlib:5:100\n"); printf(" -p [active|passive|rr] (default: passive)\n"); printf(" -P [UDP|SCTP] (default: UDP) protocol (transport) to use for all links\n"); printf(" -t [nodeid] This nodeid (required)\n"); printf(" -n [nodeid],[proto]/[link1_ip],[link2_..] Other nodes information (at least one required)\n"); printf(" Example: -n 1,192.168.8.1,SCTP/3ffe::8:1,UDP/172...\n"); printf(" can be repeated up to %d and should contain also the localnode info\n", MAX_NODES); printf(" -b [port] baseport (default: 50000)\n"); printf(" -l enable global listener on 0.0.0.0/:: (default: off, incompatible with -o)\n"); printf(" -o enable baseport offset per nodeid\n"); printf(" -m change PMTUd interval in seconds (default: 60)\n"); printf(" -w dont wait for all nodes to be up before starting the test (default: wait)\n"); printf(" -T [ping|ping_data|perf-by-size|perf-by-time]\n"); printf(" test type (default: ping)\n"); printf(" ping: will wait for all hosts to join the knet network, sleep 5 seconds and quit\n"); printf(" ping_data: will wait for all hosts to join the knet network, sends some data to all nodes and quit\n"); printf(" perf-by-size: will wait for all hosts to join the knet network,\n"); printf(" perform a series of benchmarks by transmitting a known\n"); printf(" size/quantity of packets and measuring the time, then quit\n"); printf(" perf-by-time: will wait for all hosts to join the knet network,\n"); printf(" perform a series of benchmarks by transmitting a known\n"); printf(" size of packets for a given amount of time (10 seconds)\n"); printf(" and measuring the quantity of data transmitted, then quit\n"); printf(" -s nodeid that will generate traffic for benchmarks\n"); printf(" -S [size|seconds] when used in combination with -T perf-by-size it indicates how many GB of traffic to generate for the test. (default: 1GB)\n"); printf(" when used in combination with -T perf-by-time it indicates how many Seconds of traffic to generate for the test. (default: 10 seconds)\n"); printf(" -x force packet size for perf-by-time or perf-by-size\n"); printf(" -C repeat the test continously (default: off)\n"); printf(" -X[XX] show stats at the end of the run (default: 1)\n"); printf(" 1: show handle stats, 2: show summary link stats\n"); printf(" 3: show detailed link stats\n"); printf(" -a enable machine parsable output (default: off).\n"); printf(" -v enable packet verification for performance tests (default: off).\n"); } static void parse_nodes(char *nodesinfo[MAX_NODES], int onidx, int port, struct node nodes[MAX_NODES], int *thisidx) { int i; char *temp = NULL; char port_str[10]; memset(port_str, 0, sizeof(port_str)); sprintf(port_str, "%d", port); for (i = 0; i < onidx; i++) { nodes[i].nodeid = atoi(strtok(nodesinfo[i], ",")); if ((nodes[i].nodeid < 0) || (nodes[i].nodeid > KNET_MAX_HOST)) { printf("Invalid nodeid: %d (0 - %d)\n", nodes[i].nodeid, KNET_MAX_HOST); exit(FAIL); } if (thisnodeid == nodes[i].nodeid) { *thisidx = i; } while((temp = strtok(NULL, ","))) { char *slash = NULL; uint8_t transport; if (nodes[i].links == KNET_MAX_LINK) { printf("Too many links configured. Max %d\n", KNET_MAX_LINK); exit(FAIL); } slash = strstr(temp, "/"); if (slash) { memset(slash, 0, 1); transport = knet_get_transport_id_by_name(temp); if (transport == KNET_MAX_TRANSPORTS) { printf("Unknown transport: %s\n", temp); exit(FAIL); } nodes[i].transport[nodes[i].links] = transport; temp = slash + 1; } else { nodes[i].transport[nodes[i].links] = KNET_TRANSPORT_UDP; } if (knet_strtoaddr(temp, port_str, &nodes[i].address[nodes[i].links], sizeof(struct sockaddr_storage)) < 0) { printf("Unable to convert %s to sockaddress\n", temp); exit(FAIL); } nodes[i].links++; } } if (knet_strtoaddr("0.0.0.0", port_str, &allv4, sizeof(struct sockaddr_storage)) < 0) { printf("Unable to convert 0.0.0.0 to sockaddress\n"); exit(FAIL); } if (knet_strtoaddr("::", port_str, &allv6, sizeof(struct sockaddr_storage)) < 0) { printf("Unable to convert :: to sockaddress\n"); exit(FAIL); } for (i = 1; i < onidx; i++) { if (nodes[0].links != nodes[i].links) { printf("knet_bench does not support unbalanced link configuration\n"); exit(FAIL); } } return; } static int private_data; static void sock_notify(void *pvt_data, int local_datafd, int8_t local_channel, uint8_t tx_rx, int error, int errorno) { printf("[info]: error (%d - %d - %s) from socket: %d\n", error, errorno, strerror(errno), local_datafd); return; } static int ping_dst_host_filter(void *pvt_data, const unsigned char *outdata, ssize_t outdata_len, uint8_t tx_rx, knet_node_id_t this_host_id, knet_node_id_t src_host_id, int8_t *dst_channel, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries) { if (broadcast_test) { return 1; } if (tx_rx == KNET_NOTIFY_TX) { memmove(&dst_host_ids[0], outdata, 2); } else { dst_host_ids[0] = this_host_id; } *dst_host_ids_entries = 1; return 0; } static void setup_knet(int argc, char *argv[]) { int logfd = 0; int rv; char *policystr = NULL, *protostr = NULL; char *othernodeinfo[MAX_NODES]; struct node nodes[MAX_NODES]; int thisidx = -1; int onidx = 0; int debug = KNET_LOG_INFO; int port = 50000, portoffset = 0; int thisport = 0, otherport = 0; int thisnewport = 0, othernewport = 0; struct sockaddr_in *so_in; struct sockaddr_in6 *so_in6; struct sockaddr_storage *src; int i, link_idx, allnodesup = 0; int policy = KNET_LINK_POLICY_PASSIVE, policyfound = 0; int protocol = KNET_TRANSPORT_UDP, protofound = 0; int wait = 1; int pmtud_interval = 60; struct knet_handle_crypto_cfg knet_handle_crypto_cfg; char *cryptomodel = NULL, *cryptotype = NULL, *cryptohash = NULL; struct knet_handle_compress_cfg knet_handle_compress_cfg; memset(nodes, 0, sizeof(nodes)); while ((rv = getopt(argc, argv, "aCT:S:s:lvdfom:wb:t:n:c:p:x:X::P:z:h")) != EOF) { switch(rv) { case 'h': print_help(); exit(PASS); break; case 'a': machine_output = 1; break; case 'd': debug = KNET_LOG_DEBUG; break; case 'f': use_access_lists = 1; break; case 'c': if (cryptocfg) { printf("Error: -c can only be specified once\n"); exit(FAIL); } cryptocfg = optarg; break; case 'p': if (policystr) { printf("Error: -p can only be specified once\n"); exit(FAIL); } if (optarg) { policystr = optarg; if (!strcmp(policystr, "active")) { policy = KNET_LINK_POLICY_ACTIVE; policyfound = 1; } /* * we can't use rr because clangs can't compile * an array of 3 strings, one of which is 2 bytes long */ if (!strcmp(policystr, "round-robin")) { policy = KNET_LINK_POLICY_RR; policyfound = 1; } if (!strcmp(policystr, "passive")) { policy = KNET_LINK_POLICY_PASSIVE; policyfound = 1; } } if (!policyfound) { printf("Error: invalid policy %s specified. -p accepts active|passive|rr\n", policystr); exit(FAIL); } break; case 'P': if (protostr) { printf("Error: -P can only be specified once\n"); exit(FAIL); } if (optarg) { protostr = optarg; if (!strcmp(protostr, "UDP")) { protocol = KNET_TRANSPORT_UDP; protofound = 1; } if (!strcmp(protostr, "SCTP")) { protocol = KNET_TRANSPORT_SCTP; protofound = 1; } } if (!protofound) { printf("Error: invalid protocol %s specified. -P accepts udp|sctp\n", policystr); exit(FAIL); } break; case 't': if (thisnodeid >= 0) { printf("Error: -t can only be specified once\n"); exit(FAIL); } thisnodeid = atoi(optarg); if ((thisnodeid < 0) || (thisnodeid > 65536)) { printf("Error: -t nodeid out of range %d (1 - 65536)\n", thisnodeid); exit(FAIL); } break; case 'n': if (onidx == MAX_NODES) { printf("Error: too many other nodes. Max %d\n", MAX_NODES); exit(FAIL); } othernodeinfo[onidx] = optarg; onidx++; break; case 'b': port = atoi(optarg); if ((port < 1) || (port > 65536)) { printf("Error: port %d out of range (1 - 65536)\n", port); exit(FAIL); } break; case 'o': if (globallistener) { printf("Error: -l cannot be used with -o\n"); exit(FAIL); } portoffset = 1; break; case 'm': pmtud_interval = atoi(optarg); if (pmtud_interval < 1) { printf("Error: pmtud interval %d out of range (> 0)\n", pmtud_interval); exit(FAIL); } break; case 'l': if (portoffset) { printf("Error: -o cannot be used with -l\n"); exit(FAIL); } globallistener = 1; break; case 'w': wait = 0; break; case 's': if (senderid >= 0) { printf("Error: -s can only be specified once\n"); exit(FAIL); } senderid = atoi(optarg); if ((senderid < 0) || (senderid > 65536)) { printf("Error: -s nodeid out of range %d (1 - 65536)\n", senderid); exit(FAIL); } break; case 'T': if (optarg) { if (!strcmp("ping", optarg)) { test_type = TEST_PING; } if (!strcmp("ping_data", optarg)) { test_type = TEST_PING_AND_DATA; } if (!strcmp("perf-by-size", optarg)) { test_type = TEST_PERF_BY_SIZE; } if (!strcmp("perf-by-time", optarg)) { test_type = TEST_PERF_BY_TIME; } } else { printf("Error: -T requires an option\n"); exit(FAIL); } break; case 'S': perf_by_size_size = (uint64_t)atoi(optarg) * ONE_GIGABYTE; perf_by_time_secs = (uint64_t)atoi(optarg); break; case 'x': force_packet_size = (uint32_t)atoi(optarg); if ((force_packet_size < 64) || (force_packet_size > 65536)) { printf("Unsupported packet size %u (accepted 64 - 65536)\n", force_packet_size); exit(FAIL); } break; case 'v': use_pckt_verification = 1; break; case 'C': continous = 1; break; case 'X': if (optarg) { show_stats = atoi(optarg); } else { show_stats = 1; } break; case 'z': if (compresscfg) { printf("Error: -c can only be specified once\n"); exit(FAIL); } compresscfg = optarg; break; default: break; } } if (thisnodeid < 0) { printf("Who am I?!? missing -t from command line?\n"); exit(FAIL); } if (onidx < 1) { printf("no other nodes configured?!? missing -n from command line\n"); exit(FAIL); } parse_nodes(othernodeinfo, onidx, port, nodes, &thisidx); if (thisidx < 0) { printf("no config for this node found\n"); exit(FAIL); } if (senderid >= 0) { for (i=0; i < onidx; i++) { if (senderid == nodes[i].nodeid) { break; } } if (i == onidx) { printf("Unable to find senderid in nodelist\n"); exit(FAIL); } } if (((test_type == TEST_PERF_BY_SIZE) || (test_type == TEST_PERF_BY_TIME)) && (senderid < 0)) { printf("Error: performance test requires -s to be set (for now)\n"); exit(FAIL); } logfd = start_logging(stdout); knet_h = knet_handle_new(thisnodeid, logfd, debug, 0); if (!knet_h) { printf("Unable to knet_handle_new: %s\n", strerror(errno)); exit(FAIL); } if (knet_handle_enable_access_lists(knet_h, use_access_lists) < 0) { printf("Unable to knet_handle_enable_access_lists: %s\n", strerror(errno)); exit(FAIL); } if (knet_handle_set_host_defrag_bufs(knet_h, 4, KNET_MAX_DEFRAG_BUFS_DEFAULT, KNET_SHRINK_THRESHOLD_DEFAULT, RECLAIM_POLICY_ABSOLUTE) < 0) { printf("Unable to knet_handle_set_host_defrag_bufs: %s\n", strerror(errno)); exit(FAIL); } if (cryptocfg) { memset(&knet_handle_crypto_cfg, 0, sizeof(knet_handle_crypto_cfg)); cryptomodel = strtok(cryptocfg, ":"); cryptotype = strtok(NULL, ":"); cryptohash = strtok(NULL, ":"); if (cryptomodel) { strncpy(knet_handle_crypto_cfg.crypto_model, cryptomodel, sizeof(knet_handle_crypto_cfg.crypto_model) - 1); } if (cryptotype) { strncpy(knet_handle_crypto_cfg.crypto_cipher_type, cryptotype, sizeof(knet_handle_crypto_cfg.crypto_cipher_type) - 1); } if (cryptohash) { strncpy(knet_handle_crypto_cfg.crypto_hash_type, cryptohash, sizeof(knet_handle_crypto_cfg.crypto_hash_type) - 1); } knet_handle_crypto_cfg.private_key_len = KNET_MAX_KEY_LEN; if (knet_handle_crypto_set_config(knet_h, &knet_handle_crypto_cfg, 1)) { printf("Unable to set crypto config\n"); exit(FAIL); } if (knet_handle_crypto_use_config(knet_h, 1)) { printf("Unable to use crypto config\n"); exit(FAIL); } if (knet_handle_crypto_rx_clear_traffic(knet_h, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC)) { printf("Unable to disable clear traffic on RX\n"); exit(FAIL); } } if (compresscfg) { memset(&knet_handle_compress_cfg, 0, sizeof(struct knet_handle_compress_cfg)); snprintf(knet_handle_compress_cfg.compress_model, 16, "%s", strtok(compresscfg, ":")); knet_handle_compress_cfg.compress_level = atoi(strtok(NULL, ":")); knet_handle_compress_cfg.compress_threshold = atoi(strtok(NULL, ":")); if (knet_handle_compress(knet_h, &knet_handle_compress_cfg)) { printf("Unable to configure compress\n"); exit(FAIL); } } if (knet_handle_enable_sock_notify(knet_h, &private_data, sock_notify) < 0) { printf("knet_handle_enable_sock_notify failed: %s\n", strerror(errno)); knet_handle_free(knet_h); exit(FAIL); } datafd = 0; channel = -1; if (knet_handle_add_datafd(knet_h, &datafd, &channel) < 0) { printf("knet_handle_add_datafd failed: %s\n", strerror(errno)); knet_handle_free(knet_h); exit(FAIL); } if (knet_handle_pmtud_setfreq(knet_h, pmtud_interval) < 0) { printf("knet_handle_pmtud_setfreq failed: %s\n", strerror(errno)); knet_handle_free(knet_h); exit(FAIL); } for (i=0; i < onidx; i++) { if (i == thisidx) { continue; } if (knet_host_add(knet_h, nodes[i].nodeid) < 0) { printf("knet_host_add failed: %s\n", strerror(errno)); exit(FAIL); } if (knet_host_set_policy(knet_h, nodes[i].nodeid, policy) < 0) { printf("knet_host_set_policy failed: %s\n", strerror(errno)); exit(FAIL); } for (link_idx = 0; link_idx < nodes[i].links; link_idx++) { if (portoffset) { if (nodes[thisidx].address[link_idx].ss_family == AF_INET) { so_in = (struct sockaddr_in *)&nodes[thisidx].address[link_idx]; thisport = ntohs(so_in->sin_port); thisnewport = thisport + nodes[i].nodeid; so_in->sin_port = (htons(thisnewport)); so_in = (struct sockaddr_in *)&nodes[i].address[link_idx]; otherport = ntohs(so_in->sin_port); othernewport = otherport + nodes[thisidx].nodeid; so_in->sin_port = (htons(othernewport)); } else { so_in6 = (struct sockaddr_in6 *)&nodes[thisidx].address[link_idx]; thisport = ntohs(so_in6->sin6_port); thisnewport = thisport + nodes[i].nodeid; so_in6->sin6_port = (htons(thisnewport)); so_in6 = (struct sockaddr_in6 *)&nodes[i].address[link_idx]; otherport = ntohs(so_in6->sin6_port); othernewport = otherport + nodes[thisidx].nodeid; so_in6->sin6_port = (htons(othernewport)); } } if (!globallistener) { src = &nodes[thisidx].address[link_idx]; } else { if (nodes[thisidx].address[link_idx].ss_family == AF_INET) { src = &allv4; } else { src = &allv6; } } /* * -P overrides per link protocol configuration */ if (protofound) { nodes[i].transport[link_idx] = protocol; } if (knet_link_set_config(knet_h, nodes[i].nodeid, link_idx, nodes[i].transport[link_idx], src, &nodes[i].address[link_idx], 0) < 0) { printf("Unable to configure link: %s\n", strerror(errno)); exit(FAIL); } if (portoffset) { if (nodes[thisidx].address[link_idx].ss_family == AF_INET) { so_in = (struct sockaddr_in *)&nodes[thisidx].address[link_idx]; so_in->sin_port = (htons(thisport)); so_in = (struct sockaddr_in *)&nodes[i].address[link_idx]; so_in->sin_port = (htons(otherport)); } else { so_in6 = (struct sockaddr_in6 *)&nodes[thisidx].address[link_idx]; so_in6->sin6_port = (htons(thisport)); so_in6 = (struct sockaddr_in6 *)&nodes[i].address[link_idx]; so_in6->sin6_port = (htons(otherport)); } } if (knet_link_set_enable(knet_h, nodes[i].nodeid, link_idx, 1) < 0) { printf("knet_link_set_enable failed: %s\n", strerror(errno)); exit(FAIL); } if (knet_link_set_ping_timers(knet_h, nodes[i].nodeid, link_idx, 1000, 10000, 2048) < 0) { printf("knet_link_set_ping_timers failed: %s\n", strerror(errno)); exit(FAIL); } if (knet_link_set_pong_count(knet_h, nodes[i].nodeid, link_idx, 2) < 0) { printf("knet_link_set_pong_count failed: %s\n", strerror(errno)); exit(FAIL); } } } if (knet_handle_enable_filter(knet_h, NULL, ping_dst_host_filter)) { printf("Unable to enable dst_host_filter: %s\n", strerror(errno)); exit(FAIL); } if (knet_handle_setfwd(knet_h, 1) < 0) { printf("knet_handle_setfwd failed: %s\n", strerror(errno)); exit(FAIL); } if (wait) { while(!allnodesup) { allnodesup = 1; for (i=0; i < onidx; i++) { if (i == thisidx) { continue; } if (knet_h->host_index[nodes[i].nodeid]->status.reachable != 1) { printf("[info]: waiting host %d to be reachable\n", nodes[i].nodeid); allnodesup = 0; } } if (!allnodesup) { sleep(1); } } sleep(1); } } static void *_rx_thread(void *args) { int rx_epoll; struct epoll_event ev; struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; struct sockaddr_storage address[PCKT_FRAG_MAX]; struct knet_mmsghdr msg[PCKT_FRAG_MAX]; struct iovec iov_in[PCKT_FRAG_MAX]; int i, msg_recv; struct timespec clock_start, clock_end; unsigned long long time_diff = 0; uint64_t rx_pkts = 0; uint64_t rx_bytes = 0; unsigned int current_pckt_size = 0; for (i = 0; i < PCKT_FRAG_MAX; i++) { rx_buf[i] = malloc(KNET_MAX_PACKET_SIZE); if (!rx_buf[i]) { printf("RXT: Unable to malloc!\nHALTING RX THREAD!\n"); return NULL; } memset(rx_buf[i], 0, KNET_MAX_PACKET_SIZE); iov_in[i].iov_base = (void *)rx_buf[i]; iov_in[i].iov_len = KNET_MAX_PACKET_SIZE; memset(&msg[i].msg_hdr, 0, sizeof(struct msghdr)); msg[i].msg_hdr.msg_name = &address[i]; msg[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); msg[i].msg_hdr.msg_iov = &iov_in[i]; msg[i].msg_hdr.msg_iovlen = 1; } rx_epoll = epoll_create(KNET_EPOLL_MAX_EVENTS + 1); if (rx_epoll < 0) { printf("RXT: Unable to create epoll!\nHALTING RX THREAD!\n"); return NULL; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = datafd; if (epoll_ctl(rx_epoll, EPOLL_CTL_ADD, datafd, &ev)) { printf("RXT: Unable to add datafd to epoll\nHALTING RX THREAD!\n"); return NULL; } memset(&clock_start, 0, sizeof(clock_start)); memset(&clock_end, 0, sizeof(clock_start)); while (!bench_shutdown_in_progress) { if (epoll_wait(rx_epoll, events, KNET_EPOLL_MAX_EVENTS, 1) >= 1) { msg_recv = _recvmmsg(datafd, &msg[0], PCKT_FRAG_MAX, MSG_DONTWAIT | MSG_NOSIGNAL); if (msg_recv < 0) { printf("[info]: RXT: error from recvmmsg: %s\n", strerror(errno)); } switch(test_type) { case TEST_PING_AND_DATA: for (i = 0; i < msg_recv; i++) { if (msg[i].msg_len == 0) { printf("[info]: RXT: received 0 bytes message?\n"); } printf("[info]: received %u bytes message: %s\n", msg[i].msg_len, (char *)msg[i].msg_hdr.msg_iov->iov_base); } break; case TEST_PERF_BY_TIME: case TEST_PERF_BY_SIZE: for (i = 0; i < msg_recv; i++) { if (msg[i].msg_len < 64) { if (msg[i].msg_len == 0) { printf("[info]: RXT: received 0 bytes message?\n"); } if (msg[i].msg_len == TEST_START) { if (clock_gettime(CLOCK_MONOTONIC, &clock_start) != 0) { printf("[info]: unable to get start time!\n"); } } if (msg[i].msg_len == TEST_STOP) { double average_rx_mbytes; double average_rx_pkts; double time_diff_sec; if (clock_gettime(CLOCK_MONOTONIC, &clock_end) != 0) { printf("[info]: unable to get end time!\n"); } timespec_diff(clock_start, clock_end, &time_diff); /* * adjust for sleep(2) between sending the last data and TEST_STOP */ time_diff = time_diff - 2000000000llu; /* * convert to seconds */ time_diff_sec = (double)time_diff / 1000000000llu; average_rx_mbytes = (double)((rx_bytes / time_diff_sec) / (1024 * 1024)); average_rx_pkts = (double)(rx_pkts / time_diff_sec); if (!machine_output) { printf("[perf] execution time: %8.4f secs Average speed: %8.4f MB/sec %8.4f pckts/sec (size: %u total: %" PRIu64 ")\n", time_diff_sec, average_rx_mbytes, average_rx_pkts, current_pckt_size, rx_pkts); } else { printf("[perf],%.4f,%u,%" PRIu64 ",%.4f,%.4f\n", time_diff_sec, current_pckt_size, rx_pkts, average_rx_mbytes, average_rx_pkts); } rx_pkts = 0; rx_bytes = 0; current_pckt_size = 0; } if (msg[i].msg_len == TEST_COMPLETE) { wait_for_perf_rx = 1; } continue; } if (use_pckt_verification) { struct pckt_ver *recv_pckt = (struct pckt_ver *)msg[i].msg_hdr.msg_iov->iov_base; uint32_t chksum; if (msg[i].msg_len != recv_pckt->len) { printf("Wrong packet len received: %u expected: %u!\n", msg[i].msg_len, recv_pckt->len); exit(FAIL); } chksum = compute_chksum((const unsigned char *)msg[i].msg_hdr.msg_iov->iov_base + sizeof(struct pckt_ver), msg[i].msg_len - sizeof(struct pckt_ver)); if (recv_pckt->chksum != chksum){ printf("Wrong packet checksum received: %u expected: %u!\n", recv_pckt->chksum, chksum); exit(FAIL); } } rx_pkts++; rx_bytes = rx_bytes + msg[i].msg_len; current_pckt_size = msg[i].msg_len; } break; } } } epoll_ctl(rx_epoll, EPOLL_CTL_DEL, datafd, &ev); close(rx_epoll); return NULL; } static void setup_data_txrx_common(void) { if (!rx_thread) { if (knet_handle_enable_filter(knet_h, NULL, ping_dst_host_filter)) { printf("Unable to enable dst_host_filter: %s\n", strerror(errno)); exit(FAIL); } printf("[info]: setting up rx thread\n"); if (pthread_create(&rx_thread, 0, _rx_thread, NULL)) { printf("Unable to start rx thread\n"); exit(FAIL); } } } static void stop_rx_thread(void) { void *retval; int i; if (rx_thread) { printf("[info]: shutting down rx thread\n"); sleep(2); pthread_cancel(rx_thread); pthread_join(rx_thread, &retval); for (i = 0; i < PCKT_FRAG_MAX; i ++) { free(rx_buf[i]); } } } static void send_ping_data(void) { char buf[65535]; ssize_t len; memset(&buf, 0, sizeof(buf)); snprintf(buf, sizeof(buf), "Hello world!"); if (compresscfg) { len = sizeof(buf); } else { len = strlen(buf); } if (knet_send(knet_h, buf, len, channel) != len) { printf("[info]: Error sending hello world: %s\n", strerror(errno)); } sleep(1); } static int send_messages(struct knet_mmsghdr *msg, int msgs_to_send) { int sent_msgs, prev_sent, progress, total_sent; total_sent = 0; sent_msgs = 0; prev_sent = 0; progress = 1; retry: errno = 0; sent_msgs = _sendmmsg(datafd, 0, &msg[0], msgs_to_send, MSG_NOSIGNAL); if (sent_msgs < 0) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { usleep(KNET_THREADS_TIMER_RES / 16); goto retry; } printf("[info]: Unable to send messages: %s\n", strerror(errno)); return -1; } total_sent = total_sent + sent_msgs; if ((sent_msgs >= 0) && (sent_msgs < msgs_to_send)) { if ((sent_msgs) || (progress)) { msgs_to_send = msgs_to_send - sent_msgs; prev_sent = prev_sent + sent_msgs; if (sent_msgs) { progress = 1; } else { progress = 0; } goto retry; } if (!progress) { printf("[info]: Unable to send more messages after retry\n"); } } return total_sent; } static int setup_send_buffers_common(struct knet_mmsghdr *msg, struct iovec *iov_out, char *tx_buf[]) { int i; for (i = 0; i < PCKT_FRAG_MAX; i++) { tx_buf[i] = malloc(KNET_MAX_PACKET_SIZE); if (!tx_buf[i]) { printf("TXT: Unable to malloc!\n"); return -1; } memset(tx_buf[i], i, KNET_MAX_PACKET_SIZE); iov_out[i].iov_base = (void *)tx_buf[i]; memset(&msg[i].msg_hdr, 0, sizeof(struct msghdr)); msg[i].msg_hdr.msg_iov = &iov_out[i]; msg[i].msg_hdr.msg_iovlen = 1; } return 0; } static void send_perf_data_by_size(void) { char *tx_buf[PCKT_FRAG_MAX]; struct knet_mmsghdr msg[PCKT_FRAG_MAX]; struct iovec iov_out[PCKT_FRAG_MAX]; char ctrl_message[16]; int sent_msgs; int i; uint64_t total_pkts_to_tx; uint64_t packets_to_send; uint32_t packetsize = 64; setup_send_buffers_common(msg, iov_out, tx_buf); while (packetsize <= KNET_MAX_PACKET_SIZE) { if (force_packet_size) { packetsize = force_packet_size; } for (i = 0; i < PCKT_FRAG_MAX; i++) { iov_out[i].iov_len = packetsize; if (use_pckt_verification) { struct pckt_ver *tx_pckt = (struct pckt_ver *)&iov_out[i].iov_base; tx_pckt->len = iov_out[i].iov_len; tx_pckt->chksum = compute_chksum((const unsigned char *)iov_out[i].iov_base + sizeof(struct pckt_ver), iov_out[i].iov_len - sizeof(struct pckt_ver)); } } total_pkts_to_tx = perf_by_size_size / packetsize; printf("[info]: testing with %u packet size. total bytes to transfer: %" PRIu64 " (%" PRIu64 " packets)\n", packetsize, perf_by_size_size, total_pkts_to_tx); memset(ctrl_message, 0, sizeof(ctrl_message)); knet_send(knet_h, ctrl_message, TEST_START, channel); while (total_pkts_to_tx > 0) { if (total_pkts_to_tx >= PCKT_FRAG_MAX) { packets_to_send = PCKT_FRAG_MAX; } else { packets_to_send = total_pkts_to_tx; } sent_msgs = send_messages(&msg[0], packets_to_send); if (sent_msgs < 0) { printf("Something went wrong, aborting\n"); exit(FAIL); } total_pkts_to_tx = total_pkts_to_tx - sent_msgs; } sleep(2); knet_send(knet_h, ctrl_message, TEST_STOP, channel); if ((packetsize == KNET_MAX_PACKET_SIZE) || (force_packet_size)) { break; } /* * Use a multiplier that can always divide properly a GB * into smaller chunks without worry about boundaries */ packetsize *= 4; if (packetsize > KNET_MAX_PACKET_SIZE) { packetsize = KNET_MAX_PACKET_SIZE; } } knet_send(knet_h, ctrl_message, TEST_COMPLETE, channel); for (i = 0; i < PCKT_FRAG_MAX; i++) { free(tx_buf[i]); } } /* For sorting the node list into order */ static int node_compare(const void *aptr, const void *bptr) { uint16_t a,b; a = *(uint16_t *)aptr; b = *(uint16_t *)bptr; return a > b; } static void display_stats(int level) { struct knet_handle_stats handle_stats; struct knet_link_status link_status; struct knet_link_stats total_link_stats; knet_node_id_t host_list[KNET_MAX_HOST]; uint8_t link_list[KNET_MAX_LINK]; unsigned int i,j; size_t num_hosts, num_links; if (knet_handle_get_stats(knet_h, &handle_stats, sizeof(handle_stats)) < 0) { perror("[info]: failed to get knet handle stats"); return; } if (compresscfg || cryptocfg) { printf("\n"); printf("[stat]: handle stats\n"); printf("[stat]: ------------\n"); if (compresscfg) { printf("[stat]: tx_uncompressed_packets: %" PRIu64 "\n", handle_stats.tx_uncompressed_packets); printf("[stat]: tx_compressed_packets: %" PRIu64 "\n", handle_stats.tx_compressed_packets); printf("[stat]: tx_compressed_original_bytes: %" PRIu64 "\n", handle_stats.tx_compressed_original_bytes); printf("[stat]: tx_compressed_size_bytes: %" PRIu64 "\n", handle_stats.tx_compressed_size_bytes ); printf("[stat]: tx_compress_time_ave: %" PRIu64 "\n", handle_stats.tx_compress_time_ave); printf("[stat]: tx_compress_time_min: %" PRIu64 "\n", handle_stats.tx_compress_time_min); printf("[stat]: tx_compress_time_max: %" PRIu64 "\n", handle_stats.tx_compress_time_max); printf("[stat]: tx_failed_to_compress: %" PRIu64 "\n", handle_stats.tx_failed_to_compress); printf("[stat]: tx_unable_to_compress: %" PRIu64 "\n", handle_stats.tx_unable_to_compress); printf("[stat]: rx_compressed_packets: %" PRIu64 "\n", handle_stats.rx_compressed_packets); printf("[stat]: rx_compressed_original_bytes: %" PRIu64 "\n", handle_stats.rx_compressed_original_bytes); printf("[stat]: rx_compressed_size_bytes: %" PRIu64 "\n", handle_stats.rx_compressed_size_bytes); printf("[stat]: rx_compress_time_ave: %" PRIu64 "\n", handle_stats.rx_compress_time_ave); printf("[stat]: rx_compress_time_min: %" PRIu64 "\n", handle_stats.rx_compress_time_min); printf("[stat]: rx_compress_time_max: %" PRIu64 "\n", handle_stats.rx_compress_time_max); printf("[stat]: rx_failed_to_decompress: %" PRIu64 "\n", handle_stats.rx_failed_to_decompress); printf("\n"); } if (cryptocfg) { printf("[stat]: tx_crypt_packets: %" PRIu64 "\n", handle_stats.tx_crypt_packets); printf("[stat]: tx_crypt_byte_overhead: %" PRIu64 "\n", handle_stats.tx_crypt_byte_overhead); printf("[stat]: tx_crypt_time_ave: %" PRIu64 "\n", handle_stats.tx_crypt_time_ave); printf("[stat]: tx_crypt_time_min: %" PRIu64 "\n", handle_stats.tx_crypt_time_min); printf("[stat]: tx_crypt_time_max: %" PRIu64 "\n", handle_stats.tx_crypt_time_max); printf("[stat]: rx_crypt_packets: %" PRIu64 "\n", handle_stats.rx_crypt_packets); printf("[stat]: rx_crypt_time_ave: %" PRIu64 "\n", handle_stats.rx_crypt_time_ave); printf("[stat]: rx_crypt_time_min: %" PRIu64 "\n", handle_stats.rx_crypt_time_min); printf("[stat]: rx_crypt_time_max: %" PRIu64 "\n", handle_stats.rx_crypt_time_max); printf("\n"); } } if (level < 2) { return; } memset(&total_link_stats, 0, sizeof(struct knet_link_stats)); if (knet_host_get_host_list(knet_h, host_list, &num_hosts) < 0) { perror("[info]: cannot get host list for stats"); return; } /* Print in host ID order */ qsort(host_list, num_hosts, sizeof(uint16_t), node_compare); for (j=0; j 2) { printf("\n"); printf("[stat]: Node %d Link %d\n", host_list[j], link_list[i]); printf("[stat]: tx_data_packets: %" PRIu64 "\n", link_status.stats.tx_data_packets); printf("[stat]: rx_data_packets: %" PRIu64 "\n", link_status.stats.rx_data_packets); printf("[stat]: tx_data_bytes: %" PRIu64 "\n", link_status.stats.tx_data_bytes); printf("[stat]: rx_data_bytes: %" PRIu64 "\n", link_status.stats.rx_data_bytes); printf("[stat]: rx_ping_packets: %" PRIu64 "\n", link_status.stats.rx_ping_packets); printf("[stat]: tx_ping_packets: %" PRIu64 "\n", link_status.stats.tx_ping_packets); printf("[stat]: rx_ping_bytes: %" PRIu64 "\n", link_status.stats.rx_ping_bytes); printf("[stat]: tx_ping_bytes: %" PRIu64 "\n", link_status.stats.tx_ping_bytes); printf("[stat]: rx_pong_packets: %" PRIu64 "\n", link_status.stats.rx_pong_packets); printf("[stat]: tx_pong_packets: %" PRIu64 "\n", link_status.stats.tx_pong_packets); printf("[stat]: rx_pong_bytes: %" PRIu64 "\n", link_status.stats.rx_pong_bytes); printf("[stat]: tx_pong_bytes: %" PRIu64 "\n", link_status.stats.tx_pong_bytes); printf("[stat]: rx_pmtu_packets: %" PRIu64 "\n", link_status.stats.rx_pmtu_packets); printf("[stat]: tx_pmtu_packets: %" PRIu64 "\n", link_status.stats.tx_pmtu_packets); printf("[stat]: rx_pmtu_bytes: %" PRIu64 "\n", link_status.stats.rx_pmtu_bytes); printf("[stat]: tx_pmtu_bytes: %" PRIu64 "\n", link_status.stats.tx_pmtu_bytes); printf("[stat]: tx_total_packets: %" PRIu64 "\n", link_status.stats.tx_total_packets); printf("[stat]: rx_total_packets: %" PRIu64 "\n", link_status.stats.rx_total_packets); printf("[stat]: tx_total_bytes: %" PRIu64 "\n", link_status.stats.tx_total_bytes); printf("[stat]: rx_total_bytes: %" PRIu64 "\n", link_status.stats.rx_total_bytes); printf("[stat]: tx_total_errors: %" PRIu64 "\n", link_status.stats.tx_total_errors); printf("[stat]: tx_total_retries: %" PRIu64 "\n", link_status.stats.tx_total_retries); printf("[stat]: tx_pmtu_errors: %" PRIu32 "\n", link_status.stats.tx_pmtu_errors); printf("[stat]: tx_pmtu_retries: %" PRIu32 "\n", link_status.stats.tx_pmtu_retries); printf("[stat]: tx_ping_errors: %" PRIu32 "\n", link_status.stats.tx_ping_errors); printf("[stat]: tx_ping_retries: %" PRIu32 "\n", link_status.stats.tx_ping_retries); printf("[stat]: tx_pong_errors: %" PRIu32 "\n", link_status.stats.tx_pong_errors); printf("[stat]: tx_pong_retries: %" PRIu32 "\n", link_status.stats.tx_pong_retries); printf("[stat]: tx_data_errors: %" PRIu32 "\n", link_status.stats.tx_data_errors); printf("[stat]: tx_data_retries: %" PRIu32 "\n", link_status.stats.tx_data_retries); printf("[stat]: latency_min: %" PRIu32 "\n", link_status.stats.latency_min); printf("[stat]: latency_max: %" PRIu32 "\n", link_status.stats.latency_max); printf("[stat]: latency_ave: %" PRIu32 "\n", link_status.stats.latency_ave); printf("[stat]: latency_samples: %" PRIu32 "\n", link_status.stats.latency_samples); printf("[stat]: down_count: %" PRIu32 "\n", link_status.stats.down_count); printf("[stat]: up_count: %" PRIu32 "\n", link_status.stats.up_count); } } } printf("\n"); printf("[stat]: Total link stats\n"); printf("[stat]: ----------------\n"); printf("[stat]: tx_data_packets: %" PRIu64 "\n", total_link_stats.tx_data_packets); printf("[stat]: rx_data_packets: %" PRIu64 "\n", total_link_stats.rx_data_packets); printf("[stat]: tx_data_bytes: %" PRIu64 "\n", total_link_stats.tx_data_bytes); printf("[stat]: rx_data_bytes: %" PRIu64 "\n", total_link_stats.rx_data_bytes); printf("[stat]: rx_ping_packets: %" PRIu64 "\n", total_link_stats.rx_ping_packets); printf("[stat]: tx_ping_packets: %" PRIu64 "\n", total_link_stats.tx_ping_packets); printf("[stat]: rx_ping_bytes: %" PRIu64 "\n", total_link_stats.rx_ping_bytes); printf("[stat]: tx_ping_bytes: %" PRIu64 "\n", total_link_stats.tx_ping_bytes); printf("[stat]: rx_pong_packets: %" PRIu64 "\n", total_link_stats.rx_pong_packets); printf("[stat]: tx_pong_packets: %" PRIu64 "\n", total_link_stats.tx_pong_packets); printf("[stat]: rx_pong_bytes: %" PRIu64 "\n", total_link_stats.rx_pong_bytes); printf("[stat]: tx_pong_bytes: %" PRIu64 "\n", total_link_stats.tx_pong_bytes); printf("[stat]: rx_pmtu_packets: %" PRIu64 "\n", total_link_stats.rx_pmtu_packets); printf("[stat]: tx_pmtu_packets: %" PRIu64 "\n", total_link_stats.tx_pmtu_packets); printf("[stat]: rx_pmtu_bytes: %" PRIu64 "\n", total_link_stats.rx_pmtu_bytes); printf("[stat]: tx_pmtu_bytes: %" PRIu64 "\n", total_link_stats.tx_pmtu_bytes); printf("[stat]: tx_total_packets: %" PRIu64 "\n", total_link_stats.tx_total_packets); printf("[stat]: rx_total_packets: %" PRIu64 "\n", total_link_stats.rx_total_packets); printf("[stat]: tx_total_bytes: %" PRIu64 "\n", total_link_stats.tx_total_bytes); printf("[stat]: rx_total_bytes: %" PRIu64 "\n", total_link_stats.rx_total_bytes); printf("[stat]: tx_total_errors: %" PRIu64 "\n", total_link_stats.tx_total_errors); printf("[stat]: tx_total_retries: %" PRIu64 "\n", total_link_stats.tx_total_retries); printf("[stat]: tx_pmtu_errors: %" PRIu32 "\n", total_link_stats.tx_pmtu_errors); printf("[stat]: tx_pmtu_retries: %" PRIu32 "\n", total_link_stats.tx_pmtu_retries); printf("[stat]: tx_ping_errors: %" PRIu32 "\n", total_link_stats.tx_ping_errors); printf("[stat]: tx_ping_retries: %" PRIu32 "\n", total_link_stats.tx_ping_retries); printf("[stat]: tx_pong_errors: %" PRIu32 "\n", total_link_stats.tx_pong_errors); printf("[stat]: tx_pong_retries: %" PRIu32 "\n", total_link_stats.tx_pong_retries); printf("[stat]: tx_data_errors: %" PRIu32 "\n", total_link_stats.tx_data_errors); printf("[stat]: tx_data_retries: %" PRIu32 "\n", total_link_stats.tx_data_retries); printf("[stat]: down_count: %" PRIu32 "\n", total_link_stats.down_count); printf("[stat]: up_count: %" PRIu32 "\n", total_link_stats.up_count); } static void send_perf_data_by_time(void) { char *tx_buf[PCKT_FRAG_MAX]; struct knet_mmsghdr msg[PCKT_FRAG_MAX]; struct iovec iov_out[PCKT_FRAG_MAX]; char ctrl_message[16]; int sent_msgs; int i; uint32_t packetsize = 64; struct timespec clock_start, clock_end; unsigned long long time_diff = 0; setup_send_buffers_common(msg, iov_out, tx_buf); memset(&clock_start, 0, sizeof(clock_start)); memset(&clock_end, 0, sizeof(clock_start)); while (packetsize <= KNET_MAX_PACKET_SIZE) { if (force_packet_size) { packetsize = force_packet_size; } for (i = 0; i < PCKT_FRAG_MAX; i++) { iov_out[i].iov_len = packetsize; if (use_pckt_verification) { struct pckt_ver *tx_pckt = (struct pckt_ver *)iov_out[i].iov_base; tx_pckt->len = iov_out[i].iov_len; tx_pckt->chksum = compute_chksum((const unsigned char *)iov_out[i].iov_base + sizeof(struct pckt_ver), iov_out[i].iov_len - sizeof(struct pckt_ver)); } } printf("[info]: testing with %u bytes packet size for %" PRIu64 " seconds.\n", packetsize, perf_by_time_secs); memset(ctrl_message, 0, sizeof(ctrl_message)); knet_send(knet_h, ctrl_message, TEST_START, channel); if (clock_gettime(CLOCK_MONOTONIC, &clock_start) != 0) { printf("[info]: unable to get start time!\n"); } time_diff = 0; while (time_diff < (perf_by_time_secs * 1000000000llu)) { sent_msgs = send_messages(&msg[0], PCKT_FRAG_MAX); if (sent_msgs < 0) { printf("Something went wrong, aborting\n"); exit(FAIL); } if (clock_gettime(CLOCK_MONOTONIC, &clock_end) != 0) { printf("[info]: unable to get end time!\n"); } timespec_diff(clock_start, clock_end, &time_diff); } sleep(2); knet_send(knet_h, ctrl_message, TEST_STOP, channel); if ((packetsize == KNET_MAX_PACKET_SIZE) || (force_packet_size)) { break; } /* * Use a multiplier that can always divide properly a GB * into smaller chunks without worry about boundaries */ packetsize *= 4; if (packetsize > KNET_MAX_PACKET_SIZE) { packetsize = KNET_MAX_PACKET_SIZE; } } knet_send(knet_h, ctrl_message, TEST_COMPLETE, channel); for (i = 0; i < PCKT_FRAG_MAX; i++) { free(tx_buf[i]); } } static void cleanup_all(void) { knet_handle_t knet_h_tmp[2]; if (pthread_mutex_lock(&shutdown_mutex)) { return; } if (bench_shutdown_in_progress) { pthread_mutex_unlock(&shutdown_mutex); return; } bench_shutdown_in_progress = 1; pthread_mutex_unlock(&shutdown_mutex); if (rx_thread) { stop_rx_thread(); } knet_h_tmp[1] = knet_h; knet_handle_stop_everything(knet_h_tmp, 1); } static void sigint_handler(int signum) { printf("[info]: cleaning up... got signal: %d\n", signum); cleanup_all(); exit(PASS); } int main(int argc, char *argv[]) { if (signal(SIGINT, sigint_handler) == SIG_ERR) { printf("Unable to configure SIGINT handler\n"); exit(FAIL); } setup_knet(argc, argv); setup_data_txrx_common(); sleep(5); restart: switch(test_type) { default: case TEST_PING: /* basic ping, no data */ sleep(5); break; case TEST_PING_AND_DATA: send_ping_data(); break; case TEST_PERF_BY_SIZE: if (senderid == thisnodeid) { send_perf_data_by_size(); } else { printf("[info]: waiting for perf rx thread to finish\n"); while(!wait_for_perf_rx) { sleep(1); } } break; case TEST_PERF_BY_TIME: if (senderid == thisnodeid) { send_perf_data_by_time(); } else { printf("[info]: waiting for perf rx thread to finish\n"); while(!wait_for_perf_rx) { sleep(1); } } break; } if (continous) { goto restart; } if (show_stats) { display_stats(show_stats); } cleanup_all(); return PASS; } diff --git a/libknet/tests/pckt_test.c b/libknet/tests/pckt_test.c index 8bc0c468..acfb37ff 100644 --- a/libknet/tests/pckt_test.c +++ b/libknet/tests/pckt_test.c @@ -1,23 +1,23 @@ /* - * Copyright (C) 2015-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2015-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include #include "onwire.h" int main(void) { printf("\nKronosnet network header size printout:\n\n"); printf("KNET_HEADER_ALL_SIZE: %zu\n", KNET_HEADER_ALL_SIZE); printf("KNET_HEADER_SIZE: %zu\n", KNET_HEADER_SIZE); printf("KNET_HEADER_PING_V1_SIZE: %zu (%zu)\n", KNET_HEADER_PING_V1_SIZE, sizeof(struct knet_header_payload_ping_v1)); printf("KNET_HEADER_PMTUD_V1_SIZE: %zu (%zu)\n", KNET_HEADER_PMTUD_V1_SIZE, sizeof(struct knet_header_payload_pmtud_v1)); printf("KNET_HEADER_DATA_V1_SIZE: %zu (%zu)\n", KNET_HEADER_DATA_V1_SIZE, sizeof(struct knet_header_payload_data_v1)); return 0; } diff --git a/libknet/tests/test-common.c b/libknet/tests/test-common.c index 6f77a798..124dcaa8 100644 --- a/libknet/tests/test-common.c +++ b/libknet/tests/test-common.c @@ -1,1043 +1,1043 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include "libknet.h" #include "test-common.h" static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER; static int log_init = 0; static pthread_mutex_t log_thread_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_t log_thread; static int log_thread_init = 0; static int log_fds[2]; struct log_thread_data { int logfd; FILE *std; }; static struct log_thread_data data; static char plugin_path[PATH_MAX]; static int _read_pipe(int fd, char **file, size_t *length) { char buf[4096]; int n; int done = 0; *file = NULL; *length = 0; memset(buf, 0, sizeof(buf)); while (!done) { n = read(fd, buf, sizeof(buf)); if (n < 0) { if (errno == EINTR) continue; if (*file) free(*file); return n; } if (n == 0 && (!*length)) return 0; if (n == 0) done = 1; if (*file) *file = realloc(*file, (*length) + n + done); else *file = malloc(n + done); if (!*file) return -1; memmove((*file) + (*length), buf, n); *length += (done + n); } /* Null terminator */ (*file)[(*length) - 1] = 0; return 0; } int execute_shell(const char *command, char **error_string) { pid_t pid; int status, err = 0; int fd[2]; size_t size = 0; if ((command == NULL) || (!error_string)) { errno = EINVAL; return FAIL; } *error_string = NULL; err = pipe(fd); if (err) goto out_clean; pid = fork(); if (pid < 0) { err = pid; goto out_clean; } if (pid) { /* parent */ close(fd[1]); err = _read_pipe(fd[0], error_string, &size); if (err) goto out_clean0; waitpid(pid, &status, 0); if (!WIFEXITED(status)) { err = -1; goto out_clean0; } if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { err = WEXITSTATUS(status); goto out_clean0; } goto out_clean0; } else { /* child */ close(0); close(1); close(2); close(fd[0]); dup2(fd[1], 1); dup2(fd[1], 2); close(fd[1]); execlp("/bin/sh", "/bin/sh", "-c", command, NULL); exit(FAIL); } out_clean: close(fd[1]); out_clean0: close(fd[0]); return err; } int is_memcheck(void) { char *val; val = getenv("KNETMEMCHECK"); if (val) { if (!strncmp(val, "yes", 3)) { return 1; } } return 0; } int is_helgrind(void) { char *val; val = getenv("KNETHELGRIND"); if (val) { if (!strncmp(val, "yes", 3)) { return 1; } } return 0; } void set_scheduler(int policy) { struct sched_param sched_param; int err; err = sched_get_priority_max(policy); if (err < 0) { printf("Could not get maximum scheduler priority\n"); exit(FAIL); } sched_param.sched_priority = err; err = sched_setscheduler(0, policy, &sched_param); if (err < 0) { printf("Could not set priority\n"); exit(FAIL); } return; } int setup_logpipes(int *logfds) { if (pipe2(logfds, O_CLOEXEC | O_NONBLOCK) < 0) { printf("Unable to setup logging pipe\n"); exit(FAIL); } return PASS; } void close_logpipes(int *logfds) { close(logfds[0]); logfds[0] = 0; close(logfds[1]); logfds[1] = 0; } void flush_logs(int logfd, FILE *std) { struct knet_log_msg msg; int len; while (1) { len = read(logfd, &msg, sizeof(msg)); if (len != sizeof(msg)) { /* * clear errno to avoid incorrect propagation */ errno = 0; return; } if (!msg.knet_h) { /* * this is harsh but this function is void * and it is used also inside log_thread. * this is the easiest to get out with an error */ fprintf(std, "NO HANDLE INFO IN LOG MSG!!\n"); abort(); } msg.msg[sizeof(msg.msg) - 1] = 0; fprintf(std, "[knet: %p]: [%s] %s: %.*s\n", msg.knet_h, knet_log_get_loglevel_name(msg.msglevel), knet_log_get_subsystem_name(msg.subsystem), KNET_MAX_LOG_MSG_SIZE, msg.msg); } } static void *_logthread(void *args) { while (1) { int num; struct timeval tv = { 60, 0 }; fd_set rfds; FD_ZERO(&rfds); FD_SET(data.logfd, &rfds); num = select(FD_SETSIZE, &rfds, NULL, NULL, &tv); if (num < 0) { fprintf(data.std, "Unable select over logfd!\nHALTING LOGTHREAD!\n"); return NULL; } if (num == 0) { fprintf(data.std, "[knet]: No logs in the last 60 seconds\n"); continue; } if (FD_ISSET(data.logfd, &rfds)) { flush_logs(data.logfd, data.std); } } } int start_logthread(int logfd, FILE *std) { int savederrno = 0; savederrno = pthread_mutex_lock(&log_thread_mutex); if (savederrno) { printf("Unable to get log_thread mutex lock\n"); return -1; } if (!log_thread_init) { data.logfd = logfd; data.std = std; savederrno = pthread_create(&log_thread, 0, _logthread, NULL); if (savederrno) { printf("Unable to start logging thread: %s\n", strerror(savederrno)); pthread_mutex_unlock(&log_thread_mutex); return -1; } log_thread_init = 1; } pthread_mutex_unlock(&log_thread_mutex); return 0; } int stop_logthread(void) { int savederrno = 0; void *retval; savederrno = pthread_mutex_lock(&log_thread_mutex); if (savederrno) { printf("Unable to get log_thread mutex lock\n"); return -1; } if (log_thread_init) { pthread_cancel(log_thread); pthread_join(log_thread, &retval); log_thread_init = 0; } pthread_mutex_unlock(&log_thread_mutex); return 0; } static void stop_logging(void) { stop_logthread(); flush_logs(log_fds[0], stdout); close_logpipes(log_fds); } int start_logging(FILE *std) { int savederrno = 0; savederrno = pthread_mutex_lock(&log_mutex); if (savederrno) { printf("Unable to get log_mutex lock\n"); return -1; } if (!log_init) { setup_logpipes(log_fds); if (atexit(&stop_logging) != 0) { printf("Unable to register atexit handler to stop logging: %s\n", strerror(errno)); exit(FAIL); } if (start_logthread(log_fds[0], std) < 0) { exit(FAIL); } log_init = 1; } pthread_mutex_unlock(&log_mutex); return log_fds[1]; } static int dir_filter(const struct dirent *dname) { if ( (strcmp(dname->d_name + strlen(dname->d_name)-3, ".so") == 0) && ((strncmp(dname->d_name,"crypto", 6) == 0) || (strncmp(dname->d_name,"compress", 8) == 0))) { return 1; } return 0; } /* Make sure the proposed plugin path has at least 1 of each plugin available - just as a sanity check really */ static int contains_plugins(char *path) { struct dirent **namelist; int n,i; size_t j; struct knet_compress_info compress_list[256]; struct knet_crypto_info crypto_list[256]; size_t num_compress, num_crypto; size_t compress_found = 0; size_t crypto_found = 0; if (knet_get_compress_list(compress_list, &num_compress) == -1) { return 0; } if (knet_get_crypto_list(crypto_list, &num_crypto) == -1) { return 0; } n = scandir(path, &namelist, dir_filter, alphasort); if (n == -1) { return 0; } /* Look for plugins in the list */ for (i=0; id_name) >= 7 && strncmp(crypto_list[j].name, namelist[i]->d_name+7, strlen(crypto_list[j].name)) == 0) { crypto_found++; } } for (j=0; jd_name) >= 9 && strncmp(compress_list[j].name, namelist[i]->d_name+9, strlen(compress_list[j].name)) == 0) { compress_found++; } } free(namelist[i]); } free(namelist); /* If at least one plugin was found (or none were built) */ if ((crypto_found || num_crypto == 0) && (compress_found || num_compress == 0)) { return 1; } else { return 0; } } /* libtool sets LD_LIBRARY_PATH to the build tree when running test in-tree */ char *find_plugins_path(void) { char *ld_libs_env = getenv("LD_LIBRARY_PATH"); if (ld_libs_env) { char *ld_libs = strdup(ld_libs_env); char *str = strtok(ld_libs, ":"); while (str) { if (contains_plugins(str)) { strncpy(plugin_path, str, sizeof(plugin_path)-1); free(ld_libs); printf("Using plugins from %s\n", plugin_path); return plugin_path; } str = strtok(NULL, ":"); } free(ld_libs); } return NULL; } knet_handle_t knet_handle_start(int logfds[2], uint8_t log_level, knet_handle_t knet_h_array[]) { knet_handle_t knet_h = knet_handle_new(1, logfds[1], log_level, 0); char *plugins_path; if (knet_h) { printf("knet_handle_new at %p\n", knet_h); plugins_path = find_plugins_path(); /* Use plugins from the build tree */ if (plugins_path) { knet_h->plugin_path = plugins_path; } knet_h_array[1] = knet_h; flush_logs(logfds[0], stdout); return knet_h; } else { printf("knet_handle_new failed: %s\n", strerror(errno)); flush_logs(logfds[0], stdout); close_logpipes(logfds); exit(FAIL); } } int knet_handle_reconnect_links(knet_handle_t knet_h) { size_t i, j; knet_node_id_t host_ids[KNET_MAX_HOST]; uint8_t link_ids[KNET_MAX_LINK]; size_t host_ids_entries = 0, link_ids_entries = 0; unsigned int enabled; if (!knet_h) { errno = EINVAL; return -1; } if (knet_host_get_host_list(knet_h, host_ids, &host_ids_entries) < 0) { printf("knet_host_get_host_list failed: %s\n", strerror(errno)); return -1; } for (i = 0; i < host_ids_entries; i++) { if (knet_link_get_link_list(knet_h, host_ids[i], link_ids, &link_ids_entries)) { printf("knet_link_get_link_list failed: %s\n", strerror(errno)); return -1; } for (j = 0; j < link_ids_entries; j++) { if (knet_link_get_enable(knet_h, host_ids[i], link_ids[j], &enabled)) { printf("knet_link_get_enable failed: %s\n", strerror(errno)); return -1; } if (!enabled) { if (knet_link_set_enable(knet_h, host_ids[i], j, 1)) { printf("knet_link_set_enable failed: %s\n", strerror(errno)); return -1; } } } } return 0; } int knet_handle_disconnect_links(knet_handle_t knet_h) { size_t i, j; knet_node_id_t host_ids[KNET_MAX_HOST]; uint8_t link_ids[KNET_MAX_LINK]; size_t host_ids_entries = 0, link_ids_entries = 0; unsigned int enabled; if (!knet_h) { errno = EINVAL; return -1; } if (knet_host_get_host_list(knet_h, host_ids, &host_ids_entries) < 0) { printf("knet_host_get_host_list failed: %s\n", strerror(errno)); return -1; } for (i = 0; i < host_ids_entries; i++) { if (knet_link_get_link_list(knet_h, host_ids[i], link_ids, &link_ids_entries)) { printf("knet_link_get_link_list failed: %s\n", strerror(errno)); return -1; } for (j = 0; j < link_ids_entries; j++) { if (knet_link_get_enable(knet_h, host_ids[i], link_ids[j], &enabled)) { printf("knet_link_get_enable failed: %s\n", strerror(errno)); return -1; } if (enabled) { if (knet_link_set_enable(knet_h, host_ids[i], j, 0)) { printf("knet_link_set_enable failed: %s\n", strerror(errno)); return -1; } } } } return 0; } static int _make_local_sockaddr(struct sockaddr_storage *lo, int offset, int family) { in_port_t port; char portstr[32]; if (offset < 0) { /* * api_knet_link_set_config needs to access the API directly, but * it does not send any traffic, so it´s safe to ask the kernel * for a random port. */ port = 0; } else { /* Use the pid if we can. but makes sure its in a sensible range */ port = (getpid() + offset) % (65536-1024) + 1024; } sprintf(portstr, "%u", port); memset(lo, 0, sizeof(struct sockaddr_storage)); printf("Using port %u\n", port); if (family == AF_INET6) { return knet_strtoaddr("::1", portstr, lo, sizeof(struct sockaddr_storage)); } return knet_strtoaddr("127.0.0.1", portstr, lo, sizeof(struct sockaddr_storage)); } int make_local_sockaddr(struct sockaddr_storage *lo, int offset) { return _make_local_sockaddr(lo, offset, AF_INET); } int make_local_sockaddr6(struct sockaddr_storage *lo, int offset) { return _make_local_sockaddr(lo, offset, AF_INET6); } int _knet_link_set_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t transport, uint64_t flags, int family, int dynamic, struct sockaddr_storage *lo) { int err = 0, savederrno = 0; uint32_t port; char portstr[32]; for (port = 1025; port < 65536; port++) { sprintf(portstr, "%u", port); memset(lo, 0, sizeof(struct sockaddr_storage)); if (family == AF_INET6) { err = knet_strtoaddr("::1", portstr, lo, sizeof(struct sockaddr_storage)); } else { err = knet_strtoaddr("127.0.0.1", portstr, lo, sizeof(struct sockaddr_storage)); } if (err < 0) { printf("Unable to convert loopback to sockaddr: %s\n", strerror(errno)); goto out; } errno = 0; if (dynamic) { err = knet_link_set_config(knet_h, host_id, link_id, transport, lo, NULL, flags); } else { err = knet_link_set_config(knet_h, host_id, link_id, transport, lo, lo, flags); } savederrno = errno; if ((err < 0) && (savederrno != EADDRINUSE)) { printf("Unable to configure link: %s\n", strerror(savederrno)); goto out; } if (!err) { printf("Using port %u\n", port); goto out; } } if (err) { printf("No more ports available\n"); } out: errno = savederrno; return err; } void test_sleep(knet_handle_t knet_h, int seconds) { if (is_memcheck() || is_helgrind()) { printf("Test suite is running under valgrind, adjusting sleep timers\n"); seconds = seconds * 16; } sleep(seconds); } int wait_for_packet(knet_handle_t knet_h, int seconds, int datafd, int logfd, FILE *std) { fd_set rfds; struct timeval tv; int err = 0, i = 0; if (is_memcheck() || is_helgrind()) { printf("Test suite is running under valgrind, adjusting wait_for_packet timeout\n"); seconds = seconds * 16; } try_again: FD_ZERO(&rfds); FD_SET(datafd, &rfds); tv.tv_sec = 1; tv.tv_usec = 0; err = select(datafd+1, &rfds, NULL, NULL, &tv); /* * on slow arches the first call to select can return 0. * pick an arbitrary 10 times loop (multiplied by waiting seconds) * before failing. */ if ((!err) && (i < seconds)) { flush_logs(logfd, std); i++; goto try_again; } if ((err > 0) && (FD_ISSET(datafd, &rfds))) { return 0; } errno = ETIMEDOUT; return -1; } /* * functional tests helpers */ void knet_handle_start_nodes(knet_handle_t knet_h[], uint8_t numnodes, int logfds[2], uint8_t log_level) { uint8_t i; char *plugins_path = find_plugins_path(); for (i = 1; i <= numnodes; i++) { knet_h[i] = knet_handle_new(i, logfds[1], log_level, 0); if (!knet_h[i]) { printf("failed to create handle: %s\n", strerror(errno)); break; } else { printf("knet_h[%u] at %p\n", i, knet_h[i]); } /* Use plugins from the build tree */ if (plugins_path) { knet_h[i]->plugin_path = plugins_path; } } if (i < numnodes) { knet_handle_stop_everything(knet_h, i); exit(FAIL); } return; } void knet_handle_join_nodes(knet_handle_t knet_h[], uint8_t numnodes, uint8_t numlinks, int family, uint8_t transport) { uint8_t i, x, j; struct sockaddr_storage src, dst; int offset = 0; int res; for (i = 1; i <= numnodes; i++) { for (j = 1; j <= numnodes; j++) { /* * don´t connect to itself */ if (j == i) { continue; } printf("host %u adding host: %u\n", i, j); if (knet_host_add(knet_h[i], j) < 0) { printf("Unable to add host: %s\n", strerror(errno)); knet_handle_stop_everything(knet_h, numnodes); exit(FAIL); } for (x = 0; x < numlinks; x++) { res = -1; offset = 0; while (i + x + offset++ < 65535 && res != 0) { if (_make_local_sockaddr(&src, i + x + offset, family) < 0) { printf("Unable to convert src to sockaddr: %s\n", strerror(errno)); knet_handle_stop_everything(knet_h, numnodes); exit(FAIL); } if (_make_local_sockaddr(&dst, j + x + offset, family) < 0) { printf("Unable to convert dst to sockaddr: %s\n", strerror(errno)); knet_handle_stop_everything(knet_h, numnodes); exit(FAIL); } res = knet_link_set_config(knet_h[i], j, x, transport, &src, &dst, 0); } printf("joining node %u with node %u via link %u src offset: %u dst offset: %u\n", i, j, x, i+x, j+x); if (knet_link_set_enable(knet_h[i], j, x, 1) < 0) { printf("unable to enable link: %s\n", strerror(errno)); knet_handle_stop_everything(knet_h, numnodes); exit(FAIL); } } } } for (i = 1; i <= numnodes; i++) { wait_for_nodes_state(knet_h[i], numnodes, 1, 600, knet_h[1]->logfd, stdout); } return; } static int target=0; static int state_wait_pipe[2] = {0,0}; static int host_wait_pipe[2] = {0,0}; static int count_nodes(knet_handle_t knet_h) { int nodes = 0; int i; for (i=0; i< KNET_MAX_HOST; i++) { if (knet_h->host_index[i] && knet_h->host_index[i]->status.reachable == 1) { nodes++; } } return nodes; } static void nodes_notify_callback(void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external) { knet_handle_t knet_h = (knet_handle_t) private_data; int nodes; int res; nodes = count_nodes(knet_h); if (nodes == target) { res = write(state_wait_pipe[1], ".", 1); if (res != 1) { printf("***FAILed to signal wait_for_nodes_state: %s\n", strerror(errno)); } } } /* Called atexit() */ static void finish_state_pipes() { if (state_wait_pipe[0] != 0) { close(state_wait_pipe[0]); close(state_wait_pipe[1]); state_wait_pipe[0] = 0; } if (host_wait_pipe[0] != 0) { close(host_wait_pipe[0]); close(host_wait_pipe[1]); host_wait_pipe[0] = 0; } } static void host_notify_callback(void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external) { knet_handle_t knet_h = (knet_handle_t) private_data; int res; if (knet_h->host_index[host_id]->status.reachable == 1) { res = write(host_wait_pipe[1], ".", 1); if (res != 1) { printf("***FAILed to signal wait_for_host: %s\n", strerror(errno)); } } } static int wait_for_reply(int seconds, int pipefd) { int res; struct pollfd pfds; char tmpbuf[32]; pfds.fd = pipefd; pfds.events = POLLIN | POLLERR | POLLHUP; pfds.revents = 0; res = poll(&pfds, 1, seconds*1000); if (res == 1) { if (pfds.revents & POLLIN) { res = read(pipefd, tmpbuf, sizeof(tmpbuf)); if (res > 0) { return 0; } } else { printf("Error on pipe poll revent = 0x%x\n", pfds.revents); errno = EIO; } } if (res == 0) { errno = ETIMEDOUT; return -1; } return -1; } /* Wait for a cluster of 'numnodes' to come up/go down */ int wait_for_nodes_state(knet_handle_t knet_h, size_t numnodes, uint8_t state, uint32_t timeout, int logfd, FILE *std) { int res, savederrno = 0; if (state_wait_pipe[0] == 0) { res = pipe(state_wait_pipe); if (res == -1) { savederrno = errno; printf("Error creating host reply pipe: %s\n", strerror(errno)); errno = savederrno; return -1; } if (atexit(finish_state_pipes)) { printf("Unable to register atexit handler to close pipes: %s\n", strerror(errno)); exit(FAIL); } } if (state) { target = numnodes-1; /* exclude us */ } else { target = 0; /* Wait for all to go down */ } /* Set this before checking existing status or there's a race condition */ knet_host_enable_status_change_notify(knet_h, (void *)(long)knet_h, nodes_notify_callback); /* Check we haven't already got all the nodes in the correct state */ if (count_nodes(knet_h) == target) { fprintf(stderr, "target already reached\n"); knet_host_enable_status_change_notify(knet_h, (void *)(long)0, NULL); flush_logs(logfd, std); return 0; } res = wait_for_reply(timeout, state_wait_pipe[0]); if (res == -1) { savederrno = errno; printf("Error waiting for nodes status reply: %s\n", strerror(errno)); } knet_host_enable_status_change_notify(knet_h, (void *)(long)0, NULL); flush_logs(logfd, std); errno = savederrno; return res; } /* Wait for a single node to come up */ int wait_for_host(knet_handle_t knet_h, uint16_t host_id, int seconds, int logfd, FILE *std) { int res = 0; int savederrno = 0; if (is_memcheck() || is_helgrind()) { printf("Test suite is running under valgrind, adjusting wait_for_host timeout\n"); seconds = seconds * 16; } if (host_wait_pipe[0] == 0) { res = pipe(host_wait_pipe); if (res == -1) { savederrno = errno; printf("Error creating host reply pipe: %s\n", strerror(errno)); errno = savederrno; return -1; } if (atexit(finish_state_pipes)) { printf("Unable to register atexit handler to close pipes: %s\n", strerror(errno)); exit(FAIL); } } /* Set this before checking existing status or there's a race condition */ knet_host_enable_status_change_notify(knet_h, (void *)(long)knet_h, host_notify_callback); /* Check it's not already reachable */ if (knet_h->host_index[host_id]->status.reachable == 1) { knet_host_enable_status_change_notify(knet_h, (void *)(long)0, NULL); flush_logs(logfd, std); return 0; } res = wait_for_reply(seconds, host_wait_pipe[0]); if (res == -1) { savederrno = errno; printf("Error waiting for host status reply: %s\n", strerror(errno)); } knet_host_enable_status_change_notify(knet_h, (void *)(long)0, NULL); /* Still wait for it to settle */ flush_logs(logfd, std); test_sleep(knet_h, 1); errno = savederrno; return res; } void clean_exit(knet_handle_t *knet_h, int testnodes, int *logfds, int exit_status) { knet_handle_stop_everything(knet_h, testnodes); stop_logthread(); flush_logs(logfds[0], stdout); close_logpipes(logfds); if (exit_status != CONTINUE) { exit(exit_status); } } /* Shutdown all nodes and links attached to an array of knet handles. * Mostly stolen from corosync code (that I wrote, before anyone complains about licences) */ void knet_handle_stop_everything(knet_handle_t knet_h[], uint8_t numnodes) { int res = 0; int h; size_t i,j; static knet_node_id_t nodes[KNET_MAX_HOST]; /* static to save stack */ uint8_t links[KNET_MAX_LINK]; size_t num_nodes; size_t num_links; for (h=1; h * * This software licensed under GPL-2.0+ */ #ifndef __KNET_TEST_COMMON_H__ #define __KNET_TEST_COMMON_H__ #include "internals.h" #include /* * error codes from automake test-driver */ #define PASS 0 #define SKIP 77 #define ERROR 99 #define FAIL -1 /* Extra for us to continue while still using the cleanup code */ #define CONTINUE 101 /* For *BSD compatibility */ #ifndef s6_addr16 #define s6_addr8 __u6_addr.__u6_addr8 #define s6_addr16 __u6_addr.__u6_addr16 #define s6_addr32 __u6_addr.__u6_addr32 #endif /* * common facilities */ #define TESTNODES 1 #define FAIL_ON_ERR(fn) \ printf("FOE: %s\n", #fn); \ if ((res = fn) != 0) { \ int savederrno = errno; \ knet_handle_stop_everything(knet_h, TESTNODES); \ stop_logthread(); \ flush_logs(logfds[0], stdout); \ close_logpipes(logfds); \ if (res == -2) { \ exit(SKIP); \ } else { \ printf("*** FAIL on line %d. %s failed: %s\n", __LINE__ , #fn, strerror(savederrno)); \ exit(FAIL); \ } \ } else { \ flush_logs(logfds[0], stdout); \ } /* As above but allow a SKIP to continue */ #define FAIL_ON_ERR_ONLY(fn) \ printf("FOEO: %s\n", #fn); \ if ((res = fn) == -1) { \ int savederrno = errno; \ knet_handle_stop_everything(knet_h, TESTNODES); \ stop_logthread(); \ flush_logs(logfds[0], stdout); \ close_logpipes(logfds); \ printf("*** FAIL on line %d. %s failed: %s\n", __LINE__ , #fn, strerror(savederrno)); \ exit(FAIL); \ } else { \ flush_logs(logfds[0], stdout); \ } /* Voted "Best macro name of 2022" */ #define FAIL_ON_SUCCESS(fn, errcode) \ printf("FOS: %s\n", #fn); \ if (((res = fn) == 0) || \ ((res == -1) && (errno != errcode))) { \ int savederrno = errno; \ knet_handle_stop_everything(knet_h, TESTNODES); \ stop_logthread(); \ flush_logs(logfds[0], stdout); \ close_logpipes(logfds); \ if (res == -2) { \ exit(SKIP); \ } else { \ printf("*** FAIL on line %d. %s did not return correct error: %s\n", __LINE__ , #fn, strerror(savederrno)); \ exit(FAIL); \ } \ } else { \ flush_logs(logfds[0], stdout); \ } #define CLEAN_EXIT(r) \ clean_exit(knet_h, TESTNODES, logfds, r) void clean_exit(knet_handle_t *knet_h, int testnodes, int *logfds, int exit_status); int execute_shell(const char *command, char **error_string); int is_memcheck(void); int is_helgrind(void); void set_scheduler(int policy); knet_handle_t knet_handle_start(int logfds[2], uint8_t log_level, knet_handle_t knet_h_array[]); /* * knet_link_set_config wrapper required to find a free port */ int _knet_link_set_config(knet_handle_t knet_h, knet_node_id_t host_id, uint8_t link_id, uint8_t transport, uint64_t flags, int family, int dynamic, struct sockaddr_storage *lo); /* * functional test helpers */ void knet_handle_stop_everything(knet_handle_t knet_h[], uint8_t numnodes); void knet_handle_start_nodes(knet_handle_t knet_h[], uint8_t numnodes, int logfds[2], uint8_t log_level); void knet_handle_join_nodes(knet_handle_t knet_h[], uint8_t numnodes, uint8_t numlinks, int family, uint8_t transport); int knet_handle_disconnect_links(knet_handle_t knet_h); int knet_handle_reconnect_links(knet_handle_t knet_h); /* * high level logging function. * automatically setup logpipes and start/stop logging thread. * * start_logging exit(FAIL) on error or fd to pass to knet_handle_new * and it will install an atexit handle to close logging properly * * WARNING: DO NOT use start_logging for api_ or int_ testing. * while start_logging would work just fine, the output * of the logs is more complex to read because of the way * the thread would interleave the output of printf from api_/int_ testing * with knet logs. Functionally speaking you get the exact same logs, * but a lot harder to read due to the thread latency in printing logs. */ int start_logging(FILE *std); int setup_logpipes(int *logfds); void close_logpipes(int *logfds); void flush_logs(int logfd, FILE *std); int start_logthread(int logfd, FILE *std); int stop_logthread(void); int make_local_sockaddr(struct sockaddr_storage *lo, int offset); int make_local_sockaddr6(struct sockaddr_storage *lo, int offset); int wait_for_host(knet_handle_t knet_h, uint16_t host_id, int seconds, int logfd, FILE *std); int wait_for_packet(knet_handle_t knet_h, int seconds, int datafd, int logfd, FILE *std); void test_sleep(knet_handle_t knet_h, int seconds); char *find_plugins_path(void); int wait_for_nodes_state(knet_handle_t knet_h, size_t numnodes, uint8_t state, uint32_t timeout, int logfd, FILE *std); #endif diff --git a/libknet/threads_common.c b/libknet/threads_common.c index f026bf5f..bde67767 100644 --- a/libknet/threads_common.c +++ b/libknet/threads_common.c @@ -1,353 +1,353 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include "internals.h" #include "logging.h" #include "threads_common.h" int shutdown_in_progress(knet_handle_t knet_h) { int savederrno = 0; int ret; savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_COMMON, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } ret = knet_h->fini_in_progress; pthread_rwlock_unlock(&knet_h->global_rwlock); return ret; } static int _pmtud_reschedule(knet_handle_t knet_h) { if (knet_h->pmtud_running) { knet_h->pmtud_abort = 1; if (knet_h->pmtud_waiting) { pthread_cond_signal(&knet_h->pmtud_cond); } } return 0; } static int pmtud_reschedule(knet_handle_t knet_h) { int res; if (pthread_mutex_lock(&knet_h->pmtud_mutex) != 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock"); return -1; } res = _pmtud_reschedule(knet_h); pthread_mutex_unlock(&knet_h->pmtud_mutex); return res; } int get_global_wrlock(knet_handle_t knet_h) { if (pmtud_reschedule(knet_h) < 0) { log_info(knet_h, KNET_SUB_PMTUD, "Unable to notify PMTUd to reschedule. Expect delays in executing API calls"); } return pthread_rwlock_wrlock(&knet_h->global_rwlock); } static struct pretty_names thread_names[KNET_THREAD_MAX] = { { "TX", KNET_THREAD_TX }, { "RX", KNET_THREAD_RX }, { "HB", KNET_THREAD_HB }, { "PMTUD", KNET_THREAD_PMTUD }, #ifdef HAVE_NETINET_SCTP_H { "SCTP_LISTEN", KNET_THREAD_SCTP_LISTEN }, { "SCTP_CONN", KNET_THREAD_SCTP_CONN }, #endif { "DST_LINK", KNET_THREAD_DST_LINK } }; static struct pretty_names thread_status[] = { { "unregistered", KNET_THREAD_UNREGISTERED }, { "registered", KNET_THREAD_REGISTERED }, { "started", KNET_THREAD_STARTED }, { "stopped", KNET_THREAD_STOPPED } }; static const char *get_thread_status_name(uint8_t status) { unsigned int i; for (i = 0; i < KNET_THREAD_STATUS_MAX; i++) { if (thread_status[i].val == status) { return thread_status[i].name; } } return "unknown"; } static const char *get_thread_name(uint8_t thread_id) { unsigned int i; for (i = 0; i < KNET_THREAD_MAX; i++) { if (thread_names[i].val == thread_id) { return thread_names[i].name; } } return "unknown"; } int get_thread_flush_queue(knet_handle_t knet_h, uint8_t thread_id) { uint8_t flush; if (pthread_mutex_lock(&knet_h->threads_status_mutex) != 0) { log_debug(knet_h, KNET_SUB_HANDLE, "Unable to get mutex lock"); return -1; } flush = knet_h->threads_flush_queue[thread_id]; pthread_mutex_unlock(&knet_h->threads_status_mutex); return flush; } int set_thread_flush_queue(knet_handle_t knet_h, uint8_t thread_id, uint8_t status) { if (pthread_mutex_lock(&knet_h->threads_status_mutex) != 0) { log_debug(knet_h, KNET_SUB_HANDLE, "Unable to get mutex lock"); return -1; } knet_h->threads_flush_queue[thread_id] = status; log_debug(knet_h, KNET_SUB_HANDLE, "Updated flush queue request for thread %s to %u", get_thread_name(thread_id), status); pthread_mutex_unlock(&knet_h->threads_status_mutex); return 0; } int wait_all_threads_flush_queue(knet_handle_t knet_h) { uint8_t i = 0, found = 0; while (!found) { usleep(knet_h->threads_timer_res); if (pthread_mutex_lock(&knet_h->threads_status_mutex) != 0) { continue; } found = 1; for (i = 0; i < KNET_THREAD_MAX; i++) { if (knet_h->threads_flush_queue[i] == KNET_THREAD_QUEUE_FLUSHED) { continue; } log_debug(knet_h, KNET_SUB_HANDLE, "Checking thread: %s queue: %u", get_thread_name(i), knet_h->threads_flush_queue[i]); if (knet_h->threads_flush_queue[i] != KNET_THREAD_QUEUE_FLUSHED) { found = 0; } } pthread_mutex_unlock(&knet_h->threads_status_mutex); } return 0; } int set_thread_status(knet_handle_t knet_h, uint8_t thread_id, uint8_t status) { if (pthread_mutex_lock(&knet_h->threads_status_mutex) != 0) { log_debug(knet_h, KNET_SUB_HANDLE, "Unable to get mutex lock"); return -1; } knet_h->threads_status[thread_id] = status; log_debug(knet_h, KNET_SUB_HANDLE, "Updated status for thread %s to %s", get_thread_name(thread_id), get_thread_status_name(status)); pthread_mutex_unlock(&knet_h->threads_status_mutex); return 0; } int wait_all_threads_status(knet_handle_t knet_h, uint8_t status) { uint8_t i = 0, found = 0; while (!found) { usleep(knet_h->threads_timer_res); if (pthread_mutex_lock(&knet_h->threads_status_mutex) != 0) { continue; } found = 1; for (i = 0; i < KNET_THREAD_MAX; i++) { if (knet_h->threads_status[i] == KNET_THREAD_UNREGISTERED) { continue; } log_debug(knet_h, KNET_SUB_HANDLE, "Checking thread: %s status: %s req: %s", get_thread_name(i), get_thread_status_name(knet_h->threads_status[i]), get_thread_status_name(status)); if (knet_h->threads_status[i] != status) { found = 0; } } pthread_mutex_unlock(&knet_h->threads_status_mutex); } return 0; } void force_pmtud_run(knet_handle_t knet_h, uint8_t subsystem, uint8_t reset_mtu, uint8_t force_restart) { if (reset_mtu) { log_debug(knet_h, subsystem, "PMTUd has been reset to default"); knet_h->data_mtu = calc_min_mtu(knet_h); if (knet_h->pmtud_notify_fn) { knet_h->pmtud_notify_fn(knet_h->pmtud_notify_fn_private_data, knet_h->data_mtu); } } /* * we can only try to take a lock here. This part of the code * can be invoked by any thread, including PMTUd that is already * holding a lock at that stage. * If PMTUd is holding the lock, most likely it is already running * and we don't need to notify it back. */ if (!pthread_mutex_trylock(&knet_h->pmtud_mutex)) { if (!knet_h->pmtud_running) { if (!knet_h->pmtud_forcerun) { log_debug(knet_h, subsystem, "Notifying PMTUd to rerun"); knet_h->pmtud_forcerun = 1; } } else { if (force_restart) { if (_pmtud_reschedule(knet_h) < 0) { log_info(knet_h, KNET_SUB_PMTUD, "Unable to notify PMTUd to reschedule. A joining node may struggle to connect properly"); } } } pthread_mutex_unlock(&knet_h->pmtud_mutex); } } int knet_handle_set_threads_timer_res(knet_handle_t knet_h, useconds_t timeres) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } /* * most threads use timeres / 1000 as timeout on epoll. * anything below 1000 would generate a result of 0, making * the threads spin at 100% cpu */ if ((timeres > 0) && (timeres < 1000)) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (timeres) { knet_h->threads_timer_res = timeres; log_debug(knet_h, KNET_SUB_HANDLE, "Setting new threads timer resolution to %u usecs", knet_h->threads_timer_res); } else { knet_h->threads_timer_res = KNET_THREADS_TIMER_RES; log_debug(knet_h, KNET_SUB_HANDLE, "Setting new threads timer resolution to default %u usecs", knet_h->threads_timer_res); } pthread_rwlock_unlock(&knet_h->global_rwlock); return 0; } int knet_handle_get_threads_timer_res(knet_handle_t knet_h, useconds_t *timeres) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!timeres) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } *timeres = knet_h->threads_timer_res; pthread_rwlock_unlock(&knet_h->global_rwlock); return 0; } uint32_t compute_chksum(const unsigned char *data, uint32_t data_len) { uLong crc; crc = crc32(0, NULL, 0); crc = crc32(crc, (Bytef*)data, data_len); return crc; } uint32_t compute_chksumv(const struct iovec *iov_in, int iovcnt_in) { uLong crc; int i; crc = crc32(0, NULL, 0); for (i = 0; i < iovcnt_in; i++) { crc = crc32(crc, (Bytef*)iov_in[i].iov_base, iov_in[i].iov_len); } return crc; } diff --git a/libknet/threads_common.h b/libknet/threads_common.h index f38dc283..d9616402 100644 --- a/libknet/threads_common.h +++ b/libknet/threads_common.h @@ -1,56 +1,56 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_THREADS_COMMON_H__ #define __KNET_THREADS_COMMON_H__ #include "internals.h" #define KNET_THREAD_UNREGISTERED 0 /* thread does not exist */ #define KNET_THREAD_REGISTERED 1 /* thread has been registered before pthread_create invocation. make sure threads are registered before calling wait_all_thread_status */ #define KNET_THREAD_STARTED 2 /* thread has reported to be running */ #define KNET_THREAD_STOPPED 3 /* thread has returned */ #define KNET_THREAD_STATUS_MAX KNET_THREAD_STOPPED + 1 #define KNET_THREAD_TX 0 #define KNET_THREAD_RX 1 #define KNET_THREAD_HB 2 #define KNET_THREAD_PMTUD 3 #define KNET_THREAD_DST_LINK 4 #ifdef HAVE_NETINET_SCTP_H #define KNET_THREAD_SCTP_LISTEN 5 #define KNET_THREAD_SCTP_CONN 6 #endif #define KNET_THREAD_MAX 32 #define KNET_THREAD_QUEUE_FLUSHED 0 #define KNET_THREAD_QUEUE_FLUSH 1 #define timespec_diff(start, end, diff) \ do { \ if (end.tv_sec > start.tv_sec) \ *(diff) = ((end.tv_sec - start.tv_sec) * 1000000000llu) \ + end.tv_nsec - start.tv_nsec; \ else \ *(diff) = end.tv_nsec - start.tv_nsec; \ } while (0); int shutdown_in_progress(knet_handle_t knet_h); int get_global_wrlock(knet_handle_t knet_h); int get_thread_flush_queue(knet_handle_t knet_h, uint8_t thread_id); int set_thread_flush_queue(knet_handle_t knet_h, uint8_t thread_id, uint8_t status); int wait_all_threads_flush_queue(knet_handle_t knet_h); int set_thread_status(knet_handle_t knet_h, uint8_t thread_id, uint8_t status); int wait_all_threads_status(knet_handle_t knet_h, uint8_t status); void force_pmtud_run(knet_handle_t knet_h, uint8_t subsystem, uint8_t reset_mtu, uint8_t force_restart); uint32_t compute_chksum(const unsigned char *data, uint32_t data_len); uint32_t compute_chksumv(const struct iovec *iov_in, int iovcnt_in); #endif diff --git a/libknet/threads_dsthandler.c b/libknet/threads_dsthandler.c index 5c23d278..58aa35c4 100644 --- a/libknet/threads_dsthandler.c +++ b/libknet/threads_dsthandler.c @@ -1,66 +1,66 @@ /* - * Copyright (C) 2015-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2015-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include "host.h" #include "compat.h" #include "logging.h" #include "threads_common.h" #include "threads_dsthandler.h" #include "threads_pmtud.h" static void _handle_dst_link_updates(knet_handle_t knet_h) { knet_node_id_t host_id; struct knet_host *host; if (recv(knet_h->dstsockfd[0], &host_id, sizeof(host_id), MSG_DONTWAIT | MSG_NOSIGNAL) != sizeof(host_id)) { log_debug(knet_h, KNET_SUB_DSTCACHE, "Short read on dstsockfd"); return; } if (get_global_wrlock(knet_h) != 0) { log_debug(knet_h, KNET_SUB_DSTCACHE, "Unable to get read lock"); return; } host = knet_h->host_index[host_id]; if (!host) { log_debug(knet_h, KNET_SUB_DSTCACHE, "Unable to find host: %u", host_id); goto out_unlock; } _host_dstcache_update_sync(knet_h, host); out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); return; } void *_handle_dst_link_handler_thread(void *data) { knet_handle_t knet_h = (knet_handle_t) data; struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; set_thread_status(knet_h, KNET_THREAD_DST_LINK, KNET_THREAD_STARTED); while (!shutdown_in_progress(knet_h)) { if (epoll_wait(knet_h->dst_link_handler_epollfd, events, KNET_EPOLL_MAX_EVENTS, knet_h->threads_timer_res / 1000) >= 1) _handle_dst_link_updates(knet_h); } set_thread_status(knet_h, KNET_THREAD_DST_LINK, KNET_THREAD_STOPPED); return NULL; } diff --git a/libknet/threads_dsthandler.h b/libknet/threads_dsthandler.h index 7ec675f5..ee52e9ef 100644 --- a/libknet/threads_dsthandler.h +++ b/libknet/threads_dsthandler.h @@ -1,15 +1,15 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_THREADS_DSTHANDLER_H__ #define __KNET_THREADS_DSTHANDLER_H__ void *_handle_dst_link_handler_thread(void *data); #endif diff --git a/libknet/threads_heartbeat.c b/libknet/threads_heartbeat.c index a2df4703..4f403642 100644 --- a/libknet/threads_heartbeat.c +++ b/libknet/threads_heartbeat.c @@ -1,419 +1,419 @@ /* - * Copyright (C) 2015-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2015-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "crypto.h" #include "host.h" #include "links.h" #include "logging.h" #include "transports.h" #include "threads_common.h" #include "threads_heartbeat.h" #include "onwire_v1.h" static void _link_down(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_link *dst_link) { memset(&dst_link->pmtud_last, 0, sizeof(struct timespec)); dst_link->received_pong = 0; dst_link->status.pong_last.tv_nsec = 0; dst_link->pong_timeout_backoff = KNET_LINK_PONG_TIMEOUT_BACKOFF; if (dst_link->status.connected == 1) { log_info(knet_h, KNET_SUB_LINK, "host: %u link: %u is down", dst_host->host_id, dst_link->link_id); _link_updown(knet_h, dst_host->host_id, dst_link->link_id, dst_link->status.enabled, 0, 1); } } static void send_ping(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_link *dst_link, int timed) { int err = 0, savederrno = 0, stats_err = 0; int len; ssize_t outlen; struct timespec clock_now, pong_last; unsigned long long diff_ping; unsigned char *outbuf = (unsigned char *)knet_h->pingbuf; uint8_t onwire_ver; if (dst_link->transport_connected == 0) { _link_down(knet_h, dst_host, dst_link); return; } /* caching last pong to avoid race conditions */ pong_last = dst_link->status.pong_last; if (clock_gettime(CLOCK_MONOTONIC, &clock_now) != 0) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get monotonic clock"); return; } timespec_diff(dst_link->ping_last, clock_now, &diff_ping); if ((diff_ping >= (dst_link->ping_interval * 1000llu)) || (!timed)) { if (pthread_mutex_lock(&knet_h->onwire_mutex)) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get onwire mutex lock"); return; } onwire_ver = knet_h->onwire_ver; pthread_mutex_unlock(&knet_h->onwire_mutex); if (knet_h->onwire_ver_remap) { if (prep_ping_v1(knet_h, dst_link, onwire_ver, clock_now, timed, &outlen) < 0) { return; } } else { switch (onwire_ver) { case 1: if (prep_ping_v1(knet_h, dst_link, onwire_ver, clock_now, timed, &outlen) < 0) { return; } break; default: log_warn(knet_h, KNET_SUB_HEARTBEAT, "preparing ping onwire version %u not supported", onwire_ver); return; break; } } if (knet_h->crypto_in_use_config) { if (crypto_encrypt_and_sign(knet_h, (const unsigned char *)knet_h->pingbuf, outlen, knet_h->pingbuf_crypt, &outlen) < 0) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to crypto ping packet"); return; } outbuf = knet_h->pingbuf_crypt; if (pthread_mutex_lock(&knet_h->handle_stats_mutex) < 0) { log_err(knet_h, KNET_SUB_HEARTBEAT, "Unable to get mutex lock"); return; } knet_h->stats_extra.tx_crypt_ping_packets++; pthread_mutex_unlock(&knet_h->handle_stats_mutex); } stats_err = pthread_mutex_lock(&dst_link->link_stats_mutex); if (stats_err) { log_err(knet_h, KNET_SUB_HEARTBEAT, "Unable to get stats mutex lock for host %u link %u: %s", dst_host->host_id, dst_link->link_id, strerror(stats_err)); return; } retry: if (transport_get_connection_oriented(knet_h, dst_link->transport) == TRANSPORT_PROTO_NOT_CONNECTION_ORIENTED) { len = sendto(dst_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) &dst_link->dst_addr, knet_h->knet_transport_fd_tracker[dst_link->outsock].sockaddr_len); } else { len = sendto(dst_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0); } savederrno = errno; dst_link->ping_last = clock_now; dst_link->status.stats.tx_ping_packets++; dst_link->status.stats.tx_ping_bytes += outlen; if (len != outlen) { err = transport_tx_sock_error(knet_h, dst_link->transport, dst_link->outsock, KNET_SUB_HEARTBEAT, len, savederrno); switch(err) { case KNET_TRANSPORT_SOCK_ERROR_INTERNAL: log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to send ping (sock: %d) packet (sendto): %d %s. recorded src ip: %s src port: %s dst ip: %s dst port: %s", dst_link->outsock, savederrno, strerror(savederrno), dst_link->status.src_ipaddr, dst_link->status.src_port, dst_link->status.dst_ipaddr, dst_link->status.dst_port); dst_link->status.stats.tx_ping_errors++; break; case KNET_TRANSPORT_SOCK_ERROR_IGNORE: break; case KNET_TRANSPORT_SOCK_ERROR_RETRY: dst_link->status.stats.tx_ping_retries++; goto retry; break; } } else { dst_link->last_ping_size = outlen; } pthread_mutex_unlock(&dst_link->link_stats_mutex); } timespec_diff(pong_last, clock_now, &diff_ping); if ((pong_last.tv_nsec) && (diff_ping >= (dst_link->pong_timeout_adj * 1000llu))) { _link_down(knet_h, dst_host, dst_link); } } static void send_pong(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf) { int err = 0, savederrno = 0, stats_err = 0; unsigned char *outbuf = (unsigned char *)inbuf; ssize_t len, outlen; if (knet_h->onwire_ver_remap) { prep_pong_v1(knet_h, inbuf, &outlen); } else { switch (inbuf->kh_version) { case 1: prep_pong_v1(knet_h, inbuf, &outlen); break; default: log_warn(knet_h, KNET_SUB_HEARTBEAT, "preparing pong onwire version %u not supported", inbuf->kh_version); return; break; } } if (knet_h->crypto_in_use_config) { if (crypto_encrypt_and_sign(knet_h, (const unsigned char *)inbuf, outlen, knet_h->recv_from_links_buf_crypt, &outlen) < 0) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to encrypt pong packet"); return; } outbuf = knet_h->recv_from_links_buf_crypt; stats_err = pthread_mutex_lock(&knet_h->handle_stats_mutex); if (stats_err < 0) { log_err(knet_h, KNET_SUB_HEARTBEAT, "Unable to get mutex lock: %s", strerror(stats_err)); return; } knet_h->stats_extra.tx_crypt_pong_packets++; pthread_mutex_unlock(&knet_h->handle_stats_mutex); } retry: if (src_link->transport_connected) { if (transport_get_connection_oriented(knet_h, src_link->transport) == TRANSPORT_PROTO_NOT_CONNECTION_ORIENTED) { len = sendto(src_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) &src_link->dst_addr, knet_h->knet_transport_fd_tracker[src_link->outsock].sockaddr_len); } else { len = sendto(src_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0); } savederrno = errno; if (len != outlen) { err = transport_tx_sock_error(knet_h, src_link->transport, src_link->outsock, KNET_SUB_HEARTBEAT, len, savederrno); switch(err) { case KNET_TRANSPORT_SOCK_ERROR_INTERNAL: log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to send pong reply (sock: %d) packet (sendto): %d %s. recorded src ip: %s src port: %s dst ip: %s dst port: %s", src_link->outsock, errno, strerror(errno), src_link->status.src_ipaddr, src_link->status.src_port, src_link->status.dst_ipaddr, src_link->status.dst_port); src_link->status.stats.tx_pong_errors++; break; case KNET_TRANSPORT_SOCK_ERROR_IGNORE: break; case KNET_TRANSPORT_SOCK_ERROR_RETRY: src_link->status.stats.tx_pong_retries++; goto retry; break; } } src_link->status.stats.tx_pong_packets++; src_link->status.stats.tx_pong_bytes += outlen; } } void process_ping(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, ssize_t len) { src_link->status.stats.rx_ping_packets++; src_link->status.stats.rx_ping_bytes += len; if (knet_h->onwire_ver_remap) { process_ping_v1(knet_h, src_host, src_link, inbuf, len); } else { switch (inbuf->kh_version) { case 1: process_ping_v1(knet_h, src_host, src_link, inbuf, len); break; default: log_warn(knet_h, KNET_SUB_HEARTBEAT, "parsing ping onwire version %u not supported", inbuf->kh_version); return; break; } } send_pong(knet_h, src_host, src_link, inbuf); } void process_pong(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, ssize_t len) { struct timespec recvtime; unsigned long long latency_last; clock_gettime(CLOCK_MONOTONIC, &src_link->status.pong_last); src_link->status.stats.rx_pong_packets++; src_link->status.stats.rx_pong_bytes += len; if (knet_h->onwire_ver_remap) { process_pong_v1(knet_h, src_host, src_link, inbuf, &recvtime); } else { switch (inbuf->kh_version) { case 1: process_pong_v1(knet_h, src_host, src_link, inbuf, &recvtime); break; default: log_warn(knet_h, KNET_SUB_HEARTBEAT, "parsing pong onwire version %u not supported", inbuf->kh_version); return; break; } } timespec_diff(recvtime, src_link->status.pong_last, &latency_last); if ((latency_last / 1000llu) > src_link->pong_timeout) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Incoming pong packet from host: %u link: %u has higher latency than pong_timeout. Discarding", src_host->host_id, src_link->link_id); } else { /* * in words : ('previous mean' * '(count -1)') + 'new value') / 'count' */ src_link->status.stats.latency_samples++; /* * limit to max_samples (precision) */ if (src_link->status.stats.latency_samples >= src_link->latency_max_samples) { src_link->status.stats.latency_samples = src_link->latency_max_samples; } src_link->status.stats.latency_ave = (((src_link->status.stats.latency_ave * (src_link->status.stats.latency_samples - 1)) + (latency_last / 1000llu)) / src_link->status.stats.latency_samples); if (src_link->status.stats.latency_ave < src_link->pong_timeout_adj) { if (!src_link->status.connected) { if (src_link->received_pong >= src_link->pong_count) { log_info(knet_h, KNET_SUB_HEARTBEAT, "host: %u link: %u is up", src_host->host_id, src_link->link_id); _link_updown(knet_h, src_host->host_id, src_link->link_id, src_link->status.enabled, 1, 0); } else { src_link->received_pong++; log_debug(knet_h, KNET_SUB_HEARTBEAT, "host: %u link: %u received pong: %u", src_host->host_id, src_link->link_id, src_link->received_pong); } } } /* Calculate latency stats */ if (src_link->status.stats.latency_ave > src_link->status.stats.latency_max) { src_link->status.stats.latency_max = src_link->status.stats.latency_ave; } if (src_link->status.stats.latency_ave < src_link->status.stats.latency_min) { src_link->status.stats.latency_min = src_link->status.stats.latency_ave; } } } void _send_pings(knet_handle_t knet_h, int timed) { struct knet_host *dst_host; int link_idx; if (pthread_mutex_lock(&knet_h->hb_mutex)) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get hb mutex lock"); return; } for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { if ((dst_host->link[link_idx].status.enabled != 1) || (dst_host->link[link_idx].transport == KNET_TRANSPORT_LOOPBACK ) || ((dst_host->link[link_idx].dynamic == KNET_LINK_DYNIP) && (dst_host->link[link_idx].status.dynconnected != 1))) continue; send_ping(knet_h, dst_host, &dst_host->link[link_idx], timed); } } pthread_mutex_unlock(&knet_h->hb_mutex); } static void _adjust_pong_timeouts(knet_handle_t knet_h) { struct knet_host *dst_host; struct knet_link *dst_link; int link_idx; if (pthread_mutex_lock(&knet_h->backoff_mutex)) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get backoff_mutex"); return; } for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { if ((dst_host->link[link_idx].status.enabled != 1) || (dst_host->link[link_idx].transport == KNET_TRANSPORT_LOOPBACK ) || ((dst_host->link[link_idx].dynamic == KNET_LINK_DYNIP) && (dst_host->link[link_idx].status.dynconnected != 1))) continue; dst_link = &dst_host->link[link_idx]; if (dst_link->pong_timeout_backoff > 1) { dst_link->pong_timeout_backoff--; } dst_link->pong_timeout_adj = (dst_link->pong_timeout * dst_link->pong_timeout_backoff) + (dst_link->status.stats.latency_ave * KNET_LINK_PONG_TIMEOUT_LAT_MUL); } } pthread_mutex_unlock(&knet_h->backoff_mutex); } void *_handle_heartbt_thread(void *data) { knet_handle_t knet_h = (knet_handle_t) data; int i = 1; set_thread_status(knet_h, KNET_THREAD_HB, KNET_THREAD_STARTED); while (!shutdown_in_progress(knet_h)) { usleep(knet_h->threads_timer_res); if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { log_debug(knet_h, KNET_SUB_HEARTBEAT, "Unable to get read lock"); continue; } /* * _adjust_pong_timeouts should execute approx once a second. */ if ((i % (1000000 / knet_h->threads_timer_res)) == 0) { _adjust_pong_timeouts(knet_h); i = 1; } else { i++; } _send_pings(knet_h, 1); pthread_rwlock_unlock(&knet_h->global_rwlock); } set_thread_status(knet_h, KNET_THREAD_HB, KNET_THREAD_STOPPED); return NULL; } diff --git a/libknet/threads_heartbeat.h b/libknet/threads_heartbeat.h index bdae4fab..efc27395 100644 --- a/libknet/threads_heartbeat.h +++ b/libknet/threads_heartbeat.h @@ -1,19 +1,19 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_THREADS_HEARTBEAT_H__ #define __KNET_THREADS_HEARTBEAT_H__ void _send_pings(knet_handle_t knet_h, int timed); void *_handle_heartbt_thread(void *data); void process_ping(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, ssize_t len); void process_pong(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, ssize_t len); #endif diff --git a/libknet/threads_pmtud.c b/libknet/threads_pmtud.c index 455b0a84..bdb17c8e 100644 --- a/libknet/threads_pmtud.c +++ b/libknet/threads_pmtud.c @@ -1,938 +1,938 @@ /* - * Copyright (C) 2015-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2015-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include "crypto.h" #include "links.h" #include "host.h" #include "logging.h" #include "transports.h" #include "threads_common.h" #include "threads_pmtud.h" #include "onwire_v1.h" static int _calculate_manual_mtu(knet_handle_t knet_h, struct knet_link *dst_link) { size_t ipproto_overhead_len; /* onwire packet overhead (protocol based) */ switch (dst_link->dst_addr.ss_family) { case AF_INET6: ipproto_overhead_len = KNET_PMTUD_OVERHEAD_V6 + dst_link->proto_overhead; break; case AF_INET: ipproto_overhead_len = KNET_PMTUD_OVERHEAD_V4 + dst_link->proto_overhead; break; default: log_debug(knet_h, KNET_SUB_PMTUD, "unknown protocol"); return 0; break; } dst_link->status.mtu = calc_max_data_outlen(knet_h, knet_h->manual_mtu - ipproto_overhead_len); return 1; } static int _handle_check_link_pmtud(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_link *dst_link) { int err, ret, savederrno, mutex_retry_limit, failsafe, use_kernel_mtu, warn_once; uint32_t kernel_mtu; /* record kernel_mtu from EMSGSIZE */ size_t onwire_len; /* current packet onwire size */ size_t ipproto_overhead_len; /* onwire packet overhead (protocol based) */ size_t max_mtu_len; /* max mtu for protocol */ size_t data_len; /* how much data we can send in the packet * generally would be onwire_len - ipproto_overhead_len * needs to be adjusted for crypto */ size_t app_mtu_len; /* real data that we can send onwire */ ssize_t len; /* len of what we were able to sendto onwire */ uint8_t onwire_ver; struct timespec ts, pmtud_crypto_start_ts, pmtud_crypto_stop_ts; unsigned long long pong_timeout_adj_tmp, timediff; int pmtud_crypto_reduce = 1; unsigned char *outbuf = (unsigned char *)knet_h->pmtudbuf; warn_once = 0; mutex_retry_limit = 0; failsafe = 0; switch (dst_link->dst_addr.ss_family) { case AF_INET6: max_mtu_len = KNET_PMTUD_SIZE_V6; ipproto_overhead_len = KNET_PMTUD_OVERHEAD_V6 + dst_link->proto_overhead; break; case AF_INET: max_mtu_len = KNET_PMTUD_SIZE_V4; ipproto_overhead_len = KNET_PMTUD_OVERHEAD_V4 + dst_link->proto_overhead; break; default: log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD aborted, unknown protocol"); return -1; break; } dst_link->last_bad_mtu = 0; dst_link->last_good_mtu = dst_link->last_ping_size + ipproto_overhead_len; /* * discovery starts from the top because kernel will * refuse to send packets > current iface mtu. * this saves us some time and network bw. */ onwire_len = max_mtu_len; /* * cache onwire version for this link / run */ if (pthread_mutex_lock(&knet_h->onwire_mutex)) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get onwire mutex lock"); return -1; } onwire_ver = knet_h->onwire_ver; pthread_mutex_unlock(&knet_h->onwire_mutex); restart: /* * prevent a race when interface mtu is changed _exactly_ during * the discovery process and it's complex to detect. Easier * to wait the next loop. * 30 is not an arbitrary value. To bisect from 576 to 128000 doesn't * take more than 18/19 steps. */ if (failsafe == 30) { log_err(knet_h, KNET_SUB_PMTUD, "Aborting PMTUD process: Too many attempts. MTU might have changed during discovery."); return -1; } else { failsafe++; } /* * common to all packets */ /* * calculate the application MTU based on current onwire_len minus ipproto_overhead_len */ app_mtu_len = calc_max_data_outlen(knet_h, onwire_len - ipproto_overhead_len); /* * recalculate onwire len back that might be different based * on data padding from crypto layer. */ onwire_len = calc_data_outlen(knet_h, app_mtu_len + KNET_HEADER_ALL_SIZE) + ipproto_overhead_len; /* * calculate the size of what we need to send to sendto(2). * see also onwire.c for packet format explanation. */ data_len = app_mtu_len + knet_h->sec_hash_size + knet_h->sec_salt_size + KNET_HEADER_ALL_SIZE; if (knet_h->onwire_ver_remap) { prep_pmtud_v1(knet_h, dst_link, onwire_ver, onwire_len, app_mtu_len + KNET_HEADER_ALL_SIZE); } else { switch (onwire_ver) { case 1: prep_pmtud_v1(knet_h, dst_link, onwire_ver, onwire_len, app_mtu_len + KNET_HEADER_ALL_SIZE); break; default: log_warn(knet_h, KNET_SUB_PMTUD, "preparing PMTUD onwire version %u not supported", onwire_ver); return -1; break; } } if (knet_h->crypto_in_use_config) { if (data_len < (knet_h->sec_hash_size + knet_h->sec_salt_size) + 1) { log_debug(knet_h, KNET_SUB_PMTUD, "Aborting PMTUD process: link mtu smaller than crypto header detected (link might have been disconnected)"); return -1; } if (crypto_encrypt_and_sign(knet_h, (const unsigned char *)knet_h->pmtudbuf, data_len - (knet_h->sec_hash_size + knet_h->sec_salt_size), knet_h->pmtudbuf_crypt, (ssize_t *)&data_len) < 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to crypto pmtud packet"); return -1; } outbuf = knet_h->pmtudbuf_crypt; if (pthread_mutex_lock(&knet_h->handle_stats_mutex) < 0) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock"); return -1; } knet_h->stats_extra.tx_crypt_pmtu_packets++; pthread_mutex_unlock(&knet_h->handle_stats_mutex); } /* link has gone down, aborting pmtud */ if (dst_link->status.connected != 1) { log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD detected host (%u) link (%u) has been disconnected", dst_host->host_id, dst_link->link_id); return -1; } if (dst_link->transport_connected != 1) { log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD detected host (%u) link (%u) has been disconnected", dst_host->host_id, dst_link->link_id); return -1; } if (pthread_mutex_lock(&knet_h->pmtud_mutex) != 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock"); return -1; } if (knet_h->pmtud_abort) { pthread_mutex_unlock(&knet_h->pmtud_mutex); errno = EDEADLK; return -1; } savederrno = pthread_mutex_lock(&knet_h->tx_mutex); if (savederrno) { pthread_mutex_unlock(&knet_h->pmtud_mutex); log_err(knet_h, KNET_SUB_PMTUD, "Unable to get TX mutex lock: %s", strerror(savederrno)); return -1; } savederrno = pthread_mutex_lock(&dst_link->link_stats_mutex); if (savederrno) { pthread_mutex_unlock(&knet_h->pmtud_mutex); pthread_mutex_unlock(&knet_h->tx_mutex); log_err(knet_h, KNET_SUB_PMTUD, "Unable to get stats mutex lock for host %u link %u: %s", dst_host->host_id, dst_link->link_id, strerror(savederrno)); return -1; } retry: if (transport_get_connection_oriented(knet_h, dst_link->transport) == TRANSPORT_PROTO_NOT_CONNECTION_ORIENTED) { len = sendto(dst_link->outsock, outbuf, data_len, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) &dst_link->dst_addr, knet_h->knet_transport_fd_tracker[dst_link->outsock].sockaddr_len); } else { len = sendto(dst_link->outsock, outbuf, data_len, MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0); } savederrno = errno; /* * we cannot hold a lock on kmtu_mutex between resetting * knet_h->kernel_mtu here and below where it's used. * use_kernel_mtu tells us if the knet_h->kernel_mtu was * set to 0 and we can trust its value later. */ use_kernel_mtu = 0; if (pthread_mutex_lock(&knet_h->kmtu_mutex) == 0) { use_kernel_mtu = 1; knet_h->kernel_mtu = 0; pthread_mutex_unlock(&knet_h->kmtu_mutex); } kernel_mtu = 0; err = transport_tx_sock_error(knet_h, dst_link->transport, dst_link->outsock, KNET_SUB_PMTUD, len, savederrno); switch(err) { case KNET_TRANSPORT_SOCK_ERROR_INTERNAL: log_debug(knet_h, KNET_SUB_PMTUD, "Unable to send pmtu packet (sendto): %d %s", savederrno, strerror(savederrno)); pthread_mutex_unlock(&knet_h->tx_mutex); pthread_mutex_unlock(&knet_h->pmtud_mutex); dst_link->status.stats.tx_pmtu_errors++; pthread_mutex_unlock(&dst_link->link_stats_mutex); return -1; break; case KNET_TRANSPORT_SOCK_ERROR_IGNORE: break; case KNET_TRANSPORT_SOCK_ERROR_RETRY: dst_link->status.stats.tx_pmtu_retries++; goto retry; break; } pthread_mutex_unlock(&knet_h->tx_mutex); if (len != (ssize_t )data_len) { pthread_mutex_unlock(&dst_link->link_stats_mutex); if (savederrno == EMSGSIZE || savederrno == EPERM) { /* * we cannot hold a lock on kmtu_mutex between resetting * knet_h->kernel_mtu and here. * use_kernel_mtu tells us if the knet_h->kernel_mtu was * set to 0 previously and we can trust its value now. */ if (use_kernel_mtu) { use_kernel_mtu = 0; if (pthread_mutex_lock(&knet_h->kmtu_mutex) == 0) { kernel_mtu = knet_h->kernel_mtu; pthread_mutex_unlock(&knet_h->kmtu_mutex); } } if (kernel_mtu > 0) { dst_link->last_bad_mtu = kernel_mtu + 1; } else { dst_link->last_bad_mtu = onwire_len; } } else { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to send pmtu packet len: %zu err: %s", onwire_len, strerror(savederrno)); } } else { dst_link->last_sent_mtu = onwire_len; dst_link->last_recv_mtu = 0; dst_link->status.stats.tx_pmtu_packets++; dst_link->status.stats.tx_pmtu_bytes += data_len; pthread_mutex_unlock(&dst_link->link_stats_mutex); if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get current time: %s", strerror(errno)); pthread_mutex_unlock(&knet_h->pmtud_mutex); return -1; } /* * non fatal, we can wait the next round to reduce the * multiplier */ if (clock_gettime(CLOCK_MONOTONIC, &pmtud_crypto_start_ts) < 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get current time: %s", strerror(errno)); pmtud_crypto_reduce = 0; } /* * set PMTUd reply timeout to match pong_timeout on a given link * * math: internally pong_timeout is expressed in microseconds, while * the public API exports milliseconds. So careful with the 0's here. * the loop is necessary because we are grabbing the current time just above * and add values to it that could overflow into seconds. */ if (pthread_mutex_lock(&knet_h->backoff_mutex)) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get backoff_mutex"); pthread_mutex_unlock(&knet_h->pmtud_mutex); return -1; } if (knet_h->crypto_in_use_config) { /* * crypto, under pressure, is a royal PITA */ pong_timeout_adj_tmp = dst_link->pong_timeout_adj * dst_link->pmtud_crypto_timeout_multiplier; } else { pong_timeout_adj_tmp = dst_link->pong_timeout_adj; } ts.tv_sec += pong_timeout_adj_tmp / 1000000; ts.tv_nsec += (((pong_timeout_adj_tmp) % 1000000) * 1000); while (ts.tv_nsec > 1000000000) { ts.tv_sec += 1; ts.tv_nsec -= 1000000000; } pthread_mutex_unlock(&knet_h->backoff_mutex); knet_h->pmtud_waiting = 1; ret = pthread_cond_timedwait(&knet_h->pmtud_cond, &knet_h->pmtud_mutex, &ts); knet_h->pmtud_waiting = 0; if (knet_h->pmtud_abort) { pthread_mutex_unlock(&knet_h->pmtud_mutex); errno = EDEADLK; return -1; } /* * we cannot use shutdown_in_progress in here because * we already hold the read lock */ if (knet_h->fini_in_progress) { pthread_mutex_unlock(&knet_h->pmtud_mutex); log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD aborted. shutdown in progress"); return -1; } if (ret) { if (ret == ETIMEDOUT) { if ((knet_h->crypto_in_use_config) && (dst_link->pmtud_crypto_timeout_multiplier < KNET_LINK_PMTUD_CRYPTO_TIMEOUT_MULTIPLIER_MAX)) { dst_link->pmtud_crypto_timeout_multiplier = dst_link->pmtud_crypto_timeout_multiplier * 2; pmtud_crypto_reduce = 0; log_debug(knet_h, KNET_SUB_PMTUD, "Increasing PMTUd response timeout multiplier to (%u) for host %u link: %u", dst_link->pmtud_crypto_timeout_multiplier, dst_host->host_id, dst_link->link_id); pthread_mutex_unlock(&knet_h->pmtud_mutex); goto restart; } if (!warn_once) { log_warn(knet_h, KNET_SUB_PMTUD, "possible MTU misconfiguration detected. " "kernel is reporting MTU: %u bytes for " "host %u link %u but the other node is " "not acknowledging packets of this size. ", dst_link->last_sent_mtu, dst_host->host_id, dst_link->link_id); log_warn(knet_h, KNET_SUB_PMTUD, "This can be caused by this node interface MTU " "too big or a network device that does not " "support or has been misconfigured to manage MTU " "of this size, or packet loss. knet will continue " "to run but performances might be affected."); warn_once = 1; } } else { pthread_mutex_unlock(&knet_h->pmtud_mutex); if (mutex_retry_limit == 3) { log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD aborted, unable to get mutex lock"); return -1; } mutex_retry_limit++; goto restart; } } if ((knet_h->crypto_in_use_config) && (pmtud_crypto_reduce == 1) && (dst_link->pmtud_crypto_timeout_multiplier > KNET_LINK_PMTUD_CRYPTO_TIMEOUT_MULTIPLIER_MIN)) { if (!clock_gettime(CLOCK_MONOTONIC, &pmtud_crypto_stop_ts)) { timespec_diff(pmtud_crypto_start_ts, pmtud_crypto_stop_ts, &timediff); if (((pong_timeout_adj_tmp * 1000) / 2) > timediff) { dst_link->pmtud_crypto_timeout_multiplier = dst_link->pmtud_crypto_timeout_multiplier / 2; log_debug(knet_h, KNET_SUB_PMTUD, "Decreasing PMTUd response timeout multiplier to (%u) for host %u link: %u", dst_link->pmtud_crypto_timeout_multiplier, dst_host->host_id, dst_link->link_id); } } else { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get current time: %s", strerror(errno)); } } if ((dst_link->last_recv_mtu != onwire_len) || (ret)) { dst_link->last_bad_mtu = onwire_len; } else { int found_mtu = 0; if (knet_h->sec_block_size) { if ((onwire_len + knet_h->sec_block_size >= max_mtu_len) || ((dst_link->last_bad_mtu) && (dst_link->last_bad_mtu <= (onwire_len + knet_h->sec_block_size)))) { found_mtu = 1; } } else { if ((onwire_len == max_mtu_len) || ((dst_link->last_bad_mtu) && (dst_link->last_bad_mtu == (onwire_len + 1))) || (dst_link->last_bad_mtu == dst_link->last_good_mtu)) { found_mtu = 1; } } if (found_mtu) { /* * account for IP overhead, knet headers and crypto in PMTU calculation */ dst_link->status.mtu = calc_max_data_outlen(knet_h, onwire_len - ipproto_overhead_len); pthread_mutex_unlock(&knet_h->pmtud_mutex); return 0; } dst_link->last_good_mtu = onwire_len; } } if (kernel_mtu) { onwire_len = kernel_mtu; } else { onwire_len = (dst_link->last_good_mtu + dst_link->last_bad_mtu) / 2; } pthread_mutex_unlock(&knet_h->pmtud_mutex); goto restart; } static int _handle_check_pmtud(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_link *dst_link, int force_run) { uint8_t saved_valid_pmtud; unsigned int saved_pmtud; struct timespec clock_now; unsigned long long diff_pmtud, interval; if (clock_gettime(CLOCK_MONOTONIC, &clock_now) != 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get monotonic clock"); return 0; } if (!force_run) { interval = knet_h->pmtud_interval * 1000000000llu; /* nanoseconds */ timespec_diff(dst_link->pmtud_last, clock_now, &diff_pmtud); if (diff_pmtud < interval) { return dst_link->has_valid_mtu; } } /* * status.proto_overhead should include all IP/(UDP|SCTP)/knet headers * * please note that it is not the same as link->proto_overhead that * includes only either UDP or SCTP (at the moment) overhead. */ switch (dst_link->dst_addr.ss_family) { case AF_INET6: dst_link->status.proto_overhead = KNET_PMTUD_OVERHEAD_V6 + dst_link->proto_overhead + KNET_HEADER_ALL_SIZE + knet_h->sec_hash_size + knet_h->sec_salt_size; break; case AF_INET: dst_link->status.proto_overhead = KNET_PMTUD_OVERHEAD_V4 + dst_link->proto_overhead + KNET_HEADER_ALL_SIZE + knet_h->sec_hash_size + knet_h->sec_salt_size; break; } saved_pmtud = dst_link->status.mtu; saved_valid_pmtud = dst_link->has_valid_mtu; log_debug(knet_h, KNET_SUB_PMTUD, "Starting PMTUD for host: %u link: %u", dst_host->host_id, dst_link->link_id); errno = 0; if (_handle_check_link_pmtud(knet_h, dst_host, dst_link) < 0) { if (errno == EDEADLK) { log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD for host: %u link: %u has been rescheduled", dst_host->host_id, dst_link->link_id); dst_link->status.mtu = saved_pmtud; dst_link->has_valid_mtu = saved_valid_pmtud; errno = EDEADLK; return dst_link->has_valid_mtu; } dst_link->has_valid_mtu = 0; } else { if (dst_link->status.mtu < calc_min_mtu(knet_h)) { log_info(knet_h, KNET_SUB_PMTUD, "Invalid MTU detected for host: %u link: %u mtu: %u", dst_host->host_id, dst_link->link_id, dst_link->status.mtu); dst_link->has_valid_mtu = 0; } else { dst_link->has_valid_mtu = 1; } if (dst_link->has_valid_mtu) { if ((saved_pmtud) && (saved_pmtud != dst_link->status.mtu)) { log_info(knet_h, KNET_SUB_PMTUD, "PMTUD link change for host: %u link: %u from %u to %u", dst_host->host_id, dst_link->link_id, saved_pmtud, dst_link->status.mtu); } log_debug(knet_h, KNET_SUB_PMTUD, "PMTUD completed for host: %u link: %u current link mtu: %u", dst_host->host_id, dst_link->link_id, dst_link->status.mtu); /* * set pmtud_last, if we can, after we are done with the PMTUd process * because it can take a very long time. */ dst_link->pmtud_last = clock_now; if (!clock_gettime(CLOCK_MONOTONIC, &clock_now)) { dst_link->pmtud_last = clock_now; } } } if (saved_valid_pmtud != dst_link->has_valid_mtu) { _host_dstcache_update_async(knet_h, dst_host); } return dst_link->has_valid_mtu; } void *_handle_pmtud_link_thread(void *data) { knet_handle_t knet_h = (knet_handle_t) data; struct knet_host *dst_host; struct knet_link *dst_link; int link_idx; unsigned int have_mtu; unsigned int lower_mtu; int link_has_mtu; int force_run = 0; set_thread_status(knet_h, KNET_THREAD_PMTUD, KNET_THREAD_STARTED); knet_h->data_mtu = calc_min_mtu(knet_h); while (!shutdown_in_progress(knet_h)) { usleep(knet_h->threads_timer_res); if (pthread_mutex_lock(&knet_h->pmtud_mutex) != 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock"); continue; } knet_h->pmtud_abort = 0; knet_h->pmtud_running = 1; force_run = knet_h->pmtud_forcerun; knet_h->pmtud_forcerun = 0; pthread_mutex_unlock(&knet_h->pmtud_mutex); if (force_run) { log_debug(knet_h, KNET_SUB_PMTUD, "PMTUd request to rerun has been received"); } if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get read lock"); continue; } lower_mtu = KNET_PMTUD_SIZE_V4; have_mtu = 0; for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { dst_link = &dst_host->link[link_idx]; if ((dst_link->status.enabled != 1) || (dst_link->status.connected != 1) || (dst_host->link[link_idx].transport == KNET_TRANSPORT_LOOPBACK) || (!dst_link->last_ping_size) || ((dst_link->dynamic == KNET_LINK_DYNIP) && (dst_link->status.dynconnected != 1))) continue; if (!knet_h->manual_mtu) { link_has_mtu = _handle_check_pmtud(knet_h, dst_host, dst_link, force_run); if (errno == EDEADLK) { goto out_unlock; } if (link_has_mtu) { have_mtu = 1; if (dst_link->status.mtu < lower_mtu) { lower_mtu = dst_link->status.mtu; } } } else { link_has_mtu = _calculate_manual_mtu(knet_h, dst_link); if (link_has_mtu) { have_mtu = 1; if (dst_link->status.mtu < lower_mtu) { lower_mtu = dst_link->status.mtu; } } } } } if (have_mtu) { if (knet_h->data_mtu != lower_mtu) { knet_h->data_mtu = lower_mtu; log_info(knet_h, KNET_SUB_PMTUD, "Global data MTU changed to: %u", knet_h->data_mtu); if (knet_h->pmtud_notify_fn) { knet_h->pmtud_notify_fn(knet_h->pmtud_notify_fn_private_data, knet_h->data_mtu); } } } out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); if (pthread_mutex_lock(&knet_h->pmtud_mutex) != 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock"); } else { knet_h->pmtud_running = 0; pthread_mutex_unlock(&knet_h->pmtud_mutex); } } set_thread_status(knet_h, KNET_THREAD_PMTUD, KNET_THREAD_STOPPED); return NULL; } static void send_pmtud_reply(knet_handle_t knet_h, struct knet_link *src_link, struct knet_header *inbuf) { int err = 0, savederrno = 0, stats_err = 0; unsigned char *outbuf = (unsigned char *)inbuf; ssize_t len, outlen; if (knet_h->onwire_ver_remap) { prep_pmtud_reply_v1(knet_h, inbuf, &outlen); } else { switch (inbuf->kh_version) { case 1: prep_pmtud_reply_v1(knet_h, inbuf, &outlen); break; default: log_warn(knet_h, KNET_SUB_PMTUD, "preparing PMTUD reply onwire version %u not supported", inbuf->kh_version); return; break; } } if (knet_h->crypto_in_use_config) { if (crypto_encrypt_and_sign(knet_h, (const unsigned char *)inbuf, outlen, knet_h->recv_from_links_buf_crypt, &outlen) < 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to encrypt PMTUd reply packet"); return; } outbuf = knet_h->recv_from_links_buf_crypt; stats_err = pthread_mutex_lock(&knet_h->handle_stats_mutex); if (stats_err < 0) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock: %s", strerror(stats_err)); return; } knet_h->stats_extra.tx_crypt_pmtu_reply_packets++; pthread_mutex_unlock(&knet_h->handle_stats_mutex); } savederrno = pthread_mutex_lock(&knet_h->tx_mutex); if (savederrno) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get TX mutex lock: %s", strerror(savederrno)); return; } retry: if (src_link->transport_connected) { if (transport_get_connection_oriented(knet_h, src_link->transport) == TRANSPORT_PROTO_NOT_CONNECTION_ORIENTED) { len = sendto(src_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) &src_link->dst_addr, knet_h->knet_transport_fd_tracker[src_link->outsock].sockaddr_len); } else { len = sendto(src_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0); } savederrno = errno; if (len != outlen) { err = transport_tx_sock_error(knet_h, src_link->transport, src_link->outsock, KNET_SUB_PMTUD, len, savederrno); stats_err = pthread_mutex_lock(&src_link->link_stats_mutex); if (stats_err < 0) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock: %s", strerror(stats_err)); return; } switch(err) { case KNET_TRANSPORT_SOCK_ERROR_INTERNAL: log_debug(knet_h, KNET_SUB_PMTUD, "Unable to send PMTUd reply (sock: %d) packet (sendto): %d %s. recorded src ip: %s src port: %s dst ip: %s dst port: %s", src_link->outsock, errno, strerror(errno), src_link->status.src_ipaddr, src_link->status.src_port, src_link->status.dst_ipaddr, src_link->status.dst_port); src_link->status.stats.tx_pmtu_errors++; break; case KNET_TRANSPORT_SOCK_ERROR_IGNORE: src_link->status.stats.tx_pmtu_errors++; break; case KNET_TRANSPORT_SOCK_ERROR_RETRY: src_link->status.stats.tx_pmtu_retries++; pthread_mutex_unlock(&src_link->link_stats_mutex); goto retry; break; } pthread_mutex_unlock(&src_link->link_stats_mutex); } } pthread_mutex_unlock(&knet_h->tx_mutex); } void process_pmtud(knet_handle_t knet_h, struct knet_link *src_link, struct knet_header *inbuf) { /* * at the moment we don't need to take any extra * actions when processing a PMTUd packet, except * sending a reply */ send_pmtud_reply(knet_h, src_link, inbuf); } void process_pmtud_reply(knet_handle_t knet_h, struct knet_link *src_link, struct knet_header *inbuf) { if (pthread_mutex_lock(&knet_h->pmtud_mutex) != 0) { log_debug(knet_h, KNET_SUB_PMTUD, "Unable to get mutex lock"); return; } if (knet_h->onwire_ver_remap) { process_pmtud_reply_v1(knet_h, src_link, inbuf); } else { switch (inbuf->kh_version) { case 1: process_pmtud_reply_v1(knet_h, src_link, inbuf); break; default: log_warn(knet_h, KNET_SUB_PMTUD, "preparing PMTUD reply onwire version %u not supported", inbuf->kh_version); goto out_unlock; break; } } pthread_cond_signal(&knet_h->pmtud_cond); out_unlock: pthread_mutex_unlock(&knet_h->pmtud_mutex); } int knet_handle_pmtud_getfreq(knet_handle_t knet_h, unsigned int *interval) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!interval) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } *interval = knet_h->pmtud_interval; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_handle_pmtud_setfreq(knet_handle_t knet_h, unsigned int interval) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if ((!interval) || (interval > 86400)) { errno = EINVAL; return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->pmtud_interval = interval; log_debug(knet_h, KNET_SUB_PMTUD, "PMTUd interval set to: %u seconds", interval); pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_handle_enable_pmtud_notify(knet_handle_t knet_h, void *pmtud_notify_fn_private_data, void (*pmtud_notify_fn) ( void *private_data, unsigned int data_mtu)) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->pmtud_notify_fn_private_data = pmtud_notify_fn_private_data; knet_h->pmtud_notify_fn = pmtud_notify_fn; if (knet_h->pmtud_notify_fn) { log_debug(knet_h, KNET_SUB_PMTUD, "pmtud_notify_fn enabled"); } else { log_debug(knet_h, KNET_SUB_PMTUD, "pmtud_notify_fn disabled"); } pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_handle_pmtud_set(knet_handle_t knet_h, unsigned int iface_mtu) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (iface_mtu > KNET_PMTUD_SIZE_V4) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } log_info(knet_h, KNET_SUB_PMTUD, "MTU manually set to: %u", iface_mtu); knet_h->manual_mtu = iface_mtu; force_pmtud_run(knet_h, KNET_SUB_PMTUD, 0, 0); pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_handle_pmtud_get(knet_handle_t knet_h, unsigned int *data_mtu) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!data_mtu) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_PMTUD, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } *data_mtu = knet_h->data_mtu; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } diff --git a/libknet/threads_pmtud.h b/libknet/threads_pmtud.h index 7bbd10e8..20d81857 100644 --- a/libknet/threads_pmtud.h +++ b/libknet/threads_pmtud.h @@ -1,18 +1,18 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_THREADS_PMTUD_H__ #define __KNET_THREADS_PMTUD_H__ void *_handle_pmtud_link_thread(void *data); void process_pmtud(knet_handle_t knet_h, struct knet_link *src_link, struct knet_header *inbuf); void process_pmtud_reply(knet_handle_t knet_h, struct knet_link *src_link, struct knet_header *inbuf); #endif diff --git a/libknet/threads_rx.c b/libknet/threads_rx.c index aa1aef97..63e9d65f 100644 --- a/libknet/threads_rx.c +++ b/libknet/threads_rx.c @@ -1,1228 +1,1228 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "compat.h" #include "compress.h" #include "crypto.h" #include "host.h" #include "links.h" #include "links_acl.h" #include "logging.h" #include "transports.h" #include "transport_common.h" #include "threads_common.h" #include "threads_heartbeat.h" #include "threads_pmtud.h" #include "threads_rx.h" #include "netutils.h" #include "onwire_v1.h" /* * RECV */ /* * return 1 if a > b * return -1 if b > a * return 0 if they are equal */ static inline int _timecmp(struct timespec a, struct timespec b) { if (a.tv_sec != b.tv_sec) { if (a.tv_sec > b.tv_sec) { return 1; } else { return -1; } } else { if (a.tv_nsec > b.tv_nsec) { return 1; } else if (a.tv_nsec < b.tv_nsec) { return -1; } else { return 0; } } } /* * calculate use % of defrag buffers per host * and if % is <= knet_h->defrag_bufs_shrink_threshold for the last second, then half the size */ static void _shrink_defrag_buffers(knet_handle_t knet_h) { struct knet_host *host; struct knet_host_defrag_buf *new_bufs = NULL; struct timespec now; unsigned long long time_diff; /* nanoseconds */ uint16_t i, x, in_use_bufs; uint32_t sum; /* * first run. */ if ((knet_h->defrag_bufs_last_run.tv_sec == 0) && (knet_h->defrag_bufs_last_run.tv_nsec == 0)) { clock_gettime(CLOCK_MONOTONIC, &knet_h->defrag_bufs_last_run); return; } clock_gettime(CLOCK_MONOTONIC, &now); timespec_diff(knet_h->defrag_bufs_last_run, now, &time_diff); if (time_diff < (((unsigned long long)knet_h->defrag_bufs_usage_samples_timespan * 1000000000) / knet_h->defrag_bufs_usage_samples)) { return; } /* * record the last run */ memmove(&knet_h->defrag_bufs_last_run, &now, sizeof(struct timespec)); /* * do the real work: */ for (host = knet_h->host_head; host != NULL; host = host->next) { /* * Update buffer usage stats. We do this for all nodes. */ in_use_bufs = 0; for (i = 0; i < host->allocated_defrag_bufs; i++) { if (host->defrag_bufs[i].in_use) { in_use_bufs++; } } /* * record only % */ host->in_use_defrag_buffers[host->in_use_defrag_buffers_index] = (in_use_bufs * 100 / host->allocated_defrag_bufs); host->in_use_defrag_buffers_index++; /* * make sure to stay within buffer */ if (host->in_use_defrag_buffers_index == knet_h->defrag_bufs_usage_samples) { host->in_use_defrag_buffers_index = 0; } /* * only allow shrinking if we have enough samples */ if (host->in_use_defrag_buffers_samples < knet_h->defrag_bufs_usage_samples) { host->in_use_defrag_buffers_samples++; continue; } /* * only allow shrinking if in use bufs are <= knet_h->defrag_bufs_shrink_threshold% */ if (knet_h->defrag_bufs_reclaim_policy == RECLAIM_POLICY_AVERAGE) { sum = 0; for (i = 0; i < knet_h->defrag_bufs_usage_samples; i++) { sum += host->in_use_defrag_buffers[i]; } sum = sum / knet_h->defrag_bufs_usage_samples; if (sum > knet_h->defrag_bufs_shrink_threshold) { continue; } } else { sum = 0; for (i = 0; i < knet_h->defrag_bufs_usage_samples; i++) { if (host->in_use_defrag_buffers[i] > knet_h->defrag_bufs_shrink_threshold) { sum = 1; } } if (sum) { continue; } } /* * only allow shrinking if allocated bufs > min_defrag_bufs */ if (host->allocated_defrag_bufs == knet_h->defrag_bufs_min) { continue; } /* * compat all the in_use buffers at the beginning. * we the checks above, we are 100% sure they fit */ x = 0; for (i = 0; i < host->allocated_defrag_bufs; i++) { if (host->defrag_bufs[i].in_use) { memmove(&host->defrag_bufs[x], &host->defrag_bufs[i], sizeof(struct knet_host_defrag_buf)); x++; } } /* * memory allocation is not critical. it just means the system is under * memory pressure and we will need to wait our turn to free memory... how odd :) */ new_bufs = realloc(host->defrag_bufs, sizeof(struct knet_host_defrag_buf) * (host->allocated_defrag_bufs / 2)); if (!new_bufs) { log_err(knet_h, KNET_SUB_RX, "Unable to decrease defrag buffers for host %u: %s", host->host_id, strerror(errno)); continue; } host->defrag_bufs = new_bufs; host->allocated_defrag_bufs = host->allocated_defrag_bufs / 2; /* * clear buffer use stats. Old ones are no good for new one */ _clear_defrag_bufs_stats(host); log_debug(knet_h, KNET_SUB_RX, "Defrag buffers for host %u decreased from %u to: %u", host->host_id, host->allocated_defrag_bufs * 2, host->allocated_defrag_bufs); } } /* * check if we can double the defrag buffers. * * return 0 if we cannot reallocate * return 1 if we have more buffers */ static int _realloc_defrag_buffers(knet_handle_t knet_h, struct knet_host *src_host) { struct knet_host_defrag_buf *new_bufs = NULL; int i; /* * max_defrag_bufs is a power of 2 * allocated_defrag_bufs doubles on each iteration. * Sooner or later (and hopefully never) allocated with be == to max. */ if (src_host->allocated_defrag_bufs < knet_h->defrag_bufs_max) { new_bufs = realloc(src_host->defrag_bufs, src_host->allocated_defrag_bufs * 2 * sizeof(struct knet_host_defrag_buf)); if (!new_bufs) { log_err(knet_h, KNET_SUB_RX, "Unable to increase defrag buffers for host %u: %s", src_host->host_id, strerror(errno)); return 0; } /* * keep the math simple here between arrays, pointers and what not. * Init each buffer individually. */ for (i = src_host->allocated_defrag_bufs; i < src_host->allocated_defrag_bufs * 2; i++) { memset(&new_bufs[i], 0, sizeof(struct knet_host_defrag_buf)); } src_host->allocated_defrag_bufs = src_host->allocated_defrag_bufs * 2; src_host->defrag_bufs = new_bufs; /* * clear buffer use stats. Old ones are no good for new one */ _clear_defrag_bufs_stats(src_host); log_debug(knet_h, KNET_SUB_RX, "Defrag buffers for host %u increased from %u to: %u", src_host->host_id, src_host->allocated_defrag_bufs / 2, src_host->allocated_defrag_bufs); return 1; } return 0; } /* * this functions needs to return an index * to a knet_host_defrag_buf. (-1 on errors) */ static int _find_pckt_defrag_buf(knet_handle_t knet_h, struct knet_host *src_host, seq_num_t seq_num) { int i, oldest; uint16_t cur_allocated_defrag_bufs = src_host->allocated_defrag_bufs; /* * check if there is a buffer already in use handling the same seq_num */ for (i = 0; i < src_host->allocated_defrag_bufs; i++) { if (src_host->defrag_bufs[i].in_use) { if (src_host->defrag_bufs[i].pckt_seq == seq_num) { return i; } } } /* * If there is no buffer that's handling the current seq_num * either it's new or it's been reclaimed already. * check if it's been reclaimed/seen before using the defrag circular * buffer. If the pckt has been seen before, the buffer expired (ETIME) * and there is no point to try to defrag it again. */ if (!_seq_num_lookup(knet_h, src_host, seq_num, 1, 0)) { errno = ETIME; return -1; } /* * register the pckt as seen */ _seq_num_set(src_host, seq_num, 1); /* * see if there is a free buffer */ for (i = 0; i < src_host->allocated_defrag_bufs; i++) { if (!src_host->defrag_bufs[i].in_use) { return i; } } /* * check if we can increase num of buffers */ if (_realloc_defrag_buffers(knet_h, src_host)) { return cur_allocated_defrag_bufs + 1; } /* * at this point, there are no free buffers, the pckt is new * and we need to reclaim a buffer, and we will take the one * with the oldest timestamp. It's as good as any. */ oldest = 0; for (i = 0; i < src_host->allocated_defrag_bufs; i++) { if (_timecmp(src_host->defrag_bufs[i].last_update, src_host->defrag_bufs[oldest].last_update) < 0) { oldest = i; } } src_host->defrag_bufs[oldest].in_use = 0; return oldest; } static int _pckt_defrag(knet_handle_t knet_h, struct knet_host *src_host, seq_num_t seq_num, unsigned char *data, ssize_t *len, uint8_t frags, uint8_t frag_seq) { struct knet_host_defrag_buf *defrag_buf; int defrag_buf_idx; defrag_buf_idx = _find_pckt_defrag_buf(knet_h, src_host, seq_num); if (defrag_buf_idx < 0) { return 1; } defrag_buf = &src_host->defrag_bufs[defrag_buf_idx]; /* * if the buf is not is use, then make sure it's clean */ if (!defrag_buf->in_use) { memset(defrag_buf, 0, sizeof(struct knet_host_defrag_buf)); defrag_buf->in_use = 1; defrag_buf->pckt_seq = seq_num; } /* * update timestamp on the buffer */ clock_gettime(CLOCK_MONOTONIC, &defrag_buf->last_update); /* * check if we already received this fragment */ if (defrag_buf->frag_map[frag_seq]) { /* * if we have received this fragment and we didn't clear the buffer * it means that we don't have all fragments yet */ return 1; } /* * we need to handle the last packet with gloves due to its different size */ if (frag_seq == frags) { defrag_buf->last_frag_size = *len; /* * in the event when the last packet arrives first, * we still don't know the offset vs the other fragments (based on MTU), * so we store the fragment at the end of the buffer where it's safe * and take a copy of the len so that we can restore its offset later. * remember we can't use the local MTU for this calculation because pMTU * can be asymettric between the same hosts. */ if (!defrag_buf->frag_size) { defrag_buf->last_first = 1; memmove(defrag_buf->buf + (KNET_MAX_PACKET_SIZE - *len), data, *len); } } else { defrag_buf->frag_size = *len; } if (defrag_buf->frag_size) { memmove(defrag_buf->buf + ((frag_seq - 1) * defrag_buf->frag_size), data, *len); } defrag_buf->frag_recv++; defrag_buf->frag_map[frag_seq] = 1; /* * check if we received all the fragments */ if (defrag_buf->frag_recv == frags) { /* * special case the last pckt */ if (defrag_buf->last_first) { memmove(defrag_buf->buf + ((frags - 1) * defrag_buf->frag_size), defrag_buf->buf + (KNET_MAX_PACKET_SIZE - defrag_buf->last_frag_size), defrag_buf->last_frag_size); } /* * recalculate packet lenght */ *len = ((frags - 1) * defrag_buf->frag_size) + defrag_buf->last_frag_size; /* * copy the pckt back in the user data */ memmove(data, defrag_buf->buf, *len); /* * free this buffer */ defrag_buf->in_use = 0; return 0; } return 1; } static int _handle_data_stats(knet_handle_t knet_h, struct knet_link *src_link, ssize_t len, uint64_t decrypt_time) { int stats_err; /* data stats at the top for consistency with TX */ src_link->status.stats.rx_data_packets++; src_link->status.stats.rx_data_bytes += len; if (decrypt_time) { stats_err = pthread_mutex_lock(&knet_h->handle_stats_mutex); if (stats_err < 0) { log_err(knet_h, KNET_SUB_RX, "Unable to get mutex lock: %s", strerror(stats_err)); return -1; } /* Only update the crypto overhead for data packets. Mainly to be consistent with TX */ if (decrypt_time < knet_h->stats.rx_crypt_time_min) { knet_h->stats.rx_crypt_time_min = decrypt_time; } if (decrypt_time > knet_h->stats.rx_crypt_time_max) { knet_h->stats.rx_crypt_time_max = decrypt_time; } knet_h->stats.rx_crypt_time_ave = (knet_h->stats.rx_crypt_time_ave * knet_h->stats.rx_crypt_packets + decrypt_time) / (knet_h->stats.rx_crypt_packets+1); knet_h->stats.rx_crypt_packets++; pthread_mutex_unlock(&knet_h->handle_stats_mutex); } return 0; } static int _decompress_data(knet_handle_t knet_h, uint8_t decompress_type, unsigned char *data, ssize_t *len, ssize_t header_size) { int err = 0, stats_err = 0; if (decompress_type) { ssize_t decmp_outlen = KNET_DATABUFSIZE_COMPRESS; struct timespec start_time; struct timespec end_time; uint64_t decompress_time; clock_gettime(CLOCK_MONOTONIC, &start_time); err = decompress(knet_h, decompress_type, data, *len - header_size, knet_h->recv_from_links_buf_decompress, &decmp_outlen); clock_gettime(CLOCK_MONOTONIC, &end_time); timespec_diff(start_time, end_time, &decompress_time); stats_err = pthread_mutex_lock(&knet_h->handle_stats_mutex); if (stats_err < 0) { log_err(knet_h, KNET_SUB_RX, "Unable to get mutex lock: %s", strerror(stats_err)); return -1; } if (!err) { /* Collect stats */ if (decompress_time < knet_h->stats.rx_compress_time_min) { knet_h->stats.rx_compress_time_min = decompress_time; } if (decompress_time > knet_h->stats.rx_compress_time_max) { knet_h->stats.rx_compress_time_max = decompress_time; } knet_h->stats.rx_compress_time_ave = (knet_h->stats.rx_compress_time_ave * knet_h->stats.rx_compressed_packets + decompress_time) / (knet_h->stats.rx_compressed_packets+1); knet_h->stats.rx_compressed_packets++; knet_h->stats.rx_compressed_original_bytes += decmp_outlen; knet_h->stats.rx_compressed_size_bytes += *len - KNET_HEADER_SIZE; memmove(data, knet_h->recv_from_links_buf_decompress, decmp_outlen); *len = decmp_outlen + header_size; } else { knet_h->stats.rx_failed_to_decompress++; pthread_mutex_unlock(&knet_h->handle_stats_mutex); log_err(knet_h, KNET_SUB_COMPRESS, "Unable to decompress packet (%d): %s", err, strerror(errno)); return -1; } pthread_mutex_unlock(&knet_h->handle_stats_mutex); } return 0; } static int _check_destination(knet_handle_t knet_h, struct knet_header *inbuf, unsigned char *data, ssize_t len, ssize_t header_size, int8_t *channel) { knet_node_id_t dst_host_ids[KNET_MAX_HOST]; size_t dst_host_ids_entries = 0; int bcast = 1; size_t host_idx; int found = 0; if (knet_h->dst_host_filter_fn) { bcast = knet_h->dst_host_filter_fn( knet_h->dst_host_filter_fn_private_data, data, len - header_size, KNET_NOTIFY_RX, knet_h->host_id, inbuf->kh_node, channel, dst_host_ids, &dst_host_ids_entries); if (bcast < 0) { log_debug(knet_h, KNET_SUB_RX, "Error from dst_host_filter_fn: %d", bcast); return -1; } if ((!bcast) && (!dst_host_ids_entries)) { log_debug(knet_h, KNET_SUB_RX, "Message is unicast but no dst_host_ids_entries"); return -1; } /* check if we are dst for this packet */ if (!bcast) { if (dst_host_ids_entries > KNET_MAX_HOST) { log_debug(knet_h, KNET_SUB_RX, "dst_host_filter_fn returned too many destinations"); return -1; } for (host_idx = 0; host_idx < dst_host_ids_entries; host_idx++) { if (dst_host_ids[host_idx] == knet_h->host_id) { found = 1; break; } } if (!found) { log_debug(knet_h, KNET_SUB_RX, "Packet is not for us"); return -1; } } } return 0; } static int _deliver_data(knet_handle_t knet_h, unsigned char *data, ssize_t len, ssize_t header_size, int8_t channel) { struct iovec iov_out[1]; ssize_t outlen = 0; memset(iov_out, 0, sizeof(iov_out)); retry: iov_out[0].iov_base = (void *) data + outlen; iov_out[0].iov_len = len - (outlen + header_size); outlen = writev(knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created], iov_out, 1); if ((outlen > 0) && (outlen < (ssize_t)iov_out[0].iov_len)) { log_debug(knet_h, KNET_SUB_RX, "Unable to send all data to the application in one go. Expected: %zu Sent: %zd\n", iov_out[0].iov_len, outlen); goto retry; } if (outlen <= 0) { knet_h->sock_notify_fn(knet_h->sock_notify_fn_private_data, knet_h->sockfd[channel].sockfd[0], channel, KNET_NOTIFY_RX, outlen, errno); return -1; } if ((size_t)outlen != iov_out[0].iov_len) { return -1; } return 0; } static void _process_data(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, struct knet_header *inbuf, ssize_t len, uint64_t decrypt_time) { int8_t channel; uint8_t decompress_type = 0; ssize_t header_size; seq_num_t seq_num; uint8_t frags, frag_seq; unsigned char *data; if (_handle_data_stats(knet_h, src_link, len, decrypt_time) < 0) { return; } /* * register host is sending data. Required to determine if we need * to reset circular buffers. (see onwire_v1.c) */ src_host->got_data = 1; if (knet_h->onwire_ver_remap) { get_data_header_info_v1(knet_h, inbuf, &header_size, &channel, &seq_num, &decompress_type, &frags, &frag_seq); data = get_data_v1(knet_h, inbuf); } else { switch (inbuf->kh_version) { case 1: get_data_header_info_v1(knet_h, inbuf, &header_size, &channel, &seq_num, &decompress_type, &frags, &frag_seq); data = get_data_v1(knet_h, inbuf); break; default: log_warn(knet_h, KNET_SUB_RX, "processing data onwire version %u not supported", inbuf->kh_version); return; break; } } if (!_seq_num_lookup(knet_h, src_host, seq_num, 0, 0)) { if (src_host->link_handler_policy != KNET_LINK_POLICY_ACTIVE) { log_debug(knet_h, KNET_SUB_RX, "Packet has already been delivered"); } return; } if (frags > 1) { /* * len as received from the socket also includes extra stuff * that the defrag code doesn't care about. So strip it * here and readd only for repadding once we are done * defragging * * the defrag code assumes that data packets have all the same size * except the last one that might be smaller. * */ len = len - header_size; if (_pckt_defrag(knet_h, src_host, seq_num, data, &len, frags, frag_seq)) { return; } len = len + header_size; } if (_decompress_data(knet_h, decompress_type, data, &len, header_size) < 0) { return; } if (!src_host->status.reachable) { log_debug(knet_h, KNET_SUB_RX, "Source host %u not reachable yet. Discarding packet.", src_host->host_id); return; } if (knet_h->enabled != 1) /* data forward is disabled */ return; if (_check_destination(knet_h, inbuf, data, len, header_size, &channel) < 0) { return; } if (!knet_h->sockfd[channel].in_use) { log_debug(knet_h, KNET_SUB_RX, "received packet for channel %d but there is no local sock connected", channel); return; } #ifdef ONWIRE_V1_EXTRA_DEBUG if (inbuf->khp_data_v1_checksum != compute_chksum(data, len - header_size)) { log_err(knet_h, KNET_SUB_RX, "Received incorrect data checksum after reassembly from host: %u seq: %u", src_host->host_id, seq_num); /* * give a chance to the log threads to pick up the message */ sleep(1); abort(); } #endif if (_deliver_data(knet_h, data, len, header_size, channel) < 0) { return; } _seq_num_set(src_host, seq_num, 0); } static struct knet_header *_decrypt_packet(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *len, uint64_t *decrypt_time) { int try_decrypt = 0; int i = 0; struct timespec start_time; struct timespec end_time; ssize_t outlen; for (i = 1; i <= KNET_MAX_CRYPTO_INSTANCES; i++) { if (knet_h->crypto_instance[i]) { try_decrypt = 1; break; } } if ((!try_decrypt) && (knet_h->crypto_only == KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC)) { log_debug(knet_h, KNET_SUB_RX, "RX thread configured to accept only crypto packets, but no crypto configs are configured!"); return NULL; } if (try_decrypt) { clock_gettime(CLOCK_MONOTONIC, &start_time); if (crypto_authenticate_and_decrypt(knet_h, (unsigned char *)inbuf, *len, knet_h->recv_from_links_buf_decrypt, &outlen) < 0) { log_debug(knet_h, KNET_SUB_RX, "Unable to decrypt/auth packet"); if (knet_h->crypto_only == KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC) { return NULL; } log_debug(knet_h, KNET_SUB_RX, "Attempting to process packet as clear data"); } else { clock_gettime(CLOCK_MONOTONIC, &end_time); timespec_diff(start_time, end_time, decrypt_time); *len = outlen; inbuf = (struct knet_header *)knet_h->recv_from_links_buf_decrypt; } } return inbuf; } static int _packet_checks(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t len) { #ifdef ONWIRE_V1_EXTRA_DEBUG uint32_t rx_packet_checksum, expected_packet_checksum; #endif if (len < (ssize_t)(KNET_HEADER_SIZE + 1)) { log_debug(knet_h, KNET_SUB_RX, "Packet is too short: %ld", (long)len); return -1; } #ifdef ONWIRE_V1_EXTRA_DEBUG inbuf->kh_node = htons(inbuf->kh_node); rx_packet_checksum = inbuf->kh_checksum; inbuf->kh_checksum = 0; expected_packet_checksum = compute_chksum((const unsigned char *)inbuf, len); if (rx_packet_checksum != expected_packet_checksum) { log_err(knet_h, KNET_SUB_RX, "Received packet with incorrect checksum. Received: %u Expected: %u", rx_packet_checksum, expected_packet_checksum); /* * give a chance to the log threads to pick up the message */ sleep(1); abort(); } inbuf->kh_node = ntohs(inbuf->kh_node); #endif /* * old versions of knet did not advertise max_ver and max_ver is set to 0. */ if (!inbuf->kh_max_ver) { inbuf->kh_max_ver = 1; } /* * if the node joining max version is lower than the min version * then we reject the node */ if (inbuf->kh_max_ver < knet_h->onwire_min_ver) { log_warn(knet_h, KNET_SUB_RX, "Received packet version %u from node %u, lower than currently minimal supported onwire version. Rejecting.", inbuf->kh_version, inbuf->kh_node); return -1; } /* * if the node joining with version higher than our max version * then we reject the node */ if (inbuf->kh_version > knet_h->onwire_max_ver) { log_warn(knet_h, KNET_SUB_RX, "Received packet version %u from node %u, higher than currently maximum supported onwire version. Rejecting.", inbuf->kh_version, inbuf->kh_node); return -1; } /* * if the node joining with version lower than the current in use version * then we reject the node * * NOTE: should we make this configurable and support downgrades? */ if ((!knet_h->onwire_force_ver) && (inbuf->kh_version < knet_h->onwire_ver) && (inbuf->kh_max_ver > inbuf->kh_version)) { log_warn(knet_h, KNET_SUB_RX, "Received packet version %u from node %u, lower than currently in use onwire version. Rejecting.", inbuf->kh_version, inbuf->kh_node); return -1; } return 0; } static void _handle_dynip(knet_handle_t knet_h, struct knet_host *src_host, struct knet_link *src_link, int sockfd, const struct knet_mmsghdr *msg) { if (src_link->dynamic == KNET_LINK_DYNIP) { if (cmpaddr(&src_link->dst_addr, msg->msg_hdr.msg_name) != 0) { log_debug(knet_h, KNET_SUB_RX, "host: %u link: %u appears to have changed ip address", src_host->host_id, src_link->link_id); memmove(&src_link->dst_addr, msg->msg_hdr.msg_name, sizeof(struct sockaddr_storage)); if (knet_addrtostr(&src_link->dst_addr, sockaddr_len(&src_link->dst_addr), src_link->status.dst_ipaddr, KNET_MAX_HOST_LEN, src_link->status.dst_port, KNET_MAX_PORT_LEN) != 0) { log_debug(knet_h, KNET_SUB_RX, "Unable to resolve ???"); snprintf(src_link->status.dst_ipaddr, KNET_MAX_HOST_LEN - 1, "Unknown!!!"); snprintf(src_link->status.dst_port, KNET_MAX_PORT_LEN - 1, "??"); } else { log_info(knet_h, KNET_SUB_RX, "host: %u link: %u new connection established from: %s:%s", src_host->host_id, src_link->link_id, src_link->status.dst_ipaddr, src_link->status.dst_port); } } /* * transport has already accepted the connection here * otherwise we would not be receiving packets */ transport_link_dyn_connect(knet_h, sockfd, src_link); } } /* * processing incoming packets vs access lists */ static int _check_rx_acl(knet_handle_t knet_h, struct knet_link *src_link, const struct knet_mmsghdr *msg) { if (knet_h->use_access_lists) { if (!check_validate(knet_h, src_link, msg->msg_hdr.msg_name)) { char src_ipaddr[KNET_MAX_HOST_LEN]; char src_port[KNET_MAX_PORT_LEN]; memset(src_ipaddr, 0, KNET_MAX_HOST_LEN); memset(src_port, 0, KNET_MAX_PORT_LEN); if (knet_addrtostr(msg->msg_hdr.msg_name, sockaddr_len(msg->msg_hdr.msg_name), src_ipaddr, KNET_MAX_HOST_LEN, src_port, KNET_MAX_PORT_LEN) < 0) { log_warn(knet_h, KNET_SUB_RX, "Packet rejected: unable to resolve host/port"); } else { log_warn(knet_h, KNET_SUB_RX, "Packet rejected from %s:%s", src_ipaddr, src_port); } return 0; } } return 1; } static void _parse_recv_from_links(knet_handle_t knet_h, int sockfd, const struct knet_mmsghdr *msg) { int savederrno = 0, stats_err = 0; struct knet_host *src_host; struct knet_link *src_link; uint64_t decrypt_time = 0; struct knet_header *inbuf = msg->msg_hdr.msg_iov->iov_base; ssize_t len = msg->msg_len; int i, found_link = 0; inbuf = _decrypt_packet(knet_h, inbuf, &len, &decrypt_time); if (!inbuf) { char src_ipaddr[KNET_MAX_HOST_LEN]; char src_port[KNET_MAX_PORT_LEN]; memset(src_ipaddr, 0, KNET_MAX_HOST_LEN); memset(src_port, 0, KNET_MAX_PORT_LEN); if (knet_addrtostr(msg->msg_hdr.msg_name, sockaddr_len(msg->msg_hdr.msg_name), src_ipaddr, KNET_MAX_HOST_LEN, src_port, KNET_MAX_PORT_LEN) < 0) { log_err(knet_h, KNET_SUB_RX, "Unable to decrypt packet from unknown host/port (size %zu)!", len); } else { log_err(knet_h, KNET_SUB_RX, "Unable to decrypt packet from %s:%s (size %zu)!", src_ipaddr, src_port, len); } return; } inbuf->kh_node = ntohs(inbuf->kh_node); if (_packet_checks(knet_h, inbuf, len) < 0) { if (knet_h->rx_odd_packets < KNET_RX_ODD_PACKETS_THRESHOLD) { knet_h->rx_odd_packets++; } else { log_warn(knet_h, KNET_SUB_RX, "This node has received more than %u packets that have failed basic sanity checks", KNET_RX_ODD_PACKETS_THRESHOLD); log_warn(knet_h, KNET_SUB_RX, "It is highly recommended to check if all nodes are using the same crypto configuration"); knet_h->rx_odd_packets = 0; } return; } /* * determine source host */ src_host = knet_h->host_index[inbuf->kh_node]; if (src_host == NULL) { /* host not found */ log_debug(knet_h, KNET_SUB_RX, "Unable to find source host for this packet"); return; } /* * deteremine source link */ if (inbuf->kh_type == KNET_HEADER_TYPE_PING) { _handle_onwire_version(knet_h, src_host, inbuf); if (knet_h->onwire_ver_remap) { src_link = get_link_from_pong_v1(knet_h, src_host, inbuf); } else { switch (inbuf->kh_version) { case 1: src_link = get_link_from_pong_v1(knet_h, src_host, inbuf); break; default: log_warn(knet_h, KNET_SUB_RX, "Parsing ping onwire version %u not supported", inbuf->kh_version); return; break; } } if (!_check_rx_acl(knet_h, src_link, msg)) { return; } _handle_dynip(knet_h, src_host, src_link, sockfd, msg); } else { /* all other packets */ for (i = 0; i < KNET_MAX_LINK; i++) { src_link = &src_host->link[i]; if (cmpaddr(&src_link->dst_addr, msg->msg_hdr.msg_name) == 0) { found_link = 1; break; } } if (found_link) { /* * this check is currently redundant.. Keep it here for now */ if (!_check_rx_acl(knet_h, src_link, msg)) { return; } } else { log_debug(knet_h, KNET_SUB_RX, "Unable to determine source link for data packet. Discarding packet."); return; } } stats_err = pthread_mutex_lock(&src_link->link_stats_mutex); if (stats_err) { log_err(knet_h, KNET_SUB_RX, "Unable to get stats mutex lock for host %u link %u: %s", src_host->host_id, src_link->link_id, strerror(savederrno)); return; } switch (inbuf->kh_type) { case KNET_HEADER_TYPE_DATA: _process_data(knet_h, src_host, src_link, inbuf, len, decrypt_time); break; case KNET_HEADER_TYPE_PING: process_ping(knet_h, src_host, src_link, inbuf, len); break; case KNET_HEADER_TYPE_PONG: process_pong(knet_h, src_host, src_link, inbuf, len); break; case KNET_HEADER_TYPE_PMTUD: src_link->status.stats.rx_pmtu_packets++; src_link->status.stats.rx_pmtu_bytes += len; /* Unlock so we don't deadlock with tx_mutex */ pthread_mutex_unlock(&src_link->link_stats_mutex); process_pmtud(knet_h, src_link, inbuf); return; /* Don't need to unlock link_stats_mutex */ break; case KNET_HEADER_TYPE_PMTUD_REPLY: src_link->status.stats.rx_pmtu_packets++; src_link->status.stats.rx_pmtu_bytes += len; /* pmtud_mutex can't be acquired while we hold a link_stats_mutex (ordering) */ pthread_mutex_unlock(&src_link->link_stats_mutex); process_pmtud_reply(knet_h, src_link, inbuf); return; break; default: pthread_mutex_unlock(&src_link->link_stats_mutex); return; break; } pthread_mutex_unlock(&src_link->link_stats_mutex); } static void _handle_recv_from_links(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg) { int err, savederrno; int i, msg_recv, transport; if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { log_debug(knet_h, KNET_SUB_RX, "Unable to get global read lock"); return; } if (_is_valid_fd(knet_h, sockfd) < 1) { /* * this is normal if a fd got an event and before we grab the read lock * and the link is removed by another thread */ goto exit_unlock; } transport = knet_h->knet_transport_fd_tracker[sockfd].transport; /* * reset msg_namelen to buffer size because after recvmmsg * each msg_namelen will contain sizeof sockaddr_in or sockaddr_in6 */ for (i = 0; i < PCKT_RX_BUFS; i++) { msg[i].msg_hdr.msg_namelen = knet_h->knet_transport_fd_tracker[sockfd].sockaddr_len; } msg_recv = _recvmmsg(sockfd, &msg[0], PCKT_RX_BUFS, MSG_DONTWAIT | MSG_NOSIGNAL); savederrno = errno; /* * WARNING: man page for recvmmsg is wrong. Kernel implementation here: * recvmmsg can return: * -1 on error * 0 if the previous run of recvmmsg recorded an error on the socket * N number of messages (see exception below). * * If there is an error from recvmsg after receiving a frame or more, the recvmmsg * loop is interrupted, error recorded in the socket (getsockopt(SO_ERROR) and * it will be visibile in the next run. * * Need to be careful how we handle errors at this stage. * * error messages need to be handled on a per transport/protocol base * at this point we have different layers of error handling * - msg_recv < 0 -> error from this run * msg_recv = 0 -> error from previous run and error on socket needs to be cleared * - per-transport message data * example: msg[i].msg_hdr.msg_flags & MSG_NOTIFICATION or msg_len for SCTP == EOF, * but for UDP it is perfectly legal to receive a 0 bytes message.. go figure * - NOTE: on SCTP MSG_NOTIFICATION we get msg_recv == PCKT_FRAG_MAX messages and no * errno set. That means the error api needs to be able to abort the loop below. */ if (msg_recv <= 0) { transport_rx_sock_error(knet_h, transport, sockfd, msg_recv, savederrno); goto exit_unlock; } for (i = 0; i < msg_recv; i++) { err = transport_rx_is_data(knet_h, transport, sockfd, &msg[i]); /* * TODO: make this section silent once we are confident * all protocols packet handlers are good */ switch(err) { case KNET_TRANSPORT_RX_ISDATA_ERROR: /* on error */ log_debug(knet_h, KNET_SUB_RX, "Transport reported error parsing packet"); goto exit_unlock; break; case KNET_TRANSPORT_RX_NOT_DATA_CONTINUE: /* packet is not data and we should continue the packet process loop */ log_debug(knet_h, KNET_SUB_RX, "Transport reported no data, continue"); break; case KNET_TRANSPORT_RX_NOT_DATA_STOP: /* packet is not data and we should STOP the packet process loop */ log_debug(knet_h, KNET_SUB_RX, "Transport reported no data, stop"); goto exit_unlock; break; case KNET_TRANSPORT_RX_IS_DATA: /* packet is data and should be parsed as such */ _parse_recv_from_links(knet_h, sockfd, &msg[i]); break; case KNET_TRANSPORT_RX_OOB_DATA_CONTINUE: log_debug(knet_h, KNET_SUB_RX, "Transport is processing sock OOB data, continue"); break; case KNET_TRANSPORT_RX_OOB_DATA_STOP: log_debug(knet_h, KNET_SUB_RX, "Transport has completed processing sock OOB data, stop"); goto exit_unlock; break; } } exit_unlock: _shrink_defrag_buffers(knet_h); pthread_rwlock_unlock(&knet_h->global_rwlock); } void *_handle_recv_from_links_thread(void *data) { int i, nev; knet_handle_t knet_h = (knet_handle_t) data; struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; struct sockaddr_storage address[PCKT_RX_BUFS]; struct knet_mmsghdr msg[PCKT_RX_BUFS]; struct iovec iov_in[PCKT_RX_BUFS]; set_thread_status(knet_h, KNET_THREAD_RX, KNET_THREAD_STARTED); memset(&msg, 0, sizeof(msg)); memset(&events, 0, sizeof(events)); for (i = 0; i < PCKT_RX_BUFS; i++) { iov_in[i].iov_base = (void *)knet_h->recv_from_links_buf[i]; iov_in[i].iov_len = KNET_DATABUFSIZE; memset(&msg[i].msg_hdr, 0, sizeof(struct msghdr)); msg[i].msg_hdr.msg_name = &address[i]; msg[i].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); /* Real value filled in before actual use */ msg[i].msg_hdr.msg_iov = &iov_in[i]; msg[i].msg_hdr.msg_iovlen = 1; } while (!shutdown_in_progress(knet_h)) { nev = epoll_wait(knet_h->recv_from_links_epollfd, events, KNET_EPOLL_MAX_EVENTS, knet_h->threads_timer_res / 1000); /* * the RX threads only need to notify that there has been at least * one successful run after queue flush has been requested. * See setfwd in handle.c */ if (get_thread_flush_queue(knet_h, KNET_THREAD_RX) == KNET_THREAD_QUEUE_FLUSH) { set_thread_flush_queue(knet_h, KNET_THREAD_RX, KNET_THREAD_QUEUE_FLUSHED); } /* * we use timeout to detect if thread is shutting down */ if (nev == 0) { continue; } for (i = 0; i < nev; i++) { _handle_recv_from_links(knet_h, events[i].data.fd, msg); } } set_thread_status(knet_h, KNET_THREAD_RX, KNET_THREAD_STOPPED); return NULL; } ssize_t knet_recv(knet_handle_t knet_h, char *buff, const size_t buff_len, const int8_t channel) { int savederrno = 0; ssize_t err = 0; struct iovec iov_in; if (!_is_valid_handle(knet_h)) { return -1; } if (buff == NULL) { errno = EINVAL; return -1; } if (buff_len <= 0) { errno = EINVAL; return -1; } if (buff_len > KNET_MAX_PACKET_SIZE) { errno = EINVAL; return -1; } if (channel < 0) { errno = EINVAL; return -1; } if (channel >= KNET_DATAFD_MAX) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->sockfd[channel].in_use) { savederrno = EINVAL; err = -1; goto out_unlock; } memset(&iov_in, 0, sizeof(iov_in)); iov_in.iov_base = (void *)buff; iov_in.iov_len = buff_len; err = readv(knet_h->sockfd[channel].sockfd[0], &iov_in, 1); savederrno = errno; out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } diff --git a/libknet/threads_rx.h b/libknet/threads_rx.h index f21679cf..6115bcb7 100644 --- a/libknet/threads_rx.h +++ b/libknet/threads_rx.h @@ -1,15 +1,15 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_THREADS_RX_H__ #define __KNET_THREADS_RX_H__ void *_handle_recv_from_links_thread(void *data); #endif diff --git a/libknet/threads_tx.c b/libknet/threads_tx.c index 8c285e0f..fcf5915e 100644 --- a/libknet/threads_tx.c +++ b/libknet/threads_tx.c @@ -1,999 +1,999 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "compat.h" #include "compress.h" #include "crypto.h" #include "host.h" #include "link.h" #include "logging.h" #include "transports.h" #include "transport_common.h" #include "threads_common.h" #include "threads_heartbeat.h" #include "threads_tx.h" #include "netutils.h" #include "onwire_v1.h" /* * SEND */ static int _dispatch_to_links(knet_handle_t knet_h, struct knet_host *dst_host, struct knet_mmsghdr *msg, int msgs_to_send) { int link_idx, msg_idx, sent_msgs, prev_sent, progress; int err = 0, savederrno = 0, locked = 0; unsigned int i; struct knet_mmsghdr *cur; struct knet_link *cur_link; for (link_idx = 0; link_idx < dst_host->active_link_entries; link_idx++) { prev_sent = 0; progress = 1; locked = 0; cur_link = &dst_host->link[dst_host->active_links[link_idx]]; if (cur_link->transport == KNET_TRANSPORT_LOOPBACK) { continue; } savederrno = pthread_mutex_lock(&cur_link->link_stats_mutex); if (savederrno) { log_err(knet_h, KNET_SUB_TX, "Unable to get stats mutex lock for host %u link %u: %s", dst_host->host_id, cur_link->link_id, strerror(savederrno)); continue; } locked = 1; msg_idx = 0; while (msg_idx < msgs_to_send) { msg[msg_idx].msg_hdr.msg_name = &cur_link->dst_addr; msg[msg_idx].msg_hdr.msg_namelen = knet_h->knet_transport_fd_tracker[cur_link->outsock].sockaddr_len; /* Cast for Linux/BSD compatibility */ for (i=0; i<(unsigned int)msg[msg_idx].msg_hdr.msg_iovlen; i++) { cur_link->status.stats.tx_data_bytes += msg[msg_idx].msg_hdr.msg_iov[i].iov_len; } cur_link->status.stats.tx_data_packets++; msg_idx++; } retry: cur = &msg[prev_sent]; sent_msgs = _sendmmsg(dst_host->link[dst_host->active_links[link_idx]].outsock, transport_get_connection_oriented(knet_h, dst_host->link[dst_host->active_links[link_idx]].transport), &cur[0], msgs_to_send - prev_sent, MSG_DONTWAIT | MSG_NOSIGNAL); savederrno = errno; err = transport_tx_sock_error(knet_h, dst_host->link[dst_host->active_links[link_idx]].transport, dst_host->link[dst_host->active_links[link_idx]].outsock, KNET_SUB_TX, sent_msgs, savederrno); switch(err) { case KNET_TRANSPORT_SOCK_ERROR_INTERNAL: cur_link->status.stats.tx_data_errors++; goto out_unlock; break; case KNET_TRANSPORT_SOCK_ERROR_IGNORE: break; case KNET_TRANSPORT_SOCK_ERROR_RETRY: cur_link->status.stats.tx_data_retries++; goto retry; break; } prev_sent = prev_sent + sent_msgs; if ((sent_msgs >= 0) && (prev_sent < msgs_to_send)) { if ((sent_msgs) || (progress)) { if (sent_msgs) { progress = 1; } else { progress = 0; } #ifdef DEBUG log_debug(knet_h, KNET_SUB_TX, "Unable to send all (%d/%d) data packets to host %s (%u) link %s:%s (%u)", sent_msgs, msg_idx, dst_host->name, dst_host->host_id, dst_host->link[dst_host->active_links[link_idx]].status.dst_ipaddr, dst_host->link[dst_host->active_links[link_idx]].status.dst_port, dst_host->link[dst_host->active_links[link_idx]].link_id); #endif goto retry; } if (!progress) { savederrno = EAGAIN; err = -1; goto out_unlock; } } if ((dst_host->link_handler_policy == KNET_LINK_POLICY_RR) && (dst_host->active_link_entries > 1)) { uint8_t cur_link_id = dst_host->active_links[0]; memmove(&dst_host->active_links[0], &dst_host->active_links[1], KNET_MAX_LINK - 1); dst_host->active_links[dst_host->active_link_entries - 1] = cur_link_id; break; } pthread_mutex_unlock(&cur_link->link_stats_mutex); locked = 0; } out_unlock: if (locked) { pthread_mutex_unlock(&cur_link->link_stats_mutex); } errno = savederrno; return err; } static int _dispatch_to_local(knet_handle_t knet_h, unsigned char *data, size_t inlen, int8_t channel) { int err = 0, savederrno = 0; const unsigned char *buf = data; ssize_t buflen = inlen; struct knet_link *local_link = knet_h->host_index[knet_h->host_id]->link; local_retry: err = write(knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created], buf, buflen); savederrno = errno; if (err < 0) { log_err(knet_h, KNET_SUB_TRANSP_LOOPBACK, "send local failed. error=%s\n", strerror(errno)); local_link->status.stats.tx_data_errors++; goto out; } if (err > 0 && err < buflen) { log_debug(knet_h, KNET_SUB_TRANSP_LOOPBACK, "send local incomplete=%d bytes of %zu\n", err, inlen); local_link->status.stats.tx_data_retries++; buf += err; buflen -= err; goto local_retry; } if (err == buflen) { local_link->status.stats.tx_data_packets++; local_link->status.stats.tx_data_bytes += inlen; } out: errno = savederrno; return err; } static int _prep_tx_bufs(knet_handle_t knet_h, struct knet_header *inbuf, uint8_t onwire_ver, unsigned char *data, size_t inlen, uint32_t data_checksum, seq_num_t tx_seq_num, int8_t channel, int bcast, int data_compressed, int *msgs_to_send, struct iovec iov_out[PCKT_FRAG_MAX][2], int *iovcnt_out) { int err = 0, savederrno = 0; unsigned int temp_data_mtu; if (!knet_h->data_mtu) { /* * using MIN_MTU_V4 for data mtu is not completely accurate but safe enough */ log_debug(knet_h, KNET_SUB_TX, "Received data packet but data MTU is still unknown." " Packet might not be delivered." " Assuming minimum IPv4 MTU (%d)", KNET_PMTUD_MIN_MTU_V4); temp_data_mtu = KNET_PMTUD_MIN_MTU_V4; } else { /* * take a copy of the mtu to avoid value changing under * our feet while we are sending a fragmented pckt */ temp_data_mtu = knet_h->data_mtu; } if (knet_h->onwire_ver_remap) { prep_tx_bufs_v1(knet_h, inbuf, data, inlen, data_checksum, temp_data_mtu, tx_seq_num, channel, bcast, data_compressed, msgs_to_send, iov_out, iovcnt_out); } else { switch (onwire_ver) { case 1: prep_tx_bufs_v1(knet_h, inbuf, data, inlen, data_checksum, temp_data_mtu, tx_seq_num, channel, bcast, data_compressed, msgs_to_send, iov_out, iovcnt_out); break; default: /* this should never hit as filters are in place in the calling functions */ log_warn(knet_h, KNET_SUB_TX, "preparing data onwire version %u not supported", onwire_ver); savederrno = EINVAL; err = -1; goto out; break; } } out: errno = savederrno; return err; } static int _compress_data(knet_handle_t knet_h, unsigned char* data, size_t *inlen, int *data_compressed) { int err = 0, savederrno = 0; int stats_locked = 0, stats_err = 0; size_t cmp_outlen = KNET_DATABUFSIZE_COMPRESS; struct timespec start_time; struct timespec end_time; uint64_t compress_time; /* * compress data */ if (knet_h->compress_model > 0) { if (*inlen > knet_h->compress_threshold) { clock_gettime(CLOCK_MONOTONIC, &start_time); err = compress(knet_h, data, *inlen, knet_h->send_to_links_buf_compress, (ssize_t *)&cmp_outlen); savederrno = errno; clock_gettime(CLOCK_MONOTONIC, &end_time); timespec_diff(start_time, end_time, &compress_time); stats_err = pthread_mutex_lock(&knet_h->handle_stats_mutex); if (stats_err < 0) { log_err(knet_h, KNET_SUB_TX, "Unable to get mutex lock: %s", strerror(stats_err)); err = -1; savederrno = stats_err; goto out; } stats_locked = 1; /* Collect stats */ if (compress_time < knet_h->stats.tx_compress_time_min) { knet_h->stats.tx_compress_time_min = compress_time; } if (compress_time > knet_h->stats.tx_compress_time_max) { knet_h->stats.tx_compress_time_max = compress_time; } knet_h->stats.tx_compress_time_ave = (unsigned long long)(knet_h->stats.tx_compress_time_ave * knet_h->stats.tx_compressed_packets + compress_time) / (knet_h->stats.tx_compressed_packets+1); if (err < 0) { knet_h->stats.tx_failed_to_compress++; log_warn(knet_h, KNET_SUB_COMPRESS, "Compression failed (%d): %s", err, strerror(savederrno)); } else { knet_h->stats.tx_compressed_packets++; knet_h->stats.tx_compressed_original_bytes += *inlen; knet_h->stats.tx_compressed_size_bytes += cmp_outlen; if (cmp_outlen < *inlen) { memmove(data, knet_h->send_to_links_buf_compress, cmp_outlen); *inlen = cmp_outlen; *data_compressed = 1; } else { knet_h->stats.tx_unable_to_compress++; } } } if (!*data_compressed) { if (!stats_locked) { stats_err = pthread_mutex_lock(&knet_h->handle_stats_mutex); if (stats_err < 0) { log_err(knet_h, KNET_SUB_TX, "Unable to get mutex lock: %s", strerror(stats_err)); err = -1; savederrno = stats_err; goto out; } stats_locked = 1; } knet_h->stats.tx_uncompressed_packets++; } if (stats_locked) { pthread_mutex_unlock(&knet_h->handle_stats_mutex); } } out: errno = savederrno; return err; } static int _encrypt_bufs(knet_handle_t knet_h, int msgs_to_send, struct iovec iov_out[PCKT_FRAG_MAX][2], int *iovcnt_out) { int err = 0, savederrno = 0, stats_err = 0; struct timespec start_time; struct timespec end_time; uint64_t crypt_time; uint8_t frag_idx = 0; size_t outlen, uncrypted_frag_size; int j; if (knet_h->crypto_in_use_config) { while (frag_idx < msgs_to_send) { clock_gettime(CLOCK_MONOTONIC, &start_time); if (crypto_encrypt_and_signv( knet_h, iov_out[frag_idx], *iovcnt_out, knet_h->send_to_links_buf_crypt[frag_idx], (ssize_t *)&outlen) < 0) { log_debug(knet_h, KNET_SUB_TX, "Unable to encrypt packet"); savederrno = ECHILD; err = -1; goto out; } clock_gettime(CLOCK_MONOTONIC, &end_time); timespec_diff(start_time, end_time, &crypt_time); stats_err = pthread_mutex_lock(&knet_h->handle_stats_mutex); if (stats_err < 0) { log_err(knet_h, KNET_SUB_TX, "Unable to get mutex lock: %s", strerror(stats_err)); err = -1; savederrno = stats_err; goto out; } if (crypt_time < knet_h->stats.tx_crypt_time_min) { knet_h->stats.tx_crypt_time_min = crypt_time; } if (crypt_time > knet_h->stats.tx_crypt_time_max) { knet_h->stats.tx_crypt_time_max = crypt_time; } knet_h->stats.tx_crypt_time_ave = (knet_h->stats.tx_crypt_time_ave * knet_h->stats.tx_crypt_packets + crypt_time) / (knet_h->stats.tx_crypt_packets+1); uncrypted_frag_size = 0; for (j=0; j < *iovcnt_out; j++) { uncrypted_frag_size += iov_out[frag_idx][j].iov_len; } knet_h->stats.tx_crypt_byte_overhead += (outlen - uncrypted_frag_size); knet_h->stats.tx_crypt_packets++; pthread_mutex_unlock(&knet_h->handle_stats_mutex); iov_out[frag_idx][0].iov_base = knet_h->send_to_links_buf_crypt[frag_idx]; iov_out[frag_idx][0].iov_len = outlen; frag_idx++; } *iovcnt_out = 1; } out: errno = savederrno; return err; } static int _get_tx_seq_num(knet_handle_t knet_h, seq_num_t *tx_seq_num) { int savederrno = 0; savederrno = pthread_mutex_lock(&knet_h->tx_seq_num_mutex); if (savederrno) { log_debug(knet_h, KNET_SUB_TX, "Unable to get seq mutex lock"); errno = savederrno; return -1; } knet_h->tx_seq_num++; /* * force seq_num 0 to detect a node that has crashed and rejoining * the knet instance. seq_num 0 will clear the buffers in the RX * thread */ if (knet_h->tx_seq_num == 0) { knet_h->tx_seq_num++; } /* * cache the value in locked context */ *tx_seq_num = knet_h->tx_seq_num; pthread_mutex_unlock(&knet_h->tx_seq_num_mutex); /* * forcefully broadcast a ping to all nodes every SEQ_MAX / 8 * pckts. * this solves 2 problems: * 1) on TX socket overloads we generate extra pings to keep links alive * 2) in 3+ nodes setup, where all the traffic is flowing between node 1 and 2, * node 3+ will be able to keep in sync on the TX seq_num even without * receiving traffic or pings in betweens. This avoids issues with * rollover of the circular buffer */ if (*tx_seq_num % (SEQ_MAX / 8) == 0) { _send_pings(knet_h, 0); } return 0; } static int _get_data_dests(knet_handle_t knet_h, unsigned char* data, size_t inlen, int8_t *channel, int *bcast, int *send_local, knet_node_id_t *dst_host_ids, size_t *dst_host_ids_entries, int is_sync) { int err = 0, savederrno = 0; knet_node_id_t dst_host_ids_temp[KNET_MAX_HOST]; /* store destinations from filter */ size_t dst_host_ids_entries_temp = 0; size_t dst_host_ids_entries_temp2 = 0; /* workaround gcc here */ struct knet_host *dst_host; size_t host_idx; memset(dst_host_ids_temp, 0, sizeof(dst_host_ids_temp)); if (knet_h->dst_host_filter_fn) { *bcast = knet_h->dst_host_filter_fn( knet_h->dst_host_filter_fn_private_data, data, inlen, KNET_NOTIFY_TX, knet_h->host_id, knet_h->host_id, channel, dst_host_ids_temp, &dst_host_ids_entries_temp); if (*bcast < 0) { log_debug(knet_h, KNET_SUB_TX, "Error from dst_host_filter_fn: %d", *bcast); savederrno = EFAULT; err = -1; goto out; } if ((!*bcast) && (!dst_host_ids_entries_temp)) { log_debug(knet_h, KNET_SUB_TX, "Message is unicast but no dst_host_ids_entries"); savederrno = EINVAL; err = -1; goto out; } if ((!*bcast) && (dst_host_ids_entries_temp > KNET_MAX_HOST)) { log_debug(knet_h, KNET_SUB_TX, "dst_host_filter_fn returned too many destinations"); savederrno = EINVAL; err = -1; goto out; } if (is_sync) { if ((*bcast) || ((!*bcast) && (dst_host_ids_entries_temp > 1))) { log_debug(knet_h, KNET_SUB_TX, "knet_send_sync is only supported with unicast packets for one destination"); savederrno = E2BIG; err = -1; goto out; } } } /* * check destinations hosts before spending time * in fragmenting/encrypting packets to save * time processing data for unreachable hosts. * for unicast, also remap the destination data * to skip unreachable hosts. */ if (!*bcast) { *dst_host_ids_entries = dst_host_ids_entries_temp2; for (host_idx = 0; host_idx < dst_host_ids_entries_temp; host_idx++) { dst_host = knet_h->host_index[dst_host_ids_temp[host_idx]]; if (!dst_host) { continue; } if ((dst_host->host_id == knet_h->host_id) && (knet_h->has_loop_link)) { *send_local = 1; } if (!((dst_host->host_id == knet_h->host_id) && (knet_h->has_loop_link)) && dst_host->status.reachable) { dst_host_ids[dst_host_ids_entries_temp2] = dst_host_ids_temp[host_idx]; dst_host_ids_entries_temp2++; } } if ((!dst_host_ids_entries_temp2) && (!*send_local)) { savederrno = EHOSTDOWN; err = -1; goto out; } *dst_host_ids_entries = dst_host_ids_entries_temp2; } else { *bcast = 0; *send_local = 0; for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { if ((dst_host->host_id == knet_h->host_id) && (knet_h->has_loop_link)) { *send_local = 1; } if (!(dst_host->host_id == knet_h->host_id && knet_h->has_loop_link) && dst_host->status.reachable) { *bcast = 1; } } if ((!*bcast) && (!*send_local)) { savederrno = EHOSTDOWN; err = -1; goto out; } } out: errno = savederrno; return err; } static int _prep_and_send_msgs(knet_handle_t knet_h, int bcast, knet_node_id_t *dst_host_ids, size_t dst_host_ids_entries, int msgs_to_send, struct iovec iov_out[PCKT_FRAG_MAX][2], int iovcnt_out) { int err = 0, savederrno = 0; struct knet_host *dst_host; struct knet_mmsghdr msg[PCKT_FRAG_MAX]; int msg_idx; size_t host_idx; memset(&msg, 0, sizeof(msg)); msg_idx = 0; while (msg_idx < msgs_to_send) { msg[msg_idx].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); /* this will set properly in _dispatch_to_links() */ msg[msg_idx].msg_hdr.msg_iov = &iov_out[msg_idx][0]; msg[msg_idx].msg_hdr.msg_iovlen = iovcnt_out; msg_idx++; } if (!bcast) { for (host_idx = 0; host_idx < dst_host_ids_entries; host_idx++) { dst_host = knet_h->host_index[dst_host_ids[host_idx]]; err = _dispatch_to_links(knet_h, dst_host, &msg[0], msgs_to_send); savederrno = errno; if (err) { goto out; } } } else { for (dst_host = knet_h->host_head; dst_host != NULL; dst_host = dst_host->next) { if (dst_host->status.reachable) { err = _dispatch_to_links(knet_h, dst_host, &msg[0], msgs_to_send); savederrno = errno; if (err) { goto out; } } } } out: errno = savederrno; return err; } static int _parse_recv_from_sock(knet_handle_t knet_h, size_t inlen, int8_t channel, uint8_t onwire_ver, int is_sync) { int err = 0, savederrno = 0; struct knet_header *inbuf = knet_h->recv_from_sock_buf; /* all TX packets are stored here regardless of the onwire */ unsigned char *data; /* onwire neutrual pointer to data to send */ int data_compressed = 0; /* track data compression to fill the header */ seq_num_t tx_seq_num; uint32_t data_checksum = 0; /* used only for debugging at the moment */ int bcast = 1; /* assume all packets are to be broadcasted unless filter tells us differently */ knet_node_id_t dst_host_ids[KNET_MAX_HOST]; /* store destinations from filter */ size_t dst_host_ids_entries = 0; int send_local = 0; /* send packets to loopback */ struct iovec iov_out[PCKT_FRAG_MAX][2]; int iovcnt_out = 2; int msgs_to_send = 0; if (knet_h->enabled != 1) { log_debug(knet_h, KNET_SUB_TX, "Received data packet but forwarding is disabled"); savederrno = ECANCELED; err = -1; goto out; } if (knet_h->onwire_ver_remap) { data = get_data_v1(knet_h, inbuf); } else { switch (onwire_ver) { case 1: data = get_data_v1(knet_h, inbuf); break; default: /* this should never hit as filters are in place in the calling functions */ log_warn(knet_h, KNET_SUB_TX, "preparing data onwire version %u not supported", onwire_ver); savederrno = EINVAL; err = -1; goto out; break; } } #ifdef ONWIRE_V1_EXTRA_DEBUG data_checksum = compute_chksum(data, inlen); #endif memset(dst_host_ids, 0, sizeof(dst_host_ids)); err = _get_data_dests(knet_h, data, inlen, &channel, &bcast, &send_local, dst_host_ids, &dst_host_ids_entries, is_sync); if (err < 0) { savederrno = errno; goto out; } /* Send to localhost if appropriate and enabled */ if (send_local) { err = _dispatch_to_local(knet_h, data, inlen, channel); if (err < 0) { savederrno = errno; goto out; } } err = _compress_data(knet_h, data, &inlen, &data_compressed); if (err < 0) { savederrno = errno; goto out; } err = _get_tx_seq_num(knet_h, &tx_seq_num); if (err < 0) { savederrno = errno; goto out; } err = _prep_tx_bufs(knet_h, inbuf, onwire_ver, data, inlen, data_checksum, tx_seq_num, channel, bcast, data_compressed, &msgs_to_send, iov_out, &iovcnt_out); if (err < 0) { savederrno = errno; goto out; } err = _encrypt_bufs(knet_h, msgs_to_send, iov_out, &iovcnt_out); if (err < 0) { savederrno = errno; goto out; } err = _prep_and_send_msgs(knet_h, bcast, dst_host_ids, dst_host_ids_entries, msgs_to_send, iov_out, iovcnt_out); if (err < 0) { savederrno = errno; goto out; } out: errno = savederrno; return err; } static void _handle_send_to_links(knet_handle_t knet_h, int sockfd, uint8_t onwire_ver, int8_t channel) { ssize_t inlen = 0; int savederrno = 0, docallback = 0; struct iovec iov_in; struct msghdr msg; struct sockaddr_storage address; memset(&iov_in, 0, sizeof(iov_in)); if (knet_h->onwire_ver_remap) { iov_in.iov_base = (void *)get_data_v1(knet_h, knet_h->recv_from_sock_buf); iov_in.iov_len = KNET_MAX_PACKET_SIZE; } else { switch (onwire_ver) { case 1: iov_in.iov_base = (void *)get_data_v1(knet_h, knet_h->recv_from_sock_buf); iov_in.iov_len = KNET_MAX_PACKET_SIZE; break; default: log_warn(knet_h, KNET_SUB_TX, "preparing data onwire version %u not supported", onwire_ver); break; } } memset(&msg, 0, sizeof(struct msghdr)); msg.msg_name = &address; msg.msg_namelen = knet_h->knet_transport_fd_tracker[sockfd].sockaddr_len; msg.msg_iov = &iov_in; msg.msg_iovlen = 1; if ((channel >= 0) && (channel < KNET_DATAFD_MAX) && (!knet_h->sockfd[channel].is_socket)) { inlen = readv(sockfd, msg.msg_iov, 1); } else { inlen = recvmsg(sockfd, &msg, MSG_DONTWAIT | MSG_NOSIGNAL); if (msg.msg_flags & MSG_TRUNC) { log_warn(knet_h, KNET_SUB_TX, "Received truncated message from sock %d. Discarding", sockfd); return; } } if (inlen == 0) { savederrno = 0; docallback = 1; } else if (inlen < 0) { struct epoll_event ev; savederrno = errno; docallback = 1; memset(&ev, 0, sizeof(struct epoll_event)); if (epoll_ctl(knet_h->send_to_links_epollfd, EPOLL_CTL_DEL, knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created], &ev)) { log_err(knet_h, KNET_SUB_TX, "Unable to del datafd %d from linkfd epoll pool: %s", knet_h->sockfd[channel].sockfd[0], strerror(savederrno)); } else { knet_h->sockfd[channel].has_error = 1; } } else { _parse_recv_from_sock(knet_h, inlen, channel, onwire_ver, 0); } if (docallback) { knet_h->sock_notify_fn(knet_h->sock_notify_fn_private_data, knet_h->sockfd[channel].sockfd[0], channel, KNET_NOTIFY_TX, inlen, savederrno); } } void *_handle_send_to_links_thread(void *data) { knet_handle_t knet_h = (knet_handle_t) data; struct epoll_event events[KNET_EPOLL_MAX_EVENTS + 1]; /* see _init_epolls for + 1 */ int i, nev; int flush, flush_queue_limit; int8_t channel; uint8_t onwire_ver; set_thread_status(knet_h, KNET_THREAD_TX, KNET_THREAD_STARTED); memset(&events, 0, sizeof(events)); flush_queue_limit = 0; while (!shutdown_in_progress(knet_h)) { nev = epoll_wait(knet_h->send_to_links_epollfd, events, KNET_EPOLL_MAX_EVENTS + 1, knet_h->threads_timer_res / 1000); flush = get_thread_flush_queue(knet_h, KNET_THREAD_TX); /* * we use timeout to detect if thread is shutting down */ if (nev == 0) { /* * ideally we want to communicate that we are done flushing * the queue when we have an epoll timeout event */ if (flush == KNET_THREAD_QUEUE_FLUSH) { set_thread_flush_queue(knet_h, KNET_THREAD_TX, KNET_THREAD_QUEUE_FLUSHED); flush_queue_limit = 0; } continue; } /* * fall back in case the TX sockets will continue receive traffic * and we do not hit an epoll timeout. * * allow up to a 100 loops to flush queues, then we give up. * there might be more clean ways to do it by checking the buffer queue * on each socket, but we have tons of sockets and calculations can go wrong. * Also, why would you disable data forwarding and still send packets? */ if (flush == KNET_THREAD_QUEUE_FLUSH) { if (flush_queue_limit >= 100) { log_debug(knet_h, KNET_SUB_TX, "Timeout flushing the TX queue, expect packet loss"); set_thread_flush_queue(knet_h, KNET_THREAD_TX, KNET_THREAD_QUEUE_FLUSHED); flush_queue_limit = 0; } else { flush_queue_limit++; } } else { flush_queue_limit = 0; } if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { log_debug(knet_h, KNET_SUB_TX, "Unable to get read lock"); continue; } if (pthread_mutex_lock(&knet_h->onwire_mutex)) { log_debug(knet_h, KNET_SUB_TX, "Unable to get onwire mutex lock"); goto out_unlock; } onwire_ver = knet_h->onwire_ver; pthread_mutex_unlock(&knet_h->onwire_mutex); for (i = 0; i < nev; i++) { for (channel = 0; channel < KNET_DATAFD_MAX; channel++) { if ((knet_h->sockfd[channel].in_use) && (knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created] == events[i].data.fd)) { break; } } if (channel >= KNET_DATAFD_MAX) { log_debug(knet_h, KNET_SUB_TX, "No available channels"); continue; /* channel not found */ } if (pthread_mutex_lock(&knet_h->tx_mutex) != 0) { log_debug(knet_h, KNET_SUB_TX, "Unable to get mutex lock"); continue; } _handle_send_to_links(knet_h, events[i].data.fd, onwire_ver, channel); pthread_mutex_unlock(&knet_h->tx_mutex); } out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); } set_thread_status(knet_h, KNET_THREAD_TX, KNET_THREAD_STOPPED); return NULL; } int knet_send_sync(knet_handle_t knet_h, const char *buff, const size_t buff_len, const int8_t channel) { int savederrno = 0, err = 0; uint8_t onwire_ver; if (!_is_valid_handle(knet_h)) { return -1; } if (buff == NULL) { errno = EINVAL; return -1; } if (buff_len <= 0) { errno = EINVAL; return -1; } if (buff_len > KNET_MAX_PACKET_SIZE) { errno = EINVAL; return -1; } if (channel < 0) { errno = EINVAL; return -1; } if (channel >= KNET_DATAFD_MAX) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_TX, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->dst_host_filter_fn) { savederrno = ENETDOWN; err = -1; goto out; } if (!knet_h->sockfd[channel].in_use) { savederrno = EINVAL; err = -1; goto out; } if (pthread_mutex_lock(&knet_h->onwire_mutex)) { log_debug(knet_h, KNET_SUB_TX, "Unable to get onwire mutex lock"); goto out; } onwire_ver = knet_h->onwire_ver; pthread_mutex_unlock(&knet_h->onwire_mutex); savederrno = pthread_mutex_lock(&knet_h->tx_mutex); if (savederrno) { log_err(knet_h, KNET_SUB_TX, "Unable to get TX mutex lock: %s", strerror(savederrno)); err = -1; goto out; } if (knet_h->onwire_ver_remap) { memmove(get_data_v1(knet_h, knet_h->recv_from_sock_buf), buff, buff_len); } else { switch (onwire_ver) { case 1: memmove(get_data_v1(knet_h, knet_h->recv_from_sock_buf), buff, buff_len); break; default: log_warn(knet_h, KNET_SUB_TX, "preparing sync data onwire version %u not supported", onwire_ver); goto out_tx; break; } } err = _parse_recv_from_sock(knet_h, buff_len, channel, onwire_ver, 1); savederrno = errno; out_tx: pthread_mutex_unlock(&knet_h->tx_mutex); out: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } ssize_t knet_send(knet_handle_t knet_h, const char *buff, const size_t buff_len, const int8_t channel) { int savederrno = 0; ssize_t err = 0; struct iovec iov_out[1]; if (!_is_valid_handle(knet_h)) { return -1; } if (buff == NULL) { errno = EINVAL; return -1; } if (buff_len <= 0) { errno = EINVAL; return -1; } if (buff_len > KNET_MAX_PACKET_SIZE) { errno = EINVAL; return -1; } if (channel < 0) { errno = EINVAL; return -1; } if (channel >= KNET_DATAFD_MAX) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } if (!knet_h->sockfd[channel].in_use) { savederrno = EINVAL; err = -1; goto out_unlock; } memset(iov_out, 0, sizeof(iov_out)); iov_out[0].iov_base = (void *)buff; iov_out[0].iov_len = buff_len; err = writev(knet_h->sockfd[channel].sockfd[0], iov_out, 1); savederrno = errno; out_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); errno = err ? savederrno : 0; return err; } diff --git a/libknet/threads_tx.h b/libknet/threads_tx.h index 3115e859..f2beea48 100644 --- a/libknet/threads_tx.h +++ b/libknet/threads_tx.h @@ -1,15 +1,15 @@ /* - * Copyright (C) 2012-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_THREADS_TX_H__ #define __KNET_THREADS_TX_H__ void *_handle_send_to_links_thread(void *data); #endif diff --git a/libknet/transport_common.c b/libknet/transport_common.c index feee70c4..47ddd887 100644 --- a/libknet/transport_common.c +++ b/libknet/transport_common.c @@ -1,447 +1,447 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "libknet.h" #include "compat.h" #include "host.h" #include "link.h" #include "logging.h" #include "common.h" #include "transport_common.h" /* * reuse Jan Friesse's compat layer as wrapper to drop usage of sendmmsg * * TODO: kill those wrappers once we work on packet delivery guarantees */ int _recvmmsg(int sockfd, struct knet_mmsghdr *msgvec, unsigned int vlen, unsigned int flags) { int savederrno = 0, err = 0; unsigned int i; for (i = 0; i < vlen; i++) { err = recvmsg(sockfd, &msgvec[i].msg_hdr, flags); savederrno = errno; if (err >= 0) { msgvec[i].msg_len = err; if (err == 0) { /* No point in reading anything more until we know this has been dealt with or we'll just get a vector full of them. Several in fact */ i++; break; } } else { if ((i > 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) { savederrno = 0; } break; } } errno = savederrno; return ((i > 0) ? (int)i : err); } int _sendmmsg(int sockfd, int connection_oriented, struct knet_mmsghdr *msgvec, unsigned int vlen, unsigned int flags) { int savederrno = 0, err = 0; unsigned int i; struct msghdr temp_msg; struct msghdr *use_msghdr; for (i = 0; i < vlen; i++) { if (connection_oriented == TRANSPORT_PROTO_IS_CONNECTION_ORIENTED) { memcpy(&temp_msg, &msgvec[i].msg_hdr, sizeof(struct msghdr)); temp_msg.msg_name = NULL; temp_msg.msg_namelen = 0; use_msghdr = &temp_msg; } else { use_msghdr = &msgvec[i].msg_hdr; } err = sendmsg(sockfd, use_msghdr, flags); savederrno = errno; if (err < 0) { break; } } errno = savederrno; return ((i > 0) ? (int)i : err); } /* Assume neither of these constants can ever be zero */ #ifndef SO_RCVBUFFORCE #define SO_RCVBUFFORCE 0 #endif #ifndef SO_SNDBUFFORCE #define SO_SNDBUFFORCE 0 #endif static int _configure_sockbuf(knet_handle_t knet_h, int sock, int option, int force, int target) { int savederrno = 0; int new_value; socklen_t value_len = sizeof new_value; if (setsockopt(sock, SOL_SOCKET, option, &target, sizeof target) != 0) { savederrno = errno; log_err(knet_h, KNET_SUB_TRANSPORT, "Error setting socket buffer via option %d to value %d: %s\n", option, target, strerror(savederrno)); errno = savederrno; return -1; } if (getsockopt(sock, SOL_SOCKET, option, &new_value, &value_len) != 0) { savederrno = errno; log_err(knet_h, KNET_SUB_TRANSPORT, "Error getting socket buffer via option %d: %s\n", option, strerror(savederrno)); errno = savederrno; return -1; } if (value_len != sizeof new_value) { log_err(knet_h, KNET_SUB_TRANSPORT, "Socket option %d returned unexpected size %u\n", option, value_len); errno = ERANGE; return -1; } if (target <= new_value) { return 0; } if (!force || !(knet_h->flags & KNET_HANDLE_FLAG_PRIVILEGED)) { log_err(knet_h, KNET_SUB_TRANSPORT, "Failed to set socket buffer via option %d to value %d: capped at %d", option, target, new_value); if (!(knet_h->flags & KNET_HANDLE_FLAG_PRIVILEGED)) { log_err(knet_h, KNET_SUB_TRANSPORT, "Continuing regardless, as the handle is not privileged." " Expect poor performance!"); return 0; } else { errno = ENAMETOOLONG; return -1; } } if (setsockopt(sock, SOL_SOCKET, force, &target, sizeof target) < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_TRANSPORT, "Failed to set socket buffer via force option %d: %s", force, strerror(savederrno)); if (savederrno == EPERM) { errno = ENAMETOOLONG; } else { errno = savederrno; } return -1; } return 0; } int _configure_common_socket(knet_handle_t knet_h, int sock, uint64_t flags, const char *type) { int err = 0, savederrno = 0; int value; if (_fdset_cloexec(sock)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s CLOEXEC socket opts: %s", type, strerror(savederrno)); goto exit_error; } if (_fdset_nonblock(sock)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s NONBLOCK socket opts: %s", type, strerror(savederrno)); goto exit_error; } if (_configure_sockbuf(knet_h, sock, SO_RCVBUF, SO_RCVBUFFORCE, KNET_RING_RCVBUFF)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s receive buffer: %s", type, strerror(savederrno)); goto exit_error; } if (_configure_sockbuf(knet_h, sock, SO_SNDBUF, SO_SNDBUFFORCE, KNET_RING_RCVBUFF)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s send buffer: %s", type, strerror(savederrno)); goto exit_error; } if (flags & KNET_LINK_FLAG_TRAFFICHIPRIO) { #ifdef KNET_LINUX #ifdef SO_PRIORITY value = 6; /* TC_PRIO_INTERACTIVE */ if (setsockopt(sock, SOL_SOCKET, SO_PRIORITY, &value, sizeof(value)) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s priority: %s", type, strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSPORT, "TC_PRIO_INTERACTIVE enabled on socket: %i", sock); #else log_debug(knet_h, KNET_SUB_TRANSPORT, "TC_PRIO_INTERACTIVE not available in this build/platform"); #endif #endif #if defined(IP_TOS) && defined(IPTOS_LOWDELAY) value = IPTOS_LOWDELAY; if (setsockopt(sock, IPPROTO_IP, IP_TOS, &value, sizeof(value)) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s priority: %s", type, strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSPORT, "IPTOS_LOWDELAY enabled on socket: %i", sock); #else log_debug(knet_h, KNET_SUB_TRANSPORT, "IPTOS_LOWDELAY not available in this build/platform"); #endif } exit_error: errno = savederrno; return err; } int _configure_transport_socket(knet_handle_t knet_h, int sock, struct sockaddr_storage *address, uint64_t flags, const char *type) { int err = 0, savederrno = 0; int value; if (_configure_common_socket(knet_h, sock, flags, type) < 0) { savederrno = errno; err = -1; goto exit_error; } #ifdef KNET_LINUX #ifdef IP_FREEBIND value = 1; if (setsockopt(sock, SOL_IP, IP_FREEBIND, &value, sizeof(value)) <0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set FREEBIND on %s socket: %s", type, strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSPORT, "FREEBIND enabled on socket: %i", sock); #else log_debug(knet_h, KNET_SUB_TRANSPORT, "FREEBIND not available in this build/platform"); #endif #endif #ifdef KNET_BSD #ifdef IP_BINDANY /* BSD */ value = 1; if (setsockopt(sock, IPPROTO_IP, IP_BINDANY, &value, sizeof(value)) <0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set BINDANY on %s socket: %s", type, strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSPORT, "BINDANY enabled on socket: %i", sock); #else log_debug(knet_h, KNET_SUB_TRANSPORT, "BINDANY not available in this build/platform"); #endif #endif if (address->ss_family == AF_INET6) { value = 1; if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &value, sizeof(value)) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set %s IPv6 only: %s", type, strerror(savederrno)); goto exit_error; } #ifdef KNET_LINUX #ifdef IPV6_MTU_DISCOVER value = IPV6_PMTUDISC_PROBE; if (setsockopt(sock, SOL_IPV6, IPV6_MTU_DISCOVER, &value, sizeof(value)) <0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set PMTUDISC on %s socket: %s", type, strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSPORT, "IPV6_MTU_DISCOVER enabled on socket: %i", sock); #else log_debug(knet_h, KNET_SUB_TRANSPORT, "IPV6_MTU_DISCOVER not available in this build/platform"); #endif #endif #ifdef IPV6_DONTFRAG value = 1; if (setsockopt(sock, IPPROTO_IPV6, IPV6_DONTFRAG, &value, sizeof(value)) <0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set DONTFRAG on %s socket: %s", type, strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSPORT, "IPV6_DONTFRAG enabled on socket: %i", sock); #else log_debug(knet_h, KNET_SUB_TRANSPORT, "IPV6_DONTFRAG not available in this build/platform"); #endif } else { #ifdef KNET_LINUX #ifdef IP_MTU_DISCOVER value = IP_PMTUDISC_PROBE; if (setsockopt(sock, SOL_IP, IP_MTU_DISCOVER, &value, sizeof(value)) <0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set PMTUDISC on %s socket: %s", type, strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSPORT, "PMTUDISC enabled on socket: %i", sock); #else log_debug(knet_h, KNET_SUB_TRANSPORT, "PMTUDISC not available in this build/platform"); #endif #endif #ifdef KNET_BSD #ifdef IP_DONTFRAG value = 1; if (setsockopt(sock, IPPROTO_IP, IP_DONTFRAG, &value, sizeof(value)) <0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set DONTFRAG on %s socket: %s", type, strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSPORT, "DONTFRAG enabled on socket: %i", sock); #else log_debug(knet_h, KNET_SUB_TRANSPORT, "DONTFRAG not available in this build/platform"); #endif #endif } exit_error: errno = savederrno; return err; } int _init_socketpair(knet_handle_t knet_h, int *sock) { int err = 0, savederrno = 0; int i; if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sock) != 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize socketpair: %s", strerror(savederrno)); goto exit_fail; } for (i = 0; i < 2; i++) { if (_configure_common_socket(knet_h, sock[i], 0, "local socketpair") < 0) { savederrno = errno; err = -1; goto exit_fail; } } exit_fail: errno = savederrno; return err; } void _close_socketpair(knet_handle_t knet_h, int *sock) { int i; for (i = 0; i < 2; i++) { if (sock[i]) { close(sock[i]); sock[i] = 0; } } } /* * must be called with global read lock * * return -1 on error * return 0 if fd is invalid * return 1 if fd is valid */ int _is_valid_fd(knet_handle_t knet_h, int sockfd) { int ret = 0; if (sockfd < 0) { errno = EINVAL; return -1; } if (sockfd >= KNET_MAX_FDS) { errno = EINVAL; return -1; } if (knet_h->knet_transport_fd_tracker[sockfd].transport >= KNET_MAX_TRANSPORTS) { ret = 0; } else { ret = 1; } return ret; } /* * must be called with global write lock */ int _set_fd_tracker(knet_handle_t knet_h, int sockfd, uint8_t transport, uint8_t data_type, socklen_t socklen, void *data) { if (sockfd < 0) { errno = EINVAL; return -1; } if (sockfd >= KNET_MAX_FDS) { errno = EINVAL; return -1; } knet_h->knet_transport_fd_tracker[sockfd].transport = transport; knet_h->knet_transport_fd_tracker[sockfd].data_type = data_type; knet_h->knet_transport_fd_tracker[sockfd].sockaddr_len = socklen; knet_h->knet_transport_fd_tracker[sockfd].data = data; return 0; } diff --git a/libknet/transport_common.h b/libknet/transport_common.h index 7cd99fb3..27b60679 100644 --- a/libknet/transport_common.h +++ b/libknet/transport_common.h @@ -1,24 +1,24 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_TRANSPORT_COMMON_H__ #define __KNET_TRANSPORT_COMMON_H__ int _configure_common_socket(knet_handle_t knet_h, int sock, uint64_t flags, const char *type); int _configure_transport_socket(knet_handle_t knet_h, int sock, struct sockaddr_storage *address, uint64_t flags, const char *type); int _init_socketpair(knet_handle_t knet_h, int *sock); void _close_socketpair(knet_handle_t knet_h, int *sock); int _set_fd_tracker(knet_handle_t knet_h, int sockfd, uint8_t transport, uint8_t data_type, socklen_t socklen, void *data); int _is_valid_fd(knet_handle_t knet_h, int sockfd); int _sendmmsg(int sockfd, int connection_oriented, struct knet_mmsghdr *msgvec, unsigned int vlen, unsigned int flags); int _recvmmsg(int sockfd, struct knet_mmsghdr *msgvec, unsigned int vlen, unsigned int flags); #endif diff --git a/libknet/transport_loopback.c b/libknet/transport_loopback.c index f44d1fa1..26dd66de 100644 --- a/libknet/transport_loopback.c +++ b/libknet/transport_loopback.c @@ -1,80 +1,80 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include "libknet.h" #include "compat.h" #include "host.h" #include "link.h" #include "logging.h" #include "common.h" #include "transports.h" #include "transport_loopback.h" #include "threads_common.h" /* This is just a file of empty calls as the actual loopback is in threads_tx.c as a special case when receiving a packet from the localhost */ int loopback_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link) { kn_link->transport_connected = 1; kn_link->status.connected = 1; return 0; } int loopback_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link) { return 0; } int loopback_transport_free(knet_handle_t knet_h) { return 0; } int loopback_transport_init(knet_handle_t knet_h) { return 0; } int loopback_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) { return 0; } transport_sock_error_t loopback_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int subsys, int recv_err, int recv_errno) { return KNET_TRANSPORT_SOCK_ERROR_IGNORE; } transport_rx_isdata_t loopback_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg) { return KNET_TRANSPORT_RX_NOT_DATA_CONTINUE; } int loopback_transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link) { return 0; } int loopback_transport_link_is_down(knet_handle_t knet_h, struct knet_link *kn_link) { return 0; } diff --git a/libknet/transport_loopback.h b/libknet/transport_loopback.h index cfd95e48..6e38f8d4 100644 --- a/libknet/transport_loopback.h +++ b/libknet/transport_loopback.h @@ -1,28 +1,28 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include "internals.h" #ifndef __KNET_TRANSPORT_LOOPBACK_H__ #define __KNET_TRANSPORT_LOOPBACK_H__ #define KNET_PMTUD_LOOPBACK_OVERHEAD 0 int loopback_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link); int loopback_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link); int loopback_transport_free(knet_handle_t knet_h); int loopback_transport_init(knet_handle_t knet_h); int loopback_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno); transport_sock_error_t loopback_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int subsys, int recv_err, int recv_errno); transport_rx_isdata_t loopback_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg); int loopback_transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link); int loopback_transport_link_is_down(knet_handle_t knet_h, struct knet_link *kn_link); #endif diff --git a/libknet/transport_sctp.c b/libknet/transport_sctp.c index 6d779b4b..2abebfec 100644 --- a/libknet/transport_sctp.c +++ b/libknet/transport_sctp.c @@ -1,1639 +1,1639 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "compat.h" #include "host.h" #include "links.h" #include "links_acl.h" #include "links_acl_ip.h" #include "logging.h" #include "netutils.h" #include "common.h" #include "transport_common.h" #include "transports.h" #include "threads_common.h" #ifdef HAVE_NETINET_SCTP_H #include #include "transport_sctp.h" typedef struct sctp_handle_info { struct qb_list_head listen_links_list; struct qb_list_head connect_links_list; int connect_epollfd; int connectsockfd[2]; int listen_epollfd; int listensockfd[2]; pthread_t connect_thread; pthread_t listen_thread; socklen_t event_subscribe_kernel_size; char *event_subscribe_buffer; } sctp_handle_info_t; /* * use by fd_tracker data type */ #define SCTP_NO_LINK_INFO 0 #define SCTP_LISTENER_LINK_INFO 1 #define SCTP_ACCEPTED_LINK_INFO 2 #define SCTP_CONNECT_LINK_INFO 3 /* * this value is per listener */ #define MAX_ACCEPTED_SOCKS 256 typedef struct sctp_listen_link_info { struct qb_list_head list; int listen_sock; int accepted_socks[MAX_ACCEPTED_SOCKS]; struct sockaddr_storage src_address; int on_listener_epoll; int on_rx_epoll; int sock_shutdown; } sctp_listen_link_info_t; typedef struct sctp_accepted_link_info { char mread_buf[KNET_DATABUFSIZE]; ssize_t mread_len; sctp_listen_link_info_t *link_info; } sctp_accepted_link_info_t ; typedef struct sctp_connect_link_info { struct qb_list_head list; sctp_listen_link_info_t *listener; struct knet_link *link; struct sockaddr_storage dst_address; int connect_sock; int on_rx_epoll; int close_sock; int sock_shutdown; } sctp_connect_link_info_t; /* * socket handling functions * * those functions do NOT perform locking. locking * should be handled in the right context from callers */ /* * sockets are removed from rx_epoll from callers * see also error handling functions */ static int _close_connect_socket(knet_handle_t knet_h, struct knet_link *kn_link) { int err = 0, savederrno = 0; struct epoll_event ev; sctp_connect_link_info_t *info = kn_link->transport_link; if (info->connect_sock != -1) { if (info->on_rx_epoll) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = info->connect_sock; if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, info->connect_sock, &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove connected socket from epoll pool: %s", strerror(savederrno)); goto exit_error; } info->on_rx_epoll = 0; } if (_set_fd_tracker(knet_h, info->connect_sock, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, 0, NULL) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", strerror(savederrno)); } else { close(info->connect_sock); info->connect_sock = -1; } } exit_error: errno = savederrno; return err; } static int _enable_sctp_notifications(knet_handle_t knet_h, int sock, const char *type) { int err = 0, savederrno = 0; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; if (setsockopt(sock, IPPROTO_SCTP, SCTP_EVENTS, handle_info->event_subscribe_buffer, handle_info->event_subscribe_kernel_size) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to enable %s events: %s", type, strerror(savederrno)); } errno = savederrno; return err; } static int _configure_sctp_socket(knet_handle_t knet_h, int sock, struct sockaddr_storage *address, uint64_t flags, const char *type) { int err = 0, savederrno = 0; int value; int level; #ifdef SOL_SCTP level = SOL_SCTP; #else level = IPPROTO_SCTP; #endif if (_configure_transport_socket(knet_h, sock, address, flags, type) < 0) { savederrno = errno; err = -1; goto exit_error; } value = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value)) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set reuseaddr on socket %d: %s", sock, strerror(savederrno)); goto exit_error; } value = 1; if (setsockopt(sock, level, SCTP_NODELAY, &value, sizeof(value)) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSPORT, "Unable to set sctp nodelay: %s", strerror(savederrno)); goto exit_error; } if (_enable_sctp_notifications(knet_h, sock, type) < 0) { savederrno = errno; err = -1; } exit_error: errno = savederrno; return err; } static int _reconnect_socket(knet_handle_t knet_h, struct knet_link *kn_link) { int err = 0, savederrno = 0; sctp_connect_link_info_t *info = kn_link->transport_link; if (connect(info->connect_sock, (struct sockaddr *)&kn_link->dst_addr, sockaddr_len(&kn_link->dst_addr)) < 0) { savederrno = errno; log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP socket %d received error: %s", info->connect_sock, strerror(savederrno)); if ((savederrno != EALREADY) && (savederrno != EINPROGRESS) && (savederrno != EISCONN)) { err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to connect SCTP socket %d: %s", info->connect_sock, strerror(savederrno)); } } errno = savederrno; return err; } static int _create_connect_socket(knet_handle_t knet_h, struct knet_link *kn_link) { int err = 0, savederrno = 0; struct epoll_event ev; sctp_connect_link_info_t *info = kn_link->transport_link; int connect_sock; struct sockaddr_storage connect_addr; connect_sock = socket(kn_link->dst_addr.ss_family, SOCK_STREAM, IPPROTO_SCTP); if (connect_sock < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create send/recv socket: %s", strerror(savederrno)); goto exit_error; } if (_configure_sctp_socket(knet_h, connect_sock, &kn_link->dst_addr, kn_link->flags, "SCTP connect") < 0) { savederrno = errno; err = -1; goto exit_error; } memset(&connect_addr, 0, sizeof(struct sockaddr_storage)); if (knet_strtoaddr(kn_link->status.src_ipaddr, "0", &connect_addr, sockaddr_len(&connect_addr)) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to resolve connecting socket: %s", strerror(savederrno)); goto exit_error; } if (bind(connect_sock, (struct sockaddr *)&connect_addr, sockaddr_len(&connect_addr)) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to bind connecting socket: %s", strerror(savederrno)); goto exit_error; } if (_set_fd_tracker(knet_h, connect_sock, KNET_TRANSPORT_SCTP, SCTP_CONNECT_LINK_INFO, sockaddr_len(&kn_link->src_addr), info) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", strerror(savederrno)); goto exit_error; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = connect_sock; if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_ADD, connect_sock, &ev)) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add connected socket to epoll pool: %s", strerror(errno)); } info->on_rx_epoll = 1; info->connect_sock = connect_sock; info->close_sock = 0; kn_link->outsock = info->connect_sock; if (_reconnect_socket(knet_h, kn_link) < 0) { savederrno = errno; err = -1; goto exit_error; } exit_error: if (err) { if (connect_sock >= 0) { close(connect_sock); } } errno = savederrno; return err; } static void _lock_sleep_relock(knet_handle_t knet_h) { int i = 0; /* Don't hold onto the lock while sleeping */ pthread_rwlock_unlock(&knet_h->global_rwlock); while (i < 5) { usleep(knet_h->threads_timer_res / 16); if (!pthread_rwlock_rdlock(&knet_h->global_rwlock)) { /* * lock acquired, we can go out */ return; } else { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to get read lock!"); i++; } } /* * time to crash! if we cannot re-acquire the lock * there is no easy way out of this one */ assert(0); } int sctp_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int subsys, int recv_err, int recv_errno) { sctp_connect_link_info_t *connect_info = knet_h->knet_transport_fd_tracker[sockfd].data; sctp_accepted_link_info_t *accepted_info = knet_h->knet_transport_fd_tracker[sockfd].data; sctp_listen_link_info_t *listen_info; if (recv_err < 0) { switch (knet_h->knet_transport_fd_tracker[sockfd].data_type) { case SCTP_CONNECT_LINK_INFO: if (connect_info->link->transport_connected == 0) { return KNET_TRANSPORT_SOCK_ERROR_INTERNAL; } break; case SCTP_ACCEPTED_LINK_INFO: listen_info = accepted_info->link_info; if (listen_info->listen_sock != sockfd) { if (listen_info->on_rx_epoll == 0) { return KNET_TRANSPORT_SOCK_ERROR_INTERNAL; } } break; } if (recv_errno == EAGAIN) { #ifdef DEBUG log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Sock: %d is overloaded. Slowing TX down", sockfd); #endif _lock_sleep_relock(knet_h); return KNET_TRANSPORT_SOCK_ERROR_RETRY; } return KNET_TRANSPORT_SOCK_ERROR_INTERNAL; } return KNET_TRANSPORT_SOCK_ERROR_IGNORE; } /* * socket error management functions * * both called with global read lock. * * NOTE: we need to remove the fd from the epoll as soon as possible * even before we notify the respective thread to take care of it * because scheduling can make it so that this thread will overload * and the threads supposed to take care of the error will never * be able to take action. * we CANNOT handle FDs here directly (close/reconnect/etc) due * to locking context. We need to delegate that to their respective * management threads within the global write lock. * * this function is called from: * - RX thread with recv_err <= 0 directly on recvmmsg error * - transport_rx_is_data when msg_len == 0 (recv_err = 1) * - transport_rx_is_data on notification (recv_err = 2) * * basically this small abuse of recv_err is to detect notifications * generated by sockets created by listen(). */ transport_sock_error_t sctp_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) { struct epoll_event ev; sctp_accepted_link_info_t *accepted_info = knet_h->knet_transport_fd_tracker[sockfd].data; sctp_listen_link_info_t *listen_info; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; switch (knet_h->knet_transport_fd_tracker[sockfd].data_type) { case SCTP_CONNECT_LINK_INFO: /* * all connect link have notifications enabled * and we accept only data from notification and * generic recvmmsg errors. * * Errors generated by msg_len 0 can be ignored because * they follow a notification (double notification) */ if (recv_err != 1) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Notifying connect thread that sockfd %d received an error", sockfd); if (sendto(handle_info->connectsockfd[1], &sockfd, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0) != sizeof(int)) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to notify connect thread: %s", strerror(errno)); } } break; case SCTP_ACCEPTED_LINK_INFO: listen_info = accepted_info->link_info; if (listen_info->listen_sock != sockfd) { if (recv_err != 1) { if (listen_info->on_rx_epoll) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = sockfd; if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, sockfd, &ev)) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove EOFed socket from epoll pool: %s", strerror(errno)); return -1; } listen_info->on_rx_epoll = 0; } log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Notifying listen thread that sockfd %d received an error", sockfd); if (sendto(handle_info->listensockfd[1], &sockfd, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0) != sizeof(int)) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to notify listen thread: %s", strerror(errno)); } } } else { /* * this means the listen() socket has generated * a notification. now what? :-) */ log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification for listen() socket %d", sockfd); } break; default: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received unknown notification? %d", sockfd); break; } /* * Under RX pressure we need to give time to IPC to pick up the message */ _lock_sleep_relock(knet_h); return 0; } /* * NOTE: sctp_transport_rx_is_data is called with global rdlock * delegate any FD error management to sctp_transport_rx_sock_error * and keep this code to parsing incoming data only */ transport_rx_isdata_t sctp_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg) { size_t i; struct iovec *iov = msg->msg_hdr.msg_iov; size_t iovlen = msg->msg_hdr.msg_iovlen; struct sctp_assoc_change *sac; union sctp_notification *snp; sctp_accepted_link_info_t *listen_info = knet_h->knet_transport_fd_tracker[sockfd].data; sctp_connect_link_info_t *connect_info = knet_h->knet_transport_fd_tracker[sockfd].data; if (!(msg->msg_hdr.msg_flags & MSG_NOTIFICATION)) { if (msg->msg_len == 0) { /* * NOTE: with event notification enabled, we receive error twice: * 1) from the event notification * 2) followed by a 0 byte msg_len * * the event handler should take care to avoid #2 by stopping * the rx thread from processing more packets than necessary. */ if (knet_h->knet_transport_fd_tracker[sockfd].data_type == SCTP_CONNECT_LINK_INFO) { if (connect_info->sock_shutdown) { return KNET_TRANSPORT_RX_OOB_DATA_CONTINUE; } } else { if (listen_info->link_info->sock_shutdown) { return KNET_TRANSPORT_RX_OOB_DATA_CONTINUE; } } /* * this is pretty much dead code and we should never hit it. * keep it for safety and avoid the rx thread to process * bad info / data. */ return KNET_TRANSPORT_RX_NOT_DATA_STOP; } /* * missing MSG_EOR has to be treated as a short read * from the socket and we need to fill in the mread buf * while we wait for MSG_EOR */ if (!(msg->msg_hdr.msg_flags & MSG_EOR)) { /* * copy the incoming data into mread_buf + mread_len (incremental) * and increase mread_len */ memmove(listen_info->mread_buf + listen_info->mread_len, iov->iov_base, msg->msg_len); listen_info->mread_len = listen_info->mread_len + msg->msg_len; return KNET_TRANSPORT_RX_NOT_DATA_CONTINUE; } /* * got EOR. * if mread_len is > 0 we are completing a packet from short reads * complete reassembling the packet in mread_buf, copy it back in the iov * and set the iov/msg len numbers (size) correctly */ if (listen_info->mread_len) { /* * add last fragment to mread_buf */ memmove(listen_info->mread_buf + listen_info->mread_len, iov->iov_base, msg->msg_len); listen_info->mread_len = listen_info->mread_len + msg->msg_len; /* * move all back into the iovec */ memmove(iov->iov_base, listen_info->mread_buf, listen_info->mread_len); msg->msg_len = listen_info->mread_len; listen_info->mread_len = 0; } return KNET_TRANSPORT_RX_IS_DATA; } if (!(msg->msg_hdr.msg_flags & MSG_EOR)) { return KNET_TRANSPORT_RX_NOT_DATA_STOP; } for (i = 0; i < iovlen; i++) { snp = iov[i].iov_base; switch (snp->sn_header.sn_type) { case SCTP_ASSOC_CHANGE: sac = &snp->sn_assoc_change; switch (sac->sac_state) { case SCTP_COMM_LOST: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change socket %d: comm_lost", sockfd); if (knet_h->knet_transport_fd_tracker[sockfd].data_type == SCTP_CONNECT_LINK_INFO) { connect_info->close_sock = 1; connect_info->link->transport_connected = 0; } sctp_transport_rx_sock_error(knet_h, sockfd, 2, 0); return KNET_TRANSPORT_RX_OOB_DATA_STOP; break; case SCTP_COMM_UP: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change socket %d: comm_up", sockfd); if (knet_h->knet_transport_fd_tracker[sockfd].data_type == SCTP_CONNECT_LINK_INFO) { connect_info->link->transport_connected = 1; } break; case SCTP_RESTART: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change socket %d: restart", sockfd); break; case SCTP_SHUTDOWN_COMP: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change socket %d: shutdown comp", sockfd); if (knet_h->knet_transport_fd_tracker[sockfd].data_type == SCTP_CONNECT_LINK_INFO) { connect_info->close_sock = 1; } sctp_transport_rx_sock_error(knet_h, sockfd, 2, 0); return KNET_TRANSPORT_RX_OOB_DATA_STOP; break; case SCTP_CANT_STR_ASSOC: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change socket %d: cant str assoc", sockfd); sctp_transport_rx_sock_error(knet_h, sockfd, 2, 0); break; default: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change socket %d: unknown %d", sockfd, sac->sac_state); break; } break; case SCTP_SHUTDOWN_EVENT: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp shutdown event socket %d", sockfd); if (knet_h->knet_transport_fd_tracker[sockfd].data_type == SCTP_CONNECT_LINK_INFO) { connect_info->link->transport_connected = 0; connect_info->sock_shutdown = 1; } else { listen_info->link_info->sock_shutdown = 1; } break; case SCTP_SEND_FAILED: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp send failed socket: %d", sockfd); break; case SCTP_PEER_ADDR_CHANGE: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp peer addr change socket %d", sockfd); break; case SCTP_REMOTE_ERROR: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp remote error socket %d", sockfd); break; default: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] unknown sctp event socket: %d type: %hu", sockfd, snp->sn_header.sn_type); break; } } return KNET_TRANSPORT_RX_OOB_DATA_CONTINUE; } int sctp_transport_link_is_down(knet_handle_t knet_h, struct knet_link *kn_link) { sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; sctp_connect_link_info_t *info = kn_link->transport_link; kn_link->transport_connected = 0; info->close_sock = 1; log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Notifying connect thread that sockfd %d received a link down event", info->connect_sock); if (sendto(handle_info->connectsockfd[1], &info->connect_sock, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0) != sizeof(int)) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to notify connect thread: %s", strerror(errno)); } return 0; } /* * connect / outgoing socket management thread */ /* * _handle_connected_sctp* are called with a global write lock * from the connect_thread */ static void _handle_connected_sctp_socket(knet_handle_t knet_h, int connect_sock) { int err; unsigned int status, len = sizeof(status); sctp_connect_link_info_t *info = knet_h->knet_transport_fd_tracker[connect_sock].data; struct knet_link *kn_link = info->link; if (info->close_sock) { if (_close_connect_socket(knet_h, kn_link) < 0) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to close sock %d from _handle_connected_sctp_socket: %s", connect_sock, strerror(errno)); return; } info->close_sock = 0; if (_create_connect_socket(knet_h, kn_link) < 0) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to recreate connecting sock! %s", strerror(errno)); return; } } _reconnect_socket(knet_h, info->link); err = getsockopt(connect_sock, SOL_SOCKET, SO_ERROR, &status, &len); if (err) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP getsockopt() on connecting socket %d failed: %s", connect_sock, strerror(errno)); return; } if (status) { log_info(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP connect on %d to %s port %s failed: %s", connect_sock, kn_link->status.dst_ipaddr, kn_link->status.dst_port, strerror(status)); /* * No need to create a new socket if connect failed, * just retry connect */ return; } log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP handler fd %d now connected to %s port %s", connect_sock, kn_link->status.dst_ipaddr, kn_link->status.dst_port); } static void _handle_connected_sctp_notifications(knet_handle_t knet_h) { int sockfd = -1; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; if (recv(handle_info->connectsockfd[0], &sockfd, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL) != sizeof(int)) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Short read on connectsockfd"); return; } if (_is_valid_fd(knet_h, sockfd) < 1) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification for connected socket fd error"); return; } /* * revalidate sockfd */ if ((sockfd < 0) || (sockfd >= KNET_MAX_FDS)) { return; } log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Processing connected error on socket: %d", sockfd); _handle_connected_sctp_socket(knet_h, sockfd); } static void *_sctp_connect_thread(void *data) { int savederrno; int i, nev; knet_handle_t knet_h = (knet_handle_t) data; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; set_thread_status(knet_h, KNET_THREAD_SCTP_CONN, KNET_THREAD_STARTED); memset(&events, 0, sizeof(events)); while (!shutdown_in_progress(knet_h)) { nev = epoll_wait(handle_info->connect_epollfd, events, KNET_EPOLL_MAX_EVENTS, knet_h->threads_timer_res / 1000); /* * we use timeout to detect if thread is shutting down */ if (nev == 0) { continue; } if (nev < 0) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP connect handler EPOLL ERROR: %s", strerror(errno)); continue; } /* * Sort out which FD has a connection */ savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to get write lock: %s", strerror(savederrno)); continue; } /* * minor optimization: deduplicate events * * in some cases we can receive multiple notifcations * of the same FD having issues or need handling. * It's enough to process it once even tho it's safe * to handle them multiple times. */ for (i = 0; i < nev; i++) { if (events[i].data.fd == handle_info->connectsockfd[0]) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received notification from rx_error for connected socket"); _handle_connected_sctp_notifications(knet_h); } else { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification on connected sockfd %d\n", events[i].data.fd); } } pthread_rwlock_unlock(&knet_h->global_rwlock); /* * this thread can generate events for itself. * we need to sleep in between loops to allow other threads * to be scheduled */ usleep(knet_h->reconnect_int * 1000); } set_thread_status(knet_h, KNET_THREAD_SCTP_CONN, KNET_THREAD_STOPPED); return NULL; } /* * listen/incoming connections management thread */ /* * Listener received a new connection * called with a write lock from main thread */ static void _handle_incoming_sctp(knet_handle_t knet_h, int listen_sock) { int err = 0, savederrno = 0; int new_fd; int i = -1; sctp_listen_link_info_t *info = knet_h->knet_transport_fd_tracker[listen_sock].data; struct epoll_event ev; struct sockaddr_storage ss; socklen_t sock_len = sizeof(ss); char addr_str[KNET_MAX_HOST_LEN]; char port_str[KNET_MAX_PORT_LEN]; sctp_accepted_link_info_t *accept_info = NULL; struct knet_host *host; struct knet_link *kn_link; int link_idx; sctp_connect_link_info_t *this_link_connect_info; sctp_listen_link_info_t *this_link_listen_info; int pass_acl = 0; memset(&ss, 0, sizeof(ss)); new_fd = accept(listen_sock, (struct sockaddr *)&ss, &sock_len); if (new_fd < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: accept error: %s", strerror(errno)); goto exit_error; } if (knet_addrtostr(&ss, sizeof(ss), addr_str, KNET_MAX_HOST_LEN, port_str, KNET_MAX_PORT_LEN) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: unable to gather socket info"); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: received connection from: %s port: %s", addr_str, port_str); if (knet_h->use_access_lists) { for (host = knet_h->host_head; host != NULL; host = host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { kn_link = &host->link[link_idx]; if ((kn_link->configured) && (kn_link->transport == KNET_TRANSPORT_SCTP)) { this_link_connect_info = kn_link->transport_link; this_link_listen_info = this_link_connect_info->listener; if ((this_link_listen_info->listen_sock == listen_sock) && (check_validate(knet_h, kn_link, &ss))) { pass_acl = 1; break; } } } if (pass_acl) { break; } } if (!pass_acl) { savederrno = EINVAL; log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Connection rejected from %s/%s", addr_str, port_str); close(new_fd); errno = savederrno; return; } } /* * Keep a track of all accepted FDs */ for (i=0; iaccepted_socks[i] == -1) { info->accepted_socks[i] = new_fd; break; } } if (i == MAX_ACCEPTED_SOCKS) { errno = EBUSY; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: too many connections!"); goto exit_error; } if (_configure_common_socket(knet_h, new_fd, 0, "SCTP incoming") < 0) { /* Inherit flags from listener? */ savederrno = errno; err = -1; goto exit_error; } if (_enable_sctp_notifications(knet_h, new_fd, "Incoming connection") < 0) { savederrno = errno; err = -1; goto exit_error; } accept_info = malloc(sizeof(sctp_accepted_link_info_t)); if (!accept_info) { savederrno = errno; err = -1; goto exit_error; } memset(accept_info, 0, sizeof(sctp_accepted_link_info_t)); accept_info->link_info = info; if (_set_fd_tracker(knet_h, new_fd, KNET_TRANSPORT_SCTP, SCTP_ACCEPTED_LINK_INFO, knet_h->knet_transport_fd_tracker[listen_sock].sockaddr_len, accept_info) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", strerror(errno)); goto exit_error; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = new_fd; if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_ADD, new_fd, &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: unable to add accepted socket %d to epoll pool: %s", new_fd, strerror(errno)); goto exit_error; } info->on_rx_epoll = 1; log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Incoming: accepted new fd %d for %s/%s (listen fd: %d). index: %d", new_fd, addr_str, port_str, info->listen_sock, i); exit_error: if (err) { if ((i >= 0) && (i < MAX_ACCEPTED_SOCKS)) { info->accepted_socks[i] = -1; } /* * check the error to make coverity scan happy. * _set_fd_tracker cannot fail at this stage */ if (_set_fd_tracker(knet_h, new_fd, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, 0, NULL) < 0){ log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to update fdtracker for socket %d", new_fd); } free(accept_info); if (new_fd >= 0) { close(new_fd); } } errno = savederrno; return; } /* * Listen thread received a notification of a bad socket that needs closing * called with a write lock from main thread */ static void _handle_listen_sctp_errors(knet_handle_t knet_h) { int sockfd = -1; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; sctp_accepted_link_info_t *accept_info; sctp_listen_link_info_t *info; struct knet_host *host; int link_idx; int i; if (recv(handle_info->listensockfd[0], &sockfd, sizeof(int), MSG_DONTWAIT | MSG_NOSIGNAL) != sizeof(int)) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Short read on listensockfd"); return; } if (_is_valid_fd(knet_h, sockfd) < 1) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification for listen socket fd error"); return; } /* * revalidate sockfd */ if ((sockfd < 0) || (sockfd >= KNET_MAX_FDS)) { return; } log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Processing listen error on socket: %d", sockfd); accept_info = knet_h->knet_transport_fd_tracker[sockfd].data; info = accept_info->link_info; /* * clear all links using this accepted socket as * outbound dynamically connected socket */ for (host = knet_h->host_head; host != NULL; host = host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { if ((host->link[link_idx].dynamic == KNET_LINK_DYNIP) && (host->link[link_idx].outsock == sockfd)) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Found dynamic connection on host %d link %d (%d)", host->host_id, link_idx, sockfd); host->link[link_idx].status.dynconnected = 0; host->link[link_idx].transport_connected = 0; host->link[link_idx].outsock = 0; memset(&host->link[link_idx].dst_addr, 0, sizeof(struct sockaddr_storage)); } } } for (i=0; iaccepted_socks[i]) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Closing accepted socket %d", sockfd); /* * check the error to make coverity scan happy. * _set_fd_tracker cannot fail at this stage */ if (_set_fd_tracker(knet_h, sockfd, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, 0, NULL) < 0) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to update fdtracker for socket %d", sockfd); } info->accepted_socks[i] = -1; free(accept_info); close(sockfd); break; /* Keeps covscan happy */ } } } static void *_sctp_listen_thread(void *data) { int savederrno; int i, nev; knet_handle_t knet_h = (knet_handle_t) data; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; struct epoll_event events[KNET_EPOLL_MAX_EVENTS]; set_thread_status(knet_h, KNET_THREAD_SCTP_LISTEN, KNET_THREAD_STARTED); memset(&events, 0, sizeof(events)); while (!shutdown_in_progress(knet_h)) { nev = epoll_wait(handle_info->listen_epollfd, events, KNET_EPOLL_MAX_EVENTS, knet_h->threads_timer_res / 1000); /* * we use timeout to detect if thread is shutting down */ if (nev == 0) { continue; } if (nev < 0) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP listen handler EPOLL ERROR: %s", strerror(errno)); continue; } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to get write lock: %s", strerror(savederrno)); continue; } /* * Sort out which FD has an incoming connection */ for (i = 0; i < nev; i++) { if (events[i].data.fd == handle_info->listensockfd[0]) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received notification from rx_error for listener/accepted socket"); _handle_listen_sctp_errors(knet_h); } else { if (_is_valid_fd(knet_h, events[i].data.fd) == 1) { _handle_incoming_sctp(knet_h, events[i].data.fd); } else { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received listen notification from invalid socket"); } } } pthread_rwlock_unlock(&knet_h->global_rwlock); } set_thread_status(knet_h, KNET_THREAD_SCTP_LISTEN, KNET_THREAD_STOPPED); return NULL; } /* * sctp_link_listener_start/stop are called in global write lock * context from set_config and clear_config. */ static sctp_listen_link_info_t *sctp_link_listener_start(knet_handle_t knet_h, struct knet_link *kn_link) { int err = 0, savederrno = 0; int listen_sock = -1; struct epoll_event ev; sctp_listen_link_info_t *info = NULL; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; /* * Only allocate a new listener if src address is different */ 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, kn_link, -1, &kn_link->dst_addr, &kn_link->dst_addr, CHECK_TYPE_ADDRESS, CHECK_ACCEPT) < 0) && (errno != EEXIST)) { return NULL; } return info; } } info = malloc(sizeof(sctp_listen_link_info_t)); if (!info) { err = -1; goto exit_error; } memset(info, 0, sizeof(sctp_listen_link_info_t)); memset(info->accepted_socks, -1, sizeof(info->accepted_socks)); memmove(&info->src_address, &kn_link->src_addr, sizeof(struct sockaddr_storage)); listen_sock = socket(kn_link->src_addr.ss_family, SOCK_STREAM, IPPROTO_SCTP); if (listen_sock < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create listener socket: %s", strerror(savederrno)); goto exit_error; } if (_configure_sctp_socket(knet_h, listen_sock, &kn_link->src_addr, kn_link->flags, "SCTP listener") < 0) { savederrno = errno; err = -1; goto exit_error; } if (bind(listen_sock, (struct sockaddr *)&kn_link->src_addr, sockaddr_len(&kn_link->src_addr)) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to bind listener socket: %s", strerror(savederrno)); goto exit_error; } if (listen(listen_sock, 5) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to listen on listener socket: %s", strerror(savederrno)); goto exit_error; } if (_set_fd_tracker(knet_h, listen_sock, KNET_TRANSPORT_SCTP, SCTP_LISTENER_LINK_INFO, sockaddr_len(&kn_link->src_addr), info) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", strerror(savederrno)); goto exit_error; } if ((check_add(knet_h, kn_link, -1, &kn_link->dst_addr, &kn_link->dst_addr, CHECK_TYPE_ADDRESS, CHECK_ACCEPT) < 0) && (errno != EEXIST)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to configure default access lists: %s", strerror(savederrno)); goto exit_error; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = listen_sock; if (epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_ADD, listen_sock, &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add listener to epoll pool: %s", strerror(savederrno)); goto exit_error; } info->on_listener_epoll = 1; info->listen_sock = listen_sock; 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); exit_error: if (err) { if ((info) && (info->on_listener_epoll)) { epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_DEL, listen_sock, &ev); } if (listen_sock >= 0) { check_rmall(knet_h, kn_link); close(listen_sock); } if (info) { free(info); info = NULL; } } errno = savederrno; return info; } static int sctp_link_listener_stop(knet_handle_t knet_h, struct knet_link *kn_link) { int err = 0, savederrno = 0; int found = 0, i; struct knet_host *host; int link_idx; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; sctp_connect_link_info_t *this_link_info = kn_link->transport_link; sctp_listen_link_info_t *info = this_link_info->listener; sctp_connect_link_info_t *link_info; struct epoll_event ev; for (host = knet_h->host_head; host != NULL; host = host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { if (&host->link[link_idx] == kn_link) continue; link_info = host->link[link_idx].transport_link; if ((link_info) && (link_info->listener == info)) { found = 1; break; } } } if ((check_rm(knet_h, kn_link, &kn_link->dst_addr, &kn_link->dst_addr, CHECK_TYPE_ADDRESS, CHECK_ACCEPT) < 0) && (errno != ENOENT)) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove default access lists for %d", info->listen_sock); } if (found) { this_link_info->listener = NULL; log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP listener socket %d still in use", info->listen_sock); savederrno = EBUSY; err = -1; goto exit_error; } if (info->on_listener_epoll) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = info->listen_sock; if (epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_DEL, info->listen_sock, &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove listener to epoll pool: %s", strerror(savederrno)); goto exit_error; } info->on_listener_epoll = 0; } if (_set_fd_tracker(knet_h, info->listen_sock, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, 0, NULL) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", strerror(savederrno)); goto exit_error; } check_rmall(knet_h, kn_link); close(info->listen_sock); for (i=0; i< MAX_ACCEPTED_SOCKS; i++) { if (info->accepted_socks[i] > -1) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = info->accepted_socks[i]; if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, info->accepted_socks[i], &ev)) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove EOFed socket from epoll pool: %s", strerror(errno)); } info->on_rx_epoll = 0; free(knet_h->knet_transport_fd_tracker[info->accepted_socks[i]].data); close(info->accepted_socks[i]); if (_set_fd_tracker(knet_h, info->accepted_socks[i], KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, 0, NULL) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set fd tracker: %s", strerror(savederrno)); goto exit_error; } info->accepted_socks[i] = -1; } } qb_list_del(&info->list); free(info); this_link_info->listener = NULL; exit_error: errno = savederrno; return err; } /* * Links config/clear. Both called with global wrlock from link_set_config/clear_config */ int sctp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link) { int savederrno = 0, err = 0; sctp_connect_link_info_t *info; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; info = malloc(sizeof(sctp_connect_link_info_t)); if (!info) { goto exit_error; } memset(info, 0, sizeof(sctp_connect_link_info_t)); kn_link->transport_link = info; info->link = kn_link; memmove(&info->dst_address, &kn_link->dst_addr, sizeof(struct sockaddr_storage)); info->connect_sock = -1; info->listener = sctp_link_listener_start(knet_h, kn_link); if (!info->listener) { savederrno = errno; err = -1; goto exit_error; } if (kn_link->dynamic == KNET_LINK_STATIC) { if (_create_connect_socket(knet_h, kn_link) < 0) { savederrno = errno; err = -1; goto exit_error; } kn_link->outsock = info->connect_sock; } qb_list_add(&info->list, &handle_info->connect_links_list); exit_error: if (err) { if (info) { if (info->connect_sock >= 0) { close(info->connect_sock); } if (info->listener) { sctp_link_listener_stop(knet_h, kn_link); } kn_link->transport_link = NULL; free(info); } } errno = savederrno; return err; } /* * called with global wrlock */ int sctp_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link) { int err = 0, savederrno = 0; sctp_connect_link_info_t *info; if (!kn_link) { errno = EINVAL; return -1; } info = kn_link->transport_link; if (!info) { errno = EINVAL; return -1; } if ((sctp_link_listener_stop(knet_h, kn_link) <0) && (errno != EBUSY)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove listener transport: %s", strerror(savederrno)); goto exit_error; } if (_close_connect_socket(knet_h, kn_link) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to close connected socket: %s", strerror(savederrno)); goto exit_error; } qb_list_del(&info->list); free(info); kn_link->transport_link = NULL; exit_error: errno = savederrno; return err; } /* * transport_free and transport_init are * called only from knet_handle_new and knet_handle_free. * all resources (hosts/links) should have been already freed at this point * and they are called in a write locked context, hence they * don't need their own locking. */ int sctp_transport_free(knet_handle_t knet_h) { sctp_handle_info_t *handle_info; void *thread_status; struct epoll_event ev; if (!knet_h->transports[KNET_TRANSPORT_SCTP]) { errno = EINVAL; return -1; } handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; /* * keep it here while we debug list usage and such */ 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 (!qb_list_empty(&handle_info->connect_links_list)) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Internal error. connect links list is not empty"); } if (handle_info->listen_thread) { pthread_cancel(handle_info->listen_thread); pthread_join(handle_info->listen_thread, &thread_status); } if (handle_info->connect_thread) { pthread_cancel(handle_info->connect_thread); pthread_join(handle_info->connect_thread, &thread_status); } if (handle_info->listensockfd[0] >= 0) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = handle_info->listensockfd[0]; epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_DEL, handle_info->listensockfd[0], &ev); } if (handle_info->connectsockfd[0] >= 0) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = handle_info->connectsockfd[0]; epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_DEL, handle_info->connectsockfd[0], &ev); } _close_socketpair(knet_h, handle_info->connectsockfd); _close_socketpair(knet_h, handle_info->listensockfd); if (handle_info->listen_epollfd >= 0) { close(handle_info->listen_epollfd); } if (handle_info->connect_epollfd >= 0) { close(handle_info->connect_epollfd); } free(handle_info->event_subscribe_buffer); free(handle_info); knet_h->transports[KNET_TRANSPORT_SCTP] = NULL; return 0; } static int _sctp_subscribe_init(knet_handle_t knet_h) { int test_socket, savederrno; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; char dummy_events[100]; struct sctp_event_subscribe *events; /* Below we set the first 6 fields of this expanding struct. * SCTP_EVENTS is deprecated, but SCTP_EVENT is not available * on Linux; on the other hand, FreeBSD and old Linux does not * accept small transfers, so we can't simply use this minimum * everywhere. Thus we query and store the native size. */ const unsigned int subscribe_min = 6; test_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP); if (test_socket < 0) { if (errno == EPROTONOSUPPORT) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "SCTP not supported, skipping initialization"); return 0; } savederrno = errno; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create test socket: %s", strerror(savederrno)); return savederrno; } handle_info->event_subscribe_kernel_size = sizeof dummy_events; if (getsockopt(test_socket, IPPROTO_SCTP, SCTP_EVENTS, &dummy_events, &handle_info->event_subscribe_kernel_size)) { close(test_socket); savederrno = errno; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to query kernel size of struct sctp_event_subscribe: %s", strerror(savederrno)); return savederrno; } close(test_socket); if (handle_info->event_subscribe_kernel_size < subscribe_min) { savederrno = ERANGE; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "No kernel support for the necessary notifications: struct sctp_event_subscribe is %u bytes, %u needed", handle_info->event_subscribe_kernel_size, subscribe_min); return savederrno; } events = malloc(handle_info->event_subscribe_kernel_size); if (!events) { savederrno = errno; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Failed to allocate event subscribe buffer: %s", strerror(savederrno)); return savederrno; } memset(events, 0, handle_info->event_subscribe_kernel_size); events->sctp_data_io_event = 1; events->sctp_association_event = 1; events->sctp_address_event = 1; events->sctp_send_failure_event = 1; events->sctp_peer_error_event = 1; events->sctp_shutdown_event = 1; handle_info->event_subscribe_buffer = (char *)events; log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Size of struct sctp_event_subscribe is %u in kernel, %zu in user space", handle_info->event_subscribe_kernel_size, sizeof(struct sctp_event_subscribe)); return 0; } int sctp_transport_init(knet_handle_t knet_h) { int err = 0, savederrno = 0; sctp_handle_info_t *handle_info; struct epoll_event ev; if (knet_h->transports[KNET_TRANSPORT_SCTP]) { errno = EEXIST; return -1; } handle_info = malloc(sizeof(sctp_handle_info_t)); if (!handle_info) { return -1; } memset(handle_info, 0,sizeof(sctp_handle_info_t)); knet_h->transports[KNET_TRANSPORT_SCTP] = handle_info; savederrno = _sctp_subscribe_init(knet_h); if (savederrno) { err = -1; goto exit_fail; } 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) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create epoll listen fd: %s", strerror(savederrno)); goto exit_fail; } if (_fdset_cloexec(handle_info->listen_epollfd)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set CLOEXEC on listen_epollfd: %s", strerror(savederrno)); goto exit_fail; } handle_info->connect_epollfd = epoll_create(KNET_EPOLL_MAX_EVENTS + 1); if (handle_info->connect_epollfd < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to create epoll connect fd: %s", strerror(savederrno)); goto exit_fail; } if (_fdset_cloexec(handle_info->connect_epollfd)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to set CLOEXEC on connect_epollfd: %s", strerror(savederrno)); goto exit_fail; } if (_init_socketpair(knet_h, handle_info->connectsockfd) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to init connect socketpair: %s", strerror(savederrno)); goto exit_fail; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = handle_info->connectsockfd[0]; if (epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_ADD, handle_info->connectsockfd[0], &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add connectsockfd[0] to connect epoll pool: %s", strerror(savederrno)); goto exit_fail; } if (_init_socketpair(knet_h, handle_info->listensockfd) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to init listen socketpair: %s", strerror(savederrno)); goto exit_fail; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = handle_info->listensockfd[0]; if (epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_ADD, handle_info->listensockfd[0], &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add listensockfd[0] to listen epoll pool: %s", strerror(savederrno)); goto exit_fail; } /* * Start connect & listener threads */ set_thread_status(knet_h, KNET_THREAD_SCTP_LISTEN, KNET_THREAD_REGISTERED); savederrno = pthread_create(&handle_info->listen_thread, 0, _sctp_listen_thread, (void *) knet_h); if (savederrno) { err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to start sctp listen thread: %s", strerror(savederrno)); goto exit_fail; } set_thread_status(knet_h, KNET_THREAD_SCTP_CONN, KNET_THREAD_REGISTERED); savederrno = pthread_create(&handle_info->connect_thread, 0, _sctp_connect_thread, (void *) knet_h); if (savederrno) { err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to start sctp connect thread: %s", strerror(savederrno)); goto exit_fail; } exit_fail: if (err < 0) { sctp_transport_free(knet_h); } errno = savederrno; return err; } int sctp_transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link) { kn_link->outsock = sockfd; kn_link->status.dynconnected = 1; kn_link->transport_connected = 1; return 0; } #endif diff --git a/libknet/transport_sctp.h b/libknet/transport_sctp.h index e5f7ab8f..abd498aa 100644 --- a/libknet/transport_sctp.h +++ b/libknet/transport_sctp.h @@ -1,38 +1,38 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include "internals.h" #ifndef __KNET_TRANSPORT_SCTP_H__ #define __KNET_TRANSPORT_SCTP_H__ /* * https://en.wikipedia.org/wiki/SCTP_packet_structure */ #define KNET_PMTUD_SCTP_OVERHEAD_COMMON 12 #define KNET_PMTUD_SCTP_OVERHEAD_DATA_CHUNK 16 #define KNET_PMTUD_SCTP_OVERHEAD KNET_PMTUD_SCTP_OVERHEAD_COMMON + KNET_PMTUD_SCTP_OVERHEAD_DATA_CHUNK #ifdef HAVE_NETINET_SCTP_H int sctp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link); int sctp_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link); int sctp_transport_free(knet_handle_t knet_h); int sctp_transport_init(knet_handle_t knet_h); int sctp_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno); transport_sock_error_t sctp_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int subsys, int recv_err, int recv_errno); transport_rx_isdata_t sctp_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg); int sctp_transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link); int sctp_transport_link_is_down(knet_handle_t knet_h, struct knet_link *kn_link); #endif #endif diff --git a/libknet/transport_udp.c b/libknet/transport_udp.c index f2fa0e78..0a46c527 100644 --- a/libknet/transport_udp.c +++ b/libknet/transport_udp.c @@ -1,486 +1,486 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #if defined (IP_RECVERR) || defined (IPV6_RECVERR) #include #endif #include "libknet.h" #include "compat.h" #include "host.h" #include "link.h" #include "logging.h" #include "common.h" #include "netutils.h" #include "transport_common.h" #include "transport_udp.h" #include "transports.h" #include "threads_common.h" typedef struct udp_handle_info { struct qb_list_head links_list; } udp_handle_info_t; typedef struct udp_link_info { struct qb_list_head list; struct sockaddr_storage local_address; int socket_fd; int on_epoll; } udp_link_info_t; int udp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link) { int err = 0, savederrno = 0; int sock = -1; struct epoll_event ev; udp_link_info_t *info; udp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_UDP]; #if defined (IP_RECVERR) || defined (IPV6_RECVERR) int value; #endif /* * Only allocate a new link if the local address is different */ 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; kn_link->transport_link = info; kn_link->transport_connected = 1; return 0; } } info = malloc(sizeof(udp_link_info_t)); if (!info) { err = -1; goto exit_error; } memset(info, 0, sizeof(udp_link_info_t)); sock = socket(kn_link->src_addr.ss_family, SOCK_DGRAM, 0); if (sock < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to create listener socket: %s", strerror(savederrno)); goto exit_error; } if (_configure_transport_socket(knet_h, sock, &kn_link->src_addr, kn_link->flags, "UDP") < 0) { savederrno = errno; err = -1; goto exit_error; } #ifdef IP_RECVERR if (kn_link->src_addr.ss_family == AF_INET) { value = 1; if (setsockopt(sock, SOL_IP, IP_RECVERR, &value, sizeof(value)) <0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to set RECVERR on socket: %s", strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSP_UDP, "IP_RECVERR enabled on socket: %i", sock); } #else log_debug(knet_h, KNET_SUB_TRANSP_UDP, "IP_RECVERR not available in this build/platform"); #endif #ifdef IPV6_RECVERR if (kn_link->src_addr.ss_family == AF_INET6) { value = 1; if (setsockopt(sock, SOL_IPV6, IPV6_RECVERR, &value, sizeof(value)) <0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to set RECVERR on socket: %s", strerror(savederrno)); goto exit_error; } log_debug(knet_h, KNET_SUB_TRANSP_UDP, "IPV6_RECVERR enabled on socket: %i", sock); } #else log_debug(knet_h, KNET_SUB_TRANSP_UDP, "IPV6_RECVERR not available in this build/platform"); #endif if (bind(sock, (struct sockaddr *)&kn_link->src_addr, sockaddr_len(&kn_link->src_addr))) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to bind listener socket: %s", strerror(savederrno)); goto exit_error; } memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = sock; if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_ADD, sock, &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to add listener to epoll pool: %s", strerror(savederrno)); goto exit_error; } info->on_epoll = 1; if (_set_fd_tracker(knet_h, sock, KNET_TRANSPORT_UDP, 0, sockaddr_len(&kn_link->src_addr), info) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to set fd tracker: %s", strerror(savederrno)); goto exit_error; } memmove(&info->local_address, &kn_link->src_addr, sizeof(struct sockaddr_storage)); info->socket_fd = sock; qb_list_add(&info->list, &handle_info->links_list); kn_link->outsock = sock; kn_link->transport_link = info; kn_link->transport_connected = 1; exit_error: if (err) { if (info) { if (info->on_epoll) { epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, sock, &ev); } free(info); } if (sock >= 0) { close(sock); } } errno = savederrno; return err; } int udp_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link) { int err = 0, savederrno = 0; int found = 0; struct knet_host *host; int link_idx; udp_link_info_t *info = kn_link->transport_link; struct epoll_event ev; for (host = knet_h->host_head; host != NULL; host = host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { if (&host->link[link_idx] == kn_link) continue; if (host->link[link_idx].transport_link == info) { found = 1; break; } } } if (found) { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "UDP socket %d still in use", info->socket_fd); savederrno = EBUSY; err = -1; goto exit_error; } if (info->on_epoll) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLIN; ev.data.fd = info->socket_fd; if (epoll_ctl(knet_h->recv_from_links_epollfd, EPOLL_CTL_DEL, info->socket_fd, &ev) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to remove UDP socket from epoll poll: %s", strerror(errno)); goto exit_error; } info->on_epoll = 0; } if (_set_fd_tracker(knet_h, info->socket_fd, KNET_MAX_TRANSPORTS, 0, sockaddr_len(&kn_link->src_addr), NULL) < 0) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_UDP, "Unable to set fd tracker: %s", strerror(savederrno)); goto exit_error; } close(info->socket_fd); qb_list_del(&info->list); free(kn_link->transport_link); exit_error: errno = savederrno; return err; } int udp_transport_free(knet_handle_t knet_h) { udp_handle_info_t *handle_info; if (!knet_h->transports[KNET_TRANSPORT_UDP]) { errno = EINVAL; return -1; } handle_info = knet_h->transports[KNET_TRANSPORT_UDP]; /* * keep it here while we debug list usage and such */ 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; } free(handle_info); knet_h->transports[KNET_TRANSPORT_UDP] = NULL; return 0; } int udp_transport_init(knet_handle_t knet_h) { udp_handle_info_t *handle_info; if (knet_h->transports[KNET_TRANSPORT_UDP]) { errno = EEXIST; return -1; } handle_info = malloc(sizeof(udp_handle_info_t)); if (!handle_info) { return -1; } memset(handle_info, 0, sizeof(udp_handle_info_t)); knet_h->transports[KNET_TRANSPORT_UDP] = handle_info; qb_list_init(&handle_info->links_list); return 0; } #if defined (IP_RECVERR) || defined (IPV6_RECVERR) static int read_errs_from_sock(knet_handle_t knet_h, int sockfd) { int err = 0, savederrno = 0; int got_err = 0; char buffer[1024]; struct iovec iov; struct msghdr msg; struct cmsghdr *cmsg; struct sock_extended_err *sock_err; struct icmphdr icmph; struct sockaddr_storage remote; struct sockaddr_storage *origin; char addr_str[KNET_MAX_HOST_LEN]; char port_str[KNET_MAX_PORT_LEN]; char addr_remote_str[KNET_MAX_HOST_LEN]; char port_remote_str[KNET_MAX_PORT_LEN]; iov.iov_base = &icmph; iov.iov_len = sizeof(icmph); msg.msg_name = (void*)&remote; msg.msg_namelen = sizeof(remote); msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_flags = 0; msg.msg_control = buffer; msg.msg_controllen = sizeof(buffer); for (;;) { err = recvmsg(sockfd, &msg, MSG_ERRQUEUE); savederrno = errno; if (err < 0) { if (!got_err) { errno = savederrno; return -1; } else { return 0; } } got_err = 1; for (cmsg = CMSG_FIRSTHDR(&msg);cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { if (((cmsg->cmsg_level == SOL_IP) && (cmsg->cmsg_type == IP_RECVERR)) || ((cmsg->cmsg_level == SOL_IPV6 && (cmsg->cmsg_type == IPV6_RECVERR)))) { sock_err = (struct sock_extended_err*)(void *)CMSG_DATA(cmsg); if (sock_err) { switch (sock_err->ee_origin) { case SO_EE_ORIGIN_NONE: /* no origin */ case SO_EE_ORIGIN_LOCAL: /* local source (EMSGSIZE) */ if (sock_err->ee_errno == EMSGSIZE || sock_err->ee_errno == EPERM) { if (pthread_mutex_lock(&knet_h->kmtu_mutex) != 0) { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Unable to get mutex lock"); knet_h->kernel_mtu = 0; break; } else { knet_h->kernel_mtu = sock_err->ee_info; log_debug(knet_h, KNET_SUB_TRANSP_UDP, "detected kernel MTU: %u", knet_h->kernel_mtu); pthread_mutex_unlock(&knet_h->kmtu_mutex); } force_pmtud_run(knet_h, KNET_SUB_TRANSP_UDP, 0, 0); } /* * those errors are way too noisy */ break; case SO_EE_ORIGIN_ICMP: /* ICMP */ case SO_EE_ORIGIN_ICMP6: /* ICMP6 */ origin = (struct sockaddr_storage *)(void *)SO_EE_OFFENDER(sock_err); if (knet_addrtostr(origin, sizeof(*origin), addr_str, KNET_MAX_HOST_LEN, port_str, KNET_MAX_PORT_LEN) < 0) { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Received ICMP error from unknown source: %s", strerror(sock_err->ee_errno)); } else { if (knet_addrtostr(&remote, sizeof(remote), addr_remote_str, KNET_MAX_HOST_LEN, port_remote_str, KNET_MAX_PORT_LEN) < 0) { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Received ICMP error from %s: %s destination unknown", addr_str, strerror(sock_err->ee_errno)); } else { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Received ICMP error from %s: %s %s", addr_str, strerror(sock_err->ee_errno), addr_remote_str); if ((sock_err->ee_errno == ECONNREFUSED) || /* knet is not running on the other node */ (sock_err->ee_errno == ECONNABORTED) || /* local kernel closed the socket */ (sock_err->ee_errno == ENONET) || /* network does not exist */ (sock_err->ee_errno == ENETUNREACH) || /* network unreachable */ (sock_err->ee_errno == EHOSTUNREACH) || /* host unreachable */ (sock_err->ee_errno == EHOSTDOWN) || /* host down (from kernel/net/ipv4/icmp.c */ (sock_err->ee_errno == ENETDOWN)) { /* network down */ struct knet_host *host = NULL; struct knet_link *kn_link = NULL; int link_idx, found = 0; for (host = knet_h->host_head; host != NULL; host = host->next) { for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { kn_link = &host->link[link_idx]; if (kn_link->outsock == sockfd) { if (!cmpaddr(&remote, &kn_link->dst_addr)) { found = 1; break; } } } if (found) { break; } } if ((host) && (kn_link) && (kn_link->status.connected)) { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Setting down host %u link %i", host->host_id, kn_link->link_id); /* * setting transport_connected = 0 will trigger * thread_heartbeat link_down process. * * the process terminates calling into transport_link_down * below that will set transport_connected = 1 */ kn_link->transport_connected = 0; } } } } break; } } else { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "No data in MSG_ERRQUEUE"); } } } } } #else static int read_errs_from_sock(knet_handle_t knet_h, int sockfd) { return 0; } #endif int udp_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) { if (recv_errno == EAGAIN) { read_errs_from_sock(knet_h, sockfd); } return 0; } transport_sock_error_t udp_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int subsys, int recv_err, int recv_errno) { if (recv_err < 0) { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "tx_sock_error, subsys=%d, recv_err=%d, recv_errno=%d", subsys, recv_err, recv_errno); if ((recv_errno == EMSGSIZE) || ((recv_errno == EPERM) && ((subsys == KNET_SUB_TX) || (subsys == KNET_SUB_PMTUD)))) { read_errs_from_sock(knet_h, sockfd); return KNET_TRANSPORT_SOCK_ERROR_IGNORE; } if ((recv_errno == EINVAL) || (recv_errno == EPERM) || (recv_errno == ENETUNREACH) || (recv_errno == ENETDOWN) || (recv_errno == EHOSTUNREACH)) { #ifdef DEBUG if ((recv_errno == ENETUNREACH) || (recv_errno == ENETDOWN)) { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Sock: %d is unreachable.", sockfd); } #endif return KNET_TRANSPORT_SOCK_ERROR_INTERNAL; } if ((recv_errno == ENOBUFS) || (recv_errno == EAGAIN)) { #ifdef DEBUG log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Sock: %d is overloaded. Slowing TX down", sockfd); #endif usleep(knet_h->threads_timer_res / 16); } else { read_errs_from_sock(knet_h, sockfd); } return KNET_TRANSPORT_SOCK_ERROR_RETRY; } return KNET_TRANSPORT_SOCK_ERROR_IGNORE; } transport_rx_isdata_t udp_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg) { if (msg->msg_len == 0) return KNET_TRANSPORT_RX_NOT_DATA_CONTINUE; return KNET_TRANSPORT_RX_IS_DATA; } int udp_transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link) { kn_link->status.dynconnected = 1; return 0; } int udp_transport_link_is_down(knet_handle_t knet_h, struct knet_link *kn_link) { /* * see comments about handling ICMP error messages */ kn_link->transport_connected = 1; return 0; } diff --git a/libknet/transport_udp.h b/libknet/transport_udp.h index 2211ac12..1c54ef91 100644 --- a/libknet/transport_udp.h +++ b/libknet/transport_udp.h @@ -1,28 +1,28 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include "internals.h" #ifndef __KNET_TRANSPORT_UDP_H__ #define __KNET_TRANSPORT_UDP_H__ #define KNET_PMTUD_UDP_OVERHEAD 8 int udp_transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link); int udp_transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link); int udp_transport_free(knet_handle_t knet_h); int udp_transport_init(knet_handle_t knet_h); int udp_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno); transport_sock_error_t udp_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int subsys, int recv_err, int recv_errno); transport_rx_isdata_t udp_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg); int udp_transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link); int udp_transport_link_is_down(knet_handle_t knet_h, struct knet_link *kn_link); #endif diff --git a/libknet/transports.c b/libknet/transports.c index 67f7cae6..08733ca2 100644 --- a/libknet/transports.c +++ b/libknet/transports.c @@ -1,290 +1,290 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "libknet.h" #include "compat.h" #include "host.h" #include "link.h" #include "logging.h" #include "common.h" #include "transports.h" #include "transport_loopback.h" #include "transport_udp.h" #include "transport_sctp.h" #include "threads_common.h" #define empty_module 0, -1, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, static knet_transport_ops_t transport_modules_cmd[KNET_MAX_TRANSPORTS] = { { "LOOPBACK", KNET_TRANSPORT_LOOPBACK, 1, TRANSPORT_PROTO_LOOPBACK, USE_NO_ACL, TRANSPORT_PROTO_NOT_CONNECTION_ORIENTED, KNET_PMTUD_LOOPBACK_OVERHEAD, loopback_transport_init, loopback_transport_free, loopback_transport_link_set_config, loopback_transport_link_clear_config, loopback_transport_link_dyn_connect, loopback_transport_rx_sock_error, loopback_transport_tx_sock_error, loopback_transport_rx_is_data, loopback_transport_link_is_down }, { "UDP", KNET_TRANSPORT_UDP, 1, TRANSPORT_PROTO_IP_PROTO, USE_GENERIC_ACL, TRANSPORT_PROTO_NOT_CONNECTION_ORIENTED, KNET_PMTUD_UDP_OVERHEAD, udp_transport_init, udp_transport_free, udp_transport_link_set_config, udp_transport_link_clear_config, udp_transport_link_dyn_connect, udp_transport_rx_sock_error, udp_transport_tx_sock_error, udp_transport_rx_is_data, udp_transport_link_is_down }, { "SCTP", KNET_TRANSPORT_SCTP, #ifdef HAVE_NETINET_SCTP_H 1, TRANSPORT_PROTO_IP_PROTO, USE_PROTO_ACL, TRANSPORT_PROTO_IS_CONNECTION_ORIENTED, KNET_PMTUD_SCTP_OVERHEAD, sctp_transport_init, sctp_transport_free, sctp_transport_link_set_config, sctp_transport_link_clear_config, sctp_transport_link_dyn_connect, sctp_transport_rx_sock_error, sctp_transport_tx_sock_error, sctp_transport_rx_is_data, sctp_transport_link_is_down }, #else empty_module #endif { NULL, KNET_MAX_TRANSPORTS, empty_module }; /* * transport wrappers */ int start_all_transports(knet_handle_t knet_h) { int idx = 0, savederrno = 0, err = 0; while (transport_modules_cmd[idx].transport_name != NULL) { if (transport_modules_cmd[idx].built_in) { if (transport_modules_cmd[idx].transport_init(knet_h) < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Failed to allocate transport handle for %s: %s", transport_modules_cmd[idx].transport_name, strerror(savederrno)); err = -1; goto out; } } idx++; } out: errno = savederrno; return err; } void stop_all_transports(knet_handle_t knet_h) { int idx = 0; while (transport_modules_cmd[idx].transport_name != NULL) { if (transport_modules_cmd[idx].built_in) { transport_modules_cmd[idx].transport_free(knet_h); } idx++; } } int transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link, uint8_t transport) { if (!transport_modules_cmd[transport].built_in) { errno = EINVAL; return -1; } kn_link->transport_connected = 0; kn_link->transport = transport; kn_link->proto_overhead = transport_modules_cmd[transport].transport_mtu_overhead; return transport_modules_cmd[transport].transport_link_set_config(knet_h, kn_link); } int transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link) { return transport_modules_cmd[kn_link->transport].transport_link_clear_config(knet_h, kn_link); } int transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link) { return transport_modules_cmd[kn_link->transport].transport_link_dyn_connect(knet_h, sockfd, kn_link); } int transport_rx_sock_error(knet_handle_t knet_h, uint8_t transport, int sockfd, int recv_err, int recv_errno) { return transport_modules_cmd[transport].transport_rx_sock_error(knet_h, sockfd, recv_err, recv_errno); } int transport_tx_sock_error(knet_handle_t knet_h, uint8_t transport, int sockfd, int subsys, int recv_err, int recv_errno) { return transport_modules_cmd[transport].transport_tx_sock_error(knet_h, sockfd, subsys, recv_err, recv_errno); } int transport_rx_is_data(knet_handle_t knet_h, uint8_t transport, int sockfd, struct knet_mmsghdr *msg) { return transport_modules_cmd[transport].transport_rx_is_data(knet_h, sockfd, msg); } int transport_get_proto(knet_handle_t knet_h, uint8_t transport) { return transport_modules_cmd[transport].transport_protocol; } int transport_get_acl_type(knet_handle_t knet_h, uint8_t transport) { return transport_modules_cmd[transport].transport_acl_type; } int transport_get_connection_oriented(knet_handle_t knet_h, uint8_t transport) { return transport_modules_cmd[transport].transport_is_connection_oriented; } int transport_link_is_down(knet_handle_t knet_h, struct knet_link *kn_link) { return transport_modules_cmd[kn_link->transport].transport_link_is_down(knet_h, kn_link); } /* * public api */ int knet_get_transport_list(struct knet_transport_info *transport_list, size_t *transport_list_entries) { int err = 0; int idx = 0; int outidx = 0; if (!transport_list_entries) { errno = EINVAL; return -1; } while (transport_modules_cmd[idx].transport_name != NULL) { if (transport_modules_cmd[idx].built_in) { if (transport_list) { transport_list[outidx].name = transport_modules_cmd[idx].transport_name; transport_list[outidx].id = transport_modules_cmd[idx].transport_id; } outidx++; } idx++; } *transport_list_entries = outidx; if (!err) errno = 0; return err; } const char *knet_get_transport_name_by_id(uint8_t transport) { int savederrno = 0; const char *name = NULL; if (transport == KNET_MAX_TRANSPORTS) { errno = EINVAL; return name; } if ((transport_modules_cmd[transport].transport_name) && (transport_modules_cmd[transport].built_in)) { name = transport_modules_cmd[transport].transport_name; } else { savederrno = ENOENT; } errno = name ? 0 : savederrno; return name; } uint8_t knet_get_transport_id_by_name(const char *name) { int savederrno = 0; uint8_t err = KNET_MAX_TRANSPORTS; int i, found; if (!name) { errno = EINVAL; return err; } i = 0; found = 0; while (transport_modules_cmd[i].transport_name != NULL) { if (transport_modules_cmd[i].built_in) { if (!strcmp(transport_modules_cmd[i].transport_name, name)) { err = transport_modules_cmd[i].transport_id; found = 1; break; } } i++; } if (!found) { savederrno = EINVAL; } errno = err == KNET_MAX_TRANSPORTS ? savederrno : 0; return err; } int knet_handle_set_transport_reconnect_interval(knet_handle_t knet_h, uint32_t msecs) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!msecs) { errno = EINVAL; return -1; } if (msecs < 1000) { log_warn(knet_h, KNET_SUB_HANDLE, "reconnect internval below 1 sec (%u msecs) might be too aggressive", msecs); } if (msecs > 60000) { log_warn(knet_h, KNET_SUB_HANDLE, "reconnect internval above 1 minute (%u msecs) could cause long delays in network convergiance", msecs); } savederrno = get_global_wrlock(knet_h); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->reconnect_int = msecs; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } int knet_handle_get_transport_reconnect_interval(knet_handle_t knet_h, uint32_t *msecs) { int savederrno = 0; if (!_is_valid_handle(knet_h)) { return -1; } if (!msecs) { errno = EINVAL; return -1; } savederrno = pthread_rwlock_rdlock(&knet_h->global_rwlock); if (savederrno) { log_err(knet_h, KNET_SUB_HANDLE, "Unable to get read lock: %s", strerror(savederrno)); errno = savederrno; return -1; } *msecs = knet_h->reconnect_int; pthread_rwlock_unlock(&knet_h->global_rwlock); errno = 0; return 0; } diff --git a/libknet/transports.h b/libknet/transports.h index 0ceac2a7..f5a683a6 100644 --- a/libknet/transports.h +++ b/libknet/transports.h @@ -1,26 +1,26 @@ /* - * Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __KNET_TRANSPORTS_H__ #define __KNET_TRANSPORTS_H__ int start_all_transports(knet_handle_t knet_h); void stop_all_transports(knet_handle_t knet_h); int transport_link_set_config(knet_handle_t knet_h, struct knet_link *kn_link, uint8_t transport); int transport_link_clear_config(knet_handle_t knet_h, struct knet_link *kn_link); int transport_link_dyn_connect(knet_handle_t knet_h, int sockfd, struct knet_link *kn_link); int transport_rx_sock_error(knet_handle_t knet_h, uint8_t transport, int sockfd, int recv_err, int recv_errno); int transport_tx_sock_error(knet_handle_t knet_h, uint8_t transport, int sockfd, int subsys, int recv_err, int recv_errno); int transport_rx_is_data(knet_handle_t knet_h, uint8_t transport, int sockfd, struct knet_mmsghdr *msg); int transport_get_proto(knet_handle_t knet_h, uint8_t transport); int transport_get_acl_type(knet_handle_t knet_h, uint8_t transport); int transport_get_connection_oriented(knet_handle_t knet_h, uint8_t transport); int transport_link_is_down(knet_handle_t knet_h, struct knet_link *link); #endif diff --git a/libnozzle/Makefile.am b/libnozzle/Makefile.am index 8e18b9a4..6a8382a1 100644 --- a/libnozzle/Makefile.am +++ b/libnozzle/Makefile.am @@ -1,49 +1,49 @@ # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk SYMFILE = libnozzle_exported_syms EXTRA_DIST = $(SYMFILE) if BUILD_LIBNOZZLE SUBDIRS = . tests bindings sources = libnozzle.c \ internals.c include_HEADERS = libnozzle.h pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libnozzle.pc noinst_HEADERS = \ internals.h lib_LTLIBRARIES = libnozzle.la libnozzle_la_SOURCES = $(sources) libnozzle_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) $(libnl_CFLAGS) $(libnlroute_CFLAGS) EXTRA_libnozzle_la_DEPENDENCIES = $(SYMFILE) libnozzle_la_LDFLAGS = $(AM_LDFLAGS) \ -Wl,-version-script,$(srcdir)/$(SYMFILE) \ -version-info $(libnozzleversion) libnozzle_la_LIBADD = $(PTHREAD_LIBS) $(libnl_LIBS) $(libnlroute_LIBS) check-local: check-annocheck-libs endif diff --git a/libnozzle/bindings/Makefile.am b/libnozzle/bindings/Makefile.am index 46a860a5..2a3d7536 100644 --- a/libnozzle/bindings/Makefile.am +++ b/libnozzle/bindings/Makefile.am @@ -1,17 +1,17 @@ # -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk SUBDIRS = . if BUILD_RUST_BINDINGS SUBDIRS += rust endif diff --git a/libnozzle/bindings/rust/Cargo.toml.in b/libnozzle/bindings/rust/Cargo.toml.in index 0a83c0e6..fbace2e1 100644 --- a/libnozzle/bindings/rust/Cargo.toml.in +++ b/libnozzle/bindings/rust/Cargo.toml.in @@ -1,29 +1,29 @@ -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under LGPL-2.1+ [package] name = "nozzle-bindings" version = "@libnozzlerustver@" authors = ["Christine Caulfield "] edition = "2018" readme = "README" license = "LGPL-2.1+" repository = "https://github.com/kronosnet/kronosnet" description = "Rust bindings for libnozzle library" categories = ["api-bindings"] keywords = ["cluster", "high-availability"] exclude = [ "*.in", "Makefile*", ] [dependencies] bitflags = "1.2.1" lazy_static = "1.4.0" os_socketaddr = "0.2.0" libc = "0.2.93" diff --git a/libnozzle/bindings/rust/Makefile.am b/libnozzle/bindings/rust/Makefile.am index 1f22af29..05d70142 100644 --- a/libnozzle/bindings/rust/Makefile.am +++ b/libnozzle/bindings/rust/Makefile.am @@ -1,37 +1,37 @@ # -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk include $(top_srcdir)/build-aux/rust.mk # required for make check localver = $(libnozzlerustver) SUBDIRS = . tests EXTRA_DIST = \ $(RUST_COMMON) \ $(RUST_SHIP_SRCS) \ README RUST_SHIP_SRCS = \ src/nozzle_bindings.rs \ src/lib.rs \ src/sys/mod.rs RUST_BUILT_SRCS = \ src/sys/libnozzle.rs src/sys/libnozzle.rs: ../../libnozzle.h $(top_srcdir)/build-aux/rust-regen.sh $^ $@ NOZZLE all-local: cargo-tree-prep target/$(RUST_TARGET_DIR)/nozzle_bindings.rlib clean-local: cargo-clean diff --git a/libnozzle/bindings/rust/README b/libnozzle/bindings/rust/README index c89533ec..3725877f 100644 --- a/libnozzle/bindings/rust/README +++ b/libnozzle/bindings/rust/README @@ -1,10 +1,10 @@ -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under GPL-2.0+ This crate contains Rust bindings for the libnozzle library, part of kornosnet: https://kronosnet.org/ libnozzle is a commodity library to manage tap (ethernet) interfaces diff --git a/libnozzle/bindings/rust/build.rs.in b/libnozzle/bindings/rust/build.rs.in index 1ab78a1a..a6f6a885 100644 --- a/libnozzle/bindings/rust/build.rs.in +++ b/libnozzle/bindings/rust/build.rs.in @@ -1,11 +1,11 @@ -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // fn main() { println!("cargo:rustc-link-search=native=../../../.libs"); println!("cargo:rustc-link-lib=dylib=nozzle"); } diff --git a/libnozzle/bindings/rust/src/lib.rs b/libnozzle/bindings/rust/src/lib.rs index 1ff90418..e32bb390 100644 --- a/libnozzle/bindings/rust/src/lib.rs +++ b/libnozzle/bindings/rust/src/lib.rs @@ -1,124 +1,124 @@ -// Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +// Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. // // Authors: Christine Caulfield // // This software licensed under LGPL-2.0+ // //! This crate provides access to the kronosnet libraries libknet and libnozzle //! from Rust. They are a fairly thin layer around the actual API calls but with Rust data types //! and iterators. //! //! No more information about knet itself will be provided here, it is expected that if //! you feel you need access to the knet API calls, you know what they do :) //! //! # Example //! ``` //! use nozzle_bindings::nozzle_bindings as nozzle; //! use std::io::{Result}; //! use std::env; //! use std::{thread, time}; //! //! fn main() -> Result<()> //! { //! let mut nozzle_name = String::from("rustnoz"); //! let handle = match nozzle::open(&mut nozzle_name, &String::from(env::current_dir().unwrap().to_str().unwrap())) { //! Ok(h) => { //! println!("Opened device {}", nozzle_name); //! h //! }, //! Err(e) => { //! println!("Error from open: {}", e); //! return Err(e); //! } //! }; //! //! let if Err(e) = nozzle::add_ip(handle, &"192.160.100.1".to_string(), &"24".to_string()) { //! println!("Error from add_ip: {}", e); //! return Err(e); //! } //! //! let if Err(e) = nozzle::set_mtu(handle, 157) { //! println!("Error from set_mtu: {}", e); //! return Err(e); //! } //! //! Ok(()) //! } mod sys; pub mod nozzle_bindings; use std::os::raw::c_char; use std::ptr::copy_nonoverlapping; use std::ffi::CString; use std::io::{Error, Result, ErrorKind}; // General internal routine to copy bytes from a C array into a Rust String fn string_from_bytes(bytes: *const ::std::os::raw::c_char, max_length: usize) -> Result { let mut newbytes = Vec::::new(); newbytes.resize(max_length, 0u8); // Get length of the string in old-fashioned style let mut length: usize = 0; let mut count = 0; let mut tmpbytes = bytes; while count < max_length || length == 0 { if unsafe {*tmpbytes} == 0 && length == 0 { length = count; break; } count += 1; tmpbytes = unsafe { tmpbytes.offset(1) } } // Cope with an empty string if length == 0 { return Ok(String::new()); } unsafe { // We need to fully copy it, not shallow copy it. // Messy casting on both parts of the copy here to get it to work on both signed // and unsigned char machines copy_nonoverlapping(bytes as *mut i8, newbytes.as_mut_ptr() as *mut i8, length); } let cs = CString::new(&newbytes[0..length as usize])?; // This is just to convert the error type match cs.into_string() { Ok(s) => Ok(s), Err(_) => Err(Error::new(ErrorKind::Other, "Cannot convert to String")), } } // As below but always returns a string even if there was an error doing the conversion fn string_from_bytes_safe(bytes: *const ::std::os::raw::c_char, max_length: usize) -> String { match string_from_bytes(bytes, max_length) { Ok(s) => s, Err(_)=> "".to_string() } } fn string_to_bytes(s: &str, bytes: &mut [c_char]) -> Result<()> { let c_name = match CString::new(s) { Ok(n) => n, Err(_) => return Err(Error::new(ErrorKind::Other, "Rust conversion error")), }; if c_name.as_bytes().len() > bytes.len() { return Err(Error::new(ErrorKind::Other, "String too long")); } unsafe { // NOTE param order is 'wrong-way round' from C copy_nonoverlapping(c_name.as_ptr(), bytes.as_mut_ptr(), c_name.as_bytes().len()); } Ok(()) } diff --git a/libnozzle/bindings/rust/src/nozzle_bindings.rs b/libnozzle/bindings/rust/src/nozzle_bindings.rs index b9185f05..d3de7b65 100644 --- a/libnozzle/bindings/rust/src/nozzle_bindings.rs +++ b/libnozzle/bindings/rust/src/nozzle_bindings.rs @@ -1,367 +1,367 @@ // libnozzle interface for Rust -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // // For the code generated by bindgen use crate::sys::libnozzle as ffi; use std::io::{Result, Error, ErrorKind}; use std::os::raw::{c_char, c_void}; use std::ptr::{null_mut}; use libc::free; use std::fmt; /// A handle into the nozzle library. Returned from [open] and needed for all other calls #[derive(Copy, Clone, PartialEq, Eq)] pub struct Handle { nozzle_handle: ffi::nozzle_t, } const IFNAMSZ: usize = 16; /// Create a new tap device on the system pub fn open(devname: &mut String, updownpath: &str) -> Result { let mut c_devname: [c_char; IFNAMSZ] = [0; IFNAMSZ]; let mut c_updownpath: [c_char; libc::PATH_MAX as usize] = [0; libc::PATH_MAX as usize]; let c_devname_size = IFNAMSZ; crate::string_to_bytes(devname, &mut c_devname)?; crate::string_to_bytes(updownpath, &mut c_updownpath)?; let res = unsafe { ffi::nozzle_open(c_devname.as_mut_ptr(), c_devname_size, c_updownpath.as_ptr()) }; if res.is_null() { Err(Error::last_os_error()) } else { let temp = crate::string_from_bytes(c_devname.as_ptr(), IFNAMSZ)?; *devname = temp; Ok(Handle{nozzle_handle: res}) } } /// Deconfigure and destroy a nozzle device pub fn close(handle: Handle) -> Result<()> { let res = unsafe { ffi::nozzle_close(handle.nozzle_handle) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Which script to run when [run_updown] is called pub enum Action { PreUp, Up, Down, PostDown } impl Action { pub fn to_u8(self: &Action) -> u8 { match self { Action::PreUp => 0, Action::Up => 1, Action::Down => 2, Action::PostDown => 3, } } } /// Run an up/down script before/after configuring a device. See [Action] pub fn run_updown(handle: Handle, action: Action) -> Result { let c_exec_string : *mut *mut ::std::os::raw::c_char = &mut [0;0].as_mut_ptr(); let c_action = action.to_u8(); let res = unsafe { ffi::nozzle_run_updown(handle.nozzle_handle, c_action, c_exec_string) }; match res { 0 => { unsafe { // This is unsafe because we deference a raw pointer let resstring = crate::string_from_bytes(*c_exec_string as *mut ::std::os::raw::c_char, libc::PATH_MAX as usize)?; free(*c_exec_string as *mut c_void); Ok(resstring) } }, -1 => Err(Error::last_os_error()), -2 => Err(Error::new(ErrorKind::Other, "error executing shell scripts")), _ => Err(Error::new(ErrorKind::Other, "unknown error returned from nozzle_tun_updown()")), } } /// Mark nozzle device as "up" pub fn set_up(handle: Handle) -> Result<()> { let res = unsafe { ffi::nozzle_set_up(handle.nozzle_handle) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// mark nozzle device as "down" pub fn set_down(handle: Handle) -> Result<()> { let res = unsafe { ffi::nozzle_set_down(handle.nozzle_handle) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } const IPADDR_CHAR_MAX: usize = 128; const PREFIX_CHAR_MAX: usize = 4; /// Add an ip address to a nozzle device. multiple addresses can be added to one device. /// The prefix is a the number that comes after the ip address when configuring: /// eg: 192.168.0.1/24 - the prefix is "24" pub fn add_ip(handle: Handle, ipaddr: &str, prefix: &str) -> Result<()> { let mut c_ipaddr: [c_char; IPADDR_CHAR_MAX] = [0; IPADDR_CHAR_MAX]; let mut c_prefix: [c_char; PREFIX_CHAR_MAX] = [0; PREFIX_CHAR_MAX]; crate::string_to_bytes(ipaddr, &mut c_ipaddr)?; crate::string_to_bytes(prefix, &mut c_prefix)?; let res = unsafe { ffi::nozzle_add_ip(handle.nozzle_handle, c_ipaddr.as_ptr(), c_prefix.as_ptr()) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// remove an ip address from a nozzle device pub fn del_ip(handle: Handle, ipaddr: &str, prefix: &str) -> Result<()> { let mut c_ipaddr: [c_char; IPADDR_CHAR_MAX] = [0; IPADDR_CHAR_MAX]; let mut c_prefix: [c_char; PREFIX_CHAR_MAX] = [0; PREFIX_CHAR_MAX]; crate::string_to_bytes(ipaddr, &mut c_ipaddr)?; crate::string_to_bytes(prefix, &mut c_prefix)?; let res = unsafe { ffi::nozzle_del_ip(handle.nozzle_handle, c_ipaddr.as_ptr(), c_prefix.as_ptr()) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// IpV4/IpV6 in the struct returned from [get_ips] pub enum Domain { IpV4, IpV6 } impl Domain { fn new(c_dom: i32) -> Domain { match c_dom { libc::AF_INET => Domain::IpV4, libc::AF_INET6 => Domain::IpV6, _ => Domain::IpV4, } } } impl fmt::Display for Domain { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Domain::IpV4 => write!(f, "IPv4"), Domain::IpV6 => write!(f, "IPv6"), } } } /// IP address info as returned from [get_ips] pub struct Ip { pub ipaddr: String, pub prefix: String, pub domain: Domain, } impl Ip { pub fn new(c_ip: &ffi::nozzle_ip) -> Ip { Ip { ipaddr: crate::string_from_bytes_safe(c_ip.ipaddr.as_ptr(), IPADDR_CHAR_MAX), prefix: crate::string_from_bytes_safe(c_ip.prefix.as_ptr(), PREFIX_CHAR_MAX), domain: Domain::new(c_ip.domain) } } } impl fmt::Display for Ip { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f,"{} {}/{}", self.domain, self.ipaddr, self.prefix)?; Ok(()) } } /// Return a Vec of Ip adressess attached to this device pub fn get_ips(handle: Handle) -> Result> { let mut c_ips : &mut ffi::nozzle_ip = &mut ffi::nozzle_ip{ipaddr: [0;129], prefix: [0;5], domain:0, next: null_mut()}; let res = unsafe { ffi::nozzle_get_ips(handle.nozzle_handle, &mut c_ips as *mut _ as *mut *mut ffi::nozzle_ip) }; let mut ipvec = Vec::::new(); if res == 0 { let mut ips : *mut ffi::nozzle_ip = c_ips; unsafe { while !ips.is_null() { ipvec.push(Ip::new(&*ips)); ips = (*ips).next; } } Ok(ipvec) } else { Err(Error::last_os_error()) } } /// Get the MTU of the device pub fn get_mtu(handle: Handle) -> Result { let res = unsafe { ffi::nozzle_get_mtu(handle.nozzle_handle) }; if res != -1 { Ok(res) } else { Err(Error::last_os_error()) } } /// Set the MTU of the device pub fn set_mtu(handle: Handle, new_mtu: i32) -> Result<()> { let res = unsafe { ffi::nozzle_set_mtu(handle.nozzle_handle, new_mtu) }; if res != -1 { Ok(()) } else { Err(Error::last_os_error()) } } /// Reset the device's MTU back to the default pub fn reset_mtu(handle: Handle) -> Result<()> { let res = unsafe { ffi::nozzle_reset_mtu(handle.nozzle_handle) }; if res != -1 { Ok(()) } else { Err(Error::last_os_error()) } } /// Returns the MAC address of the device pub fn get_mac(handle: Handle) -> Result { let mut c_mac: *mut c_char = null_mut(); let res = unsafe { ffi::nozzle_get_mac(handle.nozzle_handle, &mut c_mac) }; if res == 0 { let mac = crate::string_from_bytes(c_mac, 24_usize)?; // Needs to be 8byte aligned unsafe { free(c_mac as *mut c_void); }// Was created with strdup( Ok(mac) } else { Err(Error::last_os_error()) } } /// Setsthe MAC address of the device pub fn set_mac(handle: Handle, ether_addr: &str) -> Result<()> { let mut c_mac: [c_char; 24_usize] = [0; 24_usize]; // Needs to be 8byte aligned crate::string_to_bytes(ether_addr, &mut c_mac)?; let res = unsafe { ffi::nozzle_set_mac(handle.nozzle_handle, c_mac.as_ptr()) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Reset the device's MAC address to the defaut pub fn reset_mac(handle: Handle) -> Result<()> { let res = unsafe { ffi::nozzle_reset_mac(handle.nozzle_handle) }; if res == 0 { Ok(()) } else { Err(Error::last_os_error()) } } /// Find the nozzle handle of a device by giving its name pub fn get_handle_by_name(devname: &str) -> Result { let mut c_devname: [c_char; IFNAMSZ] = [0; IFNAMSZ]; crate::string_to_bytes(devname, &mut c_devname)?; let res = unsafe { ffi::nozzle_get_handle_by_name(c_devname.as_ptr()) }; if !res.is_null() { Ok(Handle{nozzle_handle:res}) } else { Err(Error::last_os_error()) } } /// Return the name of the device pub fn get_name_by_handle(handle: Handle) -> Result { let res = unsafe { ffi::nozzle_get_name_by_handle(handle.nozzle_handle) }; if !res.is_null() { crate::string_from_bytes(res, IFNAMSZ) } else { Err(Error::last_os_error()) } } /// Return a unix FD for the device pub fn get_fd(handle: Handle) -> Result { let res = unsafe { ffi::nozzle_get_fd(handle.nozzle_handle) }; if res != -1 { Ok(res) } else { Err(Error::last_os_error()) } } diff --git a/libnozzle/bindings/rust/src/sys/mod.rs b/libnozzle/bindings/rust/src/sys/mod.rs index 11b2bcf6..2c7643ef 100644 --- a/libnozzle/bindings/rust/src/sys/mod.rs +++ b/libnozzle/bindings/rust/src/sys/mod.rs @@ -1,10 +1,10 @@ -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // #![allow(non_camel_case_types, non_snake_case, dead_code, improper_ctypes)] pub mod libnozzle; diff --git a/libnozzle/bindings/rust/tests/Cargo.toml.in b/libnozzle/bindings/rust/tests/Cargo.toml.in index 6c53b2e0..0ba17f43 100644 --- a/libnozzle/bindings/rust/tests/Cargo.toml.in +++ b/libnozzle/bindings/rust/tests/Cargo.toml.in @@ -1,24 +1,24 @@ -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under GPL-2.0+ [package] name = "nozzle-bindings-tests" version = "@libnozzlerustver@" authors = ["Christine Caulfield "] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] nozzle-bindings = { path = ".." } libc = "0.2.93" tempfile = "3" [[bin]] name = "nozzle-test" test = true bench = false diff --git a/libnozzle/bindings/rust/tests/Makefile.am b/libnozzle/bindings/rust/tests/Makefile.am index 8c000e0b..ac922b82 100644 --- a/libnozzle/bindings/rust/tests/Makefile.am +++ b/libnozzle/bindings/rust/tests/Makefile.am @@ -1,33 +1,33 @@ # -# Copyright (C) 2021-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2021-2023 Red Hat, Inc. All rights reserved. # # Author: Christine Caulfield # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk include $(top_srcdir)/build-aux/rust.mk EXTRA_DIST = \ $(RUST_COMMON) \ $(RUST_SHIP_SRCS) RUST_SHIP_SRCS = src/bin/nozzle-test.rs check_SCRIPTS = target/$(RUST_TARGET_DIR)/nozzle-test noinst_SCRIPTS = $(check_SCRIPTS) if INSTALL_TESTS testsuitedir = $(TESTDIR) testsuite_SCRIPTS = $(check_SCRIPTS) endif AM_TESTS_ENVIRONMENT=LD_LIBRARY_PATH="$(abs_top_builddir)/libnozzle/.libs" TESTS = $(check_SCRIPTS) clean-local: cargo-clean diff --git a/libnozzle/bindings/rust/tests/build.rs.in b/libnozzle/bindings/rust/tests/build.rs.in index d3574709..65a65a06 100644 --- a/libnozzle/bindings/rust/tests/build.rs.in +++ b/libnozzle/bindings/rust/tests/build.rs.in @@ -1,13 +1,13 @@ -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // fn main() { // Tell the compiler to use the build-tree libs & headers for compiling println!("cargo:rustc-link-search=native=../../../.libs/"); println!("cargo:rustc-link-lib=nozzle"); } diff --git a/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs b/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs index 43bb7327..79638c86 100644 --- a/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs +++ b/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs @@ -1,245 +1,245 @@ // Testing the Nozzle Rust APIs // -// Copyright (C) 2021-2022 Red Hat, Inc. +// Copyright (C) 2021-2023 Red Hat, Inc. // // All rights reserved. // // Author: Christine Caulfield (ccaulfi@redhat.com) // use nozzle_bindings::nozzle_bindings as nozzle; use std::io::{Result, Error, ErrorKind, BufWriter, Write}; use std::fmt::Write as fmtwrite; use std::{thread, time}; use std::fs::File; use std::fs; use tempfile::tempdir; const SKIP: i32 = 77; fn main() -> Result<()> { // We must be root if unsafe { libc::geteuid() != 0 } { std::process::exit(SKIP); } // Run in a random tmpdir so we don't clash with other instances let tmp_path = tempdir()?; let tmp_dir = match tmp_path.path().to_str() { Some(td) => td, None => { println!("Error creating temp path for running"); return Err(Error::new(ErrorKind::Other, "Error creating temp path")); } }; std::env::set_current_dir(tmp_dir)?; // Let the OS generate a tap name let mut nozzle_name = String::from(""); let handle = match nozzle::open(&mut nozzle_name, tmp_dir) { Ok(h) => { println!("Opened device {}", nozzle_name); h }, Err(e) => { println!("Error from open: {}", e); return Err(e); } }; // Get default state for checking reset_* calls later let saved_mtu = match nozzle::get_mtu(handle) { Ok(m) => m, Err(e) => { println!("Error from get_mtu: {}", e); return Err(e); } }; let saved_mac = match nozzle::get_mac(handle) { Ok(m) => m, Err(e) => { println!("Error from get_mac: {}", e); return Err(e); } }; // Play with APIs if let Err(e) = nozzle::add_ip(handle, "192.160.100.1", "24") { println!("Error from add_ip: {}", e); return Err(e); } if let Err(e) = nozzle::add_ip(handle, "192.160.100.2", "24") { println!("Error from add_ip2: {}", e); return Err(e); } if let Err(e) = nozzle::add_ip(handle, "192.160.100.3", "24") { println!("Error from add_ip3: {}", e); return Err(e); } if let Err(e) = nozzle::set_mac(handle, "AA:00:04:00:22:01") { println!("Error from set_mac: {}", e); return Err(e); } if let Err(e) = nozzle::set_mtu(handle, 157) { println!("Error from set_mtu: {}", e); return Err(e); } if let Err(e) = nozzle::set_up(handle) { println!("Error from set_up: {}", e); return Err(e); } // Create the 'up' script so we can test the run_updown() function, let up_path = std::path::Path::new("up.d"); if let Err(e) = fs::create_dir_all(up_path) { eprintln!("Error creating up.d directory: {:?}", e); return Err(e); } let mut up_filename = String::new(); if let Err(e) = write!(up_filename, "up.d/{}", nozzle_name) { eprintln!("Error making up.d filename: {:?}", e); return Err(Error::new(ErrorKind::Other, "Error making up.d filename")); } match File::create(&up_filename) { Err(e) => { println!("Cannot create up.d file {}: {}", &up_filename, e); return Err(e); } Ok(fl) => { let mut f = BufWriter::new(fl); writeln!(f, "#!/bin/sh\necho 'This is a test of an \"Up\" script'")?; } } // A grotty way to do chmod, but normally this would be distributed by the sysadmin unsafe { let up_cstring = std::ffi::CString::new(up_filename.clone()).unwrap(); libc::chmod(up_cstring.as_ptr(), 0o700); } match nozzle::run_updown(handle, nozzle::Action::Up) { Ok(s) => println!("Returned from Up script: {}", s), Err(e) => { println!("Error from run_updown: {}", e); return Err(e); } } // Tidy up after ourself - remove the up.d/tapX file fs::remove_file(&up_filename)?; fs::remove_dir("up.d")?; match nozzle::get_ips(handle) { Ok(ips) => { print!("Got IPs:"); for i in ips { print!(" {}", i); } println!(); }, Err(e) => { println!("Error from get_ips: {}", e); return Err(e); } } match nozzle::get_mtu(handle) { Ok(m) => println!("Got mtu: {}", m), Err(e) => { println!("Error from get_ips: {}", e); return Err(e); } } match nozzle::get_mac(handle) { Ok(m) => println!("Got mac: {}", m), Err(e) => { println!("Error from get_ips: {}", e); return Err(e); } } match nozzle::get_fd(handle) { Ok(f) => println!("Got FD: {}", f), Err(e) => { println!("Error from get_fd: {}", e); return Err(e); } } match nozzle::get_handle_by_name(&nozzle_name) { Ok(h) => if h != handle { return Err(Error::new(ErrorKind::Other, "get_handle_by_name returned wrong value")); } Err(e) => { println!("Error from get_ips: {}", e); return Err(e); } } match nozzle::get_name_by_handle(handle) { Ok(n) => if n != nozzle_name { println!("n: {}, nozzle_name: {}", n, nozzle_name); return Err(Error::new(ErrorKind::Other, "get_name_by_handle returned wrong name")); } Err(e) => { println!("Error from get_ips: {}", e); return Err(e); } } // Wait a little while in case user wants to check with 'ip' command thread::sleep(time::Duration::from_millis(1000)); if let Err(e) = nozzle::del_ip(handle, "192.160.100.3", "24") { println!("Error from del_ip: {}", e); return Err(e); } if let Err(e) = nozzle::reset_mtu(handle) { println!("Error from reset_mtu: {}", e); return Err(e); } match nozzle::get_mtu(handle) { Ok(m) => { if m != saved_mtu { println!("Got default MTU of {}, not {}", m, saved_mtu); } } Err(e) => { println!("Error from get_ips: {}", e); return Err(e); } } if let Err(e) = nozzle::reset_mac(handle) { println!("Error from reset_mac: {}", e); return Err(e); } match nozzle::get_mac(handle) { Ok(m) => { if m != saved_mac { println!("Got default MAC of {}, not {}", m, saved_mac); } } Err(e) => { println!("Error from get_ips: {}", e); return Err(e); } } if let Err(e) = nozzle::set_down(handle){ println!("Error from set_down: {}", e); return Err(e); } if let Err(e) = nozzle::close(handle) { println!("Error from open: {}", e); return Err(e); } Ok(()) } diff --git a/libnozzle/internals.c b/libnozzle/internals.c index 897478eb..0c5a1231 100644 --- a/libnozzle/internals.c +++ b/libnozzle/internals.c @@ -1,186 +1,186 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include "libnozzle.h" #include "internals.h" static int read_pipe(int fd, char **file, size_t *length) { char buf[4096]; int n; int done = 0; *file = NULL; *length = 0; memset(buf, 0, sizeof(buf)); while (!done) { n = read(fd, buf, sizeof(buf)); if (n < 0) { if (errno == EINTR) continue; if (*file) free(*file); return n; } if (n == 0 && (!*length)) return 0; if (n == 0) done = 1; if (*file) *file = realloc(*file, (*length) + n + done); else *file = malloc(n + done); if (!*file) return -1; memmove((*file) + (*length), buf, n); *length += (done + n); } /* Null terminator */ (*file)[(*length) - 1] = 0; return 0; } int execute_bin_sh_command(const char *command, char **error_string) { pid_t pid; int status, err = 0; int fd[2]; size_t size = 0; if ((command == NULL) || (!error_string)) { errno = EINVAL; return -1; } *error_string = NULL; err = pipe(fd); if (err) goto out_clean; pid = fork(); if (pid < 0) { err = pid; goto out_clean; } if (pid) { /* parent */ close(fd[1]); err = read_pipe(fd[0], error_string, &size); if (err) goto out_clean0; waitpid(pid, &status, 0); if (!WIFEXITED(status)) { err = -1; goto out_clean0; } if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { err = WEXITSTATUS(status); goto out_clean0; } goto out_clean0; } else { /* child */ close(0); close(1); close(2); close(fd[0]); dup2(fd[1], 1); dup2(fd[1], 2); close(fd[1]); execlp("/bin/sh", "/bin/sh", "-c", command, NULL); exit(EXIT_FAILURE); } out_clean: close(fd[1]); out_clean0: close(fd[0]); return err; } char *generate_v4_broadcast(const char *ipaddr, const char *prefix) { int prefix_len; struct in_addr mask; struct in_addr broadcast; struct in_addr address; char buf[INET6_ADDRSTRLEN]; prefix_len = atoi(prefix); if ((prefix_len > 32) || (prefix_len <= 0)) return NULL; if (inet_pton(AF_INET, ipaddr, &address) <= 0) return NULL; mask.s_addr = htonl(~((1 << (32 - prefix_len)) - 1)); memset(&broadcast, 0, sizeof(broadcast)); broadcast.s_addr = (address.s_addr & mask.s_addr) | ~mask.s_addr; return strdup(inet_ntop(AF_INET, (void *)&broadcast, buf, sizeof(buf))); } int find_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix, struct nozzle_ip **ip, struct nozzle_ip **ip_prev) { struct nozzle_ip *local_ip, *local_ip_prev; int found = 0; local_ip = local_ip_prev = nozzle->ip; while(local_ip) { if ((!strcmp(local_ip->ipaddr, ipaddr)) && (!strcmp(local_ip->prefix, prefix))) { found = 1; break; } local_ip_prev = local_ip; local_ip = local_ip->next; } if (found) { *ip = local_ip; *ip_prev = local_ip_prev; } return found; } diff --git a/libnozzle/internals.h b/libnozzle/internals.h index 4e2214f3..9e87949b 100644 --- a/libnozzle/internals.h +++ b/libnozzle/internals.h @@ -1,69 +1,69 @@ /* - * Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __NOZZLE_INTERNALS_H__ #define __NOZZLE_INTERNALS_H__ #include "config.h" #ifdef KNET_LINUX #include #endif #include #include "libnozzle.h" struct nozzle_lib_config { struct nozzle_iface *head; int ioctlfd; #ifdef KNET_LINUX struct nl_sock *nlsock; #endif }; #define MACADDR_CHAR_MAX 18 /* * 11 = post-down.d * 1 = / */ #define UPDOWN_PATH_MAX PATH_MAX - 11 - 1 - IFNAMSIZ struct nozzle_iface { char name[IFNAMSIZ]; /* interface name */ int fd; /* interface fd */ int up; /* interface status 0 is down, 1 is up */ /* * extra data */ struct nozzle_ip *ip; /* configured ip addresses */ /* * default MAC address assigned by the kernel at creation time */ char default_mac[MACADDR_CHAR_MAX + 1]; int default_mtu; /* MTU assigned by the kernel at creation time */ int current_mtu; /* MTU configured by libnozzle user */ int hasupdown; /* interface has up/down path to scripts configured */ char updownpath[UPDOWN_PATH_MAX]; /* path to up/down scripts if configured */ struct nozzle_iface *next; }; #define ifname ifr.ifr_name int execute_bin_sh_command(const char *command, char **error_string); int find_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix, struct nozzle_ip **ip, struct nozzle_ip **ip_prev); char *generate_v4_broadcast(const char *ipaddr, const char *prefix); #endif diff --git a/libnozzle/libnozzle.c b/libnozzle/libnozzle.c index 1699b525..74de3f8d 100644 --- a/libnozzle/libnozzle.c +++ b/libnozzle/libnozzle.c @@ -1,1243 +1,1243 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef KNET_LINUX #include /* * libnl3 < 3.3 includes kernel headers directly * causing conflicts with net/if.h included above */ #ifdef LIBNL3_WORKAROUND #define _LINUX_IF_H 1 #endif #include #include #include #include #endif #ifdef KNET_BSD #include #include #endif #include "libnozzle.h" #include "internals.h" /* * internal functions are all _unlocked_ * locking should be handled at external API functions */ static int lib_init = 0; static struct nozzle_lib_config lib_cfg; static pthread_mutex_t config_mutex = PTHREAD_MUTEX_INITIALIZER; /* * internal helpers */ static void lib_fini(void) { if (lib_cfg.head == NULL) { #ifdef KNET_LINUX nl_close(lib_cfg.nlsock); nl_socket_free(lib_cfg.nlsock); #endif close(lib_cfg.ioctlfd); lib_init = 0; } } static int is_valid_nozzle(const nozzle_t nozzle) { nozzle_t temp; if (!nozzle) { return 0; } if (!lib_init) { return 0; } temp = lib_cfg.head; while (temp != NULL) { if (nozzle == temp) return 1; temp = temp->next; } return 0; } static void destroy_iface(nozzle_t nozzle) { #ifdef KNET_BSD struct ifreq ifr; #endif if (!nozzle) return; if (nozzle->fd >= 0) close(nozzle->fd); #ifdef KNET_BSD memset(&ifr, 0, sizeof(struct ifreq)); memmove(ifname, nozzle->name, IFNAMSIZ); ioctl(lib_cfg.ioctlfd, SIOCIFDESTROY, &ifr); #endif free(nozzle); lib_fini(); return; } static int get_iface_mtu(const nozzle_t nozzle) { int err = 0, savederrno = 0; struct ifreq ifr; memset(&ifr, 0, sizeof(struct ifreq)); memmove(ifname, nozzle->name, IFNAMSIZ); err = ioctl(lib_cfg.ioctlfd, SIOCGIFMTU, &ifr); if (err) { savederrno = errno; goto out_clean; } err = ifr.ifr_mtu; out_clean: errno = savederrno; return err; } static int get_iface_mac(const nozzle_t nozzle, char **ether_addr) { int err = 0, savederrno = 0; struct ifreq ifr; char mac[MACADDR_CHAR_MAX]; #ifdef KNET_BSD struct ifaddrs *ifap = NULL; struct ifaddrs *ifa; int found = 0; #endif memset(&mac, 0, MACADDR_CHAR_MAX); memset(&ifr, 0, sizeof(struct ifreq)); memmove(ifname, nozzle->name, IFNAMSIZ); #ifdef KNET_LINUX err = ioctl(lib_cfg.ioctlfd, SIOCGIFHWADDR, &ifr); if (err) { savederrno = errno; goto out_clean; } ether_ntoa_r((struct ether_addr *)ifr.ifr_hwaddr.sa_data, mac); #endif #ifdef KNET_BSD /* * there is no ioctl to get the ether address of an interface on FreeBSD * (not to be confused with hwaddr). Use workaround described here: * https://lists.freebsd.org/pipermail/freebsd-hackers/2004-June/007394.html */ err = getifaddrs(&ifap); if (err < 0) { savederrno = errno; goto out_clean; } ifa = ifap; while (ifa) { if (!strncmp(nozzle->name, ifa->ifa_name, IFNAMSIZ)) { found = 1; break; } ifa=ifa->ifa_next; } if (found) { ether_ntoa_r((struct ether_addr *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr), mac); } else { errno = EINVAL; err = -1; } freeifaddrs(ifap); if (err) { goto out_clean; } #endif *ether_addr = strdup(mac); if (!*ether_addr) { savederrno = errno; err = -1; } out_clean: errno = savederrno; return err; } #define IP_ADD 1 #define IP_DEL 2 static int _set_ip(nozzle_t nozzle, int command, const char *ipaddr, const char *prefix, int secondary) { int fam; char *broadcast = NULL; int err = 0; #ifdef KNET_LINUX struct rtnl_addr *addr = NULL; struct nl_addr *local_addr = NULL; struct nl_addr *bcast_addr = NULL; struct nl_cache *cache = NULL; int ifindex; #endif #ifdef KNET_BSD char cmdline[4096]; char proto[6]; char *error_string = NULL; #endif if (!strchr(ipaddr, ':')) { fam = AF_INET; broadcast = generate_v4_broadcast(ipaddr, prefix); if (!broadcast) { errno = EINVAL; return -1; } } else { fam = AF_INET6; } #ifdef KNET_LINUX addr = rtnl_addr_alloc(); if (!addr) { errno = ENOMEM; err = -1; goto out; } if (rtnl_link_alloc_cache(lib_cfg.nlsock, AF_UNSPEC, &cache) < 0) { errno = ENOMEM; err = -1; goto out; } ifindex = rtnl_link_name2i(cache, nozzle->name); if (ifindex == 0) { errno = ENOENT; err = -1; goto out; } rtnl_addr_set_ifindex(addr, ifindex); if (nl_addr_parse(ipaddr, fam, &local_addr) < 0) { errno = EINVAL; err = -1; goto out; } if (rtnl_addr_set_local(addr, local_addr) < 0) { errno = EINVAL; err = -1; goto out; } if (broadcast) { if (nl_addr_parse(broadcast, fam, &bcast_addr) < 0) { errno = EINVAL; err = -1; goto out; } if (rtnl_addr_set_broadcast(addr, bcast_addr) < 0) { errno = EINVAL; err = -1; goto out; } } rtnl_addr_set_prefixlen(addr, atoi(prefix)); if (command == IP_ADD) { if (rtnl_addr_add(lib_cfg.nlsock, addr, 0) < 0) { errno = EINVAL; err = -1; goto out; } } else { if (rtnl_addr_delete(lib_cfg.nlsock, addr, 0) < 0) { errno = EINVAL; err = -1; goto out; } } out: if (addr) { rtnl_addr_put(addr); } if (local_addr) { nl_addr_put(local_addr); } if (bcast_addr) { nl_addr_put(bcast_addr); } if (cache) { nl_cache_put(cache); } if (broadcast) { free(broadcast); } return err; #endif #ifdef KNET_BSD /* * TODO: port to use ioctl and such, drop shell forking here */ memset(cmdline, 0, sizeof(cmdline)); if (fam == AF_INET) { snprintf(proto, sizeof(proto), "inet"); } else { snprintf(proto, sizeof(proto), "inet6"); } if (command == IP_ADD) { snprintf(cmdline, sizeof(cmdline)-1, "ifconfig %s %s %s/%s", nozzle->name, proto, ipaddr, prefix); if (broadcast) { snprintf(cmdline + strlen(cmdline), sizeof(cmdline) - strlen(cmdline) -1, " broadcast %s", broadcast); } if ((secondary) && (fam == AF_INET)) { snprintf(cmdline + strlen(cmdline), sizeof(cmdline) - strlen(cmdline) -1, " alias"); } } else { snprintf(cmdline, sizeof(cmdline)-1, "ifconfig %s %s %s/%s delete", nozzle->name, proto, ipaddr, prefix); } if (broadcast) { free(broadcast); } /* * temporary workaround as we port libnozzle to BSD ioctl * for IP address management */ err = execute_bin_sh_command(cmdline, &error_string); if (error_string) { free(error_string); error_string = NULL; } return err; #endif } /* * Exported public API */ nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath) { int savederrno = 0; nozzle_t nozzle = NULL; char *temp_mac = NULL; #ifdef KNET_LINUX struct ifreq ifr; #endif #ifdef KNET_BSD uint16_t i; long int nozzlenum = 0; char curnozzle[IFNAMSIZ]; struct ifreq ifr; #endif if (devname == NULL) { errno = EINVAL; return NULL; } if (devname_size < IFNAMSIZ) { errno = EINVAL; return NULL; } /* Need to allow space for trailing NUL */ if (strlen(devname) >= IFNAMSIZ) { errno = E2BIG; return NULL; } #ifdef KNET_BSD /* * BSD does not support named devices like Linux * but it is possible to force a nozzleX device number * where X is 0 to 255. */ if (strlen(devname)) { if (strncmp(devname, "tap", 3)) { errno = EINVAL; return NULL; } errno = 0; nozzlenum = strtol(devname+3, NULL, 10); if (errno) { errno = EINVAL; return NULL; } if ((nozzlenum < 0) || (nozzlenum > 255)) { errno = EINVAL; return NULL; } } #endif if (updownpath) { /* only absolute paths */ if (updownpath[0] != '/') { errno = EINVAL; return NULL; } if (strlen(updownpath) >= UPDOWN_PATH_MAX) { errno = E2BIG; return NULL; } } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return NULL; } if (!lib_init) { lib_cfg.head = NULL; #ifdef KNET_LINUX lib_cfg.nlsock = nl_socket_alloc(); if (!lib_cfg.nlsock) { savederrno = errno; goto out_error; } if (nl_connect(lib_cfg.nlsock, NETLINK_ROUTE) < 0) { savederrno = EBUSY; goto out_error; } lib_cfg.ioctlfd = socket(AF_INET, SOCK_STREAM, 0); #endif #ifdef KNET_BSD lib_cfg.ioctlfd = socket(AF_LOCAL, SOCK_DGRAM, 0); #endif if (lib_cfg.ioctlfd < 0) { savederrno = errno; goto out_error; } lib_init = 1; } nozzle = malloc(sizeof(struct nozzle_iface)); if (!nozzle) { savederrno = ENOMEM; goto out_error; } memset(nozzle, 0, sizeof(struct nozzle_iface)); #ifdef KNET_BSD if (!strlen(devname)) { /* * FreeBSD 13 kernel has changed how the tap module * works and tap0 cannot be removed from the system. * This means that tap0 settings are never reset to default * and nozzle cannot control the default state of the device * when taking over. * nozzle expects some parameters to be default when opening * a tap device (such as random mac address, default MTU, no * other attributes, etc.) * * For 13 and higher, simply skip tap0 as usable device. */ #if __FreeBSD__ >= 13 for (i = 1; i < 256; i++) { #else for (i = 0; i < 256; i++) { #endif memset(&ifr, 0, sizeof(struct ifreq)); snprintf(curnozzle, sizeof(curnozzle) - 1, "tap%u", i); memmove(ifr.ifr_name, curnozzle, IFNAMSIZ); if (ioctl(lib_cfg.ioctlfd, SIOCIFCREATE2, &ifr) < 0) { continue; } snprintf(curnozzle, sizeof(curnozzle) - 1, "/dev/tap%u", i); nozzle->fd = open(curnozzle, O_RDWR); savederrno = errno; if (nozzle->fd > 0) { break; } /* For some reason we can't open that device, keep trying but don't leave debris */ (void)ioctl(lib_cfg.ioctlfd, SIOCIFDESTROY, &ifr); } snprintf(curnozzle, sizeof(curnozzle) -1 , "tap%u", i); } else { memmove(ifr.ifr_name, devname, IFNAMSIZ); if (ioctl(lib_cfg.ioctlfd, SIOCIFCREATE2, &ifr) < 0) { goto out_error; } snprintf(curnozzle, sizeof(curnozzle) - 1, "/dev/%s", devname); nozzle->fd = open(curnozzle, O_RDWR); savederrno = errno; snprintf(curnozzle, sizeof(curnozzle) - 1, "%s", devname); } if (nozzle->fd < 0) { savederrno = EBUSY; goto out_error; } memmove(devname, curnozzle, IFNAMSIZ); memmove(nozzle->name, curnozzle, IFNAMSIZ); #endif #ifdef KNET_LINUX if ((nozzle->fd = open("/dev/net/tun", O_RDWR)) < 0) { savederrno = errno; goto out_error; } memset(&ifr, 0, sizeof(struct ifreq)); memmove(ifname, devname, IFNAMSIZ); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; if (ioctl(nozzle->fd, TUNSETIFF, &ifr) < 0) { savederrno = errno; goto out_error; } if ((strlen(devname) > 0) && (strcmp(devname, ifname) != 0)) { savederrno = EBUSY; goto out_error; } memmove(devname, ifname, IFNAMSIZ); memmove(nozzle->name, ifname, IFNAMSIZ); #endif nozzle->default_mtu = get_iface_mtu(nozzle); if (nozzle->default_mtu < 0) { savederrno = errno; goto out_error; } if (get_iface_mac(nozzle, &temp_mac) < 0) { savederrno = errno; goto out_error; } strncpy(nozzle->default_mac, temp_mac, 18); free(temp_mac); if (updownpath) { int len = strlen(updownpath); strcpy(nozzle->updownpath, updownpath); if (nozzle->updownpath[len-1] != '/') { nozzle->updownpath[len] = '/'; } nozzle->hasupdown = 1; } nozzle->next = lib_cfg.head; lib_cfg.head = nozzle; pthread_mutex_unlock(&config_mutex); errno = savederrno; return nozzle; out_error: destroy_iface(nozzle); pthread_mutex_unlock(&config_mutex); errno = savederrno; return NULL; } int nozzle_close(nozzle_t nozzle) { int err = 0, savederrno = 0; nozzle_t temp = lib_cfg.head; nozzle_t prev = lib_cfg.head; struct nozzle_ip *ip, *ip_next; savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } while ((temp) && (temp != nozzle)) { prev = temp; temp = temp->next; } if (nozzle == prev) { lib_cfg.head = nozzle->next; } else { prev->next = nozzle->next; } ip = nozzle->ip; while (ip) { ip_next = ip->next; free(ip); ip = ip_next; } destroy_iface(nozzle); out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_run_updown(const nozzle_t nozzle, uint8_t action, char **exec_string) { int err = 0, savederrno = 0; char command[PATH_MAX]; const char *action_str = NULL; struct stat sb; if (action > NOZZLE_POSTDOWN) { errno = EINVAL; return -1; } if (!exec_string) { errno = EINVAL; return -1; } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } if (!nozzle->hasupdown) { savederrno = EINVAL; err = -1; goto out_clean; } switch(action) { case NOZZLE_PREUP: action_str = "pre-up.d"; break; case NOZZLE_UP: action_str = "up.d"; break; case NOZZLE_DOWN: action_str = "down.d"; break; case NOZZLE_POSTDOWN: action_str = "post-down.d"; break; } memset(command, 0, PATH_MAX); snprintf(command, PATH_MAX, "%s/%s/%s", nozzle->updownpath, action_str, nozzle->name); err = stat(command, &sb); if (err) { savederrno = errno; goto out_clean; } /* * clear errno from previous calls as there is no errno * returned from execute_bin_sh_command */ savederrno = 0; err = execute_bin_sh_command(command, exec_string); if (err) { err = -2; } out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_set_up(nozzle_t nozzle) { int err = 0, savederrno = 0; struct ifreq ifr; savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } if (nozzle->up) { goto out_clean; } memset(&ifr, 0, sizeof(struct ifreq)); memmove(ifname, nozzle->name, IFNAMSIZ); err = ioctl(lib_cfg.ioctlfd, SIOCGIFFLAGS, &ifr); if (err) { savederrno = errno; goto out_clean; } ifr.ifr_flags |= IFF_UP | IFF_RUNNING; err = ioctl(lib_cfg.ioctlfd, SIOCSIFFLAGS, &ifr); if (err) { savederrno = errno; goto out_clean; } nozzle->up = 1; out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_set_down(nozzle_t nozzle) { int err = 0, savederrno = 0; struct ifreq ifr; savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } if (!nozzle->up) { goto out_clean; } memset(&ifr, 0, sizeof(struct ifreq)); memmove(ifname, nozzle->name, IFNAMSIZ); err = ioctl(lib_cfg.ioctlfd, SIOCGIFFLAGS, &ifr); if (err) { savederrno = errno; goto out_clean; } ifr.ifr_flags &= ~IFF_UP; err = ioctl(lib_cfg.ioctlfd, SIOCSIFFLAGS, &ifr); if (err) { savederrno = errno; goto out_clean; } nozzle->up = 0; out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_get_mtu(const nozzle_t nozzle) { int err = 0, savederrno = 0; savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } err = get_iface_mtu(nozzle); savederrno = errno; out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_get_mac(const nozzle_t nozzle, char **ether_addr) { int err = 0, savederrno = 0; if (!ether_addr) { errno = EINVAL; return -1; } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } err = get_iface_mac(nozzle, ether_addr); out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_set_mac(nozzle_t nozzle, const char *ether_addr) { int err = 0, savederrno = 0; struct ifreq ifr; if (!ether_addr) { errno = EINVAL; return -1; } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } memset(&ifr, 0, sizeof(struct ifreq)); memmove(ifname, nozzle->name, IFNAMSIZ); #ifdef KNET_LINUX err = ioctl(lib_cfg.ioctlfd, SIOCGIFHWADDR, &ifr); if (err) { savederrno = errno; goto out_clean; } memmove(ifr.ifr_hwaddr.sa_data, ether_aton(ether_addr), ETH_ALEN); err = ioctl(lib_cfg.ioctlfd, SIOCSIFHWADDR, &ifr); savederrno = errno; #endif #ifdef KNET_BSD err = ioctl(lib_cfg.ioctlfd, SIOCGIFADDR, &ifr); if (err) { savederrno = errno; goto out_clean; } memmove(ifr.ifr_addr.sa_data, ether_aton(ether_addr), ETHER_ADDR_LEN); ifr.ifr_addr.sa_len = ETHER_ADDR_LEN; err = ioctl(lib_cfg.ioctlfd, SIOCSIFLLADDR, &ifr); savederrno = errno; #endif out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_reset_mac(nozzle_t nozzle) { return nozzle_set_mac(nozzle, nozzle->default_mac); } nozzle_t nozzle_get_handle_by_name(const char *devname) { int savederrno = 0; nozzle_t nozzle; if ((devname == NULL) || (strlen(devname) > IFNAMSIZ)) { errno = EINVAL; return NULL; } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return NULL; } nozzle = lib_cfg.head; while (nozzle != NULL) { if (!strcmp(devname, nozzle->name)) break; nozzle = nozzle->next; } if (!nozzle) { savederrno = ENOENT; } pthread_mutex_unlock(&config_mutex); errno = savederrno; return nozzle; } const char *nozzle_get_name_by_handle(const nozzle_t nozzle) { int savederrno = 0; char *name = NULL; savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return NULL; } if (!is_valid_nozzle(nozzle)) { savederrno = ENOENT; goto out_clean; } name = nozzle->name; out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return name; } int nozzle_get_fd(const nozzle_t nozzle) { int fd = -1, savederrno = 0; savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = ENOENT; fd = -1; goto out_clean; } fd = nozzle->fd; out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return fd; } int nozzle_set_mtu(nozzle_t nozzle, const int mtu) { int err = 0, savederrno = 0; struct nozzle_ip *tmp_ip; struct ifreq ifr; if (!mtu) { errno = EINVAL; return -1; } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } err = nozzle->current_mtu = get_iface_mtu(nozzle); if (err < 0) { savederrno = errno; goto out_clean; } memset(&ifr, 0, sizeof(struct ifreq)); memmove(ifname, nozzle->name, IFNAMSIZ); ifr.ifr_mtu = mtu; err = ioctl(lib_cfg.ioctlfd, SIOCSIFMTU, &ifr); if (err) { savederrno = errno; goto out_clean; } if ((nozzle->current_mtu < 1280) && (mtu >= 1280)) { tmp_ip = nozzle->ip; while(tmp_ip) { if (tmp_ip->domain == AF_INET6) { err = _set_ip(nozzle, IP_ADD, tmp_ip->ipaddr, tmp_ip->prefix, 0); if (err) { savederrno = errno; err = -1; goto out_clean; } } tmp_ip = tmp_ip->next; } } out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_reset_mtu(nozzle_t nozzle) { return nozzle_set_mtu(nozzle, nozzle->default_mtu); } int nozzle_add_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix) { int err = 0, savederrno = 0; int found = 0; struct nozzle_ip *ip = NULL, *ip_prev = NULL, *ip_last = NULL; int secondary = 0; if ((!ipaddr) || (!prefix)) { errno = EINVAL; return -1; } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } found = find_ip(nozzle, ipaddr, prefix, &ip, &ip_prev); if (found) { goto out_clean; } ip = malloc(sizeof(struct nozzle_ip)); if (!ip) { savederrno = errno; err = -1 ; goto out_clean; } memset(ip, 0, sizeof(struct nozzle_ip)); strncpy(ip->ipaddr, ipaddr, IPADDR_CHAR_MAX); strncpy(ip->prefix, prefix, PREFIX_CHAR_MAX); if (!strchr(ip->ipaddr, ':')) { ip->domain = AF_INET; } else { ip->domain = AF_INET6; } /* * if user asks for an IPv6 address, but MTU < 1280 * store the IP and bring it up later if and when MTU > 1280 */ if ((ip->domain == AF_INET6) && (get_iface_mtu(nozzle) < 1280)) { err = 0; } else { if (nozzle->ip) { secondary = 1; } err = _set_ip(nozzle, IP_ADD, ipaddr, prefix, secondary); savederrno = errno; } if (err) { free(ip); goto out_clean; } if (nozzle->ip) { ip_last = nozzle->ip; while (ip_last->next != NULL) { ip_last = ip_last->next; } ip_last->next = ip; } else { nozzle->ip = ip; } out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_del_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix) { int err = 0, savederrno = 0; int found = 0; struct nozzle_ip *ip = NULL, *ip_prev = NULL; if ((!ipaddr) || (!prefix)) { errno = EINVAL; return -1; } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { savederrno = EINVAL; err = -1; goto out_clean; } found = find_ip(nozzle, ipaddr, prefix, &ip, &ip_prev); if (!found) { goto out_clean; } /* * if user asks for an IPv6 address, but MTU < 1280 * the IP might not be configured on the interface and we only need to * remove it from our internal database */ if ((ip->domain == AF_INET6) && (get_iface_mtu(nozzle) < 1280)) { err = 0; } else { err = _set_ip(nozzle, IP_DEL, ipaddr, prefix, 0); savederrno = errno; } if (!err) { if (ip == ip_prev) { nozzle->ip = ip->next; } else { ip_prev->next = ip->next; } free(ip); } out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } int nozzle_get_ips(const nozzle_t nozzle, struct nozzle_ip **nozzle_ip) { int err = 0, savederrno = 0; if (!nozzle_ip) { errno = EINVAL; return -1; } savederrno = pthread_mutex_lock(&config_mutex); if (savederrno) { errno = savederrno; return -1; } if (!is_valid_nozzle(nozzle)) { err = -1; savederrno = EINVAL; goto out_clean; } *nozzle_ip = nozzle->ip; out_clean: pthread_mutex_unlock(&config_mutex); errno = savederrno; return err; } diff --git a/libnozzle/libnozzle.h b/libnozzle/libnozzle.h index 42de577b..bb892731 100644 --- a/libnozzle/libnozzle.h +++ b/libnozzle/libnozzle.h @@ -1,347 +1,347 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under LGPL-2.0+ */ #ifndef __LIBNOZZLE_H__ #define __LIBNOZZLE_H__ #include #include #include /** * * @file libnozzle.h * @brief tap interfaces management API include file - * @copyright Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * @copyright Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * nozzle is a commodity library to manage tap (ethernet) interfaces */ /** * An opaque handle returned from nozzle_open(), this should be * passed into all other nozzle API calls and closed with nozzle_close() * when finished with. */ typedef struct nozzle_iface *nozzle_t; /** * nozzle_open * * @brief create a new tap device on the system. * * devname - pointer to device name of at least size IFNAMSIZ. * if the dev strlen is 0, then the system will assign a name automatically. * if a string is specified, the system will try to create a device with * the specified name. * NOTE: on FreeBSD the tap device names can only be tapX where X is a * number from 0 to 255. On Linux such limitation does not apply. * The name must be unique to the system. If an interface with the same * name is already configured on the system, an error will be returned. * * devname_size - length of the buffer provided in dev (has to be at least IFNAMSIZ). * * updownpath - nozzle supports the typical filesystem structure to execute * actions for: down.d post-down.d pre-up.d up.d * in the form of: * updownpath/\/\ * updownpath specifies where to find those directories on the * filesystem and it must be an absolute path. * * @return * nozzle_open returns * a pointer to a nozzle struct on success * NULL on error and errno is set. */ nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath); /** * nozzle_close * * @brief deconfigure and destroy a nozzle device * * nozzle - pointer to the nozzle struct to destroy * * @return * 0 on success * -1 on error and errno is set. */ int nozzle_close(nozzle_t nozzle); #define NOZZLE_PREUP 0 #define NOZZLE_UP 1 #define NOZZLE_DOWN 2 #define NOZZLE_POSTDOWN 3 /** * nozzle_run_updown * * @brief execute updown commands associated with a nozzle device. * * nozzle - pointer to the nozzle struct * * action - pre-up.d / up.d / down.d / post-down.d (see defines above) * * exec_string - pointers to string to record executing action stdout/stderr. * The string is malloc'ed, the caller needs to free the buffer. * If the script generates no output this string might be NULL. * * It is the application responsibility to call helper scripts * before or after creating/destroying interfaces or IP addresses. * * @return * 0 on success * -1 on error and errno is set (sanity checks and internal calls. * -2 on error from executing the shell scripts, and no errno is set. */ int nozzle_run_updown(const nozzle_t nozzle, uint8_t action, char **exec_string); /** * nozzle_set_up * * @brief equivalent of ifconfig up * * nozzle - pointer to the nozzle struct * * @return * 0 on success * -1 on error and errno is set. */ int nozzle_set_up(nozzle_t nozzle); /** * nozzle_set_down * * @brief equivalent of ifconfig down * * nozzle - pointer to the nozzle struct * * @return * 0 on success * -1 on error and errno is set. */ int nozzle_set_down(nozzle_t nozzle); /** * nozzle_add_ip * * @brief equivalent of ip addr or ifconfig * * nozzle - pointer to the nozzle struct * * ipaddr - string containing either an IPv4 or an IPv6 address. * Please note that Linux will automatically remove any IPv6 addresses from an interface * with MTU < 1280. libnozzle will cache those IPs and re-instate them when MTU is > 1280. * MTU must be set via nozzle_set_mtu for IPv6 to be re-instated. * * prefix - 24, 64 or any valid network prefix for the requested address. * * @return * 0 on success * -1 on error and errno is set. */ int nozzle_add_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix); /** * nozzle_del_ip * * @brief equivalent of ip addr del or ifconfig del * * nozzle - pointer to the nozzle struct * * ipaddr - string containing either an IPv4 or an IPv6 address. * * prefix - 24, 64 or any valid network prefix for the requested address. * * @return * 0 on success * -1 on error and errno is set. */ int nozzle_del_ip(nozzle_t nozzle, const char *ipaddr, const char *prefix); #define IPADDR_CHAR_MAX 128 #define PREFIX_CHAR_MAX 4 /** * Info about an IP address on a nozzle interface * as returned from nozzle_get_ips */ struct nozzle_ip { /** The IP address */ char ipaddr[IPADDR_CHAR_MAX + 1]; /** Prefix - eg "24" */ char prefix[PREFIX_CHAR_MAX + 1]; /** AF_INET or AF_INET6 */ int domain; /** Pointer to next struct or NULL */ struct nozzle_ip *next; }; /** * nozzle_get_ips * * @brief retrieve the list of all configured ips for a given interface * * nozzle - pointer to the nozzle struct * * nozzle_ip - pointer to the head of a list of nozzle_ip structs. * The last IP will have next = NULL. * nozzle_ip can be NULL if there are no IP addresses * associated with this nozzle device. * *DO NOT* free those structs as they are used internally * for IP address tracking. * * @return * 0 on success * -1 on error and errno is set. * */ int nozzle_get_ips(const nozzle_t nozzle, struct nozzle_ip **nozzle_ip); /** * nozzle_get_mtu * * @brief retrieve mtu on a given nozzle interface * * nozzle - pointer to the nozzle struct * * @return * MTU on success * -1 on error and errno is set. */ int nozzle_get_mtu(const nozzle_t nozzle); /** * nozzle_set_mtu * * @brief set mtu on a given nozzle interface * * nozzle - pointer to the nozzle struct * * mtu - new MTU value * * @return * 0 on success * -1 on error and errno is set. */ int nozzle_set_mtu(nozzle_t nozzle, const int mtu); /** * nozzle_reset_mtu * * @brief reset mtu on a given nozzle interface to the system default * * nozzle - pointer to the nozzle struct * * @return * 0 on success * -1 on error and errno is set. */ int nozzle_reset_mtu(nozzle_t nozzle); /** * nozzle_get_mac * * @brief retrieve mac address on a given nozzle interface * * nozzle - pointer to the nozzle struct * * ether_addr - pointers to string containing the current mac address. * The string is malloc'ed, the caller needs to free this buffer. * @return * 0 on success. * -1 on error and errno is set. */ int nozzle_get_mac(const nozzle_t nozzle, char **ether_addr); /** * nozzle_set_mac * * @brief set mac address on a given nozzle interface * * nozzle - pointer to the nozzle struct * * ether_addr - pointers to string containing the new mac address. * * @return * 0 on success. * -1 on error and errno is set. */ int nozzle_set_mac(nozzle_t nozzle, const char *ether_addr); /** * nozzle_reset_mac * * @brief reset mac address on a given nozzle interface to system default * * nozzle - pointer to the nozzle struct * * @return * 0 on success. * -1 on error and errno is set. */ int nozzle_reset_mac(nozzle_t nozzle); /** * nozzle_get_handle_by_name * * @brief find a nozzle handle by device name * * devname - string containing the name of the interface * * @return * handle on success. * NULL on error and errno is set. */ nozzle_t nozzle_get_handle_by_name(const char *devname); /** * nozzle_get_name_by_handle * * @brief retrieve nozzle interface name by handle * * nozzle - pointer to the nozzle struct * * @return * pointer to the interface name * NULL on error and errno is set. */ const char *nozzle_get_name_by_handle(const nozzle_t nozzle); /** * nozzle_get_fd * * @brief * * nozzle - pointer to the nozzle struct * * @return * fd associated to a given nozzle on success. * -1 on error and errno is set. */ int nozzle_get_fd(const nozzle_t nozzle); #endif diff --git a/libnozzle/libnozzle.pc.in b/libnozzle/libnozzle.pc.in index a4fd7ab6..766d84af 100644 --- a/libnozzle/libnozzle.pc.in +++ b/libnozzle/libnozzle.pc.in @@ -1,19 +1,19 @@ # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under LGPL-2.0+ # prefix=@prefix@ exec_prefix=${prefix} libdir=@libdir@ includedir=${prefix}/include Name: libnozzle Version: @VERSION@ Description: library to manage a pool of tap devices Requires: Libs: -L${libdir} -lnozzle Cflags: -I${includedir} diff --git a/libnozzle/libnozzle_exported_syms b/libnozzle/libnozzle_exported_syms index 9ec56340..44f67618 100644 --- a/libnozzle/libnozzle_exported_syms +++ b/libnozzle/libnozzle_exported_syms @@ -1,15 +1,15 @@ # Version and symbol export for libnozzle.so # -# Copyright (C) 2011-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2011-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under LGPL-2.0+ # LIBNOZZLE { global: nozzle_*; local: *; }; diff --git a/libnozzle/tests/Makefile.am b/libnozzle/tests/Makefile.am index 2f692250..58c4aec9 100644 --- a/libnozzle/tests/Makefile.am +++ b/libnozzle/tests/Makefile.am @@ -1,130 +1,130 @@ # -# Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk EXTRA_DIST = \ nozzle_run_updown_exit_true \ nozzle_run_updown_exit_false \ api-test-coverage noinst_HEADERS = \ test-common.h if BUILD_LIBNOZZLE api_checks = \ api_nozzle_open_test \ api_nozzle_close_test \ api_nozzle_set_up_test \ api_nozzle_set_down_test \ api_nozzle_get_mtu_test \ api_nozzle_set_mtu_test \ api_nozzle_get_mac_test \ api_nozzle_set_mac_test \ api_nozzle_get_handle_by_name_test \ api_nozzle_get_name_by_handle_test \ api_nozzle_get_fd_test \ api_nozzle_run_updown_test \ api_nozzle_add_ip_test \ api_nozzle_del_ip_test \ api_nozzle_get_ips_test int_checks = \ int_execute_bin_sh_command_test fun_checks = benchmarks = check_PROGRAMS = \ $(api_checks) \ $(int_checks) \ $(fun_checks) noinst_PROGRAMS = \ $(benchmarks) \ $(check_PROGRAMS) noinst_SCRIPTS = \ api-test-coverage TESTS = $(check_PROGRAMS) if INSTALL_TESTS testsuitedir = $(TESTDIR) testsuite_PROGRAMS = $(noinst_PROGRAMS) endif check-local: check-api-test-coverage check-annocheck-bins check-api-test-coverage: chmod u+x $(top_srcdir)/libnozzle/tests/api-test-coverage $(top_srcdir)/libnozzle/tests/api-test-coverage $(top_srcdir) $(top_builddir) AM_CPPFLAGS = -I$(top_srcdir)/libnozzle -DABSBUILDDIR=\"$(abs_builddir)\" -DABSSRCDIR=\"$(abs_srcdir)\" AM_CFLAGS += $(PTHREAD_CFLAGS) $(libnl_CFLAGS) LIBS += $(top_builddir)/libnozzle/libnozzle.la $(PTHREAD_LIBS) $(libnl_LIBS) api_nozzle_open_test_SOURCES = api_nozzle_open.c \ test-common.c api_nozzle_close_test_SOURCES = api_nozzle_close.c \ test-common.c api_nozzle_set_up_test_SOURCES = api_nozzle_set_up.c \ test-common.c \ ../internals.c api_nozzle_set_down_test_SOURCES = api_nozzle_set_down.c \ test-common.c \ ../internals.c api_nozzle_get_mtu_test_SOURCES = api_nozzle_get_mtu.c \ test-common.c api_nozzle_set_mtu_test_SOURCES = api_nozzle_set_mtu.c \ test-common.c \ ../internals.c api_nozzle_get_mac_test_SOURCES = api_nozzle_get_mac.c \ test-common.c api_nozzle_set_mac_test_SOURCES = api_nozzle_set_mac.c \ test-common.c api_nozzle_get_handle_by_name_test_SOURCES = api_nozzle_get_handle_by_name.c \ test-common.c api_nozzle_get_name_by_handle_test_SOURCES = api_nozzle_get_name_by_handle.c \ test-common.c api_nozzle_get_fd_test_SOURCES = api_nozzle_get_fd.c \ test-common.c api_nozzle_run_updown_test_SOURCES = api_nozzle_run_updown.c \ test-common.c \ ../internals.c api_nozzle_add_ip_test_SOURCES = api_nozzle_add_ip.c \ test-common.c \ ../internals.c api_nozzle_del_ip_test_SOURCES = api_nozzle_del_ip.c \ test-common.c \ ../internals.c api_nozzle_get_ips_test_SOURCES = api_nozzle_get_ips.c \ test-common.c int_execute_bin_sh_command_test_SOURCES = int_execute_bin_sh_command.c \ test-common.c \ ../internals.c endif diff --git a/libnozzle/tests/api-test-coverage b/libnozzle/tests/api-test-coverage index 29a52708..9264ed81 100755 --- a/libnozzle/tests/api-test-coverage +++ b/libnozzle/tests/api-test-coverage @@ -1,162 +1,162 @@ #!/bin/sh # -# Copyright (C) 2016-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2016-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # srcdir="$1"/libnozzle/tests builddir="$2"/libnozzle/tests headerapicalls="$(grep nozzle_ "$srcdir"/../libnozzle.h | grep -v "^ \*" | grep -v ^struct | grep -v "^[[:space:]]" | grep -v typedef | sed -e 's/(.*//g' -e 's/^const //g' -e 's/\*//g' | awk '{print $2}')" # The PowerPC64 ELFv1 ABI defines the address of a function as that of a # function descriptor defined in .opd, a data (D) section. Other ABIs # use the entry address of the function itself in the text (T) section. exportedapicalls="$(nm -B -D "$builddir"/../.libs/libnozzle.so | grep ' [DT] ' | awk '{print $3}' | sed -e 's#@@LIBNOZZLE##g')" echo "Checking for exported symbols NOT available in header file" for i in $exportedapicalls; do found=0 for x in $headerapicalls; do if [ "$x" = "$i" ]; then found=1 break; fi done if [ "$found" = 0 ]; then echo "Symbol $i not found in header file" exit 1 fi done echo "Checking for symbols in header file NOT exported by binary lib" for i in $headerapicalls; do found=0 for x in $exportedapicalls; do if [ "$x" = "$i" ]; then found=1 break; fi done if [ "$found" = 0 ]; then echo "Symbol $i not found in binary lib" exit 1 fi done echo "Checking for tests with memcheck exceptions" for i in $(grep -l is_memcheck "$srcdir"/*.c | grep -v test-common); do echo "WARNING: $(basename $i) - has memcheck exception enabled" done echo "Checking for tests with helgrind exceptions" for i in $(grep -l is_helgrind "$srcdir"/*.c | grep -v test-common); do echo "WARNING: $(basename $i) has helgrind exception enabled" done echo "Checking for api test coverage" numapicalls=0 found=0 missing=0 for i in $headerapicalls; do [ "$i" = nozzle_reset_mtu ] && i=nozzle_set_mtu # tested together [ "$i" = nozzle_reset_mac ] && i=nozzle_set_mac # tested together numapicalls=$((numapicalls + 1)) if [ -f $srcdir/api_${i}.c ]; then found=$((found + 1)) else missing=$((missing + 1)) echo "MISSING: $i" fi done # Check Rust bindings coverage rust_found=0 rust_missing=0 deliberately_missing="" rustapicalls=$numapicalls for i in $headerapicalls; do rustcall=`echo $i|awk '{print substr($0, 8)}'` grep "^pub fn ${rustcall}(" $1/libnozzle/bindings/rust/src/nozzle_bindings.rs > /dev/null 2>/dev/null if [ $? = 0 ] then rust_found=$((rust_found+1)) else echo $deliberately_missing | grep $i 2>/dev/null >/dev/null if [ $? != 0 ] then echo "$i Missing from Rust API" rust_missing=$((rust_missing+1)) else rustapicalls=$((rustapicalls-1)) fi fi done # Check Rust test coverage rust_test_found=0 rust_test_missing=0 deliberately_missing="" rust_testapicalls=$numapicalls for i in $headerapicalls; do rustcall=`echo $i|awk '{print substr($0, 8)}'` grep "nozzle::${rustcall}(" $1/libnozzle/bindings/rust/tests/src/bin/nozzle-test.rs > /dev/null 2>/dev/null if [ $? = 0 ] then rust_test_found=$((rust_test_found+1)) else echo $deliberately_missing | grep $i 2>/dev/null >/dev/null if [ $? != 0 ] then echo "$i Missing from Rust test" rust_test_missing=$((rust_test_missing+1)) else rust_testapicalls=$((rust_testapicalls-1)) fi fi done echo "" echo "Summary" echo "-------" echo "Found : $found" echo "Missing : $missing" echo "Total : $numapicalls" which bc > /dev/null 2>&1 && { coverage=$(echo "scale=3; $found / $numapicalls * 100" | bc -l) echo "Coverage: $coverage%" } echo echo "Rust API Summary" echo "----------------" echo "Found : $rust_found" echo "Missing : $rust_missing" echo "Total : $rustapicalls" which bc > /dev/null 2>&1 && { coverage=$(echo "scale=3; $rust_found / $rustapicalls * 100" | bc -l) echo "Coverage: $coverage%" } echo echo "Rust test Summary" echo "-----------------" echo "Found : $rust_test_found" echo "Missing : $rust_test_missing" echo "Total : $rustapicalls" which bc > /dev/null 2>&1 && { coverage=$(echo "scale=3; $rust_test_found / $rust_testapicalls * 100" | bc -l) echo "Coverage: $coverage%" } exit 0 diff --git a/libnozzle/tests/api_nozzle_add_ip.c b/libnozzle/tests/api_nozzle_add_ip.c index c19fcbf1..1a201c43 100644 --- a/libnozzle/tests/api_nozzle_add_ip.c +++ b/libnozzle/tests/api_nozzle_add_ip.c @@ -1,294 +1,294 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "test-common.h" char testipv4_1[IPBUFSIZE]; char testipv4_2[IPBUFSIZE]; char testipv6_1[IPBUFSIZE]; char testipv6_2[IPBUFSIZE]; static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; char verifycmd[2048]; int err = 0; nozzle_t nozzle; char *error_string = NULL; printf("Testing interface add ip\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Testing error conditions\n"); printf("Testing invalid nozzle handle\n"); err = nozzle_add_ip(NULL, testipv4_1, "24"); if ((!err) || (errno != EINVAL)) { printf("nozzle_add_ip accepted invalid nozzle handle\n"); err = -1; goto out_clean; } printf("Testing empty ip address\n"); err = nozzle_add_ip(nozzle, NULL, "24"); if ((!err) || (errno != EINVAL)) { printf("nozzle_add_ip accepted invalid ip address\n"); err = -1; goto out_clean; } printf("Testing empty netmask\n"); err = nozzle_add_ip(nozzle, testipv4_1, NULL); if ((!err) || (errno != EINVAL)) { printf("nozzle_add_ip accepted invalid netmask\n"); err = -1; goto out_clean; } printf("Adding ip: %s/24\n", testipv4_1); err = nozzle_add_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } printf("Adding ip: %s/24\n", testipv4_2); err = nozzle_add_ip(nozzle, testipv4_2, "24"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } printf("Adding duplicate ip: %s/24\n", testipv4_1); err = nozzle_add_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to find IP address in libnozzle db\n"); err = -1; goto out_clean; } printf("Checking ip: %s/24\n", testipv4_1); memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/24", nozzle->name, testipv4_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv4_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Checking ip: %s/24\n", testipv4_2); memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/24", nozzle->name, testipv4_2); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv4_2); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/24\n", testipv4_1); err = nozzle_del_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/24\n", testipv4_2); err = nozzle_del_ip(nozzle, testipv4_2, "24"); if (err < 0) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } printf("Adding ip: %s/64\n", testipv6_1); err = nozzle_add_ip(nozzle, testipv6_1, "64"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/64\n", testipv6_1); err = nozzle_del_ip(nozzle, testipv6_1, "64"); if (err) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } printf("Testing adding an IPv6 address with mtu < 1280 and restore\n"); printf("Lowering interface MTU\n"); err = nozzle_set_mtu(nozzle, 1200); if (err) { printf("Unable to set MTU to 1200\n"); err = -1; goto out_clean; } printf("Adding ip: %s/64\n", testipv6_1); err = nozzle_add_ip(nozzle, testipv6_1, "64"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Resetting MTU\n"); err = nozzle_reset_mtu(nozzle); if (err) { printf("Unable to set reset MTU\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/64\n", testipv6_1); err = nozzle_del_ip(nozzle, testipv6_1, "64"); if (err) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } out_clean: nozzle_close(nozzle); return err; } int main(void) { need_root(); need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_close.c b/libnozzle/tests/api_nozzle_close.c index 82dc83b6..276473bf 100644 --- a/libnozzle/tests/api_nozzle_close.c +++ b/libnozzle/tests/api_nozzle_close.c @@ -1,131 +1,131 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #ifdef KNET_LINUX #include #include #endif #ifdef KNET_BSD #include #endif #include "test-common.h" char testipv4_1[IPBUFSIZE]; char testipv4_2[IPBUFSIZE]; char testipv6_1[IPBUFSIZE]; char testipv6_2[IPBUFSIZE]; static int test(void) { char device_name[2*IFNAMSIZ]; size_t size = IFNAMSIZ; nozzle_t nozzle; memset(device_name, 0, sizeof(device_name)); /* * this test is duplicated from api_nozzle_open.c */ printf("Testing random nozzle interface:\n"); if (test_iface(device_name, size, NULL) < 0) { printf("Unable to create random interface\n"); return -1; } printf("Testing ERROR conditions\n"); printf("Testing nozzle_close with NULL nozzle\n"); if ((nozzle_close(NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_close sanity checks\n"); return -1; } printf("Testing nozzle_close with random bytes nozzle pointer\n"); nozzle = (nozzle_t)0x1; if ((nozzle_close(nozzle) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_close sanity checks\n"); return -1; } return 0; } /* * requires running the test suite with valgrind */ static int check_nozzle_close_leak(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; printf("Testing close leak (needs valgrind)\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Adding ip: %s/24\n", testipv4_1); err = nozzle_add_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to assign IP address\n"); err=-1; goto out_clean; } printf("Adding ip: %s/24\n", testipv4_2); err = nozzle_add_ip(nozzle, testipv4_2, "24"); if (err < 0) { printf("Unable to assign IP address\n"); err=-1; goto out_clean; } out_clean: nozzle_close(nozzle); return err; } int main(void) { need_root(); need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; if (check_nozzle_close_leak() < 0) return FAIL; return 0; } diff --git a/libnozzle/tests/api_nozzle_del_ip.c b/libnozzle/tests/api_nozzle_del_ip.c index dfd494d8..9fb6ffd1 100644 --- a/libnozzle/tests/api_nozzle_del_ip.c +++ b/libnozzle/tests/api_nozzle_del_ip.c @@ -1,267 +1,267 @@ /* - * Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "test-common.h" char testipv4_1[IPBUFSIZE]; char testipv4_2[IPBUFSIZE]; char testipv6_1[IPBUFSIZE]; char testipv6_2[IPBUFSIZE]; static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; char verifycmd[2048]; int err = 0; nozzle_t nozzle; char *error_string = NULL; printf("Testing interface del ip\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Testing error conditions\n"); printf("Testing invalid nozzle handle\n"); err = nozzle_del_ip(NULL, testipv4_1, "24"); if ((!err) || (errno != EINVAL)) { printf("nozzle_del_ip accepted invalid nozzle handle\n"); err = -1; goto out_clean; } printf("Testing empty ip address\n"); err = nozzle_del_ip(nozzle, NULL, "24"); if ((!err) || (errno != EINVAL)) { printf("nozzle_del_ip accepted invalid ip address\n"); err = -1; goto out_clean; } printf("Testing empty netmask\n"); err = nozzle_del_ip(nozzle, testipv4_1, NULL); if ((!err) || (errno != EINVAL)) { printf("nozzle_del_ip accepted invalid netmask\n"); err = -1; goto out_clean; } printf("Adding ip: %s/24\n", testipv4_1); err = nozzle_add_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } printf("Checking ip: %s/24\n", testipv4_1); memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/24", nozzle->name, testipv4_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv4_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/24\n", testipv4_1); err = nozzle_del_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } printf("Checking ip: %s/24\n", testipv4_1); memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/24", nozzle->name, testipv4_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv4_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/24 again\n", testipv4_1); err = nozzle_del_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } printf("Adding ip: %s/64\n", testipv6_1); err = nozzle_add_ip(nozzle, testipv6_1, "64"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/64\n", testipv6_1); err = nozzle_del_ip(nozzle, testipv6_1, "64"); if (err) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Testing deleting an IPv6 address with mtu < 1280 (in db, not on interface)\n"); printf("Lowering interface MTU\n"); err = nozzle_set_mtu(nozzle, 1200); if (err) { printf("Unable to set MTU to 1200\n"); err = -1; goto out_clean; } printf("Adding ip: %s/64\n", testipv6_1); err = nozzle_add_ip(nozzle, testipv6_1, "64"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Unable to verify IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/64 with low mtu\n", testipv6_1); err = nozzle_del_ip(nozzle, testipv6_1, "64"); if (err) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } out_clean: nozzle_close(nozzle); return err; } int main(void) { need_root(); need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_fd.c b/libnozzle/tests/api_nozzle_get_fd.c index 2755a4a4..75e7c52c 100644 --- a/libnozzle/tests/api_nozzle_get_fd.c +++ b/libnozzle/tests/api_nozzle_get_fd.c @@ -1,78 +1,78 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; int fd; printf("Testing get fd\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } fd = nozzle_get_fd(nozzle); if (fd < 0) { printf("Unable to get fd\n"); err = -1; goto out_clean; } if (fcntl(fd, F_GETFD) < 0) { printf("Unable to get valid fd\n"); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Passing empty struct to get_fd\n"); if (nozzle_get_fd(NULL) > 0) { printf("Something is wrong in nozzle_get_fd sanity checks\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_handle_by_name.c b/libnozzle/tests/api_nozzle_get_handle_by_name.c index 743e3fa6..4624660b 100644 --- a/libnozzle/tests/api_nozzle_get_handle_by_name.c +++ b/libnozzle/tests/api_nozzle_get_handle_by_name.c @@ -1,86 +1,86 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include "test-common.h" static int test(void) { char device_name[2*IFNAMSIZ]; size_t size = IFNAMSIZ; nozzle_t nozzle, nozzle_tmp; int err = 0; printf("Testing get handle by name\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } nozzle_tmp = nozzle_get_handle_by_name(device_name); if ((!nozzle_tmp) && (errno != ENOENT)) { printf("Unable to get handle by name\n"); err = -1; goto out_clean; } if (nozzle != nozzle_tmp) { printf("get handle by name returned wrong handle!\n"); err = -1; goto out_clean; } printf("Testing error conditions\n"); printf("Testing with NULL device name\n"); nozzle_tmp = nozzle_get_handle_by_name(NULL); if ((nozzle_tmp) || (errno != EINVAL)) { printf("get handle by name returned wrong error\n"); err = -1; goto out_clean; } printf("Testing with device name longer than IFNAMSIZ\n"); nozzle_tmp = nozzle_get_handle_by_name("antanisupercazzolaunpotapioca"); if ((nozzle_tmp) || (errno != EINVAL)) { printf("get handle by name returned wrong error\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_ips.c b/libnozzle/tests/api_nozzle_get_ips.c index c4b4ff64..3b9d0755 100644 --- a/libnozzle/tests/api_nozzle_get_ips.c +++ b/libnozzle/tests/api_nozzle_get_ips.c @@ -1,183 +1,183 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "test-common.h" char testipv4_1[IPBUFSIZE]; char testipv4_2[IPBUFSIZE]; char testipv6_1[IPBUFSIZE]; char testipv6_2[IPBUFSIZE]; static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err = 0; nozzle_t nozzle; struct nozzle_ip *ip_list = NULL, *ip_list_tmp = NULL; int ip_list_entries = 0, ipv4_list_entries = 0, ipv6_list_entries = 0; printf("Testing get ips\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Testing error conditions\n"); printf("Testing invalid nozzle\n"); err = nozzle_get_ips(NULL, &ip_list); if ((!err) || (errno != EINVAL)) { printf("nozzle_get_ips accepted invalid nozzle\n"); err = -1; goto out_clean; } printf("Testing invalid ip list\n"); err = nozzle_get_ips(nozzle, NULL); if ((!err) || (errno != EINVAL)) { printf("nozzle_get_ips accepted invalid ip list\n"); err = -1; goto out_clean; } printf("Adding ip: %s/24\n", testipv4_1); err = nozzle_add_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } printf("Adding ip: %s/24\n", testipv4_2); err = nozzle_add_ip(nozzle, testipv4_2, "24"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } printf("Adding ip: %s/64\n", testipv6_1); err = nozzle_add_ip(nozzle, testipv6_1, "64"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } printf("Adding ip: %s/64\n", testipv6_2); err = nozzle_add_ip(nozzle, testipv6_2, "64"); if (err < 0) { printf("Unable to assign IP address\n"); err = -1; goto out_clean; } printf("Get ip list from libnozzle:\n"); if (nozzle_get_ips(nozzle, &ip_list) < 0) { printf("Not enough mem?\n"); err = -1; goto out_clean; } ip_list_tmp = ip_list; ip_list_entries = 0; while(ip_list_tmp) { ip_list_entries++; if (ip_list_tmp->domain == AF_INET) { ipv4_list_entries++; } else { ipv6_list_entries++; } printf("Found IP %s %s in libnozzle db\n", ip_list_tmp->ipaddr, ip_list_tmp->prefix); ip_list_tmp = ip_list_tmp->next; } if ((ip_list_entries != 4) || (ipv4_list_entries != 2) || (ipv6_list_entries != 2)) { printf("Didn't get enough ip back from libnozzle?\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/24\n", testipv4_1); err = nozzle_del_ip(nozzle, testipv4_1, "24"); if (err < 0) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/24\n", testipv4_2); err = nozzle_del_ip(nozzle, testipv4_2, "24"); if (err < 0) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/64\n", testipv6_1); err = nozzle_del_ip(nozzle, testipv6_1, "64"); if (err) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } printf("Deleting ip: %s/64\n", testipv6_2); err = nozzle_del_ip(nozzle, testipv6_2, "64"); if (err) { printf("Unable to delete IP address\n"); err = -1; goto out_clean; } out_clean: nozzle_close(nozzle); return err; } int main(void) { need_root(); need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_mac.c b/libnozzle/tests/api_nozzle_get_mac.c index e3f66b2a..1dd55ea6 100644 --- a/libnozzle/tests/api_nozzle_get_mac.c +++ b/libnozzle/tests/api_nozzle_get_mac.c @@ -1,131 +1,131 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #ifdef KNET_LINUX #include #include #endif #ifdef KNET_BSD #include #endif #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; char *current_mac = NULL, *temp_mac = NULL, *err_mac = NULL; struct ether_addr *cur_mac, *tmp_mac; printf("Testing get MAC\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Get current MAC\n"); if (nozzle_get_mac(nozzle, ¤t_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } printf("Current MAC: %s\n", current_mac); printf("Setting MAC: 00:01:01:01:01:01\n"); if (nozzle_set_mac(nozzle, "00:01:01:01:01:01") < 0) { printf("Unable to set current MAC address.\n"); err = -1; goto out_clean; } if (nozzle_get_mac(nozzle, &temp_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } printf("Current MAC: %s\n", temp_mac); cur_mac = ether_aton(current_mac); tmp_mac = ether_aton(temp_mac); printf("Comparing MAC addresses\n"); if (memcmp(cur_mac, tmp_mac, sizeof(struct ether_addr))) { printf("Mac addresses are not the same?!\n"); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Pass NULL to get_mac (pass1)\n"); errno = 0; if ((nozzle_get_mac(NULL, &err_mac) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_get_mac sanity checks\n"); err = -1; goto out_clean; } printf("Pass NULL to get_mac (pass2)\n"); errno = 0; if ((nozzle_get_mac(nozzle, NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_get_mac sanity checks\n"); err = -1; goto out_clean; } out_clean: if (err_mac) { printf("Something managed to set err_mac!\n"); err = -1; free(err_mac); } if (current_mac) free(current_mac); if (temp_mac) free(temp_mac); if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_mtu.c b/libnozzle/tests/api_nozzle_get_mtu.c index 288eeb6e..90b7119c 100644 --- a/libnozzle/tests/api_nozzle_get_mtu.c +++ b/libnozzle/tests/api_nozzle_get_mtu.c @@ -1,99 +1,99 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; int current_mtu = 0; int expected_mtu = 1500; printf("Testing get MTU\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Comparing default MTU\n"); current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected default [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Setting MTU to 9000\n"); expected_mtu = 9000; if (nozzle_set_mtu(nozzle, expected_mtu) < 0) { printf("Unable to set MTU to %d\n", expected_mtu); err = -1; goto out_clean; } current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected value [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Passing empty struct to get_mtu\n"); if (nozzle_get_mtu(NULL) > 0) { printf("Something is wrong in nozzle_get_mtu sanity checks\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_get_name_by_handle.c b/libnozzle/tests/api_nozzle_get_name_by_handle.c index 724f1f1a..a2965516 100644 --- a/libnozzle/tests/api_nozzle_get_name_by_handle.c +++ b/libnozzle/tests/api_nozzle_get_name_by_handle.c @@ -1,79 +1,79 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include "test-common.h" static int test(void) { char device_name[2*IFNAMSIZ]; const char *device_name_tmp; size_t size = IFNAMSIZ; nozzle_t nozzle; int err = 0; printf("Testing get name by handle\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } device_name_tmp = nozzle_get_name_by_handle(nozzle); if (!device_name_tmp) { if (errno != ENOENT) { printf("Unable to get name by handle\n"); } else { printf("received incorrect errno!\n"); } err = -1; goto out_clean; } if (strcmp(device_name, device_name_tmp)) { printf("get name by handle returned different names for the same handle\n"); err = -1; goto out_clean; } printf("Testing error conditions\n"); device_name_tmp = nozzle_get_name_by_handle(NULL); if ((device_name_tmp) || (errno != ENOENT)) { printf("get name by handle returned wrong error\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_open.c b/libnozzle/tests/api_nozzle_open.c index 631e8d47..78df2dce 100644 --- a/libnozzle/tests/api_nozzle_open.c +++ b/libnozzle/tests/api_nozzle_open.c @@ -1,203 +1,203 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include "test-common.h" static int test_multi_eth(void) { char device_name1[IFNAMSIZ]; char device_name2[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle1 = NULL; nozzle_t nozzle2 = NULL; printf("Testing multiple nozzle interface instances\n"); memset(device_name1, 0, size); memset(device_name2, 0, size); nozzle1 = nozzle_open(device_name1, size, NULL); if (!nozzle1) { printf("Unable to init %s\n", device_name1); err = -1; goto out_clean; } if (is_if_in_system(device_name1) > 0) { printf("Found interface %s on the system\n", device_name1); } else { printf("Unable to find interface %s on the system\n", device_name1); } nozzle2 = nozzle_open(device_name2, size, NULL); if (!nozzle2) { printf("Unable to init %s\n", device_name2); err = -1; goto out_clean; } if (is_if_in_system(device_name2) > 0) { printf("Found interface %s on the system\n", device_name2); } else { printf("Unable to find interface %s on the system\n", device_name2); } if (nozzle1) { nozzle_close(nozzle1); } if (nozzle2) { nozzle_close(nozzle2); } printf("Testing error conditions\n"); printf("Open same device twice\n"); memset(device_name1, 0, size); nozzle1 = nozzle_open(device_name1, size, NULL); if (!nozzle1) { printf("Unable to init %s\n", device_name1); err = -1; goto out_clean; } if (is_if_in_system(device_name1) > 0) { printf("Found interface %s on the system\n", device_name1); } else { printf("Unable to find interface %s on the system\n", device_name1); } nozzle2 = nozzle_open(device_name1, size, NULL); if (nozzle2) { printf("We were able to init 2 interfaces with the same name!\n"); err = -1; goto out_clean; } out_clean: if (nozzle1) { nozzle_close(nozzle1); } if (nozzle2) { nozzle_close(nozzle2); } return err; } static int test(void) { char device_name[2*IFNAMSIZ]; char fakepath[PATH_MAX]; size_t size = IFNAMSIZ; uint8_t randombyte = get_random_byte(); memset(device_name, 0, sizeof(device_name)); printf("Creating random nozzle interface:\n"); if (test_iface(device_name, size, NULL) < 0) { printf("Unable to create random interface\n"); return -1; } #ifdef KNET_LINUX printf("Creating kronostest%u nozzle interface:\n", randombyte); snprintf(device_name, IFNAMSIZ, "kronostest%u", randombyte); if (test_iface(device_name, size, NULL) < 0) { printf("Unable to create kronostest%u interface\n", randombyte); return -1; } #endif #ifdef KNET_BSD printf("Creating tap%u nozzle interface:\n", randombyte); snprintf(device_name, IFNAMSIZ, "tap%u", randombyte); if (test_iface(device_name, size, NULL) < 0) { printf("Unable to create tap%u interface\n", randombyte); return -1; } printf("Creating kronostest%u nozzle interface:\n", randombyte); snprintf(device_name, IFNAMSIZ, "kronostest%u", randombyte); if (test_iface(device_name, size, NULL) == 0) { printf("BSD should not accept kronostest%u interface\n", randombyte); return -1; } #endif printf("Testing ERROR conditions\n"); printf("Testing dev == NULL\n"); errno=0; if ((test_iface(NULL, size, NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } printf("Testing size < IFNAMSIZ\n"); errno=0; if ((test_iface(device_name, 1, NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } printf("Testing device_name size > IFNAMSIZ\n"); errno=0; strcpy(device_name, "abcdefghilmnopqrstuvwz"); if ((test_iface(device_name, IFNAMSIZ, NULL) >= 0) || (errno != E2BIG)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } printf("Testing updown path != abs\n"); errno=0; memset(device_name, 0, IFNAMSIZ); if ((test_iface(device_name, IFNAMSIZ, "foo") >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } memset(fakepath, 0, PATH_MAX); memset(fakepath, '/', PATH_MAX - 2); printf("Testing updown path > PATH_MAX\n"); errno=0; memset(device_name, 0, IFNAMSIZ); if ((test_iface(device_name, IFNAMSIZ, fakepath) >= 0) || (errno != E2BIG)) { printf("Something is wrong in nozzle_open sanity checks\n"); return -1; } return 0; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; if (test_multi_eth() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_run_updown.c b/libnozzle/tests/api_nozzle_run_updown.c index 985fe044..e5cabc7c 100644 --- a/libnozzle/tests/api_nozzle_run_updown.c +++ b/libnozzle/tests/api_nozzle_run_updown.c @@ -1,406 +1,406 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle = NULL; char *error_string = NULL; char *tmpdir = NULL; char tmpdirsrc[PATH_MAX*2]; char tmpstr[PATH_MAX*2]; char srcfile[PATH_MAX]; char dstfile[PATH_MAX]; char current_dir[PATH_MAX]; /* * create a tmp dir for storing up/down scripts. * we cannot create symlinks src dir */ if (getcwd(current_dir, sizeof(current_dir)) == NULL) { printf("Unable to get current working directory: %s\n", strerror(errno)); return -1; } snprintf(tmpdirsrc, sizeof(tmpdirsrc)-1, "%s/nozzle_test_XXXXXX", current_dir); tmpdir = mkdtemp(tmpdirsrc); if (!tmpdir) { printf("Unable to create temporary directory %s for testing: %s\n", tmpdirsrc, strerror(errno)); return -1; } printf("Created temporary test dir: %s\n", tmpdir); printf("Populating test dir...\n"); snprintf(tmpstr, sizeof(tmpstr) - 1, "%s/pre-up.d", tmpdir); if (mkdir(tmpstr, 0700) < 0) { printf("Unable to create %s/pre-up.d: %s", tmpdir, strerror(errno)); err = -1; goto out_clean; } snprintf(tmpstr, sizeof(tmpstr) - 1, "%s/up.d", tmpdir); if (mkdir(tmpstr, 0700) < 0) { printf("Unable to create %s/up.d: %s", tmpdir, strerror(errno)); err = -1; goto out_clean; } snprintf(tmpstr, sizeof(tmpstr) - 1, "%s/down.d", tmpdir); if (mkdir(tmpstr, 0700) < 0) { printf("Unable to create %s/down.d: %s", tmpdir, strerror(errno)); err = -1; goto out_clean; } snprintf(tmpstr, sizeof(tmpstr) - 1, "%s/post-down.d", tmpdir); if (mkdir(tmpstr, 0700) < 0) { printf("Unable to create %s/post-down.d: %s", tmpdir, strerror(errno)); err = -1; goto out_clean; } printf("Testing error conditions\n"); printf("Init nozzle device with no path\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); err = -1; goto out_clean; } err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN, &error_string); if ((!err) || (errno != EINVAL)) { printf("nozzle_run_updown sanity check failed\n"); err = -1; goto out_clean; } nozzle_close(nozzle); printf("Init nozzle device with path\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, tmpdir); if (!nozzle) { printf("Unable to init %s\n", device_name); err = -1; goto out_clean; } printf("Testing invalid nozzle handle\n"); err = nozzle_run_updown(NULL, NOZZLE_POSTDOWN, &error_string); if ((!err) || (errno != EINVAL)) { printf("nozzle_run_updown sanity check failed\n"); err = -1; goto out_clean; } printf("Testing invalid action\n"); err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN + 1, &error_string); if ((!err) || (errno != EINVAL)) { printf("nozzle_run_updown sanity check failed\n"); err = -1; goto out_clean; } printf("Testing invalid error string\n"); err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN + 1, NULL); if ((!err) || (errno != EINVAL)) { printf("nozzle_run_updown sanity check failed\n"); err = -1; goto out_clean; } printf("Testing interface pre-up/up/down/post-down (no scripts installed)\n"); err = nozzle_run_updown(nozzle, NOZZLE_PREUP, &error_string); if (!err) { printf("nozzle_run_updown failed to detect lack of script in pre-up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_UP, &error_string); if (!err) { printf("nozzle_run_updown failed to detect lack of script in up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_DOWN, &error_string); if (!err) { printf("nozzle_run_updown failed to detect lack of script in down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN, &error_string); if (!err) { printf("nozzle_run_updown failed to detect lack of script in post-down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } printf("Populating test dir with fail scripts\n"); snprintf(srcfile, sizeof(srcfile) - 1, "%s/nozzle_run_updown_exit_false", ABSSRCDIR); snprintf(dstfile, sizeof(dstfile) - 1, "%s/pre-up.d/%s", tmpdir, device_name); if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/up.d/%s", tmpdir, device_name); if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/down.d/%s", tmpdir, device_name); if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/post-down.d/%s", tmpdir, device_name); if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } printf("Testing interface pre-up/up/down/post-down (FAIL scripts installed)\n"); err = nozzle_run_updown(nozzle, NOZZLE_PREUP, &error_string); if (err != -2) { printf("nozzle_run_updown failed to detect script failure in pre-up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_UP, &error_string); if (err != -2) { printf("nozzle_run_updown failed to detect script failure in up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_DOWN, &error_string); if (err != -2) { printf("nozzle_run_updown failed to detect script failure in down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN, &error_string); if (err != -2) { printf("nozzle_run_updown failed to detect script failure in post-down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } printf("Populating test dir with true scripts\n"); snprintf(srcfile, sizeof(srcfile) - 1, "%s/nozzle_run_updown_exit_true", ABSSRCDIR); snprintf(dstfile, sizeof(dstfile) - 1, "%s/pre-up.d/%s", tmpdir, device_name); if (unlink(dstfile) < 0) { printf("unable to remove old symlink\n"); err = -1; goto out_clean; } if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/up.d/%s", tmpdir, device_name); if (unlink(dstfile) < 0) { printf("unable to remove old symlink\n"); err = -1; goto out_clean; } if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/down.d/%s", tmpdir, device_name); if (unlink(dstfile) < 0) { printf("unable to remove old symlink\n"); err = -1; goto out_clean; } if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } snprintf(dstfile, sizeof(dstfile) - 1, "%s/post-down.d/%s", tmpdir, device_name); if (unlink(dstfile) < 0) { printf("unable to remove old symlink\n"); err = -1; goto out_clean; } if (link(srcfile, dstfile) < 0) { printf("unable to create symlink\n"); err = -1; goto out_clean; } printf("Testing interface pre-up/up/down/post-down (TRUE scripts installed)\n"); err = nozzle_run_updown(nozzle, NOZZLE_PREUP, &error_string); if (err) { printf("nozzle_run_updown failed to execute true script in pre-up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_UP, &error_string); if (err) { printf("nozzle_run_updown failed to execute true script in up.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_DOWN, &error_string); if (err) { printf("nozzle_run_updown failed to execite true script in down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } err = nozzle_run_updown(nozzle, NOZZLE_POSTDOWN, &error_string); if (err) { printf("nozzle_run_updown failed to execute true script in post-down.d\n"); err = -1; goto out_clean; } else { if (error_string) { free(error_string); error_string = NULL; } } out_clean: if (tmpdir) { snprintf(tmpstr, sizeof(tmpstr) - 1, "rm -rf %s", tmpdir); printf("Removing temporary dir: %s\n", tmpstr); if (execute_bin_sh_command(tmpstr, &error_string)) { printf("Error removing directory: %s\n", error_string); } if (error_string) { free(error_string); } } if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_set_down.c b/libnozzle/tests/api_nozzle_set_down.c index 93d91fc3..f58200f3 100644 --- a/libnozzle/tests/api_nozzle_set_down.c +++ b/libnozzle/tests/api_nozzle_set_down.c @@ -1,127 +1,127 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { char verifycmd[1024]; char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; char *error_string = NULL; printf("Testing interface down\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Put the interface up\n"); err = nozzle_set_up(nozzle); if (err < 0) { printf("Unable to set interface up\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q UP", nozzle->name); #endif #ifdef KNET_BSD "ifconfig %s | grep -q UP", nozzle->name); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err < 0) { printf("Unable to verify inteface UP\n"); err = -1; goto out_clean; } printf("Put the interface down\n"); err = nozzle_set_down(nozzle); if (err < 0) { printf("Unable to put the interface down\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q UP", nozzle->name); #endif #ifdef KNET_BSD "ifconfig %s | grep -q UP", nozzle->name); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Unable to verify inteface DOWN\n"); err = -1; goto out_clean; } printf("Try to DOWN the same interface twice\n"); if (nozzle_set_down(nozzle) < 0) { printf("Interface was already DOWN, spurious error received from nozzle_set_down\n"); err = -1; goto out_clean; } printf("Pass NULL to nozzle set_down\n"); errno = 0; if ((nozzle_set_down(NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_set_down sanity checks\n"); err = -1; goto out_clean; } out_clean: nozzle_close(nozzle); return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_set_mac.c b/libnozzle/tests/api_nozzle_set_mac.c index f9da1d6f..f8904da2 100644 --- a/libnozzle/tests/api_nozzle_set_mac.c +++ b/libnozzle/tests/api_nozzle_set_mac.c @@ -1,159 +1,159 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #ifdef KNET_LINUX #include #include #endif #ifdef KNET_BSD #include #endif #include "test-common.h" static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; char *original_mac = NULL, *current_mac = NULL, *temp_mac = NULL; struct ether_addr *orig_mac, *cur_mac, *tmp_mac; printf("Testing set MAC\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Get current MAC\n"); if (nozzle_get_mac(nozzle, &original_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } orig_mac = ether_aton(original_mac); if (nozzle_get_mac(nozzle, ¤t_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } printf("Current MAC: %s\n", current_mac); printf("Setting MAC: 00:01:01:01:01:01\n"); if (nozzle_set_mac(nozzle, "00:01:01:01:01:01") < 0) { printf("Unable to set current MAC address.\n"); err = -1; goto out_clean; } if (nozzle_get_mac(nozzle, &temp_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } printf("Current MAC: %s\n", temp_mac); cur_mac = ether_aton(current_mac); tmp_mac = ether_aton(temp_mac); printf("Comparing MAC addresses\n"); if (memcmp(cur_mac, tmp_mac, sizeof(struct ether_addr))) { printf("Mac addresses are not the same?!\n"); err = -1; goto out_clean; } printf("Testing reset_mac\n"); if (nozzle_reset_mac(nozzle) < 0) { printf("Unable to reset mac address\n"); err = -1; goto out_clean; } if (current_mac) { free(current_mac); current_mac = NULL; } if (nozzle_get_mac(nozzle, ¤t_mac) < 0) { printf("Unable to get current MAC address.\n"); err = -1; goto out_clean; } cur_mac = ether_aton(current_mac); if (memcmp(cur_mac, orig_mac, sizeof(struct ether_addr))) { printf("Mac addresses are not the same?!\n"); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Pass NULL to set_mac (pass1)\n"); errno = 0; if ((nozzle_set_mac(nozzle, NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_set_mac sanity checks\n"); err = -1; goto out_clean; } printf("Pass NULL to set_mac (pass2)\n"); errno = 0; if ((nozzle_set_mac(NULL, current_mac) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_set_mac sanity checks\n"); err = -1; goto out_clean; } out_clean: if (current_mac) free(current_mac); if (temp_mac) free(temp_mac); if (original_mac) free(original_mac); if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_set_mtu.c b/libnozzle/tests/api_nozzle_set_mtu.c index 60da1416..5101ecca 100644 --- a/libnozzle/tests/api_nozzle_set_mtu.c +++ b/libnozzle/tests/api_nozzle_set_mtu.c @@ -1,298 +1,298 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "test-common.h" char testipv4_1[IPBUFSIZE]; char testipv4_2[IPBUFSIZE]; char testipv6_1[IPBUFSIZE]; char testipv6_2[IPBUFSIZE]; static int test(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; int current_mtu = 0; int expected_mtu = 1500; printf("Testing set MTU\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Comparing default MTU\n"); current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected default [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Setting MTU to 9000\n"); expected_mtu = 9000; if (nozzle_set_mtu(nozzle, expected_mtu) < 0) { printf("Unable to set MTU to %d\n", expected_mtu); err = -1; goto out_clean; } current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected value [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Restoring MTU to default\n"); expected_mtu = 1500; if (nozzle_reset_mtu(nozzle) < 0) { printf("Unable to reset mtu\n"); err = -1; goto out_clean; } current_mtu = nozzle_get_mtu(nozzle); if (current_mtu < 0) { printf("Unable to get MTU\n"); err = -1; goto out_clean; } if (current_mtu != expected_mtu) { printf("current mtu [%d] does not match expected value [%d]\n", current_mtu, expected_mtu); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("Passing empty struct to set_mtu\n"); if (nozzle_set_mtu(NULL, 1500) == 0) { printf("Something is wrong in nozzle_set_mtu sanity checks\n"); err = -1; goto out_clean; } printf("Passing 0 mtu to set_mtu\n"); if (nozzle_set_mtu(nozzle, 0) == 0) { printf("Something is wrong in nozzle_set_mtu sanity checks\n"); err = -1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } static int test_ipv6(void) { char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; char verifycmd[2048]; int err=0; nozzle_t nozzle; char *error_string = NULL; int current_mtu = 0; printf("Testing get/set MTU with IPv6 address\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Adding ip: %s/64\n", testipv6_1); err = nozzle_add_ip(nozzle, testipv6_1, "64"); if (err) { printf("Unable to assign IP address\n"); err=-1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err=-1; goto out_clean; } printf("Setting MTU to 1200\n"); if (nozzle_set_mtu(nozzle, 1200) < 0) { printf("Unable to set MTU to 1200\n"); err = -1; goto out_clean; } err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } #ifdef KNET_LINUX if (!err) { #endif #ifdef KNET_BSD if (err) { #endif printf("Unable to verify IP address\n"); err=-1; goto out_clean; } printf("Adding ip: %s/64\n", testipv6_2); err = nozzle_add_ip(nozzle, testipv6_2, "64"); if (err < 0) { printf("Unable to assign IP address\n"); err=-1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_2); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_2); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Unable to verify IP address\n"); err=-1; goto out_clean; } printf("Restoring MTU to default\n"); if (nozzle_reset_mtu(nozzle) < 0) { printf("Unable to reset mtu\n"); err = -1; goto out_clean; } current_mtu = nozzle_get_mtu(nozzle); if (current_mtu != 1500) { printf("current mtu [%d] does not match expected value [1500]\n", current_mtu); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_1); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_1); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err=-1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q %s/64", nozzle->name, testipv6_2); #endif #ifdef KNET_BSD "ifconfig %s | grep -q %s", nozzle->name, testipv6_2); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to verify IP address\n"); err=-1; goto out_clean; } out_clean: if (nozzle) { nozzle_close(nozzle); } return err; } int main(void) { need_root(); need_tun(); make_local_ips(testipv4_1, testipv4_2, testipv6_1, testipv6_2); if (test() < 0) return FAIL; if (test_ipv6() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/api_nozzle_set_up.c b/libnozzle/tests/api_nozzle_set_up.c index f500c194..6d1d701c 100644 --- a/libnozzle/tests/api_nozzle_set_up.c +++ b/libnozzle/tests/api_nozzle_set_up.c @@ -1,101 +1,101 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { char verifycmd[1024]; char device_name[IFNAMSIZ]; size_t size = IFNAMSIZ; int err=0; nozzle_t nozzle; char *error_string = NULL; printf("Testing interface up/down\n"); memset(device_name, 0, size); nozzle = nozzle_open(device_name, size, NULL); if (!nozzle) { printf("Unable to init %s\n", device_name); return -1; } printf("Put the interface up\n"); if (nozzle_set_up(nozzle) < 0) { printf("Unable to set interface up\n"); err = -1; goto out_clean; } memset(verifycmd, 0, sizeof(verifycmd)); snprintf(verifycmd, sizeof(verifycmd)-1, #ifdef KNET_LINUX "ip addr show dev %s | grep -q UP", nozzle->name); #endif #ifdef KNET_BSD "ifconfig %s | grep -q UP", nozzle->name); #endif err = execute_bin_sh_command(verifycmd, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err < 0) { printf("Unable to verify inteface UP\n"); err = -1; goto out_clean; } printf("Test ERROR conditions\n"); printf("Try to UP the same interface twice\n"); if (nozzle_set_up(nozzle) < 0) { printf("Interface was already UP, spurious error received from nozzle_set_up\n"); err = -1; goto out_clean; } printf("Pass NULL to nozzle set_up\n"); errno = 0; if ((nozzle_set_up(NULL) >= 0) || (errno != EINVAL)) { printf("Something is wrong in nozzle_set_up sanity checks\n"); err = -1; goto out_clean; } out_clean: nozzle_set_down(nozzle); nozzle_close(nozzle); return err; } int main(void) { need_root(); need_tun(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/int_execute_bin_sh_command.c b/libnozzle/tests/int_execute_bin_sh_command.c index d7bbab70..031b53cb 100644 --- a/libnozzle/tests/int_execute_bin_sh_command.c +++ b/libnozzle/tests/int_execute_bin_sh_command.c @@ -1,121 +1,121 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include "test-common.h" static int test(void) { int err = 0; char *error_string = NULL; printf("Testing execute_bin_sh_command\n"); printf("command true\n"); err = execute_bin_sh_command("true", &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (err) { printf("Unable to execute true ?!?!\n"); goto out_clean; } printf("command false\n"); err = execute_bin_sh_command("false", &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Can we really execute false successfully?!?!\n"); err = -1; goto out_clean; } printf("command that outputs to stdout (enforcing redirect)\n"); err = execute_bin_sh_command("grep -h 2>&1", &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Can we really execute grep -h successfully?!?\n"); err = -1; goto out_clean; } printf("command that outputs to stderr\n"); err = execute_bin_sh_command("grep -h", &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if (!err) { printf("Can we really execute grep -h successfully?!?\n"); err = -1; goto out_clean; } printf("Testing ERROR conditions\n"); printf("empty command\n"); err = execute_bin_sh_command(NULL, &error_string); if (error_string) { printf("Error string: %s\n", error_string); free(error_string); error_string = NULL; } if ((!err) || (errno != EINVAL)) { printf("execute_bin_sh_command returned incorrect error or incorrect errno!\n"); err = -1; goto out_clean; } printf("empty error\n"); err = execute_bin_sh_command("true", NULL); if ((!err) || (errno != EINVAL)) { printf("execute_bin_sh_command returned incorrect error or incorrect errno!\n"); err = -1; goto out_clean; } err = 0; out_clean: return err; } int main(void) { need_root(); if (test() < 0) return FAIL; return PASS; } diff --git a/libnozzle/tests/nozzle_run_updown_exit_false b/libnozzle/tests/nozzle_run_updown_exit_false index 24581ca1..d34da19b 100755 --- a/libnozzle/tests/nozzle_run_updown_exit_false +++ b/libnozzle/tests/nozzle_run_updown_exit_false @@ -1,11 +1,11 @@ #!/bin/sh # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # exit 1 diff --git a/libnozzle/tests/nozzle_run_updown_exit_true b/libnozzle/tests/nozzle_run_updown_exit_true index 94d5d4d2..06820189 100755 --- a/libnozzle/tests/nozzle_run_updown_exit_true +++ b/libnozzle/tests/nozzle_run_updown_exit_true @@ -1,11 +1,11 @@ #!/bin/sh # -# Copyright (C) 2010-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # exit 0 diff --git a/libnozzle/tests/test-common.c b/libnozzle/tests/test-common.c index 469b4f25..2d1aeffc 100644 --- a/libnozzle/tests/test-common.c +++ b/libnozzle/tests/test-common.c @@ -1,176 +1,176 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include "test-common.h" void need_root(void) { if (geteuid() != 0) { printf("This test requires root privileges\n"); exit(SKIP); } } void need_tun(void) { #ifdef KNET_LINUX const char *tundev = "/dev/net/tun"; #else const char *tundev = "/dev/tun"; #endif int fd = open(tundev, O_RDWR); if (fd < 0) { printf("Failed to open %s (errno=%d); this test requires TUN support\n", tundev, errno); exit(SKIP); } close(fd); } int test_iface(char *name, size_t size, const char *updownpath) { nozzle_t nozzle; nozzle=nozzle_open(name, size, updownpath); if (!nozzle) { printf("Unable to open nozzle (errno=%d).\n", errno); return -1; } printf("Created interface: %s\n", name); if (is_if_in_system(name) > 0) { printf("Found interface %s on the system\n", name); } else { printf("Unable to find interface %s on the system\n", name); } if (!nozzle_get_handle_by_name(name)) { printf("Unable to find interface %s in nozzle db\n", name); } else { printf("Found interface %s in nozzle db\n", name); } nozzle_close(nozzle); if (is_if_in_system(name) == 0) printf("Successfully removed interface %s from the system\n", name); return 0; } int is_if_in_system(char *name) { struct ifaddrs *ifap = NULL; struct ifaddrs *ifa; int found = 0; if (getifaddrs(&ifap) < 0) { printf("Unable to get interface list.\n"); return -1; } ifa = ifap; while (ifa) { if (!strncmp(name, ifa->ifa_name, IFNAMSIZ)) { found = 1; break; } ifa=ifa->ifa_next; } freeifaddrs(ifap); return found; } int get_random_byte(void) { pid_t mypid; uint8_t *pid; uint8_t randombyte = 0; uint8_t i; if (sizeof(pid_t) < 4) { printf("pid_t is smaller than 4 bytes?\n"); exit(77); } mypid = getpid(); pid = (uint8_t *)&mypid; for (i = 0; i < sizeof(pid_t); i++) { if (pid[i] == 0) { pid[i] = 128; } } randombyte = pid[1]; return randombyte; } void make_local_ips(char *testipv4_1, char *testipv4_2, char *testipv6_1, char *testipv6_2) { pid_t mypid; uint8_t *pid; uint8_t i; memset(testipv4_1, 0, IPBUFSIZE); memset(testipv4_2, 0, IPBUFSIZE); memset(testipv6_1, 0, IPBUFSIZE); memset(testipv6_2, 0, IPBUFSIZE); mypid = getpid(); pid = (uint8_t *)&mypid; for (i = 0; i < sizeof(pid_t); i++) { if ((pid[i] == 0) || (pid[i] == 255)) { pid[i] = 128; } } snprintf(testipv4_1, IPBUFSIZE - 1, "127.%u.%u.%u", pid[1], pid[2], pid[0]); snprintf(testipv4_2, IPBUFSIZE - 1, "127.%u.%d.%u", pid[1], pid[2]+1, pid[0]); snprintf(testipv6_1, IPBUFSIZE - 1, "fd%x:%x%x::1", pid[1], pid[2], pid[0]); snprintf(testipv6_2, IPBUFSIZE - 1, "fd%x:%x%x:1::1", pid[1], pid[2], pid[0]); } diff --git a/libnozzle/tests/test-common.h b/libnozzle/tests/test-common.h index 87a86257..a4a98939 100644 --- a/libnozzle/tests/test-common.h +++ b/libnozzle/tests/test-common.h @@ -1,37 +1,37 @@ /* - * Copyright (C) 2018-2022 Red Hat, Inc. All rights reserved. + * Copyright (C) 2018-2023 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+ */ #ifndef __NOZZLE_TEST_COMMON_H__ #define __NOZZLE_TEST_COMMON_H__ #include "internals.h" #include "libnozzle.h" /* * error codes from automake test-driver */ #define PASS 0 #define SKIP 77 #define ERROR 99 #define FAIL -1 /* * common facilities */ #define IPBUFSIZE 1024 void need_root(void); void need_tun(void); int test_iface(char *name, size_t size, const char *updownpath); int is_if_in_system(char *name); int get_random_byte(void); void make_local_ips(char *testipv4_1, char *testipv4_2, char *testipv6_1, char *testipv6_2); #endif diff --git a/m4/pkg_check_var.m4 b/m4/pkg_check_var.m4 index a238fb6e..69244a87 100644 --- a/m4/pkg_check_var.m4 +++ b/m4/pkg_check_var.m4 @@ -1,20 +1,20 @@ -# Copyright (C) 2020-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # PKG_CHECK_VAR(VARIABLE, MODULE, CONFIG-VARIABLE, # [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND]) # ------------------------------------------- # Retrieves the value of the pkg-config variable for the given module. m4_ifndef([PKG_CHECK_VAR], [AC_DEFUN([PKG_CHECK_VAR], [AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl AC_ARG_VAR([$1], [value of $3 for $2, overriding pkg-config])dnl _PKG_CONFIG([$1], [variable="][$3]["], [$2]) AS_VAR_COPY([$1], [pkg_cv_][$1]) AS_VAR_IF([$1], [""], [$5], [$4])dnl ])# PKG_CHECK_VAR ]) diff --git a/man/Doxyfile-knet.in b/man/Doxyfile-knet.in index f2c11408..16039285 100644 --- a/man/Doxyfile-knet.in +++ b/man/Doxyfile-knet.in @@ -1,17 +1,17 @@ # -# Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # Christine Caulfield # # This software licensed under GPL-2.0+ # PROJECT_NAME = @PACKAGE_NAME@ PROJECT_NUMBER = @PACKAGE_VERSION@ INPUT = @abs_top_srcdir@/libknet/libknet.h XML_OUTPUT = @abs_builddir@/xml-knet GENERATE_XML = YES XML_PROGRAMLISTING = NO AUTOLINK_SUPPORT = NO GENERATE_HTML = NO GENERATE_LATEX = NO diff --git a/man/Doxyfile-nozzle.in b/man/Doxyfile-nozzle.in index a3428933..013e2c50 100644 --- a/man/Doxyfile-nozzle.in +++ b/man/Doxyfile-nozzle.in @@ -1,17 +1,17 @@ # -# Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # Christine Caulfield # # This software licensed under GPL-2.0+ # PROJECT_NAME = @PACKAGE_NAME@ PROJECT_NUMBER = @PACKAGE_VERSION@ INPUT = @abs_top_srcdir@/libnozzle/libnozzle.h XML_OUTPUT = @abs_builddir@/xml-nozzle GENERATE_XML = YES XML_PROGRAMLISTING = NO AUTOLINK_SUPPORT = NO GENERATE_HTML = NO GENERATE_LATEX = NO diff --git a/man/Makefile.am b/man/Makefile.am index fe16e0a1..706b8bf8 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -1,153 +1,153 @@ # -# Copyright (C) 2017-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2017-2023 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # Federico Simoncelli # # This software licensed under GPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk EXTRA_DIST = \ api-to-man-page-coverage if BUILD_MAN knet_man3_MANS = \ knet_addrtostr.3 \ knet_handle_add_datafd.3 \ knet_handle_clear_stats.3 \ knet_handle_compress.3 \ knet_handle_enable_filter.3 \ knet_handle_enable_pmtud_notify.3 \ knet_handle_enable_sock_notify.3 \ knet_handle_free.3 \ knet_handle_get_channel.3 \ knet_get_compress_list.3 \ knet_get_crypto_list.3 \ knet_handle_get_datafd.3 \ knet_handle_get_stats.3 \ knet_get_transport_id_by_name.3 \ knet_get_transport_list.3 \ knet_get_transport_name_by_id.3 \ knet_handle_get_transport_reconnect_interval.3 \ knet_handle_new.3 \ knet_handle_pmtud_get.3 \ knet_handle_pmtud_set.3 \ knet_handle_pmtud_getfreq.3 \ knet_handle_pmtud_setfreq.3 \ knet_handle_remove_datafd.3 \ knet_handle_setfwd.3 \ knet_handle_set_transport_reconnect_interval.3 \ knet_host_add.3 \ knet_host_enable_status_change_notify.3 \ knet_host_get_host_list.3 \ knet_host_get_id_by_host_name.3 \ knet_host_get_name_by_host_id.3 \ knet_host_get_policy.3 \ knet_host_get_status.3 \ knet_host_remove.3 \ knet_host_set_name.3 \ knet_host_set_policy.3 \ knet_link_clear_config.3 \ knet_link_get_config.3 \ knet_link_get_enable.3 \ knet_link_get_link_list.3 \ knet_link_get_ping_timers.3 \ knet_link_get_pong_count.3 \ knet_link_get_priority.3 \ knet_link_get_status.3 \ knet_link_set_config.3 \ knet_link_set_enable.3 \ knet_link_set_ping_timers.3 \ knet_link_set_pong_count.3 \ knet_link_set_priority.3 \ knet_log_get_loglevel.3 \ knet_log_get_loglevel_id.3 \ knet_log_get_loglevel_name.3 \ knet_log_get_subsystem_id.3 \ knet_log_get_subsystem_name.3 \ knet_log_set_loglevel.3 \ knet_recv.3 \ knet_send.3 \ knet_send_sync.3 \ knet_strtoaddr.3 \ knet_handle_set_threads_timer_res.3 \ knet_handle_get_threads_timer_res.3 \ knet_link_enable_status_change_notify.3 \ knet_handle_enable_access_lists.3 \ knet_link_add_acl.3 \ knet_link_insert_acl.3 \ knet_link_rm_acl.3 \ knet_link_clear_acl.3 \ knet_handle_crypto_set_config.3 \ knet_handle_crypto_use_config.3 \ knet_handle_crypto_rx_clear_traffic.3 \ knet_handle_enable_onwire_ver_notify.3 \ knet_handle_get_onwire_ver.3 \ knet_handle_set_onwire_ver.3 \ knet_handle_get_host_defrag_bufs.3 \ knet_handle_set_host_defrag_bufs.3 if BUILD_LIBNOZZLE nozzle_man3_MANS = \ nozzle_add_ip.3 \ nozzle_close.3 \ nozzle_del_ip.3 \ nozzle_get_fd.3 \ nozzle_get_handle_by_name.3 \ nozzle_get_ips.3 \ nozzle_get_mac.3 \ nozzle_get_mtu.3 \ nozzle_get_name_by_handle.3 \ nozzle_open.3 \ nozzle_reset_mac.3 \ nozzle_reset_mtu.3 \ nozzle_run_updown.3 \ nozzle_set_down.3 \ nozzle_set_mac.3 \ nozzle_set_mtu.3 \ nozzle_set_up.3 endif man3_MANS = $(knet_man3_MANS) $(nozzle_man3_MANS) $(MANS): doxyfile-knet.stamp doxyfile-nozzle.stamp # export LSAN_OPTIONS unconditionally for now. # there is no urgency to fix doxygen2man for leaks or bad memory access # since it's a one-shot tool and doesn't affect runtime. doxyfile-knet.stamp: Doxyfile-knet $(top_srcdir)/libknet/libknet.h $(DOXYGEN) Doxyfile-knet 2>&1 | $(EGREP) -v 'warning.*macro definition' LSAN_OPTIONS="exitcode=0" $(DOXYGEN2MAN) -m -P -o $(builddir) -s 3 -p @PACKAGE_NAME@ -H "Kronosnet Programmer's Manual" \ $$($(UTC_DATE_AT)$(SOURCE_EPOCH) +"-D %F -Y %Y") -d $(builddir)/xml-knet/ libknet_8h.xml touch doxyfile-knet.stamp doxyfile-nozzle.stamp: Doxyfile-nozzle $(top_srcdir)/libnozzle/libnozzle.h if BUILD_LIBNOZZLE $(DOXYGEN) Doxyfile-nozzle 2>&1 | $(EGREP) -v 'warning.*macro definition' LSAN_OPTIONS="exitcode=0" $(DOXYGEN2MAN) -m -P -o $(builddir) -s 3 -p @PACKAGE_NAME@ -H "Kronosnet Programmer's Manual" \ $$($(UTC_DATE_AT)$(SOURCE_EPOCH) +"-D %F -Y %Y") -d $(builddir)/xml-nozzle/ libnozzle_8h.xml endif touch doxyfile-nozzle.stamp noinst_SCRIPTS = api-to-man-page-coverage check-local: check-api-to-man-page-coverage-libknet check-api-to-man-page-coverage-libnozzle check-api-to-man-page-coverage-libnozzle: if BUILD_LIBNOZZLE $(srcdir)/api-to-man-page-coverage $(top_srcdir) nozzle endif check-api-to-man-page-coverage-libknet: $(srcdir)/api-to-man-page-coverage $(top_srcdir) knet endif clean-local: rm -rf doxyfile*.stamp xml* *.3 diff --git a/man/api-to-man-page-coverage b/man/api-to-man-page-coverage index 4e588786..c50824de 100755 --- a/man/api-to-man-page-coverage +++ b/man/api-to-man-page-coverage @@ -1,48 +1,48 @@ #!/bin/sh # -# Copyright (C) 2019-2022 Red Hat, Inc. All rights reserved. +# Copyright (C) 2019-2023 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+ # err=0 srcdir="$1" target="$2" headerapicalls="$(grep ${target}_ "$srcdir"/lib${target}/lib${target}.h | grep -v "^ \*" | grep -v ^struct | grep -v "^[[:space:]]" | grep -v typedef | sed -e 's/(.*//g' -e 's/^const //g' -e 's/\*//g' | awk '{print $2}')" manpages="$(grep ${target}_ "$srcdir"/man/Makefile.am |grep -v man3 |grep -v xml | sed -e 's/\.3.*//g')" echo "Checking for header format errors" for i in $headerapicalls; do echo "Checking $i" header="$(grep " \* ${i}$" "$srcdir"/lib${target}/lib${target}.h -A2)" brief="$(echo "$header" | tail -n 1 |grep "@brief")" if [ -z "$brief" ]; then echo "Error found in $i doxy header section" echo "$header" echo "" echo "$brief" exit 1 fi done echo "Checking for symbols in header file NOT distributed as manpages" for i in $headerapicalls; do found=0 for x in $manpages; do if [ "$x" = "$i" ]; then found=1 break; fi done if [ "$found" = 0 ]; then echo "API man page $i not found not found in $srcdir/man/Makefile.am" err=1 fi done exit $err