Skip to content

sonic-yang: model BGP afi_safi as an enumeration instead of an unconstrained string#28606

Open
bhouse-nexthop wants to merge 2 commits into
sonic-net:masterfrom
nexthop-ai:sonic-yang-model-bgp-afi-safi-as
Open

sonic-yang: model BGP afi_safi as an enumeration instead of an unconstrained string#28606
bhouse-nexthop wants to merge 2 commits into
sonic-net:masterfrom
nexthop-ai:sonic-yang-model-bgp-afi-safi-as

Conversation

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator

Why I did it

The afi_safi list keys across the BGP models were declared type string with no enumeration, so any value was valid YANG. Both ipv4-unicast (hyphen) and ipv4_unicast (underscore) validated and committed, producing two distinct AF rows — only the underscore spelling is functional, since CONFIG_DB, bgpcfgd and frrcfgd all use underscores.

This is a model gap, not a CLI defect. nhcli derives all config-mode validation and completion from the YANG, so an unconstrained key is faithfully accepted; the fix belongs in the model. It also explains the original report (Randy Williams) that tab-completion walks you to the afi-safi position and then leaves you to type the value blind — there were no enum values for the CLI to offer.

Two further BGP leaves had the same shape of problem — declared type string while their consumers accept only a specific form — and are fixed here too: BGP_GLOBALS.rr_cluster_id and the BGP_DEVICE_GLOBAL confederation peers list.

How I did it

Added a bgp_afi_safi typedef to sonic-bgp-common.yang with the canonical underscore values, plus two restrictions for the contexts whose vocabulary is narrower, and wired all five afi_safi leaves to them:

Table Type Values
BGP_NEIGHBOR_AF / BGP_PEER_GROUP_AF bgp_afi_safi ipv4_unicast, ipv6_unicast, l2vpn_evpn, ipv4_srpolicy, ipv6_srpolicy
BGP_GLOBALS_AF bgp_afi_safi_global as above, minus SR-Policy
BGP_GLOBALS_AF_AGGREGATE_ADDR bgp_afi_safi_global "
BGP_GLOBALS_AF_NETWORK bgp_afi_safi_global "
BGP_DEVICE_GLOBAL_AF bgp_afi_safi_device_global ipv4_unicast, ipv6_unicast

The per-context sets are derived from the actual consumers, not from prose. SR-Policy is excluded from the globals tables because frrcfgd installs no global SR-Policy config (frrcfgd.py:3618). BGP_DEVICE_GLOBAL_AF is unicast-only because AFI_SAFI_MAP in bgpcfgd/managers_device_global_af.py:9 has exactly two entries and log_warns away anything else. This follows how these files already model sibling leaves (bgp_peer_type, bgp_community_type, bgp_tx_add_paths_type are all enumerations).

Constraining the key type makes several hand-written guards unreachable, so they are removed:

  • the three must "not(contains(., 'srpolicy'))" clauses on the BGP_GLOBALS_AF lists
  • the install_backup_path unicast must on BGP_DEVICE_GLOBAL_AF, whose key type now admits only the two unicast families

The equivalent install_backup_path guard on BGP_GLOBALS_AF is keptl2vpn_evpn is still a valid key there, so that constraint is still live.

*_multicast is deliberately excluded: no consumer implements it. The BGP_DEVICE_GLOBAL_AF_LIST description previously advertised ipv4_multicast / ipv6_multicast / l2vpn_evpn, none of which AFI_SAFI_MAP supports; that description is corrected.

No CLI code changes. libyang validates a list key against its declared type, so an out-of-enum afi_safi is rejected at commit across every entry point, and the enum values become completion candidates.

Additionally constrained (same class of gap, not afi_safi):

  • sonic-bgp-global.yang rr_cluster_id — renders into bgp cluster-id, which takes an IPv4 address or a 32-bit integer. Now a union of inet:ipv4-address and uint32 { range "1..4294967295"; }.
  • sonic-bgp-device-global.yang CONFED peers — documented as semi-colon-separated sub-ASNs, previously unvalidated. Now pattern "[1-9][0-9]*(;[1-9][0-9]*)*". The pattern enforces the shape only; it does not range-check each sub-ASN against the 32-bit AS maximum, which would need a leaf-list.

The existing "66000;63000" sample in the tests and in doc/Configuration.md satisfies the new pattern. No existing rr_cluster_id values exist anywhere in the repo.

How to verify it

Build and see yang model tests succeed.

Which release branch to port

  • 202605
  • 202511
  • 202505

Tested branch (Please provide the tested image version)

  • master as of 20260723

Description for the changelog

sonic-yang: constrain BGP afi_safi list keys, rr_cluster_id and confederation peers instead of leaving them unconstrained strings

Link to config_db schema for YANG module changes

https://github.com/sonic-net/sonic-buildimage/blob/master/src/sonic-yang-models/doc/Configuration.md#bgp_globals_af

A picture of a cute animal (not mandatory but encouraged)

…trained string

Add a bgp_afi_safi enumeration typedef in sonic-bgp-common (with a
bgp_afi_safi_global restriction that excludes the SR-Policy families) and
use it for the afi_safi list keys that were previously an unconstrained
'type string':

  * sonic-bgp-cmn-af (BGP_NEIGHBOR_AF / BGP_PEER_GROUP_AF) uses the full
    bgp_afi_safi vocabulary, including the SR-Policy families.
  * BGP_GLOBALS_AF_LIST, BGP_GLOBALS_AF_AGGREGATE_ADDR_LIST and
    BGP_GLOBALS_AF_NETWORK_LIST use bgp_afi_safi_global, so the SR-Policy
    families are rejected at validation time.

Constraining the key rejects non-canonical spellings such as
'ipv4-unicast' at commit time and lets YANG-derived CLIs offer the valid
values as completion candidates. This is a model-only change; validation
and completion are both derived from the model.

Also tighten two neighbouring free-string leaves:

  * rr_cluster_id (BGP_GLOBALS) becomes a union of an IPv4 address and a
    non-zero 32-bit integer.
  * the BGP confederation peers leaf gains a pattern enforcing its
    semi-colon-separated sub-ASN shape.

Signed-off-by: Brad House - Nexthop <bhouse@nexthop.ai>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants