Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ jobs:
# Runs every pytest-based test suite via `make check-all-pytest`.
# See TEST_PYTEST/TEST_MD in test/travis_run_linux.sh.
- name: Python pytest test suites
config: --enable-mods-shared=reallyall --with-mpm=event --enable-mpms-shared=all
config: --enable-mods-shared=reallyall --with-mpm=event --enable-mpms-shared=all --enable-proxy-beacon
pkgs: nghttp2-client
env: |
APR_VERSION=1.7.6
Expand Down
3 changes: 3 additions & 0 deletions changes-entries/proxy-balancer-add-worker.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*) mod_proxy_balancer: Add a worker to a balancer at run time under the
balancer's lock and only once it is complete, so that request threads
cannot find a half-built worker. [Jim Jagielski]
4 changes: 4 additions & 0 deletions changes-entries/proxy-define-worker-rollback.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*) mod_proxy: Don't leave a half-initialized worker in the balancer (or
server config) worker array when ap_proxy_define_worker() fails, e.g. for
a too long hostname or Unix domain socket path, which crashed the child
on a later walk of the array. [Jim Jagielski]
3 changes: 3 additions & 0 deletions changes-entries/proxy-set-wstatus-atomic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*) mod_proxy: Update the shared worker status atomically, so that the
balancer-manager, health checks and mod_proxy_beacon changing different
bits at the same time cannot undo each other's change. [Jim Jagielski]
3 changes: 3 additions & 0 deletions changes-entries/proxy-worker-beacon-status.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*) mod_proxy: Add the PROXY_WORKER_BEACON worker status bit (flag 'B',
shown as "Bcn" in the balancer-manager) to mark workers created and
managed by mod_proxy_beacon. [Jim Jagielski]
3 changes: 3 additions & 0 deletions changes-entries/proxy_beacon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*) mod_proxy_beacon: Back-end reverse proxy servers can announce
themselves and be auto-added to their front-end proxy balancer.
[Jim Jagielski]
1 change: 1 addition & 0 deletions docs/manual/mod/allmodules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<modulefile>mod_proxy.xml</modulefile>
<modulefile>mod_proxy_ajp.xml</modulefile>
<modulefile>mod_proxy_balancer.xml</modulefile>
<modulefile>mod_proxy_beacon.xml</modulefile>
<modulefile>mod_proxy_connect.xml</modulefile>
<modulefile>mod_proxy_express.xml</modulefile>
<modulefile>mod_proxy_fcgi.xml</modulefile>
Expand Down
608 changes: 608 additions & 0 deletions docs/manual/mod/mod_proxy_beacon.xml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions docs/manual/mod/mod_proxy_beacon.xml.meta
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- GENERATED FROM XML: DO NOT EDIT -->

<metafile reference="mod_proxy_beacon.xml">
<basename>mod_proxy_beacon</basename>
<path>/mod/</path>
<relpath>..</relpath>

<variants>
<variant>en</variant>
</variants>
</metafile>
4 changes: 4 additions & 0 deletions docs/manual/new_features_2_4.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@
<dd>systemd integration. It allows httpd to be used in a service with the systemd
<code>Type=notify</code>.</dd>

<dt><module>mod_proxy_beacon</module> (available since 2.4.71)</dt>
<dd>Backends announce themselves over UDP and are added to a
<module>mod_proxy_balancer</module> balancer automatically.</dd>

</dl>
</section>

Expand Down
3 changes: 2 additions & 1 deletion include/ap_mmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,15 @@
* 20120211.140 (2.4.64-dev) Add ap_set_time_process_request() to scoreboard.h
* 20120211.141 (2.4.64-dev) add ap_stat_check() to httpd.h
* 20120211.142 (2.4.64-dev) Add ap_*_timingsafe() to httpd.h
* 20120211.143 (2.4.71-dev) Add PROXY_WORKER_BEACON status bit to mod_proxy.h
*/

#define MODULE_MAGIC_COOKIE 0x41503234UL /* "AP24" */

#ifndef MODULE_MAGIC_NUMBER_MAJOR
#define MODULE_MAGIC_NUMBER_MAJOR 20120211
#endif
#define MODULE_MAGIC_NUMBER_MINOR 142 /* 0...n */
#define MODULE_MAGIC_NUMBER_MINOR 143 /* 0...n */

/**
* Determine if the server's current MODULE_MAGIC_NUMBER is at least a
Expand Down
221 changes: 221 additions & 0 deletions modules/proxy/README.beacon
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
mod_proxy_beacon

Self-registering reverse-proxy balancer membership: backend servers announce
themselves to a front-end proxy over UDP datagrams, and the proxy adds, enables,
and evicts them as live balancer members. See
docs/manual/mod/mod_proxy_beacon.xml for the user-facing directive reference;
this file describes the architecture.

The transport is plain APR UDP sockets -- small, periodic, fire-and-forget
announcements with no third-party dependency.


Dependencies:
Requires mod_watchdog and mod_proxy_balancer. No external library: the UDP
transport uses APR sockets and authentication uses SipHash from APR-util (no
OpenSSL dependency).


Transport (unicast UDP, not multicast):
Each backend sends a point-to-point UDP datagram (apr_socket_sendto) to the
proxy's configured address. This is deliberately UNICAST -- unlike
mod_heartbeat, which multicasts to a group. Multicast is filtered on most
networks and does not traverse the public Internet, so a cross-host control
plane must be unicast. The socket-handling pattern is adapted from
mod_heartmonitor's unicast receive path.

Datagrams are fire-and-forget. The design tolerates UDP loss and reordering
without a reconnect or framing layer: a lost announcement just delays an update
by one interval (announcements are periodic and idempotent), and an out-of-order
datagram is rejected by the per-url monotonic timestamp (see Authentication).


Roles:
Backend = sender (ProxyBeaconAddress): sends to the proxy, advertising its
own routable URL (ProxyBeaconAdvertise) periodically.
Proxy = receiver (ProxyBeaconListen): binds a stable address, receives
announcements, and manages balancer://<name> (ProxyBeaconBalancer).

The proxy is the fixed unicast rendezvous; each backend is configured with its
address. One proxy receiver serves many backend senders. A leading scheme in
an address (e.g. tcp://) is accepted and ignored.

ProxyBeaconListen's host and port are both optional: an omitted host/port (or
no argument at all) is inherited from the server's own address/port. Since UDP
and TCP are independent port spaces, the beacon socket can share the server's
service port without colliding with its TCP listener -- so a backend can beacon
to the proxy at its real service endpoint. (The listener binds in an
unprivileged watchdog child, so privileged ports like 80/443 cannot be shared
this way.) The sender's ProxyBeaconAddress targets the proxy and must be given
in full.


Where it runs:
All background work runs in a single mod_watchdog SINGLETON callback -- exactly
one child process owns the socket (so only one process binds the receive port
and performs membership changes). The watchdog fires every ~100ms:

STARTING open the UDP socket; receiver binds, sender resolves its dest.
RUNNING sender: send a throttled announcement (ProxyBeaconInterval).
receiver: drain the socket, then run a throttled (~1s) eviction
sweep.
STOPPING close the socket.

Because this relies on a singleton watchdog child, it is inactive under the
prefork MPM.


Adding/removing members (reuses the balancer-manager path):
Runtime worker *removal* does not exist in httpd, so membership is managed
entirely as status-flag changes via mod_proxy_balancer's exported
balancer_manage() optional function -- the same code the balancer-manager web
UI calls. The receiver synthesizes a minimal request_rec (no real client
request exists in the watchdog thread; see beacon_make_fake_request) and calls:

add b=<name> b_nwrkr=<url> b_wyes=1 b_nwrkr_flags=B
(worker created, disabled, marked)
enable b=<name> w=<url> w_status_D=0 (cleared -> serves traffic)
evict b=<name> w=<url> w_status_D=1 (disabled -> out of rotation)

Adds bump the balancer's shm "updated" timestamp, so every other child picks
up the new worker through the normal ap_proxy_sync_balancer() path -- the
change made in the singleton propagates fleet-wide. The target balancer must
have spare slots (ProxySet growth / BalancerGrowth).

Per-backend state (last-seen time, added/evicted flags) is kept in a hash in
the singleton's own pool -- no shared memory is needed, since the singleton is
the only process that adds or evicts. A re-announcement after eviction
re-enables the worker; the flags make these transitions idempotent.


Message format:
Plain ASCII, space-separated key=value tokens, one datagram per announcement:

BEACON url=http://host:port host=<h> pid=<n> seq=<n> ts=<usec> mac=<hex>

url= is the routable backend origin the proxy adds as a BalancerMember. ts=
(microseconds since the epoch) and mac= authenticate the message and are always
present, since a shared secret is required (see below). host=/pid=/seq= are
informational.

Member identity:
url= names an ORIGIN -- scheme://host[:port] -- and nothing else. The receiver
canonicalizes it (lower-case scheme and host, the scheme's default port dropped,
one trailing "/" ignored) and uses that single string as the member's identity:
the key of its lease/replay record in ctx->seen, the worker name handed to
balancer_manage(), and the name used for every later enable/evict. Keeping
those equal matters because the balancer finds a worker by LONGEST-PREFIX match
(ap_proxy_get_worker) while ap_proxy_define_worker() stores a normalized name:
tracking the raw announced string made "http://h:80" never find its worker
"http://h" (member added but left disabled, add still logged as a success), and
let "http://h/one" and "http://h/one/two" hold two leases over one worker.

Anything that is not an origin -- a path other than "/", query, fragment,
userinfo, a bad port, or the "unix:/path|url" worker syntax (no host part) -- is
dropped with a rate-limited "unacceptable url" log. ProxyBeaconAdvertise applies
the same rule at config time and announces the canonical form. After an add,
and before any enable/evict, the worker is looked up again and the operation is
reported as failed if it is not there, since balancer_manage() returns success
when it finds no such worker. Path-based routing belongs in ProxyPass on the
proxy, not in the advertised URL.

A UDP datagram is delivered whole, so the receiver reads each message into a
fixed stack buffer and NUL-terminates it (buf[len] = '\0') before parsing.
The sender transmits strlen(msg) bytes (no trailing NUL on the wire).


Ownership and watchdog hand-off:
Every worker beacon creates is marked PROXY_WORKER_BEACON (status 'B', "Bcn" in
the balancer-manager) in the worker's SHARED state. Beacon enables/evicts only
marked workers; a same-named worker without the mark (configured, or added by
an administrator) is left alone and the announcement is dropped ("member not
managed by beacon").

The leases (ctx->seen) are private to the process that owns the watchdog
singleton, but the members they describe live in shared memory and outlive that
process. When ownership moves (owner crashed, was recycled, or the server was
restarted), the new owner starts with an empty table and would never evict a
member that has since gone silent. So, once at the start of ownership
(beacon_reconcile(), first RUNNING tick of a listener), it syncs the balancer
(ap_proxy_sync_balancer()) and adopts every marked member: last_seen = now, so
a silent member is evicted one ProxyBeaconTimeout later; a member that is
already DISABLED is recorded as evicted, so its next announcement re-enables it.

Not recovered: the per-member replay high-water mark (last_ts restarts at 0).
The global freshness window (ProxyBeaconMaxSkew) still applies, so a captured
announcement can be replayed once per member after a hand-off, refreshing that
one lease for one timeout. Keeping that state across hand-off would need a
shared lease table; the marker + reconcile design was chosen as the smaller,
ABI-safe change (the status field is existing shared state, bit 0x1000 is new).

A new owner can only start once the watchdog-callback mutex is released by the
dead one. If the default mechanism is not released when its holder is killed on
a platform, use e.g. "Mutex sysvsem watchdog-callback".

Operator control:
Beacon owns the DISABLED flag of its members (lease expiry sets it, a new
announcement clears it). It never touches STOPPED, DRAIN or the hcheck
failure flag, so an administrator who needs a member held out of rotation uses
Stopped/Drain; a manual "Disable" is overridden by the next announcement after
an eviction. (After a hand-off a DISABLED beacon member is assumed to have been
evicted, for the same reason.)

Receiver validation:
Datagrams > BEACON_MAX_MSG_LEN (2048) are dropped, not truncated (recvfrom is
asked for one byte more to tell); a datagram containing a NUL is dropped. The
MAC is verified on every path, including log-only (no ProxyBeaconBalancer).
After the MAC: strict grammar (beacon_frame_check(): "BEACON " marker,
single-space key=value tokens, known fields not repeated), ts= strictly 1-18
decimal digits (beacon_parse_ts()), freshness compared in microseconds so a
sub-second ProxyBeaconMaxSkew is honoured. The url must be printable ASCII
and an origin (see Member identity).

Capacity:
At most BEACON_MAX_MEMBERS (256) urls are tracked. beacon_sweep() also frees
records that are evicted or whose add failed once they have been silent for
2 * max_skew + 1s: the record doubles as the replay high-water mark, and a
message accepted at time A carries ts within max_skew of A, so it is stale after
A + 2 * max_skew; nothing is lost by forgetting it then. A reclaimed member that
announces again is re-adopted from the balancer (try_add finds the marked worker).
Records come from a free list: memory is bounded by the cap however often they
are recycled. A flood of distinct urls from a secret holder can still fill the
table until it falls silent.

Socket open:
A failed open/bind/resolve is retried with back-off (30s doubling to 10min),
not on every tick and not never. The address lookup is synchronous on the one
watchdog thread shared by all beacon contexts; prefer IP literals.

Authentication (ProxyBeaconSecret, REQUIRED):
ProxyBeaconSecret must be set on every participating server (the receiver and
every sender); startup fails otherwise. There is no unauthenticated mode:
without a MAC, anyone who can reach the receiver port could announce a URL and
hijack client traffic, and a UDP source address is trivially spoofable, so
authentication matters at least as much here as it would over a connection.

With ProxyBeaconSecret set identically on the proxy and all backends, each
announcement is signed with a SipHash-2-4 MAC over the message prefix (the key
is derived from the passphrase via apr_md5, as in mod_session_crypto). The
receiver recomputes the MAC (constant-time compare) and rejects anything that
does not match -- before the URL is parsed or acted on. Replay protection has
two parts, both keyed on the signed ts= (microseconds): (1) a freshness window
(ProxyBeaconMaxSkew) rejects messages whose timestamp is too far from now,
which assumes proxy and backend clocks are roughly synchronized (NTP); (2) a
per-url high-water mark rejects any ts= that does not strictly exceed the last
one accepted for that url, so a byte-identical replay (same ts, same MAC) --
e.g. replaying a dead backend's last announcement to keep it from being evicted
-- is dropped even within the freshness window. Microsecond granularity ensures
genuine sub-second announcements always advance. seq= is NOT used for replay
(it resets when a backend restarts); the wall-clock ts= does not.

"hex:<32 hex digits>" is the 128-bit SipHash key itself (openssl rand -hex 16):
no passphrase, nothing to guess. Any other value is a passphrase of at least 16
characters; the key is then MD5(passphrase) and the tag is 64 bits, so a
captured announcement permits offline guessing at full speed. A valid MAC shows possession of the
secret, nothing more: every holder can register any reachable origin, speak for
other members and consume slots/records. The secret is a membership-
administrator credential, not a boundary between partially trusted workloads.

Announcements are authenticated, not encrypted -- the payload is operational
metadata, not secret. For transport confidentiality, DTLS would be a separate,
orthogonal future layer.
13 changes: 13 additions & 0 deletions modules/proxy/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ APACHE_MODULE(proxy_balancer, Apache proxy BALANCER module. Requires --enable-p
APACHE_MODULE(proxy_express, mass reverse-proxy module. Requires --enable-proxy., , , most, , proxy)
APACHE_MODULE(proxy_hcheck, [reverse-proxy health-check module. Requires --enable-proxy and --enable-watchdog.], , , most, , [proxy,watchdog])

proxy_beacon_objs="mod_proxy_beacon.lo"
APACHE_MODULE(proxy_beacon, [reverse-proxy UDP member announcement; backends self-register as balancer members. Requires --enable-proxy, --enable-proxy-balancer and --enable-watchdog.], $proxy_beacon_objs, , no, [
dnl apr_siphash.h is in APR-util 1.6 and later (and APR 2.x)
saved_CPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $APR_INCLUDES $APU_INCLUDES"
AC_TRY_COMPILE([#include <apr_siphash.h>], [], [ap_have_apr_siphash=yes], [ap_have_apr_siphash=no])
CPPFLAGS="$saved_CPPFLAGS"
if test "$ap_have_apr_siphash" = "no"; then
AC_MSG_WARN([mod_proxy_beacon needs apr_siphash.h, from APR-util 1.6 or later])
enable_proxy_beacon=no
fi
], [proxy,watchdog,proxy_balancer])

APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current -I\$(top_srcdir)/modules/http2])

module_selection=$save_module_selection
Expand Down
5 changes: 3 additions & 2 deletions modules/proxy/mod_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ proxy_wstat_t PROXY_DECLARE_DATA proxy_wstat_tbl[] = {
{PROXY_WORKER_HOT_SPARE, PROXY_WORKER_HOT_SPARE_FLAG, "Spar "},
{PROXY_WORKER_FREE, PROXY_WORKER_FREE_FLAG, "Free "},
{PROXY_WORKER_HC_FAIL, PROXY_WORKER_HC_FAIL_FLAG, "HcFl "},
{PROXY_WORKER_BEACON, PROXY_WORKER_BEACON_FLAG, "Bcn "},
{0x0, '\0', NULL}
};

Expand Down Expand Up @@ -1480,7 +1481,7 @@ static int proxy_handler(request_rec *r)
*/
if (balancer
&& !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
worker->s->status |= PROXY_WORKER_IN_ERROR;
ap_proxy_set_wstatus(PROXY_WORKER_IN_ERROR_FLAG, 1, worker);
worker->s->error_time = apr_time_now();
}
break;
Expand All @@ -1492,7 +1493,7 @@ static int proxy_handler(request_rec *r)
*/
if (balancer
&& !(worker->s->status & PROXY_WORKER_IGNORE_ERRORS)) {
worker->s->status |= PROXY_WORKER_IN_ERROR;
ap_proxy_set_wstatus(PROXY_WORKER_IN_ERROR_FLAG, 1, worker);
worker->s->error_time = apr_time_now();
}
}
Expand Down
2 changes: 2 additions & 0 deletions modules/proxy/mod_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ struct proxy_conn_pool {
#define PROXY_WORKER_FREE 0x0200
#define PROXY_WORKER_HC_FAIL 0x0400
#define PROXY_WORKER_HOT_SPARE 0x0800
#define PROXY_WORKER_BEACON 0x1000 /* created and managed by mod_proxy_beacon */

/* worker status flags */
#define PROXY_WORKER_INITIALIZED_FLAG 'O'
Expand All @@ -343,6 +344,7 @@ struct proxy_conn_pool {
#define PROXY_WORKER_FREE_FLAG 'F'
#define PROXY_WORKER_HC_FAIL_FLAG 'C'
#define PROXY_WORKER_HOT_SPARE_FLAG 'R'
#define PROXY_WORKER_BEACON_FLAG 'B'

#define PROXY_WORKER_NOT_USABLE_BITMAP ( PROXY_WORKER_IN_SHUTDOWN | \
PROXY_WORKER_DISABLED | PROXY_WORKER_STOPPED | PROXY_WORKER_IN_ERROR | \
Expand Down
Loading
Loading