1 /*
2  * qemu_conf.h: QEMU configuration management
3  *
4  * Copyright (C) 2006-2007, 2009-2013 Red Hat, Inc.
5  * Copyright (C) 2006 Daniel P. Berrange
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 #pragma once
23 
24 #include <unistd.h>
25 
26 #include "virebtables.h"
27 #include "internal.h"
28 #include "capabilities.h"
29 #include "network_conf.h"
30 #include "domain_conf.h"
31 #include "checkpoint_conf.h"
32 #include "snapshot_conf.h"
33 #include "domain_event.h"
34 #include "virthread.h"
35 #include "security/security_manager.h"
36 #include "virpci.h"
37 #include "virusb.h"
38 #include "virscsi.h"
39 #include "cpu_conf.h"
40 #include "driver.h"
41 #include "virportallocator.h"
42 #include "vircommand.h"
43 #include "virthreadpool.h"
44 #include "locking/lock_manager.h"
45 #include "qemu_capabilities.h"
46 #include "virclosecallbacks.h"
47 #include "virhostdev.h"
48 #include "virfile.h"
49 #include "virfilecache.h"
50 #include "virfirmware.h"
51 
52 #define QEMU_DRIVER_NAME "QEMU"
53 
54 typedef struct _virQEMUDriver virQEMUDriver;
55 
56 typedef struct _virQEMUDriverConfig virQEMUDriverConfig;
57 
58 /* Main driver config. The data in these object
59  * instances is immutable, so can be accessed
60  * without locking. Threads must, however, hold
61  * a valid reference on the object to prevent it
62  * being released while they use it.
63  *
64  * eg
65  *  g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
66  *
67  *  ...do stuff with 'cfg'..
68  */
69 struct _virQEMUDriverConfig {
70     virObject parent;
71 
72     char *uri;
73 
74     uid_t user;
75     gid_t group;
76     bool dynamicOwnership;
77 
78     virBitmap *namespaces;
79     bool rememberOwner;
80 
81     int cgroupControllers;
82     char **cgroupDeviceACL;
83 
84     /* These five directories are ones libvirtd uses (so must be root:root
85      * to avoid security risk from QEMU processes */
86     char *configBaseDir;
87     char *configDir;
88     char *autostartDir;
89     char *logDir;
90     char *swtpmLogDir;
91     char *stateDir;
92     char *swtpmStateDir;
93     char *slirpStateDir;
94     char *dbusStateDir;
95     /* These two directories are ones QEMU processes use (so must match
96      * the QEMU user/group */
97     char *libDir;
98     char *cacheDir;
99     char *saveDir;
100     char *snapshotDir;
101     char *checkpointDir;
102     char *channelTargetDir;
103     char *nvramDir;
104     char *swtpmStorageDir;
105 
106     char *defaultTLSx509certdir;
107     bool defaultTLSx509certdirPresent;
108     bool defaultTLSx509verify;
109     bool defaultTLSx509verifyPresent;
110     char *defaultTLSx509secretUUID;
111 
112     bool vncAutoUnixSocket;
113     bool vncTLS;
114     bool vncTLSx509verify;
115     bool vncTLSx509verifyPresent;
116     bool vncSASL;
117     char *vncTLSx509certdir;
118     char *vncTLSx509secretUUID;
119     char *vncListen;
120     char *vncPassword;
121     char *vncSASLdir;
122 
123     bool spiceTLS;
124     char *spiceTLSx509certdir;
125     bool spiceSASL;
126     char *spiceSASLdir;
127     char *spiceListen;
128     char *spicePassword;
129     bool spiceAutoUnixSocket;
130 
131     bool chardevTLS;
132     char *chardevTLSx509certdir;
133     bool chardevTLSx509verify;
134     bool chardevTLSx509verifyPresent;
135     char *chardevTLSx509secretUUID;
136 
137     char *migrateTLSx509certdir;
138     bool migrateTLSx509verify;
139     bool migrateTLSx509verifyPresent;
140     char *migrateTLSx509secretUUID;
141     bool migrateTLSForce;
142 
143     char *backupTLSx509certdir;
144     bool backupTLSx509verify;
145     bool backupTLSx509verifyPresent;
146     char *backupTLSx509secretUUID;
147 
148     bool vxhsTLS;
149     char *vxhsTLSx509certdir;
150     char *vxhsTLSx509secretUUID;
151 
152     bool nbdTLS;
153     char *nbdTLSx509certdir;
154     char *nbdTLSx509secretUUID;
155 
156     unsigned int remotePortMin;
157     unsigned int remotePortMax;
158 
159     unsigned int webSocketPortMin;
160     unsigned int webSocketPortMax;
161 
162     virHugeTLBFS *hugetlbfs;
163     size_t nhugetlbfs;
164 
165     char *bridgeHelperName;
166     char *prHelperName;
167     char *slirpHelperName;
168     char *dbusDaemonName;
169 
170     bool macFilter;
171 
172     bool relaxedACS;
173     bool vncAllowHostAudio;
174     bool nogfxAllowHostAudio;
175     bool setProcessName;
176 
177     unsigned int maxProcesses;
178     unsigned int maxFiles;
179     unsigned int maxThreadsPerProc;
180     unsigned long long maxCore;
181     bool dumpGuestCore;
182 
183     unsigned int maxQueuedJobs;
184 
185     char **securityDriverNames;
186     bool securityDefaultConfined;
187     bool securityRequireConfined;
188 
189     char *saveImageFormat;
190     char *dumpImageFormat;
191     char *snapshotImageFormat;
192 
193     char *autoDumpPath;
194     bool autoDumpBypassCache;
195     bool autoStartBypassCache;
196 
197     char *lockManagerName;
198 
199     int keepAliveInterval;
200     unsigned int keepAliveCount;
201 
202     int seccompSandbox;
203 
204     char *migrateHost;
205     /* The default for -incoming */
206     char *migrationAddress;
207     unsigned int migrationPortMin;
208     unsigned int migrationPortMax;
209 
210     bool logTimestamp;
211     bool stdioLogD;
212 
213     virFirmware **firmwares;
214     size_t nfirmwares;
215     unsigned int glusterDebugLevel;
216     bool virtiofsdDebug;
217 
218     char *memoryBackingDir;
219 
220     uid_t swtpm_user;
221     gid_t swtpm_group;
222 
223     char **capabilityfilters;
224 
225     char *deprecationBehavior;
226 };
227 
228 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virQEMUDriverConfig, virObjectUnref);
229 
230 
231 /* Main driver state */
232 struct _virQEMUDriver {
233     virMutex lock;
234 
235     /* Require lock to get reference on 'config',
236      * then lockless thereafter */
237     virQEMUDriverConfig *config;
238 
239     /* pid file FD, ensures two copies of the driver can't use the same root */
240     int lockFD;
241 
242     /* Immutable pointer, self-locking APIs */
243     virThreadPool *workerPool;
244 
245     /* Atomic increment only */
246     int lastvmid;
247 
248     /* Atomic inc/dec only */
249     unsigned int nactive;
250 
251     /* Immutable values */
252     bool privileged;
253     char *embeddedRoot;
254 
255     /* Immutable pointers. Caller must provide locking */
256     virStateInhibitCallback inhibitCallback;
257     void *inhibitOpaque;
258 
259     /* Immutable pointer, self-locking APIs */
260     virDomainObjList *domains;
261 
262     /* Immutable pointer */
263     char *qemuImgBinary;
264 
265     /* Immutable pointer, lockless APIs. Pointless abstraction */
266     ebtablesContext *ebtables;
267 
268     /* Require lock to get a reference on the object,
269      * lockless access thereafter
270      */
271     virCaps *caps;
272 
273     /* Lazy initialized on first use, immutable thereafter.
274      * Require lock to get the pointer & do optional initialization
275      */
276     virCPUDef *hostcpu;
277 
278     /* Immutable value */
279     virArch hostarch;
280 
281     /* Immutable pointer, Immutable object */
282     virDomainXMLOption *xmlopt;
283 
284     /* Immutable pointer, self-locking APIs */
285     virFileCache *qemuCapsCache;
286 
287     /* Immutable pointer, self-locking APIs */
288     virObjectEventState *domainEventState;
289 
290     /* Immutable pointer. self-locking APIs */
291     virSecurityManager *securityManager;
292 
293     virHostdevManager *hostdevMgr;
294 
295     /* Immutable pointer. Unsafe APIs. XXX */
296     GHashTable *sharedDevices;
297 
298     /* Immutable pointer, immutable object */
299     virPortAllocatorRange *remotePorts;
300 
301     /* Immutable pointer, immutable object */
302     virPortAllocatorRange *webSocketPorts;
303 
304     /* Immutable pointer, immutable object */
305     virPortAllocatorRange *migrationPorts;
306 
307     /* Immutable pointer, lockless APIs */
308     virSysinfoDef *hostsysinfo;
309 
310     /* Immutable pointer. lockless access */
311     virLockManagerPlugin *lockManager;
312 
313     /* Immutable pointer, self-clocking APIs */
314     virCloseCallbacks *closeCallbacks;
315 
316     /* Immutable pointer, self-locking APIs */
317     virHashAtomic *migrationErrors;
318 };
319 
320 virQEMUDriverConfig *virQEMUDriverConfigNew(bool privileged,
321                                               const char *root);
322 
323 int virQEMUDriverConfigLoadFile(virQEMUDriverConfig *cfg,
324                                 const char *filename,
325                                 bool privileged);
326 
327 int
328 virQEMUDriverConfigValidate(virQEMUDriverConfig *cfg);
329 
330 int
331 virQEMUDriverConfigSetDefaults(virQEMUDriverConfig *cfg);
332 
333 virQEMUDriverConfig *virQEMUDriverGetConfig(virQEMUDriver *driver);
334 
335 virCPUDef *virQEMUDriverGetHostCPU(virQEMUDriver *driver);
336 virCaps *virQEMUDriverCreateCapabilities(virQEMUDriver *driver);
337 virCaps *virQEMUDriverGetCapabilities(virQEMUDriver *driver,
338                                         bool refresh);
339 
340 virDomainCaps *
341 virQEMUDriverGetDomainCapabilities(virQEMUDriver *driver,
342                                    virQEMUCaps *qemuCaps,
343                                    const char *machine,
344                                    virArch arch,
345                                    virDomainVirtType virttype);
346 
347 typedef struct _qemuSharedDeviceEntry qemuSharedDeviceEntry;
348 
349 bool qemuSharedDeviceEntryDomainExists(qemuSharedDeviceEntry *entry,
350                                        const char *name,
351                                        int *idx)
352     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
353 
354 char *qemuGetSharedDeviceKey(const char *disk_path)
355     ATTRIBUTE_NONNULL(1);
356 
357 void qemuSharedDeviceEntryFree(void *payload);
358 
359 int qemuAddSharedDisk(virQEMUDriver *driver,
360                       virDomainDiskDef *disk,
361                       const char *name)
362     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
363 
364 int qemuAddSharedDevice(virQEMUDriver *driver,
365                         virDomainDeviceDef *dev,
366                         const char *name)
367     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
368 
369 int qemuRemoveSharedDevice(virQEMUDriver *driver,
370                            virDomainDeviceDef *dev,
371                            const char *name)
372     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
373 
374 int qemuRemoveSharedDisk(virQEMUDriver *driver,
375                          virDomainDiskDef *disk,
376                          const char *name)
377     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
378 
379 int qemuSetUnprivSGIO(virDomainDeviceDef *dev);
380 
381 int qemuDriverAllocateID(virQEMUDriver *driver);
382 virDomainXMLOption *virQEMUDriverCreateXMLConf(virQEMUDriver *driver,
383                                                  const char *defsecmodel);
384 
385 int qemuTranslateSnapshotDiskSourcePool(virDomainSnapshotDiskDef *def);
386 
387 char * qemuGetBaseHugepagePath(virQEMUDriver *driver,
388                                virHugeTLBFS *hugepage);
389 char * qemuGetDomainHugepagePath(virQEMUDriver *driver,
390                                  const virDomainDef *def,
391                                  virHugeTLBFS *hugepage);
392 
393 int qemuGetDomainHupageMemPath(virQEMUDriver *driver,
394                                const virDomainDef *def,
395                                unsigned long long pagesize,
396                                char **memPath);
397 
398 int qemuGetMemoryBackingDomainPath(virQEMUDriver *driver,
399                                    const virDomainDef *def,
400                                    char **path);
401 int qemuGetMemoryBackingPath(virQEMUDriver *driver,
402                              const virDomainDef *def,
403                              const char *alias,
404                              char **memPath);
405