]> git.proxmox.com Git - qemu.git/blob - hw/virtio-balloon.c
virtio-blk: cleanup: init and exit functions.
[qemu.git] / hw / virtio-balloon.c
1 /*
2 * Virtio Balloon Device
3 *
4 * Copyright IBM, Corp. 2008
5 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
7 *
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
14 */
15
16 #include "qemu/iov.h"
17 #include "qemu/timer.h"
18 #include "qemu-common.h"
19 #include "hw/virtio.h"
20 #include "hw/pc.h"
21 #include "cpu.h"
22 #include "sysemu/balloon.h"
23 #include "hw/virtio-balloon.h"
24 #include "sysemu/kvm.h"
25 #include "exec/address-spaces.h"
26 #include "qapi/visitor.h"
27
28 #if defined(__linux__)
29 #include <sys/mman.h>
30 #endif
31
32 static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
33 {
34 return (VirtIOBalloon *)vdev;
35 }
36
37 static void balloon_page(void *addr, int deflate)
38 {
39 #if defined(__linux__)
40 if (!kvm_enabled() || kvm_has_sync_mmu())
41 qemu_madvise(addr, TARGET_PAGE_SIZE,
42 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
43 #endif
44 }
45
46 static const char *balloon_stat_names[] = {
47 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
48 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
49 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
50 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
51 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
52 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
53 [VIRTIO_BALLOON_S_NR] = NULL
54 };
55
56 /*
57 * reset_stats - Mark all items in the stats array as unset
58 *
59 * This function needs to be called at device intialization and before
60 * before updating to a set of newly-generated stats. This will ensure that no
61 * stale values stick around in case the guest reports a subset of the supported
62 * statistics.
63 */
64 static inline void reset_stats(VirtIOBalloon *dev)
65 {
66 int i;
67 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
68 }
69
70 static bool balloon_stats_supported(const VirtIOBalloon *s)
71 {
72 return s->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ);
73 }
74
75 static bool balloon_stats_enabled(const VirtIOBalloon *s)
76 {
77 return s->stats_poll_interval > 0;
78 }
79
80 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
81 {
82 if (balloon_stats_enabled(s)) {
83 qemu_del_timer(s->stats_timer);
84 qemu_free_timer(s->stats_timer);
85 s->stats_timer = NULL;
86 s->stats_poll_interval = 0;
87 }
88 }
89
90 static void balloon_stats_change_timer(VirtIOBalloon *s, int secs)
91 {
92 qemu_mod_timer(s->stats_timer, qemu_get_clock_ms(vm_clock) + secs * 1000);
93 }
94
95 static void balloon_stats_poll_cb(void *opaque)
96 {
97 VirtIOBalloon *s = opaque;
98
99 if (!balloon_stats_supported(s)) {
100 /* re-schedule */
101 balloon_stats_change_timer(s, s->stats_poll_interval);
102 return;
103 }
104
105 virtqueue_push(s->svq, &s->stats_vq_elem, s->stats_vq_offset);
106 virtio_notify(&s->vdev, s->svq);
107 }
108
109 static void balloon_stats_get_all(Object *obj, struct Visitor *v,
110 void *opaque, const char *name, Error **errp)
111 {
112 VirtIOBalloon *s = opaque;
113 int i;
114
115 if (!s->stats_last_update) {
116 error_setg(errp, "guest hasn't updated any stats yet");
117 return;
118 }
119
120 visit_start_struct(v, NULL, "guest-stats", name, 0, errp);
121 visit_type_int(v, &s->stats_last_update, "last-update", errp);
122
123 visit_start_struct(v, NULL, NULL, "stats", 0, errp);
124 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
125 visit_type_int64(v, (int64_t *) &s->stats[i], balloon_stat_names[i],
126 errp);
127 }
128 visit_end_struct(v, errp);
129
130 visit_end_struct(v, errp);
131 }
132
133 static void balloon_stats_get_poll_interval(Object *obj, struct Visitor *v,
134 void *opaque, const char *name,
135 Error **errp)
136 {
137 VirtIOBalloon *s = opaque;
138 visit_type_int(v, &s->stats_poll_interval, name, errp);
139 }
140
141 static void balloon_stats_set_poll_interval(Object *obj, struct Visitor *v,
142 void *opaque, const char *name,
143 Error **errp)
144 {
145 VirtIOBalloon *s = opaque;
146 int64_t value;
147
148 visit_type_int(v, &value, name, errp);
149 if (error_is_set(errp)) {
150 return;
151 }
152
153 if (value < 0) {
154 error_setg(errp, "timer value must be greater than zero");
155 return;
156 }
157
158 if (value == s->stats_poll_interval) {
159 return;
160 }
161
162 if (value == 0) {
163 /* timer=0 disables the timer */
164 balloon_stats_destroy_timer(s);
165 return;
166 }
167
168 if (balloon_stats_enabled(s)) {
169 /* timer interval change */
170 s->stats_poll_interval = value;
171 balloon_stats_change_timer(s, value);
172 return;
173 }
174
175 /* create a new timer */
176 g_assert(s->stats_timer == NULL);
177 s->stats_timer = qemu_new_timer_ms(vm_clock, balloon_stats_poll_cb, s);
178 s->stats_poll_interval = value;
179 balloon_stats_change_timer(s, 0);
180 }
181
182 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
183 {
184 VirtIOBalloon *s = to_virtio_balloon(vdev);
185 VirtQueueElement elem;
186 MemoryRegionSection section;
187
188 while (virtqueue_pop(vq, &elem)) {
189 size_t offset = 0;
190 uint32_t pfn;
191
192 while (iov_to_buf(elem.out_sg, elem.out_num, offset, &pfn, 4) == 4) {
193 ram_addr_t pa;
194 ram_addr_t addr;
195
196 pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT;
197 offset += 4;
198
199 /* FIXME: remove get_system_memory(), but how? */
200 section = memory_region_find(get_system_memory(), pa, 1);
201 if (!section.size || !memory_region_is_ram(section.mr))
202 continue;
203
204 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
205 should be OK because we only want a single page. */
206 addr = section.offset_within_region;
207 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
208 !!(vq == s->dvq));
209 }
210
211 virtqueue_push(vq, &elem, offset);
212 virtio_notify(vdev, vq);
213 }
214 }
215
216 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
217 {
218 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
219 VirtQueueElement *elem = &s->stats_vq_elem;
220 VirtIOBalloonStat stat;
221 size_t offset = 0;
222 qemu_timeval tv;
223
224 if (!virtqueue_pop(vq, elem)) {
225 goto out;
226 }
227
228 /* Initialize the stats to get rid of any stale values. This is only
229 * needed to handle the case where a guest supports fewer stats than it
230 * used to (ie. it has booted into an old kernel).
231 */
232 reset_stats(s);
233
234 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
235 == sizeof(stat)) {
236 uint16_t tag = tswap16(stat.tag);
237 uint64_t val = tswap64(stat.val);
238
239 offset += sizeof(stat);
240 if (tag < VIRTIO_BALLOON_S_NR)
241 s->stats[tag] = val;
242 }
243 s->stats_vq_offset = offset;
244
245 if (qemu_gettimeofday(&tv) < 0) {
246 fprintf(stderr, "warning: %s: failed to get time of day\n", __func__);
247 goto out;
248 }
249
250 s->stats_last_update = tv.tv_sec;
251
252 out:
253 if (balloon_stats_enabled(s)) {
254 balloon_stats_change_timer(s, s->stats_poll_interval);
255 }
256 }
257
258 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
259 {
260 VirtIOBalloon *dev = to_virtio_balloon(vdev);
261 struct virtio_balloon_config config;
262
263 config.num_pages = cpu_to_le32(dev->num_pages);
264 config.actual = cpu_to_le32(dev->actual);
265
266 memcpy(config_data, &config, 8);
267 }
268
269 static void virtio_balloon_set_config(VirtIODevice *vdev,
270 const uint8_t *config_data)
271 {
272 VirtIOBalloon *dev = to_virtio_balloon(vdev);
273 struct virtio_balloon_config config;
274 uint32_t oldactual = dev->actual;
275 memcpy(&config, config_data, 8);
276 dev->actual = le32_to_cpu(config.actual);
277 if (dev->actual != oldactual) {
278 qemu_balloon_changed(ram_size -
279 (dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
280 }
281 }
282
283 static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
284 {
285 f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
286 return f;
287 }
288
289 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
290 {
291 VirtIOBalloon *dev = opaque;
292 info->actual = ram_size - ((uint64_t) dev->actual <<
293 VIRTIO_BALLOON_PFN_SHIFT);
294 }
295
296 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
297 {
298 VirtIOBalloon *dev = opaque;
299
300 if (target > ram_size) {
301 target = ram_size;
302 }
303 if (target) {
304 dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
305 virtio_notify_config(&dev->vdev);
306 }
307 }
308
309 static void virtio_balloon_save(QEMUFile *f, void *opaque)
310 {
311 VirtIOBalloon *s = opaque;
312
313 virtio_save(&s->vdev, f);
314
315 qemu_put_be32(f, s->num_pages);
316 qemu_put_be32(f, s->actual);
317 }
318
319 static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id)
320 {
321 VirtIOBalloon *s = opaque;
322 int ret;
323
324 if (version_id != 1)
325 return -EINVAL;
326
327 ret = virtio_load(&s->vdev, f);
328 if (ret) {
329 return ret;
330 }
331
332 s->num_pages = qemu_get_be32(f);
333 s->actual = qemu_get_be32(f);
334 return 0;
335 }
336
337 VirtIODevice *virtio_balloon_init(DeviceState *dev)
338 {
339 VirtIOBalloon *s;
340 int ret;
341
342 s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
343 VIRTIO_ID_BALLOON,
344 8, sizeof(VirtIOBalloon));
345
346 s->vdev.get_config = virtio_balloon_get_config;
347 s->vdev.set_config = virtio_balloon_set_config;
348 s->vdev.get_features = virtio_balloon_get_features;
349
350 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
351 virtio_balloon_stat, s);
352 if (ret < 0) {
353 virtio_cleanup(&s->vdev);
354 return NULL;
355 }
356
357 s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
358 s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
359 s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
360
361 s->qdev = dev;
362 register_savevm(dev, "virtio-balloon", -1, 1,
363 virtio_balloon_save, virtio_balloon_load, s);
364
365 object_property_add(OBJECT(dev), "guest-stats", "guest statistics",
366 balloon_stats_get_all, NULL, NULL, s, NULL);
367
368 object_property_add(OBJECT(dev), "guest-stats-polling-interval", "int",
369 balloon_stats_get_poll_interval,
370 balloon_stats_set_poll_interval,
371 NULL, s, NULL);
372
373 return &s->vdev;
374 }
375
376 void virtio_balloon_exit(VirtIODevice *vdev)
377 {
378 VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev);
379
380 balloon_stats_destroy_timer(s);
381 qemu_remove_balloon_handler(s);
382 unregister_savevm(s->qdev, "virtio-balloon", s);
383 virtio_cleanup(vdev);
384 }