xref: /linux/tools/testing/selftests/kvm/lib/proc_util.c (revision 362cc00162499413d720fd1c9dfcf1fbba762e9b)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "kvm_util.h"
3 #include "test_util.h"
4 #include "proc_util.h"
5 
6 static FILE *open_proc_interrupts(void)
7 {
8 	FILE *fp;
9 
10 	fp = fopen("/proc/interrupts", "r");
11 	TEST_ASSERT(fp, "fopen(/proc/interrupts) failed");
12 
13 	return fp;
14 }
15 
16 unsigned int vfio_msix_to_host_irq(const char *device_bdf, int msix)
17 {
18 	char search_string[64];
19 	char line[4096];
20 	int irq = -1;
21 	FILE *fp;
22 
23 	fp = open_proc_interrupts();
24 
25 	snprintf(search_string, sizeof(search_string), "vfio-msix[%d]", msix);
26 
27 	while (fgets(line, sizeof(line), fp)) {
28 		if (strstr(line, device_bdf) && strstr(line, search_string)) {
29 			TEST_ASSERT_EQ(1, sscanf(line, "%d:", &irq));
30 			break;
31 		}
32 	}
33 
34 	fclose(fp);
35 
36 	TEST_ASSERT(irq != -1, "Failed to locate IRQ for %s %s", device_bdf,
37 		    search_string);
38 	return (unsigned int)irq;
39 }
40 
41