]> git.proxmox.com Git - pve-common.git/blob - src/PVE/SysFSTools.pm
b0a025f28ea051847b41938776fcdc53c5327b16
[pve-common.git] / src / PVE / SysFSTools.pm
1 package PVE::SysFSTools;
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7
8 use PVE::Tools qw(file_read_firstline dir_glob_foreach);
9
10 my $pcisysfs = "/sys/bus/pci";
11 my $pciregex = "([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
12
13 sub lspci {
14 my ($id_filter) = @_;
15
16 my $devices = {};
17
18 dir_glob_foreach("$pcisysfs/devices", $pciregex, sub {
19 my (undef, undef, $bus, $slot, $function) = @_;
20
21 my $id = "$bus:$slot";
22 return if defined($id_filter) && $id_filter ne $id;
23
24 push @{$devices->{$id}}, { id => $id, function => $function };
25 });
26
27 # Entries should be sorted by functions.
28 foreach my $id (keys %$devices) {
29 my $dev = $devices->{$id};
30 $devices->{$id} = [ sort { $a->{function} <=> $b->{function} } @$dev ];
31 }
32
33 return $devices;
34 }
35
36 sub get_mdev_types {
37 my ($id) = @_;
38
39 my $fullid = $id;
40 if ($id !~ m/^[0-9a-fA-f]{4}:/) {
41 $fullid = "0000:$id";
42 }
43
44 my $types = [];
45
46 my $mdev_path = "$pcisysfs/devices/$fullid/mdev_supported_types";
47 if (!-d $mdev_path) {
48 return $types;
49 }
50
51 dir_glob_foreach($mdev_path, '[^\.].*', sub {
52 my ($type) = @_;
53
54 my $type_path = "$mdev_path/$type";
55
56 my $available = int(file_read_firstline("$type_path/available_instances"));
57 my $description = PVE::Tools::file_get_contents("$type_path/description");
58
59 push @$types, {
60 type => $type,
61 description => $description,
62 available => $available,
63 };
64 });
65
66 return $types;
67 }
68
69 sub check_iommu_support{
70 # we have IOMMU support if /sys/class/iommu/ is populated
71 return PVE::Tools::dir_glob_regex('/sys/class/iommu/', "[^\.].*");
72 }
73
74 sub file_write {
75 my ($filename, $buf) = @_;
76
77 my $fh = IO::File->new($filename, "w");
78 return undef if !$fh;
79
80 my $res = print $fh $buf;
81
82 $fh->close();
83
84 return $res;
85 }
86
87 sub pci_device_info {
88 my ($name) = @_;
89
90 my $res;
91
92 return undef if $name !~ m/^${pciregex}$/;
93 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
94
95 my $irq = file_read_firstline("$pcisysfs/devices/$name/irq");
96 return undef if !defined($irq) || $irq !~ m/^\d+$/;
97
98 my $vendor = file_read_firstline("$pcisysfs/devices/$name/vendor");
99 return undef if !defined($vendor) || $vendor !~ s/^0x//;
100
101 my $product = file_read_firstline("$pcisysfs/devices/$name/device");
102 return undef if !defined($product) || $product !~ s/^0x//;
103
104 $res = {
105 name => $name,
106 vendor => $vendor,
107 product => $product,
108 domain => $domain,
109 bus => $bus,
110 slot => $slot,
111 func => $func,
112 irq => $irq,
113 has_fl_reset => -f "$pcisysfs/devices/$name/reset" || 0,
114 };
115
116 return $res;
117 }
118
119 sub pci_dev_reset {
120 my ($dev) = @_;
121
122 my $name = $dev->{name};
123
124 my $fn = "$pcisysfs/devices/$name/reset";
125
126 return file_write($fn, "1");
127 }
128
129 sub pci_dev_bind_to_vfio {
130 my ($dev) = @_;
131
132 my $name = $dev->{name};
133
134 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
135
136 if (!-d $vfio_basedir) {
137 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
138 }
139 die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
140
141 my $testdir = "$vfio_basedir/$name";
142 return 1 if -d $testdir;
143
144 my $data = "$dev->{vendor} $dev->{product}";
145 return undef if !file_write("$vfio_basedir/new_id", $data);
146
147 my $fn = "$pcisysfs/devices/$name/driver/unbind";
148 if (!file_write($fn, $name)) {
149 return undef if -f $fn;
150 }
151
152 $fn = "$vfio_basedir/bind";
153 if (! -d $testdir) {
154 return undef if !file_write($fn, $name);
155 }
156
157 return -d $testdir;
158 }
159
160 sub pci_dev_group_bind_to_vfio {
161 my ($pciid) = @_;
162
163 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
164
165 if (!-d $vfio_basedir) {
166 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
167 }
168 die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
169
170 # get IOMMU group devices
171 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
172 my @devs = grep /^0000:/, readdir($D);
173 closedir($D);
174
175 foreach my $pciid (@devs) {
176 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
177
178 # pci bridges, switches or root ports are not supported
179 # they have a pci_bus subdirectory so skip them
180 next if (-e "$pcisysfs/devices/$pciid/pci_bus");
181
182 my $info = pci_device_info($1);
183 pci_dev_bind_to_vfio($info) || die "Cannot bind $pciid to vfio\n";
184 }
185
186 return 1;
187 }
188
189 sub pci_create_mdev_device {
190 my ($pciid, $uuid, $type) = @_;
191
192 my $basedir = "$pcisysfs/devices/0000:$pciid";
193 my $mdev_dir = "$basedir/mdev_supported_types";
194
195 die "pci device '$pciid' does not support mediated devices \n"
196 if !-d $mdev_dir;
197
198 die "pci device '$pciid' has no type '$type'\n"
199 if !-d "$mdev_dir/$type";
200
201 if (-d "$basedir/$uuid") {
202 # it already exists, checking type
203 my $typelink = readlink("$basedir/$uuid/mdev_type");
204 my ($existingtype) = $typelink =~ m|/([^/]+)$|;
205 die "mdev instance '$uuid' already exits, but type is not '$type'\n"
206 if $type ne $existingtype;
207
208 # instance exists, so use it but warn the user
209 warn "mdev instance '$uuid' already existed, using it.\n";
210 return undef;
211 }
212
213 my $instances = file_read_firstline("$mdev_dir/$type/available_instances");
214 my ($avail) = $instances =~ m/^(\d+)$/;
215 die "pci device '$pciid' has no available instances of '$type'\n"
216 if $avail < 1;
217
218 die "could not create 'type' for pci devices '$pciid'\n"
219 if !file_write("$mdev_dir/$type/create", $uuid);
220
221 return undef;
222 }
223
224 sub pci_cleanup_mdev_device {
225 my ($pciid, $uuid) = @_;
226
227 my $basedir = "$pcisysfs/devices/0000:$pciid/$uuid";
228
229 if (! -e $basedir) {
230 return 1; # no cleanup necessary if it does not exist
231 }
232
233 return file_write("$basedir/remove", "1");
234 }
235
236 # encode the hostpci index and vmid into the uuid
237 sub generate_mdev_uuid {
238 my ($vmid, $index) = @_;
239
240 my $string = sprintf("%08d-0000-0000-0000-%012d", $index, $vmid);
241
242 return $string;
243 }
244
245 # idea is from usbutils package (/usr/bin/usb-devices) script
246 sub __scan_usb_device {
247 my ($res, $devpath, $parent, $level) = @_;
248
249 return if ! -d $devpath;
250 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
251 my $port = $level ? int($1 - 1) : 0;
252
253 my $busnum = int(file_read_firstline("$devpath/busnum"));
254 my $devnum = int(file_read_firstline("$devpath/devnum"));
255
256 my $d = {
257 port => $port,
258 level => $level,
259 busnum => $busnum,
260 devnum => $devnum,
261 speed => file_read_firstline("$devpath/speed"),
262 class => hex(file_read_firstline("$devpath/bDeviceClass")),
263 vendid => file_read_firstline("$devpath/idVendor"),
264 prodid => file_read_firstline("$devpath/idProduct"),
265 };
266
267 if ($level) {
268 my $usbpath = $devpath;
269 $usbpath =~ s|^.*/\d+\-||;
270 $d->{usbpath} = $usbpath;
271 }
272
273 my $product = file_read_firstline("$devpath/product");
274 $d->{product} = $product if $product;
275
276 my $manu = file_read_firstline("$devpath/manufacturer");
277 $d->{manufacturer} = $manu if $manu;
278
279 my $serial => file_read_firstline("$devpath/serial");
280 $d->{serial} = $serial if $serial;
281
282 push @$res, $d;
283
284 foreach my $subdev (<$devpath/$busnum-*>) {
285 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
286 __scan_usb_device($res, $subdev, $devnum, $level + 1);
287 }
288
289 };
290
291 sub scan_usb {
292
293 my $devlist = [];
294
295 foreach my $device (</sys/bus/usb/devices/usb*>) {
296 __scan_usb_device($devlist, $device, 0, 0);
297 }
298
299 return $devlist;
300 }
301
302 1;