Added a multi IOMMU group listing

This commit is contained in:
Alexei KADIR 2024-02-21 16:39:33 +01:00
parent 9508b6ca28
commit b1efb848b2
4 changed files with 61 additions and 4 deletions

View File

@ -95,6 +95,54 @@ result_t get_iommu_group(int* _group, const char* pci) {
return success();
}
result_t get_iommu_groups(int** _groups, int* _count, const char** pcis) {
// Initialize the output parameters
*_groups = NULL;
*_count = 0;
// Count the number of PCI devices
int count = 0;
while (pcis[count] != NULL)
count++;
// Allocate memory for the groups
int* groups = malloc(count * sizeof(int));
if (groups == NULL)
return failure("Failed to allocate memory for IOMMU groups.");
// Get the IOMMU groups of the PCI devices
for (int i = 0; i < count; i++) {
// Get the IOMMU group of the PCI device
result_t result = get_iommu_group(&groups[i], pcis[i]);
if (result != success()) {
free(groups);
return result;
}
}
// Remove duplicates from the groups
for (int i = 0; i < count; i++) {
for (int j = i + 1; j < count; j++) {
if (groups[i] == groups[j]) {
// Shift the elements to the left
for (int k = j; k < count - 1; k++)
groups[k] = groups[k + 1];
// Decrement the count
count--;
// Decrement the index
j--;
}
}
}
*_groups = groups;
*_count = count;
return success();
}
bool pci_filter(const char* file) {
// Check that the PCI device exists
return check_pci_exists(file) == success();

View File

@ -18,6 +18,13 @@ result_t check_pci_exists(const char* pci);
/// @return The result of the operation.
result_t get_iommu_group(int* _group, const char* pci);
/// @brief Gets the IOMMU groups of the given PCI addresses.
/// @param _groups The integer array pointer to store the resulting IOMMU groups in. The caller is responsible for freeing the array.
/// @param _count The integer pointer to store the number of IOMMU groups in.
/// @param pcis The PCI addresses to get the IOMMU groups of.
/// @return The result of the operation.
result_t get_iommu_groups(int** _groups, int* _count, const char** pcis);
/// @brief Checks that the given file is a PCI device. This function is used as a filter for listing PCI devices.
/// @param file The file to check.
/// @return Whether the file is a PCI device.
@ -27,4 +34,4 @@ bool pci_filter(const char* file);
/// @param _devices The string array pointer to store the resulting devices in. The caller is responsible for freeing the strings and the array.
/// @param group The IOMMU group to get the devices of.
/// @return The result of the operation.
result_t get_iommu_group_devices(char*** _devices, int group);
result_t get_iommu_group_devices(char*** _devices, int group);

View File

@ -4,6 +4,7 @@
#define SANDBOX_VERSION "0.1.4"
#define SANDBOX_USER "sandbox"
#define LIBVIRT_DOMAIN "sandbox"
typedef struct {
const char* name;

View File

@ -2,6 +2,7 @@
#include "pci.h"
#include "container.h"
#include "sandbox.h"
#include <stdio.h>
#include <string.h>
@ -33,7 +34,7 @@ result_t generate_container_xml(char** _xml, char* container, int cpu, uint64_t
return result;
}
// Get the container path
// Get the container path
char* container_path;
result = get_container_path(&container_path, container);
if (result != success()) {
@ -45,7 +46,7 @@ result_t generate_container_xml(char** _xml, char* container, int cpu, uint64_t
// Generate the XML
result = format(_xml, "<domain type='kvm'>\n"
"<name>sandbox</name>\n"
"<name>%s</name>\n"
"\n<!-- Resources -->\n"
"<memory unit='B'>%llu</memory>\n"
"<vcpu placement='static'>%d</vcpu>\n"
@ -95,7 +96,7 @@ result_t generate_container_xml(char** _xml, char* container, int cpu, uint64_t
"<memballoon model='none'/>\n"
"</devices>\n"
"</domain>\n",
memory, cpu, container_path, iso_xml, vnc_xml, pci_xml);
LIBVIRT_DOMAIN, memory, cpu, container_path, iso_xml, vnc_xml, pci_xml);
// Free the PCI, ISO, and VNC XML
free(pci_xml);