1 /*
2  * libxl_api_wrapper.h: handle various libxl API variants
3  *
4  * Copyright (C) 2021 SUSE LLC
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library.  If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include <limits.h>
24 #include <libxl.h>
25 
26 static inline int
libxlDomainCreateRestoreWrapper(libxl_ctx * ctx,libxl_domain_config * d_config,uint32_t * domid,int restore_fd,const libxl_domain_restore_params * params,const libxl_asyncprogress_how * aop_console_how)27 libxlDomainCreateRestoreWrapper(libxl_ctx *ctx,
28                                 libxl_domain_config *d_config,
29                                 uint32_t *domid,
30                                 int restore_fd,
31                                 const libxl_domain_restore_params *params,
32                                 const libxl_asyncprogress_how *aop_console_how)
33 {
34     int ret;
35 
36 #if LIBXL_API_VERSION < 0x040700
37     ret = libxl_domain_create_restore(ctx, d_config, domid, restore_fd, params,
38                                       NULL, aop_console_how);
39 #else
40     ret = libxl_domain_create_restore(ctx, d_config, domid, restore_fd, -1,
41                                       params, NULL, aop_console_how);
42 #endif
43 
44     return ret;
45 }
46 
47 static inline int
libxlRetrieveDomainConfigurationWrapper(libxl_ctx * ctx,uint32_t domid,libxl_domain_config * d_config)48 libxlRetrieveDomainConfigurationWrapper(libxl_ctx *ctx,
49                                         uint32_t domid,
50                                         libxl_domain_config *d_config)
51 {
52     int ret;
53 
54 #if LIBXL_API_VERSION < 0x041300
55     ret = libxl_retrieve_domain_configuration(ctx, domid, d_config);
56 #else
57     ret = libxl_retrieve_domain_configuration(ctx, domid, d_config, NULL);
58 #endif
59 
60     return ret;
61 }
62 
63 static inline int
libxlDomainShutdownWrapper(libxl_ctx * ctx,uint32_t domid)64 libxlDomainShutdownWrapper(libxl_ctx *ctx, uint32_t domid)
65 {
66     int ret;
67 
68 #if LIBXL_API_VERSION < 0x041300
69     ret = libxl_domain_shutdown(ctx, domid);
70 #else
71     ret = libxl_domain_shutdown(ctx, domid, NULL);
72 #endif
73 
74     return ret;
75 }
76 
77 static inline int
libxlDomainRebootWrapper(libxl_ctx * ctx,uint32_t domid)78 libxlDomainRebootWrapper(libxl_ctx *ctx, uint32_t domid)
79 {
80     int ret;
81 
82 #if LIBXL_API_VERSION < 0x041300
83     ret = libxl_domain_reboot(ctx, domid);
84 #else
85     ret = libxl_domain_reboot(ctx, domid, NULL);
86 #endif
87 
88     return ret;
89 }
90 
91 static inline int
libxlDomainPauseWrapper(libxl_ctx * ctx,uint32_t domid)92 libxlDomainPauseWrapper(libxl_ctx *ctx, uint32_t domid)
93 {
94     int ret;
95 
96 #if LIBXL_API_VERSION < 0x041300
97     ret = libxl_domain_pause(ctx, domid);
98 #else
99     ret = libxl_domain_pause(ctx, domid, NULL);
100 #endif
101 
102     return ret;
103 }
104 
105 static inline int
libxlDomainUnpauseWrapper(libxl_ctx * ctx,uint32_t domid)106 libxlDomainUnpauseWrapper(libxl_ctx *ctx, uint32_t domid)
107 {
108     int ret;
109 
110 #if LIBXL_API_VERSION < 0x041300
111     ret = libxl_domain_unpause(ctx, domid);
112 #else
113     ret = libxl_domain_unpause(ctx, domid, NULL);
114 #endif
115 
116     return ret;
117 }
118 
119 #define INVALID_DOMID ~0
120 static inline int
libxlDomainNeedMemoryWrapper(libxl_ctx * ctx,libxl_domain_config * d_config,uint64_t * need_memkb)121 libxlDomainNeedMemoryWrapper(libxl_ctx *ctx,
122                              libxl_domain_config *d_config,
123                              uint64_t *need_memkb)
124 {
125     int ret;
126 
127 #if LIBXL_API_VERSION < 0x040800
128     {
129         uint32_t val32 = 0;
130 
131         ret = libxl_domain_need_memory(ctx, &d_config->b_info, &val32);
132         *need_memkb = val32;
133     }
134 #elif LIBXL_API_VERSION < 0x041300
135     ret = libxl_domain_need_memory(ctx, &d_config->b_info, need_memkb);
136 #else
137     ret = libxl_domain_need_memory(ctx, d_config, INVALID_DOMID, need_memkb);
138 #endif
139 
140     return ret;
141 }
142 
143 static inline int
libxlGetFreeMemoryWrapper(libxl_ctx * ctx,uint64_t * memkb)144 libxlGetFreeMemoryWrapper(libxl_ctx *ctx, uint64_t *memkb)
145 {
146     int ret;
147 
148 #if LIBXL_API_VERSION < 0x040800
149     {
150         uint32_t val32 = 0;
151 
152         ret = libxl_get_free_memory(ctx, &val32);
153         *memkb = val32;
154     }
155 #else
156     ret = libxl_get_free_memory(ctx, memkb);
157 #endif
158 
159     return ret;
160 }
161 
162 static inline int
libxlSetVcpuonlineWrapper(libxl_ctx * ctx,uint32_t domid,libxl_bitmap * cpumap)163 libxlSetVcpuonlineWrapper(libxl_ctx *ctx, uint32_t domid, libxl_bitmap *cpumap)
164 {
165     int ret;
166 
167 #if LIBXL_API_VERSION < 0x041300
168     ret = libxl_set_vcpuonline(ctx, domid, cpumap);
169 #else
170     ret = libxl_set_vcpuonline(ctx, domid, cpumap, NULL);
171 #endif
172 
173     return ret;
174 }
175 
176 static inline int
libxlSendTriggerWrapper(libxl_ctx * ctx,uint32_t domid,libxl_trigger trigger,uint32_t vcpuid)177 libxlSendTriggerWrapper(libxl_ctx *ctx,
178                         uint32_t domid,
179                         libxl_trigger trigger,
180                         uint32_t vcpuid)
181 {
182     int ret;
183 
184 #if LIBXL_API_VERSION < 0x041300
185     ret = libxl_send_trigger(ctx, domid, trigger, vcpuid);
186 #else
187     ret = libxl_send_trigger(ctx, domid, trigger, vcpuid, NULL);
188 #endif
189 
190     return ret;
191 }
192 
193 static inline int
libxlSetMemoryTargetWrapper(libxl_ctx * ctx,uint32_t domid,uint64_t target_memkb,int relative,int enforce)194 libxlSetMemoryTargetWrapper(libxl_ctx *ctx,
195                         uint32_t domid,
196                         uint64_t target_memkb,
197                         int relative,
198                         int enforce)
199 {
200     int ret = -1;
201 
202     /* Technically this guard could be LIBXL_HAVE_MEMKB_64BITS */
203 #if LIBXL_API_VERSION < 0x040800
204     if (target_memkb < UINT_MAX) {
205         uint32_t val32 = target_memkb;
206 
207         ret = libxl_set_memory_target(ctx, domid, val32, relative, enforce);
208     }
209 #else
210     if (target_memkb < LLONG_MAX) {
211         int64_t val64 = target_memkb;
212         ret = libxl_set_memory_target(ctx, domid, val64, relative, enforce);
213     }
214 #endif
215 
216     return ret;
217 }
218