Skip to content

Commit cd1b2da

Browse files
authored
Merge branch 'master' into repd3
2 parents 4705465 + a181a5a commit cd1b2da

File tree

6 files changed

+221
-0
lines changed

6 files changed

+221
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
# SONiC Config Update Validation via YANG
2+
3+
## Table of Contents
4+
5+
- [1 Revision](#revision)
6+
- [2 Project Overview](#project-overview)
7+
- [3 Case Study: PortChannel](#case-study)
8+
- [4 Design](#design)
9+
- [4.1 Design Summary](#design-summary)
10+
- [4.2 YANG Validation Implementation](#yang-validation-implementation)
11+
- [4.3 Additional Features](#additional-features)
12+
13+
14+
### Revision
15+
16+
| Rev | Date | Author | Change Description |
17+
| :---: | :-----: | :--------------: | ------------------ |
18+
| 1.0 | 08/2022 | Isabel Li | Phase 1 Design |
19+
20+
## Project Overview
21+
22+
### Problem Statement
23+
Currently, SONiC config fields updates are validated ad-hoc, and much of the validation criteria are duplicated both in python (hard-coded validation specs) and in YANG models
24+
25+
### Goal
26+
Refactor code to utilize preexisting YANG models for config validation, instead of duplicating the validation specs in both YANG and in python
27+
28+
### Scope
29+
30+
#### Python
31+
1. SONiC config fields updated via config CLI (e.g. config portchannel add <…>). A comprehensive list of config fields in this project’s scope is as follows, defined in sonic-utilities/config directory:
32+
- AAA
33+
- ACL
34+
- BGP
35+
- Console
36+
- DHCP Relay
37+
- Drop Counter
38+
- Dynamic Buffer Management
39+
- ECN
40+
- Feature
41+
- Interface
42+
- Interface Naming Mode
43+
- Interface Vrf Binding
44+
- IPv6 Link Local
45+
- Kubernetes
46+
- Linux Kernel Dump
47+
- Loopback Interfaces
48+
- VRF
49+
- Management VRF
50+
- Mirroring
51+
- Muxcable
52+
- NAT Enable/Disable YANG Validation
53+
- NTP
54+
- NVGRE
55+
- PBH
56+
- Platform Component Firmware
57+
- PortChannel
58+
- QoS
59+
- sFlow
60+
- SNMP
61+
- Subinterfaces
62+
- Syslog
63+
- VLAN
64+
- Warm Restart
65+
- Watermark
66+
- ZTP
67+
3. ACL rules: incremental update, delete
68+
4. pfc
69+
5. crm
70+
6. mclag
71+
7. counterpoll
72+
8. Scripts (python)
73+
a. configlet
74+
b. db_migrator
75+
c. dropconfig
76+
d. mellanox_buffer_migrator
77+
e. neighbor_advertisor
78+
f. null_route_helper
79+
g. hostcfgd
80+
81+
#### C++
82+
1. buffermgrd
83+
84+
85+
## Case Study
86+
87+
### Portchannel
88+
89+
PortChannel config CLI is defined in sonic-utilities/config/main.py. In this file, we find the following config validation:
90+
![](../../images/config_yang_validation/adhoc_portchannel.png)
91+
92+
However, these specs for validation are also defined in the sonic-portchannel YANG model:
93+
![](../../images/config_yang_validation/yang_portchannel.png)
94+
95+
For PortChannel and many of the config fields listed above, the field validation is unnecessarily duplicated in sonic-utilities/config and in YANG models. The goal of this project is to utilize preexisting YANG models to validate config CLI field updates, without unnecessarily defining separate, redundant validation specs ad-hoc in sonic-utilities.
96+
97+
Some necessary ad-hoc validations are not yet reflected in YANG models. For those missing validations that YANG infrastructure supports, GitHub issues are created to track the progress to filling in these YANG model gaps. In the uncommon scenario where YANG infrastructure is incapable of suporting a certain check, ad-hoc validation will be left in-place.
98+
99+
During the migration process, we will first leave the ad-hoc validation code in-place, and leave an option to configure type of validation used: ad-hoc or YANG validation. Once YANG validation has stabilized and YANG validation coverage has widened, we will remove the preexisting ad-hoc validation code for non performance-sensitive scenarios.
100+
101+
In performance-sensitive scenarios, such as warm and fast reboot, ad-hoc validation will remain the mode of validation, as GCU is more time-intensive.
102+
103+
104+
## Design
105+
106+
### Design Summary
107+
Currently, updates to ConfigDB are published via 2 steps:
108+
1. ad-hoc ConfigDB field validation
109+
2. Call to config_db.set_entry(target_value) which blindly pushes the target_value passed via connection to ConfigDB using ConfigDBConnector object
110+
111+
Where YANG models are applicable, this project replaces these two steps with a ConfigDBConnector class decorator which validates the new config using preexisting YANG models before pushing the new config update to ConfigDB.
112+
113+
Why use ConfigDBConnector class decorator:
114+
- allows us to preserve the preexisting instance of ConfigDBConnector (no new object initialization is required)
115+
- decorates only the necessary instances of ConfigDBConnector with added YANG validation
116+
- Non-invasive, all ConfigDBConnector code not writing to ConfigDB (no YANG validation necessary) can be left as-is
117+
118+
### Decorator Graphical Depiction
119+
Existing ConfigDBConnector class and methods-
120+
121+
![](../../images/config_yang_validation/decorator1.png)
122+
123+
Decorator only overrides contents of original set_entry() method to include YANG validation
124+
125+
![](../../images/config_yang_validation/decorator2.png)
126+
127+
### YANG Validation Implementation
128+
The YANG validation included in our ConfigDBConnector set_entry() decorator leverages the preexisting SONiC Generic Config Updater library, which uses YANG to validate JSON patches that contain write requests to ConfigDB, and then pushes these updates to ConfigDB only after YANG validation passes.
129+
130+
Sample JSON patch:
131+
132+
![](../../images/config_yang_validation/jsonpatch.png)
133+
134+
Notably, there is a performance penalty when moving from ad-hoc validation to GCU-based YANG validation. Because the bulk of this project focuses on non-performance-sensitive scenarios such as user-based CLI writes to ConfigDB, this sacrifice in performance is deemed worthwhile. Performance-sensitive scenarios will be handled on a case-by-base basis, in some cases leaving ad-hoc validation as necessary.
135+
136+
#### Performance Comparison
137+
138+
139+
140+
##### YANG Validation via GCU Performance
141+
```
142+
admin@vlab-01:~$ time sudo config portchannel add PortChannel04
143+
Patch Applier: Patch application starting.
144+
Patch Applier: Patch: [{"op": "add", "path": "/PORTCHANNEL/PortChannel04", "value": {"admin_status": "up", "mtu": "9100", "lacp_key": "auto", "min_links": "1"}}]
145+
Patch Applier: Getting current config db.
146+
Patch Applier: Simulating the target full config after applying the patch.
147+
Patch Applier: Validating target config does not have empty tables, since they do not show up in ConfigDb.
148+
Patch Applier: Sorting patch updates.
149+
Patch Applier: The patch was sorted into 1 change:
150+
Patch Applier: * [{"op": "add", "path": "/PORTCHANNEL/PortChannel04", "value": {"admin_status": "up", "mtu": "9100", "lacp_key": "auto", "min_links": "1"}}]
151+
Patch Applier: Applying 1 change in order:
152+
Patch Applier: * [{"op": "add", "path": "/PORTCHANNEL/PortChannel04", "value": {"admin_status": "up", "mtu": "9100", "lacp_key": "auto", "min_links": "1"}}]
153+
Patch Applier: Verifying patch updates are reflected on ConfigDB.
154+
Patch Applier: Patch application completed.
155+
156+
real 0m5.177s
157+
```
158+
159+
To disable GCU-based YANG validation: use the following command:
160+
```
161+
admin@vlab-01:~$ sudo config yang_config_validation disable
162+
Wrote disable yang config validation into CONFIG_DB
163+
```
164+
##### Ad-hoc Validation Performance
165+
```
166+
admin@vlab-01:~$ time sudo config portchannel add PortChannel04
167+
168+
real 0m0.453s
169+
```
170+
### Decorator Pseudo Code
171+
```
172+
def validate_decorator(config_db_connector):
173+
def validated_set_entry(updated_value):
174+
update_json_patch = updated_value.to_json_patch()
175+
GenericConfigUpdater.apply_patch(update_json_patch)
176+
config_db_connector.set_entry = validated_set_entry
177+
178+
```
179+
For the buffermgrd C++ use case, there are 3 options for YANG validation implementation:
180+
- Call into Python GCU code for YANG validation
181+
- Call into D-bus GCU APIs
182+
- C++ to invoke GCU command line
183+
184+
D-bus GCU APIs are being supported and utilized by the gNMI project; as such, our preferred implementation is to leverage D-bus GCU APIs for this project as well.
185+
186+
## Additional Features
187+
188+
### Validation Error Output - Exception Handling
189+
Currently, error messages for invalid write requests to ConfigDB are defined on a case-by-case basis. Our goal is to preserve the preexisting error messages, as Exceptions raised and default error messages output by Generic Config Updater YANG validation can be difficult for the client to comprehend.
190+
191+
To achieve this goal, we do the following:
192+
1. Take note of the type of Exception raised for a particular error (e.g. GCU would raise a ValueError for an invalid ConfigDB field format)
193+
2. From the calling function requesting a write to ConfigDB, catch the relevant exception type and output the original error message in exception handling
194+
195+
There may be a scenario where multiple error messages are associated with the same type of exception. In this case, we may combine multiple error messages into one (e.g. `Please check syntax and ensure existence of Portchannelx`). Additionally, where supported, we will add the `yang-error-msg` field in the relevant YANG model(s) which can help distinguish the specific failing YANG constraint.
196+
197+
### Enable/Disable YANG Validation
198+
In some cases, we may need to disable YANG validation prior to modifying ConfigDB. For example, a bug or an accident may cause YANG validation to improperly fail, thus blocking ConfigDB updates in a production environment. This may lead to a severe livesite. In order to mitigate such a livesite, we add a field to ConfigDB: `yangValidateEnabled`.
199+
200+
`yangValidateEnabled` is configurable via the SONiC config CLI, set to True by default. This field never undergoes any YANG validation. When the user sets `yangValidateEnabled` to False, YANG validation for updates to Config DB is skipped, and updates to Config DB will proceed without validation.
201+
202+
CLI to configure YANG config validation:
203+
`config yang_config_validation <enable|disable>`
204+
205+
Schema for new field:
206+
Add `yang_config_validation` field to DEVICE_METADATA table in ConfigDB:
207+
```
208+
“DEVICE_METADATA”: {
209+
“localhost”: {
210+
'bgp_asn': bgp_asn,
211+
'region': region,
212+
'yang_config_validation': 'disable'
213+
.
214+
.
215+
.
216+
}
217+
}
218+
219+
```
220+
221+
Loading
11.1 KB
Loading
12.3 KB
Loading
4 KB
Loading
Loading

0 commit comments

Comments
 (0)