2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2010-11-07 10:44:13 +00:00
|
|
|
* include/common/compat.h
|
|
|
|
* Operating system compatibility interface.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#ifndef _COMMON_COMPAT_H
|
|
|
|
#define _COMMON_COMPAT_H
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/* This is needed on Linux for Netfilter includes */
|
2010-11-07 10:44:13 +00:00
|
|
|
#include <sys/param.h>
|
2007-11-30 17:38:35 +00:00
|
|
|
#include <sys/types.h>
|
2010-02-26 21:00:19 +00:00
|
|
|
#include <sys/socket.h>
|
2010-11-07 10:44:13 +00:00
|
|
|
#include <arpa/inet.h>
|
2006-06-29 16:54:54 +00:00
|
|
|
#include <common/config.h>
|
2008-04-21 05:34:31 +00:00
|
|
|
#include <common/standard.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2008-07-14 22:36:31 +00:00
|
|
|
#ifndef BITS_PER_INT
|
|
|
|
#define BITS_PER_INT (8*sizeof(int))
|
|
|
|
#endif
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/* this is for libc5 for example */
|
|
|
|
#ifndef TCP_NODELAY
|
|
|
|
#define TCP_NODELAY 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SHUT_RD
|
|
|
|
#define SHUT_RD 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SHUT_WR
|
|
|
|
#define SHUT_WR 1
|
|
|
|
#endif
|
|
|
|
|
2009-08-19 09:22:33 +00:00
|
|
|
/* only Linux defines it */
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2007-11-30 17:38:35 +00:00
|
|
|
/* AIX does not define MSG_DONTWAIT. We'll define it to zero, and test it
|
|
|
|
* wherever appropriate.
|
|
|
|
*/
|
|
|
|
#ifndef MSG_DONTWAIT
|
|
|
|
#define MSG_DONTWAIT 0
|
|
|
|
#endif
|
|
|
|
|
2009-08-19 09:14:11 +00:00
|
|
|
/* Only Linux defines MSG_MORE */
|
|
|
|
#ifndef MSG_MORE
|
|
|
|
#define MSG_MORE 0
|
|
|
|
#endif
|
|
|
|
|
MEDIUM: protocol: implement a "drain" function in protocol layers
Since commit cfd97c6f was merged into 1.5-dev14 (BUG/MEDIUM: checks:
prevent TIME_WAITs from appearing also on timeouts), some valid health
checks sometimes used to show some TCP resets. For example, this HTTP
health check sent to a local server :
19:55:15.742818 IP 127.0.0.1.16568 > 127.0.0.1.8000: S 3355859679:3355859679(0) win 32792 <mss 16396,nop,nop,sackOK,nop,wscale 7>
19:55:15.742841 IP 127.0.0.1.8000 > 127.0.0.1.16568: S 1060952566:1060952566(0) ack 3355859680 win 32792 <mss 16396,nop,nop,sackOK,nop,wscale 7>
19:55:15.742863 IP 127.0.0.1.16568 > 127.0.0.1.8000: . ack 1 win 257
19:55:15.745402 IP 127.0.0.1.16568 > 127.0.0.1.8000: P 1:23(22) ack 1 win 257
19:55:15.745488 IP 127.0.0.1.8000 > 127.0.0.1.16568: FP 1:146(145) ack 23 win 257
19:55:15.747109 IP 127.0.0.1.16568 > 127.0.0.1.8000: R 23:23(0) ack 147 win 257
After some discussion with Chris Huang-Leaver, it appeared clear that
what we want is to only send the RST when we have no other choice, which
means when the server has not closed. So we still keep SYN/SYN-ACK/RST
for pure TCP checks, but don't want to see an RST emitted as above when
the server has already sent the FIN.
The solution against this consists in implementing a "drain" function at
the protocol layer, which, when defined, causes as much as possible of
the input socket buffer to be flushed to make recv() return zero so that
we know that the server's FIN was received and ACKed. On Linux, we can make
use of MSG_TRUNC on TCP sockets, which has the benefit of draining everything
at once without even copying data. On other platforms, we read up to one
buffer of data before the close. If recv() manages to get the final zero,
we don't disable lingering. Same for hard errors. Otherwise we do.
In practice, on HTTP health checks we generally find that the close was
pending and is returned upon first recv() call. The network trace becomes
cleaner :
19:55:23.650621 IP 127.0.0.1.16561 > 127.0.0.1.8000: S 3982804816:3982804816(0) win 32792 <mss 16396,nop,nop,sackOK,nop,wscale 7>
19:55:23.650644 IP 127.0.0.1.8000 > 127.0.0.1.16561: S 4082139313:4082139313(0) ack 3982804817 win 32792 <mss 16396,nop,nop,sackOK,nop,wscale 7>
19:55:23.650666 IP 127.0.0.1.16561 > 127.0.0.1.8000: . ack 1 win 257
19:55:23.651615 IP 127.0.0.1.16561 > 127.0.0.1.8000: P 1:23(22) ack 1 win 257
19:55:23.651696 IP 127.0.0.1.8000 > 127.0.0.1.16561: FP 1:146(145) ack 23 win 257
19:55:23.652628 IP 127.0.0.1.16561 > 127.0.0.1.8000: F 23:23(0) ack 147 win 257
19:55:23.652655 IP 127.0.0.1.8000 > 127.0.0.1.16561: . ack 24 win 257
This change should be backported to 1.4 which is where Chris encountered
this issue. The code is different, so probably the tcp_drain() function
will have to be put in the checks only.
2013-06-10 17:56:38 +00:00
|
|
|
/* On Linux 2.4 and above, MSG_TRUNC can be used on TCP sockets to drop any
|
|
|
|
* pending data. Let's rely on NETFILTER to detect if this is supported.
|
|
|
|
*/
|
|
|
|
#ifdef NETFILTER
|
|
|
|
#define MSG_TRUNC_CLEARS_INPUT
|
|
|
|
#endif
|
|
|
|
|
2010-11-07 10:44:13 +00:00
|
|
|
/* Maximum path length, OS-dependant */
|
|
|
|
#ifndef MAXPATHLEN
|
|
|
|
#define MAXPATHLEN 128
|
|
|
|
#endif
|
|
|
|
|
2011-10-23 19:14:29 +00:00
|
|
|
/* On Linux, allows pipes to be resized */
|
|
|
|
#ifndef F_SETPIPE_SZ
|
|
|
|
#define F_SETPIPE_SZ (1024 + 7)
|
|
|
|
#endif
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
#if defined(TPROXY) && defined(NETFILTER)
|
2008-04-21 05:34:31 +00:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/netfilter_ipv6.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <linux/netfilter_ipv4.h>
|
|
|
|
#endif
|
|
|
|
|
2008-01-13 16:37:16 +00:00
|
|
|
/* On Linux, IP_TRANSPARENT and/or IP_FREEBIND generally require a kernel patch */
|
2008-01-13 13:49:51 +00:00
|
|
|
#if defined(CONFIG_HAP_LINUX_TPROXY)
|
2008-01-13 16:37:16 +00:00
|
|
|
#if !defined(IP_FREEBIND)
|
|
|
|
#define IP_FREEBIND 15
|
|
|
|
#endif /* !IP_FREEBIND */
|
2008-01-13 13:49:51 +00:00
|
|
|
#if !defined(IP_TRANSPARENT)
|
|
|
|
#define IP_TRANSPARENT 19
|
|
|
|
#endif /* !IP_TRANSPARENT */
|
2012-07-13 12:34:59 +00:00
|
|
|
#if !defined(IPV6_TRANSPARENT)
|
|
|
|
#define IPV6_TRANSPARENT 75
|
|
|
|
#endif /* !IPV6_TRANSPARENT */
|
2008-01-13 13:49:51 +00:00
|
|
|
#endif /* CONFIG_HAP_LINUX_TPROXY */
|
|
|
|
|
2013-05-08 20:49:23 +00:00
|
|
|
#if defined(IP_FREEBIND) \
|
2013-05-08 21:22:39 +00:00
|
|
|
|| defined(IP_BINDANY) \
|
|
|
|
|| defined(IPV6_BINDANY) \
|
2013-05-08 21:30:23 +00:00
|
|
|
|| defined(SO_BINDANY) \
|
2013-05-08 20:49:23 +00:00
|
|
|
|| defined(IP_TRANSPARENT) \
|
|
|
|
|| defined(IPV6_TRANSPARENT)
|
|
|
|
#define CONFIG_HAP_TRANSPARENT
|
|
|
|
#endif
|
|
|
|
|
2006-11-13 00:22:38 +00:00
|
|
|
/* We'll try to enable SO_REUSEPORT on Linux 2.4 and 2.6 if not defined.
|
|
|
|
* There are two families of values depending on the architecture. Those
|
|
|
|
* are at least valid on Linux 2.4 and 2.6, reason why we'll rely on the
|
|
|
|
* NETFILTER define.
|
|
|
|
*/
|
|
|
|
#if !defined(SO_REUSEPORT) && defined(NETFILTER)
|
|
|
|
#if (SO_REUSEADDR == 2)
|
|
|
|
#define SO_REUSEPORT 15
|
|
|
|
#elif (SO_REUSEADDR == 0x0004)
|
|
|
|
#define SO_REUSEPORT 0x0200
|
|
|
|
#endif /* SO_REUSEADDR */
|
|
|
|
#endif /* SO_REUSEPORT */
|
|
|
|
|
2013-04-02 14:43:24 +00:00
|
|
|
/* only Linux defines TCP_FASTOPEN */
|
|
|
|
#ifdef USE_TFO
|
|
|
|
#ifndef TCP_FASTOPEN
|
|
|
|
#define TCP_FASTOPEN 23
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2013-06-23 15:37:13 +00:00
|
|
|
/* If IPv6 is supported, define IN6_IS_ADDR_V4MAPPED() if missing. */
|
|
|
|
#if defined(IPV6_TCLASS) && !defined(IN6_IS_ADDR_V4MAPPED)
|
|
|
|
#define IN6_IS_ADDR_V4MAPPED(a) \
|
|
|
|
((((const uint32_t *) (a))[0] == 0) \
|
|
|
|
&& (((const uint32_t *) (a))[1] == 0) \
|
|
|
|
&& (((const uint32_t *) (a))[2] == htonl (0xffff)))
|
|
|
|
#endif
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
#if defined(__dietlibc__)
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#endif /* _COMMON_COMPAT_H */
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|