Skip to content

Allow to override the mgmt gateway of SONiC switches - #2948

Merged
ideaship merged 2 commits into
mainfrom
implement/issue-2396-sonic-mgmt-gateway
Dec 1, 2026
Merged

ideaship merged 2 commits into
mainfrom
implement/issue-2396-sonic-mgmt-gateway

Conversation

@berendt

@berendt berendt commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

A SONiC switch can now get its management gateway from NetBox. If the rendered config context of a device has a usable IPv4 address under _segment_mgmt_gateway_ipv4, the config generator writes it as the next hop of STATIC_ROUTE["mgmt|0.0.0.0/0"] in place of the MetalBox IP. A device without the key gets the same config as before.

Closes #2396

Commits

  1. Allow to override the mgmt gateway of SONiC switches adds the key, the helpers _get_mgmt_gateway_override and _get_impossible_next_hop_reason, the call in generate_sonic_config, and the tests.
    • A prefix length is accepted and dropped.
    • An unset or empty value falls back to the MetalBox IP without a warning.
    • Any other value that is not an IPv4 address falls back to the MetalBox IP with a warning. Only strings are parsed, because ipaddress reads the integer 42 as 0.0.0.42.
    • An address that can never be a next hop falls back to the MetalBox IP in the same way, and the warning names the reason. These are the OOB IP of the switch itself, the network and broadcast address of the OOB network (only when its prefix length is below 31), 0.0.0.0, 127.0.0.0/8, multicast addresses and 255.255.255.255. Link-local addresses are not rejected.
    • Any other address outside the OOB network is used as given, with a warning.
    • If neither an override nor a MetalBox IP is found, the next hop stays None, as before. A test covers this.
    • NTP, DNS and the control-plane ACLs do not change.
  2. Document the SONiC mgmt gateway override adds docs/sonic-mgmt-gateway.md and links it in the README. The page also says that the key needs an OOB IP in NetBox, which addresses are rejected, and that the route has no next hop if there is neither a usable override nor a MetalBox IP.

Verification

Run locally with the tool versions pinned in the OSISM Zuul roles:

  • Before the generator change, the two tests that expect the override as next hop failed because the route had the MetalBox IP or None. The new helper test file failed on import. All of them pass now.
  • Before the rejection of impossible next hops was added, its 13 test cases failed. The tests for both peers of a /31 and for link-local addresses already passed and still do.
  • pytest tests/unit/tasks/conductor/sonic: 861 passed (820 on the base commit 2174a40e).
  • pytest: 4270 passed, 3 xfailed (4229 passed, 3 xfailed on the base commit).
  • flake8 on the changed Python files, and black --check . and mypy on the whole repository: no findings.
  • The three existing OOB tests pass unmodified. The diff of their file only adds lines.
  • git diff --stat 2174a40e -- tests/e2e/ files/sonic/ is empty. _add_ntp_configuration and _add_dns_configuration are identical to the base commit.

Not exercised locally, only OSISM's Zuul can verify these:

  • python-osism-sonic-e2e passes against the unchanged goldens. The check pipeline of this pull request runs it, because the change is under osism/tasks/conductor/.
  • The check jobs flake8, python-black, mypy and python-osism-unit-tests pass. The check pipeline runs them too. The local runs above are evidence, not a replacement.

Deviations from the approved plan

  • The plan in Allow to override the gateway in the mgmt network for Sonic devices #2396 uses every valid IPv4 address and only warns if it is outside the OOB network. After ideaship's review, addresses that can never be a next hop are rejected with a warning and the MetalBox IP is used. The documentation covers this, the OOB IP requirement and the case without any next hop.

Open questions

None declined.

Noticed, not touched

  • osism sonic validate --generate (osism/commands/sonic.py:1361-1387) does not load the MetalBox cache. Only sync_sonic does (osism/tasks/conductor/sonic/sync.py:68). From reading the code (not run), the command finds no MetalBox IP, so without the new key it validates a config with next hop None and no NTP or DNS server. This is an existing bug in another file.
  • README.md:21-22 (line numbers with this change) gives pipenv install --dev and pipenv run pytest for the full unit suite. Pipfile.lock lacks six packages from requirements.txt: boto3, fastapi, python-openstackclient, tabulate, uvicorn and websockets. CI gets them with pip install .. This is test setup, not part of the issue.
  • The documentation list in README.md does not include the existing page docs/collection-plans.md. The issue asks for one new entry only.

🤖 Generated with Claude Code

@berendt
berendt marked this pull request as ready for review November 29, 2026 09:11
@berendt berendt moved this from New to Ready for review in Human Board Nov 29, 2026
@berendt
berendt requested a review from ideaship November 29, 2026 09:11
@berendt berendt self-assigned this Nov 29, 2026

gateway = str(interface.ip)
network = ipaddress.ip_network(f"{oob_ip}/{prefix_len}", strict=False)
if interface.ip not in network:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some values can never work as a next hop, but they get through with either no warning or a misleading one.

These are inside the OOB network, so they're accepted with only the INFO log. For a switch at 10.42.0.5/24:

  • 10.42.0.5, the switch's own address. This is an easy copy-paste mistake in local context data, and it costs the switch its management reachability.
  • 10.42.0.0 and 10.42.0.255, the network and broadcast addresses.

These are outside the OOB network, so they're still used, with the "may be unreachable" warning:

  • 0.0.0.0
  • 127.0.0.0/8
  • multicast addresses
  • 255.255.255.255

Could all of these take the same path as a non-IPv4 value: warn, then fall back to the MetalBox IP? Two cases need care:

  • Reject the network and broadcast addresses only when prefix_len < 31. In a /31 they are the two usable peers.
  • Leave link-local addresses alone. Inside a 169.254.0.0/16 OOB network they're valid gateways, and outside it the existing membership warning already covers them.

A parametrized test with each rejected case, plus a /31 peer that is still accepted, would pin this down.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 19de804. All the addresses you list now take the same path as a non-IPv4 value: one warning that names the reason, then the MetalBox IP. The check is in the new _get_impossible_next_hop_reason and runs before the OOB network check, so the "may be unreachable" warning no longer appears for them.

  • The network and broadcast addresses are rejected only when the prefix length is below 31.
  • Link-local addresses are left alone. Outside the OOB network they still get the existing warning.

Tests: test_get_mgmt_gateway_override_impossible_next_hop_returns_none_and_warns has each rejected case, including 10.42.0.5/24 (the own address with its prefix length) and the network and broadcast addresses of a /30. test_get_mgmt_gateway_override_slash_31_peer_is_accepted covers both peers of a /31. Two more tests cover link-local addresses inside and outside the OOB network.

Comment thread docs/sonic-mgmt-gateway.md Outdated

## Setting another gateway

Set the key `_segment_mgmt_gateway_ipv4` to use another gateway. The generator

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you say here that the key only takes effect when the switch has an OOB IP in NetBox? Without one, no mgmt default route is written at all, whether or not the override is set.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 29083ed. The section now says that the key only takes effect when the switch has an OOB IP in NetBox, and that without one no default route is written for the mgmt VRF, whether or not the key is set.


## Fallback

- If the key is not set, or its value is empty, the MetalBox IP is used.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two additions, please:

  • Once the rejection above is in, list the addresses that are rejected.
  • Add a bullet for the case where the override is absent or rejected and no MetalBox IP is found either. The route is then written with no next hop. Without that bullet, this section reads as if the fallback always produces a working route.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 29083ed. The section now lists the rejected addresses, with the /31 exception. A new bullet says that if the key is not set, or its value is empty or rejected, and no MetalBox IP is found either, the route is written without a next hop and does not work.

@github-project-automation github-project-automation Bot moved this from Ready for review to In review in Human Board Nov 30, 2026
berendt and others added 2 commits November 30, 2026 18:21
The generator wrote the MetalBox IP as next hop of the default route
in the mgmt VRF. STATIC_ROUTE is rebuilt on every regen, so a
deployment whose management network uses another gateway could not
correct the route anywhere.

The next hop now comes from the config context key
_segment_mgmt_gateway_ipv4 when a device has it, in the same way as
the _segment_* keys for syslog and SNMP. NetBox merges local context
data over the assigned config contexts, so the key works for a whole
segment and for a single switch. NTP and DNS stay on the MetalBox IP,
and a device without the key gets the same config as before.

A value with a prefix length is accepted, because NetBox shows
addresses that way. Anything else that is not an IPv4 address is
ignored with a warning and the MetalBox IP is used. Raising would make
the sync skip the device, so one bad segment context would block all
changes for every switch it covers. Only strings are parsed, since
ipaddress reads the integer 42 as 0.0.0.42.

An address that can never be a next hop is ignored in the same way:
the OOB IP of the switch itself, the network and broadcast address of
the OOB network, 0.0.0.0, loopback and multicast addresses, and
255.255.255.255. In a /31 both addresses are usable peers, so the
network and broadcast check does not apply there. Link-local addresses
are allowed, because they are valid gateways in a link-local OOB
network. Any other address outside the OOB network is used as given,
with a warning.

Tests: the helper with usable, unset and unusable values, with each
kind of rejected address, with both peers of a /31, with link-local
addresses, and with an address outside an IPv4 and an IPv6 OOB
network. In the orchestrator, the override with and without a
MetalBox, the fallback for an invalid value, the unchanged None next
hop without either address, and a device without an OOB IP.

Part of #2396.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Christian Berendt <berendt@osism.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The new page describes the default next hop of the mgmt default
route, where _segment_mgmt_gateway_ipv4 can be set in NetBox, that it
only takes effect with an OOB IP, which values it accepts and rejects,
when the MetalBox IP is used instead, what happens if there is none,
and that NTP, DNS and the control-plane ACLs do not change. The README
links it.

Part of #2396.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Christian Berendt <berendt@osism.tech>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@berendt
berendt force-pushed the implement/issue-2396-sonic-mgmt-gateway branch from 4e9fd39 to 29083ed Compare November 30, 2026 18:21
@berendt
berendt requested a review from ideaship November 30, 2026 18:22
@ideaship
ideaship merged commit f54ffc3 into main Dec 1, 2026
2 checks passed
@ideaship
ideaship deleted the implement/issue-2396-sonic-mgmt-gateway branch December 1, 2026 07:00
@github-project-automation github-project-automation Bot moved this from In review to Done in Human Board Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Allow to override the gateway in the mgmt network for Sonic devices

2 participants