trafgen: update options for zsh completion
[netsniff-ng.git] / irq.c
blob2f7eafa07f6f60be5788dd3e16053c1875411d2d
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdbool.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
10 #include "irq.h"
11 #include "str.h"
12 #include "die.h"
14 int device_irq_number(const char *ifname)
16 int irq = 0;
17 char buff[128], sysname[128];
18 FILE *fp;
20 if (!strncmp("lo", ifname, strlen("lo")))
21 return 0;
23 slprintf(sysname, sizeof(sysname),
24 "/sys/class/net/%s/device/irq",
25 ifname);
27 fp = fopen(sysname, "r");
28 if (!fp)
29 return -ENOENT;
31 memset(buff, 0, sizeof(buff));
33 if (fgets(buff, sizeof(buff), fp) != NULL) {
34 buff[sizeof(buff) - 1] = 0;
35 irq = atoi(buff);
38 fclose(fp);
39 return irq;
42 static char nic_irq_affinity_list[128];
43 static bool nic_irq_stored = false;
44 static int nic_irq = -1;
46 static void device_save_irq_affinity_list(void)
48 int ret, fd;
49 char file[128];
51 bug_on(nic_irq_stored);
53 slprintf(file, sizeof(file),
54 "/proc/irq/%d/smp_affinity_list", nic_irq);
56 fd = open(file, O_RDONLY);
57 if (fd < 0)
58 return;
60 memset(nic_irq_affinity_list, 0, sizeof(nic_irq_affinity_list));
62 ret = read(fd, nic_irq_affinity_list,
63 sizeof(nic_irq_affinity_list));
64 if (ret < 0)
65 panic("Cannot store NIC IRQ affinity!\n");
67 close(fd);
69 nic_irq_stored = true;
72 void device_restore_irq_affinity_list(void)
74 int ret, fd;
75 char file[128];
77 if (!nic_irq_stored)
78 return;
80 bug_on(nic_irq == -1);
82 slprintf(file, sizeof(file),
83 "/proc/irq/%d/smp_affinity_list", nic_irq);
85 fd = open(file, O_WRONLY);
86 if (fd < 0)
87 return;
89 ret = write(fd, nic_irq_affinity_list,
90 sizeof(nic_irq_affinity_list));
91 if (ret < 0)
92 panic("Cannot restore NIC IRQ affinity!\n");
94 close(fd);
97 int device_set_irq_affinity_list(int irq, unsigned long from, unsigned long to)
99 int ret, fd;
100 char file[128], list[64];
102 if (unlikely(irq == 0))
103 return 0;
104 if (!nic_irq_stored) {
105 nic_irq = irq;
106 device_save_irq_affinity_list();
109 slprintf(file, sizeof(file), "/proc/irq/%d/smp_affinity_list", irq);
110 slprintf(list, sizeof(list), "%lu-%lu\n", from, to);
112 fd = open(file, O_WRONLY);
113 if (fd < 0)
114 return -ENOENT;
116 ret = write(fd, list, strlen(list));
118 close(fd);
119 return ret;
122 int device_set_irq_affinity(int irq, unsigned long cpu)
124 return device_set_irq_affinity_list(irq, cpu, cpu);