1 /*
2  * node_device_conf.h: config handling for node devices
3  *
4  * Copyright (C) 2009-2015 Red Hat, Inc.
5  * Copyright (C) 2008 Virtual Iron Software, Inc.
6  * Copyright (C) 2008 David F. Lively
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library.  If not, see
20  * <http://www.gnu.org/licenses/>.
21  */
22 
23 #pragma once
24 
25 #include "internal.h"
26 #include "virbitmap.h"
27 #include "virpcivpd.h"
28 #include "virscsihost.h"
29 #include "virpci.h"
30 #include "virvhba.h"
31 #include "device_conf.h"
32 #include "storage_adapter_conf.h"
33 #include "virenum.h"
34 
35 #include <libxml/tree.h>
36 
37 #define CREATE_DEVICE 1
38 #define EXISTING_DEVICE 0
39 
40 typedef enum {
41     /* Keep in sync with VIR_ENUM_IMPL in node_device_conf.c */
42     VIR_NODE_DEV_DEVNODE_DEV,
43     VIR_NODE_DEV_DEVNODE_LINK,
44 
45     VIR_NODE_DEV_DEVNODE_LAST
46 } virNodeDevDevnodeType;
47 
48 VIR_ENUM_DECL(virNodeDevDevnode);
49 
50 typedef enum {
51     /* Keep in sync with VIR_ENUM_IMPL in node_device_conf.c */
52     VIR_NODE_DEV_CAP_SYSTEM,            /* System capability */
53     VIR_NODE_DEV_CAP_PCI_DEV,           /* PCI device */
54     VIR_NODE_DEV_CAP_USB_DEV,           /* USB device */
55     VIR_NODE_DEV_CAP_USB_INTERFACE,     /* USB interface */
56     VIR_NODE_DEV_CAP_NET,               /* Network device */
57     VIR_NODE_DEV_CAP_SCSI_HOST,         /* SCSI Host Bus Adapter */
58     VIR_NODE_DEV_CAP_SCSI_TARGET,       /* SCSI Target */
59     VIR_NODE_DEV_CAP_SCSI,              /* SCSI device */
60     VIR_NODE_DEV_CAP_STORAGE,           /* Storage device */
61     VIR_NODE_DEV_CAP_FC_HOST,           /* FC Host Bus Adapter */
62     VIR_NODE_DEV_CAP_VPORTS,            /* HBA which is capable of vports */
63     VIR_NODE_DEV_CAP_SCSI_GENERIC,      /* SCSI generic device */
64     VIR_NODE_DEV_CAP_DRM,               /* DRM device */
65     VIR_NODE_DEV_CAP_MDEV_TYPES,        /* Device capable of mediated devices */
66     VIR_NODE_DEV_CAP_MDEV,              /* Mediated device */
67     VIR_NODE_DEV_CAP_CCW_DEV,           /* s390 CCW device */
68     VIR_NODE_DEV_CAP_CSS_DEV,           /* s390 channel subsystem device */
69     VIR_NODE_DEV_CAP_VDPA,              /* vDPA device */
70     VIR_NODE_DEV_CAP_AP_CARD,           /* s390 AP Card device */
71     VIR_NODE_DEV_CAP_AP_QUEUE,          /* s390 AP Queue */
72     VIR_NODE_DEV_CAP_AP_MATRIX,         /* s390 AP Matrix device */
73     VIR_NODE_DEV_CAP_VPD,               /* Device provides VPD */
74 
75     VIR_NODE_DEV_CAP_LAST
76 } virNodeDevCapType;
77 
78 typedef enum {
79     /* Keep in sync with VIR_ENUM_IMPL in node_device_conf.c */
80     VIR_NODE_DEV_CAP_NET_80203,         /* 802.03 network device */
81     VIR_NODE_DEV_CAP_NET_80211,         /* 802.11 network device */
82     VIR_NODE_DEV_CAP_NET_LAST
83 } virNodeDevNetCapType;
84 
85 VIR_ENUM_DECL(virNodeDevCap);
86 VIR_ENUM_DECL(virNodeDevNetCap);
87 
88 typedef enum {
89     VIR_NODE_DEV_CAP_STORAGE_REMOVABLE                  = (1 << 0),
90     VIR_NODE_DEV_CAP_STORAGE_REMOVABLE_MEDIA_AVAILABLE  = (1 << 1),
91     VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE               = (1 << 2),
92 } virNodeDevStorageCapFlags;
93 
94 typedef enum {
95     VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST                   = (1 << 0),
96     VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS                 = (1 << 1),
97 } virNodeDevSCSIHostCapFlags;
98 
99 typedef enum {
100     VIR_NODE_DEV_CAP_FLAG_FC_RPORT                      = (1 << 0),
101 } virNodeDevSCSITargetCapsFlags;
102 
103 typedef enum {
104     VIR_NODE_DEV_CAP_FLAG_PCI_PHYSICAL_FUNCTION     = (1 << 0),
105     VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION      = (1 << 1),
106     VIR_NODE_DEV_CAP_FLAG_PCIE                      = (1 << 2),
107     VIR_NODE_DEV_CAP_FLAG_PCI_MDEV                  = (1 << 3),
108     VIR_NODE_DEV_CAP_FLAG_PCI_VPD                   = (1 << 4),
109 } virNodeDevPCICapFlags;
110 
111 typedef enum {
112     VIR_NODE_DEV_CAP_FLAG_CSS_MDEV                  = (1 << 0),
113 } virNodeDevCCWCapFlags;
114 
115 typedef enum {
116     VIR_NODE_DEV_CAP_FLAG_AP_MATRIX_MDEV            = (1 << 0),
117 } virNodeDevAPMatrixCapFlags;
118 
119 typedef enum {
120     /* Keep in sync with VIR_ENUM_IMPL in node_device_conf.c */
121     VIR_NODE_DEV_DRM_PRIMARY,
122     VIR_NODE_DEV_DRM_CONTROL,
123     VIR_NODE_DEV_DRM_RENDER,
124 
125     VIR_NODE_DEV_DRM_LAST
126 } virNodeDevDRMType;
127 
128 VIR_ENUM_DECL(virNodeDevDRM);
129 
130 typedef struct _virNodeDevCapSystemHardware virNodeDevCapSystemHardware;
131 struct _virNodeDevCapSystemHardware {
132     char *vendor_name;
133     char *version;
134     char *serial;
135     unsigned char uuid[VIR_UUID_BUFLEN];
136 };
137 
138 typedef struct _virNodeDevCapSystemFirmware virNodeDevCapSystemFirmware;
139 struct _virNodeDevCapSystemFirmware {
140     char *vendor_name;
141     char *version;
142     char *release_date;
143 };
144 
145 typedef struct _virNodeDevCapSystem virNodeDevCapSystem;
146 struct _virNodeDevCapSystem {
147     char *product_name;
148     virNodeDevCapSystemHardware hardware;
149     virNodeDevCapSystemFirmware firmware;
150 };
151 
152 typedef struct _virNodeDevCapMdev virNodeDevCapMdev;
153 struct _virNodeDevCapMdev {
154     char *type;
155     unsigned int iommuGroupNumber;
156     char *uuid;
157     virMediatedDeviceAttr **attributes;
158     size_t nattributes;
159     char *parent_addr;
160     bool autostart;
161 };
162 
163 typedef struct _virNodeDevCapPCIDev virNodeDevCapPCIDev;
164 struct _virNodeDevCapPCIDev {
165     unsigned int domain;
166     unsigned int bus;
167     unsigned int slot;
168     unsigned int function;
169     unsigned int product;
170     unsigned int vendor;
171     int klass;
172     char *product_name;
173     char *vendor_name;
174     virPCIDeviceAddress *physical_function;
175     virPCIDeviceAddress **virtual_functions;
176     size_t num_virtual_functions;
177     unsigned int max_virtual_functions;
178     unsigned int flags;
179     virPCIDeviceAddress **iommuGroupDevices;
180     size_t nIommuGroupDevices;
181     unsigned int iommuGroupNumber;
182     int numa_node;
183     virPCIEDeviceInfo *pci_express;
184     int hdrType; /* enum virPCIHeaderType or -1 */
185     virMediatedDeviceType **mdev_types;
186     size_t nmdev_types;
187     virPCIVPDResource *vpd;
188 };
189 
190 typedef struct _virNodeDevCapUSBDev virNodeDevCapUSBDev;
191 struct _virNodeDevCapUSBDev {
192    unsigned int bus;
193    unsigned int device;
194    unsigned int product;
195    unsigned int vendor;
196    char *product_name;
197    char *vendor_name;
198 };
199 
200 typedef struct _virNodeDevCapUSBIf virNodeDevCapUSBIf;
201 struct _virNodeDevCapUSBIf {
202     unsigned int number;
203     unsigned int klass;
204     unsigned int subclass;
205     unsigned int protocol;
206     char *description;
207 };
208 
209 typedef struct _virNodeDevCapNet virNodeDevCapNet;
210 struct _virNodeDevCapNet {
211     char *address;
212     unsigned int address_len;
213     char *ifname;
214     virNetDevIfLink lnk;
215     virNodeDevNetCapType subtype;  /* LAST -> no subtype */
216     virBitmap *features; /* enum virNetDevFeature */
217 };
218 
219 typedef struct _virNodeDevCapSCSIHost virNodeDevCapSCSIHost;
220 struct _virNodeDevCapSCSIHost {
221     unsigned int host;
222     int unique_id;
223     char *wwnn;
224     char *wwpn;
225     char *fabric_wwn;
226     unsigned int flags;
227     int max_vports;
228     int vports;
229 };
230 
231 typedef struct _virNodeDevCapSCSITarget virNodeDevCapSCSITarget;
232 struct _virNodeDevCapSCSITarget {
233     char *name;
234     unsigned int flags; /* enum virNodeDevSCSITargetCapsFlags */
235     char *rport;
236     char *wwpn;
237 };
238 
239 typedef struct _virNodeDevCapSCSI virNodeDevCapSCSI;
240 struct _virNodeDevCapSCSI {
241     unsigned int host;
242     unsigned int bus;
243     unsigned int target;
244     unsigned int lun;
245     char *type;
246 };
247 
248 typedef struct _virNodeDevCapStorage virNodeDevCapStorage;
249 struct _virNodeDevCapStorage {
250     unsigned long long size;
251     unsigned long long num_blocks;
252     unsigned long long logical_block_size;
253     unsigned long long removable_media_size;
254     char *block;
255     char *bus;
256     char *drive_type;
257     char *model;
258     char *vendor;
259     char *serial;
260     char *media_label;
261     unsigned int flags; /* virNodeDevStorageCapFlags bits */
262 };
263 
264 typedef struct _virNodeDevCapSCSIGeneric virNodeDevCapSCSIGeneric;
265 struct _virNodeDevCapSCSIGeneric {
266     char *path;
267 };
268 
269 typedef struct _virNodeDevCapDRM virNodeDevCapDRM;
270 struct _virNodeDevCapDRM {
271     virNodeDevDRMType type;
272 };
273 
274 typedef struct _virNodeDevCapCCW virNodeDevCapCCW;
275 struct _virNodeDevCapCCW {
276     unsigned int cssid;
277     unsigned int ssid;
278     unsigned int devno;
279     unsigned int flags; /* enum virNodeDevCCWCapFlags */
280     virMediatedDeviceType **mdev_types;
281     size_t nmdev_types;
282 };
283 
284 typedef struct _virNodeDevCapVDPA virNodeDevCapVDPA;
285 struct _virNodeDevCapVDPA {
286     char *chardev;
287 };
288 
289 typedef struct _virNodeDevCapAPCard virNodeDevCapAPCard;
290 struct _virNodeDevCapAPCard {
291     unsigned int ap_adapter;
292 };
293 
294 typedef struct _virNodeDevCapAPQueue virNodeDevCapAPQueue;
295 struct _virNodeDevCapAPQueue {
296     unsigned int ap_adapter;
297     unsigned int ap_domain;
298 };
299 
300 typedef struct _virNodeDevCapAPMatrix virNodeDevCapAPMatrix;
301 struct _virNodeDevCapAPMatrix {
302     char *addr;
303     unsigned int flags; /* enum virNodeDevAPMatrixCapFlags */
304     virMediatedDeviceType **mdev_types;
305     size_t nmdev_types;
306 };
307 
308 typedef struct _virNodeDevCapData virNodeDevCapData;
309 struct _virNodeDevCapData {
310     virNodeDevCapType type;
311     union {
312         virNodeDevCapSystem system;
313         virNodeDevCapPCIDev pci_dev;
314         virNodeDevCapUSBDev usb_dev;
315         virNodeDevCapUSBIf usb_if;
316         virNodeDevCapNet net;
317         virNodeDevCapSCSIHost scsi_host;
318         virNodeDevCapSCSITarget scsi_target;
319         virNodeDevCapSCSI scsi;
320         virNodeDevCapStorage storage;
321         virNodeDevCapSCSIGeneric sg;
322         virNodeDevCapDRM drm;
323         virNodeDevCapMdev mdev;
324         virNodeDevCapCCW ccw_dev;
325         virNodeDevCapVDPA vdpa;
326         virNodeDevCapAPCard ap_card;
327         virNodeDevCapAPQueue ap_queue;
328         virNodeDevCapAPMatrix ap_matrix;
329     };
330 };
331 
332 typedef struct _virNodeDevCapsDef virNodeDevCapsDef;
333 struct _virNodeDevCapsDef {
334     virNodeDevCapData data;
335     virNodeDevCapsDef *next;            /* next capability */
336 };
337 
338 
339 typedef struct _virNodeDeviceDef virNodeDeviceDef;
340 struct _virNodeDeviceDef {
341     char *name;                         /* device name (unique on node) */
342     char *sysfs_path;                   /* udev name/sysfs path */
343     char *parent;                       /* optional parent device name */
344     char *parent_sysfs_path;            /* udev parent name/sysfs path */
345     char *parent_wwnn;                  /* optional parent wwnn */
346     char *parent_wwpn;                  /* optional parent wwpn */
347     char *parent_fabric_wwn;            /* optional parent fabric_wwn */
348     char *driver;                       /* optional driver name */
349     char *devnode;                      /* /dev path */
350     char **devlinks;                    /* /dev links */
351     virNodeDevCapsDef *caps;            /* optional device capabilities */
352 };
353 
354 char *
355 virNodeDeviceDefFormat(const virNodeDeviceDef *def);
356 
357 
358 typedef int (*virNodeDeviceDefPostParseCallback)(virNodeDeviceDef *dev,
359                                                  void *opaque);
360 
361 typedef int (*virNodeDeviceDefValidateCallback)(virNodeDeviceDef *dev,
362                                                 void *opaque);
363 
364 typedef struct _virNodeDeviceDefParserCallbacks {
365     virNodeDeviceDefPostParseCallback postParse;
366     virNodeDeviceDefValidateCallback validate;
367 } virNodeDeviceDefParserCallbacks;
368 
369 virNodeDeviceDef *
370 virNodeDeviceDefParseString(const char *str,
371                             int create,
372                             const char *virt_type,
373                             virNodeDeviceDefParserCallbacks *callbacks,
374                             void *opaque);
375 
376 virNodeDeviceDef *
377 virNodeDeviceDefParseFile(const char *filename,
378                           int create,
379                           const char *virt_type,
380                           virNodeDeviceDefParserCallbacks *callbacks,
381                           void *opaque);
382 
383 virNodeDeviceDef *
384 virNodeDeviceDefParseNode(xmlDocPtr xml,
385                           xmlNodePtr root,
386                           int create,
387                           const char *virt_type);
388 
389 int
390 virNodeDeviceGetWWNs(virNodeDeviceDef *def,
391                      char **wwnn,
392                      char **wwpn);
393 
394 void
395 virNodeDeviceDefFree(virNodeDeviceDef *def);
396 
397 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNodeDeviceDef, virNodeDeviceDefFree);
398 
399 void
400 virNodeDevCapsDefFree(virNodeDevCapsDef *caps);
401 
402 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNodeDevCapsDef, virNodeDevCapsDefFree);
403 
404 #define VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_CAP \
405                 (VIR_CONNECT_LIST_NODE_DEVICES_CAP_SYSTEM        | \
406                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_PCI_DEV       | \
407                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_USB_DEV       | \
408                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_USB_INTERFACE | \
409                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_NET           | \
410                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_HOST     | \
411                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_TARGET   | \
412                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI          | \
413                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_STORAGE       | \
414                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_FC_HOST       | \
415                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_VPORTS        | \
416                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_SCSI_GENERIC  | \
417                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_DRM           | \
418                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_MDEV_TYPES    | \
419                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_MDEV          | \
420                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_CCW_DEV       | \
421                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_CSS_DEV       | \
422                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_VDPA          | \
423                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_AP_CARD       | \
424                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_AP_QUEUE      | \
425                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_AP_MATRIX     | \
426                  VIR_CONNECT_LIST_NODE_DEVICES_CAP_VPD)
427 
428 #define VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_ACTIVE \
429     VIR_CONNECT_LIST_NODE_DEVICES_ACTIVE | \
430     VIR_CONNECT_LIST_NODE_DEVICES_INACTIVE
431 
432 #define VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_ALL \
433     VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_CAP | \
434     VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_ACTIVE
435 
436 int
437 virNodeDeviceGetSCSIHostCaps(virNodeDevCapSCSIHost *scsi_host);
438 
439 int
440 virNodeDeviceGetSCSITargetCaps(const char *sysfsPath,
441                                virNodeDevCapSCSITarget *scsi_target);
442 
443 int
444 virNodeDeviceGetPCIDynamicCaps(const char *sysfsPath,
445                                virNodeDevCapPCIDev *pci_dev);
446 
447 int
448 virNodeDeviceGetCSSDynamicCaps(const char *sysfsPath,
449                                virNodeDevCapCCW *ccw_dev);
450 
451 int
452 virNodeDeviceGetAPMatrixDynamicCaps(const char *sysfsPath,
453                                     virNodeDevCapAPMatrix *ap_matrix);
454 
455 int
456 virNodeDeviceUpdateCaps(virNodeDeviceDef *def);
457 
458 int
459 virNodeDeviceCapsListExport(virNodeDeviceDef *def,
460                             virNodeDevCapType **list);
461