Skip to content

sonic-cfggen should return boolean values as lowercase true and false#22526

Open
bhouse-nexthop wants to merge 1 commit intosonic-net:masterfrom
bhouse-nexthop:bhouse-nexthop/cfggen-boolean
Open

sonic-cfggen should return boolean values as lowercase true and false#22526
bhouse-nexthop wants to merge 1 commit intosonic-net:masterfrom
bhouse-nexthop:bhouse-nexthop/cfggen-boolean

Conversation

@bhouse-nexthop
Copy link
Collaborator

@bhouse-nexthop bhouse-nexthop commented May 3, 2025

Why I did it

YANG mandates that boolean values are true and false, having the first letter be capital as is represented by Python would cause yang validation failures inside of libyang. Because of this, it appears a different yang type in sonic-types.yang has a secondary boolean_types that is used that allows more formats.

This can be seen, for instance, when setting:

"MGMT_VRF_CONFIG": {
  "vrf_global": {
    "mgmtVrfEnabled": "true"
  }
}

Then running config save -y. The written value is then "True", if then just running config replace /etc/sonic/config_db.json it would result in an error of:

Invalid value "True" in "mgmtVrfEnabled" element.
Work item tracking

How I did it

Capture native yang boolean values by looking up the yang schema when writing the config_db.json via sonic-cfggen and ensure they are written as lowercase.

How to verify it

See above for reproduction method

Which release branch to backport (provide reason below if selected)

  • 201811
  • 201911
  • 202006
  • 202012
  • 202106
  • 202111
  • 202205
  • 202211
  • 202305
  • 202411

Tested branch (Please provide the tested image version)

master as of 20250502

Description for the changelog

sonic-cfggen should return boolean values as lowercase true and false

Link to config_db schema for YANG module changes

N/A

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

Fixes #22527
Signed-off-by: Brad House bhouse@nexthop.ai

@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

bradh352 added a commit to bradh352/sonic-buildimage that referenced this pull request May 3, 2025
…net#22526)

YANG mandates that boolean valeus are true and false, having
the first letter be capital as is represented by Python would
cause yang validation failures inside of libyang.

This can be seen, for instance, when setting:
```
"MGMT_VRF_CONFIG": {
  "vrf_global": {
    "mgmtVrfEnabled": "true"
  }
}
```

Then running `config save -y`.  The written value is then "True", if
then just running `config replace /etc/sonic/config_db.json` it would
result in an error of:

```
Invalid value "True" in "mgmtVrfEnabled" element.
```

Signed-off-by: Brad House <bhouse@nexthop.ai>
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352
Copy link
Collaborator

bradh352 commented May 3, 2025

build failures are caused by bad PR #22432

@vmittal-msft vmittal-msft requested a review from qiluo-msft May 7, 2025 15:50
@vmittal-msft vmittal-msft added MSFT Triaged this issue has been triaged labels May 7, 2025
@bhouse-nexthop bhouse-nexthop force-pushed the bhouse-nexthop/cfggen-boolean branch from ee405bb to d88c322 Compare May 9, 2025 00:49
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bhouse-nexthop bhouse-nexthop force-pushed the bhouse-nexthop/cfggen-boolean branch from d88c322 to f62721d Compare May 20, 2025 15:48
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bhouse-nexthop
Copy link
Collaborator Author

Thanks @lguohan I made a couple minor modifications and replied to the rest.

@bhouse-nexthop bhouse-nexthop force-pushed the bhouse-nexthop/cfggen-boolean branch from 0dced08 to 4386703 Compare October 24, 2025 14:41
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352
Copy link
Collaborator

these spurious test failures in completely unrelated code paths are getting bad. I just rebased to force a rebuild.

@TafkaMax
Copy link

these spurious test failures in completely unrelated code paths are getting bad. I just rebased to force a rebuild.

Also probably needs a rebuild again?

@bhouse-nexthop bhouse-nexthop force-pushed the bhouse-nexthop/cfggen-boolean branch from 4386703 to 07f19c0 Compare October 25, 2025 13:27
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@bradh352
Copy link
Collaborator

these spurious test failures in completely unrelated code paths are getting bad. I just rebased to force a rebuild.

Also probably needs a rebuild again?

$!*% yep

@TafkaMax
Copy link

these spurious test failures in completely unrelated code paths are getting bad. I just rebased to force a rebuild.

Also probably needs a rebuild again?

$!*% yep

Second time it worked :-)

@bradh352
Copy link
Collaborator

bradh352 commented Oct 27, 2025

@lguohan any chance you can look at this and let me know if you have similar concerns to @qiluo-msft ? It at most loads the yang models once per sonic-cfggen execution to do proper field type validation to prevent an invalid configdb yang schema from being written out.

@lguohan
Copy link
Collaborator

lguohan commented Oct 27, 2025

let me talk to qi when he is back tomorrow. do you have some measurable data to show that this won't add much overhead?

@lguohan
Copy link
Collaborator

lguohan commented Oct 27, 2025

btw, the pr description/title probably should update saying that introducing yang model validation in sonic-cfggen?

@lguohan lguohan requested a review from vaibhavhd October 27, 2025 17:00
@bhouse-nexthop
Copy link
Collaborator Author

btw, the pr description/title probably should update saying that introducing yang model validation in sonic-cfggen?

The description does say it under the "How I did it" section.

@bhouse-nexthop
Copy link
Collaborator Author

let me talk to qi when he is back tomorrow. do you have some measurable data to show that this won't add much overhead?

Considering this is only loading the yang models and not actually performing the validation step, I'd assume the hit would be negligible. That said, I don't have hard numbers. Let me see if I can collect some before / after numbers for you on real hardware.

@qiluo-msft qiluo-msft self-assigned this Nov 5, 2025
@qiluo-msft
Copy link
Collaborator

I am trying to repro the original issue mentioned in PR description, but failed to repro. I could not observe 'The written value is then "True"'

admin@sonic:~$ sonic-db-cli CONFIG_DB hset "MGMT_VRF_CONFIG|vrf_global" "mgmtVrfEnabled" true
1
admin@sonic:~$ sonic-db-cli CONFIG_DB hgetall "MGMT_VRF_CONFIG|vrf_global"
{'mgmtVrfEnabled': 'true'}
admin@sonic:~$ sudo config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json
admin@sonic:~$ grep -C 2 "mgmtVrfEnabled" /etc/sonic/config_db.json
    "MGMT_VRF_CONFIG": {
        "vrf_global": {
            "mgmtVrfEnabled": "true"
        }
    },

@bhouse-nexthop
Copy link
Collaborator Author

I am trying to repro the original issue mentioned in PR description, but failed to repro. I could not observe 'The written value is then "True"'

admin@sonic:~$ sonic-db-cli CONFIG_DB hset "MGMT_VRF_CONFIG|vrf_global" "mgmtVrfEnabled" true
1
admin@sonic:~$ sonic-db-cli CONFIG_DB hgetall "MGMT_VRF_CONFIG|vrf_global"
{'mgmtVrfEnabled': 'true'}
admin@sonic:~$ sudo config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json
admin@sonic:~$ grep -C 2 "mgmtVrfEnabled" /etc/sonic/config_db.json
    "MGMT_VRF_CONFIG": {
        "vrf_global": {
            "mgmtVrfEnabled": "true"
        }
    },

I modified the config_db.json and didn't play with the sonic-db-cli. I haven't looked at how sonic-db-cli writes that, but I'm guessing its writing the 'true' as a string. A string would be preserved in the original case. It actually has to be in Redis as a boolean value for this issue to be observed.

@bhouse-nexthop
Copy link
Collaborator Author

bhouse-nexthop commented Nov 14, 2025

Performance numbers below.

Platform: x86_64-mlnx_msn2700-r0
HwSKU: ACS-MSN2700

Pretty slow cpu:

root@sw1:~# cat /proc/cpuinfo | grep "model name" | head -n1
model name	: Intel(R) Celeron(R) CPU 1047UE @ 1.40GHz

with patch:

root@sw1:~# time config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json

real	0m1.754s
user	0m1.374s
sys	0m0.328s
root@sw1:~# time config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json

real	0m1.605s
user	0m1.341s
sys	0m0.237s
root@sw1:~# time config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json

real	0m1.616s
user	0m1.362s
sys	0m0.215s

without patch:

root@sw1:~# time config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json

real	0m1.239s
user	0m1.044s
sys	0m0.152s
root@sw1:~# time config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json

real	0m1.204s
user	0m1.004s
sys	0m0.170s
root@sw1:~# time config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json

real	0m1.197s
user	0m1.014s
sys	0m0.164s

YANG mandates that boolean valeus are true and false, having
the first letter be capital as is represented by Python would
cause yang validation failures inside of libyang.

This can be seen, for instance, when setting:
```
"MGMT_VRF_CONFIG": {
  "vrf_global": {
    "mgmtVrfEnabled": "true"
  }
}
```

Then running `config save -y`.  The written value is then "True", if
then just running `config replace /etc/sonic/config_db.json` it would
result in an error of:

```
Invalid value "True" in "mgmtVrfEnabled" element.
```

Signed-off-by: Brad House <bhouse@nexthop.ai>
@bhouse-nexthop bhouse-nexthop force-pushed the bhouse-nexthop/cfggen-boolean branch from 07f19c0 to 250c00f Compare November 14, 2025 16:19
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@qiluo-msft
Copy link
Collaborator

I am trying to repro the original issue mentioned in PR description, but failed to repro. I could not observe 'The written value is then "True"'

admin@sonic:~$ sonic-db-cli CONFIG_DB hset "MGMT_VRF_CONFIG|vrf_global" "mgmtVrfEnabled" true
1
admin@sonic:~$ sonic-db-cli CONFIG_DB hgetall "MGMT_VRF_CONFIG|vrf_global"
{'mgmtVrfEnabled': 'true'}
admin@sonic:~$ sudo config save -y
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /etc/sonic/config_db.json
admin@sonic:~$ grep -C 2 "mgmtVrfEnabled" /etc/sonic/config_db.json
    "MGMT_VRF_CONFIG": {
        "vrf_global": {
            "mgmtVrfEnabled": "true"
        }
    },

I modified the config_db.json and didn't play with the sonic-db-cli. I haven't looked at how sonic-db-cli writes that, but I'm guessing its writing the 'true' as a string. A string would be preserved in the original case. It actually has to be in Redis as a boolean value for this issue to be observed.

Could you provide your repro steps for the original issue? I do not understand "It actually has to be in Redis as a boolean value" since redis only support string as value. If you modify config_db.json and trigger a bug, I prefer to treat it as invalid input bug and just fix the input validation logic and fail sonic-cfggen immediately, so yang dependency is not needed.

@bhouse-nexthop
Copy link
Collaborator Author

bhouse-nexthop commented Nov 14, 2025

steps to reproduce are:

  1. Edit /etc/sonic/config_db.json and insert this valid section:
"MGMT_VRF_CONFIG": {
  "vrf_global": {
    "mgmtVrfEnabled": "true"
  }
}
  1. Run config replace /etc/sonic/config_db.json which applies only that diff to redis successfully
  2. Run config save -y to rewrite the redis database to /etc/sonic/config_db.json
  3. Run config replace /etc/sonic/config_db.json, this will FAIL because step Update README.md #3 wrote "True" for mgmtVrfEnabled

The trigger to this behavior is the fact that somewhere the Pythonic True / False are being written when serializing from Redis.

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

Labels

MSFT Triaged this issue has been triaged YANG YANG model related changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: sonic-cfggen returning some boolean values as True and False, breaking YANG validation

8 participants