Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Avoid creating multiple entries of udev rules
Browse files Browse the repository at this point in the history
As on date os-net-config does not support changing numvfs.
However if external scripts are used to reset numvfs to zero and
reconfigure numvfs, the udev rule shall reflect the new numvfs
and the previous udev rule shall be replaced.

Change-Id: I18e65e99b7228d8aea20d510070968f17e021e3c
(cherry picked from commit 8a053a2)
  • Loading branch information
karthiksundaravel committed Jul 15, 2021
1 parent 48c6710 commit 2e29b5a
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 8 deletions.
27 changes: 19 additions & 8 deletions os_net_config/sriov_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ def add_udev_rule_for_legacy_sriov_pf(pf_name, numvfs):
udev_line = 'KERNEL=="%s", '\
'RUN+="/bin/os-net-config-sriov -n %%k:%d"' \
% (pf_name, numvfs)
return add_udev_rule(udev_line, _UDEV_LEGACY_RULE_FILE)
pattern = 'KERNEL=="%s", RUN+="/bin/os-net-config-sriov -n' % pf_name
return add_udev_rule(udev_line, _UDEV_LEGACY_RULE_FILE, pattern)


def add_udev_rule_for_vf_representors(pf_name):
Expand Down Expand Up @@ -361,24 +362,34 @@ def add_udev_rule_to_unmanage_vf_representors_by_nm():
return add_udev_rule(udev_data_line, _UDEV_RULE_FILE)


def add_udev_rule(udev_data, udev_file):
def add_udev_rule(udev_data, udev_file, pattern=None):
trigger_udev_rule = False
udev_data = udev_data.strip()
if not pattern:
pattern = udev_data
if not os.path.exists(udev_file):
with open(udev_file, "w") as f:
data = "# This file is autogenerated by os-net-config\n%s\n"\
% udev_data
f.write(data)
reload_udev_rules()
trigger_udev_rule = True
else:
file_data = get_file_data(udev_file)
udev_lines = file_data.split("\n")
if udev_data not in udev_lines:
udev_lines = file_data.splitlines()
if pattern in file_data:
if udev_data in udev_lines:
return trigger_udev_rule
with open(udev_file, "w") as f:
for line in udev_lines:
if pattern in line:
f.write(udev_data + "\n")
else:
f.write(line + "\n")
else:
with open(udev_file, "a") as f:
f.write(udev_data + "\n")
reload_udev_rules()
trigger_udev_rule = True

reload_udev_rules()
trigger_udev_rule = True
return trigger_udev_rule


Expand Down
139 changes: 139 additions & 0 deletions os_net_config/tests/test_sriov_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,145 @@ def tearDown(self):
if os.path.isfile(sriov_config._UDEV_LEGACY_RULE_FILE):
os.remove(sriov_config._UDEV_LEGACY_RULE_FILE)

def test_add_udev_rules(self):
"""Test Add udev rules
"""
def get_pf_pci_stub(name):
pci_address = {"p2p1": "0000:01:01.0",
"p2p2": "0000:01:02.0",
"p2p3": "0000:01:03.0"}
return pci_address[name]
self.stub_out('os_net_config.sriov_config.get_pf_pci',
get_pf_pci_stub)

def reload_udev_rules_stub():
return
self.stub_out('os_net_config.sriov_config.reload_udev_rules',
reload_udev_rules_stub)

exp_udev_content = '# This file is autogenerated by os-net-config\n'\
'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", '\
'KERNELS=="0000:01:01.0", NAME="p2p1"\n'

sriov_config.add_udev_rule_for_sriov_pf("p2p1")
f = open(sriov_config._UDEV_RULE_FILE, 'r')
self.assertEqual(exp_udev_content, f.read())

def test_append_udev_rules(self):
"""Test adding udev rules
"""
def get_pf_pci_stub(name):
pci_address = {"p2p1": "0000:01:01.0",
"p2p2": "0000:01:02.0",
"p2p3": "0000:01:03.0"}
return pci_address[name]
self.stub_out('os_net_config.sriov_config.get_pf_pci',
get_pf_pci_stub)

def reload_udev_rules_stub():
return
self.stub_out('os_net_config.sriov_config.reload_udev_rules',
reload_udev_rules_stub)

exp_udev_content = '# This file is autogenerated by os-net-config\n'\
'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", '\
'KERNELS=="0000:01:01.0", NAME="p2p1"\n' \
'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", '\
'KERNELS=="0000:01:02.0", NAME="p2p2"\n'
udev_content = '# This file is autogenerated by os-net-config\n'\
'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", '\
'KERNELS=="0000:01:01.0", NAME="p2p1"\n'
udev_file = open(sriov_config._UDEV_RULE_FILE, "w")
udev_file.write(udev_content)
udev_file.close()
sriov_config.add_udev_rule_for_sriov_pf("p2p2")
f = open(sriov_config._UDEV_RULE_FILE, 'r')
self.assertEqual(exp_udev_content, f.read())

def test_add_legacy_udev_rules(self):
"""Test Add udev rules for legacy sriov
"""
def reload_udev_rules_stub():
return
self.stub_out('os_net_config.sriov_config.reload_udev_rules',
reload_udev_rules_stub)

exp_udev_content = '# This file is autogenerated by os-net-config\n'\
'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:8"\n'
sriov_config.add_udev_rule_for_legacy_sriov_pf("p2p1", 8)
f = open(sriov_config._UDEV_LEGACY_RULE_FILE, 'r')
self.assertEqual(exp_udev_content, f.read())

def test_modify_legacy_udev_rules(self):
"""Test modifying udev rules for legacy sriov
"""
def reload_udev_rules_stub():
return
self.stub_out('os_net_config.sriov_config.reload_udev_rules',
reload_udev_rules_stub)

udev_content = '# This file is autogenerated by os-net-config\n'\
'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:10"\n'\
'KERNEL=="p2p2", RUN+="/bin/os-net-config-sriov -n %k:12"\n'
exp_udev_content = '# This file is autogenerated by os-net-config\n'\
'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:8"\n'\
'KERNEL=="p2p2", RUN+="/bin/os-net-config-sriov -n %k:12"\n'
udev_file = open(sriov_config._UDEV_LEGACY_RULE_FILE, "w")
udev_file.write(udev_content)
udev_file.close()
sriov_config.add_udev_rule_for_legacy_sriov_pf("p2p1", 8)
f = open(udev_file.name, 'r')
self.assertEqual(exp_udev_content, f.read())

def test_same_legacy_udev_rules(self):
"""Test without changing udev rules for legacy sriov
"""
def reload_udev_rules_stub():
return
self.stub_out('os_net_config.sriov_config.reload_udev_rules',
reload_udev_rules_stub)

udev_content = '# This file is autogenerated by os-net-config\n'\
'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:8"\n'\
'KERNEL=="p2p2", RUN+="/bin/os-net-config-sriov -n %k:10"\n'
exp_udev_content = '# This file is autogenerated by os-net-config\n'\
'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:8"\n'\
'KERNEL=="p2p2", RUN+="/bin/os-net-config-sriov -n %k:10"\n'
udev_file = open(sriov_config._UDEV_LEGACY_RULE_FILE, "w")
udev_file.write(udev_content)
udev_file.close()
sriov_config.add_udev_rule_for_legacy_sriov_pf("p2p2", 10)
f = open(udev_file.name, 'r')
self.assertEqual(exp_udev_content, f.read())

def test_append_legacy_udev_rules(self):
"""Test appending udev rules for legacy sriov
"""
def reload_udev_rules_stub():
return
self.stub_out('os_net_config.sriov_config.reload_udev_rules',
reload_udev_rules_stub)

udev_content = '# This file is autogenerated by os-net-config\n'\
'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:8"\n'\
'KERNEL=="p2p2", RUN+="/bin/os-net-config-sriov -n %k:10"\n'
exp_udev_content = '# This file is autogenerated by os-net-config\n'\
'KERNEL=="p2p1", RUN+="/bin/os-net-config-sriov -n %k:8"\n'\
'KERNEL=="p2p2", RUN+="/bin/os-net-config-sriov -n %k:10"\n' \
'KERNEL=="p2p3", RUN+="/bin/os-net-config-sriov -n %k:12"\n'
udev_file = open(sriov_config._UDEV_LEGACY_RULE_FILE, "w")
udev_file.write(udev_content)
udev_file.close()
sriov_config.add_udev_rule_for_legacy_sriov_pf("p2p3", 12)
f = open(udev_file.name, 'r')
self.assertEqual(exp_udev_content, f.read())

def test_configure_sriov_pf(self):
"""Test the numvfs setting for SR-IOV PF
Expand Down

0 comments on commit 2e29b5a

Please sign in to comment.