root / hw / virtio-balloon.c @ 3204db98
History | View | Annotate | Download (7.1 kB)
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 "iov.h" |
17 |
#include "qemu-common.h" |
18 |
#include "virtio.h" |
19 |
#include "pc.h" |
20 |
#include "cpu.h" |
21 |
#include "balloon.h" |
22 |
#include "virtio-balloon.h" |
23 |
#include "kvm.h" |
24 |
|
25 |
#if defined(__linux__)
|
26 |
#include <sys/mman.h> |
27 |
#endif
|
28 |
|
29 |
typedef struct VirtIOBalloon |
30 |
{ |
31 |
VirtIODevice vdev; |
32 |
VirtQueue *ivq, *dvq, *svq; |
33 |
uint32_t num_pages; |
34 |
uint32_t actual; |
35 |
uint64_t stats[VIRTIO_BALLOON_S_NR]; |
36 |
VirtQueueElement stats_vq_elem; |
37 |
size_t stats_vq_offset; |
38 |
DeviceState *qdev; |
39 |
} VirtIOBalloon; |
40 |
|
41 |
static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev)
|
42 |
{ |
43 |
return (VirtIOBalloon *)vdev;
|
44 |
} |
45 |
|
46 |
static void balloon_page(void *addr, int deflate) |
47 |
{ |
48 |
#if defined(__linux__)
|
49 |
if (!kvm_enabled() || kvm_has_sync_mmu())
|
50 |
qemu_madvise(addr, TARGET_PAGE_SIZE, |
51 |
deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED); |
52 |
#endif
|
53 |
} |
54 |
|
55 |
/*
|
56 |
* reset_stats - Mark all items in the stats array as unset
|
57 |
*
|
58 |
* This function needs to be called at device intialization and before
|
59 |
* before updating to a set of newly-generated stats. This will ensure that no
|
60 |
* stale values stick around in case the guest reports a subset of the supported
|
61 |
* statistics.
|
62 |
*/
|
63 |
static inline void reset_stats(VirtIOBalloon *dev) |
64 |
{ |
65 |
int i;
|
66 |
for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1); |
67 |
} |
68 |
|
69 |
static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
70 |
{ |
71 |
VirtIOBalloon *s = to_virtio_balloon(vdev); |
72 |
VirtQueueElement elem; |
73 |
|
74 |
while (virtqueue_pop(vq, &elem)) {
|
75 |
size_t offset = 0;
|
76 |
uint32_t pfn; |
77 |
|
78 |
while (iov_to_buf(elem.out_sg, elem.out_num, &pfn, offset, 4) == 4) { |
79 |
ram_addr_t pa; |
80 |
ram_addr_t addr; |
81 |
|
82 |
pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT; |
83 |
offset += 4;
|
84 |
|
85 |
addr = cpu_get_physical_page_desc(pa); |
86 |
if ((addr & ~TARGET_PAGE_MASK) != IO_MEM_RAM)
|
87 |
continue;
|
88 |
|
89 |
/* Using qemu_get_ram_ptr is bending the rules a bit, but
|
90 |
should be OK because we only want a single page. */
|
91 |
balloon_page(qemu_get_ram_ptr(addr), !!(vq == s->dvq)); |
92 |
} |
93 |
|
94 |
virtqueue_push(vq, &elem, offset); |
95 |
virtio_notify(vdev, vq); |
96 |
} |
97 |
} |
98 |
|
99 |
static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) |
100 |
{ |
101 |
VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev); |
102 |
VirtQueueElement *elem = &s->stats_vq_elem; |
103 |
VirtIOBalloonStat stat; |
104 |
size_t offset = 0;
|
105 |
|
106 |
if (!virtqueue_pop(vq, elem)) {
|
107 |
return;
|
108 |
} |
109 |
|
110 |
/* Initialize the stats to get rid of any stale values. This is only
|
111 |
* needed to handle the case where a guest supports fewer stats than it
|
112 |
* used to (ie. it has booted into an old kernel).
|
113 |
*/
|
114 |
reset_stats(s); |
115 |
|
116 |
while (iov_to_buf(elem->out_sg, elem->out_num, &stat, offset, sizeof(stat)) |
117 |
== sizeof(stat)) {
|
118 |
uint16_t tag = tswap16(stat.tag); |
119 |
uint64_t val = tswap64(stat.val); |
120 |
|
121 |
offset += sizeof(stat);
|
122 |
if (tag < VIRTIO_BALLOON_S_NR)
|
123 |
s->stats[tag] = val; |
124 |
} |
125 |
s->stats_vq_offset = offset; |
126 |
} |
127 |
|
128 |
static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) |
129 |
{ |
130 |
VirtIOBalloon *dev = to_virtio_balloon(vdev); |
131 |
struct virtio_balloon_config config;
|
132 |
|
133 |
config.num_pages = cpu_to_le32(dev->num_pages); |
134 |
config.actual = cpu_to_le32(dev->actual); |
135 |
|
136 |
memcpy(config_data, &config, 8);
|
137 |
} |
138 |
|
139 |
static void virtio_balloon_set_config(VirtIODevice *vdev, |
140 |
const uint8_t *config_data)
|
141 |
{ |
142 |
VirtIOBalloon *dev = to_virtio_balloon(vdev); |
143 |
struct virtio_balloon_config config;
|
144 |
memcpy(&config, config_data, 8);
|
145 |
dev->actual = le32_to_cpu(config.actual); |
146 |
} |
147 |
|
148 |
static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f)
|
149 |
{ |
150 |
f |= (1 << VIRTIO_BALLOON_F_STATS_VQ);
|
151 |
return f;
|
152 |
} |
153 |
|
154 |
static void virtio_balloon_stat(void *opaque, BalloonInfo *info) |
155 |
{ |
156 |
VirtIOBalloon *dev = opaque; |
157 |
|
158 |
#if 0
|
159 |
/* Disable guest-provided stats for now. For more details please check:
|
160 |
* https://bugzilla.redhat.com/show_bug.cgi?id=623903
|
161 |
*
|
162 |
* If you do enable it (which is probably not going to happen as we
|
163 |
* need a new command for it), remember that you also need to fill the
|
164 |
* appropriate members of the BalloonInfo structure so that the stats
|
165 |
* are returned to the client.
|
166 |
*/
|
167 |
if (dev->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ)) {
|
168 |
virtqueue_push(dev->svq, &dev->stats_vq_elem, dev->stats_vq_offset);
|
169 |
virtio_notify(&dev->vdev, dev->svq);
|
170 |
return;
|
171 |
}
|
172 |
#endif
|
173 |
|
174 |
/* Stats are not supported. Clear out any stale values that might
|
175 |
* have been set by a more featureful guest kernel.
|
176 |
*/
|
177 |
reset_stats(dev); |
178 |
|
179 |
info->actual = ram_size - ((uint64_t) dev->actual << |
180 |
VIRTIO_BALLOON_PFN_SHIFT); |
181 |
} |
182 |
|
183 |
static void virtio_balloon_to_target(void *opaque, ram_addr_t target) |
184 |
{ |
185 |
VirtIOBalloon *dev = opaque; |
186 |
|
187 |
if (target > ram_size) {
|
188 |
target = ram_size; |
189 |
} |
190 |
if (target) {
|
191 |
dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT; |
192 |
virtio_notify_config(&dev->vdev); |
193 |
} |
194 |
} |
195 |
|
196 |
static void virtio_balloon_save(QEMUFile *f, void *opaque) |
197 |
{ |
198 |
VirtIOBalloon *s = opaque; |
199 |
|
200 |
virtio_save(&s->vdev, f); |
201 |
|
202 |
qemu_put_be32(f, s->num_pages); |
203 |
qemu_put_be32(f, s->actual); |
204 |
} |
205 |
|
206 |
static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id) |
207 |
{ |
208 |
VirtIOBalloon *s = opaque; |
209 |
|
210 |
if (version_id != 1) |
211 |
return -EINVAL;
|
212 |
|
213 |
virtio_load(&s->vdev, f); |
214 |
|
215 |
s->num_pages = qemu_get_be32(f); |
216 |
s->actual = qemu_get_be32(f); |
217 |
return 0; |
218 |
} |
219 |
|
220 |
VirtIODevice *virtio_balloon_init(DeviceState *dev) |
221 |
{ |
222 |
VirtIOBalloon *s; |
223 |
int ret;
|
224 |
|
225 |
s = (VirtIOBalloon *)virtio_common_init("virtio-balloon",
|
226 |
VIRTIO_ID_BALLOON, |
227 |
8, sizeof(VirtIOBalloon)); |
228 |
|
229 |
s->vdev.get_config = virtio_balloon_get_config; |
230 |
s->vdev.set_config = virtio_balloon_set_config; |
231 |
s->vdev.get_features = virtio_balloon_get_features; |
232 |
|
233 |
ret = qemu_add_balloon_handler(virtio_balloon_to_target, |
234 |
virtio_balloon_stat, s); |
235 |
if (ret < 0) { |
236 |
virtio_cleanup(&s->vdev); |
237 |
return NULL; |
238 |
} |
239 |
|
240 |
s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
|
241 |
s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output);
|
242 |
s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats);
|
243 |
|
244 |
reset_stats(s); |
245 |
|
246 |
s->qdev = dev; |
247 |
register_savevm(dev, "virtio-balloon", -1, 1, |
248 |
virtio_balloon_save, virtio_balloon_load, s); |
249 |
|
250 |
return &s->vdev;
|
251 |
} |
252 |
|
253 |
void virtio_balloon_exit(VirtIODevice *vdev)
|
254 |
{ |
255 |
VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev); |
256 |
|
257 |
qemu_remove_balloon_handler(s); |
258 |
unregister_savevm(s->qdev, "virtio-balloon", s);
|
259 |
virtio_cleanup(vdev); |
260 |
} |