-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added pciid match support #375
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,7 +125,7 @@ NETPLAN_PUBLIC gboolean | |
netplan_netdef_has_match(const NetplanNetDefinition* netdef); | ||
|
||
NETPLAN_PUBLIC gboolean | ||
netplan_netdef_match_interface(const NetplanNetDefinition* netdef, const char* name, const char* mac, const char* driver_name); | ||
netplan_netdef_match_interface(const NetplanNetDefinition* netdef, const char* name, const char* mac, const char* driver_name, const char* pciid); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: We cannot just change public API like this, but would rather need to introduce a new |
||
|
||
/********** Old API below this ***********/ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,14 +174,42 @@ def get_interface_macaddress(interface): | |
return link.get('addr', '') | ||
|
||
|
||
def get_interface_pciid(interface): # pragma: nocover (covered in autopkgtest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Can we apply some test-mocking to get this net method covered by unit tests? |
||
devdir = os.path.join('/sys/class/net', interface) | ||
pciid = '' | ||
try: | ||
uevent = os.path.realpath(os.path.join(devdir, 'device', 'uevent')) | ||
if os.path.exists(uevent): | ||
with open(uevent) as f: | ||
contents = f.read().splitlines() | ||
f.close() | ||
else: | ||
return None | ||
|
||
for line in contents: | ||
if line.split('=')[0] == 'PCI_SLOT_NAME': | ||
pciid = line.split('=')[1] # 0000:4b:00.0 | ||
break | ||
except IOError as e: | ||
logging.debug('Cannot replug %s: cannot read link %s: %s', interface, devdir, str(e)) | ||
return None | ||
|
||
# 0000:4b:00.0 | ||
if re.match('[0-9a-f]+:[0-9a-f]+:[0-9a-f]+.+', pciid, re.IGNORECASE) is None: | ||
return None | ||
|
||
return pciid | ||
|
||
|
||
def find_matching_iface(interfaces: list, netdef): | ||
assert isinstance(netdef, np.NetDefinition) | ||
assert netdef.has_match | ||
|
||
matches = list(filter(lambda itf: netdef.match_interface( | ||
itf_name=itf, | ||
itf_driver=get_interface_driver_name(itf), | ||
itf_mac=get_interface_macaddress(itf)), interfaces)) | ||
itf_mac=get_interface_macaddress(itf), | ||
itf_pciid=get_interface_pciid(itf)), interfaces)) | ||
|
||
# Return current name of unique matched interface, if available | ||
if len(matches) != 1: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -249,6 +249,7 @@ struct netplan_net_definition { | |
char* driver; | ||
char* mac; | ||
char* original_name; | ||
char* pciid; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: This has the potential of breaking ABI and we'd need to move the new field to the very end of the |
||
} match; | ||
gboolean has_match; | ||
gboolean wake_on_lan; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -857,6 +857,7 @@ static const mapping_entry_handler match_handlers[] = { | |
{"driver", YAML_NO_NODE, {.variable=handle_match_driver}}, | ||
{"macaddress", YAML_SCALAR_NODE, {.generic=handle_netdef_mac}, netdef_offset(match.mac)}, | ||
{"name", YAML_SCALAR_NODE, {.generic=handle_netdef_id}, netdef_offset(match.original_name)}, | ||
{"pciid", YAML_SCALAR_NODE, {.generic=handle_netdef_str}, netdef_offset(match.pciid)}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: We'd probably want to have a more specific handler here, that validates the input string is an actual PCI ID. |
||
{NULL} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
|
||
from .base import TestBase, ND_DHCP4, UDEV_MAC_RULE, UDEV_NO_MAC_RULE, UDEV_SRIOV_RULE, \ | ||
NM_MANAGED, NM_UNMANAGED, NM_MANAGED_MAC, NM_UNMANAGED_MAC, \ | ||
NM_MANAGED_DRIVER, NM_UNMANAGED_DRIVER | ||
NM_MANAGED_DRIVER, NM_UNMANAGED_DRIVER, NM_UNMANAGED_PCIID, UDEV_PCIID_RULE | ||
|
||
|
||
class TestNetworkd(TestBase): | ||
|
@@ -164,6 +164,27 @@ def test_eth_match_by_mac_rename(self): | |
self.assert_nm(None) | ||
self.assert_nm_udev(NM_UNMANAGED % 'lom1' + NM_UNMANAGED_MAC % '11:22:33:44:55:66') | ||
|
||
def test_eth_match_by_pciid_rename(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: Thanks for providing some test cases! I'd love to see an integration test along the lines of |
||
self.generate('''network: | ||
version: 2 | ||
ethernets: | ||
def1: | ||
match: | ||
pciid: 0000:98:00.1 | ||
set-name: lom1''') | ||
|
||
self.assert_networkd({'def1.link': '[Match]\nPath=pci-0000:98:00.1\n\n[Link]\nName=lom1\nWakeOnLan=off\n', | ||
'def1.network': '''[Match] | ||
Path=pci-0000:98:00.1 | ||
Name=lom1 | ||
|
||
[Network] | ||
LinkLocalAddressing=ipv6 | ||
'''}) | ||
self.assert_networkd_udev({'def1.rules': (UDEV_PCIID_RULE % ('?*', '0000:98:00.1', 'lom1'))}) | ||
self.assert_nm(None) | ||
self.assert_nm_udev(NM_UNMANAGED % 'lom1' + NM_UNMANAGED_PCIID % '0000:98:00.1') | ||
|
||
# https://bugs.launchpad.net/netplan/+bug/1848474 | ||
def test_eth_match_by_mac_infiniband(self): | ||
self.generate('''network: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick (non-blocking): Version needs to be bumped.
question: Also, is there any possibility of doing the same for the NetworkManager backend? The
match
stanza is a very central piece of Netplan's configuration and we should try not to diverge between the backends here.