Skip to content

Commit e423ec9

Browse files
committed
Use Systemd to activate virtual functions
Rationale: - These can be used by other units to define an ordering e.g you may want to run some before the virtual functions are created and/or run something after. - The udev method isn't working for me on Rocky 9.4 with Infiniband virtual functions.
1 parent b6fc83e commit e423ec9

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

roles/sriov/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ Example Playbook
2626
- name: configure sr-iov
2727
hosts: compute
2828
vars:
29+
# Use deprecated udev numvfs driver by default
30+
sriov_numvfs_driver: udev
2931
sriov_devices:
3032
- name: p4p1
3133
numvfs: 63
34+
# Per device override of sriov_numvfs_driver
35+
numvfs_driver: systemd
36+
# Do not start docker until virtual functions are loaded
37+
numvfs_required_by:
38+
- docker.service
3239
- name: p3p1
3340
numvfs: 8
3441
# Don't add a udev rule to set numvfs. This can be useful if you use an alternative method

roles/sriov/defaults/main.yml

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ sriov_restart_handler: reboot
2121

2222
sriov_numvfs: 8
2323

24+
# One of: systemd, or udev. Default is 'systemd'.
25+
sriov_numvfs_driver: systemd
26+
# List of services that should wait until numvfs has been set. Only applicable
27+
# to the systemd numvfs driver.
28+
sriov_numvfs_required_by: []
29+
30+
# Path to udev rules when using srivov_numvfs_driver == 'udev'.
2431
sriov_udev_rule_path: /etc/udev/rules.d/70-sriov.rules
2532

2633
sriov_mellanox_vendor_ids:

roles/sriov/tasks/config.yml

+42-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
path: "{{ sriov_udev_rule_path }}"
55
block: |
66
{% for device in sriov_devices %}
7-
{% if device.on_boot_configuration_enabled | default(true) | bool %}
7+
{% if device.on_boot_configuration_enabled | default(true) | bool and device.numvfs_driver | default(sriov_numvfs_driver) == 'udev' %}
88
SUBSYSTEM=="net", ACTION=="add", KERNEL=="{{ device.name }}", RUN+="/usr/bin/sh -c 'echo {{ device.numvfs | default(sriov_numvfs) }} > /sys/class/net/{{ device.name }}/device/sriov_numvfs'"
99
{% endif %}
1010
{% endfor %}
@@ -16,6 +16,47 @@
1616
become: true
1717
notify: "{{ sriov_restart_handler }}"
1818

19+
- name: Persist sriov_numvfs with systemd unit
20+
ansible.builtin.template:
21+
dest: "/etc/systemd/system/virtual-functions-{{ device.name }}.service"
22+
src: "virtual-function.service.j2"
23+
mode: "0644"
24+
owner: root
25+
group: root
26+
loop: "{{ sriov_devices }}"
27+
loop_control:
28+
loop_var: device
29+
become: true
30+
when:
31+
- device.numvfs_driver | default(sriov_numvfs_driver) == 'systemd'
32+
- device.on_boot_configuration_enabled | default(true)
33+
34+
- name: Ensure systemd unit is removed if on boot configuration is disabled
35+
ansible.builtin.file:
36+
path: "/etc/systemd/system/virtual-functions-{{ device.name }}.service"
37+
state: absent
38+
loop: "{{ sriov_devices }}"
39+
loop_control:
40+
loop_var: device
41+
become: true
42+
when:
43+
- not device.on_boot_configuration_enabled | default(true)
44+
45+
- name: Enable sriov_numvfs systemd unit
46+
ansible.builtin.systemd:
47+
name: "virtual-functions-{{ device.name }}.service"
48+
enabled: true
49+
daemon_reload: true
50+
loop: "{{ sriov_devices }}"
51+
loop_control:
52+
loop_var: device
53+
become: true
54+
notify:
55+
- "{{ sriov_restart_handler }}"
56+
when:
57+
- device.numvfs_driver | default(sriov_numvfs_driver) == 'systemd'
58+
- device.on_boot_configuration_enabled | default(true)
59+
1960
- name: Add iommu to kernel command line (Intel)
2061
ansible.builtin.include_role:
2162
name: stackhpc.linux.grubcmdline
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[Unit]
2+
Description=Adds virtual functions for {{ device.name }}
3+
Requires=sys-subsystem-net-devices-{{ device.name }}.device
4+
After=sys-subsystem-net-devices-{{ device.name }}.device
5+
Requires=network-pre.target
6+
Before=network-pre.target
7+
{% if ansible_facts.os_family == "Debian" %}
8+
Requires=systemd-networkd.service
9+
Before=systemd-networkd.service
10+
{% endif %}
11+
12+
[Service]
13+
Type=oneshot
14+
ExecStart=/usr/bin/sh -c 'echo {{ device.numvfs | default(sriov_numvfs) }} > /sys/class/net/{{ device.name }}/device/sriov_numvfs'
15+
RemainAfterExit=yes
16+
17+
[Install]
18+
WantedBy=network-pre.target
19+
{% if ansible_facts.os_family == "Debian" %}
20+
WantedBy=systemd-networkd.service
21+
{% endif %}
22+
{% set required_by = device.numvfs_required_by | default(sriov_numvfs_required_by) %}
23+
{% for item in required_by %}
24+
RequiredBy={{ item }}
25+
{% endfor %}

0 commit comments

Comments
 (0)