Skip to content
Merged
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ble-loadgen

BLE advertisement load generator for the ESPresense HIL bench. It advertises from a
**static random** address ~40 times a second, rotating through a pool of 4096 of them, so
**static random** address ~40 times a second, rotating through a fixed pool of 1024 of them, so
every rotation costs a listening node a new fingerprint slot — a room full of phones,
compressed. It exists to make the slow heap decline in
[ESPresense#2309](https://github.com/ESPresense/ESPresense/issues/2309) show up in a HIL
Expand Down Expand Up @@ -67,7 +67,7 @@ sudo python3 ble_flood.py --index 0 --pool 512 # smaller address pool

### Address pool

Addresses come from a pool (default 4096, or `$BLE_FLOOD_POOL`) and repeat once it wraps.
Addresses are a fixed set, `f1:ad:00:00:00:00` upwards (default 1024, or `$BLE_FLOOD_POOL`), and repeat once they wrap. Every run uses the same set, so downstream rows are reused across runs rather than minted anew; the `f1ad` prefix is the same one the flood's iBeacon UUIDs carry, so consumers can filter both the same way.
The churn a node sees is unchanged — every rotation is still a different MAC until the pool
wraps — but anything downstream that keys off the address stops growing at `--pool` rows.
That matters because the flood's addresses escape the bench: ESPresense Companion turns each
Expand Down
47 changes: 20 additions & 27 deletions ble_flood.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
"""Flood the bench with BLE advertisements, rotating through a pool of random addresses.
"""Flood the bench with BLE advertisements, rotating through a fixed pool of addresses.

Why: ESPresense fingerprints a *static random* address by MAC (BleFingerprint.cpp,
ID_TYPE_RAND_STATIC_MAC when the top two bits of the MSB are set), so every rotation
Expand All @@ -15,9 +15,9 @@
Runs as a detached step in the HIL pipeline (.woodpecker/hil.yml), so it floods for the
run and the runner stops it at the end — no external gating.

By default the addresses come from a pool of 4096 and repeat once it wraps. The node still
By default the addresses are the fixed set f1:ad:00:00:00:00 to f1:ad:00:00:03:ff and repeat once they wrap. The node still
sees a new MAC every rotation, but subscribers that key off the address (ESPresense Companion
-> MQTT discovery -> a Home Assistant device_tracker each, BlueZ's own cache) stop at 4096
-> MQTT discovery -> a Home Assistant device_tracker each, BlueZ's own cache) stop at 1024
rows instead of growing for as long as the flood runs. --pool 0 restores unbounded minting.

ble_flood.py --index 0 --rate 40 # flood until killed
Expand All @@ -33,7 +33,6 @@
import glob
import math
import os
import random
import secrets
import select
import signal
Expand Down Expand Up @@ -84,7 +83,12 @@
PROGRESS_INTERVAL = 30.0 # seconds between progress lines in the log
# Enough churn to exhaust a node's 100-200 fingerprint slots many times over, small enough
# that downstream subscribers keyed on the address stay a bounded table. 0 = unbounded.
DEFAULT_POOL = 4096
DEFAULT_POOL = 1024
# Pool addresses are f1:ad:00:<index>, the same set on every run. A run that minted a fresh
# random pool still left --pool new rows downstream per pipeline; a fixed set is reused instead.
# f1 has the top two bits set (static random), and the prefix mirrors LOADGEN_UUID_PREFIX so
# consumers can drop the flood's addresses the same way they drop its beacons.
POOL_PREFIX = bytes.fromhex("f1ad00")

# iBeacon adverts for the bench. A real beacon keeps its MAC but changes its proximity UUID while
# moving (BlueCharm BC04P, ESPresense#2492), and ESPresense must track each UUID as its own
Expand Down Expand Up @@ -130,10 +134,7 @@ def validate_period(period):
raise ValueError(f"period must be a positive, finite number of seconds, got {period!r}")


POOL_SEED = "ble-loadgen-pool-v1"


def address_pool(size, seed=POOL_SEED):
def address_pool(size):
"""A fixed set of static random addresses to cycle through, instead of endless new ones.

Unbounded rotation is a load test for the node but a leak for everything downstream:
Expand All @@ -144,21 +145,12 @@ def address_pool(size, seed=POOL_SEED):
-- it still sees a new MAC every rotation until the pool wraps -- while the downstream
row count stops at len(pool).

The pool is drawn from a seeded generator, so every run cycles the *same* addresses.
A fresh draw per run only bounds a single run: HIL floods on every pipeline, and each
run left another len(pool) devices behind (12k in Home Assistant after 20 runs at
--pool 1024). A smaller pool is a prefix of a larger one, so changing --pool does not
mint new addresses either.
The set is the same on every run (f1:ad:00:00:00:00 upwards), so those rows are reused
across runs rather than minted anew. Little-endian on the wire: the index comes first.
"""
rng = random.Random(seed)
pool = []
seen = set()
while len(pool) < size:
addr = random_static_address(rng)
if addr not in seen:
seen.add(addr)
pool.append(addr)
return pool
if size > 1 << 24:
raise ValueError(f"pool must fit in the 3 index bytes, got {size}")
return [index.to_bytes(3, "little") + POOL_PREFIX[::-1] for index in range(size)]


def random_static_address(rng=None):
Expand Down Expand Up @@ -562,7 +554,7 @@ def flood(sock, rate, stop_after, until=None, pool_size=DEFAULT_POOL,
validate_period(beacon_period)
pool = address_pool(pool_size) if pool_size else None
if pool:
print(f"[flood] cycling a pool of {len(pool)} addresses", flush=True)
print(f"[flood] cycling a pool of {len(pool)} addresses, {pool[0][::-1].hex()}..{pool[-1][::-1].hex()}", flush=True)
beacons = [random_static_address() for _ in range(beacon_count)]
for index, address in enumerate(beacons):
print(f"[flood] ibeacon {index} mac {address[::-1].hex()} major {index + 1}", flush=True)
Expand Down Expand Up @@ -635,9 +627,10 @@ def selftest():
assert len(addr) == 6 and addr[5] & 0xC0 == 0xC0, addr.hex()
assert [pool[n % len(pool)] for n in (0, 255, 256, 257)] == [
pool[0], pool[255], pool[0], pool[1]], "pool must wrap"
assert address_pool(256) == pool, "every run must cycle the same addresses"
assert address_pool(64) == pool[:64], "a smaller pool must reuse the larger one's addresses"
assert address_pool(16, seed="other") != address_pool(16), "the seed picks the pool"
# A fixed set: the same addresses on every run, so downstream rows are reused, not minted.
assert address_pool(256) == pool, "pool must be the same every run"
assert pool[0][::-1].hex() == "f1ad00000000" and pool[255][::-1].hex() == "f1ad000000ff", pool[0].hex()
assert address_pool(257)[256][::-1].hex() == "f1ad00000100"

# $BLE_FLOOD_POOL drives the default, and junk in the environment must not kill a run.
os.environ["BLE_FLOOD_POOL"] = "512"
Expand Down
Loading