Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end.
When several customers, departments or projects share your address space, sooner or later someone asks which of them uses the bandwidth. Flow export answers that in detail but needs a collector. For a quick and lightweight answer, the edge router itself can count bytes and packets per /64 using nftables dynamic sets with counters. This guide builds such accounting on a Linux edge routing a free IPv6 /48 from IPYE, counting traffic for each /64 inside 2001:db8:100::/48 in both directions.
The idea
An nftables set can hold elements added at runtime by the rules themselves, and each element can carry its own counter. By masking each packet's address down to its /64 before adding it to the set, every /64 that sends or receives traffic gets one element with a running byte and packet count. No list of subnets has to be maintained by hand: new /64s appear automatically when they first see traffic.
The ruleset
table inet acct {
set in64 {
type ipv6_addr
flags dynamic, timeout
counter
timeout 1h
size 65536
}
set out64 {
type ipv6_addr
flags dynamic, timeout
counter
timeout 1h
size 65536
}
chain count {
type filter hook forward priority -10; policy accept;
ip6 daddr 2001:db8:100::/48 update @in64 { ip6 daddr & ffff:ffff:ffff:ffff:: }
ip6 saddr 2001:db8:100::/48 update @out64 { ip6 saddr & ffff:ffff:ffff:ffff:: }
}
}
The count chain hooks into forwarding with a priority that runs before ordinary filter chains, and its policy is accept, so it never blocks anything. For traffic entering the /48, the destination address is masked to its /64 and the set in64 is updated; for traffic leaving, the source address is masked and out64 is updated.
counter on the set gives each element its own counter. update refreshes the element's timeout each time it matches, so an element for an active /64 stays in the set, while a /64 that sees no traffic for an hour disappears. Size 65536 matches the number of /64s in a /48, so the set can never overflow.
Load it alongside your existing ruleset:
nft -c -f /etc/nftables.d/acct.nft
nft -f /etc/nftables.d/acct.nft
The -c check parses the file without applying it.
Read the counters
nft list set inet acct in64
nft -j list set inet acct in64 | jq -r '.nftables[] | select(.set) | .set.elem[]? | "\(.elem.val) \(.elem.counter.bytes)"'
The plain listing shows each /64 network address with its packets, bytes and remaining timeout. The JSON form is easier to process in scripts. The exact JSON layout varies slightly between nftables versions, so print it once before writing a parser.
Turn counters into graphs
Counters only ever increase, until an element times out or the table is reloaded. To graph rates, sample them periodically and store the differences. A small script run every minute that reads the JSON and writes the values in Prometheus text format to the node exporter's textfile directory works well. Graph the per-second rate in your dashboard, and compute monthly totals there as well.
Keep the sampling interval well below the set timeout, so an element does not expire between two samples while it is still active.
Reset or keep
Reloading the table resets every counter. For monthly reports, either record totals before any planned reload, or let the monitoring system accumulate rates over time so a reset only loses one interval. Avoid reloading the accounting table as part of unrelated firewall changes; keep it in its own file and table so it can stay untouched.
Cost
Each packet triggers one set update in each direction. On a small edge this is negligible. On a router forwarding very high packet rates, measure CPU use before and after. If accounting becomes expensive, flow sampling with an exporter is the more scalable approach.
What it does not tell you
These counters show volume per /64, not which applications, ports or remote networks were involved. For that you need flow export. They also count only forwarded traffic; traffic to and from the router itself goes through the input and output hooks, which you can add as further chains if needed.
The aggregate the upstream sees
The upstream carries the /48 as a whole, announced with the LOA from the IPYE portal and validated by the ROA that IPYE creates. Per-/64 accounting is entirely your own view of how that /48 is used.
Next step
Claim your free /48, give each customer or project its own /64, and count their traffic on the edge with a dynamic nftables set. Questions: ipye@turnel.com.
About IPYE
IPYE (https://ipye.com) leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. ASN and region changes apply automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS; WHOIS is editable on IPv6 /44 and larger. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. Claim your free /48: https://my.ipye.com/products/ipv6-rental. Questions: ipye@turnel.com.
Disclosure: I run IPYE
Top comments (0)