Skip to content
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

Use Systemd to activate virtual functions #34

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions roles/sriov/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,16 @@ Example Playbook
- name: configure sr-iov
hosts: compute
vars:
# Use deprecated udev numvfs driver by default
sriov_numvfs_driver: udev
sriov_devices:
- name: p4p1
numvfs: 63
# Per device override of sriov_numvfs_driver
numvfs_driver: systemd
# Do not start docker until virtual functions are loaded
numvfs_required_by:
- docker.service
- name: p3p1
numvfs: 8
# Don't add a udev rule to set numvfs. This can be useful if you use an alternative method
Expand Down
7 changes: 7 additions & 0 deletions roles/sriov/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ sriov_restart_handler: reboot

sriov_numvfs: 8

# One of: systemd, or udev. Default is 'systemd'.
sriov_numvfs_driver: systemd
# List of services that should wait until numvfs has been set. Only applicable
# to the systemd numvfs driver.
sriov_numvfs_required_by: []

# Path to udev rules when using srivov_numvfs_driver == 'udev'.
sriov_udev_rule_path: /etc/udev/rules.d/70-sriov.rules

sriov_mellanox_vendor_ids:
Expand Down
43 changes: 42 additions & 1 deletion roles/sriov/tasks/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
path: "{{ sriov_udev_rule_path }}"
block: |
{% for device in sriov_devices %}
{% if device.on_boot_configuration_enabled | default(true) | bool %}
{% if device.on_boot_configuration_enabled | default(true) | bool and device.numvfs_driver | default(sriov_numvfs_driver) == 'udev' %}
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'"
{% endif %}
{% endfor %}
Expand All @@ -16,6 +16,47 @@
become: true
notify: "{{ sriov_restart_handler }}"

- name: Persist sriov_numvfs with systemd unit
ansible.builtin.template:
dest: "/etc/systemd/system/virtual-functions-{{ device.name }}.service"
src: "virtual-function.service.j2"
mode: "0644"
owner: root
group: root
loop: "{{ sriov_devices }}"
loop_control:
loop_var: device
become: true
when:
- device.numvfs_driver | default(sriov_numvfs_driver) == 'systemd'
- device.on_boot_configuration_enabled | default(true)

- name: Ensure systemd unit is removed if on boot configuration is disabled
ansible.builtin.file:
path: "/etc/systemd/system/virtual-functions-{{ device.name }}.service"
state: absent
loop: "{{ sriov_devices }}"
loop_control:
loop_var: device
become: true
when:
- not device.on_boot_configuration_enabled | default(true)

- name: Enable sriov_numvfs systemd unit
ansible.builtin.systemd:
name: "virtual-functions-{{ device.name }}.service"
enabled: true
daemon_reload: true
loop: "{{ sriov_devices }}"
loop_control:
loop_var: device
become: true
notify:
- "{{ sriov_restart_handler }}"
when:
- device.numvfs_driver | default(sriov_numvfs_driver) == 'systemd'
- device.on_boot_configuration_enabled | default(true)

- name: Add iommu to kernel command line (Intel)
ansible.builtin.include_role:
name: stackhpc.linux.grubcmdline
Expand Down
25 changes: 25 additions & 0 deletions roles/sriov/templates/virtual-function.service.j2
tomclark0 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[Unit]
Description=Adds virtual functions for {{ device.name }}
Requires=sys-subsystem-net-devices-{{ device.name }}.device
After=sys-subsystem-net-devices-{{ device.name }}.device
Requires=network-pre.target
Before=network-pre.target
{% if ansible_facts.os_family == "Debian" %}
Requires=systemd-networkd.service
Before=systemd-networkd.service
{% endif %}

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c 'echo {{ device.numvfs | default(sriov_numvfs) }} > /sys/class/net/{{ device.name }}/device/sriov_numvfs'
RemainAfterExit=yes

[Install]
WantedBy=network-pre.target
{% if ansible_facts.os_family == "Debian" %}
WantedBy=systemd-networkd.service
{% endif %}
{% set required_by = device.numvfs_required_by | default(sriov_numvfs_required_by) %}
{% for item in required_by %}
RequiredBy={{ item }}
{% endfor %}