From 3f8c33cd766934e0eebb8f4c753cd9c034fa25b4 Mon Sep 17 00:00:00 2001 From: "Bruce A. Mah" Date: Mon, 14 Apr 2014 14:16:07 -0700 Subject: [PATCH] Better sendfile / zerocopy detection. There's still a bunch of OS-dependent fu because every platform that supports sendfile(2) does it differently. --- configure.ac | 5 +++++ src/iperf_util.c | 10 ++++++++++ src/net.c | 22 ++++++++++------------ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index 6b6ccdd..421f650 100644 --- a/configure.ac +++ b/configure.ac @@ -84,4 +84,9 @@ AC_CHECK_FUNCS([cpuset_setaffinity sched_setaffinity], AC_DEFINE([HAVE_CPU_AFFINITY], [1], [Have CPU affinity support.])) +# Check for sendfile support. FreeBSD, Linux, and MacOS all support +# this system call, but they're all different in terms of what headers +# it needs and what arguments it expects. +AC_CHECK_FUNCS([sendfile]) + AC_OUTPUT([Makefile src/Makefile examples/Makefile]) diff --git a/src/iperf_util.c b/src/iperf_util.c index 99ccc35..9373d28 100644 --- a/src/iperf_util.c +++ b/src/iperf_util.c @@ -258,6 +258,16 @@ get_optional_features(void) numfeatures++; #endif /* HAVE_TCP_CONGESTION */ +#if defined(HAVE_SENDFILE) + if (numfeatures > 0) { + strncat(features, ", ", + sizeof(features) - strlen(features) - 1); + } + strncat(features, "sendfile / zerocopy", + sizeof(features) - strlen(features) - 1); + numfeatures++; +#endif /* HAVE_SENDFILE */ + if (numfeatures == 0) { strncat(features, "None", sizeof(features) - strlen(features) - 1); diff --git a/src/net.c b/src/net.c index 8a24154..9b5ba3d 100644 --- a/src/net.c +++ b/src/net.c @@ -1,11 +1,12 @@ /* - * Copyright (c) 2009-2011, The Regents of the University of California, + * Copyright (c) 2009-2014, The Regents of the University of California, * through Lawrence Berkeley National Laboratory (subject to receipt of any * required approvals from the U.S. Dept. of Energy). All rights reserved. * * This code is distributed under a BSD style license, see the LICENSE file * for complete information. */ +#include "iperf_config.h" #include #include @@ -20,6 +21,7 @@ #include #include +#ifdef HAVE_SENDFILE #ifdef linux #include #else @@ -34,6 +36,7 @@ #endif #endif #endif +#endif HAVE_SENDFILE #include "iperf_util.h" #include "net.h" @@ -218,19 +221,12 @@ Nwrite(int fd, const char *buf, size_t count, int prot) int has_sendfile(void) { -#ifdef linux +#if defined(HAS_SENDFILE) return 1; -#else -#ifdef __FreeBSD__ - return 1; -#else -#if defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_6) /* OS X */ - return 1; -#else +#else /* HAS_SENDFILE */ return 0; -#endif -#endif -#endif +#endif /* HAS_SENDFILE */ + } @@ -242,6 +238,7 @@ int Nsendfile(int fromfd, int tofd, const char *buf, size_t count) { off_t offset; +#if defined(HAVE_SENDFILE) #if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_6)) off_t sent; #endif @@ -271,6 +268,7 @@ Nsendfile(int fromfd, int tofd, const char *buf, size_t count) #endif #endif #endif +#endif /* HAVE_SENDFILE */ if (r < 0) { switch (errno) { case EINTR: