Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions .azure-pipelines/pr_test_scripts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ t0:
- dhcp_relay/test_dhcp_relay_stress.py
- dhcp_relay/test_dhcpv6_relay.py
- dhcp_server/test_dhcp_server.py
- dhcp_server/test_dhcp_server_lease_clear.py
- dhcp_server/test_dhcp_server_match.py
- dhcp_server/test_dhcp_server_match_cli.py
- dhcp_server/test_dhcp_server_match_config_change.py
- dhcp_server/test_dhcp_server_match_multi_vlans.py
- dhcp_server/test_dhcp_server_match_stress.py
- dhcp_server/test_dhcp_server_multi_vlans.py
- dhcp_server/test_dhcp_server_stress.py
- disk/test_disk_exhaustion.py
Expand Down
1,016 changes: 1,016 additions & 0 deletions tests/dhcp_server/dhcp_server_match_common.py

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions tests/dhcp_server/dhcp_server_match_flood.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""DHCP DISCOVER flood generator, run on the PTF host by test_dhcp_server_match_stress.py.

Usage: python3 dhcp_server_match_flood.py <config.json>

The configuration lists the clients (interface, MAC, Option 60, expected addresses), the send rate,
the duration, the reply deadline and the drain time. Every DISCOVER has a unique transaction ID, and
replies are correlated by transaction ID and client hardware address. A JSON summary is printed.
"""
import json
import random
import sys
import time

from scapy.all import AsyncSniffer, BOOTP, DHCP, Ether, IP, UDP, conf, mac2str


def build_discover(mac, xid, opt60):
options = [("message-type", "discover")]
if opt60:
options.append(("vendor_class_id", opt60.encode("utf-8")))
options.append("end")
return bytes(Ether(src=mac, dst="ff:ff:ff:ff:ff:ff") /
IP(src="0.0.0.0", dst="255.255.255.255") /
UDP(sport=68, dport=67) /
BOOTP(op=1, chaddr=mac2str(mac), xid=xid, flags=0x8000) /
DHCP(options=options))


def message_type(pkt):
for option in pkt[DHCP].options:
if isinstance(option, tuple) and option[0] == "message-type":
return option[1]
return None


def main(config_path):
with open(config_path) as config_file:
config = json.load(config_file)
clients = config["clients"]
rate, duration = config["rate"], config["duration"]
deadline, drain = config["deadline"], config["drain"]
ifaces = sorted(set(client["iface"] for client in clients))
sockets = {iface: conf.L2socket(iface=iface) for iface in ifaces}
replies = {}

def on_packet(pkt):
if BOOTP not in pkt or pkt[BOOTP].op != 2 or DHCP not in pkt:
return
key = (pkt[BOOTP].xid, bytes(pkt[BOOTP].chaddr)[:6])
replies.setdefault(key, []).append((time.time(), pkt[BOOTP].yiaddr, message_type(pkt)))

sniffer = AsyncSniffer(iface=ifaces, filter="udp and (port 67 or port 68)", prn=on_packet, store=False)
sniffer.start()
time.sleep(2)

sent = []
xid_base = random.randint(1, 0x7fffffff)
total = int(rate * duration)
started = time.time()
for index in range(total):
target = started + float(index) / rate
client = clients[index % len(clients)]
xid = (xid_base + index) & 0xffffffff
packet = build_discover(client["mac"], xid, client.get("opt60"))
delay = target - time.time()
if delay > 0:
time.sleep(delay)
sent_at = time.time()
sockets[client["iface"]].send(packet)
sent.append((xid, client, sent_at))
send_elapsed = time.time() - started
time.sleep(deadline + drain)
sniffer.stop()
for sock in sockets.values():
sock.close()

result = {"sent": len(sent), "send_seconds": round(send_elapsed, 2), "offers": 0, "missing": 0, "late": 0,
"wrong_pool": 0, "unexpected": 0, "latencies": [], "errors": []}
for xid, client, sent_at in sent:
client_replies = replies.get((xid, bytes(mac2str(client["mac"]))), [])
expected = client.get("expect")
if not expected:
if client_replies:
result["unexpected"] += 1
result["errors"].append("unmatched {} got {}".format(client["mac"], client_replies[0][1]))
continue
if not client_replies:
result["missing"] += 1
continue
# Latency is measured to the first reply; any invalid reply, even after a valid one, is an error.
result["offers"] += 1
latency = client_replies[0][0] - sent_at
result["latencies"].append(round(latency, 4))
invalid = [reply for reply in client_replies if reply[2] != 2 or reply[1] not in expected]
if invalid:
result["wrong_pool"] += 1
result["errors"].append("{} with {} got type {} address {}".format(
client["mac"], client.get("opt60"), invalid[0][2], invalid[0][1]))
elif latency > deadline:
result["late"] += 1
result["errors"] = result["errors"][:20]
print(json.dumps(result))


if __name__ == "__main__":
main(sys.argv[1])
2 changes: 2 additions & 0 deletions tests/dhcp_server/dhcp_server_test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def ping_dut_refresh_fdb(ptfhost, interface):
def clean_dhcp_server_config(duthost):
keys = duthost.shell("sonic-db-cli CONFIG_DB KEYS DHCP_SERVER_IPV4*")
clean_order = [
"DHCP_SERVER_IPV4_BINDING",
"DHCP_SERVER_IPV4_MATCH",
"DHCP_SERVER_IPV4_CUSTOMIZED_OPTIONS",
"DHCP_SERVER_IPV4_RANGE",
"DHCP_SERVER_IPV4_PORT",
Expand Down
Loading
Loading