Skip to content

Commit

Permalink
dnsdist: Add support for multiple network interfaces in the XDP helper
Browse files Browse the repository at this point in the history
  • Loading branch information
rgacogne committed Feb 7, 2025
1 parent c282871 commit c9b84d7
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions contrib/xdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@

# Main
parser = argparse.ArgumentParser(description='XDP helper for DNSDist')
parser.add_argument('--interface', '-i', type=str, default='eth0',
help='The interface on which the filter will be attached')
parser.add_argument('--interface', '-i', type=str, default=['eth0'], action='append',
help='The interface(s) on which the filter will be attached')
parser.add_argument('--maps-size', '-m', type=int, default=1024,
help='Maximum number of entries in the eBPF maps')
parser.add_argument('--number-of-queues', '-q', type=int, default=64,
Expand All @@ -110,20 +110,25 @@
parameters = parser.parse_args()
cflag = [f'-DDDIST_MAX_NUMBER_OF_QUEUES={parameters.number_of_queues}',
f'-DDDIST_MAPS_SIZE={parameters.maps_size}']
interfaces = set(parameters.interface)

if parameters.xsk:
print(f'Enabling XSK (AF_XDP) on {parameters.interface}..')
for interface in interfaces:
print(f'Enabling XSK (AF_XDP) on {interface}..')
cflag.append('-DUseXsk')
else:
ports = [53]
ports_str = ', '.join(str(port) for port in ports)
print(f'Enabling XDP on {parameters.interface} and ports {ports_str}..')
for interface in interfaces:
print(f'Enabling XDP on {interface} and ports {ports_str}..')
IN_DNS_PORT_SET = "||".join("COMPARE_PORT((x),"+str(i)+")" for i in ports)
cflag.append(r"-DIN_DNS_PORT_SET(x)=(" + IN_DNS_PORT_SET + r")")

xdp = BPF(src_file="xdp-filter.ebpf.src", cflags=cflag)

fn = xdp.load_func("xdp_dns_filter", BPF.XDP)
xdp.attach_xdp(parameters.interface, fn, 0)
for interface in interfaces:
xdp.attach_xdp(interface, fn, 0)

v4filter = xdp.get_table("v4filter")
v6filter = xdp.get_table("v6filter")
Expand Down Expand Up @@ -195,7 +200,8 @@
leaf.action = qname[2]
qnamefilter[key] = leaf

print(f"Filter is ready on {parameters.interface}")
for interface in interfaces:
print(f"Filter is ready on {interface}")

try:
xdp.trace_print()
Expand Down Expand Up @@ -229,4 +235,5 @@
for item in xsk_destinations6.items():
print(f"- {str(socket.inet_ntop(socket.AF_INET6, item[0].addr))}:{str(socket.ntohs(item[0].port))}")

xdp.remove_xdp(parameters.interface, 0)
for interface in interfaces:
xdp.remove_xdp(interface, 0)

0 comments on commit c9b84d7

Please sign in to comment.