Convert netmask to CIDR notation for ip command!#6885
Conversation
WalkthroughAdds an inner netmask_to_cidr(netmask) helper that validates dotted-decimal netmasks (exactly four 0–255 octets with contiguous one bits) and returns the CIDR prefix length or raises ValueError on invalid input. Replaces the previous use of the raw netmask in the NIC setup with the computed CIDR in the ip addr add command (ip addr add / dev ). Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
e535023 to
d3f3ca0
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py`:
- Around line 115-123: The netmask_to_cidr function silently defaults invalid
netmasks to "24", which hides configuration errors; instead validate the input
(ensure netmask has exactly 4 dot-separated octets, each an integer 0–255, and
that the combined binary representation has contiguous 1-bits followed by only
0-bits), and on any validation failure raise a ValueError (or re-raise the
original exception) with a clear message rather than returning "24"; update
netmask_to_cidr to perform these checks before counting ones and remove the
swallowing except that returns "24" so callers fail fast and receive an explicit
error.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: ee13e6fe-e189-40c0-8a21-470830efbddd
📒 Files selected for processing (1)
libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py
The 'ip addr add' command requires CIDR notation (e.g., /24) but the test was using dotted decimal netmask format (e.g., 255.255.255.0), causing the command to hang and timeout. Add netmask_to_cidr() helper function that converts dotted decimal netmask to CIDR notation by counting the number of 1 bits in the binary representation. Example: 255.255.255.0 → 11111111.11111111.11111111.00000000 → /24 Signed-off-by: Anushree-Mathur <anushree.mathur@linux.ibm.com>
d3f3ca0 to
a0d87b9
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py (1)
152-154: ⚡ Quick winPreserve exception chain when re-raising.
At line 154, use
raise ValueError(...) from eto maintain the full exception context per PEP 3134, making debugging easier if an unexpected error occurs during netmask processing.🔗 Proposed fix to preserve exception chain
except Exception as e: logging.error("Failed to convert netmask %s to CIDR: %s", netmask, str(e)) - raise ValueError("Invalid netmask format: %s" % netmask) + raise ValueError("Invalid netmask format: %s" % netmask) from e🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py` around lines 152 - 154, The except block that logs and re-raises an error when converting netmask to CIDR currently discards the original exception; update the re-raise in that block to preserve the exception chain by using "raise ValueError('Invalid netmask format: %s' % netmask) from e" so the original exception object "e" is attached and full context is retained (the logging call using logging.error and variables netmask and e should remain unchanged).Source: Linters/SAST tools
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py`:
- Around line 152-154: The except block that logs and re-raises an error when
converting netmask to CIDR currently discards the original exception; update the
re-raise in that block to preserve the exception chain by using "raise
ValueError('Invalid netmask format: %s' % netmask) from e" so the original
exception object "e" is attached and full context is retained (the logging call
using logging.error and variables netmask and e should remain unchanged).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4d7c63f0-ad71-4920-9b3b-dde4773fe260
📒 Files selected for processing (1)
libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py
The 'ip addr add' command requires CIDR notation (e.g., /24) but the test was using dotted decimal netmask format (e.g., 255.255.255.0), causing the command to hang and timeout.
Add netmask_to_cidr() helper function that converts dotted decimal netmask to CIDR notation by counting the number of 1 bits in the binary representation.
Example: 255.255.255.0 → 11111111.11111111.11111111.00000000 → /24
Signed-off-by: Anushree-Mathur anushree.mathur@linux.ibm.com
Summary by CodeRabbit