From b1efb848b2a7399ee598bd29aef6c0b2643dcc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Wed, 21 Feb 2024 16:39:33 +0100 Subject: [PATCH] Added a multi IOMMU group listing --- src/pci.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/pci.h | 9 ++++++++- src/sandbox.h | 1 + src/xml.c | 7 ++++--- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/src/pci.c b/src/pci.c index 1868610..b55cfe1 100644 --- a/src/pci.c +++ b/src/pci.c @@ -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(); diff --git a/src/pci.h b/src/pci.h index 019b668..f3cc453 100644 --- a/src/pci.h +++ b/src/pci.h @@ -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); \ No newline at end of file +result_t get_iommu_group_devices(char*** _devices, int group); diff --git a/src/sandbox.h b/src/sandbox.h index 8e8152d..4b804c1 100755 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -4,6 +4,7 @@ #define SANDBOX_VERSION "0.1.4" #define SANDBOX_USER "sandbox" +#define LIBVIRT_DOMAIN "sandbox" typedef struct { const char* name; diff --git a/src/xml.c b/src/xml.c index 0dc0261..a7bb08e 100644 --- a/src/xml.c +++ b/src/xml.c @@ -2,6 +2,7 @@ #include "pci.h" #include "container.h" +#include "sandbox.h" #include #include @@ -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, "\n" - "sandbox\n" + "%s\n" "\n\n" "%llu\n" "%d\n" @@ -95,7 +96,7 @@ result_t generate_container_xml(char** _xml, char* container, int cpu, uint64_t "\n" "\n" "\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);