From 95c61459f374f954f10c9993386009438c31db04 Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Mon, 26 Feb 2018 18:01:35 +0000 Subject: [PATCH] libsa: Add MAXWAIT to net for establishing max total timeout Current timeout behavior is to progress in timeout values from MINTMO to MAXTMO in MINTMO steps before finally timing out. This results in a fairly long time before operations finally timeout, which may not be ideal for some use-cases. Add MAXWAIT that may be configured along with MINTMO/MAXTMO. If we attempt to start our send/recv cycle over again but MAXWAIT > 0 and MAXWAIT seconds have already passed, then go ahead and timeout. This is intended for those that just want to say "timeout after 180 seconds" rather than calculate and tweak MINTMO/MAXTMO to get their desired timeout. The default is 0, or "progress from MINTMO to MAXTMO with no exception." This has been modified since review to allow for it to be defined via CFLAGS and doing appropriate error checking. Future work may add some Makefile foo to respect LOADER_NET_MAXWAIT if it's specified in the environment and pass it in as MAXWAIT accordingly. Reviewed by: imp, sbruno, tsoome (all previous version) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D14389 --- stand/libsa/net.c | 10 ++++++---- stand/libsa/net.h | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/stand/libsa/net.c b/stand/libsa/net.c index aaea26e4f87c..9f2eabb1da9c 100644 --- a/stand/libsa/net.c +++ b/stand/libsa/net.c @@ -77,6 +77,7 @@ sendrecv(struct iodesc *d, { ssize_t cc; time_t t, tmo, tlast; + time_t tref; long tleft; #ifdef NET_DEBUG @@ -87,13 +88,14 @@ sendrecv(struct iodesc *d, tmo = MINTMO; tlast = 0; tleft = 0; + tref = getsecs(); t = getsecs(); for (;;) { + if (MAXWAIT > 0 && (getsecs() - tref) >= MAXWAIT) { + errno = ETIMEDOUT; + return -1; + } if (tleft <= 0) { - if (tmo >= MAXTMO) { - errno = ETIMEDOUT; - return -1; - } cc = (*sproc)(d, sbuf, ssize); if (cc != -1 && cc < ssize) panic("sendrecv: short write! (%zd < %zd)", diff --git a/stand/libsa/net.h b/stand/libsa/net.h index 5bb1ac57f9e8..f5a5b5959ff2 100644 --- a/stand/libsa/net.h +++ b/stand/libsa/net.h @@ -61,6 +61,20 @@ enum net_proto { #define MAXTMO 120 /* seconds */ #define MINTMO 2 /* seconds */ +/* + * Maximum wait time for sending and receiving before we give up and timeout. + * If set to 0, operations will eventually timeout completely, but send/recv + * timeouts must progress exponentially from MINTMO to MAXTMO before final + * timeout is hit. + */ +#ifndef MAXWAIT +#define MAXWAIT 0 /* seconds */ +#endif + +#if MAXWAIT < 0 +#error MAXWAIT must not be a negative number +#endif + #define FNAME_SIZE 128 #define IFNAME_SIZE 16 #define RECV_SIZE 1536 /* XXX delete this */