[Spice-devel] [spice-server v2 3/6] net: Introduce red_socket_set_keepalive() helper

Frediano Ziglio fziglio at redhat.com
Fri Mar 10 17:41:05 UTC 2017


> 
> On Fri, Mar 10, 2017 at 08:46:09AM -0500, Frediano Ziglio wrote:
> > > 
> > > This allows to move some low-level code out of reds.c
> > > 
> > > Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
> > > ---
> > >  server/net-utils.c | 25 +++++++++++++++++++++++++
> > >  server/net-utils.h |  1 +
> > >  server/reds.c      | 27 ++-------------------------
> > >  3 files changed, 28 insertions(+), 25 deletions(-)
> > > 
> > > diff --git a/server/net-utils.c b/server/net-utils.c
> > > index ce409be..995b0d4 100644
> > > --- a/server/net-utils.c
> > > +++ b/server/net-utils.c
> > > @@ -31,6 +31,31 @@
> > >  
> > >  #include "net-utils.h"
> > >  
> > > +bool red_socket_set_keepalive(int fd, bool enable, int timeout)
> > > +{
> > > +    int keepalive = !!enable;
> > > +
> > > +    if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive,
> > > sizeof(keepalive)) == -1) {
> > > +        if (errno != ENOTSUP) {
> > > +            spice_printerr("setsockopt for keepalive failed, %s",
> > > strerror(errno));
> > > +            return false;
> > > +        }
> > > +    }
> > > +
> > > +    if (!enable) {
> > > +        return true;
> > > +    }
> > > +
> > > +    if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout))
> > > ==
> > > -1) {
> > 
> > About this value. NetBSD, Windows and Mac use milliseconds.
> 
> This is not true for NetBSD and Mac:
> http://www.unix.com/man-page/netbsd/4/tcp/
> "The TCP_KEEPIDLE option can be used to affect this value for a given
> socket, and specifies the number of seconds of idle time between
> keepalive probes."
> 

You are right, very similar for FreeBSD
https://www.freebsd.org/cgi/man.cgi?query=tcp&sektion=4&apropos=0&manpath=FreeBSD+11.0-RELEASE+and+Ports

> For Mac, I only found
> https://stackoverflow.com/questions/15860127/how-to-configure-tcp-keepalive-under-mac-os-x
> but this uses values in seconds.
> 

See https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man4/tcp.4.html#//apple_ref/doc/man/4/tcp
I must have been confused by the system setting which seems to use milliseconds.
Looks like Mac uses TCP_KEEPALIVE but is just changing name.
I searched a more recent page on apple developer website but I could not find it.

> 
> Christophe
> 

Frediano


More information about the Spice-devel mailing list