diff --git a/Makefile.am b/Makefile.am index 25398bbb..047127d5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,210 +1,210 @@ # # Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+, LGPL-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 = init libtap libknet kronosnetd +SUBDIRS = init libnozzle libknet kronosnetd if BUILD_MAN SUBDIRS += man endif if BUILD_POC SUBDIRS += poc-code endif dist_doc_DATA = \ COPYING.applications \ COPYING.libraries \ COPYRIGHT \ README.licence \ README all-local: $(SPEC) clean-local: rm -f $(SPEC) 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"`" && \ if [ -f $(abs_srcdir)/.tarball-version ]; then \ gitver="`cat $(abs_srcdir)/.tarball-version`" && \ rpmver=$$gitver && \ alphatag="" && \ dirty="" && \ numcomm="0"; \ elif [ "`git log -1 --pretty=format:x . 2>&1`" = "x" ]; then \ gitver="`GIT_DIR=$(abs_srcdir)/.git git describe --abbrev=4 --match='v*' HEAD 2>/dev/null`" && \ rpmver=`echo $$gitver | sed -e "s/^v//" -e "s/-.*//g"` && \ alphatag=`echo $$gitver | sed -e "s/.*-//" -e "s/^g//"` && \ vtag=`echo $$gitver | sed -e "s/-.*//g"` && \ numcomm=`GIT_DIR=$(abs_srcdir)/.git git rev-list $$vtag..HEAD | wc -l` && \ cd $(abs_srcdir) && \ git update-index --refresh > /dev/null 2>&1 || true && \ dirty=`git diff-index --name-only HEAD 2>/dev/null` && cd - 2>/dev/null; \ else \ gitver="`cd $(abs_srcdir); build-aux/git-version-gen .tarball-version .gitarchivever`" && \ rpmver=$$gitver && \ alphatag="" && \ dirty="" && \ numcomm="0"; \ 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_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_KRONOSNETD sed -i -e "s#@kronosnetd@#bcond_without#g" $@-t else sed -i -e "s#@kronosnetd@#bcond_with#g" $@-t endif -if BUILD_LIBTAP - sed -i -e "s#@libtap@#bcond_without#g" $@-t +if BUILD_LIBNOZZLE + sed -i -e "s#@libnozzle@#bcond_without#g" $@-t else - sed -i -e "s#@libtap@#bcond_with#g" $@-t + 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 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 829e7331..5acff702 100644 --- a/NOTES_TO_PACKAGE_MAINTAINERS +++ b/NOTES_TO_PACKAGE_MAINTAINERS @@ -1,31 +1,34 @@ 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. DO NOT ship kronosnetd. It's an experimental piece of super crappy code. IF you decide to ship it anyway, you get to be also the upstream maintainer :-) you have been warned. -libtap is a simple commodity library used only by kronosnetd. -I don't mind to support it, but don't ship just for the fun of it. -It has no users, that I know of, outside of kronosnetd. +libnozzle is a simple commodity library used only by kronosnetd +and a future release of corosync. This is the first GA release +for libnozzle, please allow space for minor screw ups. -libknet is still under heavy development. There might be more -onwire network changes that could make current version -incompatible with newer versions. Make sure to warn your users -about it. Plan is to release a stable version (1.0) when -we are confident that the current implementation is stable enough. +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 bf602992..a50f27fd 100644 --- a/README +++ b/README @@ -1,35 +1,36 @@ # # Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+, LGPL-2.0+ # Upstream resources ------------------ https://github.com/kronosnet/kronosnet/ https://ci.kronosnet.org/ https://trello.com/kronosnet (TODO list and activities tracking) https://goo.gl/9ZvkLS (google shared drive) https://github.com/kronosnet/knet-ansible-ci https://lists.kronosnet.org/mailman/listinfo/users https://lists.kronosnet.org/mailman/listinfo/devel https://lists.kronosnet.org/mailman/listinfo/commits https://kronosnet.org/ (web 0.1 style) IRC: #kronosnet on Freenode 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. +libnozzle requires if_tap.ko loaded in the kernel. diff --git a/build-aux/git-version-gen b/build-aux/git-version-gen index 693513e7..fc70e306 100755 --- a/build-aux/git-version-gen +++ b/build-aux/git-version-gen @@ -1,263 +1,263 @@ #!/bin/sh # Print a version string. scriptversion=2018-08-31.20; # UTC -# Copyright (C) 2018 Red Hat, Inc. +# Copyright (C) 2012-2018 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 else v=UNKNOWN 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/update-copyright.sh b/build-aux/update-copyright.sh index babb25a3..fd50f8ea 100755 --- a/build-aux/update-copyright.sh +++ b/build-aux/update-copyright.sh @@ -1,29 +1,29 @@ #!/bin/sh # # Copyright (C) 2017 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+, LGPL-2.0+ # # script to update copyright dates across the tree enddate=$(date +%Y) -input=$(grep -ril -e "Copyright.*Red Hat" |grep -v .swp |grep -v update-copyright) +input=$(grep -ril -e "Copyright.*Red Hat" |grep -v .swp |grep -v update-copyright |grep -v doxyxml.c) for i in $input; do startdate=$(git log --follow "$i" | grep ^Date: | tail -n 1 | awk '{print $6}') if [ "$startdate" != "$enddate" ]; then sed -i -e 's#Copyright (C).*Red Hat#Copyright (C) '$startdate'-'$enddate' Red Hat#g' $i else sed -i -e 's#Copyright (C).*Red Hat#Copyright (C) '$startdate' Red Hat#g' $i fi done input=$(find . -type f |grep -v ".git") for i in $input; do if [ -z "$(grep -i "Copyright" $i)" ]; then echo "WARNING: $i appears to be missing Copyright information" fi done diff --git a/configure.ac b/configure.ac index 07997ab4..2a6608f1 100644 --- a/configure.ac +++ b/configure.ac @@ -1,528 +1,540 @@ # # Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # Federico Simoncelli # # This software licensed under GPL-2.0+, LGPL-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"]) AC_SUBST([AM_LDFLAGS]) saved_LDFLAGS="$LDFLAGS" LDFLAGS="$AM_LDFLAGS $LDFLAGS" LT_INIT LDFLAGS="$saved_LDFLAGS" AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_SRCDIR([kronosnetd/main.c]) AC_CONFIG_HEADERS([config.h]) AC_CANONICAL_HOST AC_LANG([C]) systemddir=${prefix}/lib/systemd/system if test "$prefix" = "NONE"; then prefix="/usr" if test "$localstatedir" = "\${prefix}/var"; then localstatedir="/var" fi if test "$sysconfdir" = "\${prefix}/etc"; then sysconfdir="/etc" fi if test "$systemddir" = "NONE/lib/systemd/system"; then systemddir=/lib/systemd/system 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 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"]) # 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([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 < 1.1], [AC_DEFINE_UNQUOTED([BUILDCRYPTOOPENSSL10], [1], [openssl 1.0 crypto])], [PKG_CHECK_MODULES([openssl],[libcrypto >= 1.1], [AC_DEFINE_UNQUOTED([BUILDCRYPTOOPENSSL11], [1], [openssl 1.1 crypto])])]) ]) AC_ARG_ENABLE([compress-all], [AS_HELP_STRING([--disable-compress-all],[disable libknet all compress modules support])],, [ enable_compress_all="yes" ]) 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([poc], [AS_HELP_STRING([--enable-poc],[enable building poc code])],, [ enable_poc="no" ]) AM_CONDITIONAL([BUILD_POC], [test x$enable_poc = xyes]) AC_ARG_ENABLE([kronosnetd], [AS_HELP_STRING([--enable-kronosnetd],[Kronosnetd support])],, [ enable_kronosnetd="no" ]) AM_CONDITIONAL([BUILD_KRONOSNETD], [test x$enable_kronosnetd = 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([libtap], - [AS_HELP_STRING([--enable-libtap],[libtap support])],, - [ enable_libtap="no" ]) +AC_ARG_ENABLE([libnozzle], + [AS_HELP_STRING([--enable-libnozzle],[libnozzle support])],, + [ enable_libnozzle="yes" ]) if test "x$enable_kronosnetd" = xyes; then - enable_libtap=yes + enable_libnozzle=yes fi -AM_CONDITIONAL([BUILD_LIBTAP], [test x$enable_libtap = xyes]) +AM_CONDITIONAL([BUILD_LIBNOZZLE], [test x$enable_libnozzle = 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="$@" if echo $CC | grep -q clang; then CPPFLAGS="-Werror $CPPFLAGS" fi AC_MSG_CHECKING([whether $CC supports "$@"]) - AC_PREPROC_IFELSE([AC_LANG_PROGRAM([])], + 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" # 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]) - if test "x$enable_libtap" = xyes; then - AC_MSG_ERROR([libtap is not currently supported on BSD platforms]) - fi ;; *) 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 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 # required by doxyxml to build man pages dynamically # Don't let AC_PROC_CC (invoked by AX_PROG_CC_FOR_BUILD) replace # undefined CFLAGS_FOR_BUILD with -g -O2, overriding our special OPT_CFLAGS. : ${CFLAGS_FOR_BUILD=""} AX_PROG_CC_FOR_BUILD 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_MODULES([libxml_BUILD], [libxml-2.0]) PKG_CONFIG="$saved_PKG_CONFIG" ac_cv_path_PKG_CONFIG="$saved_ac_cv_path_PKG_CONFIG" fi -# checks (for kronosnetd) +# 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 + +# checks for kronosnetd if test "x$enable_kronosnetd" = xyes; then AC_CHECK_HEADERS([security/pam_appl.h], [AC_CHECK_LIB([pam], [pam_start], [AC_SUBST([pam_LIBS], [-lpam])], [AC_MSG_ERROR([Unable to find LinuxPAM devel files])])]) AC_CHECK_HEADERS([security/pam_misc.h], [AC_CHECK_LIB([pam_misc], [misc_conv], [AC_SUBST([pam_misc_LIBS], [-lpam_misc])], [AC_MSG_ERROR([Unable to find LinuxPAM MISC devel files])])]) PKG_CHECK_MODULES([libqb], [libqb]) AC_CHECK_LIB([qb], [qb_log_thread_priority_set], [have_qb_log_thread_priority_set="yes"], [have_qb_log_thread_priority_set="no"]) if test "x${have_qb_log_thread_priority_set}" = xyes; then AC_DEFINE_UNQUOTED([HAVE_QB_LOG_THREAD_PRIORITY_SET], [1], [have qb_log_thread_priority_set]) fi fi # local options AC_ARG_ENABLE([debug], [AS_HELP_STRING([--enable-debug],[enable debug build])]) AC_ARG_WITH([initdefaultdir], [AS_HELP_STRING([--with-initdefaultdir=DIR],[path to /etc/sysconfig or /etc/default dir])], [ INITDEFAULTDIR="$withval" ], [ INITDEFAULTDIR="$sysconfdir/default" ]) AC_ARG_WITH([initddir], [AS_HELP_STRING([--with-initddir=DIR],[path to init script directory])], [ INITDDIR="$withval" ], [ INITDDIR="$sysconfdir/init.d" ]) AC_ARG_WITH([systemddir], [AS_HELP_STRING([--with-systemddir=DIR],[path to systemd unit files directory])], [ SYSTEMDDIR="$withval" ], [ SYSTEMDDIR="$systemddir" ]) AC_ARG_WITH([syslogfacility], [AS_HELP_STRING([--with-syslogfacility=FACILITY],[default syslog facility])], [ SYSLOGFACILITY="$withval" ], [ SYSLOGFACILITY="LOG_DAEMON" ]) AC_ARG_WITH([sysloglevel], [AS_HELP_STRING([--with-sysloglevel=LEVEL],[default syslog level])], [ SYSLOGLEVEL="$withval" ], [ SYSLOGLEVEL="LOG_INFO" ]) AC_ARG_WITH([defaultadmgroup], [AS_HELP_STRING([--with-defaultadmgroup=GROUP], [define PAM group. Users part of this group will be allowed to configure kronosnet. Others will only receive read-only rights.])], [ DEFAULTADMGROUP="$withval" ], [ DEFAULTADMGROUP="kronosnetadm" ]) ## random vars LOGDIR=${localstatedir}/log/ RUNDIR=${localstatedir}/run/ DEFAULT_CONFIG_DIR=${sysconfdir}/kronosnet ## do subst AC_SUBST([DEFAULT_CONFIG_DIR]) AC_SUBST([INITDEFAULTDIR]) AC_SUBST([INITDDIR]) AC_SUBST([SYSTEMDDIR]) AC_SUBST([LOGDIR]) AC_SUBST([DEFAULTADMGROUP]) AC_DEFINE_UNQUOTED([DEFAULT_CONFIG_DIR], ["$(eval echo ${DEFAULT_CONFIG_DIR})"], [Default config directory]) AC_DEFINE_UNQUOTED([DEFAULT_CONFIG_FILE], ["$(eval echo ${DEFAULT_CONFIG_DIR}/kronosnetd.conf)"], [Default config file]) AC_DEFINE_UNQUOTED([LOGDIR], ["$(eval echo ${LOGDIR})"], [Default logging directory]) AC_DEFINE_UNQUOTED([DEFAULT_LOG_FILE], ["$(eval echo ${LOGDIR}/kronosnetd.log)"], [Default log file]) AC_DEFINE_UNQUOTED([RUNDIR], ["$(eval echo ${RUNDIR})"], [Default run directory]) AC_DEFINE_UNQUOTED([SYSLOGFACILITY], [$(eval echo ${SYSLOGFACILITY})], [Default syslog facility]) AC_DEFINE_UNQUOTED([SYSLOGLEVEL], [$(eval echo ${SYSLOGLEVEL})], [Default syslog level]) AC_DEFINE_UNQUOTED([DEFAULTADMGROUP], ["$(eval echo ${DEFAULTADMGROUP})"], [Default admin group]) # debug build stuff if test "x${enable_debug}" = xyes; then AC_DEFINE_UNQUOTED([DEBUG], [1], [Compiling Debugging code]) OPT_CFLAGS="-O0" else OPT_CFLAGS="-O3" fi # gdb flags if test "x${GCC}" = xyes; then GDB_FLAGS="-ggdb3" else GDB_FLAGS="-g" fi # extra warnings EXTRA_WARNINGS="" WARNLIST=" all extra unused shadow missing-prototypes missing-declarations suggest-attribute=noreturn suggest-attribute=format + property-attribute-mismatch strict-prototypes declaration-after-statement pointer-arith write-strings cast-align bad-function-cast missing-format-attribute float-equal format=2 format-signedness format-security format-nonliteral no-long-long unsigned-char gnu89-inline no-strict-aliasing error address cpp overflow parentheses sequence-point switch + shift-overflow shift-overflow=2 overlength-strings retundent-decls init-self uninitialized unused-but-set-variable + unused-const-variable unused-function unused-result unused-value unused-variable unknown-pragmas no-unused-parameter " for j in $WARNLIST; do if cc_supports_flag -W$j; then EXTRA_WARNINGS="$EXTRA_WARNINGS -W$j"; fi done AC_SUBST([AM_CFLAGS],["$OPT_CFLAGS $GDB_FLAGS $EXTRA_WARNINGS"]) 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 init/Makefile - libtap/Makefile - libtap/libtap.pc + libnozzle/Makefile + libnozzle/libnozzle.pc + libnozzle/tests/Makefile kronosnetd/Makefile kronosnetd/kronosnetd.logrotate libknet/Makefile libknet/libknet.pc libknet/tests/Makefile man/Makefile man/Doxyfile-knet + man/Doxyfile-nozzle poc-code/Makefile poc-code/iov-hash/Makefile poc-code/access-list/Makefile ]) 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 a9cb445e..9e9903aa 100644 --- a/kronosnet.spec.in +++ b/kronosnet.spec.in @@ -1,537 +1,540 @@ ############################################################################### ############################################################################### ## ## Copyright (C) 2012-2018 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 %@zlib@ zlib %@lz4@ lz4 %@lzo2@ lzo2 %@lzma@ lzma %@bzip2@ bzip2 %@kronosnetd@ kronosnetd -%@libtap@ libtap +%@libnozzle@ libnozzle %@runautogen@ runautogen %@rpmdebuginfo@ rpmdebuginfo %@overriderpmdebuginfo@ overriderpmdebuginfo %@buildman@ buildman %if %{with overriderpmdebuginfo} %undefine _enable_debug_packages %endif %if %{with sctp} %global buildsctp 1 %endif %if %{with nss} %global buildcryptonss 1 %endif %if %{with openssl} %global buildcryptoopenssl 1 %endif %if %{with zlib} %global buildcompresszlib 1 %endif %if %{with lz4} %global buildcompresslz4 1 %endif %if %{with lzo2} %global buildcompresslzo2 1 %endif %if %{with lzma} %global buildcompresslzma 1 %endif %if %{with bzip2} %global buildcompressbzip2 1 %endif -%if %{with libtap} -%global buildlibtap 1 +%if %{with libnozzle} +%global buildlibnozzle 1 %endif %if %{with kronosnetd} -%global buildlibtap 1 +%global buildlibnozzle 1 %global buildkronosnetd 1 %endif %if %{with runautogen} %global buildautogen 1 %endif %if %{with buildman} %global buildmanpages 1 %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+ Group: System Environment/Base URL: https://github.com/kronosnet/kronosnet/ Source0: https://github.com/kronosnet/kronosnet/archive/%{name}-%{version}%{?numcomm:.%{numcomm}}%{?alphatag:-%{alphatag}}%{?dirty:-%{dirty}}.tar.gz ## Setup/build bits BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) # Build dependencies BuildRequires: gcc # required to build man pages %if %{defined buildmanpages} BuildRequires: libqb-devel libxml2-devel doxygen %endif %if %{defined buildsctp} BuildRequires: lksctp-tools-devel %endif %if %{defined buildcryptonss} BuildRequires: /usr/include/nss3/nss.h /usr/include/nspr4/nspr.h %endif %if %{defined buildcryptoopenssl} BuildRequires: /usr/include/openssl/conf.h %endif %if %{defined buildcompresszlib} BuildRequires: zlib-devel %endif %if %{defined buildcompresslz4} BuildRequires: /usr/include/lz4hc.h %endif %if %{defined buildcompresslzo2} BuildRequires: lzo-devel %endif %if %{defined buildcompresslzma} BuildRequires: xz-devel %endif %if %{defined buildcompressbzip2} BuildRequires: /usr/include/bzlib.h %endif %if %{defined buildkronosnetd} BuildRequires: pam-devel %endif +%if %{defined buildlibnozzle} +BuildRequires: libnl3-devel +%endif %if %{defined buildautogen} BuildRequires: autoconf BuildRequires: automake BuildRequires: libtool %endif %prep %setup -q -n %{name}-%{version}%{?numcomm:.%{numcomm}}%{?alphatag:-%{alphatag}}%{?dirty:-%{dirty}} %build %if %{with runautogen} ./autogen.sh %endif %{configure} \ %if %{defined buildmanpages} --enable-man \ %else --disable-man \ %endif %if %{defined buildsctp} --enable-libknet-sctp \ %else --disable-libknet-sctp \ %endif %if %{defined buildcryptonss} --enable-crypto-nss \ %else --disable-crypto-nss \ %endif %if %{defined buildcryptoopenssl} --enable-crypto-openssl \ %else --disable-crypto-openssl \ %endif %if %{defined buildcompresszlib} --enable-compress-zlib \ %else --disable-compress-zlib \ %endif %if %{defined buildcompresslz4} --enable-compress-lz4 \ %else --disable-compress-lz4 \ %endif %if %{defined buildcompresslzo2} --enable-compress-lzo2 \ %else --disable-compress-lzo2 \ %endif %if %{defined buildcompresslzma} --enable-compress-lzma \ %else --disable-compress-lzma \ %endif %if %{defined buildcompressbzip2} --enable-compress-bzip2 \ %else --disable-compress-bzip2 \ %endif %if %{defined buildkronosnetd} --enable-kronosnetd \ %endif -%if %{defined buildlibtap} - --enable-libtap \ +%if %{defined buildlibnozzle} + --enable-libnozzle \ %endif --with-initdefaultdir=%{_sysconfdir}/sysconfig/ \ %if %{defined _unitdir} --with-systemddir=%{_unitdir} %else --with-initddir=%{_sysconfdir}/rc.d/init.d/ %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 {} \; # handle systemd vs init script %if %{defined _unitdir} # remove init scripts rm -rf %{buildroot}/etc/init.d %else # remove systemd specific bits find %{buildroot} -name "*.service" -exec rm {} \; %endif # remove docs rm -rf %{buildroot}/usr/share/doc/kronosnet %clean rm -rf %{buildroot} # main empty package %description kronosnet source %if %{defined buildkronosnetd} ## Runtime and subpackages section %package -n kronosnetd Group: System Environment/Base Summary: Multipoint-to-Multipoint VPN daemon %if %{defined _unitdir} # Needed for systemd unit Requires(post): systemd-sysv Requires(post): systemd-units Requires(preun): systemd-units Requires(postun): systemd-units %else Requires(post): chkconfig Requires(preun): chkconfig, initscripts %endif Requires(post): shadow-utils Requires(preun): shadow-utils Requires: pam, /etc/pam.d/passwd %description -n kronosnetd The kronosnet daemon is a bridge between kronosnet switching engine and kernel network tap devices, to create and administer a distributed LAN over multipoint-to-multipoint VPNs. The daemon does a poor attempt to provide a configure UI similar to other known network devices/tools (Cisco, quagga). Beside looking horrific, it allows runtime changes and reconfiguration of the kronosnet(s) without daemon reload or service disruption. %post -n kronosnetd %if %{defined _unitdir} %if 0%{?systemd_post:1} %systemd_post kronosnetd.service %else /bin/systemctl daemon-reload >/dev/null 2>&1 || : %endif %else /sbin/chkconfig --add kronosnetd %endif /usr/sbin/groupadd --force --system @defaultadmgroup@ %preun -n kronosnetd %if %{defined _unitdir} %if 0%{?systemd_preun:1} %systemd_preun kronosnetd.service %else if [ "$1" -eq 0 ]; then /bin/systemctl --no-reload disable kronosnetd.service /bin/systemctl stop kronosnetd.service >/dev/null 2>&1 fi %endif %else if [ "$1" = 0 ]; then /sbin/service kronosnetd stop >/dev/null 2>&1 /sbin/chkconfig --del kronosnetd fi %endif %files -n kronosnetd %defattr(-,root,root,-) %doc COPYING.* COPYRIGHT %dir %{_sysconfdir}/kronosnet %dir %{_sysconfdir}/kronosnet/* %config(noreplace) %{_sysconfdir}/sysconfig/kronosnetd %config(noreplace) %{_sysconfdir}/pam.d/kronosnetd %config(noreplace) %{_sysconfdir}/logrotate.d/kronosnetd %if %{defined _unitdir} %{_unitdir}/kronosnetd.service %else %config(noreplace) %{_sysconfdir}/rc.d/init.d/kronosnetd %endif %{_sbindir}/* %{_mandir}/man8/* %endif -%if %{defined buildlibtap} -%package -n libtap1 +%if %{defined buildlibnozzle} +%package -n libnozzle1 Group: System Environment/Libraries Summary: Simple userland wrapper around kernel tap devices -%description -n libtap1 +%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 libtap1 +%files -n libnozzle1 %defattr(-,root,root,-) %doc COPYING.* COPYRIGHT -%{_libdir}/libtap.so.* +%{_libdir}/libnozzle.so.* -%post -n libtap1 -p /sbin/ldconfig +%post -n libnozzle1 -p /sbin/ldconfig -%postun -n libtap1 -p /sbin/ldconfig +%postun -n libnozzle1 -p /sbin/ldconfig -%package -n libtap1-devel +%package -n libnozzle1-devel Group: Development/Libraries Summary: Simple userland wrapper around kernel tap devices (developer files) -Requires: libtap1 = %{version}-%{release} +Requires: libnozzle1 = %{version}-%{release} Requires: pkgconfig -%description -n libtap1-devel +%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 libtap1-devel +%files -n libnozzle1-devel %defattr(-,root,root,-) %doc COPYING.* COPYRIGHT -%{_libdir}/libtap.so -%{_includedir}/libtap.h -%{_libdir}/pkgconfig/libtap.pc +%{_libdir}/libnozzle.so +%{_includedir}/libnozzle.h +%{_libdir}/pkgconfig/libnozzle.pc %endif %package -n libknet1 Group: System Environment/Libraries Summary: Kronosnet core switching implementation %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 %defattr(-,root,root,-) %doc COPYING.* COPYRIGHT %{_libdir}/libknet.so.* %dir %{_libdir}/kronosnet %post -n libknet1 -p /sbin/ldconfig %postun -n libknet1 -p /sbin/ldconfig %package -n libknet1-devel Group: Development/Libraries Summary: Kronosnet core switching implementation (developer files) Requires: libknet1 = %{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 %defattr(-,root,root,-) %doc COPYING.* COPYRIGHT %{_libdir}/libknet.so %{_includedir}/libknet.h %{_libdir}/pkgconfig/libknet.pc %if %{defined buildmanpages} %{_mandir}/man3/knet*.3.gz %endif %if %{defined buildcryptonss} %package -n libknet1-crypto-nss-plugin Group: System Environment/Libraries Summary: libknet1 nss support Requires: libknet1 = %{version}-%{release} %description -n libknet1-crypto-nss-plugin NSS crypto support for libknet1. %files -n libknet1-crypto-nss-plugin %defattr(-,root,root,-) %{_libdir}/kronosnet/crypto_nss.so %endif %if %{defined buildcryptoopenssl} %package -n libknet1-crypto-openssl-plugin Group: System Environment/Libraries Summary: libknet1 openssl support Requires: libknet1 = %{version}-%{release} %description -n libknet1-crypto-openssl-plugin OpenSSL crypto support for libknet1. %files -n libknet1-crypto-openssl-plugin %defattr(-,root,root,-) %{_libdir}/kronosnet/crypto_openssl.so %endif %if %{defined buildcompresszlib} %package -n libknet1-compress-zlib-plugin Group: System Environment/Libraries Summary: libknet1 zlib support Requires: libknet1 = %{version}-%{release} %description -n libknet1-compress-zlib-plugin zlib compression support for libknet1. %files -n libknet1-compress-zlib-plugin %defattr(-,root,root,-) %{_libdir}/kronosnet/compress_zlib.so %endif %if %{defined buildcompresslz4} %package -n libknet1-compress-lz4-plugin Group: System Environment/Libraries Summary: libknet1 lz4 and lz4hc support Requires: libknet1 = %{version}-%{release} %description -n libknet1-compress-lz4-plugin lz4 and lz4hc compression support for libknet1. %files -n libknet1-compress-lz4-plugin %defattr(-,root,root,-) %{_libdir}/kronosnet/compress_lz4.so %{_libdir}/kronosnet/compress_lz4hc.so %endif %if %{defined buildcompresslzo2} %package -n libknet1-compress-lzo2-plugin Group: System Environment/Libraries Summary: libknet1 lzo2 support Requires: libknet1 = %{version}-%{release} %description -n libknet1-compress-lzo2-plugin lzo2 compression support for libknet1. %files -n libknet1-compress-lzo2-plugin %defattr(-,root,root,-) %{_libdir}/kronosnet/compress_lzo2.so %endif %if %{defined buildcompresslzma} %package -n libknet1-compress-lzma-plugin Group: System Environment/Libraries Summary: libknet1 lzma support Requires: libknet1 = %{version}-%{release} %description -n libknet1-compress-lzma-plugin lzma compression support for libknet1. %files -n libknet1-compress-lzma-plugin %defattr(-,root,root,-) %{_libdir}/kronosnet/compress_lzma.so %endif %if %{defined buildcompressbzip2} %package -n libknet1-compress-bzip2-plugin Group: System Environment/Libraries Summary: libknet1 bzip2 support Requires: libknet1 = %{version}-%{release} %description -n libknet1-compress-bzip2-plugin bzip2 compression support for libknet1. %files -n libknet1-compress-bzip2-plugin %defattr(-,root,root,-) %{_libdir}/kronosnet/compress_bzip2.so %endif %package -n libknet1-crypto-plugins-all Group: System Environment/Libraries Summary: libknet1 crypto plugins meta package %if %{defined buildcryptonss} Requires: libknet1-crypto-nss-plugin %endif %if %{defined buildcryptoopenssl} Requires: libknet1-crypto-openssl-plugin %endif %description -n libknet1-crypto-plugins-all meta package to install all of libknet1 crypto plugins %files -n libknet1-crypto-plugins-all %package -n libknet1-compress-plugins-all Group: System Environment/Libraries Summary: libknet1 compress plugins meta package %if %{defined buildcompresszlib} Requires: libknet1-compress-zlib-plugin %endif %if %{defined buildcompresslz4} Requires: libknet1-compress-lz4-plugin %endif %if %{defined buildcompresslzo2} Requires: libknet1-compress-lzo2-plugin %endif %if %{defined buildcompresslzma} Requires: libknet1-compress-lzma-plugin %endif %if %{defined buildcompressbzip2} Requires: libknet1-compress-bzip2-plugin %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 Group: System Environment/Libraries Summary: libknet1 plugins meta package Requires: libknet1-compress-plugins-all Requires: libknet1-crypto-plugins-all %description -n libknet1-plugins-all meta package to install all of libknet1 plugins %files -n libknet1-plugins-all %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/kronosnetd/Makefile.am b/kronosnetd/Makefile.am index 8c233579..9dfb2445 100644 --- a/kronosnetd/Makefile.am +++ b/kronosnetd/Makefile.am @@ -1,89 +1,89 @@ # # Copyright (C) 2012-2018 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+, LGPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in kronostnetd.logrotate include $(top_srcdir)/build-aux/check.mk EXTRA_DIST = kronosnetd.logrotate.in noinst_HEADERS = \ cfg.h \ etherfilter.h \ logging.h \ vty.h \ vty_auth.h \ vty_cli.h \ vty_cli_cmds.h \ vty_utils.h kronosnetd_SOURCES = \ cfg.c \ etherfilter.c \ main.c \ logging.c \ vty.c \ vty_auth.c \ vty_cli.c \ vty_cli_cmds.c \ vty_utils.c kronosnetd_CPPFLAGS = \ - -I$(top_srcdir)/libtap \ + -I$(top_srcdir)/libnozzle \ -I$(top_srcdir)/libknet kronosnetd_CFLAGS = $(AM_CFLAGS) $(libqb_CFLAGS) kronosnetd_LDADD = \ $(top_builddir)/libknet/libknet.la \ - $(top_builddir)/libtap/libtap.la \ + $(top_builddir)/libnozzle/libnozzle.la \ $(libqb_LIBS) \ $(pam_misc_LIBS) $(pam_LIBS) knet_keygen_SOURCES = keygen.c knet_keygen_CPPFLAGS = -I$(top_srcdir)/libknet if BUILD_KRONOSNETD sbin_PROGRAMS = kronosnetd \ knet-keygen install-exec-local: $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR) $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/down.d $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/post-down.d $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/pre-up.d $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/up.d $(INSTALL) -d $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/cryptokeys.d $(INSTALL) -d -m 0755 $(DESTDIR)/$(sysconfdir)/logrotate.d $(INSTALL) -m 644 kronosnetd.logrotate $(DESTDIR)/$(sysconfdir)/logrotate.d/kronosnetd $(INSTALL) -d -m 0755 $(DESTDIR)/$(sysconfdir)/pam.d if [ -a $(sysconfdir)/pam.d/password-auth ]; then \ cd $(DESTDIR)/$(sysconfdir)/pam.d && \ rm -f kronosnetd && \ $(LN_S) password-auth kronosnetd; \ else \ cd $(DESTDIR)/$(sysconfdir)/pam.d && \ rm -f kronosnetd && \ $(LN_S) passwd kronosnetd; \ fi uninstall-local: rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/cryptokeys.d || :; rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/down.d || :; rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/post-down.d || :; rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/pre-up.d || :; rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR)/up.d || :; rmdir $(DESTDIR)/$(DEFAULT_CONFIG_DIR) || :; rm -f $(DESTDIR)/$(sysconfdir)/logrotate.d/kronosnetd rmdir $(DESTDIR)/$(sysconfdir)/logrotate.d || :; rm -f $(DESTDIR)/$(sysconfdir)/pam.d/kronosnetd || :; rmdir $(DESTDIR)/$(sysconfdir)/pam.d || :; endif diff --git a/kronosnetd/cfg.c b/kronosnetd/cfg.c index 60fdc15d..d2663926 100644 --- a/kronosnetd/cfg.c +++ b/kronosnetd/cfg.c @@ -1,88 +1,88 @@ /* * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #include "cfg.h" -#include "libtap.h" +#include "libnozzle.h" struct knet_cfg *knet_get_iface(const char *name, int create) { struct knet_cfg *knet_iface = knet_cfg_head.knet_cfg; int found = 0; while (knet_iface != NULL) { if (!strcmp(tap_get_name(knet_iface->cfg_eth.tap), name)) { found = 1; break; } knet_iface = knet_iface->next; } if ((!found) && (create)) { knet_iface = malloc(sizeof(struct knet_cfg)); if (!knet_iface) goto out_clean; memset(knet_iface, 0, sizeof(struct knet_cfg)); knet_iface->cfg_ring.base_port = KNET_RING_DEFPORT; strncpy(knet_iface->knet_handle_crypto_cfg.crypto_model, "none", sizeof(knet_iface->knet_handle_crypto_cfg.crypto_model) - 1); strncpy(knet_iface->knet_handle_crypto_cfg.crypto_cipher_type, "none", sizeof(knet_iface->knet_handle_crypto_cfg.crypto_cipher_type) - 1); strncpy(knet_iface->knet_handle_crypto_cfg.crypto_hash_type, "none", sizeof(knet_iface->knet_handle_crypto_cfg.crypto_hash_type) - 1); if (knet_cfg_head.knet_cfg) { struct knet_cfg *knet_iface_last = knet_cfg_head.knet_cfg; while (knet_iface_last->next != NULL) { knet_iface_last = knet_iface_last->next; } knet_iface_last->next = knet_iface; } else { knet_cfg_head.knet_cfg = knet_iface; } } out_clean: return knet_iface; } void knet_destroy_iface(struct knet_cfg *knet_iface) { struct knet_cfg *knet_iface_tmp = knet_cfg_head.knet_cfg; struct knet_cfg *knet_iface_prev = knet_cfg_head.knet_cfg; while (knet_iface_tmp != knet_iface) { knet_iface_prev = knet_iface_tmp; knet_iface_tmp = knet_iface_tmp->next; } if (knet_iface_tmp == knet_iface) { if (knet_iface_tmp == knet_iface_prev) { knet_cfg_head.knet_cfg = knet_iface_tmp->next; } else { knet_iface_prev->next = knet_iface_tmp->next; } free(knet_iface); } } diff --git a/kronosnetd/cfg.h b/kronosnetd/cfg.h index 49f66af7..b884ffb4 100644 --- a/kronosnetd/cfg.h +++ b/kronosnetd/cfg.h @@ -1,55 +1,55 @@ /* * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #ifndef __KNETD_CFG_H__ #define __KNETD_CFG_H__ #include #include -#include "libtap.h" +#include "libnozzle.h" #include "libknet.h" #define KNET_RING_DEFPORT 50000 struct knet_cfg_eth { tap_t tap; int auto_mtu; knet_node_id_t node_id; }; struct knet_cfg_ring { knet_handle_t knet_h; int data_mtu; int base_port; }; struct knet_cfg { struct knet_cfg_eth cfg_eth; struct knet_cfg_ring cfg_ring; int active; struct knet_handle_crypto_cfg knet_handle_crypto_cfg; struct knet_cfg *next; }; struct knet_cfg_top { char *conffile; char *logfile; char *vty_ipv4; char *vty_ipv6; char *vty_port; struct knet_cfg *knet_cfg; }; struct knet_cfg *knet_get_iface(const char *name, const int create); void knet_destroy_iface(struct knet_cfg *knet_iface); extern struct knet_cfg_top knet_cfg_head; #endif diff --git a/kronosnetd/vty_cli_cmds.c b/kronosnetd/vty_cli_cmds.c index 5ed875e8..bf795e6e 100644 --- a/kronosnetd/vty_cli_cmds.c +++ b/kronosnetd/vty_cli_cmds.c @@ -1,2188 +1,2188 @@ /* * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "cfg.h" #include "etherfilter.h" #include "logging.h" -#include "libtap.h" +#include "libnozzle.h" #include "netutils.h" #include "vty.h" #include "vty_cli.h" #include "vty_cli_cmds.h" #include "vty_utils.h" #define KNET_VTY_MAX_MATCHES 64 #define KNET_VTY_MATCH_HELP 0 #define KNET_VTY_MATCH_EXEC 1 #define KNET_VTY_MATCH_EXPAND 2 #define CMDS_PARAM_NOMORE 0 #define CMDS_PARAM_KNET 1 #define CMDS_PARAM_IP 2 #define CMDS_PARAM_IP_PREFIX 3 #define CMDS_PARAM_IP_PORT 4 #define CMDS_PARAM_BOOL 5 #define CMDS_PARAM_INT 6 #define CMDS_PARAM_NODEID 7 #define CMDS_PARAM_NAME 8 #define CMDS_PARAM_MTU 9 #define CMDS_PARAM_CRYPTO_MODEL 10 #define CMDS_PARAM_CRYPTO_TYPE 11 #define CMDS_PARAM_HASH_TYPE 12 #define CMDS_PARAM_POLICY 13 #define CMDS_PARAM_LINK_ID 14 #define CMDS_PARAM_LINK_PRI 15 #define CMDS_PARAM_LINK_KEEPAL 16 #define CMDS_PARAM_LINK_HOLDTI 17 #define CMDS_PARAM_LINK_PONG 18 #define CMDS_PARAM_VTY_TIMEOUT 19 #define CMDS_PARAM_PMTU_FREQ 20 #define CMDS_PARAM_LINK_TRANSP 21 /* * CLI helper functions - menu/node stuff starts below */ /* * return 0 if we find a command in vty->line and cmd/len/no are set * return -1 if we cannot find a command. no can be trusted. cmd/len would be empty */ static int get_command(struct knet_vty *vty, char **cmd, int *cmdlen, int *cmdoffset, int *no) { int start = 0, idx; for (idx = 0; idx < vty->line_idx; idx++) { if (vty->line[idx] != ' ') break; } if (!strncmp(&vty->line[idx], "no ", 3)) { *no = 1; idx = idx + 3; for (; idx < vty->line_idx; idx++) { if (vty->line[idx] != ' ') break; } } else { *no = 0; } start = idx; if (start == vty->line_idx) return -1; *cmd = &vty->line[start]; *cmdoffset = start; for (idx = start; idx < vty->line_idx; idx++) { if (vty->line[idx] == ' ') break; } *cmdlen = idx - start; return 0; } /* * still not sure why I need to count backwards... */ static void get_n_word_from_end(struct knet_vty *vty, int n, char **word, int *wlen, int *woffset) { int widx; int idx, end, start; start = end = vty->line_idx; for (widx = 0; widx < n; widx++) { for (idx = start - 1; idx > 0; idx--) { if (vty->line[idx] != ' ') break; } end = idx; for (idx = end; idx > 0; idx--) { if (vty->line[idx-1] == ' ') break; } start = idx; } *wlen = (end - start) + 1; *word = &vty->line[start]; *woffset = start; } static int expected_params(const vty_param_t *params) { int idx = 0; while(params[idx].param != CMDS_PARAM_NOMORE) idx++; return idx; } static int count_words(struct knet_vty *vty, int offset) { int idx, widx = 0; int status = 0; for (idx = offset; idx < vty->line_idx; idx++) { if (vty->line[idx] == ' ') { status = 0; continue; } if ((vty->line[idx] != ' ') && (!status)) { widx++; status = 1; continue; } } return widx; } static int param_to_int(const char *param, int paramlen) { char buf[KNET_VTY_MAX_LINE]; memset(buf, 0, sizeof(buf)); memmove(buf, param, paramlen); return atoi(buf); } static int param_to_str(char *buf, int bufsize, const char *param, int paramlen) { if (bufsize < paramlen) return -1; memset(buf, 0, bufsize); memmove(buf, param, paramlen); return paramlen; } static const vty_node_cmds_t *get_cmds(struct knet_vty *vty, char **cmd, int *cmdlen, int *cmdoffset) { int no; const vty_node_cmds_t *cmds = knet_vty_nodes[vty->node].cmds; get_command(vty, cmd, cmdlen, cmdoffset, &no); if (no) cmds = knet_vty_nodes[vty->node].no_cmds; return cmds; } static int check_param(struct knet_vty *vty, const int paramtype, char *param, int paramlen) { int err = 0; char buf[KNET_VTY_MAX_LINE]; int tmp; struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; memset(buf, 0, sizeof(buf)); switch(paramtype) { case CMDS_PARAM_NOMORE: break; case CMDS_PARAM_KNET: if (paramlen >= IFNAMSIZ) { knet_vty_write(vty, "interface name too long%s", telnet_newline); err = -1; } break; case CMDS_PARAM_IP: break; case CMDS_PARAM_IP_PREFIX: break; case CMDS_PARAM_IP_PORT: tmp = param_to_int(param, paramlen); if ((tmp < 0) || (tmp > 65279)) { knet_vty_write(vty, "port number must be a value between 0 and 65279%s", telnet_newline); err = -1; } break; case CMDS_PARAM_BOOL: break; case CMDS_PARAM_INT: break; case CMDS_PARAM_NODEID: tmp = param_to_int(param, paramlen); if ((tmp < 0) || (tmp > 255)) { knet_vty_write(vty, "node id must be a value between 0 and 255%s", telnet_newline); err = -1; } break; case CMDS_PARAM_NAME: if (paramlen >= KNET_MAX_HOST_LEN) { knet_vty_write(vty, "name cannot exceed %d char in len%s", KNET_MAX_HOST_LEN - 1, telnet_newline); } break; case CMDS_PARAM_MTU: tmp = param_to_int(param, paramlen); if ((tmp < 576) || (tmp > 65536)) { knet_vty_write(vty, "mtu should be a value between 576 and 65536 (note: max value depends on the media)%s", telnet_newline); err = -1; } break; case CMDS_PARAM_PMTU_FREQ: tmp = param_to_int(param, paramlen); if ((tmp < 5) || (tmp > 600)) { knet_vty_write(vty, "PMTUd frequency should be a value between 5 and 600%s", telnet_newline); err = -1; } break; case CMDS_PARAM_CRYPTO_MODEL: param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); if (!strncmp("none", buf, 4)) break; if (!strncmp("nss", buf, 3)) break; knet_vty_write(vty, "unknown encryption model: %s. Supported: none/nss%s", param, telnet_newline); err = -1; break; case CMDS_PARAM_CRYPTO_TYPE: param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); if (!strncmp("none", buf, 4)) break; if (!strncmp("aes256", buf, 6)) break; if (!strncmp("aes192", buf, 6)) break; if (!strncmp("aes128", buf, 6)) break; if (!strncmp("3des", buf, 4)) break; knet_vty_write(vty, "unknown encryption method: %s. Supported: none/aes256/aes192/aes128/3des%s", param, telnet_newline); err = -1; break; case CMDS_PARAM_HASH_TYPE: param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); if (!strncmp("none", buf, 4)) break; if (!strncmp("md5", buf, 3)) break; if (!strncmp("sha1", buf, 4)) break; if (!strncmp("sha256", buf, 6)) break; if (!strncmp("sha384", buf, 6)) break; if (!strncmp("sha512", buf, 6)) break; knet_vty_write(vty, "unknown hash method: %s. Supported none/md5/sha1/sha256/sha384/sha512%s", param, telnet_newline); err = -1; break; case CMDS_PARAM_POLICY: param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); if (!strncmp("passive", buf, 7)) break; if (!strncmp("active", buf, 6)) break; if (!strncmp("round-robin", buf, 11)) break; knet_vty_write(vty, "unknown switching policy: %s. Supported passive/active/round-robin%s", param, telnet_newline); err = -1; break; case CMDS_PARAM_LINK_ID: tmp = param_to_int(param, paramlen); if ((tmp < 0) || (tmp > 7)) { knet_vty_write(vty, "link id should be a value between 0 and 7%s", telnet_newline); err = -1; } break; case CMDS_PARAM_LINK_TRANSP: param_to_str(buf, KNET_VTY_MAX_LINE, param, paramlen); if (knet_handle_get_transport_id_by_name(knet_iface->cfg_ring.knet_h, buf) == KNET_MAX_TRANSPORTS) { knet_vty_write(vty, "link transport is invalid%s", telnet_newline); err = -1; } break; case CMDS_PARAM_LINK_PRI: tmp = param_to_int(param, paramlen); if ((tmp < 0) || (tmp > 255)) { knet_vty_write(vty, "link priority should be a value between 0 and 256%s", telnet_newline); err = -1; } break; case CMDS_PARAM_LINK_KEEPAL: tmp = param_to_int(param, paramlen); if ((tmp <= 0) || (tmp > 60000)) { knet_vty_write(vty, "link keepalive should be a value between 0 and 60000 (milliseconds). Default: 1000%s", telnet_newline); err = -1; } break; case CMDS_PARAM_LINK_HOLDTI: tmp = param_to_int(param, paramlen); if ((tmp <= 0) || (tmp > 60000)) { knet_vty_write(vty, "link holdtimer should be a value between 0 and 60000 (milliseconds). Default: 5000%s", telnet_newline); err = -1; } break; case CMDS_PARAM_LINK_PONG: tmp = param_to_int(param, paramlen); if (tmp < 1) { knet_vty_write(vty, "pong_count must be a value between 0 and 255%s", telnet_newline); err = -1; } break; case CMDS_PARAM_VTY_TIMEOUT: tmp = param_to_int(param, paramlen); if ((tmp < 0) || (tmp > 3600)) { knet_vty_write(vty, "vty logout timeout should be a value between 0 (disabled) and 3600 seconds. Default: %d%s", KNET_VTY_CLI_TIMEOUT, telnet_newline); } break; default: knet_vty_write(vty, "CLI ERROR: unknown parameter type%s", telnet_newline); err = -1; break; } return err; } static void describe_param(struct knet_vty *vty, const int paramtype) { switch(paramtype) { case CMDS_PARAM_NOMORE: knet_vty_write(vty, "no more parameters%s", telnet_newline); break; case CMDS_PARAM_KNET: knet_vty_write(vty, "KNET_IFACE_NAME - interface name (max %d chars) eg: kronosnet0%s", IFNAMSIZ, telnet_newline); break; case CMDS_PARAM_IP: knet_vty_write(vty, "IP address - ipv4 or ipv6 address to add/remove%s", telnet_newline); break; case CMDS_PARAM_IP_PREFIX: knet_vty_write(vty, "IP prefix len (eg. 24, 64)%s", telnet_newline); break; case CMDS_PARAM_IP_PORT: knet_vty_write(vty, "base port (eg: %d) %s", KNET_RING_DEFPORT, telnet_newline); case CMDS_PARAM_BOOL: break; case CMDS_PARAM_INT: break; case CMDS_PARAM_NODEID: knet_vty_write(vty, "NODEID - unique identifier for this interface in this kronos network (value between 0 and 255)%s", telnet_newline); break; case CMDS_PARAM_NAME: knet_vty_write(vty, "NAME - unique name identifier for this entity (max %d chars)%s", KNET_MAX_HOST_LEN - 1, telnet_newline); break; case CMDS_PARAM_MTU: knet_vty_write(vty, "MTU - a value between 576 and 65536 (note: max value depends on the media)%s", telnet_newline); break; case CMDS_PARAM_PMTU_FREQ: knet_vty_write(vty, "PMTUd frequency - a value in seconds between 5 and 600 (default: 5)%s", telnet_newline); break; case CMDS_PARAM_CRYPTO_MODEL: knet_vty_write(vty, "MODEL - define encryption backend: none or nss%s", telnet_newline); break; case CMDS_PARAM_CRYPTO_TYPE: knet_vty_write(vty, "CRYPTO - define packets encryption method: none or aes256%s", telnet_newline); break; case CMDS_PARAM_HASH_TYPE: knet_vty_write(vty, "HASH - define packets hashing method: none/md5/sha1/sha256/sha384/sha512%s", telnet_newline); break; case CMDS_PARAM_POLICY: knet_vty_write(vty, "POLICY - define packets switching policy: passive/active/round-robin%s", telnet_newline); break; case CMDS_PARAM_LINK_ID: knet_vty_write(vty, "LINKID - specify the link identification number (0-7)%s", telnet_newline); break; case CMDS_PARAM_LINK_TRANSP: knet_vty_write(vty, "TRANSPORT - specify the link transport protocol (UDP/SCTP/..)%s", telnet_newline); break; case CMDS_PARAM_LINK_PRI: knet_vty_write(vty, "PRIORITY - specify the link priority for passive switching (0 to 255, default is 0). The higher value is preferred over lower value%s", telnet_newline); break; case CMDS_PARAM_LINK_KEEPAL: knet_vty_write(vty, "KEEPALIVE - specify the keepalive interval for this link (0 to 60000 milliseconds, default is 1000).%s", telnet_newline); break; case CMDS_PARAM_LINK_HOLDTI: knet_vty_write(vty, "HOLDTIME - specify how much time has to pass without connection before a link is considered dead (0 to 60000 milliseconds, default is 5000).%s", telnet_newline); break; case CMDS_PARAM_VTY_TIMEOUT: knet_vty_write(vty, "VTY_TIMEOUT - specify the number of seconds before a session is automatically closed.%s", telnet_newline); break; default: /* this should never happen */ knet_vty_write(vty, "CLI ERROR: unknown parameter type%s", telnet_newline); break; } } static void print_help(struct knet_vty *vty, const vty_node_cmds_t *cmds, int idx) { if ((idx < 0) || (cmds == NULL) || (cmds[idx].cmd == NULL)) return; if (cmds[idx].help != NULL) { knet_vty_write(vty, "%s\t%s%s", cmds[idx].cmd, cmds[idx].help, telnet_newline); } else { knet_vty_write(vty, "%s\tNo help available for this command%s", cmds[idx].cmd, telnet_newline); } } static int get_param(struct knet_vty *vty, int wanted_paranum, char **param, int *paramlen, int *paramoffset) { int eparams, tparams; const vty_param_t *params = (const vty_param_t *)vty->param; int paramstart = vty->paramoffset; eparams = expected_params(params); tparams = count_words(vty, paramstart); if (tparams > eparams) return -1; if (wanted_paranum == -1) { get_n_word_from_end(vty, 1, param, paramlen, paramoffset); return tparams; } if (tparams < wanted_paranum) return -1; get_n_word_from_end(vty, (tparams - wanted_paranum) + 1, param, paramlen, paramoffset); return tparams - wanted_paranum; } static int match_command(struct knet_vty *vty, const vty_node_cmds_t *cmds, char *cmd, int cmdlen, int cmdoffset, int mode) { int idx = 0, found = -1, paramoffset = 0, paramlen = 0, last_param = 0; char *param = NULL; int paramstart = cmdlen + cmdoffset; int matches[KNET_VTY_MAX_MATCHES]; memset(&matches, -1, sizeof(matches)); while ((cmds[idx].cmd != NULL) && (idx < KNET_VTY_MAX_MATCHES)) { if (!strncmp(cmds[idx].cmd, cmd, cmdlen)) { found++; matches[found] = idx; } idx++; } if (idx >= KNET_VTY_MAX_MATCHES) { knet_vty_write(vty, "Too many matches for this command%s", telnet_newline); return -1; } if (found < 0) { knet_vty_write(vty, "There is no such command%s", telnet_newline); return -1; } switch(mode) { case KNET_VTY_MATCH_HELP: if (found == 0) { if ((cmdoffset <= vty->cursor_pos) && (vty->cursor_pos <= paramstart)) { print_help(vty, cmds, matches[0]); break; } if (cmds[matches[0]].params != NULL) { vty->param = (void *)cmds[matches[0]].params; vty->paramoffset = paramstart; last_param = get_param(vty, -1, ¶m, ¶mlen, ¶moffset); if ((paramoffset <= vty->cursor_pos) && (vty->cursor_pos <= (paramoffset + paramlen))) last_param--; if (last_param >= CMDS_PARAM_NOMORE) { describe_param(vty, cmds[matches[0]].params[last_param].param); if (paramoffset > 0) check_param(vty, cmds[matches[0]].params[last_param].param, param, paramlen); } break; } } if (found >= 0) { idx = 0; while (matches[idx] >= 0) { print_help(vty, cmds, matches[idx]); idx++; } } break; case KNET_VTY_MATCH_EXEC: if (found == 0) { int exec = 0; if (cmds[matches[0]].params != NULL) { int eparams, tparams; eparams = expected_params(cmds[matches[0]].params); tparams = count_words(vty, paramstart); if (eparams != tparams) { exec = -1; idx = 0; knet_vty_write(vty, "Parameter required for this command:%s", telnet_newline); while(cmds[matches[0]].params[idx].param != CMDS_PARAM_NOMORE) { describe_param(vty, cmds[matches[0]].params[idx].param); idx++; } break; } idx = 0; vty->param = (void *)cmds[matches[0]].params; vty->paramoffset = paramstart; while(cmds[matches[0]].params[idx].param != CMDS_PARAM_NOMORE) { get_param(vty, idx + 1, ¶m, ¶mlen, ¶moffset); if (check_param(vty, cmds[matches[0]].params[idx].param, param, paramlen) < 0) { exec = -1; if (vty->filemode) return -1; } idx++; } } if (!exec) { if (cmds[matches[0]].params != NULL) { vty->param = (void *)cmds[matches[0]].params; vty->paramoffset = paramstart; } if (cmds[matches[0]].func != NULL) { return cmds[matches[0]].func(vty); } else { /* this will eventually disappear */ knet_vty_write(vty, "no fn associated to this command%s", telnet_newline); } } } if (found > 0) { knet_vty_write(vty, "Ambiguous command.%s", telnet_newline); } break; case KNET_VTY_MATCH_EXPAND: if (found == 0) { int cmdreallen; if (vty->cursor_pos > cmdoffset+cmdlen) /* complete param? */ break; cmdreallen = strlen(cmds[matches[0]].cmd); memset(vty->line + cmdoffset, 0, cmdlen); memmove(vty->line + cmdoffset, cmds[matches[0]].cmd, cmdreallen); vty->line[cmdreallen + cmdoffset] = ' '; vty->line_idx = cmdreallen + cmdoffset + 1; vty->cursor_pos = cmdreallen + cmdoffset + 1; } if (found > 0) { /* add completion to string base root */ int count = 0; idx = 0; while (matches[idx] >= 0) { knet_vty_write(vty, "%s\t\t", cmds[matches[idx]].cmd); idx++; count++; if (count == 4) { knet_vty_write(vty, "%s",telnet_newline); count = 0; } } knet_vty_write(vty, "%s",telnet_newline); } break; default: /* this should never really happen */ log_info("Unknown match mode"); break; } return found; } /* forward declarations */ /* common to almost all nodes */ static int knet_cmd_logout(struct knet_vty *vty); static int knet_cmd_who(struct knet_vty *vty); static int knet_cmd_exit_node(struct knet_vty *vty); static int knet_cmd_help(struct knet_vty *vty); /* root node */ static int knet_cmd_config(struct knet_vty *vty); /* config node */ static int knet_cmd_interface(struct knet_vty *vty); static int knet_cmd_no_interface(struct knet_vty *vty); static int knet_cmd_status(struct knet_vty *vty); static int knet_cmd_show_conf(struct knet_vty *vty); static int knet_cmd_write_conf(struct knet_vty *vty); /* interface node */ static int knet_cmd_mtu(struct knet_vty *vty); static int knet_cmd_no_mtu(struct knet_vty *vty); static int knet_cmd_ip(struct knet_vty *vty); static int knet_cmd_no_ip(struct knet_vty *vty); static int knet_cmd_peer(struct knet_vty *vty); static int knet_cmd_no_peer(struct knet_vty *vty); static int knet_cmd_start(struct knet_vty *vty); static int knet_cmd_stop(struct knet_vty *vty); static int knet_cmd_crypto(struct knet_vty *vty); static int knet_cmd_pmtufreq(struct knet_vty *vty); static int knet_cmd_no_pmtufreq(struct knet_vty *vty); /* peer node */ static int knet_cmd_link(struct knet_vty *vty); static int knet_cmd_no_link(struct knet_vty *vty); static int knet_cmd_switch_policy(struct knet_vty *vty); /* link node */ static int knet_cmd_link_pri(struct knet_vty *vty); static int knet_cmd_link_pong(struct knet_vty *vty); static int knet_cmd_link_timer(struct knet_vty *vty); /* vty node */ static int knet_cmd_vty(struct knet_vty *vty); static int knet_cmd_vty_timeout(struct knet_vty *vty); /* root node description */ vty_node_cmds_t root_cmds[] = { { "configure", "enter configuration mode", NULL, knet_cmd_config }, { "exit", "exit from CLI", NULL, knet_cmd_logout }, { "help", "display basic help", NULL, knet_cmd_help }, { "logout", "exit from CLI", NULL, knet_cmd_logout }, { "status", "display current network status", NULL, knet_cmd_status }, { "vty", "enter vty configuration mode", NULL, knet_cmd_vty }, { "who", "display users connected to CLI", NULL, knet_cmd_who }, { NULL, NULL, NULL, NULL }, }; /* config node description */ vty_param_t no_int_params[] = { { CMDS_PARAM_KNET }, { CMDS_PARAM_NOMORE }, }; vty_node_cmds_t no_config_cmds[] = { { "interface", "destroy kronosnet interface", no_int_params, knet_cmd_no_interface }, { NULL, NULL, NULL, NULL }, }; vty_param_t int_params[] = { { CMDS_PARAM_KNET }, { CMDS_PARAM_NODEID }, { CMDS_PARAM_IP_PORT }, { CMDS_PARAM_NOMORE }, }; vty_node_cmds_t config_cmds[] = { { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, { "interface", "configure kronosnet interface", int_params, knet_cmd_interface }, { "show", "show running config", NULL, knet_cmd_show_conf }, { "help", "display basic help", NULL, knet_cmd_help }, { "logout", "exit from CLI", NULL, knet_cmd_logout }, { "no", "revert command", NULL, NULL }, { "status", "display current network status", NULL, knet_cmd_status }, { "vty", "enter vty configuration mode", NULL, knet_cmd_vty }, { "who", "display users connected to CLI", NULL, knet_cmd_who }, { "write", "write current config to file", NULL, knet_cmd_write_conf }, { NULL, NULL, NULL, NULL }, }; /* interface node description */ vty_param_t ip_params[] = { { CMDS_PARAM_IP }, { CMDS_PARAM_IP_PREFIX }, { CMDS_PARAM_NOMORE }, }; vty_param_t peer_params[] = { { CMDS_PARAM_NAME }, { CMDS_PARAM_NODEID }, { CMDS_PARAM_NOMORE }, }; vty_node_cmds_t no_interface_cmds[] = { { "ip", "remove ip address", ip_params, knet_cmd_no_ip }, { "mtu", "revert to default MTU", NULL, knet_cmd_no_mtu }, { "pmtudfreq", "revert to default PMTUd frequency (default: 5)", NULL, knet_cmd_no_pmtufreq }, { "peer", "remove peer from this interface", peer_params, knet_cmd_no_peer }, { NULL, NULL, NULL, NULL }, }; vty_param_t mtu_params[] = { { CMDS_PARAM_MTU }, { CMDS_PARAM_NOMORE }, }; vty_param_t pmtu_params[] = { { CMDS_PARAM_PMTU_FREQ }, { CMDS_PARAM_NOMORE }, }; vty_param_t crypto_params[] = { { CMDS_PARAM_CRYPTO_MODEL }, { CMDS_PARAM_CRYPTO_TYPE }, { CMDS_PARAM_HASH_TYPE }, { CMDS_PARAM_NOMORE }, }; vty_node_cmds_t interface_cmds[] = { { "crypto", "enable crypto/hmac", crypto_params, knet_cmd_crypto }, { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, { "help", "display basic help", NULL, knet_cmd_help }, { "ip", "add ip address", ip_params, knet_cmd_ip }, { "logout", "exit from CLI", NULL, knet_cmd_logout }, { "mtu", "set mtu (default: auto)", mtu_params, knet_cmd_mtu }, { "pmtudfreq", "PMTUd frequency (default: 5)", pmtu_params, knet_cmd_pmtufreq }, { "no", "revert command", NULL, NULL }, { "peer", "add peer endpoint", peer_params, knet_cmd_peer }, { "show", "show running config", NULL, knet_cmd_show_conf }, { "start", "start forwarding engine", NULL, knet_cmd_start }, { "status", "display current network status", NULL, knet_cmd_status }, { "stop", "stop forwarding engine", NULL, knet_cmd_stop }, { "who", "display users connected to CLI", NULL, knet_cmd_who }, { "write", "write current config to file", NULL, knet_cmd_write_conf }, { NULL, NULL, NULL, NULL }, }; /* peer node description */ vty_param_t nolink_params[] = { { CMDS_PARAM_LINK_ID }, { CMDS_PARAM_NOMORE }, }; vty_param_t link_params[] = { { CMDS_PARAM_LINK_ID }, { CMDS_PARAM_IP }, { CMDS_PARAM_IP }, { CMDS_PARAM_LINK_TRANSP }, { CMDS_PARAM_NOMORE }, }; vty_param_t switch_params[] = { { CMDS_PARAM_POLICY }, { CMDS_PARAM_NOMORE }, }; vty_node_cmds_t no_peer_cmds[] = { { "link", "remove peer endpoint", nolink_params, knet_cmd_no_link}, { NULL, NULL, NULL, NULL }, }; vty_node_cmds_t peer_cmds[] = { { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, { "help", "display basic help", NULL, knet_cmd_help }, { "link", "add peer endpoint", link_params, knet_cmd_link }, { "logout", "exit from CLI", NULL, knet_cmd_logout }, { "no", "revert command", NULL, NULL }, { "show", "show running config", NULL, knet_cmd_show_conf }, { "status", "display current network status", NULL, knet_cmd_status }, { "switch-policy", "configure switching policy engine", switch_params, knet_cmd_switch_policy }, { "who", "display users connected to CLI", NULL, knet_cmd_who }, { "write", "write current config to file", NULL, knet_cmd_write_conf }, { NULL, NULL, NULL, NULL }, }; /* link node description */ vty_param_t link_pri_params[] = { { CMDS_PARAM_LINK_PRI }, { CMDS_PARAM_NOMORE }, }; vty_param_t link_timer_params[] = { { CMDS_PARAM_LINK_KEEPAL }, { CMDS_PARAM_LINK_HOLDTI }, { CMDS_PARAM_NOMORE }, }; vty_param_t pong_count_params[] = { { CMDS_PARAM_LINK_PONG }, { CMDS_PARAM_NOMORE }, }; vty_node_cmds_t link_cmds[] = { { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, { "help", "display basic help", NULL, knet_cmd_help }, { "logout", "exit from CLI", NULL, knet_cmd_logout }, { "no", "revert command", NULL, NULL }, { "pong_count", "set number of pongs to be received before a link is considered alive", pong_count_params, knet_cmd_link_pong }, { "priority", "set priority of this link for passive switching", link_pri_params, knet_cmd_link_pri }, { "show", "show running config", NULL, knet_cmd_show_conf }, { "status", "display current network status", NULL, knet_cmd_status }, { "timers", "set link keepalive and holdtime", link_timer_params, knet_cmd_link_timer }, { "who", "display users connected to CLI", NULL, knet_cmd_who }, { "write", "write current config to file", NULL, knet_cmd_write_conf }, { NULL, NULL, NULL, NULL }, }; vty_param_t vty_timeout_params[] = { { CMDS_PARAM_VTY_TIMEOUT }, { CMDS_PARAM_NOMORE }, }; vty_node_cmds_t vty_cmds[] = { { "exit", "exit configuration mode", NULL, knet_cmd_exit_node }, { "help", "display basic help", NULL, knet_cmd_help }, { "logout", "exit from CLI", NULL, knet_cmd_logout }, { "show", "show running config", NULL, knet_cmd_show_conf }, { "status", "display current network status", NULL, knet_cmd_status }, { "timeout", "set number of seconds before session is automatically closed", vty_timeout_params, knet_cmd_vty_timeout }, { "who", "display users connected to CLI", NULL, knet_cmd_who }, { "write", "write current config to file", NULL, knet_cmd_write_conf }, { NULL, NULL, NULL, NULL }, }; /* nodes */ vty_nodes_t knet_vty_nodes[] = { { NODE_ROOT, "knet", root_cmds, NULL }, { NODE_CONFIG, "config", config_cmds, no_config_cmds }, { NODE_INTERFACE, "iface", interface_cmds, no_interface_cmds }, { NODE_PEER, "peer", peer_cmds, no_peer_cmds }, { NODE_LINK, "link", link_cmds, NULL }, { NODE_VTY, "vty", vty_cmds, NULL }, { -1, NULL, NULL, NULL }, }; /* command execution */ /* vty */ static int knet_cmd_vty_timeout(struct knet_vty *vty) { int paramlen = 0, paramoffset = 0, timeout; char *param = NULL; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); timeout = param_to_int(param, paramlen); if ((vty->filemode) || (vty->prevnode == NODE_CONFIG)) { vty->vty_global_conf->idle_timeout = timeout; } vty->idle_timeout = timeout; return 0; } static int knet_cmd_vty(struct knet_vty *vty) { vty->prevnode = vty->node; vty->node = NODE_VTY; return 0; } /* links */ static int knet_cmd_link_pong(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0; char *param = NULL; uint8_t pong_count; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); pong_count = param_to_int(param, paramlen); knet_link_set_pong_count(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, pong_count); return 0; } static int knet_cmd_link_timer(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0; char *param = NULL; time_t keepalive, holdtime; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); keepalive = param_to_int(param, paramlen); get_param(vty, 2, ¶m, ¶mlen, ¶moffset); holdtime = param_to_int(param, paramlen); knet_link_set_ping_timers(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, keepalive, holdtime, 2048); return 0; } static int knet_cmd_link_pri(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0; char *param = NULL; uint8_t priority; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); priority = param_to_int(param, paramlen); if (knet_link_set_priority(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, priority)) { knet_vty_write(vty, "Error: unable to update link priority%s", telnet_newline); return -1; } return 0; } static int knet_cmd_no_link(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; struct knet_link_status status; int paramlen = 0, paramoffset = 0; char *param = NULL; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); vty->link_id = param_to_int(param, paramlen); knet_link_get_status(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, &status, sizeof(status)); if (status.enabled) { if (knet_link_set_enable(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, 0)) { knet_vty_write(vty, "Error: unable to update switching cache%s", telnet_newline); return -1; } knet_link_clear_config(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id); } return 0; } static int knet_cmd_link(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; struct knet_link_status status; int paramlen = 0, paramoffset = 0, err = 0; char *param = NULL; char src_ipaddr[KNET_MAX_HOST_LEN], src_port[KNET_MAX_PORT_LEN], dst_ipaddr[KNET_MAX_HOST_LEN], dst_port[KNET_MAX_PORT_LEN]; struct sockaddr_storage src_addr; struct sockaddr_storage dst_addr; struct sockaddr_storage *dst = NULL; char transport[10]; uint8_t transport_id; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); vty->link_id = param_to_int(param, paramlen); get_param(vty, 2, ¶m, ¶mlen, ¶moffset); param_to_str(src_ipaddr, KNET_MAX_HOST_LEN, param, paramlen); memset(src_port, 0, sizeof(src_port)); snprintf(src_port, KNET_MAX_PORT_LEN, "%d", knet_iface->cfg_ring.base_port + vty->host_id); get_param(vty, 3, ¶m, ¶mlen, ¶moffset); param_to_str(dst_ipaddr, KNET_MAX_HOST_LEN, param, paramlen); memset(dst_port, 0, sizeof(dst_port)); snprintf(dst_port, KNET_MAX_PORT_LEN, "%d", knet_iface->cfg_ring.base_port + knet_iface->cfg_eth.node_id); get_param(vty, 4, ¶m, ¶mlen, ¶moffset); param_to_str(transport, sizeof(transport), param, paramlen); transport_id = knet_handle_get_transport_id_by_name(knet_iface->cfg_ring.knet_h, transport); knet_link_get_status(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, &status, sizeof(status)); if (!status.enabled) { if (knet_strtoaddr(src_ipaddr, src_port, &src_addr, sizeof(struct sockaddr_storage)) != 0) { knet_vty_write(vty, "Error: unable to convert source ip addr to sockaddr!%s", telnet_newline); err = -1; goto out_clean; } if (!strncmp(dst_ipaddr, "dynamic", 7)) { dst = NULL; } else { if (knet_strtoaddr(dst_ipaddr, dst_port, &dst_addr, sizeof(struct sockaddr_storage)) != 0) { knet_vty_write(vty, "Error: unable to convert destination ip addr to sockaddr!%s", telnet_newline); err = -1; goto out_clean; } dst = &dst_addr; } knet_link_set_config(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, transport_id, &src_addr, dst, KNET_LINK_FLAG_TRAFFICHIPRIO); knet_link_set_ping_timers(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, 1000, 5000, 2048); knet_link_set_enable(knet_iface->cfg_ring.knet_h, vty->host_id, vty->link_id, 1); } vty->node = NODE_LINK; out_clean: return err; } static int knet_cmd_switch_policy(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0, err = 0; char *param = NULL; char policystr[16]; int policy = -1; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); param_to_str(policystr, sizeof(policystr), param, paramlen); if (!strncmp("passive", policystr, 7)) policy = KNET_LINK_POLICY_PASSIVE; if (!strncmp("active", policystr, 6)) policy = KNET_LINK_POLICY_ACTIVE; if (!strncmp("round-robin", policystr, 11)) policy = KNET_LINK_POLICY_RR; if (policy < 0) { knet_vty_write(vty, "Error: unknown switching policy method%s", telnet_newline); return -1; } err = knet_host_set_policy(knet_iface->cfg_ring.knet_h, vty->host_id, policy); if (err) knet_vty_write(vty, "Error: unable to set switching policy to %s%s", policystr, telnet_newline); return err; } /* * -1 on internal error * 0 host does not exist * 1 host exists */ static int knet_find_host(struct knet_vty *vty, const char *nodename, const knet_node_id_t requested_node_id) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int have_nodeid, have_name; knet_node_id_t node_id; char name[KNET_MAX_HOST_LEN]; have_nodeid = knet_host_get_id_by_host_name(knet_iface->cfg_ring.knet_h, nodename, &node_id); have_name = knet_host_get_name_by_host_id(knet_iface->cfg_ring.knet_h, requested_node_id, name); /* * host does not exist without a name */ if (have_name < 0) { return 0; } /* * internal error.. get out */ if (have_nodeid < 0) { knet_vty_write(vty, "Error: unable to query libknet for nodeid info%s", telnet_newline); return -1; } if ((!have_name) && (!have_nodeid)) { if (!strcmp(name, nodename) && (node_id == requested_node_id)) return 1; } knet_vty_write(vty, "Error: requested nodename or id already exists in libknet%s", telnet_newline); return -1; } static int knet_cmd_no_peer(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0, requested_node_id = 0, err = 0; char *param = NULL; char nodename[KNET_MAX_HOST_LEN]; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); param_to_str(nodename, sizeof(nodename), param, paramlen); get_param(vty, 2, ¶m, ¶mlen, ¶moffset); requested_node_id = param_to_int(param, paramlen); if (requested_node_id == knet_iface->cfg_eth.node_id) { knet_vty_write(vty, "Error: remote peer id cannot be the same as local id%s", telnet_newline); return -1; } err = knet_find_host(vty, nodename, requested_node_id); if (err < 0) goto out_clean; if (err != 1) { knet_vty_write(vty, "Error: peer not found in list%s", telnet_newline); goto out_clean; } err = knet_host_remove(knet_iface->cfg_ring.knet_h, requested_node_id); if (err < 0) { knet_vty_write(vty, "Error: unable to remove peer from current config%s", telnet_newline); goto out_clean; } out_clean: return err; } static int knet_cmd_peer(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0, requested_node_id = 0, err = 0, host = 0; char *param = NULL; char nodename[KNET_MAX_HOST_LEN]; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); param_to_str(nodename, sizeof(nodename), param, paramlen); get_param(vty, 2, ¶m, ¶mlen, ¶moffset); requested_node_id = param_to_int(param, paramlen); if (requested_node_id == knet_iface->cfg_eth.node_id) { knet_vty_write(vty, "Error: remote peer id cannot be the same as local id%s", telnet_newline); return -1; } err = knet_find_host(vty, nodename, requested_node_id); if (err < 0) goto out_clean; if (err == 0) { err = knet_host_add(knet_iface->cfg_ring.knet_h, requested_node_id); if (err < 0) { knet_vty_write(vty, "Error: unable to allocate memory for host struct!%s", telnet_newline); goto out_clean; } host = 1; knet_host_set_name(knet_iface->cfg_ring.knet_h, requested_node_id, nodename); knet_host_set_policy(knet_iface->cfg_ring.knet_h, requested_node_id, KNET_LINK_POLICY_PASSIVE); } vty->host_id = requested_node_id; vty->node = NODE_PEER; out_clean: if (err < 0) { if (host) knet_host_remove(knet_iface->cfg_ring.knet_h, requested_node_id); } return err; } static int knet_cmd_no_ip(struct knet_vty *vty) { int paramlen = 0, paramoffset = 0; char *param = NULL; char ipaddr[KNET_MAX_HOST_LEN], prefix[4]; struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; char *error_string = NULL; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); param_to_str(ipaddr, sizeof(ipaddr), param, paramlen); get_param(vty, 2, ¶m, ¶mlen, ¶moffset); param_to_str(prefix, sizeof(prefix), param, paramlen); if (tap_del_ip(knet_iface->cfg_eth.tap, ipaddr, prefix, &error_string) < 0) { knet_vty_write(vty, "Error: Unable to del ip addr %s/%s on device %s%s", ipaddr, prefix, tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); if (error_string) { knet_vty_write(vty, "(%s)%s", error_string, telnet_newline); free(error_string); } return -1; } return 0; } static int knet_cmd_ip(struct knet_vty *vty) { int paramlen = 0, paramoffset = 0; char *param = NULL; char ipaddr[512], prefix[4]; struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; char *error_string = NULL; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); param_to_str(ipaddr, sizeof(ipaddr), param, paramlen); get_param(vty, 2, ¶m, ¶mlen, ¶moffset); param_to_str(prefix, sizeof(prefix), param, paramlen); if (tap_add_ip(knet_iface->cfg_eth.tap, ipaddr, prefix, &error_string) < 0) { knet_vty_write(vty, "Error: Unable to set ip addr %s/%s on device %s%s", ipaddr, prefix, tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); if (error_string) { knet_vty_write(vty, "(%s)%s", error_string, telnet_newline); free(error_string); } return -1; } return 0; } static void knet_cmd_auto_mtu_notify(void *private_data, unsigned int data_mtu) { struct knet_cfg *knet_iface = (struct knet_cfg *)private_data; /* * 48 is the magic number! yes it is.. it's the magic number... */ knet_iface->cfg_ring.data_mtu = data_mtu - 48; if (!knet_iface->cfg_eth.auto_mtu) { int mtu = 0; mtu = tap_get_mtu(knet_iface->cfg_eth.tap); if (mtu < 0) { log_debug("Unable to get current MTU?"); } else { if (data_mtu < (unsigned int)mtu) { log_debug("Manually configured MTU (%d) is higher than automatically detected MTU (%d)", mtu, data_mtu); } } return; } if (tap_set_mtu(knet_iface->cfg_eth.tap, knet_iface->cfg_ring.data_mtu) < 0) { log_warn("Error: Unable to set requested mtu %d on device %s via mtu notify", knet_iface->cfg_ring.data_mtu, tap_get_name(knet_iface->cfg_eth.tap)); } else { log_info("Device %s new mtu: %d (via mtu notify)", tap_get_name(knet_iface->cfg_eth.tap), knet_iface->cfg_ring.data_mtu); } } static int knet_cmd_no_pmtufreq(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; if (knet_handle_pmtud_setfreq(knet_iface->cfg_ring.knet_h, 5) < 0) { knet_vty_write(vty, "Error: Unable to reset PMTUd frequency to 5 seconds on device %s%s", tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); return -1; } return 0; } static int knet_cmd_pmtufreq(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0, pmtufreq = 5; char *param = NULL; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); pmtufreq = param_to_int(param, paramlen); if (knet_handle_pmtud_setfreq(knet_iface->cfg_ring.knet_h, pmtufreq) < 0) { knet_vty_write(vty, "Error: Unable to set PMTUd frequency to %d seconds on device %s%s", pmtufreq, tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); return -1; } return 0; } static int knet_cmd_no_mtu(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; /* allow automatic updates of mtu */ knet_iface->cfg_eth.auto_mtu = 1; if (knet_iface->cfg_ring.data_mtu > 0) { if (tap_set_mtu(knet_iface->cfg_eth.tap, knet_iface->cfg_ring.data_mtu) < 0) { knet_iface->cfg_eth.auto_mtu = 0; knet_vty_write(vty, "Error: Unable to set auto detected mtu on device %s%s", tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); return -1; } } else { if (tap_reset_mtu(knet_iface->cfg_eth.tap) < 0) { knet_iface->cfg_eth.auto_mtu = 0; knet_vty_write(vty, "Error: Unable to reset mtu on device %s%s", tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); return -1; } } return 0; } static int knet_cmd_mtu(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0, expected_mtu = 0; char *param = NULL; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); expected_mtu = param_to_int(param, paramlen); /* disable mtu auto updates */ knet_iface->cfg_eth.auto_mtu = 0; if ((knet_iface->cfg_ring.data_mtu) && (expected_mtu > knet_iface->cfg_ring.data_mtu)) { knet_vty_write(vty, "WARNING: Manually configured MTU (%d) is higher than automatically detected MTU (%d)%s", expected_mtu, knet_iface->cfg_ring.data_mtu, telnet_newline); } if (tap_set_mtu(knet_iface->cfg_eth.tap, expected_mtu) < 0) { knet_iface->cfg_eth.auto_mtu = 1; knet_vty_write(vty, "Error: Unable to set requested mtu %d on device %s%s", expected_mtu, tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); return -1; } return 0; } static int knet_cmd_stop(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; char *error_down = NULL, *error_postdown = NULL; int err = 0; err = tap_set_down(knet_iface->cfg_eth.tap, &error_down, &error_postdown); if (err < 0) { knet_vty_write(vty, "Error: Unable to set interface %s down!%s", tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); } else { if (knet_iface->cfg_ring.knet_h) knet_handle_setfwd(knet_iface->cfg_ring.knet_h, 0); knet_iface->active = 0; } if (error_down) { knet_vty_write(vty, "down script output:%s(%s)%s", telnet_newline, error_down, telnet_newline); free(error_down); } if (error_postdown) { knet_vty_write(vty, "post-down script output:%s(%s)%s", telnet_newline, error_postdown, telnet_newline); free(error_postdown); } return err; } static int knet_cmd_crypto(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; int paramlen = 0, paramoffset = 0; char *param = NULL; int err = 0; struct knet_handle_crypto_cfg knet_handle_crypto_cfg_new; int fd = -1; char keyfile[PATH_MAX]; struct stat sb; if (knet_iface->active) { knet_vty_write(vty, "Error: Unable to activate encryption while interface is active%s", telnet_newline); return -1; } memset(&knet_handle_crypto_cfg_new, 0, sizeof(struct knet_handle_crypto_cfg)); get_param(vty, 1, ¶m, ¶mlen, ¶moffset); param_to_str(knet_handle_crypto_cfg_new.crypto_model, sizeof(knet_handle_crypto_cfg_new.crypto_model), param, paramlen); get_param(vty, 2, ¶m, ¶mlen, ¶moffset); param_to_str(knet_handle_crypto_cfg_new.crypto_cipher_type, sizeof(knet_handle_crypto_cfg_new.crypto_cipher_type), param, paramlen); get_param(vty, 3, ¶m, ¶mlen, ¶moffset); param_to_str(knet_handle_crypto_cfg_new.crypto_hash_type, sizeof(knet_handle_crypto_cfg_new.crypto_hash_type), param, paramlen); if ((!strncmp("none", knet_handle_crypto_cfg_new.crypto_model, 4)) || ((!strncmp("none", knet_handle_crypto_cfg_new.crypto_cipher_type, 4)) && ((!strncmp("none", knet_handle_crypto_cfg_new.crypto_hash_type, 4))))) goto no_key; memset(keyfile, 0, PATH_MAX); snprintf(keyfile, PATH_MAX - 1, DEFAULT_CONFIG_DIR "/cryptokeys.d/%s", tap_get_name(knet_iface->cfg_eth.tap)); fd = open(keyfile, O_RDONLY); if (fd < 0) { knet_vty_write(vty, "Error: Unable to open security key: %s%s", keyfile, telnet_newline); err = -1; return -1; } if (fstat(fd, &sb)) { knet_vty_write(vty, "Error: Unable to verify security key: %s%s", keyfile, telnet_newline); goto key_error; } if (!S_ISREG(sb.st_mode)) { knet_vty_write(vty, "Error: Key %s does not appear to be a regular file%s", keyfile, telnet_newline); goto key_error; } knet_handle_crypto_cfg_new.private_key_len = (unsigned int)sb.st_size; if ((knet_handle_crypto_cfg_new.private_key_len < KNET_MIN_KEY_LEN) || (knet_handle_crypto_cfg_new.private_key_len > KNET_MAX_KEY_LEN)) { knet_vty_write(vty, "Error: Key %s is %u long. Must be %d <= key_len <= %d%s", keyfile, knet_handle_crypto_cfg_new.private_key_len, KNET_MIN_KEY_LEN, KNET_MAX_KEY_LEN, telnet_newline); goto key_error; } if (((sb.st_mode & S_IRWXU) != S_IRUSR) || (sb.st_mode & S_IRWXG) || (sb.st_mode & S_IRWXO)) { knet_vty_write(vty, "Error: Key %s does not have the correct permission (must be user read-only)%s", keyfile, telnet_newline); goto key_error; } if (read(fd, &knet_handle_crypto_cfg_new.private_key, knet_handle_crypto_cfg_new.private_key_len) != knet_handle_crypto_cfg_new.private_key_len) { knet_vty_write(vty, "Error: Unable to read key %s%s", keyfile, telnet_newline); goto key_error; } close(fd); no_key: err = knet_handle_crypto(knet_iface->cfg_ring.knet_h, &knet_handle_crypto_cfg_new); if (!err) { memmove(&knet_iface->knet_handle_crypto_cfg, &knet_handle_crypto_cfg_new, sizeof(struct knet_handle_crypto_cfg)); } else { knet_vty_write(vty, "Error: Unable to initialize crypto module%s", telnet_newline); } return err; key_error: close(fd); return -1; } static int knet_cmd_start(struct knet_vty *vty) { struct knet_cfg *knet_iface = (struct knet_cfg *)vty->iface; char *error_preup = NULL, *error_up = NULL; int err = 0; err = tap_set_up(knet_iface->cfg_eth.tap, &error_preup, &error_up); if (err < 0) { knet_vty_write(vty, "Error: Unable to set interface %s up!%s", tap_get_name(knet_iface->cfg_eth.tap), telnet_newline); knet_handle_setfwd(knet_iface->cfg_ring.knet_h, 0); } else { knet_handle_setfwd(knet_iface->cfg_ring.knet_h, 1); knet_iface->active = 1; } if (error_preup) { knet_vty_write(vty, "pre-up script output:%s(%s)%s", telnet_newline, error_preup, telnet_newline); free(error_preup); } if (error_up) { knet_vty_write(vty, "up script output:%s(%s)%s", telnet_newline, error_up, telnet_newline); free(error_up); } return err; } static int knet_cmd_no_interface(struct knet_vty *vty) { int err = 0, paramlen = 0, paramoffset = 0; char *param = NULL; char device[IFNAMSIZ]; struct knet_cfg *knet_iface = NULL; char *ip_list = NULL; int ip_list_entries = 0, offset = 0; size_t j, i; char *error_string = NULL; 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; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); param_to_str(device, IFNAMSIZ, param, paramlen); knet_iface = knet_get_iface(device, 0); if (!knet_iface) { knet_vty_write(vty, "Error: Unable to find requested interface%s", telnet_newline); return -1; } vty->iface = (void *)knet_iface; /* * disable PTMUd notification before shutting down the tap device */ knet_handle_enable_pmtud_notify(knet_iface->cfg_ring.knet_h, NULL, NULL); tap_get_ips(knet_iface->cfg_eth.tap, &ip_list, &ip_list_entries); if ((ip_list) && (ip_list_entries > 0)) { for (i = 1; i <= (size_t)ip_list_entries; i++) { tap_del_ip(knet_iface->cfg_eth.tap, ip_list + offset, ip_list + offset + strlen(ip_list + offset) + 1, &error_string); if (error_string) { free(error_string); error_string = NULL; } offset = offset + strlen(ip_list) + 1; offset = offset + strlen(ip_list + offset) + 1; } free(ip_list); ip_list = NULL; ip_list_entries = 0; } knet_host_get_host_list(knet_iface->cfg_ring.knet_h, host_ids, &host_ids_entries); for (j = 0; j < host_ids_entries; j++) { knet_link_get_link_list(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids, &link_ids_entries); for (i = 0; i < link_ids_entries; i++) { knet_link_set_enable(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], 0); knet_link_clear_config(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i]); } knet_host_remove(knet_iface->cfg_ring.knet_h, host_ids[j]); } knet_cmd_stop(vty); if (knet_iface->cfg_ring.knet_h) { knet_handle_free(knet_iface->cfg_ring.knet_h); knet_iface->cfg_ring.knet_h = NULL; } if (knet_iface->cfg_eth.tap) tap_close(knet_iface->cfg_eth.tap); if (knet_iface) knet_destroy_iface(knet_iface); return err; } static void sock_notify_fn(void *private_data, int datafd, int8_t chan, uint8_t tx_rx, int error, int errorno) { struct knet_vty *vty = (struct knet_vty *)private_data; knet_vty_write(vty, "Error: received sock notify, datafd: %d channel: %d direction: %u error: %d errno: %d (%s)%s", datafd, chan, tx_rx, error, errorno, strerror(errorno), telnet_newline); } static int knet_cmd_interface(struct knet_vty *vty) { int err = 0, paramlen = 0, paramoffset = 0, found = 0, requested_id, tapfd; uint16_t baseport; uint8_t *bport = (uint8_t *)&baseport; char *param = NULL; char device[IFNAMSIZ]; char mac[18]; struct knet_cfg *knet_iface = NULL; int8_t channel = 0; get_param(vty, 1, ¶m, ¶mlen, ¶moffset); param_to_str(device, IFNAMSIZ, param, paramlen); get_param(vty, 2, ¶m, ¶mlen, ¶moffset); requested_id = param_to_int(param, paramlen); get_param(vty, 3, ¶m, ¶mlen, ¶moffset); baseport = param_to_int(param, paramlen); knet_iface = knet_get_iface(device, 1); if (!knet_iface) { knet_vty_write(vty, "Error: Unable to allocate memory for config structures%s", telnet_newline); return -1; } if (knet_iface->cfg_eth.tap) { found = 1; goto tap_found; } if (!knet_iface->cfg_eth.tap) knet_iface->cfg_eth.tap = tap_open(device, IFNAMSIZ, DEFAULT_CONFIG_DIR); if ((!knet_iface->cfg_eth.tap) && (errno == EBUSY)) { knet_vty_write(vty, "Error: interface %s seems to exist in the system%s", device, telnet_newline); err = -1; goto out_clean; } if (!knet_iface->cfg_eth.tap) { knet_vty_write(vty, "Error: Unable to create %s system tap device%s", device, telnet_newline); err = -1; goto out_clean; } tap_found: if (knet_iface->cfg_ring.knet_h) goto knet_found; knet_iface->cfg_ring.base_port = baseport; tapfd = tap_get_fd(knet_iface->cfg_eth.tap); knet_iface->cfg_ring.knet_h = knet_handle_new(requested_id, vty->logfd, vty->loglevel); if (!knet_iface->cfg_ring.knet_h) { knet_vty_write(vty, "Error: Unable to create ring handle for device %s%s", device, telnet_newline); err = -1; goto out_clean; } if (knet_handle_enable_sock_notify(knet_iface->cfg_ring.knet_h, &vty, sock_notify_fn)) { knet_vty_write(vty, "Error: Unable to add sock notify callback to to knet_handle %s%s", strerror(errno), telnet_newline); err = -1; goto out_clean; } if (knet_handle_add_datafd(knet_iface->cfg_ring.knet_h, &tapfd, &channel) < 0) { knet_vty_write(vty, "Error: Unable to add tapfd to knet_handle %s%s", strerror(errno), telnet_newline); err = -1; goto out_clean; } knet_handle_enable_filter(knet_iface->cfg_ring.knet_h, NULL, ether_host_filter_fn); if (knet_handle_enable_pmtud_notify(knet_iface->cfg_ring.knet_h, knet_iface, knet_cmd_auto_mtu_notify) < 0) { knet_vty_write(vty, "Error: Unable to configure auto mtu notification for device %s%s", device, telnet_newline); err = -1; goto out_clean; } knet_iface->cfg_eth.auto_mtu = 1; /* * make this configurable */ knet_handle_pmtud_setfreq(knet_iface->cfg_ring.knet_h, 5); knet_found: if (found) { if (requested_id == knet_iface->cfg_eth.node_id) goto out_found; knet_vty_write(vty, "Error: no interface %s with nodeid %d found%s", device, requested_id, telnet_newline); goto out_clean; } else { knet_iface->cfg_eth.node_id = requested_id; } baseport = htons(baseport); memset(&mac, 0, sizeof(mac)); snprintf(mac, sizeof(mac) - 1, "54:54:%x:%x:0:%x", bport[0], bport[1], knet_iface->cfg_eth.node_id); if (tap_set_mac(knet_iface->cfg_eth.tap, mac) < 0) { knet_vty_write(vty, "Error: Unable to set mac address %s on device %s%s", mac, device, telnet_newline); err = -1; goto out_clean; } out_found: vty->node = NODE_INTERFACE; vty->iface = (void *)knet_iface; out_clean: if (err) { if (knet_iface->cfg_ring.knet_h) knet_handle_free(knet_iface->cfg_ring.knet_h); if (knet_iface->cfg_eth.tap) tap_close(knet_iface->cfg_eth.tap); knet_destroy_iface(knet_iface); } return err; } static int knet_cmd_exit_node(struct knet_vty *vty) { knet_vty_exit_node(vty); return 0; } static int knet_cmd_status(struct knet_vty *vty) { size_t i, j; struct knet_cfg *knet_iface = knet_cfg_head.knet_cfg; struct knet_link_status status; const char *nl = telnet_newline; struct timespec now; char nodename[KNET_MAX_HOST_LEN]; 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; uint8_t policy; clock_gettime(CLOCK_MONOTONIC, &now); knet_vty_write(vty, "Current knet status%s", nl); knet_vty_write(vty, "-------------------%s", nl); while (knet_iface != NULL) { knet_vty_write(vty, "interface %s (active: %d)%s", tap_get_name(knet_iface->cfg_eth.tap), knet_iface->active, nl); knet_host_get_host_list(knet_iface->cfg_ring.knet_h, host_ids, &host_ids_entries); for (j = 0; j < host_ids_entries; j++) { knet_host_get_name_by_host_id(knet_iface->cfg_ring.knet_h, host_ids[j], nodename); knet_vty_write(vty, " peer %s ", nodename); knet_host_get_policy(knet_iface->cfg_ring.knet_h, host_ids[j], &policy); switch (policy) { case KNET_LINK_POLICY_PASSIVE: knet_vty_write(vty, "(passive)%s", nl); break; case KNET_LINK_POLICY_ACTIVE: knet_vty_write(vty, "(active)%s", nl); break; case KNET_LINK_POLICY_RR: knet_vty_write(vty, "(round-robin)%s", nl); break; } knet_link_get_link_list(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids, &link_ids_entries); for (i = 0; i < link_ids_entries; i++) { uint8_t dynamic, transport; const char *transport_name; struct sockaddr_storage src_addr; struct sockaddr_storage dst_addr; uint64_t flags; if (!knet_link_get_config(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &transport, &src_addr, &dst_addr, &dynamic, &flags)) { transport_name = knet_handle_get_transport_name_by_id(knet_iface->cfg_ring.knet_h, transport); knet_link_get_status(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &status, sizeof(status)); if (status.enabled == 1) { if (dynamic) { knet_vty_write(vty, " link %s dynamic (%s/connected: %d)%s", status.src_ipaddr, transport_name, status.connected, nl); } else { knet_vty_write(vty, " link %s %s (%s/connected: %d)%s", status.src_ipaddr, status.dst_ipaddr, transport_name, status.connected, nl); } if (status.connected) { knet_vty_write(vty, " average latency: %llu us%s", status.latency, nl); if ((dynamic) && (status.dynconnected)) { knet_vty_write(vty, " source ip: %s%s", status.dst_ipaddr, nl); } } else { knet_vty_write(vty, " last heard: "); if (status.pong_last.tv_sec) { knet_vty_write(vty, "%lu s ago%s", (long unsigned int)now.tv_sec - status.pong_last.tv_sec, nl); } else { knet_vty_write(vty, "never%s", nl); } } } } } } knet_iface = knet_iface->next; } return 0; } static int knet_cmd_print_conf(struct knet_vty *vty) { size_t i, j; struct knet_cfg *knet_iface = knet_cfg_head.knet_cfg; struct knet_link_status status; const char *nl = telnet_newline; char *ip_list = NULL; int ip_list_entries = 0; 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; char nodename[KNET_MAX_HOST_LEN]; uint8_t policy; unsigned int pmtudfreq = 0; if (vty->filemode) nl = file_newline; knet_vty_write(vty, "configure%s", nl); knet_vty_write(vty, " vty%s", nl); knet_vty_write(vty, " timeout %d%s", vty->idle_timeout, nl); knet_vty_write(vty, " exit%s", nl); while (knet_iface != NULL) { knet_vty_write(vty, " interface %s %d %d%s", tap_get_name(knet_iface->cfg_eth.tap), knet_iface->cfg_eth.node_id, knet_iface->cfg_ring.base_port, nl); if (!knet_iface->cfg_eth.auto_mtu) knet_vty_write(vty, " mtu %d%s", tap_get_mtu(knet_iface->cfg_eth.tap), nl); knet_handle_pmtud_getfreq(knet_iface->cfg_ring.knet_h, &pmtudfreq); if ((pmtudfreq > 0) && (pmtudfreq != 5)) knet_vty_write(vty, " pmtudfreq %u%s", pmtudfreq, nl); tap_get_ips(knet_iface->cfg_eth.tap, &ip_list, &ip_list_entries); if ((ip_list) && (ip_list_entries > 0)) { char *ipaddr = NULL, *prefix = NULL, *next = ip_list; for (i = 1; i <= (size_t)ip_list_entries; i++) { ipaddr = next; prefix = ipaddr + strlen(ipaddr) + 1; next = prefix + strlen(prefix) + 1; knet_vty_write(vty, " ip %s %s%s", ipaddr, prefix, nl); } free(ip_list); ip_list = NULL; ip_list_entries = 0; } knet_vty_write(vty, " crypto %s %s %s%s", knet_iface->knet_handle_crypto_cfg.crypto_model, knet_iface->knet_handle_crypto_cfg.crypto_cipher_type, knet_iface->knet_handle_crypto_cfg.crypto_hash_type, nl); knet_host_get_host_list(knet_iface->cfg_ring.knet_h, host_ids, &host_ids_entries); for (j = 0; j < host_ids_entries; j++) { knet_host_get_name_by_host_id(knet_iface->cfg_ring.knet_h, host_ids[j], nodename); knet_vty_write(vty, " peer %s %u%s", nodename, host_ids[j], nl); knet_host_get_policy(knet_iface->cfg_ring.knet_h, host_ids[j], &policy); switch (policy) { case KNET_LINK_POLICY_PASSIVE: knet_vty_write(vty, " switch-policy passive%s", nl); break; case KNET_LINK_POLICY_ACTIVE: knet_vty_write(vty, " switch-policy active%s", nl); break; case KNET_LINK_POLICY_RR: knet_vty_write(vty, " switch-policy round-robin%s", nl); break; } knet_link_get_link_list(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids, &link_ids_entries); for (i = 0; i < link_ids_entries; i++) { uint8_t dynamic, transport; const char *transport_name; struct sockaddr_storage src_addr; struct sockaddr_storage dst_addr; uint64_t flags; if (!knet_link_get_config(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &transport, &src_addr, &dst_addr, &dynamic, &flags)) { transport_name = knet_handle_get_transport_name_by_id(knet_iface->cfg_ring.knet_h, transport); knet_link_get_status(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &status, sizeof(status)); if (status.enabled == 1) { uint8_t priority, pong_count; unsigned int precision; time_t interval, timeout; if (dynamic) { knet_vty_write(vty, " link %d %s dynamic %s%s", link_ids[i], status.src_ipaddr, transport_name, nl); } else { knet_vty_write(vty, " link %d %s %s %s%s", link_ids[i], status.src_ipaddr, status.dst_ipaddr, transport_name, nl); } knet_link_get_pong_count(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &pong_count); knet_vty_write(vty, " pong_count %u%s", pong_count, nl); knet_link_get_ping_timers(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &interval, &timeout, &precision); knet_vty_write(vty, " timers %llu %llu%s", (unsigned long long)interval, (unsigned long long)timeout, nl); knet_link_get_priority(knet_iface->cfg_ring.knet_h, host_ids[j], link_ids[i], &priority); knet_vty_write(vty, " priority %u%s", priority, nl); /* print link properties */ knet_vty_write(vty, " exit%s", nl); } } } knet_vty_write(vty, " exit%s", nl); } if (knet_iface->active) knet_vty_write(vty, " start%s", nl); knet_vty_write(vty, " exit%s", nl); knet_iface = knet_iface->next; } knet_vty_write(vty, " exit%sexit%s", nl, nl); return 0; } static int knet_cmd_show_conf(struct knet_vty *vty) { return knet_cmd_print_conf(vty); } static int knet_cmd_write_conf(struct knet_vty *vty) { int fd = 1, vty_sock, err = 0, backup = 1; char tempfile[PATH_MAX]; memset(tempfile, 0, sizeof(tempfile)); snprintf(tempfile, sizeof(tempfile), "%s.sav", knet_cfg_head.conffile); err = rename(knet_cfg_head.conffile, tempfile); if ((err < 0) && (errno != ENOENT)) { knet_vty_write(vty, "Unable to create backup config file %s %s", tempfile, telnet_newline); return -1; } if ((err < 0) && (errno == ENOENT)) backup = 0; fd = open(knet_cfg_head.conffile, O_RDWR | O_CREAT | O_EXCL | O_TRUNC, S_IRUSR | S_IWUSR); if (fd < 0) { knet_vty_write(vty, "Error unable to open file%s", telnet_newline); return -1; } vty_sock = vty->vty_sock; vty->vty_sock = fd; vty->filemode = 1; knet_cmd_print_conf(vty); vty->vty_sock = vty_sock; vty->filemode = 0; close(fd); knet_vty_write(vty, "Configuration saved to %s%s", knet_cfg_head.conffile, telnet_newline); if (backup) knet_vty_write(vty, "Old configuration file has been stored in %s%s", tempfile, telnet_newline); return err; } static int knet_cmd_config(struct knet_vty *vty) { int err = 0; if (!vty->user_can_enable) { knet_vty_write(vty, "Error: user %s does not have enough privileges to perform config operations%s", vty->username, telnet_newline); return -1; } pthread_mutex_lock(&knet_vty_mutex); if (knet_vty_config >= 0) { knet_vty_write(vty, "Error: configuration is currently locked by user %s on vty(%d). Try again later%s", vty->username, knet_vty_config, telnet_newline); err = -1; goto out_clean; } vty->node = NODE_CONFIG; knet_vty_config = vty->conn_num; out_clean: pthread_mutex_unlock(&knet_vty_mutex); return err; } static int knet_cmd_logout(struct knet_vty *vty) { vty->got_epipe = 1; return 0; } static int knet_cmd_who(struct knet_vty *vty) { int conn_index; pthread_mutex_lock(&knet_vty_mutex); for(conn_index = 0; conn_index < KNET_VTY_TOTAL_MAX_CONN; conn_index++) { if (knet_vtys[conn_index].active) { knet_vty_write(vty, "User %s connected on vty(%d) from %s%s", knet_vtys[conn_index].username, knet_vtys[conn_index].conn_num, knet_vtys[conn_index].ip, telnet_newline); } } pthread_mutex_unlock(&knet_vty_mutex); return 0; } static int knet_cmd_help(struct knet_vty *vty) { knet_vty_write(vty, PACKAGE "d VTY provides advanced help feature.%s%s" "When you need help, anytime at the command line please press '?'.%s%s" "If nothing matches, the help list will be empty and you must backup%s" " until entering a '?' shows the available options.%s", telnet_newline, telnet_newline, telnet_newline, telnet_newline, telnet_newline, telnet_newline); return 0; } /* exported API to vty_cli.c */ int knet_vty_execute_cmd(struct knet_vty *vty) { const vty_node_cmds_t *cmds = NULL; char *cmd = NULL; int cmdlen = 0; int cmdoffset = 0; if (knet_vty_is_line_empty(vty)) return 0; cmds = get_cmds(vty, &cmd, &cmdlen, &cmdoffset); /* this will eventually disappear. keep it as safeguard for now */ if (cmds == NULL) { knet_vty_write(vty, "No commands associated to this node%s", telnet_newline); return 0; } return match_command(vty, cmds, cmd, cmdlen, cmdoffset, KNET_VTY_MATCH_EXEC); } void knet_close_down(void) { struct knet_vty *vty = &knet_vtys[0]; int err, loop = 0; vty->node = NODE_CONFIG; vty->vty_sock = 1; vty->user_can_enable = 1; vty->filemode = 1; vty->got_epipe = 0; while ((knet_cfg_head.knet_cfg) && (loop < 10)) { memset(vty->line, 0, sizeof(vty->line)); snprintf(vty->line, sizeof(vty->line) - 1, "no interface %s", tap_get_name(knet_cfg_head.knet_cfg->cfg_eth.tap)); vty->line_idx = strlen(vty->line); err = knet_vty_execute_cmd(vty); if (err != 0) { log_error("error shutting down: %s", vty->line); break; } loop++; } } int knet_read_conf(void) { int err = 0, len = 0, line = 0; struct knet_vty *vty = &knet_vtys[0]; FILE *file = NULL; file = fopen(knet_cfg_head.conffile, "r"); if ((file == NULL) && (errno != ENOENT)) { log_error("Unable to open config file for reading %s", knet_cfg_head.conffile); return -1; } if ((file == NULL) && (errno == ENOENT)) { log_info("Configuration file %s not found, starting with default empty config", knet_cfg_head.conffile); return 0; } vty->vty_sock = 1; vty->user_can_enable = 1; vty->filemode = 1; while(fgets(vty->line, sizeof(vty->line), file) != NULL) { line++; len = strlen(vty->line) - 1; memset(&vty->line[len], 0, 1); vty->line_idx = len; err = knet_vty_execute_cmd(vty); if (err != 0) { log_error("line[%d]: %s", line, vty->line); break; } } fclose(file); memset(vty, 0, sizeof(*vty)); return err; } void knet_vty_help(struct knet_vty *vty) { int idx = 0; const vty_node_cmds_t *cmds = NULL; char *cmd = NULL; int cmdlen = 0; int cmdoffset = 0; cmds = get_cmds(vty, &cmd, &cmdlen, &cmdoffset); /* this will eventually disappear. keep it as safeguard for now */ if (cmds == NULL) { knet_vty_write(vty, "No commands associated to this node%s", telnet_newline); return; } if (knet_vty_is_line_empty(vty) || cmd == NULL) { while (cmds[idx].cmd != NULL) { print_help(vty, cmds, idx); idx++; } return; } match_command(vty, cmds, cmd, cmdlen, cmdoffset, KNET_VTY_MATCH_HELP); } void knet_vty_tab_completion(struct knet_vty *vty) { const vty_node_cmds_t *cmds = NULL; char *cmd = NULL; int cmdlen = 0; int cmdoffset = 0; if (knet_vty_is_line_empty(vty)) return; knet_vty_write(vty, "%s", telnet_newline); cmds = get_cmds(vty, &cmd, &cmdlen, &cmdoffset); /* this will eventually disappear. keep it as safeguard for now */ if (cmds == NULL) { knet_vty_write(vty, "No commands associated to this node%s", telnet_newline); return; } match_command(vty, cmds, cmd, cmdlen, cmdoffset, KNET_VTY_MATCH_EXPAND); knet_vty_prompt(vty); knet_vty_write(vty, "%s", vty->line); } diff --git a/libknet/compress.c b/libknet/compress.c index 536b0439..8af7beff 100644 --- a/libknet/compress.c +++ b/libknet/compress.c @@ -1,481 +1,483 @@ /* * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, 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. */ -compress_model_t compress_modules_cmds[] = { +static compress_model_t compress_modules_cmds[] = { { "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 }, { NULL, 255, 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; 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; } 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; } 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, "Initizializing 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 (compress_modules_cmds[idx].model_name != NULL) { if ((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) && (idx < KNET_MAX_COMPRESS_METHODS)) { 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_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/crypto.c b/libknet/crypto.c index 43c21994..172bac7b 100644 --- a/libknet/crypto.c +++ b/libknet/crypto.c @@ -1,212 +1,214 @@ /* * Copyright (C) 2012-2018 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, 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 */ -crypto_model_t crypto_modules_cmds[] = { +static crypto_model_t crypto_modules_cmds[] = { { "nss", WITH_CRYPTO_NSS, 0, NULL }, { "openssl", WITH_CRYPTO_OPENSSL, 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->model].ops->crypt(knet_h, 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->model].ops->cryptv(knet_h, 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) { return crypto_modules_cmds[knet_h->crypto_instance->model].ops->decrypt(knet_h, buf_in, buf_in_len, buf_out, buf_out_len); } int crypto_init( knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) { int savederrno = 0; int model = 0; 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; log_err(knet_h, KNET_SUB_CRYPTO, "Unable to load %s lib", crypto_modules_cmds[model].model_name); goto out_err; } if (crypto_modules_cmds[model].ops->abi_ver != KNET_CRYPTO_MODEL_ABI) { 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); savederrno = EINVAL; goto out_err; } crypto_modules_cmds[model].loaded = 1; } log_debug(knet_h, KNET_SUB_CRYPTO, "Initizializing 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); knet_h->crypto_instance = malloc(sizeof(struct crypto_instance)); if (!knet_h->crypto_instance) { log_err(knet_h, KNET_SUB_CRYPTO, "Unable to allocate memory for crypto instance"); pthread_rwlock_unlock(&shlib_rwlock); savederrno = ENOMEM; goto out_err; } /* * 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. */ knet_h->crypto_instance->model = model; if (crypto_modules_cmds[knet_h->crypto_instance->model].ops->init(knet_h, knet_handle_crypto_cfg)) { savederrno = errno; goto out_err; } log_debug(knet_h, KNET_SUB_CRYPTO, "security network overhead: %zu", knet_h->sec_header_size); pthread_rwlock_unlock(&shlib_rwlock); return 0; out_err: if (knet_h->crypto_instance) { free(knet_h->crypto_instance); knet_h->crypto_instance = NULL; } pthread_rwlock_unlock(&shlib_rwlock); errno = savederrno; return -1; } void crypto_fini( knet_handle_t knet_h) { int savederrno = 0; int model = 0; 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 (knet_h->crypto_instance) { model = knet_h->crypto_instance->model; if (crypto_modules_cmds[model].ops->fini != NULL) { crypto_modules_cmds[model].ops->fini(knet_h); } free(knet_h->crypto_instance); knet_h->crypto_instance = NULL; } pthread_rwlock_unlock(&shlib_rwlock); return; } 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_openssl.c b/libknet/crypto_openssl.c index 93ebc4fd..1eb01b01 100644 --- a/libknet/crypto_openssl.c +++ b/libknet/crypto_openssl.c @@ -1,614 +1,614 @@ /* * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #define KNET_MODULE #include "config.h" #include #include #include #include #include #include #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 struct opensslcrypto_instance { void *private_key; int private_key_len; const EVP_CIPHER *crypto_cipher_type; const EVP_MD *crypto_hash_type; }; /* * crypt/decrypt functions openssl1.0 */ #ifdef BUILDCRYPTOOPENSSL10 static int encrypt_openssl( knet_handle_t knet_h, const struct iovec *iov, int iovcnt, unsigned char *buf_out, ssize_t *buf_out_len) { struct opensslcrypto_instance *instance = knet_h->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); /* * contribute to PRNG for each packet we send/receive */ RAND_seed((unsigned char *)iov[iovcnt - 1].iov_base, iov[iovcnt - 1].iov_len); 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; icrypto_instance->model_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); /* * contribute to PRNG for each packet we send/receive */ RAND_seed(buf_in, buf_in_len); /* * 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)); 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)); 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; } #endif #ifdef BUILDCRYPTOOPENSSL11 static int encrypt_openssl( knet_handle_t knet_h, const struct iovec *iov, int iovcnt, unsigned char *buf_out, ssize_t *buf_out_len) { struct opensslcrypto_instance *instance = knet_h->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(); /* * contribute to PRNG for each packet we send/receive */ RAND_seed((unsigned char *)iov[iovcnt - 1].iov_base, iov[iovcnt - 1].iov_len); 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; icrypto_instance->model_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(); /* * contribute to PRNG for each packet we send/receive */ RAND_seed(buf_in, buf_in_len); /* * 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)); 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)); 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 */ static int calculate_openssl_hash( knet_handle_t knet_h, const unsigned char *buf, const size_t buf_len, unsigned char *hash) { struct opensslcrypto_instance *instance = knet_h->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 != knet_h->sec_hash_size)) { ERR_error_string_n(ERR_get_error(), sslerr, sizeof(sslerr)); log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to calculate hash: %s", sslerr); return -1; } return 0; } /* * exported API */ static int opensslcrypto_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) { struct opensslcrypto_instance *instance = knet_h->crypto_instance->model_instance; int i; if (instance->crypto_cipher_type) { if (encrypt_openssl(knet_h, 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, buf_out, *buf_out_len, buf_out + *buf_out_len) < 0) { return -1; } *buf_out_len = *buf_out_len + knet_h->sec_hash_size; } return 0; } static int opensslcrypto_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) { 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, &iov_in, 1, buf_out, buf_out_len); } static int opensslcrypto_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) { struct opensslcrypto_instance *instance = knet_h->crypto_instance->model_instance; ssize_t temp_len = buf_in_len; if (instance->crypto_hash_type) { unsigned char tmp_hash[knet_h->sec_hash_size]; ssize_t temp_buf_len = buf_in_len - knet_h->sec_hash_size; if ((temp_buf_len <= 0) || (temp_buf_len > KNET_MAX_PACKET_SIZE)) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Incorrect packet size."); return -1; } if (calculate_openssl_hash(knet_h, buf_in, temp_buf_len, tmp_hash) < 0) { return -1; } if (memcmp(tmp_hash, buf_in + temp_buf_len, knet_h->sec_hash_size) != 0) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Digest does not match"); return -1; } temp_len = temp_len - knet_h->sec_hash_size; *buf_out_len = temp_len; } if (instance->crypto_cipher_type) { if (decrypt_openssl(knet_h, buf_in, temp_len, buf_out, buf_out_len) < 0) { return -1; } } else { memmove(buf_out, buf_in, temp_len); *buf_out_len = temp_len; } return 0; } #ifdef BUILDCRYPTOOPENSSL10 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) { - pthread_mutex_lock(&(openssl_internal_lock[type])); + (void)pthread_mutex_lock(&(openssl_internal_lock[type])); } else { pthread_mutex_unlock(&(openssl_internal_lock[type])); } } -static unsigned long openssl_internal_thread_id(void) +static pthread_t openssl_internal_thread_id(void) { - return (unsigned long)pthread_self(); + 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 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((unsigned long (*)(void))openssl_internal_thread_id); + CRYPTO_set_id_callback((void *)openssl_internal_thread_id); CRYPTO_set_locking_callback((void *)&openssl_internal_locking_callback); out: if (err) { openssl_internal_lock_cleanup(); } errno = savederrno; return err; } #endif static void opensslcrypto_fini( knet_handle_t knet_h) { struct opensslcrypto_instance *opensslcrypto_instance = knet_h->crypto_instance->model_instance; if (opensslcrypto_instance) { #ifdef BUILDCRYPTOOPENSSL10 openssl_internal_lock_cleanup(); #endif if (opensslcrypto_instance->private_key) { free(opensslcrypto_instance->private_key); opensslcrypto_instance->private_key = NULL; } free(opensslcrypto_instance); knet_h->crypto_instance->model_instance = NULL; knet_h->sec_header_size = 0; } return; } static int opensslcrypto_init( knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) { static int openssl_is_init = 0; struct opensslcrypto_instance *opensslcrypto_instance = NULL; int savederrno; log_debug(knet_h, KNET_SUB_OPENSSLCRYPTO, "Initizializing openssl crypto module [%s/%s]", knet_handle_crypto_cfg->crypto_cipher_type, knet_handle_crypto_cfg->crypto_hash_type); if (!openssl_is_init) { #ifdef BUILDCRYPTOOPENSSL10 ERR_load_crypto_strings(); OPENSSL_add_all_algorithms_noconf(); #endif #ifdef BUILDCRYPTOOPENSSL11 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; } #ifdef BUILDCRYPTOOPENSSL10 if (openssl_internal_lock_setup() < 0) { log_err(knet_h, KNET_SUB_OPENSSLCRYPTO, "Unable to init openssl"); errno = EAGAIN; return -1; } #endif knet_h->crypto_instance->model_instance = malloc(sizeof(struct opensslcrypto_instance)); if (!knet_h->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 = knet_h->crypto_instance->model_instance; memset(opensslcrypto_instance, 0, sizeof(struct opensslcrypto_instance)); 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 ((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; } 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; knet_h->sec_header_size = 0; if (opensslcrypto_instance->crypto_hash_type) { knet_h->sec_hash_size = EVP_MD_size(opensslcrypto_instance->crypto_hash_type); knet_h->sec_header_size += knet_h->sec_hash_size; } if (opensslcrypto_instance->crypto_cipher_type) { size_t block_size; block_size = EVP_CIPHER_block_size(opensslcrypto_instance->crypto_cipher_type); knet_h->sec_header_size += (block_size * 2); knet_h->sec_header_size += SALT_SIZE; knet_h->sec_salt_size = SALT_SIZE; knet_h->sec_block_size = block_size; } return 0; out_err: opensslcrypto_fini(knet_h); 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 71b6117e..71bcb569 100644 --- a/libknet/handle.c +++ b/libknet/handle.c @@ -1,1604 +1,1613 @@ /* * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #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 pthread_mutex_t handle_config_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_rwlock_t shlib_rwlock; static uint8_t shlib_wrlock_init = 0; static uint32_t knet_ref = 0; static 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; } static void _fini_shlib_tracker(void) { if (knet_ref == 0) { pthread_rwlock_destroy(&shlib_rwlock); shlib_wrlock_init = 0; } return; } 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->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; } 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); } static int _init_socks(knet_handle_t knet_h) { int savederrno = 0; if (_init_socketpair(knet_h, knet_h->hostsockfd)) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to initialize internal hostsockpair: %s", strerror(savederrno)); goto exit_fail; } 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); _close_socketpair(knet_h, knet_h->hostsockfd); } 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_PING_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_PING_SIZE); knet_h->pmtudbuf = malloc(KNET_PMTUD_SIZE_V6); 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); 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, KNET_MAX_TRANSPORTS, sizeof(knet_h->knet_transport_fd_tracker)); 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->hostsockfd[0]; if (epoll_ctl(knet_h->send_to_links_epollfd, EPOLL_CTL_ADD, knet_h->hostsockfd[0], &ev)) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to add hostsockfd[0] to epoll pool: %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->send_to_links_epollfd, EPOLL_CTL_DEL, knet_h->hostsockfd[0], &ev); 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; savederrno = pthread_create(&knet_h->pmtud_link_handler_thread, 0, _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; } savederrno = pthread_create(&knet_h->dst_link_handler_thread, 0, _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; } savederrno = pthread_create(&knet_h->send_to_links_thread, 0, _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; } savederrno = pthread_create(&knet_h->recv_from_links_thread, 0, _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; } savederrno = pthread_create(&knet_h->heartbt_thread, 0, _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; } 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_ex(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 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 '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; /* * init global shlib tracker */ 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; } knet_ref++; if (_init_shlib_tracker(knet_h) < 0) { savederrno = errno; log_err(knet_h, KNET_SUB_HANDLE, "Unable to init handles traceker: %s", strerror(savederrno)); errno = savederrno; 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_RUNNING); + errno = 0; return knet_h; exit_fail: knet_handle_free(knet_h); errno = savederrno; return NULL; } knet_handle_t knet_handle_new(knet_node_id_t host_id, int log_fd, uint8_t default_log_level) { return knet_handle_new_ex(host_id, log_fd, default_log_level, KNET_HANDLE_FLAG_PRIVILEGED); } int knet_handle_free(knet_handle_t knet_h) { int savederrno = 0; if (!knet_h) { 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->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); compress_fini(knet_h, 1); _destroy_locks(knet_h); free(knet_h); knet_h = NULL; - pthread_mutex_lock(&handle_config_mutex); + (void)pthread_mutex_lock(&handle_config_mutex); knet_ref--; _fini_shlib_tracker(); pthread_mutex_unlock(&handle_config_mutex); + errno = 0; return 0; } 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, err = 0; if (!knet_h) { errno = EINVAL; 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); + errno = err ? savederrno : 0; return err; } 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { 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->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 (!knet_h) { 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_HANDLE, "Unable to get write lock: %s", strerror(savederrno)); errno = savederrno; return -1; } knet_h->enabled = enabled; if (enabled) { log_debug(knet_h, KNET_SUB_HANDLE, "Data forwarding is enabled"); } else { 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_pmtud_getfreq(knet_handle_t knet_h, unsigned int *interval) { int savederrno = 0; if (!knet_h) { errno = EINVAL; return -1; } if (!interval) { 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; } *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 (!knet_h) { errno = EINVAL; return -1; } if ((!interval) || (interval > 86400)) { 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->pmtud_interval = interval; log_debug(knet_h, KNET_SUB_HANDLE, "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 (!knet_h) { 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->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_HANDLE, "pmtud_notify_fn enabled"); } else { log_debug(knet_h, KNET_SUB_HANDLE, "pmtud_notify_fn disabled"); } 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 (!knet_h) { errno = EINVAL; 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_HANDLE, "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; } int knet_handle_crypto(knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg) { int savederrno = 0; int err = 0; if (!knet_h) { errno = EINVAL; return -1; } if (!knet_handle_crypto_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; } crypto_fini(knet_h); 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)))) { log_debug(knet_h, KNET_SUB_CRYPTO, "crypto is not enabled"); 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 (min %d): %u", 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 (max %d): %u", 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); if (err) { err = -2; savederrno = errno; } exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; + errno = err ? savederrno : 0; 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + errno = err ? savederrno : 0; return err; } 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + errno = err ? savederrno : 0; return err; } int knet_handle_get_stats(knet_handle_t knet_h, struct knet_handle_stats *stats, size_t struct_size) { int savederrno = 0; int err = 0; if (!knet_h) { errno = EINVAL; return -1; } if (!stats) { 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 (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); pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; + errno = err ? savederrno : 0; return err; } int knet_handle_clear_stats(knet_handle_t knet_h, int clear_option) { int savederrno = 0; int err = 0; if (!knet_h) { errno = EINVAL; 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); - errno = savederrno; + errno = err ? savederrno : 0; return err; } diff --git a/libknet/host.c b/libknet/host.c index 9f6648e8..00e2298e 100644 --- a/libknet/host.c +++ b/libknet/host.c @@ -1,711 +1,712 @@ /* * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, 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 (!knet_h) { 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 = 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)); /* * set host_id */ host->host_id = host_id; /* * set default host->name to host_id for logging */ snprintf(host->name, KNET_MAX_HOST_LEN - 1, "%u", host_id); /* * initialize links internal data */ for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++) { host->link[link_idx].link_id = link_idx; 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) { free(host); } - errno = savederrno; + 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 (!knet_h) { 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; } 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; free(removed); _host_list_update(knet_h); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; + 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 (!knet_h) { 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 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 - 1)) { 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 - 1, "%s", name); exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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, err = 0; if (!knet_h) { errno = EINVAL; 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); - errno = savederrno; + errno = err ? savederrno : 0; return err; } 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { 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_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 = savederrno; + 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 (!knet_h) { 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; } 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; } int _send_host_info(knet_handle_t knet_h, const void *data, const size_t datalen) { ssize_t ret = 0; if (knet_h->fini_in_progress) { return 0; } ret = sendto(knet_h->hostsockfd[1], data, datalen, MSG_DONTWAIT | MSG_NOSIGNAL, NULL, 0); if (ret < 0) { log_debug(knet_h, KNET_SUB_HOST, "Unable to write data to hostpipe. Error: %s", strerror(errno)); return -1; } if ((size_t)ret != datalen) { log_debug(knet_h, KNET_SUB_HOST, "Unable to write all data to hostpipe. Expected: %zu, Written: %zd.", datalen, ret); return -1; } return 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 < KNET_MAX_LINK; i++) { memset(&host->defrag_buf[i], 0, sizeof(struct knet_host_defrag_buf)); } } /* * 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(struct knet_host *host, seq_num_t seq_num, int defrag_buf, int clear_buf) { size_t i, j; /* 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; if (clear_buf) { _clear_cbuffers(host, 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; } j = seq_num % KNET_CBUFFER_SIZE; if (seq_dist < KNET_CBUFFER_SIZE) { /* seq num is in ring buffer */ if (!defrag_buf) { return (dst_cbuf[j] == 0) ? 1 : 0; } else { return (dst_cbuf_defrag[j] == 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 */ i = (*dst_seq_num + 1) % KNET_CBUFFER_SIZE; if (i > j) { memset(dst_cbuf + i, 0, KNET_CBUFFER_SIZE - i); memset(dst_cbuf, 0, j + 1); memset(dst_cbuf_defrag + i, 0, KNET_CBUFFER_SIZE - i); memset(dst_cbuf_defrag, 0, j + 1); } else { memset(dst_cbuf + i, 0, j - i + 1); memset(dst_cbuf_defrag + i, 0, j - i + 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_debug(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_debug(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; } diff --git a/libknet/libknet.h b/libknet/libknet.h index c0f11a0b..d4591e87 100644 --- a/libknet/libknet.h +++ b/libknet/libknet.h @@ -1,1976 +1,1976 @@ /* * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, 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-2018 Red Hat, Inc. All rights reserved. * * Kronosnet is an advanced VPN system for High Availability applications. */ /* * libknet limits */ /* * Maximum number of hosts */ typedef uint16_t knet_node_id_t; #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) typedef struct knet_handle *knet_handle_t; /* * Handle structs/API calls */ /** * knet_handle_new_ex * * @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 UINT16T_MAX are all valid). + * 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. * * 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_ex(knet_node_id_t host_id, int log_fd, uint8_t default_log_level, uint64_t flags); /** * knet_handle_new * * @brief knet_handle_new_ex with flags = KNET_HANDLE_FLAG_PRIVILEGED. */ knet_handle_t knet_handle_new(knet_node_id_t host_id, int log_fd, uint8_t default_log_level); /** * 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_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-exit. + * User provided datafd will be marked as non-blocking and close-on-exec. * - * *channel - This value has the same effect of VLAN tagging. + * *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 */ 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. * (see also kronosnetd/etherfilter.* for an example that filters based * on ether protocol) * * @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); #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_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 256 #define KNET_MAX_KEY_LEN 4096 struct knet_handle_crypto_cfg { char crypto_model[16]; char crypto_cipher_type[16]; char crypto_hash_type[16]; unsigned char private_key[KNET_MAX_KEY_LEN]; unsigned int private_key_len; }; /** * knet_handle_crypto * * @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 only "openssl" and "nss" 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: * "3des", "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. * * 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. * - re-keying is not implemented yet. * - private/public key encryption/hashing is not currently * planned. * - crypto key must be the same for all hosts in the same * knet instance. * - it is safe to call knet_handle_crypto multiple times at runtime. * The last config will be used. * IMPORTANT: a call to knet_handle_crypto 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. * A failure in crypto init, might leave your traffic unencrypted! * It's best to stop data forwarding (see knet_handle_setfwd(3)), change crypto config, * start forward again. * * @return * knet_handle_crypto 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(knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg); #define KNET_COMPRESS_THRESHOLD 100 struct knet_handle_compress_cfg { char compress_model[16]; uint32_t compress_threshold; 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); struct knet_handle_stats { size_t size; uint64_t tx_uncompressed_packets; uint64_t tx_compressed_packets; uint64_t tx_compressed_original_bytes; uint64_t tx_compressed_size_bytes; uint64_t tx_compress_time_ave; uint64_t tx_compress_time_min; uint64_t tx_compress_time_max; uint64_t rx_compressed_packets; uint64_t rx_compressed_original_bytes; uint64_t rx_compressed_size_bytes; uint64_t rx_compress_time_ave; uint64_t rx_compress_time_min; uint64_t rx_compress_time_max; /* Overhead times, measured in usecs */ uint64_t tx_crypt_packets; uint64_t tx_crypt_byte_overhead; uint64_t tx_crypt_time_ave; uint64_t tx_crypt_time_min; uint64_t tx_crypt_time_max; uint64_t rx_crypt_packets; uint64_t rx_crypt_time_ave; uint64_t rx_crypt_time_min; 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); struct knet_crypto_info { const char *name; /* openssl,nss,etc.. */ uint8_t properties; /* currently unused */ char pad[256]; /* currently unused */ }; /** * 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); struct knet_compress_info { const char *name; /* bzip2, lz4, etc.. */ uint8_t properties; /* currently unused */ char pad[256]; /* currently unused */ }; /** * 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); /* * 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 * * knet_h - pointer to knet_handle_t * * @brief Set the switching policy for a host's links * * 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 lowest * priority 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 */ struct knet_host_status { uint8_t reachable; uint8_t remote; uint8_t external; /* add host statistics */ }; /** * knet_host_status_get * * @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. */ struct knet_transport_info { const char *name; /* UDP/SCTP/etc... */ uint8_t id; /* value that can be used for link_set_config */ uint8_t properties; /* currently unused */ char pad[256]; /* currently unused */ }; /** * 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); /** * 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. * * latency average latency of this link * see also knet_link_set/get_timeout. * * 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 struct knet_link_stats { /* onwire values */ uint64_t tx_data_packets; uint64_t rx_data_packets; uint64_t tx_data_bytes; uint64_t rx_data_bytes; uint64_t rx_ping_packets; uint64_t tx_ping_packets; uint64_t rx_ping_bytes; uint64_t tx_ping_bytes; uint64_t rx_pong_packets; uint64_t tx_pong_packets; uint64_t rx_pong_bytes; uint64_t tx_pong_bytes; uint64_t rx_pmtu_packets; uint64_t tx_pmtu_packets; uint64_t rx_pmtu_bytes; uint64_t tx_pmtu_bytes; /* Only filled in when requested */ uint64_t tx_total_packets; uint64_t rx_total_packets; uint64_t tx_total_bytes; uint64_t rx_total_bytes; uint64_t tx_total_errors; uint64_t tx_total_retries; uint32_t tx_pmtu_errors; uint32_t tx_pmtu_retries; uint32_t tx_ping_errors; uint32_t tx_ping_retries; uint32_t tx_pong_errors; uint32_t tx_pong_retries; uint32_t tx_data_errors; uint32_t tx_data_retries; /* measured in usecs */ uint32_t latency_min; uint32_t latency_max; uint32_t latency_ave; uint32_t latency_samples; /* how many times the link has been going up/down */ uint32_t down_count; uint32_t up_count; /* * circular buffer of time_t structs collecting the history * of up/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_up_times[MAX_LINK_EVENTS]; time_t last_down_times[MAX_LINK_EVENTS]; int8_t last_up_time_index; int8_t last_down_time_index; /* Always add new stats at the end */ }; struct knet_link_status { size_t size; /* For ABI checking */ char src_ipaddr[KNET_MAX_HOST_LEN]; char src_port[KNET_MAX_PORT_LEN]; char dst_ipaddr[KNET_MAX_HOST_LEN]; char dst_port[KNET_MAX_PORT_LEN]; uint8_t enabled; /* link is configured and admin enabled for traffic */ uint8_t connected; /* link is connected for data (local view) */ uint8_t dynconnected; /* link has been activated by remote dynip */ unsigned long long latency; /* average latency computed by fix/exp */ struct timespec pong_last; unsigned int mtu; /* current detected MTU on this link */ unsigned int proto_overhead; /* 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. */ /* 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); /* * 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 /* nsscrypto.c */ #define KNET_SUB_OPENSSLCRYPTO 61 /* opensslcrypto.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_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 struct knet_log_msg { char msg[KNET_MAX_LOG_MSG_SIZE]; uint8_t subsystem; /* KNET_SUB_* */ uint8_t msglevel; /* KNET_LOG_* */ }; /** * knet_log_set_log_level * * @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_log_level * * @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/links.c b/libknet/links.c index 6185d4df..ba28dbd9 100644 --- a/libknet/links.c +++ b/libknet/links.c @@ -1,1086 +1,1086 @@ /* * Copyright (C) 2012-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #include "internals.h" #include "logging.h" #include "links.h" #include "transports.h" #include "host.h" #include "threads_common.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) { struct knet_link *link = &knet_h->host_index[host_id]->link[link_id]; if ((link->status.enabled == enabled) && (link->status.connected == connected)) return 0; link->status.enabled = enabled; link->status.connected = connected; _host_dstcache_update_async(knet_h, knet_h->host_index[host_id]); if ((link->status.dynconnected) && (!link->status.connected)) link->status.dynconnected = 0; 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; } } 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; struct knet_host *host; struct knet_link *link; if (!knet_h) { errno = EINVAL; return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!src_addr) { 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; } memmove(&link->src_addr, src_addr, sizeof(struct sockaddr_storage)); 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; memmove(&link->dst_addr, dst_addr, sizeof(struct sockaddr_storage)); 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->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_fix = KNET_LINK_DEFAULT_PING_PRECISION; link->latency_exp = KNET_LINK_DEFAULT_PING_PRECISION - \ ((link->ping_interval * KNET_LINK_DEFAULT_PING_PRECISION) / 8000000); link->flags = flags; if (transport_link_set_config(knet_h, link, transport) < 0) { savederrno = errno; err = -1; goto exit_unlock; } 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 { link->status.mtu = KNET_PMTUD_MIN_MTU_V4 - KNET_HEADER_ALL_SIZE - knet_h->sec_header_size; link->has_valid_mtu = 1; } exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; + 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 (!knet_h) { errno = EINVAL; 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, sizeof(struct sockaddr_storage)); *transport = link->transport_type; *flags = link->flags; if (link->dynamic == KNET_LINK_STATIC) { *dynamic = 0; memmove(dst_addr, &link->dst_addr, sizeof(struct sockaddr_storage)); } else { *dynamic = 1; } exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; + 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; if (!knet_h) { 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 != 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; } if ((transport_link_clear_config(knet_h, link) < 0) && (errno != EBUSY)) { savederrno = errno; err = -1; goto exit_unlock; } 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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); 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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_fix = precision; link->latency_exp = precision - \ ((link->ping_interval * precision) / 8000000); 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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_fix; exit_unlock: pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; + 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 (!knet_h) { 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; } 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; return -1; } if (link_id >= KNET_MAX_LINK) { errno = EINVAL; return -1; } if (!status) { 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; } memmove(status, &link->status, struct_size); /* 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 = savederrno; + errno = err ? savederrno : 0; return err; } diff --git a/libknet/logging.c b/libknet/logging.c index 543269a5..14c6eff5 100644 --- a/libknet/logging.c +++ b/libknet/logging.c @@ -1,241 +1,247 @@ /* * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, 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[] = { { "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 }, { "zlibcomp", KNET_SUB_ZLIBCOMP }, { "lz4comp", KNET_SUB_LZ4COMP }, { "lz4hccomp", KNET_SUB_LZ4HCCOMP }, { "lzo2comp", KNET_SUB_LZO2COMP }, { "lzmacomp", KNET_SUB_LZMACOMP }, { "bzip2comp", KNET_SUB_BZIP2COMP }, { "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[] = { { "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 (!knet_h) { errno = EINVAL; 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 (!knet_h) { errno = EINVAL; 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; 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; 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); while (byte_cnt < sizeof(struct knet_log_msg)) { len = write(knet_h->logfd, &msg, sizeof(struct knet_log_msg) - byte_cnt); if (len <= 0) { goto out; } byte_cnt += len; } out: return; } diff --git a/libknet/netutils.c b/libknet/netutils.c index 69c704a9..f30054ba 100644 --- a/libknet/netutils.c +++ b/libknet/netutils.c @@ -1,167 +1,176 @@ /* * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include "internals.h" #include "netutils.h" static int is_v4_mapped(const struct sockaddr_storage *ss, socklen_t salen) { char map[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) ss; return memcmp(&addr6->sin6_addr, map, 12); } int cmpaddr(const struct sockaddr_storage *ss1, socklen_t sslen1, const struct sockaddr_storage *ss2, socklen_t sslen2) { int ss1_offset = 0, ss2_offset = 0; 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; char *addr1, *addr2; if (ss1->ss_family == ss2->ss_family) { return memcmp(ss1, ss2, sslen1); } if (ss1->ss_family == AF_INET6) { if (is_v4_mapped(ss1, sslen1)) { return 1; } addr1 = (char *)&ss1_addr6->sin6_addr; ss1_offset = 12; } else { addr1 = (char *)&ss1_addr->sin_addr; } if (ss2->ss_family == AF_INET6) { if (is_v4_mapped(ss2, sslen2)) { return 1; } addr2 = (char *)&ss2_addr6->sin6_addr; ss2_offset = 12; } else { addr2 = (char *)&ss2_addr->sin_addr; } return memcmp(addr1+ss1_offset, addr2+ss2_offset, 4); } int cpyaddrport(struct sockaddr_storage *dst, const struct sockaddr_storage *src) { struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *)dst; struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *)src; memset(dst, 0, sizeof(struct sockaddr_storage)); if (src->ss_family == AF_INET6) { dst->ss_family = src->ss_family; memmove(&dst_addr6->sin6_port, &src_addr6->sin6_port, sizeof(in_port_t)); memmove(&dst_addr6->sin6_addr, &src_addr6->sin6_addr, sizeof(struct in6_addr)); } else { memmove(dst, src, sizeof(struct sockaddr_in)); } return 0; } 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); } } /* * 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; } - return getnameinfo((struct sockaddr *)ss, sockaddr_len(ss), addr_buf, addr_buf_size, - port_buf, port_buf_size, - NI_NUMERICHOST | NI_NUMERICSERV); + 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/tests/api_knet_addrtostr.c b/libknet/tests/api_knet_addrtostr.c index d7c43959..71fc2cfe 100644 --- a/libknet/tests/api_knet_addrtostr.c +++ b/libknet/tests/api_knet_addrtostr.c @@ -1,127 +1,127 @@ /* * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, LGPL-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\n"); + 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\n"); + 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\n"); + 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\n"); + 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_transport_id_by_name.c b/libknet/tests/api_knet_get_transport_id_by_name.c index eff12e79..97d8a6c3 100644 --- a/libknet/tests/api_knet_get_transport_id_by_name.c +++ b/libknet/tests/api_knet_get_transport_id_by_name.c @@ -1,54 +1,54 @@ /* * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-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_handle_get_transport_id_by_name failed: %s\n", strerror(errno)); + 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 bd12b5d0..0c2fa938 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-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-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_handle_get_transport_list with no entries_list\n"); + 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+, LGPL-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_handle_get_transport_name_by_id with incorrect transport\n"); + 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_strtoaddr.c b/libknet/tests/api_knet_strtoaddr.c index ac301ffe..08786034 100644 --- a/libknet/tests/api_knet_strtoaddr.c +++ b/libknet/tests/api_knet_strtoaddr.c @@ -1,115 +1,115 @@ /* * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, LGPL-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)) && + if (!knet_strtoaddr(NULL, "50000", &out_addr, sizeof(struct sockaddr_storage)) || (errno != EINVAL)) { - printf("knet_strtoaddr accepted invalid host\n"); + 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)) && + if (!knet_strtoaddr("127.0.0.1", NULL, &out_addr, sizeof(struct sockaddr_storage)) || (errno != EINVAL)) { - printf("knet_strtoaddr accepted invalid port\n"); + 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)) && + if (!knet_strtoaddr("127.0.0.1", "50000", NULL, sizeof(struct sockaddr_storage)) || (errno != EINVAL)) { - printf("knet_strtoaddr accepted invalid addr\n"); + 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) && + if (!knet_strtoaddr("127.0.0.1", "50000", &out_addr, 0) || (errno != EINVAL)) { - printf("knet_strtoaddr accepted invalid size\n"); + 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/threads_rx.c b/libknet/threads_rx.c index 3017ce4c..2aa1164a 100644 --- a/libknet/threads_rx.c +++ b/libknet/threads_rx.c @@ -1,851 +1,851 @@ /* * Copyright (C) 2012-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include "compat.h" #include "compress.h" #include "crypto.h" #include "host.h" #include "links.h" #include "logging.h" #include "transports.h" #include "transport_common.h" #include "threads_common.h" #include "threads_heartbeat.h" #include "threads_rx.h" #include "netutils.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; } } } /* * this functions needs to return an index (0 to 7) * to a knet_host_defrag_buf. (-1 on errors) */ static int find_pckt_defrag_buf(knet_handle_t knet_h, struct knet_header *inbuf) { struct knet_host *src_host = knet_h->host_index[inbuf->kh_node]; int i, oldest; /* * check if there is a buffer already in use handling the same seq_num */ for (i = 0; i < KNET_MAX_LINK; i++) { if (src_host->defrag_buf[i].in_use) { if (src_host->defrag_buf[i].pckt_seq == inbuf->khp_data_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(src_host, inbuf->khp_data_seq_num, 1, 0)) { errno = ETIME; return -1; } /* * register the pckt as seen */ _seq_num_set(src_host, inbuf->khp_data_seq_num, 1); /* * see if there is a free buffer */ for (i = 0; i < KNET_MAX_LINK; i++) { if (!src_host->defrag_buf[i].in_use) { return i; } } /* * 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 < KNET_MAX_LINK; i++) { if (timecmp(src_host->defrag_buf[i].last_update, src_host->defrag_buf[oldest].last_update) < 0) { oldest = i; } } src_host->defrag_buf[oldest].in_use = 0; return oldest; } static int pckt_defrag(knet_handle_t knet_h, struct knet_header *inbuf, ssize_t *len) { struct knet_host_defrag_buf *defrag_buf; int defrag_buf_idx; defrag_buf_idx = find_pckt_defrag_buf(knet_h, inbuf); if (defrag_buf_idx < 0) { if (errno == ETIME) { log_debug(knet_h, KNET_SUB_RX, "Defrag buffer expired"); } return 1; } defrag_buf = &knet_h->host_index[inbuf->kh_node]->defrag_buf[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 = inbuf->khp_data_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[inbuf->khp_data_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 (inbuf->khp_data_frag_seq == inbuf->khp_data_frag_num) { 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), inbuf->khp_data_userdata, *len); } } else { defrag_buf->frag_size = *len; } memmove(defrag_buf->buf + ((inbuf->khp_data_frag_seq - 1) * defrag_buf->frag_size), inbuf->khp_data_userdata, *len); defrag_buf->frag_recv++; defrag_buf->frag_map[inbuf->khp_data_frag_seq] = 1; /* * check if we received all the fragments */ if (defrag_buf->frag_recv == inbuf->khp_data_frag_num) { /* * special case the last pckt */ if (defrag_buf->last_first) { memmove(defrag_buf->buf + ((inbuf->khp_data_frag_num - 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 = ((inbuf->khp_data_frag_num - 1) * defrag_buf->frag_size) + defrag_buf->last_frag_size; /* * copy the pckt back in the user data */ memmove(inbuf->khp_data_userdata, defrag_buf->buf, *len); /* * free this buffer */ defrag_buf->in_use = 0; return 0; } return 1; } static void _parse_recv_from_links(knet_handle_t knet_h, int sockfd, const struct knet_mmsghdr *msg) { int err = 0, savederrno = 0; ssize_t outlen; struct knet_host *src_host; struct knet_link *src_link; unsigned long long latency_last; knet_node_id_t dst_host_ids[KNET_MAX_HOST]; size_t dst_host_ids_entries = 0; int bcast = 1; int was_decrypted = 0; uint64_t crypt_time = 0; struct timespec recvtime; struct knet_header *inbuf = msg->msg_hdr.msg_iov->iov_base; unsigned char *outbuf = (unsigned char *)msg->msg_hdr.msg_iov->iov_base; ssize_t len = msg->msg_len; struct knet_hostinfo *knet_hostinfo; struct iovec iov_out[1]; int8_t channel; struct sockaddr_storage pckt_src; seq_num_t recv_seq_num; int wipe_bufs = 0; if (knet_h->crypto_instance) { struct timespec start_time; struct timespec end_time; 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"); return; } clock_gettime(CLOCK_MONOTONIC, &end_time); timespec_diff(start_time, end_time, &crypt_time); if (crypt_time < knet_h->stats.rx_crypt_time_min) { knet_h->stats.rx_crypt_time_min = crypt_time; } if (crypt_time > knet_h->stats.rx_crypt_time_max) { knet_h->stats.rx_crypt_time_max = crypt_time; } len = outlen; inbuf = (struct knet_header *)knet_h->recv_from_links_buf_decrypt; was_decrypted++; } if (len < (ssize_t)(KNET_HEADER_SIZE + 1)) { log_debug(knet_h, KNET_SUB_RX, "Packet is too short: %ld", (long)len); return; } if (inbuf->kh_version != KNET_HEADER_VERSION) { log_debug(knet_h, KNET_SUB_RX, "Packet version does not match"); return; } inbuf->kh_node = ntohs(inbuf->kh_node); 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; } src_link = NULL; src_link = src_host->link + (inbuf->khp_ping_link % KNET_MAX_LINK); if ((inbuf->kh_type & KNET_HEADER_TYPE_PMSK) != 0) { if (src_link->dynamic == KNET_LINK_DYNIP) { /* * cpyaddrport will only copy address and port of the incoming * packet and strip extra bits such as flow and scopeid */ cpyaddrport(&pckt_src, msg->msg_hdr.msg_name); if (cmpaddr(&src_link->dst_addr, sockaddr_len(&src_link->dst_addr), &pckt_src, sockaddr_len(&pckt_src)) != 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, &pckt_src, sizeof(struct sockaddr_storage)); if (knet_addrtostr(&src_link->dst_addr, sockaddr_len(msg->msg_hdr.msg_name), 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); } } switch (inbuf->kh_type) { case KNET_HEADER_TYPE_HOST_INFO: case KNET_HEADER_TYPE_DATA: /* * TODO: should we accept data even if we can't reply to the other node? * how would that work with SCTP and guaranteed delivery? */ if (!src_host->status.reachable) { log_debug(knet_h, KNET_SUB_RX, "Source host %u not reachable yet", src_host->host_id); //return; } inbuf->khp_data_seq_num = ntohs(inbuf->khp_data_seq_num); channel = inbuf->khp_data_channel; src_host->got_data = 1; if (src_link) { src_link->status.stats.rx_data_packets++; src_link->status.stats.rx_data_bytes += len; } if (!_seq_num_lookup(src_host, inbuf->khp_data_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 (inbuf->khp_data_frag_num > 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 */ len = len - KNET_HEADER_DATA_SIZE; if (pckt_defrag(knet_h, inbuf, &len)) { return; } len = len + KNET_HEADER_DATA_SIZE; } if (inbuf->khp_data_compress) { ssize_t decmp_outlen = KNET_DATABUFSIZE_COMPRESS; struct timespec start_time; struct timespec end_time; uint64_t compress_time; clock_gettime(CLOCK_MONOTONIC, &start_time); err = decompress(knet_h, inbuf->khp_data_compress, (const unsigned char *)inbuf->khp_data_userdata, len - KNET_HEADER_DATA_SIZE, knet_h->recv_from_links_buf_decompress, &decmp_outlen); if (!err) { /* Collect stats */ clock_gettime(CLOCK_MONOTONIC, &end_time); timespec_diff(start_time, end_time, &compress_time); if (compress_time < knet_h->stats.rx_compress_time_min) { knet_h->stats.rx_compress_time_min = compress_time; } if (compress_time > knet_h->stats.rx_compress_time_max) { knet_h->stats.rx_compress_time_max = compress_time; } knet_h->stats.rx_compress_time_ave = (knet_h->stats.rx_compress_time_ave * knet_h->stats.rx_compressed_packets + compress_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(inbuf->khp_data_userdata, knet_h->recv_from_links_buf_decompress, decmp_outlen); len = decmp_outlen + KNET_HEADER_DATA_SIZE; } else { log_warn(knet_h, KNET_SUB_COMPRESS, "Unable to decompress packet (%d): %s", err, strerror(errno)); return; } } if (inbuf->kh_type == KNET_HEADER_TYPE_DATA) { if (knet_h->enabled != 1) /* data forward is disabled */ break; /* Only update the crypto overhead for data packets. Mainly to be consistent with TX */ knet_h->stats.rx_crypt_time_ave = (knet_h->stats.rx_crypt_time_ave * knet_h->stats.rx_crypt_packets + crypt_time) / (knet_h->stats.rx_crypt_packets+1); knet_h->stats.rx_crypt_packets++; if (knet_h->dst_host_filter_fn) { size_t host_idx; int found = 0; bcast = knet_h->dst_host_filter_fn( knet_h->dst_host_filter_fn_private_data, (const unsigned char *)inbuf->khp_data_userdata, len - KNET_HEADER_DATA_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; } if ((!bcast) && (!dst_host_ids_entries)) { log_debug(knet_h, KNET_SUB_RX, "Message is unicast but no dst_host_ids_entries"); return; } /* 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; } 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; } } } } if (inbuf->kh_type == KNET_HEADER_TYPE_DATA) { 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; } memset(iov_out, 0, sizeof(iov_out)); iov_out[0].iov_base = (void *) inbuf->khp_data_userdata; iov_out[0].iov_len = len - KNET_HEADER_DATA_SIZE; outlen = writev(knet_h->sockfd[channel].sockfd[knet_h->sockfd[channel].is_created], iov_out, 1); 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; } if ((size_t)outlen == iov_out[0].iov_len) { _seq_num_set(src_host, inbuf->khp_data_seq_num, 0); } } else { /* HOSTINFO */ knet_hostinfo = (struct knet_hostinfo *)inbuf->khp_data_userdata; if (knet_hostinfo->khi_bcast == KNET_HOSTINFO_UCAST) { bcast = 0; knet_hostinfo->khi_dst_node_id = ntohs(knet_hostinfo->khi_dst_node_id); } if (!_seq_num_lookup(src_host, inbuf->khp_data_seq_num, 0, 0)) { return; } _seq_num_set(src_host, inbuf->khp_data_seq_num, 0); switch(knet_hostinfo->khi_type) { case KNET_HOSTINFO_TYPE_LINK_UP_DOWN: break; case KNET_HOSTINFO_TYPE_LINK_TABLE: break; default: log_warn(knet_h, KNET_SUB_RX, "Receiving unknown host info message from host %u", src_host->host_id); break; } } break; case KNET_HEADER_TYPE_PING: outlen = KNET_HEADER_PING_SIZE; inbuf->kh_type = KNET_HEADER_TYPE_PONG; inbuf->kh_node = htons(knet_h->host_id); recv_seq_num = ntohs(inbuf->khp_ping_seq_num); src_link->status.stats.rx_ping_packets++; src_link->status.stats.rx_ping_bytes += len; wipe_bufs = 0; if (!inbuf->khp_ping_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(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(src_host, recv_seq_num, 0, 1); } } } if (knet_h->crypto_instance) { if (crypto_encrypt_and_sign(knet_h, (const unsigned char *)inbuf, - len, + outlen, knet_h->recv_from_links_buf_crypt, &outlen) < 0) { log_debug(knet_h, KNET_SUB_RX, "Unable to encrypt pong packet"); break; } outbuf = knet_h->recv_from_links_buf_crypt; knet_h->stats_extra.tx_crypt_pong_packets++; } retry_pong: len = sendto(src_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) &src_link->dst_addr, sizeof(struct sockaddr_storage)); savederrno = errno; if (len != outlen) { err = transport_tx_sock_error(knet_h, src_link->transport_type, src_link->outsock, len, savederrno); switch(err) { case -1: /* unrecoverable error */ log_debug(knet_h, KNET_SUB_RX, "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 0: /* ignore error and continue */ break; case 1: /* retry to send those same data */ src_link->status.stats.tx_pong_retries++; goto retry_pong; break; } } src_link->status.stats.tx_pong_packets++; src_link->status.stats.tx_pong_bytes += outlen; break; case KNET_HEADER_TYPE_PONG: src_link->status.stats.rx_pong_packets++; src_link->status.stats.rx_pong_bytes += len; clock_gettime(CLOCK_MONOTONIC, &src_link->status.pong_last); memmove(&recvtime, &inbuf->khp_ping_time[0], sizeof(struct timespec)); timespec_diff(recvtime, src_link->status.pong_last, &latency_last); src_link->status.latency = ((src_link->status.latency * src_link->latency_exp) + ((latency_last / 1000llu) * (src_link->latency_fix - src_link->latency_exp))) / src_link->latency_fix; if (src_link->status.latency < 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_RX, "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); } else { src_link->received_pong++; log_debug(knet_h, KNET_SUB_RX, "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.latency > src_link->status.stats.latency_max) { src_link->status.stats.latency_max = src_link->status.latency; } if (src_link->status.latency < src_link->status.stats.latency_min) { src_link->status.stats.latency_min = src_link->status.latency; } src_link->status.stats.latency_ave = (src_link->status.stats.latency_ave * src_link->status.stats.latency_samples + src_link->status.latency) / (src_link->status.stats.latency_samples+1); src_link->status.stats.latency_samples++; break; case KNET_HEADER_TYPE_PMTUD: src_link->status.stats.rx_pmtu_packets++; src_link->status.stats.rx_pmtu_bytes += len; outlen = KNET_HEADER_PMTUD_SIZE; inbuf->kh_type = KNET_HEADER_TYPE_PMTUD_REPLY; inbuf->kh_node = htons(knet_h->host_id); if (knet_h->crypto_instance) { if (crypto_encrypt_and_sign(knet_h, (const unsigned char *)inbuf, - len, + outlen, knet_h->recv_from_links_buf_crypt, &outlen) < 0) { log_debug(knet_h, KNET_SUB_RX, "Unable to encrypt PMTUd reply packet"); break; } outbuf = knet_h->recv_from_links_buf_crypt; knet_h->stats_extra.tx_crypt_pmtu_reply_packets++; } savederrno = pthread_mutex_lock(&knet_h->tx_mutex); if (savederrno) { log_err(knet_h, KNET_SUB_RX, "Unable to get TX mutex lock: %s", strerror(savederrno)); goto out_pmtud; } retry_pmtud: len = sendto(src_link->outsock, outbuf, outlen, MSG_DONTWAIT | MSG_NOSIGNAL, (struct sockaddr *) &src_link->dst_addr, sizeof(struct sockaddr_storage)); savederrno = errno; if (len != outlen) { err = transport_tx_sock_error(knet_h, src_link->transport_type, src_link->outsock, len, savederrno); switch(err) { case -1: /* unrecoverable error */ log_debug(knet_h, KNET_SUB_RX, "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 0: /* ignore error and continue */ src_link->status.stats.tx_pmtu_errors++; break; case 1: /* retry to send those same data */ src_link->status.stats.tx_pmtu_retries++; goto retry_pmtud; break; } } pthread_mutex_unlock(&knet_h->tx_mutex); out_pmtud: break; case KNET_HEADER_TYPE_PMTUD_REPLY: src_link->status.stats.rx_pmtu_packets++; src_link->status.stats.rx_pmtu_bytes += len; if (pthread_mutex_lock(&knet_h->pmtud_mutex) != 0) { log_debug(knet_h, KNET_SUB_RX, "Unable to get mutex lock"); break; } src_link->last_recv_mtu = inbuf->khp_pmtud_size; pthread_cond_signal(&knet_h->pmtud_cond); pthread_mutex_unlock(&knet_h->pmtud_mutex); break; default: return; } } 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 = sizeof(struct sockaddr_storage); } 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 -1: /* on error */ log_debug(knet_h, KNET_SUB_RX, "Transport reported error parsing packet"); goto exit_unlock; break; case 0: /* 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 1: /* 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 2: /* packet is data and should be parsed as such */ _parse_recv_from_links(knet_h, sockfd, &msg[i]); break; } } exit_unlock: 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_RUNNING); memset(&msg, 0, sizeof(msg)); 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); 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_THREADS_TIMERES / 1000); /* * 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; } diff --git a/libknet/threads_tx.c b/libknet/threads_tx.c index 20798c0c..f73592ab 100644 --- a/libknet/threads_tx.c +++ b/libknet/threads_tx.c @@ -1,756 +1,748 @@ /* * Copyright (C) 2012-2018 Red Hat, Inc. All rights reserved. * * Authors: Fabio M. Di Nitto * Federico Simoncelli * * This software licensed under GPL-2.0+, 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 "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" /* * 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; 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++) { sent_msgs = 0; prev_sent = 0; progress = 1; cur_link = &dst_host->link[dst_host->active_links[link_idx]]; if (cur_link->transport_type == KNET_TRANSPORT_LOOPBACK) { continue; } msg_idx = 0; while (msg_idx < msgs_to_send) { msg[msg_idx].msg_hdr.msg_name = &cur_link->dst_addr; /* 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, &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_type, dst_host->link[dst_host->active_links[link_idx]].outsock, sent_msgs, savederrno); switch(err) { case -1: /* unrecoverable error */ cur_link->status.stats.tx_data_errors++; goto out_unlock; break; case 0: /* ignore error and continue */ break; case 1: /* retry to send those same data */ 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; } } out_unlock: errno = savederrno; return err; } static int _parse_recv_from_sock(knet_handle_t knet_h, size_t inlen, int8_t channel, int is_sync) { size_t outlen, frag_len; struct knet_host *dst_host; knet_node_id_t dst_host_ids_temp[KNET_MAX_HOST]; size_t dst_host_ids_entries_temp = 0; knet_node_id_t dst_host_ids[KNET_MAX_HOST]; size_t dst_host_ids_entries = 0; int bcast = 1; struct knet_hostinfo *knet_hostinfo; struct iovec iov_out[PCKT_FRAG_MAX][2]; int iovcnt_out = 2; uint8_t frag_idx; unsigned int temp_data_mtu; size_t host_idx; int send_mcast = 0; struct knet_header *inbuf; int savederrno = 0; int err = 0; seq_num_t tx_seq_num; struct knet_mmsghdr msg[PCKT_FRAG_MAX]; int msgs_to_send, msg_idx; unsigned int i; int j; int send_local = 0; int data_compressed = 0; size_t uncrypted_frag_size; inbuf = knet_h->recv_from_sock_buf; if ((knet_h->enabled != 1) && (inbuf->kh_type != KNET_HEADER_TYPE_HOST_INFO)) { /* data forward is disabled */ log_debug(knet_h, KNET_SUB_TX, "Received data packet but forwarding is disabled"); savederrno = ECANCELED; err = -1; goto out_unlock; } /* * move this into a separate function to expand on * extra switching rules */ switch(inbuf->kh_type) { case KNET_HEADER_TYPE_DATA: if (knet_h->dst_host_filter_fn) { bcast = knet_h->dst_host_filter_fn( knet_h->dst_host_filter_fn_private_data, (const unsigned char *)inbuf->khp_data_userdata, 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_unlock; } 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_unlock; } 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_unlock; } } /* Send to localhost if appropriate and enabled */ if (knet_h->has_loop_link) { send_local = 0; if (bcast) { send_local = 1; } else { for (i=0; i< dst_host_ids_entries_temp; i++) { if (dst_host_ids_temp[i] == knet_h->host_id) { send_local = 1; } } } if (send_local) { const unsigned char *buf = inbuf->khp_data_userdata; ssize_t buflen = inlen; struct knet_link *local_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); 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++; } 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; usleep(KNET_THREADS_TIMERES / 16); goto local_retry; } if (err == buflen) { local_link->status.stats.tx_data_packets++; local_link->status.stats.tx_data_bytes += inlen; } } } break; case KNET_HEADER_TYPE_HOST_INFO: knet_hostinfo = (struct knet_hostinfo *)inbuf->khp_data_userdata; if (knet_hostinfo->khi_bcast == KNET_HOSTINFO_UCAST) { bcast = 0; dst_host_ids_temp[0] = knet_hostinfo->khi_dst_node_id; dst_host_ids_entries_temp = 1; knet_hostinfo->khi_dst_node_id = htons(knet_hostinfo->khi_dst_node_id); } break; default: log_warn(knet_h, KNET_SUB_TX, "Receiving unknown messages from socket"); savederrno = ENOMSG; err = -1; goto out_unlock; break; } 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_unlock; } } /* * 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 = 0; 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) && dst_host->status.reachable) { dst_host_ids[dst_host_ids_entries] = dst_host_ids_temp[host_idx]; dst_host_ids_entries++; } } if (!dst_host_ids_entries) { savederrno = EHOSTDOWN; err = -1; goto out_unlock; } } else { send_mcast = 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) && dst_host->status.reachable) { send_mcast = 1; break; } } if (!send_mcast) { savederrno = EHOSTDOWN; err = -1; goto out_unlock; } } 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; } /* * compress data */ if ((knet_h->compress_model > 0) && (inlen > knet_h->compress_threshold)) { size_t cmp_outlen = KNET_DATABUFSIZE_COMPRESS; struct timespec start_time; struct timespec end_time; uint64_t compress_time; clock_gettime(CLOCK_MONOTONIC, &start_time); err = compress(knet_h, (const unsigned char *)inbuf->khp_data_userdata, inlen, knet_h->send_to_links_buf_compress, (ssize_t *)&cmp_outlen); if (err < 0) { log_warn(knet_h, KNET_SUB_COMPRESS, "Compression failed (%d): %s", err, strerror(errno)); } else { /* Collect stats */ clock_gettime(CLOCK_MONOTONIC, &end_time); timespec_diff(start_time, end_time, &compress_time); 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); 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(inbuf->khp_data_userdata, knet_h->send_to_links_buf_compress, cmp_outlen); inlen = cmp_outlen; data_compressed = 1; } } } - if ((knet_h->compress_model > 0) && (inlen <= knet_h->compress_threshold)) { + if (knet_h->compress_model > 0 && !data_compressed) { knet_h->stats.tx_uncompressed_packets++; } /* * prepare the outgoing buffers */ frag_len = inlen; frag_idx = 0; inbuf->khp_data_bcast = bcast; inbuf->khp_data_frag_num = ceil((float)inlen / temp_data_mtu); inbuf->khp_data_channel = channel; if (data_compressed) { inbuf->khp_data_compress = knet_h->compress_model; } else { inbuf->khp_data_compress = 0; } if (pthread_mutex_lock(&knet_h->tx_seq_num_mutex)) { log_debug(knet_h, KNET_SUB_TX, "Unable to get seq mutex lock"); goto out_unlock; } 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; inbuf->khp_data_seq_num = htons(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); } if (inbuf->khp_data_frag_num > 1) { while (frag_idx < inbuf->khp_data_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_SIZE; iov_out[frag_idx][1].iov_base = inbuf->khp_data_userdata + (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 */ knet_h->send_to_links_buf[frag_idx]->kh_type = inbuf->kh_type; knet_h->send_to_links_buf[frag_idx]->khp_data_seq_num = inbuf->khp_data_seq_num; knet_h->send_to_links_buf[frag_idx]->khp_data_frag_num = inbuf->khp_data_frag_num; knet_h->send_to_links_buf[frag_idx]->khp_data_bcast = inbuf->khp_data_bcast; knet_h->send_to_links_buf[frag_idx]->khp_data_channel = inbuf->khp_data_channel; knet_h->send_to_links_buf[frag_idx]->khp_data_compress = inbuf->khp_data_compress; 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_SIZE; iovcnt_out = 1; } if (knet_h->crypto_instance) { struct timespec start_time; struct timespec end_time; uint64_t crypt_time; frag_idx = 0; while (frag_idx < inbuf->khp_data_frag_num) { 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_unlock; } clock_gettime(CLOCK_MONOTONIC, &end_time); timespec_diff(start_time, end_time, &crypt_time); 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++; 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; } memset(&msg, 0, sizeof(msg)); msgs_to_send = inbuf->khp_data_frag_num; msg_idx = 0; while (msg_idx < msgs_to_send) { msg[msg_idx].msg_hdr.msg_namelen = sizeof(struct sockaddr_storage); 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_unlock; } } } 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_unlock; } } } } out_unlock: errno = savederrno; return err; } 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; if (!knet_h) { errno = EINVAL; 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->sockfd[channel].in_use) { savederrno = EINVAL; err = -1; goto out; } 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; } knet_h->recv_from_sock_buf->kh_type = KNET_HEADER_TYPE_DATA; memmove(knet_h->recv_from_sock_buf->khp_data_userdata, buff, buff_len); err = _parse_recv_from_sock(knet_h, buff_len, channel, 1); savederrno = errno; pthread_mutex_unlock(&knet_h->tx_mutex); out: pthread_rwlock_unlock(&knet_h->global_rwlock); - errno = savederrno; + errno = err ? savederrno : 0; return err; } static void _handle_send_to_links(knet_handle_t knet_h, struct msghdr *msg, int sockfd, int8_t channel, int type) { ssize_t inlen = 0; int savederrno = 0, docallback = 0; 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 (inlen == 0) { savederrno = 0; docallback = 1; - goto out; - } - if (inlen < 0) { - savederrno = errno; - docallback = 1; - goto out; - } - - knet_h->recv_from_sock_buf->kh_type = type; - _parse_recv_from_sock(knet_h, inlen, channel, 0); - -out: - if (inlen < 0) { + } 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 { + knet_h->recv_from_sock_buf->kh_type = type; + _parse_recv_from_sock(knet_h, inlen, channel, 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]; int i, nev, type; int8_t channel; struct iovec iov_in; struct msghdr msg; struct sockaddr_storage address; set_thread_status(knet_h, KNET_THREAD_TX, KNET_THREAD_RUNNING); memset(&iov_in, 0, sizeof(iov_in)); iov_in.iov_base = (void *)knet_h->recv_from_sock_buf->khp_data_userdata; iov_in.iov_len = KNET_MAX_PACKET_SIZE; memset(&msg, 0, sizeof(struct msghdr)); msg.msg_name = &address; msg.msg_namelen = sizeof(struct sockaddr_storage); msg.msg_iov = &iov_in; msg.msg_iovlen = 1; knet_h->recv_from_sock_buf->kh_version = KNET_HEADER_VERSION; knet_h->recv_from_sock_buf->khp_data_frag_seq = 0; knet_h->recv_from_sock_buf->kh_node = htons(knet_h->host_id); for (i = 0; i < PCKT_FRAG_MAX; i++) { knet_h->send_to_links_buf[i]->kh_version = KNET_HEADER_VERSION; knet_h->send_to_links_buf[i]->khp_data_frag_seq = i + 1; knet_h->send_to_links_buf[i]->kh_node = htons(knet_h->host_id); } while (!shutdown_in_progress(knet_h)) { nev = epoll_wait(knet_h->send_to_links_epollfd, events, KNET_EPOLL_MAX_EVENTS + 1, KNET_THREADS_TIMERES / 1000); /* * we use timeout to detect if thread is shutting down */ if (nev == 0) { continue; } if (pthread_rwlock_rdlock(&knet_h->global_rwlock) != 0) { log_debug(knet_h, KNET_SUB_TX, "Unable to get read lock"); continue; } for (i = 0; i < nev; i++) { if (events[i].data.fd == knet_h->hostsockfd[0]) { type = KNET_HEADER_TYPE_HOST_INFO; channel = -1; } else { type = KNET_HEADER_TYPE_DATA; 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, &msg, events[i].data.fd, channel, type); pthread_mutex_unlock(&knet_h->tx_mutex); } pthread_rwlock_unlock(&knet_h->global_rwlock); } set_thread_status(knet_h, KNET_THREAD_TX, KNET_THREAD_STOPPED); return NULL; } diff --git a/libknet/transport_common.c b/libknet/transport_common.c index ffae0a5c..6df50027 100644 --- a/libknet/transport_common.c +++ b/libknet/transport_common.c @@ -1,439 +1,439 @@ /* * Copyright (C) 2016-2018 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, 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 guaranteed + * 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; } else { if ((i > 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) { savederrno = 0; } break; } } errno = savederrno; return ((i > 0) ? (int)i : err); } int _sendmmsg(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 = sendmsg(sockfd, &msgvec[i].msg_hdr, 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 } 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 %s reuseaddr: %s", type, strerror(savederrno)); goto exit_error; } 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, 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].data = data; return 0; } diff --git a/libknet/transport_sctp.c b/libknet/transport_sctp.c index 7a635338..a22517ca 100644 --- a/libknet/transport_sctp.c +++ b/libknet/transport_sctp.c @@ -1,1441 +1,1502 @@ /* * Copyright (C) 2016-2018 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #include #include #include #include "compat.h" #include "host.h" #include "links.h" #include "logging.h" #include "common.h" #include "transport_common.h" #include "threads_common.h" #ifdef HAVE_NETINET_SCTP_H #include #include "transport_sctp.h" typedef struct sctp_handle_info { struct knet_list_head listen_links_list; struct knet_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 knet_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; } 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 knet_list_head list; sctp_listen_link_info_t *listener; struct knet_link *link; struct sockaddr_storage dst_address; int connect_sock; int on_connected_epoll; int on_rx_epoll; int close_sock; } 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; sctp_connect_link_info_t *info = kn_link->transport_link; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; struct epoll_event ev; if (info->on_connected_epoll) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLOUT; ev.data.fd = info->connect_sock; if (epoll_ctl(handle_info->connect_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 the epoll pool: %s", strerror(errno)); goto exit_error; } info->on_connected_epoll = 0; } exit_error: if (info->connect_sock != -1) { if (_set_fd_tracker(knet_h, info->connect_sock, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, 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; } close(info->connect_sock); info->connect_sock = -1; } errno = savederrno; return err; } static int _enable_sctp_notifications(knet_handle_t knet_h, int sock, const char *type) { int err = 0, savederrno = 0; - struct sctp_event_subscribe events; - - memset(&events, 0, sizeof (events)); - events.sctp_data_io_event = 1; - events.sctp_association_event = 1; - events.sctp_send_failure_event = 1; - events.sctp_address_event = 1; - events.sctp_peer_error_event = 1; - events.sctp_shutdown_event = 1; - if (setsockopt(sock, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof (events)) < 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, 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; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; struct epoll_event ev; if (connect(info->connect_sock, (struct sockaddr *)&kn_link->dst_addr, sockaddr_len(&kn_link->dst_addr)) < 0) { if ((errno != EALREADY) && (errno != EINPROGRESS) && (errno != EISCONN)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to connect SCTP socket %d: %s", info->connect_sock, strerror(savederrno)); goto exit_error; } } if (!info->on_connected_epoll) { memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLOUT; ev.data.fd = info->connect_sock; if (epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_ADD, info->connect_sock, &ev)) { savederrno = errno; err = -1; log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to add send/recv to epoll pool: %s", strerror(savederrno)); goto exit_error; } info->on_connected_epoll = 1; } exit_error: errno = savederrno; return err; } static int _create_connect_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; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; struct epoll_event ev; int connect_sock; 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; } if (_set_fd_tracker(knet_h, connect_sock, KNET_TRANSPORT_SCTP, SCTP_CONNECT_LINK_INFO, 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; } info->connect_sock = connect_sock; info->close_sock = 0; if (_reconnect_socket(knet_h, kn_link) < 0) { savederrno = errno; err = -1; goto exit_error; } exit_error: if (err) { if (info->on_connected_epoll) { epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_DEL, connect_sock, &ev); } if (connect_sock >= 0) { close(connect_sock); } } errno = savederrno; return err; } int sctp_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, 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 -1; } 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 -1; } } break; } if (recv_errno == EAGAIN) { #ifdef DEBUG log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Sock: %d is overloaded. Slowing TX down", sockfd); #endif /* Don't hold onto the lock while sleeping */ pthread_rwlock_unlock(&knet_h->global_rwlock); usleep(KNET_THREADS_TIMERES / 16); pthread_rwlock_rdlock(&knet_h->global_rwlock); return 1; } return -1; } return 0; } /* * 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 diretly (close/reconnect/etc) due * to locking context. We need to delegate that to their respective * management threads within 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 abouse of recv_err is to detect notifications * generated by sockets created by listen(). */ int sctp_transport_rx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) { struct epoll_event ev; 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; 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) { connect_info->link->transport_connected = 0; if (connect_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; } connect_info->on_rx_epoll = 0; } 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 */ /* Don't hold onto the lock while sleeping */ pthread_rwlock_unlock(&knet_h->global_rwlock); usleep(KNET_THREADS_TIMERES / 2); pthread_rwlock_rdlock(&knet_h->global_rwlock); 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 */ int 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 *info = knet_h->knet_transport_fd_tracker[sockfd].data; if (!(msg->msg_hdr.msg_flags & MSG_NOTIFICATION)) { if (msg->msg_len == 0) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "received 0 bytes len packet: %d", sockfd); /* * NOTE: with event notification enabled, we receive error twice: * 1) from the event notification * 2) followed by a 0 byte msg_len * * This is generally not a problem if not for causing extra * handling for the same issue. Should we drop notifications * and keep the code generic (handle all errors via msg_len = 0) * or keep the duplication as safety measure, or drop msg_len = 0 * handling (what about sockets without events enabled?) */ sctp_transport_rx_sock_error(knet_h, sockfd, 1, 0); return 1; } /* * 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(info->mread_buf + info->mread_len, iov->iov_base, msg->msg_len); info->mread_len = info->mread_len + msg->msg_len; return 0; } /* * 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 (info->mread_len) { /* * add last fragment to mread_buf */ memmove(info->mread_buf + info->mread_len, iov->iov_base, msg->msg_len); info->mread_len = info->mread_len + msg->msg_len; /* * move all back into the iovec */ memmove(iov->iov_base, info->mread_buf, info->mread_len); msg->msg_len = info->mread_len; info->mread_len = 0; } return 2; } if (!(msg->msg_hdr.msg_flags & MSG_EOR)) { return 1; } for (i=0; i< iovlen; i++) { snp = iov[i].iov_base; switch (snp->sn_header.sn_type) { case SCTP_ASSOC_CHANGE: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change"); sac = &snp->sn_assoc_change; if (sac->sac_state == SCTP_COMM_LOST) { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp assoc change: comm_lost"); sctp_transport_rx_sock_error(knet_h, sockfd, 2, 0); } break; case SCTP_SHUTDOWN_EVENT: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp shutdown event"); sctp_transport_rx_sock_error(knet_h, sockfd, 2, 0); break; case SCTP_SEND_FAILED: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp send failed"); break; case SCTP_PEER_ADDR_CHANGE: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp peer addr change"); break; case SCTP_REMOTE_ERROR: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] sctp remote error"); break; default: log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "[event] unknown sctp event type: %hu\n", snp->sn_header.sn_type); break; } } 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(knet_handle_t knet_h, int connect_sock) { int err; struct epoll_event ev; unsigned int status, len = sizeof(status); sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; sctp_connect_link_info_t *info = knet_h->knet_transport_fd_tracker[connect_sock].data; struct knet_link *kn_link = 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 (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: %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; } } 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 */ _reconnect_socket(knet_h, info->link); return; } /* * Connected - Remove us from the connect epoll */ memset(&ev, 0, sizeof(struct epoll_event)); ev.events = EPOLLOUT; ev.data.fd = connect_sock; if (epoll_ctl(handle_info->connect_epollfd, EPOLL_CTL_DEL, connect_sock, &ev)) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Unable to remove connected socket %d from epoll pool: %s", connect_sock, strerror(errno)); } info->on_connected_epoll = 0; kn_link->transport_connected = 1; kn_link->outsock = info->connect_sock; 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; 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_errors(knet_handle_t knet_h) { int sockfd = -1; sctp_handle_info_t *handle_info = knet_h->transports[KNET_TRANSPORT_SCTP]; sctp_connect_link_info_t *info; 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; } log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Processing connected error on socket: %d", sockfd); info = knet_h->knet_transport_fd_tracker[sockfd].data; info->close_sock = 1; info->link->transport_connected = 0; _reconnect_socket(knet_h, info->link); } 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_RUNNING); while (!shutdown_in_progress(knet_h)) { nev = epoll_wait(handle_info->connect_epollfd, events, KNET_EPOLL_MAX_EVENTS, KNET_THREADS_TIMERES / 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_errors(knet_h); } else { if (_is_valid_fd(knet_h, events[i].data.fd) == 1) { _handle_connected_sctp(knet_h, events[i].data.fd); } else { log_debug(knet_h, KNET_SUB_TRANSP_SCTP, "Received stray notification for dead fd %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; 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); /* * 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, 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; } _set_fd_tracker(knet_h, new_fd, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, NULL); free(accept_info); 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; } 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); _set_fd_tracker(knet_h, sockfd, KNET_MAX_TRANSPORTS, SCTP_NO_LINK_INFO, NULL); info->accepted_socks[i] = -1; free(accept_info); close(sockfd); } } } 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_RUNNING); while (!shutdown_in_progress(knet_h)) { nev = epoll_wait(handle_info->listen_epollfd, events, KNET_EPOLL_MAX_EVENTS, KNET_THREADS_TIMERES / 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 */ knet_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) { 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, 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 = 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; knet_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->on_listener_epoll) { epoll_ctl(handle_info->listen_epollfd, EPOLL_CTL_DEL, listen_sock, &ev); } if (listen_sock >= 0) { 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) && (host->link[link_idx].status.enabled == 1)) { found = 1; break; } } } 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, 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; } 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, 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; } } knet_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->on_connected_epoll = 0; 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; } knet_list_add(&info->list, &handle_info->connect_links_list); exit_error: if (err) { if (info) { if (info->connect_sock) { 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; struct epoll_event ev; 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 trasport: %s", strerror(savederrno)); goto exit_error; } 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 (_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; } knet_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 (!knet_list_empty(&handle_info->listen_links_list)) { log_err(knet_h, KNET_SUB_TRANSP_SCTP, "Internal error. listen links list is not empty"); } if (!knet_list_empty(&handle_info->connect_links_list)) { 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) { + 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; + } + knet_list_init(&handle_info->listen_links_list); knet_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; + 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; + 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 */ 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; } 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_udp.c b/libknet/transport_udp.c index 25e1ae64..31445023 100644 --- a/libknet/transport_udp.c +++ b/libknet/transport_udp.c @@ -1,433 +1,433 @@ /* * Copyright (C) 2016-2018 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under GPL-2.0+, 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 "transport_common.h" #include "transport_udp.h" #include "threads_common.h" typedef struct udp_handle_info { struct knet_list_head links_list; } udp_handle_info_t; typedef struct udp_link_info { struct knet_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 */ knet_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; } 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, 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; knet_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) && (host->link[link_idx].status.enabled == 1)) { 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, 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); knet_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 (!knet_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; knet_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]; 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 0: /* no origin */ case 1: /* local source (EMSGSIZE) */ if (sock_err->ee_errno == EMSGSIZE) { 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; pthread_mutex_unlock(&knet_h->kmtu_mutex); } /* * 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, KNET_SUB_TRANSP_UDP, "Notifying PMTUd to rerun"); knet_h->pmtud_forcerun = 1; } } pthread_mutex_unlock(&knet_h->pmtud_mutex); } } /* * those errors are way too noisy */ break; case 2: /* ICMP */ case 3: /* 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 { log_debug(knet_h, KNET_SUB_TRANSP_UDP, "Received ICMP error from %s: %s", addr_str, strerror(sock_err->ee_errno)); } 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; } int udp_transport_tx_sock_error(knet_handle_t knet_h, int sockfd, int recv_err, int recv_errno) { if (recv_err < 0) { if (recv_errno == EMSGSIZE) { read_errs_from_sock(knet_h, sockfd); return 0; } - if (recv_errno == EINVAL) { + if (recv_errno == EINVAL || recv_errno == EPERM) { return -1; } 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_THREADS_TIMERES / 16); } else { read_errs_from_sock(knet_h, sockfd); } return 1; } return 0; } int udp_transport_rx_is_data(knet_handle_t knet_h, int sockfd, struct knet_mmsghdr *msg) { if (msg->msg_len == 0) return 0; return 2; } 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; } diff --git a/libknet/transports.c b/libknet/transports.c index 42f169ce..b685212f 100644 --- a/libknet/transports.c +++ b/libknet/transports.c @@ -1,268 +1,272 @@ /* * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, 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, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, -knet_transport_ops_t transport_modules_cmd[] = { +static knet_transport_ops_t transport_modules_cmd[KNET_MAX_TRANSPORTS] = { { "LOOPBACK", KNET_TRANSPORT_LOOPBACK, 1, 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 }, { "UDP", KNET_TRANSPORT_UDP, 1, 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 }, { "SCTP", KNET_TRANSPORT_SCTP, #ifdef HAVE_NETINET_SCTP_H 1, 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 }, #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_type = 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_type].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_type].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 recv_err, int recv_errno) { return transport_modules_cmd[transport].transport_tx_sock_error(knet_h, sockfd, 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); } /* * 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 = savederrno; + 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 = savederrno; + 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 (!knet_h) { errno = EINVAL; 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 (!knet_h) { errno = EINVAL; 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/libnozzle/Makefile.am b/libnozzle/Makefile.am new file mode 100644 index 00000000..3a49d23b --- /dev/null +++ b/libnozzle/Makefile.am @@ -0,0 +1,49 @@ +# +# Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. +# +# Author: Fabio M. Di Nitto +# +# This software licensed under GPL-2.0+, LGPL-2.0+ +# + +MAINTAINERCLEANFILES = Makefile.in + +include $(top_srcdir)/build-aux/check.mk + +SYMFILE = libnozzle_exported_syms + +EXTRA_DIST = $(SYMFILE) + +if BUILD_LIBNOZZLE + +SUBDIRS = . tests + +libversion = 1:0:0 + +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 $(libversion) + +libnozzle_la_LIBADD = $(PTHREAD_LIBS) $(libnl_LIBS) $(libnlroute_LIBS) + +endif diff --git a/libnozzle/internals.c b/libnozzle/internals.c new file mode 100644 index 00000000..c294eb4e --- /dev/null +++ b/libnozzle/internals.c @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, 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; + + 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_ntoa(broadcast)); +} + +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 new file mode 100644 index 00000000..f7bd2e1e --- /dev/null +++ b/libnozzle/internals.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, 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 new file mode 100644 index 00000000..903f36ae --- /dev/null +++ b/libnozzle/libnozzle.c @@ -0,0 +1,1207 @@ +/* + * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, 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 +#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) + close(nozzle->fd); + +#ifdef KNET_BSD + memset(&ifr, 0, sizeof(struct ifreq)); + strncpy(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)); + strncpy(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)); + strncpy(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; + return -1; + } + + 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]; +#endif + + if (devname == NULL) { + errno = EINVAL; + return NULL; + } + + if (devname_size < IFNAMSIZ) { + errno = EINVAL; + return NULL; + } + + 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)) { + for (i = 0; i < 256; i++) { + snprintf(curnozzle, sizeof(curnozzle) - 1, "/dev/tap%u", i); + nozzle->fd = open(curnozzle, O_RDWR); + savederrno = errno; + if (nozzle->fd > 0) { + break; + } + } + snprintf(curnozzle, sizeof(curnozzle) -1 , "tap%u", i); + } else { + 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; + } + strncpy(devname, curnozzle, IFNAMSIZ); + strncpy(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; + } + + strncpy(devname, ifname, IFNAMSIZ); + strncpy(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)); + strncpy(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)); + strncpy(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)); + strncpy(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)); + strncpy(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 new file mode 100644 index 00000000..6baf09ee --- /dev/null +++ b/libnozzle/libnozzle.h @@ -0,0 +1,315 @@ +/* + * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-2.0+ + */ + +#ifndef __LIBNOZZLE_H__ +#define __LIBNOZZLE_H__ + +#include +#include + +/** + * + * @file libnozzle.h + * @brief tap interfaces management API include file + * @copyright Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. + * + * nozzle is a commodity library to manage tap (ethernet) interfaces + */ + +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. It is + * the application responsibility to call helper scripts + * before or after creating/destroying interfaces or IP addresses. + * + * 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. + * + * @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 + +struct nozzle_ip { + char ipaddr[IPADDR_CHAR_MAX + 1]; + char prefix[PREFIX_CHAR_MAX + 1]; + int domain; /* AF_INET or AF_INET6 */ + struct nozzle_ip *next; +}; + +/** + * nozzle_get_ips + * @brief retrive 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 retrive 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 retrive 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 retrive 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/libtap/libtap.pc.in b/libnozzle/libnozzle.pc.in similarity index 89% rename from libtap/libtap.pc.in rename to libnozzle/libnozzle.pc.in index 97340fc5..901e3af5 100644 --- a/libtap/libtap.pc.in +++ b/libnozzle/libnozzle.pc.in @@ -1,19 +1,19 @@ # # Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+, LGPL-2.0+ # prefix=@prefix@ exec_prefix=${prefix} libdir=@libdir@ includedir=${prefix}/include -Name: libtap +Name: libnozzle Version: @VERSION@ Description: library to manage a pool of tap devices Requires: -Libs: -L${libdir} -ltap +Libs: -L${libdir} -lnozzle Cflags: -I${includedir} diff --git a/libtap/libtap_exported_syms b/libnozzle/libnozzle_exported_syms similarity index 74% rename from libtap/libtap_exported_syms rename to libnozzle/libnozzle_exported_syms index cfb3502b..6745c9ba 100644 --- a/libtap/libtap_exported_syms +++ b/libnozzle/libnozzle_exported_syms @@ -1,15 +1,15 @@ -# Version and symbol export for libtap.so +# Version and symbol export for libnozzle.so # # Copyright (C) 2011-2018 Red Hat, Inc. All rights reserved. # # Author: Fabio M. Di Nitto # # This software licensed under GPL-2.0+, LGPL-2.0+ # -LIBTAP { +LIBNOZZLE { global: - tap_*; + nozzle_*; local: *; }; diff --git a/libnozzle/tests/Makefile.am b/libnozzle/tests/Makefile.am new file mode 100644 index 00000000..9a732d52 --- /dev/null +++ b/libnozzle/tests/Makefile.am @@ -0,0 +1,125 @@ +# +# Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. +# +# Author: Fabio M. Di Nitto +# +# This software licensed under GPL-2.0+, LGPL-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) + +check-local: check-api-test-coverage + +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 new file mode 100755 index 00000000..eb7b5efe --- /dev/null +++ b/libnozzle/tests/api-test-coverage @@ -0,0 +1,93 @@ +#!/bin/sh +# +# Copyright (C) 2016-2018 Red Hat, Inc. All rights reserved. +# +# Author: Fabio M. Di Nitto +# +# This software licensed under GPL-2.0+, LGPL-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}')" + +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 + +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%" +} +exit 0 + +exit 0 diff --git a/libnozzle/tests/api_nozzle_add_ip.c b/libnozzle/tests/api_nozzle_add_ip.c new file mode 100644 index 00000000..af8c5f03 --- /dev/null +++ b/libnozzle/tests/api_nozzle_add_ip.c @@ -0,0 +1,293 @@ +/* + * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..129b98c7 --- /dev/null +++ b/libnozzle/tests/api_nozzle_close.c @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..31e26c22 --- /dev/null +++ b/libnozzle/tests/api_nozzle_del_ip.c @@ -0,0 +1,266 @@ +/* + * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..d32da6aa --- /dev/null +++ b/libnozzle/tests/api_nozzle_get_fd.c @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..0abc1bab --- /dev/null +++ b/libnozzle/tests/api_nozzle_get_handle_by_name.c @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..ce31cec5 --- /dev/null +++ b/libnozzle/tests/api_nozzle_get_ips.c @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..81f31427 --- /dev/null +++ b/libnozzle/tests/api_nozzle_get_mac.c @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..2db1a527 --- /dev/null +++ b/libnozzle/tests/api_nozzle_get_mtu.c @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..8e85b832 --- /dev/null +++ b/libnozzle/tests/api_nozzle_get_name_by_handle.c @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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) && (errno != ENOENT)) { + printf("Unable to get name by handle\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(); + + if (test() < 0) + return FAIL; + + return PASS; +} diff --git a/libnozzle/tests/api_nozzle_open.c b/libnozzle/tests/api_nozzle_open.c new file mode 100644 index 00000000..19876987 --- /dev/null +++ b/libnozzle/tests/api_nozzle_open.c @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..ad81c457 --- /dev/null +++ b/libnozzle/tests/api_nozzle_run_updown.c @@ -0,0 +1,401 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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]; + char tmpstr[PATH_MAX*2]; + char srcfile[PATH_MAX]; + char dstfile[PATH_MAX]; + + /* + * create a tmp dir for storing up/down scripts. + * we cannot create symlinks src dir + */ + strcpy(tmpdirsrc, ABSBUILDDIR "/nozzle_test_XXXXXX"); + + 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); + err = execute_bin_sh_command(tmpstr, &error_string); + if (err) { + 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(); + + 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 new file mode 100644 index 00000000..f477a4b2 --- /dev/null +++ b/libnozzle/tests/api_nozzle_set_down.c @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..d7ea405d --- /dev/null +++ b/libnozzle/tests/api_nozzle_set_mac.c @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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, *err_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); + + 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, err_mac) >= 0) || (errno != EINVAL)) { + printf("Something is wrong in nozzle_set_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 (original_mac) + free(original_mac); + + if (nozzle) { + nozzle_close(nozzle); + } + + return err; +} + +int main(void) +{ + need_root(); + + 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 new file mode 100644 index 00000000..b26df345 --- /dev/null +++ b/libnozzle/tests/api_nozzle_set_mtu.c @@ -0,0 +1,297 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..687e6b6b --- /dev/null +++ b/libnozzle/tests/api_nozzle_set_up.c @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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(); + + 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 new file mode 100644 index 00000000..a6a133ff --- /dev/null +++ b/libnozzle/tests/int_execute_bin_sh_command.c @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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/libtap/tap_updown_bad/down.d/kronostest b/libnozzle/tests/nozzle_run_updown_exit_false similarity index 100% rename from libtap/tap_updown_bad/down.d/kronostest rename to libnozzle/tests/nozzle_run_updown_exit_false diff --git a/libtap/tap_updown_good/down.d/kronostest b/libnozzle/tests/nozzle_run_updown_exit_true similarity index 100% rename from libtap/tap_updown_good/down.d/kronostest rename to libnozzle/tests/nozzle_run_updown_exit_true diff --git a/libnozzle/tests/test-common.c b/libnozzle/tests/test-common.c new file mode 100644 index 00000000..752f1707 --- /dev/null +++ b/libnozzle/tests/test-common.c @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Author: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-2.0+ + */ + +#include "config.h" + +#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); + } +} + +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.\n"); + 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] = 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 new file mode 100644 index 00000000..0bfce530 --- /dev/null +++ b/libnozzle/tests/test-common.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 Red Hat, Inc. All rights reserved. + * + * Authors: Fabio M. Di Nitto + * + * This software licensed under GPL-2.0+, LGPL-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); +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/libtap/Makefile.am b/libtap/Makefile.am deleted file mode 100644 index 0690daea..00000000 --- a/libtap/Makefile.am +++ /dev/null @@ -1,60 +0,0 @@ -# -# Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -MAINTAINERCLEANFILES = Makefile.in - -include $(top_srcdir)/build-aux/check.mk - - -SYMFILE = libtap_exported_syms - -EXTRA_DIST = $(SYMFILE) tap_updown_bad tap_updown_good api-test-coverage - -sources = libtap.c - -libversion = 1:0:0 - -AM_CFLAGS += $(PTHREAD_CFLAGS) -LIBS = $(PTHREAD_LIBS) - -if BUILD_LIBTAP - -include_HEADERS = libtap.h - -pkgconfigdir = $(libdir)/pkgconfig - -pkgconfig_DATA = libtap.pc - -lib_LTLIBRARIES = libtap.la - -libtap_la_SOURCES = $(sources) - -EXTRA_libtap_la_DEPENDENCIES = $(SYMFILE) - -libtap_la_LDFLAGS = $(AM_LDFLAGS) \ - -Wl,-version-script,$(srcdir)/$(SYMFILE) \ - -version-info $(libversion) - -TESTS = $(check_PROGRAMS) - -noinst_PROGRAMS = $(check_PROGRAMS) - -check_PROGRAMS = tap_test - -check-local: check-api-test-coverage - -check-api-test-coverage: - chmod u+x $(top_srcdir)/libtap/api-test-coverage - $(top_srcdir)/libtap/api-test-coverage $(top_srcdir) $(top_builddir) - -tap_test_SOURCES = $(sources) - -tap_test_CPPFLAGS = -DTEST \ - -DABSBUILDDIR=\"$(abs_builddir)\" - -endif diff --git a/libtap/api-test-coverage b/libtap/api-test-coverage deleted file mode 100755 index e25cb2b0..00000000 --- a/libtap/api-test-coverage +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh -# -# Copyright (C) 2016-2018 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -srcdir="$1"/libtap -builddir="$2"/libtap - -headerapicalls="$(grep tap_ "$srcdir"/libtap.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}')" - -exportedapicalls="$(nm -B -D "$builddir"/.libs/libtap.so | grep ' T ' | awk '{print $3}')" - -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 - -exit 0 diff --git a/libtap/libtap.c b/libtap/libtap.c deleted file mode 100644 index 060db012..00000000 --- a/libtap/libtap.c +++ /dev/null @@ -1,2125 +0,0 @@ -/* - * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "libtap.h" - -#define MAX_IP_CHAR 128 -#define MAX_PREFIX_CHAR 4 -#define MAX_MAC_CHAR 18 - -struct _ip { - char ip_addr[MAX_IP_CHAR]; - char prefix[MAX_PREFIX_CHAR]; - int domain; - struct _ip *next; -}; - -struct _iface { - struct ifreq ifr; - int fd; - char tapname[IFNAMSIZ]; - char default_mac[MAX_MAC_CHAR]; - int default_mtu; - int current_mtu; - char updownpath[PATH_MAX - 11 - 1 - IFNAMSIZ]; /* 11 = post-down.d 1 = / */ - int hasupdown; - int up; - struct _ip *ip; - struct _iface *next; -}; -#define ifname ifr.ifr_name - -struct _config { - struct _iface *head; - int sockfd; -}; - -static int lib_init = 0; -static struct _config lib_cfg; -static pthread_mutex_t lib_mutex = PTHREAD_MUTEX_INITIALIZER; - -/* forward declarations */ -static int _execute_shell(const char *command, char **error_string); -static int _exec_updown(const tap_t tap, const char *action, char **error_string); -static int _read_pipe(int fd, char **file, size_t *length); -static int _check(const tap_t tap); -static void _close(tap_t tap); -static void _close_cfg(void); -static int _get_mtu(const tap_t tap); -static int _get_mac(const tap_t tap, char **ether_addr); -static int _set_down(tap_t tap, char **error_down, char **error_postdown); -static char *_get_v4_broadcast(const char *ip_addr, const char *prefix); -static int _set_ip(tap_t tap, const char *command, - const char *ip_addr, const char *prefix, - char **error_string); -static int _find_ip(tap_t tap, - const char *ip_addr, const char *prefix, - struct _ip **ip, struct _ip **ip_prev); - -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; -} - -static 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 -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; -} - -static int _exec_updown(const tap_t tap, const char *action, char **error_string) -{ - char command[PATH_MAX]; - struct stat sb; - int err = 0; - - if (!tap->hasupdown) - return 0; - - memset(command, 0, PATH_MAX); - - snprintf(command, PATH_MAX, "%s%s/%s", tap->updownpath, action, tap->tapname); - - err = stat(command, &sb); - if ((err < 0) && (errno == ENOENT)) - return 0; - - err = _execute_shell(command, error_string); - if ((!err) && (*error_string)) { - free(*error_string); - *error_string = NULL; - } - - return err; -} - -static int _check(const tap_t tap) -{ - tap_t temp = lib_cfg.head; - - if (!tap) { - return 0; - } - - while (temp != NULL) { - if (tap == temp) - return 1; - - temp = temp->next; - } - - return 0; -} - -static void _close(tap_t tap) -{ - if (!tap) - return; - - if (tap->fd) - close(tap->fd); - - free(tap); - - return; -} - -static void _close_cfg(void) -{ - if (lib_cfg.head == NULL) { - close(lib_cfg.sockfd); - lib_init = 0; - } -} - -static int _get_mtu(const tap_t tap) -{ - int err; - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - - err = ioctl(lib_cfg.sockfd, SIOCGIFMTU, &tap->ifr); - if (err) - goto out_clean; - - err = tap->ifr.ifr_mtu; - -out_clean: - return err; -} - -static int _get_mac(const tap_t tap, char **ether_addr) -{ - int err; - char mac[MAX_MAC_CHAR]; - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - - err = ioctl(lib_cfg.sockfd, SIOCGIFHWADDR, &tap->ifr); - if (err) - goto out_clean; - - ether_ntoa_r((struct ether_addr *)tap->ifr.ifr_hwaddr.sa_data, mac); - - *ether_addr = strdup(mac); - if (!*ether_addr) - err = -1; - -out_clean: - - return err; -} - -tap_t tap_find(char *dev, size_t dev_size) -{ - tap_t tap; - - if (dev == NULL) { - errno = EINVAL; - return NULL; - } - - if (dev_size < IFNAMSIZ) { - errno = EINVAL; - return NULL; - } - - if (strlen(dev) > IFNAMSIZ) { - errno = E2BIG; - return NULL; - } - - pthread_mutex_lock(&lib_mutex); - - tap = lib_cfg.head; - while (tap != NULL) { - if (!strcmp(dev, tap->tapname)) - break; - tap = tap->next; - } - - pthread_mutex_unlock(&lib_mutex); - return tap; -} - -tap_t tap_open(char *dev, size_t dev_size, const char *updownpath) -{ - tap_t tap; - char *temp_mac = NULL; - - if (dev == NULL) { - errno = EINVAL; - return NULL; - } - - if (dev_size < IFNAMSIZ) { - errno = EINVAL; - return NULL; - } - - if (strlen(dev) > IFNAMSIZ) { - errno = E2BIG; - return NULL; - } - - if (updownpath) { - /* only absolute paths */ - if (updownpath[0] != '/') { - errno = EINVAL; - return NULL; - } - /* 14: 2 for /, 1 for \0 + 11 (post-down.d) */ - if (strlen(updownpath) >= (PATH_MAX - (strlen(dev) + 14))) { - errno = E2BIG; - return NULL; - } - } - - pthread_mutex_lock(&lib_mutex); - - tap = malloc(sizeof(struct _iface)); - if (!tap) - return NULL; - - memset(tap, 0, sizeof(struct _iface)); - - if ((tap->fd = open("/dev/net/tun", O_RDWR)) < 0) - goto out_error; - - strncpy(tap->tapname, dev, IFNAMSIZ); - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, dev, IFNAMSIZ); - tap->ifr.ifr_flags = IFF_TAP | IFF_NO_PI; - - if (ioctl(tap->fd, TUNSETIFF, &tap->ifr) < 0) - goto out_error; - - if ((strlen(dev) > 0) && (strcmp(dev, tap->ifname) != 0)) { - errno = EBUSY; - goto out_error; - } - - strcpy(dev, tap->ifname); - strcpy(tap->tapname, tap->ifname); - - if (!lib_init) { - lib_cfg.head = NULL; - lib_cfg.sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (lib_cfg.sockfd < 0) - goto out_error; - lib_init = 1; - } - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - if (ioctl(lib_cfg.sockfd, SIOGIFINDEX, &tap->ifr) < 0) - goto out_error; - - tap->default_mtu = _get_mtu(tap); - if (tap->default_mtu < 0) - goto out_error; - - if (_get_mac(tap, &temp_mac) < 0) - goto out_error; - - strncpy(tap->default_mac, temp_mac, 18); - free(temp_mac); - - if (updownpath) { - int len = strlen(updownpath); - - strcpy(tap->updownpath, updownpath); - if (tap->updownpath[len-1] != '/') { - tap->updownpath[len] = '/'; - } - tap->hasupdown = 1; - } - - tap->next = lib_cfg.head; - lib_cfg.head = tap; - - pthread_mutex_unlock(&lib_mutex); - return tap; - -out_error: - _close(tap); - _close_cfg(); - pthread_mutex_unlock(&lib_mutex); - return NULL; -} - -// TODO: consider better error report from here -int tap_close(tap_t tap) -{ - int err = 0; - tap_t temp = lib_cfg.head; - tap_t prev = lib_cfg.head; - struct _ip *ip, *ip_next; - char *error_string = NULL; - char *error_down = NULL, *error_postdown = NULL; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - while ((temp) && (temp != tap)) { - prev = temp; - temp = temp->next; - } - - if (tap == prev) { - lib_cfg.head = tap->next; - } else { - prev->next = tap->next; - } - - _set_down(tap, &error_down, &error_postdown); - if (error_down) - free(error_down); - if (error_postdown) - free(error_postdown); - - ip = tap->ip; - while (ip) { - ip_next = ip->next; - _set_ip(tap, "del", ip->ip_addr, ip->prefix, &error_string); - if (error_string) { - free(error_string); - error_string = NULL; - } - free(ip); - ip = ip_next; - } - - _close(tap); - _close_cfg(); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_get_mtu(const tap_t tap) -{ - int err; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - err = _get_mtu(tap); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_set_mtu(tap_t tap, const int mtu) -{ - struct _ip *tmp_ip; - char *error_string = NULL; - int err; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - err = tap->current_mtu = _get_mtu(tap); - if (err < 0) - goto out_clean; - - tap->ifr.ifr_mtu = mtu; - - err = ioctl(lib_cfg.sockfd, SIOCSIFMTU, &tap->ifr); - - if ((!err) && (tap->current_mtu < 1280) && (mtu >= 1280)) { - tmp_ip = tap->ip; - while(tmp_ip) { - if (tmp_ip->domain == AF_INET6) { - err = _set_ip(tap, "add", tmp_ip->ip_addr, tmp_ip->prefix, &error_string); - if (error_string) { - free(error_string); - error_string = NULL; - } - } - tmp_ip = tmp_ip->next; - } - } - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_reset_mtu(tap_t tap) -{ - return tap_set_mtu(tap, tap->default_mtu); -} - -int tap_get_mac(const tap_t tap, char **ether_addr) -{ - int err; - - pthread_mutex_lock(&lib_mutex); - - if ((!_check(tap)) || (!ether_addr)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - err = _get_mac(tap, ether_addr); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_set_mac(tap_t tap, const char *ether_addr) -{ - int err; - - pthread_mutex_lock(&lib_mutex); - - if ((!_check(tap)) || (!ether_addr)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - err = ioctl(lib_cfg.sockfd, SIOCGIFHWADDR, &tap->ifr); - if (err) - goto out_clean; - - memmove(tap->ifr.ifr_hwaddr.sa_data, ether_aton(ether_addr), ETH_ALEN); - - err = ioctl(lib_cfg.sockfd, SIOCSIFHWADDR, &tap->ifr); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_reset_mac(tap_t tap) -{ - return tap_set_mac(tap, tap->default_mac); -} - -int tap_set_up(tap_t tap, char **error_preup, char **error_up) -{ - int err = 0; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - if ((tap->hasupdown) && ((!error_preup) || (!error_up))) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - if (tap->up) - goto out_clean; - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - - err=ioctl(lib_cfg.sockfd, SIOCGIFFLAGS, &tap->ifr); - if (err) - goto out_clean; - - _exec_updown(tap, "pre-up.d", error_preup); - - tap->ifr.ifr_flags |= IFF_UP | IFF_RUNNING; - err=ioctl(lib_cfg.sockfd, SIOCSIFFLAGS, &tap->ifr); - - if (err) - goto out_clean; - - _exec_updown(tap, "up.d", error_up); - - tap->up = 1; -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -static int _set_down(tap_t tap, char **error_down, char **error_postdown) -{ - int err = 0; - - if (!tap->up) - goto out_clean; - - memset(&tap->ifr, 0, sizeof(struct ifreq)); - strncpy(tap->ifname, tap->tapname, IFNAMSIZ); - - err=ioctl(lib_cfg.sockfd, SIOCGIFFLAGS, &tap->ifr); - if (err) - goto out_clean; - - _exec_updown(tap, "down.d", error_down); - - tap->ifr.ifr_flags &= ~IFF_UP; - err=ioctl(lib_cfg.sockfd, SIOCSIFFLAGS, &tap->ifr); - - if (err) - goto out_clean; - - _exec_updown(tap, "post-down.d", error_postdown); - - tap->up = 0; - -out_clean: - return err; -} - -int tap_set_down(tap_t tap, char **error_down, char **error_postdown) -{ - int err = 0; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - if ((tap->hasupdown) && ((!error_down) || (!error_postdown))) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - err = _set_down(tap, error_down, error_postdown); - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -static char *_get_v4_broadcast(const char *ip_addr, const char *prefix) -{ - int prefix_len; - struct in_addr mask; - struct in_addr broadcast; - struct in_addr address; - - prefix_len = atoi(prefix); - - if ((prefix_len > 32) || (prefix_len < 0)) - return NULL; - - if (inet_pton(AF_INET, ip_addr, &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_ntoa(broadcast)); -} - -static int _set_ip(tap_t tap, const char *command, - const char *ip_addr, const char *prefix, - char **error_string) -{ - char *broadcast = NULL; - char cmdline[4096]; - - if (!strchr(ip_addr, ':')) { - broadcast = _get_v4_broadcast(ip_addr, prefix); - if (!broadcast) { - errno = EINVAL; - return -1; - } - } - - memset(cmdline, 0, sizeof(cmdline)); - - if (broadcast) { - snprintf(cmdline, sizeof(cmdline)-1, - "ip addr %s %s/%s dev %s broadcast %s", - command, ip_addr, prefix, - tap->tapname, broadcast); - free(broadcast); - } else { - snprintf(cmdline, sizeof(cmdline)-1, - "ip addr %s %s/%s dev %s", - command, ip_addr, prefix, - tap->tapname); - } - - return _execute_shell(cmdline, error_string); -} - -static int _find_ip(tap_t tap, - const char *ip_addr, const char *prefix, - struct _ip **ip, struct _ip **ip_prev) -{ - struct _ip *local_ip, *local_ip_prev; - int found = 0; - - local_ip = local_ip_prev = tap->ip; - - while(local_ip) { - if ((!strcmp(local_ip->ip_addr, ip_addr)) && (!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; -} - -int tap_add_ip(tap_t tap, const char *ip_addr, const char *prefix, char **error_string) -{ - int err = 0, found; - struct _ip *ip = NULL, *ip_prev = NULL, *ip_last = NULL; - - pthread_mutex_lock(&lib_mutex); - - if ((!_check(tap)) || (!ip_addr) || (!prefix) || (!error_string)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - found = _find_ip(tap, ip_addr, prefix, &ip, &ip_prev); - if (found) - goto out_clean; - - ip = malloc(sizeof(struct _ip)); - if (!ip) { - err = -1 ; - goto out_clean; - } - memset(ip, 0, sizeof(struct _ip)); - strncpy(ip->ip_addr, ip_addr, MAX_IP_CHAR); - strncpy(ip->prefix, prefix, MAX_PREFIX_CHAR); - if (!strchr(ip->ip_addr, ':')) { - 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_mtu(tap) < 1280)) { - err = 0; - } else { - err = _set_ip(tap, "add", ip_addr, prefix, error_string); - } - - if (err) { - free(ip); - goto out_clean; - } - - if (tap->ip) { - ip_last = tap->ip; - while (ip_last->next != NULL) { - ip_last = ip_last->next; - } - ip_last->next = ip; - } else { - tap->ip = ip; - } - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_del_ip(tap_t tap, const char *ip_addr, const char *prefix, char **error_string) -{ - int err = 0, found; - struct _ip *ip = NULL, *ip_prev = NULL; - - pthread_mutex_lock(&lib_mutex); - - if ((!_check(tap)) || (!ip_addr) || (!prefix) || (!error_string)) { - errno = EINVAL; - err = -1; - goto out_clean; - } - - found = _find_ip(tap, ip_addr, prefix, &ip, &ip_prev); - if (!found) - goto out_clean; - - err = _set_ip(tap, "del", ip_addr, prefix, error_string); - - if (!err) { - if (ip == ip_prev) { - tap->ip = ip->next; - } else { - ip_prev->next = ip->next; - } - free(ip); - } - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -int tap_get_fd(const tap_t tap) -{ - int fd; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - fd = -1; - goto out_clean; - } - - fd = tap->fd; - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return fd; -} - -const char *tap_get_name(const tap_t tap) -{ - char *name = NULL; - - pthread_mutex_lock(&lib_mutex); - - if (!_check(tap)) { - errno = EINVAL; - goto out_clean; - } - - name = tap->tapname; - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return name; -} - -int tap_get_ips(const tap_t tap, char **ip_addr_list, int *entries) -{ - int err = 0; - int found = 0; - char *ip_list = NULL; - int size = 0, offset = 0, len; - struct _ip *ip = tap->ip; - - pthread_mutex_lock(&lib_mutex); - - while (ip) { - found++; - ip = ip->next; - } - - size = found * (MAX_IP_CHAR + MAX_PREFIX_CHAR + 2); - ip_list = malloc(size); - if (!ip_list) { - err = -1; - goto out_clean; - } - - memset(ip_list, 0, size); - - ip = tap->ip; - - while (ip) { - len = strlen(ip->ip_addr); - memmove(ip_list + offset, ip->ip_addr, len); - offset = offset + len + 1; - len = strlen(ip->prefix); - memmove(ip_list + offset, ip->prefix, len); - offset = offset + len + 1; - ip = ip->next; - } - - *ip_addr_list = ip_list; - *entries = found; - -out_clean: - pthread_mutex_unlock(&lib_mutex); - - return err; -} - -#ifdef TEST -static 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; -} - -static int test_iface(char *name, size_t size, const char *updownpath) -{ - tap_t tap; - - tap=tap_open(name, size, updownpath); - if (!tap) { - if (lib_cfg.sockfd < 0) - printf("Unable to open knet_socket\n"); - printf("Unable to open knet.\n"); - 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 (!tap_find(name, size)) { - printf("Unable to find interface %s in tap db\n", name); - } else { - printf("Found interface %s in tap db\n", name); - } - - tap_close(tap); - - if (is_if_in_system(name) == 0) - printf("Successfully removed interface %s from the system\n", name); - - return 0; -} - -static int check_tap_open_close(void) -{ - char device_name[2*IFNAMSIZ]; - char fakepath[PATH_MAX]; - size_t size = IFNAMSIZ; - - memset(device_name, 0, sizeof(device_name)); - - printf("Creating random tap interface:\n"); - if (test_iface(device_name, size, NULL) < 0) { - printf("Unable to create random interface\n"); - return -1; - } - - printf("Creating kronostest tap interface:\n"); - strncpy(device_name, "kronostest", IFNAMSIZ); - if (test_iface(device_name, size, NULL) < 0) { - printf("Unable to create kronosnet interface\n"); - return -1; - } - - 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 tap_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 tap_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 tap_open sanity checks\n"); - return -1; - } - - printf("Testing updown path != abs\n"); - errno=0; - strcpy(device_name, "kronostest"); - if ((test_iface(device_name, IFNAMSIZ, "foo") >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_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; - strcpy(device_name, "kronostest"); - if ((test_iface(device_name, IFNAMSIZ, fakepath) >= 0) || (errno != E2BIG)) { - printf("Something is wrong in tap_open sanity checks\n"); - return -1; - } - - return 0; -} - -static int check_knet_multi_eth(void) -{ - char device_name1[IFNAMSIZ]; - char device_name2[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap1 = NULL; - tap_t tap2 = NULL; - - printf("Testing multiple knet interface instances\n"); - - memset(device_name1, 0, size); - memset(device_name2, 0, size); - - strncpy(device_name1, "kronostest1", size); - strncpy(device_name2, "kronostest2", size); - - tap1 = tap_open(device_name1, size, NULL); - if (!tap1) { - 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); - } - - tap2 = tap_open(device_name2, size, NULL); - if (!tap2) { - 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 (tap1) - tap_close(tap1); - if (tap2) - tap_close(tap2); - - printf("Testing error conditions\n"); - - printf("Open same device twice\n"); - - tap1 = tap_open(device_name1, size, NULL); - if (!tap1) { - 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); - } - - tap2 = tap_open(device_name1, size, NULL); - if (tap2) { - printf("We were able to init 2 interfaces with the same name!\n"); - err = -1; - goto out_clean; - } - -out_clean: - if (tap1) - tap_close(tap1); - if (tap2) - tap_close(tap2); - return err; -} - -static int check_knet_mtu(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - - int current_mtu = 0; - int expected_mtu = 1500; - - printf("Testing get/set MTU\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Comparing default MTU\n"); - current_mtu = tap_get_mtu(tap); - 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 (tap_set_mtu(tap, expected_mtu) < 0) { - printf("Unable to set MTU to %d\n", expected_mtu); - err = -1; - goto out_clean; - } - - current_mtu = tap_get_mtu(tap); - 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 (tap_get_mtu(NULL) > 0) { - printf("Something is wrong in tap_get_mtu sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Passing empty struct to set_mtu\n"); - if (tap_set_mtu(NULL, 1500) == 0) { - printf("Something is wrong in tap_set_mtu sanity checks\n"); - err = -1; - goto out_clean; - } - -out_clean: - tap_close(tap); - - return err; -} - -static int check_knet_mtu_ipv6(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *error_string = NULL; - - printf("Testing get/set MTU with IPv6 address\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Adding ip: 3ffe::1/64\n"); - - err = tap_add_ip(tap, "3ffe::1", "64", &error_string); - if (error_string) { - printf("add ipv6 output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &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 (tap_set_mtu(tap, 1200) < 0) { - printf("Unable to set MTU to 1200\n"); - err = -1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &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("Adding ip: 3ffe::2/64\n"); - err = tap_add_ip(tap, "3ffe::2", "64", &error_string); - if (error_string) { - printf("add ipv6 output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::2/64", &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 (tap_reset_mtu(tap) < 0) { - printf("Unable to reset mtu\n"); - err = -1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &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; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::2/64", &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: - tap_close(tap); - - return err; -} - -static int check_knet_mac(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *current_mac = NULL, *temp_mac = NULL, *err_mac = NULL; - struct ether_addr *cur_mac, *tmp_mac; - - printf("Testing get/set MAC\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Get current MAC\n"); - - if (tap_get_mac(tap, ¤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 (tap_set_mac(tap, "00:01:01:01:01:01") < 0) { - printf("Unable to set current MAC address.\n"); - err = -1; - goto out_clean; - } - - if (tap_get_mac(tap, &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 ((tap_get_mac(NULL, &err_mac) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_get_mac sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to get_mac (pass2)\n"); - errno = 0; - if ((tap_get_mac(tap, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_get_mac sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to set_mac (pass1)\n"); - errno = 0; - if ((tap_set_mac(tap, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_mac sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to set_mac (pass2)\n"); - errno = 0; - if ((tap_set_mac(NULL, err_mac) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_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); - - tap_close(tap); - - return err; -} - -static int check_tap_execute_shell(void) -{ - int err = 0; - char command[4096]; - char *error_string = NULL; - - memset(command, 0, sizeof(command)); - - printf("Testing _execute_shell\n"); - - printf("command /bin/true\n"); - - err = _execute_shell("/bin/true", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to execute /bin/true ?!?!\n"); - goto out_clean; - } - - printf("Testing ERROR conditions\n"); - - printf("command /bin/false\n"); - - err = _execute_shell("/bin/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 /bin/false successfully?!?!\n"); - err = -1; - goto out_clean; - } - - printf("command that outputs to stdout (enforcing redirect)\n"); - - err = _execute_shell("/bin/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 /bin/grep -h successfully?!?\n"); - err = -1; - goto out_clean; - } - - printf("command that outputs to stderr\n"); - err = _execute_shell("/bin/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 /bin/grep -h successfully?!?\n"); - err = -1; - goto out_clean; - } - - printf("empty command\n"); - err = _execute_shell(NULL, &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 (nil) successfully?!?!\n"); - err = -1; - goto out_clean; - } - - printf("empty error\n"); - err = _execute_shell("/bin/true", NULL); - if (!err) { - printf("Check EINVAL filter for no error_string!\n"); - err = -1; - goto out_clean; - } - - err = 0; - -out_clean: - - return err; -} - -static int check_knet_up_down(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *error_string = NULL; - char *error_preup = NULL, *error_up = NULL; - char *error_down = NULL, *error_postdown = NULL; - - printf("Testing interface up/down\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Put the interface up\n"); - - err = tap_set_up(tap, &error_preup, &error_up); - if (error_preup) { - printf("preup output: %s\n", error_preup); - free(error_preup); - error_preup = NULL; - } - if (error_up) { - printf("up output: %s\n", error_up); - free(error_up); - error_up = NULL; - } - if (err < 0) { - printf("Unable to set interface up\n"); - err = -1; - goto out_clean; - } - - - err = _execute_shell("ip addr show dev kronostest | grep -q UP", &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 = tap_set_down(tap, &error_down, &error_postdown); - if (error_down) { - printf("down output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (error_postdown) { - printf("postdown output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (err < 0) { - printf("Unable to put the interface down\n"); - err = -1; - goto out_clean; - } - - err = _execute_shell("ifconfig kronostest | grep -q UP", &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; - } - - tap_close(tap); - - printf("Testing interface pre-up/up/down/post-down (exec errors)\n"); - - tap = tap_open(device_name, size, ABSBUILDDIR "/tap_updown_bad"); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Put the interface up\n"); - - err = tap_set_up(tap, &error_preup, &error_up); - if (error_preup) { - printf("preup output: %s\n", error_preup); - free(error_preup); - error_preup = NULL; - } - if (error_up) { - printf("up output: %s\n", error_up); - free(error_up); - error_up = NULL; - } - if (err < 0) { - printf("Unable to set interface up\n"); - err = -1; - goto out_clean; - } - - printf("Put the interface down\n"); - - err = tap_set_down(tap, &error_down, &error_postdown); - if (error_down) { - printf("down output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (error_postdown) { - printf("postdown output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (err < 0) { - printf("Unable to put the interface down\n"); - err = -1; - goto out_clean; - } - - tap_close(tap); - - printf("Testing interface pre-up/up/down/post-down\n"); - - tap = tap_open(device_name, size, ABSBUILDDIR "/tap_updown_good"); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Put the interface up\n"); - - err = tap_set_up(tap, &error_preup, &error_up); - if (error_preup) { - printf("preup output: %s\n", error_preup); - free(error_preup); - error_preup = NULL; - } - if (error_up) { - printf("up output: %s\n", error_up); - free(error_up); - error_up = NULL; - } - if (err < 0) { - printf("Unable to set interface up\n"); - err = -1; - goto out_clean; - } - - printf("Put the interface down\n"); - - err = tap_set_down(tap, &error_down, &error_postdown); - if (error_down) { - printf("down output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (error_postdown) { - printf("postdown output: %s\n", error_down); - free(error_down); - error_down = NULL; - } - if (err < 0) { - printf("Unable to put the interface down\n"); - err = -1; - goto out_clean; - } - - tap_close(tap); - - printf("Test ERROR conditions\n"); - - printf("Pass NULL to tap set_up\n"); - errno = 0; - if ((tap_set_up(NULL, &error_preup, &error_up) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_up sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to error_preup set_up\n"); - errno = 0; - if ((tap_set_up(tap, NULL, &error_up) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_up sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to error_up set_up\n"); - errno = 0; - if ((tap_set_up(tap, &error_preup, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_up sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to tap set_down\n"); - errno = 0; - if ((tap_set_down(NULL, &error_down, &error_postdown) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_down sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to error_down set_down\n"); - errno = 0; - if ((tap_set_down(tap, NULL, &error_postdown) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_down sanity checks\n"); - err = -1; - goto out_clean; - } - - printf("Pass NULL to error_postdown set_down\n"); - errno = 0; - if ((tap_set_down(tap, &error_down, NULL) >= 0) || (errno != EINVAL)) { - printf("Something is wrong in tap_set_down sanity checks\n"); - err = -1; - goto out_clean; - } - -out_clean: - - tap_close(tap); - - return err; -} - -static int check_knet_close_leak(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *error_string = NULL; - - printf("Testing close leak (needs valgrind)\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Adding ip: 192.168.168.168/24\n"); - - err = tap_add_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding ip: 192.168.169.169/24\n"); - - err = tap_add_ip(tap, "192.168.169.169", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - -out_clean: - - tap_close(tap); - - return err; -} - -static int check_knet_set_del_ip(void) -{ - char device_name[IFNAMSIZ]; - size_t size = IFNAMSIZ; - int err=0; - tap_t tap; - char *ip_list = NULL; - int ip_list_entries = 0, i, offset = 0; - char *error_string = NULL; - - printf("Testing interface add/remove ip\n"); - - memset(device_name, 0, size); - strncpy(device_name, "kronostest", size); - tap = tap_open(device_name, size, NULL); - if (!tap) { - printf("Unable to init %s\n", device_name); - return -1; - } - - printf("Adding ip: 192.168.168.168/24\n"); - - err = tap_add_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding ip: 192.168.169.169/24\n"); - - err = tap_add_ip(tap, "192.168.169.169", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - printf("Adding duplicate ip: 192.168.168.168/24\n"); - - err = tap_add_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("add ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to find IP address in libtap db\n"); - err=-1; - goto out_clean; - } - - printf("Checking ip: 192.168.168.168/24\n"); - - err = _execute_shell("ip addr show dev kronostest | grep -q 192.168.168.168/24", &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("Get ip list from libtap:\n"); - - if (tap_get_ips(tap, &ip_list, &ip_list_entries) < 0) { - printf("Not enough mem?\n"); - err=-1; - goto out_clean; - } - - if (ip_list_entries != 2) { - printf("Didn't get enough ip back from libtap?\n"); - err=-1; - goto out_clean; - } - - for (i = 1; i <= ip_list_entries; i++) { - printf("Found IP %s %s in libtap db\n", ip_list + offset, ip_list + offset + strlen(ip_list + offset) + 1); - offset = offset + strlen(ip_list) + 1; - offset = offset + strlen(ip_list + offset) + 1; - } - - free(ip_list); - - printf("Deleting ip: 192.168.168.168/24\n"); - - err = tap_del_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("del ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - printf("Deleting ip: 192.168.169.169/24\n"); - - err = tap_del_ip(tap, "192.168.169.169", "24", &error_string); - if (error_string) { - printf("del ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - printf("Deleting again ip: 192.168.168.168/24\n"); - - err = tap_del_ip(tap, "192.168.168.168", "24", &error_string); - if (error_string) { - printf("del ip output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 192.168.168.168/24", &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("Adding ip: 3ffe::1/64\n"); - - err = tap_add_ip(tap, "3ffe::1", "64", &error_string); - if (error_string) { - printf("add ipv6 output: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err < 0) { - printf("Unable to assign IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &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: 3ffe::1/64\n"); - - err = tap_del_ip(tap, "3ffe::1", "64", &error_string); - if (error_string) { - printf("Error string: %s\n", error_string); - free(error_string); - error_string = NULL; - } - if (err) { - printf("Unable to delete IP address\n"); - err=-1; - goto out_clean; - } - - err = _execute_shell("ip addr show dev kronostest | grep -q 3ffe::1/64", &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: - - tap_close(tap); - - return err; -} - -int main(void) -{ - if (geteuid() != 0) { - printf("This test requires root privileges\n"); - exit(77); - } - - if (check_tap_open_close() < 0) - return -1; - - if (check_knet_multi_eth() < 0) - return -1; - - if (check_knet_mtu() < 0) - return -1; - - if (check_knet_mtu_ipv6() < 0) - return -1; - - if (check_knet_mac() < 0) - return -1; - - if (check_tap_execute_shell() < 0) - return -1; - - if (check_knet_up_down() < 0) - return -1; - - if (check_knet_set_del_ip() < 0) - return -1; - - if (check_knet_close_leak() < 0) - return -1; - - return 0; -} -#endif diff --git a/libtap/libtap.h b/libtap/libtap.h deleted file mode 100644 index 124c3292..00000000 --- a/libtap/libtap.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. - * - * Author: Fabio M. Di Nitto - * - * This software licensed under GPL-2.0+, LGPL-2.0+ - */ - -#ifndef __LIBTAP_H__ -#define __LIBTAP_H__ - -#include - -typedef struct _iface *tap_t; - -tap_t tap_open(char *dev, size_t dev_size, const char *updownpath); -int tap_close(tap_t tap); - -tap_t tap_find(char *dev, size_t dev_size); - -int tap_get_fd(const tap_t tap); - -const char *tap_get_name(const tap_t tap); - -int tap_get_mtu(const tap_t tap); -int tap_set_mtu(tap_t tap, const int mtu); -int tap_reset_mtu(tap_t tap); - -int tap_get_mac(const tap_t tap, char **ether_addr); -int tap_set_mac(tap_t tap, const char *ether_addr); -int tap_reset_mac(tap_t tap); - -int tap_set_up(tap_t tap, char **error_preup, char **error_up); -int tap_set_down(tap_t tap, char **error_down, char **error_postdown); - -int tap_add_ip(tap_t tap, const char *ip_addr, const char *prefix, char **error_string); -int tap_del_ip(tap_t tap, const char *ip_addr, const char *prefix, char **error_string); -int tap_get_ips(const tap_t tap, char **ip_addr_list, int *entries); - -#endif diff --git a/libtap/tap_updown_bad/post-down.d/kronostest b/libtap/tap_updown_bad/post-down.d/kronostest deleted file mode 100755 index 721ecb6e..00000000 --- a/libtap/tap_updown_bad/post-down.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 1 diff --git a/libtap/tap_updown_bad/pre-up.d/kronostest b/libtap/tap_updown_bad/pre-up.d/kronostest deleted file mode 100755 index 721ecb6e..00000000 --- a/libtap/tap_updown_bad/pre-up.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 1 diff --git a/libtap/tap_updown_bad/up.d/kronostest b/libtap/tap_updown_bad/up.d/kronostest deleted file mode 100755 index 721ecb6e..00000000 --- a/libtap/tap_updown_bad/up.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 1 diff --git a/libtap/tap_updown_good/post-down.d/kronostest b/libtap/tap_updown_good/post-down.d/kronostest deleted file mode 100755 index 6e28da18..00000000 --- a/libtap/tap_updown_good/post-down.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 0 diff --git a/libtap/tap_updown_good/pre-up.d/kronostest b/libtap/tap_updown_good/pre-up.d/kronostest deleted file mode 100755 index 6e28da18..00000000 --- a/libtap/tap_updown_good/pre-up.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 0 diff --git a/libtap/tap_updown_good/up.d/kronostest b/libtap/tap_updown_good/up.d/kronostest deleted file mode 100755 index 6e28da18..00000000 --- a/libtap/tap_updown_good/up.d/kronostest +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -# -# Copyright (C) 2010-2018 Red Hat, Inc. All rights reserved. -# -# Author: Fabio M. Di Nitto -# -# This software licensed under GPL-2.0+, LGPL-2.0+ -# - -exit 0 diff --git a/man/Doxyfile-nozzle.in b/man/Doxyfile-nozzle.in new file mode 100644 index 00000000..0fb6f333 --- /dev/null +++ b/man/Doxyfile-nozzle.in @@ -0,0 +1,17 @@ +# +# Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. +# +# Author: Fabio M. Di Nitto +# Christine Caulfield +# +# This software licensed under GPL-2.0+, LGPL-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 9f2741b1..29cd45e7 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -1,111 +1,122 @@ # # Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # Federico Simoncelli # # This software licensed under GPL-2.0+, LGPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk EXTRA_DIST = kronosnetd.8 knet-keygen.8 # Avoid Automake warnings about overriding these user variables. # Programs in this directory are used during the build only. AUTOMAKE_OPTIONS = -Wno-gnu EXEEXT=$(BUILD_EXEEXT) CC=$(CC_FOR_BUILD) CFLAGS=$(CFLAGS_FOR_BUILD) CPPFLAGS=$(CPPFLAGS_FOR_BUILD) LDFLAGS=$(LDFLAGS_FOR_BUILD) if BUILD_MAN if BUILD_KRONOSNETD man8_MANS = kronosnetd.8 knet-keygen.8 endif noinst_PROGRAMS = doxyxml doxyxml_SOURCES = doxyxml.c doxyxml_CFLAGS = $(AM_CFLAGS) $(libqb_BUILD_CFLAGS) $(libxml_BUILD_CFLAGS) doxyxml_LDADD = $(libqb_BUILD_LIBS) $(libxml_BUILD_LIBS) knet_man3_MANS = \ knet_addrtostr.3 \ knet_handle_add_datafd.3 \ knet_handle_clear_stats.3 \ knet_handle_compress.3 \ knet_handle_crypto.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_new_ex.3 \ knet_handle_pmtud_get.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 -man3_MANS = $(knet_man3_MANS) +if BUILD_LIBNOZZLE +nozzle_man3_MANS = +endif + +man3_MANS = $(knet_man3_MANS) $(nozzle_man3_MANS) -$(MANS): doxyfile-knet.stamp +$(MANS): doxyfile-knet.stamp doxyfile-nozzle.stamp doxyfile-knet.stamp: $(builddir)/doxyxml Doxyfile-knet $(top_srcdir)/libknet/libknet.h $(DOXYGEN) Doxyfile-knet $(builddir)/doxyxml -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: $(builddir)/doxyxml Doxyfile-nozzle $(top_srcdir)/libnozzle/libnozzle.h +if BUILD_LIBNOZZLE + $(DOXYGEN) Doxyfile-nozzle + $(builddir)/doxyxml -m -P -o $(builddir) -s 3 -p @PACKAGE_NAME@ -H "Kronosnet Programmer's Manual" -d $(builddir)/xml-nozzle/ libnozzle_8h.xml +endif + touch doxyfile-nozzle.stamp + endif clean-local: rm -rf doxyfile*.stamp xml* *.3 diff --git a/man/doxyxml.c b/man/doxyxml.c index fdd2d072..9f493b26 100644 --- a/man/doxyxml.c +++ b/man/doxyxml.c @@ -1,856 +1,856 @@ /* * Copyright (C) 2018 Red Hat, Inc. All rights reserved. * * Author: Christine Caulfield * * This software licensed under GPL-2.0+, LGPL-2.0+ */ /* * NOTE: this code is very rough, it does the bare minimum to parse the * XML out from doxygen and is probably very fragile to changes in that XML * schema. It probably leaks memory all over the place too. * * In its favour, it *does* generate man pages and should only be run very ocasionally */ #define _DEFAULT_SOURCE #define _BSD_SOURCE #define _XOPEN_SOURCE #define _XOPEN_SOURCE_EXTENDED #include #include #include #include #include #include #include #include #include #include #include -#define XML_DIR "../../libknet/man/xml" +#define XML_DIR "../man/xml-knet" #define XML_FILE "libknet_8h.xml" static int print_ascii = 1; static int print_man = 0; static int print_params = 0; static int num_functions = 0; static const char *man_section="3"; static const char *package_name="Kronosnet"; static const char *header="Kronosnet Programmer's Manual"; static const char *output_dir="./"; static const char *xml_dir = XML_DIR; static const char *xml_file = XML_FILE; static const char *manpage_date = NULL; static long manpage_year = LONG_MIN; static struct qb_list_head params_list; static struct qb_list_head retval_list; static qb_map_t *function_map; static qb_map_t *structures_map; static qb_map_t *used_structures_map; struct param_info { char *paramname; char *paramtype; char *paramdesc; struct param_info *next; struct qb_list_head list; }; struct struct_info { char *structname; struct qb_list_head params_list; /* our params */ struct qb_list_head list; }; static char *get_texttree(int *type, xmlNode *cur_node, char **returntext); static void traverse_node(xmlNode *parentnode, const char *leafname, void (do_members(xmlNode*, void*)), void *arg); static void free_paraminfo(struct param_info *pi) { free(pi->paramname); free(pi->paramtype); free(pi->paramdesc); free(pi); } static char *get_attr(xmlNode *node, const char *tag) { xmlAttr *this_attr; for (this_attr = node->properties; this_attr; this_attr = this_attr->next) { if (this_attr->type == XML_ATTRIBUTE_NODE && strcmp((char *)this_attr->name, tag) == 0) { return strdup((char *)this_attr->children->content); } } return NULL; } static char *get_child(xmlNode *node, const char *tag) { xmlNode *this_node; xmlNode *child; char buffer[1024] = {'\0'}; char *refid = NULL; char *declname = NULL; for (this_node = node->children; this_node; this_node = this_node->next) { if ((strcmp( (char*)this_node->name, "declname") == 0)) { declname = strdup((char*)this_node->children->content); } if ((this_node->type == XML_ELEMENT_NODE && this_node->children) && ((strcmp((char *)this_node->name, tag) == 0))) { refid = NULL; for (child = this_node->children; child; child = child->next) { if (child->content) { strcat(buffer, (char *)child->content); } if ((strcmp( (char*)child->name, "ref") == 0)) { if (child->children->content) { strcat(buffer,(char *)child->children->content); } refid = get_attr(child, "refid"); } } } if (declname && refid) { qb_map_put(used_structures_map, refid, declname); } } return strdup(buffer); } static struct param_info *find_param_by_name(struct qb_list_head *list, const char *name) { struct qb_list_head *iter; struct param_info *pi; qb_list_for_each(iter, list) { pi = qb_list_entry(iter, struct param_info, list); if (strcmp(pi->paramname, name) == 0) { return pi; } } return NULL; } static int not_all_whitespace(char *string) { unsigned int i; for (i=0; ichildren; this_tag; this_tag = this_tag->next) { for (sub_tag = this_tag->children; sub_tag; sub_tag = sub_tag->next) { if (sub_tag->type == XML_ELEMENT_NODE && strcmp((char *)sub_tag->name, "parameternamelist") == 0) { paramname = (char*)sub_tag->children->next->children->content; } if (sub_tag->type == XML_ELEMENT_NODE && strcmp((char *)sub_tag->name, "parameterdescription") == 0) { paramdesc = (char*)sub_tag->children->next->children->content; /* Add text to the param_map */ pi = find_param_by_name(list, paramname); if (pi) { pi->paramdesc = paramdesc; } else { pi = malloc(sizeof(struct param_info)); if (pi) { pi->paramname = paramname; pi->paramdesc = paramdesc; pi->paramtype = NULL; /* retval */ qb_list_add_tail(&pi->list, list); } } } } } } static char *get_text(xmlNode *cur_node, char **returntext) { xmlNode *this_tag; xmlNode *sub_tag; char *kind; char buffer[4096] = {'\0'}; for (this_tag = cur_node->children; this_tag; this_tag = this_tag->next) { if (this_tag->type == XML_TEXT_NODE && strcmp((char *)this_tag->name, "text") == 0) { if (not_all_whitespace((char*)this_tag->content)) { strcat(buffer, (char*)this_tag->content); strcat(buffer, "\n"); } } if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "emphasis") == 0) { if (print_man) { strcat(buffer, "\\fB"); } strcat(buffer, (char*)this_tag->children->content); if (print_man) { strcat(buffer, "\\fR"); } } if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "itemizedlist") == 0) { for (sub_tag = this_tag->children; sub_tag; sub_tag = sub_tag->next) { if (sub_tag->type == XML_ELEMENT_NODE && strcmp((char *)sub_tag->name, "listitem") == 0) { strcat(buffer, (char*)sub_tag->children->children->content); strcat(buffer, "\n"); } } } /* Look for subsections - return value & params */ if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "simplesect") == 0) { char *tmp; kind = get_attr(this_tag, "kind"); tmp = get_text(this_tag->children, NULL); if (returntext && strcmp(kind, "return") == 0) { *returntext = tmp; } } if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "parameterlist") == 0) { kind = get_attr(this_tag, "kind"); if (strcmp(kind, "param") == 0) { get_param_info(this_tag, ¶ms_list); } if (strcmp(kind, "retval") == 0) { get_param_info(this_tag, &retval_list); } } } return strdup(buffer); } /* Called from traverse_node() */ static void read_struct(xmlNode *cur_node, void *arg) { xmlNode *this_tag; struct struct_info *si=arg; struct param_info *pi; char fullname[1024]; char *type = NULL; char *name = NULL; const char *args=""; for (this_tag = cur_node->children; this_tag; this_tag = this_tag->next) { if (strcmp((char*)this_tag->name, "type") == 0) { type = (char*)this_tag->children->content ; } if (strcmp((char*)this_tag->name, "name") == 0) { name = (char*)this_tag->children->content ; } if (this_tag->children && strcmp((char*)this_tag->name, "argsstring") == 0) { args = (char*)this_tag->children->content; } } if (name) { pi = malloc(sizeof(struct param_info)); if (pi) { snprintf(fullname, sizeof(fullname), "%s%s", name, args); pi->paramtype = strdup(type); pi->paramname = strdup(fullname); pi->paramdesc = NULL; qb_list_add_tail(&pi->list, &si->params_list); } } } static int read_structure_from_xml(char *refid, char *name) { char fname[PATH_MAX]; xmlNode *rootdoc; xmlDocPtr doc; struct struct_info *si; int ret = -1; snprintf(fname, sizeof(fname), "%s/%s.xml", xml_dir, refid); doc = xmlParseFile(fname); if (doc == NULL) { fprintf(stderr, "Error: unable to open xml file for %s\n", refid); return -1; } rootdoc = xmlDocGetRootElement(doc); if (!rootdoc) { fprintf(stderr, "Can't find \"document root\"\n"); return -1; } si = malloc(sizeof(struct struct_info)); if (si) { qb_list_init(&si->params_list); si->structname = strdup(name); traverse_node(rootdoc, "memberdef", read_struct, si); ret = 0; qb_map_put(structures_map, refid, si); } xmlFreeDoc(doc); return ret; } /* Reformat pointer params so they look nicer */ static void print_param(FILE *manfile, struct param_info *pi, int field_width, int bold, const char *delimiter) { char asterisk = ' '; char *type = pi->paramtype; if (pi->paramtype[strlen(pi->paramtype)-1] == '*') { asterisk='*'; type = strdup(pi->paramtype); type[strlen(type)-1] = '\0'; } fprintf(manfile, " %s%-*s%c%s\\fI%s\\fP%s\n", bold?"\\fB":"", field_width, type, asterisk, bold?"\\fP":"", pi->paramname, delimiter); if (type != pi->paramtype) { free(type); } } static void print_structure(FILE *manfile, char *refid, char *name) { struct struct_info *si; struct param_info *pi; struct qb_list_head *iter; unsigned int max_param_length=0; /* If it's not been read in - go and look for it */ si = qb_map_get(structures_map, refid); if (!si) { if (!read_structure_from_xml(refid, name)) { si = qb_map_get(structures_map, refid); } } if (si) { qb_list_for_each(iter, &si->params_list) { pi = qb_list_entry(iter, struct param_info, list); if (strlen(pi->paramtype) > max_param_length) { max_param_length = strlen(pi->paramtype); } } fprintf(manfile, "struct %s {\n", si->structname); qb_list_for_each(iter, &si->params_list) { pi = qb_list_entry(iter, struct param_info, list); print_param(manfile, pi, max_param_length, 0,";"); } fprintf(manfile, "};\n"); } } char *get_texttree(int *type, xmlNode *cur_node, char **returntext) { xmlNode *this_tag; char *tmp = NULL; char buffer[4096] = {'\0'}; for (this_tag = cur_node->children; this_tag; this_tag = this_tag->next) { if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "para") == 0) { tmp = get_text(this_tag, returntext); strcat(buffer, tmp); strcat(buffer, "\n"); free(tmp); } } if (buffer[0]) { tmp = strdup(buffer); } return tmp; } /* The text output is VERY basic and just a check that it's working really */ static void print_text(char *name, char *def, char *brief, char *args, char *detailed, struct qb_list_head *param_list, char *returntext) { printf(" ------------------ %s --------------------\n", name); printf("NAME\n"); printf(" %s - %s\n", name, brief); printf("SYNOPSIS\n"); printf(" %s %s\n\n", name, args); printf("DESCRIPTION\n"); printf(" %s\n", detailed); if (returntext) { printf("RETURN VALUE\n"); printf(" %s\n", returntext); } } /* Print a long string with para marks in it. */ static void man_print_long_string(FILE *manfile, char *text) { char *next_nl; char *current = text; next_nl = strchr(text, '\n'); while (next_nl && *next_nl != '\0') { *next_nl = '\0'; if (strlen(current)) { fprintf(manfile, ".PP\n%s\n", current); } *next_nl = '\n'; current = next_nl+1; next_nl = strchr(current, '\n'); } } static void print_manpage(char *name, char *def, char *brief, char *args, char *detailed, struct qb_list_head *param_map, char *returntext) { char manfilename[PATH_MAX]; char gendate[64]; const char *dateptr = gendate; FILE *manfile; time_t t; struct tm *tm; qb_map_iter_t *map_iter; struct qb_list_head *iter; struct qb_list_head *tmp; const char *p; void *data; unsigned int max_param_type_len; unsigned int max_param_name_len; unsigned int num_param_descs; int param_count = 0; int param_num = 0; struct param_info *pi; t = time(NULL); tm = localtime(&t); if (!tm) { perror("unable to get localtime"); exit(1); } strftime(gendate, sizeof(gendate), "%Y-%m-%d", tm); if (manpage_date) { dateptr = manpage_date; } if (manpage_year == LONG_MIN) { manpage_year = tm->tm_year+1900; } snprintf(manfilename, sizeof(manfilename), "%s/%s.%s", output_dir, name, man_section); manfile = fopen(manfilename, "w+"); if (!manfile) { perror("unable to open output file"); printf("%s", manfilename); exit(1); } /* Work out the length of the parameters, so we can line them up */ max_param_type_len = 0; max_param_name_len = 0; num_param_descs = 0; qb_list_for_each(iter, ¶ms_list) { pi = qb_list_entry(iter, struct param_info, list); if (strlen(pi->paramtype) > max_param_type_len) { max_param_type_len = strlen(pi->paramtype); } if (strlen(pi->paramname) > max_param_name_len) { max_param_name_len = strlen(pi->paramname); } if (pi->paramdesc) { num_param_descs++; } param_count++; } /* Off we go */ fprintf(manfile, ".\\\" Automatically generated man page, do not edit\n"); fprintf(manfile, ".TH %s %s %s \"%s\" \"%s\"\n", name, man_section, dateptr, package_name, header); fprintf(manfile, ".SH NAME\n"); fprintf(manfile, "%s \\- %s\n", name, brief); fprintf(manfile, ".SH SYNOPSIS\n"); fprintf(manfile, ".nf\n"); fprintf(manfile, ".B #include \n"); fprintf(manfile, ".sp\n"); fprintf(manfile, "\\fB%s\\fP(\n", def); qb_list_for_each(iter, ¶ms_list) { pi = qb_list_entry(iter, struct param_info, list); print_param(manfile, pi, max_param_type_len, 1, ++param_num < param_count?",":""); } fprintf(manfile, ");\n"); fprintf(manfile, ".fi\n"); if (print_params && num_param_descs) { fprintf(manfile, ".SH PARAMS\n"); qb_list_for_each(iter, ¶ms_list) { pi = qb_list_entry(iter, struct param_info, list); fprintf(manfile, "\\fB%-*s \\fP\\fI%s\\fP\n", (int)max_param_name_len, pi->paramname, pi->paramdesc); fprintf(manfile, ".PP\n"); } } fprintf(manfile, ".SH DESCRIPTION\n"); man_print_long_string(manfile, detailed); if (qb_map_count_get(used_structures_map)) { fprintf(manfile, ".SH STRUCTURES\n"); map_iter = qb_map_iter_create(used_structures_map); for (p = qb_map_iter_next(map_iter, &data); p; p = qb_map_iter_next(map_iter, &data)) { fprintf(manfile, ".SS \"\"\n"); fprintf(manfile, ".PP\n"); fprintf(manfile, ".sp\n"); fprintf(manfile, ".sp\n"); fprintf(manfile, ".RS\n"); fprintf(manfile, ".nf\n"); fprintf(manfile, "\\fB\n"); print_structure(manfile, (char*)p, (char *)data); fprintf(manfile, "\\fP\n"); fprintf(manfile, ".fi\n"); } qb_map_iter_free(map_iter); fprintf(manfile, ".RE\n"); } if (returntext) { fprintf(manfile, ".SH RETURN VALUE\n"); man_print_long_string(manfile, returntext); } qb_list_for_each(iter, &retval_list) { pi = qb_list_entry(iter, struct param_info, list); fprintf(manfile, "\\fB%-*s \\fP\\fI%s\\fP\n", 10, pi->paramname, pi->paramdesc); fprintf(manfile, ".PP\n"); } fprintf(manfile, ".SH SEE ALSO\n"); fprintf(manfile, ".PP\n"); fprintf(manfile, ".nh\n"); fprintf(manfile, ".ad l\n"); param_num = 0; map_iter = qb_map_iter_create(function_map); for (p = qb_map_iter_next(map_iter, &data); p; p = qb_map_iter_next(map_iter, &data)) { /* Exclude us! */ if (strcmp(data, name)) { fprintf(manfile, "\\fI%s(%s)%s", (char *)data, man_section, param_num < (num_functions - 1)?", ":""); } param_num++; } qb_map_iter_free(map_iter); fprintf(manfile, "\n"); fprintf(manfile, ".ad\n"); fprintf(manfile, ".hy\n"); fprintf(manfile, ".SH \"COPYRIGHT\"\n"); fprintf(manfile, ".PP\n"); fprintf(manfile, "Copyright (C) 2010-%4ld Red Hat, Inc. All rights reserved.\n", manpage_year); fclose(manfile); /* Free the params & retval info */ qb_list_for_each_safe(iter, tmp, ¶ms_list) { pi = qb_list_entry(iter, struct param_info, list); qb_list_del(&pi->list); free_paraminfo(pi); } qb_list_for_each_safe(iter, tmp, &retval_list) { pi = qb_list_entry(iter, struct param_info, list); qb_list_del(&pi->list); free_paraminfo(pi); } /* Free used-structures map */ map_iter = qb_map_iter_create(used_structures_map); for (p = qb_map_iter_next(map_iter, &data); p; p = qb_map_iter_next(map_iter, &data)) { qb_map_rm(used_structures_map, p); free(data); } } /* Same as traverse_members, but to collect function names */ static void collect_functions(xmlNode *cur_node, void *arg) { xmlNode *this_tag; char *kind; char *name = NULL; if (cur_node->name && strcmp((char *)cur_node->name, "memberdef") == 0) { kind = get_attr(cur_node, "kind"); if (kind && strcmp(kind, "function") == 0) { for (this_tag = cur_node->children; this_tag; this_tag = this_tag->next) { if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "name") == 0) { name = strdup((char *)this_tag->children->content); } } if (name) { qb_map_put(function_map, name, name); num_functions++; } } } } static void traverse_members(xmlNode *cur_node, void *arg) { xmlNode *this_tag; if (cur_node->name && strcmp((char *)cur_node->name, "memberdef") == 0) { char *kind = NULL; char *def = NULL; char *args = NULL; char *name = NULL; char *brief = NULL; char *detailed = NULL; char *returntext = NULL; int type; kind=def=args=name=NULL; kind = get_attr(cur_node, "kind"); for (this_tag = cur_node->children; this_tag; this_tag = this_tag->next) { if (!this_tag->children || !this_tag->children->content) continue; if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "definition") == 0) def = strdup((char *)this_tag->children->content); if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "argsstring") == 0) args = strdup((char *)this_tag->children->content); if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "name") == 0) name = strdup((char *)this_tag->children->content); if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "briefdescription") == 0) { brief = get_texttree(&type, this_tag, &returntext); if (brief) { /* * apparently brief text contains extra trailing space and 2 \n. * remove them. */ brief[strlen(brief) - 3] = '\0'; } } if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "detaileddescription") == 0) { detailed = get_texttree(&type, this_tag, &returntext); } /* Get all the params */ if (this_tag->type == XML_ELEMENT_NODE && strcmp((char *)this_tag->name, "param") == 0) { char *param_type = get_child(this_tag, "type"); char *param_name = get_child(this_tag, "declname"); struct param_info *pi = malloc(sizeof(struct param_info)); if (pi) { pi->paramname = param_name; pi->paramtype = param_type; pi->paramdesc = NULL; qb_list_add_tail(&pi->list, ¶ms_list); } } } if (kind && strcmp(kind, "typedef") == 0) { /* Collect typedefs? */ } if (kind && strcmp(kind, "function") == 0) { if (print_man) { print_manpage(name, def, brief, args, detailed, ¶ms_list, returntext); } else { print_text(name, def, brief, args, detailed, ¶ms_list, returntext); } } free(kind); free(def); free(args); // free(name); /* don't free, it's in the map */ } } static void traverse_node(xmlNode *parentnode, const char *leafname, void (do_members(xmlNode*, void*)), void *arg) { xmlNode *cur_node; for (cur_node = parentnode->children; cur_node; cur_node = cur_node->next) { if (cur_node->type == XML_ELEMENT_NODE && cur_node->name && strcmp((char*)cur_node->name, leafname)==0) { do_members(cur_node, arg); continue; } if (cur_node->type == XML_ELEMENT_NODE) { traverse_node(cur_node, leafname, do_members, arg); } } } static void usage(char *name) { printf("Usage:\n"); printf(" %s [OPTIONS] []\n", name); printf("\n"); printf(" defaults to %s\n", XML_FILE); printf("\n"); printf(" -a Print ASCII dump of man pages to stdout\n"); printf(" -m Write man page files to \n"); printf(" -P Print PARAMS section\n"); printf(" -s Write man pages into section Use name. default \n"); printf(" -H
Set header (default \"Kronosnet Programmer's Manual\"\n"); printf(" -D Date to print at top of man pages (format not checked, default: today)\n"); printf(" -Y Year to print at end of copyright line (default: today's year)\n"); printf(" -o Write all man pages to (default .)\n"); printf(" -d Directory for XML files (default %s)\n", XML_DIR); printf(" -h Print this usage text\n"); } int main(int argc, char *argv[]) { xmlNode *rootdoc; xmlDocPtr doc; int quiet=0; int opt; char xml_filename[PATH_MAX]; while ( (opt = getopt_long(argc, argv, "H:amPD:Y:s:d:o:p:f:h?", NULL, NULL)) != EOF) { switch(opt) { case 'a': print_ascii = 1; print_man = 0; break; case 'm': print_man = 1; print_ascii = 0; break; case 'P': print_params = 1; break; case 's': man_section = optarg; break; case 'd': xml_dir = optarg; break; case 'D': manpage_date = optarg; break; case 'Y': manpage_year = strtol(optarg, NULL, 10); /* * Don't make too many assumptions about the year. I was on call at the * 2000 rollover. #experience */ if (manpage_year == LONG_MIN || manpage_year == LONG_MAX || manpage_year < 1900) { fprintf(stderr, "Value passed to -Y is not a valid year number\n"); return 1; } break; case 'p': package_name = optarg; break; case 'H': header = optarg; break; case 'o': output_dir = optarg; break; case '?': case 'h': usage(argv[0]); return 0; } } if (argv[optind]) { xml_file = argv[optind]; } if (!quiet) { fprintf(stderr, "reading xml ... "); } snprintf(xml_filename, sizeof(xml_filename), "%s/%s", xml_dir, xml_file); doc = xmlParseFile(xml_filename); if (doc == NULL) { fprintf(stderr, "Error: unable to read xml file %s\n", xml_filename); exit(1); } rootdoc = xmlDocGetRootElement(doc); if (!rootdoc) { fprintf(stderr, "Can't find \"document root\"\n"); exit(1); } if (!quiet) fprintf(stderr, "done.\n"); qb_list_init(¶ms_list); qb_list_init(&retval_list); structures_map = qb_hashtable_create(10); function_map = qb_hashtable_create(10); used_structures_map = qb_hashtable_create(10); /* Collect functions */ traverse_node(rootdoc, "memberdef", collect_functions, NULL); /* print pages */ traverse_node(rootdoc, "memberdef", traverse_members, NULL); return 0; }