NAME
App::Netdisco::Util::L3Domain
DESCRIPTION
Helper subroutines for grouping VRFs into layer 3 domains.
A domain is a set of VRF instances (one per device) that form one routing network. Each VRF is keyed by the first evidence it has, in this order:
- 1. manual
-
A
vrf_domainsentry places it. Keymanual:<domain>. - 2. L3 I-SID
-
Key
isid:<n>. VRFs with the same L3 I-SID share a domain. - 3. route targets
-
Key
rt:<imports>/<exports>. VRFs join only when their de-duplicated, sorted import and export sets are identical and at least one is non-empty. Hub, shared-services and extranet VRFs have different sets, so they never bridge customers. An identical route distinguisher is not evidence: the VRF Naming Inconsistencies report lists it as a hint. - 4. name
-
Key
name:<vrf>. The same name joins only VRFs that have neither an I-SID nor route targets.
The key is stored in l3_domain.inferred_name. A domain's display name is its most common member VRF name (ties go to the alphabetically first), or the configured name for a manual domain. The default table (vrf = '') is domain 0 and never has a key or member rows.
The vrf_domains setting is a flat list of entries, each { domain, device, vrf, join }:
vrf_domains:
- domain: CUSTOMER-A
device: 192.0.2.1
vrf: blue
join: true # brings in every VRF sharing blue's key
- domain: CUSTOMER-A
device: 192.0.2.10
vrf: RED-LITE # a plain entry moves just this VRF
- domain: CUSTOMER-A
device: group:branch-ces # every router in a host_groups group
vrf: RED-LITE
When entries place one VRF more than once, the most specific wins: an IP entry, then a group: entry, then a VRF brought in by another entry's join: true. So a VRF can be split out even from inside a joined domain or a group. Within one level the alphabetically first domain name wins, with a warning. A join: true entry still brings in its VRF's partners when another entry takes that VRF itself, so splitting a joined VRF out leaves its partners where they were, unless entries place them too.
A group: device is matched live with acl_matches_only at every recompute. A group that holds a hostname or regular expression rule, directly or through another group:, is refused with a warning: the ACL matcher answers such a rule with an uncached DNS lookup per device, and the recompute runs on every discover, so a large group with slow DNS would add minutes to each one. A cron rule is refused too, as it makes membership depend on the time of day. Address, prefix, range, prop:, tag:, cf:, group: and op:and rules are allowed.
The entries are read through one function, manual_l3_entries, so a table could supply them later without any other change to the recompute.
There are no default exports, however the :all tag will export all subroutines.
EXPORT_OK
l3_domain_evidence_sql()
Returns a SQL SELECT over device_vrf (named VRFs only) yielding ip, vrf, key, reason and display. The key is the first evidence the VRF has: isid:<n> for an L3 I-SID, then rt:<imports>/<exports> when it has any route targets (each set de-duplicated and sorted, ignoring NULL and empty elements, so order and repeats do not matter), then name:<vrf>. A route-target key longer than 200 bytes is replaced by rt:md5:<digest> of its full text, because the key is in a unique btree index that a few hundred route targets would overflow; equal sets still give equal keys. reason is the text stored on the member and display is the VRF name, for naming the domain.
manual_l3_entries( [$schema] )
Returns the entries of the vrf_domains setting that are valid, as an arrayref of { domain, device, vrf, join }. Each entry is one future row of a manual-domain table, which is why the setting is a flat list:
vrf_domains:
- domain: CUSTOMER-A
device: 192.0.2.1 # a PE: bring its whole L3VPN domain
vrf: blue
join: true
- domain: CUSTOMER-A
device: 192.0.2.10 # the VRF-lite CE: move just this VRF
vrf: RED-LITE
- domain: CUSTOMER-A
device: group:branch-ces # every CE in a named group
vrf: RED-LITE
device is one full IP address of any interface of a known device (short forms such as 10.1 are refused), returned as the device's canonical IP (all addresses are resolved in one query, by the rule get_device uses), or exactly one group:<name> from host_groups, returned unchanged. join is false unless set. An entry that is malformed, names an unknown device, or names a group that is not cheap to match is skipped with a warning that says what to change. A group is matched against every candidate device on every recompute, so it (and every group it reaches through group:, negated or not) may hold only these rules: an address, a prefix, a range, prop:, tag:, cf:, group: and op:and. A hostname or regular expression rule would cost a DNS lookup per device, and a cron rule makes membership depend on the time of day, so a group holding one is refused. A group whose group: references form any cycle is refused too. Returns [] without querying when the setting is empty.
manual_l3_members( $schema, \@entries, [$evidence] )
Returns the (ip, vrf) pairs the entries place, as an arrayref of { ip, vrf, domain, level, direct }. An entry places the VRF on its device (or on each device in its group that has that VRF in device_vrf), and a join entry also places every VRF sharing the evidence key of the VRF it placed, with direct false. level is ip, group or join, the order that decides a pair claimed twice (an IP entry, then a group entry, then a joined VRF); within a level the alphabetically first domain wins, with a warning. An entry whose VRF is not in device_vrf places nothing and logs nothing; it takes effect once the device reports that VRF.
recompute_l3_domains( $schema )
Brings the members of l3_domain_member into line with device_vrf and the vrf_domains setting. A VRF placed by a manual entry (see manual_l3_members) is a manual member of the domain keyed manual:<domain>, named as configured. Every other VRF other than the default table joins the domain of its evidence key (see l3_domain_evidence_sql), as an inferred member. Domains are matched on inferred_name, which is unique; name is for display and may repeat. An inferred domain is named after its most common member VRF name, ties going to the alphabetically first. The recompute owns the manual rows: it writes them to match the entries, so removing an entry, or its VRF leaving device_vrf, returns the VRF to inference at the next recompute. Domains other than 0 left with no members are deleted. A domain keeps its id across recomputes while its key is in use.
The recompute is one SQL statement, so the evidence query, the expensive part, runs once and no temporary table is made. (Placing manual entries runs the same query filtered to the VRF names or keys they name.) Only what changed is written. An unchanged network and setting write nothing.
Runs in one transaction under with_l3_domain_lock. Returns the number of member rows inserted plus deleted plus domains renamed or deleted, so 0 means everything was already right. Discover calls it once per device, after the transaction that refreshes device_vrf commits, when l3_domain_recompute_needed says so.
l3_domain_recompute_needed( $schema, $device_changed, $by_arpnip? )
True when a caller that has just refreshed one device's device_vrf rows should run recompute_l3_domains, false when the recompute would find nothing to change. It is true when any of these holds:
- 1.
$device_changedis true -
The caller found that the device's evidence changed (VRFs added or removed count). No SQL runs.
- 2.
vrf_domainsis set (not for arpnip) -
Any non-empty value, a malformed one included, so that the recompute warns about it. Group membership can change with no device change, so a configured site recomputes every time.
- 3.
l3_domain_memberholds amanualrow (not for arpnip) -
This covers removing the last
vrf_domainsentry. - 4. a domain other than 0 has an
inferred_namewith none of the key prefixes, or none -
A key from before the evidence keys, which was the bare VRF name, and a VRF name may hold a
:. One cheap query re-keys it. - 5. a named
device_vrfrow has nol3_domain_memberrow - 6. an
l3_domain_memberrow has nodevice_vrfrow -
Both are primary-key anti-joins. They make a failed recompute that left rows unplaced be retried by the next discover anywhere.
- 7. a domain other than 0 has no member
-
Deleting a device removes its
device_vrfand member rows together, so neither 5 nor 6 fires. The next discover anywhere then deletes the empty domain. It is checked last, as the most expensive.
Arpnip passes $by_arpnip. It runs hourly, so it leaves 2 and 3 to the device's daily discover, which picks up a vrf_domains or group membership change.
An evidence change on another device is recomputed by that device's own discover. A failed recompute after a pure route-target or I-SID change (no VRF added or removed) stays stale until the next change. So does the display name of a domain that lost members to a device delete but still has some (cosmetic).
with_l3_domain_lock( $schema, \&code )
Runs $code inside a transaction (joining the caller's, if one is open) after pg_advisory_xact_lock(1729, 0), and returns what it returns. Every writer of l3_domain or l3_domain_member goes through this, so that it does not interleave with a recompute.
results_span_domains( \@rows )
True when any of the report rows, each carrying vrf (its domain's name), is in a named domain; two domains always include one. Reports show a VRF column only then, so that a network using only the default table sees the same report as before.
label_rows_by_domain( \@rows, $multi_domain )
Returns copies of the report rows shaped for a table rendered in the browser. When $multi_domain is true (any row in a named domain, see results_span_domains), vrf is kept and the default table's empty name reads "default". Otherwise every row is in the default table and vrf is dropped, so the page is the same as before domains.
row_l3_domain( $row, $router )
Returns { id => ..., name => ... } for the L3 domain of a result row with a vrf column, or undef when the domain cannot be determined. $router is the address of the device that reported the row (undef when unknown). The default table is domain 0 without a query. The query runs on the row's own schema, so tenants work.