1 /*
2  * vz_utils.h: core driver functions for managing
3  * Parallels Cloud Server hosts
4  *
5  * Copyright (C) 2012 Parallels, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #pragma once
24 
25 #include <Parallels.h>
26 
27 #include "driver.h"
28 #include "conf/domain_conf.h"
29 #include "conf/snapshot_conf.h"
30 #include "conf/virdomainsnapshotobjlist.h"
31 #include "conf/virdomainobjlist.h"
32 #include "conf/domain_event.h"
33 #include "virthread.h"
34 #include "datatypes.h"
35 
36 #define vzParseError() \
37     virReportErrorHelper(VIR_FROM_TEST, VIR_ERR_OPERATION_FAILED, __FILE__, \
38                          __FUNCTION__, __LINE__, _("Can't parse prlctl output"))
39 
40 #define IS_CT(def)  (def->os.type == VIR_DOMAIN_OSTYPE_EXE)
41 
42 #define vzDomNotFoundError(domain) \
43     do { \
44         char uuidstr[VIR_UUID_STRING_BUFLEN]; \
45         virUUIDFormat(domain->uuid, uuidstr); \
46         virReportError(VIR_ERR_NO_DOMAIN, \
47                        _("no domain with matching uuid '%s'"), uuidstr); \
48     } while (0)
49 
50 #define PARALLELS_DOMAIN_ROUTED_NETWORK_NAME   "host-routed"
51 #define VIRTUOZZO_VER_7 ((unsigned long)7000000)
52 
53 struct _vzCapabilities {
54     virStorageFileFormat vmDiskFormat;
55     virStorageFileFormat ctDiskFormat;
56     virDomainDiskBus *diskBuses;
57     virDomainControllerType *controllerTypes;
58     virDomainControllerModelSCSI scsiControllerModel;
59 };
60 typedef struct _vzCapabilities vzCapabilities;
61 
62 /* +2 to keep enclosing { and } */
63 #define VIR_UUID_STRING_BRACED_BUFLEN (VIR_UUID_STRING_BUFLEN + 2)
64 
65 struct _vzDriver {
66     virObjectLockable parent;
67 
68     /* Immutable pointer, self-locking APIs */
69     virDomainObjList *domains;
70     unsigned char session_uuid[VIR_UUID_BUFLEN];
71     PRL_HANDLE server;
72     virCaps *caps;
73     virDomainXMLOption *xmlopt;
74     virObjectEventState *domainEventState;
75     virSysinfoDef *hostsysinfo;
76     unsigned long vzVersion;
77     vzCapabilities vzCaps;
78 };
79 
80 typedef struct _vzDriver vzDriver;
81 
82 struct _vzConn {
83     struct _vzConn* next;
84 
85     struct _vzDriver *driver;
86     /* Immutable pointer, self-locking APIs */
87     virConnectCloseCallbackData *closeCallback;
88 };
89 
90 typedef struct _vzConn vzConn;
91 
92 struct _vzDomainJobObj {
93     virCond cond;
94     bool active;
95     /* when the job started, zeroed on time discontinuities */
96     unsigned long long started;
97     unsigned long long elapsed;
98     bool hasProgress;
99     int progress; /* percents */
100     PRL_HANDLE sdkJob;
101     bool cancelled;
102 };
103 
104 typedef struct _vzDomainJobObj vzDomainJobObj;
105 
106 struct vzDomObj {
107     int id;
108     PRL_HANDLE sdkdom;
109     PRL_HANDLE stats;
110     vzDomainJobObj job;
111 };
112 
113 void* vzDomObjAlloc(void *opaque);
114 void vzDomObjFree(void *p);
115 
116 virDomainObj *vzDomObjFromDomain(virDomainPtr domain);
117 
118 char * vzGetOutput(const char *binary, ...)
119     ATTRIBUTE_NONNULL(1) G_GNUC_NULL_TERMINATED;
120 
121 struct _vzDriver *
122 vzGetDriverConnection(void);
123 
124 void
125 vzDestroyDriverConnection(void);
126 
127 int
128 vzInitVersion(struct _vzDriver *driver);
129 int
130 vzCheckUnsupportedDisk(const virDomainDef *def,
131                        virDomainDiskDef *disk,
132                        struct _vzCapabilities *vzCaps);
133 int
134 vzCheckUnsupportedControllers(const virDomainDef *def,
135                               struct _vzCapabilities *vzCaps);
136 int
137 vzGetDefaultSCSIModel(struct _vzDriver *driver,
138                       PRL_CLUSTERED_DEVICE_SUBTYPE *scsiModel);
139 
140 int vzCheckUnsupportedGraphics(virDomainGraphicsDef *gr);
141 
142 #define PARALLELS_BLOCK_STATS_FOREACH(OP) \
143     OP(rd_req, VIR_DOMAIN_BLOCK_STATS_READ_REQ, "read_requests") \
144     OP(rd_bytes, VIR_DOMAIN_BLOCK_STATS_READ_BYTES, "read_total") \
145     OP(wr_req, VIR_DOMAIN_BLOCK_STATS_WRITE_REQ, "write_requests") \
146     OP(wr_bytes, VIR_DOMAIN_BLOCK_STATS_WRITE_BYTES, "write_total")
147 
148 int
149 vzDomainObjBeginJob(virDomainObj *dom);
150 void
151 vzDomainObjEndJob(virDomainObj *dom);
152 int
153 vzDomainJobUpdateTime(struct _vzDomainJobObj *job);
154