1 /*
2  * libxl_conf.h: libxl configuration management
3  *
4  * Copyright (C) 2011-2014 SUSE LINUX Products GmbH, Nuernberg, Germany.
5  * Copyright (C) 2011 Univention GmbH.
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 <libxl.h>
25 
26 #include "internal.h"
27 #include "libvirt_internal.h"
28 #include "virdomainobjlist.h"
29 #include "domain_event.h"
30 #include "capabilities.h"
31 #include "configmake.h"
32 #include "virportallocator.h"
33 #include "virobject.h"
34 #include "virchrdev.h"
35 #include "virhostdev.h"
36 #include "locking/lock_manager.h"
37 #include "virfirmware.h"
38 #include "libxl_capabilities.h"
39 #include "libxl_logger.h"
40 
41 #define LIBXL_DRIVER_EXTERNAL_NAME "Xen"
42 /*
43  * We are stuck with the 'xenlight' name since it is used by the hostdev
44  * manager. Changing it would break management of any host devices previously
45  * managed under the name 'xenlight'.
46  */
47 #define LIBXL_DRIVER_INTERNAL_NAME "xenlight"
48 #define LIBXL_VNC_PORT_MIN  5900
49 #define LIBXL_VNC_PORT_MAX  65535
50 
51 #define LIBXL_MIGRATION_PORT_MIN  49152
52 #define LIBXL_MIGRATION_PORT_MAX  49216
53 
54 #define LIBXL_CONFIG_BASE_DIR SYSCONFDIR "/libvirt"
55 #define LIBXL_CONFIG_DIR SYSCONFDIR "/libvirt/libxl"
56 #define LIBXL_AUTOSTART_DIR LIBXL_CONFIG_DIR "/autostart"
57 #define LIBXL_STATE_DIR RUNSTATEDIR "/libvirt/libxl"
58 #define LIBXL_LOG_DIR LOCALSTATEDIR "/log/libvirt/libxl"
59 #define LIBXL_LIB_DIR LOCALSTATEDIR "/lib/libvirt/libxl"
60 #define LIBXL_SAVE_DIR LIBXL_LIB_DIR "/save"
61 #define LIBXL_DUMP_DIR LIBXL_LIB_DIR "/dump"
62 #define LIBXL_CHANNEL_DIR LIBXL_LIB_DIR "/channel/target"
63 #define LIBXL_BOOTLOADER_PATH "pygrub"
64 
65 
66 typedef struct _libxlDriverPrivate libxlDriverPrivate;
67 
68 typedef struct _libxlDriverConfig libxlDriverConfig;
69 struct _libxlDriverConfig {
70     virObject parent;
71 
72     const libxl_version_info *verInfo;
73     unsigned int version;
74 
75     /* log stream for driver-wide libxl ctx */
76     libxlLogger *logger;
77     /* libxl ctx for driver wide ops; getVersion, getNodeInfo, ... */
78     libxl_ctx *ctx;
79 
80     /* Controls automatic ballooning of domain0. If true, attempt to get
81      * memory for new domains from domain0. */
82     bool autoballoon;
83 
84     char *lockManagerName;
85 
86     int keepAliveInterval;
87     unsigned int keepAliveCount;
88 
89     bool nested_hvm;
90 
91     /* Once created, caps are immutable */
92     virCaps *caps;
93 
94     char *configBaseDir;
95     char *configDir;
96     char *autostartDir;
97     char *logDir;
98     char *stateDir;
99     char *libDir;
100     char *saveDir;
101     char *autoDumpDir;
102     char *channelDir;
103 
104     virFirmware **firmwares;
105     size_t nfirmwares;
106 };
107 
108 G_DEFINE_AUTOPTR_CLEANUP_FUNC(libxlDriverConfig, virObjectUnref);
109 
110 
111 struct _libxlDriverPrivate {
112     virMutex lock;
113 
114     virHostdevManager *hostdevMgr;
115     /* Require lock to get reference on 'config',
116      * then lockless thereafter */
117     libxlDriverConfig *config;
118 
119     /* pid file FD, ensures two copies of the driver can't use the same root */
120     int lockFD;
121 
122     /* Atomic inc/dec only */
123     unsigned int nactive;
124 
125     /* Immutable pointers. Caller must provide locking */
126     virStateInhibitCallback inhibitCallback;
127     void *inhibitOpaque;
128 
129     /* Immutable pointer, self-locking APIs */
130     virDomainObjList *domains;
131 
132     /* Immutable pointer, immutable object */
133     virDomainXMLOption *xmlopt;
134 
135     /* Immutable pointer, self-locking APIs */
136     virObjectEventState *domainEventState;
137 
138     /* Immutable pointer, immutable object */
139     virPortAllocatorRange *reservedGraphicsPorts;
140 
141     /* Immutable pointer, immutable object */
142     virPortAllocatorRange *migrationPorts;
143 
144     /* Immutable pointer, lockless APIs */
145     virSysinfoDef *hostsysinfo;
146 
147     /* Immutable pointer. lockless access */
148     virLockManagerPlugin *lockManager;
149 };
150 
151 #define LIBXL_SAVE_MAGIC "libvirt-xml\n \0 \r"
152 #ifdef LIBXL_HAVE_SRM_V2
153 # define LIBXL_SAVE_VERSION 2
154 #else
155 # define LIBXL_SAVE_VERSION 1
156 #endif
157 
158 typedef struct _libxlSavefileHeader libxlSavefileHeader;
159 struct _libxlSavefileHeader {
160     char magic[sizeof(LIBXL_SAVE_MAGIC)-1];
161     uint32_t version;
162     uint32_t xmlLen;
163     /* 24 bytes used, pad up to 64 bytes */
164     uint32_t unused[10];
165 };
166 
167 
168 typedef struct _libxlDomainXmlNsDef libxlDomainXmlNsDef;
169 struct _libxlDomainXmlNsDef {
170     size_t num_args;
171     char **args;
172 };
173 
174 libxlDriverConfig *
175 libxlDriverConfigNew(void);
176 int
177 libxlDriverConfigInit(libxlDriverConfig *cfg);
178 
179 libxlDriverConfig *
180 libxlDriverConfigGet(libxlDriverPrivate *driver);
181 
182 int
183 libxlDriverNodeGetInfo(libxlDriverPrivate *driver,
184                        virNodeInfoPtr info);
185 
186 int libxlDriverConfigLoadFile(libxlDriverConfig *cfg,
187                               const char *filename);
188 
189 int
190 libxlDriverGetDom0MaxmemConf(libxlDriverConfig *cfg,
191                              unsigned long long *maxmem);
192 
193 int
194 libxlMakeDisk(virDomainDiskDef *l_dev, libxl_device_disk *x_dev);
195 
196 void
197 libxlUpdateDiskDef(virDomainDiskDef *l_dev, libxl_device_disk *x_dev);
198 
199 int
200 libxlMakeNic(virDomainDef *def,
201              virDomainNetDef *l_nic,
202              libxl_device_nic *x_nic,
203              bool attach);
204 int
205 libxlMakeVfb(virPortAllocatorRange *graphicsports,
206              virDomainGraphicsDef *l_vfb, libxl_device_vfb *x_vfb);
207 
208 int
209 libxlMakePCI(virDomainHostdevDef *hostdev, libxl_device_pci *pcidev);
210 
211 #ifdef LIBXL_HAVE_PVUSB
212 int
213 libxlMakeUSBController(virDomainControllerDef *controller,
214                        libxl_device_usbctrl *usbctrl);
215 
216 int
217 libxlMakeUSB(virDomainHostdevDef *hostdev, libxl_device_usbdev *usbdev);
218 #endif
219 
220 virDomainXMLOption *
221 libxlCreateXMLConf(libxlDriverPrivate *driver);
222 
223 #ifdef LIBXL_HAVE_DEVICE_CHANNEL
224 # define LIBXL_ATTR_UNUSED
225 #else
226 # define LIBXL_ATTR_UNUSED G_GNUC_UNUSED
227 #endif
228 int
229 libxlBuildDomainConfig(virPortAllocatorRange *graphicsports,
230                        virDomainDef *def,
231                        libxlDriverConfig *cfg,
232                        libxl_domain_config *d_config);
233 
234 static inline void
libxlDriverLock(libxlDriverPrivate * driver)235 libxlDriverLock(libxlDriverPrivate *driver)
236 {
237     virMutexLock(&driver->lock);
238 }
239 
240 static inline void
libxlDriverUnlock(libxlDriverPrivate * driver)241 libxlDriverUnlock(libxlDriverPrivate *driver)
242 {
243     virMutexUnlock(&driver->lock);
244 }
245