Commit Graph

6727 Commits

Author SHA1 Message Date
Dragan Dosen
16586e635b DOC: 51d: add 51Degrees git URL that points to release version 3.2.12.12
The 51Degrees C library version 3.2.12.12 has support for a new Hash Trie
algorithm.

This patch can be backported in 1.7.
2017-10-05 11:24:25 +02:00
Dragan Dosen
483b93cc9a BUILD/MINOR: 51d: fix warning when building with 51Degrees release version 3.2.12.12
The warning appears when building with 51Degrees release that uses a new
Hash Trie algorithm (release version 3.2.12.12):

src/51d.c: In function init_51degrees:
src/51d.c:566:2: warning: enumeration value DATA_SET_INIT_STATUS_TOO_MANY_OPEN_FILES not handled in switch [-Wswitch]
  switch (_51d_dataset_status) {
  ^

This patch can be backported in 1.7.
2017-10-05 11:23:38 +02:00
Bin Wang
95fad5ba4b BUG/MAJOR: stream-int: don't re-arm recv if send fails
When
    1) HAProxy configured to enable splice on both directions
    2) After some high load, there are 2 input channels with their socket buffer
       being non-empty and pipe being full at the same time, sitting in `fd_cache`
       without any other fds.

The 2 channels will repeatedly be stopped for receiving (pipe full) and waken
for receiving (data in socket), thus getting out and in of `fd_cache`, making
their fd swapping location in `fd_cache`.

There is a `if (entry < fd_cache_num && fd_cache[entry] != fd) continue;`
statement in `fd_process_cached_events` to prevent frequent polling, but since
the only 2 fds are constantly swapping location, `fd_cache[entry] != fd` will
always hold true, thus HAProxy can't make any progress.

The root cause of the issue is dual :
  - there is a single fd_cache, for next events and for the ones being
    processed, while using two distinct arrays would avoid the problem.

  - the write side of the stream interface wakes the read side up even
    when it couldn't write, and this one really is a bug.

Due to CF_WRITE_PARTIAL not being cleared during fast forwarding, a failed
send() attempt will still cause ->chk_rcv() to be called on the other side,
re-creating an entry for its connection fd in the cache, causing the same
sequence to be repeated indefinitely without any opportunity to make progress.

CF_WRITE_PARTIAL used to be used for what is present in these tests : check
if a recent write operation was performed. It's part of the CF_WRITE_ACTIVITY
set and is tested to check if timeouts need to be updated. It's also used to
detect if a failed connect() may be retried.

What this patch does is use CF_WROTE_DATA() to check for a successful write
for connection retransmits, and to clear CF_WRITE_PARTIAL before preparing
to send in stream_int_notify(). This way, timeouts are still updated each
time a write succeeds, but chk_rcv() won't be called anymore after a failed
write.

It seems the fix is required all the way down to 1.5.

Without this patch, the only workaround at this point is to disable splicing
in at least one direction. Strictly speaking, splicing is not absolutely
required, as regular forwarding could theorically cause the issue to happen
if the timing is appropriate, but in practice it appears impossible to
reproduce it without splicing, and even with splicing it may vary.

The following config manages to reproduce it after a few attempts (haproxy
going 100% CPU and having to be killed) :

  global
      maxpipes 50000
      maxconn 10000

  listen srv1
      option splice-request
      option splice-response
      bind :8001
      server s1 127.0.0.1:8002

  server$ tcploop 8002 L N20 A R10 S1000000 R10 S1000000 R10 S1000000 R10 S1000000 R10 S1000000
  client$ tcploop 8001 N20 C T S1000000 R10 J
2017-10-05 11:20:16 +02:00
Christopher Faulet
a258479e3f BUG/MEDIUM: http: Return an error when url_dec sample converter failed
url_dec sample converter uses url_decode function to decode an URL. This
function fails by returning -1 when an invalid character is found. But the
sample converter never checked the return value and it used it as length for the
decoded string. Because it always succeeded, the invalid sample (with a string
length set to -1) could be used by other sample fetches or sample converters,
leading to undefined behavior like segfault.

The fix is pretty simple, url_dec sample converter just needs to return an error
when url_decode fails.

This patch must be backported in 1.7 and 1.6.
2017-10-05 11:11:34 +02:00
Willy Tarreau
017af2477e BUG/MEDIUM: cli: fix "show fd" crash when dumping closed FDs
I misplaced the "if (!fdt.owner)" test so it can occasionally crash
when dumping an fd that's already been closed but still appears in
the table. It's not critical since this was not pushed into any
release nor backported though.
2017-10-04 20:28:26 +02:00
Willy Tarreau
00149121b7 MEDIUM: checks: do not allocate a permanent connection anymore
Health check currently cheat, they allocate a connection upon startup and never
release it, it's only recycled. The problem with doing this is that this code
is preventing the connection code from evolving towards multiplexing.

This code ensures that it's safe for the checks to run without a connection
all the time. Given that the code heavily relies on CO_FL_ERROR to signal
check errors, it is not trivial but in practice this is the principle adopted
here :

  - the connection is not allocated anymore on startup

  - new checks are not supposed to have a connection, so an attempt is made
    to allocate this connection in the check task's context. If it fails,
    the check is aborted on a resource error, and the rare code on this path
    verifying the connection was adjusted to check for its existence (in
    practice, avoid to close it)

  - returning checks necessarily have a valid connection (which may possibly
    be closed).

  - a "tcp-check connect" rule tries to allocate a new connection before
    releasing the previous one (but after closing it), so that if it fails,
    it still keeps the previous connection in a closed state. This ensures
    a connection is always valid here

Now it works well on all tested cases (regular and TCP checks, even with
multiple reconnections), including when the connection is forced to NULL or
randomly allocated.
2017-10-04 19:36:29 +02:00
Willy Tarreau
6bdcab0149 MEDIUM: checks: make tcpcheck_main() indicate if it recycled a connection
The tcp-checks are very fragile. They can modify a connection's FD by
closing and reopening a socket without informing the connection layer,
which may then possibly touch the wrong fd. Given that the events are
only cleared and that the fd is just created, there should be no visible
side effect because the old fd is deleted so even if its flags get cleared
they were already, and the new fd already has them cleared as well so it's
a NOP. Regardless, this is too fragile and will not resist to threads.

In order to address this situation, this patch makes tcpcheck_main()
indicate if it closed a connection and report it to wake_srv_chk(), which
will then report it to the connection's fd handler so that it refrains
from updating the connection polling and the fd. Instead the connection
polling status is updated in the wake() function.
2017-10-04 18:49:22 +02:00
Willy Tarreau
f411cce456 MINOR: checks: don't create then kill a dummy connection before tcp-checks
When tcp-checks are in use, a connection starts to be created, then it's
destroyed so that tcp-check can recreate its own. Now we directly move
to tcpcheck_main() when it's detected that tcp-check is in use.
2017-10-04 16:29:19 +02:00
Willy Tarreau
be74b88be8 MINOR: tcp-check: make tcpcheck_main() take a check, not a connection
We want this one to allocate its own connection so it must not take a
connection but a check.
2017-10-04 16:29:19 +02:00
Willy Tarreau
668730fd00 TESTS: checks: add a simple test config for tcp-checks
tcp-check.cfg tests various arrangements of initial tcp-check rules.
2017-10-04 16:29:19 +02:00
Willy Tarreau
894c642fbf BUG/MINOR: tcp-check: don't initialize then break a connection starting with a comment
The following config :

    backend tcp9000
        option tcp-check
        tcp-check comment "this is a comment"
        tcp-check connect port 10000
        server srv 127.0.0.1:9000 check inter 1s

will result in a connection being first made to port 9000 then immediately
destroyed and re-created on port 10000, because the first rule is a comment
and doesn't match the test for the first rule being a connect(). It's
mostly harmless (unless the server really must not receive empty
connections) and the workaround simply consists in removing the comment.

Let's proceed like in other places where we simply skip leading comments.
A new function was made to make this lookup les boring. The fix should be
backported to 1.7 and 1.6.
2017-10-04 16:13:57 +02:00
Willy Tarreau
59070784fc TESTS: checks: add a simple test config for external checks
ext-check.cfg tests both for success and failure in two different backends.
2017-10-04 15:42:00 +02:00
Willy Tarreau
b398e643d4 CLEANUP: checks: do not allocate a connection for process checks
Since this connection is not used at all anymore, do not allocate it.
It was verified that check successes and failures (both synchronous
and asynchronous) continue to be properly reported.
2017-10-04 15:25:38 +02:00
Willy Tarreau
d7c3fbd5c3 CLEANUP: checks: don't report report the fork() error twice
Upon fork() error, a first report is immediately made by connect_proc_chk()
via set_server_check_status(), then process_chk_proc() detects the error
code and makes up a dummy connection error to call chk_report_conn_err(),
which tries to retrieve the errno code from the connection, fails, then
saves the status message from the check, fails all "if" tests on its path
related to the connection then resets the check's state to the current one
with the current status message. All this useless chain is the only reason
why process checks require a connection! Let's simply get rid of this second
useless call.
2017-10-04 15:19:26 +02:00
Willy Tarreau
1e62e2a780 CLEANUP: checks: remove misleading comments and statuses for external process
The external process check code abused a little bit from copy-pasting to
the point of making think it requires a connection... The initialization
code only returns SF_ERR_NONE and SF_ERR_RESOURCE, so the other one can
be folded there. The code now only uses the connection to report the
error status.
2017-10-04 15:07:02 +02:00
Willy Tarreau
b5259bf44f MINOR: checks: make chk_report_conn_err() take a check, not a connection
Amazingly, this function takes a connection to report an error and is used
by process checks, placing a hard dependency between the connection and the
check preventing the mux from being completely implemented. Let's first get
rid of this.
2017-10-04 14:47:29 +02:00
Willy Tarreau
a1a247bd90 BUG/MINOR: unix: properly check for octal digits in the "mode" argument
A config containing "stats socket /path/to/socket mode admin" used to
silently start and be unusable (mode 0, level user) because the "mode"
parser doesn't take care of non-digits. Now it properly reports :

   [ALERT] 276/144303 (7019) : parsing [ext-check.cfg:4] : 'stats socket' : ''mode' : missing or invalid mode 'admin' (octal integer expected)'

This can probably be backported to 1.7, 1.6 and 1.5, though reporting
parsing errors in very old versions probably isn't a good idea if the
feature was left unused for years.
2017-10-04 14:43:44 +02:00
Willy Tarreau
c09572fd8b BUG/MEDIUM: tcp-check: don't call tcpcheck_main() from the I/O handlers!
This function can destroy a socket and create a new one, resulting in a
change of FD on the connection between recv() and send() for example,
which is absolutely not permitted, and can result in various funny games
like polling not being properly updated (or with the flags from a previous
fd) etc.

Let's only call this from the wake() callback which is more tolerant.
Ideally the operations should be made even more reliable by returning
a specific value to indicate that the connection was released and that
another one was created. But this is hasardous for stable releases as
it may reveal other issues.

This fix should be backported to 1.7 and 1.6.
2017-10-04 13:41:20 +02:00
Willy Tarreau
82feaaf042 BUG/MINOR: tcp-check: don't quit with pending data in the send buffer
In the rare case where the "tcp-check send" directive is the last one in
the list, it leaves the loop without sending the data. Fortunately, the
polling is still enabled on output, resulting in the connection handler
calling back to send what remains, but this is ugly and not very reliable.

This may be backported to 1.7 and 1.6.
2017-10-04 13:41:20 +02:00
Willy Tarreau
a3782e7594 BUG/MEDIUM: tcp-check: properly indicate polling state before performing I/O
While porting the connection to use the mux layer, it appeared that
tcp-checks wouldn't receive anymore because the polling is not enabled
before attempting to call xprt->rcv_buf() nor xprt->snd_buf(), and it
is illegal to call these functions with polling disabled as they
directly manipulate the FD state, resulting in an inconsistency where
the FD is enabled and the connection's polling flags disabled.

Till now it happened to work only because when recv() fails on EAGAIN
it calls fd_cant_recv() which enables polling while signaling the
failure, so that next time the message is received. But the connection's
polling is never enabled, and any tiny change resulting in a call to
conn_data_update_polling() immediately disables reading again.

It's likely that this problem already happens on some corner cases
such as multi-packet responses. It definitely breaks as soon as the
response buffer is full but we don't support consuming more than one
response buffer.

This fix should be backported to 1.7 and 1.6. In order to check for the
proper behaviour, this tcp-check must work and clearly show an SSH
banner in recvfrom() as observed under strace, otherwise it's broken :

   tcp-check connect port 22
   tcp-check expect rstring SSH
   tcp-check send blah
2017-10-04 13:41:17 +02:00
Willy Tarreau
3cad394520 CLEANUUP: checks: don't set conn->handle.fd to -1
This used to be needed to know whether there was a check in progress a
long time ago (before tcp_checks) but this is not true anymore and even
becomes wrong after the check is reused as conn_init() initializes it
to DEAD_FD_MAGIC.
2017-10-04 07:53:19 +02:00
Baptiste Assmann
46392fdd08 BUG/MEDIUM: tcp/http: set-dst-port action broken
A regression has been introduced in commit
00005ce5a1: the port being changed is the
one from 'cli_conn->addr.from' instead of 'cli_conn->addr.to'.

This patch fixes the regression.

Backport status: should be backported to HAProxy 1.7 and above.
2017-10-04 04:36:17 +02:00
Ilya Shipitsin
4473a2e9aa BUG/MINOR: contrib/halog: fixing small memory leak
Issue was identified by cppcheck
2017-10-03 13:52:45 +02:00
David Carlier
93e8b88f06 BUG/MINOR: log: fixing small memory leak in error code path.
since we do not log the sample fetch when it is invalid, we can
free the log data.
2017-09-21 17:44:31 +02:00
Willy Tarreau
6fb4ba38e0 BUG/MEDIUM: server: unwanted behavior leaving maintenance mode on tracked stopping server (take2)
Previous patch got accidently broken. This one fixes it.
2017-09-21 17:37:38 +02:00
Emeric Brun
e1e3947e7e BUG/MEDIUM: server: unwanted behavior leaving maintenance mode on tracked stopping server
Leaving the maintenance state and if the server remains in stopping mode due
to a tracked one:

- We mistakenly try to grab some pending conns and shutdown backup sessions.
- The proxy down time and last change were also mistakenly updated
2017-09-21 17:30:01 +02:00
Willy Tarreau
2ba672726c MINOR: ist: add a macro to ease const array initialization
It's not possible to use strlen() in const arrays even with const
strings, but we can use sizeof-1 via a macro. Let's provide this in
the IST() macro, as it saves the developer from having to count the
characters.
2017-09-21 15:32:31 +02:00
Willy Tarreau
82967bf9b3 MINOR: connection: adjust CO_FL_NOTIFY_DATA after removal of flags
After the removal of CO_FL_DATA_RD_SH and CO_FL_DATA_WR_SH, the
aggregate mask CO_FL_NOTIFY_DATA was not updated. It happens that
now CO_FL_NOTIFY_DATA and CO_FL_NOTIFY_DONE are similar, which may
reveal some overlap between the ->wake and ->xprt_done callbacks.
We'll see after the mux changes if both are still required.
2017-09-21 06:28:52 +02:00
Willy Tarreau
5531d5732d MINOR: net_helper: add 64-bit read/write functions
These ones are the same as the previous ones but for 64 bit values.
We're using my_ntohll() and my_htonll() from standard.h for the byte
order conversion.
2017-09-21 06:27:08 +02:00
Willy Tarreau
2888c08346 MINOR: net_helper: add write functions
These ones are the equivalent of the read_* functions. They support
writing unaligned words, possibly wrapping, in host and network order.
The write_i*() functions were not implemented since the caller can
already use the unsigned version.
2017-09-21 06:25:10 +02:00
Willy Tarreau
d5370e1d6c MINOR: net_helper: add functions to read from vectors
This patch adds the ability to read from a wrapping memory area (ie:
buffers). The new functions are called "readv_<type>". The original
ones were renamed to start with "read_" to make the difference more
obvious between the read method and the returned type.

It's worth noting that the memory barrier in readv_bytes() is critical,
as otherwise gcc decides that it doesn't need the resulting data, but
even worse, removes the length checks in readv_u64() and happily
performs an out-of-bounds unaligned read using read_u64()! Such
"optimizations" are a bit borderline, especially when they impact
security like this...
2017-09-20 11:27:31 +02:00
Willy Tarreau
26488ad358 MINOR: buffer: add b_end() and b_to_end()
These ones return respectively the pointer to the end of the buffer and
the distance between b->p and the end. These will simplify a bit some
new code needed to parse directly from a wrapping buffer.
2017-09-20 11:27:31 +02:00
Willy Tarreau
4a6425d373 MINOR: buffer: add b_del() to delete a number of characters
This will be used by code which directly parses buffers with no channel
in the middle (eg: h2, might be used by checks as well).
2017-09-20 11:27:31 +02:00
Willy Tarreau
36eb3a3ac8 MINOR: tools: make my_htonll() more efficient on x86_64
The current construct was made when developing on a 32-bit machine.
Having a simple bswap operation replaced with 2 bswap, 2 shift and
2 or is quite of a waste of precious cycles... Let's provide a trivial
asm-based implementation for x86_64.
2017-09-20 11:27:31 +02:00
Dragan Dosen
2f1cacb1aa BUG/MINOR: contrib/modsecurity: close the va_list ap before return
Make sure the va_list ap is closed before return in case the function
vsnprintf() returned an error.
2017-09-18 11:18:34 +02:00
Dragan Dosen
ccf6100e11 BUG/MINOR: contrib/mod_defender: close the va_list argp before return
Fix the case when the va_list argp could be left open if the function
vsnprintf() returned a negative value.
2017-09-18 11:18:09 +02:00
Christopher Faulet
f8bb0ce450 MINOR: ssl: Remove useless checks on bind_conf or bind_conf->is_ssl
bind_conf always exists at these steps and it is always for SSL listeners.
2017-09-15 18:42:23 +02:00
Christopher Faulet
3bbd65b23e BUG/MINOR: dns: Fix check on nameserver in snr_resolution_cb
snr_resolution_cb can be called with <nameserver> parameter set to NULL. So we
must check it before using it. This is done most of time, except when we deal
with invalid DNS response.
2017-09-15 18:42:23 +02:00
Christopher Faulet
ccbc3fd9f9 BUG/MINOR: spoe: Don't rely on SPOE ctx in debug message when its creation failed
If the SPOE context creation failed, we must not try to use it in the debug
message used to notice the error.

This patch must be backported in 1.7.
2017-09-15 18:42:23 +02:00
Christopher Faulet
3dc860d19d BUG/MINOR: compression: Check response headers before http-response rules eval
This is required if we want to use res.comp or res.comp_algo sample fetches in
http-response rules.

This patch must be backported in 1.7.
2017-09-15 18:42:23 +02:00
Christopher Faulet
03d85538b3 BUG/MEDIUM: compression: Fix check on txn in smp_fetch_res_comp_algo
The check was totally messed up. In the worse case, it led to a crash, when
res.comp_algo sample fetch was retrieved on uncompressed response (with the
compression enabled).

This patch must be backported in 1.7.
2017-09-15 18:42:23 +02:00
Willy Tarreau
0bf6fa5e40 MEDIUM: session: count the frontend's connections at a single place
There are several places where we see feconn++, feconn--, totalconn++ and
an increment on the frontend's number of connections and connection rate.
This is done exactly once per session in each direction, so better take
care of this counter in the session and simplify the callers. At least it
ensures a better symmetry. It also ensures consistency as till now the
lua/spoe/peers frontend didn't have these counters properly set, which can
be useful at least for troubleshooting.
2017-09-15 11:49:52 +02:00
Willy Tarreau
0c4ed35225 MEDIUM: session: factor out duplicated code for conn_complete_session
session_accept_fd() may either successfully complete a session creation,
or defer it to conn_complete_session() depending of whether a handshake
remains to be performed or not. The problem is that all the code after
the handshake was duplicated between the two functions.

This patch make session_accept_fd() synchronously call
conn_complete_session() to finish the session creation. It is only needed
to check if the session's task has to be released or not at the end, which
is fairly minimal. This way there is now a single place where the sessions
are created.
2017-09-15 11:49:52 +02:00
Willy Tarreau
eaa7e44ad7 MINOR: session: small cleanup of conn_complete_session()
Commit 8e3c6ce ("MEDIUM: connection: get rid of data->init() which was
not for data") simplified conn_complete_session() but introduced a
confusing check which cannot happen on CO_FL_HANDSHAKE. Make it clear
that this call is final and will either succeed and complete the
session or fail.
2017-09-15 11:49:52 +02:00
Willy Tarreau
05f5047d40 MINOR: listener: new function listener_release
Instead of duplicating some sensitive listener-specific code in the
session and in the stream code, let's call listener_release() when
releasing a connection attached to a listener.
2017-09-15 11:49:52 +02:00
Willy Tarreau
6f5e4b98df MEDIUM: session: take care of incrementing/decrementing jobs
Each user of a session increments/decrements the jobs variable at its
own place, resulting in a real mess and inconsistencies between them.
Let's have session_new() increment jobs and session_free() decrement
it.
2017-09-15 11:49:52 +02:00
Willy Tarreau
2cc5bae0b8 MINOR: listeners: make listeners count consistent with reality
Some places call delete_listener() then decrement the number of
listeners and jobs. At least one other place calls delete_listener()
without doing so, but since it's in deinit(), it's harmless and cannot
risk to cause zombie processes to survive. Given that the number of
listeners and jobs is incremented when creating the listeners, it's
much more logical to symmetrically decrement them when deleting such
listeners.
2017-09-15 11:49:52 +02:00
Willy Tarreau
0de59fd53a MINOR: listeners: new function create_listeners
This function is used to create a series of listeners for a specific
address and a port range. It automatically calls the matching protocol
handlers to add them to the relevant lists. This way cfgparse doesn't
need to manipulate listeners anymore. As an added bonus, the memory
allocation is checked.
2017-09-15 11:49:52 +02:00
Willy Tarreau
31794892af MINOR: unix: remove the now unused proto_uxst.h file
Since everything is self contained in proto_uxst.c there's no need to
export anything. The same should be done for proto_tcp.c but the file
contains other stuff that's not related to the TCP protocol itself
and which should first be moved somewhere else.
2017-09-15 11:49:52 +02:00
Willy Tarreau
9d5be5c823 MINOR: protocols: register the ->add function and stop calling them directly
cfgparse has no business directly calling each individual protocol's 'add'
function to create a listener. Now that they're all registered, better
perform a protocol lookup on the family and have a standard ->add method
for all of them.
2017-09-15 11:49:52 +02:00