]>
Commit | Line | Data |
---|---|---|
bd322087 AL |
1 | /* |
2 | * Virtio Block Device | |
3 | * | |
4 | * Copyright IBM, Corp. 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
fa6111f2 | 14 | #include "iov.h" |
bd322087 AL |
15 | #include "qemu-common.h" |
16 | #include "virtio.h" | |
17 | #include "pc.h" | |
18 | #include "sysemu.h" | |
19 | #include "cpu.h" | |
625a5bef | 20 | #include "monitor.h" |
bd322087 AL |
21 | #include "balloon.h" |
22 | #include "virtio-balloon.h" | |
23 | #include "kvm.h" | |
625a5bef AL |
24 | #include "qlist.h" |
25 | #include "qint.h" | |
26 | #include "qstring.h" | |
bd322087 AL |
27 | |
28 | #if defined(__linux__) | |
29 | #include <sys/mman.h> | |
30 | #endif | |
31 | ||
07b0403d EH |
32 | /* Disable guest-provided stats by now (https://bugzilla.redhat.com/show_bug.cgi?id=623903) */ |
33 | #define ENABLE_GUEST_STATS 0 | |
34 | ||
35 | ||
bd322087 AL |
36 | typedef struct VirtIOBalloon |
37 | { | |
38 | VirtIODevice vdev; | |
625a5bef | 39 | VirtQueue *ivq, *dvq, *svq; |
bd322087 AL |
40 | uint32_t num_pages; |
41 | uint32_t actual; | |
625a5bef AL |
42 | uint64_t stats[VIRTIO_BALLOON_S_NR]; |
43 | VirtQueueElement stats_vq_elem; | |
44 | size_t stats_vq_offset; | |
45 | MonitorCompletion *stats_callback; | |
46 | void *stats_opaque_callback_data; | |
bd322087 AL |
47 | } VirtIOBalloon; |
48 | ||
49 | static VirtIOBalloon *to_virtio_balloon(VirtIODevice *vdev) | |
50 | { | |
51 | return (VirtIOBalloon *)vdev; | |
52 | } | |
53 | ||
54 | static void balloon_page(void *addr, int deflate) | |
55 | { | |
56 | #if defined(__linux__) | |
57 | if (!kvm_enabled() || kvm_has_sync_mmu()) | |
e78815a5 AF |
58 | qemu_madvise(addr, TARGET_PAGE_SIZE, |
59 | deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED); | |
bd322087 AL |
60 | #endif |
61 | } | |
62 | ||
625a5bef AL |
63 | /* |
64 | * reset_stats - Mark all items in the stats array as unset | |
65 | * | |
66 | * This function needs to be called at device intialization and before | |
67 | * before updating to a set of newly-generated stats. This will ensure that no | |
68 | * stale values stick around in case the guest reports a subset of the supported | |
69 | * statistics. | |
70 | */ | |
71 | static inline void reset_stats(VirtIOBalloon *dev) | |
72 | { | |
73 | int i; | |
74 | for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1); | |
75 | } | |
76 | ||
77 | static void stat_put(QDict *dict, const char *label, uint64_t val) | |
78 | { | |
79 | if (val != -1) | |
80 | qdict_put(dict, label, qint_from_int(val)); | |
81 | } | |
82 | ||
83 | static QObject *get_stats_qobject(VirtIOBalloon *dev) | |
84 | { | |
85 | QDict *dict = qdict_new(); | |
bd12ff9d AL |
86 | uint64_t actual = ram_size - ((uint64_t) dev->actual << |
87 | VIRTIO_BALLOON_PFN_SHIFT); | |
625a5bef AL |
88 | |
89 | stat_put(dict, "actual", actual); | |
07b0403d | 90 | #if ENABLE_GUEST_STATS |
625a5bef AL |
91 | stat_put(dict, "mem_swapped_in", dev->stats[VIRTIO_BALLOON_S_SWAP_IN]); |
92 | stat_put(dict, "mem_swapped_out", dev->stats[VIRTIO_BALLOON_S_SWAP_OUT]); | |
93 | stat_put(dict, "major_page_faults", dev->stats[VIRTIO_BALLOON_S_MAJFLT]); | |
94 | stat_put(dict, "minor_page_faults", dev->stats[VIRTIO_BALLOON_S_MINFLT]); | |
95 | stat_put(dict, "free_mem", dev->stats[VIRTIO_BALLOON_S_MEMFREE]); | |
96 | stat_put(dict, "total_mem", dev->stats[VIRTIO_BALLOON_S_MEMTOT]); | |
07b0403d | 97 | #endif |
625a5bef AL |
98 | |
99 | return QOBJECT(dict); | |
100 | } | |
101 | ||
bd322087 AL |
102 | static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
103 | { | |
104 | VirtIOBalloon *s = to_virtio_balloon(vdev); | |
105 | VirtQueueElement elem; | |
106 | ||
107 | while (virtqueue_pop(vq, &elem)) { | |
108 | size_t offset = 0; | |
109 | uint32_t pfn; | |
110 | ||
fa6111f2 | 111 | while (iov_to_buf(elem.out_sg, elem.out_num, &pfn, offset, 4) == 4) { |
c227f099 AL |
112 | ram_addr_t pa; |
113 | ram_addr_t addr; | |
bd322087 | 114 | |
c227f099 | 115 | pa = (ram_addr_t)ldl_p(&pfn) << VIRTIO_BALLOON_PFN_SHIFT; |
bd322087 AL |
116 | offset += 4; |
117 | ||
118 | addr = cpu_get_physical_page_desc(pa); | |
119 | if ((addr & ~TARGET_PAGE_MASK) != IO_MEM_RAM) | |
120 | continue; | |
121 | ||
5c130f65 PB |
122 | /* Using qemu_get_ram_ptr is bending the rules a bit, but |
123 | should be OK because we only want a single page. */ | |
124 | balloon_page(qemu_get_ram_ptr(addr), !!(vq == s->dvq)); | |
bd322087 AL |
125 | } |
126 | ||
127 | virtqueue_push(vq, &elem, offset); | |
128 | virtio_notify(vdev, vq); | |
129 | } | |
130 | } | |
131 | ||
625a5bef AL |
132 | static void complete_stats_request(VirtIOBalloon *vb) |
133 | { | |
134 | QObject *stats; | |
135 | ||
136 | if (!vb->stats_opaque_callback_data) | |
137 | return; | |
138 | ||
139 | stats = get_stats_qobject(vb); | |
140 | vb->stats_callback(vb->stats_opaque_callback_data, stats); | |
141 | qobject_decref(stats); | |
142 | vb->stats_opaque_callback_data = NULL; | |
143 | vb->stats_callback = NULL; | |
144 | } | |
145 | ||
146 | static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq) | |
147 | { | |
148 | VirtIOBalloon *s = DO_UPCAST(VirtIOBalloon, vdev, vdev); | |
149 | VirtQueueElement *elem = &s->stats_vq_elem; | |
150 | VirtIOBalloonStat stat; | |
151 | size_t offset = 0; | |
152 | ||
153 | if (!virtqueue_pop(vq, elem)) { | |
154 | return; | |
155 | } | |
156 | ||
157 | /* Initialize the stats to get rid of any stale values. This is only | |
158 | * needed to handle the case where a guest supports fewer stats than it | |
159 | * used to (ie. it has booted into an old kernel). | |
160 | */ | |
161 | reset_stats(s); | |
162 | ||
fa6111f2 AS |
163 | while (iov_to_buf(elem->out_sg, elem->out_num, &stat, offset, sizeof(stat)) |
164 | == sizeof(stat)) { | |
625a5bef AL |
165 | uint16_t tag = tswap16(stat.tag); |
166 | uint64_t val = tswap64(stat.val); | |
167 | ||
168 | offset += sizeof(stat); | |
169 | if (tag < VIRTIO_BALLOON_S_NR) | |
170 | s->stats[tag] = val; | |
171 | } | |
172 | s->stats_vq_offset = offset; | |
173 | ||
174 | complete_stats_request(s); | |
175 | } | |
176 | ||
bd322087 AL |
177 | static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data) |
178 | { | |
179 | VirtIOBalloon *dev = to_virtio_balloon(vdev); | |
180 | struct virtio_balloon_config config; | |
181 | ||
182 | config.num_pages = cpu_to_le32(dev->num_pages); | |
183 | config.actual = cpu_to_le32(dev->actual); | |
184 | ||
185 | memcpy(config_data, &config, 8); | |
186 | } | |
187 | ||
188 | static void virtio_balloon_set_config(VirtIODevice *vdev, | |
189 | const uint8_t *config_data) | |
190 | { | |
191 | VirtIOBalloon *dev = to_virtio_balloon(vdev); | |
192 | struct virtio_balloon_config config; | |
193 | memcpy(&config, config_data, 8); | |
194 | dev->actual = config.actual; | |
195 | } | |
196 | ||
8172539d | 197 | static uint32_t virtio_balloon_get_features(VirtIODevice *vdev, uint32_t f) |
bd322087 | 198 | { |
625a5bef | 199 | f |= (1 << VIRTIO_BALLOON_F_STATS_VQ); |
8172539d | 200 | return f; |
bd322087 AL |
201 | } |
202 | ||
625a5bef AL |
203 | static void virtio_balloon_to_target(void *opaque, ram_addr_t target, |
204 | MonitorCompletion cb, void *cb_data) | |
bd322087 AL |
205 | { |
206 | VirtIOBalloon *dev = opaque; | |
207 | ||
208 | if (target > ram_size) | |
209 | target = ram_size; | |
210 | ||
211 | if (target) { | |
212 | dev->num_pages = (ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT; | |
213 | virtio_notify_config(&dev->vdev); | |
625a5bef AL |
214 | } else { |
215 | /* For now, only allow one request at a time. This restriction can be | |
216 | * removed later by queueing callback and data pairs. | |
217 | */ | |
218 | if (dev->stats_callback != NULL) { | |
219 | return; | |
220 | } | |
221 | dev->stats_callback = cb; | |
222 | dev->stats_opaque_callback_data = cb_data; | |
07b0403d | 223 | if (ENABLE_GUEST_STATS && (dev->vdev.guest_features & (1 << VIRTIO_BALLOON_F_STATS_VQ))) { |
625a5bef AL |
224 | virtqueue_push(dev->svq, &dev->stats_vq_elem, dev->stats_vq_offset); |
225 | virtio_notify(&dev->vdev, dev->svq); | |
226 | } else { | |
227 | /* Stats are not supported. Clear out any stale values that might | |
228 | * have been set by a more featureful guest kernel. | |
229 | */ | |
230 | reset_stats(dev); | |
231 | complete_stats_request(dev); | |
232 | } | |
bd322087 | 233 | } |
bd322087 AL |
234 | } |
235 | ||
236 | static void virtio_balloon_save(QEMUFile *f, void *opaque) | |
237 | { | |
238 | VirtIOBalloon *s = opaque; | |
239 | ||
240 | virtio_save(&s->vdev, f); | |
241 | ||
242 | qemu_put_be32(f, s->num_pages); | |
243 | qemu_put_be32(f, s->actual); | |
244 | } | |
245 | ||
246 | static int virtio_balloon_load(QEMUFile *f, void *opaque, int version_id) | |
247 | { | |
248 | VirtIOBalloon *s = opaque; | |
249 | ||
250 | if (version_id != 1) | |
251 | return -EINVAL; | |
252 | ||
253 | virtio_load(&s->vdev, f); | |
254 | ||
255 | s->num_pages = qemu_get_be32(f); | |
256 | s->actual = qemu_get_be32(f); | |
bd322087 AL |
257 | return 0; |
258 | } | |
259 | ||
53c25cea | 260 | VirtIODevice *virtio_balloon_init(DeviceState *dev) |
bd322087 AL |
261 | { |
262 | VirtIOBalloon *s; | |
263 | ||
53c25cea PB |
264 | s = (VirtIOBalloon *)virtio_common_init("virtio-balloon", |
265 | VIRTIO_ID_BALLOON, | |
266 | 8, sizeof(VirtIOBalloon)); | |
bd322087 AL |
267 | |
268 | s->vdev.get_config = virtio_balloon_get_config; | |
269 | s->vdev.set_config = virtio_balloon_set_config; | |
270 | s->vdev.get_features = virtio_balloon_get_features; | |
271 | ||
272 | s->ivq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output); | |
273 | s->dvq = virtio_add_queue(&s->vdev, 128, virtio_balloon_handle_output); | |
625a5bef | 274 | s->svq = virtio_add_queue(&s->vdev, 128, virtio_balloon_receive_stats); |
bd322087 | 275 | |
625a5bef | 276 | reset_stats(s); |
bd322087 AL |
277 | qemu_add_balloon_handler(virtio_balloon_to_target, s); |
278 | ||
0be71e32 AW |
279 | register_savevm(dev, "virtio-balloon", -1, 1, |
280 | virtio_balloon_save, virtio_balloon_load, s); | |
bd322087 | 281 | |
53c25cea | 282 | return &s->vdev; |
bd322087 | 283 | } |