Skip to content

Convert netmask to CIDR notation for ip command!#6885

Open
Anushree-Mathur wants to merge 1 commit into
autotest:masterfrom
Anushree-Mathur:PCI_CICD_notation
Open

Convert netmask to CIDR notation for ip command!#6885
Anushree-Mathur wants to merge 1 commit into
autotest:masterfrom
Anushree-Mathur:PCI_CICD_notation

Conversation

@Anushree-Mathur

@Anushree-Mathur Anushree-Mathur commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

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

  • Bug Fixes
    • Network interface configuration now uses proper CIDR notation for IP addresses.
    • Netmask inputs are strictly validated; invalid netmasks are reported as errors instead of being silently accepted.

@coderabbitai

coderabbitai Bot commented Jun 10, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Adds 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)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and specifically describes the main change: converting netmask to CIDR notation for the ip command, which aligns perfectly with the core objective of fixing the netmask format issue.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between c96ab65 and d3f3ca0.

📒 Files selected for processing (1)
  • libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py

Comment thread libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py Outdated
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>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py (1)

152-154: ⚡ Quick win

Preserve exception chain when re-raising.

At line 154, use raise ValueError(...) from e to 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

📥 Commits

Reviewing files that changed from the base of the PR and between d3f3ca0 and a0d87b9.

📒 Files selected for processing (1)
  • libvirt/tests/src/passthrough/pci/libvirt_pci_passthrough.py

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