Skip to content

Validate MeterConfig against MeterSpec.Type constraints - #151

Open
xovishnukosuri wants to merge 1 commit into
p4lang:mainfrom
xovishnukosuri:validate-meter-spec-type
Open

xovishnukosuri wants to merge 1 commit into
p4lang:mainfrom
xovishnukosuri:validate-meter-spec-type

Conversation

@xovishnukosuri

Copy link
Copy Markdown

Summary

P4Runtime v1.4.0 added MeterSpec.Type (TWO_RATE_THREE_COLOR, SINGLE_RATE_THREE_COLOR, SINGLE_RATE_TWO_COLOR) with specific field constraints for each type. The shell was reading spec.unit but ignoring spec.type, so invalid configs could be silently sent to the switch.

This PR:

  • Reads spec.type from the meter's P4Info in _MeterEntryBase.__init__
  • Adds _MeterConfig.validate(spec_type) which raises UserError if the config violates the type's constraints:
    • TWO_RATE_THREE_COLOR: eburst must be 0
    • SINGLE_RATE_THREE_COLOR: cir == pir and cburst == pburst
    • SINGLE_RATE_TWO_COLOR: cir == pir, cburst == pburst, and eburst == 0
  • Calls validate() from _MeterEntryBase._validate_msg() (for MeterEntry and DirectMeterEntry) and from TableEntry._validate_msg() for tables with a direct meter
  • Adds two meter fixtures (MeterB with SINGLE_RATE_THREE_COLOR, MeterC with SINGLE_RATE_TWO_COLOR) to the test P4Info
  • Adds three unit tests covering all three meter type validations

Closes #149

Test plan

  • Run existing tests: nose2 p4runtime_sh — all 57 pass, including 3 new tests
  • Manually verify that setting eburst on a TWO_RATE_THREE_COLOR meter and calling modify() raises UserError
  • Verify that SINGLE_RATE_THREE_COLOR meters accept eburst but reject cir != pir

🤖 Generated with Claude Code

P4Runtime v1.4.0 added MeterSpec.Type (TWO_RATE_THREE_COLOR,
SINGLE_RATE_THREE_COLOR, SINGLE_RATE_TWO_COLOR) with specific field
constraints for each type. The shell was not reading or enforcing
these constraints.

This change:
- Reads spec.type from the meter's P4Info in _MeterEntryBase
- Adds _MeterConfig.validate() which raises UserError if the config
  violates the constraints imposed by the meter's spec.type
- Calls validate() from _MeterEntryBase._validate_msg() and from
  TableEntry._validate_msg() for tables with a direct meter
- Adds two new meter fixtures (MeterB, MeterC) to the test P4Info
  and three new unit tests covering all three type validations

Closes p4lang#149

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant